]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/sparc64/isa/isa.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / sparc64 / isa / isa.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
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  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
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
19  * FOR 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  *      from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36
37 #include <machine/bus.h>
38
39 #include <sys/rman.h>
40
41 #include <isa/isareg.h>
42 #include <isa/isavar.h>
43 #include <isa/isa_common.h>
44
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/openfirm.h>
47
48 #include <machine/resource.h>
49
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52
53 #include <sparc64/pci/ofw_pci.h>
54 #include <sparc64/isa/ofw_isa.h>
55
56 /* There can be only one ISA bus, so it is safe to use globals. */
57 static u_int64_t isa_io_base;
58 static u_int64_t isa_io_limit;
59 static u_int64_t isa_mem_base;
60 static u_int64_t isa_mem_limit;
61
62 device_t isa_bus_device;
63
64 static phandle_t isab_node;
65 static struct isa_ranges *isab_ranges;
66 static int isab_nrange;
67 static struct ofw_bus_iinfo isa_iinfo;
68
69 /*
70  * XXX: This is really partly PCI-specific, but unfortunately is
71  * differently enough to have to duplicate it here...
72  */
73 #define ISAB_RANGE_PHYS(r)                                              \
74         (((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo)
75 #define ISAB_RANGE_SPACE(r)     (((r)->phys_hi >> 24) & 0x03)
76
77 #define ISAR_SPACE_IO           0x01
78 #define ISAR_SPACE_MEM          0x02
79
80 #define INRANGE(x, start, end)  ((x) >= (start) && (x) <= (end))
81
82 static void     isa_setup_children(device_t, phandle_t);
83
84 void
85 isa_init(device_t dev)
86 {
87         device_t bridge;
88         int i;
89
90         /* The parent of the bus must be a PCI-ISA bridge. */
91         bridge = device_get_parent(dev);
92         isab_node = ofw_bus_get_node(bridge);
93         isab_nrange = OF_getprop_alloc(isab_node, "ranges",
94             sizeof(*isab_ranges), (void **)&isab_ranges);
95         if (isab_nrange <= 0)
96                 panic("isa_init: cannot get bridge range property");
97
98         ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t));
99
100         isa_setup_children(dev, isab_node);
101
102         for (i = isab_nrange - 1; i >= 0; i--) {
103                 switch(ISAB_RANGE_SPACE(&isab_ranges[i])) {
104                 case ISAR_SPACE_IO:
105                         /* This is probably always 0. */
106                         isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
107                         isa_io_limit = isab_ranges[i].size;
108                         break;
109                 case ISAR_SPACE_MEM:
110                         /* This is probably always 0. */
111                         isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
112                         isa_mem_limit = isab_ranges[i].size;
113                         break;
114                 }
115         }
116 }
117
118 static const struct {
119         const char      *const name;
120         uint32_t        id;
121 } const ofw_isa_pnp_map[] = {
122         { "SUNW,lomh",  0x0000ae4e }, /* SUN0000 */
123         { "dma",        0x0002d041 }, /* PNP0200 */
124         { "floppy",     0x0007d041 }, /* PNP0700 */
125         { "rtc",        0x000bd041 }, /* PNP0B00 */
126         { "flashprom",  0x0100ae4e }, /* SUN0001 */
127         { "parallel",   0x0104d041 }, /* PNP0401 */
128         { "serial",     0x0105d041 }, /* PNP0501 */
129         { "su",         0x0105d041 }, /* PNP0501 */
130         { "i2c",        0x0200ae4e }, /* SUN0002 */
131         { "rmc-comm",   0x0300ae4e }, /* SUN0003 */
132         { "kb_ps2",     0x0303d041 }, /* PNP0303 */
133         { "kdmouse",    0x030fd041 }, /* PNP0F03 */
134         { "bscbus",     0x0400ae4e }, /* SUN0004 */
135         { "power",      0x0c0cd041 }, /* PNP0C0C */
136         { NULL,         0x0 }
137 };
138
139 static void
140 isa_setup_children(device_t dev, phandle_t parent)
141 {
142         struct isa_regs *regs;
143         struct resource_list *rl;
144         device_t cdev;
145         u_int64_t end, start;
146         ofw_isa_intr_t *intrs, rintr;
147         phandle_t node;
148         uint32_t *drqs, *regidx;
149         int i, ndrq, nintr, nreg, nregidx, rid, rtype;
150         char *name;
151
152         /*
153          * Loop through children and fake up PnP devices for them.
154          * Their resources are added as fully mapped and specified because
155          * adjusting the resources and the resource list entries respectively
156          * in isa_alloc_resource() causes trouble with drivers which use
157          * rman_get_start(), pass-through or allocate and release resources
158          * multiple times, etc. Adjusting the resources might be better off
159          * in a bus_activate_resource method but the common ISA code doesn't
160          * allow for an isa_activate_resource().
161          */
162         for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
163                 if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1)
164                         continue;
165
166                 /*
167                  * Keyboard and mouse controllers hang off of the `8042'
168                  * node but we have no real use for the `8042' itself.
169                  */
170                 if (strcmp(name, "8042") == 0) {
171                         isa_setup_children(dev, node);
172                         free(name, M_OFWPROP);
173                         continue;
174                 }
175
176                 for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++)
177                         if (strcmp(ofw_isa_pnp_map[i].name, name) == 0)
178                                 break;
179                 if (ofw_isa_pnp_map[i].name == NULL) {
180                         device_printf(dev, "no PnP map entry for node "
181                             "0x%lx: %s\n", (unsigned long)node, name);
182                         free(name, M_OFWPROP);
183                         continue;
184                 }
185
186                 if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) ==
187                     NULL)
188                         panic("isa_setup_children: BUS_ADD_CHILD failed");
189                 isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id);
190                 isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id);
191
192                 rl = BUS_GET_RESOURCE_LIST(dev, cdev);
193                 nreg = OF_getprop_alloc(node, "reg", sizeof(*regs),
194                     (void **)&regs);
195                 for (i = 0; i < nreg; i++) {
196                         start = ISA_REG_PHYS(&regs[i]);
197                         end = start + regs[i].size - 1;
198                         rtype = ofw_isa_range_map(isab_ranges, isab_nrange,
199                             &start, &end, NULL);
200                         rid = 0;
201                         while (resource_list_find(rl, rtype, rid) != NULL)
202                                 rid++;
203                         bus_set_resource(cdev, rtype, rid, start,
204                             end - start + 1);
205                 }
206                 if (nreg == -1 && parent != isab_node) {
207                         /*
208                          * The "reg" property still might be an index into
209                          * the set of registers of the parent device like
210                          * with the nodes hanging off of the `8042' node.
211                          */
212                         nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx),
213                             (void **)&regidx);
214                         if (nregidx > 2)
215                                 panic("isa_setup_children: impossible number "
216                                     "of register indices");
217                         if (nregidx != -1 && (nreg = OF_getprop_alloc(parent,
218                             "reg", sizeof(*regs), (void **)&regs)) >= nregidx) {
219                                 for (i = 0; i < nregidx; i++) {
220                                         start = ISA_REG_PHYS(&regs[regidx[i]]);
221                                         end = start + regs[regidx[i]].size - 1;
222                                         rtype = ofw_isa_range_map(isab_ranges,
223                                             isab_nrange, &start, &end, NULL);
224                                         rid = 0;
225                                         while (resource_list_find(rl, rtype,
226                                             rid) != NULL)
227                                                 rid++;
228                                         bus_set_resource(cdev, rtype, rid,
229                                             start, end - start + 1);
230                                 }
231                         }
232                         if (regidx != NULL)
233                                 free(regidx, M_OFWPROP);
234                 }
235                 if (regs != NULL)
236                         free(regs, M_OFWPROP);
237
238                 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs),
239                     (void **)&intrs);
240                 for (i = 0; i < nintr; i++) {
241                         if (intrs[i] > 7)
242                                 panic("isa_setup_children: intr too large");
243                         rintr = ofw_isa_route_intr(device_get_parent(dev), node,
244                             &isa_iinfo, intrs[i]);
245                         if (rintr == PCI_INVALID_IRQ) {
246                                 device_printf(dev, "could not map ISA "
247                                     "interrupt %d for node 0x%lx: %s\n",
248                                     intrs[i], (unsigned long)node, name);
249                                 continue;
250                         }
251                         bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1);
252                 }
253                 if (intrs != NULL)
254                         free(intrs, M_OFWPROP);
255
256                 ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs),
257                     (void **)&drqs);
258                 for (i = 0; i < ndrq; i++)
259                         bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1);
260                 if (drqs != NULL)
261                         free(drqs, M_OFWPROP);
262
263                 /*
264                  * Devices using DMA hang off of the `dma' node instead of
265                  * directly from the ISA bridge node.
266                  */
267                 if (strcmp(name, "dma") == 0)
268                         isa_setup_children(dev, node);
269
270                 free(name, M_OFWPROP);
271         }
272 }
273
274 struct resource *
275 isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
276     u_long start, u_long end, u_long count, u_int flags)
277 {
278         /*
279          * Consider adding a resource definition.
280          */
281         int passthrough = (device_get_parent(child) != bus);
282         int isdefault = (start == 0UL && end == ~0UL);
283         struct resource_list *rl;
284         struct resource_list_entry *rle;
285         u_long base, limit;
286
287         rl = BUS_GET_RESOURCE_LIST(bus, child);
288         if (!passthrough && !isdefault) {
289                 rle = resource_list_find(rl, type, *rid);
290                 if (!rle) {
291                         if (*rid < 0)
292                                 return (NULL);
293                         switch (type) {
294                         case SYS_RES_IRQ:
295                                 if (*rid >= ISA_NIRQ)
296                                         return (NULL);
297                                 break;
298                         case SYS_RES_DRQ:
299                                 if (*rid >= ISA_NDRQ)
300                                         return (NULL);
301                                 break;
302                         case SYS_RES_MEMORY:
303                                 if (*rid >= ISA_NMEM)
304                                         return (NULL);
305                                 break;
306                         case SYS_RES_IOPORT:
307                                 if (*rid >= ISA_NPORT)
308                                         return (NULL);
309                                 break;
310                         default:
311                                 return (NULL);
312                         }
313                         resource_list_add(rl, type, *rid, start, end, count);
314                 }
315         }
316
317         /*
318          * Sanity check if the resource in the respective entry is fully
319          * mapped and specified and its type allocable. A driver could
320          * have added an out of range resource on its own.
321          */
322         if (!passthrough) {
323                 if ((rle = resource_list_find(rl, type, *rid)) == NULL)
324                         return (NULL);
325                 base = limit = 0;
326                 switch (type) {
327                 case SYS_RES_MEMORY:
328                         base = isa_mem_base;
329                         limit = base + isa_mem_limit;
330                         break;
331                 case SYS_RES_IOPORT:
332                         base = isa_io_base;
333                         limit = base + isa_io_limit;
334                         break;
335                 case SYS_RES_IRQ:
336                         if (rle->start != rle->end || rle->start <= 7)
337                                 return (NULL);
338                         break;
339                 case SYS_RES_DRQ:
340                         break;
341                 default:
342                         return (NULL);
343                 }
344                 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
345                         if (!INRANGE(rle->start, base, limit) ||
346                             !INRANGE(rle->end, base, limit))
347                                 return (NULL);
348                 }
349         }
350
351         return (resource_list_alloc(rl, bus, child, type, rid, start, end,
352             count, flags));
353 }
354
355 int
356 isa_release_resource(device_t bus, device_t child, int type, int rid,
357     struct resource *res)
358 {
359
360         return (bus_generic_rl_release_resource(bus, child, type, rid, res));
361 }