1 /* $NetBSD: gten.c,v 1.24 2021/08/07 16:19:03 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas <matt@3am-software.com>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: gten.c,v 1.24 2021/08/07 16:19:03 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/buf.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/ioctl.h>
40 #include <sys/kernel.h>
41 #include <sys/kmem.h>
42 #include <sys/systm.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/pci/pcidevs.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wsdisplayvar.h>
52 #include <dev/rasops/rasops.h>
53
54 #include <sys/bus.h>
55 #include <machine/gtenvar.h>
56
57 static int gten_match(device_t, cfdata_t, void *);
58 static void gten_attach(device_t, device_t, void *);
59 static int gten_print(void *, const char *);
60
61 CFATTACH_DECL_NEW(gten, sizeof(struct gten_softc),
62 gten_match, gten_attach, NULL, NULL);
63
64 static struct rasops_info gten_console_ri;
65 static pcitag_t gten_console_pcitag;
66
67 static struct wsscreen_descr gten_stdscreen = {
68 "std",
69 0, 0,
70 0,
71 0, 0,
72 WSSCREEN_REVERSE
73 };
74
75 static const struct wsscreen_descr *_gten_scrlist[] = {
76 >en_stdscreen,
77 /* XXX other formats, graphics screen? */
78 };
79
80 static struct wsscreen_list gten_screenlist = {
81 sizeof(_gten_scrlist) / sizeof(struct wsscreen_descr *), _gten_scrlist
82 };
83
84 static int gten_ioctl(void *, void *, u_long, void *, int, struct proc *);
85 static paddr_t gten_mmap(void *, void *, off_t, int);
86 static int gten_alloc_screen(void *, const struct wsscreen_descr *,
87 void **, int *, int *, long *);
88 static void gten_free_screen(void *, void *);
89 static int gten_show_screen(void *, void *, int,
90 void (*) (void *, int, int), void *);
91
92 static struct wsdisplay_accessops gten_accessops = {
93 gten_ioctl,
94 gten_mmap,
95 gten_alloc_screen,
96 gten_free_screen,
97 gten_show_screen,
98 0 /* load_font */
99 };
100
101 static void gten_common_init(struct rasops_info *);
102 static int gten_getcmap(struct gten_softc *, struct wsdisplay_cmap *);
103 static int gten_putcmap(struct gten_softc *, struct wsdisplay_cmap *);
104
105 #define GTEN_VRAM_OFFSET 0xf00000
106
107 static int
gten_match(device_t parent,cfdata_t match,void * aux)108 gten_match(device_t parent, cfdata_t match, void *aux)
109 {
110 struct pci_attach_args *pa = aux;
111
112 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_WD &&
113 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_WD_90C)
114 return 2;
115
116 return 0;
117 }
118
119 static void
gten_attach(device_t parent,device_t self,void * aux)120 gten_attach(device_t parent, device_t self, void *aux)
121 {
122 struct gten_softc *gt = device_private(self);
123 struct pci_attach_args *pa = aux;
124 struct wsemuldisplaydev_attach_args a;
125 int console = (pa->pa_tag == gten_console_pcitag);
126 int error;
127 char devinfo[256], pbuf[10];
128
129 error = pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14,
130 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
131 >->gt_memaddr, >->gt_memsize, NULL);
132 if (error) {
133 aprint_error(": can't determine memory size: error=%d\n",
134 error);
135 return;
136 }
137 gt->gt_dev = self;
138 if (console) {
139 gt->gt_ri = >en_console_ri;
140 gt->gt_nscreens = 1;
141 } else {
142 gt->gt_ri = kmem_zalloc(sizeof(*gt->gt_ri),
143 KM_SLEEP);
144 #if 0
145 error = pci_mapreg_map(pa, 0x14,
146 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
147 BUS_SPACE_MAP_LINEAR, NULL,
148 (bus_space_handle_t *) >->gt_ri->ri_bits,
149 NULL, NULL);
150 #else
151 error = bus_space_map(pa->pa_memt, gt->gt_memaddr + GTEN_VRAM_OFFSET,
152 960*1024, BUS_SPACE_MAP_LINEAR,
153 (bus_space_handle_t *) >->gt_ri->ri_bits);
154 #endif
155 if (error) {
156 aprint_error(": can't map frame buffer: error=%d\n",
157 error);
158 return;
159 }
160
161 gten_common_init(gt->gt_ri);
162 }
163
164 gt->gt_paddr = vtophys((vaddr_t)gt->gt_ri->ri_bits);
165 if (gt->gt_paddr == 0) {
166 aprint_error(": cannot map framebuffer\n");
167 return;
168 }
169 gt->gt_psize = gt->gt_memsize - GTEN_VRAM_OFFSET;
170
171 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
172 aprint_normal(": %s\n", devinfo);
173 format_bytes(pbuf, sizeof(pbuf), gt->gt_psize);
174 aprint_normal_dev(self, "%s: %s, %dx%d, %dbpp\n", pbuf,
175 gt->gt_ri->ri_width, gt->gt_ri->ri_height,
176 gt->gt_ri->ri_depth);
177 #if defined(DEBUG)
178 aprint_debug_dev(self, "%s: text %dx%d, =+%d+%d\n",
179 gt->gt_ri->ri_cols, gt->gt_ri->ri_rows,
180 gt->gt_ri->ri_xorigin, gt->gt_ri->ri_yorigin);
181
182 { int i;
183 struct rasops_info *ri = gt->gt_ri;
184 for (i = 0; i < 64; i++) {
185 int j = i * ri->ri_stride;
186 int k = (ri->ri_height - i - 1) * gt->gt_ri->ri_stride;
187 memset(ri->ri_bits + j, 0, 64 - i);
188 memset(ri->ri_bits + j + 64 - i, 255, i);
189
190 memset(ri->ri_bits + j + ri->ri_width - 64 + i, 0, 64 - i);
191 memset(ri->ri_bits + j + ri->ri_width - 64, 255, i);
192
193 memset(ri->ri_bits + k, 0, 64 - i);
194 memset(ri->ri_bits + k + 64 - i, 255, i);
195
196 memset(ri->ri_bits + k + ri->ri_width - 64 + i, 0, 64 - i);
197 memset(ri->ri_bits + k + ri->ri_width - 64, 255, i);
198 }}
199 #endif
200
201 gt->gt_cmap_red[0] = gt->gt_cmap_green[0] = gt->gt_cmap_blue[0] = 0;
202 gt->gt_cmap_red[15] = gt->gt_cmap_red[255] = 0xff;
203 gt->gt_cmap_green[15] = gt->gt_cmap_green[255] = 0xff;
204 gt->gt_cmap_blue[15] = gt->gt_cmap_blue[255] = 0xff;
205
206 a.console = console;
207 a.scrdata = >en_screenlist;
208 a.accessops = >en_accessops;
209 a.accesscookie = gt;
210
211 config_found(self, &a, wsemuldisplaydevprint, CFARGS_NONE);
212 }
213
214 static void
gten_common_init(struct rasops_info * ri)215 gten_common_init(struct rasops_info *ri)
216 {
217 int32_t addr, width, height, linebytes, depth;
218 int i, screenbytes;
219
220 /* initialize rasops */
221 ri->ri_width = 640;
222 ri->ri_height = 480;
223 ri->ri_depth = 8;
224 ri->ri_stride = 640;
225 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
226
227 rasops_init(ri, 30, 80);
228 /* black on white */
229 ri->ri_devcmap[0] = 0xffffffff; /* bg */
230 ri->ri_devcmap[1] = 0; /* fg */
231
232 memset(ri->ri_bits, 0xff, ri->ri_stride * ri->ri_height);
233
234 gten_stdscreen.nrows = ri->ri_rows;
235 gten_stdscreen.ncols = ri->ri_cols;
236 gten_stdscreen.textops = &ri->ri_ops;
237 gten_stdscreen.capabilities = ri->ri_caps;
238 }
239
240 static int
gten_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct proc * p)241 gten_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
242 struct proc *p)
243 {
244 struct gten_softc *gt = v;
245 struct wsdisplay_fbinfo *wdf;
246 struct grfinfo *gm;
247
248 switch (cmd) {
249 case WSDISPLAYIO_GTYPE:
250 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
251 return 0;
252
253 case WSDISPLAYIO_GINFO:
254 wdf = (void *)data;
255 wdf->height = gt->gt_ri->ri_height;
256 wdf->width = gt->gt_ri->ri_width;
257 wdf->depth = gt->gt_ri->ri_depth;
258 wdf->cmsize = 256;
259 return 0;
260
261 case WSDISPLAYIO_GETCMAP:
262 return gten_getcmap(gt, (struct wsdisplay_cmap *)data);
263
264 case WSDISPLAYIO_PUTCMAP:
265 return gten_putcmap(gt, (struct wsdisplay_cmap *)data);
266 }
267 return EPASSTHROUGH;
268 }
269
270 static paddr_t
gten_mmap(void * v,void * vs,off_t offset,int prot)271 gten_mmap(void *v, void *vs, off_t offset, int prot)
272 {
273 struct gten_softc *gt = v;
274
275 if (offset >= 0 && offset < gt->gt_psize)
276 return gt->gt_paddr + offset;
277 if (offset >= 0x1000000 && offset < gt->gt_memsize)
278 return gt->gt_memaddr + offset - 0x1000000;
279
280 return -1;
281 }
282
283 static int
gten_alloc_screen(void * v,const struct wsscreen_descr * type,void ** cookiep,int * curxp,int * curyp,long * attrp)284 gten_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
285 int *curxp, int *curyp, long *attrp)
286 {
287 struct gten_softc *gt = v;
288 struct rasops_info *ri = gt->gt_ri;
289 long defattr;
290
291 if (gt->gt_nscreens > 0)
292 return (ENOMEM);
293
294 *cookiep = ri; /* one and only for now */
295 *curxp = 0;
296 *curyp = 0;
297 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
298 *attrp = defattr;
299 gt->gt_nscreens++;
300 return 0;
301 }
302
303 static void
gten_free_screen(void * v,void * cookie)304 gten_free_screen(void *v, void *cookie)
305 {
306 struct gten_softc *gt = v;
307
308 if (gt->gt_ri == >en_console_ri)
309 panic("gten_free_screen: console");
310
311 gt->gt_nscreens--;
312 }
313
314 static int
gten_show_screen(void * v,void * cookie,int waitok,void (* cb)(void *,int,int),void * cbarg)315 gten_show_screen(void *v, void *cookie, int waitok,
316 void (*cb)(void *, int, int), void *cbarg)
317 {
318 return (0);
319 }
320
321 int
gten_cnattach(pci_chipset_tag_t pc,bus_space_tag_t memt)322 gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt)
323 {
324 struct rasops_info *ri = >en_console_ri;
325 u_int32_t mapreg, id, mask, mapsize;
326 long defattr;
327 pcitag_t tag;
328 int s, error;
329 bus_size_t bussize;
330 bus_addr_t busaddr;
331
332 tag = pci_make_tag(pc, 0, 14, 0);
333
334 id = pci_conf_read(pc, tag, PCI_ID_REG);
335 if (PCI_VENDOR(id) != PCI_VENDOR_WD ||
336 PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C)
337 return ENXIO;
338
339 mapreg = pci_conf_read(pc, tag, 0x14);
340 if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM ||
341 PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT)
342 return ENXIO;
343
344 s = splhigh();
345 pci_conf_write(pc, tag, 0x14, 0xffffffff);
346 mask = pci_conf_read(pc, tag, 0x14);
347 pci_conf_write(pc, tag, 0x14, mapreg);
348 splx(s);
349 bussize = PCI_MAPREG_MEM_SIZE(mask);
350 busaddr = PCI_MAPREG_MEM_ADDR(mapreg);
351
352 error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024,
353 BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits);
354 if (error)
355 return error;
356
357 gten_common_init(ri);
358
359 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
360 wsdisplay_cnattach(>en_stdscreen, ri, 0, 0, defattr);
361
362 gten_console_pcitag = tag;
363
364 return 0;
365 }
366
367 static int
gten_getcmap(struct gten_softc * gt,struct wsdisplay_cmap * cm)368 gten_getcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm)
369 {
370 u_int index = cm->index;
371 u_int count = cm->count;
372 int error;
373
374 if (index >= 256 || count > 256 || index + count > 256)
375 return EINVAL;
376
377 error = copyout(>->gt_cmap_red[index], cm->red, count);
378 if (error)
379 return error;
380 error = copyout(>->gt_cmap_green[index], cm->green, count);
381 if (error)
382 return error;
383 error = copyout(>->gt_cmap_blue[index], cm->blue, count);
384 if (error)
385 return error;
386
387 return 0;
388 }
389
390 static int
gten_putcmap(struct gten_softc * gt,struct wsdisplay_cmap * cm)391 gten_putcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm)
392 {
393 int index = cm->index;
394 int count = cm->count;
395 int i, error;
396 u_char rbuf[256], gbuf[256], bbuf[256];
397
398 if (cm->index >= 256 || cm->count > 256 ||
399 (cm->index + cm->count) > 256)
400 return EINVAL;
401 error = copyin(cm->red, &rbuf[index], count);
402 if (error)
403 return error;
404 error = copyin(cm->green, &gbuf[index], count);
405 if (error)
406 return error;
407 error = copyin(cm->blue, &bbuf[index], count);
408 if (error)
409 return error;
410
411 memcpy(>->gt_cmap_red[index], &rbuf[index], count);
412 memcpy(>->gt_cmap_green[index], &gbuf[index], count);
413 memcpy(>->gt_cmap_blue[index], &bbuf[index], count);
414 return 0;
415 }
416