xref: /freebsd/sys/dev/ida/ida_pci.c (revision e27951b29c565206b57c8cdbc2ac344485c2f4f5)
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>
33db57feb7SJonathan Lemon 
349626b608SPoul-Henning Kamp #include <sys/bio.h>
35db57feb7SJonathan Lemon #include <sys/bus.h>
36e5dc8339SPoul-Henning Kamp #include <sys/conf.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 
4477e6a3b2SWarner Losh #include <dev/pci/pcireg.h>
4577e6a3b2SWarner Losh #include <dev/pci/pcivar.h>
46db57feb7SJonathan Lemon 
47891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
48891619a6SPoul-Henning Kamp 
49db57feb7SJonathan Lemon #include <dev/ida/idavar.h>
50ee7eb00eSJonathan Lemon #include <dev/ida/idareg.h>
51db57feb7SJonathan Lemon 
52db57feb7SJonathan Lemon #define IDA_PCI_MAX_DMA_ADDR	0xFFFFFFFF
53db57feb7SJonathan Lemon #define IDA_PCI_MAX_DMA_COUNT	0xFFFFFFFF
54db57feb7SJonathan Lemon 
55e27951b2SJohn Baldwin #define IDA_PCI_MEMADDR		PCIR_BAR(1)		/* Mem I/O Address */
56db57feb7SJonathan Lemon 
57db57feb7SJonathan Lemon #define IDA_DEVICEID_SMART		0xAE100E11
58f3cddbbdSJonathan Lemon #define IDA_DEVICEID_DEC_SMART		0x00461011
591ec5e8e6SJonathan Lemon #define IDA_DEVICEID_NCR_53C1510	0x00101000
60db57feb7SJonathan Lemon 
61ee7eb00eSJonathan Lemon static int
62ee7eb00eSJonathan Lemon ida_v3_fifo_full(struct ida_softc *ida)
63ee7eb00eSJonathan Lemon {
64ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_CMD_FIFO) == 0);
65ee7eb00eSJonathan Lemon }
66db57feb7SJonathan Lemon 
67ee7eb00eSJonathan Lemon static void
68ee7eb00eSJonathan Lemon ida_v3_submit(struct ida_softc *ida, struct ida_qcb *qcb)
69ee7eb00eSJonathan Lemon {
70ee7eb00eSJonathan Lemon 	ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
71ee7eb00eSJonathan Lemon }
72ee7eb00eSJonathan Lemon 
73ee7eb00eSJonathan Lemon static bus_addr_t
74ee7eb00eSJonathan Lemon ida_v3_done(struct ida_softc *ida)
75ee7eb00eSJonathan Lemon {
76ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_DONE_FIFO));
77ee7eb00eSJonathan Lemon }
78ee7eb00eSJonathan Lemon 
79ee7eb00eSJonathan Lemon static int
80ee7eb00eSJonathan Lemon ida_v3_int_pending(struct ida_softc *ida)
81ee7eb00eSJonathan Lemon {
82ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_INT_PENDING));
83ee7eb00eSJonathan Lemon }
84ee7eb00eSJonathan Lemon 
85ee7eb00eSJonathan Lemon static void
86ee7eb00eSJonathan Lemon ida_v3_int_enable(struct ida_softc *ida, int enable)
87ee7eb00eSJonathan Lemon {
8855d782fcSJonathan Lemon 	if (enable)
8955d782fcSJonathan Lemon 		ida->flags |= IDA_INTERRUPTS;
9055d782fcSJonathan Lemon 	else
9155d782fcSJonathan Lemon 		ida->flags &= ~IDA_INTERRUPTS;
92ee7eb00eSJonathan Lemon 	ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
93ee7eb00eSJonathan Lemon }
94ee7eb00eSJonathan Lemon 
95ee7eb00eSJonathan Lemon static int
96ee7eb00eSJonathan Lemon ida_v4_fifo_full(struct ida_softc *ida)
97ee7eb00eSJonathan Lemon {
98ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_42XX_REQUEST) != 0);
99ee7eb00eSJonathan Lemon }
100ee7eb00eSJonathan Lemon 
101ee7eb00eSJonathan Lemon static void
102ee7eb00eSJonathan Lemon ida_v4_submit(struct ida_softc *ida, struct ida_qcb *qcb)
103ee7eb00eSJonathan Lemon {
104ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_REQUEST, qcb->hwqcb_busaddr);
105ee7eb00eSJonathan Lemon }
106ee7eb00eSJonathan Lemon 
107ee7eb00eSJonathan Lemon static bus_addr_t
108ee7eb00eSJonathan Lemon ida_v4_done(struct ida_softc *ida)
109ee7eb00eSJonathan Lemon {
110ee7eb00eSJonathan Lemon 	bus_addr_t completed;
111ee7eb00eSJonathan Lemon 
112ee7eb00eSJonathan Lemon 	completed = ida_inl(ida, R_42XX_REPLY);
113ee7eb00eSJonathan Lemon 	if (completed == -1)
114ee7eb00eSJonathan Lemon 		return (0);			/* fifo is empty */
115ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_REPLY, 0);		/* confirm read */
116ee7eb00eSJonathan Lemon 	return (completed);
117ee7eb00eSJonathan Lemon }
118ee7eb00eSJonathan Lemon 
119ee7eb00eSJonathan Lemon static int
120ee7eb00eSJonathan Lemon ida_v4_int_pending(struct ida_softc *ida)
121ee7eb00eSJonathan Lemon {
122ee7eb00eSJonathan Lemon 	return (ida_inl(ida, R_42XX_STATUS) & STATUS_42XX_INT_PENDING);
123ee7eb00eSJonathan Lemon }
124ee7eb00eSJonathan Lemon 
125ee7eb00eSJonathan Lemon static void
126ee7eb00eSJonathan Lemon ida_v4_int_enable(struct ida_softc *ida, int enable)
127ee7eb00eSJonathan Lemon {
12855d782fcSJonathan Lemon 	if (enable)
12955d782fcSJonathan Lemon 		ida->flags |= IDA_INTERRUPTS;
13055d782fcSJonathan Lemon 	else
13155d782fcSJonathan Lemon 		ida->flags &= ~IDA_INTERRUPTS;
132ee7eb00eSJonathan Lemon 	ida_outl(ida, R_42XX_INT_MASK,
133ee7eb00eSJonathan Lemon 	    enable ? INT_ENABLE_42XX : INT_DISABLE_42XX);
134ee7eb00eSJonathan Lemon }
135ee7eb00eSJonathan Lemon 
136ee7eb00eSJonathan Lemon static struct ida_access ida_v3_access = {
137ee7eb00eSJonathan Lemon 	ida_v3_fifo_full,
138ee7eb00eSJonathan Lemon 	ida_v3_submit,
139ee7eb00eSJonathan Lemon 	ida_v3_done,
140ee7eb00eSJonathan Lemon 	ida_v3_int_pending,
141ee7eb00eSJonathan Lemon 	ida_v3_int_enable,
142ee7eb00eSJonathan Lemon };
143ee7eb00eSJonathan Lemon 
144ee7eb00eSJonathan Lemon static struct ida_access ida_v4_access = {
145ee7eb00eSJonathan Lemon 	ida_v4_fifo_full,
146ee7eb00eSJonathan Lemon 	ida_v4_submit,
147ee7eb00eSJonathan Lemon 	ida_v4_done,
148ee7eb00eSJonathan Lemon 	ida_v4_int_pending,
149ee7eb00eSJonathan Lemon 	ida_v4_int_enable,
150ee7eb00eSJonathan Lemon };
151ee7eb00eSJonathan Lemon 
152ee7eb00eSJonathan Lemon static struct ida_board board_id[] = {
1537c258c0eSJonathan Lemon 	{ 0x40300E11, "Compaq SMART-2/P array controller",
1547c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1557c258c0eSJonathan Lemon 	{ 0x40310E11, "Compaq SMART-2SL array controller",
1567c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1577c258c0eSJonathan Lemon 	{ 0x40320E11, "Compaq Smart Array 3200 controller",
1587c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1597c258c0eSJonathan Lemon 	{ 0x40330E11, "Compaq Smart Array 3100ES controller",
1607c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
1617c258c0eSJonathan Lemon 	{ 0x40340E11, "Compaq Smart Array 221 controller",
1627c258c0eSJonathan Lemon 	    &ida_v3_access, 0 },
163ee7eb00eSJonathan Lemon 
1647c258c0eSJonathan Lemon 	{ 0x40400E11, "Compaq Integrated Array controller",
1657c258c0eSJonathan Lemon 	    &ida_v4_access, IDA_FIRMWARE },
1667c258c0eSJonathan Lemon 	{ 0x40480E11, "Compaq RAID LC2 controller",
1677c258c0eSJonathan Lemon 	    &ida_v4_access, IDA_FIRMWARE },
1687c258c0eSJonathan Lemon 	{ 0x40500E11, "Compaq Smart Array 4200 controller",
1697c258c0eSJonathan Lemon 	    &ida_v4_access, 0 },
1707c258c0eSJonathan Lemon 	{ 0x40510E11, "Compaq Smart Array 4250ES controller",
1717c258c0eSJonathan Lemon 	    &ida_v4_access, 0 },
1727c258c0eSJonathan Lemon 	{ 0x40580E11, "Compaq Smart Array 431 controller",
1737c258c0eSJonathan Lemon 	    &ida_v4_access, 0 },
174f3cddbbdSJonathan Lemon 
1757c258c0eSJonathan Lemon 	{ 0, "", 0, 0 },
176db57feb7SJonathan Lemon };
177db57feb7SJonathan Lemon 
178db57feb7SJonathan Lemon static int ida_pci_probe(device_t dev);
179db57feb7SJonathan Lemon static int ida_pci_attach(device_t dev);
180db57feb7SJonathan Lemon 
181db57feb7SJonathan Lemon static device_method_t ida_pci_methods[] = {
182db57feb7SJonathan Lemon 	DEVMETHOD(device_probe,		ida_pci_probe),
183db57feb7SJonathan Lemon 	DEVMETHOD(device_attach,	ida_pci_attach),
184ee7eb00eSJonathan Lemon 	DEVMETHOD(device_detach,	ida_detach),
185db57feb7SJonathan Lemon 
18615317dd8SMatthew N. Dodd 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
187db57feb7SJonathan Lemon 
188db57feb7SJonathan Lemon 	{ 0, 0 }
189db57feb7SJonathan Lemon };
190db57feb7SJonathan Lemon 
191db57feb7SJonathan Lemon static driver_t ida_pci_driver = {
192db57feb7SJonathan Lemon 	"ida",
193db57feb7SJonathan Lemon 	ida_pci_methods,
194db57feb7SJonathan Lemon 	sizeof(struct ida_softc)
195db57feb7SJonathan Lemon };
196db57feb7SJonathan Lemon 
197db57feb7SJonathan Lemon static devclass_t ida_devclass;
198db57feb7SJonathan Lemon 
199ee7eb00eSJonathan Lemon static struct ida_board *
200834801e8SJonathan Lemon ida_pci_match(device_t dev)
201ee7eb00eSJonathan Lemon {
202ee7eb00eSJonathan Lemon 	int i;
203834801e8SJonathan Lemon 	u_int32_t id, sub_id;
204ee7eb00eSJonathan Lemon 
205834801e8SJonathan Lemon 	id = pci_get_devid(dev);
206834801e8SJonathan Lemon 	sub_id = pci_get_subdevice(dev) << 16 | pci_get_subvendor(dev);
207834801e8SJonathan Lemon 
208834801e8SJonathan Lemon 	if (id == IDA_DEVICEID_SMART ||
209834801e8SJonathan Lemon 	    id == IDA_DEVICEID_DEC_SMART ||
210834801e8SJonathan Lemon 	    id == IDA_DEVICEID_NCR_53C1510) {
211ee7eb00eSJonathan Lemon 		for (i = 0; board_id[i].board; i++)
212834801e8SJonathan Lemon 			if (board_id[i].board == sub_id)
213ee7eb00eSJonathan Lemon 				return (&board_id[i]);
214834801e8SJonathan Lemon 	}
215ee7eb00eSJonathan Lemon 	return (NULL);
216ee7eb00eSJonathan Lemon }
217ee7eb00eSJonathan Lemon 
218db57feb7SJonathan Lemon static int
219db57feb7SJonathan Lemon ida_pci_probe(device_t dev)
220db57feb7SJonathan Lemon {
221834801e8SJonathan Lemon 	struct ida_board *board = ida_pci_match(dev);
222db57feb7SJonathan Lemon 
223ee7eb00eSJonathan Lemon 	if (board != NULL) {
224ee7eb00eSJonathan Lemon 		device_set_desc(dev, board->desc);
225db57feb7SJonathan Lemon 		return (0);
226db57feb7SJonathan Lemon 	}
227db57feb7SJonathan Lemon 	return (ENXIO);
228db57feb7SJonathan Lemon }
229db57feb7SJonathan Lemon 
230db57feb7SJonathan Lemon static int
231db57feb7SJonathan Lemon ida_pci_attach(device_t dev)
232db57feb7SJonathan Lemon {
233834801e8SJonathan Lemon 	struct ida_board *board = ida_pci_match(dev);
234280a27d2SJonathan Lemon 	u_int32_t id = pci_get_devid(dev);
235db57feb7SJonathan Lemon 	struct ida_softc *ida;
236db57feb7SJonathan Lemon 	u_int command;
237db57feb7SJonathan Lemon 	int error, rid;
238db57feb7SJonathan Lemon 
239db57feb7SJonathan Lemon 	command = pci_read_config(dev, PCIR_COMMAND, 1);
240db57feb7SJonathan Lemon 
241db57feb7SJonathan Lemon 	/*
242db57feb7SJonathan Lemon 	 * it appears that this board only does MEMIO access.
243db57feb7SJonathan Lemon 	 */
244db57feb7SJonathan Lemon 	if ((command & PCIM_CMD_MEMEN) == 0) {
245db57feb7SJonathan Lemon                 device_printf(dev, "Only memory mapped I/O is supported\n");
246db57feb7SJonathan Lemon 		return (ENXIO);
247db57feb7SJonathan Lemon 	}
248db57feb7SJonathan Lemon 
249db57feb7SJonathan Lemon 	ida = (struct ida_softc *)device_get_softc(dev);
250db57feb7SJonathan Lemon 	ida->dev = dev;
251ee7eb00eSJonathan Lemon 	ida->cmd = *board->accessor;
2527c258c0eSJonathan Lemon 	ida->flags = board->flags;
253ee7eb00eSJonathan Lemon 
254db57feb7SJonathan Lemon 	ida->regs_res_type = SYS_RES_MEMORY;
255db57feb7SJonathan Lemon 	ida->regs_res_id = IDA_PCI_MEMADDR;
256280a27d2SJonathan Lemon 	if (id == IDA_DEVICEID_DEC_SMART)
257e27951b2SJohn Baldwin 		ida->regs_res_id = PCIR_BAR(0);
2581277c3baSJonathan Lemon 
259db57feb7SJonathan Lemon 	ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
260db57feb7SJonathan Lemon 	    &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
261db57feb7SJonathan Lemon 	if (ida->regs == NULL) {
2621277c3baSJonathan Lemon 		device_printf(dev, "can't allocate memory resources\n");
263db57feb7SJonathan Lemon 		return (ENOMEM);
264db57feb7SJonathan Lemon 	}
265db57feb7SJonathan Lemon 
266086646f7SJustin T. Gibbs 	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
267db57feb7SJonathan Lemon 	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
268db57feb7SJonathan Lemon 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
269db57feb7SJonathan Lemon 	    /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
270db57feb7SJonathan Lemon 	    /*maxsegsize*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/BUS_DMA_ALLOCNOW,
271f6b1c44dSScott Long 	    /*lockfunc*/NULL, /*lockarg*/NULL, &ida->parent_dmat);
272db57feb7SJonathan Lemon 	if (error != 0) {
273db57feb7SJonathan Lemon 		device_printf(dev, "can't allocate DMA tag\n");
274db57feb7SJonathan Lemon 		ida_free(ida);
275db57feb7SJonathan Lemon 		return (ENOMEM);
276db57feb7SJonathan Lemon 	}
277db57feb7SJonathan Lemon 
278db57feb7SJonathan Lemon 	rid = 0;
279db57feb7SJonathan Lemon         ida->irq_res_type = SYS_RES_IRQ;
280db57feb7SJonathan Lemon 	ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
281db57feb7SJonathan Lemon 	    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
282db57feb7SJonathan Lemon         if (ida->irq == NULL) {
283db57feb7SJonathan Lemon                 ida_free(ida);
284db57feb7SJonathan Lemon                 return (ENOMEM);
285db57feb7SJonathan Lemon         }
286ed34d0adSMark Murray 	error = bus_setup_intr(dev, ida->irq, INTR_TYPE_BIO | INTR_ENTROPY,
287db57feb7SJonathan Lemon 	    ida_intr, ida, &ida->ih);
288db57feb7SJonathan Lemon 	if (error) {
289db57feb7SJonathan Lemon 		device_printf(dev, "can't setup interrupt\n");
290db57feb7SJonathan Lemon 		ida_free(ida);
291db57feb7SJonathan Lemon 		return (ENOMEM);
292db57feb7SJonathan Lemon 	}
293db57feb7SJonathan Lemon 
294db57feb7SJonathan Lemon 	error = ida_init(ida);
295db57feb7SJonathan Lemon 	if (error) {
296db57feb7SJonathan Lemon                 ida_free(ida);
297db57feb7SJonathan Lemon                 return (error);
298db57feb7SJonathan Lemon         }
299db57feb7SJonathan Lemon 	ida_attach(ida);
3001277c3baSJonathan Lemon 	ida->flags |= IDA_ATTACHED;
301db57feb7SJonathan Lemon 
302db57feb7SJonathan Lemon 	return (0);
303db57feb7SJonathan Lemon }
304db57feb7SJonathan Lemon 
305db57feb7SJonathan Lemon DRIVER_MODULE(ida, pci, ida_pci_driver, ida_devclass, 0, 0);
306