atmel-mci.c 66 KB

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