atmel-mci.c 47 KB

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