atmel-mci.c 67 KB

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