xref: /freebsd/sys/dev/ida/ida_pci.c (revision f3cddbbdeb15bb2ff083ea16067ecb325fba4b4d)
1db57feb7SJonathan Lemon /*-
2ee7eb00eSJonathan Lemon  * Copyright (c) 1999,2000 Jonathan Lemon
3db57feb7SJonathan Lemon  * All rights reserved.
4db57feb7SJonathan Lemon  *
5db57feb7SJonathan Lemon  * Redistribution and use in source and binary forms, with or without
6db57feb7SJonathan Lemon  * modification, are permitted provided that the following conditions
7db57feb7SJonathan Lemon  * are met:
8db57feb7SJonathan Lemon  * 1. Redistributions of source code must retain the above copyright
9db57feb7SJonathan Lemon  *    notice, this list of conditions and the following disclaimer.
10db57feb7SJonathan Lemon  * 2. Redistributions in binary form must reproduce the above copyright
11db57feb7SJonathan Lemon  *    notice, this list of conditions and the following disclaimer in the
12db57feb7SJonathan Lemon  *    documentation and/or other materials provided with the distribution.
13db57feb7SJonathan Lemon  *
14db57feb7SJonathan Lemon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15db57feb7SJonathan Lemon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16db57feb7SJonathan Lemon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17db57feb7SJonathan Lemon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18db57feb7SJonathan Lemon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19db57feb7SJonathan Lemon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20db57feb7SJonathan Lemon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21db57feb7SJonathan Lemon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22db57feb7SJonathan Lemon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23db57feb7SJonathan Lemon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24db57feb7SJonathan Lemon  * SUCH DAMAGE.
25db57feb7SJonathan Lemon  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27db57feb7SJonathan Lemon  */
28db57feb7SJonathan Lemon 
29db57feb7SJonathan Lemon #include <sys/param.h>
30db57feb7SJonathan Lemon #include <sys/systm.h>
31db57feb7SJonathan Lemon #include <sys/kernel.h>
32db57feb7SJonathan Lemon 
33db57feb7SJonathan Lemon #include <sys/buf.h>
34db57feb7SJonathan Lemon #include <sys/bus.h>
35db57feb7SJonathan Lemon #include <sys/devicestat.h>
36ee7eb00eSJonathan Lemon #include <sys/disk.h>
37db57feb7SJonathan Lemon 
38db57feb7SJonathan Lemon #include <machine/bus_memio.h>
39db57feb7SJonathan Lemon #include <machine/bus_pio.h>
40db57feb7SJonathan Lemon #include <machine/bus.h>
41db57feb7SJonathan Lemon #include <machine/resource.h>
42db57feb7SJonathan Lemon #include <sys/rman.h>
43db57feb7SJonathan Lemon 
44db57feb7SJonathan Lemon #include <pci/pcireg.h>
45db57feb7SJonathan Lemon #include <pci/pcivar.h>
46db57feb7SJonathan Lemon 
47db57feb7SJonathan Lemon #include <dev/ida/idavar.h>
48ee7eb00eSJonathan Lemon #include <dev/ida/idareg.h>
49db57feb7SJonathan Lemon 
50db57feb7SJonathan Lemon #define IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
51db57feb7SJonathan Lemon #define IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
52db57feb7SJonathan Lemon 
53db57feb7SJonathan Lemon #define IDA_PCI_MEMADDR		(PCIR_MAPS + 4)		/* Mem I/O Address */
54db57feb7SJonathan Lemon 
55db57feb7SJonathan Lemon #define IDA_DEVICEID_SMART	0xAE100E11
56f3cddbbdSJonathan Lemon #define IDA_DEVICEID_DEC_SMART	0x00461011
57db57feb7SJonathan Lemon 
58ee7eb00eSJonathan Lemon static int
59ee7eb00eSJonathan Lemon ida_v3_fifo_full(struct ida_softc *ida)
60ee7eb00eSJonathan Lemon {
61ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_CMD_FIFO) == 0);
62ee7eb00eSJonathan Lemon }
63db57feb7SJonathan Lemon 
64ee7eb00eSJonathan Lemon static void
65ee7eb00eSJonathan Lemon ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
66ee7eb00eSJonathan Lemon {
67ee7eb00eSJonathan Lemon 	ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
68ee7eb00eSJonathan Lemon }
69ee7eb00eSJonathan Lemon 
70ee7eb00eSJonathan Lemon static bus_addr_t
71ee7eb00eSJonathan Lemon ida_v3_done(struct ida_softc *ida)
72ee7eb00eSJonathan Lemon {
73ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_DONE_FIFO));
74ee7eb00eSJonathan Lemon }
75ee7eb00eSJonathan Lemon 
76ee7eb00eSJonathan Lemon static int
77ee7eb00eSJonathan Lemon ida_v3_int_pending(struct ida_softc *ida)
78ee7eb00eSJonathan Lemon {
79ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_INT_PENDING));
80ee7eb00eSJonathan Lemon }
81ee7eb00eSJonathan Lemon 
82ee7eb00eSJonathan Lemon static void
83ee7eb00eSJonathan Lemon ida_v3_int_enable(struct ida_softc *ida, int enable)
84ee7eb00eSJonathan Lemon {
85ee7eb00eSJonathan Lemon 	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
86ee7eb00eSJonathan Lemon }
87ee7eb00eSJonathan Lemon 
88ee7eb00eSJonathan Lemon static int
89ee7eb00eSJonathan Lemon ida_v4_fifo_full(struct ida_softc *ida)
90ee7eb00eSJonathan Lemon {
91ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_42XX_REQUEST) != 0);
92ee7eb00eSJonathan Lemon }
93ee7eb00eSJonathan Lemon 
94ee7eb00eSJonathan Lemon static void
95ee7eb00eSJonathan Lemon ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
96ee7eb00eSJonathan Lemon {
97ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
98ee7eb00eSJonathan Lemon }
99ee7eb00eSJonathan Lemon 
100ee7eb00eSJonathan Lemon static bus_addr_t
101ee7eb00eSJonathan Lemon ida_v4_done(struct ida_softc *ida)
102ee7eb00eSJonathan Lemon {
103ee7eb00eSJonathan Lemon 	bus_addr_t completed;
104ee7eb00eSJonathan Lemon 
105ee7eb00eSJonathan Lemon 	completed = ida_inl(ida, R_42XX_REPLY);
106ee7eb00eSJonathan Lemon 	if (completed == -1)
107ee7eb00eSJonathan Lemon 		return (0);			/* fifo is empty */
108ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
109ee7eb00eSJonathan Lemon 	return (completed);
110ee7eb00eSJonathan Lemon }
111ee7eb00eSJonathan Lemon 
112ee7eb00eSJonathan Lemon static int
113ee7eb00eSJonathan Lemon ida_v4_int_pending(struct ida_softc *ida)
114ee7eb00eSJonathan Lemon {
115ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
116ee7eb00eSJonathan Lemon }
117ee7eb00eSJonathan Lemon 
118ee7eb00eSJonathan Lemon static void
119ee7eb00eSJonathan Lemon ida_v4_int_enable(struct ida_softc *ida, int enable)
120ee7eb00eSJonathan Lemon {
121ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_INT_MASK,
122ee7eb00eSJonathan Lemon 	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
123ee7eb00eSJonathan Lemon }
124ee7eb00eSJonathan Lemon 
125ee7eb00eSJonathan Lemon static struct ida_access ida_v3_access = {
126ee7eb00eSJonathan Lemon 	ida_v3_fifo_full,
127ee7eb00eSJonathan Lemon 	ida_v3_submit,
128ee7eb00eSJonathan Lemon 	ida_v3_done,
129ee7eb00eSJonathan Lemon 	ida_v3_int_pending,
130ee7eb00eSJonathan Lemon 	ida_v3_int_enable,
131ee7eb00eSJonathan Lemon };
132ee7eb00eSJonathan Lemon 
133ee7eb00eSJonathan Lemon static struct ida_access ida_v4_access = {
134ee7eb00eSJonathan Lemon 	ida_v4_fifo_full,
135ee7eb00eSJonathan Lemon 	ida_v4_submit,
136ee7eb00eSJonathan Lemon 	ida_v4_done,
137ee7eb00eSJonathan Lemon 	ida_v4_int_pending,
138ee7eb00eSJonathan Lemon 	ida_v4_int_enable,
139ee7eb00eSJonathan Lemon };
140ee7eb00eSJonathan Lemon 
141ee7eb00eSJonathan Lemon static struct ida_board board_id[] = {
142ee7eb00eSJonathan Lemon 	{ 0x4030, "Compaq SMART-2/P array controller",	    &ida_v3_access },
143ee7eb00eSJonathan Lemon 	{ 0x4031, "Compaq SMART-2SL array controller", 	    &ida_v3_access },
144ee7eb00eSJonathan Lemon 	{ 0x4032, "Compaq Smart Array 3200 controller",	    &ida_v3_access },
145ee7eb00eSJonathan Lemon 	{ 0x4033, "Compaq Smart Array 3100ES controller",   &ida_v3_access },
146ee7eb00eSJonathan Lemon 	{ 0x4034, "Compaq Smart Array 221 controller",	    &ida_v3_access },
147ee7eb00eSJonathan Lemon 
148ee7eb00eSJonathan Lemon 	{ 0x4040, "Compaq Integrated Array controller",	    &ida_v4_access },
149ee7eb00eSJonathan Lemon 	{ 0x4050, "Compaq Smart Array 4200 controller",	    &ida_v4_access },
150ee7eb00eSJonathan Lemon 	{ 0x4051, "Compaq Smart Array 4250ES controller",   &ida_v4_access },
151ee7eb00eSJonathan Lemon 
152f3cddbbdSJonathan Lemon 	{ IDA_DEVICEID_DEC_SMART,
153f3cddbbdSJonathan Lemon 		  "DEC/Compaq Smart Array 4200 controller", &ida_v4_access },
154f3cddbbdSJonathan Lemon 
155ee7eb00eSJonathan Lemon 	{ 0, "", 0 },
156db57feb7SJonathan Lemon };
157db57feb7SJonathan Lemon 
158db57feb7SJonathan Lemon static int ida_pci_probe(device_t dev);
159db57feb7SJonathan Lemon static int ida_pci_attach(device_t dev);
160db57feb7SJonathan Lemon 
161db57feb7SJonathan Lemon static device_method_t ida_pci_methods[] = {
162db57feb7SJonathan Lemon 	DEVMETHOD(device_probe,		ida_pci_probe),
163db57feb7SJonathan Lemon 	DEVMETHOD(device_attach,	ida_pci_attach),
164ee7eb00eSJonathan Lemon 	DEVMETHOD(device_detach,	ida_detach),
165db57feb7SJonathan Lemon 
16615317dd8SMatthew N. Dodd 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
167db57feb7SJonathan Lemon 
168db57feb7SJonathan Lemon 	{ 0, 0 }
169db57feb7SJonathan Lemon };
170db57feb7SJonathan Lemon 
171db57feb7SJonathan Lemon static driver_t ida_pci_driver = {
172db57feb7SJonathan Lemon 	"ida",
173db57feb7SJonathan Lemon 	ida_pci_methods,
174db57feb7SJonathan Lemon 	sizeof(struct ida_softc)
175db57feb7SJonathan Lemon };
176db57feb7SJonathan Lemon 
177db57feb7SJonathan Lemon static devclass_t ida_devclass;
178db57feb7SJonathan Lemon 
179ee7eb00eSJonathan Lemon static struct ida_board *
180ee7eb00eSJonathan Lemon ida_pci_match(u_int32_t id)
181ee7eb00eSJonathan Lemon {
182ee7eb00eSJonathan Lemon 	int i;
183ee7eb00eSJonathan Lemon 
184ee7eb00eSJonathan Lemon 	for (i = 0; board_id[i].board; i++)
185ee7eb00eSJonathan Lemon 		if (board_id[i].board == id)
186ee7eb00eSJonathan Lemon 			return (&board_id[i]);
187ee7eb00eSJonathan Lemon 	return (NULL);
188ee7eb00eSJonathan Lemon }
189ee7eb00eSJonathan Lemon 
190db57feb7SJonathan Lemon static int
191db57feb7SJonathan Lemon ida_pci_probe(device_t dev)
192db57feb7SJonathan Lemon {
193f3cddbbdSJonathan Lemon 	struct ida_board *board = NULL;
194f3cddbbdSJonathan Lemon 	u_int32_t id = pci_get_devid(dev);
195db57feb7SJonathan Lemon 
196f3cddbbdSJonathan Lemon 	if (id == IDA_DEVICEID_SMART)
197ee7eb00eSJonathan Lemon 		board = ida_pci_match(pci_get_subdevice(dev));
198f3cddbbdSJonathan Lemon 	if (id == IDA_DEVICEID_DEC_SMART)
199f3cddbbdSJonathan Lemon 		board = ida_pci_match(id);
200ee7eb00eSJonathan Lemon 	if (board != NULL) {
201ee7eb00eSJonathan Lemon 		device_set_desc(dev, board->desc);
202db57feb7SJonathan Lemon 		return (0);
203db57feb7SJonathan Lemon 	}
204db57feb7SJonathan Lemon 	return (ENXIO);
205db57feb7SJonathan Lemon }
206db57feb7SJonathan Lemon 
207db57feb7SJonathan Lemon static int
208db57feb7SJonathan Lemon ida_pci_attach(device_t dev)
209db57feb7SJonathan Lemon {
210ee7eb00eSJonathan Lemon 	struct ida_board *board;
211db57feb7SJonathan Lemon 	struct ida_softc *ida;
212db57feb7SJonathan Lemon 	u_int command;
213db57feb7SJonathan Lemon 	int error, rid;
214db57feb7SJonathan Lemon 
215db57feb7SJonathan Lemon 	command = pci_read_config(dev, PCIR_COMMAND, 1);
216db57feb7SJonathan Lemon 
217db57feb7SJonathan Lemon 	/*
218db57feb7SJonathan Lemon 	 * it appears that this board only does MEMIO access.
219db57feb7SJonathan Lemon 	 */
220db57feb7SJonathan Lemon 	if ((command & PCIM_CMD_MEMEN) == 0) {
221db57feb7SJonathan Lemon                 device_printf(dev, "Only memory mapped I/O is supported\n");
222db57feb7SJonathan Lemon 		return (ENXIO);
223db57feb7SJonathan Lemon 	}
224db57feb7SJonathan Lemon 
225db57feb7SJonathan Lemon 	ida = (struct ida_softc *)device_get_softc(dev);
226db57feb7SJonathan Lemon 	ida->dev = dev;
227db57feb7SJonathan Lemon 
228ee7eb00eSJonathan Lemon 	board = ida_pci_match(pci_get_subdevice(dev));
229f3cddbbdSJonathan Lemon 	if (board == NULL)
230f3cddbbdSJonathan Lemon 		board = ida_pci_match(pci_get_devid(dev));
231ee7eb00eSJonathan Lemon 	ida->cmd = *board->accessor;
232ee7eb00eSJonathan Lemon 
233db57feb7SJonathan Lemon 	ida->regs_res_type = SYS_RES_MEMORY;
234db57feb7SJonathan Lemon 	ida->regs_res_id = IDA_PCI_MEMADDR;
235db57feb7SJonathan Lemon 	ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
236db57feb7SJonathan Lemon 	    &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
237db57feb7SJonathan Lemon 	if (ida->regs == NULL) {
238db57feb7SJonathan Lemon 		device_printf(dev, "can't allocate register resources\n");
239db57feb7SJonathan Lemon 		return (ENOMEM);
240db57feb7SJonathan Lemon 	}
241db57feb7SJonathan Lemon 
242086646f7SJustin T. Gibbs 	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
243db57feb7SJonathan Lemon 	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
244db57feb7SJonathan Lemon 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
245db57feb7SJonathan Lemon 	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
246db57feb7SJonathan Lemon 	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
247db57feb7SJonathan Lemon 	    &ida->parent_dmat);
248db57feb7SJonathan Lemon 	if (error != 0) {
249db57feb7SJonathan Lemon 		device_printf(dev, "can't allocate DMA tag\n");
250db57feb7SJonathan Lemon 		ida_free(ida);
251db57feb7SJonathan Lemon 		return (ENOMEM);
252db57feb7SJonathan Lemon 	}
253db57feb7SJonathan Lemon 
254db57feb7SJonathan Lemon 	rid = 0;
255db57feb7SJonathan Lemon         ida->irq_res_type = SYS_RES_IRQ;
256db57feb7SJonathan Lemon 	ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
257db57feb7SJonathan Lemon 	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
258db57feb7SJonathan Lemon         if (ida->irq == NULL) {
259db57feb7SJonathan Lemon                 ida_free(ida);
260db57feb7SJonathan Lemon                 return (ENOMEM);
261db57feb7SJonathan Lemon         }
262db57feb7SJonathan Lemon 	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO,
263db57feb7SJonathan Lemon 	    ida_intr, ida, &ida->ih);
264db57feb7SJonathan Lemon 	if (error) {
265db57feb7SJonathan Lemon 		device_printf(dev, "can't setup interrupt\n");
266db57feb7SJonathan Lemon 		ida_free(ida);
267db57feb7SJonathan Lemon 		return (ENOMEM);
268db57feb7SJonathan Lemon 	}
269db57feb7SJonathan Lemon 
270db57feb7SJonathan Lemon 	error = ida_init(ida);
271db57feb7SJonathan Lemon 	if (error) {
272db57feb7SJonathan Lemon                 ida_free(ida);
273db57feb7SJonathan Lemon                 return (error);
274db57feb7SJonathan Lemon         }
275db57feb7SJonathan Lemon 	ida_attach(ida);
276db57feb7SJonathan Lemon 	ida->flags = IDA_ATTACHED;
277db57feb7SJonathan Lemon 
278db57feb7SJonathan Lemon 	return (0);
279db57feb7SJonathan Lemon }
280db57feb7SJonathan Lemon 
281db57feb7SJonathan Lemon DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
282