5 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
8 * SPDX-License-Identifier: GPL-2.0+
15 struct altera_spi_regs {
24 #define ALTERA_SPI_STATUS_ROE_MSK (0x8)
25 #define ALTERA_SPI_STATUS_TOE_MSK (0x10)
26 #define ALTERA_SPI_STATUS_TMT_MSK (0x20)
27 #define ALTERA_SPI_STATUS_TRDY_MSK (0x40)
28 #define ALTERA_SPI_STATUS_RRDY_MSK (0x80)
29 #define ALTERA_SPI_STATUS_E_MSK (0x100)
31 #define ALTERA_SPI_CONTROL_IROE_MSK (0x8)
32 #define ALTERA_SPI_CONTROL_ITOE_MSK (0x10)
33 #define ALTERA_SPI_CONTROL_ITRDY_MSK (0x40)
34 #define ALTERA_SPI_CONTROL_IRRDY_MSK (0x80)
35 #define ALTERA_SPI_CONTROL_IE_MSK (0x100)
36 #define ALTERA_SPI_CONTROL_SSO_MSK (0x400)
38 #ifndef CONFIG_SYS_ALTERA_SPI_LIST
39 #define CONFIG_SYS_ALTERA_SPI_LIST { CONFIG_SYS_SPI_BASE }
42 static ulong altera_spi_base_list[] = CONFIG_SYS_ALTERA_SPI_LIST;
44 struct altera_spi_slave {
45 struct spi_slave slave;
46 struct altera_spi_regs *regs;
48 #define to_altera_spi_slave(s) container_of(s, struct altera_spi_slave, slave)
51 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
53 return bus < ARRAY_SIZE(altera_spi_base_list) && cs < 32;
57 void spi_cs_activate(struct spi_slave *slave)
59 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
60 writel(1 << slave->cs, &altspi->regs->slave_sel);
61 writel(ALTERA_SPI_CONTROL_SSO_MSK, &altspi->regs->control);
65 void spi_cs_deactivate(struct spi_slave *slave)
67 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
68 writel(0, &altspi->regs->control);
69 writel(0, &altspi->regs->slave_sel);
76 void spi_set_speed(struct spi_slave *slave, uint hz)
78 /* altera spi core does not support programmable speed */
81 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
82 unsigned int max_hz, unsigned int mode)
84 struct altera_spi_slave *altspi;
86 if (!spi_cs_is_valid(bus, cs))
89 altspi = spi_alloc_slave(struct altera_spi_slave, bus, cs);
93 altspi->regs = (struct altera_spi_regs *)altera_spi_base_list[bus];
94 debug("%s: bus:%i cs:%i base:%p\n", __func__,
95 bus, cs, altspi->regs);
97 return &altspi->slave;
100 void spi_free_slave(struct spi_slave *slave)
102 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
106 int spi_claim_bus(struct spi_slave *slave)
108 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
110 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
111 writel(0, &altspi->regs->control);
112 writel(0, &altspi->regs->slave_sel);
116 void spi_release_bus(struct spi_slave *slave)
118 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
120 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
121 writel(0, &altspi->regs->slave_sel);
124 #ifndef CONFIG_ALTERA_SPI_IDLE_VAL
125 # define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
128 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
129 void *din, unsigned long flags)
131 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
132 /* assume spi core configured to do 8 bit transfers */
133 uint bytes = bitlen / 8;
134 const uchar *txp = dout;
137 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
138 slave->bus, slave->cs, bitlen, bytes, flags);
143 flags |= SPI_XFER_END;
147 /* empty read buffer */
148 if (readl(&altspi->regs->status) & ALTERA_SPI_STATUS_RRDY_MSK)
149 readl(&altspi->regs->rxdata);
150 if (flags & SPI_XFER_BEGIN)
151 spi_cs_activate(slave);
154 uchar d = txp ? *txp++ : CONFIG_ALTERA_SPI_IDLE_VAL;
155 debug("%s: tx:%x ", __func__, d);
156 writel(d, &altspi->regs->txdata);
157 while (!(readl(&altspi->regs->status) & ALTERA_SPI_STATUS_RRDY_MSK))
159 d = readl(&altspi->regs->rxdata);
165 if (flags & SPI_XFER_END)
166 spi_cs_deactivate(slave);