spi-pl022.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  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/delay.h>
  32. #include <linux/clk.h>
  33. #include <linux/err.h>
  34. #include <linux/amba/bus.h>
  35. #include <linux/amba/pl022.h>
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/pm_runtime.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. * @kworker: thread struct for message pump
  297. * @kworker_task: pointer to task for message pump kworker thread
  298. * @pump_messages: work struct for scheduling work to the message pump
  299. * @queue_lock: spinlock to syncronise access to message queue
  300. * @queue: message queue
  301. * @busy: message pump is busy
  302. * @running: message pump is running
  303. * @pump_transfers: Tasklet used in Interrupt Transfer mode
  304. * @cur_msg: Pointer to current spi_message being processed
  305. * @cur_transfer: Pointer to current spi_transfer
  306. * @cur_chip: pointer to current clients chip(assigned from controller_state)
  307. * @next_msg_cs_active: the next message in the queue has been examined
  308. * and it was found that it uses the same chip select as the previous
  309. * message, so we left it active after the previous transfer, and it's
  310. * active already.
  311. * @tx: current position in TX buffer to be read
  312. * @tx_end: end position in TX buffer to be read
  313. * @rx: current position in RX buffer to be written
  314. * @rx_end: end position in RX buffer to be written
  315. * @read: the type of read currently going on
  316. * @write: the type of write currently going on
  317. * @exp_fifo_level: expected FIFO level
  318. * @dma_rx_channel: optional channel for RX DMA
  319. * @dma_tx_channel: optional channel for TX DMA
  320. * @sgt_rx: scattertable for the RX transfer
  321. * @sgt_tx: scattertable for the TX transfer
  322. * @dummypage: a dummy page used for driving data on the bus with DMA
  323. */
  324. struct pl022 {
  325. struct amba_device *adev;
  326. struct vendor_data *vendor;
  327. resource_size_t phybase;
  328. void __iomem *virtbase;
  329. struct clk *clk;
  330. struct spi_master *master;
  331. struct pl022_ssp_controller *master_info;
  332. /* Message per-transfer pump */
  333. struct tasklet_struct pump_transfers;
  334. struct spi_message *cur_msg;
  335. struct spi_transfer *cur_transfer;
  336. struct chip_data *cur_chip;
  337. bool next_msg_cs_active;
  338. void *tx;
  339. void *tx_end;
  340. void *rx;
  341. void *rx_end;
  342. enum ssp_reading read;
  343. enum ssp_writing write;
  344. u32 exp_fifo_level;
  345. enum ssp_rx_level_trig rx_lev_trig;
  346. enum ssp_tx_level_trig tx_lev_trig;
  347. /* DMA settings */
  348. #ifdef CONFIG_DMA_ENGINE
  349. struct dma_chan *dma_rx_channel;
  350. struct dma_chan *dma_tx_channel;
  351. struct sg_table sgt_rx;
  352. struct sg_table sgt_tx;
  353. char *dummypage;
  354. bool dma_running;
  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. pl022->next_msg_cs_active = false;
  407. last_transfer = list_entry(pl022->cur_msg->transfers.prev,
  408. struct spi_transfer,
  409. transfer_list);
  410. /* Delay if requested before any change in chip select */
  411. if (last_transfer->delay_usecs)
  412. /*
  413. * FIXME: This runs in interrupt context.
  414. * Is this really smart?
  415. */
  416. udelay(last_transfer->delay_usecs);
  417. if (!last_transfer->cs_change) {
  418. struct spi_message *next_msg;
  419. /*
  420. * cs_change was not set. We can keep the chip select
  421. * enabled if there is message in the queue and it is
  422. * for the same spi device.
  423. *
  424. * We cannot postpone this until pump_messages, because
  425. * after calling msg->complete (below) the driver that
  426. * sent the current message could be unloaded, which
  427. * could invalidate the cs_control() callback...
  428. */
  429. /* get a pointer to the next message, if any */
  430. next_msg = spi_get_next_queued_message(pl022->master);
  431. /*
  432. * see if the next and current messages point
  433. * to the same spi device.
  434. */
  435. if (next_msg && next_msg->spi != pl022->cur_msg->spi)
  436. next_msg = NULL;
  437. if (!next_msg || pl022->cur_msg->state == STATE_ERROR)
  438. pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
  439. else
  440. pl022->next_msg_cs_active = true;
  441. }
  442. pl022->cur_msg = NULL;
  443. pl022->cur_transfer = NULL;
  444. pl022->cur_chip = NULL;
  445. spi_finalize_current_message(pl022->master);
  446. }
  447. /**
  448. * flush - flush the FIFO to reach a clean state
  449. * @pl022: SSP driver private data structure
  450. */
  451. static int flush(struct pl022 *pl022)
  452. {
  453. unsigned long limit = loops_per_jiffy << 1;
  454. dev_dbg(&pl022->adev->dev, "flush\n");
  455. do {
  456. while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  457. readw(SSP_DR(pl022->virtbase));
  458. } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
  459. pl022->exp_fifo_level = 0;
  460. return limit;
  461. }
  462. /**
  463. * restore_state - Load configuration of current chip
  464. * @pl022: SSP driver private data structure
  465. */
  466. static void restore_state(struct pl022 *pl022)
  467. {
  468. struct chip_data *chip = pl022->cur_chip;
  469. if (pl022->vendor->extended_cr)
  470. writel(chip->cr0, SSP_CR0(pl022->virtbase));
  471. else
  472. writew(chip->cr0, SSP_CR0(pl022->virtbase));
  473. writew(chip->cr1, SSP_CR1(pl022->virtbase));
  474. writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
  475. writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
  476. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  477. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  478. }
  479. /*
  480. * Default SSP Register Values
  481. */
  482. #define DEFAULT_SSP_REG_CR0 ( \
  483. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
  484. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
  485. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  486. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  487. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  488. )
  489. /* ST versions have slightly different bit layout */
  490. #define DEFAULT_SSP_REG_CR0_ST ( \
  491. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  492. GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
  493. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  494. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  495. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
  496. GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
  497. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
  498. )
  499. /* The PL023 version is slightly different again */
  500. #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
  501. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  502. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  503. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  504. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  505. )
  506. #define DEFAULT_SSP_REG_CR1 ( \
  507. GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
  508. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  509. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  510. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
  511. )
  512. /* ST versions extend this register to use all 16 bits */
  513. #define DEFAULT_SSP_REG_CR1_ST ( \
  514. DEFAULT_SSP_REG_CR1 | \
  515. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  516. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  517. GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
  518. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  519. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
  520. )
  521. /*
  522. * The PL023 variant has further differences: no loopback mode, no microwire
  523. * support, and a new clock feedback delay setting.
  524. */
  525. #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
  526. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  527. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  528. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
  529. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  530. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  531. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  532. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
  533. GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
  534. )
  535. #define DEFAULT_SSP_REG_CPSR ( \
  536. GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
  537. )
  538. #define DEFAULT_SSP_REG_DMACR (\
  539. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
  540. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
  541. )
  542. /**
  543. * load_ssp_default_config - Load default configuration for SSP
  544. * @pl022: SSP driver private data structure
  545. */
  546. static void load_ssp_default_config(struct pl022 *pl022)
  547. {
  548. if (pl022->vendor->pl023) {
  549. writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
  550. writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
  551. } else if (pl022->vendor->extended_cr) {
  552. writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
  553. writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
  554. } else {
  555. writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
  556. writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
  557. }
  558. writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
  559. writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
  560. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  561. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  562. }
  563. /**
  564. * This will write to TX and read from RX according to the parameters
  565. * set in pl022.
  566. */
  567. static void readwriter(struct pl022 *pl022)
  568. {
  569. /*
  570. * The FIFO depth is different between primecell variants.
  571. * I believe filling in too much in the FIFO might cause
  572. * errons in 8bit wide transfers on ARM variants (just 8 words
  573. * FIFO, means only 8x8 = 64 bits in FIFO) at least.
  574. *
  575. * To prevent this issue, the TX FIFO is only filled to the
  576. * unused RX FIFO fill length, regardless of what the TX
  577. * FIFO status flag indicates.
  578. */
  579. dev_dbg(&pl022->adev->dev,
  580. "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
  581. __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
  582. /* Read as much as you can */
  583. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  584. && (pl022->rx < pl022->rx_end)) {
  585. switch (pl022->read) {
  586. case READING_NULL:
  587. readw(SSP_DR(pl022->virtbase));
  588. break;
  589. case READING_U8:
  590. *(u8 *) (pl022->rx) =
  591. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  592. break;
  593. case READING_U16:
  594. *(u16 *) (pl022->rx) =
  595. (u16) readw(SSP_DR(pl022->virtbase));
  596. break;
  597. case READING_U32:
  598. *(u32 *) (pl022->rx) =
  599. readl(SSP_DR(pl022->virtbase));
  600. break;
  601. }
  602. pl022->rx += (pl022->cur_chip->n_bytes);
  603. pl022->exp_fifo_level--;
  604. }
  605. /*
  606. * Write as much as possible up to the RX FIFO size
  607. */
  608. while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
  609. && (pl022->tx < pl022->tx_end)) {
  610. switch (pl022->write) {
  611. case WRITING_NULL:
  612. writew(0x0, SSP_DR(pl022->virtbase));
  613. break;
  614. case WRITING_U8:
  615. writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
  616. break;
  617. case WRITING_U16:
  618. writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
  619. break;
  620. case WRITING_U32:
  621. writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
  622. break;
  623. }
  624. pl022->tx += (pl022->cur_chip->n_bytes);
  625. pl022->exp_fifo_level++;
  626. /*
  627. * This inner reader takes care of things appearing in the RX
  628. * FIFO as we're transmitting. This will happen a lot since the
  629. * clock starts running when you put things into the TX FIFO,
  630. * and then things are continuously clocked into the RX FIFO.
  631. */
  632. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  633. && (pl022->rx < pl022->rx_end)) {
  634. switch (pl022->read) {
  635. case READING_NULL:
  636. readw(SSP_DR(pl022->virtbase));
  637. break;
  638. case READING_U8:
  639. *(u8 *) (pl022->rx) =
  640. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  641. break;
  642. case READING_U16:
  643. *(u16 *) (pl022->rx) =
  644. (u16) readw(SSP_DR(pl022->virtbase));
  645. break;
  646. case READING_U32:
  647. *(u32 *) (pl022->rx) =
  648. readl(SSP_DR(pl022->virtbase));
  649. break;
  650. }
  651. pl022->rx += (pl022->cur_chip->n_bytes);
  652. pl022->exp_fifo_level--;
  653. }
  654. }
  655. /*
  656. * When we exit here the TX FIFO should be full and the RX FIFO
  657. * should be empty
  658. */
  659. }
  660. /**
  661. * next_transfer - Move to the Next transfer in the current spi message
  662. * @pl022: SSP driver private data structure
  663. *
  664. * This function moves though the linked list of spi transfers in the
  665. * current spi message and returns with the state of current spi
  666. * message i.e whether its last transfer is done(STATE_DONE) or
  667. * Next transfer is ready(STATE_RUNNING)
  668. */
  669. static void *next_transfer(struct pl022 *pl022)
  670. {
  671. struct spi_message *msg = pl022->cur_msg;
  672. struct spi_transfer *trans = pl022->cur_transfer;
  673. /* Move to next transfer */
  674. if (trans->transfer_list.next != &msg->transfers) {
  675. pl022->cur_transfer =
  676. list_entry(trans->transfer_list.next,
  677. struct spi_transfer, transfer_list);
  678. return STATE_RUNNING;
  679. }
  680. return STATE_DONE;
  681. }
  682. /*
  683. * This DMA functionality is only compiled in if we have
  684. * access to the generic DMA devices/DMA engine.
  685. */
  686. #ifdef CONFIG_DMA_ENGINE
  687. static void unmap_free_dma_scatter(struct pl022 *pl022)
  688. {
  689. /* Unmap and free the SG tables */
  690. dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
  691. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  692. dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
  693. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  694. sg_free_table(&pl022->sgt_rx);
  695. sg_free_table(&pl022->sgt_tx);
  696. }
  697. static void dma_callback(void *data)
  698. {
  699. struct pl022 *pl022 = data;
  700. struct spi_message *msg = pl022->cur_msg;
  701. BUG_ON(!pl022->sgt_rx.sgl);
  702. #ifdef VERBOSE_DEBUG
  703. /*
  704. * Optionally dump out buffers to inspect contents, this is
  705. * good if you want to convince yourself that the loopback
  706. * read/write contents are the same, when adopting to a new
  707. * DMA engine.
  708. */
  709. {
  710. struct scatterlist *sg;
  711. unsigned int i;
  712. dma_sync_sg_for_cpu(&pl022->adev->dev,
  713. pl022->sgt_rx.sgl,
  714. pl022->sgt_rx.nents,
  715. DMA_FROM_DEVICE);
  716. for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
  717. dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
  718. print_hex_dump(KERN_ERR, "SPI RX: ",
  719. DUMP_PREFIX_OFFSET,
  720. 16,
  721. 1,
  722. sg_virt(sg),
  723. sg_dma_len(sg),
  724. 1);
  725. }
  726. for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
  727. dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
  728. print_hex_dump(KERN_ERR, "SPI TX: ",
  729. DUMP_PREFIX_OFFSET,
  730. 16,
  731. 1,
  732. sg_virt(sg),
  733. sg_dma_len(sg),
  734. 1);
  735. }
  736. }
  737. #endif
  738. unmap_free_dma_scatter(pl022);
  739. /* Update total bytes transferred */
  740. msg->actual_length += pl022->cur_transfer->len;
  741. if (pl022->cur_transfer->cs_change)
  742. pl022->cur_chip->
  743. cs_control(SSP_CHIP_DESELECT);
  744. /* Move to next transfer */
  745. msg->state = next_transfer(pl022);
  746. tasklet_schedule(&pl022->pump_transfers);
  747. }
  748. static void setup_dma_scatter(struct pl022 *pl022,
  749. void *buffer,
  750. unsigned int length,
  751. struct sg_table *sgtab)
  752. {
  753. struct scatterlist *sg;
  754. int bytesleft = length;
  755. void *bufp = buffer;
  756. int mapbytes;
  757. int i;
  758. if (buffer) {
  759. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  760. /*
  761. * If there are less bytes left than what fits
  762. * in the current page (plus page alignment offset)
  763. * we just feed in this, else we stuff in as much
  764. * as we can.
  765. */
  766. if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
  767. mapbytes = bytesleft;
  768. else
  769. mapbytes = PAGE_SIZE - offset_in_page(bufp);
  770. sg_set_page(sg, virt_to_page(bufp),
  771. mapbytes, offset_in_page(bufp));
  772. bufp += mapbytes;
  773. bytesleft -= mapbytes;
  774. dev_dbg(&pl022->adev->dev,
  775. "set RX/TX target page @ %p, %d bytes, %d left\n",
  776. bufp, mapbytes, bytesleft);
  777. }
  778. } else {
  779. /* Map the dummy buffer on every page */
  780. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  781. if (bytesleft < PAGE_SIZE)
  782. mapbytes = bytesleft;
  783. else
  784. mapbytes = PAGE_SIZE;
  785. sg_set_page(sg, virt_to_page(pl022->dummypage),
  786. mapbytes, 0);
  787. bytesleft -= mapbytes;
  788. dev_dbg(&pl022->adev->dev,
  789. "set RX/TX to dummy page %d bytes, %d left\n",
  790. mapbytes, bytesleft);
  791. }
  792. }
  793. BUG_ON(bytesleft);
  794. }
  795. /**
  796. * configure_dma - configures the channels for the next transfer
  797. * @pl022: SSP driver's private data structure
  798. */
  799. static int configure_dma(struct pl022 *pl022)
  800. {
  801. struct dma_slave_config rx_conf = {
  802. .src_addr = SSP_DR(pl022->phybase),
  803. .direction = DMA_DEV_TO_MEM,
  804. };
  805. struct dma_slave_config tx_conf = {
  806. .dst_addr = SSP_DR(pl022->phybase),
  807. .direction = DMA_MEM_TO_DEV,
  808. };
  809. unsigned int pages;
  810. int ret;
  811. int rx_sglen, tx_sglen;
  812. struct dma_chan *rxchan = pl022->dma_rx_channel;
  813. struct dma_chan *txchan = pl022->dma_tx_channel;
  814. struct dma_async_tx_descriptor *rxdesc;
  815. struct dma_async_tx_descriptor *txdesc;
  816. /* Check that the channels are available */
  817. if (!rxchan || !txchan)
  818. return -ENODEV;
  819. /*
  820. * If supplied, the DMA burstsize should equal the FIFO trigger level.
  821. * Notice that the DMA engine uses one-to-one mapping. Since we can
  822. * not trigger on 2 elements this needs explicit mapping rather than
  823. * calculation.
  824. */
  825. switch (pl022->rx_lev_trig) {
  826. case SSP_RX_1_OR_MORE_ELEM:
  827. rx_conf.src_maxburst = 1;
  828. break;
  829. case SSP_RX_4_OR_MORE_ELEM:
  830. rx_conf.src_maxburst = 4;
  831. break;
  832. case SSP_RX_8_OR_MORE_ELEM:
  833. rx_conf.src_maxburst = 8;
  834. break;
  835. case SSP_RX_16_OR_MORE_ELEM:
  836. rx_conf.src_maxburst = 16;
  837. break;
  838. case SSP_RX_32_OR_MORE_ELEM:
  839. rx_conf.src_maxburst = 32;
  840. break;
  841. default:
  842. rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1;
  843. break;
  844. }
  845. switch (pl022->tx_lev_trig) {
  846. case SSP_TX_1_OR_MORE_EMPTY_LOC:
  847. tx_conf.dst_maxburst = 1;
  848. break;
  849. case SSP_TX_4_OR_MORE_EMPTY_LOC:
  850. tx_conf.dst_maxburst = 4;
  851. break;
  852. case SSP_TX_8_OR_MORE_EMPTY_LOC:
  853. tx_conf.dst_maxburst = 8;
  854. break;
  855. case SSP_TX_16_OR_MORE_EMPTY_LOC:
  856. tx_conf.dst_maxburst = 16;
  857. break;
  858. case SSP_TX_32_OR_MORE_EMPTY_LOC:
  859. tx_conf.dst_maxburst = 32;
  860. break;
  861. default:
  862. tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1;
  863. break;
  864. }
  865. switch (pl022->read) {
  866. case READING_NULL:
  867. /* Use the same as for writing */
  868. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  869. break;
  870. case READING_U8:
  871. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  872. break;
  873. case READING_U16:
  874. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  875. break;
  876. case READING_U32:
  877. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  878. break;
  879. }
  880. switch (pl022->write) {
  881. case WRITING_NULL:
  882. /* Use the same as for reading */
  883. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  884. break;
  885. case WRITING_U8:
  886. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  887. break;
  888. case WRITING_U16:
  889. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  890. break;
  891. case WRITING_U32:
  892. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  893. break;
  894. }
  895. /* SPI pecularity: we need to read and write the same width */
  896. if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  897. rx_conf.src_addr_width = tx_conf.dst_addr_width;
  898. if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  899. tx_conf.dst_addr_width = rx_conf.src_addr_width;
  900. BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
  901. dmaengine_slave_config(rxchan, &rx_conf);
  902. dmaengine_slave_config(txchan, &tx_conf);
  903. /* Create sglists for the transfers */
  904. pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE);
  905. dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
  906. ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC);
  907. if (ret)
  908. goto err_alloc_rx_sg;
  909. ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC);
  910. if (ret)
  911. goto err_alloc_tx_sg;
  912. /* Fill in the scatterlists for the RX+TX buffers */
  913. setup_dma_scatter(pl022, pl022->rx,
  914. pl022->cur_transfer->len, &pl022->sgt_rx);
  915. setup_dma_scatter(pl022, pl022->tx,
  916. pl022->cur_transfer->len, &pl022->sgt_tx);
  917. /* Map DMA buffers */
  918. rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  919. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  920. if (!rx_sglen)
  921. goto err_rx_sgmap;
  922. tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  923. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  924. if (!tx_sglen)
  925. goto err_tx_sgmap;
  926. /* Send both scatterlists */
  927. rxdesc = rxchan->device->device_prep_slave_sg(rxchan,
  928. pl022->sgt_rx.sgl,
  929. rx_sglen,
  930. DMA_DEV_TO_MEM,
  931. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  932. if (!rxdesc)
  933. goto err_rxdesc;
  934. txdesc = txchan->device->device_prep_slave_sg(txchan,
  935. pl022->sgt_tx.sgl,
  936. tx_sglen,
  937. DMA_MEM_TO_DEV,
  938. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  939. if (!txdesc)
  940. goto err_txdesc;
  941. /* Put the callback on the RX transfer only, that should finish last */
  942. rxdesc->callback = dma_callback;
  943. rxdesc->callback_param = pl022;
  944. /* Submit and fire RX and TX with TX last so we're ready to read! */
  945. dmaengine_submit(rxdesc);
  946. dmaengine_submit(txdesc);
  947. dma_async_issue_pending(rxchan);
  948. dma_async_issue_pending(txchan);
  949. pl022->dma_running = true;
  950. return 0;
  951. err_txdesc:
  952. dmaengine_terminate_all(txchan);
  953. err_rxdesc:
  954. dmaengine_terminate_all(rxchan);
  955. dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  956. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  957. err_tx_sgmap:
  958. dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  959. pl022->sgt_tx.nents, DMA_FROM_DEVICE);
  960. err_rx_sgmap:
  961. sg_free_table(&pl022->sgt_tx);
  962. err_alloc_tx_sg:
  963. sg_free_table(&pl022->sgt_rx);
  964. err_alloc_rx_sg:
  965. return -ENOMEM;
  966. }
  967. static int __devinit pl022_dma_probe(struct pl022 *pl022)
  968. {
  969. dma_cap_mask_t mask;
  970. /* Try to acquire a generic DMA engine slave channel */
  971. dma_cap_zero(mask);
  972. dma_cap_set(DMA_SLAVE, mask);
  973. /*
  974. * We need both RX and TX channels to do DMA, else do none
  975. * of them.
  976. */
  977. pl022->dma_rx_channel = dma_request_channel(mask,
  978. pl022->master_info->dma_filter,
  979. pl022->master_info->dma_rx_param);
  980. if (!pl022->dma_rx_channel) {
  981. dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n");
  982. goto err_no_rxchan;
  983. }
  984. pl022->dma_tx_channel = dma_request_channel(mask,
  985. pl022->master_info->dma_filter,
  986. pl022->master_info->dma_tx_param);
  987. if (!pl022->dma_tx_channel) {
  988. dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n");
  989. goto err_no_txchan;
  990. }
  991. pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
  992. if (!pl022->dummypage) {
  993. dev_dbg(&pl022->adev->dev, "no DMA dummypage!\n");
  994. goto err_no_dummypage;
  995. }
  996. dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
  997. dma_chan_name(pl022->dma_rx_channel),
  998. dma_chan_name(pl022->dma_tx_channel));
  999. return 0;
  1000. err_no_dummypage:
  1001. dma_release_channel(pl022->dma_tx_channel);
  1002. err_no_txchan:
  1003. dma_release_channel(pl022->dma_rx_channel);
  1004. pl022->dma_rx_channel = NULL;
  1005. err_no_rxchan:
  1006. dev_err(&pl022->adev->dev,
  1007. "Failed to work in dma mode, work without dma!\n");
  1008. return -ENODEV;
  1009. }
  1010. static void terminate_dma(struct pl022 *pl022)
  1011. {
  1012. struct dma_chan *rxchan = pl022->dma_rx_channel;
  1013. struct dma_chan *txchan = pl022->dma_tx_channel;
  1014. dmaengine_terminate_all(rxchan);
  1015. dmaengine_terminate_all(txchan);
  1016. unmap_free_dma_scatter(pl022);
  1017. pl022->dma_running = false;
  1018. }
  1019. static void pl022_dma_remove(struct pl022 *pl022)
  1020. {
  1021. if (pl022->dma_running)
  1022. terminate_dma(pl022);
  1023. if (pl022->dma_tx_channel)
  1024. dma_release_channel(pl022->dma_tx_channel);
  1025. if (pl022->dma_rx_channel)
  1026. dma_release_channel(pl022->dma_rx_channel);
  1027. kfree(pl022->dummypage);
  1028. }
  1029. #else
  1030. static inline int configure_dma(struct pl022 *pl022)
  1031. {
  1032. return -ENODEV;
  1033. }
  1034. static inline int pl022_dma_probe(struct pl022 *pl022)
  1035. {
  1036. return 0;
  1037. }
  1038. static inline void pl022_dma_remove(struct pl022 *pl022)
  1039. {
  1040. }
  1041. #endif
  1042. /**
  1043. * pl022_interrupt_handler - Interrupt handler for SSP controller
  1044. *
  1045. * This function handles interrupts generated for an interrupt based transfer.
  1046. * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
  1047. * current message's state as STATE_ERROR and schedule the tasklet
  1048. * pump_transfers which will do the postprocessing of the current message by
  1049. * calling giveback(). Otherwise it reads data from RX FIFO till there is no
  1050. * more data, and writes data in TX FIFO till it is not full. If we complete
  1051. * the transfer we move to the next transfer and schedule the tasklet.
  1052. */
  1053. static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
  1054. {
  1055. struct pl022 *pl022 = dev_id;
  1056. struct spi_message *msg = pl022->cur_msg;
  1057. u16 irq_status = 0;
  1058. u16 flag = 0;
  1059. if (unlikely(!msg)) {
  1060. dev_err(&pl022->adev->dev,
  1061. "bad message state in interrupt handler");
  1062. /* Never fail */
  1063. return IRQ_HANDLED;
  1064. }
  1065. /* Read the Interrupt Status Register */
  1066. irq_status = readw(SSP_MIS(pl022->virtbase));
  1067. if (unlikely(!irq_status))
  1068. return IRQ_NONE;
  1069. /*
  1070. * This handles the FIFO interrupts, the timeout
  1071. * interrupts are flatly ignored, they cannot be
  1072. * trusted.
  1073. */
  1074. if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
  1075. /*
  1076. * Overrun interrupt - bail out since our Data has been
  1077. * corrupted
  1078. */
  1079. dev_err(&pl022->adev->dev, "FIFO overrun\n");
  1080. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
  1081. dev_err(&pl022->adev->dev,
  1082. "RXFIFO is full\n");
  1083. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
  1084. dev_err(&pl022->adev->dev,
  1085. "TXFIFO is full\n");
  1086. /*
  1087. * Disable and clear interrupts, disable SSP,
  1088. * mark message with bad status so it can be
  1089. * retried.
  1090. */
  1091. writew(DISABLE_ALL_INTERRUPTS,
  1092. SSP_IMSC(pl022->virtbase));
  1093. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1094. writew((readw(SSP_CR1(pl022->virtbase)) &
  1095. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1096. msg->state = STATE_ERROR;
  1097. /* Schedule message queue handler */
  1098. tasklet_schedule(&pl022->pump_transfers);
  1099. return IRQ_HANDLED;
  1100. }
  1101. readwriter(pl022);
  1102. if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
  1103. flag = 1;
  1104. /* Disable Transmit interrupt, enable receive interrupt */
  1105. writew((readw(SSP_IMSC(pl022->virtbase)) &
  1106. ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM,
  1107. SSP_IMSC(pl022->virtbase));
  1108. }
  1109. /*
  1110. * Since all transactions must write as much as shall be read,
  1111. * we can conclude the entire transaction once RX is complete.
  1112. * At this point, all TX will always be finished.
  1113. */
  1114. if (pl022->rx >= pl022->rx_end) {
  1115. writew(DISABLE_ALL_INTERRUPTS,
  1116. SSP_IMSC(pl022->virtbase));
  1117. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1118. if (unlikely(pl022->rx > pl022->rx_end)) {
  1119. dev_warn(&pl022->adev->dev, "read %u surplus "
  1120. "bytes (did you request an odd "
  1121. "number of bytes on a 16bit bus?)\n",
  1122. (u32) (pl022->rx - pl022->rx_end));
  1123. }
  1124. /* Update total bytes transferred */
  1125. msg->actual_length += pl022->cur_transfer->len;
  1126. if (pl022->cur_transfer->cs_change)
  1127. pl022->cur_chip->
  1128. cs_control(SSP_CHIP_DESELECT);
  1129. /* Move to next transfer */
  1130. msg->state = next_transfer(pl022);
  1131. tasklet_schedule(&pl022->pump_transfers);
  1132. return IRQ_HANDLED;
  1133. }
  1134. return IRQ_HANDLED;
  1135. }
  1136. /**
  1137. * This sets up the pointers to memory for the next message to
  1138. * send out on the SPI bus.
  1139. */
  1140. static int set_up_next_transfer(struct pl022 *pl022,
  1141. struct spi_transfer *transfer)
  1142. {
  1143. int residue;
  1144. /* Sanity check the message for this bus width */
  1145. residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
  1146. if (unlikely(residue != 0)) {
  1147. dev_err(&pl022->adev->dev,
  1148. "message of %u bytes to transmit but the current "
  1149. "chip bus has a data width of %u bytes!\n",
  1150. pl022->cur_transfer->len,
  1151. pl022->cur_chip->n_bytes);
  1152. dev_err(&pl022->adev->dev, "skipping this message\n");
  1153. return -EIO;
  1154. }
  1155. pl022->tx = (void *)transfer->tx_buf;
  1156. pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
  1157. pl022->rx = (void *)transfer->rx_buf;
  1158. pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
  1159. pl022->write =
  1160. pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
  1161. pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
  1162. return 0;
  1163. }
  1164. /**
  1165. * pump_transfers - Tasklet function which schedules next transfer
  1166. * when running in interrupt or DMA transfer mode.
  1167. * @data: SSP driver private data structure
  1168. *
  1169. */
  1170. static void pump_transfers(unsigned long data)
  1171. {
  1172. struct pl022 *pl022 = (struct pl022 *) data;
  1173. struct spi_message *message = NULL;
  1174. struct spi_transfer *transfer = NULL;
  1175. struct spi_transfer *previous = NULL;
  1176. /* Get current state information */
  1177. message = pl022->cur_msg;
  1178. transfer = pl022->cur_transfer;
  1179. /* Handle for abort */
  1180. if (message->state == STATE_ERROR) {
  1181. message->status = -EIO;
  1182. giveback(pl022);
  1183. return;
  1184. }
  1185. /* Handle end of message */
  1186. if (message->state == STATE_DONE) {
  1187. message->status = 0;
  1188. giveback(pl022);
  1189. return;
  1190. }
  1191. /* Delay if requested at end of transfer before CS change */
  1192. if (message->state == STATE_RUNNING) {
  1193. previous = list_entry(transfer->transfer_list.prev,
  1194. struct spi_transfer,
  1195. transfer_list);
  1196. if (previous->delay_usecs)
  1197. /*
  1198. * FIXME: This runs in interrupt context.
  1199. * Is this really smart?
  1200. */
  1201. udelay(previous->delay_usecs);
  1202. /* Reselect chip select only if cs_change was requested */
  1203. if (previous->cs_change)
  1204. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1205. } else {
  1206. /* STATE_START */
  1207. message->state = STATE_RUNNING;
  1208. }
  1209. if (set_up_next_transfer(pl022, transfer)) {
  1210. message->state = STATE_ERROR;
  1211. message->status = -EIO;
  1212. giveback(pl022);
  1213. return;
  1214. }
  1215. /* Flush the FIFOs and let's go! */
  1216. flush(pl022);
  1217. if (pl022->cur_chip->enable_dma) {
  1218. if (configure_dma(pl022)) {
  1219. dev_dbg(&pl022->adev->dev,
  1220. "configuration of DMA failed, fall back to interrupt mode\n");
  1221. goto err_config_dma;
  1222. }
  1223. return;
  1224. }
  1225. err_config_dma:
  1226. /* enable all interrupts except RX */
  1227. writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase));
  1228. }
  1229. static void do_interrupt_dma_transfer(struct pl022 *pl022)
  1230. {
  1231. /*
  1232. * Default is to enable all interrupts except RX -
  1233. * this will be enabled once TX is complete
  1234. */
  1235. u32 irqflags = ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM;
  1236. /* Enable target chip, if not already active */
  1237. if (!pl022->next_msg_cs_active)
  1238. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1239. if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
  1240. /* Error path */
  1241. pl022->cur_msg->state = STATE_ERROR;
  1242. pl022->cur_msg->status = -EIO;
  1243. giveback(pl022);
  1244. return;
  1245. }
  1246. /* If we're using DMA, set up DMA here */
  1247. if (pl022->cur_chip->enable_dma) {
  1248. /* Configure DMA transfer */
  1249. if (configure_dma(pl022)) {
  1250. dev_dbg(&pl022->adev->dev,
  1251. "configuration of DMA failed, fall back to interrupt mode\n");
  1252. goto err_config_dma;
  1253. }
  1254. /* Disable interrupts in DMA mode, IRQ from DMA controller */
  1255. irqflags = DISABLE_ALL_INTERRUPTS;
  1256. }
  1257. err_config_dma:
  1258. /* Enable SSP, turn on interrupts */
  1259. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1260. SSP_CR1(pl022->virtbase));
  1261. writew(irqflags, SSP_IMSC(pl022->virtbase));
  1262. }
  1263. static void do_polling_transfer(struct pl022 *pl022)
  1264. {
  1265. struct spi_message *message = NULL;
  1266. struct spi_transfer *transfer = NULL;
  1267. struct spi_transfer *previous = NULL;
  1268. struct chip_data *chip;
  1269. unsigned long time, timeout;
  1270. chip = pl022->cur_chip;
  1271. message = pl022->cur_msg;
  1272. while (message->state != STATE_DONE) {
  1273. /* Handle for abort */
  1274. if (message->state == STATE_ERROR)
  1275. break;
  1276. transfer = pl022->cur_transfer;
  1277. /* Delay if requested at end of transfer */
  1278. if (message->state == STATE_RUNNING) {
  1279. previous =
  1280. list_entry(transfer->transfer_list.prev,
  1281. struct spi_transfer, transfer_list);
  1282. if (previous->delay_usecs)
  1283. udelay(previous->delay_usecs);
  1284. if (previous->cs_change)
  1285. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1286. } else {
  1287. /* STATE_START */
  1288. message->state = STATE_RUNNING;
  1289. if (!pl022->next_msg_cs_active)
  1290. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1291. }
  1292. /* Configuration Changing Per Transfer */
  1293. if (set_up_next_transfer(pl022, transfer)) {
  1294. /* Error path */
  1295. message->state = STATE_ERROR;
  1296. break;
  1297. }
  1298. /* Flush FIFOs and enable SSP */
  1299. flush(pl022);
  1300. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1301. SSP_CR1(pl022->virtbase));
  1302. dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
  1303. timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT);
  1304. while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) {
  1305. time = jiffies;
  1306. readwriter(pl022);
  1307. if (time_after(time, timeout)) {
  1308. dev_warn(&pl022->adev->dev,
  1309. "%s: timeout!\n", __func__);
  1310. message->state = STATE_ERROR;
  1311. goto out;
  1312. }
  1313. cpu_relax();
  1314. }
  1315. /* Update total byte transferred */
  1316. message->actual_length += pl022->cur_transfer->len;
  1317. if (pl022->cur_transfer->cs_change)
  1318. pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
  1319. /* Move to next transfer */
  1320. message->state = next_transfer(pl022);
  1321. }
  1322. out:
  1323. /* Handle end of message */
  1324. if (message->state == STATE_DONE)
  1325. message->status = 0;
  1326. else
  1327. message->status = -EIO;
  1328. giveback(pl022);
  1329. return;
  1330. }
  1331. static int pl022_transfer_one_message(struct spi_master *master,
  1332. struct spi_message *msg)
  1333. {
  1334. struct pl022 *pl022 = spi_master_get_devdata(master);
  1335. /* Initial message state */
  1336. pl022->cur_msg = msg;
  1337. msg->state = STATE_START;
  1338. pl022->cur_transfer = list_entry(msg->transfers.next,
  1339. struct spi_transfer, transfer_list);
  1340. /* Setup the SPI using the per chip configuration */
  1341. pl022->cur_chip = spi_get_ctldata(msg->spi);
  1342. restore_state(pl022);
  1343. flush(pl022);
  1344. if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
  1345. do_polling_transfer(pl022);
  1346. else
  1347. do_interrupt_dma_transfer(pl022);
  1348. return 0;
  1349. }
  1350. static int pl022_prepare_transfer_hardware(struct spi_master *master)
  1351. {
  1352. struct pl022 *pl022 = spi_master_get_devdata(master);
  1353. /*
  1354. * Just make sure we have all we need to run the transfer by syncing
  1355. * with the runtime PM framework.
  1356. */
  1357. pm_runtime_get_sync(&pl022->adev->dev);
  1358. return 0;
  1359. }
  1360. static int pl022_unprepare_transfer_hardware(struct spi_master *master)
  1361. {
  1362. struct pl022 *pl022 = spi_master_get_devdata(master);
  1363. /* nothing more to do - disable spi/ssp and power off */
  1364. writew((readw(SSP_CR1(pl022->virtbase)) &
  1365. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1366. if (pl022->master_info->autosuspend_delay > 0) {
  1367. pm_runtime_mark_last_busy(&pl022->adev->dev);
  1368. pm_runtime_put_autosuspend(&pl022->adev->dev);
  1369. } else {
  1370. pm_runtime_put(&pl022->adev->dev);
  1371. }
  1372. return 0;
  1373. }
  1374. static int verify_controller_parameters(struct pl022 *pl022,
  1375. struct pl022_config_chip const *chip_info)
  1376. {
  1377. if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
  1378. || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
  1379. dev_err(&pl022->adev->dev,
  1380. "interface is configured incorrectly\n");
  1381. return -EINVAL;
  1382. }
  1383. if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
  1384. (!pl022->vendor->unidir)) {
  1385. dev_err(&pl022->adev->dev,
  1386. "unidirectional mode not supported in this "
  1387. "hardware version\n");
  1388. return -EINVAL;
  1389. }
  1390. if ((chip_info->hierarchy != SSP_MASTER)
  1391. && (chip_info->hierarchy != SSP_SLAVE)) {
  1392. dev_err(&pl022->adev->dev,
  1393. "hierarchy is configured incorrectly\n");
  1394. return -EINVAL;
  1395. }
  1396. if ((chip_info->com_mode != INTERRUPT_TRANSFER)
  1397. && (chip_info->com_mode != DMA_TRANSFER)
  1398. && (chip_info->com_mode != POLLING_TRANSFER)) {
  1399. dev_err(&pl022->adev->dev,
  1400. "Communication mode is configured incorrectly\n");
  1401. return -EINVAL;
  1402. }
  1403. switch (chip_info->rx_lev_trig) {
  1404. case SSP_RX_1_OR_MORE_ELEM:
  1405. case SSP_RX_4_OR_MORE_ELEM:
  1406. case SSP_RX_8_OR_MORE_ELEM:
  1407. /* These are always OK, all variants can handle this */
  1408. break;
  1409. case SSP_RX_16_OR_MORE_ELEM:
  1410. if (pl022->vendor->fifodepth < 16) {
  1411. dev_err(&pl022->adev->dev,
  1412. "RX FIFO Trigger Level is configured incorrectly\n");
  1413. return -EINVAL;
  1414. }
  1415. break;
  1416. case SSP_RX_32_OR_MORE_ELEM:
  1417. if (pl022->vendor->fifodepth < 32) {
  1418. dev_err(&pl022->adev->dev,
  1419. "RX FIFO Trigger Level is configured incorrectly\n");
  1420. return -EINVAL;
  1421. }
  1422. break;
  1423. default:
  1424. dev_err(&pl022->adev->dev,
  1425. "RX FIFO Trigger Level is configured incorrectly\n");
  1426. return -EINVAL;
  1427. break;
  1428. }
  1429. switch (chip_info->tx_lev_trig) {
  1430. case SSP_TX_1_OR_MORE_EMPTY_LOC:
  1431. case SSP_TX_4_OR_MORE_EMPTY_LOC:
  1432. case SSP_TX_8_OR_MORE_EMPTY_LOC:
  1433. /* These are always OK, all variants can handle this */
  1434. break;
  1435. case SSP_TX_16_OR_MORE_EMPTY_LOC:
  1436. if (pl022->vendor->fifodepth < 16) {
  1437. dev_err(&pl022->adev->dev,
  1438. "TX FIFO Trigger Level is configured incorrectly\n");
  1439. return -EINVAL;
  1440. }
  1441. break;
  1442. case SSP_TX_32_OR_MORE_EMPTY_LOC:
  1443. if (pl022->vendor->fifodepth < 32) {
  1444. dev_err(&pl022->adev->dev,
  1445. "TX FIFO Trigger Level is configured incorrectly\n");
  1446. return -EINVAL;
  1447. }
  1448. break;
  1449. default:
  1450. dev_err(&pl022->adev->dev,
  1451. "TX FIFO Trigger Level is configured incorrectly\n");
  1452. return -EINVAL;
  1453. break;
  1454. }
  1455. if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
  1456. if ((chip_info->ctrl_len < SSP_BITS_4)
  1457. || (chip_info->ctrl_len > SSP_BITS_32)) {
  1458. dev_err(&pl022->adev->dev,
  1459. "CTRL LEN is configured incorrectly\n");
  1460. return -EINVAL;
  1461. }
  1462. if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
  1463. && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
  1464. dev_err(&pl022->adev->dev,
  1465. "Wait State is configured incorrectly\n");
  1466. return -EINVAL;
  1467. }
  1468. /* Half duplex is only available in the ST Micro version */
  1469. if (pl022->vendor->extended_cr) {
  1470. if ((chip_info->duplex !=
  1471. SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1472. && (chip_info->duplex !=
  1473. SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
  1474. dev_err(&pl022->adev->dev,
  1475. "Microwire duplex mode is configured incorrectly\n");
  1476. return -EINVAL;
  1477. }
  1478. } else {
  1479. if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1480. dev_err(&pl022->adev->dev,
  1481. "Microwire half duplex mode requested,"
  1482. " but this is only available in the"
  1483. " ST version of PL022\n");
  1484. return -EINVAL;
  1485. }
  1486. }
  1487. return 0;
  1488. }
  1489. static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
  1490. {
  1491. return rate / (cpsdvsr * (1 + scr));
  1492. }
  1493. static int calculate_effective_freq(struct pl022 *pl022, int freq, struct
  1494. ssp_clock_params * clk_freq)
  1495. {
  1496. /* Lets calculate the frequency parameters */
  1497. u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN;
  1498. u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0,
  1499. best_scr = 0, tmp, found = 0;
  1500. rate = clk_get_rate(pl022->clk);
  1501. /* cpsdvscr = 2 & scr 0 */
  1502. max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN);
  1503. /* cpsdvsr = 254 & scr = 255 */
  1504. min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX);
  1505. if (!((freq <= max_tclk) && (freq >= min_tclk))) {
  1506. dev_err(&pl022->adev->dev,
  1507. "controller data is incorrect: out of range frequency");
  1508. return -EINVAL;
  1509. }
  1510. /*
  1511. * best_freq will give closest possible available rate (<= requested
  1512. * freq) for all values of scr & cpsdvsr.
  1513. */
  1514. while ((cpsdvsr <= CPSDVR_MAX) && !found) {
  1515. while (scr <= SCR_MAX) {
  1516. tmp = spi_rate(rate, cpsdvsr, scr);
  1517. if (tmp > freq)
  1518. scr++;
  1519. /*
  1520. * If found exact value, update and break.
  1521. * If found more closer value, update and continue.
  1522. */
  1523. else if ((tmp == freq) || (tmp > best_freq)) {
  1524. best_freq = tmp;
  1525. best_cpsdvsr = cpsdvsr;
  1526. best_scr = scr;
  1527. if (tmp == freq)
  1528. break;
  1529. }
  1530. scr++;
  1531. }
  1532. cpsdvsr += 2;
  1533. scr = SCR_MIN;
  1534. }
  1535. clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF);
  1536. clk_freq->scr = (u8) (best_scr & 0xFF);
  1537. dev_dbg(&pl022->adev->dev,
  1538. "SSP Target Frequency is: %u, Effective Frequency is %u\n",
  1539. freq, best_freq);
  1540. dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n",
  1541. clk_freq->cpsdvsr, clk_freq->scr);
  1542. return 0;
  1543. }
  1544. /*
  1545. * A piece of default chip info unless the platform
  1546. * supplies it.
  1547. */
  1548. static const struct pl022_config_chip pl022_default_chip_info = {
  1549. .com_mode = POLLING_TRANSFER,
  1550. .iface = SSP_INTERFACE_MOTOROLA_SPI,
  1551. .hierarchy = SSP_SLAVE,
  1552. .slave_tx_disable = DO_NOT_DRIVE_TX,
  1553. .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
  1554. .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
  1555. .ctrl_len = SSP_BITS_8,
  1556. .wait_state = SSP_MWIRE_WAIT_ZERO,
  1557. .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
  1558. .cs_control = null_cs_control,
  1559. };
  1560. /**
  1561. * pl022_setup - setup function registered to SPI master framework
  1562. * @spi: spi device which is requesting setup
  1563. *
  1564. * This function is registered to the SPI framework for this SPI master
  1565. * controller. If it is the first time when setup is called by this device,
  1566. * this function will initialize the runtime state for this chip and save
  1567. * the same in the device structure. Else it will update the runtime info
  1568. * with the updated chip info. Nothing is really being written to the
  1569. * controller hardware here, that is not done until the actual transfer
  1570. * commence.
  1571. */
  1572. static int pl022_setup(struct spi_device *spi)
  1573. {
  1574. struct pl022_config_chip const *chip_info;
  1575. struct chip_data *chip;
  1576. struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0};
  1577. int status = 0;
  1578. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1579. unsigned int bits = spi->bits_per_word;
  1580. u32 tmp;
  1581. if (!spi->max_speed_hz)
  1582. return -EINVAL;
  1583. /* Get controller_state if one is supplied */
  1584. chip = spi_get_ctldata(spi);
  1585. if (chip == NULL) {
  1586. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1587. if (!chip) {
  1588. dev_err(&spi->dev,
  1589. "cannot allocate controller state\n");
  1590. return -ENOMEM;
  1591. }
  1592. dev_dbg(&spi->dev,
  1593. "allocated memory for controller's runtime state\n");
  1594. }
  1595. /* Get controller data if one is supplied */
  1596. chip_info = spi->controller_data;
  1597. if (chip_info == NULL) {
  1598. chip_info = &pl022_default_chip_info;
  1599. /* spi_board_info.controller_data not is supplied */
  1600. dev_dbg(&spi->dev,
  1601. "using default controller_data settings\n");
  1602. } else
  1603. dev_dbg(&spi->dev,
  1604. "using user supplied controller_data settings\n");
  1605. /*
  1606. * We can override with custom divisors, else we use the board
  1607. * frequency setting
  1608. */
  1609. if ((0 == chip_info->clk_freq.cpsdvsr)
  1610. && (0 == chip_info->clk_freq.scr)) {
  1611. status = calculate_effective_freq(pl022,
  1612. spi->max_speed_hz,
  1613. &clk_freq);
  1614. if (status < 0)
  1615. goto err_config_params;
  1616. } else {
  1617. memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
  1618. if ((clk_freq.cpsdvsr % 2) != 0)
  1619. clk_freq.cpsdvsr =
  1620. clk_freq.cpsdvsr - 1;
  1621. }
  1622. if ((clk_freq.cpsdvsr < CPSDVR_MIN)
  1623. || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
  1624. status = -EINVAL;
  1625. dev_err(&spi->dev,
  1626. "cpsdvsr is configured incorrectly\n");
  1627. goto err_config_params;
  1628. }
  1629. status = verify_controller_parameters(pl022, chip_info);
  1630. if (status) {
  1631. dev_err(&spi->dev, "controller data is incorrect");
  1632. goto err_config_params;
  1633. }
  1634. pl022->rx_lev_trig = chip_info->rx_lev_trig;
  1635. pl022->tx_lev_trig = chip_info->tx_lev_trig;
  1636. /* Now set controller state based on controller data */
  1637. chip->xfer_type = chip_info->com_mode;
  1638. if (!chip_info->cs_control) {
  1639. chip->cs_control = null_cs_control;
  1640. dev_warn(&spi->dev,
  1641. "chip select function is NULL for this chip\n");
  1642. } else
  1643. chip->cs_control = chip_info->cs_control;
  1644. if (bits <= 3) {
  1645. /* PL022 doesn't support less than 4-bits */
  1646. status = -ENOTSUPP;
  1647. goto err_config_params;
  1648. } else if (bits <= 8) {
  1649. dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
  1650. chip->n_bytes = 1;
  1651. chip->read = READING_U8;
  1652. chip->write = WRITING_U8;
  1653. } else if (bits <= 16) {
  1654. dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
  1655. chip->n_bytes = 2;
  1656. chip->read = READING_U16;
  1657. chip->write = WRITING_U16;
  1658. } else {
  1659. if (pl022->vendor->max_bpw >= 32) {
  1660. dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
  1661. chip->n_bytes = 4;
  1662. chip->read = READING_U32;
  1663. chip->write = WRITING_U32;
  1664. } else {
  1665. dev_err(&spi->dev,
  1666. "illegal data size for this controller!\n");
  1667. dev_err(&spi->dev,
  1668. "a standard pl022 can only handle "
  1669. "1 <= n <= 16 bit words\n");
  1670. status = -ENOTSUPP;
  1671. goto err_config_params;
  1672. }
  1673. }
  1674. /* Now Initialize all register settings required for this chip */
  1675. chip->cr0 = 0;
  1676. chip->cr1 = 0;
  1677. chip->dmacr = 0;
  1678. chip->cpsr = 0;
  1679. if ((chip_info->com_mode == DMA_TRANSFER)
  1680. && ((pl022->master_info)->enable_dma)) {
  1681. chip->enable_dma = true;
  1682. dev_dbg(&spi->dev, "DMA mode set in controller state\n");
  1683. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1684. SSP_DMACR_MASK_RXDMAE, 0);
  1685. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1686. SSP_DMACR_MASK_TXDMAE, 1);
  1687. } else {
  1688. chip->enable_dma = false;
  1689. dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
  1690. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1691. SSP_DMACR_MASK_RXDMAE, 0);
  1692. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1693. SSP_DMACR_MASK_TXDMAE, 1);
  1694. }
  1695. chip->cpsr = clk_freq.cpsdvsr;
  1696. /* Special setup for the ST micro extended control registers */
  1697. if (pl022->vendor->extended_cr) {
  1698. u32 etx;
  1699. if (pl022->vendor->pl023) {
  1700. /* These bits are only in the PL023 */
  1701. SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
  1702. SSP_CR1_MASK_FBCLKDEL_ST, 13);
  1703. } else {
  1704. /* These bits are in the PL022 but not PL023 */
  1705. SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
  1706. SSP_CR0_MASK_HALFDUP_ST, 5);
  1707. SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
  1708. SSP_CR0_MASK_CSS_ST, 16);
  1709. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1710. SSP_CR0_MASK_FRF_ST, 21);
  1711. SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
  1712. SSP_CR1_MASK_MWAIT_ST, 6);
  1713. }
  1714. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1715. SSP_CR0_MASK_DSS_ST, 0);
  1716. if (spi->mode & SPI_LSB_FIRST) {
  1717. tmp = SSP_RX_LSB;
  1718. etx = SSP_TX_LSB;
  1719. } else {
  1720. tmp = SSP_RX_MSB;
  1721. etx = SSP_TX_MSB;
  1722. }
  1723. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
  1724. SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
  1725. SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
  1726. SSP_CR1_MASK_RXIFLSEL_ST, 7);
  1727. SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
  1728. SSP_CR1_MASK_TXIFLSEL_ST, 10);
  1729. } else {
  1730. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1731. SSP_CR0_MASK_DSS, 0);
  1732. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1733. SSP_CR0_MASK_FRF, 4);
  1734. }
  1735. /* Stuff that is common for all versions */
  1736. if (spi->mode & SPI_CPOL)
  1737. tmp = SSP_CLK_POL_IDLE_HIGH;
  1738. else
  1739. tmp = SSP_CLK_POL_IDLE_LOW;
  1740. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
  1741. if (spi->mode & SPI_CPHA)
  1742. tmp = SSP_CLK_SECOND_EDGE;
  1743. else
  1744. tmp = SSP_CLK_FIRST_EDGE;
  1745. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
  1746. SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
  1747. /* Loopback is available on all versions except PL023 */
  1748. if (pl022->vendor->loopback) {
  1749. if (spi->mode & SPI_LOOP)
  1750. tmp = LOOPBACK_ENABLED;
  1751. else
  1752. tmp = LOOPBACK_DISABLED;
  1753. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
  1754. }
  1755. SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
  1756. SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
  1757. SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD,
  1758. 3);
  1759. /* Save controller_state */
  1760. spi_set_ctldata(spi, chip);
  1761. return status;
  1762. err_config_params:
  1763. spi_set_ctldata(spi, NULL);
  1764. kfree(chip);
  1765. return status;
  1766. }
  1767. /**
  1768. * pl022_cleanup - cleanup function registered to SPI master framework
  1769. * @spi: spi device which is requesting cleanup
  1770. *
  1771. * This function is registered to the SPI framework for this SPI master
  1772. * controller. It will free the runtime state of chip.
  1773. */
  1774. static void pl022_cleanup(struct spi_device *spi)
  1775. {
  1776. struct chip_data *chip = spi_get_ctldata(spi);
  1777. spi_set_ctldata(spi, NULL);
  1778. kfree(chip);
  1779. }
  1780. static int __devinit
  1781. pl022_probe(struct amba_device *adev, const struct amba_id *id)
  1782. {
  1783. struct device *dev = &adev->dev;
  1784. struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
  1785. struct spi_master *master;
  1786. struct pl022 *pl022 = NULL; /*Data for this driver */
  1787. int status = 0;
  1788. dev_info(&adev->dev,
  1789. "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
  1790. if (platform_info == NULL) {
  1791. dev_err(&adev->dev, "probe - no platform data supplied\n");
  1792. status = -ENODEV;
  1793. goto err_no_pdata;
  1794. }
  1795. /* Allocate master with space for data */
  1796. master = spi_alloc_master(dev, sizeof(struct pl022));
  1797. if (master == NULL) {
  1798. dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
  1799. status = -ENOMEM;
  1800. goto err_no_master;
  1801. }
  1802. pl022 = spi_master_get_devdata(master);
  1803. pl022->master = master;
  1804. pl022->master_info = platform_info;
  1805. pl022->adev = adev;
  1806. pl022->vendor = id->data;
  1807. /*
  1808. * Bus Number Which has been Assigned to this SSP controller
  1809. * on this board
  1810. */
  1811. master->bus_num = platform_info->bus_id;
  1812. master->num_chipselect = platform_info->num_chipselect;
  1813. master->cleanup = pl022_cleanup;
  1814. master->setup = pl022_setup;
  1815. master->prepare_transfer_hardware = pl022_prepare_transfer_hardware;
  1816. master->transfer_one_message = pl022_transfer_one_message;
  1817. master->unprepare_transfer_hardware = pl022_unprepare_transfer_hardware;
  1818. master->rt = platform_info->rt;
  1819. /*
  1820. * Supports mode 0-3, loopback, and active low CS. Transfers are
  1821. * always MS bit first on the original pl022.
  1822. */
  1823. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1824. if (pl022->vendor->extended_cr)
  1825. master->mode_bits |= SPI_LSB_FIRST;
  1826. dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
  1827. status = amba_request_regions(adev, NULL);
  1828. if (status)
  1829. goto err_no_ioregion;
  1830. pl022->phybase = adev->res.start;
  1831. pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
  1832. if (pl022->virtbase == NULL) {
  1833. status = -ENOMEM;
  1834. goto err_no_ioremap;
  1835. }
  1836. printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
  1837. adev->res.start, pl022->virtbase);
  1838. pl022->clk = clk_get(&adev->dev, NULL);
  1839. if (IS_ERR(pl022->clk)) {
  1840. status = PTR_ERR(pl022->clk);
  1841. dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
  1842. goto err_no_clk;
  1843. }
  1844. status = clk_prepare(pl022->clk);
  1845. if (status) {
  1846. dev_err(&adev->dev, "could not prepare SSP/SPI bus clock\n");
  1847. goto err_clk_prep;
  1848. }
  1849. status = clk_enable(pl022->clk);
  1850. if (status) {
  1851. dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n");
  1852. goto err_no_clk_en;
  1853. }
  1854. /* Initialize transfer pump */
  1855. tasklet_init(&pl022->pump_transfers, pump_transfers,
  1856. (unsigned long)pl022);
  1857. /* Disable SSP */
  1858. writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
  1859. SSP_CR1(pl022->virtbase));
  1860. load_ssp_default_config(pl022);
  1861. status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
  1862. pl022);
  1863. if (status < 0) {
  1864. dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
  1865. goto err_no_irq;
  1866. }
  1867. /* Get DMA channels */
  1868. if (platform_info->enable_dma) {
  1869. status = pl022_dma_probe(pl022);
  1870. if (status != 0)
  1871. platform_info->enable_dma = 0;
  1872. }
  1873. /* Register with the SPI framework */
  1874. amba_set_drvdata(adev, pl022);
  1875. status = spi_register_master(master);
  1876. if (status != 0) {
  1877. dev_err(&adev->dev,
  1878. "probe - problem registering spi master\n");
  1879. goto err_spi_register;
  1880. }
  1881. dev_dbg(dev, "probe succeeded\n");
  1882. /* let runtime pm put suspend */
  1883. if (platform_info->autosuspend_delay > 0) {
  1884. dev_info(&adev->dev,
  1885. "will use autosuspend for runtime pm, delay %dms\n",
  1886. platform_info->autosuspend_delay);
  1887. pm_runtime_set_autosuspend_delay(dev,
  1888. platform_info->autosuspend_delay);
  1889. pm_runtime_use_autosuspend(dev);
  1890. pm_runtime_put_autosuspend(dev);
  1891. } else {
  1892. pm_runtime_put(dev);
  1893. }
  1894. return 0;
  1895. err_spi_register:
  1896. if (platform_info->enable_dma)
  1897. pl022_dma_remove(pl022);
  1898. free_irq(adev->irq[0], pl022);
  1899. err_no_irq:
  1900. clk_disable(pl022->clk);
  1901. err_no_clk_en:
  1902. clk_unprepare(pl022->clk);
  1903. err_clk_prep:
  1904. clk_put(pl022->clk);
  1905. err_no_clk:
  1906. iounmap(pl022->virtbase);
  1907. err_no_ioremap:
  1908. amba_release_regions(adev);
  1909. err_no_ioregion:
  1910. spi_master_put(master);
  1911. err_no_master:
  1912. err_no_pdata:
  1913. return status;
  1914. }
  1915. static int __devexit
  1916. pl022_remove(struct amba_device *adev)
  1917. {
  1918. struct pl022 *pl022 = amba_get_drvdata(adev);
  1919. if (!pl022)
  1920. return 0;
  1921. /*
  1922. * undo pm_runtime_put() in probe. I assume that we're not
  1923. * accessing the primecell here.
  1924. */
  1925. pm_runtime_get_noresume(&adev->dev);
  1926. load_ssp_default_config(pl022);
  1927. if (pl022->master_info->enable_dma)
  1928. pl022_dma_remove(pl022);
  1929. free_irq(adev->irq[0], pl022);
  1930. clk_disable(pl022->clk);
  1931. clk_unprepare(pl022->clk);
  1932. clk_put(pl022->clk);
  1933. iounmap(pl022->virtbase);
  1934. amba_release_regions(adev);
  1935. tasklet_disable(&pl022->pump_transfers);
  1936. spi_unregister_master(pl022->master);
  1937. spi_master_put(pl022->master);
  1938. amba_set_drvdata(adev, NULL);
  1939. return 0;
  1940. }
  1941. #ifdef CONFIG_SUSPEND
  1942. static int pl022_suspend(struct device *dev)
  1943. {
  1944. struct pl022 *pl022 = dev_get_drvdata(dev);
  1945. int ret;
  1946. ret = spi_master_suspend(pl022->master);
  1947. if (ret) {
  1948. dev_warn(dev, "cannot suspend master\n");
  1949. return ret;
  1950. }
  1951. dev_dbg(dev, "suspended\n");
  1952. return 0;
  1953. }
  1954. static int pl022_resume(struct device *dev)
  1955. {
  1956. struct pl022 *pl022 = dev_get_drvdata(dev);
  1957. int ret;
  1958. /* Start the queue running */
  1959. ret = spi_master_resume(pl022->master);
  1960. if (ret)
  1961. dev_err(dev, "problem starting queue (%d)\n", ret);
  1962. else
  1963. dev_dbg(dev, "resumed\n");
  1964. return ret;
  1965. }
  1966. #endif /* CONFIG_PM */
  1967. #ifdef CONFIG_PM_RUNTIME
  1968. static int pl022_runtime_suspend(struct device *dev)
  1969. {
  1970. struct pl022 *pl022 = dev_get_drvdata(dev);
  1971. clk_disable(pl022->clk);
  1972. amba_vcore_disable(pl022->adev);
  1973. return 0;
  1974. }
  1975. static int pl022_runtime_resume(struct device *dev)
  1976. {
  1977. struct pl022 *pl022 = dev_get_drvdata(dev);
  1978. amba_vcore_enable(pl022->adev);
  1979. clk_enable(pl022->clk);
  1980. return 0;
  1981. }
  1982. #endif
  1983. static const struct dev_pm_ops pl022_dev_pm_ops = {
  1984. SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume)
  1985. SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL)
  1986. };
  1987. static struct vendor_data vendor_arm = {
  1988. .fifodepth = 8,
  1989. .max_bpw = 16,
  1990. .unidir = false,
  1991. .extended_cr = false,
  1992. .pl023 = false,
  1993. .loopback = true,
  1994. };
  1995. static struct vendor_data vendor_st = {
  1996. .fifodepth = 32,
  1997. .max_bpw = 32,
  1998. .unidir = false,
  1999. .extended_cr = true,
  2000. .pl023 = false,
  2001. .loopback = true,
  2002. };
  2003. static struct vendor_data vendor_st_pl023 = {
  2004. .fifodepth = 32,
  2005. .max_bpw = 32,
  2006. .unidir = false,
  2007. .extended_cr = true,
  2008. .pl023 = true,
  2009. .loopback = false,
  2010. };
  2011. static struct vendor_data vendor_db5500_pl023 = {
  2012. .fifodepth = 32,
  2013. .max_bpw = 32,
  2014. .unidir = false,
  2015. .extended_cr = true,
  2016. .pl023 = true,
  2017. .loopback = true,
  2018. };
  2019. static struct amba_id pl022_ids[] = {
  2020. {
  2021. /*
  2022. * ARM PL022 variant, this has a 16bit wide
  2023. * and 8 locations deep TX/RX FIFO
  2024. */
  2025. .id = 0x00041022,
  2026. .mask = 0x000fffff,
  2027. .data = &vendor_arm,
  2028. },
  2029. {
  2030. /*
  2031. * ST Micro derivative, this has 32bit wide
  2032. * and 32 locations deep TX/RX FIFO
  2033. */
  2034. .id = 0x01080022,
  2035. .mask = 0xffffffff,
  2036. .data = &vendor_st,
  2037. },
  2038. {
  2039. /*
  2040. * ST-Ericsson derivative "PL023" (this is not
  2041. * an official ARM number), this is a PL022 SSP block
  2042. * stripped to SPI mode only, it has 32bit wide
  2043. * and 32 locations deep TX/RX FIFO but no extended
  2044. * CR0/CR1 register
  2045. */
  2046. .id = 0x00080023,
  2047. .mask = 0xffffffff,
  2048. .data = &vendor_st_pl023,
  2049. },
  2050. {
  2051. .id = 0x10080023,
  2052. .mask = 0xffffffff,
  2053. .data = &vendor_db5500_pl023,
  2054. },
  2055. { 0, 0 },
  2056. };
  2057. MODULE_DEVICE_TABLE(amba, pl022_ids);
  2058. static struct amba_driver pl022_driver = {
  2059. .drv = {
  2060. .name = "ssp-pl022",
  2061. .pm = &pl022_dev_pm_ops,
  2062. },
  2063. .id_table = pl022_ids,
  2064. .probe = pl022_probe,
  2065. .remove = __devexit_p(pl022_remove),
  2066. };
  2067. static int __init pl022_init(void)
  2068. {
  2069. return amba_driver_register(&pl022_driver);
  2070. }
  2071. subsys_initcall(pl022_init);
  2072. static void __exit pl022_exit(void)
  2073. {
  2074. amba_driver_unregister(&pl022_driver);
  2075. }
  2076. module_exit(pl022_exit);
  2077. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  2078. MODULE_DESCRIPTION("PL022 SSP Controller Driver");
  2079. MODULE_LICENSE("GPL");