atmel-mci.c 57 KB

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