sh_mmcif.c 41 KB

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