atmel-mci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * Atmel MultiMedia Card Interface driver
  3. *
  4. * Copyright (C) 2004-2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/mmc/host.h>
  20. #include <asm/atmel-mci.h>
  21. #include <asm/io.h>
  22. #include <asm/unaligned.h>
  23. #include <asm/arch/board.h>
  24. #include <asm/arch/gpio.h>
  25. #include "atmel-mci-regs.h"
  26. #define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
  27. enum {
  28. EVENT_CMD_COMPLETE = 0,
  29. EVENT_DATA_ERROR,
  30. EVENT_DATA_COMPLETE,
  31. EVENT_STOP_SENT,
  32. EVENT_STOP_COMPLETE,
  33. EVENT_XFER_COMPLETE,
  34. };
  35. struct atmel_mci {
  36. struct mmc_host *mmc;
  37. void __iomem *regs;
  38. struct scatterlist *sg;
  39. unsigned int pio_offset;
  40. struct mmc_request *mrq;
  41. struct mmc_command *cmd;
  42. struct mmc_data *data;
  43. u32 cmd_status;
  44. u32 data_status;
  45. u32 stop_status;
  46. u32 stop_cmdr;
  47. u32 mode_reg;
  48. u32 sdc_reg;
  49. struct tasklet_struct tasklet;
  50. unsigned long pending_events;
  51. unsigned long completed_events;
  52. int present;
  53. int detect_pin;
  54. int wp_pin;
  55. /* For detect pin debouncing */
  56. struct timer_list detect_timer;
  57. unsigned long bus_hz;
  58. unsigned long mapbase;
  59. struct clk *mck;
  60. struct platform_device *pdev;
  61. };
  62. #define atmci_is_completed(host, event) \
  63. test_bit(event, &host->completed_events)
  64. #define atmci_test_and_clear_pending(host, event) \
  65. test_and_clear_bit(event, &host->pending_events)
  66. #define atmci_test_and_set_completed(host, event) \
  67. test_and_set_bit(event, &host->completed_events)
  68. #define atmci_set_completed(host, event) \
  69. set_bit(event, &host->completed_events)
  70. #define atmci_set_pending(host, event) \
  71. set_bit(event, &host->pending_events)
  72. #define atmci_clear_pending(host, event) \
  73. clear_bit(event, &host->pending_events)
  74. static void atmci_enable(struct atmel_mci *host)
  75. {
  76. clk_enable(host->mck);
  77. mci_writel(host, CR, MCI_CR_MCIEN);
  78. mci_writel(host, MR, host->mode_reg);
  79. mci_writel(host, SDCR, host->sdc_reg);
  80. }
  81. static void atmci_disable(struct atmel_mci *host)
  82. {
  83. mci_writel(host, CR, MCI_CR_SWRST);
  84. /* Stall until write is complete, then disable the bus clock */
  85. mci_readl(host, SR);
  86. clk_disable(host->mck);
  87. }
  88. static inline unsigned int ns_to_clocks(struct atmel_mci *host,
  89. unsigned int ns)
  90. {
  91. return (ns * (host->bus_hz / 1000000) + 999) / 1000;
  92. }
  93. static void atmci_set_timeout(struct atmel_mci *host,
  94. struct mmc_data *data)
  95. {
  96. static unsigned dtomul_to_shift[] = {
  97. 0, 4, 7, 8, 10, 12, 16, 20
  98. };
  99. unsigned timeout;
  100. unsigned dtocyc;
  101. unsigned dtomul;
  102. timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
  103. for (dtomul = 0; dtomul < 8; dtomul++) {
  104. unsigned shift = dtomul_to_shift[dtomul];
  105. dtocyc = (timeout + (1 << shift) - 1) >> shift;
  106. if (dtocyc < 15)
  107. break;
  108. }
  109. if (dtomul >= 8) {
  110. dtomul = 7;
  111. dtocyc = 15;
  112. }
  113. dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
  114. dtocyc << dtomul_to_shift[dtomul]);
  115. mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
  116. }
  117. /*
  118. * Return mask with command flags to be enabled for this command.
  119. */
  120. static u32 atmci_prepare_command(struct mmc_host *mmc,
  121. struct mmc_command *cmd)
  122. {
  123. struct mmc_data *data;
  124. u32 cmdr;
  125. cmd->error = -EINPROGRESS;
  126. cmdr = MCI_CMDR_CMDNB(cmd->opcode);
  127. if (cmd->flags & MMC_RSP_PRESENT) {
  128. if (cmd->flags & MMC_RSP_136)
  129. cmdr |= MCI_CMDR_RSPTYP_136BIT;
  130. else
  131. cmdr |= MCI_CMDR_RSPTYP_48BIT;
  132. }
  133. /*
  134. * This should really be MAXLAT_5 for CMD2 and ACMD41, but
  135. * it's too difficult to determine whether this is an ACMD or
  136. * not. Better make it 64.
  137. */
  138. cmdr |= MCI_CMDR_MAXLAT_64CYC;
  139. if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
  140. cmdr |= MCI_CMDR_OPDCMD;
  141. data = cmd->data;
  142. if (data) {
  143. cmdr |= MCI_CMDR_START_XFER;
  144. if (data->flags & MMC_DATA_STREAM)
  145. cmdr |= MCI_CMDR_STREAM;
  146. else if (data->blocks > 1)
  147. cmdr |= MCI_CMDR_MULTI_BLOCK;
  148. else
  149. cmdr |= MCI_CMDR_BLOCK;
  150. if (data->flags & MMC_DATA_READ)
  151. cmdr |= MCI_CMDR_TRDIR_READ;
  152. }
  153. return cmdr;
  154. }
  155. static void atmci_start_command(struct atmel_mci *host,
  156. struct mmc_command *cmd,
  157. u32 cmd_flags)
  158. {
  159. /* Must read host->cmd after testing event flags */
  160. smp_rmb();
  161. WARN_ON(host->cmd);
  162. host->cmd = cmd;
  163. dev_vdbg(&host->mmc->class_dev,
  164. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  165. cmd->arg, cmd_flags);
  166. mci_writel(host, ARGR, cmd->arg);
  167. mci_writel(host, CMDR, cmd_flags);
  168. }
  169. static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
  170. {
  171. struct atmel_mci *host = mmc_priv(mmc);
  172. atmci_start_command(host, data->stop, host->stop_cmdr);
  173. mci_writel(host, IER, MCI_CMDRDY);
  174. }
  175. static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
  176. {
  177. struct atmel_mci *host = mmc_priv(mmc);
  178. WARN_ON(host->cmd || host->data);
  179. host->mrq = NULL;
  180. atmci_disable(host);
  181. mmc_request_done(mmc, mrq);
  182. }
  183. /*
  184. * Returns a mask of interrupt flags to be enabled after the whole
  185. * request has been prepared.
  186. */
  187. static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
  188. {
  189. struct atmel_mci *host = mmc_priv(mmc);
  190. u32 iflags;
  191. data->error = -EINPROGRESS;
  192. WARN_ON(host->data);
  193. host->sg = NULL;
  194. host->data = data;
  195. mci_writel(host, BLKR, MCI_BCNT(data->blocks)
  196. | MCI_BLKLEN(data->blksz));
  197. dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
  198. MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
  199. iflags = ATMCI_DATA_ERROR_FLAGS;
  200. host->sg = data->sg;
  201. host->pio_offset = 0;
  202. if (data->flags & MMC_DATA_READ)
  203. iflags |= MCI_RXRDY;
  204. else
  205. iflags |= MCI_TXRDY;
  206. return iflags;
  207. }
  208. static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  209. {
  210. struct atmel_mci *host = mmc_priv(mmc);
  211. struct mmc_data *data;
  212. struct mmc_command *cmd;
  213. u32 iflags;
  214. u32 cmdflags = 0;
  215. iflags = mci_readl(host, IMR);
  216. if (iflags)
  217. dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
  218. mci_readl(host, IMR));
  219. WARN_ON(host->mrq != NULL);
  220. /*
  221. * We may "know" the card is gone even though there's still an
  222. * electrical connection. If so, we really need to communicate
  223. * this to the MMC core since there won't be any more
  224. * interrupts as the card is completely removed. Otherwise,
  225. * the MMC core might believe the card is still there even
  226. * though the card was just removed very slowly.
  227. */
  228. if (!host->present) {
  229. mrq->cmd->error = -ENOMEDIUM;
  230. mmc_request_done(mmc, mrq);
  231. return;
  232. }
  233. host->mrq = mrq;
  234. host->pending_events = 0;
  235. host->completed_events = 0;
  236. atmci_enable(host);
  237. /* We don't support multiple blocks of weird lengths. */
  238. data = mrq->data;
  239. if (data) {
  240. if (data->blocks > 1 && data->blksz & 3)
  241. goto fail;
  242. atmci_set_timeout(host, data);
  243. }
  244. iflags = MCI_CMDRDY;
  245. cmd = mrq->cmd;
  246. cmdflags = atmci_prepare_command(mmc, cmd);
  247. atmci_start_command(host, cmd, cmdflags);
  248. if (data)
  249. iflags |= atmci_submit_data(mmc, data);
  250. if (mrq->stop) {
  251. host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
  252. host->stop_cmdr |= MCI_CMDR_STOP_XFER;
  253. if (!(data->flags & MMC_DATA_WRITE))
  254. host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
  255. if (data->flags & MMC_DATA_STREAM)
  256. host->stop_cmdr |= MCI_CMDR_STREAM;
  257. else
  258. host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
  259. }
  260. /*
  261. * We could have enabled interrupts earlier, but I suspect
  262. * that would open up a nice can of interesting race
  263. * conditions (e.g. command and data complete, but stop not
  264. * prepared yet.)
  265. */
  266. mci_writel(host, IER, iflags);
  267. return;
  268. fail:
  269. atmci_disable(host);
  270. host->mrq = NULL;
  271. mrq->cmd->error = -EINVAL;
  272. mmc_request_done(mmc, mrq);
  273. }
  274. static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  275. {
  276. struct atmel_mci *host = mmc_priv(mmc);
  277. if (ios->clock) {
  278. u32 clkdiv;
  279. /* Set clock rate */
  280. clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
  281. if (clkdiv > 255) {
  282. dev_warn(&mmc->class_dev,
  283. "clock %u too slow; using %lu\n",
  284. ios->clock, host->bus_hz / (2 * 256));
  285. clkdiv = 255;
  286. }
  287. host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
  288. | MCI_MR_RDPROOF;
  289. }
  290. switch (ios->bus_width) {
  291. case MMC_BUS_WIDTH_1:
  292. host->sdc_reg = 0;
  293. break;
  294. case MMC_BUS_WIDTH_4:
  295. host->sdc_reg = MCI_SDCBUS_4BIT;
  296. break;
  297. }
  298. switch (ios->power_mode) {
  299. case MMC_POWER_ON:
  300. /* Send init sequence (74 clock cycles) */
  301. atmci_enable(host);
  302. mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
  303. while (!(mci_readl(host, SR) & MCI_CMDRDY))
  304. cpu_relax();
  305. atmci_disable(host);
  306. break;
  307. default:
  308. /*
  309. * TODO: None of the currently available AVR32-based
  310. * boards allow MMC power to be turned off. Implement
  311. * power control when this can be tested properly.
  312. */
  313. break;
  314. }
  315. }
  316. static int atmci_get_ro(struct mmc_host *mmc)
  317. {
  318. int read_only = 0;
  319. struct atmel_mci *host = mmc_priv(mmc);
  320. if (host->wp_pin >= 0) {
  321. read_only = gpio_get_value(host->wp_pin);
  322. dev_dbg(&mmc->class_dev, "card is %s\n",
  323. read_only ? "read-only" : "read-write");
  324. } else {
  325. dev_dbg(&mmc->class_dev,
  326. "no pin for checking read-only switch."
  327. " Assuming write-enable.\n");
  328. }
  329. return read_only;
  330. }
  331. static struct mmc_host_ops atmci_ops = {
  332. .request = atmci_request,
  333. .set_ios = atmci_set_ios,
  334. .get_ro = atmci_get_ro,
  335. };
  336. static void atmci_command_complete(struct atmel_mci *host,
  337. struct mmc_command *cmd, u32 status)
  338. {
  339. /* Read the response from the card (up to 16 bytes) */
  340. cmd->resp[0] = mci_readl(host, RSPR);
  341. cmd->resp[1] = mci_readl(host, RSPR);
  342. cmd->resp[2] = mci_readl(host, RSPR);
  343. cmd->resp[3] = mci_readl(host, RSPR);
  344. if (status & MCI_RTOE)
  345. cmd->error = -ETIMEDOUT;
  346. else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
  347. cmd->error = -EILSEQ;
  348. else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
  349. cmd->error = -EIO;
  350. else
  351. cmd->error = 0;
  352. if (cmd->error) {
  353. dev_dbg(&host->mmc->class_dev,
  354. "command error: status=0x%08x\n", status);
  355. if (cmd->data) {
  356. host->data = NULL;
  357. mci_writel(host, IDR, MCI_NOTBUSY
  358. | MCI_TXRDY | MCI_RXRDY
  359. | ATMCI_DATA_ERROR_FLAGS);
  360. }
  361. }
  362. }
  363. static void atmci_detect_change(unsigned long data)
  364. {
  365. struct atmel_mci *host = (struct atmel_mci *)data;
  366. struct mmc_request *mrq = host->mrq;
  367. int present;
  368. /*
  369. * atmci_remove() sets detect_pin to -1 before freeing the
  370. * interrupt. We must not re-enable the interrupt if it has
  371. * been freed.
  372. */
  373. smp_rmb();
  374. if (host->detect_pin < 0)
  375. return;
  376. enable_irq(gpio_to_irq(host->detect_pin));
  377. present = !gpio_get_value(host->detect_pin);
  378. dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
  379. present, host->present);
  380. if (present != host->present) {
  381. dev_dbg(&host->mmc->class_dev, "card %s\n",
  382. present ? "inserted" : "removed");
  383. host->present = present;
  384. /* Reset controller if card is gone */
  385. if (!present) {
  386. mci_writel(host, CR, MCI_CR_SWRST);
  387. mci_writel(host, IDR, ~0UL);
  388. mci_writel(host, CR, MCI_CR_MCIEN);
  389. }
  390. /* Clean up queue if present */
  391. if (mrq) {
  392. /*
  393. * Reset controller to terminate any ongoing
  394. * commands or data transfers.
  395. */
  396. mci_writel(host, CR, MCI_CR_SWRST);
  397. if (!atmci_is_completed(host, EVENT_CMD_COMPLETE))
  398. mrq->cmd->error = -ENOMEDIUM;
  399. if (mrq->data && !atmci_is_completed(host,
  400. EVENT_DATA_COMPLETE)) {
  401. host->data = NULL;
  402. mrq->data->error = -ENOMEDIUM;
  403. }
  404. if (mrq->stop && !atmci_is_completed(host,
  405. EVENT_STOP_COMPLETE))
  406. mrq->stop->error = -ENOMEDIUM;
  407. host->cmd = NULL;
  408. atmci_request_end(host->mmc, mrq);
  409. }
  410. mmc_detect_change(host->mmc, 0);
  411. }
  412. }
  413. static void atmci_tasklet_func(unsigned long priv)
  414. {
  415. struct mmc_host *mmc = (struct mmc_host *)priv;
  416. struct atmel_mci *host = mmc_priv(mmc);
  417. struct mmc_request *mrq = host->mrq;
  418. struct mmc_data *data = host->data;
  419. dev_vdbg(&mmc->class_dev,
  420. "tasklet: pending/completed/mask %lx/%lx/%x\n",
  421. host->pending_events, host->completed_events,
  422. mci_readl(host, IMR));
  423. if (atmci_test_and_clear_pending(host, EVENT_CMD_COMPLETE)) {
  424. /*
  425. * host->cmd must be set to NULL before the interrupt
  426. * handler sees EVENT_CMD_COMPLETE
  427. */
  428. host->cmd = NULL;
  429. smp_wmb();
  430. atmci_set_completed(host, EVENT_CMD_COMPLETE);
  431. atmci_command_complete(host, mrq->cmd, host->cmd_status);
  432. if (!mrq->cmd->error && mrq->stop
  433. && atmci_is_completed(host, EVENT_XFER_COMPLETE)
  434. && !atmci_test_and_set_completed(host,
  435. EVENT_STOP_SENT))
  436. send_stop_cmd(host->mmc, mrq->data);
  437. }
  438. if (atmci_test_and_clear_pending(host, EVENT_STOP_COMPLETE)) {
  439. /*
  440. * host->cmd must be set to NULL before the interrupt
  441. * handler sees EVENT_STOP_COMPLETE
  442. */
  443. host->cmd = NULL;
  444. smp_wmb();
  445. atmci_set_completed(host, EVENT_STOP_COMPLETE);
  446. atmci_command_complete(host, mrq->stop, host->stop_status);
  447. }
  448. if (atmci_test_and_clear_pending(host, EVENT_DATA_ERROR)) {
  449. u32 status = host->data_status;
  450. dev_vdbg(&mmc->class_dev, "data error: status=%08x\n", status);
  451. atmci_set_completed(host, EVENT_DATA_ERROR);
  452. atmci_set_completed(host, EVENT_DATA_COMPLETE);
  453. if (status & MCI_DTOE) {
  454. dev_dbg(&mmc->class_dev,
  455. "data timeout error\n");
  456. data->error = -ETIMEDOUT;
  457. } else if (status & MCI_DCRCE) {
  458. dev_dbg(&mmc->class_dev, "data CRC error\n");
  459. data->error = -EILSEQ;
  460. } else {
  461. dev_dbg(&mmc->class_dev,
  462. "data FIFO error (status=%08x)\n",
  463. status);
  464. data->error = -EIO;
  465. }
  466. if (host->present && data->stop
  467. && atmci_is_completed(host, EVENT_CMD_COMPLETE)
  468. && !atmci_test_and_set_completed(
  469. host, EVENT_STOP_SENT))
  470. send_stop_cmd(host->mmc, data);
  471. host->data = NULL;
  472. }
  473. if (atmci_test_and_clear_pending(host, EVENT_DATA_COMPLETE)) {
  474. atmci_set_completed(host, EVENT_DATA_COMPLETE);
  475. if (!atmci_is_completed(host, EVENT_DATA_ERROR)) {
  476. data->bytes_xfered = data->blocks * data->blksz;
  477. data->error = 0;
  478. }
  479. host->data = NULL;
  480. }
  481. if (host->mrq && !host->cmd && !host->data)
  482. atmci_request_end(mmc, host->mrq);
  483. }
  484. static void atmci_read_data_pio(struct atmel_mci *host)
  485. {
  486. struct scatterlist *sg = host->sg;
  487. void *buf = sg_virt(sg);
  488. unsigned int offset = host->pio_offset;
  489. struct mmc_data *data = host->data;
  490. u32 value;
  491. u32 status;
  492. unsigned int nbytes = 0;
  493. do {
  494. value = mci_readl(host, RDR);
  495. if (likely(offset + 4 <= sg->length)) {
  496. put_unaligned(value, (u32 *)(buf + offset));
  497. offset += 4;
  498. nbytes += 4;
  499. if (offset == sg->length) {
  500. host->sg = sg = sg_next(sg);
  501. if (!sg)
  502. goto done;
  503. offset = 0;
  504. buf = sg_virt(sg);
  505. }
  506. } else {
  507. unsigned int remaining = sg->length - offset;
  508. memcpy(buf + offset, &value, remaining);
  509. nbytes += remaining;
  510. flush_dcache_page(sg_page(sg));
  511. host->sg = sg = sg_next(sg);
  512. if (!sg)
  513. goto done;
  514. offset = 4 - remaining;
  515. buf = sg_virt(sg);
  516. memcpy(buf, (u8 *)&value + remaining, offset);
  517. nbytes += offset;
  518. }
  519. status = mci_readl(host, SR);
  520. if (status & ATMCI_DATA_ERROR_FLAGS) {
  521. mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
  522. | ATMCI_DATA_ERROR_FLAGS));
  523. host->data_status = status;
  524. atmci_set_pending(host, EVENT_DATA_ERROR);
  525. tasklet_schedule(&host->tasklet);
  526. break;
  527. }
  528. } while (status & MCI_RXRDY);
  529. host->pio_offset = offset;
  530. data->bytes_xfered += nbytes;
  531. return;
  532. done:
  533. mci_writel(host, IDR, MCI_RXRDY);
  534. mci_writel(host, IER, MCI_NOTBUSY);
  535. data->bytes_xfered += nbytes;
  536. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  537. if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
  538. && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
  539. send_stop_cmd(host->mmc, data);
  540. }
  541. static void atmci_write_data_pio(struct atmel_mci *host)
  542. {
  543. struct scatterlist *sg = host->sg;
  544. void *buf = sg_virt(sg);
  545. unsigned int offset = host->pio_offset;
  546. struct mmc_data *data = host->data;
  547. u32 value;
  548. u32 status;
  549. unsigned int nbytes = 0;
  550. do {
  551. if (likely(offset + 4 <= sg->length)) {
  552. value = get_unaligned((u32 *)(buf + offset));
  553. mci_writel(host, TDR, value);
  554. offset += 4;
  555. nbytes += 4;
  556. if (offset == sg->length) {
  557. host->sg = sg = sg_next(sg);
  558. if (!sg)
  559. goto done;
  560. offset = 0;
  561. buf = sg_virt(sg);
  562. }
  563. } else {
  564. unsigned int remaining = sg->length - offset;
  565. value = 0;
  566. memcpy(&value, buf + offset, remaining);
  567. nbytes += remaining;
  568. host->sg = sg = sg_next(sg);
  569. if (!sg) {
  570. mci_writel(host, TDR, value);
  571. goto done;
  572. }
  573. offset = 4 - remaining;
  574. buf = sg_virt(sg);
  575. memcpy((u8 *)&value + remaining, buf, offset);
  576. mci_writel(host, TDR, value);
  577. nbytes += offset;
  578. }
  579. status = mci_readl(host, SR);
  580. if (status & ATMCI_DATA_ERROR_FLAGS) {
  581. mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
  582. | ATMCI_DATA_ERROR_FLAGS));
  583. host->data_status = status;
  584. atmci_set_pending(host, EVENT_DATA_ERROR);
  585. tasklet_schedule(&host->tasklet);
  586. break;
  587. }
  588. } while (status & MCI_TXRDY);
  589. host->pio_offset = offset;
  590. data->bytes_xfered += nbytes;
  591. return;
  592. done:
  593. mci_writel(host, IDR, MCI_TXRDY);
  594. mci_writel(host, IER, MCI_NOTBUSY);
  595. data->bytes_xfered += nbytes;
  596. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  597. if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
  598. && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
  599. send_stop_cmd(host->mmc, data);
  600. }
  601. static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
  602. {
  603. struct atmel_mci *host = mmc_priv(mmc);
  604. mci_writel(host, IDR, MCI_CMDRDY);
  605. if (atmci_is_completed(host, EVENT_STOP_SENT)) {
  606. host->stop_status = status;
  607. atmci_set_pending(host, EVENT_STOP_COMPLETE);
  608. } else {
  609. host->cmd_status = status;
  610. atmci_set_pending(host, EVENT_CMD_COMPLETE);
  611. }
  612. tasklet_schedule(&host->tasklet);
  613. }
  614. static irqreturn_t atmci_interrupt(int irq, void *dev_id)
  615. {
  616. struct mmc_host *mmc = dev_id;
  617. struct atmel_mci *host = mmc_priv(mmc);
  618. u32 status, mask, pending;
  619. unsigned int pass_count = 0;
  620. spin_lock(&mmc->lock);
  621. do {
  622. status = mci_readl(host, SR);
  623. mask = mci_readl(host, IMR);
  624. pending = status & mask;
  625. if (!pending)
  626. break;
  627. if (pending & ATMCI_DATA_ERROR_FLAGS) {
  628. mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
  629. | MCI_RXRDY | MCI_TXRDY);
  630. pending &= mci_readl(host, IMR);
  631. host->data_status = status;
  632. atmci_set_pending(host, EVENT_DATA_ERROR);
  633. tasklet_schedule(&host->tasklet);
  634. }
  635. if (pending & MCI_NOTBUSY) {
  636. mci_writel(host, IDR, (MCI_NOTBUSY
  637. | ATMCI_DATA_ERROR_FLAGS));
  638. atmci_set_pending(host, EVENT_DATA_COMPLETE);
  639. tasklet_schedule(&host->tasklet);
  640. }
  641. if (pending & MCI_RXRDY)
  642. atmci_read_data_pio(host);
  643. if (pending & MCI_TXRDY)
  644. atmci_write_data_pio(host);
  645. if (pending & MCI_CMDRDY)
  646. atmci_cmd_interrupt(mmc, status);
  647. } while (pass_count++ < 5);
  648. spin_unlock(&mmc->lock);
  649. return pass_count ? IRQ_HANDLED : IRQ_NONE;
  650. }
  651. static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
  652. {
  653. struct mmc_host *mmc = dev_id;
  654. struct atmel_mci *host = mmc_priv(mmc);
  655. /*
  656. * Disable interrupts until the pin has stabilized and check
  657. * the state then. Use mod_timer() since we may be in the
  658. * middle of the timer routine when this interrupt triggers.
  659. */
  660. disable_irq_nosync(irq);
  661. mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
  662. return IRQ_HANDLED;
  663. }
  664. static int __init atmci_probe(struct platform_device *pdev)
  665. {
  666. struct mci_platform_data *pdata;
  667. struct atmel_mci *host;
  668. struct mmc_host *mmc;
  669. struct resource *regs;
  670. int irq;
  671. int ret;
  672. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  673. if (!regs)
  674. return -ENXIO;
  675. pdata = pdev->dev.platform_data;
  676. if (!pdata)
  677. return -ENXIO;
  678. irq = platform_get_irq(pdev, 0);
  679. if (irq < 0)
  680. return irq;
  681. mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
  682. if (!mmc)
  683. return -ENOMEM;
  684. host = mmc_priv(mmc);
  685. host->pdev = pdev;
  686. host->mmc = mmc;
  687. host->detect_pin = pdata->detect_pin;
  688. host->wp_pin = pdata->wp_pin;
  689. host->mck = clk_get(&pdev->dev, "mci_clk");
  690. if (IS_ERR(host->mck)) {
  691. ret = PTR_ERR(host->mck);
  692. goto err_clk_get;
  693. }
  694. ret = -ENOMEM;
  695. host->regs = ioremap(regs->start, regs->end - regs->start + 1);
  696. if (!host->regs)
  697. goto err_ioremap;
  698. clk_enable(host->mck);
  699. mci_writel(host, CR, MCI_CR_SWRST);
  700. host->bus_hz = clk_get_rate(host->mck);
  701. clk_disable(host->mck);
  702. host->mapbase = regs->start;
  703. mmc->ops = &atmci_ops;
  704. mmc->f_min = (host->bus_hz + 511) / 512;
  705. mmc->f_max = host->bus_hz / 2;
  706. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  707. mmc->caps |= MMC_CAP_4_BIT_DATA;
  708. mmc->max_hw_segs = 64;
  709. mmc->max_phys_segs = 64;
  710. mmc->max_req_size = 32768 * 512;
  711. mmc->max_blk_size = 32768;
  712. mmc->max_blk_count = 512;
  713. tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
  714. ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
  715. if (ret)
  716. goto err_request_irq;
  717. /* Assume card is present if we don't have a detect pin */
  718. host->present = 1;
  719. if (host->detect_pin >= 0) {
  720. if (gpio_request(host->detect_pin, "mmc_detect")) {
  721. dev_dbg(&mmc->class_dev, "no detect pin available\n");
  722. host->detect_pin = -1;
  723. } else {
  724. host->present = !gpio_get_value(host->detect_pin);
  725. }
  726. }
  727. if (host->wp_pin >= 0) {
  728. if (gpio_request(host->wp_pin, "mmc_wp")) {
  729. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  730. host->wp_pin = -1;
  731. }
  732. }
  733. platform_set_drvdata(pdev, host);
  734. mmc_add_host(mmc);
  735. if (host->detect_pin >= 0) {
  736. setup_timer(&host->detect_timer, atmci_detect_change,
  737. (unsigned long)host);
  738. ret = request_irq(gpio_to_irq(host->detect_pin),
  739. atmci_detect_interrupt,
  740. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  741. "mmc-detect", mmc);
  742. if (ret) {
  743. dev_dbg(&mmc->class_dev,
  744. "could not request IRQ %d for detect pin\n",
  745. gpio_to_irq(host->detect_pin));
  746. gpio_free(host->detect_pin);
  747. host->detect_pin = -1;
  748. }
  749. }
  750. dev_info(&mmc->class_dev,
  751. "Atmel MCI controller at 0x%08lx irq %d\n",
  752. host->mapbase, irq);
  753. return 0;
  754. err_request_irq:
  755. iounmap(host->regs);
  756. err_ioremap:
  757. clk_put(host->mck);
  758. err_clk_get:
  759. mmc_free_host(mmc);
  760. return ret;
  761. }
  762. static int __exit atmci_remove(struct platform_device *pdev)
  763. {
  764. struct atmel_mci *host = platform_get_drvdata(pdev);
  765. platform_set_drvdata(pdev, NULL);
  766. if (host) {
  767. if (host->detect_pin >= 0) {
  768. int pin = host->detect_pin;
  769. /* Make sure the timer doesn't enable the interrupt */
  770. host->detect_pin = -1;
  771. smp_wmb();
  772. free_irq(gpio_to_irq(pin), host->mmc);
  773. del_timer_sync(&host->detect_timer);
  774. gpio_free(pin);
  775. }
  776. mmc_remove_host(host->mmc);
  777. clk_enable(host->mck);
  778. mci_writel(host, IDR, ~0UL);
  779. mci_writel(host, CR, MCI_CR_MCIDIS);
  780. mci_readl(host, SR);
  781. clk_disable(host->mck);
  782. if (host->wp_pin >= 0)
  783. gpio_free(host->wp_pin);
  784. free_irq(platform_get_irq(pdev, 0), host->mmc);
  785. iounmap(host->regs);
  786. clk_put(host->mck);
  787. mmc_free_host(host->mmc);
  788. }
  789. return 0;
  790. }
  791. static struct platform_driver atmci_driver = {
  792. .remove = __exit_p(atmci_remove),
  793. .driver = {
  794. .name = "atmel_mci",
  795. },
  796. };
  797. static int __init atmci_init(void)
  798. {
  799. return platform_driver_probe(&atmci_driver, atmci_probe);
  800. }
  801. static void __exit atmci_exit(void)
  802. {
  803. platform_driver_unregister(&atmci_driver);
  804. }
  805. module_init(atmci_init);
  806. module_exit(atmci_exit);
  807. MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
  808. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
  809. MODULE_LICENSE("GPL v2");