atmel-mci.c 59 KB

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