sh_mmcif.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. complete(&host->dma_complete);
  245. }
  246. static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
  247. {
  248. struct mmc_data *data = host->mrq->data;
  249. struct scatterlist *sg = data->sg;
  250. struct dma_async_tx_descriptor *desc = NULL;
  251. struct dma_chan *chan = host->chan_rx;
  252. dma_cookie_t cookie = -EINVAL;
  253. int ret;
  254. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  255. DMA_FROM_DEVICE);
  256. if (ret > 0) {
  257. host->dma_active = true;
  258. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  259. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  260. }
  261. if (desc) {
  262. desc->callback = mmcif_dma_complete;
  263. desc->callback_param = host;
  264. cookie = dmaengine_submit(desc);
  265. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
  266. dma_async_issue_pending(chan);
  267. }
  268. dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
  269. __func__, data->sg_len, ret, cookie);
  270. if (!desc) {
  271. /* DMA failed, fall back to PIO */
  272. if (ret >= 0)
  273. ret = -EIO;
  274. host->chan_rx = NULL;
  275. host->dma_active = false;
  276. dma_release_channel(chan);
  277. /* Free the Tx channel too */
  278. chan = host->chan_tx;
  279. if (chan) {
  280. host->chan_tx = NULL;
  281. dma_release_channel(chan);
  282. }
  283. dev_warn(&host->pd->dev,
  284. "DMA failed: %d, falling back to PIO\n", ret);
  285. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  286. }
  287. dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  288. desc, cookie, data->sg_len);
  289. }
  290. static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
  291. {
  292. struct mmc_data *data = host->mrq->data;
  293. struct scatterlist *sg = data->sg;
  294. struct dma_async_tx_descriptor *desc = NULL;
  295. struct dma_chan *chan = host->chan_tx;
  296. dma_cookie_t cookie = -EINVAL;
  297. int ret;
  298. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  299. DMA_TO_DEVICE);
  300. if (ret > 0) {
  301. host->dma_active = true;
  302. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  303. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  304. }
  305. if (desc) {
  306. desc->callback = mmcif_dma_complete;
  307. desc->callback_param = host;
  308. cookie = dmaengine_submit(desc);
  309. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
  310. dma_async_issue_pending(chan);
  311. }
  312. dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
  313. __func__, data->sg_len, ret, cookie);
  314. if (!desc) {
  315. /* DMA failed, fall back to PIO */
  316. if (ret >= 0)
  317. ret = -EIO;
  318. host->chan_tx = NULL;
  319. host->dma_active = false;
  320. dma_release_channel(chan);
  321. /* Free the Rx channel too */
  322. chan = host->chan_rx;
  323. if (chan) {
  324. host->chan_rx = NULL;
  325. dma_release_channel(chan);
  326. }
  327. dev_warn(&host->pd->dev,
  328. "DMA failed: %d, falling back to PIO\n", ret);
  329. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  330. }
  331. dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__,
  332. desc, cookie);
  333. }
  334. static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
  335. struct sh_mmcif_plat_data *pdata)
  336. {
  337. struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
  338. struct dma_slave_config cfg;
  339. dma_cap_mask_t mask;
  340. int ret;
  341. host->dma_active = false;
  342. if (!pdata)
  343. return;
  344. if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
  345. return;
  346. /* We can only either use DMA for both Tx and Rx or not use it at all */
  347. dma_cap_zero(mask);
  348. dma_cap_set(DMA_SLAVE, mask);
  349. host->chan_tx = dma_request_channel(mask, shdma_chan_filter,
  350. (void *)pdata->slave_id_tx);
  351. dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
  352. host->chan_tx);
  353. if (!host->chan_tx)
  354. return;
  355. cfg.slave_id = pdata->slave_id_tx;
  356. cfg.direction = DMA_MEM_TO_DEV;
  357. cfg.dst_addr = res->start + MMCIF_CE_DATA;
  358. cfg.src_addr = 0;
  359. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  360. if (ret < 0)
  361. goto ecfgtx;
  362. host->chan_rx = dma_request_channel(mask, shdma_chan_filter,
  363. (void *)pdata->slave_id_rx);
  364. dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
  365. host->chan_rx);
  366. if (!host->chan_rx)
  367. goto erqrx;
  368. cfg.slave_id = pdata->slave_id_rx;
  369. cfg.direction = DMA_DEV_TO_MEM;
  370. cfg.dst_addr = 0;
  371. cfg.src_addr = res->start + MMCIF_CE_DATA;
  372. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  373. if (ret < 0)
  374. goto ecfgrx;
  375. init_completion(&host->dma_complete);
  376. return;
  377. ecfgrx:
  378. dma_release_channel(host->chan_rx);
  379. host->chan_rx = NULL;
  380. erqrx:
  381. ecfgtx:
  382. dma_release_channel(host->chan_tx);
  383. host->chan_tx = NULL;
  384. }
  385. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  386. {
  387. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  388. /* Descriptors are freed automatically */
  389. if (host->chan_tx) {
  390. struct dma_chan *chan = host->chan_tx;
  391. host->chan_tx = NULL;
  392. dma_release_channel(chan);
  393. }
  394. if (host->chan_rx) {
  395. struct dma_chan *chan = host->chan_rx;
  396. host->chan_rx = NULL;
  397. dma_release_channel(chan);
  398. }
  399. host->dma_active = false;
  400. }
  401. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  402. {
  403. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  404. bool sup_pclk = p ? p->sup_pclk : false;
  405. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  406. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  407. if (!clk)
  408. return;
  409. if (sup_pclk && clk == host->clk)
  410. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  411. else
  412. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  413. ((fls(DIV_ROUND_UP(host->clk,
  414. clk) - 1) - 1) << 16));
  415. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  416. }
  417. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  418. {
  419. u32 tmp;
  420. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  421. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  422. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  423. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  424. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
  425. /* byte swap on */
  426. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  427. }
  428. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  429. {
  430. u32 state1, state2;
  431. int ret, timeout;
  432. host->sd_error = false;
  433. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  434. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  435. dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
  436. dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
  437. if (state1 & STS1_CMDSEQ) {
  438. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  439. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  440. for (timeout = 10000000; timeout; timeout--) {
  441. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  442. & STS1_CMDSEQ))
  443. break;
  444. mdelay(1);
  445. }
  446. if (!timeout) {
  447. dev_err(&host->pd->dev,
  448. "Forced end of command sequence timeout err\n");
  449. return -EIO;
  450. }
  451. sh_mmcif_sync_reset(host);
  452. dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
  453. return -EIO;
  454. }
  455. if (state2 & STS2_CRC_ERR) {
  456. dev_dbg(&host->pd->dev, ": CRC error\n");
  457. ret = -EIO;
  458. } else if (state2 & STS2_TIMEOUT_ERR) {
  459. dev_dbg(&host->pd->dev, ": Timeout\n");
  460. ret = -ETIMEDOUT;
  461. } else {
  462. dev_dbg(&host->pd->dev, ": End/Index error\n");
  463. ret = -EIO;
  464. }
  465. return ret;
  466. }
  467. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  468. {
  469. struct mmc_data *data = host->mrq->data;
  470. host->sg_blkidx += host->blocksize;
  471. /* data->sg->length must be a multiple of host->blocksize? */
  472. BUG_ON(host->sg_blkidx > data->sg->length);
  473. if (host->sg_blkidx == data->sg->length) {
  474. host->sg_blkidx = 0;
  475. if (++host->sg_idx < data->sg_len)
  476. host->pio_ptr = sg_virt(++data->sg);
  477. } else {
  478. host->pio_ptr = p;
  479. }
  480. if (host->sg_idx == data->sg_len)
  481. return false;
  482. return true;
  483. }
  484. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  485. struct mmc_request *mrq)
  486. {
  487. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  488. BLOCK_SIZE_MASK) + 3;
  489. host->wait_for = MMCIF_WAIT_FOR_READ;
  490. /* buf read enable */
  491. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  492. }
  493. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  494. {
  495. struct mmc_data *data = host->mrq->data;
  496. u32 *p = sg_virt(data->sg);
  497. int i;
  498. if (host->sd_error) {
  499. data->error = sh_mmcif_error_manage(host);
  500. return false;
  501. }
  502. for (i = 0; i < host->blocksize / 4; i++)
  503. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  504. /* buffer read end */
  505. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  506. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  507. return true;
  508. }
  509. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  510. struct mmc_request *mrq)
  511. {
  512. struct mmc_data *data = mrq->data;
  513. if (!data->sg_len || !data->sg->length)
  514. return;
  515. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  516. BLOCK_SIZE_MASK;
  517. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  518. host->sg_idx = 0;
  519. host->sg_blkidx = 0;
  520. host->pio_ptr = sg_virt(data->sg);
  521. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  522. }
  523. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  524. {
  525. struct mmc_data *data = host->mrq->data;
  526. u32 *p = host->pio_ptr;
  527. int i;
  528. if (host->sd_error) {
  529. data->error = sh_mmcif_error_manage(host);
  530. return false;
  531. }
  532. BUG_ON(!data->sg->length);
  533. for (i = 0; i < host->blocksize / 4; i++)
  534. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  535. if (!sh_mmcif_next_block(host, p))
  536. return false;
  537. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  538. return true;
  539. }
  540. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  541. struct mmc_request *mrq)
  542. {
  543. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  544. BLOCK_SIZE_MASK) + 3;
  545. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  546. /* buf write enable */
  547. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  548. }
  549. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  550. {
  551. struct mmc_data *data = host->mrq->data;
  552. u32 *p = sg_virt(data->sg);
  553. int i;
  554. if (host->sd_error) {
  555. data->error = sh_mmcif_error_manage(host);
  556. return false;
  557. }
  558. for (i = 0; i < host->blocksize / 4; i++)
  559. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  560. /* buffer write end */
  561. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  562. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  563. return true;
  564. }
  565. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  566. struct mmc_request *mrq)
  567. {
  568. struct mmc_data *data = mrq->data;
  569. if (!data->sg_len || !data->sg->length)
  570. return;
  571. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  572. BLOCK_SIZE_MASK;
  573. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  574. host->sg_idx = 0;
  575. host->sg_blkidx = 0;
  576. host->pio_ptr = sg_virt(data->sg);
  577. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  578. }
  579. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  580. {
  581. struct mmc_data *data = host->mrq->data;
  582. u32 *p = host->pio_ptr;
  583. int i;
  584. if (host->sd_error) {
  585. data->error = sh_mmcif_error_manage(host);
  586. return false;
  587. }
  588. BUG_ON(!data->sg->length);
  589. for (i = 0; i < host->blocksize / 4; i++)
  590. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  591. if (!sh_mmcif_next_block(host, p))
  592. return false;
  593. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  594. return true;
  595. }
  596. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  597. struct mmc_command *cmd)
  598. {
  599. if (cmd->flags & MMC_RSP_136) {
  600. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  601. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  602. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  603. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  604. } else
  605. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  606. }
  607. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  608. struct mmc_command *cmd)
  609. {
  610. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  611. }
  612. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  613. struct mmc_request *mrq)
  614. {
  615. struct mmc_data *data = mrq->data;
  616. struct mmc_command *cmd = mrq->cmd;
  617. u32 opc = cmd->opcode;
  618. u32 tmp = 0;
  619. /* Response Type check */
  620. switch (mmc_resp_type(cmd)) {
  621. case MMC_RSP_NONE:
  622. tmp |= CMD_SET_RTYP_NO;
  623. break;
  624. case MMC_RSP_R1:
  625. case MMC_RSP_R1B:
  626. case MMC_RSP_R3:
  627. tmp |= CMD_SET_RTYP_6B;
  628. break;
  629. case MMC_RSP_R2:
  630. tmp |= CMD_SET_RTYP_17B;
  631. break;
  632. default:
  633. dev_err(&host->pd->dev, "Unsupported response type.\n");
  634. break;
  635. }
  636. switch (opc) {
  637. /* RBSY */
  638. case MMC_SLEEP_AWAKE:
  639. case MMC_SWITCH:
  640. case MMC_STOP_TRANSMISSION:
  641. case MMC_SET_WRITE_PROT:
  642. case MMC_CLR_WRITE_PROT:
  643. case MMC_ERASE:
  644. tmp |= CMD_SET_RBSY;
  645. break;
  646. }
  647. /* WDAT / DATW */
  648. if (data) {
  649. tmp |= CMD_SET_WDAT;
  650. switch (host->bus_width) {
  651. case MMC_BUS_WIDTH_1:
  652. tmp |= CMD_SET_DATW_1;
  653. break;
  654. case MMC_BUS_WIDTH_4:
  655. tmp |= CMD_SET_DATW_4;
  656. break;
  657. case MMC_BUS_WIDTH_8:
  658. tmp |= CMD_SET_DATW_8;
  659. break;
  660. default:
  661. dev_err(&host->pd->dev, "Unsupported bus width.\n");
  662. break;
  663. }
  664. switch (host->timing) {
  665. case MMC_TIMING_UHS_DDR50:
  666. /*
  667. * MMC core will only set this timing, if the host
  668. * advertises the MMC_CAP_UHS_DDR50 capability. MMCIF
  669. * implementations with this capability, e.g. sh73a0,
  670. * will have to set it in their platform data.
  671. */
  672. tmp |= CMD_SET_DARS;
  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_SLEEP_AWAKE:
  729. case MMC_SWITCH:
  730. case MMC_STOP_TRANSMISSION:
  731. case MMC_SET_WRITE_PROT:
  732. case MMC_CLR_WRITE_PROT:
  733. case MMC_ERASE:
  734. mask = MASK_START_CMD | MASK_MRBSYE;
  735. break;
  736. default:
  737. mask = MASK_START_CMD | MASK_MCRSPE;
  738. break;
  739. }
  740. if (mrq->data) {
  741. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  742. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  743. mrq->data->blksz);
  744. }
  745. opc = sh_mmcif_set_cmd(host, mrq);
  746. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  747. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  748. /* set arg */
  749. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  750. /* set cmd */
  751. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  752. host->wait_for = MMCIF_WAIT_FOR_CMD;
  753. schedule_delayed_work(&host->timeout_work, host->timeout);
  754. }
  755. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  756. struct mmc_request *mrq)
  757. {
  758. switch (mrq->cmd->opcode) {
  759. case MMC_READ_MULTIPLE_BLOCK:
  760. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  761. break;
  762. case MMC_WRITE_MULTIPLE_BLOCK:
  763. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  764. break;
  765. default:
  766. dev_err(&host->pd->dev, "unsupported stop cmd\n");
  767. mrq->stop->error = sh_mmcif_error_manage(host);
  768. return;
  769. }
  770. host->wait_for = MMCIF_WAIT_FOR_STOP;
  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->timing = ios->timing;
  871. host->bus_width = ios->bus_width;
  872. host->state = STATE_IDLE;
  873. }
  874. static int sh_mmcif_get_cd(struct mmc_host *mmc)
  875. {
  876. struct sh_mmcif_host *host = mmc_priv(mmc);
  877. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  878. int ret = mmc_gpio_get_cd(mmc);
  879. if (ret >= 0)
  880. return ret;
  881. if (!p || !p->get_cd)
  882. return -ENOSYS;
  883. else
  884. return p->get_cd(host->pd);
  885. }
  886. static struct mmc_host_ops sh_mmcif_ops = {
  887. .request = sh_mmcif_request,
  888. .set_ios = sh_mmcif_set_ios,
  889. .get_cd = sh_mmcif_get_cd,
  890. };
  891. static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
  892. {
  893. struct mmc_command *cmd = host->mrq->cmd;
  894. struct mmc_data *data = host->mrq->data;
  895. long time;
  896. if (host->sd_error) {
  897. switch (cmd->opcode) {
  898. case MMC_ALL_SEND_CID:
  899. case MMC_SELECT_CARD:
  900. case MMC_APP_CMD:
  901. cmd->error = -ETIMEDOUT;
  902. host->sd_error = false;
  903. break;
  904. default:
  905. cmd->error = sh_mmcif_error_manage(host);
  906. dev_dbg(&host->pd->dev, "Cmd(d'%d) error %d\n",
  907. cmd->opcode, cmd->error);
  908. break;
  909. }
  910. return false;
  911. }
  912. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  913. cmd->error = 0;
  914. return false;
  915. }
  916. sh_mmcif_get_response(host, cmd);
  917. if (!data)
  918. return false;
  919. if (data->flags & MMC_DATA_READ) {
  920. if (host->chan_rx)
  921. sh_mmcif_start_dma_rx(host);
  922. } else {
  923. if (host->chan_tx)
  924. sh_mmcif_start_dma_tx(host);
  925. }
  926. if (!host->dma_active) {
  927. data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
  928. if (!data->error)
  929. return true;
  930. return false;
  931. }
  932. /* Running in the IRQ thread, can sleep */
  933. time = wait_for_completion_interruptible_timeout(&host->dma_complete,
  934. host->timeout);
  935. if (data->flags & MMC_DATA_READ)
  936. dma_unmap_sg(host->chan_rx->device->dev,
  937. data->sg, data->sg_len,
  938. DMA_FROM_DEVICE);
  939. else
  940. dma_unmap_sg(host->chan_tx->device->dev,
  941. data->sg, data->sg_len,
  942. DMA_TO_DEVICE);
  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. data->error = sh_mmcif_error_manage(host);
  948. } else if (!time) {
  949. data->error = -ETIMEDOUT;
  950. } else if (time < 0) {
  951. data->error = time;
  952. }
  953. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
  954. BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  955. host->dma_active = false;
  956. if (data->error) {
  957. data->bytes_xfered = 0;
  958. /* Abort DMA */
  959. if (data->flags & MMC_DATA_READ)
  960. dmaengine_terminate_all(host->chan_rx);
  961. else
  962. dmaengine_terminate_all(host->chan_tx);
  963. }
  964. return false;
  965. }
  966. static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
  967. {
  968. struct sh_mmcif_host *host = dev_id;
  969. struct mmc_request *mrq = host->mrq;
  970. bool wait = false;
  971. cancel_delayed_work_sync(&host->timeout_work);
  972. /*
  973. * All handlers return true, if processing continues, and false, if the
  974. * request has to be completed - successfully or not
  975. */
  976. switch (host->wait_for) {
  977. case MMCIF_WAIT_FOR_REQUEST:
  978. /* We're too late, the timeout has already kicked in */
  979. return IRQ_HANDLED;
  980. case MMCIF_WAIT_FOR_CMD:
  981. /* Wait for data? */
  982. wait = sh_mmcif_end_cmd(host);
  983. break;
  984. case MMCIF_WAIT_FOR_MREAD:
  985. /* Wait for more data? */
  986. wait = sh_mmcif_mread_block(host);
  987. break;
  988. case MMCIF_WAIT_FOR_READ:
  989. /* Wait for data end? */
  990. wait = sh_mmcif_read_block(host);
  991. break;
  992. case MMCIF_WAIT_FOR_MWRITE:
  993. /* Wait data to write? */
  994. wait = sh_mmcif_mwrite_block(host);
  995. break;
  996. case MMCIF_WAIT_FOR_WRITE:
  997. /* Wait for data end? */
  998. wait = sh_mmcif_write_block(host);
  999. break;
  1000. case MMCIF_WAIT_FOR_STOP:
  1001. if (host->sd_error) {
  1002. mrq->stop->error = sh_mmcif_error_manage(host);
  1003. break;
  1004. }
  1005. sh_mmcif_get_cmd12response(host, mrq->stop);
  1006. mrq->stop->error = 0;
  1007. break;
  1008. case MMCIF_WAIT_FOR_READ_END:
  1009. case MMCIF_WAIT_FOR_WRITE_END:
  1010. if (host->sd_error)
  1011. mrq->data->error = sh_mmcif_error_manage(host);
  1012. break;
  1013. default:
  1014. BUG();
  1015. }
  1016. if (wait) {
  1017. schedule_delayed_work(&host->timeout_work, host->timeout);
  1018. /* Wait for more data */
  1019. return IRQ_HANDLED;
  1020. }
  1021. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1022. struct mmc_data *data = mrq->data;
  1023. if (!mrq->cmd->error && data && !data->error)
  1024. data->bytes_xfered =
  1025. data->blocks * data->blksz;
  1026. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1027. sh_mmcif_stop_cmd(host, mrq);
  1028. if (!mrq->stop->error) {
  1029. schedule_delayed_work(&host->timeout_work, host->timeout);
  1030. return IRQ_HANDLED;
  1031. }
  1032. }
  1033. }
  1034. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1035. host->state = STATE_IDLE;
  1036. host->mrq = NULL;
  1037. mmc_request_done(host->mmc, mrq);
  1038. return IRQ_HANDLED;
  1039. }
  1040. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1041. {
  1042. struct sh_mmcif_host *host = dev_id;
  1043. u32 state;
  1044. int err = 0;
  1045. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1046. if (state & INT_ERR_STS) {
  1047. /* error interrupts - process first */
  1048. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
  1049. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  1050. err = 1;
  1051. } else if (state & INT_RBSYE) {
  1052. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1053. ~(INT_RBSYE | INT_CRSPE));
  1054. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE);
  1055. } else if (state & INT_CRSPE) {
  1056. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE);
  1057. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE);
  1058. } else if (state & INT_BUFREN) {
  1059. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN);
  1060. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  1061. } else if (state & INT_BUFWEN) {
  1062. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFWEN);
  1063. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  1064. } else if (state & INT_CMD12DRE) {
  1065. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1066. ~(INT_CMD12DRE | INT_CMD12RBE |
  1067. INT_CMD12CRE | INT_BUFRE));
  1068. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  1069. } else if (state & INT_BUFRE) {
  1070. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE);
  1071. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  1072. } else if (state & INT_DTRANE) {
  1073. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1074. ~(INT_CMD12DRE | INT_CMD12RBE |
  1075. INT_CMD12CRE | INT_DTRANE));
  1076. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  1077. } else if (state & INT_CMD12RBE) {
  1078. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1079. ~(INT_CMD12RBE | INT_CMD12CRE));
  1080. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  1081. } else {
  1082. dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x\n", state);
  1083. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
  1084. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  1085. err = 1;
  1086. }
  1087. if (err) {
  1088. host->sd_error = true;
  1089. dev_dbg(&host->pd->dev, "int err state = %08x\n", state);
  1090. }
  1091. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1092. if (!host->dma_active)
  1093. return IRQ_WAKE_THREAD;
  1094. else if (host->sd_error)
  1095. mmcif_dma_complete(host);
  1096. } else {
  1097. dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
  1098. }
  1099. return IRQ_HANDLED;
  1100. }
  1101. static void mmcif_timeout_work(struct work_struct *work)
  1102. {
  1103. struct delayed_work *d = container_of(work, struct delayed_work, work);
  1104. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1105. struct mmc_request *mrq = host->mrq;
  1106. if (host->dying)
  1107. /* Don't run after mmc_remove_host() */
  1108. return;
  1109. /*
  1110. * Handle races with cancel_delayed_work(), unless
  1111. * cancel_delayed_work_sync() is used
  1112. */
  1113. switch (host->wait_for) {
  1114. case MMCIF_WAIT_FOR_CMD:
  1115. mrq->cmd->error = sh_mmcif_error_manage(host);
  1116. break;
  1117. case MMCIF_WAIT_FOR_STOP:
  1118. mrq->stop->error = sh_mmcif_error_manage(host);
  1119. break;
  1120. case MMCIF_WAIT_FOR_MREAD:
  1121. case MMCIF_WAIT_FOR_MWRITE:
  1122. case MMCIF_WAIT_FOR_READ:
  1123. case MMCIF_WAIT_FOR_WRITE:
  1124. case MMCIF_WAIT_FOR_READ_END:
  1125. case MMCIF_WAIT_FOR_WRITE_END:
  1126. mrq->data->error = sh_mmcif_error_manage(host);
  1127. break;
  1128. default:
  1129. BUG();
  1130. }
  1131. host->state = STATE_IDLE;
  1132. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1133. host->mrq = NULL;
  1134. mmc_request_done(host->mmc, mrq);
  1135. }
  1136. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1137. {
  1138. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  1139. struct mmc_host *mmc = host->mmc;
  1140. mmc_regulator_get_supply(mmc);
  1141. if (!pd)
  1142. return;
  1143. if (!mmc->ocr_avail)
  1144. mmc->ocr_avail = pd->ocr;
  1145. else if (pd->ocr)
  1146. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1147. }
  1148. static int sh_mmcif_probe(struct platform_device *pdev)
  1149. {
  1150. int ret = 0, irq[2];
  1151. struct mmc_host *mmc;
  1152. struct sh_mmcif_host *host;
  1153. struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
  1154. struct resource *res;
  1155. void __iomem *reg;
  1156. const char *name;
  1157. irq[0] = platform_get_irq(pdev, 0);
  1158. irq[1] = platform_get_irq(pdev, 1);
  1159. if (irq[0] < 0) {
  1160. dev_err(&pdev->dev, "Get irq error\n");
  1161. return -ENXIO;
  1162. }
  1163. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1164. if (!res) {
  1165. dev_err(&pdev->dev, "platform_get_resource error.\n");
  1166. return -ENXIO;
  1167. }
  1168. reg = ioremap(res->start, resource_size(res));
  1169. if (!reg) {
  1170. dev_err(&pdev->dev, "ioremap error.\n");
  1171. return -ENOMEM;
  1172. }
  1173. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  1174. if (!mmc) {
  1175. ret = -ENOMEM;
  1176. goto ealloch;
  1177. }
  1178. host = mmc_priv(mmc);
  1179. host->mmc = mmc;
  1180. host->addr = reg;
  1181. host->timeout = msecs_to_jiffies(1000);
  1182. host->pd = pdev;
  1183. spin_lock_init(&host->lock);
  1184. mmc->ops = &sh_mmcif_ops;
  1185. sh_mmcif_init_ocr(host);
  1186. mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1187. if (pd && pd->caps)
  1188. mmc->caps |= pd->caps;
  1189. mmc->max_segs = 32;
  1190. mmc->max_blk_size = 512;
  1191. mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
  1192. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1193. mmc->max_seg_size = mmc->max_req_size;
  1194. platform_set_drvdata(pdev, host);
  1195. pm_runtime_enable(&pdev->dev);
  1196. host->power = false;
  1197. host->hclk = clk_get(&pdev->dev, NULL);
  1198. if (IS_ERR(host->hclk)) {
  1199. ret = PTR_ERR(host->hclk);
  1200. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  1201. goto eclkget;
  1202. }
  1203. ret = sh_mmcif_clk_update(host);
  1204. if (ret < 0)
  1205. goto eclkupdate;
  1206. ret = pm_runtime_resume(&pdev->dev);
  1207. if (ret < 0)
  1208. goto eresume;
  1209. INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
  1210. sh_mmcif_sync_reset(host);
  1211. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1212. name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
  1213. ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, name, host);
  1214. if (ret) {
  1215. dev_err(&pdev->dev, "request_irq error (%s)\n", name);
  1216. goto ereqirq0;
  1217. }
  1218. if (irq[1] >= 0) {
  1219. ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt,
  1220. 0, "sh_mmc:int", host);
  1221. if (ret) {
  1222. dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
  1223. goto ereqirq1;
  1224. }
  1225. }
  1226. if (pd && pd->use_cd_gpio) {
  1227. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio);
  1228. if (ret < 0)
  1229. goto erqcd;
  1230. }
  1231. clk_disable(host->hclk);
  1232. ret = mmc_add_host(mmc);
  1233. if (ret < 0)
  1234. goto emmcaddh;
  1235. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1236. dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
  1237. dev_dbg(&pdev->dev, "chip ver H'%04x\n",
  1238. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
  1239. return ret;
  1240. emmcaddh:
  1241. erqcd:
  1242. if (irq[1] >= 0)
  1243. free_irq(irq[1], host);
  1244. ereqirq1:
  1245. free_irq(irq[0], host);
  1246. ereqirq0:
  1247. pm_runtime_suspend(&pdev->dev);
  1248. eresume:
  1249. clk_disable(host->hclk);
  1250. eclkupdate:
  1251. clk_put(host->hclk);
  1252. eclkget:
  1253. pm_runtime_disable(&pdev->dev);
  1254. mmc_free_host(mmc);
  1255. ealloch:
  1256. iounmap(reg);
  1257. return ret;
  1258. }
  1259. static int sh_mmcif_remove(struct platform_device *pdev)
  1260. {
  1261. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1262. int irq[2];
  1263. host->dying = true;
  1264. clk_enable(host->hclk);
  1265. pm_runtime_get_sync(&pdev->dev);
  1266. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1267. mmc_remove_host(host->mmc);
  1268. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1269. /*
  1270. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1271. * mmc_remove_host() call above. But swapping order doesn't help either
  1272. * (a query on the linux-mmc mailing list didn't bring any replies).
  1273. */
  1274. cancel_delayed_work_sync(&host->timeout_work);
  1275. if (host->addr)
  1276. iounmap(host->addr);
  1277. irq[0] = platform_get_irq(pdev, 0);
  1278. irq[1] = platform_get_irq(pdev, 1);
  1279. free_irq(irq[0], host);
  1280. if (irq[1] >= 0)
  1281. free_irq(irq[1], host);
  1282. platform_set_drvdata(pdev, NULL);
  1283. clk_disable(host->hclk);
  1284. mmc_free_host(host->mmc);
  1285. pm_runtime_put_sync(&pdev->dev);
  1286. pm_runtime_disable(&pdev->dev);
  1287. return 0;
  1288. }
  1289. #ifdef CONFIG_PM
  1290. static int sh_mmcif_suspend(struct device *dev)
  1291. {
  1292. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1293. int ret = mmc_suspend_host(host->mmc);
  1294. if (!ret)
  1295. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1296. return ret;
  1297. }
  1298. static int sh_mmcif_resume(struct device *dev)
  1299. {
  1300. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1301. return mmc_resume_host(host->mmc);
  1302. }
  1303. #else
  1304. #define sh_mmcif_suspend NULL
  1305. #define sh_mmcif_resume NULL
  1306. #endif /* CONFIG_PM */
  1307. static const struct of_device_id mmcif_of_match[] = {
  1308. { .compatible = "renesas,sh-mmcif" },
  1309. { }
  1310. };
  1311. MODULE_DEVICE_TABLE(of, mmcif_of_match);
  1312. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1313. .suspend = sh_mmcif_suspend,
  1314. .resume = sh_mmcif_resume,
  1315. };
  1316. static struct platform_driver sh_mmcif_driver = {
  1317. .probe = sh_mmcif_probe,
  1318. .remove = sh_mmcif_remove,
  1319. .driver = {
  1320. .name = DRIVER_NAME,
  1321. .pm = &sh_mmcif_dev_pm_ops,
  1322. .owner = THIS_MODULE,
  1323. .of_match_table = mmcif_of_match,
  1324. },
  1325. };
  1326. module_platform_driver(sh_mmcif_driver);
  1327. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1328. MODULE_LICENSE("GPL");
  1329. MODULE_ALIAS("platform:" DRIVER_NAME);
  1330. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");