dwc_eqos.c revision 1.1 1 /* $NetBSD: dwc_eqos.c,v 1.1 2022/01/03 17:19:41 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * DesignWare Ethernet Quality-of-Service controller
31 */
32
33 #include "opt_net_mpsafe.h"
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: dwc_eqos.c,v 1.1 2022/01/03 17:19:41 jmcneill Exp $");
37
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/callout.h>
46 #include <sys/cprng.h>
47
48 #include <sys/rndsource.h>
49
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 #include <net/bpf.h>
55
56 #include <dev/mii/miivar.h>
57
58 #include <dev/ic/dwc_eqos_reg.h>
59 #include <dev/ic/dwc_eqos_var.h>
60
61 CTASSERT(MCLBYTES == 2048);
62 #ifdef EQOS_DEBUG
63 #define DPRINTF(...) printf(##__VA_ARGS__)
64 #else
65 #define DPRINTF(...) ((void)0)
66 #endif
67
68 #ifdef NET_MPSAFE
69 #define EQOS_MPSAFE 1
70 #define CALLOUT_FLAGS CALLOUT_MPSAFE
71 #else
72 #define CALLOUT_FLAGS 0
73 #endif
74
75 #define DESC_BOUNDARY (1ULL << 32)
76 #define DESC_ALIGN sizeof(struct eqos_dma_desc)
77 #define TX_DESC_COUNT EQOS_DMA_DESC_COUNT
78 #define TX_DESC_SIZE (TX_DESC_COUNT * DESC_ALIGN)
79 #define RX_DESC_COUNT EQOS_DMA_DESC_COUNT
80 #define RX_DESC_SIZE (RX_DESC_COUNT * DESC_ALIGN)
81 #define MII_BUSY_RETRY 1000
82
83 #define DESC_OFF(n) ((n) * sizeof(struct eqos_dma_desc))
84 #define TX_SKIP(n, o) (((n) + (o)) % TX_DESC_COUNT)
85 #define TX_NEXT(n) TX_SKIP(n, 1)
86 #define RX_NEXT(n) (((n) + 1) % RX_DESC_COUNT)
87
88 #define TX_MAX_SEGS 128
89
90 #define EQOS_LOCK(sc) mutex_enter(&(sc)->sc_lock)
91 #define EQOS_UNLOCK(sc) mutex_exit(&(sc)->sc_lock)
92 #define EQOS_ASSERT_LOCKED(sc) KASSERT(mutex_owned(&(sc)->sc_lock))
93
94 #define EQOS_TXLOCK(sc) mutex_enter(&(sc)->sc_txlock)
95 #define EQOS_TXUNLOCK(sc) mutex_exit(&(sc)->sc_txlock)
96 #define EQOS_ASSERT_TXLOCKED(sc) KASSERT(mutex_owned(&(sc)->sc_txlock))
97
98 #define EQOS_HW_FEATURE_ADDR64_32BIT(sc) \
99 (((sc)->sc_hw_feature[1] & GMAC_MAC_HW_FEATURE1_ADDR64_MASK) == \
100 GMAC_MAC_HW_FEATURE1_ADDR64_32BIT)
101
102
103 #define RD4(sc, reg) \
104 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
105 #define WR4(sc, reg, val) \
106 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
107
108 #define STUB(...) \
109 printf("%s: TODO\n", __func__); \
110
111 static int
112 eqos_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
113 {
114 struct eqos_softc *sc = device_private(dev);
115 uint32_t addr;
116 int retry;
117
118 addr = sc->sc_clock_range |
119 (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
120 (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
121 GMAC_MAC_MDIO_ADDRESS_GOC_READ |
122 GMAC_MAC_MDIO_ADDRESS_GB;
123 WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
124
125 delay(10000);
126
127 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
128 addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
129 if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
130 *val = RD4(sc, GMAC_MAC_MDIO_DATA) & 0xFFFF;
131 break;
132 }
133 delay(10);
134 }
135 if (retry == 0) {
136 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
137 phy, reg);
138 return ETIMEDOUT;
139 }
140
141 return 0;
142 }
143
144 static int
145 eqos_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
146 {
147 struct eqos_softc *sc = device_private(dev);
148 uint32_t addr;
149 int retry;
150
151 WR4(sc, GMAC_MAC_MDIO_DATA, val);
152
153 addr = sc->sc_clock_range |
154 (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
155 (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
156 GMAC_MAC_MDIO_ADDRESS_GOC_WRITE |
157 GMAC_MAC_MDIO_ADDRESS_GB;
158 WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
159
160 delay(10000);
161
162 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
163 addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
164 if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
165 break;
166 }
167 delay(10);
168 }
169 if (retry == 0) {
170 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
171 phy, reg);
172 return ETIMEDOUT;
173 }
174
175 return 0;
176 }
177
178 static void
179 eqos_update_link(struct eqos_softc *sc)
180 {
181 struct mii_data *mii = &sc->sc_mii;
182 uint64_t baudrate;
183 uint32_t conf;
184
185 baudrate = ifmedia_baudrate(mii->mii_media_active);
186
187 conf = RD4(sc, GMAC_MAC_CONFIGURATION);
188 switch (baudrate) {
189 case IF_Mbps(10):
190 conf |= GMAC_MAC_CONFIGURATION_PS;
191 conf &= ~GMAC_MAC_CONFIGURATION_FES;
192 break;
193 case IF_Mbps(100):
194 conf |= GMAC_MAC_CONFIGURATION_PS;
195 conf |= GMAC_MAC_CONFIGURATION_FES;
196 break;
197 case IF_Gbps(1):
198 conf &= ~GMAC_MAC_CONFIGURATION_PS;
199 conf &= ~GMAC_MAC_CONFIGURATION_FES;
200 break;
201 case IF_Mbps(2500ULL):
202 conf &= ~GMAC_MAC_CONFIGURATION_PS;
203 conf |= GMAC_MAC_CONFIGURATION_FES;
204 break;
205 }
206
207 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
208 conf |= GMAC_MAC_CONFIGURATION_DM;
209 } else {
210 conf &= ~GMAC_MAC_CONFIGURATION_DM;
211 }
212
213 WR4(sc, GMAC_MAC_CONFIGURATION, conf);
214 }
215
216 static void
217 eqos_mii_statchg(struct ifnet *ifp)
218 {
219 struct eqos_softc * const sc = ifp->if_softc;
220
221 eqos_update_link(sc);
222 }
223
224 static void
225 eqos_dma_sync(struct eqos_softc *sc, bus_dmamap_t map,
226 u_int start, u_int end, u_int total, int flags)
227 {
228 if (end > start) {
229 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
230 DESC_OFF(end) - DESC_OFF(start), flags);
231 } else {
232 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
233 DESC_OFF(total) - DESC_OFF(start), flags);
234 if (DESC_OFF(end) - DESC_OFF(0) > 0) {
235 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(0),
236 DESC_OFF(end) - DESC_OFF(0), flags);
237 }
238 }
239 }
240
241 static void
242 eqos_setup_txdesc(struct eqos_softc *sc, int index, int flags,
243 bus_addr_t paddr, u_int len, u_int total_len)
244 {
245 uint32_t tdes2, tdes3;
246
247 if (paddr == 0 || len == 0) {
248 KASSERT(flags == 0);
249 tdes2 = 0;
250 tdes3 = 0;
251 --sc->sc_tx.queued;
252 } else {
253 tdes2 = (flags & EQOS_TDES3_LD) ? EQOS_TDES2_IOC : 0;
254 tdes3 = flags;
255 ++sc->sc_tx.queued;
256 }
257
258 KASSERT(!EQOS_HW_FEATURE_ADDR64_32BIT(sc) || (paddr >> 32) == 0);
259
260 sc->sc_tx.desc_ring[index].tdes0 = htole32((uint32_t)paddr);
261 sc->sc_tx.desc_ring[index].tdes1 = htole32((uint32_t)(paddr >> 32));
262 sc->sc_tx.desc_ring[index].tdes2 = htole32(tdes2 | len);
263 sc->sc_tx.desc_ring[index].tdes3 = htole32(tdes3 | total_len);
264 }
265
266 static int
267 eqos_setup_txbuf(struct eqos_softc *sc, int index, struct mbuf *m)
268 {
269 bus_dma_segment_t *segs;
270 int error, nsegs, cur, i;
271 uint32_t flags;
272 bool nospace;
273
274 /* at least one descriptor free ? */
275 if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
276 return -1;
277
278 error = bus_dmamap_load_mbuf(sc->sc_dmat,
279 sc->sc_tx.buf_map[index].map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
280 if (error == EFBIG) {
281 device_printf(sc->sc_dev,
282 "TX packet needs too many DMA segments, dropping...\n");
283 return -2;
284 }
285 if (error != 0) {
286 device_printf(sc->sc_dev,
287 "TX packet cannot be mapped, retried...\n");
288 return 0;
289 }
290
291 segs = sc->sc_tx.buf_map[index].map->dm_segs;
292 nsegs = sc->sc_tx.buf_map[index].map->dm_nsegs;
293
294 nospace = sc->sc_tx.queued >= TX_DESC_COUNT - nsegs;
295 if (nospace) {
296 bus_dmamap_unload(sc->sc_dmat,
297 sc->sc_tx.buf_map[index].map);
298 /* XXX coalesce and retry ? */
299 return -1;
300 }
301
302 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.buf_map[index].map,
303 0, sc->sc_tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
304
305 /* stored in same index as loaded map */
306 sc->sc_tx.buf_map[index].mbuf = m;
307
308 flags = EQOS_TDES3_FD;
309
310 for (cur = index, i = 0; i < nsegs; i++) {
311 if (i == nsegs - 1)
312 flags |= EQOS_TDES3_LD;
313
314 eqos_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
315 segs[i].ds_len, m->m_pkthdr.len);
316 flags &= ~EQOS_TDES3_FD;
317 cur = TX_NEXT(cur);
318
319 flags |= EQOS_TDES3_OWN;
320 }
321
322 /*
323 * Defer setting OWN bit on the first descriptor until all
324 * descriptors have been updated.
325 */
326 membar_sync();
327 sc->sc_tx.desc_ring[index].tdes3 |= htole32(EQOS_TDES3_OWN);
328
329 return nsegs;
330 }
331
332 static void
333 eqos_setup_rxdesc(struct eqos_softc *sc, int index, bus_addr_t paddr)
334 {
335 sc->sc_rx.desc_ring[index].tdes0 = htole32((uint32_t)paddr);
336 sc->sc_rx.desc_ring[index].tdes1 = htole32((uint32_t)(paddr >> 32));
337 sc->sc_rx.desc_ring[index].tdes2 = htole32(0);
338 membar_sync();
339 sc->sc_rx.desc_ring[index].tdes3 =
340 htole32(EQOS_TDES3_OWN | EQOS_TDES3_IOC | EQOS_TDES3_BUF1V);
341 }
342
343 static int
344 eqos_setup_rxbuf(struct eqos_softc *sc, int index, struct mbuf *m)
345 {
346 int error;
347
348 m_adj(m, ETHER_ALIGN);
349
350 error = bus_dmamap_load_mbuf(sc->sc_dmat,
351 sc->sc_rx.buf_map[index].map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
352 if (error != 0)
353 return error;
354
355 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
356 0, sc->sc_rx.buf_map[index].map->dm_mapsize,
357 BUS_DMASYNC_PREREAD);
358
359 sc->sc_rx.buf_map[index].mbuf = m;
360 eqos_setup_rxdesc(sc, index,
361 sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr);
362
363 return 0;
364 }
365
366 static struct mbuf *
367 eqos_alloc_mbufcl(struct eqos_softc *sc)
368 {
369 struct mbuf *m;
370
371 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
372 if (m != NULL)
373 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
374
375 return m;
376 }
377
378 static void
379 eqos_enable_intr(struct eqos_softc *sc)
380 {
381 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE,
382 GMAC_DMA_CHAN0_INTR_ENABLE_NIE |
383 GMAC_DMA_CHAN0_INTR_ENABLE_AIE |
384 GMAC_DMA_CHAN0_INTR_ENABLE_FBE |
385 GMAC_DMA_CHAN0_INTR_ENABLE_RIE |
386 GMAC_DMA_CHAN0_INTR_ENABLE_TIE);
387 }
388
389 static void
390 eqos_disable_intr(struct eqos_softc *sc)
391 {
392 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, 0);
393 }
394
395 static void
396 eqos_tick(void *softc)
397 {
398 struct eqos_softc *sc = softc;
399 struct mii_data *mii = &sc->sc_mii;
400 #ifndef EQOS_MPSAFE
401 int s = splnet();
402 #endif
403
404 EQOS_LOCK(sc);
405 mii_tick(mii);
406 callout_schedule(&sc->sc_stat_ch, hz);
407 EQOS_UNLOCK(sc);
408
409 #ifndef EQOS_MPSAFE
410 splx(s);
411 #endif
412 }
413
414 static uint32_t
415 eqos_bitrev32(uint32_t x)
416 {
417 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
418 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
419 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
420 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
421
422 return (x >> 16) | (x << 16);
423 }
424
425 static void
426 eqos_setup_rxfilter(struct eqos_softc *sc)
427 {
428 struct ethercom *ec = &sc->sc_ec;
429 struct ifnet *ifp = &ec->ec_if;
430 uint32_t pfil, crc, hashreg, hashbit, hash[2];
431 struct ether_multi *enm;
432 struct ether_multistep step;
433 const uint8_t *eaddr;
434 uint32_t val;
435
436 EQOS_ASSERT_LOCKED(sc);
437
438 pfil = RD4(sc, GMAC_MAC_PACKET_FILTER);
439 pfil &= ~(GMAC_MAC_PACKET_FILTER_PR |
440 GMAC_MAC_PACKET_FILTER_PM |
441 GMAC_MAC_PACKET_FILTER_HMC |
442 GMAC_MAC_PACKET_FILTER_PCF_MASK);
443 hash[0] = hash[1] = ~0U;
444
445 if ((ifp->if_flags & IFF_PROMISC) != 0) {
446 pfil |= GMAC_MAC_PACKET_FILTER_PR |
447 GMAC_MAC_PACKET_FILTER_PCF_ALL;
448 } else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
449 pfil |= GMAC_MAC_PACKET_FILTER_PM;
450 } else {
451 hash[0] = hash[1] = 0;
452 pfil |= GMAC_MAC_PACKET_FILTER_HMC;
453 ETHER_LOCK(ec);
454 ETHER_FIRST_MULTI(step, ec, enm);
455 while (enm != NULL) {
456 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
457 crc &= 0x7f;
458 crc = eqos_bitrev32(~crc) >> 26;
459 hashreg = (crc >> 5);
460 hashbit = (crc & 0x1f);
461 hash[hashreg] |= (1 << hashbit);
462 ETHER_NEXT_MULTI(step, enm);
463 }
464 ETHER_UNLOCK(ec);
465 }
466
467 /* Write our unicast address */
468 eaddr = CLLADDR(ifp->if_sadl);
469 val = eaddr[4] | (eaddr[5] << 8);
470 WR4(sc, GMAC_MAC_ADDRESS0_HIGH, val);
471 val = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
472 (eaddr[3] << 24);
473 WR4(sc, GMAC_MAC_ADDRESS0_LOW, val);
474
475 /* Multicast hash filters */
476 WR4(sc, GMAC_MAC_HASH_TABLE_REG0, hash[1]);
477 WR4(sc, GMAC_MAC_HASH_TABLE_REG1, hash[0]);
478
479 /* Packet filter config */
480 WR4(sc, GMAC_MAC_PACKET_FILTER, pfil);
481 }
482
483 static int
484 eqos_reset(struct eqos_softc *sc)
485 {
486 uint32_t val;
487 int retry;
488
489 WR4(sc, GMAC_DMA_MODE, GMAC_DMA_MODE_SWR);
490 for (retry = 2000; retry > 0; retry--) {
491 delay(1000);
492 val = RD4(sc, GMAC_DMA_MODE);
493 if ((val & GMAC_DMA_MODE_SWR) == 0) {
494 return 0;
495 }
496 }
497
498 device_printf(sc->sc_dev, "reset timeout!\n");
499 return ETIMEDOUT;
500 }
501
502 static void
503 eqos_init_rings(struct eqos_softc *sc, int qid)
504 {
505 sc->sc_tx.queued = 0;
506
507 WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR_HI,
508 (uint32_t)(sc->sc_tx.desc_ring_paddr >> 32));
509 WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR,
510 (uint32_t)sc->sc_tx.desc_ring_paddr);
511 WR4(sc, GMAC_DMA_CHAN0_TX_RING_LEN, TX_DESC_COUNT - 1);
512
513 WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR_HI,
514 (uint32_t)(sc->sc_rx.desc_ring_paddr >> 32));
515 WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR,
516 (uint32_t)sc->sc_rx.desc_ring_paddr);
517 WR4(sc, GMAC_DMA_CHAN0_RX_RING_LEN, RX_DESC_COUNT - 1);
518 WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
519 (uint32_t)sc->sc_rx.desc_ring_paddr +
520 DESC_OFF((sc->sc_rx.cur - 1) % RX_DESC_COUNT));
521 }
522
523 static int
524 eqos_init_locked(struct eqos_softc *sc)
525 {
526 struct ifnet *ifp = &sc->sc_ec.ec_if;
527 struct mii_data *mii = &sc->sc_mii;
528 uint32_t val;
529
530 EQOS_ASSERT_LOCKED(sc);
531 EQOS_ASSERT_TXLOCKED(sc);
532
533 if ((ifp->if_flags & IFF_RUNNING) != 0)
534 return 0;
535
536 /* Setup TX/RX rings */
537 eqos_init_rings(sc, 0);
538
539 /* Setup RX filter */
540 eqos_setup_rxfilter(sc);
541
542 WR4(sc, GMAC_MAC_1US_TIC_COUNTER, (sc->sc_csr_clock / 1000000) - 1);
543
544 /* Enable transmit and receive DMA */
545 val = RD4(sc, GMAC_DMA_CHAN0_CONTROL);
546 val &= ~GMAC_DMA_CHAN0_CONTROL_DSL_MASK;
547 val |= ((DESC_ALIGN - 16) / 8) << GMAC_DMA_CHAN0_CONTROL_DSL_SHIFT;
548 val |= GMAC_DMA_CHAN0_CONTROL_PBLX8;
549 WR4(sc, GMAC_DMA_CHAN0_CONTROL, val);
550 val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
551 val |= GMAC_DMA_CHAN0_TX_CONTROL_OSP;
552 val |= GMAC_DMA_CHAN0_TX_CONTROL_START;
553 WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
554 val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
555 val &= ~GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_MASK;
556 val |= (MCLBYTES << GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_SHIFT);
557 val |= GMAC_DMA_CHAN0_RX_CONTROL_START;
558 WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
559
560 /* Configure operation modes */
561 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE,
562 GMAC_MTL_TXQ0_OPERATION_MODE_TSF |
563 GMAC_MTL_TXQ0_OPERATION_MODE_TXQEN_EN);
564 WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE,
565 GMAC_MTL_RXQ0_OPERATION_MODE_RSF |
566 GMAC_MTL_RXQ0_OPERATION_MODE_FEP |
567 GMAC_MTL_RXQ0_OPERATION_MODE_FUP);
568
569 /* Enable flow control */
570 val = RD4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL);
571 val |= 0xFFFFU << GMAC_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT;
572 val |= GMAC_MAC_Q0_TX_FLOW_CTRL_TFE;
573 WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, val);
574 val = RD4(sc, GMAC_MAC_RX_FLOW_CTRL);
575 val |= GMAC_MAC_RX_FLOW_CTRL_RFE;
576 WR4(sc, GMAC_MAC_RX_FLOW_CTRL, val);
577
578 /* Enable transmitter and receiver */
579 val = RD4(sc, GMAC_MAC_CONFIGURATION);
580 val |= GMAC_MAC_CONFIGURATION_BE;
581 val |= GMAC_MAC_CONFIGURATION_JD;
582 val |= GMAC_MAC_CONFIGURATION_JE;
583 val |= GMAC_MAC_CONFIGURATION_DCRS;
584 val |= GMAC_MAC_CONFIGURATION_TE;
585 val |= GMAC_MAC_CONFIGURATION_RE;
586 WR4(sc, GMAC_MAC_CONFIGURATION, val);
587
588 /* Enable interrupts */
589 eqos_enable_intr(sc);
590
591 ifp->if_flags |= IFF_RUNNING;
592 ifp->if_flags &= ~IFF_OACTIVE;
593
594 mii_mediachg(mii);
595 callout_schedule(&sc->sc_stat_ch, hz);
596
597 return 0;
598 }
599
600 static int
601 eqos_init(struct ifnet *ifp)
602 {
603 struct eqos_softc *sc = ifp->if_softc;
604 int error;
605
606 EQOS_LOCK(sc);
607 EQOS_TXLOCK(sc);
608 error = eqos_init_locked(sc);
609 EQOS_TXUNLOCK(sc);
610 EQOS_UNLOCK(sc);
611
612 return error;
613 }
614
615 static void
616 eqos_stop_locked(struct eqos_softc *sc, int disable)
617 {
618 struct ifnet *ifp = &sc->sc_ec.ec_if;
619 uint32_t val;
620 int retry;
621
622 EQOS_ASSERT_LOCKED(sc);
623
624 callout_stop(&sc->sc_stat_ch);
625
626 mii_down(&sc->sc_mii);
627
628 /* Disable receiver */
629 val = RD4(sc, GMAC_MAC_CONFIGURATION);
630 val &= ~GMAC_MAC_CONFIGURATION_RE;
631 WR4(sc, GMAC_MAC_CONFIGURATION, val);
632
633 /* Stop receive DMA */
634 val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
635 val &= ~GMAC_DMA_CHAN0_RX_CONTROL_START;
636 WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
637
638 /* Stop transmit DMA */
639 val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
640 val &= ~GMAC_DMA_CHAN0_TX_CONTROL_START;
641 WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
642
643 if (disable) {
644 /* Flush data in the TX FIFO */
645 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
646 val |= GMAC_MTL_TXQ0_OPERATION_MODE_FTQ;
647 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
648 /* Wait for flush to complete */
649 for (retry = 10000; retry > 0; retry--) {
650 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
651 if ((val & GMAC_MTL_TXQ0_OPERATION_MODE_FTQ) == 0) {
652 break;
653 }
654 delay(1);
655 }
656 if (retry == 0) {
657 device_printf(sc->sc_dev,
658 "timeout flushing TX queue\n");
659 }
660 }
661
662 /* Disable transmitter */
663 val = RD4(sc, GMAC_MAC_CONFIGURATION);
664 val &= ~GMAC_MAC_CONFIGURATION_TE;
665 WR4(sc, GMAC_MAC_CONFIGURATION, val);
666
667 /* Disable interrupts */
668 eqos_disable_intr(sc);
669
670 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
671 }
672
673 static void
674 eqos_stop(struct ifnet *ifp, int disable)
675 {
676 struct eqos_softc * const sc = ifp->if_softc;
677
678 EQOS_LOCK(sc);
679 eqos_stop_locked(sc, disable);
680 EQOS_UNLOCK(sc);
681 }
682
683 static void
684 eqos_rxintr(struct eqos_softc *sc, int qid)
685 {
686 struct ifnet *ifp = &sc->sc_ec.ec_if;
687 int error, index, len, pkts = 0;
688 struct mbuf *m, *m0;
689 uint32_t tdes3;
690
691 for (index = sc->sc_rx.cur; ; index = RX_NEXT(index)) {
692 eqos_dma_sync(sc, sc->sc_rx.desc_map,
693 index, index + 1, RX_DESC_COUNT,
694 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
695
696 tdes3 = le32toh(sc->sc_rx.desc_ring[index].tdes3);
697 if ((tdes3 & EQOS_TDES3_OWN) != 0) {
698 break;
699 }
700
701 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
702 0, sc->sc_rx.buf_map[index].map->dm_mapsize,
703 BUS_DMASYNC_POSTREAD);
704 bus_dmamap_unload(sc->sc_dmat,
705 sc->sc_rx.buf_map[index].map);
706
707 len = tdes3 & EQOS_TDES3_LENGTH_MASK;
708 if (len != 0) {
709 m = sc->sc_rx.buf_map[index].mbuf;
710 m_set_rcvif(m, ifp);
711 m->m_flags |= M_HASFCS;
712 m->m_pkthdr.len = len;
713 m->m_len = len;
714 m->m_nextpkt = NULL;
715
716 if_percpuq_enqueue(ifp->if_percpuq, m);
717 ++pkts;
718 }
719
720 if ((m0 = eqos_alloc_mbufcl(sc)) != NULL) {
721 error = eqos_setup_rxbuf(sc, index, m0);
722 if (error != 0) {
723 /* XXX hole in RX ring */
724 }
725 } else {
726 if_statinc(ifp, if_ierrors);
727 }
728 eqos_dma_sync(sc, sc->sc_rx.desc_map,
729 index, index + 1, RX_DESC_COUNT,
730 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
731
732 WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
733 (uint32_t)sc->sc_rx.desc_ring_paddr +
734 DESC_OFF(sc->sc_rx.cur));
735 }
736
737 sc->sc_rx.cur = index;
738
739 if (pkts != 0) {
740 rnd_add_uint32(&sc->sc_rndsource, pkts);
741 }
742 }
743
744 static void
745 eqos_txintr(struct eqos_softc *sc, int qid)
746 {
747 struct ifnet *ifp = &sc->sc_ec.ec_if;
748 struct eqos_bufmap *bmap;
749 struct eqos_dma_desc *desc;
750 uint32_t tdes3;
751 int i, pkts = 0;
752
753 EQOS_ASSERT_LOCKED(sc);
754
755 for (i = sc->sc_tx.next; sc->sc_tx.queued > 0; i = TX_NEXT(i)) {
756 KASSERT(sc->sc_tx.queued > 0);
757 KASSERT(sc->sc_tx.queued <= TX_DESC_COUNT);
758 eqos_dma_sync(sc, sc->sc_tx.desc_map,
759 i, i + 1, TX_DESC_COUNT,
760 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
761 desc = &sc->sc_tx.desc_ring[i];
762 tdes3 = le32toh(desc->tdes3);
763 if ((tdes3 & EQOS_TDES3_OWN) != 0) {
764 break;
765 }
766 bmap = &sc->sc_tx.buf_map[i];
767 if (bmap->mbuf != NULL) {
768 bus_dmamap_sync(sc->sc_dmat, bmap->map,
769 0, bmap->map->dm_mapsize,
770 BUS_DMASYNC_POSTWRITE);
771 bus_dmamap_unload(sc->sc_dmat, bmap->map);
772 m_freem(bmap->mbuf);
773 bmap->mbuf = NULL;
774 ++pkts;
775 }
776
777 eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
778 eqos_dma_sync(sc, sc->sc_tx.desc_map,
779 i, i + 1, TX_DESC_COUNT,
780 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
781
782 ifp->if_flags &= ~IFF_OACTIVE;
783
784 /* Last descriptor in a packet contains DMA status */
785 if ((tdes3 & EQOS_TDES3_LD) != 0) {
786 if ((tdes3 & EQOS_TDES3_DE) != 0) {
787 device_printf(sc->sc_dev,
788 "TX [%u] desc error: 0x%08x\n",
789 i, tdes3);
790 if_statinc(ifp, if_oerrors);
791 } else if ((tdes3 & EQOS_TDES3_ES) != 0) {
792 device_printf(sc->sc_dev,
793 "TX [%u] tx error: 0x%08x\n",
794 i, tdes3);
795 if_statinc(ifp, if_oerrors);
796 } else {
797 if_statinc(ifp, if_opackets);
798 }
799 }
800
801 }
802
803 sc->sc_tx.next = i;
804
805 if (pkts != 0) {
806 rnd_add_uint32(&sc->sc_rndsource, pkts);
807 }
808 }
809
810 static void
811 eqos_start_locked(struct eqos_softc *sc)
812 {
813 struct ifnet *ifp = &sc->sc_ec.ec_if;
814 struct mbuf *m;
815 int cnt, nsegs, start;
816
817 EQOS_ASSERT_TXLOCKED(sc);
818
819 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
820 return;
821
822 for (cnt = 0, start = sc->sc_tx.cur; ; cnt++) {
823 if (sc->sc_tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
824 ifp->if_flags |= IFF_OACTIVE;
825 break;
826 }
827
828 IFQ_POLL(&ifp->if_snd, m);
829 if (m == NULL) {
830 break;
831 }
832
833 nsegs = eqos_setup_txbuf(sc, sc->sc_tx.cur, m);
834 if (nsegs <= 0) {
835 if (nsegs == -1) {
836 ifp->if_flags |= IFF_OACTIVE;
837 } else if (nsegs == -2) {
838 IFQ_DEQUEUE(&ifp->if_snd, m);
839 m_freem(m);
840 }
841 break;
842 }
843
844 IFQ_DEQUEUE(&ifp->if_snd, m);
845 bpf_mtap(ifp, m, BPF_D_OUT);
846
847 sc->sc_tx.cur = TX_SKIP(sc->sc_tx.cur, nsegs);
848 }
849
850 if (cnt != 0) {
851 eqos_dma_sync(sc, sc->sc_tx.desc_map,
852 start, sc->sc_tx.cur, TX_DESC_COUNT,
853 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
854
855 /* Start and run TX DMA */
856 WR4(sc, GMAC_DMA_CHAN0_TX_END_ADDR,
857 (uint32_t)sc->sc_tx.desc_ring_paddr +
858 DESC_OFF(sc->sc_tx.cur));
859 }
860 }
861
862 static void
863 eqos_start(struct ifnet *ifp)
864 {
865 struct eqos_softc *sc = ifp->if_softc;
866
867 EQOS_TXLOCK(sc);
868 eqos_start_locked(sc);
869 EQOS_TXUNLOCK(sc);
870 }
871
872 int
873 eqos_intr(void *arg)
874 {
875 struct eqos_softc *sc = arg;
876 struct ifnet *ifp = &sc->sc_ec.ec_if;
877 uint32_t mac_status, mtl_status, dma_status, rx_tx_status;
878
879 mac_status = RD4(sc, GMAC_MAC_INTERRUPT_STATUS);
880 mac_status &= RD4(sc, GMAC_MAC_INTERRUPT_ENABLE);
881
882 if (mac_status) {
883 device_printf(sc->sc_dev,
884 "GMAC_MAC_INTERRUPT_STATUS = 0x%08X\n", mac_status);
885 }
886
887 mtl_status = RD4(sc, GMAC_MTL_INTERRUPT_STATUS);
888 if (mtl_status) {
889 device_printf(sc->sc_dev,
890 "GMAC_MTL_INTERRUPT_STATUS = 0x%08X\n", mtl_status);
891 }
892
893 dma_status = RD4(sc, GMAC_DMA_CHAN0_STATUS);
894 dma_status &= RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
895 if (dma_status) {
896 WR4(sc, GMAC_DMA_CHAN0_STATUS, dma_status);
897 }
898
899 EQOS_LOCK(sc);
900 if ((dma_status & GMAC_DMA_CHAN0_STATUS_RI) != 0) {
901 eqos_rxintr(sc, 0);
902 dma_status &= ~GMAC_DMA_CHAN0_STATUS_RI;
903 }
904
905 if ((dma_status & GMAC_DMA_CHAN0_STATUS_TI) != 0) {
906 eqos_txintr(sc, 0);
907 dma_status &= ~GMAC_DMA_CHAN0_STATUS_TI;
908 if_schedule_deferred_start(ifp);
909 }
910 EQOS_UNLOCK(sc);
911
912 if ((mac_status | mtl_status | dma_status) == 0) {
913 device_printf(sc->sc_dev, "spurious interrupt?!\n");
914 }
915
916 rx_tx_status = RD4(sc, GMAC_MAC_RX_TX_STATUS);
917 if (rx_tx_status) {
918 device_printf(sc->sc_dev, "GMAC_MAC_RX_TX_STATUS = 0x%08x\n",
919 rx_tx_status);
920 }
921
922 return 1;
923 }
924
925 static int
926 eqos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
927 {
928 struct eqos_softc *sc = ifp->if_softc;
929 int error, s;
930
931 #ifndef EQOS_MPSAFE
932 s = splnet();
933 #endif
934
935 switch (cmd) {
936 default:
937 #ifdef EQOS_MPSAFE
938 s = splnet();
939 #endif
940 error = ether_ioctl(ifp, cmd, data);
941 #ifdef EQOS_MPSAFE
942 splx(s);
943 #endif
944 if (error != ENETRESET)
945 break;
946
947 error = 0;
948
949 if (cmd == SIOCSIFCAP)
950 error = (*ifp->if_init)(ifp);
951 else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
952 ;
953 else if ((ifp->if_flags & IFF_RUNNING) != 0) {
954 EQOS_LOCK(sc);
955 eqos_setup_rxfilter(sc);
956 EQOS_UNLOCK(sc);
957 }
958 break;
959 }
960
961 #ifndef EQOS_MPSAFE
962 splx(s);
963 #endif
964
965 return error;
966 }
967
968 static void
969 eqos_get_eaddr(struct eqos_softc *sc, uint8_t *eaddr)
970 {
971 prop_dictionary_t prop = device_properties(sc->sc_dev);
972 uint32_t maclo, machi;
973 prop_data_t eaprop;
974
975 eaprop = prop_dictionary_get(prop, "mac-address");
976 if (eaprop != NULL) {
977 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
978 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
979 memcpy(eaddr, prop_data_value(eaprop),
980 ETHER_ADDR_LEN);
981 return;
982 }
983
984 maclo = htobe32(RD4(sc, GMAC_MAC_ADDRESS0_LOW));
985 machi = htobe16(RD4(sc, GMAC_MAC_ADDRESS0_HIGH) & 0xFFFF);
986
987 if (maclo == 0xFFFFFFFF && machi == 0xFFFF) {
988 /* Create one */
989 maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
990 machi = cprng_strong32() & 0xffff;
991 }
992
993 eaddr[0] = maclo & 0xff;
994 eaddr[1] = (maclo >> 8) & 0xff;
995 eaddr[2] = (maclo >> 16) & 0xff;
996 eaddr[3] = (maclo >> 24) & 0xff;
997 eaddr[4] = machi & 0xff;
998 eaddr[5] = (machi >> 8) & 0xff;
999 }
1000
1001 static void
1002 eqos_axi_configure(struct eqos_softc *sc)
1003 {
1004 prop_dictionary_t prop = device_properties(sc->sc_dev);
1005 uint32_t val;
1006 u_int uival;
1007 bool bval;
1008
1009 val = RD4(sc, GMAC_DMA_SYSBUS_MODE);
1010 if (prop_dictionary_get_bool(prop, "snps,mixed-burst", &bval) && bval) {
1011 val |= GMAC_DMA_SYSBUS_MODE_MB;
1012 }
1013 if (prop_dictionary_get_bool(prop, "snps,fixed-burst", &bval) && bval) {
1014 val |= GMAC_DMA_SYSBUS_MODE_FB;
1015 }
1016 if (prop_dictionary_get_uint(prop, "snps,wr_osr_lmt", &uival)) {
1017 val &= ~GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
1018 val |= uival << GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_SHIFT;
1019 }
1020 if (prop_dictionary_get_uint(prop, "snps,rd_osr_lmt", &uival)) {
1021 val &= ~GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
1022 val |= uival << GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT;
1023 }
1024
1025 if (!EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
1026 val |= GMAC_DMA_SYSBUS_MODE_EAME;
1027 }
1028
1029 /* XXX */
1030 val |= GMAC_DMA_SYSBUS_MODE_BLEN16;
1031 val |= GMAC_DMA_SYSBUS_MODE_BLEN8;
1032 val |= GMAC_DMA_SYSBUS_MODE_BLEN4;
1033
1034 WR4(sc, GMAC_DMA_SYSBUS_MODE, val);
1035 }
1036
1037 static int
1038 eqos_setup_dma(struct eqos_softc *sc, int qid)
1039 {
1040 struct mbuf *m;
1041 int error, nsegs, i;
1042
1043 /* Setup TX ring */
1044 error = bus_dmamap_create(sc->sc_dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE,
1045 DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_tx.desc_map);
1046 if (error) {
1047 return error;
1048 }
1049 error = bus_dmamem_alloc(sc->sc_dmat, TX_DESC_SIZE, DESC_ALIGN,
1050 DESC_BOUNDARY, &sc->sc_tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1051 if (error) {
1052 return error;
1053 }
1054 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_tx.desc_dmaseg, nsegs,
1055 TX_DESC_SIZE, (void *)&sc->sc_tx.desc_ring, BUS_DMA_WAITOK);
1056 if (error) {
1057 return error;
1058 }
1059 error = bus_dmamap_load(sc->sc_dmat, sc->sc_tx.desc_map,
1060 sc->sc_tx.desc_ring, TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1061 if (error) {
1062 return error;
1063 }
1064 sc->sc_tx.desc_ring_paddr = sc->sc_tx.desc_map->dm_segs[0].ds_addr;
1065
1066 memset(sc->sc_tx.desc_ring, 0, TX_DESC_SIZE);
1067 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map, 0, TX_DESC_SIZE,
1068 BUS_DMASYNC_PREWRITE);
1069
1070 sc->sc_tx.queued = TX_DESC_COUNT;
1071 for (i = 0; i < TX_DESC_COUNT; i++) {
1072 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1073 TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
1074 &sc->sc_tx.buf_map[i].map);
1075 if (error != 0) {
1076 device_printf(sc->sc_dev,
1077 "cannot create TX buffer map\n");
1078 return error;
1079 }
1080 eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
1081 }
1082
1083 /* Setup RX ring */
1084 error = bus_dmamap_create(sc->sc_dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE,
1085 DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_rx.desc_map);
1086 if (error) {
1087 return error;
1088 }
1089 error = bus_dmamem_alloc(sc->sc_dmat, RX_DESC_SIZE, DESC_ALIGN,
1090 DESC_BOUNDARY, &sc->sc_rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1091 if (error) {
1092 return error;
1093 }
1094 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rx.desc_dmaseg, nsegs,
1095 RX_DESC_SIZE, (void *)&sc->sc_rx.desc_ring, BUS_DMA_WAITOK);
1096 if (error) {
1097 return error;
1098 }
1099 error = bus_dmamap_load(sc->sc_dmat, sc->sc_rx.desc_map,
1100 sc->sc_rx.desc_ring, RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1101 if (error) {
1102 return error;
1103 }
1104 sc->sc_rx.desc_ring_paddr = sc->sc_rx.desc_map->dm_segs[0].ds_addr;
1105
1106 memset(sc->sc_rx.desc_ring, 0, RX_DESC_SIZE);
1107
1108 for (i = 0; i < RX_DESC_COUNT; i++) {
1109 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1110 RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
1111 &sc->sc_rx.buf_map[i].map);
1112 if (error != 0) {
1113 device_printf(sc->sc_dev,
1114 "cannot create RX buffer map\n");
1115 return error;
1116 }
1117 if ((m = eqos_alloc_mbufcl(sc)) == NULL) {
1118 device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
1119 return ENOMEM;
1120 }
1121 error = eqos_setup_rxbuf(sc, i, m);
1122 if (error != 0) {
1123 device_printf(sc->sc_dev, "cannot create RX buffer\n");
1124 return error;
1125 }
1126 }
1127 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
1128 0, sc->sc_rx.desc_map->dm_mapsize,
1129 BUS_DMASYNC_PREWRITE);
1130
1131 aprint_debug_dev(sc->sc_dev, "TX ring @ 0x%lX, RX ring @ 0x%lX\n",
1132 sc->sc_tx.desc_ring_paddr, sc->sc_rx.desc_ring_paddr);
1133
1134 return 0;
1135 }
1136
1137 int
1138 eqos_attach(struct eqos_softc *sc)
1139 {
1140 struct mii_data *mii = &sc->sc_mii;
1141 struct ifnet *ifp = &sc->sc_ec.ec_if;
1142 uint8_t eaddr[ETHER_ADDR_LEN];
1143 u_int userver, snpsver;
1144 int mii_flags = 0;
1145 int error;
1146 int n;
1147
1148 const uint32_t ver = RD4(sc, GMAC_MAC_VERSION);
1149 userver = (ver & GMAC_MAC_VERSION_USERVER_MASK) >>
1150 GMAC_MAC_VERSION_USERVER_SHIFT;
1151 snpsver = ver & GMAC_MAC_VERSION_SNPSVER_MASK;
1152
1153 if (snpsver != 0x51) {
1154 aprint_error(": EQOS version 0x%02xx not supported\n",
1155 snpsver);
1156 return ENXIO;
1157 }
1158
1159 if (sc->sc_csr_clock < 20000000) {
1160 aprint_error(": CSR clock too low\n");
1161 return EINVAL;
1162 } else if (sc->sc_csr_clock < 35000000) {
1163 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_20_35;
1164 } else if (sc->sc_csr_clock < 60000000) {
1165 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_35_60;
1166 } else if (sc->sc_csr_clock < 100000000) {
1167 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_60_100;
1168 } else if (sc->sc_csr_clock < 150000000) {
1169 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
1170 } else if (sc->sc_csr_clock < 250000000) {
1171 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_150_250;
1172 } else if (sc->sc_csr_clock < 300000000) {
1173 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_300_500;
1174 } else if (sc->sc_csr_clock < 800000000) {
1175 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_500_800;
1176 } else {
1177 aprint_error(": CSR clock too high\n");
1178 return EINVAL;
1179 }
1180
1181 for (n = 0; n < 4; n++) {
1182 sc->sc_hw_feature[n] = RD4(sc, GMAC_MAC_HW_FEATURE(n));
1183 }
1184
1185 aprint_naive("\n");
1186 aprint_normal(": DesignWare EQOS ver 0x%02x (0x%02x)\n",
1187 snpsver, userver);
1188 aprint_verbose_dev(sc->sc_dev, "hw features %08x %08x %08x %08x\n",
1189 sc->sc_hw_feature[0], sc->sc_hw_feature[1],
1190 sc->sc_hw_feature[2], sc->sc_hw_feature[3]);
1191
1192 if (EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
1193 bus_dma_tag_t ntag;
1194
1195 error = bus_dmatag_subregion(sc->sc_dmat, 0, UINT32_MAX,
1196 &ntag, 0);
1197 if (error) {
1198 aprint_error_dev(sc->sc_dev,
1199 "failed to restrict DMA: %d\n", error);
1200 return error;
1201 }
1202 aprint_verbose_dev(sc->sc_dev, "using 32-bit DMA\n");
1203 sc->sc_dmat = ntag;
1204 }
1205
1206 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
1207 mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
1208 callout_init(&sc->sc_stat_ch, CALLOUT_FLAGS);
1209 callout_setfunc(&sc->sc_stat_ch, eqos_tick, sc);
1210
1211 eqos_get_eaddr(sc, eaddr);
1212 aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n", ether_sprintf(eaddr));
1213
1214 /* Soft reset EMAC core */
1215 error = eqos_reset(sc);
1216 if (error != 0) {
1217 return error;
1218 }
1219
1220 /* Configure AXI Bus mode parameters */
1221 eqos_axi_configure(sc);
1222
1223 /* Setup DMA descriptors */
1224 if (eqos_setup_dma(sc, 0) != 0) {
1225 aprint_error_dev(sc->sc_dev, "failed to setup DMA descriptors\n");
1226 return EINVAL;
1227 }
1228
1229 /* Setup ethernet interface */
1230 ifp->if_softc = sc;
1231 snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
1232 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1233 #ifdef EQOS_MPSAFE
1234 ifp->if_extflags = IFEF_MPSAFE;
1235 #endif
1236 ifp->if_start = eqos_start;
1237 ifp->if_ioctl = eqos_ioctl;
1238 ifp->if_init = eqos_init;
1239 ifp->if_stop = eqos_stop;
1240 ifp->if_capabilities = 0;
1241 ifp->if_capenable = ifp->if_capabilities;
1242 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
1243 IFQ_SET_READY(&ifp->if_snd);
1244
1245 /* 802.1Q VLAN-sized frames are supported */
1246 sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
1247
1248 /* Attach MII driver */
1249 sc->sc_ec.ec_mii = mii;
1250 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
1251 mii->mii_ifp = ifp;
1252 mii->mii_readreg = eqos_mii_readreg;
1253 mii->mii_writereg = eqos_mii_writereg;
1254 mii->mii_statchg = eqos_mii_statchg;
1255 mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
1256 mii_flags);
1257
1258 if (LIST_EMPTY(&mii->mii_phys)) {
1259 aprint_error_dev(sc->sc_dev, "no PHY found!\n");
1260 return ENOENT;
1261 }
1262 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1263
1264 /* Attach interface */
1265 if_attach(ifp);
1266 if_deferred_start_init(ifp, NULL);
1267
1268 /* Attach ethernet interface */
1269 ether_ifattach(ifp, eaddr);
1270
1271 rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
1272 RND_FLAG_DEFAULT);
1273
1274 return 0;
1275 }
1276