omap_hsmmc.c 52 KB

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