1 /* $NetBSD: ibm561.c,v 1.14 2020/12/04 00:38:08 thorpej 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.14 2020/12/04 00:38:08 thorpej 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/kmem.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 *
ibm561_funcs(void)130 ibm561_funcs(void)
131 {
132           return &ibm561_funcsstruct;
133 }
134 
135 struct ramdac_cookie *
ibm561_register(void * v,int (* sched_update)(void *,void (*)(void *)),void (* wr)(void *,u_int,u_int8_t),u_int8_t (* rd)(void *,u_int))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 = kmem_zalloc(sizeof *data, KM_SLEEP);
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 static struct ibm561data saved_console_data;
159 
160 void
ibm561_cninit(void * v,int (* sched_update)(void *,void (*)(void *)),void (* wr)(void *,u_int,u_int8_t),u_int8_t (* rd)(void *,u_int),u_int dotclock)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 *data = &saved_console_data;
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           ibm561_init((struct ramdac_cookie *)data);
176 }
177 
178 void
ibm561_init(struct ramdac_cookie * rc)179 ibm561_init(struct ramdac_cookie *rc)
180 {
181           struct    ibm561data *data = (struct ibm561data *)rc;
182           int       i;
183 
184           /* XXX this is _essential_ */
185 
186           ibm561_load_dotclock(data);
187 
188           /* XXXrcd: bunch of magic of which I have no current clue */
189           ibm561_regwr(data, IBM561_CONFIG_REG1, 0x2a);
190           ibm561_regwr(data, IBM561_CONFIG_REG3, 0x41);
191           ibm561_regwr(data, IBM561_CONFIG_REG4, 0x20);
192 
193           /* initialize the card a bit */
194           ibm561_regwr(data, IBM561_SYNC_CNTL, 0x1);
195           ibm561_regwr(data, IBM561_CONFIG_REG2, 0x19);
196 
197           ibm561_regwr(data, IBM561_CONFIG_REG1, 0x2a);
198           ibm561_regwr(data, IBM561_CONFIG_REG4, 0x20);
199 
200           ibm561_regbegin(data, IBM561_WAT_SEG_REG);
201           ibm561_regcont(data, IBM561_CMD, 0x00);
202           ibm561_regcont(data, IBM561_CMD, 0x00);
203           ibm561_regcont(data, IBM561_CMD, 0x00);
204           ibm561_regcont(data, IBM561_CMD, 0x00);
205           ibm561_regbegin(data, IBM561_CHROMAKEY0);
206           ibm561_regcont(data, IBM561_CMD, 0x00);
207           ibm561_regcont(data, IBM561_CMD, 0x00);
208           ibm561_regcont(data, IBM561_CMD, 0x00);
209           ibm561_regcont(data, IBM561_CMD, 0x00);
210 
211           ibm561_regwr(data, IBM561_CURS_CNTL_REG, 0x00); /* XXX off? */
212 
213           /* cursor `hot spot' registers */
214           ibm561_regbegin(data, IBM561_HOTSPOT_REG);
215           ibm561_regcont(data, IBM561_CMD, 0x00);
216           ibm561_regcont(data, IBM561_CMD, 0x00);
217           ibm561_regcont(data, IBM561_CMD, 0xff);
218           ibm561_regcont(data, IBM561_CMD, 0x00);
219           ibm561_regcont(data, IBM561_CMD, 0xff);
220           ibm561_regcont(data, IBM561_CMD, 0x00);
221 
222           /* VRAM Mask Registers (diagnostics) */
223           ibm561_regbegin(data, IBM561_VRAM_MASK_REG);
224           ibm561_regcont(data, IBM561_CMD, 0xff);
225           ibm561_regcont(data, IBM561_CMD, 0xff);
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 
232           /* let's set up some decent default colour maps and gammas */
233           for (i=0; i < IBM561_NCMAP_ENTRIES; i++)
234                     data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 0xff;
235           data->cmap_r[0]   = data->cmap_g[0]   = data->cmap_b[0]   = 0x00;
236           data->cmap_r[256] = data->cmap_g[256] = data->cmap_b[256] = 0x00;
237           data->cmap_r[512] = data->cmap_g[512] = data->cmap_b[512] = 0x00;
238           data->cmap_r[768] = data->cmap_g[768] = data->cmap_b[768] = 0x00;
239 
240           data->gamma_r[0] = data->gamma_g[0] = data->gamma_b[0] = 0x00;
241           for (i=0; i < IBM561_NGAMMA_ENTRIES; i++)
242                     data->gamma_r[i] = data->gamma_g[i] = data->gamma_b[i] = 0xff;
243 
244           for (i=0; i < IBM561_NWTYPES; i++)
245                     data->wtype[i] = 0x0036;
246           data->wtype[1] = 0x0028;
247 
248           /* the last step: */
249           data->changed = CHANGED_ALL;
250           data->ramdac_sched_update(data->cookie, ibm561_update);
251 }
252 
253 int
ibm561_set_cmap(struct ramdac_cookie * rc,struct wsdisplay_cmap * cmapp)254 ibm561_set_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
255 {
256           struct ibm561data *data = (struct ibm561data *)rc;
257           u_int count, index;
258           uint8_t *cmap_entries, *r, *g, *b;
259           int s, error;
260 
261           if (cmapp->index >= IBM561_NCMAP_ENTRIES ||
262               cmapp->count > IBM561_NCMAP_ENTRIES - cmapp->index)
263                     return (EINVAL);
264 
265           cmap_entries = kmem_alloc(IBM561_NCMAP_ENTRIES * 3, KM_SLEEP);
266           r = &cmap_entries[0 * IBM561_NCMAP_ENTRIES];
267           g = &cmap_entries[1 * IBM561_NCMAP_ENTRIES];
268           b = &cmap_entries[2 * IBM561_NCMAP_ENTRIES];
269 
270           index = cmapp->index;
271           count = cmapp->count;
272           error = copyin(cmapp->red, &r[index], count);
273           if (error)
274                     goto out;
275           error = copyin(cmapp->green, &g[index], count);
276           if (error)
277                     goto out;
278           error = copyin(cmapp->blue, &b[index], count);
279           if (error)
280                     goto out;
281 
282           s = spltty();
283           memcpy(&data->cmap_r[index], &r[index], count);
284           memcpy(&data->cmap_g[index], &g[index], count);
285           memcpy(&data->cmap_b[index], &b[index], count);
286           data->changed |= CHANGED_CMAP;
287           data->ramdac_sched_update(data->cookie, ibm561_update);
288           splx(s);
289 
290  out:
291           kmem_free(cmap_entries, IBM561_NCMAP_ENTRIES * 3);
292           return (error);
293 }
294 
295 int
ibm561_get_cmap(struct ramdac_cookie * rc,struct wsdisplay_cmap * cmapp)296 ibm561_get_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
297 {
298           struct ibm561data *data = (struct ibm561data *)rc;
299           u_int count, index;
300           int error;
301 
302           if (cmapp->index >= IBM561_NCMAP_ENTRIES ||
303               cmapp->count > IBM561_NCMAP_ENTRIES - cmapp->index)
304                     return (EINVAL);
305           count = cmapp->count;
306           index = cmapp->index;
307           error = copyout(&data->cmap_r[index], cmapp->red, count);
308           if (error)
309                     return (error);
310           error = copyout(&data->cmap_g[index], cmapp->green, count);
311           if (error)
312                     return (error);
313           error = copyout(&data->cmap_b[index], cmapp->blue, count);
314           return (error);
315 }
316 
317 /*
318  * XXX:
319  *  I am leaving these functions returning EINVAL, as they are
320  *  not strictly necessary for the correct functioning of the
321  *  card and in fact are not used on the other TGA variants, except
322  *  they are exported via ioctl(2) to userland, which does not in
323  *  fact use them.
324  */
325 
326 int
ibm561_set_cursor(struct ramdac_cookie * rc,struct wsdisplay_cursor * cursorp)327 ibm561_set_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
328 {
329           return EINVAL;
330 }
331 
332 int
ibm561_get_cursor(struct ramdac_cookie * rc,struct wsdisplay_cursor * cursorp)333 ibm561_get_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
334 {
335           return EINVAL;
336 }
337 
338 int
ibm561_set_curpos(struct ramdac_cookie * rc,struct wsdisplay_curpos * curposp)339 ibm561_set_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
340 {
341           return EINVAL;
342 }
343 
344 int
ibm561_get_curpos(struct ramdac_cookie * rc,struct wsdisplay_curpos * curposp)345 ibm561_get_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
346 {
347           return EINVAL;
348 }
349 
350 int
ibm561_get_curmax(struct ramdac_cookie * rc,struct wsdisplay_curpos * curposp)351 ibm561_get_curmax(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
352 {
353           return EINVAL;
354 }
355 
356 int
ibm561_set_dotclock(struct ramdac_cookie * rc,unsigned dotclock)357 ibm561_set_dotclock(struct ramdac_cookie *rc, unsigned dotclock)
358 {
359           struct ibm561data *data = (struct ibm561data *)rc;
360 
361           /* XXXrcd:  a couple of these are a little hazy, vis a vis
362            *          check 175MHz and 202MHz, which are wrong...
363            */
364           switch (dotclock) {
365           case  25175000: data->vco_div = 0x3e; data->pll_ref = 0x09; break;
366           case  31500000: data->vco_div = 0x17; data->pll_ref = 0x05; break;
367           case  40000000: data->vco_div = 0x42; data->pll_ref = 0x06; break;
368           case  50000000: data->vco_div = 0x45; data->pll_ref = 0x05; break;
369           case  65000000: data->vco_div = 0xac; data->pll_ref = 0x0c; break;
370           case  69000000: data->vco_div = 0xa9; data->pll_ref = 0x0b; break;
371           case  74000000: data->vco_div = 0x9c; data->pll_ref = 0x09; break;
372           case  75000000: data->vco_div = 0x93; data->pll_ref = 0x08; break;
373           case 103994000: data->vco_div = 0x96; data->pll_ref = 0x06; break;
374           case 108180000: data->vco_div = 0xb8; data->pll_ref = 0x08; break;
375           case 110000000: data->vco_div = 0xba; data->pll_ref = 0x08; break;
376           case 119840000: data->vco_div = 0x82; data->pll_ref = 0x04; break;
377           case 130808000: data->vco_div = 0xc8; data->pll_ref = 0x08; break;
378           case 135000000: data->vco_div = 0xc1; data->pll_ref = 0x07; break;
379           case 175000000: data->vco_div = 0xe2; data->pll_ref = 0x07; break;
380           case 202500000: data->vco_div = 0xe2; data->pll_ref = 0x07; break;
381           default:
382                     return EINVAL;
383           }
384 
385           data->div_dotclock = 0xb0;
386           data->changed |= CHANGED_DOTCLOCK;
387           return 0;
388 }
389 
390 /*
391  * Internal Functions
392  */
393 
394 void
ibm561_update(void * vp)395 ibm561_update(void *vp)
396 {
397           struct ibm561data *data = (struct ibm561data *)vp;
398           int       i;
399 
400           /* XXX see comment above ibm561_cninit() */
401           if (!data)
402                     data = &saved_console_data;
403 
404           if (data->changed & CHANGED_WTYPE) {
405                     ibm561_regbegin(data, IBM561_FB_WINTYPE);
406                     for (i=0; i < IBM561_NWTYPES; i++)
407                               ibm561_regcont10bit(data, IBM561_CMD_FB_WAT, data->wtype[i]);
408 
409                     /* XXXrcd:  quick hack here for AUX FB table */
410                     ibm561_regbegin(data, IBM561_AUXFB_WINTYPE);
411                     for (i=0; i < IBM561_NWTYPES; i++)
412                               ibm561_regcont(data, IBM561_CMD, 0x04);
413 
414                     /* XXXrcd:  quick hack here for OL WAT table */
415                     ibm561_regbegin(data, IBM561_OL_WINTYPE);
416                     for (i=0; i < IBM561_NWTYPES; i++)
417                               ibm561_regcont10bit(data, IBM561_CMD_FB_WAT, 0x0231);
418 
419                     /* XXXrcd:  quick hack here for AUX OL WAT table */
420                     ibm561_regbegin(data, IBM561_AUXOL_WINTYPE);
421                     for (i=0; i < IBM561_NWTYPES; i++)
422                               ibm561_regcont(data, IBM561_CMD, 0x0c);
423           }
424 
425           if (data->changed & CHANGED_CMAP)
426                     ibm561_load_cmap(data);
427 
428           /* XXX:  I am not sure in what situations it is safe to
429            *       change the dotclock---hope this is good.
430            */
431           if (data->changed & CHANGED_DOTCLOCK)
432                     ibm561_load_dotclock(data);
433 }
434 
435 static void
ibm561_load_cmap(struct ibm561data * data)436 ibm561_load_cmap(struct ibm561data *data)
437 {
438           int       i;
439 
440           ibm561_regbegin(data, IBM561_CMAP_TABLE);
441           for (i=0; i < IBM561_NCMAP_ENTRIES; i++) {
442                     ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_r[i]);
443                     ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_g[i]);
444                     ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_b[i]);
445           }
446 
447           ibm561_regbegin(data, IBM561_RED_GAMMA_TABLE);
448           for (i=0; i < 256; i++)
449                     ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_r[i]);
450 
451           ibm561_regbegin(data, IBM561_GREEN_GAMMA_TABLE);
452           for (i=1; i < 256; i++)
453                     ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_g[i]);
454 
455           ibm561_regbegin(data, IBM561_BLUE_GAMMA_TABLE);
456           for (i=1; i < 256; i++)
457                     ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_b[i]);
458 
459 }
460 
461 static void
ibm561_load_dotclock(struct ibm561data * data)462 ibm561_load_dotclock(struct ibm561data *data)
463 {
464           /* XXX
465            * we should probably be more pro-active here, but it shouldn't
466            * actually happen...
467            */
468           if (!data->vco_div || !data->pll_ref || ! data->div_dotclock) {
469                     panic("ibm561_load_dotclock: called uninitialized");
470           }
471 
472           ibm561_regwr(data, IBM561_PLL_VCO_DIV,  data->vco_div);
473           ibm561_regwr(data, IBM561_PLL_REF_REG, data->pll_ref);
474           ibm561_regwr(data, IBM561_DIV_DOTCLCK, data->div_dotclock);
475 }
476 
477 static void
ibm561_regcont10bit(struct ibm561data * data,u_int16_t reg,u_int16_t val)478 ibm561_regcont10bit(struct ibm561data *data, u_int16_t reg, u_int16_t val)
479 {
480           data->ramdac_wr(data->cookie, IBM561_CMD_GAMMA, (val >> 2) & 0xff);
481           data->ramdac_wr(data->cookie, IBM561_CMD_GAMMA, (val & 0x3) << 6);
482 }
483 
484 static void
ibm561_regbegin(struct ibm561data * data,u_int16_t reg)485 ibm561_regbegin(struct ibm561data *data, u_int16_t reg)
486 {
487           data->ramdac_wr(data->cookie, IBM561_ADDR_LOW, reg & 0xff);
488           data->ramdac_wr(data->cookie, IBM561_ADDR_HIGH, (reg >> 8) & 0xff);
489 }
490 
491 static void
ibm561_regcont(struct ibm561data * data,u_int16_t reg,u_int8_t val)492 ibm561_regcont(struct ibm561data *data, u_int16_t reg, u_int8_t val)
493 {
494           data->ramdac_wr(data->cookie, reg, val);
495 }
496 
497 static void
ibm561_regwr(struct ibm561data * data,u_int16_t reg,u_int8_t val)498 ibm561_regwr(struct ibm561data *data, u_int16_t reg, u_int8_t val)
499 {
500           ibm561_regbegin(data, reg);
501           ibm561_regcont(data, IBM561_CMD, val);
502 }
503