1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/pci.h>
4 #include <linux/vmalloc.h>
5 
6 #include <drm/drm_drv.h>
7 
8 #include "mgag200_drv.h"
9 
10 static int mgag200_g200_init_pci_options(struct pci_dev *pdev)
11 {
12 	struct device *dev = &pdev->dev;
13 	bool has_sgram;
14 	u32 option;
15 	int err;
16 
17 	err = pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
18 	if (err != PCIBIOS_SUCCESSFUL) {
19 		dev_err(dev, "pci_read_config_dword(PCI_MGA_OPTION) failed: %d\n", err);
20 		return pcibios_err_to_errno(err);
21 	}
22 
23 	has_sgram = !!(option & PCI_MGA_OPTION_HARDPWMSK);
24 
25 	if (has_sgram)
26 		option = 0x4049cd21;
27 	else
28 		option = 0x40499121;
29 
30 	return mgag200_init_pci_options(pdev, option, 0x00008000);
31 }
32 
33 /*
34  * DRM Device
35  */
36 
37 static const struct mgag200_device_info mgag200_g200_device_info =
38 	MGAG200_DEVICE_INFO_INIT(2048, 2048, 0, false, 1, 3, false);
39 
40 static void mgag200_g200_interpret_bios(struct mgag200_g200_device *g200,
41 					const unsigned char *bios, size_t size)
42 {
43 	static const char matrox[] = {'M', 'A', 'T', 'R', 'O', 'X'};
44 	static const unsigned int expected_length[6] = {
45 		0, 64, 64, 64, 128, 128
46 	};
47 	struct mga_device *mdev = &g200->base;
48 	struct drm_device *dev = &mdev->base;
49 	const unsigned char *pins;
50 	unsigned int pins_len, version;
51 	int offset;
52 	int tmp;
53 
54 	/* Test for MATROX string. */
55 	if (size < 45 + sizeof(matrox))
56 		return;
57 	if (memcmp(&bios[45], matrox, sizeof(matrox)) != 0)
58 		return;
59 
60 	/* Get the PInS offset. */
61 	if (size < MGA_BIOS_OFFSET + 2)
62 		return;
63 	offset = (bios[MGA_BIOS_OFFSET + 1] << 8) | bios[MGA_BIOS_OFFSET];
64 
65 	/* Get PInS data structure. */
66 
67 	if (size < offset + 6)
68 		return;
69 	pins = bios + offset;
70 	if (pins[0] == 0x2e && pins[1] == 0x41) {
71 		version = pins[5];
72 		pins_len = pins[2];
73 	} else {
74 		version = 1;
75 		pins_len = pins[0] + (pins[1] << 8);
76 	}
77 
78 	if (version < 1 || version > 5) {
79 		drm_warn(dev, "Unknown BIOS PInS version: %d\n", version);
80 		return;
81 	}
82 	if (pins_len != expected_length[version]) {
83 		drm_warn(dev, "Unexpected BIOS PInS size: %d expected: %d\n",
84 			 pins_len, expected_length[version]);
85 		return;
86 	}
87 	if (size < offset + pins_len)
88 		return;
89 
90 	drm_dbg_kms(dev, "MATROX BIOS PInS version %d size: %d found\n", version, pins_len);
91 
92 	/* Extract the clock values */
93 
94 	switch (version) {
95 	case 1:
96 		tmp = pins[24] + (pins[25] << 8);
97 		if (tmp)
98 			g200->pclk_max = tmp * 10;
99 		break;
100 	case 2:
101 		if (pins[41] != 0xff)
102 			g200->pclk_max = (pins[41] + 100) * 1000;
103 		break;
104 	case 3:
105 		if (pins[36] != 0xff)
106 			g200->pclk_max = (pins[36] + 100) * 1000;
107 		if (pins[52] & 0x20)
108 			g200->ref_clk = 14318;
109 		break;
110 	case 4:
111 		if (pins[39] != 0xff)
112 			g200->pclk_max = pins[39] * 4 * 1000;
113 		if (pins[92] & 0x01)
114 			g200->ref_clk = 14318;
115 		break;
116 	case 5:
117 		tmp = pins[4] ? 8000 : 6000;
118 		if (pins[123] != 0xff)
119 			g200->pclk_min = pins[123] * tmp;
120 		if (pins[38] != 0xff)
121 			g200->pclk_max = pins[38] * tmp;
122 		if (pins[110] & 0x01)
123 			g200->ref_clk = 14318;
124 		break;
125 	default:
126 		break;
127 	}
128 }
129 
130 static void mgag200_g200_init_refclk(struct mgag200_g200_device *g200)
131 {
132 	struct mga_device *mdev = &g200->base;
133 	struct drm_device *dev = &mdev->base;
134 	struct pci_dev *pdev = to_pci_dev(dev->dev);
135 	unsigned char __iomem *rom;
136 	unsigned char *bios;
137 	size_t size;
138 
139 	g200->pclk_min = 50000;
140 	g200->pclk_max = 230000;
141 	g200->ref_clk = 27050;
142 
143 	rom = pci_map_rom(pdev, &size);
144 	if (!rom)
145 		return;
146 
147 	bios = vmalloc(size);
148 	if (!bios)
149 		goto out;
150 	memcpy_fromio(bios, rom, size);
151 
152 	if (size != 0 && bios[0] == 0x55 && bios[1] == 0xaa)
153 		mgag200_g200_interpret_bios(g200, bios, size);
154 
155 	drm_dbg_kms(dev, "pclk_min: %ld pclk_max: %ld ref_clk: %ld\n",
156 		    g200->pclk_min, g200->pclk_max, g200->ref_clk);
157 
158 	vfree(bios);
159 out:
160 	pci_unmap_rom(pdev, rom);
161 }
162 
163 struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct drm_driver *drv,
164 					      enum mga_type type)
165 {
166 	struct mgag200_g200_device *g200;
167 	struct mga_device *mdev;
168 	struct drm_device *dev;
169 	resource_size_t vram_available;
170 	int ret;
171 
172 	g200 = devm_drm_dev_alloc(&pdev->dev, drv, struct mgag200_g200_device, base.base);
173 	if (IS_ERR(g200))
174 		return ERR_CAST(g200);
175 	mdev = &g200->base;
176 	dev = &mdev->base;
177 
178 	pci_set_drvdata(pdev, dev);
179 
180 	ret = mgag200_g200_init_pci_options(pdev);
181 	if (ret)
182 		return ERR_PTR(ret);
183 
184 	ret = mgag200_device_preinit(mdev);
185 	if (ret)
186 		return ERR_PTR(ret);
187 
188 	mgag200_g200_init_refclk(g200);
189 
190 	ret = mgag200_device_init(mdev, type, &mgag200_g200_device_info);
191 	if (ret)
192 		return ERR_PTR(ret);
193 
194 	vram_available = mgag200_device_probe_vram(mdev);
195 
196 	ret = mgag200_modeset_init(mdev, vram_available);
197 	if (ret)
198 		return ERR_PTR(ret);
199 
200 	return mdev;
201 }
202