tmio_mmc_pio.c 34 KB

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