atmel-mci.c 67 KB

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