atmel-mci.c 74 KB

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