atmel-mci.c 46 KB

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