bcm2835_genfb.c revision 1.3 1 1.3 jmcneill /* $NetBSD: bcm2835_genfb.c,v 1.3 2013/01/10 14:12:16 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2013 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill /*
30 1.1 jmcneill * Generic framebuffer console driver
31 1.1 jmcneill */
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/cdefs.h>
34 1.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: bcm2835_genfb.c,v 1.3 2013/01/10 14:12:16 jmcneill Exp $");
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/types.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/device.h>
40 1.1 jmcneill #include <sys/conf.h>
41 1.1 jmcneill #include <sys/bus.h>
42 1.1 jmcneill #include <sys/kmem.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <arm/broadcom/bcm_amba.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/wsfb/genfbvar.h>
47 1.1 jmcneill
48 1.1 jmcneill struct bcmgenfb_softc {
49 1.1 jmcneill struct genfb_softc sc_gen;
50 1.1 jmcneill bus_space_tag_t sc_iot;
51 1.1 jmcneill bus_space_handle_t sc_ioh;
52 1.2 jmcneill
53 1.2 jmcneill uint32_t sc_wstype;
54 1.1 jmcneill };
55 1.1 jmcneill
56 1.1 jmcneill static int bcmgenfb_match(device_t, cfdata_t, void *);
57 1.1 jmcneill static void bcmgenfb_attach(device_t, device_t, void *);
58 1.1 jmcneill
59 1.1 jmcneill static int bcmgenfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
60 1.1 jmcneill static paddr_t bcmgenfb_mmap(void *, void *, off_t, int);
61 1.1 jmcneill
62 1.1 jmcneill CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
63 1.1 jmcneill bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
64 1.1 jmcneill
65 1.1 jmcneill static int
66 1.1 jmcneill bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
67 1.1 jmcneill {
68 1.1 jmcneill struct amba_attach_args *aaa = aux;
69 1.1 jmcneill
70 1.1 jmcneill return !strcmp(aaa->aaa_name, "fb");
71 1.1 jmcneill }
72 1.1 jmcneill
73 1.1 jmcneill static void
74 1.1 jmcneill bcmgenfb_attach(device_t parent, device_t self, void *aux)
75 1.1 jmcneill {
76 1.1 jmcneill struct bcmgenfb_softc *sc = device_private(self);
77 1.1 jmcneill struct amba_attach_args *aaa = aux;
78 1.2 jmcneill prop_dictionary_t dict = device_properties(self);
79 1.1 jmcneill struct genfb_ops ops;
80 1.3 jmcneill bool is_console = false;
81 1.1 jmcneill int error;
82 1.1 jmcneill
83 1.1 jmcneill sc->sc_gen.sc_dev = self;
84 1.1 jmcneill sc->sc_iot = aaa->aaa_iot;
85 1.1 jmcneill
86 1.2 jmcneill sc->sc_wstype = WSDISPLAY_TYPE_VC4;
87 1.2 jmcneill prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
88 1.3 jmcneill prop_dictionary_get_bool(dict, "is_console", &is_console);
89 1.2 jmcneill
90 1.1 jmcneill genfb_init(&sc->sc_gen);
91 1.1 jmcneill
92 1.1 jmcneill if (sc->sc_gen.sc_width == 0 ||
93 1.1 jmcneill sc->sc_gen.sc_fbsize == 0) {
94 1.1 jmcneill aprint_normal(": disabled\n");
95 1.1 jmcneill return;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
99 1.1 jmcneill sc->sc_gen.sc_fbsize,
100 1.1 jmcneill BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
101 1.1 jmcneill if (error) {
102 1.1 jmcneill aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
103 1.1 jmcneill error);
104 1.1 jmcneill return;
105 1.1 jmcneill }
106 1.1 jmcneill sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
107 1.1 jmcneill
108 1.1 jmcneill memset(&ops, 0, sizeof(ops));
109 1.1 jmcneill ops.genfb_ioctl = bcmgenfb_ioctl;
110 1.1 jmcneill ops.genfb_mmap = bcmgenfb_mmap;
111 1.1 jmcneill
112 1.1 jmcneill aprint_naive("\n");
113 1.3 jmcneill
114 1.3 jmcneill if (is_console)
115 1.3 jmcneill aprint_normal(": switching to framebuffer console\n");
116 1.3 jmcneill else
117 1.3 jmcneill aprint_normal("\n");
118 1.1 jmcneill
119 1.1 jmcneill genfb_attach(&sc->sc_gen, &ops);
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill static int
123 1.1 jmcneill bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
124 1.1 jmcneill {
125 1.2 jmcneill struct bcmgenfb_softc *sc = v;
126 1.1 jmcneill struct wsdisplayio_bus_id *busid;
127 1.1 jmcneill
128 1.1 jmcneill switch (cmd) {
129 1.1 jmcneill case WSDISPLAYIO_GTYPE:
130 1.2 jmcneill *(u_int *)data = sc->sc_wstype;
131 1.1 jmcneill return 0;
132 1.1 jmcneill case WSDISPLAYIO_GET_BUSID:
133 1.1 jmcneill busid = data;
134 1.1 jmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC;
135 1.1 jmcneill return 0;
136 1.1 jmcneill default:
137 1.1 jmcneill return EPASSTHROUGH;
138 1.1 jmcneill }
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static paddr_t
142 1.1 jmcneill bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
143 1.1 jmcneill {
144 1.1 jmcneill struct bcmgenfb_softc *sc = v;
145 1.1 jmcneill
146 1.1 jmcneill if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
147 1.1 jmcneill return -1;
148 1.1 jmcneill
149 1.1 jmcneill return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
150 1.1 jmcneill prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
151 1.1 jmcneill }
152