atmel-mci.c 46 KB

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