atmel-mci.c 27 KB

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