1 /* Ethernet Physical Receiver model.
3 Copyright (C) 2010-2019 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/>. */
26 #if defined (HAVE_LINUX_MII_H) && defined (HAVE_LINUX_TYPES_H)
28 /* Workaround old/broken linux headers. */
29 #include <linux/types.h>
30 #include <linux/mii.h>
32 #define REG_PHY_SIZE 0x20
37 bu16 regs
[REG_PHY_SIZE
];
39 #define reg_base() offsetof(struct eth_phy, regs[0])
40 #define reg_offset(reg) (offsetof(struct eth_phy, reg) - reg_base())
41 #define reg_idx(reg) (reg_offset (reg) / 4)
43 static const char * const reg_names
[] =
45 [MII_BMCR
] = "MII_BMCR",
46 [MII_BMSR
] = "MII_BMSR",
47 [MII_PHYSID1
] = "MII_PHYSID1",
48 [MII_PHYSID2
] = "MII_PHYSID2",
49 [MII_ADVERTISE
] = "MII_ADVERTISE",
50 [MII_LPA
] = "MII_LPA",
51 [MII_EXPANSION
] = "MII_EXPANSION",
53 [MII_CTRL1000
] = "MII_CTRL1000",
56 [MII_STAT1000
] = "MII_STAT1000",
59 [MII_ESTATUS
] = "MII_ESTATUS",
61 [MII_DCOUNTER
] = "MII_DCOUNTER",
62 [MII_FCSCOUNTER
] = "MII_FCSCOUNTER",
63 [MII_NWAYTEST
] = "MII_NWAYTEST",
64 [MII_RERRCOUNTER
] = "MII_RERRCOUNTER",
65 [MII_SREVISION
] = "MII_SREVISION",
66 [MII_RESV1
] = "MII_RESV1",
67 [MII_LBRERROR
] = "MII_LBRERROR",
68 [MII_PHYADDR
] = "MII_PHYADDR",
69 [MII_RESV2
] = "MII_RESV2",
70 [MII_TPISTATUS
] = "MII_TPISTATUS",
71 [MII_NCONFIG
] = "MII_NCONFIG",
73 #define mmr_name(off) (reg_names[off] ? : "<INV>")
74 #define mmr_off reg_off
77 eth_phy_io_write_buffer (struct hw
*me
, const void *source
,
78 int space
, address_word addr
, unsigned nr_bytes
)
80 struct eth_phy
*phy
= hw_data (me
);
85 value
= dv_load_2 (source
);
87 reg_off
= addr
- phy
->base
;
88 valuep
= (void *)((unsigned long)phy
+ reg_base() + reg_off
);
99 /* Discard writes to these. */
102 /* XXX: Discard writes to unknown regs ? */
111 eth_phy_io_read_buffer (struct hw
*me
, void *dest
,
112 int space
, address_word addr
, unsigned nr_bytes
)
114 struct eth_phy
*phy
= hw_data (me
);
118 reg_off
= addr
- phy
->base
;
119 valuep
= (void *)((unsigned long)phy
+ reg_base() + reg_off
);
126 dv_store_2 (dest
, *valuep
);
129 /* XXX: Let people control this ? */
130 *valuep
= BMSR_100FULL
| BMSR_100HALF
| BMSR_10FULL
| BMSR_10HALF
|
131 BMSR_ANEGCOMPLETE
| BMSR_ANEGCAPABLE
| BMSR_LSTATUS
;
132 dv_store_2 (dest
, *valuep
);
135 /* XXX: Let people control this ? */
136 *valuep
= LPA_100FULL
| LPA_100HALF
| LPA_10FULL
| LPA_10HALF
;
137 dv_store_2 (dest
, *valuep
);
140 dv_store_2 (dest
, *valuep
);
148 attach_eth_phy_regs (struct hw
*me
, struct eth_phy
*phy
)
150 address_word attach_address
;
152 unsigned attach_size
;
153 reg_property_spec reg
;
155 if (hw_find_property (me
, "reg") == NULL
)
156 hw_abort (me
, "Missing \"reg\" property");
158 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
159 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
161 hw_unit_address_to_attach_address (hw_parent (me
),
163 &attach_space
, &attach_address
, me
);
164 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
166 if (attach_size
!= REG_PHY_SIZE
)
167 hw_abort (me
, "\"reg\" size must be %#x", REG_PHY_SIZE
);
169 hw_attach_address (hw_parent (me
),
170 0, attach_space
, attach_address
, attach_size
, me
);
172 phy
->base
= attach_address
;
176 eth_phy_finish (struct hw
*me
)
180 phy
= HW_ZALLOC (me
, struct eth_phy
);
182 set_hw_data (me
, phy
);
183 set_hw_io_read_buffer (me
, eth_phy_io_read_buffer
);
184 set_hw_io_write_buffer (me
, eth_phy_io_write_buffer
);
186 attach_eth_phy_regs (me
, phy
);
188 /* Initialize the PHY. */
189 phy
->regs
[MII_PHYSID1
] = 0; /* Unassigned Vendor */
190 phy
->regs
[MII_PHYSID2
] = 0xAD; /* Product */
196 eth_phy_finish (struct hw
*me
)
198 HW_TRACE ((me
, "No linux/mii.h support found"));
203 const struct hw_descriptor dv_eth_phy_descriptor
[] =
205 {"eth_phy", eth_phy_finish
,},