spi-pl022.c 67 KB

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