atmel-mci.c 45 KB

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