omap_hsmmc.c 54 KB

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