sti_sgc.c revision 1.2 1 /* $NetBSD: sti_sgc.c,v 1.2 2014/04/20 04:12:54 tsutsui Exp $ */
2 /* $OpenBSD: sti_sgc.c,v 1.14 2007/05/26 00:36:03 krw Exp $ */
3
4 /*
5 * Copyright (c) 2005, Miodrag Vallat
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: sti_sgc.c,v 1.2 2014/04/20 04:12:54 tsutsui Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/bus.h>
35
36 #include <uvm/uvm_extern.h>
37
38 #include <dev/wscons/wsdisplayvar.h>
39
40 #include <dev/ic/stireg.h>
41 #include <dev/ic/stivar.h>
42
43 #include <hp300/dev/sgcvar.h>
44 #include <hp300/dev/sti_sgcvar.h>
45 #include <machine/autoconf.h>
46
47 static int sticonslot = -1;
48 static struct sti_rom sticn_rom;
49 static struct sti_screen sticn_scr;
50 static bus_addr_t sticn_bases[STI_REGION_MAX];
51
52 static int sti_sgc_match(device_t, struct cfdata *, void *);
53 static void sti_sgc_attach(device_t, device_t, void *);
54
55 static int sti_sgc_probe(bus_space_tag_t, int);
56
57 CFATTACH_DECL_NEW(sti_sgc, sizeof(struct sti_softc),
58 sti_sgc_match, sti_sgc_attach, NULL, NULL);
59
60 static int
61 sti_sgc_match(device_t parent, struct cfdata *cf, void *aux)
62 {
63 struct sgc_attach_args *saa = aux;
64
65 /*
66 * If we already probed it successfully as a console device, go ahead,
67 * since we will not be able to bus_space_map() again.
68 */
69 if (saa->saa_slot == sticonslot)
70 return 1;
71
72 return sti_sgc_probe(saa->saa_iot, saa->saa_slot);
73 }
74
75 static void
76 sti_sgc_attach(device_t parent, device_t self, void *aux)
77 {
78 struct sti_softc *sc = device_private(self);
79 struct sgc_attach_args *saa = aux;
80 bus_space_handle_t romh;
81 bus_addr_t base;
82 u_int romend;
83 int i;
84
85 sc->sc_dev = self;
86
87 if (saa->saa_slot == sticonslot) {
88 sc->sc_flags |= STI_CONSOLE | STI_ATTACHED;
89 sc->sc_rom = &sticn_rom;
90 sc->sc_scr = &sticn_scr;
91 memcpy(sc->bases, sticn_bases, sizeof(sc->bases));
92
93 sti_describe(sc);
94 } else {
95 base = (bus_addr_t)sgc_slottopa(saa->saa_slot);
96 if (bus_space_map(saa->saa_iot, base, PAGE_SIZE, 0, &romh)) {
97 aprint_error(": can't map ROM");
98 return;
99 }
100 /*
101 * Compute real PROM size
102 */
103 romend = sti_rom_size(saa->saa_iot, romh);
104
105 bus_space_unmap(saa->saa_iot, romh, PAGE_SIZE);
106
107 if (bus_space_map(saa->saa_iot, base, romend, 0, &romh)) {
108 aprint_error(": can't map frame buffer");
109 return;
110 }
111
112 sc->bases[0] = romh;
113 for (i = 0; i < STI_REGION_MAX; i++)
114 sc->bases[i] = base;
115
116 if (sti_attach_common(sc, saa->saa_iot, saa->saa_iot, romh,
117 STI_CODEBASE_ALT) != 0)
118 return;
119 }
120
121 /*
122 * Note on 425e sti(4) framebuffer bitmap memory can be accessed at
123 * (sgc_slottopa(saa->saa_slot) + 0x200000)
124 * but the mmap function to map bitmap display is not provided yet.
125 */
126
127 sti_end_attach(sc);
128 }
129
130 static int
131 sti_sgc_probe(bus_space_tag_t iot, int slot)
132 {
133 bus_space_handle_t ioh;
134 int devtype;
135
136 if (bus_space_map(iot, (bus_addr_t)sgc_slottopa(slot),
137 PAGE_SIZE, 0, &ioh))
138 return 0;
139
140 devtype = bus_space_read_1(iot, ioh, 3);
141
142 bus_space_unmap(iot, ioh, PAGE_SIZE);
143
144 /*
145 * This might not be reliable enough. On the other hand, non-STI
146 * SGC cards will apparently not initialize in an hp300, to the
147 * point of not even answering bus probes (checked with an
148 * Harmony/FDDI SGC card).
149 */
150 if (devtype != STI_DEVTYPE1 && devtype != STI_DEVTYPE4)
151 return 0;
152
153 return 1;
154 }
155
156 int
157 sti_sgc_cnprobe(bus_space_tag_t bst, bus_addr_t addr, int slot)
158 {
159 void *va;
160 bus_space_handle_t romh;
161 int devtype, rv = 0;
162
163 if (bus_space_map(bst, addr, PAGE_SIZE, 0, &romh))
164 return 0;
165
166 va = bus_space_vaddr(bst, romh);
167 if (badaddr(va))
168 goto out;
169
170 devtype = bus_space_read_1(bst, romh, 3);
171 if (devtype == STI_DEVTYPE1 || devtype == STI_DEVTYPE4)
172 rv = 1;
173
174 out:
175 bus_space_unmap(bst, romh, PAGE_SIZE);
176 return rv;
177 }
178
179 void
180 sti_sgc_cnattach(bus_space_tag_t bst, bus_addr_t addr, int slot)
181 {
182 int i;
183
184 /* sticn_bases[0] will be fixed in sti_cnattach() */
185 for (i = 0; i < STI_REGION_MAX; i++)
186 sticn_bases[i] = addr;
187
188 sti_cnattach(&sticn_rom, &sticn_scr, bst, sticn_bases,
189 STI_CODEBASE_ALT);
190
191 sticonslot = slot;
192 }
193