spi-pl022.c 66 KB

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