amba-pl022.c 54 KB

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