ncr53c500_pccard.c revision 240172
167468Snon/*	$NecBSD: ncr53c500_pisa.c,v 1.28 1998/11/26 01:59:11 honda Exp $	*/
267468Snon/*	$NetBSD$	*/
367468Snon
4139749Simp/*-
567468Snon * [Ported for FreeBSD]
667468Snon *  Copyright (c) 2000
767468Snon *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
867468Snon *      All rights reserved.
967468Snon * [NetBSD for NEC PC-98 series]
1067468Snon *  Copyright (c) 1995, 1996, 1997, 1998
1167468Snon *	NetBSD/pc98 porting staff. All rights reserved.
1267468Snon *  Copyright (c) 1995, 1996, 1997, 1998
1367468Snon *	Naofumi HONDA. All rights reserved.
1467468Snon *
1567468Snon *  Redistribution and use in source and binary forms, with or without
1667468Snon *  modification, are permitted provided that the following conditions
1767468Snon *  are met:
1867468Snon *  1. Redistributions of source code must retain the above copyright
1967468Snon *     notice, this list of conditions and the following disclaimer.
2067468Snon *  2. Redistributions in binary form must reproduce the above copyright
2167468Snon *     notice, this list of conditions and the following disclaimer in the
2267468Snon *     documentation and/or other materials provided with the distribution.
2367468Snon *  3. The name of the author may not be used to endorse or promote products
2467468Snon *     derived from this software without specific prior written permission.
2567468Snon *
2667468Snon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2767468Snon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2867468Snon * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2967468Snon * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3067468Snon * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3167468Snon * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3267468Snon * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3367468Snon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3467468Snon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3567468Snon * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3667468Snon * POSSIBILITY OF SUCH DAMAGE.
3767468Snon */
3867468Snon
39119418Sobrien#include <sys/cdefs.h>
40119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/ncv/ncr53c500_pccard.c 240172 2012-09-06 18:53:33Z jhb $");
41119418Sobrien
4267468Snon#include <sys/param.h>
43129764Simp#include <sys/errno.h>
44129764Simp#include <sys/kernel.h>
45129764Simp#include <sys/malloc.h>
46240172Sjhb#include <sys/module.h>
4767468Snon#include <sys/systm.h>
4867468Snon
4967468Snon#include <machine/bus.h>
50129764Simp#include <machine/resource.h>
51129764Simp#include <sys/rman.h>
52126928Speter#include <compat/netbsd/dvcfg.h>
5367468Snon
54240172Sjhb#include <sys/bus.h>
5567468Snon
56107362Snon#include <dev/pccard/pccardvar.h>
57107362Snon
5867468Snon#include <cam/scsi/scsi_low.h>
5967468Snon#include <cam/scsi/scsi_low_pisa.h>
6067468Snon
6167468Snon#include <dev/ncv/ncr53c500reg.h>
6267468Snon#include <dev/ncv/ncr53c500hw.h>
6367468Snon#include <dev/ncv/ncr53c500var.h>
6467468Snon
6567848Snon#define KME_KXLC004_01 0x100
6667468Snon#define OFFSET_KME_KXLC004_01 0x10
6767468Snon
6867468Snon
69129764Simp#include "pccarddevs.h"
70129764Simp
71240172Sjhbstatic int ncvprobe(device_t devi);
72240172Sjhbstatic int ncvattach(device_t devi);
7367468Snon
74240172Sjhbstatic void	ncv_card_unload(device_t);
7567468Snon
76107362Snonstatic const struct ncv_product {
77107362Snon	struct pccard_product	prod;
78107362Snon	int flags;
79107362Snon} ncv_products[] = {
80147580Simp	{ PCMCIA_CARD(EPSON, SC200), 0},
81147580Simp	{ PCMCIA_CARD(PANASONIC, KXLC002), 0xb4d00000 },
82147580Simp	{ PCMCIA_CARD(PANASONIC, KXLC003), 0xb4d00000 },	/* untested */
83147580Simp	{ PCMCIA_CARD(PANASONIC, KXLC004), 0xb4d00100 },
84147580Simp	{ PCMCIA_CARD(MACNICA, MPS100), 0xb6250000 },
85147580Simp	{ PCMCIA_CARD(MACNICA, MPS110), 0 },
86147580Simp	{ PCMCIA_CARD(NEC, PC9801N_J03R), 0 },
87147580Simp	{ PCMCIA_CARD(NEWMEDIA, BASICS_SCSI), 0 },
88147580Simp	{ PCMCIA_CARD(QLOGIC, PC05), 0x84d00000 },
89107362Snon#define FLAGS_REX5572 0x84d00000
90147580Simp	{ PCMCIA_CARD(RATOC, REX5572), FLAGS_REX5572 },
91147580Simp	{ PCMCIA_CARD(RATOC, REX9530), 0x84d00000 },
92107362Snon	{ { NULL }, 0 }
93107362Snon};
94107362Snon
9567468Snon/*
9667468Snon * Additional code for FreeBSD new-bus PCCard frontend
9767468Snon */
9867468Snon
9967468Snonstatic void
10067468Snonncv_pccard_intr(void * arg)
10167468Snon{
10267468Snon	ncvintr(arg);
10367468Snon}
10467468Snon
10567468Snonstatic void
106240172Sjhbncv_release_resource(device_t dev)
10767468Snon{
10867468Snon	struct ncv_softc	*sc = device_get_softc(dev);
10967468Snon
11067468Snon	if (sc->ncv_intrhand) {
11167468Snon		bus_teardown_intr(dev, sc->irq_res, sc->ncv_intrhand);
11267468Snon	}
11367468Snon
11467468Snon	if (sc->port_res) {
11567468Snon		bus_release_resource(dev, SYS_RES_IOPORT,
11667468Snon				     sc->port_rid, sc->port_res);
11767468Snon	}
11867468Snon
11973087Snon	if (sc->port_res_dmy) {
12073087Snon		bus_release_resource(dev, SYS_RES_IOPORT,
12173087Snon				     sc->port_rid_dmy, sc->port_res_dmy);
12273087Snon	}
12373087Snon
12467468Snon	if (sc->irq_res) {
12567468Snon		bus_release_resource(dev, SYS_RES_IRQ,
12667468Snon				     sc->irq_rid, sc->irq_res);
12767468Snon	}
12867468Snon
12967468Snon	if (sc->mem_res) {
13067468Snon		bus_release_resource(dev, SYS_RES_MEMORY,
13167468Snon				     sc->mem_rid, sc->mem_res);
13267468Snon	}
13367468Snon}
13467468Snon
13567468Snonstatic int
136240172Sjhbncv_alloc_resource(device_t dev)
13767468Snon{
13867468Snon	struct ncv_softc	*sc = device_get_softc(dev);
139240172Sjhb	u_int32_t		flags = device_get_flags(dev);
14073087Snon	u_long			ioaddr, iosize, maddr, msize;
14167468Snon	int			error;
14267468Snon	bus_addr_t		offset = 0;
14367468Snon
14467468Snon	if(flags & KME_KXLC004_01)
14567468Snon		offset = OFFSET_KME_KXLC004_01;
14667468Snon
14773087Snon	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
14873087Snon	if (error || (iosize < (offset + NCVIOSZ))) {
14973087Snon		return(ENOMEM);
15073087Snon	}
15173087Snon
15267468Snon	sc->port_rid = 0;
15367468Snon	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
15473087Snon					  ioaddr+offset, ioaddr+iosize-offset,
15573087Snon					  iosize-offset, RF_ACTIVE);
15667468Snon	if (sc->port_res == NULL) {
15767468Snon		ncv_release_resource(dev);
15867468Snon		return(ENOMEM);
15967468Snon	}
16067468Snon
16173087Snon	if (offset != 0) {
16273087Snon		sc->port_rid_dmy = 0;
16373087Snon		sc->port_res_dmy = bus_alloc_resource(dev, SYS_RES_IOPORT,
16473087Snon						&sc->port_rid_dmy,
16573087Snon						ioaddr, ioaddr+offset, offset,
16673087Snon						RF_ACTIVE);
16773087Snon		if (sc->port_res_dmy == NULL) {
16873087Snon			printf("Warning: cannot allocate IOPORT partially.\n");
16973087Snon		}
17073087Snon	} else {
17173087Snon		sc->port_rid_dmy = 0;
17273087Snon		sc->port_res_dmy = NULL;
17373087Snon	}
17473087Snon
17567468Snon	sc->irq_rid = 0;
176127135Snjl	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
177127135Snjl					     RF_ACTIVE);
17867468Snon	if (sc->irq_res == NULL) {
17967468Snon		ncv_release_resource(dev);
18067468Snon		return(ENOMEM);
18167468Snon	}
18267468Snon
18367468Snon	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
18467468Snon	if (error) {
18567468Snon		return(0);	/* XXX */
18667468Snon	}
18767468Snon
18867468Snon	/* no need to allocate memory if not configured */
18967468Snon	if (maddr == 0 || msize == 0) {
19067468Snon		return(0);
19167468Snon	}
19267468Snon
19367468Snon	sc->mem_rid = 0;
194127135Snjl	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
195127135Snjl					     RF_ACTIVE);
19667468Snon	if (sc->mem_res == NULL) {
19767468Snon		ncv_release_resource(dev);
19867468Snon		return(ENOMEM);
19967468Snon	}
20067468Snon
20167468Snon	return(0);
20267468Snon}
20367468Snon
204150396Simpstatic int
205150396Simpncv_pccard_probe(device_t dev)
206107362Snon{
207107362Snon	const struct ncv_product *pp;
208121521Simp	const char *vendorstr;
209121521Simp	const char *prodstr;
210107362Snon
211107362Snon	if ((pp = (const struct ncv_product *) pccard_product_lookup(dev,
212107362Snon	    (const struct pccard_product *) ncv_products,
213107362Snon	    sizeof(ncv_products[0]), NULL)) != NULL) {
214113315Simp		if (pp->prod.pp_name != NULL)
215113315Simp			device_set_desc(dev, pp->prod.pp_name);
216107362Snon		device_set_flags(dev, pp->flags);
217107362Snon		return(0);
218107362Snon	}
219107362Snon	if (pccard_get_vendor_str(dev, &vendorstr))
220107362Snon		return(EIO);
221107362Snon	if (pccard_get_product_str(dev, &prodstr))
222107362Snon		return(EIO);
223107362Snon	if (strcmp(vendorstr, "RATOC System Inc.") == 0 &&
224107362Snon		strncmp(prodstr, "SOUND/SCSI2 CARD", 16) == 0) {
225107362Snon		device_set_desc(dev, "RATOC REX-5572");
226107362Snon		device_set_flags(dev, FLAGS_REX5572);
227107362Snon		return (0);
228107362Snon	}
229107362Snon	return(EIO);
230107362Snon}
231107362Snon
23267468Snonstatic int
233150396Simpncv_pccard_attach(device_t dev)
23467468Snon{
23567468Snon	struct ncv_softc	*sc = device_get_softc(dev);
23667468Snon	int			error;
23767468Snon
23867468Snon	bzero(sc, sizeof(struct ncv_softc));
23967468Snon
24067468Snon	error = ncv_alloc_resource(dev);
24167468Snon	if (error) {
24267468Snon		return(error);
24367468Snon	}
24467468Snon
24567468Snon	if (ncvprobe(dev) == 0) {
24667468Snon		ncv_release_resource(dev);
24767468Snon		return(ENXIO);
24867468Snon	}
24973280Smarkm	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
250166901Spiso			       NULL, ncv_pccard_intr, (void *)sc, &sc->ncv_intrhand);
25167468Snon	if (error) {
25267468Snon		ncv_release_resource(dev);
25367468Snon		return(error);
25467468Snon	}
25567468Snon
25667468Snon	if (ncvattach(dev) == 0) {
25767468Snon		ncv_release_resource(dev);
25867468Snon		return(ENXIO);
25967468Snon	}
26067468Snon
26167468Snon	return(0);
26267468Snon}
26367468Snon
264194023Savgstatic	int
265150396Simpncv_pccard_detach(device_t dev)
26667468Snon{
26767468Snon	ncv_card_unload(dev);
26867468Snon	ncv_release_resource(dev);
269194023Savg
270194023Savg	return (0);
27167468Snon}
27267468Snon
27367468Snonstatic device_method_t ncv_pccard_methods[] = {
27467468Snon	/* Device interface */
275150396Simp	DEVMETHOD(device_probe,		ncv_pccard_probe),
276150396Simp	DEVMETHOD(device_attach,	ncv_pccard_attach),
27767468Snon	DEVMETHOD(device_detach,	ncv_pccard_detach),
27867468Snon
27967468Snon	{ 0, 0 }
28067468Snon};
28167468Snon
28267468Snonstatic driver_t ncv_pccard_driver = {
28367468Snon	"ncv",
28467468Snon	ncv_pccard_methods,
28567468Snon	sizeof(struct ncv_softc),
28667468Snon};
28767468Snon
28867468Snonstatic devclass_t ncv_devclass;
28967468Snon
29087983SnonMODULE_DEPEND(ncv, scsi_low, 1, 1, 1);
29167468SnonDRIVER_MODULE(ncv, pccard, ncv_pccard_driver, ncv_devclass, 0, 0);
29267468Snon
29367468Snonstatic void
294240172Sjhbncv_card_unload(device_t devi)
29567468Snon{
296240172Sjhb	struct ncv_softc *sc = device_get_softc(devi);
29779697Snon	intrmask_t s;
29867468Snon
29979697Snon	s = splcam();
30067468Snon	scsi_low_deactivate((struct scsi_low_softc *)sc);
30167468Snon        scsi_low_dettach(&sc->sc_sclow);
30279697Snon	splx(s);
30367468Snon}
30467468Snon
30567468Snonstatic int
306240172Sjhbncvprobe(device_t devi)
30767468Snon{
30867468Snon	int rv;
30967468Snon	struct ncv_softc *sc = device_get_softc(devi);
310240172Sjhb	u_int32_t flags = device_get_flags(devi);
31167468Snon
31267468Snon	rv = ncvprobesubr(rman_get_bustag(sc->port_res),
31367468Snon			  rman_get_bushandle(sc->port_res),
31467468Snon			  flags, NCV_HOSTID);
31567468Snon
31667468Snon	return rv;
31767468Snon}
31867468Snon
31967468Snonstatic int
320240172Sjhbncvattach(device_t devi)
32167468Snon{
32267468Snon	struct ncv_softc *sc;
32367468Snon	struct scsi_low_softc *slp;
324240172Sjhb	u_int32_t flags = device_get_flags(devi);
32579697Snon	intrmask_t s;
32667468Snon	char dvname[16]; /* SCSI_LOW_DVNAME_LEN */
32767468Snon
32867468Snon	strcpy(dvname, "ncv");
32967468Snon
330240172Sjhb	sc = device_get_softc(devi);
33167468Snon	if (sc == NULL) {
33267468Snon		return(0);
33367468Snon	}
33467468Snon
33567468Snon	slp = &sc->sc_sclow;
33667468Snon	slp->sl_dev = devi;
33767468Snon	sc->sc_iot = rman_get_bustag(sc->port_res);
33867468Snon	sc->sc_ioh = rman_get_bushandle(sc->port_res);
33967468Snon
34067468Snon	slp->sl_hostid = NCV_HOSTID;
34167468Snon	slp->sl_cfgflags = flags;
34267468Snon
34379697Snon	s = splcam();
34467468Snon	ncvattachsubr(sc);
34579697Snon	splx(s);
34667468Snon
34767468Snon	return(NCVIOSZ);
34867468Snon}
349