tmio_mmc_pio.c 28 KB

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