tmio_mmc.c 36 KB

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