spi-pl022.c 70 KB

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