Patrick Georgi | 593124d | 2020-05-10 19:44:08 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Patrick Georgi | 16849bb | 2020-05-10 17:52:40 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copied from Linux drivers/gpu/drm/ast/ast_mode.c |
| 4 | */ |
Elyes HAOUAS | 27718ac | 2020-09-19 09:32:36 +0200 | [diff] [blame] | 5 | |
| 6 | #include <console/console.h> |
Patrick Rudolph | f1a4ae0 | 2019-09-30 11:02:04 +0200 | [diff] [blame] | 7 | #include <edid.h> |
Elyes HAOUAS | a4dd33c | 2020-08-11 09:39:43 +0200 | [diff] [blame] | 8 | #include <device/pci_def.h> |
Patrick Rudolph | f1a4ae0 | 2019-09-30 11:02:04 +0200 | [diff] [blame] | 9 | |
| 10 | #include "ast_drv.h" |
| 11 | |
| 12 | /* |
| 13 | * Set framebuffer MMIO address, which must fall into BAR0 MMIO window. |
| 14 | * |
| 15 | * Complete reimplementation as the original expects multiple kernel internal |
| 16 | * subsystems to be present. |
| 17 | */ |
| 18 | int ast_crtc_do_set_base(struct drm_crtc *crtc) |
| 19 | { |
| 20 | struct ast_private *ast = crtc->dev->dev_private; |
| 21 | struct drm_framebuffer *fb = crtc->primary->fb; |
| 22 | |
| 23 | /* PCI BAR 0 */ |
Elyes HAOUAS | a4dd33c | 2020-08-11 09:39:43 +0200 | [diff] [blame] | 24 | struct resource *res = find_resource(crtc->dev->pdev, PCI_BASE_ADDRESS_0); |
Patrick Rudolph | f1a4ae0 | 2019-09-30 11:02:04 +0200 | [diff] [blame] | 25 | if (!res) { |
| 26 | printk(BIOS_ERR, "BAR0 resource not found.\n"); |
| 27 | return -EIO; |
| 28 | } |
| 29 | |
| 30 | if (res->size < fb->pitches[0] * crtc->mode.vdisplay) { |
| 31 | dev_err(dev->pdev, "Framebuffer doesn't fit into BAR0 MMIO window\n"); |
| 32 | return -ENOMEM; |
| 33 | } |
| 34 | |
Patrick Rudolph | 0e3884c | 2020-11-30 13:44:40 +0100 | [diff] [blame] | 35 | fb->mmio_addr = (uintptr_t)res2mmio(res, 4095, 4095); |
Patrick Rudolph | f1a4ae0 | 2019-09-30 11:02:04 +0200 | [diff] [blame] | 36 | |
| 37 | ast_set_offset_reg(crtc); |
| 38 | ast_set_start_address_crt1(ast, fb->mmio_addr); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static void ast_edid_to_drmmode(struct edid *edid, struct drm_display_mode *mode) |
| 44 | { |
| 45 | memset(mode, 0, sizeof(*mode)); |
| 46 | |
| 47 | mode->hdisplay = edid->mode.ha; |
| 48 | mode->vdisplay = edid->mode.va; |
| 49 | mode->crtc_hdisplay = edid->mode.ha; |
| 50 | mode->crtc_vdisplay = edid->mode.va; |
| 51 | |
| 52 | /* EDID clock is in 10kHz, but drm clock is in KHz */ |
| 53 | mode->clock = edid->mode.pixel_clock * 10; |
| 54 | mode->vrefresh = edid->mode.refresh; |
| 55 | |
| 56 | mode->crtc_hblank_start = edid->mode.ha; |
| 57 | mode->crtc_hblank_end = edid->mode.ha + edid->mode.hbl; |
| 58 | mode->crtc_hsync_start = edid->mode.ha + edid->mode.hso; |
| 59 | mode->crtc_hsync_end = edid->mode.ha + edid->mode.hso + edid->mode.hspw; |
| 60 | mode->crtc_htotal = mode->crtc_hblank_end; |
| 61 | |
| 62 | mode->crtc_vblank_start = edid->mode.va; |
| 63 | mode->crtc_vblank_end = edid->mode.va + edid->mode.vbl; |
| 64 | mode->crtc_vsync_start = edid->mode.va + edid->mode.vso; |
| 65 | mode->crtc_vsync_end = edid->mode.va + edid->mode.vso + edid->mode.vspw; |
| 66 | mode->crtc_vtotal = mode->crtc_vblank_end; |
| 67 | |
| 68 | mode->flags = 0; |
| 69 | if (edid->mode.phsync == '+') |
| 70 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 71 | else |
| 72 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 73 | |
| 74 | if (edid->mode.pvsync == '+') |
| 75 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 76 | else |
| 77 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 78 | } |
| 79 | |
| 80 | static int ast_select_mode(struct drm_connector *connector, |
| 81 | struct edid *edid) |
| 82 | { |
| 83 | struct ast_private *ast = connector->dev->dev_private; |
| 84 | bool widescreen; |
| 85 | u8 raw[128]; |
| 86 | bool flags = false; |
| 87 | |
| 88 | if (ast->tx_chip_type == AST_TX_DP501) { |
| 89 | ast->dp501_maxclk = 0xff; |
| 90 | flags = ast_dp501_read_edid(connector->dev, (u8 *)raw); |
| 91 | if (flags) |
| 92 | ast->dp501_maxclk = ast_get_dp501_max_clk(connector->dev); |
| 93 | else |
| 94 | dev_err(dev->pdev, "I2C transmission error\n"); |
| 95 | } |
| 96 | |
| 97 | if (!flags) |
| 98 | ast_software_i2c_read(ast, raw); |
| 99 | |
| 100 | if (decode_edid(raw, sizeof(raw), edid) != EDID_CONFORMANT) { |
Angel Pons | 0f65165 | 2020-09-10 11:12:01 +0200 | [diff] [blame] | 101 | /* |
| 102 | * Servers often run headless, so a missing EDID is not an error. |
| 103 | * We still need to initialize a framebuffer for KVM, though. |
| 104 | */ |
| 105 | dev_info(dev->pdev, "Failed to decode EDID\n"); |
Patrick Rudolph | f1a4ae0 | 2019-09-30 11:02:04 +0200 | [diff] [blame] | 106 | printk(BIOS_DEBUG, "Assuming VGA for KVM\n"); |
| 107 | |
| 108 | memset(edid, 0, sizeof(*edid)); |
| 109 | |
| 110 | edid->mode.pixel_clock = 6411; |
| 111 | edid->mode.refresh = 60; |
| 112 | edid->mode.ha = 1024; |
| 113 | edid->mode.hspw = 4; |
| 114 | edid->mode.hso = 56; |
| 115 | edid->mode.hbl = 264; |
| 116 | edid->mode.phsync = '-'; |
| 117 | |
| 118 | edid->mode.va = 768; |
| 119 | edid->mode.vspw = 3; |
| 120 | edid->mode.vso = 1; |
| 121 | edid->mode.vbl = 26; |
| 122 | edid->mode.pvsync = '+'; |
| 123 | } |
| 124 | |
| 125 | printk(BIOS_DEBUG, "AST: Display has %dpx x %dpx\n", edid->mode.ha, edid->mode.va); |
| 126 | |
| 127 | widescreen = !!(((edid->mode.ha * 4) % (edid->mode.va * 3))); |
| 128 | |
| 129 | while (ast_mode_valid(connector, edid->mode.ha, edid->mode.va) != MODE_OK) { |
| 130 | /* Select a compatible smaller mode */ |
| 131 | if (edid->mode.ha > 1920 && widescreen) { |
| 132 | edid->mode.ha = 1920; |
| 133 | edid->mode.va = 1080; |
| 134 | } else if (edid->mode.ha >= 1920 && widescreen) { |
| 135 | edid->mode.ha = 1680; |
| 136 | edid->mode.va = 1050; |
| 137 | } else if (edid->mode.ha >= 1680 && widescreen) { |
| 138 | edid->mode.ha = 1600; |
| 139 | edid->mode.va = 900; |
| 140 | } else if (edid->mode.ha >= 1680 && !widescreen) { |
| 141 | edid->mode.ha = 1600; |
| 142 | edid->mode.va = 1200; |
| 143 | } else if (edid->mode.ha >= 1600 && widescreen) { |
| 144 | edid->mode.ha = 1440; |
| 145 | edid->mode.va = 900; |
| 146 | } else if (edid->mode.ha >= 1440 && widescreen) { |
| 147 | edid->mode.ha = 1360; |
| 148 | edid->mode.va = 768; |
| 149 | } else if (edid->mode.ha >= 1360 && widescreen) { |
| 150 | edid->mode.ha = 1280; |
| 151 | edid->mode.va = 800; |
| 152 | } else if (edid->mode.ha >= 1360 && !widescreen) { |
| 153 | edid->mode.ha = 1280; |
| 154 | edid->mode.va = 1024; |
| 155 | } else if (edid->mode.ha >= 1280) { |
| 156 | edid->mode.ha = 1024; |
| 157 | edid->mode.va = 768; |
| 158 | } else if (edid->mode.ha >= 1024) { |
| 159 | edid->mode.ha = 800; |
| 160 | edid->mode.va = 600; |
| 161 | } else if (edid->mode.ha >= 800) { |
| 162 | edid->mode.ha = 640; |
| 163 | edid->mode.va = 480; |
| 164 | } else { |
| 165 | dev_err(dev->pdev, "No compatible mode found.\n"); |
| 166 | |
| 167 | return -EIO; |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int ast_driver_framebuffer_init(struct drm_device *dev, int flags) |
| 175 | { |
| 176 | struct drm_display_mode adjusted_mode; |
| 177 | struct drm_crtc crtc; |
| 178 | struct drm_format format; |
| 179 | struct drm_primary primary; |
| 180 | struct drm_framebuffer fb; |
| 181 | struct drm_connector connector; |
| 182 | struct edid edid; |
| 183 | int ret; |
| 184 | |
| 185 | /* Init wrapper structs */ |
| 186 | connector.dev = dev; |
| 187 | |
| 188 | format.cpp[0] = 4; /* 32 BPP */ |
| 189 | fb.format = &format; |
| 190 | |
| 191 | primary.fb = &fb; |
| 192 | |
| 193 | crtc.dev = dev; |
| 194 | crtc.primary = &primary; |
| 195 | |
| 196 | /* Read EDID and find mode */ |
| 197 | ret = ast_select_mode(&connector, &edid); |
| 198 | if (ret) { |
| 199 | dev_err(dev->pdev, "Failed to select mode.\n"); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | /* Updated edid for set_vbe_mode_info_valid */ |
| 204 | edid.x_resolution = edid.mode.ha; |
| 205 | edid.y_resolution = edid.mode.va; |
| 206 | edid.framebuffer_bits_per_pixel = format.cpp[0] * 8; |
| 207 | edid.bytes_per_line = ALIGN_UP(edid.x_resolution * format.cpp[0], 8); |
| 208 | |
| 209 | /* Updated framebuffer info for ast_crtc_mode_set */ |
| 210 | fb.pitches[0] = edid.bytes_per_line; |
| 211 | |
| 212 | printk(BIOS_DEBUG, "Using framebuffer %dpx x %dpx pitch %d @ %d BPP\n", |
| 213 | edid.x_resolution, edid.y_resolution, edid.bytes_per_line, |
| 214 | edid.framebuffer_bits_per_pixel); |
| 215 | |
| 216 | /* Convert EDID to AST DRM mode */ |
| 217 | ast_edid_to_drmmode(&edid, &crtc.mode); |
| 218 | |
| 219 | memcpy(&adjusted_mode, &crtc.mode, sizeof(crtc.mode)); |
| 220 | |
| 221 | ret = ast_crtc_mode_set(&crtc, &crtc.mode, &adjusted_mode); |
| 222 | if (ret) { |
| 223 | dev_err(dev->pdev, "Failed to set mode.\n"); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | ast_hide_cursor(&crtc); |
| 228 | |
| 229 | /* Advertise new mode */ |
| 230 | set_vbe_mode_info_valid(&edid, fb.mmio_addr); |
| 231 | |
| 232 | /* Clear display */ |
Patrick Rudolph | 0e3884c | 2020-11-30 13:44:40 +0100 | [diff] [blame] | 233 | memset((void *)(uintptr_t)fb.mmio_addr, 0, edid.bytes_per_line * edid.y_resolution); |
Patrick Rudolph | f1a4ae0 | 2019-09-30 11:02:04 +0200 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |