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