Remove redundant settings. These are the same in /etc/defaults/rc.conf.
[dragonfly.git] / sys / dev / raid / ida / ida_eisa.c
1 /*
2  * Copyright (c) 2000 Jonathan Lemon
3  * Copyright (c) 1999 by Matthew N. Dodd <winter@jurai.net>
4  * All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/ida/ida_eisa.c,v 1.1.2.4 2001/07/30 20:29:58 jlemon Exp $
28  * $DragonFly: src/sys/dev/raid/ida/ida_eisa.c,v 1.6 2006/10/25 20:56:01 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/buf.h>
36 #include <sys/devicestat.h>
37 #include <sys/disk.h>
38 #include <sys/rman.h>
39
40 #include "idavar.h"
41 #include "idareg.h"
42
43 #include <bus/eisa/eisaconf.h>
44
45 #define IDA_EISA_IOPORT_START   0x0c88
46 #define IDA_EISA_IOPORT_LEN     0x0017
47
48 #define IDA_EISA_IRQ_REG        0x0cc0
49 #define IDA_EISA_IRQ_MASK       0xf0
50 #define IDA_EISA_IRQ_15         0x80
51 #define IDA_EISA_IRQ_14         0x40
52 #define IDA_EISA_IRQ_11         0x10
53 #define IDA_EISA_IRQ_10         0x20
54
55 static int
56 ida_v1_fifo_full(struct ida_softc *ida)
57 {
58         u_int8_t status;
59
60         status = ida_inb(ida, R_EISA_SYSTEM_DOORBELL);
61         return ((status & EISA_CHANNEL_CLEAR) == 0);
62 }
63
64 static void
65 ida_v1_submit(struct ida_softc *ida, struct ida_qcb *qcb)
66 {
67         u_int16_t size;
68
69         /*
70          * On these cards, this location is actually for control flags.
71          * Set them to zero and pass in structure size via an I/O port.
72          */
73         size = qcb->hwqcb->hdr.size << 2;
74         qcb->hwqcb->hdr.size = 0;
75
76         ida_outb(ida, R_EISA_SYSTEM_DOORBELL, EISA_CHANNEL_CLEAR);
77         ida_outl(ida, R_EISA_LIST_ADDR, qcb->hwqcb_busaddr);
78         ida_outw(ida, R_EISA_LIST_LEN, size);
79         ida_outb(ida, R_EISA_LOCAL_DOORBELL, EISA_CHANNEL_BUSY);
80 }
81
82 static bus_addr_t
83 ida_v1_done(struct ida_softc *ida)
84 {
85         struct ida_hardware_qcb *hwqcb;
86         bus_addr_t completed;
87         u_int8_t status;
88
89         if ((ida_inb(ida, R_EISA_SYSTEM_DOORBELL) & EISA_CHANNEL_BUSY) == 0)
90                 return (0);
91
92         ida_outb(ida, R_EISA_SYSTEM_DOORBELL, EISA_CHANNEL_BUSY);
93         completed = ida_inl(ida, R_EISA_COMPLETE_ADDR);
94         status = ida_inb(ida, R_EISA_LIST_STATUS);
95         ida_outb(ida, R_EISA_LOCAL_DOORBELL, EISA_CHANNEL_CLEAR);
96
97         if (completed != 0) {
98                 hwqcb = (struct ida_hardware_qcb *)
99                     ((bus_addr_t)ida->hwqcbs +
100                     ((completed & ~3) - ida->hwqcb_busaddr));
101                 hwqcb->req.error = status;
102         }
103
104         return (completed);
105 }
106
107 static int
108 ida_v1_int_pending(struct ida_softc *ida)
109 {
110         return (ida_inb(ida, R_EISA_SYSTEM_DOORBELL) & EISA_CHANNEL_BUSY);
111 }
112
113 static void
114 ida_v1_int_enable(struct ida_softc *ida, int enable)
115 {
116         if (enable) {
117                 ida_outb(ida, R_EISA_SYSTEM_DOORBELL, ~EISA_CHANNEL_CLEAR);
118                 ida_outb(ida, R_EISA_LOCAL_DOORBELL, EISA_CHANNEL_BUSY);
119                 ida_outb(ida, R_EISA_INT_MASK, INT_ENABLE);
120                 ida_outb(ida, R_EISA_SYSTEM_MASK, INT_ENABLE);
121                 ida->flags |= IDA_INTERRUPTS;
122         } else {
123                 ida_outb(ida, R_EISA_SYSTEM_MASK, INT_DISABLE);
124                 ida->flags &= ~IDA_INTERRUPTS;
125         }
126 }
127
128 static int
129 ida_v2_fifo_full(struct ida_softc *ida)
130 {
131         return (ida_inl(ida, R_CMD_FIFO) == 0);
132 }
133
134 static void
135 ida_v2_submit(struct ida_softc *ida, struct ida_qcb *qcb)
136 {
137         ida_outl(ida, R_CMD_FIFO, qcb->hwqcb_busaddr);
138 }
139
140 static bus_addr_t
141 ida_v2_done(struct ida_softc *ida)
142 {
143         return (ida_inl(ida, R_DONE_FIFO));
144 }
145
146 static int
147 ida_v2_int_pending(struct ida_softc *ida)
148 {
149         return (ida_inl(ida, R_INT_PENDING));
150 }
151
152 static void
153 ida_v2_int_enable(struct ida_softc *ida, int enable)
154 {
155         if (enable)
156                 ida->flags |= IDA_INTERRUPTS;
157         else
158                 ida->flags &= ~IDA_INTERRUPTS;
159         ida_outl(ida, R_INT_MASK, enable ? INT_ENABLE : INT_DISABLE);
160 }
161
162 static struct ida_access ida_v1_access = {
163         ida_v1_fifo_full,
164         ida_v1_submit,
165         ida_v1_done,
166         ida_v1_int_pending,
167         ida_v1_int_enable,
168 };
169
170 static struct ida_access ida_v2_access = {
171         ida_v2_fifo_full,
172         ida_v2_submit,
173         ida_v2_done,
174         ida_v2_int_pending,
175         ida_v2_int_enable,
176 };
177
178 static struct ida_board board_id[] = {
179         { 0x0e114001, "Compaq IDA controller",
180             &ida_v1_access, 0 },
181         { 0x0e114002, "Compaq IDA-2 controller",
182             &ida_v1_access, 0 },        
183         { 0x0e114010, "Compaq IAES controller",
184             &ida_v1_access, 0 },
185         { 0x0e114020, "Compaq SMART array controller",
186             &ida_v1_access, 0 },
187         { 0x0e114030, "Compaq SMART-2/E array controller",
188             &ida_v2_access, 0 },
189
190         { 0, "", 0, 0 }
191 };
192
193 static struct   ida_board *ida_eisa_match(eisa_id_t);
194 static int      ida_eisa_probe(device_t);
195 static int      ida_eisa_attach(device_t);
196
197 static device_method_t ida_eisa_methods[] = {
198         DEVMETHOD(device_probe,         ida_eisa_probe),
199         DEVMETHOD(device_attach,        ida_eisa_attach),
200         DEVMETHOD(device_detach,        ida_detach),
201
202         { 0, 0 }
203 };
204
205 static driver_t ida_eisa_driver = {
206         "ida",
207         ida_eisa_methods,
208         sizeof(struct ida_softc)
209 };
210
211 static devclass_t ida_devclass;
212
213 static struct ida_board *
214 ida_eisa_match(eisa_id_t id)
215 {
216         int i;
217
218         for (i = 0; board_id[i].board; i++)
219                 if (board_id[i].board == id)
220                         return (&board_id[i]);
221         return (NULL);
222 }
223
224 static int
225 ida_eisa_probe(device_t dev)
226 {
227         struct ida_board        *board;
228         u_int32_t               io_base;
229         u_int                   irq = 0;
230
231         board = ida_eisa_match(eisa_get_id(dev));
232         if (board == NULL)
233                 return (ENXIO);
234         device_set_desc(dev, board->desc);
235
236         io_base = (eisa_get_slot(dev) * EISA_SLOT_SIZE);
237
238         switch (IDA_EISA_IRQ_MASK & (inb(IDA_EISA_IRQ_REG + io_base))) {
239         case IDA_EISA_IRQ_15:
240                 irq = 15;
241                 break;
242         case IDA_EISA_IRQ_14:
243                 irq = 14;
244                 break;
245         case IDA_EISA_IRQ_11:
246                 irq = 11;
247                 break;
248         case IDA_EISA_IRQ_10:
249                 irq = 10;
250                 break;
251         default:
252                 device_printf(dev, "slot %d, illegal irq setting.\n",
253                     eisa_get_slot(dev));
254                 return (ENXIO);
255         }
256
257         eisa_add_iospace(dev, (io_base + IDA_EISA_IOPORT_START),
258                          IDA_EISA_IOPORT_LEN, RESVADDR_NONE);
259
260         eisa_add_intr(dev, irq, EISA_TRIGGER_LEVEL);            /* XXX ??? */
261
262         return (0);
263 }
264
265 static int
266 ida_eisa_attach(device_t dev)
267 {
268         struct ida_softc        *ida;
269         struct ida_board        *board;
270         int                     error;
271         int                     rid;
272
273         ida = device_get_softc(dev);
274         ida->dev = dev;
275
276         board = ida_eisa_match(eisa_get_id(dev));
277         ida->cmd = *board->accessor;
278         ida->flags = board->flags;
279
280         ida->regs_res_type = SYS_RES_IOPORT;
281         ida->regs_res_id = 0;
282         ida->regs = bus_alloc_resource(dev, ida->regs_res_type,
283             &ida->regs_res_id, 0, ~0, 1, RF_ACTIVE);
284         if (ida->regs == NULL) {
285                 device_printf(dev, "can't allocate register resources\n");
286                 return (ENOMEM);
287         }
288
289         error = bus_dma_tag_create(
290                 /* parent       */      NULL,
291                 /* alignment    */      0,
292                 /* boundary     */      0,
293                 /* lowaddr      */      BUS_SPACE_MAXADDR_32BIT,
294                 /* highaddr     */      BUS_SPACE_MAXADDR,
295                 /* filter       */      NULL,
296                 /* filterarg    */      NULL,
297                 /* maxsize      */      MAXBSIZE,
298                 /* nsegments    */      IDA_NSEG,
299                 /* maxsegsize   */      BUS_SPACE_MAXSIZE_32BIT,
300                 /* flags        */      BUS_DMA_ALLOCNOW,
301                 &ida->parent_dmat);
302
303         if (error != 0) {
304                 device_printf(dev, "can't allocate DMA tag\n");
305                 ida_free(ida);
306                 return (ENOMEM);
307         }
308
309         rid = 0;
310         ida->irq_res_type = SYS_RES_IRQ;
311         ida->irq = bus_alloc_resource(dev, ida->irq_res_type, &rid,
312             0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
313         if (ida->irq == NULL) {
314                 ida_free(ida);
315                 return (ENOMEM);
316         }
317
318         error = bus_setup_intr(dev, ida->irq, 0,
319                                ida_intr, ida, &ida->ih, NULL);
320         if (error) {
321                 device_printf(dev, "can't setup interrupt\n");
322                 ida_free(ida);
323                 return (ENOMEM);
324         }
325
326         error = ida_init(ida);
327         if (error) {
328                 ida_free(ida);
329                 return (error);
330         }
331
332         ida_attach(ida);
333         ida->flags |= IDA_ATTACHED;
334
335         return (0);
336 }
337
338 DRIVER_MODULE(ida, eisa, ida_eisa_driver, ida_devclass, 0, 0);