1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Marvell International Ltd.
5 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
15 #include <asm/global_data.h>
17 #include <dm/device_compat.h>
18 #include <linux/bitops.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #define MVEBU_SPI_A3700_XFER_RDY BIT(1)
24 #define MVEBU_SPI_A3700_FIFO_FLUSH BIT(9)
25 #define MVEBU_SPI_A3700_BYTE_LEN BIT(5)
26 #define MVEBU_SPI_A3700_CLK_PHA BIT(6)
27 #define MVEBU_SPI_A3700_CLK_POL BIT(7)
28 #define MVEBU_SPI_A3700_FIFO_EN BIT(17)
29 #define MVEBU_SPI_A3700_SPI_EN_0 BIT(16)
30 #define MVEBU_SPI_A3700_CLK_PRESCALE_MASK 0x1f
32 #define MAX_CS_COUNT 4
36 u32 ctrl; /* 0x10600 */
37 u32 cfg; /* 0x10604 */
38 u32 dout; /* 0x10608 */
39 u32 din; /* 0x1060c */
42 struct mvebu_spi_plat {
43 struct spi_reg *spireg;
45 struct gpio_desc cs_gpios[MAX_CS_COUNT];
48 static void spi_cs_activate(struct mvebu_spi_plat *plat, int cs)
50 if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs]))
51 dm_gpio_set_value(&plat->cs_gpios[cs], 1);
53 setbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
56 static void spi_cs_deactivate(struct mvebu_spi_plat *plat, int cs)
58 if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs]))
59 dm_gpio_set_value(&plat->cs_gpios[cs], 0);
61 clrbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
65 * spi_legacy_shift_byte() - triggers the real SPI transfer
66 * @bytelen: Indicate how many bytes to transfer.
67 * @dout: Buffer address of what to send.
68 * @din: Buffer address of where to receive.
70 * This function triggers the real SPI transfer in legacy mode. It
71 * will shift out char buffer from @dout, and shift in char buffer to
74 * This function assumes that only one byte is shifted at one time.
75 * However, it is not its responisbility to set the transfer type to
76 * one-byte. Also, it does not guarantee that it will work if transfer
77 * type becomes two-byte. See spi_set_legacy() for details.
79 * In legacy mode, simply write to the SPI_DOUT register will trigger
82 * If @dout == NULL, which means no actual data needs to be sent out,
83 * then the function will shift out 0x00 in order to shift in data.
84 * The XFER_RDY flag is checked every time before accessing SPI_DOUT
85 * and SPI_DIN register.
87 * The number of transfers to be triggerred is decided by @bytelen.
90 * -ETIMEDOUT - XFER_RDY flag timeout
92 static int spi_legacy_shift_byte(struct spi_reg *reg, unsigned int bytelen,
93 const void *dout, void *din)
99 /* Use 0x00 as dummy dout */
100 const u8 dummy_dout = 0x0;
101 u32 pending_dout = 0x0;
103 /* dout_8: pointer of current dout */
105 /* din_8: pointer of current din */
109 ret = wait_for_bit_le32(®->ctrl,
110 MVEBU_SPI_A3700_XFER_RDY,
116 pending_dout = (u32)*dout_8;
118 pending_dout = (u32)dummy_dout;
120 /* Trigger the xfer */
121 writel(pending_dout, ®->dout);
124 ret = wait_for_bit_le32(®->ctrl,
125 MVEBU_SPI_A3700_XFER_RDY,
130 /* Read what is transferred in */
131 *din_8 = (u8)readl(®->din);
134 /* Don't increment the current pointer if NULL */
146 static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
147 const void *dout, void *din, unsigned long flags)
149 struct udevice *bus = dev->parent;
150 struct mvebu_spi_plat *plat = dev_get_plat(bus);
151 struct spi_reg *reg = plat->spireg;
152 unsigned int bytelen;
155 bytelen = bitlen / 8;
158 debug("This is a duplex transfer.\n");
161 if (flags & SPI_XFER_BEGIN) {
162 debug("SPI: activate cs.\n");
163 spi_cs_activate(plat, spi_chip_select(dev));
166 /* Send and/or receive */
168 ret = spi_legacy_shift_byte(reg, bytelen, dout, din);
174 if (flags & SPI_XFER_END) {
175 ret = wait_for_bit_le32(®->ctrl,
176 MVEBU_SPI_A3700_XFER_RDY,
181 debug("SPI: deactivate cs.\n");
182 spi_cs_deactivate(plat, spi_chip_select(dev));
188 static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
190 struct mvebu_spi_plat *plat = dev_get_plat(bus);
191 struct spi_reg *reg = plat->spireg;
194 data = readl(®->cfg);
196 prescale = DIV_ROUND_UP(clk_get_rate(&plat->clk), hz);
198 prescale = 0x10 + (prescale + 1) / 2;
199 prescale = min(prescale, 0x1fu);
201 data &= ~MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
202 data |= prescale & MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
204 writel(data, ®->cfg);
209 static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
211 struct mvebu_spi_plat *plat = dev_get_plat(bus);
212 struct spi_reg *reg = plat->spireg;
216 * 0: Serial interface clock is low when inactive
217 * 1: Serial interface clock is high when inactive
220 setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL);
222 clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL);
224 setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA);
226 clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA);
231 static int mvebu_spi_probe(struct udevice *bus)
233 struct mvebu_spi_plat *plat = dev_get_plat(bus);
234 struct spi_reg *reg = plat->spireg;
239 * Settings SPI controller to be working in legacy mode, which
240 * means use only DO pin (I/O 1) for Data Out, and DI pin (I/O 0)
244 /* Flush read/write FIFO */
245 data = readl(®->cfg);
246 writel(data | MVEBU_SPI_A3700_FIFO_FLUSH, ®->cfg);
247 ret = wait_for_bit_le32(®->cfg, MVEBU_SPI_A3700_FIFO_FLUSH,
252 /* Disable FIFO mode */
253 data &= ~MVEBU_SPI_A3700_FIFO_EN;
255 /* Always shift 1 byte at a time */
256 data &= ~MVEBU_SPI_A3700_BYTE_LEN;
258 writel(data, ®->cfg);
260 /* Set up CS GPIOs in device tree, if any */
261 if (CONFIG_IS_ENABLED(DM_GPIO) && gpio_get_list_count(bus, "cs-gpios") > 0) {
264 for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) {
265 ret = gpio_request_by_name(bus, "cs-gpios", i, &plat->cs_gpios[i], 0);
266 if (ret < 0 || !dm_gpio_is_valid(&plat->cs_gpios[i])) {
267 /* Use the native CS function for this line */
271 ret = dm_gpio_set_dir_flags(&plat->cs_gpios[i],
272 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
274 dev_err(bus, "Setting cs %d error\n", i);
283 static int mvebu_spi_of_to_plat(struct udevice *bus)
285 struct mvebu_spi_plat *plat = dev_get_plat(bus);
288 plat->spireg = dev_read_addr_ptr(bus);
290 ret = clk_get_by_index(bus, 0, &plat->clk);
292 dev_err(bus, "cannot get clock\n");
299 static int mvebu_spi_remove(struct udevice *bus)
301 struct mvebu_spi_plat *plat = dev_get_plat(bus);
303 clk_free(&plat->clk);
308 static const struct dm_spi_ops mvebu_spi_ops = {
309 .xfer = mvebu_spi_xfer,
310 .set_speed = mvebu_spi_set_speed,
311 .set_mode = mvebu_spi_set_mode,
313 * cs_info is not needed, since we require all chip selects to be
314 * in the device tree explicitly
318 static const struct udevice_id mvebu_spi_ids[] = {
319 { .compatible = "marvell,armada-3700-spi" },
323 U_BOOT_DRIVER(mvebu_spi) = {
326 .of_match = mvebu_spi_ids,
327 .ops = &mvebu_spi_ops,
328 .of_to_plat = mvebu_spi_of_to_plat,
329 .plat_auto = sizeof(struct mvebu_spi_plat),
330 .probe = mvebu_spi_probe,
331 .remove = mvebu_spi_remove,