spi-pl022.c 68 KB

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