tmio_mmc.c 32 KB

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