Merge from vendor branch HEIMDAL:
[dragonfly.git] / sys / dev / raid / dpt / dpt_eisa.c
1 /*-
2  * Copyright (c) 1997, 2000 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/dpt/dpt_eisa.c,v 1.12.2.1 2000/08/07 18:48:14 peter Exp $
27  *      $DragonFly: src/sys/dev/raid/dpt/dpt_eisa.c,v 1.3 2003/08/07 21:17:08 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35
36 #include <machine/bus_pio.h>
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40
41 #include <bus/eisa/eisaconf.h>
42
43 #include <bus/cam/scsi/scsi_all.h>
44
45 #include "dpt.h"
46
47 #define DPT_EISA_IOSIZE                 0x100
48 #define DPT_EISA_SLOT_OFFSET            0x0c00
49 #define DPT_EISA_EATA_REG_OFFSET        0x0088
50
51 #define DPT_EISA_DPT2402        0x12142402      /* DPT PM2012A/9X       */
52 #define DPT_EISA_DPTA401        0x1214A401      /* DPT PM2012B/9X       */
53 #define DPT_EISA_DPTA402        0x1214A402      /* DPT PM2012B2/9X      */
54 #define DPT_EISA_DPTA410        0x1214A410      /* DPT PM2x22A/9X       */
55 #define DPT_EISA_DPTA411        0x1214A411      /* DPT Spectre          */
56 #define DPT_EISA_DPTA412        0x1214A412      /* DPT PM2021A/9X       */
57 #define DPT_EISA_DPTA420        0x1214A420      /* DPT Smart Cache IV (PM2042) */
58 #define DPT_EISA_DPTA501        0x1214A501      /* DPT PM2012B1/9X"     */
59 #define DPT_EISA_DPTA502        0x1214A502      /* DPT PM2012Bx/9X      */
60 #define DPT_EISA_DPTA701        0x1214A701      /* DPT PM2011B1/9X      */
61 #define DPT_EISA_DPTBC01        0x1214BC01      /* DPT PM3011/7X ESDI   */ 
62 #define DPT_EISA_DPT8200        0x12148200      /* NEC EATA SCSI        */
63 #define DPT_EISA_DPT2408        0x12142408      /* ATT EATA SCSI        */
64
65 /* Function Prototypes */
66
67 static const char *     dpt_eisa_match  (eisa_id_t);
68 static int              dpt_eisa_probe  (device_t);
69 static int              dpt_eisa_attach (device_t);
70
71
72 static int
73 dpt_eisa_probe (device_t dev)
74 {
75         const char *    desc;
76         u_int32_t       io_base;
77         dpt_conf_t *    conf;
78
79         desc = dpt_eisa_match(eisa_get_id(dev));
80         if (!desc)
81                 return (ENXIO);
82         device_set_desc(dev, desc);
83
84         io_base = (eisa_get_slot(dev) * EISA_SLOT_SIZE) + DPT_EISA_SLOT_OFFSET;
85
86         conf = dpt_pio_get_conf(io_base + DPT_EISA_EATA_REG_OFFSET);
87         if (!conf) {
88                 printf("dpt: dpt_pio_get_conf() failed.\n");
89                 return (ENXIO);
90         }
91
92         eisa_add_iospace(dev, io_base, DPT_EISA_IOSIZE, RESVADDR_NONE);
93         eisa_add_intr(dev, conf->IRQ,
94                       (conf->IRQ_TR ? EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE));
95
96         return 0;
97 }
98
99 static int
100 dpt_eisa_attach (device_t dev)
101 {
102         dpt_softc_t * dpt;
103         struct resource *io = 0;
104         struct resource *irq = 0;
105         int             s;
106         int             rid;
107         void *          ih;
108         int             error = 0;
109
110         rid = 0;
111         io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
112         if (!io) {
113                 device_printf(dev, "No I/O space?!\n");
114                 error = ENOMEM;
115                 goto bad;
116         }
117
118         rid = 0;
119         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
120         if (!irq) {
121                 device_printf(dev, "No irq?!\n");
122                 error = ENOMEM;
123                 goto bad;
124         }
125
126         dpt = dpt_alloc(dev, rman_get_bustag(io),
127                         rman_get_bushandle(io) + DPT_EISA_EATA_REG_OFFSET);
128         if (dpt == NULL) {
129                 error = ENOMEM;
130                 goto bad;
131         }
132
133         /* Allocate a dmatag representing the capabilities of this attachment */
134         /* XXX Should be a child of the EISA bus dma tag */
135         if (bus_dma_tag_create( /* parent    */ NULL,
136                                 /* alignemnt */ 1,
137                                 /* boundary  */ 0,
138                                 /* lowaddr   */ BUS_SPACE_MAXADDR_32BIT,
139                                 /* highaddr  */ BUS_SPACE_MAXADDR,
140                                 /* filter    */ NULL,
141                                 /* filterarg */ NULL,
142                                 /* maxsize   */ BUS_SPACE_MAXSIZE_32BIT,
143                                 /* nsegments */ BUS_SPACE_UNRESTRICTED,
144                                 /* maxsegsz  */ BUS_SPACE_MAXSIZE_32BIT,
145                                 /* flags     */0,
146                                 &dpt->parent_dmat) != 0) {
147                 dpt_free(dpt);
148                 error = ENXIO;
149                 goto bad;
150         }
151
152         s = splcam();
153
154         if (dpt_init(dpt) != 0) {
155                 dpt_free(dpt);
156                 error = ENXIO;
157                 goto bad;
158         }
159
160         /* Register with the XPT */
161         dpt_attach(dpt);
162
163         splx(s);
164
165         if (bus_setup_intr(dev, irq, INTR_TYPE_CAM, dpt_intr, dpt, &ih)) {
166                 device_printf(dev, "Unable to register interrupt handler\n");
167                 error = ENXIO;
168                 goto bad;
169         }
170
171         return (error);
172
173  bad:
174         if (io)
175                 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
176         if (irq)
177                 bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
178
179         return (error);
180 }
181
182 static const char       *
183 dpt_eisa_match(type)
184         eisa_id_t       type;
185 {
186         switch (type) {
187                 case DPT_EISA_DPT2402:
188                 case DPT_EISA_DPTA401:
189                 case DPT_EISA_DPTA402:
190                 case DPT_EISA_DPTA410:
191                 case DPT_EISA_DPTA411:
192                 case DPT_EISA_DPTA412:
193                 case DPT_EISA_DPTA420:
194                 case DPT_EISA_DPTA501:
195                 case DPT_EISA_DPTA502:
196                 case DPT_EISA_DPTA701:
197                 case DPT_EISA_DPTBC01:
198                 case DPT_EISA_DPT8200:
199                 case DPT_EISA_DPT2408:
200                         return ("DPT SCSI Host Bus Adapter");
201                         break;
202                 default:
203                         break;
204         }
205         
206         return (NULL);
207 }
208
209 static device_method_t dpt_eisa_methods[] = {
210         /* Device interface */
211         DEVMETHOD(device_probe,         dpt_eisa_probe),
212         DEVMETHOD(device_attach,        dpt_eisa_attach),
213
214         { 0, 0 }
215 };
216
217 static driver_t dpt_eisa_driver = {
218         "dpt",
219         dpt_eisa_methods,
220         sizeof(dpt_softc_t),
221 };
222
223 static devclass_t dpt_devclass;
224
225 DRIVER_MODULE(dpt, eisa, dpt_eisa_driver, dpt_devclass, 0, 0);