3 * Marvell Semiconductor <www.marvell.com>
4 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 * Derived from drivers/spi/mpc8xxx_spi.c
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/arch/soc.h>
16 #ifdef CONFIG_KIRKWOOD
17 #include <asm/arch/mpp.h>
19 #include <asm/arch-mvebu/spi.h>
21 static struct kwspi_registers *spireg =
22 (struct kwspi_registers *)MVEBU_SPI_BASE;
24 #ifdef CONFIG_KIRKWOOD
25 static u32 cs_spi_mpp_back[2];
28 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
29 unsigned int max_hz, unsigned int mode)
31 struct spi_slave *slave;
33 #ifdef CONFIG_KIRKWOOD
34 static const u32 kwspi_mpp_config[2][2] = {
35 { MPP0_SPI_SCn, 0 }, /* if cs == 0 */
36 { MPP7_SPI_SCn, 0 } /* if cs != 0 */
40 if (!spi_cs_is_valid(bus, cs))
43 slave = spi_alloc_slave_base(bus, cs);
47 writel(KWSPI_SMEMRDY, &spireg->ctrl);
49 /* calculate spi clock prescaller using max_hz */
50 data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10;
51 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
52 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
54 /* program spi clock prescaller using max_hz */
55 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
56 debug("data = 0x%08x\n", data);
58 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
59 writel(KWSPI_IRQMASK, &spireg->irq_mask);
61 #ifdef CONFIG_KIRKWOOD
62 /* program mpp registers to select SPI_CSn */
63 kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
69 void spi_free_slave(struct spi_slave *slave)
71 #ifdef CONFIG_KIRKWOOD
72 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
77 #if defined(CONFIG_SYS_KW_SPI_MPP)
78 u32 spi_mpp_backup[4];
81 __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
86 int spi_claim_bus(struct spi_slave *slave)
88 #if defined(CONFIG_SYS_KW_SPI_MPP)
90 u32 spi_mpp_config[4];
92 config = CONFIG_SYS_KW_SPI_MPP;
94 if (config & MOSI_MPP6)
95 spi_mpp_config[0] = MPP6_SPI_MOSI;
97 spi_mpp_config[0] = MPP1_SPI_MOSI;
99 if (config & SCK_MPP10)
100 spi_mpp_config[1] = MPP10_SPI_SCK;
102 spi_mpp_config[1] = MPP2_SPI_SCK;
104 if (config & MISO_MPP11)
105 spi_mpp_config[2] = MPP11_SPI_MISO;
107 spi_mpp_config[2] = MPP3_SPI_MISO;
109 spi_mpp_config[3] = 0;
110 spi_mpp_backup[3] = 0;
112 /* set new spi mpp and save current mpp config */
113 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
116 return board_spi_claim_bus(slave);
119 __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
123 void spi_release_bus(struct spi_slave *slave)
125 #if defined(CONFIG_SYS_KW_SPI_MPP)
126 kirkwood_mpp_conf(spi_mpp_backup, NULL);
129 board_spi_release_bus(slave);
132 #ifndef CONFIG_SPI_CS_IS_VALID
134 * you can define this function board specific
135 * define above CONFIG in board specific config file and
136 * provide the function in board specific src file
138 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
140 return bus == 0 && (cs == 0 || cs == 1);
148 static void _spi_cs_activate(struct kwspi_registers *reg)
150 setbits_le32(®->ctrl, KWSPI_CSN_ACT);
153 static void _spi_cs_deactivate(struct kwspi_registers *reg)
155 clrbits_le32(®->ctrl, KWSPI_CSN_ACT);
158 void spi_cs_activate(struct spi_slave *slave)
160 _spi_cs_activate(spireg);
163 void spi_cs_deactivate(struct spi_slave *slave)
165 _spi_cs_deactivate(spireg);
168 static int _spi_xfer(struct kwspi_registers *reg, unsigned int bitlen,
169 const void *dout, void *din, unsigned long flags)
171 unsigned int tmpdout, tmpdin;
174 debug("spi_xfer: dout %p din %p bitlen %u\n", dout, din, bitlen);
176 if (flags & SPI_XFER_BEGIN)
177 _spi_cs_activate(reg);
180 * handle data in 8-bit chunks
181 * TBD: 2byte xfer mode to be enabled
183 clrsetbits_le32(®->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE);
186 debug("loopstart bitlen %d\n", bitlen);
189 /* Shift data so it's msb-justified */
191 tmpdout = *(u32 *)dout & 0xff;
193 clrbits_le32(®->irq_cause, KWSPI_SMEMRDIRQ);
194 writel(tmpdout, ®->dout); /* Write the data out */
195 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
199 * Wait for SPI transmit to get out
200 * or time out (1 second = 1000 ms)
201 * The NE event must be read and cleared first
203 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
204 if (readl(®->irq_cause) & KWSPI_SMEMRDIRQ) {
206 tmpdin = readl(®->din);
207 debug("spi_xfer: din %p..%08x read\n",
211 *((u8 *)din) = (u8)tmpdin;
221 if (tm >= KWSPI_TIMEOUT)
222 printf("*** spi_xfer: Time out during SPI transfer\n");
224 debug("loopend bitlen %d\n", bitlen);
227 if (flags & SPI_XFER_END)
228 _spi_cs_deactivate(reg);
233 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
234 const void *dout, void *din, unsigned long flags)
236 return _spi_xfer(spireg, bitlen, dout, din, flags);