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 "sysemu/sysemu.h"
30 #include "hw/sysbus.h"
32 #include "hw/ptimer.h"
34 #include "registers.h"
36 /* #define HEX_DUMP */
37 /* #define DEBUG_REGISTER */
40 static const int debug_etsec
= 1;
42 static const int debug_etsec
;
45 #define DPRINTF(fmt, ...) do { \
47 qemu_log(fmt , ## __VA_ARGS__); \
51 static uint64_t etsec_read(void *opaque
, hwaddr addr
, unsigned size
)
53 eTSEC
*etsec
= opaque
;
54 uint32_t reg_index
= addr
/ 4;
55 eTSEC_Register
*reg
= NULL
;
58 assert(reg_index
< ETSEC_REG_NUMBER
);
60 reg
= &etsec
->regs
[reg_index
];
63 switch (reg
->access
) {
76 DPRINTF("Read 0x%08x @ 0x" TARGET_FMT_plx
78 ret
, addr
, reg
->name
, reg
->desc
);
83 static void write_tstat(eTSEC
*etsec
,
90 for (i
= 0; i
< 8; i
++) {
91 /* Check THLTi flag in TSTAT */
92 if (value
& (1 << (31 - i
))) {
93 etsec_walk_tx_ring(etsec
, i
);
97 /* Write 1 to clear */
101 static void write_rstat(eTSEC
*etsec
,
108 for (i
= 0; i
< 8; i
++) {
109 /* Check QHLTi flag in RSTAT */
110 if (value
& (1 << (23 - i
)) && !(reg
->value
& (1 << (23 - i
)))) {
111 etsec_walk_rx_ring(etsec
, i
);
115 /* Write 1 to clear */
116 reg
->value
&= ~value
;
119 static void write_tbasex(eTSEC
*etsec
,
124 reg
->value
= value
& ~0x7;
126 /* Copy this value in the ring's TxBD pointer */
127 etsec
->regs
[TBPTR0
+ (reg_index
- TBASE0
)].value
= value
& ~0x7;
130 static void write_rbasex(eTSEC
*etsec
,
135 reg
->value
= value
& ~0x7;
137 /* Copy this value in the ring's RxBD pointer */
138 etsec
->regs
[RBPTR0
+ (reg_index
- RBASE0
)].value
= value
& ~0x7;
141 static void write_ievent(eTSEC
*etsec
,
146 /* Write 1 to clear */
147 reg
->value
&= ~value
;
149 if (!(reg
->value
& (IEVENT_TXF
| IEVENT_TXF
))) {
150 qemu_irq_lower(etsec
->tx_irq
);
152 if (!(reg
->value
& (IEVENT_RXF
| IEVENT_RXF
))) {
153 qemu_irq_lower(etsec
->rx_irq
);
156 if (!(reg
->value
& (IEVENT_MAG
| IEVENT_GTSC
| IEVENT_GRSC
| IEVENT_TXC
|
157 IEVENT_RXC
| IEVENT_BABR
| IEVENT_BABT
| IEVENT_LC
|
158 IEVENT_CRL
| IEVENT_FGPI
| IEVENT_FIR
| IEVENT_FIQ
|
159 IEVENT_DPE
| IEVENT_PERR
| IEVENT_EBERR
| IEVENT_TXE
|
160 IEVENT_XFUN
| IEVENT_BSY
| IEVENT_MSRO
| IEVENT_MMRD
|
162 qemu_irq_lower(etsec
->err_irq
);
166 static void write_dmactrl(eTSEC
*etsec
,
173 if (value
& DMACTRL_GRS
) {
175 if (etsec
->rx_buffer_len
!= 0) {
176 /* Graceful receive stop delayed until end of frame */
178 /* Graceful receive stop now */
179 etsec
->regs
[IEVENT
].value
|= IEVENT_GRSC
;
180 if (etsec
->regs
[IMASK
].value
& IMASK_GRSCEN
) {
181 qemu_irq_raise(etsec
->err_irq
);
186 if (value
& DMACTRL_GTS
) {
188 if (etsec
->tx_buffer_len
!= 0) {
189 /* Graceful transmit stop delayed until end of frame */
191 /* Graceful transmit stop now */
192 etsec
->regs
[IEVENT
].value
|= IEVENT_GTSC
;
193 if (etsec
->regs
[IMASK
].value
& IMASK_GTSCEN
) {
194 qemu_irq_raise(etsec
->err_irq
);
199 if (!(value
& DMACTRL_WOP
)) {
201 ptimer_stop(etsec
->ptimer
);
202 ptimer_set_count(etsec
->ptimer
, 1);
203 ptimer_run(etsec
->ptimer
, 1);
207 static void etsec_write(void *opaque
,
212 eTSEC
*etsec
= opaque
;
213 uint32_t reg_index
= addr
/ 4;
214 eTSEC_Register
*reg
= NULL
;
215 uint32_t before
= 0x0;
217 assert(reg_index
< ETSEC_REG_NUMBER
);
219 reg
= &etsec
->regs
[reg_index
];
224 write_ievent(etsec
, reg
, reg_index
, value
);
228 write_dmactrl(etsec
, reg
, reg_index
, value
);
232 write_tstat(etsec
, reg
, reg_index
, value
);
236 write_rstat(etsec
, reg
, reg_index
, value
);
239 case TBASE0
... TBASE7
:
240 write_tbasex(etsec
, reg
, reg_index
, value
);
243 case RBASE0
... RBASE7
:
244 write_rbasex(etsec
, reg
, reg_index
, value
);
247 case MIIMCFG
... MIIMIND
:
248 etsec_write_miim(etsec
, reg
, reg_index
, value
);
252 /* Default handling */
253 switch (reg
->access
) {
261 reg
->value
&= ~value
;
266 /* Read Only or Unknown register */
271 DPRINTF("Write 0x%08x @ 0x" TARGET_FMT_plx
272 " val:0x%08x->0x%08x : %s (%s)\n",
273 (unsigned int)value
, addr
, before
, reg
->value
,
274 reg
->name
, reg
->desc
);
277 static const MemoryRegionOps etsec_ops
= {
279 .write
= etsec_write
,
280 .endianness
= DEVICE_NATIVE_ENDIAN
,
282 .min_access_size
= 4,
283 .max_access_size
= 4,
287 static void etsec_timer_hit(void *opaque
)
289 eTSEC
*etsec
= opaque
;
291 ptimer_stop(etsec
->ptimer
);
293 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_WOP
)) {
295 if (!(etsec
->regs
[DMACTRL
].value
& DMACTRL_GTS
)) {
296 etsec_walk_tx_ring(etsec
, 0);
298 ptimer_set_count(etsec
->ptimer
, 1);
299 ptimer_run(etsec
->ptimer
, 1);
303 static void etsec_reset(DeviceState
*d
)
305 eTSEC
*etsec
= ETSEC_COMMON(d
);
309 /* Default value for all registers */
310 for (i
= 0; i
< ETSEC_REG_NUMBER
; i
++) {
311 etsec
->regs
[i
].name
= "Reserved";
312 etsec
->regs
[i
].desc
= "";
313 etsec
->regs
[i
].access
= ACC_UNKNOWN
;
314 etsec
->regs
[i
].value
= 0x00000000;
317 /* Set-up known registers */
318 for (i
= 0; eTSEC_registers_def
[i
].name
!= NULL
; i
++) {
320 reg_index
= eTSEC_registers_def
[i
].offset
/ 4;
322 etsec
->regs
[reg_index
].name
= eTSEC_registers_def
[i
].name
;
323 etsec
->regs
[reg_index
].desc
= eTSEC_registers_def
[i
].desc
;
324 etsec
->regs
[reg_index
].access
= eTSEC_registers_def
[i
].access
;
325 etsec
->regs
[reg_index
].value
= eTSEC_registers_def
[i
].reset
;
328 etsec
->tx_buffer
= NULL
;
329 etsec
->tx_buffer_len
= 0;
330 etsec
->rx_buffer
= NULL
;
331 etsec
->rx_buffer_len
= 0;
334 MII_SR_EXTENDED_CAPS
| MII_SR_LINK_STATUS
| MII_SR_AUTONEG_CAPS
|
335 MII_SR_AUTONEG_COMPLETE
| MII_SR_PREAMBLE_SUPPRESS
|
336 MII_SR_EXTENDED_STATUS
| MII_SR_100T2_HD_CAPS
| MII_SR_100T2_FD_CAPS
|
337 MII_SR_10T_HD_CAPS
| MII_SR_10T_FD_CAPS
| MII_SR_100X_HD_CAPS
|
338 MII_SR_100X_FD_CAPS
| MII_SR_100T4_CAPS
;
341 static int etsec_can_receive(NetClientState
*nc
)
343 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
345 return etsec
->rx_buffer_len
== 0;
348 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:%d\n", etsec
->nic
->nc
.name
, size
);
356 qemu_hexdump(buf
, stderr
, "", size
);
358 etsec_rx_ring_write(etsec
, buf
, size
);
363 static void etsec_set_link_status(NetClientState
*nc
)
365 eTSEC
*etsec
= qemu_get_nic_opaque(nc
);
367 etsec_miim_link_status(etsec
, nc
);
370 static NetClientInfo net_etsec_info
= {
371 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
372 .size
= sizeof(NICState
),
373 .can_receive
= etsec_can_receive
,
374 .receive
= etsec_receive
,
375 .link_status_changed
= etsec_set_link_status
,
378 static void etsec_realize(DeviceState
*dev
, Error
**errp
)
380 eTSEC
*etsec
= ETSEC_COMMON(dev
);
382 etsec
->nic
= qemu_new_nic(&net_etsec_info
, &etsec
->conf
,
383 object_get_typename(OBJECT(dev
)), dev
->id
, etsec
);
384 qemu_format_nic_info_str(qemu_get_queue(etsec
->nic
), etsec
->conf
.macaddr
.a
);
387 etsec
->bh
= qemu_bh_new(etsec_timer_hit
, etsec
);
388 etsec
->ptimer
= ptimer_init(etsec
->bh
);
389 ptimer_set_freq(etsec
->ptimer
, 100);
392 static void etsec_instance_init(Object
*obj
)
394 eTSEC
*etsec
= ETSEC_COMMON(obj
);
395 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
397 memory_region_init_io(&etsec
->io_area
, OBJECT(etsec
), &etsec_ops
, etsec
,
399 sysbus_init_mmio(sbd
, &etsec
->io_area
);
401 sysbus_init_irq(sbd
, &etsec
->tx_irq
);
402 sysbus_init_irq(sbd
, &etsec
->rx_irq
);
403 sysbus_init_irq(sbd
, &etsec
->err_irq
);
406 static Property etsec_properties
[] = {
407 DEFINE_NIC_PROPERTIES(eTSEC
, conf
),
408 DEFINE_PROP_END_OF_LIST(),
411 static void etsec_class_init(ObjectClass
*klass
, void *data
)
413 DeviceClass
*dc
= DEVICE_CLASS(klass
);
415 dc
->realize
= etsec_realize
;
416 dc
->reset
= etsec_reset
;
417 dc
->props
= etsec_properties
;
420 static TypeInfo etsec_info
= {
422 .parent
= TYPE_SYS_BUS_DEVICE
,
423 .instance_size
= sizeof(eTSEC
),
424 .class_init
= etsec_class_init
,
425 .instance_init
= etsec_instance_init
,
428 static void etsec_register_types(void)
430 type_register_static(&etsec_info
);
433 type_init(etsec_register_types
)
435 DeviceState
*etsec_create(hwaddr base
,
444 dev
= qdev_create(NULL
, "eTSEC");
445 qdev_set_nic_properties(dev
, nd
);
446 qdev_init_nofail(dev
);
448 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, tx_irq
);
449 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 1, rx_irq
);
450 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 2, err_irq
);
452 memory_region_add_subregion(mr
, base
,
453 SYS_BUS_DEVICE(dev
)->mmio
[0].memory
);