ida_pci.c revision 48156
1/*-
2 * Copyright (c) 1999 Jonathan Lemon
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id$
27 */
28
29#include <pci.h>
30#if NPCI > 0
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/kernel.h>
36
37#include <sys/buf.h>
38#include <sys/bus.h>
39#include <sys/device.h>
40#include <sys/devicestat.h>
41
42#include <machine/bus_memio.h>
43#include <machine/bus_pio.h>
44#include <machine/bus.h>
45#include <machine/resource.h>
46#include <sys/rman.h>
47
48#include <pci/pcireg.h>
49#include <pci/pcivar.h>
50
51#include <dev/ida/idavar.h>
52
53#define IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
54#define IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
55
56#define IDA_PCI_MEMADDR		(PCIR_MAPS + 4)		/* Mem I/O Address */
57
58#define IDA_DEVICEID_SMART	0xAE100E11
59
60static struct {
61        u_long	board;
62        char    *desc;
63} board_id[] = {
64	{ 0x4030,	"Compaq SMART-2/P array controller" },
65	{ 0x4031,	"Compaq SMART-2SL array controller" },
66	{ 0x4032,	"Compaq Smart Array 3200 controller" },
67	{ 0x4033,	"Compaq Smart Array 3100ES controller" },
68	{ 0x4034,	"Compaq Smart Array 221 controller" },
69
70	{ 0,		"" },
71};
72
73static int ida_pci_probe(device_t dev);
74static int ida_pci_attach(device_t dev);
75static void ida_pci_print_child(device_t bus, device_t dev);
76
77static device_method_t ida_pci_methods[] = {
78	DEVMETHOD(device_probe,		ida_pci_probe),
79	DEVMETHOD(device_attach,	ida_pci_attach),
80
81	DEVMETHOD(bus_print_child,	ida_pci_print_child),
82
83	{ 0, 0 }
84};
85
86static driver_t ida_pci_driver = {
87	"ida",
88	ida_pci_methods,
89	sizeof(struct ida_softc)
90};
91
92static devclass_t ida_devclass;
93
94static int
95ida_pci_probe(device_t dev)
96{
97	u_long board;
98	int i;
99
100	if (pci_get_devid(dev) == IDA_DEVICEID_SMART) {
101		board = pci_get_subdevice(dev);
102		for (i = 0; board_id[i].board; i++) {
103			if (board_id[i].board == board) {
104				device_set_desc(dev, board_id[i].desc);
105				return (0);
106			}
107		}
108		/*
109		 * It's an unknown Compaq SMART device, but assume we
110		 * can support it.
111		 */
112		device_set_desc(dev, "Unknown Compaq Smart Array controller");
113		return (0);
114	}
115	return (ENXIO);
116}
117
118static int
119ida_pci_attach(device_t dev)
120{
121	struct ida_softc *ida;
122	u_int command;
123	int error, rid;
124
125	command = pci_read_config(dev, PCIR_COMMAND, 1);
126
127	/*
128	 * for multiple card types, need to re-determine which type is
129	 * being attached here
130	 */
131
132	/*
133	 * it appears that this board only does MEMIO access.
134	 */
135	if ((command & PCIM_CMD_MEMEN) == 0) {
136                device_printf(dev, "Only memory mapped I/O is supported\n");
137		return (ENXIO);
138	}
139
140	ida = (struct ida_softc *)device_get_softc(dev);
141	ida->dev = dev;
142
143	ida->regs_res_type = SYS_RES_MEMORY;
144	ida->regs_res_id = IDA_PCI_MEMADDR;
145	ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
146	    &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
147	if (ida->regs == NULL) {
148		device_printf(dev, "can't allocate register resources\n");
149		return (ENOMEM);
150	}
151
152	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/0,
153	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
154	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
155	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
156	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
157	    &ida->parent_dmat);
158	if (error != 0) {
159		device_printf(dev, "can't allocate DMA tag\n");
160		ida_free(ida);
161		return (ENOMEM);
162	}
163
164	rid = 0;
165        ida->irq_res_type = SYS_RES_IRQ;
166	ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
167	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
168        if (ida->irq == NULL) {
169                ida_free(ida);
170                return (ENOMEM);
171        }
172	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO,
173	    ida_intr, ida, &ida->ih);
174	if (error) {
175		device_printf(dev, "can't setup interrupt\n");
176		ida_free(ida);
177		return (ENOMEM);
178	}
179
180	error = ida_init(ida);
181	if (error) {
182                ida_free(ida);
183                return (error);
184        }
185	ida_attach(ida);
186	ida->flags = IDA_ATTACHED;
187
188	return (0);
189}
190
191static void
192ida_pci_print_child(device_t bus, device_t dev)
193{
194	printf(" on %s%d", device_get_name(bus), device_get_unit(bus));
195}
196
197DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
198
199#endif /* NPCI > 0 */
200