1 /*        $NetBSD: if_sn_ap.c,v 1.13 2018/10/14 00:10:11 tsutsui Exp $          */
2 
3 /*
4  * Copyright (C) 1997 Allen Briggs
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Allen Briggs
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_sn_ap.c,v 1.13 2018/10/14 00:10:11 tsutsui Exp $");
35 
36 #include "opt_inet.h"
37 
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #include <sys/syslog.h>
44 #include <sys/systm.h>
45 
46 #include <net/if.h>
47 #include <net/if_ether.h>
48 
49 #include <machine/cpu.h>
50 #include <machine/adrsmap.h>
51 
52 #include <newsmips/apbus/apbusvar.h>
53 #include <newsmips/apbus/if_snreg.h>
54 #include <newsmips/apbus/if_snvar.h>
55 
56 #include <newsmips/newsmips/machid.h>
57 
58 #define SONIC_MACROM_OFFSET   0x40
59 
60 #define SONIC_APBUS_REG_OFFSET          0x00010000
61 #define SONIC_APBUS_MEM_OFFSET          0x00020000
62 #define SONIC_APBUS_CTL_OFFSET          (-0x00100000)
63 
64 static int          sn_ap_match(device_t, cfdata_t, void *);
65 static void         sn_ap_attach(device_t, device_t, void *);
66 static int          sn_ap_getaddr(struct sn_softc *, uint8_t *);
67 
68 CFATTACH_DECL_NEW(sn_ap, sizeof(struct sn_softc),
69     sn_ap_match, sn_ap_attach, NULL, NULL);
70 
71 static int
sn_ap_match(device_t parent,cfdata_t cf,void * aux)72 sn_ap_match(device_t parent, cfdata_t cf, void *aux)
73 {
74           struct apbus_attach_args *apa = aux;
75 
76           if (strcmp(apa->apa_name, "sonic") != 0)
77                     return 0;
78 
79           return 1;
80 }
81 
82 /*
83  * Install interface into kernel networking data structures
84  */
85 static void
sn_ap_attach(device_t parent,device_t self,void * aux)86 sn_ap_attach(device_t parent, device_t self, void *aux)
87 {
88           struct sn_softc *sc = device_private(self);
89           struct apbus_attach_args *apa = aux;
90           uint8_t myaddr[ETHER_ADDR_LEN];
91           uint32_t intrmask = 0;
92 
93           sc->sc_dev = self;
94           sc->slotno = apa->apa_slotno;
95           if (systype == NEWS4000 && sc->slotno == 0) {
96                     sc->sc_flags |= F_NWS40S0;
97           }
98           if ((sc->sc_flags & F_NWS40S0) != 0) {
99                     sc->sc_hwbase = (void *)apa->apa_hwbase;
100                     sc->sc_regbase = (void *)apa->apa_hwbase;
101                     sc->memory = (void *)NEWS4000_SONIC_MEMORY;
102                     sc->buffer = (void *)NEWS4000_SONIC_BUFFER;
103           } else {
104                     sc->sc_hwbase = (void *)apa->apa_hwbase;
105                     sc->sc_regbase =
106                         (void *)(apa->apa_hwbase + SONIC_APBUS_REG_OFFSET);
107                     sc->memory =
108                         (void *)(apa->apa_hwbase + SONIC_APBUS_MEM_OFFSET);
109           }
110 
111           aprint_normal(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
112 
113           if ((sc->sc_flags & F_NWS40S0) != 0) {
114                     sc->snr_dcr = DCR_STERM | DCR_TFT28;
115                     sc->snr_dcr2 = 0x0100;
116           } else {
117                     sc->snr_dcr = DCR_WAIT0 | DCR_DMABLOCK | DCR_RFT16 | DCR_TFT16;
118                     sc->snr_dcr2 = 0;
119                     sc->snr_dcr |= DCR_EXBUS;
120           }
121           sc->bitmode = 1;
122 
123           if (sn_ap_getaddr(sc, myaddr)) {
124                     aprint_error(": failed to get MAC address\n");
125                     return;
126           }
127 
128           aprint_normal("\n");
129 
130           /* snsetup returns 1 if something fails */
131           if (snsetup(sc, myaddr))
132                     return;
133 
134           if (systype == NEWS5000) {
135                     intrmask = (apa->apa_slotno == 0) ?
136                         NEWS5000_INT0_SONIC : NEWS5000_SLOTTOMASK(apa->apa_slotno);
137           }
138           if (systype == NEWS4000) {
139                     intrmask = (apa->apa_slotno == 0) ?
140                         NEWS4000_INT0_SONIC : NEWS4000_SLOTTOMASK(apa->apa_slotno);
141           }
142 
143           apbus_intr_establish(0, /* interrupt level (0 or 1) */
144               intrmask,
145               0, /* priority */
146               snintr, sc, device_xname(self), apa->apa_ctlnum);
147 }
148 
149 int
sn_ap_getaddr(struct sn_softc * sc,uint8_t * lladdr)150 sn_ap_getaddr(struct sn_softc *sc, uint8_t *lladdr)
151 {
152 
153           if ((sc->sc_flags & F_NWS40S0) != 0) {
154                     extern struct idrom idrom;
155                     memcpy(lladdr, idrom.id_macadrs, sizeof(idrom.id_macadrs));
156           } else {
157                     uint32_t *p;
158                     int i;
159 
160                     p = (uint32_t *)((uint8_t *)sc->sc_hwbase +
161                         SONIC_MACROM_OFFSET);
162                     for (i = 0; i < ETHER_ADDR_LEN; i++) {
163                               int h = *p++ & 0x0f;
164                               int l = *p++ & 0x0f;
165                               *lladdr++ = (h << 4) + l;
166                     }
167           }
168 
169           return 0;
170 }
171 
172 #define APSONIC_INT_MASK      0x00007f00          /* XXX */
173 #define   APSONIC_INT_REG(base)         (((u_long)(base) & 0xffc00000) | 0x00100000)
174 
175 void
sn_md_init(struct sn_softc * sc)176 sn_md_init(struct sn_softc *sc)
177 {
178 
179           if ((sc->sc_flags & F_NWS40S0) != 0) {
180                     *(volatile uint32_t *)0xbf700000 = 1;
181           } else {
182                     volatile uint32_t *reg =
183                         (uint32_t *)APSONIC_INT_REG(sc->sc_hwbase);
184 
185                     *reg = APSONIC_INT_MASK;
186           }
187           wbflush();
188           apbus_wbflush();
189           delay(10000);
190 }
191