1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/vmalloc.h>
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_drv.h>
9 #include <drm/drm_gem_atomic_helper.h>
10 #include <drm/drm_probe_helper.h>
12 #include "mgag200_drv.h"
14 static int mgag200_g200_init_pci_options(struct pci_dev *pdev)
16 struct device *dev = &pdev->dev;
21 err = pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
22 if (err != PCIBIOS_SUCCESSFUL) {
23 dev_err(dev, "pci_read_config_dword(PCI_MGA_OPTION) failed: %d\n", err);
24 return pcibios_err_to_errno(err);
27 has_sgram = !!(option & PCI_MGA_OPTION_HARDPWMSK);
34 return mgag200_init_pci_options(pdev, option, 0x00008000);
37 static void mgag200_g200_init_registers(struct mgag200_g200_device *g200)
39 static const u8 dacvalue[] = {
40 MGAG200_DAC_DEFAULT(0x00, 0xc9, 0x1f,
44 struct mga_device *mdev = &g200->base;
47 for (i = 0; i < ARRAY_SIZE(dacvalue); ++i) {
51 ((i >= 0x1f) && (i <= 0x29)) ||
52 ((i >= 0x30) && (i <= 0x37)))
54 WREG_DAC(i, dacvalue[i]);
57 mgag200_init_registers(mdev);
64 static int mgag200_g200_pixpllc_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *new_state)
66 static const int post_div_max = 7;
67 static const int in_div_min = 1;
68 static const int in_div_max = 6;
69 static const int feed_div_min = 7;
70 static const int feed_div_max = 127;
72 struct drm_device *dev = crtc->dev;
73 struct mgag200_g200_device *g200 = to_mgag200_g200_device(dev);
74 struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc);
75 struct mgag200_crtc_state *new_mgag200_crtc_state = to_mgag200_crtc_state(new_crtc_state);
76 long clock = new_crtc_state->mode.clock;
77 struct mgag200_pll_values *pixpllc = &new_mgag200_crtc_state->pixpllc;
78 u8 testp, testm, testn;
79 u8 n = 0, m = 0, p, s;
82 long delta, tmp_delta;
83 long ref_clk = g200->ref_clk;
84 long p_clk_min = g200->pclk_min;
85 long p_clk_max = g200->pclk_max;
87 if (clock > p_clk_max) {
88 drm_err(dev, "Pixel Clock %ld too high\n", clock);
92 if (clock < p_clk_min >> 3)
93 clock = p_clk_min >> 3;
97 testp <= post_div_max && f_vco < p_clk_min;
98 testp = (testp << 1) + 1, f_vco <<= 1)
104 for (testm = in_div_min; testm <= in_div_max; testm++) {
105 for (testn = feed_div_min; testn <= feed_div_max; testn++) {
106 computed = ref_clk * (testn + 1) / (testm + 1);
107 if (computed < f_vco)
108 tmp_delta = f_vco - computed;
110 tmp_delta = computed - f_vco;
111 if (tmp_delta < delta) {
118 f_vco = ref_clk * n / m;
121 else if (f_vco < 140000)
123 else if (f_vco < 180000)
128 drm_dbg_kms(dev, "clock: %ld vco: %ld m: %d n: %d p: %d s: %d\n",
129 clock, f_vco, m, n, p, s);
139 static void mgag200_g200_pixpllc_atomic_update(struct drm_crtc *crtc,
140 struct drm_atomic_state *old_state)
142 struct drm_device *dev = crtc->dev;
143 struct mga_device *mdev = to_mga_device(dev);
144 struct drm_crtc_state *crtc_state = crtc->state;
145 struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
146 struct mgag200_pll_values *pixpllc = &mgag200_crtc_state->pixpllc;
147 unsigned int pixpllcm, pixpllcn, pixpllcp, pixpllcs;
148 u8 xpixpllcm, xpixpllcn, xpixpllcp;
150 pixpllcm = pixpllc->m - 1;
151 pixpllcn = pixpllc->n - 1;
152 pixpllcp = pixpllc->p - 1;
153 pixpllcs = pixpllc->s;
155 xpixpllcm = pixpllcm;
156 xpixpllcn = pixpllcn;
157 xpixpllcp = (pixpllcs << 3) | pixpllcp;
159 WREG_MISC_MASKED(MGAREG_MISC_CLKSEL_MGA, MGAREG_MISC_CLKSEL_MASK);
161 WREG_DAC(MGA1064_PIX_PLLC_M, xpixpllcm);
162 WREG_DAC(MGA1064_PIX_PLLC_N, xpixpllcn);
163 WREG_DAC(MGA1064_PIX_PLLC_P, xpixpllcp);
167 * Mode-setting pipeline
170 static const struct drm_plane_helper_funcs mgag200_g200_primary_plane_helper_funcs = {
171 MGAG200_PRIMARY_PLANE_HELPER_FUNCS,
174 static const struct drm_plane_funcs mgag200_g200_primary_plane_funcs = {
175 MGAG200_PRIMARY_PLANE_FUNCS,
178 static const struct drm_crtc_helper_funcs mgag200_g200_crtc_helper_funcs = {
179 MGAG200_CRTC_HELPER_FUNCS,
182 static const struct drm_crtc_funcs mgag200_g200_crtc_funcs = {
186 static const struct drm_encoder_funcs mgag200_g200_dac_encoder_funcs = {
187 MGAG200_DAC_ENCODER_FUNCS,
190 static const struct drm_connector_helper_funcs mgag200_g200_vga_connector_helper_funcs = {
191 MGAG200_VGA_CONNECTOR_HELPER_FUNCS,
194 static const struct drm_connector_funcs mgag200_g200_vga_connector_funcs = {
195 MGAG200_VGA_CONNECTOR_FUNCS,
198 static int mgag200_g200_pipeline_init(struct mga_device *mdev)
200 struct drm_device *dev = &mdev->base;
201 struct drm_plane *primary_plane = &mdev->primary_plane;
202 struct drm_crtc *crtc = &mdev->crtc;
203 struct drm_encoder *encoder = &mdev->encoder;
204 struct mga_i2c_chan *i2c = &mdev->i2c;
205 struct drm_connector *connector = &mdev->connector;
208 ret = drm_universal_plane_init(dev, primary_plane, 0,
209 &mgag200_g200_primary_plane_funcs,
210 mgag200_primary_plane_formats,
211 mgag200_primary_plane_formats_size,
212 mgag200_primary_plane_fmtmods,
213 DRM_PLANE_TYPE_PRIMARY, NULL);
215 drm_err(dev, "drm_universal_plane_init() failed: %d\n", ret);
218 drm_plane_helper_add(primary_plane, &mgag200_g200_primary_plane_helper_funcs);
219 drm_plane_enable_fb_damage_clips(primary_plane);
221 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
222 &mgag200_g200_crtc_funcs, NULL);
224 drm_err(dev, "drm_crtc_init_with_planes() failed: %d\n", ret);
227 drm_crtc_helper_add(crtc, &mgag200_g200_crtc_helper_funcs);
229 /* FIXME: legacy gamma tables, but atomic gamma doesn't work without */
230 drm_mode_crtc_set_gamma_size(crtc, MGAG200_LUT_SIZE);
231 drm_crtc_enable_color_mgmt(crtc, 0, false, MGAG200_LUT_SIZE);
233 encoder->possible_crtcs = drm_crtc_mask(crtc);
234 ret = drm_encoder_init(dev, encoder, &mgag200_g200_dac_encoder_funcs,
235 DRM_MODE_ENCODER_DAC, NULL);
237 drm_err(dev, "drm_encoder_init() failed: %d\n", ret);
241 ret = mgag200_i2c_init(mdev, i2c);
243 drm_err(dev, "failed to add DDC bus: %d\n", ret);
247 ret = drm_connector_init_with_ddc(dev, connector,
248 &mgag200_g200_vga_connector_funcs,
249 DRM_MODE_CONNECTOR_VGA,
252 drm_err(dev, "drm_connector_init_with_ddc() failed: %d\n", ret);
255 drm_connector_helper_add(connector, &mgag200_g200_vga_connector_helper_funcs);
257 ret = drm_connector_attach_encoder(connector, encoder);
259 drm_err(dev, "drm_connector_attach_encoder() failed: %d\n", ret);
270 static const struct mgag200_device_info mgag200_g200_device_info =
271 MGAG200_DEVICE_INFO_INIT(2048, 2048, 0, false, 1, 3, false);
273 static void mgag200_g200_interpret_bios(struct mgag200_g200_device *g200,
274 const unsigned char *bios, size_t size)
276 static const char matrox[] = {'M', 'A', 'T', 'R', 'O', 'X'};
277 static const unsigned int expected_length[6] = {
278 0, 64, 64, 64, 128, 128
280 struct mga_device *mdev = &g200->base;
281 struct drm_device *dev = &mdev->base;
282 const unsigned char *pins;
283 unsigned int pins_len, version;
287 /* Test for MATROX string. */
288 if (size < 45 + sizeof(matrox))
290 if (memcmp(&bios[45], matrox, sizeof(matrox)) != 0)
293 /* Get the PInS offset. */
294 if (size < MGA_BIOS_OFFSET + 2)
296 offset = (bios[MGA_BIOS_OFFSET + 1] << 8) | bios[MGA_BIOS_OFFSET];
298 /* Get PInS data structure. */
300 if (size < offset + 6)
302 pins = bios + offset;
303 if (pins[0] == 0x2e && pins[1] == 0x41) {
308 pins_len = pins[0] + (pins[1] << 8);
311 if (version < 1 || version > 5) {
312 drm_warn(dev, "Unknown BIOS PInS version: %d\n", version);
315 if (pins_len != expected_length[version]) {
316 drm_warn(dev, "Unexpected BIOS PInS size: %d expected: %d\n",
317 pins_len, expected_length[version]);
320 if (size < offset + pins_len)
323 drm_dbg_kms(dev, "MATROX BIOS PInS version %d size: %d found\n", version, pins_len);
325 /* Extract the clock values */
329 tmp = pins[24] + (pins[25] << 8);
331 g200->pclk_max = tmp * 10;
334 if (pins[41] != 0xff)
335 g200->pclk_max = (pins[41] + 100) * 1000;
338 if (pins[36] != 0xff)
339 g200->pclk_max = (pins[36] + 100) * 1000;
341 g200->ref_clk = 14318;
344 if (pins[39] != 0xff)
345 g200->pclk_max = pins[39] * 4 * 1000;
347 g200->ref_clk = 14318;
350 tmp = pins[4] ? 8000 : 6000;
351 if (pins[123] != 0xff)
352 g200->pclk_min = pins[123] * tmp;
353 if (pins[38] != 0xff)
354 g200->pclk_max = pins[38] * tmp;
355 if (pins[110] & 0x01)
356 g200->ref_clk = 14318;
363 static void mgag200_g200_init_refclk(struct mgag200_g200_device *g200)
365 struct mga_device *mdev = &g200->base;
366 struct drm_device *dev = &mdev->base;
367 struct pci_dev *pdev = to_pci_dev(dev->dev);
368 unsigned char __iomem *rom;
372 g200->pclk_min = 50000;
373 g200->pclk_max = 230000;
374 g200->ref_clk = 27050;
376 rom = pci_map_rom(pdev, &size);
380 bios = vmalloc(size);
383 memcpy_fromio(bios, rom, size);
385 if (size != 0 && bios[0] == 0x55 && bios[1] == 0xaa)
386 mgag200_g200_interpret_bios(g200, bios, size);
388 drm_dbg_kms(dev, "pclk_min: %ld pclk_max: %ld ref_clk: %ld\n",
389 g200->pclk_min, g200->pclk_max, g200->ref_clk);
393 pci_unmap_rom(pdev, rom);
396 static const struct mgag200_device_funcs mgag200_g200_device_funcs = {
397 .pixpllc_atomic_check = mgag200_g200_pixpllc_atomic_check,
398 .pixpllc_atomic_update = mgag200_g200_pixpllc_atomic_update,
401 struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct drm_driver *drv)
403 struct mgag200_g200_device *g200;
404 struct mga_device *mdev;
405 struct drm_device *dev;
406 resource_size_t vram_available;
409 g200 = devm_drm_dev_alloc(&pdev->dev, drv, struct mgag200_g200_device, base.base);
411 return ERR_CAST(g200);
415 pci_set_drvdata(pdev, dev);
417 ret = mgag200_g200_init_pci_options(pdev);
421 ret = mgag200_device_preinit(mdev);
425 mgag200_g200_init_refclk(g200);
427 ret = mgag200_device_init(mdev, &mgag200_g200_device_info,
428 &mgag200_g200_device_funcs);
432 mgag200_g200_init_registers(g200);
434 vram_available = mgag200_device_probe_vram(mdev);
436 ret = mgag200_mode_config_init(mdev, vram_available);
440 ret = mgag200_g200_pipeline_init(mdev);
444 drm_mode_config_reset(dev);