blob: 32a26d40413e6a94b7dfc68a2bc6a68c6cb0a176 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese9ec1c782016-05-19 15:56:44 +02002/*
3 * Copyright (C) 2015 Marvell International Ltd.
4 *
5 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
Stefan Roese9ec1c782016-05-19 15:56:44 +02006 */
7
8#include <common.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Stefan Roese9ec1c782016-05-19 15:56:44 +020011#include <malloc.h>
12#include <spi.h>
Marek Behún0afd9342018-04-24 17:21:26 +020013#include <clk.h>
Stefan Roese9ec1c782016-05-19 15:56:44 +020014#include <wait_bit.h>
15#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
George Hilliarde2863802020-09-30 09:28:21 -050018#include <asm/gpio.h>
Stefan Roese9ec1c782016-05-19 15:56:44 +020019
20DECLARE_GLOBAL_DATA_PTR;
21
22#define MVEBU_SPI_A3700_XFER_RDY BIT(1)
23#define MVEBU_SPI_A3700_FIFO_FLUSH BIT(9)
24#define MVEBU_SPI_A3700_BYTE_LEN BIT(5)
25#define MVEBU_SPI_A3700_CLK_PHA BIT(6)
26#define MVEBU_SPI_A3700_CLK_POL BIT(7)
27#define MVEBU_SPI_A3700_FIFO_EN BIT(17)
28#define MVEBU_SPI_A3700_SPI_EN_0 BIT(16)
Marek Behún0afd9342018-04-24 17:21:26 +020029#define MVEBU_SPI_A3700_CLK_PRESCALE_MASK 0x1f
30
George Hilliarde2863802020-09-30 09:28:21 -050031#define MAX_CS_COUNT 4
Stefan Roese9ec1c782016-05-19 15:56:44 +020032
33/* SPI registers */
34struct spi_reg {
35 u32 ctrl; /* 0x10600 */
36 u32 cfg; /* 0x10604 */
37 u32 dout; /* 0x10608 */
38 u32 din; /* 0x1060c */
39};
40
41struct mvebu_spi_platdata {
42 struct spi_reg *spireg;
Marek Behún0afd9342018-04-24 17:21:26 +020043 struct clk clk;
George Hilliarde2863802020-09-30 09:28:21 -050044 struct gpio_desc cs_gpios[MAX_CS_COUNT];
Stefan Roese9ec1c782016-05-19 15:56:44 +020045};
46
George Hilliarde2863802020-09-30 09:28:21 -050047static void spi_cs_activate(struct mvebu_spi_platdata *plat, int cs)
Stefan Roese9ec1c782016-05-19 15:56:44 +020048{
George Hilliarde2863802020-09-30 09:28:21 -050049 if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs]))
50 dm_gpio_set_value(&plat->cs_gpios[cs], 1);
51 else
52 setbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
Stefan Roese9ec1c782016-05-19 15:56:44 +020053}
54
George Hilliarde2863802020-09-30 09:28:21 -050055static void spi_cs_deactivate(struct mvebu_spi_platdata *plat, int cs)
Stefan Roese9ec1c782016-05-19 15:56:44 +020056{
George Hilliarde2863802020-09-30 09:28:21 -050057 if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs]))
58 dm_gpio_set_value(&plat->cs_gpios[cs], 0);
59 else
60 clrbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
Stefan Roese9ec1c782016-05-19 15:56:44 +020061}
62
63/**
64 * spi_legacy_shift_byte() - triggers the real SPI transfer
65 * @bytelen: Indicate how many bytes to transfer.
66 * @dout: Buffer address of what to send.
67 * @din: Buffer address of where to receive.
68 *
69 * This function triggers the real SPI transfer in legacy mode. It
70 * will shift out char buffer from @dout, and shift in char buffer to
71 * @din, if necessary.
72 *
73 * This function assumes that only one byte is shifted at one time.
74 * However, it is not its responisbility to set the transfer type to
75 * one-byte. Also, it does not guarantee that it will work if transfer
76 * type becomes two-byte. See spi_set_legacy() for details.
77 *
78 * In legacy mode, simply write to the SPI_DOUT register will trigger
79 * the transfer.
80 *
81 * If @dout == NULL, which means no actual data needs to be sent out,
82 * then the function will shift out 0x00 in order to shift in data.
83 * The XFER_RDY flag is checked every time before accessing SPI_DOUT
84 * and SPI_DIN register.
85 *
86 * The number of transfers to be triggerred is decided by @bytelen.
87 *
88 * Return: 0 - cool
89 * -ETIMEDOUT - XFER_RDY flag timeout
90 */
91static int spi_legacy_shift_byte(struct spi_reg *reg, unsigned int bytelen,
92 const void *dout, void *din)
93{
94 const u8 *dout_8;
95 u8 *din_8;
96 int ret;
97
98 /* Use 0x00 as dummy dout */
99 const u8 dummy_dout = 0x0;
100 u32 pending_dout = 0x0;
101
102 /* dout_8: pointer of current dout */
103 dout_8 = dout;
104 /* din_8: pointer of current din */
105 din_8 = din;
106
107 while (bytelen) {
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100108 ret = wait_for_bit_le32(&reg->ctrl,
109 MVEBU_SPI_A3700_XFER_RDY,
110 true,100, false);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200111 if (ret)
112 return ret;
113
114 if (dout)
115 pending_dout = (u32)*dout_8;
116 else
117 pending_dout = (u32)dummy_dout;
118
119 /* Trigger the xfer */
120 writel(pending_dout, &reg->dout);
121
122 if (din) {
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100123 ret = wait_for_bit_le32(&reg->ctrl,
124 MVEBU_SPI_A3700_XFER_RDY,
125 true, 100, false);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200126 if (ret)
127 return ret;
128
129 /* Read what is transferred in */
130 *din_8 = (u8)readl(&reg->din);
131 }
132
133 /* Don't increment the current pointer if NULL */
134 if (dout)
135 dout_8++;
136 if (din)
137 din_8++;
138
139 bytelen--;
140 }
141
142 return 0;
143}
144
145static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
146 const void *dout, void *din, unsigned long flags)
147{
148 struct udevice *bus = dev->parent;
Simon Glassfa20e932020-12-03 16:55:20 -0700149 struct mvebu_spi_platdata *plat = dev_get_plat(bus);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200150 struct spi_reg *reg = plat->spireg;
151 unsigned int bytelen;
152 int ret;
153
154 bytelen = bitlen / 8;
155
156 if (dout && din)
157 debug("This is a duplex transfer.\n");
158
159 /* Activate CS */
160 if (flags & SPI_XFER_BEGIN) {
161 debug("SPI: activate cs.\n");
George Hilliarde2863802020-09-30 09:28:21 -0500162 spi_cs_activate(plat, spi_chip_select(dev));
Stefan Roese9ec1c782016-05-19 15:56:44 +0200163 }
164
165 /* Send and/or receive */
166 if (dout || din) {
167 ret = spi_legacy_shift_byte(reg, bytelen, dout, din);
168 if (ret)
169 return ret;
170 }
171
172 /* Deactivate CS */
173 if (flags & SPI_XFER_END) {
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100174 ret = wait_for_bit_le32(&reg->ctrl,
175 MVEBU_SPI_A3700_XFER_RDY,
176 true, 100, false);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200177 if (ret)
178 return ret;
179
180 debug("SPI: deactivate cs.\n");
George Hilliarde2863802020-09-30 09:28:21 -0500181 spi_cs_deactivate(plat, spi_chip_select(dev));
Stefan Roese9ec1c782016-05-19 15:56:44 +0200182 }
183
184 return 0;
185}
186
187static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
188{
Simon Glassfa20e932020-12-03 16:55:20 -0700189 struct mvebu_spi_platdata *plat = dev_get_plat(bus);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200190 struct spi_reg *reg = plat->spireg;
Marek Behún0afd9342018-04-24 17:21:26 +0200191 u32 data, prescale;
Stefan Roese9ec1c782016-05-19 15:56:44 +0200192
193 data = readl(&reg->cfg);
194
Marek Behún0afd9342018-04-24 17:21:26 +0200195 prescale = DIV_ROUND_UP(clk_get_rate(&plat->clk), hz);
Marek Behúna7926162019-07-23 16:49:32 +0200196 if (prescale > 0xf)
Marek Behún0afd9342018-04-24 17:21:26 +0200197 prescale = 0x10 + (prescale + 1) / 2;
Marek Behúna7926162019-07-23 16:49:32 +0200198 prescale = min(prescale, 0x1fu);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200199
Marek Behún0afd9342018-04-24 17:21:26 +0200200 data &= ~MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
201 data |= prescale & MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
Stefan Roese9ec1c782016-05-19 15:56:44 +0200202
203 writel(data, &reg->cfg);
204
205 return 0;
206}
207
208static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
209{
Simon Glassfa20e932020-12-03 16:55:20 -0700210 struct mvebu_spi_platdata *plat = dev_get_plat(bus);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200211 struct spi_reg *reg = plat->spireg;
212
213 /*
214 * Set SPI polarity
215 * 0: Serial interface clock is low when inactive
216 * 1: Serial interface clock is high when inactive
217 */
218 if (mode & SPI_CPOL)
219 setbits_le32(&reg->cfg, MVEBU_SPI_A3700_CLK_POL);
220 else
221 clrbits_le32(&reg->cfg, MVEBU_SPI_A3700_CLK_POL);
222 if (mode & SPI_CPHA)
223 setbits_le32(&reg->cfg, MVEBU_SPI_A3700_CLK_PHA);
224 else
225 clrbits_le32(&reg->cfg, MVEBU_SPI_A3700_CLK_PHA);
226
227 return 0;
228}
229
230static int mvebu_spi_probe(struct udevice *bus)
231{
Simon Glassfa20e932020-12-03 16:55:20 -0700232 struct mvebu_spi_platdata *plat = dev_get_plat(bus);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200233 struct spi_reg *reg = plat->spireg;
234 u32 data;
235 int ret;
236
237 /*
238 * Settings SPI controller to be working in legacy mode, which
239 * means use only DO pin (I/O 1) for Data Out, and DI pin (I/O 0)
240 * for Data In.
241 */
242
243 /* Flush read/write FIFO */
244 data = readl(&reg->cfg);
245 writel(data | MVEBU_SPI_A3700_FIFO_FLUSH, &reg->cfg);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100246 ret = wait_for_bit_le32(&reg->cfg, MVEBU_SPI_A3700_FIFO_FLUSH,
247 false, 1000, false);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200248 if (ret)
249 return ret;
250
251 /* Disable FIFO mode */
252 data &= ~MVEBU_SPI_A3700_FIFO_EN;
253
254 /* Always shift 1 byte at a time */
255 data &= ~MVEBU_SPI_A3700_BYTE_LEN;
256
257 writel(data, &reg->cfg);
258
George Hilliarde2863802020-09-30 09:28:21 -0500259 /* Set up CS GPIOs in device tree, if any */
260 if (CONFIG_IS_ENABLED(DM_GPIO) && gpio_get_list_count(bus, "cs-gpios") > 0) {
261 int i;
262
263 for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) {
264 ret = gpio_request_by_name(bus, "cs-gpios", i, &plat->cs_gpios[i], 0);
265 if (ret < 0 || !dm_gpio_is_valid(&plat->cs_gpios[i])) {
266 /* Use the native CS function for this line */
267 continue;
268 }
269
270 ret = dm_gpio_set_dir_flags(&plat->cs_gpios[i],
271 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
272 if (ret) {
273 dev_err(bus, "Setting cs %d error\n", i);
274 return ret;
275 }
276 }
277 }
278
Stefan Roese9ec1c782016-05-19 15:56:44 +0200279 return 0;
280}
281
Simon Glassaad29ae2020-12-03 16:55:21 -0700282static int mvebu_spi_of_to_plat(struct udevice *bus)
Stefan Roese9ec1c782016-05-19 15:56:44 +0200283{
Simon Glassfa20e932020-12-03 16:55:20 -0700284 struct mvebu_spi_platdata *plat = dev_get_plat(bus);
Marek Behún0afd9342018-04-24 17:21:26 +0200285 int ret;
Stefan Roese9ec1c782016-05-19 15:56:44 +0200286
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900287 plat->spireg = dev_read_addr_ptr(bus);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200288
Marek Behún0afd9342018-04-24 17:21:26 +0200289 ret = clk_get_by_index(bus, 0, &plat->clk);
290 if (ret) {
291 dev_err(bus, "cannot get clock\n");
292 return ret;
293 }
294
295 return 0;
296}
297
298static int mvebu_spi_remove(struct udevice *bus)
299{
Simon Glassfa20e932020-12-03 16:55:20 -0700300 struct mvebu_spi_platdata *plat = dev_get_plat(bus);
Marek Behún0afd9342018-04-24 17:21:26 +0200301
302 clk_free(&plat->clk);
Stefan Roese9ec1c782016-05-19 15:56:44 +0200303
304 return 0;
305}
306
307static const struct dm_spi_ops mvebu_spi_ops = {
308 .xfer = mvebu_spi_xfer,
309 .set_speed = mvebu_spi_set_speed,
310 .set_mode = mvebu_spi_set_mode,
311 /*
312 * cs_info is not needed, since we require all chip selects to be
313 * in the device tree explicitly
314 */
315};
316
317static const struct udevice_id mvebu_spi_ids[] = {
318 { .compatible = "marvell,armada-3700-spi" },
319 { }
320};
321
322U_BOOT_DRIVER(mvebu_spi) = {
323 .name = "mvebu_spi",
324 .id = UCLASS_SPI,
325 .of_match = mvebu_spi_ids,
326 .ops = &mvebu_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700327 .of_to_plat = mvebu_spi_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700328 .plat_auto = sizeof(struct mvebu_spi_platdata),
Stefan Roese9ec1c782016-05-19 15:56:44 +0200329 .probe = mvebu_spi_probe,
Marek Behún0afd9342018-04-24 17:21:26 +0200330 .remove = mvebu_spi_remove,
Stefan Roese9ec1c782016-05-19 15:56:44 +0200331};