amba-pl022.c 64 KB

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