amba-pl022.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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 clock! */
  459. clk_disable(pl022->clk);
  460. }
  461. /**
  462. * flush - flush the FIFO to reach a clean state
  463. * @pl022: SSP driver private data structure
  464. */
  465. static int flush(struct pl022 *pl022)
  466. {
  467. unsigned long limit = loops_per_jiffy << 1;
  468. dev_dbg(&pl022->adev->dev, "flush\n");
  469. do {
  470. while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  471. readw(SSP_DR(pl022->virtbase));
  472. } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
  473. pl022->exp_fifo_level = 0;
  474. return limit;
  475. }
  476. /**
  477. * restore_state - Load configuration of current chip
  478. * @pl022: SSP driver private data structure
  479. */
  480. static void restore_state(struct pl022 *pl022)
  481. {
  482. struct chip_data *chip = pl022->cur_chip;
  483. if (pl022->vendor->extended_cr)
  484. writel(chip->cr0, SSP_CR0(pl022->virtbase));
  485. else
  486. writew(chip->cr0, SSP_CR0(pl022->virtbase));
  487. writew(chip->cr1, SSP_CR1(pl022->virtbase));
  488. writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
  489. writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
  490. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  491. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  492. }
  493. /*
  494. * Default SSP Register Values
  495. */
  496. #define DEFAULT_SSP_REG_CR0 ( \
  497. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
  498. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
  499. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  500. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  501. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  502. )
  503. /* ST versions have slightly different bit layout */
  504. #define DEFAULT_SSP_REG_CR0_ST ( \
  505. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  506. GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
  507. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  508. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  509. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
  510. GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
  511. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
  512. )
  513. /* The PL023 version is slightly different again */
  514. #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
  515. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  516. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  517. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  518. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  519. )
  520. #define DEFAULT_SSP_REG_CR1 ( \
  521. GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
  522. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  523. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  524. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
  525. )
  526. /* ST versions extend this register to use all 16 bits */
  527. #define DEFAULT_SSP_REG_CR1_ST ( \
  528. DEFAULT_SSP_REG_CR1 | \
  529. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  530. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  531. GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
  532. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  533. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
  534. )
  535. /*
  536. * The PL023 variant has further differences: no loopback mode, no microwire
  537. * support, and a new clock feedback delay setting.
  538. */
  539. #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
  540. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  541. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  542. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
  543. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  544. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  545. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  546. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
  547. GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
  548. )
  549. #define DEFAULT_SSP_REG_CPSR ( \
  550. GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
  551. )
  552. #define DEFAULT_SSP_REG_DMACR (\
  553. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
  554. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
  555. )
  556. /**
  557. * load_ssp_default_config - Load default configuration for SSP
  558. * @pl022: SSP driver private data structure
  559. */
  560. static void load_ssp_default_config(struct pl022 *pl022)
  561. {
  562. if (pl022->vendor->pl023) {
  563. writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
  564. writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
  565. } else if (pl022->vendor->extended_cr) {
  566. writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
  567. writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
  568. } else {
  569. writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
  570. writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
  571. }
  572. writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
  573. writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
  574. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  575. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  576. }
  577. /**
  578. * This will write to TX and read from RX according to the parameters
  579. * set in pl022.
  580. */
  581. static void readwriter(struct pl022 *pl022)
  582. {
  583. /*
  584. * The FIFO depth is different inbetween primecell variants.
  585. * I believe filling in too much in the FIFO might cause
  586. * errons in 8bit wide transfers on ARM variants (just 8 words
  587. * FIFO, means only 8x8 = 64 bits in FIFO) at least.
  588. *
  589. * To prevent this issue, the TX FIFO is only filled to the
  590. * unused RX FIFO fill length, regardless of what the TX
  591. * FIFO status flag indicates.
  592. */
  593. dev_dbg(&pl022->adev->dev,
  594. "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
  595. __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
  596. /* Read as much as you can */
  597. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  598. && (pl022->rx < pl022->rx_end)) {
  599. switch (pl022->read) {
  600. case READING_NULL:
  601. readw(SSP_DR(pl022->virtbase));
  602. break;
  603. case READING_U8:
  604. *(u8 *) (pl022->rx) =
  605. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  606. break;
  607. case READING_U16:
  608. *(u16 *) (pl022->rx) =
  609. (u16) readw(SSP_DR(pl022->virtbase));
  610. break;
  611. case READING_U32:
  612. *(u32 *) (pl022->rx) =
  613. readl(SSP_DR(pl022->virtbase));
  614. break;
  615. }
  616. pl022->rx += (pl022->cur_chip->n_bytes);
  617. pl022->exp_fifo_level--;
  618. }
  619. /*
  620. * Write as much as possible up to the RX FIFO size
  621. */
  622. while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
  623. && (pl022->tx < pl022->tx_end)) {
  624. switch (pl022->write) {
  625. case WRITING_NULL:
  626. writew(0x0, SSP_DR(pl022->virtbase));
  627. break;
  628. case WRITING_U8:
  629. writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
  630. break;
  631. case WRITING_U16:
  632. writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
  633. break;
  634. case WRITING_U32:
  635. writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
  636. break;
  637. }
  638. pl022->tx += (pl022->cur_chip->n_bytes);
  639. pl022->exp_fifo_level++;
  640. /*
  641. * This inner reader takes care of things appearing in the RX
  642. * FIFO as we're transmitting. This will happen a lot since the
  643. * clock starts running when you put things into the TX FIFO,
  644. * and then things are continously clocked into the RX FIFO.
  645. */
  646. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  647. && (pl022->rx < pl022->rx_end)) {
  648. switch (pl022->read) {
  649. case READING_NULL:
  650. readw(SSP_DR(pl022->virtbase));
  651. break;
  652. case READING_U8:
  653. *(u8 *) (pl022->rx) =
  654. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  655. break;
  656. case READING_U16:
  657. *(u16 *) (pl022->rx) =
  658. (u16) readw(SSP_DR(pl022->virtbase));
  659. break;
  660. case READING_U32:
  661. *(u32 *) (pl022->rx) =
  662. readl(SSP_DR(pl022->virtbase));
  663. break;
  664. }
  665. pl022->rx += (pl022->cur_chip->n_bytes);
  666. pl022->exp_fifo_level--;
  667. }
  668. }
  669. /*
  670. * When we exit here the TX FIFO should be full and the RX FIFO
  671. * should be empty
  672. */
  673. }
  674. /**
  675. * next_transfer - Move to the Next transfer in the current spi message
  676. * @pl022: SSP driver private data structure
  677. *
  678. * This function moves though the linked list of spi transfers in the
  679. * current spi message and returns with the state of current spi
  680. * message i.e whether its last transfer is done(STATE_DONE) or
  681. * Next transfer is ready(STATE_RUNNING)
  682. */
  683. static void *next_transfer(struct pl022 *pl022)
  684. {
  685. struct spi_message *msg = pl022->cur_msg;
  686. struct spi_transfer *trans = pl022->cur_transfer;
  687. /* Move to next transfer */
  688. if (trans->transfer_list.next != &msg->transfers) {
  689. pl022->cur_transfer =
  690. list_entry(trans->transfer_list.next,
  691. struct spi_transfer, transfer_list);
  692. return STATE_RUNNING;
  693. }
  694. return STATE_DONE;
  695. }
  696. /**
  697. * pl022_interrupt_handler - Interrupt handler for SSP controller
  698. *
  699. * This function handles interrupts generated for an interrupt based transfer.
  700. * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
  701. * current message's state as STATE_ERROR and schedule the tasklet
  702. * pump_transfers which will do the postprocessing of the current message by
  703. * calling giveback(). Otherwise it reads data from RX FIFO till there is no
  704. * more data, and writes data in TX FIFO till it is not full. If we complete
  705. * the transfer we move to the next transfer and schedule the tasklet.
  706. */
  707. static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
  708. {
  709. struct pl022 *pl022 = dev_id;
  710. struct spi_message *msg = pl022->cur_msg;
  711. u16 irq_status = 0;
  712. u16 flag = 0;
  713. if (unlikely(!msg)) {
  714. dev_err(&pl022->adev->dev,
  715. "bad message state in interrupt handler");
  716. /* Never fail */
  717. return IRQ_HANDLED;
  718. }
  719. /* Read the Interrupt Status Register */
  720. irq_status = readw(SSP_MIS(pl022->virtbase));
  721. if (unlikely(!irq_status))
  722. return IRQ_NONE;
  723. /* This handles the error code interrupts */
  724. if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
  725. /*
  726. * Overrun interrupt - bail out since our Data has been
  727. * corrupted
  728. */
  729. dev_err(&pl022->adev->dev,
  730. "FIFO overrun\n");
  731. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
  732. dev_err(&pl022->adev->dev,
  733. "RXFIFO is full\n");
  734. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
  735. dev_err(&pl022->adev->dev,
  736. "TXFIFO is full\n");
  737. /*
  738. * Disable and clear interrupts, disable SSP,
  739. * mark message with bad status so it can be
  740. * retried.
  741. */
  742. writew(DISABLE_ALL_INTERRUPTS,
  743. SSP_IMSC(pl022->virtbase));
  744. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  745. writew((readw(SSP_CR1(pl022->virtbase)) &
  746. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  747. msg->state = STATE_ERROR;
  748. /* Schedule message queue handler */
  749. tasklet_schedule(&pl022->pump_transfers);
  750. return IRQ_HANDLED;
  751. }
  752. readwriter(pl022);
  753. if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
  754. flag = 1;
  755. /* Disable Transmit interrupt */
  756. writew(readw(SSP_IMSC(pl022->virtbase)) &
  757. (~SSP_IMSC_MASK_TXIM),
  758. SSP_IMSC(pl022->virtbase));
  759. }
  760. /*
  761. * Since all transactions must write as much as shall be read,
  762. * we can conclude the entire transaction once RX is complete.
  763. * At this point, all TX will always be finished.
  764. */
  765. if (pl022->rx >= pl022->rx_end) {
  766. writew(DISABLE_ALL_INTERRUPTS,
  767. SSP_IMSC(pl022->virtbase));
  768. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  769. if (unlikely(pl022->rx > pl022->rx_end)) {
  770. dev_warn(&pl022->adev->dev, "read %u surplus "
  771. "bytes (did you request an odd "
  772. "number of bytes on a 16bit bus?)\n",
  773. (u32) (pl022->rx - pl022->rx_end));
  774. }
  775. /* Update total bytes transfered */
  776. msg->actual_length += pl022->cur_transfer->len;
  777. if (pl022->cur_transfer->cs_change)
  778. pl022->cur_chip->
  779. cs_control(SSP_CHIP_DESELECT);
  780. /* Move to next transfer */
  781. msg->state = next_transfer(pl022);
  782. tasklet_schedule(&pl022->pump_transfers);
  783. return IRQ_HANDLED;
  784. }
  785. return IRQ_HANDLED;
  786. }
  787. /**
  788. * This sets up the pointers to memory for the next message to
  789. * send out on the SPI bus.
  790. */
  791. static int set_up_next_transfer(struct pl022 *pl022,
  792. struct spi_transfer *transfer)
  793. {
  794. int residue;
  795. /* Sanity check the message for this bus width */
  796. residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
  797. if (unlikely(residue != 0)) {
  798. dev_err(&pl022->adev->dev,
  799. "message of %u bytes to transmit but the current "
  800. "chip bus has a data width of %u bytes!\n",
  801. pl022->cur_transfer->len,
  802. pl022->cur_chip->n_bytes);
  803. dev_err(&pl022->adev->dev, "skipping this message\n");
  804. return -EIO;
  805. }
  806. pl022->tx = (void *)transfer->tx_buf;
  807. pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
  808. pl022->rx = (void *)transfer->rx_buf;
  809. pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
  810. pl022->write =
  811. pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
  812. pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
  813. return 0;
  814. }
  815. /**
  816. * pump_transfers - Tasklet function which schedules next interrupt transfer
  817. * when running in interrupt transfer mode.
  818. * @data: SSP driver private data structure
  819. *
  820. */
  821. static void pump_transfers(unsigned long data)
  822. {
  823. struct pl022 *pl022 = (struct pl022 *) data;
  824. struct spi_message *message = NULL;
  825. struct spi_transfer *transfer = NULL;
  826. struct spi_transfer *previous = NULL;
  827. /* Get current state information */
  828. message = pl022->cur_msg;
  829. transfer = pl022->cur_transfer;
  830. /* Handle for abort */
  831. if (message->state == STATE_ERROR) {
  832. message->status = -EIO;
  833. giveback(pl022);
  834. return;
  835. }
  836. /* Handle end of message */
  837. if (message->state == STATE_DONE) {
  838. message->status = 0;
  839. giveback(pl022);
  840. return;
  841. }
  842. /* Delay if requested at end of transfer before CS change */
  843. if (message->state == STATE_RUNNING) {
  844. previous = list_entry(transfer->transfer_list.prev,
  845. struct spi_transfer,
  846. transfer_list);
  847. if (previous->delay_usecs)
  848. /*
  849. * FIXME: This runs in interrupt context.
  850. * Is this really smart?
  851. */
  852. udelay(previous->delay_usecs);
  853. /* Drop chip select only if cs_change is requested */
  854. if (previous->cs_change)
  855. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  856. } else {
  857. /* STATE_START */
  858. message->state = STATE_RUNNING;
  859. }
  860. if (set_up_next_transfer(pl022, transfer)) {
  861. message->state = STATE_ERROR;
  862. message->status = -EIO;
  863. giveback(pl022);
  864. return;
  865. }
  866. /* Flush the FIFOs and let's go! */
  867. flush(pl022);
  868. writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  869. }
  870. /**
  871. * NOT IMPLEMENTED
  872. * configure_dma - It configures the DMA pipes for DMA transfers
  873. * @data: SSP driver's private data structure
  874. *
  875. */
  876. static int configure_dma(void *data)
  877. {
  878. struct pl022 *pl022 = data;
  879. dev_dbg(&pl022->adev->dev, "configure DMA\n");
  880. return -ENOTSUPP;
  881. }
  882. /**
  883. * do_dma_transfer - It handles transfers of the current message
  884. * if it is DMA xfer.
  885. * NOT FULLY IMPLEMENTED
  886. * @data: SSP driver's private data structure
  887. */
  888. static void do_dma_transfer(void *data)
  889. {
  890. struct pl022 *pl022 = data;
  891. if (configure_dma(data)) {
  892. dev_dbg(&pl022->adev->dev, "configuration of DMA Failed!\n");
  893. goto err_config_dma;
  894. }
  895. /* TODO: Implememt DMA setup of pipes here */
  896. /* Enable target chip, set up transfer */
  897. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  898. if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
  899. /* Error path */
  900. pl022->cur_msg->state = STATE_ERROR;
  901. pl022->cur_msg->status = -EIO;
  902. giveback(pl022);
  903. return;
  904. }
  905. /* Enable SSP */
  906. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  907. SSP_CR1(pl022->virtbase));
  908. /* TODO: Enable the DMA transfer here */
  909. return;
  910. err_config_dma:
  911. pl022->cur_msg->state = STATE_ERROR;
  912. pl022->cur_msg->status = -EIO;
  913. giveback(pl022);
  914. return;
  915. }
  916. static void do_interrupt_transfer(void *data)
  917. {
  918. struct pl022 *pl022 = data;
  919. /* Enable target chip */
  920. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  921. if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
  922. /* Error path */
  923. pl022->cur_msg->state = STATE_ERROR;
  924. pl022->cur_msg->status = -EIO;
  925. giveback(pl022);
  926. return;
  927. }
  928. /* Enable SSP, turn on interrupts */
  929. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  930. SSP_CR1(pl022->virtbase));
  931. writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  932. }
  933. static void do_polling_transfer(void *data)
  934. {
  935. struct pl022 *pl022 = data;
  936. struct spi_message *message = NULL;
  937. struct spi_transfer *transfer = NULL;
  938. struct spi_transfer *previous = NULL;
  939. struct chip_data *chip;
  940. chip = pl022->cur_chip;
  941. message = pl022->cur_msg;
  942. while (message->state != STATE_DONE) {
  943. /* Handle for abort */
  944. if (message->state == STATE_ERROR)
  945. break;
  946. transfer = pl022->cur_transfer;
  947. /* Delay if requested at end of transfer */
  948. if (message->state == STATE_RUNNING) {
  949. previous =
  950. list_entry(transfer->transfer_list.prev,
  951. struct spi_transfer, transfer_list);
  952. if (previous->delay_usecs)
  953. udelay(previous->delay_usecs);
  954. if (previous->cs_change)
  955. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  956. } else {
  957. /* STATE_START */
  958. message->state = STATE_RUNNING;
  959. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  960. }
  961. /* Configuration Changing Per Transfer */
  962. if (set_up_next_transfer(pl022, transfer)) {
  963. /* Error path */
  964. message->state = STATE_ERROR;
  965. break;
  966. }
  967. /* Flush FIFOs and enable SSP */
  968. flush(pl022);
  969. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  970. SSP_CR1(pl022->virtbase));
  971. dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
  972. /* FIXME: insert a timeout so we don't hang here indefinately */
  973. while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end)
  974. readwriter(pl022);
  975. /* Update total byte transfered */
  976. message->actual_length += pl022->cur_transfer->len;
  977. if (pl022->cur_transfer->cs_change)
  978. pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
  979. /* Move to next transfer */
  980. message->state = next_transfer(pl022);
  981. }
  982. /* Handle end of message */
  983. if (message->state == STATE_DONE)
  984. message->status = 0;
  985. else
  986. message->status = -EIO;
  987. giveback(pl022);
  988. return;
  989. }
  990. /**
  991. * pump_messages - Workqueue function which processes spi message queue
  992. * @data: pointer to private data of SSP driver
  993. *
  994. * This function checks if there is any spi message in the queue that
  995. * needs processing and delegate control to appropriate function
  996. * do_polling_transfer()/do_interrupt_transfer()/do_dma_transfer()
  997. * based on the kind of the transfer
  998. *
  999. */
  1000. static void pump_messages(struct work_struct *work)
  1001. {
  1002. struct pl022 *pl022 =
  1003. container_of(work, struct pl022, pump_messages);
  1004. unsigned long flags;
  1005. /* Lock queue and check for queue work */
  1006. spin_lock_irqsave(&pl022->queue_lock, flags);
  1007. if (list_empty(&pl022->queue) || pl022->run == QUEUE_STOPPED) {
  1008. pl022->busy = 0;
  1009. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1010. return;
  1011. }
  1012. /* Make sure we are not already running a message */
  1013. if (pl022->cur_msg) {
  1014. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1015. return;
  1016. }
  1017. /* Extract head of queue */
  1018. pl022->cur_msg =
  1019. list_entry(pl022->queue.next, struct spi_message, queue);
  1020. list_del_init(&pl022->cur_msg->queue);
  1021. pl022->busy = 1;
  1022. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1023. /* Initial message state */
  1024. pl022->cur_msg->state = STATE_START;
  1025. pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next,
  1026. struct spi_transfer,
  1027. transfer_list);
  1028. /* Setup the SPI using the per chip configuration */
  1029. pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi);
  1030. /*
  1031. * We enable the clock here, then the clock will be disabled when
  1032. * giveback() is called in each method (poll/interrupt/DMA)
  1033. */
  1034. clk_enable(pl022->clk);
  1035. restore_state(pl022);
  1036. flush(pl022);
  1037. if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
  1038. do_polling_transfer(pl022);
  1039. else if (pl022->cur_chip->xfer_type == INTERRUPT_TRANSFER)
  1040. do_interrupt_transfer(pl022);
  1041. else
  1042. do_dma_transfer(pl022);
  1043. }
  1044. static int __init init_queue(struct pl022 *pl022)
  1045. {
  1046. INIT_LIST_HEAD(&pl022->queue);
  1047. spin_lock_init(&pl022->queue_lock);
  1048. pl022->run = QUEUE_STOPPED;
  1049. pl022->busy = 0;
  1050. tasklet_init(&pl022->pump_transfers,
  1051. pump_transfers, (unsigned long)pl022);
  1052. INIT_WORK(&pl022->pump_messages, pump_messages);
  1053. pl022->workqueue = create_singlethread_workqueue(
  1054. dev_name(pl022->master->dev.parent));
  1055. if (pl022->workqueue == NULL)
  1056. return -EBUSY;
  1057. return 0;
  1058. }
  1059. static int start_queue(struct pl022 *pl022)
  1060. {
  1061. unsigned long flags;
  1062. spin_lock_irqsave(&pl022->queue_lock, flags);
  1063. if (pl022->run == QUEUE_RUNNING || pl022->busy) {
  1064. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1065. return -EBUSY;
  1066. }
  1067. pl022->run = QUEUE_RUNNING;
  1068. pl022->cur_msg = NULL;
  1069. pl022->cur_transfer = NULL;
  1070. pl022->cur_chip = NULL;
  1071. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1072. queue_work(pl022->workqueue, &pl022->pump_messages);
  1073. return 0;
  1074. }
  1075. static int stop_queue(struct pl022 *pl022)
  1076. {
  1077. unsigned long flags;
  1078. unsigned limit = 500;
  1079. int status = 0;
  1080. spin_lock_irqsave(&pl022->queue_lock, flags);
  1081. /* This is a bit lame, but is optimized for the common execution path.
  1082. * A wait_queue on the pl022->busy could be used, but then the common
  1083. * execution path (pump_messages) would be required to call wake_up or
  1084. * friends on every SPI message. Do this instead */
  1085. while (!list_empty(&pl022->queue) && pl022->busy && limit--) {
  1086. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1087. msleep(10);
  1088. spin_lock_irqsave(&pl022->queue_lock, flags);
  1089. }
  1090. if (!list_empty(&pl022->queue) || pl022->busy)
  1091. status = -EBUSY;
  1092. else pl022->run = QUEUE_STOPPED;
  1093. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1094. return status;
  1095. }
  1096. static int destroy_queue(struct pl022 *pl022)
  1097. {
  1098. int status;
  1099. status = stop_queue(pl022);
  1100. /* we are unloading the module or failing to load (only two calls
  1101. * to this routine), and neither call can handle a return value.
  1102. * However, destroy_workqueue calls flush_workqueue, and that will
  1103. * block until all work is done. If the reason that stop_queue
  1104. * timed out is that the work will never finish, then it does no
  1105. * good to call destroy_workqueue, so return anyway. */
  1106. if (status != 0)
  1107. return status;
  1108. destroy_workqueue(pl022->workqueue);
  1109. return 0;
  1110. }
  1111. static int verify_controller_parameters(struct pl022 *pl022,
  1112. struct pl022_config_chip *chip_info)
  1113. {
  1114. if ((chip_info->lbm != LOOPBACK_ENABLED)
  1115. && (chip_info->lbm != LOOPBACK_DISABLED)) {
  1116. dev_err(chip_info->dev,
  1117. "loopback Mode is configured incorrectly\n");
  1118. return -EINVAL;
  1119. }
  1120. if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
  1121. || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
  1122. dev_err(chip_info->dev,
  1123. "interface is configured incorrectly\n");
  1124. return -EINVAL;
  1125. }
  1126. if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
  1127. (!pl022->vendor->unidir)) {
  1128. dev_err(chip_info->dev,
  1129. "unidirectional mode not supported in this "
  1130. "hardware version\n");
  1131. return -EINVAL;
  1132. }
  1133. if ((chip_info->hierarchy != SSP_MASTER)
  1134. && (chip_info->hierarchy != SSP_SLAVE)) {
  1135. dev_err(chip_info->dev,
  1136. "hierarchy is configured incorrectly\n");
  1137. return -EINVAL;
  1138. }
  1139. if (((chip_info->clk_freq).cpsdvsr < CPSDVR_MIN)
  1140. || ((chip_info->clk_freq).cpsdvsr > CPSDVR_MAX)) {
  1141. dev_err(chip_info->dev,
  1142. "cpsdvsr is configured incorrectly\n");
  1143. return -EINVAL;
  1144. }
  1145. if ((chip_info->endian_rx != SSP_RX_MSB)
  1146. && (chip_info->endian_rx != SSP_RX_LSB)) {
  1147. dev_err(chip_info->dev,
  1148. "RX FIFO endianess is configured incorrectly\n");
  1149. return -EINVAL;
  1150. }
  1151. if ((chip_info->endian_tx != SSP_TX_MSB)
  1152. && (chip_info->endian_tx != SSP_TX_LSB)) {
  1153. dev_err(chip_info->dev,
  1154. "TX FIFO endianess is configured incorrectly\n");
  1155. return -EINVAL;
  1156. }
  1157. if ((chip_info->data_size < SSP_DATA_BITS_4)
  1158. || (chip_info->data_size > SSP_DATA_BITS_32)) {
  1159. dev_err(chip_info->dev,
  1160. "DATA Size is configured incorrectly\n");
  1161. return -EINVAL;
  1162. }
  1163. if ((chip_info->com_mode != INTERRUPT_TRANSFER)
  1164. && (chip_info->com_mode != DMA_TRANSFER)
  1165. && (chip_info->com_mode != POLLING_TRANSFER)) {
  1166. dev_err(chip_info->dev,
  1167. "Communication mode is configured incorrectly\n");
  1168. return -EINVAL;
  1169. }
  1170. if ((chip_info->rx_lev_trig < SSP_RX_1_OR_MORE_ELEM)
  1171. || (chip_info->rx_lev_trig > SSP_RX_32_OR_MORE_ELEM)) {
  1172. dev_err(chip_info->dev,
  1173. "RX FIFO Trigger Level is configured incorrectly\n");
  1174. return -EINVAL;
  1175. }
  1176. if ((chip_info->tx_lev_trig < SSP_TX_1_OR_MORE_EMPTY_LOC)
  1177. || (chip_info->tx_lev_trig > SSP_TX_32_OR_MORE_EMPTY_LOC)) {
  1178. dev_err(chip_info->dev,
  1179. "TX FIFO Trigger Level is configured incorrectly\n");
  1180. return -EINVAL;
  1181. }
  1182. if (chip_info->iface == SSP_INTERFACE_MOTOROLA_SPI) {
  1183. if ((chip_info->clk_phase != SSP_CLK_FIRST_EDGE)
  1184. && (chip_info->clk_phase != SSP_CLK_SECOND_EDGE)) {
  1185. dev_err(chip_info->dev,
  1186. "Clock Phase is configured incorrectly\n");
  1187. return -EINVAL;
  1188. }
  1189. if ((chip_info->clk_pol != SSP_CLK_POL_IDLE_LOW)
  1190. && (chip_info->clk_pol != SSP_CLK_POL_IDLE_HIGH)) {
  1191. dev_err(chip_info->dev,
  1192. "Clock Polarity is configured incorrectly\n");
  1193. return -EINVAL;
  1194. }
  1195. }
  1196. if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
  1197. if ((chip_info->ctrl_len < SSP_BITS_4)
  1198. || (chip_info->ctrl_len > SSP_BITS_32)) {
  1199. dev_err(chip_info->dev,
  1200. "CTRL LEN is configured incorrectly\n");
  1201. return -EINVAL;
  1202. }
  1203. if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
  1204. && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
  1205. dev_err(chip_info->dev,
  1206. "Wait State is configured incorrectly\n");
  1207. return -EINVAL;
  1208. }
  1209. /* Half duplex is only available in the ST Micro version */
  1210. if (pl022->vendor->extended_cr) {
  1211. if ((chip_info->duplex !=
  1212. SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1213. && (chip_info->duplex !=
  1214. SSP_MICROWIRE_CHANNEL_HALF_DUPLEX))
  1215. dev_err(chip_info->dev,
  1216. "Microwire duplex mode is configured incorrectly\n");
  1217. return -EINVAL;
  1218. } else {
  1219. if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1220. dev_err(chip_info->dev,
  1221. "Microwire half duplex mode requested,"
  1222. " but this is only available in the"
  1223. " ST version of PL022\n");
  1224. return -EINVAL;
  1225. }
  1226. }
  1227. if (chip_info->cs_control == NULL) {
  1228. dev_warn(chip_info->dev,
  1229. "Chip Select Function is NULL for this chip\n");
  1230. chip_info->cs_control = null_cs_control;
  1231. }
  1232. return 0;
  1233. }
  1234. /**
  1235. * pl022_transfer - transfer function registered to SPI master framework
  1236. * @spi: spi device which is requesting transfer
  1237. * @msg: spi message which is to handled is queued to driver queue
  1238. *
  1239. * This function is registered to the SPI framework for this SPI master
  1240. * controller. It will queue the spi_message in the queue of driver if
  1241. * the queue is not stopped and return.
  1242. */
  1243. static int pl022_transfer(struct spi_device *spi, struct spi_message *msg)
  1244. {
  1245. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1246. unsigned long flags;
  1247. spin_lock_irqsave(&pl022->queue_lock, flags);
  1248. if (pl022->run == QUEUE_STOPPED) {
  1249. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1250. return -ESHUTDOWN;
  1251. }
  1252. msg->actual_length = 0;
  1253. msg->status = -EINPROGRESS;
  1254. msg->state = STATE_START;
  1255. list_add_tail(&msg->queue, &pl022->queue);
  1256. if (pl022->run == QUEUE_RUNNING && !pl022->busy)
  1257. queue_work(pl022->workqueue, &pl022->pump_messages);
  1258. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1259. return 0;
  1260. }
  1261. static int calculate_effective_freq(struct pl022 *pl022,
  1262. int freq,
  1263. struct ssp_clock_params *clk_freq)
  1264. {
  1265. /* Lets calculate the frequency parameters */
  1266. u16 cpsdvsr = 2;
  1267. u16 scr = 0;
  1268. bool freq_found = false;
  1269. u32 rate;
  1270. u32 max_tclk;
  1271. u32 min_tclk;
  1272. rate = clk_get_rate(pl022->clk);
  1273. /* cpsdvscr = 2 & scr 0 */
  1274. max_tclk = (rate / (CPSDVR_MIN * (1 + SCR_MIN)));
  1275. /* cpsdvsr = 254 & scr = 255 */
  1276. min_tclk = (rate / (CPSDVR_MAX * (1 + SCR_MAX)));
  1277. if ((freq <= max_tclk) && (freq >= min_tclk)) {
  1278. while (cpsdvsr <= CPSDVR_MAX && !freq_found) {
  1279. while (scr <= SCR_MAX && !freq_found) {
  1280. if ((rate /
  1281. (cpsdvsr * (1 + scr))) > freq)
  1282. scr += 1;
  1283. else {
  1284. /*
  1285. * This bool is made true when
  1286. * effective frequency >=
  1287. * target frequency is found
  1288. */
  1289. freq_found = true;
  1290. if ((rate /
  1291. (cpsdvsr * (1 + scr))) != freq) {
  1292. if (scr == SCR_MIN) {
  1293. cpsdvsr -= 2;
  1294. scr = SCR_MAX;
  1295. } else
  1296. scr -= 1;
  1297. }
  1298. }
  1299. }
  1300. if (!freq_found) {
  1301. cpsdvsr += 2;
  1302. scr = SCR_MIN;
  1303. }
  1304. }
  1305. if (cpsdvsr != 0) {
  1306. dev_dbg(&pl022->adev->dev,
  1307. "SSP Effective Frequency is %u\n",
  1308. (rate / (cpsdvsr * (1 + scr))));
  1309. clk_freq->cpsdvsr = (u8) (cpsdvsr & 0xFF);
  1310. clk_freq->scr = (u8) (scr & 0xFF);
  1311. dev_dbg(&pl022->adev->dev,
  1312. "SSP cpsdvsr = %d, scr = %d\n",
  1313. clk_freq->cpsdvsr, clk_freq->scr);
  1314. }
  1315. } else {
  1316. dev_err(&pl022->adev->dev,
  1317. "controller data is incorrect: out of range frequency");
  1318. return -EINVAL;
  1319. }
  1320. return 0;
  1321. }
  1322. /**
  1323. * NOT IMPLEMENTED
  1324. * process_dma_info - Processes the DMA info provided by client drivers
  1325. * @chip_info: chip info provided by client device
  1326. * @chip: Runtime state maintained by the SSP controller for each spi device
  1327. *
  1328. * This function processes and stores DMA config provided by client driver
  1329. * into the runtime state maintained by the SSP controller driver
  1330. */
  1331. static int process_dma_info(struct pl022_config_chip *chip_info,
  1332. struct chip_data *chip)
  1333. {
  1334. dev_err(chip_info->dev,
  1335. "cannot process DMA info, DMA not implemented!\n");
  1336. return -ENOTSUPP;
  1337. }
  1338. /**
  1339. * pl022_setup - setup function registered to SPI master framework
  1340. * @spi: spi device which is requesting setup
  1341. *
  1342. * This function is registered to the SPI framework for this SPI master
  1343. * controller. If it is the first time when setup is called by this device,
  1344. * this function will initialize the runtime state for this chip and save
  1345. * the same in the device structure. Else it will update the runtime info
  1346. * with the updated chip info. Nothing is really being written to the
  1347. * controller hardware here, that is not done until the actual transfer
  1348. * commence.
  1349. */
  1350. /* FIXME: JUST GUESSING the spi->mode bits understood by this driver */
  1351. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  1352. | SPI_LSB_FIRST | SPI_LOOP)
  1353. static int pl022_setup(struct spi_device *spi)
  1354. {
  1355. struct pl022_config_chip *chip_info;
  1356. struct chip_data *chip;
  1357. int status = 0;
  1358. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1359. if (spi->mode & ~MODEBITS) {
  1360. dev_dbg(&spi->dev, "unsupported mode bits %x\n",
  1361. spi->mode & ~MODEBITS);
  1362. return -EINVAL;
  1363. }
  1364. if (!spi->max_speed_hz)
  1365. return -EINVAL;
  1366. /* Get controller_state if one is supplied */
  1367. chip = spi_get_ctldata(spi);
  1368. if (chip == NULL) {
  1369. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1370. if (!chip) {
  1371. dev_err(&spi->dev,
  1372. "cannot allocate controller state\n");
  1373. return -ENOMEM;
  1374. }
  1375. dev_dbg(&spi->dev,
  1376. "allocated memory for controller's runtime state\n");
  1377. }
  1378. /* Get controller data if one is supplied */
  1379. chip_info = spi->controller_data;
  1380. if (chip_info == NULL) {
  1381. /* spi_board_info.controller_data not is supplied */
  1382. dev_dbg(&spi->dev,
  1383. "using default controller_data settings\n");
  1384. chip_info =
  1385. kzalloc(sizeof(struct pl022_config_chip), GFP_KERNEL);
  1386. if (!chip_info) {
  1387. dev_err(&spi->dev,
  1388. "cannot allocate controller data\n");
  1389. status = -ENOMEM;
  1390. goto err_first_setup;
  1391. }
  1392. dev_dbg(&spi->dev, "allocated memory for controller data\n");
  1393. /* Pointer back to the SPI device */
  1394. chip_info->dev = &spi->dev;
  1395. /*
  1396. * Set controller data default values:
  1397. * Polling is supported by default
  1398. */
  1399. chip_info->lbm = LOOPBACK_DISABLED;
  1400. chip_info->com_mode = POLLING_TRANSFER;
  1401. chip_info->iface = SSP_INTERFACE_MOTOROLA_SPI;
  1402. chip_info->hierarchy = SSP_SLAVE;
  1403. chip_info->slave_tx_disable = DO_NOT_DRIVE_TX;
  1404. chip_info->endian_tx = SSP_TX_LSB;
  1405. chip_info->endian_rx = SSP_RX_LSB;
  1406. chip_info->data_size = SSP_DATA_BITS_12;
  1407. chip_info->rx_lev_trig = SSP_RX_1_OR_MORE_ELEM;
  1408. chip_info->tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC;
  1409. chip_info->clk_phase = SSP_CLK_SECOND_EDGE;
  1410. chip_info->clk_pol = SSP_CLK_POL_IDLE_LOW;
  1411. chip_info->ctrl_len = SSP_BITS_8;
  1412. chip_info->wait_state = SSP_MWIRE_WAIT_ZERO;
  1413. chip_info->duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX;
  1414. chip_info->cs_control = null_cs_control;
  1415. } else {
  1416. dev_dbg(&spi->dev,
  1417. "using user supplied controller_data settings\n");
  1418. }
  1419. /*
  1420. * We can override with custom divisors, else we use the board
  1421. * frequency setting
  1422. */
  1423. if ((0 == chip_info->clk_freq.cpsdvsr)
  1424. && (0 == chip_info->clk_freq.scr)) {
  1425. status = calculate_effective_freq(pl022,
  1426. spi->max_speed_hz,
  1427. &chip_info->clk_freq);
  1428. if (status < 0)
  1429. goto err_config_params;
  1430. } else {
  1431. if ((chip_info->clk_freq.cpsdvsr % 2) != 0)
  1432. chip_info->clk_freq.cpsdvsr =
  1433. chip_info->clk_freq.cpsdvsr - 1;
  1434. }
  1435. status = verify_controller_parameters(pl022, chip_info);
  1436. if (status) {
  1437. dev_err(&spi->dev, "controller data is incorrect");
  1438. goto err_config_params;
  1439. }
  1440. /* Now set controller state based on controller data */
  1441. chip->xfer_type = chip_info->com_mode;
  1442. chip->cs_control = chip_info->cs_control;
  1443. if (chip_info->data_size <= 8) {
  1444. dev_dbg(&spi->dev, "1 <= n <=8 bits per word\n");
  1445. chip->n_bytes = 1;
  1446. chip->read = READING_U8;
  1447. chip->write = WRITING_U8;
  1448. } else if (chip_info->data_size <= 16) {
  1449. dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
  1450. chip->n_bytes = 2;
  1451. chip->read = READING_U16;
  1452. chip->write = WRITING_U16;
  1453. } else {
  1454. if (pl022->vendor->max_bpw >= 32) {
  1455. dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
  1456. chip->n_bytes = 4;
  1457. chip->read = READING_U32;
  1458. chip->write = WRITING_U32;
  1459. } else {
  1460. dev_err(&spi->dev,
  1461. "illegal data size for this controller!\n");
  1462. dev_err(&spi->dev,
  1463. "a standard pl022 can only handle "
  1464. "1 <= n <= 16 bit words\n");
  1465. goto err_config_params;
  1466. }
  1467. }
  1468. /* Now Initialize all register settings required for this chip */
  1469. chip->cr0 = 0;
  1470. chip->cr1 = 0;
  1471. chip->dmacr = 0;
  1472. chip->cpsr = 0;
  1473. if ((chip_info->com_mode == DMA_TRANSFER)
  1474. && ((pl022->master_info)->enable_dma)) {
  1475. chip->enable_dma = 1;
  1476. dev_dbg(&spi->dev, "DMA mode set in controller state\n");
  1477. status = process_dma_info(chip_info, chip);
  1478. if (status < 0)
  1479. goto err_config_params;
  1480. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1481. SSP_DMACR_MASK_RXDMAE, 0);
  1482. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1483. SSP_DMACR_MASK_TXDMAE, 1);
  1484. } else {
  1485. chip->enable_dma = 0;
  1486. dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
  1487. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1488. SSP_DMACR_MASK_RXDMAE, 0);
  1489. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1490. SSP_DMACR_MASK_TXDMAE, 1);
  1491. }
  1492. chip->cpsr = chip_info->clk_freq.cpsdvsr;
  1493. /* Special setup for the ST micro extended control registers */
  1494. if (pl022->vendor->extended_cr) {
  1495. if (pl022->vendor->pl023) {
  1496. /* These bits are only in the PL023 */
  1497. SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
  1498. SSP_CR1_MASK_FBCLKDEL_ST, 13);
  1499. } else {
  1500. /* These bits are in the PL022 but not PL023 */
  1501. SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
  1502. SSP_CR0_MASK_HALFDUP_ST, 5);
  1503. SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
  1504. SSP_CR0_MASK_CSS_ST, 16);
  1505. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1506. SSP_CR0_MASK_FRF_ST, 21);
  1507. SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
  1508. SSP_CR1_MASK_MWAIT_ST, 6);
  1509. }
  1510. SSP_WRITE_BITS(chip->cr0, chip_info->data_size,
  1511. SSP_CR0_MASK_DSS_ST, 0);
  1512. SSP_WRITE_BITS(chip->cr1, chip_info->endian_rx,
  1513. SSP_CR1_MASK_RENDN_ST, 4);
  1514. SSP_WRITE_BITS(chip->cr1, chip_info->endian_tx,
  1515. SSP_CR1_MASK_TENDN_ST, 5);
  1516. SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
  1517. SSP_CR1_MASK_RXIFLSEL_ST, 7);
  1518. SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
  1519. SSP_CR1_MASK_TXIFLSEL_ST, 10);
  1520. } else {
  1521. SSP_WRITE_BITS(chip->cr0, chip_info->data_size,
  1522. SSP_CR0_MASK_DSS, 0);
  1523. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1524. SSP_CR0_MASK_FRF, 4);
  1525. }
  1526. /* Stuff that is common for all versions */
  1527. SSP_WRITE_BITS(chip->cr0, chip_info->clk_pol, SSP_CR0_MASK_SPO, 6);
  1528. SSP_WRITE_BITS(chip->cr0, chip_info->clk_phase, SSP_CR0_MASK_SPH, 7);
  1529. SSP_WRITE_BITS(chip->cr0, chip_info->clk_freq.scr, SSP_CR0_MASK_SCR, 8);
  1530. /* Loopback is available on all versions except PL023 */
  1531. if (!pl022->vendor->pl023)
  1532. SSP_WRITE_BITS(chip->cr1, chip_info->lbm, SSP_CR1_MASK_LBM, 0);
  1533. SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
  1534. SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
  1535. SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 3);
  1536. /* Save controller_state */
  1537. spi_set_ctldata(spi, chip);
  1538. return status;
  1539. err_config_params:
  1540. err_first_setup:
  1541. kfree(chip);
  1542. return status;
  1543. }
  1544. /**
  1545. * pl022_cleanup - cleanup function registered to SPI master framework
  1546. * @spi: spi device which is requesting cleanup
  1547. *
  1548. * This function is registered to the SPI framework for this SPI master
  1549. * controller. It will free the runtime state of chip.
  1550. */
  1551. static void pl022_cleanup(struct spi_device *spi)
  1552. {
  1553. struct chip_data *chip = spi_get_ctldata(spi);
  1554. spi_set_ctldata(spi, NULL);
  1555. kfree(chip);
  1556. }
  1557. static int __init
  1558. pl022_probe(struct amba_device *adev, struct amba_id *id)
  1559. {
  1560. struct device *dev = &adev->dev;
  1561. struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
  1562. struct spi_master *master;
  1563. struct pl022 *pl022 = NULL; /*Data for this driver */
  1564. int status = 0;
  1565. dev_info(&adev->dev,
  1566. "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
  1567. if (platform_info == NULL) {
  1568. dev_err(&adev->dev, "probe - no platform data supplied\n");
  1569. status = -ENODEV;
  1570. goto err_no_pdata;
  1571. }
  1572. /* Allocate master with space for data */
  1573. master = spi_alloc_master(dev, sizeof(struct pl022));
  1574. if (master == NULL) {
  1575. dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
  1576. status = -ENOMEM;
  1577. goto err_no_master;
  1578. }
  1579. pl022 = spi_master_get_devdata(master);
  1580. pl022->master = master;
  1581. pl022->master_info = platform_info;
  1582. pl022->adev = adev;
  1583. pl022->vendor = id->data;
  1584. /*
  1585. * Bus Number Which has been Assigned to this SSP controller
  1586. * on this board
  1587. */
  1588. master->bus_num = platform_info->bus_id;
  1589. master->num_chipselect = platform_info->num_chipselect;
  1590. master->cleanup = pl022_cleanup;
  1591. master->setup = pl022_setup;
  1592. master->transfer = pl022_transfer;
  1593. dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
  1594. status = amba_request_regions(adev, NULL);
  1595. if (status)
  1596. goto err_no_ioregion;
  1597. pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
  1598. if (pl022->virtbase == NULL) {
  1599. status = -ENOMEM;
  1600. goto err_no_ioremap;
  1601. }
  1602. printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
  1603. adev->res.start, pl022->virtbase);
  1604. pl022->clk = clk_get(&adev->dev, NULL);
  1605. if (IS_ERR(pl022->clk)) {
  1606. status = PTR_ERR(pl022->clk);
  1607. dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
  1608. goto err_no_clk;
  1609. }
  1610. /* Disable SSP */
  1611. clk_enable(pl022->clk);
  1612. writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
  1613. SSP_CR1(pl022->virtbase));
  1614. load_ssp_default_config(pl022);
  1615. clk_disable(pl022->clk);
  1616. status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
  1617. pl022);
  1618. if (status < 0) {
  1619. dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
  1620. goto err_no_irq;
  1621. }
  1622. /* Initialize and start queue */
  1623. status = init_queue(pl022);
  1624. if (status != 0) {
  1625. dev_err(&adev->dev, "probe - problem initializing queue\n");
  1626. goto err_init_queue;
  1627. }
  1628. status = start_queue(pl022);
  1629. if (status != 0) {
  1630. dev_err(&adev->dev, "probe - problem starting queue\n");
  1631. goto err_start_queue;
  1632. }
  1633. /* Register with the SPI framework */
  1634. amba_set_drvdata(adev, pl022);
  1635. status = spi_register_master(master);
  1636. if (status != 0) {
  1637. dev_err(&adev->dev,
  1638. "probe - problem registering spi master\n");
  1639. goto err_spi_register;
  1640. }
  1641. dev_dbg(dev, "probe succeded\n");
  1642. return 0;
  1643. err_spi_register:
  1644. err_start_queue:
  1645. err_init_queue:
  1646. destroy_queue(pl022);
  1647. free_irq(adev->irq[0], pl022);
  1648. err_no_irq:
  1649. clk_put(pl022->clk);
  1650. err_no_clk:
  1651. iounmap(pl022->virtbase);
  1652. err_no_ioremap:
  1653. amba_release_regions(adev);
  1654. err_no_ioregion:
  1655. spi_master_put(master);
  1656. err_no_master:
  1657. err_no_pdata:
  1658. return status;
  1659. }
  1660. static int __exit
  1661. pl022_remove(struct amba_device *adev)
  1662. {
  1663. struct pl022 *pl022 = amba_get_drvdata(adev);
  1664. int status = 0;
  1665. if (!pl022)
  1666. return 0;
  1667. /* Remove the queue */
  1668. status = destroy_queue(pl022);
  1669. if (status != 0) {
  1670. dev_err(&adev->dev,
  1671. "queue remove failed (%d)\n", status);
  1672. return status;
  1673. }
  1674. load_ssp_default_config(pl022);
  1675. free_irq(adev->irq[0], pl022);
  1676. clk_disable(pl022->clk);
  1677. clk_put(pl022->clk);
  1678. iounmap(pl022->virtbase);
  1679. amba_release_regions(adev);
  1680. tasklet_disable(&pl022->pump_transfers);
  1681. spi_unregister_master(pl022->master);
  1682. spi_master_put(pl022->master);
  1683. amba_set_drvdata(adev, NULL);
  1684. dev_dbg(&adev->dev, "remove succeded\n");
  1685. return 0;
  1686. }
  1687. #ifdef CONFIG_PM
  1688. static int pl022_suspend(struct amba_device *adev, pm_message_t state)
  1689. {
  1690. struct pl022 *pl022 = amba_get_drvdata(adev);
  1691. int status = 0;
  1692. status = stop_queue(pl022);
  1693. if (status) {
  1694. dev_warn(&adev->dev, "suspend cannot stop queue\n");
  1695. return status;
  1696. }
  1697. clk_enable(pl022->clk);
  1698. load_ssp_default_config(pl022);
  1699. clk_disable(pl022->clk);
  1700. dev_dbg(&adev->dev, "suspended\n");
  1701. return 0;
  1702. }
  1703. static int pl022_resume(struct amba_device *adev)
  1704. {
  1705. struct pl022 *pl022 = amba_get_drvdata(adev);
  1706. int status = 0;
  1707. /* Start the queue running */
  1708. status = start_queue(pl022);
  1709. if (status)
  1710. dev_err(&adev->dev, "problem starting queue (%d)\n", status);
  1711. else
  1712. dev_dbg(&adev->dev, "resumed\n");
  1713. return status;
  1714. }
  1715. #else
  1716. #define pl022_suspend NULL
  1717. #define pl022_resume NULL
  1718. #endif /* CONFIG_PM */
  1719. static struct vendor_data vendor_arm = {
  1720. .fifodepth = 8,
  1721. .max_bpw = 16,
  1722. .unidir = false,
  1723. .extended_cr = false,
  1724. .pl023 = false,
  1725. };
  1726. static struct vendor_data vendor_st = {
  1727. .fifodepth = 32,
  1728. .max_bpw = 32,
  1729. .unidir = false,
  1730. .extended_cr = true,
  1731. .pl023 = false,
  1732. };
  1733. static struct vendor_data vendor_st_pl023 = {
  1734. .fifodepth = 32,
  1735. .max_bpw = 32,
  1736. .unidir = false,
  1737. .extended_cr = true,
  1738. .pl023 = true,
  1739. };
  1740. static struct amba_id pl022_ids[] = {
  1741. {
  1742. /*
  1743. * ARM PL022 variant, this has a 16bit wide
  1744. * and 8 locations deep TX/RX FIFO
  1745. */
  1746. .id = 0x00041022,
  1747. .mask = 0x000fffff,
  1748. .data = &vendor_arm,
  1749. },
  1750. {
  1751. /*
  1752. * ST Micro derivative, this has 32bit wide
  1753. * and 32 locations deep TX/RX FIFO
  1754. */
  1755. .id = 0x01080022,
  1756. .mask = 0xffffffff,
  1757. .data = &vendor_st,
  1758. },
  1759. {
  1760. /*
  1761. * ST-Ericsson derivative "PL023" (this is not
  1762. * an official ARM number), this is a PL022 SSP block
  1763. * stripped to SPI mode only, it has 32bit wide
  1764. * and 32 locations deep TX/RX FIFO but no extended
  1765. * CR0/CR1 register
  1766. */
  1767. .id = 0x00080023,
  1768. .mask = 0xffffffff,
  1769. .data = &vendor_st_pl023,
  1770. },
  1771. { 0, 0 },
  1772. };
  1773. static struct amba_driver pl022_driver = {
  1774. .drv = {
  1775. .name = "ssp-pl022",
  1776. },
  1777. .id_table = pl022_ids,
  1778. .probe = pl022_probe,
  1779. .remove = __exit_p(pl022_remove),
  1780. .suspend = pl022_suspend,
  1781. .resume = pl022_resume,
  1782. };
  1783. static int __init pl022_init(void)
  1784. {
  1785. return amba_driver_register(&pl022_driver);
  1786. }
  1787. module_init(pl022_init);
  1788. static void __exit pl022_exit(void)
  1789. {
  1790. amba_driver_unregister(&pl022_driver);
  1791. }
  1792. module_exit(pl022_exit);
  1793. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  1794. MODULE_DESCRIPTION("PL022 SSP Controller Driver");
  1795. MODULE_LICENSE("GPL");