android-goldfish.c 13 KB

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