spi-pl022.c 68 KB

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