ncr53c500_pccard.c revision 139749
1/*	$NecBSD: ncr53c500_pisa.c,v 1.28 1998/11/26 01:59:11 honda Exp $	*/
2/*	$NetBSD$	*/
3
4/*-
5 * [Ported for FreeBSD]
6 *  Copyright (c) 2000
7 *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
8 *      All rights reserved.
9 * [NetBSD for NEC PC-98 series]
10 *  Copyright (c) 1995, 1996, 1997, 1998
11 *	NetBSD/pc98 porting staff. All rights reserved.
12 *  Copyright (c) 1995, 1996, 1997, 1998
13 *	Naofumi HONDA. All rights reserved.
14 *
15 *  Redistribution and use in source and binary forms, with or without
16 *  modification, are permitted provided that the following conditions
17 *  are met:
18 *  1. Redistributions of source code must retain the above copyright
19 *     notice, this list of conditions and the following disclaimer.
20 *  2. Redistributions in binary form must reproduce the above copyright
21 *     notice, this list of conditions and the following disclaimer in the
22 *     documentation and/or other materials provided with the distribution.
23 *  3. The name of the author may not be used to endorse or promote products
24 *     derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/dev/ncv/ncr53c500_pccard.c 139749 2005-01-06 01:43:34Z imp $");
41
42#include <sys/param.h>
43#include <sys/errno.h>
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#include <sys/systm.h>
47
48#include <machine/bus.h>
49#include <machine/bus_pio.h>
50#include <machine/resource.h>
51#include <sys/rman.h>
52#include <compat/netbsd/dvcfg.h>
53
54#include <sys/device_port.h>
55
56#include <dev/pccard/pccardvar.h>
57
58#include <cam/scsi/scsi_low.h>
59#include <cam/scsi/scsi_low_pisa.h>
60
61#include <dev/ncv/ncr53c500reg.h>
62#include <dev/ncv/ncr53c500hw.h>
63#include <dev/ncv/ncr53c500var.h>
64
65#define KME_KXLC004_01 0x100
66#define OFFSET_KME_KXLC004_01 0x10
67
68
69#include "pccarddevs.h"
70
71static int ncvprobe(DEVPORT_PDEVICE devi);
72static int ncvattach(DEVPORT_PDEVICE devi);
73
74static void	ncv_card_unload(DEVPORT_PDEVICE);
75
76static const struct ncv_product {
77	struct pccard_product	prod;
78	int flags;
79} ncv_products[] = {
80	{ PCMCIA_CARD(EPSON, SC200, 0), 0},
81	{ PCMCIA_CARD(PANASONIC, KXLC002, 0), 0xb4d00000 },
82	{ PCMCIA_CARD(PANASONIC, KXLC004, 0), 0xb4d00100 },
83	{ PCMCIA_CARD(MACNICA, MPS100, 0), 0xb6250000 },
84	{ PCMCIA_CARD(MACNICA, MPS110, 0), 0 },
85	{ PCMCIA_CARD(NEC, PC9801N_J03R, 0), 0 },
86	{ PCMCIA_CARD(NEWMEDIA, BASICS_SCSI, 0), 0 },
87	{ PCMCIA_CARD(QLOGIC, PC05, 0), 0x84d00000 },
88#define FLAGS_REX5572 0x84d00000
89	{ PCMCIA_CARD(RATOC, REX5572, 0), FLAGS_REX5572 },
90	{ PCMCIA_CARD(RATOC, REX9530, 0), 0x84d00000 },
91	{ { NULL }, 0 }
92};
93
94/*
95 * Additional code for FreeBSD new-bus PCCard frontend
96 */
97
98static void
99ncv_pccard_intr(void * arg)
100{
101	ncvintr(arg);
102}
103
104static void
105ncv_release_resource(DEVPORT_PDEVICE dev)
106{
107	struct ncv_softc	*sc = device_get_softc(dev);
108
109	if (sc->ncv_intrhand) {
110		bus_teardown_intr(dev, sc->irq_res, sc->ncv_intrhand);
111	}
112
113	if (sc->port_res) {
114		bus_release_resource(dev, SYS_RES_IOPORT,
115				     sc->port_rid, sc->port_res);
116	}
117
118	if (sc->port_res_dmy) {
119		bus_release_resource(dev, SYS_RES_IOPORT,
120				     sc->port_rid_dmy, sc->port_res_dmy);
121	}
122
123	if (sc->irq_res) {
124		bus_release_resource(dev, SYS_RES_IRQ,
125				     sc->irq_rid, sc->irq_res);
126	}
127
128	if (sc->mem_res) {
129		bus_release_resource(dev, SYS_RES_MEMORY,
130				     sc->mem_rid, sc->mem_res);
131	}
132}
133
134static int
135ncv_alloc_resource(DEVPORT_PDEVICE dev)
136{
137	struct ncv_softc	*sc = device_get_softc(dev);
138	u_int32_t		flags = DEVPORT_PDEVFLAGS(dev);
139	u_long			ioaddr, iosize, maddr, msize;
140	int			error;
141	bus_addr_t		offset = 0;
142
143	if(flags & KME_KXLC004_01)
144		offset = OFFSET_KME_KXLC004_01;
145
146	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
147	if (error || (iosize < (offset + NCVIOSZ))) {
148		return(ENOMEM);
149	}
150
151	sc->port_rid = 0;
152	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
153					  ioaddr+offset, ioaddr+iosize-offset,
154					  iosize-offset, RF_ACTIVE);
155	if (sc->port_res == NULL) {
156		ncv_release_resource(dev);
157		return(ENOMEM);
158	}
159
160	if (offset != 0) {
161		sc->port_rid_dmy = 0;
162		sc->port_res_dmy = bus_alloc_resource(dev, SYS_RES_IOPORT,
163						&sc->port_rid_dmy,
164						ioaddr, ioaddr+offset, offset,
165						RF_ACTIVE);
166		if (sc->port_res_dmy == NULL) {
167			printf("Warning: cannot allocate IOPORT partially.\n");
168		}
169	} else {
170		sc->port_rid_dmy = 0;
171		sc->port_res_dmy = NULL;
172	}
173
174	sc->irq_rid = 0;
175	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
176					     RF_ACTIVE);
177	if (sc->irq_res == NULL) {
178		ncv_release_resource(dev);
179		return(ENOMEM);
180	}
181
182	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
183	if (error) {
184		return(0);	/* XXX */
185	}
186
187	/* no need to allocate memory if not configured */
188	if (maddr == 0 || msize == 0) {
189		return(0);
190	}
191
192	sc->mem_rid = 0;
193	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
194					     RF_ACTIVE);
195	if (sc->mem_res == NULL) {
196		ncv_release_resource(dev);
197		return(ENOMEM);
198	}
199
200	return(0);
201}
202
203static int ncv_pccard_match(device_t dev)
204{
205	const struct ncv_product *pp;
206	const char *vendorstr;
207	const char *prodstr;
208
209	if ((pp = (const struct ncv_product *) pccard_product_lookup(dev,
210	    (const struct pccard_product *) ncv_products,
211	    sizeof(ncv_products[0]), NULL)) != NULL) {
212		if (pp->prod.pp_name != NULL)
213			device_set_desc(dev, pp->prod.pp_name);
214		device_set_flags(dev, pp->flags);
215		return(0);
216	}
217	if (pccard_get_vendor_str(dev, &vendorstr))
218		return(EIO);
219	if (pccard_get_product_str(dev, &prodstr))
220		return(EIO);
221	if (strcmp(vendorstr, "RATOC System Inc.") == 0 &&
222		strncmp(prodstr, "SOUND/SCSI2 CARD", 16) == 0) {
223		device_set_desc(dev, "RATOC REX-5572");
224		device_set_flags(dev, FLAGS_REX5572);
225		return (0);
226	}
227	return(EIO);
228}
229
230static int
231ncv_pccard_probe(DEVPORT_PDEVICE dev)
232{
233	struct ncv_softc	*sc = device_get_softc(dev);
234	int			error;
235
236	bzero(sc, sizeof(struct ncv_softc));
237
238	error = ncv_alloc_resource(dev);
239	if (error) {
240		return(error);
241	}
242
243	if (ncvprobe(dev) == 0) {
244		ncv_release_resource(dev);
245		return(ENXIO);
246	}
247
248	ncv_release_resource(dev);
249
250	return(0);
251}
252
253static int
254ncv_pccard_attach(DEVPORT_PDEVICE dev)
255{
256	struct ncv_softc	*sc = device_get_softc(dev);
257	int			error;
258
259	error = ncv_alloc_resource(dev);
260	if (error) {
261		return(error);
262	}
263
264	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
265			       ncv_pccard_intr, (void *)sc, &sc->ncv_intrhand);
266	if (error) {
267		ncv_release_resource(dev);
268		return(error);
269	}
270
271	if (ncvattach(dev) == 0) {
272		ncv_release_resource(dev);
273		return(ENXIO);
274	}
275
276	return(0);
277}
278
279static	void
280ncv_pccard_detach(DEVPORT_PDEVICE dev)
281{
282	ncv_card_unload(dev);
283	ncv_release_resource(dev);
284}
285
286static device_method_t ncv_pccard_methods[] = {
287	/* Device interface */
288	DEVMETHOD(device_probe,		pccard_compat_probe),
289	DEVMETHOD(device_attach,	pccard_compat_attach),
290	DEVMETHOD(device_detach,	ncv_pccard_detach),
291
292	/* Card interface */
293	DEVMETHOD(card_compat_match,	ncv_pccard_match),
294	DEVMETHOD(card_compat_probe,	ncv_pccard_probe),
295	DEVMETHOD(card_compat_attach,	ncv_pccard_attach),
296
297	{ 0, 0 }
298};
299
300static driver_t ncv_pccard_driver = {
301	"ncv",
302	ncv_pccard_methods,
303	sizeof(struct ncv_softc),
304};
305
306static devclass_t ncv_devclass;
307
308MODULE_DEPEND(ncv, scsi_low, 1, 1, 1);
309DRIVER_MODULE(ncv, pccard, ncv_pccard_driver, ncv_devclass, 0, 0);
310
311static void
312ncv_card_unload(DEVPORT_PDEVICE devi)
313{
314	struct ncv_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
315	intrmask_t s;
316
317	printf("%s: unload\n", sc->sc_sclow.sl_xname);
318	s = splcam();
319	scsi_low_deactivate((struct scsi_low_softc *)sc);
320        scsi_low_dettach(&sc->sc_sclow);
321	splx(s);
322}
323
324static int
325ncvprobe(DEVPORT_PDEVICE devi)
326{
327	int rv;
328	struct ncv_softc *sc = device_get_softc(devi);
329	u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
330
331	rv = ncvprobesubr(rman_get_bustag(sc->port_res),
332			  rman_get_bushandle(sc->port_res),
333			  flags, NCV_HOSTID);
334
335	return rv;
336}
337
338static int
339ncvattach(DEVPORT_PDEVICE devi)
340{
341	struct ncv_softc *sc;
342	struct scsi_low_softc *slp;
343	u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
344	intrmask_t s;
345	char dvname[16]; /* SCSI_LOW_DVNAME_LEN */
346
347	strcpy(dvname, "ncv");
348
349	sc = DEVPORT_PDEVALLOC_SOFTC(devi);
350	if (sc == NULL) {
351		return(0);
352	}
353
354	slp = &sc->sc_sclow;
355	slp->sl_dev = devi;
356	sc->sc_iot = rman_get_bustag(sc->port_res);
357	sc->sc_ioh = rman_get_bushandle(sc->port_res);
358
359	slp->sl_hostid = NCV_HOSTID;
360	slp->sl_cfgflags = flags;
361
362	s = splcam();
363	ncvattachsubr(sc);
364	splx(s);
365
366	return(NCVIOSZ);
367}
368