atmel-mci.c 57 KB

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