omap_hsmmc.c 52 KB

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