atmel-mci.c 42 KB

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