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