mali_dp.c 10.7 KB
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2016-2018 ARM Ltd.
 * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
 *
 */
#define DEBUG
#include <common.h>
#include <malloc.h>
#include <video.h>
#include <dm.h>
#ifdef CONFIG_DISPLAY
#include <display.h>
#endif
#include <fdtdec.h>
#include <asm/io.h>
#include <os.h>
#include <fdt_support.h>
#include <clk.h>
#include <dm/device_compat.h>
#include <linux/sizes.h>

#define MALIDP_CORE_ID		0x0018
#define MALIDP_REG_BG_COLOR	0x0044
#define MALIDP_LAYER_LV1	0x0100
#define MALIDP_DC_STATUS	0xc000
#define MALIDP_DC_CONTROL	0xc010
#define MALIDP_DC_CFG_VALID	0xc014

/* offsets inside the modesetting register block */
#define MALIDP_H_INTERVALS	0x0000
#define MALIDP_V_INTERVALS	0x0004
#define MALIDP_SYNC_CONTROL	0x0008
#define MALIDP_HV_ACTIVESIZE	0x000c
#define MALIDP_OUTPUT_DEPTH	0x001c

/* offsets inside the layer register block */
#define MALIDP_LAYER_FORMAT	0x0000
#define MALIDP_LAYER_CONTROL	0x0004
#define MALIDP_LAYER_IN_SIZE	0x000c
#define MALIDP_LAYER_CMP_SIZE	0x0010
#define MALIDP_LAYER_STRIDE	0x0018
#define MALIDP_LAYER_PTR_LOW	0x0024
#define MALIDP_LAYER_PTR_HIGH	0x0028

/* offsets inside the IRQ control blocks */
#define MALIDP_REG_MASKIRQ	0x0008
#define MALIDP_REG_CLEARIRQ	0x000c

#define M1BITS	0x0001
#define M2BITS	0x0003
#define M4BITS	0x000f
#define M8BITS	0x00ff
#define M10BITS	0x03ff
#define M12BITS	0x0fff
#define M13BITS	0x1fff
#define M16BITS	0xffff
#define M17BITS	0x1ffff

#define MALIDP_H_FRONTPORCH(x)	(((x) & M12BITS) << 0)
#define MALIDP_H_BACKPORCH(x)	(((x) & M10BITS) << 16)
#define MALIDP_V_FRONTPORCH(x)	(((x) & M12BITS) << 0)
#define MALIDP_V_BACKPORCH(x)	(((x) & M8BITS) << 16)
#define MALIDP_H_SYNCWIDTH(x)	(((x) & M10BITS) << 0)
#define MALIDP_V_SYNCWIDTH(x)	(((x) & M8BITS) << 16)
#define MALIDP_H_ACTIVE(x)	(((x) & M13BITS) << 0)
#define MALIDP_V_ACTIVE(x)	(((x) & M13BITS) << 16)

#define MALIDP_CMP_V_SIZE(x)	(((x) & M13BITS) << 16)
#define MALIDP_CMP_H_SIZE(x)	(((x) & M13BITS) << 0)

#define MALIDP_IN_V_SIZE(x)	(((x) & M13BITS) << 16)
#define MALIDP_IN_H_SIZE(x)	(((x) & M13BITS) << 0)

#define MALIDP_DC_CM_CONTROL(x)	((x) & M1BITS) << 16, 1 << 16
#define MALIDP_DC_STATUS_GET_CM(reg) (((reg) >> 16) & M1BITS)

#define MALIDP_FORMAT_ARGB8888	0x08
#define MALIDP_DEFAULT_BG_R 0x0
#define MALIDP_DEFAULT_BG_G 0x0
#define MALIDP_DEFAULT_BG_B 0x0

#define MALIDP_PRODUCT_ID(core_id)	((u32)(core_id) >> 16)

#define MALIDP500	0x500

DECLARE_GLOBAL_DATA_PTR;

struct malidp_priv {
	phys_addr_t base_addr;
	phys_addr_t dc_status_addr;
	phys_addr_t dc_control_addr;
	phys_addr_t cval_addr;
	struct udevice *display;	/* display device attached */
	struct clk aclk;
	struct clk pxlclk;
	u16 modeset_regs_offset;
	u8 config_bit_shift;
	u8 clear_irq;			/* offset for IRQ clear register */
};

static const struct video_ops malidp_ops = {
};

static int malidp_get_hwid(phys_addr_t base_addr)
{
	int hwid;

	/*
	 * reading from the old CORE_ID offset will always
	 * return 0x5000000 on DP500
	 */
	hwid = readl(base_addr + MALIDP_CORE_ID);
	if (MALIDP_PRODUCT_ID(hwid) == MALIDP500)
		return hwid;
	/* otherwise try the other gen CORE_ID offset */
	hwid = readl(base_addr + MALIDP_DC_STATUS + MALIDP_CORE_ID);

	return hwid;
}

/*
 * wait for config mode bit setup to be acted upon by the hardware
 */
static int malidp_wait_configdone(struct malidp_priv *malidp)
{
	u32 status, tries = 300;

	while (tries--) {
		status = readl(malidp->dc_status_addr);
		if ((status >> malidp->config_bit_shift) & 1)
			break;
		udelay(500);
	}

	if (!tries)
		return -ETIMEDOUT;

	return 0;
}

/*
 * signal the hardware to enter configuration mode
 */
static int malidp_enter_config(struct malidp_priv *malidp)
{
	setbits_le32(malidp->dc_control_addr, 1 << malidp->config_bit_shift);
	return malidp_wait_configdone(malidp);
}

/*
 * signal the hardware to exit configuration mode
 */
static int malidp_leave_config(struct malidp_priv *malidp)
{
	clrbits_le32(malidp->dc_control_addr, 1 << malidp->config_bit_shift);
	return malidp_wait_configdone(malidp);
}

static void malidp_setup_timings(struct malidp_priv *malidp,
				 struct display_timing *timings)
{
	u32 val = MALIDP_H_SYNCWIDTH(timings->hsync_len.typ) |
		  MALIDP_V_SYNCWIDTH(timings->vsync_len.typ);
	writel(val, malidp->base_addr + malidp->modeset_regs_offset +
	       MALIDP_SYNC_CONTROL);
	val = MALIDP_H_BACKPORCH(timings->hback_porch.typ) |
		MALIDP_H_FRONTPORCH(timings->hfront_porch.typ);
	writel(val, malidp->base_addr + malidp->modeset_regs_offset +
	       MALIDP_H_INTERVALS);
	val = MALIDP_V_BACKPORCH(timings->vback_porch.typ) |
		MALIDP_V_FRONTPORCH(timings->vfront_porch.typ);
	writel(val, malidp->base_addr + malidp->modeset_regs_offset +
	       MALIDP_V_INTERVALS);
	val = MALIDP_H_ACTIVE(timings->hactive.typ) |
		MALIDP_V_ACTIVE(timings->vactive.typ);
	writel(val, malidp->base_addr + malidp->modeset_regs_offset +
	       MALIDP_HV_ACTIVESIZE);
	/* default output bit-depth per colour is 8 bits */
	writel(0x080808, malidp->base_addr + malidp->modeset_regs_offset +
	       MALIDP_OUTPUT_DEPTH);
}

static int malidp_setup_mode(struct malidp_priv *malidp,
			     struct display_timing *timings)
{
	int err;

	if (clk_set_rate(&malidp->pxlclk, timings->pixelclock.typ) == 0)
		return -EIO;

	malidp_setup_timings(malidp, timings);

	err = display_enable(malidp->display, 8, timings);
	if (err)
		printf("display_enable failed with %d\n", err);

	return err;
}

static void malidp_setup_layer(struct malidp_priv *malidp,
			       struct display_timing *timings,
			       u32 layer_offset, phys_addr_t fb_addr)
{
	u32 val;

	/* setup the base layer's pixel format to A8R8G8B8 */
	writel(MALIDP_FORMAT_ARGB8888, malidp->base_addr + layer_offset +
	       MALIDP_LAYER_FORMAT);
	/* setup layer composition size */
	val = MALIDP_CMP_V_SIZE(timings->vactive.typ) |
		MALIDP_CMP_H_SIZE(timings->hactive.typ);
	writel(val, malidp->base_addr + layer_offset +
	       MALIDP_LAYER_CMP_SIZE);
	/* setup layer input size */
	val = MALIDP_IN_V_SIZE(timings->vactive.typ) |
		MALIDP_IN_H_SIZE(timings->hactive.typ);
	writel(val, malidp->base_addr + layer_offset + MALIDP_LAYER_IN_SIZE);
	/* setup layer stride in bytes */
	writel(timings->hactive.typ << 2, malidp->base_addr + layer_offset +
	       MALIDP_LAYER_STRIDE);
	/* set framebuffer address */
	writel(lower_32_bits(fb_addr), malidp->base_addr + layer_offset +
	       MALIDP_LAYER_PTR_LOW);
	writel(upper_32_bits(fb_addr), malidp->base_addr + layer_offset +
	       MALIDP_LAYER_PTR_HIGH);
	/* enable layer */
	setbits_le32(malidp->base_addr + layer_offset +
		     MALIDP_LAYER_CONTROL, 1);
}

static void malidp_set_configvalid(struct malidp_priv *malidp)
{
	setbits_le32(malidp->cval_addr, 1);
}

static int malidp_update_timings_from_edid(struct udevice *dev,
					   struct display_timing *timings)
{
#ifdef CONFIG_DISPLAY
	struct malidp_priv *priv = dev_get_priv(dev);
	struct udevice *disp_dev;
	int err;

	err = uclass_first_device(UCLASS_DISPLAY, &disp_dev);
	if (err)
		return err;

	priv->display = disp_dev;

	err = display_read_timing(disp_dev, timings);
	if (err)
		return err;

#endif
	return 0;
}

static int malidp_probe(struct udevice *dev)
{
	struct video_priv *uc_priv = dev_get_uclass_priv(dev);
	struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
	ofnode framebuffer = ofnode_find_subnode(dev_ofnode(dev), "framebuffer");
	struct malidp_priv *priv = dev_get_priv(dev);
	struct display_timing timings;
	phys_addr_t fb_base, fb_size;
	const char *format;
	u32 value;
	int err;

	if (!ofnode_valid(framebuffer))
		return -EINVAL;

	err = clk_get_by_name(dev, "pxlclk", &priv->pxlclk);
	if (err) {
		dev_err(dev, "failed to get pixel clock\n");
		return err;
	}
	err = clk_get_by_name(dev, "aclk", &priv->aclk);
	if (err) {
		dev_err(dev, "failed to get AXI clock\n");
		goto fail_aclk;
	}

	err = ofnode_decode_display_timing(dev_ofnode(dev), 1, &timings);
	if (err) {
		dev_err(dev, "failed to get any display timings\n");
		goto fail_timings;
	}

	err = malidp_update_timings_from_edid(dev, &timings);
	if (err) {
		printf("malidp_update_timings_from_edid failed: %d\n", err);
		goto fail_timings;
	}

	fb_base = ofnode_get_addr_size(framebuffer, "reg", &fb_size);
	if (fb_base != FDT_ADDR_T_NONE) {
		uc_plat->base = fb_base;
		uc_plat->size = fb_size;
	} else {
		printf("cannot get address size for framebuffer\n");
	}

	err = ofnode_read_u32(framebuffer, "width", &value);
	if (err)
		goto fail_timings;
	uc_priv->xsize = (ushort)value;

	err = ofnode_read_u32(framebuffer, "height", &value);
	if (err)
		goto fail_timings;
	uc_priv->ysize = (ushort)value;

	format = ofnode_read_string(framebuffer, "format");
	if (!format) {
		err = -EINVAL;
		goto fail_timings;
	} else if (!strncmp(format, "a8r8g8b8", 8)) {
		uc_priv->bpix = VIDEO_BPP32;
	}

	uc_priv->rot = 0;
	priv->base_addr = (phys_addr_t)dev_read_addr(dev);

	clk_enable(&priv->pxlclk);
	clk_enable(&priv->aclk);

	value = malidp_get_hwid(priv->base_addr);
	printf("Display: Arm Mali DP%3x r%dp%d\n", MALIDP_PRODUCT_ID(value),
	       (value >> 12) & 0xf, (value >> 8) & 0xf);

	if (MALIDP_PRODUCT_ID(value) == MALIDP500) {
		/* DP500 is special */
		priv->modeset_regs_offset = 0x28;
		priv->dc_status_addr = priv->base_addr;
		priv->dc_control_addr = priv->base_addr + 0xc;
		priv->cval_addr = priv->base_addr + 0xf00;
		priv->config_bit_shift = 17;
		priv->clear_irq = 0;
	} else {
		priv->modeset_regs_offset = 0x30;
		priv->dc_status_addr = priv->base_addr + MALIDP_DC_STATUS;
		priv->dc_control_addr = priv->base_addr + MALIDP_DC_CONTROL;
		priv->cval_addr = priv->base_addr + MALIDP_DC_CFG_VALID;
		priv->config_bit_shift = 16;
		priv->clear_irq = MALIDP_REG_CLEARIRQ;
	}

	/* enter config mode */
	err  = malidp_enter_config(priv);
	if (err)
		return err;

	/* disable interrupts */
	writel(0, priv->dc_status_addr + MALIDP_REG_MASKIRQ);
	writel(0xffffffff, priv->dc_status_addr + priv->clear_irq);

	err = malidp_setup_mode(priv, &timings);
	if (err)
		goto fail_timings;

	malidp_setup_layer(priv, &timings, MALIDP_LAYER_LV1,
			   (phys_addr_t)uc_plat->base);

	err = malidp_leave_config(priv);
	if (err)
		goto fail_timings;

	malidp_set_configvalid(priv);

	return 0;

fail_timings:
	clk_free(&priv->aclk);
fail_aclk:
	clk_free(&priv->pxlclk);

	return err;
}

static int malidp_bind(struct udevice *dev)
{
	struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);

	/* choose max possible size: 2K x 2K, XRGB888 framebuffer */
	uc_plat->size = 4 * 2048 * 2048;

	return 0;
}

static const struct udevice_id malidp_ids[] = {
	{ .compatible = "arm,mali-dp500" },
	{ .compatible = "arm,mali-dp550" },
	{ .compatible = "arm,mali-dp650" },
	{ }
};

U_BOOT_DRIVER(mali_dp) = {
	.name		= "mali_dp",
	.id		= UCLASS_VIDEO,
	.of_match	= malidp_ids,
	.bind		= malidp_bind,
	.probe		= malidp_probe,
	.priv_auto_alloc_size	= sizeof(struct malidp_priv),
	.ops		= &malidp_ops,
};