atmel-mci.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 <mach/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. clk_enable(host->mck);
  170. memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
  171. clk_disable(host->mck);
  172. spin_unlock_irq(&host->mmc->lock);
  173. seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
  174. buf[MCI_MR / 4],
  175. buf[MCI_MR / 4] & MCI_MR_RDPROOF ? " RDPROOF" : "",
  176. buf[MCI_MR / 4] & MCI_MR_WRPROOF ? " WRPROOF" : "",
  177. buf[MCI_MR / 4] & 0xff);
  178. seq_printf(s, "DTOR:\t0x%08x\n", buf[MCI_DTOR / 4]);
  179. seq_printf(s, "SDCR:\t0x%08x\n", buf[MCI_SDCR / 4]);
  180. seq_printf(s, "ARGR:\t0x%08x\n", buf[MCI_ARGR / 4]);
  181. seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
  182. buf[MCI_BLKR / 4],
  183. buf[MCI_BLKR / 4] & 0xffff,
  184. (buf[MCI_BLKR / 4] >> 16) & 0xffff);
  185. /* Don't read RSPR and RDR; it will consume the data there */
  186. atmci_show_status_reg(s, "SR", buf[MCI_SR / 4]);
  187. atmci_show_status_reg(s, "IMR", buf[MCI_IMR / 4]);
  188. kfree(buf);
  189. return 0;
  190. }
  191. static int atmci_regs_open(struct inode *inode, struct file *file)
  192. {
  193. return single_open(file, atmci_regs_show, inode->i_private);
  194. }
  195. static const struct file_operations atmci_regs_fops = {
  196. .owner = THIS_MODULE,
  197. .open = atmci_regs_open,
  198. .read = seq_read,
  199. .llseek = seq_lseek,
  200. .release = single_release,
  201. };
  202. static void atmci_init_debugfs(struct atmel_mci *host)
  203. {
  204. struct mmc_host *mmc;
  205. struct dentry *root;
  206. struct dentry *node;
  207. mmc = host->mmc;
  208. root = mmc->debugfs_root;
  209. if (!root)
  210. return;
  211. node = debugfs_create_file("regs", S_IRUSR, root, host,
  212. &atmci_regs_fops);
  213. if (IS_ERR(node))
  214. return;
  215. if (!node)
  216. goto err;
  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. dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
  355. MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
  356. iflags = ATMCI_DATA_ERROR_FLAGS;
  357. host->sg = data->sg;
  358. host->pio_offset = 0;
  359. if (data->flags & MMC_DATA_READ)
  360. iflags |= MCI_RXRDY;
  361. else
  362. iflags |= MCI_TXRDY;
  363. return iflags;
  364. }
  365. static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  366. {
  367. struct atmel_mci *host = mmc_priv(mmc);
  368. struct mmc_data *data;
  369. struct mmc_command *cmd;
  370. u32 iflags;
  371. u32 cmdflags = 0;
  372. iflags = mci_readl(host, IMR);
  373. if (iflags)
  374. dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
  375. mci_readl(host, IMR));
  376. WARN_ON(host->mrq != NULL);
  377. /*
  378. * We may "know" the card is gone even though there's still an
  379. * electrical connection. If so, we really need to communicate
  380. * this to the MMC core since there won't be any more
  381. * interrupts as the card is completely removed. Otherwise,
  382. * the MMC core might believe the card is still there even
  383. * though the card was just removed very slowly.
  384. */
  385. if (!host->present) {
  386. mrq->cmd->error = -ENOMEDIUM;
  387. mmc_request_done(mmc, mrq);
  388. return;
  389. }
  390. host->mrq = mrq;
  391. host->pending_events = 0;
  392. host->completed_events = 0;
  393. atmci_enable(host);
  394. /* We don't support multiple blocks of weird lengths. */
  395. data = mrq->data;
  396. if (data) {
  397. if (data->blocks > 1 && data->blksz & 3)
  398. goto fail;
  399. atmci_set_timeout(host, data);
  400. /* Must set block count/size before sending command */
  401. mci_writel(host, BLKR, MCI_BCNT(data->blocks)
  402. | MCI_BLKLEN(data->blksz));
  403. }
  404. iflags = MCI_CMDRDY;
  405. cmd = mrq->cmd;
  406. cmdflags = atmci_prepare_command(mmc, cmd);
  407. atmci_start_command(host, cmd, cmdflags);
  408. if (data)
  409. iflags |= atmci_submit_data(mmc, data);
  410. if (mrq->stop) {
  411. host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
  412. host->stop_cmdr |= MCI_CMDR_STOP_XFER;
  413. if (!(data->flags & MMC_DATA_WRITE))
  414. host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
  415. if (data->flags & MMC_DATA_STREAM)
  416. host->stop_cmdr |= MCI_CMDR_STREAM;
  417. else
  418. host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
  419. }
  420. /*
  421. * We could have enabled interrupts earlier, but I suspect
  422. * that would open up a nice can of interesting race
  423. * conditions (e.g. command and data complete, but stop not
  424. * prepared yet.)
  425. */
  426. mci_writel(host, IER, iflags);
  427. return;
  428. fail:
  429. atmci_disable(host);
  430. host->mrq = NULL;
  431. mrq->cmd->error = -EINVAL;
  432. mmc_request_done(mmc, mrq);
  433. }
  434. static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  435. {
  436. struct atmel_mci *host = mmc_priv(mmc);
  437. if (ios->clock) {
  438. u32 clkdiv;
  439. /* Set clock rate */
  440. clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
  441. if (clkdiv > 255) {
  442. dev_warn(&mmc->class_dev,
  443. "clock %u too slow; using %lu\n",
  444. ios->clock, host->bus_hz / (2 * 256));
  445. clkdiv = 255;
  446. }
  447. host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
  448. | MCI_MR_RDPROOF;
  449. }
  450. switch (ios->bus_width) {
  451. case MMC_BUS_WIDTH_1:
  452. host->sdc_reg = 0;
  453. break;
  454. case MMC_BUS_WIDTH_4:
  455. host->sdc_reg = MCI_SDCBUS_4BIT;
  456. break;
  457. }
  458. switch (ios->power_mode) {
  459. case MMC_POWER_ON:
  460. /* Send init sequence (74 clock cycles) */
  461. atmci_enable(host);
  462. mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
  463. while (!(mci_readl(host, SR) & MCI_CMDRDY))
  464. cpu_relax();
  465. atmci_disable(host);
  466. break;
  467. default:
  468. /*
  469. * TODO: None of the currently available AVR32-based
  470. * boards allow MMC power to be turned off. Implement
  471. * power control when this can be tested properly.
  472. */
  473. break;
  474. }
  475. }
  476. static int atmci_get_ro(struct mmc_host *mmc)
  477. {
  478. int read_only = 0;
  479. struct atmel_mci *host = mmc_priv(mmc);
  480. if (gpio_is_valid(host->wp_pin)) {
  481. read_only = gpio_get_value(host->wp_pin);
  482. dev_dbg(&mmc->class_dev, "card is %s\n",
  483. read_only ? "read-only" : "read-write");
  484. } else {
  485. dev_dbg(&mmc->class_dev,
  486. "no pin for checking read-only switch."
  487. " Assuming write-enable.\n");
  488. }
  489. return read_only;
  490. }
  491. static struct mmc_host_ops atmci_ops = {
  492. .request = atmci_request,
  493. .set_ios = atmci_set_ios,
  494. .get_ro = atmci_get_ro,
  495. };
  496. static void atmci_command_complete(struct atmel_mci *host,
  497. struct mmc_command *cmd, u32 status)
  498. {
  499. /* Read the response from the card (up to 16 bytes) */
  500. cmd->resp[0] = mci_readl(host, RSPR);
  501. cmd->resp[1] = mci_readl(host, RSPR);
  502. cmd->resp[2] = mci_readl(host, RSPR);
  503. cmd->resp[3] = mci_readl(host, RSPR);
  504. if (status & MCI_RTOE)
  505. cmd->error = -ETIMEDOUT;
  506. else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
  507. cmd->error = -EILSEQ;
  508. else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
  509. cmd->error = -EIO;
  510. else
  511. cmd->error = 0;
  512. if (cmd->error) {
  513. dev_dbg(&host->mmc->class_dev,
  514. "command error: status=0x%08x\n", status);
  515. if (cmd->data) {
  516. host->data = NULL;
  517. mci_writel(host, IDR, MCI_NOTBUSY
  518. | MCI_TXRDY | MCI_RXRDY
  519. | ATMCI_DATA_ERROR_FLAGS);
  520. }
  521. }
  522. }
  523. static void atmci_detect_change(unsigned long data)
  524. {
  525. struct atmel_mci *host = (struct atmel_mci *)data;
  526. struct mmc_request *mrq = host->mrq;
  527. int present;
  528. /*
  529. * atmci_remove() sets detect_pin to -1 before freeing the
  530. * interrupt. We must not re-enable the interrupt if it has
  531. * been freed.
  532. */
  533. smp_rmb();
  534. if (!gpio_is_valid(host->detect_pin))
  535. return;
  536. enable_irq(gpio_to_irq(host->detect_pin));
  537. present = !gpio_get_value(host->detect_pin);
  538. dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
  539. present, host->present);
  540. if (present != host->present) {
  541. dev_dbg(&host->mmc->class_dev, "card %s\n",
  542. present ? "inserted" : "removed");
  543. host->present = present;
  544. /* Reset controller if card is gone */
  545. if (!present) {
  546. mci_writel(host, CR, MCI_CR_SWRST);
  547. mci_writel(host, IDR, ~0UL);
  548. mci_writel(host, CR, MCI_CR_MCIEN);
  549. }
  550. /* Clean up queue if present */
  551. if (mrq) {
  552. /*
  553. * Reset controller to terminate any ongoing
  554. * commands or data transfers.
  555. */
  556. mci_writel(host, CR, MCI_CR_SWRST);
  557. if (!atmci_is_completed(host, EVENT_CMD_COMPLETE))
  558. mrq->cmd->error = -ENOMEDIUM;
  559. if (mrq->data && !atmci_is_completed(host,
  560. EVENT_DATA_COMPLETE)) {
  561. host->data = NULL;
  562. mrq->data->error = -ENOMEDIUM;
  563. }
  564. if (mrq->stop && !atmci_is_completed(host,
  565. EVENT_STOP_COMPLETE))
  566. mrq->stop->error = -ENOMEDIUM;
  567. host->cmd = NULL;
  568. atmci_request_end(host->mmc, mrq);
  569. }
  570. mmc_detect_change(host->mmc, 0);
  571. }
  572. }
  573. static void atmci_tasklet_func(unsigned long priv)
  574. {
  575. struct mmc_host *mmc = (struct mmc_host *)priv;
  576. struct atmel_mci *host = mmc_priv(mmc);
  577. struct mmc_request *mrq = host->mrq;
  578. struct mmc_data *data = host->data;
  579. dev_vdbg(&mmc->class_dev,
  580. "tasklet: pending/completed/mask %lx/%lx/%x\n",
  581. host->pending_events, host->completed_events,
  582. mci_readl(host, IMR));
  583. if (atmci_test_and_clear_pending(host, EVENT_CMD_COMPLETE)) {
  584. /*
  585. * host->cmd must be set to NULL before the interrupt
  586. * handler sees EVENT_CMD_COMPLETE
  587. */
  588. host->cmd = NULL;
  589. smp_wmb();
  590. atmci_set_completed(host, EVENT_CMD_COMPLETE);
  591. atmci_command_complete(host, mrq->cmd, host->cmd_status);
  592. if (!mrq->cmd->error && mrq->stop
  593. && atmci_is_completed(host, EVENT_XFER_COMPLETE)
  594. && !atmci_test_and_set_completed(host,
  595. EVENT_STOP_SENT))
  596. send_stop_cmd(host->mmc, mrq->data);
  597. }
  598. if (atmci_test_and_clear_pending(host, EVENT_STOP_COMPLETE)) {
  599. /*
  600. * host->cmd must be set to NULL before the interrupt
  601. * handler sees EVENT_STOP_COMPLETE
  602. */
  603. host->cmd = NULL;
  604. smp_wmb();
  605. atmci_set_completed(host, EVENT_STOP_COMPLETE);
  606. atmci_command_complete(host, mrq->stop, host->stop_status);
  607. }
  608. if (atmci_test_and_clear_pending(host, EVENT_DATA_ERROR)) {
  609. u32 status = host->data_status;
  610. dev_vdbg(&mmc->class_dev, "data error: status=%08x\n", status);
  611. atmci_set_completed(host, EVENT_DATA_ERROR);
  612. atmci_set_completed(host, EVENT_DATA_COMPLETE);
  613. if (status & MCI_DTOE) {
  614. dev_dbg(&mmc->class_dev,
  615. "data timeout error\n");
  616. data->error = -ETIMEDOUT;
  617. } else if (status & MCI_DCRCE) {
  618. dev_dbg(&mmc->class_dev, "data CRC error\n");
  619. data->error = -EILSEQ;
  620. } else {
  621. dev_dbg(&mmc->class_dev,
  622. "data FIFO error (status=%08x)\n",
  623. status);
  624. data->error = -EIO;
  625. }
  626. if (host->present && data->stop
  627. && atmci_is_completed(host, EVENT_CMD_COMPLETE)
  628. && !atmci_test_and_set_completed(
  629. host, EVENT_STOP_SENT))
  630. send_stop_cmd(host->mmc, data);
  631. host->data = NULL;
  632. }
  633. if (atmci_test_and_clear_pending(host, EVENT_DATA_COMPLETE)) {
  634. atmci_set_completed(host, EVENT_DATA_COMPLETE);
  635. if (!atmci_is_completed(host, EVENT_DATA_ERROR)) {
  636. data->bytes_xfered = data->blocks * data->blksz;
  637. data->error = 0;
  638. }
  639. host->data = NULL;
  640. }
  641. if (host->mrq && !host->cmd && !host->data)
  642. atmci_request_end(mmc, host->mrq);
  643. }
  644. static void atmci_read_data_pio(struct atmel_mci *host)
  645. {
  646. struct scatterlist *sg = host->sg;
  647. void *buf = sg_virt(sg);
  648. unsigned int offset = host->pio_offset;
  649. struct mmc_data *data = host->data;
  650. u32 value;
  651. u32 status;
  652. unsigned int nbytes = 0;
  653. do {
  654. value = mci_readl(host, RDR);
  655. if (likely(offset + 4 <= sg->length)) {
  656. put_unaligned(value, (u32 *)(buf + offset));
  657. offset += 4;
  658. nbytes += 4;
  659. if (offset == sg->length) {
  660. host->sg = sg = sg_next(sg);
  661. if (!sg)
  662. goto done;
  663. offset = 0;
  664. buf = sg_virt(sg);
  665. }
  666. } else {
  667. unsigned int remaining = sg->length - offset;
  668. memcpy(buf + offset, &value, remaining);
  669. nbytes += remaining;
  670. flush_dcache_page(sg_page(sg));
  671. host->sg = sg = sg_next(sg);
  672. if (!sg)
  673. goto done;
  674. offset = 4 - remaining;
  675. buf = sg_virt(sg);
  676. memcpy(buf, (u8 *)&value + remaining, offset);
  677. nbytes += offset;
  678. }
  679. status = mci_readl(host, SR);
  680. if (status & ATMCI_DATA_ERROR_FLAGS) {
  681. mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
  682. | ATMCI_DATA_ERROR_FLAGS));
  683. host->data_status = status;
  684. atmci_set_pending(host, EVENT_DATA_ERROR);
  685. tasklet_schedule(&host->tasklet);
  686. break;
  687. }
  688. } while (status & MCI_RXRDY);
  689. host->pio_offset = offset;
  690. data->bytes_xfered += nbytes;
  691. return;
  692. done:
  693. mci_writel(host, IDR, MCI_RXRDY);
  694. mci_writel(host, IER, MCI_NOTBUSY);
  695. data->bytes_xfered += nbytes;
  696. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  697. if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
  698. && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
  699. send_stop_cmd(host->mmc, data);
  700. }
  701. static void atmci_write_data_pio(struct atmel_mci *host)
  702. {
  703. struct scatterlist *sg = host->sg;
  704. void *buf = sg_virt(sg);
  705. unsigned int offset = host->pio_offset;
  706. struct mmc_data *data = host->data;
  707. u32 value;
  708. u32 status;
  709. unsigned int nbytes = 0;
  710. do {
  711. if (likely(offset + 4 <= sg->length)) {
  712. value = get_unaligned((u32 *)(buf + offset));
  713. mci_writel(host, TDR, value);
  714. offset += 4;
  715. nbytes += 4;
  716. if (offset == sg->length) {
  717. host->sg = sg = sg_next(sg);
  718. if (!sg)
  719. goto done;
  720. offset = 0;
  721. buf = sg_virt(sg);
  722. }
  723. } else {
  724. unsigned int remaining = sg->length - offset;
  725. value = 0;
  726. memcpy(&value, buf + offset, remaining);
  727. nbytes += remaining;
  728. host->sg = sg = sg_next(sg);
  729. if (!sg) {
  730. mci_writel(host, TDR, value);
  731. goto done;
  732. }
  733. offset = 4 - remaining;
  734. buf = sg_virt(sg);
  735. memcpy((u8 *)&value + remaining, buf, offset);
  736. mci_writel(host, TDR, value);
  737. nbytes += offset;
  738. }
  739. status = mci_readl(host, SR);
  740. if (status & ATMCI_DATA_ERROR_FLAGS) {
  741. mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
  742. | ATMCI_DATA_ERROR_FLAGS));
  743. host->data_status = status;
  744. atmci_set_pending(host, EVENT_DATA_ERROR);
  745. tasklet_schedule(&host->tasklet);
  746. break;
  747. }
  748. } while (status & MCI_TXRDY);
  749. host->pio_offset = offset;
  750. data->bytes_xfered += nbytes;
  751. return;
  752. done:
  753. mci_writel(host, IDR, MCI_TXRDY);
  754. mci_writel(host, IER, MCI_NOTBUSY);
  755. data->bytes_xfered += nbytes;
  756. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  757. if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
  758. && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
  759. send_stop_cmd(host->mmc, data);
  760. }
  761. static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
  762. {
  763. struct atmel_mci *host = mmc_priv(mmc);
  764. mci_writel(host, IDR, MCI_CMDRDY);
  765. if (atmci_is_completed(host, EVENT_STOP_SENT)) {
  766. host->stop_status = status;
  767. atmci_set_pending(host, EVENT_STOP_COMPLETE);
  768. } else {
  769. host->cmd_status = status;
  770. atmci_set_pending(host, EVENT_CMD_COMPLETE);
  771. }
  772. tasklet_schedule(&host->tasklet);
  773. }
  774. static irqreturn_t atmci_interrupt(int irq, void *dev_id)
  775. {
  776. struct mmc_host *mmc = dev_id;
  777. struct atmel_mci *host = mmc_priv(mmc);
  778. u32 status, mask, pending;
  779. unsigned int pass_count = 0;
  780. spin_lock(&mmc->lock);
  781. do {
  782. status = mci_readl(host, SR);
  783. mask = mci_readl(host, IMR);
  784. pending = status & mask;
  785. if (!pending)
  786. break;
  787. if (pending & ATMCI_DATA_ERROR_FLAGS) {
  788. mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
  789. | MCI_RXRDY | MCI_TXRDY);
  790. pending &= mci_readl(host, IMR);
  791. host->data_status = status;
  792. atmci_set_pending(host, EVENT_DATA_ERROR);
  793. tasklet_schedule(&host->tasklet);
  794. }
  795. if (pending & MCI_NOTBUSY) {
  796. mci_writel(host, IDR, (MCI_NOTBUSY
  797. | ATMCI_DATA_ERROR_FLAGS));
  798. atmci_set_pending(host, EVENT_DATA_COMPLETE);
  799. tasklet_schedule(&host->tasklet);
  800. }
  801. if (pending & MCI_RXRDY)
  802. atmci_read_data_pio(host);
  803. if (pending & MCI_TXRDY)
  804. atmci_write_data_pio(host);
  805. if (pending & MCI_CMDRDY)
  806. atmci_cmd_interrupt(mmc, status);
  807. } while (pass_count++ < 5);
  808. spin_unlock(&mmc->lock);
  809. return pass_count ? IRQ_HANDLED : IRQ_NONE;
  810. }
  811. static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
  812. {
  813. struct mmc_host *mmc = dev_id;
  814. struct atmel_mci *host = mmc_priv(mmc);
  815. /*
  816. * Disable interrupts until the pin has stabilized and check
  817. * the state then. Use mod_timer() since we may be in the
  818. * middle of the timer routine when this interrupt triggers.
  819. */
  820. disable_irq_nosync(irq);
  821. mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
  822. return IRQ_HANDLED;
  823. }
  824. static int __init atmci_probe(struct platform_device *pdev)
  825. {
  826. struct mci_platform_data *pdata;
  827. struct atmel_mci *host;
  828. struct mmc_host *mmc;
  829. struct resource *regs;
  830. int irq;
  831. int ret;
  832. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  833. if (!regs)
  834. return -ENXIO;
  835. pdata = pdev->dev.platform_data;
  836. if (!pdata)
  837. return -ENXIO;
  838. irq = platform_get_irq(pdev, 0);
  839. if (irq < 0)
  840. return irq;
  841. mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
  842. if (!mmc)
  843. return -ENOMEM;
  844. host = mmc_priv(mmc);
  845. host->pdev = pdev;
  846. host->mmc = mmc;
  847. host->detect_pin = pdata->detect_pin;
  848. host->wp_pin = pdata->wp_pin;
  849. host->mck = clk_get(&pdev->dev, "mci_clk");
  850. if (IS_ERR(host->mck)) {
  851. ret = PTR_ERR(host->mck);
  852. goto err_clk_get;
  853. }
  854. ret = -ENOMEM;
  855. host->regs = ioremap(regs->start, regs->end - regs->start + 1);
  856. if (!host->regs)
  857. goto err_ioremap;
  858. clk_enable(host->mck);
  859. mci_writel(host, CR, MCI_CR_SWRST);
  860. host->bus_hz = clk_get_rate(host->mck);
  861. clk_disable(host->mck);
  862. host->mapbase = regs->start;
  863. mmc->ops = &atmci_ops;
  864. mmc->f_min = (host->bus_hz + 511) / 512;
  865. mmc->f_max = host->bus_hz / 2;
  866. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  867. mmc->caps |= MMC_CAP_4_BIT_DATA;
  868. mmc->max_hw_segs = 64;
  869. mmc->max_phys_segs = 64;
  870. mmc->max_req_size = 32768 * 512;
  871. mmc->max_blk_size = 32768;
  872. mmc->max_blk_count = 512;
  873. tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
  874. ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
  875. if (ret)
  876. goto err_request_irq;
  877. /* Assume card is present if we don't have a detect pin */
  878. host->present = 1;
  879. if (gpio_is_valid(host->detect_pin)) {
  880. if (gpio_request(host->detect_pin, "mmc_detect")) {
  881. dev_dbg(&mmc->class_dev, "no detect pin available\n");
  882. host->detect_pin = -1;
  883. } else {
  884. host->present = !gpio_get_value(host->detect_pin);
  885. }
  886. }
  887. if (!gpio_is_valid(host->detect_pin))
  888. mmc->caps |= MMC_CAP_NEEDS_POLL;
  889. if (gpio_is_valid(host->wp_pin)) {
  890. if (gpio_request(host->wp_pin, "mmc_wp")) {
  891. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  892. host->wp_pin = -1;
  893. }
  894. }
  895. platform_set_drvdata(pdev, host);
  896. mmc_add_host(mmc);
  897. if (gpio_is_valid(host->detect_pin)) {
  898. setup_timer(&host->detect_timer, atmci_detect_change,
  899. (unsigned long)host);
  900. ret = request_irq(gpio_to_irq(host->detect_pin),
  901. atmci_detect_interrupt,
  902. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  903. "mmc-detect", mmc);
  904. if (ret) {
  905. dev_dbg(&mmc->class_dev,
  906. "could not request IRQ %d for detect pin\n",
  907. gpio_to_irq(host->detect_pin));
  908. gpio_free(host->detect_pin);
  909. host->detect_pin = -1;
  910. }
  911. }
  912. dev_info(&mmc->class_dev,
  913. "Atmel MCI controller at 0x%08lx irq %d\n",
  914. host->mapbase, irq);
  915. atmci_init_debugfs(host);
  916. return 0;
  917. err_request_irq:
  918. iounmap(host->regs);
  919. err_ioremap:
  920. clk_put(host->mck);
  921. err_clk_get:
  922. mmc_free_host(mmc);
  923. return ret;
  924. }
  925. static int __exit atmci_remove(struct platform_device *pdev)
  926. {
  927. struct atmel_mci *host = platform_get_drvdata(pdev);
  928. platform_set_drvdata(pdev, NULL);
  929. if (host) {
  930. /* Debugfs stuff is cleaned up by mmc core */
  931. if (gpio_is_valid(host->detect_pin)) {
  932. int pin = host->detect_pin;
  933. /* Make sure the timer doesn't enable the interrupt */
  934. host->detect_pin = -1;
  935. smp_wmb();
  936. free_irq(gpio_to_irq(pin), host->mmc);
  937. del_timer_sync(&host->detect_timer);
  938. gpio_free(pin);
  939. }
  940. mmc_remove_host(host->mmc);
  941. clk_enable(host->mck);
  942. mci_writel(host, IDR, ~0UL);
  943. mci_writel(host, CR, MCI_CR_MCIDIS);
  944. mci_readl(host, SR);
  945. clk_disable(host->mck);
  946. if (gpio_is_valid(host->wp_pin))
  947. gpio_free(host->wp_pin);
  948. free_irq(platform_get_irq(pdev, 0), host->mmc);
  949. iounmap(host->regs);
  950. clk_put(host->mck);
  951. mmc_free_host(host->mmc);
  952. }
  953. return 0;
  954. }
  955. static struct platform_driver atmci_driver = {
  956. .remove = __exit_p(atmci_remove),
  957. .driver = {
  958. .name = "atmel_mci",
  959. },
  960. };
  961. static int __init atmci_init(void)
  962. {
  963. return platform_driver_probe(&atmci_driver, atmci_probe);
  964. }
  965. static void __exit atmci_exit(void)
  966. {
  967. platform_driver_unregister(&atmci_driver);
  968. }
  969. module_init(atmci_init);
  970. module_exit(atmci_exit);
  971. MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
  972. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
  973. MODULE_LICENSE("GPL v2");