atmel-mci.c 67 KB

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