1 // SPDX-License-Identifier: GPL-2.0+
6 * Copyright (c) 2005-2008 Analog Devices Inc.
18 #define ALTERA_SPI_STATUS_RRDY_MSK BIT(7)
19 #define ALTERA_SPI_CONTROL_SSO_MSK BIT(10)
21 #ifndef CONFIG_ALTERA_SPI_IDLE_VAL
22 #define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
25 struct altera_spi_regs {
34 struct altera_spi_platdata {
35 struct altera_spi_regs *regs;
38 struct altera_spi_priv {
39 struct altera_spi_regs *regs;
42 static void spi_cs_activate(struct udevice *dev, uint cs)
44 struct udevice *bus = dev->parent;
45 struct altera_spi_priv *priv = dev_get_priv(bus);
46 struct altera_spi_regs *const regs = priv->regs;
48 writel(1 << cs, ®s->slave_sel);
49 writel(ALTERA_SPI_CONTROL_SSO_MSK, ®s->control);
52 static void spi_cs_deactivate(struct udevice *dev)
54 struct udevice *bus = dev->parent;
55 struct altera_spi_priv *priv = dev_get_priv(bus);
56 struct altera_spi_regs *const regs = priv->regs;
58 writel(0, ®s->control);
59 writel(0, ®s->slave_sel);
62 static int altera_spi_claim_bus(struct udevice *dev)
64 struct udevice *bus = dev->parent;
65 struct altera_spi_priv *priv = dev_get_priv(bus);
66 struct altera_spi_regs *const regs = priv->regs;
68 writel(0, ®s->control);
69 writel(0, ®s->slave_sel);
74 static int altera_spi_release_bus(struct udevice *dev)
76 struct udevice *bus = dev->parent;
77 struct altera_spi_priv *priv = dev_get_priv(bus);
78 struct altera_spi_regs *const regs = priv->regs;
80 writel(0, ®s->slave_sel);
85 static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen,
86 const void *dout, void *din, unsigned long flags)
88 struct udevice *bus = dev->parent;
89 struct altera_spi_priv *priv = dev_get_priv(bus);
90 struct altera_spi_regs *const regs = priv->regs;
91 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
93 /* assume spi core configured to do 8 bit transfers */
94 unsigned int bytes = bitlen / 8;
95 const unsigned char *txp = dout;
96 unsigned char *rxp = din;
97 uint32_t reg, data, start;
99 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
100 bus->seq, slave_plat->cs, bitlen, bytes, flags);
106 flags |= SPI_XFER_END;
110 /* empty read buffer */
111 if (readl(®s->status) & ALTERA_SPI_STATUS_RRDY_MSK)
112 readl(®s->rxdata);
114 if (flags & SPI_XFER_BEGIN)
115 spi_cs_activate(dev, slave_plat->cs);
121 data = CONFIG_ALTERA_SPI_IDLE_VAL;
123 debug("%s: tx:%x ", __func__, data);
124 writel(data, ®s->txdata);
126 start = get_timer(0);
128 reg = readl(®s->status);
129 if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
131 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
132 debug("%s: Transmission timed out!\n", __func__);
137 data = readl(®s->rxdata);
139 *rxp++ = data & 0xff;
141 debug("rx:%x\n", data);
145 if (flags & SPI_XFER_END)
146 spi_cs_deactivate(dev);
151 static int altera_spi_set_speed(struct udevice *bus, uint speed)
156 static int altera_spi_set_mode(struct udevice *bus, uint mode)
161 static int altera_spi_probe(struct udevice *bus)
163 struct altera_spi_platdata *plat = dev_get_platdata(bus);
164 struct altera_spi_priv *priv = dev_get_priv(bus);
166 priv->regs = plat->regs;
171 static int altera_spi_ofdata_to_platdata(struct udevice *bus)
173 struct altera_spi_platdata *plat = dev_get_platdata(bus);
175 plat->regs = map_physmem(devfdt_get_addr(bus),
176 sizeof(struct altera_spi_regs),
182 static const struct dm_spi_ops altera_spi_ops = {
183 .claim_bus = altera_spi_claim_bus,
184 .release_bus = altera_spi_release_bus,
185 .xfer = altera_spi_xfer,
186 .set_speed = altera_spi_set_speed,
187 .set_mode = altera_spi_set_mode,
189 * cs_info is not needed, since we require all chip selects to be
190 * in the device tree explicitly
194 static const struct udevice_id altera_spi_ids[] = {
195 { .compatible = "altr,spi-1.0" },
199 U_BOOT_DRIVER(altera_spi) = {
200 .name = "altera_spi",
202 .of_match = altera_spi_ids,
203 .ops = &altera_spi_ops,
204 .ofdata_to_platdata = altera_spi_ofdata_to_platdata,
205 .platdata_auto_alloc_size = sizeof(struct altera_spi_platdata),
206 .priv_auto_alloc_size = sizeof(struct altera_spi_priv),
207 .probe = altera_spi_probe,