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/kirkwood.h>
16 #include <asm/arch/spi.h>
17 #include <asm/arch/mpp.h>
19 static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE;
21 static u32 cs_spi_mpp_back[2];
23 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
24 unsigned int max_hz, unsigned int mode)
26 struct spi_slave *slave;
28 static const u32 kwspi_mpp_config[2][2] = {
29 { MPP0_SPI_SCn, 0 }, /* if cs == 0 */
30 { MPP7_SPI_SCn, 0 } /* if cs != 0 */
33 if (!spi_cs_is_valid(bus, cs))
36 slave = spi_alloc_slave_base(bus, cs);
40 writel(KWSPI_SMEMRDY, &spireg->ctrl);
42 /* calculate spi clock prescaller using max_hz */
43 data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10;
44 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
45 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
47 /* program spi clock prescaller using max_hz */
48 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
49 debug("data = 0x%08x\n", data);
51 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
52 writel(KWSPI_IRQMASK, &spireg->irq_mask);
54 /* program mpp registers to select SPI_CSn */
55 kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
60 void spi_free_slave(struct spi_slave *slave)
62 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
66 #if defined(CONFIG_SYS_KW_SPI_MPP)
67 u32 spi_mpp_backup[4];
70 __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
75 int spi_claim_bus(struct spi_slave *slave)
77 #if defined(CONFIG_SYS_KW_SPI_MPP)
79 u32 spi_mpp_config[4];
81 config = CONFIG_SYS_KW_SPI_MPP;
83 if (config & MOSI_MPP6)
84 spi_mpp_config[0] = MPP6_SPI_MOSI;
86 spi_mpp_config[0] = MPP1_SPI_MOSI;
88 if (config & SCK_MPP10)
89 spi_mpp_config[1] = MPP10_SPI_SCK;
91 spi_mpp_config[1] = MPP2_SPI_SCK;
93 if (config & MISO_MPP11)
94 spi_mpp_config[2] = MPP11_SPI_MISO;
96 spi_mpp_config[2] = MPP3_SPI_MISO;
98 spi_mpp_config[3] = 0;
99 spi_mpp_backup[3] = 0;
101 /* set new spi mpp and save current mpp config */
102 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
105 return board_spi_claim_bus(slave);
108 __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
112 void spi_release_bus(struct spi_slave *slave)
114 #if defined(CONFIG_SYS_KW_SPI_MPP)
115 kirkwood_mpp_conf(spi_mpp_backup, NULL);
118 board_spi_release_bus(slave);
121 #ifndef CONFIG_SPI_CS_IS_VALID
123 * you can define this function board specific
124 * define above CONFIG in board specific config file and
125 * provide the function in board specific src file
127 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
129 return bus == 0 && (cs == 0 || cs == 1);
137 void spi_cs_activate(struct spi_slave *slave)
139 setbits_le32(&spireg->ctrl, KWSPI_CSN_ACT);
142 void spi_cs_deactivate(struct spi_slave *slave)
144 clrbits_le32(&spireg->ctrl, KWSPI_CSN_ACT);
147 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
148 void *din, unsigned long flags)
150 unsigned int tmpdout, tmpdin;
153 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
154 slave->bus, slave->cs, dout, din, bitlen);
156 if (flags & SPI_XFER_BEGIN)
157 spi_cs_activate(slave);
160 * handle data in 8-bit chunks
161 * TBD: 2byte xfer mode to be enabled
163 clrsetbits_le32(&spireg->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE);
166 debug("loopstart bitlen %d\n", bitlen);
169 /* Shift data so it's msb-justified */
171 tmpdout = *(u32 *)dout & 0xff;
173 clrbits_le32(&spireg->irq_cause, KWSPI_SMEMRDIRQ);
174 writel(tmpdout, &spireg->dout); /* Write the data out */
175 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
179 * Wait for SPI transmit to get out
180 * or time out (1 second = 1000 ms)
181 * The NE event must be read and cleared first
183 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
184 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
186 tmpdin = readl(&spireg->din);
187 debug("spi_xfer: din %p..%08x read\n",
191 *((u8 *)din) = (u8)tmpdin;
201 if (tm >= KWSPI_TIMEOUT)
202 printf("*** spi_xfer: Time out during SPI transfer\n");
204 debug("loopend bitlen %d\n", bitlen);
207 if (flags & SPI_XFER_END)
208 spi_cs_deactivate(slave);