atmel-mci.c 65 KB

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