sh_mmcif.c 41 KB

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