atmel-mci.c 65 KB

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