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