atmel-mci.c 56 KB

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