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: releng/11.0/sys/dev/pst/pst-pci.c 274678 2014-11-18 21:58:57Z jhb $");
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    /* get resources */
77274678Sjhb    rid = PCIR_BAR(0);
78101100Ssos    sc->r_mem =
79127135Snjl	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
80101100Ssos
81101100Ssos    if (!sc->r_mem)
82274678Sjhb	return ENXIO;
83101100Ssos
84101100Ssos    rid = 0x00;
85127135Snjl    sc->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
86127135Snjl				       RF_SHAREABLE | RF_ACTIVE);
87101100Ssos
88101100Ssos    /* now setup the infrastructure to talk to the device */
89254263Sscottl    pci_enable_busmaster(dev);
90101100Ssos
91101100Ssos    sc->ibase = rman_get_virtual(sc->r_mem);
92101100Ssos    sc->reg = (struct i2o_registers *)sc->ibase;
93101100Ssos    sc->dev = dev;
94229984Struckman    mtx_init(&sc->mtx, "pst lock", NULL, MTX_DEF);
95101100Ssos
96101100Ssos    if (!iop_init(sc))
97101100Ssos	return 0;
98101100Ssos    return bus_generic_attach(dev);
99101100Ssos}
100101100Ssos
101114154Ssosstatic int
102114154Ssosiop_pci_detach(device_t dev)
103114154Ssos{
104114154Ssos    struct iop_softc *sc = device_get_softc(dev);
105274678Sjhb    int error;
106114154Ssos
107274678Sjhb    error = bus_generic_detach(dev);
108274678Sjhb    if (error)
109274678Sjhb	    return (error);
110114154Ssos    bus_teardown_intr(dev, sc->r_irq, sc->handle);
111114154Ssos    bus_release_resource(dev, SYS_RES_IRQ, 0x00, sc->r_irq);
112274678Sjhb    bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->r_mem);
113274678Sjhb    mtx_destroy(&sc->mtx);
114274678Sjhb    return (0);
115114154Ssos}
116114154Ssos
117114154Ssos
118101100Ssosstatic device_method_t pst_pci_methods[] = {
119101100Ssos    DEVMETHOD(device_probe,		iop_pci_probe),
120101100Ssos    DEVMETHOD(device_attach,		iop_pci_attach),
121114154Ssos    DEVMETHOD(device_detach,		iop_pci_detach),
122101100Ssos    { 0, 0 }
123101100Ssos};
124101100Ssos
125101100Ssosstatic driver_t pst_pci_driver = {
126101100Ssos    "pstpci",
127101100Ssos    pst_pci_methods,
128101100Ssos    sizeof(struct iop_softc),
129101100Ssos};
130101100Ssos
131101100Ssosstatic devclass_t pst_pci_devclass;
132101100Ssos
133101100SsosDRIVER_MODULE(pstpci, pci, pst_pci_driver, pst_pci_devclass, 0, 0);
134