atmel-mci.c 43 KB

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