blob: 3dc62f351a06617eefbe8d56a7caf7cbee4469bf [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar5710de42009-05-30 01:13:33 +05302/*
3 * (C) Copyright 2009
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 *
7 * Derived from drivers/spi/mpc8xxx_spi.c
Prafulla Wadaskar5710de42009-05-30 01:13:33 +05308 */
9
10#include <common.h>
Stefan Roese9985bdb2015-11-20 13:39:43 +010011#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053013#include <malloc.h>
14#include <spi.h>
Lei Wena7efd712011-10-18 20:11:42 +053015#include <asm/io.h>
Stefan Roese3dc23f72014-10-22 12:13:06 +020016#include <asm/arch/soc.h>
Trevor Woernerbb0fb4c2020-05-06 08:02:40 -040017#ifdef CONFIG_ARCH_KIRKWOOD
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053018#include <asm/arch/mpp.h>
Stefan Roese4aceea22014-10-22 12:13:10 +020019#endif
Stefan Roese3e972cb2014-10-22 12:13:07 +020020#include <asm/arch-mvebu/spi.h>
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053021
Bhargav Shaha58c7ff2020-06-18 23:15:13 +053022struct mvebu_spi_dev {
23 bool is_errata_50mhz_ac;
24};
25
Simon Glass8a8d24b2020-12-03 16:55:23 -070026struct mvebu_spi_plat {
Bhargav Shaha58c7ff2020-06-18 23:15:13 +053027 struct kwspi_registers *spireg;
28 bool is_errata_50mhz_ac;
29};
30
31struct mvebu_spi_priv {
32 struct kwspi_registers *spireg;
33};
34
Stefan Roese9985bdb2015-11-20 13:39:43 +010035static void _spi_cs_activate(struct kwspi_registers *reg)
36{
37 setbits_le32(&reg->ctrl, KWSPI_CSN_ACT);
38}
39
40static void _spi_cs_deactivate(struct kwspi_registers *reg)
41{
42 clrbits_le32(&reg->ctrl, KWSPI_CSN_ACT);
43}
44
45static int _spi_xfer(struct kwspi_registers *reg, unsigned int bitlen,
46 const void *dout, void *din, unsigned long flags)
47{
48 unsigned int tmpdout, tmpdin;
49 int tm, isread = 0;
50
51 debug("spi_xfer: dout %p din %p bitlen %u\n", dout, din, bitlen);
52
53 if (flags & SPI_XFER_BEGIN)
54 _spi_cs_activate(reg);
55
56 /*
57 * handle data in 8-bit chunks
58 * TBD: 2byte xfer mode to be enabled
59 */
60 clrsetbits_le32(&reg->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE);
61
62 while (bitlen > 4) {
63 debug("loopstart bitlen %d\n", bitlen);
64 tmpdout = 0;
65
66 /* Shift data so it's msb-justified */
67 if (dout)
68 tmpdout = *(u32 *)dout & 0xff;
69
70 clrbits_le32(&reg->irq_cause, KWSPI_SMEMRDIRQ);
71 writel(tmpdout, &reg->dout); /* Write the data out */
72 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
73 tmpdout, bitlen);
74
75 /*
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
79 */
80 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
81 if (readl(&reg->irq_cause) & KWSPI_SMEMRDIRQ) {
82 isread = 1;
83 tmpdin = readl(&reg->din);
84 debug("spi_xfer: din %p..%08x read\n",
85 din, tmpdin);
86
87 if (din) {
88 *((u8 *)din) = (u8)tmpdin;
89 din += 1;
90 }
91 if (dout)
92 dout += 1;
93 bitlen -= 8;
94 }
95 if (isread)
96 break;
97 }
98 if (tm >= KWSPI_TIMEOUT)
99 printf("*** spi_xfer: Time out during SPI transfer\n");
100
101 debug("loopend bitlen %d\n", bitlen);
102 }
103
104 if (flags & SPI_XFER_END)
105 _spi_cs_deactivate(reg);
106
107 return 0;
108}
109
Stefan Roese9985bdb2015-11-20 13:39:43 +0100110static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
111{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700112 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Stefan Roese9985bdb2015-11-20 13:39:43 +0100113 struct kwspi_registers *reg = plat->spireg;
Ken Ma037818c2021-04-30 15:26:29 +0200114 u32 data, divider;
115 unsigned int spr, sppr;
Stefan Roese9985bdb2015-11-20 13:39:43 +0100116
Ken Ma037818c2021-04-30 15:26:29 +0200117 /*
118 * Calculate spi clock prescaller using max_hz.
119 * SPPR is SPI Baud Rate Pre-selection, it holds bits 5 and 7:6 in
120 * SPI Interface Configuration Register;
121 * SPR is SPI Baud Rate Selection, it holds bits 3:0 in SPI Interface
122 * Configuration Register.
123 * The SPR together with the SPPR define the SPI CLK frequency as
124 * follows:
125 * SPI actual frequency = core_clk / (SPR * (2 ^ SPPR))
126 */
127 divider = DIV_ROUND_UP(CONFIG_SYS_TCLK, hz);
128 if (divider < 16) {
129 /* This is the easy case, divider is less than 16 */
130 spr = divider;
131 sppr = 0;
132
133 } else {
134 unsigned int two_pow_sppr;
135 /*
136 * Find the highest bit set in divider. This and the
137 * three next bits define SPR (apart from rounding).
138 * SPPR is then the number of zero bits that must be
139 * appended:
140 */
141 sppr = fls(divider) - 4;
142
143 /*
144 * As SPR only has 4 bits, we have to round divider up
145 * to the next multiple of 2 ** sppr.
146 */
147 two_pow_sppr = 1 << sppr;
148 divider = (divider + two_pow_sppr - 1) & -two_pow_sppr;
149
150 /*
151 * recalculate sppr as rounding up divider might have
152 * increased it enough to change the position of the
153 * highest set bit. In this case the bit that now
154 * doesn't make it into SPR is 0, so there is no need to
155 * round again.
156 */
157 sppr = fls(divider) - 4;
158 spr = divider >> sppr;
159
160 /*
161 * Now do range checking. SPR is constructed to have a
162 * width of 4 bits, so this is fine for sure. So we
163 * still need to check for sppr to fit into 3 bits:
164 */
165 if (sppr > 7)
166 return -EINVAL;
167 }
168
169 data = ((sppr & 0x6) << 5) | ((sppr & 0x1) << 4) | spr;
Stefan Roese9985bdb2015-11-20 13:39:43 +0100170
171 /* program spi clock prescaler using max_hz */
172 writel(KWSPI_ADRLEN_3BYTE | data, &reg->cfg);
173 debug("data = 0x%08x\n", data);
174
175 return 0;
176}
177
Chris Packhamdf168812018-01-22 22:44:20 +1300178static void mvebu_spi_50mhz_ac_timing_erratum(struct udevice *bus, uint mode)
179{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700180 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Chris Packhamdf168812018-01-22 22:44:20 +1300181 struct kwspi_registers *reg = plat->spireg;
182 u32 data;
183
184 /*
185 * Erratum description: (Erratum NO. FE-9144572) The device
186 * SPI interface supports frequencies of up to 50 MHz.
187 * However, due to this erratum, when the device core clock is
188 * 250 MHz and the SPI interfaces is configured for 50MHz SPI
189 * clock and CPOL=CPHA=1 there might occur data corruption on
190 * reads from the SPI device.
191 * Erratum Workaround:
192 * Work in one of the following configurations:
193 * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
194 * Register".
195 * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
196 * Register" before setting the interface.
197 */
198 data = readl(&reg->timing1);
199 data &= ~KW_SPI_TMISO_SAMPLE_MASK;
200
201 if (CONFIG_SYS_TCLK == 250000000 &&
202 mode & SPI_CPOL &&
203 mode & SPI_CPHA)
204 data |= KW_SPI_TMISO_SAMPLE_2;
205 else
206 data |= KW_SPI_TMISO_SAMPLE_1;
207
208 writel(data, &reg->timing1);
209}
210
Stefan Roese9985bdb2015-11-20 13:39:43 +0100211static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
212{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700213 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Chris Packhamebfa18c2016-10-27 21:16:05 +1300214 struct kwspi_registers *reg = plat->spireg;
215 u32 data = readl(&reg->cfg);
216
217 data &= ~(KWSPI_CPHA | KWSPI_CPOL | KWSPI_RXLSBF | KWSPI_TXLSBF);
218
219 if (mode & SPI_CPHA)
220 data |= KWSPI_CPHA;
221 if (mode & SPI_CPOL)
222 data |= KWSPI_CPOL;
223 if (mode & SPI_LSB_FIRST)
224 data |= (KWSPI_RXLSBF | KWSPI_TXLSBF);
225
226 writel(data, &reg->cfg);
227
Jagan Tekif5ff46f2018-03-15 17:03:22 +0530228 if (plat->is_errata_50mhz_ac)
Chris Packhamdf168812018-01-22 22:44:20 +1300229 mvebu_spi_50mhz_ac_timing_erratum(bus, mode);
230
Stefan Roese9985bdb2015-11-20 13:39:43 +0100231 return 0;
232}
233
234static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
235 const void *dout, void *din, unsigned long flags)
236{
237 struct udevice *bus = dev->parent;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700238 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Stefan Roese9985bdb2015-11-20 13:39:43 +0100239
240 return _spi_xfer(plat->spireg, bitlen, dout, din, flags);
241}
242
Pascal Linderf1696532019-06-18 08:41:01 +0200243__attribute__((weak)) int mvebu_board_spi_claim_bus(struct udevice *dev)
244{
245 return 0;
246}
247
Stefan Roese9fc56632016-02-11 11:37:38 +0100248static int mvebu_spi_claim_bus(struct udevice *dev)
249{
250 struct udevice *bus = dev->parent;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700251 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Stefan Roese9fc56632016-02-11 11:37:38 +0100252
253 /* Configure the chip-select in the CTRL register */
254 clrsetbits_le32(&plat->spireg->ctrl,
255 KWSPI_CS_MASK << KWSPI_CS_SHIFT,
256 spi_chip_select(dev) << KWSPI_CS_SHIFT);
257
Pascal Linderf1696532019-06-18 08:41:01 +0200258 return mvebu_board_spi_claim_bus(dev);
259}
260
261__attribute__((weak)) int mvebu_board_spi_release_bus(struct udevice *dev)
262{
Stefan Roese9fc56632016-02-11 11:37:38 +0100263 return 0;
264}
265
Pascal Linderf1696532019-06-18 08:41:01 +0200266static int mvebu_spi_release_bus(struct udevice *dev)
267{
268 return mvebu_board_spi_release_bus(dev);
269}
270
Stefan Roese9985bdb2015-11-20 13:39:43 +0100271static int mvebu_spi_probe(struct udevice *bus)
272{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700273 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Stefan Roese9985bdb2015-11-20 13:39:43 +0100274 struct kwspi_registers *reg = plat->spireg;
275
276 writel(KWSPI_SMEMRDY, &reg->ctrl);
277 writel(KWSPI_SMEMRDIRQ, &reg->irq_cause);
278 writel(KWSPI_IRQMASK, &reg->irq_mask);
279
280 return 0;
281}
282
Simon Glassd1998a92020-12-03 16:55:21 -0700283static int mvebu_spi_of_to_plat(struct udevice *bus)
Stefan Roese9985bdb2015-11-20 13:39:43 +0100284{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700285 struct mvebu_spi_plat *plat = dev_get_plat(bus);
Jagan Tekif5ff46f2018-03-15 17:03:22 +0530286 const struct mvebu_spi_dev *drvdata =
287 (struct mvebu_spi_dev *)dev_get_driver_data(bus);
Stefan Roese9985bdb2015-11-20 13:39:43 +0100288
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900289 plat->spireg = dev_read_addr_ptr(bus);
Jagan Tekif5ff46f2018-03-15 17:03:22 +0530290 plat->is_errata_50mhz_ac = drvdata->is_errata_50mhz_ac;
Stefan Roese9985bdb2015-11-20 13:39:43 +0100291
292 return 0;
293}
294
295static const struct dm_spi_ops mvebu_spi_ops = {
Stefan Roese9fc56632016-02-11 11:37:38 +0100296 .claim_bus = mvebu_spi_claim_bus,
Pascal Linderf1696532019-06-18 08:41:01 +0200297 .release_bus = mvebu_spi_release_bus,
Stefan Roese9985bdb2015-11-20 13:39:43 +0100298 .xfer = mvebu_spi_xfer,
299 .set_speed = mvebu_spi_set_speed,
300 .set_mode = mvebu_spi_set_mode,
301 /*
302 * cs_info is not needed, since we require all chip selects to be
303 * in the device tree explicitly
304 */
305};
306
Chris Packham4f4dde02018-08-01 12:19:26 +0530307static const struct mvebu_spi_dev armada_spi_dev_data = {
308 .is_errata_50mhz_ac = false,
309};
310
Chris Packhamdf168812018-01-22 22:44:20 +1300311static const struct mvebu_spi_dev armada_xp_spi_dev_data = {
312 .is_errata_50mhz_ac = false,
313};
314
315static const struct mvebu_spi_dev armada_375_spi_dev_data = {
316 .is_errata_50mhz_ac = false,
317};
318
319static const struct mvebu_spi_dev armada_380_spi_dev_data = {
320 .is_errata_50mhz_ac = true,
321};
322
Stefan Roese9985bdb2015-11-20 13:39:43 +0100323static const struct udevice_id mvebu_spi_ids[] = {
Chris Packhamdf168812018-01-22 22:44:20 +1300324 {
Chris Packham4f4dde02018-08-01 12:19:26 +0530325 .compatible = "marvell,orion-spi",
326 .data = (ulong)&armada_spi_dev_data,
327 },
328 {
Chris Packhamdf168812018-01-22 22:44:20 +1300329 .compatible = "marvell,armada-375-spi",
330 .data = (ulong)&armada_375_spi_dev_data
331 },
332 {
333 .compatible = "marvell,armada-380-spi",
334 .data = (ulong)&armada_380_spi_dev_data
335 },
336 {
337 .compatible = "marvell,armada-xp-spi",
338 .data = (ulong)&armada_xp_spi_dev_data
339 },
Stefan Roese9985bdb2015-11-20 13:39:43 +0100340 { }
341};
342
343U_BOOT_DRIVER(mvebu_spi) = {
344 .name = "mvebu_spi",
345 .id = UCLASS_SPI,
346 .of_match = mvebu_spi_ids,
347 .ops = &mvebu_spi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700348 .of_to_plat = mvebu_spi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700349 .plat_auto = sizeof(struct mvebu_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700350 .priv_auto = sizeof(struct mvebu_spi_priv),
Stefan Roese9985bdb2015-11-20 13:39:43 +0100351 .probe = mvebu_spi_probe,
352};