atmel-mci.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  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. #define ATMCI_MAX_NR_SLOTS 2
  39. /*
  40. * Superset of MCI IP registers integrated in Atmel AT91 Processor
  41. * Registers and bitfields marked with [2] are only available in MCI2
  42. */
  43. /* MCI Register Definitions */
  44. #define ATMCI_CR 0x0000 /* Control */
  45. #define ATMCI_CR_MCIEN BIT(0) /* MCI Enable */
  46. #define ATMCI_CR_MCIDIS BIT(1) /* MCI Disable */
  47. #define ATMCI_CR_PWSEN BIT(2) /* Power Save Enable */
  48. #define ATMCI_CR_PWSDIS BIT(3) /* Power Save Disable */
  49. #define ATMCI_CR_SWRST BIT(7) /* Software Reset */
  50. #define ATMCI_MR 0x0004 /* Mode */
  51. #define ATMCI_MR_CLKDIV(x) ((x) << 0) /* Clock Divider */
  52. #define ATMCI_MR_PWSDIV(x) ((x) << 8) /* Power Saving Divider */
  53. #define ATMCI_MR_RDPROOF BIT(11) /* Read Proof */
  54. #define ATMCI_MR_WRPROOF BIT(12) /* Write Proof */
  55. #define ATMCI_MR_PDCFBYTE BIT(13) /* Force Byte Transfer */
  56. #define ATMCI_MR_PDCPADV BIT(14) /* Padding Value */
  57. #define ATMCI_MR_PDCMODE BIT(15) /* PDC-oriented Mode */
  58. #define ATMCI_MR_CLKODD(x) ((x) << 16) /* LSB of Clock Divider */
  59. #define ATMCI_DTOR 0x0008 /* Data Timeout */
  60. #define ATMCI_DTOCYC(x) ((x) << 0) /* Data Timeout Cycles */
  61. #define ATMCI_DTOMUL(x) ((x) << 4) /* Data Timeout Multiplier */
  62. #define ATMCI_SDCR 0x000c /* SD Card / SDIO */
  63. #define ATMCI_SDCSEL_SLOT_A (0 << 0) /* Select SD slot A */
  64. #define ATMCI_SDCSEL_SLOT_B (1 << 0) /* Select SD slot A */
  65. #define ATMCI_SDCSEL_MASK (3 << 0)
  66. #define ATMCI_SDCBUS_1BIT (0 << 6) /* 1-bit data bus */
  67. #define ATMCI_SDCBUS_4BIT (2 << 6) /* 4-bit data bus */
  68. #define ATMCI_SDCBUS_8BIT (3 << 6) /* 8-bit data bus[2] */
  69. #define ATMCI_SDCBUS_MASK (3 << 6)
  70. #define ATMCI_ARGR 0x0010 /* Command Argument */
  71. #define ATMCI_CMDR 0x0014 /* Command */
  72. #define ATMCI_CMDR_CMDNB(x) ((x) << 0) /* Command Opcode */
  73. #define ATMCI_CMDR_RSPTYP_NONE (0 << 6) /* No response */
  74. #define ATMCI_CMDR_RSPTYP_48BIT (1 << 6) /* 48-bit response */
  75. #define ATMCI_CMDR_RSPTYP_136BIT (2 << 6) /* 136-bit response */
  76. #define ATMCI_CMDR_SPCMD_INIT (1 << 8) /* Initialization command */
  77. #define ATMCI_CMDR_SPCMD_SYNC (2 << 8) /* Synchronized command */
  78. #define ATMCI_CMDR_SPCMD_INT (4 << 8) /* Interrupt command */
  79. #define ATMCI_CMDR_SPCMD_INTRESP (5 << 8) /* Interrupt response */
  80. #define ATMCI_CMDR_OPDCMD (1 << 11) /* Open Drain */
  81. #define ATMCI_CMDR_MAXLAT_5CYC (0 << 12) /* Max latency 5 cycles */
  82. #define ATMCI_CMDR_MAXLAT_64CYC (1 << 12) /* Max latency 64 cycles */
  83. #define ATMCI_CMDR_START_XFER (1 << 16) /* Start data transfer */
  84. #define ATMCI_CMDR_STOP_XFER (2 << 16) /* Stop data transfer */
  85. #define ATMCI_CMDR_TRDIR_WRITE (0 << 18) /* Write data */
  86. #define ATMCI_CMDR_TRDIR_READ (1 << 18) /* Read data */
  87. #define ATMCI_CMDR_BLOCK (0 << 19) /* Single-block transfer */
  88. #define ATMCI_CMDR_MULTI_BLOCK (1 << 19) /* Multi-block transfer */
  89. #define ATMCI_CMDR_STREAM (2 << 19) /* MMC Stream transfer */
  90. #define ATMCI_CMDR_SDIO_BYTE (4 << 19) /* SDIO Byte transfer */
  91. #define ATMCI_CMDR_SDIO_BLOCK (5 << 19) /* SDIO Block transfer */
  92. #define ATMCI_CMDR_SDIO_SUSPEND (1 << 24) /* SDIO Suspend Command */
  93. #define ATMCI_CMDR_SDIO_RESUME (2 << 24) /* SDIO Resume Command */
  94. #define ATMCI_BLKR 0x0018 /* Block */
  95. #define ATMCI_BCNT(x) ((x) << 0) /* Data Block Count */
  96. #define ATMCI_BLKLEN(x) ((x) << 16) /* Data Block Length */
  97. #define ATMCI_CSTOR 0x001c /* Completion Signal Timeout[2] */
  98. #define ATMCI_CSTOCYC(x) ((x) << 0) /* CST cycles */
  99. #define ATMCI_CSTOMUL(x) ((x) << 4) /* CST multiplier */
  100. #define ATMCI_RSPR 0x0020 /* Response 0 */
  101. #define ATMCI_RSPR1 0x0024 /* Response 1 */
  102. #define ATMCI_RSPR2 0x0028 /* Response 2 */
  103. #define ATMCI_RSPR3 0x002c /* Response 3 */
  104. #define ATMCI_RDR 0x0030 /* Receive Data */
  105. #define ATMCI_TDR 0x0034 /* Transmit Data */
  106. #define ATMCI_SR 0x0040 /* Status */
  107. #define ATMCI_IER 0x0044 /* Interrupt Enable */
  108. #define ATMCI_IDR 0x0048 /* Interrupt Disable */
  109. #define ATMCI_IMR 0x004c /* Interrupt Mask */
  110. #define ATMCI_CMDRDY BIT(0) /* Command Ready */
  111. #define ATMCI_RXRDY BIT(1) /* Receiver Ready */
  112. #define ATMCI_TXRDY BIT(2) /* Transmitter Ready */
  113. #define ATMCI_BLKE BIT(3) /* Data Block Ended */
  114. #define ATMCI_DTIP BIT(4) /* Data Transfer In Progress */
  115. #define ATMCI_NOTBUSY BIT(5) /* Data Not Busy */
  116. #define ATMCI_ENDRX BIT(6) /* End of RX Buffer */
  117. #define ATMCI_ENDTX BIT(7) /* End of TX Buffer */
  118. #define ATMCI_SDIOIRQA BIT(8) /* SDIO IRQ in slot A */
  119. #define ATMCI_SDIOIRQB BIT(9) /* SDIO IRQ in slot B */
  120. #define ATMCI_SDIOWAIT BIT(12) /* SDIO Read Wait Operation Status */
  121. #define ATMCI_CSRCV BIT(13) /* CE-ATA Completion Signal Received */
  122. #define ATMCI_RXBUFF BIT(14) /* RX Buffer Full */
  123. #define ATMCI_TXBUFE BIT(15) /* TX Buffer Empty */
  124. #define ATMCI_RINDE BIT(16) /* Response Index Error */
  125. #define ATMCI_RDIRE BIT(17) /* Response Direction Error */
  126. #define ATMCI_RCRCE BIT(18) /* Response CRC Error */
  127. #define ATMCI_RENDE BIT(19) /* Response End Bit Error */
  128. #define ATMCI_RTOE BIT(20) /* Response Time-Out Error */
  129. #define ATMCI_DCRCE BIT(21) /* Data CRC Error */
  130. #define ATMCI_DTOE BIT(22) /* Data Time-Out Error */
  131. #define ATMCI_CSTOE BIT(23) /* Completion Signal Time-out Error */
  132. #define ATMCI_BLKOVRE BIT(24) /* DMA Block Overrun Error */
  133. #define ATMCI_DMADONE BIT(25) /* DMA Transfer Done */
  134. #define ATMCI_FIFOEMPTY BIT(26) /* FIFO Empty Flag */
  135. #define ATMCI_XFRDONE BIT(27) /* Transfer Done Flag */
  136. #define ATMCI_ACKRCV BIT(28) /* Boot Operation Acknowledge Received */
  137. #define ATMCI_ACKRCVE BIT(29) /* Boot Operation Acknowledge Error */
  138. #define ATMCI_OVRE BIT(30) /* RX Overrun Error */
  139. #define ATMCI_UNRE BIT(31) /* TX Underrun Error */
  140. #define ATMCI_DMA 0x0050 /* DMA Configuration[2] */
  141. #define ATMCI_DMA_OFFSET(x) ((x) << 0) /* DMA Write Buffer Offset */
  142. #define ATMCI_DMA_CHKSIZE(x) ((x) << 4) /* DMA Channel Read and Write Chunk Size */
  143. #define ATMCI_DMAEN BIT(8) /* DMA Hardware Handshaking Enable */
  144. #define ATMCI_CFG 0x0054 /* Configuration[2] */
  145. #define ATMCI_CFG_FIFOMODE_1DATA BIT(0) /* MCI Internal FIFO control mode */
  146. #define ATMCI_CFG_FERRCTRL_COR BIT(4) /* Flow Error flag reset control mode */
  147. #define ATMCI_CFG_HSMODE BIT(8) /* High Speed Mode */
  148. #define ATMCI_CFG_LSYNC BIT(12) /* Synchronize on the last block */
  149. #define ATMCI_WPMR 0x00e4 /* Write Protection Mode[2] */
  150. #define ATMCI_WP_EN BIT(0) /* WP Enable */
  151. #define ATMCI_WP_KEY (0x4d4349 << 8) /* WP Key */
  152. #define ATMCI_WPSR 0x00e8 /* Write Protection Status[2] */
  153. #define ATMCI_GET_WP_VS(x) ((x) & 0x0f)
  154. #define ATMCI_GET_WP_VSRC(x) (((x) >> 8) & 0xffff)
  155. #define ATMCI_VERSION 0x00FC /* Version */
  156. #define ATMCI_FIFO_APERTURE 0x0200 /* FIFO Aperture[2] */
  157. /* This is not including the FIFO Aperture on MCI2 */
  158. #define ATMCI_REGS_SIZE 0x100
  159. /* Register access macros */
  160. #define atmci_readl(port, reg) \
  161. __raw_readl((port)->regs + reg)
  162. #define atmci_writel(port, reg, value) \
  163. __raw_writel((value), (port)->regs + reg)
  164. #define ATMCI_CMD_TIMEOUT_MS 2000
  165. #define AUTOSUSPEND_DELAY 50
  166. #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
  167. #define ATMCI_DMA_THRESHOLD 16
  168. enum {
  169. EVENT_CMD_RDY = 0,
  170. EVENT_XFER_COMPLETE,
  171. EVENT_NOTBUSY,
  172. EVENT_DATA_ERROR,
  173. };
  174. enum atmel_mci_state {
  175. STATE_IDLE = 0,
  176. STATE_SENDING_CMD,
  177. STATE_DATA_XFER,
  178. STATE_WAITING_NOTBUSY,
  179. STATE_SENDING_STOP,
  180. STATE_END_REQUEST,
  181. };
  182. enum atmci_xfer_dir {
  183. XFER_RECEIVE = 0,
  184. XFER_TRANSMIT,
  185. };
  186. enum atmci_pdc_buf {
  187. PDC_FIRST_BUF = 0,
  188. PDC_SECOND_BUF,
  189. };
  190. /**
  191. * struct mci_slot_pdata - board-specific per-slot configuration
  192. * @bus_width: Number of data lines wired up the slot
  193. * @detect_pin: GPIO pin wired to the card detect switch
  194. * @wp_pin: GPIO pin wired to the write protect sensor
  195. * @non_removable: The slot is not removable, only detect once
  196. *
  197. * If a given slot is not present on the board, @bus_width should be
  198. * set to 0. The other fields are ignored in this case.
  199. *
  200. * Any pins that aren't available should be set to a negative value.
  201. *
  202. * Note that support for multiple slots is experimental -- some cards
  203. * might get upset if we don't get the clock management exactly right.
  204. * But in most cases, it should work just fine.
  205. */
  206. struct mci_slot_pdata {
  207. unsigned int bus_width;
  208. struct gpio_desc *detect_pin;
  209. struct gpio_desc *wp_pin;
  210. bool non_removable;
  211. };
  212. struct atmel_mci_caps {
  213. bool has_dma_conf_reg;
  214. bool has_pdc;
  215. bool has_cfg_reg;
  216. bool has_cstor_reg;
  217. bool has_highspeed;
  218. bool has_rwproof;
  219. bool has_odd_clk_div;
  220. bool has_bad_data_ordering;
  221. bool need_reset_after_xfer;
  222. bool need_blksz_mul_4;
  223. bool need_notbusy_for_read_ops;
  224. };
  225. struct atmel_mci_dma {
  226. struct dma_chan *chan;
  227. struct dma_async_tx_descriptor *data_desc;
  228. };
  229. /**
  230. * struct atmel_mci - MMC controller state shared between all slots
  231. * @lock: Spinlock protecting the queue and associated data.
  232. * @regs: Pointer to MMIO registers.
  233. * @sg: Scatterlist entry currently being processed by PIO or PDC code.
  234. * @sg_len: Size of the scatterlist
  235. * @pio_offset: Offset into the current scatterlist entry.
  236. * @buffer: Buffer used if we don't have the r/w proof capability. We
  237. * don't have the time to switch pdc buffers so we have to use only
  238. * one buffer for the full transaction.
  239. * @buf_size: size of the buffer.
  240. * @buf_phys_addr: buffer address needed for pdc.
  241. * @cur_slot: The slot which is currently using the controller.
  242. * @mrq: The request currently being processed on @cur_slot,
  243. * or NULL if the controller is idle.
  244. * @cmd: The command currently being sent to the card, or NULL.
  245. * @data: The data currently being transferred, or NULL if no data
  246. * transfer is in progress.
  247. * @data_size: just data->blocks * data->blksz.
  248. * @dma: DMA client state.
  249. * @data_chan: DMA channel being used for the current data transfer.
  250. * @dma_conf: Configuration for the DMA slave
  251. * @cmd_status: Snapshot of SR taken upon completion of the current
  252. * command. Only valid when EVENT_CMD_COMPLETE is pending.
  253. * @data_status: Snapshot of SR taken upon completion of the current
  254. * data transfer. Only valid when EVENT_DATA_COMPLETE or
  255. * EVENT_DATA_ERROR is pending.
  256. * @stop_cmdr: Value to be loaded into CMDR when the stop command is
  257. * to be sent.
  258. * @bh_work: Work running the request state machine.
  259. * @pending_events: Bitmask of events flagged by the interrupt handler
  260. * to be processed by the work.
  261. * @completed_events: Bitmask of events which the state machine has
  262. * processed.
  263. * @state: Work state.
  264. * @queue: List of slots waiting for access to the controller.
  265. * @need_clock_update: Update the clock rate before the next request.
  266. * @need_reset: Reset controller before next request.
  267. * @timer: Timer to balance the data timeout error flag which cannot rise.
  268. * @mode_reg: Value of the MR register.
  269. * @cfg_reg: Value of the CFG register.
  270. * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
  271. * rate and timeout calculations.
  272. * @mapbase: Physical address of the MMIO registers.
  273. * @mck: The peripheral bus clock hooked up to the MMC controller.
  274. * @dev: Device associated with the MMC controller.
  275. * @pdata: Per-slot configuration data.
  276. * @slot: Slots sharing this MMC controller.
  277. * @caps: MCI capabilities depending on MCI version.
  278. * @prepare_data: function to setup MCI before data transfer which
  279. * depends on MCI capabilities.
  280. * @submit_data: function to start data transfer which depends on MCI
  281. * capabilities.
  282. * @stop_transfer: function to stop data transfer which depends on MCI
  283. * capabilities.
  284. *
  285. * Locking
  286. * =======
  287. *
  288. * @lock is a softirq-safe spinlock protecting @queue as well as
  289. * @cur_slot, @mrq and @state. These must always be updated
  290. * at the same time while holding @lock.
  291. *
  292. * @lock also protects mode_reg and need_clock_update since these are
  293. * used to synchronize mode register updates with the queue
  294. * processing.
  295. *
  296. * The @mrq field of struct atmel_mci_slot is also protected by @lock,
  297. * and must always be written at the same time as the slot is added to
  298. * @queue.
  299. *
  300. * @pending_events and @completed_events are accessed using atomic bit
  301. * operations, so they don't need any locking.
  302. *
  303. * None of the fields touched by the interrupt handler need any
  304. * locking. However, ordering is important: Before EVENT_DATA_ERROR or
  305. * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
  306. * interrupts must be disabled and @data_status updated with a
  307. * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
  308. * CMDRDY interrupt must be disabled and @cmd_status updated with a
  309. * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
  310. * bytes_xfered field of @data must be written. This is ensured by
  311. * using barriers.
  312. */
  313. struct atmel_mci {
  314. spinlock_t lock;
  315. void __iomem *regs;
  316. struct scatterlist *sg;
  317. unsigned int sg_len;
  318. unsigned int pio_offset;
  319. unsigned int *buffer;
  320. unsigned int buf_size;
  321. dma_addr_t buf_phys_addr;
  322. struct atmel_mci_slot *cur_slot;
  323. struct mmc_request *mrq;
  324. struct mmc_command *cmd;
  325. struct mmc_data *data;
  326. unsigned int data_size;
  327. struct atmel_mci_dma dma;
  328. struct dma_chan *data_chan;
  329. struct dma_slave_config dma_conf;
  330. u32 cmd_status;
  331. u32 data_status;
  332. u32 stop_cmdr;
  333. struct work_struct bh_work;
  334. unsigned long pending_events;
  335. unsigned long completed_events;
  336. enum atmel_mci_state state;
  337. struct list_head queue;
  338. bool need_clock_update;
  339. bool need_reset;
  340. struct timer_list timer;
  341. u32 mode_reg;
  342. u32 cfg_reg;
  343. unsigned long bus_hz;
  344. unsigned long mapbase;
  345. struct clk *mck;
  346. struct device *dev;
  347. struct mci_slot_pdata pdata[ATMCI_MAX_NR_SLOTS];
  348. struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
  349. struct atmel_mci_caps caps;
  350. u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
  351. void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
  352. void (*stop_transfer)(struct atmel_mci *host);
  353. };
  354. /**
  355. * struct atmel_mci_slot - MMC slot state
  356. * @mmc: The mmc_host representing this slot.
  357. * @host: The MMC controller this slot is using.
  358. * @sdc_reg: Value of SDCR to be written before using this slot.
  359. * @sdio_irq: SDIO irq mask for this slot.
  360. * @mrq: mmc_request currently being processed or waiting to be
  361. * processed, or NULL when the slot is idle.
  362. * @queue_node: List node for placing this node in the @queue list of
  363. * &struct atmel_mci.
  364. * @clock: Clock rate configured by set_ios(). Protected by host->lock.
  365. * @flags: Random state bits associated with the slot.
  366. * @detect_pin: GPIO pin used for card detection, or negative if not
  367. * available.
  368. * @wp_pin: GPIO pin used for card write protect sending, or negative
  369. * if not available.
  370. * @detect_timer: Timer used for debouncing @detect_pin interrupts.
  371. */
  372. struct atmel_mci_slot {
  373. struct mmc_host *mmc;
  374. struct atmel_mci *host;
  375. u32 sdc_reg;
  376. u32 sdio_irq;
  377. struct mmc_request *mrq;
  378. struct list_head queue_node;
  379. unsigned int clock;
  380. unsigned long flags;
  381. #define ATMCI_CARD_PRESENT 0
  382. #define ATMCI_CARD_NEED_INIT 1
  383. #define ATMCI_SHUTDOWN 2
  384. struct gpio_desc *detect_pin;
  385. struct gpio_desc *wp_pin;
  386. struct timer_list detect_timer;
  387. };
  388. #define atmci_test_and_clear_pending(host, event) \
  389. test_and_clear_bit(event, &host->pending_events)
  390. #define atmci_set_completed(host, event) \
  391. set_bit(event, &host->completed_events)
  392. #define atmci_set_pending(host, event) \
  393. set_bit(event, &host->pending_events)
  394. /*
  395. * The debugfs stuff below is mostly optimized away when
  396. * CONFIG_DEBUG_FS is not set.
  397. */
  398. static int atmci_req_show(struct seq_file *s, void *v)
  399. {
  400. struct atmel_mci_slot *slot = s->private;
  401. struct mmc_request *mrq;
  402. struct mmc_command *cmd;
  403. struct mmc_command *stop;
  404. struct mmc_data *data;
  405. /* Make sure we get a consistent snapshot */
  406. spin_lock_bh(&slot->host->lock);
  407. mrq = slot->mrq;
  408. if (mrq) {
  409. cmd = mrq->cmd;
  410. data = mrq->data;
  411. stop = mrq->stop;
  412. if (cmd)
  413. seq_printf(s,
  414. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  415. cmd->opcode, cmd->arg, cmd->flags,
  416. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  417. cmd->resp[3], cmd->error);
  418. if (data)
  419. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  420. data->bytes_xfered, data->blocks,
  421. data->blksz, data->flags, data->error);
  422. if (stop)
  423. seq_printf(s,
  424. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  425. stop->opcode, stop->arg, stop->flags,
  426. stop->resp[0], stop->resp[1], stop->resp[2],
  427. stop->resp[3], stop->error);
  428. }
  429. spin_unlock_bh(&slot->host->lock);
  430. return 0;
  431. }
  432. DEFINE_SHOW_ATTRIBUTE(atmci_req);
  433. static void atmci_show_status_reg(struct seq_file *s,
  434. const char *regname, u32 value)
  435. {
  436. static const char *sr_bit[] = {
  437. [0] = "CMDRDY",
  438. [1] = "RXRDY",
  439. [2] = "TXRDY",
  440. [3] = "BLKE",
  441. [4] = "DTIP",
  442. [5] = "NOTBUSY",
  443. [6] = "ENDRX",
  444. [7] = "ENDTX",
  445. [8] = "SDIOIRQA",
  446. [9] = "SDIOIRQB",
  447. [12] = "SDIOWAIT",
  448. [14] = "RXBUFF",
  449. [15] = "TXBUFE",
  450. [16] = "RINDE",
  451. [17] = "RDIRE",
  452. [18] = "RCRCE",
  453. [19] = "RENDE",
  454. [20] = "RTOE",
  455. [21] = "DCRCE",
  456. [22] = "DTOE",
  457. [23] = "CSTOE",
  458. [24] = "BLKOVRE",
  459. [25] = "DMADONE",
  460. [26] = "FIFOEMPTY",
  461. [27] = "XFRDONE",
  462. [30] = "OVRE",
  463. [31] = "UNRE",
  464. };
  465. unsigned int i;
  466. seq_printf(s, "%s:\t0x%08x", regname, value);
  467. for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
  468. if (value & (1 << i)) {
  469. if (sr_bit[i])
  470. seq_printf(s, " %s", sr_bit[i]);
  471. else
  472. seq_puts(s, " UNKNOWN");
  473. }
  474. }
  475. seq_putc(s, '\n');
  476. }
  477. static int atmci_regs_show(struct seq_file *s, void *v)
  478. {
  479. struct atmel_mci *host = s->private;
  480. struct device *dev = host->dev;
  481. u32 *buf;
  482. int ret = 0;
  483. buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
  484. if (!buf)
  485. return -ENOMEM;
  486. pm_runtime_get_sync(dev);
  487. /*
  488. * Grab a more or less consistent snapshot. Note that we're
  489. * not disabling interrupts, so IMR and SR may not be
  490. * consistent.
  491. */
  492. spin_lock_bh(&host->lock);
  493. memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
  494. spin_unlock_bh(&host->lock);
  495. pm_runtime_mark_last_busy(dev);
  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", S_IRUSR, root, host, &atmci_regs_fops);
  552. debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
  553. debugfs_create_u32("state", S_IRUSR, root, &host->state);
  554. debugfs_create_xul("pending_events", S_IRUSR, root,
  555. &host->pending_events);
  556. debugfs_create_xul("completed_events", S_IRUSR, 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 = from_timer(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 alignement 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. del_timer(&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 = from_timer(slot, t, detect_timer);
  1439. bool present;
  1440. bool present_old;
  1441. /*
  1442. * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
  1443. * freeing the interrupt. We must not re-enable the interrupt
  1444. * if it has been freed, and if we're shutting down, it
  1445. * doesn't really matter whether the card is present or not.
  1446. */
  1447. smp_rmb();
  1448. if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
  1449. return;
  1450. enable_irq(gpiod_to_irq(slot->detect_pin));
  1451. present = gpiod_get_value_cansleep(slot->detect_pin);
  1452. present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1453. dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
  1454. present, present_old);
  1455. if (present != present_old) {
  1456. struct atmel_mci *host = slot->host;
  1457. struct mmc_request *mrq;
  1458. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1459. present ? "inserted" : "removed");
  1460. spin_lock(&host->lock);
  1461. if (!present)
  1462. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1463. else
  1464. set_bit(ATMCI_CARD_PRESENT, &slot->flags);
  1465. /* Clean up queue if present */
  1466. mrq = slot->mrq;
  1467. if (mrq) {
  1468. if (mrq == host->mrq) {
  1469. /*
  1470. * Reset controller to terminate any ongoing
  1471. * commands or data transfers.
  1472. */
  1473. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  1474. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
  1475. atmci_writel(host, ATMCI_MR, host->mode_reg);
  1476. if (host->caps.has_cfg_reg)
  1477. atmci_writel(host, ATMCI_CFG, host->cfg_reg);
  1478. host->data = NULL;
  1479. host->cmd = NULL;
  1480. switch (host->state) {
  1481. case STATE_IDLE:
  1482. break;
  1483. case STATE_SENDING_CMD:
  1484. mrq->cmd->error = -ENOMEDIUM;
  1485. if (mrq->data)
  1486. host->stop_transfer(host);
  1487. break;
  1488. case STATE_DATA_XFER:
  1489. mrq->data->error = -ENOMEDIUM;
  1490. host->stop_transfer(host);
  1491. break;
  1492. case STATE_WAITING_NOTBUSY:
  1493. mrq->data->error = -ENOMEDIUM;
  1494. break;
  1495. case STATE_SENDING_STOP:
  1496. mrq->stop->error = -ENOMEDIUM;
  1497. break;
  1498. case STATE_END_REQUEST:
  1499. break;
  1500. }
  1501. atmci_request_end(host, mrq);
  1502. } else {
  1503. list_del(&slot->queue_node);
  1504. mrq->cmd->error = -ENOMEDIUM;
  1505. if (mrq->data)
  1506. mrq->data->error = -ENOMEDIUM;
  1507. if (mrq->stop)
  1508. mrq->stop->error = -ENOMEDIUM;
  1509. spin_unlock(&host->lock);
  1510. mmc_request_done(slot->mmc, mrq);
  1511. spin_lock(&host->lock);
  1512. }
  1513. }
  1514. spin_unlock(&host->lock);
  1515. mmc_detect_change(slot->mmc, 0);
  1516. }
  1517. }
  1518. static void atmci_work_func(struct work_struct *t)
  1519. {
  1520. struct atmel_mci *host = from_work(host, t, bh_work);
  1521. struct mmc_request *mrq = host->mrq;
  1522. struct mmc_data *data = host->data;
  1523. struct device *dev = host->dev;
  1524. enum atmel_mci_state state = host->state;
  1525. enum atmel_mci_state prev_state;
  1526. u32 status;
  1527. spin_lock(&host->lock);
  1528. state = host->state;
  1529. dev_vdbg(dev, "bh_work: state %u pending/completed/mask %lx/%lx/%x\n",
  1530. state, host->pending_events, host->completed_events,
  1531. atmci_readl(host, ATMCI_IMR));
  1532. do {
  1533. prev_state = state;
  1534. dev_dbg(dev, "FSM: state=%d\n", state);
  1535. switch (state) {
  1536. case STATE_IDLE:
  1537. break;
  1538. case STATE_SENDING_CMD:
  1539. /*
  1540. * Command has been sent, we are waiting for command
  1541. * ready. Then we have three next states possible:
  1542. * END_REQUEST by default, WAITING_NOTBUSY if it's a
  1543. * command needing it or DATA_XFER if there is data.
  1544. */
  1545. dev_dbg(dev, "FSM: cmd ready?\n");
  1546. if (!atmci_test_and_clear_pending(host,
  1547. EVENT_CMD_RDY))
  1548. break;
  1549. dev_dbg(dev, "set completed cmd ready\n");
  1550. host->cmd = NULL;
  1551. atmci_set_completed(host, EVENT_CMD_RDY);
  1552. atmci_command_complete(host, mrq->cmd);
  1553. if (mrq->data) {
  1554. dev_dbg(dev, "command with data transfer\n");
  1555. /*
  1556. * If there is a command error don't start
  1557. * data transfer.
  1558. */
  1559. if (mrq->cmd->error) {
  1560. host->stop_transfer(host);
  1561. host->data = NULL;
  1562. atmci_writel(host, ATMCI_IDR,
  1563. ATMCI_TXRDY | ATMCI_RXRDY
  1564. | ATMCI_DATA_ERROR_FLAGS);
  1565. state = STATE_END_REQUEST;
  1566. } else
  1567. state = STATE_DATA_XFER;
  1568. } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
  1569. dev_dbg(dev, "command response need waiting notbusy\n");
  1570. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1571. state = STATE_WAITING_NOTBUSY;
  1572. } else
  1573. state = STATE_END_REQUEST;
  1574. break;
  1575. case STATE_DATA_XFER:
  1576. if (atmci_test_and_clear_pending(host,
  1577. EVENT_DATA_ERROR)) {
  1578. dev_dbg(dev, "set completed data error\n");
  1579. atmci_set_completed(host, EVENT_DATA_ERROR);
  1580. state = STATE_END_REQUEST;
  1581. break;
  1582. }
  1583. /*
  1584. * A data transfer is in progress. The event expected
  1585. * to move to the next state depends of data transfer
  1586. * type (PDC or DMA). Once transfer done we can move
  1587. * to the next step which is WAITING_NOTBUSY in write
  1588. * case and directly SENDING_STOP in read case.
  1589. */
  1590. dev_dbg(dev, "FSM: xfer complete?\n");
  1591. if (!atmci_test_and_clear_pending(host,
  1592. EVENT_XFER_COMPLETE))
  1593. break;
  1594. dev_dbg(dev, "(%s) set completed xfer complete\n", __func__);
  1595. atmci_set_completed(host, EVENT_XFER_COMPLETE);
  1596. if (host->caps.need_notbusy_for_read_ops ||
  1597. (host->data->flags & MMC_DATA_WRITE)) {
  1598. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1599. state = STATE_WAITING_NOTBUSY;
  1600. } else if (host->mrq->stop) {
  1601. atmci_send_stop_cmd(host, data);
  1602. state = STATE_SENDING_STOP;
  1603. } else {
  1604. host->data = NULL;
  1605. data->bytes_xfered = data->blocks * data->blksz;
  1606. data->error = 0;
  1607. state = STATE_END_REQUEST;
  1608. }
  1609. break;
  1610. case STATE_WAITING_NOTBUSY:
  1611. /*
  1612. * We can be in the state for two reasons: a command
  1613. * requiring waiting not busy signal (stop command
  1614. * included) or a write operation. In the latest case,
  1615. * we need to send a stop command.
  1616. */
  1617. dev_dbg(dev, "FSM: not busy?\n");
  1618. if (!atmci_test_and_clear_pending(host,
  1619. EVENT_NOTBUSY))
  1620. break;
  1621. dev_dbg(dev, "set completed not busy\n");
  1622. atmci_set_completed(host, EVENT_NOTBUSY);
  1623. if (host->data) {
  1624. /*
  1625. * For some commands such as CMD53, even if
  1626. * there is data transfer, there is no stop
  1627. * command to send.
  1628. */
  1629. if (host->mrq->stop) {
  1630. atmci_send_stop_cmd(host, data);
  1631. state = STATE_SENDING_STOP;
  1632. } else {
  1633. host->data = NULL;
  1634. data->bytes_xfered = data->blocks
  1635. * data->blksz;
  1636. data->error = 0;
  1637. state = STATE_END_REQUEST;
  1638. }
  1639. } else
  1640. state = STATE_END_REQUEST;
  1641. break;
  1642. case STATE_SENDING_STOP:
  1643. /*
  1644. * In this state, it is important to set host->data to
  1645. * NULL (which is tested in the waiting notbusy state)
  1646. * in order to go to the end request state instead of
  1647. * sending stop again.
  1648. */
  1649. dev_dbg(dev, "FSM: cmd ready?\n");
  1650. if (!atmci_test_and_clear_pending(host,
  1651. EVENT_CMD_RDY))
  1652. break;
  1653. dev_dbg(dev, "FSM: cmd ready\n");
  1654. host->cmd = NULL;
  1655. data->bytes_xfered = data->blocks * data->blksz;
  1656. data->error = 0;
  1657. atmci_command_complete(host, mrq->stop);
  1658. if (mrq->stop->error) {
  1659. host->stop_transfer(host);
  1660. atmci_writel(host, ATMCI_IDR,
  1661. ATMCI_TXRDY | ATMCI_RXRDY
  1662. | ATMCI_DATA_ERROR_FLAGS);
  1663. state = STATE_END_REQUEST;
  1664. } else {
  1665. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1666. state = STATE_WAITING_NOTBUSY;
  1667. }
  1668. host->data = NULL;
  1669. break;
  1670. case STATE_END_REQUEST:
  1671. atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
  1672. | ATMCI_DATA_ERROR_FLAGS);
  1673. status = host->data_status;
  1674. if (unlikely(status)) {
  1675. host->stop_transfer(host);
  1676. host->data = NULL;
  1677. if (data) {
  1678. if (status & ATMCI_DTOE) {
  1679. data->error = -ETIMEDOUT;
  1680. } else if (status & ATMCI_DCRCE) {
  1681. data->error = -EILSEQ;
  1682. } else {
  1683. data->error = -EIO;
  1684. }
  1685. }
  1686. }
  1687. atmci_request_end(host, host->mrq);
  1688. goto unlock; /* atmci_request_end() sets host->state */
  1689. break;
  1690. }
  1691. } while (state != prev_state);
  1692. host->state = state;
  1693. unlock:
  1694. spin_unlock(&host->lock);
  1695. }
  1696. static void atmci_read_data_pio(struct atmel_mci *host)
  1697. {
  1698. struct scatterlist *sg = host->sg;
  1699. unsigned int offset = host->pio_offset;
  1700. struct mmc_data *data = host->data;
  1701. u32 value;
  1702. u32 status;
  1703. unsigned int nbytes = 0;
  1704. do {
  1705. value = atmci_readl(host, ATMCI_RDR);
  1706. if (likely(offset + 4 <= sg->length)) {
  1707. sg_pcopy_from_buffer(sg, 1, &value, sizeof(u32), offset);
  1708. offset += 4;
  1709. nbytes += 4;
  1710. if (offset == sg->length) {
  1711. flush_dcache_page(sg_page(sg));
  1712. host->sg = sg = sg_next(sg);
  1713. host->sg_len--;
  1714. if (!sg || !host->sg_len)
  1715. goto done;
  1716. offset = 0;
  1717. }
  1718. } else {
  1719. unsigned int remaining = sg->length - offset;
  1720. sg_pcopy_from_buffer(sg, 1, &value, remaining, offset);
  1721. nbytes += remaining;
  1722. flush_dcache_page(sg_page(sg));
  1723. host->sg = sg = sg_next(sg);
  1724. host->sg_len--;
  1725. if (!sg || !host->sg_len)
  1726. goto done;
  1727. offset = 4 - remaining;
  1728. sg_pcopy_from_buffer(sg, 1, (u8 *)&value + remaining,
  1729. offset, 0);
  1730. nbytes += offset;
  1731. }
  1732. status = atmci_readl(host, ATMCI_SR);
  1733. if (status & ATMCI_DATA_ERROR_FLAGS) {
  1734. atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
  1735. | ATMCI_DATA_ERROR_FLAGS));
  1736. host->data_status = status;
  1737. data->bytes_xfered += nbytes;
  1738. return;
  1739. }
  1740. } while (status & ATMCI_RXRDY);
  1741. host->pio_offset = offset;
  1742. data->bytes_xfered += nbytes;
  1743. return;
  1744. done:
  1745. atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
  1746. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1747. data->bytes_xfered += nbytes;
  1748. smp_wmb();
  1749. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  1750. }
  1751. static void atmci_write_data_pio(struct atmel_mci *host)
  1752. {
  1753. struct scatterlist *sg = host->sg;
  1754. unsigned int offset = host->pio_offset;
  1755. struct mmc_data *data = host->data;
  1756. u32 value;
  1757. u32 status;
  1758. unsigned int nbytes = 0;
  1759. do {
  1760. if (likely(offset + 4 <= sg->length)) {
  1761. sg_pcopy_to_buffer(sg, 1, &value, sizeof(u32), offset);
  1762. atmci_writel(host, ATMCI_TDR, value);
  1763. offset += 4;
  1764. nbytes += 4;
  1765. if (offset == sg->length) {
  1766. host->sg = sg = sg_next(sg);
  1767. host->sg_len--;
  1768. if (!sg || !host->sg_len)
  1769. goto done;
  1770. offset = 0;
  1771. }
  1772. } else {
  1773. unsigned int remaining = sg->length - offset;
  1774. value = 0;
  1775. sg_pcopy_to_buffer(sg, 1, &value, remaining, offset);
  1776. nbytes += remaining;
  1777. host->sg = sg = sg_next(sg);
  1778. host->sg_len--;
  1779. if (!sg || !host->sg_len) {
  1780. atmci_writel(host, ATMCI_TDR, value);
  1781. goto done;
  1782. }
  1783. offset = 4 - remaining;
  1784. sg_pcopy_to_buffer(sg, 1, (u8 *)&value + remaining,
  1785. offset, 0);
  1786. atmci_writel(host, ATMCI_TDR, value);
  1787. nbytes += offset;
  1788. }
  1789. status = atmci_readl(host, ATMCI_SR);
  1790. if (status & ATMCI_DATA_ERROR_FLAGS) {
  1791. atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
  1792. | ATMCI_DATA_ERROR_FLAGS));
  1793. host->data_status = status;
  1794. data->bytes_xfered += nbytes;
  1795. return;
  1796. }
  1797. } while (status & ATMCI_TXRDY);
  1798. host->pio_offset = offset;
  1799. data->bytes_xfered += nbytes;
  1800. return;
  1801. done:
  1802. atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
  1803. atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
  1804. data->bytes_xfered += nbytes;
  1805. smp_wmb();
  1806. atmci_set_pending(host, EVENT_XFER_COMPLETE);
  1807. }
  1808. static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
  1809. {
  1810. int i;
  1811. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  1812. struct atmel_mci_slot *slot = host->slot[i];
  1813. if (slot && (status & slot->sdio_irq)) {
  1814. mmc_signal_sdio_irq(slot->mmc);
  1815. }
  1816. }
  1817. }
  1818. static irqreturn_t atmci_interrupt(int irq, void *dev_id)
  1819. {
  1820. struct atmel_mci *host = dev_id;
  1821. struct device *dev = host->dev;
  1822. u32 status, mask, pending;
  1823. unsigned int pass_count = 0;
  1824. do {
  1825. status = atmci_readl(host, ATMCI_SR);
  1826. mask = atmci_readl(host, ATMCI_IMR);
  1827. pending = status & mask;
  1828. if (!pending)
  1829. break;
  1830. if (pending & ATMCI_DATA_ERROR_FLAGS) {
  1831. dev_dbg(dev, "IRQ: data error\n");
  1832. atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
  1833. | ATMCI_RXRDY | ATMCI_TXRDY
  1834. | ATMCI_ENDRX | ATMCI_ENDTX
  1835. | ATMCI_RXBUFF | ATMCI_TXBUFE);
  1836. host->data_status = status;
  1837. dev_dbg(dev, "set pending data error\n");
  1838. smp_wmb();
  1839. atmci_set_pending(host, EVENT_DATA_ERROR);
  1840. queue_work(system_bh_wq, &host->bh_work);
  1841. }
  1842. if (pending & ATMCI_TXBUFE) {
  1843. dev_dbg(dev, "IRQ: tx buffer empty\n");
  1844. atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
  1845. atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
  1846. /*
  1847. * We can receive this interruption before having configured
  1848. * the second pdc buffer, so we need to reconfigure first and
  1849. * second buffers again
  1850. */
  1851. if (host->data_size) {
  1852. atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
  1853. atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
  1854. atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
  1855. } else {
  1856. atmci_pdc_complete(host);
  1857. }
  1858. } else if (pending & ATMCI_ENDTX) {
  1859. dev_dbg(dev, "IRQ: end of tx buffer\n");
  1860. atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
  1861. if (host->data_size) {
  1862. atmci_pdc_set_single_buf(host,
  1863. XFER_TRANSMIT, PDC_SECOND_BUF);
  1864. atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
  1865. }
  1866. }
  1867. if (pending & ATMCI_RXBUFF) {
  1868. dev_dbg(dev, "IRQ: rx buffer full\n");
  1869. atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
  1870. atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
  1871. /*
  1872. * We can receive this interruption before having configured
  1873. * the second pdc buffer, so we need to reconfigure first and
  1874. * second buffers again
  1875. */
  1876. if (host->data_size) {
  1877. atmci_pdc_set_both_buf(host, XFER_RECEIVE);
  1878. atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
  1879. atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
  1880. } else {
  1881. atmci_pdc_complete(host);
  1882. }
  1883. } else if (pending & ATMCI_ENDRX) {
  1884. dev_dbg(dev, "IRQ: end of rx buffer\n");
  1885. atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
  1886. if (host->data_size) {
  1887. atmci_pdc_set_single_buf(host,
  1888. XFER_RECEIVE, PDC_SECOND_BUF);
  1889. atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
  1890. }
  1891. }
  1892. /*
  1893. * First mci IPs, so mainly the ones having pdc, have some
  1894. * issues with the notbusy signal. You can't get it after
  1895. * data transmission if you have not sent a stop command.
  1896. * The appropriate workaround is to use the BLKE signal.
  1897. */
  1898. if (pending & ATMCI_BLKE) {
  1899. dev_dbg(dev, "IRQ: blke\n");
  1900. atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
  1901. smp_wmb();
  1902. dev_dbg(dev, "set pending notbusy\n");
  1903. atmci_set_pending(host, EVENT_NOTBUSY);
  1904. queue_work(system_bh_wq, &host->bh_work);
  1905. }
  1906. if (pending & ATMCI_NOTBUSY) {
  1907. dev_dbg(dev, "IRQ: not_busy\n");
  1908. atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
  1909. smp_wmb();
  1910. dev_dbg(dev, "set pending notbusy\n");
  1911. atmci_set_pending(host, EVENT_NOTBUSY);
  1912. queue_work(system_bh_wq, &host->bh_work);
  1913. }
  1914. if (pending & ATMCI_RXRDY)
  1915. atmci_read_data_pio(host);
  1916. if (pending & ATMCI_TXRDY)
  1917. atmci_write_data_pio(host);
  1918. if (pending & ATMCI_CMDRDY) {
  1919. dev_dbg(dev, "IRQ: cmd ready\n");
  1920. atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
  1921. host->cmd_status = status;
  1922. smp_wmb();
  1923. dev_dbg(dev, "set pending cmd rdy\n");
  1924. atmci_set_pending(host, EVENT_CMD_RDY);
  1925. queue_work(system_bh_wq, &host->bh_work);
  1926. }
  1927. if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
  1928. atmci_sdio_interrupt(host, status);
  1929. } while (pass_count++ < 5);
  1930. return pass_count ? IRQ_HANDLED : IRQ_NONE;
  1931. }
  1932. static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
  1933. {
  1934. struct atmel_mci_slot *slot = dev_id;
  1935. /*
  1936. * Disable interrupts until the pin has stabilized and check
  1937. * the state then. Use mod_timer() since we may be in the
  1938. * middle of the timer routine when this interrupt triggers.
  1939. */
  1940. disable_irq_nosync(irq);
  1941. mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
  1942. return IRQ_HANDLED;
  1943. }
  1944. static int atmci_init_slot(struct atmel_mci *host,
  1945. struct mci_slot_pdata *slot_data, unsigned int id,
  1946. u32 sdc_reg, u32 sdio_irq)
  1947. {
  1948. struct device *dev = host->dev;
  1949. struct mmc_host *mmc;
  1950. struct atmel_mci_slot *slot;
  1951. int ret;
  1952. mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), dev);
  1953. if (!mmc)
  1954. return -ENOMEM;
  1955. slot = mmc_priv(mmc);
  1956. slot->mmc = mmc;
  1957. slot->host = host;
  1958. slot->detect_pin = slot_data->detect_pin;
  1959. slot->wp_pin = slot_data->wp_pin;
  1960. slot->sdc_reg = sdc_reg;
  1961. slot->sdio_irq = sdio_irq;
  1962. dev_dbg(&mmc->class_dev,
  1963. "slot[%u]: bus_width=%u, detect_pin=%d, "
  1964. "detect_is_active_high=%s, wp_pin=%d\n",
  1965. id, slot_data->bus_width, desc_to_gpio(slot_data->detect_pin),
  1966. !gpiod_is_active_low(slot_data->detect_pin) ? "true" : "false",
  1967. desc_to_gpio(slot_data->wp_pin));
  1968. mmc->ops = &atmci_ops;
  1969. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
  1970. mmc->f_max = host->bus_hz / 2;
  1971. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1972. if (sdio_irq)
  1973. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1974. if (host->caps.has_highspeed)
  1975. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1976. /*
  1977. * Without the read/write proof capability, it is strongly suggested to
  1978. * use only one bit for data to prevent fifo underruns and overruns
  1979. * which will corrupt data.
  1980. */
  1981. if ((slot_data->bus_width >= 4) && host->caps.has_rwproof) {
  1982. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1983. if (slot_data->bus_width >= 8)
  1984. mmc->caps |= MMC_CAP_8_BIT_DATA;
  1985. }
  1986. if (atmci_get_version(host) < 0x200) {
  1987. mmc->max_segs = 256;
  1988. mmc->max_blk_size = 4095;
  1989. mmc->max_blk_count = 256;
  1990. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1991. mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
  1992. } else {
  1993. mmc->max_segs = 64;
  1994. mmc->max_req_size = 32768 * 512;
  1995. mmc->max_blk_size = 32768;
  1996. mmc->max_blk_count = 512;
  1997. }
  1998. /* Assume card is present initially */
  1999. set_bit(ATMCI_CARD_PRESENT, &slot->flags);
  2000. if (slot->detect_pin) {
  2001. if (!gpiod_get_value_cansleep(slot->detect_pin))
  2002. clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
  2003. } else {
  2004. dev_dbg(&mmc->class_dev, "no detect pin available\n");
  2005. }
  2006. if (!slot->detect_pin) {
  2007. if (slot_data->non_removable)
  2008. mmc->caps |= MMC_CAP_NONREMOVABLE;
  2009. else
  2010. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2011. }
  2012. if (!slot->wp_pin)
  2013. dev_dbg(&mmc->class_dev, "no WP pin available\n");
  2014. host->slot[id] = slot;
  2015. mmc_regulator_get_supply(mmc);
  2016. ret = mmc_add_host(mmc);
  2017. if (ret) {
  2018. mmc_free_host(mmc);
  2019. return ret;
  2020. }
  2021. if (slot->detect_pin) {
  2022. timer_setup(&slot->detect_timer, atmci_detect_change, 0);
  2023. ret = request_irq(gpiod_to_irq(slot->detect_pin),
  2024. atmci_detect_interrupt,
  2025. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  2026. "mmc-detect", slot);
  2027. if (ret) {
  2028. dev_dbg(&mmc->class_dev,
  2029. "could not request IRQ %d for detect pin\n",
  2030. gpiod_to_irq(slot->detect_pin));
  2031. slot->detect_pin = NULL;
  2032. }
  2033. }
  2034. atmci_init_debugfs(slot);
  2035. return 0;
  2036. }
  2037. static void atmci_cleanup_slot(struct atmel_mci_slot *slot,
  2038. unsigned int id)
  2039. {
  2040. /* Debugfs stuff is cleaned up by mmc core */
  2041. set_bit(ATMCI_SHUTDOWN, &slot->flags);
  2042. smp_wmb();
  2043. mmc_remove_host(slot->mmc);
  2044. if (slot->detect_pin) {
  2045. free_irq(gpiod_to_irq(slot->detect_pin), slot);
  2046. del_timer_sync(&slot->detect_timer);
  2047. }
  2048. slot->host->slot[id] = NULL;
  2049. mmc_free_host(slot->mmc);
  2050. }
  2051. static int atmci_configure_dma(struct atmel_mci *host)
  2052. {
  2053. struct device *dev = host->dev;
  2054. host->dma.chan = dma_request_chan(dev, "rxtx");
  2055. if (IS_ERR(host->dma.chan))
  2056. return PTR_ERR(host->dma.chan);
  2057. dev_info(dev, "using %s for DMA transfers\n", dma_chan_name(host->dma.chan));
  2058. host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
  2059. host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2060. host->dma_conf.src_maxburst = 1;
  2061. host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
  2062. host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2063. host->dma_conf.dst_maxburst = 1;
  2064. host->dma_conf.device_fc = false;
  2065. return 0;
  2066. }
  2067. /*
  2068. * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
  2069. * HSMCI provides DMA support and a new config register but no more supports
  2070. * PDC.
  2071. */
  2072. static void atmci_get_cap(struct atmel_mci *host)
  2073. {
  2074. struct device *dev = host->dev;
  2075. unsigned int version;
  2076. version = atmci_get_version(host);
  2077. dev_info(dev, "version: 0x%x\n", version);
  2078. host->caps.has_dma_conf_reg = false;
  2079. host->caps.has_pdc = true;
  2080. host->caps.has_cfg_reg = false;
  2081. host->caps.has_cstor_reg = false;
  2082. host->caps.has_highspeed = false;
  2083. host->caps.has_rwproof = false;
  2084. host->caps.has_odd_clk_div = false;
  2085. host->caps.has_bad_data_ordering = true;
  2086. host->caps.need_reset_after_xfer = true;
  2087. host->caps.need_blksz_mul_4 = true;
  2088. host->caps.need_notbusy_for_read_ops = false;
  2089. /* keep only major version number */
  2090. switch (version & 0xf00) {
  2091. case 0x600:
  2092. case 0x500:
  2093. host->caps.has_odd_clk_div = true;
  2094. fallthrough;
  2095. case 0x400:
  2096. case 0x300:
  2097. host->caps.has_dma_conf_reg = true;
  2098. host->caps.has_pdc = false;
  2099. host->caps.has_cfg_reg = true;
  2100. host->caps.has_cstor_reg = true;
  2101. host->caps.has_highspeed = true;
  2102. fallthrough;
  2103. case 0x200:
  2104. host->caps.has_rwproof = true;
  2105. host->caps.need_blksz_mul_4 = false;
  2106. host->caps.need_notbusy_for_read_ops = true;
  2107. fallthrough;
  2108. case 0x100:
  2109. host->caps.has_bad_data_ordering = false;
  2110. host->caps.need_reset_after_xfer = false;
  2111. fallthrough;
  2112. case 0x0:
  2113. break;
  2114. default:
  2115. host->caps.has_pdc = false;
  2116. dev_warn(dev, "Unmanaged mci version, set minimum capabilities\n");
  2117. break;
  2118. }
  2119. }
  2120. static int atmci_probe(struct platform_device *pdev)
  2121. {
  2122. struct device *dev = &pdev->dev;
  2123. struct atmel_mci *host;
  2124. struct resource *regs;
  2125. unsigned int nr_slots;
  2126. int irq;
  2127. int ret, i;
  2128. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2129. if (!regs)
  2130. return -ENXIO;
  2131. irq = platform_get_irq(pdev, 0);
  2132. if (irq < 0)
  2133. return irq;
  2134. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  2135. if (!host)
  2136. return -ENOMEM;
  2137. host->dev = dev;
  2138. spin_lock_init(&host->lock);
  2139. INIT_LIST_HEAD(&host->queue);
  2140. ret = atmci_of_init(host);
  2141. if (ret)
  2142. return dev_err_probe(dev, ret, "Slot information not available\n");
  2143. host->mck = devm_clk_get(dev, "mci_clk");
  2144. if (IS_ERR(host->mck))
  2145. return PTR_ERR(host->mck);
  2146. host->regs = devm_ioremap(dev, regs->start, resource_size(regs));
  2147. if (!host->regs)
  2148. return -ENOMEM;
  2149. ret = clk_prepare_enable(host->mck);
  2150. if (ret)
  2151. return ret;
  2152. atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
  2153. host->bus_hz = clk_get_rate(host->mck);
  2154. host->mapbase = regs->start;
  2155. INIT_WORK(&host->bh_work, atmci_work_func);
  2156. ret = request_irq(irq, atmci_interrupt, 0, dev_name(dev), host);
  2157. if (ret) {
  2158. clk_disable_unprepare(host->mck);
  2159. return ret;
  2160. }
  2161. /* Get MCI capabilities and set operations according to it */
  2162. atmci_get_cap(host);
  2163. ret = atmci_configure_dma(host);
  2164. if (ret == -EPROBE_DEFER) {
  2165. clk_disable_unprepare(host->mck);
  2166. goto err_dma_probe_defer;
  2167. }
  2168. if (ret == 0) {
  2169. host->prepare_data = &atmci_prepare_data_dma;
  2170. host->submit_data = &atmci_submit_data_dma;
  2171. host->stop_transfer = &atmci_stop_transfer_dma;
  2172. } else if (host->caps.has_pdc) {
  2173. dev_info(dev, "using PDC\n");
  2174. host->prepare_data = &atmci_prepare_data_pdc;
  2175. host->submit_data = &atmci_submit_data_pdc;
  2176. host->stop_transfer = &atmci_stop_transfer_pdc;
  2177. } else {
  2178. dev_info(dev, "using PIO\n");
  2179. host->prepare_data = &atmci_prepare_data;
  2180. host->submit_data = &atmci_submit_data;
  2181. host->stop_transfer = &atmci_stop_transfer;
  2182. }
  2183. platform_set_drvdata(pdev, host);
  2184. timer_setup(&host->timer, atmci_timeout_timer, 0);
  2185. pm_runtime_get_noresume(dev);
  2186. pm_runtime_set_active(dev);
  2187. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
  2188. pm_runtime_use_autosuspend(dev);
  2189. pm_runtime_enable(dev);
  2190. /* We need at least one slot to succeed */
  2191. nr_slots = 0;
  2192. ret = -ENODEV;
  2193. if (host->pdata[0].bus_width) {
  2194. ret = atmci_init_slot(host, &host->pdata[0],
  2195. 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
  2196. if (!ret) {
  2197. nr_slots++;
  2198. host->buf_size = host->slot[0]->mmc->max_req_size;
  2199. }
  2200. }
  2201. if (host->pdata[1].bus_width) {
  2202. ret = atmci_init_slot(host, &host->pdata[1],
  2203. 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
  2204. if (!ret) {
  2205. nr_slots++;
  2206. if (host->slot[1]->mmc->max_req_size > host->buf_size)
  2207. host->buf_size =
  2208. host->slot[1]->mmc->max_req_size;
  2209. }
  2210. }
  2211. if (!nr_slots) {
  2212. dev_err_probe(dev, ret, "init failed: no slot defined\n");
  2213. goto err_init_slot;
  2214. }
  2215. if (!host->caps.has_rwproof) {
  2216. host->buffer = dma_alloc_coherent(dev, host->buf_size,
  2217. &host->buf_phys_addr,
  2218. GFP_KERNEL);
  2219. if (!host->buffer) {
  2220. ret = dev_err_probe(dev, -ENOMEM, "buffer allocation failed\n");
  2221. goto err_dma_alloc;
  2222. }
  2223. }
  2224. dev_info(dev, "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
  2225. host->mapbase, irq, nr_slots);
  2226. pm_runtime_mark_last_busy(dev);
  2227. pm_runtime_put_autosuspend(dev);
  2228. return 0;
  2229. err_dma_alloc:
  2230. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  2231. if (host->slot[i])
  2232. atmci_cleanup_slot(host->slot[i], i);
  2233. }
  2234. err_init_slot:
  2235. clk_disable_unprepare(host->mck);
  2236. pm_runtime_disable(dev);
  2237. pm_runtime_put_noidle(dev);
  2238. del_timer_sync(&host->timer);
  2239. if (!IS_ERR(host->dma.chan))
  2240. dma_release_channel(host->dma.chan);
  2241. err_dma_probe_defer:
  2242. free_irq(irq, host);
  2243. return ret;
  2244. }
  2245. static void atmci_remove(struct platform_device *pdev)
  2246. {
  2247. struct atmel_mci *host = platform_get_drvdata(pdev);
  2248. struct device *dev = &pdev->dev;
  2249. unsigned int i;
  2250. pm_runtime_get_sync(dev);
  2251. if (host->buffer)
  2252. dma_free_coherent(dev, host->buf_size, host->buffer, host->buf_phys_addr);
  2253. for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
  2254. if (host->slot[i])
  2255. atmci_cleanup_slot(host->slot[i], i);
  2256. }
  2257. atmci_writel(host, ATMCI_IDR, ~0UL);
  2258. atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
  2259. atmci_readl(host, ATMCI_SR);
  2260. del_timer_sync(&host->timer);
  2261. if (!IS_ERR(host->dma.chan))
  2262. dma_release_channel(host->dma.chan);
  2263. free_irq(platform_get_irq(pdev, 0), host);
  2264. clk_disable_unprepare(host->mck);
  2265. pm_runtime_disable(dev);
  2266. pm_runtime_put_noidle(dev);
  2267. }
  2268. #ifdef CONFIG_PM
  2269. static int atmci_runtime_suspend(struct device *dev)
  2270. {
  2271. struct atmel_mci *host = dev_get_drvdata(dev);
  2272. clk_disable_unprepare(host->mck);
  2273. pinctrl_pm_select_sleep_state(dev);
  2274. return 0;
  2275. }
  2276. static int atmci_runtime_resume(struct device *dev)
  2277. {
  2278. struct atmel_mci *host = dev_get_drvdata(dev);
  2279. pinctrl_select_default_state(dev);
  2280. return clk_prepare_enable(host->mck);
  2281. }
  2282. #endif
  2283. static const struct dev_pm_ops atmci_dev_pm_ops = {
  2284. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  2285. pm_runtime_force_resume)
  2286. SET_RUNTIME_PM_OPS(atmci_runtime_suspend, atmci_runtime_resume, NULL)
  2287. };
  2288. static struct platform_driver atmci_driver = {
  2289. .probe = atmci_probe,
  2290. .remove_new = atmci_remove,
  2291. .driver = {
  2292. .name = "atmel_mci",
  2293. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  2294. .of_match_table = atmci_dt_ids,
  2295. .pm = &atmci_dev_pm_ops,
  2296. },
  2297. };
  2298. module_platform_driver(atmci_driver);
  2299. MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
  2300. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  2301. MODULE_LICENSE("GPL v2");