tmio_mmc_core.c 35 KB

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