sh_mmcif.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*
  2. * MMCIF eMMC driver.
  3. *
  4. * Copyright (C) 2010 Renesas Solutions Corp.
  5. * Yusuke Goda <yusuke.goda.sx@renesas.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. *
  12. * TODO
  13. * 1. DMA
  14. * 2. Power management
  15. * 3. Handle MMC errors better
  16. *
  17. */
  18. /*
  19. * The MMCIF driver is now processing MMC requests asynchronously, according
  20. * to the Linux MMC API requirement.
  21. *
  22. * The MMCIF driver processes MMC requests in up to 3 stages: command, optional
  23. * data, and optional stop. To achieve asynchronous processing each of these
  24. * stages is split into two halves: a top and a bottom half. The top half
  25. * initialises the hardware, installs a timeout handler to handle completion
  26. * timeouts, and returns. In case of the command stage this immediately returns
  27. * control to the caller, leaving all further processing to run asynchronously.
  28. * All further request processing is performed by the bottom halves.
  29. *
  30. * The bottom half further consists of a "hard" IRQ handler, an IRQ handler
  31. * thread, a DMA completion callback, if DMA is used, a timeout work, and
  32. * request- and stage-specific handler methods.
  33. *
  34. * Each bottom half run begins with either a hardware interrupt, a DMA callback
  35. * invocation, or a timeout work run. In case of an error or a successful
  36. * processing completion, the MMC core is informed and the request processing is
  37. * finished. In case processing has to continue, i.e., if data has to be read
  38. * from or written to the card, or if a stop command has to be sent, the next
  39. * top half is called, which performs the necessary hardware handling and
  40. * reschedules the timeout work. This returns the driver state machine into the
  41. * bottom half waiting state.
  42. */
  43. #include <linux/bitops.h>
  44. #include <linux/clk.h>
  45. #include <linux/completion.h>
  46. #include <linux/delay.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/dmaengine.h>
  49. #include <linux/mmc/card.h>
  50. #include <linux/mmc/core.h>
  51. #include <linux/mmc/host.h>
  52. #include <linux/mmc/mmc.h>
  53. #include <linux/mmc/sdio.h>
  54. #include <linux/mmc/sh_mmcif.h>
  55. #include <linux/mmc/slot-gpio.h>
  56. #include <linux/mod_devicetable.h>
  57. #include <linux/mutex.h>
  58. #include <linux/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;
  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. cfg.dst_addr = res->start + MMCIF_CE_DATA;
  378. cfg.src_addr = 0;
  379. ret = dmaengine_slave_config(chan, &cfg);
  380. if (ret < 0) {
  381. dma_release_channel(chan);
  382. return NULL;
  383. }
  384. return chan;
  385. }
  386. static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
  387. struct sh_mmcif_plat_data *pdata)
  388. {
  389. host->dma_active = false;
  390. if (pdata) {
  391. if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
  392. return;
  393. } else if (!host->pd->dev.of_node) {
  394. return;
  395. }
  396. /* We can only either use DMA for both Tx and Rx or not use it at all */
  397. host->chan_tx = sh_mmcif_request_dma_one(host, pdata, DMA_MEM_TO_DEV);
  398. if (!host->chan_tx)
  399. return;
  400. host->chan_rx = sh_mmcif_request_dma_one(host, pdata, DMA_DEV_TO_MEM);
  401. if (!host->chan_rx) {
  402. dma_release_channel(host->chan_tx);
  403. host->chan_tx = NULL;
  404. }
  405. }
  406. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  407. {
  408. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  409. /* Descriptors are freed automatically */
  410. if (host->chan_tx) {
  411. struct dma_chan *chan = host->chan_tx;
  412. host->chan_tx = NULL;
  413. dma_release_channel(chan);
  414. }
  415. if (host->chan_rx) {
  416. struct dma_chan *chan = host->chan_rx;
  417. host->chan_rx = NULL;
  418. dma_release_channel(chan);
  419. }
  420. host->dma_active = false;
  421. }
  422. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  423. {
  424. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  425. bool sup_pclk = p ? p->sup_pclk : false;
  426. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  427. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  428. if (!clk)
  429. return;
  430. if (sup_pclk && clk == host->clk)
  431. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  432. else
  433. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  434. ((fls(DIV_ROUND_UP(host->clk,
  435. clk) - 1) - 1) << 16));
  436. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  437. }
  438. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  439. {
  440. u32 tmp;
  441. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  442. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  443. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  444. if (host->ccs_enable)
  445. tmp |= SCCSTO_29;
  446. if (host->clk_ctrl2_enable)
  447. sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL2, 0x0F0F0000);
  448. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  449. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29);
  450. /* byte swap on */
  451. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  452. }
  453. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  454. {
  455. u32 state1, state2;
  456. int ret, timeout;
  457. host->sd_error = false;
  458. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  459. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  460. dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
  461. dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
  462. if (state1 & STS1_CMDSEQ) {
  463. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  464. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  465. for (timeout = 10000000; timeout; timeout--) {
  466. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  467. & STS1_CMDSEQ))
  468. break;
  469. mdelay(1);
  470. }
  471. if (!timeout) {
  472. dev_err(&host->pd->dev,
  473. "Forced end of command sequence timeout err\n");
  474. return -EIO;
  475. }
  476. sh_mmcif_sync_reset(host);
  477. dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
  478. return -EIO;
  479. }
  480. if (state2 & STS2_CRC_ERR) {
  481. dev_err(&host->pd->dev, " CRC error: state %u, wait %u\n",
  482. host->state, host->wait_for);
  483. ret = -EIO;
  484. } else if (state2 & STS2_TIMEOUT_ERR) {
  485. dev_err(&host->pd->dev, " Timeout: state %u, wait %u\n",
  486. host->state, host->wait_for);
  487. ret = -ETIMEDOUT;
  488. } else {
  489. dev_dbg(&host->pd->dev, " End/Index error: state %u, wait %u\n",
  490. host->state, host->wait_for);
  491. ret = -EIO;
  492. }
  493. return ret;
  494. }
  495. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  496. {
  497. struct mmc_data *data = host->mrq->data;
  498. host->sg_blkidx += host->blocksize;
  499. /* data->sg->length must be a multiple of host->blocksize? */
  500. BUG_ON(host->sg_blkidx > data->sg->length);
  501. if (host->sg_blkidx == data->sg->length) {
  502. host->sg_blkidx = 0;
  503. if (++host->sg_idx < data->sg_len)
  504. host->pio_ptr = sg_virt(++data->sg);
  505. } else {
  506. host->pio_ptr = p;
  507. }
  508. return host->sg_idx != data->sg_len;
  509. }
  510. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  511. struct mmc_request *mrq)
  512. {
  513. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  514. BLOCK_SIZE_MASK) + 3;
  515. host->wait_for = MMCIF_WAIT_FOR_READ;
  516. /* buf read enable */
  517. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  518. }
  519. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  520. {
  521. struct mmc_data *data = host->mrq->data;
  522. u32 *p = sg_virt(data->sg);
  523. int i;
  524. if (host->sd_error) {
  525. data->error = sh_mmcif_error_manage(host);
  526. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  527. return false;
  528. }
  529. for (i = 0; i < host->blocksize / 4; i++)
  530. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  531. /* buffer read end */
  532. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  533. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  534. return true;
  535. }
  536. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  537. struct mmc_request *mrq)
  538. {
  539. struct mmc_data *data = mrq->data;
  540. if (!data->sg_len || !data->sg->length)
  541. return;
  542. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  543. BLOCK_SIZE_MASK;
  544. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  545. host->sg_idx = 0;
  546. host->sg_blkidx = 0;
  547. host->pio_ptr = sg_virt(data->sg);
  548. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  549. }
  550. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  551. {
  552. struct mmc_data *data = host->mrq->data;
  553. u32 *p = host->pio_ptr;
  554. int i;
  555. if (host->sd_error) {
  556. data->error = sh_mmcif_error_manage(host);
  557. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  558. return false;
  559. }
  560. BUG_ON(!data->sg->length);
  561. for (i = 0; i < host->blocksize / 4; i++)
  562. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  563. if (!sh_mmcif_next_block(host, p))
  564. return false;
  565. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  566. return true;
  567. }
  568. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  569. struct mmc_request *mrq)
  570. {
  571. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  572. BLOCK_SIZE_MASK) + 3;
  573. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  574. /* buf write enable */
  575. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  576. }
  577. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  578. {
  579. struct mmc_data *data = host->mrq->data;
  580. u32 *p = sg_virt(data->sg);
  581. int i;
  582. if (host->sd_error) {
  583. data->error = sh_mmcif_error_manage(host);
  584. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  585. return false;
  586. }
  587. for (i = 0; i < host->blocksize / 4; i++)
  588. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  589. /* buffer write end */
  590. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  591. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  592. return true;
  593. }
  594. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  595. struct mmc_request *mrq)
  596. {
  597. struct mmc_data *data = mrq->data;
  598. if (!data->sg_len || !data->sg->length)
  599. return;
  600. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  601. BLOCK_SIZE_MASK;
  602. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  603. host->sg_idx = 0;
  604. host->sg_blkidx = 0;
  605. host->pio_ptr = sg_virt(data->sg);
  606. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  607. }
  608. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  609. {
  610. struct mmc_data *data = host->mrq->data;
  611. u32 *p = host->pio_ptr;
  612. int i;
  613. if (host->sd_error) {
  614. data->error = sh_mmcif_error_manage(host);
  615. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  616. return false;
  617. }
  618. BUG_ON(!data->sg->length);
  619. for (i = 0; i < host->blocksize / 4; i++)
  620. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  621. if (!sh_mmcif_next_block(host, p))
  622. return false;
  623. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  624. return true;
  625. }
  626. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  627. struct mmc_command *cmd)
  628. {
  629. if (cmd->flags & MMC_RSP_136) {
  630. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  631. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  632. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  633. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  634. } else
  635. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  636. }
  637. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  638. struct mmc_command *cmd)
  639. {
  640. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  641. }
  642. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  643. struct mmc_request *mrq)
  644. {
  645. struct mmc_data *data = mrq->data;
  646. struct mmc_command *cmd = mrq->cmd;
  647. u32 opc = cmd->opcode;
  648. u32 tmp = 0;
  649. /* Response Type check */
  650. switch (mmc_resp_type(cmd)) {
  651. case MMC_RSP_NONE:
  652. tmp |= CMD_SET_RTYP_NO;
  653. break;
  654. case MMC_RSP_R1:
  655. case MMC_RSP_R1B:
  656. case MMC_RSP_R3:
  657. tmp |= CMD_SET_RTYP_6B;
  658. break;
  659. case MMC_RSP_R2:
  660. tmp |= CMD_SET_RTYP_17B;
  661. break;
  662. default:
  663. dev_err(&host->pd->dev, "Unsupported response type.\n");
  664. break;
  665. }
  666. switch (opc) {
  667. /* RBSY */
  668. case MMC_SLEEP_AWAKE:
  669. case MMC_SWITCH:
  670. case MMC_STOP_TRANSMISSION:
  671. case MMC_SET_WRITE_PROT:
  672. case MMC_CLR_WRITE_PROT:
  673. case MMC_ERASE:
  674. tmp |= CMD_SET_RBSY;
  675. break;
  676. }
  677. /* WDAT / DATW */
  678. if (data) {
  679. tmp |= CMD_SET_WDAT;
  680. switch (host->bus_width) {
  681. case MMC_BUS_WIDTH_1:
  682. tmp |= CMD_SET_DATW_1;
  683. break;
  684. case MMC_BUS_WIDTH_4:
  685. tmp |= CMD_SET_DATW_4;
  686. break;
  687. case MMC_BUS_WIDTH_8:
  688. tmp |= CMD_SET_DATW_8;
  689. break;
  690. default:
  691. dev_err(&host->pd->dev, "Unsupported bus width.\n");
  692. break;
  693. }
  694. switch (host->timing) {
  695. case MMC_TIMING_MMC_DDR52:
  696. /*
  697. * MMC core will only set this timing, if the host
  698. * advertises the MMC_CAP_1_8V_DDR/MMC_CAP_1_2V_DDR
  699. * capability. MMCIF implementations with this
  700. * capability, e.g. sh73a0, will have to set it
  701. * in their platform data.
  702. */
  703. tmp |= CMD_SET_DARS;
  704. break;
  705. }
  706. }
  707. /* DWEN */
  708. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  709. tmp |= CMD_SET_DWEN;
  710. /* CMLTE/CMD12EN */
  711. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  712. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  713. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  714. data->blocks << 16);
  715. }
  716. /* RIDXC[1:0] check bits */
  717. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  718. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  719. tmp |= CMD_SET_RIDXC_BITS;
  720. /* RCRC7C[1:0] check bits */
  721. if (opc == MMC_SEND_OP_COND)
  722. tmp |= CMD_SET_CRC7C_BITS;
  723. /* RCRC7C[1:0] internal CRC7 */
  724. if (opc == MMC_ALL_SEND_CID ||
  725. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  726. tmp |= CMD_SET_CRC7C_INTERNAL;
  727. return (opc << 24) | tmp;
  728. }
  729. static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
  730. struct mmc_request *mrq, u32 opc)
  731. {
  732. switch (opc) {
  733. case MMC_READ_MULTIPLE_BLOCK:
  734. sh_mmcif_multi_read(host, mrq);
  735. return 0;
  736. case MMC_WRITE_MULTIPLE_BLOCK:
  737. sh_mmcif_multi_write(host, mrq);
  738. return 0;
  739. case MMC_WRITE_BLOCK:
  740. sh_mmcif_single_write(host, mrq);
  741. return 0;
  742. case MMC_READ_SINGLE_BLOCK:
  743. case MMC_SEND_EXT_CSD:
  744. sh_mmcif_single_read(host, mrq);
  745. return 0;
  746. default:
  747. dev_err(&host->pd->dev, "Unsupported CMD%d\n", opc);
  748. return -EINVAL;
  749. }
  750. }
  751. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  752. struct mmc_request *mrq)
  753. {
  754. struct mmc_command *cmd = mrq->cmd;
  755. u32 opc = cmd->opcode;
  756. u32 mask;
  757. switch (opc) {
  758. /* response busy check */
  759. case MMC_SLEEP_AWAKE:
  760. case MMC_SWITCH:
  761. case MMC_STOP_TRANSMISSION:
  762. case MMC_SET_WRITE_PROT:
  763. case MMC_CLR_WRITE_PROT:
  764. case MMC_ERASE:
  765. mask = MASK_START_CMD | MASK_MRBSYE;
  766. break;
  767. default:
  768. mask = MASK_START_CMD | MASK_MCRSPE;
  769. break;
  770. }
  771. if (host->ccs_enable)
  772. mask |= MASK_MCCSTO;
  773. if (mrq->data) {
  774. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  775. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  776. mrq->data->blksz);
  777. }
  778. opc = sh_mmcif_set_cmd(host, mrq);
  779. if (host->ccs_enable)
  780. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  781. else
  782. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0 | INT_CCS);
  783. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  784. /* set arg */
  785. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  786. /* set cmd */
  787. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  788. host->wait_for = MMCIF_WAIT_FOR_CMD;
  789. schedule_delayed_work(&host->timeout_work, host->timeout);
  790. }
  791. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  792. struct mmc_request *mrq)
  793. {
  794. switch (mrq->cmd->opcode) {
  795. case MMC_READ_MULTIPLE_BLOCK:
  796. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  797. break;
  798. case MMC_WRITE_MULTIPLE_BLOCK:
  799. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  800. break;
  801. default:
  802. dev_err(&host->pd->dev, "unsupported stop cmd\n");
  803. mrq->stop->error = sh_mmcif_error_manage(host);
  804. return;
  805. }
  806. host->wait_for = MMCIF_WAIT_FOR_STOP;
  807. }
  808. static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
  809. {
  810. struct sh_mmcif_host *host = mmc_priv(mmc);
  811. unsigned long flags;
  812. spin_lock_irqsave(&host->lock, flags);
  813. if (host->state != STATE_IDLE) {
  814. dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
  815. spin_unlock_irqrestore(&host->lock, flags);
  816. mrq->cmd->error = -EAGAIN;
  817. mmc_request_done(mmc, mrq);
  818. return;
  819. }
  820. host->state = STATE_REQUEST;
  821. spin_unlock_irqrestore(&host->lock, flags);
  822. switch (mrq->cmd->opcode) {
  823. /* MMCIF does not support SD/SDIO command */
  824. case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
  825. case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
  826. if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
  827. break;
  828. case MMC_APP_CMD:
  829. case SD_IO_RW_DIRECT:
  830. host->state = STATE_IDLE;
  831. mrq->cmd->error = -ETIMEDOUT;
  832. mmc_request_done(mmc, mrq);
  833. return;
  834. default:
  835. break;
  836. }
  837. host->mrq = mrq;
  838. sh_mmcif_start_cmd(host, mrq);
  839. }
  840. static int sh_mmcif_clk_update(struct sh_mmcif_host *host)
  841. {
  842. int ret = clk_prepare_enable(host->hclk);
  843. if (!ret) {
  844. host->clk = clk_get_rate(host->hclk);
  845. host->mmc->f_max = host->clk / 2;
  846. host->mmc->f_min = host->clk / 512;
  847. }
  848. return ret;
  849. }
  850. static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
  851. {
  852. struct mmc_host *mmc = host->mmc;
  853. if (!IS_ERR(mmc->supply.vmmc))
  854. /* Errors ignored... */
  855. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  856. ios->power_mode ? ios->vdd : 0);
  857. }
  858. static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  859. {
  860. struct sh_mmcif_host *host = mmc_priv(mmc);
  861. unsigned long flags;
  862. spin_lock_irqsave(&host->lock, flags);
  863. if (host->state != STATE_IDLE) {
  864. dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
  865. spin_unlock_irqrestore(&host->lock, flags);
  866. return;
  867. }
  868. host->state = STATE_IOS;
  869. spin_unlock_irqrestore(&host->lock, flags);
  870. if (ios->power_mode == MMC_POWER_UP) {
  871. if (!host->card_present) {
  872. /* See if we also get DMA */
  873. sh_mmcif_request_dma(host, host->pd->dev.platform_data);
  874. host->card_present = true;
  875. }
  876. sh_mmcif_set_power(host, ios);
  877. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  878. /* clock stop */
  879. sh_mmcif_clock_control(host, 0);
  880. if (ios->power_mode == MMC_POWER_OFF) {
  881. if (host->card_present) {
  882. sh_mmcif_release_dma(host);
  883. host->card_present = false;
  884. }
  885. }
  886. if (host->power) {
  887. pm_runtime_put_sync(&host->pd->dev);
  888. clk_disable_unprepare(host->hclk);
  889. host->power = false;
  890. if (ios->power_mode == MMC_POWER_OFF)
  891. sh_mmcif_set_power(host, ios);
  892. }
  893. host->state = STATE_IDLE;
  894. return;
  895. }
  896. if (ios->clock) {
  897. if (!host->power) {
  898. sh_mmcif_clk_update(host);
  899. pm_runtime_get_sync(&host->pd->dev);
  900. host->power = true;
  901. sh_mmcif_sync_reset(host);
  902. }
  903. sh_mmcif_clock_control(host, ios->clock);
  904. }
  905. host->timing = ios->timing;
  906. host->bus_width = ios->bus_width;
  907. host->state = STATE_IDLE;
  908. }
  909. static int sh_mmcif_get_cd(struct mmc_host *mmc)
  910. {
  911. struct sh_mmcif_host *host = mmc_priv(mmc);
  912. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  913. int ret = mmc_gpio_get_cd(mmc);
  914. if (ret >= 0)
  915. return ret;
  916. if (!p || !p->get_cd)
  917. return -ENOSYS;
  918. else
  919. return p->get_cd(host->pd);
  920. }
  921. static struct mmc_host_ops sh_mmcif_ops = {
  922. .request = sh_mmcif_request,
  923. .set_ios = sh_mmcif_set_ios,
  924. .get_cd = sh_mmcif_get_cd,
  925. };
  926. static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
  927. {
  928. struct mmc_command *cmd = host->mrq->cmd;
  929. struct mmc_data *data = host->mrq->data;
  930. long time;
  931. if (host->sd_error) {
  932. switch (cmd->opcode) {
  933. case MMC_ALL_SEND_CID:
  934. case MMC_SELECT_CARD:
  935. case MMC_APP_CMD:
  936. cmd->error = -ETIMEDOUT;
  937. break;
  938. default:
  939. cmd->error = sh_mmcif_error_manage(host);
  940. break;
  941. }
  942. dev_dbg(&host->pd->dev, "CMD%d error %d\n",
  943. cmd->opcode, cmd->error);
  944. host->sd_error = false;
  945. return false;
  946. }
  947. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  948. cmd->error = 0;
  949. return false;
  950. }
  951. sh_mmcif_get_response(host, cmd);
  952. if (!data)
  953. return false;
  954. /*
  955. * Completion can be signalled from DMA callback and error, so, have to
  956. * reset here, before setting .dma_active
  957. */
  958. init_completion(&host->dma_complete);
  959. if (data->flags & MMC_DATA_READ) {
  960. if (host->chan_rx)
  961. sh_mmcif_start_dma_rx(host);
  962. } else {
  963. if (host->chan_tx)
  964. sh_mmcif_start_dma_tx(host);
  965. }
  966. if (!host->dma_active) {
  967. data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
  968. return !data->error;
  969. }
  970. /* Running in the IRQ thread, can sleep */
  971. time = wait_for_completion_interruptible_timeout(&host->dma_complete,
  972. host->timeout);
  973. if (data->flags & MMC_DATA_READ)
  974. dma_unmap_sg(host->chan_rx->device->dev,
  975. data->sg, data->sg_len,
  976. DMA_FROM_DEVICE);
  977. else
  978. dma_unmap_sg(host->chan_tx->device->dev,
  979. data->sg, data->sg_len,
  980. DMA_TO_DEVICE);
  981. if (host->sd_error) {
  982. dev_err(host->mmc->parent,
  983. "Error IRQ while waiting for DMA completion!\n");
  984. /* Woken up by an error IRQ: abort DMA */
  985. data->error = sh_mmcif_error_manage(host);
  986. } else if (!time) {
  987. dev_err(host->mmc->parent, "DMA timeout!\n");
  988. data->error = -ETIMEDOUT;
  989. } else if (time < 0) {
  990. dev_err(host->mmc->parent,
  991. "wait_for_completion_...() error %ld!\n", time);
  992. data->error = time;
  993. }
  994. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
  995. BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  996. host->dma_active = false;
  997. if (data->error) {
  998. data->bytes_xfered = 0;
  999. /* Abort DMA */
  1000. if (data->flags & MMC_DATA_READ)
  1001. dmaengine_terminate_all(host->chan_rx);
  1002. else
  1003. dmaengine_terminate_all(host->chan_tx);
  1004. }
  1005. return false;
  1006. }
  1007. static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
  1008. {
  1009. struct sh_mmcif_host *host = dev_id;
  1010. struct mmc_request *mrq;
  1011. bool wait = false;
  1012. cancel_delayed_work_sync(&host->timeout_work);
  1013. mutex_lock(&host->thread_lock);
  1014. mrq = host->mrq;
  1015. if (!mrq) {
  1016. dev_dbg(&host->pd->dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
  1017. host->state, host->wait_for);
  1018. mutex_unlock(&host->thread_lock);
  1019. return IRQ_HANDLED;
  1020. }
  1021. /*
  1022. * All handlers return true, if processing continues, and false, if the
  1023. * request has to be completed - successfully or not
  1024. */
  1025. switch (host->wait_for) {
  1026. case MMCIF_WAIT_FOR_REQUEST:
  1027. /* We're too late, the timeout has already kicked in */
  1028. mutex_unlock(&host->thread_lock);
  1029. return IRQ_HANDLED;
  1030. case MMCIF_WAIT_FOR_CMD:
  1031. /* Wait for data? */
  1032. wait = sh_mmcif_end_cmd(host);
  1033. break;
  1034. case MMCIF_WAIT_FOR_MREAD:
  1035. /* Wait for more data? */
  1036. wait = sh_mmcif_mread_block(host);
  1037. break;
  1038. case MMCIF_WAIT_FOR_READ:
  1039. /* Wait for data end? */
  1040. wait = sh_mmcif_read_block(host);
  1041. break;
  1042. case MMCIF_WAIT_FOR_MWRITE:
  1043. /* Wait data to write? */
  1044. wait = sh_mmcif_mwrite_block(host);
  1045. break;
  1046. case MMCIF_WAIT_FOR_WRITE:
  1047. /* Wait for data end? */
  1048. wait = sh_mmcif_write_block(host);
  1049. break;
  1050. case MMCIF_WAIT_FOR_STOP:
  1051. if (host->sd_error) {
  1052. mrq->stop->error = sh_mmcif_error_manage(host);
  1053. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->stop->error);
  1054. break;
  1055. }
  1056. sh_mmcif_get_cmd12response(host, mrq->stop);
  1057. mrq->stop->error = 0;
  1058. break;
  1059. case MMCIF_WAIT_FOR_READ_END:
  1060. case MMCIF_WAIT_FOR_WRITE_END:
  1061. if (host->sd_error) {
  1062. mrq->data->error = sh_mmcif_error_manage(host);
  1063. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->data->error);
  1064. }
  1065. break;
  1066. default:
  1067. BUG();
  1068. }
  1069. if (wait) {
  1070. schedule_delayed_work(&host->timeout_work, host->timeout);
  1071. /* Wait for more data */
  1072. mutex_unlock(&host->thread_lock);
  1073. return IRQ_HANDLED;
  1074. }
  1075. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1076. struct mmc_data *data = mrq->data;
  1077. if (!mrq->cmd->error && data && !data->error)
  1078. data->bytes_xfered =
  1079. data->blocks * data->blksz;
  1080. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1081. sh_mmcif_stop_cmd(host, mrq);
  1082. if (!mrq->stop->error) {
  1083. schedule_delayed_work(&host->timeout_work, host->timeout);
  1084. mutex_unlock(&host->thread_lock);
  1085. return IRQ_HANDLED;
  1086. }
  1087. }
  1088. }
  1089. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1090. host->state = STATE_IDLE;
  1091. host->mrq = NULL;
  1092. mmc_request_done(host->mmc, mrq);
  1093. mutex_unlock(&host->thread_lock);
  1094. return IRQ_HANDLED;
  1095. }
  1096. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1097. {
  1098. struct sh_mmcif_host *host = dev_id;
  1099. u32 state, mask;
  1100. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1101. mask = sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK);
  1102. if (host->ccs_enable)
  1103. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(state & mask));
  1104. else
  1105. sh_mmcif_writel(host->addr, MMCIF_CE_INT, INT_CCS | ~(state & mask));
  1106. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN);
  1107. if (state & ~MASK_CLEAN)
  1108. dev_dbg(&host->pd->dev, "IRQ state = 0x%08x incompletely cleared\n",
  1109. state);
  1110. if (state & INT_ERR_STS || state & ~INT_ALL) {
  1111. host->sd_error = true;
  1112. dev_dbg(&host->pd->dev, "int err state = 0x%08x\n", state);
  1113. }
  1114. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1115. if (!host->mrq)
  1116. dev_dbg(&host->pd->dev, "NULL IRQ state = 0x%08x\n", state);
  1117. if (!host->dma_active)
  1118. return IRQ_WAKE_THREAD;
  1119. else if (host->sd_error)
  1120. mmcif_dma_complete(host);
  1121. } else {
  1122. dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
  1123. }
  1124. return IRQ_HANDLED;
  1125. }
  1126. static void mmcif_timeout_work(struct work_struct *work)
  1127. {
  1128. struct delayed_work *d = container_of(work, struct delayed_work, work);
  1129. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1130. struct mmc_request *mrq = host->mrq;
  1131. unsigned long flags;
  1132. if (host->dying)
  1133. /* Don't run after mmc_remove_host() */
  1134. return;
  1135. dev_err(&host->pd->dev, "Timeout waiting for %u on CMD%u\n",
  1136. host->wait_for, mrq->cmd->opcode);
  1137. spin_lock_irqsave(&host->lock, flags);
  1138. if (host->state == STATE_IDLE) {
  1139. spin_unlock_irqrestore(&host->lock, flags);
  1140. return;
  1141. }
  1142. host->state = STATE_TIMEOUT;
  1143. spin_unlock_irqrestore(&host->lock, flags);
  1144. /*
  1145. * Handle races with cancel_delayed_work(), unless
  1146. * cancel_delayed_work_sync() is used
  1147. */
  1148. switch (host->wait_for) {
  1149. case MMCIF_WAIT_FOR_CMD:
  1150. mrq->cmd->error = sh_mmcif_error_manage(host);
  1151. break;
  1152. case MMCIF_WAIT_FOR_STOP:
  1153. mrq->stop->error = sh_mmcif_error_manage(host);
  1154. break;
  1155. case MMCIF_WAIT_FOR_MREAD:
  1156. case MMCIF_WAIT_FOR_MWRITE:
  1157. case MMCIF_WAIT_FOR_READ:
  1158. case MMCIF_WAIT_FOR_WRITE:
  1159. case MMCIF_WAIT_FOR_READ_END:
  1160. case MMCIF_WAIT_FOR_WRITE_END:
  1161. mrq->data->error = sh_mmcif_error_manage(host);
  1162. break;
  1163. default:
  1164. BUG();
  1165. }
  1166. host->state = STATE_IDLE;
  1167. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1168. host->mrq = NULL;
  1169. mmc_request_done(host->mmc, mrq);
  1170. }
  1171. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1172. {
  1173. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  1174. struct mmc_host *mmc = host->mmc;
  1175. mmc_regulator_get_supply(mmc);
  1176. if (!pd)
  1177. return;
  1178. if (!mmc->ocr_avail)
  1179. mmc->ocr_avail = pd->ocr;
  1180. else if (pd->ocr)
  1181. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1182. }
  1183. static int sh_mmcif_probe(struct platform_device *pdev)
  1184. {
  1185. int ret = 0, irq[2];
  1186. struct mmc_host *mmc;
  1187. struct sh_mmcif_host *host;
  1188. struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
  1189. struct resource *res;
  1190. void __iomem *reg;
  1191. const char *name;
  1192. irq[0] = platform_get_irq(pdev, 0);
  1193. irq[1] = platform_get_irq(pdev, 1);
  1194. if (irq[0] < 0) {
  1195. dev_err(&pdev->dev, "Get irq error\n");
  1196. return -ENXIO;
  1197. }
  1198. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1199. if (!res) {
  1200. dev_err(&pdev->dev, "platform_get_resource error.\n");
  1201. return -ENXIO;
  1202. }
  1203. reg = ioremap(res->start, resource_size(res));
  1204. if (!reg) {
  1205. dev_err(&pdev->dev, "ioremap error.\n");
  1206. return -ENOMEM;
  1207. }
  1208. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  1209. if (!mmc) {
  1210. ret = -ENOMEM;
  1211. goto ealloch;
  1212. }
  1213. ret = mmc_of_parse(mmc);
  1214. if (ret < 0)
  1215. goto eofparse;
  1216. host = mmc_priv(mmc);
  1217. host->mmc = mmc;
  1218. host->addr = reg;
  1219. host->timeout = msecs_to_jiffies(1000);
  1220. host->ccs_enable = !pd || !pd->ccs_unsupported;
  1221. host->clk_ctrl2_enable = pd && pd->clk_ctrl2_present;
  1222. host->pd = pdev;
  1223. spin_lock_init(&host->lock);
  1224. mmc->ops = &sh_mmcif_ops;
  1225. sh_mmcif_init_ocr(host);
  1226. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1227. if (pd && pd->caps)
  1228. mmc->caps |= pd->caps;
  1229. mmc->max_segs = 32;
  1230. mmc->max_blk_size = 512;
  1231. mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
  1232. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1233. mmc->max_seg_size = mmc->max_req_size;
  1234. platform_set_drvdata(pdev, host);
  1235. pm_runtime_enable(&pdev->dev);
  1236. host->power = false;
  1237. host->hclk = clk_get(&pdev->dev, NULL);
  1238. if (IS_ERR(host->hclk)) {
  1239. ret = PTR_ERR(host->hclk);
  1240. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  1241. goto eclkget;
  1242. }
  1243. ret = sh_mmcif_clk_update(host);
  1244. if (ret < 0)
  1245. goto eclkupdate;
  1246. ret = pm_runtime_resume(&pdev->dev);
  1247. if (ret < 0)
  1248. goto eresume;
  1249. INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
  1250. sh_mmcif_sync_reset(host);
  1251. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1252. name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
  1253. ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, name, host);
  1254. if (ret) {
  1255. dev_err(&pdev->dev, "request_irq error (%s)\n", name);
  1256. goto ereqirq0;
  1257. }
  1258. if (irq[1] >= 0) {
  1259. ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt,
  1260. 0, "sh_mmc:int", host);
  1261. if (ret) {
  1262. dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
  1263. goto ereqirq1;
  1264. }
  1265. }
  1266. if (pd && pd->use_cd_gpio) {
  1267. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0);
  1268. if (ret < 0)
  1269. goto erqcd;
  1270. }
  1271. mutex_init(&host->thread_lock);
  1272. clk_disable_unprepare(host->hclk);
  1273. ret = mmc_add_host(mmc);
  1274. if (ret < 0)
  1275. goto emmcaddh;
  1276. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1277. dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
  1278. dev_dbg(&pdev->dev, "chip ver H'%04x\n",
  1279. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
  1280. return ret;
  1281. emmcaddh:
  1282. erqcd:
  1283. if (irq[1] >= 0)
  1284. free_irq(irq[1], host);
  1285. ereqirq1:
  1286. free_irq(irq[0], host);
  1287. ereqirq0:
  1288. pm_runtime_suspend(&pdev->dev);
  1289. eresume:
  1290. clk_disable_unprepare(host->hclk);
  1291. eclkupdate:
  1292. clk_put(host->hclk);
  1293. eclkget:
  1294. pm_runtime_disable(&pdev->dev);
  1295. eofparse:
  1296. mmc_free_host(mmc);
  1297. ealloch:
  1298. iounmap(reg);
  1299. return ret;
  1300. }
  1301. static int sh_mmcif_remove(struct platform_device *pdev)
  1302. {
  1303. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1304. int irq[2];
  1305. host->dying = true;
  1306. clk_prepare_enable(host->hclk);
  1307. pm_runtime_get_sync(&pdev->dev);
  1308. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1309. mmc_remove_host(host->mmc);
  1310. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1311. /*
  1312. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1313. * mmc_remove_host() call above. But swapping order doesn't help either
  1314. * (a query on the linux-mmc mailing list didn't bring any replies).
  1315. */
  1316. cancel_delayed_work_sync(&host->timeout_work);
  1317. if (host->addr)
  1318. iounmap(host->addr);
  1319. irq[0] = platform_get_irq(pdev, 0);
  1320. irq[1] = platform_get_irq(pdev, 1);
  1321. free_irq(irq[0], host);
  1322. if (irq[1] >= 0)
  1323. free_irq(irq[1], host);
  1324. clk_disable_unprepare(host->hclk);
  1325. mmc_free_host(host->mmc);
  1326. pm_runtime_put_sync(&pdev->dev);
  1327. pm_runtime_disable(&pdev->dev);
  1328. return 0;
  1329. }
  1330. #ifdef CONFIG_PM_SLEEP
  1331. static int sh_mmcif_suspend(struct device *dev)
  1332. {
  1333. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1334. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1335. return 0;
  1336. }
  1337. static int sh_mmcif_resume(struct device *dev)
  1338. {
  1339. return 0;
  1340. }
  1341. #endif
  1342. static const struct of_device_id mmcif_of_match[] = {
  1343. { .compatible = "renesas,sh-mmcif" },
  1344. { }
  1345. };
  1346. MODULE_DEVICE_TABLE(of, mmcif_of_match);
  1347. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1348. SET_SYSTEM_SLEEP_PM_OPS(sh_mmcif_suspend, sh_mmcif_resume)
  1349. };
  1350. static struct platform_driver sh_mmcif_driver = {
  1351. .probe = sh_mmcif_probe,
  1352. .remove = sh_mmcif_remove,
  1353. .driver = {
  1354. .name = DRIVER_NAME,
  1355. .pm = &sh_mmcif_dev_pm_ops,
  1356. .owner = THIS_MODULE,
  1357. .of_match_table = mmcif_of_match,
  1358. },
  1359. };
  1360. module_platform_driver(sh_mmcif_driver);
  1361. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1362. MODULE_LICENSE("GPL");
  1363. MODULE_ALIAS("platform:" DRIVER_NAME);
  1364. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");