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>
12 #include <linux/vmalloc.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_file.h>
16 #include <drm/drm_ioctl.h>
17 #include <drm/drm_pciids.h>
19 #include "mgag200_drv.h"
21 int mgag200_modeset = -1;
22 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
23 module_param_named(modeset, mgag200_modeset, int, 0400);
29 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
31 static struct drm_driver mgag200_driver = {
32 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
33 .fops = &mgag200_driver_fops,
37 .major = DRIVER_MAJOR,
38 .minor = DRIVER_MINOR,
39 .patchlevel = DRIVER_PATCHLEVEL,
40 .gem_create_object = drm_gem_shmem_create_object_cached,
41 DRM_GEM_SHMEM_DRIVER_OPS,
48 static bool mgag200_has_sgram(struct mga_device *mdev)
50 struct drm_device *dev = &mdev->base;
54 ret = pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
55 if (drm_WARN(dev, ret, "failed to read PCI config dword: %d\n", ret))
58 return !!(option & PCI_MGA_OPTION_HARDPWMSK);
61 static int mgag200_regs_init(struct mga_device *mdev)
63 struct drm_device *dev = &mdev->base;
70 if (mgag200_has_sgram(mdev))
79 if (mgag200_has_sgram(mdev))
80 option |= PCI_MGA_OPTION_HARDPWMSK;
103 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
105 pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
107 /* BAR 1 contains registers */
108 mdev->rmmio_base = pci_resource_start(dev->pdev, 1);
109 mdev->rmmio_size = pci_resource_len(dev->pdev, 1);
111 if (!devm_request_mem_region(dev->dev, mdev->rmmio_base,
112 mdev->rmmio_size, "mgadrmfb_mmio")) {
113 drm_err(dev, "can't reserve mmio registers\n");
117 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
118 if (mdev->rmmio == NULL)
121 RREG_ECRT(0x03, crtcext3);
122 crtcext3 |= MGAREG_CRTCEXT3_MGAMODE;
123 WREG_ECRT(0x03, crtcext3);
128 static void mgag200_g200_interpret_bios(struct mga_device *mdev,
129 const unsigned char *bios,
132 static const char matrox[] = {'M', 'A', 'T', 'R', 'O', 'X'};
133 static const unsigned int expected_length[6] = {
134 0, 64, 64, 64, 128, 128
136 struct drm_device *dev = &mdev->base;
137 const unsigned char *pins;
138 unsigned int pins_len, version;
142 /* Test for MATROX string. */
143 if (size < 45 + sizeof(matrox))
145 if (memcmp(&bios[45], matrox, sizeof(matrox)) != 0)
148 /* Get the PInS offset. */
149 if (size < MGA_BIOS_OFFSET + 2)
151 offset = (bios[MGA_BIOS_OFFSET + 1] << 8) | bios[MGA_BIOS_OFFSET];
153 /* Get PInS data structure. */
155 if (size < offset + 6)
157 pins = bios + offset;
158 if (pins[0] == 0x2e && pins[1] == 0x41) {
163 pins_len = pins[0] + (pins[1] << 8);
166 if (version < 1 || version > 5) {
167 drm_warn(dev, "Unknown BIOS PInS version: %d\n", version);
170 if (pins_len != expected_length[version]) {
171 drm_warn(dev, "Unexpected BIOS PInS size: %d expeced: %d\n",
172 pins_len, expected_length[version]);
175 if (size < offset + pins_len)
178 drm_dbg_kms(dev, "MATROX BIOS PInS version %d size: %d found\n",
181 /* Extract the clock values */
185 tmp = pins[24] + (pins[25] << 8);
187 mdev->model.g200.pclk_max = tmp * 10;
190 if (pins[41] != 0xff)
191 mdev->model.g200.pclk_max = (pins[41] + 100) * 1000;
194 if (pins[36] != 0xff)
195 mdev->model.g200.pclk_max = (pins[36] + 100) * 1000;
197 mdev->model.g200.ref_clk = 14318;
200 if (pins[39] != 0xff)
201 mdev->model.g200.pclk_max = pins[39] * 4 * 1000;
203 mdev->model.g200.ref_clk = 14318;
206 tmp = pins[4] ? 8000 : 6000;
207 if (pins[123] != 0xff)
208 mdev->model.g200.pclk_min = pins[123] * tmp;
209 if (pins[38] != 0xff)
210 mdev->model.g200.pclk_max = pins[38] * tmp;
211 if (pins[110] & 0x01)
212 mdev->model.g200.ref_clk = 14318;
219 static void mgag200_g200_init_refclk(struct mga_device *mdev)
221 struct drm_device *dev = &mdev->base;
222 unsigned char __iomem *rom;
226 mdev->model.g200.pclk_min = 50000;
227 mdev->model.g200.pclk_max = 230000;
228 mdev->model.g200.ref_clk = 27050;
230 rom = pci_map_rom(dev->pdev, &size);
234 bios = vmalloc(size);
237 memcpy_fromio(bios, rom, size);
239 if (size != 0 && bios[0] == 0x55 && bios[1] == 0xaa)
240 mgag200_g200_interpret_bios(mdev, bios, size);
242 drm_dbg_kms(dev, "pclk_min: %ld pclk_max: %ld ref_clk: %ld\n",
243 mdev->model.g200.pclk_min, mdev->model.g200.pclk_max,
244 mdev->model.g200.ref_clk);
248 pci_unmap_rom(dev->pdev, rom);
251 static void mgag200_g200se_init_unique_id(struct mga_device *mdev)
253 struct drm_device *dev = &mdev->base;
255 /* stash G200 SE model number for later use */
256 mdev->model.g200se.unique_rev_id = RREG32(0x1e24);
258 drm_dbg(dev, "G200 SE unique revision id is 0x%x\n",
259 mdev->model.g200se.unique_rev_id);
262 static int mgag200_device_init(struct mga_device *mdev, unsigned long flags)
264 struct drm_device *dev = &mdev->base;
267 mdev->flags = mgag200_flags_from_driver_data(flags);
268 mdev->type = mgag200_type_from_driver_data(flags);
270 ret = mgag200_regs_init(mdev);
274 if (mdev->type == G200_PCI || mdev->type == G200_AGP)
275 mgag200_g200_init_refclk(mdev);
276 else if (IS_G200_SE(mdev))
277 mgag200_g200se_init_unique_id(mdev);
279 ret = mgag200_mm_init(mdev);
283 ret = mgag200_modeset_init(mdev);
285 drm_err(dev, "Fatal error during modeset init: %d\n", ret);
292 static struct mga_device *
293 mgag200_device_create(struct pci_dev *pdev, unsigned long flags)
295 struct drm_device *dev;
296 struct mga_device *mdev;
299 mdev = devm_drm_dev_alloc(&pdev->dev, &mgag200_driver,
300 struct mga_device, base);
306 pci_set_drvdata(pdev, dev);
308 ret = mgag200_device_init(mdev, flags);
319 static const struct pci_device_id mgag200_pciidlist[] = {
320 { PCI_VENDOR_ID_MATROX, 0x520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_PCI },
321 { PCI_VENDOR_ID_MATROX, 0x521, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_AGP },
322 { PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
323 G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
324 { PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
325 { PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
326 { PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
327 { PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
328 { PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
329 { PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
330 { PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
334 MODULE_DEVICE_TABLE(pci, mgag200_pciidlist);
337 mgag200_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
339 struct mga_device *mdev;
340 struct drm_device *dev;
343 drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
345 ret = pcim_enable_device(pdev);
349 mdev = mgag200_device_create(pdev, ent->driver_data);
351 return PTR_ERR(mdev);
354 ret = drm_dev_register(dev, ent->driver_data);
358 drm_fbdev_generic_setup(dev, 0);
363 static void mgag200_pci_remove(struct pci_dev *pdev)
365 struct drm_device *dev = pci_get_drvdata(pdev);
367 drm_dev_unregister(dev);
370 static struct pci_driver mgag200_pci_driver = {
372 .id_table = mgag200_pciidlist,
373 .probe = mgag200_pci_probe,
374 .remove = mgag200_pci_remove,
377 static int __init mgag200_init(void)
379 if (vgacon_text_force() && mgag200_modeset == -1)
382 if (mgag200_modeset == 0)
385 return pci_register_driver(&mgag200_pci_driver);
388 static void __exit mgag200_exit(void)
390 pci_unregister_driver(&mgag200_pci_driver);
393 module_init(mgag200_init);
394 module_exit(mgag200_exit);
396 MODULE_AUTHOR(DRIVER_AUTHOR);
397 MODULE_DESCRIPTION(DRIVER_DESC);
398 MODULE_LICENSE("GPL");