OLD | NEW |
---|---|
(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 #include <assert.h> | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 | |
27 #include "flash.h" | |
28 #include "programmer.h" | |
29 #include "tegra2_spi.h" | |
30 | |
31 static void *gpio_base, *clkrst_base, *apbmisc_base, *spi_base; | |
32 int need_spi_clock_disable; | |
dhendrix
2011/03/25 04:34:43
you can remove the need_spi_clock_disable variable
Louis
2011/03/25 10:55:48
Done.
| |
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 | |
dhendrix
2011/03/25 04:34:43
2 extra newlines
Louis
2011/03/25 10:55:48
Done.
| |
88 | |
89 | |
90 /* set GPIO port:bit as an output, with polarity 'value' */ | |
91 static int tg2_gpio_direction_output(unsigned port, unsigned bit, int value) | |
92 { | |
93 msg_pdbg("%s: port = %d, bit = %d, value = %d\n", | |
94 __FUNCTION__, port, bit, value); | |
95 | |
96 /* Configure as a GPIO */ | |
97 __set_config(port, bit, 1); | |
98 | |
99 /* Configure GPIO output value. */ | |
100 __set_level(port, bit, value); | |
101 | |
102 /* Configure GPIO direction as output. */ | |
103 __set_direction(port, bit, 1); | |
104 | |
105 return 0; | |
106 } | |
107 | |
108 static void spi_init(void) | |
109 { | |
110 uint32_t *spi_cmd = (uint32_t *)spi_base; | |
111 uint32_t *spi_sts = (uint32_t *)(spi_base + 0x04); | |
112 u32 val; | |
113 | |
114 /* Enable UART via GPIO_PI3 (port 8, bit 3) so serial console works */ | |
115 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 0); | |
116 | |
117 /* | |
118 * SPI reset/clocks init - reset SPI, set clocks, release from reset | |
119 */ | |
120 val = mmio_readl(clkrst_base + 0x08); | |
121 mmio_writel((val | 0x800), (clkrst_base + 0x08)); | |
122 msg_pdbg("spi_init: ClkRst = %08x\n", val); | |
123 | |
124 val = mmio_readl(clkrst_base + 0x14); | |
125 mmio_writel((val | 0x800), (clkrst_base + 0x14)); | |
126 msg_pdbg("spi_init: ClkEnable = %08x\n", val); | |
127 | |
128 /* Change default SPI clock from 12MHz to 6MHz, same as BootROM */ | |
129 val = mmio_readl(clkrst_base + 0x114); | |
130 mmio_writel((val | 0x2), (clkrst_base + 0x114)); | |
131 msg_pdbg("spi_init: ClkSrc = %08x\n", val); | |
132 | |
133 val = mmio_readl(clkrst_base + 0x08); | |
134 mmio_writel((val & 0xFFFFF7FF), (clkrst_base + 0x08)); | |
135 msg_pdbg("spi_init: ClkRst final = %08x\n", val); | |
136 | |
137 /* Clear stale status here */ | |
138 | |
dhendrix
2011/03/25 04:34:43
extra newline
Louis
2011/03/25 10:55:48
Done.
| |
139 mmio_writel(SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | | |
140 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF, spi_sts); | |
141 msg_pdbg("%s: STATUS = %08x\n", __FUNCTION__, mmio_readl(spi_sts)); | |
142 | |
143 /* | |
144 * Use sw-controlled CS, so we can clock in data after ReadID, etc. | |
145 */ | |
146 | |
dhendrix
2011/03/25 04:34:43
extra newline
Louis
2011/03/25 10:55:48
Done.
| |
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 | |
dhendrix
2011/03/25 04:34:43
extra newline
Louis
2011/03/25 10:55:48
Done.
| |
154 val = mmio_readl(apbmisc_base + 0x88); | |
155 val |= 0xC0000000; | |
156 mmio_writel(val, (apbmisc_base + 0x88)); | |
157 msg_pdbg("spi_init: PinMuxRegC = %08x\n", val); | |
158 | |
159 val = mmio_readl(apbmisc_base + 0x20); | |
160 val &= 0xFFFFFFFE; | |
161 mmio_writel(val, (apbmisc_base + 0x20)); | |
162 msg_pdbg("spi_init: TriStateReg = %08x\n", val); | |
163 } | |
164 | |
dhendrix
2011/03/25 04:34:43
extra newline
Louis
2011/03/25 10:55:48
Done.
| |
165 | |
166 static void spi_cs_activate(void) | |
167 { | |
168 uint32_t *spi_cmd = (uint32_t *)spi_base; | |
169 u32 val; | |
170 | |
171 /* | |
172 * Delay here to clean up comms - spurious chars seen around SPI xfers. | |
173 * Fine-tune later. | |
174 */ | |
175 programmer_delay(1000); | |
176 | |
177 /* | |
178 * We need to dynamically change the pinmux, shared w/UART RXD/CTS! | |
179 */ | |
180 val = mmio_readl(apbmisc_base + 0x84); | |
181 val |= 0x0000000C; /* 3 = SFLASH */ | |
182 mmio_writel(val, (apbmisc_base + 0x84)); | |
183 msg_pdbg("%s: PinMuxRegB = %08x\n", __FUNCTION__, val); | |
184 | |
185 /* On Seaboard, MOSI/MISO are shared w/UART. | |
186 * Use GPIO I3 (UART_DISABLE) to tristate UART during SPI activity. | |
187 * Enable UART later (cs_deactivate) so we can use it for U-Boot comms. | |
188 */ | |
189 msg_pdbg("%s: DISABLING UART!\n", __FUNCTION__); | |
190 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 1); | |
191 | |
192 /* | |
193 * CS is negated on Tegra, so drive a 1 to get a 0 | |
194 */ | |
195 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_CS_VAL, spi_cmd); | |
196 msg_pdbg("%s: CS driven %s\n", __FUNCTION__, | |
197 (mmio_readl(spi_cmd) & SPI_CMD_CS_VAL) ? "LOW" : "HIGH"); | |
198 } | |
199 | |
200 static void spi_cs_deactivate(void) | |
201 { | |
202 uint32_t *spi_cmd = (uint32_t *)spi_base; | |
203 u32 val; | |
204 | |
205 /* | |
206 * Delay here to clean up comms - spurious chars seen around SPI xfers. | |
207 * Fine-tune later. | |
208 */ | |
209 programmer_delay(1000); | |
210 | |
211 /* | |
212 * Looks like we may also need to dynamically change the pinmux, | |
213 * shared w/UART RXD/CTS! | |
214 */ | |
215 val = mmio_readl(apbmisc_base + 0x84); | |
216 val &= 0xFFFFFFF3; /* 0 = UART */ | |
217 mmio_writel(val, (apbmisc_base + 0x84)); | |
218 msg_pdbg("%s: PinMuxRegB = %08x\n", __FUNCTION__, val); | |
219 | |
220 /* On Seaboard, MOSI/MISO are shared w/UART. | |
221 * GPIO I3 (UART_DISABLE) was used to tristate UART in cs_activate. | |
222 * Enable UART here by setting that GPIO to 0 so we can do U-Boot comms. | |
223 */ | |
224 tg2_gpio_direction_output(UART_DISABLE_PORT, UART_DISABLE_BIT, 0); | |
225 msg_pdbg("%s: ENABLED UART!\n", __FUNCTION__); | |
226 | |
227 /* | |
228 * CS is negated on Tegra, so drive a 0 to get a 1 | |
229 */ | |
230 mmio_writel(mmio_readl(spi_cmd) & ~SPI_CMD_CS_VAL, spi_cmd); | |
231 msg_pdbg("%s: CS driven %s\n", __FUNCTION__, | |
232 (mmio_readl(spi_cmd) & SPI_CMD_CS_VAL) ? "LOW" : "HIGH"); | |
233 } | |
234 | |
235 | |
236 /* Helper function to calculate the clock cycle in this round. | |
237 * Also upates the byte counts reminded and to be consumed in this round. | |
dhendrix
2011/03/25 04:34:43
suggest alternative wording: "Also updates the byt
Louis
2011/03/25 10:55:48
Done.
| |
238 * | |
239 * For example, we want to write 5 bytes to SPI and then read 6 bytes back. | |
240 * | |
241 * +---+---+---+---+---+ | |
242 * | W | W | W | W | W | | |
243 * +---+---+---+---+---+---+---+---+---+---+---+ | |
244 * | R | R | R | R | R | R | | |
245 * +---+---+---+---+---+---+ | |
246 * |<-- round 0 -->| | |
247 * |<->| round 1 | |
248 * | | |
249 * |<-- round 2 -->| | |
250 * |<-- -->| | |
251 * round 3 | |
252 * | |
253 * So that the continuous calling this function would get: | |
254 * | |
255 * round| RET| writecnt readcnt bits to_write to_read | |
256 * -----+----+--------------------------------------------- | |
257 * INIT | | 5 6 | |
258 * 0 | 1 | 1 0 32 4 0 | |
259 * 1 | 1 | 0 6 8 1 0 | |
260 * 2 | 1 | 0 2 32 0 4 | |
261 * 3 | 1 | 0 0 16 0 2 | |
262 * 4 | 0 | - - - - - | |
263 * | |
264 */ | |
265 int next4Bytes(uint32_t *writecnt, uint32_t *readcnt, int *bits, | |
dhendrix
2011/03/25 04:34:43
I recommend renaming bits to something like num_bi
Louis
2011/03/25 10:55:48
Done.
| |
266 uint32_t *to_write, uint32_t *to_read) { | |
267 assert(writecnt); | |
268 assert(readcnt); | |
269 assert(bits); | |
270 assert(to_write); | |
271 assert(to_read); | |
272 | |
273 *to_write = min(*writecnt, 4); | |
274 if (*to_write == 0) { | |
275 *to_read = min(*readcnt, 4 - *to_write); | |
dhendrix
2011/03/25 04:34:43
min(*readcnt, 4) since *to_write is 0.
Louis
2011/03/25 10:55:48
This case has been optimized and refined. :-)
On
| |
276 } else { | |
277 *to_read = 0; | |
278 } | |
279 | |
280 *writecnt -= *to_write; | |
281 *readcnt -= *to_read; | |
282 | |
283 *bits = (*to_write + *to_read) * 8; | |
284 | |
285 if (*bits) | |
286 return 1; /* need to be called again. */ | |
287 else | |
288 return 0; /* handled write and read requests. */ | |
dhendrix
2011/03/25 04:34:43
Maybe just return *bits? The loop in tegra2_spi_se
Louis
2011/03/25 10:55:48
Intentionally separate them because the meanings a
| |
289 } | |
290 | |
291 /* | |
292 * Tegra2 FIFO design is ... interesting. For example, you want to Tx 2 bytes: | |
dhendrix
2011/03/25 04:34:43
LOL! Excellent comment :-)
Louis
2011/03/25 10:55:48
Done.
| |
293 * | |
294 * +---+---+ | |
295 * writearr[]: | 0 | 1 | | |
296 * +---+---+ | |
297 * \ \ | |
298 * \ \ | |
299 * \ \ | |
300 * \ \ | |
301 * \ \ | |
302 * 31 +---+---+---+---+ 0 | |
303 * tmp(32-bits): | X | X | 0 | 1 | | |
304 * +---+---+---+---+ LSB | |
305 * | |
306 * It is neither little or big endian. The first bit for SPI controller to | |
307 * transfer is the bit 15 in FIFO, neither bit 31 or bit 0, because the transfer | |
308 * length is 16 bits (2 bytes). | |
309 * | |
310 * Rx follows the similar rule. First bit comes at bit 0, and the whole FIFO | |
311 * left-shifts 1 bit for every bit comes in. Hence, after reading 3 byes, | |
dhendrix
2011/03/25 04:34:43
s/byes/bytes
Louis
2011/03/25 10:55:48
Done.
| |
312 * the first coming bit will reside in bit 23. | |
313 */ | |
314 int tegra2_spi_send_command(unsigned int writecnt, | |
315 unsigned int readcnt, | |
316 const unsigned char *writearr, | |
317 unsigned char *readarr) | |
318 { | |
319 int retval = 0; | |
320 uint8_t *delayed_msg = NULL; /* for UART is disabled. */ | |
321 uint32_t *spi_cmd = (uint32_t *)spi_base; | |
322 uint32_t *spi_sts = (uint32_t *)(spi_base + 0x04); | |
323 uint32_t *tx_fifo = (uint32_t *)(spi_base + 0x10); | |
324 uint32_t *rx_fifo = (uint32_t *)(spi_base + 0x20); | |
325 uint32_t status; | |
dhendrix
2011/03/25 04:34:43
The "status" variable isn't really used anymore --
Louis
2011/03/25 10:55:48
It is used in line 365 for snprintf delayed_msg.
O
| |
326 uint32_t to_write, to_read; /* byte counts to fill FIFO. */ | |
327 uint32_t bits; /* bit count to tell SPI controller. */ | |
328 | |
329 mmio_writel(mmio_readl(spi_sts), spi_sts); | |
330 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_TXEN | SPI_CMD_RXEN, spi_cmd); | |
331 spi_cs_activate(); | |
332 | |
333 while (next4Bytes(&writecnt, &readcnt, &bits, &to_write, &to_read)) { | |
334 int i; | |
335 uint32_t tmp; | |
336 uint32_t tm; /* timeout counter */ | |
337 | |
338 /* prepare Tx FIFO */ | |
339 for (tmp = 0, i = 0; i < to_write; ++i) { | |
340 tmp |= (*writearr++) << ((to_write - 1 - i) * 8); | |
341 } | |
342 mmio_writel(tmp, tx_fifo); | |
343 | |
344 /* Kick the SCLK running: Shift out TX FIFO, and receive RX. */ | |
345 mmio_writel(mmio_readl(spi_cmd) & ~SPI_CMD_BIT_LENGTH_MASK, | |
346 spi_cmd); | |
347 mmio_writel(mmio_readl(spi_cmd) | (bits - 1), | |
348 spi_cmd); | |
349 mmio_writel(mmio_readl(spi_cmd) | SPI_CMD_GO, spi_cmd); | |
350 | |
351 /* Wait for controller completes the task. */ | |
352 for (tm = 0; | |
353 tm < SPI_TIMEOUT && | |
354 ((status = mmio_readl(spi_sts)) & | |
355 (SPI_STAT_BSY | SPI_STAT_RDY)) != SPI_STAT_RDY; | |
dhendrix
2011/03/25 04:34:43
heh, this is getting a little bit too complicated
Louis
2011/03/25 10:55:48
Good suggestion. Thanks.
On 2011/03/25 04:34:43,
| |
356 ++tm) { | |
357 programmer_delay(10); | |
358 } | |
359 mmio_writel(mmio_readl(spi_sts) | SPI_STAT_RDY, spi_sts); | |
360 if (tm >= SPI_TIMEOUT) { | |
dhendrix
2011/03/25 04:34:43
Is this necessary as to avoid using the UART? Mayb
Louis
2011/03/25 10:55:48
Ah... I just think more for future if we need to a
| |
361 static uint8_t err[256]; | |
362 retval = -1; | |
363 snprintf(err, sizeof(err), | |
364 "%s():%d BSY&RDY timeout, status = 0x%08x\n", | |
365 __FUNCTION__, __LINE__, status); | |
366 delayed_msg = err; | |
367 break; | |
368 } | |
369 | |
370 /* read RX FIFO */ | |
371 tmp = mmio_readl(rx_fifo); | |
372 for (i = 0; i < to_read; ++i) { | |
373 *readarr++ = (tmp >> ((to_read - 1 - i) * 8)) & 0xFF; | |
374 } | |
375 } | |
376 | |
377 mmio_writel(status = mmio_readl(spi_sts), spi_sts); | |
378 | |
379 spi_cs_deactivate(); | |
380 if (delayed_msg) { | |
381 msg_perr("%s\n", delayed_msg); | |
382 } | |
383 | |
384 return retval; | |
385 } | |
386 | |
387 | |
388 int tegra2_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) | |
389 { | |
390 return spi_read_chunked(flash, buf, start, len, 256); | |
391 } | |
392 | |
393 | |
394 int tegra2_spi_write(struct flashchip *flash, uint8_t *buf, int start, int len) | |
395 { | |
396 return spi_write_chunked(flash, buf, start, len, 256); | |
397 } | |
398 | |
399 | |
400 int tegra2_spi_init(void) | |
401 { | |
402 int error = 0; | |
403 uint32_t *clkrst_enb; | |
404 | |
405 gpio_base = physmap("GPIO", TEGRA2_GPIO_BASE, 4096); | |
406 clkrst_base = physmap("CLK/RST", NV_ADDRESS_MAP_PPSB_CLK_RST_BASE, 4096) ; | |
407 apbmisc_base = physmap("APB MISC", NV_ADDRESS_MAP_APB_MISC_BASE, 4096); | |
408 /* non-page offset */ | |
409 spi_base = physmap("SPI", TEGRA2_SPI_BASE - 0x380, 4096) + 0x380; | |
410 | |
411 if (error) | |
412 return 1; | |
413 | |
414 flashbase = 0; /* FIXME: to make sanity check happy. */ | |
415 buses_supported = CHIP_BUSTYPE_SPI; | |
416 spi_controller = SPI_CONTROLLER_TEGRA2; | |
417 | |
418 spi_init(); | |
419 | |
420 /* Enable SPI 1 clock */ | |
421 clkrst_enb = (uint32_t *)(clkrst_base + CLK_RST_ENB_H_0_OFFSET); | |
dhendrix
2011/03/25 04:34:43
You can remove this part. It is no longer needed s
Louis
2011/03/25 10:55:48
Done.
| |
422 if (!(mmio_readl(clkrst_enb) & CLK_RST_ENB_H_0_SPI1)) { | |
423 msg_pdbg("%s: enabling SPI 1 clock\n", __func__); | |
424 mmio_writel(mmio_readl(clkrst_enb) | CLK_RST_ENB_H_0_SPI1, | |
425 clkrst_enb); | |
426 need_spi_clock_disable = 1; | |
427 } | |
428 | |
429 return 0; | |
430 } | |
431 | |
432 | |
433 int tegra2_spi_shutdown(void) | |
434 { | |
435 if (need_spi_clock_disable) { | |
dhendrix
2011/03/25 04:34:43
You can remove this part.
Perhaps we should make
Louis
2011/03/25 10:55:48
Done.
| |
436 uint32_t *clkrst_enb = (uint32_t *)(clkrst_base + | |
437 CLK_RST_ENB_H_0_OFFSET); | |
438 | |
439 msg_pdbg("%s: disabling SPI 1 clock\n", __func__); | |
440 mmio_writel(mmio_readl(clkrst_enb) & ~CLK_RST_ENB_H_0_SPI1, | |
441 clkrst_enb); | |
442 need_spi_clock_disable = 0; | |
443 } | |
444 | |
445 physunmap(gpio_base, 4096); | |
446 physunmap(clkrst_base, 4096); | |
447 physunmap(apbmisc_base, 4096); | |
448 physunmap(spi_base - 0x380, 4096); | |
449 | |
450 return 0; | |
451 } | |
OLD | NEW |