Home | History | Annotate | Line # | Download | only in xscale
ixp425_npe.c revision 1.12
      1 /*	$NetBSD: ixp425_npe.c,v 1.12 2021/04/24 23:36:29 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer,
     12  *    without modification.
     13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     15  *    redistribution must be conditioned upon including a substantially
     16  *    similar Disclaimer requirement for further binary redistribution.
     17  *
     18  * NO WARRANTY
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     29  * THE POSSIBILITY OF SUCH DAMAGES.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2001-2005, Intel Corporation.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the Intel Corporation nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
     50  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60 */
     61 #include <sys/cdefs.h>
     62 #if 0
     63 __FBSDID("$FreeBSD: src/sys/arm/xscale/ixp425/ixp425_npe.c,v 1.1 2006/11/19 23:55:23 sam Exp $");
     64 #endif
     65 __KERNEL_RCSID(0, "$NetBSD: ixp425_npe.c,v 1.12 2021/04/24 23:36:29 thorpej Exp $");
     66 
     67 /*
     68  * Intel XScale Network Processing Engine (NPE) support.
     69  *
     70  * Each NPE has an ixpnpeX device associated with it that is
     71  * attached at boot.  Depending on the microcode loaded into
     72  * an NPE there may be an Ethernet interface (npeX) or some
     73  * other network interface (e.g. for ATM).  This file has support
     74  * for loading microcode images and the associated NPE CPU
     75  * manipulations (start, stop, reset).
     76  *
     77  * The code here basically replaces the npeDl and npeMh classes
     78  * in the Intel Access Library (IAL).
     79  *
     80  * NB: Microcode images are loaded with firmware(9).  To
     81  *     include microcode in a static kernel include the
     82  *     ixpnpe_fw device.  Otherwise the firmware will be
     83  *     automatically loaded from the filesystem.
     84  */
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/kernel.h>
     88 #include <sys/malloc.h>
     89 #include <sys/mutex.h>
     90 #include <sys/time.h>
     91 #include <sys/proc.h>
     92 
     93 #include <dev/firmload.h>
     94 
     95 #include <sys/bus.h>
     96 #include <machine/cpu.h>
     97 #include <machine/intr.h>
     98 
     99 #include <arm/xscale/ixp425reg.h>
    100 #include <arm/xscale/ixp425var.h>
    101 #include <arm/xscale/ixp425_ixmevar.h>
    102 
    103 #include <arm/xscale/ixp425_npereg.h>
    104 #include <arm/xscale/ixp425_npevar.h>
    105 #include <arm/xscale/ixp425_if_npereg.h>
    106 
    107 #include "locators.h"
    108 
    109 /*
    110  * IXP425_NPE_MICROCODE will be defined by ixp425-fw.mk IFF the
    111  * microcode object file exists in sys/arch/arm/xscale.
    112  *
    113  * To permit building the NPE drivers without microcode (so they
    114  * don't bitrot due to lack of use), we use "empty" microcode so
    115  * that the NPE drivers will simply fail to start at runtime.
    116  */
    117 #ifdef IXP425_NPE_MICROCODE
    118 extern char	_binary_IxNpeMicrocode_dat_start[];
    119 #else
    120 static char	_binary_IxNpeMicrocode_dat_start[] = {
    121 	0xfe, 0xed, 0xf0, 0x0d, 0xfe, 0xed, 0xf0, 0x0d
    122 };
    123 #endif
    124 
    125 #define	IX_NPEDL_NPEIMAGE_FIELD_MASK	0xff
    126 
    127 /* used to read download map from version in microcode image */
    128 #define IX_NPEDL_BLOCK_TYPE_INSTRUCTION	0x00000000
    129 #define IX_NPEDL_BLOCK_TYPE_DATA	0x00000001
    130 #define IX_NPEDL_BLOCK_TYPE_STATE	0x00000002
    131 #define IX_NPEDL_END_OF_DOWNLOAD_MAP	0x0000000F
    132 
    133 /*
    134  * masks used to extract address info from State information context
    135  * register addresses as read from microcode image
    136  */
    137 #define IX_NPEDL_MASK_STATE_ADDR_CTXT_REG         0x0000000F
    138 #define IX_NPEDL_MASK_STATE_ADDR_CTXT_NUM         0x000000F0
    139 
    140 /* LSB offset of Context Number field in State-Info Context Address */
    141 #define IX_NPEDL_OFFSET_STATE_ADDR_CTXT_NUM       4
    142 
    143 /* size (in words) of single State Information entry (ctxt reg address|data) */
    144 #define IX_NPEDL_STATE_INFO_ENTRY_SIZE	2
    145 
    146 typedef struct {
    147     uint32_t type;
    148     uint32_t offset;
    149 } IxNpeDlNpeMgrDownloadMapBlockEntry;
    150 
    151 typedef union {
    152     IxNpeDlNpeMgrDownloadMapBlockEntry block;
    153     uint32_t eodmMarker;
    154 } IxNpeDlNpeMgrDownloadMapEntry;
    155 
    156 typedef struct {
    157     /* 1st entry in the download map (there may be more than one) */
    158     IxNpeDlNpeMgrDownloadMapEntry entry[1];
    159 } IxNpeDlNpeMgrDownloadMap;
    160 
    161 /* used to access an instruction or data block in a microcode image */
    162 typedef struct {
    163     uint32_t npeMemAddress;
    164     uint32_t size;
    165     uint32_t data[1];
    166 } IxNpeDlNpeMgrCodeBlock;
    167 
    168 /* used to access each Context Reg entry state-information block */
    169 typedef struct {
    170     uint32_t addressInfo;
    171     uint32_t value;
    172 } IxNpeDlNpeMgrStateInfoCtxtRegEntry;
    173 
    174 /* used to access a state-information block in a microcode image */
    175 typedef struct {
    176     uint32_t size;
    177     IxNpeDlNpeMgrStateInfoCtxtRegEntry ctxtRegEntry[1];
    178 } IxNpeDlNpeMgrStateInfoBlock;
    179 
    180 static int npe_debug = 0;
    181 #define	DPRINTF(dev, fmt, ...) do {				\
    182 	if (npe_debug) printf(fmt, __VA_ARGS__);		\
    183 } while (0)
    184 #define	DPRINTFn(n, dev, fmt, ...) do {				\
    185 	if (npe_debug >= n) printf(fmt, __VA_ARGS__);		\
    186 } while (0)
    187 
    188 static int npe_checkbits(struct ixpnpe_softc *, uint32_t reg, uint32_t);
    189 static int npe_isstopped(struct ixpnpe_softc *);
    190 static int npe_load_ins(struct ixpnpe_softc *,
    191 		const IxNpeDlNpeMgrCodeBlock *bp, int verify);
    192 static int npe_load_data(struct ixpnpe_softc *,
    193 		const IxNpeDlNpeMgrCodeBlock *bp, int verify);
    194 static int npe_load_stateinfo(struct ixpnpe_softc *,
    195 		const IxNpeDlNpeMgrStateInfoBlock *bp, int verify);
    196 static int npe_load_image(struct ixpnpe_softc *,
    197 		const uint32_t *imageCodePtr, int verify);
    198 static int npe_cpu_reset(struct ixpnpe_softc *);
    199 static int npe_cpu_start(struct ixpnpe_softc *);
    200 static int npe_cpu_stop(struct ixpnpe_softc *);
    201 static void npe_cmd_issue_write(struct ixpnpe_softc *,
    202 		uint32_t cmd, uint32_t addr, uint32_t data);
    203 static uint32_t npe_cmd_issue_read(struct ixpnpe_softc *,
    204 		uint32_t cmd, uint32_t addr);
    205 static int npe_ins_write(struct ixpnpe_softc *,
    206 		uint32_t addr, uint32_t data, int verify);
    207 static int npe_data_write(struct ixpnpe_softc *,
    208 		uint32_t addr, uint32_t data, int verify);
    209 static void npe_ecs_reg_write(struct ixpnpe_softc *,
    210 		uint32_t reg, uint32_t data);
    211 static uint32_t npe_ecs_reg_read(struct ixpnpe_softc *, uint32_t reg);
    212 static void npe_issue_cmd(struct ixpnpe_softc *, uint32_t command);
    213 static void npe_cpu_step_save(struct ixpnpe_softc *);
    214 static int npe_cpu_step(struct ixpnpe_softc *, uint32_t npeInstruction,
    215 		uint32_t ctxtNum, uint32_t ldur);
    216 static void npe_cpu_step_restore(struct ixpnpe_softc *);
    217 static int npe_logical_reg_read(struct ixpnpe_softc *,
    218 		uint32_t regAddr, uint32_t regSize,
    219 		uint32_t ctxtNum, uint32_t *regVal);
    220 static int npe_logical_reg_write(struct ixpnpe_softc *,
    221 		uint32_t regAddr, uint32_t regVal,
    222 		uint32_t regSize, uint32_t ctxtNum, int verify);
    223 static int npe_physical_reg_write(struct ixpnpe_softc *,
    224 		uint32_t regAddr, uint32_t regValue, int verify);
    225 static int npe_ctx_reg_write(struct ixpnpe_softc *, uint32_t ctxtNum,
    226 		uint32_t ctxtReg, uint32_t ctxtRegVal, int verify);
    227 
    228 static int ixpnpe_intr(void *arg);
    229 
    230 static uint32_t
    231 npe_reg_read(struct ixpnpe_softc *sc, bus_size_t off)
    232 {
    233     uint32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, off);
    234     DPRINTFn(9, sc->sc_dev, "%s(0x%lx) => 0x%x\n", __func__, off, v);
    235     return v;
    236 }
    237 
    238 static void
    239 npe_reg_write(struct ixpnpe_softc *sc, bus_size_t off, uint32_t val)
    240 {
    241     DPRINTFn(9, sc->sc_dev, "%s(0x%lx, 0x%x)\n", __func__, off, val);
    242     bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    243 }
    244 
    245 static int	ixpnpe_match(device_t, cfdata_t, void *);
    246 static void	ixpnpe_attach(device_t, device_t, void *);
    247 static int	ixpnpe_print(void *, const char *);
    248 static int	ixpnpe_search(device_t, cfdata_t, const int *, void *);
    249 
    250 CFATTACH_DECL_NEW(ixpnpe, sizeof(struct ixpnpe_softc),
    251     ixpnpe_match, ixpnpe_attach, NULL, NULL);
    252 
    253 static int
    254 ixpnpe_match(device_t parent, cfdata_t match, void *arg)
    255 {
    256 	struct ixme_attach_args *ixa = arg;
    257 
    258 	return (ixa->ixa_npe == 1 || ixa->ixa_npe == 2);
    259 }
    260 
    261 static void
    262 ixpnpe_attach(device_t parent, device_t self, void *arg)
    263 {
    264     struct ixpnpe_softc *sc = device_private(self);
    265     struct ixme_attach_args *ixa = arg;
    266     bus_addr_t base;
    267     int irq;
    268 
    269     aprint_naive("\n");
    270     aprint_normal("\n");
    271 
    272     sc->sc_dev = self;
    273     sc->sc_iot = ixa->ixa_iot;
    274     sc->sc_dt = ixa->ixa_dt;
    275     sc->sc_unit = ixa->ixa_npe;
    276 
    277     mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    278 
    279     /* XXX: Check features to ensure this NPE is enabled */
    280 
    281     switch (ixa->ixa_npe) {
    282     default:
    283 	panic("%s: Invalid NPE!", device_xname(self));
    284 
    285     case 1:
    286 	base = IXP425_NPE_B_HWBASE;
    287 	sc->sc_size = IXP425_NPE_B_SIZE;
    288 	irq = IXP425_INT_NPE_B;
    289 
    290 	/* size of instruction memory */
    291 	sc->insMemSize = IX_NPEDL_INS_MEMSIZE_WORDS_NPEB;
    292 	/* size of data memory */
    293 	sc->dataMemSize = IX_NPEDL_DATA_MEMSIZE_WORDS_NPEB;
    294 	break;
    295 
    296     case 2:
    297 	base = IXP425_NPE_C_HWBASE;
    298 	sc->sc_size = IXP425_NPE_C_SIZE;
    299 	irq = IXP425_INT_NPE_C;
    300 
    301 	/* size of instruction memory */
    302 	sc->insMemSize = IX_NPEDL_INS_MEMSIZE_WORDS_NPEC;
    303 	/* size of data memory */
    304 	sc->dataMemSize = IX_NPEDL_DATA_MEMSIZE_WORDS_NPEC;
    305 	break;
    306     }
    307     if (bus_space_map(sc->sc_iot, base, sc->sc_size, 0, &sc->sc_ioh))
    308 	panic("%s: Cannot map registers", device_xname(self));
    309 
    310     /*
    311      * Setup IRQ and handler for NPE message support.
    312      */
    313     sc->sc_ih = ixp425_intr_establish(irq, IPL_NET, ixpnpe_intr, sc);
    314     if (sc->sc_ih == NULL)
    315 	panic("%s: Unable to establish irq %u", device_xname(self), irq);
    316     /* enable output fifo interrupts (NB: must also set OFIFO Write Enable) */
    317     npe_reg_write(sc, IX_NPECTL,
    318 	npe_reg_read(sc, IX_NPECTL) | (IX_NPECTL_OFE | IX_NPECTL_OFWE));
    319 
    320     config_search(self, ixa,
    321 	CFARG_SEARCH, ixpnpe_search,
    322 	CFARG_EOL);
    323 }
    324 
    325 static int
    326 ixpnpe_print(void *arg, const char *name)
    327 {
    328 
    329 	return (UNCONF);
    330 }
    331 
    332 static int
    333 ixpnpe_search(device_t parent, cfdata_t cf, const int *ldesc, void *arg)
    334 {
    335 	struct ixpnpe_softc *sc = device_private(parent);
    336 	struct ixme_attach_args *ixa = arg;
    337 	struct ixpnpe_attach_args na;
    338 
    339 	na.na_unit = ixa->ixa_npe;
    340 	na.na_phy = cf->cf_loc[IXPNPECF_PHY];
    341 	na.na_npe = sc;
    342 	na.na_iot = ixa->ixa_iot;
    343 	na.na_dt = ixa->ixa_dt;
    344 
    345 	if (config_probe(parent, cf, &na)) {
    346 		config_attach(parent, cf, &na, ixpnpe_print, CFARG_EOL);
    347 		return (1);
    348 	}
    349 
    350 	return (0);
    351 }
    352 
    353 int
    354 ixpnpe_stopandreset(struct ixpnpe_softc *sc)
    355 {
    356     int error;
    357 
    358     mutex_enter(&sc->sc_lock);
    359     error = npe_cpu_stop(sc);		/* stop NPE */
    360     if (error == 0)
    361 	error = npe_cpu_reset(sc);	/* reset it */
    362     if (error == 0)
    363 	sc->started = 0;		/* mark stopped */
    364     mutex_exit(&sc->sc_lock);
    365 
    366     DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
    367     return error;
    368 }
    369 
    370 static int
    371 ixpnpe_start_locked(struct ixpnpe_softc *sc)
    372 {
    373     int error;
    374 
    375     if (!sc->started) {
    376 	error = npe_cpu_start(sc);
    377 	if (error == 0)
    378 	    sc->started = 1;
    379     } else
    380 	error = 0;
    381 
    382     DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
    383     return error;
    384 }
    385 
    386 int
    387 ixpnpe_start(struct ixpnpe_softc *sc)
    388 {
    389 	int ret;
    390 
    391 	mutex_enter(&sc->sc_lock);
    392 	ret = ixpnpe_start_locked(sc);
    393 	mutex_exit(&sc->sc_lock);
    394 	return (ret);
    395 }
    396 
    397 int
    398 ixpnpe_stop(struct ixpnpe_softc *sc)
    399 {
    400     int error;
    401 
    402     mutex_enter(&sc->sc_lock);
    403     error = npe_cpu_stop(sc);
    404     if (error == 0)
    405 	sc->started = 0;
    406     mutex_exit(&sc->sc_lock);
    407 
    408     DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
    409     return error;
    410 }
    411 
    412 /*
    413  * Indicates the start of an NPE Image, in new NPE Image Library format.
    414  * 2 consecutive occurances indicates the end of the NPE Image Library
    415  */
    416 #define NPE_IMAGE_MARKER 0xfeedf00d
    417 
    418 /*
    419  * NPE Image Header definition, used in new NPE Image Library format
    420  */
    421 typedef struct {
    422     uint32_t marker;
    423     uint32_t id;
    424     uint32_t size;
    425 } IxNpeDlImageMgrImageHeader;
    426 
    427 static int
    428 npe_findimage(struct ixpnpe_softc *sc,
    429     const uint32_t *imageLibrary, uint32_t imageId,
    430     const uint32_t **imagePtr, uint32_t *imageSize)
    431 {
    432     const IxNpeDlImageMgrImageHeader *image;
    433     uint32_t offset = 0;
    434 
    435     while (imageLibrary[offset] == NPE_IMAGE_MARKER) {
    436         image = (const IxNpeDlImageMgrImageHeader *)&imageLibrary[offset];
    437         offset += sizeof(IxNpeDlImageMgrImageHeader)/sizeof(uint32_t);
    438 
    439         DPRINTF(sc->sc_dev, "%s: off %u mark 0x%x id 0x%x size %u\n",
    440 	    __func__, offset, image->marker, image->id, image->size);
    441         if (image->id == imageId) {
    442             *imagePtr = imageLibrary + offset;
    443             *imageSize = image->size;
    444             return 0;
    445         }
    446         /* 2 consecutive NPE_IMAGE_MARKER's indicates end of library */
    447         if (image->id == NPE_IMAGE_MARKER) {
    448 	    printf("%s: imageId 0x%08x not found in image library header\n",
    449 	        device_xname(sc->sc_dev), imageId);
    450             /* reached end of library, image not found */
    451             return EIO;
    452         }
    453         offset += image->size;
    454     }
    455     return EIO;
    456 }
    457 
    458 int
    459 ixpnpe_init(struct ixpnpe_softc *sc, const char *imageName, uint32_t imageId)
    460 {
    461     uint32_t imageSize;
    462     const uint32_t *imageCodePtr;
    463     void *fw;
    464     int error;
    465 
    466     DPRINTF(sc->sc_dev, "load %s, imageId 0x%08x\n", imageName, imageId);
    467 
    468 #if 0
    469     IxFeatureCtrlDeviceId devid = IX_NPEDL_DEVICEID_FROM_IMAGEID_GET(imageId);
    470     /*
    471      * Checking if image being loaded is meant for device that is running.
    472      * Image is forward compatible. i.e Image built for IXP42X should run
    473      * on IXP46X but not vice versa.
    474      */
    475     if (devid > (ixFeatureCtrlDeviceRead() & IX_FEATURE_CTRL_DEVICE_TYPE_MASK))
    476 	return EINVAL;
    477 #endif
    478     error = ixpnpe_stopandreset(sc);		/* stop and reset the NPE */
    479     if (error != 0)
    480 	return error;
    481 
    482     fw = (void *)_binary_IxNpeMicrocode_dat_start;
    483 
    484     /* Locate desired image in files w/ combined images */
    485     error = npe_findimage(sc, (void *)fw /*fw->data*/, imageId, &imageCodePtr, &imageSize);
    486     if (error != 0)
    487 	goto done;
    488 
    489     /*
    490      * If download was successful, store image Id in list of
    491      * currently loaded images. If a critical error occured
    492      * during download, record that the NPE has an invalid image
    493      */
    494     mutex_enter(&sc->sc_lock);
    495     error = npe_load_image(sc, imageCodePtr, 1 /*VERIFY*/);
    496     if (error == 0) {
    497 	sc->validImage = 1;
    498 	error = ixpnpe_start_locked(sc);
    499     } else {
    500 	sc->validImage = 0;
    501     }
    502     sc->functionalityId = IX_NPEDL_FUNCTIONID_FROM_IMAGEID_GET(imageId);
    503     mutex_exit(&sc->sc_lock);
    504 done:
    505     DPRINTF(sc->sc_dev, "%s: error %d\n", __func__, error);
    506     return error;
    507 }
    508 
    509 int
    510 ixpnpe_getfunctionality(struct ixpnpe_softc *sc)
    511 {
    512     return (sc->validImage ? sc->functionalityId : 0);
    513 }
    514 
    515 static int
    516 npe_checkbits(struct ixpnpe_softc *sc, uint32_t reg, uint32_t expectedBitsSet)
    517 {
    518     uint32_t val;
    519 
    520     val = npe_reg_read(sc, reg);
    521     DPRINTFn(5, sc->sc_dev, "%s(0x%x, 0x%x) => 0x%x (%u)\n",
    522 	__func__, reg, expectedBitsSet, val,
    523 	(val & expectedBitsSet) == expectedBitsSet);
    524     return ((val & expectedBitsSet) == expectedBitsSet);
    525 }
    526 
    527 static int
    528 npe_isstopped(struct ixpnpe_softc *sc)
    529 {
    530     return npe_checkbits(sc,
    531 	IX_NPEDL_REG_OFFSET_EXCTL, IX_NPEDL_EXCTL_STATUS_STOP);
    532 }
    533 
    534 static int
    535 npe_load_ins(struct ixpnpe_softc *sc,
    536     const IxNpeDlNpeMgrCodeBlock *bp, int verify)
    537 {
    538     uint32_t npeMemAddress;
    539     int i, blockSize;
    540 
    541     npeMemAddress = bp->npeMemAddress;
    542     blockSize = bp->size;		/* NB: instruction/data count */
    543     if (npeMemAddress + blockSize > sc->insMemSize) {
    544 	printf("%s: Block size too big for NPE memory\n", device_xname(sc->sc_dev));
    545 	return EINVAL;	/* XXX */
    546     }
    547     for (i = 0; i < blockSize; i++, npeMemAddress++) {
    548 	if (npe_ins_write(sc, npeMemAddress, bp->data[i], verify) != 0) {
    549 	    printf("%s: NPE instruction write failed", device_xname(sc->sc_dev));
    550 	    return EIO;
    551 	}
    552     }
    553     return 0;
    554 }
    555 
    556 static int
    557 npe_load_data(struct ixpnpe_softc *sc,
    558     const IxNpeDlNpeMgrCodeBlock *bp, int verify)
    559 {
    560     uint32_t npeMemAddress;
    561     int i, blockSize;
    562 
    563     npeMemAddress = bp->npeMemAddress;
    564     blockSize = bp->size;		/* NB: instruction/data count */
    565     if (npeMemAddress + blockSize > sc->dataMemSize) {
    566 	printf("%s: Block size too big for NPE memory\n", device_xname(sc->sc_dev));
    567 	return EINVAL;
    568     }
    569     for (i = 0; i < blockSize; i++, npeMemAddress++) {
    570 	if (npe_data_write(sc, npeMemAddress, bp->data[i], verify) != 0) {
    571 	    printf("%s: NPE data write failed\n", device_xname(sc->sc_dev));
    572 	    return EIO;
    573 	}
    574     }
    575     return 0;
    576 }
    577 
    578 static int
    579 npe_load_stateinfo(struct ixpnpe_softc *sc,
    580     const IxNpeDlNpeMgrStateInfoBlock *bp, int verify)
    581 {
    582     int i, nentries, error;
    583 
    584     npe_cpu_step_save(sc);
    585 
    586     /* for each state-info context register entry in block */
    587     nentries = bp->size / IX_NPEDL_STATE_INFO_ENTRY_SIZE;
    588     error = 0;
    589     for (i = 0; i < nentries; i++) {
    590 	/* each state-info entry is 2 words (address, value) in length */
    591 	uint32_t regVal = bp->ctxtRegEntry[i].value;
    592 	uint32_t addrInfo = bp->ctxtRegEntry[i].addressInfo;
    593 
    594 	uint32_t reg = (addrInfo & IX_NPEDL_MASK_STATE_ADDR_CTXT_REG);
    595 	uint32_t cNum = (addrInfo & IX_NPEDL_MASK_STATE_ADDR_CTXT_NUM) >>
    596 	    IX_NPEDL_OFFSET_STATE_ADDR_CTXT_NUM;
    597 
    598 	/* error-check Context Register No. and Context Number values  */
    599 	if (reg >= IX_NPEDL_CTXT_REG_MAX) {
    600 	    printf("%s: invalid Context Register %u\n", device_xname(sc->sc_dev),
    601 		reg);
    602 	    error = EINVAL;
    603 	    break;
    604 	}
    605 	if (cNum >= IX_NPEDL_CTXT_NUM_MAX) {
    606 	    printf("%s: invalid Context Number %u\n", device_xname(sc->sc_dev),
    607 	        cNum);
    608 	    error = EINVAL;
    609 	    break;
    610 	}
    611 	/* NOTE that there is no STEVT register for Context 0 */
    612 	if (cNum == 0 && reg == IX_NPEDL_CTXT_REG_STEVT) {
    613 	    printf("%s: no STEVT for Context 0\n", device_xname(sc->sc_dev));
    614 	    error = EINVAL;
    615 	    break;
    616 	}
    617 
    618 	if (npe_ctx_reg_write(sc, cNum, reg, regVal, verify) != 0) {
    619 	    printf("%s: write of state-info to NPE failed\n",
    620 	        device_xname(sc->sc_dev));
    621 	    error = EIO;
    622 	    break;
    623 	}
    624     }
    625 
    626     npe_cpu_step_restore(sc);
    627     return error;
    628 }
    629 
    630 static int
    631 npe_load_image(struct ixpnpe_softc *sc,
    632     const uint32_t *imageCodePtr, int verify)
    633 {
    634 #define	EOM(marker)	((marker) == IX_NPEDL_END_OF_DOWNLOAD_MAP)
    635     const IxNpeDlNpeMgrDownloadMap *downloadMap;
    636     int i, error;
    637 
    638     if (!npe_isstopped(sc)) {		/* verify NPE is stopped */
    639 	printf("%s: cannot load image, NPE not stopped\n", device_xname(sc->sc_dev));
    640 	return EIO;
    641     }
    642 
    643     /*
    644      * Read Download Map, checking each block type and calling
    645      * appropriate function to perform download
    646      */
    647     error = 0;
    648     downloadMap = (const IxNpeDlNpeMgrDownloadMap *) imageCodePtr;
    649     for (i = 0; !EOM(downloadMap->entry[i].eodmMarker); i++) {
    650 	/* calculate pointer to block to be downloaded */
    651 	const uint32_t *bp = imageCodePtr + downloadMap->entry[i].block.offset;
    652 	switch (downloadMap->entry[i].block.type) {
    653 	case IX_NPEDL_BLOCK_TYPE_INSTRUCTION:
    654 	    error = npe_load_ins(sc,
    655 			 (const IxNpeDlNpeMgrCodeBlock *) bp, verify);
    656 	    DPRINTF(sc->sc_dev, "%s: inst, error %d\n", __func__, error);
    657 	    break;
    658 	case IX_NPEDL_BLOCK_TYPE_DATA:
    659 	    error = npe_load_data(sc,
    660 			 (const IxNpeDlNpeMgrCodeBlock *) bp, verify);
    661 	    DPRINTF(sc->sc_dev, "%s: data, error %d\n", __func__, error);
    662 	    break;
    663 	case IX_NPEDL_BLOCK_TYPE_STATE:
    664 	    error = npe_load_stateinfo(sc,
    665 			 (const IxNpeDlNpeMgrStateInfoBlock *) bp, verify);
    666 	    DPRINTF(sc->sc_dev, "%s: state, error %d\n", __func__, error);
    667 	    break;
    668 	default:
    669 	    printf("%s: unknown block type 0x%x in download map\n",
    670 		device_xname(sc->sc_dev), downloadMap->entry[i].block.type);
    671 	    error = EIO;		/* XXX */
    672 	    break;
    673 	}
    674 	if (error != 0)
    675 	    break;
    676     }
    677     return error;
    678 #undef EOM
    679 }
    680 
    681 /* contains Reset values for Context Store Registers  */
    682 static const struct {
    683     uint32_t regAddr;
    684     uint32_t regResetVal;
    685 } ixNpeDlEcsRegResetValues[] = {
    686     { IX_NPEDL_ECS_BG_CTXT_REG_0,    IX_NPEDL_ECS_BG_CTXT_REG_0_RESET },
    687     { IX_NPEDL_ECS_BG_CTXT_REG_1,    IX_NPEDL_ECS_BG_CTXT_REG_1_RESET },
    688     { IX_NPEDL_ECS_BG_CTXT_REG_2,    IX_NPEDL_ECS_BG_CTXT_REG_2_RESET },
    689     { IX_NPEDL_ECS_PRI_1_CTXT_REG_0, IX_NPEDL_ECS_PRI_1_CTXT_REG_0_RESET },
    690     { IX_NPEDL_ECS_PRI_1_CTXT_REG_1, IX_NPEDL_ECS_PRI_1_CTXT_REG_1_RESET },
    691     { IX_NPEDL_ECS_PRI_1_CTXT_REG_2, IX_NPEDL_ECS_PRI_1_CTXT_REG_2_RESET },
    692     { IX_NPEDL_ECS_PRI_2_CTXT_REG_0, IX_NPEDL_ECS_PRI_2_CTXT_REG_0_RESET },
    693     { IX_NPEDL_ECS_PRI_2_CTXT_REG_1, IX_NPEDL_ECS_PRI_2_CTXT_REG_1_RESET },
    694     { IX_NPEDL_ECS_PRI_2_CTXT_REG_2, IX_NPEDL_ECS_PRI_2_CTXT_REG_2_RESET },
    695     { IX_NPEDL_ECS_DBG_CTXT_REG_0,   IX_NPEDL_ECS_DBG_CTXT_REG_0_RESET },
    696     { IX_NPEDL_ECS_DBG_CTXT_REG_1,   IX_NPEDL_ECS_DBG_CTXT_REG_1_RESET },
    697     { IX_NPEDL_ECS_DBG_CTXT_REG_2,   IX_NPEDL_ECS_DBG_CTXT_REG_2_RESET },
    698     { IX_NPEDL_ECS_INSTRUCT_REG,     IX_NPEDL_ECS_INSTRUCT_REG_RESET }
    699 };
    700 
    701 /* contains Reset values for Context Store Registers  */
    702 static const uint32_t ixNpeDlCtxtRegResetValues[] = {
    703     IX_NPEDL_CTXT_REG_RESET_STEVT,
    704     IX_NPEDL_CTXT_REG_RESET_STARTPC,
    705     IX_NPEDL_CTXT_REG_RESET_REGMAP,
    706     IX_NPEDL_CTXT_REG_RESET_CINDEX,
    707 };
    708 
    709 #define	IX_NPEDL_RESET_NPE_PARITY	0x0800
    710 #define	IX_NPEDL_PARITY_BIT_MASK	0x3F00FFFF
    711 #define	IX_NPEDL_CONFIG_CTRL_REG_MASK	0x3F3FFFFF
    712 
    713 static int
    714 npe_cpu_reset(struct ixpnpe_softc *sc)
    715 {
    716 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    717     uint32_t ctxtReg; /* identifies Context Store reg (0-3) */
    718     uint32_t regAddr;
    719     uint32_t regVal;
    720     uint32_t resetNpeParity;
    721     uint32_t ixNpeConfigCtrlRegVal;
    722     int i, error = 0;
    723 
    724     /* pre-store the NPE Config Control Register Value */
    725     ixNpeConfigCtrlRegVal = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_CTL);
    726     ixNpeConfigCtrlRegVal |= 0x3F000000;
    727 
    728     /* disable the parity interrupt */
    729     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_CTL,
    730 	(ixNpeConfigCtrlRegVal & IX_NPEDL_PARITY_BIT_MASK));
    731     DPRINTFn(2, sc->sc_dev, "%s: dis parity int, CTL => 0x%x\n",
    732 	__func__, ixNpeConfigCtrlRegVal & IX_NPEDL_PARITY_BIT_MASK);
    733 
    734     npe_cpu_step_save(sc);
    735 
    736     /*
    737      * Clear the FIFOs.
    738      */
    739     while (npe_checkbits(sc,
    740 	  IX_NPEDL_REG_OFFSET_WFIFO, IX_NPEDL_MASK_WFIFO_VALID)) {
    741 	/* read from the Watch-point FIFO until empty */
    742 	(void) npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WFIFO);
    743     }
    744 
    745     while (npe_checkbits(sc,
    746 	  IX_NPEDL_REG_OFFSET_STAT, IX_NPEDL_MASK_STAT_OFNE)) {
    747 	/* read from the outFIFO until empty */
    748 	(void) npe_reg_read(sc, IX_NPEDL_REG_OFFSET_FIFO);
    749     }
    750 
    751     while (npe_checkbits(sc,
    752 	  IX_NPEDL_REG_OFFSET_STAT, IX_NPEDL_MASK_STAT_IFNE)) {
    753 	/*
    754 	 * Step execution of the NPE intruction to read inFIFO using
    755 	 * the Debug Executing Context stack.
    756 	 */
    757 	error = npe_cpu_step(sc, IX_NPEDL_INSTR_RD_FIFO, 0, 0);
    758 	if (error != 0) {
    759 	    DPRINTF(sc->sc_dev, "%s: cannot step (1), error %u\n",
    760 		__func__, error);
    761 	    npe_cpu_step_restore(sc);
    762 	    return error;
    763 	}
    764     }
    765 
    766     /*
    767      * Reset the mailbox reg
    768      */
    769     /* ...from XScale side */
    770     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_MBST, IX_NPEDL_REG_RESET_MBST);
    771     /* ...from NPE side */
    772     error = npe_cpu_step(sc, IX_NPEDL_INSTR_RESET_MBOX, 0, 0);
    773     if (error != 0) {
    774 	DPRINTF(sc->sc_dev, "%s: cannot step (2), error %u\n", __func__, error);
    775 	npe_cpu_step_restore(sc);
    776         return error;
    777     }
    778 
    779     /*
    780      * Reset the physical registers in the NPE register file:
    781      * Note: no need to save/restore REGMAP for Context 0 here
    782      * since all Context Store regs are reset in subsequent code.
    783      */
    784     for (regAddr = 0;
    785 	 regAddr < IX_NPEDL_TOTAL_NUM_PHYS_REG && error == 0;
    786 	 regAddr++) {
    787 	/* for each physical register in the NPE reg file, write 0 : */
    788 	error = npe_physical_reg_write(sc, regAddr, 0, true);
    789 	if (error != 0) {
    790 	    DPRINTF(sc->sc_dev, "%s: cannot write phy reg, error %u\n",
    791 		__func__, error);
    792 	    npe_cpu_step_restore(sc);
    793 	    return error;		/* abort reset */
    794 	}
    795     }
    796 
    797     /*
    798      * Reset the context store:
    799      */
    800     for (i = IX_NPEDL_CTXT_NUM_MIN; i <= IX_NPEDL_CTXT_NUM_MAX; i++) {
    801 	/* set each context's Context Store registers to reset values: */
    802 	for (ctxtReg = 0; ctxtReg < IX_NPEDL_CTXT_REG_MAX; ctxtReg++) {
    803 	    /* NOTE that there is no STEVT register for Context 0 */
    804 	    if (!(i == 0 && ctxtReg == IX_NPEDL_CTXT_REG_STEVT)) {
    805 		regVal = ixNpeDlCtxtRegResetValues[ctxtReg];
    806 		error = npe_ctx_reg_write(sc, i, ctxtReg, regVal, true);
    807 		if (error != 0) {
    808 		    DPRINTF(sc->sc_dev, "%s: cannot write ctx reg, error %u\n",
    809 			__func__, error);
    810 		    npe_cpu_step_restore(sc);
    811 		    return error;	 /* abort reset */
    812 		}
    813 	    }
    814 	}
    815     }
    816 
    817     npe_cpu_step_restore(sc);
    818 
    819     /* write Reset values to Execution Context Stack registers */
    820     for (i = 0; i < N(ixNpeDlEcsRegResetValues); i++)
    821 	npe_ecs_reg_write(sc,
    822 	    ixNpeDlEcsRegResetValues[i].regAddr,
    823 	    ixNpeDlEcsRegResetValues[i].regResetVal);
    824 
    825     /* clear the profile counter */
    826     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_CLR_PROFILE_CNT);
    827 
    828     /* clear registers EXCT, AP0, AP1, AP2 and AP3 */
    829     for (regAddr = IX_NPEDL_REG_OFFSET_EXCT;
    830 	 regAddr <= IX_NPEDL_REG_OFFSET_AP3;
    831 	 regAddr += sizeof(uint32_t))
    832 	npe_reg_write(sc, regAddr, 0);
    833 
    834     /* Reset the Watch-count register */
    835     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_WC, 0);
    836 
    837     /*
    838      * WR IXA00055043 - Remove IMEM Parity Introduced by NPE Reset Operation
    839      */
    840 
    841     /*
    842      * Reset the NPE and its coprocessor - to reset internal
    843      * states and remove parity error.  Note this makes no
    844      * sense based on the documentation.  The feature control
    845      * register always reads back as 0 on the ixp425 and further
    846      * the bit definition of NPEA/NPEB is off by 1 according to
    847      * the Intel documention--so we're blindly following the
    848      * Intel code w/o any real understanding.
    849      */
    850     regVal = EXP_BUS_READ_4(ixp425_softc, EXP_FCTRL_OFFSET);
    851     DPRINTFn(2, sc->sc_dev, "%s: FCTRL 0x%x\n", __func__, regVal);
    852     resetNpeParity =
    853 	IX_NPEDL_RESET_NPE_PARITY << (1 + sc->sc_unit);
    854     DPRINTFn(2, sc->sc_dev, "%s: FCTRL fuse parity, write 0x%x\n",
    855 	__func__, regVal | resetNpeParity);
    856     EXP_BUS_WRITE_4(ixp425_softc, EXP_FCTRL_OFFSET, regVal | resetNpeParity);
    857 
    858     /* un-fuse and un-reset the NPE & coprocessor */
    859     DPRINTFn(2, sc->sc_dev, "%s: FCTRL unfuse parity, write 0x%x\n",
    860 	__func__, regVal & ~resetNpeParity);
    861     EXP_BUS_WRITE_4(ixp425_softc, EXP_FCTRL_OFFSET, regVal &~ resetNpeParity);
    862 
    863     /*
    864      * Call NpeMgr function to stop the NPE again after the Feature Control
    865      * has unfused and Un-Reset the NPE and its associated Coprocessors.
    866      */
    867     error = npe_cpu_stop(sc);
    868 
    869     /* restore NPE configuration bus Control Register - Parity Settings  */
    870     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_CTL,
    871         (ixNpeConfigCtrlRegVal & IX_NPEDL_CONFIG_CTRL_REG_MASK));
    872     DPRINTFn(2, sc->sc_dev, "%s: restore CTL => 0x%x\n",
    873 	__func__, npe_reg_read(sc, IX_NPEDL_REG_OFFSET_CTL));
    874 
    875     return error;
    876 #undef N
    877 }
    878 
    879 static int
    880 npe_cpu_start(struct ixpnpe_softc *sc)
    881 {
    882     uint32_t ecsRegVal;
    883 
    884     /*
    885      * Ensure only Background Context Stack Level is Active by turning off
    886      * the Active bit in each of the other Executing Context Stack levels.
    887      */
    888     ecsRegVal = npe_ecs_reg_read(sc, IX_NPEDL_ECS_PRI_1_CTXT_REG_0);
    889     ecsRegVal &= ~IX_NPEDL_MASK_ECS_REG_0_ACTIVE;
    890     npe_ecs_reg_write(sc, IX_NPEDL_ECS_PRI_1_CTXT_REG_0, ecsRegVal);
    891 
    892     ecsRegVal = npe_ecs_reg_read(sc, IX_NPEDL_ECS_PRI_2_CTXT_REG_0);
    893     ecsRegVal &= ~IX_NPEDL_MASK_ECS_REG_0_ACTIVE;
    894     npe_ecs_reg_write(sc, IX_NPEDL_ECS_PRI_2_CTXT_REG_0, ecsRegVal);
    895 
    896     ecsRegVal = npe_ecs_reg_read(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0);
    897     ecsRegVal &= ~IX_NPEDL_MASK_ECS_REG_0_ACTIVE;
    898     npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, ecsRegVal);
    899 
    900     /* clear the pipeline */
    901     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
    902 
    903     /* start NPE execution by issuing command through EXCTL register on NPE */
    904     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_START);
    905 
    906     /*
    907      * Check execution status of NPE to verify operation was successful.
    908      */
    909     return npe_checkbits(sc,
    910 	IX_NPEDL_REG_OFFSET_EXCTL, IX_NPEDL_EXCTL_STATUS_RUN) ? 0 : EIO;
    911 }
    912 
    913 static int
    914 npe_cpu_stop(struct ixpnpe_softc *sc)
    915 {
    916     /* stop NPE execution by issuing command through EXCTL register on NPE */
    917     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_STOP);
    918 
    919     /* verify that NPE Stop was successful */
    920     return npe_checkbits(sc,
    921 	IX_NPEDL_REG_OFFSET_EXCTL, IX_NPEDL_EXCTL_STATUS_STOP) ? 0 : EIO;
    922 }
    923 
    924 #define IX_NPEDL_REG_SIZE_BYTE            8
    925 #define IX_NPEDL_REG_SIZE_SHORT           16
    926 #define IX_NPEDL_REG_SIZE_WORD            32
    927 
    928 /*
    929  * Introduce extra read cycles after issuing read command to NPE
    930  * so that we read the register after the NPE has updated it
    931  * This is to overcome race condition between XScale and NPE
    932  */
    933 #define IX_NPEDL_DELAY_READ_CYCLES        2
    934 /*
    935  * To mask top three MSBs of 32bit word to download into NPE IMEM
    936  */
    937 #define IX_NPEDL_MASK_UNUSED_IMEM_BITS    0x1FFFFFFF;
    938 
    939 static void
    940 npe_cmd_issue_write(struct ixpnpe_softc *sc,
    941     uint32_t cmd, uint32_t addr, uint32_t data)
    942 {
    943     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, data);
    944     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXAD, addr);
    945     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCTL, cmd);
    946 }
    947 
    948 static uint32_t
    949 npe_cmd_issue_read(struct ixpnpe_softc *sc, uint32_t cmd, uint32_t addr)
    950 {
    951     uint32_t data;
    952     int i;
    953 
    954     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXAD, addr);
    955     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCTL, cmd);
    956     for (i = 0; i <= IX_NPEDL_DELAY_READ_CYCLES; i++)
    957 	data = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXDATA);
    958     return data;
    959 }
    960 
    961 static int
    962 npe_ins_write(struct ixpnpe_softc *sc, uint32_t addr, uint32_t data, int verify)
    963 {
    964     DPRINTFn(4, sc->sc_dev, "%s(0x%x, 0x%x)\n", __func__, addr, data);
    965     npe_cmd_issue_write(sc, IX_NPEDL_EXCTL_CMD_WR_INS_MEM, addr, data);
    966     if (verify) {
    967 	uint32_t rdata;
    968 
    969         /*
    970 	 * Write invalid data to this reg, so we can see if we're reading
    971 	 * the EXDATA register too early.
    972 	 */
    973 	npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, ~data);
    974 
    975         /* Disabled since top 3 MSB are not used for Azusa hardware Refer WR:IXA00053900*/
    976         data &= IX_NPEDL_MASK_UNUSED_IMEM_BITS;
    977 
    978         rdata = npe_cmd_issue_read(sc, IX_NPEDL_EXCTL_CMD_RD_INS_MEM, addr);
    979         rdata &= IX_NPEDL_MASK_UNUSED_IMEM_BITS;
    980 
    981 	if (data != rdata)
    982 	    return EIO;
    983     }
    984     return 0;
    985 }
    986 
    987 static int
    988 npe_data_write(struct ixpnpe_softc *sc, uint32_t addr, uint32_t data, int verify)
    989 {
    990     DPRINTFn(4, sc->sc_dev, "%s(0x%x, 0x%x)\n", __func__, addr, data);
    991     npe_cmd_issue_write(sc, IX_NPEDL_EXCTL_CMD_WR_DATA_MEM, addr, data);
    992     if (verify) {
    993         /*
    994 	 * Write invalid data to this reg, so we can see if we're reading
    995 	 * the EXDATA register too early.
    996 	 */
    997 	npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, ~data);
    998 	if (data != npe_cmd_issue_read(sc, IX_NPEDL_EXCTL_CMD_RD_DATA_MEM, addr))
    999 	    return EIO;
   1000     }
   1001     return 0;
   1002 }
   1003 
   1004 static void
   1005 npe_ecs_reg_write(struct ixpnpe_softc *sc, uint32_t reg, uint32_t data)
   1006 {
   1007     npe_cmd_issue_write(sc, IX_NPEDL_EXCTL_CMD_WR_ECS_REG, reg, data);
   1008 }
   1009 
   1010 static uint32_t
   1011 npe_ecs_reg_read(struct ixpnpe_softc *sc, uint32_t reg)
   1012 {
   1013     return npe_cmd_issue_read(sc, IX_NPEDL_EXCTL_CMD_RD_ECS_REG, reg);
   1014 }
   1015 
   1016 static void
   1017 npe_issue_cmd(struct ixpnpe_softc *sc, uint32_t command)
   1018 {
   1019     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCTL, command);
   1020 }
   1021 
   1022 static void
   1023 npe_cpu_step_save(struct ixpnpe_softc *sc)
   1024 {
   1025     /* turn off the halt bit by clearing Execution Count register. */
   1026     /* save reg contents 1st and restore later */
   1027     sc->savedExecCount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXCT);
   1028     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCT, 0);
   1029 
   1030     /* ensure that IF and IE are on (temporarily), so that we don't end up
   1031      * stepping forever */
   1032     sc->savedEcsDbgCtxtReg2 = npe_ecs_reg_read(sc, IX_NPEDL_ECS_DBG_CTXT_REG_2);
   1033 
   1034     npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_2,
   1035 	(sc->savedEcsDbgCtxtReg2 | IX_NPEDL_MASK_ECS_DBG_REG_2_IF |
   1036 	 IX_NPEDL_MASK_ECS_DBG_REG_2_IE));
   1037 }
   1038 
   1039 static int
   1040 npe_cpu_step(struct ixpnpe_softc *sc, uint32_t npeInstruction,
   1041     uint32_t ctxtNum, uint32_t ldur)
   1042 {
   1043 #define	IX_NPE_DL_MAX_NUM_OF_RETRIES	1000000
   1044     uint32_t ecsDbgRegVal;
   1045     uint32_t oldWatchcount, newWatchcount;
   1046     int tries;
   1047 
   1048     /* set the Active bit, and the LDUR, in the debug level */
   1049     ecsDbgRegVal = IX_NPEDL_MASK_ECS_REG_0_ACTIVE |
   1050 	(ldur << IX_NPEDL_OFFSET_ECS_REG_0_LDUR);
   1051 
   1052     npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, ecsDbgRegVal);
   1053 
   1054     /*
   1055      * Set CCTXT at ECS DEBUG L3 to specify in which context to execute the
   1056      * instruction, and set SELCTXT at ECS DEBUG Level to specify which context
   1057      * store to access.
   1058      * Debug ECS Level Reg 1 has form  0x000n000n, where n = context number
   1059      */
   1060     ecsDbgRegVal = (ctxtNum << IX_NPEDL_OFFSET_ECS_REG_1_CCTXT) |
   1061 	(ctxtNum << IX_NPEDL_OFFSET_ECS_REG_1_SELCTXT);
   1062 
   1063     npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_1, ecsDbgRegVal);
   1064 
   1065     /* clear the pipeline */
   1066     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
   1067 
   1068     /* load NPE instruction into the instruction register */
   1069     npe_ecs_reg_write(sc, IX_NPEDL_ECS_INSTRUCT_REG, npeInstruction);
   1070 
   1071     /* we need this value later to wait for completion of NPE execution step */
   1072     oldWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
   1073 
   1074     /* issue a Step One command via the Execution Control register */
   1075     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_STEP);
   1076 
   1077     /*
   1078      * Force the XScale to wait until the NPE has finished execution step
   1079      * NOTE that this delay will be very small, just long enough to allow a
   1080      * single NPE instruction to complete execution; if instruction execution
   1081      * is not completed before timeout retries, exit the while loop.
   1082      */
   1083     newWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
   1084     for (tries = 0; tries < IX_NPE_DL_MAX_NUM_OF_RETRIES &&
   1085         newWatchcount == oldWatchcount; tries++) {
   1086 	/* Watch Count register increments when NPE completes an instruction */
   1087 	newWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
   1088     }
   1089     return (tries < IX_NPE_DL_MAX_NUM_OF_RETRIES) ? 0 : EIO;
   1090 #undef IX_NPE_DL_MAX_NUM_OF_RETRIES
   1091 }
   1092 
   1093 static void
   1094 npe_cpu_step_restore(struct ixpnpe_softc *sc)
   1095 {
   1096     /* clear active bit in debug level */
   1097     npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, 0);
   1098 
   1099     /* clear the pipeline */
   1100     npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
   1101 
   1102     /* restore Execution Count register contents. */
   1103     npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCT, sc->savedExecCount);
   1104 
   1105     /* restore IF and IE bits to original values */
   1106     npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_2, sc->savedEcsDbgCtxtReg2);
   1107 }
   1108 
   1109 static int
   1110 npe_logical_reg_read(struct ixpnpe_softc *sc,
   1111     uint32_t regAddr, uint32_t regSize,
   1112     uint32_t ctxtNum, uint32_t *regVal)
   1113 {
   1114     uint32_t npeInstruction, mask;
   1115     int error;
   1116 
   1117     switch (regSize) {
   1118     case IX_NPEDL_REG_SIZE_BYTE:
   1119 	npeInstruction = IX_NPEDL_INSTR_RD_REG_BYTE;
   1120 	mask = 0xff;
   1121 	break;
   1122     case IX_NPEDL_REG_SIZE_SHORT:
   1123 	npeInstruction = IX_NPEDL_INSTR_RD_REG_SHORT;
   1124 	mask = 0xffff;
   1125 	break;
   1126     case IX_NPEDL_REG_SIZE_WORD:
   1127 	npeInstruction = IX_NPEDL_INSTR_RD_REG_WORD;
   1128 	mask = 0xffffffff;
   1129 	break;
   1130     default:
   1131 	return EINVAL;
   1132     }
   1133 
   1134     /* make regAddr be the SRC and DEST operands (e.g. movX d0, d0) */
   1135     npeInstruction |= (regAddr << IX_NPEDL_OFFSET_INSTR_SRC) |
   1136 	(regAddr << IX_NPEDL_OFFSET_INSTR_DEST);
   1137 
   1138     /* step execution of NPE intruction using Debug Executing Context stack */
   1139     error = npe_cpu_step(sc, npeInstruction, ctxtNum, IX_NPEDL_RD_INSTR_LDUR);
   1140     if (error != 0) {
   1141 	DPRINTF(sc->sc_dev, "%s(0x%x, %u, %u), cannot step, error %d\n",
   1142 	    __func__, regAddr, regSize, ctxtNum, error);
   1143 	return error;
   1144     }
   1145     /* read value of register from Execution Data register */
   1146     *regVal = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXDATA);
   1147 
   1148     /* align value from left to right */
   1149     *regVal = (*regVal >> (IX_NPEDL_REG_SIZE_WORD - regSize)) & mask;
   1150 
   1151     return 0;
   1152 }
   1153 
   1154 static int
   1155 npe_logical_reg_write(struct ixpnpe_softc *sc, uint32_t regAddr, uint32_t regVal,
   1156     uint32_t regSize, uint32_t ctxtNum, int verify)
   1157 {
   1158     int error;
   1159 
   1160     DPRINTFn(4, sc->sc_dev, "%s(0x%x, 0x%x, %u, %u)\n",
   1161 	__func__, regAddr, regVal, regSize, ctxtNum);
   1162     if (regSize == IX_NPEDL_REG_SIZE_WORD) {
   1163 	/* NPE register addressing is left-to-right: e.g. |d0|d1|d2|d3| */
   1164 	/* Write upper half-word (short) to |d0|d1| */
   1165 	error = npe_logical_reg_write(sc, regAddr,
   1166 		     regVal >> IX_NPEDL_REG_SIZE_SHORT,
   1167 		     IX_NPEDL_REG_SIZE_SHORT, ctxtNum, verify);
   1168 	if (error != 0)
   1169 	    return error;
   1170 
   1171 	/* Write lower half-word (short) to |d2|d3| */
   1172 	error = npe_logical_reg_write(sc,
   1173 		     regAddr + sizeof(uint16_t),
   1174 		     regVal & 0xffff,
   1175 		     IX_NPEDL_REG_SIZE_SHORT, ctxtNum, verify);
   1176     } else {
   1177 	uint32_t npeInstruction;
   1178 
   1179         switch (regSize) {
   1180 	case IX_NPEDL_REG_SIZE_BYTE:
   1181 	    npeInstruction = IX_NPEDL_INSTR_WR_REG_BYTE;
   1182 	    regVal &= 0xff;
   1183 	    break;
   1184 	case IX_NPEDL_REG_SIZE_SHORT:
   1185             npeInstruction = IX_NPEDL_INSTR_WR_REG_SHORT;
   1186 	    regVal &= 0xffff;
   1187 	    break;
   1188 	default:
   1189 	    return EINVAL;
   1190 	}
   1191 	/* fill dest operand field of  instruction with destination reg addr */
   1192 	npeInstruction |= (regAddr << IX_NPEDL_OFFSET_INSTR_DEST);
   1193 
   1194 	/* fill src operand field of instruction with least-sig 5 bits of val*/
   1195 	npeInstruction |= ((regVal & IX_NPEDL_MASK_IMMED_INSTR_SRC_DATA) <<
   1196 			   IX_NPEDL_OFFSET_INSTR_SRC);
   1197 
   1198 	/* fill coprocessor field of instruction with most-sig 11 bits of val*/
   1199 	npeInstruction |= ((regVal & IX_NPEDL_MASK_IMMED_INSTR_COPROC_DATA) <<
   1200 			   IX_NPEDL_DISPLACE_IMMED_INSTR_COPROC_DATA);
   1201 
   1202 	/* step execution of NPE intruction using Debug ECS */
   1203 	error = npe_cpu_step(sc, npeInstruction,
   1204 					  ctxtNum, IX_NPEDL_WR_INSTR_LDUR);
   1205     }
   1206     if (error != 0) {
   1207 	DPRINTF(sc->sc_dev, "%s(0x%x, 0x%x, %u, %u), error %u writing reg\n",
   1208 	    __func__, regAddr, regVal, regSize, ctxtNum, error);
   1209 	return error;
   1210     }
   1211     if (verify) {
   1212 	uint32_t retRegVal;
   1213 
   1214     	error = npe_logical_reg_read(sc, regAddr, regSize, ctxtNum, &retRegVal);
   1215         if (error == 0 && regVal != retRegVal)
   1216 	    error = EIO;	/* XXX ambiguous */
   1217     }
   1218     return error;
   1219 }
   1220 
   1221 /*
   1222  * There are 32 physical registers used in an NPE.  These are
   1223  * treated as 16 pairs of 32-bit registers.  To write one of the pair,
   1224  * write the pair number (0-16) to the REGMAP for Context 0.  Then write
   1225  * the value to register  0 or 4 in the regfile, depending on which
   1226  * register of the pair is to be written
   1227  */
   1228 static int
   1229 npe_physical_reg_write(struct ixpnpe_softc *sc,
   1230     uint32_t regAddr, uint32_t regValue, int verify)
   1231 {
   1232     int error;
   1233 
   1234     /*
   1235      * Set REGMAP for context 0 to (regAddr >> 1) to choose which pair (0-16)
   1236      * of physical registers to write .
   1237      */
   1238     error = npe_logical_reg_write(sc, IX_NPEDL_CTXT_REG_ADDR_REGMAP,
   1239 	       (regAddr >> IX_NPEDL_OFFSET_PHYS_REG_ADDR_REGMAP),
   1240 	       IX_NPEDL_REG_SIZE_SHORT, 0, verify);
   1241     if (error == 0) {
   1242 	/* regAddr = 0 or 4  */
   1243 	regAddr = (regAddr & IX_NPEDL_MASK_PHYS_REG_ADDR_LOGICAL_ADDR) *
   1244 	    sizeof(uint32_t);
   1245 	error = npe_logical_reg_write(sc, regAddr, regValue,
   1246 	    IX_NPEDL_REG_SIZE_WORD, 0, verify);
   1247     }
   1248     return error;
   1249 }
   1250 
   1251 static int
   1252 npe_ctx_reg_write(struct ixpnpe_softc *sc, uint32_t ctxtNum,
   1253     uint32_t ctxtReg, uint32_t ctxtRegVal, int verify)
   1254 {
   1255     DPRINTFn(4, sc->sc_dev, "%s(%u, %u, %u)\n",
   1256 	__func__, ctxtNum, ctxtReg, ctxtRegVal);
   1257     /*
   1258      * Context 0 has no STARTPC. Instead, this value is used to set
   1259      * NextPC for Background ECS, to set where NPE starts executing code
   1260      */
   1261     if (ctxtNum == 0 && ctxtReg == IX_NPEDL_CTXT_REG_STARTPC) {
   1262 	/* read BG_CTXT_REG_0, update NEXTPC bits, and write back to reg */
   1263 	uint32_t v = npe_ecs_reg_read(sc, IX_NPEDL_ECS_BG_CTXT_REG_0);
   1264 	v &= ~IX_NPEDL_MASK_ECS_REG_0_NEXTPC;
   1265 	v |= (ctxtRegVal << IX_NPEDL_OFFSET_ECS_REG_0_NEXTPC) &
   1266 	    IX_NPEDL_MASK_ECS_REG_0_NEXTPC;
   1267 
   1268 	npe_ecs_reg_write(sc, IX_NPEDL_ECS_BG_CTXT_REG_0, v);
   1269 	return 0;
   1270     } else {
   1271 	static const struct {
   1272 	    uint32_t regAddress;
   1273 	    uint32_t regSize;
   1274 	} regAccInfo[IX_NPEDL_CTXT_REG_MAX] = {
   1275 	    { IX_NPEDL_CTXT_REG_ADDR_STEVT,	IX_NPEDL_REG_SIZE_BYTE },
   1276 	    { IX_NPEDL_CTXT_REG_ADDR_STARTPC,	IX_NPEDL_REG_SIZE_SHORT },
   1277 	    { IX_NPEDL_CTXT_REG_ADDR_REGMAP,	IX_NPEDL_REG_SIZE_SHORT },
   1278 	    { IX_NPEDL_CTXT_REG_ADDR_CINDEX,	IX_NPEDL_REG_SIZE_BYTE }
   1279 	};
   1280 	return npe_logical_reg_write(sc, regAccInfo[ctxtReg].regAddress,
   1281 		ctxtRegVal, regAccInfo[ctxtReg].regSize, ctxtNum, verify);
   1282     }
   1283 }
   1284 
   1285 /*
   1286  * NPE Mailbox support.
   1287  */
   1288 #define	IX_NPEMH_MAXTRIES	100000
   1289 
   1290 static int
   1291 ixpnpe_ofifo_wait(struct ixpnpe_softc *sc)
   1292 {
   1293     int i;
   1294 
   1295     for (i = 0; i < IX_NPEMH_MAXTRIES; i++) {
   1296         if (npe_reg_read(sc, IX_NPESTAT) & IX_NPESTAT_OFNE)
   1297 	    return 1;
   1298 	DELAY(10);
   1299     }
   1300     printf("%s: %s: timeout, last status 0x%x\n", device_xname(sc->sc_dev),
   1301         __func__, npe_reg_read(sc, IX_NPESTAT));
   1302     return 0;
   1303 }
   1304 
   1305 static int
   1306 ixpnpe_intr(void *arg)
   1307 {
   1308     struct ixpnpe_softc *sc = arg;
   1309     uint32_t status;
   1310 
   1311     status = npe_reg_read(sc, IX_NPESTAT);
   1312     if ((status & IX_NPESTAT_OFINT) == 0) {
   1313 	/* NB: should not happen */
   1314 	printf("%s: %s: status 0x%x\n", device_xname(sc->sc_dev), __func__, status);
   1315 	/* XXX must silence interrupt? */
   1316 	return(1);
   1317     }
   1318     /*
   1319      * A message is waiting in the output FIFO, copy it so
   1320      * the interrupt will be silenced; then signal anyone
   1321      * waiting to collect the result.
   1322      */
   1323     sc->sc_msgwaiting = -1;		/* NB: error indicator */
   1324     if (ixpnpe_ofifo_wait(sc)) {
   1325 	sc->sc_msg[0] = npe_reg_read(sc, IX_NPEFIFO);
   1326 	if (ixpnpe_ofifo_wait(sc)) {
   1327 	    sc->sc_msg[1] = npe_reg_read(sc, IX_NPEFIFO);
   1328 	    sc->sc_msgwaiting = 1;	/* successful fetch */
   1329 	}
   1330     }
   1331     if (sc->sc_msg[0] == (NPE_MACRECOVERYSTART << NPE_MAC_MSGID_SHL)) {
   1332 	    int s;
   1333 
   1334 	    s = splnet();
   1335 	    delay(100); /* delay 100usec */
   1336 	    if (sc->macresetcbfunc != NULL)
   1337 		    sc->macresetcbfunc(sc->macresetcbarg);
   1338 	    splx(s);
   1339     }
   1340 
   1341 #if 0
   1342     /* XXX Too dangerous! see ixpnpe_recvmsg_locked() */
   1343     wakeup(sc);
   1344 #endif
   1345 
   1346     return (1);
   1347 }
   1348 
   1349 static int
   1350 ixpnpe_ififo_wait(struct ixpnpe_softc *sc)
   1351 {
   1352     int i;
   1353 
   1354     for (i = 0; i < IX_NPEMH_MAXTRIES; i++) {
   1355 	if (npe_reg_read(sc, IX_NPESTAT) & IX_NPESTAT_IFNF)
   1356 	    return 1;
   1357 	DELAY(10);
   1358     }
   1359     return 0;
   1360 }
   1361 
   1362 static int
   1363 ixpnpe_sendmsg_locked(struct ixpnpe_softc *sc, const uint32_t msg[2])
   1364 {
   1365     int error = 0;
   1366 
   1367     sc->sc_msgwaiting = 0;
   1368     if (ixpnpe_ififo_wait(sc)) {
   1369 	npe_reg_write(sc, IX_NPEFIFO, msg[0]);
   1370 	if (ixpnpe_ififo_wait(sc))
   1371 	    npe_reg_write(sc, IX_NPEFIFO, msg[1]);
   1372 	else
   1373 	    error = EIO;
   1374     } else
   1375 	error = EIO;
   1376 
   1377     if (error)
   1378 	printf("%s: input FIFO timeout, msg [0x%x,0x%x]\n",
   1379 	    device_xname(sc->sc_dev), msg[0], msg[1]);
   1380     return error;
   1381 }
   1382 
   1383 static int
   1384 ixpnpe_recvmsg_locked(struct ixpnpe_softc *sc, uint32_t msg[2])
   1385 {
   1386 
   1387 	if (!sc->sc_msgwaiting) {
   1388 		/* XXX interrupt context - cannot sleep */
   1389 		delay(1000);	/* wait 1ms (is it ok?)*/
   1390 	}
   1391 	memcpy(msg, sc->sc_msg, sizeof(sc->sc_msg));
   1392 	/* NB: sc_msgwaiting != 1 means the ack fetch failed */
   1393 	return sc->sc_msgwaiting != 1 ? EIO : 0;
   1394 }
   1395 
   1396 /*
   1397  * Send a msg to the NPE and wait for a reply.  We use the
   1398  * private mutex and sleep until an interrupt is received
   1399  * signalling the availability of data in the output FIFO
   1400  * so the caller cannot be holding a mutex.  May be better
   1401  * piggyback on the caller's mutex instead but that would
   1402  * make other locking confusing.
   1403  */
   1404 int
   1405 ixpnpe_sendandrecvmsg(struct ixpnpe_softc *sc,
   1406 	const uint32_t send[2], uint32_t recv[2])
   1407 {
   1408     int error;
   1409 
   1410     mutex_enter(&sc->sc_lock);
   1411     error = ixpnpe_sendmsg_locked(sc, send);
   1412     if (error == 0)
   1413 	error = ixpnpe_recvmsg_locked(sc, recv);
   1414     mutex_exit(&sc->sc_lock);
   1415 
   1416     return error;
   1417 }
   1418 
   1419 /* XXX temporary, not reliable */
   1420 
   1421 int
   1422 ixpnpe_sendmsg(struct ixpnpe_softc *sc, const uint32_t msg[2])
   1423 {
   1424     int error;
   1425 
   1426     mutex_enter(&sc->sc_lock);
   1427     error = ixpnpe_sendmsg_locked(sc, msg);
   1428     mutex_exit(&sc->sc_lock);
   1429 
   1430     return error;
   1431 }
   1432 
   1433 int
   1434 ixpnpe_recvmsg(struct ixpnpe_softc *sc, uint32_t msg[2])
   1435 {
   1436     int error;
   1437 
   1438     mutex_enter(&sc->sc_lock);
   1439     if (sc->sc_msgwaiting)
   1440 	memcpy(msg, sc->sc_msg, sizeof(sc->sc_msg));
   1441     /* NB: sc_msgwaiting != 1 means the ack fetch failed */
   1442     error = sc->sc_msgwaiting != 1 ? EIO : 0;
   1443     mutex_exit(&sc->sc_lock);
   1444 
   1445     return error;
   1446 }
   1447