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+
16 #include <asm/arch/soc.h>
17 #ifdef CONFIG_KIRKWOOD
18 #include <asm/arch/mpp.h>
20 #include <asm/arch-mvebu/spi.h>
22 static void _spi_cs_activate(struct kwspi_registers *reg)
24 setbits_le32(®->ctrl, KWSPI_CSN_ACT);
27 static void _spi_cs_deactivate(struct kwspi_registers *reg)
29 clrbits_le32(®->ctrl, KWSPI_CSN_ACT);
32 static int _spi_xfer(struct kwspi_registers *reg, unsigned int bitlen,
33 const void *dout, void *din, unsigned long flags)
35 unsigned int tmpdout, tmpdin;
38 debug("spi_xfer: dout %p din %p bitlen %u\n", dout, din, bitlen);
40 if (flags & SPI_XFER_BEGIN)
41 _spi_cs_activate(reg);
44 * handle data in 8-bit chunks
45 * TBD: 2byte xfer mode to be enabled
47 clrsetbits_le32(®->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE);
50 debug("loopstart bitlen %d\n", bitlen);
53 /* Shift data so it's msb-justified */
55 tmpdout = *(u32 *)dout & 0xff;
57 clrbits_le32(®->irq_cause, KWSPI_SMEMRDIRQ);
58 writel(tmpdout, ®->dout); /* Write the data out */
59 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
63 * Wait for SPI transmit to get out
64 * or time out (1 second = 1000 ms)
65 * The NE event must be read and cleared first
67 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
68 if (readl(®->irq_cause) & KWSPI_SMEMRDIRQ) {
70 tmpdin = readl(®->din);
71 debug("spi_xfer: din %p..%08x read\n",
75 *((u8 *)din) = (u8)tmpdin;
85 if (tm >= KWSPI_TIMEOUT)
86 printf("*** spi_xfer: Time out during SPI transfer\n");
88 debug("loopend bitlen %d\n", bitlen);
91 if (flags & SPI_XFER_END)
92 _spi_cs_deactivate(reg);
99 static struct kwspi_registers *spireg =
100 (struct kwspi_registers *)MVEBU_SPI_BASE;
102 #ifdef CONFIG_KIRKWOOD
103 static u32 cs_spi_mpp_back[2];
106 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
107 unsigned int max_hz, unsigned int mode)
109 struct spi_slave *slave;
111 #ifdef CONFIG_KIRKWOOD
112 static const u32 kwspi_mpp_config[2][2] = {
113 { MPP0_SPI_SCn, 0 }, /* if cs == 0 */
114 { MPP7_SPI_SCn, 0 } /* if cs != 0 */
118 if (!spi_cs_is_valid(bus, cs))
121 slave = spi_alloc_slave_base(bus, cs);
125 writel(KWSPI_SMEMRDY, &spireg->ctrl);
127 /* calculate spi clock prescaller using max_hz */
128 data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10;
129 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
130 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
132 /* program spi clock prescaller using max_hz */
133 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
134 debug("data = 0x%08x\n", data);
136 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
137 writel(KWSPI_IRQMASK, &spireg->irq_mask);
139 #ifdef CONFIG_KIRKWOOD
140 /* program mpp registers to select SPI_CSn */
141 kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
147 void spi_free_slave(struct spi_slave *slave)
149 #ifdef CONFIG_KIRKWOOD
150 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
155 #if defined(CONFIG_SYS_KW_SPI_MPP)
156 u32 spi_mpp_backup[4];
159 __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
164 int spi_claim_bus(struct spi_slave *slave)
166 #if defined(CONFIG_SYS_KW_SPI_MPP)
168 u32 spi_mpp_config[4];
170 config = CONFIG_SYS_KW_SPI_MPP;
172 if (config & MOSI_MPP6)
173 spi_mpp_config[0] = MPP6_SPI_MOSI;
175 spi_mpp_config[0] = MPP1_SPI_MOSI;
177 if (config & SCK_MPP10)
178 spi_mpp_config[1] = MPP10_SPI_SCK;
180 spi_mpp_config[1] = MPP2_SPI_SCK;
182 if (config & MISO_MPP11)
183 spi_mpp_config[2] = MPP11_SPI_MISO;
185 spi_mpp_config[2] = MPP3_SPI_MISO;
187 spi_mpp_config[3] = 0;
188 spi_mpp_backup[3] = 0;
190 /* set new spi mpp and save current mpp config */
191 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
194 return board_spi_claim_bus(slave);
197 __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
201 void spi_release_bus(struct spi_slave *slave)
203 #if defined(CONFIG_SYS_KW_SPI_MPP)
204 kirkwood_mpp_conf(spi_mpp_backup, NULL);
207 board_spi_release_bus(slave);
210 #ifndef CONFIG_SPI_CS_IS_VALID
212 * you can define this function board specific
213 * define above CONFIG in board specific config file and
214 * provide the function in board specific src file
216 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
218 return bus == 0 && (cs == 0 || cs == 1);
226 void spi_cs_activate(struct spi_slave *slave)
228 _spi_cs_activate(spireg);
231 void spi_cs_deactivate(struct spi_slave *slave)
233 _spi_cs_deactivate(spireg);
236 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
237 const void *dout, void *din, unsigned long flags)
239 return _spi_xfer(spireg, bitlen, dout, din, flags);
244 /* Here now the DM part */
246 struct mvebu_spi_platdata {
247 struct kwspi_registers *spireg;
250 struct mvebu_spi_priv {
251 struct kwspi_registers *spireg;
254 static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
256 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
257 struct kwspi_registers *reg = plat->spireg;
260 /* calculate spi clock prescaller using max_hz */
261 data = ((CONFIG_SYS_TCLK / 2) / hz) + 0x10;
262 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
263 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
265 /* program spi clock prescaler using max_hz */
266 writel(KWSPI_ADRLEN_3BYTE | data, ®->cfg);
267 debug("data = 0x%08x\n", data);
272 static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
277 static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
278 const void *dout, void *din, unsigned long flags)
280 struct udevice *bus = dev->parent;
281 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
283 return _spi_xfer(plat->spireg, bitlen, dout, din, flags);
286 static int mvebu_spi_claim_bus(struct udevice *dev)
288 struct udevice *bus = dev->parent;
289 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
291 /* Configure the chip-select in the CTRL register */
292 clrsetbits_le32(&plat->spireg->ctrl,
293 KWSPI_CS_MASK << KWSPI_CS_SHIFT,
294 spi_chip_select(dev) << KWSPI_CS_SHIFT);
299 static int mvebu_spi_probe(struct udevice *bus)
301 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
302 struct kwspi_registers *reg = plat->spireg;
304 writel(KWSPI_SMEMRDY, ®->ctrl);
305 writel(KWSPI_SMEMRDIRQ, ®->irq_cause);
306 writel(KWSPI_IRQMASK, ®->irq_mask);
311 static int mvebu_spi_ofdata_to_platdata(struct udevice *bus)
313 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
315 plat->spireg = (struct kwspi_registers *)dev_get_addr(bus);
320 static const struct dm_spi_ops mvebu_spi_ops = {
321 .claim_bus = mvebu_spi_claim_bus,
322 .xfer = mvebu_spi_xfer,
323 .set_speed = mvebu_spi_set_speed,
324 .set_mode = mvebu_spi_set_mode,
326 * cs_info is not needed, since we require all chip selects to be
327 * in the device tree explicitly
331 static const struct udevice_id mvebu_spi_ids[] = {
332 { .compatible = "marvell,armada-375-spi" },
333 { .compatible = "marvell,armada-380-spi" },
334 { .compatible = "marvell,armada-xp-spi" },
338 U_BOOT_DRIVER(mvebu_spi) = {
341 .of_match = mvebu_spi_ids,
342 .ops = &mvebu_spi_ops,
343 .ofdata_to_platdata = mvebu_spi_ofdata_to_platdata,
344 .platdata_auto_alloc_size = sizeof(struct mvebu_spi_platdata),
345 .priv_auto_alloc_size = sizeof(struct mvebu_spi_priv),
346 .probe = mvebu_spi_probe,