2 * QEMU Freescale eTSEC Emulator
4 * Copyright (c) 2011-2013 AdaCore
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * This implementation doesn't include ring priority, TCP/IP Off-Load, QoS.
29 #include "qemu/osdep.h"
30 #include "hw/sysbus.h"
32 #include "hw/ptimer.h"
33 #include "hw/qdev-properties.h"
35 #include "registers.h"
36 #include "qapi/error.h"
38 #include "qemu/module.h"
40 /* #define HEX_DUMP */
41 /* #define DEBUG_REGISTER */
44 static const int debug_etsec
= 1;
46 static const int debug_etsec
;
49 #define DPRINTF(fmt, ...) do { \
51 qemu_log(fmt , ## __VA_ARGS__); \
55 /* call after any change to IEVENT or IMASK */
56 void etsec_update_irq(eTSEC
*etsec
)
58 uint32_t ievent
= etsec
->regs
[IEVENT
].value
;
59 uint32_t imask
= etsec
->regs
[IMASK
].value
;
60 uint32_t active
= ievent
& imask
;
62 int tx
= !!(active
& IEVENT_TX_MASK
);
63 int rx
= !!(active
& IEVENT_RX_MASK
);
64 int err
= !!(active
& IEVENT_ERR_MASK
);
66 DPRINTF("%s IRQ ievent=%"PRIx32
" imask=%"PRIx32
" %c%c%c",
67 __func__
, ievent
, imask
,
72 qemu_set_irq(etsec
->tx_irq
, tx
);
73 qemu_set_irq(etsec
->rx_irq
, rx
);
74 qemu_set_irq(etsec
->err_irq
, err
);
77 static uint64_t etsec_read(void *opaque
, hwaddr addr
, unsigned size
)
79 eTSEC
*etsec
= opaque
;
80 uint32_t reg_index
= addr
/ 4;
81 eTSEC_Register
*reg
= NULL
;
84 assert(reg_index
< ETSEC_REG_NUMBER
);
86 reg
= &etsec
->regs
[reg_index
];
89 switch (reg
->access
) {
102 DPRINTF("Read 0x%08x @ 0x" TARGET_FMT_plx
104 ret
, addr
, reg
->name
, reg
->desc
);
109 static void write_tstat(eTSEC
*etsec
,
116 for (i
= 0; i
< 8; i
++) {
117 /* Check THLTi flag in TSTAT */
118 if (value
& (1 << (31 - i
))) {
119 etsec_walk_tx_ring(etsec
, i
);
123 /* Write 1 to clear */
124 reg
->value
&= ~value
;
127 static void write_rstat(eTSEC
*etsec
,
134 for (i
= 0; i
< 8; i
++) {
135 /* Check QHLTi flag in RSTAT */
136 if (value
& (1 << (23 - i
)) && !(reg
->value
& (1 << (23 - i
)))) {
137 etsec_walk_rx_ring(etsec
, i
);
141 /* Write 1 to clear */
142 reg
->value
&= ~value
;
145 static void write_tbasex(eTSEC
*etsec
,
150 reg
->value
= value
& ~0x7;
152 /* Copy this value in the ring's TxBD pointer */
153 etsec
->regs
[TBPTR0
+ (reg_index
- TBASE0
)].value
= value
& ~0x7;
156 static void write_rbasex(eTSEC
*etsec
,
161 reg
->value
= value
& ~0x7;
163 /* Copy this value in the ring's RxBD pointer */
164 etsec
->regs
[RBPTR0
+ (reg_index
- RBASE0
)].value
= value
& ~0x7;
167 static void write_dmactrl(eTSEC
*etsec
,
174 if (value
& DMACTRL_GRS
) {
176 if (etsec
->rx_buffer_len
!= 0) {
177 /* Graceful receive stop delayed until end of frame */
179 /* Graceful receive stop now */
180 etsec
->regs
[IEVENT
].value
|= IEVENT_GRSC
;
181 etsec_update_irq(etsec
);
185 if (value
& DMACTRL_GTS
) {
187 if (etsec
->tx_buffer_len
!= 0) {
188 /* Graceful transmit stop delayed until end of frame */
190 /* Graceful transmit stop now */
191 etsec
->regs
[IEVENT
].value
|= IEVENT_GTSC
;
192 etsec_update_irq(etsec
);
196 if (!(value
& DMACTRL_WOP
)) {
198 ptimer_transaction_begin(etsec
->ptimer
);
199 ptimer_stop(etsec
->ptimer
);
200 ptimer_set_count(etsec
->ptimer
, 1);
201 ptimer_run(etsec
->ptimer
, 1);
202 ptimer_transaction_commit(etsec
->ptimer
);
206 static void etsec_write(void *opaque
,
211 eTSEC
*etsec
= opaque
;
212 uint32_t reg_index
= addr
/ 4;
213 eTSEC_Register
*reg
= NULL
;
214 uint32_t before
= 0x0;
216 assert(reg_index
< ETSEC_REG_NUMBER
);
218 reg
= &etsec
->regs
[reg_index
];
223 /* Write 1 to clear */
224 reg
->value
&= ~value
;
226 etsec_update_irq(etsec
);
232 etsec_update_irq(etsec
);
236 write_dmactrl(etsec
, reg
, reg_index
, value
);
240 write_tstat(etsec
, reg
, reg_index
, value
);
244 write_rstat(etsec
, reg
, reg_index
, value
);
247 case TBASE0
... TBASE7
:
248 write_tbasex(etsec
, reg
, reg_index
, value
);
251 case RBASE0
... RBASE7
:
252 write_rbasex(etsec
, reg
, reg_index
, value
);
255 case MIIMCFG
... MIIMIND
:
256 etsec_write_miim(etsec
, reg
, reg_index
, value
);
260 /* Default handling */
261 switch (reg
->access
) {
269 reg
->value
&= ~value
;
274 /* Read Only or Unknown register */
279 DPRINTF("Write 0x%08x @ 0x" TARGET_FMT_plx
280 " val:0x%08x->0x%08x : %s (%s)\n",
281 (unsigned int)value
, addr
, before
, reg
->value
,
282 reg
->name
, reg
->desc
);
285 static const MemoryRegionOps etsec_ops
= {
287 .write
= etsec_write
,
288 .endianness
= DEVICE_NATIVE_ENDIAN
,
290 .min_access_size
= 4,
291 .max_access_size
= 4,
295 static void etsec_timer_hit(void *opaque
)
297 eTSEC
*etsec
= opaque
;
299 ptimer_stop(etsec
->ptimer
);
301 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_WOP
)) {
303 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_GTS
)) {
304 etsec_walk_tx_ring(etsec
, 0);
306 ptimer_set_count(etsec
->ptimer
, 1);
307 ptimer_run(etsec
->ptimer
, 1);
311 static void etsec_reset(DeviceState
*d
)
313 eTSEC
*etsec
= ETSEC_COMMON(d
);
317 /* Default value for all registers */
318 for (i
= 0; i
< ETSEC_REG_NUMBER
; i
++) {
319 etsec
->regs
[i
].name
= "Reserved";
320 etsec
->regs
[i
].desc
= "";
321 etsec
->regs
[i
].access
= ACC_UNKNOWN
;
322 etsec
->regs
[i
].value
= 0x00000000;
325 /* Set-up known registers */
326 for (i
= 0; eTSEC_registers_def
[i
].name
!= NULL
; i
++) {
328 reg_index
= eTSEC_registers_def
[i
].offset
/ 4;
330 etsec
->regs
[reg_index
].name
= eTSEC_registers_def
[i
].name
;
331 etsec
->regs
[reg_index
].desc
= eTSEC_registers_def
[i
].desc
;
332 etsec
->regs
[reg_index
].access
= eTSEC_registers_def
[i
].access
;
333 etsec
->regs
[reg_index
].value
= eTSEC_registers_def
[i
].reset
;
336 etsec
->tx_buffer
= NULL
;
337 etsec
->tx_buffer_len
= 0;
338 etsec
->rx_buffer
= NULL
;
339 etsec
->rx_buffer_len
= 0;
342 MII_SR_EXTENDED_CAPS
| MII_SR_LINK_STATUS
| MII_SR_AUTONEG_CAPS
|
343 MII_SR_AUTONEG_COMPLETE
| MII_SR_PREAMBLE_SUPPRESS
|
344 MII_SR_EXTENDED_STATUS
| MII_SR_100T2_HD_CAPS
| MII_SR_100T2_FD_CAPS
|
345 MII_SR_10T_HD_CAPS
| MII_SR_10T_FD_CAPS
| MII_SR_100X_HD_CAPS
|
346 MII_SR_100X_FD_CAPS
| MII_SR_100T4_CAPS
;
348 etsec_update_irq(etsec
);
351 static ssize_t
etsec_receive(NetClientState
*nc
,
356 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
358 #if defined(HEX_DUMP)
359 fprintf(stderr
, "%s receive size:%zd\n", nc
->name
, size
);
360 qemu_hexdump((void *)buf
, stderr
, "", size
);
362 /* Flush is unnecessary as are already in receiving path */
363 etsec
->need_flush
= false;
364 ret
= etsec_rx_ring_write(etsec
, buf
, size
);
366 /* The packet will be queued, let's flush it when buffer is available
368 etsec
->need_flush
= true;
374 static void etsec_set_link_status(NetClientState
*nc
)
376 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
378 etsec_miim_link_status(etsec
, nc
);
381 static NetClientInfo net_etsec_info
= {
382 .type
= NET_CLIENT_DRIVER_NIC
,
383 .size
= sizeof(NICState
),
384 .receive
= etsec_receive
,
385 .link_status_changed
= etsec_set_link_status
,
388 static void etsec_realize(DeviceState
*dev
, Error
**errp
)
390 eTSEC
*etsec
= ETSEC_COMMON(dev
);
392 etsec
->nic
= qemu_new_nic(&net_etsec_info
, &etsec
->conf
,
393 object_get_typename(OBJECT(dev
)), dev
->id
, etsec
);
394 qemu_format_nic_info_str(qemu_get_queue(etsec
->nic
), etsec
->conf
.macaddr
.a
);
396 etsec
->ptimer
= ptimer_init(etsec_timer_hit
, etsec
, PTIMER_POLICY_DEFAULT
);
397 ptimer_transaction_begin(etsec
->ptimer
);
398 ptimer_set_freq(etsec
->ptimer
, 100);
399 ptimer_transaction_commit(etsec
->ptimer
);
402 static void etsec_instance_init(Object
*obj
)
404 eTSEC
*etsec
= ETSEC_COMMON(obj
);
405 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
407 memory_region_init_io(&etsec
->io_area
, OBJECT(etsec
), &etsec_ops
, etsec
,
409 sysbus_init_mmio(sbd
, &etsec
->io_area
);
411 sysbus_init_irq(sbd
, &etsec
->tx_irq
);
412 sysbus_init_irq(sbd
, &etsec
->rx_irq
);
413 sysbus_init_irq(sbd
, &etsec
->err_irq
);
416 static Property etsec_properties
[] = {
417 DEFINE_NIC_PROPERTIES(eTSEC
, conf
),
418 DEFINE_PROP_END_OF_LIST(),
421 static void etsec_class_init(ObjectClass
*klass
, void *data
)
423 DeviceClass
*dc
= DEVICE_CLASS(klass
);
425 dc
->realize
= etsec_realize
;
426 dc
->reset
= etsec_reset
;
427 device_class_set_props(dc
, etsec_properties
);
428 /* Supported by ppce500 machine */
429 dc
->user_creatable
= true;
432 static TypeInfo etsec_info
= {
434 .parent
= TYPE_SYS_BUS_DEVICE
,
435 .instance_size
= sizeof(eTSEC
),
436 .class_init
= etsec_class_init
,
437 .instance_init
= etsec_instance_init
,
440 static void etsec_register_types(void)
442 type_register_static(&etsec_info
);
445 type_init(etsec_register_types
)
447 DeviceState
*etsec_create(hwaddr base
,
456 dev
= qdev_new("eTSEC");
457 qdev_set_nic_properties(dev
, nd
);
458 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
460 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, tx_irq
);
461 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 1, rx_irq
);
462 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 2, err_irq
);
464 memory_region_add_subregion(mr
, base
,
465 SYS_BUS_DEVICE(dev
)->mmio
[0].memory
);