sh_mmcif.c 42 KB

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