ida_pci.c revision 51957
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 * $FreeBSD: head/sys/dev/ida/ida_pci.c 51957 1999-10-05 21:19:41Z n_hibma $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/malloc.h>
32#include <sys/kernel.h>
33
34#include <sys/buf.h>
35#include <sys/bus.h>
36#include <sys/devicestat.h>
37
38#include <machine/bus_memio.h>
39#include <machine/bus_pio.h>
40#include <machine/bus.h>
41#include <machine/resource.h>
42#include <sys/rman.h>
43
44#include <pci/pcireg.h>
45#include <pci/pcivar.h>
46
47#include <dev/ida/idavar.h>
48
49#define IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
50#define IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
51
52#define IDA_PCI_MEMADDR		(PCIR_MAPS + 4)		/* Mem I/O Address */
53
54#define IDA_DEVICEID_SMART	0xAE100E11
55
56static struct {
57        u_long	board;
58        char    *desc;
59} board_id[] = {
60	{ 0x4030,	"Compaq SMART-2/P array controller" },
61	{ 0x4031,	"Compaq SMART-2SL array controller" },
62	{ 0x4032,	"Compaq Smart Array 3200 controller" },
63	{ 0x4033,	"Compaq Smart Array 3100ES controller" },
64	{ 0x4034,	"Compaq Smart Array 221 controller" },
65
66	{ 0,		"" },
67};
68
69static int ida_pci_probe(device_t dev);
70static int ida_pci_attach(device_t dev);
71
72static device_method_t ida_pci_methods[] = {
73	DEVMETHOD(device_probe,		ida_pci_probe),
74	DEVMETHOD(device_attach,	ida_pci_attach),
75
76	DEVMETHOD(bus_print_child,	bus_generic_print_child),
77
78	{ 0, 0 }
79};
80
81static driver_t ida_pci_driver = {
82	"ida",
83	ida_pci_methods,
84	sizeof(struct ida_softc)
85};
86
87static devclass_t ida_devclass;
88
89static int
90ida_pci_probe(device_t dev)
91{
92	u_long board;
93	int i;
94
95	if (pci_get_devid(dev) == IDA_DEVICEID_SMART) {
96		board = pci_get_subdevice(dev);
97		for (i = 0; board_id[i].board; i++) {
98			if (board_id[i].board == board) {
99				device_set_desc(dev, board_id[i].desc);
100				return (0);
101			}
102		}
103		/*
104		 * It's an unknown Compaq SMART device, but assume we
105		 * can support it.
106		 */
107		device_set_desc(dev, "Unknown Compaq Smart Array controller");
108		return (0);
109	}
110	return (ENXIO);
111}
112
113static int
114ida_pci_attach(device_t dev)
115{
116	struct ida_softc *ida;
117	u_int command;
118	int error, rid;
119
120	command = pci_read_config(dev, PCIR_COMMAND, 1);
121
122	/*
123	 * for multiple card types, need to re-determine which type is
124	 * being attached here
125	 */
126
127	/*
128	 * it appears that this board only does MEMIO access.
129	 */
130	if ((command & PCIM_CMD_MEMEN) == 0) {
131                device_printf(dev, "Only memory mapped I/O is supported\n");
132		return (ENXIO);
133	}
134
135	ida = (struct ida_softc *)device_get_softc(dev);
136	ida->dev = dev;
137
138	ida->regs_res_type = SYS_RES_MEMORY;
139	ida->regs_res_id = IDA_PCI_MEMADDR;
140	ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
141	    &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
142	if (ida->regs == NULL) {
143		device_printf(dev, "can't allocate register resources\n");
144		return (ENOMEM);
145	}
146
147	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
148	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
149	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
150	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
151	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
152	    &ida->parent_dmat);
153	if (error != 0) {
154		device_printf(dev, "can't allocate DMA tag\n");
155		ida_free(ida);
156		return (ENOMEM);
157	}
158
159	rid = 0;
160        ida->irq_res_type = SYS_RES_IRQ;
161	ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
162	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
163        if (ida->irq == NULL) {
164                ida_free(ida);
165                return (ENOMEM);
166        }
167	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO,
168	    ida_intr, ida, &ida->ih);
169	if (error) {
170		device_printf(dev, "can't setup interrupt\n");
171		ida_free(ida);
172		return (ENOMEM);
173	}
174
175	error = ida_init(ida);
176	if (error) {
177                ida_free(ida);
178                return (error);
179        }
180	ida_attach(ida);
181	ida->flags = IDA_ATTACHED;
182
183	return (0);
184}
185
186DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
187