xref: /freebsd/sys/dev/ida/ida_pci.c (revision fc601c53dcb1d99c5f63d111baadbce118ffa9c1)
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  */
26db57feb7SJonathan Lemon 
27aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
28aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
29aad970f1SDavid E. O'Brien 
30db57feb7SJonathan Lemon #include <sys/param.h>
31db57feb7SJonathan Lemon #include <sys/systm.h>
32db57feb7SJonathan Lemon #include <sys/kernel.h>
33fe12f24bSPoul-Henning Kamp #include <sys/module.h>
34db57feb7SJonathan Lemon 
359626b608SPoul-Henning Kamp #include <sys/bio.h>
36db57feb7SJonathan Lemon #include <sys/bus.h>
37e5dc8339SPoul-Henning Kamp #include <sys/conf.h>
38db57feb7SJonathan Lemon 
39db57feb7SJonathan Lemon #include <machine/bus_memio.h>
40db57feb7SJonathan Lemon #include <machine/bus_pio.h>
41db57feb7SJonathan Lemon #include <machine/bus.h>
42db57feb7SJonathan Lemon #include <machine/resource.h>
43db57feb7SJonathan Lemon #include <sys/rman.h>
44db57feb7SJonathan Lemon 
4577e6a3b2SWarner Losh #include <dev/pci/pcireg.h>
4677e6a3b2SWarner Losh #include <dev/pci/pcivar.h>
47db57feb7SJonathan Lemon 
48891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
49891619a6SPoul-Henning Kamp 
50db57feb7SJonathan Lemon #include <dev/ida/idavar.h>
51ee7eb00eSJonathan Lemon #include <dev/ida/idareg.h>
52db57feb7SJonathan Lemon 
53db57feb7SJonathan Lemon #define	IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
54db57feb7SJonathan Lemon #define	IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
55db57feb7SJonathan Lemon 
56e27951b2SJohn Baldwin #define	IDA_PCI_MEMADDR		PCIR_BAR(1)		/* Mem I/O Address */
57db57feb7SJonathan Lemon 
58db57feb7SJonathan Lemon #define	IDA_DEVICEID_SMART		0xAE100E11
59f3cddbbdSJonathan Lemon #define	IDA_DEVICEID_DEC_SMART		0x00461011
601ec5e8e6SJonathan Lemon #define	IDA_DEVICEID_NCR_53C1510	0x00101000
61db57feb7SJonathan Lemon 
62ee7eb00eSJonathan Lemon static int
63ee7eb00eSJonathan Lemon ida_v3_fifo_full(struct ida_softc *ida)
64ee7eb00eSJonathan Lemon {
65ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_CMD_FIFO) == 0);
66ee7eb00eSJonathan Lemon }
67db57feb7SJonathan Lemon 
68ee7eb00eSJonathan Lemon static void
69ee7eb00eSJonathan Lemon ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
70ee7eb00eSJonathan Lemon {
71ee7eb00eSJonathan Lemon 	ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
72ee7eb00eSJonathan Lemon }
73ee7eb00eSJonathan Lemon 
74ee7eb00eSJonathan Lemon static bus_addr_t
75ee7eb00eSJonathan Lemon ida_v3_done(struct ida_softc *ida)
76ee7eb00eSJonathan Lemon {
77fc601c53SMatthew N. Dodd 	bus_addr_t completed;
78fc601c53SMatthew N. Dodd 
79fc601c53SMatthew N. Dodd 	completed = ida_inl(ida, R_DONE_FIFO);
80fc601c53SMatthew N. Dodd 	if (completed == -1) {
81fc601c53SMatthew N. Dodd 		return (0);			/* fifo is empty */
82fc601c53SMatthew N. Dodd 	}
83fc601c53SMatthew N. Dodd 	return (completed);
84ee7eb00eSJonathan Lemon }
85ee7eb00eSJonathan Lemon 
86ee7eb00eSJonathan Lemon static int
87ee7eb00eSJonathan Lemon ida_v3_int_pending(struct ida_softc *ida)
88ee7eb00eSJonathan Lemon {
89ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_INT_PENDING));
90ee7eb00eSJonathan Lemon }
91ee7eb00eSJonathan Lemon 
92ee7eb00eSJonathan Lemon static void
93ee7eb00eSJonathan Lemon ida_v3_int_enable(struct ida_softc *ida, int enable)
94ee7eb00eSJonathan Lemon {
9555d782fcSJonathan Lemon 	if (enable)
9655d782fcSJonathan Lemon 		ida->flags |= IDA_INTERRUPTS;
9755d782fcSJonathan Lemon 	else
9855d782fcSJonathan Lemon 		ida->flags &= ~IDA_INTERRUPTS;
99ee7eb00eSJonathan Lemon 	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
100ee7eb00eSJonathan Lemon }
101ee7eb00eSJonathan Lemon 
102ee7eb00eSJonathan Lemon static int
103ee7eb00eSJonathan Lemon ida_v4_fifo_full(struct ida_softc *ida)
104ee7eb00eSJonathan Lemon {
105ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_42XX_REQUEST) != 0);
106ee7eb00eSJonathan Lemon }
107ee7eb00eSJonathan Lemon 
108ee7eb00eSJonathan Lemon static void
109ee7eb00eSJonathan Lemon ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
110ee7eb00eSJonathan Lemon {
111ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
112ee7eb00eSJonathan Lemon }
113ee7eb00eSJonathan Lemon 
114ee7eb00eSJonathan Lemon static bus_addr_t
115ee7eb00eSJonathan Lemon ida_v4_done(struct ida_softc *ida)
116ee7eb00eSJonathan Lemon {
117ee7eb00eSJonathan Lemon 	bus_addr_t completed;
118ee7eb00eSJonathan Lemon 
119ee7eb00eSJonathan Lemon 	completed = ida_inl(ida, R_42XX_REPLY);
120ee7eb00eSJonathan Lemon 	if (completed == -1)
121ee7eb00eSJonathan Lemon 		return (0);			/* fifo is empty */
122ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
123ee7eb00eSJonathan Lemon 	return (completed);
124ee7eb00eSJonathan Lemon }
125ee7eb00eSJonathan Lemon 
126ee7eb00eSJonathan Lemon static int
127ee7eb00eSJonathan Lemon ida_v4_int_pending(struct ida_softc *ida)
128ee7eb00eSJonathan Lemon {
129ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
130ee7eb00eSJonathan Lemon }
131ee7eb00eSJonathan Lemon 
132ee7eb00eSJonathan Lemon static void
133ee7eb00eSJonathan Lemon ida_v4_int_enable(struct ida_softc *ida, int enable)
134ee7eb00eSJonathan Lemon {
13555d782fcSJonathan Lemon 	if (enable)
13655d782fcSJonathan Lemon 		ida->flags |= IDA_INTERRUPTS;
13755d782fcSJonathan Lemon 	else
13855d782fcSJonathan Lemon 		ida->flags &= ~IDA_INTERRUPTS;
139ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_INT_MASK,
140ee7eb00eSJonathan Lemon 	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
141ee7eb00eSJonathan Lemon }
142ee7eb00eSJonathan Lemon 
143ee7eb00eSJonathan Lemon static struct ida_access ida_v3_access = {
144ee7eb00eSJonathan Lemon 	ida_v3_fifo_full,
145ee7eb00eSJonathan Lemon 	ida_v3_submit,
146ee7eb00eSJonathan Lemon 	ida_v3_done,
147ee7eb00eSJonathan Lemon 	ida_v3_int_pending,
148ee7eb00eSJonathan Lemon 	ida_v3_int_enable,
149ee7eb00eSJonathan Lemon };
150ee7eb00eSJonathan Lemon 
151ee7eb00eSJonathan Lemon static struct ida_access ida_v4_access = {
152ee7eb00eSJonathan Lemon 	ida_v4_fifo_full,
153ee7eb00eSJonathan Lemon 	ida_v4_submit,
154ee7eb00eSJonathan Lemon 	ida_v4_done,
155ee7eb00eSJonathan Lemon 	ida_v4_int_pending,
156ee7eb00eSJonathan Lemon 	ida_v4_int_enable,
157ee7eb00eSJonathan Lemon };
158ee7eb00eSJonathan Lemon 
159ee7eb00eSJonathan Lemon static struct ida_board board_id[] = {
1607c258c0eSJonathan Lemon 	{ 0x40300E11, "Compaq SMART-2/P array controller",
1617c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1627c258c0eSJonathan Lemon 	{ 0x40310E11, "Compaq SMART-2SL array controller",
1637c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1647c258c0eSJonathan Lemon 	{ 0x40320E11, "Compaq Smart Array 3200 controller",
1657c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1667c258c0eSJonathan Lemon 	{ 0x40330E11, "Compaq Smart Array 3100ES controller",
1677c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1687c258c0eSJonathan Lemon 	{ 0x40340E11, "Compaq Smart Array 221 controller",
1697c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
170ee7eb00eSJonathan Lemon 
1717c258c0eSJonathan Lemon 	{ 0x40400E11, "Compaq Integrated Array controller",
1727c258c0eSJonathan Lemon 	    &ida_v4_access, IDA_FIRMWARE },
1737c258c0eSJonathan Lemon 	{ 0x40480E11, "Compaq RAID LC2 controller",
1747c258c0eSJonathan Lemon 	    &ida_v4_access, IDA_FIRMWARE },
1757c258c0eSJonathan Lemon 	{ 0x40500E11, "Compaq Smart Array 4200 controller",
1767c258c0eSJonathan Lemon 	    &ida_v4_access, 0 },
1777c258c0eSJonathan Lemon 	{ 0x40510E11, "Compaq Smart Array 4250ES controller",
1787c258c0eSJonathan Lemon 	    &ida_v4_access, 0 },
1797c258c0eSJonathan Lemon 	{ 0x40580E11, "Compaq Smart Array 431 controller",
1807c258c0eSJonathan Lemon 	    &ida_v4_access, 0 },
181f3cddbbdSJonathan Lemon 
1827c258c0eSJonathan Lemon 	{ 0, "", 0, 0 },
183db57feb7SJonathan Lemon };
184db57feb7SJonathan Lemon 
185db57feb7SJonathan Lemon static int ida_pci_probe(device_t dev);
186db57feb7SJonathan Lemon static int ida_pci_attach(device_t dev);
187db57feb7SJonathan Lemon 
188db57feb7SJonathan Lemon static device_method_t ida_pci_methods[] = {
189db57feb7SJonathan Lemon 	DEVMETHOD(device_probe,		ida_pci_probe),
190db57feb7SJonathan Lemon 	DEVMETHOD(device_attach,	ida_pci_attach),
191ee7eb00eSJonathan Lemon 	DEVMETHOD(device_detach,	ida_detach),
192db57feb7SJonathan Lemon 
19315317dd8SMatthew N. Dodd 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
194db57feb7SJonathan Lemon 
195db57feb7SJonathan Lemon 	{ 0, 0 }
196db57feb7SJonathan Lemon };
197db57feb7SJonathan Lemon 
198db57feb7SJonathan Lemon static driver_t ida_pci_driver = {
199db57feb7SJonathan Lemon 	"ida",
200db57feb7SJonathan Lemon 	ida_pci_methods,
201db57feb7SJonathan Lemon 	sizeof(struct ida_softc)
202db57feb7SJonathan Lemon };
203db57feb7SJonathan Lemon 
204db57feb7SJonathan Lemon static devclass_t ida_devclass;
205db57feb7SJonathan Lemon 
206ee7eb00eSJonathan Lemon static struct ida_board *
207834801e8SJonathan Lemon ida_pci_match(device_t dev)
208ee7eb00eSJonathan Lemon {
209ee7eb00eSJonathan Lemon 	int i;
210834801e8SJonathan Lemon 	u_int32_t id, sub_id;
211ee7eb00eSJonathan Lemon 
212834801e8SJonathan Lemon 	id = pci_get_devid(dev);
213834801e8SJonathan Lemon 	sub_id = pci_get_subdevice(dev) << 16 | pci_get_subvendor(dev);
214834801e8SJonathan Lemon 
215834801e8SJonathan Lemon 	if (id == IDA_DEVICEID_SMART ||
216834801e8SJonathan Lemon 	    id == IDA_DEVICEID_DEC_SMART ||
217834801e8SJonathan Lemon 	    id == IDA_DEVICEID_NCR_53C1510) {
218ee7eb00eSJonathan Lemon 		for (i = 0; board_id[i].board; i++)
219834801e8SJonathan Lemon 			if (board_id[i].board == sub_id)
220ee7eb00eSJonathan Lemon 				return (&board_id[i]);
221834801e8SJonathan Lemon 	}
222ee7eb00eSJonathan Lemon 	return (NULL);
223ee7eb00eSJonathan Lemon }
224ee7eb00eSJonathan Lemon 
225db57feb7SJonathan Lemon static int
226db57feb7SJonathan Lemon ida_pci_probe(device_t dev)
227db57feb7SJonathan Lemon {
228834801e8SJonathan Lemon 	struct ida_board *board = ida_pci_match(dev);
229db57feb7SJonathan Lemon 
230ee7eb00eSJonathan Lemon 	if (board != NULL) {
231ee7eb00eSJonathan Lemon 		device_set_desc(dev, board->desc);
232db57feb7SJonathan Lemon 		return (0);
233db57feb7SJonathan Lemon 	}
234db57feb7SJonathan Lemon 	return (ENXIO);
235db57feb7SJonathan Lemon }
236db57feb7SJonathan Lemon 
237db57feb7SJonathan Lemon static int
238db57feb7SJonathan Lemon ida_pci_attach(device_t dev)
239db57feb7SJonathan Lemon {
240834801e8SJonathan Lemon 	struct ida_board *board = ida_pci_match(dev);
241280a27d2SJonathan Lemon 	u_int32_t id = pci_get_devid(dev);
242db57feb7SJonathan Lemon 	struct ida_softc *ida;
243db57feb7SJonathan Lemon 	u_int command;
244db57feb7SJonathan Lemon 	int error, rid;
245db57feb7SJonathan Lemon 
246db57feb7SJonathan Lemon 	command = pci_read_config(dev, PCIR_COMMAND, 1);
247db57feb7SJonathan Lemon 
248db57feb7SJonathan Lemon 	/*
249db57feb7SJonathan Lemon 	 * it appears that this board only does MEMIO access.
250db57feb7SJonathan Lemon 	 */
251db57feb7SJonathan Lemon 	if ((command & PCIM_CMD_MEMEN) == 0) {
252db57feb7SJonathan Lemon                 device_printf(dev, "Only memory mapped I/O is supported\n");
253db57feb7SJonathan Lemon 		return (ENXIO);
254db57feb7SJonathan Lemon 	}
255db57feb7SJonathan Lemon 
256db57feb7SJonathan Lemon 	ida = (struct ida_softc *)device_get_softc(dev);
257db57feb7SJonathan Lemon 	ida->dev = dev;
258ee7eb00eSJonathan Lemon 	ida->cmd = *board->accessor;
2597c258c0eSJonathan Lemon 	ida->flags = board->flags;
260ee7eb00eSJonathan Lemon 
261db57feb7SJonathan Lemon 	ida->regs_res_type = SYS_RES_MEMORY;
262db57feb7SJonathan Lemon 	ida->regs_res_id = IDA_PCI_MEMADDR;
263280a27d2SJonathan Lemon 	if (id == IDA_DEVICEID_DEC_SMART)
264e27951b2SJohn Baldwin 		ida->regs_res_id = PCIR_BAR(0);
2651277c3baSJonathan Lemon 
2665f96beb9SNate Lawson 	ida->regs = bus_alloc_resource_any(dev, ida->regs_res_type,
2675f96beb9SNate Lawson 	    &ida->regs_res_id, RF_ACTIVE);
268db57feb7SJonathan Lemon 	if (ida->regs == NULL) {
2691277c3baSJonathan Lemon 		device_printf(dev, "can't allocate memory resources\n");
270db57feb7SJonathan Lemon 		return (ENOMEM);
271db57feb7SJonathan Lemon 	}
272db57feb7SJonathan Lemon 
273b75ab906SMatthew N. Dodd 	error = bus_dma_tag_create(
274b75ab906SMatthew N. Dodd 		/* parent	*/ NULL,
275b75ab906SMatthew N. Dodd 		/* alignment	*/ 1,
276b75ab906SMatthew N. Dodd 		/* boundary	*/ 0,
277b75ab906SMatthew N. Dodd 		/* lowaddr	*/ BUS_SPACE_MAXADDR_32BIT,
278b75ab906SMatthew N. Dodd 		/* highaddr	*/ BUS_SPACE_MAXADDR,
279b75ab906SMatthew N. Dodd 		/* filter	*/ NULL,
280b75ab906SMatthew N. Dodd 		/* filterarg	*/ NULL,
281b75ab906SMatthew N. Dodd 		/* maxsize	*/ MAXBSIZE,
282b75ab906SMatthew N. Dodd 		/* nsegments	*/ IDA_NSEG,
283b75ab906SMatthew N. Dodd 		/* maxsegsize	*/ BUS_SPACE_MAXSIZE_32BIT,
284b75ab906SMatthew N. Dodd 		/* flags	*/ BUS_DMA_ALLOCNOW,
285b75ab906SMatthew N. Dodd 		/* lockfunc	*/ NULL,
286b75ab906SMatthew N. Dodd 		/* lockarg	*/ NULL,
287b75ab906SMatthew N. Dodd 		&ida->parent_dmat);
288db57feb7SJonathan Lemon 	if (error != 0) {
289db57feb7SJonathan Lemon 		device_printf(dev, "can't allocate DMA tag\n");
290db57feb7SJonathan Lemon 		ida_free(ida);
291db57feb7SJonathan Lemon 		return (ENOMEM);
292db57feb7SJonathan Lemon 	}
293db57feb7SJonathan Lemon 
294db57feb7SJonathan Lemon 	rid = 0;
295db57feb7SJonathan Lemon         ida->irq_res_type = SYS_RES_IRQ;
2965f96beb9SNate Lawson 	ida->irq = bus_alloc_resource_any(dev, ida->irq_res_type, &rid,
2975f96beb9SNate Lawson 	    RF_ACTIVE | RF_SHAREABLE);
298db57feb7SJonathan Lemon         if (ida->irq == NULL) {
299db57feb7SJonathan Lemon                 ida_free(ida);
300db57feb7SJonathan Lemon                 return (ENOMEM);
301db57feb7SJonathan Lemon         }
302ed34d0adSMark Murray 	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO | INTR_ENTROPY,
303db57feb7SJonathan Lemon 	    ida_intr, ida, &ida->ih);
304db57feb7SJonathan Lemon 	if (error) {
305db57feb7SJonathan Lemon 		device_printf(dev, "can't setup interrupt\n");
306db57feb7SJonathan Lemon 		ida_free(ida);
307db57feb7SJonathan Lemon 		return (ENOMEM);
308db57feb7SJonathan Lemon 	}
309db57feb7SJonathan Lemon 
310db57feb7SJonathan Lemon 	error = ida_init(ida);
311db57feb7SJonathan Lemon 	if (error) {
312db57feb7SJonathan Lemon                 ida_free(ida);
313db57feb7SJonathan Lemon                 return (error);
314db57feb7SJonathan Lemon         }
315db57feb7SJonathan Lemon 	ida_attach(ida);
3161277c3baSJonathan Lemon 	ida->flags |= IDA_ATTACHED;
317db57feb7SJonathan Lemon 
318db57feb7SJonathan Lemon 	return (0);
319db57feb7SJonathan Lemon }
320db57feb7SJonathan Lemon 
321db57feb7SJonathan Lemon DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
322