Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: tegra2_spi.c

Issue 6731011: Add Tegra2 SPI controller. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/flashrom.git@master
Patch Set: remove an empty line. Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tegra2_spi.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 NVIDIA Corporation
5 * Copyright (C) 2011 Google Inc
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #if defined(__arm__)
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "flash.h"
29 #include "programmer.h"
30 #include "tegra2_spi.h"
31
32 static void *gpio_base, *clkrst_base, *apbmisc_base, *spi_base;
33
34 #define SPI_TIMEOUT 50000 /* 100ms = 50000 * 2us */
35
36 /* On Seaboard: GPIO_PI3 = Port I = 8, bit = 3 */
37 #define UART_DISABLE_PORT 8
38 #define UART_DISABLE_BIT 3
39
40 /* To restore the original value of register. */
41 struct tegra2_undo_register {
42 void *reg_addr;
43 uint32_t value;
44 };
45
46 #define register_tegra2_undo(reg) \
47 do { \
48 struct tegra2_undo_register *undo; \
49 undo = malloc(sizeof(struct tegra2_undo_register)); \
50 undo->reg_addr = (reg); \
51 undo->value = mmio_readl(reg); \
52 register_shutdown(tegra2_do_undo, undo); \
53 } while (0)
54
55 static void tegra2_do_undo(void *p) {
56 struct tegra2_undo_register *undo = (struct tegra2_undo_register*)p;
57
58 mmio_writel(undo->value, undo->reg_addr);
59 free(p);
60 }
61
62 /* Config port:bit as GPIO, not SFPIO (default) */
63 static void __set_config(unsigned port, unsigned bit, int type)
64 {
65 u32 u;
66
67 msg_pdbg("%s: port = %d, bit = %d, %s\n", __func__,
68 port, bit, type ? "GPIO" : "SFPIO");
69
70 u = mmio_readl(GPIO_CNF(port));
71 if (type) /* GPIO */
72 u |= 1 << bit;
73 else
74 u &= ~(1 << bit);
75 register_tegra2_undo(GPIO_CNF(port));
76 mmio_writel(u, GPIO_CNF(port));
77 }
78
79 /* Config GPIO port:bit as input or output (OE) */
80 static void __set_direction(unsigned port, unsigned bit, int output)
81 {
82 u32 u;
83
84 msg_pdbg("%s: port = %d, bit = %d, %s\n", __func__,
85 port, bit, output ? "OUT" : "IN");
86
87 u = mmio_readl(GPIO_OE(port));
88 if (output)
89 u |= 1 << bit;
90 else
91 u &= ~(1 << bit);
92 register_tegra2_undo(GPIO_OE(port));
93 mmio_writel(u, GPIO_OE(port));
94 }
95
96 /* set GPIO OUT port:bit as 0 or 1 */
97 static void __set_level(unsigned port, unsigned bit, int high)
98 {
99 u32 u;
100
101 msg_pdbg("%s: port = %d, bit %d == %d\n", __func__,
102 port, bit, high);
103
104 u = mmio_readl(GPIO_OUT(port));
105 if (high)
106 u |= 1 << bit;
107 else
108 u &= ~(1 << bit);
109 register_tegra2_undo(GPIO_OUT(port));
110 mmio_writel(u, GPIO_OUT(port));
111 }
112
113
114 /* set GPIO port:bit as an output, with polarity 'value' */
115 static int tg2_gpio_direction_output(unsigned port, unsigned bit, int value)
116 {
117 msg_pdbg("%s: port = %d, bit = %d, value = %d\n",
118 __func__, port, bit, value);
119
120 /* Configure as a GPIO */
121 __set_config(port, bit, 1);
122
123 /* Configure GPIO output value. */
124 __set_level(port, bit, value);
125
126 /* Configure GPIO direction as output. */
127 __set_direction(port, bit, 1);
128
129 return 0;
130 }
131
132 static void spi_cs_activate(void)
133 {
134 uint32_t *spi_cmd = (uint32_t *)spi_base;
135
136 /*
137 * CS is negated on Tegra, so drive a 1 to get a 0
138 */
139 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_CS_VAL, spi_cmd);
140 msg_pdbg("%s: CS driven %s\n", __func__,
141 (mmio_readl(spi_cmd) & SPI_CMD_CS_VAL) ? "LOW" : "HIGH");
142 }
143
144 static void spi_cs_deactivate(void)
145 {
146 uint32_t *spi_cmd = (uint32_t *)spi_base;
147
148
149 /*
150 * CS is negated on Tegra, so drive a 0 to get a 1
151 */
152 mmio_writel(mmio_readl(spi_cmd) & ~SPI_CMD_CS_VAL, spi_cmd);
153 msg_pdbg("%s: CS driven %s\n", __func__,
154 (mmio_readl(spi_cmd) & SPI_CMD_CS_VAL) ? "LOW" : "HIGH");
155 }
156
157
158 /* Helper function to calculate the clock cycle in this round.
159 * Also updates the byte count remaining to be used this round.
160 *
161 * For example, we want to write 6 bytes to SPI and then read 5 bytes back.
162 *
163 * +---+---+---+---+---+---+
164 * | W | W | W | W | W | W |
165 * +---+---+---+---+---+---+---+---+---+---+---+
166 * | R | R | R | R | R |
167 * +---+---+---+---+---+
168 * |<-- round 0 -->|
169 * |<-- round 1 -->|
170 * |<-- round 2 -->|
171 *
172 * So that the continuous calling this function would get:
173 *
174 * round| RET| writecnt readcnt bits to_write to_read
175 * -----+----+---------------------------------------------
176 * INIT | | 6 5
177 * 0 | 1 | 2 5 32 4 0
178 * 1 | 1 | 0 3 32 2 2
179 * 2 | 1 | 0 0 24 0 3
180 * 3 | 0 | - - - - -
181 *
182 */
183 int next4Bytes(uint32_t *writecnt, uint32_t *readcnt, int *num_bits,
184 uint32_t *to_write, uint32_t *to_read) {
185 assert(writecnt);
186 assert(readcnt);
187 assert(num_bits);
188 assert(to_write);
189 assert(to_read);
190
191 *to_write = min(*writecnt, 4);
192 *to_read = min(*readcnt, 4 - *to_write);
193
194 *writecnt -= *to_write;
195 *readcnt -= *to_read;
196
197 *num_bits = (*to_write + *to_read) * 8;
198
199 if (*num_bits)
200 return 1; /* need to be called again. */
201 else
202 return 0; /* handled write and read requests. */
203 }
204
205 /*
206 * Tegra2 FIFO design is ... interesting. For example, you want to Tx 2 bytes:
207 *
208 * +---+---+
209 * writearr[]: | 0 | 1 |
210 * +---+---+
211 * \ \
212 * \ \
213 * \ \
214 * \ \
215 * \ \
216 * 31 +---+---+---+---+ 0
217 * tmp(32-bits): | X | X | 0 | 1 |
218 * +---+---+---+---+ LSB
219 *
220 * It is neither little or big endian. The first bit for SPI controller to
221 * transfer is the bit 15 in FIFO, neither bit 31 or bit 0, because the transfer
222 * length is 16 bits (2 bytes).
223 *
224 * Rx follows the similar rule. First bit comes at bit 0, and the whole FIFO
225 * left-shifts 1 bit for every bit comes in. Hence, after reading 3 bytes,
226 * the first coming bit will reside in bit 23.
227 */
228 int tegra2_spi_send_command(unsigned int writecnt,
229 unsigned int readcnt,
230 const unsigned char *writearr,
231 unsigned char *readarr)
232 {
233 int retval = 0;
234 uint8_t *delayed_msg = NULL; /* for UART is disabled. */
235 uint32_t *spi_cmd = (uint32_t *)spi_base;
236 uint32_t *spi_sts = (uint32_t *)(spi_base + 0x04);
237 uint32_t *tx_fifo = (uint32_t *)(spi_base + 0x10);
238 uint32_t *rx_fifo = (uint32_t *)(spi_base + 0x20);
239 uint32_t status;
240 uint32_t to_write, to_read; /* byte counts to fill FIFO. */
241 uint32_t bits; /* bit count to tell SPI controller. */
242
243 mmio_writel(mmio_readl(spi_sts), spi_sts);
244 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_TXEN | SPI_CMD_RXEN, spi_cmd);
245 spi_cs_activate();
246
247 while (next4Bytes(&writecnt, &readcnt, &bits, &to_write, &to_read)) {
248 int i;
249 uint32_t tmp;
250 uint32_t tm; /* timeout counter */
251
252 /* prepare Tx FIFO */
253 for (tmp = 0, i = 0; i < to_write; ++i) {
254 tmp |= (*writearr++) << ((bits / 8 - 1 - i) * 8);
255 }
256 mmio_writel(tmp, tx_fifo);
257
258 /* Kick the SCLK running: Shift out TX FIFO, and receive RX. */
259 mmio_writel(mmio_readl(spi_cmd) & ~SPI_CMD_BIT_LENGTH_MASK,
260 spi_cmd);
261 mmio_writel(mmio_readl(spi_cmd) | (bits - 1),
262 spi_cmd);
263 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_GO, spi_cmd);
264
265 /* Wait for controller completes the task. */
266 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
267 if (((status = mmio_readl(spi_sts)) &
268 (SPI_STAT_BSY | SPI_STAT_RDY)) == SPI_STAT_RDY)
269 break;
270 /* We setup clock to 6MHz, so that we shall come back
271 * after: 1 / (6MHz) * 8 bits = 1.333us
272 */
273 programmer_delay(2);
274 }
275 mmio_writel(mmio_readl(spi_sts) | SPI_STAT_RDY, spi_sts);
276
277 /* Since the UART is disabled here, we delay printing the
278 * message until spi_cs_deactivate() is called.
279 */
280 if (tm >= SPI_TIMEOUT) {
281 static uint8_t err[256];
282 retval = -1;
283 snprintf(err, sizeof(err),
284 "%s():%d BSY&RDY timeout, status = 0x%08x\n",
285 __func__, __LINE__, status);
286 delayed_msg = err;
287 break;
288 }
289
290 /* read RX FIFO */
291 tmp = mmio_readl(rx_fifo);
292 for (i = 0; i < to_read; ++i) {
293 *readarr++ = (tmp >> ((to_read - 1 - i) * 8)) & 0xFF;
294 }
295 }
296
297 mmio_writel(status = mmio_readl(spi_sts), spi_sts);
298
299 spi_cs_deactivate();
300 if (delayed_msg) {
301 msg_perr("%s\n", delayed_msg);
302 }
303
304 return retval;
305 }
306
307
308 int tegra2_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
309 {
310 return spi_read_chunked(flash, buf, start, len, 256);
311 }
312
313
314 int tegra2_spi_write(struct flashchip *flash, uint8_t *buf, int start, int len)
315 {
316 return spi_write_chunked(flash, buf, start, len, 256);
317 }
318
319
320 /* Map register spaces */
321 int tegra2_spi_init(void)
322 {
323 u32 val;
324 int error = 0;
325 uint32_t *spi_cmd;
326 uint32_t *spi_sts;
327
328 gpio_base = physmap("GPIO", TEGRA2_GPIO_BASE, 4096);
329 clkrst_base = physmap("CLK/RST", NV_ADDRESS_MAP_PPSB_CLK_RST_BASE,
330 4096);
331 apbmisc_base = physmap("APB MISC", NV_ADDRESS_MAP_APB_MISC_BASE, 4096);
332 /* non-page offset */
333 spi_base = physmap("SPI", TEGRA2_SPI_BASE - 0x380, 4096) + 0x380;
334 register_shutdown(tegra2_spi_shutdown, NULL);
335
336 if (error)
337 return 1;
338
339 flashbase = 0; /* FIXME: to make sanity check happy. */
340 buses_supported = CHIP_BUSTYPE_SPI;
341 spi_controller = SPI_CONTROLLER_TEGRA2;
342
343 /* Init variables */
344 spi_cmd = (uint32_t *)spi_base;
345 spi_sts = (uint32_t *)(spi_base + 0x04);
346
347 register_tegra2_undo(clkrst_base + 0x08);
348 register_tegra2_undo(clkrst_base + 0x114);
349 register_tegra2_undo(clkrst_base + 0x14);
350 /*
351 * SPI reset/clocks init - reset SPI, set clocks, release from reset
352 */
353 val = mmio_readl(clkrst_base + 0x08);
354 mmio_writel((val | 0x800), (clkrst_base + 0x08));
355 msg_pdbg("%s: ClkRst = %08x\n", __func__, val);
356
357 val = mmio_readl(clkrst_base + 0x14);
358 mmio_writel((val | 0x800), (clkrst_base + 0x14));
359 msg_pdbg("%s: ClkEnable = %08x\n", __func__, val);
360
361 /* Change default SPI clock from 12MHz to 6MHz, same as BootROM */
362 val = mmio_readl(clkrst_base + 0x114);
363 mmio_writel((val | 0x2), (clkrst_base + 0x114));
364 msg_pdbg("%s: ClkSrc = %08x\n", __func__, val);
365
366 val = mmio_readl(clkrst_base + 0x08);
367 mmio_writel((val & 0xFFFFF7FF), (clkrst_base + 0x08));
368 msg_pdbg("%s: ClkRst final = %08x\n", __func__, val);
369
370 /* Clear stale status here */
371 mmio_writel(SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH |
372 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF, spi_sts);
373 msg_pdbg("%s: STATUS = %08x\n", __func__, mmio_readl(spi_sts));
374
375 /*
376 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
377 */
378 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_CS_SOFT, spi_cmd);
379 msg_pdbg("%s: COMMAND = %08x\n", __func__, mmio_readl(spi_cmd));
380
381 register_tegra2_undo(apbmisc_base + 0x84);
382 register_tegra2_undo(apbmisc_base + 0x20);
383 register_tegra2_undo(apbmisc_base + 0x88);
384 /*
385 * SPI pins on Tegra2 are muxed - change pinmux last due to UART issue
386 */
387 val = mmio_readl(apbmisc_base + 0x88);
388 val |= 0xC0000000;
389 mmio_writel(val, (apbmisc_base + 0x88));
390 msg_pdbg("%s: PinMuxRegC = %08x\n", __func__, val);
391
392 val = mmio_readl(apbmisc_base + 0x20);
393 val &= 0xFFFFFFFE;
394 mmio_writel(val, (apbmisc_base + 0x20));
395 msg_pdbg("%s: TriStateReg = %08x\n", __func__, val);
396
397 /* delay 100ms so that all chars in buffer (1KB) can be flushed. */
398 programmer_delay(100000);
399
400 /*
401 * We need to dynamically change the pinmux, shared w/UART RXD/CTS!
402 */
403 val = mmio_readl(apbmisc_base + 0x84);
404 val |= 0x0000000C; /* 3 = SFLASH */
405 mmio_writel(val, (apbmisc_base + 0x84));
406 msg_pdbg("%s: PinMuxRegB = %08x\n", __func__, val);
407
408 /* On Seaboard, MOSI/MISO are shared w/UART.
409 * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity.
410 * Enable UART later (cs_deactivate) so we can use it for U-Boot comms.
411 */
412 msg_pdbg("%s: DISABLING UART!\n", __func__);
413 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 1);
414
415 return 0;
416 }
417
418
419 /* Unmap register spaces */
420 void tegra2_spi_shutdown(void *not_used)
421 {
422 physunmap(gpio_base, 4096);
423 physunmap(clkrst_base, 4096);
424 physunmap(apbmisc_base, 4096);
425 physunmap(spi_base - 0x380, 4096);
426 }
427 #endif
OLDNEW
« no previous file with comments | « tegra2_spi.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698