spi-pl022.c 64 KB

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