SPI
The FPGC has six SPI buses, one per device on the board to allow independent operation. Each bus has its own pins and its own controller, so the devices never share a bus or wait for each other. Software reaches each one through five registers, and sends the device’s commands itself.
The buses
| Bus | Address | Device | Clock | Quad | Read-ahead | Interrupt |
|---|---|---|---|---|---|---|
| flash 1 | 0xFFFF8400 | W25Q128JV, 16 MiB: to boot from | 50 MHz | yes | yes | none |
| flash 2 | 0xFFFF8500 | W25Q128JV, 16 MiB: for storage | 50 MHz | yes | yes | none |
| USB 1 | 0xFFFF8600 | CH376T, USB host | 12.5 MHz | no | no | 3 |
| USB 2 | 0xFFFF8700 | CH376T, USB host | 12.5 MHz | no | no | 4 |
| Ethernet | 0xFFFF8800 | ENC28J60 | 12.5 MHz | no | no | 5 |
| SD card | 0xFFFF8900 | micro SD card, in SPI mode | 25 MHz | no | no | none |
- Every bus uses SPI mode 0: the clock is low between transfers, a device takes a bit at the rising edge, and changes its bit at the falling edge. Bytes go most significant bit first.
- Each bus has a fixed clock, set for its device. The ENC28J60 needs at least 8 MHz and at most 20 MHz, and the CH376T at most about 23 MHz, so both run at 12.5 MHz. The SD card runs at 25 MHz from its first command, also during its initialization, for which the SD specification asks for 400 kHz at most. While this is out of spec, the SD cards I tested worked fine with this.
Registers
The same five registers on every bus, from the bus’s address:
| Offset | Register | Read | Write |
|---|---|---|---|
+0x0 | BYTE | receives a byte, in bits [7:0] | sends bits [7:0] |
+0x4 | WORD | receives 4 bytes, the first in bits [7:0] | sends 4 bytes, bits [7:0] first |
+0x8 | CONTROL | the bits below | sets the bits below |
+0xC | STATUS | bit 0: the device’s interrupt pin is active | ignored |
+0x10 | INTERRUPT | bit 0: the interrupt is enabled | bit 0: enable the interrupt (1) or not (0) |
| CONTROL bit | Meaning |
|---|---|
| 0 | SELECT: the chip select is active (low). 0 after a reset. |
| 1 | QUAD: receives use four data lines. Only on the flashes; on the other buses it stays 0. |
STATUS follows the interrupt pins of the USB and Ethernet chips, through a synchronizer: 1 while the chip asks for attention. On the other buses, STATUS is 0. With INTERRUPT, the pin also interrupts the CPU: see interrupts.
Transfers
A transfer either sends or receives:
- A send puts the byte or the word on the bus, and ignores what comes back. A WORD write sends bits [7:0] first, then [15:8], [23:16] and [31:24], so a word goes to the device in the order it has in memory.
- A receive sends 1s (
0xFF) and keeps what comes back. A WORD read puts the first byte in bits [7:0]. None of the devices needs to send and receive in the same byte, and the SD card needs 1s while it’s read.
Every access waits until the transfer before it is done, so a program never polls:
- A write of BYTE or WORD is answered when its transfer starts. The program goes on while the byte goes out.
- A read of BYTE or WORD is answered when its own transfer is done.
- A write of CONTROL waits until the last transfer is done, so the chip select never changes in the middle of a byte.
| Transfer | Clocks | At 50 MHz | At 25 MHz | At 12.5 MHz |
|---|---|---|---|---|
| a byte | 8 | 160 ns | 320 ns | 640 ns |
| a word | 32 | 640 ns | 1.28 µs | 2.56 µs |
| a byte with QUAD | 2 | 40 ns | ||
| a word with QUAD | 8 | 160 ns |
Transfers also run with the chip select inactive. That’s how the SD card gets its clocks at power-up (see below).
A flash’s JEDEC ID, three bytes:
load32 0xFFFF8400 r10 ; flash 1
load 1 r1
write 8 r10 r1 ; CONTROL: select
load 0x9F r1
write 0 r10 r1 ; BYTE: send 9Fh
read 0 r10 r2 ; BYTE: EFh
read 0 r10 r3 ; BYTE: 40h
read 0 r10 r4 ; BYTE: 18h
write 8 r10 r0 ; CONTROL: deselectRules of the devices
The controller only clocks bytes. What the devices need besides that is up to the program:
| Device | What the program does |
|---|---|
| flash | keeps the chip select inactive for at least 5 cycles (50 ns) after a program, an erase or a Write Enable, before the next command |
| ENC28J60 | keeps the chip select inactive for at least 50 ns between commands, and active for 210 ns after the last byte of a MAC or MII register; reads a dummy byte before the data of a MAC or MII register |
| CH376T | waits about 1.5 µs after every byte before the next |
| SD card | sends 80 clocks with the chip select inactive after power-up; reads 0xFF until an answer comes |
Quad reads
The flashes can send data on all four data lines with Fast Read Quad Output (6Bh): the command and the address go out on one line, as with every command, and only the data comes back on four, two clocks per byte. With QUAD set, the FPGA stops driving all four lines, and receives take four bits per clock.
| Step | Access | On the bus |
|---|---|---|
| 1 | CONTROL = 1 | the chip select goes low |
| 2 | WORD = 0x6B and the address, high byte first | the command and the address, 32 clocks |
| 3 | CONTROL = 3 | QUAD: the FPGA lets go of the lines |
| 4 | a read of WORD, thrown away | the 8 dummy clocks of 6Bh |
| 5 | reads of WORD | 8 clocks per word |
| 6 | CONTROL = 0 | the chip select goes high, QUAD off |
For step 2, the address’s bytes go in the other order: the command and address 0x123456 are the word 0x5634126B.
Read-ahead
On the flashes, a WORD read starts a stream: the controller receives the next words on its own, until it holds two. The next WORD read takes the first of them at once, and the controller receives another one. So while the program handles one word, the next one is already coming in, and the bus is hardly ever idle.
program read w1 . . . . . . . . . . read w2 read w3 read w4
bus [ w1 ][ w2 ][ w3 ] [ w4 ] [ w5 ] [ w6 ]
^ two words held: the bus waitsA stream ends at anything else on the bus: a BYTE read, a write of BYTE or WORD, or a write of CONTROL. That access first waits until the controller holds its two words, and then drops them. The flash has already sent them, so:
- In one command, read either words or bytes, not both. A byte read after words gets the byte after the two dropped words.
- For a 6Bh read that goes on with bytes, the 8 dummy clocks are four quad BYTE reads instead of one quad WORD read, so they don’t start a stream.
- A deselect at the end of a word stream costs the time of the two words it waits for.
The other buses don’t read ahead: on the SD card, the ENC28J60 and the CH376T, reading moves on in a buffer, so a word read too early would be lost.
Interrupts
The USB and Ethernet chips each have an interrupt pin, and their buses are interrupts 3, 4 and 5. They work like every interrupt on the I/O devices page: STATUS is the flag, INTERRUPT the enable, and the bus interrupts when the pin becomes active while the interrupt is enabled. INTERRUPT is 0 after a reset.
The handler releases the pin through the chip itself, with an SPI command:
| Chip | It pulls the pin | The handler releases it by |
|---|---|---|
| CH376T | when a command is done, or a USB device connects or disconnects | reading the reason with GET_STATUS (22h) |
| ENC28J60 | when a flag in EIR is set, its enable in EIE is set, and INTIE in EIE is set | clearing the flag in EIR |
- Until the pin is released, the bus can’t interrupt again.
- An access to a bus in the handler would get in the middle of a command that the interrupted program sends to the same chip. So either the program disables the bus’s interrupt while it sends a command, or the handler only writes 0 to INTERRUPT and leaves the chip to the program.
- INTERRUPT doesn’t wait for a transfer, and doesn’t change anything on the bus.
- On the flashes and the SD card, INTERRUPT keeps its bit, but never gives an interrupt.
Flash performance
Flash performance varies a lot depending on whether you are reading or writing, and on the type of command used for reading.
Reading
Reading can be done extremely fast, and is great for quickly loading data into RAM. Copying all 16 MiB of a flash into SDRAM, in a loop of WORD reads and writes:
| Read | Time | Speed | Per word |
|---|---|---|---|
| Fast Read Quad Output (6Bh) | 0.79 s | 21.3 MB/s | 18.7 cycles |
| Read Data (03h), one line | 2.77 s | 6.1 MB/s | 66 cycles |
With quad reads, a word is 16 cycles on the bus. The rest is the gap between two words.
Writing
Writing SPI flash (which is NOR flash) is extremely slow compared to reading, and more complex since you need to remove sectors or blocks before writing them. Therefore, it is important to minimize writes, and if speed is important, to cache and batch the writes.
Writing the flash is set by the flash itself: a page of 256 bytes takes about 0.4 ms to program, a 4 KiB sector about 45 ms to erase, and a 64 KiB block about 0.15 s.