sh_mmcif.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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. unsigned int slave_id;
  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_id = direction == DMA_MEM_TO_DEV
  363. ? pdata->slave_id_tx : pdata->slave_id_rx;
  364. else
  365. slave_id = 0;
  366. chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  367. (void *)(unsigned long)slave_id, &host->pd->dev,
  368. direction == DMA_MEM_TO_DEV ? "tx" : "rx");
  369. dev_dbg(&host->pd->dev, "%s: %s: got channel %p\n", __func__,
  370. direction == DMA_MEM_TO_DEV ? "TX" : "RX", chan);
  371. if (!chan)
  372. return NULL;
  373. res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
  374. /* In the OF case the driver will get the slave ID from the DT */
  375. cfg.slave_id = slave_id;
  376. cfg.direction = direction;
  377. if (direction == DMA_DEV_TO_MEM) {
  378. cfg.src_addr = res->start + MMCIF_CE_DATA;
  379. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  380. } else {
  381. cfg.dst_addr = res->start + MMCIF_CE_DATA;
  382. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  383. }
  384. ret = dmaengine_slave_config(chan, &cfg);
  385. if (ret < 0) {
  386. dma_release_channel(chan);
  387. return NULL;
  388. }
  389. return chan;
  390. }
  391. static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
  392. struct sh_mmcif_plat_data *pdata)
  393. {
  394. host->dma_active = false;
  395. if (pdata) {
  396. if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
  397. return;
  398. } else if (!host->pd->dev.of_node) {
  399. return;
  400. }
  401. /* We can only either use DMA for both Tx and Rx or not use it at all */
  402. host->chan_tx = sh_mmcif_request_dma_one(host, pdata, DMA_MEM_TO_DEV);
  403. if (!host->chan_tx)
  404. return;
  405. host->chan_rx = sh_mmcif_request_dma_one(host, pdata, DMA_DEV_TO_MEM);
  406. if (!host->chan_rx) {
  407. dma_release_channel(host->chan_tx);
  408. host->chan_tx = NULL;
  409. }
  410. }
  411. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  412. {
  413. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  414. /* Descriptors are freed automatically */
  415. if (host->chan_tx) {
  416. struct dma_chan *chan = host->chan_tx;
  417. host->chan_tx = NULL;
  418. dma_release_channel(chan);
  419. }
  420. if (host->chan_rx) {
  421. struct dma_chan *chan = host->chan_rx;
  422. host->chan_rx = NULL;
  423. dma_release_channel(chan);
  424. }
  425. host->dma_active = false;
  426. }
  427. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  428. {
  429. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  430. bool sup_pclk = p ? p->sup_pclk : false;
  431. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  432. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  433. if (!clk)
  434. return;
  435. if (sup_pclk && clk == host->clk)
  436. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  437. else
  438. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  439. ((fls(DIV_ROUND_UP(host->clk,
  440. clk) - 1) - 1) << 16));
  441. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  442. }
  443. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  444. {
  445. u32 tmp;
  446. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  447. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  448. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  449. if (host->ccs_enable)
  450. tmp |= SCCSTO_29;
  451. if (host->clk_ctrl2_enable)
  452. sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL2, 0x0F0F0000);
  453. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  454. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29);
  455. /* byte swap on */
  456. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  457. }
  458. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  459. {
  460. u32 state1, state2;
  461. int ret, timeout;
  462. host->sd_error = false;
  463. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  464. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  465. dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
  466. dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
  467. if (state1 & STS1_CMDSEQ) {
  468. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  469. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  470. for (timeout = 10000000; timeout; timeout--) {
  471. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  472. & STS1_CMDSEQ))
  473. break;
  474. mdelay(1);
  475. }
  476. if (!timeout) {
  477. dev_err(&host->pd->dev,
  478. "Forced end of command sequence timeout err\n");
  479. return -EIO;
  480. }
  481. sh_mmcif_sync_reset(host);
  482. dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
  483. return -EIO;
  484. }
  485. if (state2 & STS2_CRC_ERR) {
  486. dev_err(&host->pd->dev, " CRC error: state %u, wait %u\n",
  487. host->state, host->wait_for);
  488. ret = -EIO;
  489. } else if (state2 & STS2_TIMEOUT_ERR) {
  490. dev_err(&host->pd->dev, " Timeout: state %u, wait %u\n",
  491. host->state, host->wait_for);
  492. ret = -ETIMEDOUT;
  493. } else {
  494. dev_dbg(&host->pd->dev, " End/Index error: state %u, wait %u\n",
  495. host->state, host->wait_for);
  496. ret = -EIO;
  497. }
  498. return ret;
  499. }
  500. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  501. {
  502. struct mmc_data *data = host->mrq->data;
  503. host->sg_blkidx += host->blocksize;
  504. /* data->sg->length must be a multiple of host->blocksize? */
  505. BUG_ON(host->sg_blkidx > data->sg->length);
  506. if (host->sg_blkidx == data->sg->length) {
  507. host->sg_blkidx = 0;
  508. if (++host->sg_idx < data->sg_len)
  509. host->pio_ptr = sg_virt(++data->sg);
  510. } else {
  511. host->pio_ptr = p;
  512. }
  513. return host->sg_idx != data->sg_len;
  514. }
  515. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  516. struct mmc_request *mrq)
  517. {
  518. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  519. BLOCK_SIZE_MASK) + 3;
  520. host->wait_for = MMCIF_WAIT_FOR_READ;
  521. /* buf read enable */
  522. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  523. }
  524. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  525. {
  526. struct mmc_data *data = host->mrq->data;
  527. u32 *p = sg_virt(data->sg);
  528. int i;
  529. if (host->sd_error) {
  530. data->error = sh_mmcif_error_manage(host);
  531. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  532. return false;
  533. }
  534. for (i = 0; i < host->blocksize / 4; i++)
  535. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  536. /* buffer read end */
  537. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  538. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  539. return true;
  540. }
  541. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  542. struct mmc_request *mrq)
  543. {
  544. struct mmc_data *data = mrq->data;
  545. if (!data->sg_len || !data->sg->length)
  546. return;
  547. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  548. BLOCK_SIZE_MASK;
  549. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  550. host->sg_idx = 0;
  551. host->sg_blkidx = 0;
  552. host->pio_ptr = sg_virt(data->sg);
  553. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  554. }
  555. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  556. {
  557. struct mmc_data *data = host->mrq->data;
  558. u32 *p = host->pio_ptr;
  559. int i;
  560. if (host->sd_error) {
  561. data->error = sh_mmcif_error_manage(host);
  562. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  563. return false;
  564. }
  565. BUG_ON(!data->sg->length);
  566. for (i = 0; i < host->blocksize / 4; i++)
  567. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  568. if (!sh_mmcif_next_block(host, p))
  569. return false;
  570. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  571. return true;
  572. }
  573. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  574. struct mmc_request *mrq)
  575. {
  576. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  577. BLOCK_SIZE_MASK) + 3;
  578. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  579. /* buf write enable */
  580. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  581. }
  582. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  583. {
  584. struct mmc_data *data = host->mrq->data;
  585. u32 *p = sg_virt(data->sg);
  586. int i;
  587. if (host->sd_error) {
  588. data->error = sh_mmcif_error_manage(host);
  589. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  590. return false;
  591. }
  592. for (i = 0; i < host->blocksize / 4; i++)
  593. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  594. /* buffer write end */
  595. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  596. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  597. return true;
  598. }
  599. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  600. struct mmc_request *mrq)
  601. {
  602. struct mmc_data *data = mrq->data;
  603. if (!data->sg_len || !data->sg->length)
  604. return;
  605. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  606. BLOCK_SIZE_MASK;
  607. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  608. host->sg_idx = 0;
  609. host->sg_blkidx = 0;
  610. host->pio_ptr = sg_virt(data->sg);
  611. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  612. }
  613. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  614. {
  615. struct mmc_data *data = host->mrq->data;
  616. u32 *p = host->pio_ptr;
  617. int i;
  618. if (host->sd_error) {
  619. data->error = sh_mmcif_error_manage(host);
  620. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  621. return false;
  622. }
  623. BUG_ON(!data->sg->length);
  624. for (i = 0; i < host->blocksize / 4; i++)
  625. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  626. if (!sh_mmcif_next_block(host, p))
  627. return false;
  628. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  629. return true;
  630. }
  631. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  632. struct mmc_command *cmd)
  633. {
  634. if (cmd->flags & MMC_RSP_136) {
  635. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  636. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  637. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  638. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  639. } else
  640. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  641. }
  642. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  643. struct mmc_command *cmd)
  644. {
  645. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  646. }
  647. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  648. struct mmc_request *mrq)
  649. {
  650. struct mmc_data *data = mrq->data;
  651. struct mmc_command *cmd = mrq->cmd;
  652. u32 opc = cmd->opcode;
  653. u32 tmp = 0;
  654. /* Response Type check */
  655. switch (mmc_resp_type(cmd)) {
  656. case MMC_RSP_NONE:
  657. tmp |= CMD_SET_RTYP_NO;
  658. break;
  659. case MMC_RSP_R1:
  660. case MMC_RSP_R1B:
  661. case MMC_RSP_R3:
  662. tmp |= CMD_SET_RTYP_6B;
  663. break;
  664. case MMC_RSP_R2:
  665. tmp |= CMD_SET_RTYP_17B;
  666. break;
  667. default:
  668. dev_err(&host->pd->dev, "Unsupported response type.\n");
  669. break;
  670. }
  671. switch (opc) {
  672. /* RBSY */
  673. case MMC_SLEEP_AWAKE:
  674. case MMC_SWITCH:
  675. case MMC_STOP_TRANSMISSION:
  676. case MMC_SET_WRITE_PROT:
  677. case MMC_CLR_WRITE_PROT:
  678. case MMC_ERASE:
  679. tmp |= CMD_SET_RBSY;
  680. break;
  681. }
  682. /* WDAT / DATW */
  683. if (data) {
  684. tmp |= CMD_SET_WDAT;
  685. switch (host->bus_width) {
  686. case MMC_BUS_WIDTH_1:
  687. tmp |= CMD_SET_DATW_1;
  688. break;
  689. case MMC_BUS_WIDTH_4:
  690. tmp |= CMD_SET_DATW_4;
  691. break;
  692. case MMC_BUS_WIDTH_8:
  693. tmp |= CMD_SET_DATW_8;
  694. break;
  695. default:
  696. dev_err(&host->pd->dev, "Unsupported bus width.\n");
  697. break;
  698. }
  699. switch (host->timing) {
  700. case MMC_TIMING_MMC_DDR52:
  701. /*
  702. * MMC core will only set this timing, if the host
  703. * advertises the MMC_CAP_1_8V_DDR/MMC_CAP_1_2V_DDR
  704. * capability. MMCIF implementations with this
  705. * capability, e.g. sh73a0, will have to set it
  706. * in their platform data.
  707. */
  708. tmp |= CMD_SET_DARS;
  709. break;
  710. }
  711. }
  712. /* DWEN */
  713. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  714. tmp |= CMD_SET_DWEN;
  715. /* CMLTE/CMD12EN */
  716. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  717. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  718. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  719. data->blocks << 16);
  720. }
  721. /* RIDXC[1:0] check bits */
  722. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  723. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  724. tmp |= CMD_SET_RIDXC_BITS;
  725. /* RCRC7C[1:0] check bits */
  726. if (opc == MMC_SEND_OP_COND)
  727. tmp |= CMD_SET_CRC7C_BITS;
  728. /* RCRC7C[1:0] internal CRC7 */
  729. if (opc == MMC_ALL_SEND_CID ||
  730. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  731. tmp |= CMD_SET_CRC7C_INTERNAL;
  732. return (opc << 24) | tmp;
  733. }
  734. static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
  735. struct mmc_request *mrq, u32 opc)
  736. {
  737. switch (opc) {
  738. case MMC_READ_MULTIPLE_BLOCK:
  739. sh_mmcif_multi_read(host, mrq);
  740. return 0;
  741. case MMC_WRITE_MULTIPLE_BLOCK:
  742. sh_mmcif_multi_write(host, mrq);
  743. return 0;
  744. case MMC_WRITE_BLOCK:
  745. sh_mmcif_single_write(host, mrq);
  746. return 0;
  747. case MMC_READ_SINGLE_BLOCK:
  748. case MMC_SEND_EXT_CSD:
  749. sh_mmcif_single_read(host, mrq);
  750. return 0;
  751. default:
  752. dev_err(&host->pd->dev, "Unsupported CMD%d\n", opc);
  753. return -EINVAL;
  754. }
  755. }
  756. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  757. struct mmc_request *mrq)
  758. {
  759. struct mmc_command *cmd = mrq->cmd;
  760. u32 opc = cmd->opcode;
  761. u32 mask;
  762. switch (opc) {
  763. /* response busy check */
  764. case MMC_SLEEP_AWAKE:
  765. case MMC_SWITCH:
  766. case MMC_STOP_TRANSMISSION:
  767. case MMC_SET_WRITE_PROT:
  768. case MMC_CLR_WRITE_PROT:
  769. case MMC_ERASE:
  770. mask = MASK_START_CMD | MASK_MRBSYE;
  771. break;
  772. default:
  773. mask = MASK_START_CMD | MASK_MCRSPE;
  774. break;
  775. }
  776. if (host->ccs_enable)
  777. mask |= MASK_MCCSTO;
  778. if (mrq->data) {
  779. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  780. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  781. mrq->data->blksz);
  782. }
  783. opc = sh_mmcif_set_cmd(host, mrq);
  784. if (host->ccs_enable)
  785. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  786. else
  787. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0 | INT_CCS);
  788. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  789. /* set arg */
  790. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  791. /* set cmd */
  792. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  793. host->wait_for = MMCIF_WAIT_FOR_CMD;
  794. schedule_delayed_work(&host->timeout_work, host->timeout);
  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. cancel_delayed_work_sync(&host->timeout_work);
  1018. mutex_lock(&host->thread_lock);
  1019. mrq = host->mrq;
  1020. if (!mrq) {
  1021. dev_dbg(&host->pd->dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
  1022. host->state, host->wait_for);
  1023. mutex_unlock(&host->thread_lock);
  1024. return IRQ_HANDLED;
  1025. }
  1026. /*
  1027. * All handlers return true, if processing continues, and false, if the
  1028. * request has to be completed - successfully or not
  1029. */
  1030. switch (host->wait_for) {
  1031. case MMCIF_WAIT_FOR_REQUEST:
  1032. /* We're too late, the timeout has already kicked in */
  1033. mutex_unlock(&host->thread_lock);
  1034. return IRQ_HANDLED;
  1035. case MMCIF_WAIT_FOR_CMD:
  1036. /* Wait for data? */
  1037. wait = sh_mmcif_end_cmd(host);
  1038. break;
  1039. case MMCIF_WAIT_FOR_MREAD:
  1040. /* Wait for more data? */
  1041. wait = sh_mmcif_mread_block(host);
  1042. break;
  1043. case MMCIF_WAIT_FOR_READ:
  1044. /* Wait for data end? */
  1045. wait = sh_mmcif_read_block(host);
  1046. break;
  1047. case MMCIF_WAIT_FOR_MWRITE:
  1048. /* Wait data to write? */
  1049. wait = sh_mmcif_mwrite_block(host);
  1050. break;
  1051. case MMCIF_WAIT_FOR_WRITE:
  1052. /* Wait for data end? */
  1053. wait = sh_mmcif_write_block(host);
  1054. break;
  1055. case MMCIF_WAIT_FOR_STOP:
  1056. if (host->sd_error) {
  1057. mrq->stop->error = sh_mmcif_error_manage(host);
  1058. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->stop->error);
  1059. break;
  1060. }
  1061. sh_mmcif_get_cmd12response(host, mrq->stop);
  1062. mrq->stop->error = 0;
  1063. break;
  1064. case MMCIF_WAIT_FOR_READ_END:
  1065. case MMCIF_WAIT_FOR_WRITE_END:
  1066. if (host->sd_error) {
  1067. mrq->data->error = sh_mmcif_error_manage(host);
  1068. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->data->error);
  1069. }
  1070. break;
  1071. default:
  1072. BUG();
  1073. }
  1074. if (wait) {
  1075. schedule_delayed_work(&host->timeout_work, host->timeout);
  1076. /* Wait for more data */
  1077. mutex_unlock(&host->thread_lock);
  1078. return IRQ_HANDLED;
  1079. }
  1080. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1081. struct mmc_data *data = mrq->data;
  1082. if (!mrq->cmd->error && data && !data->error)
  1083. data->bytes_xfered =
  1084. data->blocks * data->blksz;
  1085. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1086. sh_mmcif_stop_cmd(host, mrq);
  1087. if (!mrq->stop->error) {
  1088. schedule_delayed_work(&host->timeout_work, host->timeout);
  1089. mutex_unlock(&host->thread_lock);
  1090. return IRQ_HANDLED;
  1091. }
  1092. }
  1093. }
  1094. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1095. host->state = STATE_IDLE;
  1096. host->mrq = NULL;
  1097. mmc_request_done(host->mmc, mrq);
  1098. mutex_unlock(&host->thread_lock);
  1099. return IRQ_HANDLED;
  1100. }
  1101. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1102. {
  1103. struct sh_mmcif_host *host = dev_id;
  1104. u32 state, mask;
  1105. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1106. mask = sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK);
  1107. if (host->ccs_enable)
  1108. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(state & mask));
  1109. else
  1110. sh_mmcif_writel(host->addr, MMCIF_CE_INT, INT_CCS | ~(state & mask));
  1111. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN);
  1112. if (state & ~MASK_CLEAN)
  1113. dev_dbg(&host->pd->dev, "IRQ state = 0x%08x incompletely cleared\n",
  1114. state);
  1115. if (state & INT_ERR_STS || state & ~INT_ALL) {
  1116. host->sd_error = true;
  1117. dev_dbg(&host->pd->dev, "int err state = 0x%08x\n", state);
  1118. }
  1119. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1120. if (!host->mrq)
  1121. dev_dbg(&host->pd->dev, "NULL IRQ state = 0x%08x\n", state);
  1122. if (!host->dma_active)
  1123. return IRQ_WAKE_THREAD;
  1124. else if (host->sd_error)
  1125. mmcif_dma_complete(host);
  1126. } else {
  1127. dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
  1128. }
  1129. return IRQ_HANDLED;
  1130. }
  1131. static void mmcif_timeout_work(struct work_struct *work)
  1132. {
  1133. struct delayed_work *d = container_of(work, struct delayed_work, work);
  1134. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1135. struct mmc_request *mrq = host->mrq;
  1136. unsigned long flags;
  1137. if (host->dying)
  1138. /* Don't run after mmc_remove_host() */
  1139. return;
  1140. dev_err(&host->pd->dev, "Timeout waiting for %u on CMD%u\n",
  1141. host->wait_for, mrq->cmd->opcode);
  1142. spin_lock_irqsave(&host->lock, flags);
  1143. if (host->state == STATE_IDLE) {
  1144. spin_unlock_irqrestore(&host->lock, flags);
  1145. return;
  1146. }
  1147. host->state = STATE_TIMEOUT;
  1148. spin_unlock_irqrestore(&host->lock, flags);
  1149. /*
  1150. * Handle races with cancel_delayed_work(), unless
  1151. * cancel_delayed_work_sync() is used
  1152. */
  1153. switch (host->wait_for) {
  1154. case MMCIF_WAIT_FOR_CMD:
  1155. mrq->cmd->error = sh_mmcif_error_manage(host);
  1156. break;
  1157. case MMCIF_WAIT_FOR_STOP:
  1158. mrq->stop->error = sh_mmcif_error_manage(host);
  1159. break;
  1160. case MMCIF_WAIT_FOR_MREAD:
  1161. case MMCIF_WAIT_FOR_MWRITE:
  1162. case MMCIF_WAIT_FOR_READ:
  1163. case MMCIF_WAIT_FOR_WRITE:
  1164. case MMCIF_WAIT_FOR_READ_END:
  1165. case MMCIF_WAIT_FOR_WRITE_END:
  1166. mrq->data->error = sh_mmcif_error_manage(host);
  1167. break;
  1168. default:
  1169. BUG();
  1170. }
  1171. host->state = STATE_IDLE;
  1172. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1173. host->mrq = NULL;
  1174. mmc_request_done(host->mmc, mrq);
  1175. }
  1176. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1177. {
  1178. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  1179. struct mmc_host *mmc = host->mmc;
  1180. mmc_regulator_get_supply(mmc);
  1181. if (!pd)
  1182. return;
  1183. if (!mmc->ocr_avail)
  1184. mmc->ocr_avail = pd->ocr;
  1185. else if (pd->ocr)
  1186. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1187. }
  1188. static int sh_mmcif_probe(struct platform_device *pdev)
  1189. {
  1190. int ret = 0, irq[2];
  1191. struct mmc_host *mmc;
  1192. struct sh_mmcif_host *host;
  1193. struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
  1194. struct resource *res;
  1195. void __iomem *reg;
  1196. const char *name;
  1197. irq[0] = platform_get_irq(pdev, 0);
  1198. irq[1] = platform_get_irq(pdev, 1);
  1199. if (irq[0] < 0) {
  1200. dev_err(&pdev->dev, "Get irq error\n");
  1201. return -ENXIO;
  1202. }
  1203. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1204. reg = devm_ioremap_resource(&pdev->dev, res);
  1205. if (IS_ERR(reg))
  1206. return PTR_ERR(reg);
  1207. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  1208. if (!mmc)
  1209. return -ENOMEM;
  1210. ret = mmc_of_parse(mmc);
  1211. if (ret < 0)
  1212. goto err_host;
  1213. host = mmc_priv(mmc);
  1214. host->mmc = mmc;
  1215. host->addr = reg;
  1216. host->timeout = msecs_to_jiffies(1000);
  1217. host->ccs_enable = !pd || !pd->ccs_unsupported;
  1218. host->clk_ctrl2_enable = pd && pd->clk_ctrl2_present;
  1219. host->pd = pdev;
  1220. spin_lock_init(&host->lock);
  1221. mmc->ops = &sh_mmcif_ops;
  1222. sh_mmcif_init_ocr(host);
  1223. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1224. if (pd && pd->caps)
  1225. mmc->caps |= pd->caps;
  1226. mmc->max_segs = 32;
  1227. mmc->max_blk_size = 512;
  1228. mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
  1229. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1230. mmc->max_seg_size = mmc->max_req_size;
  1231. platform_set_drvdata(pdev, host);
  1232. pm_runtime_enable(&pdev->dev);
  1233. host->power = false;
  1234. host->hclk = devm_clk_get(&pdev->dev, NULL);
  1235. if (IS_ERR(host->hclk)) {
  1236. ret = PTR_ERR(host->hclk);
  1237. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  1238. goto err_pm;
  1239. }
  1240. ret = sh_mmcif_clk_update(host);
  1241. if (ret < 0)
  1242. goto err_pm;
  1243. ret = pm_runtime_resume(&pdev->dev);
  1244. if (ret < 0)
  1245. goto err_clk;
  1246. INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
  1247. sh_mmcif_sync_reset(host);
  1248. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1249. name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
  1250. ret = devm_request_threaded_irq(&pdev->dev, irq[0], sh_mmcif_intr,
  1251. sh_mmcif_irqt, 0, name, host);
  1252. if (ret) {
  1253. dev_err(&pdev->dev, "request_irq error (%s)\n", name);
  1254. goto err_clk;
  1255. }
  1256. if (irq[1] >= 0) {
  1257. ret = devm_request_threaded_irq(&pdev->dev, irq[1],
  1258. sh_mmcif_intr, sh_mmcif_irqt,
  1259. 0, "sh_mmc:int", host);
  1260. if (ret) {
  1261. dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
  1262. goto err_clk;
  1263. }
  1264. }
  1265. if (pd && pd->use_cd_gpio) {
  1266. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0);
  1267. if (ret < 0)
  1268. goto err_clk;
  1269. }
  1270. mutex_init(&host->thread_lock);
  1271. ret = mmc_add_host(mmc);
  1272. if (ret < 0)
  1273. goto err_clk;
  1274. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1275. dev_info(&pdev->dev, "Chip version 0x%04x, clock rate %luMHz\n",
  1276. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0xffff,
  1277. clk_get_rate(host->hclk) / 1000000UL);
  1278. clk_disable_unprepare(host->hclk);
  1279. return ret;
  1280. err_clk:
  1281. clk_disable_unprepare(host->hclk);
  1282. err_pm:
  1283. pm_runtime_disable(&pdev->dev);
  1284. err_host:
  1285. mmc_free_host(mmc);
  1286. return ret;
  1287. }
  1288. static int sh_mmcif_remove(struct platform_device *pdev)
  1289. {
  1290. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1291. host->dying = true;
  1292. clk_prepare_enable(host->hclk);
  1293. pm_runtime_get_sync(&pdev->dev);
  1294. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1295. mmc_remove_host(host->mmc);
  1296. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1297. /*
  1298. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1299. * mmc_remove_host() call above. But swapping order doesn't help either
  1300. * (a query on the linux-mmc mailing list didn't bring any replies).
  1301. */
  1302. cancel_delayed_work_sync(&host->timeout_work);
  1303. clk_disable_unprepare(host->hclk);
  1304. mmc_free_host(host->mmc);
  1305. pm_runtime_put_sync(&pdev->dev);
  1306. pm_runtime_disable(&pdev->dev);
  1307. return 0;
  1308. }
  1309. #ifdef CONFIG_PM_SLEEP
  1310. static int sh_mmcif_suspend(struct device *dev)
  1311. {
  1312. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1313. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1314. return 0;
  1315. }
  1316. static int sh_mmcif_resume(struct device *dev)
  1317. {
  1318. return 0;
  1319. }
  1320. #endif
  1321. static const struct of_device_id mmcif_of_match[] = {
  1322. { .compatible = "renesas,sh-mmcif" },
  1323. { }
  1324. };
  1325. MODULE_DEVICE_TABLE(of, mmcif_of_match);
  1326. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1327. SET_SYSTEM_SLEEP_PM_OPS(sh_mmcif_suspend, sh_mmcif_resume)
  1328. };
  1329. static struct platform_driver sh_mmcif_driver = {
  1330. .probe = sh_mmcif_probe,
  1331. .remove = sh_mmcif_remove,
  1332. .driver = {
  1333. .name = DRIVER_NAME,
  1334. .pm = &sh_mmcif_dev_pm_ops,
  1335. .of_match_table = mmcif_of_match,
  1336. },
  1337. };
  1338. module_platform_driver(sh_mmcif_driver);
  1339. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1340. MODULE_LICENSE("GPL");
  1341. MODULE_ALIAS("platform:" DRIVER_NAME);
  1342. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");