atmel-mci.c 63 KB

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