atmel-mci.c 68 KB

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