1 // SPDX-License-Identifier: GPL-2.0+
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
7 * Derived from drivers/spi/mpc8xxx_spi.c
16 #include <asm/arch/soc.h>
17 #ifdef CONFIG_ARCH_KIRKWOOD
18 #include <asm/arch/mpp.h>
20 #include <asm/arch-mvebu/spi.h>
22 struct mvebu_spi_dev {
23 bool is_errata_50mhz_ac;
26 struct mvebu_spi_platdata {
27 struct kwspi_registers *spireg;
28 bool is_errata_50mhz_ac;
31 struct mvebu_spi_priv {
32 struct kwspi_registers *spireg;
35 static void _spi_cs_activate(struct kwspi_registers *reg)
37 setbits_le32(®->ctrl, KWSPI_CSN_ACT);
40 static void _spi_cs_deactivate(struct kwspi_registers *reg)
42 clrbits_le32(®->ctrl, KWSPI_CSN_ACT);
45 static int _spi_xfer(struct kwspi_registers *reg, unsigned int bitlen,
46 const void *dout, void *din, unsigned long flags)
48 unsigned int tmpdout, tmpdin;
51 debug("spi_xfer: dout %p din %p bitlen %u\n", dout, din, bitlen);
53 if (flags & SPI_XFER_BEGIN)
54 _spi_cs_activate(reg);
57 * handle data in 8-bit chunks
58 * TBD: 2byte xfer mode to be enabled
60 clrsetbits_le32(®->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE);
63 debug("loopstart bitlen %d\n", bitlen);
66 /* Shift data so it's msb-justified */
68 tmpdout = *(u32 *)dout & 0xff;
70 clrbits_le32(®->irq_cause, KWSPI_SMEMRDIRQ);
71 writel(tmpdout, ®->dout); /* Write the data out */
72 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
76 * Wait for SPI transmit to get out
77 * or time out (1 second = 1000 ms)
78 * The NE event must be read and cleared first
80 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
81 if (readl(®->irq_cause) & KWSPI_SMEMRDIRQ) {
83 tmpdin = readl(®->din);
84 debug("spi_xfer: din %p..%08x read\n",
88 *((u8 *)din) = (u8)tmpdin;
98 if (tm >= KWSPI_TIMEOUT)
99 printf("*** spi_xfer: Time out during SPI transfer\n");
101 debug("loopend bitlen %d\n", bitlen);
104 if (flags & SPI_XFER_END)
105 _spi_cs_deactivate(reg);
110 static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
112 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
113 struct kwspi_registers *reg = plat->spireg;
116 /* calculate spi clock prescaller using max_hz */
117 data = ((CONFIG_SYS_TCLK / 2) / hz) + 0x10;
118 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
119 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
121 /* program spi clock prescaler using max_hz */
122 writel(KWSPI_ADRLEN_3BYTE | data, ®->cfg);
123 debug("data = 0x%08x\n", data);
128 static void mvebu_spi_50mhz_ac_timing_erratum(struct udevice *bus, uint mode)
130 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
131 struct kwspi_registers *reg = plat->spireg;
135 * Erratum description: (Erratum NO. FE-9144572) The device
136 * SPI interface supports frequencies of up to 50 MHz.
137 * However, due to this erratum, when the device core clock is
138 * 250 MHz and the SPI interfaces is configured for 50MHz SPI
139 * clock and CPOL=CPHA=1 there might occur data corruption on
140 * reads from the SPI device.
141 * Erratum Workaround:
142 * Work in one of the following configurations:
143 * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
145 * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
146 * Register" before setting the interface.
148 data = readl(®->timing1);
149 data &= ~KW_SPI_TMISO_SAMPLE_MASK;
151 if (CONFIG_SYS_TCLK == 250000000 &&
154 data |= KW_SPI_TMISO_SAMPLE_2;
156 data |= KW_SPI_TMISO_SAMPLE_1;
158 writel(data, ®->timing1);
161 static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
163 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
164 struct kwspi_registers *reg = plat->spireg;
165 u32 data = readl(®->cfg);
167 data &= ~(KWSPI_CPHA | KWSPI_CPOL | KWSPI_RXLSBF | KWSPI_TXLSBF);
173 if (mode & SPI_LSB_FIRST)
174 data |= (KWSPI_RXLSBF | KWSPI_TXLSBF);
176 writel(data, ®->cfg);
178 if (plat->is_errata_50mhz_ac)
179 mvebu_spi_50mhz_ac_timing_erratum(bus, mode);
184 static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
185 const void *dout, void *din, unsigned long flags)
187 struct udevice *bus = dev->parent;
188 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
190 return _spi_xfer(plat->spireg, bitlen, dout, din, flags);
193 __attribute__((weak)) int mvebu_board_spi_claim_bus(struct udevice *dev)
198 static int mvebu_spi_claim_bus(struct udevice *dev)
200 struct udevice *bus = dev->parent;
201 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
203 /* Configure the chip-select in the CTRL register */
204 clrsetbits_le32(&plat->spireg->ctrl,
205 KWSPI_CS_MASK << KWSPI_CS_SHIFT,
206 spi_chip_select(dev) << KWSPI_CS_SHIFT);
208 return mvebu_board_spi_claim_bus(dev);
211 __attribute__((weak)) int mvebu_board_spi_release_bus(struct udevice *dev)
216 static int mvebu_spi_release_bus(struct udevice *dev)
218 return mvebu_board_spi_release_bus(dev);
221 static int mvebu_spi_probe(struct udevice *bus)
223 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
224 struct kwspi_registers *reg = plat->spireg;
226 writel(KWSPI_SMEMRDY, ®->ctrl);
227 writel(KWSPI_SMEMRDIRQ, ®->irq_cause);
228 writel(KWSPI_IRQMASK, ®->irq_mask);
233 static int mvebu_spi_ofdata_to_platdata(struct udevice *bus)
235 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
236 const struct mvebu_spi_dev *drvdata =
237 (struct mvebu_spi_dev *)dev_get_driver_data(bus);
239 plat->spireg = dev_read_addr_ptr(bus);
240 plat->is_errata_50mhz_ac = drvdata->is_errata_50mhz_ac;
245 static const struct dm_spi_ops mvebu_spi_ops = {
246 .claim_bus = mvebu_spi_claim_bus,
247 .release_bus = mvebu_spi_release_bus,
248 .xfer = mvebu_spi_xfer,
249 .set_speed = mvebu_spi_set_speed,
250 .set_mode = mvebu_spi_set_mode,
252 * cs_info is not needed, since we require all chip selects to be
253 * in the device tree explicitly
257 static const struct mvebu_spi_dev armada_spi_dev_data = {
258 .is_errata_50mhz_ac = false,
261 static const struct mvebu_spi_dev armada_xp_spi_dev_data = {
262 .is_errata_50mhz_ac = false,
265 static const struct mvebu_spi_dev armada_375_spi_dev_data = {
266 .is_errata_50mhz_ac = false,
269 static const struct mvebu_spi_dev armada_380_spi_dev_data = {
270 .is_errata_50mhz_ac = true,
273 static const struct udevice_id mvebu_spi_ids[] = {
275 .compatible = "marvell,orion-spi",
276 .data = (ulong)&armada_spi_dev_data,
279 .compatible = "marvell,armada-375-spi",
280 .data = (ulong)&armada_375_spi_dev_data
283 .compatible = "marvell,armada-380-spi",
284 .data = (ulong)&armada_380_spi_dev_data
287 .compatible = "marvell,armada-xp-spi",
288 .data = (ulong)&armada_xp_spi_dev_data
293 U_BOOT_DRIVER(mvebu_spi) = {
296 .of_match = mvebu_spi_ids,
297 .ops = &mvebu_spi_ops,
298 .ofdata_to_platdata = mvebu_spi_ofdata_to_platdata,
299 .platdata_auto_alloc_size = sizeof(struct mvebu_spi_platdata),
300 .priv_auto_alloc_size = sizeof(struct mvebu_spi_priv),
301 .probe = mvebu_spi_probe,