xref: /netbsd-src/sys/arch/macppc/dev/if_wi_obio.c (revision cfe2093dadc7e1a4babcbbd408b815b407449711)
1*cfe2093dSrin /*	$NetBSD: if_wi_obio.c,v 1.25 2021/03/05 07:15:53 rin Exp $	*/
29b368d61Stsubai 
39b368d61Stsubai /*-
49b368d61Stsubai  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
59b368d61Stsubai  *
69b368d61Stsubai  * Redistribution and use in source and binary forms, with or without
79b368d61Stsubai  * modification, are permitted provided that the following conditions
89b368d61Stsubai  * are met:
99b368d61Stsubai  * 1. Redistributions of source code must retain the above copyright
109b368d61Stsubai  *    notice, this list of conditions and the following disclaimer.
119b368d61Stsubai  * 2. Redistributions in binary form must reproduce the above copyright
129b368d61Stsubai  *    notice, this list of conditions and the following disclaimer in the
139b368d61Stsubai  *    documentation and/or other materials provided with the distribution.
149b368d61Stsubai  * 3. The name of the author may not be used to endorse or promote products
159b368d61Stsubai  *    derived from this software without specific prior written permission.
169b368d61Stsubai  *
179b368d61Stsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189b368d61Stsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199b368d61Stsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209b368d61Stsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219b368d61Stsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229b368d61Stsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239b368d61Stsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249b368d61Stsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259b368d61Stsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269b368d61Stsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279b368d61Stsubai  */
289b368d61Stsubai 
294b2744bfSlukem #include <sys/cdefs.h>
30*cfe2093dSrin __KERNEL_RCSID(0, "$NetBSD: if_wi_obio.c,v 1.25 2021/03/05 07:15:53 rin Exp $");
314b2744bfSlukem 
329b368d61Stsubai #include "opt_inet.h"
339b368d61Stsubai 
349b368d61Stsubai #include <sys/param.h>
359b368d61Stsubai #include <sys/callout.h>
369b368d61Stsubai #include <sys/device.h>
379b368d61Stsubai #include <sys/socket.h>
389b368d61Stsubai #include <sys/systm.h>
399b368d61Stsubai 
409b368d61Stsubai #ifdef INET
419b368d61Stsubai #include <net/if.h>
429b368d61Stsubai #include <net/if_ether.h>
439b368d61Stsubai #include <net/if_media.h>
44ef743c64Sdyoung #include <net80211/ieee80211_var.h>
4523d8f486Sdyoung #include <net80211/ieee80211_radiotap.h>
46609617afSdyoung #include <net80211/ieee80211_rssadapt.h>
479b368d61Stsubai #endif
489b368d61Stsubai 
499b368d61Stsubai #include <machine/autoconf.h>
50b6584574Sdyoung #include <sys/bus.h>
519b368d61Stsubai 
529b368d61Stsubai #include <dev/ic/wi_ieee.h>
539b368d61Stsubai #include <dev/ic/wireg.h>
549b368d61Stsubai #include <dev/ic/wivar.h>
558bcef57bSmacallan #include <macppc/dev/obiovar.h>
569b368d61Stsubai 
5705b09539Smatt static int wi_obio_match(device_t, cfdata_t, void *);
5805b09539Smatt static void wi_obio_attach(device_t, device_t, void *);
59301198acShe static int wi_obio_enable(device_t, int);
609b368d61Stsubai 
619b368d61Stsubai struct wi_obio_softc {
629b368d61Stsubai 	struct wi_softc sc_wi;
63d974db0aSgarbled 	bus_space_tag_t sc_tag;
649b368d61Stsubai };
659b368d61Stsubai 
66293786a1Smacallan CFATTACH_DECL_NEW(wi_obio, sizeof(struct wi_obio_softc),
67c5e91d44Sthorpej     wi_obio_match, wi_obio_attach, NULL, NULL);
689b368d61Stsubai 
699b368d61Stsubai int
wi_obio_match(device_t parent,cfdata_t match,void * aux)7005b09539Smatt wi_obio_match(device_t parent, cfdata_t match, void *aux)
719b368d61Stsubai {
729b368d61Stsubai 	struct confargs *ca = aux;
739b368d61Stsubai 
749b368d61Stsubai 	if (ca->ca_nintr < 4 || ca->ca_nreg < 8)
759b368d61Stsubai 		return 0;
769b368d61Stsubai 
779b368d61Stsubai 	if (strcmp(ca->ca_name, "radio") != 0)
789b368d61Stsubai 		return 0;
799b368d61Stsubai 
809b368d61Stsubai 	return 1;
819b368d61Stsubai }
829b368d61Stsubai 
838bcef57bSmacallan #define OBIO_WI_FCR2	0x40
848bcef57bSmacallan #define OBIO_WI_GPIO	0x6a
858bcef57bSmacallan #define OBIO_WI_EXTINT	0x58
868bcef57bSmacallan 
879b368d61Stsubai void
wi_obio_attach(device_t parent,device_t self,void * aux)88293786a1Smacallan wi_obio_attach(device_t parent, device_t self, void *aux)
899b368d61Stsubai {
90293786a1Smacallan 	struct wi_obio_softc * const sc = device_private(self);
91d974db0aSgarbled 	struct wi_softc * const wisc = &sc->sc_wi;
92d974db0aSgarbled 	struct confargs * const ca = aux;
939b368d61Stsubai 
94d974db0aSgarbled 	aprint_normal(" irq %d:", ca->ca_intr[0]);
95*cfe2093dSrin 	intr_establish_xname(ca->ca_intr[0], IST_LEVEL, IPL_NET, wi_intr, sc,
96*cfe2093dSrin 	    device_xname(self));
979b368d61Stsubai 
98293786a1Smacallan 	wisc->sc_dev = self;
99f0960084Smacallan 	sc->sc_tag = wisc->sc_iot = ca->ca_tag;
100d974db0aSgarbled 
101f0960084Smacallan 	if (bus_space_map(wisc->sc_iot, ca->ca_baseaddr + ca->ca_reg[0],
102f0960084Smacallan 	    ca->ca_reg[1], 0, &wisc->sc_ioh)) {
1039b368d61Stsubai 		printf(" can't map i/o space\n");
1049b368d61Stsubai 		return;
1059b368d61Stsubai 	}
1069b368d61Stsubai 
1079b368d61Stsubai 	wisc->sc_enabled = 1;
1089b368d61Stsubai 	wisc->sc_enable = wi_obio_enable;
1099b368d61Stsubai 
1105e289bdfSmycroft 	if (wi_attach(wisc, 0)) {
111cbab9cadSchs 		printf("%s: failed to attach controller\n", device_xname(self));
1129b368d61Stsubai 		return;
1139b368d61Stsubai 	}
1149b368d61Stsubai 
1154c1d81b2Sjmcneill 	if (!pmf_device_register(self, NULL, NULL))
1164c1d81b2Sjmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
1174c1d81b2Sjmcneill 	else
1181e6d2e66Smacallan 		pmf_class_network_register(self, &sc->sc_wi.sc_if);
1199b368d61Stsubai 
1209b368d61Stsubai 	/* Disable the card. */
1219b368d61Stsubai 	wisc->sc_enabled = 0;
122301198acShe 	wi_obio_enable(self, 0);
1239b368d61Stsubai }
1249b368d61Stsubai 
1259b368d61Stsubai int
wi_obio_enable(device_t self,int enable)126301198acShe wi_obio_enable(device_t self, int enable)
1279b368d61Stsubai {
128d974db0aSgarbled 	uint32_t x;
1299b368d61Stsubai 
130301198acShe 	if (enable) {
1318bcef57bSmacallan 		x = obio_read_4(OBIO_WI_FCR2);
1329b368d61Stsubai 		x |= 0x4;
1338bcef57bSmacallan 		obio_write_4(OBIO_WI_FCR2, x);
1349b368d61Stsubai 
1359b368d61Stsubai 		/* Enable card slot. */
1368bcef57bSmacallan 		obio_write_1(OBIO_WI_GPIO + 0x0f, 5);
1379b368d61Stsubai 		delay(1000);
1388bcef57bSmacallan 		obio_write_1(OBIO_WI_GPIO + 0x0f, 4);
1399b368d61Stsubai 		delay(1000);
1408bcef57bSmacallan 		x = obio_read_4(OBIO_WI_FCR2);
1419b368d61Stsubai 		x &= ~0x8000000;
1429b368d61Stsubai 
1438bcef57bSmacallan 		obio_write_4(OBIO_WI_FCR2, x);
1449b368d61Stsubai 		/* out8(gpio + 0x10, 4); */
1459b368d61Stsubai 
1468bcef57bSmacallan 		obio_write_1(OBIO_WI_EXTINT + 0x0b, 0);
1478bcef57bSmacallan 		obio_write_1(OBIO_WI_EXTINT + 0x0a, 0x28);
1488bcef57bSmacallan 		obio_write_1(OBIO_WI_EXTINT + 0x0d, 0x28);
1498bcef57bSmacallan 		obio_write_1(OBIO_WI_GPIO + 0x0d, 0x28);
1508bcef57bSmacallan 		obio_write_1(OBIO_WI_GPIO + 0x0e, 0x28);
1518bcef57bSmacallan 		obio_write_4(0x1c000, 0);
1529b368d61Stsubai 
1539b368d61Stsubai 		/* Initialize the card. */
1548bcef57bSmacallan 		obio_write_4(0x1a3e0, 0x41);
1558bcef57bSmacallan 		x = obio_read_4(OBIO_WI_FCR2);
1569b368d61Stsubai 		x |= 0x8000000;
1578bcef57bSmacallan 		obio_write_4(OBIO_WI_FCR2, x);
158301198acShe 	} else {
1598bcef57bSmacallan 		x = obio_read_4(OBIO_WI_FCR2);
1609b368d61Stsubai 		x &= ~0x4;
1618bcef57bSmacallan 		obio_write_4(OBIO_WI_FCR2, x);
1629b368d61Stsubai 		/* out8(gpio + 0x10, 0); */
1639b368d61Stsubai 	}
164301198acShe 
165301198acShe 	return 0;
166301198acShe }
167