Debian Codegraph @

common/dev_c2600_iofpga.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <termios.h>
12 #include <fcntl.h>
13 #include <pthread.h>
14
15 #include "ptask.h"
16 #include "cpu.h"
17 #include "vm.h"
18 #include "dynamips.h"
19 #include "memory.h"
20 #include "device.h"
21 #include "dev_vtty.h"
22 #include "nmc93cX6.h"
23 #include "dev_mpc860.h"
24 #include "dev_c2600.h"
25
26 /* Debugging flags */
27 #define DEBUG_UNKNOWN 1
28 #define DEBUG_ACCESS 0
29 #define DEBUG_NET_IRQ 0
30
31 /* Definitions for Mainboard EEPROM */
32 #define EEPROM_MB_DOUT 3
33 #define EEPROM_MB_DIN 2
34 #define EEPROM_MB_CLK 1
35 #define EEPROM_MB_CS 0
36
37 /* Definitions for Network Modules EEPROM */
38 #define EEPROM_NM_DOUT 7
39 #define EEPROM_NM_DIN 6
40 #define EEPROM_NM_CLK 2
41 #define EEPROM_NM_CS 4
42
43 /* Network IRQ distribution */
44 static u_int net_irq_dist[C2600_MAX_NM_BAYS] = {
45 4, /* reg 0x08, bits 4-5 */
46 0, /* reg 0x08, bits 0-3 */
47 };
48
49 /* IO FPGA structure */
50 struct c2600_iofpga_data {
51 vm_obj_t vm_obj;
52 struct vdevice dev;
53 c2600_t *router;
54
55 /* Network Interrupt status */
56 m_uint8_t net_irq_status;
57
58 /* Interrupt mask */
59 m_uint16_t intr_mask;
60
61 /* WIC SPI selection */
62 m_uint8_t wic_select;
63 };
64
65 /* Mainboard EEPROM definition */
66 static const struct nmc93cX6_eeprom_def eeprom_mb_def = {
67 EEPROM_MB_CLK, EEPROM_MB_CS,
68 EEPROM_MB_DIN, EEPROM_MB_DOUT,
69 };
70
71 /* Mainboard EEPROM */
72 static const struct nmc93cX6_group eeprom_mb_group = {
73 EEPROM_TYPE_NMC93C46, 1, 0,
74 EEPROM_DORD_NORMAL,
75 EEPROM_DOUT_HIGH,
76 EEPROM_DEBUG_DISABLED,
77 "Mainboard EEPROM",
78 { &eeprom_mb_def },
79 };
80
81 /* NM EEPROM definition */
82 static const struct nmc93cX6_eeprom_def eeprom_nm_def = {
83 EEPROM_NM_CLK, EEPROM_NM_CS,
84 EEPROM_NM_DIN, EEPROM_NM_DOUT,
85 };
86
87 /* NM EEPROM */
88 static const struct nmc93cX6_group eeprom_nm_group = {
89 EEPROM_TYPE_NMC93C46, 1, 0,
90 EEPROM_DORD_NORMAL,
91 EEPROM_DOUT_HIGH,
92 EEPROM_DEBUG_DISABLED,
93 "NM EEPROM",
94 { &eeprom_nm_def },
95 };
96
97 /* Update network interrupt status */
98 static inline void dev_c2600_iofpga_net_update_irq(struct c2600_iofpga_data *d)
99 {
100 if (d->net_irq_status) {
101 vm_set_irq(d->router->vm,C2600_NETIO_IRQ);
102 } else {
103 vm_clear_irq(d->router->vm,C2600_NETIO_IRQ);
104 }
105 }
106
107 /* Trigger a Network IRQ for the specified slot/port */
108 void dev_c2600_iofpga_net_set_irq(struct c2600_iofpga_data *d,
109 u_int slot,u_int port)
110 {
111 #if DEBUG_NET_IRQ
112 vm_log(d->router->vm,"IO_FPGA","setting NetIRQ for slot %u port %u\n",
113 slot,port);
114 #endif
115 d->net_irq_status |= 1 << (net_irq_dist[slot] + port);
116 dev_c2600_iofpga_net_update_irq(d);
117 }
118
119 /* Clear a Network IRQ for the specified slot/port */
120 void dev_c2600_iofpga_net_clear_irq(struct c2600_iofpga_data *d,
121 u_int slot,u_int port)
122 {
123 #if DEBUG_NET_IRQ
124 vm_log(d->router->vm,"IO_FPGA","clearing NetIRQ for slot %u port %u\n",
125 slot,port);
126 #endif
127 d->net_irq_status &= ~(1 << (net_irq_dist[slot] + port));
128 dev_c2600_iofpga_net_update_irq(d);
129 }
130
131 /* Callback for MPC860 SPI Transmit */
132 static void dev_c2600_mpc860_spi_tx_callback(struct mpc860_data *mpc_data,
133 u_char *buffer,u_int len,
134 void *user_arg)
135 {
136 struct c2600_iofpga_data *d = user_arg;
137 struct cisco_eeprom *eeprom;
138 u_char reply_buf[4];
139 u_int wic_port;
140 u_int eeprom_offset;
141
142 if (d->wic_select & 0x20)
143 wic_port = 0x10;
144 else if (d->wic_select & 0x08)
145 wic_port = 0x20;
146 else {
147 vm_error(d->router->vm,"unknown value for wic_select (0x%8.8x)\n",
148 d->wic_select);
149 wic_port = 0;
150 }
151
152 /* No WIC in slot or no EEPROM: fake an empty EEPROM */
153 if (!wic_port || !(eeprom = vm_slot_get_eeprom(d->router->vm,0,wic_port))) {
154 memset(reply_buf,0xFF,sizeof(reply_buf));
155 mpc860_spi_receive(mpc_data,reply_buf,sizeof(reply_buf));
156 return;
157 }
158
159 /* Read request: 0x03 offset 0x00 0x00 */
160 eeprom_offset = buffer[1];
161
162 reply_buf[0] = 0;
163 reply_buf[1] = 0;
164 cisco_eeprom_get_byte(eeprom,eeprom_offset,&reply_buf[2]);
165 cisco_eeprom_get_byte(eeprom,eeprom_offset+1,&reply_buf[3]);
166
167 mpc860_spi_receive(mpc_data,reply_buf,sizeof(reply_buf));
168 }
169
170 /*
171 * dev_c2600_iofpga_access()
172 */
173 static void *
174 dev_c2600_iofpga_access(cpu_gen_t *cpu,struct vdevice *dev,
175 m_uint32_t offset,u_int op_size,u_int op_type,
176 m_uint64_t *data)
177 {
178 struct c2600_iofpga_data *d = dev->priv_data;
179
180 if (op_type == MTS_READ)
181 *data = 0x0;
182
183 #if DEBUG_ACCESS
184 if (op_type == MTS_READ) {
185 cpu_log(cpu,"IO_FPGA","reading reg 0x%x at pc=0x%llx (size=%u)\n",
186 offset,cpu_get_pc(cpu),op_size);
187 } else {
188 cpu_log(cpu,"IO_FPGA",
189 "writing reg 0x%x at pc=0x%llx, data=0x%llx (size=%u)\n",
190 offset,cpu_get_pc(cpu),*data,op_size);
191 }
192 #endif
193
194 switch(offset) {
195 case 0x04:
196 if (op_type == MTS_READ)
197 *data = 0x00;
198 break;
199
200 /*
201 * Network Interrupt.
202 *
203 * Bit 0-3: slot 1.
204 * Bit 4: slot 0 (MB), port 0
205 * Bit 5: slot 0 (MB), port 1
206 * Other: AIM ? (error messages displayed)
207 */
208 case 0x08:
209 if (op_type == MTS_READ)
210 *data = d->net_irq_status;
211 break;
212
213 case 0x10:
214 if (op_type == MTS_READ)
215 *data = d->wic_select;
216 else {
217 d->wic_select = *data;
218 }
219 break;
220
221 case 0x14:
222 if (op_type == MTS_READ)
223 *data = 0; //0xFFFFFFFF;
224 break;
225
226 /*
227 * Flash Related: 0x1y
228 *
229 * Bit 1: card present in slot 0 / WIC 0.
230 * Bit 2: card present in slot 0 / WIC 1.
231 *
232 * Other bits unknown.
233 */
234 case 0x0c:
235 if (op_type == MTS_READ) {
236 *data = 0x10;
237
238 /* check WIC 0 */
239 if (vm_slot_check_eeprom(d->router->vm,0,0x10))
240 *data |= 0x02;
241
242 /* check WIC 1 */
243 if (vm_slot_check_eeprom(d->router->vm,0,0x20))
244 *data |= 0x04;
245 }
246 break;
247
248 /* NM EEPROM */
249 case 0x1c:
250 if (op_type == MTS_WRITE)
251 nmc93cX6_write(&d->router->nm_eeprom_group,(u_int)(*data));
252 else
253 *data = nmc93cX6_read(&d->router->nm_eeprom_group);
254 break;
255
256 #if DEBUG_UNKNOWN
257 default:
258 if (op_type == MTS_READ) {
259 cpu_log(cpu,"IO_FPGA",
260 "read from unknown addr 0x%x, pc=0x%llx (size=%u)\n",
261 offset,cpu_get_pc(cpu),op_size);
262 } else {
263 cpu_log(cpu,"IO_FPGA",
264 "write to unknown addr 0x%x, value=0x%llx, "
265 "pc=0x%llx (size=%u)\n",
266 offset,*data,cpu_get_pc(cpu),op_size);
267 }
268 #endif
269 }
270
271 return NULL;
272 }
273
274 /* Shutdown the IO FPGA device */
275 static void
276 dev_c2600_iofpga_shutdown(vm_instance_t *vm,struct c2600_iofpga_data *d)
277 {
278 if (d != NULL) {
279 /* Remove the device */
280 dev_remove(vm,&d->dev);
281
282 /* Free the structure itself */
283 free(d);
284 }
285 }
286
287 /*
288 * dev_c2600_iofpga_init()
289 */
290 int dev_c2600_iofpga_init(c2600_t *router,m_uint64_t paddr,m_uint32_t len)
291 {
292 vm_instance_t *vm = router->vm;
293 struct c2600_iofpga_data *d;
294
295 /* Allocate private data structure */
296 if (!(d = malloc(sizeof(*d)))) {
297 fprintf(stderr,"IO_FPGA: out of memory\n");
298 return(-1);
299 }
300
301 memset(d,0,sizeof(*d));
302 d->router = router;
303
304 vm_object_init(&d->vm_obj);
305 d->vm_obj.name = "io_fpga";
306 d->vm_obj.data = d;
307 d->vm_obj.shutdown = (vm_shutdown_t)dev_c2600_iofpga_shutdown;
308
309 /* Set device properties */
310 dev_init(&d->dev);
311 d->dev.name = "io_fpga";
312 d->dev.phys_addr = paddr;
313 d->dev.phys_len = len;
314 d->dev.priv_data = d;
315 d->dev.handler = dev_c2600_iofpga_access;
316
317 /* Initialize the MPC860 SPI TX callback to read mainboard WIC EEPROMs */
318 mpc860_spi_set_tx_callback(router->mpc_data,
319 dev_c2600_mpc860_spi_tx_callback,d);
320
321 /* Map this device to the VM */
322 vm_bind_device(router->vm,&d->dev);
323 vm_object_add(vm,&d->vm_obj);
324 return(0);
325 }
326
327 /* Initialize EEPROM groups */
328 void c2600_init_eeprom_groups(c2600_t *router)
329 {
330 /* Initialize Mainboard EEPROM */
331 router->mb_eeprom_group = eeprom_mb_group;
332 router->mb_eeprom_group.eeprom[0] = &router->mb_eeprom;
333 router->mb_eeprom.data = NULL;
334 router->mb_eeprom.len = 0;
335
336 /* EEPROM for NM slot 1 */
337 router->nm_eeprom_group = eeprom_nm_group;
338 }