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/io.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/slab.h>
  30. #include <linux/stat.h>
  31. #include <linux/types.h>
  32. #include <linux/platform_data/atmel.h>
  33. #include <linux/mmc/host.h>
  34. #include <linux/mmc/sdio.h>
  35. #include <mach/atmel-mci.h>
  36. #include <linux/atmel-mci.h>
  37. #include <linux/atmel_pdc.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/io.h>
  40. #include <asm/unaligned.h>
  41. #include "atmel-mci-regs.h"
  42. #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
  43. #define ATMCI_DMA_THRESHOLD 16
  44. enum {
  45. EVENT_CMD_RDY = 0,
  46. EVENT_XFER_COMPLETE,
  47. EVENT_NOTBUSY,
  48. EVENT_DATA_ERROR,
  49. };
  50. enum atmel_mci_state {
  51. STATE_IDLE = 0,
  52. STATE_SENDING_CMD,
  53. STATE_DATA_XFER,
  54. STATE_WAITING_NOTBUSY,
  55. STATE_SENDING_STOP,
  56. STATE_END_REQUEST,
  57. };
  58. enum atmci_xfer_dir {
  59. XFER_RECEIVE = 0,
  60. XFER_TRANSMIT,
  61. };
  62. enum atmci_pdc_buf {
  63. PDC_FIRST_BUF = 0,
  64. PDC_SECOND_BUF,
  65. };
  66. struct atmel_mci_caps {
  67. bool has_dma_conf_reg;
  68. bool has_pdc;
  69. bool has_cfg_reg;
  70. bool has_cstor_reg;
  71. bool has_highspeed;
  72. bool has_rwproof;
  73. bool has_odd_clk_div;
  74. bool has_bad_data_ordering;
  75. bool need_reset_after_xfer;
  76. bool need_blksz_mul_4;
  77. bool need_notbusy_for_read_ops;
  78. };
  79. struct atmel_mci_dma {
  80. struct dma_chan *chan;
  81. struct dma_async_tx_descriptor *data_desc;
  82. };
  83. /**
  84. * struct atmel_mci - MMC controller state shared between all slots
  85. * @lock: Spinlock protecting the queue and associated data.
  86. * @regs: Pointer to MMIO registers.
  87. * @sg: Scatterlist entry currently being processed by PIO or PDC code.
  88. * @pio_offset: Offset into the current scatterlist entry.
  89. * @buffer: Buffer used if we don't have the r/w proof capability. We
  90. * don't have the time to switch pdc buffers so we have to use only
  91. * one buffer for the full transaction.
  92. * @buf_size: size of the buffer.
  93. * @phys_buf_addr: buffer address needed for pdc.
  94. * @cur_slot: The slot which is currently using the controller.
  95. * @mrq: The request currently being processed on @cur_slot,
  96. * or NULL if the controller is idle.
  97. * @cmd: The command currently being sent to the card, or NULL.
  98. * @data: The data currently being transferred, or NULL if no data
  99. * transfer is in progress.
  100. * @data_size: just data->blocks * data->blksz.
  101. * @dma: DMA client state.
  102. * @data_chan: DMA channel being used for the current data transfer.
  103. * @cmd_status: Snapshot of SR taken upon completion of the current
  104. * command. Only valid when EVENT_CMD_COMPLETE is pending.
  105. * @data_status: Snapshot of SR taken upon completion of the current
  106. * data transfer. Only valid when EVENT_DATA_COMPLETE or
  107. * EVENT_DATA_ERROR is pending.
  108. * @stop_cmdr: Value to be loaded into CMDR when the stop command is
  109. * to be sent.
  110. * @tasklet: Tasklet running the request state machine.
  111. * @pending_events: Bitmask of events flagged by the interrupt handler
  112. * to be processed by the tasklet.
  113. * @completed_events: Bitmask of events which the state machine has
  114. * processed.
  115. * @state: Tasklet state.
  116. * @queue: List of slots waiting for access to the controller.
  117. * @need_clock_update: Update the clock rate before the next request.
  118. * @need_reset: Reset controller before next request.
  119. * @timer: Timer to balance the data timeout error flag which cannot rise.
  120. * @mode_reg: Value of the MR register.
  121. * @cfg_reg: Value of the CFG register.
  122. * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
  123. * rate and timeout calculations.
  124. * @mapbase: Physical address of the MMIO registers.
  125. * @mck: The peripheral bus clock hooked up to the MMC controller.
  126. * @pdev: Platform device associated with the MMC controller.
  127. * @slot: Slots sharing this MMC controller.
  128. * @caps: MCI capabilities depending on MCI version.
  129. * @prepare_data: function to setup MCI before data transfer which
  130. * depends on MCI capabilities.
  131. * @submit_data: function to start data transfer which depends on MCI
  132. * capabilities.
  133. * @stop_transfer: function to stop data transfer which depends on MCI
  134. * capabilities.
  135. *
  136. * Locking
  137. * =======
  138. *
  139. * @lock is a softirq-safe spinlock protecting @queue as well as
  140. * @cur_slot, @mrq and @state. These must always be updated
  141. * at the same time while holding @lock.
  142. *
  143. * @lock also protects mode_reg and need_clock_update since these are
  144. * used to synchronize mode register updates with the queue
  145. * processing.
  146. *
  147. * The @mrq field of struct atmel_mci_slot is also protected by @lock,
  148. * and must always be written at the same time as the slot is added to
  149. * @queue.
  150. *
  151. * @pending_events and @completed_events are accessed using atomic bit
  152. * operations, so they don't need any locking.
  153. *
  154. * None of the fields touched by the interrupt handler need any
  155. * locking. However, ordering is important: Before EVENT_DATA_ERROR or
  156. * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
  157. * interrupts must be disabled and @data_status updated with a
  158. * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
  159. * CMDRDY interrupt must be disabled and @cmd_status updated with a
  160. * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
  161. * bytes_xfered field of @data must be written. This is ensured by
  162. * using barriers.
  163. */
  164. struct atmel_mci {
  165. spinlock_t lock;
  166. void __iomem *regs;
  167. struct scatterlist *sg;
  168. unsigned int sg_len;
  169. unsigned int pio_offset;
  170. unsigned int *buffer;
  171. unsigned int buf_size;
  172. dma_addr_t buf_phys_addr;
  173. struct atmel_mci_slot *cur_slot;
  174. struct mmc_request *mrq;
  175. struct mmc_command *cmd;
  176. struct mmc_data *data;
  177. unsigned int data_size;
  178. struct atmel_mci_dma dma;
  179. struct dma_chan *data_chan;
  180. struct dma_slave_config dma_conf;
  181. u32 cmd_status;
  182. u32 data_status;
  183. u32 stop_cmdr;
  184. struct tasklet_struct tasklet;
  185. unsigned long pending_events;
  186. unsigned long completed_events;
  187. enum atmel_mci_state state;
  188. struct list_head queue;
  189. bool need_clock_update;
  190. bool need_reset;
  191. struct timer_list timer;
  192. u32 mode_reg;
  193. u32 cfg_reg;
  194. unsigned long bus_hz;
  195. unsigned long mapbase;
  196. struct clk *mck;
  197. struct platform_device *pdev;
  198. struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
  199. struct atmel_mci_caps caps;
  200. u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
  201. void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
  202. void (*stop_transfer)(struct atmel_mci *host);
  203. };
  204. /**
  205. * struct atmel_mci_slot - MMC slot state
  206. * @mmc: The mmc_host representing this slot.
  207. * @host: The MMC controller this slot is using.
  208. * @sdc_reg: Value of SDCR to be written before using this slot.
  209. * @sdio_irq: SDIO irq mask for this slot.
  210. * @mrq: mmc_request currently being processed or waiting to be
  211. * processed, or NULL when the slot is idle.
  212. * @queue_node: List node for placing this node in the @queue list of
  213. * &struct atmel_mci.
  214. * @clock: Clock rate configured by set_ios(). Protected by host->lock.
  215. * @flags: Random state bits associated with the slot.
  216. * @detect_pin: GPIO pin used for card detection, or negative if not
  217. * available.
  218. * @wp_pin: GPIO pin used for card write protect sending, or negative
  219. * if not available.
  220. * @detect_is_active_high: The state of the detect pin when it is active.
  221. * @detect_timer: Timer used for debouncing @detect_pin interrupts.
  222. */
  223. struct atmel_mci_slot {
  224. struct mmc_host *mmc;
  225. struct atmel_mci *host;
  226. u32 sdc_reg;
  227. u32 sdio_irq;
  228. struct mmc_request *mrq;
  229. struct list_head queue_node;
  230. unsigned int clock;
  231. unsigned long flags;
  232. #define ATMCI_CARD_PRESENT 0
  233. #define ATMCI_CARD_NEED_INIT 1
  234. #define ATMCI_SHUTDOWN 2
  235. int detect_pin;
  236. int wp_pin;
  237. bool detect_is_active_high;
  238. struct timer_list detect_timer;
  239. };
  240. #define atmci_test_and_clear_pending(host, event) \
  241. test_and_clear_bit(event, &host->pending_events)
  242. #define atmci_set_completed(host, event) \
  243. set_bit(event, &host->completed_events)
  244. #define atmci_set_pending(host, event) \
  245. set_bit(event, &host->pending_events)
  246. /*
  247. * The debugfs stuff below is mostly optimized away when
  248. * CONFIG_DEBUG_FS is not set.
  249. */
  250. static int atmci_req_show(struct seq_file *s, void *v)
  251. {
  252. struct atmel_mci_slot *slot = s->private;
  253. struct mmc_request *mrq;
  254. struct mmc_command *cmd;
  255. struct mmc_command *stop;
  256. struct mmc_data *data;
  257. /* Make sure we get a consistent snapshot */
  258. spin_lock_bh(&slot->host->lock);
  259. mrq = slot->mrq;
  260. if (mrq) {
  261. cmd = mrq->cmd;
  262. data = mrq->data;
  263. stop = mrq->stop;
  264. if (cmd)
  265. seq_printf(s,
  266. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  267. cmd->opcode, cmd->arg, cmd->flags,
  268. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  269. cmd->resp[3], cmd->error);
  270. if (data)
  271. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  272. data->bytes_xfered, data->blocks,
  273. data->blksz, data->flags, data->error);
  274. if (stop)
  275. seq_printf(s,
  276. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  277. stop->opcode, stop->arg, stop->flags,
  278. stop->resp[0], stop->resp[1], stop->resp[2],
  279. stop->resp[3], stop->error);
  280. }
  281. spin_unlock_bh(&slot->host->lock);
  282. return 0;
  283. }
  284. static int atmci_req_open(struct inode *inode, struct file *file)
  285. {
  286. return single_open(file, atmci_req_show, inode->i_private);
  287. }
  288. static const struct file_operations atmci_req_fops = {
  289. .owner = THIS_MODULE,
  290. .open = atmci_req_open,
  291. .read = seq_read,
  292. .llseek = seq_lseek,
  293. .release = single_release,
  294. };
  295. static void atmci_show_status_reg(struct seq_file *s,
  296. const char *regname, u32 value)
  297. {
  298. static const char *sr_bit[] = {
  299. [0] = "CMDRDY",
  300. [1] = "RXRDY",
  301. [2] = "TXRDY",
  302. [3] = "BLKE",
  303. [4] = "DTIP",
  304. [5] = "NOTBUSY",
  305. [6] = "ENDRX",
  306. [7] = "ENDTX",
  307. [8] = "SDIOIRQA",
  308. [9] = "SDIOIRQB",
  309. [12] = "SDIOWAIT",
  310. [14] = "RXBUFF",
  311. [15] = "TXBUFE",
  312. [16] = "RINDE",
  313. [17] = "RDIRE",
  314. [18] = "RCRCE",
  315. [19] = "RENDE",
  316. [20] = "RTOE",
  317. [21] = "DCRCE",
  318. [22] = "DTOE",
  319. [23] = "CSTOE",
  320. [24] = "BLKOVRE",
  321. [25] = "DMADONE",
  322. [26] = "FIFOEMPTY",
  323. [27] = "XFRDONE",
  324. [30] = "OVRE",
  325. [31] = "UNRE",
  326. };
  327. unsigned int i;
  328. seq_printf(s, "%s:\t0x%08x", regname, value);
  329. for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
  330. if (value & (1 << i)) {
  331. if (sr_bit[i])
  332. seq_printf(s, " %s", sr_bit[i]);
  333. else
  334. seq_puts(s, " UNKNOWN");
  335. }
  336. }
  337. seq_putc(s, '\n');
  338. }
  339. static int atmci_regs_show(struct seq_file *s, void *v)
  340. {
  341. struct atmel_mci *host = s->private;
  342. u32 *buf;
  343. int ret = 0;
  344. buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
  345. if (!buf)
  346. return -ENOMEM;
  347. /*
  348. * Grab a more or less consistent snapshot. Note that we're
  349. * not disabling interrupts, so IMR and SR may not be
  350. * consistent.
  351. */
  352. ret = clk_prepare_enable(host->mck);
  353. if (ret)
  354. goto out;
  355. spin_lock_bh(&host->lock);
  356. memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
  357. spin_unlock_bh(&host->lock);
  358. clk_disable_unprepare(host->mck);
  359. seq_printf(s, "MR:\t0x%08x%s%s ",
  360. buf[ATMCI_MR / 4],
  361. buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
  362. buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
  363. if (host->caps.has_odd_clk_div)
  364. seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
  365. ((buf[ATMCI_MR / 4] & 0xff) << 1)
  366. | ((buf[ATMCI_MR / 4] >> 16) & 1));
  367. else
  368. seq_printf(s, "CLKDIV=%u\n",
  369. (buf[ATMCI_MR / 4] & 0xff));
  370. seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
  371. seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
  372. seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
  373. seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
  374. buf[ATMCI_BLKR / 4],
  375. buf[ATMCI_BLKR / 4] & 0xffff,
  376. (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
  377. if (host->caps.has_cstor_reg)
  378. seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
  379. /* Don't read RSPR and RDR; it will consume the data there */
  380. atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
  381. atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
  382. if (host->caps.has_dma_conf_reg) {
  383. u32 val;
  384. val = buf[ATMCI_DMA / 4];
  385. seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
  386. val, val & 3,
  387. ((val >> 4) & 3) ?
  388. 1 << (((val >> 4) & 3) + 1) : 1,
  389. val & ATMCI_DMAEN ? " DMAEN" : "");
  390. }
  391. if (host->caps.has_cfg_reg) {
  392. u32 val;
  393. val = buf[ATMCI_CFG / 4];
  394. seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
  395. val,
  396. val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
  397. val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
  398. val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
  399. val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
  400. }
  401. out:
  402. kfree(buf);
  403. return ret;
  404. }
  405. static int atmci_regs_open(struct inode *inode, struct file *file)
  406. {
  407. return single_open(file, atmci_regs_show, inode->i_private);
  408. }
  409. static const struct file_operations atmci_regs_fops = {
  410. .owner = THIS_MODULE,
  411. .open = atmci_regs_open,
  412. .read = seq_read,
  413. .llseek = seq_lseek,
  414. .release = single_release,
  415. };
  416. static void atmci_init_debugfs(struct atmel_mci_slot *slot)
  417. {
  418. struct mmc_host *mmc = slot->mmc;
  419. struct atmel_mci *host = slot->host;
  420. struct dentry *root;
  421. struct dentry *node;
  422. root = mmc->debugfs_root;
  423. if (!root)
  424. return;
  425. node = debugfs_create_file("regs", S_IRUSR, root, host,
  426. &atmci_regs_fops);
  427. if (IS_ERR(node))
  428. return;
  429. if (!node)
  430. goto err;
  431. node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
  432. if (!node)
  433. goto err;
  434. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  435. if (!node)
  436. goto err;
  437. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  438. (u32 *)&host->pending_events);
  439. if (!node)
  440. goto err;
  441. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  442. (u32 *)&host->completed_events);
  443. if (!node)
  444. goto err;
  445. return;
  446. err:
  447. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  448. }
  449. #if defined(CONFIG_OF)
  450. static const struct of_device_id atmci_dt_ids[] = {
  451. { .compatible = "atmel,hsmci" },
  452. { /* sentinel */ }
  453. };
  454. MODULE_DEVICE_TABLE(of, atmci_dt_ids);
  455. static struct mci_platform_data*
  456. atmci_of_init(struct platform_device *pdev)
  457. {
  458. struct device_node *np = pdev->dev.of_node;
  459. struct device_node *cnp;
  460. struct mci_platform_data *pdata;
  461. u32 slot_id;
  462. if (!np) {
  463. dev_err(&pdev->dev, "device node not found\n");
  464. return ERR_PTR(-EINVAL);
  465. }
  466. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  467. if (!pdata) {
  468. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  469. return ERR_PTR(-ENOMEM);
  470. }
  471. for_each_child_of_node(np, cnp) {
  472. if (of_property_read_u32(cnp, "reg", &slot_id)) {
  473. dev_warn(&pdev->dev, "reg property is missing for %s\n",
  474. cnp->full_name);
  475. continue;
  476. }
  477. if (slot_id >= ATMCI_MAX_NR_SLOTS) {
  478. dev_warn(&pdev->dev, "can't have more than %d slots\n",
  479. ATMCI_MAX_NR_SLOTS);
  480. break;
  481. }
  482. if (of_property_read_u32(cnp, "bus-width",
  483. &pdata->slot[slot_id].bus_width))
  484. pdata->slot[slot_id].bus_width = 1;
  485. pdata->slot[slot_id].detect_pin =
  486. of_get_named_gpio(cnp, "cd-gpios", 0);
  487. pdata->slot[slot_id].detect_is_active_high =
  488. of_property_read_bool(cnp, "cd-inverted");
  489. pdata->slot[slot_id].wp_pin =
  490. of_get_named_gpio(cnp, "wp-gpios", 0);
  491. }
  492. return pdata;
  493. }
  494. #else /* CONFIG_OF */
  495. static inline struct mci_platform_data*
  496. atmci_of_init(struct platform_device *dev)
  497. {
  498. return ERR_PTR(-EINVAL);
  499. }
  500. #endif
  501. static inline unsigned int atmci_get_version(struct atmel_mci *host)
  502. {
  503. return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
  504. }
  505. static void atmci_timeout_timer(unsigned long data)
  506. {
  507. struct atmel_mci *host;
  508. host = (struct atmel_mci *)data;
  509. dev_dbg(&host->pdev->dev, "software timeout\n");
  510. if (host->mrq->cmd->data) {
  511. host->mrq->cmd->data->error = -ETIMEDOUT;
  512. host->data = NULL;
  513. /*
  514. * With some SDIO modules, sometimes DMA transfer hangs. If
  515. * stop_transfer() is not called then the DMA request is not
  516. * removed, following ones are queued and never computed.
  517. */
  518. if (host->state == STATE_DATA_XFER)
  519. host->stop_transfer(host);
  520. } else {
  521. host->mrq->cmd->error = -ETIMEDOUT;
  522. host->cmd = NULL;
  523. }
  524. host->need_reset = 1;
  525. host->state = STATE_END_REQUEST;
  526. smp_wmb();
  527. tasklet_schedule(&host->tasklet);
  528. }
  529. static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
  530. unsigned int ns)
  531. {
  532. /*
  533. * It is easier here to use us instead of ns for the timeout,
  534. * it prevents from overflows during calculation.
  535. */
  536. unsigned int us = DIV_ROUND_UP(ns, 1000);
  537. /* Maximum clock frequency is host->bus_hz/2 */
  538. return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
  539. }
  540. static void atmci_set_timeout(struct atmel_mci *host,
  541. struct atmel_mci_slot *slot, struct mmc_data *data)
  542. {
  543. static unsigned dtomul_to_shift[] = {
  544. 0, 4, 7, 8, 10, 12, 16, 20
  545. };
  546. unsigned timeout;
  547. unsigned dtocyc;
  548. unsigned dtomul;
  549. timeout = atmci_ns_to_clocks(host, data->timeout_ns)
  550. + data->timeout_clks;
  551. for (dtomul = 0; dtomul < 8; dtomul++) {
  552. unsigned shift = dtomul_to_shift[dtomul];
  553. dtocyc = (timeout + (1 << shift) - 1) >> shift;
  554. if (dtocyc < 15)
  555. break;
  556. }
  557. if (dtomul >= 8) {
  558. dtomul = 7;
  559. dtocyc = 15;
  560. }
  561. dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
  562. dtocyc << dtomul_to_shift[dtomul]);
  563. atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
  564. }
  565. /*
  566. * Return mask with command flags to be enabled for this command.
  567. */
  568. static u32 atmci_prepare_command(struct mmc_host *mmc,
  569. struct mmc_command *cmd)
  570. {
  571. struct mmc_data *data;
  572. u32 cmdr;
  573. cmd->error = -EINPROGRESS;
  574. cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
  575. if (cmd->flags & MMC_RSP_PRESENT) {
  576. if (cmd->flags & MMC_RSP_136)
  577. cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
  578. else
  579. cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
  580. }
  581. /*
  582. * This should really be MAXLAT_5 for CMD2 and ACMD41, but
  583. * it's too difficult to determine whether this is an ACMD or
  584. * not. Better make it 64.
  585. */
  586. cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
  587. if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
  588. cmdr |= ATMCI_CMDR_OPDCMD;
  589. data = cmd->data;
  590. if (data) {
  591. cmdr |= ATMCI_CMDR_START_XFER;
  592. if (cmd->opcode == SD_IO_RW_EXTENDED) {
  593. cmdr |= ATMCI_CMDR_SDIO_BLOCK;
  594. } else {
  595. if (data->flags & MMC_DATA_STREAM)
  596. cmdr |= ATMCI_CMDR_STREAM;
  597. else if (data->blocks > 1)
  598. cmdr |= ATMCI_CMDR_MULTI_BLOCK;
  599. else
  600. cmdr |= ATMCI_CMDR_BLOCK;
  601. }
  602. if (data->flags & MMC_DATA_READ)
  603. cmdr |= ATMCI_CMDR_TRDIR_READ;
  604. }
  605. return cmdr;
  606. }
  607. static void atmci_send_command(struct atmel_mci *host,
  608. struct mmc_command *cmd, u32 cmd_flags)
  609. {
  610. WARN_ON(host->cmd);
  611. host->cmd = cmd;
  612. dev_vdbg(&host->pdev->dev,
  613. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  614. cmd->arg, cmd_flags);
  615. atmci_writel(host, ATMCI_ARGR, cmd->arg);
  616. atmci_writel(host, ATMCI_CMDR, cmd_flags);
  617. }
  618. static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
  619. {
  620. dev_dbg(&host->pdev->dev, "send stop command\n");
  621. atmci_send_command(host, data->stop, host->stop_cmdr);
  622. atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
  623. }
  624. /*
  625. * Configure given PDC buffer taking care of alignement issues.
  626. * Update host->data_size and host->sg.
  627. */
  628. static void atmci_pdc_set_single_buf(struct atmel_mci *host,
  629. enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
  630. {
  631. u32 pointer_reg, counter_reg;
  632. unsigned int buf_size;
  633. if (dir == XFER_RECEIVE) {
  634. pointer_reg = ATMEL_PDC_RPR;
  635. counter_reg = ATMEL_PDC_RCR;
  636. } else {
  637. pointer_reg = ATMEL_PDC_TPR;
  638. counter_reg = ATMEL_PDC_TCR;
  639. }
  640. if (buf_nb == PDC_SECOND_BUF) {
  641. pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
  642. counter_reg += ATMEL_PDC_SCND_BUF_OFF;
  643. }
  644. if (!host->caps.has_rwproof) {
  645. buf_size = host->buf_size;
  646. atmci_writel(host, pointer_reg, host->buf_phys_addr);
  647. } else {
  648. buf_size = sg_dma_len(host->sg);
  649. atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
  650. }
  651. if (host->data_size <= buf_size) {
  652. if (host->data_size & 0x3) {
  653. /* If size is different from modulo 4, transfer bytes */
  654. atmci_writel(host, counter_reg, host->data_size);
  655. atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
  656. } else {
  657. /* Else transfer 32-bits words */
  658. atmci_writel(host, counter_reg, host->data_size / 4);
  659. }
  660. host->data_size = 0;
  661. } else {
  662. /* We assume the size of a page is 32-bits aligned */
  663. atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
  664. host->data_size -= sg_dma_len(host->sg);
  665. if (host->data_size)
  666. host->sg = sg_next(host->sg);
  667. }
  668. }
  669. /*
  670. * Configure PDC buffer according to the data size ie configuring one or two
  671. * buffers. Don't use this function if you want to configure only the second
  672. * buffer. In this case, use atmci_pdc_set_single_buf.
  673. */
  674. static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
  675. {
  676. atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
  677. if (host->data_size)
  678. atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
  679. }
  680. /*
  681. * Unmap sg lists, called when transfer is finished.
  682. */
  683. static void atmci_pdc_cleanup(struct atmel_mci *host)
  684. {
  685. struct mmc_data *data = host->data;
  686. if (data)
  687. dma_unmap_sg(&host->pdev->dev,
  688. data->sg, data->sg_len,
  689. ((data->flags & MMC_DATA_WRITE)
  690. ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
  691. }
  692. /*
  693. * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
  694. * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
  695. * interrupt needed for both transfer directions.
  696. */
  697. static void atmci_pdc_complete(struct atmel_mci *host)
  698. {
  699. int transfer_size = host->data->blocks * host->data->blksz;
  700. int i;
  701. atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
  702. if ((!host->caps.has_rwproof)
  703. && (host->data->flags & MMC_DATA_READ)) {
  704. if (host->caps.has_bad_data_ordering)
  705. for (i = 0; i < transfer_size; i++)
  706. host->buffer[i] = swab32(host->buffer[i]);
  707. sg_copy_from_buffer(host->data->sg, host->data->sg_len,
  708. host->buffer, transfer_size);
  709. }
  710. atmci_pdc_cleanup(host);
  711. dev_dbg(&host->pdev->dev, "(%s) set pending xfer complete\n", __func__);
  712. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  713. tasklet_schedule(&host->tasklet);
  714. }
  715. static void atmci_dma_cleanup(struct atmel_mci *host)
  716. {
  717. struct mmc_data *data = host->data;
  718. if (data)
  719. dma_unmap_sg(host->dma.chan->device->dev,
  720. data->sg, data->sg_len,
  721. ((data->flags & MMC_DATA_WRITE)
  722. ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
  723. }
  724. /*
  725. * This function is called by the DMA driver from tasklet context.
  726. */
  727. static void atmci_dma_complete(void *arg)
  728. {
  729. struct atmel_mci *host = arg;
  730. struct mmc_data *data = host->data;
  731. dev_vdbg(&host->pdev->dev, "DMA complete\n");
  732. if (host->caps.has_dma_conf_reg)
  733. /* Disable DMA hardware handshaking on MCI */
  734. atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
  735. atmci_dma_cleanup(host);
  736. /*
  737. * If the card was removed, data will be NULL. No point trying
  738. * to send the stop command or waiting for NBUSY in this case.
  739. */
  740. if (data) {
  741. dev_dbg(&host->pdev->dev,
  742. "(%s) set pending xfer complete\n", __func__);
  743. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  744. tasklet_schedule(&host->tasklet);
  745. /*
  746. * Regardless of what the documentation says, we have
  747. * to wait for NOTBUSY even after block read
  748. * operations.
  749. *
  750. * When the DMA transfer is complete, the controller
  751. * may still be reading the CRC from the card, i.e.
  752. * the data transfer is still in progress and we
  753. * haven't seen all the potential error bits yet.
  754. *
  755. * The interrupt handler will schedule a different
  756. * tasklet to finish things up when the data transfer
  757. * is completely done.
  758. *
  759. * We may not complete the mmc request here anyway
  760. * because the mmc layer may call back and cause us to
  761. * violate the "don't submit new operations from the
  762. * completion callback" rule of the dma engine
  763. * framework.
  764. */
  765. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  766. }
  767. }
  768. /*
  769. * Returns a mask of interrupt flags to be enabled after the whole
  770. * request has been prepared.
  771. */
  772. static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
  773. {
  774. u32 iflags;
  775. data->error = -EINPROGRESS;
  776. host->sg = data->sg;
  777. host->sg_len = data->sg_len;
  778. host->data = data;
  779. host->data_chan = NULL;
  780. iflags = ATMCI_DATA_ERROR_FLAGS;
  781. /*
  782. * Errata: MMC data write operation with less than 12
  783. * bytes is impossible.
  784. *
  785. * Errata: MCI Transmit Data Register (TDR) FIFO
  786. * corruption when length is not multiple of 4.
  787. */
  788. if (data->blocks * data->blksz < 12
  789. || (data->blocks * data->blksz) & 3)
  790. host->need_reset = true;
  791. host->pio_offset = 0;
  792. if (data->flags & MMC_DATA_READ)
  793. iflags |= ATMCI_RXRDY;
  794. else
  795. iflags |= ATMCI_TXRDY;
  796. return iflags;
  797. }
  798. /*
  799. * Set interrupt flags and set block length into the MCI mode register even
  800. * if this value is also accessible in the MCI block register. It seems to be
  801. * necessary before the High Speed MCI version. It also map sg and configure
  802. * PDC registers.
  803. */
  804. static u32
  805. atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
  806. {
  807. u32 iflags, tmp;
  808. unsigned int sg_len;
  809. enum dma_data_direction dir;
  810. int i;
  811. data->error = -EINPROGRESS;
  812. host->data = data;
  813. host->sg = data->sg;
  814. iflags = ATMCI_DATA_ERROR_FLAGS;
  815. /* Enable pdc mode */
  816. atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
  817. if (data->flags & MMC_DATA_READ) {
  818. dir = DMA_FROM_DEVICE;
  819. iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
  820. } else {
  821. dir = DMA_TO_DEVICE;
  822. iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
  823. }
  824. /* Set BLKLEN */
  825. tmp = atmci_readl(host, ATMCI_MR);
  826. tmp &= 0x0000ffff;
  827. tmp |= ATMCI_BLKLEN(data->blksz);
  828. atmci_writel(host, ATMCI_MR, tmp);
  829. /* Configure PDC */
  830. host->data_size = data->blocks * data->blksz;
  831. sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
  832. if ((!host->caps.has_rwproof)
  833. && (host->data->flags & MMC_DATA_WRITE)) {
  834. sg_copy_to_buffer(host->data->sg, host->data->sg_len,
  835. host->buffer, host->data_size);
  836. if (host->caps.has_bad_data_ordering)
  837. for (i = 0; i < host->data_size; i++)
  838. host->buffer[i] = swab32(host->buffer[i]);
  839. }
  840. if (host->data_size)
  841. atmci_pdc_set_both_buf(host,
  842. ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
  843. return iflags;
  844. }
  845. static u32
  846. atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
  847. {
  848. struct dma_chan *chan;
  849. struct dma_async_tx_descriptor *desc;
  850. struct scatterlist *sg;
  851. unsigned int i;
  852. enum dma_data_direction direction;
  853. enum dma_transfer_direction slave_dirn;
  854. unsigned int sglen;
  855. u32 maxburst;
  856. u32 iflags;
  857. data->error = -EINPROGRESS;
  858. WARN_ON(host->data);
  859. host->sg = NULL;
  860. host->data = data;
  861. iflags = ATMCI_DATA_ERROR_FLAGS;
  862. /*
  863. * We don't do DMA on "complex" transfers, i.e. with
  864. * non-word-aligned buffers or lengths. Also, we don't bother
  865. * with all the DMA setup overhead for short transfers.
  866. */
  867. if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
  868. return atmci_prepare_data(host, data);
  869. if (data->blksz & 3)
  870. return atmci_prepare_data(host, data);
  871. for_each_sg(data->sg, sg, data->sg_len, i) {
  872. if (sg->offset & 3 || sg->length & 3)
  873. return atmci_prepare_data(host, data);
  874. }
  875. /* If we don't have a channel, we can't do DMA */
  876. chan = host->dma.chan;
  877. if (chan)
  878. host->data_chan = chan;
  879. if (!chan)
  880. return -ENODEV;
  881. if (data->flags & MMC_DATA_READ) {
  882. direction = DMA_FROM_DEVICE;
  883. host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
  884. maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst);
  885. } else {
  886. direction = DMA_TO_DEVICE;
  887. host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
  888. maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst);
  889. }
  890. if (host->caps.has_dma_conf_reg)
  891. atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) |
  892. ATMCI_DMAEN);
  893. sglen = dma_map_sg(chan->device->dev, data->sg,
  894. data->sg_len, direction);
  895. dmaengine_slave_config(chan, &host->dma_conf);
  896. desc = dmaengine_prep_slave_sg(chan,
  897. data->sg, sglen, slave_dirn,
  898. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  899. if (!desc)
  900. goto unmap_exit;
  901. host->dma.data_desc = desc;
  902. desc->callback = atmci_dma_complete;
  903. desc->callback_param = host;
  904. return iflags;
  905. unmap_exit:
  906. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
  907. return -ENOMEM;
  908. }
  909. static void
  910. atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
  911. {
  912. return;
  913. }
  914. /*
  915. * Start PDC according to transfer direction.
  916. */
  917. static void
  918. atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
  919. {
  920. if (data->flags & MMC_DATA_READ)
  921. atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
  922. else
  923. atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
  924. }
  925. static void
  926. atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
  927. {
  928. struct dma_chan *chan = host->data_chan;
  929. struct dma_async_tx_descriptor *desc = host->dma.data_desc;
  930. if (chan) {
  931. dmaengine_submit(desc);
  932. dma_async_issue_pending(chan);
  933. }
  934. }
  935. static void atmci_stop_transfer(struct atmel_mci *host)
  936. {
  937. dev_dbg(&host->pdev->dev,
  938. "(%s) set pending xfer complete\n", __func__);
  939. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  940. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  941. }
  942. /*
  943. * Stop data transfer because error(s) occurred.
  944. */
  945. static void atmci_stop_transfer_pdc(struct atmel_mci *host)
  946. {
  947. atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
  948. }
  949. static void atmci_stop_transfer_dma(struct atmel_mci *host)
  950. {
  951. struct dma_chan *chan = host->data_chan;
  952. if (chan) {
  953. dmaengine_terminate_all(chan);
  954. atmci_dma_cleanup(host);
  955. } else {
  956. /* Data transfer was stopped by the interrupt handler */
  957. dev_dbg(&host->pdev->dev,
  958. "(%s) set pending xfer complete\n", __func__);
  959. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  960. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  961. }
  962. }
  963. /*
  964. * Start a request: prepare data if needed, prepare the command and activate
  965. * interrupts.
  966. */
  967. static void atmci_start_request(struct atmel_mci *host,
  968. struct atmel_mci_slot *slot)
  969. {
  970. struct mmc_request *mrq;
  971. struct mmc_command *cmd;
  972. struct mmc_data *data;
  973. u32 iflags;
  974. u32 cmdflags;
  975. mrq = slot->mrq;
  976. host->cur_slot = slot;
  977. host->mrq = mrq;
  978. host->pending_events = 0;
  979. host->completed_events = 0;
  980. host->cmd_status = 0;
  981. host->data_status = 0;
  982. dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);
  983. if (host->need_reset || host->caps.need_reset_after_xfer) {
  984. iflags = atmci_readl(host, ATMCI_IMR);
  985. iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
  986. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  987. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
  988. atmci_writel(host, ATMCI_MR, host->mode_reg);
  989. if (host->caps.has_cfg_reg)
  990. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  991. atmci_writel(host, ATMCI_IER, iflags);
  992. host->need_reset = false;
  993. }
  994. atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
  995. iflags = atmci_readl(host, ATMCI_IMR);
  996. if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
  997. dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
  998. iflags);
  999. if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
  1000. /* Send init sequence (74 clock cycles) */
  1001. atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
  1002. while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
  1003. cpu_relax();
  1004. }
  1005. iflags = 0;
  1006. data = mrq->data;
  1007. if (data) {
  1008. atmci_set_timeout(host, slot, data);
  1009. /* Must set block count/size before sending command */
  1010. atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
  1011. | ATMCI_BLKLEN(data->blksz));
  1012. dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
  1013. ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
  1014. iflags |= host->prepare_data(host, data);
  1015. }
  1016. iflags |= ATMCI_CMDRDY;
  1017. cmd = mrq->cmd;
  1018. cmdflags = atmci_prepare_command(slot->mmc, cmd);
  1019. /*
  1020. * DMA transfer should be started before sending the command to avoid
  1021. * unexpected errors especially for read operations in SDIO mode.
  1022. * Unfortunately, in PDC mode, command has to be sent before starting
  1023. * the transfer.
  1024. */
  1025. if (host->submit_data != &atmci_submit_data_dma)
  1026. atmci_send_command(host, cmd, cmdflags);
  1027. if (data)
  1028. host->submit_data(host, data);
  1029. if (host->submit_data == &atmci_submit_data_dma)
  1030. atmci_send_command(host, cmd, cmdflags);
  1031. if (mrq->stop) {
  1032. host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
  1033. host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
  1034. if (!(data->flags & MMC_DATA_WRITE))
  1035. host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
  1036. if (data->flags & MMC_DATA_STREAM)
  1037. host->stop_cmdr |= ATMCI_CMDR_STREAM;
  1038. else
  1039. host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
  1040. }
  1041. /*
  1042. * We could have enabled interrupts earlier, but I suspect
  1043. * that would open up a nice can of interesting race
  1044. * conditions (e.g. command and data complete, but stop not
  1045. * prepared yet.)
  1046. */
  1047. atmci_writel(host, ATMCI_IER, iflags);
  1048. mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
  1049. }
  1050. static void atmci_queue_request(struct atmel_mci *host,
  1051. struct atmel_mci_slot *slot, struct mmc_request *mrq)
  1052. {
  1053. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  1054. host->state);
  1055. spin_lock_bh(&host->lock);
  1056. slot->mrq = mrq;
  1057. if (host->state == STATE_IDLE) {
  1058. host->state = STATE_SENDING_CMD;
  1059. atmci_start_request(host, slot);
  1060. } else {
  1061. dev_dbg(&host->pdev->dev, "queue request\n");
  1062. list_add_tail(&slot->queue_node, &host->queue);
  1063. }
  1064. spin_unlock_bh(&host->lock);
  1065. }
  1066. static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1067. {
  1068. struct atmel_mci_slot *slot = mmc_priv(mmc);
  1069. struct atmel_mci *host = slot->host;
  1070. struct mmc_data *data;
  1071. WARN_ON(slot->mrq);
  1072. dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
  1073. /*
  1074. * We may "know" the card is gone even though there's still an
  1075. * electrical connection. If so, we really need to communicate
  1076. * this to the MMC core since there won't be any more
  1077. * interrupts as the card is completely removed. Otherwise,
  1078. * the MMC core might believe the card is still there even
  1079. * though the card was just removed very slowly.
  1080. */
  1081. if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
  1082. mrq->cmd->error = -ENOMEDIUM;
  1083. mmc_request_done(mmc, mrq);
  1084. return;
  1085. }
  1086. /* We don't support multiple blocks of weird lengths. */
  1087. data = mrq->data;
  1088. if (data && data->blocks > 1 && data->blksz & 3) {
  1089. mrq->cmd->error = -EINVAL;
  1090. mmc_request_done(mmc, mrq);
  1091. }
  1092. atmci_queue_request(host, slot, mrq);
  1093. }
  1094. static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1095. {
  1096. struct atmel_mci_slot *slot = mmc_priv(mmc);
  1097. struct atmel_mci *host = slot->host;
  1098. unsigned int i;
  1099. bool unprepare_clk;
  1100. slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
  1101. switch (ios->bus_width) {
  1102. case MMC_BUS_WIDTH_1:
  1103. slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
  1104. break;
  1105. case MMC_BUS_WIDTH_4:
  1106. slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
  1107. break;
  1108. }
  1109. if (ios->clock) {
  1110. unsigned int clock_min = ~0U;
  1111. int clkdiv;
  1112. clk_prepare(host->mck);
  1113. unprepare_clk = true;
  1114. spin_lock_bh(&host->lock);
  1115. if (!host->mode_reg) {
  1116. clk_enable(host->mck);
  1117. unprepare_clk = false;
  1118. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  1119. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
  1120. if (host->caps.has_cfg_reg)
  1121. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  1122. }
  1123. /*
  1124. * Use mirror of ios->clock to prevent race with mmc
  1125. * core ios update when finding the minimum.
  1126. */
  1127. slot->clock = ios->clock;
  1128. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  1129. if (host->slot[i] && host->slot[i]->clock
  1130. && host->slot[i]->clock < clock_min)
  1131. clock_min = host->slot[i]->clock;
  1132. }
  1133. /* Calculate clock divider */
  1134. if (host->caps.has_odd_clk_div) {
  1135. clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
  1136. if (clkdiv < 0) {
  1137. dev_warn(&mmc->class_dev,
  1138. "clock %u too fast; using %lu\n",
  1139. clock_min, host->bus_hz / 2);
  1140. clkdiv = 0;
  1141. } else 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 (devm_gpio_request(&host->pdev->dev, slot->detect_pin,
  1902. "mmc_detect")) {
  1903. dev_dbg(&mmc->class_dev, "no detect pin available\n");
  1904. slot->detect_pin = -EBUSY;
  1905. } else if (gpio_get_value(slot->detect_pin) ^
  1906. slot->detect_is_active_high) {
  1907. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1908. }
  1909. }
  1910. if (!gpio_is_valid(slot->detect_pin))
  1911. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1912. if (gpio_is_valid(slot->wp_pin)) {
  1913. if (devm_gpio_request(&host->pdev->dev, slot->wp_pin,
  1914. "mmc_wp")) {
  1915. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  1916. slot->wp_pin = -EBUSY;
  1917. }
  1918. }
  1919. host->slot[id] = slot;
  1920. mmc_regulator_get_supply(mmc);
  1921. mmc_add_host(mmc);
  1922. if (gpio_is_valid(slot->detect_pin)) {
  1923. int ret;
  1924. setup_timer(&slot->detect_timer, atmci_detect_change,
  1925. (unsigned long)slot);
  1926. ret = request_irq(gpio_to_irq(slot->detect_pin),
  1927. atmci_detect_interrupt,
  1928. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  1929. "mmc-detect", slot);
  1930. if (ret) {
  1931. dev_dbg(&mmc->class_dev,
  1932. "could not request IRQ %d for detect pin\n",
  1933. gpio_to_irq(slot->detect_pin));
  1934. slot->detect_pin = -EBUSY;
  1935. }
  1936. }
  1937. atmci_init_debugfs(slot);
  1938. return 0;
  1939. }
  1940. static void atmci_cleanup_slot(struct atmel_mci_slot *slot,
  1941. unsigned int id)
  1942. {
  1943. /* Debugfs stuff is cleaned up by mmc core */
  1944. set_bit(ATMCI_SHUTDOWN, &slot->flags);
  1945. smp_wmb();
  1946. mmc_remove_host(slot->mmc);
  1947. if (gpio_is_valid(slot->detect_pin)) {
  1948. int pin = slot->detect_pin;
  1949. free_irq(gpio_to_irq(pin), slot);
  1950. del_timer_sync(&slot->detect_timer);
  1951. }
  1952. slot->host->slot[id] = NULL;
  1953. mmc_free_host(slot->mmc);
  1954. }
  1955. static bool atmci_filter(struct dma_chan *chan, void *pdata)
  1956. {
  1957. struct mci_platform_data *sl_pdata = pdata;
  1958. struct mci_dma_data *sl;
  1959. if (!sl_pdata)
  1960. return false;
  1961. sl = sl_pdata->dma_slave;
  1962. if (sl && find_slave_dev(sl) == chan->device->dev) {
  1963. chan->private = slave_data_ptr(sl);
  1964. return true;
  1965. } else {
  1966. return false;
  1967. }
  1968. }
  1969. static bool atmci_configure_dma(struct atmel_mci *host)
  1970. {
  1971. struct mci_platform_data *pdata;
  1972. dma_cap_mask_t mask;
  1973. if (host == NULL)
  1974. return false;
  1975. pdata = host->pdev->dev.platform_data;
  1976. dma_cap_zero(mask);
  1977. dma_cap_set(DMA_SLAVE, mask);
  1978. host->dma.chan = dma_request_slave_channel_compat(mask, atmci_filter, pdata,
  1979. &host->pdev->dev, "rxtx");
  1980. if (!host->dma.chan) {
  1981. dev_warn(&host->pdev->dev, "no DMA channel available\n");
  1982. return false;
  1983. } else {
  1984. dev_info(&host->pdev->dev,
  1985. "using %s for DMA transfers\n",
  1986. dma_chan_name(host->dma.chan));
  1987. host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
  1988. host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1989. host->dma_conf.src_maxburst = 1;
  1990. host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
  1991. host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1992. host->dma_conf.dst_maxburst = 1;
  1993. host->dma_conf.device_fc = false;
  1994. return true;
  1995. }
  1996. }
  1997. /*
  1998. * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
  1999. * HSMCI provides DMA support and a new config register but no more supports
  2000. * PDC.
  2001. */
  2002. static void __init atmci_get_cap(struct atmel_mci *host)
  2003. {
  2004. unsigned int version;
  2005. version = atmci_get_version(host);
  2006. dev_info(&host->pdev->dev,
  2007. "version: 0x%x\n", version);
  2008. host->caps.has_dma_conf_reg = 0;
  2009. host->caps.has_pdc = ATMCI_PDC_CONNECTED;
  2010. host->caps.has_cfg_reg = 0;
  2011. host->caps.has_cstor_reg = 0;
  2012. host->caps.has_highspeed = 0;
  2013. host->caps.has_rwproof = 0;
  2014. host->caps.has_odd_clk_div = 0;
  2015. host->caps.has_bad_data_ordering = 1;
  2016. host->caps.need_reset_after_xfer = 1;
  2017. host->caps.need_blksz_mul_4 = 1;
  2018. host->caps.need_notbusy_for_read_ops = 0;
  2019. /* keep only major version number */
  2020. switch (version & 0xf00) {
  2021. case 0x600:
  2022. case 0x500:
  2023. host->caps.has_odd_clk_div = 1;
  2024. case 0x400:
  2025. case 0x300:
  2026. host->caps.has_dma_conf_reg = 1;
  2027. host->caps.has_pdc = 0;
  2028. host->caps.has_cfg_reg = 1;
  2029. host->caps.has_cstor_reg = 1;
  2030. host->caps.has_highspeed = 1;
  2031. case 0x200:
  2032. host->caps.has_rwproof = 1;
  2033. host->caps.need_blksz_mul_4 = 0;
  2034. host->caps.need_notbusy_for_read_ops = 1;
  2035. case 0x100:
  2036. host->caps.has_bad_data_ordering = 0;
  2037. host->caps.need_reset_after_xfer = 0;
  2038. case 0x0:
  2039. break;
  2040. default:
  2041. host->caps.has_pdc = 0;
  2042. dev_warn(&host->pdev->dev,
  2043. "Unmanaged mci version, set minimum capabilities\n");
  2044. break;
  2045. }
  2046. }
  2047. static int __init atmci_probe(struct platform_device *pdev)
  2048. {
  2049. struct mci_platform_data *pdata;
  2050. struct atmel_mci *host;
  2051. struct resource *regs;
  2052. unsigned int nr_slots;
  2053. int irq;
  2054. int ret, i;
  2055. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2056. if (!regs)
  2057. return -ENXIO;
  2058. pdata = pdev->dev.platform_data;
  2059. if (!pdata) {
  2060. pdata = atmci_of_init(pdev);
  2061. if (IS_ERR(pdata)) {
  2062. dev_err(&pdev->dev, "platform data not available\n");
  2063. return PTR_ERR(pdata);
  2064. }
  2065. }
  2066. irq = platform_get_irq(pdev, 0);
  2067. if (irq < 0)
  2068. return irq;
  2069. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  2070. if (!host)
  2071. return -ENOMEM;
  2072. host->pdev = pdev;
  2073. spin_lock_init(&host->lock);
  2074. INIT_LIST_HEAD(&host->queue);
  2075. host->mck = devm_clk_get(&pdev->dev, "mci_clk");
  2076. if (IS_ERR(host->mck))
  2077. return PTR_ERR(host->mck);
  2078. host->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
  2079. if (!host->regs)
  2080. return -ENOMEM;
  2081. ret = clk_prepare_enable(host->mck);
  2082. if (ret)
  2083. return ret;
  2084. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  2085. host->bus_hz = clk_get_rate(host->mck);
  2086. clk_disable_unprepare(host->mck);
  2087. host->mapbase = regs->start;
  2088. tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
  2089. ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
  2090. if (ret)
  2091. return ret;
  2092. /* Get MCI capabilities and set operations according to it */
  2093. atmci_get_cap(host);
  2094. if (atmci_configure_dma(host)) {
  2095. host->prepare_data = &atmci_prepare_data_dma;
  2096. host->submit_data = &atmci_submit_data_dma;
  2097. host->stop_transfer = &atmci_stop_transfer_dma;
  2098. } else if (host->caps.has_pdc) {
  2099. dev_info(&pdev->dev, "using PDC\n");
  2100. host->prepare_data = &atmci_prepare_data_pdc;
  2101. host->submit_data = &atmci_submit_data_pdc;
  2102. host->stop_transfer = &atmci_stop_transfer_pdc;
  2103. } else {
  2104. dev_info(&pdev->dev, "using PIO\n");
  2105. host->prepare_data = &atmci_prepare_data;
  2106. host->submit_data = &atmci_submit_data;
  2107. host->stop_transfer = &atmci_stop_transfer;
  2108. }
  2109. platform_set_drvdata(pdev, host);
  2110. setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
  2111. /* We need at least one slot to succeed */
  2112. nr_slots = 0;
  2113. ret = -ENODEV;
  2114. if (pdata->slot[0].bus_width) {
  2115. ret = atmci_init_slot(host, &pdata->slot[0],
  2116. 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
  2117. if (!ret) {
  2118. nr_slots++;
  2119. host->buf_size = host->slot[0]->mmc->max_req_size;
  2120. }
  2121. }
  2122. if (pdata->slot[1].bus_width) {
  2123. ret = atmci_init_slot(host, &pdata->slot[1],
  2124. 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
  2125. if (!ret) {
  2126. nr_slots++;
  2127. if (host->slot[1]->mmc->max_req_size > host->buf_size)
  2128. host->buf_size =
  2129. host->slot[1]->mmc->max_req_size;
  2130. }
  2131. }
  2132. if (!nr_slots) {
  2133. dev_err(&pdev->dev, "init failed: no slot defined\n");
  2134. goto err_init_slot;
  2135. }
  2136. if (!host->caps.has_rwproof) {
  2137. host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
  2138. &host->buf_phys_addr,
  2139. GFP_KERNEL);
  2140. if (!host->buffer) {
  2141. ret = -ENOMEM;
  2142. dev_err(&pdev->dev, "buffer allocation failed\n");
  2143. goto err_dma_alloc;
  2144. }
  2145. }
  2146. dev_info(&pdev->dev,
  2147. "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
  2148. host->mapbase, irq, nr_slots);
  2149. return 0;
  2150. err_dma_alloc:
  2151. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  2152. if (host->slot[i])
  2153. atmci_cleanup_slot(host->slot[i], i);
  2154. }
  2155. err_init_slot:
  2156. del_timer_sync(&host->timer);
  2157. if (host->dma.chan)
  2158. dma_release_channel(host->dma.chan);
  2159. free_irq(irq, host);
  2160. return ret;
  2161. }
  2162. static int __exit atmci_remove(struct platform_device *pdev)
  2163. {
  2164. struct atmel_mci *host = platform_get_drvdata(pdev);
  2165. unsigned int i;
  2166. if (host->buffer)
  2167. dma_free_coherent(&pdev->dev, host->buf_size,
  2168. host->buffer, host->buf_phys_addr);
  2169. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  2170. if (host->slot[i])
  2171. atmci_cleanup_slot(host->slot[i], i);
  2172. }
  2173. clk_prepare_enable(host->mck);
  2174. atmci_writel(host, ATMCI_IDR, ~0UL);
  2175. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
  2176. atmci_readl(host, ATMCI_SR);
  2177. clk_disable_unprepare(host->mck);
  2178. del_timer_sync(&host->timer);
  2179. if (host->dma.chan)
  2180. dma_release_channel(host->dma.chan);
  2181. free_irq(platform_get_irq(pdev, 0), 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");