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