tmio_mmc_core.c 34 KB

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