1 /*        $NetBSD: imx_genfb.c,v 1.1 2015/12/21 04:26:28 hkenken Exp $          */
2 
3 /*
4  * Copyright (c) 2015 Genetec Corporation.  All rights reserved.
5  * Written by Hashimoto Kenichi for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: imx_genfb.c,v 1.1 2015/12/21 04:26:28 hkenken Exp $");
31 
32 #include "opt_ddb.h"
33 #include "opt_genfb.h"
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 
42 #include <arm/imx/imx51reg.h>
43 #include <arm/imx/imx51var.h>
44 #include <arm/imx/imx51_ipuv3var.h>
45 
46 #if defined(DDB)
47 #include <machine/db_machdep.h>
48 #include <ddb/db_extern.h>
49 #endif
50 
51 #include <dev/videomode/videomode.h>
52 #include <dev/wsfb/genfbvar.h>
53 
54 struct imx_genfb_softc {
55           struct genfb_softc sc_gen;
56 
57           bus_dma_tag_t sc_dmat;
58           bus_dma_segment_t *sc_dmasegs;
59           int sc_ndmasegs;
60 };
61 
62 #if defined(DDB)
63 static void         imx_genfb_ddb_trap_callback(int);
64 static device_t     imx_genfb_consoledev = NULL;
65 #endif
66 
67 static int          imx_genfb_match(device_t, cfdata_t, void *);
68 static void         imx_genfb_attach(device_t, device_t, void *);
69 
70 static int          imx_genfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
71 static paddr_t      imx_genfb_mmap(void *, void *, off_t, int);
72 static bool         imx_genfb_shutdown(device_t, int);
73 
74 CFATTACH_DECL_NEW(imx_genfb, sizeof(struct imx_genfb_softc),
75           imx_genfb_match, imx_genfb_attach, NULL, NULL);
76 
77 static int
imx_genfb_match(device_t parent,cfdata_t cf,void * aux)78 imx_genfb_match(device_t parent, cfdata_t cf, void *aux)
79 {
80           return 1;
81 }
82 
83 static void
imx_genfb_attach(device_t parent,device_t self,void * aux)84 imx_genfb_attach(device_t parent, device_t self, void *aux)
85 {
86           struct imx_genfb_softc *sc = device_private(self);
87           struct imxfb_attach_args * const ifb = aux;
88           prop_dictionary_t cfg = device_properties(self);
89           struct genfb_ops ops;
90 
91           sc->sc_gen.sc_dev = self;
92           sc->sc_dmat = ifb->ifb_dmat;
93           sc->sc_dmasegs = ifb->ifb_dmasegs;
94           sc->sc_ndmasegs = ifb->ifb_ndmasegs;
95 
96           prop_dictionary_set_uint32(cfg, "width", ifb->ifb_width);
97           prop_dictionary_set_uint32(cfg, "height", ifb->ifb_height);
98           prop_dictionary_set_uint8(cfg, "depth", ifb->ifb_depth);
99           prop_dictionary_set_uint16(cfg, "linebytes", ifb->ifb_stride);
100           prop_dictionary_set_uint32(cfg, "address", 0);
101           prop_dictionary_set_uint32(cfg, "virtual_address",
102               (uintptr_t)ifb->ifb_fb);
103 
104           genfb_init(&sc->sc_gen);
105 
106           if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) {
107                     aprint_normal(": disabled\n");
108                     return;
109           }
110 
111           pmf_device_register1(self, NULL, NULL, imx_genfb_shutdown);
112 
113           memset(&ops, 0, sizeof(ops));
114           ops.genfb_ioctl = imx_genfb_ioctl;
115           ops.genfb_mmap = imx_genfb_mmap;
116 
117           aprint_naive("\n");
118 
119           bool is_console = false;
120           prop_dictionary_get_bool(cfg, "is_console", &is_console);
121 
122           if (is_console)
123                     aprint_normal(": switching to framebuffer console\n");
124           else
125                     aprint_normal("\n");
126 
127           genfb_attach(&sc->sc_gen, &ops);
128 
129 #if defined(DDB)
130           if (is_console) {
131                     imx_genfb_consoledev = self;
132                     db_trap_callback = imx_genfb_ddb_trap_callback;
133           }
134 #endif
135 }
136 
137 static int
imx_genfb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,lwp_t * l)138 imx_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
139 {
140           struct wsdisplayio_bus_id *busid;
141 
142           switch (cmd) {
143           case WSDISPLAYIO_GTYPE:
144                     *(u_int *)data = WSDISPLAY_TYPE_IMXIPU;
145                     return 0;
146           case WSDISPLAYIO_GET_BUSID:
147                     busid = data;
148                     busid->bus_type = WSDISPLAYIO_BUS_SOC;
149                     return 0;
150           default:
151                     return EPASSTHROUGH;
152           }
153 }
154 
155 static paddr_t
imx_genfb_mmap(void * v,void * vs,off_t offset,int prot)156 imx_genfb_mmap(void *v, void *vs, off_t offset, int prot)
157 {
158           struct imx_genfb_softc *sc = v;
159 
160           KASSERT(offset >= 0);
161           KASSERT(offset < sc->sc_dmasegs[0].ds_len);
162 
163           return bus_dmamem_mmap(sc->sc_dmat, sc->sc_dmasegs, sc->sc_ndmasegs,
164               offset, prot, BUS_DMA_PREFETCHABLE);
165 }
166 
167 static bool
imx_genfb_shutdown(device_t self,int flags)168 imx_genfb_shutdown(device_t self, int flags)
169 {
170           genfb_enable_polling(self);
171           return true;
172 }
173 
174 #if defined(DDB)
175 static void
imx_genfb_ddb_trap_callback(int where)176 imx_genfb_ddb_trap_callback(int where)
177 {
178           if (imx_genfb_consoledev == NULL)
179                     return;
180 
181           if (where)
182                     genfb_enable_polling(imx_genfb_consoledev);
183           else
184                     genfb_disable_polling(imx_genfb_consoledev);
185 }
186 #endif
187 
188 void
imx_genfb_set_videomode(device_t dev,u_int width,u_int height)189 imx_genfb_set_videomode(device_t dev, u_int width, u_int height)
190 {
191           struct imx_genfb_softc *sc = device_private(dev);
192 
193           if (sc->sc_gen.sc_width != width || sc->sc_gen.sc_height != height) {
194                     device_printf(sc->sc_gen.sc_dev,
195                         "mode switching not yet supported\n");
196           }
197 }
198