atmel-mci.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. 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->detect_pin))
  887. mmc->caps |= MMC_CAP_NEEDS_POLL;
  888. if (gpio_is_valid(host->wp_pin)) {
  889. if (gpio_request(host->wp_pin, "mmc_wp")) {
  890. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  891. host->wp_pin = -1;
  892. }
  893. }
  894. platform_set_drvdata(pdev, host);
  895. mmc_add_host(mmc);
  896. if (gpio_is_valid(host->detect_pin)) {
  897. setup_timer(&host->detect_timer, atmci_detect_change,
  898. (unsigned long)host);
  899. ret = request_irq(gpio_to_irq(host->detect_pin),
  900. atmci_detect_interrupt,
  901. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  902. "mmc-detect", mmc);
  903. if (ret) {
  904. dev_dbg(&mmc->class_dev,
  905. "could not request IRQ %d for detect pin\n",
  906. gpio_to_irq(host->detect_pin));
  907. gpio_free(host->detect_pin);
  908. host->detect_pin = -1;
  909. }
  910. }
  911. dev_info(&mmc->class_dev,
  912. "Atmel MCI controller at 0x%08lx irq %d\n",
  913. host->mapbase, irq);
  914. atmci_init_debugfs(host);
  915. return 0;
  916. err_request_irq:
  917. iounmap(host->regs);
  918. err_ioremap:
  919. clk_put(host->mck);
  920. err_clk_get:
  921. mmc_free_host(mmc);
  922. return ret;
  923. }
  924. static int __exit atmci_remove(struct platform_device *pdev)
  925. {
  926. struct atmel_mci *host = platform_get_drvdata(pdev);
  927. platform_set_drvdata(pdev, NULL);
  928. if (host) {
  929. /* Debugfs stuff is cleaned up by mmc core */
  930. if (gpio_is_valid(host->detect_pin)) {
  931. int pin = host->detect_pin;
  932. /* Make sure the timer doesn't enable the interrupt */
  933. host->detect_pin = -1;
  934. smp_wmb();
  935. free_irq(gpio_to_irq(pin), host->mmc);
  936. del_timer_sync(&host->detect_timer);
  937. gpio_free(pin);
  938. }
  939. mmc_remove_host(host->mmc);
  940. clk_enable(host->mck);
  941. mci_writel(host, IDR, ~0UL);
  942. mci_writel(host, CR, MCI_CR_MCIDIS);
  943. mci_readl(host, SR);
  944. clk_disable(host->mck);
  945. if (gpio_is_valid(host->wp_pin))
  946. gpio_free(host->wp_pin);
  947. free_irq(platform_get_irq(pdev, 0), host->mmc);
  948. iounmap(host->regs);
  949. clk_put(host->mck);
  950. mmc_free_host(host->mmc);
  951. }
  952. return 0;
  953. }
  954. static struct platform_driver atmci_driver = {
  955. .remove = __exit_p(atmci_remove),
  956. .driver = {
  957. .name = "atmel_mci",
  958. },
  959. };
  960. static int __init atmci_init(void)
  961. {
  962. return platform_driver_probe(&atmci_driver, atmci_probe);
  963. }
  964. static void __exit atmci_exit(void)
  965. {
  966. platform_driver_unregister(&atmci_driver);
  967. }
  968. module_init(atmci_init);
  969. module_exit(atmci_exit);
  970. MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
  971. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
  972. MODULE_LICENSE("GPL v2");