xref: /linux/drivers/gpu/drm/aspeed/aspeed_gfx_drv.c (revision 92614ad540171382d856e178cd2073377f2d1a7c)
14f2a8f58SJoel Stanley // SPDX-License-Identifier: GPL-2.0+
24f2a8f58SJoel Stanley // Copyright 2018 IBM Corporation
34f2a8f58SJoel Stanley 
44f2a8f58SJoel Stanley #include <linux/clk.h>
54f2a8f58SJoel Stanley #include <linux/dma-mapping.h>
64f2a8f58SJoel Stanley #include <linux/irq.h>
74f2a8f58SJoel Stanley #include <linux/mfd/syscon.h>
84f2a8f58SJoel Stanley #include <linux/module.h>
94f2a8f58SJoel Stanley #include <linux/of.h>
104f2a8f58SJoel Stanley #include <linux/of_reserved_mem.h>
114f2a8f58SJoel Stanley #include <linux/platform_device.h>
124f2a8f58SJoel Stanley #include <linux/regmap.h>
134f2a8f58SJoel Stanley #include <linux/reset.h>
144f2a8f58SJoel Stanley 
154f2a8f58SJoel Stanley #include <drm/drm_atomic_helper.h>
164f2a8f58SJoel Stanley #include <drm/drm_crtc_helper.h>
174f2a8f58SJoel Stanley #include <drm/drm_device.h>
184f2a8f58SJoel Stanley #include <drm/drm_fb_cma_helper.h>
194f2a8f58SJoel Stanley #include <drm/drm_fb_helper.h>
204f2a8f58SJoel Stanley #include <drm/drm_gem_cma_helper.h>
214f2a8f58SJoel Stanley #include <drm/drm_gem_framebuffer_helper.h>
224f2a8f58SJoel Stanley #include <drm/drm_probe_helper.h>
234f2a8f58SJoel Stanley #include <drm/drm_simple_kms_helper.h>
244f2a8f58SJoel Stanley #include <drm/drm_vblank.h>
254f2a8f58SJoel Stanley #include <drm/drm_drv.h>
264f2a8f58SJoel Stanley 
274f2a8f58SJoel Stanley #include "aspeed_gfx.h"
284f2a8f58SJoel Stanley 
294f2a8f58SJoel Stanley /**
304f2a8f58SJoel Stanley  * DOC: ASPEED GFX Driver
314f2a8f58SJoel Stanley  *
324f2a8f58SJoel Stanley  * This driver is for the ASPEED BMC SoC's 'GFX' display hardware, also called
334f2a8f58SJoel Stanley  * the 'SOC Display Controller' in the datasheet. This driver runs on the ARM
344f2a8f58SJoel Stanley  * based BMC systems, unlike the ast driver which runs on a host CPU and is for
354f2a8f58SJoel Stanley  * a PCIe graphics device.
364f2a8f58SJoel Stanley  *
374f2a8f58SJoel Stanley  * The AST2500 supports a total of 3 output paths:
384f2a8f58SJoel Stanley  *
394f2a8f58SJoel Stanley  *   1. VGA output, the output target can choose either or both to the DAC
404f2a8f58SJoel Stanley  *   or DVO interface.
414f2a8f58SJoel Stanley  *
424f2a8f58SJoel Stanley  *   2. Graphics CRT output, the output target can choose either or both to
434f2a8f58SJoel Stanley  *   the DAC or DVO interface.
444f2a8f58SJoel Stanley  *
454f2a8f58SJoel Stanley  *   3. Video input from DVO, the video input can be used for video engine
464f2a8f58SJoel Stanley  *   capture or DAC display output.
474f2a8f58SJoel Stanley  *
484f2a8f58SJoel Stanley  * Output options are selected in SCU2C.
494f2a8f58SJoel Stanley  *
504f2a8f58SJoel Stanley  * The "VGA mode" device is the PCI attached controller. The "Graphics CRT"
514f2a8f58SJoel Stanley  * is the ARM's internal display controller.
524f2a8f58SJoel Stanley  *
534f2a8f58SJoel Stanley  * The driver only supports a simple configuration consisting of a 40MHz
544f2a8f58SJoel Stanley  * pixel clock, fixed by hardware limitations, and the VGA output path.
554f2a8f58SJoel Stanley  *
564f2a8f58SJoel Stanley  * The driver was written with the 'AST2500 Software Programming Guide' v17,
574f2a8f58SJoel Stanley  * which is available under NDA from ASPEED.
584f2a8f58SJoel Stanley  */
594f2a8f58SJoel Stanley 
604f2a8f58SJoel Stanley static const struct drm_mode_config_funcs aspeed_gfx_mode_config_funcs = {
614f2a8f58SJoel Stanley 	.fb_create		= drm_gem_fb_create,
624f2a8f58SJoel Stanley 	.atomic_check		= drm_atomic_helper_check,
634f2a8f58SJoel Stanley 	.atomic_commit		= drm_atomic_helper_commit,
644f2a8f58SJoel Stanley };
654f2a8f58SJoel Stanley 
66c2c25c1cSDaniel Vetter static int aspeed_gfx_setup_mode_config(struct drm_device *drm)
674f2a8f58SJoel Stanley {
68c2c25c1cSDaniel Vetter 	int ret;
69c2c25c1cSDaniel Vetter 
70c2c25c1cSDaniel Vetter 	ret = drmm_mode_config_init(drm);
71c2c25c1cSDaniel Vetter 	if (ret)
72c2c25c1cSDaniel Vetter 		return ret;
734f2a8f58SJoel Stanley 
744f2a8f58SJoel Stanley 	drm->mode_config.min_width = 0;
754f2a8f58SJoel Stanley 	drm->mode_config.min_height = 0;
764f2a8f58SJoel Stanley 	drm->mode_config.max_width = 800;
774f2a8f58SJoel Stanley 	drm->mode_config.max_height = 600;
784f2a8f58SJoel Stanley 	drm->mode_config.funcs = &aspeed_gfx_mode_config_funcs;
79c2c25c1cSDaniel Vetter 
80c2c25c1cSDaniel Vetter 	return ret;
814f2a8f58SJoel Stanley }
824f2a8f58SJoel Stanley 
834f2a8f58SJoel Stanley static irqreturn_t aspeed_gfx_irq_handler(int irq, void *data)
844f2a8f58SJoel Stanley {
854f2a8f58SJoel Stanley 	struct drm_device *drm = data;
86cd829454SDaniel Vetter 	struct aspeed_gfx *priv = to_aspeed_gfx(drm);
874f2a8f58SJoel Stanley 	u32 reg;
884f2a8f58SJoel Stanley 
894f2a8f58SJoel Stanley 	reg = readl(priv->base + CRT_CTRL1);
904f2a8f58SJoel Stanley 
914f2a8f58SJoel Stanley 	if (reg & CRT_CTRL_VERTICAL_INTR_STS) {
924f2a8f58SJoel Stanley 		drm_crtc_handle_vblank(&priv->pipe.crtc);
934f2a8f58SJoel Stanley 		writel(reg, priv->base + CRT_CTRL1);
944f2a8f58SJoel Stanley 		return IRQ_HANDLED;
954f2a8f58SJoel Stanley 	}
964f2a8f58SJoel Stanley 
974f2a8f58SJoel Stanley 	return IRQ_NONE;
984f2a8f58SJoel Stanley }
994f2a8f58SJoel Stanley 
1004f2a8f58SJoel Stanley 
1014f2a8f58SJoel Stanley 
1024f2a8f58SJoel Stanley static int aspeed_gfx_load(struct drm_device *drm)
1034f2a8f58SJoel Stanley {
1044f2a8f58SJoel Stanley 	struct platform_device *pdev = to_platform_device(drm->dev);
105cd829454SDaniel Vetter 	struct aspeed_gfx *priv = to_aspeed_gfx(drm);
106*92614ad5SJoel Stanley 	struct device_node *np = pdev->dev.of_node;
1074f2a8f58SJoel Stanley 	struct resource *res;
1084f2a8f58SJoel Stanley 	int ret;
1094f2a8f58SJoel Stanley 
1104f2a8f58SJoel Stanley 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1114f2a8f58SJoel Stanley 	priv->base = devm_ioremap_resource(drm->dev, res);
1124f2a8f58SJoel Stanley 	if (IS_ERR(priv->base))
1134f2a8f58SJoel Stanley 		return PTR_ERR(priv->base);
1144f2a8f58SJoel Stanley 
115*92614ad5SJoel Stanley 	priv->scu = syscon_regmap_lookup_by_phandle(np, "syscon");
116*92614ad5SJoel Stanley 	if (IS_ERR(priv->scu)) {
1174f2a8f58SJoel Stanley 		priv->scu = syscon_regmap_lookup_by_compatible("aspeed,ast2500-scu");
1184f2a8f58SJoel Stanley 		if (IS_ERR(priv->scu)) {
1194f2a8f58SJoel Stanley 			dev_err(&pdev->dev, "failed to find SCU regmap\n");
1204f2a8f58SJoel Stanley 			return PTR_ERR(priv->scu);
1214f2a8f58SJoel Stanley 		}
122*92614ad5SJoel Stanley 	}
1234f2a8f58SJoel Stanley 
1244f2a8f58SJoel Stanley 	ret = of_reserved_mem_device_init(drm->dev);
1254f2a8f58SJoel Stanley 	if (ret) {
1264f2a8f58SJoel Stanley 		dev_err(&pdev->dev,
1274f2a8f58SJoel Stanley 			"failed to initialize reserved mem: %d\n", ret);
1284f2a8f58SJoel Stanley 		return ret;
1294f2a8f58SJoel Stanley 	}
1304f2a8f58SJoel Stanley 
1314f2a8f58SJoel Stanley 	ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
1324f2a8f58SJoel Stanley 	if (ret) {
1334f2a8f58SJoel Stanley 		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", ret);
1344f2a8f58SJoel Stanley 		return ret;
1354f2a8f58SJoel Stanley 	}
1364f2a8f58SJoel Stanley 
1374f2a8f58SJoel Stanley 	priv->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1384f2a8f58SJoel Stanley 	if (IS_ERR(priv->rst)) {
1394f2a8f58SJoel Stanley 		dev_err(&pdev->dev,
1404f2a8f58SJoel Stanley 			"missing or invalid reset controller device tree entry");
1414f2a8f58SJoel Stanley 		return PTR_ERR(priv->rst);
1424f2a8f58SJoel Stanley 	}
1434f2a8f58SJoel Stanley 	reset_control_deassert(priv->rst);
1444f2a8f58SJoel Stanley 
1454f2a8f58SJoel Stanley 	priv->clk = devm_clk_get(drm->dev, NULL);
1464f2a8f58SJoel Stanley 	if (IS_ERR(priv->clk)) {
1474f2a8f58SJoel Stanley 		dev_err(&pdev->dev,
1484f2a8f58SJoel Stanley 			"missing or invalid clk device tree entry");
1494f2a8f58SJoel Stanley 		return PTR_ERR(priv->clk);
1504f2a8f58SJoel Stanley 	}
1514f2a8f58SJoel Stanley 	clk_prepare_enable(priv->clk);
1524f2a8f58SJoel Stanley 
1534f2a8f58SJoel Stanley 	/* Sanitize control registers */
1544f2a8f58SJoel Stanley 	writel(0, priv->base + CRT_CTRL1);
1554f2a8f58SJoel Stanley 	writel(0, priv->base + CRT_CTRL2);
1564f2a8f58SJoel Stanley 
157c2c25c1cSDaniel Vetter 	ret = aspeed_gfx_setup_mode_config(drm);
158c2c25c1cSDaniel Vetter 	if (ret < 0)
159c2c25c1cSDaniel Vetter 		return ret;
1604f2a8f58SJoel Stanley 
1614f2a8f58SJoel Stanley 	ret = drm_vblank_init(drm, 1);
1624f2a8f58SJoel Stanley 	if (ret < 0) {
1634f2a8f58SJoel Stanley 		dev_err(drm->dev, "Failed to initialise vblank\n");
1644f2a8f58SJoel Stanley 		return ret;
1654f2a8f58SJoel Stanley 	}
1664f2a8f58SJoel Stanley 
1674f2a8f58SJoel Stanley 	ret = aspeed_gfx_create_output(drm);
1684f2a8f58SJoel Stanley 	if (ret < 0) {
1694f2a8f58SJoel Stanley 		dev_err(drm->dev, "Failed to create outputs\n");
1704f2a8f58SJoel Stanley 		return ret;
1714f2a8f58SJoel Stanley 	}
1724f2a8f58SJoel Stanley 
1734f2a8f58SJoel Stanley 	ret = aspeed_gfx_create_pipe(drm);
1744f2a8f58SJoel Stanley 	if (ret < 0) {
1754f2a8f58SJoel Stanley 		dev_err(drm->dev, "Cannot setup simple display pipe\n");
1764f2a8f58SJoel Stanley 		return ret;
1774f2a8f58SJoel Stanley 	}
1784f2a8f58SJoel Stanley 
1794f2a8f58SJoel Stanley 	ret = devm_request_irq(drm->dev, platform_get_irq(pdev, 0),
1804f2a8f58SJoel Stanley 			       aspeed_gfx_irq_handler, 0, "aspeed gfx", drm);
1814f2a8f58SJoel Stanley 	if (ret < 0) {
1824f2a8f58SJoel Stanley 		dev_err(drm->dev, "Failed to install IRQ handler\n");
1834f2a8f58SJoel Stanley 		return ret;
1844f2a8f58SJoel Stanley 	}
1854f2a8f58SJoel Stanley 
1864f2a8f58SJoel Stanley 	drm_mode_config_reset(drm);
1874f2a8f58SJoel Stanley 
1884f2a8f58SJoel Stanley 	return 0;
1894f2a8f58SJoel Stanley }
1904f2a8f58SJoel Stanley 
1914f2a8f58SJoel Stanley static void aspeed_gfx_unload(struct drm_device *drm)
1924f2a8f58SJoel Stanley {
1934f2a8f58SJoel Stanley 	drm_kms_helper_poll_fini(drm);
1944f2a8f58SJoel Stanley }
1954f2a8f58SJoel Stanley 
1964f2a8f58SJoel Stanley DEFINE_DRM_GEM_CMA_FOPS(fops);
1974f2a8f58SJoel Stanley 
19870a59dd8SDaniel Vetter static const struct drm_driver aspeed_gfx_driver = {
1990424fdafSDaniel Vetter 	.driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
200178c7235SThomas Zimmermann 	DRM_GEM_CMA_DRIVER_OPS,
2014f2a8f58SJoel Stanley 	.fops = &fops,
2024f2a8f58SJoel Stanley 	.name = "aspeed-gfx-drm",
2034f2a8f58SJoel Stanley 	.desc = "ASPEED GFX DRM",
2044f2a8f58SJoel Stanley 	.date = "20180319",
2054f2a8f58SJoel Stanley 	.major = 1,
2064f2a8f58SJoel Stanley 	.minor = 0,
2074f2a8f58SJoel Stanley };
2084f2a8f58SJoel Stanley 
2094f2a8f58SJoel Stanley static const struct of_device_id aspeed_gfx_match[] = {
2104f2a8f58SJoel Stanley 	{ .compatible = "aspeed,ast2500-gfx" },
2114f2a8f58SJoel Stanley 	{ }
2124f2a8f58SJoel Stanley };
2134f2a8f58SJoel Stanley 
214696029ebSJoel Stanley #define ASPEED_SCU_VGA0		0x50
215696029ebSJoel Stanley #define ASPEED_SCU_MISC_CTRL	0x2c
216696029ebSJoel Stanley 
217696029ebSJoel Stanley static ssize_t dac_mux_store(struct device *dev, struct device_attribute *attr,
218696029ebSJoel Stanley 			     const char *buf, size_t count)
219696029ebSJoel Stanley {
220696029ebSJoel Stanley 	struct aspeed_gfx *priv = dev_get_drvdata(dev);
221696029ebSJoel Stanley 	u32 val;
222696029ebSJoel Stanley 	int rc;
223696029ebSJoel Stanley 
224696029ebSJoel Stanley 	rc = kstrtou32(buf, 0, &val);
225696029ebSJoel Stanley 	if (rc)
226696029ebSJoel Stanley 		return rc;
227696029ebSJoel Stanley 
228696029ebSJoel Stanley 	if (val > 3)
229696029ebSJoel Stanley 		return -EINVAL;
230696029ebSJoel Stanley 
231696029ebSJoel Stanley 	rc = regmap_update_bits(priv->scu, ASPEED_SCU_MISC_CTRL, 0x30000, val << 16);
232696029ebSJoel Stanley 	if (rc < 0)
233696029ebSJoel Stanley 		return 0;
234696029ebSJoel Stanley 
235696029ebSJoel Stanley 	return count;
236696029ebSJoel Stanley }
237696029ebSJoel Stanley 
238696029ebSJoel Stanley static ssize_t dac_mux_show(struct device *dev, struct device_attribute *attr, char *buf)
239696029ebSJoel Stanley {
240696029ebSJoel Stanley 	struct aspeed_gfx *priv = dev_get_drvdata(dev);
241696029ebSJoel Stanley 	u32 reg;
242696029ebSJoel Stanley 	int rc;
243696029ebSJoel Stanley 
244696029ebSJoel Stanley 	rc = regmap_read(priv->scu, ASPEED_SCU_MISC_CTRL, &reg);
245696029ebSJoel Stanley 	if (rc)
246696029ebSJoel Stanley 		return rc;
247696029ebSJoel Stanley 
248696029ebSJoel Stanley 	return sprintf(buf, "%u\n", (reg >> 16) & 0x3);
249696029ebSJoel Stanley }
250696029ebSJoel Stanley static DEVICE_ATTR_RW(dac_mux);
251696029ebSJoel Stanley 
252696029ebSJoel Stanley static ssize_t
253696029ebSJoel Stanley vga_pw_show(struct device *dev, struct device_attribute *attr, char *buf)
254696029ebSJoel Stanley {
255696029ebSJoel Stanley 	struct aspeed_gfx *priv = dev_get_drvdata(dev);
256696029ebSJoel Stanley 	u32 reg;
257696029ebSJoel Stanley 	int rc;
258696029ebSJoel Stanley 
259696029ebSJoel Stanley 	rc = regmap_read(priv->scu, ASPEED_SCU_VGA0, &reg);
260696029ebSJoel Stanley 	if (rc)
261696029ebSJoel Stanley 		return rc;
262696029ebSJoel Stanley 
263696029ebSJoel Stanley 	return sprintf(buf, "%u\n", reg & 1);
264696029ebSJoel Stanley }
265696029ebSJoel Stanley static DEVICE_ATTR_RO(vga_pw);
266696029ebSJoel Stanley 
267696029ebSJoel Stanley static struct attribute *aspeed_sysfs_entries[] = {
268696029ebSJoel Stanley 	&dev_attr_vga_pw.attr,
269696029ebSJoel Stanley 	&dev_attr_dac_mux.attr,
270696029ebSJoel Stanley 	NULL,
271696029ebSJoel Stanley };
272696029ebSJoel Stanley 
273696029ebSJoel Stanley static struct attribute_group aspeed_sysfs_attr_group = {
274696029ebSJoel Stanley 	.attrs = aspeed_sysfs_entries,
275696029ebSJoel Stanley };
276696029ebSJoel Stanley 
2774f2a8f58SJoel Stanley static int aspeed_gfx_probe(struct platform_device *pdev)
2784f2a8f58SJoel Stanley {
279cd829454SDaniel Vetter 	struct aspeed_gfx *priv;
2804f2a8f58SJoel Stanley 	int ret;
2814f2a8f58SJoel Stanley 
282cd829454SDaniel Vetter 	priv = devm_drm_dev_alloc(&pdev->dev, &aspeed_gfx_driver,
283cd829454SDaniel Vetter 				  struct aspeed_gfx, drm);
284cd829454SDaniel Vetter 	if (IS_ERR(priv))
285cd829454SDaniel Vetter 		return PTR_ERR(priv);
2864f2a8f58SJoel Stanley 
287cd829454SDaniel Vetter 	ret = aspeed_gfx_load(&priv->drm);
2884f2a8f58SJoel Stanley 	if (ret)
289cd829454SDaniel Vetter 		return ret;
2904f2a8f58SJoel Stanley 
291696029ebSJoel Stanley 	dev_set_drvdata(&pdev->dev, priv);
292696029ebSJoel Stanley 
293696029ebSJoel Stanley 	ret = sysfs_create_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group);
294696029ebSJoel Stanley 	if (ret)
295696029ebSJoel Stanley 		return ret;
296696029ebSJoel Stanley 
297cd829454SDaniel Vetter 	ret = drm_dev_register(&priv->drm, 0);
2984f2a8f58SJoel Stanley 	if (ret)
2994f2a8f58SJoel Stanley 		goto err_unload;
3004f2a8f58SJoel Stanley 
30122493574SGuenter Roeck 	drm_fbdev_generic_setup(&priv->drm, 32);
3024f2a8f58SJoel Stanley 	return 0;
3034f2a8f58SJoel Stanley 
3044f2a8f58SJoel Stanley err_unload:
305696029ebSJoel Stanley 	sysfs_remove_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group);
306cd829454SDaniel Vetter 	aspeed_gfx_unload(&priv->drm);
3074f2a8f58SJoel Stanley 
3084f2a8f58SJoel Stanley 	return ret;
3094f2a8f58SJoel Stanley }
3104f2a8f58SJoel Stanley 
3114f2a8f58SJoel Stanley static int aspeed_gfx_remove(struct platform_device *pdev)
3124f2a8f58SJoel Stanley {
3134f2a8f58SJoel Stanley 	struct drm_device *drm = platform_get_drvdata(pdev);
3144f2a8f58SJoel Stanley 
315696029ebSJoel Stanley 	sysfs_remove_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group);
3164f2a8f58SJoel Stanley 	drm_dev_unregister(drm);
3174f2a8f58SJoel Stanley 	aspeed_gfx_unload(drm);
3184f2a8f58SJoel Stanley 
3194f2a8f58SJoel Stanley 	return 0;
3204f2a8f58SJoel Stanley }
3214f2a8f58SJoel Stanley 
3224f2a8f58SJoel Stanley static struct platform_driver aspeed_gfx_platform_driver = {
3234f2a8f58SJoel Stanley 	.probe		= aspeed_gfx_probe,
3244f2a8f58SJoel Stanley 	.remove		= aspeed_gfx_remove,
3254f2a8f58SJoel Stanley 	.driver = {
3264f2a8f58SJoel Stanley 		.name = "aspeed_gfx",
3274f2a8f58SJoel Stanley 		.of_match_table = aspeed_gfx_match,
3284f2a8f58SJoel Stanley 	},
3294f2a8f58SJoel Stanley };
3304f2a8f58SJoel Stanley 
3314f2a8f58SJoel Stanley module_platform_driver(aspeed_gfx_platform_driver);
3324f2a8f58SJoel Stanley 
3334f2a8f58SJoel Stanley MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
3344f2a8f58SJoel Stanley MODULE_DESCRIPTION("ASPEED BMC DRM/KMS driver");
3354f2a8f58SJoel Stanley MODULE_LICENSE("GPL");
336