Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / disk / aha / aha_mca.c
1 /*-
2  * Copyright (c) 1999 Matthew N. Dodd <winter@jurai.net>
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: src/sys/dev/aha/aha_mca.c,v 1.2 2000/01/24 07:08:40 imp Exp $
27  * $DragonFly: src/sys/dev/disk/aha/Attic/aha_mca.c,v 1.3 2003/08/07 21:16:50 dillon Exp $
28  *
29  * Based on aha_isa.c
30  */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42
43 #include <bus/isa/i386/isa_dma.h>
44 #include <bus/mca/mca_busreg.h>
45 #include <bus/mca/mca_busvar.h>
46
47 #include "ahareg.h"
48
49 static struct mca_ident aha_mca_devs[] = {
50         { 0x0f1f, "Adaptec AHA-1640 SCSI Adapter" },
51         { 0, NULL },
52 };
53
54 #define AHA_MCA_IOPORT_POS              MCA_ADP_POS(MCA_POS1)
55 # define AHA_MCA_IOPORT_MASK1           0x07
56 # define AHA_MCA_IOPORT_MASK2           0xc0
57 # define AHA_MCA_IOPORT_SIZE            0x03
58 # define AHA_MCA_IOPORT(pos)            (0x30 + \
59                                         (((u_int32_t)pos & \
60                                                 AHA_MCA_IOPORT_MASK1) << 8) + \
61                                         (((u_int32_t)pos & \
62                                                 AHA_MCA_IOPORT_MASK2) >> 4))
63
64 #define AHA_MCA_DRQ_POS                 MCA_ADP_POS(MCA_POS3)
65 # define AHA_MCA_DRQ_MASK               0x0f
66 # define AHA_MCA_DRQ(pos)               (pos & AHA_MCA_DRQ_MASK)
67
68 #define AHA_MCA_IRQ_POS                 MCA_ADP_POS(MCA_POS2)
69 # define AHA_MCA_IRQ_MASK               0x07
70 # define AHA_MCA_IRQ(pos)               ((pos & AHA_MCA_IRQ_MASK) + 8)
71
72 /*
73  * Not needed as the board knows its config
74  * internally and the ID will be fetched 
75  * via AOP_INQUIRE_SETUP_INFO command.
76  */
77 #define AHA_MCA_SCSIID_POS              MCA_ADP_POS(MCA_POS2)
78 #define AHA_MCA_SCSIID_MASK             0xe0
79 #define AHA_MCA_SCSIID(pos)             ((pos & AHA_MCA_SCSIID_MASK) >> 5)
80
81 static int
82 aha_mca_probe (device_t dev)
83 {
84         const char *    desc;
85         mca_id_t        id = mca_get_id(dev);
86         u_int32_t       iobase = 0;
87         u_int32_t       iosize = 0;
88         u_int8_t        drq = 0;
89         u_int8_t        irq = 0;
90         u_int8_t        pos;
91
92         desc = mca_match_id(id, aha_mca_devs);
93         if (!desc)
94                 return (ENXIO);
95         device_set_desc(dev, desc);
96
97         pos = mca_pos_read(dev, AHA_MCA_IOPORT_POS);
98         iobase = AHA_MCA_IOPORT(pos);
99         iosize = AHA_MCA_IOPORT_SIZE;
100
101         pos = mca_pos_read(dev, AHA_MCA_DRQ_POS);
102         drq = AHA_MCA_DRQ(pos);
103
104         pos = mca_pos_read(dev, AHA_MCA_IRQ_POS);
105         irq = AHA_MCA_IRQ(pos);
106
107         mca_add_iospace(dev, iobase, iosize);
108         mca_add_drq(dev, drq);
109         mca_add_irq(dev, irq);
110
111         aha_unit++;
112
113         return (0);
114 }
115
116 static int
117 aha_mca_attach (device_t dev)
118 {
119         struct aha_softc *      sc = NULL;
120         struct resource *       io = NULL;
121         struct resource *       irq = NULL;
122         struct resource *       drq = NULL;
123         int                     error = 0;
124         int                     unit = device_get_unit(dev);
125         int                     rid;
126         void *                  ih;
127
128         rid = 0;
129         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
130                                 0, ~0, 1, RF_ACTIVE);
131         if (!io) {
132                 device_printf(dev, "No I/O space?!\n");
133                 error = ENOMEM;
134                 goto bad;
135         }
136
137         rid = 0;
138         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
139                                  0, ~0, 1, RF_ACTIVE);
140         if (irq == NULL) {
141                 device_printf(dev, "No IRQ?!\n");
142                 error = ENOMEM;
143                 goto bad;
144         }
145
146         rid = 0;
147         drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
148                                  0, ~0, 1, RF_ACTIVE);
149         if (drq == NULL) {
150                 device_printf(dev, "No DRQ?!\n");
151                 error = ENOMEM;
152                 goto bad;
153         }
154
155         sc = aha_alloc(unit, rman_get_bustag(io), rman_get_bushandle(io));
156         if (sc == NULL) {
157                 device_printf(dev, "aha_alloc() failed!\n");
158                 error = ENOMEM;
159                 goto bad;
160         }
161
162         error = aha_probe(sc);
163         if (error) {
164                 device_printf(dev, "aha_probe() failed!\n");
165                 goto bad;
166         }
167
168         error = aha_fetch_adapter_info(sc);
169         if (error) {
170                 device_printf(dev, "aha_fetch_adapter_info() failed!\n");
171                 goto bad;
172         }
173
174         isa_dmacascade(rman_get_start(drq));
175
176         error = bus_dma_tag_create(/* parent    */      NULL,
177                                    /* alignemnt */      1,
178                                    /* boundary  */      0,
179                                                         BUS_SPACE_MAXADDR_24BIT,
180                                    /* highaddr  */      BUS_SPACE_MAXADDR,
181                                                         NULL,
182                                                         NULL,
183                                    /* maxsize   */      BUS_SPACE_MAXSIZE_24BIT,
184                                    /* nsegments */      BUS_SPACE_UNRESTRICTED,
185                                    /* maxsegsz  */      BUS_SPACE_MAXSIZE_24BIT,
186                                    /* flags     */      0,
187                                                         &sc->parent_dmat);
188         if (error) {
189                 device_printf(dev, "bus_dma_tag_create() failed!\n");
190                 goto bad;
191         }                             
192
193         error = aha_init(sc);
194         if (error) {
195                 device_printf(dev, "aha_init() failed\n");
196                 goto bad;
197         }
198
199         error = aha_attach(sc);
200         if (error) {
201                 device_printf(dev, "aha_attach() failed\n");
202                 goto bad;
203         }
204
205         error = bus_setup_intr(dev, irq, INTR_TYPE_CAM, aha_intr, sc, &ih);
206         if (error) {
207                 device_printf(dev, "Unable to register interrupt handler\n");
208                 goto bad;
209         }
210
211         return (0);
212
213 bad:
214         aha_free(sc);
215
216         if (io)
217                 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
218         if (irq)
219                 bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
220         if (drq)
221                 bus_release_resource(dev, SYS_RES_DRQ, 0, drq);
222
223         return (error);
224 }
225
226 static device_method_t aha_mca_methods[] = {
227         DEVMETHOD(device_probe,         aha_mca_probe),
228         DEVMETHOD(device_attach,        aha_mca_attach),
229
230         { 0, 0 }
231 };
232
233 static driver_t aha_mca_driver = {
234         "aha",
235         aha_mca_methods,
236         1,
237 /*
238         sizeof(struct aha_softc *),
239  */
240 };
241
242 static devclass_t aha_devclass;
243
244 DRIVER_MODULE(aha, mca, aha_mca_driver, aha_devclass, 0, 0);