tmio_mmc.c 36 KB

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