blob: 7c3a3fc35b1aec9ae445af9e9cbe40c20c102e89 [file] [log] [blame]
Tom Warrenee554f82011-11-05 09:48:11 +00001/*
Allen Martin73e0f1b2013-03-16 18:58:06 +00002 * Copyright (c) 2010-2013 NVIDIA Corporation
Tom Warrenee554f82011-11-05 09:48:11 +00003 * 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>
Tom Warrenee554f82011-11-05 09:48:11 +000026#include <malloc.h>
Tom Warrenee554f82011-11-05 09:48:11 +000027#include <asm/io.h>
28#include <asm/gpio.h>
Tom Warrenee554f82011-11-05 09:48:11 +000029#include <asm/arch/clock.h>
30#include <asm/arch/pinmux.h>
Tom Warrenab371962012-09-19 15:50:56 -070031#include <asm/arch-tegra/clk_rst.h>
Allen Martine8e99ab2013-03-16 18:58:03 +000032#include <asm/arch-tegra20/tegra20_sflash.h>
Tom Warrenab371962012-09-19 15:50:56 -070033#include <spi.h>
Allen Martine7659522013-01-29 13:51:24 +000034#include <fdtdec.h>
35
36DECLARE_GLOBAL_DATA_PTR;
Tom Warrenee554f82011-11-05 09:48:11 +000037
Allen Martin8db241b2013-03-16 18:58:05 +000038#define SPI_CMD_GO (1 << 30)
39#define SPI_CMD_ACTIVE_SCLK_SHIFT 26
40#define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT)
41#define SPI_CMD_CK_SDA (1 << 21)
42#define SPI_CMD_ACTIVE_SDA_SHIFT 18
43#define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT)
44#define SPI_CMD_CS_POL (1 << 16)
45#define SPI_CMD_TXEN (1 << 15)
46#define SPI_CMD_RXEN (1 << 14)
47#define SPI_CMD_CS_VAL (1 << 13)
48#define SPI_CMD_CS_SOFT (1 << 12)
49#define SPI_CMD_CS_DELAY (1 << 9)
50#define SPI_CMD_CS3_EN (1 << 8)
51#define SPI_CMD_CS2_EN (1 << 7)
52#define SPI_CMD_CS1_EN (1 << 6)
53#define SPI_CMD_CS0_EN (1 << 5)
54#define SPI_CMD_BIT_LENGTH (1 << 4)
55#define SPI_CMD_BIT_LENGTH_MASK 0x0000001F
56
57#define SPI_STAT_BSY (1 << 31)
58#define SPI_STAT_RDY (1 << 30)
59#define SPI_STAT_RXF_FLUSH (1 << 29)
60#define SPI_STAT_TXF_FLUSH (1 << 28)
61#define SPI_STAT_RXF_UNR (1 << 27)
62#define SPI_STAT_TXF_OVF (1 << 26)
63#define SPI_STAT_RXF_EMPTY (1 << 25)
64#define SPI_STAT_RXF_FULL (1 << 24)
65#define SPI_STAT_TXF_EMPTY (1 << 23)
66#define SPI_STAT_TXF_FULL (1 << 22)
67#define SPI_STAT_SEL_TXRX_N (1 << 16)
68#define SPI_STAT_CUR_BLKCNT (1 << 15)
69
70#define SPI_TIMEOUT 1000
71#define TEGRA_SPI_MAX_FREQ 52000000
72
73struct spi_regs {
74 u32 command; /* SPI_COMMAND_0 register */
75 u32 status; /* SPI_STATUS_0 register */
76 u32 rx_cmp; /* SPI_RX_CMP_0 register */
77 u32 dma_ctl; /* SPI_DMA_CTL_0 register */
78 u32 tx_fifo; /* SPI_TX_FIFO_0 register */
79 u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */
80 u32 rx_fifo; /* SPI_RX_FIFO_0 register */
81};
82
Allen Martin73e0f1b2013-03-16 18:58:06 +000083struct tegra_spi_ctrl {
Allen Martin8db241b2013-03-16 18:58:05 +000084 struct spi_regs *regs;
Tom Warrenee554f82011-11-05 09:48:11 +000085 unsigned int freq;
86 unsigned int mode;
Allen Martine7659522013-01-29 13:51:24 +000087 int periph_id;
Allen Martin73e0f1b2013-03-16 18:58:06 +000088 int valid;
89};
90
91struct tegra_spi_slave {
92 struct spi_slave slave;
93 struct tegra_spi_ctrl *ctrl;
Tom Warrenee554f82011-11-05 09:48:11 +000094};
95
Allen Martin73e0f1b2013-03-16 18:58:06 +000096/* tegra20 only supports one SFLASH controller */
97static struct tegra_spi_ctrl spi_ctrls[1];
98
Tom Warrenee554f82011-11-05 09:48:11 +000099static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
100{
101 return container_of(slave, struct tegra_spi_slave, slave);
102}
103
Allen Martinb98691c2013-03-16 18:58:07 +0000104int tegra20_spi_cs_is_valid(unsigned int bus, unsigned int cs)
Tom Warrenee554f82011-11-05 09:48:11 +0000105{
Allen Martin55d98a12012-08-31 08:30:00 +0000106 /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
Tom Warrenee554f82011-11-05 09:48:11 +0000107 if (bus != 0 || cs != 0)
108 return 0;
109 else
110 return 1;
111}
112
Allen Martinb98691c2013-03-16 18:58:07 +0000113struct spi_slave *tegra20_spi_setup_slave(unsigned int bus, unsigned int cs,
114 unsigned int max_hz, unsigned int mode)
Tom Warrenee554f82011-11-05 09:48:11 +0000115{
116 struct tegra_spi_slave *spi;
117
118 if (!spi_cs_is_valid(bus, cs)) {
119 printf("SPI error: unsupported bus %d / chip select %d\n",
120 bus, cs);
121 return NULL;
122 }
123
Tom Warren22562a42012-09-04 17:00:24 -0700124 if (max_hz > TEGRA_SPI_MAX_FREQ) {
Tom Warrenee554f82011-11-05 09:48:11 +0000125 printf("SPI error: unsupported frequency %d Hz. Max frequency"
Tom Warren22562a42012-09-04 17:00:24 -0700126 " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
Tom Warrenee554f82011-11-05 09:48:11 +0000127 return NULL;
128 }
129
Albert ARIBAUD32adc232013-03-28 18:50:01 +0100130 spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
Tom Warrenee554f82011-11-05 09:48:11 +0000131 if (!spi) {
132 printf("SPI error: malloc of SPI structure failed\n");
133 return NULL;
134 }
Allen Martin73e0f1b2013-03-16 18:58:06 +0000135 spi->ctrl = &spi_ctrls[bus];
136 if (!spi->ctrl) {
137 printf("SPI error: could not find controller for bus %d\n",
138 bus);
Allen Martine7659522013-01-29 13:51:24 +0000139 return NULL;
140 }
Allen Martin73e0f1b2013-03-16 18:58:06 +0000141
142 if (max_hz < spi->ctrl->freq) {
Allen Martine7659522013-01-29 13:51:24 +0000143 debug("%s: limiting frequency from %u to %u\n", __func__,
Allen Martin73e0f1b2013-03-16 18:58:06 +0000144 spi->ctrl->freq, max_hz);
145 spi->ctrl->freq = max_hz;
Allen Martine7659522013-01-29 13:51:24 +0000146 }
Allen Martin73e0f1b2013-03-16 18:58:06 +0000147 spi->ctrl->mode = mode;
Tom Warrenee554f82011-11-05 09:48:11 +0000148
149 return &spi->slave;
150}
151
Allen Martinb98691c2013-03-16 18:58:07 +0000152void tegra20_spi_free_slave(struct spi_slave *slave)
Tom Warrenee554f82011-11-05 09:48:11 +0000153{
154 struct tegra_spi_slave *spi = to_tegra_spi(slave);
155
156 free(spi);
157}
158
Allen Martinb98691c2013-03-16 18:58:07 +0000159int tegra20_spi_init(int *node_list, int count)
Tom Warrenee554f82011-11-05 09:48:11 +0000160{
Allen Martin73e0f1b2013-03-16 18:58:06 +0000161 struct tegra_spi_ctrl *ctrl;
162 int i;
163 int node = 0;
Allen Martinb98691c2013-03-16 18:58:07 +0000164 int found = 0;
Allen Martin73e0f1b2013-03-16 18:58:06 +0000165
Allen Martin73e0f1b2013-03-16 18:58:06 +0000166 for (i = 0; i < count; i++) {
167 ctrl = &spi_ctrls[i];
168 node = node_list[i];
169
170 ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
171 node, "reg");
172 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
173 debug("%s: no slink register found\n", __func__);
174 continue;
175 }
176 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
177 "spi-max-frequency", 0);
178 if (!ctrl->freq) {
179 debug("%s: no slink max frequency found\n", __func__);
180 continue;
181 }
182
183 ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
184 if (ctrl->periph_id == PERIPH_ID_NONE) {
185 debug("%s: could not decode periph id\n", __func__);
186 continue;
187 }
188 ctrl->valid = 1;
Allen Martinb98691c2013-03-16 18:58:07 +0000189 found = 1;
Allen Martin73e0f1b2013-03-16 18:58:06 +0000190
191 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
192 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
193 }
Allen Martinb98691c2013-03-16 18:58:07 +0000194 return !found;
Tom Warrenee554f82011-11-05 09:48:11 +0000195}
196
Allen Martinb98691c2013-03-16 18:58:07 +0000197int tegra20_spi_claim_bus(struct spi_slave *slave)
Tom Warrenee554f82011-11-05 09:48:11 +0000198{
199 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin73e0f1b2013-03-16 18:58:06 +0000200 struct spi_regs *regs = spi->ctrl->regs;
Tom Warrenee554f82011-11-05 09:48:11 +0000201 u32 reg;
202
203 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Allen Martin73e0f1b2013-03-16 18:58:06 +0000204 clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
205 spi->ctrl->freq);
Tom Warrenee554f82011-11-05 09:48:11 +0000206
207 /* Clear stale status here */
208 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
209 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
210 writel(reg, &regs->status);
Allen Martinb98691c2013-03-16 18:58:07 +0000211 debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
Tom Warrenee554f82011-11-05 09:48:11 +0000212
213 /*
214 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
215 */
Allen Martin73e0f1b2013-03-16 18:58:06 +0000216 reg = (spi->ctrl->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
217 if (spi->ctrl->mode & 2)
Tom Warrenee554f82011-11-05 09:48:11 +0000218 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
219 clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
220 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
Allen Martinb98691c2013-03-16 18:58:07 +0000221 debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
Tom Warrenee554f82011-11-05 09:48:11 +0000222
223 /*
Allen Martin55d98a12012-08-31 08:30:00 +0000224 * SPI pins on Tegra20 are muxed - change pinmux later due to UART
Tom Warrenee554f82011-11-05 09:48:11 +0000225 * issue.
226 */
227 pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
228 pinmux_tristate_disable(PINGRP_LSPI);
Allen Martine0158b82013-03-16 18:58:02 +0000229 pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
Simon Glass34bad072011-11-05 04:46:50 +0000230
Tom Warrenee554f82011-11-05 09:48:11 +0000231 return 0;
232}
233
Allen Martinb98691c2013-03-16 18:58:07 +0000234void tegra20_spi_cs_activate(struct spi_slave *slave)
Tom Warrenee554f82011-11-05 09:48:11 +0000235{
236 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin73e0f1b2013-03-16 18:58:06 +0000237 struct spi_regs *regs = spi->ctrl->regs;
Tom Warrenee554f82011-11-05 09:48:11 +0000238
Tom Warrenee554f82011-11-05 09:48:11 +0000239 /* CS is negated on Tegra, so drive a 1 to get a 0 */
Allen Martin73e0f1b2013-03-16 18:58:06 +0000240 setbits_le32(&regs->command, SPI_CMD_CS_VAL);
Tom Warrenee554f82011-11-05 09:48:11 +0000241}
242
Allen Martinb98691c2013-03-16 18:58:07 +0000243void tegra20_spi_cs_deactivate(struct spi_slave *slave)
Tom Warrenee554f82011-11-05 09:48:11 +0000244{
245 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin73e0f1b2013-03-16 18:58:06 +0000246 struct spi_regs *regs = spi->ctrl->regs;
Tom Warrenee554f82011-11-05 09:48:11 +0000247
248 /* CS is negated on Tegra, so drive a 0 to get a 1 */
Allen Martin73e0f1b2013-03-16 18:58:06 +0000249 clrbits_le32(&regs->command, SPI_CMD_CS_VAL);
Tom Warrenee554f82011-11-05 09:48:11 +0000250}
251
Allen Martinb98691c2013-03-16 18:58:07 +0000252int tegra20_spi_xfer(struct spi_slave *slave, unsigned int bitlen,
Tom Warrenee554f82011-11-05 09:48:11 +0000253 const void *data_out, void *data_in, unsigned long flags)
254{
255 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin73e0f1b2013-03-16 18:58:06 +0000256 struct spi_regs *regs = spi->ctrl->regs;
Tom Warrenee554f82011-11-05 09:48:11 +0000257 u32 reg, tmpdout, tmpdin = 0;
258 const u8 *dout = data_out;
259 u8 *din = data_in;
260 int num_bytes;
261 int ret;
262
263 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
264 slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
265 if (bitlen % 8)
266 return -1;
267 num_bytes = bitlen / 8;
268
269 ret = 0;
270
271 reg = readl(&regs->status);
272 writel(reg, &regs->status); /* Clear all SPI events via R/W */
273 debug("spi_xfer entry: STATUS = %08x\n", reg);
274
275 reg = readl(&regs->command);
276 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
277 writel(reg, &regs->command);
278 debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
279
280 if (flags & SPI_XFER_BEGIN)
281 spi_cs_activate(slave);
282
283 /* handle data in 32-bit chunks */
284 while (num_bytes > 0) {
285 int bytes;
286 int is_read = 0;
287 int tm, i;
288
289 tmpdout = 0;
290 bytes = (num_bytes > 4) ? 4 : num_bytes;
291
292 if (dout != NULL) {
293 for (i = 0; i < bytes; ++i)
294 tmpdout = (tmpdout << 8) | dout[i];
295 }
296
297 num_bytes -= bytes;
298 if (dout)
299 dout += bytes;
300
301 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
302 bytes * 8 - 1);
303 writel(tmpdout, &regs->tx_fifo);
304 setbits_le32(&regs->command, SPI_CMD_GO);
305
306 /*
307 * Wait for SPI transmit FIFO to empty, or to time out.
308 * The RX FIFO status will be read and cleared last
309 */
310 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
311 u32 status;
312
313 status = readl(&regs->status);
314
315 /* We can exit when we've had both RX and TX activity */
316 if (is_read && (status & SPI_STAT_TXF_EMPTY))
317 break;
318
319 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
320 SPI_STAT_RDY)
321 tm++;
322
323 else if (!(status & SPI_STAT_RXF_EMPTY)) {
324 tmpdin = readl(&regs->rx_fifo);
325 is_read = 1;
326
327 /* swap bytes read in */
328 if (din != NULL) {
329 for (i = bytes - 1; i >= 0; --i) {
330 din[i] = tmpdin & 0xff;
331 tmpdin >>= 8;
332 }
333 din += bytes;
334 }
335 }
336 }
337
338 if (tm >= SPI_TIMEOUT)
339 ret = tm;
340
341 /* clear ACK RDY, etc. bits */
342 writel(readl(&regs->status), &regs->status);
343 }
344
345 if (flags & SPI_XFER_END)
346 spi_cs_deactivate(slave);
347
348 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
349 tmpdin, readl(&regs->status));
350
351 if (ret) {
352 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
353 return -1;
354 }
355
356 return 0;
357}