xref: /freebsd/sys/dev/ida/ida_pci.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*-
2  * Copyright (c) 1999,2000 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$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 
33 #include <sys/buf.h>
34 #include <sys/bus.h>
35 #include <sys/devicestat.h>
36 #include <sys/disk.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 #include <dev/ida/idareg.h>
49 
50 #define IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
51 #define IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
52 
53 #define IDA_PCI_MEMADDR		(PCIR_MAPS + 4)		/* Mem I/O Address */
54 
55 #define IDA_DEVICEID_SMART	0xAE100E11
56 
57 static int
58 ida_v3_fifo_full(struct ida_softc *ida)
59 {
60 	return (ida_inl(ida, R_CMD_FIFO) == 0);
61 }
62 
63 static void
64 ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
65 {
66 	ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
67 }
68 
69 static bus_addr_t
70 ida_v3_done(struct ida_softc *ida)
71 {
72 	return (ida_inl(ida, R_DONE_FIFO));
73 }
74 
75 static int
76 ida_v3_int_pending(struct ida_softc *ida)
77 {
78 	return (ida_inl(ida, R_INT_PENDING));
79 }
80 
81 static void
82 ida_v3_int_enable(struct ida_softc *ida, int enable)
83 {
84 	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
85 }
86 
87 static int
88 ida_v4_fifo_full(struct ida_softc *ida)
89 {
90 	return (ida_inl(ida, R_42XX_REQUEST) != 0);
91 }
92 
93 static void
94 ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
95 {
96 	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
97 }
98 
99 static bus_addr_t
100 ida_v4_done(struct ida_softc *ida)
101 {
102 	bus_addr_t completed;
103 
104 	completed = ida_inl(ida, R_42XX_REPLY);
105 	if (completed == -1)
106 		return (0);			/* fifo is empty */
107 	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
108 	return (completed);
109 }
110 
111 static int
112 ida_v4_int_pending(struct ida_softc *ida)
113 {
114 	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
115 }
116 
117 static void
118 ida_v4_int_enable(struct ida_softc *ida, int enable)
119 {
120 	ida_outl(ida, R_42XX_INT_MASK,
121 	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
122 }
123 
124 static struct ida_access ida_v3_access = {
125 	ida_v3_fifo_full,
126 	ida_v3_submit,
127 	ida_v3_done,
128 	ida_v3_int_pending,
129 	ida_v3_int_enable,
130 };
131 
132 static struct ida_access ida_v4_access = {
133 	ida_v4_fifo_full,
134 	ida_v4_submit,
135 	ida_v4_done,
136 	ida_v4_int_pending,
137 	ida_v4_int_enable,
138 };
139 
140 static struct ida_board board_id[] = {
141 	{ 0x4030, "Compaq SMART-2/P array controller",	    &ida_v3_access },
142 	{ 0x4031, "Compaq SMART-2SL array controller", 	    &ida_v3_access },
143 	{ 0x4032, "Compaq Smart Array 3200 controller",	    &ida_v3_access },
144 	{ 0x4033, "Compaq Smart Array 3100ES controller",   &ida_v3_access },
145 	{ 0x4034, "Compaq Smart Array 221 controller",	    &ida_v3_access },
146 
147 	{ 0x4040, "Compaq Integrated Array controller",	    &ida_v4_access },
148 	{ 0x4050, "Compaq Smart Array 4200 controller",	    &ida_v4_access },
149 	{ 0x4051, "Compaq Smart Array 4250ES controller",   &ida_v4_access },
150 
151 	{ 0, "", 0 },
152 };
153 
154 static int ida_pci_probe(device_t dev);
155 static int ida_pci_attach(device_t dev);
156 
157 static device_method_t ida_pci_methods[] = {
158 	DEVMETHOD(device_probe,		ida_pci_probe),
159 	DEVMETHOD(device_attach,	ida_pci_attach),
160 	DEVMETHOD(device_detach,	ida_detach),
161 
162 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
163 
164 	{ 0, 0 }
165 };
166 
167 static driver_t ida_pci_driver = {
168 	"ida",
169 	ida_pci_methods,
170 	sizeof(struct ida_softc)
171 };
172 
173 static devclass_t ida_devclass;
174 
175 static struct ida_board *
176 ida_pci_match(u_int32_t id)
177 {
178 	int i;
179 
180 	for (i = 0; board_id[i].board; i++)
181 		if (board_id[i].board == id)
182 			return (&board_id[i]);
183 	return (NULL);
184 }
185 
186 static int
187 ida_pci_probe(device_t dev)
188 {
189 	struct ida_board *board;
190 
191 	if (pci_get_devid(dev) == IDA_DEVICEID_SMART) {
192 		board = ida_pci_match(pci_get_subdevice(dev));
193 		if (board != NULL) {
194 			device_set_desc(dev, board->desc);
195 			return (0);
196 		}
197 	}
198 	return (ENXIO);
199 }
200 
201 static int
202 ida_pci_attach(device_t dev)
203 {
204 	struct ida_board *board;
205 	struct ida_softc *ida;
206 	u_int command;
207 	int error, rid;
208 
209 	command = pci_read_config(dev, PCIR_COMMAND, 1);
210 
211 	/*
212 	 * it appears that this board only does MEMIO access.
213 	 */
214 	if ((command & PCIM_CMD_MEMEN) == 0) {
215                 device_printf(dev, "Only memory mapped I/O is supported\n");
216 		return (ENXIO);
217 	}
218 
219 	ida = (struct ida_softc *)device_get_softc(dev);
220 	ida->dev = dev;
221 
222 	board = ida_pci_match(pci_get_subdevice(dev));
223 	ida->cmd = *board->accessor;
224 
225 	ida->regs_res_type = SYS_RES_MEMORY;
226 	ida->regs_res_id = IDA_PCI_MEMADDR;
227 	ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
228 	    &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
229 	if (ida->regs == NULL) {
230 		device_printf(dev, "can't allocate register resources\n");
231 		return (ENOMEM);
232 	}
233 
234 	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
235 	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
236 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
237 	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
238 	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
239 	    &ida->parent_dmat);
240 	if (error != 0) {
241 		device_printf(dev, "can't allocate DMA tag\n");
242 		ida_free(ida);
243 		return (ENOMEM);
244 	}
245 
246 	rid = 0;
247         ida->irq_res_type = SYS_RES_IRQ;
248 	ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
249 	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
250         if (ida->irq == NULL) {
251                 ida_free(ida);
252                 return (ENOMEM);
253         }
254 	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO,
255 	    ida_intr, ida, &ida->ih);
256 	if (error) {
257 		device_printf(dev, "can't setup interrupt\n");
258 		ida_free(ida);
259 		return (ENOMEM);
260 	}
261 
262 	error = ida_init(ida);
263 	if (error) {
264                 ida_free(ida);
265                 return (error);
266         }
267 	ida_attach(ida);
268 	ida->flags = IDA_ATTACHED;
269 
270 	return (0);
271 }
272 
273 DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
274