amba-pl022.c 65 KB

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