atmel-mci.c 58 KB

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