omap_hsmmc.c 54 KB

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