spi-pl022.c 67 KB

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