sh_mmcif.c 43 KB

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