xref: /qemu/hw/net/imx_fec.c (revision 88e1b59ee30950a66ea28b740914bf603fa9a1ec)
1fcbd8018SJean-Christophe Dubois /*
2fcbd8018SJean-Christophe Dubois  * i.MX Fast Ethernet Controller emulation.
3fcbd8018SJean-Christophe Dubois  *
4fcbd8018SJean-Christophe Dubois  * Copyright (c) 2013 Jean-Christophe Dubois. <jcd@tribudubois.net>
5fcbd8018SJean-Christophe Dubois  *
6fcbd8018SJean-Christophe Dubois  * Based on Coldfire Fast Ethernet Controller emulation.
7fcbd8018SJean-Christophe Dubois  *
8fcbd8018SJean-Christophe Dubois  * Copyright (c) 2007 CodeSourcery.
9fcbd8018SJean-Christophe Dubois  *
10fcbd8018SJean-Christophe Dubois  *  This program is free software; you can redistribute it and/or modify it
11fcbd8018SJean-Christophe Dubois  *  under the terms of the GNU General Public License as published by the
12fcbd8018SJean-Christophe Dubois  *  Free Software Foundation; either version 2 of the License, or
13fcbd8018SJean-Christophe Dubois  *  (at your option) any later version.
14fcbd8018SJean-Christophe Dubois  *
15fcbd8018SJean-Christophe Dubois  *  This program is distributed in the hope that it will be useful, but WITHOUT
16fcbd8018SJean-Christophe Dubois  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17fcbd8018SJean-Christophe Dubois  *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18fcbd8018SJean-Christophe Dubois  *  for more details.
19fcbd8018SJean-Christophe Dubois  *
20fcbd8018SJean-Christophe Dubois  *  You should have received a copy of the GNU General Public License along
21fcbd8018SJean-Christophe Dubois  *  with this program; if not, see <http://www.gnu.org/licenses/>.
22fcbd8018SJean-Christophe Dubois  */
23fcbd8018SJean-Christophe Dubois 
248ef94f0bSPeter Maydell #include "qemu/osdep.h"
25fcbd8018SJean-Christophe Dubois #include "hw/net/imx_fec.h"
26fcbd8018SJean-Christophe Dubois #include "sysemu/dma.h"
2703dd024fSPaolo Bonzini #include "qemu/log.h"
280b8fa32fSMarkus Armbruster #include "qemu/module.h"
29a699b410SJean-Christophe Dubois #include "net/checksum.h"
30a699b410SJean-Christophe Dubois #include "net/eth.h"
31fcbd8018SJean-Christophe Dubois 
32fcbd8018SJean-Christophe Dubois /* For crc32 */
33fcbd8018SJean-Christophe Dubois #include <zlib.h>
34fcbd8018SJean-Christophe Dubois 
35b72d8d25SJean-Christophe Dubois #ifndef DEBUG_IMX_FEC
36b72d8d25SJean-Christophe Dubois #define DEBUG_IMX_FEC 0
37fcbd8018SJean-Christophe Dubois #endif
38fcbd8018SJean-Christophe Dubois 
39b72d8d25SJean-Christophe Dubois #define FEC_PRINTF(fmt, args...) \
40b72d8d25SJean-Christophe Dubois     do { \
41b72d8d25SJean-Christophe Dubois         if (DEBUG_IMX_FEC) { \
42b72d8d25SJean-Christophe Dubois             fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_FEC, \
43b72d8d25SJean-Christophe Dubois                                              __func__, ##args); \
44b72d8d25SJean-Christophe Dubois         } \
45fcbd8018SJean-Christophe Dubois     } while (0)
46b72d8d25SJean-Christophe Dubois 
47b72d8d25SJean-Christophe Dubois #ifndef DEBUG_IMX_PHY
48b72d8d25SJean-Christophe Dubois #define DEBUG_IMX_PHY 0
49fcbd8018SJean-Christophe Dubois #endif
50fcbd8018SJean-Christophe Dubois 
51b72d8d25SJean-Christophe Dubois #define PHY_PRINTF(fmt, args...) \
52b72d8d25SJean-Christophe Dubois     do { \
53b72d8d25SJean-Christophe Dubois         if (DEBUG_IMX_PHY) { \
54b72d8d25SJean-Christophe Dubois             fprintf(stderr, "[%s.phy]%s: " fmt , TYPE_IMX_FEC, \
55b72d8d25SJean-Christophe Dubois                                                  __func__, ##args); \
56b72d8d25SJean-Christophe Dubois         } \
57fcbd8018SJean-Christophe Dubois     } while (0)
58fcbd8018SJean-Christophe Dubois 
5981f17e0dSPrasad J Pandit #define IMX_MAX_DESC    1024
6081f17e0dSPrasad J Pandit 
61a699b410SJean-Christophe Dubois static const char *imx_default_reg_name(IMXFECState *s, uint32_t index)
62db0de352SJean-Christophe Dubois {
63db0de352SJean-Christophe Dubois     static char tmp[20];
64a699b410SJean-Christophe Dubois     sprintf(tmp, "index %d", index);
65a699b410SJean-Christophe Dubois     return tmp;
66a699b410SJean-Christophe Dubois }
67db0de352SJean-Christophe Dubois 
68a699b410SJean-Christophe Dubois static const char *imx_fec_reg_name(IMXFECState *s, uint32_t index)
69a699b410SJean-Christophe Dubois {
70a699b410SJean-Christophe Dubois     switch (index) {
71a699b410SJean-Christophe Dubois     case ENET_FRBR:
72a699b410SJean-Christophe Dubois         return "FRBR";
73a699b410SJean-Christophe Dubois     case ENET_FRSR:
74a699b410SJean-Christophe Dubois         return "FRSR";
75a699b410SJean-Christophe Dubois     case ENET_MIIGSK_CFGR:
76a699b410SJean-Christophe Dubois         return "MIIGSK_CFGR";
77a699b410SJean-Christophe Dubois     case ENET_MIIGSK_ENR:
78a699b410SJean-Christophe Dubois         return "MIIGSK_ENR";
79a699b410SJean-Christophe Dubois     default:
80a699b410SJean-Christophe Dubois         return imx_default_reg_name(s, index);
81a699b410SJean-Christophe Dubois     }
82a699b410SJean-Christophe Dubois }
83a699b410SJean-Christophe Dubois 
84a699b410SJean-Christophe Dubois static const char *imx_enet_reg_name(IMXFECState *s, uint32_t index)
85a699b410SJean-Christophe Dubois {
86a699b410SJean-Christophe Dubois     switch (index) {
87a699b410SJean-Christophe Dubois     case ENET_RSFL:
88a699b410SJean-Christophe Dubois         return "RSFL";
89a699b410SJean-Christophe Dubois     case ENET_RSEM:
90a699b410SJean-Christophe Dubois         return "RSEM";
91a699b410SJean-Christophe Dubois     case ENET_RAEM:
92a699b410SJean-Christophe Dubois         return "RAEM";
93a699b410SJean-Christophe Dubois     case ENET_RAFL:
94a699b410SJean-Christophe Dubois         return "RAFL";
95a699b410SJean-Christophe Dubois     case ENET_TSEM:
96a699b410SJean-Christophe Dubois         return "TSEM";
97a699b410SJean-Christophe Dubois     case ENET_TAEM:
98a699b410SJean-Christophe Dubois         return "TAEM";
99a699b410SJean-Christophe Dubois     case ENET_TAFL:
100a699b410SJean-Christophe Dubois         return "TAFL";
101a699b410SJean-Christophe Dubois     case ENET_TIPG:
102a699b410SJean-Christophe Dubois         return "TIPG";
103a699b410SJean-Christophe Dubois     case ENET_FTRL:
104a699b410SJean-Christophe Dubois         return "FTRL";
105a699b410SJean-Christophe Dubois     case ENET_TACC:
106a699b410SJean-Christophe Dubois         return "TACC";
107a699b410SJean-Christophe Dubois     case ENET_RACC:
108a699b410SJean-Christophe Dubois         return "RACC";
109a699b410SJean-Christophe Dubois     case ENET_ATCR:
110a699b410SJean-Christophe Dubois         return "ATCR";
111a699b410SJean-Christophe Dubois     case ENET_ATVR:
112a699b410SJean-Christophe Dubois         return "ATVR";
113a699b410SJean-Christophe Dubois     case ENET_ATOFF:
114a699b410SJean-Christophe Dubois         return "ATOFF";
115a699b410SJean-Christophe Dubois     case ENET_ATPER:
116a699b410SJean-Christophe Dubois         return "ATPER";
117a699b410SJean-Christophe Dubois     case ENET_ATCOR:
118a699b410SJean-Christophe Dubois         return "ATCOR";
119a699b410SJean-Christophe Dubois     case ENET_ATINC:
120a699b410SJean-Christophe Dubois         return "ATINC";
121a699b410SJean-Christophe Dubois     case ENET_ATSTMP:
122a699b410SJean-Christophe Dubois         return "ATSTMP";
123a699b410SJean-Christophe Dubois     case ENET_TGSR:
124a699b410SJean-Christophe Dubois         return "TGSR";
125a699b410SJean-Christophe Dubois     case ENET_TCSR0:
126a699b410SJean-Christophe Dubois         return "TCSR0";
127a699b410SJean-Christophe Dubois     case ENET_TCCR0:
128a699b410SJean-Christophe Dubois         return "TCCR0";
129a699b410SJean-Christophe Dubois     case ENET_TCSR1:
130a699b410SJean-Christophe Dubois         return "TCSR1";
131a699b410SJean-Christophe Dubois     case ENET_TCCR1:
132a699b410SJean-Christophe Dubois         return "TCCR1";
133a699b410SJean-Christophe Dubois     case ENET_TCSR2:
134a699b410SJean-Christophe Dubois         return "TCSR2";
135a699b410SJean-Christophe Dubois     case ENET_TCCR2:
136a699b410SJean-Christophe Dubois         return "TCCR2";
137a699b410SJean-Christophe Dubois     case ENET_TCSR3:
138a699b410SJean-Christophe Dubois         return "TCSR3";
139a699b410SJean-Christophe Dubois     case ENET_TCCR3:
140a699b410SJean-Christophe Dubois         return "TCCR3";
141a699b410SJean-Christophe Dubois     default:
142a699b410SJean-Christophe Dubois         return imx_default_reg_name(s, index);
143a699b410SJean-Christophe Dubois     }
144a699b410SJean-Christophe Dubois }
145a699b410SJean-Christophe Dubois 
146a699b410SJean-Christophe Dubois static const char *imx_eth_reg_name(IMXFECState *s, uint32_t index)
147a699b410SJean-Christophe Dubois {
148db0de352SJean-Christophe Dubois     switch (index) {
149db0de352SJean-Christophe Dubois     case ENET_EIR:
150db0de352SJean-Christophe Dubois         return "EIR";
151db0de352SJean-Christophe Dubois     case ENET_EIMR:
152db0de352SJean-Christophe Dubois         return "EIMR";
153db0de352SJean-Christophe Dubois     case ENET_RDAR:
154db0de352SJean-Christophe Dubois         return "RDAR";
155db0de352SJean-Christophe Dubois     case ENET_TDAR:
156db0de352SJean-Christophe Dubois         return "TDAR";
157db0de352SJean-Christophe Dubois     case ENET_ECR:
158db0de352SJean-Christophe Dubois         return "ECR";
159db0de352SJean-Christophe Dubois     case ENET_MMFR:
160db0de352SJean-Christophe Dubois         return "MMFR";
161db0de352SJean-Christophe Dubois     case ENET_MSCR:
162db0de352SJean-Christophe Dubois         return "MSCR";
163db0de352SJean-Christophe Dubois     case ENET_MIBC:
164db0de352SJean-Christophe Dubois         return "MIBC";
165db0de352SJean-Christophe Dubois     case ENET_RCR:
166db0de352SJean-Christophe Dubois         return "RCR";
167db0de352SJean-Christophe Dubois     case ENET_TCR:
168db0de352SJean-Christophe Dubois         return "TCR";
169db0de352SJean-Christophe Dubois     case ENET_PALR:
170db0de352SJean-Christophe Dubois         return "PALR";
171db0de352SJean-Christophe Dubois     case ENET_PAUR:
172db0de352SJean-Christophe Dubois         return "PAUR";
173db0de352SJean-Christophe Dubois     case ENET_OPD:
174db0de352SJean-Christophe Dubois         return "OPD";
175db0de352SJean-Christophe Dubois     case ENET_IAUR:
176db0de352SJean-Christophe Dubois         return "IAUR";
177db0de352SJean-Christophe Dubois     case ENET_IALR:
178db0de352SJean-Christophe Dubois         return "IALR";
179db0de352SJean-Christophe Dubois     case ENET_GAUR:
180db0de352SJean-Christophe Dubois         return "GAUR";
181db0de352SJean-Christophe Dubois     case ENET_GALR:
182db0de352SJean-Christophe Dubois         return "GALR";
183db0de352SJean-Christophe Dubois     case ENET_TFWR:
184db0de352SJean-Christophe Dubois         return "TFWR";
185db0de352SJean-Christophe Dubois     case ENET_RDSR:
186db0de352SJean-Christophe Dubois         return "RDSR";
187db0de352SJean-Christophe Dubois     case ENET_TDSR:
188db0de352SJean-Christophe Dubois         return "TDSR";
189db0de352SJean-Christophe Dubois     case ENET_MRBR:
190db0de352SJean-Christophe Dubois         return "MRBR";
191db0de352SJean-Christophe Dubois     default:
192a699b410SJean-Christophe Dubois         if (s->is_fec) {
193a699b410SJean-Christophe Dubois             return imx_fec_reg_name(s, index);
194a699b410SJean-Christophe Dubois         } else {
195a699b410SJean-Christophe Dubois             return imx_enet_reg_name(s, index);
196a699b410SJean-Christophe Dubois         }
197db0de352SJean-Christophe Dubois     }
198db0de352SJean-Christophe Dubois }
199db0de352SJean-Christophe Dubois 
200f93f961cSAndrey Smirnov /*
201f93f961cSAndrey Smirnov  * Versions of this device with more than one TX descriptor save the
202f93f961cSAndrey Smirnov  * 2nd and 3rd descriptors in a subsection, to maintain migration
203f93f961cSAndrey Smirnov  * compatibility with previous versions of the device that only
204f93f961cSAndrey Smirnov  * supported a single descriptor.
205f93f961cSAndrey Smirnov  */
206f93f961cSAndrey Smirnov static bool imx_eth_is_multi_tx_ring(void *opaque)
207f93f961cSAndrey Smirnov {
208f93f961cSAndrey Smirnov     IMXFECState *s = IMX_FEC(opaque);
209f93f961cSAndrey Smirnov 
210f93f961cSAndrey Smirnov     return s->tx_ring_num > 1;
211f93f961cSAndrey Smirnov }
212f93f961cSAndrey Smirnov 
213f93f961cSAndrey Smirnov static const VMStateDescription vmstate_imx_eth_txdescs = {
214f93f961cSAndrey Smirnov     .name = "imx.fec/txdescs",
215f93f961cSAndrey Smirnov     .version_id = 1,
216f93f961cSAndrey Smirnov     .minimum_version_id = 1,
217f93f961cSAndrey Smirnov     .needed = imx_eth_is_multi_tx_ring,
218f93f961cSAndrey Smirnov     .fields = (VMStateField[]) {
219f93f961cSAndrey Smirnov          VMSTATE_UINT32(tx_descriptor[1], IMXFECState),
220f93f961cSAndrey Smirnov          VMSTATE_UINT32(tx_descriptor[2], IMXFECState),
221f93f961cSAndrey Smirnov          VMSTATE_END_OF_LIST()
222f93f961cSAndrey Smirnov     }
223f93f961cSAndrey Smirnov };
224f93f961cSAndrey Smirnov 
225a699b410SJean-Christophe Dubois static const VMStateDescription vmstate_imx_eth = {
226fcbd8018SJean-Christophe Dubois     .name = TYPE_IMX_FEC,
227db0de352SJean-Christophe Dubois     .version_id = 2,
228db0de352SJean-Christophe Dubois     .minimum_version_id = 2,
229fcbd8018SJean-Christophe Dubois     .fields = (VMStateField[]) {
230db0de352SJean-Christophe Dubois         VMSTATE_UINT32_ARRAY(regs, IMXFECState, ENET_MAX),
231fcbd8018SJean-Christophe Dubois         VMSTATE_UINT32(rx_descriptor, IMXFECState),
232f93f961cSAndrey Smirnov         VMSTATE_UINT32(tx_descriptor[0], IMXFECState),
233fcbd8018SJean-Christophe Dubois         VMSTATE_UINT32(phy_status, IMXFECState),
234fcbd8018SJean-Christophe Dubois         VMSTATE_UINT32(phy_control, IMXFECState),
235fcbd8018SJean-Christophe Dubois         VMSTATE_UINT32(phy_advertise, IMXFECState),
236fcbd8018SJean-Christophe Dubois         VMSTATE_UINT32(phy_int, IMXFECState),
237fcbd8018SJean-Christophe Dubois         VMSTATE_UINT32(phy_int_mask, IMXFECState),
238fcbd8018SJean-Christophe Dubois         VMSTATE_END_OF_LIST()
239f93f961cSAndrey Smirnov     },
240f93f961cSAndrey Smirnov     .subsections = (const VMStateDescription * []) {
241f93f961cSAndrey Smirnov         &vmstate_imx_eth_txdescs,
242f93f961cSAndrey Smirnov         NULL
243f93f961cSAndrey Smirnov     },
244fcbd8018SJean-Christophe Dubois };
245fcbd8018SJean-Christophe Dubois 
246fcbd8018SJean-Christophe Dubois #define PHY_INT_ENERGYON            (1 << 7)
247fcbd8018SJean-Christophe Dubois #define PHY_INT_AUTONEG_COMPLETE    (1 << 6)
248fcbd8018SJean-Christophe Dubois #define PHY_INT_FAULT               (1 << 5)
249fcbd8018SJean-Christophe Dubois #define PHY_INT_DOWN                (1 << 4)
250fcbd8018SJean-Christophe Dubois #define PHY_INT_AUTONEG_LP          (1 << 3)
251fcbd8018SJean-Christophe Dubois #define PHY_INT_PARFAULT            (1 << 2)
252fcbd8018SJean-Christophe Dubois #define PHY_INT_AUTONEG_PAGE        (1 << 1)
253fcbd8018SJean-Christophe Dubois 
254a699b410SJean-Christophe Dubois static void imx_eth_update(IMXFECState *s);
255fcbd8018SJean-Christophe Dubois 
256fcbd8018SJean-Christophe Dubois /*
257fcbd8018SJean-Christophe Dubois  * The MII phy could raise a GPIO to the processor which in turn
258fcbd8018SJean-Christophe Dubois  * could be handled as an interrpt by the OS.
259fcbd8018SJean-Christophe Dubois  * For now we don't handle any GPIO/interrupt line, so the OS will
260fcbd8018SJean-Christophe Dubois  * have to poll for the PHY status.
261fcbd8018SJean-Christophe Dubois  */
262fcbd8018SJean-Christophe Dubois static void phy_update_irq(IMXFECState *s)
263fcbd8018SJean-Christophe Dubois {
264a699b410SJean-Christophe Dubois     imx_eth_update(s);
265fcbd8018SJean-Christophe Dubois }
266fcbd8018SJean-Christophe Dubois 
267fcbd8018SJean-Christophe Dubois static void phy_update_link(IMXFECState *s)
268fcbd8018SJean-Christophe Dubois {
269fcbd8018SJean-Christophe Dubois     /* Autonegotiation status mirrors link status.  */
270fcbd8018SJean-Christophe Dubois     if (qemu_get_queue(s->nic)->link_down) {
271fcbd8018SJean-Christophe Dubois         PHY_PRINTF("link is down\n");
272fcbd8018SJean-Christophe Dubois         s->phy_status &= ~0x0024;
273fcbd8018SJean-Christophe Dubois         s->phy_int |= PHY_INT_DOWN;
274fcbd8018SJean-Christophe Dubois     } else {
275fcbd8018SJean-Christophe Dubois         PHY_PRINTF("link is up\n");
276fcbd8018SJean-Christophe Dubois         s->phy_status |= 0x0024;
277fcbd8018SJean-Christophe Dubois         s->phy_int |= PHY_INT_ENERGYON;
278fcbd8018SJean-Christophe Dubois         s->phy_int |= PHY_INT_AUTONEG_COMPLETE;
279fcbd8018SJean-Christophe Dubois     }
280fcbd8018SJean-Christophe Dubois     phy_update_irq(s);
281fcbd8018SJean-Christophe Dubois }
282fcbd8018SJean-Christophe Dubois 
283a699b410SJean-Christophe Dubois static void imx_eth_set_link(NetClientState *nc)
284fcbd8018SJean-Christophe Dubois {
285fcbd8018SJean-Christophe Dubois     phy_update_link(IMX_FEC(qemu_get_nic_opaque(nc)));
286fcbd8018SJean-Christophe Dubois }
287fcbd8018SJean-Christophe Dubois 
288fcbd8018SJean-Christophe Dubois static void phy_reset(IMXFECState *s)
289fcbd8018SJean-Christophe Dubois {
290fcbd8018SJean-Christophe Dubois     s->phy_status = 0x7809;
291fcbd8018SJean-Christophe Dubois     s->phy_control = 0x3000;
292fcbd8018SJean-Christophe Dubois     s->phy_advertise = 0x01e1;
293fcbd8018SJean-Christophe Dubois     s->phy_int_mask = 0;
294fcbd8018SJean-Christophe Dubois     s->phy_int = 0;
295fcbd8018SJean-Christophe Dubois     phy_update_link(s);
296fcbd8018SJean-Christophe Dubois }
297fcbd8018SJean-Christophe Dubois 
298fcbd8018SJean-Christophe Dubois static uint32_t do_phy_read(IMXFECState *s, int reg)
299fcbd8018SJean-Christophe Dubois {
300fcbd8018SJean-Christophe Dubois     uint32_t val;
301fcbd8018SJean-Christophe Dubois 
302fcbd8018SJean-Christophe Dubois     if (reg > 31) {
303fcbd8018SJean-Christophe Dubois         /* we only advertise one phy */
304fcbd8018SJean-Christophe Dubois         return 0;
305fcbd8018SJean-Christophe Dubois     }
306fcbd8018SJean-Christophe Dubois 
307fcbd8018SJean-Christophe Dubois     switch (reg) {
308fcbd8018SJean-Christophe Dubois     case 0:     /* Basic Control */
309fcbd8018SJean-Christophe Dubois         val = s->phy_control;
310fcbd8018SJean-Christophe Dubois         break;
311fcbd8018SJean-Christophe Dubois     case 1:     /* Basic Status */
312fcbd8018SJean-Christophe Dubois         val = s->phy_status;
313fcbd8018SJean-Christophe Dubois         break;
314fcbd8018SJean-Christophe Dubois     case 2:     /* ID1 */
315fcbd8018SJean-Christophe Dubois         val = 0x0007;
316fcbd8018SJean-Christophe Dubois         break;
317fcbd8018SJean-Christophe Dubois     case 3:     /* ID2 */
318fcbd8018SJean-Christophe Dubois         val = 0xc0d1;
319fcbd8018SJean-Christophe Dubois         break;
320fcbd8018SJean-Christophe Dubois     case 4:     /* Auto-neg advertisement */
321fcbd8018SJean-Christophe Dubois         val = s->phy_advertise;
322fcbd8018SJean-Christophe Dubois         break;
323fcbd8018SJean-Christophe Dubois     case 5:     /* Auto-neg Link Partner Ability */
324fcbd8018SJean-Christophe Dubois         val = 0x0f71;
325fcbd8018SJean-Christophe Dubois         break;
326fcbd8018SJean-Christophe Dubois     case 6:     /* Auto-neg Expansion */
327fcbd8018SJean-Christophe Dubois         val = 1;
328fcbd8018SJean-Christophe Dubois         break;
329fcbd8018SJean-Christophe Dubois     case 29:    /* Interrupt source.  */
330fcbd8018SJean-Christophe Dubois         val = s->phy_int;
331fcbd8018SJean-Christophe Dubois         s->phy_int = 0;
332fcbd8018SJean-Christophe Dubois         phy_update_irq(s);
333fcbd8018SJean-Christophe Dubois         break;
334fcbd8018SJean-Christophe Dubois     case 30:    /* Interrupt mask */
335fcbd8018SJean-Christophe Dubois         val = s->phy_int_mask;
336fcbd8018SJean-Christophe Dubois         break;
337fcbd8018SJean-Christophe Dubois     case 17:
338fcbd8018SJean-Christophe Dubois     case 18:
339fcbd8018SJean-Christophe Dubois     case 27:
340fcbd8018SJean-Christophe Dubois     case 31:
341b72d8d25SJean-Christophe Dubois         qemu_log_mask(LOG_UNIMP, "[%s.phy]%s: reg %d not implemented\n",
342fcbd8018SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__, reg);
343fcbd8018SJean-Christophe Dubois         val = 0;
344fcbd8018SJean-Christophe Dubois         break;
345fcbd8018SJean-Christophe Dubois     default:
346b72d8d25SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s.phy]%s: Bad address at offset %d\n",
347fcbd8018SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__, reg);
348fcbd8018SJean-Christophe Dubois         val = 0;
349fcbd8018SJean-Christophe Dubois         break;
350fcbd8018SJean-Christophe Dubois     }
351fcbd8018SJean-Christophe Dubois 
352fcbd8018SJean-Christophe Dubois     PHY_PRINTF("read 0x%04x @ %d\n", val, reg);
353fcbd8018SJean-Christophe Dubois 
354fcbd8018SJean-Christophe Dubois     return val;
355fcbd8018SJean-Christophe Dubois }
356fcbd8018SJean-Christophe Dubois 
357fcbd8018SJean-Christophe Dubois static void do_phy_write(IMXFECState *s, int reg, uint32_t val)
358fcbd8018SJean-Christophe Dubois {
359fcbd8018SJean-Christophe Dubois     PHY_PRINTF("write 0x%04x @ %d\n", val, reg);
360fcbd8018SJean-Christophe Dubois 
361fcbd8018SJean-Christophe Dubois     if (reg > 31) {
362fcbd8018SJean-Christophe Dubois         /* we only advertise one phy */
363fcbd8018SJean-Christophe Dubois         return;
364fcbd8018SJean-Christophe Dubois     }
365fcbd8018SJean-Christophe Dubois 
366fcbd8018SJean-Christophe Dubois     switch (reg) {
367fcbd8018SJean-Christophe Dubois     case 0:     /* Basic Control */
368fcbd8018SJean-Christophe Dubois         if (val & 0x8000) {
369fcbd8018SJean-Christophe Dubois             phy_reset(s);
370fcbd8018SJean-Christophe Dubois         } else {
371fcbd8018SJean-Christophe Dubois             s->phy_control = val & 0x7980;
372fcbd8018SJean-Christophe Dubois             /* Complete autonegotiation immediately.  */
373fcbd8018SJean-Christophe Dubois             if (val & 0x1000) {
374fcbd8018SJean-Christophe Dubois                 s->phy_status |= 0x0020;
375fcbd8018SJean-Christophe Dubois             }
376fcbd8018SJean-Christophe Dubois         }
377fcbd8018SJean-Christophe Dubois         break;
378fcbd8018SJean-Christophe Dubois     case 4:     /* Auto-neg advertisement */
379fcbd8018SJean-Christophe Dubois         s->phy_advertise = (val & 0x2d7f) | 0x80;
380fcbd8018SJean-Christophe Dubois         break;
381fcbd8018SJean-Christophe Dubois     case 30:    /* Interrupt mask */
382fcbd8018SJean-Christophe Dubois         s->phy_int_mask = val & 0xff;
383fcbd8018SJean-Christophe Dubois         phy_update_irq(s);
384fcbd8018SJean-Christophe Dubois         break;
385fcbd8018SJean-Christophe Dubois     case 17:
386fcbd8018SJean-Christophe Dubois     case 18:
387fcbd8018SJean-Christophe Dubois     case 27:
388fcbd8018SJean-Christophe Dubois     case 31:
389b72d8d25SJean-Christophe Dubois         qemu_log_mask(LOG_UNIMP, "[%s.phy)%s: reg %d not implemented\n",
390fcbd8018SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__, reg);
391fcbd8018SJean-Christophe Dubois         break;
392fcbd8018SJean-Christophe Dubois     default:
393b72d8d25SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s.phy]%s: Bad address at offset %d\n",
394fcbd8018SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__, reg);
395fcbd8018SJean-Christophe Dubois         break;
396fcbd8018SJean-Christophe Dubois     }
397fcbd8018SJean-Christophe Dubois }
398fcbd8018SJean-Christophe Dubois 
399fcbd8018SJean-Christophe Dubois static void imx_fec_read_bd(IMXFECBufDesc *bd, dma_addr_t addr)
400fcbd8018SJean-Christophe Dubois {
401fcbd8018SJean-Christophe Dubois     dma_memory_read(&address_space_memory, addr, bd, sizeof(*bd));
402fcbd8018SJean-Christophe Dubois }
403fcbd8018SJean-Christophe Dubois 
404fcbd8018SJean-Christophe Dubois static void imx_fec_write_bd(IMXFECBufDesc *bd, dma_addr_t addr)
405fcbd8018SJean-Christophe Dubois {
406fcbd8018SJean-Christophe Dubois     dma_memory_write(&address_space_memory, addr, bd, sizeof(*bd));
407fcbd8018SJean-Christophe Dubois }
408fcbd8018SJean-Christophe Dubois 
409a699b410SJean-Christophe Dubois static void imx_enet_read_bd(IMXENETBufDesc *bd, dma_addr_t addr)
410fcbd8018SJean-Christophe Dubois {
411a699b410SJean-Christophe Dubois     dma_memory_read(&address_space_memory, addr, bd, sizeof(*bd));
412a699b410SJean-Christophe Dubois }
413a699b410SJean-Christophe Dubois 
414a699b410SJean-Christophe Dubois static void imx_enet_write_bd(IMXENETBufDesc *bd, dma_addr_t addr)
415a699b410SJean-Christophe Dubois {
416a699b410SJean-Christophe Dubois     dma_memory_write(&address_space_memory, addr, bd, sizeof(*bd));
417a699b410SJean-Christophe Dubois }
418a699b410SJean-Christophe Dubois 
419a699b410SJean-Christophe Dubois static void imx_eth_update(IMXFECState *s)
420a699b410SJean-Christophe Dubois {
4216461d7e2SGuenter Roeck     /*
4226461d7e2SGuenter Roeck      * Previous versions of qemu had the ENET_INT_MAC and ENET_INT_TS_TIMER
4236461d7e2SGuenter Roeck      * interrupts swapped. This worked with older versions of Linux (4.14
4246461d7e2SGuenter Roeck      * and older) since Linux associated both interrupt lines with Ethernet
4256461d7e2SGuenter Roeck      * MAC interrupts. Specifically,
4266461d7e2SGuenter Roeck      * - Linux 4.15 and later have separate interrupt handlers for the MAC and
4276461d7e2SGuenter Roeck      *   timer interrupts. Those versions of Linux fail with versions of QEMU
4286461d7e2SGuenter Roeck      *   with swapped interrupt assignments.
4296461d7e2SGuenter Roeck      * - In linux 4.14, both interrupt lines were registered with the Ethernet
4306461d7e2SGuenter Roeck      *   MAC interrupt handler. As a result, all versions of qemu happen to
4316461d7e2SGuenter Roeck      *   work, though that is accidental.
4326461d7e2SGuenter Roeck      * - In Linux 4.9 and older, the timer interrupt was registered directly
4336461d7e2SGuenter Roeck      *   with the Ethernet MAC interrupt handler. The MAC interrupt was
4346461d7e2SGuenter Roeck      *   redirected to a GPIO interrupt to work around erratum ERR006687.
4356461d7e2SGuenter Roeck      *   This was implemented using the SOC's IOMUX block. In qemu, this GPIO
4366461d7e2SGuenter Roeck      *   interrupt never fired since IOMUX is currently not supported in qemu.
4376461d7e2SGuenter Roeck      *   Linux instead received MAC interrupts on the timer interrupt.
4386461d7e2SGuenter Roeck      *   As a result, qemu versions with the swapped interrupt assignment work,
4396461d7e2SGuenter Roeck      *   albeit accidentally, but qemu versions with the correct interrupt
4406461d7e2SGuenter Roeck      *   assignment fail.
4416461d7e2SGuenter Roeck      *
4426461d7e2SGuenter Roeck      * To ensure that all versions of Linux work, generate ENET_INT_MAC
4436461d7e2SGuenter Roeck      * interrrupts on both interrupt lines. This should be changed if and when
4446461d7e2SGuenter Roeck      * qemu supports IOMUX.
4456461d7e2SGuenter Roeck      */
4466461d7e2SGuenter Roeck     if (s->regs[ENET_EIR] & s->regs[ENET_EIMR] &
4476461d7e2SGuenter Roeck         (ENET_INT_MAC | ENET_INT_TS_TIMER)) {
448a699b410SJean-Christophe Dubois         qemu_set_irq(s->irq[1], 1);
449db0de352SJean-Christophe Dubois     } else {
450a699b410SJean-Christophe Dubois         qemu_set_irq(s->irq[1], 0);
451a699b410SJean-Christophe Dubois     }
452a699b410SJean-Christophe Dubois 
453a699b410SJean-Christophe Dubois     if (s->regs[ENET_EIR] & s->regs[ENET_EIMR] & ENET_INT_MAC) {
454a699b410SJean-Christophe Dubois         qemu_set_irq(s->irq[0], 1);
455a699b410SJean-Christophe Dubois     } else {
456a699b410SJean-Christophe Dubois         qemu_set_irq(s->irq[0], 0);
457fcbd8018SJean-Christophe Dubois     }
458fcbd8018SJean-Christophe Dubois }
459fcbd8018SJean-Christophe Dubois 
460fcbd8018SJean-Christophe Dubois static void imx_fec_do_tx(IMXFECState *s)
461fcbd8018SJean-Christophe Dubois {
46281f17e0dSPrasad J Pandit     int frame_size = 0, descnt = 0;
4637bac20dcSAndrey Smirnov     uint8_t *ptr = s->frame;
464f93f961cSAndrey Smirnov     uint32_t addr = s->tx_descriptor[0];
465fcbd8018SJean-Christophe Dubois 
46681f17e0dSPrasad J Pandit     while (descnt++ < IMX_MAX_DESC) {
467fcbd8018SJean-Christophe Dubois         IMXFECBufDesc bd;
468fcbd8018SJean-Christophe Dubois         int len;
469fcbd8018SJean-Christophe Dubois 
470fcbd8018SJean-Christophe Dubois         imx_fec_read_bd(&bd, addr);
471fcbd8018SJean-Christophe Dubois         FEC_PRINTF("tx_bd %x flags %04x len %d data %08x\n",
472fcbd8018SJean-Christophe Dubois                    addr, bd.flags, bd.length, bd.data);
4731bb3c371SJean-Christophe Dubois         if ((bd.flags & ENET_BD_R) == 0) {
474fcbd8018SJean-Christophe Dubois             /* Run out of descriptors to transmit.  */
475a699b410SJean-Christophe Dubois             FEC_PRINTF("tx_bd ran out of descriptors to transmit\n");
476fcbd8018SJean-Christophe Dubois             break;
477fcbd8018SJean-Christophe Dubois         }
478fcbd8018SJean-Christophe Dubois         len = bd.length;
4791bb3c371SJean-Christophe Dubois         if (frame_size + len > ENET_MAX_FRAME_SIZE) {
4801bb3c371SJean-Christophe Dubois             len = ENET_MAX_FRAME_SIZE - frame_size;
481db0de352SJean-Christophe Dubois             s->regs[ENET_EIR] |= ENET_INT_BABT;
482fcbd8018SJean-Christophe Dubois         }
483fcbd8018SJean-Christophe Dubois         dma_memory_read(&address_space_memory, bd.data, ptr, len);
484fcbd8018SJean-Christophe Dubois         ptr += len;
485fcbd8018SJean-Christophe Dubois         frame_size += len;
4861bb3c371SJean-Christophe Dubois         if (bd.flags & ENET_BD_L) {
487fcbd8018SJean-Christophe Dubois             /* Last buffer in frame.  */
4887bac20dcSAndrey Smirnov             qemu_send_packet(qemu_get_queue(s->nic), s->frame, frame_size);
4897bac20dcSAndrey Smirnov             ptr = s->frame;
490fcbd8018SJean-Christophe Dubois             frame_size = 0;
491db0de352SJean-Christophe Dubois             s->regs[ENET_EIR] |= ENET_INT_TXF;
492fcbd8018SJean-Christophe Dubois         }
493db0de352SJean-Christophe Dubois         s->regs[ENET_EIR] |= ENET_INT_TXB;
4941bb3c371SJean-Christophe Dubois         bd.flags &= ~ENET_BD_R;
495fcbd8018SJean-Christophe Dubois         /* Write back the modified descriptor.  */
496fcbd8018SJean-Christophe Dubois         imx_fec_write_bd(&bd, addr);
497fcbd8018SJean-Christophe Dubois         /* Advance to the next descriptor.  */
4981bb3c371SJean-Christophe Dubois         if ((bd.flags & ENET_BD_W) != 0) {
499db0de352SJean-Christophe Dubois             addr = s->regs[ENET_TDSR];
500fcbd8018SJean-Christophe Dubois         } else {
501db0de352SJean-Christophe Dubois             addr += sizeof(bd);
502fcbd8018SJean-Christophe Dubois         }
503fcbd8018SJean-Christophe Dubois     }
504fcbd8018SJean-Christophe Dubois 
505f93f961cSAndrey Smirnov     s->tx_descriptor[0] = addr;
506fcbd8018SJean-Christophe Dubois 
507a699b410SJean-Christophe Dubois     imx_eth_update(s);
508fcbd8018SJean-Christophe Dubois }
509fcbd8018SJean-Christophe Dubois 
510f93f961cSAndrey Smirnov static void imx_enet_do_tx(IMXFECState *s, uint32_t index)
511a699b410SJean-Christophe Dubois {
51281f17e0dSPrasad J Pandit     int frame_size = 0, descnt = 0;
513f93f961cSAndrey Smirnov 
5147bac20dcSAndrey Smirnov     uint8_t *ptr = s->frame;
515f93f961cSAndrey Smirnov     uint32_t addr, int_txb, int_txf, tdsr;
516f93f961cSAndrey Smirnov     size_t ring;
517f93f961cSAndrey Smirnov 
518f93f961cSAndrey Smirnov     switch (index) {
519f93f961cSAndrey Smirnov     case ENET_TDAR:
520f93f961cSAndrey Smirnov         ring    = 0;
521f93f961cSAndrey Smirnov         int_txb = ENET_INT_TXB;
522f93f961cSAndrey Smirnov         int_txf = ENET_INT_TXF;
523f93f961cSAndrey Smirnov         tdsr    = ENET_TDSR;
524f93f961cSAndrey Smirnov         break;
525f93f961cSAndrey Smirnov     case ENET_TDAR1:
526f93f961cSAndrey Smirnov         ring    = 1;
527f93f961cSAndrey Smirnov         int_txb = ENET_INT_TXB1;
528f93f961cSAndrey Smirnov         int_txf = ENET_INT_TXF1;
529f93f961cSAndrey Smirnov         tdsr    = ENET_TDSR1;
530f93f961cSAndrey Smirnov         break;
531f93f961cSAndrey Smirnov     case ENET_TDAR2:
532f93f961cSAndrey Smirnov         ring    = 2;
533f93f961cSAndrey Smirnov         int_txb = ENET_INT_TXB2;
534f93f961cSAndrey Smirnov         int_txf = ENET_INT_TXF2;
535f93f961cSAndrey Smirnov         tdsr    = ENET_TDSR2;
536f93f961cSAndrey Smirnov         break;
537f93f961cSAndrey Smirnov     default:
538f93f961cSAndrey Smirnov         qemu_log_mask(LOG_GUEST_ERROR,
539f93f961cSAndrey Smirnov                       "%s: bogus value for index %x\n",
540f93f961cSAndrey Smirnov                       __func__, index);
541f93f961cSAndrey Smirnov         abort();
542f93f961cSAndrey Smirnov         break;
543f93f961cSAndrey Smirnov     }
544f93f961cSAndrey Smirnov 
545f93f961cSAndrey Smirnov     addr = s->tx_descriptor[ring];
546a699b410SJean-Christophe Dubois 
54781f17e0dSPrasad J Pandit     while (descnt++ < IMX_MAX_DESC) {
548a699b410SJean-Christophe Dubois         IMXENETBufDesc bd;
549a699b410SJean-Christophe Dubois         int len;
550a699b410SJean-Christophe Dubois 
551a699b410SJean-Christophe Dubois         imx_enet_read_bd(&bd, addr);
552a699b410SJean-Christophe Dubois         FEC_PRINTF("tx_bd %x flags %04x len %d data %08x option %04x "
553a699b410SJean-Christophe Dubois                    "status %04x\n", addr, bd.flags, bd.length, bd.data,
554a699b410SJean-Christophe Dubois                    bd.option, bd.status);
555a699b410SJean-Christophe Dubois         if ((bd.flags & ENET_BD_R) == 0) {
556a699b410SJean-Christophe Dubois             /* Run out of descriptors to transmit.  */
557a699b410SJean-Christophe Dubois             break;
558a699b410SJean-Christophe Dubois         }
559a699b410SJean-Christophe Dubois         len = bd.length;
560a699b410SJean-Christophe Dubois         if (frame_size + len > ENET_MAX_FRAME_SIZE) {
561a699b410SJean-Christophe Dubois             len = ENET_MAX_FRAME_SIZE - frame_size;
562a699b410SJean-Christophe Dubois             s->regs[ENET_EIR] |= ENET_INT_BABT;
563a699b410SJean-Christophe Dubois         }
564a699b410SJean-Christophe Dubois         dma_memory_read(&address_space_memory, bd.data, ptr, len);
565a699b410SJean-Christophe Dubois         ptr += len;
566a699b410SJean-Christophe Dubois         frame_size += len;
567a699b410SJean-Christophe Dubois         if (bd.flags & ENET_BD_L) {
568a699b410SJean-Christophe Dubois             if (bd.option & ENET_BD_PINS) {
5697bac20dcSAndrey Smirnov                 struct ip_header *ip_hd = PKT_GET_IP_HDR(s->frame);
570a699b410SJean-Christophe Dubois                 if (IP_HEADER_VERSION(ip_hd) == 4) {
5717bac20dcSAndrey Smirnov                     net_checksum_calculate(s->frame, frame_size);
572a699b410SJean-Christophe Dubois                 }
573a699b410SJean-Christophe Dubois             }
574a699b410SJean-Christophe Dubois             if (bd.option & ENET_BD_IINS) {
5757bac20dcSAndrey Smirnov                 struct ip_header *ip_hd = PKT_GET_IP_HDR(s->frame);
576a699b410SJean-Christophe Dubois                 /* We compute checksum only for IPv4 frames */
577a699b410SJean-Christophe Dubois                 if (IP_HEADER_VERSION(ip_hd) == 4) {
578a699b410SJean-Christophe Dubois                     uint16_t csum;
579a699b410SJean-Christophe Dubois                     ip_hd->ip_sum = 0;
580a699b410SJean-Christophe Dubois                     csum = net_raw_checksum((uint8_t *)ip_hd, sizeof(*ip_hd));
581a699b410SJean-Christophe Dubois                     ip_hd->ip_sum = cpu_to_be16(csum);
582a699b410SJean-Christophe Dubois                 }
583a699b410SJean-Christophe Dubois             }
584a699b410SJean-Christophe Dubois             /* Last buffer in frame.  */
5857bac20dcSAndrey Smirnov 
58652cfd584SAndrey Smirnov             qemu_send_packet(qemu_get_queue(s->nic), s->frame, frame_size);
5877bac20dcSAndrey Smirnov             ptr = s->frame;
5887bac20dcSAndrey Smirnov 
589a699b410SJean-Christophe Dubois             frame_size = 0;
590a699b410SJean-Christophe Dubois             if (bd.option & ENET_BD_TX_INT) {
591f93f961cSAndrey Smirnov                 s->regs[ENET_EIR] |= int_txf;
592a699b410SJean-Christophe Dubois             }
593*88e1b59eSAaron Hill             /* Indicate that we've updated the last buffer descriptor. */
594*88e1b59eSAaron Hill             bd.last_buffer = ENET_BD_BDU;
595a699b410SJean-Christophe Dubois         }
596a699b410SJean-Christophe Dubois         if (bd.option & ENET_BD_TX_INT) {
597f93f961cSAndrey Smirnov             s->regs[ENET_EIR] |= int_txb;
598a699b410SJean-Christophe Dubois         }
599a699b410SJean-Christophe Dubois         bd.flags &= ~ENET_BD_R;
600a699b410SJean-Christophe Dubois         /* Write back the modified descriptor.  */
601a699b410SJean-Christophe Dubois         imx_enet_write_bd(&bd, addr);
602a699b410SJean-Christophe Dubois         /* Advance to the next descriptor.  */
603a699b410SJean-Christophe Dubois         if ((bd.flags & ENET_BD_W) != 0) {
604f93f961cSAndrey Smirnov             addr = s->regs[tdsr];
605a699b410SJean-Christophe Dubois         } else {
606a699b410SJean-Christophe Dubois             addr += sizeof(bd);
607a699b410SJean-Christophe Dubois         }
608a699b410SJean-Christophe Dubois     }
609a699b410SJean-Christophe Dubois 
610f93f961cSAndrey Smirnov     s->tx_descriptor[ring] = addr;
611a699b410SJean-Christophe Dubois 
612a699b410SJean-Christophe Dubois     imx_eth_update(s);
613a699b410SJean-Christophe Dubois }
614a699b410SJean-Christophe Dubois 
615f93f961cSAndrey Smirnov static void imx_eth_do_tx(IMXFECState *s, uint32_t index)
616a699b410SJean-Christophe Dubois {
617a699b410SJean-Christophe Dubois     if (!s->is_fec && (s->regs[ENET_ECR] & ENET_ECR_EN1588)) {
618f93f961cSAndrey Smirnov         imx_enet_do_tx(s, index);
619a699b410SJean-Christophe Dubois     } else {
620a699b410SJean-Christophe Dubois         imx_fec_do_tx(s);
621a699b410SJean-Christophe Dubois     }
622a699b410SJean-Christophe Dubois }
623a699b410SJean-Christophe Dubois 
624b2b012afSAndrey Smirnov static void imx_eth_enable_rx(IMXFECState *s, bool flush)
625fcbd8018SJean-Christophe Dubois {
626fcbd8018SJean-Christophe Dubois     IMXFECBufDesc bd;
627fcbd8018SJean-Christophe Dubois 
628fcbd8018SJean-Christophe Dubois     imx_fec_read_bd(&bd, s->rx_descriptor);
629fcbd8018SJean-Christophe Dubois 
6301b58d58fSJean-Christophe Dubois     s->regs[ENET_RDAR] = (bd.flags & ENET_BD_E) ? ENET_RDAR_RDAR : 0;
631fcbd8018SJean-Christophe Dubois 
6321b58d58fSJean-Christophe Dubois     if (!s->regs[ENET_RDAR]) {
633fcbd8018SJean-Christophe Dubois         FEC_PRINTF("RX buffer full\n");
634b2b012afSAndrey Smirnov     } else if (flush) {
635fcbd8018SJean-Christophe Dubois         qemu_flush_queued_packets(qemu_get_queue(s->nic));
636fcbd8018SJean-Christophe Dubois     }
637fcbd8018SJean-Christophe Dubois }
638fcbd8018SJean-Christophe Dubois 
639a699b410SJean-Christophe Dubois static void imx_eth_reset(DeviceState *d)
640fcbd8018SJean-Christophe Dubois {
641fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(d);
642fcbd8018SJean-Christophe Dubois 
643a699b410SJean-Christophe Dubois     /* Reset the Device */
644db0de352SJean-Christophe Dubois     memset(s->regs, 0, sizeof(s->regs));
645db0de352SJean-Christophe Dubois     s->regs[ENET_ECR]   = 0xf0000000;
646db0de352SJean-Christophe Dubois     s->regs[ENET_MIBC]  = 0xc0000000;
647db0de352SJean-Christophe Dubois     s->regs[ENET_RCR]   = 0x05ee0001;
648db0de352SJean-Christophe Dubois     s->regs[ENET_OPD]   = 0x00010000;
649db0de352SJean-Christophe Dubois 
650db0de352SJean-Christophe Dubois     s->regs[ENET_PALR]  = (s->conf.macaddr.a[0] << 24)
651db0de352SJean-Christophe Dubois                           | (s->conf.macaddr.a[1] << 16)
652db0de352SJean-Christophe Dubois                           | (s->conf.macaddr.a[2] << 8)
653db0de352SJean-Christophe Dubois                           | s->conf.macaddr.a[3];
654db0de352SJean-Christophe Dubois     s->regs[ENET_PAUR]  = (s->conf.macaddr.a[4] << 24)
655db0de352SJean-Christophe Dubois                           | (s->conf.macaddr.a[5] << 16)
656db0de352SJean-Christophe Dubois                           | 0x8808;
657db0de352SJean-Christophe Dubois 
658a699b410SJean-Christophe Dubois     if (s->is_fec) {
659db0de352SJean-Christophe Dubois         s->regs[ENET_FRBR]  = 0x00000600;
660db0de352SJean-Christophe Dubois         s->regs[ENET_FRSR]  = 0x00000500;
661db0de352SJean-Christophe Dubois         s->regs[ENET_MIIGSK_ENR]  = 0x00000006;
662a699b410SJean-Christophe Dubois     } else {
663a699b410SJean-Christophe Dubois         s->regs[ENET_RAEM]  = 0x00000004;
664a699b410SJean-Christophe Dubois         s->regs[ENET_RAFL]  = 0x00000004;
665a699b410SJean-Christophe Dubois         s->regs[ENET_TAEM]  = 0x00000004;
666a699b410SJean-Christophe Dubois         s->regs[ENET_TAFL]  = 0x00000008;
667a699b410SJean-Christophe Dubois         s->regs[ENET_TIPG]  = 0x0000000c;
668a699b410SJean-Christophe Dubois         s->regs[ENET_FTRL]  = 0x000007ff;
669a699b410SJean-Christophe Dubois         s->regs[ENET_ATPER] = 0x3b9aca00;
670a699b410SJean-Christophe Dubois     }
671db0de352SJean-Christophe Dubois 
672db0de352SJean-Christophe Dubois     s->rx_descriptor = 0;
673f93f961cSAndrey Smirnov     memset(s->tx_descriptor, 0, sizeof(s->tx_descriptor));
674fcbd8018SJean-Christophe Dubois 
675fcbd8018SJean-Christophe Dubois     /* We also reset the PHY */
676fcbd8018SJean-Christophe Dubois     phy_reset(s);
677fcbd8018SJean-Christophe Dubois }
678fcbd8018SJean-Christophe Dubois 
679a699b410SJean-Christophe Dubois static uint32_t imx_default_read(IMXFECState *s, uint32_t index)
680a699b410SJean-Christophe Dubois {
681a699b410SJean-Christophe Dubois     qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
682a699b410SJean-Christophe Dubois                   PRIx32 "\n", TYPE_IMX_FEC, __func__, index * 4);
683a699b410SJean-Christophe Dubois     return 0;
684a699b410SJean-Christophe Dubois }
685a699b410SJean-Christophe Dubois 
686a699b410SJean-Christophe Dubois static uint32_t imx_fec_read(IMXFECState *s, uint32_t index)
687a699b410SJean-Christophe Dubois {
688a699b410SJean-Christophe Dubois     switch (index) {
689a699b410SJean-Christophe Dubois     case ENET_FRBR:
690a699b410SJean-Christophe Dubois     case ENET_FRSR:
691a699b410SJean-Christophe Dubois     case ENET_MIIGSK_CFGR:
692a699b410SJean-Christophe Dubois     case ENET_MIIGSK_ENR:
693a699b410SJean-Christophe Dubois         return s->regs[index];
694a699b410SJean-Christophe Dubois     default:
695a699b410SJean-Christophe Dubois         return imx_default_read(s, index);
696a699b410SJean-Christophe Dubois     }
697a699b410SJean-Christophe Dubois }
698a699b410SJean-Christophe Dubois 
699a699b410SJean-Christophe Dubois static uint32_t imx_enet_read(IMXFECState *s, uint32_t index)
700a699b410SJean-Christophe Dubois {
701a699b410SJean-Christophe Dubois     switch (index) {
702a699b410SJean-Christophe Dubois     case ENET_RSFL:
703a699b410SJean-Christophe Dubois     case ENET_RSEM:
704a699b410SJean-Christophe Dubois     case ENET_RAEM:
705a699b410SJean-Christophe Dubois     case ENET_RAFL:
706a699b410SJean-Christophe Dubois     case ENET_TSEM:
707a699b410SJean-Christophe Dubois     case ENET_TAEM:
708a699b410SJean-Christophe Dubois     case ENET_TAFL:
709a699b410SJean-Christophe Dubois     case ENET_TIPG:
710a699b410SJean-Christophe Dubois     case ENET_FTRL:
711a699b410SJean-Christophe Dubois     case ENET_TACC:
712a699b410SJean-Christophe Dubois     case ENET_RACC:
713a699b410SJean-Christophe Dubois     case ENET_ATCR:
714a699b410SJean-Christophe Dubois     case ENET_ATVR:
715a699b410SJean-Christophe Dubois     case ENET_ATOFF:
716a699b410SJean-Christophe Dubois     case ENET_ATPER:
717a699b410SJean-Christophe Dubois     case ENET_ATCOR:
718a699b410SJean-Christophe Dubois     case ENET_ATINC:
719a699b410SJean-Christophe Dubois     case ENET_ATSTMP:
720a699b410SJean-Christophe Dubois     case ENET_TGSR:
721a699b410SJean-Christophe Dubois     case ENET_TCSR0:
722a699b410SJean-Christophe Dubois     case ENET_TCCR0:
723a699b410SJean-Christophe Dubois     case ENET_TCSR1:
724a699b410SJean-Christophe Dubois     case ENET_TCCR1:
725a699b410SJean-Christophe Dubois     case ENET_TCSR2:
726a699b410SJean-Christophe Dubois     case ENET_TCCR2:
727a699b410SJean-Christophe Dubois     case ENET_TCSR3:
728a699b410SJean-Christophe Dubois     case ENET_TCCR3:
729a699b410SJean-Christophe Dubois         return s->regs[index];
730a699b410SJean-Christophe Dubois     default:
731a699b410SJean-Christophe Dubois         return imx_default_read(s, index);
732a699b410SJean-Christophe Dubois     }
733a699b410SJean-Christophe Dubois }
734a699b410SJean-Christophe Dubois 
735a699b410SJean-Christophe Dubois static uint64_t imx_eth_read(void *opaque, hwaddr offset, unsigned size)
736fcbd8018SJean-Christophe Dubois {
737db0de352SJean-Christophe Dubois     uint32_t value = 0;
738fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(opaque);
739a699b410SJean-Christophe Dubois     uint32_t index = offset >> 2;
740fcbd8018SJean-Christophe Dubois 
741db0de352SJean-Christophe Dubois     switch (index) {
742db0de352SJean-Christophe Dubois     case ENET_EIR:
743db0de352SJean-Christophe Dubois     case ENET_EIMR:
744db0de352SJean-Christophe Dubois     case ENET_RDAR:
745db0de352SJean-Christophe Dubois     case ENET_TDAR:
746db0de352SJean-Christophe Dubois     case ENET_ECR:
747db0de352SJean-Christophe Dubois     case ENET_MMFR:
748db0de352SJean-Christophe Dubois     case ENET_MSCR:
749db0de352SJean-Christophe Dubois     case ENET_MIBC:
750db0de352SJean-Christophe Dubois     case ENET_RCR:
751db0de352SJean-Christophe Dubois     case ENET_TCR:
752db0de352SJean-Christophe Dubois     case ENET_PALR:
753db0de352SJean-Christophe Dubois     case ENET_PAUR:
754db0de352SJean-Christophe Dubois     case ENET_OPD:
755db0de352SJean-Christophe Dubois     case ENET_IAUR:
756db0de352SJean-Christophe Dubois     case ENET_IALR:
757db0de352SJean-Christophe Dubois     case ENET_GAUR:
758db0de352SJean-Christophe Dubois     case ENET_GALR:
759db0de352SJean-Christophe Dubois     case ENET_TFWR:
760db0de352SJean-Christophe Dubois     case ENET_RDSR:
761db0de352SJean-Christophe Dubois     case ENET_TDSR:
762db0de352SJean-Christophe Dubois     case ENET_MRBR:
763db0de352SJean-Christophe Dubois         value = s->regs[index];
764fcbd8018SJean-Christophe Dubois         break;
765fcbd8018SJean-Christophe Dubois     default:
766a699b410SJean-Christophe Dubois         if (s->is_fec) {
767a699b410SJean-Christophe Dubois             value = imx_fec_read(s, index);
768a699b410SJean-Christophe Dubois         } else {
769a699b410SJean-Christophe Dubois             value = imx_enet_read(s, index);
770a699b410SJean-Christophe Dubois         }
771db0de352SJean-Christophe Dubois         break;
772fcbd8018SJean-Christophe Dubois     }
773db0de352SJean-Christophe Dubois 
774a699b410SJean-Christophe Dubois     FEC_PRINTF("reg[%s] => 0x%" PRIx32 "\n", imx_eth_reg_name(s, index),
775db0de352SJean-Christophe Dubois                                               value);
776db0de352SJean-Christophe Dubois 
777db0de352SJean-Christophe Dubois     return value;
778fcbd8018SJean-Christophe Dubois }
779fcbd8018SJean-Christophe Dubois 
780a699b410SJean-Christophe Dubois static void imx_default_write(IMXFECState *s, uint32_t index, uint32_t value)
781a699b410SJean-Christophe Dubois {
782a699b410SJean-Christophe Dubois     qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad address at offset 0x%"
783a699b410SJean-Christophe Dubois                   PRIx32 "\n", TYPE_IMX_FEC, __func__, index * 4);
784a699b410SJean-Christophe Dubois     return;
785a699b410SJean-Christophe Dubois }
786a699b410SJean-Christophe Dubois 
787a699b410SJean-Christophe Dubois static void imx_fec_write(IMXFECState *s, uint32_t index, uint32_t value)
788a699b410SJean-Christophe Dubois {
789a699b410SJean-Christophe Dubois     switch (index) {
790a699b410SJean-Christophe Dubois     case ENET_FRBR:
791a699b410SJean-Christophe Dubois         /* FRBR is read only */
792a699b410SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Register FRBR is read only\n",
793a699b410SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__);
794a699b410SJean-Christophe Dubois         break;
795a699b410SJean-Christophe Dubois     case ENET_FRSR:
796a699b410SJean-Christophe Dubois         s->regs[index] = (value & 0x000003fc) | 0x00000400;
797a699b410SJean-Christophe Dubois         break;
798a699b410SJean-Christophe Dubois     case ENET_MIIGSK_CFGR:
799a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x00000053;
800a699b410SJean-Christophe Dubois         break;
801a699b410SJean-Christophe Dubois     case ENET_MIIGSK_ENR:
802a699b410SJean-Christophe Dubois         s->regs[index] = (value & 0x00000002) ? 0x00000006 : 0;
803a699b410SJean-Christophe Dubois         break;
804a699b410SJean-Christophe Dubois     default:
805a699b410SJean-Christophe Dubois         imx_default_write(s, index, value);
806a699b410SJean-Christophe Dubois         break;
807a699b410SJean-Christophe Dubois     }
808a699b410SJean-Christophe Dubois }
809a699b410SJean-Christophe Dubois 
810a699b410SJean-Christophe Dubois static void imx_enet_write(IMXFECState *s, uint32_t index, uint32_t value)
811a699b410SJean-Christophe Dubois {
812a699b410SJean-Christophe Dubois     switch (index) {
813a699b410SJean-Christophe Dubois     case ENET_RSFL:
814a699b410SJean-Christophe Dubois     case ENET_RSEM:
815a699b410SJean-Christophe Dubois     case ENET_RAEM:
816a699b410SJean-Christophe Dubois     case ENET_RAFL:
817a699b410SJean-Christophe Dubois     case ENET_TSEM:
818a699b410SJean-Christophe Dubois     case ENET_TAEM:
819a699b410SJean-Christophe Dubois     case ENET_TAFL:
820a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x000001ff;
821a699b410SJean-Christophe Dubois         break;
822a699b410SJean-Christophe Dubois     case ENET_TIPG:
823a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x0000001f;
824a699b410SJean-Christophe Dubois         break;
825a699b410SJean-Christophe Dubois     case ENET_FTRL:
826a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x00003fff;
827a699b410SJean-Christophe Dubois         break;
828a699b410SJean-Christophe Dubois     case ENET_TACC:
829a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x00000019;
830a699b410SJean-Christophe Dubois         break;
831a699b410SJean-Christophe Dubois     case ENET_RACC:
832a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x000000C7;
833a699b410SJean-Christophe Dubois         break;
834a699b410SJean-Christophe Dubois     case ENET_ATCR:
835a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x00002a9d;
836a699b410SJean-Christophe Dubois         break;
837a699b410SJean-Christophe Dubois     case ENET_ATVR:
838a699b410SJean-Christophe Dubois     case ENET_ATOFF:
839a699b410SJean-Christophe Dubois     case ENET_ATPER:
840a699b410SJean-Christophe Dubois         s->regs[index] = value;
841a699b410SJean-Christophe Dubois         break;
842a699b410SJean-Christophe Dubois     case ENET_ATSTMP:
843a699b410SJean-Christophe Dubois         /* ATSTMP is read only */
844a699b410SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Register ATSTMP is read only\n",
845a699b410SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__);
846a699b410SJean-Christophe Dubois         break;
847a699b410SJean-Christophe Dubois     case ENET_ATCOR:
848a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x7fffffff;
849a699b410SJean-Christophe Dubois         break;
850a699b410SJean-Christophe Dubois     case ENET_ATINC:
851a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x00007f7f;
852a699b410SJean-Christophe Dubois         break;
853a699b410SJean-Christophe Dubois     case ENET_TGSR:
854a699b410SJean-Christophe Dubois         /* implement clear timer flag */
855a699b410SJean-Christophe Dubois         value = value & 0x0000000f;
856a699b410SJean-Christophe Dubois         break;
857a699b410SJean-Christophe Dubois     case ENET_TCSR0:
858a699b410SJean-Christophe Dubois     case ENET_TCSR1:
859a699b410SJean-Christophe Dubois     case ENET_TCSR2:
860a699b410SJean-Christophe Dubois     case ENET_TCSR3:
861a699b410SJean-Christophe Dubois         value = value & 0x000000fd;
862a699b410SJean-Christophe Dubois         break;
863a699b410SJean-Christophe Dubois     case ENET_TCCR0:
864a699b410SJean-Christophe Dubois     case ENET_TCCR1:
865a699b410SJean-Christophe Dubois     case ENET_TCCR2:
866a699b410SJean-Christophe Dubois     case ENET_TCCR3:
867a699b410SJean-Christophe Dubois         s->regs[index] = value;
868a699b410SJean-Christophe Dubois         break;
869a699b410SJean-Christophe Dubois     default:
870a699b410SJean-Christophe Dubois         imx_default_write(s, index, value);
871a699b410SJean-Christophe Dubois         break;
872a699b410SJean-Christophe Dubois     }
873a699b410SJean-Christophe Dubois }
874a699b410SJean-Christophe Dubois 
875a699b410SJean-Christophe Dubois static void imx_eth_write(void *opaque, hwaddr offset, uint64_t value,
876a699b410SJean-Christophe Dubois                            unsigned size)
877fcbd8018SJean-Christophe Dubois {
878fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(opaque);
879f93f961cSAndrey Smirnov     const bool single_tx_ring = !imx_eth_is_multi_tx_ring(s);
880a699b410SJean-Christophe Dubois     uint32_t index = offset >> 2;
881fcbd8018SJean-Christophe Dubois 
882a699b410SJean-Christophe Dubois     FEC_PRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx_eth_reg_name(s, index),
883db0de352SJean-Christophe Dubois                 (uint32_t)value);
884fcbd8018SJean-Christophe Dubois 
885db0de352SJean-Christophe Dubois     switch (index) {
886db0de352SJean-Christophe Dubois     case ENET_EIR:
887db0de352SJean-Christophe Dubois         s->regs[index] &= ~value;
888fcbd8018SJean-Christophe Dubois         break;
889db0de352SJean-Christophe Dubois     case ENET_EIMR:
890db0de352SJean-Christophe Dubois         s->regs[index] = value;
891fcbd8018SJean-Christophe Dubois         break;
892db0de352SJean-Christophe Dubois     case ENET_RDAR:
893db0de352SJean-Christophe Dubois         if (s->regs[ENET_ECR] & ENET_ECR_ETHEREN) {
894db0de352SJean-Christophe Dubois             if (!s->regs[index]) {
895b2b012afSAndrey Smirnov                 imx_eth_enable_rx(s, true);
896fcbd8018SJean-Christophe Dubois             }
897db0de352SJean-Christophe Dubois         } else {
898db0de352SJean-Christophe Dubois             s->regs[index] = 0;
899db0de352SJean-Christophe Dubois         }
900fcbd8018SJean-Christophe Dubois         break;
901f93f961cSAndrey Smirnov     case ENET_TDAR1:    /* FALLTHROUGH */
902f93f961cSAndrey Smirnov     case ENET_TDAR2:    /* FALLTHROUGH */
903f93f961cSAndrey Smirnov         if (unlikely(single_tx_ring)) {
904f93f961cSAndrey Smirnov             qemu_log_mask(LOG_GUEST_ERROR,
905f93f961cSAndrey Smirnov                           "[%s]%s: trying to access TDAR2 or TDAR1\n",
906f93f961cSAndrey Smirnov                           TYPE_IMX_FEC, __func__);
907f93f961cSAndrey Smirnov             return;
908f93f961cSAndrey Smirnov         }
909f93f961cSAndrey Smirnov     case ENET_TDAR:     /* FALLTHROUGH */
910db0de352SJean-Christophe Dubois         if (s->regs[ENET_ECR] & ENET_ECR_ETHEREN) {
911db0de352SJean-Christophe Dubois             s->regs[index] = ENET_TDAR_TDAR;
912f93f961cSAndrey Smirnov             imx_eth_do_tx(s, index);
913fcbd8018SJean-Christophe Dubois         }
914db0de352SJean-Christophe Dubois         s->regs[index] = 0;
915fcbd8018SJean-Christophe Dubois         break;
916db0de352SJean-Christophe Dubois     case ENET_ECR:
9171bb3c371SJean-Christophe Dubois         if (value & ENET_ECR_RESET) {
918a699b410SJean-Christophe Dubois             return imx_eth_reset(DEVICE(s));
919fcbd8018SJean-Christophe Dubois         }
920db0de352SJean-Christophe Dubois         s->regs[index] = value;
921db0de352SJean-Christophe Dubois         if ((s->regs[index] & ENET_ECR_ETHEREN) == 0) {
922db0de352SJean-Christophe Dubois             s->regs[ENET_RDAR] = 0;
923db0de352SJean-Christophe Dubois             s->rx_descriptor = s->regs[ENET_RDSR];
924db0de352SJean-Christophe Dubois             s->regs[ENET_TDAR]  = 0;
925f93f961cSAndrey Smirnov             s->regs[ENET_TDAR1] = 0;
926f93f961cSAndrey Smirnov             s->regs[ENET_TDAR2] = 0;
927f93f961cSAndrey Smirnov             s->tx_descriptor[0] = s->regs[ENET_TDSR];
928f93f961cSAndrey Smirnov             s->tx_descriptor[1] = s->regs[ENET_TDSR1];
929f93f961cSAndrey Smirnov             s->tx_descriptor[2] = s->regs[ENET_TDSR2];
930fcbd8018SJean-Christophe Dubois         }
931fcbd8018SJean-Christophe Dubois         break;
932db0de352SJean-Christophe Dubois     case ENET_MMFR:
933db0de352SJean-Christophe Dubois         s->regs[index] = value;
9344816dc16SJean-Christophe Dubois         if (extract32(value, 29, 1)) {
935db0de352SJean-Christophe Dubois             /* This is a read operation */
936db0de352SJean-Christophe Dubois             s->regs[ENET_MMFR] = deposit32(s->regs[ENET_MMFR], 0, 16,
937db0de352SJean-Christophe Dubois                                            do_phy_read(s,
938db0de352SJean-Christophe Dubois                                                        extract32(value,
939db0de352SJean-Christophe Dubois                                                                  18, 10)));
9404816dc16SJean-Christophe Dubois         } else {
941db0de352SJean-Christophe Dubois             /* This a write operation */
942b413643aSJean-Christophe Dubois             do_phy_write(s, extract32(value, 18, 10), extract32(value, 0, 16));
943fcbd8018SJean-Christophe Dubois         }
944fcbd8018SJean-Christophe Dubois         /* raise the interrupt as the PHY operation is done */
945db0de352SJean-Christophe Dubois         s->regs[ENET_EIR] |= ENET_INT_MII;
946fcbd8018SJean-Christophe Dubois         break;
947db0de352SJean-Christophe Dubois     case ENET_MSCR:
948db0de352SJean-Christophe Dubois         s->regs[index] = value & 0xfe;
949fcbd8018SJean-Christophe Dubois         break;
950db0de352SJean-Christophe Dubois     case ENET_MIBC:
951fcbd8018SJean-Christophe Dubois         /* TODO: Implement MIB.  */
952db0de352SJean-Christophe Dubois         s->regs[index] = (value & 0x80000000) ? 0xc0000000 : 0;
953fcbd8018SJean-Christophe Dubois         break;
954db0de352SJean-Christophe Dubois     case ENET_RCR:
955db0de352SJean-Christophe Dubois         s->regs[index] = value & 0x07ff003f;
956fcbd8018SJean-Christophe Dubois         /* TODO: Implement LOOP mode.  */
957fcbd8018SJean-Christophe Dubois         break;
958db0de352SJean-Christophe Dubois     case ENET_TCR:
959fcbd8018SJean-Christophe Dubois         /* We transmit immediately, so raise GRA immediately.  */
960db0de352SJean-Christophe Dubois         s->regs[index] = value;
961fcbd8018SJean-Christophe Dubois         if (value & 1) {
962db0de352SJean-Christophe Dubois             s->regs[ENET_EIR] |= ENET_INT_GRA;
963fcbd8018SJean-Christophe Dubois         }
964fcbd8018SJean-Christophe Dubois         break;
965db0de352SJean-Christophe Dubois     case ENET_PALR:
966db0de352SJean-Christophe Dubois         s->regs[index] = value;
967fcbd8018SJean-Christophe Dubois         s->conf.macaddr.a[0] = value >> 24;
968fcbd8018SJean-Christophe Dubois         s->conf.macaddr.a[1] = value >> 16;
969fcbd8018SJean-Christophe Dubois         s->conf.macaddr.a[2] = value >> 8;
970fcbd8018SJean-Christophe Dubois         s->conf.macaddr.a[3] = value;
971fcbd8018SJean-Christophe Dubois         break;
972db0de352SJean-Christophe Dubois     case ENET_PAUR:
973db0de352SJean-Christophe Dubois         s->regs[index] = (value | 0x0000ffff) & 0xffff8808;
974fcbd8018SJean-Christophe Dubois         s->conf.macaddr.a[4] = value >> 24;
975fcbd8018SJean-Christophe Dubois         s->conf.macaddr.a[5] = value >> 16;
976fcbd8018SJean-Christophe Dubois         break;
977db0de352SJean-Christophe Dubois     case ENET_OPD:
978db0de352SJean-Christophe Dubois         s->regs[index] = (value & 0x0000ffff) | 0x00010000;
979fcbd8018SJean-Christophe Dubois         break;
980db0de352SJean-Christophe Dubois     case ENET_IAUR:
981db0de352SJean-Christophe Dubois     case ENET_IALR:
982db0de352SJean-Christophe Dubois     case ENET_GAUR:
983db0de352SJean-Christophe Dubois     case ENET_GALR:
984fcbd8018SJean-Christophe Dubois         /* TODO: implement MAC hash filtering.  */
985fcbd8018SJean-Christophe Dubois         break;
986db0de352SJean-Christophe Dubois     case ENET_TFWR:
987a699b410SJean-Christophe Dubois         if (s->is_fec) {
988a699b410SJean-Christophe Dubois             s->regs[index] = value & 0x3;
989a699b410SJean-Christophe Dubois         } else {
990a699b410SJean-Christophe Dubois             s->regs[index] = value & 0x13f;
991a699b410SJean-Christophe Dubois         }
992fcbd8018SJean-Christophe Dubois         break;
993db0de352SJean-Christophe Dubois     case ENET_RDSR:
994a699b410SJean-Christophe Dubois         if (s->is_fec) {
995db0de352SJean-Christophe Dubois             s->regs[index] = value & ~3;
996a699b410SJean-Christophe Dubois         } else {
997a699b410SJean-Christophe Dubois             s->regs[index] = value & ~7;
998a699b410SJean-Christophe Dubois         }
999db0de352SJean-Christophe Dubois         s->rx_descriptor = s->regs[index];
1000fcbd8018SJean-Christophe Dubois         break;
1001db0de352SJean-Christophe Dubois     case ENET_TDSR:
1002a699b410SJean-Christophe Dubois         if (s->is_fec) {
1003db0de352SJean-Christophe Dubois             s->regs[index] = value & ~3;
1004a699b410SJean-Christophe Dubois         } else {
1005a699b410SJean-Christophe Dubois             s->regs[index] = value & ~7;
1006a699b410SJean-Christophe Dubois         }
1007f93f961cSAndrey Smirnov         s->tx_descriptor[0] = s->regs[index];
1008f93f961cSAndrey Smirnov         break;
1009f93f961cSAndrey Smirnov     case ENET_TDSR1:
1010f93f961cSAndrey Smirnov         if (unlikely(single_tx_ring)) {
1011f93f961cSAndrey Smirnov             qemu_log_mask(LOG_GUEST_ERROR,
1012f93f961cSAndrey Smirnov                           "[%s]%s: trying to access TDSR1\n",
1013f93f961cSAndrey Smirnov                           TYPE_IMX_FEC, __func__);
1014f93f961cSAndrey Smirnov             return;
1015f93f961cSAndrey Smirnov         }
1016f93f961cSAndrey Smirnov 
1017f93f961cSAndrey Smirnov         s->regs[index] = value & ~7;
1018f93f961cSAndrey Smirnov         s->tx_descriptor[1] = s->regs[index];
1019f93f961cSAndrey Smirnov         break;
1020f93f961cSAndrey Smirnov     case ENET_TDSR2:
1021f93f961cSAndrey Smirnov         if (unlikely(single_tx_ring)) {
1022f93f961cSAndrey Smirnov             qemu_log_mask(LOG_GUEST_ERROR,
1023f93f961cSAndrey Smirnov                           "[%s]%s: trying to access TDSR2\n",
1024f93f961cSAndrey Smirnov                           TYPE_IMX_FEC, __func__);
1025f93f961cSAndrey Smirnov             return;
1026f93f961cSAndrey Smirnov         }
1027f93f961cSAndrey Smirnov 
1028f93f961cSAndrey Smirnov         s->regs[index] = value & ~7;
1029f93f961cSAndrey Smirnov         s->tx_descriptor[2] = s->regs[index];
1030fcbd8018SJean-Christophe Dubois         break;
1031db0de352SJean-Christophe Dubois     case ENET_MRBR:
1032a699b410SJean-Christophe Dubois         s->regs[index] = value & 0x00003ff0;
1033fcbd8018SJean-Christophe Dubois         break;
1034fcbd8018SJean-Christophe Dubois     default:
1035a699b410SJean-Christophe Dubois         if (s->is_fec) {
1036a699b410SJean-Christophe Dubois             imx_fec_write(s, index, value);
1037a699b410SJean-Christophe Dubois         } else {
1038a699b410SJean-Christophe Dubois             imx_enet_write(s, index, value);
1039a699b410SJean-Christophe Dubois         }
1040a699b410SJean-Christophe Dubois         return;
1041fcbd8018SJean-Christophe Dubois     }
1042fcbd8018SJean-Christophe Dubois 
1043a699b410SJean-Christophe Dubois     imx_eth_update(s);
1044fcbd8018SJean-Christophe Dubois }
1045fcbd8018SJean-Christophe Dubois 
1046a699b410SJean-Christophe Dubois static int imx_eth_can_receive(NetClientState *nc)
1047fcbd8018SJean-Christophe Dubois {
1048fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1049fcbd8018SJean-Christophe Dubois 
1050a699b410SJean-Christophe Dubois     FEC_PRINTF("\n");
1051a699b410SJean-Christophe Dubois 
1052b2b012afSAndrey Smirnov     return !!s->regs[ENET_RDAR];
1053fcbd8018SJean-Christophe Dubois }
1054fcbd8018SJean-Christophe Dubois 
1055fcbd8018SJean-Christophe Dubois static ssize_t imx_fec_receive(NetClientState *nc, const uint8_t *buf,
1056fcbd8018SJean-Christophe Dubois                                size_t len)
1057fcbd8018SJean-Christophe Dubois {
1058fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1059fcbd8018SJean-Christophe Dubois     IMXFECBufDesc bd;
1060fcbd8018SJean-Christophe Dubois     uint32_t flags = 0;
1061fcbd8018SJean-Christophe Dubois     uint32_t addr;
1062fcbd8018SJean-Christophe Dubois     uint32_t crc;
1063fcbd8018SJean-Christophe Dubois     uint32_t buf_addr;
1064fcbd8018SJean-Christophe Dubois     uint8_t *crc_ptr;
1065fcbd8018SJean-Christophe Dubois     unsigned int buf_len;
1066fcbd8018SJean-Christophe Dubois     size_t size = len;
1067fcbd8018SJean-Christophe Dubois 
1068fcbd8018SJean-Christophe Dubois     FEC_PRINTF("len %d\n", (int)size);
1069fcbd8018SJean-Christophe Dubois 
1070db0de352SJean-Christophe Dubois     if (!s->regs[ENET_RDAR]) {
1071b72d8d25SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Unexpected packet\n",
1072fcbd8018SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__);
1073fcbd8018SJean-Christophe Dubois         return 0;
1074fcbd8018SJean-Christophe Dubois     }
1075fcbd8018SJean-Christophe Dubois 
1076fcbd8018SJean-Christophe Dubois     /* 4 bytes for the CRC.  */
1077fcbd8018SJean-Christophe Dubois     size += 4;
1078fcbd8018SJean-Christophe Dubois     crc = cpu_to_be32(crc32(~0, buf, size));
1079fcbd8018SJean-Christophe Dubois     crc_ptr = (uint8_t *) &crc;
1080fcbd8018SJean-Christophe Dubois 
1081a699b410SJean-Christophe Dubois     /* Huge frames are truncated.  */
10821bb3c371SJean-Christophe Dubois     if (size > ENET_MAX_FRAME_SIZE) {
10831bb3c371SJean-Christophe Dubois         size = ENET_MAX_FRAME_SIZE;
10841bb3c371SJean-Christophe Dubois         flags |= ENET_BD_TR | ENET_BD_LG;
1085fcbd8018SJean-Christophe Dubois     }
1086fcbd8018SJean-Christophe Dubois 
1087fcbd8018SJean-Christophe Dubois     /* Frames larger than the user limit just set error flags.  */
1088db0de352SJean-Christophe Dubois     if (size > (s->regs[ENET_RCR] >> 16)) {
10891bb3c371SJean-Christophe Dubois         flags |= ENET_BD_LG;
1090fcbd8018SJean-Christophe Dubois     }
1091fcbd8018SJean-Christophe Dubois 
1092fcbd8018SJean-Christophe Dubois     addr = s->rx_descriptor;
1093fcbd8018SJean-Christophe Dubois     while (size > 0) {
1094fcbd8018SJean-Christophe Dubois         imx_fec_read_bd(&bd, addr);
10951bb3c371SJean-Christophe Dubois         if ((bd.flags & ENET_BD_E) == 0) {
1096fcbd8018SJean-Christophe Dubois             /* No descriptors available.  Bail out.  */
1097fcbd8018SJean-Christophe Dubois             /*
1098fcbd8018SJean-Christophe Dubois              * FIXME: This is wrong. We should probably either
1099fcbd8018SJean-Christophe Dubois              * save the remainder for when more RX buffers are
1100fcbd8018SJean-Christophe Dubois              * available, or flag an error.
1101fcbd8018SJean-Christophe Dubois              */
1102b72d8d25SJean-Christophe Dubois             qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Lost end of frame\n",
1103fcbd8018SJean-Christophe Dubois                           TYPE_IMX_FEC, __func__);
1104fcbd8018SJean-Christophe Dubois             break;
1105fcbd8018SJean-Christophe Dubois         }
1106db0de352SJean-Christophe Dubois         buf_len = (size <= s->regs[ENET_MRBR]) ? size : s->regs[ENET_MRBR];
1107fcbd8018SJean-Christophe Dubois         bd.length = buf_len;
1108fcbd8018SJean-Christophe Dubois         size -= buf_len;
1109b72d8d25SJean-Christophe Dubois 
1110b72d8d25SJean-Christophe Dubois         FEC_PRINTF("rx_bd 0x%x length %d\n", addr, bd.length);
1111b72d8d25SJean-Christophe Dubois 
1112fcbd8018SJean-Christophe Dubois         /* The last 4 bytes are the CRC.  */
1113fcbd8018SJean-Christophe Dubois         if (size < 4) {
1114fcbd8018SJean-Christophe Dubois             buf_len += size - 4;
1115fcbd8018SJean-Christophe Dubois         }
1116fcbd8018SJean-Christophe Dubois         buf_addr = bd.data;
1117fcbd8018SJean-Christophe Dubois         dma_memory_write(&address_space_memory, buf_addr, buf, buf_len);
1118fcbd8018SJean-Christophe Dubois         buf += buf_len;
1119fcbd8018SJean-Christophe Dubois         if (size < 4) {
1120fcbd8018SJean-Christophe Dubois             dma_memory_write(&address_space_memory, buf_addr + buf_len,
1121fcbd8018SJean-Christophe Dubois                              crc_ptr, 4 - size);
1122fcbd8018SJean-Christophe Dubois             crc_ptr += 4 - size;
1123fcbd8018SJean-Christophe Dubois         }
11241bb3c371SJean-Christophe Dubois         bd.flags &= ~ENET_BD_E;
1125fcbd8018SJean-Christophe Dubois         if (size == 0) {
1126fcbd8018SJean-Christophe Dubois             /* Last buffer in frame.  */
11271bb3c371SJean-Christophe Dubois             bd.flags |= flags | ENET_BD_L;
1128fcbd8018SJean-Christophe Dubois             FEC_PRINTF("rx frame flags %04x\n", bd.flags);
1129db0de352SJean-Christophe Dubois             s->regs[ENET_EIR] |= ENET_INT_RXF;
1130fcbd8018SJean-Christophe Dubois         } else {
1131db0de352SJean-Christophe Dubois             s->regs[ENET_EIR] |= ENET_INT_RXB;
1132fcbd8018SJean-Christophe Dubois         }
1133fcbd8018SJean-Christophe Dubois         imx_fec_write_bd(&bd, addr);
1134fcbd8018SJean-Christophe Dubois         /* Advance to the next descriptor.  */
11351bb3c371SJean-Christophe Dubois         if ((bd.flags & ENET_BD_W) != 0) {
1136db0de352SJean-Christophe Dubois             addr = s->regs[ENET_RDSR];
1137fcbd8018SJean-Christophe Dubois         } else {
1138db0de352SJean-Christophe Dubois             addr += sizeof(bd);
1139fcbd8018SJean-Christophe Dubois         }
1140fcbd8018SJean-Christophe Dubois     }
1141fcbd8018SJean-Christophe Dubois     s->rx_descriptor = addr;
1142b2b012afSAndrey Smirnov     imx_eth_enable_rx(s, false);
1143a699b410SJean-Christophe Dubois     imx_eth_update(s);
1144fcbd8018SJean-Christophe Dubois     return len;
1145fcbd8018SJean-Christophe Dubois }
1146fcbd8018SJean-Christophe Dubois 
1147a699b410SJean-Christophe Dubois static ssize_t imx_enet_receive(NetClientState *nc, const uint8_t *buf,
1148a699b410SJean-Christophe Dubois                                 size_t len)
1149a699b410SJean-Christophe Dubois {
1150a699b410SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1151a699b410SJean-Christophe Dubois     IMXENETBufDesc bd;
1152a699b410SJean-Christophe Dubois     uint32_t flags = 0;
1153a699b410SJean-Christophe Dubois     uint32_t addr;
1154a699b410SJean-Christophe Dubois     uint32_t crc;
1155a699b410SJean-Christophe Dubois     uint32_t buf_addr;
1156a699b410SJean-Christophe Dubois     uint8_t *crc_ptr;
1157a699b410SJean-Christophe Dubois     unsigned int buf_len;
1158a699b410SJean-Christophe Dubois     size_t size = len;
1159ebdd8cddSAndrey Smirnov     bool shift16 = s->regs[ENET_RACC] & ENET_RACC_SHIFT16;
1160a699b410SJean-Christophe Dubois 
1161a699b410SJean-Christophe Dubois     FEC_PRINTF("len %d\n", (int)size);
1162a699b410SJean-Christophe Dubois 
1163a699b410SJean-Christophe Dubois     if (!s->regs[ENET_RDAR]) {
1164a699b410SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Unexpected packet\n",
1165a699b410SJean-Christophe Dubois                       TYPE_IMX_FEC, __func__);
1166a699b410SJean-Christophe Dubois         return 0;
1167a699b410SJean-Christophe Dubois     }
1168a699b410SJean-Christophe Dubois 
1169a699b410SJean-Christophe Dubois     /* 4 bytes for the CRC.  */
1170a699b410SJean-Christophe Dubois     size += 4;
1171a699b410SJean-Christophe Dubois     crc = cpu_to_be32(crc32(~0, buf, size));
1172a699b410SJean-Christophe Dubois     crc_ptr = (uint8_t *) &crc;
1173a699b410SJean-Christophe Dubois 
1174ebdd8cddSAndrey Smirnov     if (shift16) {
1175ebdd8cddSAndrey Smirnov         size += 2;
1176ebdd8cddSAndrey Smirnov     }
1177ebdd8cddSAndrey Smirnov 
1178894d74ccSAndrey Smirnov     /* Huge frames are truncated. */
1179ff9a7feeSAndrey Smirnov     if (size > s->regs[ENET_FTRL]) {
1180ff9a7feeSAndrey Smirnov         size = s->regs[ENET_FTRL];
1181a699b410SJean-Christophe Dubois         flags |= ENET_BD_TR | ENET_BD_LG;
1182a699b410SJean-Christophe Dubois     }
1183a699b410SJean-Christophe Dubois 
1184a699b410SJean-Christophe Dubois     /* Frames larger than the user limit just set error flags.  */
1185a699b410SJean-Christophe Dubois     if (size > (s->regs[ENET_RCR] >> 16)) {
1186a699b410SJean-Christophe Dubois         flags |= ENET_BD_LG;
1187a699b410SJean-Christophe Dubois     }
1188a699b410SJean-Christophe Dubois 
1189a699b410SJean-Christophe Dubois     addr = s->rx_descriptor;
1190a699b410SJean-Christophe Dubois     while (size > 0) {
1191a699b410SJean-Christophe Dubois         imx_enet_read_bd(&bd, addr);
1192a699b410SJean-Christophe Dubois         if ((bd.flags & ENET_BD_E) == 0) {
1193a699b410SJean-Christophe Dubois             /* No descriptors available.  Bail out.  */
1194a699b410SJean-Christophe Dubois             /*
1195a699b410SJean-Christophe Dubois              * FIXME: This is wrong. We should probably either
1196a699b410SJean-Christophe Dubois              * save the remainder for when more RX buffers are
1197a699b410SJean-Christophe Dubois              * available, or flag an error.
1198a699b410SJean-Christophe Dubois              */
1199a699b410SJean-Christophe Dubois             qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Lost end of frame\n",
1200a699b410SJean-Christophe Dubois                           TYPE_IMX_FEC, __func__);
1201a699b410SJean-Christophe Dubois             break;
1202a699b410SJean-Christophe Dubois         }
12034c5e7a6cSAndrey Smirnov         buf_len = MIN(size, s->regs[ENET_MRBR]);
1204a699b410SJean-Christophe Dubois         bd.length = buf_len;
1205a699b410SJean-Christophe Dubois         size -= buf_len;
1206a699b410SJean-Christophe Dubois 
1207a699b410SJean-Christophe Dubois         FEC_PRINTF("rx_bd 0x%x length %d\n", addr, bd.length);
1208a699b410SJean-Christophe Dubois 
1209a699b410SJean-Christophe Dubois         /* The last 4 bytes are the CRC.  */
1210a699b410SJean-Christophe Dubois         if (size < 4) {
1211a699b410SJean-Christophe Dubois             buf_len += size - 4;
1212a699b410SJean-Christophe Dubois         }
1213a699b410SJean-Christophe Dubois         buf_addr = bd.data;
1214ebdd8cddSAndrey Smirnov 
1215ebdd8cddSAndrey Smirnov         if (shift16) {
1216ebdd8cddSAndrey Smirnov             /*
1217ebdd8cddSAndrey Smirnov              * If SHIFT16 bit of ENETx_RACC register is set we need to
1218ebdd8cddSAndrey Smirnov              * align the payload to 4-byte boundary.
1219ebdd8cddSAndrey Smirnov              */
1220ebdd8cddSAndrey Smirnov             const uint8_t zeros[2] = { 0 };
1221ebdd8cddSAndrey Smirnov 
1222ebdd8cddSAndrey Smirnov             dma_memory_write(&address_space_memory, buf_addr,
1223ebdd8cddSAndrey Smirnov                              zeros, sizeof(zeros));
1224ebdd8cddSAndrey Smirnov 
1225ebdd8cddSAndrey Smirnov             buf_addr += sizeof(zeros);
1226ebdd8cddSAndrey Smirnov             buf_len  -= sizeof(zeros);
1227ebdd8cddSAndrey Smirnov 
1228ebdd8cddSAndrey Smirnov             /* We only do this once per Ethernet frame */
1229ebdd8cddSAndrey Smirnov             shift16 = false;
1230ebdd8cddSAndrey Smirnov         }
1231ebdd8cddSAndrey Smirnov 
1232a699b410SJean-Christophe Dubois         dma_memory_write(&address_space_memory, buf_addr, buf, buf_len);
1233a699b410SJean-Christophe Dubois         buf += buf_len;
1234a699b410SJean-Christophe Dubois         if (size < 4) {
1235a699b410SJean-Christophe Dubois             dma_memory_write(&address_space_memory, buf_addr + buf_len,
1236a699b410SJean-Christophe Dubois                              crc_ptr, 4 - size);
1237a699b410SJean-Christophe Dubois             crc_ptr += 4 - size;
1238a699b410SJean-Christophe Dubois         }
1239a699b410SJean-Christophe Dubois         bd.flags &= ~ENET_BD_E;
1240a699b410SJean-Christophe Dubois         if (size == 0) {
1241a699b410SJean-Christophe Dubois             /* Last buffer in frame.  */
1242a699b410SJean-Christophe Dubois             bd.flags |= flags | ENET_BD_L;
1243a699b410SJean-Christophe Dubois             FEC_PRINTF("rx frame flags %04x\n", bd.flags);
1244*88e1b59eSAaron Hill             /* Indicate that we've updated the last buffer descriptor. */
1245*88e1b59eSAaron Hill             bd.last_buffer = ENET_BD_BDU;
1246a699b410SJean-Christophe Dubois             if (bd.option & ENET_BD_RX_INT) {
1247a699b410SJean-Christophe Dubois                 s->regs[ENET_EIR] |= ENET_INT_RXF;
1248a699b410SJean-Christophe Dubois             }
1249a699b410SJean-Christophe Dubois         } else {
1250a699b410SJean-Christophe Dubois             if (bd.option & ENET_BD_RX_INT) {
1251a699b410SJean-Christophe Dubois                 s->regs[ENET_EIR] |= ENET_INT_RXB;
1252a699b410SJean-Christophe Dubois             }
1253a699b410SJean-Christophe Dubois         }
1254a699b410SJean-Christophe Dubois         imx_enet_write_bd(&bd, addr);
1255a699b410SJean-Christophe Dubois         /* Advance to the next descriptor.  */
1256a699b410SJean-Christophe Dubois         if ((bd.flags & ENET_BD_W) != 0) {
1257a699b410SJean-Christophe Dubois             addr = s->regs[ENET_RDSR];
1258a699b410SJean-Christophe Dubois         } else {
1259a699b410SJean-Christophe Dubois             addr += sizeof(bd);
1260a699b410SJean-Christophe Dubois         }
1261a699b410SJean-Christophe Dubois     }
1262a699b410SJean-Christophe Dubois     s->rx_descriptor = addr;
1263b2b012afSAndrey Smirnov     imx_eth_enable_rx(s, false);
1264a699b410SJean-Christophe Dubois     imx_eth_update(s);
1265a699b410SJean-Christophe Dubois     return len;
1266a699b410SJean-Christophe Dubois }
1267a699b410SJean-Christophe Dubois 
1268a699b410SJean-Christophe Dubois static ssize_t imx_eth_receive(NetClientState *nc, const uint8_t *buf,
1269a699b410SJean-Christophe Dubois                                 size_t len)
1270a699b410SJean-Christophe Dubois {
1271a699b410SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1272a699b410SJean-Christophe Dubois 
1273a699b410SJean-Christophe Dubois     if (!s->is_fec && (s->regs[ENET_ECR] & ENET_ECR_EN1588)) {
1274a699b410SJean-Christophe Dubois         return imx_enet_receive(nc, buf, len);
1275a699b410SJean-Christophe Dubois     } else {
1276a699b410SJean-Christophe Dubois         return imx_fec_receive(nc, buf, len);
1277a699b410SJean-Christophe Dubois     }
1278a699b410SJean-Christophe Dubois }
1279a699b410SJean-Christophe Dubois 
1280a699b410SJean-Christophe Dubois static const MemoryRegionOps imx_eth_ops = {
1281a699b410SJean-Christophe Dubois     .read                  = imx_eth_read,
1282a699b410SJean-Christophe Dubois     .write                 = imx_eth_write,
1283fcbd8018SJean-Christophe Dubois     .valid.min_access_size = 4,
1284fcbd8018SJean-Christophe Dubois     .valid.max_access_size = 4,
1285fcbd8018SJean-Christophe Dubois     .endianness            = DEVICE_NATIVE_ENDIAN,
1286fcbd8018SJean-Christophe Dubois };
1287fcbd8018SJean-Christophe Dubois 
1288a699b410SJean-Christophe Dubois static void imx_eth_cleanup(NetClientState *nc)
1289fcbd8018SJean-Christophe Dubois {
1290fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1291fcbd8018SJean-Christophe Dubois 
1292fcbd8018SJean-Christophe Dubois     s->nic = NULL;
1293fcbd8018SJean-Christophe Dubois }
1294fcbd8018SJean-Christophe Dubois 
1295a699b410SJean-Christophe Dubois static NetClientInfo imx_eth_net_info = {
1296f394b2e2SEric Blake     .type                = NET_CLIENT_DRIVER_NIC,
1297fcbd8018SJean-Christophe Dubois     .size                = sizeof(NICState),
1298a699b410SJean-Christophe Dubois     .can_receive         = imx_eth_can_receive,
1299a699b410SJean-Christophe Dubois     .receive             = imx_eth_receive,
1300a699b410SJean-Christophe Dubois     .cleanup             = imx_eth_cleanup,
1301a699b410SJean-Christophe Dubois     .link_status_changed = imx_eth_set_link,
1302fcbd8018SJean-Christophe Dubois };
1303fcbd8018SJean-Christophe Dubois 
1304fcbd8018SJean-Christophe Dubois 
1305a699b410SJean-Christophe Dubois static void imx_eth_realize(DeviceState *dev, Error **errp)
1306fcbd8018SJean-Christophe Dubois {
1307fcbd8018SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(dev);
1308fcbd8018SJean-Christophe Dubois     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1309fcbd8018SJean-Christophe Dubois 
1310a699b410SJean-Christophe Dubois     memory_region_init_io(&s->iomem, OBJECT(dev), &imx_eth_ops, s,
1311831858adSAndrey Smirnov                           TYPE_IMX_FEC, FSL_IMX25_FEC_SIZE);
1312fcbd8018SJean-Christophe Dubois     sysbus_init_mmio(sbd, &s->iomem);
1313a699b410SJean-Christophe Dubois     sysbus_init_irq(sbd, &s->irq[0]);
1314a699b410SJean-Christophe Dubois     sysbus_init_irq(sbd, &s->irq[1]);
1315a699b410SJean-Christophe Dubois 
1316fcbd8018SJean-Christophe Dubois     qemu_macaddr_default_if_unset(&s->conf.macaddr);
1317fcbd8018SJean-Christophe Dubois 
1318a699b410SJean-Christophe Dubois     s->nic = qemu_new_nic(&imx_eth_net_info, &s->conf,
1319a699b410SJean-Christophe Dubois                           object_get_typename(OBJECT(dev)),
1320a699b410SJean-Christophe Dubois                           DEVICE(dev)->id, s);
1321a699b410SJean-Christophe Dubois 
1322fcbd8018SJean-Christophe Dubois     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
1323fcbd8018SJean-Christophe Dubois }
1324fcbd8018SJean-Christophe Dubois 
1325a699b410SJean-Christophe Dubois static Property imx_eth_properties[] = {
1326fcbd8018SJean-Christophe Dubois     DEFINE_NIC_PROPERTIES(IMXFECState, conf),
1327f93f961cSAndrey Smirnov     DEFINE_PROP_UINT32("tx-ring-num", IMXFECState, tx_ring_num, 1),
1328fcbd8018SJean-Christophe Dubois     DEFINE_PROP_END_OF_LIST(),
1329fcbd8018SJean-Christophe Dubois };
1330fcbd8018SJean-Christophe Dubois 
1331a699b410SJean-Christophe Dubois static void imx_eth_class_init(ObjectClass *klass, void *data)
1332fcbd8018SJean-Christophe Dubois {
1333fcbd8018SJean-Christophe Dubois     DeviceClass *dc = DEVICE_CLASS(klass);
1334fcbd8018SJean-Christophe Dubois 
1335a699b410SJean-Christophe Dubois     dc->vmsd    = &vmstate_imx_eth;
1336a699b410SJean-Christophe Dubois     dc->reset   = imx_eth_reset;
1337a699b410SJean-Christophe Dubois     dc->props   = imx_eth_properties;
1338a699b410SJean-Christophe Dubois     dc->realize = imx_eth_realize;
1339a699b410SJean-Christophe Dubois     dc->desc    = "i.MX FEC/ENET Ethernet Controller";
1340a699b410SJean-Christophe Dubois }
1341a699b410SJean-Christophe Dubois 
1342a699b410SJean-Christophe Dubois static void imx_fec_init(Object *obj)
1343a699b410SJean-Christophe Dubois {
1344a699b410SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(obj);
1345a699b410SJean-Christophe Dubois 
1346a699b410SJean-Christophe Dubois     s->is_fec = true;
1347a699b410SJean-Christophe Dubois }
1348a699b410SJean-Christophe Dubois 
1349a699b410SJean-Christophe Dubois static void imx_enet_init(Object *obj)
1350a699b410SJean-Christophe Dubois {
1351a699b410SJean-Christophe Dubois     IMXFECState *s = IMX_FEC(obj);
1352a699b410SJean-Christophe Dubois 
1353a699b410SJean-Christophe Dubois     s->is_fec = false;
1354fcbd8018SJean-Christophe Dubois }
1355fcbd8018SJean-Christophe Dubois 
1356fcbd8018SJean-Christophe Dubois static const TypeInfo imx_fec_info = {
1357fcbd8018SJean-Christophe Dubois     .name          = TYPE_IMX_FEC,
1358fcbd8018SJean-Christophe Dubois     .parent        = TYPE_SYS_BUS_DEVICE,
1359fcbd8018SJean-Christophe Dubois     .instance_size = sizeof(IMXFECState),
1360a699b410SJean-Christophe Dubois     .instance_init = imx_fec_init,
1361a699b410SJean-Christophe Dubois     .class_init    = imx_eth_class_init,
1362fcbd8018SJean-Christophe Dubois };
1363fcbd8018SJean-Christophe Dubois 
1364a699b410SJean-Christophe Dubois static const TypeInfo imx_enet_info = {
1365a699b410SJean-Christophe Dubois     .name          = TYPE_IMX_ENET,
1366a699b410SJean-Christophe Dubois     .parent        = TYPE_IMX_FEC,
1367a699b410SJean-Christophe Dubois     .instance_init = imx_enet_init,
1368a699b410SJean-Christophe Dubois };
1369a699b410SJean-Christophe Dubois 
1370a699b410SJean-Christophe Dubois static void imx_eth_register_types(void)
1371fcbd8018SJean-Christophe Dubois {
1372fcbd8018SJean-Christophe Dubois     type_register_static(&imx_fec_info);
1373a699b410SJean-Christophe Dubois     type_register_static(&imx_enet_info);
1374fcbd8018SJean-Christophe Dubois }
1375fcbd8018SJean-Christophe Dubois 
1376a699b410SJean-Christophe Dubois type_init(imx_eth_register_types)
1377