sh_mmcif.c 39 KB

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