android-goldfish.c 14 KB

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