if_lii.c revision 1.4.4.2 1 /* $NetBSD: if_lii.c,v 1.4.4.2 2008/06/03 20:47:24 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation.
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 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Driver for Attansic/Atheros's L2 Fast Ethernet controller
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_lii.c,v 1.4.4.2 2008/06/03 20:47:24 skrll Exp $");
38
39 #include "bpfilter.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
44 #include <sys/device.h>
45 #include <sys/endian.h>
46 #include <sys/kernel.h>
47 #include <sys/sockio.h>
48
49 #include <net/if.h>
50 #include <net/if_media.h>
51 #include <net/if_ether.h>
52
53 #if NBPFILTER > 0
54 #include <net/bpf.h>
55 #endif
56
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcidevs.h>
63
64 #include <dev/pci/if_liireg.h>
65
66 /* #define LII_DEBUG */
67 #ifdef LII_DEBUG
68 #define DPRINTF(x) printf x
69 #else
70 #define DPRINTF(x)
71 #endif
72
73 struct lii_softc {
74 struct device sc_dev;
75 pci_chipset_tag_t sc_pc;
76 pcitag_t sc_tag;
77
78 bus_space_tag_t sc_mmiot;
79 bus_space_handle_t sc_mmioh;
80
81 /*
82 * We allocate a big chunk of DMA-safe memory for all data exchanges.
83 * It is unfortunate that this chip doesn't seem to do scatter-gather.
84 */
85 bus_dma_tag_t sc_dmat;
86 bus_dmamap_t sc_ringmap;
87 bus_dma_segment_t sc_ringseg;
88
89 uint8_t *sc_ring; /* the whole area */
90 size_t sc_ringsize;
91
92 struct rx_pkt *sc_rxp; /* the part used for RX */
93 struct tx_pkt_status *sc_txs; /* the parts used for TX */
94 bus_addr_t sc_txsp;
95 char *sc_txdbase;
96 bus_addr_t sc_txdp;
97
98 unsigned int sc_rxcur;
99 /* the active area is [ack; cur[ */
100 int sc_txs_cur;
101 int sc_txs_ack;
102 int sc_txd_cur;
103 int sc_txd_ack;
104 bool sc_free_tx_slots;
105
106 void *sc_ih;
107
108 struct ethercom sc_ec;
109 struct mii_data sc_mii;
110 struct callout sc_tick_ch;
111 uint8_t sc_eaddr[ETHER_ADDR_LEN];
112
113 int (*sc_memread)(struct lii_softc *, uint32_t,
114 uint32_t *);
115 };
116
117 static int lii_match(device_t, cfdata_t, void *);
118 static void lii_attach(device_t, device_t, void *);
119
120 static int lii_reset(struct lii_softc *);
121 static bool lii_eeprom_present(struct lii_softc *);
122 static int lii_read_macaddr(struct lii_softc *, uint8_t *);
123 static int lii_eeprom_read(struct lii_softc *, uint32_t, uint32_t *);
124 static void lii_spi_configure(struct lii_softc *);
125 static int lii_spi_read(struct lii_softc *, uint32_t, uint32_t *);
126 static void lii_setmulti(struct lii_softc *);
127 static void lii_tick(void *);
128
129 static int lii_alloc_rings(struct lii_softc *);
130 static int lii_free_tx_space(struct lii_softc *);
131
132 static int lii_mii_readreg(device_t, int, int);
133 static void lii_mii_writereg(device_t, int, int, int);
134 static void lii_mii_statchg(device_t);
135
136 static int lii_media_change(struct ifnet *);
137 static void lii_media_status(struct ifnet *, struct ifmediareq *);
138
139 static int lii_init(struct ifnet *);
140 static void lii_start(struct ifnet *);
141 static void lii_stop(struct ifnet *, int);
142 static void lii_watchdog(struct ifnet *);
143 static int lii_ioctl(struct ifnet *, u_long, caddr_t);
144
145 static int lii_intr(void *);
146 static void lii_rxintr(struct lii_softc *);
147 static void lii_txintr(struct lii_softc *);
148
149 CFATTACH_DECL(lii, sizeof(struct lii_softc),
150 lii_match, lii_attach, NULL, NULL);
151
152 /* #define LII_DEBUG_REGS */
153 #ifndef LII_DEBUG_REGS
154 #define AT_READ_4(sc,reg) \
155 bus_space_read_4((sc)->sc_mmiot, (sc)->sc_mmioh, (reg))
156 #define AT_READ_2(sc,reg) \
157 bus_space_read_2((sc)->sc_mmiot, (sc)->sc_mmioh, (reg))
158 #define AT_READ_1(sc,reg) \
159 bus_space_read_1((sc)->sc_mmiot, (sc)->sc_mmioh, (reg))
160 #define AT_WRITE_4(sc,reg,val) \
161 bus_space_write_4((sc)->sc_mmiot, (sc)->sc_mmioh, (reg), (val))
162 #define AT_WRITE_2(sc,reg,val) \
163 bus_space_write_2((sc)->sc_mmiot, (sc)->sc_mmioh, (reg), (val))
164 #define AT_WRITE_1(sc,reg,val) \
165 bus_space_write_1((sc)->sc_mmiot, (sc)->sc_mmioh, (reg), (val))
166 #else
167 static inline uint32_t
168 AT_READ_4(struct lii_softc *sc, bus_size_t reg)
169 {
170 uint32_t r = bus_space_read_4(sc->sc_mmiot, sc->sc_mmioh, reg);
171 printf("AT_READ_4(%x) = %x\n", (unsigned int)reg, r);
172 return r;
173 }
174
175 static inline uint16_t
176 AT_READ_2(struct lii_softc *sc, bus_size_t reg)
177 {
178 uint16_t r = bus_space_read_2(sc->sc_mmiot, sc->sc_mmioh, reg);
179 printf("AT_READ_2(%x) = %x\n", (unsigned int)reg, r);
180 return r;
181 }
182
183 static inline uint8_t
184 AT_READ_1(struct lii_softc *sc, bus_size_t reg)
185 {
186 uint8_t r = bus_space_read_1(sc->sc_mmiot, sc->sc_mmioh, reg);
187 printf("AT_READ_1(%x) = %x\n", (unsigned int)reg, r);
188 return r;
189 }
190
191 static inline void
192 AT_WRITE_4(struct lii_softc *sc, bus_size_t reg, uint32_t val)
193 {
194 printf("AT_WRITE_4(%x, %x)\n", (unsigned int)reg, val);
195 bus_space_write_4(sc->sc_mmiot, sc->sc_mmioh, reg, val);
196 }
197
198 static inline void
199 AT_WRITE_2(struct lii_softc *sc, bus_size_t reg, uint16_t val)
200 {
201 printf("AT_WRITE_2(%x, %x)\n", (unsigned int)reg, val);
202 bus_space_write_2(sc->sc_mmiot, sc->sc_mmioh, reg, val);
203 }
204
205 static inline void
206 AT_WRITE_1(struct lii_softc *sc, bus_size_t reg, uint8_t val)
207 {
208 printf("AT_WRITE_1(%x, %x)\n", (unsigned int)reg, val);
209 bus_space_write_1(sc->sc_mmiot, sc->sc_mmioh, reg, val);
210 }
211 #endif
212
213 /*
214 * Those are the default Linux parameters.
215 */
216
217 #define AT_TXD_NUM 64
218 #define AT_TXD_BUFFER_SIZE 8192
219 #define AT_RXD_NUM 64
220
221 /*
222 * Assuming (you know what that word makes of you) the chunk of memory
223 * bus_dmamem_alloc returns us is 128-byte aligned, we won't use the
224 * first 120 bytes of it, so that the space for the packets, and not the
225 * whole descriptors themselves, are on a 128-byte boundary.
226 */
227
228 #define AT_RXD_PADDING 120
229
230 static int
231 lii_match(device_t parent, cfdata_t cfmatch, void *aux)
232 {
233 struct pci_attach_args *pa = aux;
234
235 return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATTANSIC &&
236 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATTANSIC_ETHERNET_100);
237 }
238
239 static void
240 lii_attach(device_t parent, device_t self, void *aux)
241 {
242 struct lii_softc *sc = device_private(self);
243 struct pci_attach_args *pa = aux;
244 uint8_t eaddr[ETHER_ADDR_LEN];
245 struct ifnet *ifp = &sc->sc_ec.ec_if;
246 pci_intr_handle_t ih;
247 const char *intrstr;
248 pcireg_t cmd;
249
250 aprint_naive("\n");
251 aprint_normal(": Attansic/Atheros L2 Fast Ethernet\n");
252
253 sc->sc_pc = pa->pa_pc;
254 sc->sc_tag = pa->pa_tag;
255 sc->sc_dmat = pa->pa_dmat;
256
257 cmd = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
258 cmd |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
259 cmd &= ~PCI_COMMAND_IO_ENABLE;
260 pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, cmd);
261
262 switch (cmd = pci_mapreg_type(sc->sc_pc, sc->sc_tag, PCI_MAPREG_START)) {
263 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
264 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT_1M:
265 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
266 break;
267 default:
268 aprint_error("invalid base address register\n");
269 break;
270 }
271 if (pci_mapreg_map(pa, PCI_MAPREG_START, cmd, 0,
272 &sc->sc_mmiot, &sc->sc_mmioh, NULL, NULL) != 0) {
273 aprint_error("failed to map registers\n");
274 return;
275 }
276
277 if (lii_reset(sc))
278 return;
279
280 lii_spi_configure(sc);
281
282 if (lii_eeprom_present(sc))
283 sc->sc_memread = lii_eeprom_read;
284 else
285 sc->sc_memread = lii_spi_read;
286
287 if (lii_read_macaddr(sc, eaddr))
288 return;
289 memcpy(sc->sc_eaddr, eaddr, ETHER_ADDR_LEN);
290
291 aprint_normal("Ethernet address %s\n",
292 ether_sprintf(eaddr));
293
294 if (pci_intr_map(pa, &ih) != 0) {
295 aprint_error("failed to map interrupt\n");
296 return;
297 }
298 intrstr = pci_intr_string(sc->sc_pc, ih);
299 sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, lii_intr, sc);
300 if (sc->sc_ih == NULL) {
301 aprint_error("failed to establish interrupt");
302 if (intrstr != NULL)
303 aprint_error(" at %s", intrstr);
304 aprint_error("\n");
305 return;
306 }
307 aprint_normal("interrupting at %s\n", intrstr);
308
309 if (lii_alloc_rings(sc)) {
310 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
311 return;
312 }
313
314 callout_init(&sc->sc_tick_ch);
315 callout_setfunc(&sc->sc_tick_ch, lii_tick, sc);
316
317 sc->sc_mii.mii_ifp = ifp;
318 sc->sc_mii.mii_readreg = lii_mii_readreg;
319 sc->sc_mii.mii_writereg = lii_mii_writereg;
320 sc->sc_mii.mii_statchg = lii_mii_statchg;
321 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, lii_media_change,
322 lii_media_status);
323 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
324 MII_OFFSET_ANY, 0);
325 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
326
327 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
328 ifp->if_softc = sc;
329 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
330 ifp->if_ioctl = lii_ioctl;
331 ifp->if_start = lii_start;
332 ifp->if_watchdog = lii_watchdog;
333 ifp->if_init = lii_init;
334 ifp->if_stop = lii_stop;
335 IFQ_SET_READY(&ifp->if_snd);
336
337 /*
338 * While the device does support HW VLAN tagging, there is no
339 * real point using that feature.
340 */
341 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
342
343 if_attach(ifp);
344 ether_ifattach(ifp, eaddr);
345
346 return;
347 }
348
349 static int
350 lii_reset(struct lii_softc *sc)
351 {
352 int i;
353
354 DPRINTF(("lii_reset\n"));
355
356 AT_WRITE_4(sc, ATL2_SMC, SMC_SOFT_RST);
357 DELAY(1000);
358
359 for (i = 0; i < 10; ++i) {
360 if (AT_READ_4(sc, ATL2_BIS) == 0)
361 break;
362 DELAY(1000);
363 }
364
365 if (i == 10) {
366 aprint_error("reset failed\n");
367 return 1;
368 }
369
370 AT_WRITE_4(sc, ATL2_PHYC, PHYC_ENABLE);
371 DELAY(10);
372
373 /* Init PCI-Express module */
374 /* Magic Numbers Warning */
375 AT_WRITE_4(sc, ATL2_PCELTM, PCELTM_DEF);
376 AT_WRITE_4(sc, ATL2_PCEDTXC, PCEDTX_DEF);
377
378 return 0;
379 }
380
381 static bool
382 lii_eeprom_present(struct lii_softc *sc)
383 {
384 /*
385 * The Linux driver does this, but then it has a very weird way of
386 * checking whether the PCI configuration space exposes the Vital
387 * Product Data capability, so maybe it's not really needed.
388 */
389
390 #ifdef weirdloonix
391 uint32_t val;
392
393 val = AT_READ_4(sc, ATL2_SFC);
394 if (val & SFC_EN_VPD)
395 AT_WRITE_4(sc, ATL2_SFC, val & ~(SFC_EN_VPD));
396 #endif
397
398 return pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_VPD,
399 NULL, NULL) == 1;
400 }
401
402 static int
403 lii_eeprom_read(struct lii_softc *sc, uint32_t reg, uint32_t *val)
404 {
405 int r = pci_vpd_read(sc->sc_pc, sc->sc_tag, reg, 1, (pcireg_t *)val);
406
407 DPRINTF(("lii_eeprom_read(%x) = %x\n", reg, *val));
408
409 return r;
410 }
411
412 static void
413 lii_spi_configure(struct lii_softc *sc)
414 {
415 /*
416 * We don't offer a way to configure the SPI Flash vendor parameter, so
417 * the table is given for reference
418 */
419 static const struct lii_spi_flash_vendor {
420 const char *sfv_name;
421 const uint8_t sfv_opcodes[9];
422 } lii_sfv[] = {
423 { "Atmel", { 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62 } },
424 { "SST", { 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60 } },
425 { "ST", { 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xab, 0xd8, 0xc7 } },
426 };
427 #define SF_OPCODE_WRSR 0
428 #define SF_OPCODE_READ 1
429 #define SF_OPCODE_PRGM 2
430 #define SF_OPCODE_WREN 3
431 #define SF_OPCODE_WRDI 4
432 #define SF_OPCODE_RDSR 5
433 #define SF_OPCODE_RDID 6
434 #define SF_OPCODE_SECT_ER 7
435 #define SF_OPCODE_CHIP_ER 8
436
437 #define SF_DEFAULT_VENDOR 0
438 static const uint8_t vendor = SF_DEFAULT_VENDOR;
439
440 /*
441 * Why isn't WRDI used? Heck if I know.
442 */
443
444 AT_WRITE_1(sc, ATL2_SFOP_WRSR,
445 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_WRSR]);
446 AT_WRITE_1(sc, ATL2_SFOP_READ,
447 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_READ]);
448 AT_WRITE_1(sc, ATL2_SFOP_PROGRAM,
449 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_PRGM]);
450 AT_WRITE_1(sc, ATL2_SFOP_WREN,
451 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_WREN]);
452 AT_WRITE_1(sc, ATL2_SFOP_RDSR,
453 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_RDSR]);
454 AT_WRITE_1(sc, ATL2_SFOP_RDID,
455 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_RDID]);
456 AT_WRITE_1(sc, ATL2_SFOP_SC_ERASE,
457 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_SECT_ER]);
458 AT_WRITE_1(sc, ATL2_SFOP_CHIP_ERASE,
459 lii_sfv[vendor].sfv_opcodes[SF_OPCODE_CHIP_ER]);
460 }
461
462 #define MAKE_SFC(cssetup, clkhi, clklo, cshold, cshi, ins) \
463 ( (((cssetup) & SFC_CS_SETUP_MASK) \
464 << SFC_CS_SETUP_SHIFT) \
465 | (((clkhi) & SFC_CLK_HI_MASK) \
466 << SFC_CLK_HI_SHIFT) \
467 | (((clklo) & SFC_CLK_LO_MASK) \
468 << SFC_CLK_LO_SHIFT) \
469 | (((cshold) & SFC_CS_HOLD_MASK) \
470 << SFC_CS_HOLD_SHIFT) \
471 | (((cshi) & SFC_CS_HI_MASK) \
472 << SFC_CS_HI_SHIFT) \
473 | (((ins) & SFC_INS_MASK) \
474 << SFC_INS_SHIFT))
475
476 /* Magic settings from the Linux driver */
477
478 #define CUSTOM_SPI_CS_SETUP 2
479 #define CUSTOM_SPI_CLK_HI 2
480 #define CUSTOM_SPI_CLK_LO 2
481 #define CUSTOM_SPI_CS_HOLD 2
482 #define CUSTOM_SPI_CS_HI 3
483
484 static int
485 lii_spi_read(struct lii_softc *sc, uint32_t reg, uint32_t *val)
486 {
487 uint32_t v;
488 int i;
489
490 AT_WRITE_4(sc, ATL2_SF_DATA, 0);
491 AT_WRITE_4(sc, ATL2_SF_ADDR, reg);
492
493 v = SFC_WAIT_READY |
494 MAKE_SFC(CUSTOM_SPI_CS_SETUP, CUSTOM_SPI_CLK_HI,
495 CUSTOM_SPI_CLK_LO, CUSTOM_SPI_CS_HOLD, CUSTOM_SPI_CS_HI, 1);
496
497 AT_WRITE_4(sc, ATL2_SFC, v);
498 v |= SFC_START;
499 AT_WRITE_4(sc, ATL2_SFC, v);
500
501 for (i = 0; i < 10; ++i) {
502 DELAY(1000);
503 if (!(AT_READ_4(sc, ATL2_SFC) & SFC_START))
504 break;
505 }
506 if (i == 10)
507 return EBUSY;
508
509 *val = AT_READ_4(sc, ATL2_SF_DATA);
510 return 0;
511 }
512
513 static int
514 lii_read_macaddr(struct lii_softc *sc, uint8_t *ea)
515 {
516 uint32_t offset = 0x100;
517 uint32_t val, val1, addr0 = 0, addr1 = 0;
518 uint8_t found = 0;
519
520 while ((*sc->sc_memread)(sc, offset, &val) == 0) {
521 offset += 4;
522
523 /* Each chunk of data starts with a signature */
524 if ((val & 0xff) != 0x5a)
525 break;
526 if ((*sc->sc_memread)(sc, offset, &val1))
527 break;
528
529 offset += 4;
530
531 val >>= 16;
532 switch (val) {
533 case ATL2_MAC_ADDR_0:
534 addr0 = val1;
535 ++found;
536 break;
537 case ATL2_MAC_ADDR_1:
538 addr1 = val1;
539 ++found;
540 break;
541 default:
542 continue;
543 }
544 }
545
546 if (found < 2) {
547 aprint_error("error reading MAC address\n");
548 return 1;
549 }
550
551 addr0 = htole32(addr0);
552 addr1 = htole32(addr1);
553
554 if ((addr0 == 0xffffff && (addr1 & 0xffff) == 0xffff) ||
555 (addr0 == 0 && (addr1 & 0xffff) == 0)) {
556 addr0 = htole32(AT_READ_4(sc, ATL2_MAC_ADDR_0));
557 addr1 = htole32(AT_READ_4(sc, ATL2_MAC_ADDR_1));
558 }
559
560 ea[0] = (addr1 & 0x0000ff00) >> 8;
561 ea[1] = (addr1 & 0x000000ff);
562 ea[2] = (addr0 & 0xff000000) >> 24;
563 ea[3] = (addr0 & 0x00ff0000) >> 16;
564 ea[4] = (addr0 & 0x0000ff00) >> 8;
565 ea[5] = (addr0 & 0x000000ff);
566
567 return 0;
568 }
569
570 static int
571 lii_mii_readreg(device_t dev, int phy, int reg)
572 {
573 struct lii_softc *sc = device_private(dev);
574 uint32_t val;
575 int i;
576
577 val = (reg & MDIOC_REG_MASK) << MDIOC_REG_SHIFT;
578
579 val |= MDIOC_START | MDIOC_SUP_PREAMBLE;
580 val |= MDIOC_CLK_25_4 << MDIOC_CLK_SEL_SHIFT;
581
582 val |= MDIOC_READ;
583
584 AT_WRITE_4(sc, ATL2_MDIOC, val);
585
586 for (i = 0; i < MDIO_WAIT_TIMES; ++i) {
587 DELAY(2);
588 val = AT_READ_4(sc, ATL2_MDIOC);
589 if ((val & (MDIOC_START | MDIOC_BUSY)) == 0)
590 break;
591 }
592
593 if (i == MDIO_WAIT_TIMES)
594 aprint_error("timeout reading PHY %d reg %d\n", phy,
595 reg);
596
597 return (val & 0x0000ffff);
598 }
599
600 static void
601 lii_mii_writereg(device_t dev, int phy, int reg, int data)
602 {
603 struct lii_softc *sc = device_private(dev);
604 uint32_t val;
605 int i;
606
607 val = (reg & MDIOC_REG_MASK) << MDIOC_REG_SHIFT;
608 val |= (data & MDIOC_DATA_MASK) << MDIOC_DATA_SHIFT;
609
610 val |= MDIOC_START | MDIOC_SUP_PREAMBLE;
611 val |= MDIOC_CLK_25_4 << MDIOC_CLK_SEL_SHIFT;
612
613 /* val |= MDIOC_WRITE; */
614
615 AT_WRITE_4(sc, ATL2_MDIOC, val);
616
617 for (i = 0; i < MDIO_WAIT_TIMES; ++i) {
618 DELAY(2);
619 val = AT_READ_4(sc, ATL2_MDIOC);
620 if ((val & (MDIOC_START | MDIOC_BUSY)) == 0)
621 break;
622 }
623
624 if (i == MDIO_WAIT_TIMES)
625 aprint_error("timeout writing PHY %d reg %d\n", phy,
626 reg);
627 }
628
629 static void
630 lii_mii_statchg(device_t dev)
631 {
632 struct lii_softc *sc = device_private(dev);
633 uint32_t val;
634
635 DPRINTF(("lii_mii_statchg\n"));
636
637 val = AT_READ_4(sc, ATL2_MACC);
638
639 if ((sc->sc_mii.mii_media_active & IFM_GMASK) == IFM_FDX)
640 val |= MACC_FDX;
641 else
642 val &= ~MACC_FDX;
643
644 AT_WRITE_4(sc, ATL2_MACC, val);
645 }
646
647 static int
648 lii_media_change(struct ifnet *ifp)
649 {
650 struct lii_softc *sc = ifp->if_softc;
651
652 DPRINTF(("lii_media_change\n"));
653
654 if (ifp->if_flags & IFF_UP)
655 mii_mediachg(&sc->sc_mii);
656 return 0;
657 }
658
659 static void
660 lii_media_status(struct ifnet *ifp, struct ifmediareq *imr)
661 {
662 struct lii_softc *sc = ifp->if_softc;
663
664 DPRINTF(("lii_media_status\n"));
665
666 mii_pollstat(&sc->sc_mii);
667 imr->ifm_status = sc->sc_mii.mii_media_status;
668 imr->ifm_active = sc->sc_mii.mii_media_active;
669 }
670
671 static int
672 lii_init(struct ifnet *ifp)
673 {
674 struct lii_softc *sc = ifp->if_softc;
675 uint32_t val;
676 int error;
677
678 DPRINTF(("lii_init\n"));
679
680 lii_stop(ifp, 0);
681
682 memset(sc->sc_ring, 0, sc->sc_ringsize);
683
684 /* Disable all interrupts */
685 AT_WRITE_4(sc, ATL2_ISR, 0xffffffff);
686
687 /* XXX endianness */
688 AT_WRITE_4(sc, ATL2_MAC_ADDR_0,
689 sc->sc_eaddr[2] << 24 |
690 sc->sc_eaddr[3] << 16 |
691 sc->sc_eaddr[4] << 8 |
692 sc->sc_eaddr[5]);
693 AT_WRITE_4(sc, ATL2_MAC_ADDR_1,
694 sc->sc_eaddr[0] << 8 |
695 sc->sc_eaddr[1]);
696
697 AT_WRITE_4(sc, ATL2_DESC_BASE_ADDR_HI, 0);
698 /* XXX
699 sc->sc_ringmap->dm_segs[0].ds_addr >> 32);
700 */
701 AT_WRITE_4(sc, ATL2_RXD_BASE_ADDR_LO,
702 (sc->sc_ringmap->dm_segs[0].ds_addr & 0xffffffff)
703 + AT_RXD_PADDING);
704 AT_WRITE_4(sc, ATL2_TXS_BASE_ADDR_LO,
705 sc->sc_txsp & 0xffffffff);
706 AT_WRITE_4(sc, ATL2_TXD_BASE_ADDR_LO,
707 sc->sc_txdp & 0xffffffff);
708
709 AT_WRITE_2(sc, ATL2_TXD_BUFFER_SIZE, AT_TXD_BUFFER_SIZE / 4);
710 AT_WRITE_2(sc, ATL2_TXS_NUM_ENTRIES, AT_TXD_NUM);
711 AT_WRITE_2(sc, ATL2_RXD_NUM_ENTRIES, AT_RXD_NUM);
712
713 /*
714 * Inter Paket Gap Time = 0x60 (IPGT)
715 * Minimum inter-frame gap for RX = 0x50 (MIFG)
716 * 64-bit Carrier-Sense window = 0x40 (IPGR1)
717 * 96-bit IPG window = 0x60 (IPGR2)
718 */
719 AT_WRITE_4(sc, ATL2_MIPFG, 0x60405060);
720
721 /*
722 * Collision window = 0x37 (LCOL)
723 * Maximum # of retrans = 0xf (RETRY)
724 * Maximum binary expansion # = 0xa (ABEBT)
725 * IPG to start jam = 0x7 (JAMIPG)
726 */
727 AT_WRITE_4(sc, ATL2_MHDC, 0x07a0f037 |
728 MHDC_EXC_DEF_EN);
729
730 /* 100 means 200us */
731 AT_WRITE_2(sc, ATL2_IMTIV, 100);
732 AT_WRITE_2(sc, ATL2_SMC, SMC_ITIMER_EN);
733
734 /* 500000 means 100ms */
735 AT_WRITE_2(sc, ATL2_IALTIV, 50000);
736
737 AT_WRITE_4(sc, ATL2_MTU, ifp->if_mtu + ETHER_HDR_LEN
738 + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
739
740 /* unit unknown for TX cur-through threshold */
741 AT_WRITE_4(sc, ATL2_TX_CUT_THRESH, 0x177);
742
743 AT_WRITE_2(sc, ATL2_PAUSE_ON_TH, AT_RXD_NUM * 7 / 8);
744 AT_WRITE_2(sc, ATL2_PAUSE_OFF_TH, AT_RXD_NUM / 12);
745
746 sc->sc_rxcur = 0;
747 sc->sc_txs_cur = sc->sc_txs_ack = 0;
748 sc->sc_txd_cur = sc->sc_txd_ack = 0;
749 sc->sc_free_tx_slots = true;
750 AT_WRITE_2(sc, ATL2_MB_TXD_WR_IDX, sc->sc_txd_cur);
751 AT_WRITE_2(sc, ATL2_MB_RXD_RD_IDX, sc->sc_rxcur);
752
753 AT_WRITE_1(sc, ATL2_DMAR, DMAR_EN);
754 AT_WRITE_1(sc, ATL2_DMAW, DMAW_EN);
755
756 AT_WRITE_4(sc, ATL2_SMC, AT_READ_4(sc, ATL2_SMC) | SMC_MANUAL_INT);
757
758 error = ((AT_READ_4(sc, ATL2_ISR) & ISR_PHY_LINKDOWN) != 0);
759 AT_WRITE_4(sc, ATL2_ISR, 0x3fffffff);
760 AT_WRITE_4(sc, ATL2_ISR, 0);
761 if (error) {
762 aprint_error("init failed\n");
763 goto out;
764 }
765
766 lii_setmulti(sc);
767
768 val = AT_READ_4(sc, ATL2_MACC) & MACC_FDX;
769
770 val |= MACC_RX_EN | MACC_TX_EN | MACC_MACLP_CLK_PHY |
771 MACC_TX_FLOW_EN | MACC_RX_FLOW_EN |
772 MACC_ADD_CRC | MACC_PAD | MACC_BCAST_EN;
773
774 if (ifp->if_flags & IFF_PROMISC)
775 val |= MACC_PROMISC_EN;
776 else if (ifp->if_flags & IFF_ALLMULTI)
777 val |= MACC_ALLMULTI_EN;
778
779 val |= 7 << MACC_PREAMBLE_LEN_SHIFT;
780 val |= 2 << MACC_HDX_LEFT_BUF_SHIFT;
781
782 AT_WRITE_4(sc, ATL2_MACC, val);
783
784 mii_mediachg(&sc->sc_mii);
785
786 AT_WRITE_4(sc, ATL2_IMR, IMR_NORMAL_MASK);
787
788 callout_schedule(&sc->sc_tick_ch, hz);
789
790 ifp->if_flags |= IFF_RUNNING;
791 ifp->if_flags &= ~IFF_OACTIVE;
792
793 out:
794 return error;
795 }
796
797 static void
798 lii_tx_put(struct lii_softc *sc, struct mbuf *m)
799 {
800 int left;
801 struct tx_pkt_header *tph =
802 (struct tx_pkt_header *)(sc->sc_txdbase + sc->sc_txd_cur);
803
804 memset(tph, 0, sizeof *tph);
805 tph->txph_size = m->m_pkthdr.len;
806
807 sc->sc_txd_cur = (sc->sc_txd_cur + 4) % AT_TXD_BUFFER_SIZE;
808
809 /*
810 * We already know we have enough space, so if there is a part of the
811 * space ahead of txd_cur that is active, it doesn't matter because
812 * left will be large enough even without it.
813 */
814 left = AT_TXD_BUFFER_SIZE - sc->sc_txd_cur;
815
816 if (left > m->m_pkthdr.len) {
817 m_copydata(m, 0, m->m_pkthdr.len,
818 sc->sc_txdbase + sc->sc_txd_cur);
819 sc->sc_txd_cur += m->m_pkthdr.len;
820 } else {
821 m_copydata(m, 0, left, sc->sc_txdbase + sc->sc_txd_cur);
822 m_copydata(m, left, m->m_pkthdr.len - left, sc->sc_txdbase);
823 sc->sc_txd_cur = m->m_pkthdr.len - left;
824 }
825
826 /* Round to a 32-bit boundary */
827 sc->sc_txd_cur = ((sc->sc_txd_cur + 3) & ~3) % AT_TXD_BUFFER_SIZE;
828 if (sc->sc_txd_cur == sc->sc_txd_ack)
829 sc->sc_free_tx_slots = false;
830 }
831
832 static int
833 lii_free_tx_space(struct lii_softc *sc)
834 {
835 int space;
836
837 if (sc->sc_txd_cur >= sc->sc_txd_ack)
838 space = (AT_TXD_BUFFER_SIZE - sc->sc_txd_cur) +
839 sc->sc_txd_ack;
840 else
841 space = sc->sc_txd_ack - sc->sc_txd_cur;
842
843 /* Account for the tx_pkt_header */
844 return (space - 4);
845 }
846
847 static void
848 lii_start(struct ifnet *ifp)
849 {
850 struct lii_softc *sc = ifp->if_softc;
851 struct mbuf *m0;
852
853 DPRINTF(("lii_start\n"));
854
855 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
856 return;
857
858 for (;;) {
859 IFQ_POLL(&ifp->if_snd, m0);
860 if (m0 == NULL)
861 break;
862
863 if (!sc->sc_free_tx_slots ||
864 lii_free_tx_space(sc) < m0->m_pkthdr.len) {
865 ifp->if_flags |= IFF_OACTIVE;
866 break;
867 }
868
869 lii_tx_put(sc, m0);
870
871 DPRINTF(("lii_start: put %d\n", sc->sc_txs_cur));
872
873 sc->sc_txs[sc->sc_txs_cur].txps_update = 0;
874 sc->sc_txs_cur = (sc->sc_txs_cur + 1) % AT_TXD_NUM;
875 if (sc->sc_txs_cur == sc->sc_txs_ack)
876 sc->sc_free_tx_slots = false;
877
878 AT_WRITE_2(sc, ATL2_MB_TXD_WR_IDX, sc->sc_txd_cur/4);
879
880 IFQ_DEQUEUE(&ifp->if_snd, m0);
881
882 #if NBPFILTER > 0
883 if (ifp->if_bpf != NULL)
884 bpf_mtap(ifp->if_bpf, m0);
885 #endif
886 m_freem(m0);
887 }
888 }
889
890 static void
891 lii_stop(struct ifnet *ifp, int disable)
892 {
893 struct lii_softc *sc = ifp->if_softc;
894
895 callout_stop(&sc->sc_tick_ch);
896
897 ifp->if_timer = 0;
898 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
899
900 mii_down(&sc->sc_mii);
901
902 lii_reset(sc);
903
904 AT_WRITE_4(sc, ATL2_IMR, 0);
905 }
906
907 static int
908 lii_intr(void *v)
909 {
910 struct lii_softc *sc = v;
911 uint32_t status;
912
913 status = AT_READ_4(sc, ATL2_ISR);
914 if (status == 0)
915 return 0;
916
917 DPRINTF(("lii_intr (%x)\n", status));
918
919 /* Clear the interrupt and disable them */
920 AT_WRITE_4(sc, ATL2_ISR, status | ISR_DIS_INT);
921
922 if (status & (ISR_PHY | ISR_MANUAL)) {
923 /* Ack PHY interrupt. Magic register */
924 if (status & ISR_PHY)
925 (void)lii_mii_readreg(&sc->sc_dev, 1, 19);
926 mii_mediachg(&sc->sc_mii);
927 }
928
929 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST | ISR_PHY_LINKDOWN)) {
930 lii_init(&sc->sc_ec.ec_if);
931 return 1;
932 }
933
934 if (status & ISR_RX_EVENT) {
935 #ifdef LII_DEBUG
936 if (!(status & ISR_RS_UPDATE))
937 printf("rxintr %08x\n", status);
938 #endif
939 lii_rxintr(sc);
940 }
941
942 if (status & ISR_TX_EVENT)
943 lii_txintr(sc);
944
945 /* Re-enable interrupts */
946 AT_WRITE_4(sc, ATL2_ISR, 0);
947
948 return 1;
949 }
950
951 static void
952 lii_rxintr(struct lii_softc *sc)
953 {
954 struct ifnet *ifp = &sc->sc_ec.ec_if;
955 struct rx_pkt *rxp;
956 struct mbuf *m;
957 uint16_t size;
958
959 DPRINTF(("lii_rxintr\n"));
960
961 for (;;) {
962 rxp = &sc->sc_rxp[sc->sc_rxcur];
963 if (rxp->rxp_update == 0)
964 break;
965
966 DPRINTF(("lii_rxintr: getting %u (%u) [%x]\n", sc->sc_rxcur,
967 rxp->rxp_size, rxp->rxp_flags));
968 sc->sc_rxcur = (sc->sc_rxcur + 1) % AT_RXD_NUM;
969 rxp->rxp_update = 0;
970 if (!(rxp->rxp_flags & ATL2_RXF_SUCCESS)) {
971 ++ifp->if_ierrors;
972 continue;
973 }
974
975 MGETHDR(m, M_DONTWAIT, MT_DATA);
976 if (m == NULL) {
977 ++ifp->if_ierrors;
978 continue;
979 }
980 size = rxp->rxp_size - ETHER_CRC_LEN;
981 if (size > MHLEN) {
982 MCLGET(m, M_DONTWAIT);
983 if ((m->m_flags & M_EXT) == 0) {
984 m_freem(m);
985 ++ifp->if_ierrors;
986 continue;
987 }
988 }
989
990 m->m_pkthdr.rcvif = ifp;
991 /* Copy the packet withhout the FCS */
992 m->m_pkthdr.len = m->m_len = size;
993 memcpy(mtod(m, void *), &rxp->rxp_data[0], size);
994 ++ifp->if_ipackets;
995
996 #if NBPFILTER > 0
997 if (ifp->if_bpf)
998 bpf_mtap(ifp->if_bpf, m);
999 #endif
1000
1001 (*ifp->if_input)(ifp, m);
1002 }
1003
1004 AT_WRITE_4(sc, ATL2_MB_RXD_RD_IDX, sc->sc_rxcur);
1005 }
1006
1007 static void
1008 lii_txintr(struct lii_softc *sc)
1009 {
1010 struct ifnet *ifp = &sc->sc_ec.ec_if;
1011 struct tx_pkt_status *txs;
1012 struct tx_pkt_header *txph;
1013
1014 DPRINTF(("lii_txintr\n"));
1015
1016 for (;;) {
1017 txs = &sc->sc_txs[sc->sc_txs_ack];
1018 if (txs->txps_update == 0)
1019 break;
1020 DPRINTF(("lii_txintr: ack'd %d\n", sc->sc_txs_ack));
1021 sc->sc_txs_ack = (sc->sc_txs_ack + 1) % AT_TXD_NUM;
1022 sc->sc_free_tx_slots = true;
1023
1024 txs->txps_update = 0;
1025
1026 txph = (struct tx_pkt_header *)
1027 (sc->sc_txdbase + sc->sc_txd_ack);
1028
1029 if (txph->txph_size != txs->txps_size)
1030 aprint_error("mismatched status and packet\n");
1031 /*
1032 * Move ack by the packet size, taking the packet header in
1033 * account and round to the next 32-bit boundary
1034 * (7 = sizeof(header) + 3)
1035 */
1036 sc->sc_txd_ack = (sc->sc_txd_ack + txph->txph_size + 7 ) & ~3;
1037 sc->sc_txd_ack %= AT_TXD_BUFFER_SIZE;
1038
1039 if (txs->txps_flags & ATL2_TXF_SUCCESS)
1040 ++ifp->if_opackets;
1041 else
1042 ++ifp->if_oerrors;
1043 ifp->if_flags &= ~IFF_OACTIVE;
1044 }
1045
1046 if (sc->sc_free_tx_slots)
1047 lii_start(ifp);
1048 }
1049
1050 static int
1051 lii_alloc_rings(struct lii_softc *sc)
1052 {
1053 int nsegs;
1054 bus_size_t bs;
1055
1056 /*
1057 * We need a big chunk of DMA-friendly memory because descriptors
1058 * are not separate from data on that crappy hardware, which means
1059 * we'll have to copy data from and to that memory zone to and from
1060 * the mbufs.
1061 *
1062 * How lame is that? Using the default values from the Linux driver,
1063 * we allocate space for receiving up to 64 full-size Ethernet frames,
1064 * and only 8kb for transmitting up to 64 Ethernet frames.
1065 */
1066
1067 sc->sc_ringsize = bs = AT_RXD_PADDING
1068 + AT_RXD_NUM * sizeof(struct rx_pkt)
1069 + AT_TXD_NUM * sizeof(struct tx_pkt_status)
1070 + AT_TXD_BUFFER_SIZE;
1071
1072 if (bus_dmamap_create(sc->sc_dmat, bs, 1, bs, (1<<30),
1073 BUS_DMA_NOWAIT, &sc->sc_ringmap) != 0) {
1074 aprint_error("bus_dmamap_create failed\n");
1075 return 1;
1076 }
1077
1078 if (bus_dmamem_alloc(sc->sc_dmat, bs, PAGE_SIZE, (1<<30),
1079 &sc->sc_ringseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
1080 aprint_error("bus_dmamem_alloc failed\n");
1081 goto fail;
1082 }
1083
1084 if (bus_dmamem_map(sc->sc_dmat, &sc->sc_ringseg, nsegs, bs,
1085 (caddr_t *)&sc->sc_ring, BUS_DMA_NOWAIT) != 0) {
1086 aprint_error("bus_dmamem_map failed\n");
1087 goto fail1;
1088 }
1089
1090 if (bus_dmamap_load(sc->sc_dmat, sc->sc_ringmap, sc->sc_ring,
1091 bs, NULL, BUS_DMA_NOWAIT) != 0) {
1092 aprint_error("bus_dmamap_load failed\n");
1093 goto fail2;
1094 }
1095
1096 sc->sc_rxp = (void *)(sc->sc_ring + AT_RXD_PADDING);
1097 sc->sc_txs = (void *)(sc->sc_ring + AT_RXD_PADDING
1098 + AT_RXD_NUM * sizeof(struct rx_pkt));
1099 sc->sc_txdbase = ((char *)sc->sc_txs)
1100 + AT_TXD_NUM * sizeof(struct tx_pkt_status);
1101 sc->sc_txsp = sc->sc_ringmap->dm_segs[0].ds_addr
1102 + ((char *)sc->sc_txs - (char *)sc->sc_ring);
1103 sc->sc_txdp = sc->sc_ringmap->dm_segs[0].ds_addr
1104 + ((char *)sc->sc_txdbase - (char *)sc->sc_ring);
1105
1106 return 0;
1107
1108 fail2:
1109 bus_dmamem_unmap(sc->sc_dmat, sc->sc_ring, bs);
1110 fail1:
1111 bus_dmamem_free(sc->sc_dmat, &sc->sc_ringseg, nsegs);
1112 fail:
1113 bus_dmamap_destroy(sc->sc_dmat, sc->sc_ringmap);
1114 return 1;
1115 }
1116
1117 static void
1118 lii_watchdog(struct ifnet *ifp)
1119 {
1120 aprint_error("watchdog timeout\n");
1121 ++ifp->if_oerrors;
1122 lii_init(ifp);
1123 }
1124
1125 static int
1126 lii_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1127 {
1128 struct lii_softc *sc = ifp->if_softc;
1129 int s, error;
1130
1131 s = splnet();
1132
1133 switch(cmd) {
1134 case SIOCADDMULTI:
1135 case SIOCDELMULTI:
1136 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1137 if (ifp->if_flags & IFF_RUNNING)
1138 lii_setmulti(sc);
1139 error = 0;
1140 }
1141 break;
1142 case SIOCSIFMEDIA:
1143 case SIOCGIFMEDIA:
1144 error = ifmedia_ioctl(ifp, (struct ifreq *)data,
1145 &sc->sc_mii.mii_media, cmd);
1146 break;
1147 default:
1148 error = ether_ioctl(ifp, cmd, data);
1149 if (error == ENETRESET) {
1150 if (ifp->if_flags & IFF_RUNNING)
1151 lii_setmulti(sc);
1152 error = 0;
1153 }
1154 break;
1155 }
1156
1157 splx(s);
1158
1159 return error;
1160 }
1161
1162 static void
1163 lii_setmulti(struct lii_softc *sc)
1164 {
1165 struct ethercom *ec = &sc->sc_ec;
1166 struct ifnet *ifp = &ec->ec_if;
1167 uint32_t mht0 = 0, mht1 = 0, crc;
1168 struct ether_multi *enm;
1169 struct ether_multistep step;
1170
1171 /* Clear multicast hash table */
1172 AT_WRITE_4(sc, ATL2_MHT, 0);
1173 AT_WRITE_4(sc, ATL2_MHT + 4, 0);
1174
1175 ifp->if_flags &= ~IFF_ALLMULTI;
1176
1177 ETHER_FIRST_MULTI(step, ec, enm);
1178 while (enm != NULL) {
1179 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1180 ifp->if_flags |= IFF_ALLMULTI;
1181 mht0 = mht1 = 0;
1182 goto alldone;
1183 }
1184
1185 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1186
1187 if (crc & (1 << 31))
1188 mht1 |= (1 << (crc & 0x0000001f));
1189 else
1190 mht0 |= (1 << (crc & 0x0000001f));
1191
1192 ETHER_NEXT_MULTI(step, enm);
1193 }
1194
1195 alldone:
1196 AT_WRITE_4(sc, ATL2_MHT, mht0);
1197 AT_WRITE_4(sc, ATL2_MHT+4, mht1);
1198 }
1199
1200 static void
1201 lii_tick(void *v)
1202 {
1203 struct lii_softc *sc = v;
1204 int s;
1205
1206 s = splnet();
1207 mii_tick(&sc->sc_mii);
1208 splx(s);
1209
1210 callout_schedule(&sc->sc_tick_ch, hz);
1211 }
1212