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