mvsdio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * Marvell MMC/SD/SDIO driver
  3. *
  4. * Authors: Maen Suleiman, Nicolas Pitre
  5. * Copyright (C) 2008-2009 Marvell Ltd.
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mbus.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/irq.h>
  21. #include <linux/gpio.h>
  22. #include <linux/mmc/host.h>
  23. #include <asm/sizes.h>
  24. #include <asm/unaligned.h>
  25. #include <plat/mvsdio.h>
  26. #include "mvsdio.h"
  27. #define DRIVER_NAME "mvsdio"
  28. static int maxfreq = MVSD_CLOCKRATE_MAX;
  29. static int nodma;
  30. struct mvsd_host {
  31. void __iomem *base;
  32. struct mmc_request *mrq;
  33. spinlock_t lock;
  34. unsigned int xfer_mode;
  35. unsigned int intr_en;
  36. unsigned int ctrl;
  37. unsigned int pio_size;
  38. void *pio_ptr;
  39. unsigned int sg_frags;
  40. unsigned int ns_per_clk;
  41. unsigned int clock;
  42. unsigned int base_clock;
  43. struct timer_list timer;
  44. struct mmc_host *mmc;
  45. struct device *dev;
  46. struct resource *res;
  47. int irq;
  48. int gpio_card_detect;
  49. int gpio_write_protect;
  50. };
  51. #define mvsd_write(offs, val) writel(val, iobase + (offs))
  52. #define mvsd_read(offs) readl(iobase + (offs))
  53. static int mvsd_setup_data(struct mvsd_host *host, struct mmc_data *data)
  54. {
  55. void __iomem *iobase = host->base;
  56. unsigned int tmout;
  57. int tmout_index;
  58. /*
  59. * Hardware weirdness. The FIFO_EMPTY bit of the HW_STATE
  60. * register is sometimes not set before a while when some
  61. * "unusual" data block sizes are used (such as with the SWITCH
  62. * command), even despite the fact that the XFER_DONE interrupt
  63. * was raised. And if another data transfer starts before
  64. * this bit comes to good sense (which eventually happens by
  65. * itself) then the new transfer simply fails with a timeout.
  66. */
  67. if (!(mvsd_read(MVSD_HW_STATE) & (1 << 13))) {
  68. unsigned long t = jiffies + HZ;
  69. unsigned int hw_state, count = 0;
  70. do {
  71. if (time_after(jiffies, t)) {
  72. dev_warn(host->dev, "FIFO_EMPTY bit missing\n");
  73. break;
  74. }
  75. hw_state = mvsd_read(MVSD_HW_STATE);
  76. count++;
  77. } while (!(hw_state & (1 << 13)));
  78. dev_dbg(host->dev, "*** wait for FIFO_EMPTY bit "
  79. "(hw=0x%04x, count=%d, jiffies=%ld)\n",
  80. hw_state, count, jiffies - (t - HZ));
  81. }
  82. /* If timeout=0 then maximum timeout index is used. */
  83. tmout = DIV_ROUND_UP(data->timeout_ns, host->ns_per_clk);
  84. tmout += data->timeout_clks;
  85. tmout_index = fls(tmout - 1) - 12;
  86. if (tmout_index < 0)
  87. tmout_index = 0;
  88. if (tmout_index > MVSD_HOST_CTRL_TMOUT_MAX)
  89. tmout_index = MVSD_HOST_CTRL_TMOUT_MAX;
  90. dev_dbg(host->dev, "data %s at 0x%08x: blocks=%d blksz=%d tmout=%u (%d)\n",
  91. (data->flags & MMC_DATA_READ) ? "read" : "write",
  92. (u32)sg_virt(data->sg), data->blocks, data->blksz,
  93. tmout, tmout_index);
  94. host->ctrl &= ~MVSD_HOST_CTRL_TMOUT_MASK;
  95. host->ctrl |= MVSD_HOST_CTRL_TMOUT(tmout_index);
  96. mvsd_write(MVSD_HOST_CTRL, host->ctrl);
  97. mvsd_write(MVSD_BLK_COUNT, data->blocks);
  98. mvsd_write(MVSD_BLK_SIZE, data->blksz);
  99. if (nodma || (data->blksz | data->sg->offset) & 3) {
  100. /*
  101. * We cannot do DMA on a buffer which offset or size
  102. * is not aligned on a 4-byte boundary.
  103. */
  104. host->pio_size = data->blocks * data->blksz;
  105. host->pio_ptr = sg_virt(data->sg);
  106. if (!nodma)
  107. pr_debug("%s: fallback to PIO for data "
  108. "at 0x%p size %d\n",
  109. mmc_hostname(host->mmc),
  110. host->pio_ptr, host->pio_size);
  111. return 1;
  112. } else {
  113. dma_addr_t phys_addr;
  114. int dma_dir = (data->flags & MMC_DATA_READ) ?
  115. DMA_FROM_DEVICE : DMA_TO_DEVICE;
  116. host->sg_frags = dma_map_sg(mmc_dev(host->mmc), data->sg,
  117. data->sg_len, dma_dir);
  118. phys_addr = sg_dma_address(data->sg);
  119. mvsd_write(MVSD_SYS_ADDR_LOW, (u32)phys_addr & 0xffff);
  120. mvsd_write(MVSD_SYS_ADDR_HI, (u32)phys_addr >> 16);
  121. return 0;
  122. }
  123. }
  124. static void mvsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  125. {
  126. struct mvsd_host *host = mmc_priv(mmc);
  127. void __iomem *iobase = host->base;
  128. struct mmc_command *cmd = mrq->cmd;
  129. u32 cmdreg = 0, xfer = 0, intr = 0;
  130. unsigned long flags;
  131. BUG_ON(host->mrq != NULL);
  132. host->mrq = mrq;
  133. dev_dbg(host->dev, "cmd %d (hw state 0x%04x)\n",
  134. cmd->opcode, mvsd_read(MVSD_HW_STATE));
  135. cmdreg = MVSD_CMD_INDEX(cmd->opcode);
  136. if (cmd->flags & MMC_RSP_BUSY)
  137. cmdreg |= MVSD_CMD_RSP_48BUSY;
  138. else if (cmd->flags & MMC_RSP_136)
  139. cmdreg |= MVSD_CMD_RSP_136;
  140. else if (cmd->flags & MMC_RSP_PRESENT)
  141. cmdreg |= MVSD_CMD_RSP_48;
  142. else
  143. cmdreg |= MVSD_CMD_RSP_NONE;
  144. if (cmd->flags & MMC_RSP_CRC)
  145. cmdreg |= MVSD_CMD_CHECK_CMDCRC;
  146. if (cmd->flags & MMC_RSP_OPCODE)
  147. cmdreg |= MVSD_CMD_INDX_CHECK;
  148. if (cmd->flags & MMC_RSP_PRESENT) {
  149. cmdreg |= MVSD_UNEXPECTED_RESP;
  150. intr |= MVSD_NOR_UNEXP_RSP;
  151. }
  152. if (mrq->data) {
  153. struct mmc_data *data = mrq->data;
  154. int pio;
  155. cmdreg |= MVSD_CMD_DATA_PRESENT | MVSD_CMD_CHECK_DATACRC16;
  156. xfer |= MVSD_XFER_MODE_HW_WR_DATA_EN;
  157. if (data->flags & MMC_DATA_READ)
  158. xfer |= MVSD_XFER_MODE_TO_HOST;
  159. pio = mvsd_setup_data(host, data);
  160. if (pio) {
  161. xfer |= MVSD_XFER_MODE_PIO;
  162. /* PIO section of mvsd_irq has comments on those bits */
  163. if (data->flags & MMC_DATA_WRITE)
  164. intr |= MVSD_NOR_TX_AVAIL;
  165. else if (host->pio_size > 32)
  166. intr |= MVSD_NOR_RX_FIFO_8W;
  167. else
  168. intr |= MVSD_NOR_RX_READY;
  169. }
  170. if (data->stop) {
  171. struct mmc_command *stop = data->stop;
  172. u32 cmd12reg = 0;
  173. mvsd_write(MVSD_AUTOCMD12_ARG_LOW, stop->arg & 0xffff);
  174. mvsd_write(MVSD_AUTOCMD12_ARG_HI, stop->arg >> 16);
  175. if (stop->flags & MMC_RSP_BUSY)
  176. cmd12reg |= MVSD_AUTOCMD12_BUSY;
  177. if (stop->flags & MMC_RSP_OPCODE)
  178. cmd12reg |= MVSD_AUTOCMD12_INDX_CHECK;
  179. cmd12reg |= MVSD_AUTOCMD12_INDEX(stop->opcode);
  180. mvsd_write(MVSD_AUTOCMD12_CMD, cmd12reg);
  181. xfer |= MVSD_XFER_MODE_AUTO_CMD12;
  182. intr |= MVSD_NOR_AUTOCMD12_DONE;
  183. } else {
  184. intr |= MVSD_NOR_XFER_DONE;
  185. }
  186. } else {
  187. intr |= MVSD_NOR_CMD_DONE;
  188. }
  189. mvsd_write(MVSD_ARG_LOW, cmd->arg & 0xffff);
  190. mvsd_write(MVSD_ARG_HI, cmd->arg >> 16);
  191. spin_lock_irqsave(&host->lock, flags);
  192. host->xfer_mode &= MVSD_XFER_MODE_INT_CHK_EN;
  193. host->xfer_mode |= xfer;
  194. mvsd_write(MVSD_XFER_MODE, host->xfer_mode);
  195. mvsd_write(MVSD_NOR_INTR_STATUS, ~MVSD_NOR_CARD_INT);
  196. mvsd_write(MVSD_ERR_INTR_STATUS, 0xffff);
  197. mvsd_write(MVSD_CMD, cmdreg);
  198. host->intr_en &= MVSD_NOR_CARD_INT;
  199. host->intr_en |= intr | MVSD_NOR_ERROR;
  200. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  201. mvsd_write(MVSD_ERR_INTR_EN, 0xffff);
  202. mod_timer(&host->timer, jiffies + 5 * HZ);
  203. spin_unlock_irqrestore(&host->lock, flags);
  204. }
  205. static u32 mvsd_finish_cmd(struct mvsd_host *host, struct mmc_command *cmd,
  206. u32 err_status)
  207. {
  208. void __iomem *iobase = host->base;
  209. if (cmd->flags & MMC_RSP_136) {
  210. unsigned int response[8], i;
  211. for (i = 0; i < 8; i++)
  212. response[i] = mvsd_read(MVSD_RSP(i));
  213. cmd->resp[0] = ((response[0] & 0x03ff) << 22) |
  214. ((response[1] & 0xffff) << 6) |
  215. ((response[2] & 0xfc00) >> 10);
  216. cmd->resp[1] = ((response[2] & 0x03ff) << 22) |
  217. ((response[3] & 0xffff) << 6) |
  218. ((response[4] & 0xfc00) >> 10);
  219. cmd->resp[2] = ((response[4] & 0x03ff) << 22) |
  220. ((response[5] & 0xffff) << 6) |
  221. ((response[6] & 0xfc00) >> 10);
  222. cmd->resp[3] = ((response[6] & 0x03ff) << 22) |
  223. ((response[7] & 0x3fff) << 8);
  224. } else if (cmd->flags & MMC_RSP_PRESENT) {
  225. unsigned int response[3], i;
  226. for (i = 0; i < 3; i++)
  227. response[i] = mvsd_read(MVSD_RSP(i));
  228. cmd->resp[0] = ((response[2] & 0x003f) << (8 - 8)) |
  229. ((response[1] & 0xffff) << (14 - 8)) |
  230. ((response[0] & 0x03ff) << (30 - 8));
  231. cmd->resp[1] = ((response[0] & 0xfc00) >> 10);
  232. cmd->resp[2] = 0;
  233. cmd->resp[3] = 0;
  234. }
  235. if (err_status & MVSD_ERR_CMD_TIMEOUT) {
  236. cmd->error = -ETIMEDOUT;
  237. } else if (err_status & (MVSD_ERR_CMD_CRC | MVSD_ERR_CMD_ENDBIT |
  238. MVSD_ERR_CMD_INDEX | MVSD_ERR_CMD_STARTBIT)) {
  239. cmd->error = -EILSEQ;
  240. }
  241. err_status &= ~(MVSD_ERR_CMD_TIMEOUT | MVSD_ERR_CMD_CRC |
  242. MVSD_ERR_CMD_ENDBIT | MVSD_ERR_CMD_INDEX |
  243. MVSD_ERR_CMD_STARTBIT);
  244. return err_status;
  245. }
  246. static u32 mvsd_finish_data(struct mvsd_host *host, struct mmc_data *data,
  247. u32 err_status)
  248. {
  249. void __iomem *iobase = host->base;
  250. if (host->pio_ptr) {
  251. host->pio_ptr = NULL;
  252. host->pio_size = 0;
  253. } else {
  254. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_frags,
  255. (data->flags & MMC_DATA_READ) ?
  256. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  257. }
  258. if (err_status & MVSD_ERR_DATA_TIMEOUT)
  259. data->error = -ETIMEDOUT;
  260. else if (err_status & (MVSD_ERR_DATA_CRC | MVSD_ERR_DATA_ENDBIT))
  261. data->error = -EILSEQ;
  262. else if (err_status & MVSD_ERR_XFER_SIZE)
  263. data->error = -EBADE;
  264. err_status &= ~(MVSD_ERR_DATA_TIMEOUT | MVSD_ERR_DATA_CRC |
  265. MVSD_ERR_DATA_ENDBIT | MVSD_ERR_XFER_SIZE);
  266. dev_dbg(host->dev, "data done: blocks_left=%d, bytes_left=%d\n",
  267. mvsd_read(MVSD_CURR_BLK_LEFT), mvsd_read(MVSD_CURR_BYTE_LEFT));
  268. data->bytes_xfered =
  269. (data->blocks - mvsd_read(MVSD_CURR_BLK_LEFT)) * data->blksz;
  270. /* We can't be sure about the last block when errors are detected */
  271. if (data->bytes_xfered && data->error)
  272. data->bytes_xfered -= data->blksz;
  273. /* Handle Auto cmd 12 response */
  274. if (data->stop) {
  275. unsigned int response[3], i;
  276. for (i = 0; i < 3; i++)
  277. response[i] = mvsd_read(MVSD_AUTO_RSP(i));
  278. data->stop->resp[0] = ((response[2] & 0x003f) << (8 - 8)) |
  279. ((response[1] & 0xffff) << (14 - 8)) |
  280. ((response[0] & 0x03ff) << (30 - 8));
  281. data->stop->resp[1] = ((response[0] & 0xfc00) >> 10);
  282. data->stop->resp[2] = 0;
  283. data->stop->resp[3] = 0;
  284. if (err_status & MVSD_ERR_AUTOCMD12) {
  285. u32 err_cmd12 = mvsd_read(MVSD_AUTOCMD12_ERR_STATUS);
  286. dev_dbg(host->dev, "c12err 0x%04x\n", err_cmd12);
  287. if (err_cmd12 & MVSD_AUTOCMD12_ERR_NOTEXE)
  288. data->stop->error = -ENOEXEC;
  289. else if (err_cmd12 & MVSD_AUTOCMD12_ERR_TIMEOUT)
  290. data->stop->error = -ETIMEDOUT;
  291. else if (err_cmd12)
  292. data->stop->error = -EILSEQ;
  293. err_status &= ~MVSD_ERR_AUTOCMD12;
  294. }
  295. }
  296. return err_status;
  297. }
  298. static irqreturn_t mvsd_irq(int irq, void *dev)
  299. {
  300. struct mvsd_host *host = dev;
  301. void __iomem *iobase = host->base;
  302. u32 intr_status, intr_done_mask;
  303. int irq_handled = 0;
  304. intr_status = mvsd_read(MVSD_NOR_INTR_STATUS);
  305. dev_dbg(host->dev, "intr 0x%04x intr_en 0x%04x hw_state 0x%04x\n",
  306. intr_status, mvsd_read(MVSD_NOR_INTR_EN),
  307. mvsd_read(MVSD_HW_STATE));
  308. spin_lock(&host->lock);
  309. /* PIO handling, if needed. Messy business... */
  310. if (host->pio_size &&
  311. (intr_status & host->intr_en &
  312. (MVSD_NOR_RX_READY | MVSD_NOR_RX_FIFO_8W))) {
  313. u16 *p = host->pio_ptr;
  314. int s = host->pio_size;
  315. while (s >= 32 && (intr_status & MVSD_NOR_RX_FIFO_8W)) {
  316. readsw(iobase + MVSD_FIFO, p, 16);
  317. p += 16;
  318. s -= 32;
  319. intr_status = mvsd_read(MVSD_NOR_INTR_STATUS);
  320. }
  321. /*
  322. * Normally we'd use < 32 here, but the RX_FIFO_8W bit
  323. * doesn't appear to assert when there is exactly 32 bytes
  324. * (8 words) left to fetch in a transfer.
  325. */
  326. if (s <= 32) {
  327. while (s >= 4 && (intr_status & MVSD_NOR_RX_READY)) {
  328. put_unaligned(mvsd_read(MVSD_FIFO), p++);
  329. put_unaligned(mvsd_read(MVSD_FIFO), p++);
  330. s -= 4;
  331. intr_status = mvsd_read(MVSD_NOR_INTR_STATUS);
  332. }
  333. if (s && s < 4 && (intr_status & MVSD_NOR_RX_READY)) {
  334. u16 val[2] = {0, 0};
  335. val[0] = mvsd_read(MVSD_FIFO);
  336. val[1] = mvsd_read(MVSD_FIFO);
  337. memcpy(p, ((void *)&val) + 4 - s, s);
  338. s = 0;
  339. intr_status = mvsd_read(MVSD_NOR_INTR_STATUS);
  340. }
  341. if (s == 0) {
  342. host->intr_en &=
  343. ~(MVSD_NOR_RX_READY | MVSD_NOR_RX_FIFO_8W);
  344. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  345. } else if (host->intr_en & MVSD_NOR_RX_FIFO_8W) {
  346. host->intr_en &= ~MVSD_NOR_RX_FIFO_8W;
  347. host->intr_en |= MVSD_NOR_RX_READY;
  348. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  349. }
  350. }
  351. dev_dbg(host->dev, "pio %d intr 0x%04x hw_state 0x%04x\n",
  352. s, intr_status, mvsd_read(MVSD_HW_STATE));
  353. host->pio_ptr = p;
  354. host->pio_size = s;
  355. irq_handled = 1;
  356. } else if (host->pio_size &&
  357. (intr_status & host->intr_en &
  358. (MVSD_NOR_TX_AVAIL | MVSD_NOR_TX_FIFO_8W))) {
  359. u16 *p = host->pio_ptr;
  360. int s = host->pio_size;
  361. /*
  362. * The TX_FIFO_8W bit is unreliable. When set, bursting
  363. * 16 halfwords all at once in the FIFO drops data. Actually
  364. * TX_AVAIL does go off after only one word is pushed even if
  365. * TX_FIFO_8W remains set.
  366. */
  367. while (s >= 4 && (intr_status & MVSD_NOR_TX_AVAIL)) {
  368. mvsd_write(MVSD_FIFO, get_unaligned(p++));
  369. mvsd_write(MVSD_FIFO, get_unaligned(p++));
  370. s -= 4;
  371. intr_status = mvsd_read(MVSD_NOR_INTR_STATUS);
  372. }
  373. if (s < 4) {
  374. if (s && (intr_status & MVSD_NOR_TX_AVAIL)) {
  375. u16 val[2] = {0, 0};
  376. memcpy(((void *)&val) + 4 - s, p, s);
  377. mvsd_write(MVSD_FIFO, val[0]);
  378. mvsd_write(MVSD_FIFO, val[1]);
  379. s = 0;
  380. intr_status = mvsd_read(MVSD_NOR_INTR_STATUS);
  381. }
  382. if (s == 0) {
  383. host->intr_en &=
  384. ~(MVSD_NOR_TX_AVAIL | MVSD_NOR_TX_FIFO_8W);
  385. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  386. }
  387. }
  388. dev_dbg(host->dev, "pio %d intr 0x%04x hw_state 0x%04x\n",
  389. s, intr_status, mvsd_read(MVSD_HW_STATE));
  390. host->pio_ptr = p;
  391. host->pio_size = s;
  392. irq_handled = 1;
  393. }
  394. mvsd_write(MVSD_NOR_INTR_STATUS, intr_status);
  395. intr_done_mask = MVSD_NOR_CARD_INT | MVSD_NOR_RX_READY |
  396. MVSD_NOR_RX_FIFO_8W | MVSD_NOR_TX_FIFO_8W;
  397. if (intr_status & host->intr_en & ~intr_done_mask) {
  398. struct mmc_request *mrq = host->mrq;
  399. struct mmc_command *cmd = mrq->cmd;
  400. u32 err_status = 0;
  401. del_timer(&host->timer);
  402. host->mrq = NULL;
  403. host->intr_en &= MVSD_NOR_CARD_INT;
  404. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  405. mvsd_write(MVSD_ERR_INTR_EN, 0);
  406. spin_unlock(&host->lock);
  407. if (intr_status & MVSD_NOR_UNEXP_RSP) {
  408. cmd->error = -EPROTO;
  409. } else if (intr_status & MVSD_NOR_ERROR) {
  410. err_status = mvsd_read(MVSD_ERR_INTR_STATUS);
  411. dev_dbg(host->dev, "err 0x%04x\n", err_status);
  412. }
  413. err_status = mvsd_finish_cmd(host, cmd, err_status);
  414. if (mrq->data)
  415. err_status = mvsd_finish_data(host, mrq->data, err_status);
  416. if (err_status) {
  417. pr_err("%s: unhandled error status %#04x\n",
  418. mmc_hostname(host->mmc), err_status);
  419. cmd->error = -ENOMSG;
  420. }
  421. mmc_request_done(host->mmc, mrq);
  422. irq_handled = 1;
  423. } else
  424. spin_unlock(&host->lock);
  425. if (intr_status & MVSD_NOR_CARD_INT) {
  426. mmc_signal_sdio_irq(host->mmc);
  427. irq_handled = 1;
  428. }
  429. if (irq_handled)
  430. return IRQ_HANDLED;
  431. pr_err("%s: unhandled interrupt status=0x%04x en=0x%04x "
  432. "pio=%d\n", mmc_hostname(host->mmc), intr_status,
  433. host->intr_en, host->pio_size);
  434. return IRQ_NONE;
  435. }
  436. static void mvsd_timeout_timer(unsigned long data)
  437. {
  438. struct mvsd_host *host = (struct mvsd_host *)data;
  439. void __iomem *iobase = host->base;
  440. struct mmc_request *mrq;
  441. unsigned long flags;
  442. spin_lock_irqsave(&host->lock, flags);
  443. mrq = host->mrq;
  444. if (mrq) {
  445. pr_err("%s: Timeout waiting for hardware interrupt.\n",
  446. mmc_hostname(host->mmc));
  447. pr_err("%s: hw_state=0x%04x, intr_status=0x%04x "
  448. "intr_en=0x%04x\n", mmc_hostname(host->mmc),
  449. mvsd_read(MVSD_HW_STATE),
  450. mvsd_read(MVSD_NOR_INTR_STATUS),
  451. mvsd_read(MVSD_NOR_INTR_EN));
  452. host->mrq = NULL;
  453. mvsd_write(MVSD_SW_RESET, MVSD_SW_RESET_NOW);
  454. host->xfer_mode &= MVSD_XFER_MODE_INT_CHK_EN;
  455. mvsd_write(MVSD_XFER_MODE, host->xfer_mode);
  456. host->intr_en &= MVSD_NOR_CARD_INT;
  457. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  458. mvsd_write(MVSD_ERR_INTR_EN, 0);
  459. mvsd_write(MVSD_ERR_INTR_STATUS, 0xffff);
  460. mrq->cmd->error = -ETIMEDOUT;
  461. mvsd_finish_cmd(host, mrq->cmd, 0);
  462. if (mrq->data) {
  463. mrq->data->error = -ETIMEDOUT;
  464. mvsd_finish_data(host, mrq->data, 0);
  465. }
  466. }
  467. spin_unlock_irqrestore(&host->lock, flags);
  468. if (mrq)
  469. mmc_request_done(host->mmc, mrq);
  470. }
  471. static irqreturn_t mvsd_card_detect_irq(int irq, void *dev)
  472. {
  473. struct mvsd_host *host = dev;
  474. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  475. return IRQ_HANDLED;
  476. }
  477. static void mvsd_enable_sdio_irq(struct mmc_host *mmc, int enable)
  478. {
  479. struct mvsd_host *host = mmc_priv(mmc);
  480. void __iomem *iobase = host->base;
  481. unsigned long flags;
  482. spin_lock_irqsave(&host->lock, flags);
  483. if (enable) {
  484. host->xfer_mode |= MVSD_XFER_MODE_INT_CHK_EN;
  485. host->intr_en |= MVSD_NOR_CARD_INT;
  486. } else {
  487. host->xfer_mode &= ~MVSD_XFER_MODE_INT_CHK_EN;
  488. host->intr_en &= ~MVSD_NOR_CARD_INT;
  489. }
  490. mvsd_write(MVSD_XFER_MODE, host->xfer_mode);
  491. mvsd_write(MVSD_NOR_INTR_EN, host->intr_en);
  492. spin_unlock_irqrestore(&host->lock, flags);
  493. }
  494. static int mvsd_get_ro(struct mmc_host *mmc)
  495. {
  496. struct mvsd_host *host = mmc_priv(mmc);
  497. if (host->gpio_write_protect)
  498. return gpio_get_value(host->gpio_write_protect);
  499. /*
  500. * Board doesn't support read only detection; let the mmc core
  501. * decide what to do.
  502. */
  503. return -ENOSYS;
  504. }
  505. static void mvsd_power_up(struct mvsd_host *host)
  506. {
  507. void __iomem *iobase = host->base;
  508. dev_dbg(host->dev, "power up\n");
  509. mvsd_write(MVSD_NOR_INTR_EN, 0);
  510. mvsd_write(MVSD_ERR_INTR_EN, 0);
  511. mvsd_write(MVSD_SW_RESET, MVSD_SW_RESET_NOW);
  512. mvsd_write(MVSD_XFER_MODE, 0);
  513. mvsd_write(MVSD_NOR_STATUS_EN, 0xffff);
  514. mvsd_write(MVSD_ERR_STATUS_EN, 0xffff);
  515. mvsd_write(MVSD_NOR_INTR_STATUS, 0xffff);
  516. mvsd_write(MVSD_ERR_INTR_STATUS, 0xffff);
  517. }
  518. static void mvsd_power_down(struct mvsd_host *host)
  519. {
  520. void __iomem *iobase = host->base;
  521. dev_dbg(host->dev, "power down\n");
  522. mvsd_write(MVSD_NOR_INTR_EN, 0);
  523. mvsd_write(MVSD_ERR_INTR_EN, 0);
  524. mvsd_write(MVSD_SW_RESET, MVSD_SW_RESET_NOW);
  525. mvsd_write(MVSD_XFER_MODE, MVSD_XFER_MODE_STOP_CLK);
  526. mvsd_write(MVSD_NOR_STATUS_EN, 0);
  527. mvsd_write(MVSD_ERR_STATUS_EN, 0);
  528. mvsd_write(MVSD_NOR_INTR_STATUS, 0xffff);
  529. mvsd_write(MVSD_ERR_INTR_STATUS, 0xffff);
  530. }
  531. static void mvsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  532. {
  533. struct mvsd_host *host = mmc_priv(mmc);
  534. void __iomem *iobase = host->base;
  535. u32 ctrl_reg = 0;
  536. if (ios->power_mode == MMC_POWER_UP)
  537. mvsd_power_up(host);
  538. if (ios->clock == 0) {
  539. mvsd_write(MVSD_XFER_MODE, MVSD_XFER_MODE_STOP_CLK);
  540. mvsd_write(MVSD_CLK_DIV, MVSD_BASE_DIV_MAX);
  541. host->clock = 0;
  542. dev_dbg(host->dev, "clock off\n");
  543. } else if (ios->clock != host->clock) {
  544. u32 m = DIV_ROUND_UP(host->base_clock, ios->clock) - 1;
  545. if (m > MVSD_BASE_DIV_MAX)
  546. m = MVSD_BASE_DIV_MAX;
  547. mvsd_write(MVSD_CLK_DIV, m);
  548. host->clock = ios->clock;
  549. host->ns_per_clk = 1000000000 / (host->base_clock / (m+1));
  550. dev_dbg(host->dev, "clock=%d (%d), div=0x%04x\n",
  551. ios->clock, host->base_clock / (m+1), m);
  552. }
  553. /* default transfer mode */
  554. ctrl_reg |= MVSD_HOST_CTRL_BIG_ENDIAN;
  555. ctrl_reg &= ~MVSD_HOST_CTRL_LSB_FIRST;
  556. /* default to maximum timeout */
  557. ctrl_reg |= MVSD_HOST_CTRL_TMOUT_MASK;
  558. ctrl_reg |= MVSD_HOST_CTRL_TMOUT_EN;
  559. if (ios->bus_mode == MMC_BUSMODE_PUSHPULL)
  560. ctrl_reg |= MVSD_HOST_CTRL_PUSH_PULL_EN;
  561. if (ios->bus_width == MMC_BUS_WIDTH_4)
  562. ctrl_reg |= MVSD_HOST_CTRL_DATA_WIDTH_4_BITS;
  563. /*
  564. * The HI_SPEED_EN bit is causing trouble with many (but not all)
  565. * high speed SD, SDHC and SDIO cards. Not enabling that bit
  566. * makes all cards work. So let's just ignore that bit for now
  567. * and revisit this issue if problems for not enabling this bit
  568. * are ever reported.
  569. */
  570. #if 0
  571. if (ios->timing == MMC_TIMING_MMC_HS ||
  572. ios->timing == MMC_TIMING_SD_HS)
  573. ctrl_reg |= MVSD_HOST_CTRL_HI_SPEED_EN;
  574. #endif
  575. host->ctrl = ctrl_reg;
  576. mvsd_write(MVSD_HOST_CTRL, ctrl_reg);
  577. dev_dbg(host->dev, "ctrl 0x%04x: %s %s %s\n", ctrl_reg,
  578. (ctrl_reg & MVSD_HOST_CTRL_PUSH_PULL_EN) ?
  579. "push-pull" : "open-drain",
  580. (ctrl_reg & MVSD_HOST_CTRL_DATA_WIDTH_4_BITS) ?
  581. "4bit-width" : "1bit-width",
  582. (ctrl_reg & MVSD_HOST_CTRL_HI_SPEED_EN) ?
  583. "high-speed" : "");
  584. if (ios->power_mode == MMC_POWER_OFF)
  585. mvsd_power_down(host);
  586. }
  587. static const struct mmc_host_ops mvsd_ops = {
  588. .request = mvsd_request,
  589. .get_ro = mvsd_get_ro,
  590. .set_ios = mvsd_set_ios,
  591. .enable_sdio_irq = mvsd_enable_sdio_irq,
  592. };
  593. static void __init
  594. mv_conf_mbus_windows(struct mvsd_host *host,
  595. const struct mbus_dram_target_info *dram)
  596. {
  597. void __iomem *iobase = host->base;
  598. int i;
  599. for (i = 0; i < 4; i++) {
  600. writel(0, iobase + MVSD_WINDOW_CTRL(i));
  601. writel(0, iobase + MVSD_WINDOW_BASE(i));
  602. }
  603. for (i = 0; i < dram->num_cs; i++) {
  604. const struct mbus_dram_window *cs = dram->cs + i;
  605. writel(((cs->size - 1) & 0xffff0000) |
  606. (cs->mbus_attr << 8) |
  607. (dram->mbus_dram_target_id << 4) | 1,
  608. iobase + MVSD_WINDOW_CTRL(i));
  609. writel(cs->base, iobase + MVSD_WINDOW_BASE(i));
  610. }
  611. }
  612. static int __init mvsd_probe(struct platform_device *pdev)
  613. {
  614. struct mmc_host *mmc = NULL;
  615. struct mvsd_host *host = NULL;
  616. const struct mvsdio_platform_data *mvsd_data;
  617. const struct mbus_dram_target_info *dram;
  618. struct resource *r;
  619. int ret, irq;
  620. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  621. irq = platform_get_irq(pdev, 0);
  622. mvsd_data = pdev->dev.platform_data;
  623. if (!r || irq < 0 || !mvsd_data)
  624. return -ENXIO;
  625. r = request_mem_region(r->start, SZ_1K, DRIVER_NAME);
  626. if (!r)
  627. return -EBUSY;
  628. mmc = mmc_alloc_host(sizeof(struct mvsd_host), &pdev->dev);
  629. if (!mmc) {
  630. ret = -ENOMEM;
  631. goto out;
  632. }
  633. host = mmc_priv(mmc);
  634. host->mmc = mmc;
  635. host->dev = &pdev->dev;
  636. host->res = r;
  637. host->base_clock = mvsd_data->clock / 2;
  638. mmc->ops = &mvsd_ops;
  639. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  640. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ |
  641. MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  642. mmc->f_min = DIV_ROUND_UP(host->base_clock, MVSD_BASE_DIV_MAX);
  643. mmc->f_max = maxfreq;
  644. mmc->max_blk_size = 2048;
  645. mmc->max_blk_count = 65535;
  646. mmc->max_segs = 1;
  647. mmc->max_seg_size = mmc->max_blk_size * mmc->max_blk_count;
  648. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  649. spin_lock_init(&host->lock);
  650. host->base = ioremap(r->start, SZ_4K);
  651. if (!host->base) {
  652. ret = -ENOMEM;
  653. goto out;
  654. }
  655. /* (Re-)program MBUS remapping windows if we are asked to. */
  656. dram = mv_mbus_dram_info();
  657. if (dram)
  658. mv_conf_mbus_windows(host, dram);
  659. mvsd_power_down(host);
  660. ret = request_irq(irq, mvsd_irq, 0, DRIVER_NAME, host);
  661. if (ret) {
  662. pr_err("%s: cannot assign irq %d\n", DRIVER_NAME, irq);
  663. goto out;
  664. } else
  665. host->irq = irq;
  666. if (mvsd_data->gpio_card_detect) {
  667. ret = gpio_request(mvsd_data->gpio_card_detect,
  668. DRIVER_NAME " cd");
  669. if (ret == 0) {
  670. gpio_direction_input(mvsd_data->gpio_card_detect);
  671. irq = gpio_to_irq(mvsd_data->gpio_card_detect);
  672. ret = request_irq(irq, mvsd_card_detect_irq,
  673. IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING,
  674. DRIVER_NAME " cd", host);
  675. if (ret == 0)
  676. host->gpio_card_detect =
  677. mvsd_data->gpio_card_detect;
  678. else
  679. gpio_free(mvsd_data->gpio_card_detect);
  680. }
  681. }
  682. if (!host->gpio_card_detect)
  683. mmc->caps |= MMC_CAP_NEEDS_POLL;
  684. if (mvsd_data->gpio_write_protect) {
  685. ret = gpio_request(mvsd_data->gpio_write_protect,
  686. DRIVER_NAME " wp");
  687. if (ret == 0) {
  688. gpio_direction_input(mvsd_data->gpio_write_protect);
  689. host->gpio_write_protect =
  690. mvsd_data->gpio_write_protect;
  691. }
  692. }
  693. setup_timer(&host->timer, mvsd_timeout_timer, (unsigned long)host);
  694. platform_set_drvdata(pdev, mmc);
  695. ret = mmc_add_host(mmc);
  696. if (ret)
  697. goto out;
  698. pr_notice("%s: %s driver initialized, ",
  699. mmc_hostname(mmc), DRIVER_NAME);
  700. if (host->gpio_card_detect)
  701. printk("using GPIO %d for card detection\n",
  702. host->gpio_card_detect);
  703. else
  704. printk("lacking card detect (fall back to polling)\n");
  705. return 0;
  706. out:
  707. if (host) {
  708. if (host->irq)
  709. free_irq(host->irq, host);
  710. if (host->gpio_card_detect) {
  711. free_irq(gpio_to_irq(host->gpio_card_detect), host);
  712. gpio_free(host->gpio_card_detect);
  713. }
  714. if (host->gpio_write_protect)
  715. gpio_free(host->gpio_write_protect);
  716. if (host->base)
  717. iounmap(host->base);
  718. }
  719. if (r)
  720. release_resource(r);
  721. if (mmc)
  722. mmc_free_host(mmc);
  723. return ret;
  724. }
  725. static int __exit mvsd_remove(struct platform_device *pdev)
  726. {
  727. struct mmc_host *mmc = platform_get_drvdata(pdev);
  728. if (mmc) {
  729. struct mvsd_host *host = mmc_priv(mmc);
  730. if (host->gpio_card_detect) {
  731. free_irq(gpio_to_irq(host->gpio_card_detect), host);
  732. gpio_free(host->gpio_card_detect);
  733. }
  734. mmc_remove_host(mmc);
  735. free_irq(host->irq, host);
  736. if (host->gpio_write_protect)
  737. gpio_free(host->gpio_write_protect);
  738. del_timer_sync(&host->timer);
  739. mvsd_power_down(host);
  740. iounmap(host->base);
  741. release_resource(host->res);
  742. mmc_free_host(mmc);
  743. }
  744. platform_set_drvdata(pdev, NULL);
  745. return 0;
  746. }
  747. #ifdef CONFIG_PM
  748. static int mvsd_suspend(struct platform_device *dev, pm_message_t state)
  749. {
  750. struct mmc_host *mmc = platform_get_drvdata(dev);
  751. int ret = 0;
  752. if (mmc)
  753. ret = mmc_suspend_host(mmc);
  754. return ret;
  755. }
  756. static int mvsd_resume(struct platform_device *dev)
  757. {
  758. struct mmc_host *mmc = platform_get_drvdata(dev);
  759. int ret = 0;
  760. if (mmc)
  761. ret = mmc_resume_host(mmc);
  762. return ret;
  763. }
  764. #else
  765. #define mvsd_suspend NULL
  766. #define mvsd_resume NULL
  767. #endif
  768. static struct platform_driver mvsd_driver = {
  769. .remove = __exit_p(mvsd_remove),
  770. .suspend = mvsd_suspend,
  771. .resume = mvsd_resume,
  772. .driver = {
  773. .name = DRIVER_NAME,
  774. },
  775. };
  776. static int __init mvsd_init(void)
  777. {
  778. return platform_driver_probe(&mvsd_driver, mvsd_probe);
  779. }
  780. static void __exit mvsd_exit(void)
  781. {
  782. platform_driver_unregister(&mvsd_driver);
  783. }
  784. module_init(mvsd_init);
  785. module_exit(mvsd_exit);
  786. /* maximum card clock frequency (default 50MHz) */
  787. module_param(maxfreq, int, 0);
  788. /* force PIO transfers all the time */
  789. module_param(nodma, int, 0);
  790. MODULE_AUTHOR("Maen Suleiman, Nicolas Pitre");
  791. MODULE_DESCRIPTION("Marvell MMC,SD,SDIO Host Controller driver");
  792. MODULE_LICENSE("GPL");
  793. MODULE_ALIAS("platform:mvsdio");