spi-stm32.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // STMicroelectronics STM32 SPI Controller driver
  4. //
  5. // Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. // Author(s): Amelie Delaunay <amelie.delaunay@st.com> for STMicroelectronics.
  7. #include <linux/bitfield.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/reset.h>
  20. #include <linux/spi/spi.h>
  21. #define DRIVER_NAME "spi_stm32"
  22. /* STM32F4/7 SPI registers */
  23. #define STM32FX_SPI_CR1 0x00
  24. #define STM32FX_SPI_CR2 0x04
  25. #define STM32FX_SPI_SR 0x08
  26. #define STM32FX_SPI_DR 0x0C
  27. #define STM32FX_SPI_I2SCFGR 0x1C
  28. /* STM32FX_SPI_CR1 bit fields */
  29. #define STM32FX_SPI_CR1_CPHA BIT(0)
  30. #define STM32FX_SPI_CR1_CPOL BIT(1)
  31. #define STM32FX_SPI_CR1_MSTR BIT(2)
  32. #define STM32FX_SPI_CR1_BR_SHIFT 3
  33. #define STM32FX_SPI_CR1_BR GENMASK(5, 3)
  34. #define STM32FX_SPI_CR1_SPE BIT(6)
  35. #define STM32FX_SPI_CR1_LSBFRST BIT(7)
  36. #define STM32FX_SPI_CR1_SSI BIT(8)
  37. #define STM32FX_SPI_CR1_SSM BIT(9)
  38. #define STM32FX_SPI_CR1_RXONLY BIT(10)
  39. #define STM32F4_SPI_CR1_DFF BIT(11)
  40. #define STM32F7_SPI_CR1_CRCL BIT(11)
  41. #define STM32FX_SPI_CR1_CRCNEXT BIT(12)
  42. #define STM32FX_SPI_CR1_CRCEN BIT(13)
  43. #define STM32FX_SPI_CR1_BIDIOE BIT(14)
  44. #define STM32FX_SPI_CR1_BIDIMODE BIT(15)
  45. #define STM32FX_SPI_CR1_BR_MIN 0
  46. #define STM32FX_SPI_CR1_BR_MAX (GENMASK(5, 3) >> 3)
  47. /* STM32FX_SPI_CR2 bit fields */
  48. #define STM32FX_SPI_CR2_RXDMAEN BIT(0)
  49. #define STM32FX_SPI_CR2_TXDMAEN BIT(1)
  50. #define STM32FX_SPI_CR2_SSOE BIT(2)
  51. #define STM32FX_SPI_CR2_FRF BIT(4)
  52. #define STM32FX_SPI_CR2_ERRIE BIT(5)
  53. #define STM32FX_SPI_CR2_RXNEIE BIT(6)
  54. #define STM32FX_SPI_CR2_TXEIE BIT(7)
  55. #define STM32F7_SPI_CR2_DS GENMASK(11, 8)
  56. #define STM32F7_SPI_CR2_FRXTH BIT(12)
  57. #define STM32F7_SPI_CR2_LDMA_RX BIT(13)
  58. #define STM32F7_SPI_CR2_LDMA_TX BIT(14)
  59. /* STM32FX_SPI_SR bit fields */
  60. #define STM32FX_SPI_SR_RXNE BIT(0)
  61. #define STM32FX_SPI_SR_TXE BIT(1)
  62. #define STM32FX_SPI_SR_CHSIDE BIT(2)
  63. #define STM32FX_SPI_SR_UDR BIT(3)
  64. #define STM32FX_SPI_SR_CRCERR BIT(4)
  65. #define STM32FX_SPI_SR_MODF BIT(5)
  66. #define STM32FX_SPI_SR_OVR BIT(6)
  67. #define STM32FX_SPI_SR_BSY BIT(7)
  68. #define STM32FX_SPI_SR_FRE BIT(8)
  69. #define STM32F7_SPI_SR_FRLVL GENMASK(10, 9)
  70. #define STM32F7_SPI_SR_FTLVL GENMASK(12, 11)
  71. /* STM32FX_SPI_I2SCFGR bit fields */
  72. #define STM32FX_SPI_I2SCFGR_I2SMOD BIT(11)
  73. /* STM32F4 SPI Baud Rate min/max divisor */
  74. #define STM32FX_SPI_BR_DIV_MIN (2 << STM32FX_SPI_CR1_BR_MIN)
  75. #define STM32FX_SPI_BR_DIV_MAX (2 << STM32FX_SPI_CR1_BR_MAX)
  76. /* STM32H7 SPI registers */
  77. #define STM32H7_SPI_CR1 0x00
  78. #define STM32H7_SPI_CR2 0x04
  79. #define STM32H7_SPI_CFG1 0x08
  80. #define STM32H7_SPI_CFG2 0x0C
  81. #define STM32H7_SPI_IER 0x10
  82. #define STM32H7_SPI_SR 0x14
  83. #define STM32H7_SPI_IFCR 0x18
  84. #define STM32H7_SPI_TXDR 0x20
  85. #define STM32H7_SPI_RXDR 0x30
  86. #define STM32H7_SPI_I2SCFGR 0x50
  87. /* STM32H7_SPI_CR1 bit fields */
  88. #define STM32H7_SPI_CR1_SPE BIT(0)
  89. #define STM32H7_SPI_CR1_MASRX BIT(8)
  90. #define STM32H7_SPI_CR1_CSTART BIT(9)
  91. #define STM32H7_SPI_CR1_CSUSP BIT(10)
  92. #define STM32H7_SPI_CR1_HDDIR BIT(11)
  93. #define STM32H7_SPI_CR1_SSI BIT(12)
  94. /* STM32H7_SPI_CR2 bit fields */
  95. #define STM32H7_SPI_CR2_TSIZE GENMASK(15, 0)
  96. #define STM32H7_SPI_TSIZE_MAX GENMASK(15, 0)
  97. /* STM32H7_SPI_CFG1 bit fields */
  98. #define STM32H7_SPI_CFG1_DSIZE GENMASK(4, 0)
  99. #define STM32H7_SPI_CFG1_FTHLV GENMASK(8, 5)
  100. #define STM32H7_SPI_CFG1_RXDMAEN BIT(14)
  101. #define STM32H7_SPI_CFG1_TXDMAEN BIT(15)
  102. #define STM32H7_SPI_CFG1_MBR GENMASK(30, 28)
  103. #define STM32H7_SPI_CFG1_MBR_SHIFT 28
  104. #define STM32H7_SPI_CFG1_MBR_MIN 0
  105. #define STM32H7_SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28)
  106. /* STM32H7_SPI_CFG2 bit fields */
  107. #define STM32H7_SPI_CFG2_MIDI GENMASK(7, 4)
  108. #define STM32H7_SPI_CFG2_COMM GENMASK(18, 17)
  109. #define STM32H7_SPI_CFG2_SP GENMASK(21, 19)
  110. #define STM32H7_SPI_CFG2_MASTER BIT(22)
  111. #define STM32H7_SPI_CFG2_LSBFRST BIT(23)
  112. #define STM32H7_SPI_CFG2_CPHA BIT(24)
  113. #define STM32H7_SPI_CFG2_CPOL BIT(25)
  114. #define STM32H7_SPI_CFG2_SSM BIT(26)
  115. #define STM32H7_SPI_CFG2_SSIOP BIT(28)
  116. #define STM32H7_SPI_CFG2_AFCNTR BIT(31)
  117. /* STM32H7_SPI_IER bit fields */
  118. #define STM32H7_SPI_IER_RXPIE BIT(0)
  119. #define STM32H7_SPI_IER_TXPIE BIT(1)
  120. #define STM32H7_SPI_IER_DXPIE BIT(2)
  121. #define STM32H7_SPI_IER_EOTIE BIT(3)
  122. #define STM32H7_SPI_IER_TXTFIE BIT(4)
  123. #define STM32H7_SPI_IER_OVRIE BIT(6)
  124. #define STM32H7_SPI_IER_MODFIE BIT(9)
  125. #define STM32H7_SPI_IER_ALL GENMASK(10, 0)
  126. /* STM32H7_SPI_SR bit fields */
  127. #define STM32H7_SPI_SR_RXP BIT(0)
  128. #define STM32H7_SPI_SR_TXP BIT(1)
  129. #define STM32H7_SPI_SR_EOT BIT(3)
  130. #define STM32H7_SPI_SR_OVR BIT(6)
  131. #define STM32H7_SPI_SR_MODF BIT(9)
  132. #define STM32H7_SPI_SR_SUSP BIT(11)
  133. #define STM32H7_SPI_SR_RXPLVL GENMASK(14, 13)
  134. #define STM32H7_SPI_SR_RXWNE BIT(15)
  135. /* STM32H7_SPI_IFCR bit fields */
  136. #define STM32H7_SPI_IFCR_ALL GENMASK(11, 3)
  137. /* STM32H7_SPI_I2SCFGR bit fields */
  138. #define STM32H7_SPI_I2SCFGR_I2SMOD BIT(0)
  139. /* STM32MP25 SPI registers bit fields */
  140. #define STM32MP25_SPI_HWCFGR1 0x3F0
  141. /* STM32MP25_SPI_CR2 bit fields */
  142. #define STM32MP25_SPI_TSIZE_MAX_LIMITED GENMASK(9, 0)
  143. /* STM32MP25_SPI_HWCFGR1 */
  144. #define STM32MP25_SPI_HWCFGR1_FULLCFG GENMASK(27, 24)
  145. #define STM32MP25_SPI_HWCFGR1_FULLCFG_LIMITED 0x0
  146. #define STM32MP25_SPI_HWCFGR1_FULLCFG_FULL 0x1
  147. #define STM32MP25_SPI_HWCFGR1_DSCFG GENMASK(19, 16)
  148. #define STM32MP25_SPI_HWCFGR1_DSCFG_16_B 0x0
  149. #define STM32MP25_SPI_HWCFGR1_DSCFG_32_B 0x1
  150. /* STM32H7 SPI Master Baud Rate min/max divisor */
  151. #define STM32H7_SPI_MBR_DIV_MIN (2 << STM32H7_SPI_CFG1_MBR_MIN)
  152. #define STM32H7_SPI_MBR_DIV_MAX (2 << STM32H7_SPI_CFG1_MBR_MAX)
  153. /* STM32H7 SPI Communication mode */
  154. #define STM32H7_SPI_FULL_DUPLEX 0
  155. #define STM32H7_SPI_SIMPLEX_TX 1
  156. #define STM32H7_SPI_SIMPLEX_RX 2
  157. #define STM32H7_SPI_HALF_DUPLEX 3
  158. /* SPI Communication type */
  159. #define SPI_FULL_DUPLEX 0
  160. #define SPI_SIMPLEX_TX 1
  161. #define SPI_SIMPLEX_RX 2
  162. #define SPI_3WIRE_TX 3
  163. #define SPI_3WIRE_RX 4
  164. #define STM32_SPI_AUTOSUSPEND_DELAY 1 /* 1 ms */
  165. /*
  166. * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers
  167. * without fifo buffers.
  168. */
  169. #define SPI_DMA_MIN_BYTES 16
  170. /* STM32 SPI driver helpers */
  171. #define STM32_SPI_HOST_MODE(stm32_spi) (!(stm32_spi)->device_mode)
  172. #define STM32_SPI_DEVICE_MODE(stm32_spi) ((stm32_spi)->device_mode)
  173. /**
  174. * struct stm32_spi_reg - stm32 SPI register & bitfield desc
  175. * @reg: register offset
  176. * @mask: bitfield mask
  177. * @shift: left shift
  178. */
  179. struct stm32_spi_reg {
  180. int reg;
  181. int mask;
  182. int shift;
  183. };
  184. /**
  185. * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data
  186. * @en: enable register and SPI enable bit
  187. * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit
  188. * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit
  189. * @cpol: clock polarity register and polarity bit
  190. * @cpha: clock phase register and phase bit
  191. * @lsb_first: LSB transmitted first register and bit
  192. * @cs_high: chips select active value
  193. * @br: baud rate register and bitfields
  194. * @rx: SPI RX data register
  195. * @tx: SPI TX data register
  196. * @fullcfg: SPI full or limited feature set register
  197. */
  198. struct stm32_spi_regspec {
  199. const struct stm32_spi_reg en;
  200. const struct stm32_spi_reg dma_rx_en;
  201. const struct stm32_spi_reg dma_tx_en;
  202. const struct stm32_spi_reg cpol;
  203. const struct stm32_spi_reg cpha;
  204. const struct stm32_spi_reg lsb_first;
  205. const struct stm32_spi_reg cs_high;
  206. const struct stm32_spi_reg br;
  207. const struct stm32_spi_reg rx;
  208. const struct stm32_spi_reg tx;
  209. const struct stm32_spi_reg fullcfg;
  210. };
  211. struct stm32_spi;
  212. /**
  213. * struct stm32_spi_cfg - stm32 compatible configuration data
  214. * @regs: registers descriptions
  215. * @get_fifo_size: routine to get fifo size
  216. * @get_bpw_mask: routine to get bits per word mask
  217. * @disable: routine to disable controller
  218. * @config: routine to configure controller as SPI Host
  219. * @set_bpw: routine to configure registers to for bits per word
  220. * @set_mode: routine to configure registers to desired mode
  221. * @set_data_idleness: optional routine to configure registers to desired idle
  222. * time between frames (if driver has this functionality)
  223. * @set_number_of_data: optional routine to configure registers to desired
  224. * number of data (if driver has this functionality)
  225. * @write_tx: routine to write to transmit register/FIFO
  226. * @read_rx: routine to read from receive register/FIFO
  227. * @transfer_one_dma_start: routine to start transfer a single spi_transfer
  228. * using DMA
  229. * @dma_rx_cb: routine to call after DMA RX channel operation is complete
  230. * @dma_tx_cb: routine to call after DMA TX channel operation is complete
  231. * @transfer_one_irq: routine to configure interrupts for driver
  232. * @irq_handler_event: Interrupt handler for SPI controller events
  233. * @irq_handler_thread: thread of interrupt handler for SPI controller
  234. * @baud_rate_div_min: minimum baud rate divisor
  235. * @baud_rate_div_max: maximum baud rate divisor
  236. * @has_fifo: boolean to know if fifo is used for driver
  237. * @has_device_mode: is this compatible capable to switch on device mode
  238. * @flags: compatible specific SPI controller flags used at registration time
  239. * @prevent_dma_burst: boolean to indicate to prevent DMA burst
  240. */
  241. struct stm32_spi_cfg {
  242. const struct stm32_spi_regspec *regs;
  243. int (*get_fifo_size)(struct stm32_spi *spi);
  244. int (*get_bpw_mask)(struct stm32_spi *spi);
  245. void (*disable)(struct stm32_spi *spi);
  246. int (*config)(struct stm32_spi *spi);
  247. void (*set_bpw)(struct stm32_spi *spi);
  248. int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
  249. void (*set_data_idleness)(struct stm32_spi *spi, u32 length);
  250. int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
  251. void (*write_tx)(struct stm32_spi *spi);
  252. void (*read_rx)(struct stm32_spi *spi);
  253. void (*transfer_one_dma_start)(struct stm32_spi *spi);
  254. void (*dma_rx_cb)(void *data);
  255. void (*dma_tx_cb)(void *data);
  256. int (*transfer_one_irq)(struct stm32_spi *spi);
  257. irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
  258. irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
  259. unsigned int baud_rate_div_min;
  260. unsigned int baud_rate_div_max;
  261. bool has_fifo;
  262. bool has_device_mode;
  263. u16 flags;
  264. bool prevent_dma_burst;
  265. };
  266. /**
  267. * struct stm32_spi - private data of the SPI controller
  268. * @dev: driver model representation of the controller
  269. * @ctrl: controller interface
  270. * @cfg: compatible configuration data
  271. * @base: virtual memory area
  272. * @clk: hw kernel clock feeding the SPI clock generator
  273. * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
  274. * @lock: prevent I/O concurrent access
  275. * @irq: SPI controller interrupt line
  276. * @fifo_size: size of the embedded fifo in bytes
  277. * @t_size_max: maximum number of data of one transfer
  278. * @feature_set: SPI full or limited feature set
  279. * @cur_midi: host inter-data idleness in ns
  280. * @cur_speed: speed configured in Hz
  281. * @cur_half_period: time of a half bit in us
  282. * @cur_bpw: number of bits in a single SPI data frame
  283. * @cur_fthlv: fifo threshold level (data frames in a single data packet)
  284. * @cur_comm: SPI communication mode
  285. * @cur_xferlen: current transfer length in bytes
  286. * @cur_usedma: boolean to know if dma is used in current transfer
  287. * @tx_buf: data to be written, or NULL
  288. * @rx_buf: data to be read, or NULL
  289. * @tx_len: number of data to be written in bytes
  290. * @rx_len: number of data to be read in bytes
  291. * @dma_tx: dma channel for TX transfer
  292. * @dma_rx: dma channel for RX transfer
  293. * @phys_addr: SPI registers physical base address
  294. * @device_mode: the controller is configured as SPI device
  295. */
  296. struct stm32_spi {
  297. struct device *dev;
  298. struct spi_controller *ctrl;
  299. const struct stm32_spi_cfg *cfg;
  300. void __iomem *base;
  301. struct clk *clk;
  302. u32 clk_rate;
  303. spinlock_t lock; /* prevent I/O concurrent access */
  304. int irq;
  305. unsigned int fifo_size;
  306. unsigned int t_size_max;
  307. unsigned int feature_set;
  308. #define STM32_SPI_FEATURE_LIMITED STM32MP25_SPI_HWCFGR1_FULLCFG_LIMITED /* 0x0 */
  309. #define STM32_SPI_FEATURE_FULL STM32MP25_SPI_HWCFGR1_FULLCFG_FULL /* 0x1 */
  310. unsigned int cur_midi;
  311. unsigned int cur_speed;
  312. unsigned int cur_half_period;
  313. unsigned int cur_bpw;
  314. unsigned int cur_fthlv;
  315. unsigned int cur_comm;
  316. unsigned int cur_xferlen;
  317. bool cur_usedma;
  318. const void *tx_buf;
  319. void *rx_buf;
  320. int tx_len;
  321. int rx_len;
  322. struct dma_chan *dma_tx;
  323. struct dma_chan *dma_rx;
  324. dma_addr_t phys_addr;
  325. bool device_mode;
  326. };
  327. static const struct stm32_spi_regspec stm32fx_spi_regspec = {
  328. .en = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_SPE },
  329. .dma_rx_en = { STM32FX_SPI_CR2, STM32FX_SPI_CR2_RXDMAEN },
  330. .dma_tx_en = { STM32FX_SPI_CR2, STM32FX_SPI_CR2_TXDMAEN },
  331. .cpol = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_CPOL },
  332. .cpha = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_CPHA },
  333. .lsb_first = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_LSBFRST },
  334. .cs_high = {},
  335. .br = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_BR, STM32FX_SPI_CR1_BR_SHIFT },
  336. .rx = { STM32FX_SPI_DR },
  337. .tx = { STM32FX_SPI_DR },
  338. };
  339. static const struct stm32_spi_regspec stm32h7_spi_regspec = {
  340. /* SPI data transfer is enabled but spi_ker_ck is idle.
  341. * CFG1 and CFG2 registers are write protected when SPE is enabled.
  342. */
  343. .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
  344. .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
  345. .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
  346. .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
  347. .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
  348. .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
  349. .cs_high = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_SSIOP },
  350. .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
  351. STM32H7_SPI_CFG1_MBR_SHIFT },
  352. .rx = { STM32H7_SPI_RXDR },
  353. .tx = { STM32H7_SPI_TXDR },
  354. };
  355. static const struct stm32_spi_regspec stm32mp25_spi_regspec = {
  356. /* SPI data transfer is enabled but spi_ker_ck is idle.
  357. * CFG1 and CFG2 registers are write protected when SPE is enabled.
  358. */
  359. .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
  360. .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
  361. .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
  362. .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
  363. .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
  364. .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
  365. .cs_high = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_SSIOP },
  366. .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
  367. STM32H7_SPI_CFG1_MBR_SHIFT },
  368. .rx = { STM32H7_SPI_RXDR },
  369. .tx = { STM32H7_SPI_TXDR },
  370. .fullcfg = { STM32MP25_SPI_HWCFGR1, STM32MP25_SPI_HWCFGR1_FULLCFG },
  371. };
  372. static inline void stm32_spi_set_bits(struct stm32_spi *spi,
  373. u32 offset, u32 bits)
  374. {
  375. writel_relaxed(readl_relaxed(spi->base + offset) | bits,
  376. spi->base + offset);
  377. }
  378. static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
  379. u32 offset, u32 bits)
  380. {
  381. writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
  382. spi->base + offset);
  383. }
  384. /**
  385. * stm32h7_spi_get_fifo_size - Return fifo size
  386. * @spi: pointer to the spi controller data structure
  387. */
  388. static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
  389. {
  390. unsigned long flags;
  391. u32 count = 0;
  392. spin_lock_irqsave(&spi->lock, flags);
  393. stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
  394. while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
  395. writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
  396. stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
  397. spin_unlock_irqrestore(&spi->lock, flags);
  398. dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
  399. return count;
  400. }
  401. /**
  402. * stm32f4_spi_get_bpw_mask - Return bits per word mask
  403. * @spi: pointer to the spi controller data structure
  404. */
  405. static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
  406. {
  407. dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
  408. return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  409. }
  410. /**
  411. * stm32f7_spi_get_bpw_mask - Return bits per word mask
  412. * @spi: pointer to the spi controller data structure
  413. */
  414. static int stm32f7_spi_get_bpw_mask(struct stm32_spi *spi)
  415. {
  416. dev_dbg(spi->dev, "16-bit maximum data frame\n");
  417. return SPI_BPW_RANGE_MASK(4, 16);
  418. }
  419. /**
  420. * stm32h7_spi_get_bpw_mask - Return bits per word mask
  421. * @spi: pointer to the spi controller data structure
  422. */
  423. static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
  424. {
  425. unsigned long flags;
  426. u32 cfg1, max_bpw;
  427. spin_lock_irqsave(&spi->lock, flags);
  428. /*
  429. * The most significant bit at DSIZE bit field is reserved when the
  430. * maximum data size of periperal instances is limited to 16-bit
  431. */
  432. stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
  433. cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
  434. max_bpw = FIELD_GET(STM32H7_SPI_CFG1_DSIZE, cfg1) + 1;
  435. spin_unlock_irqrestore(&spi->lock, flags);
  436. dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
  437. return SPI_BPW_RANGE_MASK(4, max_bpw);
  438. }
  439. /**
  440. * stm32mp25_spi_get_bpw_mask - Return bits per word mask
  441. * @spi: pointer to the spi controller data structure
  442. */
  443. static int stm32mp25_spi_get_bpw_mask(struct stm32_spi *spi)
  444. {
  445. u32 dscfg, max_bpw;
  446. if (spi->feature_set == STM32_SPI_FEATURE_LIMITED) {
  447. dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
  448. return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  449. }
  450. dscfg = FIELD_GET(STM32MP25_SPI_HWCFGR1_DSCFG,
  451. readl_relaxed(spi->base + STM32MP25_SPI_HWCFGR1));
  452. max_bpw = 16;
  453. if (dscfg == STM32MP25_SPI_HWCFGR1_DSCFG_32_B)
  454. max_bpw = 32;
  455. dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
  456. return SPI_BPW_RANGE_MASK(4, max_bpw);
  457. }
  458. /**
  459. * stm32_spi_prepare_mbr - Determine baud rate divisor value
  460. * @spi: pointer to the spi controller data structure
  461. * @speed_hz: requested speed
  462. * @min_div: minimum baud rate divisor
  463. * @max_div: maximum baud rate divisor
  464. *
  465. * Return baud rate divisor value in case of success or -EINVAL
  466. */
  467. static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
  468. u32 min_div, u32 max_div)
  469. {
  470. u32 div, mbrdiv;
  471. /* Ensure spi->clk_rate is even */
  472. div = DIV_ROUND_CLOSEST(spi->clk_rate & ~0x1, speed_hz);
  473. /*
  474. * SPI framework set xfer->speed_hz to ctrl->max_speed_hz if
  475. * xfer->speed_hz is greater than ctrl->max_speed_hz, and it returns
  476. * an error when xfer->speed_hz is lower than ctrl->min_speed_hz, so
  477. * no need to check it there.
  478. * However, we need to ensure the following calculations.
  479. */
  480. if ((div < min_div) || (div > max_div))
  481. return -EINVAL;
  482. /* Determine the first power of 2 greater than or equal to div */
  483. if (div & (div - 1))
  484. mbrdiv = fls(div);
  485. else
  486. mbrdiv = fls(div) - 1;
  487. spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
  488. spi->cur_half_period = DIV_ROUND_CLOSEST(USEC_PER_SEC, 2 * spi->cur_speed);
  489. return mbrdiv - 1;
  490. }
  491. /**
  492. * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
  493. * @spi: pointer to the spi controller data structure
  494. * @xfer_len: length of the message to be transferred
  495. */
  496. static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
  497. {
  498. u32 packet, bpw;
  499. /* data packet should not exceed 1/2 of fifo space */
  500. packet = clamp(xfer_len, 1U, spi->fifo_size / 2);
  501. /* align packet size with data registers access */
  502. bpw = DIV_ROUND_UP(spi->cur_bpw, 8);
  503. return DIV_ROUND_UP(packet, bpw);
  504. }
  505. /**
  506. * stm32f4_spi_write_tx - Write bytes to Transmit Data Register
  507. * @spi: pointer to the spi controller data structure
  508. *
  509. * Read from tx_buf depends on remaining bytes to avoid to read beyond
  510. * tx_buf end.
  511. */
  512. static void stm32f4_spi_write_tx(struct stm32_spi *spi)
  513. {
  514. if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32FX_SPI_SR) &
  515. STM32FX_SPI_SR_TXE)) {
  516. u32 offs = spi->cur_xferlen - spi->tx_len;
  517. if (spi->cur_bpw == 16) {
  518. const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
  519. writew_relaxed(*tx_buf16, spi->base + STM32FX_SPI_DR);
  520. spi->tx_len -= sizeof(u16);
  521. } else {
  522. const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
  523. writeb_relaxed(*tx_buf8, spi->base + STM32FX_SPI_DR);
  524. spi->tx_len -= sizeof(u8);
  525. }
  526. }
  527. dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
  528. }
  529. /**
  530. * stm32f7_spi_write_tx - Write bytes to Transmit Data Register
  531. * @spi: pointer to the spi controller data structure
  532. *
  533. * Read from tx_buf depends on remaining bytes to avoid to read beyond
  534. * tx_buf end.
  535. */
  536. static void stm32f7_spi_write_tx(struct stm32_spi *spi)
  537. {
  538. if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32FX_SPI_SR) &
  539. STM32FX_SPI_SR_TXE)) {
  540. u32 offs = spi->cur_xferlen - spi->tx_len;
  541. if (spi->tx_len >= sizeof(u16)) {
  542. const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
  543. writew_relaxed(*tx_buf16, spi->base + STM32FX_SPI_DR);
  544. spi->tx_len -= sizeof(u16);
  545. } else {
  546. const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
  547. writeb_relaxed(*tx_buf8, spi->base + STM32FX_SPI_DR);
  548. spi->tx_len -= sizeof(u8);
  549. }
  550. }
  551. dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
  552. }
  553. /**
  554. * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register
  555. * @spi: pointer to the spi controller data structure
  556. *
  557. * Read from tx_buf depends on remaining bytes to avoid to read beyond
  558. * tx_buf end.
  559. */
  560. static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
  561. {
  562. while ((spi->tx_len > 0) &&
  563. (readl_relaxed(spi->base + STM32H7_SPI_SR) &
  564. STM32H7_SPI_SR_TXP)) {
  565. u32 offs = spi->cur_xferlen - spi->tx_len;
  566. if (spi->tx_len >= sizeof(u32)) {
  567. const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
  568. writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
  569. spi->tx_len -= sizeof(u32);
  570. } else if (spi->tx_len >= sizeof(u16)) {
  571. const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
  572. writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
  573. spi->tx_len -= sizeof(u16);
  574. } else {
  575. const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
  576. writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
  577. spi->tx_len -= sizeof(u8);
  578. }
  579. }
  580. dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
  581. }
  582. /**
  583. * stm32f4_spi_read_rx - Read bytes from Receive Data Register
  584. * @spi: pointer to the spi controller data structure
  585. *
  586. * Write in rx_buf depends on remaining bytes to avoid to write beyond
  587. * rx_buf end.
  588. */
  589. static void stm32f4_spi_read_rx(struct stm32_spi *spi)
  590. {
  591. if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32FX_SPI_SR) &
  592. STM32FX_SPI_SR_RXNE)) {
  593. u32 offs = spi->cur_xferlen - spi->rx_len;
  594. if (spi->cur_bpw == 16) {
  595. u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
  596. *rx_buf16 = readw_relaxed(spi->base + STM32FX_SPI_DR);
  597. spi->rx_len -= sizeof(u16);
  598. } else {
  599. u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
  600. *rx_buf8 = readb_relaxed(spi->base + STM32FX_SPI_DR);
  601. spi->rx_len -= sizeof(u8);
  602. }
  603. }
  604. dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
  605. }
  606. /**
  607. * stm32f7_spi_read_rx - Read bytes from Receive Data Register
  608. * @spi: pointer to the spi controller data structure
  609. *
  610. * Write in rx_buf depends on remaining bytes to avoid to write beyond
  611. * rx_buf end.
  612. */
  613. static void stm32f7_spi_read_rx(struct stm32_spi *spi)
  614. {
  615. u32 sr = readl_relaxed(spi->base + STM32FX_SPI_SR);
  616. u32 frlvl = FIELD_GET(STM32F7_SPI_SR_FRLVL, sr);
  617. while ((spi->rx_len > 0) && (frlvl > 0)) {
  618. u32 offs = spi->cur_xferlen - spi->rx_len;
  619. if ((spi->rx_len >= sizeof(u16)) && (frlvl >= 2)) {
  620. u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
  621. *rx_buf16 = readw_relaxed(spi->base + STM32FX_SPI_DR);
  622. spi->rx_len -= sizeof(u16);
  623. } else {
  624. u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
  625. *rx_buf8 = readb_relaxed(spi->base + STM32FX_SPI_DR);
  626. spi->rx_len -= sizeof(u8);
  627. }
  628. sr = readl_relaxed(spi->base + STM32FX_SPI_SR);
  629. frlvl = FIELD_GET(STM32F7_SPI_SR_FRLVL, sr);
  630. }
  631. if (spi->rx_len >= sizeof(u16))
  632. stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
  633. else
  634. stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
  635. dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
  636. __func__, spi->rx_len, sr);
  637. }
  638. /**
  639. * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register
  640. * @spi: pointer to the spi controller data structure
  641. *
  642. * Write in rx_buf depends on remaining bytes to avoid to write beyond
  643. * rx_buf end.
  644. */
  645. static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi)
  646. {
  647. u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
  648. u32 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
  649. while ((spi->rx_len > 0) &&
  650. ((sr & STM32H7_SPI_SR_RXP) ||
  651. ((sr & STM32H7_SPI_SR_EOT) &&
  652. ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
  653. u32 offs = spi->cur_xferlen - spi->rx_len;
  654. if ((spi->rx_len >= sizeof(u32)) ||
  655. (sr & STM32H7_SPI_SR_RXWNE)) {
  656. u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
  657. *rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
  658. spi->rx_len -= sizeof(u32);
  659. } else if ((spi->rx_len >= sizeof(u16)) ||
  660. (!(sr & STM32H7_SPI_SR_RXWNE) &&
  661. (rxplvl >= 2 || spi->cur_bpw > 8))) {
  662. u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
  663. *rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
  664. spi->rx_len -= sizeof(u16);
  665. } else {
  666. u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
  667. *rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
  668. spi->rx_len -= sizeof(u8);
  669. }
  670. sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
  671. rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
  672. }
  673. dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
  674. __func__, spi->rx_len, sr);
  675. }
  676. /**
  677. * stm32_spi_enable - Enable SPI controller
  678. * @spi: pointer to the spi controller data structure
  679. */
  680. static void stm32_spi_enable(struct stm32_spi *spi)
  681. {
  682. dev_dbg(spi->dev, "enable controller\n");
  683. stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
  684. spi->cfg->regs->en.mask);
  685. }
  686. /**
  687. * stm32fx_spi_disable - Disable SPI controller
  688. * @spi: pointer to the spi controller data structure
  689. */
  690. static void stm32fx_spi_disable(struct stm32_spi *spi)
  691. {
  692. unsigned long flags;
  693. u32 sr;
  694. dev_dbg(spi->dev, "disable controller\n");
  695. spin_lock_irqsave(&spi->lock, flags);
  696. if (!(readl_relaxed(spi->base + STM32FX_SPI_CR1) &
  697. STM32FX_SPI_CR1_SPE)) {
  698. spin_unlock_irqrestore(&spi->lock, flags);
  699. return;
  700. }
  701. /* Disable interrupts */
  702. stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_TXEIE |
  703. STM32FX_SPI_CR2_RXNEIE |
  704. STM32FX_SPI_CR2_ERRIE);
  705. /* Wait until BSY = 0 */
  706. if (readl_relaxed_poll_timeout_atomic(spi->base + STM32FX_SPI_SR,
  707. sr, !(sr & STM32FX_SPI_SR_BSY),
  708. 10, 100000) < 0) {
  709. dev_warn(spi->dev, "disabling condition timeout\n");
  710. }
  711. if (spi->cur_usedma && spi->dma_tx)
  712. dmaengine_terminate_async(spi->dma_tx);
  713. if (spi->cur_usedma && spi->dma_rx)
  714. dmaengine_terminate_async(spi->dma_rx);
  715. stm32_spi_clr_bits(spi, STM32FX_SPI_CR1, STM32FX_SPI_CR1_SPE);
  716. stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_TXDMAEN |
  717. STM32FX_SPI_CR2_RXDMAEN);
  718. /* Sequence to clear OVR flag */
  719. readl_relaxed(spi->base + STM32FX_SPI_DR);
  720. readl_relaxed(spi->base + STM32FX_SPI_SR);
  721. spin_unlock_irqrestore(&spi->lock, flags);
  722. }
  723. /**
  724. * stm32h7_spi_disable - Disable SPI controller
  725. * @spi: pointer to the spi controller data structure
  726. *
  727. * RX-Fifo is flushed when SPI controller is disabled.
  728. */
  729. static void stm32h7_spi_disable(struct stm32_spi *spi)
  730. {
  731. unsigned long flags;
  732. u32 cr1;
  733. dev_dbg(spi->dev, "disable controller\n");
  734. spin_lock_irqsave(&spi->lock, flags);
  735. cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
  736. if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
  737. spin_unlock_irqrestore(&spi->lock, flags);
  738. return;
  739. }
  740. /* Add a delay to make sure that transmission is ended. */
  741. if (spi->cur_half_period)
  742. udelay(spi->cur_half_period);
  743. if (spi->cur_usedma && spi->dma_tx)
  744. dmaengine_terminate_async(spi->dma_tx);
  745. if (spi->cur_usedma && spi->dma_rx)
  746. dmaengine_terminate_async(spi->dma_rx);
  747. stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
  748. stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
  749. STM32H7_SPI_CFG1_RXDMAEN);
  750. /* Disable interrupts and clear status flags */
  751. writel_relaxed(0, spi->base + STM32H7_SPI_IER);
  752. writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
  753. spin_unlock_irqrestore(&spi->lock, flags);
  754. }
  755. /**
  756. * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
  757. * @ctrl: controller interface
  758. * @spi_dev: pointer to the spi device
  759. * @transfer: pointer to spi transfer
  760. *
  761. * If driver has fifo and the current transfer size is greater than fifo size,
  762. * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
  763. */
  764. static bool stm32_spi_can_dma(struct spi_controller *ctrl,
  765. struct spi_device *spi_dev,
  766. struct spi_transfer *transfer)
  767. {
  768. unsigned int dma_size;
  769. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  770. if (spi->cfg->has_fifo)
  771. dma_size = spi->fifo_size;
  772. else
  773. dma_size = SPI_DMA_MIN_BYTES;
  774. dev_dbg(spi->dev, "%s: %s\n", __func__,
  775. (transfer->len > dma_size) ? "true" : "false");
  776. return (transfer->len > dma_size);
  777. }
  778. /**
  779. * stm32fx_spi_irq_event - Interrupt handler for SPI controller events
  780. * @irq: interrupt line
  781. * @dev_id: SPI controller ctrl interface
  782. */
  783. static irqreturn_t stm32fx_spi_irq_event(int irq, void *dev_id)
  784. {
  785. struct spi_controller *ctrl = dev_id;
  786. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  787. u32 sr, mask = 0;
  788. bool end = false;
  789. spin_lock(&spi->lock);
  790. sr = readl_relaxed(spi->base + STM32FX_SPI_SR);
  791. /*
  792. * BSY flag is not handled in interrupt but it is normal behavior when
  793. * this flag is set.
  794. */
  795. sr &= ~STM32FX_SPI_SR_BSY;
  796. if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
  797. spi->cur_comm == SPI_3WIRE_TX)) {
  798. /* OVR flag shouldn't be handled for TX only mode */
  799. sr &= ~(STM32FX_SPI_SR_OVR | STM32FX_SPI_SR_RXNE);
  800. mask |= STM32FX_SPI_SR_TXE;
  801. }
  802. if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
  803. spi->cur_comm == SPI_SIMPLEX_RX ||
  804. spi->cur_comm == SPI_3WIRE_RX)) {
  805. /* TXE flag is set and is handled when RXNE flag occurs */
  806. sr &= ~STM32FX_SPI_SR_TXE;
  807. mask |= STM32FX_SPI_SR_RXNE | STM32FX_SPI_SR_OVR;
  808. }
  809. if (!(sr & mask)) {
  810. dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
  811. spin_unlock(&spi->lock);
  812. return IRQ_NONE;
  813. }
  814. if (sr & STM32FX_SPI_SR_OVR) {
  815. dev_warn(spi->dev, "Overrun: received value discarded\n");
  816. /* Sequence to clear OVR flag */
  817. readl_relaxed(spi->base + STM32FX_SPI_DR);
  818. readl_relaxed(spi->base + STM32FX_SPI_SR);
  819. /*
  820. * If overrun is detected, it means that something went wrong,
  821. * so stop the current transfer. Transfer can wait for next
  822. * RXNE but DR is already read and end never happens.
  823. */
  824. end = true;
  825. goto end_irq;
  826. }
  827. if (sr & STM32FX_SPI_SR_TXE) {
  828. if (spi->tx_buf)
  829. spi->cfg->write_tx(spi);
  830. if (spi->tx_len == 0)
  831. end = true;
  832. }
  833. if (sr & STM32FX_SPI_SR_RXNE) {
  834. spi->cfg->read_rx(spi);
  835. if (spi->rx_len == 0)
  836. end = true;
  837. else if (spi->tx_buf)/* Load data for discontinuous mode */
  838. spi->cfg->write_tx(spi);
  839. }
  840. end_irq:
  841. if (end) {
  842. /* Immediately disable interrupts to do not generate new one */
  843. stm32_spi_clr_bits(spi, STM32FX_SPI_CR2,
  844. STM32FX_SPI_CR2_TXEIE |
  845. STM32FX_SPI_CR2_RXNEIE |
  846. STM32FX_SPI_CR2_ERRIE);
  847. spin_unlock(&spi->lock);
  848. return IRQ_WAKE_THREAD;
  849. }
  850. spin_unlock(&spi->lock);
  851. return IRQ_HANDLED;
  852. }
  853. /**
  854. * stm32fx_spi_irq_thread - Thread of interrupt handler for SPI controller
  855. * @irq: interrupt line
  856. * @dev_id: SPI controller interface
  857. */
  858. static irqreturn_t stm32fx_spi_irq_thread(int irq, void *dev_id)
  859. {
  860. struct spi_controller *ctrl = dev_id;
  861. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  862. spi_finalize_current_transfer(ctrl);
  863. stm32fx_spi_disable(spi);
  864. return IRQ_HANDLED;
  865. }
  866. /**
  867. * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller
  868. * @irq: interrupt line
  869. * @dev_id: SPI controller interface
  870. */
  871. static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
  872. {
  873. struct spi_controller *ctrl = dev_id;
  874. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  875. u32 sr, ier, mask;
  876. unsigned long flags;
  877. bool end = false;
  878. spin_lock_irqsave(&spi->lock, flags);
  879. sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
  880. ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
  881. mask = ier;
  882. /*
  883. * EOTIE enables irq from EOT, SUSP and TXC events. We need to set
  884. * SUSP to acknowledge it later. TXC is automatically cleared
  885. */
  886. mask |= STM32H7_SPI_SR_SUSP;
  887. /*
  888. * DXPIE is set in Full-Duplex, one IT will be raised if TXP and RXP
  889. * are set. So in case of Full-Duplex, need to poll TXP and RXP event.
  890. */
  891. if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
  892. mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
  893. if (!(sr & mask)) {
  894. dev_vdbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
  895. sr, ier);
  896. spin_unlock_irqrestore(&spi->lock, flags);
  897. return IRQ_NONE;
  898. }
  899. if (sr & STM32H7_SPI_SR_SUSP) {
  900. static DEFINE_RATELIMIT_STATE(rs,
  901. DEFAULT_RATELIMIT_INTERVAL * 10,
  902. 1);
  903. ratelimit_set_flags(&rs, RATELIMIT_MSG_ON_RELEASE);
  904. if (__ratelimit(&rs))
  905. dev_dbg_ratelimited(spi->dev, "Communication suspended\n");
  906. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  907. stm32h7_spi_read_rxfifo(spi);
  908. /*
  909. * If communication is suspended while using DMA, it means
  910. * that something went wrong, so stop the current transfer
  911. */
  912. if (spi->cur_usedma)
  913. end = true;
  914. }
  915. if (sr & STM32H7_SPI_SR_MODF) {
  916. dev_warn(spi->dev, "Mode fault: transfer aborted\n");
  917. end = true;
  918. }
  919. if (sr & STM32H7_SPI_SR_OVR) {
  920. dev_err(spi->dev, "Overrun: RX data lost\n");
  921. end = true;
  922. }
  923. if (sr & STM32H7_SPI_SR_EOT) {
  924. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  925. stm32h7_spi_read_rxfifo(spi);
  926. if (!spi->cur_usedma ||
  927. (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX))
  928. end = true;
  929. }
  930. if (sr & STM32H7_SPI_SR_TXP)
  931. if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
  932. stm32h7_spi_write_txfifo(spi);
  933. if (sr & STM32H7_SPI_SR_RXP)
  934. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  935. stm32h7_spi_read_rxfifo(spi);
  936. writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR);
  937. spin_unlock_irqrestore(&spi->lock, flags);
  938. if (end) {
  939. stm32h7_spi_disable(spi);
  940. spi_finalize_current_transfer(ctrl);
  941. }
  942. return IRQ_HANDLED;
  943. }
  944. static int stm32_spi_optimize_message(struct spi_message *msg)
  945. {
  946. struct spi_controller *ctrl = msg->spi->controller;
  947. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  948. /* On STM32H7, messages should not exceed a maximum size set
  949. * later via the set_number_of_data function. In order to
  950. * ensure that, split large messages into several messages
  951. */
  952. if (spi->cfg->set_number_of_data)
  953. return spi_split_transfers_maxwords(ctrl, msg, spi->t_size_max);
  954. return 0;
  955. }
  956. /**
  957. * stm32_spi_prepare_msg - set up the controller to transfer a single message
  958. * @ctrl: controller interface
  959. * @msg: pointer to spi message
  960. */
  961. static int stm32_spi_prepare_msg(struct spi_controller *ctrl,
  962. struct spi_message *msg)
  963. {
  964. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  965. struct spi_device *spi_dev = msg->spi;
  966. struct device_node *np = spi_dev->dev.of_node;
  967. unsigned long flags;
  968. u32 clrb = 0, setb = 0;
  969. /* SPI target device may need time between data frames */
  970. spi->cur_midi = 0;
  971. if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
  972. dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
  973. if (spi_dev->mode & SPI_CPOL)
  974. setb |= spi->cfg->regs->cpol.mask;
  975. else
  976. clrb |= spi->cfg->regs->cpol.mask;
  977. if (spi_dev->mode & SPI_CPHA)
  978. setb |= spi->cfg->regs->cpha.mask;
  979. else
  980. clrb |= spi->cfg->regs->cpha.mask;
  981. if (spi_dev->mode & SPI_LSB_FIRST)
  982. setb |= spi->cfg->regs->lsb_first.mask;
  983. else
  984. clrb |= spi->cfg->regs->lsb_first.mask;
  985. if (STM32_SPI_DEVICE_MODE(spi) && spi_dev->mode & SPI_CS_HIGH)
  986. setb |= spi->cfg->regs->cs_high.mask;
  987. else
  988. clrb |= spi->cfg->regs->cs_high.mask;
  989. dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
  990. !!(spi_dev->mode & SPI_CPOL),
  991. !!(spi_dev->mode & SPI_CPHA),
  992. !!(spi_dev->mode & SPI_LSB_FIRST),
  993. !!(spi_dev->mode & SPI_CS_HIGH));
  994. spin_lock_irqsave(&spi->lock, flags);
  995. /* CPOL, CPHA and LSB FIRST bits have common register */
  996. if (clrb || setb)
  997. writel_relaxed(
  998. (readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
  999. ~clrb) | setb,
  1000. spi->base + spi->cfg->regs->cpol.reg);
  1001. spin_unlock_irqrestore(&spi->lock, flags);
  1002. return 0;
  1003. }
  1004. /**
  1005. * stm32fx_spi_dma_tx_cb - dma callback
  1006. * @data: pointer to the spi controller data structure
  1007. *
  1008. * DMA callback is called when the transfer is complete for DMA TX channel.
  1009. */
  1010. static void stm32fx_spi_dma_tx_cb(void *data)
  1011. {
  1012. struct stm32_spi *spi = data;
  1013. if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
  1014. spi_finalize_current_transfer(spi->ctrl);
  1015. stm32fx_spi_disable(spi);
  1016. }
  1017. }
  1018. /**
  1019. * stm32_spi_dma_rx_cb - dma callback
  1020. * @data: pointer to the spi controller data structure
  1021. *
  1022. * DMA callback is called when the transfer is complete for DMA RX channel.
  1023. */
  1024. static void stm32_spi_dma_rx_cb(void *data)
  1025. {
  1026. struct stm32_spi *spi = data;
  1027. spi_finalize_current_transfer(spi->ctrl);
  1028. spi->cfg->disable(spi);
  1029. }
  1030. /**
  1031. * stm32_spi_dma_config - configure dma slave channel depending on current
  1032. * transfer bits_per_word.
  1033. * @spi: pointer to the spi controller data structure
  1034. * @dma_chan: pointer to the DMA channel
  1035. * @dma_conf: pointer to the dma_slave_config structure
  1036. * @dir: direction of the dma transfer
  1037. */
  1038. static void stm32_spi_dma_config(struct stm32_spi *spi,
  1039. struct dma_chan *dma_chan,
  1040. struct dma_slave_config *dma_conf,
  1041. enum dma_transfer_direction dir)
  1042. {
  1043. enum dma_slave_buswidth buswidth;
  1044. struct dma_slave_caps caps;
  1045. u32 maxburst = 1;
  1046. int ret;
  1047. if (spi->cur_bpw <= 8)
  1048. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1049. else if (spi->cur_bpw <= 16)
  1050. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  1051. else
  1052. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1053. /* Valid for DMA Half or Full Fifo threshold */
  1054. if (!spi->cfg->prevent_dma_burst && spi->cfg->has_fifo && spi->cur_fthlv != 2)
  1055. maxburst = spi->cur_fthlv;
  1056. /* Get the DMA channel caps, and adjust maxburst if possible */
  1057. ret = dma_get_slave_caps(dma_chan, &caps);
  1058. if (!ret)
  1059. maxburst = min(maxburst, caps.max_burst);
  1060. memset(dma_conf, 0, sizeof(struct dma_slave_config));
  1061. dma_conf->direction = dir;
  1062. if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
  1063. dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
  1064. dma_conf->src_addr_width = buswidth;
  1065. dma_conf->src_maxburst = maxburst;
  1066. dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
  1067. buswidth, maxburst);
  1068. } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
  1069. dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
  1070. dma_conf->dst_addr_width = buswidth;
  1071. dma_conf->dst_maxburst = maxburst;
  1072. dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
  1073. buswidth, maxburst);
  1074. }
  1075. }
  1076. /**
  1077. * stm32fx_spi_transfer_one_irq - transfer a single spi_transfer using
  1078. * interrupts
  1079. * @spi: pointer to the spi controller data structure
  1080. *
  1081. * It must returns 0 if the transfer is finished or 1 if the transfer is still
  1082. * in progress.
  1083. */
  1084. static int stm32fx_spi_transfer_one_irq(struct stm32_spi *spi)
  1085. {
  1086. unsigned long flags;
  1087. u32 cr2 = 0;
  1088. /* Enable the interrupts relative to the current communication mode */
  1089. if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
  1090. cr2 |= STM32FX_SPI_CR2_TXEIE;
  1091. } else if (spi->cur_comm == SPI_FULL_DUPLEX ||
  1092. spi->cur_comm == SPI_SIMPLEX_RX ||
  1093. spi->cur_comm == SPI_3WIRE_RX) {
  1094. /* In transmit-only mode, the OVR flag is set in the SR register
  1095. * since the received data are never read. Therefore set OVR
  1096. * interrupt only when rx buffer is available.
  1097. */
  1098. cr2 |= STM32FX_SPI_CR2_RXNEIE | STM32FX_SPI_CR2_ERRIE;
  1099. } else {
  1100. return -EINVAL;
  1101. }
  1102. spin_lock_irqsave(&spi->lock, flags);
  1103. stm32_spi_set_bits(spi, STM32FX_SPI_CR2, cr2);
  1104. stm32_spi_enable(spi);
  1105. /* starting data transfer when buffer is loaded */
  1106. if (spi->tx_buf)
  1107. spi->cfg->write_tx(spi);
  1108. spin_unlock_irqrestore(&spi->lock, flags);
  1109. return 1;
  1110. }
  1111. /**
  1112. * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using
  1113. * interrupts
  1114. * @spi: pointer to the spi controller data structure
  1115. *
  1116. * It must returns 0 if the transfer is finished or 1 if the transfer is still
  1117. * in progress.
  1118. */
  1119. static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
  1120. {
  1121. unsigned long flags;
  1122. u32 ier = 0;
  1123. /* Enable the interrupts relative to the current communication mode */
  1124. if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
  1125. ier |= STM32H7_SPI_IER_DXPIE;
  1126. else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
  1127. ier |= STM32H7_SPI_IER_TXPIE;
  1128. else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
  1129. ier |= STM32H7_SPI_IER_RXPIE;
  1130. /* Enable the interrupts relative to the end of transfer */
  1131. ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
  1132. STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
  1133. spin_lock_irqsave(&spi->lock, flags);
  1134. stm32_spi_enable(spi);
  1135. /* Be sure to have data in fifo before starting data transfer */
  1136. if (spi->tx_buf)
  1137. stm32h7_spi_write_txfifo(spi);
  1138. if (STM32_SPI_HOST_MODE(spi))
  1139. stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
  1140. writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
  1141. spin_unlock_irqrestore(&spi->lock, flags);
  1142. return 1;
  1143. }
  1144. /**
  1145. * stm32fx_spi_transfer_one_dma_start - Set SPI driver registers to start
  1146. * transfer using DMA
  1147. * @spi: pointer to the spi controller data structure
  1148. */
  1149. static void stm32fx_spi_transfer_one_dma_start(struct stm32_spi *spi)
  1150. {
  1151. /* In DMA mode end of transfer is handled by DMA TX or RX callback. */
  1152. if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
  1153. spi->cur_comm == SPI_FULL_DUPLEX) {
  1154. /*
  1155. * In transmit-only mode, the OVR flag is set in the SR register
  1156. * since the received data are never read. Therefore set OVR
  1157. * interrupt only when rx buffer is available.
  1158. */
  1159. stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_ERRIE);
  1160. }
  1161. stm32_spi_enable(spi);
  1162. }
  1163. /**
  1164. * stm32f7_spi_transfer_one_dma_start - Set SPI driver registers to start
  1165. * transfer using DMA
  1166. * @spi: pointer to the spi controller data structure
  1167. */
  1168. static void stm32f7_spi_transfer_one_dma_start(struct stm32_spi *spi)
  1169. {
  1170. /* Configure DMA request trigger threshold according to DMA width */
  1171. if (spi->cur_bpw <= 8)
  1172. stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
  1173. else
  1174. stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
  1175. stm32fx_spi_transfer_one_dma_start(spi);
  1176. }
  1177. /**
  1178. * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start
  1179. * transfer using DMA
  1180. * @spi: pointer to the spi controller data structure
  1181. */
  1182. static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
  1183. {
  1184. uint32_t ier = STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
  1185. /* Enable the interrupts */
  1186. if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX)
  1187. ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE;
  1188. stm32_spi_set_bits(spi, STM32H7_SPI_IER, ier);
  1189. stm32_spi_enable(spi);
  1190. if (STM32_SPI_HOST_MODE(spi))
  1191. stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
  1192. }
  1193. /**
  1194. * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
  1195. * @spi: pointer to the spi controller data structure
  1196. * @xfer: pointer to the spi_transfer structure
  1197. *
  1198. * It must returns 0 if the transfer is finished or 1 if the transfer is still
  1199. * in progress.
  1200. */
  1201. static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
  1202. struct spi_transfer *xfer)
  1203. {
  1204. struct dma_slave_config tx_dma_conf, rx_dma_conf;
  1205. struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
  1206. unsigned long flags;
  1207. spin_lock_irqsave(&spi->lock, flags);
  1208. rx_dma_desc = NULL;
  1209. if (spi->rx_buf && spi->dma_rx) {
  1210. stm32_spi_dma_config(spi, spi->dma_rx, &rx_dma_conf, DMA_DEV_TO_MEM);
  1211. dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
  1212. /* Enable Rx DMA request */
  1213. stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
  1214. spi->cfg->regs->dma_rx_en.mask);
  1215. rx_dma_desc = dmaengine_prep_slave_sg(
  1216. spi->dma_rx, xfer->rx_sg.sgl,
  1217. xfer->rx_sg.nents,
  1218. rx_dma_conf.direction,
  1219. DMA_PREP_INTERRUPT);
  1220. }
  1221. tx_dma_desc = NULL;
  1222. if (spi->tx_buf && spi->dma_tx) {
  1223. stm32_spi_dma_config(spi, spi->dma_tx, &tx_dma_conf, DMA_MEM_TO_DEV);
  1224. dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
  1225. tx_dma_desc = dmaengine_prep_slave_sg(
  1226. spi->dma_tx, xfer->tx_sg.sgl,
  1227. xfer->tx_sg.nents,
  1228. tx_dma_conf.direction,
  1229. DMA_PREP_INTERRUPT);
  1230. }
  1231. if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
  1232. (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
  1233. goto dma_desc_error;
  1234. if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
  1235. goto dma_desc_error;
  1236. if (rx_dma_desc) {
  1237. rx_dma_desc->callback = spi->cfg->dma_rx_cb;
  1238. rx_dma_desc->callback_param = spi;
  1239. if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
  1240. dev_err(spi->dev, "Rx DMA submit failed\n");
  1241. goto dma_desc_error;
  1242. }
  1243. /* Enable Rx DMA channel */
  1244. dma_async_issue_pending(spi->dma_rx);
  1245. }
  1246. if (tx_dma_desc) {
  1247. if (spi->cur_comm == SPI_SIMPLEX_TX ||
  1248. spi->cur_comm == SPI_3WIRE_TX) {
  1249. tx_dma_desc->callback = spi->cfg->dma_tx_cb;
  1250. tx_dma_desc->callback_param = spi;
  1251. }
  1252. if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
  1253. dev_err(spi->dev, "Tx DMA submit failed\n");
  1254. goto dma_submit_error;
  1255. }
  1256. /* Enable Tx DMA channel */
  1257. dma_async_issue_pending(spi->dma_tx);
  1258. /* Enable Tx DMA request */
  1259. stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
  1260. spi->cfg->regs->dma_tx_en.mask);
  1261. }
  1262. spi->cfg->transfer_one_dma_start(spi);
  1263. spin_unlock_irqrestore(&spi->lock, flags);
  1264. return 1;
  1265. dma_submit_error:
  1266. if (spi->dma_rx)
  1267. dmaengine_terminate_sync(spi->dma_rx);
  1268. dma_desc_error:
  1269. stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
  1270. spi->cfg->regs->dma_rx_en.mask);
  1271. spin_unlock_irqrestore(&spi->lock, flags);
  1272. dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
  1273. spi->cur_usedma = false;
  1274. return spi->cfg->transfer_one_irq(spi);
  1275. }
  1276. /**
  1277. * stm32f4_spi_set_bpw - Configure bits per word
  1278. * @spi: pointer to the spi controller data structure
  1279. */
  1280. static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
  1281. {
  1282. if (spi->cur_bpw == 16)
  1283. stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32F4_SPI_CR1_DFF);
  1284. else
  1285. stm32_spi_clr_bits(spi, STM32FX_SPI_CR1, STM32F4_SPI_CR1_DFF);
  1286. }
  1287. /**
  1288. * stm32f7_spi_set_bpw - Configure bits per word
  1289. * @spi: pointer to the spi controller data structure
  1290. */
  1291. static void stm32f7_spi_set_bpw(struct stm32_spi *spi)
  1292. {
  1293. u32 bpw;
  1294. u32 cr2_clrb = 0, cr2_setb = 0;
  1295. bpw = spi->cur_bpw - 1;
  1296. cr2_clrb |= STM32F7_SPI_CR2_DS;
  1297. cr2_setb |= FIELD_PREP(STM32F7_SPI_CR2_DS, bpw);
  1298. if (spi->rx_len >= sizeof(u16))
  1299. cr2_clrb |= STM32F7_SPI_CR2_FRXTH;
  1300. else
  1301. cr2_setb |= STM32F7_SPI_CR2_FRXTH;
  1302. writel_relaxed(
  1303. (readl_relaxed(spi->base + STM32FX_SPI_CR2) &
  1304. ~cr2_clrb) | cr2_setb,
  1305. spi->base + STM32FX_SPI_CR2);
  1306. }
  1307. /**
  1308. * stm32h7_spi_set_bpw - configure bits per word
  1309. * @spi: pointer to the spi controller data structure
  1310. */
  1311. static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
  1312. {
  1313. u32 bpw, fthlv;
  1314. u32 cfg1_clrb = 0, cfg1_setb = 0;
  1315. bpw = spi->cur_bpw - 1;
  1316. cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
  1317. cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
  1318. spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
  1319. fthlv = spi->cur_fthlv - 1;
  1320. cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
  1321. cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
  1322. writel_relaxed(
  1323. (readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
  1324. ~cfg1_clrb) | cfg1_setb,
  1325. spi->base + STM32H7_SPI_CFG1);
  1326. }
  1327. /**
  1328. * stm32_spi_set_mbr - Configure baud rate divisor in host mode
  1329. * @spi: pointer to the spi controller data structure
  1330. * @mbrdiv: baud rate divisor value
  1331. */
  1332. static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
  1333. {
  1334. u32 clrb = 0, setb = 0;
  1335. clrb |= spi->cfg->regs->br.mask;
  1336. setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
  1337. writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
  1338. ~clrb) | setb,
  1339. spi->base + spi->cfg->regs->br.reg);
  1340. }
  1341. /**
  1342. * stm32_spi_communication_type - return transfer communication type
  1343. * @spi_dev: pointer to the spi device
  1344. * @transfer: pointer to spi transfer
  1345. */
  1346. static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
  1347. struct spi_transfer *transfer)
  1348. {
  1349. unsigned int type = SPI_FULL_DUPLEX;
  1350. if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
  1351. /*
  1352. * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
  1353. * is forbidden and unvalidated by SPI subsystem so depending
  1354. * on the valid buffer, we can determine the direction of the
  1355. * transfer.
  1356. */
  1357. if (!transfer->tx_buf)
  1358. type = SPI_3WIRE_RX;
  1359. else
  1360. type = SPI_3WIRE_TX;
  1361. } else {
  1362. if (!transfer->tx_buf)
  1363. type = SPI_SIMPLEX_RX;
  1364. else if (!transfer->rx_buf)
  1365. type = SPI_SIMPLEX_TX;
  1366. }
  1367. return type;
  1368. }
  1369. /**
  1370. * stm32fx_spi_set_mode - configure communication mode
  1371. * @spi: pointer to the spi controller data structure
  1372. * @comm_type: type of communication to configure
  1373. */
  1374. static int stm32fx_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
  1375. {
  1376. if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
  1377. stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
  1378. STM32FX_SPI_CR1_BIDIMODE |
  1379. STM32FX_SPI_CR1_BIDIOE);
  1380. } else if (comm_type == SPI_FULL_DUPLEX ||
  1381. comm_type == SPI_SIMPLEX_RX) {
  1382. stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
  1383. STM32FX_SPI_CR1_BIDIMODE |
  1384. STM32FX_SPI_CR1_BIDIOE);
  1385. } else if (comm_type == SPI_3WIRE_RX) {
  1386. stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
  1387. STM32FX_SPI_CR1_BIDIMODE);
  1388. stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
  1389. STM32FX_SPI_CR1_BIDIOE);
  1390. } else {
  1391. return -EINVAL;
  1392. }
  1393. return 0;
  1394. }
  1395. /**
  1396. * stm32h7_spi_set_mode - configure communication mode
  1397. * @spi: pointer to the spi controller data structure
  1398. * @comm_type: type of communication to configure
  1399. */
  1400. static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
  1401. {
  1402. u32 mode;
  1403. u32 cfg2_clrb = 0, cfg2_setb = 0;
  1404. if (comm_type == SPI_3WIRE_RX) {
  1405. mode = STM32H7_SPI_HALF_DUPLEX;
  1406. stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
  1407. } else if (comm_type == SPI_3WIRE_TX) {
  1408. mode = STM32H7_SPI_HALF_DUPLEX;
  1409. stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
  1410. } else if (comm_type == SPI_SIMPLEX_RX) {
  1411. mode = STM32H7_SPI_SIMPLEX_RX;
  1412. } else if (comm_type == SPI_SIMPLEX_TX) {
  1413. mode = STM32H7_SPI_SIMPLEX_TX;
  1414. } else {
  1415. mode = STM32H7_SPI_FULL_DUPLEX;
  1416. }
  1417. cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
  1418. cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
  1419. writel_relaxed(
  1420. (readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
  1421. ~cfg2_clrb) | cfg2_setb,
  1422. spi->base + STM32H7_SPI_CFG2);
  1423. return 0;
  1424. }
  1425. /**
  1426. * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
  1427. * consecutive data frames in host mode
  1428. * @spi: pointer to the spi controller data structure
  1429. * @len: transfer len
  1430. */
  1431. static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len)
  1432. {
  1433. u32 cfg2_clrb = 0, cfg2_setb = 0;
  1434. cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
  1435. if ((len > 1) && (spi->cur_midi > 0)) {
  1436. u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
  1437. u32 midi = min_t(u32,
  1438. DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
  1439. FIELD_GET(STM32H7_SPI_CFG2_MIDI,
  1440. STM32H7_SPI_CFG2_MIDI));
  1441. dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
  1442. sck_period_ns, midi, midi * sck_period_ns);
  1443. cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
  1444. }
  1445. writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
  1446. ~cfg2_clrb) | cfg2_setb,
  1447. spi->base + STM32H7_SPI_CFG2);
  1448. }
  1449. /**
  1450. * stm32h7_spi_number_of_data - configure number of data at current transfer
  1451. * @spi: pointer to the spi controller data structure
  1452. * @nb_words: transfer length (in words)
  1453. */
  1454. static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
  1455. {
  1456. if (nb_words <= spi->t_size_max) {
  1457. writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
  1458. spi->base + STM32H7_SPI_CR2);
  1459. } else {
  1460. return -EMSGSIZE;
  1461. }
  1462. return 0;
  1463. }
  1464. /**
  1465. * stm32_spi_transfer_one_setup - common setup to transfer a single
  1466. * spi_transfer either using DMA or
  1467. * interrupts.
  1468. * @spi: pointer to the spi controller data structure
  1469. * @spi_dev: pointer to the spi device
  1470. * @transfer: pointer to spi transfer
  1471. */
  1472. static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
  1473. struct spi_device *spi_dev,
  1474. struct spi_transfer *transfer)
  1475. {
  1476. unsigned long flags;
  1477. unsigned int comm_type;
  1478. int nb_words, ret = 0;
  1479. int mbr;
  1480. spin_lock_irqsave(&spi->lock, flags);
  1481. spi->cur_xferlen = transfer->len;
  1482. spi->cur_bpw = transfer->bits_per_word;
  1483. spi->cfg->set_bpw(spi);
  1484. /* Update spi->cur_speed with real clock speed */
  1485. if (STM32_SPI_HOST_MODE(spi)) {
  1486. mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
  1487. spi->cfg->baud_rate_div_min,
  1488. spi->cfg->baud_rate_div_max);
  1489. if (mbr < 0) {
  1490. ret = mbr;
  1491. goto out;
  1492. }
  1493. transfer->speed_hz = spi->cur_speed;
  1494. stm32_spi_set_mbr(spi, mbr);
  1495. }
  1496. comm_type = stm32_spi_communication_type(spi_dev, transfer);
  1497. ret = spi->cfg->set_mode(spi, comm_type);
  1498. if (ret < 0)
  1499. goto out;
  1500. spi->cur_comm = comm_type;
  1501. if (STM32_SPI_HOST_MODE(spi) && spi->cfg->set_data_idleness)
  1502. spi->cfg->set_data_idleness(spi, transfer->len);
  1503. if (spi->cur_bpw <= 8)
  1504. nb_words = transfer->len;
  1505. else if (spi->cur_bpw <= 16)
  1506. nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
  1507. else
  1508. nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
  1509. if (spi->cfg->set_number_of_data) {
  1510. ret = spi->cfg->set_number_of_data(spi, nb_words);
  1511. if (ret < 0)
  1512. goto out;
  1513. }
  1514. dev_dbg(spi->dev, "transfer communication mode set to %d\n",
  1515. spi->cur_comm);
  1516. dev_dbg(spi->dev,
  1517. "data frame of %d-bit, data packet of %d data frames\n",
  1518. spi->cur_bpw, spi->cur_fthlv);
  1519. if (STM32_SPI_HOST_MODE(spi))
  1520. dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
  1521. dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
  1522. spi->cur_xferlen, nb_words);
  1523. dev_dbg(spi->dev, "dma %s\n",
  1524. (spi->cur_usedma) ? "enabled" : "disabled");
  1525. out:
  1526. spin_unlock_irqrestore(&spi->lock, flags);
  1527. return ret;
  1528. }
  1529. /**
  1530. * stm32_spi_transfer_one - transfer a single spi_transfer
  1531. * @ctrl: controller interface
  1532. * @spi_dev: pointer to the spi device
  1533. * @transfer: pointer to spi transfer
  1534. *
  1535. * It must return 0 if the transfer is finished or 1 if the transfer is still
  1536. * in progress.
  1537. */
  1538. static int stm32_spi_transfer_one(struct spi_controller *ctrl,
  1539. struct spi_device *spi_dev,
  1540. struct spi_transfer *transfer)
  1541. {
  1542. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  1543. int ret;
  1544. spi->tx_buf = transfer->tx_buf;
  1545. spi->rx_buf = transfer->rx_buf;
  1546. spi->tx_len = spi->tx_buf ? transfer->len : 0;
  1547. spi->rx_len = spi->rx_buf ? transfer->len : 0;
  1548. spi->cur_usedma = (ctrl->can_dma &&
  1549. ctrl->can_dma(ctrl, spi_dev, transfer));
  1550. ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
  1551. if (ret) {
  1552. dev_err(spi->dev, "SPI transfer setup failed\n");
  1553. return ret;
  1554. }
  1555. if (spi->cur_usedma)
  1556. return stm32_spi_transfer_one_dma(spi, transfer);
  1557. else
  1558. return spi->cfg->transfer_one_irq(spi);
  1559. }
  1560. /**
  1561. * stm32_spi_unprepare_msg - relax the hardware
  1562. * @ctrl: controller interface
  1563. * @msg: pointer to the spi message
  1564. */
  1565. static int stm32_spi_unprepare_msg(struct spi_controller *ctrl,
  1566. struct spi_message *msg)
  1567. {
  1568. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  1569. spi->cfg->disable(spi);
  1570. return 0;
  1571. }
  1572. /**
  1573. * stm32fx_spi_config - Configure SPI controller as SPI host
  1574. * @spi: pointer to the spi controller data structure
  1575. */
  1576. static int stm32fx_spi_config(struct stm32_spi *spi)
  1577. {
  1578. unsigned long flags;
  1579. spin_lock_irqsave(&spi->lock, flags);
  1580. /* Ensure I2SMOD bit is kept cleared */
  1581. stm32_spi_clr_bits(spi, STM32FX_SPI_I2SCFGR,
  1582. STM32FX_SPI_I2SCFGR_I2SMOD);
  1583. /*
  1584. * - SS input value high
  1585. * - transmitter half duplex direction
  1586. * - Set the host mode (default Motorola mode)
  1587. * - Consider 1 host/n targets configuration and
  1588. * SS input value is determined by the SSI bit
  1589. */
  1590. stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32FX_SPI_CR1_SSI |
  1591. STM32FX_SPI_CR1_BIDIOE |
  1592. STM32FX_SPI_CR1_MSTR |
  1593. STM32FX_SPI_CR1_SSM);
  1594. spin_unlock_irqrestore(&spi->lock, flags);
  1595. return 0;
  1596. }
  1597. /**
  1598. * stm32h7_spi_config - Configure SPI controller
  1599. * @spi: pointer to the spi controller data structure
  1600. */
  1601. static int stm32h7_spi_config(struct stm32_spi *spi)
  1602. {
  1603. unsigned long flags;
  1604. u32 cr1 = 0, cfg2 = 0;
  1605. spin_lock_irqsave(&spi->lock, flags);
  1606. /* Ensure I2SMOD bit is kept cleared */
  1607. stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
  1608. STM32H7_SPI_I2SCFGR_I2SMOD);
  1609. if (STM32_SPI_DEVICE_MODE(spi)) {
  1610. /* Use native device select */
  1611. cfg2 &= ~STM32H7_SPI_CFG2_SSM;
  1612. } else {
  1613. /*
  1614. * - Transmitter half duplex direction
  1615. * - Automatic communication suspend when RX-Fifo is full
  1616. * - SS input value high
  1617. */
  1618. cr1 |= STM32H7_SPI_CR1_HDDIR | STM32H7_SPI_CR1_MASRX | STM32H7_SPI_CR1_SSI;
  1619. /*
  1620. * - Set the host mode (default Motorola mode)
  1621. * - Consider 1 host/n devices configuration and
  1622. * SS input value is determined by the SSI bit
  1623. * - keep control of all associated GPIOs
  1624. */
  1625. cfg2 |= STM32H7_SPI_CFG2_MASTER | STM32H7_SPI_CFG2_SSM | STM32H7_SPI_CFG2_AFCNTR;
  1626. }
  1627. stm32_spi_set_bits(spi, STM32H7_SPI_CR1, cr1);
  1628. stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, cfg2);
  1629. spin_unlock_irqrestore(&spi->lock, flags);
  1630. return 0;
  1631. }
  1632. static const struct stm32_spi_cfg stm32f4_spi_cfg = {
  1633. .regs = &stm32fx_spi_regspec,
  1634. .get_bpw_mask = stm32f4_spi_get_bpw_mask,
  1635. .disable = stm32fx_spi_disable,
  1636. .config = stm32fx_spi_config,
  1637. .set_bpw = stm32f4_spi_set_bpw,
  1638. .set_mode = stm32fx_spi_set_mode,
  1639. .write_tx = stm32f4_spi_write_tx,
  1640. .read_rx = stm32f4_spi_read_rx,
  1641. .transfer_one_dma_start = stm32fx_spi_transfer_one_dma_start,
  1642. .dma_tx_cb = stm32fx_spi_dma_tx_cb,
  1643. .dma_rx_cb = stm32_spi_dma_rx_cb,
  1644. .transfer_one_irq = stm32fx_spi_transfer_one_irq,
  1645. .irq_handler_event = stm32fx_spi_irq_event,
  1646. .irq_handler_thread = stm32fx_spi_irq_thread,
  1647. .baud_rate_div_min = STM32FX_SPI_BR_DIV_MIN,
  1648. .baud_rate_div_max = STM32FX_SPI_BR_DIV_MAX,
  1649. .has_fifo = false,
  1650. .has_device_mode = false,
  1651. .flags = SPI_CONTROLLER_MUST_TX,
  1652. };
  1653. static const struct stm32_spi_cfg stm32f7_spi_cfg = {
  1654. .regs = &stm32fx_spi_regspec,
  1655. .get_bpw_mask = stm32f7_spi_get_bpw_mask,
  1656. .disable = stm32fx_spi_disable,
  1657. .config = stm32fx_spi_config,
  1658. .set_bpw = stm32f7_spi_set_bpw,
  1659. .set_mode = stm32fx_spi_set_mode,
  1660. .write_tx = stm32f7_spi_write_tx,
  1661. .read_rx = stm32f7_spi_read_rx,
  1662. .transfer_one_dma_start = stm32f7_spi_transfer_one_dma_start,
  1663. .dma_tx_cb = stm32fx_spi_dma_tx_cb,
  1664. .dma_rx_cb = stm32_spi_dma_rx_cb,
  1665. .transfer_one_irq = stm32fx_spi_transfer_one_irq,
  1666. .irq_handler_event = stm32fx_spi_irq_event,
  1667. .irq_handler_thread = stm32fx_spi_irq_thread,
  1668. .baud_rate_div_min = STM32FX_SPI_BR_DIV_MIN,
  1669. .baud_rate_div_max = STM32FX_SPI_BR_DIV_MAX,
  1670. .has_fifo = false,
  1671. .flags = SPI_CONTROLLER_MUST_TX,
  1672. };
  1673. static const struct stm32_spi_cfg stm32h7_spi_cfg = {
  1674. .regs = &stm32h7_spi_regspec,
  1675. .get_fifo_size = stm32h7_spi_get_fifo_size,
  1676. .get_bpw_mask = stm32h7_spi_get_bpw_mask,
  1677. .disable = stm32h7_spi_disable,
  1678. .config = stm32h7_spi_config,
  1679. .set_bpw = stm32h7_spi_set_bpw,
  1680. .set_mode = stm32h7_spi_set_mode,
  1681. .set_data_idleness = stm32h7_spi_data_idleness,
  1682. .set_number_of_data = stm32h7_spi_number_of_data,
  1683. .write_tx = stm32h7_spi_write_txfifo,
  1684. .read_rx = stm32h7_spi_read_rxfifo,
  1685. .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
  1686. .dma_rx_cb = stm32_spi_dma_rx_cb,
  1687. /*
  1688. * dma_tx_cb is not necessary since in case of TX, dma is followed by
  1689. * SPI access hence handling is performed within the SPI interrupt
  1690. */
  1691. .transfer_one_irq = stm32h7_spi_transfer_one_irq,
  1692. .irq_handler_thread = stm32h7_spi_irq_thread,
  1693. .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
  1694. .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
  1695. .has_fifo = true,
  1696. .has_device_mode = true,
  1697. };
  1698. /*
  1699. * STM32MP2 is compatible with the STM32H7 except:
  1700. * - enforce the DMA maxburst value to 1
  1701. * - spi8 have limited feature set (TSIZE_MAX = 1024, BPW of 8 OR 16)
  1702. */
  1703. static const struct stm32_spi_cfg stm32mp25_spi_cfg = {
  1704. .regs = &stm32mp25_spi_regspec,
  1705. .get_fifo_size = stm32h7_spi_get_fifo_size,
  1706. .get_bpw_mask = stm32mp25_spi_get_bpw_mask,
  1707. .disable = stm32h7_spi_disable,
  1708. .config = stm32h7_spi_config,
  1709. .set_bpw = stm32h7_spi_set_bpw,
  1710. .set_mode = stm32h7_spi_set_mode,
  1711. .set_data_idleness = stm32h7_spi_data_idleness,
  1712. .set_number_of_data = stm32h7_spi_number_of_data,
  1713. .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
  1714. .dma_rx_cb = stm32_spi_dma_rx_cb,
  1715. /*
  1716. * dma_tx_cb is not necessary since in case of TX, dma is followed by
  1717. * SPI access hence handling is performed within the SPI interrupt
  1718. */
  1719. .transfer_one_irq = stm32h7_spi_transfer_one_irq,
  1720. .irq_handler_thread = stm32h7_spi_irq_thread,
  1721. .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
  1722. .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
  1723. .has_fifo = true,
  1724. .prevent_dma_burst = true,
  1725. .has_device_mode = true,
  1726. };
  1727. static const struct of_device_id stm32_spi_of_match[] = {
  1728. { .compatible = "st,stm32mp25-spi", .data = (void *)&stm32mp25_spi_cfg },
  1729. { .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
  1730. { .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
  1731. { .compatible = "st,stm32f7-spi", .data = (void *)&stm32f7_spi_cfg },
  1732. {},
  1733. };
  1734. MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
  1735. static int stm32h7_spi_device_abort(struct spi_controller *ctrl)
  1736. {
  1737. spi_finalize_current_transfer(ctrl);
  1738. return 0;
  1739. }
  1740. static int stm32_spi_probe(struct platform_device *pdev)
  1741. {
  1742. struct spi_controller *ctrl;
  1743. struct stm32_spi *spi;
  1744. struct resource *res;
  1745. struct reset_control *rst;
  1746. struct device_node *np = pdev->dev.of_node;
  1747. const struct stm32_spi_cfg *cfg;
  1748. bool device_mode;
  1749. int ret;
  1750. cfg = of_device_get_match_data(&pdev->dev);
  1751. if (!cfg) {
  1752. dev_err(&pdev->dev, "Failed to get match data for platform\n");
  1753. return -ENODEV;
  1754. }
  1755. device_mode = of_property_read_bool(np, "spi-slave");
  1756. if (!cfg->has_device_mode && device_mode) {
  1757. dev_err(&pdev->dev, "spi-slave not supported\n");
  1758. return -EPERM;
  1759. }
  1760. if (device_mode)
  1761. ctrl = devm_spi_alloc_target(&pdev->dev, sizeof(struct stm32_spi));
  1762. else
  1763. ctrl = devm_spi_alloc_host(&pdev->dev, sizeof(struct stm32_spi));
  1764. if (!ctrl) {
  1765. dev_err(&pdev->dev, "spi controller allocation failed\n");
  1766. return -ENOMEM;
  1767. }
  1768. platform_set_drvdata(pdev, ctrl);
  1769. spi = spi_controller_get_devdata(ctrl);
  1770. spi->dev = &pdev->dev;
  1771. spi->ctrl = ctrl;
  1772. spi->device_mode = device_mode;
  1773. spin_lock_init(&spi->lock);
  1774. spi->cfg = cfg;
  1775. spi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1776. if (IS_ERR(spi->base))
  1777. return PTR_ERR(spi->base);
  1778. spi->phys_addr = (dma_addr_t)res->start;
  1779. spi->irq = platform_get_irq(pdev, 0);
  1780. if (spi->irq <= 0)
  1781. return spi->irq;
  1782. ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
  1783. spi->cfg->irq_handler_event,
  1784. spi->cfg->irq_handler_thread,
  1785. IRQF_ONESHOT, pdev->name, ctrl);
  1786. if (ret) {
  1787. dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
  1788. ret);
  1789. return ret;
  1790. }
  1791. spi->clk = devm_clk_get(&pdev->dev, NULL);
  1792. if (IS_ERR(spi->clk)) {
  1793. ret = PTR_ERR(spi->clk);
  1794. dev_err(&pdev->dev, "clk get failed: %d\n", ret);
  1795. return ret;
  1796. }
  1797. ret = clk_prepare_enable(spi->clk);
  1798. if (ret) {
  1799. dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
  1800. return ret;
  1801. }
  1802. spi->clk_rate = clk_get_rate(spi->clk);
  1803. if (!spi->clk_rate) {
  1804. dev_err(&pdev->dev, "clk rate = 0\n");
  1805. ret = -EINVAL;
  1806. goto err_clk_disable;
  1807. }
  1808. rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
  1809. if (rst) {
  1810. if (IS_ERR(rst)) {
  1811. ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
  1812. "failed to get reset\n");
  1813. goto err_clk_disable;
  1814. }
  1815. reset_control_assert(rst);
  1816. udelay(2);
  1817. reset_control_deassert(rst);
  1818. }
  1819. if (spi->cfg->has_fifo)
  1820. spi->fifo_size = spi->cfg->get_fifo_size(spi);
  1821. spi->feature_set = STM32_SPI_FEATURE_FULL;
  1822. if (spi->cfg->regs->fullcfg.reg) {
  1823. spi->feature_set =
  1824. FIELD_GET(STM32MP25_SPI_HWCFGR1_FULLCFG,
  1825. readl_relaxed(spi->base + spi->cfg->regs->fullcfg.reg));
  1826. dev_dbg(spi->dev, "%s feature set\n",
  1827. spi->feature_set == STM32_SPI_FEATURE_FULL ? "full" : "limited");
  1828. }
  1829. /* Only for STM32H7 and after */
  1830. spi->t_size_max = spi->feature_set == STM32_SPI_FEATURE_FULL ?
  1831. STM32H7_SPI_TSIZE_MAX :
  1832. STM32MP25_SPI_TSIZE_MAX_LIMITED;
  1833. dev_dbg(spi->dev, "one message max size %d\n", spi->t_size_max);
  1834. ret = spi->cfg->config(spi);
  1835. if (ret) {
  1836. dev_err(&pdev->dev, "controller configuration failed: %d\n",
  1837. ret);
  1838. goto err_clk_disable;
  1839. }
  1840. ctrl->dev.of_node = pdev->dev.of_node;
  1841. ctrl->auto_runtime_pm = true;
  1842. ctrl->bus_num = pdev->id;
  1843. ctrl->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
  1844. SPI_3WIRE;
  1845. ctrl->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
  1846. ctrl->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
  1847. ctrl->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
  1848. ctrl->use_gpio_descriptors = true;
  1849. ctrl->optimize_message = stm32_spi_optimize_message;
  1850. ctrl->prepare_message = stm32_spi_prepare_msg;
  1851. ctrl->transfer_one = stm32_spi_transfer_one;
  1852. ctrl->unprepare_message = stm32_spi_unprepare_msg;
  1853. ctrl->flags = spi->cfg->flags;
  1854. if (STM32_SPI_DEVICE_MODE(spi))
  1855. ctrl->target_abort = stm32h7_spi_device_abort;
  1856. spi->dma_tx = dma_request_chan(spi->dev, "tx");
  1857. if (IS_ERR(spi->dma_tx)) {
  1858. ret = PTR_ERR(spi->dma_tx);
  1859. spi->dma_tx = NULL;
  1860. if (ret == -EPROBE_DEFER)
  1861. goto err_clk_disable;
  1862. dev_warn(&pdev->dev, "failed to request tx dma channel\n");
  1863. } else {
  1864. ctrl->dma_tx = spi->dma_tx;
  1865. }
  1866. spi->dma_rx = dma_request_chan(spi->dev, "rx");
  1867. if (IS_ERR(spi->dma_rx)) {
  1868. ret = PTR_ERR(spi->dma_rx);
  1869. spi->dma_rx = NULL;
  1870. if (ret == -EPROBE_DEFER)
  1871. goto err_dma_release;
  1872. dev_warn(&pdev->dev, "failed to request rx dma channel\n");
  1873. } else {
  1874. ctrl->dma_rx = spi->dma_rx;
  1875. }
  1876. if (spi->dma_tx || spi->dma_rx)
  1877. ctrl->can_dma = stm32_spi_can_dma;
  1878. pm_runtime_set_autosuspend_delay(&pdev->dev,
  1879. STM32_SPI_AUTOSUSPEND_DELAY);
  1880. pm_runtime_use_autosuspend(&pdev->dev);
  1881. pm_runtime_set_active(&pdev->dev);
  1882. pm_runtime_get_noresume(&pdev->dev);
  1883. pm_runtime_enable(&pdev->dev);
  1884. ret = spi_register_controller(ctrl);
  1885. if (ret) {
  1886. dev_err(&pdev->dev, "spi controller registration failed: %d\n",
  1887. ret);
  1888. goto err_pm_disable;
  1889. }
  1890. pm_runtime_mark_last_busy(&pdev->dev);
  1891. pm_runtime_put_autosuspend(&pdev->dev);
  1892. dev_info(&pdev->dev, "driver initialized (%s mode)\n",
  1893. STM32_SPI_HOST_MODE(spi) ? "host" : "device");
  1894. return 0;
  1895. err_pm_disable:
  1896. pm_runtime_disable(&pdev->dev);
  1897. pm_runtime_put_noidle(&pdev->dev);
  1898. pm_runtime_set_suspended(&pdev->dev);
  1899. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1900. err_dma_release:
  1901. if (spi->dma_tx)
  1902. dma_release_channel(spi->dma_tx);
  1903. if (spi->dma_rx)
  1904. dma_release_channel(spi->dma_rx);
  1905. err_clk_disable:
  1906. clk_disable_unprepare(spi->clk);
  1907. return ret;
  1908. }
  1909. static void stm32_spi_remove(struct platform_device *pdev)
  1910. {
  1911. struct spi_controller *ctrl = platform_get_drvdata(pdev);
  1912. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  1913. pm_runtime_get_sync(&pdev->dev);
  1914. spi_unregister_controller(ctrl);
  1915. spi->cfg->disable(spi);
  1916. pm_runtime_disable(&pdev->dev);
  1917. pm_runtime_put_noidle(&pdev->dev);
  1918. pm_runtime_set_suspended(&pdev->dev);
  1919. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1920. if (ctrl->dma_tx)
  1921. dma_release_channel(ctrl->dma_tx);
  1922. if (ctrl->dma_rx)
  1923. dma_release_channel(ctrl->dma_rx);
  1924. clk_disable_unprepare(spi->clk);
  1925. pinctrl_pm_select_sleep_state(&pdev->dev);
  1926. }
  1927. static int __maybe_unused stm32_spi_runtime_suspend(struct device *dev)
  1928. {
  1929. struct spi_controller *ctrl = dev_get_drvdata(dev);
  1930. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  1931. clk_disable_unprepare(spi->clk);
  1932. return pinctrl_pm_select_sleep_state(dev);
  1933. }
  1934. static int __maybe_unused stm32_spi_runtime_resume(struct device *dev)
  1935. {
  1936. struct spi_controller *ctrl = dev_get_drvdata(dev);
  1937. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  1938. int ret;
  1939. ret = pinctrl_pm_select_default_state(dev);
  1940. if (ret)
  1941. return ret;
  1942. return clk_prepare_enable(spi->clk);
  1943. }
  1944. static int __maybe_unused stm32_spi_suspend(struct device *dev)
  1945. {
  1946. struct spi_controller *ctrl = dev_get_drvdata(dev);
  1947. int ret;
  1948. ret = spi_controller_suspend(ctrl);
  1949. if (ret)
  1950. return ret;
  1951. return pm_runtime_force_suspend(dev);
  1952. }
  1953. static int __maybe_unused stm32_spi_resume(struct device *dev)
  1954. {
  1955. struct spi_controller *ctrl = dev_get_drvdata(dev);
  1956. struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
  1957. int ret;
  1958. ret = pm_runtime_force_resume(dev);
  1959. if (ret)
  1960. return ret;
  1961. ret = spi_controller_resume(ctrl);
  1962. if (ret) {
  1963. clk_disable_unprepare(spi->clk);
  1964. return ret;
  1965. }
  1966. ret = pm_runtime_resume_and_get(dev);
  1967. if (ret < 0) {
  1968. dev_err(dev, "Unable to power device:%d\n", ret);
  1969. return ret;
  1970. }
  1971. spi->cfg->config(spi);
  1972. pm_runtime_mark_last_busy(dev);
  1973. pm_runtime_put_autosuspend(dev);
  1974. return 0;
  1975. }
  1976. static const struct dev_pm_ops stm32_spi_pm_ops = {
  1977. SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
  1978. SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
  1979. stm32_spi_runtime_resume, NULL)
  1980. };
  1981. static struct platform_driver stm32_spi_driver = {
  1982. .probe = stm32_spi_probe,
  1983. .remove_new = stm32_spi_remove,
  1984. .driver = {
  1985. .name = DRIVER_NAME,
  1986. .pm = &stm32_spi_pm_ops,
  1987. .of_match_table = stm32_spi_of_match,
  1988. },
  1989. };
  1990. module_platform_driver(stm32_spi_driver);
  1991. MODULE_ALIAS("platform:" DRIVER_NAME);
  1992. MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
  1993. MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
  1994. MODULE_LICENSE("GPL v2");