atmel-mci.c 27 KB

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