tmio_mmc_core.c 36 KB

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