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 const VMStateDescription vmstate_mipsnet
= {
208 .minimum_version_id
= 0,
209 .minimum_version_id_old
= 0,
210 .fields
= (VMStateField
[]) {
211 VMSTATE_UINT32(busy
, MIPSnetState
),
212 VMSTATE_UINT32(rx_count
, MIPSnetState
),
213 VMSTATE_UINT32(rx_read
, MIPSnetState
),
214 VMSTATE_UINT32(tx_count
, MIPSnetState
),
215 VMSTATE_UINT32(tx_written
, MIPSnetState
),
216 VMSTATE_UINT32(intctl
, MIPSnetState
),
217 VMSTATE_BUFFER(rx_buffer
, MIPSnetState
),
218 VMSTATE_BUFFER(tx_buffer
, MIPSnetState
),
219 VMSTATE_END_OF_LIST()
223 static void mipsnet_cleanup(VLANClientState
*nc
)
225 MIPSnetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
227 vmstate_unregister(NULL
, &vmstate_mipsnet
, s
);
229 isa_unassign_ioport(s
->io_base
, 36);
234 static NetClientInfo net_mipsnet_info
= {
235 .type
= NET_CLIENT_TYPE_NIC
,
236 .size
= sizeof(NICState
),
237 .can_receive
= mipsnet_can_receive
,
238 .receive
= mipsnet_receive
,
239 .cleanup
= mipsnet_cleanup
,
242 void mipsnet_init (int base
, qemu_irq irq
, NICInfo
*nd
)
246 qemu_check_nic_model(nd
, "mipsnet");
248 s
= qemu_mallocz(sizeof(MIPSnetState
));
250 register_ioport_write(base
, 36, 1, mipsnet_ioport_write
, s
);
251 register_ioport_read(base
, 36, 1, mipsnet_ioport_read
, s
);
252 register_ioport_write(base
, 36, 2, mipsnet_ioport_write
, s
);
253 register_ioport_read(base
, 36, 2, mipsnet_ioport_read
, s
);
254 register_ioport_write(base
, 36, 4, mipsnet_ioport_write
, s
);
255 register_ioport_read(base
, 36, 4, mipsnet_ioport_read
, s
);
261 s
->conf
.macaddr
= nd
->macaddr
;
262 s
->conf
.vlan
= nd
->vlan
;
263 s
->conf
.peer
= nd
->netdev
;
265 s
->nic
= qemu_new_nic(&net_mipsnet_info
, &s
->conf
,
266 nd
->model
, nd
->name
, s
);
268 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
272 vmstate_register(NULL
, 0, &vmstate_mipsnet
, s
);