1 // SPDX-License-Identifier: GPL-2.0+
6 * Copyright (c) 2005-2008 Analog Devices Inc.
7 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
17 #include <linux/bitops.h>
19 #define ALTERA_SPI_STATUS_RRDY_MSK BIT(7)
20 #define ALTERA_SPI_CONTROL_SSO_MSK BIT(10)
22 #ifndef CONFIG_ALTERA_SPI_IDLE_VAL
23 #define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
26 struct altera_spi_regs {
35 struct altera_spi_platdata {
36 struct altera_spi_regs *regs;
39 struct altera_spi_priv {
40 struct altera_spi_regs *regs;
43 static void spi_cs_activate(struct udevice *dev, uint cs)
45 struct udevice *bus = dev->parent;
46 struct altera_spi_priv *priv = dev_get_priv(bus);
47 struct altera_spi_regs *const regs = priv->regs;
49 writel(1 << cs, ®s->slave_sel);
50 writel(ALTERA_SPI_CONTROL_SSO_MSK, ®s->control);
53 static void spi_cs_deactivate(struct udevice *dev)
55 struct udevice *bus = dev->parent;
56 struct altera_spi_priv *priv = dev_get_priv(bus);
57 struct altera_spi_regs *const regs = priv->regs;
59 writel(0, ®s->control);
60 writel(0, ®s->slave_sel);
63 static int altera_spi_claim_bus(struct udevice *dev)
65 struct udevice *bus = dev->parent;
66 struct altera_spi_priv *priv = dev_get_priv(bus);
67 struct altera_spi_regs *const regs = priv->regs;
69 writel(0, ®s->control);
70 writel(0, ®s->slave_sel);
75 static int altera_spi_release_bus(struct udevice *dev)
77 struct udevice *bus = dev->parent;
78 struct altera_spi_priv *priv = dev_get_priv(bus);
79 struct altera_spi_regs *const regs = priv->regs;
81 writel(0, ®s->slave_sel);
86 static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen,
87 const void *dout, void *din, unsigned long flags)
89 struct udevice *bus = dev->parent;
90 struct altera_spi_priv *priv = dev_get_priv(bus);
91 struct altera_spi_regs *const regs = priv->regs;
92 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
94 /* assume spi core configured to do 8 bit transfers */
95 unsigned int bytes = bitlen / 8;
96 const unsigned char *txp = dout;
97 unsigned char *rxp = din;
98 uint32_t reg, data, start;
100 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
101 bus->seq, slave_plat->cs, bitlen, bytes, flags);
107 flags |= SPI_XFER_END;
111 /* empty read buffer */
112 if (readl(®s->status) & ALTERA_SPI_STATUS_RRDY_MSK)
113 readl(®s->rxdata);
115 if (flags & SPI_XFER_BEGIN)
116 spi_cs_activate(dev, slave_plat->cs);
122 data = CONFIG_ALTERA_SPI_IDLE_VAL;
124 debug("%s: tx:%x ", __func__, data);
125 writel(data, ®s->txdata);
127 start = get_timer(0);
129 reg = readl(®s->status);
130 if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
132 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
133 debug("%s: Transmission timed out!\n", __func__);
138 data = readl(®s->rxdata);
140 *rxp++ = data & 0xff;
142 debug("rx:%x\n", data);
146 if (flags & SPI_XFER_END)
147 spi_cs_deactivate(dev);
152 static int altera_spi_set_speed(struct udevice *bus, uint speed)
157 static int altera_spi_set_mode(struct udevice *bus, uint mode)
162 static int altera_spi_probe(struct udevice *bus)
164 struct altera_spi_platdata *plat = dev_get_platdata(bus);
165 struct altera_spi_priv *priv = dev_get_priv(bus);
167 priv->regs = plat->regs;
172 static int altera_spi_ofdata_to_platdata(struct udevice *bus)
174 struct altera_spi_platdata *plat = dev_get_platdata(bus);
176 plat->regs = map_physmem(devfdt_get_addr(bus),
177 sizeof(struct altera_spi_regs),
183 static const struct dm_spi_ops altera_spi_ops = {
184 .claim_bus = altera_spi_claim_bus,
185 .release_bus = altera_spi_release_bus,
186 .xfer = altera_spi_xfer,
187 .set_speed = altera_spi_set_speed,
188 .set_mode = altera_spi_set_mode,
190 * cs_info is not needed, since we require all chip selects to be
191 * in the device tree explicitly
195 static const struct udevice_id altera_spi_ids[] = {
196 { .compatible = "altr,spi-1.0" },
200 U_BOOT_DRIVER(altera_spi) = {
201 .name = "altera_spi",
203 .of_match = altera_spi_ids,
204 .ops = &altera_spi_ops,
205 .ofdata_to_platdata = altera_spi_ofdata_to_platdata,
206 .platdata_auto_alloc_size = sizeof(struct altera_spi_platdata),
207 .priv_auto_alloc_size = sizeof(struct altera_spi_priv),
208 .probe = altera_spi_probe,