atmel-mci.c 56 KB

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