tegra2: spi: Add SPI driver for Tegra2 SOC
[oweals/u-boot.git] / drivers / spi / tegra2_spi.c
1 /*
2  * Copyright (c) 2010-2011 NVIDIA Corporation
3  * With help from the mpc8xxx SPI driver
4  * With more help from omap3_spi SPI driver
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26
27 #include <malloc.h>
28 #include <spi.h>
29 #include <asm/io.h>
30 #include <asm/gpio.h>
31 #include <asm/arch/clk_rst.h>
32 #include <asm/arch/clock.h>
33 #include <asm/arch/pinmux.h>
34 #include <asm/arch/tegra2_spi.h>
35
36 struct tegra_spi_slave {
37         struct spi_slave slave;
38         struct spi_tegra *regs;
39         unsigned int freq;
40         unsigned int mode;
41 };
42
43 static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
44 {
45         return container_of(slave, struct tegra_spi_slave, slave);
46 }
47
48 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
49 {
50         /* Tegra2 SPI-Flash - only 1 device ('bus/cs') */
51         if (bus != 0 || cs != 0)
52                 return 0;
53         else
54                 return 1;
55 }
56
57 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
58                 unsigned int max_hz, unsigned int mode)
59 {
60         struct tegra_spi_slave *spi;
61
62         if (!spi_cs_is_valid(bus, cs)) {
63                 printf("SPI error: unsupported bus %d / chip select %d\n",
64                        bus, cs);
65                 return NULL;
66         }
67
68         if (max_hz > TEGRA2_SPI_MAX_FREQ) {
69                 printf("SPI error: unsupported frequency %d Hz. Max frequency"
70                         " is %d Hz\n", max_hz, TEGRA2_SPI_MAX_FREQ);
71                 return NULL;
72         }
73
74         spi = malloc(sizeof(struct tegra_spi_slave));
75         if (!spi) {
76                 printf("SPI error: malloc of SPI structure failed\n");
77                 return NULL;
78         }
79         spi->slave.bus = bus;
80         spi->slave.cs = cs;
81         spi->freq = max_hz;
82         spi->regs = (struct spi_tegra *)TEGRA2_SPI_BASE;
83         spi->mode = mode;
84
85         return &spi->slave;
86 }
87
88 void spi_free_slave(struct spi_slave *slave)
89 {
90         struct tegra_spi_slave *spi = to_tegra_spi(slave);
91
92         free(spi);
93 }
94
95 void spi_init(void)
96 {
97         /* do nothing */
98 }
99
100 int spi_claim_bus(struct spi_slave *slave)
101 {
102         struct tegra_spi_slave *spi = to_tegra_spi(slave);
103         struct spi_tegra *regs = spi->regs;
104         u32 reg;
105
106         /* Change SPI clock to correct frequency, PLLP_OUT0 source */
107         clock_start_periph_pll(PERIPH_ID_SPI1, CLOCK_ID_PERIPH, spi->freq);
108
109         /* Clear stale status here */
110         reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
111                 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
112         writel(reg, &regs->status);
113         debug("spi_init: STATUS = %08x\n", readl(&regs->status));
114
115         /*
116          * Use sw-controlled CS, so we can clock in data after ReadID, etc.
117          */
118         reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
119         if (spi->mode & 2)
120                 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
121         clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
122                 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
123         debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
124
125         /*
126          * SPI pins on Tegra2 are muxed - change pinmux later due to UART
127          * issue.
128          */
129         pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
130         pinmux_tristate_disable(PINGRP_LSPI);
131         return 0;
132 }
133
134 void spi_release_bus(struct spi_slave *slave)
135 {
136         /*
137          * We can't release UART_DISABLE and set pinmux to UART4 here since
138          * some code (e,g, spi_flash_probe) uses printf() while the SPI
139          * bus is held. That is arguably bad, but it has the advantage of
140          * already being in the source tree.
141          */
142 }
143
144 void spi_cs_activate(struct spi_slave *slave)
145 {
146         struct tegra_spi_slave *spi = to_tegra_spi(slave);
147
148         /* CS is negated on Tegra, so drive a 1 to get a 0 */
149         setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
150 }
151
152 void spi_cs_deactivate(struct spi_slave *slave)
153 {
154         struct tegra_spi_slave *spi = to_tegra_spi(slave);
155
156         /* CS is negated on Tegra, so drive a 0 to get a 1 */
157         clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
158 }
159
160 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
161                 const void *data_out, void *data_in, unsigned long flags)
162 {
163         struct tegra_spi_slave *spi = to_tegra_spi(slave);
164         struct spi_tegra *regs = spi->regs;
165         u32 reg, tmpdout, tmpdin = 0;
166         const u8 *dout = data_out;
167         u8 *din = data_in;
168         int num_bytes;
169         int ret;
170
171         debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
172               slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
173         if (bitlen % 8)
174                 return -1;
175         num_bytes = bitlen / 8;
176
177         ret = 0;
178
179         reg = readl(&regs->status);
180         writel(reg, &regs->status);     /* Clear all SPI events via R/W */
181         debug("spi_xfer entry: STATUS = %08x\n", reg);
182
183         reg = readl(&regs->command);
184         reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
185         writel(reg, &regs->command);
186         debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
187
188         if (flags & SPI_XFER_BEGIN)
189                 spi_cs_activate(slave);
190
191         /* handle data in 32-bit chunks */
192         while (num_bytes > 0) {
193                 int bytes;
194                 int is_read = 0;
195                 int tm, i;
196
197                 tmpdout = 0;
198                 bytes = (num_bytes > 4) ?  4 : num_bytes;
199
200                 if (dout != NULL) {
201                         for (i = 0; i < bytes; ++i)
202                                 tmpdout = (tmpdout << 8) | dout[i];
203                 }
204
205                 num_bytes -= bytes;
206                 if (dout)
207                         dout += bytes;
208
209                 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
210                                 bytes * 8 - 1);
211                 writel(tmpdout, &regs->tx_fifo);
212                 setbits_le32(&regs->command, SPI_CMD_GO);
213
214                 /*
215                  * Wait for SPI transmit FIFO to empty, or to time out.
216                  * The RX FIFO status will be read and cleared last
217                  */
218                 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
219                         u32 status;
220
221                         status = readl(&regs->status);
222
223                         /* We can exit when we've had both RX and TX activity */
224                         if (is_read && (status & SPI_STAT_TXF_EMPTY))
225                                 break;
226
227                         if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
228                                         SPI_STAT_RDY)
229                                 tm++;
230
231                         else if (!(status & SPI_STAT_RXF_EMPTY)) {
232                                 tmpdin = readl(&regs->rx_fifo);
233                                 is_read = 1;
234
235                                 /* swap bytes read in */
236                                 if (din != NULL) {
237                                         for (i = bytes - 1; i >= 0; --i) {
238                                                 din[i] = tmpdin & 0xff;
239                                                 tmpdin >>= 8;
240                                         }
241                                         din += bytes;
242                                 }
243                         }
244                 }
245
246                 if (tm >= SPI_TIMEOUT)
247                         ret = tm;
248
249                 /* clear ACK RDY, etc. bits */
250                 writel(readl(&regs->status), &regs->status);
251         }
252
253         if (flags & SPI_XFER_END)
254                 spi_cs_deactivate(slave);
255
256         debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
257                 tmpdin, readl(&regs->status));
258
259         if (ret) {
260                 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
261                 return -1;
262         }
263
264         return 0;
265 }