amba-pl022.c 52 KB

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