omap_hsmmc.c 52 KB

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