spi-pl022.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  1. /*
  2. * A driver for the ARM PL022 PrimeCell SSP/SPI bus master.
  3. *
  4. * Copyright (C) 2008-2012 ST-Ericsson AB
  5. * Copyright (C) 2006 STMicroelectronics Pvt. Ltd.
  6. *
  7. * Author: Linus Walleij <linus.walleij@stericsson.com>
  8. *
  9. * Initial version inspired by:
  10. * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
  11. * Initial adoption to PL022 by:
  12. * Sachin Verma <sachin.verma@st.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/ioport.h>
  28. #include <linux/errno.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/delay.h>
  32. #include <linux/clk.h>
  33. #include <linux/err.h>
  34. #include <linux/amba/bus.h>
  35. #include <linux/amba/pl022.h>
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/pm_runtime.h>
  42. #include <linux/gpio.h>
  43. #include <linux/of_gpio.h>
  44. #include <linux/pinctrl/consumer.h>
  45. /*
  46. * This macro is used to define some register default values.
  47. * reg is masked with mask, the OR:ed with an (again masked)
  48. * val shifted sb steps to the left.
  49. */
  50. #define SSP_WRITE_BITS(reg, val, mask, sb) \
  51. ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
  52. /*
  53. * This macro is also used to define some default values.
  54. * It will just shift val by sb steps to the left and mask
  55. * the result with mask.
  56. */
  57. #define GEN_MASK_BITS(val, mask, sb) \
  58. (((val)<<(sb)) & (mask))
  59. #define DRIVE_TX 0
  60. #define DO_NOT_DRIVE_TX 1
  61. #define DO_NOT_QUEUE_DMA 0
  62. #define QUEUE_DMA 1
  63. #define RX_TRANSFER 1
  64. #define TX_TRANSFER 2
  65. /*
  66. * Macros to access SSP Registers with their offsets
  67. */
  68. #define SSP_CR0(r) (r + 0x000)
  69. #define SSP_CR1(r) (r + 0x004)
  70. #define SSP_DR(r) (r + 0x008)
  71. #define SSP_SR(r) (r + 0x00C)
  72. #define SSP_CPSR(r) (r + 0x010)
  73. #define SSP_IMSC(r) (r + 0x014)
  74. #define SSP_RIS(r) (r + 0x018)
  75. #define SSP_MIS(r) (r + 0x01C)
  76. #define SSP_ICR(r) (r + 0x020)
  77. #define SSP_DMACR(r) (r + 0x024)
  78. #define SSP_CSR(r) (r + 0x030) /* vendor extension */
  79. #define SSP_ITCR(r) (r + 0x080)
  80. #define SSP_ITIP(r) (r + 0x084)
  81. #define SSP_ITOP(r) (r + 0x088)
  82. #define SSP_TDR(r) (r + 0x08C)
  83. #define SSP_PID0(r) (r + 0xFE0)
  84. #define SSP_PID1(r) (r + 0xFE4)
  85. #define SSP_PID2(r) (r + 0xFE8)
  86. #define SSP_PID3(r) (r + 0xFEC)
  87. #define SSP_CID0(r) (r + 0xFF0)
  88. #define SSP_CID1(r) (r + 0xFF4)
  89. #define SSP_CID2(r) (r + 0xFF8)
  90. #define SSP_CID3(r) (r + 0xFFC)
  91. /*
  92. * SSP Control Register 0 - SSP_CR0
  93. */
  94. #define SSP_CR0_MASK_DSS (0x0FUL << 0)
  95. #define SSP_CR0_MASK_FRF (0x3UL << 4)
  96. #define SSP_CR0_MASK_SPO (0x1UL << 6)
  97. #define SSP_CR0_MASK_SPH (0x1UL << 7)
  98. #define SSP_CR0_MASK_SCR (0xFFUL << 8)
  99. /*
  100. * The ST version of this block moves som bits
  101. * in SSP_CR0 and extends it to 32 bits
  102. */
  103. #define SSP_CR0_MASK_DSS_ST (0x1FUL << 0)
  104. #define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5)
  105. #define SSP_CR0_MASK_CSS_ST (0x1FUL << 16)
  106. #define SSP_CR0_MASK_FRF_ST (0x3UL << 21)
  107. /*
  108. * SSP Control Register 0 - SSP_CR1
  109. */
  110. #define SSP_CR1_MASK_LBM (0x1UL << 0)
  111. #define SSP_CR1_MASK_SSE (0x1UL << 1)
  112. #define SSP_CR1_MASK_MS (0x1UL << 2)
  113. #define SSP_CR1_MASK_SOD (0x1UL << 3)
  114. /*
  115. * The ST version of this block adds some bits
  116. * in SSP_CR1
  117. */
  118. #define SSP_CR1_MASK_RENDN_ST (0x1UL << 4)
  119. #define SSP_CR1_MASK_TENDN_ST (0x1UL << 5)
  120. #define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6)
  121. #define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
  122. #define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
  123. /* This one is only in the PL023 variant */
  124. #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
  125. /*
  126. * SSP Status Register - SSP_SR
  127. */
  128. #define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
  129. #define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
  130. #define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
  131. #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
  132. #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
  133. /*
  134. * SSP Clock Prescale Register - SSP_CPSR
  135. */
  136. #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
  137. /*
  138. * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
  139. */
  140. #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
  141. #define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
  142. #define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
  143. #define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
  144. /*
  145. * SSP Raw Interrupt Status Register - SSP_RIS
  146. */
  147. /* Receive Overrun Raw Interrupt status */
  148. #define SSP_RIS_MASK_RORRIS (0x1UL << 0)
  149. /* Receive Timeout Raw Interrupt status */
  150. #define SSP_RIS_MASK_RTRIS (0x1UL << 1)
  151. /* Receive FIFO Raw Interrupt status */
  152. #define SSP_RIS_MASK_RXRIS (0x1UL << 2)
  153. /* Transmit FIFO Raw Interrupt status */
  154. #define SSP_RIS_MASK_TXRIS (0x1UL << 3)
  155. /*
  156. * SSP Masked Interrupt Status Register - SSP_MIS
  157. */
  158. /* Receive Overrun Masked Interrupt status */
  159. #define SSP_MIS_MASK_RORMIS (0x1UL << 0)
  160. /* Receive Timeout Masked Interrupt status */
  161. #define SSP_MIS_MASK_RTMIS (0x1UL << 1)
  162. /* Receive FIFO Masked Interrupt status */
  163. #define SSP_MIS_MASK_RXMIS (0x1UL << 2)
  164. /* Transmit FIFO Masked Interrupt status */
  165. #define SSP_MIS_MASK_TXMIS (0x1UL << 3)
  166. /*
  167. * SSP Interrupt Clear Register - SSP_ICR
  168. */
  169. /* Receive Overrun Raw Clear Interrupt bit */
  170. #define SSP_ICR_MASK_RORIC (0x1UL << 0)
  171. /* Receive Timeout Clear Interrupt bit */
  172. #define SSP_ICR_MASK_RTIC (0x1UL << 1)
  173. /*
  174. * SSP DMA Control Register - SSP_DMACR
  175. */
  176. /* Receive DMA Enable bit */
  177. #define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
  178. /* Transmit DMA Enable bit */
  179. #define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
  180. /*
  181. * SSP Chip Select Control Register - SSP_CSR
  182. * (vendor extension)
  183. */
  184. #define SSP_CSR_CSVALUE_MASK (0x1FUL << 0)
  185. /*
  186. * SSP Integration Test control Register - SSP_ITCR
  187. */
  188. #define SSP_ITCR_MASK_ITEN (0x1UL << 0)
  189. #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
  190. /*
  191. * SSP Integration Test Input Register - SSP_ITIP
  192. */
  193. #define ITIP_MASK_SSPRXD (0x1UL << 0)
  194. #define ITIP_MASK_SSPFSSIN (0x1UL << 1)
  195. #define ITIP_MASK_SSPCLKIN (0x1UL << 2)
  196. #define ITIP_MASK_RXDMAC (0x1UL << 3)
  197. #define ITIP_MASK_TXDMAC (0x1UL << 4)
  198. #define ITIP_MASK_SSPTXDIN (0x1UL << 5)
  199. /*
  200. * SSP Integration Test output Register - SSP_ITOP
  201. */
  202. #define ITOP_MASK_SSPTXD (0x1UL << 0)
  203. #define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
  204. #define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
  205. #define ITOP_MASK_SSPOEn (0x1UL << 3)
  206. #define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
  207. #define ITOP_MASK_RORINTR (0x1UL << 5)
  208. #define ITOP_MASK_RTINTR (0x1UL << 6)
  209. #define ITOP_MASK_RXINTR (0x1UL << 7)
  210. #define ITOP_MASK_TXINTR (0x1UL << 8)
  211. #define ITOP_MASK_INTR (0x1UL << 9)
  212. #define ITOP_MASK_RXDMABREQ (0x1UL << 10)
  213. #define ITOP_MASK_RXDMASREQ (0x1UL << 11)
  214. #define ITOP_MASK_TXDMABREQ (0x1UL << 12)
  215. #define ITOP_MASK_TXDMASREQ (0x1UL << 13)
  216. /*
  217. * SSP Test Data Register - SSP_TDR
  218. */
  219. #define TDR_MASK_TESTDATA (0xFFFFFFFF)
  220. /*
  221. * Message State
  222. * we use the spi_message.state (void *) pointer to
  223. * hold a single state value, that's why all this
  224. * (void *) casting is done here.
  225. */
  226. #define STATE_START ((void *) 0)
  227. #define STATE_RUNNING ((void *) 1)
  228. #define STATE_DONE ((void *) 2)
  229. #define STATE_ERROR ((void *) -1)
  230. /*
  231. * SSP State - Whether Enabled or Disabled
  232. */
  233. #define SSP_DISABLED (0)
  234. #define SSP_ENABLED (1)
  235. /*
  236. * SSP DMA State - Whether DMA Enabled or Disabled
  237. */
  238. #define SSP_DMA_DISABLED (0)
  239. #define SSP_DMA_ENABLED (1)
  240. /*
  241. * SSP Clock Defaults
  242. */
  243. #define SSP_DEFAULT_CLKRATE 0x2
  244. #define SSP_DEFAULT_PRESCALE 0x40
  245. /*
  246. * SSP Clock Parameter ranges
  247. */
  248. #define CPSDVR_MIN 0x02
  249. #define CPSDVR_MAX 0xFE
  250. #define SCR_MIN 0x00
  251. #define SCR_MAX 0xFF
  252. /*
  253. * SSP Interrupt related Macros
  254. */
  255. #define DEFAULT_SSP_REG_IMSC 0x0UL
  256. #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
  257. #define ENABLE_ALL_INTERRUPTS ( \
  258. SSP_IMSC_MASK_RORIM | \
  259. SSP_IMSC_MASK_RTIM | \
  260. SSP_IMSC_MASK_RXIM | \
  261. SSP_IMSC_MASK_TXIM \
  262. )
  263. #define CLEAR_ALL_INTERRUPTS 0x3
  264. #define SPI_POLLING_TIMEOUT 1000
  265. /*
  266. * The type of reading going on on this chip
  267. */
  268. enum ssp_reading {
  269. READING_NULL,
  270. READING_U8,
  271. READING_U16,
  272. READING_U32
  273. };
  274. /**
  275. * The type of writing going on on this chip
  276. */
  277. enum ssp_writing {
  278. WRITING_NULL,
  279. WRITING_U8,
  280. WRITING_U16,
  281. WRITING_U32
  282. };
  283. /**
  284. * struct vendor_data - vendor-specific config parameters
  285. * for PL022 derivates
  286. * @fifodepth: depth of FIFOs (both)
  287. * @max_bpw: maximum number of bits per word
  288. * @unidir: supports unidirection transfers
  289. * @extended_cr: 32 bit wide control register 0 with extra
  290. * features and extra features in CR1 as found in the ST variants
  291. * @pl023: supports a subset of the ST extensions called "PL023"
  292. * @internal_cs_ctrl: supports chip select control register
  293. */
  294. struct vendor_data {
  295. int fifodepth;
  296. int max_bpw;
  297. bool unidir;
  298. bool extended_cr;
  299. bool pl023;
  300. bool loopback;
  301. bool internal_cs_ctrl;
  302. };
  303. /**
  304. * struct pl022 - This is the private SSP driver data structure
  305. * @adev: AMBA device model hookup
  306. * @vendor: vendor data for the IP block
  307. * @phybase: the physical memory where the SSP device resides
  308. * @virtbase: the virtual memory where the SSP is mapped
  309. * @clk: outgoing clock "SPICLK" for the SPI bus
  310. * @master: SPI framework hookup
  311. * @master_info: controller-specific data from machine setup
  312. * @kworker: thread struct for message pump
  313. * @kworker_task: pointer to task for message pump kworker thread
  314. * @pump_messages: work struct for scheduling work to the message pump
  315. * @queue_lock: spinlock to syncronise access to message queue
  316. * @queue: message queue
  317. * @busy: message pump is busy
  318. * @running: message pump is running
  319. * @pump_transfers: Tasklet used in Interrupt Transfer mode
  320. * @cur_msg: Pointer to current spi_message being processed
  321. * @cur_transfer: Pointer to current spi_transfer
  322. * @cur_chip: pointer to current clients chip(assigned from controller_state)
  323. * @next_msg_cs_active: the next message in the queue has been examined
  324. * and it was found that it uses the same chip select as the previous
  325. * message, so we left it active after the previous transfer, and it's
  326. * active already.
  327. * @tx: current position in TX buffer to be read
  328. * @tx_end: end position in TX buffer to be read
  329. * @rx: current position in RX buffer to be written
  330. * @rx_end: end position in RX buffer to be written
  331. * @read: the type of read currently going on
  332. * @write: the type of write currently going on
  333. * @exp_fifo_level: expected FIFO level
  334. * @dma_rx_channel: optional channel for RX DMA
  335. * @dma_tx_channel: optional channel for TX DMA
  336. * @sgt_rx: scattertable for the RX transfer
  337. * @sgt_tx: scattertable for the TX transfer
  338. * @dummypage: a dummy page used for driving data on the bus with DMA
  339. * @cur_cs: current chip select (gpio)
  340. * @chipselects: list of chipselects (gpios)
  341. */
  342. struct pl022 {
  343. struct amba_device *adev;
  344. struct vendor_data *vendor;
  345. resource_size_t phybase;
  346. void __iomem *virtbase;
  347. struct clk *clk;
  348. struct spi_master *master;
  349. struct pl022_ssp_controller *master_info;
  350. /* Message per-transfer pump */
  351. struct tasklet_struct pump_transfers;
  352. struct spi_message *cur_msg;
  353. struct spi_transfer *cur_transfer;
  354. struct chip_data *cur_chip;
  355. bool next_msg_cs_active;
  356. void *tx;
  357. void *tx_end;
  358. void *rx;
  359. void *rx_end;
  360. enum ssp_reading read;
  361. enum ssp_writing write;
  362. u32 exp_fifo_level;
  363. enum ssp_rx_level_trig rx_lev_trig;
  364. enum ssp_tx_level_trig tx_lev_trig;
  365. /* DMA settings */
  366. #ifdef CONFIG_DMA_ENGINE
  367. struct dma_chan *dma_rx_channel;
  368. struct dma_chan *dma_tx_channel;
  369. struct sg_table sgt_rx;
  370. struct sg_table sgt_tx;
  371. char *dummypage;
  372. bool dma_running;
  373. #endif
  374. int cur_cs;
  375. int *chipselects;
  376. };
  377. /**
  378. * struct chip_data - To maintain runtime state of SSP for each client chip
  379. * @cr0: Value of control register CR0 of SSP - on later ST variants this
  380. * register is 32 bits wide rather than just 16
  381. * @cr1: Value of control register CR1 of SSP
  382. * @dmacr: Value of DMA control Register of SSP
  383. * @cpsr: Value of Clock prescale register
  384. * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
  385. * @enable_dma: Whether to enable DMA or not
  386. * @read: function ptr to be used to read when doing xfer for this chip
  387. * @write: function ptr to be used to write when doing xfer for this chip
  388. * @cs_control: chip select callback provided by chip
  389. * @xfer_type: polling/interrupt/DMA
  390. *
  391. * Runtime state of the SSP controller, maintained per chip,
  392. * This would be set according to the current message that would be served
  393. */
  394. struct chip_data {
  395. u32 cr0;
  396. u16 cr1;
  397. u16 dmacr;
  398. u16 cpsr;
  399. u8 n_bytes;
  400. bool enable_dma;
  401. enum ssp_reading read;
  402. enum ssp_writing write;
  403. void (*cs_control) (u32 command);
  404. int xfer_type;
  405. };
  406. /**
  407. * null_cs_control - Dummy chip select function
  408. * @command: select/delect the chip
  409. *
  410. * If no chip select function is provided by client this is used as dummy
  411. * chip select
  412. */
  413. static void null_cs_control(u32 command)
  414. {
  415. pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
  416. }
  417. /**
  418. * internal_cs_control - Control chip select signals via SSP_CSR.
  419. * @pl022: SSP driver private data structure
  420. * @command: select/delect the chip
  421. *
  422. * Used on controller with internal chip select control via SSP_CSR register
  423. * (vendor extension). Each of the 5 LSB in the register controls one chip
  424. * select signal.
  425. */
  426. static void internal_cs_control(struct pl022 *pl022, u32 command)
  427. {
  428. u32 tmp;
  429. tmp = readw(SSP_CSR(pl022->virtbase));
  430. if (command == SSP_CHIP_SELECT)
  431. tmp &= ~BIT(pl022->cur_cs);
  432. else
  433. tmp |= BIT(pl022->cur_cs);
  434. writew(tmp, SSP_CSR(pl022->virtbase));
  435. }
  436. static void pl022_cs_control(struct pl022 *pl022, u32 command)
  437. {
  438. if (pl022->vendor->internal_cs_ctrl)
  439. internal_cs_control(pl022, command);
  440. else if (gpio_is_valid(pl022->cur_cs))
  441. gpio_set_value(pl022->cur_cs, command);
  442. else
  443. pl022->cur_chip->cs_control(command);
  444. }
  445. /**
  446. * giveback - current spi_message is over, schedule next message and call
  447. * callback of this message. Assumes that caller already
  448. * set message->status; dma and pio irqs are blocked
  449. * @pl022: SSP driver private data structure
  450. */
  451. static void giveback(struct pl022 *pl022)
  452. {
  453. struct spi_transfer *last_transfer;
  454. pl022->next_msg_cs_active = false;
  455. last_transfer = list_last_entry(&pl022->cur_msg->transfers,
  456. struct spi_transfer, transfer_list);
  457. /* Delay if requested before any change in chip select */
  458. if (last_transfer->delay_usecs)
  459. /*
  460. * FIXME: This runs in interrupt context.
  461. * Is this really smart?
  462. */
  463. udelay(last_transfer->delay_usecs);
  464. if (!last_transfer->cs_change) {
  465. struct spi_message *next_msg;
  466. /*
  467. * cs_change was not set. We can keep the chip select
  468. * enabled if there is message in the queue and it is
  469. * for the same spi device.
  470. *
  471. * We cannot postpone this until pump_messages, because
  472. * after calling msg->complete (below) the driver that
  473. * sent the current message could be unloaded, which
  474. * could invalidate the cs_control() callback...
  475. */
  476. /* get a pointer to the next message, if any */
  477. next_msg = spi_get_next_queued_message(pl022->master);
  478. /*
  479. * see if the next and current messages point
  480. * to the same spi device.
  481. */
  482. if (next_msg && next_msg->spi != pl022->cur_msg->spi)
  483. next_msg = NULL;
  484. if (!next_msg || pl022->cur_msg->state == STATE_ERROR)
  485. pl022_cs_control(pl022, SSP_CHIP_DESELECT);
  486. else
  487. pl022->next_msg_cs_active = true;
  488. }
  489. pl022->cur_msg = NULL;
  490. pl022->cur_transfer = NULL;
  491. pl022->cur_chip = NULL;
  492. /* disable the SPI/SSP operation */
  493. writew((readw(SSP_CR1(pl022->virtbase)) &
  494. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  495. spi_finalize_current_message(pl022->master);
  496. }
  497. /**
  498. * flush - flush the FIFO to reach a clean state
  499. * @pl022: SSP driver private data structure
  500. */
  501. static int flush(struct pl022 *pl022)
  502. {
  503. unsigned long limit = loops_per_jiffy << 1;
  504. dev_dbg(&pl022->adev->dev, "flush\n");
  505. do {
  506. while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  507. readw(SSP_DR(pl022->virtbase));
  508. } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
  509. pl022->exp_fifo_level = 0;
  510. return limit;
  511. }
  512. /**
  513. * restore_state - Load configuration of current chip
  514. * @pl022: SSP driver private data structure
  515. */
  516. static void restore_state(struct pl022 *pl022)
  517. {
  518. struct chip_data *chip = pl022->cur_chip;
  519. if (pl022->vendor->extended_cr)
  520. writel(chip->cr0, SSP_CR0(pl022->virtbase));
  521. else
  522. writew(chip->cr0, SSP_CR0(pl022->virtbase));
  523. writew(chip->cr1, SSP_CR1(pl022->virtbase));
  524. writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
  525. writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
  526. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  527. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  528. }
  529. /*
  530. * Default SSP Register Values
  531. */
  532. #define DEFAULT_SSP_REG_CR0 ( \
  533. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
  534. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
  535. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  536. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  537. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  538. )
  539. /* ST versions have slightly different bit layout */
  540. #define DEFAULT_SSP_REG_CR0_ST ( \
  541. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  542. GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
  543. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  544. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  545. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
  546. GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
  547. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
  548. )
  549. /* The PL023 version is slightly different again */
  550. #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
  551. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  552. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  553. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  554. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  555. )
  556. #define DEFAULT_SSP_REG_CR1 ( \
  557. GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
  558. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  559. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  560. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
  561. )
  562. /* ST versions extend this register to use all 16 bits */
  563. #define DEFAULT_SSP_REG_CR1_ST ( \
  564. DEFAULT_SSP_REG_CR1 | \
  565. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  566. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  567. GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
  568. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  569. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
  570. )
  571. /*
  572. * The PL023 variant has further differences: no loopback mode, no microwire
  573. * support, and a new clock feedback delay setting.
  574. */
  575. #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
  576. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  577. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  578. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
  579. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  580. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  581. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  582. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
  583. GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
  584. )
  585. #define DEFAULT_SSP_REG_CPSR ( \
  586. GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
  587. )
  588. #define DEFAULT_SSP_REG_DMACR (\
  589. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
  590. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
  591. )
  592. /**
  593. * load_ssp_default_config - Load default configuration for SSP
  594. * @pl022: SSP driver private data structure
  595. */
  596. static void load_ssp_default_config(struct pl022 *pl022)
  597. {
  598. if (pl022->vendor->pl023) {
  599. writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
  600. writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
  601. } else if (pl022->vendor->extended_cr) {
  602. writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
  603. writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
  604. } else {
  605. writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
  606. writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
  607. }
  608. writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
  609. writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
  610. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  611. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  612. }
  613. /**
  614. * This will write to TX and read from RX according to the parameters
  615. * set in pl022.
  616. */
  617. static void readwriter(struct pl022 *pl022)
  618. {
  619. /*
  620. * The FIFO depth is different between primecell variants.
  621. * I believe filling in too much in the FIFO might cause
  622. * errons in 8bit wide transfers on ARM variants (just 8 words
  623. * FIFO, means only 8x8 = 64 bits in FIFO) at least.
  624. *
  625. * To prevent this issue, the TX FIFO is only filled to the
  626. * unused RX FIFO fill length, regardless of what the TX
  627. * FIFO status flag indicates.
  628. */
  629. dev_dbg(&pl022->adev->dev,
  630. "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
  631. __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
  632. /* Read as much as you can */
  633. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  634. && (pl022->rx < pl022->rx_end)) {
  635. switch (pl022->read) {
  636. case READING_NULL:
  637. readw(SSP_DR(pl022->virtbase));
  638. break;
  639. case READING_U8:
  640. *(u8 *) (pl022->rx) =
  641. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  642. break;
  643. case READING_U16:
  644. *(u16 *) (pl022->rx) =
  645. (u16) readw(SSP_DR(pl022->virtbase));
  646. break;
  647. case READING_U32:
  648. *(u32 *) (pl022->rx) =
  649. readl(SSP_DR(pl022->virtbase));
  650. break;
  651. }
  652. pl022->rx += (pl022->cur_chip->n_bytes);
  653. pl022->exp_fifo_level--;
  654. }
  655. /*
  656. * Write as much as possible up to the RX FIFO size
  657. */
  658. while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
  659. && (pl022->tx < pl022->tx_end)) {
  660. switch (pl022->write) {
  661. case WRITING_NULL:
  662. writew(0x0, SSP_DR(pl022->virtbase));
  663. break;
  664. case WRITING_U8:
  665. writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
  666. break;
  667. case WRITING_U16:
  668. writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
  669. break;
  670. case WRITING_U32:
  671. writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
  672. break;
  673. }
  674. pl022->tx += (pl022->cur_chip->n_bytes);
  675. pl022->exp_fifo_level++;
  676. /*
  677. * This inner reader takes care of things appearing in the RX
  678. * FIFO as we're transmitting. This will happen a lot since the
  679. * clock starts running when you put things into the TX FIFO,
  680. * and then things are continuously clocked into the RX FIFO.
  681. */
  682. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  683. && (pl022->rx < pl022->rx_end)) {
  684. switch (pl022->read) {
  685. case READING_NULL:
  686. readw(SSP_DR(pl022->virtbase));
  687. break;
  688. case READING_U8:
  689. *(u8 *) (pl022->rx) =
  690. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  691. break;
  692. case READING_U16:
  693. *(u16 *) (pl022->rx) =
  694. (u16) readw(SSP_DR(pl022->virtbase));
  695. break;
  696. case READING_U32:
  697. *(u32 *) (pl022->rx) =
  698. readl(SSP_DR(pl022->virtbase));
  699. break;
  700. }
  701. pl022->rx += (pl022->cur_chip->n_bytes);
  702. pl022->exp_fifo_level--;
  703. }
  704. }
  705. /*
  706. * When we exit here the TX FIFO should be full and the RX FIFO
  707. * should be empty
  708. */
  709. }
  710. /**
  711. * next_transfer - Move to the Next transfer in the current spi message
  712. * @pl022: SSP driver private data structure
  713. *
  714. * This function moves though the linked list of spi transfers in the
  715. * current spi message and returns with the state of current spi
  716. * message i.e whether its last transfer is done(STATE_DONE) or
  717. * Next transfer is ready(STATE_RUNNING)
  718. */
  719. static void *next_transfer(struct pl022 *pl022)
  720. {
  721. struct spi_message *msg = pl022->cur_msg;
  722. struct spi_transfer *trans = pl022->cur_transfer;
  723. /* Move to next transfer */
  724. if (trans->transfer_list.next != &msg->transfers) {
  725. pl022->cur_transfer =
  726. list_entry(trans->transfer_list.next,
  727. struct spi_transfer, transfer_list);
  728. return STATE_RUNNING;
  729. }
  730. return STATE_DONE;
  731. }
  732. /*
  733. * This DMA functionality is only compiled in if we have
  734. * access to the generic DMA devices/DMA engine.
  735. */
  736. #ifdef CONFIG_DMA_ENGINE
  737. static void unmap_free_dma_scatter(struct pl022 *pl022)
  738. {
  739. /* Unmap and free the SG tables */
  740. dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
  741. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  742. dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
  743. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  744. sg_free_table(&pl022->sgt_rx);
  745. sg_free_table(&pl022->sgt_tx);
  746. }
  747. static void dma_callback(void *data)
  748. {
  749. struct pl022 *pl022 = data;
  750. struct spi_message *msg = pl022->cur_msg;
  751. BUG_ON(!pl022->sgt_rx.sgl);
  752. #ifdef VERBOSE_DEBUG
  753. /*
  754. * Optionally dump out buffers to inspect contents, this is
  755. * good if you want to convince yourself that the loopback
  756. * read/write contents are the same, when adopting to a new
  757. * DMA engine.
  758. */
  759. {
  760. struct scatterlist *sg;
  761. unsigned int i;
  762. dma_sync_sg_for_cpu(&pl022->adev->dev,
  763. pl022->sgt_rx.sgl,
  764. pl022->sgt_rx.nents,
  765. DMA_FROM_DEVICE);
  766. for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
  767. dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
  768. print_hex_dump(KERN_ERR, "SPI RX: ",
  769. DUMP_PREFIX_OFFSET,
  770. 16,
  771. 1,
  772. sg_virt(sg),
  773. sg_dma_len(sg),
  774. 1);
  775. }
  776. for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
  777. dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
  778. print_hex_dump(KERN_ERR, "SPI TX: ",
  779. DUMP_PREFIX_OFFSET,
  780. 16,
  781. 1,
  782. sg_virt(sg),
  783. sg_dma_len(sg),
  784. 1);
  785. }
  786. }
  787. #endif
  788. unmap_free_dma_scatter(pl022);
  789. /* Update total bytes transferred */
  790. msg->actual_length += pl022->cur_transfer->len;
  791. if (pl022->cur_transfer->cs_change)
  792. pl022_cs_control(pl022, SSP_CHIP_DESELECT);
  793. /* Move to next transfer */
  794. msg->state = next_transfer(pl022);
  795. tasklet_schedule(&pl022->pump_transfers);
  796. }
  797. static void setup_dma_scatter(struct pl022 *pl022,
  798. void *buffer,
  799. unsigned int length,
  800. struct sg_table *sgtab)
  801. {
  802. struct scatterlist *sg;
  803. int bytesleft = length;
  804. void *bufp = buffer;
  805. int mapbytes;
  806. int i;
  807. if (buffer) {
  808. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  809. /*
  810. * If there are less bytes left than what fits
  811. * in the current page (plus page alignment offset)
  812. * we just feed in this, else we stuff in as much
  813. * as we can.
  814. */
  815. if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
  816. mapbytes = bytesleft;
  817. else
  818. mapbytes = PAGE_SIZE - offset_in_page(bufp);
  819. sg_set_page(sg, virt_to_page(bufp),
  820. mapbytes, offset_in_page(bufp));
  821. bufp += mapbytes;
  822. bytesleft -= mapbytes;
  823. dev_dbg(&pl022->adev->dev,
  824. "set RX/TX target page @ %p, %d bytes, %d left\n",
  825. bufp, mapbytes, bytesleft);
  826. }
  827. } else {
  828. /* Map the dummy buffer on every page */
  829. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  830. if (bytesleft < PAGE_SIZE)
  831. mapbytes = bytesleft;
  832. else
  833. mapbytes = PAGE_SIZE;
  834. sg_set_page(sg, virt_to_page(pl022->dummypage),
  835. mapbytes, 0);
  836. bytesleft -= mapbytes;
  837. dev_dbg(&pl022->adev->dev,
  838. "set RX/TX to dummy page %d bytes, %d left\n",
  839. mapbytes, bytesleft);
  840. }
  841. }
  842. BUG_ON(bytesleft);
  843. }
  844. /**
  845. * configure_dma - configures the channels for the next transfer
  846. * @pl022: SSP driver's private data structure
  847. */
  848. static int configure_dma(struct pl022 *pl022)
  849. {
  850. struct dma_slave_config rx_conf = {
  851. .src_addr = SSP_DR(pl022->phybase),
  852. .direction = DMA_DEV_TO_MEM,
  853. .device_fc = false,
  854. };
  855. struct dma_slave_config tx_conf = {
  856. .dst_addr = SSP_DR(pl022->phybase),
  857. .direction = DMA_MEM_TO_DEV,
  858. .device_fc = false,
  859. };
  860. unsigned int pages;
  861. int ret;
  862. int rx_sglen, tx_sglen;
  863. struct dma_chan *rxchan = pl022->dma_rx_channel;
  864. struct dma_chan *txchan = pl022->dma_tx_channel;
  865. struct dma_async_tx_descriptor *rxdesc;
  866. struct dma_async_tx_descriptor *txdesc;
  867. /* Check that the channels are available */
  868. if (!rxchan || !txchan)
  869. return -ENODEV;
  870. /*
  871. * If supplied, the DMA burstsize should equal the FIFO trigger level.
  872. * Notice that the DMA engine uses one-to-one mapping. Since we can
  873. * not trigger on 2 elements this needs explicit mapping rather than
  874. * calculation.
  875. */
  876. switch (pl022->rx_lev_trig) {
  877. case SSP_RX_1_OR_MORE_ELEM:
  878. rx_conf.src_maxburst = 1;
  879. break;
  880. case SSP_RX_4_OR_MORE_ELEM:
  881. rx_conf.src_maxburst = 4;
  882. break;
  883. case SSP_RX_8_OR_MORE_ELEM:
  884. rx_conf.src_maxburst = 8;
  885. break;
  886. case SSP_RX_16_OR_MORE_ELEM:
  887. rx_conf.src_maxburst = 16;
  888. break;
  889. case SSP_RX_32_OR_MORE_ELEM:
  890. rx_conf.src_maxburst = 32;
  891. break;
  892. default:
  893. rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1;
  894. break;
  895. }
  896. switch (pl022->tx_lev_trig) {
  897. case SSP_TX_1_OR_MORE_EMPTY_LOC:
  898. tx_conf.dst_maxburst = 1;
  899. break;
  900. case SSP_TX_4_OR_MORE_EMPTY_LOC:
  901. tx_conf.dst_maxburst = 4;
  902. break;
  903. case SSP_TX_8_OR_MORE_EMPTY_LOC:
  904. tx_conf.dst_maxburst = 8;
  905. break;
  906. case SSP_TX_16_OR_MORE_EMPTY_LOC:
  907. tx_conf.dst_maxburst = 16;
  908. break;
  909. case SSP_TX_32_OR_MORE_EMPTY_LOC:
  910. tx_conf.dst_maxburst = 32;
  911. break;
  912. default:
  913. tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1;
  914. break;
  915. }
  916. switch (pl022->read) {
  917. case READING_NULL:
  918. /* Use the same as for writing */
  919. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  920. break;
  921. case READING_U8:
  922. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  923. break;
  924. case READING_U16:
  925. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  926. break;
  927. case READING_U32:
  928. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  929. break;
  930. }
  931. switch (pl022->write) {
  932. case WRITING_NULL:
  933. /* Use the same as for reading */
  934. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  935. break;
  936. case WRITING_U8:
  937. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  938. break;
  939. case WRITING_U16:
  940. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  941. break;
  942. case WRITING_U32:
  943. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  944. break;
  945. }
  946. /* SPI pecularity: we need to read and write the same width */
  947. if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  948. rx_conf.src_addr_width = tx_conf.dst_addr_width;
  949. if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  950. tx_conf.dst_addr_width = rx_conf.src_addr_width;
  951. BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
  952. dmaengine_slave_config(rxchan, &rx_conf);
  953. dmaengine_slave_config(txchan, &tx_conf);
  954. /* Create sglists for the transfers */
  955. pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE);
  956. dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
  957. ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC);
  958. if (ret)
  959. goto err_alloc_rx_sg;
  960. ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC);
  961. if (ret)
  962. goto err_alloc_tx_sg;
  963. /* Fill in the scatterlists for the RX+TX buffers */
  964. setup_dma_scatter(pl022, pl022->rx,
  965. pl022->cur_transfer->len, &pl022->sgt_rx);
  966. setup_dma_scatter(pl022, pl022->tx,
  967. pl022->cur_transfer->len, &pl022->sgt_tx);
  968. /* Map DMA buffers */
  969. rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  970. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  971. if (!rx_sglen)
  972. goto err_rx_sgmap;
  973. tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  974. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  975. if (!tx_sglen)
  976. goto err_tx_sgmap;
  977. /* Send both scatterlists */
  978. rxdesc = dmaengine_prep_slave_sg(rxchan,
  979. pl022->sgt_rx.sgl,
  980. rx_sglen,
  981. DMA_DEV_TO_MEM,
  982. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  983. if (!rxdesc)
  984. goto err_rxdesc;
  985. txdesc = dmaengine_prep_slave_sg(txchan,
  986. pl022->sgt_tx.sgl,
  987. tx_sglen,
  988. DMA_MEM_TO_DEV,
  989. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  990. if (!txdesc)
  991. goto err_txdesc;
  992. /* Put the callback on the RX transfer only, that should finish last */
  993. rxdesc->callback = dma_callback;
  994. rxdesc->callback_param = pl022;
  995. /* Submit and fire RX and TX with TX last so we're ready to read! */
  996. dmaengine_submit(rxdesc);
  997. dmaengine_submit(txdesc);
  998. dma_async_issue_pending(rxchan);
  999. dma_async_issue_pending(txchan);
  1000. pl022->dma_running = true;
  1001. return 0;
  1002. err_txdesc:
  1003. dmaengine_terminate_all(txchan);
  1004. err_rxdesc:
  1005. dmaengine_terminate_all(rxchan);
  1006. dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  1007. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  1008. err_tx_sgmap:
  1009. dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  1010. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  1011. err_rx_sgmap:
  1012. sg_free_table(&pl022->sgt_tx);
  1013. err_alloc_tx_sg:
  1014. sg_free_table(&pl022->sgt_rx);
  1015. err_alloc_rx_sg:
  1016. return -ENOMEM;
  1017. }
  1018. static int pl022_dma_probe(struct pl022 *pl022)
  1019. {
  1020. dma_cap_mask_t mask;
  1021. /* Try to acquire a generic DMA engine slave channel */
  1022. dma_cap_zero(mask);
  1023. dma_cap_set(DMA_SLAVE, mask);
  1024. /*
  1025. * We need both RX and TX channels to do DMA, else do none
  1026. * of them.
  1027. */
  1028. pl022->dma_rx_channel = dma_request_channel(mask,
  1029. pl022->master_info->dma_filter,
  1030. pl022->master_info->dma_rx_param);
  1031. if (!pl022->dma_rx_channel) {
  1032. dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n");
  1033. goto err_no_rxchan;
  1034. }
  1035. pl022->dma_tx_channel = dma_request_channel(mask,
  1036. pl022->master_info->dma_filter,
  1037. pl022->master_info->dma_tx_param);
  1038. if (!pl022->dma_tx_channel) {
  1039. dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n");
  1040. goto err_no_txchan;
  1041. }
  1042. pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1043. if (!pl022->dummypage)
  1044. goto err_no_dummypage;
  1045. dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
  1046. dma_chan_name(pl022->dma_rx_channel),
  1047. dma_chan_name(pl022->dma_tx_channel));
  1048. return 0;
  1049. err_no_dummypage:
  1050. dma_release_channel(pl022->dma_tx_channel);
  1051. err_no_txchan:
  1052. dma_release_channel(pl022->dma_rx_channel);
  1053. pl022->dma_rx_channel = NULL;
  1054. err_no_rxchan:
  1055. dev_err(&pl022->adev->dev,
  1056. "Failed to work in dma mode, work without dma!\n");
  1057. return -ENODEV;
  1058. }
  1059. static int pl022_dma_autoprobe(struct pl022 *pl022)
  1060. {
  1061. struct device *dev = &pl022->adev->dev;
  1062. struct dma_chan *chan;
  1063. int err;
  1064. /* automatically configure DMA channels from platform, normally using DT */
  1065. chan = dma_request_slave_channel_reason(dev, "rx");
  1066. if (IS_ERR(chan)) {
  1067. err = PTR_ERR(chan);
  1068. goto err_no_rxchan;
  1069. }
  1070. pl022->dma_rx_channel = chan;
  1071. chan = dma_request_slave_channel_reason(dev, "tx");
  1072. if (IS_ERR(chan)) {
  1073. err = PTR_ERR(chan);
  1074. goto err_no_txchan;
  1075. }
  1076. pl022->dma_tx_channel = chan;
  1077. pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1078. if (!pl022->dummypage) {
  1079. err = -ENOMEM;
  1080. goto err_no_dummypage;
  1081. }
  1082. return 0;
  1083. err_no_dummypage:
  1084. dma_release_channel(pl022->dma_tx_channel);
  1085. pl022->dma_tx_channel = NULL;
  1086. err_no_txchan:
  1087. dma_release_channel(pl022->dma_rx_channel);
  1088. pl022->dma_rx_channel = NULL;
  1089. err_no_rxchan:
  1090. return err;
  1091. }
  1092. static void terminate_dma(struct pl022 *pl022)
  1093. {
  1094. struct dma_chan *rxchan = pl022->dma_rx_channel;
  1095. struct dma_chan *txchan = pl022->dma_tx_channel;
  1096. dmaengine_terminate_all(rxchan);
  1097. dmaengine_terminate_all(txchan);
  1098. unmap_free_dma_scatter(pl022);
  1099. pl022->dma_running = false;
  1100. }
  1101. static void pl022_dma_remove(struct pl022 *pl022)
  1102. {
  1103. if (pl022->dma_running)
  1104. terminate_dma(pl022);
  1105. if (pl022->dma_tx_channel)
  1106. dma_release_channel(pl022->dma_tx_channel);
  1107. if (pl022->dma_rx_channel)
  1108. dma_release_channel(pl022->dma_rx_channel);
  1109. kfree(pl022->dummypage);
  1110. }
  1111. #else
  1112. static inline int configure_dma(struct pl022 *pl022)
  1113. {
  1114. return -ENODEV;
  1115. }
  1116. static inline int pl022_dma_autoprobe(struct pl022 *pl022)
  1117. {
  1118. return 0;
  1119. }
  1120. static inline int pl022_dma_probe(struct pl022 *pl022)
  1121. {
  1122. return 0;
  1123. }
  1124. static inline void pl022_dma_remove(struct pl022 *pl022)
  1125. {
  1126. }
  1127. #endif
  1128. /**
  1129. * pl022_interrupt_handler - Interrupt handler for SSP controller
  1130. *
  1131. * This function handles interrupts generated for an interrupt based transfer.
  1132. * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
  1133. * current message's state as STATE_ERROR and schedule the tasklet
  1134. * pump_transfers which will do the postprocessing of the current message by
  1135. * calling giveback(). Otherwise it reads data from RX FIFO till there is no
  1136. * more data, and writes data in TX FIFO till it is not full. If we complete
  1137. * the transfer we move to the next transfer and schedule the tasklet.
  1138. */
  1139. static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
  1140. {
  1141. struct pl022 *pl022 = dev_id;
  1142. struct spi_message *msg = pl022->cur_msg;
  1143. u16 irq_status = 0;
  1144. if (unlikely(!msg)) {
  1145. dev_err(&pl022->adev->dev,
  1146. "bad message state in interrupt handler");
  1147. /* Never fail */
  1148. return IRQ_HANDLED;
  1149. }
  1150. /* Read the Interrupt Status Register */
  1151. irq_status = readw(SSP_MIS(pl022->virtbase));
  1152. if (unlikely(!irq_status))
  1153. return IRQ_NONE;
  1154. /*
  1155. * This handles the FIFO interrupts, the timeout
  1156. * interrupts are flatly ignored, they cannot be
  1157. * trusted.
  1158. */
  1159. if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
  1160. /*
  1161. * Overrun interrupt - bail out since our Data has been
  1162. * corrupted
  1163. */
  1164. dev_err(&pl022->adev->dev, "FIFO overrun\n");
  1165. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
  1166. dev_err(&pl022->adev->dev,
  1167. "RXFIFO is full\n");
  1168. /*
  1169. * Disable and clear interrupts, disable SSP,
  1170. * mark message with bad status so it can be
  1171. * retried.
  1172. */
  1173. writew(DISABLE_ALL_INTERRUPTS,
  1174. SSP_IMSC(pl022->virtbase));
  1175. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1176. writew((readw(SSP_CR1(pl022->virtbase)) &
  1177. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1178. msg->state = STATE_ERROR;
  1179. /* Schedule message queue handler */
  1180. tasklet_schedule(&pl022->pump_transfers);
  1181. return IRQ_HANDLED;
  1182. }
  1183. readwriter(pl022);
  1184. if (pl022->tx == pl022->tx_end) {
  1185. /* Disable Transmit interrupt, enable receive interrupt */
  1186. writew((readw(SSP_IMSC(pl022->virtbase)) &
  1187. ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM,
  1188. SSP_IMSC(pl022->virtbase));
  1189. }
  1190. /*
  1191. * Since all transactions must write as much as shall be read,
  1192. * we can conclude the entire transaction once RX is complete.
  1193. * At this point, all TX will always be finished.
  1194. */
  1195. if (pl022->rx >= pl022->rx_end) {
  1196. writew(DISABLE_ALL_INTERRUPTS,
  1197. SSP_IMSC(pl022->virtbase));
  1198. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1199. if (unlikely(pl022->rx > pl022->rx_end)) {
  1200. dev_warn(&pl022->adev->dev, "read %u surplus "
  1201. "bytes (did you request an odd "
  1202. "number of bytes on a 16bit bus?)\n",
  1203. (u32) (pl022->rx - pl022->rx_end));
  1204. }
  1205. /* Update total bytes transferred */
  1206. msg->actual_length += pl022->cur_transfer->len;
  1207. if (pl022->cur_transfer->cs_change)
  1208. pl022_cs_control(pl022, SSP_CHIP_DESELECT);
  1209. /* Move to next transfer */
  1210. msg->state = next_transfer(pl022);
  1211. tasklet_schedule(&pl022->pump_transfers);
  1212. return IRQ_HANDLED;
  1213. }
  1214. return IRQ_HANDLED;
  1215. }
  1216. /**
  1217. * This sets up the pointers to memory for the next message to
  1218. * send out on the SPI bus.
  1219. */
  1220. static int set_up_next_transfer(struct pl022 *pl022,
  1221. struct spi_transfer *transfer)
  1222. {
  1223. int residue;
  1224. /* Sanity check the message for this bus width */
  1225. residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
  1226. if (unlikely(residue != 0)) {
  1227. dev_err(&pl022->adev->dev,
  1228. "message of %u bytes to transmit but the current "
  1229. "chip bus has a data width of %u bytes!\n",
  1230. pl022->cur_transfer->len,
  1231. pl022->cur_chip->n_bytes);
  1232. dev_err(&pl022->adev->dev, "skipping this message\n");
  1233. return -EIO;
  1234. }
  1235. pl022->tx = (void *)transfer->tx_buf;
  1236. pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
  1237. pl022->rx = (void *)transfer->rx_buf;
  1238. pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
  1239. pl022->write =
  1240. pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
  1241. pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
  1242. return 0;
  1243. }
  1244. /**
  1245. * pump_transfers - Tasklet function which schedules next transfer
  1246. * when running in interrupt or DMA transfer mode.
  1247. * @data: SSP driver private data structure
  1248. *
  1249. */
  1250. static void pump_transfers(unsigned long data)
  1251. {
  1252. struct pl022 *pl022 = (struct pl022 *) data;
  1253. struct spi_message *message = NULL;
  1254. struct spi_transfer *transfer = NULL;
  1255. struct spi_transfer *previous = NULL;
  1256. /* Get current state information */
  1257. message = pl022->cur_msg;
  1258. transfer = pl022->cur_transfer;
  1259. /* Handle for abort */
  1260. if (message->state == STATE_ERROR) {
  1261. message->status = -EIO;
  1262. giveback(pl022);
  1263. return;
  1264. }
  1265. /* Handle end of message */
  1266. if (message->state == STATE_DONE) {
  1267. message->status = 0;
  1268. giveback(pl022);
  1269. return;
  1270. }
  1271. /* Delay if requested at end of transfer before CS change */
  1272. if (message->state == STATE_RUNNING) {
  1273. previous = list_entry(transfer->transfer_list.prev,
  1274. struct spi_transfer,
  1275. transfer_list);
  1276. if (previous->delay_usecs)
  1277. /*
  1278. * FIXME: This runs in interrupt context.
  1279. * Is this really smart?
  1280. */
  1281. udelay(previous->delay_usecs);
  1282. /* Reselect chip select only if cs_change was requested */
  1283. if (previous->cs_change)
  1284. pl022_cs_control(pl022, SSP_CHIP_SELECT);
  1285. } else {
  1286. /* STATE_START */
  1287. message->state = STATE_RUNNING;
  1288. }
  1289. if (set_up_next_transfer(pl022, transfer)) {
  1290. message->state = STATE_ERROR;
  1291. message->status = -EIO;
  1292. giveback(pl022);
  1293. return;
  1294. }
  1295. /* Flush the FIFOs and let's go! */
  1296. flush(pl022);
  1297. if (pl022->cur_chip->enable_dma) {
  1298. if (configure_dma(pl022)) {
  1299. dev_dbg(&pl022->adev->dev,
  1300. "configuration of DMA failed, fall back to interrupt mode\n");
  1301. goto err_config_dma;
  1302. }
  1303. return;
  1304. }
  1305. err_config_dma:
  1306. /* enable all interrupts except RX */
  1307. writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase));
  1308. }
  1309. static void do_interrupt_dma_transfer(struct pl022 *pl022)
  1310. {
  1311. /*
  1312. * Default is to enable all interrupts except RX -
  1313. * this will be enabled once TX is complete
  1314. */
  1315. u32 irqflags = (u32)(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM);
  1316. /* Enable target chip, if not already active */
  1317. if (!pl022->next_msg_cs_active)
  1318. pl022_cs_control(pl022, SSP_CHIP_SELECT);
  1319. if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
  1320. /* Error path */
  1321. pl022->cur_msg->state = STATE_ERROR;
  1322. pl022->cur_msg->status = -EIO;
  1323. giveback(pl022);
  1324. return;
  1325. }
  1326. /* If we're using DMA, set up DMA here */
  1327. if (pl022->cur_chip->enable_dma) {
  1328. /* Configure DMA transfer */
  1329. if (configure_dma(pl022)) {
  1330. dev_dbg(&pl022->adev->dev,
  1331. "configuration of DMA failed, fall back to interrupt mode\n");
  1332. goto err_config_dma;
  1333. }
  1334. /* Disable interrupts in DMA mode, IRQ from DMA controller */
  1335. irqflags = DISABLE_ALL_INTERRUPTS;
  1336. }
  1337. err_config_dma:
  1338. /* Enable SSP, turn on interrupts */
  1339. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1340. SSP_CR1(pl022->virtbase));
  1341. writew(irqflags, SSP_IMSC(pl022->virtbase));
  1342. }
  1343. static void do_polling_transfer(struct pl022 *pl022)
  1344. {
  1345. struct spi_message *message = NULL;
  1346. struct spi_transfer *transfer = NULL;
  1347. struct spi_transfer *previous = NULL;
  1348. struct chip_data *chip;
  1349. unsigned long time, timeout;
  1350. chip = pl022->cur_chip;
  1351. message = pl022->cur_msg;
  1352. while (message->state != STATE_DONE) {
  1353. /* Handle for abort */
  1354. if (message->state == STATE_ERROR)
  1355. break;
  1356. transfer = pl022->cur_transfer;
  1357. /* Delay if requested at end of transfer */
  1358. if (message->state == STATE_RUNNING) {
  1359. previous =
  1360. list_entry(transfer->transfer_list.prev,
  1361. struct spi_transfer, transfer_list);
  1362. if (previous->delay_usecs)
  1363. udelay(previous->delay_usecs);
  1364. if (previous->cs_change)
  1365. pl022_cs_control(pl022, SSP_CHIP_SELECT);
  1366. } else {
  1367. /* STATE_START */
  1368. message->state = STATE_RUNNING;
  1369. if (!pl022->next_msg_cs_active)
  1370. pl022_cs_control(pl022, SSP_CHIP_SELECT);
  1371. }
  1372. /* Configuration Changing Per Transfer */
  1373. if (set_up_next_transfer(pl022, transfer)) {
  1374. /* Error path */
  1375. message->state = STATE_ERROR;
  1376. break;
  1377. }
  1378. /* Flush FIFOs and enable SSP */
  1379. flush(pl022);
  1380. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1381. SSP_CR1(pl022->virtbase));
  1382. dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
  1383. timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT);
  1384. while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) {
  1385. time = jiffies;
  1386. readwriter(pl022);
  1387. if (time_after(time, timeout)) {
  1388. dev_warn(&pl022->adev->dev,
  1389. "%s: timeout!\n", __func__);
  1390. message->state = STATE_ERROR;
  1391. goto out;
  1392. }
  1393. cpu_relax();
  1394. }
  1395. /* Update total byte transferred */
  1396. message->actual_length += pl022->cur_transfer->len;
  1397. if (pl022->cur_transfer->cs_change)
  1398. pl022_cs_control(pl022, SSP_CHIP_DESELECT);
  1399. /* Move to next transfer */
  1400. message->state = next_transfer(pl022);
  1401. }
  1402. out:
  1403. /* Handle end of message */
  1404. if (message->state == STATE_DONE)
  1405. message->status = 0;
  1406. else
  1407. message->status = -EIO;
  1408. giveback(pl022);
  1409. return;
  1410. }
  1411. static int pl022_transfer_one_message(struct spi_master *master,
  1412. struct spi_message *msg)
  1413. {
  1414. struct pl022 *pl022 = spi_master_get_devdata(master);
  1415. /* Initial message state */
  1416. pl022->cur_msg = msg;
  1417. msg->state = STATE_START;
  1418. pl022->cur_transfer = list_entry(msg->transfers.next,
  1419. struct spi_transfer, transfer_list);
  1420. /* Setup the SPI using the per chip configuration */
  1421. pl022->cur_chip = spi_get_ctldata(msg->spi);
  1422. pl022->cur_cs = pl022->chipselects[msg->spi->chip_select];
  1423. restore_state(pl022);
  1424. flush(pl022);
  1425. if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
  1426. do_polling_transfer(pl022);
  1427. else
  1428. do_interrupt_dma_transfer(pl022);
  1429. return 0;
  1430. }
  1431. static int pl022_unprepare_transfer_hardware(struct spi_master *master)
  1432. {
  1433. struct pl022 *pl022 = spi_master_get_devdata(master);
  1434. /* nothing more to do - disable spi/ssp and power off */
  1435. writew((readw(SSP_CR1(pl022->virtbase)) &
  1436. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1437. return 0;
  1438. }
  1439. static int verify_controller_parameters(struct pl022 *pl022,
  1440. struct pl022_config_chip const *chip_info)
  1441. {
  1442. if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
  1443. || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
  1444. dev_err(&pl022->adev->dev,
  1445. "interface is configured incorrectly\n");
  1446. return -EINVAL;
  1447. }
  1448. if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
  1449. (!pl022->vendor->unidir)) {
  1450. dev_err(&pl022->adev->dev,
  1451. "unidirectional mode not supported in this "
  1452. "hardware version\n");
  1453. return -EINVAL;
  1454. }
  1455. if ((chip_info->hierarchy != SSP_MASTER)
  1456. && (chip_info->hierarchy != SSP_SLAVE)) {
  1457. dev_err(&pl022->adev->dev,
  1458. "hierarchy is configured incorrectly\n");
  1459. return -EINVAL;
  1460. }
  1461. if ((chip_info->com_mode != INTERRUPT_TRANSFER)
  1462. && (chip_info->com_mode != DMA_TRANSFER)
  1463. && (chip_info->com_mode != POLLING_TRANSFER)) {
  1464. dev_err(&pl022->adev->dev,
  1465. "Communication mode is configured incorrectly\n");
  1466. return -EINVAL;
  1467. }
  1468. switch (chip_info->rx_lev_trig) {
  1469. case SSP_RX_1_OR_MORE_ELEM:
  1470. case SSP_RX_4_OR_MORE_ELEM:
  1471. case SSP_RX_8_OR_MORE_ELEM:
  1472. /* These are always OK, all variants can handle this */
  1473. break;
  1474. case SSP_RX_16_OR_MORE_ELEM:
  1475. if (pl022->vendor->fifodepth < 16) {
  1476. dev_err(&pl022->adev->dev,
  1477. "RX FIFO Trigger Level is configured incorrectly\n");
  1478. return -EINVAL;
  1479. }
  1480. break;
  1481. case SSP_RX_32_OR_MORE_ELEM:
  1482. if (pl022->vendor->fifodepth < 32) {
  1483. dev_err(&pl022->adev->dev,
  1484. "RX FIFO Trigger Level is configured incorrectly\n");
  1485. return -EINVAL;
  1486. }
  1487. break;
  1488. default:
  1489. dev_err(&pl022->adev->dev,
  1490. "RX FIFO Trigger Level is configured incorrectly\n");
  1491. return -EINVAL;
  1492. }
  1493. switch (chip_info->tx_lev_trig) {
  1494. case SSP_TX_1_OR_MORE_EMPTY_LOC:
  1495. case SSP_TX_4_OR_MORE_EMPTY_LOC:
  1496. case SSP_TX_8_OR_MORE_EMPTY_LOC:
  1497. /* These are always OK, all variants can handle this */
  1498. break;
  1499. case SSP_TX_16_OR_MORE_EMPTY_LOC:
  1500. if (pl022->vendor->fifodepth < 16) {
  1501. dev_err(&pl022->adev->dev,
  1502. "TX FIFO Trigger Level is configured incorrectly\n");
  1503. return -EINVAL;
  1504. }
  1505. break;
  1506. case SSP_TX_32_OR_MORE_EMPTY_LOC:
  1507. if (pl022->vendor->fifodepth < 32) {
  1508. dev_err(&pl022->adev->dev,
  1509. "TX FIFO Trigger Level is configured incorrectly\n");
  1510. return -EINVAL;
  1511. }
  1512. break;
  1513. default:
  1514. dev_err(&pl022->adev->dev,
  1515. "TX FIFO Trigger Level is configured incorrectly\n");
  1516. return -EINVAL;
  1517. }
  1518. if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
  1519. if ((chip_info->ctrl_len < SSP_BITS_4)
  1520. || (chip_info->ctrl_len > SSP_BITS_32)) {
  1521. dev_err(&pl022->adev->dev,
  1522. "CTRL LEN is configured incorrectly\n");
  1523. return -EINVAL;
  1524. }
  1525. if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
  1526. && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
  1527. dev_err(&pl022->adev->dev,
  1528. "Wait State is configured incorrectly\n");
  1529. return -EINVAL;
  1530. }
  1531. /* Half duplex is only available in the ST Micro version */
  1532. if (pl022->vendor->extended_cr) {
  1533. if ((chip_info->duplex !=
  1534. SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1535. && (chip_info->duplex !=
  1536. SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
  1537. dev_err(&pl022->adev->dev,
  1538. "Microwire duplex mode is configured incorrectly\n");
  1539. return -EINVAL;
  1540. }
  1541. } else {
  1542. if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1543. dev_err(&pl022->adev->dev,
  1544. "Microwire half duplex mode requested,"
  1545. " but this is only available in the"
  1546. " ST version of PL022\n");
  1547. return -EINVAL;
  1548. }
  1549. }
  1550. return 0;
  1551. }
  1552. static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
  1553. {
  1554. return rate / (cpsdvsr * (1 + scr));
  1555. }
  1556. static int calculate_effective_freq(struct pl022 *pl022, int freq, struct
  1557. ssp_clock_params * clk_freq)
  1558. {
  1559. /* Lets calculate the frequency parameters */
  1560. u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN;
  1561. u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0,
  1562. best_scr = 0, tmp, found = 0;
  1563. rate = clk_get_rate(pl022->clk);
  1564. /* cpsdvscr = 2 & scr 0 */
  1565. max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN);
  1566. /* cpsdvsr = 254 & scr = 255 */
  1567. min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX);
  1568. if (freq > max_tclk)
  1569. dev_warn(&pl022->adev->dev,
  1570. "Max speed that can be programmed is %d Hz, you requested %d\n",
  1571. max_tclk, freq);
  1572. if (freq < min_tclk) {
  1573. dev_err(&pl022->adev->dev,
  1574. "Requested frequency: %d Hz is less than minimum possible %d Hz\n",
  1575. freq, min_tclk);
  1576. return -EINVAL;
  1577. }
  1578. /*
  1579. * best_freq will give closest possible available rate (<= requested
  1580. * freq) for all values of scr & cpsdvsr.
  1581. */
  1582. while ((cpsdvsr <= CPSDVR_MAX) && !found) {
  1583. while (scr <= SCR_MAX) {
  1584. tmp = spi_rate(rate, cpsdvsr, scr);
  1585. if (tmp > freq) {
  1586. /* we need lower freq */
  1587. scr++;
  1588. continue;
  1589. }
  1590. /*
  1591. * If found exact value, mark found and break.
  1592. * If found more closer value, update and break.
  1593. */
  1594. if (tmp > best_freq) {
  1595. best_freq = tmp;
  1596. best_cpsdvsr = cpsdvsr;
  1597. best_scr = scr;
  1598. if (tmp == freq)
  1599. found = 1;
  1600. }
  1601. /*
  1602. * increased scr will give lower rates, which are not
  1603. * required
  1604. */
  1605. break;
  1606. }
  1607. cpsdvsr += 2;
  1608. scr = SCR_MIN;
  1609. }
  1610. WARN(!best_freq, "pl022: Matching cpsdvsr and scr not found for %d Hz rate \n",
  1611. freq);
  1612. clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF);
  1613. clk_freq->scr = (u8) (best_scr & 0xFF);
  1614. dev_dbg(&pl022->adev->dev,
  1615. "SSP Target Frequency is: %u, Effective Frequency is %u\n",
  1616. freq, best_freq);
  1617. dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n",
  1618. clk_freq->cpsdvsr, clk_freq->scr);
  1619. return 0;
  1620. }
  1621. /*
  1622. * A piece of default chip info unless the platform
  1623. * supplies it.
  1624. */
  1625. static const struct pl022_config_chip pl022_default_chip_info = {
  1626. .com_mode = POLLING_TRANSFER,
  1627. .iface = SSP_INTERFACE_MOTOROLA_SPI,
  1628. .hierarchy = SSP_SLAVE,
  1629. .slave_tx_disable = DO_NOT_DRIVE_TX,
  1630. .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
  1631. .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
  1632. .ctrl_len = SSP_BITS_8,
  1633. .wait_state = SSP_MWIRE_WAIT_ZERO,
  1634. .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
  1635. .cs_control = null_cs_control,
  1636. };
  1637. /**
  1638. * pl022_setup - setup function registered to SPI master framework
  1639. * @spi: spi device which is requesting setup
  1640. *
  1641. * This function is registered to the SPI framework for this SPI master
  1642. * controller. If it is the first time when setup is called by this device,
  1643. * this function will initialize the runtime state for this chip and save
  1644. * the same in the device structure. Else it will update the runtime info
  1645. * with the updated chip info. Nothing is really being written to the
  1646. * controller hardware here, that is not done until the actual transfer
  1647. * commence.
  1648. */
  1649. static int pl022_setup(struct spi_device *spi)
  1650. {
  1651. struct pl022_config_chip const *chip_info;
  1652. struct pl022_config_chip chip_info_dt;
  1653. struct chip_data *chip;
  1654. struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0};
  1655. int status = 0;
  1656. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1657. unsigned int bits = spi->bits_per_word;
  1658. u32 tmp;
  1659. struct device_node *np = spi->dev.of_node;
  1660. if (!spi->max_speed_hz)
  1661. return -EINVAL;
  1662. /* Get controller_state if one is supplied */
  1663. chip = spi_get_ctldata(spi);
  1664. if (chip == NULL) {
  1665. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1666. if (!chip)
  1667. return -ENOMEM;
  1668. dev_dbg(&spi->dev,
  1669. "allocated memory for controller's runtime state\n");
  1670. }
  1671. /* Get controller data if one is supplied */
  1672. chip_info = spi->controller_data;
  1673. if (chip_info == NULL) {
  1674. if (np) {
  1675. chip_info_dt = pl022_default_chip_info;
  1676. chip_info_dt.hierarchy = SSP_MASTER;
  1677. of_property_read_u32(np, "pl022,interface",
  1678. &chip_info_dt.iface);
  1679. of_property_read_u32(np, "pl022,com-mode",
  1680. &chip_info_dt.com_mode);
  1681. of_property_read_u32(np, "pl022,rx-level-trig",
  1682. &chip_info_dt.rx_lev_trig);
  1683. of_property_read_u32(np, "pl022,tx-level-trig",
  1684. &chip_info_dt.tx_lev_trig);
  1685. of_property_read_u32(np, "pl022,ctrl-len",
  1686. &chip_info_dt.ctrl_len);
  1687. of_property_read_u32(np, "pl022,wait-state",
  1688. &chip_info_dt.wait_state);
  1689. of_property_read_u32(np, "pl022,duplex",
  1690. &chip_info_dt.duplex);
  1691. chip_info = &chip_info_dt;
  1692. } else {
  1693. chip_info = &pl022_default_chip_info;
  1694. /* spi_board_info.controller_data not is supplied */
  1695. dev_dbg(&spi->dev,
  1696. "using default controller_data settings\n");
  1697. }
  1698. } else
  1699. dev_dbg(&spi->dev,
  1700. "using user supplied controller_data settings\n");
  1701. /*
  1702. * We can override with custom divisors, else we use the board
  1703. * frequency setting
  1704. */
  1705. if ((0 == chip_info->clk_freq.cpsdvsr)
  1706. && (0 == chip_info->clk_freq.scr)) {
  1707. status = calculate_effective_freq(pl022,
  1708. spi->max_speed_hz,
  1709. &clk_freq);
  1710. if (status < 0)
  1711. goto err_config_params;
  1712. } else {
  1713. memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
  1714. if ((clk_freq.cpsdvsr % 2) != 0)
  1715. clk_freq.cpsdvsr =
  1716. clk_freq.cpsdvsr - 1;
  1717. }
  1718. if ((clk_freq.cpsdvsr < CPSDVR_MIN)
  1719. || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
  1720. status = -EINVAL;
  1721. dev_err(&spi->dev,
  1722. "cpsdvsr is configured incorrectly\n");
  1723. goto err_config_params;
  1724. }
  1725. status = verify_controller_parameters(pl022, chip_info);
  1726. if (status) {
  1727. dev_err(&spi->dev, "controller data is incorrect");
  1728. goto err_config_params;
  1729. }
  1730. pl022->rx_lev_trig = chip_info->rx_lev_trig;
  1731. pl022->tx_lev_trig = chip_info->tx_lev_trig;
  1732. /* Now set controller state based on controller data */
  1733. chip->xfer_type = chip_info->com_mode;
  1734. if (!chip_info->cs_control) {
  1735. chip->cs_control = null_cs_control;
  1736. if (!gpio_is_valid(pl022->chipselects[spi->chip_select]))
  1737. dev_warn(&spi->dev,
  1738. "invalid chip select\n");
  1739. } else
  1740. chip->cs_control = chip_info->cs_control;
  1741. /* Check bits per word with vendor specific range */
  1742. if ((bits <= 3) || (bits > pl022->vendor->max_bpw)) {
  1743. status = -ENOTSUPP;
  1744. dev_err(&spi->dev, "illegal data size for this controller!\n");
  1745. dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n",
  1746. pl022->vendor->max_bpw);
  1747. goto err_config_params;
  1748. } else if (bits <= 8) {
  1749. dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
  1750. chip->n_bytes = 1;
  1751. chip->read = READING_U8;
  1752. chip->write = WRITING_U8;
  1753. } else if (bits <= 16) {
  1754. dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
  1755. chip->n_bytes = 2;
  1756. chip->read = READING_U16;
  1757. chip->write = WRITING_U16;
  1758. } else {
  1759. dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
  1760. chip->n_bytes = 4;
  1761. chip->read = READING_U32;
  1762. chip->write = WRITING_U32;
  1763. }
  1764. /* Now Initialize all register settings required for this chip */
  1765. chip->cr0 = 0;
  1766. chip->cr1 = 0;
  1767. chip->dmacr = 0;
  1768. chip->cpsr = 0;
  1769. if ((chip_info->com_mode == DMA_TRANSFER)
  1770. && ((pl022->master_info)->enable_dma)) {
  1771. chip->enable_dma = true;
  1772. dev_dbg(&spi->dev, "DMA mode set in controller state\n");
  1773. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1774. SSP_DMACR_MASK_RXDMAE, 0);
  1775. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1776. SSP_DMACR_MASK_TXDMAE, 1);
  1777. } else {
  1778. chip->enable_dma = false;
  1779. dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
  1780. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1781. SSP_DMACR_MASK_RXDMAE, 0);
  1782. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1783. SSP_DMACR_MASK_TXDMAE, 1);
  1784. }
  1785. chip->cpsr = clk_freq.cpsdvsr;
  1786. /* Special setup for the ST micro extended control registers */
  1787. if (pl022->vendor->extended_cr) {
  1788. u32 etx;
  1789. if (pl022->vendor->pl023) {
  1790. /* These bits are only in the PL023 */
  1791. SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
  1792. SSP_CR1_MASK_FBCLKDEL_ST, 13);
  1793. } else {
  1794. /* These bits are in the PL022 but not PL023 */
  1795. SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
  1796. SSP_CR0_MASK_HALFDUP_ST, 5);
  1797. SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
  1798. SSP_CR0_MASK_CSS_ST, 16);
  1799. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1800. SSP_CR0_MASK_FRF_ST, 21);
  1801. SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
  1802. SSP_CR1_MASK_MWAIT_ST, 6);
  1803. }
  1804. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1805. SSP_CR0_MASK_DSS_ST, 0);
  1806. if (spi->mode & SPI_LSB_FIRST) {
  1807. tmp = SSP_RX_LSB;
  1808. etx = SSP_TX_LSB;
  1809. } else {
  1810. tmp = SSP_RX_MSB;
  1811. etx = SSP_TX_MSB;
  1812. }
  1813. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
  1814. SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
  1815. SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
  1816. SSP_CR1_MASK_RXIFLSEL_ST, 7);
  1817. SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
  1818. SSP_CR1_MASK_TXIFLSEL_ST, 10);
  1819. } else {
  1820. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1821. SSP_CR0_MASK_DSS, 0);
  1822. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1823. SSP_CR0_MASK_FRF, 4);
  1824. }
  1825. /* Stuff that is common for all versions */
  1826. if (spi->mode & SPI_CPOL)
  1827. tmp = SSP_CLK_POL_IDLE_HIGH;
  1828. else
  1829. tmp = SSP_CLK_POL_IDLE_LOW;
  1830. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
  1831. if (spi->mode & SPI_CPHA)
  1832. tmp = SSP_CLK_SECOND_EDGE;
  1833. else
  1834. tmp = SSP_CLK_FIRST_EDGE;
  1835. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
  1836. SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
  1837. /* Loopback is available on all versions except PL023 */
  1838. if (pl022->vendor->loopback) {
  1839. if (spi->mode & SPI_LOOP)
  1840. tmp = LOOPBACK_ENABLED;
  1841. else
  1842. tmp = LOOPBACK_DISABLED;
  1843. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
  1844. }
  1845. SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
  1846. SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
  1847. SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD,
  1848. 3);
  1849. /* Save controller_state */
  1850. spi_set_ctldata(spi, chip);
  1851. return status;
  1852. err_config_params:
  1853. spi_set_ctldata(spi, NULL);
  1854. kfree(chip);
  1855. return status;
  1856. }
  1857. /**
  1858. * pl022_cleanup - cleanup function registered to SPI master framework
  1859. * @spi: spi device which is requesting cleanup
  1860. *
  1861. * This function is registered to the SPI framework for this SPI master
  1862. * controller. It will free the runtime state of chip.
  1863. */
  1864. static void pl022_cleanup(struct spi_device *spi)
  1865. {
  1866. struct chip_data *chip = spi_get_ctldata(spi);
  1867. spi_set_ctldata(spi, NULL);
  1868. kfree(chip);
  1869. }
  1870. static struct pl022_ssp_controller *
  1871. pl022_platform_data_dt_get(struct device *dev)
  1872. {
  1873. struct device_node *np = dev->of_node;
  1874. struct pl022_ssp_controller *pd;
  1875. u32 tmp;
  1876. if (!np) {
  1877. dev_err(dev, "no dt node defined\n");
  1878. return NULL;
  1879. }
  1880. pd = devm_kzalloc(dev, sizeof(struct pl022_ssp_controller), GFP_KERNEL);
  1881. if (!pd)
  1882. return NULL;
  1883. pd->bus_id = -1;
  1884. pd->enable_dma = 1;
  1885. of_property_read_u32(np, "num-cs", &tmp);
  1886. pd->num_chipselect = tmp;
  1887. of_property_read_u32(np, "pl022,autosuspend-delay",
  1888. &pd->autosuspend_delay);
  1889. pd->rt = of_property_read_bool(np, "pl022,rt");
  1890. return pd;
  1891. }
  1892. static int pl022_probe(struct amba_device *adev, const struct amba_id *id)
  1893. {
  1894. struct device *dev = &adev->dev;
  1895. struct pl022_ssp_controller *platform_info =
  1896. dev_get_platdata(&adev->dev);
  1897. struct spi_master *master;
  1898. struct pl022 *pl022 = NULL; /*Data for this driver */
  1899. struct device_node *np = adev->dev.of_node;
  1900. int status = 0, i, num_cs;
  1901. dev_info(&adev->dev,
  1902. "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
  1903. if (!platform_info && IS_ENABLED(CONFIG_OF))
  1904. platform_info = pl022_platform_data_dt_get(dev);
  1905. if (!platform_info) {
  1906. dev_err(dev, "probe: no platform data defined\n");
  1907. return -ENODEV;
  1908. }
  1909. if (platform_info->num_chipselect) {
  1910. num_cs = platform_info->num_chipselect;
  1911. } else {
  1912. dev_err(dev, "probe: no chip select defined\n");
  1913. return -ENODEV;
  1914. }
  1915. /* Allocate master with space for data */
  1916. master = spi_alloc_master(dev, sizeof(struct pl022));
  1917. if (master == NULL) {
  1918. dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
  1919. return -ENOMEM;
  1920. }
  1921. pl022 = spi_master_get_devdata(master);
  1922. pl022->master = master;
  1923. pl022->master_info = platform_info;
  1924. pl022->adev = adev;
  1925. pl022->vendor = id->data;
  1926. pl022->chipselects = devm_kzalloc(dev, num_cs * sizeof(int),
  1927. GFP_KERNEL);
  1928. if (!pl022->chipselects) {
  1929. status = -ENOMEM;
  1930. goto err_no_mem;
  1931. }
  1932. /*
  1933. * Bus Number Which has been Assigned to this SSP controller
  1934. * on this board
  1935. */
  1936. master->bus_num = platform_info->bus_id;
  1937. master->num_chipselect = num_cs;
  1938. master->cleanup = pl022_cleanup;
  1939. master->setup = pl022_setup;
  1940. master->auto_runtime_pm = true;
  1941. master->transfer_one_message = pl022_transfer_one_message;
  1942. master->unprepare_transfer_hardware = pl022_unprepare_transfer_hardware;
  1943. master->rt = platform_info->rt;
  1944. master->dev.of_node = dev->of_node;
  1945. if (platform_info->num_chipselect && platform_info->chipselects) {
  1946. for (i = 0; i < num_cs; i++)
  1947. pl022->chipselects[i] = platform_info->chipselects[i];
  1948. } else if (pl022->vendor->internal_cs_ctrl) {
  1949. for (i = 0; i < num_cs; i++)
  1950. pl022->chipselects[i] = i;
  1951. } else if (IS_ENABLED(CONFIG_OF)) {
  1952. for (i = 0; i < num_cs; i++) {
  1953. int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
  1954. if (cs_gpio == -EPROBE_DEFER) {
  1955. status = -EPROBE_DEFER;
  1956. goto err_no_gpio;
  1957. }
  1958. pl022->chipselects[i] = cs_gpio;
  1959. if (gpio_is_valid(cs_gpio)) {
  1960. if (devm_gpio_request(dev, cs_gpio, "ssp-pl022"))
  1961. dev_err(&adev->dev,
  1962. "could not request %d gpio\n",
  1963. cs_gpio);
  1964. else if (gpio_direction_output(cs_gpio, 1))
  1965. dev_err(&adev->dev,
  1966. "could not set gpio %d as output\n",
  1967. cs_gpio);
  1968. }
  1969. }
  1970. }
  1971. /*
  1972. * Supports mode 0-3, loopback, and active low CS. Transfers are
  1973. * always MS bit first on the original pl022.
  1974. */
  1975. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1976. if (pl022->vendor->extended_cr)
  1977. master->mode_bits |= SPI_LSB_FIRST;
  1978. dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
  1979. status = amba_request_regions(adev, NULL);
  1980. if (status)
  1981. goto err_no_ioregion;
  1982. pl022->phybase = adev->res.start;
  1983. pl022->virtbase = devm_ioremap(dev, adev->res.start,
  1984. resource_size(&adev->res));
  1985. if (pl022->virtbase == NULL) {
  1986. status = -ENOMEM;
  1987. goto err_no_ioremap;
  1988. }
  1989. dev_info(&adev->dev, "mapped registers from %pa to %p\n",
  1990. &adev->res.start, pl022->virtbase);
  1991. pl022->clk = devm_clk_get(&adev->dev, NULL);
  1992. if (IS_ERR(pl022->clk)) {
  1993. status = PTR_ERR(pl022->clk);
  1994. dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
  1995. goto err_no_clk;
  1996. }
  1997. status = clk_prepare_enable(pl022->clk);
  1998. if (status) {
  1999. dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n");
  2000. goto err_no_clk_en;
  2001. }
  2002. /* Initialize transfer pump */
  2003. tasklet_init(&pl022->pump_transfers, pump_transfers,
  2004. (unsigned long)pl022);
  2005. /* Disable SSP */
  2006. writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
  2007. SSP_CR1(pl022->virtbase));
  2008. load_ssp_default_config(pl022);
  2009. status = devm_request_irq(dev, adev->irq[0], pl022_interrupt_handler,
  2010. 0, "pl022", pl022);
  2011. if (status < 0) {
  2012. dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
  2013. goto err_no_irq;
  2014. }
  2015. /* Get DMA channels, try autoconfiguration first */
  2016. status = pl022_dma_autoprobe(pl022);
  2017. if (status == -EPROBE_DEFER) {
  2018. dev_dbg(dev, "deferring probe to get DMA channel\n");
  2019. goto err_no_irq;
  2020. }
  2021. /* If that failed, use channels from platform_info */
  2022. if (status == 0)
  2023. platform_info->enable_dma = 1;
  2024. else if (platform_info->enable_dma) {
  2025. status = pl022_dma_probe(pl022);
  2026. if (status != 0)
  2027. platform_info->enable_dma = 0;
  2028. }
  2029. /* Register with the SPI framework */
  2030. amba_set_drvdata(adev, pl022);
  2031. status = devm_spi_register_master(&adev->dev, master);
  2032. if (status != 0) {
  2033. dev_err(&adev->dev,
  2034. "probe - problem registering spi master\n");
  2035. goto err_spi_register;
  2036. }
  2037. dev_dbg(dev, "probe succeeded\n");
  2038. /* let runtime pm put suspend */
  2039. if (platform_info->autosuspend_delay > 0) {
  2040. dev_info(&adev->dev,
  2041. "will use autosuspend for runtime pm, delay %dms\n",
  2042. platform_info->autosuspend_delay);
  2043. pm_runtime_set_autosuspend_delay(dev,
  2044. platform_info->autosuspend_delay);
  2045. pm_runtime_use_autosuspend(dev);
  2046. }
  2047. pm_runtime_put(dev);
  2048. return 0;
  2049. err_spi_register:
  2050. if (platform_info->enable_dma)
  2051. pl022_dma_remove(pl022);
  2052. err_no_irq:
  2053. clk_disable_unprepare(pl022->clk);
  2054. err_no_clk_en:
  2055. err_no_clk:
  2056. err_no_ioremap:
  2057. amba_release_regions(adev);
  2058. err_no_ioregion:
  2059. err_no_gpio:
  2060. err_no_mem:
  2061. spi_master_put(master);
  2062. return status;
  2063. }
  2064. static int
  2065. pl022_remove(struct amba_device *adev)
  2066. {
  2067. struct pl022 *pl022 = amba_get_drvdata(adev);
  2068. if (!pl022)
  2069. return 0;
  2070. /*
  2071. * undo pm_runtime_put() in probe. I assume that we're not
  2072. * accessing the primecell here.
  2073. */
  2074. pm_runtime_get_noresume(&adev->dev);
  2075. load_ssp_default_config(pl022);
  2076. if (pl022->master_info->enable_dma)
  2077. pl022_dma_remove(pl022);
  2078. clk_disable_unprepare(pl022->clk);
  2079. amba_release_regions(adev);
  2080. tasklet_disable(&pl022->pump_transfers);
  2081. return 0;
  2082. }
  2083. #ifdef CONFIG_PM_SLEEP
  2084. static int pl022_suspend(struct device *dev)
  2085. {
  2086. struct pl022 *pl022 = dev_get_drvdata(dev);
  2087. int ret;
  2088. ret = spi_master_suspend(pl022->master);
  2089. if (ret) {
  2090. dev_warn(dev, "cannot suspend master\n");
  2091. return ret;
  2092. }
  2093. ret = pm_runtime_force_suspend(dev);
  2094. if (ret) {
  2095. spi_master_resume(pl022->master);
  2096. return ret;
  2097. }
  2098. pinctrl_pm_select_sleep_state(dev);
  2099. dev_dbg(dev, "suspended\n");
  2100. return 0;
  2101. }
  2102. static int pl022_resume(struct device *dev)
  2103. {
  2104. struct pl022 *pl022 = dev_get_drvdata(dev);
  2105. int ret;
  2106. ret = pm_runtime_force_resume(dev);
  2107. if (ret)
  2108. dev_err(dev, "problem resuming\n");
  2109. /* Start the queue running */
  2110. ret = spi_master_resume(pl022->master);
  2111. if (ret)
  2112. dev_err(dev, "problem starting queue (%d)\n", ret);
  2113. else
  2114. dev_dbg(dev, "resumed\n");
  2115. return ret;
  2116. }
  2117. #endif
  2118. #ifdef CONFIG_PM
  2119. static int pl022_runtime_suspend(struct device *dev)
  2120. {
  2121. struct pl022 *pl022 = dev_get_drvdata(dev);
  2122. clk_disable_unprepare(pl022->clk);
  2123. pinctrl_pm_select_idle_state(dev);
  2124. return 0;
  2125. }
  2126. static int pl022_runtime_resume(struct device *dev)
  2127. {
  2128. struct pl022 *pl022 = dev_get_drvdata(dev);
  2129. pinctrl_pm_select_default_state(dev);
  2130. clk_prepare_enable(pl022->clk);
  2131. return 0;
  2132. }
  2133. #endif
  2134. static const struct dev_pm_ops pl022_dev_pm_ops = {
  2135. SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume)
  2136. SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL)
  2137. };
  2138. static struct vendor_data vendor_arm = {
  2139. .fifodepth = 8,
  2140. .max_bpw = 16,
  2141. .unidir = false,
  2142. .extended_cr = false,
  2143. .pl023 = false,
  2144. .loopback = true,
  2145. .internal_cs_ctrl = false,
  2146. };
  2147. static struct vendor_data vendor_st = {
  2148. .fifodepth = 32,
  2149. .max_bpw = 32,
  2150. .unidir = false,
  2151. .extended_cr = true,
  2152. .pl023 = false,
  2153. .loopback = true,
  2154. .internal_cs_ctrl = false,
  2155. };
  2156. static struct vendor_data vendor_st_pl023 = {
  2157. .fifodepth = 32,
  2158. .max_bpw = 32,
  2159. .unidir = false,
  2160. .extended_cr = true,
  2161. .pl023 = true,
  2162. .loopback = false,
  2163. .internal_cs_ctrl = false,
  2164. };
  2165. static struct vendor_data vendor_lsi = {
  2166. .fifodepth = 8,
  2167. .max_bpw = 16,
  2168. .unidir = false,
  2169. .extended_cr = false,
  2170. .pl023 = false,
  2171. .loopback = true,
  2172. .internal_cs_ctrl = true,
  2173. };
  2174. static struct amba_id pl022_ids[] = {
  2175. {
  2176. /*
  2177. * ARM PL022 variant, this has a 16bit wide
  2178. * and 8 locations deep TX/RX FIFO
  2179. */
  2180. .id = 0x00041022,
  2181. .mask = 0x000fffff,
  2182. .data = &vendor_arm,
  2183. },
  2184. {
  2185. /*
  2186. * ST Micro derivative, this has 32bit wide
  2187. * and 32 locations deep TX/RX FIFO
  2188. */
  2189. .id = 0x01080022,
  2190. .mask = 0xffffffff,
  2191. .data = &vendor_st,
  2192. },
  2193. {
  2194. /*
  2195. * ST-Ericsson derivative "PL023" (this is not
  2196. * an official ARM number), this is a PL022 SSP block
  2197. * stripped to SPI mode only, it has 32bit wide
  2198. * and 32 locations deep TX/RX FIFO but no extended
  2199. * CR0/CR1 register
  2200. */
  2201. .id = 0x00080023,
  2202. .mask = 0xffffffff,
  2203. .data = &vendor_st_pl023,
  2204. },
  2205. {
  2206. /*
  2207. * PL022 variant that has a chip select control register whih
  2208. * allows control of 5 output signals nCS[0:4].
  2209. */
  2210. .id = 0x000b6022,
  2211. .mask = 0x000fffff,
  2212. .data = &vendor_lsi,
  2213. },
  2214. { 0, 0 },
  2215. };
  2216. MODULE_DEVICE_TABLE(amba, pl022_ids);
  2217. static struct amba_driver pl022_driver = {
  2218. .drv = {
  2219. .name = "ssp-pl022",
  2220. .pm = &pl022_dev_pm_ops,
  2221. },
  2222. .id_table = pl022_ids,
  2223. .probe = pl022_probe,
  2224. .remove = pl022_remove,
  2225. };
  2226. static int __init pl022_init(void)
  2227. {
  2228. return amba_driver_register(&pl022_driver);
  2229. }
  2230. subsys_initcall(pl022_init);
  2231. static void __exit pl022_exit(void)
  2232. {
  2233. amba_driver_unregister(&pl022_driver);
  2234. }
  2235. module_exit(pl022_exit);
  2236. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  2237. MODULE_DESCRIPTION("PL022 SSP Controller Driver");
  2238. MODULE_LICENSE("GPL");