omap_hsmmc.c 53 KB

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