atmel-mci.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. /*
  2. * Atmel MultiMedia Card Interface driver
  3. *
  4. * Copyright (C) 2004-2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/clk.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/device.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioport.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/slab.h>
  26. #include <linux/stat.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/sdio.h>
  29. #include <mach/atmel-mci.h>
  30. #include <linux/atmel-mci.h>
  31. #include <asm/io.h>
  32. #include <asm/unaligned.h>
  33. #include <mach/cpu.h>
  34. #include <mach/board.h>
  35. #include "atmel-mci-regs.h"
  36. #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
  37. #define ATMCI_DMA_THRESHOLD 16
  38. enum {
  39. EVENT_CMD_COMPLETE = 0,
  40. EVENT_XFER_COMPLETE,
  41. EVENT_DATA_COMPLETE,
  42. EVENT_DATA_ERROR,
  43. };
  44. enum atmel_mci_state {
  45. STATE_IDLE = 0,
  46. STATE_SENDING_CMD,
  47. STATE_SENDING_DATA,
  48. STATE_DATA_BUSY,
  49. STATE_SENDING_STOP,
  50. STATE_DATA_ERROR,
  51. };
  52. struct atmel_mci_dma {
  53. #ifdef CONFIG_MMC_ATMELMCI_DMA
  54. struct dma_chan *chan;
  55. struct dma_async_tx_descriptor *data_desc;
  56. #endif
  57. };
  58. /**
  59. * struct atmel_mci - MMC controller state shared between all slots
  60. * @lock: Spinlock protecting the queue and associated data.
  61. * @regs: Pointer to MMIO registers.
  62. * @sg: Scatterlist entry currently being processed by PIO code, if any.
  63. * @pio_offset: Offset into the current scatterlist entry.
  64. * @cur_slot: The slot which is currently using the controller.
  65. * @mrq: The request currently being processed on @cur_slot,
  66. * or NULL if the controller is idle.
  67. * @cmd: The command currently being sent to the card, or NULL.
  68. * @data: The data currently being transferred, or NULL if no data
  69. * transfer is in progress.
  70. * @dma: DMA client state.
  71. * @data_chan: DMA channel being used for the current data transfer.
  72. * @cmd_status: Snapshot of SR taken upon completion of the current
  73. * command. Only valid when EVENT_CMD_COMPLETE is pending.
  74. * @data_status: Snapshot of SR taken upon completion of the current
  75. * data transfer. Only valid when EVENT_DATA_COMPLETE or
  76. * EVENT_DATA_ERROR is pending.
  77. * @stop_cmdr: Value to be loaded into CMDR when the stop command is
  78. * to be sent.
  79. * @tasklet: Tasklet running the request state machine.
  80. * @pending_events: Bitmask of events flagged by the interrupt handler
  81. * to be processed by the tasklet.
  82. * @completed_events: Bitmask of events which the state machine has
  83. * processed.
  84. * @state: Tasklet state.
  85. * @queue: List of slots waiting for access to the controller.
  86. * @need_clock_update: Update the clock rate before the next request.
  87. * @need_reset: Reset controller before next request.
  88. * @mode_reg: Value of the MR register.
  89. * @cfg_reg: Value of the CFG register.
  90. * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
  91. * rate and timeout calculations.
  92. * @mapbase: Physical address of the MMIO registers.
  93. * @mck: The peripheral bus clock hooked up to the MMC controller.
  94. * @pdev: Platform device associated with the MMC controller.
  95. * @slot: Slots sharing this MMC controller.
  96. *
  97. * Locking
  98. * =======
  99. *
  100. * @lock is a softirq-safe spinlock protecting @queue as well as
  101. * @cur_slot, @mrq and @state. These must always be updated
  102. * at the same time while holding @lock.
  103. *
  104. * @lock also protects mode_reg and need_clock_update since these are
  105. * used to synchronize mode register updates with the queue
  106. * processing.
  107. *
  108. * The @mrq field of struct atmel_mci_slot is also protected by @lock,
  109. * and must always be written at the same time as the slot is added to
  110. * @queue.
  111. *
  112. * @pending_events and @completed_events are accessed using atomic bit
  113. * operations, so they don't need any locking.
  114. *
  115. * None of the fields touched by the interrupt handler need any
  116. * locking. However, ordering is important: Before EVENT_DATA_ERROR or
  117. * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
  118. * interrupts must be disabled and @data_status updated with a
  119. * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
  120. * CMDRDY interrupt must be disabled and @cmd_status updated with a
  121. * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
  122. * bytes_xfered field of @data must be written. This is ensured by
  123. * using barriers.
  124. */
  125. struct atmel_mci {
  126. spinlock_t lock;
  127. void __iomem *regs;
  128. struct scatterlist *sg;
  129. unsigned int pio_offset;
  130. struct atmel_mci_slot *cur_slot;
  131. struct mmc_request *mrq;
  132. struct mmc_command *cmd;
  133. struct mmc_data *data;
  134. struct atmel_mci_dma dma;
  135. struct dma_chan *data_chan;
  136. u32 cmd_status;
  137. u32 data_status;
  138. u32 stop_cmdr;
  139. struct tasklet_struct tasklet;
  140. unsigned long pending_events;
  141. unsigned long completed_events;
  142. enum atmel_mci_state state;
  143. struct list_head queue;
  144. bool need_clock_update;
  145. bool need_reset;
  146. u32 mode_reg;
  147. u32 cfg_reg;
  148. unsigned long bus_hz;
  149. unsigned long mapbase;
  150. struct clk *mck;
  151. struct platform_device *pdev;
  152. struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
  153. };
  154. /**
  155. * struct atmel_mci_slot - MMC slot state
  156. * @mmc: The mmc_host representing this slot.
  157. * @host: The MMC controller this slot is using.
  158. * @sdc_reg: Value of SDCR to be written before using this slot.
  159. * @sdio_irq: SDIO irq mask for this slot.
  160. * @mrq: mmc_request currently being processed or waiting to be
  161. * processed, or NULL when the slot is idle.
  162. * @queue_node: List node for placing this node in the @queue list of
  163. * &struct atmel_mci.
  164. * @clock: Clock rate configured by set_ios(). Protected by host->lock.
  165. * @flags: Random state bits associated with the slot.
  166. * @detect_pin: GPIO pin used for card detection, or negative if not
  167. * available.
  168. * @wp_pin: GPIO pin used for card write protect sending, or negative
  169. * if not available.
  170. * @detect_is_active_high: The state of the detect pin when it is active.
  171. * @detect_timer: Timer used for debouncing @detect_pin interrupts.
  172. */
  173. struct atmel_mci_slot {
  174. struct mmc_host *mmc;
  175. struct atmel_mci *host;
  176. u32 sdc_reg;
  177. u32 sdio_irq;
  178. struct mmc_request *mrq;
  179. struct list_head queue_node;
  180. unsigned int clock;
  181. unsigned long flags;
  182. #define ATMCI_CARD_PRESENT 0
  183. #define ATMCI_CARD_NEED_INIT 1
  184. #define ATMCI_SHUTDOWN 2
  185. #define ATMCI_SUSPENDED 3
  186. int detect_pin;
  187. int wp_pin;
  188. bool detect_is_active_high;
  189. struct timer_list detect_timer;
  190. };
  191. #define atmci_test_and_clear_pending(host, event) \
  192. test_and_clear_bit(event, &host->pending_events)
  193. #define atmci_set_completed(host, event) \
  194. set_bit(event, &host->completed_events)
  195. #define atmci_set_pending(host, event) \
  196. set_bit(event, &host->pending_events)
  197. /*
  198. * Enable or disable features/registers based on
  199. * whether the processor supports them
  200. */
  201. static bool atmci_has_rwproof(void)
  202. {
  203. if (cpu_is_at91sam9261() || cpu_is_at91rm9200())
  204. return false;
  205. else
  206. return true;
  207. }
  208. /*
  209. * The new MCI2 module isn't 100% compatible with the old MCI module,
  210. * and it has a few nice features which we want to use...
  211. */
  212. static inline bool atmci_is_mci2(void)
  213. {
  214. if (cpu_is_at91sam9g45())
  215. return true;
  216. return false;
  217. }
  218. /*
  219. * The debugfs stuff below is mostly optimized away when
  220. * CONFIG_DEBUG_FS is not set.
  221. */
  222. static int atmci_req_show(struct seq_file *s, void *v)
  223. {
  224. struct atmel_mci_slot *slot = s->private;
  225. struct mmc_request *mrq;
  226. struct mmc_command *cmd;
  227. struct mmc_command *stop;
  228. struct mmc_data *data;
  229. /* Make sure we get a consistent snapshot */
  230. spin_lock_bh(&slot->host->lock);
  231. mrq = slot->mrq;
  232. if (mrq) {
  233. cmd = mrq->cmd;
  234. data = mrq->data;
  235. stop = mrq->stop;
  236. if (cmd)
  237. seq_printf(s,
  238. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  239. cmd->opcode, cmd->arg, cmd->flags,
  240. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  241. cmd->resp[3], cmd->error);
  242. if (data)
  243. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  244. data->bytes_xfered, data->blocks,
  245. data->blksz, data->flags, data->error);
  246. if (stop)
  247. seq_printf(s,
  248. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  249. stop->opcode, stop->arg, stop->flags,
  250. stop->resp[0], stop->resp[1], stop->resp[2],
  251. stop->resp[3], stop->error);
  252. }
  253. spin_unlock_bh(&slot->host->lock);
  254. return 0;
  255. }
  256. static int atmci_req_open(struct inode *inode, struct file *file)
  257. {
  258. return single_open(file, atmci_req_show, inode->i_private);
  259. }
  260. static const struct file_operations atmci_req_fops = {
  261. .owner = THIS_MODULE,
  262. .open = atmci_req_open,
  263. .read = seq_read,
  264. .llseek = seq_lseek,
  265. .release = single_release,
  266. };
  267. static void atmci_show_status_reg(struct seq_file *s,
  268. const char *regname, u32 value)
  269. {
  270. static const char *sr_bit[] = {
  271. [0] = "CMDRDY",
  272. [1] = "RXRDY",
  273. [2] = "TXRDY",
  274. [3] = "BLKE",
  275. [4] = "DTIP",
  276. [5] = "NOTBUSY",
  277. [6] = "ENDRX",
  278. [7] = "ENDTX",
  279. [8] = "SDIOIRQA",
  280. [9] = "SDIOIRQB",
  281. [12] = "SDIOWAIT",
  282. [14] = "RXBUFF",
  283. [15] = "TXBUFE",
  284. [16] = "RINDE",
  285. [17] = "RDIRE",
  286. [18] = "RCRCE",
  287. [19] = "RENDE",
  288. [20] = "RTOE",
  289. [21] = "DCRCE",
  290. [22] = "DTOE",
  291. [23] = "CSTOE",
  292. [24] = "BLKOVRE",
  293. [25] = "DMADONE",
  294. [26] = "FIFOEMPTY",
  295. [27] = "XFRDONE",
  296. [30] = "OVRE",
  297. [31] = "UNRE",
  298. };
  299. unsigned int i;
  300. seq_printf(s, "%s:\t0x%08x", regname, value);
  301. for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
  302. if (value & (1 << i)) {
  303. if (sr_bit[i])
  304. seq_printf(s, " %s", sr_bit[i]);
  305. else
  306. seq_puts(s, " UNKNOWN");
  307. }
  308. }
  309. seq_putc(s, '\n');
  310. }
  311. static int atmci_regs_show(struct seq_file *s, void *v)
  312. {
  313. struct atmel_mci *host = s->private;
  314. u32 *buf;
  315. buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
  316. if (!buf)
  317. return -ENOMEM;
  318. /*
  319. * Grab a more or less consistent snapshot. Note that we're
  320. * not disabling interrupts, so IMR and SR may not be
  321. * consistent.
  322. */
  323. spin_lock_bh(&host->lock);
  324. clk_enable(host->mck);
  325. memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
  326. clk_disable(host->mck);
  327. spin_unlock_bh(&host->lock);
  328. seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
  329. buf[ATMCI_MR / 4],
  330. buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
  331. buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "",
  332. buf[ATMCI_MR / 4] & 0xff);
  333. seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
  334. seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
  335. seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
  336. seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
  337. buf[ATMCI_BLKR / 4],
  338. buf[ATMCI_BLKR / 4] & 0xffff,
  339. (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
  340. if (atmci_is_mci2())
  341. seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
  342. /* Don't read RSPR and RDR; it will consume the data there */
  343. atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
  344. atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
  345. if (atmci_is_mci2()) {
  346. u32 val;
  347. val = buf[ATMCI_DMA / 4];
  348. seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
  349. val, val & 3,
  350. ((val >> 4) & 3) ?
  351. 1 << (((val >> 4) & 3) + 1) : 1,
  352. val & ATMCI_DMAEN ? " DMAEN" : "");
  353. val = buf[ATMCI_CFG / 4];
  354. seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
  355. val,
  356. val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
  357. val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
  358. val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
  359. val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
  360. }
  361. kfree(buf);
  362. return 0;
  363. }
  364. static int atmci_regs_open(struct inode *inode, struct file *file)
  365. {
  366. return single_open(file, atmci_regs_show, inode->i_private);
  367. }
  368. static const struct file_operations atmci_regs_fops = {
  369. .owner = THIS_MODULE,
  370. .open = atmci_regs_open,
  371. .read = seq_read,
  372. .llseek = seq_lseek,
  373. .release = single_release,
  374. };
  375. static void atmci_init_debugfs(struct atmel_mci_slot *slot)
  376. {
  377. struct mmc_host *mmc = slot->mmc;
  378. struct atmel_mci *host = slot->host;
  379. struct dentry *root;
  380. struct dentry *node;
  381. root = mmc->debugfs_root;
  382. if (!root)
  383. return;
  384. node = debugfs_create_file("regs", S_IRUSR, root, host,
  385. &atmci_regs_fops);
  386. if (IS_ERR(node))
  387. return;
  388. if (!node)
  389. goto err;
  390. node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
  391. if (!node)
  392. goto err;
  393. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  394. if (!node)
  395. goto err;
  396. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  397. (u32 *)&host->pending_events);
  398. if (!node)
  399. goto err;
  400. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  401. (u32 *)&host->completed_events);
  402. if (!node)
  403. goto err;
  404. return;
  405. err:
  406. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  407. }
  408. static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
  409. unsigned int ns)
  410. {
  411. return (ns * (host->bus_hz / 1000000) + 999) / 1000;
  412. }
  413. static void atmci_set_timeout(struct atmel_mci *host,
  414. struct atmel_mci_slot *slot, struct mmc_data *data)
  415. {
  416. static unsigned dtomul_to_shift[] = {
  417. 0, 4, 7, 8, 10, 12, 16, 20
  418. };
  419. unsigned timeout;
  420. unsigned dtocyc;
  421. unsigned dtomul;
  422. timeout = atmci_ns_to_clocks(host, data->timeout_ns)
  423. + data->timeout_clks;
  424. for (dtomul = 0; dtomul < 8; dtomul++) {
  425. unsigned shift = dtomul_to_shift[dtomul];
  426. dtocyc = (timeout + (1 << shift) - 1) >> shift;
  427. if (dtocyc < 15)
  428. break;
  429. }
  430. if (dtomul >= 8) {
  431. dtomul = 7;
  432. dtocyc = 15;
  433. }
  434. dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
  435. dtocyc << dtomul_to_shift[dtomul]);
  436. atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
  437. }
  438. /*
  439. * Return mask with command flags to be enabled for this command.
  440. */
  441. static u32 atmci_prepare_command(struct mmc_host *mmc,
  442. struct mmc_command *cmd)
  443. {
  444. struct mmc_data *data;
  445. u32 cmdr;
  446. cmd->error = -EINPROGRESS;
  447. cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
  448. if (cmd->flags & MMC_RSP_PRESENT) {
  449. if (cmd->flags & MMC_RSP_136)
  450. cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
  451. else
  452. cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
  453. }
  454. /*
  455. * This should really be MAXLAT_5 for CMD2 and ACMD41, but
  456. * it's too difficult to determine whether this is an ACMD or
  457. * not. Better make it 64.
  458. */
  459. cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
  460. if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
  461. cmdr |= ATMCI_CMDR_OPDCMD;
  462. data = cmd->data;
  463. if (data) {
  464. cmdr |= ATMCI_CMDR_START_XFER;
  465. if (cmd->opcode == SD_IO_RW_EXTENDED) {
  466. cmdr |= ATMCI_CMDR_SDIO_BLOCK;
  467. } else {
  468. if (data->flags & MMC_DATA_STREAM)
  469. cmdr |= ATMCI_CMDR_STREAM;
  470. else if (data->blocks > 1)
  471. cmdr |= ATMCI_CMDR_MULTI_BLOCK;
  472. else
  473. cmdr |= ATMCI_CMDR_BLOCK;
  474. }
  475. if (data->flags & MMC_DATA_READ)
  476. cmdr |= ATMCI_CMDR_TRDIR_READ;
  477. }
  478. return cmdr;
  479. }
  480. static void atmci_start_command(struct atmel_mci *host,
  481. struct mmc_command *cmd, u32 cmd_flags)
  482. {
  483. WARN_ON(host->cmd);
  484. host->cmd = cmd;
  485. dev_vdbg(&host->pdev->dev,
  486. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  487. cmd->arg, cmd_flags);
  488. atmci_writel(host, ATMCI_ARGR, cmd->arg);
  489. atmci_writel(host, ATMCI_CMDR, cmd_flags);
  490. }
  491. static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
  492. {
  493. atmci_start_command(host, data->stop, host->stop_cmdr);
  494. atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
  495. }
  496. #ifdef CONFIG_MMC_ATMELMCI_DMA
  497. static void atmci_dma_cleanup(struct atmel_mci *host)
  498. {
  499. struct mmc_data *data = host->data;
  500. if (data)
  501. dma_unmap_sg(host->dma.chan->device->dev,
  502. data->sg, data->sg_len,
  503. ((data->flags & MMC_DATA_WRITE)
  504. ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
  505. }
  506. static void atmci_stop_dma(struct atmel_mci *host)
  507. {
  508. struct dma_chan *chan = host->data_chan;
  509. if (chan) {
  510. dmaengine_terminate_all(chan);
  511. atmci_dma_cleanup(host);
  512. } else {
  513. /* Data transfer was stopped by the interrupt handler */
  514. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  515. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  516. }
  517. }
  518. /* This function is called by the DMA driver from tasklet context. */
  519. static void atmci_dma_complete(void *arg)
  520. {
  521. struct atmel_mci *host = arg;
  522. struct mmc_data *data = host->data;
  523. dev_vdbg(&host->pdev->dev, "DMA complete\n");
  524. if (atmci_is_mci2())
  525. /* Disable DMA hardware handshaking on MCI */
  526. atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
  527. atmci_dma_cleanup(host);
  528. /*
  529. * If the card was removed, data will be NULL. No point trying
  530. * to send the stop command or waiting for NBUSY in this case.
  531. */
  532. if (data) {
  533. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  534. tasklet_schedule(&host->tasklet);
  535. /*
  536. * Regardless of what the documentation says, we have
  537. * to wait for NOTBUSY even after block read
  538. * operations.
  539. *
  540. * When the DMA transfer is complete, the controller
  541. * may still be reading the CRC from the card, i.e.
  542. * the data transfer is still in progress and we
  543. * haven't seen all the potential error bits yet.
  544. *
  545. * The interrupt handler will schedule a different
  546. * tasklet to finish things up when the data transfer
  547. * is completely done.
  548. *
  549. * We may not complete the mmc request here anyway
  550. * because the mmc layer may call back and cause us to
  551. * violate the "don't submit new operations from the
  552. * completion callback" rule of the dma engine
  553. * framework.
  554. */
  555. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  556. }
  557. }
  558. static int
  559. atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
  560. {
  561. struct dma_chan *chan;
  562. struct dma_async_tx_descriptor *desc;
  563. struct scatterlist *sg;
  564. unsigned int i;
  565. enum dma_data_direction direction;
  566. unsigned int sglen;
  567. /*
  568. * We don't do DMA on "complex" transfers, i.e. with
  569. * non-word-aligned buffers or lengths. Also, we don't bother
  570. * with all the DMA setup overhead for short transfers.
  571. */
  572. if (data->blocks * data->blksz < ATATMCI_DMA_THRESHOLD)
  573. return -EINVAL;
  574. if (data->blksz & 3)
  575. return -EINVAL;
  576. for_each_sg(data->sg, sg, data->sg_len, i) {
  577. if (sg->offset & 3 || sg->length & 3)
  578. return -EINVAL;
  579. }
  580. /* If we don't have a channel, we can't do DMA */
  581. chan = host->dma.chan;
  582. if (chan)
  583. host->data_chan = chan;
  584. if (!chan)
  585. return -ENODEV;
  586. if (atmci_is_mci2())
  587. atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(3) | ATMCI_DMAEN);
  588. if (data->flags & MMC_DATA_READ)
  589. direction = DMA_FROM_DEVICE;
  590. else
  591. direction = DMA_TO_DEVICE;
  592. sglen = dma_map_sg(chan->device->dev, data->sg,
  593. data->sg_len, direction);
  594. desc = chan->device->device_prep_slave_sg(chan,
  595. data->sg, sglen, direction,
  596. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  597. if (!desc)
  598. goto unmap_exit;
  599. host->dma.data_desc = desc;
  600. desc->callback = atmci_dma_complete;
  601. desc->callback_param = host;
  602. return 0;
  603. unmap_exit:
  604. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
  605. return -ENOMEM;
  606. }
  607. static void atmci_submit_data(struct atmel_mci *host)
  608. {
  609. struct dma_chan *chan = host->data_chan;
  610. struct dma_async_tx_descriptor *desc = host->dma.data_desc;
  611. if (chan) {
  612. dmaengine_submit(desc);
  613. dma_async_issue_pending(chan);
  614. }
  615. }
  616. #else /* CONFIG_MMC_ATMELMCI_DMA */
  617. static int atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
  618. {
  619. return -ENOSYS;
  620. }
  621. static void atmci_submit_data(struct atmel_mci *host) {}
  622. static void atmci_stop_dma(struct atmel_mci *host)
  623. {
  624. /* Data transfer was stopped by the interrupt handler */
  625. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  626. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  627. }
  628. #endif /* CONFIG_MMC_ATMELMCI_DMA */
  629. /*
  630. * Returns a mask of interrupt flags to be enabled after the whole
  631. * request has been prepared.
  632. */
  633. static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
  634. {
  635. u32 iflags;
  636. data->error = -EINPROGRESS;
  637. WARN_ON(host->data);
  638. host->sg = NULL;
  639. host->data = data;
  640. iflags = ATMCI_DATA_ERROR_FLAGS;
  641. if (atmci_prepare_data_dma(host, data)) {
  642. host->data_chan = NULL;
  643. /*
  644. * Errata: MMC data write operation with less than 12
  645. * bytes is impossible.
  646. *
  647. * Errata: MCI Transmit Data Register (TDR) FIFO
  648. * corruption when length is not multiple of 4.
  649. */
  650. if (data->blocks * data->blksz < 12
  651. || (data->blocks * data->blksz) & 3)
  652. host->need_reset = true;
  653. host->sg = data->sg;
  654. host->pio_offset = 0;
  655. if (data->flags & MMC_DATA_READ)
  656. iflags |= ATMCI_RXRDY;
  657. else
  658. iflags |= ATMCI_TXRDY;
  659. }
  660. return iflags;
  661. }
  662. static void atmci_start_request(struct atmel_mci *host,
  663. struct atmel_mci_slot *slot)
  664. {
  665. struct mmc_request *mrq;
  666. struct mmc_command *cmd;
  667. struct mmc_data *data;
  668. u32 iflags;
  669. u32 cmdflags;
  670. mrq = slot->mrq;
  671. host->cur_slot = slot;
  672. host->mrq = mrq;
  673. host->pending_events = 0;
  674. host->completed_events = 0;
  675. host->data_status = 0;
  676. if (host->need_reset) {
  677. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  678. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
  679. atmci_writel(host, ATMCI_MR, host->mode_reg);
  680. if (atmci_is_mci2())
  681. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  682. host->need_reset = false;
  683. }
  684. atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
  685. iflags = atmci_readl(host, ATMCI_IMR);
  686. if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
  687. dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
  688. iflags);
  689. if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
  690. /* Send init sequence (74 clock cycles) */
  691. atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
  692. while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
  693. cpu_relax();
  694. }
  695. iflags = 0;
  696. data = mrq->data;
  697. if (data) {
  698. atmci_set_timeout(host, slot, data);
  699. /* Must set block count/size before sending command */
  700. atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
  701. | ATMCI_BLKLEN(data->blksz));
  702. dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
  703. ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
  704. iflags |= atmci_prepare_data(host, data);
  705. }
  706. iflags |= ATMCI_CMDRDY;
  707. cmd = mrq->cmd;
  708. cmdflags = atmci_prepare_command(slot->mmc, cmd);
  709. atmci_start_command(host, cmd, cmdflags);
  710. if (data)
  711. atmci_submit_data(host);
  712. if (mrq->stop) {
  713. host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
  714. host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
  715. if (!(data->flags & MMC_DATA_WRITE))
  716. host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
  717. if (data->flags & MMC_DATA_STREAM)
  718. host->stop_cmdr |= ATMCI_CMDR_STREAM;
  719. else
  720. host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
  721. }
  722. /*
  723. * We could have enabled interrupts earlier, but I suspect
  724. * that would open up a nice can of interesting race
  725. * conditions (e.g. command and data complete, but stop not
  726. * prepared yet.)
  727. */
  728. atmci_writel(host, ATMCI_IER, iflags);
  729. }
  730. static void atmci_queue_request(struct atmel_mci *host,
  731. struct atmel_mci_slot *slot, struct mmc_request *mrq)
  732. {
  733. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  734. host->state);
  735. spin_lock_bh(&host->lock);
  736. slot->mrq = mrq;
  737. if (host->state == STATE_IDLE) {
  738. host->state = STATE_SENDING_CMD;
  739. atmci_start_request(host, slot);
  740. } else {
  741. list_add_tail(&slot->queue_node, &host->queue);
  742. }
  743. spin_unlock_bh(&host->lock);
  744. }
  745. static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  746. {
  747. struct atmel_mci_slot *slot = mmc_priv(mmc);
  748. struct atmel_mci *host = slot->host;
  749. struct mmc_data *data;
  750. WARN_ON(slot->mrq);
  751. /*
  752. * We may "know" the card is gone even though there's still an
  753. * electrical connection. If so, we really need to communicate
  754. * this to the MMC core since there won't be any more
  755. * interrupts as the card is completely removed. Otherwise,
  756. * the MMC core might believe the card is still there even
  757. * though the card was just removed very slowly.
  758. */
  759. if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
  760. mrq->cmd->error = -ENOMEDIUM;
  761. mmc_request_done(mmc, mrq);
  762. return;
  763. }
  764. /* We don't support multiple blocks of weird lengths. */
  765. data = mrq->data;
  766. if (data && data->blocks > 1 && data->blksz & 3) {
  767. mrq->cmd->error = -EINVAL;
  768. mmc_request_done(mmc, mrq);
  769. }
  770. atmci_queue_request(host, slot, mrq);
  771. }
  772. static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  773. {
  774. struct atmel_mci_slot *slot = mmc_priv(mmc);
  775. struct atmel_mci *host = slot->host;
  776. unsigned int i;
  777. slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
  778. switch (ios->bus_width) {
  779. case MMC_BUS_WIDTH_1:
  780. slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
  781. break;
  782. case MMC_BUS_WIDTH_4:
  783. slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
  784. break;
  785. }
  786. if (ios->clock) {
  787. unsigned int clock_min = ~0U;
  788. u32 clkdiv;
  789. spin_lock_bh(&host->lock);
  790. if (!host->mode_reg) {
  791. clk_enable(host->mck);
  792. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  793. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
  794. if (atmci_is_mci2())
  795. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  796. }
  797. /*
  798. * Use mirror of ios->clock to prevent race with mmc
  799. * core ios update when finding the minimum.
  800. */
  801. slot->clock = ios->clock;
  802. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  803. if (host->slot[i] && host->slot[i]->clock
  804. && host->slot[i]->clock < clock_min)
  805. clock_min = host->slot[i]->clock;
  806. }
  807. /* Calculate clock divider */
  808. clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
  809. if (clkdiv > 255) {
  810. dev_warn(&mmc->class_dev,
  811. "clock %u too slow; using %lu\n",
  812. clock_min, host->bus_hz / (2 * 256));
  813. clkdiv = 255;
  814. }
  815. host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
  816. /*
  817. * WRPROOF and RDPROOF prevent overruns/underruns by
  818. * stopping the clock when the FIFO is full/empty.
  819. * This state is not expected to last for long.
  820. */
  821. if (atmci_has_rwproof())
  822. host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
  823. if (atmci_is_mci2()) {
  824. /* setup High Speed mode in relation with card capacity */
  825. if (ios->timing == MMC_TIMING_SD_HS)
  826. host->cfg_reg |= ATMCI_CFG_HSMODE;
  827. else
  828. host->cfg_reg &= ~ATMCI_CFG_HSMODE;
  829. }
  830. if (list_empty(&host->queue)) {
  831. atmci_writel(host, ATMCI_MR, host->mode_reg);
  832. if (atmci_is_mci2())
  833. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  834. } else {
  835. host->need_clock_update = true;
  836. }
  837. spin_unlock_bh(&host->lock);
  838. } else {
  839. bool any_slot_active = false;
  840. spin_lock_bh(&host->lock);
  841. slot->clock = 0;
  842. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  843. if (host->slot[i] && host->slot[i]->clock) {
  844. any_slot_active = true;
  845. break;
  846. }
  847. }
  848. if (!any_slot_active) {
  849. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
  850. if (host->mode_reg) {
  851. atmci_readl(host, ATMCI_MR);
  852. clk_disable(host->mck);
  853. }
  854. host->mode_reg = 0;
  855. }
  856. spin_unlock_bh(&host->lock);
  857. }
  858. switch (ios->power_mode) {
  859. case MMC_POWER_UP:
  860. set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
  861. break;
  862. default:
  863. /*
  864. * TODO: None of the currently available AVR32-based
  865. * boards allow MMC power to be turned off. Implement
  866. * power control when this can be tested properly.
  867. *
  868. * We also need to hook this into the clock management
  869. * somehow so that newly inserted cards aren't
  870. * subjected to a fast clock before we have a chance
  871. * to figure out what the maximum rate is. Currently,
  872. * there's no way to avoid this, and there never will
  873. * be for boards that don't support power control.
  874. */
  875. break;
  876. }
  877. }
  878. static int atmci_get_ro(struct mmc_host *mmc)
  879. {
  880. int read_only = -ENOSYS;
  881. struct atmel_mci_slot *slot = mmc_priv(mmc);
  882. if (gpio_is_valid(slot->wp_pin)) {
  883. read_only = gpio_get_value(slot->wp_pin);
  884. dev_dbg(&mmc->class_dev, "card is %s\n",
  885. read_only ? "read-only" : "read-write");
  886. }
  887. return read_only;
  888. }
  889. static int atmci_get_cd(struct mmc_host *mmc)
  890. {
  891. int present = -ENOSYS;
  892. struct atmel_mci_slot *slot = mmc_priv(mmc);
  893. if (gpio_is_valid(slot->detect_pin)) {
  894. present = !(gpio_get_value(slot->detect_pin) ^
  895. slot->detect_is_active_high);
  896. dev_dbg(&mmc->class_dev, "card is %spresent\n",
  897. present ? "" : "not ");
  898. }
  899. return present;
  900. }
  901. static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  902. {
  903. struct atmel_mci_slot *slot = mmc_priv(mmc);
  904. struct atmel_mci *host = slot->host;
  905. if (enable)
  906. atmci_writel(host, ATMCI_IER, slot->sdio_irq);
  907. else
  908. atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
  909. }
  910. static const struct mmc_host_ops atmci_ops = {
  911. .request = atmci_request,
  912. .set_ios = atmci_set_ios,
  913. .get_ro = atmci_get_ro,
  914. .get_cd = atmci_get_cd,
  915. .enable_sdio_irq = atmci_enable_sdio_irq,
  916. };
  917. /* Called with host->lock held */
  918. static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
  919. __releases(&host->lock)
  920. __acquires(&host->lock)
  921. {
  922. struct atmel_mci_slot *slot = NULL;
  923. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  924. WARN_ON(host->cmd || host->data);
  925. /*
  926. * Update the MMC clock rate if necessary. This may be
  927. * necessary if set_ios() is called when a different slot is
  928. * busy transferring data.
  929. */
  930. if (host->need_clock_update) {
  931. atmci_writel(host, ATMCI_MR, host->mode_reg);
  932. if (atmci_is_mci2())
  933. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  934. }
  935. host->cur_slot->mrq = NULL;
  936. host->mrq = NULL;
  937. if (!list_empty(&host->queue)) {
  938. slot = list_entry(host->queue.next,
  939. struct atmel_mci_slot, queue_node);
  940. list_del(&slot->queue_node);
  941. dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
  942. mmc_hostname(slot->mmc));
  943. host->state = STATE_SENDING_CMD;
  944. atmci_start_request(host, slot);
  945. } else {
  946. dev_vdbg(&host->pdev->dev, "list empty\n");
  947. host->state = STATE_IDLE;
  948. }
  949. spin_unlock(&host->lock);
  950. mmc_request_done(prev_mmc, mrq);
  951. spin_lock(&host->lock);
  952. }
  953. static void atmci_command_complete(struct atmel_mci *host,
  954. struct mmc_command *cmd)
  955. {
  956. u32 status = host->cmd_status;
  957. /* Read the response from the card (up to 16 bytes) */
  958. cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
  959. cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
  960. cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
  961. cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
  962. if (status & ATMCI_RTOE)
  963. cmd->error = -ETIMEDOUT;
  964. else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
  965. cmd->error = -EILSEQ;
  966. else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
  967. cmd->error = -EIO;
  968. else
  969. cmd->error = 0;
  970. if (cmd->error) {
  971. dev_dbg(&host->pdev->dev,
  972. "command error: status=0x%08x\n", status);
  973. if (cmd->data) {
  974. atmci_stop_dma(host);
  975. host->data = NULL;
  976. atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY
  977. | ATMCI_TXRDY | ATMCI_RXRDY
  978. | ATMCI_DATA_ERROR_FLAGS);
  979. }
  980. }
  981. }
  982. static void atmci_detect_change(unsigned long data)
  983. {
  984. struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
  985. bool present;
  986. bool present_old;
  987. /*
  988. * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
  989. * freeing the interrupt. We must not re-enable the interrupt
  990. * if it has been freed, and if we're shutting down, it
  991. * doesn't really matter whether the card is present or not.
  992. */
  993. smp_rmb();
  994. if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
  995. return;
  996. enable_irq(gpio_to_irq(slot->detect_pin));
  997. present = !(gpio_get_value(slot->detect_pin) ^
  998. slot->detect_is_active_high);
  999. present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1000. dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
  1001. present, present_old);
  1002. if (present != present_old) {
  1003. struct atmel_mci *host = slot->host;
  1004. struct mmc_request *mrq;
  1005. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1006. present ? "inserted" : "removed");
  1007. spin_lock(&host->lock);
  1008. if (!present)
  1009. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1010. else
  1011. set_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1012. /* Clean up queue if present */
  1013. mrq = slot->mrq;
  1014. if (mrq) {
  1015. if (mrq == host->mrq) {
  1016. /*
  1017. * Reset controller to terminate any ongoing
  1018. * commands or data transfers.
  1019. */
  1020. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  1021. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
  1022. atmci_writel(host, ATMCI_MR, host->mode_reg);
  1023. if (atmci_is_mci2())
  1024. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  1025. host->data = NULL;
  1026. host->cmd = NULL;
  1027. switch (host->state) {
  1028. case STATE_IDLE:
  1029. break;
  1030. case STATE_SENDING_CMD:
  1031. mrq->cmd->error = -ENOMEDIUM;
  1032. if (!mrq->data)
  1033. break;
  1034. /* fall through */
  1035. case STATE_SENDING_DATA:
  1036. mrq->data->error = -ENOMEDIUM;
  1037. atmci_stop_dma(host);
  1038. break;
  1039. case STATE_DATA_BUSY:
  1040. case STATE_DATA_ERROR:
  1041. if (mrq->data->error == -EINPROGRESS)
  1042. mrq->data->error = -ENOMEDIUM;
  1043. if (!mrq->stop)
  1044. break;
  1045. /* fall through */
  1046. case STATE_SENDING_STOP:
  1047. mrq->stop->error = -ENOMEDIUM;
  1048. break;
  1049. }
  1050. atmci_request_end(host, mrq);
  1051. } else {
  1052. list_del(&slot->queue_node);
  1053. mrq->cmd->error = -ENOMEDIUM;
  1054. if (mrq->data)
  1055. mrq->data->error = -ENOMEDIUM;
  1056. if (mrq->stop)
  1057. mrq->stop->error = -ENOMEDIUM;
  1058. spin_unlock(&host->lock);
  1059. mmc_request_done(slot->mmc, mrq);
  1060. spin_lock(&host->lock);
  1061. }
  1062. }
  1063. spin_unlock(&host->lock);
  1064. mmc_detect_change(slot->mmc, 0);
  1065. }
  1066. }
  1067. static void atmci_tasklet_func(unsigned long priv)
  1068. {
  1069. struct atmel_mci *host = (struct atmel_mci *)priv;
  1070. struct mmc_request *mrq = host->mrq;
  1071. struct mmc_data *data = host->data;
  1072. struct mmc_command *cmd = host->cmd;
  1073. enum atmel_mci_state state = host->state;
  1074. enum atmel_mci_state prev_state;
  1075. u32 status;
  1076. spin_lock(&host->lock);
  1077. state = host->state;
  1078. dev_vdbg(&host->pdev->dev,
  1079. "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
  1080. state, host->pending_events, host->completed_events,
  1081. atmci_readl(host, ATMCI_IMR));
  1082. do {
  1083. prev_state = state;
  1084. switch (state) {
  1085. case STATE_IDLE:
  1086. break;
  1087. case STATE_SENDING_CMD:
  1088. if (!atmci_test_and_clear_pending(host,
  1089. EVENT_CMD_COMPLETE))
  1090. break;
  1091. host->cmd = NULL;
  1092. atmci_set_completed(host, EVENT_CMD_COMPLETE);
  1093. atmci_command_complete(host, mrq->cmd);
  1094. if (!mrq->data || cmd->error) {
  1095. atmci_request_end(host, host->mrq);
  1096. goto unlock;
  1097. }
  1098. prev_state = state = STATE_SENDING_DATA;
  1099. /* fall through */
  1100. case STATE_SENDING_DATA:
  1101. if (atmci_test_and_clear_pending(host,
  1102. EVENT_DATA_ERROR)) {
  1103. atmci_stop_dma(host);
  1104. if (data->stop)
  1105. atmci_send_stop_cmd(host, data);
  1106. state = STATE_DATA_ERROR;
  1107. break;
  1108. }
  1109. if (!atmci_test_and_clear_pending(host,
  1110. EVENT_XFER_COMPLETE))
  1111. break;
  1112. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  1113. prev_state = state = STATE_DATA_BUSY;
  1114. /* fall through */
  1115. case STATE_DATA_BUSY:
  1116. if (!atmci_test_and_clear_pending(host,
  1117. EVENT_DATA_COMPLETE))
  1118. break;
  1119. host->data = NULL;
  1120. atmci_set_completed(host, EVENT_DATA_COMPLETE);
  1121. status = host->data_status;
  1122. if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
  1123. if (status & ATMCI_DTOE) {
  1124. dev_dbg(&host->pdev->dev,
  1125. "data timeout error\n");
  1126. data->error = -ETIMEDOUT;
  1127. } else if (status & ATMCI_DCRCE) {
  1128. dev_dbg(&host->pdev->dev,
  1129. "data CRC error\n");
  1130. data->error = -EILSEQ;
  1131. } else {
  1132. dev_dbg(&host->pdev->dev,
  1133. "data FIFO error (status=%08x)\n",
  1134. status);
  1135. data->error = -EIO;
  1136. }
  1137. } else {
  1138. data->bytes_xfered = data->blocks * data->blksz;
  1139. data->error = 0;
  1140. atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS);
  1141. }
  1142. if (!data->stop) {
  1143. atmci_request_end(host, host->mrq);
  1144. goto unlock;
  1145. }
  1146. prev_state = state = STATE_SENDING_STOP;
  1147. if (!data->error)
  1148. atmci_send_stop_cmd(host, data);
  1149. /* fall through */
  1150. case STATE_SENDING_STOP:
  1151. if (!atmci_test_and_clear_pending(host,
  1152. EVENT_CMD_COMPLETE))
  1153. break;
  1154. host->cmd = NULL;
  1155. atmci_command_complete(host, mrq->stop);
  1156. atmci_request_end(host, host->mrq);
  1157. goto unlock;
  1158. case STATE_DATA_ERROR:
  1159. if (!atmci_test_and_clear_pending(host,
  1160. EVENT_XFER_COMPLETE))
  1161. break;
  1162. state = STATE_DATA_BUSY;
  1163. break;
  1164. }
  1165. } while (state != prev_state);
  1166. host->state = state;
  1167. unlock:
  1168. spin_unlock(&host->lock);
  1169. }
  1170. static void atmci_read_data_pio(struct atmel_mci *host)
  1171. {
  1172. struct scatterlist *sg = host->sg;
  1173. void *buf = sg_virt(sg);
  1174. unsigned int offset = host->pio_offset;
  1175. struct mmc_data *data = host->data;
  1176. u32 value;
  1177. u32 status;
  1178. unsigned int nbytes = 0;
  1179. do {
  1180. value = atmci_readl(host, ATMCI_RDR);
  1181. if (likely(offset + 4 <= sg->length)) {
  1182. put_unaligned(value, (u32 *)(buf + offset));
  1183. offset += 4;
  1184. nbytes += 4;
  1185. if (offset == sg->length) {
  1186. flush_dcache_page(sg_page(sg));
  1187. host->sg = sg = sg_next(sg);
  1188. if (!sg)
  1189. goto done;
  1190. offset = 0;
  1191. buf = sg_virt(sg);
  1192. }
  1193. } else {
  1194. unsigned int remaining = sg->length - offset;
  1195. memcpy(buf + offset, &value, remaining);
  1196. nbytes += remaining;
  1197. flush_dcache_page(sg_page(sg));
  1198. host->sg = sg = sg_next(sg);
  1199. if (!sg)
  1200. goto done;
  1201. offset = 4 - remaining;
  1202. buf = sg_virt(sg);
  1203. memcpy(buf, (u8 *)&value + remaining, offset);
  1204. nbytes += offset;
  1205. }
  1206. status = atmci_readl(host, ATMCI_SR);
  1207. if (status & ATMCI_DATA_ERROR_FLAGS) {
  1208. atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
  1209. | ATMCI_DATA_ERROR_FLAGS));
  1210. host->data_status = status;
  1211. data->bytes_xfered += nbytes;
  1212. smp_wmb();
  1213. atmci_set_pending(host, EVENT_DATA_ERROR);
  1214. tasklet_schedule(&host->tasklet);
  1215. return;
  1216. }
  1217. } while (status & ATMCI_RXRDY);
  1218. host->pio_offset = offset;
  1219. data->bytes_xfered += nbytes;
  1220. return;
  1221. done:
  1222. atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
  1223. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1224. data->bytes_xfered += nbytes;
  1225. smp_wmb();
  1226. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  1227. }
  1228. static void atmci_write_data_pio(struct atmel_mci *host)
  1229. {
  1230. struct scatterlist *sg = host->sg;
  1231. void *buf = sg_virt(sg);
  1232. unsigned int offset = host->pio_offset;
  1233. struct mmc_data *data = host->data;
  1234. u32 value;
  1235. u32 status;
  1236. unsigned int nbytes = 0;
  1237. do {
  1238. if (likely(offset + 4 <= sg->length)) {
  1239. value = get_unaligned((u32 *)(buf + offset));
  1240. atmci_writel(host, ATMCI_TDR, value);
  1241. offset += 4;
  1242. nbytes += 4;
  1243. if (offset == sg->length) {
  1244. host->sg = sg = sg_next(sg);
  1245. if (!sg)
  1246. goto done;
  1247. offset = 0;
  1248. buf = sg_virt(sg);
  1249. }
  1250. } else {
  1251. unsigned int remaining = sg->length - offset;
  1252. value = 0;
  1253. memcpy(&value, buf + offset, remaining);
  1254. nbytes += remaining;
  1255. host->sg = sg = sg_next(sg);
  1256. if (!sg) {
  1257. atmci_writel(host, ATMCI_TDR, value);
  1258. goto done;
  1259. }
  1260. offset = 4 - remaining;
  1261. buf = sg_virt(sg);
  1262. memcpy((u8 *)&value + remaining, buf, offset);
  1263. atmci_writel(host, ATMCI_TDR, value);
  1264. nbytes += offset;
  1265. }
  1266. status = atmci_readl(host, ATMCI_SR);
  1267. if (status & ATMCI_DATA_ERROR_FLAGS) {
  1268. atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
  1269. | ATMCI_DATA_ERROR_FLAGS));
  1270. host->data_status = status;
  1271. data->bytes_xfered += nbytes;
  1272. smp_wmb();
  1273. atmci_set_pending(host, EVENT_DATA_ERROR);
  1274. tasklet_schedule(&host->tasklet);
  1275. return;
  1276. }
  1277. } while (status & ATMCI_TXRDY);
  1278. host->pio_offset = offset;
  1279. data->bytes_xfered += nbytes;
  1280. return;
  1281. done:
  1282. atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
  1283. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1284. data->bytes_xfered += nbytes;
  1285. smp_wmb();
  1286. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  1287. }
  1288. static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
  1289. {
  1290. atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
  1291. host->cmd_status = status;
  1292. smp_wmb();
  1293. atmci_set_pending(host, EVENT_CMD_COMPLETE);
  1294. tasklet_schedule(&host->tasklet);
  1295. }
  1296. static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
  1297. {
  1298. int i;
  1299. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  1300. struct atmel_mci_slot *slot = host->slot[i];
  1301. if (slot && (status & slot->sdio_irq)) {
  1302. mmc_signal_sdio_irq(slot->mmc);
  1303. }
  1304. }
  1305. }
  1306. static irqreturn_t atmci_interrupt(int irq, void *dev_id)
  1307. {
  1308. struct atmel_mci *host = dev_id;
  1309. u32 status, mask, pending;
  1310. unsigned int pass_count = 0;
  1311. do {
  1312. status = atmci_readl(host, ATMCI_SR);
  1313. mask = atmci_readl(host, ATMCI_IMR);
  1314. pending = status & mask;
  1315. if (!pending)
  1316. break;
  1317. if (pending & ATMCI_DATA_ERROR_FLAGS) {
  1318. atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
  1319. | ATMCI_RXRDY | ATMCI_TXRDY);
  1320. pending &= atmci_readl(host, ATMCI_IMR);
  1321. host->data_status = status;
  1322. smp_wmb();
  1323. atmci_set_pending(host, EVENT_DATA_ERROR);
  1324. tasklet_schedule(&host->tasklet);
  1325. }
  1326. if (pending & ATMCI_NOTBUSY) {
  1327. atmci_writel(host, ATMCI_IDR,
  1328. ATMCI_DATA_ERROR_FLAGS | ATMCI_NOTBUSY);
  1329. if (!host->data_status)
  1330. host->data_status = status;
  1331. smp_wmb();
  1332. atmci_set_pending(host, EVENT_DATA_COMPLETE);
  1333. tasklet_schedule(&host->tasklet);
  1334. }
  1335. if (pending & ATMCI_RXRDY)
  1336. atmci_read_data_pio(host);
  1337. if (pending & ATMCI_TXRDY)
  1338. atmci_write_data_pio(host);
  1339. if (pending & ATMCI_CMDRDY)
  1340. atmci_cmd_interrupt(host, status);
  1341. if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
  1342. atmci_sdio_interrupt(host, status);
  1343. } while (pass_count++ < 5);
  1344. return pass_count ? IRQ_HANDLED : IRQ_NONE;
  1345. }
  1346. static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
  1347. {
  1348. struct atmel_mci_slot *slot = dev_id;
  1349. /*
  1350. * Disable interrupts until the pin has stabilized and check
  1351. * the state then. Use mod_timer() since we may be in the
  1352. * middle of the timer routine when this interrupt triggers.
  1353. */
  1354. disable_irq_nosync(irq);
  1355. mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
  1356. return IRQ_HANDLED;
  1357. }
  1358. static int __init atmci_init_slot(struct atmel_mci *host,
  1359. struct mci_slot_pdata *slot_data, unsigned int id,
  1360. u32 sdc_reg, u32 sdio_irq)
  1361. {
  1362. struct mmc_host *mmc;
  1363. struct atmel_mci_slot *slot;
  1364. mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
  1365. if (!mmc)
  1366. return -ENOMEM;
  1367. slot = mmc_priv(mmc);
  1368. slot->mmc = mmc;
  1369. slot->host = host;
  1370. slot->detect_pin = slot_data->detect_pin;
  1371. slot->wp_pin = slot_data->wp_pin;
  1372. slot->detect_is_active_high = slot_data->detect_is_active_high;
  1373. slot->sdc_reg = sdc_reg;
  1374. slot->sdio_irq = sdio_irq;
  1375. mmc->ops = &atmci_ops;
  1376. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
  1377. mmc->f_max = host->bus_hz / 2;
  1378. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1379. if (sdio_irq)
  1380. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1381. if (atmci_is_mci2())
  1382. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1383. if (slot_data->bus_width >= 4)
  1384. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1385. mmc->max_segs = 64;
  1386. mmc->max_req_size = 32768 * 512;
  1387. mmc->max_blk_size = 32768;
  1388. mmc->max_blk_count = 512;
  1389. /* Assume card is present initially */
  1390. set_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1391. if (gpio_is_valid(slot->detect_pin)) {
  1392. if (gpio_request(slot->detect_pin, "mmc_detect")) {
  1393. dev_dbg(&mmc->class_dev, "no detect pin available\n");
  1394. slot->detect_pin = -EBUSY;
  1395. } else if (gpio_get_value(slot->detect_pin) ^
  1396. slot->detect_is_active_high) {
  1397. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1398. }
  1399. }
  1400. if (!gpio_is_valid(slot->detect_pin))
  1401. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1402. if (gpio_is_valid(slot->wp_pin)) {
  1403. if (gpio_request(slot->wp_pin, "mmc_wp")) {
  1404. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  1405. slot->wp_pin = -EBUSY;
  1406. }
  1407. }
  1408. host->slot[id] = slot;
  1409. mmc_add_host(mmc);
  1410. if (gpio_is_valid(slot->detect_pin)) {
  1411. int ret;
  1412. setup_timer(&slot->detect_timer, atmci_detect_change,
  1413. (unsigned long)slot);
  1414. ret = request_irq(gpio_to_irq(slot->detect_pin),
  1415. atmci_detect_interrupt,
  1416. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  1417. "mmc-detect", slot);
  1418. if (ret) {
  1419. dev_dbg(&mmc->class_dev,
  1420. "could not request IRQ %d for detect pin\n",
  1421. gpio_to_irq(slot->detect_pin));
  1422. gpio_free(slot->detect_pin);
  1423. slot->detect_pin = -EBUSY;
  1424. }
  1425. }
  1426. atmci_init_debugfs(slot);
  1427. return 0;
  1428. }
  1429. static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
  1430. unsigned int id)
  1431. {
  1432. /* Debugfs stuff is cleaned up by mmc core */
  1433. set_bit(ATMCI_SHUTDOWN, &slot->flags);
  1434. smp_wmb();
  1435. mmc_remove_host(slot->mmc);
  1436. if (gpio_is_valid(slot->detect_pin)) {
  1437. int pin = slot->detect_pin;
  1438. free_irq(gpio_to_irq(pin), slot);
  1439. del_timer_sync(&slot->detect_timer);
  1440. gpio_free(pin);
  1441. }
  1442. if (gpio_is_valid(slot->wp_pin))
  1443. gpio_free(slot->wp_pin);
  1444. slot->host->slot[id] = NULL;
  1445. mmc_free_host(slot->mmc);
  1446. }
  1447. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1448. static bool atmci_filter(struct dma_chan *chan, void *slave)
  1449. {
  1450. struct mci_dma_data *sl = slave;
  1451. if (sl && find_slave_dev(sl) == chan->device->dev) {
  1452. chan->private = slave_data_ptr(sl);
  1453. return true;
  1454. } else {
  1455. return false;
  1456. }
  1457. }
  1458. static void atmci_configure_dma(struct atmel_mci *host)
  1459. {
  1460. struct mci_platform_data *pdata;
  1461. if (host == NULL)
  1462. return;
  1463. pdata = host->pdev->dev.platform_data;
  1464. if (pdata && find_slave_dev(pdata->dma_slave)) {
  1465. dma_cap_mask_t mask;
  1466. setup_dma_addr(pdata->dma_slave,
  1467. host->mapbase + ATMCI_TDR,
  1468. host->mapbase + ATMCI_RDR);
  1469. /* Try to grab a DMA channel */
  1470. dma_cap_zero(mask);
  1471. dma_cap_set(DMA_SLAVE, mask);
  1472. host->dma.chan =
  1473. dma_request_channel(mask, atmci_filter, pdata->dma_slave);
  1474. }
  1475. if (!host->dma.chan)
  1476. dev_notice(&host->pdev->dev, "DMA not available, using PIO\n");
  1477. else
  1478. dev_info(&host->pdev->dev,
  1479. "Using %s for DMA transfers\n",
  1480. dma_chan_name(host->dma.chan));
  1481. }
  1482. #else
  1483. static void atmci_configure_dma(struct atmel_mci *host) {}
  1484. #endif
  1485. static int __init atmci_probe(struct platform_device *pdev)
  1486. {
  1487. struct mci_platform_data *pdata;
  1488. struct atmel_mci *host;
  1489. struct resource *regs;
  1490. unsigned int nr_slots;
  1491. int irq;
  1492. int ret;
  1493. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1494. if (!regs)
  1495. return -ENXIO;
  1496. pdata = pdev->dev.platform_data;
  1497. if (!pdata)
  1498. return -ENXIO;
  1499. irq = platform_get_irq(pdev, 0);
  1500. if (irq < 0)
  1501. return irq;
  1502. host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
  1503. if (!host)
  1504. return -ENOMEM;
  1505. host->pdev = pdev;
  1506. spin_lock_init(&host->lock);
  1507. INIT_LIST_HEAD(&host->queue);
  1508. host->mck = clk_get(&pdev->dev, "mci_clk");
  1509. if (IS_ERR(host->mck)) {
  1510. ret = PTR_ERR(host->mck);
  1511. goto err_clk_get;
  1512. }
  1513. ret = -ENOMEM;
  1514. host->regs = ioremap(regs->start, resource_size(regs));
  1515. if (!host->regs)
  1516. goto err_ioremap;
  1517. clk_enable(host->mck);
  1518. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  1519. host->bus_hz = clk_get_rate(host->mck);
  1520. clk_disable(host->mck);
  1521. host->mapbase = regs->start;
  1522. tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
  1523. ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
  1524. if (ret)
  1525. goto err_request_irq;
  1526. atmci_configure_dma(host);
  1527. platform_set_drvdata(pdev, host);
  1528. /* We need at least one slot to succeed */
  1529. nr_slots = 0;
  1530. ret = -ENODEV;
  1531. if (pdata->slot[0].bus_width) {
  1532. ret = atmci_init_slot(host, &pdata->slot[0],
  1533. 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
  1534. if (!ret)
  1535. nr_slots++;
  1536. }
  1537. if (pdata->slot[1].bus_width) {
  1538. ret = atmci_init_slot(host, &pdata->slot[1],
  1539. 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
  1540. if (!ret)
  1541. nr_slots++;
  1542. }
  1543. if (!nr_slots) {
  1544. dev_err(&pdev->dev, "init failed: no slot defined\n");
  1545. goto err_init_slot;
  1546. }
  1547. dev_info(&pdev->dev,
  1548. "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
  1549. host->mapbase, irq, nr_slots);
  1550. return 0;
  1551. err_init_slot:
  1552. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1553. if (host->dma.chan)
  1554. dma_release_channel(host->dma.chan);
  1555. #endif
  1556. free_irq(irq, host);
  1557. err_request_irq:
  1558. iounmap(host->regs);
  1559. err_ioremap:
  1560. clk_put(host->mck);
  1561. err_clk_get:
  1562. kfree(host);
  1563. return ret;
  1564. }
  1565. static int __exit atmci_remove(struct platform_device *pdev)
  1566. {
  1567. struct atmel_mci *host = platform_get_drvdata(pdev);
  1568. unsigned int i;
  1569. platform_set_drvdata(pdev, NULL);
  1570. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  1571. if (host->slot[i])
  1572. atmci_cleanup_slot(host->slot[i], i);
  1573. }
  1574. clk_enable(host->mck);
  1575. atmci_writel(host, ATMCI_IDR, ~0UL);
  1576. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
  1577. atmci_readl(host, ATMCI_SR);
  1578. clk_disable(host->mck);
  1579. #ifdef CONFIG_MMC_ATMELMCI_DMA
  1580. if (host->dma.chan)
  1581. dma_release_channel(host->dma.chan);
  1582. #endif
  1583. free_irq(platform_get_irq(pdev, 0), host);
  1584. iounmap(host->regs);
  1585. clk_put(host->mck);
  1586. kfree(host);
  1587. return 0;
  1588. }
  1589. #ifdef CONFIG_PM
  1590. static int atmci_suspend(struct device *dev)
  1591. {
  1592. struct atmel_mci *host = dev_get_drvdata(dev);
  1593. int i;
  1594. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  1595. struct atmel_mci_slot *slot = host->slot[i];
  1596. int ret;
  1597. if (!slot)
  1598. continue;
  1599. ret = mmc_suspend_host(slot->mmc);
  1600. if (ret < 0) {
  1601. while (--i >= 0) {
  1602. slot = host->slot[i];
  1603. if (slot
  1604. && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
  1605. mmc_resume_host(host->slot[i]->mmc);
  1606. clear_bit(ATMCI_SUSPENDED, &slot->flags);
  1607. }
  1608. }
  1609. return ret;
  1610. } else {
  1611. set_bit(ATMCI_SUSPENDED, &slot->flags);
  1612. }
  1613. }
  1614. return 0;
  1615. }
  1616. static int atmci_resume(struct device *dev)
  1617. {
  1618. struct atmel_mci *host = dev_get_drvdata(dev);
  1619. int i;
  1620. int ret = 0;
  1621. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  1622. struct atmel_mci_slot *slot = host->slot[i];
  1623. int err;
  1624. slot = host->slot[i];
  1625. if (!slot)
  1626. continue;
  1627. if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
  1628. continue;
  1629. err = mmc_resume_host(slot->mmc);
  1630. if (err < 0)
  1631. ret = err;
  1632. else
  1633. clear_bit(ATMCI_SUSPENDED, &slot->flags);
  1634. }
  1635. return ret;
  1636. }
  1637. static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
  1638. #define ATMCI_PM_OPS (&atmci_pm)
  1639. #else
  1640. #define ATMCI_PM_OPS NULL
  1641. #endif
  1642. static struct platform_driver atmci_driver = {
  1643. .remove = __exit_p(atmci_remove),
  1644. .driver = {
  1645. .name = "atmel_mci",
  1646. .pm = ATMCI_PM_OPS,
  1647. },
  1648. };
  1649. static int __init atmci_init(void)
  1650. {
  1651. return platform_driver_probe(&atmci_driver, atmci_probe);
  1652. }
  1653. static void __exit atmci_exit(void)
  1654. {
  1655. platform_driver_unregister(&atmci_driver);
  1656. }
  1657. late_initcall(atmci_init); /* try to load after dma driver when built-in */
  1658. module_exit(atmci_exit);
  1659. MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
  1660. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1661. MODULE_LICENSE("GPL v2");