2 * Opencore tiny_spi driver
4 * http://opencores.org/project,tiny_spi
7 * Copyright (c) 2005-2008 Analog Devices Inc.
8 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
10 * Licensed under the GPL-2 or later.
19 #define TINY_SPI_STATUS_TXE 0x1
20 #define TINY_SPI_STATUS_TXR 0x2
22 struct tiny_spi_regs {
23 unsigned rxdata; /* Rx data reg */
24 unsigned txdata; /* Tx data reg */
25 unsigned status; /* Status reg */
26 unsigned control; /* Control reg */
27 unsigned baud; /* Baud reg */
30 struct tiny_spi_host {
35 static const struct tiny_spi_host tiny_spi_host_list[] =
36 CONFIG_SYS_TINY_SPI_LIST;
38 struct tiny_spi_slave {
39 struct spi_slave slave;
40 const struct tiny_spi_host *host;
45 #define to_tiny_spi_slave(s) container_of(s, struct tiny_spi_slave, slave)
47 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
49 return bus < ARRAY_SIZE(tiny_spi_host_list) && gpio_is_valid(cs);
52 void spi_cs_activate(struct spi_slave *slave)
54 struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
55 unsigned int cs = slave->cs;
57 gpio_set_value(cs, tiny_spi->flg);
58 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
61 void spi_cs_deactivate(struct spi_slave *slave)
63 struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
64 unsigned int cs = slave->cs;
66 gpio_set_value(cs, !tiny_spi->flg);
67 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
70 void spi_set_speed(struct spi_slave *slave, uint hz)
72 struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
73 const struct tiny_spi_host *host = tiny_spi->host;
75 tiny_spi->baud = min(DIV_ROUND_UP(host->freq, hz * 2),
76 (1 << host->baudwidth)) - 1;
77 debug("%s: speed %u actual %u\n", __func__, hz,
78 host->freq / ((tiny_spi->baud + 1) * 2));
85 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
86 unsigned int hz, unsigned int mode)
88 struct tiny_spi_slave *tiny_spi;
90 if (!spi_cs_is_valid(bus, cs) || gpio_request(cs, "tiny_spi"))
93 tiny_spi = malloc(sizeof(*tiny_spi));
96 memset(tiny_spi, 0, sizeof(*tiny_spi));
98 tiny_spi->slave.bus = bus;
99 tiny_spi->slave.cs = cs;
100 tiny_spi->host = &tiny_spi_host_list[bus];
101 tiny_spi->mode = mode & (SPI_CPOL | SPI_CPHA);
102 tiny_spi->flg = mode & SPI_CS_HIGH ? 1 : 0;
103 spi_set_speed(&tiny_spi->slave, hz);
105 debug("%s: bus:%i cs:%i base:%lx\n", __func__,
106 bus, cs, tiny_spi->host->base);
107 return &tiny_spi->slave;
110 void spi_free_slave(struct spi_slave *slave)
112 struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
114 gpio_free(slave->cs);
118 int spi_claim_bus(struct spi_slave *slave)
120 struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
121 struct tiny_spi_regs *regs = (void *)tiny_spi->host->base;
123 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
124 gpio_direction_output(slave->cs, !tiny_spi->flg);
125 writel(tiny_spi->mode, ®s->control);
126 writel(tiny_spi->baud, ®s->baud);
130 void spi_release_bus(struct spi_slave *slave)
132 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
135 #ifndef CONFIG_TINY_SPI_IDLE_VAL
136 # define CONFIG_TINY_SPI_IDLE_VAL 0xff
139 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
140 void *din, unsigned long flags)
142 struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
143 struct tiny_spi_regs *regs = (void *)tiny_spi->host->base;
144 const u8 *txp = dout;
146 uint bytes = bitlen / 8;
149 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
150 slave->bus, slave->cs, bitlen, bytes, flags);
154 /* assume to do 8 bits transfers */
156 flags |= SPI_XFER_END;
160 if (flags & SPI_XFER_BEGIN)
161 spi_cs_activate(slave);
163 /* we need to tighten the transfer loop */
165 writeb(*txp++, ®s->txdata);
167 writeb(*txp++, ®s->txdata);
168 for (i = 2; i < bytes; i++) {
170 while (!(readb(®s->status) &
171 TINY_SPI_STATUS_TXR))
173 rx = readb(®s->txdata);
174 writeb(tx, ®s->txdata);
177 while (!(readb(®s->status) &
178 TINY_SPI_STATUS_TXR))
180 *rxp++ = readb(®s->txdata);
182 while (!(readb(®s->status) &
183 TINY_SPI_STATUS_TXE))
185 *rxp++ = readb(®s->rxdata);
187 writeb(CONFIG_TINY_SPI_IDLE_VAL, ®s->txdata);
189 writeb(CONFIG_TINY_SPI_IDLE_VAL,
191 for (i = 2; i < bytes; i++) {
193 while (!(readb(®s->status) &
194 TINY_SPI_STATUS_TXR))
196 rx = readb(®s->txdata);
197 writeb(CONFIG_TINY_SPI_IDLE_VAL,
201 while (!(readb(®s->status) &
202 TINY_SPI_STATUS_TXR))
204 *rxp++ = readb(®s->txdata);
206 while (!(readb(®s->status) &
207 TINY_SPI_STATUS_TXE))
209 *rxp++ = readb(®s->rxdata);
211 writeb(*txp++, ®s->txdata);
213 writeb(*txp++, ®s->txdata);
214 for (i = 2; i < bytes; i++) {
216 while (!(readb(®s->status) &
217 TINY_SPI_STATUS_TXR))
219 writeb(tx, ®s->txdata);
222 while (!(readb(®s->status) &
223 TINY_SPI_STATUS_TXE))
226 writeb(CONFIG_TINY_SPI_IDLE_VAL, ®s->txdata);
228 writeb(CONFIG_TINY_SPI_IDLE_VAL,
230 for (i = 2; i < bytes; i++) {
231 while (!(readb(®s->status) &
232 TINY_SPI_STATUS_TXR))
234 writeb(CONFIG_TINY_SPI_IDLE_VAL,
238 while (!(readb(®s->status) &
239 TINY_SPI_STATUS_TXE))
244 if (flags & SPI_XFER_END)
245 spi_cs_deactivate(slave);