atmel-mci.c 57 KB

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