1 /* Ethernet Physical Receiver model.
3 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 Contributed by Analog Devices, Inc.
6 This file is part of simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* This must come before any other includes. */
27 #if defined (HAVE_LINUX_MII_H) && defined (HAVE_LINUX_TYPES_H)
29 /* Workaround old/broken linux headers. */
30 #include <linux/types.h>
31 #include <linux/mii.h>
33 #define REG_PHY_SIZE 0x20
38 bu16 regs
[REG_PHY_SIZE
];
40 #define reg_base() offsetof(struct eth_phy, regs[0])
41 #define reg_offset(reg) (offsetof(struct eth_phy, reg) - reg_base())
42 #define reg_idx(reg) (reg_offset (reg) / 4)
44 static const char * const reg_names
[] =
46 [MII_BMCR
] = "MII_BMCR",
47 [MII_BMSR
] = "MII_BMSR",
48 [MII_PHYSID1
] = "MII_PHYSID1",
49 [MII_PHYSID2
] = "MII_PHYSID2",
50 [MII_ADVERTISE
] = "MII_ADVERTISE",
51 [MII_LPA
] = "MII_LPA",
52 [MII_EXPANSION
] = "MII_EXPANSION",
54 [MII_CTRL1000
] = "MII_CTRL1000",
57 [MII_STAT1000
] = "MII_STAT1000",
60 [MII_ESTATUS
] = "MII_ESTATUS",
62 [MII_DCOUNTER
] = "MII_DCOUNTER",
63 [MII_FCSCOUNTER
] = "MII_FCSCOUNTER",
64 [MII_NWAYTEST
] = "MII_NWAYTEST",
65 [MII_RERRCOUNTER
] = "MII_RERRCOUNTER",
66 [MII_SREVISION
] = "MII_SREVISION",
67 [MII_RESV1
] = "MII_RESV1",
68 [MII_LBRERROR
] = "MII_LBRERROR",
69 [MII_PHYADDR
] = "MII_PHYADDR",
70 [MII_RESV2
] = "MII_RESV2",
71 [MII_TPISTATUS
] = "MII_TPISTATUS",
72 [MII_NCONFIG
] = "MII_NCONFIG",
74 #define mmr_name(off) (reg_names[off] ? : "<INV>")
75 #define mmr_off reg_off
78 eth_phy_io_write_buffer (struct hw
*me
, const void *source
,
79 int space
, address_word addr
, unsigned nr_bytes
)
81 struct eth_phy
*phy
= hw_data (me
);
86 value
= dv_load_2 (source
);
88 reg_off
= addr
- phy
->base
;
89 valuep
= (void *)((uintptr_t)phy
+ reg_base() + reg_off
);
100 /* Discard writes to these. */
103 /* XXX: Discard writes to unknown regs ? */
112 eth_phy_io_read_buffer (struct hw
*me
, void *dest
,
113 int space
, address_word addr
, unsigned nr_bytes
)
115 struct eth_phy
*phy
= hw_data (me
);
119 reg_off
= addr
- phy
->base
;
120 valuep
= (void *)((uintptr_t)phy
+ reg_base() + reg_off
);
127 dv_store_2 (dest
, *valuep
);
130 /* XXX: Let people control this ? */
131 *valuep
= BMSR_100FULL
| BMSR_100HALF
| BMSR_10FULL
| BMSR_10HALF
|
132 BMSR_ANEGCOMPLETE
| BMSR_ANEGCAPABLE
| BMSR_LSTATUS
;
133 dv_store_2 (dest
, *valuep
);
136 /* XXX: Let people control this ? */
137 *valuep
= LPA_100FULL
| LPA_100HALF
| LPA_10FULL
| LPA_10HALF
;
138 dv_store_2 (dest
, *valuep
);
141 dv_store_2 (dest
, *valuep
);
149 attach_eth_phy_regs (struct hw
*me
, struct eth_phy
*phy
)
151 address_word attach_address
;
153 unsigned attach_size
;
154 reg_property_spec reg
;
156 if (hw_find_property (me
, "reg") == NULL
)
157 hw_abort (me
, "Missing \"reg\" property");
159 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
160 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
162 hw_unit_address_to_attach_address (hw_parent (me
),
164 &attach_space
, &attach_address
, me
);
165 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
167 if (attach_size
!= REG_PHY_SIZE
)
168 hw_abort (me
, "\"reg\" size must be %#x", REG_PHY_SIZE
);
170 hw_attach_address (hw_parent (me
),
171 0, attach_space
, attach_address
, attach_size
, me
);
173 phy
->base
= attach_address
;
177 eth_phy_finish (struct hw
*me
)
181 phy
= HW_ZALLOC (me
, struct eth_phy
);
183 set_hw_data (me
, phy
);
184 set_hw_io_read_buffer (me
, eth_phy_io_read_buffer
);
185 set_hw_io_write_buffer (me
, eth_phy_io_write_buffer
);
187 attach_eth_phy_regs (me
, phy
);
189 /* Initialize the PHY. */
190 phy
->regs
[MII_PHYSID1
] = 0; /* Unassigned Vendor */
191 phy
->regs
[MII_PHYSID2
] = 0xAD; /* Product */
197 eth_phy_finish (struct hw
*me
)
199 HW_TRACE ((me
, "No linux/mii.h support found"));
204 const struct hw_descriptor dv_eth_phy_descriptor
[] =
206 {"eth_phy", eth_phy_finish
,},