spi-pl022.c 68 KB

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