tmio_mmc_pio.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * linux/drivers/mmc/host/tmio_mmc_pio.c
  3. *
  4. * Copyright (C) 2011 Guennadi Liakhovetski
  5. * Copyright (C) 2007 Ian Molton
  6. * Copyright (C) 2004 Ian Molton
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Driver for the MMC / SD / SDIO IP found in:
  13. *
  14. * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
  15. *
  16. * This driver draws mainly on scattered spec sheets, Reverse engineering
  17. * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
  18. * support). (Further 4 bit support from a later datasheet).
  19. *
  20. * TODO:
  21. * Investigate using a workqueue for PIO transfers
  22. * Eliminate FIXMEs
  23. * SDIO support
  24. * Better Power management
  25. * Handle MMC errors better
  26. * double buffer support
  27. *
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/device.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/tmio.h>
  36. #include <linux/mmc/host.h>
  37. #include <linux/mmc/tmio.h>
  38. #include <linux/module.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/pm_runtime.h>
  42. #include <linux/scatterlist.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/spinlock.h>
  45. #include "tmio_mmc.h"
  46. static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
  47. {
  48. return readw(host->ctl + (addr << host->bus_shift));
  49. }
  50. static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
  51. u16 *buf, int count)
  52. {
  53. readsw(host->ctl + (addr << host->bus_shift), buf, count);
  54. }
  55. static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
  56. {
  57. return readw(host->ctl + (addr << host->bus_shift)) |
  58. readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
  59. }
  60. static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
  61. {
  62. writew(val, host->ctl + (addr << host->bus_shift));
  63. }
  64. static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
  65. u16 *buf, int count)
  66. {
  67. writesw(host->ctl + (addr << host->bus_shift), buf, count);
  68. }
  69. static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
  70. {
  71. writew(val, host->ctl + (addr << host->bus_shift));
  72. writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
  73. }
  74. void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  75. {
  76. u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) & ~(i & TMIO_MASK_IRQ);
  77. sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
  78. }
  79. void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  80. {
  81. u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) | (i & TMIO_MASK_IRQ);
  82. sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
  83. }
  84. static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  85. {
  86. sd_ctrl_write32(host, CTL_STATUS, ~i);
  87. }
  88. static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
  89. {
  90. host->sg_len = data->sg_len;
  91. host->sg_ptr = data->sg;
  92. host->sg_orig = data->sg;
  93. host->sg_off = 0;
  94. }
  95. static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
  96. {
  97. host->sg_ptr = sg_next(host->sg_ptr);
  98. host->sg_off = 0;
  99. return --host->sg_len;
  100. }
  101. #ifdef CONFIG_MMC_DEBUG
  102. #define STATUS_TO_TEXT(a, status, i) \
  103. do { \
  104. if (status & TMIO_STAT_##a) { \
  105. if (i++) \
  106. printk(" | "); \
  107. printk(#a); \
  108. } \
  109. } while (0)
  110. static void pr_debug_status(u32 status)
  111. {
  112. int i = 0;
  113. printk(KERN_DEBUG "status: %08x = ", status);
  114. STATUS_TO_TEXT(CARD_REMOVE, status, i);
  115. STATUS_TO_TEXT(CARD_INSERT, status, i);
  116. STATUS_TO_TEXT(SIGSTATE, status, i);
  117. STATUS_TO_TEXT(WRPROTECT, status, i);
  118. STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
  119. STATUS_TO_TEXT(CARD_INSERT_A, status, i);
  120. STATUS_TO_TEXT(SIGSTATE_A, status, i);
  121. STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
  122. STATUS_TO_TEXT(STOPBIT_ERR, status, i);
  123. STATUS_TO_TEXT(ILL_FUNC, status, i);
  124. STATUS_TO_TEXT(CMD_BUSY, status, i);
  125. STATUS_TO_TEXT(CMDRESPEND, status, i);
  126. STATUS_TO_TEXT(DATAEND, status, i);
  127. STATUS_TO_TEXT(CRCFAIL, status, i);
  128. STATUS_TO_TEXT(DATATIMEOUT, status, i);
  129. STATUS_TO_TEXT(CMDTIMEOUT, status, i);
  130. STATUS_TO_TEXT(RXOVERFLOW, status, i);
  131. STATUS_TO_TEXT(TXUNDERRUN, status, i);
  132. STATUS_TO_TEXT(RXRDY, status, i);
  133. STATUS_TO_TEXT(TXRQ, status, i);
  134. STATUS_TO_TEXT(ILL_ACCESS, status, i);
  135. printk("\n");
  136. }
  137. #else
  138. #define pr_debug_status(s) do { } while (0)
  139. #endif
  140. static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  141. {
  142. struct tmio_mmc_host *host = mmc_priv(mmc);
  143. if (enable) {
  144. host->sdio_irq_enabled = 1;
  145. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
  146. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
  147. (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
  148. } else {
  149. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
  150. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
  151. host->sdio_irq_enabled = 0;
  152. }
  153. }
  154. static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
  155. {
  156. u32 clk = 0, clock;
  157. if (new_clock) {
  158. for (clock = host->mmc->f_min, clk = 0x80000080;
  159. new_clock >= (clock<<1); clk >>= 1)
  160. clock <<= 1;
  161. clk |= 0x100;
  162. }
  163. if (host->set_clk_div)
  164. host->set_clk_div(host->pdev, (clk>>22) & 1);
  165. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
  166. }
  167. static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
  168. {
  169. struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
  170. /* implicit BUG_ON(!res) */
  171. if (resource_size(res) > 0x100) {
  172. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
  173. msleep(10);
  174. }
  175. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
  176. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  177. msleep(10);
  178. }
  179. static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
  180. {
  181. struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
  182. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
  183. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  184. msleep(10);
  185. /* implicit BUG_ON(!res) */
  186. if (resource_size(res) > 0x100) {
  187. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
  188. msleep(10);
  189. }
  190. }
  191. static void tmio_mmc_reset(struct tmio_mmc_host *host)
  192. {
  193. struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
  194. /* FIXME - should we set stop clock reg here */
  195. sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
  196. /* implicit BUG_ON(!res) */
  197. if (resource_size(res) > 0x100)
  198. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  199. msleep(10);
  200. sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
  201. if (resource_size(res) > 0x100)
  202. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  203. msleep(10);
  204. }
  205. static void tmio_mmc_reset_work(struct work_struct *work)
  206. {
  207. struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
  208. delayed_reset_work.work);
  209. struct mmc_request *mrq;
  210. unsigned long flags;
  211. spin_lock_irqsave(&host->lock, flags);
  212. mrq = host->mrq;
  213. /*
  214. * is request already finished? Since we use a non-blocking
  215. * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
  216. * us, so, have to check for IS_ERR(host->mrq)
  217. */
  218. if (IS_ERR_OR_NULL(mrq)
  219. || time_is_after_jiffies(host->last_req_ts +
  220. msecs_to_jiffies(2000))) {
  221. spin_unlock_irqrestore(&host->lock, flags);
  222. return;
  223. }
  224. dev_warn(&host->pdev->dev,
  225. "timeout waiting for hardware interrupt (CMD%u)\n",
  226. mrq->cmd->opcode);
  227. if (host->data)
  228. host->data->error = -ETIMEDOUT;
  229. else if (host->cmd)
  230. host->cmd->error = -ETIMEDOUT;
  231. else
  232. mrq->cmd->error = -ETIMEDOUT;
  233. host->cmd = NULL;
  234. host->data = NULL;
  235. host->force_pio = false;
  236. spin_unlock_irqrestore(&host->lock, flags);
  237. tmio_mmc_reset(host);
  238. /* Ready for new calls */
  239. host->mrq = NULL;
  240. mmc_request_done(host->mmc, mrq);
  241. }
  242. /* called with host->lock held, interrupts disabled */
  243. static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
  244. {
  245. struct mmc_request *mrq = host->mrq;
  246. if (!mrq)
  247. return;
  248. host->cmd = NULL;
  249. host->data = NULL;
  250. host->force_pio = false;
  251. cancel_delayed_work(&host->delayed_reset_work);
  252. host->mrq = NULL;
  253. /* FIXME: mmc_request_done() can schedule! */
  254. mmc_request_done(host->mmc, mrq);
  255. }
  256. /* These are the bitmasks the tmio chip requires to implement the MMC response
  257. * types. Note that R1 and R6 are the same in this scheme. */
  258. #define APP_CMD 0x0040
  259. #define RESP_NONE 0x0300
  260. #define RESP_R1 0x0400
  261. #define RESP_R1B 0x0500
  262. #define RESP_R2 0x0600
  263. #define RESP_R3 0x0700
  264. #define DATA_PRESENT 0x0800
  265. #define TRANSFER_READ 0x1000
  266. #define TRANSFER_MULTI 0x2000
  267. #define SECURITY_CMD 0x4000
  268. static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
  269. {
  270. struct mmc_data *data = host->data;
  271. int c = cmd->opcode;
  272. /* Command 12 is handled by hardware */
  273. if (cmd->opcode == 12 && !cmd->arg) {
  274. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
  275. return 0;
  276. }
  277. switch (mmc_resp_type(cmd)) {
  278. case MMC_RSP_NONE: c |= RESP_NONE; break;
  279. case MMC_RSP_R1: c |= RESP_R1; break;
  280. case MMC_RSP_R1B: c |= RESP_R1B; break;
  281. case MMC_RSP_R2: c |= RESP_R2; break;
  282. case MMC_RSP_R3: c |= RESP_R3; break;
  283. default:
  284. pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
  285. return -EINVAL;
  286. }
  287. host->cmd = cmd;
  288. /* FIXME - this seems to be ok commented out but the spec suggest this bit
  289. * should be set when issuing app commands.
  290. * if(cmd->flags & MMC_FLAG_ACMD)
  291. * c |= APP_CMD;
  292. */
  293. if (data) {
  294. c |= DATA_PRESENT;
  295. if (data->blocks > 1) {
  296. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
  297. c |= TRANSFER_MULTI;
  298. }
  299. if (data->flags & MMC_DATA_READ)
  300. c |= TRANSFER_READ;
  301. }
  302. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
  303. /* Fire off the command */
  304. sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
  305. sd_ctrl_write16(host, CTL_SD_CMD, c);
  306. return 0;
  307. }
  308. /*
  309. * This chip always returns (at least?) as much data as you ask for.
  310. * I'm unsure what happens if you ask for less than a block. This should be
  311. * looked into to ensure that a funny length read doesn't hose the controller.
  312. */
  313. static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
  314. {
  315. struct mmc_data *data = host->data;
  316. void *sg_virt;
  317. unsigned short *buf;
  318. unsigned int count;
  319. unsigned long flags;
  320. if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
  321. pr_err("PIO IRQ in DMA mode!\n");
  322. return;
  323. } else if (!data) {
  324. pr_debug("Spurious PIO IRQ\n");
  325. return;
  326. }
  327. sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
  328. buf = (unsigned short *)(sg_virt + host->sg_off);
  329. count = host->sg_ptr->length - host->sg_off;
  330. if (count > data->blksz)
  331. count = data->blksz;
  332. pr_debug("count: %08x offset: %08x flags %08x\n",
  333. count, host->sg_off, data->flags);
  334. /* Transfer the data */
  335. if (data->flags & MMC_DATA_READ)
  336. sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  337. else
  338. sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  339. host->sg_off += count;
  340. tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
  341. if (host->sg_off == host->sg_ptr->length)
  342. tmio_mmc_next_sg(host);
  343. return;
  344. }
  345. static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
  346. {
  347. if (host->sg_ptr == &host->bounce_sg) {
  348. unsigned long flags;
  349. void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
  350. memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
  351. tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
  352. }
  353. }
  354. /* needs to be called with host->lock held */
  355. void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
  356. {
  357. struct mmc_data *data = host->data;
  358. struct mmc_command *stop;
  359. host->data = NULL;
  360. if (!data) {
  361. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  362. return;
  363. }
  364. stop = data->stop;
  365. /* FIXME - return correct transfer count on errors */
  366. if (!data->error)
  367. data->bytes_xfered = data->blocks * data->blksz;
  368. else
  369. data->bytes_xfered = 0;
  370. pr_debug("Completed data request\n");
  371. /*
  372. * FIXME: other drivers allow an optional stop command of any given type
  373. * which we dont do, as the chip can auto generate them.
  374. * Perhaps we can be smarter about when to use auto CMD12 and
  375. * only issue the auto request when we know this is the desired
  376. * stop command, allowing fallback to the stop command the
  377. * upper layers expect. For now, we do what works.
  378. */
  379. if (data->flags & MMC_DATA_READ) {
  380. if (host->chan_rx && !host->force_pio)
  381. tmio_mmc_check_bounce_buffer(host);
  382. dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
  383. host->mrq);
  384. } else {
  385. dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
  386. host->mrq);
  387. }
  388. if (stop) {
  389. if (stop->opcode == 12 && !stop->arg)
  390. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
  391. else
  392. BUG();
  393. }
  394. tmio_mmc_finish_request(host);
  395. }
  396. static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
  397. {
  398. struct mmc_data *data;
  399. spin_lock(&host->lock);
  400. data = host->data;
  401. if (!data)
  402. goto out;
  403. if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
  404. /*
  405. * Has all data been written out yet? Testing on SuperH showed,
  406. * that in most cases the first interrupt comes already with the
  407. * BUSY status bit clear, but on some operations, like mount or
  408. * in the beginning of a write / sync / umount, there is one
  409. * DATAEND interrupt with the BUSY bit set, in this cases
  410. * waiting for one more interrupt fixes the problem.
  411. */
  412. if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
  413. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  414. tasklet_schedule(&host->dma_complete);
  415. }
  416. } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
  417. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  418. tasklet_schedule(&host->dma_complete);
  419. } else {
  420. tmio_mmc_do_data_irq(host);
  421. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
  422. }
  423. out:
  424. spin_unlock(&host->lock);
  425. }
  426. static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
  427. unsigned int stat)
  428. {
  429. struct mmc_command *cmd = host->cmd;
  430. int i, addr;
  431. spin_lock(&host->lock);
  432. if (!host->cmd) {
  433. pr_debug("Spurious CMD irq\n");
  434. goto out;
  435. }
  436. host->cmd = NULL;
  437. /* This controller is sicker than the PXA one. Not only do we need to
  438. * drop the top 8 bits of the first response word, we also need to
  439. * modify the order of the response for short response command types.
  440. */
  441. for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
  442. cmd->resp[i] = sd_ctrl_read32(host, addr);
  443. if (cmd->flags & MMC_RSP_136) {
  444. cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
  445. cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
  446. cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
  447. cmd->resp[3] <<= 8;
  448. } else if (cmd->flags & MMC_RSP_R3) {
  449. cmd->resp[0] = cmd->resp[3];
  450. }
  451. if (stat & TMIO_STAT_CMDTIMEOUT)
  452. cmd->error = -ETIMEDOUT;
  453. else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
  454. cmd->error = -EILSEQ;
  455. /* If there is data to handle we enable data IRQs here, and
  456. * we will ultimatley finish the request in the data_end handler.
  457. * If theres no data or we encountered an error, finish now.
  458. */
  459. if (host->data && !cmd->error) {
  460. if (host->data->flags & MMC_DATA_READ) {
  461. if (host->force_pio || !host->chan_rx)
  462. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
  463. else
  464. tasklet_schedule(&host->dma_issue);
  465. } else {
  466. if (host->force_pio || !host->chan_tx)
  467. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  468. else
  469. tasklet_schedule(&host->dma_issue);
  470. }
  471. } else {
  472. tmio_mmc_finish_request(host);
  473. }
  474. out:
  475. spin_unlock(&host->lock);
  476. }
  477. irqreturn_t tmio_mmc_irq(int irq, void *devid)
  478. {
  479. struct tmio_mmc_host *host = devid;
  480. struct tmio_mmc_data *pdata = host->pdata;
  481. unsigned int ireg, irq_mask, status;
  482. unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
  483. pr_debug("MMC IRQ begin\n");
  484. status = sd_ctrl_read32(host, CTL_STATUS);
  485. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  486. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  487. sdio_ireg = 0;
  488. if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
  489. sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
  490. sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
  491. sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
  492. sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
  493. if (sdio_ireg && !host->sdio_irq_enabled) {
  494. pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
  495. sdio_status, sdio_irq_mask, sdio_ireg);
  496. tmio_mmc_enable_sdio_irq(host->mmc, 0);
  497. goto out;
  498. }
  499. if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
  500. sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
  501. mmc_signal_sdio_irq(host->mmc);
  502. if (sdio_ireg)
  503. goto out;
  504. }
  505. pr_debug_status(status);
  506. pr_debug_status(ireg);
  507. if (!ireg) {
  508. tmio_mmc_disable_mmc_irqs(host, status & ~irq_mask);
  509. pr_warning("tmio_mmc: Spurious irq, disabling! "
  510. "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
  511. pr_debug_status(status);
  512. goto out;
  513. }
  514. while (ireg) {
  515. /* Card insert / remove attempts */
  516. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  517. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  518. TMIO_STAT_CARD_REMOVE);
  519. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  520. }
  521. /* CRC and other errors */
  522. /* if (ireg & TMIO_STAT_ERR_IRQ)
  523. * handled |= tmio_error_irq(host, irq, stat);
  524. */
  525. /* Command completion */
  526. if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
  527. tmio_mmc_ack_mmc_irqs(host,
  528. TMIO_STAT_CMDRESPEND |
  529. TMIO_STAT_CMDTIMEOUT);
  530. tmio_mmc_cmd_irq(host, status);
  531. }
  532. /* Data transfer */
  533. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  534. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  535. tmio_mmc_pio_irq(host);
  536. }
  537. /* Data transfer completion */
  538. if (ireg & TMIO_STAT_DATAEND) {
  539. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  540. tmio_mmc_data_irq(host);
  541. }
  542. /* Check status - keep going until we've handled it all */
  543. status = sd_ctrl_read32(host, CTL_STATUS);
  544. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  545. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  546. pr_debug("Status at end of loop: %08x\n", status);
  547. pr_debug_status(status);
  548. }
  549. pr_debug("MMC IRQ end\n");
  550. out:
  551. return IRQ_HANDLED;
  552. }
  553. EXPORT_SYMBOL(tmio_mmc_irq);
  554. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  555. struct mmc_data *data)
  556. {
  557. struct tmio_mmc_data *pdata = host->pdata;
  558. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  559. data->blksz, data->blocks);
  560. /* Some hardware cannot perform 2 byte requests in 4 bit mode */
  561. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  562. int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
  563. if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
  564. pr_err("%s: %d byte block unsupported in 4 bit mode\n",
  565. mmc_hostname(host->mmc), data->blksz);
  566. return -EINVAL;
  567. }
  568. }
  569. tmio_mmc_init_sg(host, data);
  570. host->data = data;
  571. /* Set transfer length / blocksize */
  572. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  573. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  574. tmio_mmc_start_dma(host, data);
  575. return 0;
  576. }
  577. /* Process requests from the MMC layer */
  578. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  579. {
  580. struct tmio_mmc_host *host = mmc_priv(mmc);
  581. unsigned long flags;
  582. int ret;
  583. spin_lock_irqsave(&host->lock, flags);
  584. if (host->mrq) {
  585. pr_debug("request not null\n");
  586. if (IS_ERR(host->mrq)) {
  587. spin_unlock_irqrestore(&host->lock, flags);
  588. mrq->cmd->error = -EAGAIN;
  589. mmc_request_done(mmc, mrq);
  590. return;
  591. }
  592. }
  593. host->last_req_ts = jiffies;
  594. wmb();
  595. host->mrq = mrq;
  596. spin_unlock_irqrestore(&host->lock, flags);
  597. if (mrq->data) {
  598. ret = tmio_mmc_start_data(host, mrq->data);
  599. if (ret)
  600. goto fail;
  601. }
  602. ret = tmio_mmc_start_command(host, mrq->cmd);
  603. if (!ret) {
  604. schedule_delayed_work(&host->delayed_reset_work,
  605. msecs_to_jiffies(2000));
  606. return;
  607. }
  608. fail:
  609. host->force_pio = false;
  610. host->mrq = NULL;
  611. mrq->cmd->error = ret;
  612. mmc_request_done(mmc, mrq);
  613. }
  614. /* Set MMC clock / power.
  615. * Note: This controller uses a simple divider scheme therefore it cannot
  616. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  617. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  618. * slowest setting.
  619. */
  620. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  621. {
  622. struct tmio_mmc_host *host = mmc_priv(mmc);
  623. struct tmio_mmc_data *pdata = host->pdata;
  624. unsigned long flags;
  625. spin_lock_irqsave(&host->lock, flags);
  626. if (host->mrq) {
  627. if (IS_ERR(host->mrq)) {
  628. dev_dbg(&host->pdev->dev,
  629. "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
  630. current->comm, task_pid_nr(current),
  631. ios->clock, ios->power_mode);
  632. host->mrq = ERR_PTR(-EINTR);
  633. } else {
  634. dev_dbg(&host->pdev->dev,
  635. "%s.%d: CMD%u active since %lu, now %lu!\n",
  636. current->comm, task_pid_nr(current),
  637. host->mrq->cmd->opcode, host->last_req_ts, jiffies);
  638. }
  639. spin_unlock_irqrestore(&host->lock, flags);
  640. return;
  641. }
  642. host->mrq = ERR_PTR(-EBUSY);
  643. spin_unlock_irqrestore(&host->lock, flags);
  644. if (ios->clock)
  645. tmio_mmc_set_clock(host, ios->clock);
  646. /* Power sequence - OFF -> UP -> ON */
  647. if (ios->power_mode == MMC_POWER_UP) {
  648. if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) && !pdata->power) {
  649. pm_runtime_get_sync(&host->pdev->dev);
  650. pdata->power = true;
  651. }
  652. /* power up SD bus */
  653. if (host->set_pwr)
  654. host->set_pwr(host->pdev, 1);
  655. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  656. /* power down SD bus */
  657. if (ios->power_mode == MMC_POWER_OFF) {
  658. if (host->set_pwr)
  659. host->set_pwr(host->pdev, 0);
  660. if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) &&
  661. pdata->power) {
  662. pdata->power = false;
  663. pm_runtime_put(&host->pdev->dev);
  664. }
  665. }
  666. tmio_mmc_clk_stop(host);
  667. } else {
  668. /* start bus clock */
  669. tmio_mmc_clk_start(host);
  670. }
  671. switch (ios->bus_width) {
  672. case MMC_BUS_WIDTH_1:
  673. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
  674. break;
  675. case MMC_BUS_WIDTH_4:
  676. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
  677. break;
  678. }
  679. /* Let things settle. delay taken from winCE driver */
  680. udelay(140);
  681. if (PTR_ERR(host->mrq) == -EINTR)
  682. dev_dbg(&host->pdev->dev,
  683. "%s.%d: IOS interrupted: clk %u, mode %u",
  684. current->comm, task_pid_nr(current),
  685. ios->clock, ios->power_mode);
  686. host->mrq = NULL;
  687. }
  688. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  689. {
  690. struct tmio_mmc_host *host = mmc_priv(mmc);
  691. struct tmio_mmc_data *pdata = host->pdata;
  692. return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  693. !(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
  694. }
  695. static int tmio_mmc_get_cd(struct mmc_host *mmc)
  696. {
  697. struct tmio_mmc_host *host = mmc_priv(mmc);
  698. struct tmio_mmc_data *pdata = host->pdata;
  699. if (!pdata->get_cd)
  700. return -ENOSYS;
  701. else
  702. return pdata->get_cd(host->pdev);
  703. }
  704. static const struct mmc_host_ops tmio_mmc_ops = {
  705. .request = tmio_mmc_request,
  706. .set_ios = tmio_mmc_set_ios,
  707. .get_ro = tmio_mmc_get_ro,
  708. .get_cd = tmio_mmc_get_cd,
  709. .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
  710. };
  711. int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
  712. struct platform_device *pdev,
  713. struct tmio_mmc_data *pdata)
  714. {
  715. struct tmio_mmc_host *_host;
  716. struct mmc_host *mmc;
  717. struct resource *res_ctl;
  718. int ret;
  719. u32 irq_mask = TMIO_MASK_CMD;
  720. res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  721. if (!res_ctl)
  722. return -EINVAL;
  723. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
  724. if (!mmc)
  725. return -ENOMEM;
  726. pdata->dev = &pdev->dev;
  727. _host = mmc_priv(mmc);
  728. _host->pdata = pdata;
  729. _host->mmc = mmc;
  730. _host->pdev = pdev;
  731. platform_set_drvdata(pdev, mmc);
  732. _host->set_pwr = pdata->set_pwr;
  733. _host->set_clk_div = pdata->set_clk_div;
  734. /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
  735. _host->bus_shift = resource_size(res_ctl) >> 10;
  736. _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
  737. if (!_host->ctl) {
  738. ret = -ENOMEM;
  739. goto host_free;
  740. }
  741. mmc->ops = &tmio_mmc_ops;
  742. mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
  743. mmc->f_max = pdata->hclk;
  744. mmc->f_min = mmc->f_max / 512;
  745. mmc->max_segs = 32;
  746. mmc->max_blk_size = 512;
  747. mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
  748. mmc->max_segs;
  749. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  750. mmc->max_seg_size = mmc->max_req_size;
  751. if (pdata->ocr_mask)
  752. mmc->ocr_avail = pdata->ocr_mask;
  753. else
  754. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  755. pdata->power = false;
  756. pm_runtime_enable(&pdev->dev);
  757. ret = pm_runtime_resume(&pdev->dev);
  758. if (ret < 0)
  759. goto pm_disable;
  760. tmio_mmc_clk_stop(_host);
  761. tmio_mmc_reset(_host);
  762. tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
  763. if (pdata->flags & TMIO_MMC_SDIO_IRQ)
  764. tmio_mmc_enable_sdio_irq(mmc, 0);
  765. spin_lock_init(&_host->lock);
  766. /* Init delayed work for request timeouts */
  767. INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
  768. /* See if we also get DMA */
  769. tmio_mmc_request_dma(_host, pdata);
  770. /* We have to keep the device powered for its card detection to work */
  771. if (!(pdata->flags & TMIO_MMC_HAS_COLD_CD))
  772. pm_runtime_get_noresume(&pdev->dev);
  773. mmc_add_host(mmc);
  774. /* Unmask the IRQs we want to know about */
  775. if (!_host->chan_rx)
  776. irq_mask |= TMIO_MASK_READOP;
  777. if (!_host->chan_tx)
  778. irq_mask |= TMIO_MASK_WRITEOP;
  779. tmio_mmc_enable_mmc_irqs(_host, irq_mask);
  780. *host = _host;
  781. return 0;
  782. pm_disable:
  783. pm_runtime_disable(&pdev->dev);
  784. iounmap(_host->ctl);
  785. host_free:
  786. mmc_free_host(mmc);
  787. return ret;
  788. }
  789. EXPORT_SYMBOL(tmio_mmc_host_probe);
  790. void tmio_mmc_host_remove(struct tmio_mmc_host *host)
  791. {
  792. struct platform_device *pdev = host->pdev;
  793. /*
  794. * We don't have to manipulate pdata->power here: if there is a card in
  795. * the slot, the runtime PM is active and our .runtime_resume() will not
  796. * be run. If there is no card in the slot and the platform can suspend
  797. * the controller, the runtime PM is suspended and pdata->power == false,
  798. * so, our .runtime_resume() will not try to detect a card in the slot.
  799. */
  800. if (host->pdata->flags & TMIO_MMC_HAS_COLD_CD)
  801. pm_runtime_get_sync(&pdev->dev);
  802. mmc_remove_host(host->mmc);
  803. cancel_delayed_work_sync(&host->delayed_reset_work);
  804. tmio_mmc_release_dma(host);
  805. pm_runtime_put_sync(&pdev->dev);
  806. pm_runtime_disable(&pdev->dev);
  807. iounmap(host->ctl);
  808. mmc_free_host(host->mmc);
  809. }
  810. EXPORT_SYMBOL(tmio_mmc_host_remove);
  811. #ifdef CONFIG_PM
  812. int tmio_mmc_host_suspend(struct device *dev)
  813. {
  814. struct mmc_host *mmc = dev_get_drvdata(dev);
  815. struct tmio_mmc_host *host = mmc_priv(mmc);
  816. int ret = mmc_suspend_host(mmc);
  817. if (!ret)
  818. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
  819. host->pm_error = pm_runtime_put_sync(dev);
  820. return ret;
  821. }
  822. EXPORT_SYMBOL(tmio_mmc_host_suspend);
  823. int tmio_mmc_host_resume(struct device *dev)
  824. {
  825. struct mmc_host *mmc = dev_get_drvdata(dev);
  826. struct tmio_mmc_host *host = mmc_priv(mmc);
  827. /* The MMC core will perform the complete set up */
  828. host->pdata->power = false;
  829. if (!host->pm_error)
  830. pm_runtime_get_sync(dev);
  831. tmio_mmc_reset(mmc_priv(mmc));
  832. tmio_mmc_request_dma(host, host->pdata);
  833. return mmc_resume_host(mmc);
  834. }
  835. EXPORT_SYMBOL(tmio_mmc_host_resume);
  836. #endif /* CONFIG_PM */
  837. int tmio_mmc_host_runtime_suspend(struct device *dev)
  838. {
  839. return 0;
  840. }
  841. EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
  842. int tmio_mmc_host_runtime_resume(struct device *dev)
  843. {
  844. struct mmc_host *mmc = dev_get_drvdata(dev);
  845. struct tmio_mmc_host *host = mmc_priv(mmc);
  846. struct tmio_mmc_data *pdata = host->pdata;
  847. tmio_mmc_reset(host);
  848. if (pdata->power) {
  849. /* Only entered after a card-insert interrupt */
  850. tmio_mmc_set_ios(mmc, &mmc->ios);
  851. mmc_detect_change(mmc, msecs_to_jiffies(100));
  852. }
  853. return 0;
  854. }
  855. EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
  856. MODULE_LICENSE("GPL v2");