omap_hsmmc.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/timer.h>
  27. #include <linux/clk.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/io.h>
  30. #include <linux/semaphore.h>
  31. #include <mach/dma.h>
  32. #include <mach/hardware.h>
  33. #include <mach/board.h>
  34. #include <mach/mmc.h>
  35. #include <mach/cpu.h>
  36. /* OMAP HSMMC Host Controller Registers */
  37. #define OMAP_HSMMC_SYSCONFIG 0x0010
  38. #define OMAP_HSMMC_SYSSTATUS 0x0014
  39. #define OMAP_HSMMC_CON 0x002C
  40. #define OMAP_HSMMC_BLK 0x0104
  41. #define OMAP_HSMMC_ARG 0x0108
  42. #define OMAP_HSMMC_CMD 0x010C
  43. #define OMAP_HSMMC_RSP10 0x0110
  44. #define OMAP_HSMMC_RSP32 0x0114
  45. #define OMAP_HSMMC_RSP54 0x0118
  46. #define OMAP_HSMMC_RSP76 0x011C
  47. #define OMAP_HSMMC_DATA 0x0120
  48. #define OMAP_HSMMC_HCTL 0x0128
  49. #define OMAP_HSMMC_SYSCTL 0x012C
  50. #define OMAP_HSMMC_STAT 0x0130
  51. #define OMAP_HSMMC_IE 0x0134
  52. #define OMAP_HSMMC_ISE 0x0138
  53. #define OMAP_HSMMC_CAPA 0x0140
  54. #define VS18 (1 << 26)
  55. #define VS30 (1 << 25)
  56. #define SDVS18 (0x5 << 9)
  57. #define SDVS30 (0x6 << 9)
  58. #define SDVS33 (0x7 << 9)
  59. #define SDVS_MASK 0x00000E00
  60. #define SDVSCLR 0xFFFFF1FF
  61. #define SDVSDET 0x00000400
  62. #define AUTOIDLE 0x1
  63. #define SDBP (1 << 8)
  64. #define DTO 0xe
  65. #define ICE 0x1
  66. #define ICS 0x2
  67. #define CEN (1 << 2)
  68. #define CLKD_MASK 0x0000FFC0
  69. #define CLKD_SHIFT 6
  70. #define DTO_MASK 0x000F0000
  71. #define DTO_SHIFT 16
  72. #define INT_EN_MASK 0x307F0033
  73. #define BWR_ENABLE (1 << 4)
  74. #define BRR_ENABLE (1 << 5)
  75. #define INIT_STREAM (1 << 1)
  76. #define DP_SELECT (1 << 21)
  77. #define DDIR (1 << 4)
  78. #define DMA_EN 0x1
  79. #define MSBS (1 << 5)
  80. #define BCE (1 << 1)
  81. #define FOUR_BIT (1 << 1)
  82. #define DW8 (1 << 5)
  83. #define CC 0x1
  84. #define TC 0x02
  85. #define OD 0x1
  86. #define ERR (1 << 15)
  87. #define CMD_TIMEOUT (1 << 16)
  88. #define DATA_TIMEOUT (1 << 20)
  89. #define CMD_CRC (1 << 17)
  90. #define DATA_CRC (1 << 21)
  91. #define CARD_ERR (1 << 28)
  92. #define STAT_CLEAR 0xFFFFFFFF
  93. #define INIT_STREAM_CMD 0x00000000
  94. #define DUAL_VOLT_OCR_BIT 7
  95. #define SRC (1 << 25)
  96. #define SRD (1 << 26)
  97. #define SOFTRESET (1 << 1)
  98. #define RESETDONE (1 << 0)
  99. /*
  100. * FIXME: Most likely all the data using these _DEVID defines should come
  101. * from the platform_data, or implemented in controller and slot specific
  102. * functions.
  103. */
  104. #define OMAP_MMC1_DEVID 0
  105. #define OMAP_MMC2_DEVID 1
  106. #define OMAP_MMC3_DEVID 2
  107. #define MMC_TIMEOUT_MS 20
  108. #define OMAP_MMC_MASTER_CLOCK 96000000
  109. #define DRIVER_NAME "mmci-omap-hs"
  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 mmc_omap_host {
  124. struct device *dev;
  125. struct mmc_host *mmc;
  126. struct mmc_request *mrq;
  127. struct mmc_command *cmd;
  128. struct mmc_data *data;
  129. struct clk *fclk;
  130. struct clk *iclk;
  131. struct clk *dbclk;
  132. struct semaphore sem;
  133. struct work_struct mmc_carddetect_work;
  134. void __iomem *base;
  135. resource_size_t mapbase;
  136. unsigned int id;
  137. unsigned int dma_len;
  138. unsigned int dma_sg_idx;
  139. unsigned char bus_mode;
  140. unsigned char power_mode;
  141. u32 *buffer;
  142. u32 bytesleft;
  143. int suspended;
  144. int irq;
  145. int carddetect;
  146. int use_dma, dma_ch;
  147. int dma_line_tx, dma_line_rx;
  148. int slot_id;
  149. int dbclk_enabled;
  150. int response_busy;
  151. int context_loss;
  152. struct omap_mmc_platform_data *pdata;
  153. };
  154. /*
  155. * Stop clock to the card
  156. */
  157. static void omap_mmc_stop_clock(struct mmc_omap_host *host)
  158. {
  159. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  160. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  161. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  162. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
  163. }
  164. #ifdef CONFIG_PM
  165. /*
  166. * Restore the MMC host context, if it was lost as result of a
  167. * power state change.
  168. */
  169. static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
  170. {
  171. struct mmc_ios *ios = &host->mmc->ios;
  172. struct omap_mmc_platform_data *pdata = host->pdata;
  173. int context_loss = 0;
  174. u32 hctl, capa, con;
  175. u16 dsor = 0;
  176. unsigned long timeout;
  177. if (pdata->get_context_loss_count) {
  178. context_loss = pdata->get_context_loss_count(host->dev);
  179. if (context_loss < 0)
  180. return 1;
  181. }
  182. dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
  183. context_loss == host->context_loss ? "not " : "");
  184. if (host->context_loss == context_loss)
  185. return 1;
  186. /* Wait for hardware reset */
  187. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  188. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  189. && time_before(jiffies, timeout))
  190. ;
  191. /* Do software reset */
  192. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
  193. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  194. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  195. && time_before(jiffies, timeout))
  196. ;
  197. OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
  198. OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
  199. if (host->id == OMAP_MMC1_DEVID) {
  200. if (host->power_mode != MMC_POWER_OFF &&
  201. (1 << ios->vdd) <= MMC_VDD_23_24)
  202. hctl = SDVS18;
  203. else
  204. hctl = SDVS30;
  205. capa = VS30 | VS18;
  206. } else {
  207. hctl = SDVS18;
  208. capa = VS18;
  209. }
  210. OMAP_HSMMC_WRITE(host->base, HCTL,
  211. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  212. OMAP_HSMMC_WRITE(host->base, CAPA,
  213. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  214. OMAP_HSMMC_WRITE(host->base, HCTL,
  215. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  216. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  217. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  218. && time_before(jiffies, timeout))
  219. ;
  220. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  221. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  222. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  223. /* Do not initialize card-specific things if the power is off */
  224. if (host->power_mode == MMC_POWER_OFF)
  225. goto out;
  226. con = OMAP_HSMMC_READ(host->base, CON);
  227. switch (ios->bus_width) {
  228. case MMC_BUS_WIDTH_8:
  229. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  230. break;
  231. case MMC_BUS_WIDTH_4:
  232. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  233. OMAP_HSMMC_WRITE(host->base, HCTL,
  234. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  235. break;
  236. case MMC_BUS_WIDTH_1:
  237. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  238. OMAP_HSMMC_WRITE(host->base, HCTL,
  239. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  240. break;
  241. }
  242. if (ios->clock) {
  243. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  244. if (dsor < 1)
  245. dsor = 1;
  246. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  247. dsor++;
  248. if (dsor > 250)
  249. dsor = 250;
  250. }
  251. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  252. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  253. OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
  254. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  255. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  256. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  257. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  258. && time_before(jiffies, timeout))
  259. ;
  260. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  261. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  262. con = OMAP_HSMMC_READ(host->base, CON);
  263. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  264. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  265. else
  266. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  267. out:
  268. host->context_loss = context_loss;
  269. dev_dbg(mmc_dev(host->mmc), "context is restored\n");
  270. return 0;
  271. }
  272. /*
  273. * Save the MMC host context (store the number of power state changes so far).
  274. */
  275. static void omap_mmc_save_ctx(struct mmc_omap_host *host)
  276. {
  277. struct omap_mmc_platform_data *pdata = host->pdata;
  278. int context_loss;
  279. if (pdata->get_context_loss_count) {
  280. context_loss = pdata->get_context_loss_count(host->dev);
  281. if (context_loss < 0)
  282. return;
  283. host->context_loss = context_loss;
  284. }
  285. }
  286. #else
  287. static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
  288. {
  289. return 0;
  290. }
  291. static void omap_mmc_save_ctx(struct mmc_omap_host *host)
  292. {
  293. }
  294. #endif
  295. /*
  296. * Send init stream sequence to card
  297. * before sending IDLE command
  298. */
  299. static void send_init_stream(struct mmc_omap_host *host)
  300. {
  301. int reg = 0;
  302. unsigned long timeout;
  303. disable_irq(host->irq);
  304. OMAP_HSMMC_WRITE(host->base, CON,
  305. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  306. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  307. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  308. while ((reg != CC) && time_before(jiffies, timeout))
  309. reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
  310. OMAP_HSMMC_WRITE(host->base, CON,
  311. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  312. enable_irq(host->irq);
  313. }
  314. static inline
  315. int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
  316. {
  317. int r = 1;
  318. if (host->pdata->slots[host->slot_id].get_cover_state)
  319. r = host->pdata->slots[host->slot_id].get_cover_state(host->dev,
  320. host->slot_id);
  321. return r;
  322. }
  323. static ssize_t
  324. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  325. char *buf)
  326. {
  327. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  328. struct mmc_omap_host *host = mmc_priv(mmc);
  329. return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
  330. "open");
  331. }
  332. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  333. static ssize_t
  334. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  335. char *buf)
  336. {
  337. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  338. struct mmc_omap_host *host = mmc_priv(mmc);
  339. struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
  340. return sprintf(buf, "%s\n", slot.name);
  341. }
  342. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  343. /*
  344. * Configure the response type and send the cmd.
  345. */
  346. static void
  347. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
  348. struct mmc_data *data)
  349. {
  350. int cmdreg = 0, resptype = 0, cmdtype = 0;
  351. dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  352. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  353. host->cmd = cmd;
  354. /*
  355. * Clear status bits and enable interrupts
  356. */
  357. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  358. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  359. if (host->use_dma)
  360. OMAP_HSMMC_WRITE(host->base, IE,
  361. INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
  362. else
  363. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  364. host->response_busy = 0;
  365. if (cmd->flags & MMC_RSP_PRESENT) {
  366. if (cmd->flags & MMC_RSP_136)
  367. resptype = 1;
  368. else if (cmd->flags & MMC_RSP_BUSY) {
  369. resptype = 3;
  370. host->response_busy = 1;
  371. } else
  372. resptype = 2;
  373. }
  374. /*
  375. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  376. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  377. * a val of 0x3, rest 0x0.
  378. */
  379. if (cmd == host->mrq->stop)
  380. cmdtype = 0x3;
  381. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  382. if (data) {
  383. cmdreg |= DP_SELECT | MSBS | BCE;
  384. if (data->flags & MMC_DATA_READ)
  385. cmdreg |= DDIR;
  386. else
  387. cmdreg &= ~(DDIR);
  388. }
  389. if (host->use_dma)
  390. cmdreg |= DMA_EN;
  391. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  392. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  393. }
  394. static int
  395. mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
  396. {
  397. if (data->flags & MMC_DATA_WRITE)
  398. return DMA_TO_DEVICE;
  399. else
  400. return DMA_FROM_DEVICE;
  401. }
  402. /*
  403. * Notify the transfer complete to MMC core
  404. */
  405. static void
  406. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  407. {
  408. if (!data) {
  409. struct mmc_request *mrq = host->mrq;
  410. host->mrq = NULL;
  411. mmc_request_done(host->mmc, mrq);
  412. return;
  413. }
  414. host->data = NULL;
  415. if (host->use_dma && host->dma_ch != -1)
  416. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  417. mmc_omap_get_dma_dir(host, data));
  418. if (!data->error)
  419. data->bytes_xfered += data->blocks * (data->blksz);
  420. else
  421. data->bytes_xfered = 0;
  422. if (!data->stop) {
  423. host->mrq = NULL;
  424. mmc_request_done(host->mmc, data->mrq);
  425. return;
  426. }
  427. mmc_omap_start_command(host, data->stop, NULL);
  428. }
  429. /*
  430. * Notify the core about command completion
  431. */
  432. static void
  433. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  434. {
  435. host->cmd = NULL;
  436. if (cmd->flags & MMC_RSP_PRESENT) {
  437. if (cmd->flags & MMC_RSP_136) {
  438. /* response type 2 */
  439. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  440. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  441. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  442. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  443. } else {
  444. /* response types 1, 1b, 3, 4, 5, 6 */
  445. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  446. }
  447. }
  448. if ((host->data == NULL && !host->response_busy) || cmd->error) {
  449. host->mrq = NULL;
  450. mmc_request_done(host->mmc, cmd->mrq);
  451. }
  452. }
  453. /*
  454. * DMA clean up for command errors
  455. */
  456. static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
  457. {
  458. host->data->error = errno;
  459. if (host->use_dma && host->dma_ch != -1) {
  460. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
  461. mmc_omap_get_dma_dir(host, host->data));
  462. omap_free_dma(host->dma_ch);
  463. host->dma_ch = -1;
  464. up(&host->sem);
  465. }
  466. host->data = NULL;
  467. }
  468. /*
  469. * Readable error output
  470. */
  471. #ifdef CONFIG_MMC_DEBUG
  472. static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
  473. {
  474. /* --- means reserved bit without definition at documentation */
  475. static const char *mmc_omap_status_bits[] = {
  476. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  477. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  478. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  479. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  480. };
  481. char res[256];
  482. char *buf = res;
  483. int len, i;
  484. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  485. buf += len;
  486. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  487. if (status & (1 << i)) {
  488. len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
  489. buf += len;
  490. }
  491. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  492. }
  493. #endif /* CONFIG_MMC_DEBUG */
  494. /*
  495. * MMC controller internal state machines reset
  496. *
  497. * Used to reset command or data internal state machines, using respectively
  498. * SRC or SRD bit of SYSCTL register
  499. * Can be called from interrupt context
  500. */
  501. static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
  502. unsigned long bit)
  503. {
  504. unsigned long i = 0;
  505. unsigned long limit = (loops_per_jiffy *
  506. msecs_to_jiffies(MMC_TIMEOUT_MS));
  507. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  508. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  509. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  510. (i++ < limit))
  511. cpu_relax();
  512. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  513. dev_err(mmc_dev(host->mmc),
  514. "Timeout waiting on controller reset in %s\n",
  515. __func__);
  516. }
  517. /*
  518. * MMC controller IRQ handler
  519. */
  520. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  521. {
  522. struct mmc_omap_host *host = dev_id;
  523. struct mmc_data *data;
  524. int end_cmd = 0, end_trans = 0, status;
  525. if (host->mrq == NULL) {
  526. OMAP_HSMMC_WRITE(host->base, STAT,
  527. OMAP_HSMMC_READ(host->base, STAT));
  528. /* Flush posted write */
  529. OMAP_HSMMC_READ(host->base, STAT);
  530. return IRQ_HANDLED;
  531. }
  532. data = host->data;
  533. status = OMAP_HSMMC_READ(host->base, STAT);
  534. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  535. if (status & ERR) {
  536. #ifdef CONFIG_MMC_DEBUG
  537. mmc_omap_report_irq(host, status);
  538. #endif
  539. if ((status & CMD_TIMEOUT) ||
  540. (status & CMD_CRC)) {
  541. if (host->cmd) {
  542. if (status & CMD_TIMEOUT) {
  543. mmc_omap_reset_controller_fsm(host, SRC);
  544. host->cmd->error = -ETIMEDOUT;
  545. } else {
  546. host->cmd->error = -EILSEQ;
  547. }
  548. end_cmd = 1;
  549. }
  550. if (host->data || host->response_busy) {
  551. if (host->data)
  552. mmc_dma_cleanup(host, -ETIMEDOUT);
  553. host->response_busy = 0;
  554. mmc_omap_reset_controller_fsm(host, SRD);
  555. }
  556. }
  557. if ((status & DATA_TIMEOUT) ||
  558. (status & DATA_CRC)) {
  559. if (host->data || host->response_busy) {
  560. int err = (status & DATA_TIMEOUT) ?
  561. -ETIMEDOUT : -EILSEQ;
  562. if (host->data)
  563. mmc_dma_cleanup(host, err);
  564. else
  565. host->mrq->cmd->error = err;
  566. host->response_busy = 0;
  567. mmc_omap_reset_controller_fsm(host, SRD);
  568. end_trans = 1;
  569. }
  570. }
  571. if (status & CARD_ERR) {
  572. dev_dbg(mmc_dev(host->mmc),
  573. "Ignoring card err CMD%d\n", host->cmd->opcode);
  574. if (host->cmd)
  575. end_cmd = 1;
  576. if (host->data)
  577. end_trans = 1;
  578. }
  579. }
  580. OMAP_HSMMC_WRITE(host->base, STAT, status);
  581. /* Flush posted write */
  582. OMAP_HSMMC_READ(host->base, STAT);
  583. if (end_cmd || ((status & CC) && host->cmd))
  584. mmc_omap_cmd_done(host, host->cmd);
  585. if (end_trans || (status & TC))
  586. mmc_omap_xfer_done(host, data);
  587. return IRQ_HANDLED;
  588. }
  589. static void set_sd_bus_power(struct mmc_omap_host *host)
  590. {
  591. unsigned long i;
  592. OMAP_HSMMC_WRITE(host->base, HCTL,
  593. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  594. for (i = 0; i < loops_per_jiffy; i++) {
  595. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  596. break;
  597. cpu_relax();
  598. }
  599. }
  600. /*
  601. * Switch MMC interface voltage ... only relevant for MMC1.
  602. *
  603. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  604. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  605. * Some chips, like eMMC ones, use internal transceivers.
  606. */
  607. static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
  608. {
  609. u32 reg_val = 0;
  610. int ret;
  611. /* Disable the clocks */
  612. clk_disable(host->fclk);
  613. clk_disable(host->iclk);
  614. clk_disable(host->dbclk);
  615. /* Turn the power off */
  616. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  617. if (ret != 0)
  618. goto err;
  619. /* Turn the power ON with given VDD 1.8 or 3.0v */
  620. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
  621. if (ret != 0)
  622. goto err;
  623. clk_enable(host->fclk);
  624. clk_enable(host->iclk);
  625. clk_enable(host->dbclk);
  626. OMAP_HSMMC_WRITE(host->base, HCTL,
  627. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  628. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  629. /*
  630. * If a MMC dual voltage card is detected, the set_ios fn calls
  631. * this fn with VDD bit set for 1.8V. Upon card removal from the
  632. * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  633. *
  634. * Cope with a bit of slop in the range ... per data sheets:
  635. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  636. * but recommended values are 1.71V to 1.89V
  637. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  638. * but recommended values are 2.7V to 3.3V
  639. *
  640. * Board setup code shouldn't permit anything very out-of-range.
  641. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  642. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  643. */
  644. if ((1 << vdd) <= MMC_VDD_23_24)
  645. reg_val |= SDVS18;
  646. else
  647. reg_val |= SDVS30;
  648. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  649. set_sd_bus_power(host);
  650. return 0;
  651. err:
  652. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  653. return ret;
  654. }
  655. /*
  656. * Work Item to notify the core about card insertion/removal
  657. */
  658. static void mmc_omap_detect(struct work_struct *work)
  659. {
  660. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  661. mmc_carddetect_work);
  662. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  663. if (mmc_slot(host).card_detect)
  664. host->carddetect = slot->card_detect(slot->card_detect_irq);
  665. else
  666. host->carddetect = -ENOSYS;
  667. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  668. if (host->carddetect) {
  669. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  670. } else {
  671. mmc_host_enable(host->mmc);
  672. mmc_omap_reset_controller_fsm(host, SRD);
  673. mmc_host_lazy_disable(host->mmc);
  674. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  675. }
  676. }
  677. /*
  678. * ISR for handling card insertion and removal
  679. */
  680. static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
  681. {
  682. struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
  683. schedule_work(&host->mmc_carddetect_work);
  684. return IRQ_HANDLED;
  685. }
  686. static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
  687. struct mmc_data *data)
  688. {
  689. int sync_dev;
  690. if (data->flags & MMC_DATA_WRITE)
  691. sync_dev = host->dma_line_tx;
  692. else
  693. sync_dev = host->dma_line_rx;
  694. return sync_dev;
  695. }
  696. static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
  697. struct mmc_data *data,
  698. struct scatterlist *sgl)
  699. {
  700. int blksz, nblk, dma_ch;
  701. dma_ch = host->dma_ch;
  702. if (data->flags & MMC_DATA_WRITE) {
  703. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  704. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  705. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  706. sg_dma_address(sgl), 0, 0);
  707. } else {
  708. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  709. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  710. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  711. sg_dma_address(sgl), 0, 0);
  712. }
  713. blksz = host->data->blksz;
  714. nblk = sg_dma_len(sgl) / blksz;
  715. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  716. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  717. mmc_omap_get_dma_sync_dev(host, data),
  718. !(data->flags & MMC_DATA_WRITE));
  719. omap_start_dma(dma_ch);
  720. }
  721. /*
  722. * DMA call back function
  723. */
  724. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  725. {
  726. struct mmc_omap_host *host = data;
  727. if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
  728. dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
  729. if (host->dma_ch < 0)
  730. return;
  731. host->dma_sg_idx++;
  732. if (host->dma_sg_idx < host->dma_len) {
  733. /* Fire up the next transfer. */
  734. mmc_omap_config_dma_params(host, host->data,
  735. host->data->sg + host->dma_sg_idx);
  736. return;
  737. }
  738. omap_free_dma(host->dma_ch);
  739. host->dma_ch = -1;
  740. /*
  741. * DMA Callback: run in interrupt context.
  742. * mutex_unlock will throw a kernel warning if used.
  743. */
  744. up(&host->sem);
  745. }
  746. /*
  747. * Routine to configure and start DMA for the MMC card
  748. */
  749. static int
  750. mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
  751. {
  752. int dma_ch = 0, ret = 0, err = 1, i;
  753. struct mmc_data *data = req->data;
  754. /* Sanity check: all the SG entries must be aligned by block size. */
  755. for (i = 0; i < host->dma_len; i++) {
  756. struct scatterlist *sgl;
  757. sgl = data->sg + i;
  758. if (sgl->length % data->blksz)
  759. return -EINVAL;
  760. }
  761. if ((data->blksz % 4) != 0)
  762. /* REVISIT: The MMC buffer increments only when MSB is written.
  763. * Return error for blksz which is non multiple of four.
  764. */
  765. return -EINVAL;
  766. /*
  767. * If for some reason the DMA transfer is still active,
  768. * we wait for timeout period and free the dma
  769. */
  770. if (host->dma_ch != -1) {
  771. set_current_state(TASK_UNINTERRUPTIBLE);
  772. schedule_timeout(100);
  773. if (down_trylock(&host->sem)) {
  774. omap_free_dma(host->dma_ch);
  775. host->dma_ch = -1;
  776. up(&host->sem);
  777. return err;
  778. }
  779. } else {
  780. if (down_trylock(&host->sem))
  781. return err;
  782. }
  783. ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
  784. mmc_omap_dma_cb,host, &dma_ch);
  785. if (ret != 0) {
  786. dev_err(mmc_dev(host->mmc),
  787. "%s: omap_request_dma() failed with %d\n",
  788. mmc_hostname(host->mmc), ret);
  789. return ret;
  790. }
  791. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  792. data->sg_len, mmc_omap_get_dma_dir(host, data));
  793. host->dma_ch = dma_ch;
  794. host->dma_sg_idx = 0;
  795. mmc_omap_config_dma_params(host, data, data->sg);
  796. return 0;
  797. }
  798. static void set_data_timeout(struct mmc_omap_host *host,
  799. struct mmc_request *req)
  800. {
  801. unsigned int timeout, cycle_ns;
  802. uint32_t reg, clkd, dto = 0;
  803. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  804. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  805. if (clkd == 0)
  806. clkd = 1;
  807. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  808. timeout = req->data->timeout_ns / cycle_ns;
  809. timeout += req->data->timeout_clks;
  810. if (timeout) {
  811. while ((timeout & 0x80000000) == 0) {
  812. dto += 1;
  813. timeout <<= 1;
  814. }
  815. dto = 31 - dto;
  816. timeout <<= 1;
  817. if (timeout && dto)
  818. dto += 1;
  819. if (dto >= 13)
  820. dto -= 13;
  821. else
  822. dto = 0;
  823. if (dto > 14)
  824. dto = 14;
  825. }
  826. reg &= ~DTO_MASK;
  827. reg |= dto << DTO_SHIFT;
  828. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  829. }
  830. /*
  831. * Configure block length for MMC/SD cards and initiate the transfer.
  832. */
  833. static int
  834. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  835. {
  836. int ret;
  837. host->data = req->data;
  838. if (req->data == NULL) {
  839. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  840. return 0;
  841. }
  842. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  843. | (req->data->blocks << 16));
  844. set_data_timeout(host, req);
  845. if (host->use_dma) {
  846. ret = mmc_omap_start_dma_transfer(host, req);
  847. if (ret != 0) {
  848. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  849. return ret;
  850. }
  851. }
  852. return 0;
  853. }
  854. static int omap_mmc_enable(struct mmc_host *mmc)
  855. {
  856. struct mmc_omap_host *host = mmc_priv(mmc);
  857. int err;
  858. err = clk_enable(host->fclk);
  859. if (err)
  860. return err;
  861. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
  862. omap_mmc_restore_ctx(host);
  863. return 0;
  864. }
  865. static int omap_mmc_disable(struct mmc_host *mmc, int lazy)
  866. {
  867. struct mmc_omap_host *host = mmc_priv(mmc);
  868. omap_mmc_save_ctx(host);
  869. clk_disable(host->fclk);
  870. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
  871. return 0;
  872. }
  873. /*
  874. * Request function. for read/write operation
  875. */
  876. static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  877. {
  878. struct mmc_omap_host *host = mmc_priv(mmc);
  879. WARN_ON(host->mrq != NULL);
  880. host->mrq = req;
  881. mmc_omap_prepare_data(host, req);
  882. mmc_omap_start_command(host, req->cmd, req->data);
  883. }
  884. /* Routine to configure clock values. Exposed API to core */
  885. static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  886. {
  887. struct mmc_omap_host *host = mmc_priv(mmc);
  888. u16 dsor = 0;
  889. unsigned long regval;
  890. unsigned long timeout;
  891. u32 con;
  892. int do_send_init_stream = 0;
  893. mmc_host_enable(host->mmc);
  894. if (ios->power_mode != host->power_mode) {
  895. switch (ios->power_mode) {
  896. case MMC_POWER_OFF:
  897. mmc_slot(host).set_power(host->dev, host->slot_id,
  898. 0, 0);
  899. break;
  900. case MMC_POWER_UP:
  901. mmc_slot(host).set_power(host->dev, host->slot_id,
  902. 1, ios->vdd);
  903. break;
  904. case MMC_POWER_ON:
  905. do_send_init_stream = 1;
  906. break;
  907. }
  908. host->power_mode = ios->power_mode;
  909. }
  910. con = OMAP_HSMMC_READ(host->base, CON);
  911. switch (mmc->ios.bus_width) {
  912. case MMC_BUS_WIDTH_8:
  913. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  914. break;
  915. case MMC_BUS_WIDTH_4:
  916. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  917. OMAP_HSMMC_WRITE(host->base, HCTL,
  918. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  919. break;
  920. case MMC_BUS_WIDTH_1:
  921. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  922. OMAP_HSMMC_WRITE(host->base, HCTL,
  923. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  924. break;
  925. }
  926. if (host->id == OMAP_MMC1_DEVID) {
  927. /* Only MMC1 can interface at 3V without some flavor
  928. * of external transceiver; but they all handle 1.8V.
  929. */
  930. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  931. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  932. /*
  933. * The mmc_select_voltage fn of the core does
  934. * not seem to set the power_mode to
  935. * MMC_POWER_UP upon recalculating the voltage.
  936. * vdd 1.8v.
  937. */
  938. if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
  939. dev_dbg(mmc_dev(host->mmc),
  940. "Switch operation failed\n");
  941. }
  942. }
  943. if (ios->clock) {
  944. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  945. if (dsor < 1)
  946. dsor = 1;
  947. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  948. dsor++;
  949. if (dsor > 250)
  950. dsor = 250;
  951. }
  952. omap_mmc_stop_clock(host);
  953. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  954. regval = regval & ~(CLKD_MASK);
  955. regval = regval | (dsor << 6) | (DTO << 16);
  956. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  957. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  958. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  959. /* Wait till the ICS bit is set */
  960. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  961. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  962. && time_before(jiffies, timeout))
  963. msleep(1);
  964. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  965. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  966. if (do_send_init_stream)
  967. send_init_stream(host);
  968. con = OMAP_HSMMC_READ(host->base, CON);
  969. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  970. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  971. else
  972. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  973. mmc_host_lazy_disable(host->mmc);
  974. }
  975. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  976. {
  977. struct mmc_omap_host *host = mmc_priv(mmc);
  978. struct omap_mmc_platform_data *pdata = host->pdata;
  979. if (!pdata->slots[0].card_detect)
  980. return -ENOSYS;
  981. return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
  982. }
  983. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  984. {
  985. struct mmc_omap_host *host = mmc_priv(mmc);
  986. struct omap_mmc_platform_data *pdata = host->pdata;
  987. if (!pdata->slots[0].get_ro)
  988. return -ENOSYS;
  989. return pdata->slots[0].get_ro(host->dev, 0);
  990. }
  991. static void omap_hsmmc_init(struct mmc_omap_host *host)
  992. {
  993. u32 hctl, capa, value;
  994. /* Only MMC1 supports 3.0V */
  995. if (host->id == OMAP_MMC1_DEVID) {
  996. hctl = SDVS30;
  997. capa = VS30 | VS18;
  998. } else {
  999. hctl = SDVS18;
  1000. capa = VS18;
  1001. }
  1002. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1003. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1004. value = OMAP_HSMMC_READ(host->base, CAPA);
  1005. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1006. /* Set the controller to AUTO IDLE mode */
  1007. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  1008. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  1009. /* Set SD bus power bit */
  1010. set_sd_bus_power(host);
  1011. }
  1012. static struct mmc_host_ops mmc_omap_ops = {
  1013. .enable = omap_mmc_enable,
  1014. .disable = omap_mmc_disable,
  1015. .request = omap_mmc_request,
  1016. .set_ios = omap_mmc_set_ios,
  1017. .get_cd = omap_hsmmc_get_cd,
  1018. .get_ro = omap_hsmmc_get_ro,
  1019. /* NYET -- enable_sdio_irq */
  1020. };
  1021. #ifdef CONFIG_DEBUG_FS
  1022. static int mmc_regs_show(struct seq_file *s, void *data)
  1023. {
  1024. struct mmc_host *mmc = s->private;
  1025. struct mmc_omap_host *host = mmc_priv(mmc);
  1026. struct omap_mmc_platform_data *pdata = host->pdata;
  1027. int context_loss = 0;
  1028. if (pdata->get_context_loss_count)
  1029. context_loss = pdata->get_context_loss_count(host->dev);
  1030. seq_printf(s, "mmc%d:\n"
  1031. " enabled:\t%d\n"
  1032. " nesting_cnt:\t%d\n"
  1033. " ctx_loss:\t%d:%d\n"
  1034. "\nregs:\n",
  1035. mmc->index, mmc->enabled ? 1 : 0, mmc->nesting_cnt,
  1036. host->context_loss, context_loss);
  1037. if (clk_enable(host->fclk) != 0) {
  1038. seq_printf(s, "can't read the regs\n");
  1039. goto err;
  1040. }
  1041. seq_printf(s, "SYSCONFIG:\t0x%08x\n",
  1042. OMAP_HSMMC_READ(host->base, SYSCONFIG));
  1043. seq_printf(s, "CON:\t\t0x%08x\n",
  1044. OMAP_HSMMC_READ(host->base, CON));
  1045. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1046. OMAP_HSMMC_READ(host->base, HCTL));
  1047. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1048. OMAP_HSMMC_READ(host->base, SYSCTL));
  1049. seq_printf(s, "IE:\t\t0x%08x\n",
  1050. OMAP_HSMMC_READ(host->base, IE));
  1051. seq_printf(s, "ISE:\t\t0x%08x\n",
  1052. OMAP_HSMMC_READ(host->base, ISE));
  1053. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1054. OMAP_HSMMC_READ(host->base, CAPA));
  1055. clk_disable(host->fclk);
  1056. err:
  1057. return 0;
  1058. }
  1059. static int mmc_regs_open(struct inode *inode, struct file *file)
  1060. {
  1061. return single_open(file, mmc_regs_show, inode->i_private);
  1062. }
  1063. static const struct file_operations mmc_regs_fops = {
  1064. .open = mmc_regs_open,
  1065. .read = seq_read,
  1066. .llseek = seq_lseek,
  1067. .release = single_release,
  1068. };
  1069. static void omap_mmc_debugfs(struct mmc_host *mmc)
  1070. {
  1071. if (mmc->debugfs_root)
  1072. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1073. mmc, &mmc_regs_fops);
  1074. }
  1075. #else
  1076. static void omap_mmc_debugfs(struct mmc_host *mmc)
  1077. {
  1078. }
  1079. #endif
  1080. static int __init omap_mmc_probe(struct platform_device *pdev)
  1081. {
  1082. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1083. struct mmc_host *mmc;
  1084. struct mmc_omap_host *host = NULL;
  1085. struct resource *res;
  1086. int ret = 0, irq;
  1087. if (pdata == NULL) {
  1088. dev_err(&pdev->dev, "Platform Data is missing\n");
  1089. return -ENXIO;
  1090. }
  1091. if (pdata->nr_slots == 0) {
  1092. dev_err(&pdev->dev, "No Slots\n");
  1093. return -ENXIO;
  1094. }
  1095. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1096. irq = platform_get_irq(pdev, 0);
  1097. if (res == NULL || irq < 0)
  1098. return -ENXIO;
  1099. res = request_mem_region(res->start, res->end - res->start + 1,
  1100. pdev->name);
  1101. if (res == NULL)
  1102. return -EBUSY;
  1103. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  1104. if (!mmc) {
  1105. ret = -ENOMEM;
  1106. goto err;
  1107. }
  1108. host = mmc_priv(mmc);
  1109. host->mmc = mmc;
  1110. host->pdata = pdata;
  1111. host->dev = &pdev->dev;
  1112. host->use_dma = 1;
  1113. host->dev->dma_mask = &pdata->dma_mask;
  1114. host->dma_ch = -1;
  1115. host->irq = irq;
  1116. host->id = pdev->id;
  1117. host->slot_id = 0;
  1118. host->mapbase = res->start;
  1119. host->base = ioremap(host->mapbase, SZ_4K);
  1120. host->power_mode = -1;
  1121. platform_set_drvdata(pdev, host);
  1122. INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
  1123. mmc->ops = &mmc_omap_ops;
  1124. mmc->f_min = 400000;
  1125. mmc->f_max = 52000000;
  1126. sema_init(&host->sem, 1);
  1127. host->iclk = clk_get(&pdev->dev, "ick");
  1128. if (IS_ERR(host->iclk)) {
  1129. ret = PTR_ERR(host->iclk);
  1130. host->iclk = NULL;
  1131. goto err1;
  1132. }
  1133. host->fclk = clk_get(&pdev->dev, "fck");
  1134. if (IS_ERR(host->fclk)) {
  1135. ret = PTR_ERR(host->fclk);
  1136. host->fclk = NULL;
  1137. clk_put(host->iclk);
  1138. goto err1;
  1139. }
  1140. omap_mmc_save_ctx(host);
  1141. mmc->caps |= MMC_CAP_DISABLE;
  1142. mmc_set_disable_delay(mmc, 100);
  1143. if (mmc_host_enable(host->mmc) != 0) {
  1144. clk_put(host->iclk);
  1145. clk_put(host->fclk);
  1146. goto err1;
  1147. }
  1148. if (clk_enable(host->iclk) != 0) {
  1149. mmc_host_disable(host->mmc);
  1150. clk_put(host->iclk);
  1151. clk_put(host->fclk);
  1152. goto err1;
  1153. }
  1154. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  1155. /*
  1156. * MMC can still work without debounce clock.
  1157. */
  1158. if (IS_ERR(host->dbclk))
  1159. dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
  1160. else
  1161. if (clk_enable(host->dbclk) != 0)
  1162. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  1163. " clk failed\n");
  1164. else
  1165. host->dbclk_enabled = 1;
  1166. /* Since we do only SG emulation, we can have as many segs
  1167. * as we want. */
  1168. mmc->max_phys_segs = 1024;
  1169. mmc->max_hw_segs = 1024;
  1170. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1171. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1172. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1173. mmc->max_seg_size = mmc->max_req_size;
  1174. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  1175. if (pdata->slots[host->slot_id].wires >= 8)
  1176. mmc->caps |= MMC_CAP_8_BIT_DATA;
  1177. else if (pdata->slots[host->slot_id].wires >= 4)
  1178. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1179. omap_hsmmc_init(host);
  1180. /* Select DMA lines */
  1181. switch (host->id) {
  1182. case OMAP_MMC1_DEVID:
  1183. host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
  1184. host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
  1185. break;
  1186. case OMAP_MMC2_DEVID:
  1187. host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
  1188. host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
  1189. break;
  1190. case OMAP_MMC3_DEVID:
  1191. host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
  1192. host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
  1193. break;
  1194. default:
  1195. dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
  1196. goto err_irq;
  1197. }
  1198. /* Request IRQ for MMC operations */
  1199. ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
  1200. mmc_hostname(mmc), host);
  1201. if (ret) {
  1202. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1203. goto err_irq;
  1204. }
  1205. /* initialize power supplies, gpios, etc */
  1206. if (pdata->init != NULL) {
  1207. if (pdata->init(&pdev->dev) != 0) {
  1208. dev_dbg(mmc_dev(host->mmc), "late init error\n");
  1209. goto err_irq_cd_init;
  1210. }
  1211. }
  1212. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1213. /* Request IRQ for card detect */
  1214. if ((mmc_slot(host).card_detect_irq)) {
  1215. ret = request_irq(mmc_slot(host).card_detect_irq,
  1216. omap_mmc_cd_handler,
  1217. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  1218. | IRQF_DISABLED,
  1219. mmc_hostname(mmc), host);
  1220. if (ret) {
  1221. dev_dbg(mmc_dev(host->mmc),
  1222. "Unable to grab MMC CD IRQ\n");
  1223. goto err_irq_cd;
  1224. }
  1225. }
  1226. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  1227. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  1228. mmc_host_lazy_disable(host->mmc);
  1229. mmc_add_host(mmc);
  1230. if (host->pdata->slots[host->slot_id].name != NULL) {
  1231. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1232. if (ret < 0)
  1233. goto err_slot_name;
  1234. }
  1235. if (mmc_slot(host).card_detect_irq &&
  1236. host->pdata->slots[host->slot_id].get_cover_state) {
  1237. ret = device_create_file(&mmc->class_dev,
  1238. &dev_attr_cover_switch);
  1239. if (ret < 0)
  1240. goto err_cover_switch;
  1241. }
  1242. omap_mmc_debugfs(mmc);
  1243. return 0;
  1244. err_cover_switch:
  1245. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1246. err_slot_name:
  1247. mmc_remove_host(mmc);
  1248. err_irq_cd:
  1249. free_irq(mmc_slot(host).card_detect_irq, host);
  1250. err_irq_cd_init:
  1251. free_irq(host->irq, host);
  1252. err_irq:
  1253. mmc_host_disable(host->mmc);
  1254. clk_disable(host->iclk);
  1255. clk_put(host->fclk);
  1256. clk_put(host->iclk);
  1257. if (host->dbclk_enabled) {
  1258. clk_disable(host->dbclk);
  1259. clk_put(host->dbclk);
  1260. }
  1261. err1:
  1262. iounmap(host->base);
  1263. err:
  1264. dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
  1265. release_mem_region(res->start, res->end - res->start + 1);
  1266. if (host)
  1267. mmc_free_host(mmc);
  1268. return ret;
  1269. }
  1270. static int omap_mmc_remove(struct platform_device *pdev)
  1271. {
  1272. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1273. struct resource *res;
  1274. if (host) {
  1275. mmc_host_enable(host->mmc);
  1276. mmc_remove_host(host->mmc);
  1277. if (host->pdata->cleanup)
  1278. host->pdata->cleanup(&pdev->dev);
  1279. free_irq(host->irq, host);
  1280. if (mmc_slot(host).card_detect_irq)
  1281. free_irq(mmc_slot(host).card_detect_irq, host);
  1282. flush_scheduled_work();
  1283. mmc_host_disable(host->mmc);
  1284. clk_disable(host->iclk);
  1285. clk_put(host->fclk);
  1286. clk_put(host->iclk);
  1287. if (host->dbclk_enabled) {
  1288. clk_disable(host->dbclk);
  1289. clk_put(host->dbclk);
  1290. }
  1291. mmc_free_host(host->mmc);
  1292. iounmap(host->base);
  1293. }
  1294. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1295. if (res)
  1296. release_mem_region(res->start, res->end - res->start + 1);
  1297. platform_set_drvdata(pdev, NULL);
  1298. return 0;
  1299. }
  1300. #ifdef CONFIG_PM
  1301. static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
  1302. {
  1303. int ret = 0;
  1304. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1305. if (host && host->suspended)
  1306. return 0;
  1307. if (host) {
  1308. mmc_host_enable(host->mmc);
  1309. ret = mmc_suspend_host(host->mmc, state);
  1310. if (ret == 0) {
  1311. host->suspended = 1;
  1312. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1313. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1314. if (host->pdata->suspend) {
  1315. ret = host->pdata->suspend(&pdev->dev,
  1316. host->slot_id);
  1317. if (ret)
  1318. dev_dbg(mmc_dev(host->mmc),
  1319. "Unable to handle MMC board"
  1320. " level suspend\n");
  1321. }
  1322. OMAP_HSMMC_WRITE(host->base, HCTL,
  1323. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1324. mmc_host_disable(host->mmc);
  1325. clk_disable(host->iclk);
  1326. clk_disable(host->dbclk);
  1327. } else
  1328. mmc_host_disable(host->mmc);
  1329. }
  1330. return ret;
  1331. }
  1332. /* Routine to resume the MMC device */
  1333. static int omap_mmc_resume(struct platform_device *pdev)
  1334. {
  1335. int ret = 0;
  1336. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1337. if (host && !host->suspended)
  1338. return 0;
  1339. if (host) {
  1340. ret = clk_enable(host->iclk);
  1341. if (ret)
  1342. goto clk_en_err;
  1343. if (clk_enable(host->dbclk) != 0)
  1344. dev_dbg(mmc_dev(host->mmc),
  1345. "Enabling debounce clk failed\n");
  1346. if (mmc_host_enable(host->mmc) != 0) {
  1347. clk_disable(host->iclk);
  1348. goto clk_en_err;
  1349. }
  1350. omap_hsmmc_init(host);
  1351. if (host->pdata->resume) {
  1352. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  1353. if (ret)
  1354. dev_dbg(mmc_dev(host->mmc),
  1355. "Unmask interrupt failed\n");
  1356. }
  1357. /* Notify the core to resume the host */
  1358. ret = mmc_resume_host(host->mmc);
  1359. if (ret == 0)
  1360. host->suspended = 0;
  1361. mmc_host_lazy_disable(host->mmc);
  1362. }
  1363. return ret;
  1364. clk_en_err:
  1365. dev_dbg(mmc_dev(host->mmc),
  1366. "Failed to enable MMC clocks during resume\n");
  1367. return ret;
  1368. }
  1369. #else
  1370. #define omap_mmc_suspend NULL
  1371. #define omap_mmc_resume NULL
  1372. #endif
  1373. static struct platform_driver omap_mmc_driver = {
  1374. .remove = omap_mmc_remove,
  1375. .suspend = omap_mmc_suspend,
  1376. .resume = omap_mmc_resume,
  1377. .driver = {
  1378. .name = DRIVER_NAME,
  1379. .owner = THIS_MODULE,
  1380. },
  1381. };
  1382. static int __init omap_mmc_init(void)
  1383. {
  1384. /* Register the MMC driver */
  1385. return platform_driver_probe(&omap_mmc_driver, omap_mmc_probe);
  1386. }
  1387. static void __exit omap_mmc_cleanup(void)
  1388. {
  1389. /* Unregister MMC driver */
  1390. platform_driver_unregister(&omap_mmc_driver);
  1391. }
  1392. module_init(omap_mmc_init);
  1393. module_exit(omap_mmc_cleanup);
  1394. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1395. MODULE_LICENSE("GPL");
  1396. MODULE_ALIAS("platform:" DRIVER_NAME);
  1397. MODULE_AUTHOR("Texas Instruments Inc");