xref: /netbsd-src/sys/arch/sgimips/ioc/if_le_oioc.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: if_le_oioc.c,v 1.3 2011/01/10 13:29:29 tsutsui 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.3 2011/01/10 13:29:29 tsutsui 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 sc_dev->dv_private points
92  * back at the top (i.e. to le_softc, am7990_softc and lance_softc). Bloody
93  * 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
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
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
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
151 le_attach(device_t parent, device_t self, void *aux)
152 {
153 	extern paddr_t avail_start, avail_end;
154 
155 	struct le_softc *lesc = device_private(self);
156 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
157 	struct oioc_attach_args *oa = aux;
158 	struct pglist mlist;
159 	const char *enaddrstr;
160 	char enaddr[ETHER_ADDR_LEN];
161 	char pbuf[9];
162 	int i, error;
163 
164 	sc->sc_dev = self;
165 	lesc->sc_st = oa->oa_st;
166 
167 	enaddrstr = ARCBIOS->GetEnvironmentVariable("eaddr");
168 	if (enaddrstr == NULL) {
169 		aprint_error(": failed to obtain MAC address\n");
170 		return;
171 	}
172 
173         if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, OIOC_LANCE_RDP,
174 	    OIOC_LANCE_RDP_SIZE, &lesc->sc_rdph)) != 0) {
175 		printf(": unable to map rdp reg, error=%d\n", error);
176 		goto fail_0;
177 	}
178 
179         if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh, OIOC_LANCE_RAP,
180 	    OIOC_LANCE_RAP_SIZE, &lesc->sc_raph)) != 0) {
181 		printf(": unable to map rap reg, error=%d\n", error);
182 		goto fail_1;
183 	}
184 
185         if ((error = bus_space_subregion(oa->oa_st, oa->oa_sh,
186 	    OIOC_ENET_PGMAP_BASE, OIOC_ENET_PGMAP_SIZE, &lesc->sc_maph)) != 0) {
187 		printf(": unable to map rap reg, error=%d\n", error);
188 		goto fail_2;
189 	}
190 
191 	/* Allocate a contiguous chunk of physical memory for the le buffer. */
192 	error = uvm_pglistalloc(OIOC_LANCE_NPAGES * PAGE_SIZE,
193 	    avail_start, avail_end, PAGE_SIZE, 0, &mlist, 1, 0);
194 	if (error) {
195 		aprint_error(": failed to allocate ioc<->lance buffer space, "
196 		    "error = %d\n", error);
197 		goto fail_3;
198 	}
199 
200 	/* Use IOC to map the physical memory into the Ethernet chip's space. */
201 	for (i = 0; i < OIOC_LANCE_NPAGES; i++) {
202 		bus_space_write_2(lesc->sc_st,lesc->sc_maph,
203 		    OIOC_ENET_PGMAP_OFF(i),
204 		    (VM_PAGE_TO_PHYS(mlist.tqh_first) >> PAGE_SHIFT) + i);
205 	}
206 
207 	sc->sc_mem = (void *)MIPS_PHYS_TO_KSEG1(
208 	    (uint32_t)VM_PAGE_TO_PHYS(mlist.tqh_first));
209 	sc->sc_memsize = OIOC_LANCE_NPAGES * PAGE_SIZE;
210 	sc->sc_addr = 0;
211 	sc->sc_conf3 = LE_C3_BSWP;
212 
213 	ether_aton_r(enaddr, sizeof(enaddr), enaddrstr);
214 	memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
215 
216 	if (cpu_intr_establish(oa->oa_irq, IPL_NET, am7990_intr, sc) == NULL) {
217 		aprint_error(": failed to establish interrupt %d\n",oa->oa_irq);
218 		goto fail_4;
219 	}
220 
221 	sc->sc_copytodesc   = lance_copytobuf_contig;
222 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
223 	sc->sc_copytobuf    = lance_copytobuf_contig;
224 	sc->sc_copyfrombuf  = lance_copyfrombuf_contig;
225 	sc->sc_zerobuf      = lance_zerobuf_contig;
226 
227 	sc->sc_rdcsr  = lerdcsr;
228 	sc->sc_wrcsr  = lewrcsr;
229 	sc->sc_hwinit = NULL;
230 
231 	format_bytes(pbuf, sizeof(pbuf), OIOC_LANCE_NPAGES * PAGE_SIZE);
232 	aprint_normal(": main memory used = %s\n", pbuf);
233 	aprint_normal("%s", device_xname(self));
234 
235 	am7990_config(&lesc->sc_am7990);
236 
237 	return;
238 
239 fail_4:
240 	uvm_pglistfree(&mlist);
241 fail_3:
242 	bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_maph);
243 fail_2:
244 	bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_raph);
245 fail_1:
246 	bus_space_unmap(oa->oa_st, oa->oa_sh, lesc->sc_rdph);
247 fail_0:
248 	return;
249 }
250