amba-pl022.c 56 KB

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