amba-pl022.c 56 KB

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