android-goldfish.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 <asm/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. if (data->flags & MMC_DATA_WRITE)
  174. dma_data_dir = DMA_TO_DEVICE;
  175. else
  176. dma_data_dir = DMA_FROM_DEVICE;
  177. if (dma_data_dir == DMA_FROM_DEVICE) {
  178. /*
  179. * We don't really have DMA, so we need
  180. * to copy from our platform driver buffer
  181. */
  182. uint8_t *dest = (uint8_t *)sg_virt(data->sg);
  183. memcpy(dest, host->virt_base, data->sg->length);
  184. }
  185. host->data->bytes_xfered += data->sg->length;
  186. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
  187. dma_data_dir);
  188. }
  189. host->data = NULL;
  190. host->sg_len = 0;
  191. /*
  192. * NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  193. * dozens of requests until the card finishes writing data.
  194. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  195. */
  196. if (!data->stop) {
  197. host->mrq = NULL;
  198. mmc_request_done(host->mmc, data->mrq);
  199. return;
  200. }
  201. goldfish_mmc_start_command(host, data->stop);
  202. }
  203. static void goldfish_mmc_end_of_data(struct goldfish_mmc_host *host,
  204. struct mmc_data *data)
  205. {
  206. if (!host->dma_in_use) {
  207. goldfish_mmc_xfer_done(host, data);
  208. return;
  209. }
  210. if (host->dma_done)
  211. goldfish_mmc_xfer_done(host, data);
  212. }
  213. static void goldfish_mmc_cmd_done(struct goldfish_mmc_host *host,
  214. struct mmc_command *cmd)
  215. {
  216. host->cmd = NULL;
  217. if (cmd->flags & MMC_RSP_PRESENT) {
  218. if (cmd->flags & MMC_RSP_136) {
  219. /* response type 2 */
  220. cmd->resp[3] =
  221. GOLDFISH_MMC_READ(host, MMC_RESP_0);
  222. cmd->resp[2] =
  223. GOLDFISH_MMC_READ(host, MMC_RESP_1);
  224. cmd->resp[1] =
  225. GOLDFISH_MMC_READ(host, MMC_RESP_2);
  226. cmd->resp[0] =
  227. GOLDFISH_MMC_READ(host, MMC_RESP_3);
  228. } else {
  229. /* response types 1, 1b, 3, 4, 5, 6 */
  230. cmd->resp[0] =
  231. GOLDFISH_MMC_READ(host, MMC_RESP_0);
  232. }
  233. }
  234. if (host->data == NULL || cmd->error) {
  235. host->mrq = NULL;
  236. mmc_request_done(host->mmc, cmd->mrq);
  237. }
  238. }
  239. static irqreturn_t goldfish_mmc_irq(int irq, void *dev_id)
  240. {
  241. struct goldfish_mmc_host *host = (struct goldfish_mmc_host *)dev_id;
  242. u16 status;
  243. int end_command = 0;
  244. int end_transfer = 0;
  245. int transfer_error = 0;
  246. int state_changed = 0;
  247. int cmd_timeout = 0;
  248. while ((status = GOLDFISH_MMC_READ(host, MMC_INT_STATUS)) != 0) {
  249. GOLDFISH_MMC_WRITE(host, MMC_INT_STATUS, status);
  250. if (status & MMC_STAT_END_OF_CMD)
  251. end_command = 1;
  252. if (status & MMC_STAT_END_OF_DATA)
  253. end_transfer = 1;
  254. if (status & MMC_STAT_STATE_CHANGE)
  255. state_changed = 1;
  256. if (status & MMC_STAT_CMD_TIMEOUT) {
  257. end_command = 0;
  258. cmd_timeout = 1;
  259. }
  260. }
  261. if (cmd_timeout) {
  262. struct mmc_request *mrq = host->mrq;
  263. mrq->cmd->error = -ETIMEDOUT;
  264. host->mrq = NULL;
  265. mmc_request_done(host->mmc, mrq);
  266. }
  267. if (end_command)
  268. goldfish_mmc_cmd_done(host, host->cmd);
  269. if (transfer_error)
  270. goldfish_mmc_xfer_done(host, host->data);
  271. else if (end_transfer) {
  272. host->dma_done = 1;
  273. goldfish_mmc_end_of_data(host, host->data);
  274. } else if (host->data != NULL) {
  275. /*
  276. * WORKAROUND -- after porting this driver from 2.6 to 3.4,
  277. * during device initialization, cases where host->data is
  278. * non-null but end_transfer is false would occur. Doing
  279. * nothing in such cases results in no further interrupts,
  280. * and initialization failure.
  281. * TODO -- find the real cause.
  282. */
  283. host->dma_done = 1;
  284. goldfish_mmc_end_of_data(host, host->data);
  285. }
  286. if (state_changed) {
  287. u32 state = GOLDFISH_MMC_READ(host, MMC_STATE);
  288. pr_info("%s: Card detect now %d\n", __func__,
  289. (state & MMC_STATE_INSERTED));
  290. mmc_detect_change(host->mmc, 0);
  291. }
  292. if (!end_command && !end_transfer &&
  293. !transfer_error && !state_changed && !cmd_timeout) {
  294. status = GOLDFISH_MMC_READ(host, MMC_INT_STATUS);
  295. dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
  296. if (status != 0) {
  297. GOLDFISH_MMC_WRITE(host, MMC_INT_STATUS, status);
  298. GOLDFISH_MMC_WRITE(host, MMC_INT_ENABLE, 0);
  299. }
  300. }
  301. return IRQ_HANDLED;
  302. }
  303. static void goldfish_mmc_prepare_data(struct goldfish_mmc_host *host,
  304. struct mmc_request *req)
  305. {
  306. struct mmc_data *data = req->data;
  307. int block_size;
  308. unsigned sg_len;
  309. enum dma_data_direction dma_data_dir;
  310. host->data = data;
  311. if (data == NULL) {
  312. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_LENGTH, 0);
  313. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_COUNT, 0);
  314. host->dma_in_use = 0;
  315. return;
  316. }
  317. block_size = data->blksz;
  318. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_COUNT, data->blocks - 1);
  319. GOLDFISH_MMC_WRITE(host, MMC_BLOCK_LENGTH, block_size - 1);
  320. /*
  321. * Cope with calling layer confusion; it issues "single
  322. * block" writes using multi-block scatterlists.
  323. */
  324. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  325. if (data->flags & MMC_DATA_WRITE)
  326. dma_data_dir = DMA_TO_DEVICE;
  327. else
  328. dma_data_dir = DMA_FROM_DEVICE;
  329. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  330. sg_len, dma_data_dir);
  331. host->dma_done = 0;
  332. host->dma_in_use = 1;
  333. if (dma_data_dir == DMA_TO_DEVICE) {
  334. /*
  335. * We don't really have DMA, so we need to copy to our
  336. * platform driver buffer
  337. */
  338. const uint8_t *src = (uint8_t *)sg_virt(data->sg);
  339. memcpy(host->virt_base, src, data->sg->length);
  340. }
  341. }
  342. static void goldfish_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  343. {
  344. struct goldfish_mmc_host *host = mmc_priv(mmc);
  345. WARN_ON(host->mrq != NULL);
  346. host->mrq = req;
  347. goldfish_mmc_prepare_data(host, req);
  348. goldfish_mmc_start_command(host, req->cmd);
  349. /*
  350. * This is to avoid accidentally being detected as an SDIO card
  351. * in mmc_attach_sdio().
  352. */
  353. if (req->cmd->opcode == SD_IO_SEND_OP_COND &&
  354. req->cmd->flags == (MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR))
  355. req->cmd->error = -EINVAL;
  356. }
  357. static void goldfish_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  358. {
  359. struct goldfish_mmc_host *host = mmc_priv(mmc);
  360. host->bus_mode = ios->bus_mode;
  361. host->hw_bus_mode = host->bus_mode;
  362. }
  363. static int goldfish_mmc_get_ro(struct mmc_host *mmc)
  364. {
  365. uint32_t state;
  366. struct goldfish_mmc_host *host = mmc_priv(mmc);
  367. state = GOLDFISH_MMC_READ(host, MMC_STATE);
  368. return ((state & MMC_STATE_READ_ONLY) != 0);
  369. }
  370. static const struct mmc_host_ops goldfish_mmc_ops = {
  371. .request = goldfish_mmc_request,
  372. .set_ios = goldfish_mmc_set_ios,
  373. .get_ro = goldfish_mmc_get_ro,
  374. };
  375. static int goldfish_mmc_probe(struct platform_device *pdev)
  376. {
  377. struct mmc_host *mmc;
  378. struct goldfish_mmc_host *host = NULL;
  379. struct resource *res;
  380. int ret = 0;
  381. int irq;
  382. dma_addr_t buf_addr;
  383. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  384. irq = platform_get_irq(pdev, 0);
  385. if (res == NULL || irq < 0)
  386. return -ENXIO;
  387. mmc = mmc_alloc_host(sizeof(struct goldfish_mmc_host), &pdev->dev);
  388. if (mmc == NULL) {
  389. ret = -ENOMEM;
  390. goto err_alloc_host_failed;
  391. }
  392. host = mmc_priv(mmc);
  393. host->mmc = mmc;
  394. pr_err("mmc: Mapping %lX to %lX\n", (long)res->start, (long)res->end);
  395. host->reg_base = ioremap(res->start, resource_size(res));
  396. if (host->reg_base == NULL) {
  397. ret = -ENOMEM;
  398. goto ioremap_failed;
  399. }
  400. host->virt_base = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  401. &buf_addr, GFP_KERNEL);
  402. if (host->virt_base == 0) {
  403. ret = -ENOMEM;
  404. goto dma_alloc_failed;
  405. }
  406. host->phys_base = buf_addr;
  407. host->id = pdev->id;
  408. host->irq = irq;
  409. mmc->ops = &goldfish_mmc_ops;
  410. mmc->f_min = 400000;
  411. mmc->f_max = 24000000;
  412. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  413. mmc->caps = MMC_CAP_4_BIT_DATA;
  414. /* Use scatterlist DMA to reduce per-transfer costs.
  415. * NOTE max_seg_size assumption that small blocks aren't
  416. * normally used (except e.g. for reading SD registers).
  417. */
  418. mmc->max_segs = 32;
  419. mmc->max_blk_size = 2048; /* MMC_BLOCK_LENGTH is 11 bits (+1) */
  420. mmc->max_blk_count = 2048; /* MMC_BLOCK_COUNT is 11 bits (+1) */
  421. mmc->max_req_size = BUFFER_SIZE;
  422. mmc->max_seg_size = mmc->max_req_size;
  423. ret = request_irq(host->irq, goldfish_mmc_irq, 0, DRIVER_NAME, host);
  424. if (ret) {
  425. dev_err(&pdev->dev, "Failed IRQ Adding goldfish MMC\n");
  426. goto err_request_irq_failed;
  427. }
  428. host->dev = &pdev->dev;
  429. platform_set_drvdata(pdev, host);
  430. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  431. if (ret)
  432. dev_warn(mmc_dev(host->mmc),
  433. "Unable to create sysfs attributes\n");
  434. GOLDFISH_MMC_WRITE(host, MMC_SET_BUFFER, host->phys_base);
  435. GOLDFISH_MMC_WRITE(host, MMC_INT_ENABLE,
  436. MMC_STAT_END_OF_CMD | MMC_STAT_END_OF_DATA |
  437. MMC_STAT_STATE_CHANGE | MMC_STAT_CMD_TIMEOUT);
  438. mmc_add_host(mmc);
  439. return 0;
  440. err_request_irq_failed:
  441. dma_free_coherent(&pdev->dev, BUFFER_SIZE, host->virt_base,
  442. host->phys_base);
  443. dma_alloc_failed:
  444. iounmap(host->reg_base);
  445. ioremap_failed:
  446. mmc_free_host(host->mmc);
  447. err_alloc_host_failed:
  448. return ret;
  449. }
  450. static int goldfish_mmc_remove(struct platform_device *pdev)
  451. {
  452. struct goldfish_mmc_host *host = platform_get_drvdata(pdev);
  453. BUG_ON(host == NULL);
  454. mmc_remove_host(host->mmc);
  455. free_irq(host->irq, host);
  456. dma_free_coherent(&pdev->dev, BUFFER_SIZE, host->virt_base, host->phys_base);
  457. iounmap(host->reg_base);
  458. mmc_free_host(host->mmc);
  459. return 0;
  460. }
  461. static struct platform_driver goldfish_mmc_driver = {
  462. .probe = goldfish_mmc_probe,
  463. .remove = goldfish_mmc_remove,
  464. .driver = {
  465. .name = DRIVER_NAME,
  466. },
  467. };
  468. module_platform_driver(goldfish_mmc_driver);
  469. MODULE_LICENSE("GPL v2");