Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
/*	$NetBSD: ixp425_npe.c,v 1.17 2023/06/17 11:57:49 rin Exp $	*/

/*-
 * Copyright (c) 2006 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 */

/*-
 * Copyright (c) 2001-2005, Intel Corporation.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
*/
#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 $");

/*
 * Intel XScale Network Processing Engine (NPE) support.
 *
 * Each NPE has an ixpnpeX device associated with it that is
 * attached at boot.  Depending on the microcode loaded into
 * an NPE there may be an Ethernet interface (npeX) or some
 * other network interface (e.g. for ATM).  This file has support
 * for loading microcode images and the associated NPE CPU
 * manipulations (start, stop, reset).
 *
 * The code here basically replaces the npeDl and npeMh classes
 * in the Intel Access Library (IAL).
 *
 * NB: Microcode images are loaded with firmware(9).  To
 *     include microcode in a static kernel include the
 *     ixpnpe_fw device.  Otherwise the firmware will be
 *     automatically loaded from the filesystem.
 */
#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

/* used to read download map from version in microcode image */
#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

/*
 * masks used to extract address info from State information context
 * register addresses as read from microcode image 
 */
#define IX_NPEDL_MASK_STATE_ADDR_CTXT_REG         0x0000000F
#define IX_NPEDL_MASK_STATE_ADDR_CTXT_NUM         0x000000F0

/* LSB offset of Context Number field in State-Info Context Address */
#define IX_NPEDL_OFFSET_STATE_ADDR_CTXT_NUM       4

/* size (in words) of single State Information entry (ctxt reg address|data) */
#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 {
    /* 1st entry in the download map (there may be more than one) */
    IxNpeDlNpeMgrDownloadMapEntry entry[1];
} IxNpeDlNpeMgrDownloadMap;

/* used to access an instruction or data block in a microcode image */
typedef struct {
    uint32_t npeMemAddress;
    uint32_t size;
    uint32_t data[1];
} IxNpeDlNpeMgrCodeBlock;

/* used to access each Context Reg entry state-information block */
typedef struct {
    uint32_t addressInfo;
    uint32_t value;
} IxNpeDlNpeMgrStateInfoCtxtRegEntry;

/* used to access a state-information block in a microcode image */
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);

    /* XXX: Check features to ensure this NPE is enabled */

    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;

	/* size of instruction memory */
	sc->insMemSize = IX_NPEDL_INS_MEMSIZE_WORDS_NPEB;
	/* size of data memory */
	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;

	/* size of instruction memory */
	sc->insMemSize = IX_NPEDL_INS_MEMSIZE_WORDS_NPEC;
	/* size of data memory */
	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));

    /*
     * Setup IRQ and handler for NPE message support.
     */
    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);
    /* enable output fifo interrupts (NB: must also set OFIFO Write Enable) */ 
    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);		/* stop NPE */
    if (error == 0)
	error = npe_cpu_reset(sc);	/* reset it */
    if (error == 0)
	sc->started = 0;		/* mark stopped */
    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;
}

/*
 * Indicates the start of an NPE Image, in new NPE Image Library format.
 * 2 consecutive occurrences indicates the end of the NPE Image Library
 */
#define NPE_IMAGE_MARKER 0xfeedf00d

/*
 * NPE Image Header definition, used in new NPE Image Library format
 */
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;
        }
        /* 2 consecutive NPE_IMAGE_MARKER's indicates end of library */
        if (image->id == NPE_IMAGE_MARKER) {
	    printf("%s: imageId 0x%08x not found in image library header\n",
	        device_xname(sc->sc_dev), imageId);
            /* reached end of library, image not found */
            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);
    /*
     * Checking if image being loaded is meant for device that is running.
     * Image is forward compatible. i.e Image built for IXP42X should run
     * on IXP46X but not vice versa.
     */
    if (devid > (ixFeatureCtrlDeviceRead() & IX_FEATURE_CTRL_DEVICE_TYPE_MASK))
	return EINVAL;
#endif
    error = ixpnpe_stopandreset(sc);		/* stop and reset the NPE */
    if (error != 0)
	return error;

    fw = (void *)_binary_IxNpeMicrocode_dat_start;

    /* Locate desired image in files w/ combined images */
    error = npe_findimage(sc, (void *)fw /*fw->data*/, imageId, &imageCodePtr, &imageSize);
    if (error != 0)
	goto done;

    /*
     * If download was successful, store image Id in list of
     * currently loaded images. If a critical error occurred
     * during download, record that the NPE has an invalid image
     */
    mutex_enter(&sc->sc_lock);
    error = npe_load_image(sc, imageCodePtr, 1 /*VERIFY*/);
    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;		/* NB: instruction/data count */
    if (npeMemAddress + blockSize > sc->insMemSize) {
	printf("%s: Block size too big for NPE memory\n", device_xname(sc->sc_dev));
	return EINVAL;	/* XXX */
    }
    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;		/* NB: instruction/data count */
    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);

    /* for each state-info context register entry in block */
    nentries = bp->size / IX_NPEDL_STATE_INFO_ENTRY_SIZE;
    error = 0;
    for (i = 0; i < nentries; i++) {
	/* each state-info entry is 2 words (address, value) in length */
	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;
	
	/* error-check Context Register No. and Context Number values  */
	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;
	}    
	/* NOTE that there is no STEVT register for Context 0 */
	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)) {		/* verify NPE is stopped */
	printf("%s: cannot load image, NPE not stopped\n", device_xname(sc->sc_dev));
	return EIO;
    }

    /*
     * Read Download Map, checking each block type and calling
     * appropriate function to perform download 
     */
    error = 0;
    downloadMap = (const IxNpeDlNpeMgrDownloadMap *) imageCodePtr;
    for (i = 0; !EOM(downloadMap->entry[i].eodmMarker); i++) {
	/* calculate pointer to block to be downloaded */
	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;		/* XXX */
	    break;
	}
	if (error != 0)
	    break;
    }
    return error;
#undef EOM
}

/* contains Reset values for Context Store Registers  */
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 }
};

/* contains Reset values for Context Store Registers  */
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; /* identifies Context Store reg (0-3) */
    uint32_t regAddr;
    uint32_t regVal;
    uint32_t resetNpeParity;
    uint32_t ixNpeConfigCtrlRegVal;
    int i, error = 0;
    
    /* pre-store the NPE Config Control Register Value */
    ixNpeConfigCtrlRegVal = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_CTL);
    ixNpeConfigCtrlRegVal |= 0x3F000000;

    /* disable the parity interrupt */
    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);

    /*
     * Clear the FIFOs.
     */
    while (npe_checkbits(sc,
	  IX_NPEDL_REG_OFFSET_WFIFO, IX_NPEDL_MASK_WFIFO_VALID)) {
	/* read from the Watch-point FIFO until empty */
	(void) npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WFIFO);
    }

    while (npe_checkbits(sc,
	  IX_NPEDL_REG_OFFSET_STAT, IX_NPEDL_MASK_STAT_OFNE)) {
	/* read from the outFIFO until empty */
	(void) npe_reg_read(sc, IX_NPEDL_REG_OFFSET_FIFO);
    }
    
    while (npe_checkbits(sc,
	  IX_NPEDL_REG_OFFSET_STAT, IX_NPEDL_MASK_STAT_IFNE)) {
	/*
	 * Step execution of the NPE instruction to read inFIFO using
	 * the Debug Executing Context stack.
	 */
	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;   
	}
    }
    
    /*
     * Reset the mailbox reg
     */
    /* ...from XScale side */
    npe_reg_write(sc, IX_NPEDL_REG_OFFSET_MBST, IX_NPEDL_REG_RESET_MBST);
    /* ...from NPE side */
    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;   
    }

    /* 
     * Reset the physical registers in the NPE register file:
     * Note: no need to save/restore REGMAP for Context 0 here
     * since all Context Store regs are reset in subsequent code.
     */
    for (regAddr = 0;
	 regAddr < IX_NPEDL_TOTAL_NUM_PHYS_REG && error == 0;
	 regAddr++) {
	/* for each physical register in the NPE reg file, write 0 : */
	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;		/* abort reset */
	}
    }

    /*
     * Reset the context store:
     */
    for (i = IX_NPEDL_CTXT_NUM_MIN; i <= IX_NPEDL_CTXT_NUM_MAX; i++) {	
	/* set each context's Context Store registers to reset values: */
	for (ctxtReg = 0; ctxtReg < IX_NPEDL_CTXT_REG_MAX; ctxtReg++) {
	    /* NOTE that there is no STEVT register for Context 0 */
	    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;	 /* abort reset */
		}
	    }
	}
    }

    npe_cpu_step_restore(sc);

    /* write Reset values to Execution Context Stack registers */
    for (i = 0; i < N(ixNpeDlEcsRegResetValues); i++)
	npe_ecs_reg_write(sc,
	    ixNpeDlEcsRegResetValues[i].regAddr,
	    ixNpeDlEcsRegResetValues[i].regResetVal);

    /* clear the profile counter */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_CLR_PROFILE_CNT);
    
    /* clear registers EXCT, AP0, AP1, AP2 and AP3 */
    for (regAddr = IX_NPEDL_REG_OFFSET_EXCT;
	 regAddr <= IX_NPEDL_REG_OFFSET_AP3;
	 regAddr += sizeof(uint32_t))
	npe_reg_write(sc, regAddr, 0);

    /* Reset the Watch-count register */
    npe_reg_write(sc, IX_NPEDL_REG_OFFSET_WC, 0);
    
    /*
     * WR IXA00055043 - Remove IMEM Parity Introduced by NPE Reset Operation
     */

    /*
     * Reset the NPE and its coprocessor - to reset internal
     * states and remove parity error.  Note this makes no
     * sense based on the documentation.  The feature control
     * register always reads back as 0 on the ixp425 and further
     * the bit definition of NPEA/NPEB is off by 1 according to
     * the Intel documentation--so we're blindly following the
     * Intel code w/o any real understanding.
     */
    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);

    /* un-fuse and un-reset the NPE & coprocessor */
    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);

    /*
     * Call NpeMgr function to stop the NPE again after the Feature Control
     * has unfused and Un-Reset the NPE and its associated Coprocessors.
     */
    error = npe_cpu_stop(sc);

    /* restore NPE configuration bus Control Register - Parity Settings  */
    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;

    /*
     * Ensure only Background Context Stack Level is Active by turning off
     * the Active bit in each of the other Executing Context Stack levels.
     */
    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);
    
    /* clear the pipeline */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);
    
    /* start NPE execution by issuing command through EXCTL register on NPE */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_START);

    /*
     * Check execution status of NPE to verify operation was successful.
     */
    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)
{
    /* stop NPE execution by issuing command through EXCTL register on NPE */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_STOP);

    /* verify that NPE Stop was successful */
    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

/*
 * Introduce extra read cycles after issuing read command to NPE
 * so that we read the register after the NPE has updated it
 * This is to overcome race condition between XScale and NPE
 */
#define IX_NPEDL_DELAY_READ_CYCLES        2
/*
 * To mask top three MSBs of 32bit word to download into NPE IMEM
 */
#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;

        /*
	 * Write invalid data to this reg, so we can see if we're reading 
	 * the EXDATA register too early.
	 */
	npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXDATA, ~data);

        /* Disabled since top 3 MSB are not used for Azusa hardware Refer WR:IXA00053900*/
        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) {
        /*
	 * Write invalid data to this reg, so we can see if we're reading 
	 * the EXDATA register too early.
	 */
	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)
{
    /* turn off the halt bit by clearing Execution Count register. */
    /* save reg contents 1st and restore later */
    sc->savedExecCount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXCT);
    npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCT, 0);

    /* ensure that IF and IE are on (temporarily), so that we don't end up
     * stepping forever */
    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;

    /* set the Active bit, and the LDUR, in the debug level */
    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);

    /*
     * Set CCTXT at ECS DEBUG L3 to specify in which context to execute the
     * instruction, and set SELCTXT at ECS DEBUG Level to specify which context
     * store to access.
     * Debug ECS Level Reg 1 has form  0x000n000n, where n = context number
     */
    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);

    /* clear the pipeline */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);

    /* load NPE instruction into the instruction register */
    npe_ecs_reg_write(sc, IX_NPEDL_ECS_INSTRUCT_REG, npeInstruction);

    /* we need this value later to wait for completion of NPE execution step */
    oldWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);

    /* issue a Step One command via the Execution Control register */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_STEP);

    /*
     * Force the XScale to wait until the NPE has finished execution step
     * NOTE that this delay will be very small, just long enough to allow a
     * single NPE instruction to complete execution; if instruction execution
     * is not completed before timeout retries, exit the while loop.
     */
    newWatchcount = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_WC);
    for (tries = 0; tries < IX_NPE_DL_MAX_NUM_OF_RETRIES &&
        newWatchcount == oldWatchcount; tries++) {
	/* Watch Count register increments when NPE completes an instruction */
	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)
{
    /* clear active bit in debug level */
    npe_ecs_reg_write(sc, IX_NPEDL_ECS_DBG_CTXT_REG_0, 0);

    /* clear the pipeline */
    npe_issue_cmd(sc, IX_NPEDL_EXCTL_CMD_NPE_CLR_PIPE);

    /* restore Execution Count register contents. */
    npe_reg_write(sc, IX_NPEDL_REG_OFFSET_EXCT, sc->savedExecCount);

    /* restore IF and IE bits to original values */
    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;
    }

    /* make regAddr be the SRC and DEST operands (e.g. movX d0, d0) */
    npeInstruction |= (regAddr << IX_NPEDL_OFFSET_INSTR_SRC) |
	(regAddr << IX_NPEDL_OFFSET_INSTR_DEST);

    /* step execution of NPE instruction using Debug Executing Context stack */
    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;
    }
    /* read value of register from Execution Data register */
    *regVal = npe_reg_read(sc, IX_NPEDL_REG_OFFSET_EXDATA);

    /* align value from left to right */
    *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) {
	/* NPE register addressing is left-to-right: e.g. |d0|d1|d2|d3| */
	/* Write upper half-word (short) to |d0|d1| */
	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;

	/* Write lower half-word (short) to |d2|d3| */
	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;
	}
	/* fill dest operand field of instruction with destination reg addr */
	npeInstruction |= (regAddr << IX_NPEDL_OFFSET_INSTR_DEST);

	/* fill src operand field of instruction with least-sig 5 bits of val*/
	npeInstruction |= ((regVal & IX_NPEDL_MASK_IMMED_INSTR_SRC_DATA) <<
			   IX_NPEDL_OFFSET_INSTR_SRC);

	/* fill coprocessor field of instruction with most-sig 11 bits of val*/
	npeInstruction |= ((regVal & IX_NPEDL_MASK_IMMED_INSTR_COPROC_DATA) <<
			   IX_NPEDL_DISPLACE_IMMED_INSTR_COPROC_DATA);

	/* step execution of NPE instruction using Debug ECS */
	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;	/* XXX ambiguous */
    }
    return error;
}

/*
 * There are 32 physical registers used in an NPE.  These are
 * treated as 16 pairs of 32-bit registers.  To write one of the pair,
 * write the pair number (0-16) to the REGMAP for Context 0.  Then write
 * the value to register  0 or 4 in the regfile, depending on which
 * register of the pair is to be written
 */
static int
npe_physical_reg_write(struct ixpnpe_softc *sc,
    uint32_t regAddr, uint32_t regValue, int verify)
{
    int error;

    /*
     * Set REGMAP for context 0 to (regAddr >> 1) to choose which pair (0-16)
     * of physical registers to write .
     */
    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 = 0 or 4  */
	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);
    /*
     * Context 0 has no STARTPC. Instead, this value is used to set
     * NextPC for Background ECS, to set where NPE starts executing code
     */
    if (ctxtNum == 0 && ctxtReg == IX_NPEDL_CTXT_REG_STARTPC) {
	/* read BG_CTXT_REG_0, update NEXTPC bits, and write back to reg */
	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);
    }
}

/*
 * NPE Mailbox support.
 */
#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) {
	/* NB: should not happen */
	printf("%s: %s: status 0x%x\n", device_xname(sc->sc_dev), __func__, status);
	/* XXX must silence interrupt? */
	return(1);
    }
    /*
     * A message is waiting in the output FIFO, copy it so
     * the interrupt will be silenced; then signal anyone
     * waiting to collect the result.
     */
    sc->sc_msgwaiting = -1;		/* NB: error indicator */
    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;	/* successful fetch */
	}
    }
    if (sc->sc_msg[0] == (NPE_MACRECOVERYSTART << NPE_MAC_MSGID_SHL)) {
	    int s;

	    s = splnet();
	    delay(100); /* delay 100usec */
	    if (sc->macresetcbfunc != NULL)
		    sc->macresetcbfunc(sc->macresetcbarg);
	    splx(s);
    }

#if 0
    /* XXX Too dangerous! see ixpnpe_recvmsg_locked() */
    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) {
		/* XXX interrupt context - cannot sleep */
		delay(1000);	/* wait 1ms (is it ok?)*/
	}
	memcpy(msg, sc->sc_msg, sizeof(sc->sc_msg));
	/* NB: sc_msgwaiting != 1 means the ack fetch failed */
	return sc->sc_msgwaiting != 1 ? EIO : 0;
}

/*
 * Send a msg to the NPE and wait for a reply.  We use the
 * private mutex and sleep until an interrupt is received
 * signalling the availability of data in the output FIFO
 * so the caller cannot be holding a mutex.  May be better
 * piggyback on the caller's mutex instead but that would
 * make other locking confusing.
 */
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;
}

/* XXX temporary, not reliable */

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));
    /* NB: sc_msgwaiting != 1 means the ack fetch failed */
    error = sc->sc_msgwaiting != 1 ? EIO : 0;
    mutex_exit(&sc->sc_lock);

    return error;
}