1 /*      $NetBSD: wmlcd.c,v 1.3 2021/08/07 16:18:48 thorpej Exp $      */
2 /*
3  * Copyright (c) 2013 KIYOHARA Takashi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: wmlcd.c,v 1.3 2021/08/07 16:18:48 thorpej Exp $");
29 
30 #include "rnd.h"
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/errno.h>
36 
37 #include <uvm/uvm_extern.h>
38 
39 #include <epoc32/windermere/windermerereg.h>
40 #include <epoc32/windermere/windermerevar.h>
41 
42 #include <dev/cons.h>
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/rasops/rasops.h>
46 
47 #include "locators.h"
48 
49 #define LCD_SIZE    0x100
50 
51 static int is_console;
52 struct wmlcd_softc {
53           device_t sc_dev;
54 
55           bus_space_tag_t sc_iot;
56           bus_space_handle_t sc_ioh;
57 
58           vaddr_t sc_buffer;
59 
60           struct rasops_info sc_ri;
61 };
62 
63 static int wmlcd_match(device_t, cfdata_t, void *);
64 static void wmlcd_attach(device_t, device_t, void *);
65 
66 /* wsdisplay functions */
67 static int wmlcd_ioctl(void *, void *, u_long, void *, int, struct lwp *);
68 static paddr_t wmlcd_mmap(void *, void *, off_t, int);
69 static int wmlcd_alloc_screen(void *, const struct wsscreen_descr *, void **,
70                                     int *, int *, long *);
71 static void wmlcd_free_screen(void *, void *);
72 static int wmlcd_show_screen(void *, void *, int,
73                                    void (*)(void *, int, int), void *);
74 
75 CFATTACH_DECL_NEW(wmlcd, sizeof(struct wmlcd_softc),
76     wmlcd_match, wmlcd_attach, NULL, NULL);
77 
78 
79 #define WMLCD_DEFAULT_DEPTH   4
80 /* Linux like palette data */
81 const static char palette_2bpp[] = {
82           0x00, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x0f, 0x00,
83           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 };
87 const static char palette_4bpp[] = {
88           0x00, 0x00, 0x01, 0x00, 0x06, 0x00, 0x07, 0x00,
89           0x03, 0x00, 0x04, 0x00, 0x06, 0x00, 0x0a, 0x00,
90           0x05, 0x00, 0x06, 0x00, 0x0b, 0x00, 0x0c, 0x00,
91           0x08, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x0f, 0x00,
92 };
93 
94 static struct wsscreen_descr wmlcd_descr = {
95           .name = "wmlcd",
96           .fontwidth = 8,
97           .fontheight = 16,
98           .capabilities = WSSCREEN_WSCOLORS,
99 };
100 static const struct wsscreen_descr *wmlcd_descrs[] = {
101           &wmlcd_descr
102 };
103 
104 static const struct wsscreen_list wmlcd_screen_list = {
105           .nscreens = __arraycount(wmlcd_descrs),
106           .screens = wmlcd_descrs,
107 };
108 
109 struct wsdisplay_accessops wmlcd_accessops = {
110           wmlcd_ioctl,
111           wmlcd_mmap,
112           wmlcd_alloc_screen,
113           wmlcd_free_screen,
114           wmlcd_show_screen,
115           NULL,
116           NULL,
117           NULL,
118 };
119 
120 /* ARGSUSED */
121 static int
wmlcd_match(device_t parent,cfdata_t match,void * aux)122 wmlcd_match(device_t parent, cfdata_t match, void *aux)
123 {
124           struct windermere_attach_args *aa = aux;
125 
126           /* Wildcard not accept */
127           if (aa->aa_offset == WINDERMERECF_OFFSET_DEFAULT)
128                     return 0;
129 
130           aa->aa_size = LCD_SIZE;
131           return 1;
132 }
133 
134 /* ARGSUSED */
135 static void
wmlcd_attach(device_t parent,device_t self,void * aux)136 wmlcd_attach(device_t parent, device_t self, void *aux)
137 {
138           struct wmlcd_softc *sc = device_private(self);
139           struct windermere_attach_args *aa = aux;
140           struct wsemuldisplaydev_attach_args waa;
141           prop_dictionary_t dict = device_properties(self);
142           paddr_t paddr;
143           struct rasops_info *ri;
144           uint32_t lcdctl, width, height, addr, depth;
145           int c, i;
146           const char *palette;
147 
148           aprint_naive("\n");
149           aprint_normal("\n");
150 
151           sc->sc_dev = self;
152           if (windermere_bus_space_subregion(aa->aa_iot, *aa->aa_ioh,
153                                         aa->aa_offset, aa->aa_size, &sc->sc_ioh) != 0) {
154                     aprint_error_dev(self, "can't map registers\n");
155                     return;
156           }
157           sc->sc_iot = aa->aa_iot;
158 
159           if (!prop_dictionary_get_uint32(dict, "width", &width) ||
160               !prop_dictionary_get_uint32(dict, "height", &height) ||
161               !prop_dictionary_get_uint32(dict, "addr", &addr)) {
162                     aprint_error_dev(self, "can't get properties\n");
163                     return;
164           }
165           sc->sc_buffer = addr;
166           pmap_extract(pmap_kernel(), addr, &paddr);
167 
168           /* Setup palette data */
169           depth = WMLCD_DEFAULT_DEPTH;
170           if (depth == 2)
171                     palette = palette_2bpp;
172           else
173                     palette = palette_4bpp;
174           for (i = 0; i < LCD_PALETTE_SIZE; i++)
175                     *((uint8_t *)addr + i) = palette[i];
176           *(uint16_t *)addr |= (depth >> 1) << 12;
177 
178           lcdctl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LCDCTL);
179           bus_space_write_4(sc->sc_iot, sc->sc_ioh, LCDCTL, lcdctl & ~LCDCTL_EN);
180 
181           bus_space_write_4(sc->sc_iot, sc->sc_ioh, LCDDBAR1, paddr);
182 
183           lcdctl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LCDCTL);
184           bus_space_write_4(sc->sc_iot, sc->sc_ioh, LCDCTL,
185               lcdctl | LCDCTL_EN | LCDCTL_BW);
186 
187           aprint_normal_dev(self,
188               ": %dx%d pixels, %d bpp mono\n", width, height, 1 << depth);
189 
190           ri = &sc->sc_ri;
191           ri->ri_depth = depth;
192           ri->ri_bits = (void *)(addr + LCD_PALETTE_SIZE);
193           ri->ri_width = width;
194           ri->ri_height = height;
195           ri->ri_stride = width * ri->ri_depth / 8/*bits*/;
196           ri->ri_flg = RI_FORCEMONO | RI_CLEAR | RI_CENTER;
197 
198           if (is_console) {
199                     long defattr;
200 
201                     if (rasops_init(ri, 0, 0) < 0)
202                               panic("rasops_init failed");
203 
204                     if (ri->ri_depth == 4) {
205                               /* XXXXX: Create color map. */
206                               ri->ri_devcmap[0] = 0;
207                               for (i = 1; i < 15; i++) {
208                                         c = (i + 0xc) & 0xf;
209                                         ri->ri_devcmap[i] =
210                                             c | (c << 8) | (c << 16) | (c << 24);
211                               }
212                     }
213                     ri->ri_devcmap[15] = -1;
214 
215                     wmlcd_descr.ncols = ri->ri_cols;
216                     wmlcd_descr.nrows = ri->ri_rows;
217                     wmlcd_descr.textops = &ri->ri_ops;
218                     wmlcd_descr.capabilities = ri->ri_caps;
219 
220                     if ((ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr) != 0)
221                               panic("allocattr failed");
222                     wsdisplay_cnattach(&wmlcd_descr, ri, ri->ri_ccol, ri->ri_crow,
223                         defattr);
224           }
225 
226           waa.console = is_console;
227           waa.scrdata = &wmlcd_screen_list;
228           waa.accessops = &wmlcd_accessops;
229           waa.accesscookie = sc;
230 
231           config_found(self, &waa, wsemuldisplaydevprint, CFARGS_NONE);
232 }
233 
234 static int
wmlcd_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)235 wmlcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
236 {
237           struct wmlcd_softc *sc = v;
238           struct wsdisplay_fbinfo *wsdisp_info;
239 
240           switch (cmd) {
241           case WSDISPLAYIO_GTYPE:
242                     *(int *)data = WSDISPLAY_TYPE_WINDERMERE;
243                     return 0;
244 
245           case WSDISPLAYIO_GINFO:
246                     wsdisp_info = (struct wsdisplay_fbinfo *)data;
247                     wsdisp_info->height = sc->sc_ri.ri_height;
248                     wsdisp_info->width = sc->sc_ri.ri_width;
249                     wsdisp_info->depth = sc->sc_ri.ri_depth;
250                     wsdisp_info->cmsize = 0;
251                     return 0;
252 
253           case WSDISPLAYIO_GVIDEO:
254                     if (1)    /* XXXX */
255                               *(int *)data = WSDISPLAYIO_VIDEO_ON;
256                     else
257                               *(int *)data = WSDISPLAYIO_VIDEO_OFF;
258                     return 0;
259 
260           case WSDISPLAYIO_SVIDEO:
261                     if (*(int *)data == WSDISPLAYIO_VIDEO_ON) {
262                               /* XXXX: turn on */
263                     } else {
264                               /* XXXX: turn off */
265                     }
266                     return 0;
267 
268           case WSDISPLAYIO_LINEBYTES:
269                     *(int *)data = sc->sc_ri.ri_stride;
270                     return 0;
271           }
272 
273           return EPASSTHROUGH;
274 }
275 
276 static paddr_t
wmlcd_mmap(void * v,void * vs,off_t off,int prot)277 wmlcd_mmap(void *v, void *vs, off_t off, int prot)
278 {
279           struct wmlcd_softc *sc = v;
280 
281           if (off < 0 || sc->sc_ri.ri_stride * sc->sc_ri.ri_height <= off)
282                     return -1;
283 
284           return (paddr_t)sc->sc_ri.ri_bits + off;
285 }
286 
287 static int
wmlcd_alloc_screen(void * v,const struct wsscreen_descr * scr,void ** cookiep,int * curxp,int * curyp,long * attrp)288 wmlcd_alloc_screen(void *v, const struct wsscreen_descr *scr, void **cookiep,
289                        int *curxp, int *curyp, long *attrp)
290 {
291 printf("%s\n", __func__);
292 return -1;
293 }
294 
295 static void
wmlcd_free_screen(void * v,void * cookie)296 wmlcd_free_screen(void *v, void *cookie)
297 {
298 printf("%s\n", __func__);
299 }
300 
301 static int
wmlcd_show_screen(void * v,void * cookie,int waitok,void (* func)(void *,int,int),void * arg)302 wmlcd_show_screen(void *v, void *cookie, int waitok,
303                       void (*func)(void *, int, int), void *arg)
304 {
305 printf("%s\n", __func__);
306 return -1;
307 }
308 
309 int
wmlcd_cnattach(void)310 wmlcd_cnattach(void)
311 {
312 
313           is_console = 1;
314           return 0;
315 }
316