1101100Ssos/*-
2230132Suqs * Copyright (c) 2001,2002,2003 S��ren Schmidt <sos@FreeBSD.org>
3101100Ssos * All rights reserved.
4101100Ssos *
5101100Ssos * Redistribution and use in source and binary forms, with or without
6101100Ssos * modification, are permitted provided that the following conditions
7101100Ssos * are met:
8101100Ssos * 1. Redistributions of source code must retain the above copyright
9101100Ssos *    notice, this list of conditions and the following disclaimer,
10101100Ssos *    without modification, immediately at the beginning of the file.
11101100Ssos * 2. Redistributions in binary form must reproduce the above copyright
12101100Ssos *    notice, this list of conditions and the following disclaimer in the
13101100Ssos *    documentation and/or other materials provided with the distribution.
14101100Ssos * 3. The name of the author may not be used to endorse or promote products
15101100Ssos *    derived from this software without specific prior written permission.
16101100Ssos *
17101100Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18101100Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19101100Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20101100Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21101100Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22101100Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23101100Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24101100Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25101100Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26101100Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27101100Ssos */
28101100Ssos
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD$");
31119418Sobrien
32101100Ssos#include <sys/param.h>
33101100Ssos#include <sys/systm.h>
34101100Ssos#include <sys/kernel.h>
35101100Ssos#include <sys/module.h>
36101100Ssos#include <sys/bus.h>
37101100Ssos#include <sys/bio.h>
38101100Ssos#include <sys/malloc.h>
39114154Ssos#include <sys/lock.h>
40114154Ssos#include <sys/mutex.h>
41101100Ssos#include <vm/vm.h>
42101100Ssos#include <vm/pmap.h>
43101100Ssos#include <machine/stdarg.h>
44101100Ssos#include <machine/resource.h>
45101100Ssos#include <machine/bus.h>
46101100Ssos#include <sys/rman.h>
47119285Simp#include <dev/pci/pcivar.h>
48119285Simp#include <dev/pci/pcireg.h>
49101100Ssos
50101100Ssos#include "dev/pst/pst-iop.h"
51101100Ssos
52101100Ssosstatic int
53101100Ssosiop_pci_probe(device_t dev)
54101100Ssos{
55101100Ssos    /* tested with actual hardware kindly donated by Promise */
56101100Ssos    if (pci_get_devid(dev) == 0x19628086 && pci_get_subvendor(dev) == 0x105a) {
57101100Ssos	device_set_desc(dev, "Promise SuperTrak SX6000 ATA RAID controller");
58143158Simp	return BUS_PROBE_DEFAULT;
59101100Ssos    }
60101100Ssos
61108126Ssos    /* support the older SuperTrak 100 as well */
62108126Ssos    if (pci_get_devid(dev) == 0x19608086 && pci_get_subvendor(dev) == 0x105a) {
63101100Ssos	device_set_desc(dev, "Promise SuperTrak 100 ATA RAID controller");
64143158Simp	return BUS_PROBE_DEFAULT;
65101100Ssos    }
66101100Ssos
67101100Ssos    return ENXIO;
68101100Ssos}
69101100Ssos
70101100Ssosstatic int
71101100Ssosiop_pci_attach(device_t dev)
72101100Ssos{
73101100Ssos    struct iop_softc *sc = device_get_softc(dev);
74101100Ssos    int rid;
75101100Ssos
76101100Ssos    bzero(sc, sizeof(struct iop_softc));
77101100Ssos
78101100Ssos    /* get resources */
79101100Ssos    rid = 0x10;
80101100Ssos    sc->r_mem =
81127135Snjl	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
82101100Ssos
83101100Ssos    if (!sc->r_mem)
84101100Ssos	return 0;
85101100Ssos
86101100Ssos    rid = 0x00;
87127135Snjl    sc->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
88127135Snjl				       RF_SHAREABLE | RF_ACTIVE);
89101100Ssos
90101100Ssos    /* now setup the infrastructure to talk to the device */
91254263Sscottl    pci_enable_busmaster(dev);
92101100Ssos
93101100Ssos    sc->ibase = rman_get_virtual(sc->r_mem);
94101100Ssos    sc->reg = (struct i2o_registers *)sc->ibase;
95101100Ssos    sc->dev = dev;
96229984Struckman    mtx_init(&sc->mtx, "pst lock", NULL, MTX_DEF);
97101100Ssos
98101100Ssos    if (!iop_init(sc))
99101100Ssos	return 0;
100101100Ssos    return bus_generic_attach(dev);
101101100Ssos}
102101100Ssos
103114154Ssosstatic int
104114154Ssosiop_pci_detach(device_t dev)
105114154Ssos{
106114154Ssos    struct iop_softc *sc = device_get_softc(dev);
107114154Ssos
108114154Ssos    bus_teardown_intr(dev, sc->r_irq, sc->handle);
109114154Ssos    bus_release_resource(dev, SYS_RES_IRQ, 0x00, sc->r_irq);
110114154Ssos    bus_release_resource(dev, SYS_RES_MEMORY, 0x10, sc->r_mem);
111114154Ssos    return bus_generic_detach(dev);
112114154Ssos}
113114154Ssos
114114154Ssos
115101100Ssosstatic device_method_t pst_pci_methods[] = {
116101100Ssos    DEVMETHOD(device_probe,		iop_pci_probe),
117101100Ssos    DEVMETHOD(device_attach,		iop_pci_attach),
118114154Ssos    DEVMETHOD(device_detach,		iop_pci_detach),
119101100Ssos    { 0, 0 }
120101100Ssos};
121101100Ssos
122101100Ssosstatic driver_t pst_pci_driver = {
123101100Ssos    "pstpci",
124101100Ssos    pst_pci_methods,
125101100Ssos    sizeof(struct iop_softc),
126101100Ssos};
127101100Ssos
128101100Ssosstatic devclass_t pst_pci_devclass;
129101100Ssos
130101100SsosDRIVER_MODULE(pstpci, pci, pst_pci_driver, pst_pci_devclass, 0, 0);
131