treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / mgag200 / mgag200_main.c
blobe278b6a547bde6fb8e3b8e3b5f7b93f9e8348d56
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2010 Matt Turner.
4 * Copyright 2012 Red Hat
6 * Authors: Matthew Garrett
7 * Matt Turner
8 * Dave Airlie
9 */
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)
24 int offset;
25 int orig;
26 int test1, test2;
27 int orig1, orig2;
28 unsigned int vram_size;
30 /* Probe */
31 orig = ioread16(mem);
32 iowrite16(0, mem);
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) {
54 break;
57 if (test2) {
58 break;
62 iowrite16(orig, mem);
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)
69 void __iomem *mem;
71 /* BAR 0 is VRAM */
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,
76 "mgadrmfb_vram")) {
77 DRM_ERROR("can't reserve VRAM\n");
78 return -ENXIO;
81 mem = pci_iomap(mdev->dev->pdev, 0, 0);
82 if (!mem)
83 return -ENOMEM;
85 mdev->mc.vram_size = mga_probe_vram(mdev, mem);
87 pci_iounmap(mdev->dev->pdev, mem);
89 return 0;
92 static int mgag200_device_init(struct drm_device *dev,
93 uint32_t flags)
95 struct mga_device *mdev = dev->dev_private;
96 int ret, option;
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 */
102 mdev->num_crtc = 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,
112 "mgadrmfb_mmio")) {
113 DRM_ERROR("can't reserve mmio registers\n");
114 return -ENOMEM;
117 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
118 if (mdev->rmmio == NULL)
119 return -ENOMEM;
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);
129 if (ret)
130 return ret;
132 mdev->bpp_shifts[0] = 0;
133 mdev->bpp_shifts[1] = 1;
134 mdev->bpp_shifts[2] = 0;
135 mdev->bpp_shifts[3] = 2;
136 return 0;
140 * Functions here will be called by the core once it's bound the driver to
141 * a PCI device
145 int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
147 struct mga_device *mdev;
148 int r;
150 mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL);
151 if (mdev == NULL)
152 return -ENOMEM;
153 dev->dev_private = (void *)mdev;
154 mdev->dev = dev;
156 r = mgag200_device_init(dev, flags);
157 if (r) {
158 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
159 return r;
161 r = mgag200_mm_init(mdev);
162 if (r)
163 goto err_mm;
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;
169 else
170 dev->mode_config.preferred_depth = 32;
171 dev->mode_config.prefer_shadow = 1;
173 r = mgag200_modeset_init(mdev);
174 if (r) {
175 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
176 goto err_modeset;
179 r = mgag200_cursor_init(mdev);
180 if (r)
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);
185 if (r)
186 goto err_modeset;
188 return 0;
190 err_modeset:
191 drm_mode_config_cleanup(dev);
192 mgag200_cursor_fini(mdev);
193 mgag200_mm_fini(mdev);
194 err_mm:
195 dev->dev_private = NULL;
197 return r;
200 void mgag200_driver_unload(struct drm_device *dev)
202 struct mga_device *mdev = dev->dev_private;
204 if (mdev == NULL)
205 return;
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;