atmel-mci.c 45 KB

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