tmio_mmc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * linux/drivers/mmc/tmio_mmc.c
  3. *
  4. * Copyright (C) 2004 Ian Molton
  5. * Copyright (C) 2007 Ian Molton
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Driver for the MMC / SD / SDIO cell found in:
  12. *
  13. * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
  14. *
  15. * This driver draws mainly on scattered spec sheets, Reverse engineering
  16. * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
  17. * support). (Further 4 bit support from a later datasheet).
  18. *
  19. * TODO:
  20. * Investigate using a workqueue for PIO transfers
  21. * Eliminate FIXMEs
  22. * SDIO support
  23. * Better Power management
  24. * Handle MMC errors better
  25. * double buffer support
  26. *
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/device.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/highmem.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/irq.h>
  35. #include <linux/mfd/core.h>
  36. #include <linux/mfd/tmio.h>
  37. #include <linux/mmc/host.h>
  38. #include <linux/module.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/spinlock.h>
  43. #define CTL_SD_CMD 0x00
  44. #define CTL_ARG_REG 0x04
  45. #define CTL_STOP_INTERNAL_ACTION 0x08
  46. #define CTL_XFER_BLK_COUNT 0xa
  47. #define CTL_RESPONSE 0x0c
  48. #define CTL_STATUS 0x1c
  49. #define CTL_IRQ_MASK 0x20
  50. #define CTL_SD_CARD_CLK_CTL 0x24
  51. #define CTL_SD_XFER_LEN 0x26
  52. #define CTL_SD_MEM_CARD_OPT 0x28
  53. #define CTL_SD_ERROR_DETAIL_STATUS 0x2c
  54. #define CTL_SD_DATA_PORT 0x30
  55. #define CTL_TRANSACTION_CTL 0x34
  56. #define CTL_SDIO_STATUS 0x36
  57. #define CTL_SDIO_IRQ_MASK 0x38
  58. #define CTL_RESET_SD 0xe0
  59. #define CTL_SDIO_REGS 0x100
  60. #define CTL_CLK_AND_WAIT_CTL 0x138
  61. #define CTL_RESET_SDIO 0x1e0
  62. /* Definitions for values the CTRL_STATUS register can take. */
  63. #define TMIO_STAT_CMDRESPEND 0x00000001
  64. #define TMIO_STAT_DATAEND 0x00000004
  65. #define TMIO_STAT_CARD_REMOVE 0x00000008
  66. #define TMIO_STAT_CARD_INSERT 0x00000010
  67. #define TMIO_STAT_SIGSTATE 0x00000020
  68. #define TMIO_STAT_WRPROTECT 0x00000080
  69. #define TMIO_STAT_CARD_REMOVE_A 0x00000100
  70. #define TMIO_STAT_CARD_INSERT_A 0x00000200
  71. #define TMIO_STAT_SIGSTATE_A 0x00000400
  72. #define TMIO_STAT_CMD_IDX_ERR 0x00010000
  73. #define TMIO_STAT_CRCFAIL 0x00020000
  74. #define TMIO_STAT_STOPBIT_ERR 0x00040000
  75. #define TMIO_STAT_DATATIMEOUT 0x00080000
  76. #define TMIO_STAT_RXOVERFLOW 0x00100000
  77. #define TMIO_STAT_TXUNDERRUN 0x00200000
  78. #define TMIO_STAT_CMDTIMEOUT 0x00400000
  79. #define TMIO_STAT_RXRDY 0x01000000
  80. #define TMIO_STAT_TXRQ 0x02000000
  81. #define TMIO_STAT_ILL_FUNC 0x20000000
  82. #define TMIO_STAT_CMD_BUSY 0x40000000
  83. #define TMIO_STAT_ILL_ACCESS 0x80000000
  84. /* Definitions for values the CTRL_SDIO_STATUS register can take. */
  85. #define TMIO_SDIO_STAT_IOIRQ 0x0001
  86. #define TMIO_SDIO_STAT_EXPUB52 0x4000
  87. #define TMIO_SDIO_STAT_EXWT 0x8000
  88. #define TMIO_SDIO_MASK_ALL 0xc007
  89. /* Define some IRQ masks */
  90. /* This is the mask used at reset by the chip */
  91. #define TMIO_MASK_ALL 0x837f031d
  92. #define TMIO_MASK_READOP (TMIO_STAT_RXRDY | TMIO_STAT_DATAEND)
  93. #define TMIO_MASK_WRITEOP (TMIO_STAT_TXRQ | TMIO_STAT_DATAEND)
  94. #define TMIO_MASK_CMD (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT | \
  95. TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT)
  96. #define TMIO_MASK_IRQ (TMIO_MASK_READOP | TMIO_MASK_WRITEOP | TMIO_MASK_CMD)
  97. #define enable_mmc_irqs(host, i) \
  98. do { \
  99. u32 mask;\
  100. mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
  101. mask &= ~((i) & TMIO_MASK_IRQ); \
  102. sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
  103. } while (0)
  104. #define disable_mmc_irqs(host, i) \
  105. do { \
  106. u32 mask;\
  107. mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
  108. mask |= ((i) & TMIO_MASK_IRQ); \
  109. sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
  110. } while (0)
  111. #define ack_mmc_irqs(host, i) \
  112. do { \
  113. sd_ctrl_write32((host), CTL_STATUS, ~(i)); \
  114. } while (0)
  115. /* This is arbitrary, just noone needed any higher alignment yet */
  116. #define MAX_ALIGN 4
  117. struct tmio_mmc_host {
  118. void __iomem *ctl;
  119. unsigned long bus_shift;
  120. struct mmc_command *cmd;
  121. struct mmc_request *mrq;
  122. struct mmc_data *data;
  123. struct mmc_host *mmc;
  124. int irq;
  125. unsigned int sdio_irq_enabled;
  126. /* Callbacks for clock / power control */
  127. void (*set_pwr)(struct platform_device *host, int state);
  128. void (*set_clk_div)(struct platform_device *host, int state);
  129. /* pio related stuff */
  130. struct scatterlist *sg_ptr;
  131. struct scatterlist *sg_orig;
  132. unsigned int sg_len;
  133. unsigned int sg_off;
  134. struct platform_device *pdev;
  135. /* DMA support */
  136. struct dma_chan *chan_rx;
  137. struct dma_chan *chan_tx;
  138. struct tasklet_struct dma_complete;
  139. struct tasklet_struct dma_issue;
  140. #ifdef CONFIG_TMIO_MMC_DMA
  141. u8 bounce_buf[PAGE_CACHE_SIZE] __attribute__((aligned(MAX_ALIGN)));
  142. struct scatterlist bounce_sg;
  143. #endif
  144. /* Track lost interrupts */
  145. struct delayed_work delayed_reset_work;
  146. spinlock_t lock;
  147. unsigned long last_req_ts;
  148. };
  149. static void tmio_check_bounce_buffer(struct tmio_mmc_host *host);
  150. static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
  151. {
  152. return readw(host->ctl + (addr << host->bus_shift));
  153. }
  154. static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
  155. u16 *buf, int count)
  156. {
  157. readsw(host->ctl + (addr << host->bus_shift), buf, count);
  158. }
  159. static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
  160. {
  161. return readw(host->ctl + (addr << host->bus_shift)) |
  162. readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
  163. }
  164. static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
  165. {
  166. writew(val, host->ctl + (addr << host->bus_shift));
  167. }
  168. static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
  169. u16 *buf, int count)
  170. {
  171. writesw(host->ctl + (addr << host->bus_shift), buf, count);
  172. }
  173. static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
  174. {
  175. writew(val, host->ctl + (addr << host->bus_shift));
  176. writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
  177. }
  178. static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
  179. {
  180. host->sg_len = data->sg_len;
  181. host->sg_ptr = data->sg;
  182. host->sg_orig = data->sg;
  183. host->sg_off = 0;
  184. }
  185. static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
  186. {
  187. host->sg_ptr = sg_next(host->sg_ptr);
  188. host->sg_off = 0;
  189. return --host->sg_len;
  190. }
  191. static char *tmio_mmc_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  192. {
  193. local_irq_save(*flags);
  194. return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
  195. }
  196. static void tmio_mmc_kunmap_atomic(struct scatterlist *sg, unsigned long *flags, void *virt)
  197. {
  198. kunmap_atomic(virt - sg->offset, KM_BIO_SRC_IRQ);
  199. local_irq_restore(*flags);
  200. }
  201. #ifdef CONFIG_MMC_DEBUG
  202. #define STATUS_TO_TEXT(a, status, i) \
  203. do { \
  204. if (status & TMIO_STAT_##a) { \
  205. if (i++) \
  206. printk(" | "); \
  207. printk(#a); \
  208. } \
  209. } while (0)
  210. void pr_debug_status(u32 status)
  211. {
  212. int i = 0;
  213. printk(KERN_DEBUG "status: %08x = ", status);
  214. STATUS_TO_TEXT(CARD_REMOVE, status, i);
  215. STATUS_TO_TEXT(CARD_INSERT, status, i);
  216. STATUS_TO_TEXT(SIGSTATE, status, i);
  217. STATUS_TO_TEXT(WRPROTECT, status, i);
  218. STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
  219. STATUS_TO_TEXT(CARD_INSERT_A, status, i);
  220. STATUS_TO_TEXT(SIGSTATE_A, status, i);
  221. STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
  222. STATUS_TO_TEXT(STOPBIT_ERR, status, i);
  223. STATUS_TO_TEXT(ILL_FUNC, status, i);
  224. STATUS_TO_TEXT(CMD_BUSY, status, i);
  225. STATUS_TO_TEXT(CMDRESPEND, status, i);
  226. STATUS_TO_TEXT(DATAEND, status, i);
  227. STATUS_TO_TEXT(CRCFAIL, status, i);
  228. STATUS_TO_TEXT(DATATIMEOUT, status, i);
  229. STATUS_TO_TEXT(CMDTIMEOUT, status, i);
  230. STATUS_TO_TEXT(RXOVERFLOW, status, i);
  231. STATUS_TO_TEXT(TXUNDERRUN, status, i);
  232. STATUS_TO_TEXT(RXRDY, status, i);
  233. STATUS_TO_TEXT(TXRQ, status, i);
  234. STATUS_TO_TEXT(ILL_ACCESS, status, i);
  235. printk("\n");
  236. }
  237. #else
  238. #define pr_debug_status(s) do { } while (0)
  239. #endif
  240. static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  241. {
  242. struct tmio_mmc_host *host = mmc_priv(mmc);
  243. if (enable) {
  244. host->sdio_irq_enabled = 1;
  245. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
  246. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
  247. (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
  248. } else {
  249. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
  250. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
  251. host->sdio_irq_enabled = 0;
  252. }
  253. }
  254. static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
  255. {
  256. u32 clk = 0, clock;
  257. if (new_clock) {
  258. for (clock = host->mmc->f_min, clk = 0x80000080;
  259. new_clock >= (clock<<1); clk >>= 1)
  260. clock <<= 1;
  261. clk |= 0x100;
  262. }
  263. if (host->set_clk_div)
  264. host->set_clk_div(host->pdev, (clk>>22) & 1);
  265. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
  266. }
  267. static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
  268. {
  269. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  270. struct tmio_mmc_data *pdata = cell->driver_data;
  271. /*
  272. * Testing on sh-mobile showed that SDIO IRQs are unmasked when
  273. * CTL_CLK_AND_WAIT_CTL gets written, so we have to disable the
  274. * device IRQ here and restore the SDIO IRQ mask before
  275. * re-enabling the device IRQ.
  276. */
  277. if (pdata->flags & TMIO_MMC_SDIO_IRQ)
  278. disable_irq(host->irq);
  279. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
  280. msleep(10);
  281. if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
  282. tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
  283. enable_irq(host->irq);
  284. }
  285. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
  286. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  287. msleep(10);
  288. }
  289. static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
  290. {
  291. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  292. struct tmio_mmc_data *pdata = cell->driver_data;
  293. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
  294. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  295. msleep(10);
  296. /* see comment in tmio_mmc_clk_stop above */
  297. if (pdata->flags & TMIO_MMC_SDIO_IRQ)
  298. disable_irq(host->irq);
  299. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
  300. msleep(10);
  301. if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
  302. tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
  303. enable_irq(host->irq);
  304. }
  305. }
  306. static void reset(struct tmio_mmc_host *host)
  307. {
  308. /* FIXME - should we set stop clock reg here */
  309. sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
  310. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  311. msleep(10);
  312. sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
  313. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  314. msleep(10);
  315. }
  316. static void tmio_mmc_reset_work(struct work_struct *work)
  317. {
  318. struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
  319. delayed_reset_work.work);
  320. struct mmc_request *mrq;
  321. unsigned long flags;
  322. spin_lock_irqsave(&host->lock, flags);
  323. mrq = host->mrq;
  324. /* request already finished */
  325. if (!mrq
  326. || time_is_after_jiffies(host->last_req_ts +
  327. msecs_to_jiffies(2000))) {
  328. spin_unlock_irqrestore(&host->lock, flags);
  329. return;
  330. }
  331. dev_warn(&host->pdev->dev,
  332. "timeout waiting for hardware interrupt (CMD%u)\n",
  333. mrq->cmd->opcode);
  334. if (host->data)
  335. host->data->error = -ETIMEDOUT;
  336. else if (host->cmd)
  337. host->cmd->error = -ETIMEDOUT;
  338. else
  339. mrq->cmd->error = -ETIMEDOUT;
  340. host->cmd = NULL;
  341. host->data = NULL;
  342. host->mrq = NULL;
  343. spin_unlock_irqrestore(&host->lock, flags);
  344. reset(host);
  345. mmc_request_done(host->mmc, mrq);
  346. }
  347. static void
  348. tmio_mmc_finish_request(struct tmio_mmc_host *host)
  349. {
  350. struct mmc_request *mrq = host->mrq;
  351. if (!mrq)
  352. return;
  353. host->mrq = NULL;
  354. host->cmd = NULL;
  355. host->data = NULL;
  356. cancel_delayed_work(&host->delayed_reset_work);
  357. mmc_request_done(host->mmc, mrq);
  358. }
  359. /* These are the bitmasks the tmio chip requires to implement the MMC response
  360. * types. Note that R1 and R6 are the same in this scheme. */
  361. #define APP_CMD 0x0040
  362. #define RESP_NONE 0x0300
  363. #define RESP_R1 0x0400
  364. #define RESP_R1B 0x0500
  365. #define RESP_R2 0x0600
  366. #define RESP_R3 0x0700
  367. #define DATA_PRESENT 0x0800
  368. #define TRANSFER_READ 0x1000
  369. #define TRANSFER_MULTI 0x2000
  370. #define SECURITY_CMD 0x4000
  371. static int
  372. tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
  373. {
  374. struct mmc_data *data = host->data;
  375. int c = cmd->opcode;
  376. /* Command 12 is handled by hardware */
  377. if (cmd->opcode == 12 && !cmd->arg) {
  378. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
  379. return 0;
  380. }
  381. switch (mmc_resp_type(cmd)) {
  382. case MMC_RSP_NONE: c |= RESP_NONE; break;
  383. case MMC_RSP_R1: c |= RESP_R1; break;
  384. case MMC_RSP_R1B: c |= RESP_R1B; break;
  385. case MMC_RSP_R2: c |= RESP_R2; break;
  386. case MMC_RSP_R3: c |= RESP_R3; break;
  387. default:
  388. pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
  389. return -EINVAL;
  390. }
  391. host->cmd = cmd;
  392. /* FIXME - this seems to be ok commented out but the spec suggest this bit
  393. * should be set when issuing app commands.
  394. * if(cmd->flags & MMC_FLAG_ACMD)
  395. * c |= APP_CMD;
  396. */
  397. if (data) {
  398. c |= DATA_PRESENT;
  399. if (data->blocks > 1) {
  400. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
  401. c |= TRANSFER_MULTI;
  402. }
  403. if (data->flags & MMC_DATA_READ)
  404. c |= TRANSFER_READ;
  405. }
  406. enable_mmc_irqs(host, TMIO_MASK_CMD);
  407. /* Fire off the command */
  408. sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
  409. sd_ctrl_write16(host, CTL_SD_CMD, c);
  410. return 0;
  411. }
  412. /*
  413. * This chip always returns (at least?) as much data as you ask for.
  414. * I'm unsure what happens if you ask for less than a block. This should be
  415. * looked into to ensure that a funny length read doesnt hose the controller.
  416. */
  417. static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
  418. {
  419. struct mmc_data *data = host->data;
  420. void *sg_virt;
  421. unsigned short *buf;
  422. unsigned int count;
  423. unsigned long flags;
  424. if (!data) {
  425. pr_debug("Spurious PIO IRQ\n");
  426. return;
  427. }
  428. sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
  429. buf = (unsigned short *)(sg_virt + host->sg_off);
  430. count = host->sg_ptr->length - host->sg_off;
  431. if (count > data->blksz)
  432. count = data->blksz;
  433. pr_debug("count: %08x offset: %08x flags %08x\n",
  434. count, host->sg_off, data->flags);
  435. /* Transfer the data */
  436. if (data->flags & MMC_DATA_READ)
  437. sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  438. else
  439. sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  440. host->sg_off += count;
  441. tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
  442. if (host->sg_off == host->sg_ptr->length)
  443. tmio_mmc_next_sg(host);
  444. return;
  445. }
  446. /* needs to be called with host->lock held */
  447. static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
  448. {
  449. struct mmc_data *data = host->data;
  450. struct mmc_command *stop;
  451. host->data = NULL;
  452. if (!data) {
  453. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  454. return;
  455. }
  456. stop = data->stop;
  457. /* FIXME - return correct transfer count on errors */
  458. if (!data->error)
  459. data->bytes_xfered = data->blocks * data->blksz;
  460. else
  461. data->bytes_xfered = 0;
  462. pr_debug("Completed data request\n");
  463. /*
  464. * FIXME: other drivers allow an optional stop command of any given type
  465. * which we dont do, as the chip can auto generate them.
  466. * Perhaps we can be smarter about when to use auto CMD12 and
  467. * only issue the auto request when we know this is the desired
  468. * stop command, allowing fallback to the stop command the
  469. * upper layers expect. For now, we do what works.
  470. */
  471. if (data->flags & MMC_DATA_READ) {
  472. if (!host->chan_rx)
  473. disable_mmc_irqs(host, TMIO_MASK_READOP);
  474. else
  475. tmio_check_bounce_buffer(host);
  476. dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
  477. host->mrq);
  478. } else {
  479. if (!host->chan_tx)
  480. disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  481. dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
  482. host->mrq);
  483. }
  484. if (stop) {
  485. if (stop->opcode == 12 && !stop->arg)
  486. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
  487. else
  488. BUG();
  489. }
  490. tmio_mmc_finish_request(host);
  491. }
  492. static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
  493. {
  494. struct mmc_data *data;
  495. spin_lock(&host->lock);
  496. data = host->data;
  497. if (!data)
  498. goto out;
  499. if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
  500. /*
  501. * Has all data been written out yet? Testing on SuperH showed,
  502. * that in most cases the first interrupt comes already with the
  503. * BUSY status bit clear, but on some operations, like mount or
  504. * in the beginning of a write / sync / umount, there is one
  505. * DATAEND interrupt with the BUSY bit set, in this cases
  506. * waiting for one more interrupt fixes the problem.
  507. */
  508. if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
  509. disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  510. tasklet_schedule(&host->dma_complete);
  511. }
  512. } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
  513. disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  514. tasklet_schedule(&host->dma_complete);
  515. } else {
  516. tmio_mmc_do_data_irq(host);
  517. }
  518. out:
  519. spin_unlock(&host->lock);
  520. }
  521. static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
  522. unsigned int stat)
  523. {
  524. struct mmc_command *cmd = host->cmd;
  525. int i, addr;
  526. spin_lock(&host->lock);
  527. if (!host->cmd) {
  528. pr_debug("Spurious CMD irq\n");
  529. goto out;
  530. }
  531. host->cmd = NULL;
  532. /* This controller is sicker than the PXA one. Not only do we need to
  533. * drop the top 8 bits of the first response word, we also need to
  534. * modify the order of the response for short response command types.
  535. */
  536. for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
  537. cmd->resp[i] = sd_ctrl_read32(host, addr);
  538. if (cmd->flags & MMC_RSP_136) {
  539. cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
  540. cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
  541. cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
  542. cmd->resp[3] <<= 8;
  543. } else if (cmd->flags & MMC_RSP_R3) {
  544. cmd->resp[0] = cmd->resp[3];
  545. }
  546. if (stat & TMIO_STAT_CMDTIMEOUT)
  547. cmd->error = -ETIMEDOUT;
  548. else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
  549. cmd->error = -EILSEQ;
  550. /* If there is data to handle we enable data IRQs here, and
  551. * we will ultimatley finish the request in the data_end handler.
  552. * If theres no data or we encountered an error, finish now.
  553. */
  554. if (host->data && !cmd->error) {
  555. if (host->data->flags & MMC_DATA_READ) {
  556. if (!host->chan_rx)
  557. enable_mmc_irqs(host, TMIO_MASK_READOP);
  558. } else {
  559. if (!host->chan_tx)
  560. enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  561. else
  562. tasklet_schedule(&host->dma_issue);
  563. }
  564. } else {
  565. tmio_mmc_finish_request(host);
  566. }
  567. out:
  568. spin_unlock(&host->lock);
  569. return;
  570. }
  571. static irqreturn_t tmio_mmc_irq(int irq, void *devid)
  572. {
  573. struct tmio_mmc_host *host = devid;
  574. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  575. struct tmio_mmc_data *pdata = cell->driver_data;
  576. unsigned int ireg, irq_mask, status;
  577. unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
  578. pr_debug("MMC IRQ begin\n");
  579. status = sd_ctrl_read32(host, CTL_STATUS);
  580. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  581. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  582. sdio_ireg = 0;
  583. if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
  584. sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
  585. sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
  586. sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
  587. sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
  588. if (sdio_ireg && !host->sdio_irq_enabled) {
  589. pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
  590. sdio_status, sdio_irq_mask, sdio_ireg);
  591. tmio_mmc_enable_sdio_irq(host->mmc, 0);
  592. goto out;
  593. }
  594. if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
  595. sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
  596. mmc_signal_sdio_irq(host->mmc);
  597. if (sdio_ireg)
  598. goto out;
  599. }
  600. pr_debug_status(status);
  601. pr_debug_status(ireg);
  602. if (!ireg) {
  603. disable_mmc_irqs(host, status & ~irq_mask);
  604. pr_warning("tmio_mmc: Spurious irq, disabling! "
  605. "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
  606. pr_debug_status(status);
  607. goto out;
  608. }
  609. while (ireg) {
  610. /* Card insert / remove attempts */
  611. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  612. ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  613. TMIO_STAT_CARD_REMOVE);
  614. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  615. }
  616. /* CRC and other errors */
  617. /* if (ireg & TMIO_STAT_ERR_IRQ)
  618. * handled |= tmio_error_irq(host, irq, stat);
  619. */
  620. /* Command completion */
  621. if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
  622. ack_mmc_irqs(host,
  623. TMIO_STAT_CMDRESPEND |
  624. TMIO_STAT_CMDTIMEOUT);
  625. tmio_mmc_cmd_irq(host, status);
  626. }
  627. /* Data transfer */
  628. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  629. ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  630. tmio_mmc_pio_irq(host);
  631. }
  632. /* Data transfer completion */
  633. if (ireg & TMIO_STAT_DATAEND) {
  634. ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  635. tmio_mmc_data_irq(host);
  636. }
  637. /* Check status - keep going until we've handled it all */
  638. status = sd_ctrl_read32(host, CTL_STATUS);
  639. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  640. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  641. pr_debug("Status at end of loop: %08x\n", status);
  642. pr_debug_status(status);
  643. }
  644. pr_debug("MMC IRQ end\n");
  645. out:
  646. return IRQ_HANDLED;
  647. }
  648. #ifdef CONFIG_TMIO_MMC_DMA
  649. static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
  650. {
  651. if (host->sg_ptr == &host->bounce_sg) {
  652. unsigned long flags;
  653. void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
  654. memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
  655. tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
  656. }
  657. }
  658. static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
  659. {
  660. #if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
  661. /* Switch DMA mode on or off - SuperH specific? */
  662. sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
  663. #endif
  664. }
  665. static void tmio_dma_complete(void *arg)
  666. {
  667. struct tmio_mmc_host *host = arg;
  668. dev_dbg(&host->pdev->dev, "Command completed\n");
  669. if (!host->data)
  670. dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
  671. else
  672. enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  673. }
  674. static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
  675. {
  676. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  677. struct dma_async_tx_descriptor *desc = NULL;
  678. struct dma_chan *chan = host->chan_rx;
  679. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  680. struct tmio_mmc_data *pdata = cell->driver_data;
  681. dma_cookie_t cookie;
  682. int ret, i;
  683. bool aligned = true, multiple = true;
  684. unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
  685. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  686. if (sg_tmp->offset & align)
  687. aligned = false;
  688. if (sg_tmp->length & align) {
  689. multiple = false;
  690. break;
  691. }
  692. }
  693. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
  694. align >= MAX_ALIGN)) || !multiple) {
  695. ret = -EINVAL;
  696. goto pio;
  697. }
  698. /* The only sg element can be unaligned, use our bounce buffer then */
  699. if (!aligned) {
  700. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  701. host->sg_ptr = &host->bounce_sg;
  702. sg = host->sg_ptr;
  703. }
  704. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
  705. if (ret > 0)
  706. desc = chan->device->device_prep_slave_sg(chan, sg, ret,
  707. DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  708. if (desc) {
  709. desc->callback = tmio_dma_complete;
  710. desc->callback_param = host;
  711. cookie = dmaengine_submit(desc);
  712. dma_async_issue_pending(chan);
  713. }
  714. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  715. __func__, host->sg_len, ret, cookie, host->mrq);
  716. pio:
  717. if (!desc) {
  718. /* DMA failed, fall back to PIO */
  719. if (ret >= 0)
  720. ret = -EIO;
  721. host->chan_rx = NULL;
  722. dma_release_channel(chan);
  723. /* Free the Tx channel too */
  724. chan = host->chan_tx;
  725. if (chan) {
  726. host->chan_tx = NULL;
  727. dma_release_channel(chan);
  728. }
  729. dev_warn(&host->pdev->dev,
  730. "DMA failed: %d, falling back to PIO\n", ret);
  731. tmio_mmc_enable_dma(host, false);
  732. }
  733. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  734. desc, cookie, host->sg_len);
  735. }
  736. static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
  737. {
  738. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  739. struct dma_async_tx_descriptor *desc = NULL;
  740. struct dma_chan *chan = host->chan_tx;
  741. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  742. struct tmio_mmc_data *pdata = cell->driver_data;
  743. dma_cookie_t cookie;
  744. int ret, i;
  745. bool aligned = true, multiple = true;
  746. unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
  747. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  748. if (sg_tmp->offset & align)
  749. aligned = false;
  750. if (sg_tmp->length & align) {
  751. multiple = false;
  752. break;
  753. }
  754. }
  755. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
  756. align >= MAX_ALIGN)) || !multiple) {
  757. ret = -EINVAL;
  758. goto pio;
  759. }
  760. /* The only sg element can be unaligned, use our bounce buffer then */
  761. if (!aligned) {
  762. unsigned long flags;
  763. void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
  764. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  765. memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
  766. tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
  767. host->sg_ptr = &host->bounce_sg;
  768. sg = host->sg_ptr;
  769. }
  770. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
  771. if (ret > 0)
  772. desc = chan->device->device_prep_slave_sg(chan, sg, ret,
  773. DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  774. if (desc) {
  775. desc->callback = tmio_dma_complete;
  776. desc->callback_param = host;
  777. cookie = dmaengine_submit(desc);
  778. }
  779. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  780. __func__, host->sg_len, ret, cookie, host->mrq);
  781. pio:
  782. if (!desc) {
  783. /* DMA failed, fall back to PIO */
  784. if (ret >= 0)
  785. ret = -EIO;
  786. host->chan_tx = NULL;
  787. dma_release_channel(chan);
  788. /* Free the Rx channel too */
  789. chan = host->chan_rx;
  790. if (chan) {
  791. host->chan_rx = NULL;
  792. dma_release_channel(chan);
  793. }
  794. dev_warn(&host->pdev->dev,
  795. "DMA failed: %d, falling back to PIO\n", ret);
  796. tmio_mmc_enable_dma(host, false);
  797. }
  798. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
  799. desc, cookie);
  800. }
  801. static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
  802. struct mmc_data *data)
  803. {
  804. if (data->flags & MMC_DATA_READ) {
  805. if (host->chan_rx)
  806. tmio_mmc_start_dma_rx(host);
  807. } else {
  808. if (host->chan_tx)
  809. tmio_mmc_start_dma_tx(host);
  810. }
  811. }
  812. static void tmio_issue_tasklet_fn(unsigned long priv)
  813. {
  814. struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
  815. struct dma_chan *chan = host->chan_tx;
  816. dma_async_issue_pending(chan);
  817. }
  818. static void tmio_tasklet_fn(unsigned long arg)
  819. {
  820. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  821. unsigned long flags;
  822. spin_lock_irqsave(&host->lock, flags);
  823. if (!host->data)
  824. goto out;
  825. if (host->data->flags & MMC_DATA_READ)
  826. dma_unmap_sg(host->chan_rx->device->dev,
  827. host->sg_ptr, host->sg_len,
  828. DMA_FROM_DEVICE);
  829. else
  830. dma_unmap_sg(host->chan_tx->device->dev,
  831. host->sg_ptr, host->sg_len,
  832. DMA_TO_DEVICE);
  833. tmio_mmc_do_data_irq(host);
  834. out:
  835. spin_unlock_irqrestore(&host->lock, flags);
  836. }
  837. /* It might be necessary to make filter MFD specific */
  838. static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
  839. {
  840. dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
  841. chan->private = arg;
  842. return true;
  843. }
  844. static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
  845. struct tmio_mmc_data *pdata)
  846. {
  847. /* We can only either use DMA for both Tx and Rx or not use it at all */
  848. if (pdata->dma) {
  849. dma_cap_mask_t mask;
  850. dma_cap_zero(mask);
  851. dma_cap_set(DMA_SLAVE, mask);
  852. host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
  853. pdata->dma->chan_priv_tx);
  854. dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
  855. host->chan_tx);
  856. if (!host->chan_tx)
  857. return;
  858. host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
  859. pdata->dma->chan_priv_rx);
  860. dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
  861. host->chan_rx);
  862. if (!host->chan_rx) {
  863. dma_release_channel(host->chan_tx);
  864. host->chan_tx = NULL;
  865. return;
  866. }
  867. tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
  868. tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
  869. tmio_mmc_enable_dma(host, true);
  870. }
  871. }
  872. static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  873. {
  874. if (host->chan_tx) {
  875. struct dma_chan *chan = host->chan_tx;
  876. host->chan_tx = NULL;
  877. dma_release_channel(chan);
  878. }
  879. if (host->chan_rx) {
  880. struct dma_chan *chan = host->chan_rx;
  881. host->chan_rx = NULL;
  882. dma_release_channel(chan);
  883. }
  884. }
  885. #else
  886. static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
  887. {
  888. }
  889. static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
  890. struct mmc_data *data)
  891. {
  892. }
  893. static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
  894. struct tmio_mmc_data *pdata)
  895. {
  896. host->chan_tx = NULL;
  897. host->chan_rx = NULL;
  898. }
  899. static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  900. {
  901. }
  902. #endif
  903. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  904. struct mmc_data *data)
  905. {
  906. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  907. struct tmio_mmc_data *pdata = cell->driver_data;
  908. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  909. data->blksz, data->blocks);
  910. /* Some hardware cannot perform 2 byte requests in 4 bit mode */
  911. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  912. int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
  913. if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
  914. pr_err("%s: %d byte block unsupported in 4 bit mode\n",
  915. mmc_hostname(host->mmc), data->blksz);
  916. return -EINVAL;
  917. }
  918. }
  919. tmio_mmc_init_sg(host, data);
  920. host->data = data;
  921. /* Set transfer length / blocksize */
  922. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  923. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  924. tmio_mmc_start_dma(host, data);
  925. return 0;
  926. }
  927. /* Process requests from the MMC layer */
  928. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  929. {
  930. struct tmio_mmc_host *host = mmc_priv(mmc);
  931. int ret;
  932. if (host->mrq)
  933. pr_debug("request not null\n");
  934. host->last_req_ts = jiffies;
  935. wmb();
  936. host->mrq = mrq;
  937. if (mrq->data) {
  938. ret = tmio_mmc_start_data(host, mrq->data);
  939. if (ret)
  940. goto fail;
  941. }
  942. ret = tmio_mmc_start_command(host, mrq->cmd);
  943. if (!ret) {
  944. schedule_delayed_work(&host->delayed_reset_work,
  945. msecs_to_jiffies(2000));
  946. return;
  947. }
  948. fail:
  949. host->mrq = NULL;
  950. mrq->cmd->error = ret;
  951. mmc_request_done(mmc, mrq);
  952. }
  953. /* Set MMC clock / power.
  954. * Note: This controller uses a simple divider scheme therefore it cannot
  955. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  956. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  957. * slowest setting.
  958. */
  959. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  960. {
  961. struct tmio_mmc_host *host = mmc_priv(mmc);
  962. if (ios->clock)
  963. tmio_mmc_set_clock(host, ios->clock);
  964. /* Power sequence - OFF -> ON -> UP */
  965. switch (ios->power_mode) {
  966. case MMC_POWER_OFF: /* power down SD bus */
  967. if (host->set_pwr)
  968. host->set_pwr(host->pdev, 0);
  969. tmio_mmc_clk_stop(host);
  970. break;
  971. case MMC_POWER_ON: /* power up SD bus */
  972. if (host->set_pwr)
  973. host->set_pwr(host->pdev, 1);
  974. break;
  975. case MMC_POWER_UP: /* start bus clock */
  976. tmio_mmc_clk_start(host);
  977. break;
  978. }
  979. switch (ios->bus_width) {
  980. case MMC_BUS_WIDTH_1:
  981. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
  982. break;
  983. case MMC_BUS_WIDTH_4:
  984. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
  985. break;
  986. }
  987. /* Let things settle. delay taken from winCE driver */
  988. udelay(140);
  989. }
  990. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  991. {
  992. struct tmio_mmc_host *host = mmc_priv(mmc);
  993. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  994. struct tmio_mmc_data *pdata = cell->driver_data;
  995. return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  996. (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
  997. }
  998. static int tmio_mmc_get_cd(struct mmc_host *mmc)
  999. {
  1000. struct tmio_mmc_host *host = mmc_priv(mmc);
  1001. struct mfd_cell *cell = mfd_get_cell(host->pdev);
  1002. struct tmio_mmc_data *pdata = cell->driver_data;
  1003. if (!pdata->get_cd)
  1004. return -ENOSYS;
  1005. else
  1006. return pdata->get_cd(host->pdev);
  1007. }
  1008. static const struct mmc_host_ops tmio_mmc_ops = {
  1009. .request = tmio_mmc_request,
  1010. .set_ios = tmio_mmc_set_ios,
  1011. .get_ro = tmio_mmc_get_ro,
  1012. .get_cd = tmio_mmc_get_cd,
  1013. .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
  1014. };
  1015. #ifdef CONFIG_PM
  1016. static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
  1017. {
  1018. struct mfd_cell *cell = mfd_get_cell(dev);
  1019. struct mmc_host *mmc = platform_get_drvdata(dev);
  1020. int ret;
  1021. ret = mmc_suspend_host(mmc);
  1022. /* Tell MFD core it can disable us now.*/
  1023. if (!ret && cell->disable)
  1024. cell->disable(dev);
  1025. return ret;
  1026. }
  1027. static int tmio_mmc_resume(struct platform_device *dev)
  1028. {
  1029. struct mfd_cell *cell = mfd_get_cell(dev);
  1030. struct mmc_host *mmc = platform_get_drvdata(dev);
  1031. int ret = 0;
  1032. /* Tell the MFD core we are ready to be enabled */
  1033. if (cell->resume) {
  1034. ret = cell->resume(dev);
  1035. if (ret)
  1036. goto out;
  1037. }
  1038. mmc_resume_host(mmc);
  1039. out:
  1040. return ret;
  1041. }
  1042. #else
  1043. #define tmio_mmc_suspend NULL
  1044. #define tmio_mmc_resume NULL
  1045. #endif
  1046. static int __devinit tmio_mmc_probe(struct platform_device *dev)
  1047. {
  1048. struct mfd_cell *cell = mfd_get_cell(dev);
  1049. struct tmio_mmc_data *pdata;
  1050. struct resource *res_ctl;
  1051. struct tmio_mmc_host *host;
  1052. struct mmc_host *mmc;
  1053. int ret = -EINVAL;
  1054. u32 irq_mask = TMIO_MASK_CMD;
  1055. if (dev->num_resources != 2)
  1056. goto out;
  1057. res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1058. if (!res_ctl)
  1059. goto out;
  1060. pdata = cell->driver_data;
  1061. if (!pdata || !pdata->hclk)
  1062. goto out;
  1063. ret = -ENOMEM;
  1064. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
  1065. if (!mmc)
  1066. goto out;
  1067. host = mmc_priv(mmc);
  1068. host->mmc = mmc;
  1069. host->pdev = dev;
  1070. platform_set_drvdata(dev, mmc);
  1071. host->set_pwr = pdata->set_pwr;
  1072. host->set_clk_div = pdata->set_clk_div;
  1073. /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
  1074. host->bus_shift = resource_size(res_ctl) >> 10;
  1075. host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
  1076. if (!host->ctl)
  1077. goto host_free;
  1078. mmc->ops = &tmio_mmc_ops;
  1079. mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
  1080. mmc->f_max = pdata->hclk;
  1081. mmc->f_min = mmc->f_max / 512;
  1082. mmc->max_segs = 32;
  1083. mmc->max_blk_size = 512;
  1084. mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
  1085. mmc->max_segs;
  1086. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1087. mmc->max_seg_size = mmc->max_req_size;
  1088. if (pdata->ocr_mask)
  1089. mmc->ocr_avail = pdata->ocr_mask;
  1090. else
  1091. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1092. /* Tell the MFD core we are ready to be enabled */
  1093. if (cell->enable) {
  1094. ret = cell->enable(dev);
  1095. if (ret)
  1096. goto unmap_ctl;
  1097. }
  1098. tmio_mmc_clk_stop(host);
  1099. reset(host);
  1100. ret = platform_get_irq(dev, 0);
  1101. if (ret >= 0)
  1102. host->irq = ret;
  1103. else
  1104. goto cell_disable;
  1105. disable_mmc_irqs(host, TMIO_MASK_ALL);
  1106. if (pdata->flags & TMIO_MMC_SDIO_IRQ)
  1107. tmio_mmc_enable_sdio_irq(mmc, 0);
  1108. ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
  1109. IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
  1110. if (ret)
  1111. goto cell_disable;
  1112. spin_lock_init(&host->lock);
  1113. /* Init delayed work for request timeouts */
  1114. INIT_DELAYED_WORK(&host->delayed_reset_work, tmio_mmc_reset_work);
  1115. /* See if we also get DMA */
  1116. tmio_mmc_request_dma(host, pdata);
  1117. mmc_add_host(mmc);
  1118. pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
  1119. (unsigned long)host->ctl, host->irq);
  1120. /* Unmask the IRQs we want to know about */
  1121. if (!host->chan_rx)
  1122. irq_mask |= TMIO_MASK_READOP;
  1123. if (!host->chan_tx)
  1124. irq_mask |= TMIO_MASK_WRITEOP;
  1125. enable_mmc_irqs(host, irq_mask);
  1126. return 0;
  1127. cell_disable:
  1128. if (cell->disable)
  1129. cell->disable(dev);
  1130. unmap_ctl:
  1131. iounmap(host->ctl);
  1132. host_free:
  1133. mmc_free_host(mmc);
  1134. out:
  1135. return ret;
  1136. }
  1137. static int __devexit tmio_mmc_remove(struct platform_device *dev)
  1138. {
  1139. struct mfd_cell *cell = mfd_get_cell(dev);
  1140. struct mmc_host *mmc = platform_get_drvdata(dev);
  1141. platform_set_drvdata(dev, NULL);
  1142. if (mmc) {
  1143. struct tmio_mmc_host *host = mmc_priv(mmc);
  1144. mmc_remove_host(mmc);
  1145. cancel_delayed_work_sync(&host->delayed_reset_work);
  1146. tmio_mmc_release_dma(host);
  1147. free_irq(host->irq, host);
  1148. if (cell->disable)
  1149. cell->disable(dev);
  1150. iounmap(host->ctl);
  1151. mmc_free_host(mmc);
  1152. }
  1153. return 0;
  1154. }
  1155. /* ------------------- device registration ----------------------- */
  1156. static struct platform_driver tmio_mmc_driver = {
  1157. .driver = {
  1158. .name = "tmio-mmc",
  1159. .owner = THIS_MODULE,
  1160. },
  1161. .probe = tmio_mmc_probe,
  1162. .remove = __devexit_p(tmio_mmc_remove),
  1163. .suspend = tmio_mmc_suspend,
  1164. .resume = tmio_mmc_resume,
  1165. };
  1166. static int __init tmio_mmc_init(void)
  1167. {
  1168. return platform_driver_register(&tmio_mmc_driver);
  1169. }
  1170. static void __exit tmio_mmc_exit(void)
  1171. {
  1172. platform_driver_unregister(&tmio_mmc_driver);
  1173. }
  1174. module_init(tmio_mmc_init);
  1175. module_exit(tmio_mmc_exit);
  1176. MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
  1177. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
  1178. MODULE_LICENSE("GPL v2");
  1179. MODULE_ALIAS("platform:tmio-mmc");