1 // SPDX-License-Identifier: GPL-2.0-only 2 /* tcx.c: TCX frame buffer driver 3 * 4 * Copyright (C) 2003, 2006 David S. Miller (davem@davemloft.net) 5 * Copyright (C) 1996,1998 Jakub Jelinek (jj@ultra.linux.cz) 6 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx) 7 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be) 8 * 9 * Driver layout based loosely on tgafb.c, see that file for credits. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/errno.h> 15 #include <linux/string.h> 16 #include <linux/delay.h> 17 #include <linux/init.h> 18 #include <linux/fb.h> 19 #include <linux/mm.h> 20 #include <linux/of_device.h> 21 22 #include <asm/io.h> 23 #include <asm/fbio.h> 24 25 #include "sbuslib.h" 26 27 /* 28 * Local functions. 29 */ 30 31 static int tcx_setcolreg(unsigned, unsigned, unsigned, unsigned, 32 unsigned, struct fb_info *); 33 static int tcx_blank(int, struct fb_info *); 34 static int tcx_pan_display(struct fb_var_screeninfo *, struct fb_info *); 35 36 static int tcx_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma); 37 static int tcx_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg); 38 39 /* 40 * Frame buffer operations 41 */ 42 43 static const struct fb_ops tcx_ops = { 44 .owner = THIS_MODULE, 45 FB_DEFAULT_SBUS_OPS(tcx), 46 .fb_setcolreg = tcx_setcolreg, 47 .fb_blank = tcx_blank, 48 .fb_pan_display = tcx_pan_display, 49 }; 50 51 /* THC definitions */ 52 #define TCX_THC_MISC_REV_SHIFT 16 53 #define TCX_THC_MISC_REV_MASK 15 54 #define TCX_THC_MISC_VSYNC_DIS (1 << 25) 55 #define TCX_THC_MISC_HSYNC_DIS (1 << 24) 56 #define TCX_THC_MISC_RESET (1 << 12) 57 #define TCX_THC_MISC_VIDEO (1 << 10) 58 #define TCX_THC_MISC_SYNC (1 << 9) 59 #define TCX_THC_MISC_VSYNC (1 << 8) 60 #define TCX_THC_MISC_SYNC_ENAB (1 << 7) 61 #define TCX_THC_MISC_CURS_RES (1 << 6) 62 #define TCX_THC_MISC_INT_ENAB (1 << 5) 63 #define TCX_THC_MISC_INT (1 << 4) 64 #define TCX_THC_MISC_INIT 0x9f 65 #define TCX_THC_REV_REV_SHIFT 20 66 #define TCX_THC_REV_REV_MASK 15 67 #define TCX_THC_REV_MINREV_SHIFT 28 68 #define TCX_THC_REV_MINREV_MASK 15 69 70 /* The contents are unknown */ 71 struct tcx_tec { 72 u32 tec_matrix; 73 u32 tec_clip; 74 u32 tec_vdc; 75 }; 76 77 struct tcx_thc { 78 u32 thc_rev; 79 u32 thc_pad0[511]; 80 u32 thc_hs; /* hsync timing */ 81 u32 thc_hsdvs; 82 u32 thc_hd; 83 u32 thc_vs; /* vsync timing */ 84 u32 thc_vd; 85 u32 thc_refresh; 86 u32 thc_misc; 87 u32 thc_pad1[56]; 88 u32 thc_cursxy; /* cursor x,y position (16 bits each) */ 89 u32 thc_cursmask[32]; /* cursor mask bits */ 90 u32 thc_cursbits[32]; /* what to show where mask enabled */ 91 }; 92 93 struct bt_regs { 94 u32 addr; 95 u32 color_map; 96 u32 control; 97 u32 cursor; 98 }; 99 100 #define TCX_MMAP_ENTRIES 14 101 102 struct tcx_par { 103 spinlock_t lock; 104 struct bt_regs __iomem *bt; 105 struct tcx_thc __iomem *thc; 106 struct tcx_tec __iomem *tec; 107 u32 __iomem *cplane; 108 109 u32 flags; 110 #define TCX_FLAG_BLANKED 0x00000001 111 112 unsigned long which_io; 113 114 struct sbus_mmap_map mmap_map[TCX_MMAP_ENTRIES]; 115 int lowdepth; 116 }; 117 118 /* Reset control plane so that WID is 8-bit plane. */ 119 static void __tcx_set_control_plane(struct fb_info *info) 120 { 121 struct tcx_par *par = info->par; 122 u32 __iomem *p, *pend; 123 124 if (par->lowdepth) 125 return; 126 127 p = par->cplane; 128 if (p == NULL) 129 return; 130 for (pend = p + info->fix.smem_len; p < pend; p++) { 131 u32 tmp = sbus_readl(p); 132 133 tmp &= 0xffffff; 134 sbus_writel(tmp, p); 135 } 136 } 137 138 static void tcx_reset(struct fb_info *info) 139 { 140 struct tcx_par *par = (struct tcx_par *) info->par; 141 unsigned long flags; 142 143 spin_lock_irqsave(&par->lock, flags); 144 __tcx_set_control_plane(info); 145 spin_unlock_irqrestore(&par->lock, flags); 146 } 147 148 static int tcx_pan_display(struct fb_var_screeninfo *var, struct fb_info *info) 149 { 150 tcx_reset(info); 151 return 0; 152 } 153 154 /** 155 * tcx_setcolreg - Optional function. Sets a color register. 156 * @regno: boolean, 0 copy local, 1 get_user() function 157 * @red: frame buffer colormap structure 158 * @green: The green value which can be up to 16 bits wide 159 * @blue: The blue value which can be up to 16 bits wide. 160 * @transp: If supported the alpha value which can be up to 16 bits wide. 161 * @info: frame buffer info structure 162 */ 163 static int tcx_setcolreg(unsigned regno, 164 unsigned red, unsigned green, unsigned blue, 165 unsigned transp, struct fb_info *info) 166 { 167 struct tcx_par *par = (struct tcx_par *) info->par; 168 struct bt_regs __iomem *bt = par->bt; 169 unsigned long flags; 170 171 if (regno >= 256) 172 return 1; 173 174 red >>= 8; 175 green >>= 8; 176 blue >>= 8; 177 178 spin_lock_irqsave(&par->lock, flags); 179 180 sbus_writel(regno << 24, &bt->addr); 181 sbus_writel(red << 24, &bt->color_map); 182 sbus_writel(green << 24, &bt->color_map); 183 sbus_writel(blue << 24, &bt->color_map); 184 185 spin_unlock_irqrestore(&par->lock, flags); 186 187 return 0; 188 } 189 190 /** 191 * tcx_blank - Optional function. Blanks the display. 192 * @blank: the blank mode we want. 193 * @info: frame buffer structure that represents a single frame buffer 194 */ 195 static int 196 tcx_blank(int blank, struct fb_info *info) 197 { 198 struct tcx_par *par = (struct tcx_par *) info->par; 199 struct tcx_thc __iomem *thc = par->thc; 200 unsigned long flags; 201 u32 val; 202 203 spin_lock_irqsave(&par->lock, flags); 204 205 val = sbus_readl(&thc->thc_misc); 206 207 switch (blank) { 208 case FB_BLANK_UNBLANK: /* Unblanking */ 209 val &= ~(TCX_THC_MISC_VSYNC_DIS | 210 TCX_THC_MISC_HSYNC_DIS); 211 val |= TCX_THC_MISC_VIDEO; 212 par->flags &= ~TCX_FLAG_BLANKED; 213 break; 214 215 case FB_BLANK_NORMAL: /* Normal blanking */ 216 val &= ~TCX_THC_MISC_VIDEO; 217 par->flags |= TCX_FLAG_BLANKED; 218 break; 219 220 case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */ 221 val |= TCX_THC_MISC_VSYNC_DIS; 222 break; 223 case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */ 224 val |= TCX_THC_MISC_HSYNC_DIS; 225 break; 226 227 case FB_BLANK_POWERDOWN: /* Poweroff */ 228 break; 229 } 230 231 sbus_writel(val, &thc->thc_misc); 232 233 spin_unlock_irqrestore(&par->lock, flags); 234 235 return 0; 236 } 237 238 static struct sbus_mmap_map __tcx_mmap_map[TCX_MMAP_ENTRIES] = { 239 { 240 .voff = TCX_RAM8BIT, 241 .size = SBUS_MMAP_FBSIZE(1) 242 }, 243 { 244 .voff = TCX_RAM24BIT, 245 .size = SBUS_MMAP_FBSIZE(4) 246 }, 247 { 248 .voff = TCX_UNK3, 249 .size = SBUS_MMAP_FBSIZE(8) 250 }, 251 { 252 .voff = TCX_UNK4, 253 .size = SBUS_MMAP_FBSIZE(8) 254 }, 255 { 256 .voff = TCX_CONTROLPLANE, 257 .size = SBUS_MMAP_FBSIZE(4) 258 }, 259 { 260 .voff = TCX_UNK6, 261 .size = SBUS_MMAP_FBSIZE(8) 262 }, 263 { 264 .voff = TCX_UNK7, 265 .size = SBUS_MMAP_FBSIZE(8) 266 }, 267 { 268 .voff = TCX_TEC, 269 .size = PAGE_SIZE 270 }, 271 { 272 .voff = TCX_BTREGS, 273 .size = PAGE_SIZE 274 }, 275 { 276 .voff = TCX_THC, 277 .size = PAGE_SIZE 278 }, 279 { 280 .voff = TCX_DHC, 281 .size = PAGE_SIZE 282 }, 283 { 284 .voff = TCX_ALT, 285 .size = PAGE_SIZE 286 }, 287 { 288 .voff = TCX_UNK2, 289 .size = 0x20000 290 }, 291 { .size = 0 } 292 }; 293 294 static int tcx_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma) 295 { 296 struct tcx_par *par = (struct tcx_par *)info->par; 297 298 return sbusfb_mmap_helper(par->mmap_map, 299 info->fix.smem_start, info->fix.smem_len, 300 par->which_io, vma); 301 } 302 303 static int tcx_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg) 304 { 305 struct tcx_par *par = (struct tcx_par *) info->par; 306 307 return sbusfb_ioctl_helper(cmd, arg, info, 308 FBTYPE_TCXCOLOR, 309 (par->lowdepth ? 8 : 24), 310 info->fix.smem_len); 311 } 312 313 /* 314 * Initialisation 315 */ 316 317 static void 318 tcx_init_fix(struct fb_info *info, int linebytes) 319 { 320 struct tcx_par *par = (struct tcx_par *)info->par; 321 const char *tcx_name; 322 323 if (par->lowdepth) 324 tcx_name = "TCX8"; 325 else 326 tcx_name = "TCX24"; 327 328 strscpy(info->fix.id, tcx_name, sizeof(info->fix.id)); 329 330 info->fix.type = FB_TYPE_PACKED_PIXELS; 331 info->fix.visual = FB_VISUAL_PSEUDOCOLOR; 332 333 info->fix.line_length = linebytes; 334 335 info->fix.accel = FB_ACCEL_SUN_TCX; 336 } 337 338 static void tcx_unmap_regs(struct platform_device *op, struct fb_info *info, 339 struct tcx_par *par) 340 { 341 if (par->tec) 342 of_iounmap(&op->resource[7], 343 par->tec, sizeof(struct tcx_tec)); 344 if (par->thc) 345 of_iounmap(&op->resource[9], 346 par->thc, sizeof(struct tcx_thc)); 347 if (par->bt) 348 of_iounmap(&op->resource[8], 349 par->bt, sizeof(struct bt_regs)); 350 if (par->cplane) 351 of_iounmap(&op->resource[4], 352 par->cplane, info->fix.smem_len * sizeof(u32)); 353 if (info->screen_base) 354 of_iounmap(&op->resource[0], 355 info->screen_base, info->fix.smem_len); 356 } 357 358 static int tcx_probe(struct platform_device *op) 359 { 360 struct device_node *dp = op->dev.of_node; 361 struct fb_info *info; 362 struct tcx_par *par; 363 int linebytes, i, err; 364 365 info = framebuffer_alloc(sizeof(struct tcx_par), &op->dev); 366 367 err = -ENOMEM; 368 if (!info) 369 goto out_err; 370 par = info->par; 371 372 spin_lock_init(&par->lock); 373 374 par->lowdepth = of_property_read_bool(dp, "tcx-8-bit"); 375 376 sbusfb_fill_var(&info->var, dp, 8); 377 info->var.red.length = 8; 378 info->var.green.length = 8; 379 info->var.blue.length = 8; 380 381 linebytes = of_getintprop_default(dp, "linebytes", 382 info->var.xres); 383 info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres); 384 385 par->tec = of_ioremap(&op->resource[7], 0, 386 sizeof(struct tcx_tec), "tcx tec"); 387 par->thc = of_ioremap(&op->resource[9], 0, 388 sizeof(struct tcx_thc), "tcx thc"); 389 par->bt = of_ioremap(&op->resource[8], 0, 390 sizeof(struct bt_regs), "tcx dac"); 391 info->screen_base = of_ioremap(&op->resource[0], 0, 392 info->fix.smem_len, "tcx ram"); 393 if (!par->tec || !par->thc || 394 !par->bt || !info->screen_base) 395 goto out_unmap_regs; 396 397 memcpy(&par->mmap_map, &__tcx_mmap_map, sizeof(par->mmap_map)); 398 if (!par->lowdepth) { 399 par->cplane = of_ioremap(&op->resource[4], 0, 400 info->fix.smem_len * sizeof(u32), 401 "tcx cplane"); 402 if (!par->cplane) 403 goto out_unmap_regs; 404 } else { 405 par->mmap_map[1].size = SBUS_MMAP_EMPTY; 406 par->mmap_map[4].size = SBUS_MMAP_EMPTY; 407 par->mmap_map[5].size = SBUS_MMAP_EMPTY; 408 par->mmap_map[6].size = SBUS_MMAP_EMPTY; 409 } 410 411 info->fix.smem_start = op->resource[0].start; 412 par->which_io = op->resource[0].flags & IORESOURCE_BITS; 413 414 for (i = 0; i < TCX_MMAP_ENTRIES; i++) { 415 int j; 416 417 switch (i) { 418 case 10: 419 j = 12; 420 break; 421 422 case 11: case 12: 423 j = i - 1; 424 break; 425 426 default: 427 j = i; 428 break; 429 } 430 par->mmap_map[i].poff = op->resource[j].start; 431 } 432 433 info->fbops = &tcx_ops; 434 435 /* Initialize brooktree DAC. */ 436 sbus_writel(0x04 << 24, &par->bt->addr); /* color planes */ 437 sbus_writel(0xff << 24, &par->bt->control); 438 sbus_writel(0x05 << 24, &par->bt->addr); 439 sbus_writel(0x00 << 24, &par->bt->control); 440 sbus_writel(0x06 << 24, &par->bt->addr); /* overlay plane */ 441 sbus_writel(0x73 << 24, &par->bt->control); 442 sbus_writel(0x07 << 24, &par->bt->addr); 443 sbus_writel(0x00 << 24, &par->bt->control); 444 445 tcx_reset(info); 446 447 tcx_blank(FB_BLANK_UNBLANK, info); 448 449 if (fb_alloc_cmap(&info->cmap, 256, 0)) 450 goto out_unmap_regs; 451 452 fb_set_cmap(&info->cmap, info); 453 tcx_init_fix(info, linebytes); 454 455 err = register_framebuffer(info); 456 if (err < 0) 457 goto out_dealloc_cmap; 458 459 dev_set_drvdata(&op->dev, info); 460 461 printk(KERN_INFO "%pOF: TCX at %lx:%lx, %s\n", 462 dp, 463 par->which_io, 464 info->fix.smem_start, 465 par->lowdepth ? "8-bit only" : "24-bit depth"); 466 467 return 0; 468 469 out_dealloc_cmap: 470 fb_dealloc_cmap(&info->cmap); 471 472 out_unmap_regs: 473 tcx_unmap_regs(op, info, par); 474 framebuffer_release(info); 475 476 out_err: 477 return err; 478 } 479 480 static void tcx_remove(struct platform_device *op) 481 { 482 struct fb_info *info = dev_get_drvdata(&op->dev); 483 struct tcx_par *par = info->par; 484 485 unregister_framebuffer(info); 486 fb_dealloc_cmap(&info->cmap); 487 488 tcx_unmap_regs(op, info, par); 489 490 framebuffer_release(info); 491 } 492 493 static const struct of_device_id tcx_match[] = { 494 { 495 .name = "SUNW,tcx", 496 }, 497 {}, 498 }; 499 MODULE_DEVICE_TABLE(of, tcx_match); 500 501 static struct platform_driver tcx_driver = { 502 .driver = { 503 .name = "tcx", 504 .of_match_table = tcx_match, 505 }, 506 .probe = tcx_probe, 507 .remove_new = tcx_remove, 508 }; 509 510 static int __init tcx_init(void) 511 { 512 if (fb_get_options("tcxfb", NULL)) 513 return -ENODEV; 514 515 return platform_driver_register(&tcx_driver); 516 } 517 518 static void __exit tcx_exit(void) 519 { 520 platform_driver_unregister(&tcx_driver); 521 } 522 523 module_init(tcx_init); 524 module_exit(tcx_exit); 525 526 MODULE_DESCRIPTION("framebuffer driver for TCX chipsets"); 527 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); 528 MODULE_VERSION("2.0"); 529 MODULE_LICENSE("GPL"); 530