omap_hsmmc.c 53 KB

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