atmel-mci.c 67 KB

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