1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2012 Red Hat 4 * 5 * Authors: Matthew Garrett 6 * Dave Airlie 7 */ 8 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 12 #include <drm/drm_aperture.h> 13 #include <drm/drm_drv.h> 14 #include <drm/drm_file.h> 15 #include <drm/drm_ioctl.h> 16 #include <drm/drm_managed.h> 17 #include <drm/drm_module.h> 18 #include <drm/drm_pciids.h> 19 20 #include "mgag200_drv.h" 21 22 int mgag200_modeset = -1; 23 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 24 module_param_named(modeset, mgag200_modeset, int, 0400); 25 26 int mgag200_init_pci_options(struct pci_dev *pdev, u32 option, u32 option2) 27 { 28 struct device *dev = &pdev->dev; 29 int err; 30 31 err = pci_write_config_dword(pdev, PCI_MGA_OPTION, option); 32 if (err != PCIBIOS_SUCCESSFUL) { 33 dev_err(dev, "pci_write_config_dword(PCI_MGA_OPTION) failed: %d\n", err); 34 return pcibios_err_to_errno(err); 35 } 36 37 err = pci_write_config_dword(pdev, PCI_MGA_OPTION2, option2); 38 if (err != PCIBIOS_SUCCESSFUL) { 39 dev_err(dev, "pci_write_config_dword(PCI_MGA_OPTION2) failed: %d\n", err); 40 return pcibios_err_to_errno(err); 41 } 42 43 return 0; 44 } 45 46 resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size) 47 { 48 int offset; 49 int orig; 50 int test1, test2; 51 int orig1, orig2; 52 size_t vram_size; 53 54 /* Probe */ 55 orig = ioread16(mem); 56 iowrite16(0, mem); 57 58 vram_size = size; 59 60 for (offset = 0x100000; offset < vram_size; offset += 0x4000) { 61 orig1 = ioread8(mem + offset); 62 orig2 = ioread8(mem + offset + 0x100); 63 64 iowrite16(0xaa55, mem + offset); 65 iowrite16(0xaa55, mem + offset + 0x100); 66 67 test1 = ioread16(mem + offset); 68 test2 = ioread16(mem); 69 70 iowrite16(orig1, mem + offset); 71 iowrite16(orig2, mem + offset + 0x100); 72 73 if (test1 != 0xaa55) 74 break; 75 76 if (test2) 77 break; 78 } 79 80 iowrite16(orig, mem); 81 82 return offset - 65536; 83 } 84 85 /* 86 * DRM driver 87 */ 88 89 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops); 90 91 static const struct drm_driver mgag200_driver = { 92 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET, 93 .fops = &mgag200_driver_fops, 94 .name = DRIVER_NAME, 95 .desc = DRIVER_DESC, 96 .date = DRIVER_DATE, 97 .major = DRIVER_MAJOR, 98 .minor = DRIVER_MINOR, 99 .patchlevel = DRIVER_PATCHLEVEL, 100 DRM_GEM_SHMEM_DRIVER_OPS, 101 }; 102 103 /* 104 * DRM device 105 */ 106 107 resource_size_t mgag200_device_probe_vram(struct mga_device *mdev) 108 { 109 return mgag200_probe_vram(mdev->vram, resource_size(mdev->vram_res)); 110 } 111 112 int mgag200_device_preinit(struct mga_device *mdev) 113 { 114 struct drm_device *dev = &mdev->base; 115 struct pci_dev *pdev = to_pci_dev(dev->dev); 116 resource_size_t start, len; 117 struct resource *res; 118 119 /* BAR 1 contains registers */ 120 121 start = pci_resource_start(pdev, 1); 122 len = pci_resource_len(pdev, 1); 123 124 res = devm_request_mem_region(dev->dev, start, len, "mgadrmfb_mmio"); 125 if (!res) { 126 drm_err(dev, "devm_request_mem_region(MMIO) failed\n"); 127 return -ENXIO; 128 } 129 mdev->rmmio_res = res; 130 131 mdev->rmmio = pcim_iomap(pdev, 1, 0); 132 if (!mdev->rmmio) 133 return -ENOMEM; 134 135 /* BAR 0 is VRAM */ 136 137 start = pci_resource_start(pdev, 0); 138 len = pci_resource_len(pdev, 0); 139 140 res = devm_request_mem_region(dev->dev, start, len, "mgadrmfb_vram"); 141 if (!res) { 142 drm_err(dev, "devm_request_mem_region(VRAM) failed\n"); 143 return -ENXIO; 144 } 145 mdev->vram_res = res; 146 147 /* Don't fail on errors, but performance might be reduced. */ 148 devm_arch_io_reserve_memtype_wc(dev->dev, res->start, resource_size(res)); 149 devm_arch_phys_wc_add(dev->dev, res->start, resource_size(res)); 150 151 mdev->vram = devm_ioremap(dev->dev, res->start, resource_size(res)); 152 if (!mdev->vram) 153 return -ENOMEM; 154 155 return 0; 156 } 157 158 int mgag200_device_init(struct mga_device *mdev, 159 const struct mgag200_device_info *info, 160 const struct mgag200_device_funcs *funcs) 161 { 162 struct drm_device *dev = &mdev->base; 163 u8 crtcext3, misc; 164 int ret; 165 166 mdev->info = info; 167 mdev->funcs = funcs; 168 169 ret = drmm_mutex_init(dev, &mdev->rmmio_lock); 170 if (ret) 171 return ret; 172 173 mutex_lock(&mdev->rmmio_lock); 174 175 RREG_ECRT(0x03, crtcext3); 176 crtcext3 |= MGAREG_CRTCEXT3_MGAMODE; 177 WREG_ECRT(0x03, crtcext3); 178 179 WREG_ECRT(0x04, 0x00); 180 181 misc = RREG8(MGA_MISC_IN); 182 misc |= MGAREG_MISC_RAMMAPEN | 183 MGAREG_MISC_HIGH_PG_SEL; 184 WREG8(MGA_MISC_OUT, misc); 185 186 mutex_unlock(&mdev->rmmio_lock); 187 188 return 0; 189 } 190 191 /* 192 * PCI driver 193 */ 194 195 static const struct pci_device_id mgag200_pciidlist[] = { 196 { PCI_VENDOR_ID_MATROX, 0x520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_PCI }, 197 { PCI_VENDOR_ID_MATROX, 0x521, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_AGP }, 198 { PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_A }, 199 { PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B }, 200 { PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV }, 201 { PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB }, 202 { PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH }, 203 { PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER }, 204 { PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 }, 205 { PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 }, 206 {0,} 207 }; 208 209 MODULE_DEVICE_TABLE(pci, mgag200_pciidlist); 210 211 static int 212 mgag200_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 213 { 214 enum mga_type type = (enum mga_type)ent->driver_data; 215 struct mga_device *mdev; 216 struct drm_device *dev; 217 int ret; 218 219 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &mgag200_driver); 220 if (ret) 221 return ret; 222 223 ret = pcim_enable_device(pdev); 224 if (ret) 225 return ret; 226 227 switch (type) { 228 case G200_PCI: 229 case G200_AGP: 230 mdev = mgag200_g200_device_create(pdev, &mgag200_driver); 231 break; 232 case G200_SE_A: 233 case G200_SE_B: 234 mdev = mgag200_g200se_device_create(pdev, &mgag200_driver, type); 235 break; 236 case G200_WB: 237 mdev = mgag200_g200wb_device_create(pdev, &mgag200_driver); 238 break; 239 case G200_EV: 240 mdev = mgag200_g200ev_device_create(pdev, &mgag200_driver); 241 break; 242 case G200_EH: 243 mdev = mgag200_g200eh_device_create(pdev, &mgag200_driver); 244 break; 245 case G200_EH3: 246 mdev = mgag200_g200eh3_device_create(pdev, &mgag200_driver); 247 break; 248 case G200_ER: 249 mdev = mgag200_g200er_device_create(pdev, &mgag200_driver); 250 break; 251 case G200_EW3: 252 mdev = mgag200_g200ew3_device_create(pdev, &mgag200_driver); 253 break; 254 default: 255 dev_err(&pdev->dev, "Device type %d is unsupported\n", type); 256 return -ENODEV; 257 } 258 if (IS_ERR(mdev)) 259 return PTR_ERR(mdev); 260 dev = &mdev->base; 261 262 ret = drm_dev_register(dev, 0); 263 if (ret) 264 return ret; 265 266 drm_fbdev_generic_setup(dev, 0); 267 268 return 0; 269 } 270 271 static void mgag200_pci_remove(struct pci_dev *pdev) 272 { 273 struct drm_device *dev = pci_get_drvdata(pdev); 274 275 drm_dev_unregister(dev); 276 } 277 278 static struct pci_driver mgag200_pci_driver = { 279 .name = DRIVER_NAME, 280 .id_table = mgag200_pciidlist, 281 .probe = mgag200_pci_probe, 282 .remove = mgag200_pci_remove, 283 }; 284 285 drm_module_pci_driver_if_modeset(mgag200_pci_driver, mgag200_modeset); 286 287 MODULE_AUTHOR(DRIVER_AUTHOR); 288 MODULE_DESCRIPTION(DRIVER_DESC); 289 MODULE_LICENSE("GPL"); 290