Skip to content

Commit

Permalink
SPI: support SPI boards on Linux
Browse files Browse the repository at this point in the history
This properly programs and verifies the eeprom on my SPI 7i90

typical commandline:
    mesaflash --device 7I90 --addr /dev/spidev1.0 --spi \
	--verify 7i90_spi_svst4_8.bit
  • Loading branch information
jepler committed Sep 12, 2014
1 parent 6b5fc95 commit c1e47db
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
72 changes: 72 additions & 0 deletions spi_boards.c
Expand Up @@ -17,13 +17,34 @@
//

#include "types.h"

#include <stdio.h>
#include <string.h>

#include "eeprom_local.h"
#include "spi_boards.h"
#include "common.h"
#include "spilbp.h"

extern board_t boards[MAX_BOARDS];
extern int boards_count;

static int spi_board_open(board_t *board) {
eeprom_init(&(board->llio));
board->flash_id = read_flash_id(&(board->llio));
if (board->fallback_support == 1) {
eeprom_prepare_boot_block(board->flash_id);
board->flash_start_address = eeprom_calc_user_space(board->flash_id);
} else {
board->flash_start_address = 0;
}
return 0;
}

static int spi_board_close(board_t *board) {
return 0;
}

int spi_boards_init(board_access_t *access) {
spilbp_init(access);
return 0;
Expand All @@ -33,7 +54,58 @@ void spi_boards_cleanup(board_access_t *access) {
spilbp_release();
}

void spi_read(llio_t *self, u32 off, void *buf, int sz) {
return spilbp_read(off, buf, sz);
}

void spi_write(llio_t *self, u32 off, void *buf, int sz) {
return spilbp_write(off, buf, sz);
}

void spi_boards_scan(board_access_t *access) {
u32 buf[4];
u32 cookie[] = {0x55aacafe, 0x54534f48, 0x32544f4d};
if(spilbp_read(0x100, &buf, sizeof(buf)) < 0) return;

if(memcmp(buf, cookie, sizeof(cookie))) {
fprintf(stderr, "Unexpected cookie at 0000..000c:\n%08x %08x %08x\n",
buf[0], buf[1], buf[2]);
return 0;
}

char ident[8];
if(spilbp_read(buf[3] + 0xc, &ident, sizeof(ident)) < 0) return;

if(!memcmp(ident, "MESA7I90", 8)) {
board_t *board = &boards[boards_count];
board->type = BOARD_SPI;
strcpy(board->dev_addr, access->dev_addr);
strncpy(board->llio.board_name, "7I90", 4);
board->llio.num_ioport_connectors = 24;
board->llio.ioport_connector_name[0] = "P1";
board->llio.ioport_connector_name[1] = "P2";
board->llio.ioport_connector_name[2] = "P3";
board->llio.num_leds = 2;
board->llio.private = board;
board->llio.verbose = access->verbose;
board->llio.write = spi_write;
board->llio.read = spi_read;
board->llio.write_flash = local_write_flash;
board->llio.verify_flash = local_verify_flash;
board->llio.fpga_part_number = "6slx9tqg144";
board->open = spi_board_open;
board->close = spi_board_close;
board->print_info = spi_print_info;
board->flash = BOARD_FLASH_HM2;
board->fallback_support = 1;
boards_count ++;
} else {
int i=0;
for(i=0; i<sizeof(ident); i++)
if(!isprint(ident[i])) ident[i] = '?';

fprintf(stderr, "Unknown board: %.8s\n", ident);
}
}

void spi_boards_release(board_access_t *access) {
Expand Down
89 changes: 89 additions & 0 deletions spilbp.c
Expand Up @@ -17,13 +17,102 @@
//

#include "types.h"

#include <fcntl.h>
#include <linux/spi/spidev.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "eeprom_local.h"
#include "boards.h"

int sd = -1;
struct spi_ioc_transfer settings;

static u32 read_command(u16 addr, unsigned nelem) {
bool increment = true;
return (addr << 16) | 0xA000 | (increment ? 0x800 : 0) | (nelem << 4);
}

static u32 write_command(u16 addr, unsigned nelem) {
bool increment = true;
return (addr << 16) | 0xB000 | (increment ? 0x800 : 0) | (nelem << 4);
}


int spilbp_write(u16 addr, void *buffer, int size) {
if(size % 4 != 0) return -1;
u32 txbuf[1+size/4];
txbuf[0] = write_command(addr, size/4);
memcpy(txbuf + 1, buffer, size);

struct spi_ioc_transfer t;
t = settings;
t.tx_buf = (uint64_t)(intptr_t)txbuf;
t.len = sizeof(txbuf);

int r = ioctl(sd, SPI_IOC_MESSAGE(1), &t);
if(r <= 0) return r;
return 0;
}

int spilbp_read(u16 addr, void *buffer, int size) {
if(size % 4 != 0) return -1;
u32 trxbuf[1+size/4];

This comment has been minimized.

Copy link
@jepler

jepler Mar 10, 2016

Author Member

For size not a multiple of 4, this code is dubious. This rounds down, then read_command also uses size/4 rounded down, but memcpy uses size. Some items should be rounded up and/or converted to byte counts, or it should be asserted that size%4 == 0.

trxbuf[0] = read_command(addr, size/4);
memset(trxbuf+1, 0, size);

struct spi_ioc_transfer t;
t = settings;
t.tx_buf = t.rx_buf = (uint64_t)(intptr_t)trxbuf;
t.len = sizeof(trxbuf);

int r = ioctl(sd, SPI_IOC_MESSAGE(1), &t);
if(r < 0) return r;

memcpy(buffer, trxbuf+1, size);
return 0;
}

void spilbp_print_info() {
}

static int spidev_set_lsb_first(int fd, u8 lsb_first) {
return ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb_first);
}

static int spidev_set_mode(int fd, u8 mode) {
return ioctl(fd, SPI_IOC_WR_MODE, &mode);
}

static int spidev_set_max_speed_hz(int fd, u32 speed_hz) {
return ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz);
}

static int spidev_set_bits_per_word(int fd, u8 bits) {
return ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
}


void spilbp_init(board_access_t *access) {
settings.speed_hz = 8 * 1000 * 1000;
settings.bits_per_word = 32;

sd = open(access->dev_addr, O_RDWR);
if(sd == -1) {
perror("open");
return;
}
spidev_set_lsb_first(sd, false);
spidev_set_mode(sd, 0);
spidev_set_bits_per_word(sd, 32);
spidev_set_max_speed_hz(sd, settings.speed_hz);
}

void spilbp_release() {
if(sd != -1) close(sd);
}
2 changes: 2 additions & 0 deletions spilbp.h
Expand Up @@ -40,6 +40,8 @@ typedef struct {
void spilbp_print_info();
void spilbp_init(board_access_t *access);
void spilbp_release();
int spilbp_write(u32 addr, void *buffer, int size);
int spilbp_read(u32 addr, void *buffer, int size);

#endif

Expand Down

0 comments on commit c1e47db

Please sign in to comment.