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

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: refine according to code review. Created 9 years, 9 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
« tegra2_spi.h ('K') | « 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 10000 /* 100ms = 10000 * 10us */
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 /* Config port:bit as GPIO, not SFPIO (default) */
41 static void __set_config(unsigned port, unsigned bit, int type)
42 {
43 u32 u;
44
45 msg_pdbg("%s: port = %d, bit = %d, %s\n", __FUNCTION__,
46 port, bit, type ? "GPIO" : "SFPIO");
47
48 u = mmio_readl(GPIO_CNF(port));
49 if (type) /* GPIO */
50 u |= 1 << bit;
51 else
52 u &= ~(1 << bit);
53 mmio_writel(u, GPIO_CNF(port));
54 }
55
56 /* Config GPIO port:bit as input or output (OE) */
57 static void __set_direction(unsigned port, unsigned bit, int output)
58 {
59 u32 u;
60
61 msg_pdbg("%s: port = %d, bit = %d, %s\n", __FUNCTION__,
62 port, bit, output ? "OUT" : "IN");
63
64 u = mmio_readl(GPIO_OE(port));
65 if (output)
66 u |= 1 << bit;
67 else
68 u &= ~(1 << bit);
69 mmio_writel(u, GPIO_OE(port));
70 }
71
72 /* set GPIO OUT port:bit as 0 or 1 */
73 static void __set_level(unsigned port, unsigned bit, int high)
74 {
75 u32 u;
76
77 msg_pdbg("%s: port = %d, bit %d == %d\n", __FUNCTION__,
78 port, bit, high);
79
80 u = mmio_readl(GPIO_OUT(port));
81 if (high)
82 u |= 1 << bit;
83 else
84 u &= ~(1 << bit);
85 mmio_writel(u, GPIO_OUT(port));
86 }
87
88
89 /* set GPIO port:bit as an output, with polarity 'value' */
90 static int tg2_gpio_direction_output(unsigned port, unsigned bit, int value)
91 {
92 msg_pdbg("%s: port = %d, bit = %d, value = %d\n",
93 __FUNCTION__, port, bit, value);
94
95 /* Configure as a GPIO */
96 __set_config(port, bit, 1);
97
98 /* Configure GPIO output value. */
99 __set_level(port, bit, value);
100
101 /* Configure GPIO direction as output. */
102 __set_direction(port, bit, 1);
103
104 return 0;
105 }
106
107
108 /* SPI controller register related init code. */
109 static void spi_init(void)
110 {
111 uint32_t *spi_cmd = (uint32_t *)spi_base;
112 uint32_t *spi_sts = (uint32_t *)(spi_base + 0x04);
113 uint32_t *clkrst_enb;
114 u32 val;
115
116 /* Enable UART via GPIO_PI3 (port 8, bit 3) so serial console works */
117 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 0);
118
119 /*
120 * SPI reset/clocks init - reset SPI, set clocks, release from reset
121 */
122 val = mmio_readl(clkrst_base + 0x08);
123 mmio_writel((val | 0x800), (clkrst_base + 0x08));
124 msg_pdbg("spi_init: ClkRst = %08x\n", val);
125
126 val = mmio_readl(clkrst_base + 0x14);
127 mmio_writel((val | 0x800), (clkrst_base + 0x14));
128 msg_pdbg("spi_init: ClkEnable = %08x\n", val);
129
130 /* Change default SPI clock from 12MHz to 6MHz, same as BootROM */
131 val = mmio_readl(clkrst_base + 0x114);
132 mmio_writel((val | 0x2), (clkrst_base + 0x114));
133 msg_pdbg("spi_init: ClkSrc = %08x\n", val);
134
135 val = mmio_readl(clkrst_base + 0x08);
136 mmio_writel((val & 0xFFFFF7FF), (clkrst_base + 0x08));
137 msg_pdbg("spi_init: ClkRst final = %08x\n", val);
138
139 /* Clear stale status here */
140 mmio_writel(SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH |
141 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF, spi_sts);
142 msg_pdbg("%s: STATUS = %08x\n", __FUNCTION__, mmio_readl(spi_sts));
143
144 /*
145 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
146 */
147 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_CS_SOFT, spi_cmd);
148 msg_pdbg("%s: COMMAND = %08x\n", __FUNCTION__, mmio_readl(spi_cmd));
149
150 /*
151 * SPI pins on Tegra2 are muxed - change pinmux last due to UART issue
152 */
153 val = mmio_readl(apbmisc_base + 0x88);
154 val |= 0xC0000000;
155 mmio_writel(val, (apbmisc_base + 0x88));
156 msg_pdbg("spi_init: PinMuxRegC = %08x\n", val);
157
158 val = mmio_readl(apbmisc_base + 0x20);
159 val &= 0xFFFFFFFE;
160 mmio_writel(val, (apbmisc_base + 0x20));
161 msg_pdbg("spi_init: TriStateReg = %08x\n", val);
162
163 /* delay 100ms so that all chars in buffer (1KB) can be flushed. */
164 programmer_delay(100000);
Micah C 2011/03/28 02:08:22 This is a big delay. Is there any other signal th
Louis 2011/03/30 10:15:39 I don't know a signal for this. Actually, this is
165
166 /*
167 * We need to dynamically change the pinmux, shared w/UART RXD/CTS!
168 */
169 val = mmio_readl(apbmisc_base + 0x84);
170 val |= 0x0000000C; /* 3 = SFLASH */
171 mmio_writel(val, (apbmisc_base + 0x84));
172 msg_pdbg("%s: PinMuxRegB = %08x\n", __FUNCTION__, val);
173
174 /* On Seaboard, MOSI/MISO are shared w/UART.
175 * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity.
176 * Enable UART later (cs_deactivate) so we can use it for U-Boot comms.
177 */
178 msg_pdbg("%s: DISABLING UART!\n", __FUNCTION__);
179 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 1);
180
181 /* wait for UART Tx FIFO flushed. */
182 programmer_delay(2000);
Micah C 2011/03/28 02:08:22 Is there some status register that can detect UART
Louis 2011/03/30 10:15:39 This is not necessary because nothing will be send
183 }
184
185 /* SPI controller register related shutdown code. */
186 static void spi_shutdown(void) {
187 u32 val;
188
189 /*
190 * Looks like we may also need to dynamically change the pinmux,
Micah C 2011/03/28 02:08:22 It might be a good idea to create functions like "
Louis 2011/03/30 10:15:39 Done. Call register_shutdown() to restore all chan
191 * shared w/UART RXD/CTS!
192 */
193 val = mmio_readl(apbmisc_base + 0x84);
194 val &= 0xFFFFFFF3; /* 0 = UART */
195 mmio_writel(val, (apbmisc_base + 0x84));
196 msg_pdbg("%s: PinMuxRegB = %08x\n", __FUNCTION__, val);
197
198 /* On Seaboard, MOSI/MISO are shared w/UART.
199 * GPIO I3 (UART_DISABLE) was used to tristate UART in cs_activate.
200 * Enable UART here by setting that GPIO to 0 so we can do U-Boot comms.
201 */
202 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 0);
203 msg_pdbg("%s: ENABLED UART!\n", __FUNCTION__);
204 }
205
206
207 static void spi_cs_activate(void)
208 {
209 uint32_t *spi_cmd = (uint32_t *)spi_base;
210
211 /*
212 * CS is negated on Tegra, so drive a 1 to get a 0
213 */
214 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_CS_VAL, spi_cmd);
215 msg_pdbg("%s: CS driven %s\n", __FUNCTION__,
216 (mmio_readl(spi_cmd) & SPI_CMD_CS_VAL) ? "LOW" : "HIGH");
217 }
218
219 static void spi_cs_deactivate(void)
220 {
221 uint32_t *spi_cmd = (uint32_t *)spi_base;
222
223
224 /*
225 * CS is negated on Tegra, so drive a 0 to get a 1
226 */
227 mmio_writel(mmio_readl(spi_cmd) & ~SPI_CMD_CS_VAL, spi_cmd);
228 msg_pdbg("%s: CS driven %s\n", __FUNCTION__,
229 (mmio_readl(spi_cmd) & SPI_CMD_CS_VAL) ? "LOW" : "HIGH");
230 }
231
232
233 /* Helper function to calculate the clock cycle in this round.
234 * Also updates the byte count remaining to be used this round.
235 *
236 * For example, we want to write 6 bytes to SPI and then read 5 bytes back.
237 *
238 * +---+---+---+---+---+---+
239 * | W | W | W | W | W | W |
240 * +---+---+---+---+---+---+---+---+---+---+---+
241 * | R | R | R | R | R |
242 * +---+---+---+---+---+
243 * |<-- round 0 -->|
244 * |<-- round 1 -->|
245 * |<-- round 2 -->|
246 *
247 * So that the continuous calling this function would get:
248 *
249 * round| RET| writecnt readcnt bits to_write to_read
250 * -----+----+---------------------------------------------
251 * INIT | | 6 5
252 * 0 | 1 | 2 5 32 4 0
253 * 1 | 1 | 0 3 32 2 2
254 * 2 | 1 | 0 0 24 0 3
255 * 3 | 0 | - - - - -
256 *
257 */
258 int next4Bytes(uint32_t *writecnt, uint32_t *readcnt, int *num_bits,
259 uint32_t *to_write, uint32_t *to_read) {
260 assert(writecnt);
261 assert(readcnt);
262 assert(num_bits);
263 assert(to_write);
264 assert(to_read);
265
266 *to_write = min(*writecnt, 4);
267 *to_read = min(*readcnt, 4 - *to_write);
268
269 *writecnt -= *to_write;
270 *readcnt -= *to_read;
271
272 *num_bits = (*to_write + *to_read) * 8;
273
274 if (*num_bits)
275 return 1; /* need to be called again. */
276 else
277 return 0; /* handled write and read requests. */
278 }
279
280 /*
281 * Tegra2 FIFO design is ... interesting. For example, you want to Tx 2 bytes:
282 *
283 * +---+---+
284 * writearr[]: | 0 | 1 |
285 * +---+---+
286 * \ \
287 * \ \
288 * \ \
289 * \ \
290 * \ \
291 * 31 +---+---+---+---+ 0
292 * tmp(32-bits): | X | X | 0 | 1 |
293 * +---+---+---+---+ LSB
294 *
295 * It is neither little or big endian. The first bit for SPI controller to
296 * transfer is the bit 15 in FIFO, neither bit 31 or bit 0, because the transfer
297 * length is 16 bits (2 bytes).
298 *
299 * Rx follows the similar rule. First bit comes at bit 0, and the whole FIFO
300 * left-shifts 1 bit for every bit comes in. Hence, after reading 3 bytes,
301 * the first coming bit will reside in bit 23.
302 */
303 int tegra2_spi_send_command(unsigned int writecnt,
304 unsigned int readcnt,
305 const unsigned char *writearr,
306 unsigned char *readarr)
307 {
308 int retval = 0;
309 uint8_t *delayed_msg = NULL; /* for UART is disabled. */
310 uint32_t *spi_cmd = (uint32_t *)spi_base;
311 uint32_t *spi_sts = (uint32_t *)(spi_base + 0x04);
312 uint32_t *tx_fifo = (uint32_t *)(spi_base + 0x10);
313 uint32_t *rx_fifo = (uint32_t *)(spi_base + 0x20);
314 uint32_t status;
315 uint32_t to_write, to_read; /* byte counts to fill FIFO. */
316 uint32_t bits; /* bit count to tell SPI controller. */
317
318 mmio_writel(mmio_readl(spi_sts), spi_sts);
319 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_TXEN | SPI_CMD_RXEN, spi_cmd);
320 spi_cs_activate();
321
322 while (next4Bytes(&writecnt, &readcnt, &bits, &to_write, &to_read)) {
323 int i;
324 uint32_t tmp;
325 uint32_t tm; /* timeout counter */
326
327 /* prepare Tx FIFO */
328 for (tmp = 0, i = 0; i < to_write; ++i) {
329 tmp |= (*writearr++) << ((bits / 8 - 1 - i) * 8);
330 }
331 mmio_writel(tmp, tx_fifo);
332
333 /* Kick the SCLK running: Shift out TX FIFO, and receive RX. */
334 mmio_writel(mmio_readl(spi_cmd) & ~SPI_CMD_BIT_LENGTH_MASK,
335 spi_cmd);
336 mmio_writel(mmio_readl(spi_cmd) | (bits - 1),
337 spi_cmd);
338 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_GO, spi_cmd);
339
340 /* Wait for controller completes the task. */
341 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
342 if (((status = mmio_readl(spi_sts)) &
343 (SPI_STAT_BSY | SPI_STAT_RDY)) == SPI_STAT_RDY)
344 break;
345 programmer_delay(10);
Micah C 2011/03/28 02:08:22 Is this "10" the same 10us that's multiplied into
Louis 2011/03/30 10:15:39 Good catch. I refined this to 2us (roughly larger
346 }
347 mmio_writel(mmio_readl(spi_sts) | SPI_STAT_RDY, spi_sts);
348
349 /* Since the UART is disabled here, we delay printing the
350 * message until spi_cs_deactivate() is called.
351 */
352 if (tm >= SPI_TIMEOUT) {
353 static uint8_t err[256];
354 retval = -1;
355 snprintf(err, sizeof(err),
356 "%s():%d BSY&RDY timeout, status = 0x%08x\n",
357 __FUNCTION__, __LINE__, status);
358 delayed_msg = err;
359 break;
360 }
361
362 /* read RX FIFO */
363 tmp = mmio_readl(rx_fifo);
364 for (i = 0; i < to_read; ++i) {
365 *readarr++ = (tmp >> ((to_read - 1 - i) * 8)) & 0xFF;
366 }
367 }
368
369 mmio_writel(status = mmio_readl(spi_sts), spi_sts);
370
371 spi_cs_deactivate();
372 if (delayed_msg) {
373 msg_perr("%s\n", delayed_msg);
374 }
375
376 return retval;
377 }
378
379
380 int tegra2_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
381 {
382 return spi_read_chunked(flash, buf, start, len, 256);
383 }
384
385
386 int tegra2_spi_write(struct flashchip *flash, uint8_t *buf, int start, int len)
387 {
388 return spi_write_chunked(flash, buf, start, len, 256);
389 }
390
391
392 /* Map register spaces */
393 int tegra2_spi_init(void)
394 {
395 int error = 0;
396
397 gpio_base = physmap("GPIO", TEGRA2_GPIO_BASE, 4096);
398 clkrst_base = physmap("CLK/RST", NV_ADDRESS_MAP_PPSB_CLK_RST_BASE,
399 4096);
400 apbmisc_base = physmap("APB MISC", NV_ADDRESS_MAP_APB_MISC_BASE, 4096);
401 /* non-page offset */
402 spi_base = physmap("SPI", TEGRA2_SPI_BASE - 0x380, 4096) + 0x380;
403
404 if (error)
405 return 1;
406
407 flashbase = 0; /* FIXME: to make sanity check happy. */
408 buses_supported = CHIP_BUSTYPE_SPI;
409 spi_controller = SPI_CONTROLLER_TEGRA2;
410
411 spi_init();
Stefan Reinauer 2011/03/25 17:22:03 It seems spi_init() could just be rolled into this
Louis 2011/03/30 10:15:39 Done.
412
413 return 0;
414 }
415
416
417 /* Unmap register spaces */
418 int tegra2_spi_shutdown(void)
419 {
420 spi_shutdown();
Stefan Reinauer 2011/03/25 17:22:03 Same applies for tegra2_spi_shutdown() and spi_shu
Louis 2011/03/30 10:15:39 Done.
421
422 physunmap(gpio_base, 4096);
423 physunmap(clkrst_base, 4096);
424 physunmap(apbmisc_base, 4096);
425 physunmap(spi_base - 0x380, 4096);
426
427 return 0;
428 }
429 #endif
OLDNEW
« tegra2_spi.h ('K') | « tegra2_spi.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698