atmel-mci.c 65 KB

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