1 1.1 riastrad /* $NetBSD: ast_dp501.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $ */ 2 1.1 riastrad 3 1.3 riastrad // SPDX-License-Identifier: GPL-2.0 4 1.1 riastrad 5 1.1 riastrad #include <sys/cdefs.h> 6 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: ast_dp501.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $"); 7 1.1 riastrad 8 1.3 riastrad #include <linux/delay.h> 9 1.1 riastrad #include <linux/firmware.h> 10 1.3 riastrad #include <linux/module.h> 11 1.3 riastrad 12 1.1 riastrad #include "ast_drv.h" 13 1.3 riastrad 14 1.1 riastrad MODULE_FIRMWARE("ast_dp501_fw.bin"); 15 1.1 riastrad 16 1.3 riastrad static int ast_load_dp501_microcode(struct drm_device *dev) 17 1.1 riastrad { 18 1.1 riastrad struct ast_private *ast = dev->dev_private; 19 1.1 riastrad 20 1.3 riastrad return request_firmware(&ast->dp501_fw, "ast_dp501_fw.bin", dev->dev); 21 1.1 riastrad } 22 1.1 riastrad 23 1.1 riastrad static void send_ack(struct ast_private *ast) 24 1.1 riastrad { 25 1.1 riastrad u8 sendack; 26 1.1 riastrad sendack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0xff); 27 1.1 riastrad sendack |= 0x80; 28 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0x00, sendack); 29 1.1 riastrad } 30 1.1 riastrad 31 1.1 riastrad static void send_nack(struct ast_private *ast) 32 1.1 riastrad { 33 1.1 riastrad u8 sendack; 34 1.1 riastrad sendack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0xff); 35 1.1 riastrad sendack &= ~0x80; 36 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, 0x00, sendack); 37 1.1 riastrad } 38 1.1 riastrad 39 1.1 riastrad static bool wait_ack(struct ast_private *ast) 40 1.1 riastrad { 41 1.1 riastrad u8 waitack; 42 1.1 riastrad u32 retry = 0; 43 1.1 riastrad do { 44 1.1 riastrad waitack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff); 45 1.1 riastrad waitack &= 0x80; 46 1.1 riastrad udelay(100); 47 1.1 riastrad } while ((!waitack) && (retry++ < 1000)); 48 1.1 riastrad 49 1.1 riastrad if (retry < 1000) 50 1.1 riastrad return true; 51 1.1 riastrad else 52 1.1 riastrad return false; 53 1.1 riastrad } 54 1.1 riastrad 55 1.1 riastrad static bool wait_nack(struct ast_private *ast) 56 1.1 riastrad { 57 1.1 riastrad u8 waitack; 58 1.1 riastrad u32 retry = 0; 59 1.1 riastrad do { 60 1.1 riastrad waitack = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff); 61 1.1 riastrad waitack &= 0x80; 62 1.1 riastrad udelay(100); 63 1.1 riastrad } while ((waitack) && (retry++ < 1000)); 64 1.1 riastrad 65 1.1 riastrad if (retry < 1000) 66 1.1 riastrad return true; 67 1.1 riastrad else 68 1.1 riastrad return false; 69 1.1 riastrad } 70 1.1 riastrad 71 1.1 riastrad static void set_cmd_trigger(struct ast_private *ast) 72 1.1 riastrad { 73 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, ~0x40, 0x40); 74 1.1 riastrad } 75 1.1 riastrad 76 1.1 riastrad static void clear_cmd_trigger(struct ast_private *ast) 77 1.1 riastrad { 78 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9b, ~0x40, 0x00); 79 1.1 riastrad } 80 1.1 riastrad 81 1.1 riastrad #if 0 82 1.1 riastrad static bool wait_fw_ready(struct ast_private *ast) 83 1.1 riastrad { 84 1.1 riastrad u8 waitready; 85 1.1 riastrad u32 retry = 0; 86 1.1 riastrad do { 87 1.1 riastrad waitready = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd2, 0xff); 88 1.1 riastrad waitready &= 0x40; 89 1.1 riastrad udelay(100); 90 1.1 riastrad } while ((!waitready) && (retry++ < 1000)); 91 1.1 riastrad 92 1.1 riastrad if (retry < 1000) 93 1.1 riastrad return true; 94 1.1 riastrad else 95 1.1 riastrad return false; 96 1.1 riastrad } 97 1.1 riastrad #endif 98 1.1 riastrad 99 1.1 riastrad static bool ast_write_cmd(struct drm_device *dev, u8 data) 100 1.1 riastrad { 101 1.1 riastrad struct ast_private *ast = dev->dev_private; 102 1.1 riastrad int retry = 0; 103 1.1 riastrad if (wait_nack(ast)) { 104 1.1 riastrad send_nack(ast); 105 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, data); 106 1.1 riastrad send_ack(ast); 107 1.1 riastrad set_cmd_trigger(ast); 108 1.1 riastrad do { 109 1.1 riastrad if (wait_ack(ast)) { 110 1.1 riastrad clear_cmd_trigger(ast); 111 1.1 riastrad send_nack(ast); 112 1.1 riastrad return true; 113 1.1 riastrad } 114 1.1 riastrad } while (retry++ < 100); 115 1.1 riastrad } 116 1.1 riastrad clear_cmd_trigger(ast); 117 1.1 riastrad send_nack(ast); 118 1.1 riastrad return false; 119 1.1 riastrad } 120 1.1 riastrad 121 1.1 riastrad static bool ast_write_data(struct drm_device *dev, u8 data) 122 1.1 riastrad { 123 1.1 riastrad struct ast_private *ast = dev->dev_private; 124 1.1 riastrad 125 1.1 riastrad if (wait_nack(ast)) { 126 1.1 riastrad send_nack(ast); 127 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, data); 128 1.1 riastrad send_ack(ast); 129 1.1 riastrad if (wait_ack(ast)) { 130 1.1 riastrad send_nack(ast); 131 1.1 riastrad return true; 132 1.1 riastrad } 133 1.1 riastrad } 134 1.1 riastrad send_nack(ast); 135 1.1 riastrad return false; 136 1.1 riastrad } 137 1.1 riastrad 138 1.1 riastrad #if 0 139 1.1 riastrad static bool ast_read_data(struct drm_device *dev, u8 *data) 140 1.1 riastrad { 141 1.1 riastrad struct ast_private *ast = dev->dev_private; 142 1.1 riastrad u8 tmp; 143 1.1 riastrad 144 1.1 riastrad *data = 0; 145 1.1 riastrad 146 1.1 riastrad if (wait_ack(ast) == false) 147 1.1 riastrad return false; 148 1.1 riastrad tmp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd3, 0xff); 149 1.1 riastrad *data = tmp; 150 1.1 riastrad if (wait_nack(ast) == false) { 151 1.1 riastrad send_nack(ast); 152 1.1 riastrad return false; 153 1.1 riastrad } 154 1.1 riastrad send_nack(ast); 155 1.1 riastrad return true; 156 1.1 riastrad } 157 1.1 riastrad 158 1.1 riastrad static void clear_cmd(struct ast_private *ast) 159 1.1 riastrad { 160 1.1 riastrad send_nack(ast); 161 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x9a, 0x00, 0x00); 162 1.1 riastrad } 163 1.1 riastrad #endif 164 1.1 riastrad 165 1.1 riastrad void ast_set_dp501_video_output(struct drm_device *dev, u8 mode) 166 1.1 riastrad { 167 1.1 riastrad ast_write_cmd(dev, 0x40); 168 1.1 riastrad ast_write_data(dev, mode); 169 1.1 riastrad 170 1.1 riastrad msleep(10); 171 1.1 riastrad } 172 1.1 riastrad 173 1.1 riastrad static u32 get_fw_base(struct ast_private *ast) 174 1.1 riastrad { 175 1.1 riastrad return ast_mindwm(ast, 0x1e6e2104) & 0x7fffffff; 176 1.1 riastrad } 177 1.1 riastrad 178 1.1 riastrad bool ast_backup_fw(struct drm_device *dev, u8 *addr, u32 size) 179 1.1 riastrad { 180 1.1 riastrad struct ast_private *ast = dev->dev_private; 181 1.1 riastrad u32 i, data; 182 1.1 riastrad u32 boot_address; 183 1.1 riastrad 184 1.1 riastrad data = ast_mindwm(ast, 0x1e6e2100) & 0x01; 185 1.1 riastrad if (data) { 186 1.1 riastrad boot_address = get_fw_base(ast); 187 1.1 riastrad for (i = 0; i < size; i += 4) 188 1.1 riastrad *(u32 *)(addr + i) = ast_mindwm(ast, boot_address + i); 189 1.1 riastrad return true; 190 1.1 riastrad } 191 1.1 riastrad return false; 192 1.1 riastrad } 193 1.1 riastrad 194 1.3 riastrad static bool ast_launch_m68k(struct drm_device *dev) 195 1.1 riastrad { 196 1.1 riastrad struct ast_private *ast = dev->dev_private; 197 1.1 riastrad u32 i, data, len = 0; 198 1.1 riastrad u32 boot_address; 199 1.1 riastrad u8 *fw_addr = NULL; 200 1.1 riastrad u8 jreg; 201 1.1 riastrad 202 1.1 riastrad data = ast_mindwm(ast, 0x1e6e2100) & 0x01; 203 1.1 riastrad if (!data) { 204 1.1 riastrad 205 1.1 riastrad if (ast->dp501_fw_addr) { 206 1.1 riastrad fw_addr = ast->dp501_fw_addr; 207 1.1 riastrad len = 32*1024; 208 1.3 riastrad } else { 209 1.3 riastrad if (!ast->dp501_fw && 210 1.3 riastrad ast_load_dp501_microcode(dev) < 0) 211 1.3 riastrad return false; 212 1.3 riastrad 213 1.1 riastrad fw_addr = (u8 *)ast->dp501_fw->data; 214 1.1 riastrad len = ast->dp501_fw->size; 215 1.1 riastrad } 216 1.1 riastrad /* Get BootAddress */ 217 1.1 riastrad ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8); 218 1.1 riastrad data = ast_mindwm(ast, 0x1e6e0004); 219 1.1 riastrad switch (data & 0x03) { 220 1.1 riastrad case 0: 221 1.1 riastrad boot_address = 0x44000000; 222 1.1 riastrad break; 223 1.1 riastrad default: 224 1.1 riastrad case 1: 225 1.1 riastrad boot_address = 0x48000000; 226 1.1 riastrad break; 227 1.1 riastrad case 2: 228 1.1 riastrad boot_address = 0x50000000; 229 1.1 riastrad break; 230 1.1 riastrad case 3: 231 1.1 riastrad boot_address = 0x60000000; 232 1.1 riastrad break; 233 1.1 riastrad } 234 1.1 riastrad boot_address -= 0x200000; /* -2MB */ 235 1.1 riastrad 236 1.1 riastrad /* copy image to buffer */ 237 1.1 riastrad for (i = 0; i < len; i += 4) { 238 1.1 riastrad data = *(u32 *)(fw_addr + i); 239 1.1 riastrad ast_moutdwm(ast, boot_address + i, data); 240 1.1 riastrad } 241 1.1 riastrad 242 1.1 riastrad /* Init SCU */ 243 1.1 riastrad ast_moutdwm(ast, 0x1e6e2000, 0x1688a8a8); 244 1.1 riastrad 245 1.1 riastrad /* Launch FW */ 246 1.1 riastrad ast_moutdwm(ast, 0x1e6e2104, 0x80000000 + boot_address); 247 1.1 riastrad ast_moutdwm(ast, 0x1e6e2100, 1); 248 1.1 riastrad 249 1.1 riastrad /* Update Scratch */ 250 1.1 riastrad data = ast_mindwm(ast, 0x1e6e2040) & 0xfffff1ff; /* D[11:9] = 100b: UEFI handling */ 251 1.1 riastrad data |= 0x800; 252 1.1 riastrad ast_moutdwm(ast, 0x1e6e2040, data); 253 1.1 riastrad 254 1.1 riastrad jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xfc); /* D[1:0]: Reserved Video Buffer */ 255 1.1 riastrad jreg |= 0x02; 256 1.1 riastrad ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x99, jreg); 257 1.1 riastrad } 258 1.1 riastrad return true; 259 1.1 riastrad } 260 1.1 riastrad 261 1.1 riastrad u8 ast_get_dp501_max_clk(struct drm_device *dev) 262 1.1 riastrad { 263 1.1 riastrad struct ast_private *ast = dev->dev_private; 264 1.1 riastrad u32 boot_address, offset, data; 265 1.1 riastrad u8 linkcap[4], linkrate, linklanes, maxclk = 0xff; 266 1.1 riastrad 267 1.1 riastrad boot_address = get_fw_base(ast); 268 1.1 riastrad 269 1.1 riastrad /* validate FW version */ 270 1.1 riastrad offset = 0xf000; 271 1.1 riastrad data = ast_mindwm(ast, boot_address + offset); 272 1.1 riastrad if ((data & 0xf0) != 0x10) /* version: 1x */ 273 1.1 riastrad return maxclk; 274 1.1 riastrad 275 1.1 riastrad /* Read Link Capability */ 276 1.1 riastrad offset = 0xf014; 277 1.1 riastrad *(u32 *)linkcap = ast_mindwm(ast, boot_address + offset); 278 1.1 riastrad if (linkcap[2] == 0) { 279 1.1 riastrad linkrate = linkcap[0]; 280 1.1 riastrad linklanes = linkcap[1]; 281 1.1 riastrad data = (linkrate == 0x0a) ? (90 * linklanes) : (54 * linklanes); 282 1.1 riastrad if (data > 0xff) 283 1.1 riastrad data = 0xff; 284 1.1 riastrad maxclk = (u8)data; 285 1.1 riastrad } 286 1.1 riastrad return maxclk; 287 1.1 riastrad } 288 1.1 riastrad 289 1.1 riastrad bool ast_dp501_read_edid(struct drm_device *dev, u8 *ediddata) 290 1.1 riastrad { 291 1.1 riastrad struct ast_private *ast = dev->dev_private; 292 1.1 riastrad u32 i, boot_address, offset, data; 293 1.1 riastrad 294 1.1 riastrad boot_address = get_fw_base(ast); 295 1.1 riastrad 296 1.1 riastrad /* validate FW version */ 297 1.1 riastrad offset = 0xf000; 298 1.1 riastrad data = ast_mindwm(ast, boot_address + offset); 299 1.1 riastrad if ((data & 0xf0) != 0x10) 300 1.1 riastrad return false; 301 1.1 riastrad 302 1.1 riastrad /* validate PnP Monitor */ 303 1.1 riastrad offset = 0xf010; 304 1.1 riastrad data = ast_mindwm(ast, boot_address + offset); 305 1.1 riastrad if (!(data & 0x01)) 306 1.1 riastrad return false; 307 1.1 riastrad 308 1.1 riastrad /* Read EDID */ 309 1.1 riastrad offset = 0xf020; 310 1.1 riastrad for (i = 0; i < 128; i += 4) { 311 1.1 riastrad data = ast_mindwm(ast, boot_address + offset + i); 312 1.1 riastrad *(u32 *)(ediddata + i) = data; 313 1.1 riastrad } 314 1.1 riastrad 315 1.1 riastrad return true; 316 1.1 riastrad } 317 1.1 riastrad 318 1.1 riastrad static bool ast_init_dvo(struct drm_device *dev) 319 1.1 riastrad { 320 1.1 riastrad struct ast_private *ast = dev->dev_private; 321 1.1 riastrad u8 jreg; 322 1.1 riastrad u32 data; 323 1.1 riastrad ast_write32(ast, 0xf004, 0x1e6e0000); 324 1.1 riastrad ast_write32(ast, 0xf000, 0x1); 325 1.1 riastrad ast_write32(ast, 0x12000, 0x1688a8a8); 326 1.1 riastrad 327 1.1 riastrad jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff); 328 1.1 riastrad if (!(jreg & 0x80)) { 329 1.1 riastrad /* Init SCU DVO Settings */ 330 1.1 riastrad data = ast_read32(ast, 0x12008); 331 1.1 riastrad /* delay phase */ 332 1.1 riastrad data &= 0xfffff8ff; 333 1.1 riastrad data |= 0x00000500; 334 1.1 riastrad ast_write32(ast, 0x12008, data); 335 1.1 riastrad 336 1.1 riastrad if (ast->chip == AST2300) { 337 1.1 riastrad data = ast_read32(ast, 0x12084); 338 1.1 riastrad /* multi-pins for DVO single-edge */ 339 1.1 riastrad data |= 0xfffe0000; 340 1.1 riastrad ast_write32(ast, 0x12084, data); 341 1.1 riastrad 342 1.1 riastrad data = ast_read32(ast, 0x12088); 343 1.1 riastrad /* multi-pins for DVO single-edge */ 344 1.1 riastrad data |= 0x000fffff; 345 1.1 riastrad ast_write32(ast, 0x12088, data); 346 1.1 riastrad 347 1.1 riastrad data = ast_read32(ast, 0x12090); 348 1.1 riastrad /* multi-pins for DVO single-edge */ 349 1.1 riastrad data &= 0xffffffcf; 350 1.1 riastrad data |= 0x00000020; 351 1.1 riastrad ast_write32(ast, 0x12090, data); 352 1.1 riastrad } else { /* AST2400 */ 353 1.1 riastrad data = ast_read32(ast, 0x12088); 354 1.1 riastrad /* multi-pins for DVO single-edge */ 355 1.1 riastrad data |= 0x30000000; 356 1.1 riastrad ast_write32(ast, 0x12088, data); 357 1.1 riastrad 358 1.1 riastrad data = ast_read32(ast, 0x1208c); 359 1.1 riastrad /* multi-pins for DVO single-edge */ 360 1.1 riastrad data |= 0x000000cf; 361 1.1 riastrad ast_write32(ast, 0x1208c, data); 362 1.1 riastrad 363 1.1 riastrad data = ast_read32(ast, 0x120a4); 364 1.1 riastrad /* multi-pins for DVO single-edge */ 365 1.1 riastrad data |= 0xffff0000; 366 1.1 riastrad ast_write32(ast, 0x120a4, data); 367 1.1 riastrad 368 1.1 riastrad data = ast_read32(ast, 0x120a8); 369 1.1 riastrad /* multi-pins for DVO single-edge */ 370 1.1 riastrad data |= 0x0000000f; 371 1.1 riastrad ast_write32(ast, 0x120a8, data); 372 1.1 riastrad 373 1.1 riastrad data = ast_read32(ast, 0x12094); 374 1.1 riastrad /* multi-pins for DVO single-edge */ 375 1.1 riastrad data |= 0x00000002; 376 1.1 riastrad ast_write32(ast, 0x12094, data); 377 1.1 riastrad } 378 1.1 riastrad } 379 1.1 riastrad 380 1.1 riastrad /* Force to DVO */ 381 1.1 riastrad data = ast_read32(ast, 0x1202c); 382 1.1 riastrad data &= 0xfffbffff; 383 1.1 riastrad ast_write32(ast, 0x1202c, data); 384 1.1 riastrad 385 1.1 riastrad /* Init VGA DVO Settings */ 386 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x80); 387 1.1 riastrad return true; 388 1.1 riastrad } 389 1.1 riastrad 390 1.1 riastrad 391 1.1 riastrad static void ast_init_analog(struct drm_device *dev) 392 1.1 riastrad { 393 1.1 riastrad struct ast_private *ast = dev->dev_private; 394 1.1 riastrad u32 data; 395 1.1 riastrad 396 1.1 riastrad /* 397 1.1 riastrad * Set DAC source to VGA mode in SCU2C via the P2A 398 1.1 riastrad * bridge. First configure the P2U to target the SCU 399 1.1 riastrad * in case it isn't at this stage. 400 1.1 riastrad */ 401 1.1 riastrad ast_write32(ast, 0xf004, 0x1e6e0000); 402 1.1 riastrad ast_write32(ast, 0xf000, 0x1); 403 1.1 riastrad 404 1.1 riastrad /* Then unlock the SCU with the magic password */ 405 1.1 riastrad ast_write32(ast, 0x12000, 0x1688a8a8); 406 1.1 riastrad ast_write32(ast, 0x12000, 0x1688a8a8); 407 1.1 riastrad ast_write32(ast, 0x12000, 0x1688a8a8); 408 1.1 riastrad 409 1.1 riastrad /* Finally, clear bits [17:16] of SCU2c */ 410 1.1 riastrad data = ast_read32(ast, 0x1202c); 411 1.1 riastrad data &= 0xfffcffff; 412 1.1 riastrad ast_write32(ast, 0, data); 413 1.1 riastrad 414 1.1 riastrad /* Disable DVO */ 415 1.1 riastrad ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xcf, 0x00); 416 1.1 riastrad } 417 1.1 riastrad 418 1.1 riastrad void ast_init_3rdtx(struct drm_device *dev) 419 1.1 riastrad { 420 1.1 riastrad struct ast_private *ast = dev->dev_private; 421 1.1 riastrad u8 jreg; 422 1.1 riastrad 423 1.1 riastrad if (ast->chip == AST2300 || ast->chip == AST2400) { 424 1.1 riastrad jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff); 425 1.1 riastrad switch (jreg & 0x0e) { 426 1.1 riastrad case 0x04: 427 1.1 riastrad ast_init_dvo(dev); 428 1.1 riastrad break; 429 1.1 riastrad case 0x08: 430 1.1 riastrad ast_launch_m68k(dev); 431 1.1 riastrad break; 432 1.1 riastrad case 0x0c: 433 1.1 riastrad ast_init_dvo(dev); 434 1.1 riastrad break; 435 1.1 riastrad default: 436 1.1 riastrad if (ast->tx_chip_type == AST_TX_SIL164) 437 1.1 riastrad ast_init_dvo(dev); 438 1.1 riastrad else 439 1.1 riastrad ast_init_analog(dev); 440 1.1 riastrad } 441 1.1 riastrad } 442 1.1 riastrad } 443 1.3 riastrad 444 1.3 riastrad void ast_release_firmware(struct drm_device *dev) 445 1.3 riastrad { 446 1.3 riastrad struct ast_private *ast = dev->dev_private; 447 1.3 riastrad 448 1.3 riastrad release_firmware(ast->dp501_fw); 449 1.3 riastrad ast->dp501_fw = NULL; 450 1.3 riastrad } 451