xref: /netbsd-src/sys/arch/prep/pci/gten.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: gten.c,v 1.18 2008/12/17 20:51:32 cegger 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.18 2008/12/17 20:51:32 cegger 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/malloc.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 <machine/bus.h>
55 #include <machine/gtenvar.h>
56 
57 static	int	gten_match(struct device *, struct cfdata *, void *);
58 static	void	gten_attach(struct device *, struct device *, void *);
59 static	int	gten_print(void *, const char *);
60 
61 CFATTACH_DECL(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 	&gten_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
108 gten_match(struct device *parent, struct cfdata *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
120 gten_attach(struct device *parent, struct device *self, void *aux)
121 {
122 	struct gten_softc *gt = (struct gten_softc *)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->gt_memaddr, &gt->gt_memsize, NULL);
132 	if (error) {
133 		aprint_error(": can't determine memory size: error=%d\n",
134 			error);
135 		return;
136 	}
137 	if (console) {
138 		gt->gt_ri = &gten_console_ri;
139 		gt->gt_nscreens = 1;
140 	} else {
141 		gt->gt_ri = malloc(sizeof(*gt->gt_ri),
142 			M_DEVBUF, M_NOWAIT|M_ZERO);
143 		if (gt->gt_ri == NULL) {
144 			aprint_error(": can't alloc memory\n");
145 			return;
146 		}
147 #if 0
148 		error = pci_mapreg_map(pa, 0x14,
149 			PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
150 			BUS_SPACE_MAP_LINEAR, NULL,
151 			(bus_space_handle_t *) &gt->gt_ri->ri_bits,
152 			NULL, NULL);
153 #else
154 		error = bus_space_map(pa->pa_memt, gt->gt_memaddr + GTEN_VRAM_OFFSET,
155 			960*1024, BUS_SPACE_MAP_LINEAR,
156 			(bus_space_handle_t *) &gt->gt_ri->ri_bits);
157 #endif
158 		if (error) {
159 			aprint_error(": can't map frame buffer: error=%d\n",
160 			    error);
161 			return;
162 		}
163 
164 		gten_common_init(gt->gt_ri);
165 	}
166 
167 	gt->gt_paddr = vtophys((vaddr_t)gt->gt_ri->ri_bits);
168 	if (gt->gt_paddr == 0) {
169 		aprint_error(": cannot map framebuffer\n");
170 		return;
171 	}
172 	gt->gt_psize = gt->gt_memsize - GTEN_VRAM_OFFSET;
173 
174 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
175 	aprint_normal(": %s\n", devinfo);
176 	format_bytes(pbuf, sizeof(pbuf), gt->gt_psize);
177 	aprint_normal("%s: %s, %dx%d, %dbpp\n", self->dv_xname, pbuf,
178 	       gt->gt_ri->ri_width, gt->gt_ri->ri_height,
179 	       gt->gt_ri->ri_depth);
180 #if defined(DEBUG)
181 	aprint_debug("%s: text %dx%d, =+%d+%d\n", self->dv_xname,
182 	       gt->gt_ri->ri_cols, gt->gt_ri->ri_rows,
183 	       gt->gt_ri->ri_xorigin, gt->gt_ri->ri_yorigin);
184 
185 	{ int i;
186 	  struct rasops_info *ri = gt->gt_ri;
187 	for (i = 0; i < 64; i++) {
188 		int j = i * ri->ri_stride;
189 		int k = (ri->ri_height - i - 1) * gt->gt_ri->ri_stride;
190 		memset(ri->ri_bits + j, 0, 64 - i);
191 		memset(ri->ri_bits + j + 64 - i, 255, i);
192 
193 		memset(ri->ri_bits + j + ri->ri_width - 64 + i, 0, 64 - i);
194 		memset(ri->ri_bits + j + ri->ri_width - 64, 255, i);
195 
196 		memset(ri->ri_bits + k, 0, 64 - i);
197 		memset(ri->ri_bits + k + 64 - i, 255, i);
198 
199 		memset(ri->ri_bits + k + ri->ri_width - 64 + i, 0, 64 - i);
200 		memset(ri->ri_bits + k + ri->ri_width - 64, 255, i);
201 	}}
202 #endif
203 
204 	gt->gt_cmap_red[0] = gt->gt_cmap_green[0] = gt->gt_cmap_blue[0] = 0;
205 	gt->gt_cmap_red[15] = gt->gt_cmap_red[255] = 0xff;
206 	gt->gt_cmap_green[15] = gt->gt_cmap_green[255] = 0xff;
207 	gt->gt_cmap_blue[15] = gt->gt_cmap_blue[255] = 0xff;
208 
209 	a.console = console;
210 	a.scrdata = &gten_screenlist;
211 	a.accessops = &gten_accessops;
212 	a.accesscookie = gt;
213 
214 	config_found(self, &a, wsemuldisplaydevprint);
215 }
216 
217 static void
218 gten_common_init(struct rasops_info *ri)
219 {
220 	int32_t addr, width, height, linebytes, depth;
221 	int i, screenbytes;
222 
223 	/* initialize rasops */
224 	ri->ri_width = 640;
225 	ri->ri_height = 480;
226 	ri->ri_depth = 8;
227 	ri->ri_stride = 640;
228 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
229 
230 	rasops_init(ri, 30, 80);
231 	/* black on white */
232 	ri->ri_devcmap[0] = 0xffffffff;			/* bg */
233 	ri->ri_devcmap[1] = 0;				/* fg */
234 
235 	memset(ri->ri_bits, 0xff, ri->ri_stride * ri->ri_height);
236 
237 	gten_stdscreen.nrows = ri->ri_rows;
238 	gten_stdscreen.ncols = ri->ri_cols;
239 	gten_stdscreen.textops = &ri->ri_ops;
240 	gten_stdscreen.capabilities = ri->ri_caps;
241 }
242 
243 static int
244 gten_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
245     struct proc *p)
246 {
247 	struct gten_softc *gt = v;
248 	struct wsdisplay_fbinfo *wdf;
249 	struct grfinfo *gm;
250 
251 	switch (cmd) {
252 	case WSDISPLAYIO_GTYPE:
253 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
254 		return 0;
255 
256 	case WSDISPLAYIO_GINFO:
257 		wdf = (void *)data;
258 		wdf->height = gt->gt_ri->ri_height;
259 		wdf->width = gt->gt_ri->ri_width;
260 		wdf->depth = gt->gt_ri->ri_depth;
261 		wdf->cmsize = 256;
262 		return 0;
263 
264 	case WSDISPLAYIO_GETCMAP:
265 		return gten_getcmap(gt, (struct wsdisplay_cmap *)data);
266 
267 	case WSDISPLAYIO_PUTCMAP:
268 		return gten_putcmap(gt, (struct wsdisplay_cmap *)data);
269 	}
270 	return EPASSTHROUGH;
271 }
272 
273 static paddr_t
274 gten_mmap(void *v, void *vs, off_t offset, int prot)
275 {
276 	struct gten_softc *gt = v;
277 
278 	if (offset >= 0 && offset < gt->gt_psize)
279 		return gt->gt_paddr + offset;
280 	if (offset >= 0x1000000 && offset < gt->gt_memsize)
281 		return gt->gt_memaddr + offset - 0x1000000;
282 
283 	return -1;
284 }
285 
286 static int
287 gten_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
288     int *curxp, int *curyp, long *attrp)
289 {
290 	struct gten_softc *gt = v;
291 	struct rasops_info *ri = gt->gt_ri;
292 	long defattr;
293 
294 	if (gt->gt_nscreens > 0)
295 		return (ENOMEM);
296 
297 	*cookiep = ri;			/* one and only for now */
298 	*curxp = 0;
299 	*curyp = 0;
300 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
301 	*attrp = defattr;
302 	gt->gt_nscreens++;
303 	return 0;
304 }
305 
306 static void
307 gten_free_screen(void *v, void *cookie)
308 {
309 	struct gten_softc *gt = v;
310 
311 	if (gt->gt_ri == &gten_console_ri)
312 		panic("gten_free_screen: console");
313 
314 	gt->gt_nscreens--;
315 }
316 
317 static int
318 gten_show_screen(void *v, void *cookie, int waitok,
319     void (*cb)(void *, int, int), void *cbarg)
320 {
321 	return (0);
322 }
323 
324 int
325 gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt)
326 {
327 	struct rasops_info *ri = &gten_console_ri;
328 	u_int32_t mapreg, id, mask, mapsize;
329 	long defattr;
330 	pcitag_t tag;
331 	int s, error;
332 	bus_size_t bussize;
333 	bus_addr_t busaddr;
334 
335 	tag = pci_make_tag(pc, 0, 14, 0);
336 
337 	id = pci_conf_read(pc, tag, PCI_ID_REG);
338 	if (PCI_VENDOR(id) != PCI_VENDOR_WD ||
339 	    PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C)
340 		return ENXIO;
341 
342 	mapreg = pci_conf_read(pc, tag, 0x14);
343 	if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM ||
344 	    PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT)
345 		return ENXIO;
346 
347         s = splhigh();
348         pci_conf_write(pc, tag, 0x14, 0xffffffff);
349         mask = pci_conf_read(pc, tag, 0x14);
350         pci_conf_write(pc, tag, 0x14, mapreg);
351 	splx(s);
352 	bussize = PCI_MAPREG_MEM_SIZE(mask);
353 	busaddr = PCI_MAPREG_MEM_ADDR(mapreg);
354 
355 	error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024,
356 		BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits);
357 	if (error)
358 		return error;
359 
360 	gten_common_init(ri);
361 
362 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
363 	wsdisplay_cnattach(&gten_stdscreen, ri, 0, 0, defattr);
364 
365 	gten_console_pcitag = tag;
366 
367 	return 0;
368 }
369 
370 static int
371 gten_getcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm)
372 {
373 	u_int index = cm->index;
374 	u_int count = cm->count;
375 	int error;
376 
377 	if (index >= 256 || count > 256 || index + count > 256)
378 		return EINVAL;
379 
380 	error = copyout(&gt->gt_cmap_red[index],   cm->red,   count);
381 	if (error)
382 		return error;
383 	error = copyout(&gt->gt_cmap_green[index], cm->green, count);
384 	if (error)
385 		return error;
386 	error = copyout(&gt->gt_cmap_blue[index],  cm->blue,  count);
387 	if (error)
388 		return error;
389 
390 	return 0;
391 }
392 
393 static int
394 gten_putcmap(struct gten_softc *gt, struct wsdisplay_cmap *cm)
395 {
396 	int index = cm->index;
397 	int count = cm->count;
398 	int i, error;
399 	u_char rbuf[256], gbuf[256], bbuf[256];
400 
401 	if (cm->index >= 256 || cm->count > 256 ||
402 	    (cm->index + cm->count) > 256)
403 		return EINVAL;
404 	error = copyin(cm->red, &rbuf[index], count);
405 	if (error)
406 		return error;
407 	error = copyin(cm->green, &gbuf[index], count);
408 	if (error)
409 		return error;
410 	error = copyin(cm->blue, &bbuf[index], count);
411 	if (error)
412 		return error;
413 
414 	memcpy(&gt->gt_cmap_red[index], &rbuf[index], count);
415 	memcpy(&gt->gt_cmap_green[index], &gbuf[index], count);
416 	memcpy(&gt->gt_cmap_blue[index], &bbuf[index], count);
417 	return 0;
418 }
419