5 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
8 * Licensed under the GPL-2 or later.
15 #define ALTERA_SPI_RXDATA 0
16 #define ALTERA_SPI_TXDATA 4
17 #define ALTERA_SPI_STATUS 8
18 #define ALTERA_SPI_CONTROL 12
19 #define ALTERA_SPI_SLAVE_SEL 20
21 #define ALTERA_SPI_STATUS_ROE_MSK (0x8)
22 #define ALTERA_SPI_STATUS_TOE_MSK (0x10)
23 #define ALTERA_SPI_STATUS_TMT_MSK (0x20)
24 #define ALTERA_SPI_STATUS_TRDY_MSK (0x40)
25 #define ALTERA_SPI_STATUS_RRDY_MSK (0x80)
26 #define ALTERA_SPI_STATUS_E_MSK (0x100)
28 #define ALTERA_SPI_CONTROL_IROE_MSK (0x8)
29 #define ALTERA_SPI_CONTROL_ITOE_MSK (0x10)
30 #define ALTERA_SPI_CONTROL_ITRDY_MSK (0x40)
31 #define ALTERA_SPI_CONTROL_IRRDY_MSK (0x80)
32 #define ALTERA_SPI_CONTROL_IE_MSK (0x100)
33 #define ALTERA_SPI_CONTROL_SSO_MSK (0x400)
35 #ifndef CONFIG_SYS_ALTERA_SPI_LIST
36 #define CONFIG_SYS_ALTERA_SPI_LIST { CONFIG_SYS_SPI_BASE }
39 static ulong altera_spi_base_list[] = CONFIG_SYS_ALTERA_SPI_LIST;
41 struct altera_spi_slave {
42 struct spi_slave slave;
45 #define to_altera_spi_slave(s) container_of(s, struct altera_spi_slave, slave)
48 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
50 return bus < ARRAY_SIZE(altera_spi_base_list) && cs < 32;
54 void spi_cs_activate(struct spi_slave *slave)
56 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
57 writel(1 << slave->cs, altspi->base + ALTERA_SPI_SLAVE_SEL);
58 writel(ALTERA_SPI_CONTROL_SSO_MSK, altspi->base + ALTERA_SPI_CONTROL);
62 void spi_cs_deactivate(struct spi_slave *slave)
64 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
65 writel(0, altspi->base + ALTERA_SPI_CONTROL);
66 writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL);
73 void spi_set_speed(struct spi_slave *slave, uint hz)
75 /* altera spi core does not support programmable speed */
78 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
79 unsigned int max_hz, unsigned int mode)
81 struct altera_spi_slave *altspi;
83 if (!spi_cs_is_valid(bus, cs))
86 altspi = malloc(sizeof(*altspi));
90 altspi->slave.bus = bus;
91 altspi->slave.cs = cs;
92 altspi->base = altera_spi_base_list[bus];
93 debug("%s: bus:%i cs:%i base:%lx\n", __func__,
94 bus, cs, altspi->base);
96 return &altspi->slave;
99 void spi_free_slave(struct spi_slave *slave)
101 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
105 int spi_claim_bus(struct spi_slave *slave)
107 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
109 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
110 writel(0, altspi->base + ALTERA_SPI_CONTROL);
111 writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL);
115 void spi_release_bus(struct spi_slave *slave)
117 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
119 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
120 writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL);
123 #ifndef CONFIG_ALTERA_SPI_IDLE_VAL
124 # define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
127 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
128 void *din, unsigned long flags)
130 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
131 /* assume spi core configured to do 8 bit transfers */
132 uint bytes = bitlen / 8;
133 const uchar *txp = dout;
136 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
137 slave->bus, slave->cs, bitlen, bytes, flags);
142 flags |= SPI_XFER_END;
146 /* empty read buffer */
147 if (readl(altspi->base + ALTERA_SPI_STATUS) &
148 ALTERA_SPI_STATUS_RRDY_MSK)
149 readl(altspi->base + ALTERA_SPI_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->base + ALTERA_SPI_TXDATA);
157 while (!(readl(altspi->base + ALTERA_SPI_STATUS) &
158 ALTERA_SPI_STATUS_RRDY_MSK))
160 d = readl(altspi->base + ALTERA_SPI_RXDATA);
166 if (flags & SPI_XFER_END)
167 spi_cs_deactivate(slave);