2006-08-15 12:22:35 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2006
|
|
|
|
* Heiko Schocher, DENX Software Engineering, hs@denx.de
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Altera FPGA configuration support for the ALPR computer from prodrive
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <altera.h>
|
|
|
|
#include <ACEX1K.h>
|
|
|
|
#include <command.h>
|
2010-04-13 03:28:07 +00:00
|
|
|
#include <asm/processor.h>
|
2010-09-09 17:18:00 +00:00
|
|
|
#include <asm/ppc440.h>
|
2006-08-15 12:22:35 +00:00
|
|
|
#include "fpga.h"
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2006-11-27 13:12:17 +00:00
|
|
|
#if defined(CONFIG_FPGA)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
#ifdef FPGA_DEBUG
|
2011-09-05 10:52:21 +00:00
|
|
|
#define PRINTF(fmt, args...) printf(fmt , ##args)
|
2006-08-15 12:22:35 +00:00
|
|
|
#else
|
2011-09-05 10:52:21 +00:00
|
|
|
#define PRINTF(fmt, args...)
|
2006-08-15 12:22:35 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-27 13:12:17 +00:00
|
|
|
static unsigned long regval;
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2011-09-05 10:52:21 +00:00
|
|
|
#define SET_GPIO_REG_0(reg, bit) do { \
|
2006-11-27 13:12:17 +00:00
|
|
|
regval = in32(reg); \
|
|
|
|
regval &= ~(0x80000000 >> bit); \
|
|
|
|
out32(reg, regval); \
|
2011-09-05 10:52:21 +00:00
|
|
|
} while (0)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2011-09-05 10:52:21 +00:00
|
|
|
#define SET_GPIO_REG_1(reg, bit) do { \
|
2006-11-27 13:12:17 +00:00
|
|
|
regval = in32(reg); \
|
|
|
|
regval |= (0x80000000 >> bit); \
|
|
|
|
out32(reg, regval); \
|
2011-09-05 10:52:21 +00:00
|
|
|
} while (0)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2006-11-27 13:12:17 +00:00
|
|
|
#define SET_GPIO_0(bit) SET_GPIO_REG_0(GPIO0_OR, bit)
|
|
|
|
#define SET_GPIO_1(bit) SET_GPIO_REG_1(GPIO0_OR, bit)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#define FPGA_PRG (0x80000000 >> CONFIG_SYS_GPIO_PROG_EN)
|
|
|
|
#define FPGA_CONFIG (0x80000000 >> CONFIG_SYS_GPIO_CONFIG)
|
|
|
|
#define FPGA_DATA (0x80000000 >> CONFIG_SYS_GPIO_DATA)
|
|
|
|
#define FPGA_CLK (0x80000000 >> CONFIG_SYS_GPIO_CLK)
|
2006-11-27 13:12:17 +00:00
|
|
|
#define OLD_VAL (FPGA_PRG | FPGA_CONFIG)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2006-11-27 13:12:17 +00:00
|
|
|
#define SET_FPGA(data) out32(GPIO0_OR, data)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2011-09-05 10:52:21 +00:00
|
|
|
#define FPGA_WRITE_1 do { \
|
|
|
|
SET_FPGA(OLD_VAL | 0 | FPGA_DATA); /* set data to 1 */ \
|
|
|
|
SET_FPGA(OLD_VAL | FPGA_CLK | FPGA_DATA); /* set data to 1 */ \
|
|
|
|
} while (0)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2011-09-05 10:52:21 +00:00
|
|
|
#define FPGA_WRITE_0 do { \
|
|
|
|
SET_FPGA(OLD_VAL | 0 | 0); /* set data to 0 */ \
|
|
|
|
SET_FPGA(OLD_VAL | FPGA_CLK | 0); /* set data to 1 */ \
|
|
|
|
} while (0)
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
/* Plattforminitializations */
|
|
|
|
/* Here we have to set the FPGA Chain */
|
|
|
|
/* PROGRAM_PROG_EN = HIGH */
|
|
|
|
/* PROGRAM_SEL_DPR = LOW */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_pre_fn(int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
|
|
|
/* Enable the FPGA Chain */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_PROG_EN);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_PROG_EN);
|
|
|
|
SET_GPIO_1(CONFIG_SYS_GPIO_PROG_EN);
|
|
|
|
SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_SEL_DPR);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_SEL_DPR);
|
|
|
|
SET_GPIO_0((CONFIG_SYS_GPIO_SEL_DPR));
|
2006-08-15 12:22:35 +00:00
|
|
|
|
2006-11-27 13:12:17 +00:00
|
|
|
/* initialize the GPIO Pins */
|
2006-08-15 12:22:35 +00:00
|
|
|
/* output */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_CLK);
|
|
|
|
SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CLK);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CLK);
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
/* output */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_DATA);
|
|
|
|
SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_DATA);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_DATA);
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
/* First we set STATUS to 0 then as an input */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS);
|
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_STATUS);
|
|
|
|
SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS);
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
/* output */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CONFIG);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CONFIG);
|
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
/* input */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_CON_DON);
|
|
|
|
SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_CON_DON);
|
|
|
|
SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CON_DON);
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
/* CONFIG = 0 STATUS = 0 -> FPGA in reset state */
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
|
2006-08-15 12:22:35 +00:00
|
|
|
return FPGA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the state of CONFIG Pin */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_config_fn(int assert_config, int flush, int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
2011-09-05 10:52:21 +00:00
|
|
|
if (assert_config)
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_1(CONFIG_SYS_GPIO_CONFIG);
|
2011-09-05 10:52:21 +00:00
|
|
|
else
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
|
2011-09-05 10:52:21 +00:00
|
|
|
|
2006-08-15 12:22:35 +00:00
|
|
|
return FPGA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the state of STATUS Pin */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_status_fn(int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
|
|
|
unsigned long reg;
|
|
|
|
|
|
|
|
reg = in32(GPIO0_IR);
|
2011-09-05 10:52:21 +00:00
|
|
|
if (reg & (0x80000000 >> CONFIG_SYS_GPIO_STATUS)) {
|
2006-08-15 12:22:35 +00:00
|
|
|
PRINTF("STATUS = HIGH\n");
|
|
|
|
return FPGA_FAIL;
|
|
|
|
}
|
|
|
|
PRINTF("STATUS = LOW\n");
|
|
|
|
return FPGA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the state of CONF_DONE Pin */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_done_fn(int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
|
|
|
unsigned long reg;
|
|
|
|
reg = in32(GPIO0_IR);
|
2011-09-05 10:52:21 +00:00
|
|
|
if (reg & (0x80000000 >> CONFIG_SYS_GPIO_CON_DON)) {
|
2006-08-15 12:22:35 +00:00
|
|
|
PRINTF("CONF_DON = HIGH\n");
|
|
|
|
return FPGA_FAIL;
|
|
|
|
}
|
|
|
|
PRINTF("CONF_DON = LOW\n");
|
|
|
|
return FPGA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* writes the complete buffer to the FPGA
|
2006-11-27 13:12:17 +00:00
|
|
|
writing the complete buffer in one function is much faster,
|
2006-08-15 12:22:35 +00:00
|
|
|
then calling it for every bit */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
|
|
|
size_t bytecount = 0;
|
|
|
|
unsigned char *data = (unsigned char *) buf;
|
2011-09-05 10:52:21 +00:00
|
|
|
unsigned char val = 0;
|
2006-08-15 12:22:35 +00:00
|
|
|
int i;
|
2006-11-27 13:12:17 +00:00
|
|
|
int len_40 = len / 40;
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
while (bytecount < len) {
|
2006-11-27 13:12:17 +00:00
|
|
|
val = data[bytecount++];
|
2006-08-15 12:22:35 +00:00
|
|
|
i = 8;
|
|
|
|
do {
|
2011-09-05 10:52:21 +00:00
|
|
|
if (val & 0x01)
|
2006-08-15 12:22:35 +00:00
|
|
|
FPGA_WRITE_1;
|
2011-09-05 10:52:21 +00:00
|
|
|
else
|
2006-08-15 12:22:35 +00:00
|
|
|
FPGA_WRITE_0;
|
2011-09-05 10:52:21 +00:00
|
|
|
|
2006-08-15 12:22:35 +00:00
|
|
|
val >>= 1;
|
2011-09-05 10:52:21 +00:00
|
|
|
i--;
|
2006-08-15 12:22:35 +00:00
|
|
|
} while (i > 0);
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
|
2006-11-27 13:12:17 +00:00
|
|
|
if (bytecount % len_40 == 0) {
|
2011-09-05 10:52:21 +00:00
|
|
|
putc('.'); /* let them know we are alive */
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
|
2011-09-05 10:52:21 +00:00
|
|
|
if (ctrlc())
|
2006-11-27 13:12:17 +00:00
|
|
|
return FPGA_FAIL;
|
|
|
|
#endif
|
|
|
|
}
|
2006-08-15 12:22:35 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return FPGA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called, when programming is aborted */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_abort_fn(int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
2008-10-16 13:01:15 +00:00
|
|
|
SET_GPIO_1((CONFIG_SYS_GPIO_SEL_DPR));
|
2006-08-15 12:22:35 +00:00
|
|
|
return FPGA_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called, when programming was succesful */
|
2011-09-05 10:52:21 +00:00
|
|
|
int fpga_post_fn(int cookie)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
2011-09-05 10:52:21 +00:00
|
|
|
return fpga_abort_fn(cookie);
|
2006-08-15 12:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that these are pointers to code that is in Flash. They will be
|
|
|
|
* relocated at runtime.
|
|
|
|
*/
|
|
|
|
Altera_CYC2_Passive_Serial_fns fpga_fns = {
|
|
|
|
fpga_pre_fn,
|
|
|
|
fpga_config_fn,
|
|
|
|
fpga_status_fn,
|
|
|
|
fpga_done_fn,
|
|
|
|
fpga_write_fn,
|
|
|
|
fpga_abort_fn,
|
|
|
|
fpga_post_fn
|
|
|
|
};
|
|
|
|
|
|
|
|
Altera_desc fpga[CONFIG_FPGA_COUNT] = {
|
|
|
|
{Altera_CYC2,
|
|
|
|
passive_serial,
|
|
|
|
Altera_EP2C35_SIZE,
|
|
|
|
(void *) &fpga_fns,
|
|
|
|
NULL,
|
|
|
|
0}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the fpga. Return 1 on success, 0 on failure.
|
|
|
|
*/
|
2011-09-05 10:52:21 +00:00
|
|
|
int alpr_fpga_init(void)
|
2006-08-15 12:22:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-09-05 10:52:21 +00:00
|
|
|
PRINTF("%s:%d: Initialize FPGA interface\n", __func__, __LINE__);
|
|
|
|
fpga_init();
|
2006-08-15 12:22:35 +00:00
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
|
2011-09-05 10:52:21 +00:00
|
|
|
PRINTF("%s:%d: Adding fpga %d\n", __func__, __LINE__, i);
|
|
|
|
fpga_add(fpga_altera, &fpga[i]);
|
2006-08-15 12:22:35 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|