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, enum mga_type type,
159 			const struct mgag200_device_info *info)
160 {
161 	struct drm_device *dev = &mdev->base;
162 	u8 crtcext3, misc;
163 	int ret;
164 
165 	mdev->info = info;
166 	mdev->type = type;
167 
168 	ret = drmm_mutex_init(dev, &mdev->rmmio_lock);
169 	if (ret)
170 		return ret;
171 
172 	mutex_lock(&mdev->rmmio_lock);
173 
174 	RREG_ECRT(0x03, crtcext3);
175 	crtcext3 |= MGAREG_CRTCEXT3_MGAMODE;
176 	WREG_ECRT(0x03, crtcext3);
177 
178 	WREG_ECRT(0x04, 0x00);
179 
180 	misc = RREG8(MGA_MISC_IN);
181 	misc |= MGAREG_MISC_RAMMAPEN |
182 		MGAREG_MISC_HIGH_PG_SEL;
183 	WREG8(MGA_MISC_OUT, misc);
184 
185 	mutex_unlock(&mdev->rmmio_lock);
186 
187 	return 0;
188 }
189 
190 /*
191  * PCI driver
192  */
193 
194 static const struct pci_device_id mgag200_pciidlist[] = {
195 	{ PCI_VENDOR_ID_MATROX, 0x520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_PCI },
196 	{ PCI_VENDOR_ID_MATROX, 0x521, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_AGP },
197 	{ PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_A },
198 	{ PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
199 	{ PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
200 	{ PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
201 	{ PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
202 	{ PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
203 	{ PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
204 	{ PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
205 	{0,}
206 };
207 
208 MODULE_DEVICE_TABLE(pci, mgag200_pciidlist);
209 
210 static int
211 mgag200_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
212 {
213 	enum mga_type type = (enum mga_type)ent->driver_data;
214 	struct mga_device *mdev;
215 	struct drm_device *dev;
216 	int ret;
217 
218 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &mgag200_driver);
219 	if (ret)
220 		return ret;
221 
222 	ret = pcim_enable_device(pdev);
223 	if (ret)
224 		return ret;
225 
226 	switch (type) {
227 	case G200_PCI:
228 	case G200_AGP:
229 		mdev = mgag200_g200_device_create(pdev, &mgag200_driver, type);
230 		break;
231 	case G200_SE_A:
232 	case G200_SE_B:
233 		mdev = mgag200_g200se_device_create(pdev, &mgag200_driver, type);
234 		break;
235 	case G200_WB:
236 		mdev = mgag200_g200wb_device_create(pdev, &mgag200_driver, type);
237 		break;
238 	case G200_EV:
239 		mdev = mgag200_g200ev_device_create(pdev, &mgag200_driver, type);
240 		break;
241 	case G200_EH:
242 		mdev = mgag200_g200eh_device_create(pdev, &mgag200_driver, type);
243 		break;
244 	case G200_EH3:
245 		mdev = mgag200_g200eh3_device_create(pdev, &mgag200_driver, type);
246 		break;
247 	case G200_ER:
248 		mdev = mgag200_g200er_device_create(pdev, &mgag200_driver, type);
249 		break;
250 	case G200_EW3:
251 		mdev = mgag200_g200ew3_device_create(pdev, &mgag200_driver, type);
252 		break;
253 	default:
254 		dev_err(&pdev->dev, "Device type %d is unsupported\n", type);
255 		return -ENODEV;
256 	}
257 	if (IS_ERR(mdev))
258 		return PTR_ERR(mdev);
259 	dev = &mdev->base;
260 
261 	ret = drm_dev_register(dev, 0);
262 	if (ret)
263 		return ret;
264 
265 	drm_fbdev_generic_setup(dev, 0);
266 
267 	return 0;
268 }
269 
270 static void mgag200_pci_remove(struct pci_dev *pdev)
271 {
272 	struct drm_device *dev = pci_get_drvdata(pdev);
273 
274 	drm_dev_unregister(dev);
275 }
276 
277 static struct pci_driver mgag200_pci_driver = {
278 	.name = DRIVER_NAME,
279 	.id_table = mgag200_pciidlist,
280 	.probe = mgag200_pci_probe,
281 	.remove = mgag200_pci_remove,
282 };
283 
284 drm_module_pci_driver_if_modeset(mgag200_pci_driver, mgag200_modeset);
285 
286 MODULE_AUTHOR(DRIVER_AUTHOR);
287 MODULE_DESCRIPTION(DRIVER_DESC);
288 MODULE_LICENSE("GPL");
289