sh_mmcif.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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 card_present;
  234. bool ccs_enable; /* Command Completion Signal support */
  235. bool clk_ctrl2_enable;
  236. struct mutex thread_lock;
  237. u32 clkdiv_map; /* see CE_CLK_CTRL::CLKDIV */
  238. /* DMA support */
  239. struct dma_chan *chan_rx;
  240. struct dma_chan *chan_tx;
  241. struct completion dma_complete;
  242. bool dma_active;
  243. };
  244. static const struct of_device_id sh_mmcif_of_match[] = {
  245. { .compatible = "renesas,sh-mmcif" },
  246. { }
  247. };
  248. MODULE_DEVICE_TABLE(of, sh_mmcif_of_match);
  249. #define sh_mmcif_host_to_dev(host) (&host->pd->dev)
  250. static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
  251. unsigned int reg, u32 val)
  252. {
  253. writel(val | readl(host->addr + reg), host->addr + reg);
  254. }
  255. static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
  256. unsigned int reg, u32 val)
  257. {
  258. writel(~val & readl(host->addr + reg), host->addr + reg);
  259. }
  260. static void sh_mmcif_dma_complete(void *arg)
  261. {
  262. struct sh_mmcif_host *host = arg;
  263. struct mmc_request *mrq = host->mrq;
  264. struct device *dev = sh_mmcif_host_to_dev(host);
  265. dev_dbg(dev, "Command completed\n");
  266. if (WARN(!mrq || !mrq->data, "%s: NULL data in DMA completion!\n",
  267. dev_name(dev)))
  268. return;
  269. complete(&host->dma_complete);
  270. }
  271. static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
  272. {
  273. struct mmc_data *data = host->mrq->data;
  274. struct scatterlist *sg = data->sg;
  275. struct dma_async_tx_descriptor *desc = NULL;
  276. struct dma_chan *chan = host->chan_rx;
  277. struct device *dev = sh_mmcif_host_to_dev(host);
  278. dma_cookie_t cookie = -EINVAL;
  279. int ret;
  280. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  281. DMA_FROM_DEVICE);
  282. if (ret > 0) {
  283. host->dma_active = true;
  284. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  285. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  286. }
  287. if (desc) {
  288. desc->callback = sh_mmcif_dma_complete;
  289. desc->callback_param = host;
  290. cookie = dmaengine_submit(desc);
  291. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
  292. dma_async_issue_pending(chan);
  293. }
  294. dev_dbg(dev, "%s(): mapped %d -> %d, cookie %d\n",
  295. __func__, data->sg_len, ret, cookie);
  296. if (!desc) {
  297. /* DMA failed, fall back to PIO */
  298. if (ret >= 0)
  299. ret = -EIO;
  300. host->chan_rx = NULL;
  301. host->dma_active = false;
  302. dma_release_channel(chan);
  303. /* Free the Tx channel too */
  304. chan = host->chan_tx;
  305. if (chan) {
  306. host->chan_tx = NULL;
  307. dma_release_channel(chan);
  308. }
  309. dev_warn(dev,
  310. "DMA failed: %d, falling back to PIO\n", ret);
  311. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  312. }
  313. dev_dbg(dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  314. desc, cookie, data->sg_len);
  315. }
  316. static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
  317. {
  318. struct mmc_data *data = host->mrq->data;
  319. struct scatterlist *sg = data->sg;
  320. struct dma_async_tx_descriptor *desc = NULL;
  321. struct dma_chan *chan = host->chan_tx;
  322. struct device *dev = sh_mmcif_host_to_dev(host);
  323. dma_cookie_t cookie = -EINVAL;
  324. int ret;
  325. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  326. DMA_TO_DEVICE);
  327. if (ret > 0) {
  328. host->dma_active = true;
  329. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  330. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  331. }
  332. if (desc) {
  333. desc->callback = sh_mmcif_dma_complete;
  334. desc->callback_param = host;
  335. cookie = dmaengine_submit(desc);
  336. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
  337. dma_async_issue_pending(chan);
  338. }
  339. dev_dbg(dev, "%s(): mapped %d -> %d, cookie %d\n",
  340. __func__, data->sg_len, ret, cookie);
  341. if (!desc) {
  342. /* DMA failed, fall back to PIO */
  343. if (ret >= 0)
  344. ret = -EIO;
  345. host->chan_tx = NULL;
  346. host->dma_active = false;
  347. dma_release_channel(chan);
  348. /* Free the Rx channel too */
  349. chan = host->chan_rx;
  350. if (chan) {
  351. host->chan_rx = NULL;
  352. dma_release_channel(chan);
  353. }
  354. dev_warn(dev,
  355. "DMA failed: %d, falling back to PIO\n", ret);
  356. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  357. }
  358. dev_dbg(dev, "%s(): desc %p, cookie %d\n", __func__,
  359. desc, cookie);
  360. }
  361. static struct dma_chan *
  362. sh_mmcif_request_dma_pdata(struct sh_mmcif_host *host, uintptr_t slave_id)
  363. {
  364. dma_cap_mask_t mask;
  365. dma_cap_zero(mask);
  366. dma_cap_set(DMA_SLAVE, mask);
  367. if (slave_id <= 0)
  368. return NULL;
  369. return dma_request_channel(mask, shdma_chan_filter, (void *)slave_id);
  370. }
  371. static int sh_mmcif_dma_slave_config(struct sh_mmcif_host *host,
  372. struct dma_chan *chan,
  373. enum dma_transfer_direction direction)
  374. {
  375. struct resource *res;
  376. struct dma_slave_config cfg = { 0, };
  377. res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
  378. cfg.direction = direction;
  379. if (direction == DMA_DEV_TO_MEM) {
  380. cfg.src_addr = res->start + MMCIF_CE_DATA;
  381. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  382. } else {
  383. cfg.dst_addr = res->start + MMCIF_CE_DATA;
  384. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  385. }
  386. return dmaengine_slave_config(chan, &cfg);
  387. }
  388. static void sh_mmcif_request_dma(struct sh_mmcif_host *host)
  389. {
  390. struct device *dev = sh_mmcif_host_to_dev(host);
  391. host->dma_active = false;
  392. /* We can only either use DMA for both Tx and Rx or not use it at all */
  393. if (IS_ENABLED(CONFIG_SUPERH) && dev->platform_data) {
  394. struct sh_mmcif_plat_data *pdata = dev->platform_data;
  395. host->chan_tx = sh_mmcif_request_dma_pdata(host,
  396. pdata->slave_id_tx);
  397. host->chan_rx = sh_mmcif_request_dma_pdata(host,
  398. pdata->slave_id_rx);
  399. } else {
  400. host->chan_tx = dma_request_slave_channel(dev, "tx");
  401. host->chan_rx = dma_request_slave_channel(dev, "rx");
  402. }
  403. dev_dbg(dev, "%s: got channel TX %p RX %p\n", __func__, host->chan_tx,
  404. host->chan_rx);
  405. if (!host->chan_tx || !host->chan_rx ||
  406. sh_mmcif_dma_slave_config(host, host->chan_tx, DMA_MEM_TO_DEV) ||
  407. sh_mmcif_dma_slave_config(host, host->chan_rx, DMA_DEV_TO_MEM))
  408. goto error;
  409. return;
  410. error:
  411. if (host->chan_tx)
  412. dma_release_channel(host->chan_tx);
  413. if (host->chan_rx)
  414. dma_release_channel(host->chan_rx);
  415. host->chan_tx = host->chan_rx = NULL;
  416. }
  417. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  418. {
  419. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  420. /* Descriptors are freed automatically */
  421. if (host->chan_tx) {
  422. struct dma_chan *chan = host->chan_tx;
  423. host->chan_tx = NULL;
  424. dma_release_channel(chan);
  425. }
  426. if (host->chan_rx) {
  427. struct dma_chan *chan = host->chan_rx;
  428. host->chan_rx = NULL;
  429. dma_release_channel(chan);
  430. }
  431. host->dma_active = false;
  432. }
  433. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  434. {
  435. struct device *dev = sh_mmcif_host_to_dev(host);
  436. struct sh_mmcif_plat_data *p = dev->platform_data;
  437. bool sup_pclk = p ? p->sup_pclk : false;
  438. unsigned int current_clk = clk_get_rate(host->clk);
  439. unsigned int clkdiv;
  440. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  441. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  442. if (!clk)
  443. return;
  444. if (host->clkdiv_map) {
  445. unsigned int freq, best_freq, myclk, div, diff_min, diff;
  446. int i;
  447. clkdiv = 0;
  448. diff_min = ~0;
  449. best_freq = 0;
  450. for (i = 31; i >= 0; i--) {
  451. if (!((1 << i) & host->clkdiv_map))
  452. continue;
  453. /*
  454. * clk = parent_freq / div
  455. * -> parent_freq = clk x div
  456. */
  457. div = 1 << (i + 1);
  458. freq = clk_round_rate(host->clk, clk * div);
  459. myclk = freq / div;
  460. diff = (myclk > clk) ? myclk - clk : clk - myclk;
  461. if (diff <= diff_min) {
  462. best_freq = freq;
  463. clkdiv = i;
  464. diff_min = diff;
  465. }
  466. }
  467. dev_dbg(dev, "clk %u/%u (%u, 0x%x)\n",
  468. (best_freq / (1 << (clkdiv + 1))), clk,
  469. best_freq, clkdiv);
  470. clk_set_rate(host->clk, best_freq);
  471. clkdiv = clkdiv << 16;
  472. } else if (sup_pclk && clk == current_clk) {
  473. clkdiv = CLK_SUP_PCLK;
  474. } else {
  475. clkdiv = (fls(DIV_ROUND_UP(current_clk, clk) - 1) - 1) << 16;
  476. }
  477. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR & clkdiv);
  478. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  479. }
  480. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  481. {
  482. u32 tmp;
  483. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  484. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  485. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  486. if (host->ccs_enable)
  487. tmp |= SCCSTO_29;
  488. if (host->clk_ctrl2_enable)
  489. sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL2, 0x0F0F0000);
  490. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  491. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29);
  492. /* byte swap on */
  493. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  494. }
  495. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  496. {
  497. struct device *dev = sh_mmcif_host_to_dev(host);
  498. u32 state1, state2;
  499. int ret, timeout;
  500. host->sd_error = false;
  501. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  502. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  503. dev_dbg(dev, "ERR HOST_STS1 = %08x\n", state1);
  504. dev_dbg(dev, "ERR HOST_STS2 = %08x\n", state2);
  505. if (state1 & STS1_CMDSEQ) {
  506. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  507. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  508. for (timeout = 10000000; timeout; timeout--) {
  509. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  510. & STS1_CMDSEQ))
  511. break;
  512. mdelay(1);
  513. }
  514. if (!timeout) {
  515. dev_err(dev,
  516. "Forced end of command sequence timeout err\n");
  517. return -EIO;
  518. }
  519. sh_mmcif_sync_reset(host);
  520. dev_dbg(dev, "Forced end of command sequence\n");
  521. return -EIO;
  522. }
  523. if (state2 & STS2_CRC_ERR) {
  524. dev_err(dev, " CRC error: state %u, wait %u\n",
  525. host->state, host->wait_for);
  526. ret = -EIO;
  527. } else if (state2 & STS2_TIMEOUT_ERR) {
  528. dev_err(dev, " Timeout: state %u, wait %u\n",
  529. host->state, host->wait_for);
  530. ret = -ETIMEDOUT;
  531. } else {
  532. dev_dbg(dev, " End/Index error: state %u, wait %u\n",
  533. host->state, host->wait_for);
  534. ret = -EIO;
  535. }
  536. return ret;
  537. }
  538. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  539. {
  540. struct mmc_data *data = host->mrq->data;
  541. host->sg_blkidx += host->blocksize;
  542. /* data->sg->length must be a multiple of host->blocksize? */
  543. BUG_ON(host->sg_blkidx > data->sg->length);
  544. if (host->sg_blkidx == data->sg->length) {
  545. host->sg_blkidx = 0;
  546. if (++host->sg_idx < data->sg_len)
  547. host->pio_ptr = sg_virt(++data->sg);
  548. } else {
  549. host->pio_ptr = p;
  550. }
  551. return host->sg_idx != data->sg_len;
  552. }
  553. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  554. struct mmc_request *mrq)
  555. {
  556. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  557. BLOCK_SIZE_MASK) + 3;
  558. host->wait_for = MMCIF_WAIT_FOR_READ;
  559. /* buf read enable */
  560. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  561. }
  562. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  563. {
  564. struct device *dev = sh_mmcif_host_to_dev(host);
  565. struct mmc_data *data = host->mrq->data;
  566. u32 *p = sg_virt(data->sg);
  567. int i;
  568. if (host->sd_error) {
  569. data->error = sh_mmcif_error_manage(host);
  570. dev_dbg(dev, "%s(): %d\n", __func__, data->error);
  571. return false;
  572. }
  573. for (i = 0; i < host->blocksize / 4; i++)
  574. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  575. /* buffer read end */
  576. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  577. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  578. return true;
  579. }
  580. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  581. struct mmc_request *mrq)
  582. {
  583. struct mmc_data *data = mrq->data;
  584. if (!data->sg_len || !data->sg->length)
  585. return;
  586. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  587. BLOCK_SIZE_MASK;
  588. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  589. host->sg_idx = 0;
  590. host->sg_blkidx = 0;
  591. host->pio_ptr = sg_virt(data->sg);
  592. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  593. }
  594. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  595. {
  596. struct device *dev = sh_mmcif_host_to_dev(host);
  597. struct mmc_data *data = host->mrq->data;
  598. u32 *p = host->pio_ptr;
  599. int i;
  600. if (host->sd_error) {
  601. data->error = sh_mmcif_error_manage(host);
  602. dev_dbg(dev, "%s(): %d\n", __func__, data->error);
  603. return false;
  604. }
  605. BUG_ON(!data->sg->length);
  606. for (i = 0; i < host->blocksize / 4; i++)
  607. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  608. if (!sh_mmcif_next_block(host, p))
  609. return false;
  610. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  611. return true;
  612. }
  613. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  614. struct mmc_request *mrq)
  615. {
  616. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  617. BLOCK_SIZE_MASK) + 3;
  618. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  619. /* buf write enable */
  620. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  621. }
  622. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  623. {
  624. struct device *dev = sh_mmcif_host_to_dev(host);
  625. struct mmc_data *data = host->mrq->data;
  626. u32 *p = sg_virt(data->sg);
  627. int i;
  628. if (host->sd_error) {
  629. data->error = sh_mmcif_error_manage(host);
  630. dev_dbg(dev, "%s(): %d\n", __func__, data->error);
  631. return false;
  632. }
  633. for (i = 0; i < host->blocksize / 4; i++)
  634. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  635. /* buffer write end */
  636. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  637. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  638. return true;
  639. }
  640. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  641. struct mmc_request *mrq)
  642. {
  643. struct mmc_data *data = mrq->data;
  644. if (!data->sg_len || !data->sg->length)
  645. return;
  646. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  647. BLOCK_SIZE_MASK;
  648. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  649. host->sg_idx = 0;
  650. host->sg_blkidx = 0;
  651. host->pio_ptr = sg_virt(data->sg);
  652. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  653. }
  654. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  655. {
  656. struct device *dev = sh_mmcif_host_to_dev(host);
  657. struct mmc_data *data = host->mrq->data;
  658. u32 *p = host->pio_ptr;
  659. int i;
  660. if (host->sd_error) {
  661. data->error = sh_mmcif_error_manage(host);
  662. dev_dbg(dev, "%s(): %d\n", __func__, data->error);
  663. return false;
  664. }
  665. BUG_ON(!data->sg->length);
  666. for (i = 0; i < host->blocksize / 4; i++)
  667. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  668. if (!sh_mmcif_next_block(host, p))
  669. return false;
  670. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  671. return true;
  672. }
  673. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  674. struct mmc_command *cmd)
  675. {
  676. if (cmd->flags & MMC_RSP_136) {
  677. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  678. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  679. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  680. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  681. } else
  682. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  683. }
  684. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  685. struct mmc_command *cmd)
  686. {
  687. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  688. }
  689. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  690. struct mmc_request *mrq)
  691. {
  692. struct device *dev = sh_mmcif_host_to_dev(host);
  693. struct mmc_data *data = mrq->data;
  694. struct mmc_command *cmd = mrq->cmd;
  695. u32 opc = cmd->opcode;
  696. u32 tmp = 0;
  697. /* Response Type check */
  698. switch (mmc_resp_type(cmd)) {
  699. case MMC_RSP_NONE:
  700. tmp |= CMD_SET_RTYP_NO;
  701. break;
  702. case MMC_RSP_R1:
  703. case MMC_RSP_R1B:
  704. case MMC_RSP_R3:
  705. tmp |= CMD_SET_RTYP_6B;
  706. break;
  707. case MMC_RSP_R2:
  708. tmp |= CMD_SET_RTYP_17B;
  709. break;
  710. default:
  711. dev_err(dev, "Unsupported response type.\n");
  712. break;
  713. }
  714. switch (opc) {
  715. /* RBSY */
  716. case MMC_SLEEP_AWAKE:
  717. case MMC_SWITCH:
  718. case MMC_STOP_TRANSMISSION:
  719. case MMC_SET_WRITE_PROT:
  720. case MMC_CLR_WRITE_PROT:
  721. case MMC_ERASE:
  722. tmp |= CMD_SET_RBSY;
  723. break;
  724. }
  725. /* WDAT / DATW */
  726. if (data) {
  727. tmp |= CMD_SET_WDAT;
  728. switch (host->bus_width) {
  729. case MMC_BUS_WIDTH_1:
  730. tmp |= CMD_SET_DATW_1;
  731. break;
  732. case MMC_BUS_WIDTH_4:
  733. tmp |= CMD_SET_DATW_4;
  734. break;
  735. case MMC_BUS_WIDTH_8:
  736. tmp |= CMD_SET_DATW_8;
  737. break;
  738. default:
  739. dev_err(dev, "Unsupported bus width.\n");
  740. break;
  741. }
  742. switch (host->timing) {
  743. case MMC_TIMING_MMC_DDR52:
  744. /*
  745. * MMC core will only set this timing, if the host
  746. * advertises the MMC_CAP_1_8V_DDR/MMC_CAP_1_2V_DDR
  747. * capability. MMCIF implementations with this
  748. * capability, e.g. sh73a0, will have to set it
  749. * in their platform data.
  750. */
  751. tmp |= CMD_SET_DARS;
  752. break;
  753. }
  754. }
  755. /* DWEN */
  756. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  757. tmp |= CMD_SET_DWEN;
  758. /* CMLTE/CMD12EN */
  759. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  760. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  761. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  762. data->blocks << 16);
  763. }
  764. /* RIDXC[1:0] check bits */
  765. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  766. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  767. tmp |= CMD_SET_RIDXC_BITS;
  768. /* RCRC7C[1:0] check bits */
  769. if (opc == MMC_SEND_OP_COND)
  770. tmp |= CMD_SET_CRC7C_BITS;
  771. /* RCRC7C[1:0] internal CRC7 */
  772. if (opc == MMC_ALL_SEND_CID ||
  773. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  774. tmp |= CMD_SET_CRC7C_INTERNAL;
  775. return (opc << 24) | tmp;
  776. }
  777. static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
  778. struct mmc_request *mrq, u32 opc)
  779. {
  780. struct device *dev = sh_mmcif_host_to_dev(host);
  781. switch (opc) {
  782. case MMC_READ_MULTIPLE_BLOCK:
  783. sh_mmcif_multi_read(host, mrq);
  784. return 0;
  785. case MMC_WRITE_MULTIPLE_BLOCK:
  786. sh_mmcif_multi_write(host, mrq);
  787. return 0;
  788. case MMC_WRITE_BLOCK:
  789. sh_mmcif_single_write(host, mrq);
  790. return 0;
  791. case MMC_READ_SINGLE_BLOCK:
  792. case MMC_SEND_EXT_CSD:
  793. sh_mmcif_single_read(host, mrq);
  794. return 0;
  795. default:
  796. dev_err(dev, "Unsupported CMD%d\n", opc);
  797. return -EINVAL;
  798. }
  799. }
  800. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  801. struct mmc_request *mrq)
  802. {
  803. struct mmc_command *cmd = mrq->cmd;
  804. u32 opc = cmd->opcode;
  805. u32 mask;
  806. unsigned long flags;
  807. switch (opc) {
  808. /* response busy check */
  809. case MMC_SLEEP_AWAKE:
  810. case MMC_SWITCH:
  811. case MMC_STOP_TRANSMISSION:
  812. case MMC_SET_WRITE_PROT:
  813. case MMC_CLR_WRITE_PROT:
  814. case MMC_ERASE:
  815. mask = MASK_START_CMD | MASK_MRBSYE;
  816. break;
  817. default:
  818. mask = MASK_START_CMD | MASK_MCRSPE;
  819. break;
  820. }
  821. if (host->ccs_enable)
  822. mask |= MASK_MCCSTO;
  823. if (mrq->data) {
  824. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  825. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  826. mrq->data->blksz);
  827. }
  828. opc = sh_mmcif_set_cmd(host, mrq);
  829. if (host->ccs_enable)
  830. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  831. else
  832. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0 | INT_CCS);
  833. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  834. /* set arg */
  835. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  836. /* set cmd */
  837. spin_lock_irqsave(&host->lock, flags);
  838. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  839. host->wait_for = MMCIF_WAIT_FOR_CMD;
  840. schedule_delayed_work(&host->timeout_work, host->timeout);
  841. spin_unlock_irqrestore(&host->lock, flags);
  842. }
  843. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  844. struct mmc_request *mrq)
  845. {
  846. struct device *dev = sh_mmcif_host_to_dev(host);
  847. switch (mrq->cmd->opcode) {
  848. case MMC_READ_MULTIPLE_BLOCK:
  849. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  850. break;
  851. case MMC_WRITE_MULTIPLE_BLOCK:
  852. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  853. break;
  854. default:
  855. dev_err(dev, "unsupported stop cmd\n");
  856. mrq->stop->error = sh_mmcif_error_manage(host);
  857. return;
  858. }
  859. host->wait_for = MMCIF_WAIT_FOR_STOP;
  860. }
  861. static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
  862. {
  863. struct sh_mmcif_host *host = mmc_priv(mmc);
  864. struct device *dev = sh_mmcif_host_to_dev(host);
  865. unsigned long flags;
  866. spin_lock_irqsave(&host->lock, flags);
  867. if (host->state != STATE_IDLE) {
  868. dev_dbg(dev, "%s() rejected, state %u\n",
  869. __func__, host->state);
  870. spin_unlock_irqrestore(&host->lock, flags);
  871. mrq->cmd->error = -EAGAIN;
  872. mmc_request_done(mmc, mrq);
  873. return;
  874. }
  875. host->state = STATE_REQUEST;
  876. spin_unlock_irqrestore(&host->lock, flags);
  877. switch (mrq->cmd->opcode) {
  878. /* MMCIF does not support SD/SDIO command */
  879. case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
  880. case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
  881. if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
  882. break;
  883. case MMC_APP_CMD:
  884. case SD_IO_RW_DIRECT:
  885. host->state = STATE_IDLE;
  886. mrq->cmd->error = -ETIMEDOUT;
  887. mmc_request_done(mmc, mrq);
  888. return;
  889. default:
  890. break;
  891. }
  892. host->mrq = mrq;
  893. sh_mmcif_start_cmd(host, mrq);
  894. }
  895. static void sh_mmcif_clk_setup(struct sh_mmcif_host *host)
  896. {
  897. struct device *dev = sh_mmcif_host_to_dev(host);
  898. if (host->mmc->f_max) {
  899. unsigned int f_max, f_min = 0, f_min_old;
  900. f_max = host->mmc->f_max;
  901. for (f_min_old = f_max; f_min_old > 2;) {
  902. f_min = clk_round_rate(host->clk, f_min_old / 2);
  903. if (f_min == f_min_old)
  904. break;
  905. f_min_old = f_min;
  906. }
  907. /*
  908. * This driver assumes this SoC is R-Car Gen2 or later
  909. */
  910. host->clkdiv_map = 0x3ff;
  911. host->mmc->f_max = f_max / (1 << ffs(host->clkdiv_map));
  912. host->mmc->f_min = f_min / (1 << fls(host->clkdiv_map));
  913. } else {
  914. unsigned int clk = clk_get_rate(host->clk);
  915. host->mmc->f_max = clk / 2;
  916. host->mmc->f_min = clk / 512;
  917. }
  918. dev_dbg(dev, "clk max/min = %d/%d\n",
  919. host->mmc->f_max, host->mmc->f_min);
  920. }
  921. static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
  922. {
  923. struct mmc_host *mmc = host->mmc;
  924. if (!IS_ERR(mmc->supply.vmmc))
  925. /* Errors ignored... */
  926. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  927. ios->power_mode ? ios->vdd : 0);
  928. }
  929. static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  930. {
  931. struct sh_mmcif_host *host = mmc_priv(mmc);
  932. struct device *dev = sh_mmcif_host_to_dev(host);
  933. unsigned long flags;
  934. spin_lock_irqsave(&host->lock, flags);
  935. if (host->state != STATE_IDLE) {
  936. dev_dbg(dev, "%s() rejected, state %u\n",
  937. __func__, host->state);
  938. spin_unlock_irqrestore(&host->lock, flags);
  939. return;
  940. }
  941. host->state = STATE_IOS;
  942. spin_unlock_irqrestore(&host->lock, flags);
  943. if (ios->power_mode == MMC_POWER_UP) {
  944. if (!host->card_present) {
  945. /* See if we also get DMA */
  946. sh_mmcif_request_dma(host);
  947. host->card_present = true;
  948. }
  949. sh_mmcif_set_power(host, ios);
  950. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  951. /* clock stop */
  952. sh_mmcif_clock_control(host, 0);
  953. if (ios->power_mode == MMC_POWER_OFF) {
  954. if (host->card_present) {
  955. sh_mmcif_release_dma(host);
  956. host->card_present = false;
  957. }
  958. }
  959. if (host->power) {
  960. pm_runtime_put_sync(dev);
  961. clk_disable_unprepare(host->clk);
  962. host->power = false;
  963. if (ios->power_mode == MMC_POWER_OFF)
  964. sh_mmcif_set_power(host, ios);
  965. }
  966. host->state = STATE_IDLE;
  967. return;
  968. }
  969. if (ios->clock) {
  970. if (!host->power) {
  971. clk_prepare_enable(host->clk);
  972. pm_runtime_get_sync(dev);
  973. host->power = true;
  974. sh_mmcif_sync_reset(host);
  975. }
  976. sh_mmcif_clock_control(host, ios->clock);
  977. }
  978. host->timing = ios->timing;
  979. host->bus_width = ios->bus_width;
  980. host->state = STATE_IDLE;
  981. }
  982. static int sh_mmcif_get_cd(struct mmc_host *mmc)
  983. {
  984. struct sh_mmcif_host *host = mmc_priv(mmc);
  985. struct device *dev = sh_mmcif_host_to_dev(host);
  986. struct sh_mmcif_plat_data *p = dev->platform_data;
  987. int ret = mmc_gpio_get_cd(mmc);
  988. if (ret >= 0)
  989. return ret;
  990. if (!p || !p->get_cd)
  991. return -ENOSYS;
  992. else
  993. return p->get_cd(host->pd);
  994. }
  995. static struct mmc_host_ops sh_mmcif_ops = {
  996. .request = sh_mmcif_request,
  997. .set_ios = sh_mmcif_set_ios,
  998. .get_cd = sh_mmcif_get_cd,
  999. };
  1000. static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
  1001. {
  1002. struct mmc_command *cmd = host->mrq->cmd;
  1003. struct mmc_data *data = host->mrq->data;
  1004. struct device *dev = sh_mmcif_host_to_dev(host);
  1005. long time;
  1006. if (host->sd_error) {
  1007. switch (cmd->opcode) {
  1008. case MMC_ALL_SEND_CID:
  1009. case MMC_SELECT_CARD:
  1010. case MMC_APP_CMD:
  1011. cmd->error = -ETIMEDOUT;
  1012. break;
  1013. default:
  1014. cmd->error = sh_mmcif_error_manage(host);
  1015. break;
  1016. }
  1017. dev_dbg(dev, "CMD%d error %d\n",
  1018. cmd->opcode, cmd->error);
  1019. host->sd_error = false;
  1020. return false;
  1021. }
  1022. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  1023. cmd->error = 0;
  1024. return false;
  1025. }
  1026. sh_mmcif_get_response(host, cmd);
  1027. if (!data)
  1028. return false;
  1029. /*
  1030. * Completion can be signalled from DMA callback and error, so, have to
  1031. * reset here, before setting .dma_active
  1032. */
  1033. init_completion(&host->dma_complete);
  1034. if (data->flags & MMC_DATA_READ) {
  1035. if (host->chan_rx)
  1036. sh_mmcif_start_dma_rx(host);
  1037. } else {
  1038. if (host->chan_tx)
  1039. sh_mmcif_start_dma_tx(host);
  1040. }
  1041. if (!host->dma_active) {
  1042. data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
  1043. return !data->error;
  1044. }
  1045. /* Running in the IRQ thread, can sleep */
  1046. time = wait_for_completion_interruptible_timeout(&host->dma_complete,
  1047. host->timeout);
  1048. if (data->flags & MMC_DATA_READ)
  1049. dma_unmap_sg(host->chan_rx->device->dev,
  1050. data->sg, data->sg_len,
  1051. DMA_FROM_DEVICE);
  1052. else
  1053. dma_unmap_sg(host->chan_tx->device->dev,
  1054. data->sg, data->sg_len,
  1055. DMA_TO_DEVICE);
  1056. if (host->sd_error) {
  1057. dev_err(host->mmc->parent,
  1058. "Error IRQ while waiting for DMA completion!\n");
  1059. /* Woken up by an error IRQ: abort DMA */
  1060. data->error = sh_mmcif_error_manage(host);
  1061. } else if (!time) {
  1062. dev_err(host->mmc->parent, "DMA timeout!\n");
  1063. data->error = -ETIMEDOUT;
  1064. } else if (time < 0) {
  1065. dev_err(host->mmc->parent,
  1066. "wait_for_completion_...() error %ld!\n", time);
  1067. data->error = time;
  1068. }
  1069. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
  1070. BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  1071. host->dma_active = false;
  1072. if (data->error) {
  1073. data->bytes_xfered = 0;
  1074. /* Abort DMA */
  1075. if (data->flags & MMC_DATA_READ)
  1076. dmaengine_terminate_all(host->chan_rx);
  1077. else
  1078. dmaengine_terminate_all(host->chan_tx);
  1079. }
  1080. return false;
  1081. }
  1082. static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
  1083. {
  1084. struct sh_mmcif_host *host = dev_id;
  1085. struct mmc_request *mrq;
  1086. struct device *dev = sh_mmcif_host_to_dev(host);
  1087. bool wait = false;
  1088. unsigned long flags;
  1089. int wait_work;
  1090. spin_lock_irqsave(&host->lock, flags);
  1091. wait_work = host->wait_for;
  1092. spin_unlock_irqrestore(&host->lock, flags);
  1093. cancel_delayed_work_sync(&host->timeout_work);
  1094. mutex_lock(&host->thread_lock);
  1095. mrq = host->mrq;
  1096. if (!mrq) {
  1097. dev_dbg(dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
  1098. host->state, host->wait_for);
  1099. mutex_unlock(&host->thread_lock);
  1100. return IRQ_HANDLED;
  1101. }
  1102. /*
  1103. * All handlers return true, if processing continues, and false, if the
  1104. * request has to be completed - successfully or not
  1105. */
  1106. switch (wait_work) {
  1107. case MMCIF_WAIT_FOR_REQUEST:
  1108. /* We're too late, the timeout has already kicked in */
  1109. mutex_unlock(&host->thread_lock);
  1110. return IRQ_HANDLED;
  1111. case MMCIF_WAIT_FOR_CMD:
  1112. /* Wait for data? */
  1113. wait = sh_mmcif_end_cmd(host);
  1114. break;
  1115. case MMCIF_WAIT_FOR_MREAD:
  1116. /* Wait for more data? */
  1117. wait = sh_mmcif_mread_block(host);
  1118. break;
  1119. case MMCIF_WAIT_FOR_READ:
  1120. /* Wait for data end? */
  1121. wait = sh_mmcif_read_block(host);
  1122. break;
  1123. case MMCIF_WAIT_FOR_MWRITE:
  1124. /* Wait data to write? */
  1125. wait = sh_mmcif_mwrite_block(host);
  1126. break;
  1127. case MMCIF_WAIT_FOR_WRITE:
  1128. /* Wait for data end? */
  1129. wait = sh_mmcif_write_block(host);
  1130. break;
  1131. case MMCIF_WAIT_FOR_STOP:
  1132. if (host->sd_error) {
  1133. mrq->stop->error = sh_mmcif_error_manage(host);
  1134. dev_dbg(dev, "%s(): %d\n", __func__, mrq->stop->error);
  1135. break;
  1136. }
  1137. sh_mmcif_get_cmd12response(host, mrq->stop);
  1138. mrq->stop->error = 0;
  1139. break;
  1140. case MMCIF_WAIT_FOR_READ_END:
  1141. case MMCIF_WAIT_FOR_WRITE_END:
  1142. if (host->sd_error) {
  1143. mrq->data->error = sh_mmcif_error_manage(host);
  1144. dev_dbg(dev, "%s(): %d\n", __func__, mrq->data->error);
  1145. }
  1146. break;
  1147. default:
  1148. BUG();
  1149. }
  1150. if (wait) {
  1151. schedule_delayed_work(&host->timeout_work, host->timeout);
  1152. /* Wait for more data */
  1153. mutex_unlock(&host->thread_lock);
  1154. return IRQ_HANDLED;
  1155. }
  1156. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1157. struct mmc_data *data = mrq->data;
  1158. if (!mrq->cmd->error && data && !data->error)
  1159. data->bytes_xfered =
  1160. data->blocks * data->blksz;
  1161. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1162. sh_mmcif_stop_cmd(host, mrq);
  1163. if (!mrq->stop->error) {
  1164. schedule_delayed_work(&host->timeout_work, host->timeout);
  1165. mutex_unlock(&host->thread_lock);
  1166. return IRQ_HANDLED;
  1167. }
  1168. }
  1169. }
  1170. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1171. host->state = STATE_IDLE;
  1172. host->mrq = NULL;
  1173. mmc_request_done(host->mmc, mrq);
  1174. mutex_unlock(&host->thread_lock);
  1175. return IRQ_HANDLED;
  1176. }
  1177. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1178. {
  1179. struct sh_mmcif_host *host = dev_id;
  1180. struct device *dev = sh_mmcif_host_to_dev(host);
  1181. u32 state, mask;
  1182. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1183. mask = sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK);
  1184. if (host->ccs_enable)
  1185. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(state & mask));
  1186. else
  1187. sh_mmcif_writel(host->addr, MMCIF_CE_INT, INT_CCS | ~(state & mask));
  1188. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN);
  1189. if (state & ~MASK_CLEAN)
  1190. dev_dbg(dev, "IRQ state = 0x%08x incompletely cleared\n",
  1191. state);
  1192. if (state & INT_ERR_STS || state & ~INT_ALL) {
  1193. host->sd_error = true;
  1194. dev_dbg(dev, "int err state = 0x%08x\n", state);
  1195. }
  1196. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1197. if (!host->mrq)
  1198. dev_dbg(dev, "NULL IRQ state = 0x%08x\n", state);
  1199. if (!host->dma_active)
  1200. return IRQ_WAKE_THREAD;
  1201. else if (host->sd_error)
  1202. sh_mmcif_dma_complete(host);
  1203. } else {
  1204. dev_dbg(dev, "Unexpected IRQ 0x%x\n", state);
  1205. }
  1206. return IRQ_HANDLED;
  1207. }
  1208. static void sh_mmcif_timeout_work(struct work_struct *work)
  1209. {
  1210. struct delayed_work *d = to_delayed_work(work);
  1211. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1212. struct mmc_request *mrq = host->mrq;
  1213. struct device *dev = sh_mmcif_host_to_dev(host);
  1214. unsigned long flags;
  1215. if (host->dying)
  1216. /* Don't run after mmc_remove_host() */
  1217. return;
  1218. spin_lock_irqsave(&host->lock, flags);
  1219. if (host->state == STATE_IDLE) {
  1220. spin_unlock_irqrestore(&host->lock, flags);
  1221. return;
  1222. }
  1223. dev_err(dev, "Timeout waiting for %u on CMD%u\n",
  1224. host->wait_for, mrq->cmd->opcode);
  1225. host->state = STATE_TIMEOUT;
  1226. spin_unlock_irqrestore(&host->lock, flags);
  1227. /*
  1228. * Handle races with cancel_delayed_work(), unless
  1229. * cancel_delayed_work_sync() is used
  1230. */
  1231. switch (host->wait_for) {
  1232. case MMCIF_WAIT_FOR_CMD:
  1233. mrq->cmd->error = sh_mmcif_error_manage(host);
  1234. break;
  1235. case MMCIF_WAIT_FOR_STOP:
  1236. mrq->stop->error = sh_mmcif_error_manage(host);
  1237. break;
  1238. case MMCIF_WAIT_FOR_MREAD:
  1239. case MMCIF_WAIT_FOR_MWRITE:
  1240. case MMCIF_WAIT_FOR_READ:
  1241. case MMCIF_WAIT_FOR_WRITE:
  1242. case MMCIF_WAIT_FOR_READ_END:
  1243. case MMCIF_WAIT_FOR_WRITE_END:
  1244. mrq->data->error = sh_mmcif_error_manage(host);
  1245. break;
  1246. default:
  1247. BUG();
  1248. }
  1249. host->state = STATE_IDLE;
  1250. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1251. host->mrq = NULL;
  1252. mmc_request_done(host->mmc, mrq);
  1253. }
  1254. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1255. {
  1256. struct device *dev = sh_mmcif_host_to_dev(host);
  1257. struct sh_mmcif_plat_data *pd = dev->platform_data;
  1258. struct mmc_host *mmc = host->mmc;
  1259. mmc_regulator_get_supply(mmc);
  1260. if (!pd)
  1261. return;
  1262. if (!mmc->ocr_avail)
  1263. mmc->ocr_avail = pd->ocr;
  1264. else if (pd->ocr)
  1265. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1266. }
  1267. static int sh_mmcif_probe(struct platform_device *pdev)
  1268. {
  1269. int ret = 0, irq[2];
  1270. struct mmc_host *mmc;
  1271. struct sh_mmcif_host *host;
  1272. struct device *dev = &pdev->dev;
  1273. struct sh_mmcif_plat_data *pd = dev->platform_data;
  1274. struct resource *res;
  1275. void __iomem *reg;
  1276. const char *name;
  1277. irq[0] = platform_get_irq(pdev, 0);
  1278. irq[1] = platform_get_irq(pdev, 1);
  1279. if (irq[0] < 0) {
  1280. dev_err(dev, "Get irq error\n");
  1281. return -ENXIO;
  1282. }
  1283. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1284. reg = devm_ioremap_resource(dev, res);
  1285. if (IS_ERR(reg))
  1286. return PTR_ERR(reg);
  1287. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), dev);
  1288. if (!mmc)
  1289. return -ENOMEM;
  1290. ret = mmc_of_parse(mmc);
  1291. if (ret < 0)
  1292. goto err_host;
  1293. host = mmc_priv(mmc);
  1294. host->mmc = mmc;
  1295. host->addr = reg;
  1296. host->timeout = msecs_to_jiffies(10000);
  1297. host->ccs_enable = !pd || !pd->ccs_unsupported;
  1298. host->clk_ctrl2_enable = pd && pd->clk_ctrl2_present;
  1299. host->pd = pdev;
  1300. spin_lock_init(&host->lock);
  1301. mmc->ops = &sh_mmcif_ops;
  1302. sh_mmcif_init_ocr(host);
  1303. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1304. if (pd && pd->caps)
  1305. mmc->caps |= pd->caps;
  1306. mmc->max_segs = 32;
  1307. mmc->max_blk_size = 512;
  1308. mmc->max_req_size = PAGE_SIZE * mmc->max_segs;
  1309. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1310. mmc->max_seg_size = mmc->max_req_size;
  1311. platform_set_drvdata(pdev, host);
  1312. pm_runtime_enable(dev);
  1313. host->power = false;
  1314. host->clk = devm_clk_get(dev, NULL);
  1315. if (IS_ERR(host->clk)) {
  1316. ret = PTR_ERR(host->clk);
  1317. dev_err(dev, "cannot get clock: %d\n", ret);
  1318. goto err_pm;
  1319. }
  1320. ret = clk_prepare_enable(host->clk);
  1321. if (ret < 0)
  1322. goto err_pm;
  1323. sh_mmcif_clk_setup(host);
  1324. ret = pm_runtime_resume(dev);
  1325. if (ret < 0)
  1326. goto err_clk;
  1327. INIT_DELAYED_WORK(&host->timeout_work, sh_mmcif_timeout_work);
  1328. sh_mmcif_sync_reset(host);
  1329. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1330. name = irq[1] < 0 ? dev_name(dev) : "sh_mmc:error";
  1331. ret = devm_request_threaded_irq(dev, irq[0], sh_mmcif_intr,
  1332. sh_mmcif_irqt, 0, name, host);
  1333. if (ret) {
  1334. dev_err(dev, "request_irq error (%s)\n", name);
  1335. goto err_clk;
  1336. }
  1337. if (irq[1] >= 0) {
  1338. ret = devm_request_threaded_irq(dev, irq[1],
  1339. sh_mmcif_intr, sh_mmcif_irqt,
  1340. 0, "sh_mmc:int", host);
  1341. if (ret) {
  1342. dev_err(dev, "request_irq error (sh_mmc:int)\n");
  1343. goto err_clk;
  1344. }
  1345. }
  1346. if (pd && pd->use_cd_gpio) {
  1347. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0);
  1348. if (ret < 0)
  1349. goto err_clk;
  1350. }
  1351. mutex_init(&host->thread_lock);
  1352. ret = mmc_add_host(mmc);
  1353. if (ret < 0)
  1354. goto err_clk;
  1355. dev_pm_qos_expose_latency_limit(dev, 100);
  1356. dev_info(dev, "Chip version 0x%04x, clock rate %luMHz\n",
  1357. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0xffff,
  1358. clk_get_rate(host->clk) / 1000000UL);
  1359. clk_disable_unprepare(host->clk);
  1360. return ret;
  1361. err_clk:
  1362. clk_disable_unprepare(host->clk);
  1363. err_pm:
  1364. pm_runtime_disable(dev);
  1365. err_host:
  1366. mmc_free_host(mmc);
  1367. return ret;
  1368. }
  1369. static int sh_mmcif_remove(struct platform_device *pdev)
  1370. {
  1371. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1372. host->dying = true;
  1373. clk_prepare_enable(host->clk);
  1374. pm_runtime_get_sync(&pdev->dev);
  1375. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1376. mmc_remove_host(host->mmc);
  1377. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1378. /*
  1379. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1380. * mmc_remove_host() call above. But swapping order doesn't help either
  1381. * (a query on the linux-mmc mailing list didn't bring any replies).
  1382. */
  1383. cancel_delayed_work_sync(&host->timeout_work);
  1384. clk_disable_unprepare(host->clk);
  1385. mmc_free_host(host->mmc);
  1386. pm_runtime_put_sync(&pdev->dev);
  1387. pm_runtime_disable(&pdev->dev);
  1388. return 0;
  1389. }
  1390. #ifdef CONFIG_PM_SLEEP
  1391. static int sh_mmcif_suspend(struct device *dev)
  1392. {
  1393. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1394. pm_runtime_get_sync(dev);
  1395. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1396. pm_runtime_put(dev);
  1397. return 0;
  1398. }
  1399. static int sh_mmcif_resume(struct device *dev)
  1400. {
  1401. return 0;
  1402. }
  1403. #endif
  1404. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1405. SET_SYSTEM_SLEEP_PM_OPS(sh_mmcif_suspend, sh_mmcif_resume)
  1406. };
  1407. static struct platform_driver sh_mmcif_driver = {
  1408. .probe = sh_mmcif_probe,
  1409. .remove = sh_mmcif_remove,
  1410. .driver = {
  1411. .name = DRIVER_NAME,
  1412. .pm = &sh_mmcif_dev_pm_ops,
  1413. .of_match_table = sh_mmcif_of_match,
  1414. },
  1415. };
  1416. module_platform_driver(sh_mmcif_driver);
  1417. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1418. MODULE_LICENSE("GPL");
  1419. MODULE_ALIAS("platform:" DRIVER_NAME);
  1420. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");