atmel-mci.c 45 KB

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