xref: /netbsd-src/sys/arch/sgimips/ioc/if_le_oioc.c (revision 3aa5a3ae30f092daa39cb540c574af3082fdc595)
1 /*	$NetBSD: if_le_oioc.c,v 1.6 2022/02/12 03:24:34 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Stephen M. Rumble
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. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: if_le_oioc.c,v 1.6 2022/02/12 03:24:34 riastradh Exp $");
29 
30 #include "opt_inet.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mbuf.h>
35 #include <sys/syslog.h>
36 #include <sys/socket.h>
37 #include <sys/device.h>
38 
39 #include <uvm/uvm.h>	// for uvm_pglistalloc
40 
41 #include <net/if.h>
42 #include <net/if_ether.h>
43 #include <net/if_media.h>
44 
45 #ifdef INET
46 #include <netinet/in.h>
47 #include <netinet/if_inarp.h>
48 #endif
49 
50 #include <machine/cpu.h>
51 #include <machine/machtype.h>
52 
53 #include <dev/ic/lancereg.h>
54 #include <dev/ic/lancevar.h>
55 #include <dev/ic/am7990reg.h>
56 #include <dev/ic/am7990var.h>
57 
58 #include <dev/arcbios/arcbios.h>
59 #include <dev/arcbios/arcbiosvar.h>
60 
61 #include <sgimips/ioc/oiocvar.h>
62 #include <sgimips/ioc/oiocreg.h>
63 
64 #include <mips/include/cache.h>
65 
66 #ifndef OIOC_LANCE_NPAGES
67 #define OIOC_LANCE_NPAGES	64	/* 256KB */
68 #endif
69 
70 #if OIOC_LANCE_NPAGES > OIOC_ENET_NPGMAPS
71 #error OIOC_LANCE_NPAGES > OIOC_ENET_NPGMAPS (512)
72 #endif
73 
74 /*
75  * Ethernet software status per interface.
76  * The real stuff is in dev/ic/am7990var.h
77  * The really real stuff is in dev/ic/lancevar.h
78  *
79  * Lance is somewhat nasty MI code. We basically get:
80  *	struct le_softc {
81  *		struct am7990_softc {
82  *			struct lance_softc {
83  *				device_t sc_dev;
84  *				...
85  *			}
86  * 		}
87  *
88  *		bus_space_tag ...
89  *	}
90  *
91  * So, we can cast any three to any other three, plus
92  * device_private(sc_dev) points back at the top (i.e. to le_softc,
93  * am7990_softc and lance_softc). Bloody hell!
94  */
95 struct le_softc {
96 	struct	am7990_softc sc_am7990;		/* glue to MI code */
97 
98 	bus_space_tag_t      sc_st;
99 	bus_space_handle_t   sc_maph;		/* ioc<->lance page map regs */
100 	bus_space_handle_t   sc_rdph;		/* lance rdp */
101 	bus_space_handle_t   sc_raph;		/* lance rap */
102 };
103 
104 static int	le_match(device_t, cfdata_t, void *);
105 static void	le_attach(device_t, device_t, void *);
106 
107 CFATTACH_DECL_NEW(le, sizeof(struct le_softc),
108     le_match, le_attach, NULL, NULL);
109 
110 #if defined(_KERNEL_OPT)
111 #include "opt_ddb.h"
112 #endif
113 
114 static void	lewrcsr(struct lance_softc *, uint16_t, uint16_t);
115 static uint16_t	lerdcsr(struct lance_softc *, uint16_t);
116 
117 static void
lewrcsr(struct lance_softc * sc,uint16_t port,uint16_t val)118 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
119 {
120 	struct le_softc *lesc = (struct le_softc *)sc;
121 	bus_space_write_2(lesc->sc_st, lesc->sc_raph, 0, port);
122 	bus_space_write_2(lesc->sc_st, lesc->sc_rdph, 0, val);
123 }
124 
125 static uint16_t
lerdcsr(struct lance_softc * sc,uint16_t port)126 lerdcsr(struct lance_softc *sc, uint16_t port)
127 {
128 	struct le_softc *lesc = (struct le_softc *)sc;
129 	bus_space_write_2(lesc->sc_st, lesc->sc_raph, 0, port);
130 	return (bus_space_read_2(lesc->sc_st, lesc->sc_rdph, 0));
131 }
132 
133 /*
134  * Always present on IP6 and IP10. IP4? Unknown.
135  */
136 int
le_match(device_t parent,cfdata_t cf,void * aux)137 le_match(device_t parent, cfdata_t cf, void *aux)
138 {
139 	struct oioc_attach_args *oa = aux;
140 
141 	if (mach_type == MACH_SGI_IP4)
142 		return (0);
143 
144         if (strcmp(oa->oa_name, cf->cf_name) == 0)
145 		return (1);
146 
147 	return (0);
148 }
149 
150 void
le_attach(device_t parent,device_t self,void * aux)151 le_attach(device_t parent, device_t self, void *aux)
152 {
153 	struct le_softc *lesc = device_private(self);
154 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
155 	struct oioc_attach_args *oa = aux;
156 	struct pglist mlist;
157 	const char *enaddrstr;
158 	char enaddr[ETHER_ADDR_LEN];
159 	char pbuf[9];
160 	int i, error;
161 
162 	sc->sc_dev = self;
163 	lesc->sc_st = oa->oa_st;
164 
165 	enaddrstr = arcbios_GetEnvironmentVariable("eaddr");
166 	if (enaddrstr == NULL) {
167 		aprint_error(": failed to obtain MAC address\n");
168 		return;
169 	}
170 
171         if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, OIOC_LANCE_RDP,
172 	    OIOC_LANCE_RDP_SIZE, &lesc->sc_rdph)) != 0) {
173 		printf(": unable to map rdp reg, error=%d\n", error);
174 		goto fail_0;
175 	}
176 
177         if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, OIOC_LANCE_RAP,
178 	    OIOC_LANCE_RAP_SIZE, &lesc->sc_raph)) != 0) {
179 		printf(": unable to map rap reg, error=%d\n", error);
180 		goto fail_1;
181 	}
182 
183         if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh,
184 	    OIOC_ENET_PGMAP_BASE, OIOC_ENET_PGMAP_SIZE, &lesc->sc_maph)) != 0) {
185 		printf(": unable to map rap reg, error=%d\n", error);
186 		goto fail_2;
187 	}
188 
189 	/* Allocate a contiguous chunk of physical memory for the le buffer. */
190 	error = uvm_pglistalloc(OIOC_LANCE_NPAGES * PAGE_SIZE,
191 	    pmap_limits.avail_start, pmap_limits.avail_end, PAGE_SIZE, 0,
192 	    &mlist, 1, 0);
193 	if (error) {
194 		aprint_error(": failed to allocate ioc<->lance buffer space, "
195 		    "error = %d\n", error);
196 		goto fail_3;
197 	}
198 
199 	/* Use IOC to map the physical memory into the Ethernet chip's space. */
200 	for (i = 0; i < OIOC_LANCE_NPAGES; i++) {
201 		bus_space_write_2(lesc->sc_st,lesc->sc_maph,
202 		    OIOC_ENET_PGMAP_OFF(i),
203 		    (VM_PAGE_TO_PHYS(mlist.tqh_first) >> PAGE_SHIFT) + i);
204 	}
205 
206 	sc->sc_mem = (void *)MIPS_PHYS_TO_KSEG1(
207 	    (uint32_t)VM_PAGE_TO_PHYS(mlist.tqh_first));
208 	sc->sc_memsize = OIOC_LANCE_NPAGES * PAGE_SIZE;
209 	sc->sc_addr = 0;
210 	sc->sc_conf3 = LE_C3_BSWP;
211 
212 	ether_aton_r(enaddr, sizeof(enaddr), enaddrstr);
213 	memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
214 
215 	if (cpu_intr_establish(oa->oa_irq, IPL_NET, am7990_intr, sc) == NULL) {
216 		aprint_error(": failed to establish interrupt %d\n",oa->oa_irq);
217 		goto fail_4;
218 	}
219 
220 	sc->sc_copytodesc   = lance_copytobuf_contig;
221 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
222 	sc->sc_copytobuf    = lance_copytobuf_contig;
223 	sc->sc_copyfrombuf  = lance_copyfrombuf_contig;
224 	sc->sc_zerobuf      = lance_zerobuf_contig;
225 
226 	sc->sc_rdcsr  = lerdcsr;
227 	sc->sc_wrcsr  = lewrcsr;
228 	sc->sc_hwinit = NULL;
229 
230 	format_bytes(pbuf, sizeof(pbuf), OIOC_LANCE_NPAGES * PAGE_SIZE);
231 	aprint_normal(": main memory used = %s\n", pbuf);
232 	aprint_normal("%s", device_xname(self));
233 
234 	am7990_config(&lesc->sc_am7990);
235 
236 	return;
237 
238 fail_4:
239 	uvm_pglistfree(&mlist);
240 fail_3:
241 	bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_maph);
242 fail_2:
243 	bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_raph);
244 fail_1:
245 	bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_rdph);
246 fail_0:
247 	return;
248 }
249