tmio_mmc.c 36 KB

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