xref: /netbsd-src/sys/dev/ic/ibm561.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* $NetBSD: ibm561.c,v 1.11 2012/02/12 16:34:11 matt Exp $ */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roland C. Dowdeswell of Ponte, Inc.
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: ibm561.c,v 1.11 2012/02/12 16:34:11 matt Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/buf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 
42 #include <dev/pci/pcivar.h>
43 #include <dev/ic/ibm561reg.h>
44 #include <dev/ic/ibm561var.h>
45 #include <dev/ic/ramdac.h>
46 
47 #include <dev/wscons/wsconsio.h>
48 
49 /*
50  * Functions exported via the RAMDAC configuration table.
51  */
52 void	ibm561_init(struct ramdac_cookie *);
53 int	ibm561_set_cmap(struct ramdac_cookie *, struct wsdisplay_cmap *);
54 int	ibm561_get_cmap(struct ramdac_cookie *, struct wsdisplay_cmap *);
55 int	ibm561_set_cursor(struct ramdac_cookie *, struct wsdisplay_cursor *);
56 int	ibm561_get_cursor(struct ramdac_cookie *, struct wsdisplay_cursor *);
57 int	ibm561_set_curpos(struct ramdac_cookie *, struct wsdisplay_curpos *);
58 int	ibm561_get_curpos(struct ramdac_cookie *, struct wsdisplay_curpos *);
59 int	ibm561_get_curmax(struct ramdac_cookie *, struct wsdisplay_curpos *);
60 int	ibm561_set_dotclock(struct ramdac_cookie *, unsigned);
61 
62 /* XXX const */
63 struct ramdac_funcs ibm561_funcsstruct = {
64 	"IBM561",
65 	ibm561_register,
66 	ibm561_init,
67 	ibm561_set_cmap,
68 	ibm561_get_cmap,
69 	ibm561_set_cursor,
70 	ibm561_get_cursor,
71 	ibm561_set_curpos,
72 	ibm561_get_curpos,
73 	ibm561_get_curmax,
74 	NULL,			/* check_curcmap; not needed */
75 	NULL,			/* set_curcmap; not needed */
76 	NULL,			/* get_curcmap; not needed */
77 	ibm561_set_dotclock,
78 };
79 
80 /*
81  * Private data.
82  */
83 struct ibm561data {
84 	void            *cookie;
85 
86 	int             (*ramdac_sched_update)(void *, void (*)(void *));
87 	void            (*ramdac_wr)(void *, u_int, u_int8_t);
88 	u_int8_t        (*ramdac_rd)(void *, u_int);
89 
90 #define CHANGED_CURCMAP		0x0001	/* cursor cmap */
91 #define CHANGED_CMAP		0x0002	/* color map */
92 #define CHANGED_WTYPE		0x0004	/* window types */
93 #define CHANGED_DOTCLOCK	0x0008	/* dot clock */
94 #define CHANGED_ALL		0x000f	/* or of all above */
95 	u_int8_t	changed;
96 
97 	/* dotclock parameters */
98 	u_int8_t	vco_div;
99 	u_int8_t	pll_ref;
100 	u_int8_t	div_dotclock;
101 
102 	/* colormaps et al. */
103 	u_int8_t	curcmap_r[2];
104 	u_int8_t	curcmap_g[2];
105 	u_int8_t	curcmap_b[2];
106 
107 	u_int8_t	cmap_r[IBM561_NCMAP_ENTRIES];
108 	u_int8_t	cmap_g[IBM561_NCMAP_ENTRIES];
109 	u_int8_t	cmap_b[IBM561_NCMAP_ENTRIES];
110 
111 	u_int16_t	gamma_r[IBM561_NGAMMA_ENTRIES];
112 	u_int16_t	gamma_g[IBM561_NGAMMA_ENTRIES];
113 	u_int16_t	gamma_b[IBM561_NGAMMA_ENTRIES];
114 
115 	u_int16_t	wtype[IBM561_NWTYPES];
116 };
117 
118 /*
119  * private functions
120  */
121 void	ibm561_update(void *);
122 static void ibm561_load_cmap(struct ibm561data *);
123 static void ibm561_load_dotclock(struct ibm561data *);
124 static void ibm561_regbegin(struct ibm561data *, u_int16_t);
125 static void ibm561_regcont(struct ibm561data *, u_int16_t, u_int8_t);
126 static void ibm561_regcont10bit(struct ibm561data *, u_int16_t, u_int16_t);
127 static void ibm561_regwr(struct ibm561data *, u_int16_t, u_int8_t);
128 
129 struct ramdac_funcs *
130 ibm561_funcs(void)
131 {
132 	return &ibm561_funcsstruct;
133 }
134 
135 struct ramdac_cookie *
136 ibm561_register(
137 	void *v,
138 	int (*sched_update)(void *, void (*)(void *)),
139 	void (*wr)(void *, u_int, u_int8_t),
140 	u_int8_t (*rd)(void *, u_int))
141 {
142 	struct ibm561data *data;
143 
144 	data = malloc(sizeof *data, M_DEVBUF, M_WAITOK|M_ZERO);
145 	data->cookie = v;
146 	data->ramdac_sched_update = sched_update;
147 	data->ramdac_wr = wr;
148 	data->ramdac_rd = rd;
149 	return (struct ramdac_cookie *)data;
150 }
151 
152 /*
153  * This function exists solely to provide a means to init
154  * the RAMDAC without first registering.  It is useful for
155  * initializing the console early on.
156  */
157 
158 struct ibm561data *saved_console_data;
159 
160 void
161 ibm561_cninit(
162 	void *v,
163 	int (*sched_update)(void *, void (*)(void *)),
164 	void (*wr)(void *, u_int, u_int8_t),
165 	u_int8_t (*rd)(void *, u_int),
166 	u_int dotclock)
167 {
168 	struct ibm561data tmp, *data = &tmp;
169 	memset(data, 0x0, sizeof *data);
170 	data->cookie = v;
171 	data->ramdac_sched_update = sched_update;
172 	data->ramdac_wr = wr;
173 	data->ramdac_rd = rd;
174 	ibm561_set_dotclock((struct ramdac_cookie *)data, dotclock);
175 	saved_console_data = data;
176 	ibm561_init((struct ramdac_cookie *)data);
177 	saved_console_data = NULL;
178 }
179 
180 void
181 ibm561_init(struct ramdac_cookie *rc)
182 {
183 	struct	ibm561data *data = (struct ibm561data *)rc;
184 	int	i;
185 
186 	/* XXX this is _essential_ */
187 
188 	ibm561_load_dotclock(data);
189 
190 	/* XXXrcd: bunch of magic of which I have no current clue */
191 	ibm561_regwr(data, IBM561_CONFIG_REG1, 0x2a);
192 	ibm561_regwr(data, IBM561_CONFIG_REG3, 0x41);
193 	ibm561_regwr(data, IBM561_CONFIG_REG4, 0x20);
194 
195 	/* initialize the card a bit */
196 	ibm561_regwr(data, IBM561_SYNC_CNTL, 0x1);
197 	ibm561_regwr(data, IBM561_CONFIG_REG2, 0x19);
198 
199 	ibm561_regwr(data, IBM561_CONFIG_REG1, 0x2a);
200 	ibm561_regwr(data, IBM561_CONFIG_REG4, 0x20);
201 
202 	ibm561_regbegin(data, IBM561_WAT_SEG_REG);
203 	ibm561_regcont(data, IBM561_CMD, 0x00);
204 	ibm561_regcont(data, IBM561_CMD, 0x00);
205 	ibm561_regcont(data, IBM561_CMD, 0x00);
206 	ibm561_regcont(data, IBM561_CMD, 0x00);
207 	ibm561_regbegin(data, IBM561_CHROMAKEY0);
208 	ibm561_regcont(data, IBM561_CMD, 0x00);
209 	ibm561_regcont(data, IBM561_CMD, 0x00);
210 	ibm561_regcont(data, IBM561_CMD, 0x00);
211 	ibm561_regcont(data, IBM561_CMD, 0x00);
212 
213 	ibm561_regwr(data, IBM561_CURS_CNTL_REG, 0x00); /* XXX off? */
214 
215 	/* cursor `hot spot' registers */
216 	ibm561_regbegin(data, IBM561_HOTSPOT_REG);
217 	ibm561_regcont(data, IBM561_CMD, 0x00);
218 	ibm561_regcont(data, IBM561_CMD, 0x00);
219 	ibm561_regcont(data, IBM561_CMD, 0xff);
220 	ibm561_regcont(data, IBM561_CMD, 0x00);
221 	ibm561_regcont(data, IBM561_CMD, 0xff);
222 	ibm561_regcont(data, IBM561_CMD, 0x00);
223 
224 	/* VRAM Mask Registers (diagnostics) */
225 	ibm561_regbegin(data, IBM561_VRAM_MASK_REG);
226 	ibm561_regcont(data, IBM561_CMD, 0xff);
227 	ibm561_regcont(data, IBM561_CMD, 0xff);
228 	ibm561_regcont(data, IBM561_CMD, 0xff);
229 	ibm561_regcont(data, IBM561_CMD, 0xff);
230 	ibm561_regcont(data, IBM561_CMD, 0xff);
231 	ibm561_regcont(data, IBM561_CMD, 0xff);
232 	ibm561_regcont(data, IBM561_CMD, 0xff);
233 
234 	/* let's set up some decent default colour maps and gammas */
235 	for (i=0; i < IBM561_NCMAP_ENTRIES; i++)
236 		data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 0xff;
237 	data->cmap_r[0]   = data->cmap_g[0]   = data->cmap_b[0]   = 0x00;
238 	data->cmap_r[256] = data->cmap_g[256] = data->cmap_b[256] = 0x00;
239 	data->cmap_r[512] = data->cmap_g[512] = data->cmap_b[512] = 0x00;
240 	data->cmap_r[768] = data->cmap_g[768] = data->cmap_b[768] = 0x00;
241 
242 	data->gamma_r[0] = data->gamma_g[0] = data->gamma_b[0] = 0x00;
243 	for (i=0; i < IBM561_NGAMMA_ENTRIES; i++)
244 		data->gamma_r[i] = data->gamma_g[i] = data->gamma_b[i] = 0xff;
245 
246 	for (i=0; i < IBM561_NWTYPES; i++)
247 		data->wtype[i] = 0x0036;
248 	data->wtype[1] = 0x0028;
249 
250 	/* the last step: */
251 	data->changed = CHANGED_ALL;
252 	data->ramdac_sched_update(data->cookie, ibm561_update);
253 }
254 
255 int
256 ibm561_set_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
257 {
258 	struct ibm561data *data = (struct ibm561data *)rc;
259 	u_int count, index;
260 	uint8_t r[IBM561_NCMAP_ENTRIES];
261 	uint8_t g[IBM561_NCMAP_ENTRIES];
262 	uint8_t b[IBM561_NCMAP_ENTRIES];
263 	int s, error;
264 
265 	if (cmapp->index >= IBM561_NCMAP_ENTRIES ||
266 	    cmapp->count > IBM561_NCMAP_ENTRIES - cmapp->index)
267 		return (EINVAL);
268 
269 	index = cmapp->index;
270 	count = cmapp->count;
271 	error = copyin(cmapp->red, &r[index], count);
272 	if (error)
273 		return error;
274 	error = copyin(cmapp->green, &g[index], count);
275 	if (error)
276 		return error;
277 	error = copyin(cmapp->blue, &b[index], count);
278 	if (error)
279 		return error;
280 	s = spltty();
281 	memcpy(&data->cmap_r[index], &r[index], count);
282 	memcpy(&data->cmap_g[index], &g[index], count);
283 	memcpy(&data->cmap_b[index], &b[index], count);
284 	data->changed |= CHANGED_CMAP;
285 	data->ramdac_sched_update(data->cookie, ibm561_update);
286 	splx(s);
287 	return (0);
288 }
289 
290 int
291 ibm561_get_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
292 {
293 	struct ibm561data *data = (struct ibm561data *)rc;
294 	u_int count, index;
295 	int error;
296 
297 	if (cmapp->index >= IBM561_NCMAP_ENTRIES ||
298 	    cmapp->count > IBM561_NCMAP_ENTRIES - cmapp->index)
299 		return (EINVAL);
300 	count = cmapp->count;
301 	index = cmapp->index;
302 	error = copyout(&data->cmap_r[index], cmapp->red, count);
303 	if (error)
304 		return (error);
305 	error = copyout(&data->cmap_g[index], cmapp->green, count);
306 	if (error)
307 		return (error);
308 	error = copyout(&data->cmap_b[index], cmapp->blue, count);
309 	return (error);
310 }
311 
312 /*
313  * XXX:
314  *  I am leaving these functions returning EINVAL, as they are
315  *  not strictly necessary for the correct functioning of the
316  *  card and in fact are not used on the other TGA variants, except
317  *  they are exported via ioctl(2) to userland, which does not in
318  *  fact use them.
319  */
320 
321 int
322 ibm561_set_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
323 {
324 	return EINVAL;
325 }
326 
327 int
328 ibm561_get_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
329 {
330 	return EINVAL;
331 }
332 
333 int
334 ibm561_set_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
335 {
336 	return EINVAL;
337 }
338 
339 int
340 ibm561_get_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
341 {
342 	return EINVAL;
343 }
344 
345 int
346 ibm561_get_curmax(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
347 {
348 	return EINVAL;
349 }
350 
351 int
352 ibm561_set_dotclock(struct ramdac_cookie *rc, unsigned dotclock)
353 {
354 	struct ibm561data *data = (struct ibm561data *)rc;
355 
356 	/* XXXrcd:  a couple of these are a little hazy, vis a vis
357 	 *          check 175MHz and 202MHz, which are wrong...
358 	 */
359 	switch (dotclock) {
360 	case  25175000: data->vco_div = 0x3e; data->pll_ref = 0x09; break;
361 	case  31500000: data->vco_div = 0x17; data->pll_ref = 0x05; break;
362 	case  40000000: data->vco_div = 0x42; data->pll_ref = 0x06; break;
363 	case  50000000: data->vco_div = 0x45; data->pll_ref = 0x05; break;
364 	case  65000000: data->vco_div = 0xac; data->pll_ref = 0x0c; break;
365 	case  69000000: data->vco_div = 0xa9; data->pll_ref = 0x0b; break;
366 	case  74000000: data->vco_div = 0x9c; data->pll_ref = 0x09; break;
367 	case  75000000: data->vco_div = 0x93; data->pll_ref = 0x08; break;
368 	case 103994000: data->vco_div = 0x96; data->pll_ref = 0x06; break;
369 	case 108180000: data->vco_div = 0xb8; data->pll_ref = 0x08; break;
370 	case 110000000: data->vco_div = 0xba; data->pll_ref = 0x08; break;
371 	case 119840000: data->vco_div = 0x82; data->pll_ref = 0x04; break;
372 	case 130808000: data->vco_div = 0xc8; data->pll_ref = 0x08; break;
373 	case 135000000: data->vco_div = 0xc1; data->pll_ref = 0x07; break;
374 	case 175000000: data->vco_div = 0xe2; data->pll_ref = 0x07; break;
375 	case 202500000: data->vco_div = 0xe2; data->pll_ref = 0x07; break;
376 	default:
377 		return EINVAL;
378 	}
379 
380 	data->div_dotclock = 0xb0;
381 	data->changed |= CHANGED_DOTCLOCK;
382 	return 0;
383 }
384 
385 /*
386  * Internal Functions
387  */
388 
389 void
390 ibm561_update(void *vp)
391 {
392 	struct ibm561data *data = (struct ibm561data *)vp;
393 	int	i;
394 
395 	/* XXX see comment above ibm561_cninit() */
396 	if (!data)
397 		data = saved_console_data;
398 
399 	if (data->changed & CHANGED_WTYPE) {
400 		ibm561_regbegin(data, IBM561_FB_WINTYPE);
401 		for (i=0; i < IBM561_NWTYPES; i++)
402 			ibm561_regcont10bit(data, IBM561_CMD_FB_WAT, data->wtype[i]);
403 
404 		/* XXXrcd:  quick hack here for AUX FB table */
405 		ibm561_regbegin(data, IBM561_AUXFB_WINTYPE);
406 		for (i=0; i < IBM561_NWTYPES; i++)
407 			ibm561_regcont(data, IBM561_CMD, 0x04);
408 
409 		/* XXXrcd:  quick hack here for OL WAT table */
410 		ibm561_regbegin(data, IBM561_OL_WINTYPE);
411 		for (i=0; i < IBM561_NWTYPES; i++)
412 			ibm561_regcont10bit(data, IBM561_CMD_FB_WAT, 0x0231);
413 
414 		/* XXXrcd:  quick hack here for AUX OL WAT table */
415 		ibm561_regbegin(data, IBM561_AUXOL_WINTYPE);
416 		for (i=0; i < IBM561_NWTYPES; i++)
417 			ibm561_regcont(data, IBM561_CMD, 0x0c);
418 	}
419 
420 	if (data->changed & CHANGED_CMAP)
421 		ibm561_load_cmap(data);
422 
423 	/* XXX:  I am not sure in what situations it is safe to
424 	 *       change the dotclock---hope this is good.
425 	 */
426 	if (data->changed & CHANGED_DOTCLOCK)
427 		ibm561_load_dotclock(data);
428 }
429 
430 static void
431 ibm561_load_cmap(struct ibm561data *data)
432 {
433 	int	i;
434 
435 	ibm561_regbegin(data, IBM561_CMAP_TABLE);
436 	for (i=0; i < IBM561_NCMAP_ENTRIES; i++) {
437 		ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_r[i]);
438 		ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_g[i]);
439 		ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_b[i]);
440 	}
441 
442 	ibm561_regbegin(data, IBM561_RED_GAMMA_TABLE);
443 	for (i=0; i < 256; i++)
444 		ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_r[i]);
445 
446 	ibm561_regbegin(data, IBM561_GREEN_GAMMA_TABLE);
447 	for (i=1; i < 256; i++)
448 		ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_g[i]);
449 
450 	ibm561_regbegin(data, IBM561_BLUE_GAMMA_TABLE);
451 	for (i=1; i < 256; i++)
452 		ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_b[i]);
453 
454 }
455 
456 static void
457 ibm561_load_dotclock(struct ibm561data *data)
458 {
459 	/* XXX
460 	 * we should probably be more pro-active here, but it shouldn't
461 	 * actually happen...
462 	 */
463 	if (!data->vco_div || !data->pll_ref || ! data->div_dotclock) {
464 		panic("ibm561_load_dotclock: called uninitialized");
465 	}
466 
467 	ibm561_regwr(data, IBM561_PLL_VCO_DIV,  data->vco_div);
468 	ibm561_regwr(data, IBM561_PLL_REF_REG, data->pll_ref);
469 	ibm561_regwr(data, IBM561_DIV_DOTCLCK, data->div_dotclock);
470 }
471 
472 static void
473 ibm561_regcont10bit(struct ibm561data *data, u_int16_t reg, u_int16_t val)
474 {
475 	data->ramdac_wr(data->cookie, IBM561_CMD_GAMMA, (val >> 2) & 0xff);
476 	data->ramdac_wr(data->cookie, IBM561_CMD_GAMMA, (val & 0x3) << 6);
477 }
478 
479 static void
480 ibm561_regbegin(struct ibm561data *data, u_int16_t reg)
481 {
482 	data->ramdac_wr(data->cookie, IBM561_ADDR_LOW, reg & 0xff);
483 	data->ramdac_wr(data->cookie, IBM561_ADDR_HIGH, (reg >> 8) & 0xff);
484 }
485 
486 static void
487 ibm561_regcont(struct ibm561data *data, u_int16_t reg, u_int8_t val)
488 {
489 	data->ramdac_wr(data->cookie, reg, val);
490 }
491 
492 static void
493 ibm561_regwr(struct ibm561data *data, u_int16_t reg, u_int8_t val)
494 {
495 	ibm561_regbegin(data, reg);
496 	ibm561_regcont(data, IBM561_CMD, val);
497 }
498