omap_hsmmc.c 54 KB

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