atmel-mci.c 73 KB

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