Home | History | Annotate | Line # | Download | only in tc
px.c revision 1.5
      1 /* 	$NetBSD: px.c,v 1.5 2001/03/04 13:32:25 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Driver for DEC PixelStamp graphics adapters (PMAG-C).
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/types.h>
     45 #include <sys/systm.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <sys/callout.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #if defined(pmax)
     53 #include <mips/cpuregs.h>
     54 #elif defined(alpha)
     55 #include <alpha/alpha_cpu.h>
     56 #endif
     57 
     58 #include <machine/autoconf.h>
     59 #include <machine/cpu.h>
     60 #include <machine/bus.h>
     61 
     62 #include <dev/cons.h>
     63 
     64 #include <dev/wscons/wsconsio.h>
     65 #include <dev/wscons/wsdisplayvar.h>
     66 
     67 #include <dev/ic/bt459reg.h>
     68 
     69 #include <dev/tc/tcvar.h>
     70 #include <dev/tc/sticreg.h>
     71 #include <dev/tc/sticvar.h>
     72 
     73 #define	PX_STIC_POLL_OFFSET	0x000000	/* STIC DMA poll space */
     74 #define	PX_STAMP_OFFSET		0x0c0000	/* pixelstamp space on STIC */
     75 #define	PX_STIC_OFFSET		0x180000	/* STIC registers */
     76 #define	PX_VDAC_OFFSET		0x200000	/* VDAC registers (bt459) */
     77 #define	PX_VDAC_RESET_OFFSET	0x300000	/* VDAC reset register */
     78 #define	PX_ROM_OFFSET		0x300000	/* ROM code */
     79 
     80 #define	PX_BUF_SIZE		(STIC_PACKET_SIZE*2 + STIC_IMGBUF_SIZE*2)
     81 #define	PX_BUF_ALIGN		32768
     82 
     83 static void	px_attach(struct device *, struct device *, void *);
     84 static void	px_init(struct stic_info *, int);
     85 static int	px_match(struct device *, struct cfdata *, void *);
     86 
     87 static int	px_intr(void *);
     88 static u_int32_t	*px_pbuf_get(struct stic_info *);
     89 static int	px_pbuf_post(struct stic_info *, u_int32_t *);
     90 
     91 void	px_cnattach(tc_addr_t);
     92 
     93 struct px_softc {
     94 	struct	device px_dv;
     95 	struct	stic_info *px_si;
     96 };
     97 
     98 struct cfattach px_ca = {
     99 	sizeof(struct px_softc), px_match, px_attach
    100 };
    101 
    102 static int
    103 px_match(struct device *parent, struct cfdata *match, void *aux)
    104 {
    105 	struct tc_attach_args *ta;
    106 
    107 	ta = aux;
    108 
    109 	return (strncmp("PMAG-CA ", ta->ta_modname, TC_ROM_LLEN) == 0);
    110 }
    111 
    112 static void
    113 px_attach(struct device *parent, struct device *self, void *aux)
    114 {
    115 	struct stic_info *si;
    116 	struct tc_attach_args *ta;
    117 	struct px_softc *px;
    118 	int console;
    119 
    120 	px = (struct px_softc *)self;
    121 	ta = (struct tc_attach_args *)aux;
    122 
    123 	if (ta->ta_addr == stic_consinfo.si_slotbase) {
    124 		si = &stic_consinfo;
    125 		console = 1;
    126 	} else {
    127 		if (stic_consinfo.si_slotbase == NULL)
    128 			si = &stic_consinfo;
    129 		else {
    130 			si = malloc(sizeof(*si), M_DEVBUF, M_NOWAIT);
    131 			memset(si, 0, sizeof(*si));
    132 		}
    133 		si->si_slotbase = ta->ta_addr;
    134 		px_init(si, 0);
    135 		console = 0;
    136 	}
    137 
    138 	px->px_si = si;
    139 	tc_intr_establish(parent, ta->ta_cookie, IPL_TTY, px_intr, si);
    140 
    141 	printf(": 2DA, 8 plane, %dx%d stamp\n", si->si_stampw, si->si_stamph);
    142 
    143 	stic_attach(self, si, console);
    144 }
    145 
    146 void
    147 px_cnattach(tc_addr_t addr)
    148 {
    149 	struct stic_info *si;
    150 
    151 	si = &stic_consinfo;
    152 	si->si_slotbase = addr;
    153 	px_init(si, 1);
    154 	stic_cnattach(si);
    155 }
    156 
    157 static void
    158 px_init(struct stic_info *si, int bootstrap)
    159 {
    160 	struct pglist pglist;
    161 	caddr_t kva;
    162 	paddr_t bpa;
    163 
    164 	/*
    165 	 * Allocate memory for the packet buffers.  It must be located below
    166 	 * 8MB, since the STIC can't access outside that region.  Also, due
    167 	 * to the holes in STIC address space, each buffer mustn't cross a
    168 	 * 32kB boundary.
    169 	 */
    170 	if (bootstrap) {
    171 		/*
    172 		 * UVM won't be initalised at this point, so grab memory
    173 		 * directly from vm_physmem[].
    174 		 */
    175 		kva = (caddr_t)pmap_steal_memory(PX_BUF_SIZE + PX_BUF_ALIGN,
    176 		    NULL, NULL);
    177 		bpa = (STIC_KSEG_TO_PHYS(kva) + PX_BUF_ALIGN - 1) &
    178 		    ~(PX_BUF_ALIGN - 1);
    179 		if (bpa + PX_BUF_SIZE > 8192*1024)
    180 			panic("px_init: allocation out of bounds");
    181 	} else {
    182 		TAILQ_INIT(&pglist);
    183 		if (uvm_pglistalloc(PX_BUF_SIZE, 0, 8192*1024, PX_BUF_ALIGN,
    184 		    PX_BUF_ALIGN, &pglist, 1, 0) != 0)
    185 			panic("px_init: allocation failure");
    186 		bpa = TAILQ_FIRST(&pglist)->phys_addr;
    187 	}
    188 
    189 	kva = (caddr_t)TC_PHYS_TO_UNCACHED(si->si_slotbase);
    190 
    191 	si->si_vdac = (u_int32_t *)(kva + PX_VDAC_OFFSET);
    192 	si->si_vdac_reset = (u_int32_t *)(kva + PX_VDAC_RESET_OFFSET);
    193 	si->si_stic = (volatile struct stic_regs *)(kva + PX_STIC_OFFSET);
    194 	si->si_stamp = (u_int32_t *)(kva + PX_STAMP_OFFSET);
    195 	si->si_buf = (u_int32_t *)TC_PHYS_TO_UNCACHED(bpa);
    196 	si->si_buf_phys =  bpa;
    197 	si->si_buf_size = PX_BUF_SIZE;
    198 	si->si_disptype = WSDISPLAY_TYPE_PX;
    199 	si->si_depth = 8;
    200 
    201 	si->si_pbuf_get = px_pbuf_get;
    202 	si->si_pbuf_post = px_pbuf_post;
    203 
    204 	memset(si->si_buf, 0, PX_BUF_SIZE);
    205 
    206 	stic_init(si);
    207 }
    208 
    209 static int
    210 px_intr(void *cookie)
    211 {
    212 	volatile struct stic_regs *sr;
    213 	struct stic_info *si;
    214 	int state;
    215 
    216 	si = cookie;
    217 	sr = si->si_stic;
    218 	state = sr->sr_ipdvint;
    219 
    220 	if ((state & STIC_INT_V) != 0) {
    221 		sr->sr_ipdvint =
    222 		    STIC_INT_V_WE | (sr->sr_ipdvint & STIC_INT_V_EN);
    223 		tc_wmb();
    224 		stic_flush(si);
    225 	}
    226 
    227 #ifdef DEBUG
    228 	if ((sr->sr_ipdvint & STIC_INT_E) != 0) {
    229 		printf("%s: error intr, %x %x %x %x %x", si->si_dv.dv_xname,
    230 		    sr->sr_ipdvint, sr->sr_sticsr, sr->sr_buscsr,
    231 		    sr->sr_busadr, sr->sr_busdat);
    232 	}
    233 #endif
    234 
    235 	return (1);
    236 }
    237 
    238 static u_int32_t *
    239 px_pbuf_get(struct stic_info *si)
    240 {
    241 
    242 	si->si_pbuf_select ^= STIC_PACKET_SIZE;
    243 	return ((u_int32_t *)((caddr_t)si->si_buf + si->si_pbuf_select));
    244 }
    245 
    246 static int
    247 px_pbuf_post(struct stic_info *si, u_int32_t *buf)
    248 {
    249 	volatile u_int32_t *poll, junk;
    250 	volatile struct stic_regs *sr;
    251 	u_long v;
    252 	int c;
    253 
    254 	sr = si->si_stic;
    255 
    256 	/* Get address of poll register for this buffer. */
    257 	v = (u_long)STIC_KSEG_TO_PHYS(buf);
    258 	v = ((v & 0xffff8000) << 3) | (v & 0x7fff);
    259 	poll = (volatile u_int32_t *)((caddr_t)si->si_slotbase + (v >> 9));
    260 
    261 	/*
    262 	 * Read the poll register and make sure the stamp wants to accept
    263 	 * our packet.  This read will initiate the DMA.  Don't wait for
    264 	 * ever, just in case something's wrong.
    265 	 */
    266 	tc_mb();
    267 
    268 	for (c = STAMP_RETRIES; c != 0; c--) {
    269 		if ((sr->sr_ipdvint & STIC_INT_P) != 0) {
    270 			sr->sr_ipdvint = STIC_INT_P_WE | STIC_INT_P_EN;
    271 			tc_wmb();
    272 			junk = *poll;
    273 			return (0);
    274 		}
    275 		DELAY(STAMP_DELAY);
    276 	}
    277 
    278 	/* STIC has lost the plot, punish it. */
    279 	stic_reset(si);
    280 	return (-1);
    281 }
    282