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 "sysemu/sysemu.h"
31 #include "hw/sysbus.h"
32 #include "hw/ptimer.h"
34 #include "registers.h"
36 #include "qemu/module.h"
38 /* #define HEX_DUMP */
39 /* #define DEBUG_REGISTER */
42 static const int debug_etsec
= 1;
44 static const int debug_etsec
;
47 #define DPRINTF(fmt, ...) do { \
49 qemu_log(fmt , ## __VA_ARGS__); \
53 /* call after any change to IEVENT or IMASK */
54 void etsec_update_irq(eTSEC
*etsec
)
56 uint32_t ievent
= etsec
->regs
[IEVENT
].value
;
57 uint32_t imask
= etsec
->regs
[IMASK
].value
;
58 uint32_t active
= ievent
& imask
;
60 int tx
= !!(active
& IEVENT_TX_MASK
);
61 int rx
= !!(active
& IEVENT_RX_MASK
);
62 int err
= !!(active
& IEVENT_ERR_MASK
);
64 DPRINTF("%s IRQ ievent=%"PRIx32
" imask=%"PRIx32
" %c%c%c",
65 __func__
, ievent
, imask
,
70 qemu_set_irq(etsec
->tx_irq
, tx
);
71 qemu_set_irq(etsec
->rx_irq
, rx
);
72 qemu_set_irq(etsec
->err_irq
, err
);
75 static uint64_t etsec_read(void *opaque
, hwaddr addr
, unsigned size
)
77 eTSEC
*etsec
= opaque
;
78 uint32_t reg_index
= addr
/ 4;
79 eTSEC_Register
*reg
= NULL
;
82 assert(reg_index
< ETSEC_REG_NUMBER
);
84 reg
= &etsec
->regs
[reg_index
];
87 switch (reg
->access
) {
100 DPRINTF("Read 0x%08x @ 0x" TARGET_FMT_plx
102 ret
, addr
, reg
->name
, reg
->desc
);
107 static void write_tstat(eTSEC
*etsec
,
114 for (i
= 0; i
< 8; i
++) {
115 /* Check THLTi flag in TSTAT */
116 if (value
& (1 << (31 - i
))) {
117 etsec_walk_tx_ring(etsec
, i
);
121 /* Write 1 to clear */
122 reg
->value
&= ~value
;
125 static void write_rstat(eTSEC
*etsec
,
132 for (i
= 0; i
< 8; i
++) {
133 /* Check QHLTi flag in RSTAT */
134 if (value
& (1 << (23 - i
)) && !(reg
->value
& (1 << (23 - i
)))) {
135 etsec_walk_rx_ring(etsec
, i
);
139 /* Write 1 to clear */
140 reg
->value
&= ~value
;
143 static void write_tbasex(eTSEC
*etsec
,
148 reg
->value
= value
& ~0x7;
150 /* Copy this value in the ring's TxBD pointer */
151 etsec
->regs
[TBPTR0
+ (reg_index
- TBASE0
)].value
= value
& ~0x7;
154 static void write_rbasex(eTSEC
*etsec
,
159 reg
->value
= value
& ~0x7;
161 /* Copy this value in the ring's RxBD pointer */
162 etsec
->regs
[RBPTR0
+ (reg_index
- RBASE0
)].value
= value
& ~0x7;
165 static void write_dmactrl(eTSEC
*etsec
,
172 if (value
& DMACTRL_GRS
) {
174 if (etsec
->rx_buffer_len
!= 0) {
175 /* Graceful receive stop delayed until end of frame */
177 /* Graceful receive stop now */
178 etsec
->regs
[IEVENT
].value
|= IEVENT_GRSC
;
179 etsec_update_irq(etsec
);
183 if (value
& DMACTRL_GTS
) {
185 if (etsec
->tx_buffer_len
!= 0) {
186 /* Graceful transmit stop delayed until end of frame */
188 /* Graceful transmit stop now */
189 etsec
->regs
[IEVENT
].value
|= IEVENT_GTSC
;
190 etsec_update_irq(etsec
);
194 if (!(value
& DMACTRL_WOP
)) {
196 ptimer_stop(etsec
->ptimer
);
197 ptimer_set_count(etsec
->ptimer
, 1);
198 ptimer_run(etsec
->ptimer
, 1);
202 static void etsec_write(void *opaque
,
207 eTSEC
*etsec
= opaque
;
208 uint32_t reg_index
= addr
/ 4;
209 eTSEC_Register
*reg
= NULL
;
210 uint32_t before
= 0x0;
212 assert(reg_index
< ETSEC_REG_NUMBER
);
214 reg
= &etsec
->regs
[reg_index
];
219 /* Write 1 to clear */
220 reg
->value
&= ~value
;
222 etsec_update_irq(etsec
);
228 etsec_update_irq(etsec
);
232 write_dmactrl(etsec
, reg
, reg_index
, value
);
236 write_tstat(etsec
, reg
, reg_index
, value
);
240 write_rstat(etsec
, reg
, reg_index
, value
);
243 case TBASE0
... TBASE7
:
244 write_tbasex(etsec
, reg
, reg_index
, value
);
247 case RBASE0
... RBASE7
:
248 write_rbasex(etsec
, reg
, reg_index
, value
);
251 case MIIMCFG
... MIIMIND
:
252 etsec_write_miim(etsec
, reg
, reg_index
, value
);
256 /* Default handling */
257 switch (reg
->access
) {
265 reg
->value
&= ~value
;
270 /* Read Only or Unknown register */
275 DPRINTF("Write 0x%08x @ 0x" TARGET_FMT_plx
276 " val:0x%08x->0x%08x : %s (%s)\n",
277 (unsigned int)value
, addr
, before
, reg
->value
,
278 reg
->name
, reg
->desc
);
281 static const MemoryRegionOps etsec_ops
= {
283 .write
= etsec_write
,
284 .endianness
= DEVICE_NATIVE_ENDIAN
,
286 .min_access_size
= 4,
287 .max_access_size
= 4,
291 static void etsec_timer_hit(void *opaque
)
293 eTSEC
*etsec
= opaque
;
295 ptimer_stop(etsec
->ptimer
);
297 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_WOP
)) {
299 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_GTS
)) {
300 etsec_walk_tx_ring(etsec
, 0);
302 ptimer_set_count(etsec
->ptimer
, 1);
303 ptimer_run(etsec
->ptimer
, 1);
307 static void etsec_reset(DeviceState
*d
)
309 eTSEC
*etsec
= ETSEC_COMMON(d
);
313 /* Default value for all registers */
314 for (i
= 0; i
< ETSEC_REG_NUMBER
; i
++) {
315 etsec
->regs
[i
].name
= "Reserved";
316 etsec
->regs
[i
].desc
= "";
317 etsec
->regs
[i
].access
= ACC_UNKNOWN
;
318 etsec
->regs
[i
].value
= 0x00000000;
321 /* Set-up known registers */
322 for (i
= 0; eTSEC_registers_def
[i
].name
!= NULL
; i
++) {
324 reg_index
= eTSEC_registers_def
[i
].offset
/ 4;
326 etsec
->regs
[reg_index
].name
= eTSEC_registers_def
[i
].name
;
327 etsec
->regs
[reg_index
].desc
= eTSEC_registers_def
[i
].desc
;
328 etsec
->regs
[reg_index
].access
= eTSEC_registers_def
[i
].access
;
329 etsec
->regs
[reg_index
].value
= eTSEC_registers_def
[i
].reset
;
332 etsec
->tx_buffer
= NULL
;
333 etsec
->tx_buffer_len
= 0;
334 etsec
->rx_buffer
= NULL
;
335 etsec
->rx_buffer_len
= 0;
338 MII_SR_EXTENDED_CAPS
| MII_SR_LINK_STATUS
| MII_SR_AUTONEG_CAPS
|
339 MII_SR_AUTONEG_COMPLETE
| MII_SR_PREAMBLE_SUPPRESS
|
340 MII_SR_EXTENDED_STATUS
| MII_SR_100T2_HD_CAPS
| MII_SR_100T2_FD_CAPS
|
341 MII_SR_10T_HD_CAPS
| MII_SR_10T_FD_CAPS
| MII_SR_100X_HD_CAPS
|
342 MII_SR_100X_FD_CAPS
| MII_SR_100T4_CAPS
;
344 etsec_update_irq(etsec
);
347 static ssize_t
etsec_receive(NetClientState
*nc
,
352 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
354 #if defined(HEX_DUMP)
355 fprintf(stderr
, "%s receive size:%zd\n", nc
->name
, size
);
356 qemu_hexdump((void *)buf
, stderr
, "", size
);
358 /* Flush is unnecessary as are already in receiving path */
359 etsec
->need_flush
= false;
360 ret
= etsec_rx_ring_write(etsec
, buf
, size
);
362 /* The packet will be queued, let's flush it when buffer is available
364 etsec
->need_flush
= true;
370 static void etsec_set_link_status(NetClientState
*nc
)
372 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
374 etsec_miim_link_status(etsec
, nc
);
377 static NetClientInfo net_etsec_info
= {
378 .type
= NET_CLIENT_DRIVER_NIC
,
379 .size
= sizeof(NICState
),
380 .receive
= etsec_receive
,
381 .link_status_changed
= etsec_set_link_status
,
384 static void etsec_realize(DeviceState
*dev
, Error
**errp
)
386 eTSEC
*etsec
= ETSEC_COMMON(dev
);
388 etsec
->nic
= qemu_new_nic(&net_etsec_info
, &etsec
->conf
,
389 object_get_typename(OBJECT(dev
)), dev
->id
, etsec
);
390 qemu_format_nic_info_str(qemu_get_queue(etsec
->nic
), etsec
->conf
.macaddr
.a
);
393 etsec
->bh
= qemu_bh_new(etsec_timer_hit
, etsec
);
394 etsec
->ptimer
= ptimer_init(etsec
->bh
, PTIMER_POLICY_DEFAULT
);
395 ptimer_set_freq(etsec
->ptimer
, 100);
398 static void etsec_instance_init(Object
*obj
)
400 eTSEC
*etsec
= ETSEC_COMMON(obj
);
401 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
403 memory_region_init_io(&etsec
->io_area
, OBJECT(etsec
), &etsec_ops
, etsec
,
405 sysbus_init_mmio(sbd
, &etsec
->io_area
);
407 sysbus_init_irq(sbd
, &etsec
->tx_irq
);
408 sysbus_init_irq(sbd
, &etsec
->rx_irq
);
409 sysbus_init_irq(sbd
, &etsec
->err_irq
);
412 static Property etsec_properties
[] = {
413 DEFINE_NIC_PROPERTIES(eTSEC
, conf
),
414 DEFINE_PROP_END_OF_LIST(),
417 static void etsec_class_init(ObjectClass
*klass
, void *data
)
419 DeviceClass
*dc
= DEVICE_CLASS(klass
);
421 dc
->realize
= etsec_realize
;
422 dc
->reset
= etsec_reset
;
423 dc
->props
= etsec_properties
;
424 /* Supported by ppce500 machine */
425 dc
->user_creatable
= true;
428 static TypeInfo etsec_info
= {
430 .parent
= TYPE_SYS_BUS_DEVICE
,
431 .instance_size
= sizeof(eTSEC
),
432 .class_init
= etsec_class_init
,
433 .instance_init
= etsec_instance_init
,
436 static void etsec_register_types(void)
438 type_register_static(&etsec_info
);
441 type_init(etsec_register_types
)
443 DeviceState
*etsec_create(hwaddr base
,
452 dev
= qdev_create(NULL
, "eTSEC");
453 qdev_set_nic_properties(dev
, nd
);
454 qdev_init_nofail(dev
);
456 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, tx_irq
);
457 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 1, rx_irq
);
458 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 2, err_irq
);
460 memory_region_add_subregion(mr
, base
,
461 SYS_BUS_DEVICE(dev
)->mmio
[0].memory
);