tmio_mmc_core.c 35 KB

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