atmel-mci.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioport.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/stat.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/atmel-mci.h>
  28. #include <asm/io.h>
  29. #include <asm/unaligned.h>
  30. #include <mach/board.h>
  31. #include "atmel-mci-regs.h"
  32. #define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
  33. #define ATMCI_DMA_THRESHOLD 16
  34. enum {
  35. EVENT_CMD_COMPLETE = 0,
  36. EVENT_XFER_COMPLETE,
  37. EVENT_DATA_COMPLETE,
  38. EVENT_DATA_ERROR,
  39. };
  40. enum atmel_mci_state {
  41. STATE_IDLE = 0,
  42. STATE_SENDING_CMD,
  43. STATE_SENDING_DATA,
  44. STATE_DATA_BUSY,
  45. STATE_SENDING_STOP,
  46. STATE_DATA_ERROR,
  47. };
  48. struct atmel_mci_dma {
  49. #ifdef CONFIG_MMC_ATMELMCI_DMA
  50. struct dma_chan *chan;
  51. struct dma_async_tx_descriptor *data_desc;
  52. #endif
  53. };
  54. /**
  55. * struct atmel_mci - MMC controller state shared between all slots
  56. * @lock: Spinlock protecting the queue and associated data.
  57. * @regs: Pointer to MMIO registers.
  58. * @sg: Scatterlist entry currently being processed by PIO code, if any.
  59. * @pio_offset: Offset into the current scatterlist entry.
  60. * @cur_slot: The slot which is currently using the controller.
  61. * @mrq: The request currently being processed on @cur_slot,
  62. * or NULL if the controller is idle.
  63. * @cmd: The command currently being sent to the card, or NULL.
  64. * @data: The data currently being transferred, or NULL if no data
  65. * transfer is in progress.
  66. * @dma: DMA client state.
  67. * @data_chan: DMA channel being used for the current data transfer.
  68. * @cmd_status: Snapshot of SR taken upon completion of the current
  69. * command. Only valid when EVENT_CMD_COMPLETE is pending.
  70. * @data_status: Snapshot of SR taken upon completion of the current
  71. * data transfer. Only valid when EVENT_DATA_COMPLETE or
  72. * EVENT_DATA_ERROR is pending.
  73. * @stop_cmdr: Value to be loaded into CMDR when the stop command is
  74. * to be sent.
  75. * @tasklet: Tasklet running the request state machine.
  76. * @pending_events: Bitmask of events flagged by the interrupt handler
  77. * to be processed by the tasklet.
  78. * @completed_events: Bitmask of events which the state machine has
  79. * processed.
  80. * @state: Tasklet state.
  81. * @queue: List of slots waiting for access to the controller.
  82. * @need_clock_update: Update the clock rate before the next request.
  83. * @need_reset: Reset controller before next request.
  84. * @mode_reg: Value of the MR register.
  85. * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
  86. * rate and timeout calculations.
  87. * @mapbase: Physical address of the MMIO registers.
  88. * @mck: The peripheral bus clock hooked up to the MMC controller.
  89. * @pdev: Platform device associated with the MMC controller.
  90. * @slot: Slots sharing this MMC controller.
  91. *
  92. * Locking
  93. * =======
  94. *
  95. * @lock is a softirq-safe spinlock protecting @queue as well as
  96. * @cur_slot, @mrq and @state. These must always be updated
  97. * at the same time while holding @lock.
  98. *
  99. * @lock also protects mode_reg and need_clock_update since these are
  100. * used to synchronize mode register updates with the queue
  101. * processing.
  102. *
  103. * The @mrq field of struct atmel_mci_slot is also protected by @lock,
  104. * and must always be written at the same time as the slot is added to
  105. * @queue.
  106. *
  107. * @pending_events and @completed_events are accessed using atomic bit
  108. * operations, so they don't need any locking.
  109. *
  110. * None of the fields touched by the interrupt handler need any
  111. * locking. However, ordering is important: Before EVENT_DATA_ERROR or
  112. * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
  113. * interrupts must be disabled and @data_status updated with a
  114. * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
  115. * CMDRDY interupt must be disabled and @cmd_status updated with a
  116. * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
  117. * bytes_xfered field of @data must be written. This is ensured by
  118. * using barriers.
  119. */
  120. struct atmel_mci {
  121. spinlock_t lock;
  122. void __iomem *regs;
  123. struct scatterlist *sg;
  124. unsigned int pio_offset;
  125. struct atmel_mci_slot *cur_slot;
  126. struct mmc_request *mrq;
  127. struct mmc_command *cmd;
  128. struct mmc_data *data;
  129. struct atmel_mci_dma dma;
  130. struct dma_chan *data_chan;
  131. u32 cmd_status;
  132. u32 data_status;
  133. u32 stop_cmdr;
  134. struct tasklet_struct tasklet;
  135. unsigned long pending_events;
  136. unsigned long completed_events;
  137. enum atmel_mci_state state;
  138. struct list_head queue;
  139. bool need_clock_update;
  140. bool need_reset;
  141. u32 mode_reg;
  142. unsigned long bus_hz;
  143. unsigned long mapbase;
  144. struct clk *mck;
  145. struct platform_device *pdev;
  146. struct atmel_mci_slot *slot[ATMEL_MCI_MAX_NR_SLOTS];
  147. };
  148. /**
  149. * struct atmel_mci_slot - MMC slot state
  150. * @mmc: The mmc_host representing this slot.
  151. * @host: The MMC controller this slot is using.
  152. * @sdc_reg: Value of SDCR to be written before using this slot.
  153. * @mrq: mmc_request currently being processed or waiting to be
  154. * processed, or NULL when the slot is idle.
  155. * @queue_node: List node for placing this node in the @queue list of
  156. * &struct atmel_mci.
  157. * @clock: Clock rate configured by set_ios(). Protected by host->lock.
  158. * @flags: Random state bits associated with the slot.
  159. * @detect_pin: GPIO pin used for card detection, or negative if not
  160. * available.
  161. * @wp_pin: GPIO pin used for card write protect sending, or negative
  162. * if not available.
  163. * @detect_timer: Timer used for debouncing @detect_pin interrupts.
  164. */
  165. struct atmel_mci_slot {
  166. struct mmc_host *mmc;
  167. struct atmel_mci *host;
  168. u32 sdc_reg;
  169. struct mmc_request *mrq;
  170. struct list_head queue_node;
  171. unsigned int clock;
  172. unsigned long flags;
  173. #define ATMCI_CARD_PRESENT 0
  174. #define ATMCI_CARD_NEED_INIT 1
  175. #define ATMCI_SHUTDOWN 2
  176. int detect_pin;
  177. int wp_pin;
  178. struct timer_list detect_timer;
  179. };
  180. #define atmci_test_and_clear_pending(host, event) \
  181. test_and_clear_bit(event, &host->pending_events)
  182. #define atmci_set_completed(host, event) \
  183. set_bit(event, &host->completed_events)
  184. #define atmci_set_pending(host, event) \
  185. set_bit(event, &host->pending_events)
  186. /*
  187. * The debugfs stuff below is mostly optimized away when
  188. * CONFIG_DEBUG_FS is not set.
  189. */
  190. static int atmci_req_show(struct seq_file *s, void *v)
  191. {
  192. struct atmel_mci_slot *slot = s->private;
  193. struct mmc_request *mrq;
  194. struct mmc_command *cmd;
  195. struct mmc_command *stop;
  196. struct mmc_data *data;
  197. /* Make sure we get a consistent snapshot */
  198. spin_lock_bh(&slot->host->lock);
  199. mrq = slot->mrq;
  200. if (mrq) {
  201. cmd = mrq->cmd;
  202. data = mrq->data;
  203. stop = mrq->stop;
  204. if (cmd)
  205. seq_printf(s,
  206. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  207. cmd->opcode, cmd->arg, cmd->flags,
  208. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  209. cmd->resp[2], cmd->error);
  210. if (data)
  211. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  212. data->bytes_xfered, data->blocks,
  213. data->blksz, data->flags, data->error);
  214. if (stop)
  215. seq_printf(s,
  216. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  217. stop->opcode, stop->arg, stop->flags,
  218. stop->resp[0], stop->resp[1], stop->resp[2],
  219. stop->resp[2], stop->error);
  220. }
  221. spin_unlock_bh(&slot->host->lock);
  222. return 0;
  223. }
  224. static int atmci_req_open(struct inode *inode, struct file *file)
  225. {
  226. return single_open(file, atmci_req_show, inode->i_private);
  227. }
  228. static const struct file_operations atmci_req_fops = {
  229. .owner = THIS_MODULE,
  230. .open = atmci_req_open,
  231. .read = seq_read,
  232. .llseek = seq_lseek,
  233. .release = single_release,
  234. };
  235. static void atmci_show_status_reg(struct seq_file *s,
  236. const char *regname, u32 value)
  237. {
  238. static const char *sr_bit[] = {
  239. [0] = "CMDRDY",
  240. [1] = "RXRDY",
  241. [2] = "TXRDY",
  242. [3] = "BLKE",
  243. [4] = "DTIP",
  244. [5] = "NOTBUSY",
  245. [8] = "SDIOIRQA",
  246. [9] = "SDIOIRQB",
  247. [16] = "RINDE",
  248. [17] = "RDIRE",
  249. [18] = "RCRCE",
  250. [19] = "RENDE",
  251. [20] = "RTOE",
  252. [21] = "DCRCE",
  253. [22] = "DTOE",
  254. [30] = "OVRE",
  255. [31] = "UNRE",
  256. };
  257. unsigned int i;
  258. seq_printf(s, "%s:\t0x%08x", regname, value);
  259. for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
  260. if (value & (1 << i)) {
  261. if (sr_bit[i])
  262. seq_printf(s, " %s", sr_bit[i]);
  263. else
  264. seq_puts(s, " UNKNOWN");
  265. }
  266. }
  267. seq_putc(s, '\n');
  268. }
  269. static int atmci_regs_show(struct seq_file *s, void *v)
  270. {
  271. struct atmel_mci *host = s->private;
  272. u32 *buf;
  273. buf = kmalloc(MCI_REGS_SIZE, GFP_KERNEL);
  274. if (!buf)
  275. return -ENOMEM;
  276. /*
  277. * Grab a more or less consistent snapshot. Note that we're
  278. * not disabling interrupts, so IMR and SR may not be
  279. * consistent.
  280. */
  281. spin_lock_bh(&host->lock);
  282. clk_enable(host->mck);
  283. memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
  284. clk_disable(host->mck);
  285. spin_unlock_bh(&host->lock);
  286. seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
  287. buf[MCI_MR / 4],
  288. buf[MCI_MR / 4] & MCI_MR_RDPROOF ? " RDPROOF" : "",
  289. buf[MCI_MR / 4] & MCI_MR_WRPROOF ? " WRPROOF" : "",
  290. buf[MCI_MR / 4] & 0xff);
  291. seq_printf(s, "DTOR:\t0x%08x\n", buf[MCI_DTOR / 4]);
  292. seq_printf(s, "SDCR:\t0x%08x\n", buf[MCI_SDCR / 4]);
  293. seq_printf(s, "ARGR:\t0x%08x\n", buf[MCI_ARGR / 4]);
  294. seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
  295. buf[MCI_BLKR / 4],
  296. buf[MCI_BLKR / 4] & 0xffff,
  297. (buf[MCI_BLKR / 4] >> 16) & 0xffff);
  298. /* Don't read RSPR and RDR; it will consume the data there */
  299. atmci_show_status_reg(s, "SR", buf[MCI_SR / 4]);
  300. atmci_show_status_reg(s, "IMR", buf[MCI_IMR / 4]);
  301. kfree(buf);
  302. return 0;
  303. }
  304. static int atmci_regs_open(struct inode *inode, struct file *file)
  305. {
  306. return single_open(file, atmci_regs_show, inode->i_private);
  307. }
  308. static const struct file_operations atmci_regs_fops = {
  309. .owner = THIS_MODULE,
  310. .open = atmci_regs_open,
  311. .read = seq_read,
  312. .llseek = seq_lseek,
  313. .release = single_release,
  314. };
  315. static void atmci_init_debugfs(struct atmel_mci_slot *slot)
  316. {
  317. struct mmc_host *mmc = slot->mmc;
  318. struct atmel_mci *host = slot->host;
  319. struct dentry *root;
  320. struct dentry *node;
  321. root = mmc->debugfs_root;
  322. if (!root)
  323. return;
  324. node = debugfs_create_file("regs", S_IRUSR, root, host,
  325. &atmci_regs_fops);
  326. if (IS_ERR(node))
  327. return;
  328. if (!node)
  329. goto err;
  330. node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
  331. if (!node)
  332. goto err;
  333. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  334. if (!node)
  335. goto err;
  336. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  337. (u32 *)&host->pending_events);
  338. if (!node)
  339. goto err;
  340. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  341. (u32 *)&host->completed_events);
  342. if (!node)
  343. goto err;
  344. return;
  345. err:
  346. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  347. }
  348. static inline unsigned int ns_to_clocks(struct atmel_mci *host,
  349. unsigned int ns)
  350. {
  351. return (ns * (host->bus_hz / 1000000) + 999) / 1000;
  352. }
  353. static void atmci_set_timeout(struct atmel_mci *host,
  354. struct atmel_mci_slot *slot, struct mmc_data *data)
  355. {
  356. static unsigned dtomul_to_shift[] = {
  357. 0, 4, 7, 8, 10, 12, 16, 20
  358. };
  359. unsigned timeout;
  360. unsigned dtocyc;
  361. unsigned dtomul;
  362. timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
  363. for (dtomul = 0; dtomul < 8; dtomul++) {
  364. unsigned shift = dtomul_to_shift[dtomul];
  365. dtocyc = (timeout + (1 << shift) - 1) >> shift;
  366. if (dtocyc < 15)
  367. break;
  368. }
  369. if (dtomul >= 8) {
  370. dtomul = 7;
  371. dtocyc = 15;
  372. }
  373. dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
  374. dtocyc << dtomul_to_shift[dtomul]);
  375. mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
  376. }
  377. /*
  378. * Return mask with command flags to be enabled for this command.
  379. */
  380. static u32 atmci_prepare_command(struct mmc_host *mmc,
  381. struct mmc_command *cmd)
  382. {
  383. struct mmc_data *data;
  384. u32 cmdr;
  385. cmd->error = -EINPROGRESS;
  386. cmdr = MCI_CMDR_CMDNB(cmd->opcode);
  387. if (cmd->flags & MMC_RSP_PRESENT) {
  388. if (cmd->flags & MMC_RSP_136)
  389. cmdr |= MCI_CMDR_RSPTYP_136BIT;
  390. else
  391. cmdr |= MCI_CMDR_RSPTYP_48BIT;
  392. }
  393. /*
  394. * This should really be MAXLAT_5 for CMD2 and ACMD41, but
  395. * it's too difficult to determine whether this is an ACMD or
  396. * not. Better make it 64.
  397. */
  398. cmdr |= MCI_CMDR_MAXLAT_64CYC;
  399. if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
  400. cmdr |= MCI_CMDR_OPDCMD;
  401. data = cmd->data;
  402. if (data) {
  403. cmdr |= MCI_CMDR_START_XFER;
  404. if (data->flags & MMC_DATA_STREAM)
  405. cmdr |= MCI_CMDR_STREAM;
  406. else if (data->blocks > 1)
  407. cmdr |= MCI_CMDR_MULTI_BLOCK;
  408. else
  409. cmdr |= MCI_CMDR_BLOCK;
  410. if (data->flags & MMC_DATA_READ)
  411. cmdr |= MCI_CMDR_TRDIR_READ;
  412. }
  413. return cmdr;
  414. }
  415. static void atmci_start_command(struct atmel_mci *host,
  416. struct mmc_command *cmd, u32 cmd_flags)
  417. {
  418. WARN_ON(host->cmd);
  419. host->cmd = cmd;
  420. dev_vdbg(&host->pdev->dev,
  421. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  422. cmd->arg, cmd_flags);
  423. mci_writel(host, ARGR, cmd->arg);
  424. mci_writel(host, CMDR, cmd_flags);
  425. }
  426. static void send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
  427. {
  428. atmci_start_command(host, data->stop, host->stop_cmdr);
  429. mci_writel(host, IER, MCI_CMDRDY);
  430. }
  431. #ifdef CONFIG_MMC_ATMELMCI_DMA
  432. static void atmci_dma_cleanup(struct atmel_mci *host)
  433. {
  434. struct mmc_data *data = host->data;
  435. dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
  436. ((data->flags & MMC_DATA_WRITE)
  437. ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
  438. }
  439. static void atmci_stop_dma(struct atmel_mci *host)
  440. {
  441. struct dma_chan *chan = host->data_chan;
  442. if (chan) {
  443. chan->device->device_terminate_all(chan);
  444. atmci_dma_cleanup(host);
  445. } else {
  446. /* Data transfer was stopped by the interrupt handler */
  447. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  448. mci_writel(host, IER, MCI_NOTBUSY);
  449. }
  450. }
  451. /* This function is called by the DMA driver from tasklet context. */
  452. static void atmci_dma_complete(void *arg)
  453. {
  454. struct atmel_mci *host = arg;
  455. struct mmc_data *data = host->data;
  456. dev_vdbg(&host->pdev->dev, "DMA complete\n");
  457. atmci_dma_cleanup(host);
  458. /*
  459. * If the card was removed, data will be NULL. No point trying
  460. * to send the stop command or waiting for NBUSY in this case.
  461. */
  462. if (data) {
  463. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  464. tasklet_schedule(&host->tasklet);
  465. /*
  466. * Regardless of what the documentation says, we have
  467. * to wait for NOTBUSY even after block read
  468. * operations.
  469. *
  470. * When the DMA transfer is complete, the controller
  471. * may still be reading the CRC from the card, i.e.
  472. * the data transfer is still in progress and we
  473. * haven't seen all the potential error bits yet.
  474. *
  475. * The interrupt handler will schedule a different
  476. * tasklet to finish things up when the data transfer
  477. * is completely done.
  478. *
  479. * We may not complete the mmc request here anyway
  480. * because the mmc layer may call back and cause us to
  481. * violate the "don't submit new operations from the
  482. * completion callback" rule of the dma engine
  483. * framework.
  484. */
  485. mci_writel(host, IER, MCI_NOTBUSY);
  486. }
  487. }
  488. static int
  489. atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
  490. {
  491. struct dma_chan *chan;
  492. struct dma_async_tx_descriptor *desc;
  493. struct scatterlist *sg;
  494. unsigned int i;
  495. enum dma_data_direction direction;
  496. /*
  497. * We don't do DMA on "complex" transfers, i.e. with
  498. * non-word-aligned buffers or lengths. Also, we don't bother
  499. * with all the DMA setup overhead for short transfers.
  500. */
  501. if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
  502. return -EINVAL;
  503. if (data->blksz & 3)
  504. return -EINVAL;
  505. for_each_sg(data->sg, sg, data->sg_len, i) {
  506. if (sg->offset & 3 || sg->length & 3)
  507. return -EINVAL;
  508. }
  509. /* If we don't have a channel, we can't do DMA */
  510. chan = host->dma.chan;
  511. if (chan)
  512. host->data_chan = chan;
  513. if (!chan)
  514. return -ENODEV;
  515. if (data->flags & MMC_DATA_READ)
  516. direction = DMA_FROM_DEVICE;
  517. else
  518. direction = DMA_TO_DEVICE;
  519. desc = chan->device->device_prep_slave_sg(chan,
  520. data->sg, data->sg_len, direction,
  521. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  522. if (!desc)
  523. return -ENOMEM;
  524. host->dma.data_desc = desc;
  525. desc->callback = atmci_dma_complete;
  526. desc->callback_param = host;
  527. desc->tx_submit(desc);
  528. /* Go! */
  529. chan->device->device_issue_pending(chan);
  530. return 0;
  531. }
  532. #else /* CONFIG_MMC_ATMELMCI_DMA */
  533. static int atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
  534. {
  535. return -ENOSYS;
  536. }
  537. static void atmci_stop_dma(struct atmel_mci *host)
  538. {
  539. /* Data transfer was stopped by the interrupt handler */
  540. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  541. mci_writel(host, IER, MCI_NOTBUSY);
  542. }
  543. #endif /* CONFIG_MMC_ATMELMCI_DMA */
  544. /*
  545. * Returns a mask of interrupt flags to be enabled after the whole
  546. * request has been prepared.
  547. */
  548. static u32 atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
  549. {
  550. u32 iflags;
  551. data->error = -EINPROGRESS;
  552. WARN_ON(host->data);
  553. host->sg = NULL;
  554. host->data = data;
  555. iflags = ATMCI_DATA_ERROR_FLAGS;
  556. if (atmci_submit_data_dma(host, data)) {
  557. host->data_chan = NULL;
  558. /*
  559. * Errata: MMC data write operation with less than 12
  560. * bytes is impossible.
  561. *
  562. * Errata: MCI Transmit Data Register (TDR) FIFO
  563. * corruption when length is not multiple of 4.
  564. */
  565. if (data->blocks * data->blksz < 12
  566. || (data->blocks * data->blksz) & 3)
  567. host->need_reset = true;
  568. host->sg = data->sg;
  569. host->pio_offset = 0;
  570. if (data->flags & MMC_DATA_READ)
  571. iflags |= MCI_RXRDY;
  572. else
  573. iflags |= MCI_TXRDY;
  574. }
  575. return iflags;
  576. }
  577. static void atmci_start_request(struct atmel_mci *host,
  578. struct atmel_mci_slot *slot)
  579. {
  580. struct mmc_request *mrq;
  581. struct mmc_command *cmd;
  582. struct mmc_data *data;
  583. u32 iflags;
  584. u32 cmdflags;
  585. mrq = slot->mrq;
  586. host->cur_slot = slot;
  587. host->mrq = mrq;
  588. host->pending_events = 0;
  589. host->completed_events = 0;
  590. host->data_status = 0;
  591. if (host->need_reset) {
  592. mci_writel(host, CR, MCI_CR_SWRST);
  593. mci_writel(host, CR, MCI_CR_MCIEN);
  594. mci_writel(host, MR, host->mode_reg);
  595. host->need_reset = false;
  596. }
  597. mci_writel(host, SDCR, slot->sdc_reg);
  598. iflags = mci_readl(host, IMR);
  599. if (iflags)
  600. dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
  601. iflags);
  602. if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
  603. /* Send init sequence (74 clock cycles) */
  604. mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
  605. while (!(mci_readl(host, SR) & MCI_CMDRDY))
  606. cpu_relax();
  607. }
  608. data = mrq->data;
  609. if (data) {
  610. atmci_set_timeout(host, slot, data);
  611. /* Must set block count/size before sending command */
  612. mci_writel(host, BLKR, MCI_BCNT(data->blocks)
  613. | MCI_BLKLEN(data->blksz));
  614. dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
  615. MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
  616. }
  617. iflags = MCI_CMDRDY;
  618. cmd = mrq->cmd;
  619. cmdflags = atmci_prepare_command(slot->mmc, cmd);
  620. atmci_start_command(host, cmd, cmdflags);
  621. if (data)
  622. iflags |= atmci_submit_data(host, data);
  623. if (mrq->stop) {
  624. host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
  625. host->stop_cmdr |= MCI_CMDR_STOP_XFER;
  626. if (!(data->flags & MMC_DATA_WRITE))
  627. host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
  628. if (data->flags & MMC_DATA_STREAM)
  629. host->stop_cmdr |= MCI_CMDR_STREAM;
  630. else
  631. host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
  632. }
  633. /*
  634. * We could have enabled interrupts earlier, but I suspect
  635. * that would open up a nice can of interesting race
  636. * conditions (e.g. command and data complete, but stop not
  637. * prepared yet.)
  638. */
  639. mci_writel(host, IER, iflags);
  640. }
  641. static void atmci_queue_request(struct atmel_mci *host,
  642. struct atmel_mci_slot *slot, struct mmc_request *mrq)
  643. {
  644. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  645. host->state);
  646. spin_lock_bh(&host->lock);
  647. slot->mrq = mrq;
  648. if (host->state == STATE_IDLE) {
  649. host->state = STATE_SENDING_CMD;
  650. atmci_start_request(host, slot);
  651. } else {
  652. list_add_tail(&slot->queue_node, &host->queue);
  653. }
  654. spin_unlock_bh(&host->lock);
  655. }
  656. static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  657. {
  658. struct atmel_mci_slot *slot = mmc_priv(mmc);
  659. struct atmel_mci *host = slot->host;
  660. struct mmc_data *data;
  661. WARN_ON(slot->mrq);
  662. /*
  663. * We may "know" the card is gone even though there's still an
  664. * electrical connection. If so, we really need to communicate
  665. * this to the MMC core since there won't be any more
  666. * interrupts as the card is completely removed. Otherwise,
  667. * the MMC core might believe the card is still there even
  668. * though the card was just removed very slowly.
  669. */
  670. if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
  671. mrq->cmd->error = -ENOMEDIUM;
  672. mmc_request_done(mmc, mrq);
  673. return;
  674. }
  675. /* We don't support multiple blocks of weird lengths. */
  676. data = mrq->data;
  677. if (data && data->blocks > 1 && data->blksz & 3) {
  678. mrq->cmd->error = -EINVAL;
  679. mmc_request_done(mmc, mrq);
  680. }
  681. atmci_queue_request(host, slot, mrq);
  682. }
  683. static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  684. {
  685. struct atmel_mci_slot *slot = mmc_priv(mmc);
  686. struct atmel_mci *host = slot->host;
  687. unsigned int i;
  688. slot->sdc_reg &= ~MCI_SDCBUS_MASK;
  689. switch (ios->bus_width) {
  690. case MMC_BUS_WIDTH_1:
  691. slot->sdc_reg |= MCI_SDCBUS_1BIT;
  692. break;
  693. case MMC_BUS_WIDTH_4:
  694. slot->sdc_reg |= MCI_SDCBUS_4BIT;
  695. break;
  696. }
  697. if (ios->clock) {
  698. unsigned int clock_min = ~0U;
  699. u32 clkdiv;
  700. spin_lock_bh(&host->lock);
  701. if (!host->mode_reg) {
  702. clk_enable(host->mck);
  703. mci_writel(host, CR, MCI_CR_SWRST);
  704. mci_writel(host, CR, MCI_CR_MCIEN);
  705. }
  706. /*
  707. * Use mirror of ios->clock to prevent race with mmc
  708. * core ios update when finding the minimum.
  709. */
  710. slot->clock = ios->clock;
  711. for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
  712. if (host->slot[i] && host->slot[i]->clock
  713. && host->slot[i]->clock < clock_min)
  714. clock_min = host->slot[i]->clock;
  715. }
  716. /* Calculate clock divider */
  717. clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
  718. if (clkdiv > 255) {
  719. dev_warn(&mmc->class_dev,
  720. "clock %u too slow; using %lu\n",
  721. clock_min, host->bus_hz / (2 * 256));
  722. clkdiv = 255;
  723. }
  724. /*
  725. * WRPROOF and RDPROOF prevent overruns/underruns by
  726. * stopping the clock when the FIFO is full/empty.
  727. * This state is not expected to last for long.
  728. */
  729. host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
  730. | MCI_MR_RDPROOF;
  731. if (list_empty(&host->queue))
  732. mci_writel(host, MR, host->mode_reg);
  733. else
  734. host->need_clock_update = true;
  735. spin_unlock_bh(&host->lock);
  736. } else {
  737. bool any_slot_active = false;
  738. spin_lock_bh(&host->lock);
  739. slot->clock = 0;
  740. for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
  741. if (host->slot[i] && host->slot[i]->clock) {
  742. any_slot_active = true;
  743. break;
  744. }
  745. }
  746. if (!any_slot_active) {
  747. mci_writel(host, CR, MCI_CR_MCIDIS);
  748. if (host->mode_reg) {
  749. mci_readl(host, MR);
  750. clk_disable(host->mck);
  751. }
  752. host->mode_reg = 0;
  753. }
  754. spin_unlock_bh(&host->lock);
  755. }
  756. switch (ios->power_mode) {
  757. case MMC_POWER_UP:
  758. set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
  759. break;
  760. default:
  761. /*
  762. * TODO: None of the currently available AVR32-based
  763. * boards allow MMC power to be turned off. Implement
  764. * power control when this can be tested properly.
  765. *
  766. * We also need to hook this into the clock management
  767. * somehow so that newly inserted cards aren't
  768. * subjected to a fast clock before we have a chance
  769. * to figure out what the maximum rate is. Currently,
  770. * there's no way to avoid this, and there never will
  771. * be for boards that don't support power control.
  772. */
  773. break;
  774. }
  775. }
  776. static int atmci_get_ro(struct mmc_host *mmc)
  777. {
  778. int read_only = -ENOSYS;
  779. struct atmel_mci_slot *slot = mmc_priv(mmc);
  780. if (gpio_is_valid(slot->wp_pin)) {
  781. read_only = gpio_get_value(slot->wp_pin);
  782. dev_dbg(&mmc->class_dev, "card is %s\n",
  783. read_only ? "read-only" : "read-write");
  784. }
  785. return read_only;
  786. }
  787. static int atmci_get_cd(struct mmc_host *mmc)
  788. {
  789. int present = -ENOSYS;
  790. struct atmel_mci_slot *slot = mmc_priv(mmc);
  791. if (gpio_is_valid(slot->detect_pin)) {
  792. present = !gpio_get_value(slot->detect_pin);
  793. dev_dbg(&mmc->class_dev, "card is %spresent\n",
  794. present ? "" : "not ");
  795. }
  796. return present;
  797. }
  798. static const struct mmc_host_ops atmci_ops = {
  799. .request = atmci_request,
  800. .set_ios = atmci_set_ios,
  801. .get_ro = atmci_get_ro,
  802. .get_cd = atmci_get_cd,
  803. };
  804. /* Called with host->lock held */
  805. static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
  806. __releases(&host->lock)
  807. __acquires(&host->lock)
  808. {
  809. struct atmel_mci_slot *slot = NULL;
  810. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  811. WARN_ON(host->cmd || host->data);
  812. /*
  813. * Update the MMC clock rate if necessary. This may be
  814. * necessary if set_ios() is called when a different slot is
  815. * busy transfering data.
  816. */
  817. if (host->need_clock_update)
  818. mci_writel(host, MR, host->mode_reg);
  819. host->cur_slot->mrq = NULL;
  820. host->mrq = NULL;
  821. if (!list_empty(&host->queue)) {
  822. slot = list_entry(host->queue.next,
  823. struct atmel_mci_slot, queue_node);
  824. list_del(&slot->queue_node);
  825. dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
  826. mmc_hostname(slot->mmc));
  827. host->state = STATE_SENDING_CMD;
  828. atmci_start_request(host, slot);
  829. } else {
  830. dev_vdbg(&host->pdev->dev, "list empty\n");
  831. host->state = STATE_IDLE;
  832. }
  833. spin_unlock(&host->lock);
  834. mmc_request_done(prev_mmc, mrq);
  835. spin_lock(&host->lock);
  836. }
  837. static void atmci_command_complete(struct atmel_mci *host,
  838. struct mmc_command *cmd)
  839. {
  840. u32 status = host->cmd_status;
  841. /* Read the response from the card (up to 16 bytes) */
  842. cmd->resp[0] = mci_readl(host, RSPR);
  843. cmd->resp[1] = mci_readl(host, RSPR);
  844. cmd->resp[2] = mci_readl(host, RSPR);
  845. cmd->resp[3] = mci_readl(host, RSPR);
  846. if (status & MCI_RTOE)
  847. cmd->error = -ETIMEDOUT;
  848. else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
  849. cmd->error = -EILSEQ;
  850. else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
  851. cmd->error = -EIO;
  852. else
  853. cmd->error = 0;
  854. if (cmd->error) {
  855. dev_dbg(&host->pdev->dev,
  856. "command error: status=0x%08x\n", status);
  857. if (cmd->data) {
  858. host->data = NULL;
  859. atmci_stop_dma(host);
  860. mci_writel(host, IDR, MCI_NOTBUSY
  861. | MCI_TXRDY | MCI_RXRDY
  862. | ATMCI_DATA_ERROR_FLAGS);
  863. }
  864. }
  865. }
  866. static void atmci_detect_change(unsigned long data)
  867. {
  868. struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
  869. bool present;
  870. bool present_old;
  871. /*
  872. * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
  873. * freeing the interrupt. We must not re-enable the interrupt
  874. * if it has been freed, and if we're shutting down, it
  875. * doesn't really matter whether the card is present or not.
  876. */
  877. smp_rmb();
  878. if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
  879. return;
  880. enable_irq(gpio_to_irq(slot->detect_pin));
  881. present = !gpio_get_value(slot->detect_pin);
  882. present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
  883. dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
  884. present, present_old);
  885. if (present != present_old) {
  886. struct atmel_mci *host = slot->host;
  887. struct mmc_request *mrq;
  888. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  889. present ? "inserted" : "removed");
  890. spin_lock(&host->lock);
  891. if (!present)
  892. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  893. else
  894. set_bit(ATMCI_CARD_PRESENT, &slot->flags);
  895. /* Clean up queue if present */
  896. mrq = slot->mrq;
  897. if (mrq) {
  898. if (mrq == host->mrq) {
  899. /*
  900. * Reset controller to terminate any ongoing
  901. * commands or data transfers.
  902. */
  903. mci_writel(host, CR, MCI_CR_SWRST);
  904. mci_writel(host, CR, MCI_CR_MCIEN);
  905. mci_writel(host, MR, host->mode_reg);
  906. host->data = NULL;
  907. host->cmd = NULL;
  908. switch (host->state) {
  909. case STATE_IDLE:
  910. break;
  911. case STATE_SENDING_CMD:
  912. mrq->cmd->error = -ENOMEDIUM;
  913. if (!mrq->data)
  914. break;
  915. /* fall through */
  916. case STATE_SENDING_DATA:
  917. mrq->data->error = -ENOMEDIUM;
  918. atmci_stop_dma(host);
  919. break;
  920. case STATE_DATA_BUSY:
  921. case STATE_DATA_ERROR:
  922. if (mrq->data->error == -EINPROGRESS)
  923. mrq->data->error = -ENOMEDIUM;
  924. if (!mrq->stop)
  925. break;
  926. /* fall through */
  927. case STATE_SENDING_STOP:
  928. mrq->stop->error = -ENOMEDIUM;
  929. break;
  930. }
  931. atmci_request_end(host, mrq);
  932. } else {
  933. list_del(&slot->queue_node);
  934. mrq->cmd->error = -ENOMEDIUM;
  935. if (mrq->data)
  936. mrq->data->error = -ENOMEDIUM;
  937. if (mrq->stop)
  938. mrq->stop->error = -ENOMEDIUM;
  939. spin_unlock(&host->lock);
  940. mmc_request_done(slot->mmc, mrq);
  941. spin_lock(&host->lock);
  942. }
  943. }
  944. spin_unlock(&host->lock);
  945. mmc_detect_change(slot->mmc, 0);
  946. }
  947. }
  948. static void atmci_tasklet_func(unsigned long priv)
  949. {
  950. struct atmel_mci *host = (struct atmel_mci *)priv;
  951. struct mmc_request *mrq = host->mrq;
  952. struct mmc_data *data = host->data;
  953. struct mmc_command *cmd = host->cmd;
  954. enum atmel_mci_state state = host->state;
  955. enum atmel_mci_state prev_state;
  956. u32 status;
  957. spin_lock(&host->lock);
  958. state = host->state;
  959. dev_vdbg(&host->pdev->dev,
  960. "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
  961. state, host->pending_events, host->completed_events,
  962. mci_readl(host, IMR));
  963. do {
  964. prev_state = state;
  965. switch (state) {
  966. case STATE_IDLE:
  967. break;
  968. case STATE_SENDING_CMD:
  969. if (!atmci_test_and_clear_pending(host,
  970. EVENT_CMD_COMPLETE))
  971. break;
  972. host->cmd = NULL;
  973. atmci_set_completed(host, EVENT_CMD_COMPLETE);
  974. atmci_command_complete(host, mrq->cmd);
  975. if (!mrq->data || cmd->error) {
  976. atmci_request_end(host, host->mrq);
  977. goto unlock;
  978. }
  979. prev_state = state = STATE_SENDING_DATA;
  980. /* fall through */
  981. case STATE_SENDING_DATA:
  982. if (atmci_test_and_clear_pending(host,
  983. EVENT_DATA_ERROR)) {
  984. atmci_stop_dma(host);
  985. if (data->stop)
  986. send_stop_cmd(host, data);
  987. state = STATE_DATA_ERROR;
  988. break;
  989. }
  990. if (!atmci_test_and_clear_pending(host,
  991. EVENT_XFER_COMPLETE))
  992. break;
  993. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  994. prev_state = state = STATE_DATA_BUSY;
  995. /* fall through */
  996. case STATE_DATA_BUSY:
  997. if (!atmci_test_and_clear_pending(host,
  998. EVENT_DATA_COMPLETE))
  999. break;
  1000. host->data = NULL;
  1001. atmci_set_completed(host, EVENT_DATA_COMPLETE);
  1002. status = host->data_status;
  1003. if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
  1004. if (status & MCI_DTOE) {
  1005. dev_dbg(&host->pdev->dev,
  1006. "data timeout error\n");
  1007. data->error = -ETIMEDOUT;
  1008. } else if (status & MCI_DCRCE) {
  1009. dev_dbg(&host->pdev->dev,
  1010. "data CRC error\n");
  1011. data->error = -EILSEQ;
  1012. } else {
  1013. dev_dbg(&host->pdev->dev,
  1014. "data FIFO error (status=%08x)\n",
  1015. status);
  1016. data->error = -EIO;
  1017. }
  1018. } else {
  1019. data->bytes_xfered = data->blocks * data->blksz;
  1020. data->error = 0;
  1021. }
  1022. if (!data->stop) {
  1023. atmci_request_end(host, host->mrq);
  1024. goto unlock;
  1025. }
  1026. prev_state = state = STATE_SENDING_STOP;
  1027. if (!data->error)
  1028. send_stop_cmd(host, data);
  1029. /* fall through */
  1030. case STATE_SENDING_STOP:
  1031. if (!atmci_test_and_clear_pending(host,
  1032. EVENT_CMD_COMPLETE))
  1033. break;
  1034. host->cmd = NULL;
  1035. atmci_command_complete(host, mrq->stop);
  1036. atmci_request_end(host, host->mrq);
  1037. goto unlock;
  1038. case STATE_DATA_ERROR:
  1039. if (!atmci_test_and_clear_pending(host,
  1040. EVENT_XFER_COMPLETE))
  1041. break;
  1042. state = STATE_DATA_BUSY;
  1043. break;
  1044. }
  1045. } while (state != prev_state);
  1046. host->state = state;
  1047. unlock:
  1048. spin_unlock(&host->lock);
  1049. }
  1050. static void atmci_read_data_pio(struct atmel_mci *host)
  1051. {
  1052. struct scatterlist *sg = host->sg;
  1053. void *buf = sg_virt(sg);
  1054. unsigned int offset = host->pio_offset;
  1055. struct mmc_data *data = host->data;
  1056. u32 value;
  1057. u32 status;
  1058. unsigned int nbytes = 0;
  1059. do {
  1060. value = mci_readl(host, RDR);
  1061. if (likely(offset + 4 <= sg->length)) {
  1062. put_unaligned(value, (u32 *)(buf + offset));
  1063. offset += 4;
  1064. nbytes += 4;
  1065. if (offset == sg->length) {
  1066. flush_dcache_page(sg_page(sg));
  1067. host->sg = sg = sg_next(sg);
  1068. if (!sg)
  1069. goto done;
  1070. offset = 0;
  1071. buf = sg_virt(sg);
  1072. }
  1073. } else {
  1074. unsigned int remaining = sg->length - offset;
  1075. memcpy(buf + offset, &value, remaining);
  1076. nbytes += remaining;
  1077. flush_dcache_page(sg_page(sg));
  1078. host->sg = sg = sg_next(sg);
  1079. if (!sg)
  1080. goto done;
  1081. offset = 4 - remaining;
  1082. buf = sg_virt(sg);
  1083. memcpy(buf, (u8 *)&value + remaining, offset);
  1084. nbytes += offset;
  1085. }
  1086. status = mci_readl(host, SR);
  1087. if (status & ATMCI_DATA_ERROR_FLAGS) {
  1088. mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
  1089. | ATMCI_DATA_ERROR_FLAGS));
  1090. host->data_status = status;
  1091. data->bytes_xfered += nbytes;
  1092. smp_wmb();
  1093. atmci_set_pending(host, EVENT_DATA_ERROR);
  1094. tasklet_schedule(&host->tasklet);
  1095. return;
  1096. }
  1097. } while (status & MCI_RXRDY);
  1098. host->pio_offset = offset;
  1099. data->bytes_xfered += nbytes;
  1100. return;
  1101. done:
  1102. mci_writel(host, IDR, MCI_RXRDY);
  1103. mci_writel(host, IER, MCI_NOTBUSY);
  1104. data->bytes_xfered += nbytes;
  1105. smp_wmb();
  1106. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  1107. }
  1108. static void atmci_write_data_pio(struct atmel_mci *host)
  1109. {
  1110. struct scatterlist *sg = host->sg;
  1111. void *buf = sg_virt(sg);
  1112. unsigned int offset = host->pio_offset;
  1113. struct mmc_data *data = host->data;
  1114. u32 value;
  1115. u32 status;
  1116. unsigned int nbytes = 0;
  1117. do {
  1118. if (likely(offset + 4 <= sg->length)) {
  1119. value = get_unaligned((u32 *)(buf + offset));
  1120. mci_writel(host, TDR, value);
  1121. offset += 4;
  1122. nbytes += 4;
  1123. if (offset == sg->length) {
  1124. host->sg = sg = sg_next(sg);
  1125. if (!sg)
  1126. goto done;
  1127. offset = 0;
  1128. buf = sg_virt(sg);
  1129. }
  1130. } else {
  1131. unsigned int remaining = sg->length - offset;
  1132. value = 0;
  1133. memcpy(&value, buf + offset, remaining);
  1134. nbytes += remaining;
  1135. host->sg = sg = sg_next(sg);
  1136. if (!sg) {
  1137. mci_writel(host, TDR, value);
  1138. goto done;
  1139. }
  1140. offset = 4 - remaining;
  1141. buf = sg_virt(sg);
  1142. memcpy((u8 *)&value + remaining, buf, offset);
  1143. mci_writel(host, TDR, value);
  1144. nbytes += offset;
  1145. }
  1146. status = mci_readl(host, SR);
  1147. if (status & ATMCI_DATA_ERROR_FLAGS) {
  1148. mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
  1149. | ATMCI_DATA_ERROR_FLAGS));
  1150. host->data_status = status;
  1151. data->bytes_xfered += nbytes;
  1152. smp_wmb();
  1153. atmci_set_pending(host, EVENT_DATA_ERROR);
  1154. tasklet_schedule(&host->tasklet);
  1155. return;
  1156. }
  1157. } while (status & MCI_TXRDY);
  1158. host->pio_offset = offset;
  1159. data->bytes_xfered += nbytes;
  1160. return;
  1161. done:
  1162. mci_writel(host, IDR, MCI_TXRDY);
  1163. mci_writel(host, IER, MCI_NOTBUSY);
  1164. data->bytes_xfered += nbytes;
  1165. smp_wmb();
  1166. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  1167. }
  1168. static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
  1169. {
  1170. mci_writel(host, IDR, MCI_CMDRDY);
  1171. host->cmd_status = status;
  1172. smp_wmb();
  1173. atmci_set_pending(host, EVENT_CMD_COMPLETE);
  1174. tasklet_schedule(&host->tasklet);
  1175. }
  1176. static irqreturn_t atmci_interrupt(int irq, void *dev_id)
  1177. {
  1178. struct atmel_mci *host = dev_id;
  1179. u32 status, mask, pending;
  1180. unsigned int pass_count = 0;
  1181. do {
  1182. status = mci_readl(host, SR);
  1183. mask = mci_readl(host, IMR);
  1184. pending = status & mask;
  1185. if (!pending)
  1186. break;
  1187. if (pending & ATMCI_DATA_ERROR_FLAGS) {
  1188. mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
  1189. | MCI_RXRDY | MCI_TXRDY);
  1190. pending &= mci_readl(host, IMR);
  1191. host->data_status = status;
  1192. smp_wmb();
  1193. atmci_set_pending(host, EVENT_DATA_ERROR);
  1194. tasklet_schedule(&host->tasklet);
  1195. }
  1196. if (pending & MCI_NOTBUSY) {
  1197. mci_writel(host, IDR,
  1198. ATMCI_DATA_ERROR_FLAGS | MCI_NOTBUSY);
  1199. if (!host->data_status)
  1200. host->data_status = status;
  1201. smp_wmb();
  1202. atmci_set_pending(host, EVENT_DATA_COMPLETE);
  1203. tasklet_schedule(&host->tasklet);
  1204. }
  1205. if (pending & MCI_RXRDY)
  1206. atmci_read_data_pio(host);
  1207. if (pending & MCI_TXRDY)
  1208. atmci_write_data_pio(host);
  1209. if (pending & MCI_CMDRDY)
  1210. atmci_cmd_interrupt(host, status);
  1211. } while (pass_count++ < 5);
  1212. return pass_count ? IRQ_HANDLED : IRQ_NONE;
  1213. }
  1214. static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
  1215. {
  1216. struct atmel_mci_slot *slot = dev_id;
  1217. /*
  1218. * Disable interrupts until the pin has stabilized and check
  1219. * the state then. Use mod_timer() since we may be in the
  1220. * middle of the timer routine when this interrupt triggers.
  1221. */
  1222. disable_irq_nosync(irq);
  1223. mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
  1224. return IRQ_HANDLED;
  1225. }
  1226. static int __init atmci_init_slot(struct atmel_mci *host,
  1227. struct mci_slot_pdata *slot_data, unsigned int id,
  1228. u32 sdc_reg)
  1229. {
  1230. struct mmc_host *mmc;
  1231. struct atmel_mci_slot *slot;
  1232. mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
  1233. if (!mmc)
  1234. return -ENOMEM;
  1235. slot = mmc_priv(mmc);
  1236. slot->mmc = mmc;
  1237. slot->host = host;
  1238. slot->detect_pin = slot_data->detect_pin;
  1239. slot->wp_pin = slot_data->wp_pin;
  1240. slot->sdc_reg = sdc_reg;
  1241. mmc->ops = &atmci_ops;
  1242. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
  1243. mmc->f_max = host->bus_hz / 2;
  1244. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1245. if (slot_data->bus_width >= 4)
  1246. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1247. mmc->max_hw_segs = 64;
  1248. mmc->max_phys_segs = 64;
  1249. mmc->max_req_size = 32768 * 512;
  1250. mmc->max_blk_size = 32768;
  1251. mmc->max_blk_count = 512;
  1252. /* Assume card is present initially */
  1253. set_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1254. if (gpio_is_valid(slot->detect_pin)) {
  1255. if (gpio_request(slot->detect_pin, "mmc_detect")) {
  1256. dev_dbg(&mmc->class_dev, "no detect pin available\n");
  1257. slot->detect_pin = -EBUSY;
  1258. } else if (gpio_get_value(slot->detect_pin)) {
  1259. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1260. }
  1261. }
  1262. if (!gpio_is_valid(slot->detect_pin))
  1263. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1264. if (gpio_is_valid(slot->wp_pin)) {
  1265. if (gpio_request(slot->wp_pin, "mmc_wp")) {
  1266. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  1267. slot->wp_pin = -EBUSY;
  1268. }
  1269. }
  1270. host->slot[id] = slot;
  1271. mmc_add_host(mmc);
  1272. if (gpio_is_valid(slot->detect_pin)) {
  1273. int ret;
  1274. setup_timer(&slot->detect_timer, atmci_detect_change,
  1275. (unsigned long)slot);
  1276. ret = request_irq(gpio_to_irq(slot->detect_pin),
  1277. atmci_detect_interrupt,
  1278. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  1279. "mmc-detect", slot);
  1280. if (ret) {
  1281. dev_dbg(&mmc->class_dev,
  1282. "could not request IRQ %d for detect pin\n",
  1283. gpio_to_irq(slot->detect_pin));
  1284. gpio_free(slot->detect_pin);
  1285. slot->detect_pin = -EBUSY;
  1286. }
  1287. }
  1288. atmci_init_debugfs(slot);
  1289. return 0;
  1290. }
  1291. static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
  1292. unsigned int id)
  1293. {
  1294. /* Debugfs stuff is cleaned up by mmc core */
  1295. set_bit(ATMCI_SHUTDOWN, &slot->flags);
  1296. smp_wmb();
  1297. mmc_remove_host(slot->mmc);
  1298. if (gpio_is_valid(slot->detect_pin)) {
  1299. int pin = slot->detect_pin;
  1300. free_irq(gpio_to_irq(pin), slot);
  1301. del_timer_sync(&slot->detect_timer);
  1302. gpio_free(pin);
  1303. }
  1304. if (gpio_is_valid(slot->wp_pin))
  1305. gpio_free(slot->wp_pin);
  1306. slot->host->slot[id] = NULL;
  1307. mmc_free_host(slot->mmc);
  1308. }
  1309. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1310. static bool filter(struct dma_chan *chan, void *slave)
  1311. {
  1312. struct dw_dma_slave *dws = slave;
  1313. if (dws->dma_dev == chan->device->dev) {
  1314. chan->private = dws;
  1315. return true;
  1316. } else
  1317. return false;
  1318. }
  1319. #endif
  1320. static int __init atmci_probe(struct platform_device *pdev)
  1321. {
  1322. struct mci_platform_data *pdata;
  1323. struct atmel_mci *host;
  1324. struct resource *regs;
  1325. unsigned int nr_slots;
  1326. int irq;
  1327. int ret;
  1328. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1329. if (!regs)
  1330. return -ENXIO;
  1331. pdata = pdev->dev.platform_data;
  1332. if (!pdata)
  1333. return -ENXIO;
  1334. irq = platform_get_irq(pdev, 0);
  1335. if (irq < 0)
  1336. return irq;
  1337. host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
  1338. if (!host)
  1339. return -ENOMEM;
  1340. host->pdev = pdev;
  1341. spin_lock_init(&host->lock);
  1342. INIT_LIST_HEAD(&host->queue);
  1343. host->mck = clk_get(&pdev->dev, "mci_clk");
  1344. if (IS_ERR(host->mck)) {
  1345. ret = PTR_ERR(host->mck);
  1346. goto err_clk_get;
  1347. }
  1348. ret = -ENOMEM;
  1349. host->regs = ioremap(regs->start, regs->end - regs->start + 1);
  1350. if (!host->regs)
  1351. goto err_ioremap;
  1352. clk_enable(host->mck);
  1353. mci_writel(host, CR, MCI_CR_SWRST);
  1354. host->bus_hz = clk_get_rate(host->mck);
  1355. clk_disable(host->mck);
  1356. host->mapbase = regs->start;
  1357. tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
  1358. ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
  1359. if (ret)
  1360. goto err_request_irq;
  1361. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1362. if (pdata->dma_slave.dma_dev) {
  1363. struct dw_dma_slave *dws = &pdata->dma_slave;
  1364. dma_cap_mask_t mask;
  1365. dws->tx_reg = regs->start + MCI_TDR;
  1366. dws->rx_reg = regs->start + MCI_RDR;
  1367. /* Try to grab a DMA channel */
  1368. dma_cap_zero(mask);
  1369. dma_cap_set(DMA_SLAVE, mask);
  1370. host->dma.chan = dma_request_channel(mask, filter, dws);
  1371. }
  1372. if (!host->dma.chan)
  1373. dev_notice(&pdev->dev, "DMA not available, using PIO\n");
  1374. #endif /* CONFIG_MMC_ATMELMCI_DMA */
  1375. platform_set_drvdata(pdev, host);
  1376. /* We need at least one slot to succeed */
  1377. nr_slots = 0;
  1378. ret = -ENODEV;
  1379. if (pdata->slot[0].bus_width) {
  1380. ret = atmci_init_slot(host, &pdata->slot[0],
  1381. MCI_SDCSEL_SLOT_A, 0);
  1382. if (!ret)
  1383. nr_slots++;
  1384. }
  1385. if (pdata->slot[1].bus_width) {
  1386. ret = atmci_init_slot(host, &pdata->slot[1],
  1387. MCI_SDCSEL_SLOT_B, 1);
  1388. if (!ret)
  1389. nr_slots++;
  1390. }
  1391. if (!nr_slots)
  1392. goto err_init_slot;
  1393. dev_info(&pdev->dev,
  1394. "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
  1395. host->mapbase, irq, nr_slots);
  1396. return 0;
  1397. err_init_slot:
  1398. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1399. if (host->dma.chan)
  1400. dma_release_channel(host->dma.chan);
  1401. #endif
  1402. free_irq(irq, host);
  1403. err_request_irq:
  1404. iounmap(host->regs);
  1405. err_ioremap:
  1406. clk_put(host->mck);
  1407. err_clk_get:
  1408. kfree(host);
  1409. return ret;
  1410. }
  1411. static int __exit atmci_remove(struct platform_device *pdev)
  1412. {
  1413. struct atmel_mci *host = platform_get_drvdata(pdev);
  1414. unsigned int i;
  1415. platform_set_drvdata(pdev, NULL);
  1416. for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
  1417. if (host->slot[i])
  1418. atmci_cleanup_slot(host->slot[i], i);
  1419. }
  1420. clk_enable(host->mck);
  1421. mci_writel(host, IDR, ~0UL);
  1422. mci_writel(host, CR, MCI_CR_MCIDIS);
  1423. mci_readl(host, SR);
  1424. clk_disable(host->mck);
  1425. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1426. if (host->dma.chan)
  1427. dma_release_channel(host->dma.chan);
  1428. #endif
  1429. free_irq(platform_get_irq(pdev, 0), host);
  1430. iounmap(host->regs);
  1431. clk_put(host->mck);
  1432. kfree(host);
  1433. return 0;
  1434. }
  1435. static struct platform_driver atmci_driver = {
  1436. .remove = __exit_p(atmci_remove),
  1437. .driver = {
  1438. .name = "atmel_mci",
  1439. },
  1440. };
  1441. static int __init atmci_init(void)
  1442. {
  1443. return platform_driver_probe(&atmci_driver, atmci_probe);
  1444. }
  1445. static void __exit atmci_exit(void)
  1446. {
  1447. platform_driver_unregister(&atmci_driver);
  1448. }
  1449. late_initcall(atmci_init); /* try to load after dma driver when built-in */
  1450. module_exit(atmci_exit);
  1451. MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
  1452. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
  1453. MODULE_LICENSE("GPL v2");