sh_mmcif.c 38 KB

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