xref: /netbsd-src/sys/arch/macppc/dev/if_wi_obio.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: if_wi_obio.c,v 1.15 2007/10/22 19:29:13 macallan Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: if_wi_obio.c,v 1.15 2007/10/22 19:29:13 macallan Exp $");
31 
32 #include "opt_inet.h"
33 
34 #include <sys/param.h>
35 #include <sys/callout.h>
36 #include <sys/device.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39 
40 #ifdef INET
41 #include <net/if.h>
42 #include <net/if_ether.h>
43 #include <net/if_media.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
46 #include <net80211/ieee80211_rssadapt.h>
47 #endif
48 
49 #include <machine/autoconf.h>
50 #include <machine/bus.h>
51 
52 #include <dev/ic/wi_ieee.h>
53 #include <dev/ic/wireg.h>
54 #include <dev/ic/wivar.h>
55 
56 static int wi_obio_match(struct device *, struct cfdata *, void *);
57 static void wi_obio_attach(struct device *, struct device *, void *);
58 static int wi_obio_enable(struct wi_softc *);
59 static void wi_obio_disable(struct wi_softc *);
60 
61 struct wi_obio_softc {
62 	struct wi_softc sc_wi;
63 	void *sc_powerhook;
64 	void *sc_sdhook;
65 	bus_space_tag_t sc_tag;
66 	bus_space_handle_t sc_bsh;
67 	bus_space_handle_t sc_fcr2h;
68 	bus_space_handle_t sc_gpioh;
69 	bus_space_handle_t sc_extint_gpioh;
70 };
71 
72 CFATTACH_DECL(wi_obio, sizeof(struct wi_obio_softc),
73     wi_obio_match, wi_obio_attach, NULL, NULL);
74 
75 int
76 wi_obio_match(struct device *parent, struct cfdata *match, void *aux)
77 {
78 	struct confargs *ca = aux;
79 
80 	if (ca->ca_nintr < 4 || ca->ca_nreg < 8)
81 		return 0;
82 
83 	if (strcmp(ca->ca_name, "radio") != 0)
84 		return 0;
85 
86 	return 1;
87 }
88 
89 void
90 wi_obio_attach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct wi_obio_softc * const sc = (void *)self;
93 	struct wi_softc * const wisc = &sc->sc_wi;
94 	struct confargs * const ca = aux;
95 
96 	aprint_normal(" irq %d:", ca->ca_intr[0]);
97 	intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_NET, wi_intr, sc);
98 
99 	sc->sc_tag = wisc->sc_iot = ca->ca_tag;
100 	bus_space_map(sc->sc_tag, 0x8000000, 0x20000, 0, &sc->sc_bsh);
101 	bus_space_subregion(sc->sc_tag, sc->sc_bsh, 0x40, 4, &sc->sc_fcr2h);
102 	bus_space_subregion(sc->sc_tag, sc->sc_bsh, 0x6a, 16, &sc->sc_gpioh);
103 	bus_space_subregion(sc->sc_tag, sc->sc_bsh, 0x58, 16, &sc->sc_extint_gpioh);
104 
105 	if (bus_space_map(wisc->sc_iot, ca->ca_baseaddr + ca->ca_reg[0],
106 	    ca->ca_reg[1], 0, &wisc->sc_ioh)) {
107 		printf(" can't map i/o space\n");
108 		return;
109 	}
110 
111 	wisc->sc_enabled = 1;
112 	wisc->sc_enable = wi_obio_enable;
113 	wisc->sc_disable = wi_obio_disable;
114 
115 	if (wi_attach(wisc, 0)) {
116 		printf("%s: failed to attach controller\n", self->dv_xname);
117 		return;
118 	}
119 
120 	sc->sc_sdhook = shutdownhook_establish(
121 	    (void (*)(void *))wi_shutdown, wisc);
122 	sc->sc_powerhook = powerhook_establish(
123 	    self->dv_xname, (void (*)(int, void *))wi_power, wisc);
124 
125 	/* Disable the card. */
126 	wisc->sc_enabled = 0;
127 	wi_obio_disable(wisc);
128 }
129 
130 int
131 wi_obio_enable(struct wi_softc *wisc)
132 {
133 	struct wi_obio_softc * const sc = (void *)wisc;
134 	uint32_t x;
135 
136 	x = bus_space_read_4(sc->sc_tag, sc->sc_fcr2h, 0);
137 	x |= 0x4;
138 	bus_space_write_4(sc->sc_tag, sc->sc_fcr2h, 0, x);
139 
140 	/* Enable card slot. */
141 	bus_space_write_1(sc->sc_tag, sc->sc_gpioh, 0x0f, 5);
142 	delay(1000);
143 	bus_space_write_1(sc->sc_tag, sc->sc_gpioh, 0x0f, 4);
144 	delay(1000);
145 	x = bus_space_read_4(sc->sc_tag, sc->sc_fcr2h, 0);
146 	x &= ~0x8000000;
147 
148 	bus_space_write_4(sc->sc_tag, sc->sc_fcr2h, 0, x);
149 	/* out8(gpio + 0x10, 4); */
150 
151 	bus_space_write_1(sc->sc_tag, sc->sc_extint_gpioh, 0x0b, 0);
152 	bus_space_write_1(sc->sc_tag, sc->sc_extint_gpioh, 0x0a, 0x28);
153 	bus_space_write_1(sc->sc_tag, sc->sc_extint_gpioh, 0x0d, 0x28);
154 	bus_space_write_1(sc->sc_tag, sc->sc_gpioh, 0x0d, 0x28);
155 	bus_space_write_1(sc->sc_tag, sc->sc_gpioh, 0x0e, 0x28);
156 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, 0x1c000, 0);
157 
158 	/* Initialize the card. */
159 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, 0x1a3e0, 0x41);
160 	x = bus_space_read_4(sc->sc_tag, sc->sc_fcr2h, 0);
161 	x |= 0x8000000;
162 	bus_space_write_4(sc->sc_tag, sc->sc_fcr2h, 0, x);
163 
164 	return 0;
165 }
166 
167 void
168 wi_obio_disable(struct wi_softc *wisc)
169 {
170 	struct wi_obio_softc * const sc = (void *)wisc;
171 	uint32_t x;
172 
173 	x = bus_space_read_4(sc->sc_tag, sc->sc_fcr2h, 0);
174 	x &= ~0x4;
175 	bus_space_write_4(sc->sc_tag, sc->sc_fcr2h, 0, x);
176 	/* out8(gpio + 0x10, 0); */
177 }
178