atmel-mci.c 75 KB

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