tmio_mmc.c 36 KB

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