amba-pl022.c 64 KB

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