#include <sys/cdefs.h>
#if 0
__FBSDID("$FreeBSD: src/sys/arm/xscale/ixp425/ixp425_npe.c,v 1.1 2006/11/19 23:55:23 sam Exp $");
#endif
__KERNEL_RCSID(0, "$NetBSD: ixp425_npe.c,v 1.17 2023/06/17 11:57:49 rin Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <dev/firmload.h>
#include <sys/bus.h>
#include <machine/cpu.h>
#include <machine/intr.h>
#include <arm/xscale/ixp425reg.h>
#include <arm/xscale/ixp425var.h>
#include <arm/xscale/ixp425_ixmevar.h>
#include <arm/xscale/ixp425_npereg.h>
#include <arm/xscale/ixp425_npevar.h>
#include <arm/xscale/ixp425_if_npereg.h>
#include "locators.h"
extern char _binary_IxNpeMicrocode_dat_start[];
#define IX_NPEDL_NPEIMAGE_FIELD_MASK 0xff
#define IX_NPEDL_BLOCK_TYPE_INSTRUCTION 0x00000000
#define IX_NPEDL_BLOCK_TYPE_DATA 0x00000001
#define IX_NPEDL_BLOCK_TYPE_STATE 0x00000002
#define IX_NPEDL_END_OF_DOWNLOAD_MAP 0x0000000F
#define IX_NPEDL_MASK_STATE_ADDR_CTXT_REG 0x0000000F
#define IX_NPEDL_MASK_STATE_ADDR_CTXT_NUM 0x000000F0
#define IX_NPEDL_OFFSET_STATE_ADDR_CTXT_NUM 4
#define IX_NPEDL_STATE_INFO_ENTRY_SIZE 2
typedef struct {
uint32_t type;
uint32_t offset;
} IxNpeDlNpeMgrDownloadMapBlockEntry;
typedef union {
IxNpeDlNpeMgrDownloadMapBlockEntry block;
uint32_t eodmMarker;
} IxNpeDlNpeMgrDownloadMapEntry;
typedef struct {
IxNpeDlNpeMgrDownloadMapEntry entry[1];
} IxNpeDlNpeMgrDownloadMap;
typedef struct {
uint32_t npeMemAddress;
uint32_t size;
uint32_t data[1];
} IxNpeDlNpeMgrCodeBlock;
typedef struct {
uint32_t addressInfo;
uint32_t value;
} IxNpeDlNpeMgrStateInfoCtxtRegEntry;
typedef struct {
uint32_t size;
IxNpeDlNpeMgrStateInfoCtxtRegEntry ctxtRegEntry[1];
} IxNpeDlNpeMgrStateInfoBlock;
static int npe_debug = 0;
#define DPRINTF(dev, fmt, ...) do { \
if (npe_debug) printf(fmt, __VA_ARGS__); \
} while (0)
#define DPRINTFn(n, dev, fmt, ...) do { \
if (npe_debug >= n) printf(fmt, __VA_ARGS__); \
} while (0)
static int npe_checkbits(struct ixpnpe_softc *, uint32_t reg, uint32_t);
static int npe_isstopped(struct ixpnpe_softc *);
static int npe_load_ins(struct ixpnpe_softc *,
const IxNpeDlNpeMgrCodeBlock *bp, int verify);
static int npe_load_data(struct ixpnpe_softc *,
const IxNpeDlNpeMgrCodeBlock *bp, int verify);
static int npe_load_stateinfo(struct ixpnpe_softc *,
const IxNpeDlNpeMgrStateInfoBlock *bp, int verify);
static int npe_load_image(struct ixpnpe_softc *,
const uint32_t *imageCodePtr, int verify);
static int npe_cpu_reset(struct ixpnpe_softc *);
static int npe_cpu_start(struct ixpnpe_softc *);
static int npe_cpu_stop(struct ixpnpe_softc *);
static void npe_cmd_issue_write(struct ixpnpe_softc *,
uint32_t cmd, uint32_t addr, uint32_t data);
static uint32_t npe_cmd_issue_read(struct ixpnpe_softc *,
uint32_t cmd, uint32_t addr);
static int npe_ins_write(struct ixpnpe_softc *,
uint32_t addr, uint32_t data, int verify);
static int npe_data_write(struct ixpnpe_softc *,
uint32_t addr, uint32_t data, int verify);
static void npe_ecs_reg_write(struct ixpnpe_softc *,
uint32_t reg, uint32_t data);
static uint32_t npe_ecs_reg_read(struct ixpnpe_softc *, uint32_t reg);
static void npe_issue_cmd(struct ixpnpe_softc *, uint32_t command);
static void npe_cpu_step_save(struct ixpnpe_softc *);
static int npe_cpu_step(struct ixpnpe_softc *, uint32_t npeInstruction,
uint32_t ctxtNum, uint32_t ldur);
static void npe_cpu_step_restore(struct ixpnpe_softc *);
static int npe_logical_reg_read(struct ixpnpe_softc *,
uint32_t regAddr, uint32_t regSize,
uint32_t ctxtNum, uint32_t *regVal);
static int npe_logical_reg_write(struct ixpnpe_softc *,
uint32_t regAddr, uint32_t regVal,
uint32_t regSize, uint32_t ctxtNum, int verify);
static int npe_physical_reg_write(struct ixpnpe_softc *,
uint32_t regAddr, uint32_t regValue, int verify);
static int npe_ctx_reg_write(struct ixpnpe_softc *, uint32_t ctxtNum,
uint32_t ctxtReg, uint32_t ctxtRegVal, int verify);
static int ixpnpe_intr(void *arg);
static uint32_t
npe_reg_read(struct ixpnpe_softc *sc, bus_size_t off)
{
uint32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, off);
DPRINTFn(9, sc->sc_dev, "%s(0x%lx) => 0x%x\n", __func__, off, v);
return v;
}
static void
npe_reg_write(struct ixpnpe_softc *sc, bus_size_t off, uint32_t val)
{
DPRINTFn(9, sc->sc_dev, "%s(0x%lx, 0x%x)\n", __func__, off, val);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
}
static int ixpnpe_match(device_t, cfdata_t, void *);
static void ixpnpe_attach(device_t, device_t, void *);
static int ixpnpe_print(void *, const char *);
static int ixpnpe_search(device_t, cfdata_t, const int *, void *);
CFATTACH_DECL_NEW(ixpnpe, sizeof(struct ixpnpe_softc),
ixpnpe_match, ixpnpe_attach, NULL, NULL);
static int
ixpnpe_match(device_t parent, cfdata_t match, void *arg)
{
struct ixme_attach_args *ixa = arg;
return (ixa->ixa_npe == 1 || ixa->ixa_npe == 2);
}
static void
ixpnpe_attach(device_t parent, device_t self, void *arg)
{
struct ixpnpe_softc *sc = device_private(self);
struct ixme_attach_args *ixa = arg;
bus_addr_t base;
int irq;
aprint_naive("\n");
aprint_normal("\n");
sc->sc_dev = self;
sc->sc_iot = ixa->ixa_iot;
sc->sc_dt = ixa->ixa_dt;
sc->sc_unit = ixa->ixa_npe;
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
switch (ixa->ixa_npe) {
default:
panic("%s: Invalid NPE!", device_xname(self));
case 1:
base = IXP425_NPE_B_HWBASE;
sc->sc_size = IXP425_NPE_B_SIZE;
irq = IXP425_INT_NPE_B;
sc->insMemSize = IX_NPEDL_INS_MEMSIZE_WORDS_NPEB;
sc->dataMemSize = IX_NPEDL_DATA_MEMSIZE_WORDS_NPEB;
break;
case 2:
base = IXP425_NPE_C_HWBASE;
sc->sc_size = IXP425_NPE_C_SIZE;
irq = IXP425_INT_NPE_C;
sc->insMemSize = IX_NPEDL_INS_MEMSIZE_WORDS_NPEC;
sc->dataMemSize = IX_NPEDL_DATA_MEMSIZE_WORDS_NPEC;
break;
}
if (bus_space_map(sc->sc_iot, base, sc->sc_size, 0, &sc->sc_ioh))
panic("%s: Cannot map registers", device_xname(self));
sc->sc_ih = ixp425_intr_establish(irq, IPL_NET, ixpnpe_intr, sc);
if (sc->sc_ih == NULL)
panic("%s: Unable to establish irq %u", device_xname(self), irq);
npe_reg_write(sc, IX_NPECTL,
npe_reg_read(sc, IX_NPECTL) | (IX_NPECTL_OFE | IX_NPECTL_OFWE));
config_search(self, ixa,
CFARGS(.search = ixpnpe_search));
}
static int
ixpnpe_print(void *arg, const char *name)
{
return (UNCONF);
}
static int
ixpnpe_search(device_t parent, cfdata_t cf, const int *ldesc, void *arg)
{
struct ixpnpe_softc *sc = device_private(parent);
struct ixme_attach_args *ixa = arg;
struct ixpnpe_attach_args na;
na.na_unit = ixa->ixa_npe;
na.na_phy = cf->cf_loc[IXPNPECF_PHY];
na.na_npe = sc;
na.na_iot = ixa->ixa_iot;
na.na_dt = ixa->ixa_dt;
if (config_probe(parent, cf, &na)) {
config_attach(parent, cf, &na, ixpnpe_print, CFARGS_NONE);
return (1);
}
return (0);
}
int
ixpnpe_stopandreset(struct ixpnpe_softc *sc)
{
int error;
mutex_enter(&sc->sc_lock);
error = npe_cpu_stop(sc);
if (error == 0)
error = npe_cpu_reset(sc);
if (error == 0)
sc->started = 0;
mutex_exit(&sc->sc_lock);
DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
return error;
}
static int
ixpnpe_start_locked(struct ixpnpe_softc *sc)
{
int error;
if (!sc->started) {
error = npe_cpu_start(sc);
if (error == 0)
sc->started = 1;
} else
error = 0;
DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
return error;
}
int
ixpnpe_start(struct ixpnpe_softc *sc)
{
int ret;
mutex_enter(&sc->sc_lock);
ret = ixpnpe_start_locked(sc);
mutex_exit(&sc->sc_lock);
return (ret);
}
int
ixpnpe_stop(struct ixpnpe_softc *sc)
{
int error;
mutex_enter(&sc->sc_lock);
error = npe_cpu_stop(sc);
if (error == 0)
sc->started = 0;
mutex_exit(&sc->sc_lock);
DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
return error;
}
#define NPE_IMAGE_MARKER 0xfeedf00d
typedef struct {
uint32_t marker;
uint32_t id;
uint32_t size;
} IxNpeDlImageMgrImageHeader;
static int
npe_findimage(struct ixpnpe_softc *sc,
const uint32_t *imageLibrary, uint32_t imageId,
const uint32_t **imagePtr, uint32_t *imageSize)
{
const IxNpeDlImageMgrImageHeader *image;
uint32_t offset = 0;
while (imageLibrary[offset] == NPE_IMAGE_MARKER) {
image = (const IxNpeDlImageMgrImageHeader *)&imageLibrary[offset];
offset += sizeof(IxNpeDlImageMgrImageHeader)/sizeof(uint32_t);
DPRINTF(sc->sc_dev, "%s: off %u mark 0x%x id 0x%x size %u\n",
__func__, offset, image->marker, image->id, image->size);
if (image->id == imageId) {
*imagePtr = imageLibrary + offset;
*imageSize = image->size;
return 0;
}
if (image->id == NPE_IMAGE_MARKER) {
printf("%s: imageId 0x%08x not found in image library header\n",
device_xname(sc->sc_dev), imageId);
return EIO;
}
offset += image->size;
}
return EIO;
}
int
ixpnpe_init(struct ixpnpe_softc *sc, const char *imageName, uint32_t imageId)
{
uint32_t imageSize;
const uint32_t *imageCodePtr;
void *fw;
int error;
DPRINTF(sc->sc_dev, "load %s, imageId 0x%08x\n", imageName, imageId);
#if 0
IxFeatureCtrlDeviceId devid = IX_NPEDL_DEVICEID_FROM_IMAGEID_GET(imageId);
if (devid > (ixFeatureCtrlDeviceRead() & IX_FEATURE_CTRL_DEVICE_TYPE_MASK))
return EINVAL;
#endif
error = ixpnpe_stopandreset(sc);
if (error != 0)
return error;
fw = (void *)_binary_IxNpeMicrocode_dat_start;
error = npe_findimage(sc, (void *)fw , imageId, &imageCodePtr, &imageSize);
if (error != 0)
goto done;
mutex_enter(&sc->sc_lock);
error = npe_load_image(sc, imageCodePtr, 1 );
if (error == 0) {
sc->validImage = 1;
error = ixpnpe_start_locked(sc);
} else {
sc->validImage = 0;
}
sc->functionalityId = IX_NPEDL_FUNCTIONID_FROM_IMAGEID_GET(imageId);
mutex_exit(&sc->sc_lock);
done:
DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
return error;
}
int
ixpnpe_getfunctionality(struct ixpnpe_softc *sc)
{
return (sc->validImage ? sc->functionalityId : 0);
}
static int
npe_checkbits(struct ixpnpe_softc *sc, uint32_t reg, uint32_t expectedBitsSet)
{
uint32_t val;
val = npe_reg_read(sc, reg);
DPRINTFn(5, sc->sc_dev, "%s(0x%x, 0x%x) => 0x%x (%u)\n",
__func__, reg, expectedBitsSet, val,
(val & expectedBitsSet) == expectedBitsSet);
return ((val & expectedBitsSet) == expectedBitsSet);
}
static int
npe_isstopped(struct ixpnpe_softc *sc)
{
return npe_checkbits(sc,
IX_NPEDL_REG_OFFSET_EXCTL, IX_NPEDL_EXCTL_STATUS_STOP);
}
static int
npe_load_ins(struct ixpnpe_softc *sc,
const IxNpeDlNpeMgrCodeBlock *bp, int verify)
{
uint32_t npeMemAddress;
int i, blockSize;
npeMemAddress = bp->npeMemAddress;
blockSize = bp->size;
if (npeMemAddress + blockSize > sc->insMemSize) {
printf("%s: Block size too big for NPE memory\n", device_xname(sc->sc_dev));
return EINVAL;
}
for (i = 0; i < blockSize; i++, npeMemAddress++) {
if (npe_ins_write(sc, npeMemAddress, bp->data[i], verify) != 0) {
printf("%s: NPE instruction write failed", device_xname(sc->sc_dev));
return EIO;
}
}
return 0;
}
static int
npe_load_data(struct ixpnpe_softc *sc,
const IxNpeDlNpeMgrCodeBlock *bp, int verify)
{
uint32_t npeMemAddress;
int i, blockSize;
npeMemAddress = bp->npeMemAddress;
blockSize = bp->size;
if (npeMemAddress + blockSize > sc->dataMemSize) {
printf("%s: Block size too big for NPE memory\n", device_xname(sc->sc_dev));
return EINVAL;
}
for (i = 0; i < blockSize; i++, npeMemAddress++) {
if (npe_data_write(sc, npeMemAddress, bp->data[i], verify) != 0) {
printf("%s: NPE data write failed\n", device_xname(sc->sc_dev));
return EIO;
}
}
return 0;
}
static int
npe_load_stateinfo(struct ixpnpe_softc *sc,
const IxNpeDlNpeMgrStateInfoBlock *bp, int verify)
{
int i, nentries, error;
npe_cpu_step_save(sc);
nentries = bp->size / IX_NPEDL_STATE_INFO_ENTRY_SIZE;
error = 0;
for (i = 0; i < nentries; i++) {
uint32_t regVal = bp->ctxtRegEntry[i].value;
uint32_t addrInfo = bp->ctxtRegEntry[i].addressInfo;
uint32_t reg = (addrInfo & IX_NPEDL_MASK_STATE_ADDR_CTXT_REG);
uint32_t cNum = (addrInfo & IX_NPEDL_MASK_STATE_ADDR_CTXT_NUM) >>
IX_NPEDL_OFFSET_STATE_ADDR_CTXT_NUM;
if (reg >= IX_NPEDL_CTXT_REG_MAX) {
printf("%s: invalid Context Register %u\n", device_xname(sc->sc_dev),
reg);
error = EINVAL;
break;
}
if (cNum >= IX_NPEDL_CTXT_NUM_MAX) {
printf("%s: invalid Context Number %u\n", device_xname(sc->sc_dev),
cNum);
error = EINVAL;
break;
}
if (cNum == 0 && reg == IX_NPEDL_CTXT_REG_STEVT) {
printf("%s: no STEVT for Context 0\n", device_xname(sc->sc_dev));
error = EINVAL;
break;
}
if (npe_ctx_reg_write(sc, cNum, reg, regVal, verify) != 0) {
printf("%s: write of state-info to NPE failed\n",
device_xname(sc->sc_dev));
error = EIO;
break;
}
}
npe_cpu_step_restore(sc);
return error;
}
static int
npe_load_image(struct ixpnpe_softc *sc,
const uint32_t *imageCodePtr, int verify)
{
#define EOM(marker) ((marker) == IX_NPEDL_END_OF_DOWNLOAD_MAP)
const IxNpeDlNpeMgrDownloadMap *downloadMap;
int i, error;
if (!npe_isstopped(sc)) {
printf("%s: cannot load image, NPE not stopped\n", device_xname(sc->sc_dev));
return EIO;
}
error = 0;
downloadMap = (const IxNpeDlNpeMgrDownloadMap *) imageCodePtr;
for (i = 0; !EOM(downloadMap->entry[i].eodmMarker); i++) {
const uint32_t *bp = imageCodePtr + downloadMap->entry[i].block.offset;
switch (downloadMap->entry[i].block.type) {
case IX_NPEDL_BLOCK_TYPE_INSTRUCTION:
error = npe_load_ins(sc,
(const IxNpeDlNpeMgrCodeBlock *) bp, verify);
DPRINTF(sc->sc_dev, "%s: inst, error %d\n", __func__, error);
break;
case IX_NPEDL_BLOCK_TYPE_DATA:
error = npe_load_data(sc,
(const IxNpeDlNpeMgrCodeBlock *) bp, verify);
DPRINTF(sc->sc_dev, "%s: data, error %d\n", __func__, error);
break;
case IX_NPEDL_BLOCK_TYPE_STATE:
error = npe_load_stateinfo(sc,
(const IxNpeDlNpeMgrStateInfoBlock *) bp, verify);
DPRINTF(sc->sc_dev, "%s: state, error %d\n", __func__, error);
break;
default:
printf("%s: unknown block type 0x%x in download map\n",
device_xname(sc->sc_dev), downloadMap->entry[i].block.type);
error = EIO;
break;
}
if (error != 0)
break;
}
return error;
#undef EOM
}
static const struct {
uint32_t regAddr;
uint32_t regResetVal;
} ixNpeDlEcsRegResetValues[] = {
{ IX_NPEDL_ECS_BG_CTXT_REG_0, IX_NPEDL_ECS_BG_CTXT_REG_0_RESET },
{ IX_NPEDL_ECS_BG_CTXT_REG_1, IX_NPEDL_ECS_BG_CTXT_REG_1_RESET },
{ IX_NPEDL_ECS_BG_CTXT_REG_2, IX_NPEDL_ECS_BG_CTXT_REG_2_RESET },
{ IX_NPEDL_ECS_PRI_1_CTXT_REG_0, IX_NPEDL_ECS_PRI_1_CTXT_REG_0_RESET },
{ IX_NPEDL_ECS_PRI_1_CTXT_REG_1, IX_NPEDL_ECS_PRI_1_CTXT_REG_1_RESET },
{ IX_NPEDL_ECS_PRI_1_CTXT_REG_2, IX_NPEDL_ECS_PRI_1_CTXT_REG_2_RESET },
{ IX_NPEDL_ECS_PRI_2_CTXT_REG_0, IX_NPEDL_ECS_PRI_2_CTXT_REG_0_RESET },
{ IX_NPEDL_ECS_PRI_2_CTXT_REG_1, IX_NPEDL_ECS_PRI_2_CTXT_REG_1_RESET },
{ IX_NPEDL_ECS_PRI_2_CTXT_REG_2, IX_NPEDL_ECS_PRI_2_CTXT_REG_2_RESET },
{ IX_NPEDL_ECS_DBG_CTXT_REG_0, IX_NPEDL_ECS_DBG_CTXT_REG_0_RESET },
{ IX_NPEDL_ECS_DBG_CTXT_REG_1, IX_NPEDL_ECS_DBG_CTXT_REG_1_RESET },
{ IX_NPEDL_ECS_DBG_CTXT_REG_2, IX_NPEDL_ECS_DBG_CTXT_REG_2_RESET },
{ IX_NPEDL_ECS_INSTRUCT_REG, IX_NPEDL_ECS_INSTRUCT_REG_RESET }
};
static const uint32_t ixNpeDlCtxtRegResetValues[] = {
IX_NPEDL_CTXT_REG_RESET_STEVT,
IX_NPEDL_CTXT_REG_RESET_STARTPC,
IX_NPEDL_CTXT_REG_RESET_REGMAP,
IX_NPEDL_CTXT_REG_RESET_CINDEX,
};
#define IX_NPEDL_RESET_NPE_PARITY 0x0800
#define IX_NPEDL_PARITY_BIT_MASK 0x3F00FFFF
#define IX_NPEDL_CONFIG_CTRL_REG_MASK 0x3F3FFFFF
static int
npe_cpu_reset(struct ixpnpe_softc *sc)
{
#define N(a) (sizeof(a) / sizeof(a[0]))
uint32_t ctxtReg;
uint32_t regAddr;
uint32_t regVal;
uint32_t resetNpeParity;
uint32_t ixNpeConfigCtrlRegVal;
int i, error = 0;
ixNpeConfigCtrlRegVal = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_CTL);
ixNpeConfigCtrlRegVal |= 0x3F000000;
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_CTL,
(ixNpeConfigCtrlRegVal & IX_NPEDL_PARITY_BIT_MASK));
DPRINTFn(2, sc->sc_dev, "%s: dis parity int, CTL => 0x%x\n",
__func__, ixNpeConfigCtrlRegVal & IX_NPEDL_PARITY_BIT_MASK);
npe_cpu_step_save(sc);
while (npe_checkbits(sc,
IX_NPEDL_REG_OFFSET_WFIFO, IX_NPEDL_MASK_WFIFO_VALID)) {
(void) npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WFIFO);
}
while (npe_checkbits(sc,
IX_NPEDL_REG_OFFSET_STAT, IX_NPEDL_MASK_STAT_OFNE)) {
(void) npe_reg_read(sc, IX_NPEDL_REG_OFFSET_FIFO);
}
while (npe_checkbits(sc,
IX_NPEDL_REG_OFFSET_STAT, IX_NPEDL_MASK_STAT_IFNE)) {
error = npe_cpu_step(sc, IX_NPEDL_INSTR_RD_FIFO, 0, 0);
if (error != 0) {
DPRINTF(sc->sc_dev, "%s: cannot step (1), error %u\n",
__func__, error);
npe_cpu_step_restore(sc);
return error;
}
}
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_MBST, IX_NPEDL_REG_RESET_MBST);
error = npe_cpu_step(sc, IX_NPEDL_INSTR_RESET_MBOX, 0, 0);
if (error != 0) {
DPRINTF(sc->sc_dev, "%s: cannot step (2), error %u\n", __func__, error);
npe_cpu_step_restore(sc);
return error;
}
for (regAddr = 0;
regAddr < IX_NPEDL_TOTAL_NUM_PHYS_REG && error == 0;
regAddr++) {
error = npe_physical_reg_write(sc, regAddr, 0, true);
if (error != 0) {
DPRINTF(sc->sc_dev, "%s: cannot write phy reg, error %u\n",
__func__, error);
npe_cpu_step_restore(sc);
return error;
}
}
for (i = IX_NPEDL_CTXT_NUM_MIN; i <= IX_NPEDL_CTXT_NUM_MAX; i++) {
for (ctxtReg = 0; ctxtReg < IX_NPEDL_CTXT_REG_MAX; ctxtReg++) {
if (!(i == 0 && ctxtReg == IX_NPEDL_CTXT_REG_STEVT)) {
regVal = ixNpeDlCtxtRegResetValues[ctxtReg];
error = npe_ctx_reg_write(sc, i, ctxtReg, regVal, true);
if (error != 0) {
DPRINTF(sc->sc_dev, "%s: cannot write ctx reg, error %u\n",
__func__, error);
npe_cpu_step_restore(sc);
return error;
}
}
}
}
npe_cpu_step_restore(sc);
for (i = 0; i < N(ixNpeDlEcsRegResetValues); i++)
npe_ecs_reg_write(sc,
ixNpeDlEcsRegResetValues[i].regAddr,
ixNpeDlEcsRegResetValues[i].regResetVal);
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_CLR_PROFILE_CNT);
for (regAddr = IX_NPEDL_REG_OFFSET_EXCT;
regAddr <= IX_NPEDL_REG_OFFSET_AP3;
regAddr += sizeof(uint32_t))
npe_reg_write(sc, regAddr, 0);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_WC, 0);
regVal = EXP_BUS_READ_4(ixp425_softc, EXP_FCTRL_OFFSET);
DPRINTFn(2, sc->sc_dev, "%s: FCTRL 0x%x\n", __func__, regVal);
resetNpeParity =
IX_NPEDL_RESET_NPE_PARITY << (1 + sc->sc_unit);
DPRINTFn(2, sc->sc_dev, "%s: FCTRL fuse parity, write 0x%x\n",
__func__, regVal | resetNpeParity);
EXP_BUS_WRITE_4(ixp425_softc, EXP_FCTRL_OFFSET, regVal | resetNpeParity);
DPRINTFn(2, sc->sc_dev, "%s: FCTRL unfuse parity, write 0x%x\n",
__func__, regVal & ~resetNpeParity);
EXP_BUS_WRITE_4(ixp425_softc, EXP_FCTRL_OFFSET, regVal &~ resetNpeParity);
error = npe_cpu_stop(sc);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_CTL,
(ixNpeConfigCtrlRegVal & IX_NPEDL_CONFIG_CTRL_REG_MASK));
DPRINTFn(2, sc->sc_dev, "%s: restore CTL => 0x%x\n",
__func__, npe_reg_read(sc, IX_NPEDL_REG_OFFSET_CTL));
return error;
#undef N
}
static int
npe_cpu_start(struct ixpnpe_softc *sc)
{
uint32_t ecsRegVal;
ecsRegVal = npe_ecs_reg_read(sc, IX_NPEDL_ECS_PRI_1_CTXT_REG_0);
ecsRegVal &= ~IX_NPEDL_MASK_ECS_REG_0_ACTIVE;
npe_ecs_reg_write(sc, IX_NPEDL_ECS_PRI_1_CTXT_REG_0, ecsRegVal);
ecsRegVal = npe_ecs_reg_read(sc, IX_NPEDL_ECS_PRI_2_CTXT_REG_0);
ecsRegVal &= ~IX_NPEDL_MASK_ECS_REG_0_ACTIVE;
npe_ecs_reg_write(sc, IX_NPEDL_ECS_PRI_2_CTXT_REG_0, ecsRegVal);
ecsRegVal = npe_ecs_reg_read(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0);
ecsRegVal &= ~IX_NPEDL_MASK_ECS_REG_0_ACTIVE;
npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, ecsRegVal);
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_START);
return npe_checkbits(sc,
IX_NPEDL_REG_OFFSET_EXCTL, IX_NPEDL_EXCTL_STATUS_RUN) ? 0 : EIO;
}
static int
npe_cpu_stop(struct ixpnpe_softc *sc)
{
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_STOP);
return npe_checkbits(sc,
IX_NPEDL_REG_OFFSET_EXCTL, IX_NPEDL_EXCTL_STATUS_STOP) ? 0 : EIO;
}
#define IX_NPEDL_REG_SIZE_BYTE 8
#define IX_NPEDL_REG_SIZE_SHORT 16
#define IX_NPEDL_REG_SIZE_WORD 32
#define IX_NPEDL_DELAY_READ_CYCLES 2
#define IX_NPEDL_MASK_UNUSED_IMEM_BITS 0x1FFFFFFF;
static void
npe_cmd_issue_write(struct ixpnpe_softc *sc,
uint32_t cmd, uint32_t addr, uint32_t data)
{
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, data);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXAD, addr);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCTL, cmd);
}
static uint32_t
npe_cmd_issue_read(struct ixpnpe_softc *sc, uint32_t cmd, uint32_t addr)
{
uint32_t data;
int i;
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXAD, addr);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCTL, cmd);
for (i = 0; i <= IX_NPEDL_DELAY_READ_CYCLES; i++)
data = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXDATA);
return data;
}
static int
npe_ins_write(struct ixpnpe_softc *sc, uint32_t addr, uint32_t data, int verify)
{
DPRINTFn(4, sc->sc_dev, "%s(0x%x, 0x%x)\n", __func__, addr, data);
npe_cmd_issue_write(sc, IX_NPEDL_EXCTL_CMD_WR_INS_MEM, addr, data);
if (verify) {
uint32_t rdata;
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, ~data);
data &= IX_NPEDL_MASK_UNUSED_IMEM_BITS;
rdata = npe_cmd_issue_read(sc, IX_NPEDL_EXCTL_CMD_RD_INS_MEM, addr);
rdata &= IX_NPEDL_MASK_UNUSED_IMEM_BITS;
if (data != rdata)
return EIO;
}
return 0;
}
static int
npe_data_write(struct ixpnpe_softc *sc, uint32_t addr, uint32_t data, int verify)
{
DPRINTFn(4, sc->sc_dev, "%s(0x%x, 0x%x)\n", __func__, addr, data);
npe_cmd_issue_write(sc, IX_NPEDL_EXCTL_CMD_WR_DATA_MEM, addr, data);
if (verify) {
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, ~data);
if (data != npe_cmd_issue_read(sc, IX_NPEDL_EXCTL_CMD_RD_DATA_MEM, addr))
return EIO;
}
return 0;
}
static void
npe_ecs_reg_write(struct ixpnpe_softc *sc, uint32_t reg, uint32_t data)
{
npe_cmd_issue_write(sc, IX_NPEDL_EXCTL_CMD_WR_ECS_REG, reg, data);
}
static uint32_t
npe_ecs_reg_read(struct ixpnpe_softc *sc, uint32_t reg)
{
return npe_cmd_issue_read(sc, IX_NPEDL_EXCTL_CMD_RD_ECS_REG, reg);
}
static void
npe_issue_cmd(struct ixpnpe_softc *sc, uint32_t command)
{
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCTL, command);
}
static void
npe_cpu_step_save(struct ixpnpe_softc *sc)
{
sc->savedExecCount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXCT);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCT, 0);
sc->savedEcsDbgCtxtReg2 = npe_ecs_reg_read(sc, IX_NPEDL_ECS_DBG_CTXT_REG_2);
npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_2,
(sc->savedEcsDbgCtxtReg2 | IX_NPEDL_MASK_ECS_DBG_REG_2_IF |
IX_NPEDL_MASK_ECS_DBG_REG_2_IE));
}
static int
npe_cpu_step(struct ixpnpe_softc *sc, uint32_t npeInstruction,
uint32_t ctxtNum, uint32_t ldur)
{
#define IX_NPE_DL_MAX_NUM_OF_RETRIES 1000000
uint32_t ecsDbgRegVal;
uint32_t oldWatchcount, newWatchcount;
int tries;
ecsDbgRegVal = IX_NPEDL_MASK_ECS_REG_0_ACTIVE |
(ldur << IX_NPEDL_OFFSET_ECS_REG_0_LDUR);
npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, ecsDbgRegVal);
ecsDbgRegVal = (ctxtNum << IX_NPEDL_OFFSET_ECS_REG_1_CCTXT) |
(ctxtNum << IX_NPEDL_OFFSET_ECS_REG_1_SELCTXT);
npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_1, ecsDbgRegVal);
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
npe_ecs_reg_write(sc, IX_NPEDL_ECS_INSTRUCT_REG, npeInstruction);
oldWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_STEP);
newWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
for (tries = 0; tries < IX_NPE_DL_MAX_NUM_OF_RETRIES &&
newWatchcount == oldWatchcount; tries++) {
newWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
}
return (tries < IX_NPE_DL_MAX_NUM_OF_RETRIES) ? 0 : EIO;
#undef IX_NPE_DL_MAX_NUM_OF_RETRIES
}
static void
npe_cpu_step_restore(struct ixpnpe_softc *sc)
{
npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, 0);
npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCT, sc->savedExecCount);
npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_2, sc->savedEcsDbgCtxtReg2);
}
static int
npe_logical_reg_read(struct ixpnpe_softc *sc,
uint32_t regAddr, uint32_t regSize,
uint32_t ctxtNum, uint32_t *regVal)
{
uint32_t npeInstruction, mask;
int error;
switch (regSize) {
case IX_NPEDL_REG_SIZE_BYTE:
npeInstruction = IX_NPEDL_INSTR_RD_REG_BYTE;
mask = 0xff;
break;
case IX_NPEDL_REG_SIZE_SHORT:
npeInstruction = IX_NPEDL_INSTR_RD_REG_SHORT;
mask = 0xffff;
break;
case IX_NPEDL_REG_SIZE_WORD:
npeInstruction = IX_NPEDL_INSTR_RD_REG_WORD;
mask = 0xffffffff;
break;
default:
return EINVAL;
}
npeInstruction |= (regAddr << IX_NPEDL_OFFSET_INSTR_SRC) |
(regAddr << IX_NPEDL_OFFSET_INSTR_DEST);
error = npe_cpu_step(sc, npeInstruction, ctxtNum, IX_NPEDL_RD_INSTR_LDUR);
if (error != 0) {
DPRINTF(sc->sc_dev, "%s(0x%x, %u, %u), cannot step, error %d\n",
__func__, regAddr, regSize, ctxtNum, error);
return error;
}
*regVal = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXDATA);
*regVal = (*regVal >> (IX_NPEDL_REG_SIZE_WORD - regSize)) & mask;
return 0;
}
static int
npe_logical_reg_write(struct ixpnpe_softc *sc, uint32_t regAddr, uint32_t regVal,
uint32_t regSize, uint32_t ctxtNum, int verify)
{
int error;
DPRINTFn(4, sc->sc_dev, "%s(0x%x, 0x%x, %u, %u)\n",
__func__, regAddr, regVal, regSize, ctxtNum);
if (regSize == IX_NPEDL_REG_SIZE_WORD) {
error = npe_logical_reg_write(sc, regAddr,
regVal >> IX_NPEDL_REG_SIZE_SHORT,
IX_NPEDL_REG_SIZE_SHORT, ctxtNum, verify);
if (error != 0)
return error;
error = npe_logical_reg_write(sc,
regAddr + sizeof(uint16_t),
regVal & 0xffff,
IX_NPEDL_REG_SIZE_SHORT, ctxtNum, verify);
} else {
uint32_t npeInstruction;
switch (regSize) {
case IX_NPEDL_REG_SIZE_BYTE:
npeInstruction = IX_NPEDL_INSTR_WR_REG_BYTE;
regVal &= 0xff;
break;
case IX_NPEDL_REG_SIZE_SHORT:
npeInstruction = IX_NPEDL_INSTR_WR_REG_SHORT;
regVal &= 0xffff;
break;
default:
return EINVAL;
}
npeInstruction |= (regAddr << IX_NPEDL_OFFSET_INSTR_DEST);
npeInstruction |= ((regVal & IX_NPEDL_MASK_IMMED_INSTR_SRC_DATA) <<
IX_NPEDL_OFFSET_INSTR_SRC);
npeInstruction |= ((regVal & IX_NPEDL_MASK_IMMED_INSTR_COPROC_DATA) <<
IX_NPEDL_DISPLACE_IMMED_INSTR_COPROC_DATA);
error = npe_cpu_step(sc, npeInstruction,
ctxtNum, IX_NPEDL_WR_INSTR_LDUR);
}
if (error != 0) {
DPRINTF(sc->sc_dev, "%s(0x%x, 0x%x, %u, %u), error %u writing reg\n",
__func__, regAddr, regVal, regSize, ctxtNum, error);
return error;
}
if (verify) {
uint32_t retRegVal;
error = npe_logical_reg_read(sc, regAddr, regSize, ctxtNum, &retRegVal);
if (error == 0 && regVal != retRegVal)
error = EIO;
}
return error;
}
static int
npe_physical_reg_write(struct ixpnpe_softc *sc,
uint32_t regAddr, uint32_t regValue, int verify)
{
int error;
error = npe_logical_reg_write(sc, IX_NPEDL_CTXT_REG_ADDR_REGMAP,
(regAddr >> IX_NPEDL_OFFSET_PHYS_REG_ADDR_REGMAP),
IX_NPEDL_REG_SIZE_SHORT, 0, verify);
if (error == 0) {
regAddr = (regAddr & IX_NPEDL_MASK_PHYS_REG_ADDR_LOGICAL_ADDR) *
sizeof(uint32_t);
error = npe_logical_reg_write(sc, regAddr, regValue,
IX_NPEDL_REG_SIZE_WORD, 0, verify);
}
return error;
}
static int
npe_ctx_reg_write(struct ixpnpe_softc *sc, uint32_t ctxtNum,
uint32_t ctxtReg, uint32_t ctxtRegVal, int verify)
{
DPRINTFn(4, sc->sc_dev, "%s(%u, %u, %u)\n",
__func__, ctxtNum, ctxtReg, ctxtRegVal);
if (ctxtNum == 0 && ctxtReg == IX_NPEDL_CTXT_REG_STARTPC) {
uint32_t v = npe_ecs_reg_read(sc, IX_NPEDL_ECS_BG_CTXT_REG_0);
v &= ~IX_NPEDL_MASK_ECS_REG_0_NEXTPC;
v |= (ctxtRegVal << IX_NPEDL_OFFSET_ECS_REG_0_NEXTPC) &
IX_NPEDL_MASK_ECS_REG_0_NEXTPC;
npe_ecs_reg_write(sc, IX_NPEDL_ECS_BG_CTXT_REG_0, v);
return 0;
} else {
static const struct {
uint32_t regAddress;
uint32_t regSize;
} regAccInfo[IX_NPEDL_CTXT_REG_MAX] = {
{ IX_NPEDL_CTXT_REG_ADDR_STEVT, IX_NPEDL_REG_SIZE_BYTE },
{ IX_NPEDL_CTXT_REG_ADDR_STARTPC, IX_NPEDL_REG_SIZE_SHORT },
{ IX_NPEDL_CTXT_REG_ADDR_REGMAP, IX_NPEDL_REG_SIZE_SHORT },
{ IX_NPEDL_CTXT_REG_ADDR_CINDEX, IX_NPEDL_REG_SIZE_BYTE }
};
return npe_logical_reg_write(sc, regAccInfo[ctxtReg].regAddress,
ctxtRegVal, regAccInfo[ctxtReg].regSize, ctxtNum, verify);
}
}
#define IX_NPEMH_MAXTRIES 100000
static int
ixpnpe_ofifo_wait(struct ixpnpe_softc *sc)
{
int i;
for (i = 0; i < IX_NPEMH_MAXTRIES; i++) {
if (npe_reg_read(sc, IX_NPESTAT) & IX_NPESTAT_OFNE)
return 1;
DELAY(10);
}
printf("%s: %s: timeout, last status 0x%x\n", device_xname(sc->sc_dev),
__func__, npe_reg_read(sc, IX_NPESTAT));
return 0;
}
static int
ixpnpe_intr(void *arg)
{
struct ixpnpe_softc *sc = arg;
uint32_t status;
status = npe_reg_read(sc, IX_NPESTAT);
if ((status & IX_NPESTAT_OFINT) == 0) {
printf("%s: %s: status 0x%x\n", device_xname(sc->sc_dev), __func__, status);
return(1);
}
sc->sc_msgwaiting = -1;
if (ixpnpe_ofifo_wait(sc)) {
sc->sc_msg[0] = npe_reg_read(sc, IX_NPEFIFO);
if (ixpnpe_ofifo_wait(sc)) {
sc->sc_msg[1] = npe_reg_read(sc, IX_NPEFIFO);
sc->sc_msgwaiting = 1;
}
}
if (sc->sc_msg[0] == (NPE_MACRECOVERYSTART << NPE_MAC_MSGID_SHL)) {
int s;
s = splnet();
delay(100);
if (sc->macresetcbfunc != NULL)
sc->macresetcbfunc(sc->macresetcbarg);
splx(s);
}
#if 0
wakeup(sc);
#endif
return (1);
}
static int
ixpnpe_ififo_wait(struct ixpnpe_softc *sc)
{
int i;
for (i = 0; i < IX_NPEMH_MAXTRIES; i++) {
if (npe_reg_read(sc, IX_NPESTAT) & IX_NPESTAT_IFNF)
return 1;
DELAY(10);
}
return 0;
}
static int
ixpnpe_sendmsg_locked(struct ixpnpe_softc *sc, const uint32_t msg[2])
{
int error = 0;
sc->sc_msgwaiting = 0;
if (ixpnpe_ififo_wait(sc)) {
npe_reg_write(sc, IX_NPEFIFO, msg[0]);
if (ixpnpe_ififo_wait(sc))
npe_reg_write(sc, IX_NPEFIFO, msg[1]);
else
error = EIO;
} else
error = EIO;
if (error)
printf("%s: input FIFO timeout, msg [0x%x,0x%x]\n",
device_xname(sc->sc_dev), msg[0], msg[1]);
return error;
}
static int
ixpnpe_recvmsg_locked(struct ixpnpe_softc *sc, uint32_t msg[2])
{
if (!sc->sc_msgwaiting) {
delay(1000);
}
memcpy(msg, sc->sc_msg, sizeof(sc->sc_msg));
return sc->sc_msgwaiting != 1 ? EIO : 0;
}
int
ixpnpe_sendandrecvmsg(struct ixpnpe_softc *sc,
const uint32_t send[2], uint32_t recv[2])
{
int error;
mutex_enter(&sc->sc_lock);
error = ixpnpe_sendmsg_locked(sc, send);
if (error == 0)
error = ixpnpe_recvmsg_locked(sc, recv);
mutex_exit(&sc->sc_lock);
return error;
}
int
ixpnpe_sendmsg(struct ixpnpe_softc *sc, const uint32_t msg[2])
{
int error;
mutex_enter(&sc->sc_lock);
error = ixpnpe_sendmsg_locked(sc, msg);
mutex_exit(&sc->sc_lock);
return error;
}
int
ixpnpe_recvmsg(struct ixpnpe_softc *sc, uint32_t msg[2])
{
int error;
mutex_enter(&sc->sc_lock);
if (sc->sc_msgwaiting)
memcpy(msg, sc->sc_msg, sizeof(sc->sc_msg));
error = sc->sc_msgwaiting != 1 ? EIO : 0;
mutex_exit(&sc->sc_lock);
return error;
}