spi-pl022.c 68 KB

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