| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005-2007 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Cisco c7200 Midplane FPGA. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "cpu.h" |
| 13 |
#include "vm.h" |
| 14 |
#include "dynamips.h" |
| 15 |
#include "memory.h" |
| 16 |
#include "device.h" |
| 17 |
#include "nmc93cX6.h" |
| 18 |
#include "dev_c7200.h" |
| 19 |
|
| 20 |
#define DEBUG_UNKNOWN 1 |
| 21 |
#define DEBUG_ACCESS 0 |
| 22 |
#define DEBUG_NET_IRQ 0 |
| 23 |
#define DEBUG_OIR 1 |
| 24 |
|
| 25 |
/* |
| 26 |
* Definitions for Port Adapter Status. |
| 27 |
*/ |
| 28 |
#define PCI_BAY_3V 0x00000002 /* 3.3V */ |
| 29 |
#define PCI_BAY_5V 0x00000004 /* 5V */ |
| 30 |
#define PCI_BAY_POWER_OK (PCI_BAY_3V | PCI_BAY_5V) |
| 31 |
|
| 32 |
/* Bay power */ |
| 33 |
struct bay_power { |
| 34 |
u_int reg; |
| 35 |
u_int offset; |
| 36 |
}; |
| 37 |
|
| 38 |
static struct bay_power bay_power_info[C7200_MAX_PA_BAYS] = { |
| 39 |
{ 0, 0 }, /* I/O card */ |
| 40 |
{ 0, 8 }, /* Slot 1 */ |
| 41 |
{ 0, 12 }, /* Slot 2 */ |
| 42 |
{ 0, 24 }, /* Slot 3 */ |
| 43 |
{ 0, 16 }, /* Slot 4 */ |
| 44 |
{ 0, 28 }, /* Slot 5 */ |
| 45 |
{ 0, 20 }, /* Slot 6 */ |
| 46 |
{ 1, 24 }, /* Slot 7 */ |
| 47 |
}; |
| 48 |
|
| 49 |
/* |
| 50 |
* Definitions for EEPROM access (slots 0,1,3,4) (0x60) |
| 51 |
*/ |
| 52 |
#define BAY0_EEPROM_SELECT_BIT 1 |
| 53 |
#define BAY0_EEPROM_CLOCK_BIT 3 |
| 54 |
#define BAY0_EEPROM_DIN_BIT 4 |
| 55 |
#define BAY0_EEPROM_DOUT_BIT 6 |
| 56 |
|
| 57 |
#define BAY1_EEPROM_SELECT_BIT 9 |
| 58 |
#define BAY1_EEPROM_CLOCK_BIT 11 |
| 59 |
#define BAY1_EEPROM_DIN_BIT 12 |
| 60 |
#define BAY1_EEPROM_DOUT_BIT 14 |
| 61 |
|
| 62 |
#define BAY3_EEPROM_SELECT_BIT 25 |
| 63 |
#define BAY3_EEPROM_CLOCK_BIT 27 |
| 64 |
#define BAY3_EEPROM_DIN_BIT 28 |
| 65 |
#define BAY3_EEPROM_DOUT_BIT 30 |
| 66 |
|
| 67 |
#define BAY4_EEPROM_SELECT_BIT 17 |
| 68 |
#define BAY4_EEPROM_CLOCK_BIT 19 |
| 69 |
#define BAY4_EEPROM_DIN_BIT 20 |
| 70 |
#define BAY4_EEPROM_DOUT_BIT 22 |
| 71 |
|
| 72 |
/* |
| 73 |
* Definitions for EEPROM access (slots 2,5,6) (0x68) |
| 74 |
*/ |
| 75 |
#define BAY2_EEPROM_SELECT_BIT 9 |
| 76 |
#define BAY2_EEPROM_CLOCK_BIT 11 |
| 77 |
#define BAY2_EEPROM_DIN_BIT 12 |
| 78 |
#define BAY2_EEPROM_DOUT_BIT 14 |
| 79 |
|
| 80 |
#define BAY5_EEPROM_SELECT_BIT 25 |
| 81 |
#define BAY5_EEPROM_CLOCK_BIT 27 |
| 82 |
#define BAY5_EEPROM_DIN_BIT 28 |
| 83 |
#define BAY5_EEPROM_DOUT_BIT 30 |
| 84 |
|
| 85 |
#define BAY6_EEPROM_SELECT_BIT 17 |
| 86 |
#define BAY6_EEPROM_CLOCK_BIT 19 |
| 87 |
#define BAY6_EEPROM_DIN_BIT 20 |
| 88 |
#define BAY6_EEPROM_DOUT_BIT 22 |
| 89 |
|
| 90 |
/* Definitions for EEPROM access (slot 7) (0x2e4, in iofpga) */ |
| 91 |
#define BAY7_EEPROM_SELECT_BIT 25 |
| 92 |
#define BAY7_EEPROM_CLOCK_BIT 27 |
| 93 |
#define BAY7_EEPROM_DIN_BIT 28 |
| 94 |
#define BAY7_EEPROM_DOUT_BIT 30 |
| 95 |
|
| 96 |
/* PA Bay EEPROM definitions */ |
| 97 |
static const struct nmc93cX6_eeprom_def eeprom_bay_def[C7200_MAX_PA_BAYS] = { |
| 98 |
/* Bay 0 */ |
| 99 |
{ BAY0_EEPROM_CLOCK_BIT , BAY0_EEPROM_SELECT_BIT, |
| 100 |
BAY0_EEPROM_DIN_BIT , BAY0_EEPROM_DOUT_BIT, |
| 101 |
}, |
| 102 |
|
| 103 |
/* Bay 1 */ |
| 104 |
{ BAY1_EEPROM_CLOCK_BIT , BAY1_EEPROM_SELECT_BIT, |
| 105 |
BAY1_EEPROM_DIN_BIT , BAY1_EEPROM_DOUT_BIT, |
| 106 |
}, |
| 107 |
|
| 108 |
/* Bay 2 */ |
| 109 |
{ BAY2_EEPROM_CLOCK_BIT , BAY2_EEPROM_SELECT_BIT, |
| 110 |
BAY2_EEPROM_DIN_BIT , BAY2_EEPROM_DOUT_BIT, |
| 111 |
}, |
| 112 |
|
| 113 |
/* Bay 3 */ |
| 114 |
{ BAY3_EEPROM_CLOCK_BIT , BAY3_EEPROM_SELECT_BIT, |
| 115 |
BAY3_EEPROM_DIN_BIT , BAY3_EEPROM_DOUT_BIT, |
| 116 |
}, |
| 117 |
|
| 118 |
/* Bay 4 */ |
| 119 |
{ BAY4_EEPROM_CLOCK_BIT , BAY4_EEPROM_SELECT_BIT, |
| 120 |
BAY4_EEPROM_DIN_BIT , BAY4_EEPROM_DOUT_BIT, |
| 121 |
}, |
| 122 |
|
| 123 |
/* Bay 5 */ |
| 124 |
{ BAY5_EEPROM_CLOCK_BIT , BAY5_EEPROM_SELECT_BIT, |
| 125 |
BAY5_EEPROM_DIN_BIT , BAY5_EEPROM_DOUT_BIT, |
| 126 |
}, |
| 127 |
|
| 128 |
/* Bay 6 */ |
| 129 |
{ BAY6_EEPROM_CLOCK_BIT , BAY6_EEPROM_SELECT_BIT, |
| 130 |
BAY6_EEPROM_DIN_BIT , BAY6_EEPROM_DOUT_BIT, |
| 131 |
}, |
| 132 |
|
| 133 |
/* Bay 7 */ |
| 134 |
{ BAY7_EEPROM_CLOCK_BIT , BAY7_EEPROM_SELECT_BIT, |
| 135 |
BAY7_EEPROM_DIN_BIT , BAY7_EEPROM_DOUT_BIT, |
| 136 |
}, |
| 137 |
}; |
| 138 |
|
| 139 |
/* EEPROM group #1 (Bays 0, 1, 3, 4) */ |
| 140 |
static const struct nmc93cX6_group eeprom_bays_g1 = { |
| 141 |
EEPROM_TYPE_NMC93C46, 4, 0, |
| 142 |
EEPROM_DORD_NORMAL, |
| 143 |
EEPROM_DOUT_HIGH, |
| 144 |
EEPROM_DEBUG_DISABLED, |
| 145 |
"PA Bays (Group #1) EEPROM", |
| 146 |
{ &eeprom_bay_def[0], &eeprom_bay_def[1], |
| 147 |
&eeprom_bay_def[3], &eeprom_bay_def[4], |
| 148 |
}, |
| 149 |
}; |
| 150 |
|
| 151 |
/* EEPROM group #2 (Bays 2, 5, 6) */ |
| 152 |
static const struct nmc93cX6_group eeprom_bays_g2 = { |
| 153 |
EEPROM_TYPE_NMC93C46, 3, 0, |
| 154 |
EEPROM_DORD_NORMAL, |
| 155 |
EEPROM_DOUT_HIGH, |
| 156 |
EEPROM_DEBUG_DISABLED, |
| 157 |
"PA Bays (Group #2) EEPROM", |
| 158 |
{ &eeprom_bay_def[2], &eeprom_bay_def[5], &eeprom_bay_def[6] }, |
| 159 |
}; |
| 160 |
|
| 161 |
/* EEPROM group #3 (Bay 7) */ |
| 162 |
static const struct nmc93cX6_group eeprom_bays_g3 = { |
| 163 |
EEPROM_TYPE_NMC93C46, 1, 0, |
| 164 |
EEPROM_DORD_NORMAL, |
| 165 |
EEPROM_DOUT_HIGH, |
| 166 |
EEPROM_DEBUG_DISABLED, |
| 167 |
"PA Bay (Group #3) EEPROM", |
| 168 |
{ &eeprom_bay_def[7] }, |
| 169 |
}; |
| 170 |
|
| 171 |
/* Midplane FPGA private data */ |
| 172 |
struct c7200_mpfpga_data { |
| 173 |
vm_obj_t vm_obj; |
| 174 |
struct vdevice dev; |
| 175 |
c7200_t *router; |
| 176 |
}; |
| 177 |
|
| 178 |
/* Returns TRUE if a slot has to be powered */ |
| 179 |
static int pa_get_power_status(c7200_t *router,u_int slot) |
| 180 |
{ |
| 181 |
if (slot == 0) |
| 182 |
return(TRUE); |
| 183 |
|
| 184 |
return(vm_slot_check_eeprom(router->vm,slot,0)); |
| 185 |
} |
| 186 |
|
| 187 |
/* Update Port Adapter Status */ |
| 188 |
static void pa_update_status_reg(struct c7200_mpfpga_data *d) |
| 189 |
{ |
| 190 |
c7200_t *router = d->router; |
| 191 |
struct bay_power *pw; |
| 192 |
u_int i; |
| 193 |
|
| 194 |
router->pa_status_reg[0] = router->pa_status_reg[1] = 0; |
| 195 |
|
| 196 |
for(i=0;i<C7200_MAX_PA_BAYS;i++) { |
| 197 |
if (pa_get_power_status(router,i)) { |
| 198 |
pw = &bay_power_info[i]; |
| 199 |
router->pa_status_reg[pw->reg] |= (PCI_BAY_POWER_OK << pw->offset); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* dev_mpfpga_access() |
| 206 |
*/ |
| 207 |
void *dev_c7200_mpfpga_access(cpu_gen_t *cpu,struct vdevice *dev, |
| 208 |
m_uint32_t offset,u_int op_size,u_int op_type, |
| 209 |
m_uint64_t *data) |
| 210 |
{ |
| 211 |
struct c7200_mpfpga_data *d = dev->priv_data; |
| 212 |
c7200_t *router = d->router; |
| 213 |
|
| 214 |
if (op_type == MTS_READ) |
| 215 |
*data = 0x0; |
| 216 |
|
| 217 |
/* Optimization: this is written regularly */ |
| 218 |
if (offset == 0x7b) |
| 219 |
return NULL; |
| 220 |
|
| 221 |
#if DEBUG_ACCESS |
| 222 |
if (op_type == MTS_READ) { |
| 223 |
cpu_log(cpu,"MP_FPGA","reading reg 0x%x at pc=0x%llx (size=%u)\n", |
| 224 |
offset,cpu_get_pc(cpu),op_size); |
| 225 |
} else { |
| 226 |
cpu_log(cpu,"MP_FPGA", |
| 227 |
"writing reg 0x%x at pc=0x%llx, data=0x%llx (size=%u)\n", |
| 228 |
offset,cpu_get_pc(cpu),*data,op_size); |
| 229 |
} |
| 230 |
#endif |
| 231 |
|
| 232 |
switch(offset) { |
| 233 |
/* Interrupt status for slots 0, 1, 3, 4 */ |
| 234 |
case 0x10: |
| 235 |
case 0x11: |
| 236 |
case 0x12: |
| 237 |
case 0x13: |
| 238 |
if (op_type == MTS_READ) { |
| 239 |
*data = router->net_irq_status[0]; |
| 240 |
|
| 241 |
/* if we have a card in slot 7, report status as for slot 0 */ |
| 242 |
if (vm_slot_active(router->vm,7,0)) { |
| 243 |
u_int offset; |
| 244 |
|
| 245 |
offset = dev_c7200_net_get_reg_offset(0); |
| 246 |
|
| 247 |
if (router->net_irq_status[2]) { |
| 248 |
*data |= 0xFF << offset; |
| 249 |
} else { |
| 250 |
*data &= ~(0xFF << offset); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
break; |
| 255 |
|
| 256 |
/* Interrupt status for slots 2, 5, 6 */ |
| 257 |
case 0x18: |
| 258 |
case 0x19: |
| 259 |
case 0x1a: |
| 260 |
case 0x1b: |
| 261 |
if (op_type == MTS_READ) |
| 262 |
*data = router->net_irq_status[1]; |
| 263 |
break; |
| 264 |
|
| 265 |
/* Interrupt mask for slots 0, 1, 3, 4 */ |
| 266 |
case 0x20: |
| 267 |
if (op_type == MTS_READ) { |
| 268 |
*data = router->net_irq_mask[0]; |
| 269 |
} else { |
| 270 |
router->net_irq_mask[0] = *data; |
| 271 |
dev_c7200_net_update_irq(router); |
| 272 |
} |
| 273 |
break; |
| 274 |
|
| 275 |
/* Interrupt mask for slots 2, 5, 6 */ |
| 276 |
case 0x28: |
| 277 |
if (op_type == MTS_READ) { |
| 278 |
*data = router->net_irq_mask[1]; |
| 279 |
} else { |
| 280 |
router->net_irq_mask[1] = *data; |
| 281 |
dev_c7200_net_update_irq(router); |
| 282 |
} |
| 283 |
break; |
| 284 |
|
| 285 |
/* |
| 286 |
* - PCI errors (seen with IRQ 6) |
| 287 |
* - Used when PA Mgmt IRQ is triggered. |
| 288 |
* |
| 289 |
* If the PA Mgmt IRQ is triggered for an undefined slot, a crash |
| 290 |
* occurs with "Error: Unexpected NM Interrupt received from slot: 6" |
| 291 |
* So, we use the PA status reg as mask to return something safe |
| 292 |
* (slot order is identical). |
| 293 |
*/ |
| 294 |
case 0x40: |
| 295 |
if (op_type == MTS_READ) |
| 296 |
*data = 0x66666600 & router->pa_status_reg[0]; |
| 297 |
|
| 298 |
vm_clear_irq(router->vm,C7200_PA_MGMT_IRQ); |
| 299 |
break; |
| 300 |
|
| 301 |
case 0x48: /* ??? (test) */ |
| 302 |
if (op_type == MTS_READ) |
| 303 |
*data = 0xFFFFFFFF; |
| 304 |
break; |
| 305 |
|
| 306 |
/* |
| 307 |
* This corresponds to err_stat in error message when IRQ 6 is |
| 308 |
* triggered. |
| 309 |
* |
| 310 |
* Bit 7 => SRAM error. |
| 311 |
* Bits 1-6 => OIR on slot 1-6 |
| 312 |
*/ |
| 313 |
case 0x70: |
| 314 |
if (op_type == MTS_READ) { |
| 315 |
#if DEBUG_OIR |
| 316 |
cpu_log(cpu,"MP_FPGA","reading reg 0x%x at pc=0x%llx, val=0x%x\n", |
| 317 |
offset,cpu_get_pc(cpu),router->oir_status[0]); |
| 318 |
#endif |
| 319 |
*data = router->oir_status[0]; |
| 320 |
vm_clear_irq(router->vm,C7200_OIR_IRQ); |
| 321 |
} else { |
| 322 |
#if DEBUG_OIR |
| 323 |
cpu_log(cpu,"MP_FPGA","writing reg 0x%x at pc=0x%llx " |
| 324 |
"(data=0x%llx)\n",offset,cpu_get_pc(cpu),*data); |
| 325 |
#endif |
| 326 |
router->oir_status[0] &= ~(*data); |
| 327 |
vm_clear_irq(router->vm,C7200_OIR_IRQ); |
| 328 |
} |
| 329 |
break; |
| 330 |
|
| 331 |
/* |
| 332 |
* This corresponds to err_enable in error message when IRQ 6 is |
| 333 |
* triggered. No idea of what it really means. |
| 334 |
*/ |
| 335 |
case 0x78: |
| 336 |
if (op_type == MTS_READ) { |
| 337 |
#if DEBUG_OIR |
| 338 |
cpu_log(cpu,"MP_FPGA","reading 0x78 at pc=0x%llx\n", |
| 339 |
cpu_get_pc(cpu)); |
| 340 |
#endif |
| 341 |
*data = 0x00; |
| 342 |
} else { |
| 343 |
#if DEBUG_OIR |
| 344 |
cpu_log(cpu,"MP_FPGA","writing reg 0x78 at pc=0x%llx " |
| 345 |
"(data=0x%llx)\n",cpu_get_pc(cpu),*data); |
| 346 |
#endif |
| 347 |
} |
| 348 |
break; |
| 349 |
|
| 350 |
case 0x38: /* TDM status */ |
| 351 |
break; |
| 352 |
|
| 353 |
case 0x50: /* Port Adapter Status */ |
| 354 |
if (op_type == MTS_READ) { |
| 355 |
pa_update_status_reg(d); |
| 356 |
*data = router->pa_status_reg[0]; |
| 357 |
} |
| 358 |
break; |
| 359 |
|
| 360 |
case 0x58: /* Port Adapter Control */ |
| 361 |
if (op_type == MTS_WRITE) |
| 362 |
router->pa_ctrl_reg[0] = *data; |
| 363 |
else |
| 364 |
*data = router->pa_ctrl_reg[0]; |
| 365 |
break; |
| 366 |
|
| 367 |
case 0x60: /* EEPROM for PA in slots 0,1,3,4 */ |
| 368 |
if (op_type == MTS_WRITE) |
| 369 |
nmc93cX6_write(&router->pa_eeprom_g1,*data); |
| 370 |
else |
| 371 |
*data = nmc93cX6_read(&router->pa_eeprom_g1); |
| 372 |
break; |
| 373 |
|
| 374 |
case 0x68: /* EEPROM for PA in slots 2,5,6 */ |
| 375 |
if (op_type == MTS_WRITE) { |
| 376 |
nmc93cX6_write(&router->pa_eeprom_g2,*data); |
| 377 |
router->ps_status = *data & 0x03; |
| 378 |
|
| 379 |
if (router->ps_status == 0) { |
| 380 |
printf("\n\n\nVM %s: power shutdown - halting...\n\n\n", |
| 381 |
router->vm->name); |
| 382 |
vm_log(router->vm,"MP_FPGA","shutdown of power supplies\n"); |
| 383 |
vm_stop(router->vm); |
| 384 |
} |
| 385 |
} else { |
| 386 |
*data = nmc93cX6_read(&router->pa_eeprom_g2); |
| 387 |
*data |= router->ps_status & 0x03; |
| 388 |
} |
| 389 |
break; |
| 390 |
|
| 391 |
case 0x7b: /* ??? */ |
| 392 |
break; |
| 393 |
|
| 394 |
#if DEBUG_UNKNOWN |
| 395 |
default: |
| 396 |
if (op_type == MTS_READ) { |
| 397 |
cpu_log(cpu,"MP_FPGA","read from addr 0x%x, pc=0x%llx\n", |
| 398 |
offset,cpu_get_pc(cpu)); |
| 399 |
} else { |
| 400 |
cpu_log(cpu,"MP_FPGA","write to addr 0x%x, value=0x%llx, " |
| 401 |
"pc=0x%llx\n",offset,*data,cpu_get_pc(cpu)); |
| 402 |
} |
| 403 |
#endif |
| 404 |
} |
| 405 |
|
| 406 |
return NULL; |
| 407 |
} |
| 408 |
|
| 409 |
/* Initialize EEPROM groups */ |
| 410 |
void c7200_init_mp_eeprom_groups(c7200_t *router) |
| 411 |
{ |
| 412 |
/* Group 1: bays 0, 1, 3, 4 */ |
| 413 |
router->pa_eeprom_g1 = eeprom_bays_g1; |
| 414 |
router->pa_eeprom_g1.eeprom[0] = NULL; |
| 415 |
router->pa_eeprom_g1.eeprom[1] = NULL; |
| 416 |
router->pa_eeprom_g1.eeprom[2] = NULL; |
| 417 |
router->pa_eeprom_g1.eeprom[3] = NULL; |
| 418 |
|
| 419 |
/* Group 2: bays 2, 5, 6 */ |
| 420 |
router->pa_eeprom_g2 = eeprom_bays_g2; |
| 421 |
router->pa_eeprom_g2.eeprom[0] = NULL; |
| 422 |
router->pa_eeprom_g2.eeprom[1] = NULL; |
| 423 |
router->pa_eeprom_g2.eeprom[2] = NULL; |
| 424 |
|
| 425 |
/* Group 3: bay 7 */ |
| 426 |
router->pa_eeprom_g3 = eeprom_bays_g3; |
| 427 |
router->pa_eeprom_g3.eeprom[0] = NULL; |
| 428 |
} |
| 429 |
|
| 430 |
/* Shutdown the MP FPGA device */ |
| 431 |
static void |
| 432 |
dev_c7200_mpfpga_shutdown(vm_instance_t *vm,struct c7200_mpfpga_data *d) |
| 433 |
{ |
| 434 |
if (d != NULL) { |
| 435 |
/* Remove the device */ |
| 436 |
dev_remove(vm,&d->dev); |
| 437 |
|
| 438 |
/* Free the structure itself */ |
| 439 |
free(d); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/* Create the c7200 Midplane FPGA */ |
| 444 |
int dev_c7200_mpfpga_init(c7200_t *router,m_uint64_t paddr,m_uint32_t len) |
| 445 |
{ |
| 446 |
struct c7200_mpfpga_data *d; |
| 447 |
|
| 448 |
/* Allocate private data structure */ |
| 449 |
if (!(d = malloc(sizeof(*d)))) { |
| 450 |
fprintf(stderr,"MP_FPGA: out of memory\n"); |
| 451 |
return(-1); |
| 452 |
} |
| 453 |
|
| 454 |
memset(d,0,sizeof(*d)); |
| 455 |
d->router = router; |
| 456 |
router->ps_status = 0x03; |
| 457 |
|
| 458 |
vm_object_init(&d->vm_obj); |
| 459 |
d->vm_obj.name = "mp_fpga"; |
| 460 |
d->vm_obj.data = d; |
| 461 |
d->vm_obj.shutdown = (vm_shutdown_t)dev_c7200_mpfpga_shutdown; |
| 462 |
|
| 463 |
/* Set device properties */ |
| 464 |
dev_init(&d->dev); |
| 465 |
d->dev.name = "mp_fpga"; |
| 466 |
d->dev.phys_addr = paddr; |
| 467 |
d->dev.phys_len = len; |
| 468 |
d->dev.handler = dev_c7200_mpfpga_access; |
| 469 |
d->dev.priv_data = d; |
| 470 |
|
| 471 |
/* Map this device to the VM */ |
| 472 |
vm_bind_device(router->vm,&d->dev); |
| 473 |
vm_object_add(router->vm,&d->vm_obj); |
| 474 |
return(0); |
| 475 |
} |