omap_hsmmc.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. /*
  2. * drivers/mmc/host/omap_hsmmc.c
  3. *
  4. * Driver for OMAP2430/3430 MMC controller.
  5. *
  6. * Copyright (C) 2007 Texas Instruments.
  7. *
  8. * Authors:
  9. * Syed Mohammed Khasim <x0khasim@ti.com>
  10. * Madhusudhan <madhu.cr@ti.com>
  11. * Mohit Jalori <mjalori@ti.com>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/sizes.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/timer.h>
  29. #include <linux/clk.h>
  30. #include <linux/of.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_device.h>
  34. #include <linux/mmc/host.h>
  35. #include <linux/mmc/core.h>
  36. #include <linux/mmc/mmc.h>
  37. #include <linux/mmc/slot-gpio.h>
  38. #include <linux/io.h>
  39. #include <linux/irq.h>
  40. #include <linux/gpio.h>
  41. #include <linux/regulator/consumer.h>
  42. #include <linux/pinctrl/consumer.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/pm_wakeirq.h>
  45. #include <linux/platform_data/hsmmc-omap.h>
  46. /* OMAP HSMMC Host Controller Registers */
  47. #define OMAP_HSMMC_SYSSTATUS 0x0014
  48. #define OMAP_HSMMC_CON 0x002C
  49. #define OMAP_HSMMC_SDMASA 0x0100
  50. #define OMAP_HSMMC_BLK 0x0104
  51. #define OMAP_HSMMC_ARG 0x0108
  52. #define OMAP_HSMMC_CMD 0x010C
  53. #define OMAP_HSMMC_RSP10 0x0110
  54. #define OMAP_HSMMC_RSP32 0x0114
  55. #define OMAP_HSMMC_RSP54 0x0118
  56. #define OMAP_HSMMC_RSP76 0x011C
  57. #define OMAP_HSMMC_DATA 0x0120
  58. #define OMAP_HSMMC_PSTATE 0x0124
  59. #define OMAP_HSMMC_HCTL 0x0128
  60. #define OMAP_HSMMC_SYSCTL 0x012C
  61. #define OMAP_HSMMC_STAT 0x0130
  62. #define OMAP_HSMMC_IE 0x0134
  63. #define OMAP_HSMMC_ISE 0x0138
  64. #define OMAP_HSMMC_AC12 0x013C
  65. #define OMAP_HSMMC_CAPA 0x0140
  66. #define VS18 (1 << 26)
  67. #define VS30 (1 << 25)
  68. #define HSS (1 << 21)
  69. #define SDVS18 (0x5 << 9)
  70. #define SDVS30 (0x6 << 9)
  71. #define SDVS33 (0x7 << 9)
  72. #define SDVS_MASK 0x00000E00
  73. #define SDVSCLR 0xFFFFF1FF
  74. #define SDVSDET 0x00000400
  75. #define AUTOIDLE 0x1
  76. #define SDBP (1 << 8)
  77. #define DTO 0xe
  78. #define ICE 0x1
  79. #define ICS 0x2
  80. #define CEN (1 << 2)
  81. #define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
  82. #define CLKD_MASK 0x0000FFC0
  83. #define CLKD_SHIFT 6
  84. #define DTO_MASK 0x000F0000
  85. #define DTO_SHIFT 16
  86. #define INIT_STREAM (1 << 1)
  87. #define ACEN_ACMD23 (2 << 2)
  88. #define DP_SELECT (1 << 21)
  89. #define DDIR (1 << 4)
  90. #define DMAE 0x1
  91. #define MSBS (1 << 5)
  92. #define BCE (1 << 1)
  93. #define FOUR_BIT (1 << 1)
  94. #define HSPE (1 << 2)
  95. #define IWE (1 << 24)
  96. #define DDR (1 << 19)
  97. #define CLKEXTFREE (1 << 16)
  98. #define CTPL (1 << 11)
  99. #define DW8 (1 << 5)
  100. #define OD 0x1
  101. #define STAT_CLEAR 0xFFFFFFFF
  102. #define INIT_STREAM_CMD 0x00000000
  103. #define DUAL_VOLT_OCR_BIT 7
  104. #define SRC (1 << 25)
  105. #define SRD (1 << 26)
  106. #define SOFTRESET (1 << 1)
  107. /* PSTATE */
  108. #define DLEV_DAT(x) (1 << (20 + (x)))
  109. /* Interrupt masks for IE and ISE register */
  110. #define CC_EN (1 << 0)
  111. #define TC_EN (1 << 1)
  112. #define BWR_EN (1 << 4)
  113. #define BRR_EN (1 << 5)
  114. #define CIRQ_EN (1 << 8)
  115. #define ERR_EN (1 << 15)
  116. #define CTO_EN (1 << 16)
  117. #define CCRC_EN (1 << 17)
  118. #define CEB_EN (1 << 18)
  119. #define CIE_EN (1 << 19)
  120. #define DTO_EN (1 << 20)
  121. #define DCRC_EN (1 << 21)
  122. #define DEB_EN (1 << 22)
  123. #define ACE_EN (1 << 24)
  124. #define CERR_EN (1 << 28)
  125. #define BADA_EN (1 << 29)
  126. #define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
  127. DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
  128. BRR_EN | BWR_EN | TC_EN | CC_EN)
  129. #define CNI (1 << 7)
  130. #define ACIE (1 << 4)
  131. #define ACEB (1 << 3)
  132. #define ACCE (1 << 2)
  133. #define ACTO (1 << 1)
  134. #define ACNE (1 << 0)
  135. #define MMC_AUTOSUSPEND_DELAY 100
  136. #define MMC_TIMEOUT_MS 20 /* 20 mSec */
  137. #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
  138. #define OMAP_MMC_MIN_CLOCK 400000
  139. #define OMAP_MMC_MAX_CLOCK 52000000
  140. #define DRIVER_NAME "omap_hsmmc"
  141. /*
  142. * One controller can have multiple slots, like on some omap boards using
  143. * omap.c controller driver. Luckily this is not currently done on any known
  144. * omap_hsmmc.c device.
  145. */
  146. #define mmc_pdata(host) host->pdata
  147. /*
  148. * MMC Host controller read/write API's
  149. */
  150. #define OMAP_HSMMC_READ(base, reg) \
  151. __raw_readl((base) + OMAP_HSMMC_##reg)
  152. #define OMAP_HSMMC_WRITE(base, reg, val) \
  153. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  154. struct omap_hsmmc_next {
  155. unsigned int dma_len;
  156. s32 cookie;
  157. };
  158. struct omap_hsmmc_host {
  159. struct device *dev;
  160. struct mmc_host *mmc;
  161. struct mmc_request *mrq;
  162. struct mmc_command *cmd;
  163. struct mmc_data *data;
  164. struct clk *fclk;
  165. struct clk *dbclk;
  166. struct regulator *pbias;
  167. bool pbias_enabled;
  168. void __iomem *base;
  169. int vqmmc_enabled;
  170. resource_size_t mapbase;
  171. spinlock_t irq_lock; /* Prevent races with irq handler */
  172. unsigned int dma_len;
  173. unsigned int dma_sg_idx;
  174. unsigned char bus_mode;
  175. unsigned char power_mode;
  176. int suspended;
  177. u32 con;
  178. u32 hctl;
  179. u32 sysctl;
  180. u32 capa;
  181. int irq;
  182. int wake_irq;
  183. int use_dma, dma_ch;
  184. struct dma_chan *tx_chan;
  185. struct dma_chan *rx_chan;
  186. int response_busy;
  187. int context_loss;
  188. int protect_card;
  189. int reqs_blocked;
  190. int req_in_progress;
  191. unsigned long clk_rate;
  192. unsigned int flags;
  193. #define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
  194. #define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
  195. struct omap_hsmmc_next next_data;
  196. struct omap_hsmmc_platform_data *pdata;
  197. /* return MMC cover switch state, can be NULL if not supported.
  198. *
  199. * possible return values:
  200. * 0 - closed
  201. * 1 - open
  202. */
  203. int (*get_cover_state)(struct device *dev);
  204. int (*card_detect)(struct device *dev);
  205. };
  206. struct omap_mmc_of_data {
  207. u32 reg_offset;
  208. u8 controller_flags;
  209. };
  210. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
  211. static int omap_hsmmc_card_detect(struct device *dev)
  212. {
  213. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  214. return mmc_gpio_get_cd(host->mmc);
  215. }
  216. static int omap_hsmmc_get_cover_state(struct device *dev)
  217. {
  218. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  219. return mmc_gpio_get_cd(host->mmc);
  220. }
  221. static int omap_hsmmc_enable_supply(struct mmc_host *mmc)
  222. {
  223. int ret;
  224. struct omap_hsmmc_host *host = mmc_priv(mmc);
  225. struct mmc_ios *ios = &mmc->ios;
  226. if (!IS_ERR(mmc->supply.vmmc)) {
  227. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  228. if (ret)
  229. return ret;
  230. }
  231. /* Enable interface voltage rail, if needed */
  232. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  233. ret = regulator_enable(mmc->supply.vqmmc);
  234. if (ret) {
  235. dev_err(mmc_dev(mmc), "vmmc_aux reg enable failed\n");
  236. goto err_vqmmc;
  237. }
  238. host->vqmmc_enabled = 1;
  239. }
  240. return 0;
  241. err_vqmmc:
  242. if (!IS_ERR(mmc->supply.vmmc))
  243. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  244. return ret;
  245. }
  246. static int omap_hsmmc_disable_supply(struct mmc_host *mmc)
  247. {
  248. int ret;
  249. int status;
  250. struct omap_hsmmc_host *host = mmc_priv(mmc);
  251. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  252. ret = regulator_disable(mmc->supply.vqmmc);
  253. if (ret) {
  254. dev_err(mmc_dev(mmc), "vmmc_aux reg disable failed\n");
  255. return ret;
  256. }
  257. host->vqmmc_enabled = 0;
  258. }
  259. if (!IS_ERR(mmc->supply.vmmc)) {
  260. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  261. if (ret)
  262. goto err_set_ocr;
  263. }
  264. return 0;
  265. err_set_ocr:
  266. if (!IS_ERR(mmc->supply.vqmmc)) {
  267. status = regulator_enable(mmc->supply.vqmmc);
  268. if (status)
  269. dev_err(mmc_dev(mmc), "vmmc_aux re-enable failed\n");
  270. }
  271. return ret;
  272. }
  273. static int omap_hsmmc_set_pbias(struct omap_hsmmc_host *host, bool power_on)
  274. {
  275. int ret;
  276. if (IS_ERR(host->pbias))
  277. return 0;
  278. if (power_on) {
  279. if (host->pbias_enabled == 0) {
  280. ret = regulator_enable(host->pbias);
  281. if (ret) {
  282. dev_err(host->dev, "pbias reg enable fail\n");
  283. return ret;
  284. }
  285. host->pbias_enabled = 1;
  286. }
  287. } else {
  288. if (host->pbias_enabled == 1) {
  289. ret = regulator_disable(host->pbias);
  290. if (ret) {
  291. dev_err(host->dev, "pbias reg disable fail\n");
  292. return ret;
  293. }
  294. host->pbias_enabled = 0;
  295. }
  296. }
  297. return 0;
  298. }
  299. static int omap_hsmmc_set_power(struct omap_hsmmc_host *host, int power_on)
  300. {
  301. struct mmc_host *mmc = host->mmc;
  302. int ret = 0;
  303. /*
  304. * If we don't see a Vcc regulator, assume it's a fixed
  305. * voltage always-on regulator.
  306. */
  307. if (IS_ERR(mmc->supply.vmmc))
  308. return 0;
  309. ret = omap_hsmmc_set_pbias(host, false);
  310. if (ret)
  311. return ret;
  312. /*
  313. * Assume Vcc regulator is used only to power the card ... OMAP
  314. * VDDS is used to power the pins, optionally with a transceiver to
  315. * support cards using voltages other than VDDS (1.8V nominal). When a
  316. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  317. *
  318. * In some cases this regulator won't support enable/disable;
  319. * e.g. it's a fixed rail for a WLAN chip.
  320. *
  321. * In other cases vcc_aux switches interface power. Example, for
  322. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  323. * chips/cards need an interface voltage rail too.
  324. */
  325. if (power_on) {
  326. ret = omap_hsmmc_enable_supply(mmc);
  327. if (ret)
  328. return ret;
  329. ret = omap_hsmmc_set_pbias(host, true);
  330. if (ret)
  331. goto err_set_voltage;
  332. } else {
  333. ret = omap_hsmmc_disable_supply(mmc);
  334. if (ret)
  335. return ret;
  336. }
  337. return 0;
  338. err_set_voltage:
  339. omap_hsmmc_disable_supply(mmc);
  340. return ret;
  341. }
  342. static int omap_hsmmc_disable_boot_regulator(struct regulator *reg)
  343. {
  344. int ret;
  345. if (IS_ERR(reg))
  346. return 0;
  347. if (regulator_is_enabled(reg)) {
  348. ret = regulator_enable(reg);
  349. if (ret)
  350. return ret;
  351. ret = regulator_disable(reg);
  352. if (ret)
  353. return ret;
  354. }
  355. return 0;
  356. }
  357. static int omap_hsmmc_disable_boot_regulators(struct omap_hsmmc_host *host)
  358. {
  359. struct mmc_host *mmc = host->mmc;
  360. int ret;
  361. /*
  362. * disable regulators enabled during boot and get the usecount
  363. * right so that regulators can be enabled/disabled by checking
  364. * the return value of regulator_is_enabled
  365. */
  366. ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vmmc);
  367. if (ret) {
  368. dev_err(host->dev, "fail to disable boot enabled vmmc reg\n");
  369. return ret;
  370. }
  371. ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vqmmc);
  372. if (ret) {
  373. dev_err(host->dev,
  374. "fail to disable boot enabled vmmc_aux reg\n");
  375. return ret;
  376. }
  377. ret = omap_hsmmc_disable_boot_regulator(host->pbias);
  378. if (ret) {
  379. dev_err(host->dev,
  380. "failed to disable boot enabled pbias reg\n");
  381. return ret;
  382. }
  383. return 0;
  384. }
  385. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  386. {
  387. int ret;
  388. struct mmc_host *mmc = host->mmc;
  389. ret = mmc_regulator_get_supply(mmc);
  390. if (ret)
  391. return ret;
  392. /* Allow an aux regulator */
  393. if (IS_ERR(mmc->supply.vqmmc)) {
  394. mmc->supply.vqmmc = devm_regulator_get_optional(host->dev,
  395. "vmmc_aux");
  396. if (IS_ERR(mmc->supply.vqmmc)) {
  397. ret = PTR_ERR(mmc->supply.vqmmc);
  398. if ((ret != -ENODEV) && host->dev->of_node)
  399. return ret;
  400. dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
  401. PTR_ERR(mmc->supply.vqmmc));
  402. }
  403. }
  404. host->pbias = devm_regulator_get_optional(host->dev, "pbias");
  405. if (IS_ERR(host->pbias)) {
  406. ret = PTR_ERR(host->pbias);
  407. if ((ret != -ENODEV) && host->dev->of_node) {
  408. dev_err(host->dev,
  409. "SD card detect fail? enable CONFIG_REGULATOR_PBIAS\n");
  410. return ret;
  411. }
  412. dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
  413. PTR_ERR(host->pbias));
  414. }
  415. /* For eMMC do not power off when not in sleep state */
  416. if (mmc_pdata(host)->no_regulator_off_init)
  417. return 0;
  418. ret = omap_hsmmc_disable_boot_regulators(host);
  419. if (ret)
  420. return ret;
  421. return 0;
  422. }
  423. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
  424. static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
  425. struct omap_hsmmc_host *host,
  426. struct omap_hsmmc_platform_data *pdata)
  427. {
  428. int ret;
  429. if (gpio_is_valid(pdata->gpio_cod)) {
  430. ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
  431. if (ret)
  432. return ret;
  433. host->get_cover_state = omap_hsmmc_get_cover_state;
  434. mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
  435. } else if (gpio_is_valid(pdata->gpio_cd)) {
  436. ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
  437. if (ret)
  438. return ret;
  439. host->card_detect = omap_hsmmc_card_detect;
  440. }
  441. if (gpio_is_valid(pdata->gpio_wp)) {
  442. ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
  443. if (ret)
  444. return ret;
  445. }
  446. return 0;
  447. }
  448. /*
  449. * Start clock to the card
  450. */
  451. static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
  452. {
  453. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  454. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  455. }
  456. /*
  457. * Stop clock to the card
  458. */
  459. static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
  460. {
  461. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  462. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  463. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  464. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
  465. }
  466. static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
  467. struct mmc_command *cmd)
  468. {
  469. u32 irq_mask = INT_EN_MASK;
  470. unsigned long flags;
  471. if (host->use_dma)
  472. irq_mask &= ~(BRR_EN | BWR_EN);
  473. /* Disable timeout for erases */
  474. if (cmd->opcode == MMC_ERASE)
  475. irq_mask &= ~DTO_EN;
  476. spin_lock_irqsave(&host->irq_lock, flags);
  477. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  478. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  479. /* latch pending CIRQ, but don't signal MMC core */
  480. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  481. irq_mask |= CIRQ_EN;
  482. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  483. spin_unlock_irqrestore(&host->irq_lock, flags);
  484. }
  485. static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
  486. {
  487. u32 irq_mask = 0;
  488. unsigned long flags;
  489. spin_lock_irqsave(&host->irq_lock, flags);
  490. /* no transfer running but need to keep cirq if enabled */
  491. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  492. irq_mask |= CIRQ_EN;
  493. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  494. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  495. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  496. spin_unlock_irqrestore(&host->irq_lock, flags);
  497. }
  498. /* Calculate divisor for the given clock frequency */
  499. static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
  500. {
  501. u16 dsor = 0;
  502. if (ios->clock) {
  503. dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
  504. if (dsor > CLKD_MAX)
  505. dsor = CLKD_MAX;
  506. }
  507. return dsor;
  508. }
  509. static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
  510. {
  511. struct mmc_ios *ios = &host->mmc->ios;
  512. unsigned long regval;
  513. unsigned long timeout;
  514. unsigned long clkdiv;
  515. dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
  516. omap_hsmmc_stop_clock(host);
  517. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  518. regval = regval & ~(CLKD_MASK | DTO_MASK);
  519. clkdiv = calc_divisor(host, ios);
  520. regval = regval | (clkdiv << 6) | (DTO << 16);
  521. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  522. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  523. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  524. /* Wait till the ICS bit is set */
  525. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  526. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  527. && time_before(jiffies, timeout))
  528. cpu_relax();
  529. /*
  530. * Enable High-Speed Support
  531. * Pre-Requisites
  532. * - Controller should support High-Speed-Enable Bit
  533. * - Controller should not be using DDR Mode
  534. * - Controller should advertise that it supports High Speed
  535. * in capabilities register
  536. * - MMC/SD clock coming out of controller > 25MHz
  537. */
  538. if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
  539. (ios->timing != MMC_TIMING_MMC_DDR52) &&
  540. (ios->timing != MMC_TIMING_UHS_DDR50) &&
  541. ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
  542. regval = OMAP_HSMMC_READ(host->base, HCTL);
  543. if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
  544. regval |= HSPE;
  545. else
  546. regval &= ~HSPE;
  547. OMAP_HSMMC_WRITE(host->base, HCTL, regval);
  548. }
  549. omap_hsmmc_start_clock(host);
  550. }
  551. static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
  552. {
  553. struct mmc_ios *ios = &host->mmc->ios;
  554. u32 con;
  555. con = OMAP_HSMMC_READ(host->base, CON);
  556. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  557. ios->timing == MMC_TIMING_UHS_DDR50)
  558. con |= DDR; /* configure in DDR mode */
  559. else
  560. con &= ~DDR;
  561. switch (ios->bus_width) {
  562. case MMC_BUS_WIDTH_8:
  563. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  564. break;
  565. case MMC_BUS_WIDTH_4:
  566. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  567. OMAP_HSMMC_WRITE(host->base, HCTL,
  568. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  569. break;
  570. case MMC_BUS_WIDTH_1:
  571. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  572. OMAP_HSMMC_WRITE(host->base, HCTL,
  573. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  574. break;
  575. }
  576. }
  577. static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
  578. {
  579. struct mmc_ios *ios = &host->mmc->ios;
  580. u32 con;
  581. con = OMAP_HSMMC_READ(host->base, CON);
  582. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  583. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  584. else
  585. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  586. }
  587. #ifdef CONFIG_PM
  588. /*
  589. * Restore the MMC host context, if it was lost as result of a
  590. * power state change.
  591. */
  592. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  593. {
  594. struct mmc_ios *ios = &host->mmc->ios;
  595. u32 hctl, capa;
  596. unsigned long timeout;
  597. if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
  598. host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
  599. host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
  600. host->capa == OMAP_HSMMC_READ(host->base, CAPA))
  601. return 0;
  602. host->context_loss++;
  603. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  604. if (host->power_mode != MMC_POWER_OFF &&
  605. (1 << ios->vdd) <= MMC_VDD_23_24)
  606. hctl = SDVS18;
  607. else
  608. hctl = SDVS30;
  609. capa = VS30 | VS18;
  610. } else {
  611. hctl = SDVS18;
  612. capa = VS18;
  613. }
  614. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  615. hctl |= IWE;
  616. OMAP_HSMMC_WRITE(host->base, HCTL,
  617. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  618. OMAP_HSMMC_WRITE(host->base, CAPA,
  619. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  620. OMAP_HSMMC_WRITE(host->base, HCTL,
  621. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  622. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  623. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  624. && time_before(jiffies, timeout))
  625. ;
  626. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  627. OMAP_HSMMC_WRITE(host->base, IE, 0);
  628. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  629. /* Do not initialize card-specific things if the power is off */
  630. if (host->power_mode == MMC_POWER_OFF)
  631. goto out;
  632. omap_hsmmc_set_bus_width(host);
  633. omap_hsmmc_set_clock(host);
  634. omap_hsmmc_set_bus_mode(host);
  635. out:
  636. dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
  637. host->context_loss);
  638. return 0;
  639. }
  640. /*
  641. * Save the MMC host context (store the number of power state changes so far).
  642. */
  643. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  644. {
  645. host->con = OMAP_HSMMC_READ(host->base, CON);
  646. host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
  647. host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
  648. host->capa = OMAP_HSMMC_READ(host->base, CAPA);
  649. }
  650. #else
  651. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  652. {
  653. return 0;
  654. }
  655. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  656. {
  657. }
  658. #endif
  659. /*
  660. * Send init stream sequence to card
  661. * before sending IDLE command
  662. */
  663. static void send_init_stream(struct omap_hsmmc_host *host)
  664. {
  665. int reg = 0;
  666. unsigned long timeout;
  667. if (host->protect_card)
  668. return;
  669. disable_irq(host->irq);
  670. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  671. OMAP_HSMMC_WRITE(host->base, CON,
  672. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  673. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  674. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  675. while ((reg != CC_EN) && time_before(jiffies, timeout))
  676. reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
  677. OMAP_HSMMC_WRITE(host->base, CON,
  678. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  679. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  680. OMAP_HSMMC_READ(host->base, STAT);
  681. enable_irq(host->irq);
  682. }
  683. static inline
  684. int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
  685. {
  686. int r = 1;
  687. if (host->get_cover_state)
  688. r = host->get_cover_state(host->dev);
  689. return r;
  690. }
  691. static ssize_t
  692. omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
  693. char *buf)
  694. {
  695. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  696. struct omap_hsmmc_host *host = mmc_priv(mmc);
  697. return sprintf(buf, "%s\n",
  698. omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
  699. }
  700. static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
  701. static ssize_t
  702. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  703. char *buf)
  704. {
  705. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  706. struct omap_hsmmc_host *host = mmc_priv(mmc);
  707. return sprintf(buf, "%s\n", mmc_pdata(host)->name);
  708. }
  709. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  710. /*
  711. * Configure the response type and send the cmd.
  712. */
  713. static void
  714. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  715. struct mmc_data *data)
  716. {
  717. int cmdreg = 0, resptype = 0, cmdtype = 0;
  718. dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  719. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  720. host->cmd = cmd;
  721. omap_hsmmc_enable_irq(host, cmd);
  722. host->response_busy = 0;
  723. if (cmd->flags & MMC_RSP_PRESENT) {
  724. if (cmd->flags & MMC_RSP_136)
  725. resptype = 1;
  726. else if (cmd->flags & MMC_RSP_BUSY) {
  727. resptype = 3;
  728. host->response_busy = 1;
  729. } else
  730. resptype = 2;
  731. }
  732. /*
  733. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  734. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  735. * a val of 0x3, rest 0x0.
  736. */
  737. if (cmd == host->mrq->stop)
  738. cmdtype = 0x3;
  739. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  740. if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
  741. host->mrq->sbc) {
  742. cmdreg |= ACEN_ACMD23;
  743. OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
  744. }
  745. if (data) {
  746. cmdreg |= DP_SELECT | MSBS | BCE;
  747. if (data->flags & MMC_DATA_READ)
  748. cmdreg |= DDIR;
  749. else
  750. cmdreg &= ~(DDIR);
  751. }
  752. if (host->use_dma)
  753. cmdreg |= DMAE;
  754. host->req_in_progress = 1;
  755. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  756. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  757. }
  758. static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
  759. struct mmc_data *data)
  760. {
  761. return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
  762. }
  763. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  764. {
  765. int dma_ch;
  766. unsigned long flags;
  767. spin_lock_irqsave(&host->irq_lock, flags);
  768. host->req_in_progress = 0;
  769. dma_ch = host->dma_ch;
  770. spin_unlock_irqrestore(&host->irq_lock, flags);
  771. omap_hsmmc_disable_irq(host);
  772. /* Do not complete the request if DMA is still in progress */
  773. if (mrq->data && host->use_dma && dma_ch != -1)
  774. return;
  775. host->mrq = NULL;
  776. mmc_request_done(host->mmc, mrq);
  777. }
  778. /*
  779. * Notify the transfer complete to MMC core
  780. */
  781. static void
  782. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  783. {
  784. if (!data) {
  785. struct mmc_request *mrq = host->mrq;
  786. /* TC before CC from CMD6 - don't know why, but it happens */
  787. if (host->cmd && host->cmd->opcode == 6 &&
  788. host->response_busy) {
  789. host->response_busy = 0;
  790. return;
  791. }
  792. omap_hsmmc_request_done(host, mrq);
  793. return;
  794. }
  795. host->data = NULL;
  796. if (!data->error)
  797. data->bytes_xfered += data->blocks * (data->blksz);
  798. else
  799. data->bytes_xfered = 0;
  800. if (data->stop && (data->error || !host->mrq->sbc))
  801. omap_hsmmc_start_command(host, data->stop, NULL);
  802. else
  803. omap_hsmmc_request_done(host, data->mrq);
  804. }
  805. /*
  806. * Notify the core about command completion
  807. */
  808. static void
  809. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  810. {
  811. if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
  812. !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
  813. host->cmd = NULL;
  814. omap_hsmmc_start_dma_transfer(host);
  815. omap_hsmmc_start_command(host, host->mrq->cmd,
  816. host->mrq->data);
  817. return;
  818. }
  819. host->cmd = NULL;
  820. if (cmd->flags & MMC_RSP_PRESENT) {
  821. if (cmd->flags & MMC_RSP_136) {
  822. /* response type 2 */
  823. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  824. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  825. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  826. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  827. } else {
  828. /* response types 1, 1b, 3, 4, 5, 6 */
  829. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  830. }
  831. }
  832. if ((host->data == NULL && !host->response_busy) || cmd->error)
  833. omap_hsmmc_request_done(host, host->mrq);
  834. }
  835. /*
  836. * DMA clean up for command errors
  837. */
  838. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  839. {
  840. int dma_ch;
  841. unsigned long flags;
  842. host->data->error = errno;
  843. spin_lock_irqsave(&host->irq_lock, flags);
  844. dma_ch = host->dma_ch;
  845. host->dma_ch = -1;
  846. spin_unlock_irqrestore(&host->irq_lock, flags);
  847. if (host->use_dma && dma_ch != -1) {
  848. struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
  849. dmaengine_terminate_all(chan);
  850. dma_unmap_sg(chan->device->dev,
  851. host->data->sg, host->data->sg_len,
  852. mmc_get_dma_dir(host->data));
  853. host->data->host_cookie = 0;
  854. }
  855. host->data = NULL;
  856. }
  857. /*
  858. * Readable error output
  859. */
  860. #ifdef CONFIG_MMC_DEBUG
  861. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  862. {
  863. /* --- means reserved bit without definition at documentation */
  864. static const char *omap_hsmmc_status_bits[] = {
  865. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  866. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  867. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  868. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  869. };
  870. char res[256];
  871. char *buf = res;
  872. int len, i;
  873. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  874. buf += len;
  875. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  876. if (status & (1 << i)) {
  877. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  878. buf += len;
  879. }
  880. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  881. }
  882. #else
  883. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  884. u32 status)
  885. {
  886. }
  887. #endif /* CONFIG_MMC_DEBUG */
  888. /*
  889. * MMC controller internal state machines reset
  890. *
  891. * Used to reset command or data internal state machines, using respectively
  892. * SRC or SRD bit of SYSCTL register
  893. * Can be called from interrupt context
  894. */
  895. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  896. unsigned long bit)
  897. {
  898. unsigned long i = 0;
  899. unsigned long limit = MMC_TIMEOUT_US;
  900. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  901. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  902. /*
  903. * OMAP4 ES2 and greater has an updated reset logic.
  904. * Monitor a 0->1 transition first
  905. */
  906. if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
  907. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  908. && (i++ < limit))
  909. udelay(1);
  910. }
  911. i = 0;
  912. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  913. (i++ < limit))
  914. udelay(1);
  915. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  916. dev_err(mmc_dev(host->mmc),
  917. "Timeout waiting on controller reset in %s\n",
  918. __func__);
  919. }
  920. static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
  921. int err, int end_cmd)
  922. {
  923. if (end_cmd) {
  924. omap_hsmmc_reset_controller_fsm(host, SRC);
  925. if (host->cmd)
  926. host->cmd->error = err;
  927. }
  928. if (host->data) {
  929. omap_hsmmc_reset_controller_fsm(host, SRD);
  930. omap_hsmmc_dma_cleanup(host, err);
  931. } else if (host->mrq && host->mrq->cmd)
  932. host->mrq->cmd->error = err;
  933. }
  934. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  935. {
  936. struct mmc_data *data;
  937. int end_cmd = 0, end_trans = 0;
  938. int error = 0;
  939. data = host->data;
  940. dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  941. if (status & ERR_EN) {
  942. omap_hsmmc_dbg_report_irq(host, status);
  943. if (status & (CTO_EN | CCRC_EN | CEB_EN))
  944. end_cmd = 1;
  945. if (host->data || host->response_busy) {
  946. end_trans = !end_cmd;
  947. host->response_busy = 0;
  948. }
  949. if (status & (CTO_EN | DTO_EN))
  950. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  951. else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
  952. BADA_EN))
  953. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  954. if (status & ACE_EN) {
  955. u32 ac12;
  956. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  957. if (!(ac12 & ACNE) && host->mrq->sbc) {
  958. end_cmd = 1;
  959. if (ac12 & ACTO)
  960. error = -ETIMEDOUT;
  961. else if (ac12 & (ACCE | ACEB | ACIE))
  962. error = -EILSEQ;
  963. host->mrq->sbc->error = error;
  964. hsmmc_command_incomplete(host, error, end_cmd);
  965. }
  966. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  967. }
  968. }
  969. OMAP_HSMMC_WRITE(host->base, STAT, status);
  970. if (end_cmd || ((status & CC_EN) && host->cmd))
  971. omap_hsmmc_cmd_done(host, host->cmd);
  972. if ((end_trans || (status & TC_EN)) && host->mrq)
  973. omap_hsmmc_xfer_done(host, data);
  974. }
  975. /*
  976. * MMC controller IRQ handler
  977. */
  978. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  979. {
  980. struct omap_hsmmc_host *host = dev_id;
  981. int status;
  982. status = OMAP_HSMMC_READ(host->base, STAT);
  983. while (status & (INT_EN_MASK | CIRQ_EN)) {
  984. if (host->req_in_progress)
  985. omap_hsmmc_do_irq(host, status);
  986. if (status & CIRQ_EN)
  987. mmc_signal_sdio_irq(host->mmc);
  988. /* Flush posted write */
  989. status = OMAP_HSMMC_READ(host->base, STAT);
  990. }
  991. return IRQ_HANDLED;
  992. }
  993. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  994. {
  995. unsigned long i;
  996. OMAP_HSMMC_WRITE(host->base, HCTL,
  997. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  998. for (i = 0; i < loops_per_jiffy; i++) {
  999. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  1000. break;
  1001. cpu_relax();
  1002. }
  1003. }
  1004. /*
  1005. * Switch MMC interface voltage ... only relevant for MMC1.
  1006. *
  1007. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  1008. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  1009. * Some chips, like eMMC ones, use internal transceivers.
  1010. */
  1011. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  1012. {
  1013. u32 reg_val = 0;
  1014. int ret;
  1015. /* Disable the clocks */
  1016. if (host->dbclk)
  1017. clk_disable_unprepare(host->dbclk);
  1018. /* Turn the power off */
  1019. ret = omap_hsmmc_set_power(host, 0);
  1020. /* Turn the power ON with given VDD 1.8 or 3.0v */
  1021. if (!ret)
  1022. ret = omap_hsmmc_set_power(host, 1);
  1023. if (host->dbclk)
  1024. clk_prepare_enable(host->dbclk);
  1025. if (ret != 0)
  1026. goto err;
  1027. OMAP_HSMMC_WRITE(host->base, HCTL,
  1028. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  1029. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  1030. /*
  1031. * If a MMC dual voltage card is detected, the set_ios fn calls
  1032. * this fn with VDD bit set for 1.8V. Upon card removal from the
  1033. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  1034. *
  1035. * Cope with a bit of slop in the range ... per data sheets:
  1036. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  1037. * but recommended values are 1.71V to 1.89V
  1038. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1039. * but recommended values are 2.7V to 3.3V
  1040. *
  1041. * Board setup code shouldn't permit anything very out-of-range.
  1042. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1043. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1044. */
  1045. if ((1 << vdd) <= MMC_VDD_23_24)
  1046. reg_val |= SDVS18;
  1047. else
  1048. reg_val |= SDVS30;
  1049. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1050. set_sd_bus_power(host);
  1051. return 0;
  1052. err:
  1053. dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1054. return ret;
  1055. }
  1056. /* Protect the card while the cover is open */
  1057. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1058. {
  1059. if (!host->get_cover_state)
  1060. return;
  1061. host->reqs_blocked = 0;
  1062. if (host->get_cover_state(host->dev)) {
  1063. if (host->protect_card) {
  1064. dev_info(host->dev, "%s: cover is closed, "
  1065. "card is now accessible\n",
  1066. mmc_hostname(host->mmc));
  1067. host->protect_card = 0;
  1068. }
  1069. } else {
  1070. if (!host->protect_card) {
  1071. dev_info(host->dev, "%s: cover is open, "
  1072. "card is now inaccessible\n",
  1073. mmc_hostname(host->mmc));
  1074. host->protect_card = 1;
  1075. }
  1076. }
  1077. }
  1078. /*
  1079. * irq handler when (cell-phone) cover is mounted/removed
  1080. */
  1081. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
  1082. {
  1083. struct omap_hsmmc_host *host = dev_id;
  1084. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1085. omap_hsmmc_protect_card(host);
  1086. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1087. return IRQ_HANDLED;
  1088. }
  1089. static void omap_hsmmc_dma_callback(void *param)
  1090. {
  1091. struct omap_hsmmc_host *host = param;
  1092. struct dma_chan *chan;
  1093. struct mmc_data *data;
  1094. int req_in_progress;
  1095. spin_lock_irq(&host->irq_lock);
  1096. if (host->dma_ch < 0) {
  1097. spin_unlock_irq(&host->irq_lock);
  1098. return;
  1099. }
  1100. data = host->mrq->data;
  1101. chan = omap_hsmmc_get_dma_chan(host, data);
  1102. if (!data->host_cookie)
  1103. dma_unmap_sg(chan->device->dev,
  1104. data->sg, data->sg_len,
  1105. mmc_get_dma_dir(data));
  1106. req_in_progress = host->req_in_progress;
  1107. host->dma_ch = -1;
  1108. spin_unlock_irq(&host->irq_lock);
  1109. /* If DMA has finished after TC, complete the request */
  1110. if (!req_in_progress) {
  1111. struct mmc_request *mrq = host->mrq;
  1112. host->mrq = NULL;
  1113. mmc_request_done(host->mmc, mrq);
  1114. }
  1115. }
  1116. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1117. struct mmc_data *data,
  1118. struct omap_hsmmc_next *next,
  1119. struct dma_chan *chan)
  1120. {
  1121. int dma_len;
  1122. if (!next && data->host_cookie &&
  1123. data->host_cookie != host->next_data.cookie) {
  1124. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1125. " host->next_data.cookie %d\n",
  1126. __func__, data->host_cookie, host->next_data.cookie);
  1127. data->host_cookie = 0;
  1128. }
  1129. /* Check if next job is already prepared */
  1130. if (next || data->host_cookie != host->next_data.cookie) {
  1131. dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  1132. mmc_get_dma_dir(data));
  1133. } else {
  1134. dma_len = host->next_data.dma_len;
  1135. host->next_data.dma_len = 0;
  1136. }
  1137. if (dma_len == 0)
  1138. return -EINVAL;
  1139. if (next) {
  1140. next->dma_len = dma_len;
  1141. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1142. } else
  1143. host->dma_len = dma_len;
  1144. return 0;
  1145. }
  1146. /*
  1147. * Routine to configure and start DMA for the MMC card
  1148. */
  1149. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1150. struct mmc_request *req)
  1151. {
  1152. struct dma_async_tx_descriptor *tx;
  1153. int ret = 0, i;
  1154. struct mmc_data *data = req->data;
  1155. struct dma_chan *chan;
  1156. struct dma_slave_config cfg = {
  1157. .src_addr = host->mapbase + OMAP_HSMMC_DATA,
  1158. .dst_addr = host->mapbase + OMAP_HSMMC_DATA,
  1159. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  1160. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  1161. .src_maxburst = data->blksz / 4,
  1162. .dst_maxburst = data->blksz / 4,
  1163. };
  1164. /* Sanity check: all the SG entries must be aligned by block size. */
  1165. for (i = 0; i < data->sg_len; i++) {
  1166. struct scatterlist *sgl;
  1167. sgl = data->sg + i;
  1168. if (sgl->length % data->blksz)
  1169. return -EINVAL;
  1170. }
  1171. if ((data->blksz % 4) != 0)
  1172. /* REVISIT: The MMC buffer increments only when MSB is written.
  1173. * Return error for blksz which is non multiple of four.
  1174. */
  1175. return -EINVAL;
  1176. BUG_ON(host->dma_ch != -1);
  1177. chan = omap_hsmmc_get_dma_chan(host, data);
  1178. ret = dmaengine_slave_config(chan, &cfg);
  1179. if (ret)
  1180. return ret;
  1181. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1182. if (ret)
  1183. return ret;
  1184. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1185. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1186. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1187. if (!tx) {
  1188. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1189. /* FIXME: cleanup */
  1190. return -1;
  1191. }
  1192. tx->callback = omap_hsmmc_dma_callback;
  1193. tx->callback_param = host;
  1194. /* Does not fail */
  1195. dmaengine_submit(tx);
  1196. host->dma_ch = 1;
  1197. return 0;
  1198. }
  1199. static void set_data_timeout(struct omap_hsmmc_host *host,
  1200. unsigned long long timeout_ns,
  1201. unsigned int timeout_clks)
  1202. {
  1203. unsigned long long timeout = timeout_ns;
  1204. unsigned int cycle_ns;
  1205. uint32_t reg, clkd, dto = 0;
  1206. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1207. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1208. if (clkd == 0)
  1209. clkd = 1;
  1210. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1211. do_div(timeout, cycle_ns);
  1212. timeout += timeout_clks;
  1213. if (timeout) {
  1214. while ((timeout & 0x80000000) == 0) {
  1215. dto += 1;
  1216. timeout <<= 1;
  1217. }
  1218. dto = 31 - dto;
  1219. timeout <<= 1;
  1220. if (timeout && dto)
  1221. dto += 1;
  1222. if (dto >= 13)
  1223. dto -= 13;
  1224. else
  1225. dto = 0;
  1226. if (dto > 14)
  1227. dto = 14;
  1228. }
  1229. reg &= ~DTO_MASK;
  1230. reg |= dto << DTO_SHIFT;
  1231. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1232. }
  1233. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1234. {
  1235. struct mmc_request *req = host->mrq;
  1236. struct dma_chan *chan;
  1237. if (!req->data)
  1238. return;
  1239. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1240. | (req->data->blocks << 16));
  1241. set_data_timeout(host, req->data->timeout_ns,
  1242. req->data->timeout_clks);
  1243. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1244. dma_async_issue_pending(chan);
  1245. }
  1246. /*
  1247. * Configure block length for MMC/SD cards and initiate the transfer.
  1248. */
  1249. static int
  1250. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1251. {
  1252. int ret;
  1253. unsigned long long timeout;
  1254. host->data = req->data;
  1255. if (req->data == NULL) {
  1256. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1257. if (req->cmd->flags & MMC_RSP_BUSY) {
  1258. timeout = req->cmd->busy_timeout * NSEC_PER_MSEC;
  1259. /*
  1260. * Set an arbitrary 100ms data timeout for commands with
  1261. * busy signal and no indication of busy_timeout.
  1262. */
  1263. if (!timeout)
  1264. timeout = 100000000U;
  1265. set_data_timeout(host, timeout, 0);
  1266. }
  1267. return 0;
  1268. }
  1269. if (host->use_dma) {
  1270. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1271. if (ret != 0) {
  1272. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1273. return ret;
  1274. }
  1275. }
  1276. return 0;
  1277. }
  1278. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1279. int err)
  1280. {
  1281. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1282. struct mmc_data *data = mrq->data;
  1283. if (host->use_dma && data->host_cookie) {
  1284. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
  1285. dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
  1286. mmc_get_dma_dir(data));
  1287. data->host_cookie = 0;
  1288. }
  1289. }
  1290. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  1291. {
  1292. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1293. if (mrq->data->host_cookie) {
  1294. mrq->data->host_cookie = 0;
  1295. return ;
  1296. }
  1297. if (host->use_dma) {
  1298. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1299. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1300. &host->next_data, c))
  1301. mrq->data->host_cookie = 0;
  1302. }
  1303. }
  1304. /*
  1305. * Request function. for read/write operation
  1306. */
  1307. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1308. {
  1309. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1310. int err;
  1311. BUG_ON(host->req_in_progress);
  1312. BUG_ON(host->dma_ch != -1);
  1313. if (host->protect_card) {
  1314. if (host->reqs_blocked < 3) {
  1315. /*
  1316. * Ensure the controller is left in a consistent
  1317. * state by resetting the command and data state
  1318. * machines.
  1319. */
  1320. omap_hsmmc_reset_controller_fsm(host, SRD);
  1321. omap_hsmmc_reset_controller_fsm(host, SRC);
  1322. host->reqs_blocked += 1;
  1323. }
  1324. req->cmd->error = -EBADF;
  1325. if (req->data)
  1326. req->data->error = -EBADF;
  1327. req->cmd->retries = 0;
  1328. mmc_request_done(mmc, req);
  1329. return;
  1330. } else if (host->reqs_blocked)
  1331. host->reqs_blocked = 0;
  1332. WARN_ON(host->mrq != NULL);
  1333. host->mrq = req;
  1334. host->clk_rate = clk_get_rate(host->fclk);
  1335. err = omap_hsmmc_prepare_data(host, req);
  1336. if (err) {
  1337. req->cmd->error = err;
  1338. if (req->data)
  1339. req->data->error = err;
  1340. host->mrq = NULL;
  1341. mmc_request_done(mmc, req);
  1342. return;
  1343. }
  1344. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1345. omap_hsmmc_start_command(host, req->sbc, NULL);
  1346. return;
  1347. }
  1348. omap_hsmmc_start_dma_transfer(host);
  1349. omap_hsmmc_start_command(host, req->cmd, req->data);
  1350. }
  1351. /* Routine to configure clock values. Exposed API to core */
  1352. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1353. {
  1354. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1355. int do_send_init_stream = 0;
  1356. if (ios->power_mode != host->power_mode) {
  1357. switch (ios->power_mode) {
  1358. case MMC_POWER_OFF:
  1359. omap_hsmmc_set_power(host, 0);
  1360. break;
  1361. case MMC_POWER_UP:
  1362. omap_hsmmc_set_power(host, 1);
  1363. break;
  1364. case MMC_POWER_ON:
  1365. do_send_init_stream = 1;
  1366. break;
  1367. }
  1368. host->power_mode = ios->power_mode;
  1369. }
  1370. /* FIXME: set registers based only on changes to ios */
  1371. omap_hsmmc_set_bus_width(host);
  1372. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1373. /* Only MMC1 can interface at 3V without some flavor
  1374. * of external transceiver; but they all handle 1.8V.
  1375. */
  1376. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1377. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1378. /*
  1379. * The mmc_select_voltage fn of the core does
  1380. * not seem to set the power_mode to
  1381. * MMC_POWER_UP upon recalculating the voltage.
  1382. * vdd 1.8v.
  1383. */
  1384. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1385. dev_dbg(mmc_dev(host->mmc),
  1386. "Switch operation failed\n");
  1387. }
  1388. }
  1389. omap_hsmmc_set_clock(host);
  1390. if (do_send_init_stream)
  1391. send_init_stream(host);
  1392. omap_hsmmc_set_bus_mode(host);
  1393. }
  1394. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1395. {
  1396. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1397. if (!host->card_detect)
  1398. return -ENOSYS;
  1399. return host->card_detect(host->dev);
  1400. }
  1401. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1402. {
  1403. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1404. if (mmc_pdata(host)->init_card)
  1405. mmc_pdata(host)->init_card(card);
  1406. }
  1407. static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1408. {
  1409. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1410. u32 irq_mask, con;
  1411. unsigned long flags;
  1412. spin_lock_irqsave(&host->irq_lock, flags);
  1413. con = OMAP_HSMMC_READ(host->base, CON);
  1414. irq_mask = OMAP_HSMMC_READ(host->base, ISE);
  1415. if (enable) {
  1416. host->flags |= HSMMC_SDIO_IRQ_ENABLED;
  1417. irq_mask |= CIRQ_EN;
  1418. con |= CTPL | CLKEXTFREE;
  1419. } else {
  1420. host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
  1421. irq_mask &= ~CIRQ_EN;
  1422. con &= ~(CTPL | CLKEXTFREE);
  1423. }
  1424. OMAP_HSMMC_WRITE(host->base, CON, con);
  1425. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  1426. /*
  1427. * if enable, piggy back detection on current request
  1428. * but always disable immediately
  1429. */
  1430. if (!host->req_in_progress || !enable)
  1431. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  1432. /* flush posted write */
  1433. OMAP_HSMMC_READ(host->base, IE);
  1434. spin_unlock_irqrestore(&host->irq_lock, flags);
  1435. }
  1436. static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
  1437. {
  1438. int ret;
  1439. /*
  1440. * For omaps with wake-up path, wakeirq will be irq from pinctrl and
  1441. * for other omaps, wakeirq will be from GPIO (dat line remuxed to
  1442. * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
  1443. * with functional clock disabled.
  1444. */
  1445. if (!host->dev->of_node || !host->wake_irq)
  1446. return -ENODEV;
  1447. ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
  1448. if (ret) {
  1449. dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
  1450. goto err;
  1451. }
  1452. /*
  1453. * Some omaps don't have wake-up path from deeper idle states
  1454. * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
  1455. */
  1456. if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
  1457. struct pinctrl *p = devm_pinctrl_get(host->dev);
  1458. if (IS_ERR(p)) {
  1459. ret = PTR_ERR(p);
  1460. goto err_free_irq;
  1461. }
  1462. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
  1463. dev_info(host->dev, "missing default pinctrl state\n");
  1464. devm_pinctrl_put(p);
  1465. ret = -EINVAL;
  1466. goto err_free_irq;
  1467. }
  1468. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
  1469. dev_info(host->dev, "missing idle pinctrl state\n");
  1470. devm_pinctrl_put(p);
  1471. ret = -EINVAL;
  1472. goto err_free_irq;
  1473. }
  1474. devm_pinctrl_put(p);
  1475. }
  1476. OMAP_HSMMC_WRITE(host->base, HCTL,
  1477. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  1478. return 0;
  1479. err_free_irq:
  1480. dev_pm_clear_wake_irq(host->dev);
  1481. err:
  1482. dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
  1483. host->wake_irq = 0;
  1484. return ret;
  1485. }
  1486. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1487. {
  1488. u32 hctl, capa, value;
  1489. /* Only MMC1 supports 3.0V */
  1490. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1491. hctl = SDVS30;
  1492. capa = VS30 | VS18;
  1493. } else {
  1494. hctl = SDVS18;
  1495. capa = VS18;
  1496. }
  1497. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1498. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1499. value = OMAP_HSMMC_READ(host->base, CAPA);
  1500. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1501. /* Set SD bus power bit */
  1502. set_sd_bus_power(host);
  1503. }
  1504. static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
  1505. unsigned int direction, int blk_size)
  1506. {
  1507. /* This controller can't do multiblock reads due to hw bugs */
  1508. if (direction == MMC_DATA_READ)
  1509. return 1;
  1510. return blk_size;
  1511. }
  1512. static struct mmc_host_ops omap_hsmmc_ops = {
  1513. .post_req = omap_hsmmc_post_req,
  1514. .pre_req = omap_hsmmc_pre_req,
  1515. .request = omap_hsmmc_request,
  1516. .set_ios = omap_hsmmc_set_ios,
  1517. .get_cd = omap_hsmmc_get_cd,
  1518. .get_ro = mmc_gpio_get_ro,
  1519. .init_card = omap_hsmmc_init_card,
  1520. .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
  1521. };
  1522. #ifdef CONFIG_DEBUG_FS
  1523. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1524. {
  1525. struct mmc_host *mmc = s->private;
  1526. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1527. seq_printf(s, "mmc%d:\n", mmc->index);
  1528. seq_printf(s, "sdio irq mode\t%s\n",
  1529. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
  1530. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1531. seq_printf(s, "sdio irq \t%s\n",
  1532. (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
  1533. : "disabled");
  1534. }
  1535. seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
  1536. pm_runtime_get_sync(host->dev);
  1537. seq_puts(s, "\nregs:\n");
  1538. seq_printf(s, "CON:\t\t0x%08x\n",
  1539. OMAP_HSMMC_READ(host->base, CON));
  1540. seq_printf(s, "PSTATE:\t\t0x%08x\n",
  1541. OMAP_HSMMC_READ(host->base, PSTATE));
  1542. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1543. OMAP_HSMMC_READ(host->base, HCTL));
  1544. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1545. OMAP_HSMMC_READ(host->base, SYSCTL));
  1546. seq_printf(s, "IE:\t\t0x%08x\n",
  1547. OMAP_HSMMC_READ(host->base, IE));
  1548. seq_printf(s, "ISE:\t\t0x%08x\n",
  1549. OMAP_HSMMC_READ(host->base, ISE));
  1550. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1551. OMAP_HSMMC_READ(host->base, CAPA));
  1552. pm_runtime_mark_last_busy(host->dev);
  1553. pm_runtime_put_autosuspend(host->dev);
  1554. return 0;
  1555. }
  1556. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1557. {
  1558. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1559. }
  1560. static const struct file_operations mmc_regs_fops = {
  1561. .open = omap_hsmmc_regs_open,
  1562. .read = seq_read,
  1563. .llseek = seq_lseek,
  1564. .release = single_release,
  1565. };
  1566. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1567. {
  1568. if (mmc->debugfs_root)
  1569. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1570. mmc, &mmc_regs_fops);
  1571. }
  1572. #else
  1573. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1574. {
  1575. }
  1576. #endif
  1577. #ifdef CONFIG_OF
  1578. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  1579. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  1580. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  1581. };
  1582. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  1583. .reg_offset = 0x100,
  1584. };
  1585. static const struct omap_mmc_of_data am33xx_mmc_of_data = {
  1586. .reg_offset = 0x100,
  1587. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
  1588. };
  1589. static const struct of_device_id omap_mmc_of_match[] = {
  1590. {
  1591. .compatible = "ti,omap2-hsmmc",
  1592. },
  1593. {
  1594. .compatible = "ti,omap3-pre-es3-hsmmc",
  1595. .data = &omap3_pre_es3_mmc_of_data,
  1596. },
  1597. {
  1598. .compatible = "ti,omap3-hsmmc",
  1599. },
  1600. {
  1601. .compatible = "ti,omap4-hsmmc",
  1602. .data = &omap4_mmc_of_data,
  1603. },
  1604. {
  1605. .compatible = "ti,am33xx-hsmmc",
  1606. .data = &am33xx_mmc_of_data,
  1607. },
  1608. {},
  1609. };
  1610. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1611. static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1612. {
  1613. struct omap_hsmmc_platform_data *pdata, *legacy;
  1614. struct device_node *np = dev->of_node;
  1615. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1616. if (!pdata)
  1617. return ERR_PTR(-ENOMEM); /* out of memory */
  1618. legacy = dev_get_platdata(dev);
  1619. if (legacy && legacy->name)
  1620. pdata->name = legacy->name;
  1621. if (of_find_property(np, "ti,dual-volt", NULL))
  1622. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1623. pdata->gpio_cd = -EINVAL;
  1624. pdata->gpio_cod = -EINVAL;
  1625. pdata->gpio_wp = -EINVAL;
  1626. if (of_find_property(np, "ti,non-removable", NULL)) {
  1627. pdata->nonremovable = true;
  1628. pdata->no_regulator_off_init = true;
  1629. }
  1630. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1631. pdata->features |= HSMMC_HAS_UPDATED_RESET;
  1632. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  1633. pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
  1634. return pdata;
  1635. }
  1636. #else
  1637. static inline struct omap_hsmmc_platform_data
  1638. *of_get_hsmmc_pdata(struct device *dev)
  1639. {
  1640. return ERR_PTR(-EINVAL);
  1641. }
  1642. #endif
  1643. static int omap_hsmmc_probe(struct platform_device *pdev)
  1644. {
  1645. struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
  1646. struct mmc_host *mmc;
  1647. struct omap_hsmmc_host *host = NULL;
  1648. struct resource *res;
  1649. int ret, irq;
  1650. const struct of_device_id *match;
  1651. const struct omap_mmc_of_data *data;
  1652. void __iomem *base;
  1653. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1654. if (match) {
  1655. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1656. if (IS_ERR(pdata))
  1657. return PTR_ERR(pdata);
  1658. if (match->data) {
  1659. data = match->data;
  1660. pdata->reg_offset = data->reg_offset;
  1661. pdata->controller_flags |= data->controller_flags;
  1662. }
  1663. }
  1664. if (pdata == NULL) {
  1665. dev_err(&pdev->dev, "Platform Data is missing\n");
  1666. return -ENXIO;
  1667. }
  1668. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1669. irq = platform_get_irq(pdev, 0);
  1670. if (res == NULL || irq < 0)
  1671. return -ENXIO;
  1672. base = devm_ioremap_resource(&pdev->dev, res);
  1673. if (IS_ERR(base))
  1674. return PTR_ERR(base);
  1675. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1676. if (!mmc) {
  1677. ret = -ENOMEM;
  1678. goto err;
  1679. }
  1680. ret = mmc_of_parse(mmc);
  1681. if (ret)
  1682. goto err1;
  1683. host = mmc_priv(mmc);
  1684. host->mmc = mmc;
  1685. host->pdata = pdata;
  1686. host->dev = &pdev->dev;
  1687. host->use_dma = 1;
  1688. host->dma_ch = -1;
  1689. host->irq = irq;
  1690. host->mapbase = res->start + pdata->reg_offset;
  1691. host->base = base + pdata->reg_offset;
  1692. host->power_mode = MMC_POWER_OFF;
  1693. host->next_data.cookie = 1;
  1694. host->pbias_enabled = 0;
  1695. host->vqmmc_enabled = 0;
  1696. ret = omap_hsmmc_gpio_init(mmc, host, pdata);
  1697. if (ret)
  1698. goto err_gpio;
  1699. platform_set_drvdata(pdev, host);
  1700. if (pdev->dev.of_node)
  1701. host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1702. mmc->ops = &omap_hsmmc_ops;
  1703. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1704. if (pdata->max_freq > 0)
  1705. mmc->f_max = pdata->max_freq;
  1706. else if (mmc->f_max == 0)
  1707. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1708. spin_lock_init(&host->irq_lock);
  1709. host->fclk = devm_clk_get(&pdev->dev, "fck");
  1710. if (IS_ERR(host->fclk)) {
  1711. ret = PTR_ERR(host->fclk);
  1712. host->fclk = NULL;
  1713. goto err1;
  1714. }
  1715. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1716. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1717. omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
  1718. }
  1719. device_init_wakeup(&pdev->dev, true);
  1720. pm_runtime_enable(host->dev);
  1721. pm_runtime_get_sync(host->dev);
  1722. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1723. pm_runtime_use_autosuspend(host->dev);
  1724. omap_hsmmc_context_save(host);
  1725. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  1726. /*
  1727. * MMC can still work without debounce clock.
  1728. */
  1729. if (IS_ERR(host->dbclk)) {
  1730. host->dbclk = NULL;
  1731. } else if (clk_prepare_enable(host->dbclk) != 0) {
  1732. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  1733. host->dbclk = NULL;
  1734. }
  1735. /* Set this to a value that allows allocating an entire descriptor
  1736. * list within a page (zero order allocation). */
  1737. mmc->max_segs = 64;
  1738. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1739. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1740. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1741. mmc->max_seg_size = mmc->max_req_size;
  1742. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1743. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE | MMC_CAP_CMD23;
  1744. mmc->caps |= mmc_pdata(host)->caps;
  1745. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1746. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1747. if (mmc_pdata(host)->nonremovable)
  1748. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1749. mmc->pm_caps |= mmc_pdata(host)->pm_caps;
  1750. omap_hsmmc_conf_bus_power(host);
  1751. host->rx_chan = dma_request_chan(&pdev->dev, "rx");
  1752. if (IS_ERR(host->rx_chan)) {
  1753. dev_err(mmc_dev(host->mmc), "RX DMA channel request failed\n");
  1754. ret = PTR_ERR(host->rx_chan);
  1755. goto err_irq;
  1756. }
  1757. host->tx_chan = dma_request_chan(&pdev->dev, "tx");
  1758. if (IS_ERR(host->tx_chan)) {
  1759. dev_err(mmc_dev(host->mmc), "TX DMA channel request failed\n");
  1760. ret = PTR_ERR(host->tx_chan);
  1761. goto err_irq;
  1762. }
  1763. /* Request IRQ for MMC operations */
  1764. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  1765. mmc_hostname(mmc), host);
  1766. if (ret) {
  1767. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1768. goto err_irq;
  1769. }
  1770. ret = omap_hsmmc_reg_get(host);
  1771. if (ret)
  1772. goto err_irq;
  1773. if (!mmc->ocr_avail)
  1774. mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
  1775. omap_hsmmc_disable_irq(host);
  1776. /*
  1777. * For now, only support SDIO interrupt if we have a separate
  1778. * wake-up interrupt configured from device tree. This is because
  1779. * the wake-up interrupt is needed for idle state and some
  1780. * platforms need special quirks. And we don't want to add new
  1781. * legacy mux platform init code callbacks any longer as we
  1782. * are moving to DT based booting anyways.
  1783. */
  1784. ret = omap_hsmmc_configure_wake_irq(host);
  1785. if (!ret)
  1786. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1787. omap_hsmmc_protect_card(host);
  1788. mmc_add_host(mmc);
  1789. if (mmc_pdata(host)->name != NULL) {
  1790. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1791. if (ret < 0)
  1792. goto err_slot_name;
  1793. }
  1794. if (host->get_cover_state) {
  1795. ret = device_create_file(&mmc->class_dev,
  1796. &dev_attr_cover_switch);
  1797. if (ret < 0)
  1798. goto err_slot_name;
  1799. }
  1800. omap_hsmmc_debugfs(mmc);
  1801. pm_runtime_mark_last_busy(host->dev);
  1802. pm_runtime_put_autosuspend(host->dev);
  1803. return 0;
  1804. err_slot_name:
  1805. mmc_remove_host(mmc);
  1806. err_irq:
  1807. device_init_wakeup(&pdev->dev, false);
  1808. if (!IS_ERR_OR_NULL(host->tx_chan))
  1809. dma_release_channel(host->tx_chan);
  1810. if (!IS_ERR_OR_NULL(host->rx_chan))
  1811. dma_release_channel(host->rx_chan);
  1812. pm_runtime_dont_use_autosuspend(host->dev);
  1813. pm_runtime_put_sync(host->dev);
  1814. pm_runtime_disable(host->dev);
  1815. if (host->dbclk)
  1816. clk_disable_unprepare(host->dbclk);
  1817. err1:
  1818. err_gpio:
  1819. mmc_free_host(mmc);
  1820. err:
  1821. return ret;
  1822. }
  1823. static int omap_hsmmc_remove(struct platform_device *pdev)
  1824. {
  1825. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1826. pm_runtime_get_sync(host->dev);
  1827. mmc_remove_host(host->mmc);
  1828. dma_release_channel(host->tx_chan);
  1829. dma_release_channel(host->rx_chan);
  1830. dev_pm_clear_wake_irq(host->dev);
  1831. pm_runtime_dont_use_autosuspend(host->dev);
  1832. pm_runtime_put_sync(host->dev);
  1833. pm_runtime_disable(host->dev);
  1834. device_init_wakeup(&pdev->dev, false);
  1835. if (host->dbclk)
  1836. clk_disable_unprepare(host->dbclk);
  1837. mmc_free_host(host->mmc);
  1838. return 0;
  1839. }
  1840. #ifdef CONFIG_PM_SLEEP
  1841. static int omap_hsmmc_suspend(struct device *dev)
  1842. {
  1843. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1844. if (!host)
  1845. return 0;
  1846. pm_runtime_get_sync(host->dev);
  1847. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1848. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1849. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1850. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1851. OMAP_HSMMC_WRITE(host->base, HCTL,
  1852. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1853. }
  1854. if (host->dbclk)
  1855. clk_disable_unprepare(host->dbclk);
  1856. pm_runtime_put_sync(host->dev);
  1857. return 0;
  1858. }
  1859. /* Routine to resume the MMC device */
  1860. static int omap_hsmmc_resume(struct device *dev)
  1861. {
  1862. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1863. if (!host)
  1864. return 0;
  1865. pm_runtime_get_sync(host->dev);
  1866. if (host->dbclk)
  1867. clk_prepare_enable(host->dbclk);
  1868. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1869. omap_hsmmc_conf_bus_power(host);
  1870. omap_hsmmc_protect_card(host);
  1871. pm_runtime_mark_last_busy(host->dev);
  1872. pm_runtime_put_autosuspend(host->dev);
  1873. return 0;
  1874. }
  1875. #endif
  1876. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1877. {
  1878. struct omap_hsmmc_host *host;
  1879. unsigned long flags;
  1880. int ret = 0;
  1881. host = platform_get_drvdata(to_platform_device(dev));
  1882. omap_hsmmc_context_save(host);
  1883. dev_dbg(dev, "disabled\n");
  1884. spin_lock_irqsave(&host->irq_lock, flags);
  1885. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1886. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1887. /* disable sdio irq handling to prevent race */
  1888. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1889. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1890. if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
  1891. /*
  1892. * dat1 line low, pending sdio irq
  1893. * race condition: possible irq handler running on
  1894. * multi-core, abort
  1895. */
  1896. dev_dbg(dev, "pending sdio irq, abort suspend\n");
  1897. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1898. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1899. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1900. pm_runtime_mark_last_busy(dev);
  1901. ret = -EBUSY;
  1902. goto abort;
  1903. }
  1904. pinctrl_pm_select_idle_state(dev);
  1905. } else {
  1906. pinctrl_pm_select_idle_state(dev);
  1907. }
  1908. abort:
  1909. spin_unlock_irqrestore(&host->irq_lock, flags);
  1910. return ret;
  1911. }
  1912. static int omap_hsmmc_runtime_resume(struct device *dev)
  1913. {
  1914. struct omap_hsmmc_host *host;
  1915. unsigned long flags;
  1916. host = platform_get_drvdata(to_platform_device(dev));
  1917. omap_hsmmc_context_restore(host);
  1918. dev_dbg(dev, "enabled\n");
  1919. spin_lock_irqsave(&host->irq_lock, flags);
  1920. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1921. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1922. pinctrl_pm_select_default_state(host->dev);
  1923. /* irq lost, if pinmux incorrect */
  1924. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1925. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1926. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1927. } else {
  1928. pinctrl_pm_select_default_state(host->dev);
  1929. }
  1930. spin_unlock_irqrestore(&host->irq_lock, flags);
  1931. return 0;
  1932. }
  1933. static const struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1934. SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
  1935. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1936. .runtime_resume = omap_hsmmc_runtime_resume,
  1937. };
  1938. static struct platform_driver omap_hsmmc_driver = {
  1939. .probe = omap_hsmmc_probe,
  1940. .remove = omap_hsmmc_remove,
  1941. .driver = {
  1942. .name = DRIVER_NAME,
  1943. .pm = &omap_hsmmc_dev_pm_ops,
  1944. .of_match_table = of_match_ptr(omap_mmc_of_match),
  1945. },
  1946. };
  1947. module_platform_driver(omap_hsmmc_driver);
  1948. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1949. MODULE_LICENSE("GPL");
  1950. MODULE_ALIAS("platform:" DRIVER_NAME);
  1951. MODULE_AUTHOR("Texas Instruments Inc");