1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "qemu/osdep.h"
15#include "hw/irq.h"
16#include "hw/net/ftgmac100.h"
17#include "sysemu/dma.h"
18#include "qapi/error.h"
19#include "qemu/log.h"
20#include "qemu/module.h"
21#include "net/checksum.h"
22#include "net/eth.h"
23#include "hw/net/mii.h"
24#include "hw/qdev-properties.h"
25#include "migration/vmstate.h"
26
27
28#include <zlib.h>
29
30
31
32
33#define FTGMAC100_ISR 0x00
34#define FTGMAC100_IER 0x04
35#define FTGMAC100_MAC_MADR 0x08
36#define FTGMAC100_MAC_LADR 0x0c
37#define FTGMAC100_MATH0 0x10
38#define FTGMAC100_MATH1 0x14
39#define FTGMAC100_NPTXPD 0x18
40#define FTGMAC100_RXPD 0x1C
41#define FTGMAC100_NPTXR_BADR 0x20
42#define FTGMAC100_RXR_BADR 0x24
43#define FTGMAC100_HPTXPD 0x28
44#define FTGMAC100_HPTXR_BADR 0x2c
45#define FTGMAC100_ITC 0x30
46#define FTGMAC100_APTC 0x34
47#define FTGMAC100_DBLAC 0x38
48#define FTGMAC100_REVR 0x40
49#define FTGMAC100_FEAR1 0x44
50#define FTGMAC100_RBSR 0x4c
51#define FTGMAC100_TPAFCR 0x48
52
53#define FTGMAC100_MACCR 0x50
54#define FTGMAC100_MACSR 0x54
55#define FTGMAC100_PHYCR 0x60
56#define FTGMAC100_PHYDATA 0x64
57#define FTGMAC100_FCR 0x68
58
59
60
61
62#define FTGMAC100_INT_RPKT_BUF (1 << 0)
63#define FTGMAC100_INT_RPKT_FIFO (1 << 1)
64#define FTGMAC100_INT_NO_RXBUF (1 << 2)
65#define FTGMAC100_INT_RPKT_LOST (1 << 3)
66#define FTGMAC100_INT_XPKT_ETH (1 << 4)
67#define FTGMAC100_INT_XPKT_FIFO (1 << 5)
68#define FTGMAC100_INT_NO_NPTXBUF (1 << 6)
69#define FTGMAC100_INT_XPKT_LOST (1 << 7)
70#define FTGMAC100_INT_AHB_ERR (1 << 8)
71#define FTGMAC100_INT_PHYSTS_CHG (1 << 9)
72#define FTGMAC100_INT_NO_HPTXBUF (1 << 10)
73
74
75
76
77#define FTGMAC100_APTC_RXPOLL_CNT(x) ((x) & 0xf)
78#define FTGMAC100_APTC_RXPOLL_TIME_SEL (1 << 4)
79#define FTGMAC100_APTC_TXPOLL_CNT(x) (((x) >> 8) & 0xf)
80#define FTGMAC100_APTC_TXPOLL_TIME_SEL (1 << 12)
81
82
83
84
85#define FTGMAC100_DBLAC_RXBURST_SIZE(x) (((x) >> 8) & 0x3)
86#define FTGMAC100_DBLAC_TXBURST_SIZE(x) (((x) >> 10) & 0x3)
87#define FTGMAC100_DBLAC_RXDES_SIZE(x) ((((x) >> 12) & 0xf) * 8)
88#define FTGMAC100_DBLAC_TXDES_SIZE(x) ((((x) >> 16) & 0xf) * 8)
89#define FTGMAC100_DBLAC_IFG_CNT(x) (((x) >> 20) & 0x7)
90#define FTGMAC100_DBLAC_IFG_INC (1 << 23)
91
92
93
94
95#define FTGMAC100_PHYCR_MIIRD (1 << 26)
96#define FTGMAC100_PHYCR_MIIWR (1 << 27)
97
98#define FTGMAC100_PHYCR_DEV(x) (((x) >> 16) & 0x1f)
99#define FTGMAC100_PHYCR_REG(x) (((x) >> 21) & 0x1f)
100
101
102
103
104#define FTGMAC100_PHYDATA_MIIWDATA(x) ((x) & 0xffff)
105#define FTGMAC100_PHYDATA_MIIRDATA(x) (((x) >> 16) & 0xffff)
106
107
108
109
110#define FTGMAC100_PHYCR_NEW_DATA(x) (((x) >> 16) & 0xffff)
111#define FTGMAC100_PHYCR_NEW_FIRE (1 << 15)
112#define FTGMAC100_PHYCR_NEW_ST_22 (1 << 12)
113#define FTGMAC100_PHYCR_NEW_OP(x) (((x) >> 10) & 3)
114#define FTGMAC100_PHYCR_NEW_OP_WRITE 0x1
115#define FTGMAC100_PHYCR_NEW_OP_READ 0x2
116#define FTGMAC100_PHYCR_NEW_DEV(x) (((x) >> 5) & 0x1f)
117#define FTGMAC100_PHYCR_NEW_REG(x) ((x) & 0x1f)
118
119
120
121
122#define FTGMAC100_REVR_NEW_MDIO_INTERFACE (1 << 31)
123
124
125
126
127#define FTGMAC100_MACCR_TXDMA_EN (1 << 0)
128#define FTGMAC100_MACCR_RXDMA_EN (1 << 1)
129#define FTGMAC100_MACCR_TXMAC_EN (1 << 2)
130#define FTGMAC100_MACCR_RXMAC_EN (1 << 3)
131#define FTGMAC100_MACCR_RM_VLAN (1 << 4)
132#define FTGMAC100_MACCR_HPTXR_EN (1 << 5)
133#define FTGMAC100_MACCR_LOOP_EN (1 << 6)
134#define FTGMAC100_MACCR_ENRX_IN_HALFTX (1 << 7)
135#define FTGMAC100_MACCR_FULLDUP (1 << 8)
136#define FTGMAC100_MACCR_GIGA_MODE (1 << 9)
137#define FTGMAC100_MACCR_CRC_APD (1 << 10)
138#define FTGMAC100_MACCR_RX_RUNT (1 << 12)
139#define FTGMAC100_MACCR_JUMBO_LF (1 << 13)
140#define FTGMAC100_MACCR_RX_ALL (1 << 14)
141#define FTGMAC100_MACCR_HT_MULTI_EN (1 << 15)
142#define FTGMAC100_MACCR_RX_MULTIPKT (1 << 16)
143#define FTGMAC100_MACCR_RX_BROADPKT (1 << 17)
144#define FTGMAC100_MACCR_DISCARD_CRCERR (1 << 18)
145#define FTGMAC100_MACCR_FAST_MODE (1 << 19)
146#define FTGMAC100_MACCR_SW_RST (1 << 31)
147
148
149
150
151#define FTGMAC100_TXDES0_TXBUF_SIZE(x) ((x) & 0x3fff)
152#define FTGMAC100_TXDES0_EDOTR (1 << 15)
153#define FTGMAC100_TXDES0_CRC_ERR (1 << 19)
154#define FTGMAC100_TXDES0_LTS (1 << 28)
155#define FTGMAC100_TXDES0_FTS (1 << 29)
156#define FTGMAC100_TXDES0_EDOTR_ASPEED (1 << 30)
157#define FTGMAC100_TXDES0_TXDMA_OWN (1 << 31)
158
159#define FTGMAC100_TXDES1_VLANTAG_CI(x) ((x) & 0xffff)
160#define FTGMAC100_TXDES1_INS_VLANTAG (1 << 16)
161#define FTGMAC100_TXDES1_TCP_CHKSUM (1 << 17)
162#define FTGMAC100_TXDES1_UDP_CHKSUM (1 << 18)
163#define FTGMAC100_TXDES1_IP_CHKSUM (1 << 19)
164#define FTGMAC100_TXDES1_LLC (1 << 22)
165#define FTGMAC100_TXDES1_TX2FIC (1 << 30)
166#define FTGMAC100_TXDES1_TXIC (1 << 31)
167
168
169
170
171#define FTGMAC100_RXDES0_VDBC 0x3fff
172#define FTGMAC100_RXDES0_EDORR (1 << 15)
173#define FTGMAC100_RXDES0_MULTICAST (1 << 16)
174#define FTGMAC100_RXDES0_BROADCAST (1 << 17)
175#define FTGMAC100_RXDES0_RX_ERR (1 << 18)
176#define FTGMAC100_RXDES0_CRC_ERR (1 << 19)
177#define FTGMAC100_RXDES0_FTL (1 << 20)
178#define FTGMAC100_RXDES0_RUNT (1 << 21)
179#define FTGMAC100_RXDES0_RX_ODD_NB (1 << 22)
180#define FTGMAC100_RXDES0_FIFO_FULL (1 << 23)
181#define FTGMAC100_RXDES0_PAUSE_OPCODE (1 << 24)
182#define FTGMAC100_RXDES0_PAUSE_FRAME (1 << 25)
183#define FTGMAC100_RXDES0_LRS (1 << 28)
184#define FTGMAC100_RXDES0_FRS (1 << 29)
185#define FTGMAC100_RXDES0_EDORR_ASPEED (1 << 30)
186#define FTGMAC100_RXDES0_RXPKT_RDY (1 << 31)
187
188#define FTGMAC100_RXDES1_VLANTAG_CI 0xffff
189#define FTGMAC100_RXDES1_PROT_MASK (0x3 << 20)
190#define FTGMAC100_RXDES1_PROT_NONIP (0x0 << 20)
191#define FTGMAC100_RXDES1_PROT_IP (0x1 << 20)
192#define FTGMAC100_RXDES1_PROT_TCPIP (0x2 << 20)
193#define FTGMAC100_RXDES1_PROT_UDPIP (0x3 << 20)
194#define FTGMAC100_RXDES1_LLC (1 << 22)
195#define FTGMAC100_RXDES1_DF (1 << 23)
196#define FTGMAC100_RXDES1_VLANTAG_AVAIL (1 << 24)
197#define FTGMAC100_RXDES1_TCP_CHKSUM_ERR (1 << 25)
198#define FTGMAC100_RXDES1_UDP_CHKSUM_ERR (1 << 26)
199#define FTGMAC100_RXDES1_IP_CHKSUM_ERR (1 << 27)
200
201
202
203
204typedef struct {
205 uint32_t des0;
206 uint32_t des1;
207 uint32_t des2;
208 uint32_t des3;
209} FTGMAC100Desc;
210
211#define FTGMAC100_DESC_ALIGNMENT 16
212
213
214
215
216#define RTL8211E_MII_PHYCR 16
217#define RTL8211E_MII_PHYSR 17
218#define RTL8211E_MII_INER 18
219#define RTL8211E_MII_INSR 19
220#define RTL8211E_MII_RXERC 24
221#define RTL8211E_MII_LDPSR 27
222#define RTL8211E_MII_EPAGSR 30
223#define RTL8211E_MII_PAGSEL 31
224
225
226
227
228#define PHY_INT_AUTONEG_ERROR (1 << 15)
229#define PHY_INT_PAGE_RECV (1 << 12)
230#define PHY_INT_AUTONEG_COMPLETE (1 << 11)
231#define PHY_INT_LINK_STATUS (1 << 10)
232#define PHY_INT_ERROR (1 << 9)
233#define PHY_INT_DOWN (1 << 8)
234#define PHY_INT_JABBER (1 << 0)
235
236
237
238
239#define FTGMAC100_MAX_FRAME_SIZE 9220
240
241
242
243
244
245
246static int ftgmac100_max_frame_size(FTGMAC100State *s, uint16_t proto)
247{
248 int max = (s->maccr & FTGMAC100_MACCR_JUMBO_LF ? 9216 : 1518);
249
250 return max + (proto == ETH_P_VLAN ? 4 : 0);
251}
252
253static void ftgmac100_update_irq(FTGMAC100State *s)
254{
255 qemu_set_irq(s->irq, s->isr & s->ier);
256}
257
258
259
260
261
262
263
264static void phy_update_irq(FTGMAC100State *s)
265{
266 ftgmac100_update_irq(s);
267}
268
269static void phy_update_link(FTGMAC100State *s)
270{
271
272 if (qemu_get_queue(s->nic)->link_down) {
273 s->phy_status &= ~(MII_BMSR_LINK_ST | MII_BMSR_AN_COMP);
274 s->phy_int |= PHY_INT_DOWN;
275 } else {
276 s->phy_status |= (MII_BMSR_LINK_ST | MII_BMSR_AN_COMP);
277 s->phy_int |= PHY_INT_AUTONEG_COMPLETE;
278 }
279 phy_update_irq(s);
280}
281
282static void ftgmac100_set_link(NetClientState *nc)
283{
284 phy_update_link(FTGMAC100(qemu_get_nic_opaque(nc)));
285}
286
287static void phy_reset(FTGMAC100State *s)
288{
289 s->phy_status = (MII_BMSR_100TX_FD | MII_BMSR_100TX_HD | MII_BMSR_10T_FD |
290 MII_BMSR_10T_HD | MII_BMSR_EXTSTAT | MII_BMSR_MFPS |
291 MII_BMSR_AN_COMP | MII_BMSR_AUTONEG | MII_BMSR_LINK_ST |
292 MII_BMSR_EXTCAP);
293 s->phy_control = (MII_BMCR_AUTOEN | MII_BMCR_FD | MII_BMCR_SPEED1000);
294 s->phy_advertise = (MII_ANAR_PAUSE_ASYM | MII_ANAR_PAUSE | MII_ANAR_TXFD |
295 MII_ANAR_TX | MII_ANAR_10FD | MII_ANAR_10 |
296 MII_ANAR_CSMACD);
297 s->phy_int_mask = 0;
298 s->phy_int = 0;
299}
300
301static uint16_t do_phy_read(FTGMAC100State *s, uint8_t reg)
302{
303 uint16_t val;
304
305 switch (reg) {
306 case MII_BMCR:
307 val = s->phy_control;
308 break;
309 case MII_BMSR:
310 val = s->phy_status;
311 break;
312 case MII_PHYID1:
313 val = RTL8211E_PHYID1;
314 break;
315 case MII_PHYID2:
316 val = RTL8211E_PHYID2;
317 break;
318 case MII_ANAR:
319 val = s->phy_advertise;
320 break;
321 case MII_ANLPAR:
322 val = (MII_ANLPAR_ACK | MII_ANLPAR_PAUSE | MII_ANLPAR_TXFD |
323 MII_ANLPAR_TX | MII_ANLPAR_10FD | MII_ANLPAR_10 |
324 MII_ANLPAR_CSMACD);
325 break;
326 case MII_ANER:
327 val = MII_ANER_NWAY;
328 break;
329 case MII_CTRL1000:
330 val = (MII_CTRL1000_HALF | MII_CTRL1000_FULL);
331 break;
332 case MII_STAT1000:
333 val = MII_STAT1000_FULL;
334 break;
335 case RTL8211E_MII_INSR:
336 val = s->phy_int;
337 s->phy_int = 0;
338 phy_update_irq(s);
339 break;
340 case RTL8211E_MII_INER:
341 val = s->phy_int_mask;
342 break;
343 case RTL8211E_MII_PHYCR:
344 case RTL8211E_MII_PHYSR:
345 case RTL8211E_MII_RXERC:
346 case RTL8211E_MII_LDPSR:
347 case RTL8211E_MII_EPAGSR:
348 case RTL8211E_MII_PAGSEL:
349 qemu_log_mask(LOG_UNIMP, "%s: reg %d not implemented\n",
350 __func__, reg);
351 val = 0;
352 break;
353 default:
354 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad address at offset %d\n",
355 __func__, reg);
356 val = 0;
357 break;
358 }
359
360 return val;
361}
362
363#define MII_BMCR_MASK (MII_BMCR_LOOPBACK | MII_BMCR_SPEED100 | \
364 MII_BMCR_SPEED | MII_BMCR_AUTOEN | MII_BMCR_PDOWN | \
365 MII_BMCR_FD | MII_BMCR_CTST)
366#define MII_ANAR_MASK 0x2d7f
367
368static void do_phy_write(FTGMAC100State *s, uint8_t reg, uint16_t val)
369{
370 switch (reg) {
371 case MII_BMCR:
372 if (val & MII_BMCR_RESET) {
373 phy_reset(s);
374 } else {
375 s->phy_control = val & MII_BMCR_MASK;
376
377 if (val & MII_BMCR_AUTOEN) {
378 s->phy_status |= MII_BMSR_AN_COMP;
379 }
380 }
381 break;
382 case MII_ANAR:
383 s->phy_advertise = (val & MII_ANAR_MASK) | MII_ANAR_TX;
384 break;
385 case RTL8211E_MII_INER:
386 s->phy_int_mask = val & 0xff;
387 phy_update_irq(s);
388 break;
389 case RTL8211E_MII_PHYCR:
390 case RTL8211E_MII_PHYSR:
391 case RTL8211E_MII_RXERC:
392 case RTL8211E_MII_LDPSR:
393 case RTL8211E_MII_EPAGSR:
394 case RTL8211E_MII_PAGSEL:
395 qemu_log_mask(LOG_UNIMP, "%s: reg %d not implemented\n",
396 __func__, reg);
397 break;
398 default:
399 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad address at offset %d\n",
400 __func__, reg);
401 break;
402 }
403}
404
405static void do_phy_new_ctl(FTGMAC100State *s)
406{
407 uint8_t reg;
408 uint16_t data;
409
410 if (!(s->phycr & FTGMAC100_PHYCR_NEW_ST_22)) {
411 qemu_log_mask(LOG_UNIMP, "%s: unsupported ST code\n", __func__);
412 return;
413 }
414
415
416 if (!(s->phycr & FTGMAC100_PHYCR_NEW_FIRE)) {
417 return;
418 }
419
420 reg = FTGMAC100_PHYCR_NEW_REG(s->phycr);
421 data = FTGMAC100_PHYCR_NEW_DATA(s->phycr);
422
423 switch (FTGMAC100_PHYCR_NEW_OP(s->phycr)) {
424 case FTGMAC100_PHYCR_NEW_OP_WRITE:
425 do_phy_write(s, reg, data);
426 break;
427 case FTGMAC100_PHYCR_NEW_OP_READ:
428 s->phydata = do_phy_read(s, reg) & 0xffff;
429 break;
430 default:
431 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid OP code %08x\n",
432 __func__, s->phycr);
433 }
434
435 s->phycr &= ~FTGMAC100_PHYCR_NEW_FIRE;
436}
437
438static void do_phy_ctl(FTGMAC100State *s)
439{
440 uint8_t reg = FTGMAC100_PHYCR_REG(s->phycr);
441
442 if (s->phycr & FTGMAC100_PHYCR_MIIWR) {
443 do_phy_write(s, reg, s->phydata & 0xffff);
444 s->phycr &= ~FTGMAC100_PHYCR_MIIWR;
445 } else if (s->phycr & FTGMAC100_PHYCR_MIIRD) {
446 s->phydata = do_phy_read(s, reg) << 16;
447 s->phycr &= ~FTGMAC100_PHYCR_MIIRD;
448 } else {
449 qemu_log_mask(LOG_GUEST_ERROR, "%s: no OP code %08x\n",
450 __func__, s->phycr);
451 }
452}
453
454static int ftgmac100_read_bd(FTGMAC100Desc *bd, dma_addr_t addr)
455{
456 if (dma_memory_read(&address_space_memory, addr,
457 bd, sizeof(*bd), MEMTXATTRS_UNSPECIFIED)) {
458 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to read descriptor @ 0x%"
459 HWADDR_PRIx "\n", __func__, addr);
460 return -1;
461 }
462 bd->des0 = le32_to_cpu(bd->des0);
463 bd->des1 = le32_to_cpu(bd->des1);
464 bd->des2 = le32_to_cpu(bd->des2);
465 bd->des3 = le32_to_cpu(bd->des3);
466 return 0;
467}
468
469static int ftgmac100_write_bd(FTGMAC100Desc *bd, dma_addr_t addr)
470{
471 FTGMAC100Desc lebd;
472
473 lebd.des0 = cpu_to_le32(bd->des0);
474 lebd.des1 = cpu_to_le32(bd->des1);
475 lebd.des2 = cpu_to_le32(bd->des2);
476 lebd.des3 = cpu_to_le32(bd->des3);
477 if (dma_memory_write(&address_space_memory, addr,
478 &lebd, sizeof(lebd), MEMTXATTRS_UNSPECIFIED)) {
479 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to write descriptor @ 0x%"
480 HWADDR_PRIx "\n", __func__, addr);
481 return -1;
482 }
483 return 0;
484}
485
486static int ftgmac100_insert_vlan(FTGMAC100State *s, int frame_size,
487 uint8_t vlan_tci)
488{
489 uint8_t *vlan_hdr = s->frame + (ETH_ALEN * 2);
490 uint8_t *payload = vlan_hdr + sizeof(struct vlan_header);
491
492 if (frame_size < sizeof(struct eth_header)) {
493 qemu_log_mask(LOG_GUEST_ERROR,
494 "%s: frame too small for VLAN insertion : %d bytes\n",
495 __func__, frame_size);
496 s->isr |= FTGMAC100_INT_XPKT_LOST;
497 goto out;
498 }
499
500 if (frame_size + sizeof(struct vlan_header) > sizeof(s->frame)) {
501 qemu_log_mask(LOG_GUEST_ERROR,
502 "%s: frame too big : %d bytes\n",
503 __func__, frame_size);
504 s->isr |= FTGMAC100_INT_XPKT_LOST;
505 frame_size -= sizeof(struct vlan_header);
506 }
507
508 memmove(payload, vlan_hdr, frame_size - (ETH_ALEN * 2));
509 stw_be_p(vlan_hdr, ETH_P_VLAN);
510 stw_be_p(vlan_hdr + 2, vlan_tci);
511 frame_size += sizeof(struct vlan_header);
512
513out:
514 return frame_size;
515}
516
517static void ftgmac100_do_tx(FTGMAC100State *s, uint32_t tx_ring,
518 uint32_t tx_descriptor)
519{
520 int frame_size = 0;
521 uint8_t *ptr = s->frame;
522 uint32_t addr = tx_descriptor;
523 uint32_t flags = 0;
524
525 while (1) {
526 FTGMAC100Desc bd;
527 int len;
528
529 if (ftgmac100_read_bd(&bd, addr) ||
530 ((bd.des0 & FTGMAC100_TXDES0_TXDMA_OWN) == 0)) {
531
532 s->isr |= FTGMAC100_INT_NO_NPTXBUF;
533 break;
534 }
535
536
537
538 if (bd.des0 & FTGMAC100_TXDES0_FTS) {
539 flags = bd.des1;
540 }
541
542 len = FTGMAC100_TXDES0_TXBUF_SIZE(bd.des0);
543 if (!len) {
544
545
546
547
548 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid segment size\n",
549 __func__);
550 }
551
552 if (frame_size + len > sizeof(s->frame)) {
553 qemu_log_mask(LOG_GUEST_ERROR, "%s: frame too big : %d bytes\n",
554 __func__, len);
555 s->isr |= FTGMAC100_INT_XPKT_LOST;
556 len = sizeof(s->frame) - frame_size;
557 }
558
559 if (dma_memory_read(&address_space_memory, bd.des3,
560 ptr, len, MEMTXATTRS_UNSPECIFIED)) {
561 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to read packet @ 0x%x\n",
562 __func__, bd.des3);
563 s->isr |= FTGMAC100_INT_AHB_ERR;
564 break;
565 }
566
567 ptr += len;
568 frame_size += len;
569 if (bd.des0 & FTGMAC100_TXDES0_LTS) {
570 int csum = 0;
571
572
573 if (flags & FTGMAC100_TXDES1_INS_VLANTAG &&
574 be16_to_cpu(PKT_GET_ETH_HDR(s->frame)->h_proto) != ETH_P_VLAN) {
575 frame_size = ftgmac100_insert_vlan(s, frame_size,
576 FTGMAC100_TXDES1_VLANTAG_CI(flags));
577 }
578
579 if (flags & FTGMAC100_TXDES1_IP_CHKSUM) {
580 csum |= CSUM_IP;
581 }
582 if (flags & FTGMAC100_TXDES1_TCP_CHKSUM) {
583 csum |= CSUM_TCP;
584 }
585 if (flags & FTGMAC100_TXDES1_UDP_CHKSUM) {
586 csum |= CSUM_UDP;
587 }
588 if (csum) {
589 net_checksum_calculate(s->frame, frame_size, csum);
590 }
591
592
593 qemu_send_packet(qemu_get_queue(s->nic), s->frame, frame_size);
594 ptr = s->frame;
595 frame_size = 0;
596 s->isr |= FTGMAC100_INT_XPKT_ETH;
597 }
598
599 if (flags & FTGMAC100_TXDES1_TX2FIC) {
600 s->isr |= FTGMAC100_INT_XPKT_FIFO;
601 }
602 bd.des0 &= ~FTGMAC100_TXDES0_TXDMA_OWN;
603
604
605 ftgmac100_write_bd(&bd, addr);
606
607 if (bd.des0 & s->txdes0_edotr) {
608 addr = tx_ring;
609 } else {
610 addr += FTGMAC100_DBLAC_TXDES_SIZE(s->dblac);
611 }
612 }
613
614 s->tx_descriptor = addr;
615
616 ftgmac100_update_irq(s);
617}
618
619static bool ftgmac100_can_receive(NetClientState *nc)
620{
621 FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
622 FTGMAC100Desc bd;
623
624 if ((s->maccr & (FTGMAC100_MACCR_RXDMA_EN | FTGMAC100_MACCR_RXMAC_EN))
625 != (FTGMAC100_MACCR_RXDMA_EN | FTGMAC100_MACCR_RXMAC_EN)) {
626 return false;
627 }
628
629 if (ftgmac100_read_bd(&bd, s->rx_descriptor)) {
630 return false;
631 }
632 return !(bd.des0 & FTGMAC100_RXDES0_RXPKT_RDY);
633}
634
635
636
637
638
639
640static uint32_t ftgmac100_rxpoll(FTGMAC100State *s)
641{
642
643
644
645
646
647
648
649
650 static const int div[] = { 20, 200, 1000 };
651
652 uint32_t cnt = 1024 * FTGMAC100_APTC_RXPOLL_CNT(s->aptcr);
653 uint32_t speed = (s->maccr & FTGMAC100_MACCR_FAST_MODE) ? 1 : 0;
654
655 if (s->aptcr & FTGMAC100_APTC_RXPOLL_TIME_SEL) {
656 cnt <<= 4;
657 }
658
659 if (s->maccr & FTGMAC100_MACCR_GIGA_MODE) {
660 speed = 2;
661 }
662
663 return cnt / div[speed];
664}
665
666static void ftgmac100_do_reset(FTGMAC100State *s, bool sw_reset)
667{
668
669 s->isr = 0;
670 s->ier = 0;
671 s->rx_enabled = 0;
672 s->rx_ring = 0;
673 s->rbsr = 0x640;
674 s->rx_descriptor = 0;
675 s->tx_ring = 0;
676 s->tx_descriptor = 0;
677 s->math[0] = 0;
678 s->math[1] = 0;
679 s->itc = 0;
680 s->aptcr = 1;
681 s->dblac = 0x00022f00;
682 s->revr = 0;
683 s->fear1 = 0;
684 s->tpafcr = 0xf1;
685
686 if (sw_reset) {
687 s->maccr &= FTGMAC100_MACCR_GIGA_MODE | FTGMAC100_MACCR_FAST_MODE;
688 } else {
689 s->maccr = 0;
690 }
691
692 s->phycr = 0;
693 s->phydata = 0;
694 s->fcr = 0x400;
695
696
697 phy_reset(s);
698}
699
700static void ftgmac100_reset(DeviceState *d)
701{
702 ftgmac100_do_reset(FTGMAC100(d), false);
703}
704
705static uint64_t ftgmac100_read(void *opaque, hwaddr addr, unsigned size)
706{
707 FTGMAC100State *s = FTGMAC100(opaque);
708
709 switch (addr & 0xff) {
710 case FTGMAC100_ISR:
711 return s->isr;
712 case FTGMAC100_IER:
713 return s->ier;
714 case FTGMAC100_MAC_MADR:
715 return (s->conf.macaddr.a[0] << 8) | s->conf.macaddr.a[1];
716 case FTGMAC100_MAC_LADR:
717 return ((uint32_t) s->conf.macaddr.a[2] << 24) |
718 (s->conf.macaddr.a[3] << 16) | (s->conf.macaddr.a[4] << 8) |
719 s->conf.macaddr.a[5];
720 case FTGMAC100_MATH0:
721 return s->math[0];
722 case FTGMAC100_MATH1:
723 return s->math[1];
724 case FTGMAC100_RXR_BADR:
725 return s->rx_ring;
726 case FTGMAC100_NPTXR_BADR:
727 return s->tx_ring;
728 case FTGMAC100_ITC:
729 return s->itc;
730 case FTGMAC100_DBLAC:
731 return s->dblac;
732 case FTGMAC100_REVR:
733 return s->revr;
734 case FTGMAC100_FEAR1:
735 return s->fear1;
736 case FTGMAC100_TPAFCR:
737 return s->tpafcr;
738 case FTGMAC100_FCR:
739 return s->fcr;
740 case FTGMAC100_MACCR:
741 return s->maccr;
742 case FTGMAC100_PHYCR:
743 return s->phycr;
744 case FTGMAC100_PHYDATA:
745 return s->phydata;
746
747
748 case FTGMAC100_HPTXPD:
749 case FTGMAC100_HPTXR_BADR:
750 case FTGMAC100_MACSR:
751 qemu_log_mask(LOG_UNIMP, "%s: read to unimplemented register 0x%"
752 HWADDR_PRIx "\n", __func__, addr);
753 return 0;
754 default:
755 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad address at offset 0x%"
756 HWADDR_PRIx "\n", __func__, addr);
757 return 0;
758 }
759}
760
761static void ftgmac100_write(void *opaque, hwaddr addr,
762 uint64_t value, unsigned size)
763{
764 FTGMAC100State *s = FTGMAC100(opaque);
765
766 switch (addr & 0xff) {
767 case FTGMAC100_ISR:
768 s->isr &= ~value;
769 break;
770 case FTGMAC100_IER:
771 s->ier = value;
772 break;
773 case FTGMAC100_MAC_MADR:
774 s->conf.macaddr.a[0] = value >> 8;
775 s->conf.macaddr.a[1] = value;
776 break;
777 case FTGMAC100_MAC_LADR:
778 s->conf.macaddr.a[2] = value >> 24;
779 s->conf.macaddr.a[3] = value >> 16;
780 s->conf.macaddr.a[4] = value >> 8;
781 s->conf.macaddr.a[5] = value;
782 break;
783 case FTGMAC100_MATH0:
784 s->math[0] = value;
785 break;
786 case FTGMAC100_MATH1:
787 s->math[1] = value;
788 break;
789 case FTGMAC100_ITC:
790 s->itc = value;
791 break;
792 case FTGMAC100_RXR_BADR:
793 if (!QEMU_IS_ALIGNED(value, FTGMAC100_DESC_ALIGNMENT)) {
794 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad RX buffer alignment 0x%"
795 HWADDR_PRIx "\n", __func__, value);
796 return;
797 }
798
799 s->rx_ring = value;
800 s->rx_descriptor = s->rx_ring;
801 break;
802
803 case FTGMAC100_RBSR:
804 s->rbsr = value;
805 break;
806
807 case FTGMAC100_NPTXR_BADR:
808 if (!QEMU_IS_ALIGNED(value, FTGMAC100_DESC_ALIGNMENT)) {
809 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad TX buffer alignment 0x%"
810 HWADDR_PRIx "\n", __func__, value);
811 return;
812 }
813 s->tx_ring = value;
814 s->tx_descriptor = s->tx_ring;
815 break;
816
817 case FTGMAC100_NPTXPD:
818 if ((s->maccr & (FTGMAC100_MACCR_TXDMA_EN | FTGMAC100_MACCR_TXMAC_EN))
819 == (FTGMAC100_MACCR_TXDMA_EN | FTGMAC100_MACCR_TXMAC_EN)) {
820
821 ftgmac100_do_tx(s, s->tx_ring, s->tx_descriptor);
822 }
823 if (ftgmac100_can_receive(qemu_get_queue(s->nic))) {
824 qemu_flush_queued_packets(qemu_get_queue(s->nic));
825 }
826 break;
827
828 case FTGMAC100_RXPD:
829 if (ftgmac100_can_receive(qemu_get_queue(s->nic))) {
830 qemu_flush_queued_packets(qemu_get_queue(s->nic));
831 }
832 break;
833
834 case FTGMAC100_APTC:
835 s->aptcr = value;
836
837 if (FTGMAC100_APTC_RXPOLL_CNT(s->aptcr)) {
838 ftgmac100_rxpoll(s);
839 }
840
841 if (FTGMAC100_APTC_TXPOLL_CNT(s->aptcr)) {
842 qemu_log_mask(LOG_UNIMP, "%s: no transmit polling\n", __func__);
843 }
844 break;
845
846 case FTGMAC100_MACCR:
847 s->maccr = value;
848 if (value & FTGMAC100_MACCR_SW_RST) {
849 ftgmac100_do_reset(s, true);
850 }
851
852 if (ftgmac100_can_receive(qemu_get_queue(s->nic))) {
853 qemu_flush_queued_packets(qemu_get_queue(s->nic));
854 }
855 break;
856
857 case FTGMAC100_PHYCR:
858 s->phycr = value;
859 if (s->revr & FTGMAC100_REVR_NEW_MDIO_INTERFACE) {
860 do_phy_new_ctl(s);
861 } else {
862 do_phy_ctl(s);
863 }
864 break;
865 case FTGMAC100_PHYDATA:
866 s->phydata = value & 0xffff;
867 break;
868 case FTGMAC100_DBLAC:
869 if (FTGMAC100_DBLAC_TXDES_SIZE(value) < sizeof(FTGMAC100Desc)) {
870 qemu_log_mask(LOG_GUEST_ERROR,
871 "%s: transmit descriptor too small: %" PRIx64
872 " bytes\n", __func__,
873 FTGMAC100_DBLAC_TXDES_SIZE(value));
874 break;
875 }
876 if (FTGMAC100_DBLAC_RXDES_SIZE(value) < sizeof(FTGMAC100Desc)) {
877 qemu_log_mask(LOG_GUEST_ERROR,
878 "%s: receive descriptor too small : %" PRIx64
879 " bytes\n", __func__,
880 FTGMAC100_DBLAC_RXDES_SIZE(value));
881 break;
882 }
883 s->dblac = value;
884 break;
885 case FTGMAC100_REVR:
886 s->revr = value;
887 break;
888 case FTGMAC100_FEAR1:
889 s->fear1 = value;
890 break;
891 case FTGMAC100_TPAFCR:
892 s->tpafcr = value;
893 break;
894 case FTGMAC100_FCR:
895 s->fcr = value;
896 break;
897
898 case FTGMAC100_HPTXPD:
899 case FTGMAC100_HPTXR_BADR:
900 case FTGMAC100_MACSR:
901 qemu_log_mask(LOG_UNIMP, "%s: write to unimplemented register 0x%"
902 HWADDR_PRIx "\n", __func__, addr);
903 break;
904 default:
905 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad address at offset 0x%"
906 HWADDR_PRIx "\n", __func__, addr);
907 break;
908 }
909
910 ftgmac100_update_irq(s);
911}
912
913static int ftgmac100_filter(FTGMAC100State *s, const uint8_t *buf, size_t len)
914{
915 unsigned mcast_idx;
916
917 if (s->maccr & FTGMAC100_MACCR_RX_ALL) {
918 return 1;
919 }
920
921 switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) {
922 case ETH_PKT_BCAST:
923 if (!(s->maccr & FTGMAC100_MACCR_RX_BROADPKT)) {
924 return 0;
925 }
926 break;
927 case ETH_PKT_MCAST:
928 if (!(s->maccr & FTGMAC100_MACCR_RX_MULTIPKT)) {
929 if (!(s->maccr & FTGMAC100_MACCR_HT_MULTI_EN)) {
930 return 0;
931 }
932
933 mcast_idx = net_crc32_le(buf, ETH_ALEN);
934 mcast_idx = (~(mcast_idx >> 2)) & 0x3f;
935 if (!(s->math[mcast_idx / 32] & (1 << (mcast_idx % 32)))) {
936 return 0;
937 }
938 }
939 break;
940 case ETH_PKT_UCAST:
941 if (memcmp(s->conf.macaddr.a, buf, 6)) {
942 return 0;
943 }
944 break;
945 }
946
947 return 1;
948}
949
950static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
951 size_t len)
952{
953 FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
954 FTGMAC100Desc bd;
955 uint32_t flags = 0;
956 uint32_t addr;
957 uint32_t crc;
958 uint32_t buf_addr;
959 uint8_t *crc_ptr;
960 uint32_t buf_len;
961 size_t size = len;
962 uint32_t first = FTGMAC100_RXDES0_FRS;
963 uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(buf)->h_proto);
964 int max_frame_size = ftgmac100_max_frame_size(s, proto);
965
966 if ((s->maccr & (FTGMAC100_MACCR_RXDMA_EN | FTGMAC100_MACCR_RXMAC_EN))
967 != (FTGMAC100_MACCR_RXDMA_EN | FTGMAC100_MACCR_RXMAC_EN)) {
968 return -1;
969 }
970
971
972
973 if (size < 10) {
974 qemu_log_mask(LOG_GUEST_ERROR, "%s: dropped frame of %zd bytes\n",
975 __func__, size);
976 return size;
977 }
978
979 if (!ftgmac100_filter(s, buf, size)) {
980 return size;
981 }
982
983
984 size += 4;
985 crc = cpu_to_be32(crc32(~0, buf, size));
986 crc_ptr = (uint8_t *) &crc;
987
988
989 if (size > max_frame_size) {
990 qemu_log_mask(LOG_GUEST_ERROR, "%s: frame too big : %zd bytes\n",
991 __func__, size);
992 size = max_frame_size;
993 flags |= FTGMAC100_RXDES0_FTL;
994 }
995
996 switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) {
997 case ETH_PKT_BCAST:
998 flags |= FTGMAC100_RXDES0_BROADCAST;
999 break;
1000 case ETH_PKT_MCAST:
1001 flags |= FTGMAC100_RXDES0_MULTICAST;
1002 break;
1003 case ETH_PKT_UCAST:
1004 break;
1005 }
1006
1007 s->isr |= FTGMAC100_INT_RPKT_FIFO;
1008 addr = s->rx_descriptor;
1009 while (size > 0) {
1010 if (!ftgmac100_can_receive(nc)) {
1011 qemu_log_mask(LOG_GUEST_ERROR, "%s: Unexpected packet\n", __func__);
1012 return -1;
1013 }
1014
1015 if (ftgmac100_read_bd(&bd, addr) ||
1016 (bd.des0 & FTGMAC100_RXDES0_RXPKT_RDY)) {
1017
1018 qemu_log_mask(LOG_GUEST_ERROR, "%s: Lost end of frame\n",
1019 __func__);
1020 s->isr |= FTGMAC100_INT_NO_RXBUF;
1021 break;
1022 }
1023 buf_len = (size <= s->rbsr) ? size : s->rbsr;
1024 bd.des0 |= buf_len & 0x3fff;
1025 size -= buf_len;
1026
1027
1028 if (size < 4) {
1029 buf_len += size - 4;
1030 }
1031 buf_addr = bd.des3;
1032 if (first && proto == ETH_P_VLAN && buf_len >= 18) {
1033 bd.des1 = lduw_be_p(buf + 14) | FTGMAC100_RXDES1_VLANTAG_AVAIL;
1034
1035 if (s->maccr & FTGMAC100_MACCR_RM_VLAN) {
1036 dma_memory_write(&address_space_memory, buf_addr, buf, 12,
1037 MEMTXATTRS_UNSPECIFIED);
1038 dma_memory_write(&address_space_memory, buf_addr + 12,
1039 buf + 16, buf_len - 16,
1040 MEMTXATTRS_UNSPECIFIED);
1041 } else {
1042 dma_memory_write(&address_space_memory, buf_addr, buf,
1043 buf_len, MEMTXATTRS_UNSPECIFIED);
1044 }
1045 } else {
1046 bd.des1 = 0;
1047 dma_memory_write(&address_space_memory, buf_addr, buf, buf_len,
1048 MEMTXATTRS_UNSPECIFIED);
1049 }
1050 buf += buf_len;
1051 if (size < 4) {
1052 dma_memory_write(&address_space_memory, buf_addr + buf_len,
1053 crc_ptr, 4 - size, MEMTXATTRS_UNSPECIFIED);
1054 crc_ptr += 4 - size;
1055 }
1056
1057 bd.des0 |= first | FTGMAC100_RXDES0_RXPKT_RDY;
1058 first = 0;
1059 if (size == 0) {
1060
1061 bd.des0 |= flags | FTGMAC100_RXDES0_LRS;
1062 s->isr |= FTGMAC100_INT_RPKT_BUF;
1063 }
1064 ftgmac100_write_bd(&bd, addr);
1065 if (bd.des0 & s->rxdes0_edorr) {
1066 addr = s->rx_ring;
1067 } else {
1068 addr += FTGMAC100_DBLAC_RXDES_SIZE(s->dblac);
1069 }
1070 }
1071 s->rx_descriptor = addr;
1072
1073 ftgmac100_update_irq(s);
1074 return len;
1075}
1076
1077static const MemoryRegionOps ftgmac100_ops = {
1078 .read = ftgmac100_read,
1079 .write = ftgmac100_write,
1080 .valid.min_access_size = 4,
1081 .valid.max_access_size = 4,
1082 .endianness = DEVICE_LITTLE_ENDIAN,
1083};
1084
1085static void ftgmac100_cleanup(NetClientState *nc)
1086{
1087 FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
1088
1089 s->nic = NULL;
1090}
1091
1092static NetClientInfo net_ftgmac100_info = {
1093 .type = NET_CLIENT_DRIVER_NIC,
1094 .size = sizeof(NICState),
1095 .can_receive = ftgmac100_can_receive,
1096 .receive = ftgmac100_receive,
1097 .cleanup = ftgmac100_cleanup,
1098 .link_status_changed = ftgmac100_set_link,
1099};
1100
1101static void ftgmac100_realize(DeviceState *dev, Error **errp)
1102{
1103 FTGMAC100State *s = FTGMAC100(dev);
1104 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1105
1106 if (s->aspeed) {
1107 s->txdes0_edotr = FTGMAC100_TXDES0_EDOTR_ASPEED;
1108 s->rxdes0_edorr = FTGMAC100_RXDES0_EDORR_ASPEED;
1109 } else {
1110 s->txdes0_edotr = FTGMAC100_TXDES0_EDOTR;
1111 s->rxdes0_edorr = FTGMAC100_RXDES0_EDORR;
1112 }
1113
1114 memory_region_init_io(&s->iomem, OBJECT(dev), &ftgmac100_ops, s,
1115 TYPE_FTGMAC100, 0x2000);
1116 sysbus_init_mmio(sbd, &s->iomem);
1117 sysbus_init_irq(sbd, &s->irq);
1118 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1119
1120 s->nic = qemu_new_nic(&net_ftgmac100_info, &s->conf,
1121 object_get_typename(OBJECT(dev)), dev->id, s);
1122 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
1123}
1124
1125static const VMStateDescription vmstate_ftgmac100 = {
1126 .name = TYPE_FTGMAC100,
1127 .version_id = 1,
1128 .minimum_version_id = 1,
1129 .fields = (VMStateField[]) {
1130 VMSTATE_UINT32(irq_state, FTGMAC100State),
1131 VMSTATE_UINT32(isr, FTGMAC100State),
1132 VMSTATE_UINT32(ier, FTGMAC100State),
1133 VMSTATE_UINT32(rx_enabled, FTGMAC100State),
1134 VMSTATE_UINT32(rx_ring, FTGMAC100State),
1135 VMSTATE_UINT32(rbsr, FTGMAC100State),
1136 VMSTATE_UINT32(tx_ring, FTGMAC100State),
1137 VMSTATE_UINT32(rx_descriptor, FTGMAC100State),
1138 VMSTATE_UINT32(tx_descriptor, FTGMAC100State),
1139 VMSTATE_UINT32_ARRAY(math, FTGMAC100State, 2),
1140 VMSTATE_UINT32(itc, FTGMAC100State),
1141 VMSTATE_UINT32(aptcr, FTGMAC100State),
1142 VMSTATE_UINT32(dblac, FTGMAC100State),
1143 VMSTATE_UINT32(revr, FTGMAC100State),
1144 VMSTATE_UINT32(fear1, FTGMAC100State),
1145 VMSTATE_UINT32(tpafcr, FTGMAC100State),
1146 VMSTATE_UINT32(maccr, FTGMAC100State),
1147 VMSTATE_UINT32(phycr, FTGMAC100State),
1148 VMSTATE_UINT32(phydata, FTGMAC100State),
1149 VMSTATE_UINT32(fcr, FTGMAC100State),
1150 VMSTATE_UINT32(phy_status, FTGMAC100State),
1151 VMSTATE_UINT32(phy_control, FTGMAC100State),
1152 VMSTATE_UINT32(phy_advertise, FTGMAC100State),
1153 VMSTATE_UINT32(phy_int, FTGMAC100State),
1154 VMSTATE_UINT32(phy_int_mask, FTGMAC100State),
1155 VMSTATE_UINT32(txdes0_edotr, FTGMAC100State),
1156 VMSTATE_UINT32(rxdes0_edorr, FTGMAC100State),
1157 VMSTATE_END_OF_LIST()
1158 }
1159};
1160
1161static Property ftgmac100_properties[] = {
1162 DEFINE_PROP_BOOL("aspeed", FTGMAC100State, aspeed, false),
1163 DEFINE_NIC_PROPERTIES(FTGMAC100State, conf),
1164 DEFINE_PROP_END_OF_LIST(),
1165};
1166
1167static void ftgmac100_class_init(ObjectClass *klass, void *data)
1168{
1169 DeviceClass *dc = DEVICE_CLASS(klass);
1170
1171 dc->vmsd = &vmstate_ftgmac100;
1172 dc->reset = ftgmac100_reset;
1173 device_class_set_props(dc, ftgmac100_properties);
1174 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1175 dc->realize = ftgmac100_realize;
1176 dc->desc = "Faraday FTGMAC100 Gigabit Ethernet emulation";
1177}
1178
1179static const TypeInfo ftgmac100_info = {
1180 .name = TYPE_FTGMAC100,
1181 .parent = TYPE_SYS_BUS_DEVICE,
1182 .instance_size = sizeof(FTGMAC100State),
1183 .class_init = ftgmac100_class_init,
1184};
1185
1186
1187
1188
1189#define ASPEED_MII_PHYCR_FIRE BIT(31)
1190#define ASPEED_MII_PHYCR_ST_22 BIT(28)
1191#define ASPEED_MII_PHYCR_OP(x) ((x) & (ASPEED_MII_PHYCR_OP_WRITE | \
1192 ASPEED_MII_PHYCR_OP_READ))
1193#define ASPEED_MII_PHYCR_OP_WRITE BIT(26)
1194#define ASPEED_MII_PHYCR_OP_READ BIT(27)
1195#define ASPEED_MII_PHYCR_DATA(x) (x & 0xffff)
1196#define ASPEED_MII_PHYCR_PHY(x) (((x) >> 21) & 0x1f)
1197#define ASPEED_MII_PHYCR_REG(x) (((x) >> 16) & 0x1f)
1198
1199#define ASPEED_MII_PHYDATA_IDLE BIT(16)
1200
1201static void aspeed_mii_transition(AspeedMiiState *s, bool fire)
1202{
1203 if (fire) {
1204 s->phycr |= ASPEED_MII_PHYCR_FIRE;
1205 s->phydata &= ~ASPEED_MII_PHYDATA_IDLE;
1206 } else {
1207 s->phycr &= ~ASPEED_MII_PHYCR_FIRE;
1208 s->phydata |= ASPEED_MII_PHYDATA_IDLE;
1209 }
1210}
1211
1212static void aspeed_mii_do_phy_ctl(AspeedMiiState *s)
1213{
1214 uint8_t reg;
1215 uint16_t data;
1216
1217 if (!(s->phycr & ASPEED_MII_PHYCR_ST_22)) {
1218 aspeed_mii_transition(s, !ASPEED_MII_PHYCR_FIRE);
1219 qemu_log_mask(LOG_UNIMP, "%s: unsupported ST code\n", __func__);
1220 return;
1221 }
1222
1223
1224 if (!(s->phycr & ASPEED_MII_PHYCR_FIRE)) {
1225 return;
1226 }
1227
1228 reg = ASPEED_MII_PHYCR_REG(s->phycr);
1229 data = ASPEED_MII_PHYCR_DATA(s->phycr);
1230
1231 switch (ASPEED_MII_PHYCR_OP(s->phycr)) {
1232 case ASPEED_MII_PHYCR_OP_WRITE:
1233 do_phy_write(s->nic, reg, data);
1234 break;
1235 case ASPEED_MII_PHYCR_OP_READ:
1236 s->phydata = (s->phydata & ~0xffff) | do_phy_read(s->nic, reg);
1237 break;
1238 default:
1239 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid OP code %08x\n",
1240 __func__, s->phycr);
1241 }
1242
1243 aspeed_mii_transition(s, !ASPEED_MII_PHYCR_FIRE);
1244}
1245
1246static uint64_t aspeed_mii_read(void *opaque, hwaddr addr, unsigned size)
1247{
1248 AspeedMiiState *s = ASPEED_MII(opaque);
1249
1250 switch (addr) {
1251 case 0x0:
1252 return s->phycr;
1253 case 0x4:
1254 return s->phydata;
1255 default:
1256 g_assert_not_reached();
1257 }
1258}
1259
1260static void aspeed_mii_write(void *opaque, hwaddr addr,
1261 uint64_t value, unsigned size)
1262{
1263 AspeedMiiState *s = ASPEED_MII(opaque);
1264
1265 switch (addr) {
1266 case 0x0:
1267 s->phycr = value & ~(s->phycr & ASPEED_MII_PHYCR_FIRE);
1268 break;
1269 case 0x4:
1270 s->phydata = value & ~(0xffff | ASPEED_MII_PHYDATA_IDLE);
1271 break;
1272 default:
1273 g_assert_not_reached();
1274 }
1275
1276 aspeed_mii_transition(s, !!(s->phycr & ASPEED_MII_PHYCR_FIRE));
1277 aspeed_mii_do_phy_ctl(s);
1278}
1279
1280static const MemoryRegionOps aspeed_mii_ops = {
1281 .read = aspeed_mii_read,
1282 .write = aspeed_mii_write,
1283 .valid.min_access_size = 4,
1284 .valid.max_access_size = 4,
1285 .endianness = DEVICE_LITTLE_ENDIAN,
1286};
1287
1288static void aspeed_mii_reset(DeviceState *dev)
1289{
1290 AspeedMiiState *s = ASPEED_MII(dev);
1291
1292 s->phycr = 0;
1293 s->phydata = 0;
1294
1295 aspeed_mii_transition(s, !!(s->phycr & ASPEED_MII_PHYCR_FIRE));
1296};
1297
1298static void aspeed_mii_realize(DeviceState *dev, Error **errp)
1299{
1300 AspeedMiiState *s = ASPEED_MII(dev);
1301 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1302
1303 assert(s->nic);
1304
1305 memory_region_init_io(&s->iomem, OBJECT(dev), &aspeed_mii_ops, s,
1306 TYPE_ASPEED_MII, 0x8);
1307 sysbus_init_mmio(sbd, &s->iomem);
1308}
1309
1310static const VMStateDescription vmstate_aspeed_mii = {
1311 .name = TYPE_ASPEED_MII,
1312 .version_id = 1,
1313 .minimum_version_id = 1,
1314 .fields = (VMStateField[]) {
1315 VMSTATE_UINT32(phycr, FTGMAC100State),
1316 VMSTATE_UINT32(phydata, FTGMAC100State),
1317 VMSTATE_END_OF_LIST()
1318 }
1319};
1320
1321static Property aspeed_mii_properties[] = {
1322 DEFINE_PROP_LINK("nic", AspeedMiiState, nic, TYPE_FTGMAC100,
1323 FTGMAC100State *),
1324 DEFINE_PROP_END_OF_LIST(),
1325};
1326
1327static void aspeed_mii_class_init(ObjectClass *klass, void *data)
1328{
1329 DeviceClass *dc = DEVICE_CLASS(klass);
1330
1331 dc->vmsd = &vmstate_aspeed_mii;
1332 dc->reset = aspeed_mii_reset;
1333 dc->realize = aspeed_mii_realize;
1334 dc->desc = "Aspeed MII controller";
1335 device_class_set_props(dc, aspeed_mii_properties);
1336}
1337
1338static const TypeInfo aspeed_mii_info = {
1339 .name = TYPE_ASPEED_MII,
1340 .parent = TYPE_SYS_BUS_DEVICE,
1341 .instance_size = sizeof(AspeedMiiState),
1342 .class_init = aspeed_mii_class_init,
1343};
1344
1345static void ftgmac100_register_types(void)
1346{
1347 type_register_static(&ftgmac100_info);
1348 type_register_static(&aspeed_mii_info);
1349}
1350
1351type_init(ftgmac100_register_types)
1352