/*
* Copyright (c) 1996
* The President and Fellows of Harvard College. All rights reserved.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Harvard University.
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* This product includes software developed by Harvard University and
* its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Based on:
* NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
* NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
*/
/*
* Driver for Campus-II on-board mbus-based video (cgfourteen).
*
* Does not handle interrupts, even though they can occur.
*
* XXX should defer colormap updates to vertical retrace interrupts
*/
/*
* Match a cgfourteen.
*/
int
cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
{
union obio_attach_args *uoba = aux;
struct sbus_attach_args *sa = &uoba->uoba_sbus;
/*
* The cgfourteen is a local-bus video adaptor, accessed directly
* via the processor, and not through device space or an external
* bus. Thus we look _only_ at the obio bus.
* Additionally, these things exist only on the Sun4m.
*/
if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
return (0);
#if NSX > 0
/* see if we've got an SX to help us */
sc->sc_sx = NULL;
for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
dv != NULL;
dv = deviter_next(&di)) {
if (device_is_a(dv, "sx")) {
sc->sc_sx = device_private(dv);
}
}
deviter_release(&di);
if (sc->sc_sx != NULL) {
sc->sc_fb_paddr = bus_space_mmap(sc->sc_bustag,
sc->sc_fbaddr, 0, 0, 0) & 0xfffff000;
aprint_normal_dev(sc->sc_dev, "using %s\n",
device_xname(sc->sc_sx->sc_dev));
aprint_normal_dev(sc->sc_dev, "fb paddr: %08x\n",
sc->sc_fb_paddr);
sx_write(sc->sc_sx, SX_PAGE_BOUND_LOWER, sc->sc_fb_paddr);
sx_write(sc->sc_sx, SX_PAGE_BOUND_UPPER,
sc->sc_fb_paddr + 0x03ffffff);
}
cg14_wait_idle(sc);
#endif
cg14_setup_wsdisplay(sc, isconsole);
#endif
/* Attach to /dev/fb */
fb_attach(&sc->sc_fb, isconsole);
}
/*
* Keep track of the number of opens made. In the 24-bit driver, we need to
* switch to 24-bit mode on the first open, and switch back to 8-bit on
* the last close. This kind of nonsense is needed to give screenblank
* a fighting chance of working.
*/
int
cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
{
struct cgfourteen_softc *sc;
int oldopens;
/* Setup the cg14 as we want it, and save the original PROM state */
if (oldopens == 0) /* first open only, to make screenblank work */
cg14_init(sc);
return (0);
}
int
cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
{
struct cgfourteen_softc *sc =
device_lookup_private(&cgfourteen_cd, minor(dev));
int opens;
/*
* Restore video state to make the PROM happy, on last close.
*/
if (opens == 0) {
cg14_reset(sc);
#if NSX > 0
if (sc->sc_sx)
glyphcache_wipe(&sc->sc_gc);
#endif
}
return (0);
}
int
cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
{
struct cgfourteen_softc *sc =
device_lookup_private(&cgfourteen_cd, minor(dev));
struct fbgattr *fba;
int error;
switch (cmd) {
case FBIOGTYPE:
*(struct fbtype *)data = sc->sc_fb.fb_type;
break;
case FBIOGETCMAP:
return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
CG14_CLUT_SIZE));
case FBIOPUTCMAP:
/* copy to software map */
#define p ((struct fbcmap *)data)
error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
if (error)
return (error);
/* now blast them into the chip */
/* XXX should use retrace interrupt */
if (sc->sc_depth == 8)
cg14_load_hwcmap(sc, p->index, p->count);
#undef p
break;
case FBIOGVIDEO:
*(int *)data = cg14_get_video(sc);
break;
case FBIOSVIDEO:
cg14_set_video(sc, *(int *)data);
break;
case CG14_SET_PIXELMODE: {
int depth = *(int *)data;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
return EINVAL;
/*
* Undo the effect of an FBIOSVIDEO that turns the video off.
*/
static void
cgfourteenunblank(device_t dev)
{
struct cgfourteen_softc *sc = device_private(dev);
/*
* Return the address that would map the given device at the given
* offset, allowing for the given protection, or return -1 for error.
*/
paddr_t
cgfourteenmmap(dev_t dev, off_t off, int prot)
{
struct cgfourteen_softc *sc =
device_lookup_private(&cgfourteen_cd, minor(dev));
off_t offset = -1;
if (off & PGOFSET)
panic("cgfourteenmmap");
if (off < 0)
return (-1);
if (off >= 0 && off < 0x10000) {
offset = sc->sc_regaddr;
} else if (off >= CG14_CURSOR_VOFF &&
off < (CG14_CURSOR_VOFF + 0x1000)) {
offset = sc->sc_regaddr + CG14_OFFSET_CURS;
off -= CG14_CURSOR_VOFF;
} else if (off >= CG14_DIRECT_VOFF &&
off < (CG14_DIRECT_VOFF + sc->sc_vramsize)) {
offset = sc->sc_fbaddr + CG14_FB_VRAM;
off -= CG14_DIRECT_VOFF;
} else if (off >= CG14_BGR_VOFF &&
off < (CG14_BGR_VOFF + sc->sc_vramsize)) {
offset = sc->sc_fbaddr + CG14_FB_CBGR;
off -= CG14_BGR_VOFF;
} else if (off >= CG14_X32_VOFF &&
off < (CG14_X32_VOFF + (sc->sc_vramsize >> 2))) {
offset = sc->sc_fbaddr + CG14_FB_PX32;
off -= CG14_X32_VOFF;
} else if (off >= CG14_B32_VOFF &&
off < (CG14_B32_VOFF + (sc->sc_vramsize >> 2))) {
offset = sc->sc_fbaddr + CG14_FB_PB32;
off -= CG14_B32_VOFF;
} else if (off >= CG14_G32_VOFF &&
off < (CG14_G32_VOFF + (sc->sc_vramsize >> 2))) {
offset = sc->sc_fbaddr + CG14_FB_PG32;
off -= CG14_G32_VOFF;
} else if (off >= CG14_R32_VOFF &&
off < CG14_R32_VOFF + (sc->sc_vramsize >> 2)) {
offset = sc->sc_fbaddr + CG14_FB_PR32;
off -= CG14_R32_VOFF;
#if NSX > 0
/*
* for convenience we also map the SX ranges here:
* - one page userland registers
* - CG14-sized IO space at 0x800000000 ( not a typo, it's above 4GB )
*/
} else if (sc->sc_sx == NULL) {
return -1;
} else if (off >= CG14_SXREG_VOFF &&
off < (CG14_SXREG_VOFF + 0x400)) {
return (bus_space_mmap(sc->sc_sx->sc_tag, sc->sc_sx->sc_uregs,
0, prot, BUS_SPACE_MAP_LINEAR));
} else if (off >= CG14_SXIO_VOFF &&
off < (CG14_SXIO_VOFF + 0x03ffffff)) {
off -= CG14_SXIO_VOFF;
return (bus_space_mmap(sc->sc_sx->sc_tag, 0x800000000LL,
sc->sc_fb_paddr + off,
prot, BUS_SPACE_MAP_LINEAR));
#endif
} else
return -1;
int
cgfourteenpoll(dev_t dev, int events, struct lwp *l)
{
return (seltrue(dev, events, l));
}
/*
* Miscellaneous helper functions
*/
/* Initialize the framebuffer, storing away useful state for later reset */
static void
cg14_init(struct cgfourteen_softc *sc)
{
cg14_set_depth(sc, 32);
cg14_init_cmap(sc);
}
static void
/* Restore the state saved on cg14_init */
cg14_reset(struct cgfourteen_softc *sc)
{
cg14_set_depth(sc, 8);
cg14_init_cmap(sc);
}
/* Enable/disable video display; power down monitor if DPMS-capable */
static void
cg14_set_video(struct cgfourteen_softc *sc, int enable)
{
/*
* We can only use DPMS to power down the display if the chip revision
* is greater than 0.
*/
if (enable) {
if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
CG14_MCTL_POWERCTL);
else
sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
} else {
if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
CG14_MCTL_POWERCTL);
else
sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
}
}
/* Get status of video display */
static int
cg14_get_video(struct cgfourteen_softc *sc)
{
return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
}
/* Read the software shadow colormap */
static int
cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
{
u_int i, start, count;
u_char *cp;
int error;
for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
error = copyout(&cp[3], &p->red[i], 1);
if (error)
return error;
error = copyout(&cp[2], &p->green[i], 1);
if (error)
return error;
error = copyout(&cp[1], &p->blue[i], 1);
if (error)
return error;
}
return (0);
}
/* Write the software shadow colormap */
static int
cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
{
u_int i, start, count;
u_char *cp;
u_char cmap[256][4];
int error;
if (sc->sc_depth == 16) {
/* construct an R5G6B5 palette in CLUT1/2 */
for (i = 0; i < 0x100; i++) {
/* upper byte first */
r = (i & 0xf8); /* red component */
r |= r >> 5; /* fill lower bits so 0xf8 */
c = 0x40000000 | r; /* becomes 0xff */
g = i & 0x7; /* upper 3 bits of green */
g |= g << 3; /* 0x003f */
c |= ((g << 10) & 0xf000); /* make it 4 */
sc->sc_clut1->clut_lut[i] = c;
/* lower byte */
g = i & 0xe0; /* lower half of green */
g |= g >> 3;
cc = 0x40000000 | ((g << 4) & 0x0f00);
b = i & 0x1f; /* and blue */
b = (b << 3) | (b >> 2);
cc |= (b << 16);
sc->sc_clut2->clut_lut[i] = cc;
}
/*
* because we alpha blend our components everything is half
* intensity, fix that with the gamma LUT
*/
sc->sc_dac->dac_mode &= ~6; /* 8bit mode, for simlicity */
for (i = 0; i < 128; i++) {
sc->sc_dac->dac_addr = i;
sc->sc_dac->dac_gammalut = i << 1;
sc->sc_dac->dac_gammalut = i << 1;
sc->sc_dac->dac_gammalut = i << 1;
}
/* finally fill the XLUT */
for (i = 0; i < 256; i++)
sc->sc_xlut->xlut_lut[i] =
CG14_LEFT_CLUT2 | CG14_LEFT_B |
CG14_RIGHT_CLUT1 | CG14_RIGHT_X;
} else {
/* in 8 or 24bit put a linear ramp in the gamma LUT */
sc->sc_dac->dac_mode &= ~6; /* 8bit mode, for simlicity */
for (i = 0; i < 256; i++) {
sc->sc_dac->dac_addr = i;
sc->sc_dac->dac_gammalut = i;
sc->sc_dac->dac_gammalut = i;
sc->sc_dac->dac_gammalut = i;
}
/* and zero the XLUT */
for (i = 0; i < 256; i++)
sc->sc_xlut->xlut_lut[i] = 0;
if (sc->sc_depth == 8) {
rasops_get_cmap(ri, cmap, sizeof(cmap));
/* allow mmap()ing the full framebuffer, not just what we use */
if (offset < sc->sc_vramsize)
return bus_space_mmap(sc->sc_bustag,
BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
/*
* unaligned copies with horizontal overlap are slow, so don't bother
* handling them in cg14_bitblt() and use putchar() instead
*/
if (sc->sc_sx != NULL) {
scr->scr_flags |= VCONS_NO_COPYCOLS;
} else
#endif
scr->scr_flags |= VCONS_DONT_READ;
static void
cg14_rectfill(struct cgfourteen_softc *sc, int x, int y, int wi, int he,
uint32_t colour)
{
uint32_t addr, pptr;
int line, cnt, pre, words;
int stride = sc->sc_fb.fb_type.fb_width;
addr = sc->sc_fb_paddr + x + stride * y;
sx_write(sc->sc_sx, SX_QUEUED(8), colour);
sx_write(sc->sc_sx, SX_QUEUED(9), colour);
/*
* Calculate the number of pixels we need to do one by one
* until we're 32bit aligned, then do the rest in 32bit
* mode. Assumes that stride is always a multiple of 4.
*/
/* TODO: use 32bit writes with byte mask instead */
pre = addr & 3;
if (pre != 0) pre = 4 - pre;
for (line = 0; line < he; line++) {
pptr = addr;
cnt = wi;
if (pre) {
sxm(SX_STBS, pptr, 8, pre - 1);
pptr += pre;
cnt -= pre;
}
/* now do the aligned pixels in 32bit chunks */
while(cnt > 3) {
words = uimin(32, cnt >> 2);
sxm(SX_STS, pptr, 8, words - 1);
pptr += words << 2;
cnt -= words << 2;
}
/* do any remaining pixels byte-wise again */
if (cnt > 0)
sxm(SX_STBS, pptr, 8, cnt - 1);
addr += stride;
}
}
static void
cg14_rectfill_a(void *cookie, int dstx, int dsty,
int width, int height, long attr)
{
struct cgfourteen_softc *sc = cookie;
static inline void
cg14_set_mask(struct cgfourteen_softc *sc, uint32_t mask)
{
if (mask == sc->sc_mask) return;
sc->sc_mask = mask;
sx_write(sc->sc_sx, SX_QUEUED(R_MASK), mask);
}
/*
* invert a rectangle, used only to (un)draw the cursor.
* - does a scanline at a time
* - does not handle wi > 64 or wi < 4, not that we need it for our fonts
* - uses all 32bit accesses
*/
static void
cg14_invert(struct cgfourteen_softc *sc, int x, int y, int wi, int he)
{
uint32_t addr, pptr, lmask, rmask;
int line, cnt, pre, words, pwrds = 0, post, reg;
int stride = sc->sc_fb.fb_type.fb_width;
addr = (sc->sc_fb_paddr + x + stride * y) & ~3;
sx_write(sc->sc_sx, SX_ROP_CONTROL, 0x3C); /* ~src a / src a */
/*
* Calculate the number of pixels we need to mask on each end of the
* scanline and how many we can do without mask, if any
*/
pre = x & 3;
if (pre != 0) {
lmask = 0xffffffff >> pre;
pre = 4 - pre;
pwrds++;
}
post = (x + wi) & 3;
if (post != 0) {
rmask = ~(0xffffffff >> post);
pwrds++;
}
words = (wi + pre + 3) >> 2;
cnt = words - pwrds;
static inline void
cg14_slurp(int reg, uint32_t addr, int cnt)
{
int num;
while (cnt > 0) {
num = uimin(32, cnt);
sxm(SX_LD, addr, reg, num - 1);
cnt -= num;
reg += num;
addr += (num << 2);
}
}
static inline void
cg14_spit(int reg, uint32_t addr, int cnt)
{
int num;
while (cnt > 0) {
num = uimin(32, cnt);
sxm(SX_ST, addr, reg, num - 1);
cnt -= num;
reg += num;
addr += (num << 2);
}
}
static void
cg14_bitblt(void *cookie, int xs, int ys, int xd, int yd,
int wi, int he, int rop)
{
struct cgfourteen_softc *sc = cookie;
uint32_t saddr, daddr, sptr, dptr;
int line, cnt, stride = sc->sc_fb.fb_type.fb_width;
int num, words, skip;
if ((saddr & 3) == (daddr & 3)) {
int pre = saddr & 3; /* pixels to copy byte-wise */
if (pre != 0) pre = 4 - pre;
for (line = 0; line < he; line++) {
sptr = saddr;
dptr = daddr;
cnt = wi;
if (pre > 0) {
sxm(SX_LDB, sptr, 32, pre - 1);
sxm(SX_STB, dptr, 32, pre - 1);
cnt -= pre;
sptr += pre;
dptr += pre;
}
words = cnt >> 2;
while(cnt > 3) {
num = uimin(120, words);
cg14_slurp(8, sptr, num);
cg14_spit(8, dptr, num);
sptr += num << 2;
dptr += num << 2;
cnt -= num << 2;
}
if (cnt > 0) {
sxm(SX_LDB, sptr, 32, cnt - 1);
sxm(SX_STB, dptr, 32, cnt - 1);
}
saddr += skip;
daddr += skip;
}
} else {
/* unaligned, have to use byte mode */
/* funnel shifter & byte mask trickery? */
for (line = 0; line < he; line++) {
sptr = saddr;
dptr = daddr;
cnt = wi;
while(cnt > 31) {
sxm(SX_LDB, sptr, 32, 31);
sxm(SX_STB, dptr, 32, 31);
sptr += 32;
dptr += 32;
cnt -= 32;
}
if (cnt > 0) {
sxm(SX_LDB, sptr, 32, cnt - 1);
sxm(SX_STB, dptr, 32, cnt - 1);
}
saddr += skip;
daddr += skip;
}
}
}
/*
* for copying glyphs around
* - uses all quads for reads
* - uses quads for writes as far as possible
* - limited by number of registers - won't do more than 120 wide
* - doesn't handle overlaps
*/
static void
cg14_bitblt_gc(void *cookie, int xs, int ys, int xd, int yd,
int wi, int he, int rop)
{
struct cgfourteen_softc *sc = cookie;
uint32_t saddr, daddr;
int line, cnt = wi, stride = sc->sc_fb.fb_type.fb_width;
int dreg = 8, swi = wi, dd;
int in = 0, q = 0, out = 0, r;
if (ri->ri_flg & RI_CURSOR) {
wi = ri->ri_font->fontwidth;
he = ri->ri_font->fontheight;
x = ri->ri_ccol * wi + ri->ri_xorigin;
y = ri->ri_crow * he + ri->ri_yorigin;
cg14_invert(sc, x, y, wi, he);
ri->ri_flg &= ~RI_CURSOR;
}
}
static void
cg14_cursor(void *cookie, int on, int row, int col)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
struct cgfourteen_softc *sc = scr->scr_cookie;
int x, y, wi, he;
wi = ri->ri_font->fontwidth;
he = ri->ri_font->fontheight;
if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
if (on) {
if (ri->ri_flg & RI_CURSOR) {
cg14_nuke_cursor(ri);
}
x = col * wi + ri->ri_xorigin;
y = row * he + ri->ri_yorigin;
cg14_invert(sc, x, y, wi, he);
ri->ri_flg |= RI_CURSOR;
}
ri->ri_crow = row;
ri->ri_ccol = col;
} else {
scr->scr_ri.ri_crow = row;
scr->scr_ri.ri_ccol = col;
scr->scr_ri.ri_flg &= ~RI_CURSOR;
}
}
static void
cg14_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
{
struct rasops_info *ri = cookie;
struct wsdisplay_font *font = PICK_FONT(ri, c);
struct vcons_screen *scr = ri->ri_hw;
struct cgfourteen_softc *sc = scr->scr_cookie;
int stride = sc->sc_fb.fb_type.fb_width;
uint32_t bg, fg, addr, bg8, fg8, pixel, in, q, next;
int i, j, x, y, wi, he, r, g, b, aval, cnt, reg;
int r1, g1, b1, r0, g0, b0, fgo, bgo, rv;
uint8_t *data8;
if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
return;
if (!CHAR_IN_FONT(c, font))
return;
if (row == ri->ri_crow && col == ri->ri_ccol) {
ri->ri_flg &= ~RI_CURSOR;
}
wi = font->fontwidth;
he = font->fontheight;
bg = ri->ri_devcmap[(attr >> 16) & 0xf];
fg = ri->ri_devcmap[(attr >> 24) & 0xf];
x = ri->ri_xorigin + col * wi;
y = ri->ri_yorigin + row * he;
if (c == 0x20) {
cg14_rectfill(sc, x, y, wi, he, bg);
if (attr & 1)
cg14_rectfill(sc, x, y + he - 2, wi, 1, fg);
return;
}
rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
if (rv == GC_OK)
return;