atmel-mci.c 67 KB

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