atmel-mci.c 68 KB

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