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