tmio_mmc.c 29 KB

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