sh_mmcif.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. #include <linux/dma-mapping.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/card.h>
  21. #include <linux/mmc/core.h>
  22. #include <linux/mmc/mmc.h>
  23. #include <linux/mmc/sdio.h>
  24. #include <linux/delay.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/clk.h>
  27. #include <linux/mmc/sh_mmcif.h>
  28. #define DRIVER_NAME "sh_mmcif"
  29. #define DRIVER_VERSION "2010-04-28"
  30. #define MMCIF_CE_CMD_SET 0x00000000
  31. #define MMCIF_CE_ARG 0x00000008
  32. #define MMCIF_CE_ARG_CMD12 0x0000000C
  33. #define MMCIF_CE_CMD_CTRL 0x00000010
  34. #define MMCIF_CE_BLOCK_SET 0x00000014
  35. #define MMCIF_CE_CLK_CTRL 0x00000018
  36. #define MMCIF_CE_BUF_ACC 0x0000001C
  37. #define MMCIF_CE_RESP3 0x00000020
  38. #define MMCIF_CE_RESP2 0x00000024
  39. #define MMCIF_CE_RESP1 0x00000028
  40. #define MMCIF_CE_RESP0 0x0000002C
  41. #define MMCIF_CE_RESP_CMD12 0x00000030
  42. #define MMCIF_CE_DATA 0x00000034
  43. #define MMCIF_CE_INT 0x00000040
  44. #define MMCIF_CE_INT_MASK 0x00000044
  45. #define MMCIF_CE_HOST_STS1 0x00000048
  46. #define MMCIF_CE_HOST_STS2 0x0000004C
  47. #define MMCIF_CE_VERSION 0x0000007C
  48. /* CE_CMD_SET */
  49. #define CMD_MASK 0x3f000000
  50. #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
  51. #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
  52. #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
  53. #define CMD_SET_RBSY (1 << 21) /* R1b */
  54. #define CMD_SET_CCSEN (1 << 20)
  55. #define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
  56. #define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
  57. #define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
  58. #define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
  59. #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
  60. #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
  61. #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
  62. #define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
  63. #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
  64. #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
  65. #define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
  66. #define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
  67. #define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
  68. #define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
  69. #define CMD_SET_CCSH (1 << 5)
  70. #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
  71. #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
  72. #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
  73. /* CE_CMD_CTRL */
  74. #define CMD_CTRL_BREAK (1 << 0)
  75. /* CE_BLOCK_SET */
  76. #define BLOCK_SIZE_MASK 0x0000ffff
  77. /* CE_CLK_CTRL */
  78. #define CLK_ENABLE (1 << 24) /* 1: output mmc clock */
  79. #define CLK_CLEAR ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
  80. #define CLK_SUP_PCLK ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
  81. #define SRSPTO_256 ((1 << 13) | (0 << 12)) /* resp timeout */
  82. #define SRBSYTO_29 ((1 << 11) | (1 << 10) | \
  83. (1 << 9) | (1 << 8)) /* resp busy timeout */
  84. #define SRWDTO_29 ((1 << 7) | (1 << 6) | \
  85. (1 << 5) | (1 << 4)) /* read/write timeout */
  86. #define SCCSTO_29 ((1 << 3) | (1 << 2) | \
  87. (1 << 1) | (1 << 0)) /* ccs timeout */
  88. /* CE_BUF_ACC */
  89. #define BUF_ACC_DMAWEN (1 << 25)
  90. #define BUF_ACC_DMAREN (1 << 24)
  91. #define BUF_ACC_BUSW_32 (0 << 17)
  92. #define BUF_ACC_BUSW_16 (1 << 17)
  93. #define BUF_ACC_ATYP (1 << 16)
  94. /* CE_INT */
  95. #define INT_CCSDE (1 << 29)
  96. #define INT_CMD12DRE (1 << 26)
  97. #define INT_CMD12RBE (1 << 25)
  98. #define INT_CMD12CRE (1 << 24)
  99. #define INT_DTRANE (1 << 23)
  100. #define INT_BUFRE (1 << 22)
  101. #define INT_BUFWEN (1 << 21)
  102. #define INT_BUFREN (1 << 20)
  103. #define INT_CCSRCV (1 << 19)
  104. #define INT_RBSYE (1 << 17)
  105. #define INT_CRSPE (1 << 16)
  106. #define INT_CMDVIO (1 << 15)
  107. #define INT_BUFVIO (1 << 14)
  108. #define INT_WDATERR (1 << 11)
  109. #define INT_RDATERR (1 << 10)
  110. #define INT_RIDXERR (1 << 9)
  111. #define INT_RSPERR (1 << 8)
  112. #define INT_CCSTO (1 << 5)
  113. #define INT_CRCSTO (1 << 4)
  114. #define INT_WDATTO (1 << 3)
  115. #define INT_RDATTO (1 << 2)
  116. #define INT_RBSYTO (1 << 1)
  117. #define INT_RSPTO (1 << 0)
  118. #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
  119. INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
  120. INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
  121. INT_RDATTO | INT_RBSYTO | INT_RSPTO)
  122. /* CE_INT_MASK */
  123. #define MASK_ALL 0x00000000
  124. #define MASK_MCCSDE (1 << 29)
  125. #define MASK_MCMD12DRE (1 << 26)
  126. #define MASK_MCMD12RBE (1 << 25)
  127. #define MASK_MCMD12CRE (1 << 24)
  128. #define MASK_MDTRANE (1 << 23)
  129. #define MASK_MBUFRE (1 << 22)
  130. #define MASK_MBUFWEN (1 << 21)
  131. #define MASK_MBUFREN (1 << 20)
  132. #define MASK_MCCSRCV (1 << 19)
  133. #define MASK_MRBSYE (1 << 17)
  134. #define MASK_MCRSPE (1 << 16)
  135. #define MASK_MCMDVIO (1 << 15)
  136. #define MASK_MBUFVIO (1 << 14)
  137. #define MASK_MWDATERR (1 << 11)
  138. #define MASK_MRDATERR (1 << 10)
  139. #define MASK_MRIDXERR (1 << 9)
  140. #define MASK_MRSPERR (1 << 8)
  141. #define MASK_MCCSTO (1 << 5)
  142. #define MASK_MCRCSTO (1 << 4)
  143. #define MASK_MWDATTO (1 << 3)
  144. #define MASK_MRDATTO (1 << 2)
  145. #define MASK_MRBSYTO (1 << 1)
  146. #define MASK_MRSPTO (1 << 0)
  147. /* CE_HOST_STS1 */
  148. #define STS1_CMDSEQ (1 << 31)
  149. /* CE_HOST_STS2 */
  150. #define STS2_CRCSTE (1 << 31)
  151. #define STS2_CRC16E (1 << 30)
  152. #define STS2_AC12CRCE (1 << 29)
  153. #define STS2_RSPCRC7E (1 << 28)
  154. #define STS2_CRCSTEBE (1 << 27)
  155. #define STS2_RDATEBE (1 << 26)
  156. #define STS2_AC12REBE (1 << 25)
  157. #define STS2_RSPEBE (1 << 24)
  158. #define STS2_AC12IDXE (1 << 23)
  159. #define STS2_RSPIDXE (1 << 22)
  160. #define STS2_CCSTO (1 << 15)
  161. #define STS2_RDATTO (1 << 14)
  162. #define STS2_DATBSYTO (1 << 13)
  163. #define STS2_CRCSTTO (1 << 12)
  164. #define STS2_AC12BSYTO (1 << 11)
  165. #define STS2_RSPBSYTO (1 << 10)
  166. #define STS2_AC12RSPTO (1 << 9)
  167. #define STS2_RSPTO (1 << 8)
  168. #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
  169. STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
  170. #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
  171. STS2_DATBSYTO | STS2_CRCSTTO | \
  172. STS2_AC12BSYTO | STS2_RSPBSYTO | \
  173. STS2_AC12RSPTO | STS2_RSPTO)
  174. /* CE_VERSION */
  175. #define SOFT_RST_ON (1 << 31)
  176. #define SOFT_RST_OFF (0 << 31)
  177. #define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
  178. #define CLKDEV_MMC_DATA 20000000 /* 20MHz */
  179. #define CLKDEV_INIT 400000 /* 400 KHz */
  180. struct sh_mmcif_host {
  181. struct mmc_host *mmc;
  182. struct mmc_data *data;
  183. struct mmc_command *cmd;
  184. struct platform_device *pd;
  185. struct clk *hclk;
  186. unsigned int clk;
  187. int bus_width;
  188. u16 wait_int;
  189. u16 sd_error;
  190. long timeout;
  191. void __iomem *addr;
  192. wait_queue_head_t intr_wait;
  193. };
  194. static inline u32 sh_mmcif_readl(struct sh_mmcif_host *host, unsigned int reg)
  195. {
  196. return readl(host->addr + reg);
  197. }
  198. static inline void sh_mmcif_writel(struct sh_mmcif_host *host,
  199. unsigned int reg, u32 val)
  200. {
  201. writel(val, host->addr + reg);
  202. }
  203. static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
  204. unsigned int reg, u32 val)
  205. {
  206. writel(val | sh_mmcif_readl(host, reg), host->addr + reg);
  207. }
  208. static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
  209. unsigned int reg, u32 val)
  210. {
  211. writel(~val & sh_mmcif_readl(host, reg), host->addr + reg);
  212. }
  213. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  214. {
  215. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  216. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  217. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  218. if (!clk)
  219. return;
  220. if (p->sup_pclk && clk == host->clk)
  221. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  222. else
  223. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  224. (ilog2(__rounddown_pow_of_two(host->clk / clk)) << 16));
  225. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  226. }
  227. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  228. {
  229. u32 tmp;
  230. tmp = 0x010f0000 & sh_mmcif_readl(host, MMCIF_CE_CLK_CTRL);
  231. sh_mmcif_writel(host, MMCIF_CE_VERSION, SOFT_RST_ON);
  232. sh_mmcif_writel(host, MMCIF_CE_VERSION, SOFT_RST_OFF);
  233. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  234. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
  235. /* byte swap on */
  236. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  237. }
  238. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  239. {
  240. u32 state1, state2;
  241. int ret, timeout = 10000000;
  242. host->sd_error = 0;
  243. host->wait_int = 0;
  244. state1 = sh_mmcif_readl(host, MMCIF_CE_HOST_STS1);
  245. state2 = sh_mmcif_readl(host, MMCIF_CE_HOST_STS2);
  246. pr_debug("%s: ERR HOST_STS1 = %08x\n", \
  247. DRIVER_NAME, sh_mmcif_readl(host, MMCIF_CE_HOST_STS1));
  248. pr_debug("%s: ERR HOST_STS2 = %08x\n", \
  249. DRIVER_NAME, sh_mmcif_readl(host, MMCIF_CE_HOST_STS2));
  250. if (state1 & STS1_CMDSEQ) {
  251. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  252. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  253. while (1) {
  254. timeout--;
  255. if (timeout < 0) {
  256. pr_err(DRIVER_NAME": Forceed end of " \
  257. "command sequence timeout err\n");
  258. return -EIO;
  259. }
  260. if (!(sh_mmcif_readl(host, MMCIF_CE_HOST_STS1)
  261. & STS1_CMDSEQ))
  262. break;
  263. mdelay(1);
  264. }
  265. sh_mmcif_sync_reset(host);
  266. pr_debug(DRIVER_NAME": Forced end of command sequence\n");
  267. return -EIO;
  268. }
  269. if (state2 & STS2_CRC_ERR) {
  270. pr_debug(DRIVER_NAME": Happened CRC error\n");
  271. ret = -EIO;
  272. } else if (state2 & STS2_TIMEOUT_ERR) {
  273. pr_debug(DRIVER_NAME": Happened Timeout error\n");
  274. ret = -ETIMEDOUT;
  275. } else {
  276. pr_debug(DRIVER_NAME": Happened End/Index error\n");
  277. ret = -EIO;
  278. }
  279. return ret;
  280. }
  281. static int sh_mmcif_single_read(struct sh_mmcif_host *host,
  282. struct mmc_request *mrq)
  283. {
  284. struct mmc_data *data = mrq->data;
  285. long time;
  286. u32 blocksize, i, *p = sg_virt(data->sg);
  287. host->wait_int = 0;
  288. /* buf read enable */
  289. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  290. time = wait_event_interruptible_timeout(host->intr_wait,
  291. host->wait_int == 1 ||
  292. host->sd_error == 1, host->timeout);
  293. if (host->wait_int != 1 && (time == 0 || host->sd_error != 0))
  294. return sh_mmcif_error_manage(host);
  295. host->wait_int = 0;
  296. blocksize = (BLOCK_SIZE_MASK &
  297. sh_mmcif_readl(host, MMCIF_CE_BLOCK_SET)) + 3;
  298. for (i = 0; i < blocksize / 4; i++)
  299. *p++ = sh_mmcif_readl(host, MMCIF_CE_DATA);
  300. /* buffer read end */
  301. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  302. time = wait_event_interruptible_timeout(host->intr_wait,
  303. host->wait_int == 1 ||
  304. host->sd_error == 1, host->timeout);
  305. if (host->wait_int != 1 && (time == 0 || host->sd_error != 0))
  306. return sh_mmcif_error_manage(host);
  307. host->wait_int = 0;
  308. return 0;
  309. }
  310. static int sh_mmcif_multi_read(struct sh_mmcif_host *host,
  311. struct mmc_request *mrq)
  312. {
  313. struct mmc_data *data = mrq->data;
  314. long time;
  315. u32 blocksize, i, j, sec, *p;
  316. blocksize = BLOCK_SIZE_MASK & sh_mmcif_readl(host, MMCIF_CE_BLOCK_SET);
  317. for (j = 0; j < data->sg_len; j++) {
  318. p = sg_virt(data->sg);
  319. host->wait_int = 0;
  320. for (sec = 0; sec < data->sg->length / blocksize; sec++) {
  321. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  322. /* buf read enable */
  323. time = wait_event_interruptible_timeout(host->intr_wait,
  324. host->wait_int == 1 ||
  325. host->sd_error == 1, host->timeout);
  326. if (host->wait_int != 1 &&
  327. (time == 0 || host->sd_error != 0))
  328. return sh_mmcif_error_manage(host);
  329. host->wait_int = 0;
  330. for (i = 0; i < blocksize / 4; i++)
  331. *p++ = sh_mmcif_readl(host, MMCIF_CE_DATA);
  332. }
  333. if (j < data->sg_len - 1)
  334. data->sg++;
  335. }
  336. return 0;
  337. }
  338. static int sh_mmcif_single_write(struct sh_mmcif_host *host,
  339. struct mmc_request *mrq)
  340. {
  341. struct mmc_data *data = mrq->data;
  342. long time;
  343. u32 blocksize, i, *p = sg_virt(data->sg);
  344. host->wait_int = 0;
  345. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  346. /* buf write enable */
  347. time = wait_event_interruptible_timeout(host->intr_wait,
  348. host->wait_int == 1 ||
  349. host->sd_error == 1, host->timeout);
  350. if (host->wait_int != 1 && (time == 0 || host->sd_error != 0))
  351. return sh_mmcif_error_manage(host);
  352. host->wait_int = 0;
  353. blocksize = (BLOCK_SIZE_MASK &
  354. sh_mmcif_readl(host, MMCIF_CE_BLOCK_SET)) + 3;
  355. for (i = 0; i < blocksize / 4; i++)
  356. sh_mmcif_writel(host, MMCIF_CE_DATA, *p++);
  357. /* buffer write end */
  358. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  359. time = wait_event_interruptible_timeout(host->intr_wait,
  360. host->wait_int == 1 ||
  361. host->sd_error == 1, host->timeout);
  362. if (host->wait_int != 1 && (time == 0 || host->sd_error != 0))
  363. return sh_mmcif_error_manage(host);
  364. host->wait_int = 0;
  365. return 0;
  366. }
  367. static int sh_mmcif_multi_write(struct sh_mmcif_host *host,
  368. struct mmc_request *mrq)
  369. {
  370. struct mmc_data *data = mrq->data;
  371. long time;
  372. u32 i, sec, j, blocksize, *p;
  373. blocksize = BLOCK_SIZE_MASK & sh_mmcif_readl(host, MMCIF_CE_BLOCK_SET);
  374. for (j = 0; j < data->sg_len; j++) {
  375. p = sg_virt(data->sg);
  376. host->wait_int = 0;
  377. for (sec = 0; sec < data->sg->length / blocksize; sec++) {
  378. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  379. /* buf write enable*/
  380. time = wait_event_interruptible_timeout(host->intr_wait,
  381. host->wait_int == 1 ||
  382. host->sd_error == 1, host->timeout);
  383. if (host->wait_int != 1 &&
  384. (time == 0 || host->sd_error != 0))
  385. return sh_mmcif_error_manage(host);
  386. host->wait_int = 0;
  387. for (i = 0; i < blocksize / 4; i++)
  388. sh_mmcif_writel(host, MMCIF_CE_DATA, *p++);
  389. }
  390. if (j < data->sg_len - 1)
  391. data->sg++;
  392. }
  393. return 0;
  394. }
  395. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  396. struct mmc_command *cmd)
  397. {
  398. if (cmd->flags & MMC_RSP_136) {
  399. cmd->resp[0] = sh_mmcif_readl(host, MMCIF_CE_RESP3);
  400. cmd->resp[1] = sh_mmcif_readl(host, MMCIF_CE_RESP2);
  401. cmd->resp[2] = sh_mmcif_readl(host, MMCIF_CE_RESP1);
  402. cmd->resp[3] = sh_mmcif_readl(host, MMCIF_CE_RESP0);
  403. } else
  404. cmd->resp[0] = sh_mmcif_readl(host, MMCIF_CE_RESP0);
  405. }
  406. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  407. struct mmc_command *cmd)
  408. {
  409. cmd->resp[0] = sh_mmcif_readl(host, MMCIF_CE_RESP_CMD12);
  410. }
  411. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  412. struct mmc_request *mrq, struct mmc_command *cmd, u32 opc)
  413. {
  414. u32 tmp = 0;
  415. /* Response Type check */
  416. switch (mmc_resp_type(cmd)) {
  417. case MMC_RSP_NONE:
  418. tmp |= CMD_SET_RTYP_NO;
  419. break;
  420. case MMC_RSP_R1:
  421. case MMC_RSP_R1B:
  422. case MMC_RSP_R3:
  423. tmp |= CMD_SET_RTYP_6B;
  424. break;
  425. case MMC_RSP_R2:
  426. tmp |= CMD_SET_RTYP_17B;
  427. break;
  428. default:
  429. pr_err(DRIVER_NAME": Not support type response.\n");
  430. break;
  431. }
  432. switch (opc) {
  433. /* RBSY */
  434. case MMC_SWITCH:
  435. case MMC_STOP_TRANSMISSION:
  436. case MMC_SET_WRITE_PROT:
  437. case MMC_CLR_WRITE_PROT:
  438. case MMC_ERASE:
  439. case MMC_GEN_CMD:
  440. tmp |= CMD_SET_RBSY;
  441. break;
  442. }
  443. /* WDAT / DATW */
  444. if (host->data) {
  445. tmp |= CMD_SET_WDAT;
  446. switch (host->bus_width) {
  447. case MMC_BUS_WIDTH_1:
  448. tmp |= CMD_SET_DATW_1;
  449. break;
  450. case MMC_BUS_WIDTH_4:
  451. tmp |= CMD_SET_DATW_4;
  452. break;
  453. case MMC_BUS_WIDTH_8:
  454. tmp |= CMD_SET_DATW_8;
  455. break;
  456. default:
  457. pr_err(DRIVER_NAME": Not support bus width.\n");
  458. break;
  459. }
  460. }
  461. /* DWEN */
  462. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  463. tmp |= CMD_SET_DWEN;
  464. /* CMLTE/CMD12EN */
  465. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  466. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  467. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  468. mrq->data->blocks << 16);
  469. }
  470. /* RIDXC[1:0] check bits */
  471. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  472. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  473. tmp |= CMD_SET_RIDXC_BITS;
  474. /* RCRC7C[1:0] check bits */
  475. if (opc == MMC_SEND_OP_COND)
  476. tmp |= CMD_SET_CRC7C_BITS;
  477. /* RCRC7C[1:0] internal CRC7 */
  478. if (opc == MMC_ALL_SEND_CID ||
  479. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  480. tmp |= CMD_SET_CRC7C_INTERNAL;
  481. return opc = ((opc << 24) | tmp);
  482. }
  483. static u32 sh_mmcif_data_trans(struct sh_mmcif_host *host,
  484. struct mmc_request *mrq, u32 opc)
  485. {
  486. u32 ret;
  487. switch (opc) {
  488. case MMC_READ_MULTIPLE_BLOCK:
  489. ret = sh_mmcif_multi_read(host, mrq);
  490. break;
  491. case MMC_WRITE_MULTIPLE_BLOCK:
  492. ret = sh_mmcif_multi_write(host, mrq);
  493. break;
  494. case MMC_WRITE_BLOCK:
  495. ret = sh_mmcif_single_write(host, mrq);
  496. break;
  497. case MMC_READ_SINGLE_BLOCK:
  498. case MMC_SEND_EXT_CSD:
  499. ret = sh_mmcif_single_read(host, mrq);
  500. break;
  501. default:
  502. pr_err(DRIVER_NAME": NOT SUPPORT CMD = d'%08d\n", opc);
  503. ret = -EINVAL;
  504. break;
  505. }
  506. return ret;
  507. }
  508. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  509. struct mmc_request *mrq, struct mmc_command *cmd)
  510. {
  511. long time;
  512. int ret = 0, mask = 0;
  513. u32 opc = cmd->opcode;
  514. host->cmd = cmd;
  515. switch (opc) {
  516. /* respons busy check */
  517. case MMC_SWITCH:
  518. case MMC_STOP_TRANSMISSION:
  519. case MMC_SET_WRITE_PROT:
  520. case MMC_CLR_WRITE_PROT:
  521. case MMC_ERASE:
  522. case MMC_GEN_CMD:
  523. mask = MASK_MRBSYE;
  524. break;
  525. default:
  526. mask = MASK_MCRSPE;
  527. break;
  528. }
  529. mask |= MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR |
  530. MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR |
  531. MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO |
  532. MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO;
  533. if (host->data) {
  534. sh_mmcif_writel(host, MMCIF_CE_BLOCK_SET, 0);
  535. sh_mmcif_writel(host, MMCIF_CE_BLOCK_SET, mrq->data->blksz);
  536. }
  537. opc = sh_mmcif_set_cmd(host, mrq, cmd, opc);
  538. sh_mmcif_writel(host, MMCIF_CE_INT, 0xD80430C0);
  539. sh_mmcif_writel(host, MMCIF_CE_INT_MASK, mask);
  540. /* set arg */
  541. sh_mmcif_writel(host, MMCIF_CE_ARG, cmd->arg);
  542. host->wait_int = 0;
  543. /* set cmd */
  544. sh_mmcif_writel(host, MMCIF_CE_CMD_SET, opc);
  545. time = wait_event_interruptible_timeout(host->intr_wait,
  546. host->wait_int == 1 || host->sd_error == 1, host->timeout);
  547. if (host->wait_int != 1 && time == 0) {
  548. cmd->error = sh_mmcif_error_manage(host);
  549. return;
  550. }
  551. if (host->sd_error) {
  552. switch (cmd->opcode) {
  553. case MMC_ALL_SEND_CID:
  554. case MMC_SELECT_CARD:
  555. case MMC_APP_CMD:
  556. cmd->error = -ETIMEDOUT;
  557. break;
  558. default:
  559. pr_debug("%s: Cmd(d'%d) err\n",
  560. DRIVER_NAME, cmd->opcode);
  561. cmd->error = sh_mmcif_error_manage(host);
  562. break;
  563. }
  564. host->sd_error = 0;
  565. host->wait_int = 0;
  566. return;
  567. }
  568. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  569. cmd->error = ret;
  570. host->wait_int = 0;
  571. return;
  572. }
  573. if (host->wait_int == 1) {
  574. sh_mmcif_get_response(host, cmd);
  575. host->wait_int = 0;
  576. }
  577. if (host->data) {
  578. ret = sh_mmcif_data_trans(host, mrq, cmd->opcode);
  579. if (ret < 0)
  580. mrq->data->bytes_xfered = 0;
  581. else
  582. mrq->data->bytes_xfered =
  583. mrq->data->blocks * mrq->data->blksz;
  584. }
  585. cmd->error = ret;
  586. }
  587. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  588. struct mmc_request *mrq, struct mmc_command *cmd)
  589. {
  590. long time;
  591. if (mrq->cmd->opcode == MMC_READ_MULTIPLE_BLOCK)
  592. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  593. else if (mrq->cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)
  594. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  595. else {
  596. pr_err(DRIVER_NAME": not support stop cmd\n");
  597. cmd->error = sh_mmcif_error_manage(host);
  598. return;
  599. }
  600. time = wait_event_interruptible_timeout(host->intr_wait,
  601. host->wait_int == 1 ||
  602. host->sd_error == 1, host->timeout);
  603. if (host->wait_int != 1 && (time == 0 || host->sd_error != 0)) {
  604. cmd->error = sh_mmcif_error_manage(host);
  605. return;
  606. }
  607. sh_mmcif_get_cmd12response(host, cmd);
  608. host->wait_int = 0;
  609. cmd->error = 0;
  610. }
  611. static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
  612. {
  613. struct sh_mmcif_host *host = mmc_priv(mmc);
  614. switch (mrq->cmd->opcode) {
  615. /* MMCIF does not support SD/SDIO command */
  616. case SD_IO_SEND_OP_COND:
  617. case MMC_APP_CMD:
  618. mrq->cmd->error = -ETIMEDOUT;
  619. mmc_request_done(mmc, mrq);
  620. return;
  621. case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
  622. if (!mrq->data) {
  623. /* send_if_cond cmd (not support) */
  624. mrq->cmd->error = -ETIMEDOUT;
  625. mmc_request_done(mmc, mrq);
  626. return;
  627. }
  628. break;
  629. default:
  630. break;
  631. }
  632. host->data = mrq->data;
  633. sh_mmcif_start_cmd(host, mrq, mrq->cmd);
  634. host->data = NULL;
  635. if (mrq->cmd->error != 0) {
  636. mmc_request_done(mmc, mrq);
  637. return;
  638. }
  639. if (mrq->stop)
  640. sh_mmcif_stop_cmd(host, mrq, mrq->stop);
  641. mmc_request_done(mmc, mrq);
  642. }
  643. static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  644. {
  645. struct sh_mmcif_host *host = mmc_priv(mmc);
  646. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  647. if (ios->power_mode == MMC_POWER_OFF) {
  648. /* clock stop */
  649. sh_mmcif_clock_control(host, 0);
  650. if (p->down_pwr)
  651. p->down_pwr(host->pd);
  652. return;
  653. } else if (ios->power_mode == MMC_POWER_UP) {
  654. if (p->set_pwr)
  655. p->set_pwr(host->pd, ios->power_mode);
  656. }
  657. if (ios->clock)
  658. sh_mmcif_clock_control(host, ios->clock);
  659. host->bus_width = ios->bus_width;
  660. }
  661. static struct mmc_host_ops sh_mmcif_ops = {
  662. .request = sh_mmcif_request,
  663. .set_ios = sh_mmcif_set_ios,
  664. };
  665. static void sh_mmcif_detect(struct mmc_host *mmc)
  666. {
  667. mmc_detect_change(mmc, 0);
  668. }
  669. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  670. {
  671. struct sh_mmcif_host *host = dev_id;
  672. u32 state = 0;
  673. int err = 0;
  674. state = sh_mmcif_readl(host, MMCIF_CE_INT);
  675. if (state & INT_RBSYE) {
  676. sh_mmcif_writel(host, MMCIF_CE_INT, ~(INT_RBSYE | INT_CRSPE));
  677. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE);
  678. } else if (state & INT_CRSPE) {
  679. sh_mmcif_writel(host, MMCIF_CE_INT, ~INT_CRSPE);
  680. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE);
  681. } else if (state & INT_BUFREN) {
  682. sh_mmcif_writel(host, MMCIF_CE_INT, ~INT_BUFREN);
  683. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  684. } else if (state & INT_BUFWEN) {
  685. sh_mmcif_writel(host, MMCIF_CE_INT, ~INT_BUFWEN);
  686. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  687. } else if (state & INT_CMD12DRE) {
  688. sh_mmcif_writel(host, MMCIF_CE_INT,
  689. ~(INT_CMD12DRE | INT_CMD12RBE |
  690. INT_CMD12CRE | INT_BUFRE));
  691. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  692. } else if (state & INT_BUFRE) {
  693. sh_mmcif_writel(host, MMCIF_CE_INT, ~INT_BUFRE);
  694. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  695. } else if (state & INT_DTRANE) {
  696. sh_mmcif_writel(host, MMCIF_CE_INT, ~INT_DTRANE);
  697. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  698. } else if (state & INT_CMD12RBE) {
  699. sh_mmcif_writel(host, MMCIF_CE_INT,
  700. ~(INT_CMD12RBE | INT_CMD12CRE));
  701. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  702. } else if (state & INT_ERR_STS) {
  703. /* err interrupts */
  704. sh_mmcif_writel(host, MMCIF_CE_INT, ~state);
  705. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  706. err = 1;
  707. } else {
  708. pr_debug("%s: Not support int\n", DRIVER_NAME);
  709. sh_mmcif_writel(host, MMCIF_CE_INT, ~state);
  710. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  711. err = 1;
  712. }
  713. if (err) {
  714. host->sd_error = 1;
  715. pr_debug("%s: int err state = %08x\n", DRIVER_NAME, state);
  716. }
  717. host->wait_int = 1;
  718. wake_up(&host->intr_wait);
  719. return IRQ_HANDLED;
  720. }
  721. static int __devinit sh_mmcif_probe(struct platform_device *pdev)
  722. {
  723. int ret = 0, irq[2];
  724. struct mmc_host *mmc;
  725. struct sh_mmcif_host *host = NULL;
  726. struct sh_mmcif_plat_data *pd = NULL;
  727. struct resource *res;
  728. void __iomem *reg;
  729. char clk_name[8];
  730. irq[0] = platform_get_irq(pdev, 0);
  731. irq[1] = platform_get_irq(pdev, 1);
  732. if (irq[0] < 0 || irq[1] < 0) {
  733. pr_err(DRIVER_NAME": Get irq error\n");
  734. return -ENXIO;
  735. }
  736. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  737. if (!res) {
  738. dev_err(&pdev->dev, "platform_get_resource error.\n");
  739. return -ENXIO;
  740. }
  741. reg = ioremap(res->start, resource_size(res));
  742. if (!reg) {
  743. dev_err(&pdev->dev, "ioremap error.\n");
  744. return -ENOMEM;
  745. }
  746. pd = (struct sh_mmcif_plat_data *)(pdev->dev.platform_data);
  747. if (!pd) {
  748. dev_err(&pdev->dev, "sh_mmcif plat data error.\n");
  749. ret = -ENXIO;
  750. goto clean_up;
  751. }
  752. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  753. if (!mmc) {
  754. ret = -ENOMEM;
  755. goto clean_up;
  756. }
  757. host = mmc_priv(mmc);
  758. host->mmc = mmc;
  759. host->addr = reg;
  760. host->timeout = 1000;
  761. snprintf(clk_name, sizeof(clk_name), "mmc%d", pdev->id);
  762. host->hclk = clk_get(&pdev->dev, clk_name);
  763. if (IS_ERR(host->hclk)) {
  764. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  765. ret = PTR_ERR(host->hclk);
  766. goto clean_up1;
  767. }
  768. clk_enable(host->hclk);
  769. host->clk = clk_get_rate(host->hclk);
  770. host->pd = pdev;
  771. init_waitqueue_head(&host->intr_wait);
  772. mmc->ops = &sh_mmcif_ops;
  773. mmc->f_max = host->clk;
  774. /* close to 400KHz */
  775. if (mmc->f_max < 51200000)
  776. mmc->f_min = mmc->f_max / 128;
  777. else if (mmc->f_max < 102400000)
  778. mmc->f_min = mmc->f_max / 256;
  779. else
  780. mmc->f_min = mmc->f_max / 512;
  781. if (pd->ocr)
  782. mmc->ocr_avail = pd->ocr;
  783. mmc->caps = MMC_CAP_MMC_HIGHSPEED;
  784. if (pd->caps)
  785. mmc->caps |= pd->caps;
  786. mmc->max_phys_segs = 128;
  787. mmc->max_hw_segs = 128;
  788. mmc->max_blk_size = 512;
  789. mmc->max_blk_count = 65535;
  790. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  791. mmc->max_seg_size = mmc->max_req_size;
  792. sh_mmcif_sync_reset(host);
  793. platform_set_drvdata(pdev, host);
  794. mmc_add_host(mmc);
  795. ret = request_irq(irq[0], sh_mmcif_intr, 0, "sh_mmc:error", host);
  796. if (ret) {
  797. pr_err(DRIVER_NAME": request_irq error (sh_mmc:error)\n");
  798. goto clean_up2;
  799. }
  800. ret = request_irq(irq[1], sh_mmcif_intr, 0, "sh_mmc:int", host);
  801. if (ret) {
  802. free_irq(irq[0], host);
  803. pr_err(DRIVER_NAME": request_irq error (sh_mmc:int)\n");
  804. goto clean_up2;
  805. }
  806. sh_mmcif_writel(host, MMCIF_CE_INT_MASK, MASK_ALL);
  807. sh_mmcif_detect(host->mmc);
  808. pr_info("%s: driver version %s\n", DRIVER_NAME, DRIVER_VERSION);
  809. pr_debug("%s: chip ver H'%04x\n", DRIVER_NAME,
  810. sh_mmcif_readl(host, MMCIF_CE_VERSION) & 0x0000ffff);
  811. return ret;
  812. clean_up2:
  813. clk_disable(host->hclk);
  814. clean_up1:
  815. mmc_free_host(mmc);
  816. clean_up:
  817. if (reg)
  818. iounmap(reg);
  819. return ret;
  820. }
  821. static int __devexit sh_mmcif_remove(struct platform_device *pdev)
  822. {
  823. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  824. int irq[2];
  825. sh_mmcif_writel(host, MMCIF_CE_INT_MASK, MASK_ALL);
  826. irq[0] = platform_get_irq(pdev, 0);
  827. irq[1] = platform_get_irq(pdev, 1);
  828. if (host->addr)
  829. iounmap(host->addr);
  830. platform_set_drvdata(pdev, NULL);
  831. mmc_remove_host(host->mmc);
  832. free_irq(irq[0], host);
  833. free_irq(irq[1], host);
  834. clk_disable(host->hclk);
  835. mmc_free_host(host->mmc);
  836. return 0;
  837. }
  838. static struct platform_driver sh_mmcif_driver = {
  839. .probe = sh_mmcif_probe,
  840. .remove = sh_mmcif_remove,
  841. .driver = {
  842. .name = DRIVER_NAME,
  843. },
  844. };
  845. static int __init sh_mmcif_init(void)
  846. {
  847. return platform_driver_register(&sh_mmcif_driver);
  848. }
  849. static void __exit sh_mmcif_exit(void)
  850. {
  851. platform_driver_unregister(&sh_mmcif_driver);
  852. }
  853. module_init(sh_mmcif_init);
  854. module_exit(sh_mmcif_exit);
  855. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  856. MODULE_LICENSE("GPL");
  857. MODULE_ALIAS(DRIVER_NAME);
  858. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");