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 #define ALTERA_SPI_IDLE_VAL 0xff
24 struct altera_spi_regs {
33 struct altera_spi_plat {
34 struct altera_spi_regs *regs;
37 struct altera_spi_priv {
38 struct altera_spi_regs *regs;
41 static void spi_cs_activate(struct udevice *dev, uint cs)
43 struct udevice *bus = dev->parent;
44 struct altera_spi_priv *priv = dev_get_priv(bus);
45 struct altera_spi_regs *const regs = priv->regs;
47 writel(1 << cs, ®s->slave_sel);
48 writel(ALTERA_SPI_CONTROL_SSO_MSK, ®s->control);
51 static void spi_cs_deactivate(struct udevice *dev)
53 struct udevice *bus = dev->parent;
54 struct altera_spi_priv *priv = dev_get_priv(bus);
55 struct altera_spi_regs *const regs = priv->regs;
57 writel(0, ®s->control);
58 writel(0, ®s->slave_sel);
61 static int altera_spi_claim_bus(struct udevice *dev)
63 struct udevice *bus = dev->parent;
64 struct altera_spi_priv *priv = dev_get_priv(bus);
65 struct altera_spi_regs *const regs = priv->regs;
67 writel(0, ®s->control);
68 writel(0, ®s->slave_sel);
73 static int altera_spi_release_bus(struct udevice *dev)
75 struct udevice *bus = dev->parent;
76 struct altera_spi_priv *priv = dev_get_priv(bus);
77 struct altera_spi_regs *const regs = priv->regs;
79 writel(0, ®s->slave_sel);
84 static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen,
85 const void *dout, void *din, unsigned long flags)
87 struct udevice *bus = dev->parent;
88 struct altera_spi_priv *priv = dev_get_priv(bus);
89 struct altera_spi_regs *const regs = priv->regs;
90 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
92 /* assume spi core configured to do 8 bit transfers */
93 unsigned int bytes = bitlen / 8;
94 const unsigned char *txp = dout;
95 unsigned char *rxp = din;
96 uint32_t reg, data, start;
98 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
99 dev_seq(bus), slave_plat->cs, bitlen, bytes, flags);
105 flags |= SPI_XFER_END;
109 /* empty read buffer */
110 if (readl(®s->status) & ALTERA_SPI_STATUS_RRDY_MSK)
111 readl(®s->rxdata);
113 if (flags & SPI_XFER_BEGIN)
114 spi_cs_activate(dev, slave_plat->cs);
120 data = ALTERA_SPI_IDLE_VAL;
122 debug("%s: tx:%x ", __func__, data);
123 writel(data, ®s->txdata);
125 start = get_timer(0);
127 reg = readl(®s->status);
128 if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
130 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
131 debug("%s: Transmission timed out!\n", __func__);
136 data = readl(®s->rxdata);
138 *rxp++ = data & 0xff;
140 debug("rx:%x\n", data);
144 if (flags & SPI_XFER_END)
145 spi_cs_deactivate(dev);
150 static int altera_spi_set_speed(struct udevice *bus, uint speed)
155 static int altera_spi_set_mode(struct udevice *bus, uint mode)
160 static int altera_spi_probe(struct udevice *bus)
162 struct altera_spi_plat *plat = dev_get_plat(bus);
163 struct altera_spi_priv *priv = dev_get_priv(bus);
165 priv->regs = plat->regs;
170 static int altera_spi_of_to_plat(struct udevice *bus)
172 struct altera_spi_plat *plat = dev_get_plat(bus);
174 plat->regs = map_physmem(dev_read_addr(bus),
175 sizeof(struct altera_spi_regs),
181 static const struct dm_spi_ops altera_spi_ops = {
182 .claim_bus = altera_spi_claim_bus,
183 .release_bus = altera_spi_release_bus,
184 .xfer = altera_spi_xfer,
185 .set_speed = altera_spi_set_speed,
186 .set_mode = altera_spi_set_mode,
188 * cs_info is not needed, since we require all chip selects to be
189 * in the device tree explicitly
193 static const struct udevice_id altera_spi_ids[] = {
194 { .compatible = "altr,spi-1.0" },
198 U_BOOT_DRIVER(altera_spi) = {
199 .name = "altera_spi",
201 .of_match = altera_spi_ids,
202 .ops = &altera_spi_ops,
203 .of_to_plat = altera_spi_of_to_plat,
204 .plat_auto = sizeof(struct altera_spi_plat),
205 .priv_auto = sizeof(struct altera_spi_priv),
206 .probe = altera_spi_probe,