if_snc_cbus.c revision 242871
1139735Simp/*-
2129198Scognet * Copyright (c) 1995, David Greenman
3129198Scognet * All rights reserved.
4129198Scognet *
5129198Scognet * Redistribution and use in source and binary forms, with or without
6129198Scognet * modification, are permitted provided that the following conditions
7129198Scognet * are met:
8129198Scognet * 1. Redistributions of source code must retain the above copyright
9129198Scognet *    notice unmodified, this list of conditions, and the following
10129198Scognet *    disclaimer.
11129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
12129198Scognet *    notice, this list of conditions and the following disclaimer in the
13129198Scognet *    documentation and/or other materials provided with the distribution.
14129198Scognet *
15182934Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25129198Scognet * SUCH DAMAGE.
26129198Scognet *
27129198Scognet */
28129198Scognet
29129198Scognet#include <sys/cdefs.h>
30129198Scognet__FBSDID("$FreeBSD: head/sys/dev/snc/if_snc_cbus.c 242871 2012-11-10 14:58:06Z nyan $");
31129198Scognet
32129198Scognet/*
33129198Scognet *	National Semiconductor  DP8393X SONIC Driver
34129198Scognet *
35129198Scognet *	This is the C-bus specific attachment on FreeBSD
36129198Scognet *		written by Motomichi Matsuzaki <mzaki@e-mail.ne.jp>
37129198Scognet */
38129198Scognet
39129198Scognet#include <sys/param.h>
40129198Scognet#include <sys/systm.h>
41129198Scognet#include <sys/socket.h>
42129198Scognet#include <sys/kernel.h>
43129198Scognet
44129198Scognet#include <sys/module.h>
45129198Scognet#include <sys/bus.h>
46129198Scognet#include <machine/bus.h>
47129198Scognet#include <machine/resource.h>
48129198Scognet
49129198Scognet#include <net/if.h>
50129198Scognet#include <net/if_arp.h>
51129198Scognet#include <net/if_media.h>
52129198Scognet
53129198Scognet#include <isa/isavar.h>
54129198Scognet#include <sys/malloc.h>		/* as dependency for isa/isa_common.h */
55129198Scognet#include <isa/isa_common.h>	/* for snc_isapnp_reconfig() */
56129198Scognet
57129198Scognet#include <dev/snc/dp83932var.h>
58129198Scognet#include <dev/snc/if_sncreg.h>
59129198Scognet#include <dev/snc/if_sncvar.h>
60129198Scognet
61129198Scognetstatic void snc_isapnp_reconfig	(device_t);
62129198Scognetstatic int snc_isa_probe	(device_t);
63266000Sianstatic int snc_isa_attach	(device_t);
64266000Sian
65266000Sianstatic struct isa_pnp_id snc_ids[] = {
66266079Sian	{ 0x6180a3b8,	NULL },		/* NEC8061 NEC PC-9801-104 */
67266000Sian	{ 0,		NULL }
68266079Sian};
69266160Sian
70266160Sianstatic void
71129198Scognetsnc_isapnp_reconfig(device_t dev)
72129198Scognet{
73129198Scognet	struct isa_device *idev = DEVTOISA(dev);
74129198Scognet        struct isa_config config;
75129198Scognet	u_long start, count;
76129198Scognet	int rid;
77129198Scognet
78129198Scognet	bzero(&config, sizeof(config));
79129198Scognet
80129198Scognet	for (rid = 0; rid < ISA_NMEM; rid++) {
81129198Scognet		if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count))
82129198Scognet			break;
83129198Scognet		config.ic_mem[rid].ir_start = start;
84212413Savg		config.ic_mem[rid].ir_end = start;
85129198Scognet		config.ic_mem[rid].ir_size = count;
86182934Sraj	}
87129198Scognet	config.ic_nmem = rid;
88182934Sraj	for (rid = 0; rid < ISA_NPORT; rid++) {
89266070Sian		if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count))
90266070Sian			break;
91266000Sian		config.ic_port[rid].ir_start = start;
92266000Sian		config.ic_port[rid].ir_end = start;
93266000Sian		config.ic_port[rid].ir_size = count;
94182934Sraj	}
95182934Sraj	config.ic_nport = rid;
96182934Sraj	for (rid = 0; rid < ISA_NIRQ; rid++) {
97147166Scognet		if (bus_get_resource(dev, SYS_RES_IRQ, rid, &start, &count))
98266079Sian			break;
99266079Sian		config.ic_irqmask[rid] = 1 << start;
100266128Sian	}
101266079Sian	config.ic_nirq = rid;
102266079Sian	for (rid = 0; rid < ISA_NDRQ; rid++) {
103129198Scognet		if (bus_get_resource(dev, SYS_RES_DRQ, rid, &start, &count))
104129198Scognet			break;
105129198Scognet		config.ic_drqmask[rid] = 1 << start;
106129198Scognet	}
107129198Scognet	config.ic_ndrq = rid;
108129198Scognet
109129198Scognet	idev->id_config_cb(idev->id_config_arg, &config, 1);
110129198Scognet}
111129198Scognet
112266070Sianstatic int
113266000Siansnc_isa_probe(device_t dev)
114129198Scognet{
115147166Scognet	struct snc_softc *sc = device_get_softc(dev);
116266079Sian	int type;
117266079Sian 	int error = 0;
118266079Sian
119129198Scognet	bzero(sc, sizeof(struct snc_softc));
120129198Scognet
121129198Scognet	/* Check isapnp ids */
122266000Sian	error = ISA_PNP_PROBE(device_get_parent(dev), dev, snc_ids);
123129198Scognet
124129198Scognet	/* If the card had a PnP ID that didn't match any we know about */
125129198Scognet	if (error == ENXIO) {
126129198Scognet		return(error);
127129198Scognet	}
128270075Sian
129270075Sian	switch (error) {
130129198Scognet	case 0:		/* Matched PnP */
131129198Scognet		type = SNEC_TYPE_PNP;
132129198Scognet		break;
133129198Scognet
134209129Sraj	case ENOENT:	/* Legacy ISA */
135129198Scognet		type = SNEC_TYPE_LEGACY;
136182934Sraj		break;
137209129Sraj
138129198Scognet	default:	/* If we had some other problem. */
139129198Scognet		return(error);
140129198Scognet	}
141129198Scognet
142129198Scognet	if (type == SNEC_TYPE_PNP && isa_get_portsize(dev) == 0) {
143182934Sraj		int port;
144209232Sraj		int rid = 0;
145221218Sjhb		struct resource *res = NULL;
146209232Sraj
147209232Sraj		for (port = 0x0888; port <= 0x3888; port += 0x1000) {
148221218Sjhb			bus_set_resource(dev, SYS_RES_IOPORT, rid,
149209232Sraj					 port, SNEC_NREGS);
150209232Sraj			res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
151129198Scognet						 0ul, ~0ul, SNEC_NREGS,
152129198Scognet						 0 /* !RF_ACTIVE */);
153129198Scognet			if (res) break;
154129198Scognet		}
155129198Scognet
156182934Sraj		printf("snc_isa_probe: broken PnP resource, ");
157182934Sraj		if (res) {
158129198Scognet			printf("use port 0x%x\n", port);
159129198Scognet			bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
160129198Scognet			snc_isapnp_reconfig(dev);
161129198Scognet		} else {
162129198Scognet			printf("and can't find port\n");
163129198Scognet		}
164182934Sraj	}
165129198Scognet
166235907Sgber	error = snc_alloc_port(dev, 0);
167182934Sraj	error = max(error, snc_alloc_memory(dev, 0));
168129198Scognet	error = max(error, snc_alloc_irq(dev, 0, 0));
169129198Scognet
170129198Scognet	if (!error && !snc_probe(dev, type))
171129198Scognet		error = ENOENT;
172212413Savg
173129198Scognet	snc_release_resources(dev);
174182934Sraj	return (error);
175129198Scognet}
176182934Sraj
177129198Scognetstatic int
178129198Scognetsnc_isa_attach(device_t dev)
179182934Sraj{
180129198Scognet	struct snc_softc *sc = device_get_softc(dev);
181129198Scognet
182129198Scognet	bzero(sc, sizeof(struct snc_softc));
183182934Sraj
184129198Scognet	snc_alloc_port(dev, 0);
185129198Scognet	snc_alloc_memory(dev, 0);
186182934Sraj	snc_alloc_irq(dev, 0, 0);
187182934Sraj
188129198Scognet	/* This interface is always enabled. */
189129198Scognet	sc->sc_enabled = 1;
190129198Scognet
191129198Scognet	return snc_attach(dev);
192129198Scognet}
193129198Scognet
194129198Scognetstatic device_method_t snc_isa_methods[] = {
195129198Scognet	/* Device interface */
196129198Scognet	DEVMETHOD(device_probe,		snc_isa_probe),
197129198Scognet	DEVMETHOD(device_attach,	snc_isa_attach),
198182934Sraj	DEVMETHOD(device_shutdown,	snc_shutdown),
199129198Scognet
200129198Scognet	{ 0, 0 }
201129198Scognet};
202129198Scognet
203129198Scognetstatic driver_t snc_isa_driver = {
204129198Scognet	"snc",
205129198Scognet	snc_isa_methods,
206266000Sian	sizeof(struct snc_softc)
207129198Scognet};
208129198Scognet
209182934SrajDRIVER_MODULE(snc, isa, snc_isa_driver, snc_devclass, 0, 0);
210129198ScognetMODULE_DEPEND(snc, isa, 1, 1, 1);
211182934SrajMODULE_DEPEND(snc, ether, 1, 1, 1);
212129198Scognet