atmel-mci.c 43 KB

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