atmel-mci.c 42 KB

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