spi-stm32.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. * STMicroelectronics STM32 SPI Controller driver (master mode only)
  3. *
  4. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  5. * Author(s): Amelie Delaunay <amelie.delaunay@st.com> for STMicroelectronics.
  6. *
  7. * License terms: GPL V2.0.
  8. *
  9. * spi_stm32 driver is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * spi_stm32 driver is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * spi_stm32 driver. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/debugfs.h>
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/gpio.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/iopoll.h>
  28. #include <linux/module.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/reset.h>
  32. #include <linux/spi/spi.h>
  33. #define DRIVER_NAME "spi_stm32"
  34. /* STM32 SPI registers */
  35. #define STM32_SPI_CR1 0x00
  36. #define STM32_SPI_CR2 0x04
  37. #define STM32_SPI_CFG1 0x08
  38. #define STM32_SPI_CFG2 0x0C
  39. #define STM32_SPI_IER 0x10
  40. #define STM32_SPI_SR 0x14
  41. #define STM32_SPI_IFCR 0x18
  42. #define STM32_SPI_TXDR 0x20
  43. #define STM32_SPI_RXDR 0x30
  44. #define STM32_SPI_I2SCFGR 0x50
  45. /* STM32_SPI_CR1 bit fields */
  46. #define SPI_CR1_SPE BIT(0)
  47. #define SPI_CR1_MASRX BIT(8)
  48. #define SPI_CR1_CSTART BIT(9)
  49. #define SPI_CR1_CSUSP BIT(10)
  50. #define SPI_CR1_HDDIR BIT(11)
  51. #define SPI_CR1_SSI BIT(12)
  52. /* STM32_SPI_CR2 bit fields */
  53. #define SPI_CR2_TSIZE_SHIFT 0
  54. #define SPI_CR2_TSIZE GENMASK(15, 0)
  55. /* STM32_SPI_CFG1 bit fields */
  56. #define SPI_CFG1_DSIZE_SHIFT 0
  57. #define SPI_CFG1_DSIZE GENMASK(4, 0)
  58. #define SPI_CFG1_FTHLV_SHIFT 5
  59. #define SPI_CFG1_FTHLV GENMASK(8, 5)
  60. #define SPI_CFG1_RXDMAEN BIT(14)
  61. #define SPI_CFG1_TXDMAEN BIT(15)
  62. #define SPI_CFG1_MBR_SHIFT 28
  63. #define SPI_CFG1_MBR GENMASK(30, 28)
  64. #define SPI_CFG1_MBR_MIN 0
  65. #define SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28)
  66. /* STM32_SPI_CFG2 bit fields */
  67. #define SPI_CFG2_MIDI_SHIFT 4
  68. #define SPI_CFG2_MIDI GENMASK(7, 4)
  69. #define SPI_CFG2_COMM_SHIFT 17
  70. #define SPI_CFG2_COMM GENMASK(18, 17)
  71. #define SPI_CFG2_SP_SHIFT 19
  72. #define SPI_CFG2_SP GENMASK(21, 19)
  73. #define SPI_CFG2_MASTER BIT(22)
  74. #define SPI_CFG2_LSBFRST BIT(23)
  75. #define SPI_CFG2_CPHA BIT(24)
  76. #define SPI_CFG2_CPOL BIT(25)
  77. #define SPI_CFG2_SSM BIT(26)
  78. #define SPI_CFG2_AFCNTR BIT(31)
  79. /* STM32_SPI_IER bit fields */
  80. #define SPI_IER_RXPIE BIT(0)
  81. #define SPI_IER_TXPIE BIT(1)
  82. #define SPI_IER_DXPIE BIT(2)
  83. #define SPI_IER_EOTIE BIT(3)
  84. #define SPI_IER_TXTFIE BIT(4)
  85. #define SPI_IER_OVRIE BIT(6)
  86. #define SPI_IER_MODFIE BIT(9)
  87. #define SPI_IER_ALL GENMASK(10, 0)
  88. /* STM32_SPI_SR bit fields */
  89. #define SPI_SR_RXP BIT(0)
  90. #define SPI_SR_TXP BIT(1)
  91. #define SPI_SR_EOT BIT(3)
  92. #define SPI_SR_OVR BIT(6)
  93. #define SPI_SR_MODF BIT(9)
  94. #define SPI_SR_SUSP BIT(11)
  95. #define SPI_SR_RXPLVL_SHIFT 13
  96. #define SPI_SR_RXPLVL GENMASK(14, 13)
  97. #define SPI_SR_RXWNE BIT(15)
  98. /* STM32_SPI_IFCR bit fields */
  99. #define SPI_IFCR_ALL GENMASK(11, 3)
  100. /* STM32_SPI_I2SCFGR bit fields */
  101. #define SPI_I2SCFGR_I2SMOD BIT(0)
  102. /* SPI Master Baud Rate min/max divisor */
  103. #define SPI_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
  104. #define SPI_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
  105. /* SPI Communication mode */
  106. #define SPI_FULL_DUPLEX 0
  107. #define SPI_SIMPLEX_TX 1
  108. #define SPI_SIMPLEX_RX 2
  109. #define SPI_HALF_DUPLEX 3
  110. #define SPI_1HZ_NS 1000000000
  111. /**
  112. * struct stm32_spi - private data of the SPI controller
  113. * @dev: driver model representation of the controller
  114. * @master: controller master interface
  115. * @base: virtual memory area
  116. * @clk: hw kernel clock feeding the SPI clock generator
  117. * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
  118. * @rst: SPI controller reset line
  119. * @lock: prevent I/O concurrent access
  120. * @irq: SPI controller interrupt line
  121. * @fifo_size: size of the embedded fifo in bytes
  122. * @cur_midi: master inter-data idleness in ns
  123. * @cur_speed: speed configured in Hz
  124. * @cur_bpw: number of bits in a single SPI data frame
  125. * @cur_fthlv: fifo threshold level (data frames in a single data packet)
  126. * @cur_comm: SPI communication mode
  127. * @cur_xferlen: current transfer length in bytes
  128. * @cur_usedma: boolean to know if dma is used in current transfer
  129. * @tx_buf: data to be written, or NULL
  130. * @rx_buf: data to be read, or NULL
  131. * @tx_len: number of data to be written in bytes
  132. * @rx_len: number of data to be read in bytes
  133. * @dma_tx: dma channel for TX transfer
  134. * @dma_rx: dma channel for RX transfer
  135. * @phys_addr: SPI registers physical base address
  136. */
  137. struct stm32_spi {
  138. struct device *dev;
  139. struct spi_master *master;
  140. void __iomem *base;
  141. struct clk *clk;
  142. u32 clk_rate;
  143. struct reset_control *rst;
  144. spinlock_t lock; /* prevent I/O concurrent access */
  145. int irq;
  146. unsigned int fifo_size;
  147. unsigned int cur_midi;
  148. unsigned int cur_speed;
  149. unsigned int cur_bpw;
  150. unsigned int cur_fthlv;
  151. unsigned int cur_comm;
  152. unsigned int cur_xferlen;
  153. bool cur_usedma;
  154. const void *tx_buf;
  155. void *rx_buf;
  156. int tx_len;
  157. int rx_len;
  158. struct dma_chan *dma_tx;
  159. struct dma_chan *dma_rx;
  160. dma_addr_t phys_addr;
  161. };
  162. static inline void stm32_spi_set_bits(struct stm32_spi *spi,
  163. u32 offset, u32 bits)
  164. {
  165. writel_relaxed(readl_relaxed(spi->base + offset) | bits,
  166. spi->base + offset);
  167. }
  168. static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
  169. u32 offset, u32 bits)
  170. {
  171. writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
  172. spi->base + offset);
  173. }
  174. /**
  175. * stm32_spi_get_fifo_size - Return fifo size
  176. * @spi: pointer to the spi controller data structure
  177. */
  178. static int stm32_spi_get_fifo_size(struct stm32_spi *spi)
  179. {
  180. unsigned long flags;
  181. u32 count = 0;
  182. spin_lock_irqsave(&spi->lock, flags);
  183. stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
  184. while (readl_relaxed(spi->base + STM32_SPI_SR) & SPI_SR_TXP)
  185. writeb_relaxed(++count, spi->base + STM32_SPI_TXDR);
  186. stm32_spi_clr_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
  187. spin_unlock_irqrestore(&spi->lock, flags);
  188. dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
  189. return count;
  190. }
  191. /**
  192. * stm32_spi_get_bpw_mask - Return bits per word mask
  193. * @spi: pointer to the spi controller data structure
  194. */
  195. static int stm32_spi_get_bpw_mask(struct stm32_spi *spi)
  196. {
  197. unsigned long flags;
  198. u32 cfg1, max_bpw;
  199. spin_lock_irqsave(&spi->lock, flags);
  200. /*
  201. * The most significant bit at DSIZE bit field is reserved when the
  202. * maximum data size of periperal instances is limited to 16-bit
  203. */
  204. stm32_spi_set_bits(spi, STM32_SPI_CFG1, SPI_CFG1_DSIZE);
  205. cfg1 = readl_relaxed(spi->base + STM32_SPI_CFG1);
  206. max_bpw = (cfg1 & SPI_CFG1_DSIZE) >> SPI_CFG1_DSIZE_SHIFT;
  207. max_bpw += 1;
  208. spin_unlock_irqrestore(&spi->lock, flags);
  209. dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
  210. return SPI_BPW_RANGE_MASK(4, max_bpw);
  211. }
  212. /**
  213. * stm32_spi_prepare_mbr - Determine SPI_CFG1.MBR value
  214. * @spi: pointer to the spi controller data structure
  215. * @speed_hz: requested speed
  216. *
  217. * Return SPI_CFG1.MBR value in case of success or -EINVAL
  218. */
  219. static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz)
  220. {
  221. u32 div, mbrdiv;
  222. /* Ensure spi->clk_rate is even */
  223. div = DIV_ROUND_UP(spi->clk_rate & ~0x1, speed_hz);
  224. /*
  225. * SPI framework set xfer->speed_hz to master->max_speed_hz if
  226. * xfer->speed_hz is greater than master->max_speed_hz, and it returns
  227. * an error when xfer->speed_hz is lower than master->min_speed_hz, so
  228. * no need to check it there.
  229. * However, we need to ensure the following calculations.
  230. */
  231. if (div < SPI_MBR_DIV_MIN ||
  232. div > SPI_MBR_DIV_MAX)
  233. return -EINVAL;
  234. /* Determine the first power of 2 greater than or equal to div */
  235. if (div & (div - 1))
  236. mbrdiv = fls(div);
  237. else
  238. mbrdiv = fls(div) - 1;
  239. spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
  240. return mbrdiv - 1;
  241. }
  242. /**
  243. * stm32_spi_prepare_fthlv - Determine FIFO threshold level
  244. * @spi: pointer to the spi controller data structure
  245. */
  246. static u32 stm32_spi_prepare_fthlv(struct stm32_spi *spi)
  247. {
  248. u32 fthlv, half_fifo;
  249. /* data packet should not exceed 1/2 of fifo space */
  250. half_fifo = (spi->fifo_size / 2);
  251. if (spi->cur_bpw <= 8)
  252. fthlv = half_fifo;
  253. else if (spi->cur_bpw <= 16)
  254. fthlv = half_fifo / 2;
  255. else
  256. fthlv = half_fifo / 4;
  257. /* align packet size with data registers access */
  258. if (spi->cur_bpw > 8)
  259. fthlv += (fthlv % 2) ? 1 : 0;
  260. else
  261. fthlv += (fthlv % 4) ? (4 - (fthlv % 4)) : 0;
  262. return fthlv;
  263. }
  264. /**
  265. * stm32_spi_write_txfifo - Write bytes in Transmit Data Register
  266. * @spi: pointer to the spi controller data structure
  267. *
  268. * Read from tx_buf depends on remaining bytes to avoid to read beyond
  269. * tx_buf end.
  270. */
  271. static void stm32_spi_write_txfifo(struct stm32_spi *spi)
  272. {
  273. while ((spi->tx_len > 0) &&
  274. (readl_relaxed(spi->base + STM32_SPI_SR) & SPI_SR_TXP)) {
  275. u32 offs = spi->cur_xferlen - spi->tx_len;
  276. if (spi->tx_len >= sizeof(u32)) {
  277. const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
  278. writel_relaxed(*tx_buf32, spi->base + STM32_SPI_TXDR);
  279. spi->tx_len -= sizeof(u32);
  280. } else if (spi->tx_len >= sizeof(u16)) {
  281. const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
  282. writew_relaxed(*tx_buf16, spi->base + STM32_SPI_TXDR);
  283. spi->tx_len -= sizeof(u16);
  284. } else {
  285. const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
  286. writeb_relaxed(*tx_buf8, spi->base + STM32_SPI_TXDR);
  287. spi->tx_len -= sizeof(u8);
  288. }
  289. }
  290. dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
  291. }
  292. /**
  293. * stm32_spi_read_rxfifo - Read bytes in Receive Data Register
  294. * @spi: pointer to the spi controller data structure
  295. *
  296. * Write in rx_buf depends on remaining bytes to avoid to write beyond
  297. * rx_buf end.
  298. */
  299. static void stm32_spi_read_rxfifo(struct stm32_spi *spi, bool flush)
  300. {
  301. u32 sr = readl_relaxed(spi->base + STM32_SPI_SR);
  302. u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
  303. while ((spi->rx_len > 0) &&
  304. ((sr & SPI_SR_RXP) ||
  305. (flush && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
  306. u32 offs = spi->cur_xferlen - spi->rx_len;
  307. if ((spi->rx_len >= sizeof(u32)) ||
  308. (flush && (sr & SPI_SR_RXWNE))) {
  309. u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
  310. *rx_buf32 = readl_relaxed(spi->base + STM32_SPI_RXDR);
  311. spi->rx_len -= sizeof(u32);
  312. } else if ((spi->rx_len >= sizeof(u16)) ||
  313. (flush && (rxplvl >= 2 || spi->cur_bpw > 8))) {
  314. u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
  315. *rx_buf16 = readw_relaxed(spi->base + STM32_SPI_RXDR);
  316. spi->rx_len -= sizeof(u16);
  317. } else {
  318. u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
  319. *rx_buf8 = readb_relaxed(spi->base + STM32_SPI_RXDR);
  320. spi->rx_len -= sizeof(u8);
  321. }
  322. sr = readl_relaxed(spi->base + STM32_SPI_SR);
  323. rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
  324. }
  325. dev_dbg(spi->dev, "%s%s: %d bytes left\n", __func__,
  326. flush ? "(flush)" : "", spi->rx_len);
  327. }
  328. /**
  329. * stm32_spi_enable - Enable SPI controller
  330. * @spi: pointer to the spi controller data structure
  331. *
  332. * SPI data transfer is enabled but spi_ker_ck is idle.
  333. * SPI_CFG1 and SPI_CFG2 are now write protected.
  334. */
  335. static void stm32_spi_enable(struct stm32_spi *spi)
  336. {
  337. dev_dbg(spi->dev, "enable controller\n");
  338. stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
  339. }
  340. /**
  341. * stm32_spi_disable - Disable SPI controller
  342. * @spi: pointer to the spi controller data structure
  343. *
  344. * RX-Fifo is flushed when SPI controller is disabled. To prevent any data
  345. * loss, use stm32_spi_read_rxfifo(flush) to read the remaining bytes in
  346. * RX-Fifo.
  347. */
  348. static void stm32_spi_disable(struct stm32_spi *spi)
  349. {
  350. unsigned long flags;
  351. u32 cr1, sr;
  352. dev_dbg(spi->dev, "disable controller\n");
  353. spin_lock_irqsave(&spi->lock, flags);
  354. cr1 = readl_relaxed(spi->base + STM32_SPI_CR1);
  355. if (!(cr1 & SPI_CR1_SPE)) {
  356. spin_unlock_irqrestore(&spi->lock, flags);
  357. return;
  358. }
  359. /* Wait on EOT or suspend the flow */
  360. if (readl_relaxed_poll_timeout_atomic(spi->base + STM32_SPI_SR,
  361. sr, !(sr & SPI_SR_EOT),
  362. 10, 100000) < 0) {
  363. if (cr1 & SPI_CR1_CSTART) {
  364. writel_relaxed(cr1 | SPI_CR1_CSUSP,
  365. spi->base + STM32_SPI_CR1);
  366. if (readl_relaxed_poll_timeout_atomic(
  367. spi->base + STM32_SPI_SR,
  368. sr, !(sr & SPI_SR_SUSP),
  369. 10, 100000) < 0)
  370. dev_warn(spi->dev,
  371. "Suspend request timeout\n");
  372. }
  373. }
  374. if (!spi->cur_usedma && spi->rx_buf && (spi->rx_len > 0))
  375. stm32_spi_read_rxfifo(spi, true);
  376. if (spi->cur_usedma && spi->tx_buf)
  377. dmaengine_terminate_all(spi->dma_tx);
  378. if (spi->cur_usedma && spi->rx_buf)
  379. dmaengine_terminate_all(spi->dma_rx);
  380. stm32_spi_clr_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
  381. stm32_spi_clr_bits(spi, STM32_SPI_CFG1, SPI_CFG1_TXDMAEN |
  382. SPI_CFG1_RXDMAEN);
  383. /* Disable interrupts and clear status flags */
  384. writel_relaxed(0, spi->base + STM32_SPI_IER);
  385. writel_relaxed(SPI_IFCR_ALL, spi->base + STM32_SPI_IFCR);
  386. spin_unlock_irqrestore(&spi->lock, flags);
  387. }
  388. /**
  389. * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
  390. *
  391. * If the current transfer size is greater than fifo size, use DMA.
  392. */
  393. static bool stm32_spi_can_dma(struct spi_master *master,
  394. struct spi_device *spi_dev,
  395. struct spi_transfer *transfer)
  396. {
  397. struct stm32_spi *spi = spi_master_get_devdata(master);
  398. dev_dbg(spi->dev, "%s: %s\n", __func__,
  399. (transfer->len > spi->fifo_size) ? "true" : "false");
  400. return (transfer->len > spi->fifo_size);
  401. }
  402. /**
  403. * stm32_spi_irq - Interrupt handler for SPI controller events
  404. * @irq: interrupt line
  405. * @dev_id: SPI controller master interface
  406. */
  407. static irqreturn_t stm32_spi_irq(int irq, void *dev_id)
  408. {
  409. struct spi_master *master = dev_id;
  410. struct stm32_spi *spi = spi_master_get_devdata(master);
  411. u32 sr, ier, mask;
  412. unsigned long flags;
  413. bool end = false;
  414. spin_lock_irqsave(&spi->lock, flags);
  415. sr = readl_relaxed(spi->base + STM32_SPI_SR);
  416. ier = readl_relaxed(spi->base + STM32_SPI_IER);
  417. mask = ier;
  418. /* EOTIE is triggered on EOT, SUSP and TXC events. */
  419. mask |= SPI_SR_SUSP;
  420. /*
  421. * When TXTF is set, DXPIE and TXPIE are cleared. So in case of
  422. * Full-Duplex, need to poll RXP event to know if there are remaining
  423. * data, before disabling SPI.
  424. */
  425. if (spi->rx_buf && !spi->cur_usedma)
  426. mask |= SPI_SR_RXP;
  427. if (!(sr & mask)) {
  428. dev_dbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
  429. sr, ier);
  430. spin_unlock_irqrestore(&spi->lock, flags);
  431. return IRQ_NONE;
  432. }
  433. if (sr & SPI_SR_SUSP) {
  434. dev_warn(spi->dev, "Communication suspended\n");
  435. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  436. stm32_spi_read_rxfifo(spi, false);
  437. /*
  438. * If communication is suspended while using DMA, it means
  439. * that something went wrong, so stop the current transfer
  440. */
  441. if (spi->cur_usedma)
  442. end = true;
  443. }
  444. if (sr & SPI_SR_MODF) {
  445. dev_warn(spi->dev, "Mode fault: transfer aborted\n");
  446. end = true;
  447. }
  448. if (sr & SPI_SR_OVR) {
  449. dev_warn(spi->dev, "Overrun: received value discarded\n");
  450. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  451. stm32_spi_read_rxfifo(spi, false);
  452. /*
  453. * If overrun is detected while using DMA, it means that
  454. * something went wrong, so stop the current transfer
  455. */
  456. if (spi->cur_usedma)
  457. end = true;
  458. }
  459. if (sr & SPI_SR_EOT) {
  460. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  461. stm32_spi_read_rxfifo(spi, true);
  462. end = true;
  463. }
  464. if (sr & SPI_SR_TXP)
  465. if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
  466. stm32_spi_write_txfifo(spi);
  467. if (sr & SPI_SR_RXP)
  468. if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
  469. stm32_spi_read_rxfifo(spi, false);
  470. writel_relaxed(mask, spi->base + STM32_SPI_IFCR);
  471. spin_unlock_irqrestore(&spi->lock, flags);
  472. if (end) {
  473. spi_finalize_current_transfer(master);
  474. stm32_spi_disable(spi);
  475. }
  476. return IRQ_HANDLED;
  477. }
  478. /**
  479. * stm32_spi_setup - setup device chip select
  480. */
  481. static int stm32_spi_setup(struct spi_device *spi_dev)
  482. {
  483. int ret = 0;
  484. if (!gpio_is_valid(spi_dev->cs_gpio)) {
  485. dev_err(&spi_dev->dev, "%d is not a valid gpio\n",
  486. spi_dev->cs_gpio);
  487. return -EINVAL;
  488. }
  489. dev_dbg(&spi_dev->dev, "%s: set gpio%d output %s\n", __func__,
  490. spi_dev->cs_gpio,
  491. (spi_dev->mode & SPI_CS_HIGH) ? "low" : "high");
  492. ret = gpio_direction_output(spi_dev->cs_gpio,
  493. !(spi_dev->mode & SPI_CS_HIGH));
  494. return ret;
  495. }
  496. /**
  497. * stm32_spi_prepare_msg - set up the controller to transfer a single message
  498. */
  499. static int stm32_spi_prepare_msg(struct spi_master *master,
  500. struct spi_message *msg)
  501. {
  502. struct stm32_spi *spi = spi_master_get_devdata(master);
  503. struct spi_device *spi_dev = msg->spi;
  504. struct device_node *np = spi_dev->dev.of_node;
  505. unsigned long flags;
  506. u32 cfg2_clrb = 0, cfg2_setb = 0;
  507. /* SPI slave device may need time between data frames */
  508. spi->cur_midi = 0;
  509. if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
  510. dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
  511. if (spi_dev->mode & SPI_CPOL)
  512. cfg2_setb |= SPI_CFG2_CPOL;
  513. else
  514. cfg2_clrb |= SPI_CFG2_CPOL;
  515. if (spi_dev->mode & SPI_CPHA)
  516. cfg2_setb |= SPI_CFG2_CPHA;
  517. else
  518. cfg2_clrb |= SPI_CFG2_CPHA;
  519. if (spi_dev->mode & SPI_LSB_FIRST)
  520. cfg2_setb |= SPI_CFG2_LSBFRST;
  521. else
  522. cfg2_clrb |= SPI_CFG2_LSBFRST;
  523. dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
  524. spi_dev->mode & SPI_CPOL,
  525. spi_dev->mode & SPI_CPHA,
  526. spi_dev->mode & SPI_LSB_FIRST,
  527. spi_dev->mode & SPI_CS_HIGH);
  528. spin_lock_irqsave(&spi->lock, flags);
  529. if (cfg2_clrb || cfg2_setb)
  530. writel_relaxed(
  531. (readl_relaxed(spi->base + STM32_SPI_CFG2) &
  532. ~cfg2_clrb) | cfg2_setb,
  533. spi->base + STM32_SPI_CFG2);
  534. spin_unlock_irqrestore(&spi->lock, flags);
  535. return 0;
  536. }
  537. /**
  538. * stm32_spi_dma_cb - dma callback
  539. *
  540. * DMA callback is called when the transfer is complete or when an error
  541. * occurs. If the transfer is complete, EOT flag is raised.
  542. */
  543. static void stm32_spi_dma_cb(void *data)
  544. {
  545. struct stm32_spi *spi = data;
  546. unsigned long flags;
  547. u32 sr;
  548. spin_lock_irqsave(&spi->lock, flags);
  549. sr = readl_relaxed(spi->base + STM32_SPI_SR);
  550. spin_unlock_irqrestore(&spi->lock, flags);
  551. if (!(sr & SPI_SR_EOT))
  552. dev_warn(spi->dev, "DMA error (sr=0x%08x)\n", sr);
  553. /* Now wait for EOT, or SUSP or OVR in case of error */
  554. }
  555. /**
  556. * stm32_spi_dma_config - configure dma slave channel depending on current
  557. * transfer bits_per_word.
  558. */
  559. static void stm32_spi_dma_config(struct stm32_spi *spi,
  560. struct dma_slave_config *dma_conf,
  561. enum dma_transfer_direction dir)
  562. {
  563. enum dma_slave_buswidth buswidth;
  564. u32 maxburst;
  565. if (spi->cur_bpw <= 8)
  566. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  567. else if (spi->cur_bpw <= 16)
  568. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  569. else
  570. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  571. /* Valid for DMA Half or Full Fifo threshold */
  572. if (spi->cur_fthlv == 2)
  573. maxburst = 1;
  574. else
  575. maxburst = spi->cur_fthlv;
  576. memset(dma_conf, 0, sizeof(struct dma_slave_config));
  577. dma_conf->direction = dir;
  578. if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
  579. dma_conf->src_addr = spi->phys_addr + STM32_SPI_RXDR;
  580. dma_conf->src_addr_width = buswidth;
  581. dma_conf->src_maxburst = maxburst;
  582. dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
  583. buswidth, maxburst);
  584. } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
  585. dma_conf->dst_addr = spi->phys_addr + STM32_SPI_TXDR;
  586. dma_conf->dst_addr_width = buswidth;
  587. dma_conf->dst_maxburst = maxburst;
  588. dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
  589. buswidth, maxburst);
  590. }
  591. }
  592. /**
  593. * stm32_spi_transfer_one_irq - transfer a single spi_transfer using
  594. * interrupts
  595. *
  596. * It must returns 0 if the transfer is finished or 1 if the transfer is still
  597. * in progress.
  598. */
  599. static int stm32_spi_transfer_one_irq(struct stm32_spi *spi)
  600. {
  601. unsigned long flags;
  602. u32 ier = 0;
  603. /* Enable the interrupts relative to the current communication mode */
  604. if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
  605. ier |= SPI_IER_DXPIE;
  606. else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
  607. ier |= SPI_IER_TXPIE;
  608. else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
  609. ier |= SPI_IER_RXPIE;
  610. /* Enable the interrupts relative to the end of transfer */
  611. ier |= SPI_IER_EOTIE | SPI_IER_TXTFIE | SPI_IER_OVRIE | SPI_IER_MODFIE;
  612. spin_lock_irqsave(&spi->lock, flags);
  613. stm32_spi_enable(spi);
  614. /* Be sure to have data in fifo before starting data transfer */
  615. if (spi->tx_buf)
  616. stm32_spi_write_txfifo(spi);
  617. stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_CSTART);
  618. writel_relaxed(ier, spi->base + STM32_SPI_IER);
  619. spin_unlock_irqrestore(&spi->lock, flags);
  620. return 1;
  621. }
  622. /**
  623. * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
  624. *
  625. * It must returns 0 if the transfer is finished or 1 if the transfer is still
  626. * in progress.
  627. */
  628. static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
  629. struct spi_transfer *xfer)
  630. {
  631. struct dma_slave_config tx_dma_conf, rx_dma_conf;
  632. struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
  633. unsigned long flags;
  634. u32 ier = 0;
  635. spin_lock_irqsave(&spi->lock, flags);
  636. rx_dma_desc = NULL;
  637. if (spi->rx_buf) {
  638. stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
  639. dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
  640. /* Enable Rx DMA request */
  641. stm32_spi_set_bits(spi, STM32_SPI_CFG1, SPI_CFG1_RXDMAEN);
  642. rx_dma_desc = dmaengine_prep_slave_sg(
  643. spi->dma_rx, xfer->rx_sg.sgl,
  644. xfer->rx_sg.nents,
  645. rx_dma_conf.direction,
  646. DMA_PREP_INTERRUPT);
  647. }
  648. tx_dma_desc = NULL;
  649. if (spi->tx_buf) {
  650. stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
  651. dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
  652. tx_dma_desc = dmaengine_prep_slave_sg(
  653. spi->dma_tx, xfer->tx_sg.sgl,
  654. xfer->tx_sg.nents,
  655. tx_dma_conf.direction,
  656. DMA_PREP_INTERRUPT);
  657. }
  658. if ((spi->tx_buf && !tx_dma_desc) ||
  659. (spi->rx_buf && !rx_dma_desc))
  660. goto dma_desc_error;
  661. if (rx_dma_desc) {
  662. rx_dma_desc->callback = stm32_spi_dma_cb;
  663. rx_dma_desc->callback_param = spi;
  664. if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
  665. dev_err(spi->dev, "Rx DMA submit failed\n");
  666. goto dma_desc_error;
  667. }
  668. /* Enable Rx DMA channel */
  669. dma_async_issue_pending(spi->dma_rx);
  670. }
  671. if (tx_dma_desc) {
  672. if (spi->cur_comm == SPI_SIMPLEX_TX) {
  673. tx_dma_desc->callback = stm32_spi_dma_cb;
  674. tx_dma_desc->callback_param = spi;
  675. }
  676. if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
  677. dev_err(spi->dev, "Tx DMA submit failed\n");
  678. goto dma_submit_error;
  679. }
  680. /* Enable Tx DMA channel */
  681. dma_async_issue_pending(spi->dma_tx);
  682. /* Enable Tx DMA request */
  683. stm32_spi_set_bits(spi, STM32_SPI_CFG1, SPI_CFG1_TXDMAEN);
  684. }
  685. /* Enable the interrupts relative to the end of transfer */
  686. ier |= SPI_IER_EOTIE | SPI_IER_TXTFIE | SPI_IER_OVRIE | SPI_IER_MODFIE;
  687. writel_relaxed(ier, spi->base + STM32_SPI_IER);
  688. stm32_spi_enable(spi);
  689. stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_CSTART);
  690. spin_unlock_irqrestore(&spi->lock, flags);
  691. return 1;
  692. dma_submit_error:
  693. if (spi->rx_buf)
  694. dmaengine_terminate_all(spi->dma_rx);
  695. dma_desc_error:
  696. stm32_spi_clr_bits(spi, STM32_SPI_CFG1, SPI_CFG1_RXDMAEN);
  697. spin_unlock_irqrestore(&spi->lock, flags);
  698. dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
  699. return stm32_spi_transfer_one_irq(spi);
  700. }
  701. /**
  702. * stm32_spi_transfer_one_setup - common setup to transfer a single
  703. * spi_transfer either using DMA or
  704. * interrupts.
  705. */
  706. static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
  707. struct spi_device *spi_dev,
  708. struct spi_transfer *transfer)
  709. {
  710. unsigned long flags;
  711. u32 cfg1_clrb = 0, cfg1_setb = 0, cfg2_clrb = 0, cfg2_setb = 0;
  712. u32 mode, nb_words;
  713. int ret = 0;
  714. spin_lock_irqsave(&spi->lock, flags);
  715. if (spi->cur_bpw != transfer->bits_per_word) {
  716. u32 bpw, fthlv;
  717. spi->cur_bpw = transfer->bits_per_word;
  718. bpw = spi->cur_bpw - 1;
  719. cfg1_clrb |= SPI_CFG1_DSIZE;
  720. cfg1_setb |= (bpw << SPI_CFG1_DSIZE_SHIFT) & SPI_CFG1_DSIZE;
  721. spi->cur_fthlv = stm32_spi_prepare_fthlv(spi);
  722. fthlv = spi->cur_fthlv - 1;
  723. cfg1_clrb |= SPI_CFG1_FTHLV;
  724. cfg1_setb |= (fthlv << SPI_CFG1_FTHLV_SHIFT) & SPI_CFG1_FTHLV;
  725. }
  726. if (spi->cur_speed != transfer->speed_hz) {
  727. int mbr;
  728. /* Update spi->cur_speed with real clock speed */
  729. mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz);
  730. if (mbr < 0) {
  731. ret = mbr;
  732. goto out;
  733. }
  734. transfer->speed_hz = spi->cur_speed;
  735. cfg1_clrb |= SPI_CFG1_MBR;
  736. cfg1_setb |= ((u32)mbr << SPI_CFG1_MBR_SHIFT) & SPI_CFG1_MBR;
  737. }
  738. if (cfg1_clrb || cfg1_setb)
  739. writel_relaxed((readl_relaxed(spi->base + STM32_SPI_CFG1) &
  740. ~cfg1_clrb) | cfg1_setb,
  741. spi->base + STM32_SPI_CFG1);
  742. mode = SPI_FULL_DUPLEX;
  743. if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
  744. /*
  745. * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
  746. * is forbidden und unvalidated by SPI subsystem so depending
  747. * on the valid buffer, we can determine the direction of the
  748. * transfer.
  749. */
  750. mode = SPI_HALF_DUPLEX;
  751. if (!transfer->tx_buf)
  752. stm32_spi_clr_bits(spi, STM32_SPI_CR1, SPI_CR1_HDDIR);
  753. else if (!transfer->rx_buf)
  754. stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_HDDIR);
  755. } else {
  756. if (!transfer->tx_buf)
  757. mode = SPI_SIMPLEX_RX;
  758. else if (!transfer->rx_buf)
  759. mode = SPI_SIMPLEX_TX;
  760. }
  761. if (spi->cur_comm != mode) {
  762. spi->cur_comm = mode;
  763. cfg2_clrb |= SPI_CFG2_COMM;
  764. cfg2_setb |= (mode << SPI_CFG2_COMM_SHIFT) & SPI_CFG2_COMM;
  765. }
  766. cfg2_clrb |= SPI_CFG2_MIDI;
  767. if ((transfer->len > 1) && (spi->cur_midi > 0)) {
  768. u32 sck_period_ns = DIV_ROUND_UP(SPI_1HZ_NS, spi->cur_speed);
  769. u32 midi = min((u32)DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
  770. (u32)SPI_CFG2_MIDI >> SPI_CFG2_MIDI_SHIFT);
  771. dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
  772. sck_period_ns, midi, midi * sck_period_ns);
  773. cfg2_setb |= (midi << SPI_CFG2_MIDI_SHIFT) & SPI_CFG2_MIDI;
  774. }
  775. if (cfg2_clrb || cfg2_setb)
  776. writel_relaxed((readl_relaxed(spi->base + STM32_SPI_CFG2) &
  777. ~cfg2_clrb) | cfg2_setb,
  778. spi->base + STM32_SPI_CFG2);
  779. if (spi->cur_bpw <= 8)
  780. nb_words = transfer->len;
  781. else if (spi->cur_bpw <= 16)
  782. nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
  783. else
  784. nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
  785. nb_words <<= SPI_CR2_TSIZE_SHIFT;
  786. if (nb_words <= SPI_CR2_TSIZE) {
  787. writel_relaxed(nb_words, spi->base + STM32_SPI_CR2);
  788. } else {
  789. ret = -EMSGSIZE;
  790. goto out;
  791. }
  792. spi->cur_xferlen = transfer->len;
  793. dev_dbg(spi->dev, "transfer communication mode set to %d\n",
  794. spi->cur_comm);
  795. dev_dbg(spi->dev,
  796. "data frame of %d-bit, data packet of %d data frames\n",
  797. spi->cur_bpw, spi->cur_fthlv);
  798. dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
  799. dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
  800. spi->cur_xferlen, nb_words);
  801. dev_dbg(spi->dev, "dma %s\n",
  802. (spi->cur_usedma) ? "enabled" : "disabled");
  803. out:
  804. spin_unlock_irqrestore(&spi->lock, flags);
  805. return ret;
  806. }
  807. /**
  808. * stm32_spi_transfer_one - transfer a single spi_transfer
  809. *
  810. * It must return 0 if the transfer is finished or 1 if the transfer is still
  811. * in progress.
  812. */
  813. static int stm32_spi_transfer_one(struct spi_master *master,
  814. struct spi_device *spi_dev,
  815. struct spi_transfer *transfer)
  816. {
  817. struct stm32_spi *spi = spi_master_get_devdata(master);
  818. int ret;
  819. /* Don't do anything on 0 bytes transfers */
  820. if (transfer->len == 0)
  821. return 0;
  822. spi->tx_buf = transfer->tx_buf;
  823. spi->rx_buf = transfer->rx_buf;
  824. spi->tx_len = spi->tx_buf ? transfer->len : 0;
  825. spi->rx_len = spi->rx_buf ? transfer->len : 0;
  826. spi->cur_usedma = (master->can_dma &&
  827. stm32_spi_can_dma(master, spi_dev, transfer));
  828. ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
  829. if (ret) {
  830. dev_err(spi->dev, "SPI transfer setup failed\n");
  831. return ret;
  832. }
  833. if (spi->cur_usedma)
  834. return stm32_spi_transfer_one_dma(spi, transfer);
  835. else
  836. return stm32_spi_transfer_one_irq(spi);
  837. }
  838. /**
  839. * stm32_spi_unprepare_msg - relax the hardware
  840. *
  841. * Normally, if TSIZE has been configured, we should relax the hardware at the
  842. * reception of the EOT interrupt. But in case of error, EOT will not be
  843. * raised. So the subsystem unprepare_message call allows us to properly
  844. * complete the transfer from an hardware point of view.
  845. */
  846. static int stm32_spi_unprepare_msg(struct spi_master *master,
  847. struct spi_message *msg)
  848. {
  849. struct stm32_spi *spi = spi_master_get_devdata(master);
  850. stm32_spi_disable(spi);
  851. return 0;
  852. }
  853. /**
  854. * stm32_spi_config - Configure SPI controller as SPI master
  855. */
  856. static int stm32_spi_config(struct stm32_spi *spi)
  857. {
  858. unsigned long flags;
  859. spin_lock_irqsave(&spi->lock, flags);
  860. /* Ensure I2SMOD bit is kept cleared */
  861. stm32_spi_clr_bits(spi, STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
  862. /*
  863. * - SS input value high
  864. * - transmitter half duplex direction
  865. * - automatic communication suspend when RX-Fifo is full
  866. */
  867. stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_SSI |
  868. SPI_CR1_HDDIR |
  869. SPI_CR1_MASRX);
  870. /*
  871. * - Set the master mode (default Motorola mode)
  872. * - Consider 1 master/n slaves configuration and
  873. * SS input value is determined by the SSI bit
  874. * - keep control of all associated GPIOs
  875. */
  876. stm32_spi_set_bits(spi, STM32_SPI_CFG2, SPI_CFG2_MASTER |
  877. SPI_CFG2_SSM |
  878. SPI_CFG2_AFCNTR);
  879. spin_unlock_irqrestore(&spi->lock, flags);
  880. return 0;
  881. }
  882. static const struct of_device_id stm32_spi_of_match[] = {
  883. { .compatible = "st,stm32h7-spi", },
  884. {},
  885. };
  886. MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
  887. static int stm32_spi_probe(struct platform_device *pdev)
  888. {
  889. struct spi_master *master;
  890. struct stm32_spi *spi;
  891. struct resource *res;
  892. int i, ret;
  893. master = spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
  894. if (!master) {
  895. dev_err(&pdev->dev, "spi master allocation failed\n");
  896. return -ENOMEM;
  897. }
  898. platform_set_drvdata(pdev, master);
  899. spi = spi_master_get_devdata(master);
  900. spi->dev = &pdev->dev;
  901. spi->master = master;
  902. spin_lock_init(&spi->lock);
  903. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  904. spi->base = devm_ioremap_resource(&pdev->dev, res);
  905. if (IS_ERR(spi->base)) {
  906. ret = PTR_ERR(spi->base);
  907. goto err_master_put;
  908. }
  909. spi->phys_addr = (dma_addr_t)res->start;
  910. spi->irq = platform_get_irq(pdev, 0);
  911. if (spi->irq <= 0) {
  912. dev_err(&pdev->dev, "no irq: %d\n", spi->irq);
  913. ret = -ENOENT;
  914. goto err_master_put;
  915. }
  916. ret = devm_request_threaded_irq(&pdev->dev, spi->irq, NULL,
  917. stm32_spi_irq, IRQF_ONESHOT,
  918. pdev->name, master);
  919. if (ret) {
  920. dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
  921. ret);
  922. goto err_master_put;
  923. }
  924. spi->clk = devm_clk_get(&pdev->dev, 0);
  925. if (IS_ERR(spi->clk)) {
  926. ret = PTR_ERR(spi->clk);
  927. dev_err(&pdev->dev, "clk get failed: %d\n", ret);
  928. goto err_master_put;
  929. }
  930. ret = clk_prepare_enable(spi->clk);
  931. if (ret) {
  932. dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
  933. goto err_master_put;
  934. }
  935. spi->clk_rate = clk_get_rate(spi->clk);
  936. if (!spi->clk_rate) {
  937. dev_err(&pdev->dev, "clk rate = 0\n");
  938. ret = -EINVAL;
  939. goto err_master_put;
  940. }
  941. spi->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  942. if (!IS_ERR(spi->rst)) {
  943. reset_control_assert(spi->rst);
  944. udelay(2);
  945. reset_control_deassert(spi->rst);
  946. }
  947. spi->fifo_size = stm32_spi_get_fifo_size(spi);
  948. ret = stm32_spi_config(spi);
  949. if (ret) {
  950. dev_err(&pdev->dev, "controller configuration failed: %d\n",
  951. ret);
  952. goto err_clk_disable;
  953. }
  954. master->dev.of_node = pdev->dev.of_node;
  955. master->auto_runtime_pm = true;
  956. master->bus_num = pdev->id;
  957. master->mode_bits = SPI_MODE_3 | SPI_CS_HIGH | SPI_LSB_FIRST |
  958. SPI_3WIRE | SPI_LOOP;
  959. master->bits_per_word_mask = stm32_spi_get_bpw_mask(spi);
  960. master->max_speed_hz = spi->clk_rate / SPI_MBR_DIV_MIN;
  961. master->min_speed_hz = spi->clk_rate / SPI_MBR_DIV_MAX;
  962. master->setup = stm32_spi_setup;
  963. master->prepare_message = stm32_spi_prepare_msg;
  964. master->transfer_one = stm32_spi_transfer_one;
  965. master->unprepare_message = stm32_spi_unprepare_msg;
  966. spi->dma_tx = dma_request_slave_channel(spi->dev, "tx");
  967. if (!spi->dma_tx)
  968. dev_warn(&pdev->dev, "failed to request tx dma channel\n");
  969. else
  970. master->dma_tx = spi->dma_tx;
  971. spi->dma_rx = dma_request_slave_channel(spi->dev, "rx");
  972. if (!spi->dma_rx)
  973. dev_warn(&pdev->dev, "failed to request rx dma channel\n");
  974. else
  975. master->dma_rx = spi->dma_rx;
  976. if (spi->dma_tx || spi->dma_rx)
  977. master->can_dma = stm32_spi_can_dma;
  978. pm_runtime_set_active(&pdev->dev);
  979. pm_runtime_enable(&pdev->dev);
  980. ret = devm_spi_register_master(&pdev->dev, master);
  981. if (ret) {
  982. dev_err(&pdev->dev, "spi master registration failed: %d\n",
  983. ret);
  984. goto err_dma_release;
  985. }
  986. if (!master->cs_gpios) {
  987. dev_err(&pdev->dev, "no CS gpios available\n");
  988. ret = -EINVAL;
  989. goto err_dma_release;
  990. }
  991. for (i = 0; i < master->num_chipselect; i++) {
  992. if (!gpio_is_valid(master->cs_gpios[i])) {
  993. dev_err(&pdev->dev, "%i is not a valid gpio\n",
  994. master->cs_gpios[i]);
  995. ret = -EINVAL;
  996. goto err_dma_release;
  997. }
  998. ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
  999. DRIVER_NAME);
  1000. if (ret) {
  1001. dev_err(&pdev->dev, "can't get CS gpio %i\n",
  1002. master->cs_gpios[i]);
  1003. goto err_dma_release;
  1004. }
  1005. }
  1006. dev_info(&pdev->dev, "driver initialized\n");
  1007. return 0;
  1008. err_dma_release:
  1009. if (spi->dma_tx)
  1010. dma_release_channel(spi->dma_tx);
  1011. if (spi->dma_rx)
  1012. dma_release_channel(spi->dma_rx);
  1013. pm_runtime_disable(&pdev->dev);
  1014. err_clk_disable:
  1015. clk_disable_unprepare(spi->clk);
  1016. err_master_put:
  1017. spi_master_put(master);
  1018. return ret;
  1019. }
  1020. static int stm32_spi_remove(struct platform_device *pdev)
  1021. {
  1022. struct spi_master *master = platform_get_drvdata(pdev);
  1023. struct stm32_spi *spi = spi_master_get_devdata(master);
  1024. stm32_spi_disable(spi);
  1025. if (master->dma_tx)
  1026. dma_release_channel(master->dma_tx);
  1027. if (master->dma_rx)
  1028. dma_release_channel(master->dma_rx);
  1029. clk_disable_unprepare(spi->clk);
  1030. pm_runtime_disable(&pdev->dev);
  1031. return 0;
  1032. }
  1033. #ifdef CONFIG_PM
  1034. static int stm32_spi_runtime_suspend(struct device *dev)
  1035. {
  1036. struct spi_master *master = dev_get_drvdata(dev);
  1037. struct stm32_spi *spi = spi_master_get_devdata(master);
  1038. clk_disable_unprepare(spi->clk);
  1039. return 0;
  1040. }
  1041. static int stm32_spi_runtime_resume(struct device *dev)
  1042. {
  1043. struct spi_master *master = dev_get_drvdata(dev);
  1044. struct stm32_spi *spi = spi_master_get_devdata(master);
  1045. return clk_prepare_enable(spi->clk);
  1046. }
  1047. #endif
  1048. #ifdef CONFIG_PM_SLEEP
  1049. static int stm32_spi_suspend(struct device *dev)
  1050. {
  1051. struct spi_master *master = dev_get_drvdata(dev);
  1052. int ret;
  1053. ret = spi_master_suspend(master);
  1054. if (ret)
  1055. return ret;
  1056. return pm_runtime_force_suspend(dev);
  1057. }
  1058. static int stm32_spi_resume(struct device *dev)
  1059. {
  1060. struct spi_master *master = dev_get_drvdata(dev);
  1061. struct stm32_spi *spi = spi_master_get_devdata(master);
  1062. int ret;
  1063. ret = pm_runtime_force_resume(dev);
  1064. if (ret)
  1065. return ret;
  1066. ret = spi_master_resume(master);
  1067. if (ret)
  1068. clk_disable_unprepare(spi->clk);
  1069. return ret;
  1070. }
  1071. #endif
  1072. static const struct dev_pm_ops stm32_spi_pm_ops = {
  1073. SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
  1074. SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
  1075. stm32_spi_runtime_resume, NULL)
  1076. };
  1077. static struct platform_driver stm32_spi_driver = {
  1078. .probe = stm32_spi_probe,
  1079. .remove = stm32_spi_remove,
  1080. .driver = {
  1081. .name = DRIVER_NAME,
  1082. .pm = &stm32_spi_pm_ops,
  1083. .of_match_table = stm32_spi_of_match,
  1084. },
  1085. };
  1086. module_platform_driver(stm32_spi_driver);
  1087. MODULE_ALIAS("platform:" DRIVER_NAME);
  1088. MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
  1089. MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
  1090. MODULE_LICENSE("GPL v2");