atmel-mci.c 43 KB

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