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