1*c7fb772bSthorpej /* $NetBSD: epsonlcd.c,v 1.4 2021/08/07 16:18:53 thorpej Exp $ */
2587ef940Skiyohara /*
3587ef940Skiyohara * Copyright (c) 2011 KIYOHARA Takashi
4587ef940Skiyohara * All rights reserved.
5587ef940Skiyohara *
6587ef940Skiyohara * Redistribution and use in source and binary forms, with or without
7587ef940Skiyohara * modification, are permitted provided that the following conditions
8587ef940Skiyohara * are met:
9587ef940Skiyohara * 1. Redistributions of source code must retain the above copyright
10587ef940Skiyohara * notice, this list of conditions and the following disclaimer.
11587ef940Skiyohara * 2. Redistributions in binary form must reproduce the above copyright
12587ef940Skiyohara * notice, this list of conditions and the following disclaimer in the
13587ef940Skiyohara * documentation and/or other materials provided with the distribution.
14587ef940Skiyohara *
15587ef940Skiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16587ef940Skiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17587ef940Skiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18587ef940Skiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19587ef940Skiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20587ef940Skiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21587ef940Skiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22587ef940Skiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23587ef940Skiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24587ef940Skiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25587ef940Skiyohara * POSSIBILITY OF SUCH DAMAGE.
26587ef940Skiyohara */
27587ef940Skiyohara #include <sys/cdefs.h>
28*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: epsonlcd.c,v 1.4 2021/08/07 16:18:53 thorpej Exp $");
29587ef940Skiyohara
30587ef940Skiyohara #include <sys/param.h>
31587ef940Skiyohara #include <sys/bus.h>
32587ef940Skiyohara #include <sys/device.h>
33587ef940Skiyohara #include <sys/errno.h>
34587ef940Skiyohara
35587ef940Skiyohara #include <machine/bootinfo.h>
36587ef940Skiyohara #include <machine/platid.h>
37587ef940Skiyohara #include <machine/platid_mask.h>
38587ef940Skiyohara
39587ef940Skiyohara #include <dev/wscons/wsdisplayvar.h>
40587ef940Skiyohara #include <dev/rasops/rasops.h>
41587ef940Skiyohara #include <dev/hpc/hpcfbio.h>
42587ef940Skiyohara #include <dev/hpc/hpcfbvar.h>
43587ef940Skiyohara #include <dev/hpc/video_subr.h>
44587ef940Skiyohara
45587ef940Skiyohara #include <arm/xscale/pxa2x0var.h>
46587ef940Skiyohara
47587ef940Skiyohara #include <hpcarm/dev/s1d138xxreg.h>
48587ef940Skiyohara
49587ef940Skiyohara
50587ef940Skiyohara struct epsonlcd_softc {
51587ef940Skiyohara device_t sc_dev;
52587ef940Skiyohara
53587ef940Skiyohara bus_space_tag_t sc_iot;
54587ef940Skiyohara bus_space_handle_t sc_ioh;
55587ef940Skiyohara
56587ef940Skiyohara bus_addr_t sc_fbaddr;
57587ef940Skiyohara bus_space_tag_t sc_fbt;
58587ef940Skiyohara bus_space_handle_t sc_fbh;
59587ef940Skiyohara
60587ef940Skiyohara struct video_chip sc_vc;
61587ef940Skiyohara struct hpcfb_fbconf sc_fb;
62587ef940Skiyohara struct hpcfb_dspconf sc_dsp;
63587ef940Skiyohara };
64587ef940Skiyohara
65587ef940Skiyohara static int epsonlcd_match(device_t, cfdata_t, void *);
66587ef940Skiyohara static void epsonlcd_attach(device_t, device_t, void *);
67587ef940Skiyohara
68587ef940Skiyohara static int epsonlcd_ioctl(void *, u_long, void *, int, struct lwp *);
69587ef940Skiyohara static paddr_t epsonlcd_mmap(void *, off_t, int);
70587ef940Skiyohara
71587ef940Skiyohara static void epsonlcd_setup_hpcfbif(struct epsonlcd_softc *,
72587ef940Skiyohara struct hpcfb_fbconf *);
73587ef940Skiyohara static void epsonlcd_get_video_chip(struct epsonlcd_softc *,
74587ef940Skiyohara struct video_chip *);
75587ef940Skiyohara
76587ef940Skiyohara CFATTACH_DECL_NEW(epsonlcd, sizeof(struct epsonlcd_softc),
77587ef940Skiyohara epsonlcd_match, epsonlcd_attach, NULL, NULL);
78587ef940Skiyohara
79587ef940Skiyohara
80587ef940Skiyohara static struct hpcfb_accessops epsonlcd_ha = {
81587ef940Skiyohara .ioctl = epsonlcd_ioctl,
82587ef940Skiyohara .mmap = epsonlcd_mmap,
83587ef940Skiyohara };
84587ef940Skiyohara
85587ef940Skiyohara /* ARGSUSED */
86587ef940Skiyohara static int
epsonlcd_match(device_t parent,cfdata_t match,void * aux)87587ef940Skiyohara epsonlcd_match(device_t parent, cfdata_t match, void *aux)
88587ef940Skiyohara {
89587ef940Skiyohara struct pxaip_attach_args *pxa = aux;
90587ef940Skiyohara
91587ef940Skiyohara if (strcmp(pxa->pxa_name, match->cf_name) != 0 ||
92587ef940Skiyohara !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
93587ef940Skiyohara return 0;
94587ef940Skiyohara
95587ef940Skiyohara return 1;
96587ef940Skiyohara }
97587ef940Skiyohara
98587ef940Skiyohara /* ARGSUSED */
99587ef940Skiyohara static void
epsonlcd_attach(device_t parent,device_t self,void * aux)100587ef940Skiyohara epsonlcd_attach(device_t parent, device_t self, void *aux)
101587ef940Skiyohara {
102587ef940Skiyohara struct epsonlcd_softc *sc = device_private(self);
103587ef940Skiyohara struct pxaip_attach_args *pxa = aux;
104587ef940Skiyohara struct hpcfb_attach_args haa;
105587ef940Skiyohara int rv;
106587ef940Skiyohara
107587ef940Skiyohara aprint_naive("\n");
108587ef940Skiyohara aprint_normal(": Epson S1D138xx LCD Controller\n");
109587ef940Skiyohara
110587ef940Skiyohara sc->sc_dev = self;
111587ef940Skiyohara sc->sc_iot = pxa->pxa_iot;
112587ef940Skiyohara sc->sc_fbt = pxa->pxa_iot;
113587ef940Skiyohara
114587ef940Skiyohara /* NETBOOK PRO map Framebuffer here. */
115587ef940Skiyohara sc->sc_fbaddr = pxa->pxa_addr + 0x200000;
116587ef940Skiyohara
117587ef940Skiyohara if (bus_space_map(sc->sc_iot, pxa->pxa_addr, 0x2000, 0, &sc->sc_fbh) != 0) {
118587ef940Skiyohara aprint_error_dev(self, "can't map I/O space\n");
119587ef940Skiyohara return;
120587ef940Skiyohara }
121587ef940Skiyohara rv = bus_space_map(sc->sc_fbt, sc->sc_fbaddr, S1D138XX_FRAMEBUFFER_SIZE,
122587ef940Skiyohara 0, &sc->sc_fbh);
123587ef940Skiyohara if (rv != 0) {
124587ef940Skiyohara aprint_error_dev(self, "can't map Framebuffer\n");
125587ef940Skiyohara return;
126587ef940Skiyohara }
127587ef940Skiyohara
128587ef940Skiyohara epsonlcd_get_video_chip(sc, &sc->sc_vc);
129587ef940Skiyohara epsonlcd_setup_hpcfbif(sc, &sc->sc_fb);
130587ef940Skiyohara if (hpcfb_cnattach(&sc->sc_fb) != 0)
131587ef940Skiyohara panic("%s: hpcfb_cnattach failed\n", __func__);
132587ef940Skiyohara /* always console */
133587ef940Skiyohara aprint_normal_dev(self, "console\n");
134587ef940Skiyohara
135587ef940Skiyohara /* register interface to hpcfb */
136587ef940Skiyohara haa.ha_console = 1; /* Always console */
137587ef940Skiyohara haa.ha_accessops = &epsonlcd_ha;
138587ef940Skiyohara haa.ha_accessctx = sc;
139587ef940Skiyohara haa.ha_curfbconf = 0;
140587ef940Skiyohara haa.ha_nfbconf = 1;
141587ef940Skiyohara haa.ha_fbconflist = &sc->sc_fb;
142587ef940Skiyohara haa.ha_curdspconf = 0;
143587ef940Skiyohara haa.ha_ndspconf = 1;
144587ef940Skiyohara haa.ha_dspconflist = &sc->sc_dsp;
145587ef940Skiyohara
146*c7fb772bSthorpej config_found(self, &haa, hpcfbprint, CFARGS_NONE);
147587ef940Skiyohara
148587ef940Skiyohara if (!pmf_device_register(self, NULL, NULL))
149587ef940Skiyohara aprint_error_dev(self, "unable to establish power handler\n");
150587ef940Skiyohara }
151587ef940Skiyohara
152587ef940Skiyohara
153587ef940Skiyohara static int
epsonlcd_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)154587ef940Skiyohara epsonlcd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
155587ef940Skiyohara {
156587ef940Skiyohara printf("%s: To Be Done...: cmd=0x%lx\n", __func__, cmd);
157587ef940Skiyohara switch (cmd) {
158587ef940Skiyohara }
159587ef940Skiyohara return EPASSTHROUGH;
160587ef940Skiyohara }
161587ef940Skiyohara
162587ef940Skiyohara static paddr_t
epsonlcd_mmap(void * ctx,off_t offset,int prot)163587ef940Skiyohara epsonlcd_mmap(void *ctx, off_t offset, int prot)
164587ef940Skiyohara {
165587ef940Skiyohara struct epsonlcd_softc *sc = ctx;
166587ef940Skiyohara struct hpcfb_fbconf *fb = &sc->sc_fb;
167587ef940Skiyohara
168587ef940Skiyohara if (offset < 0 || (fb->hf_bytes_per_plane + fb->hf_offset) < offset)
169587ef940Skiyohara return -1;
170587ef940Skiyohara
171587ef940Skiyohara return arm_btop(sc->sc_fbaddr + offset);
172587ef940Skiyohara }
173587ef940Skiyohara
174587ef940Skiyohara static void
epsonlcd_setup_hpcfbif(struct epsonlcd_softc * sc,struct hpcfb_fbconf * fb)175587ef940Skiyohara epsonlcd_setup_hpcfbif(struct epsonlcd_softc *sc, struct hpcfb_fbconf *fb)
176587ef940Skiyohara {
177587ef940Skiyohara struct video_chip *vc = &sc->sc_vc;
178587ef940Skiyohara
179587ef940Skiyohara memset(fb, 0, sizeof(struct hpcfb_fbconf));
180587ef940Skiyohara fb->hf_conf_index = 0; /* configuration index */
181587ef940Skiyohara fb->hf_nconfs = 1; /* how many configurations */
182587ef940Skiyohara /* frame buffer name */
183587ef940Skiyohara strncpy(fb->hf_name, "Epson S1D138xx LCD Controller", HPCFB_MAXNAMELEN);
184587ef940Skiyohara
185587ef940Skiyohara /* configuration name */
186587ef940Skiyohara strncpy(fb->hf_conf_name, "LCD", HPCFB_MAXNAMELEN);
187587ef940Skiyohara
188587ef940Skiyohara fb->hf_height = vc->vc_fbheight;
189587ef940Skiyohara fb->hf_width = vc->vc_fbwidth;
190587ef940Skiyohara fb->hf_baseaddr = vc->vc_fbvaddr;
191587ef940Skiyohara /* frame buffer start offset */
192587ef940Skiyohara fb->hf_offset = vc->vc_fbvaddr - arm_ptob(arm_btop(vc->vc_fbvaddr));
193587ef940Skiyohara
194587ef940Skiyohara fb->hf_bytes_per_line = (vc->vc_fbwidth * vc->vc_fbdepth) / NBBY;
195587ef940Skiyohara fb->hf_nplanes = 1;
196587ef940Skiyohara fb->hf_bytes_per_plane = vc->vc_fbheight * fb->hf_bytes_per_line;
197587ef940Skiyohara
198587ef940Skiyohara fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
199587ef940Skiyohara fb->hf_access_flags |= HPCFB_ACCESS_WORD;
200587ef940Skiyohara fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
201587ef940Skiyohara if (vc->vc_reverse)
202587ef940Skiyohara fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
203587ef940Skiyohara
204587ef940Skiyohara switch (vc->vc_fbdepth) {
205587ef940Skiyohara default:
206587ef940Skiyohara panic("%s: not supported color depth %d\n",
207587ef940Skiyohara __func__, vc->vc_fbdepth);
208587ef940Skiyohara
209587ef940Skiyohara /* NOTREACHED */
210587ef940Skiyohara case 16:
211587ef940Skiyohara fb->hf_class = HPCFB_CLASS_RGBCOLOR;
212587ef940Skiyohara fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
213587ef940Skiyohara fb->hf_order_flags = HPCFB_REVORDER_BYTE;
214587ef940Skiyohara fb->hf_pack_width = 16;
215587ef940Skiyohara fb->hf_pixels_per_pack = 1;
216587ef940Skiyohara fb->hf_pixel_width = 16;
217587ef940Skiyohara
218587ef940Skiyohara fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
219587ef940Skiyohara /* reserved for future use */
220587ef940Skiyohara fb->hf_u.hf_rgb.hf_flags = 0;
221587ef940Skiyohara
222587ef940Skiyohara fb->hf_u.hf_rgb.hf_red_width = 5;
223587ef940Skiyohara fb->hf_u.hf_rgb.hf_red_shift = 11;
224587ef940Skiyohara fb->hf_u.hf_rgb.hf_green_width = 6;
225587ef940Skiyohara fb->hf_u.hf_rgb.hf_green_shift = 5;
226587ef940Skiyohara fb->hf_u.hf_rgb.hf_blue_width = 5;
227587ef940Skiyohara fb->hf_u.hf_rgb.hf_blue_shift = 0;
228587ef940Skiyohara fb->hf_u.hf_rgb.hf_alpha_width = 0;
229587ef940Skiyohara fb->hf_u.hf_rgb.hf_alpha_shift = 0;
230587ef940Skiyohara break;
231587ef940Skiyohara
232587ef940Skiyohara case 8:
233587ef940Skiyohara fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
234587ef940Skiyohara fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
235587ef940Skiyohara fb->hf_pack_width = 8;
236587ef940Skiyohara fb->hf_pixels_per_pack = 1;
237587ef940Skiyohara fb->hf_pixel_width = 8;
238587ef940Skiyohara
239587ef940Skiyohara fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
240587ef940Skiyohara /* reserved for future use */
241587ef940Skiyohara fb->hf_u.hf_indexed.hf_flags = 0;
242587ef940Skiyohara break;
243587ef940Skiyohara }
244587ef940Skiyohara }
245587ef940Skiyohara
246587ef940Skiyohara static void
epsonlcd_get_video_chip(struct epsonlcd_softc * sc,struct video_chip * vc)247587ef940Skiyohara epsonlcd_get_video_chip(struct epsonlcd_softc *sc, struct video_chip *vc)
248587ef940Skiyohara {
249587ef940Skiyohara
250587ef940Skiyohara #define BSH2VADDR(ioh) (ioh)
251587ef940Skiyohara
252587ef940Skiyohara vc->vc_fbvaddr = BSH2VADDR(sc->sc_fbh);
253587ef940Skiyohara vc->vc_fbpaddr = sc->sc_fbaddr;
254587ef940Skiyohara vc->vc_fbsize = S1D138XX_FRAMEBUFFER_SIZE;
255587ef940Skiyohara vc->vc_fbdepth = (bootinfo->fb_line_bytes / bootinfo->fb_width) * NBBY;
256587ef940Skiyohara vc->vc_fbwidth = bootinfo->fb_width;
257587ef940Skiyohara vc->vc_fbheight = bootinfo->fb_height;
258587ef940Skiyohara vc->vc_reverse = video_reverse_color();
259587ef940Skiyohara }
260