atmel-mci.c 46 KB

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