sh_mmcif.c 25 KB

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