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/seq_file.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/timer.h>
  27. #include <linux/clk.h>
  28. #include <linux/of.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/of_device.h>
  31. #include <linux/mmc/host.h>
  32. #include <linux/mmc/core.h>
  33. #include <linux/mmc/mmc.h>
  34. #include <linux/io.h>
  35. #include <linux/semaphore.h>
  36. #include <linux/gpio.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/pm_runtime.h>
  39. #include <plat/dma.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 ACEN_ACMD12 (1 << 2)
  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 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. #define AUTO_CMD12 (1 << 0) /* Auto CMD12 support */
  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. int dma_line_tx, dma_line_rx;
  162. int slot_id;
  163. int got_dbclk;
  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. unsigned int flags;
  172. struct omap_hsmmc_next next_data;
  173. struct omap_mmc_platform_data *pdata;
  174. };
  175. static int omap_hsmmc_card_detect(struct device *dev, int slot)
  176. {
  177. struct omap_mmc_platform_data *mmc = dev->platform_data;
  178. /* NOTE: assumes card detect signal is active-low */
  179. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  180. }
  181. static int omap_hsmmc_get_wp(struct device *dev, int slot)
  182. {
  183. struct omap_mmc_platform_data *mmc = dev->platform_data;
  184. /* NOTE: assumes write protect signal is active-high */
  185. return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
  186. }
  187. static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
  188. {
  189. struct omap_mmc_platform_data *mmc = dev->platform_data;
  190. /* NOTE: assumes card detect signal is active-low */
  191. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  192. }
  193. #ifdef CONFIG_PM
  194. static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
  195. {
  196. struct omap_mmc_platform_data *mmc = dev->platform_data;
  197. disable_irq(mmc->slots[0].card_detect_irq);
  198. return 0;
  199. }
  200. static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
  201. {
  202. struct omap_mmc_platform_data *mmc = dev->platform_data;
  203. enable_irq(mmc->slots[0].card_detect_irq);
  204. return 0;
  205. }
  206. #else
  207. #define omap_hsmmc_suspend_cdirq NULL
  208. #define omap_hsmmc_resume_cdirq NULL
  209. #endif
  210. #ifdef CONFIG_REGULATOR
  211. static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
  212. int vdd)
  213. {
  214. struct omap_hsmmc_host *host =
  215. platform_get_drvdata(to_platform_device(dev));
  216. int ret = 0;
  217. /*
  218. * If we don't see a Vcc regulator, assume it's a fixed
  219. * voltage always-on regulator.
  220. */
  221. if (!host->vcc)
  222. return 0;
  223. /*
  224. * With DT, never turn OFF the regulator. This is because
  225. * the pbias cell programming support is still missing when
  226. * booting with Device tree
  227. */
  228. if (dev->of_node && !vdd)
  229. return 0;
  230. if (mmc_slot(host).before_set_reg)
  231. mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
  232. /*
  233. * Assume Vcc regulator is used only to power the card ... OMAP
  234. * VDDS is used to power the pins, optionally with a transceiver to
  235. * support cards using voltages other than VDDS (1.8V nominal). When a
  236. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  237. *
  238. * In some cases this regulator won't support enable/disable;
  239. * e.g. it's a fixed rail for a WLAN chip.
  240. *
  241. * In other cases vcc_aux switches interface power. Example, for
  242. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  243. * chips/cards need an interface voltage rail too.
  244. */
  245. if (power_on) {
  246. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  247. /* Enable interface voltage rail, if needed */
  248. if (ret == 0 && host->vcc_aux) {
  249. ret = regulator_enable(host->vcc_aux);
  250. if (ret < 0)
  251. ret = mmc_regulator_set_ocr(host->mmc,
  252. host->vcc, 0);
  253. }
  254. } else {
  255. /* Shut down the rail */
  256. if (host->vcc_aux)
  257. ret = regulator_disable(host->vcc_aux);
  258. if (!ret) {
  259. /* Then proceed to shut down the local regulator */
  260. ret = mmc_regulator_set_ocr(host->mmc,
  261. host->vcc, 0);
  262. }
  263. }
  264. if (mmc_slot(host).after_set_reg)
  265. mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
  266. return ret;
  267. }
  268. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  269. {
  270. struct regulator *reg;
  271. int ocr_value = 0;
  272. mmc_slot(host).set_power = omap_hsmmc_set_power;
  273. reg = regulator_get(host->dev, "vmmc");
  274. if (IS_ERR(reg)) {
  275. dev_dbg(host->dev, "vmmc regulator missing\n");
  276. } else {
  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. 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_dbg(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 ((host->flags & AUTO_CMD12) && mmc_op_multi(cmd->opcode))
  655. cmdreg |= ACEN_ACMD12;
  656. if (data) {
  657. cmdreg |= DP_SELECT | MSBS | BCE;
  658. if (data->flags & MMC_DATA_READ)
  659. cmdreg |= DDIR;
  660. else
  661. cmdreg &= ~(DDIR);
  662. }
  663. if (host->use_dma)
  664. cmdreg |= DMA_EN;
  665. host->req_in_progress = 1;
  666. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  667. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  668. }
  669. static int
  670. omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
  671. {
  672. if (data->flags & MMC_DATA_WRITE)
  673. return DMA_TO_DEVICE;
  674. else
  675. return DMA_FROM_DEVICE;
  676. }
  677. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  678. {
  679. int dma_ch;
  680. spin_lock(&host->irq_lock);
  681. host->req_in_progress = 0;
  682. dma_ch = host->dma_ch;
  683. spin_unlock(&host->irq_lock);
  684. omap_hsmmc_disable_irq(host);
  685. /* Do not complete the request if DMA is still in progress */
  686. if (mrq->data && host->use_dma && dma_ch != -1)
  687. return;
  688. host->mrq = NULL;
  689. mmc_request_done(host->mmc, mrq);
  690. }
  691. /*
  692. * Notify the transfer complete to MMC core
  693. */
  694. static void
  695. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  696. {
  697. if (!data) {
  698. struct mmc_request *mrq = host->mrq;
  699. /* TC before CC from CMD6 - don't know why, but it happens */
  700. if (host->cmd && host->cmd->opcode == 6 &&
  701. host->response_busy) {
  702. host->response_busy = 0;
  703. return;
  704. }
  705. omap_hsmmc_request_done(host, mrq);
  706. return;
  707. }
  708. host->data = NULL;
  709. if (!data->error)
  710. data->bytes_xfered += data->blocks * (data->blksz);
  711. else
  712. data->bytes_xfered = 0;
  713. if (data->stop && ((!(host->flags & AUTO_CMD12)) || data->error)) {
  714. omap_hsmmc_start_command(host, data->stop, NULL);
  715. } else {
  716. if (data->stop)
  717. data->stop->resp[0] = OMAP_HSMMC_READ(host->base,
  718. RSP76);
  719. omap_hsmmc_request_done(host, data->mrq);
  720. }
  721. }
  722. /*
  723. * Notify the core about command completion
  724. */
  725. static void
  726. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  727. {
  728. host->cmd = NULL;
  729. if (cmd->flags & MMC_RSP_PRESENT) {
  730. if (cmd->flags & MMC_RSP_136) {
  731. /* response type 2 */
  732. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  733. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  734. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  735. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  736. } else {
  737. /* response types 1, 1b, 3, 4, 5, 6 */
  738. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  739. }
  740. }
  741. if ((host->data == NULL && !host->response_busy) || cmd->error)
  742. omap_hsmmc_request_done(host, cmd->mrq);
  743. }
  744. /*
  745. * DMA clean up for command errors
  746. */
  747. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  748. {
  749. int dma_ch;
  750. host->data->error = errno;
  751. spin_lock(&host->irq_lock);
  752. dma_ch = host->dma_ch;
  753. host->dma_ch = -1;
  754. spin_unlock(&host->irq_lock);
  755. if (host->use_dma && dma_ch != -1) {
  756. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
  757. host->data->sg_len,
  758. omap_hsmmc_get_dma_dir(host, host->data));
  759. omap_free_dma(dma_ch);
  760. host->data->host_cookie = 0;
  761. }
  762. host->data = NULL;
  763. }
  764. /*
  765. * Readable error output
  766. */
  767. #ifdef CONFIG_MMC_DEBUG
  768. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  769. {
  770. /* --- means reserved bit without definition at documentation */
  771. static const char *omap_hsmmc_status_bits[] = {
  772. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  773. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  774. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  775. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  776. };
  777. char res[256];
  778. char *buf = res;
  779. int len, i;
  780. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  781. buf += len;
  782. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  783. if (status & (1 << i)) {
  784. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  785. buf += len;
  786. }
  787. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  788. }
  789. #else
  790. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  791. u32 status)
  792. {
  793. }
  794. #endif /* CONFIG_MMC_DEBUG */
  795. /*
  796. * MMC controller internal state machines reset
  797. *
  798. * Used to reset command or data internal state machines, using respectively
  799. * SRC or SRD bit of SYSCTL register
  800. * Can be called from interrupt context
  801. */
  802. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  803. unsigned long bit)
  804. {
  805. unsigned long i = 0;
  806. unsigned long limit = (loops_per_jiffy *
  807. msecs_to_jiffies(MMC_TIMEOUT_MS));
  808. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  809. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  810. /*
  811. * OMAP4 ES2 and greater has an updated reset logic.
  812. * Monitor a 0->1 transition first
  813. */
  814. if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
  815. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  816. && (i++ < limit))
  817. cpu_relax();
  818. }
  819. i = 0;
  820. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  821. (i++ < limit))
  822. cpu_relax();
  823. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  824. dev_err(mmc_dev(host->mmc),
  825. "Timeout waiting on controller reset in %s\n",
  826. __func__);
  827. }
  828. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  829. {
  830. struct mmc_data *data;
  831. int end_cmd = 0, end_trans = 0;
  832. if (!host->req_in_progress) {
  833. do {
  834. OMAP_HSMMC_WRITE(host->base, STAT, status);
  835. /* Flush posted write */
  836. status = OMAP_HSMMC_READ(host->base, STAT);
  837. } while (status & INT_EN_MASK);
  838. return;
  839. }
  840. data = host->data;
  841. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  842. if (status & ERR) {
  843. omap_hsmmc_dbg_report_irq(host, status);
  844. if ((status & CMD_TIMEOUT) ||
  845. (status & CMD_CRC)) {
  846. if (host->cmd) {
  847. if (status & CMD_TIMEOUT) {
  848. omap_hsmmc_reset_controller_fsm(host,
  849. SRC);
  850. host->cmd->error = -ETIMEDOUT;
  851. } else {
  852. host->cmd->error = -EILSEQ;
  853. }
  854. end_cmd = 1;
  855. }
  856. if (host->data || host->response_busy) {
  857. if (host->data)
  858. omap_hsmmc_dma_cleanup(host,
  859. -ETIMEDOUT);
  860. host->response_busy = 0;
  861. omap_hsmmc_reset_controller_fsm(host, SRD);
  862. }
  863. }
  864. if ((status & DATA_TIMEOUT) ||
  865. (status & DATA_CRC)) {
  866. if (host->data || host->response_busy) {
  867. int err = (status & DATA_TIMEOUT) ?
  868. -ETIMEDOUT : -EILSEQ;
  869. if (host->data)
  870. omap_hsmmc_dma_cleanup(host, err);
  871. else
  872. host->mrq->cmd->error = err;
  873. host->response_busy = 0;
  874. omap_hsmmc_reset_controller_fsm(host, SRD);
  875. end_trans = 1;
  876. }
  877. }
  878. if (status & CARD_ERR) {
  879. dev_dbg(mmc_dev(host->mmc),
  880. "Ignoring card err CMD%d\n", host->cmd->opcode);
  881. if (host->cmd)
  882. end_cmd = 1;
  883. if (host->data)
  884. end_trans = 1;
  885. }
  886. }
  887. OMAP_HSMMC_WRITE(host->base, STAT, status);
  888. if (end_cmd || ((status & CC) && host->cmd))
  889. omap_hsmmc_cmd_done(host, host->cmd);
  890. if ((end_trans || (status & TC)) && host->mrq)
  891. omap_hsmmc_xfer_done(host, data);
  892. }
  893. /*
  894. * MMC controller IRQ handler
  895. */
  896. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  897. {
  898. struct omap_hsmmc_host *host = dev_id;
  899. int status;
  900. status = OMAP_HSMMC_READ(host->base, STAT);
  901. do {
  902. omap_hsmmc_do_irq(host, status);
  903. /* Flush posted write */
  904. status = OMAP_HSMMC_READ(host->base, STAT);
  905. } while (status & INT_EN_MASK);
  906. return IRQ_HANDLED;
  907. }
  908. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  909. {
  910. unsigned long i;
  911. OMAP_HSMMC_WRITE(host->base, HCTL,
  912. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  913. for (i = 0; i < loops_per_jiffy; i++) {
  914. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  915. break;
  916. cpu_relax();
  917. }
  918. }
  919. /*
  920. * Switch MMC interface voltage ... only relevant for MMC1.
  921. *
  922. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  923. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  924. * Some chips, like eMMC ones, use internal transceivers.
  925. */
  926. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  927. {
  928. u32 reg_val = 0;
  929. int ret;
  930. /* Disable the clocks */
  931. pm_runtime_put_sync(host->dev);
  932. if (host->got_dbclk)
  933. clk_disable(host->dbclk);
  934. /* Turn the power off */
  935. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  936. /* Turn the power ON with given VDD 1.8 or 3.0v */
  937. if (!ret)
  938. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
  939. vdd);
  940. pm_runtime_get_sync(host->dev);
  941. if (host->got_dbclk)
  942. clk_enable(host->dbclk);
  943. if (ret != 0)
  944. goto err;
  945. OMAP_HSMMC_WRITE(host->base, HCTL,
  946. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  947. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  948. /*
  949. * If a MMC dual voltage card is detected, the set_ios fn calls
  950. * this fn with VDD bit set for 1.8V. Upon card removal from the
  951. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  952. *
  953. * Cope with a bit of slop in the range ... per data sheets:
  954. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  955. * but recommended values are 1.71V to 1.89V
  956. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  957. * but recommended values are 2.7V to 3.3V
  958. *
  959. * Board setup code shouldn't permit anything very out-of-range.
  960. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  961. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  962. */
  963. if ((1 << vdd) <= MMC_VDD_23_24)
  964. reg_val |= SDVS18;
  965. else
  966. reg_val |= SDVS30;
  967. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  968. set_sd_bus_power(host);
  969. return 0;
  970. err:
  971. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  972. return ret;
  973. }
  974. /* Protect the card while the cover is open */
  975. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  976. {
  977. if (!mmc_slot(host).get_cover_state)
  978. return;
  979. host->reqs_blocked = 0;
  980. if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
  981. if (host->protect_card) {
  982. dev_info(host->dev, "%s: cover is closed, "
  983. "card is now accessible\n",
  984. mmc_hostname(host->mmc));
  985. host->protect_card = 0;
  986. }
  987. } else {
  988. if (!host->protect_card) {
  989. dev_info(host->dev, "%s: cover is open, "
  990. "card is now inaccessible\n",
  991. mmc_hostname(host->mmc));
  992. host->protect_card = 1;
  993. }
  994. }
  995. }
  996. /*
  997. * irq handler to notify the core about card insertion/removal
  998. */
  999. static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
  1000. {
  1001. struct omap_hsmmc_host *host = dev_id;
  1002. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  1003. int carddetect;
  1004. if (host->suspended)
  1005. return IRQ_HANDLED;
  1006. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1007. if (slot->card_detect)
  1008. carddetect = slot->card_detect(host->dev, host->slot_id);
  1009. else {
  1010. omap_hsmmc_protect_card(host);
  1011. carddetect = -ENOSYS;
  1012. }
  1013. if (carddetect)
  1014. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1015. else
  1016. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  1017. return IRQ_HANDLED;
  1018. }
  1019. static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
  1020. struct mmc_data *data)
  1021. {
  1022. int sync_dev;
  1023. if (data->flags & MMC_DATA_WRITE)
  1024. sync_dev = host->dma_line_tx;
  1025. else
  1026. sync_dev = host->dma_line_rx;
  1027. return sync_dev;
  1028. }
  1029. static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
  1030. struct mmc_data *data,
  1031. struct scatterlist *sgl)
  1032. {
  1033. int blksz, nblk, dma_ch;
  1034. dma_ch = host->dma_ch;
  1035. if (data->flags & MMC_DATA_WRITE) {
  1036. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  1037. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  1038. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  1039. sg_dma_address(sgl), 0, 0);
  1040. } else {
  1041. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  1042. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  1043. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  1044. sg_dma_address(sgl), 0, 0);
  1045. }
  1046. blksz = host->data->blksz;
  1047. nblk = sg_dma_len(sgl) / blksz;
  1048. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  1049. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  1050. omap_hsmmc_get_dma_sync_dev(host, data),
  1051. !(data->flags & MMC_DATA_WRITE));
  1052. omap_start_dma(dma_ch);
  1053. }
  1054. /*
  1055. * DMA call back function
  1056. */
  1057. static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
  1058. {
  1059. struct omap_hsmmc_host *host = cb_data;
  1060. struct mmc_data *data;
  1061. int dma_ch, req_in_progress;
  1062. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  1063. dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
  1064. ch_status);
  1065. return;
  1066. }
  1067. spin_lock(&host->irq_lock);
  1068. if (host->dma_ch < 0) {
  1069. spin_unlock(&host->irq_lock);
  1070. return;
  1071. }
  1072. data = host->mrq->data;
  1073. host->dma_sg_idx++;
  1074. if (host->dma_sg_idx < host->dma_len) {
  1075. /* Fire up the next transfer. */
  1076. omap_hsmmc_config_dma_params(host, data,
  1077. data->sg + host->dma_sg_idx);
  1078. spin_unlock(&host->irq_lock);
  1079. return;
  1080. }
  1081. if (!data->host_cookie)
  1082. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  1083. omap_hsmmc_get_dma_dir(host, data));
  1084. req_in_progress = host->req_in_progress;
  1085. dma_ch = host->dma_ch;
  1086. host->dma_ch = -1;
  1087. spin_unlock(&host->irq_lock);
  1088. omap_free_dma(dma_ch);
  1089. /* If DMA has finished after TC, complete the request */
  1090. if (!req_in_progress) {
  1091. struct mmc_request *mrq = host->mrq;
  1092. host->mrq = NULL;
  1093. mmc_request_done(host->mmc, mrq);
  1094. }
  1095. }
  1096. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1097. struct mmc_data *data,
  1098. struct omap_hsmmc_next *next)
  1099. {
  1100. int dma_len;
  1101. if (!next && data->host_cookie &&
  1102. data->host_cookie != host->next_data.cookie) {
  1103. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1104. " host->next_data.cookie %d\n",
  1105. __func__, data->host_cookie, host->next_data.cookie);
  1106. data->host_cookie = 0;
  1107. }
  1108. /* Check if next job is already prepared */
  1109. if (next ||
  1110. (!next && data->host_cookie != host->next_data.cookie)) {
  1111. dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  1112. data->sg_len,
  1113. omap_hsmmc_get_dma_dir(host, data));
  1114. } else {
  1115. dma_len = host->next_data.dma_len;
  1116. host->next_data.dma_len = 0;
  1117. }
  1118. if (dma_len == 0)
  1119. return -EINVAL;
  1120. if (next) {
  1121. next->dma_len = dma_len;
  1122. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1123. } else
  1124. host->dma_len = dma_len;
  1125. return 0;
  1126. }
  1127. /*
  1128. * Routine to configure and start DMA for the MMC card
  1129. */
  1130. static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
  1131. struct mmc_request *req)
  1132. {
  1133. int dma_ch = 0, ret = 0, i;
  1134. struct mmc_data *data = req->data;
  1135. /* Sanity check: all the SG entries must be aligned by block size. */
  1136. for (i = 0; i < data->sg_len; i++) {
  1137. struct scatterlist *sgl;
  1138. sgl = data->sg + i;
  1139. if (sgl->length % data->blksz)
  1140. return -EINVAL;
  1141. }
  1142. if ((data->blksz % 4) != 0)
  1143. /* REVISIT: The MMC buffer increments only when MSB is written.
  1144. * Return error for blksz which is non multiple of four.
  1145. */
  1146. return -EINVAL;
  1147. BUG_ON(host->dma_ch != -1);
  1148. ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
  1149. "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
  1150. if (ret != 0) {
  1151. dev_err(mmc_dev(host->mmc),
  1152. "%s: omap_request_dma() failed with %d\n",
  1153. mmc_hostname(host->mmc), ret);
  1154. return ret;
  1155. }
  1156. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
  1157. if (ret)
  1158. return ret;
  1159. host->dma_ch = dma_ch;
  1160. host->dma_sg_idx = 0;
  1161. omap_hsmmc_config_dma_params(host, data, data->sg);
  1162. return 0;
  1163. }
  1164. static void set_data_timeout(struct omap_hsmmc_host *host,
  1165. unsigned int timeout_ns,
  1166. unsigned int timeout_clks)
  1167. {
  1168. unsigned int timeout, cycle_ns;
  1169. uint32_t reg, clkd, dto = 0;
  1170. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1171. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1172. if (clkd == 0)
  1173. clkd = 1;
  1174. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  1175. timeout = timeout_ns / cycle_ns;
  1176. timeout += timeout_clks;
  1177. if (timeout) {
  1178. while ((timeout & 0x80000000) == 0) {
  1179. dto += 1;
  1180. timeout <<= 1;
  1181. }
  1182. dto = 31 - dto;
  1183. timeout <<= 1;
  1184. if (timeout && dto)
  1185. dto += 1;
  1186. if (dto >= 13)
  1187. dto -= 13;
  1188. else
  1189. dto = 0;
  1190. if (dto > 14)
  1191. dto = 14;
  1192. }
  1193. reg &= ~DTO_MASK;
  1194. reg |= dto << DTO_SHIFT;
  1195. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1196. }
  1197. /*
  1198. * Configure block length for MMC/SD cards and initiate the transfer.
  1199. */
  1200. static int
  1201. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1202. {
  1203. int ret;
  1204. host->data = req->data;
  1205. if (req->data == NULL) {
  1206. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1207. /*
  1208. * Set an arbitrary 100ms data timeout for commands with
  1209. * busy signal.
  1210. */
  1211. if (req->cmd->flags & MMC_RSP_BUSY)
  1212. set_data_timeout(host, 100000000U, 0);
  1213. return 0;
  1214. }
  1215. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1216. | (req->data->blocks << 16));
  1217. set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
  1218. if (host->use_dma) {
  1219. ret = omap_hsmmc_start_dma_transfer(host, req);
  1220. if (ret != 0) {
  1221. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  1222. return ret;
  1223. }
  1224. }
  1225. return 0;
  1226. }
  1227. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1228. int err)
  1229. {
  1230. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1231. struct mmc_data *data = mrq->data;
  1232. if (host->use_dma) {
  1233. if (data->host_cookie)
  1234. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  1235. data->sg_len,
  1236. omap_hsmmc_get_dma_dir(host, data));
  1237. data->host_cookie = 0;
  1238. }
  1239. }
  1240. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1241. bool is_first_req)
  1242. {
  1243. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1244. if (mrq->data->host_cookie) {
  1245. mrq->data->host_cookie = 0;
  1246. return ;
  1247. }
  1248. if (host->use_dma)
  1249. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1250. &host->next_data))
  1251. mrq->data->host_cookie = 0;
  1252. }
  1253. /*
  1254. * Request function. for read/write operation
  1255. */
  1256. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1257. {
  1258. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1259. int err;
  1260. BUG_ON(host->req_in_progress);
  1261. BUG_ON(host->dma_ch != -1);
  1262. if (host->protect_card) {
  1263. if (host->reqs_blocked < 3) {
  1264. /*
  1265. * Ensure the controller is left in a consistent
  1266. * state by resetting the command and data state
  1267. * machines.
  1268. */
  1269. omap_hsmmc_reset_controller_fsm(host, SRD);
  1270. omap_hsmmc_reset_controller_fsm(host, SRC);
  1271. host->reqs_blocked += 1;
  1272. }
  1273. req->cmd->error = -EBADF;
  1274. if (req->data)
  1275. req->data->error = -EBADF;
  1276. req->cmd->retries = 0;
  1277. mmc_request_done(mmc, req);
  1278. return;
  1279. } else if (host->reqs_blocked)
  1280. host->reqs_blocked = 0;
  1281. WARN_ON(host->mrq != NULL);
  1282. host->mrq = req;
  1283. err = omap_hsmmc_prepare_data(host, req);
  1284. if (err) {
  1285. req->cmd->error = err;
  1286. if (req->data)
  1287. req->data->error = err;
  1288. host->mrq = NULL;
  1289. mmc_request_done(mmc, req);
  1290. return;
  1291. }
  1292. omap_hsmmc_start_command(host, req->cmd, req->data);
  1293. }
  1294. /* Routine to configure clock values. Exposed API to core */
  1295. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1296. {
  1297. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1298. int do_send_init_stream = 0;
  1299. pm_runtime_get_sync(host->dev);
  1300. if (ios->power_mode != host->power_mode) {
  1301. switch (ios->power_mode) {
  1302. case MMC_POWER_OFF:
  1303. mmc_slot(host).set_power(host->dev, host->slot_id,
  1304. 0, 0);
  1305. host->vdd = 0;
  1306. break;
  1307. case MMC_POWER_UP:
  1308. mmc_slot(host).set_power(host->dev, host->slot_id,
  1309. 1, ios->vdd);
  1310. host->vdd = ios->vdd;
  1311. break;
  1312. case MMC_POWER_ON:
  1313. do_send_init_stream = 1;
  1314. break;
  1315. }
  1316. host->power_mode = ios->power_mode;
  1317. }
  1318. /* FIXME: set registers based only on changes to ios */
  1319. omap_hsmmc_set_bus_width(host);
  1320. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1321. /* Only MMC1 can interface at 3V without some flavor
  1322. * of external transceiver; but they all handle 1.8V.
  1323. */
  1324. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1325. (ios->vdd == DUAL_VOLT_OCR_BIT) &&
  1326. /*
  1327. * With pbias cell programming missing, this
  1328. * can't be allowed when booting with device
  1329. * tree.
  1330. */
  1331. !host->dev->of_node) {
  1332. /*
  1333. * The mmc_select_voltage fn of the core does
  1334. * not seem to set the power_mode to
  1335. * MMC_POWER_UP upon recalculating the voltage.
  1336. * vdd 1.8v.
  1337. */
  1338. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1339. dev_dbg(mmc_dev(host->mmc),
  1340. "Switch operation failed\n");
  1341. }
  1342. }
  1343. omap_hsmmc_set_clock(host);
  1344. if (do_send_init_stream)
  1345. send_init_stream(host);
  1346. omap_hsmmc_set_bus_mode(host);
  1347. pm_runtime_put_autosuspend(host->dev);
  1348. }
  1349. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1350. {
  1351. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1352. if (!mmc_slot(host).card_detect)
  1353. return -ENOSYS;
  1354. return mmc_slot(host).card_detect(host->dev, host->slot_id);
  1355. }
  1356. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  1357. {
  1358. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1359. if (!mmc_slot(host).get_ro)
  1360. return -ENOSYS;
  1361. return mmc_slot(host).get_ro(host->dev, 0);
  1362. }
  1363. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1364. {
  1365. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1366. if (mmc_slot(host).init_card)
  1367. mmc_slot(host).init_card(card);
  1368. }
  1369. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1370. {
  1371. u32 hctl, capa, value;
  1372. /* Only MMC1 supports 3.0V */
  1373. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1374. hctl = SDVS30;
  1375. capa = VS30 | VS18;
  1376. } else {
  1377. hctl = SDVS18;
  1378. capa = VS18;
  1379. }
  1380. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1381. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1382. value = OMAP_HSMMC_READ(host->base, CAPA);
  1383. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1384. /* Set the controller to AUTO IDLE mode */
  1385. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  1386. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  1387. /* Set SD bus power bit */
  1388. set_sd_bus_power(host);
  1389. }
  1390. static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
  1391. {
  1392. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1393. pm_runtime_get_sync(host->dev);
  1394. return 0;
  1395. }
  1396. static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
  1397. {
  1398. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1399. pm_runtime_mark_last_busy(host->dev);
  1400. pm_runtime_put_autosuspend(host->dev);
  1401. return 0;
  1402. }
  1403. static const struct mmc_host_ops omap_hsmmc_ops = {
  1404. .enable = omap_hsmmc_enable_fclk,
  1405. .disable = omap_hsmmc_disable_fclk,
  1406. .post_req = omap_hsmmc_post_req,
  1407. .pre_req = omap_hsmmc_pre_req,
  1408. .request = omap_hsmmc_request,
  1409. .set_ios = omap_hsmmc_set_ios,
  1410. .get_cd = omap_hsmmc_get_cd,
  1411. .get_ro = omap_hsmmc_get_ro,
  1412. .init_card = omap_hsmmc_init_card,
  1413. /* NYET -- enable_sdio_irq */
  1414. };
  1415. #ifdef CONFIG_DEBUG_FS
  1416. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1417. {
  1418. struct mmc_host *mmc = s->private;
  1419. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1420. int context_loss = 0;
  1421. if (host->pdata->get_context_loss_count)
  1422. context_loss = host->pdata->get_context_loss_count(host->dev);
  1423. seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
  1424. mmc->index, host->context_loss, context_loss);
  1425. if (host->suspended) {
  1426. seq_printf(s, "host suspended, can't read registers\n");
  1427. return 0;
  1428. }
  1429. pm_runtime_get_sync(host->dev);
  1430. seq_printf(s, "SYSCONFIG:\t0x%08x\n",
  1431. OMAP_HSMMC_READ(host->base, SYSCONFIG));
  1432. seq_printf(s, "CON:\t\t0x%08x\n",
  1433. OMAP_HSMMC_READ(host->base, CON));
  1434. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1435. OMAP_HSMMC_READ(host->base, HCTL));
  1436. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1437. OMAP_HSMMC_READ(host->base, SYSCTL));
  1438. seq_printf(s, "IE:\t\t0x%08x\n",
  1439. OMAP_HSMMC_READ(host->base, IE));
  1440. seq_printf(s, "ISE:\t\t0x%08x\n",
  1441. OMAP_HSMMC_READ(host->base, ISE));
  1442. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1443. OMAP_HSMMC_READ(host->base, CAPA));
  1444. pm_runtime_mark_last_busy(host->dev);
  1445. pm_runtime_put_autosuspend(host->dev);
  1446. return 0;
  1447. }
  1448. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1449. {
  1450. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1451. }
  1452. static const struct file_operations mmc_regs_fops = {
  1453. .open = omap_hsmmc_regs_open,
  1454. .read = seq_read,
  1455. .llseek = seq_lseek,
  1456. .release = single_release,
  1457. };
  1458. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1459. {
  1460. if (mmc->debugfs_root)
  1461. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1462. mmc, &mmc_regs_fops);
  1463. }
  1464. #else
  1465. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1466. {
  1467. }
  1468. #endif
  1469. #ifdef CONFIG_OF
  1470. static u16 omap4_reg_offset = 0x100;
  1471. static const struct of_device_id omap_mmc_of_match[] = {
  1472. {
  1473. .compatible = "ti,omap2-hsmmc",
  1474. },
  1475. {
  1476. .compatible = "ti,omap3-hsmmc",
  1477. },
  1478. {
  1479. .compatible = "ti,omap4-hsmmc",
  1480. .data = &omap4_reg_offset,
  1481. },
  1482. {},
  1483. };
  1484. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1485. static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1486. {
  1487. struct omap_mmc_platform_data *pdata;
  1488. struct device_node *np = dev->of_node;
  1489. u32 bus_width;
  1490. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1491. if (!pdata)
  1492. return NULL; /* out of memory */
  1493. if (of_find_property(np, "ti,dual-volt", NULL))
  1494. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1495. /* This driver only supports 1 slot */
  1496. pdata->nr_slots = 1;
  1497. pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
  1498. pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
  1499. if (of_find_property(np, "ti,non-removable", NULL)) {
  1500. pdata->slots[0].nonremovable = true;
  1501. pdata->slots[0].no_regulator_off_init = true;
  1502. }
  1503. of_property_read_u32(np, "ti,bus-width", &bus_width);
  1504. if (bus_width == 4)
  1505. pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
  1506. else if (bus_width == 8)
  1507. pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
  1508. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1509. pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
  1510. return pdata;
  1511. }
  1512. #else
  1513. static inline struct omap_mmc_platform_data
  1514. *of_get_hsmmc_pdata(struct device *dev)
  1515. {
  1516. return NULL;
  1517. }
  1518. #endif
  1519. static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
  1520. {
  1521. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1522. struct mmc_host *mmc;
  1523. struct omap_hsmmc_host *host = NULL;
  1524. struct resource *res;
  1525. int ret, irq;
  1526. const struct of_device_id *match;
  1527. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1528. if (match) {
  1529. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1530. if (match->data) {
  1531. u16 *offsetp = match->data;
  1532. pdata->reg_offset = *offsetp;
  1533. }
  1534. }
  1535. if (pdata == NULL) {
  1536. dev_err(&pdev->dev, "Platform Data is missing\n");
  1537. return -ENXIO;
  1538. }
  1539. if (pdata->nr_slots == 0) {
  1540. dev_err(&pdev->dev, "No Slots\n");
  1541. return -ENXIO;
  1542. }
  1543. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1544. irq = platform_get_irq(pdev, 0);
  1545. if (res == NULL || irq < 0)
  1546. return -ENXIO;
  1547. res = request_mem_region(res->start, resource_size(res), pdev->name);
  1548. if (res == NULL)
  1549. return -EBUSY;
  1550. ret = omap_hsmmc_gpio_init(pdata);
  1551. if (ret)
  1552. goto err;
  1553. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1554. if (!mmc) {
  1555. ret = -ENOMEM;
  1556. goto err_alloc;
  1557. }
  1558. host = mmc_priv(mmc);
  1559. host->mmc = mmc;
  1560. host->pdata = pdata;
  1561. host->dev = &pdev->dev;
  1562. host->use_dma = 1;
  1563. host->dev->dma_mask = &pdata->dma_mask;
  1564. host->dma_ch = -1;
  1565. host->irq = irq;
  1566. host->slot_id = 0;
  1567. host->mapbase = res->start + pdata->reg_offset;
  1568. host->base = ioremap(host->mapbase, SZ_4K);
  1569. host->power_mode = MMC_POWER_OFF;
  1570. host->flags = AUTO_CMD12;
  1571. host->next_data.cookie = 1;
  1572. platform_set_drvdata(pdev, host);
  1573. mmc->ops = &omap_hsmmc_ops;
  1574. /*
  1575. * If regulator_disable can only put vcc_aux to sleep then there is
  1576. * no off state.
  1577. */
  1578. if (mmc_slot(host).vcc_aux_disable_is_sleep)
  1579. mmc_slot(host).no_off = 1;
  1580. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1581. if (pdata->max_freq > 0)
  1582. mmc->f_max = pdata->max_freq;
  1583. else
  1584. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1585. spin_lock_init(&host->irq_lock);
  1586. host->fclk = clk_get(&pdev->dev, "fck");
  1587. if (IS_ERR(host->fclk)) {
  1588. ret = PTR_ERR(host->fclk);
  1589. host->fclk = NULL;
  1590. goto err1;
  1591. }
  1592. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1593. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1594. mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
  1595. }
  1596. pm_runtime_enable(host->dev);
  1597. pm_runtime_get_sync(host->dev);
  1598. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1599. pm_runtime_use_autosuspend(host->dev);
  1600. omap_hsmmc_context_save(host);
  1601. if (cpu_is_omap2430()) {
  1602. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  1603. /*
  1604. * MMC can still work without debounce clock.
  1605. */
  1606. if (IS_ERR(host->dbclk))
  1607. dev_warn(mmc_dev(host->mmc),
  1608. "Failed to get debounce clock\n");
  1609. else
  1610. host->got_dbclk = 1;
  1611. if (host->got_dbclk)
  1612. if (clk_enable(host->dbclk) != 0)
  1613. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  1614. " clk failed\n");
  1615. }
  1616. /* Since we do only SG emulation, we can have as many segs
  1617. * as we want. */
  1618. mmc->max_segs = 1024;
  1619. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1620. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1621. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1622. mmc->max_seg_size = mmc->max_req_size;
  1623. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1624. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1625. mmc->caps |= mmc_slot(host).caps;
  1626. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1627. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1628. if (mmc_slot(host).nonremovable)
  1629. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1630. mmc->pm_caps = mmc_slot(host).pm_caps;
  1631. omap_hsmmc_conf_bus_power(host);
  1632. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1633. if (!res) {
  1634. dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
  1635. goto err_irq;
  1636. }
  1637. host->dma_line_tx = res->start;
  1638. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1639. if (!res) {
  1640. dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
  1641. goto err_irq;
  1642. }
  1643. host->dma_line_rx = res->start;
  1644. /* Request IRQ for MMC operations */
  1645. ret = request_irq(host->irq, omap_hsmmc_irq, 0,
  1646. mmc_hostname(mmc), host);
  1647. if (ret) {
  1648. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1649. goto err_irq;
  1650. }
  1651. if (pdata->init != NULL) {
  1652. if (pdata->init(&pdev->dev) != 0) {
  1653. dev_dbg(mmc_dev(host->mmc),
  1654. "Unable to configure MMC IRQs\n");
  1655. goto err_irq_cd_init;
  1656. }
  1657. }
  1658. if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
  1659. ret = omap_hsmmc_reg_get(host);
  1660. if (ret)
  1661. goto err_reg;
  1662. host->use_reg = 1;
  1663. }
  1664. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1665. /* Request IRQ for card detect */
  1666. if ((mmc_slot(host).card_detect_irq)) {
  1667. ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
  1668. NULL,
  1669. omap_hsmmc_detect,
  1670. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1671. mmc_hostname(mmc), host);
  1672. if (ret) {
  1673. dev_dbg(mmc_dev(host->mmc),
  1674. "Unable to grab MMC CD IRQ\n");
  1675. goto err_irq_cd;
  1676. }
  1677. pdata->suspend = omap_hsmmc_suspend_cdirq;
  1678. pdata->resume = omap_hsmmc_resume_cdirq;
  1679. }
  1680. omap_hsmmc_disable_irq(host);
  1681. omap_hsmmc_protect_card(host);
  1682. mmc_add_host(mmc);
  1683. if (mmc_slot(host).name != NULL) {
  1684. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1685. if (ret < 0)
  1686. goto err_slot_name;
  1687. }
  1688. if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
  1689. ret = device_create_file(&mmc->class_dev,
  1690. &dev_attr_cover_switch);
  1691. if (ret < 0)
  1692. goto err_slot_name;
  1693. }
  1694. omap_hsmmc_debugfs(mmc);
  1695. pm_runtime_mark_last_busy(host->dev);
  1696. pm_runtime_put_autosuspend(host->dev);
  1697. return 0;
  1698. err_slot_name:
  1699. mmc_remove_host(mmc);
  1700. free_irq(mmc_slot(host).card_detect_irq, host);
  1701. err_irq_cd:
  1702. if (host->use_reg)
  1703. omap_hsmmc_reg_put(host);
  1704. err_reg:
  1705. if (host->pdata->cleanup)
  1706. host->pdata->cleanup(&pdev->dev);
  1707. err_irq_cd_init:
  1708. free_irq(host->irq, host);
  1709. err_irq:
  1710. pm_runtime_put_sync(host->dev);
  1711. pm_runtime_disable(host->dev);
  1712. clk_put(host->fclk);
  1713. if (host->got_dbclk) {
  1714. clk_disable(host->dbclk);
  1715. clk_put(host->dbclk);
  1716. }
  1717. err1:
  1718. iounmap(host->base);
  1719. platform_set_drvdata(pdev, NULL);
  1720. mmc_free_host(mmc);
  1721. err_alloc:
  1722. omap_hsmmc_gpio_free(pdata);
  1723. err:
  1724. release_mem_region(res->start, resource_size(res));
  1725. return ret;
  1726. }
  1727. static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
  1728. {
  1729. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1730. struct resource *res;
  1731. pm_runtime_get_sync(host->dev);
  1732. mmc_remove_host(host->mmc);
  1733. if (host->use_reg)
  1734. omap_hsmmc_reg_put(host);
  1735. if (host->pdata->cleanup)
  1736. host->pdata->cleanup(&pdev->dev);
  1737. free_irq(host->irq, host);
  1738. if (mmc_slot(host).card_detect_irq)
  1739. free_irq(mmc_slot(host).card_detect_irq, host);
  1740. pm_runtime_put_sync(host->dev);
  1741. pm_runtime_disable(host->dev);
  1742. clk_put(host->fclk);
  1743. if (host->got_dbclk) {
  1744. clk_disable(host->dbclk);
  1745. clk_put(host->dbclk);
  1746. }
  1747. mmc_free_host(host->mmc);
  1748. iounmap(host->base);
  1749. omap_hsmmc_gpio_free(pdev->dev.platform_data);
  1750. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1751. if (res)
  1752. release_mem_region(res->start, resource_size(res));
  1753. platform_set_drvdata(pdev, NULL);
  1754. return 0;
  1755. }
  1756. #ifdef CONFIG_PM
  1757. static int omap_hsmmc_suspend(struct device *dev)
  1758. {
  1759. int ret = 0;
  1760. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1761. if (!host)
  1762. return 0;
  1763. if (host && host->suspended)
  1764. return 0;
  1765. pm_runtime_get_sync(host->dev);
  1766. host->suspended = 1;
  1767. if (host->pdata->suspend) {
  1768. ret = host->pdata->suspend(dev, host->slot_id);
  1769. if (ret) {
  1770. dev_dbg(dev, "Unable to handle MMC board"
  1771. " level suspend\n");
  1772. host->suspended = 0;
  1773. return ret;
  1774. }
  1775. }
  1776. ret = mmc_suspend_host(host->mmc);
  1777. if (ret) {
  1778. host->suspended = 0;
  1779. if (host->pdata->resume) {
  1780. ret = host->pdata->resume(dev, host->slot_id);
  1781. if (ret)
  1782. dev_dbg(dev, "Unmask interrupt failed\n");
  1783. }
  1784. goto err;
  1785. }
  1786. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1787. omap_hsmmc_disable_irq(host);
  1788. OMAP_HSMMC_WRITE(host->base, HCTL,
  1789. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1790. }
  1791. if (host->got_dbclk)
  1792. clk_disable(host->dbclk);
  1793. err:
  1794. pm_runtime_put_sync(host->dev);
  1795. return ret;
  1796. }
  1797. /* Routine to resume the MMC device */
  1798. static int omap_hsmmc_resume(struct device *dev)
  1799. {
  1800. int ret = 0;
  1801. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1802. if (!host)
  1803. return 0;
  1804. if (host && !host->suspended)
  1805. return 0;
  1806. pm_runtime_get_sync(host->dev);
  1807. if (host->got_dbclk)
  1808. clk_enable(host->dbclk);
  1809. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1810. omap_hsmmc_conf_bus_power(host);
  1811. if (host->pdata->resume) {
  1812. ret = host->pdata->resume(dev, host->slot_id);
  1813. if (ret)
  1814. dev_dbg(dev, "Unmask interrupt failed\n");
  1815. }
  1816. omap_hsmmc_protect_card(host);
  1817. /* Notify the core to resume the host */
  1818. ret = mmc_resume_host(host->mmc);
  1819. if (ret == 0)
  1820. host->suspended = 0;
  1821. pm_runtime_mark_last_busy(host->dev);
  1822. pm_runtime_put_autosuspend(host->dev);
  1823. return ret;
  1824. }
  1825. #else
  1826. #define omap_hsmmc_suspend NULL
  1827. #define omap_hsmmc_resume NULL
  1828. #endif
  1829. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1830. {
  1831. struct omap_hsmmc_host *host;
  1832. host = platform_get_drvdata(to_platform_device(dev));
  1833. omap_hsmmc_context_save(host);
  1834. dev_dbg(dev, "disabled\n");
  1835. return 0;
  1836. }
  1837. static int omap_hsmmc_runtime_resume(struct device *dev)
  1838. {
  1839. struct omap_hsmmc_host *host;
  1840. host = platform_get_drvdata(to_platform_device(dev));
  1841. omap_hsmmc_context_restore(host);
  1842. dev_dbg(dev, "enabled\n");
  1843. return 0;
  1844. }
  1845. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1846. .suspend = omap_hsmmc_suspend,
  1847. .resume = omap_hsmmc_resume,
  1848. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1849. .runtime_resume = omap_hsmmc_runtime_resume,
  1850. };
  1851. static struct platform_driver omap_hsmmc_driver = {
  1852. .probe = omap_hsmmc_probe,
  1853. .remove = __devexit_p(omap_hsmmc_remove),
  1854. .driver = {
  1855. .name = DRIVER_NAME,
  1856. .owner = THIS_MODULE,
  1857. .pm = &omap_hsmmc_dev_pm_ops,
  1858. .of_match_table = of_match_ptr(omap_mmc_of_match),
  1859. },
  1860. };
  1861. module_platform_driver(omap_hsmmc_driver);
  1862. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1863. MODULE_LICENSE("GPL");
  1864. MODULE_ALIAS("platform:" DRIVER_NAME);
  1865. MODULE_AUTHOR("Texas Instruments Inc");