tmio_mmc_pio.c 27 KB

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