1 /* Hisilicon Hibmc SoC drm driver 2 * 3 * Based on the bochs drm driver. 4 * 5 * Copyright (c) 2016 Huawei Limited. 6 * 7 * Author: 8 * Rongrong Zou <zourongrong@huawei.com> 9 * Rongrong Zou <zourongrong@gmail.com> 10 * Jianhua Li <lijianhua@huawei.com> 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 */ 18 19 #include <linux/console.h> 20 #include <linux/module.h> 21 22 #include <drm/drm_atomic_helper.h> 23 #include <drm/drm_probe_helper.h> 24 25 #include "hibmc_drm_drv.h" 26 #include "hibmc_drm_regs.h" 27 28 static const struct file_operations hibmc_fops = { 29 .owner = THIS_MODULE, 30 DRM_VRAM_MM_FILE_OPERATIONS 31 }; 32 33 static irqreturn_t hibmc_drm_interrupt(int irq, void *arg) 34 { 35 struct drm_device *dev = (struct drm_device *)arg; 36 struct hibmc_drm_private *priv = 37 (struct hibmc_drm_private *)dev->dev_private; 38 u32 status; 39 40 status = readl(priv->mmio + HIBMC_RAW_INTERRUPT); 41 42 if (status & HIBMC_RAW_INTERRUPT_VBLANK(1)) { 43 writel(HIBMC_RAW_INTERRUPT_VBLANK(1), 44 priv->mmio + HIBMC_RAW_INTERRUPT); 45 drm_handle_vblank(dev, 0); 46 } 47 48 return IRQ_HANDLED; 49 } 50 51 static struct drm_driver hibmc_driver = { 52 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 53 .fops = &hibmc_fops, 54 .name = "hibmc", 55 .date = "20160828", 56 .desc = "hibmc drm driver", 57 .major = 1, 58 .minor = 0, 59 .gem_free_object_unlocked = 60 drm_gem_vram_driver_gem_free_object_unlocked, 61 .dumb_create = hibmc_dumb_create, 62 .dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset, 63 .irq_handler = hibmc_drm_interrupt, 64 }; 65 66 static int __maybe_unused hibmc_pm_suspend(struct device *dev) 67 { 68 struct pci_dev *pdev = to_pci_dev(dev); 69 struct drm_device *drm_dev = pci_get_drvdata(pdev); 70 71 return drm_mode_config_helper_suspend(drm_dev); 72 } 73 74 static int __maybe_unused hibmc_pm_resume(struct device *dev) 75 { 76 struct pci_dev *pdev = to_pci_dev(dev); 77 struct drm_device *drm_dev = pci_get_drvdata(pdev); 78 79 return drm_mode_config_helper_resume(drm_dev); 80 } 81 82 static const struct dev_pm_ops hibmc_pm_ops = { 83 SET_SYSTEM_SLEEP_PM_OPS(hibmc_pm_suspend, 84 hibmc_pm_resume) 85 }; 86 87 static int hibmc_kms_init(struct hibmc_drm_private *priv) 88 { 89 int ret; 90 91 drm_mode_config_init(priv->dev); 92 priv->mode_config_initialized = true; 93 94 priv->dev->mode_config.min_width = 0; 95 priv->dev->mode_config.min_height = 0; 96 priv->dev->mode_config.max_width = 1920; 97 priv->dev->mode_config.max_height = 1440; 98 99 priv->dev->mode_config.fb_base = priv->fb_base; 100 priv->dev->mode_config.preferred_depth = 24; 101 priv->dev->mode_config.prefer_shadow = 0; 102 103 priv->dev->mode_config.funcs = (void *)&hibmc_mode_funcs; 104 105 ret = hibmc_de_init(priv); 106 if (ret) { 107 DRM_ERROR("failed to init de: %d\n", ret); 108 return ret; 109 } 110 111 ret = hibmc_vdac_init(priv); 112 if (ret) { 113 DRM_ERROR("failed to init vdac: %d\n", ret); 114 return ret; 115 } 116 117 return 0; 118 } 119 120 static void hibmc_kms_fini(struct hibmc_drm_private *priv) 121 { 122 if (priv->mode_config_initialized) { 123 drm_mode_config_cleanup(priv->dev); 124 priv->mode_config_initialized = false; 125 } 126 } 127 128 /* 129 * It can operate in one of three modes: 0, 1 or Sleep. 130 */ 131 void hibmc_set_power_mode(struct hibmc_drm_private *priv, 132 unsigned int power_mode) 133 { 134 unsigned int control_value = 0; 135 void __iomem *mmio = priv->mmio; 136 unsigned int input = 1; 137 138 if (power_mode > HIBMC_PW_MODE_CTL_MODE_SLEEP) 139 return; 140 141 if (power_mode == HIBMC_PW_MODE_CTL_MODE_SLEEP) 142 input = 0; 143 144 control_value = readl(mmio + HIBMC_POWER_MODE_CTRL); 145 control_value &= ~(HIBMC_PW_MODE_CTL_MODE_MASK | 146 HIBMC_PW_MODE_CTL_OSC_INPUT_MASK); 147 control_value |= HIBMC_FIELD(HIBMC_PW_MODE_CTL_MODE, power_mode); 148 control_value |= HIBMC_FIELD(HIBMC_PW_MODE_CTL_OSC_INPUT, input); 149 writel(control_value, mmio + HIBMC_POWER_MODE_CTRL); 150 } 151 152 void hibmc_set_current_gate(struct hibmc_drm_private *priv, unsigned int gate) 153 { 154 unsigned int gate_reg; 155 unsigned int mode; 156 void __iomem *mmio = priv->mmio; 157 158 /* Get current power mode. */ 159 mode = (readl(mmio + HIBMC_POWER_MODE_CTRL) & 160 HIBMC_PW_MODE_CTL_MODE_MASK) >> HIBMC_PW_MODE_CTL_MODE_SHIFT; 161 162 switch (mode) { 163 case HIBMC_PW_MODE_CTL_MODE_MODE0: 164 gate_reg = HIBMC_MODE0_GATE; 165 break; 166 167 case HIBMC_PW_MODE_CTL_MODE_MODE1: 168 gate_reg = HIBMC_MODE1_GATE; 169 break; 170 171 default: 172 gate_reg = HIBMC_MODE0_GATE; 173 break; 174 } 175 writel(gate, mmio + gate_reg); 176 } 177 178 static void hibmc_hw_config(struct hibmc_drm_private *priv) 179 { 180 unsigned int reg; 181 182 /* On hardware reset, power mode 0 is default. */ 183 hibmc_set_power_mode(priv, HIBMC_PW_MODE_CTL_MODE_MODE0); 184 185 /* Enable display power gate & LOCALMEM power gate*/ 186 reg = readl(priv->mmio + HIBMC_CURRENT_GATE); 187 reg &= ~HIBMC_CURR_GATE_DISPLAY_MASK; 188 reg &= ~HIBMC_CURR_GATE_LOCALMEM_MASK; 189 reg |= HIBMC_CURR_GATE_DISPLAY(1); 190 reg |= HIBMC_CURR_GATE_LOCALMEM(1); 191 192 hibmc_set_current_gate(priv, reg); 193 194 /* 195 * Reset the memory controller. If the memory controller 196 * is not reset in chip,the system might hang when sw accesses 197 * the memory.The memory should be resetted after 198 * changing the MXCLK. 199 */ 200 reg = readl(priv->mmio + HIBMC_MISC_CTRL); 201 reg &= ~HIBMC_MSCCTL_LOCALMEM_RESET_MASK; 202 reg |= HIBMC_MSCCTL_LOCALMEM_RESET(0); 203 writel(reg, priv->mmio + HIBMC_MISC_CTRL); 204 205 reg &= ~HIBMC_MSCCTL_LOCALMEM_RESET_MASK; 206 reg |= HIBMC_MSCCTL_LOCALMEM_RESET(1); 207 208 writel(reg, priv->mmio + HIBMC_MISC_CTRL); 209 } 210 211 static int hibmc_hw_map(struct hibmc_drm_private *priv) 212 { 213 struct drm_device *dev = priv->dev; 214 struct pci_dev *pdev = dev->pdev; 215 resource_size_t addr, size, ioaddr, iosize; 216 217 ioaddr = pci_resource_start(pdev, 1); 218 iosize = pci_resource_len(pdev, 1); 219 priv->mmio = devm_ioremap_nocache(dev->dev, ioaddr, iosize); 220 if (!priv->mmio) { 221 DRM_ERROR("Cannot map mmio region\n"); 222 return -ENOMEM; 223 } 224 225 addr = pci_resource_start(pdev, 0); 226 size = pci_resource_len(pdev, 0); 227 priv->fb_map = devm_ioremap(dev->dev, addr, size); 228 if (!priv->fb_map) { 229 DRM_ERROR("Cannot map framebuffer\n"); 230 return -ENOMEM; 231 } 232 priv->fb_base = addr; 233 priv->fb_size = size; 234 235 return 0; 236 } 237 238 static int hibmc_hw_init(struct hibmc_drm_private *priv) 239 { 240 int ret; 241 242 ret = hibmc_hw_map(priv); 243 if (ret) 244 return ret; 245 246 hibmc_hw_config(priv); 247 248 return 0; 249 } 250 251 static int hibmc_unload(struct drm_device *dev) 252 { 253 struct hibmc_drm_private *priv = dev->dev_private; 254 255 hibmc_fbdev_fini(priv); 256 257 drm_atomic_helper_shutdown(dev); 258 259 if (dev->irq_enabled) 260 drm_irq_uninstall(dev); 261 if (priv->msi_enabled) 262 pci_disable_msi(dev->pdev); 263 264 hibmc_kms_fini(priv); 265 hibmc_mm_fini(priv); 266 dev->dev_private = NULL; 267 return 0; 268 } 269 270 static int hibmc_load(struct drm_device *dev) 271 { 272 struct hibmc_drm_private *priv; 273 int ret; 274 275 priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL); 276 if (!priv) { 277 DRM_ERROR("no memory to allocate for hibmc_drm_private\n"); 278 return -ENOMEM; 279 } 280 dev->dev_private = priv; 281 priv->dev = dev; 282 283 ret = hibmc_hw_init(priv); 284 if (ret) 285 goto err; 286 287 ret = hibmc_mm_init(priv); 288 if (ret) 289 goto err; 290 291 ret = hibmc_kms_init(priv); 292 if (ret) 293 goto err; 294 295 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 296 if (ret) { 297 DRM_ERROR("failed to initialize vblank: %d\n", ret); 298 goto err; 299 } 300 301 priv->msi_enabled = 0; 302 ret = pci_enable_msi(dev->pdev); 303 if (ret) { 304 DRM_WARN("enabling MSI failed: %d\n", ret); 305 } else { 306 priv->msi_enabled = 1; 307 ret = drm_irq_install(dev, dev->pdev->irq); 308 if (ret) 309 DRM_WARN("install irq failed: %d\n", ret); 310 } 311 312 /* reset all the states of crtc/plane/encoder/connector */ 313 drm_mode_config_reset(dev); 314 315 ret = hibmc_fbdev_init(priv); 316 if (ret) { 317 DRM_ERROR("failed to initialize fbdev: %d\n", ret); 318 goto err; 319 } 320 321 return 0; 322 323 err: 324 hibmc_unload(dev); 325 DRM_ERROR("failed to initialize drm driver: %d\n", ret); 326 return ret; 327 } 328 329 static int hibmc_pci_probe(struct pci_dev *pdev, 330 const struct pci_device_id *ent) 331 { 332 struct drm_device *dev; 333 int ret; 334 335 dev = drm_dev_alloc(&hibmc_driver, &pdev->dev); 336 if (IS_ERR(dev)) { 337 DRM_ERROR("failed to allocate drm_device\n"); 338 return PTR_ERR(dev); 339 } 340 341 dev->pdev = pdev; 342 pci_set_drvdata(pdev, dev); 343 344 ret = pci_enable_device(pdev); 345 if (ret) { 346 DRM_ERROR("failed to enable pci device: %d\n", ret); 347 goto err_free; 348 } 349 350 ret = hibmc_load(dev); 351 if (ret) { 352 DRM_ERROR("failed to load hibmc: %d\n", ret); 353 goto err_disable; 354 } 355 356 ret = drm_dev_register(dev, 0); 357 if (ret) { 358 DRM_ERROR("failed to register drv for userspace access: %d\n", 359 ret); 360 goto err_unload; 361 } 362 return 0; 363 364 err_unload: 365 hibmc_unload(dev); 366 err_disable: 367 pci_disable_device(pdev); 368 err_free: 369 drm_dev_put(dev); 370 371 return ret; 372 } 373 374 static void hibmc_pci_remove(struct pci_dev *pdev) 375 { 376 struct drm_device *dev = pci_get_drvdata(pdev); 377 378 drm_dev_unregister(dev); 379 hibmc_unload(dev); 380 drm_dev_put(dev); 381 } 382 383 static struct pci_device_id hibmc_pci_table[] = { 384 { PCI_VDEVICE(HUAWEI, 0x1711) }, 385 {0,} 386 }; 387 388 static struct pci_driver hibmc_pci_driver = { 389 .name = "hibmc-drm", 390 .id_table = hibmc_pci_table, 391 .probe = hibmc_pci_probe, 392 .remove = hibmc_pci_remove, 393 .driver.pm = &hibmc_pm_ops, 394 }; 395 396 static int __init hibmc_init(void) 397 { 398 return pci_register_driver(&hibmc_pci_driver); 399 } 400 401 static void __exit hibmc_exit(void) 402 { 403 return pci_unregister_driver(&hibmc_pci_driver); 404 } 405 406 module_init(hibmc_init); 407 module_exit(hibmc_exit); 408 409 MODULE_DEVICE_TABLE(pci, hibmc_pci_table); 410 MODULE_AUTHOR("RongrongZou <zourongrong@huawei.com>"); 411 MODULE_DESCRIPTION("DRM Driver for Hisilicon Hibmc"); 412 MODULE_LICENSE("GPL v2"); 413