sh_mmcif.c 30 KB

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