1 /* $NetBSD: px.c,v 1.4 2000/12/17 13:52:03 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000 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_slotkva = (u_int32_t *)kva; 192 si->si_vdac = (u_int32_t *)(kva + PX_VDAC_OFFSET); 193 si->si_vdac_reset = (u_int32_t *)(kva + PX_VDAC_RESET_OFFSET); 194 si->si_stic = (volatile struct stic_regs *)(kva + PX_STIC_OFFSET); 195 si->si_stamp = (u_int32_t *)(kva + PX_STAMP_OFFSET); 196 si->si_buf = (u_int32_t *)TC_PHYS_TO_UNCACHED(bpa); 197 si->si_buf_phys = bpa; 198 si->si_buf_size = PX_BUF_SIZE; 199 si->si_disptype = WSDISPLAY_TYPE_PX; 200 si->si_depth = 8; 201 202 si->si_pbuf_get = px_pbuf_get; 203 si->si_pbuf_post = px_pbuf_post; 204 205 memset(si->si_buf, 0, PX_BUF_SIZE); 206 207 stic_init(si); 208 } 209 210 static int 211 px_intr(void *cookie) 212 { 213 volatile struct stic_regs *sr; 214 struct stic_info *si; 215 int state; 216 217 si = cookie; 218 sr = si->si_stic; 219 state = sr->sr_ipdvint; 220 221 if ((state & STIC_INT_V) != 0) { 222 sr->sr_ipdvint = 223 STIC_INT_V_WE | (sr->sr_ipdvint & STIC_INT_V_EN); 224 tc_wmb(); 225 stic_flush(si); 226 } 227 228 if ((state & STIC_INT_P) != 0) { 229 sr->sr_ipdvint = 230 STIC_INT_P_WE | (sr->sr_ipdvint & STIC_INT_P_EN); 231 tc_wmb(); 232 } 233 234 #ifdef DEBUG 235 if ((sr->sr_ipdvint & STIC_INT_E) != 0) { 236 printf("%s: error intr, %x %x %x %x %x", si->si_dv.dv_xname, 237 sr->sr_ipdvint, sr->sr_sticsr, sr->sr_buscsr, 238 sr->sr_busadr, sr->sr_busdat); 239 } 240 #endif 241 242 return (1); 243 } 244 245 static u_int32_t * 246 px_pbuf_get(struct stic_info *si) 247 { 248 249 /* 250 * XXX We should be synchronizing with STIC_INT_P so that an ISR 251 * doesn't blow us up. 252 */ 253 si->si_pbuf_select ^= STIC_PACKET_SIZE; 254 return ((u_int32_t *)((caddr_t)si->si_buf + si->si_pbuf_select)); 255 } 256 257 static int 258 px_pbuf_post(struct stic_info *si, u_int32_t *buf) 259 { 260 volatile u_int32_t *poll; 261 u_long v; 262 int c; 263 264 /* Get address of poll register for this buffer. */ 265 v = (u_long)STIC_KSEG_TO_PHYS(buf); 266 v = ((v & 0xffff8000) << 3) | (v & 0x7fff); 267 poll = (volatile u_int32_t *)((caddr_t)si->si_slotkva + (v >> 9)); 268 269 /* 270 * Read the poll register and make sure the stamp wants to accept 271 * our packet. This read will initiate the DMA. Don't wait for 272 * ever, just in case something's wrong. 273 */ 274 tc_syncbus(); 275 276 for (c = STAMP_RETRIES; c != 0; c--) { 277 if (*poll == STAMP_OK) 278 return (0); 279 DELAY(STAMP_DELAY); 280 } 281 282 /* STIC has lost the plot, punish it. */ 283 stic_reset(si); 284 return (-1); 285 } 286