atmel-mci.c 65 KB

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