Debian Codegraph @

common/dev_c1700_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_c1700.h"
25
26 /* Debugging flags */
27 #define DEBUG_UNKNOWN 1
28 #define DEBUG_ACCESS 0
29 #define DEBUG_WIC 0
30 #define DEBUG_NET_IRQ 0
31
32 /* Definitions for Mainboard EEPROM */
33 #define EEPROM_MB_DOUT 3
34 #define EEPROM_MB_DIN 2
35 #define EEPROM_MB_CLK 1
36 #define EEPROM_MB_CS 0
37
38 /* Network IRQ distribution */
39 static u_int net_irq_dist[C1700_MAX_NM_BAYS] = {
40 0, /* XXX: required/does exist ??? */
41 };
42
43 /* IO FPGA structure */
44 struct c1700_iofpga_data {
45 vm_obj_t vm_obj;
46 struct vdevice dev;
47 c1700_t *router;
48
49 /* Network Interrupt status */
50 m_uint8_t net_irq_status;
51
52 /* Interrupt mask */
53 m_uint16_t intr_mask;
54
55 /* WIC SPI selection */
56 m_uint8_t wic_select;
57 };
58
59 /* Mainboard EEPROM definition */
60 static const struct nmc93cX6_eeprom_def eeprom_mb_def = {
61 EEPROM_MB_CLK, EEPROM_MB_CS,
62 EEPROM_MB_DIN, EEPROM_MB_DOUT,
63 };
64
65 /* Mainboard EEPROM */
66 static const struct nmc93cX6_group eeprom_mb_group = {
67 EEPROM_TYPE_NMC93C46, 1, 0,
68 EEPROM_DORD_NORMAL,
69 EEPROM_DOUT_HIGH,
70 EEPROM_DEBUG_DISABLED,
71 "Mainboard EEPROM",
72 { &eeprom_mb_def },
73 };
74
75 /* Update network interrupt status */
76 static inline void dev_c1700_iofpga_net_update_irq(struct c1700_iofpga_data *d)
77 {
78 if (d->net_irq_status) {
79 vm_set_irq(d->router->vm,C1700_NETIO_IRQ);
80 } else {
81 vm_clear_irq(d->router->vm,C1700_NETIO_IRQ);
82 }
83 }
84
85 /* Trigger a Network IRQ for the specified slot/port */
86 void dev_c1700_iofpga_net_set_irq(struct c1700_iofpga_data *d,
87 u_int slot,u_int port)
88 {
89 #if DEBUG_NET_IRQ
90 vm_log(d->router->vm,"IO_FPGA","setting NetIRQ for slot %u port %u\n",
91 slot,port);
92 #endif
93 d->net_irq_status |= 1 << (net_irq_dist[slot] + port);
94 dev_c1700_iofpga_net_update_irq(d);
95 }
96
97 /* Clear a Network IRQ for the specified slot/port */
98 void dev_c1700_iofpga_net_clear_irq(struct c1700_iofpga_data *d,
99 u_int slot,u_int port)
100 {
101 #if DEBUG_NET_IRQ
102 vm_log(d->router->vm,"IO_FPGA","clearing NetIRQ for slot %u port %u\n",
103 slot,port);
104 #endif
105 d->net_irq_status &= ~(1 << (net_irq_dist[slot] + port));
106 dev_c1700_iofpga_net_update_irq(d);
107 }
108
109 /* Callback for MPC860 SPI Transmit */
110 static void dev_c1700_mpc860_spi_tx_callback(struct mpc860_data *mpc_data,
111 u_char *buffer,u_int len,
112 void *user_arg)
113 {
114 struct c1700_iofpga_data *d = user_arg;
115 struct cisco_eeprom *eeprom;
116 u_char reply_buf[4];
117 u_int wic_port;
118 u_int eeprom_offset;
119
120 if (d->wic_select & 0x20)
121 wic_port = 0x10;
122 else if (d->wic_select & 0x08)
123 wic_port = 0x20;
124 else {
125 #if DEBUG_WIC
126 vm_error(d->router->vm,"unknown value for wic_select (0x%8.8x)\n",
127 d->wic_select);
128 #endif
129 wic_port = 0;
130 }
131
132 /* No WIC in slot or no EEPROM: fake an empty EEPROM */
133 if (!wic_port || !(eeprom = vm_slot_get_eeprom(d->router->vm,0,wic_port))) {
134 memset(reply_buf,0xFF,sizeof(reply_buf));
135 mpc860_spi_receive(mpc_data,reply_buf,sizeof(reply_buf));
136 return;
137 }
138
139 /* Read request: 0x03 offset 0x00 0x00 */
140 eeprom_offset = buffer[1];
141
142 reply_buf[0] = 0;
143 reply_buf[1] = 0;
144 cisco_eeprom_get_byte(eeprom,eeprom_offset,&reply_buf[2]);
145 cisco_eeprom_get_byte(eeprom,eeprom_offset+1,&reply_buf[3]);
146
147 mpc860_spi_receive(mpc_data,reply_buf,sizeof(reply_buf));
148 }
149
150 /*
151 * dev_c1700_iofpga_access()
152 */
153 static void *
154 dev_c1700_iofpga_access(cpu_gen_t *cpu,struct vdevice *dev,
155 m_uint32_t offset,u_int op_size,u_int op_type,
156 m_uint64_t *data)
157 {
158 struct c1700_iofpga_data *d = dev->priv_data;
159
160 if (op_type == MTS_READ)
161 *data = 0x0;
162
163 #if DEBUG_ACCESS
164 if (op_type == MTS_READ) {
165 cpu_log(cpu,"IO_FPGA","reading reg 0x%x at pc=0x%llx (size=%u)\n",
166 offset,cpu_get_pc(cpu),op_size);
167 } else {
168 cpu_log(cpu,"IO_FPGA",
169 "writing reg 0x%x at pc=0x%llx, data=0x%llx (size=%u)\n",
170 offset,cpu_get_pc(cpu),*data,op_size);
171 }
172 #endif
173
174 switch(offset) {
175 /*
176 * Bits 0-2: motherboard model (change with caution, different log patterns)
177 * 0=MPC860T
178 * 1=MPC860T
179 * 2=MPC860
180 * 3=MPC860P
181 * 4=MPC862P
182 * 5=MPC855T
183 * 6=MPC860P
184 * 7=(see iofpga[5]&0xf)
185 * Bits 3-7: ???
186 */
187 case 0x04:
188 break;
189
190 /* (iopfga[4]&0x7 == 7)
191 * Bits 0-3: extended motherboard model
192 * 0-1=MPC860P
193 * 2-15=MPC860
194 * Bits 4-7: ??? related to clock speed?
195 case 0x05:
196 */
197
198 /* (iopfga[4]&0x7 == 7 and iofpga[5]&0xF == 1)
199 * Bits 0-7: ???
200 case 0x0A:
201 */
202
203 /*
204 * Bit 0: ???
205 * Bit 1: card present in slot 0 / WIC 0.
206 * Bit 2: card present in slot 0 / WIC 1.
207 * Bit 3: compression/VPN module ? (mention of "slot 3")
208 * Bits 4-7: ???
209 */
210 case 0x10:
211 if (op_type == MTS_READ) {
212 *data = 0;
213
214 /* check WIC 0 */
215 if (vm_slot_check_eeprom(d->router->vm,0,0x10))
216 *data |= 0x02;
217
218 /* check WIC 1 */
219 if (vm_slot_check_eeprom(d->router->vm,0,0x20))
220 *data |= 0x04;
221 }
222 break;
223
224 /*
225 * Bit 0: ??? NVRAM write protection?
226 * Bit 1: ???
227 * Bit 2: ??? related to syscalls?
228 * Bits 3-7: ???
229 case 0x14:
230 */
231
232 /* WIC card selection for EEPROM reading
233 * Bits 0-2: ???
234 * Bit 3: wic_port 0x10
235 * Bit 4: ???
236 * Bit 5: wic_port 0x20
237 * Bits 6-7: ???
238 */
239 case 0x18:
240 if (op_type == MTS_READ)
241 *data = d->wic_select;
242 else {
243 d->wic_select = *data;
244 }
245 break;
246
247 /*
248 * Bit 0-7: ???
249 case 0x20:
250 */
251
252 /* Unknown, read on 1760. Considering the activity pattern,
253 * it's probably used to update the fpga.
254 */
255 case 0x4c:
256 if (op_type == MTS_READ)
257 *data = 0xFF;
258 break;
259
260 #if DEBUG_UNKNOWN
261 default:
262 if (op_type == MTS_READ) {
263 cpu_log(cpu,"IO_FPGA",
264 "read from unknown addr 0x%x, pc=0x%llx (size=%u)\n",
265 offset,cpu_get_pc(cpu),op_size);
266 } else {
267 cpu_log(cpu,"IO_FPGA",
268 "write to unknown addr 0x%x, value=0x%llx, "
269 "pc=0x%llx (size=%u)\n",
270 offset,*data,cpu_get_pc(cpu),op_size);
271 }
272 #endif
273 }
274
275 return NULL;
276 }
277
278 /* Shutdown the IO FPGA device */
279 static void
280 dev_c1700_iofpga_shutdown(vm_instance_t *vm,struct c1700_iofpga_data *d)
281 {
282 if (d != NULL) {
283 /* Remove the device */
284 dev_remove(vm,&d->dev);
285
286 /* Free the structure itself */
287 free(d);
288 }
289 }
290
291 /*
292 * dev_c1700_iofpga_init()
293 */
294 int dev_c1700_iofpga_init(c1700_t *router,m_uint64_t paddr,m_uint32_t len)
295 {
296 vm_instance_t *vm = router->vm;
297 struct c1700_iofpga_data *d;
298
299 /* Allocate private data structure */
300 if (!(d = malloc(sizeof(*d)))) {
301 fprintf(stderr,"IO_FPGA: out of memory\n");
302 return(-1);
303 }
304
305 memset(d,0,sizeof(*d));
306 d->router = router;
307
308 vm_object_init(&d->vm_obj);
309 d->vm_obj.name = "io_fpga";
310 d->vm_obj.data = d;
311 d->vm_obj.shutdown = (vm_shutdown_t)dev_c1700_iofpga_shutdown;
312
313 /* Set device properties */
314 dev_init(&d->dev);
315 d->dev.name = "io_fpga";
316 d->dev.phys_addr = paddr;
317 d->dev.phys_len = len;
318 d->dev.priv_data = d;
319 d->dev.handler = dev_c1700_iofpga_access;
320
321 /* Initialize the MPC860 SPI TX callback to read mainboard WIC EEPROMs */
322 mpc860_spi_set_tx_callback(router->mpc_data,
323 dev_c1700_mpc860_spi_tx_callback,d);
324
325 /* Map this device to the VM */
326 vm_bind_device(router->vm,&d->dev);
327 vm_object_add(vm,&d->vm_obj);
328 return(0);
329 }
330
331 /* Initialize EEPROM groups */
332 void c1700_init_eeprom_groups(c1700_t *router)
333 {
334 /* Initialize Mainboard EEPROM */
335 router->mb_eeprom_group = eeprom_mb_group;
336 router->mb_eeprom_group.eeprom[0] = &router->mb_eeprom;
337 router->mb_eeprom.data = NULL;
338 router->mb_eeprom.len = 0;
339 }