sh_mmcif.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * MMCIF eMMC driver.
  3. *
  4. * Copyright (C) 2010 Renesas Solutions Corp.
  5. * Yusuke Goda <yusuke.goda.sx@renesas.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. *
  12. * TODO
  13. * 1. DMA
  14. * 2. Power management
  15. * 3. Handle MMC errors better
  16. *
  17. */
  18. /*
  19. * The MMCIF driver is now processing MMC requests asynchronously, according
  20. * to the Linux MMC API requirement.
  21. *
  22. * The MMCIF driver processes MMC requests in up to 3 stages: command, optional
  23. * data, and optional stop. To achieve asynchronous processing each of these
  24. * stages is split into two halves: a top and a bottom half. The top half
  25. * initialises the hardware, installs a timeout handler to handle completion
  26. * timeouts, and returns. In case of the command stage this immediately returns
  27. * control to the caller, leaving all further processing to run asynchronously.
  28. * All further request processing is performed by the bottom halves.
  29. *
  30. * The bottom half further consists of a "hard" IRQ handler, an IRQ handler
  31. * thread, a DMA completion callback, if DMA is used, a timeout work, and
  32. * request- and stage-specific handler methods.
  33. *
  34. * Each bottom half run begins with either a hardware interrupt, a DMA callback
  35. * invocation, or a timeout work run. In case of an error or a successful
  36. * processing completion, the MMC core is informed and the request processing is
  37. * finished. In case processing has to continue, i.e., if data has to be read
  38. * from or written to the card, or if a stop command has to be sent, the next
  39. * top half is called, which performs the necessary hardware handling and
  40. * reschedules the timeout work. This returns the driver state machine into the
  41. * bottom half waiting state.
  42. */
  43. #include <linux/bitops.h>
  44. #include <linux/clk.h>
  45. #include <linux/completion.h>
  46. #include <linux/delay.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/dmaengine.h>
  49. #include <linux/mmc/card.h>
  50. #include <linux/mmc/core.h>
  51. #include <linux/mmc/host.h>
  52. #include <linux/mmc/mmc.h>
  53. #include <linux/mmc/sdio.h>
  54. #include <linux/mmc/sh_mmcif.h>
  55. #include <linux/mmc/slot-gpio.h>
  56. #include <linux/mod_devicetable.h>
  57. #include <linux/pagemap.h>
  58. #include <linux/platform_device.h>
  59. #include <linux/pm_qos.h>
  60. #include <linux/pm_runtime.h>
  61. #include <linux/spinlock.h>
  62. #include <linux/module.h>
  63. #define DRIVER_NAME "sh_mmcif"
  64. #define DRIVER_VERSION "2010-04-28"
  65. /* CE_CMD_SET */
  66. #define CMD_MASK 0x3f000000
  67. #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
  68. #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
  69. #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
  70. #define CMD_SET_RBSY (1 << 21) /* R1b */
  71. #define CMD_SET_CCSEN (1 << 20)
  72. #define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
  73. #define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
  74. #define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
  75. #define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
  76. #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
  77. #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
  78. #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
  79. #define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
  80. #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
  81. #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
  82. #define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
  83. #define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
  84. #define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
  85. #define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
  86. #define CMD_SET_CCSH (1 << 5)
  87. #define CMD_SET_DARS (1 << 2) /* Dual Data Rate */
  88. #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
  89. #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
  90. #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
  91. /* CE_CMD_CTRL */
  92. #define CMD_CTRL_BREAK (1 << 0)
  93. /* CE_BLOCK_SET */
  94. #define BLOCK_SIZE_MASK 0x0000ffff
  95. /* CE_INT */
  96. #define INT_CCSDE (1 << 29)
  97. #define INT_CMD12DRE (1 << 26)
  98. #define INT_CMD12RBE (1 << 25)
  99. #define INT_CMD12CRE (1 << 24)
  100. #define INT_DTRANE (1 << 23)
  101. #define INT_BUFRE (1 << 22)
  102. #define INT_BUFWEN (1 << 21)
  103. #define INT_BUFREN (1 << 20)
  104. #define INT_CCSRCV (1 << 19)
  105. #define INT_RBSYE (1 << 17)
  106. #define INT_CRSPE (1 << 16)
  107. #define INT_CMDVIO (1 << 15)
  108. #define INT_BUFVIO (1 << 14)
  109. #define INT_WDATERR (1 << 11)
  110. #define INT_RDATERR (1 << 10)
  111. #define INT_RIDXERR (1 << 9)
  112. #define INT_RSPERR (1 << 8)
  113. #define INT_CCSTO (1 << 5)
  114. #define INT_CRCSTO (1 << 4)
  115. #define INT_WDATTO (1 << 3)
  116. #define INT_RDATTO (1 << 2)
  117. #define INT_RBSYTO (1 << 1)
  118. #define INT_RSPTO (1 << 0)
  119. #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
  120. INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
  121. INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
  122. INT_RDATTO | INT_RBSYTO | INT_RSPTO)
  123. /* CE_INT_MASK */
  124. #define MASK_ALL 0x00000000
  125. #define MASK_MCCSDE (1 << 29)
  126. #define MASK_MCMD12DRE (1 << 26)
  127. #define MASK_MCMD12RBE (1 << 25)
  128. #define MASK_MCMD12CRE (1 << 24)
  129. #define MASK_MDTRANE (1 << 23)
  130. #define MASK_MBUFRE (1 << 22)
  131. #define MASK_MBUFWEN (1 << 21)
  132. #define MASK_MBUFREN (1 << 20)
  133. #define MASK_MCCSRCV (1 << 19)
  134. #define MASK_MRBSYE (1 << 17)
  135. #define MASK_MCRSPE (1 << 16)
  136. #define MASK_MCMDVIO (1 << 15)
  137. #define MASK_MBUFVIO (1 << 14)
  138. #define MASK_MWDATERR (1 << 11)
  139. #define MASK_MRDATERR (1 << 10)
  140. #define MASK_MRIDXERR (1 << 9)
  141. #define MASK_MRSPERR (1 << 8)
  142. #define MASK_MCCSTO (1 << 5)
  143. #define MASK_MCRCSTO (1 << 4)
  144. #define MASK_MWDATTO (1 << 3)
  145. #define MASK_MRDATTO (1 << 2)
  146. #define MASK_MRBSYTO (1 << 1)
  147. #define MASK_MRSPTO (1 << 0)
  148. #define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \
  149. MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \
  150. MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO | \
  151. MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO)
  152. /* CE_HOST_STS1 */
  153. #define STS1_CMDSEQ (1 << 31)
  154. /* CE_HOST_STS2 */
  155. #define STS2_CRCSTE (1 << 31)
  156. #define STS2_CRC16E (1 << 30)
  157. #define STS2_AC12CRCE (1 << 29)
  158. #define STS2_RSPCRC7E (1 << 28)
  159. #define STS2_CRCSTEBE (1 << 27)
  160. #define STS2_RDATEBE (1 << 26)
  161. #define STS2_AC12REBE (1 << 25)
  162. #define STS2_RSPEBE (1 << 24)
  163. #define STS2_AC12IDXE (1 << 23)
  164. #define STS2_RSPIDXE (1 << 22)
  165. #define STS2_CCSTO (1 << 15)
  166. #define STS2_RDATTO (1 << 14)
  167. #define STS2_DATBSYTO (1 << 13)
  168. #define STS2_CRCSTTO (1 << 12)
  169. #define STS2_AC12BSYTO (1 << 11)
  170. #define STS2_RSPBSYTO (1 << 10)
  171. #define STS2_AC12RSPTO (1 << 9)
  172. #define STS2_RSPTO (1 << 8)
  173. #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
  174. STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
  175. #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
  176. STS2_DATBSYTO | STS2_CRCSTTO | \
  177. STS2_AC12BSYTO | STS2_RSPBSYTO | \
  178. STS2_AC12RSPTO | STS2_RSPTO)
  179. #define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
  180. #define CLKDEV_MMC_DATA 20000000 /* 20MHz */
  181. #define CLKDEV_INIT 400000 /* 400 KHz */
  182. enum mmcif_state {
  183. STATE_IDLE,
  184. STATE_REQUEST,
  185. STATE_IOS,
  186. };
  187. enum mmcif_wait_for {
  188. MMCIF_WAIT_FOR_REQUEST,
  189. MMCIF_WAIT_FOR_CMD,
  190. MMCIF_WAIT_FOR_MREAD,
  191. MMCIF_WAIT_FOR_MWRITE,
  192. MMCIF_WAIT_FOR_READ,
  193. MMCIF_WAIT_FOR_WRITE,
  194. MMCIF_WAIT_FOR_READ_END,
  195. MMCIF_WAIT_FOR_WRITE_END,
  196. MMCIF_WAIT_FOR_STOP,
  197. };
  198. struct sh_mmcif_host {
  199. struct mmc_host *mmc;
  200. struct mmc_request *mrq;
  201. struct platform_device *pd;
  202. struct clk *hclk;
  203. unsigned int clk;
  204. int bus_width;
  205. unsigned char timing;
  206. bool sd_error;
  207. bool dying;
  208. long timeout;
  209. void __iomem *addr;
  210. u32 *pio_ptr;
  211. spinlock_t lock; /* protect sh_mmcif_host::state */
  212. enum mmcif_state state;
  213. enum mmcif_wait_for wait_for;
  214. struct delayed_work timeout_work;
  215. size_t blocksize;
  216. int sg_idx;
  217. int sg_blkidx;
  218. bool power;
  219. bool card_present;
  220. /* DMA support */
  221. struct dma_chan *chan_rx;
  222. struct dma_chan *chan_tx;
  223. struct completion dma_complete;
  224. bool dma_active;
  225. };
  226. static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
  227. unsigned int reg, u32 val)
  228. {
  229. writel(val | readl(host->addr + reg), host->addr + reg);
  230. }
  231. static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
  232. unsigned int reg, u32 val)
  233. {
  234. writel(~val & readl(host->addr + reg), host->addr + reg);
  235. }
  236. static void mmcif_dma_complete(void *arg)
  237. {
  238. struct sh_mmcif_host *host = arg;
  239. struct mmc_data *data = host->mrq->data;
  240. dev_dbg(&host->pd->dev, "Command completed\n");
  241. if (WARN(!data, "%s: NULL data in DMA completion!\n",
  242. dev_name(&host->pd->dev)))
  243. return;
  244. if (data->flags & MMC_DATA_READ)
  245. dma_unmap_sg(host->chan_rx->device->dev,
  246. data->sg, data->sg_len,
  247. DMA_FROM_DEVICE);
  248. else
  249. dma_unmap_sg(host->chan_tx->device->dev,
  250. data->sg, data->sg_len,
  251. DMA_TO_DEVICE);
  252. complete(&host->dma_complete);
  253. }
  254. static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
  255. {
  256. struct mmc_data *data = host->mrq->data;
  257. struct scatterlist *sg = data->sg;
  258. struct dma_async_tx_descriptor *desc = NULL;
  259. struct dma_chan *chan = host->chan_rx;
  260. dma_cookie_t cookie = -EINVAL;
  261. int ret;
  262. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  263. DMA_FROM_DEVICE);
  264. if (ret > 0) {
  265. host->dma_active = true;
  266. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  267. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  268. }
  269. if (desc) {
  270. desc->callback = mmcif_dma_complete;
  271. desc->callback_param = host;
  272. cookie = dmaengine_submit(desc);
  273. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
  274. dma_async_issue_pending(chan);
  275. }
  276. dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
  277. __func__, data->sg_len, ret, cookie);
  278. if (!desc) {
  279. /* DMA failed, fall back to PIO */
  280. if (ret >= 0)
  281. ret = -EIO;
  282. host->chan_rx = NULL;
  283. host->dma_active = false;
  284. dma_release_channel(chan);
  285. /* Free the Tx channel too */
  286. chan = host->chan_tx;
  287. if (chan) {
  288. host->chan_tx = NULL;
  289. dma_release_channel(chan);
  290. }
  291. dev_warn(&host->pd->dev,
  292. "DMA failed: %d, falling back to PIO\n", ret);
  293. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  294. }
  295. dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  296. desc, cookie, data->sg_len);
  297. }
  298. static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
  299. {
  300. struct mmc_data *data = host->mrq->data;
  301. struct scatterlist *sg = data->sg;
  302. struct dma_async_tx_descriptor *desc = NULL;
  303. struct dma_chan *chan = host->chan_tx;
  304. dma_cookie_t cookie = -EINVAL;
  305. int ret;
  306. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  307. DMA_TO_DEVICE);
  308. if (ret > 0) {
  309. host->dma_active = true;
  310. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  311. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  312. }
  313. if (desc) {
  314. desc->callback = mmcif_dma_complete;
  315. desc->callback_param = host;
  316. cookie = dmaengine_submit(desc);
  317. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
  318. dma_async_issue_pending(chan);
  319. }
  320. dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
  321. __func__, data->sg_len, ret, cookie);
  322. if (!desc) {
  323. /* DMA failed, fall back to PIO */
  324. if (ret >= 0)
  325. ret = -EIO;
  326. host->chan_tx = NULL;
  327. host->dma_active = false;
  328. dma_release_channel(chan);
  329. /* Free the Rx channel too */
  330. chan = host->chan_rx;
  331. if (chan) {
  332. host->chan_rx = NULL;
  333. dma_release_channel(chan);
  334. }
  335. dev_warn(&host->pd->dev,
  336. "DMA failed: %d, falling back to PIO\n", ret);
  337. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  338. }
  339. dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__,
  340. desc, cookie);
  341. }
  342. static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
  343. struct sh_mmcif_plat_data *pdata)
  344. {
  345. struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
  346. struct dma_slave_config cfg;
  347. dma_cap_mask_t mask;
  348. int ret;
  349. host->dma_active = false;
  350. if (!pdata)
  351. return;
  352. if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
  353. return;
  354. /* We can only either use DMA for both Tx and Rx or not use it at all */
  355. dma_cap_zero(mask);
  356. dma_cap_set(DMA_SLAVE, mask);
  357. host->chan_tx = dma_request_channel(mask, shdma_chan_filter,
  358. (void *)pdata->slave_id_tx);
  359. dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
  360. host->chan_tx);
  361. if (!host->chan_tx)
  362. return;
  363. cfg.slave_id = pdata->slave_id_tx;
  364. cfg.direction = DMA_MEM_TO_DEV;
  365. cfg.dst_addr = res->start + MMCIF_CE_DATA;
  366. cfg.src_addr = 0;
  367. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  368. if (ret < 0)
  369. goto ecfgtx;
  370. host->chan_rx = dma_request_channel(mask, shdma_chan_filter,
  371. (void *)pdata->slave_id_rx);
  372. dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
  373. host->chan_rx);
  374. if (!host->chan_rx)
  375. goto erqrx;
  376. cfg.slave_id = pdata->slave_id_rx;
  377. cfg.direction = DMA_DEV_TO_MEM;
  378. cfg.dst_addr = 0;
  379. cfg.src_addr = res->start + MMCIF_CE_DATA;
  380. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  381. if (ret < 0)
  382. goto ecfgrx;
  383. init_completion(&host->dma_complete);
  384. return;
  385. ecfgrx:
  386. dma_release_channel(host->chan_rx);
  387. host->chan_rx = NULL;
  388. erqrx:
  389. ecfgtx:
  390. dma_release_channel(host->chan_tx);
  391. host->chan_tx = NULL;
  392. }
  393. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  394. {
  395. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  396. /* Descriptors are freed automatically */
  397. if (host->chan_tx) {
  398. struct dma_chan *chan = host->chan_tx;
  399. host->chan_tx = NULL;
  400. dma_release_channel(chan);
  401. }
  402. if (host->chan_rx) {
  403. struct dma_chan *chan = host->chan_rx;
  404. host->chan_rx = NULL;
  405. dma_release_channel(chan);
  406. }
  407. host->dma_active = false;
  408. }
  409. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  410. {
  411. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  412. bool sup_pclk = p ? p->sup_pclk : false;
  413. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  414. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  415. if (!clk)
  416. return;
  417. if (sup_pclk && clk == host->clk)
  418. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  419. else
  420. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  421. ((fls(DIV_ROUND_UP(host->clk,
  422. clk) - 1) - 1) << 16));
  423. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  424. }
  425. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  426. {
  427. u32 tmp;
  428. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  429. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  430. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  431. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  432. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
  433. /* byte swap on */
  434. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  435. }
  436. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  437. {
  438. u32 state1, state2;
  439. int ret, timeout;
  440. host->sd_error = false;
  441. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  442. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  443. dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
  444. dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
  445. if (state1 & STS1_CMDSEQ) {
  446. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  447. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  448. for (timeout = 10000000; timeout; timeout--) {
  449. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  450. & STS1_CMDSEQ))
  451. break;
  452. mdelay(1);
  453. }
  454. if (!timeout) {
  455. dev_err(&host->pd->dev,
  456. "Forced end of command sequence timeout err\n");
  457. return -EIO;
  458. }
  459. sh_mmcif_sync_reset(host);
  460. dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
  461. return -EIO;
  462. }
  463. if (state2 & STS2_CRC_ERR) {
  464. dev_dbg(&host->pd->dev, ": CRC error\n");
  465. ret = -EIO;
  466. } else if (state2 & STS2_TIMEOUT_ERR) {
  467. dev_dbg(&host->pd->dev, ": Timeout\n");
  468. ret = -ETIMEDOUT;
  469. } else {
  470. dev_dbg(&host->pd->dev, ": End/Index error\n");
  471. ret = -EIO;
  472. }
  473. return ret;
  474. }
  475. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  476. {
  477. struct mmc_data *data = host->mrq->data;
  478. host->sg_blkidx += host->blocksize;
  479. /* data->sg->length must be a multiple of host->blocksize? */
  480. BUG_ON(host->sg_blkidx > data->sg->length);
  481. if (host->sg_blkidx == data->sg->length) {
  482. host->sg_blkidx = 0;
  483. if (++host->sg_idx < data->sg_len)
  484. host->pio_ptr = sg_virt(++data->sg);
  485. } else {
  486. host->pio_ptr = p;
  487. }
  488. if (host->sg_idx == data->sg_len)
  489. return false;
  490. return true;
  491. }
  492. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  493. struct mmc_request *mrq)
  494. {
  495. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  496. BLOCK_SIZE_MASK) + 3;
  497. host->wait_for = MMCIF_WAIT_FOR_READ;
  498. /* buf read enable */
  499. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  500. }
  501. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  502. {
  503. struct mmc_data *data = host->mrq->data;
  504. u32 *p = sg_virt(data->sg);
  505. int i;
  506. if (host->sd_error) {
  507. data->error = sh_mmcif_error_manage(host);
  508. return false;
  509. }
  510. for (i = 0; i < host->blocksize / 4; i++)
  511. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  512. /* buffer read end */
  513. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  514. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  515. return true;
  516. }
  517. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  518. struct mmc_request *mrq)
  519. {
  520. struct mmc_data *data = mrq->data;
  521. if (!data->sg_len || !data->sg->length)
  522. return;
  523. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  524. BLOCK_SIZE_MASK;
  525. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  526. host->sg_idx = 0;
  527. host->sg_blkidx = 0;
  528. host->pio_ptr = sg_virt(data->sg);
  529. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  530. }
  531. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  532. {
  533. struct mmc_data *data = host->mrq->data;
  534. u32 *p = host->pio_ptr;
  535. int i;
  536. if (host->sd_error) {
  537. data->error = sh_mmcif_error_manage(host);
  538. return false;
  539. }
  540. BUG_ON(!data->sg->length);
  541. for (i = 0; i < host->blocksize / 4; i++)
  542. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  543. if (!sh_mmcif_next_block(host, p))
  544. return false;
  545. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  546. return true;
  547. }
  548. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  549. struct mmc_request *mrq)
  550. {
  551. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  552. BLOCK_SIZE_MASK) + 3;
  553. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  554. /* buf write enable */
  555. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  556. }
  557. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  558. {
  559. struct mmc_data *data = host->mrq->data;
  560. u32 *p = sg_virt(data->sg);
  561. int i;
  562. if (host->sd_error) {
  563. data->error = sh_mmcif_error_manage(host);
  564. return false;
  565. }
  566. for (i = 0; i < host->blocksize / 4; i++)
  567. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  568. /* buffer write end */
  569. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  570. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  571. return true;
  572. }
  573. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  574. struct mmc_request *mrq)
  575. {
  576. struct mmc_data *data = mrq->data;
  577. if (!data->sg_len || !data->sg->length)
  578. return;
  579. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  580. BLOCK_SIZE_MASK;
  581. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  582. host->sg_idx = 0;
  583. host->sg_blkidx = 0;
  584. host->pio_ptr = sg_virt(data->sg);
  585. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  586. }
  587. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  588. {
  589. struct mmc_data *data = host->mrq->data;
  590. u32 *p = host->pio_ptr;
  591. int i;
  592. if (host->sd_error) {
  593. data->error = sh_mmcif_error_manage(host);
  594. return false;
  595. }
  596. BUG_ON(!data->sg->length);
  597. for (i = 0; i < host->blocksize / 4; i++)
  598. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  599. if (!sh_mmcif_next_block(host, p))
  600. return false;
  601. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  602. return true;
  603. }
  604. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  605. struct mmc_command *cmd)
  606. {
  607. if (cmd->flags & MMC_RSP_136) {
  608. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  609. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  610. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  611. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  612. } else
  613. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  614. }
  615. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  616. struct mmc_command *cmd)
  617. {
  618. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  619. }
  620. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  621. struct mmc_request *mrq)
  622. {
  623. struct mmc_data *data = mrq->data;
  624. struct mmc_command *cmd = mrq->cmd;
  625. u32 opc = cmd->opcode;
  626. u32 tmp = 0;
  627. /* Response Type check */
  628. switch (mmc_resp_type(cmd)) {
  629. case MMC_RSP_NONE:
  630. tmp |= CMD_SET_RTYP_NO;
  631. break;
  632. case MMC_RSP_R1:
  633. case MMC_RSP_R1B:
  634. case MMC_RSP_R3:
  635. tmp |= CMD_SET_RTYP_6B;
  636. break;
  637. case MMC_RSP_R2:
  638. tmp |= CMD_SET_RTYP_17B;
  639. break;
  640. default:
  641. dev_err(&host->pd->dev, "Unsupported response type.\n");
  642. break;
  643. }
  644. switch (opc) {
  645. /* RBSY */
  646. case MMC_SLEEP_AWAKE:
  647. case MMC_SWITCH:
  648. case MMC_STOP_TRANSMISSION:
  649. case MMC_SET_WRITE_PROT:
  650. case MMC_CLR_WRITE_PROT:
  651. case MMC_ERASE:
  652. tmp |= CMD_SET_RBSY;
  653. break;
  654. }
  655. /* WDAT / DATW */
  656. if (data) {
  657. tmp |= CMD_SET_WDAT;
  658. switch (host->bus_width) {
  659. case MMC_BUS_WIDTH_1:
  660. tmp |= CMD_SET_DATW_1;
  661. break;
  662. case MMC_BUS_WIDTH_4:
  663. tmp |= CMD_SET_DATW_4;
  664. break;
  665. case MMC_BUS_WIDTH_8:
  666. tmp |= CMD_SET_DATW_8;
  667. break;
  668. default:
  669. dev_err(&host->pd->dev, "Unsupported bus width.\n");
  670. break;
  671. }
  672. switch (host->timing) {
  673. case MMC_TIMING_UHS_DDR50:
  674. /*
  675. * MMC core will only set this timing, if the host
  676. * advertises the MMC_CAP_UHS_DDR50 capability. MMCIF
  677. * implementations with this capability, e.g. sh73a0,
  678. * will have to set it in their platform data.
  679. */
  680. tmp |= CMD_SET_DARS;
  681. break;
  682. }
  683. }
  684. /* DWEN */
  685. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  686. tmp |= CMD_SET_DWEN;
  687. /* CMLTE/CMD12EN */
  688. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  689. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  690. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  691. data->blocks << 16);
  692. }
  693. /* RIDXC[1:0] check bits */
  694. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  695. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  696. tmp |= CMD_SET_RIDXC_BITS;
  697. /* RCRC7C[1:0] check bits */
  698. if (opc == MMC_SEND_OP_COND)
  699. tmp |= CMD_SET_CRC7C_BITS;
  700. /* RCRC7C[1:0] internal CRC7 */
  701. if (opc == MMC_ALL_SEND_CID ||
  702. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  703. tmp |= CMD_SET_CRC7C_INTERNAL;
  704. return (opc << 24) | tmp;
  705. }
  706. static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
  707. struct mmc_request *mrq, u32 opc)
  708. {
  709. switch (opc) {
  710. case MMC_READ_MULTIPLE_BLOCK:
  711. sh_mmcif_multi_read(host, mrq);
  712. return 0;
  713. case MMC_WRITE_MULTIPLE_BLOCK:
  714. sh_mmcif_multi_write(host, mrq);
  715. return 0;
  716. case MMC_WRITE_BLOCK:
  717. sh_mmcif_single_write(host, mrq);
  718. return 0;
  719. case MMC_READ_SINGLE_BLOCK:
  720. case MMC_SEND_EXT_CSD:
  721. sh_mmcif_single_read(host, mrq);
  722. return 0;
  723. default:
  724. dev_err(&host->pd->dev, "UNSUPPORTED CMD = d'%08d\n", opc);
  725. return -EINVAL;
  726. }
  727. }
  728. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  729. struct mmc_request *mrq)
  730. {
  731. struct mmc_command *cmd = mrq->cmd;
  732. u32 opc = cmd->opcode;
  733. u32 mask;
  734. switch (opc) {
  735. /* response busy check */
  736. case MMC_SLEEP_AWAKE:
  737. case MMC_SWITCH:
  738. case MMC_STOP_TRANSMISSION:
  739. case MMC_SET_WRITE_PROT:
  740. case MMC_CLR_WRITE_PROT:
  741. case MMC_ERASE:
  742. mask = MASK_START_CMD | MASK_MRBSYE;
  743. break;
  744. default:
  745. mask = MASK_START_CMD | MASK_MCRSPE;
  746. break;
  747. }
  748. if (mrq->data) {
  749. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  750. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  751. mrq->data->blksz);
  752. }
  753. opc = sh_mmcif_set_cmd(host, mrq);
  754. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  755. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  756. /* set arg */
  757. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  758. /* set cmd */
  759. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  760. host->wait_for = MMCIF_WAIT_FOR_CMD;
  761. schedule_delayed_work(&host->timeout_work, host->timeout);
  762. }
  763. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  764. struct mmc_request *mrq)
  765. {
  766. switch (mrq->cmd->opcode) {
  767. case MMC_READ_MULTIPLE_BLOCK:
  768. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  769. break;
  770. case MMC_WRITE_MULTIPLE_BLOCK:
  771. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  772. break;
  773. default:
  774. dev_err(&host->pd->dev, "unsupported stop cmd\n");
  775. mrq->stop->error = sh_mmcif_error_manage(host);
  776. return;
  777. }
  778. host->wait_for = MMCIF_WAIT_FOR_STOP;
  779. }
  780. static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
  781. {
  782. struct sh_mmcif_host *host = mmc_priv(mmc);
  783. unsigned long flags;
  784. spin_lock_irqsave(&host->lock, flags);
  785. if (host->state != STATE_IDLE) {
  786. spin_unlock_irqrestore(&host->lock, flags);
  787. mrq->cmd->error = -EAGAIN;
  788. mmc_request_done(mmc, mrq);
  789. return;
  790. }
  791. host->state = STATE_REQUEST;
  792. spin_unlock_irqrestore(&host->lock, flags);
  793. switch (mrq->cmd->opcode) {
  794. /* MMCIF does not support SD/SDIO command */
  795. case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
  796. case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
  797. if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
  798. break;
  799. case MMC_APP_CMD:
  800. case SD_IO_RW_DIRECT:
  801. host->state = STATE_IDLE;
  802. mrq->cmd->error = -ETIMEDOUT;
  803. mmc_request_done(mmc, mrq);
  804. return;
  805. default:
  806. break;
  807. }
  808. host->mrq = mrq;
  809. sh_mmcif_start_cmd(host, mrq);
  810. }
  811. static int sh_mmcif_clk_update(struct sh_mmcif_host *host)
  812. {
  813. int ret = clk_enable(host->hclk);
  814. if (!ret) {
  815. host->clk = clk_get_rate(host->hclk);
  816. host->mmc->f_max = host->clk / 2;
  817. host->mmc->f_min = host->clk / 512;
  818. }
  819. return ret;
  820. }
  821. static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
  822. {
  823. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  824. struct mmc_host *mmc = host->mmc;
  825. if (pd && pd->set_pwr)
  826. pd->set_pwr(host->pd, ios->power_mode != MMC_POWER_OFF);
  827. if (!IS_ERR(mmc->supply.vmmc))
  828. /* Errors ignored... */
  829. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  830. ios->power_mode ? ios->vdd : 0);
  831. }
  832. static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  833. {
  834. struct sh_mmcif_host *host = mmc_priv(mmc);
  835. unsigned long flags;
  836. spin_lock_irqsave(&host->lock, flags);
  837. if (host->state != STATE_IDLE) {
  838. spin_unlock_irqrestore(&host->lock, flags);
  839. return;
  840. }
  841. host->state = STATE_IOS;
  842. spin_unlock_irqrestore(&host->lock, flags);
  843. if (ios->power_mode == MMC_POWER_UP) {
  844. if (!host->card_present) {
  845. /* See if we also get DMA */
  846. sh_mmcif_request_dma(host, host->pd->dev.platform_data);
  847. host->card_present = true;
  848. }
  849. sh_mmcif_set_power(host, ios);
  850. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  851. /* clock stop */
  852. sh_mmcif_clock_control(host, 0);
  853. if (ios->power_mode == MMC_POWER_OFF) {
  854. if (host->card_present) {
  855. sh_mmcif_release_dma(host);
  856. host->card_present = false;
  857. }
  858. }
  859. if (host->power) {
  860. pm_runtime_put_sync(&host->pd->dev);
  861. clk_disable(host->hclk);
  862. host->power = false;
  863. if (ios->power_mode == MMC_POWER_OFF)
  864. sh_mmcif_set_power(host, ios);
  865. }
  866. host->state = STATE_IDLE;
  867. return;
  868. }
  869. if (ios->clock) {
  870. if (!host->power) {
  871. sh_mmcif_clk_update(host);
  872. pm_runtime_get_sync(&host->pd->dev);
  873. host->power = true;
  874. sh_mmcif_sync_reset(host);
  875. }
  876. sh_mmcif_clock_control(host, ios->clock);
  877. }
  878. host->timing = ios->timing;
  879. host->bus_width = ios->bus_width;
  880. host->state = STATE_IDLE;
  881. }
  882. static int sh_mmcif_get_cd(struct mmc_host *mmc)
  883. {
  884. struct sh_mmcif_host *host = mmc_priv(mmc);
  885. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  886. int ret = mmc_gpio_get_cd(mmc);
  887. if (ret >= 0)
  888. return ret;
  889. if (!p || !p->get_cd)
  890. return -ENOSYS;
  891. else
  892. return p->get_cd(host->pd);
  893. }
  894. static struct mmc_host_ops sh_mmcif_ops = {
  895. .request = sh_mmcif_request,
  896. .set_ios = sh_mmcif_set_ios,
  897. .get_cd = sh_mmcif_get_cd,
  898. };
  899. static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
  900. {
  901. struct mmc_command *cmd = host->mrq->cmd;
  902. struct mmc_data *data = host->mrq->data;
  903. long time;
  904. if (host->sd_error) {
  905. switch (cmd->opcode) {
  906. case MMC_ALL_SEND_CID:
  907. case MMC_SELECT_CARD:
  908. case MMC_APP_CMD:
  909. cmd->error = -ETIMEDOUT;
  910. host->sd_error = false;
  911. break;
  912. default:
  913. cmd->error = sh_mmcif_error_manage(host);
  914. dev_dbg(&host->pd->dev, "Cmd(d'%d) error %d\n",
  915. cmd->opcode, cmd->error);
  916. break;
  917. }
  918. return false;
  919. }
  920. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  921. cmd->error = 0;
  922. return false;
  923. }
  924. sh_mmcif_get_response(host, cmd);
  925. if (!data)
  926. return false;
  927. if (data->flags & MMC_DATA_READ) {
  928. if (host->chan_rx)
  929. sh_mmcif_start_dma_rx(host);
  930. } else {
  931. if (host->chan_tx)
  932. sh_mmcif_start_dma_tx(host);
  933. }
  934. if (!host->dma_active) {
  935. data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
  936. if (!data->error)
  937. return true;
  938. return false;
  939. }
  940. /* Running in the IRQ thread, can sleep */
  941. time = wait_for_completion_interruptible_timeout(&host->dma_complete,
  942. host->timeout);
  943. if (host->sd_error) {
  944. dev_err(host->mmc->parent,
  945. "Error IRQ while waiting for DMA completion!\n");
  946. /* Woken up by an error IRQ: abort DMA */
  947. if (data->flags & MMC_DATA_READ)
  948. dmaengine_terminate_all(host->chan_rx);
  949. else
  950. dmaengine_terminate_all(host->chan_tx);
  951. data->error = sh_mmcif_error_manage(host);
  952. } else if (!time) {
  953. data->error = -ETIMEDOUT;
  954. } else if (time < 0) {
  955. data->error = time;
  956. }
  957. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
  958. BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  959. host->dma_active = false;
  960. if (data->error)
  961. data->bytes_xfered = 0;
  962. return false;
  963. }
  964. static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
  965. {
  966. struct sh_mmcif_host *host = dev_id;
  967. struct mmc_request *mrq = host->mrq;
  968. bool wait = false;
  969. cancel_delayed_work_sync(&host->timeout_work);
  970. /*
  971. * All handlers return true, if processing continues, and false, if the
  972. * request has to be completed - successfully or not
  973. */
  974. switch (host->wait_for) {
  975. case MMCIF_WAIT_FOR_REQUEST:
  976. /* We're too late, the timeout has already kicked in */
  977. return IRQ_HANDLED;
  978. case MMCIF_WAIT_FOR_CMD:
  979. /* Wait for data? */
  980. wait = sh_mmcif_end_cmd(host);
  981. break;
  982. case MMCIF_WAIT_FOR_MREAD:
  983. /* Wait for more data? */
  984. wait = sh_mmcif_mread_block(host);
  985. break;
  986. case MMCIF_WAIT_FOR_READ:
  987. /* Wait for data end? */
  988. wait = sh_mmcif_read_block(host);
  989. break;
  990. case MMCIF_WAIT_FOR_MWRITE:
  991. /* Wait data to write? */
  992. wait = sh_mmcif_mwrite_block(host);
  993. break;
  994. case MMCIF_WAIT_FOR_WRITE:
  995. /* Wait for data end? */
  996. wait = sh_mmcif_write_block(host);
  997. break;
  998. case MMCIF_WAIT_FOR_STOP:
  999. if (host->sd_error) {
  1000. mrq->stop->error = sh_mmcif_error_manage(host);
  1001. break;
  1002. }
  1003. sh_mmcif_get_cmd12response(host, mrq->stop);
  1004. mrq->stop->error = 0;
  1005. break;
  1006. case MMCIF_WAIT_FOR_READ_END:
  1007. case MMCIF_WAIT_FOR_WRITE_END:
  1008. if (host->sd_error)
  1009. mrq->data->error = sh_mmcif_error_manage(host);
  1010. break;
  1011. default:
  1012. BUG();
  1013. }
  1014. if (wait) {
  1015. schedule_delayed_work(&host->timeout_work, host->timeout);
  1016. /* Wait for more data */
  1017. return IRQ_HANDLED;
  1018. }
  1019. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1020. struct mmc_data *data = mrq->data;
  1021. if (!mrq->cmd->error && data && !data->error)
  1022. data->bytes_xfered =
  1023. data->blocks * data->blksz;
  1024. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1025. sh_mmcif_stop_cmd(host, mrq);
  1026. if (!mrq->stop->error) {
  1027. schedule_delayed_work(&host->timeout_work, host->timeout);
  1028. return IRQ_HANDLED;
  1029. }
  1030. }
  1031. }
  1032. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1033. host->state = STATE_IDLE;
  1034. host->mrq = NULL;
  1035. mmc_request_done(host->mmc, mrq);
  1036. return IRQ_HANDLED;
  1037. }
  1038. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1039. {
  1040. struct sh_mmcif_host *host = dev_id;
  1041. u32 state;
  1042. int err = 0;
  1043. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1044. if (state & INT_ERR_STS) {
  1045. /* error interrupts - process first */
  1046. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
  1047. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  1048. err = 1;
  1049. } else if (state & INT_RBSYE) {
  1050. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1051. ~(INT_RBSYE | INT_CRSPE));
  1052. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE);
  1053. } else if (state & INT_CRSPE) {
  1054. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE);
  1055. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE);
  1056. } else if (state & INT_BUFREN) {
  1057. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN);
  1058. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  1059. } else if (state & INT_BUFWEN) {
  1060. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFWEN);
  1061. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  1062. } else if (state & INT_CMD12DRE) {
  1063. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1064. ~(INT_CMD12DRE | INT_CMD12RBE |
  1065. INT_CMD12CRE | INT_BUFRE));
  1066. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  1067. } else if (state & INT_BUFRE) {
  1068. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE);
  1069. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  1070. } else if (state & INT_DTRANE) {
  1071. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1072. ~(INT_CMD12DRE | INT_CMD12RBE |
  1073. INT_CMD12CRE | INT_DTRANE));
  1074. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  1075. } else if (state & INT_CMD12RBE) {
  1076. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1077. ~(INT_CMD12RBE | INT_CMD12CRE));
  1078. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  1079. } else {
  1080. dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x\n", state);
  1081. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
  1082. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  1083. err = 1;
  1084. }
  1085. if (err) {
  1086. host->sd_error = true;
  1087. dev_dbg(&host->pd->dev, "int err state = %08x\n", state);
  1088. }
  1089. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1090. if (!host->dma_active)
  1091. return IRQ_WAKE_THREAD;
  1092. else if (host->sd_error)
  1093. mmcif_dma_complete(host);
  1094. } else {
  1095. dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
  1096. }
  1097. return IRQ_HANDLED;
  1098. }
  1099. static void mmcif_timeout_work(struct work_struct *work)
  1100. {
  1101. struct delayed_work *d = container_of(work, struct delayed_work, work);
  1102. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1103. struct mmc_request *mrq = host->mrq;
  1104. if (host->dying)
  1105. /* Don't run after mmc_remove_host() */
  1106. return;
  1107. /*
  1108. * Handle races with cancel_delayed_work(), unless
  1109. * cancel_delayed_work_sync() is used
  1110. */
  1111. switch (host->wait_for) {
  1112. case MMCIF_WAIT_FOR_CMD:
  1113. mrq->cmd->error = sh_mmcif_error_manage(host);
  1114. break;
  1115. case MMCIF_WAIT_FOR_STOP:
  1116. mrq->stop->error = sh_mmcif_error_manage(host);
  1117. break;
  1118. case MMCIF_WAIT_FOR_MREAD:
  1119. case MMCIF_WAIT_FOR_MWRITE:
  1120. case MMCIF_WAIT_FOR_READ:
  1121. case MMCIF_WAIT_FOR_WRITE:
  1122. case MMCIF_WAIT_FOR_READ_END:
  1123. case MMCIF_WAIT_FOR_WRITE_END:
  1124. mrq->data->error = sh_mmcif_error_manage(host);
  1125. break;
  1126. default:
  1127. BUG();
  1128. }
  1129. host->state = STATE_IDLE;
  1130. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1131. host->mrq = NULL;
  1132. mmc_request_done(host->mmc, mrq);
  1133. }
  1134. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1135. {
  1136. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  1137. struct mmc_host *mmc = host->mmc;
  1138. mmc_regulator_get_supply(mmc);
  1139. if (!pd)
  1140. return;
  1141. if (!mmc->ocr_avail)
  1142. mmc->ocr_avail = pd->ocr;
  1143. else if (pd->ocr)
  1144. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1145. }
  1146. static int sh_mmcif_probe(struct platform_device *pdev)
  1147. {
  1148. int ret = 0, irq[2];
  1149. struct mmc_host *mmc;
  1150. struct sh_mmcif_host *host;
  1151. struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
  1152. struct resource *res;
  1153. void __iomem *reg;
  1154. const char *name;
  1155. irq[0] = platform_get_irq(pdev, 0);
  1156. irq[1] = platform_get_irq(pdev, 1);
  1157. if (irq[0] < 0) {
  1158. dev_err(&pdev->dev, "Get irq error\n");
  1159. return -ENXIO;
  1160. }
  1161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1162. if (!res) {
  1163. dev_err(&pdev->dev, "platform_get_resource error.\n");
  1164. return -ENXIO;
  1165. }
  1166. reg = ioremap(res->start, resource_size(res));
  1167. if (!reg) {
  1168. dev_err(&pdev->dev, "ioremap error.\n");
  1169. return -ENOMEM;
  1170. }
  1171. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  1172. if (!mmc) {
  1173. ret = -ENOMEM;
  1174. goto ealloch;
  1175. }
  1176. host = mmc_priv(mmc);
  1177. host->mmc = mmc;
  1178. host->addr = reg;
  1179. host->timeout = msecs_to_jiffies(1000);
  1180. host->pd = pdev;
  1181. spin_lock_init(&host->lock);
  1182. mmc->ops = &sh_mmcif_ops;
  1183. sh_mmcif_init_ocr(host);
  1184. mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1185. if (pd && pd->caps)
  1186. mmc->caps |= pd->caps;
  1187. mmc->max_segs = 32;
  1188. mmc->max_blk_size = 512;
  1189. mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
  1190. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1191. mmc->max_seg_size = mmc->max_req_size;
  1192. platform_set_drvdata(pdev, host);
  1193. pm_runtime_enable(&pdev->dev);
  1194. host->power = false;
  1195. host->hclk = clk_get(&pdev->dev, NULL);
  1196. if (IS_ERR(host->hclk)) {
  1197. ret = PTR_ERR(host->hclk);
  1198. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  1199. goto eclkget;
  1200. }
  1201. ret = sh_mmcif_clk_update(host);
  1202. if (ret < 0)
  1203. goto eclkupdate;
  1204. ret = pm_runtime_resume(&pdev->dev);
  1205. if (ret < 0)
  1206. goto eresume;
  1207. INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
  1208. sh_mmcif_sync_reset(host);
  1209. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1210. name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
  1211. ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, name, host);
  1212. if (ret) {
  1213. dev_err(&pdev->dev, "request_irq error (%s)\n", name);
  1214. goto ereqirq0;
  1215. }
  1216. if (irq[1] >= 0) {
  1217. ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt,
  1218. 0, "sh_mmc:int", host);
  1219. if (ret) {
  1220. dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
  1221. goto ereqirq1;
  1222. }
  1223. }
  1224. if (pd && pd->use_cd_gpio) {
  1225. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio);
  1226. if (ret < 0)
  1227. goto erqcd;
  1228. }
  1229. clk_disable(host->hclk);
  1230. ret = mmc_add_host(mmc);
  1231. if (ret < 0)
  1232. goto emmcaddh;
  1233. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1234. dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
  1235. dev_dbg(&pdev->dev, "chip ver H'%04x\n",
  1236. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
  1237. return ret;
  1238. emmcaddh:
  1239. erqcd:
  1240. if (irq[1] >= 0)
  1241. free_irq(irq[1], host);
  1242. ereqirq1:
  1243. free_irq(irq[0], host);
  1244. ereqirq0:
  1245. pm_runtime_suspend(&pdev->dev);
  1246. eresume:
  1247. clk_disable(host->hclk);
  1248. eclkupdate:
  1249. clk_put(host->hclk);
  1250. eclkget:
  1251. pm_runtime_disable(&pdev->dev);
  1252. mmc_free_host(mmc);
  1253. ealloch:
  1254. iounmap(reg);
  1255. return ret;
  1256. }
  1257. static int sh_mmcif_remove(struct platform_device *pdev)
  1258. {
  1259. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1260. int irq[2];
  1261. host->dying = true;
  1262. clk_enable(host->hclk);
  1263. pm_runtime_get_sync(&pdev->dev);
  1264. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1265. mmc_remove_host(host->mmc);
  1266. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1267. /*
  1268. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1269. * mmc_remove_host() call above. But swapping order doesn't help either
  1270. * (a query on the linux-mmc mailing list didn't bring any replies).
  1271. */
  1272. cancel_delayed_work_sync(&host->timeout_work);
  1273. if (host->addr)
  1274. iounmap(host->addr);
  1275. irq[0] = platform_get_irq(pdev, 0);
  1276. irq[1] = platform_get_irq(pdev, 1);
  1277. free_irq(irq[0], host);
  1278. if (irq[1] >= 0)
  1279. free_irq(irq[1], host);
  1280. platform_set_drvdata(pdev, NULL);
  1281. clk_disable(host->hclk);
  1282. mmc_free_host(host->mmc);
  1283. pm_runtime_put_sync(&pdev->dev);
  1284. pm_runtime_disable(&pdev->dev);
  1285. return 0;
  1286. }
  1287. #ifdef CONFIG_PM
  1288. static int sh_mmcif_suspend(struct device *dev)
  1289. {
  1290. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1291. int ret = mmc_suspend_host(host->mmc);
  1292. if (!ret)
  1293. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1294. return ret;
  1295. }
  1296. static int sh_mmcif_resume(struct device *dev)
  1297. {
  1298. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1299. return mmc_resume_host(host->mmc);
  1300. }
  1301. #else
  1302. #define sh_mmcif_suspend NULL
  1303. #define sh_mmcif_resume NULL
  1304. #endif /* CONFIG_PM */
  1305. static const struct of_device_id mmcif_of_match[] = {
  1306. { .compatible = "renesas,sh-mmcif" },
  1307. { }
  1308. };
  1309. MODULE_DEVICE_TABLE(of, mmcif_of_match);
  1310. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1311. .suspend = sh_mmcif_suspend,
  1312. .resume = sh_mmcif_resume,
  1313. };
  1314. static struct platform_driver sh_mmcif_driver = {
  1315. .probe = sh_mmcif_probe,
  1316. .remove = sh_mmcif_remove,
  1317. .driver = {
  1318. .name = DRIVER_NAME,
  1319. .pm = &sh_mmcif_dev_pm_ops,
  1320. .owner = THIS_MODULE,
  1321. .of_match_table = mmcif_of_match,
  1322. },
  1323. };
  1324. module_platform_driver(sh_mmcif_driver);
  1325. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1326. MODULE_LICENSE("GPL");
  1327. MODULE_ALIAS("platform:" DRIVER_NAME);
  1328. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");