android-goldfish.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright 2007, Google Inc.
  3. * Copyright 2012, Intel Inc.
  4. *
  5. * based on omap.c driver, which was
  6. * Copyright (C) 2004 Nokia Corporation
  7. * Written by Tuukka Tikkanen and Juha Yrjölä <juha.yrjola@nokia.com>
  8. * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
  9. * Other hacks (DMA, SD, etc) by David Brownell
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/major.h>
  18. #include <linux/types.h>
  19. #include <linux/pci.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/fs.h>
  23. #include <linux/errno.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/mutex.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/mmc/mmc.h>
  30. #include <linux/mmc/sdio.h>
  31. #include <linux/mmc/host.h>
  32. #include <linux/mmc/card.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/init.h>
  35. #include <linux/ioport.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/delay.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/timer.h>
  40. #include <linux/clk.h>
  41. #include <linux/scatterlist.h>
  42. #include <asm/io.h>
  43. #include <asm/irq.h>
  44. #include <asm/types.h>
  45. #include <asm/io.h>
  46. #include <linux/uaccess.h>
  47. #define DRIVER_NAME "goldfish_mmc"
  48. #define BUFFER_SIZE 16384
  49. #define GOLDFISH_MMC_READ(host, addr) (readl(host->reg_base + addr))
  50. #define GOLDFISH_MMC_WRITE(host, addr, x) (writel(x, host->reg_base + addr))
  51. enum {
  52. /* status register */
  53. MMC_INT_STATUS = 0x00,
  54. /* set this to enable IRQ */
  55. MMC_INT_ENABLE = 0x04,
  56. /* set this to specify buffer address */
  57. MMC_SET_BUFFER = 0x08,
  58. /* MMC command number */
  59. MMC_CMD = 0x0C,
  60. /* MMC argument */
  61. MMC_ARG = 0x10,
  62. /* MMC response (or R2 bits 0 - 31) */
  63. MMC_RESP_0 = 0x14,
  64. /* MMC R2 response bits 32 - 63 */
  65. MMC_RESP_1 = 0x18,
  66. /* MMC R2 response bits 64 - 95 */
  67. MMC_RESP_2 = 0x1C,
  68. /* MMC R2 response bits 96 - 127 */
  69. MMC_RESP_3 = 0x20,
  70. MMC_BLOCK_LENGTH = 0x24,
  71. MMC_BLOCK_COUNT = 0x28,
  72. /* MMC state flags */
  73. MMC_STATE = 0x2C,
  74. /* MMC_INT_STATUS bits */
  75. MMC_STAT_END_OF_CMD = 1U << 0,
  76. MMC_STAT_END_OF_DATA = 1U << 1,
  77. MMC_STAT_STATE_CHANGE = 1U << 2,
  78. MMC_STAT_CMD_TIMEOUT = 1U << 3,
  79. /* MMC_STATE bits */
  80. MMC_STATE_INSERTED = 1U << 0,
  81. MMC_STATE_READ_ONLY = 1U << 1,
  82. };
  83. /*
  84. * Command types
  85. */
  86. #define OMAP_MMC_CMDTYPE_BC 0
  87. #define OMAP_MMC_CMDTYPE_BCR 1
  88. #define OMAP_MMC_CMDTYPE_AC 2
  89. #define OMAP_MMC_CMDTYPE_ADTC 3
  90. struct goldfish_mmc_host {
  91. struct mmc_request *mrq;
  92. struct mmc_command *cmd;
  93. struct mmc_data *data;
  94. struct mmc_host *mmc;
  95. struct device *dev;
  96. unsigned char id; /* 16xx chips have 2 MMC blocks */
  97. void *virt_base;
  98. unsigned int phys_base;
  99. int irq;
  100. unsigned char bus_mode;
  101. unsigned char hw_bus_mode;
  102. unsigned int sg_len;
  103. unsigned dma_done:1;
  104. unsigned dma_in_use:1;
  105. void __iomem *reg_base;
  106. };
  107. static inline int
  108. goldfish_mmc_cover_is_open(struct goldfish_mmc_host *host)
  109. {
  110. return 0;
  111. }
  112. static ssize_t
  113. goldfish_mmc_show_cover_switch(struct device *dev,
  114. struct device_attribute *attr, char *buf)
  115. {
  116. struct goldfish_mmc_host *host = dev_get_drvdata(dev);
  117. return sprintf(buf, "%s\n", goldfish_mmc_cover_is_open(host) ? "open" :
  118. "closed");
  119. }
  120. static DEVICE_ATTR(cover_switch, S_IRUGO, goldfish_mmc_show_cover_switch, NULL);
  121. static void
  122. goldfish_mmc_start_command(struct goldfish_mmc_host *host, struct mmc_command *cmd)
  123. {
  124. u32 cmdreg;
  125. u32 resptype;
  126. u32 cmdtype;
  127. host->cmd = cmd;
  128. resptype = 0;
  129. cmdtype = 0;
  130. /* Our hardware needs to know exact type */
  131. switch (mmc_resp_type(cmd)) {
  132. case MMC_RSP_NONE:
  133. break;
  134. case MMC_RSP_R1:
  135. case MMC_RSP_R1B:
  136. /* resp 1, 1b, 6, 7 */
  137. resptype = 1;
  138. break;
  139. case MMC_RSP_R2:
  140. resptype = 2;
  141. break;
  142. case MMC_RSP_R3:
  143. resptype = 3;
  144. break;
  145. default:
  146. dev_err(mmc_dev(host->mmc),
  147. "Invalid response type: %04x\n", mmc_resp_type(cmd));
  148. break;
  149. }
  150. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  151. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  152. else if (mmc_cmd_type(cmd) == MMC_CMD_BC)
  153. cmdtype = OMAP_MMC_CMDTYPE_BC;
  154. else if (mmc_cmd_type(cmd) == MMC_CMD_BCR)
  155. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  156. else
  157. cmdtype = OMAP_MMC_CMDTYPE_AC;
  158. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  159. if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
  160. cmdreg |= 1 << 6;
  161. if (cmd->flags & MMC_RSP_BUSY)
  162. cmdreg |= 1 << 11;
  163. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  164. cmdreg |= 1 << 15;
  165. GOLDFISH_MMC_WRITE(host, MMC_ARG, cmd->arg);
  166. GOLDFISH_MMC_WRITE(host, MMC_CMD, cmdreg);
  167. }
  168. static void goldfish_mmc_xfer_done(struct goldfish_mmc_host *host,
  169. struct mmc_data *data)
  170. {
  171. if (host->dma_in_use) {
  172. enum dma_data_direction dma_data_dir;
  173. dma_data_dir = mmc_get_dma_dir(data);
  174. if (dma_data_dir == DMA_FROM_DEVICE) {
  175. /*
  176. * We don't really have DMA, so we need
  177. * to copy from our platform driver buffer
  178. */
  179. uint8_t *dest = (uint8_t *)sg_virt(data->sg);
  180. memcpy(dest, host->virt_base, data->sg->length);
  181. }
  182. host->data->bytes_xfered += data->sg->length;
  183. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
  184. dma_data_dir);
  185. }
  186. host->data = NULL;
  187. host->sg_len = 0;
  188. /*
  189. * NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  190. * dozens of requests until the card finishes writing data.
  191. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  192. */
  193. if (!data->stop) {
  194. host->mrq = NULL;
  195. mmc_request_done(host->mmc, data->mrq);
  196. return;
  197. }
  198. goldfish_mmc_start_command(host, data->stop);
  199. }
  200. static void goldfish_mmc_end_of_data(struct goldfish_mmc_host *host,
  201. struct mmc_data *data)
  202. {
  203. if (!host->dma_in_use) {
  204. goldfish_mmc_xfer_done(host, data);
  205. return;
  206. }
  207. if (host->dma_done)
  208. goldfish_mmc_xfer_done(host, data);
  209. }
  210. static void goldfish_mmc_cmd_done(struct goldfish_mmc_host *host,
  211. struct mmc_command *cmd)
  212. {
  213. host->cmd = NULL;
  214. if (cmd->flags & MMC_RSP_PRESENT) {
  215. if (cmd->flags & MMC_RSP_136) {
  216. /* response type 2 */
  217. cmd->resp[3] =
  218. GOLDFISH_MMC_READ(host, MMC_RESP_0);
  219. cmd->resp[2] =
  220. GOLDFISH_MMC_READ(host, MMC_RESP_1);
  221. cmd->resp[1] =
  222. GOLDFISH_MMC_READ(host, MMC_RESP_2);
  223. cmd->resp[0] =
  224. GOLDFISH_MMC_READ(host, MMC_RESP_3);
  225. } else {
  226. /* response types 1, 1b, 3, 4, 5, 6 */
  227. cmd->resp[0] =
  228. GOLDFISH_MMC_READ(host, MMC_RESP_0);
  229. }
  230. }
  231. if (host->data == NULL || cmd->error) {
  232. host->mrq = NULL;
  233. mmc_request_done(host->mmc, cmd->mrq);
  234. }
  235. }
  236. static irqreturn_t goldfish_mmc_irq(int irq, void *dev_id)
  237. {
  238. struct goldfish_mmc_host *host = (struct goldfish_mmc_host *)dev_id;
  239. u16 status;
  240. int end_command = 0;
  241. int end_transfer = 0;
  242. int state_changed = 0;
  243. int cmd_timeout = 0;
  244. while ((status = GOLDFISH_MMC_READ(host, MMC_INT_STATUS)) != 0) {
  245. GOLDFISH_MMC_WRITE(host, MMC_INT_STATUS, status);
  246. if (status & MMC_STAT_END_OF_CMD)
  247. end_command = 1;
  248. if (status & MMC_STAT_END_OF_DATA)
  249. end_transfer = 1;
  250. if (status & MMC_STAT_STATE_CHANGE)
  251. state_changed = 1;
  252. if (status & MMC_STAT_CMD_TIMEOUT) {
  253. end_command = 0;
  254. cmd_timeout = 1;
  255. }
  256. }
  257. if (cmd_timeout) {
  258. struct mmc_request *mrq = host->mrq;
  259. mrq->cmd->error = -ETIMEDOUT;
  260. host->mrq = NULL;
  261. mmc_request_done(host->mmc, mrq);
  262. }
  263. if (end_command)
  264. goldfish_mmc_cmd_done(host, host->cmd);
  265. if (end_transfer) {
  266. host->dma_done = 1;
  267. goldfish_mmc_end_of_data(host, host->data);
  268. } else if (host->data != NULL) {
  269. /*
  270. * WORKAROUND -- after porting this driver from 2.6 to 3.4,
  271. * during device initialization, cases where host->data is
  272. * non-null but end_transfer is false would occur. Doing
  273. * nothing in such cases results in no further interrupts,
  274. * and initialization failure.
  275. * TODO -- find the real cause.
  276. */
  277. host->dma_done = 1;
  278. goldfish_mmc_end_of_data(host, host->data);
  279. }
  280. if (state_changed) {
  281. u32 state = GOLDFISH_MMC_READ(host, MMC_STATE);
  282. pr_info("%s: Card detect now %d\n", __func__,
  283. (state & MMC_STATE_INSERTED));
  284. mmc_detect_change(host->mmc, 0);
  285. }
  286. if (!end_command && !end_transfer && !state_changed && !cmd_timeout) {
  287. status = GOLDFISH_MMC_READ(host, MMC_INT_STATUS);
  288. dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
  289. if (status != 0) {
  290. GOLDFISH_MMC_WRITE(host, MMC_INT_STATUS, status);
  291. GOLDFISH_MMC_WRITE(host, MMC_INT_ENABLE, 0);
  292. }
  293. }
  294. return IRQ_HANDLED;
  295. }
  296. static void goldfish_mmc_prepare_data(struct goldfish_mmc_host *host,
  297. struct mmc_request *req)
  298. {
  299. struct mmc_data *data = req->data;
  300. int block_size;
  301. unsigned sg_len;
  302. enum dma_data_direction dma_data_dir;
  303. host->data = data;
  304. if (data == NULL) {
  305. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_LENGTH, 0);
  306. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_COUNT, 0);
  307. host->dma_in_use = 0;
  308. return;
  309. }
  310. block_size = data->blksz;
  311. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_COUNT, data->blocks - 1);
  312. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_LENGTH, block_size - 1);
  313. /*
  314. * Cope with calling layer confusion; it issues "single
  315. * block" writes using multi-block scatterlists.
  316. */
  317. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  318. dma_data_dir = mmc_get_dma_dir(data);
  319. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  320. sg_len, dma_data_dir);
  321. host->dma_done = 0;
  322. host->dma_in_use = 1;
  323. if (dma_data_dir == DMA_TO_DEVICE) {
  324. /*
  325. * We don't really have DMA, so we need to copy to our
  326. * platform driver buffer
  327. */
  328. const uint8_t *src = (uint8_t *)sg_virt(data->sg);
  329. memcpy(host->virt_base, src, data->sg->length);
  330. }
  331. }
  332. static void goldfish_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  333. {
  334. struct goldfish_mmc_host *host = mmc_priv(mmc);
  335. WARN_ON(host->mrq != NULL);
  336. host->mrq = req;
  337. goldfish_mmc_prepare_data(host, req);
  338. goldfish_mmc_start_command(host, req->cmd);
  339. /*
  340. * This is to avoid accidentally being detected as an SDIO card
  341. * in mmc_attach_sdio().
  342. */
  343. if (req->cmd->opcode == SD_IO_SEND_OP_COND &&
  344. req->cmd->flags == (MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR))
  345. req->cmd->error = -EINVAL;
  346. }
  347. static void goldfish_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  348. {
  349. struct goldfish_mmc_host *host = mmc_priv(mmc);
  350. host->bus_mode = ios->bus_mode;
  351. host->hw_bus_mode = host->bus_mode;
  352. }
  353. static int goldfish_mmc_get_ro(struct mmc_host *mmc)
  354. {
  355. uint32_t state;
  356. struct goldfish_mmc_host *host = mmc_priv(mmc);
  357. state = GOLDFISH_MMC_READ(host, MMC_STATE);
  358. return ((state & MMC_STATE_READ_ONLY) != 0);
  359. }
  360. static const struct mmc_host_ops goldfish_mmc_ops = {
  361. .request = goldfish_mmc_request,
  362. .set_ios = goldfish_mmc_set_ios,
  363. .get_ro = goldfish_mmc_get_ro,
  364. };
  365. static int goldfish_mmc_probe(struct platform_device *pdev)
  366. {
  367. struct mmc_host *mmc;
  368. struct goldfish_mmc_host *host = NULL;
  369. struct resource *res;
  370. int ret = 0;
  371. int irq;
  372. dma_addr_t buf_addr;
  373. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  374. irq = platform_get_irq(pdev, 0);
  375. if (res == NULL || irq < 0)
  376. return -ENXIO;
  377. mmc = mmc_alloc_host(sizeof(struct goldfish_mmc_host), &pdev->dev);
  378. if (mmc == NULL) {
  379. ret = -ENOMEM;
  380. goto err_alloc_host_failed;
  381. }
  382. host = mmc_priv(mmc);
  383. host->mmc = mmc;
  384. pr_err("mmc: Mapping %lX to %lX\n", (long)res->start, (long)res->end);
  385. host->reg_base = ioremap(res->start, resource_size(res));
  386. if (host->reg_base == NULL) {
  387. ret = -ENOMEM;
  388. goto ioremap_failed;
  389. }
  390. host->virt_base = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  391. &buf_addr, GFP_KERNEL);
  392. if (host->virt_base == 0) {
  393. ret = -ENOMEM;
  394. goto dma_alloc_failed;
  395. }
  396. host->phys_base = buf_addr;
  397. host->id = pdev->id;
  398. host->irq = irq;
  399. mmc->ops = &goldfish_mmc_ops;
  400. mmc->f_min = 400000;
  401. mmc->f_max = 24000000;
  402. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  403. mmc->caps = MMC_CAP_4_BIT_DATA;
  404. /* Use scatterlist DMA to reduce per-transfer costs.
  405. * NOTE max_seg_size assumption that small blocks aren't
  406. * normally used (except e.g. for reading SD registers).
  407. */
  408. mmc->max_segs = 32;
  409. mmc->max_blk_size = 2048; /* MMC_BLOCK_LENGTH is 11 bits (+1) */
  410. mmc->max_blk_count = 2048; /* MMC_BLOCK_COUNT is 11 bits (+1) */
  411. mmc->max_req_size = BUFFER_SIZE;
  412. mmc->max_seg_size = mmc->max_req_size;
  413. ret = request_irq(host->irq, goldfish_mmc_irq, 0, DRIVER_NAME, host);
  414. if (ret) {
  415. dev_err(&pdev->dev, "Failed IRQ Adding goldfish MMC\n");
  416. goto err_request_irq_failed;
  417. }
  418. host->dev = &pdev->dev;
  419. platform_set_drvdata(pdev, host);
  420. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  421. if (ret)
  422. dev_warn(mmc_dev(host->mmc),
  423. "Unable to create sysfs attributes\n");
  424. GOLDFISH_MMC_WRITE(host, MMC_SET_BUFFER, host->phys_base);
  425. GOLDFISH_MMC_WRITE(host, MMC_INT_ENABLE,
  426. MMC_STAT_END_OF_CMD | MMC_STAT_END_OF_DATA |
  427. MMC_STAT_STATE_CHANGE | MMC_STAT_CMD_TIMEOUT);
  428. mmc_add_host(mmc);
  429. return 0;
  430. err_request_irq_failed:
  431. dma_free_coherent(&pdev->dev, BUFFER_SIZE, host->virt_base,
  432. host->phys_base);
  433. dma_alloc_failed:
  434. iounmap(host->reg_base);
  435. ioremap_failed:
  436. mmc_free_host(host->mmc);
  437. err_alloc_host_failed:
  438. return ret;
  439. }
  440. static int goldfish_mmc_remove(struct platform_device *pdev)
  441. {
  442. struct goldfish_mmc_host *host = platform_get_drvdata(pdev);
  443. BUG_ON(host == NULL);
  444. mmc_remove_host(host->mmc);
  445. free_irq(host->irq, host);
  446. dma_free_coherent(&pdev->dev, BUFFER_SIZE, host->virt_base, host->phys_base);
  447. iounmap(host->reg_base);
  448. mmc_free_host(host->mmc);
  449. return 0;
  450. }
  451. static struct platform_driver goldfish_mmc_driver = {
  452. .probe = goldfish_mmc_probe,
  453. .remove = goldfish_mmc_remove,
  454. .driver = {
  455. .name = DRIVER_NAME,
  456. },
  457. };
  458. module_platform_driver(goldfish_mmc_driver);
  459. MODULE_LICENSE("GPL v2");