blob: f3b09644889c530a9ac9cc2764e1f5a5ac05adec [file] [log] [blame]
Allen Martinb19f5742013-01-29 13:51:28 +00001/*
2 * NVIDIA Tegra SPI-SLINK controller
3 *
4 * Copyright (c) 2010-2013 NVIDIA Corporation
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <malloc.h>
26#include <asm/io.h>
27#include <asm/gpio.h>
28#include <asm/arch/clock.h>
29#include <asm/arch-tegra/clk_rst.h>
Allen Martinff1da6f2013-03-16 18:58:03 +000030#include <asm/arch-tegra20/tegra20_slink.h>
Allen Martinb19f5742013-01-29 13:51:28 +000031#include <spi.h>
32#include <fdtdec.h>
33
34DECLARE_GLOBAL_DATA_PTR;
35
Allen Martin7a49ba62013-03-16 18:58:05 +000036/* COMMAND */
37#define SLINK_CMD_ENB (1 << 31)
38#define SLINK_CMD_GO (1 << 30)
39#define SLINK_CMD_M_S (1 << 28)
40#define SLINK_CMD_CK_SDA (1 << 21)
41#define SLINK_CMD_CS_POL (1 << 13)
42#define SLINK_CMD_CS_VAL (1 << 12)
43#define SLINK_CMD_CS_SOFT (1 << 11)
44#define SLINK_CMD_BIT_LENGTH (1 << 4)
45#define SLINK_CMD_BIT_LENGTH_MASK 0x0000001F
46/* COMMAND2 */
47#define SLINK_CMD2_TXEN (1 << 30)
48#define SLINK_CMD2_RXEN (1 << 31)
49#define SLINK_CMD2_SS_EN (1 << 18)
50#define SLINK_CMD2_SS_EN_SHIFT 18
51#define SLINK_CMD2_SS_EN_MASK 0x000C0000
52#define SLINK_CMD2_CS_ACTIVE_BETWEEN (1 << 17)
53/* STATUS */
54#define SLINK_STAT_BSY (1 << 31)
55#define SLINK_STAT_RDY (1 << 30)
56#define SLINK_STAT_ERR (1 << 29)
57#define SLINK_STAT_RXF_FLUSH (1 << 27)
58#define SLINK_STAT_TXF_FLUSH (1 << 26)
59#define SLINK_STAT_RXF_OVF (1 << 25)
60#define SLINK_STAT_TXF_UNR (1 << 24)
61#define SLINK_STAT_RXF_EMPTY (1 << 23)
62#define SLINK_STAT_RXF_FULL (1 << 22)
63#define SLINK_STAT_TXF_EMPTY (1 << 21)
64#define SLINK_STAT_TXF_FULL (1 << 20)
65#define SLINK_STAT_TXF_OVF (1 << 19)
66#define SLINK_STAT_RXF_UNR (1 << 18)
67#define SLINK_STAT_CUR_BLKCNT (1 << 15)
68/* STATUS2 */
69#define SLINK_STAT2_RXF_FULL_CNT (1 << 16)
70#define SLINK_STAT2_TXF_FULL_CNT (1 << 0)
71
72#define SPI_TIMEOUT 1000
73#define TEGRA_SPI_MAX_FREQ 52000000
74
75struct spi_regs {
76 u32 command; /* SLINK_COMMAND_0 register */
77 u32 command2; /* SLINK_COMMAND2_0 reg */
78 u32 status; /* SLINK_STATUS_0 register */
79 u32 reserved; /* Reserved offset 0C */
80 u32 mas_data; /* SLINK_MAS_DATA_0 reg */
81 u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
82 u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
83 u32 status2; /* SLINK_STATUS2_0 reg */
84 u32 rsvd[56]; /* 0x20 to 0xFF reserved */
85 u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
86 u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
87 u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
88};
89
Allen Martinb19f5742013-01-29 13:51:28 +000090struct tegra_spi_ctrl {
Allen Martin7a49ba62013-03-16 18:58:05 +000091 struct spi_regs *regs;
Allen Martinb19f5742013-01-29 13:51:28 +000092 unsigned int freq;
93 unsigned int mode;
94 int periph_id;
95 int valid;
96};
97
98struct tegra_spi_slave {
99 struct spi_slave slave;
100 struct tegra_spi_ctrl *ctrl;
101};
102
103static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS];
104
105static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
106{
107 return container_of(slave, struct tegra_spi_slave, slave);
108}
109
110int spi_cs_is_valid(unsigned int bus, unsigned int cs)
111{
112 if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
113 return 0;
114 else
115 return 1;
116}
117
118struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
119 unsigned int max_hz, unsigned int mode)
120{
121 struct tegra_spi_slave *spi;
122
123 debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
124 bus, cs, max_hz, mode);
125
126 if (!spi_cs_is_valid(bus, cs)) {
127 printf("SPI error: unsupported bus %d / chip select %d\n",
128 bus, cs);
129 return NULL;
130 }
131
132 if (max_hz > TEGRA_SPI_MAX_FREQ) {
133 printf("SPI error: unsupported frequency %d Hz. Max frequency"
134 " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
135 return NULL;
136 }
137
138 spi = malloc(sizeof(struct tegra_spi_slave));
139 if (!spi) {
140 printf("SPI error: malloc of SPI structure failed\n");
141 return NULL;
142 }
143 spi->slave.bus = bus;
144 spi->slave.cs = cs;
145 spi->ctrl = &spi_ctrls[bus];
146 if (!spi->ctrl) {
147 printf("SPI error: could not find controller for bus %d\n",
148 bus);
149 return NULL;
150 }
151
152 if (max_hz < spi->ctrl->freq) {
153 debug("%s: limiting frequency from %u to %u\n", __func__,
154 spi->ctrl->freq, max_hz);
155 spi->ctrl->freq = max_hz;
156 }
157 spi->ctrl->mode = mode;
158
159 return &spi->slave;
160}
161
162void spi_free_slave(struct spi_slave *slave)
163{
164 struct tegra_spi_slave *spi = to_tegra_spi(slave);
165
166 free(spi);
167}
168
169void spi_init(void)
170{
171 struct tegra_spi_ctrl *ctrl;
172 int i;
Allen Martinb19f5742013-01-29 13:51:28 +0000173 int node = 0;
174 int count;
175 int node_list[CONFIG_TEGRA_SLINK_CTRLS];
176
177 count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
178 COMPAT_NVIDIA_TEGRA20_SLINK,
179 node_list,
180 CONFIG_TEGRA_SLINK_CTRLS);
181 for (i = 0; i < count; i++) {
182 ctrl = &spi_ctrls[i];
183 node = node_list[i];
184
Allen Martin7a49ba62013-03-16 18:58:05 +0000185 ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
186 node, "reg");
Allen Martinb19f5742013-01-29 13:51:28 +0000187 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
188 debug("%s: no slink register found\n", __func__);
189 continue;
190 }
191 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
192 "spi-max-frequency", 0);
193 if (!ctrl->freq) {
194 debug("%s: no slink max frequency found\n", __func__);
195 continue;
196 }
197
198 ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
199 if (ctrl->periph_id == PERIPH_ID_NONE) {
200 debug("%s: could not decode periph id\n", __func__);
201 continue;
202 }
203 ctrl->valid = 1;
204
205 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
206 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
207 }
Allen Martinb19f5742013-01-29 13:51:28 +0000208}
209
210int spi_claim_bus(struct spi_slave *slave)
211{
212 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin7a49ba62013-03-16 18:58:05 +0000213 struct spi_regs *regs = spi->ctrl->regs;
Allen Martinb19f5742013-01-29 13:51:28 +0000214 u32 reg;
215
216 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
217 clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
218 spi->ctrl->freq);
219
220 /* Clear stale status here */
221 reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
222 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
223 writel(reg, &regs->status);
224 debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
225
226 /* Set master mode and sw controlled CS */
227 reg = readl(&regs->command);
228 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
229 writel(reg, &regs->command);
230 debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
231
232 return 0;
233}
234
235void spi_release_bus(struct spi_slave *slave)
236{
237}
238
239void spi_cs_activate(struct spi_slave *slave)
240{
241 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin7a49ba62013-03-16 18:58:05 +0000242 struct spi_regs *regs = spi->ctrl->regs;
Allen Martinb19f5742013-01-29 13:51:28 +0000243
244 /* CS is negated on Tegra, so drive a 1 to get a 0 */
245 setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
246}
247
248void spi_cs_deactivate(struct spi_slave *slave)
249{
250 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin7a49ba62013-03-16 18:58:05 +0000251 struct spi_regs *regs = spi->ctrl->regs;
Allen Martinb19f5742013-01-29 13:51:28 +0000252
253 /* CS is negated on Tegra, so drive a 0 to get a 1 */
254 clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
255}
256
257int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
258 const void *data_out, void *data_in, unsigned long flags)
259{
260 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin7a49ba62013-03-16 18:58:05 +0000261 struct spi_regs *regs = spi->ctrl->regs;
Allen Martinb19f5742013-01-29 13:51:28 +0000262 u32 reg, tmpdout, tmpdin = 0;
263 const u8 *dout = data_out;
264 u8 *din = data_in;
265 int num_bytes;
266 int ret;
267
268 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
269 __func__, slave->bus, slave->cs, dout, din, bitlen);
270 if (bitlen % 8)
271 return -1;
272 num_bytes = bitlen / 8;
273
274 ret = 0;
275
276 reg = readl(&regs->status);
277 writel(reg, &regs->status); /* Clear all SPI events via R/W */
278 debug("%s entry: STATUS = %08x\n", __func__, reg);
279
280 reg = readl(&regs->status2);
281 writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
282 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
283
284 debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
285
286 clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
287 SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
288 (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
289 debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
290
291 if (flags & SPI_XFER_BEGIN)
292 spi_cs_activate(slave);
293
294 /* handle data in 32-bit chunks */
295 while (num_bytes > 0) {
296 int bytes;
297 int is_read = 0;
298 int tm, i;
299
300 tmpdout = 0;
301 bytes = (num_bytes > 4) ? 4 : num_bytes;
302
303 if (dout != NULL) {
304 for (i = 0; i < bytes; ++i)
305 tmpdout = (tmpdout << 8) | dout[i];
306 dout += bytes;
307 }
308
309 num_bytes -= bytes;
310
311 clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
312 bytes * 8 - 1);
313 writel(tmpdout, &regs->tx_fifo);
314 setbits_le32(&regs->command, SLINK_CMD_GO);
315
316 /*
317 * Wait for SPI transmit FIFO to empty, or to time out.
318 * The RX FIFO status will be read and cleared last
319 */
320 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
321 u32 status;
322
323 status = readl(&regs->status);
324
325 /* We can exit when we've had both RX and TX activity */
326 if (is_read && (status & SLINK_STAT_TXF_EMPTY))
327 break;
328
329 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
330 SLINK_STAT_RDY)
331 tm++;
332
333 else if (!(status & SLINK_STAT_RXF_EMPTY)) {
334 tmpdin = readl(&regs->rx_fifo);
335 is_read = 1;
336
337 /* swap bytes read in */
338 if (din != NULL) {
339 for (i = bytes - 1; i >= 0; --i) {
340 din[i] = tmpdin & 0xff;
341 tmpdin >>= 8;
342 }
343 din += bytes;
344 }
345 }
346 }
347
348 if (tm >= SPI_TIMEOUT)
349 ret = tm;
350
351 /* clear ACK RDY, etc. bits */
352 writel(readl(&regs->status), &regs->status);
353 }
354
355 if (flags & SPI_XFER_END)
356 spi_cs_deactivate(slave);
357
358 debug("%s: transfer ended. Value=%08x, status = %08x\n",
359 __func__, tmpdin, readl(&regs->status));
360
361 if (ret) {
362 printf("%s: timeout during SPI transfer, tm %d\n",
363 __func__, ret);
364 return -1;
365 }
366
367 return 0;
368}