1 // SPDX-License-Identifier: GPL-2.0+
3 * LPC32xx SSP interface (SPI mode)
5 * (C) Copyright 2014 DENX Software Engineering GmbH
6 * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
10 #include <linux/compat.h>
14 #include <asm/arch/clk.h>
16 /* SSP chip registers */
30 /* CR1 register defines */
31 #define SSP_CR1_SSP_ENABLE 0x0002
33 /* SR register defines */
34 #define SSP_SR_TNF 0x0002
35 /* SSP status RX FIFO not empty bit */
36 #define SSP_SR_RNE 0x0004
38 /* lpc32xx spi slave */
39 struct lpc32xx_spi_slave {
40 struct spi_slave slave;
41 struct ssp_regs *regs;
44 static inline struct lpc32xx_spi_slave *to_lpc32xx_spi_slave(
45 struct spi_slave *slave)
47 return container_of(slave, struct lpc32xx_spi_slave, slave);
50 /* the following is called in sequence by do_spi_xfer() */
52 struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode)
54 struct lpc32xx_spi_slave *lslave;
56 /* we only set up SSP0 for now, so ignore bus */
58 if (mode & SPI_3WIRE) {
59 pr_err("3-wire mode not supported");
63 if (mode & SPI_SLAVE) {
64 pr_err("slave mode not supported\n");
68 if (mode & SPI_PREAMBLE) {
69 pr_err("preamble byte skipping not supported\n");
73 lslave = spi_alloc_slave(struct lpc32xx_spi_slave, bus, cs);
75 printf("SPI_error: Fail to allocate lpc32xx_spi_slave\n");
79 lslave->regs = (struct ssp_regs *)SSP0_BASE;
82 * 8 bit frame, SPI fmt, 500kbps -> clock divider is 26.
83 * Set SCR to 0 and CPSDVSR to 26.
86 writel(0x7, &lslave->regs->cr0); /* 8-bit chunks, SPI, 1 clk/bit */
87 writel(26, &lslave->regs->cpsr); /* SSP clock = HCLK/26 = 500kbps */
88 writel(0, &lslave->regs->imsc); /* do not raise any interrupts */
89 writel(0, &lslave->regs->icr); /* clear any pending interrupt */
90 writel(0, &lslave->regs->dmacr); /* do not do DMAs */
91 writel(SSP_CR1_SSP_ENABLE, &lslave->regs->cr1); /* enable SSP0 */
92 return &lslave->slave;
95 void spi_free_slave(struct spi_slave *slave)
97 struct lpc32xx_spi_slave *lslave = to_lpc32xx_spi_slave(slave);
99 debug("(lpc32xx) spi_free_slave: 0x%08x\n", (u32)lslave);
103 int spi_claim_bus(struct spi_slave *slave)
105 /* only one bus and slave so far, always available */
109 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
110 const void *dout, void *din, unsigned long flags)
112 struct lpc32xx_spi_slave *lslave = to_lpc32xx_spi_slave(slave);
113 int bytelen = bitlen >> 3;
118 start_time = get_timer(0);
119 while ((idx_out < bytelen) || (idx_in < bytelen)) {
120 int status = readl(&lslave->regs->sr);
121 if ((idx_out < bytelen) && (status & SSP_SR_TNF))
122 writel(((u8 *)dout)[idx_out++], &lslave->regs->data);
123 if ((idx_in < bytelen) && (status & SSP_SR_RNE))
124 ((u8 *)din)[idx_in++] = readl(&lslave->regs->data);
125 if (get_timer(start_time) >= CONFIG_LPC32XX_SSP_TIMEOUT)
131 void spi_release_bus(struct spi_slave *slave)