3 * Michael Kurz <michi.kurz@googlemail.com>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <asm/addrspace.h>
29 #include <asm/types.h>
30 #include <asm/ar71xx.h>
32 /*-----------------------------------------------------------------------
37 #define PRINTD(fmt,args...) printf (fmt ,##args)
39 #define PRINTD(fmt,args...)
42 struct ar71xx_spi_slave {
43 struct spi_slave slave;
47 static inline struct ar71xx_spi_slave *to_ar71xx_spi(struct spi_slave *slave)
49 return container_of(slave, struct ar71xx_spi_slave, slave);
52 /*=====================================================================*/
53 /* Public Functions */
54 /*=====================================================================*/
56 /*-----------------------------------------------------------------------
62 PRINTD("ar71xx_spi: spi_init");
64 // Init SPI Hardware, disable remap, set clock
65 __raw_writel(0x43, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_CTRL));
67 PRINTD(" ---> out\n");
70 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
71 unsigned int max_hz, unsigned int mode)
73 struct ar71xx_spi_slave *ss;
75 PRINTD("ar71xx_spi: spi_setup_slave");
77 if ((bus != 0) || (cs > 2))
80 ss = malloc(sizeof(struct ar71xx_spi_slave));
88 /* TODO: Use max_hz to limit the SCK rate */
90 PRINTD(" ---> out\n");
95 void spi_free_slave(struct spi_slave *slave)
97 struct ar71xx_spi_slave *ss = to_ar71xx_spi(slave);
102 int spi_claim_bus(struct spi_slave *slave)
108 void spi_release_bus(struct spi_slave *slave)
113 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
114 void *din, unsigned long flags)
116 struct ar71xx_spi_slave *ss = to_ar71xx_spi(slave);
118 const uint8_t *tx = dout;
119 uint8_t curbyte, curbitlen, restbits;
120 uint32_t bytes = bitlen / 8;
124 PRINTD("ar71xx_spi: spi_xfer: slave:%p bitlen:%08x dout:%p din:%p flags:%08x\n", slave, bitlen, dout, din, flags);
126 if (flags & SPI_XFER_BEGIN) {
127 __raw_writel(SPI_FS_GPIO, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_FS));
128 __raw_writel(SPI_IOC_CS_ALL, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
131 restbits = (bitlen % 8);
135 // enable chip select
136 out = SPI_IOC_CS_ALL & ~(SPI_IOC_CS(slave->cs));
146 curbitlen = restbits;
147 curbyte <<= 8 - restbits;
152 PRINTD("ar71xx_spi: sending: data:%02x length:%d\n", curbyte, curbitlen);
154 /* clock starts at inactive polarity */
155 for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
157 if (curbyte & (1 << 7))
160 out &= ~(SPI_IOC_DO);
162 /* setup MSB (to slave) on trailing edge */
163 __raw_writel(out, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
165 __raw_writel(out | SPI_IOC_CLK, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
170 in = __raw_readl(KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_RDS));
171 PRINTD("ar71xx_spi: received:%02x\n", in);
177 *rx++ = (in << (8 - restbits));
182 if (flags & SPI_XFER_END) {
183 __raw_writel(SPI_IOC_CS(slave->cs), KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
184 __raw_writel(SPI_IOC_CS_ALL, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
185 __raw_writel(0, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_FS));
188 PRINTD(" ---> out\n");