atmel-mci.c 56 KB

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