tmio_mmc_pio.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * linux/drivers/mmc/host/tmio_mmc_pio.c
  3. *
  4. * Copyright (C) 2016 Sang Engineering, Wolfram Sang
  5. * Copyright (C) 2015-16 Renesas Electronics Corporation
  6. * Copyright (C) 2011 Guennadi Liakhovetski
  7. * Copyright (C) 2007 Ian Molton
  8. * Copyright (C) 2004 Ian Molton
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Driver for the MMC / SD / SDIO IP found in:
  15. *
  16. * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
  17. *
  18. * This driver draws mainly on scattered spec sheets, Reverse engineering
  19. * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
  20. * support). (Further 4 bit support from a later datasheet).
  21. *
  22. * TODO:
  23. * Investigate using a workqueue for PIO transfers
  24. * Eliminate FIXMEs
  25. * Better Power management
  26. * Handle MMC errors better
  27. * double buffer support
  28. *
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/device.h>
  32. #include <linux/highmem.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/irq.h>
  36. #include <linux/mfd/tmio.h>
  37. #include <linux/mmc/card.h>
  38. #include <linux/mmc/host.h>
  39. #include <linux/mmc/mmc.h>
  40. #include <linux/mmc/slot-gpio.h>
  41. #include <linux/module.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/pm_qos.h>
  45. #include <linux/pm_runtime.h>
  46. #include <linux/regulator/consumer.h>
  47. #include <linux/mmc/sdio.h>
  48. #include <linux/scatterlist.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/workqueue.h>
  51. #include "tmio_mmc.h"
  52. void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  53. {
  54. host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
  55. sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
  56. }
  57. void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  58. {
  59. host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
  60. sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
  61. }
  62. static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  63. {
  64. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, ~i);
  65. }
  66. static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
  67. {
  68. host->sg_len = data->sg_len;
  69. host->sg_ptr = data->sg;
  70. host->sg_orig = data->sg;
  71. host->sg_off = 0;
  72. }
  73. static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
  74. {
  75. host->sg_ptr = sg_next(host->sg_ptr);
  76. host->sg_off = 0;
  77. return --host->sg_len;
  78. }
  79. #define CMDREQ_TIMEOUT 5000
  80. #ifdef CONFIG_MMC_DEBUG
  81. #define STATUS_TO_TEXT(a, status, i) \
  82. do { \
  83. if (status & TMIO_STAT_##a) { \
  84. if (i++) \
  85. printk(" | "); \
  86. printk(#a); \
  87. } \
  88. } while (0)
  89. static void pr_debug_status(u32 status)
  90. {
  91. int i = 0;
  92. pr_debug("status: %08x = ", status);
  93. STATUS_TO_TEXT(CARD_REMOVE, status, i);
  94. STATUS_TO_TEXT(CARD_INSERT, status, i);
  95. STATUS_TO_TEXT(SIGSTATE, status, i);
  96. STATUS_TO_TEXT(WRPROTECT, status, i);
  97. STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
  98. STATUS_TO_TEXT(CARD_INSERT_A, status, i);
  99. STATUS_TO_TEXT(SIGSTATE_A, status, i);
  100. STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
  101. STATUS_TO_TEXT(STOPBIT_ERR, status, i);
  102. STATUS_TO_TEXT(ILL_FUNC, status, i);
  103. STATUS_TO_TEXT(CMD_BUSY, status, i);
  104. STATUS_TO_TEXT(CMDRESPEND, status, i);
  105. STATUS_TO_TEXT(DATAEND, status, i);
  106. STATUS_TO_TEXT(CRCFAIL, status, i);
  107. STATUS_TO_TEXT(DATATIMEOUT, status, i);
  108. STATUS_TO_TEXT(CMDTIMEOUT, status, i);
  109. STATUS_TO_TEXT(RXOVERFLOW, status, i);
  110. STATUS_TO_TEXT(TXUNDERRUN, status, i);
  111. STATUS_TO_TEXT(RXRDY, status, i);
  112. STATUS_TO_TEXT(TXRQ, status, i);
  113. STATUS_TO_TEXT(ILL_ACCESS, status, i);
  114. printk("\n");
  115. }
  116. #else
  117. #define pr_debug_status(s) do { } while (0)
  118. #endif
  119. static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  120. {
  121. struct tmio_mmc_host *host = mmc_priv(mmc);
  122. if (enable && !host->sdio_irq_enabled) {
  123. /* Keep device active while SDIO irq is enabled */
  124. pm_runtime_get_sync(mmc_dev(mmc));
  125. host->sdio_irq_enabled = true;
  126. host->sdio_irq_mask = TMIO_SDIO_MASK_ALL &
  127. ~TMIO_SDIO_STAT_IOIRQ;
  128. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
  129. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
  130. } else if (!enable && host->sdio_irq_enabled) {
  131. host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
  132. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
  133. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
  134. host->sdio_irq_enabled = false;
  135. pm_runtime_mark_last_busy(mmc_dev(mmc));
  136. pm_runtime_put_autosuspend(mmc_dev(mmc));
  137. }
  138. }
  139. static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
  140. {
  141. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  142. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  143. msleep(host->pdata->flags & TMIO_MMC_MIN_RCAR2 ? 1 : 10);
  144. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
  145. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
  146. msleep(10);
  147. }
  148. }
  149. static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
  150. {
  151. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
  152. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
  153. msleep(10);
  154. }
  155. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  156. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  157. msleep(host->pdata->flags & TMIO_MMC_MIN_RCAR2 ? 5 : 10);
  158. }
  159. static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
  160. unsigned int new_clock)
  161. {
  162. u32 clk = 0, clock;
  163. if (new_clock == 0) {
  164. tmio_mmc_clk_stop(host);
  165. return;
  166. }
  167. if (host->clk_update)
  168. clock = host->clk_update(host, new_clock) / 512;
  169. else
  170. clock = host->mmc->f_min;
  171. for (clk = 0x80000080; new_clock >= (clock << 1); clk >>= 1)
  172. clock <<= 1;
  173. /* 1/1 clock is option */
  174. if ((host->pdata->flags & TMIO_MMC_CLK_ACTUAL) && ((clk >> 22) & 0x1))
  175. clk |= 0xff;
  176. if (host->set_clk_div)
  177. host->set_clk_div(host->pdev, (clk >> 22) & 1);
  178. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  179. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  180. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
  181. if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
  182. msleep(10);
  183. tmio_mmc_clk_start(host);
  184. }
  185. static void tmio_mmc_reset(struct tmio_mmc_host *host)
  186. {
  187. /* FIXME - should we set stop clock reg here */
  188. sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
  189. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
  190. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  191. msleep(10);
  192. sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
  193. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
  194. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  195. msleep(10);
  196. }
  197. static void tmio_mmc_reset_work(struct work_struct *work)
  198. {
  199. struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
  200. delayed_reset_work.work);
  201. struct mmc_request *mrq;
  202. unsigned long flags;
  203. spin_lock_irqsave(&host->lock, flags);
  204. mrq = host->mrq;
  205. /*
  206. * is request already finished? Since we use a non-blocking
  207. * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
  208. * us, so, have to check for IS_ERR(host->mrq)
  209. */
  210. if (IS_ERR_OR_NULL(mrq)
  211. || time_is_after_jiffies(host->last_req_ts +
  212. msecs_to_jiffies(CMDREQ_TIMEOUT))) {
  213. spin_unlock_irqrestore(&host->lock, flags);
  214. return;
  215. }
  216. dev_warn(&host->pdev->dev,
  217. "timeout waiting for hardware interrupt (CMD%u)\n",
  218. mrq->cmd->opcode);
  219. if (host->data)
  220. host->data->error = -ETIMEDOUT;
  221. else if (host->cmd)
  222. host->cmd->error = -ETIMEDOUT;
  223. else
  224. mrq->cmd->error = -ETIMEDOUT;
  225. host->cmd = NULL;
  226. host->data = NULL;
  227. host->force_pio = false;
  228. spin_unlock_irqrestore(&host->lock, flags);
  229. tmio_mmc_reset(host);
  230. /* Ready for new calls */
  231. host->mrq = NULL;
  232. tmio_mmc_abort_dma(host);
  233. mmc_request_done(host->mmc, mrq);
  234. }
  235. /* called with host->lock held, interrupts disabled */
  236. static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
  237. {
  238. struct mmc_request *mrq;
  239. unsigned long flags;
  240. spin_lock_irqsave(&host->lock, flags);
  241. mrq = host->mrq;
  242. if (IS_ERR_OR_NULL(mrq)) {
  243. spin_unlock_irqrestore(&host->lock, flags);
  244. return;
  245. }
  246. host->cmd = NULL;
  247. host->data = NULL;
  248. host->force_pio = false;
  249. cancel_delayed_work(&host->delayed_reset_work);
  250. host->mrq = NULL;
  251. spin_unlock_irqrestore(&host->lock, flags);
  252. if (mrq->cmd->error || (mrq->data && mrq->data->error))
  253. tmio_mmc_abort_dma(host);
  254. if (host->check_scc_error)
  255. host->check_scc_error(host);
  256. mmc_request_done(host->mmc, mrq);
  257. }
  258. static void tmio_mmc_done_work(struct work_struct *work)
  259. {
  260. struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
  261. done);
  262. tmio_mmc_finish_request(host);
  263. }
  264. /* These are the bitmasks the tmio chip requires to implement the MMC response
  265. * types. Note that R1 and R6 are the same in this scheme. */
  266. #define APP_CMD 0x0040
  267. #define RESP_NONE 0x0300
  268. #define RESP_R1 0x0400
  269. #define RESP_R1B 0x0500
  270. #define RESP_R2 0x0600
  271. #define RESP_R3 0x0700
  272. #define DATA_PRESENT 0x0800
  273. #define TRANSFER_READ 0x1000
  274. #define TRANSFER_MULTI 0x2000
  275. #define SECURITY_CMD 0x4000
  276. #define NO_CMD12_ISSUE 0x4000 /* TMIO_MMC_HAVE_CMD12_CTRL */
  277. static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
  278. {
  279. struct mmc_data *data = host->data;
  280. int c = cmd->opcode;
  281. u32 irq_mask = TMIO_MASK_CMD;
  282. /* CMD12 is handled by hardware */
  283. if (cmd->opcode == MMC_STOP_TRANSMISSION && !cmd->arg) {
  284. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
  285. return 0;
  286. }
  287. switch (mmc_resp_type(cmd)) {
  288. case MMC_RSP_NONE: c |= RESP_NONE; break;
  289. case MMC_RSP_R1:
  290. case MMC_RSP_R1_NO_CRC:
  291. c |= RESP_R1; break;
  292. case MMC_RSP_R1B: c |= RESP_R1B; break;
  293. case MMC_RSP_R2: c |= RESP_R2; break;
  294. case MMC_RSP_R3: c |= RESP_R3; break;
  295. default:
  296. pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
  297. return -EINVAL;
  298. }
  299. host->cmd = cmd;
  300. /* FIXME - this seems to be ok commented out but the spec suggest this bit
  301. * should be set when issuing app commands.
  302. * if(cmd->flags & MMC_FLAG_ACMD)
  303. * c |= APP_CMD;
  304. */
  305. if (data) {
  306. c |= DATA_PRESENT;
  307. if (data->blocks > 1) {
  308. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
  309. c |= TRANSFER_MULTI;
  310. /*
  311. * Disable auto CMD12 at IO_RW_EXTENDED when
  312. * multiple block transfer
  313. */
  314. if ((host->pdata->flags & TMIO_MMC_HAVE_CMD12_CTRL) &&
  315. (cmd->opcode == SD_IO_RW_EXTENDED))
  316. c |= NO_CMD12_ISSUE;
  317. }
  318. if (data->flags & MMC_DATA_READ)
  319. c |= TRANSFER_READ;
  320. }
  321. if (!host->native_hotplug)
  322. irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
  323. tmio_mmc_enable_mmc_irqs(host, irq_mask);
  324. /* Fire off the command */
  325. sd_ctrl_write32_as_16_and_16(host, CTL_ARG_REG, cmd->arg);
  326. sd_ctrl_write16(host, CTL_SD_CMD, c);
  327. return 0;
  328. }
  329. static void tmio_mmc_transfer_data(struct tmio_mmc_host *host,
  330. unsigned short *buf,
  331. unsigned int count)
  332. {
  333. int is_read = host->data->flags & MMC_DATA_READ;
  334. u8 *buf8;
  335. /*
  336. * Transfer the data
  337. */
  338. if (host->pdata->flags & TMIO_MMC_32BIT_DATA_PORT) {
  339. u8 data[4] = { };
  340. if (is_read)
  341. sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, (u32 *)buf,
  342. count >> 2);
  343. else
  344. sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, (u32 *)buf,
  345. count >> 2);
  346. /* if count was multiple of 4 */
  347. if (!(count & 0x3))
  348. return;
  349. buf8 = (u8 *)(buf + (count >> 2));
  350. count %= 4;
  351. if (is_read) {
  352. sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT,
  353. (u32 *)data, 1);
  354. memcpy(buf8, data, count);
  355. } else {
  356. memcpy(data, buf8, count);
  357. sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT,
  358. (u32 *)data, 1);
  359. }
  360. return;
  361. }
  362. if (is_read)
  363. sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  364. else
  365. sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  366. /* if count was even number */
  367. if (!(count & 0x1))
  368. return;
  369. /* if count was odd number */
  370. buf8 = (u8 *)(buf + (count >> 1));
  371. /*
  372. * FIXME
  373. *
  374. * driver and this function are assuming that
  375. * it is used as little endian
  376. */
  377. if (is_read)
  378. *buf8 = sd_ctrl_read16(host, CTL_SD_DATA_PORT) & 0xff;
  379. else
  380. sd_ctrl_write16(host, CTL_SD_DATA_PORT, *buf8);
  381. }
  382. /*
  383. * This chip always returns (at least?) as much data as you ask for.
  384. * I'm unsure what happens if you ask for less than a block. This should be
  385. * looked into to ensure that a funny length read doesn't hose the controller.
  386. */
  387. static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
  388. {
  389. struct mmc_data *data = host->data;
  390. void *sg_virt;
  391. unsigned short *buf;
  392. unsigned int count;
  393. unsigned long flags;
  394. if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
  395. pr_err("PIO IRQ in DMA mode!\n");
  396. return;
  397. } else if (!data) {
  398. pr_debug("Spurious PIO IRQ\n");
  399. return;
  400. }
  401. sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
  402. buf = (unsigned short *)(sg_virt + host->sg_off);
  403. count = host->sg_ptr->length - host->sg_off;
  404. if (count > data->blksz)
  405. count = data->blksz;
  406. pr_debug("count: %08x offset: %08x flags %08x\n",
  407. count, host->sg_off, data->flags);
  408. /* Transfer the data */
  409. tmio_mmc_transfer_data(host, buf, count);
  410. host->sg_off += count;
  411. tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
  412. if (host->sg_off == host->sg_ptr->length)
  413. tmio_mmc_next_sg(host);
  414. return;
  415. }
  416. static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
  417. {
  418. if (host->sg_ptr == &host->bounce_sg) {
  419. unsigned long flags;
  420. void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
  421. memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
  422. tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
  423. }
  424. }
  425. /* needs to be called with host->lock held */
  426. void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
  427. {
  428. struct mmc_data *data = host->data;
  429. struct mmc_command *stop;
  430. host->data = NULL;
  431. if (!data) {
  432. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  433. return;
  434. }
  435. stop = data->stop;
  436. /* FIXME - return correct transfer count on errors */
  437. if (!data->error)
  438. data->bytes_xfered = data->blocks * data->blksz;
  439. else
  440. data->bytes_xfered = 0;
  441. pr_debug("Completed data request\n");
  442. /*
  443. * FIXME: other drivers allow an optional stop command of any given type
  444. * which we dont do, as the chip can auto generate them.
  445. * Perhaps we can be smarter about when to use auto CMD12 and
  446. * only issue the auto request when we know this is the desired
  447. * stop command, allowing fallback to the stop command the
  448. * upper layers expect. For now, we do what works.
  449. */
  450. if (data->flags & MMC_DATA_READ) {
  451. if (host->chan_rx && !host->force_pio)
  452. tmio_mmc_check_bounce_buffer(host);
  453. dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
  454. host->mrq);
  455. } else {
  456. dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
  457. host->mrq);
  458. }
  459. if (stop) {
  460. if (stop->opcode == MMC_STOP_TRANSMISSION && !stop->arg)
  461. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
  462. else
  463. BUG();
  464. }
  465. schedule_work(&host->done);
  466. }
  467. static void tmio_mmc_data_irq(struct tmio_mmc_host *host, unsigned int stat)
  468. {
  469. struct mmc_data *data;
  470. spin_lock(&host->lock);
  471. data = host->data;
  472. if (!data)
  473. goto out;
  474. if (stat & TMIO_STAT_CRCFAIL || stat & TMIO_STAT_STOPBIT_ERR ||
  475. stat & TMIO_STAT_TXUNDERRUN)
  476. data->error = -EILSEQ;
  477. if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
  478. u32 status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
  479. bool done = false;
  480. /*
  481. * Has all data been written out yet? Testing on SuperH showed,
  482. * that in most cases the first interrupt comes already with the
  483. * BUSY status bit clear, but on some operations, like mount or
  484. * in the beginning of a write / sync / umount, there is one
  485. * DATAEND interrupt with the BUSY bit set, in this cases
  486. * waiting for one more interrupt fixes the problem.
  487. */
  488. if (host->pdata->flags & TMIO_MMC_HAS_IDLE_WAIT) {
  489. if (status & TMIO_STAT_SCLKDIVEN)
  490. done = true;
  491. } else {
  492. if (!(status & TMIO_STAT_CMD_BUSY))
  493. done = true;
  494. }
  495. if (done) {
  496. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  497. tasklet_schedule(&host->dma_complete);
  498. }
  499. } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
  500. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  501. tasklet_schedule(&host->dma_complete);
  502. } else {
  503. tmio_mmc_do_data_irq(host);
  504. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
  505. }
  506. out:
  507. spin_unlock(&host->lock);
  508. }
  509. static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
  510. unsigned int stat)
  511. {
  512. struct mmc_command *cmd = host->cmd;
  513. int i, addr;
  514. spin_lock(&host->lock);
  515. if (!host->cmd) {
  516. pr_debug("Spurious CMD irq\n");
  517. goto out;
  518. }
  519. /* This controller is sicker than the PXA one. Not only do we need to
  520. * drop the top 8 bits of the first response word, we also need to
  521. * modify the order of the response for short response command types.
  522. */
  523. for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
  524. cmd->resp[i] = sd_ctrl_read16_and_16_as_32(host, addr);
  525. if (cmd->flags & MMC_RSP_136) {
  526. cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
  527. cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
  528. cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
  529. cmd->resp[3] <<= 8;
  530. } else if (cmd->flags & MMC_RSP_R3) {
  531. cmd->resp[0] = cmd->resp[3];
  532. }
  533. if (stat & TMIO_STAT_CMDTIMEOUT)
  534. cmd->error = -ETIMEDOUT;
  535. else if ((stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) ||
  536. stat & TMIO_STAT_STOPBIT_ERR ||
  537. stat & TMIO_STAT_CMD_IDX_ERR)
  538. cmd->error = -EILSEQ;
  539. /* If there is data to handle we enable data IRQs here, and
  540. * we will ultimatley finish the request in the data_end handler.
  541. * If theres no data or we encountered an error, finish now.
  542. */
  543. if (host->data && (!cmd->error || cmd->error == -EILSEQ)) {
  544. if (host->data->flags & MMC_DATA_READ) {
  545. if (host->force_pio || !host->chan_rx)
  546. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
  547. else
  548. tasklet_schedule(&host->dma_issue);
  549. } else {
  550. if (host->force_pio || !host->chan_tx)
  551. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  552. else
  553. tasklet_schedule(&host->dma_issue);
  554. }
  555. } else {
  556. schedule_work(&host->done);
  557. }
  558. out:
  559. spin_unlock(&host->lock);
  560. }
  561. static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
  562. int ireg, int status)
  563. {
  564. struct mmc_host *mmc = host->mmc;
  565. /* Card insert / remove attempts */
  566. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  567. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  568. TMIO_STAT_CARD_REMOVE);
  569. if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
  570. ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
  571. !work_pending(&mmc->detect.work))
  572. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  573. return true;
  574. }
  575. return false;
  576. }
  577. static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host,
  578. int ireg, int status)
  579. {
  580. /* Command completion */
  581. if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
  582. tmio_mmc_ack_mmc_irqs(host,
  583. TMIO_STAT_CMDRESPEND |
  584. TMIO_STAT_CMDTIMEOUT);
  585. tmio_mmc_cmd_irq(host, status);
  586. return true;
  587. }
  588. /* Data transfer */
  589. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  590. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  591. tmio_mmc_pio_irq(host);
  592. return true;
  593. }
  594. /* Data transfer completion */
  595. if (ireg & TMIO_STAT_DATAEND) {
  596. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  597. tmio_mmc_data_irq(host, status);
  598. return true;
  599. }
  600. return false;
  601. }
  602. static void tmio_mmc_sdio_irq(int irq, void *devid)
  603. {
  604. struct tmio_mmc_host *host = devid;
  605. struct mmc_host *mmc = host->mmc;
  606. struct tmio_mmc_data *pdata = host->pdata;
  607. unsigned int ireg, status;
  608. unsigned int sdio_status;
  609. if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
  610. return;
  611. status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
  612. ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdio_irq_mask;
  613. sdio_status = status & ~TMIO_SDIO_MASK_ALL;
  614. if (pdata->flags & TMIO_MMC_SDIO_STATUS_QUIRK)
  615. sdio_status |= 6;
  616. sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
  617. if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
  618. mmc_signal_sdio_irq(mmc);
  619. }
  620. irqreturn_t tmio_mmc_irq(int irq, void *devid)
  621. {
  622. struct tmio_mmc_host *host = devid;
  623. unsigned int ireg, status;
  624. status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
  625. ireg = status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
  626. pr_debug_status(status);
  627. pr_debug_status(ireg);
  628. /* Clear the status except the interrupt status */
  629. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, TMIO_MASK_IRQ);
  630. if (__tmio_mmc_card_detect_irq(host, ireg, status))
  631. return IRQ_HANDLED;
  632. if (__tmio_mmc_sdcard_irq(host, ireg, status))
  633. return IRQ_HANDLED;
  634. tmio_mmc_sdio_irq(irq, devid);
  635. return IRQ_HANDLED;
  636. }
  637. EXPORT_SYMBOL(tmio_mmc_irq);
  638. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  639. struct mmc_data *data)
  640. {
  641. struct tmio_mmc_data *pdata = host->pdata;
  642. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  643. data->blksz, data->blocks);
  644. /* Some hardware cannot perform 2 byte requests in 4/8 bit mode */
  645. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4 ||
  646. host->mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
  647. int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
  648. if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
  649. pr_err("%s: %d byte block unsupported in 4/8 bit mode\n",
  650. mmc_hostname(host->mmc), data->blksz);
  651. return -EINVAL;
  652. }
  653. }
  654. tmio_mmc_init_sg(host, data);
  655. host->data = data;
  656. /* Set transfer length / blocksize */
  657. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  658. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  659. tmio_mmc_start_dma(host, data);
  660. return 0;
  661. }
  662. static void tmio_mmc_hw_reset(struct mmc_host *mmc)
  663. {
  664. struct tmio_mmc_host *host = mmc_priv(mmc);
  665. if (host->hw_reset)
  666. host->hw_reset(host);
  667. }
  668. static int tmio_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  669. {
  670. struct tmio_mmc_host *host = mmc_priv(mmc);
  671. int i, ret = 0;
  672. if (!host->tap_num) {
  673. if (!host->init_tuning || !host->select_tuning)
  674. /* Tuning is not supported */
  675. goto out;
  676. host->tap_num = host->init_tuning(host);
  677. if (!host->tap_num)
  678. /* Tuning is not supported */
  679. goto out;
  680. }
  681. if (host->tap_num * 2 >= sizeof(host->taps) * BITS_PER_BYTE) {
  682. dev_warn_once(&host->pdev->dev,
  683. "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
  684. goto out;
  685. }
  686. bitmap_zero(host->taps, host->tap_num * 2);
  687. /* Issue CMD19 twice for each tap */
  688. for (i = 0; i < 2 * host->tap_num; i++) {
  689. if (host->prepare_tuning)
  690. host->prepare_tuning(host, i % host->tap_num);
  691. ret = mmc_send_tuning(mmc, opcode, NULL);
  692. if (ret && ret != -EILSEQ)
  693. goto out;
  694. if (ret == 0)
  695. set_bit(i, host->taps);
  696. mdelay(1);
  697. }
  698. ret = host->select_tuning(host);
  699. out:
  700. if (ret < 0) {
  701. dev_warn(&host->pdev->dev, "Tuning procedure failed\n");
  702. tmio_mmc_hw_reset(mmc);
  703. }
  704. return ret;
  705. }
  706. /* Process requests from the MMC layer */
  707. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  708. {
  709. struct tmio_mmc_host *host = mmc_priv(mmc);
  710. unsigned long flags;
  711. int ret;
  712. spin_lock_irqsave(&host->lock, flags);
  713. if (host->mrq) {
  714. pr_debug("request not null\n");
  715. if (IS_ERR(host->mrq)) {
  716. spin_unlock_irqrestore(&host->lock, flags);
  717. mrq->cmd->error = -EAGAIN;
  718. mmc_request_done(mmc, mrq);
  719. return;
  720. }
  721. }
  722. host->last_req_ts = jiffies;
  723. wmb();
  724. host->mrq = mrq;
  725. spin_unlock_irqrestore(&host->lock, flags);
  726. if (mrq->data) {
  727. ret = tmio_mmc_start_data(host, mrq->data);
  728. if (ret)
  729. goto fail;
  730. }
  731. ret = tmio_mmc_start_command(host, mrq->cmd);
  732. if (!ret) {
  733. schedule_delayed_work(&host->delayed_reset_work,
  734. msecs_to_jiffies(CMDREQ_TIMEOUT));
  735. return;
  736. }
  737. fail:
  738. host->force_pio = false;
  739. host->mrq = NULL;
  740. mrq->cmd->error = ret;
  741. mmc_request_done(mmc, mrq);
  742. }
  743. static int tmio_mmc_clk_enable(struct tmio_mmc_host *host)
  744. {
  745. if (!host->clk_enable)
  746. return -ENOTSUPP;
  747. return host->clk_enable(host);
  748. }
  749. static void tmio_mmc_power_on(struct tmio_mmc_host *host, unsigned short vdd)
  750. {
  751. struct mmc_host *mmc = host->mmc;
  752. int ret = 0;
  753. /* .set_ios() is returning void, so, no chance to report an error */
  754. if (host->set_pwr)
  755. host->set_pwr(host->pdev, 1);
  756. if (!IS_ERR(mmc->supply.vmmc)) {
  757. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  758. /*
  759. * Attention: empiric value. With a b43 WiFi SDIO card this
  760. * delay proved necessary for reliable card-insertion probing.
  761. * 100us were not enough. Is this the same 140us delay, as in
  762. * tmio_mmc_set_ios()?
  763. */
  764. udelay(200);
  765. }
  766. /*
  767. * It seems, VccQ should be switched on after Vcc, this is also what the
  768. * omap_hsmmc.c driver does.
  769. */
  770. if (!IS_ERR(mmc->supply.vqmmc) && !ret) {
  771. ret = regulator_enable(mmc->supply.vqmmc);
  772. udelay(200);
  773. }
  774. if (ret < 0)
  775. dev_dbg(&host->pdev->dev, "Regulators failed to power up: %d\n",
  776. ret);
  777. }
  778. static void tmio_mmc_power_off(struct tmio_mmc_host *host)
  779. {
  780. struct mmc_host *mmc = host->mmc;
  781. if (!IS_ERR(mmc->supply.vqmmc))
  782. regulator_disable(mmc->supply.vqmmc);
  783. if (!IS_ERR(mmc->supply.vmmc))
  784. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  785. if (host->set_pwr)
  786. host->set_pwr(host->pdev, 0);
  787. }
  788. static void tmio_mmc_set_bus_width(struct tmio_mmc_host *host,
  789. unsigned char bus_width)
  790. {
  791. u16 reg = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT)
  792. & ~(CARD_OPT_WIDTH | CARD_OPT_WIDTH8);
  793. /* reg now applies to MMC_BUS_WIDTH_4 */
  794. if (bus_width == MMC_BUS_WIDTH_1)
  795. reg |= CARD_OPT_WIDTH;
  796. else if (bus_width == MMC_BUS_WIDTH_8)
  797. reg |= CARD_OPT_WIDTH8;
  798. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, reg);
  799. }
  800. /* Set MMC clock / power.
  801. * Note: This controller uses a simple divider scheme therefore it cannot
  802. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  803. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  804. * slowest setting.
  805. */
  806. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  807. {
  808. struct tmio_mmc_host *host = mmc_priv(mmc);
  809. struct device *dev = &host->pdev->dev;
  810. unsigned long flags;
  811. mutex_lock(&host->ios_lock);
  812. spin_lock_irqsave(&host->lock, flags);
  813. if (host->mrq) {
  814. if (IS_ERR(host->mrq)) {
  815. dev_dbg(dev,
  816. "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
  817. current->comm, task_pid_nr(current),
  818. ios->clock, ios->power_mode);
  819. host->mrq = ERR_PTR(-EINTR);
  820. } else {
  821. dev_dbg(dev,
  822. "%s.%d: CMD%u active since %lu, now %lu!\n",
  823. current->comm, task_pid_nr(current),
  824. host->mrq->cmd->opcode, host->last_req_ts, jiffies);
  825. }
  826. spin_unlock_irqrestore(&host->lock, flags);
  827. mutex_unlock(&host->ios_lock);
  828. return;
  829. }
  830. host->mrq = ERR_PTR(-EBUSY);
  831. spin_unlock_irqrestore(&host->lock, flags);
  832. switch (ios->power_mode) {
  833. case MMC_POWER_OFF:
  834. tmio_mmc_power_off(host);
  835. tmio_mmc_clk_stop(host);
  836. break;
  837. case MMC_POWER_UP:
  838. tmio_mmc_power_on(host, ios->vdd);
  839. tmio_mmc_set_clock(host, ios->clock);
  840. tmio_mmc_set_bus_width(host, ios->bus_width);
  841. break;
  842. case MMC_POWER_ON:
  843. tmio_mmc_set_clock(host, ios->clock);
  844. tmio_mmc_set_bus_width(host, ios->bus_width);
  845. break;
  846. }
  847. /* Let things settle. delay taken from winCE driver */
  848. udelay(140);
  849. if (PTR_ERR(host->mrq) == -EINTR)
  850. dev_dbg(&host->pdev->dev,
  851. "%s.%d: IOS interrupted: clk %u, mode %u",
  852. current->comm, task_pid_nr(current),
  853. ios->clock, ios->power_mode);
  854. host->mrq = NULL;
  855. host->clk_cache = ios->clock;
  856. mutex_unlock(&host->ios_lock);
  857. }
  858. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  859. {
  860. struct tmio_mmc_host *host = mmc_priv(mmc);
  861. struct tmio_mmc_data *pdata = host->pdata;
  862. int ret = mmc_gpio_get_ro(mmc);
  863. if (ret >= 0)
  864. return ret;
  865. ret = !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  866. (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
  867. return ret;
  868. }
  869. static int tmio_multi_io_quirk(struct mmc_card *card,
  870. unsigned int direction, int blk_size)
  871. {
  872. struct tmio_mmc_host *host = mmc_priv(card->host);
  873. if (host->multi_io_quirk)
  874. return host->multi_io_quirk(card, direction, blk_size);
  875. return blk_size;
  876. }
  877. static struct mmc_host_ops tmio_mmc_ops = {
  878. .request = tmio_mmc_request,
  879. .set_ios = tmio_mmc_set_ios,
  880. .get_ro = tmio_mmc_get_ro,
  881. .get_cd = mmc_gpio_get_cd,
  882. .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
  883. .multi_io_quirk = tmio_multi_io_quirk,
  884. .hw_reset = tmio_mmc_hw_reset,
  885. .execute_tuning = tmio_mmc_execute_tuning,
  886. };
  887. static int tmio_mmc_init_ocr(struct tmio_mmc_host *host)
  888. {
  889. struct tmio_mmc_data *pdata = host->pdata;
  890. struct mmc_host *mmc = host->mmc;
  891. mmc_regulator_get_supply(mmc);
  892. /* use ocr_mask if no regulator */
  893. if (!mmc->ocr_avail)
  894. mmc->ocr_avail = pdata->ocr_mask;
  895. /*
  896. * try again.
  897. * There is possibility that regulator has not been probed
  898. */
  899. if (!mmc->ocr_avail)
  900. return -EPROBE_DEFER;
  901. return 0;
  902. }
  903. static void tmio_mmc_of_parse(struct platform_device *pdev,
  904. struct tmio_mmc_data *pdata)
  905. {
  906. const struct device_node *np = pdev->dev.of_node;
  907. if (!np)
  908. return;
  909. if (of_get_property(np, "toshiba,mmc-wrprotect-disable", NULL))
  910. pdata->flags |= TMIO_MMC_WRPROTECT_DISABLE;
  911. }
  912. struct tmio_mmc_host*
  913. tmio_mmc_host_alloc(struct platform_device *pdev)
  914. {
  915. struct tmio_mmc_host *host;
  916. struct mmc_host *mmc;
  917. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
  918. if (!mmc)
  919. return NULL;
  920. host = mmc_priv(mmc);
  921. host->mmc = mmc;
  922. host->pdev = pdev;
  923. return host;
  924. }
  925. EXPORT_SYMBOL(tmio_mmc_host_alloc);
  926. void tmio_mmc_host_free(struct tmio_mmc_host *host)
  927. {
  928. mmc_free_host(host->mmc);
  929. }
  930. EXPORT_SYMBOL(tmio_mmc_host_free);
  931. int tmio_mmc_host_probe(struct tmio_mmc_host *_host,
  932. struct tmio_mmc_data *pdata)
  933. {
  934. struct platform_device *pdev = _host->pdev;
  935. struct mmc_host *mmc = _host->mmc;
  936. struct resource *res_ctl;
  937. int ret;
  938. u32 irq_mask = TMIO_MASK_CMD;
  939. tmio_mmc_of_parse(pdev, pdata);
  940. if (!(pdata->flags & TMIO_MMC_HAS_IDLE_WAIT))
  941. _host->write16_hook = NULL;
  942. res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  943. if (!res_ctl)
  944. return -EINVAL;
  945. ret = mmc_of_parse(mmc);
  946. if (ret < 0)
  947. goto host_free;
  948. _host->pdata = pdata;
  949. platform_set_drvdata(pdev, mmc);
  950. _host->set_pwr = pdata->set_pwr;
  951. _host->set_clk_div = pdata->set_clk_div;
  952. ret = tmio_mmc_init_ocr(_host);
  953. if (ret < 0)
  954. goto host_free;
  955. _host->ctl = devm_ioremap(&pdev->dev,
  956. res_ctl->start, resource_size(res_ctl));
  957. if (!_host->ctl) {
  958. ret = -ENOMEM;
  959. goto host_free;
  960. }
  961. tmio_mmc_ops.card_busy = _host->card_busy;
  962. tmio_mmc_ops.start_signal_voltage_switch = _host->start_signal_voltage_switch;
  963. mmc->ops = &tmio_mmc_ops;
  964. mmc->caps |= MMC_CAP_4_BIT_DATA | pdata->capabilities;
  965. mmc->caps2 |= pdata->capabilities2;
  966. mmc->max_segs = 32;
  967. mmc->max_blk_size = 512;
  968. mmc->max_blk_count = (PAGE_SIZE / mmc->max_blk_size) *
  969. mmc->max_segs;
  970. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  971. mmc->max_seg_size = mmc->max_req_size;
  972. _host->native_hotplug = !(pdata->flags & TMIO_MMC_USE_GPIO_CD ||
  973. mmc->caps & MMC_CAP_NEEDS_POLL ||
  974. !mmc_card_is_removable(mmc) ||
  975. mmc->slot.cd_irq >= 0);
  976. /*
  977. * On Gen2+, eMMC with NONREMOVABLE currently fails because native
  978. * hotplug gets disabled. It seems RuntimePM related yet we need further
  979. * research. Since we are planning a PM overhaul anyway, let's enforce
  980. * for now the device being active by enabling native hotplug always.
  981. */
  982. if (pdata->flags & TMIO_MMC_MIN_RCAR2)
  983. _host->native_hotplug = true;
  984. if (tmio_mmc_clk_enable(_host) < 0) {
  985. mmc->f_max = pdata->hclk;
  986. mmc->f_min = mmc->f_max / 512;
  987. }
  988. /*
  989. * Check the sanity of mmc->f_min to prevent tmio_mmc_set_clock() from
  990. * looping forever...
  991. */
  992. if (mmc->f_min == 0) {
  993. ret = -EINVAL;
  994. goto host_free;
  995. }
  996. /*
  997. * While using internal tmio hardware logic for card detection, we need
  998. * to ensure it stays powered for it to work.
  999. */
  1000. if (_host->native_hotplug)
  1001. pm_runtime_get_noresume(&pdev->dev);
  1002. tmio_mmc_clk_stop(_host);
  1003. tmio_mmc_reset(_host);
  1004. _host->sdcard_irq_mask = sd_ctrl_read16_and_16_as_32(_host, CTL_IRQ_MASK);
  1005. tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
  1006. /* Unmask the IRQs we want to know about */
  1007. if (!_host->chan_rx)
  1008. irq_mask |= TMIO_MASK_READOP;
  1009. if (!_host->chan_tx)
  1010. irq_mask |= TMIO_MASK_WRITEOP;
  1011. if (!_host->native_hotplug)
  1012. irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
  1013. _host->sdcard_irq_mask &= ~irq_mask;
  1014. _host->sdio_irq_enabled = false;
  1015. if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
  1016. _host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
  1017. sd_ctrl_write16(_host, CTL_SDIO_IRQ_MASK, _host->sdio_irq_mask);
  1018. sd_ctrl_write16(_host, CTL_TRANSACTION_CTL, 0x0000);
  1019. }
  1020. spin_lock_init(&_host->lock);
  1021. mutex_init(&_host->ios_lock);
  1022. /* Init delayed work for request timeouts */
  1023. INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
  1024. INIT_WORK(&_host->done, tmio_mmc_done_work);
  1025. /* See if we also get DMA */
  1026. tmio_mmc_request_dma(_host, pdata);
  1027. pm_runtime_set_active(&pdev->dev);
  1028. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1029. pm_runtime_use_autosuspend(&pdev->dev);
  1030. pm_runtime_enable(&pdev->dev);
  1031. ret = mmc_add_host(mmc);
  1032. if (ret < 0) {
  1033. tmio_mmc_host_remove(_host);
  1034. return ret;
  1035. }
  1036. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1037. if (pdata->flags & TMIO_MMC_USE_GPIO_CD) {
  1038. ret = mmc_gpio_request_cd(mmc, pdata->cd_gpio, 0);
  1039. if (ret < 0) {
  1040. tmio_mmc_host_remove(_host);
  1041. return ret;
  1042. }
  1043. mmc_gpiod_request_cd_irq(mmc);
  1044. }
  1045. return 0;
  1046. host_free:
  1047. return ret;
  1048. }
  1049. EXPORT_SYMBOL(tmio_mmc_host_probe);
  1050. void tmio_mmc_host_remove(struct tmio_mmc_host *host)
  1051. {
  1052. struct platform_device *pdev = host->pdev;
  1053. struct mmc_host *mmc = host->mmc;
  1054. if (!host->native_hotplug)
  1055. pm_runtime_get_sync(&pdev->dev);
  1056. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1057. mmc_remove_host(mmc);
  1058. cancel_work_sync(&host->done);
  1059. cancel_delayed_work_sync(&host->delayed_reset_work);
  1060. tmio_mmc_release_dma(host);
  1061. pm_runtime_put_sync(&pdev->dev);
  1062. pm_runtime_disable(&pdev->dev);
  1063. }
  1064. EXPORT_SYMBOL(tmio_mmc_host_remove);
  1065. #ifdef CONFIG_PM
  1066. int tmio_mmc_host_runtime_suspend(struct device *dev)
  1067. {
  1068. struct mmc_host *mmc = dev_get_drvdata(dev);
  1069. struct tmio_mmc_host *host = mmc_priv(mmc);
  1070. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
  1071. if (host->clk_cache)
  1072. tmio_mmc_clk_stop(host);
  1073. if (host->clk_disable)
  1074. host->clk_disable(host);
  1075. return 0;
  1076. }
  1077. EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
  1078. static bool tmio_mmc_can_retune(struct tmio_mmc_host *host)
  1079. {
  1080. return host->tap_num && mmc_can_retune(host->mmc);
  1081. }
  1082. int tmio_mmc_host_runtime_resume(struct device *dev)
  1083. {
  1084. struct mmc_host *mmc = dev_get_drvdata(dev);
  1085. struct tmio_mmc_host *host = mmc_priv(mmc);
  1086. tmio_mmc_reset(host);
  1087. tmio_mmc_clk_enable(host);
  1088. if (host->clk_cache)
  1089. tmio_mmc_set_clock(host, host->clk_cache);
  1090. tmio_mmc_enable_dma(host, true);
  1091. if (tmio_mmc_can_retune(host) && host->select_tuning(host))
  1092. dev_warn(&host->pdev->dev, "Tuning selection failed\n");
  1093. return 0;
  1094. }
  1095. EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
  1096. #endif
  1097. MODULE_LICENSE("GPL v2");