plfb_fdt.c revision 1.2.6.2 1 1.2.6.2 jdolecek /* $NetBSD: plfb_fdt.c,v 1.2.6.2 2017/12/03 11:35:52 jdolecek Exp $ */
2 1.2.6.2 jdolecek
3 1.2.6.2 jdolecek /*-
4 1.2.6.2 jdolecek * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.2.6.2 jdolecek * All rights reserved.
6 1.2.6.2 jdolecek *
7 1.2.6.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 jdolecek * modification, are permitted provided that the following conditions
9 1.2.6.2 jdolecek * are met:
10 1.2.6.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.2.6.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.6.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.2.6.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.2.6.2 jdolecek *
16 1.2.6.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2.6.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.6.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.6.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2.6.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.6.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.6.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.6.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.6.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.6.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.6.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.2.6.2 jdolecek */
28 1.2.6.2 jdolecek
29 1.2.6.2 jdolecek /*
30 1.2.6.2 jdolecek * ARM PrimeCell PL111 framebuffer console driver
31 1.2.6.2 jdolecek */
32 1.2.6.2 jdolecek
33 1.2.6.2 jdolecek #include <sys/cdefs.h>
34 1.2.6.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: plfb_fdt.c,v 1.2.6.2 2017/12/03 11:35:52 jdolecek Exp $");
35 1.2.6.2 jdolecek
36 1.2.6.2 jdolecek #include <sys/param.h>
37 1.2.6.2 jdolecek #include <sys/types.h>
38 1.2.6.2 jdolecek #include <sys/systm.h>
39 1.2.6.2 jdolecek #include <sys/device.h>
40 1.2.6.2 jdolecek #include <sys/conf.h>
41 1.2.6.2 jdolecek #include <sys/bus.h>
42 1.2.6.2 jdolecek #include <sys/kmem.h>
43 1.2.6.2 jdolecek #include <sys/sysctl.h>
44 1.2.6.2 jdolecek
45 1.2.6.2 jdolecek #include <dev/wsfb/genfbvar.h>
46 1.2.6.2 jdolecek
47 1.2.6.2 jdolecek #include <dev/fdt/fdtvar.h>
48 1.2.6.2 jdolecek #include <dev/fdt/display_timing.h>
49 1.2.6.2 jdolecek
50 1.2.6.2 jdolecek #define LCDTIMING0 0x000
51 1.2.6.2 jdolecek #define LCDTIMING0_HBP __BITS(31,24)
52 1.2.6.2 jdolecek #define LCDTIMING0_HFP __BITS(23,16)
53 1.2.6.2 jdolecek #define LCDTIMING0_HSW __BITS(15,8)
54 1.2.6.2 jdolecek #define LCDTIMING0_PPL __BITS(7,2)
55 1.2.6.2 jdolecek #define LCDTIMING1 0x004
56 1.2.6.2 jdolecek #define LCDTIMING1_VBP __BITS(31,24)
57 1.2.6.2 jdolecek #define LCDTIMING1_VFP __BITS(23,16)
58 1.2.6.2 jdolecek #define LCDTIMING1_VSW __BITS(15,10)
59 1.2.6.2 jdolecek #define LCDTIMING1_LPP __BITS(9,0)
60 1.2.6.2 jdolecek #define LCDUPBASE 0x010
61 1.2.6.2 jdolecek #define LCDLPBASE 0x014
62 1.2.6.2 jdolecek #define LCDCONTROL 0x018
63 1.2.6.2 jdolecek #define LCDCONTROL_PWR __BIT(11)
64 1.2.6.2 jdolecek #define LCDCONTROL_BGR __BIT(8)
65 1.2.6.2 jdolecek #define LCDCONTROL_BPP __BITS(3,1)
66 1.2.6.2 jdolecek #define LCDCONTROL_BPP_24 __SHIFTIN(5, LCDCONTROL_BPP)
67 1.2.6.2 jdolecek #define LCDCONTROL_EN __BIT(0)
68 1.2.6.2 jdolecek
69 1.2.6.2 jdolecek #define PLFB_BPP 32
70 1.2.6.2 jdolecek
71 1.2.6.2 jdolecek static int plfb_console_phandle = -1;
72 1.2.6.2 jdolecek
73 1.2.6.2 jdolecek struct plfb_softc {
74 1.2.6.2 jdolecek struct genfb_softc sc_gen;
75 1.2.6.2 jdolecek bus_space_tag_t sc_bst;
76 1.2.6.2 jdolecek bus_space_handle_t sc_bsh;
77 1.2.6.2 jdolecek int sc_phandle;
78 1.2.6.2 jdolecek
79 1.2.6.2 jdolecek bus_space_handle_t sc_vram_bsh;
80 1.2.6.2 jdolecek bus_addr_t sc_vram_addr;
81 1.2.6.2 jdolecek bus_size_t sc_vram_size;
82 1.2.6.2 jdolecek uintptr_t sc_vram;
83 1.2.6.2 jdolecek
84 1.2.6.2 jdolecek uint32_t sc_wstype;
85 1.2.6.2 jdolecek };
86 1.2.6.2 jdolecek
87 1.2.6.2 jdolecek static int plfb_match(device_t, cfdata_t, void *);
88 1.2.6.2 jdolecek static void plfb_attach(device_t, device_t, void *);
89 1.2.6.2 jdolecek
90 1.2.6.2 jdolecek static int plfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
91 1.2.6.2 jdolecek static paddr_t plfb_mmap(void *, void *, off_t, int);
92 1.2.6.2 jdolecek static bool plfb_shutdown(device_t, int);
93 1.2.6.2 jdolecek
94 1.2.6.2 jdolecek static void plfb_init(struct plfb_softc *);
95 1.2.6.2 jdolecek
96 1.2.6.2 jdolecek static const char * const compatible[] = {
97 1.2.6.2 jdolecek "arm,pl111",
98 1.2.6.2 jdolecek NULL
99 1.2.6.2 jdolecek };
100 1.2.6.2 jdolecek
101 1.2.6.2 jdolecek CFATTACH_DECL_NEW(plfb_fdt, sizeof(struct plfb_softc),
102 1.2.6.2 jdolecek plfb_match, plfb_attach, NULL, NULL);
103 1.2.6.2 jdolecek
104 1.2.6.2 jdolecek #define FB_READ(sc, reg) \
105 1.2.6.2 jdolecek bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
106 1.2.6.2 jdolecek #define FB_WRITE(sc, reg, val) \
107 1.2.6.2 jdolecek bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
108 1.2.6.2 jdolecek
109 1.2.6.2 jdolecek static int
110 1.2.6.2 jdolecek plfb_match(device_t parent, cfdata_t match, void *aux)
111 1.2.6.2 jdolecek {
112 1.2.6.2 jdolecek struct fdt_attach_args * const faa = aux;
113 1.2.6.2 jdolecek
114 1.2.6.2 jdolecek return of_match_compatible(faa->faa_phandle, compatible);
115 1.2.6.2 jdolecek }
116 1.2.6.2 jdolecek
117 1.2.6.2 jdolecek static void
118 1.2.6.2 jdolecek plfb_attach(device_t parent, device_t self, void *aux)
119 1.2.6.2 jdolecek {
120 1.2.6.2 jdolecek struct plfb_softc *sc = device_private(self);
121 1.2.6.2 jdolecek prop_dictionary_t dict = device_properties(self);
122 1.2.6.2 jdolecek struct fdt_attach_args * const faa = aux;
123 1.2.6.2 jdolecek const int phandle = faa->faa_phandle;
124 1.2.6.2 jdolecek struct genfb_ops ops;
125 1.2.6.2 jdolecek struct clk *clk;
126 1.2.6.2 jdolecek bus_addr_t addr;
127 1.2.6.2 jdolecek bus_size_t size;
128 1.2.6.2 jdolecek
129 1.2.6.2 jdolecek sc->sc_gen.sc_dev = self;
130 1.2.6.2 jdolecek sc->sc_phandle = phandle;
131 1.2.6.2 jdolecek sc->sc_bst = faa->faa_bst;
132 1.2.6.2 jdolecek
133 1.2.6.2 jdolecek /* Enable clocks */
134 1.2.6.2 jdolecek for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++)
135 1.2.6.2 jdolecek if (clk_enable(clk) != 0) {
136 1.2.6.2 jdolecek aprint_error(": couldn't enable clock #%d\n", i);
137 1.2.6.2 jdolecek return;
138 1.2.6.2 jdolecek }
139 1.2.6.2 jdolecek
140 1.2.6.2 jdolecek /* Map CLCD registers */
141 1.2.6.2 jdolecek if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
142 1.2.6.2 jdolecek aprint_error(": missing 'reg' property\n");
143 1.2.6.2 jdolecek return;
144 1.2.6.2 jdolecek }
145 1.2.6.2 jdolecek if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh)) {
146 1.2.6.2 jdolecek aprint_error(": couldn't map device\n");
147 1.2.6.2 jdolecek return;
148 1.2.6.2 jdolecek }
149 1.2.6.2 jdolecek
150 1.2.6.2 jdolecek /* Map VRAM */
151 1.2.6.2 jdolecek const int vram_phandle = fdtbus_get_phandle(phandle, "memory-region");
152 1.2.6.2 jdolecek if (vram_phandle == -1) {
153 1.2.6.2 jdolecek /*
154 1.2.6.2 jdolecek * The 'memory-region' property is optional. If
155 1.2.6.2 jdolecek * absent, we can allocate FB from main RAM. (TODO)
156 1.2.6.2 jdolecek */
157 1.2.6.2 jdolecek aprint_error(": missing 'memory-region' property\n");
158 1.2.6.2 jdolecek return;
159 1.2.6.2 jdolecek }
160 1.2.6.2 jdolecek if (fdtbus_get_reg(vram_phandle, 0, &sc->sc_vram_addr,
161 1.2.6.2 jdolecek &sc->sc_vram_size) != 0) {
162 1.2.6.2 jdolecek aprint_error(": missing 'reg' property on memory-region\n");
163 1.2.6.2 jdolecek return;
164 1.2.6.2 jdolecek }
165 1.2.6.2 jdolecek if (bus_space_map(sc->sc_bst, sc->sc_vram_addr, sc->sc_vram_size,
166 1.2.6.2 jdolecek BUS_SPACE_MAP_LINEAR, &sc->sc_vram_bsh)) {
167 1.2.6.2 jdolecek aprint_error(": couldn't map vram\n");
168 1.2.6.2 jdolecek return;
169 1.2.6.2 jdolecek }
170 1.2.6.2 jdolecek sc->sc_vram = (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_vram_bsh);
171 1.2.6.2 jdolecek
172 1.2.6.2 jdolecek plfb_init(sc);
173 1.2.6.2 jdolecek
174 1.2.6.2 jdolecek sc->sc_wstype = WSDISPLAY_TYPE_PLFB;
175 1.2.6.2 jdolecek prop_dictionary_set_bool(dict, "is_console",
176 1.2.6.2 jdolecek phandle == plfb_console_phandle);
177 1.2.6.2 jdolecek
178 1.2.6.2 jdolecek genfb_init(&sc->sc_gen);
179 1.2.6.2 jdolecek
180 1.2.6.2 jdolecek if (sc->sc_gen.sc_width == 0 ||
181 1.2.6.2 jdolecek sc->sc_gen.sc_fbsize == 0) {
182 1.2.6.2 jdolecek aprint_normal(": disabled\n");
183 1.2.6.2 jdolecek return;
184 1.2.6.2 jdolecek }
185 1.2.6.2 jdolecek
186 1.2.6.2 jdolecek pmf_device_register1(self, NULL, NULL, plfb_shutdown);
187 1.2.6.2 jdolecek
188 1.2.6.2 jdolecek memset(&ops, 0, sizeof(ops));
189 1.2.6.2 jdolecek ops.genfb_ioctl = plfb_ioctl;
190 1.2.6.2 jdolecek ops.genfb_mmap = plfb_mmap;
191 1.2.6.2 jdolecek
192 1.2.6.2 jdolecek aprint_naive("\n");
193 1.2.6.2 jdolecek aprint_normal("\n");
194 1.2.6.2 jdolecek
195 1.2.6.2 jdolecek genfb_attach(&sc->sc_gen, &ops);
196 1.2.6.2 jdolecek }
197 1.2.6.2 jdolecek
198 1.2.6.2 jdolecek static int
199 1.2.6.2 jdolecek plfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
200 1.2.6.2 jdolecek {
201 1.2.6.2 jdolecek struct plfb_softc *sc = v;
202 1.2.6.2 jdolecek struct wsdisplayio_bus_id *busid;
203 1.2.6.2 jdolecek
204 1.2.6.2 jdolecek switch (cmd) {
205 1.2.6.2 jdolecek case WSDISPLAYIO_GTYPE:
206 1.2.6.2 jdolecek *(u_int *)data = sc->sc_wstype;
207 1.2.6.2 jdolecek return 0;
208 1.2.6.2 jdolecek case WSDISPLAYIO_GET_BUSID:
209 1.2.6.2 jdolecek busid = data;
210 1.2.6.2 jdolecek busid->bus_type = WSDISPLAYIO_BUS_SOC;
211 1.2.6.2 jdolecek return 0;
212 1.2.6.2 jdolecek case WSDISPLAYIO_GET_FBINFO:
213 1.2.6.2 jdolecek {
214 1.2.6.2 jdolecek struct wsdisplayio_fbinfo *fbi = data;
215 1.2.6.2 jdolecek struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
216 1.2.6.2 jdolecek
217 1.2.6.2 jdolecek return wsdisplayio_get_fbinfo(ri, fbi);
218 1.2.6.2 jdolecek }
219 1.2.6.2 jdolecek default:
220 1.2.6.2 jdolecek return EPASSTHROUGH;
221 1.2.6.2 jdolecek }
222 1.2.6.2 jdolecek }
223 1.2.6.2 jdolecek
224 1.2.6.2 jdolecek static paddr_t
225 1.2.6.2 jdolecek plfb_mmap(void *v, void *vs, off_t offset, int prot)
226 1.2.6.2 jdolecek {
227 1.2.6.2 jdolecek struct plfb_softc *sc = v;
228 1.2.6.2 jdolecek
229 1.2.6.2 jdolecek if (offset < 0 || offset >= sc->sc_vram_size)
230 1.2.6.2 jdolecek return -1;
231 1.2.6.2 jdolecek
232 1.2.6.2 jdolecek return bus_space_mmap(sc->sc_bst, sc->sc_vram_addr, offset, prot,
233 1.2.6.2 jdolecek BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
234 1.2.6.2 jdolecek }
235 1.2.6.2 jdolecek
236 1.2.6.2 jdolecek static bool
237 1.2.6.2 jdolecek plfb_shutdown(device_t self, int flags)
238 1.2.6.2 jdolecek {
239 1.2.6.2 jdolecek genfb_enable_polling(self);
240 1.2.6.2 jdolecek return true;
241 1.2.6.2 jdolecek }
242 1.2.6.2 jdolecek
243 1.2.6.2 jdolecek static int
244 1.2.6.2 jdolecek plfb_get_panel_timing(struct plfb_softc *sc, struct display_timing *timing)
245 1.2.6.2 jdolecek {
246 1.2.6.2 jdolecek int panel, panel_timing;
247 1.2.6.2 jdolecek
248 1.2.6.2 jdolecek panel = of_find_firstchild_byname(sc->sc_phandle, "panel");
249 1.2.6.2 jdolecek if (panel <= 0)
250 1.2.6.2 jdolecek return ENOENT;
251 1.2.6.2 jdolecek panel_timing = of_find_firstchild_byname(panel, "panel-timing");
252 1.2.6.2 jdolecek if (panel_timing <= 0)
253 1.2.6.2 jdolecek return ENOENT;
254 1.2.6.2 jdolecek
255 1.2.6.2 jdolecek return display_timing_parse(panel_timing, timing);
256 1.2.6.2 jdolecek }
257 1.2.6.2 jdolecek
258 1.2.6.2 jdolecek static void
259 1.2.6.2 jdolecek plfb_init(struct plfb_softc *sc)
260 1.2.6.2 jdolecek {
261 1.2.6.2 jdolecek prop_dictionary_t dict = device_properties(sc->sc_gen.sc_dev);
262 1.2.6.2 jdolecek struct display_timing timing;
263 1.2.6.2 jdolecek
264 1.2.6.2 jdolecek if (plfb_get_panel_timing(sc, &timing) != 0) {
265 1.2.6.2 jdolecek aprint_error_dev(sc->sc_gen.sc_dev,
266 1.2.6.2 jdolecek "couldn't get panel timings\n");
267 1.2.6.2 jdolecek return;
268 1.2.6.2 jdolecek }
269 1.2.6.2 jdolecek
270 1.2.6.2 jdolecek prop_dictionary_set_uint32(dict, "width", timing.hactive);
271 1.2.6.2 jdolecek prop_dictionary_set_uint32(dict, "height", timing.vactive);
272 1.2.6.2 jdolecek prop_dictionary_set_uint8(dict, "depth", PLFB_BPP);
273 1.2.6.2 jdolecek prop_dictionary_set_bool(dict, "dblscan", 0);
274 1.2.6.2 jdolecek prop_dictionary_set_bool(dict, "interlace", 0);
275 1.2.6.2 jdolecek prop_dictionary_set_uint16(dict, "linebytes", timing.hactive * (PLFB_BPP / 8));
276 1.2.6.2 jdolecek prop_dictionary_set_uint32(dict, "address", sc->sc_vram_addr);
277 1.2.6.2 jdolecek prop_dictionary_set_uint32(dict, "virtual_address", sc->sc_vram);
278 1.2.6.2 jdolecek
279 1.2.6.2 jdolecek /* FB base address */
280 1.2.6.2 jdolecek FB_WRITE(sc, LCDUPBASE, sc->sc_vram_addr);
281 1.2.6.2 jdolecek FB_WRITE(sc, LCDLPBASE, 0);
282 1.2.6.2 jdolecek
283 1.2.6.2 jdolecek /* CRTC timings */
284 1.2.6.2 jdolecek FB_WRITE(sc, LCDTIMING0,
285 1.2.6.2 jdolecek __SHIFTIN(timing.hback_porch - 1, LCDTIMING0_HBP) |
286 1.2.6.2 jdolecek __SHIFTIN(timing.hfront_porch - 1, LCDTIMING0_HFP) |
287 1.2.6.2 jdolecek __SHIFTIN(timing.hsync_len - 1, LCDTIMING0_HSW) |
288 1.2.6.2 jdolecek __SHIFTIN((timing.hactive / 16) - 1, LCDTIMING0_PPL));
289 1.2.6.2 jdolecek FB_WRITE(sc, LCDTIMING1,
290 1.2.6.2 jdolecek __SHIFTIN(timing.vback_porch - 1, LCDTIMING1_VBP) |
291 1.2.6.2 jdolecek __SHIFTIN(timing.vfront_porch - 1, LCDTIMING1_VFP) |
292 1.2.6.2 jdolecek __SHIFTIN(timing.vsync_len - 1, LCDTIMING1_VSW) |
293 1.2.6.2 jdolecek __SHIFTIN(timing.vactive - 1, LCDTIMING1_LPP));
294 1.2.6.2 jdolecek
295 1.2.6.2 jdolecek /* Configure and enable CLCD */
296 1.2.6.2 jdolecek FB_WRITE(sc, LCDCONTROL,
297 1.2.6.2 jdolecek LCDCONTROL_PWR | LCDCONTROL_EN | LCDCONTROL_BPP_24 |
298 1.2.6.2 jdolecek LCDCONTROL_BGR);
299 1.2.6.2 jdolecek }
300 1.2.6.2 jdolecek
301 1.2.6.2 jdolecek static int
302 1.2.6.2 jdolecek plfb_console_match(int phandle)
303 1.2.6.2 jdolecek {
304 1.2.6.2 jdolecek return of_match_compatible(phandle, compatible);
305 1.2.6.2 jdolecek }
306 1.2.6.2 jdolecek
307 1.2.6.2 jdolecek static void
308 1.2.6.2 jdolecek plfb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
309 1.2.6.2 jdolecek {
310 1.2.6.2 jdolecek plfb_console_phandle = faa->faa_phandle;
311 1.2.6.2 jdolecek genfb_cnattach();
312 1.2.6.2 jdolecek }
313 1.2.6.2 jdolecek
314 1.2.6.2 jdolecek static const struct fdt_console plfb_fdt_console = {
315 1.2.6.2 jdolecek .match = plfb_console_match,
316 1.2.6.2 jdolecek .consinit = plfb_console_consinit
317 1.2.6.2 jdolecek };
318 1.2.6.2 jdolecek
319 1.2.6.2 jdolecek FDT_CONSOLE(plfb, &plfb_fdt_console);
320