Commit eeedb3f6 authored by Alessandro Rubini's avatar Alessandro Rubini

tools: new dir for basic tools for the switch

parent 9a7b37ae
CFLAGS = -Wall -ggdb $(EXTRA_CFLAGS)
ifdef LINUX
CFLAGS += -I$(LINUX)/include \
-I$(LINUX)/arch/arm/mach-at91/include
endif
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
/*
* devmem2.c: Simple program to read/write from/to any location in memory.
*
* Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
* modified 2005 by Alessandro Rubini, to make it less verbose
*
*
* This software has been developed for the LART computing board
* (http://www.lart.tudelft.nl/). The development has been sponsored by
* the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
* and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
* projects.
*
* The author can be reached at:
*
* Jan-Derk Bakker
* Information and Communication Theory Group
* Faculty of Information Technology and Systems
* Delft University of Technology
* P.O. Box 5031
* 2600 GA Delft
* The Netherlands
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int verbose = 0;
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
unsigned long read_result, writeval;
int wid;
char format[16];
off_t target;
int access_type = 'w';
setuid(0); /* try */
if(argc < 2) {
fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
"\taddress : memory address to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
"\tdata : data to be written\n\n",
argv[0]);
exit(1);
}
target = strtoul(argv[1], 0, 0);
if(argc > 2)
access_type = tolower(argv[2][0]);
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
if (verbose) {
printf("/dev/mem opened.\n");
fflush(stdout);
}
/* Map one page */
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if(map_base == (void *) -1) FATAL;
if (verbose) {
printf("Memory mapped at address %p.\n", map_base);
fflush(stdout);
}
virt_addr = map_base + (target & MAP_MASK);
if (argc <= 3) {
switch(access_type) {
case 'b':
read_result = *((unsigned char *) virt_addr);
wid = 2;
break;
case 'h':
read_result = *((unsigned short *) virt_addr);
wid = 4;
break;
case 'w':
read_result = *((unsigned long *) virt_addr);
wid = 8;
break;
default:
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
exit(2);
}
sprintf(format, "0x%%0%ix\n", wid);
printf(format, read_result);
fflush(stdout);
}
if(argc > 3) {
writeval = strtoul(argv[3], 0, 0);
switch(access_type) {
case 'b':
*((unsigned char *) virt_addr) = writeval;
break;
case 'h':
*((unsigned short *) virt_addr) = writeval;
break;
case 'w':
*((unsigned long *) virt_addr) = writeval;
break;
}
}
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
close(fd);
return 0;
}
/*
* Trivial gpio bit banger.
* Alessandro Rubini, 2011, for CERN.
* Released to the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <linux/types.h>
#include <mach/at91_pio.h> /* -I$LINUX/arch/arm/mach-at91/include/ */
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
unsigned char bstream[10*1000*1000]; /* me lazy bastard */
void *pio[5]; /* from mmap(/dev/mem) */
/* describe the g45 memory layout */
unsigned char *piobase = (void *)0xfffff000;
int offsets[] = {0x200, 0x400, 0x600, 0x800, 0xa00};
void *pio[5]; /* from mmap(/dev/mem) plus offsets above */
enum {
PIOA = 0,
PIOB = 1,
PIOC = 2,
PIOD = 3,
PIOE = 4
};
/* macro to access 32-bit registers */
#define __PIO(port, regname) (*(volatile __u32 *)(pio[port] + regname))
/* This sets a bit. To clear output se set ODR (output disable register) */
static inline void pio_set(int regname, int port, int bit)
{
__PIO(port, regname) |= (1 << bit);
}
/* This only reads input data, returns 0 or non-0 */
static int pio_get(int port, int bit)
{
return __PIO(port, PIO_PDSR) & (1 << bit);
}
/* our bits */
#define TK0 PIOD, 0 //out
#define TD0 PIOD, 2 //out
#define LED0 PIOA, 0
#define LED1 PIOA, 1
#define DONE PIOA, 2 //in -- inverted
#define INITB PIOA, 3 //in
#define PROGRAMB PIOA, 4 //out
#define FPGA_RESET PIOA, 5 //out
void ud(int usecs) /* horrible udelay thing without scheduling */
{
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
do
gettimeofday(&tv2, NULL);
while ((tv2.tv_sec - tv1.tv_sec) * 1000*1000
+ tv2.tv_usec - tv1.tv_usec < usecs);
}
int main(int argc, char **argv)
{
int pagesize, bs_size, i;
int fdmem;
if (argc != 2) {
fprintf(stderr, "%s: Use: \"%s <bitstream>\"\n", argv[0],
argv[0]);
exit(1);
}
{
/* read the bisstream file */
FILE *f = fopen(argv[1], "r");
if (!f) {
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1],
strerror(errno));
exit(1);
}
bs_size = fread(bstream, 1, sizeof(bstream), f);
if (bs_size < 0) {
fprintf(stderr, "%s: read(%s): %s\n", argv[0],
argv[1], strerror(errno));
exit(1);
}
if (bs_size == sizeof(bstream)) {
fprintf(stderr, "%s: %s: too big\n", argv[0], argv[1]);
exit(1);
}
fclose(f);
}
pagesize = getpagesize(); /* can't fail */
/* /dev/mem for mmap of both gpio and spi1 */
if ((fdmem = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "%s: /dev/mem: %s\n", argv[0], strerror(errno));
exit(1);
}
/* map a whole page (4kB, but we called getpagesize to know it) */
piobase = mmap(0, pagesize, PROT_READ | PROT_WRITE,
MAP_SHARED, fdmem,
(int)piobase & (~(pagesize-1)));
if (piobase == MAP_FAILED) {
fprintf(stderr, "%s: mmap(/dev/mem): %s\n",
argv[0], strerror(errno));
exit(1);
}
/* ok, now move the pointers within the page, we won't munmap anyways */
for (i = 0; i < ARRAY_SIZE(pio); i++) {
pio[i] = piobase + offsets[i];
}
/*
* all of this stuff is working on gpio so enable pio, out or in
*/
/* clock and data are output, low by now */
pio_set(PIO_PER, TK0);
pio_set(PIO_CODR, TK0);
pio_set(PIO_OER, TK0);
pio_set(PIO_PER, TD0);
pio_set(PIO_CODR, TD0);
pio_set(PIO_OER, TD0);
/* fpga_reset is high */
pio_set(PIO_PER, FPGA_RESET);
pio_set(PIO_SODR, FPGA_RESET);
pio_set(PIO_OER, FPGA_RESET);
/* program_b is output high: is is pulsed low to start programming */
pio_set(PIO_PER, PROGRAMB);
pio_set(PIO_SODR, PROGRAMB);
pio_set(PIO_OER, PROGRAMB);
/* init_b is input: it's the fpga telling us it's done initializing */
pio_set(PIO_PER, INITB);
pio_set(PIO_ODR, INITB);
/* done is input */
pio_set(PIO_PER, DONE);
pio_set(PIO_ODR, DONE);
/* flip the leds for 0.1 secs to show we are alive and well */
pio_set(PIO_PER, LED0);
pio_set(PIO_OER, LED0);
pio_set(PIO_CODR, LED0);
pio_set(PIO_PER, LED1);
pio_set(PIO_OER, LED1);
pio_set(PIO_CODR, LED1);
ud(100*1000);
pio_set(PIO_SODR, LED0);
pio_set(PIO_SODR, LED1);
/*
* What follows is based on information from
* www.xilinx.com/support/documentation/user_guides/ug360.pdf
*/
/* First, turn PROGRAM_B low */
pio_set(PIO_CODR, PROGRAMB);
/* Then, wait little and then check init_b must go low */
for (i = 0; i < 50; i++) {
ud(10);
if (!pio_get(INITB))
break;
}
/* check status: initb must be low and done must be low */
if (pio_get(INITB)) {
fprintf(stderr, "%s: INIT_B is still high after PROGRAM_B\n",
argv[0]);
//exit(1);
}
if (!pio_get(DONE)) {
fprintf(stderr, "%s: DONE is already high after PROGRAM_B\n",
argv[0]);
//exit(1);
}
/* raise program_b, and initb must go high, too */
pio_set(PIO_SODR, PROGRAMB);
for (i = 0; i < 50; i++) {
ud(10);
if (pio_get(INITB))
break;
}
if (pio_get(INITB)) {
fprintf(stderr, "%s: INIT_B is not going back high\n",
argv[0]);
//exit(1);
}
/* Then write one byte at a time */
for (i = 0; i < bs_size; i++) {
int byte = bstream[i];
int bit;
if (!(i & 1023)) {
putchar('.'); fflush(stdout);
}
/* msb first */
for (bit = 0x80; bit > 0; bit >>= 1) {
/* data on rising edge of clock, which starts low */
if (byte & bit)
pio_set(PIO_SODR, TD0);
else
pio_set(PIO_CODR, TD0);
/* so, rising edge, after it's stable */
asm volatile("nop\n nop\n nop\n nop");
pio_set(PIO_SODR, TK0);
pio_set(PIO_CODR, TK0);
}
if (0 && /* don't do this check */ !pio_get(DONE)) {
fprintf(stderr, "%s: DONE is already high after "
"%i bytes (missing %i)\n", argv[0],
i, bs_size - i);
exit(1);
}
}
putchar('\n');
/* Then, wait a little and then check done must go high */
for (i = 0; i < 500; i++) {
ud(100);
if (!pio_get(DONE))
break;
}
if (pio_get(DONE)) {
fprintf(stderr, "%s: DONE is not going high after %i bytes\n",
argv[0], bs_size);
exit(1);
}
/* PA5 must go low then high */
pio_set(PIO_CODR, FPGA_RESET);
ud(10);
pio_set(PIO_SODR, FPGA_RESET);
exit(0);
}
/*
* mapper.c -- simple file that mmap()s a file region and prints it
* Alessandro Rubini 1997-2008
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
int main(int argc, char **argv)
{
char *fname;
FILE *f;
int pagesize = getpagesize();
unsigned int pos, pos_pg, pos_off;
unsigned int len, len_pg, len_off;
void *address;
char *rest;
if (argc !=4
|| sscanf(argv[2],"%i", &pos) != 1
|| sscanf(argv[3],"%i", &len) != 1) {
fprintf(stderr, "%s: Usage \"%s <file> <offset> <len>\"\n", argv[0],
argv[0]);
exit(1);
}
/* note: pos may be more than 2G, so use strtoul */
pos = strtoul(argv[2], &rest, 0);
if (rest && *rest) {
fprintf(stderr, "%s: Usage \"%s <file> <offset> <len>\"\n", argv[0],
argv[0]);
exit(1);
}
fname=argv[1];
if (!(f=fopen(fname,"r"))) {
fprintf(stderr, "%s: %s: %s\n", argv[0], fname, strerror(errno));
exit(1);
}
/* page-align our numbers, as we depend on the MMU */
pos_pg = pos & ~(pagesize-1);
pos_off = pos & (pagesize-1);
len_pg = (len + pos_off + pagesize-1) & ~(pagesize-1);
len_off = (len + pos_off) & (pagesize-1);
address=mmap(0, len_pg,
PROT_READ, MAP_FILE | MAP_PRIVATE, fileno(f), pos_pg);
if (address == (void *)-1) {
fprintf(stderr,"%s: mmap(): %s\n",argv[0],strerror(errno));
exit(1);
}
fclose(f);
fprintf(stderr, "mapped \"%s\" from %i to %i (0x%x to 0x%x) \n",
fname, pos, pos+len, pos_pg, pos_pg+len_pg);
if (fwrite(address+pos_off, 1, len, stdout) != len) {
fprintf(stderr, "%s: write(): %s\n", argv[0], strerror(errno));
exit(1);
}
return 0;
}
/*
* Trivial pll programmer using an spi controoler.
* PLL is AD9516, SPI is opencores
* Tomasz Wlostowski, Alessandro Rubini, 2011, for CERN.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/mman.h>
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
#define WRS3_SPI_BASE 0x10000000 /* FIXME */
static void ud(int usecs) /* horrible udelay thing without scheduling */
{
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
do
gettimeofday(&tv2, NULL);
while ((tv2.tv_sec - tv1.tv_sec) * 1000*1000
+ tv2.tv_usec - tv1.tv_usec < usecs);
}
/*
* SPI stuff, used by later code
*/
#define SPI_REG_RX0 0
#define SPI_REG_TX0 0
#define SPI_REG_RX1 4
#define SPI_REG_TX1 4
#define SPI_REG_RX2 8
#define SPI_REG_TX2 8
#define SPI_REG_RX3 12
#define SPI_REG_TX3 12
#define SPI_REG_CTRL 16
#define SPI_REG_DIVIDER 20
#define SPI_REG_SS 24
#define SPI_CTRL_ASS (1<<13)
#define SPI_CTRL_IE (1<<12)
#define SPI_CTRL_LSB (1<<11)
#define SPI_CTRL_TXNEG (1<<10)
#define SPI_CTRL_RXNEG (1<<9)
#define SPI_CTRL_GO_BSY (1<<8)
#define SPI_CTRL_CHAR_LEN(x) ((x) & 0x7f)
static int oc_spi_base;
static inline void ocspi_write(int addr, uint32_t val)
{
if (!oc_spi_base)
return;
*(uint32_t *)(oc_spi_base + addr) = val;
printf("%08x := %08x\n", oc_spi_base + addr, val);
}
static inline uint32_t ocspi_read(int addr)
{
uint32_t val;
if (!oc_spi_base)
return -1;
val =*(uint32_t *)(oc_spi_base + addr);
printf("%08x = %08x\n", oc_spi_base + addr, val);
return val;
}
int oc_spi_init(char *prgname, uint32_t base_addr)
{
int pagesize = getpagesize(); /* can't fail */
int fdmem;
void *addr;
/* /dev/mem for mmap of both gpio and spi1 */
if ((fdmem = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "%s: /dev/mem: %s\n",
prgname, strerror(errno));
return -1;
}
/* map a whole page (4kB, but we called getpagesize to know it) */
addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
MAP_SHARED, fdmem,
base_addr & (~(pagesize-1)));
if (addr == MAP_FAILED) {
fprintf(stderr, "%s: mmap(/dev/mem): %s\n",
prgname, strerror(errno));
return -1;
}
close(fdmem);
oc_spi_base = (int)addr + (base_addr & (pagesize-1));
ocspi_write(SPI_REG_DIVIDER, 1000);
return 0;
}
int oc_spi_txrx(int ss, int nbits, uint32_t in, uint32_t *out)
{
uint32_t rval;
if (!out)
out = &rval;
ocspi_write( SPI_REG_CTRL,
SPI_CTRL_ASS | SPI_CTRL_CHAR_LEN(nbits)
| SPI_CTRL_TXNEG);
ocspi_write( SPI_REG_TX0, in);
ocspi_write( SPI_REG_SS, (1 << ss));
ocspi_write( SPI_REG_CTRL,
SPI_CTRL_ASS | SPI_CTRL_CHAR_LEN(nbits)
| SPI_CTRL_TXNEG | SPI_CTRL_GO_BSY);
while(ocspi_read(SPI_REG_CTRL) & SPI_CTRL_GO_BSY)
;
*out = ocspi_read(SPI_REG_RX0);
return 0;
}
#define CS_PLL 0 /* AD9516 on SPI CS0 */
/*
* AD9516 stuff, using SPI, used by later code.
* "reg" is 12 bits, "val" is 8 bits, but both are better used as int
*/
static void ad9516_write_reg(int reg, int val)
{
oc_spi_txrx(CS_PLL, 24, (reg << 8) | val, NULL);
}
static int ad9516_read_reg(int reg)
{
uint32_t rval;
oc_spi_txrx(CS_PLL, 24, (reg << 8) | (1 << 23), &rval);
return rval & 0xff;
}
/* FIXME: table of registers .... */
struct adregval {
int reg;
int val;
};
static struct adregval ad9516_regs[0];
static int ad9516_init(FILE *fout)
{
int i;
if (fout)
fprintf(fout, "Initializing AD9516 PLL...\n");
ad9516_write_reg(0x000, 0x99);
ad9516_write_reg(0x232, 0x01);
if (ad9516_read_reg(0x3) != 0xc3) {
fprintf(stderr,"Error: AD9516 PLL not responding.\n");
return -1;
}
for (i=0; i < ARRAY_SIZE(ad9516_regs); i++)
ad9516_write_reg (ad9516_regs[i].reg, ad9516_regs[i].val);
while ((ad9516_read_reg(0x1f) & 1) == 0)
ud(100);
// sync channels
ad9516_write_reg(0x230, 1);
ad9516_write_reg(0x232, 1);
ad9516_write_reg(0x230, 0);
ad9516_write_reg(0x232, 1);
if (fout)
fprintf(fout, "AD9516 locked.\n");
return 0;
}
/*
* That's it. Here's our main function
*/
int main(int argc, char **argv)
{
if (oc_spi_init(argv[0], WRS3_SPI_BASE) < 0)
exit(1);
if (ad9516_init(stdout) < 0)
exit(1);
/* Wow! Nothing to do! */
exit(0);
}
/*
* wmapper.c -- simple file that mmap()s a file region and writes to it
* Alessandro Rubini 1997-2008
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
int main(int argc, char **argv)
{
char *fname;
FILE *f;
int pagesize = getpagesize();
unsigned int pos, pos_pg, pos_off;
unsigned int len, len_pg, len_off;
void *address;
char *rest;
if (argc !=4
|| sscanf(argv[2],"%i", &pos) != 1
|| sscanf(argv[3],"%i", &len) != 1) {
fprintf(stderr, "%s: Usage \"%s <file> <offset> <len>\"\n", argv[0],
argv[0]);
exit(1);
}
/* note: pos may be more than 2G, so use strtoul */
pos = strtoul(argv[2], &rest, 0);
if (rest && *rest) {
fprintf(stderr, "%s: Usage \"%s <file> <offset> <len>\"\n", argv[0],
argv[0]);
exit(1);
}
fname=argv[1];
if (!(f=fopen(fname,"r+"))) {
fprintf(stderr, "%s: %s: %s\n", argv[0], fname, strerror(errno));
exit(1);
}
/* page-align our numbers, as we depend on the MMU */
pos_pg = pos & ~(pagesize-1);
pos_off = pos & (pagesize-1);
len_pg = (len + pos_off + pagesize-1) & ~(pagesize-1);
len_off = (len + pos_off) & (pagesize-1);
address=mmap(0, len_pg, PROT_READ | PROT_WRITE,
MAP_FILE | MAP_SHARED, fileno(f), pos_pg);
if (address == (void *)-1) {
fprintf(stderr,"%s: mmap(): %s\n",argv[0],strerror(errno));
exit(1);
}
fclose(f);
fprintf(stderr, "mapped \"%s\" from %i to %i (0x%x to 0x%x) \n",
fname, pos, pos+len, pos_pg, pos_pg+len_pg);
if (fread(address + pos_off, 1, len, stdin) != len) {
fprintf(stderr, "%s: short read\n", argv[0]);
exit(1);
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment