6 //#define DEBUG_MIPSNET_SEND
7 //#define DEBUG_MIPSNET_RECEIVE
8 //#define DEBUG_MIPSNET_DATA
9 //#define DEBUG_MIPSNET_IRQ
11 /* MIPSnet register offsets */
13 #define MIPSNET_DEV_ID 0x00
14 #define MIPSNET_BUSY 0x08
15 #define MIPSNET_RX_DATA_COUNT 0x0c
16 #define MIPSNET_TX_DATA_COUNT 0x10
17 #define MIPSNET_INT_CTL 0x14
18 # define MIPSNET_INTCTL_TXDONE 0x00000001
19 # define MIPSNET_INTCTL_RXDONE 0x00000002
20 # define MIPSNET_INTCTL_TESTBIT 0x80000000
21 #define MIPSNET_INTERRUPT_INFO 0x18
22 #define MIPSNET_RX_DATA_BUFFER 0x1c
23 #define MIPSNET_TX_DATA_BUFFER 0x20
25 #define MAX_ETH_FRAME_SIZE 1514
27 typedef struct MIPSnetState
{
34 uint8_t rx_buffer
[MAX_ETH_FRAME_SIZE
];
35 uint8_t tx_buffer
[MAX_ETH_FRAME_SIZE
];
42 static void mipsnet_reset(MIPSnetState
*s
)
50 memset(s
->rx_buffer
, 0, MAX_ETH_FRAME_SIZE
);
51 memset(s
->tx_buffer
, 0, MAX_ETH_FRAME_SIZE
);
54 static void mipsnet_update_irq(MIPSnetState
*s
)
56 int isr
= !!s
->intctl
;
57 #ifdef DEBUG_MIPSNET_IRQ
58 printf("mipsnet: Set IRQ to %d (%02x)\n", isr
, s
->intctl
);
60 qemu_set_irq(s
->irq
, isr
);
63 static int mipsnet_buffer_full(MIPSnetState
*s
)
65 if (s
->rx_count
>= MAX_ETH_FRAME_SIZE
)
70 static int mipsnet_can_receive(VLANClientState
*nc
)
72 MIPSnetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
76 return !mipsnet_buffer_full(s
);
79 static ssize_t
mipsnet_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
81 MIPSnetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
83 #ifdef DEBUG_MIPSNET_RECEIVE
84 printf("mipsnet: receiving len=%zu\n", size
);
86 if (!mipsnet_can_receive(nc
))
91 /* Just accept everything. */
93 /* Write packet data. */
94 memcpy(s
->rx_buffer
, buf
, size
);
99 /* Now we can signal we have received something. */
100 s
->intctl
|= MIPSNET_INTCTL_RXDONE
;
101 mipsnet_update_irq(s
);
106 static uint32_t mipsnet_ioport_read(void *opaque
, uint32_t addr
)
108 MIPSnetState
*s
= opaque
;
114 ret
= be32_to_cpu(0x4d495053); /* MIPS */
116 case MIPSNET_DEV_ID
+ 4:
117 ret
= be32_to_cpu(0x4e455430); /* NET0 */
122 case MIPSNET_RX_DATA_COUNT
:
125 case MIPSNET_TX_DATA_COUNT
:
128 case MIPSNET_INT_CTL
:
130 s
->intctl
&= ~MIPSNET_INTCTL_TESTBIT
;
132 case MIPSNET_INTERRUPT_INFO
:
133 /* XXX: This seems to be a per-VPE interrupt number. */
136 case MIPSNET_RX_DATA_BUFFER
:
139 ret
= s
->rx_buffer
[s
->rx_read
++];
143 case MIPSNET_TX_DATA_BUFFER
:
147 #ifdef DEBUG_MIPSNET_DATA
148 printf("mipsnet: read addr=0x%02x val=0x%02x\n", addr
, ret
);
153 static void mipsnet_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
155 MIPSnetState
*s
= opaque
;
158 #ifdef DEBUG_MIPSNET_DATA
159 printf("mipsnet: write addr=0x%02x val=0x%02x\n", addr
, val
);
162 case MIPSNET_TX_DATA_COUNT
:
163 s
->tx_count
= (val
<= MAX_ETH_FRAME_SIZE
) ? val
: 0;
166 case MIPSNET_INT_CTL
:
167 if (val
& MIPSNET_INTCTL_TXDONE
) {
168 s
->intctl
&= ~MIPSNET_INTCTL_TXDONE
;
169 } else if (val
& MIPSNET_INTCTL_RXDONE
) {
170 s
->intctl
&= ~MIPSNET_INTCTL_RXDONE
;
171 } else if (val
& MIPSNET_INTCTL_TESTBIT
) {
173 s
->intctl
|= MIPSNET_INTCTL_TESTBIT
;
175 /* ACK testbit interrupt, flag was cleared on read. */
177 s
->busy
= !!s
->intctl
;
178 mipsnet_update_irq(s
);
180 case MIPSNET_TX_DATA_BUFFER
:
181 s
->tx_buffer
[s
->tx_written
++] = val
;
182 if (s
->tx_written
== s
->tx_count
) {
184 #ifdef DEBUG_MIPSNET_SEND
185 printf("mipsnet: sending len=%d\n", s
->tx_count
);
187 qemu_send_packet(&s
->nic
->nc
, s
->tx_buffer
, s
->tx_count
);
188 s
->tx_count
= s
->tx_written
= 0;
189 s
->intctl
|= MIPSNET_INTCTL_TXDONE
;
191 mipsnet_update_irq(s
);
194 /* Read-only registers */
197 case MIPSNET_RX_DATA_COUNT
:
198 case MIPSNET_INTERRUPT_INFO
:
199 case MIPSNET_RX_DATA_BUFFER
:
205 static void mipsnet_save(QEMUFile
*f
, void *opaque
)
207 MIPSnetState
*s
= opaque
;
209 qemu_put_be32s(f
, &s
->busy
);
210 qemu_put_be32s(f
, &s
->rx_count
);
211 qemu_put_be32s(f
, &s
->rx_read
);
212 qemu_put_be32s(f
, &s
->tx_count
);
213 qemu_put_be32s(f
, &s
->tx_written
);
214 qemu_put_be32s(f
, &s
->intctl
);
215 qemu_put_buffer(f
, s
->rx_buffer
, MAX_ETH_FRAME_SIZE
);
216 qemu_put_buffer(f
, s
->tx_buffer
, MAX_ETH_FRAME_SIZE
);
219 static int mipsnet_load(QEMUFile
*f
, void *opaque
, int version_id
)
221 MIPSnetState
*s
= opaque
;
226 qemu_get_be32s(f
, &s
->busy
);
227 qemu_get_be32s(f
, &s
->rx_count
);
228 qemu_get_be32s(f
, &s
->rx_read
);
229 qemu_get_be32s(f
, &s
->tx_count
);
230 qemu_get_be32s(f
, &s
->tx_written
);
231 qemu_get_be32s(f
, &s
->intctl
);
232 qemu_get_buffer(f
, s
->rx_buffer
, MAX_ETH_FRAME_SIZE
);
233 qemu_get_buffer(f
, s
->tx_buffer
, MAX_ETH_FRAME_SIZE
);
238 static void mipsnet_cleanup(VLANClientState
*nc
)
240 MIPSnetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
242 unregister_savevm(NULL
, "mipsnet", s
);
244 isa_unassign_ioport(s
->io_base
, 36);
249 static NetClientInfo net_mipsnet_info
= {
250 .type
= NET_CLIENT_TYPE_NIC
,
251 .size
= sizeof(NICState
),
252 .can_receive
= mipsnet_can_receive
,
253 .receive
= mipsnet_receive
,
254 .cleanup
= mipsnet_cleanup
,
257 void mipsnet_init (int base
, qemu_irq irq
, NICInfo
*nd
)
261 qemu_check_nic_model(nd
, "mipsnet");
263 s
= qemu_mallocz(sizeof(MIPSnetState
));
265 register_ioport_write(base
, 36, 1, mipsnet_ioport_write
, s
);
266 register_ioport_read(base
, 36, 1, mipsnet_ioport_read
, s
);
267 register_ioport_write(base
, 36, 2, mipsnet_ioport_write
, s
);
268 register_ioport_read(base
, 36, 2, mipsnet_ioport_read
, s
);
269 register_ioport_write(base
, 36, 4, mipsnet_ioport_write
, s
);
270 register_ioport_read(base
, 36, 4, mipsnet_ioport_read
, s
);
276 memcpy(s
->conf
.macaddr
.a
, nd
->macaddr
, sizeof(nd
->macaddr
));
277 s
->conf
.vlan
= nd
->vlan
;
278 s
->conf
.peer
= nd
->netdev
;
280 s
->nic
= qemu_new_nic(&net_mipsnet_info
, &s
->conf
,
281 nd
->model
, nd
->name
, s
);
283 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
287 register_savevm(NULL
, "mipsnet", 0, 0, mipsnet_save
, mipsnet_load
, s
);