sh_mmcif.c 41 KB

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