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"
37 #include "qemu/main-loop.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_stop(etsec
->ptimer
);
199 ptimer_set_count(etsec
->ptimer
, 1);
200 ptimer_run(etsec
->ptimer
, 1);
204 static void etsec_write(void *opaque
,
209 eTSEC
*etsec
= opaque
;
210 uint32_t reg_index
= addr
/ 4;
211 eTSEC_Register
*reg
= NULL
;
212 uint32_t before
= 0x0;
214 assert(reg_index
< ETSEC_REG_NUMBER
);
216 reg
= &etsec
->regs
[reg_index
];
221 /* Write 1 to clear */
222 reg
->value
&= ~value
;
224 etsec_update_irq(etsec
);
230 etsec_update_irq(etsec
);
234 write_dmactrl(etsec
, reg
, reg_index
, value
);
238 write_tstat(etsec
, reg
, reg_index
, value
);
242 write_rstat(etsec
, reg
, reg_index
, value
);
245 case TBASE0
... TBASE7
:
246 write_tbasex(etsec
, reg
, reg_index
, value
);
249 case RBASE0
... RBASE7
:
250 write_rbasex(etsec
, reg
, reg_index
, value
);
253 case MIIMCFG
... MIIMIND
:
254 etsec_write_miim(etsec
, reg
, reg_index
, value
);
258 /* Default handling */
259 switch (reg
->access
) {
267 reg
->value
&= ~value
;
272 /* Read Only or Unknown register */
277 DPRINTF("Write 0x%08x @ 0x" TARGET_FMT_plx
278 " val:0x%08x->0x%08x : %s (%s)\n",
279 (unsigned int)value
, addr
, before
, reg
->value
,
280 reg
->name
, reg
->desc
);
283 static const MemoryRegionOps etsec_ops
= {
285 .write
= etsec_write
,
286 .endianness
= DEVICE_NATIVE_ENDIAN
,
288 .min_access_size
= 4,
289 .max_access_size
= 4,
293 static void etsec_timer_hit(void *opaque
)
295 eTSEC
*etsec
= opaque
;
297 ptimer_stop(etsec
->ptimer
);
299 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_WOP
)) {
301 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_GTS
)) {
302 etsec_walk_tx_ring(etsec
, 0);
304 ptimer_set_count(etsec
->ptimer
, 1);
305 ptimer_run(etsec
->ptimer
, 1);
309 static void etsec_reset(DeviceState
*d
)
311 eTSEC
*etsec
= ETSEC_COMMON(d
);
315 /* Default value for all registers */
316 for (i
= 0; i
< ETSEC_REG_NUMBER
; i
++) {
317 etsec
->regs
[i
].name
= "Reserved";
318 etsec
->regs
[i
].desc
= "";
319 etsec
->regs
[i
].access
= ACC_UNKNOWN
;
320 etsec
->regs
[i
].value
= 0x00000000;
323 /* Set-up known registers */
324 for (i
= 0; eTSEC_registers_def
[i
].name
!= NULL
; i
++) {
326 reg_index
= eTSEC_registers_def
[i
].offset
/ 4;
328 etsec
->regs
[reg_index
].name
= eTSEC_registers_def
[i
].name
;
329 etsec
->regs
[reg_index
].desc
= eTSEC_registers_def
[i
].desc
;
330 etsec
->regs
[reg_index
].access
= eTSEC_registers_def
[i
].access
;
331 etsec
->regs
[reg_index
].value
= eTSEC_registers_def
[i
].reset
;
334 etsec
->tx_buffer
= NULL
;
335 etsec
->tx_buffer_len
= 0;
336 etsec
->rx_buffer
= NULL
;
337 etsec
->rx_buffer_len
= 0;
340 MII_SR_EXTENDED_CAPS
| MII_SR_LINK_STATUS
| MII_SR_AUTONEG_CAPS
|
341 MII_SR_AUTONEG_COMPLETE
| MII_SR_PREAMBLE_SUPPRESS
|
342 MII_SR_EXTENDED_STATUS
| MII_SR_100T2_HD_CAPS
| MII_SR_100T2_FD_CAPS
|
343 MII_SR_10T_HD_CAPS
| MII_SR_10T_FD_CAPS
| MII_SR_100X_HD_CAPS
|
344 MII_SR_100X_FD_CAPS
| MII_SR_100T4_CAPS
;
346 etsec_update_irq(etsec
);
349 static ssize_t
etsec_receive(NetClientState
*nc
,
354 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
356 #if defined(HEX_DUMP)
357 fprintf(stderr
, "%s receive size:%zd\n", nc
->name
, size
);
358 qemu_hexdump((void *)buf
, stderr
, "", size
);
360 /* Flush is unnecessary as are already in receiving path */
361 etsec
->need_flush
= false;
362 ret
= etsec_rx_ring_write(etsec
, buf
, size
);
364 /* The packet will be queued, let's flush it when buffer is available
366 etsec
->need_flush
= true;
372 static void etsec_set_link_status(NetClientState
*nc
)
374 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
376 etsec_miim_link_status(etsec
, nc
);
379 static NetClientInfo net_etsec_info
= {
380 .type
= NET_CLIENT_DRIVER_NIC
,
381 .size
= sizeof(NICState
),
382 .receive
= etsec_receive
,
383 .link_status_changed
= etsec_set_link_status
,
386 static void etsec_realize(DeviceState
*dev
, Error
**errp
)
388 eTSEC
*etsec
= ETSEC_COMMON(dev
);
390 etsec
->nic
= qemu_new_nic(&net_etsec_info
, &etsec
->conf
,
391 object_get_typename(OBJECT(dev
)), dev
->id
, etsec
);
392 qemu_format_nic_info_str(qemu_get_queue(etsec
->nic
), etsec
->conf
.macaddr
.a
);
395 etsec
->bh
= qemu_bh_new(etsec_timer_hit
, etsec
);
396 etsec
->ptimer
= ptimer_init(etsec
->bh
, PTIMER_POLICY_DEFAULT
);
397 ptimer_set_freq(etsec
->ptimer
, 100);
400 static void etsec_instance_init(Object
*obj
)
402 eTSEC
*etsec
= ETSEC_COMMON(obj
);
403 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
405 memory_region_init_io(&etsec
->io_area
, OBJECT(etsec
), &etsec_ops
, etsec
,
407 sysbus_init_mmio(sbd
, &etsec
->io_area
);
409 sysbus_init_irq(sbd
, &etsec
->tx_irq
);
410 sysbus_init_irq(sbd
, &etsec
->rx_irq
);
411 sysbus_init_irq(sbd
, &etsec
->err_irq
);
414 static Property etsec_properties
[] = {
415 DEFINE_NIC_PROPERTIES(eTSEC
, conf
),
416 DEFINE_PROP_END_OF_LIST(),
419 static void etsec_class_init(ObjectClass
*klass
, void *data
)
421 DeviceClass
*dc
= DEVICE_CLASS(klass
);
423 dc
->realize
= etsec_realize
;
424 dc
->reset
= etsec_reset
;
425 dc
->props
= etsec_properties
;
426 /* Supported by ppce500 machine */
427 dc
->user_creatable
= true;
430 static TypeInfo etsec_info
= {
432 .parent
= TYPE_SYS_BUS_DEVICE
,
433 .instance_size
= sizeof(eTSEC
),
434 .class_init
= etsec_class_init
,
435 .instance_init
= etsec_instance_init
,
438 static void etsec_register_types(void)
440 type_register_static(&etsec_info
);
443 type_init(etsec_register_types
)
445 DeviceState
*etsec_create(hwaddr base
,
454 dev
= qdev_create(NULL
, "eTSEC");
455 qdev_set_nic_properties(dev
, nd
);
456 qdev_init_nofail(dev
);
458 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, tx_irq
);
459 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 1, rx_irq
);
460 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 2, err_irq
);
462 memory_region_add_subregion(mr
, base
,
463 SYS_BUS_DEVICE(dev
)->mmio
[0].memory
);