1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2010 Matt Turner.
4 * Copyright 2012 Red Hat
6 * Authors: Matthew Garrett
11 #include <linux/pci.h>
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_gem_framebuffer_helper.h>
16 #include "mgag200_drv.h"
18 static const struct drm_mode_config_funcs mga_mode_funcs
= {
19 .fb_create
= drm_gem_fb_create
22 static int mga_probe_vram(struct mga_device
*mdev
, void __iomem
*mem
)
28 unsigned int vram_size
;
34 vram_size
= mdev
->mc
.vram_window
;
36 if ((mdev
->type
== G200_EW3
) && (vram_size
>= 0x1000000)) {
37 vram_size
= vram_size
- 0x400000;
40 for (offset
= 0x100000; offset
< vram_size
; offset
+= 0x4000) {
41 orig1
= ioread8(mem
+ offset
);
42 orig2
= ioread8(mem
+ offset
+ 0x100);
44 iowrite16(0xaa55, mem
+ offset
);
45 iowrite16(0xaa55, mem
+ offset
+ 0x100);
47 test1
= ioread16(mem
+ offset
);
48 test2
= ioread16(mem
);
50 iowrite16(orig1
, mem
+ offset
);
51 iowrite16(orig2
, mem
+ offset
+ 0x100);
53 if (test1
!= 0xaa55) {
63 return offset
- 65536;
66 /* Map the framebuffer from the card and configure the core */
67 static int mga_vram_init(struct mga_device
*mdev
)
72 mdev
->mc
.vram_base
= pci_resource_start(mdev
->dev
->pdev
, 0);
73 mdev
->mc
.vram_window
= pci_resource_len(mdev
->dev
->pdev
, 0);
75 if (!devm_request_mem_region(mdev
->dev
->dev
, mdev
->mc
.vram_base
, mdev
->mc
.vram_window
,
77 DRM_ERROR("can't reserve VRAM\n");
81 mem
= pci_iomap(mdev
->dev
->pdev
, 0, 0);
85 mdev
->mc
.vram_size
= mga_probe_vram(mdev
, mem
);
87 pci_iounmap(mdev
->dev
->pdev
, mem
);
92 static int mgag200_device_init(struct drm_device
*dev
,
95 struct mga_device
*mdev
= dev
->dev_private
;
98 mdev
->flags
= mgag200_flags_from_driver_data(flags
);
99 mdev
->type
= mgag200_type_from_driver_data(flags
);
101 /* Hardcode the number of CRTCs to 1 */
104 pci_read_config_dword(dev
->pdev
, PCI_MGA_OPTION
, &option
);
105 mdev
->has_sdram
= !(option
& (1 << 14));
107 /* BAR 0 is the framebuffer, BAR 1 contains registers */
108 mdev
->rmmio_base
= pci_resource_start(mdev
->dev
->pdev
, 1);
109 mdev
->rmmio_size
= pci_resource_len(mdev
->dev
->pdev
, 1);
111 if (!devm_request_mem_region(mdev
->dev
->dev
, mdev
->rmmio_base
, mdev
->rmmio_size
,
113 DRM_ERROR("can't reserve mmio registers\n");
117 mdev
->rmmio
= pcim_iomap(dev
->pdev
, 1, 0);
118 if (mdev
->rmmio
== NULL
)
121 /* stash G200 SE model number for later use */
122 if (IS_G200_SE(mdev
)) {
123 mdev
->unique_rev_id
= RREG32(0x1e24);
124 DRM_DEBUG("G200 SE unique revision id is 0x%x\n",
125 mdev
->unique_rev_id
);
128 ret
= mga_vram_init(mdev
);
132 mdev
->bpp_shifts
[0] = 0;
133 mdev
->bpp_shifts
[1] = 1;
134 mdev
->bpp_shifts
[2] = 0;
135 mdev
->bpp_shifts
[3] = 2;
140 * Functions here will be called by the core once it's bound the driver to
145 int mgag200_driver_load(struct drm_device
*dev
, unsigned long flags
)
147 struct mga_device
*mdev
;
150 mdev
= devm_kzalloc(dev
->dev
, sizeof(struct mga_device
), GFP_KERNEL
);
153 dev
->dev_private
= (void *)mdev
;
156 r
= mgag200_device_init(dev
, flags
);
158 dev_err(&dev
->pdev
->dev
, "Fatal error during GPU init: %d\n", r
);
161 r
= mgag200_mm_init(mdev
);
165 drm_mode_config_init(dev
);
166 dev
->mode_config
.funcs
= (void *)&mga_mode_funcs
;
167 if (IS_G200_SE(mdev
) && mdev
->vram_fb_available
< (2048*1024))
168 dev
->mode_config
.preferred_depth
= 16;
170 dev
->mode_config
.preferred_depth
= 32;
171 dev
->mode_config
.prefer_shadow
= 1;
173 r
= mgag200_modeset_init(mdev
);
175 dev_err(&dev
->pdev
->dev
, "Fatal error during modeset init: %d\n", r
);
179 r
= mgag200_cursor_init(mdev
);
181 dev_warn(&dev
->pdev
->dev
,
182 "Could not initialize cursors. Not doing hardware cursors.\n");
184 r
= drm_fbdev_generic_setup(mdev
->dev
, 0);
191 drm_mode_config_cleanup(dev
);
192 mgag200_cursor_fini(mdev
);
193 mgag200_mm_fini(mdev
);
195 dev
->dev_private
= NULL
;
200 void mgag200_driver_unload(struct drm_device
*dev
)
202 struct mga_device
*mdev
= dev
->dev_private
;
206 mgag200_modeset_fini(mdev
);
207 drm_mode_config_cleanup(dev
);
208 mgag200_cursor_fini(mdev
);
209 mgag200_mm_fini(mdev
);
210 dev
->dev_private
= NULL
;