atmel-mci.c 67 KB

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