1 1.41 chs /* $NetBSD: px.c,v 1.41 2019/11/10 21:16:37 chs Exp $ */ 2 1.1 jonathan 3 1.4 ad /*- 4 1.5 ad * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc. 5 1.1 jonathan * All rights reserved. 6 1.1 jonathan * 7 1.4 ad * This code is derived from software contributed to The NetBSD Foundation 8 1.4 ad * by Andrew Doran. 9 1.4 ad * 10 1.1 jonathan * Redistribution and use in source and binary forms, with or without 11 1.1 jonathan * modification, are permitted provided that the following conditions 12 1.1 jonathan * are met: 13 1.1 jonathan * 1. Redistributions of source code must retain the above copyright 14 1.1 jonathan * notice, this list of conditions and the following disclaimer. 15 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 jonathan * notice, this list of conditions and the following disclaimer in the 17 1.1 jonathan * documentation and/or other materials provided with the distribution. 18 1.1 jonathan * 19 1.4 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.4 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.4 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.4 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.4 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.4 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.4 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.4 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.4 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.4 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.4 ad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 jonathan */ 31 1.1 jonathan 32 1.1 jonathan /* 33 1.4 ad * Driver for DEC PixelStamp graphics adapters (PMAG-C). 34 1.1 jonathan */ 35 1.10 lukem 36 1.10 lukem #include <sys/cdefs.h> 37 1.41 chs __KERNEL_RCSID(0, "$NetBSD: px.c,v 1.41 2019/11/10 21:16:37 chs Exp $"); 38 1.1 jonathan 39 1.1 jonathan #include <sys/param.h> 40 1.1 jonathan #include <sys/systm.h> 41 1.1 jonathan #include <sys/device.h> 42 1.4 ad #include <sys/malloc.h> 43 1.4 ad #include <sys/callout.h> 44 1.1 jonathan 45 1.38 uebayasi #include <uvm/uvm.h> /* XXX uvm_pageboot_alloc */ 46 1.4 ad 47 1.4 ad #if defined(pmax) 48 1.4 ad #include <mips/cpuregs.h> 49 1.4 ad #elif defined(alpha) 50 1.4 ad #include <alpha/alpha_cpu.h> 51 1.4 ad #endif 52 1.1 jonathan 53 1.1 jonathan #include <machine/autoconf.h> 54 1.31 ad #include <sys/cpu.h> 55 1.31 ad #include <sys/bus.h> 56 1.1 jonathan 57 1.4 ad #include <dev/cons.h> 58 1.4 ad 59 1.4 ad #include <dev/wscons/wsconsio.h> 60 1.4 ad #include <dev/wscons/wsdisplayvar.h> 61 1.4 ad 62 1.4 ad #include <dev/ic/bt459reg.h> 63 1.4 ad 64 1.4 ad #include <dev/tc/tcvar.h> 65 1.4 ad #include <dev/tc/sticreg.h> 66 1.9 ad #include <dev/tc/sticio.h> 67 1.4 ad #include <dev/tc/sticvar.h> 68 1.4 ad 69 1.4 ad #define PX_STIC_POLL_OFFSET 0x000000 /* STIC DMA poll space */ 70 1.4 ad #define PX_STAMP_OFFSET 0x0c0000 /* pixelstamp space on STIC */ 71 1.4 ad #define PX_STIC_OFFSET 0x180000 /* STIC registers */ 72 1.4 ad #define PX_VDAC_OFFSET 0x200000 /* VDAC registers (bt459) */ 73 1.4 ad #define PX_VDAC_RESET_OFFSET 0x300000 /* VDAC reset register */ 74 1.4 ad #define PX_ROM_OFFSET 0x300000 /* ROM code */ 75 1.4 ad 76 1.9 ad #define PX_BUF_COUNT 16 77 1.9 ad #define PX_BUF_INC(x) ((x + 1) & (PX_BUF_COUNT - 1)) 78 1.9 ad 79 1.9 ad /* 80 1.9 ad * We need enough aligned memory to hold: 81 1.9 ad * 82 1.9 ad * - Xserver communication area (4096 bytes) 83 1.9 ad * - 16 packet buffers (4096 bytes each) 84 1.9 ad * - 2 image buffers (5120 bytes each) 85 1.9 ad * 86 1.9 ad */ 87 1.9 ad #define PX_BUF_SIZE \ 88 1.9 ad (STIC_PACKET_SIZE * PX_BUF_COUNT + STIC_IMGBUF_SIZE*2 + STIC_XCOMM_SIZE) 89 1.4 ad #define PX_BUF_ALIGN 32768 90 1.4 ad 91 1.9 ad #define PXF_QUEUE 0x01 92 1.9 ad 93 1.36 cegger static void px_attach(device_t, device_t, void *); 94 1.28 thorpej static void px_init(struct stic_info *, int); 95 1.29 christos static int px_ioctl(struct stic_info *, u_long, void *, int, 96 1.28 thorpej struct lwp *); 97 1.36 cegger static int px_match(device_t, cfdata_t, void *); 98 1.28 thorpej 99 1.28 thorpej static int px_intr(void *); 100 1.28 thorpej static uint32_t *px_pbuf_get(struct stic_info *); 101 1.37 tsutsui static int px_pbuf_post(struct stic_info *, uint32_t *); 102 1.1 jonathan 103 1.4 ad void px_cnattach(tc_addr_t); 104 1.1 jonathan 105 1.1 jonathan struct px_softc { 106 1.34 joerg device_t px_dev; 107 1.4 ad struct stic_info *px_si; 108 1.37 tsutsui volatile uint32_t *px_qpoll[PX_BUF_COUNT]; 109 1.1 jonathan }; 110 1.1 jonathan 111 1.34 joerg CFATTACH_DECL_NEW(px, sizeof(struct px_softc), 112 1.17 thorpej px_match, px_attach, NULL, NULL); 113 1.1 jonathan 114 1.28 thorpej static int 115 1.34 joerg px_match(device_t parent, cfdata_t match, void *aux) 116 1.4 ad { 117 1.4 ad struct tc_attach_args *ta; 118 1.4 ad 119 1.4 ad ta = aux; 120 1.1 jonathan 121 1.4 ad return (strncmp("PMAG-CA ", ta->ta_modname, TC_ROM_LLEN) == 0); 122 1.1 jonathan } 123 1.1 jonathan 124 1.28 thorpej static void 125 1.34 joerg px_attach(device_t parent, device_t self, void *aux) 126 1.4 ad { 127 1.4 ad struct stic_info *si; 128 1.4 ad struct tc_attach_args *ta; 129 1.4 ad struct px_softc *px; 130 1.9 ad int console, i; 131 1.9 ad u_long v; 132 1.4 ad 133 1.27 thorpej px = device_private(self); 134 1.39 chs ta = aux; 135 1.4 ad 136 1.34 joerg px->px_dev = self; 137 1.34 joerg 138 1.4 ad if (ta->ta_addr == stic_consinfo.si_slotbase) { 139 1.4 ad si = &stic_consinfo; 140 1.4 ad console = 1; 141 1.4 ad } else { 142 1.20 mycroft if (stic_consinfo.si_slotbase == 0) 143 1.4 ad si = &stic_consinfo; 144 1.4 ad else { 145 1.41 chs si = malloc(sizeof(*si), M_DEVBUF, M_WAITOK | M_ZERO); 146 1.4 ad } 147 1.4 ad si->si_slotbase = ta->ta_addr; 148 1.4 ad px_init(si, 0); 149 1.4 ad console = 0; 150 1.4 ad } 151 1.4 ad 152 1.4 ad px->px_si = si; 153 1.9 ad si->si_dv = self; 154 1.4 ad tc_intr_establish(parent, ta->ta_cookie, IPL_TTY, px_intr, si); 155 1.1 jonathan 156 1.7 ad printf(": 8 plane, %dx%d stamp\n", si->si_stampw, si->si_stamph); 157 1.1 jonathan 158 1.9 ad for (i = 0; i < PX_BUF_COUNT; i++) { 159 1.9 ad v = i * STIC_PACKET_SIZE + 160 1.9 ad si->si_buf_phys + STIC_XCOMM_SIZE; 161 1.9 ad v = ((v & 0xffff8000) << 3) | (v & 0x7fff); 162 1.37 tsutsui px->px_qpoll[i] = (volatile uint32_t *) 163 1.30 yamt ((char *)si->si_slotbase + (v >> 9)); 164 1.9 ad } 165 1.9 ad 166 1.4 ad stic_attach(self, si, console); 167 1.4 ad } 168 1.1 jonathan 169 1.4 ad void 170 1.4 ad px_cnattach(tc_addr_t addr) 171 1.4 ad { 172 1.4 ad struct stic_info *si; 173 1.1 jonathan 174 1.4 ad si = &stic_consinfo; 175 1.4 ad si->si_slotbase = addr; 176 1.4 ad px_init(si, 1); 177 1.4 ad stic_cnattach(si); 178 1.1 jonathan } 179 1.4 ad 180 1.28 thorpej static void 181 1.4 ad px_init(struct stic_info *si, int bootstrap) 182 1.1 jonathan { 183 1.4 ad struct pglist pglist; 184 1.30 yamt char *kva, *bva; 185 1.4 ad paddr_t bpa; 186 1.4 ad 187 1.29 christos kva = (void *)si->si_slotbase; 188 1.21 chs 189 1.4 ad /* 190 1.4 ad * Allocate memory for the packet buffers. It must be located below 191 1.4 ad * 8MB, since the STIC can't access outside that region. Also, due 192 1.4 ad * to the holes in STIC address space, each buffer mustn't cross a 193 1.4 ad * 32kB boundary. 194 1.4 ad */ 195 1.4 ad if (bootstrap) { 196 1.23 perry /* 197 1.8 wiz * UVM won't be initialised at this point, so grab memory 198 1.4 ad * directly from vm_physmem[]. 199 1.4 ad */ 200 1.30 yamt bva = (char *)uvm_pageboot_alloc(PX_BUF_SIZE + PX_BUF_ALIGN); 201 1.22 mhitch bpa = (STIC_KSEG_TO_PHYS(bva) + PX_BUF_ALIGN - 1) & 202 1.4 ad ~(PX_BUF_ALIGN - 1); 203 1.4 ad if (bpa + PX_BUF_SIZE > 8192*1024) 204 1.4 ad panic("px_init: allocation out of bounds"); 205 1.4 ad } else { 206 1.9 ad if (uvm_pglistalloc(PX_BUF_SIZE, 0, 8192*1024, PX_BUF_ALIGN, 207 1.9 ad 0, &pglist, 1, 0) != 0) 208 1.4 ad panic("px_init: allocation failure"); 209 1.25 yamt bpa = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist)); 210 1.4 ad } 211 1.1 jonathan 212 1.37 tsutsui si->si_vdac = (uint32_t *)(kva + PX_VDAC_OFFSET); 213 1.37 tsutsui si->si_vdac_reset = (uint32_t *)(kva + PX_VDAC_RESET_OFFSET); 214 1.4 ad si->si_stic = (volatile struct stic_regs *)(kva + PX_STIC_OFFSET); 215 1.37 tsutsui si->si_stamp = (uint32_t *)(kva + PX_STAMP_OFFSET); 216 1.37 tsutsui si->si_buf = (uint32_t *)TC_PHYS_TO_UNCACHED(bpa); 217 1.9 ad si->si_buf_phys = bpa; 218 1.4 ad si->si_buf_size = PX_BUF_SIZE; 219 1.4 ad si->si_disptype = WSDISPLAY_TYPE_PX; 220 1.4 ad si->si_depth = 8; 221 1.9 ad si->si_sxc = (volatile struct stic_xcomm *)si->si_buf; 222 1.1 jonathan 223 1.4 ad si->si_pbuf_get = px_pbuf_get; 224 1.4 ad si->si_pbuf_post = px_pbuf_post; 225 1.9 ad si->si_ioctl = px_ioctl; 226 1.1 jonathan 227 1.4 ad memset(si->si_buf, 0, PX_BUF_SIZE); 228 1.4 ad 229 1.4 ad stic_init(si); 230 1.4 ad } 231 1.1 jonathan 232 1.28 thorpej static int 233 1.4 ad px_intr(void *cookie) 234 1.4 ad { 235 1.4 ad volatile struct stic_regs *sr; 236 1.9 ad volatile struct stic_xcomm *sxc; 237 1.4 ad struct stic_info *si; 238 1.9 ad struct px_softc *px; 239 1.4 ad int state; 240 1.4 ad 241 1.4 ad si = cookie; 242 1.39 chs px = device_private(si->si_dv); 243 1.4 ad sr = si->si_stic; 244 1.4 ad state = sr->sr_ipdvint; 245 1.9 ad sxc = si->si_sxc; 246 1.4 ad 247 1.9 ad /* 248 1.9 ad * Vertical-retrace condition. 249 1.9 ad * 250 1.9 ad * Clear the flag and flush out any waiting VDAC updates. We do 251 1.9 ad * this at retrace time to avoid producing `shearing' and other 252 1.9 ad * nasty artifacts. 253 1.23 perry */ 254 1.4 ad if ((state & STIC_INT_V) != 0) { 255 1.9 ad sr->sr_ipdvint = STIC_INT_V_WE | STIC_INT_V_EN; 256 1.1 jonathan tc_wmb(); 257 1.4 ad stic_flush(si); 258 1.1 jonathan } 259 1.1 jonathan 260 1.9 ad /* 261 1.9 ad * Error condition. 262 1.9 ad * 263 1.9 ad * Simply clear the flag and report the error. 264 1.9 ad */ 265 1.9 ad if ((state & STIC_INT_E) != 0) { 266 1.34 joerg aprint_error_dev(px->px_dev, "error intr, %x %x %x %x %x", 267 1.4 ad sr->sr_ipdvint, sr->sr_sticsr, sr->sr_buscsr, 268 1.4 ad sr->sr_busadr, sr->sr_busdat); 269 1.9 ad sr->sr_ipdvint = STIC_INT_E_WE | STIC_INT_E_EN; 270 1.9 ad tc_wmb(); 271 1.9 ad } 272 1.9 ad 273 1.9 ad /* 274 1.9 ad * Check for queue stalls. 275 1.9 ad */ 276 1.9 ad if (sxc->sxc_tail != sxc->sxc_head && !sxc->sxc_busy) 277 1.9 ad state |= STIC_INT_P; 278 1.9 ad 279 1.9 ad /* 280 1.9 ad * Packet-done condition. 281 1.9 ad * 282 1.9 ad * If packet queueing is enabled, clear the condition, and increment 283 1.9 ad * the tail (submitted) pointer. 284 1.9 ad */ 285 1.9 ad if ((si->si_hwflags & PXF_QUEUE) != 0 && (state & STIC_INT_P) != 0) { 286 1.9 ad sr->sr_ipdvint = STIC_INT_P_WE | STIC_INT_P_EN; 287 1.9 ad tc_wmb(); 288 1.9 ad 289 1.9 ad if (sxc->sxc_tail != sxc->sxc_head) { 290 1.9 ad sxc->sxc_done[sxc->sxc_tail] = 0; 291 1.9 ad sxc->sxc_tail = PX_BUF_INC(sxc->sxc_tail); 292 1.9 ad } 293 1.9 ad 294 1.9 ad if (sxc->sxc_tail != sxc->sxc_head) { 295 1.9 ad if (*px->px_qpoll[sxc->sxc_tail] != STAMP_OK) { 296 1.9 ad sxc->sxc_nreject++; 297 1.9 ad sxc->sxc_busy = 0; 298 1.9 ad } else 299 1.9 ad sxc->sxc_busy = 1; 300 1.9 ad } else 301 1.9 ad sxc->sxc_busy = 0; 302 1.4 ad } 303 1.9 ad 304 1.9 ad if ((si->si_hwflags & PXF_QUEUE) != 0 && (state & STIC_INT_P_EN) == 0) 305 1.9 ad printf("px_intr: STIC_INT_P_EN == 0\n"); 306 1.1 jonathan 307 1.4 ad return (1); 308 1.1 jonathan } 309 1.1 jonathan 310 1.28 thorpej static uint32_t * 311 1.4 ad px_pbuf_get(struct stic_info *si) 312 1.1 jonathan { 313 1.9 ad u_long off; 314 1.1 jonathan 315 1.4 ad si->si_pbuf_select ^= STIC_PACKET_SIZE; 316 1.9 ad off = si->si_pbuf_select + STIC_XCOMM_SIZE; 317 1.37 tsutsui return ((uint32_t *)((char *)si->si_buf + off)); 318 1.1 jonathan } 319 1.1 jonathan 320 1.28 thorpej static int 321 1.37 tsutsui px_pbuf_post(struct stic_info *si, uint32_t *buf) 322 1.1 jonathan { 323 1.37 tsutsui volatile uint32_t *poll, junk; 324 1.5 ad volatile struct stic_regs *sr; 325 1.4 ad u_long v; 326 1.4 ad int c; 327 1.4 ad 328 1.5 ad sr = si->si_stic; 329 1.5 ad 330 1.4 ad /* Get address of poll register for this buffer. */ 331 1.4 ad v = (u_long)STIC_KSEG_TO_PHYS(buf); 332 1.4 ad v = ((v & 0xffff8000) << 3) | (v & 0x7fff); 333 1.37 tsutsui poll = (volatile uint32_t *)((char *)si->si_slotbase + (v >> 9)); 334 1.4 ad 335 1.4 ad /* 336 1.4 ad * Read the poll register and make sure the stamp wants to accept 337 1.4 ad * our packet. This read will initiate the DMA. Don't wait for 338 1.4 ad * ever, just in case something's wrong. 339 1.4 ad */ 340 1.5 ad tc_mb(); 341 1.4 ad 342 1.4 ad for (c = STAMP_RETRIES; c != 0; c--) { 343 1.5 ad if ((sr->sr_ipdvint & STIC_INT_P) != 0) { 344 1.9 ad sr->sr_ipdvint = STIC_INT_P_WE; 345 1.5 ad tc_wmb(); 346 1.5 ad junk = *poll; 347 1.40 christos __USE(junk); 348 1.4 ad return (0); 349 1.5 ad } 350 1.4 ad DELAY(STAMP_DELAY); 351 1.4 ad } 352 1.1 jonathan 353 1.4 ad /* STIC has lost the plot, punish it. */ 354 1.4 ad stic_reset(si); 355 1.4 ad return (-1); 356 1.9 ad } 357 1.9 ad 358 1.28 thorpej static int 359 1.29 christos px_ioctl(struct stic_info *si, u_long cmd, void *data, int flag, 360 1.26 christos struct lwp *l) 361 1.9 ad { 362 1.9 ad volatile struct stic_xcomm *sxc; 363 1.9 ad volatile struct stic_regs *sr; 364 1.9 ad struct stic_xinfo *sxi; 365 1.9 ad int rv, s; 366 1.9 ad 367 1.9 ad sr = si->si_stic; 368 1.9 ad 369 1.9 ad switch (cmd) { 370 1.9 ad case STICIO_STARTQ: 371 1.9 ad if (si->si_dispmode != WSDISPLAYIO_MODE_MAPPED || 372 1.9 ad (si->si_hwflags & PXF_QUEUE) != 0) { 373 1.9 ad rv = EBUSY; 374 1.9 ad break; 375 1.9 ad } 376 1.9 ad 377 1.9 ad sxc = si->si_sxc; 378 1.24 he memset((void *)__UNVOLATILE(sxc->sxc_done), 0, 379 1.24 he sizeof(sxc->sxc_done)); 380 1.9 ad sxc->sxc_head = 0; 381 1.9 ad sxc->sxc_tail = 0; 382 1.9 ad sxc->sxc_nreject = 0; 383 1.9 ad sxc->sxc_nstall = 0; 384 1.9 ad 385 1.9 ad s = spltty(); 386 1.9 ad si->si_hwflags |= PXF_QUEUE; 387 1.9 ad sr->sr_ipdvint = STIC_INT_P_WE | STIC_INT_P_EN; 388 1.9 ad tc_wmb(); 389 1.9 ad splx(s); 390 1.9 ad 391 1.9 ad rv = 0; 392 1.9 ad break; 393 1.9 ad 394 1.9 ad case STICIO_STOPQ: 395 1.9 ad s = spltty(); 396 1.9 ad si->si_hwflags &= ~PXF_QUEUE; 397 1.9 ad sr->sr_ipdvint = STIC_INT_P_WE | STIC_INT_P; 398 1.9 ad tc_wmb(); 399 1.9 ad splx(s); 400 1.9 ad rv = 0; 401 1.9 ad break; 402 1.9 ad 403 1.9 ad case STICIO_GXINFO: 404 1.9 ad sxi = (struct stic_xinfo *)data; 405 1.9 ad sxi->sxi_unit = si->si_unit; 406 1.9 ad sxi->sxi_stampw = si->si_stampw; 407 1.9 ad sxi->sxi_stamph = si->si_stamph; 408 1.9 ad sxi->sxi_buf_size = si->si_buf_size; 409 1.9 ad sxi->sxi_buf_phys = (u_int)si->si_buf_phys; 410 1.9 ad sxi->sxi_buf_pktoff = STIC_XCOMM_SIZE; 411 1.9 ad sxi->sxi_buf_pktcnt = PX_BUF_COUNT; 412 1.9 ad sxi->sxi_buf_imgoff = 413 1.9 ad STIC_XCOMM_SIZE + STIC_PACKET_SIZE * PX_BUF_COUNT; 414 1.9 ad rv = 0; 415 1.9 ad break; 416 1.9 ad 417 1.9 ad default: 418 1.13 atatat rv = EPASSTHROUGH; 419 1.9 ad break; 420 1.9 ad } 421 1.9 ad 422 1.9 ad return (rv); 423 1.1 jonathan } 424