1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright 2018 IBM Corporation 3 4 #include <linux/clk.h> 5 #include <linux/dma-mapping.h> 6 #include <linux/irq.h> 7 #include <linux/mfd/syscon.h> 8 #include <linux/module.h> 9 #include <linux/of.h> 10 #include <linux/of_device.h> 11 #include <linux/of_reserved_mem.h> 12 #include <linux/platform_device.h> 13 #include <linux/regmap.h> 14 #include <linux/reset.h> 15 16 #include <drm/drm_atomic_helper.h> 17 #include <drm/drm_device.h> 18 #include <drm/drm_fbdev_dma.h> 19 #include <drm/drm_gem_dma_helper.h> 20 #include <drm/drm_gem_framebuffer_helper.h> 21 #include <drm/drm_module.h> 22 #include <drm/drm_probe_helper.h> 23 #include <drm/drm_simple_kms_helper.h> 24 #include <drm/drm_vblank.h> 25 #include <drm/drm_drv.h> 26 27 #include "aspeed_gfx.h" 28 29 /** 30 * DOC: ASPEED GFX Driver 31 * 32 * This driver is for the ASPEED BMC SoC's 'GFX' display hardware, also called 33 * the 'SOC Display Controller' in the datasheet. This driver runs on the ARM 34 * based BMC systems, unlike the ast driver which runs on a host CPU and is for 35 * a PCIe graphics device. 36 * 37 * The AST2500 supports a total of 3 output paths: 38 * 39 * 1. VGA output, the output target can choose either or both to the DAC 40 * or DVO interface. 41 * 42 * 2. Graphics CRT output, the output target can choose either or both to 43 * the DAC or DVO interface. 44 * 45 * 3. Video input from DVO, the video input can be used for video engine 46 * capture or DAC display output. 47 * 48 * Output options are selected in SCU2C. 49 * 50 * The "VGA mode" device is the PCI attached controller. The "Graphics CRT" 51 * is the ARM's internal display controller. 52 * 53 * The driver only supports a simple configuration consisting of a 40MHz 54 * pixel clock, fixed by hardware limitations, and the VGA output path. 55 * 56 * The driver was written with the 'AST2500 Software Programming Guide' v17, 57 * which is available under NDA from ASPEED. 58 */ 59 60 struct aspeed_gfx_config { 61 u32 dac_reg; /* DAC register in SCU */ 62 u32 int_clear_reg; /* Interrupt clear register */ 63 u32 vga_scratch_reg; /* VGA scratch register in SCU */ 64 u32 throd_val; /* Default Threshold Seting */ 65 u32 scan_line_max; /* Max memory size of one scan line */ 66 }; 67 68 static const struct aspeed_gfx_config ast2400_config = { 69 .dac_reg = 0x2c, 70 .int_clear_reg = 0x60, 71 .vga_scratch_reg = 0x50, 72 .throd_val = CRT_THROD_LOW(0x1e) | CRT_THROD_HIGH(0x12), 73 .scan_line_max = 64, 74 }; 75 76 static const struct aspeed_gfx_config ast2500_config = { 77 .dac_reg = 0x2c, 78 .int_clear_reg = 0x60, 79 .vga_scratch_reg = 0x50, 80 .throd_val = CRT_THROD_LOW(0x24) | CRT_THROD_HIGH(0x3c), 81 .scan_line_max = 128, 82 }; 83 84 static const struct aspeed_gfx_config ast2600_config = { 85 .dac_reg = 0xc0, 86 .int_clear_reg = 0x68, 87 .vga_scratch_reg = 0x50, 88 .throd_val = CRT_THROD_LOW(0x50) | CRT_THROD_HIGH(0x70), 89 .scan_line_max = 128, 90 }; 91 92 static const struct of_device_id aspeed_gfx_match[] = { 93 { .compatible = "aspeed,ast2400-gfx", .data = &ast2400_config }, 94 { .compatible = "aspeed,ast2500-gfx", .data = &ast2500_config }, 95 { .compatible = "aspeed,ast2600-gfx", .data = &ast2600_config }, 96 { }, 97 }; 98 MODULE_DEVICE_TABLE(of, aspeed_gfx_match); 99 100 static const struct drm_mode_config_funcs aspeed_gfx_mode_config_funcs = { 101 .fb_create = drm_gem_fb_create, 102 .atomic_check = drm_atomic_helper_check, 103 .atomic_commit = drm_atomic_helper_commit, 104 }; 105 106 static int aspeed_gfx_setup_mode_config(struct drm_device *drm) 107 { 108 int ret; 109 110 ret = drmm_mode_config_init(drm); 111 if (ret) 112 return ret; 113 114 drm->mode_config.min_width = 0; 115 drm->mode_config.min_height = 0; 116 drm->mode_config.max_width = 800; 117 drm->mode_config.max_height = 600; 118 drm->mode_config.funcs = &aspeed_gfx_mode_config_funcs; 119 120 return ret; 121 } 122 123 static irqreturn_t aspeed_gfx_irq_handler(int irq, void *data) 124 { 125 struct drm_device *drm = data; 126 struct aspeed_gfx *priv = to_aspeed_gfx(drm); 127 u32 reg; 128 129 reg = readl(priv->base + CRT_CTRL1); 130 131 if (reg & CRT_CTRL_VERTICAL_INTR_STS) { 132 drm_crtc_handle_vblank(&priv->pipe.crtc); 133 writel(reg, priv->base + priv->int_clr_reg); 134 return IRQ_HANDLED; 135 } 136 137 return IRQ_NONE; 138 } 139 140 static int aspeed_gfx_load(struct drm_device *drm) 141 { 142 struct platform_device *pdev = to_platform_device(drm->dev); 143 struct aspeed_gfx *priv = to_aspeed_gfx(drm); 144 struct device_node *np = pdev->dev.of_node; 145 const struct aspeed_gfx_config *config; 146 const struct of_device_id *match; 147 struct resource *res; 148 int ret; 149 150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 151 priv->base = devm_ioremap_resource(drm->dev, res); 152 if (IS_ERR(priv->base)) 153 return PTR_ERR(priv->base); 154 155 match = of_match_device(aspeed_gfx_match, &pdev->dev); 156 if (!match) 157 return -EINVAL; 158 config = match->data; 159 160 priv->dac_reg = config->dac_reg; 161 priv->int_clr_reg = config->int_clear_reg; 162 priv->vga_scratch_reg = config->vga_scratch_reg; 163 priv->throd_val = config->throd_val; 164 priv->scan_line_max = config->scan_line_max; 165 166 priv->scu = syscon_regmap_lookup_by_phandle(np, "syscon"); 167 if (IS_ERR(priv->scu)) { 168 priv->scu = syscon_regmap_lookup_by_compatible("aspeed,ast2500-scu"); 169 if (IS_ERR(priv->scu)) { 170 dev_err(&pdev->dev, "failed to find SCU regmap\n"); 171 return PTR_ERR(priv->scu); 172 } 173 } 174 175 ret = of_reserved_mem_device_init(drm->dev); 176 if (ret) { 177 dev_err(&pdev->dev, 178 "failed to initialize reserved mem: %d\n", ret); 179 return ret; 180 } 181 182 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)); 183 if (ret) { 184 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", ret); 185 return ret; 186 } 187 188 priv->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL); 189 if (IS_ERR(priv->rst)) { 190 dev_err(&pdev->dev, 191 "missing or invalid reset controller device tree entry"); 192 return PTR_ERR(priv->rst); 193 } 194 reset_control_deassert(priv->rst); 195 196 priv->clk = devm_clk_get(drm->dev, NULL); 197 if (IS_ERR(priv->clk)) { 198 dev_err(&pdev->dev, 199 "missing or invalid clk device tree entry"); 200 return PTR_ERR(priv->clk); 201 } 202 clk_prepare_enable(priv->clk); 203 204 /* Sanitize control registers */ 205 writel(0, priv->base + CRT_CTRL1); 206 writel(0, priv->base + CRT_CTRL2); 207 208 ret = aspeed_gfx_setup_mode_config(drm); 209 if (ret < 0) 210 return ret; 211 212 ret = drm_vblank_init(drm, 1); 213 if (ret < 0) { 214 dev_err(drm->dev, "Failed to initialise vblank\n"); 215 return ret; 216 } 217 218 ret = aspeed_gfx_create_output(drm); 219 if (ret < 0) { 220 dev_err(drm->dev, "Failed to create outputs\n"); 221 return ret; 222 } 223 224 ret = aspeed_gfx_create_pipe(drm); 225 if (ret < 0) { 226 dev_err(drm->dev, "Cannot setup simple display pipe\n"); 227 return ret; 228 } 229 230 ret = devm_request_irq(drm->dev, platform_get_irq(pdev, 0), 231 aspeed_gfx_irq_handler, 0, "aspeed gfx", drm); 232 if (ret < 0) { 233 dev_err(drm->dev, "Failed to install IRQ handler\n"); 234 return ret; 235 } 236 237 drm_mode_config_reset(drm); 238 239 return 0; 240 } 241 242 static void aspeed_gfx_unload(struct drm_device *drm) 243 { 244 drm_kms_helper_poll_fini(drm); 245 } 246 247 DEFINE_DRM_GEM_DMA_FOPS(fops); 248 249 static const struct drm_driver aspeed_gfx_driver = { 250 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 251 DRM_GEM_DMA_DRIVER_OPS, 252 .fops = &fops, 253 .name = "aspeed-gfx-drm", 254 .desc = "ASPEED GFX DRM", 255 .date = "20180319", 256 .major = 1, 257 .minor = 0, 258 }; 259 260 static ssize_t dac_mux_store(struct device *dev, struct device_attribute *attr, 261 const char *buf, size_t count) 262 { 263 struct aspeed_gfx *priv = dev_get_drvdata(dev); 264 u32 val; 265 int rc; 266 267 rc = kstrtou32(buf, 0, &val); 268 if (rc) 269 return rc; 270 271 if (val > 3) 272 return -EINVAL; 273 274 rc = regmap_update_bits(priv->scu, priv->dac_reg, 0x30000, val << 16); 275 if (rc < 0) 276 return 0; 277 278 return count; 279 } 280 281 static ssize_t dac_mux_show(struct device *dev, struct device_attribute *attr, char *buf) 282 { 283 struct aspeed_gfx *priv = dev_get_drvdata(dev); 284 u32 reg; 285 int rc; 286 287 rc = regmap_read(priv->scu, priv->dac_reg, ®); 288 if (rc) 289 return rc; 290 291 return sprintf(buf, "%u\n", (reg >> 16) & 0x3); 292 } 293 static DEVICE_ATTR_RW(dac_mux); 294 295 static ssize_t 296 vga_pw_show(struct device *dev, struct device_attribute *attr, char *buf) 297 { 298 struct aspeed_gfx *priv = dev_get_drvdata(dev); 299 u32 reg; 300 int rc; 301 302 rc = regmap_read(priv->scu, priv->vga_scratch_reg, ®); 303 if (rc) 304 return rc; 305 306 return sprintf(buf, "%u\n", reg); 307 } 308 static DEVICE_ATTR_RO(vga_pw); 309 310 static struct attribute *aspeed_sysfs_entries[] = { 311 &dev_attr_vga_pw.attr, 312 &dev_attr_dac_mux.attr, 313 NULL, 314 }; 315 316 static struct attribute_group aspeed_sysfs_attr_group = { 317 .attrs = aspeed_sysfs_entries, 318 }; 319 320 static int aspeed_gfx_probe(struct platform_device *pdev) 321 { 322 struct aspeed_gfx *priv; 323 int ret; 324 325 priv = devm_drm_dev_alloc(&pdev->dev, &aspeed_gfx_driver, 326 struct aspeed_gfx, drm); 327 if (IS_ERR(priv)) 328 return PTR_ERR(priv); 329 330 ret = aspeed_gfx_load(&priv->drm); 331 if (ret) 332 return ret; 333 334 platform_set_drvdata(pdev, priv); 335 336 ret = sysfs_create_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group); 337 if (ret) 338 return ret; 339 340 ret = drm_dev_register(&priv->drm, 0); 341 if (ret) 342 goto err_unload; 343 344 drm_fbdev_dma_setup(&priv->drm, 32); 345 return 0; 346 347 err_unload: 348 sysfs_remove_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group); 349 aspeed_gfx_unload(&priv->drm); 350 351 return ret; 352 } 353 354 static void aspeed_gfx_remove(struct platform_device *pdev) 355 { 356 struct drm_device *drm = platform_get_drvdata(pdev); 357 358 sysfs_remove_group(&pdev->dev.kobj, &aspeed_sysfs_attr_group); 359 drm_dev_unregister(drm); 360 aspeed_gfx_unload(drm); 361 drm_atomic_helper_shutdown(drm); 362 } 363 364 static void aspeed_gfx_shutdown(struct platform_device *pdev) 365 { 366 drm_atomic_helper_shutdown(platform_get_drvdata(pdev)); 367 } 368 369 static struct platform_driver aspeed_gfx_platform_driver = { 370 .probe = aspeed_gfx_probe, 371 .remove_new = aspeed_gfx_remove, 372 .shutdown = aspeed_gfx_shutdown, 373 .driver = { 374 .name = "aspeed_gfx", 375 .of_match_table = aspeed_gfx_match, 376 }, 377 }; 378 379 drm_module_platform_driver(aspeed_gfx_platform_driver); 380 381 MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>"); 382 MODULE_DESCRIPTION("ASPEED BMC DRM/KMS driver"); 383 MODULE_LICENSE("GPL"); 384