summaryrefslogtreecommitdiffstats
path: root/sys/arm/amlogic/aml8726/aml8726_fb.c
blob: 94d39065afcc9b47c7ca31c02101276feeb3be8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*-
 * Copyright 2013-2014 John Wehle <john@feith.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Amlogic aml8726 frame buffer driver.
 *
 * The current implementation has limited flexibility.
 * For example only progressive scan is supported when
 * using HDMI and the resolution / frame rate is not
 * negotiated.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/resource.h>
#include <sys/rman.h>

#include <sys/fbio.h>

#include <vm/vm.h>
#include <vm/pmap.h>

#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/fdt.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <dev/fb/fbreg.h>
#include <dev/vt/vt.h>

#include <arm/amlogic/aml8726/aml8726_fb.h>

#include "fb_if.h"


enum aml8726_fb_output {
	aml8726_unknown_fb_output,
	aml8726_cvbs_fb_output,
	aml8726_hdmi_fb_output,
	aml8726_lcd_fb_output
};

struct aml8726_fb_clk {
	uint32_t	freq;
	uint32_t	video_pre;
	uint32_t	video_post;
	uint32_t	video_x;
	uint32_t	hdmi_tx;
	uint32_t	encp;
	uint32_t	enci;
	uint32_t	enct;
	uint32_t	encl;
	uint32_t	vdac0;
	uint32_t	vdac1;
};

struct aml8726_fb_softc {
	device_t		dev;
	struct resource		*res[4];
	struct mtx		mtx;
	void			*ih_cookie;
	struct fb_info		info;
	enum aml8726_fb_output	output;
	struct aml8726_fb_clk	clk;
};

static struct resource_spec aml8726_fb_spec[] = {
	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },	/* CANVAS */
	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },	/* VIU */
	{ SYS_RES_MEMORY,	2,	RF_ACTIVE },	/* VPP */
	{ SYS_RES_IRQ,		1,	RF_ACTIVE },	/* INT_VIU_VSYNC */
	{ -1, 0 }
};

#define	AML_FB_LOCK(sc)			mtx_lock(&(sc)->mtx)
#define	AML_FB_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
#define	AML_FB_LOCK_INIT(sc)		\
    mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev),	\
    "fb", MTX_DEF)
#define	AML_FB_LOCK_DESTROY(sc)		mtx_destroy(&(sc)->mtx);

#define	CAV_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[0], reg, (val))
#define	CAV_READ_4(sc, reg)		bus_read_4((sc)->res[0], reg)
#define	CAV_BARRIER(sc, reg)		bus_barrier((sc)->res[0], reg, 4, \
    (BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE))

#define	VIU_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[1], reg, (val))
#define	VIU_READ_4(sc, reg)		bus_read_4((sc)->res[1], reg)

#define	VPP_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[2], reg, (val))
#define	VPP_READ_4(sc, reg)		bus_read_4((sc)->res[2], reg)

#define	CLK_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[X], reg, (val))
#define	CLK_READ_4(sc, reg)		bus_read_4((sc)->res[X], reg)

#define	AML_FB_CLK_FREQ_SD		1080
#define	AML_FB_CLK_FREQ_HD		1488

static void
aml8726_fb_cfg_output(struct aml8726_fb_softc *sc)
{
	/* XXX */
}

static void
aml8726_fb_cfg_video(struct aml8726_fb_softc *sc)
{
	uint32_t value;

	/*
	 * basic initialization
	 *
	 * The fifo depth is in units of 8 so programming 32
	 * sets the depth to 256.
	 */

	value = (32 << AML_VIU_OSD_FIFO_CTRL_DEPTH_SHIFT);
	value |= AML_VIU_OSD_FIFO_CTRL_BURST_LEN_64;
	value |= (4 << AML_VIU_OSD_FIFO_CTRL_HOLD_LINES_SHIFT);

	VIU_WRITE_4(sc, AML_VIU_OSD1_FIFO_CTRL_REG, value);
	VIU_WRITE_4(sc, AML_VIU_OSD2_FIFO_CTRL_REG, value);

	value = VPP_READ_4(sc, AML_VPP_MISC_REG);

	value &= ~AML_VPP_MISC_PREBLEND_EN;
	value |= AML_VPP_MISC_POSTBLEND_EN;
	value &= ~(AML_VPP_MISC_OSD1_POSTBLEND | AML_VPP_MISC_OSD2_POSTBLEND
	    | AML_VPP_MISC_VD1_POSTBLEND | AML_VPP_MISC_VD2_POSTBLEND);

	VPP_WRITE_4(sc, AML_VPP_MISC_REG, value);

	value = AML_VIU_OSD_CTRL_OSD_EN;
	value |= (0xff << AML_VIU_OSD_CTRL_GLOBAL_ALPHA_SHIFT);

	VIU_WRITE_4(sc, AML_VIU_OSD1_CTRL_REG, value);
	VIU_WRITE_4(sc, AML_VIU_OSD2_CTRL_REG, value);

	/* color mode for OSD1 block 0 */

	value = (AML_CAV_OSD1_INDEX << AML_VIU_OSD_BLK_CFG_W0_INDEX_SHIFT)
	    | AML_VIU_OSD_BLK_CFG_W0_LITTLE_ENDIAN
	    | AML_VIU_OSD_BLK_CFG_W0_BLKMODE_24
	    | AML_VIU_OSD_BLK_CFG_W0_RGB_EN
	    | AML_VIU_OSD_BLK_CFG_W0_CMATRIX_RGB;

	VIU_WRITE_4(sc, AML_VIU_OSD1_BLK0_CFG_W0_REG, value);

	/* geometry / scaling for OSD1 block 0 */

	value = ((sc->info.fb_width - 1) << AML_VIU_OSD_BLK_CFG_W1_X_END_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W1_X_END_MASK;
	value |= (0 << AML_VIU_OSD_BLK_CFG_W1_X_START_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W1_X_START_MASK;

	VIU_WRITE_4(sc, AML_VIU_OSD1_BLK0_CFG_W1_REG, value);

	value = ((sc->info.fb_height - 1) << AML_VIU_OSD_BLK_CFG_W2_Y_END_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W2_Y_END_MASK;
	value |= (0 << AML_VIU_OSD_BLK_CFG_W2_Y_START_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W2_Y_START_MASK;

	VIU_WRITE_4(sc, AML_VIU_OSD1_BLK0_CFG_W2_REG, value);

	value = ((sc->info.fb_width - 1) << AML_VIU_OSD_BLK_CFG_W3_H_END_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W3_H_END_MASK;
	value |= (0 << AML_VIU_OSD_BLK_CFG_W3_H_START_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W3_H_START_MASK;

	VIU_WRITE_4(sc, AML_VIU_OSD1_BLK0_CFG_W3_REG, value);

	value = ((sc->info.fb_height - 1) << AML_VIU_OSD_BLK_CFG_W4_V_END_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W4_V_END_MASK;
	value |= (0 << AML_VIU_OSD_BLK_CFG_W4_V_START_SHIFT)
	    & AML_VIU_OSD_BLK_CFG_W4_V_START_MASK;

	VIU_WRITE_4(sc, AML_VIU_OSD1_BLK0_CFG_W4_REG, value);

	/* Enable the OSD block now that it's fully configured */

	value = VIU_READ_4(sc, AML_VIU_OSD1_CTRL_REG);

	value &= ~AML_VIU_OSD_CTRL_OSD_BLK_EN_MASK;
	value |= 1 << AML_VIU_OSD_CTRL_OSD_BLK_EN_SHIFT;

	VIU_WRITE_4(sc, AML_VIU_OSD1_CTRL_REG, value);

	/* enable video processing of OSD1 */

	value = VPP_READ_4(sc, AML_VPP_MISC_REG);

	value |= AML_VPP_MISC_OSD1_POSTBLEND;

	VPP_WRITE_4(sc, AML_VPP_MISC_REG, value);
}

static void
aml8726_fb_cfg_canvas(struct aml8726_fb_softc *sc)
{
	uint32_t value;
	uint32_t width;

	/*
	 * The frame buffer address and width are programmed in units of 8
	 * (meaning they need to be aligned and the actual values divided
	 * by 8 prior to programming the hardware).
	 */

	width = (uint32_t)sc->info.fb_stride / 8;

	/* lower bits of the width */
	value = (width << AML_CAV_LUT_DATAL_WIDTH_SHIFT) &
	    AML_CAV_LUT_DATAL_WIDTH_MASK;

	/* physical address */
	value |= (uint32_t)sc->info.fb_pbase / 8;

	CAV_WRITE_4(sc, AML_CAV_LUT_DATAL_REG, value);

	/* upper bits of the width */
	value = ((width >> AML_CAV_LUT_DATAL_WIDTH_WIDTH) <<
	    AML_CAV_LUT_DATAH_WIDTH_SHIFT) & AML_CAV_LUT_DATAH_WIDTH_MASK;

	/* height */
	value |= ((uint32_t)sc->info.fb_height <<
	    AML_CAV_LUT_DATAH_HEIGHT_SHIFT) & AML_CAV_LUT_DATAH_HEIGHT_MASK;

	/* mode */
	value |= AML_CAV_LUT_DATAH_BLKMODE_LINEAR;

	CAV_WRITE_4(sc, AML_CAV_LUT_DATAH_REG, value);

	CAV_WRITE_4(sc, AML_CAV_LUT_ADDR_REG, (AML_CAV_LUT_ADDR_WR_EN |
	    (AML_CAV_OSD1_INDEX << AML_CAV_LUT_ADDR_INDEX_SHIFT)));

	CAV_BARRIER(sc, AML_CAV_LUT_ADDR_REG);
}

static void
aml8726_fb_intr(void *arg)
{
	struct aml8726_fb_softc *sc = (struct aml8726_fb_softc *)arg;

	AML_FB_LOCK(sc);

	AML_FB_UNLOCK(sc);
}

static int
aml8726_fb_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-fb"))
		return (ENXIO);

	device_set_desc(dev, "Amlogic aml8726 FB");

	return (BUS_PROBE_DEFAULT);
}

static int
aml8726_fb_attach(device_t dev)
{
	struct aml8726_fb_softc *sc = device_get_softc(dev);
	int error;
	device_t child;
	pcell_t prop;
	phandle_t node;

	sc->dev = dev;

	sc->info.fb_name = device_get_nameunit(sc->dev);

	node = ofw_bus_get_node(dev);

	if (OF_getencprop(node, "width", &prop, sizeof(prop)) <= 0) {
		device_printf(dev, "missing width attribute in FDT\n");
		return (ENXIO);
	}
	if ((prop % 8) != 0) {
		device_printf(dev,
		    "width attribute in FDT must be a multiple of 8\n");
		return (ENXIO);
	}
	sc->info.fb_width = prop;

	if (OF_getencprop(node, "height", &prop, sizeof(prop)) <= 0) {
		device_printf(dev, "missing height attribute in FDT\n");
		return (ENXIO);
	}
	sc->info.fb_height = prop;

	if (OF_getencprop(node, "depth", &prop, sizeof(prop)) <= 0) {
		device_printf(dev, "missing depth attribute in FDT\n");
		return (ENXIO);
	}
	if (prop != 24) {
		device_printf(dev,
		    "depth attribute in FDT is an unsupported value\n");
		return (ENXIO);
	}
	sc->info.fb_depth = prop;
	sc->info.fb_bpp = prop;

	if (OF_getencprop(node, "linebytes", &prop, sizeof(prop)) <= 0) {
		device_printf(dev, "missing linebytes attribute in FDT\n");
		return (ENXIO);
	}
	if ((prop % 8) != 0) {
		device_printf(dev,
		    "linebytes attribute in FDT must be a multiple of 8\n");
		return (ENXIO);
	}
	if (prop < (sc->info.fb_width * 3)) {
		device_printf(dev,
		    "linebytes attribute in FDT is too small\n");
		return (ENXIO);
	}
	sc->info.fb_stride = prop;

	if (OF_getencprop(node, "address", &prop, sizeof(prop)) <= 0) {
		device_printf(dev, "missing address attribute in FDT\n");
		return (ENXIO);
	}
	if ((prop % 8) != 0) {
		device_printf(dev,
		    "address attribute in FDT must be a multiple of 8\n");
		return (ENXIO);
	}
	sc->info.fb_pbase = prop;
	sc->info.fb_size = sc->info.fb_height * sc->info.fb_stride;
	sc->info.fb_vbase = (intptr_t)pmap_mapdev(sc->info.fb_pbase,
	    sc->info.fb_size);

	if (bus_alloc_resources(dev, aml8726_fb_spec, sc->res)) {
		device_printf(dev, "could not allocate resources for device\n");
		pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
		return (ENXIO);
	}

	aml8726_fb_cfg_output(sc);

	aml8726_fb_cfg_video(sc);

	aml8726_fb_cfg_canvas(sc);

	AML_FB_LOCK_INIT(sc);

	error = bus_setup_intr(dev, sc->res[3], INTR_TYPE_MISC | INTR_MPSAFE,
	    NULL, aml8726_fb_intr, sc, &sc->ih_cookie);

	if (error) {
		device_printf(dev, "could not setup interrupt handler\n");
		goto fail;
	}

	child = device_add_child(dev, "fbd", device_get_unit(dev));

	if (!child) {
		device_printf(dev, "could not add fbd\n");
		error = ENXIO;
		goto fail;
	}

	error = device_probe_and_attach(child);

	if (error) {
		device_printf(dev, "could not attach fbd\n");
		goto fail;
	}

	return (0);

fail:
	if (sc->ih_cookie)
		bus_teardown_intr(dev, sc->res[3], sc->ih_cookie);

	AML_FB_LOCK_DESTROY(sc);

	bus_release_resources(dev, aml8726_fb_spec, sc->res);

	pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);

	return (error);
}

static int
aml8726_fb_detach(device_t dev)
{
	struct aml8726_fb_softc *sc = device_get_softc(dev);

	bus_generic_detach(dev);

	bus_teardown_intr(dev, sc->res[3], sc->ih_cookie);

	AML_FB_LOCK_DESTROY(sc);

	bus_release_resources(dev, aml8726_fb_spec, sc->res);

	pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);

	return (0);
}

static struct fb_info *
aml8726_fb_getinfo(device_t dev)
{
	struct aml8726_fb_softc *sc = device_get_softc(dev);

	return (&sc->info);
}

static device_method_t aml8726_fb_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		aml8726_fb_probe),
	DEVMETHOD(device_attach,	aml8726_fb_attach),
	DEVMETHOD(device_detach,	aml8726_fb_detach),

	/* FB interface */
	DEVMETHOD(fb_getinfo,		aml8726_fb_getinfo),

	DEVMETHOD_END
};

static driver_t aml8726_fb_driver = {
	"fb",
	aml8726_fb_methods,
	sizeof(struct aml8726_fb_softc),
};

static devclass_t aml8726_fb_devclass;

DRIVER_MODULE(fb, simplebus, aml8726_fb_driver, aml8726_fb_devclass, 0, 0);
OpenPOWER on IntegriCloud