obio.c revision 1.3 1 1.3 deraadt /* $NetBSD: obio.c,v 1.3 1994/10/02 22:00:29 deraadt Exp $ */
2 1.1 deraadt
3 1.1 deraadt /*
4 1.1 deraadt * Copyright (c) 1993, 1994 Theo de Raadt
5 1.1 deraadt * All rights reserved.
6 1.1 deraadt *
7 1.1 deraadt * Redistribution and use in source and binary forms, with or without
8 1.1 deraadt * modification, are permitted provided that the following conditions
9 1.1 deraadt * are met:
10 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
11 1.1 deraadt * notice, this list of conditions and the following disclaimer.
12 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
14 1.1 deraadt * documentation and/or other materials provided with the distribution.
15 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
16 1.1 deraadt * must display the following acknowledgement:
17 1.1 deraadt * This product includes software developed by Theo de Raadt.
18 1.1 deraadt * 4. The name of the author may not be used to endorse or promote products
19 1.1 deraadt * derived from this software without specific prior written permission.
20 1.1 deraadt *
21 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 deraadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 deraadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 deraadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 deraadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 deraadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 deraadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 deraadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 deraadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 deraadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 deraadt */
32 1.1 deraadt
33 1.1 deraadt #include <sys/param.h>
34 1.1 deraadt #include <sys/device.h>
35 1.1 deraadt #include <sys/malloc.h>
36 1.1 deraadt
37 1.1 deraadt #ifdef DEBUG
38 1.1 deraadt #include <sys/proc.h>
39 1.1 deraadt #include <sys/syslog.h>
40 1.1 deraadt #endif
41 1.1 deraadt
42 1.1 deraadt #include <vm/vm.h>
43 1.1 deraadt
44 1.1 deraadt #include <machine/autoconf.h>
45 1.1 deraadt #include <machine/pmap.h>
46 1.2 deraadt #include <machine/oldmon.h>
47 1.2 deraadt #include <machine/ctlreg.h>
48 1.2 deraadt #include <sparc/sparc/asm.h>
49 1.2 deraadt #include <sparc/sparc/vaddrs.h>
50 1.1 deraadt
51 1.1 deraadt struct obio_softc {
52 1.1 deraadt struct device sc_dev; /* base device */
53 1.1 deraadt int nothing;
54 1.1 deraadt };
55 1.1 deraadt
56 1.1 deraadt /* autoconfiguration driver */
57 1.1 deraadt static int obiomatch(struct device *, struct cfdata *, void *);
58 1.1 deraadt static void obioattach(struct device *, struct device *, void *);
59 1.3 deraadt struct cfdriver obiocd = { NULL, "obio", obiomatch, obioattach,
60 1.1 deraadt DV_DULL, sizeof(struct obio_softc)
61 1.1 deraadt };
62 1.1 deraadt
63 1.2 deraadt void * obio_map __P((void *, int));
64 1.2 deraadt void * obio_tmp_map __P((void *));
65 1.2 deraadt void obio_tmp_unmap __P((void));
66 1.2 deraadt
67 1.1 deraadt int
68 1.3 deraadt obiomatch(parent, cf, aux)
69 1.1 deraadt struct device *parent;
70 1.1 deraadt struct cfdata *cf;
71 1.3 deraadt void *aux;
72 1.1 deraadt {
73 1.3 deraadt register struct confargs *ca = aux;
74 1.3 deraadt register struct romaux *ra = &ca->ca_ra;
75 1.3 deraadt
76 1.1 deraadt if (cputyp != CPU_SUN4)
77 1.3 deraadt return (0);
78 1.3 deraadt return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
79 1.1 deraadt }
80 1.1 deraadt
81 1.1 deraadt int
82 1.1 deraadt obio_print(args, obio)
83 1.1 deraadt void *args;
84 1.1 deraadt char *obio;
85 1.1 deraadt {
86 1.2 deraadt register struct confargs *ca = args;
87 1.2 deraadt
88 1.2 deraadt if (ca->ca_ra.ra_name == NULL)
89 1.2 deraadt ca->ca_ra.ra_name = "<unknown>";
90 1.2 deraadt if (obio)
91 1.3 deraadt printf("[%s at %s]", ca->ca_ra.ra_name, obio);
92 1.3 deraadt printf(" addr %x", ca->ca_ra.ra_paddr);
93 1.1 deraadt return (UNCONF);
94 1.1 deraadt }
95 1.1 deraadt
96 1.1 deraadt void
97 1.3 deraadt obioattach(parent, self, args)
98 1.1 deraadt struct device *parent, *self;
99 1.3 deraadt void *args;
100 1.1 deraadt {
101 1.2 deraadt register struct obio_softc *sc = (struct obio_softc *)self;
102 1.1 deraadt extern struct cfdata cfdata[];
103 1.3 deraadt register struct confargs *ca = args;
104 1.3 deraadt struct confargs oca;
105 1.1 deraadt register short *p;
106 1.1 deraadt struct cfdata *cf;
107 1.3 deraadt caddr_t tmpmap;
108 1.3 deraadt
109 1.3 deraadt printf("\n");
110 1.1 deraadt
111 1.1 deraadt if (sc->sc_dev.dv_unit > 0) {
112 1.1 deraadt printf(" unsupported\n");
113 1.1 deraadt return;
114 1.1 deraadt }
115 1.1 deraadt
116 1.1 deraadt for (cf = cfdata; cf->cf_driver; cf++) {
117 1.1 deraadt if (cf->cf_fstate == FSTATE_FOUND)
118 1.1 deraadt continue;
119 1.1 deraadt for (p = cf->cf_parents; *p >= 0; p++)
120 1.3 deraadt if (self->dv_cfdata == &cfdata[*p]) {
121 1.3 deraadt oca.ca_ra.ra_iospace = -1;
122 1.3 deraadt oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
123 1.3 deraadt oca.ca_ra.ra_len = 0;
124 1.3 deraadt tmpmap = NULL;
125 1.3 deraadt if (oca.ca_ra.ra_paddr)
126 1.3 deraadt tmpmap = obio_tmp_map(oca.ca_ra.ra_paddr);
127 1.3 deraadt oca.ca_ra.ra_vaddr = tmpmap;
128 1.3 deraadt oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
129 1.3 deraadt oca.ca_ra.ra_intr[0].int_vec = 0;
130 1.3 deraadt oca.ca_ra.ra_nintr = 1;
131 1.3 deraadt oca.ca_ra.ra_name = cf->cf_driver->cd_name;
132 1.3 deraadt oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
133 1.3 deraadt oca.ca_bustype = BUS_OBIO;
134 1.3 deraadt
135 1.3 deraadt if ((*cf->cf_driver->cd_match)(self, cf, &oca) == 0)
136 1.2 deraadt continue;
137 1.2 deraadt
138 1.3 deraadt /*
139 1.3 deraadt * check if XXmatch routine replaced the
140 1.3 deraadt * temporary mapping with a real mapping.
141 1.3 deraadt */
142 1.3 deraadt if (tmpmap == oca.ca_ra.ra_vaddr)
143 1.3 deraadt oca.ca_ra.ra_vaddr = NULL;
144 1.3 deraadt /*
145 1.3 deraadt * or if it has asked us to create a mapping..
146 1.3 deraadt * (which won't be seen on future XXmatch calls,
147 1.3 deraadt * so not as useful as it seems.)
148 1.3 deraadt */
149 1.3 deraadt if (oca.ca_ra.ra_len)
150 1.3 deraadt oca.ca_ra.ra_vaddr =
151 1.3 deraadt obio_map(oca.ca_ra.ra_paddr,
152 1.3 deraadt oca.ca_ra.ra_len);
153 1.3 deraadt
154 1.3 deraadt config_attach(self, cf, &oca, obio_print);
155 1.1 deraadt }
156 1.1 deraadt }
157 1.2 deraadt obio_tmp_unmap();
158 1.2 deraadt }
159 1.2 deraadt
160 1.2 deraadt #define getpte(va) lda(va, ASI_PTE)
161 1.2 deraadt
162 1.2 deraadt /*
163 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
164 1.2 deraadt * Else, create a new mapping.
165 1.2 deraadt */
166 1.2 deraadt void *
167 1.2 deraadt obio_map(pa, len)
168 1.2 deraadt void *pa;
169 1.2 deraadt int len;
170 1.2 deraadt {
171 1.3 deraadt u_long pf = (u_long)pa >> PGSHIFT;
172 1.2 deraadt u_long va, pte;
173 1.2 deraadt
174 1.3 deraadt if (len <= NBPG) {
175 1.3 deraadt for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
176 1.3 deraadt pte = getpte(va);
177 1.3 deraadt if ((pte & PG_V) != 0 && (pte & PG_TYPE) == PG_OBIO &&
178 1.3 deraadt (pte & PG_PFNUM) == pf)
179 1.3 deraadt return ((void *)va);
180 1.3 deraadt }
181 1.2 deraadt }
182 1.2 deraadt return mapiodev(pa, len);
183 1.2 deraadt }
184 1.2 deraadt
185 1.2 deraadt void *
186 1.2 deraadt obio_tmp_map(pa)
187 1.2 deraadt void *pa;
188 1.2 deraadt {
189 1.3 deraadt vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
190 1.3 deraadt
191 1.2 deraadt pmap_enter(kernel_pmap, TMPMAP_VA,
192 1.3 deraadt addr | PMAP_OBIO | PMAP_NC,
193 1.2 deraadt VM_PROT_READ | VM_PROT_WRITE, 1);
194 1.2 deraadt return ((void *)TMPMAP_VA);
195 1.2 deraadt }
196 1.2 deraadt
197 1.2 deraadt void
198 1.2 deraadt obio_tmp_unmap()
199 1.2 deraadt {
200 1.2 deraadt pmap_remove(kernel_pmap, TMPMAP_VA, TMPMAP_VA+NBPG);
201 1.1 deraadt }
202