1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Red Hat
5 * Authors: Matthew Garrett
9 #include <linux/console.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_ioctl.h>
16 #include <drm/drm_pciids.h>
18 #include "mgag200_drv.h"
20 int mgag200_modeset = -1;
21 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
22 module_param_named(modeset, mgag200_modeset, int, 0400);
28 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
30 static struct drm_driver mgag200_driver = {
31 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
32 .fops = &mgag200_driver_fops,
36 .major = DRIVER_MAJOR,
37 .minor = DRIVER_MINOR,
38 .patchlevel = DRIVER_PATCHLEVEL,
39 .gem_create_object = drm_gem_shmem_create_object_cached,
40 DRM_GEM_SHMEM_DRIVER_OPS,
47 static bool mgag200_has_sgram(struct mga_device *mdev)
49 struct drm_device *dev = &mdev->base;
53 ret = pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
54 if (drm_WARN(dev, ret, "failed to read PCI config dword: %d\n", ret))
57 return !!(option & PCI_MGA_OPTION_HARDPWMSK);
60 static int mgag200_regs_init(struct mga_device *mdev)
62 struct drm_device *dev = &mdev->base;
69 if (mgag200_has_sgram(mdev))
70 option |= PCI_MGA_OPTION_HARDPWMSK;
93 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
95 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
97 /* BAR 1 contains registers */
98 mdev->rmmio_base = pci_resource_start(dev->pdev, 1);
99 mdev->rmmio_size = pci_resource_len(dev->pdev, 1);
101 if (!devm_request_mem_region(dev->dev, mdev->rmmio_base,
102 mdev->rmmio_size, "mgadrmfb_mmio")) {
103 drm_err(dev, "can't reserve mmio registers\n");
107 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
108 if (mdev->rmmio == NULL)
111 RREG_ECRT(0x03, crtcext3);
112 crtcext3 |= MGAREG_CRTCEXT3_MGAMODE;
113 WREG_ECRT(0x03, crtcext3);
118 static void mgag200_g200se_init_unique_id(struct mga_device *mdev)
120 struct drm_device *dev = &mdev->base;
122 /* stash G200 SE model number for later use */
123 mdev->model.g200se.unique_rev_id = RREG32(0x1e24);
125 drm_dbg(dev, "G200 SE unique revision id is 0x%x\n",
126 mdev->model.g200se.unique_rev_id);
129 static int mgag200_device_init(struct mga_device *mdev, unsigned long flags)
131 struct drm_device *dev = &mdev->base;
134 mdev->flags = mgag200_flags_from_driver_data(flags);
135 mdev->type = mgag200_type_from_driver_data(flags);
137 ret = mgag200_regs_init(mdev);
141 if (IS_G200_SE(mdev))
142 mgag200_g200se_init_unique_id(mdev);
144 ret = mgag200_mm_init(mdev);
148 ret = mgag200_modeset_init(mdev);
150 drm_err(dev, "Fatal error during modeset init: %d\n", ret);
157 static struct mga_device *
158 mgag200_device_create(struct pci_dev *pdev, unsigned long flags)
160 struct drm_device *dev;
161 struct mga_device *mdev;
164 mdev = devm_drm_dev_alloc(&pdev->dev, &mgag200_driver,
165 struct mga_device, base);
171 pci_set_drvdata(pdev, dev);
173 ret = mgag200_device_init(mdev, flags);
184 static const struct pci_device_id mgag200_pciidlist[] = {
185 { PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
186 G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
187 { PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
188 { PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
189 { PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
190 { PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
191 { PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
192 { PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
193 { PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
197 MODULE_DEVICE_TABLE(pci, mgag200_pciidlist);
200 mgag200_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
202 struct mga_device *mdev;
203 struct drm_device *dev;
206 drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
208 ret = pcim_enable_device(pdev);
212 mdev = mgag200_device_create(pdev, ent->driver_data);
214 return PTR_ERR(mdev);
217 ret = drm_dev_register(dev, ent->driver_data);
221 drm_fbdev_generic_setup(dev, 0);
226 static void mgag200_pci_remove(struct pci_dev *pdev)
228 struct drm_device *dev = pci_get_drvdata(pdev);
230 drm_dev_unregister(dev);
233 static struct pci_driver mgag200_pci_driver = {
235 .id_table = mgag200_pciidlist,
236 .probe = mgag200_pci_probe,
237 .remove = mgag200_pci_remove,
240 static int __init mgag200_init(void)
242 if (vgacon_text_force() && mgag200_modeset == -1)
245 if (mgag200_modeset == 0)
248 return pci_register_driver(&mgag200_pci_driver);
251 static void __exit mgag200_exit(void)
253 pci_unregister_driver(&mgag200_pci_driver);
256 module_init(mgag200_init);
257 module_exit(mgag200_exit);
259 MODULE_AUTHOR(DRIVER_AUTHOR);
260 MODULE_DESCRIPTION(DRIVER_DESC);
261 MODULE_LICENSE("GPL");