2 * NVIDIA Tegra SPI controller (T114 and later)
4 * Copyright (c) 2010-2013 NVIDIA Corporation
6 * See file CREDITS for list of people who contributed to this
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
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/arch/clock.h>
29 #include <asm/arch-tegra/clk_rst.h>
30 #include <asm/arch-tegra114/tegra114_spi.h>
34 DECLARE_GLOBAL_DATA_PTR;
37 #define SPI_CMD1_GO (1 << 31)
38 #define SPI_CMD1_M_S (1 << 30)
39 #define SPI_CMD1_MODE_MASK 0x3
40 #define SPI_CMD1_MODE_SHIFT 28
41 #define SPI_CMD1_CS_SEL_MASK 0x3
42 #define SPI_CMD1_CS_SEL_SHIFT 26
43 #define SPI_CMD1_CS_POL_INACTIVE3 (1 << 25)
44 #define SPI_CMD1_CS_POL_INACTIVE2 (1 << 24)
45 #define SPI_CMD1_CS_POL_INACTIVE1 (1 << 23)
46 #define SPI_CMD1_CS_POL_INACTIVE0 (1 << 22)
47 #define SPI_CMD1_CS_SW_HW (1 << 21)
48 #define SPI_CMD1_CS_SW_VAL (1 << 20)
49 #define SPI_CMD1_IDLE_SDA_MASK 0x3
50 #define SPI_CMD1_IDLE_SDA_SHIFT 18
51 #define SPI_CMD1_BIDIR (1 << 17)
52 #define SPI_CMD1_LSBI_FE (1 << 16)
53 #define SPI_CMD1_LSBY_FE (1 << 15)
54 #define SPI_CMD1_BOTH_EN_BIT (1 << 14)
55 #define SPI_CMD1_BOTH_EN_BYTE (1 << 13)
56 #define SPI_CMD1_RX_EN (1 << 12)
57 #define SPI_CMD1_TX_EN (1 << 11)
58 #define SPI_CMD1_PACKED (1 << 5)
59 #define SPI_CMD1_BIT_LEN_MASK 0x1F
60 #define SPI_CMD1_BIT_LEN_SHIFT 0
63 #define SPI_CMD2_TX_CLK_TAP_DELAY (1 << 6)
64 #define SPI_CMD2_TX_CLK_TAP_DELAY_MASK (0x3F << 6)
65 #define SPI_CMD2_RX_CLK_TAP_DELAY (1 << 0)
66 #define SPI_CMD2_RX_CLK_TAP_DELAY_MASK (0x3F << 0)
69 #define SPI_XFER_STS_RDY (1 << 30)
72 #define SPI_FIFO_STS_CS_INACTIVE (1 << 31)
73 #define SPI_FIFO_STS_FRAME_END (1 << 30)
74 #define SPI_FIFO_STS_RX_FIFO_FLUSH (1 << 15)
75 #define SPI_FIFO_STS_TX_FIFO_FLUSH (1 << 14)
76 #define SPI_FIFO_STS_ERR (1 << 8)
77 #define SPI_FIFO_STS_TX_FIFO_OVF (1 << 7)
78 #define SPI_FIFO_STS_TX_FIFO_UNR (1 << 6)
79 #define SPI_FIFO_STS_RX_FIFO_OVF (1 << 5)
80 #define SPI_FIFO_STS_RX_FIFO_UNR (1 << 4)
81 #define SPI_FIFO_STS_TX_FIFO_FULL (1 << 3)
82 #define SPI_FIFO_STS_TX_FIFO_EMPTY (1 << 2)
83 #define SPI_FIFO_STS_RX_FIFO_FULL (1 << 1)
84 #define SPI_FIFO_STS_RX_FIFO_EMPTY (1 << 0)
86 #define SPI_TIMEOUT 1000
87 #define TEGRA_SPI_MAX_FREQ 52000000
90 u32 command1; /* 000:SPI_COMMAND1 register */
91 u32 command2; /* 004:SPI_COMMAND2 register */
92 u32 timing1; /* 008:SPI_CS_TIM1 register */
93 u32 timing2; /* 00c:SPI_CS_TIM2 register */
94 u32 xfer_status;/* 010:SPI_TRANS_STATUS register */
95 u32 fifo_status;/* 014:SPI_FIFO_STATUS register */
96 u32 tx_data; /* 018:SPI_TX_DATA register */
97 u32 rx_data; /* 01c:SPI_RX_DATA register */
98 u32 dma_ctl; /* 020:SPI_DMA_CTL register */
99 u32 dma_blk; /* 024:SPI_DMA_BLK register */
100 u32 rsvd[56]; /* 028-107 reserved */
101 u32 tx_fifo; /* 108:SPI_FIFO1 register */
102 u32 rsvd2[31]; /* 10c-187 reserved */
103 u32 rx_fifo; /* 188:SPI_FIFO2 register */
104 u32 spare_ctl; /* 18c:SPI_SPARE_CTRL register */
107 struct tegra_spi_ctrl {
108 struct spi_regs *regs;
115 struct tegra_spi_slave {
116 struct spi_slave slave;
117 struct tegra_spi_ctrl *ctrl;
120 static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA114_SPI_CTRLS];
122 static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
124 return container_of(slave, struct tegra_spi_slave, slave);
127 int tegra114_spi_cs_is_valid(unsigned int bus, unsigned int cs)
129 if (bus >= CONFIG_TEGRA114_SPI_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
135 struct spi_slave *tegra114_spi_setup_slave(unsigned int bus, unsigned int cs,
136 unsigned int max_hz, unsigned int mode)
138 struct tegra_spi_slave *spi;
140 debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
141 bus, cs, max_hz, mode);
143 if (!spi_cs_is_valid(bus, cs)) {
144 printf("SPI error: unsupported bus %d / chip select %d\n",
149 if (max_hz > TEGRA_SPI_MAX_FREQ) {
150 printf("SPI error: unsupported frequency %d Hz. Max frequency"
151 " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
155 spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
157 printf("SPI error: malloc of SPI structure failed\n");
160 spi->ctrl = &spi_ctrls[bus];
162 printf("SPI error: could not find controller for bus %d\n",
167 if (max_hz < spi->ctrl->freq) {
168 debug("%s: limiting frequency from %u to %u\n", __func__,
169 spi->ctrl->freq, max_hz);
170 spi->ctrl->freq = max_hz;
172 spi->ctrl->mode = mode;
177 void tegra114_spi_free_slave(struct spi_slave *slave)
179 struct tegra_spi_slave *spi = to_tegra_spi(slave);
184 int tegra114_spi_init(int *node_list, int count)
186 struct tegra_spi_ctrl *ctrl;
191 for (i = 0; i < count; i++) {
192 ctrl = &spi_ctrls[i];
195 ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
197 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
198 debug("%s: no spi register found\n", __func__);
201 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
202 "spi-max-frequency", 0);
204 debug("%s: no spi max frequency found\n", __func__);
208 ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
209 if (ctrl->periph_id == PERIPH_ID_NONE) {
210 debug("%s: could not decode periph id\n", __func__);
216 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
217 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
223 int tegra114_spi_claim_bus(struct spi_slave *slave)
225 struct tegra_spi_slave *spi = to_tegra_spi(slave);
226 struct spi_regs *regs = spi->ctrl->regs;
228 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
229 clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
232 /* Clear stale status here */
233 setbits_le32(®s->fifo_status,
235 SPI_FIFO_STS_TX_FIFO_OVF |
236 SPI_FIFO_STS_TX_FIFO_UNR |
237 SPI_FIFO_STS_RX_FIFO_OVF |
238 SPI_FIFO_STS_RX_FIFO_UNR |
239 SPI_FIFO_STS_TX_FIFO_FULL |
240 SPI_FIFO_STS_TX_FIFO_EMPTY |
241 SPI_FIFO_STS_RX_FIFO_FULL |
242 SPI_FIFO_STS_RX_FIFO_EMPTY);
243 debug("%s: FIFO STATUS = %08x\n", __func__, readl(®s->fifo_status));
245 /* Set master mode and sw controlled CS */
246 setbits_le32(®s->command1, SPI_CMD1_M_S | SPI_CMD1_CS_SW_HW |
247 (spi->ctrl->mode << SPI_CMD1_MODE_SHIFT));
248 debug("%s: COMMAND1 = %08x\n", __func__, readl(®s->command1));
253 void tegra114_spi_cs_activate(struct spi_slave *slave)
255 struct tegra_spi_slave *spi = to_tegra_spi(slave);
256 struct spi_regs *regs = spi->ctrl->regs;
258 clrbits_le32(®s->command1, SPI_CMD1_CS_SW_VAL);
261 void tegra114_spi_cs_deactivate(struct spi_slave *slave)
263 struct tegra_spi_slave *spi = to_tegra_spi(slave);
264 struct spi_regs *regs = spi->ctrl->regs;
266 setbits_le32(®s->command1, SPI_CMD1_CS_SW_VAL);
269 int tegra114_spi_xfer(struct spi_slave *slave, unsigned int bitlen,
270 const void *data_out, void *data_in, unsigned long flags)
272 struct tegra_spi_slave *spi = to_tegra_spi(slave);
273 struct spi_regs *regs = spi->ctrl->regs;
274 u32 reg, tmpdout, tmpdin = 0;
275 const u8 *dout = data_out;
280 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
281 __func__, slave->bus, slave->cs, dout, din, bitlen);
284 num_bytes = bitlen / 8;
288 /* clear all error status bits */
289 reg = readl(®s->fifo_status);
290 writel(reg, ®s->fifo_status);
292 clrsetbits_le32(®s->command1, SPI_CMD1_CS_SW_VAL,
293 SPI_CMD1_RX_EN | SPI_CMD1_TX_EN | SPI_CMD1_LSBY_FE |
294 (slave->cs << SPI_CMD1_CS_SEL_SHIFT));
296 /* set xfer size to 1 block (32 bits) */
297 writel(0, ®s->dma_blk);
299 if (flags & SPI_XFER_BEGIN)
300 spi_cs_activate(slave);
302 /* handle data in 32-bit chunks */
303 while (num_bytes > 0) {
308 bytes = (num_bytes > 4) ? 4 : num_bytes;
311 for (i = 0; i < bytes; ++i)
312 tmpdout = (tmpdout << 8) | dout[i];
318 /* clear ready bit */
319 setbits_le32(®s->xfer_status, SPI_XFER_STS_RDY);
321 clrsetbits_le32(®s->command1,
322 SPI_CMD1_BIT_LEN_MASK << SPI_CMD1_BIT_LEN_SHIFT,
323 (bytes * 8 - 1) << SPI_CMD1_BIT_LEN_SHIFT);
324 writel(tmpdout, ®s->tx_fifo);
325 setbits_le32(®s->command1, SPI_CMD1_GO);
328 * Wait for SPI transmit FIFO to empty, or to time out.
329 * The RX FIFO status will be read and cleared last
331 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
332 u32 fifo_status, xfer_status;
334 xfer_status = readl(®s->xfer_status);
335 if (!(xfer_status & SPI_XFER_STS_RDY))
338 fifo_status = readl(®s->fifo_status);
339 if (fifo_status & SPI_FIFO_STS_ERR) {
340 debug("%s: got a fifo error: ", __func__);
341 if (fifo_status & SPI_FIFO_STS_TX_FIFO_OVF)
342 debug("tx FIFO overflow ");
343 if (fifo_status & SPI_FIFO_STS_TX_FIFO_UNR)
344 debug("tx FIFO underrun ");
345 if (fifo_status & SPI_FIFO_STS_RX_FIFO_OVF)
346 debug("rx FIFO overflow ");
347 if (fifo_status & SPI_FIFO_STS_RX_FIFO_UNR)
348 debug("rx FIFO underrun ");
349 if (fifo_status & SPI_FIFO_STS_TX_FIFO_FULL)
350 debug("tx FIFO full ");
351 if (fifo_status & SPI_FIFO_STS_TX_FIFO_EMPTY)
352 debug("tx FIFO empty ");
353 if (fifo_status & SPI_FIFO_STS_RX_FIFO_FULL)
354 debug("rx FIFO full ");
355 if (fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)
356 debug("rx FIFO empty ");
361 if (!(fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)) {
362 tmpdin = readl(®s->rx_fifo);
364 /* swap bytes read in */
366 for (i = bytes - 1; i >= 0; --i) {
367 din[i] = tmpdin & 0xff;
373 /* We can exit when we've had both RX and TX */
378 if (tm >= SPI_TIMEOUT)
381 /* clear ACK RDY, etc. bits */
382 writel(readl(®s->fifo_status), ®s->fifo_status);
385 if (flags & SPI_XFER_END)
386 spi_cs_deactivate(slave);
388 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
389 __func__, tmpdin, readl(®s->fifo_status));
392 printf("%s: timeout during SPI transfer, tm %d\n",