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