bktr_os.c revision 1.4 1 /* $NetBSD: bktr_os.c,v 1.4 2000/05/07 14:43:32 veego Exp $ */
2
3 /* FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.7 2000/04/16 07:50:09 roger Exp */
4
5 /*
6 * This is part of the Driver for Video Capture Cards (Frame grabbers)
7 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
8 * chipset.
9 * Copyright Roger Hardiman and Amancio Hasty.
10 *
11 * bktr_os : This has all the Operating System dependant code,
12 * probe/attach and open/close/ioctl/read/mmap
13 * memory allocation
14 * PCI bus interfacing
15 *
16 *
17 */
18
19 /*
20 * 1. Redistributions of source code must retain the
21 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by Amancio Hasty and
35 * Roger Hardiman
36 * 4. The name of the author may not be used to endorse or promote products
37 * derived from this software without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
41 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
43 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52
53 #ifdef __FreeBSD__
54 #include "bktr.h"
55 #include "opt_devfs.h"
56 #endif /* __FreeBSD__ */
57
58 #include "opt_bktr.h" /* include any kernel config options */
59
60 #define FIFO_RISC_DISABLED 0
61 #define ALL_INTS_DISABLED 0
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/conf.h>
66 #include <sys/uio.h>
67 #include <sys/kernel.h>
68 #include <sys/signalvar.h>
69 #include <sys/mman.h>
70 #include <sys/poll.h>
71 #include <sys/select.h>
72 #include <sys/vnode.h>
73
74 #include <vm/vm.h>
75 #include <vm/vm_kern.h>
76 #include <vm/pmap.h>
77 #include <vm/vm_extern.h>
78
79 /*******************/
80 /* *** FreeBSD *** */
81 /*******************/
82 #ifdef __FreeBSD__
83
84 #if (__FreeBSD_version < 400000)
85 #ifdef DEVFS
86 #include <sys/devfsext.h>
87 #endif /* DEVFS */
88 #endif
89
90 #if (__FreeBSD_version >=400000) || (NSMBUS > 0)
91 #include <sys/bus.h> /* used by smbus and newbus */
92 #endif
93
94 #if (__FreeBSD_version >=300000)
95 #include <machine/bus_memio.h> /* used by bus space */
96 #include <machine/bus.h> /* used by bus space and newbus */
97 #include <sys/bus.h>
98 #endif
99
100 #if (__FreeBSD_version >=400000)
101 #include <sys/rman.h> /* used by newbus */
102 #include <machine/resource.h> /* used by newbus */
103 #endif
104
105
106 #include <machine/clock.h> /* for DELAY */
107 #include <pci/pcivar.h>
108 #include <pci/pcireg.h>
109 #endif
110
111 #ifdef __NetBSD__
112 #include <dev/ic/bt8xx.h> /* NetBSD location for .h files */
113 #include <dev/pci/bktr/bktr_reg.h>
114 #include <dev/pci/bktr/bktr_tuner.h>
115 #include <dev/pci/bktr/bktr_card.h>
116 #include <dev/pci/bktr/bktr_audio.h>
117 #include <dev/pci/bktr/bktr_core.h>
118 #include <dev/pci/bktr/bktr_os.h>
119 #else /* Traditional location for .h files */
120 #include <machine/ioctl_meteor.h>
121 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
122 #include <dev/bktr/bktr_reg.h>
123 #include <dev/bktr/bktr_tuner.h>
124 #include <dev/bktr/bktr_card.h>
125 #include <dev/bktr/bktr_audio.h>
126 #include <dev/bktr/bktr_core.h>
127 #include <dev/bktr/bktr_os.h>
128 #endif
129
130 #if defined(__FreeBSD__)
131 #if (NSMBUS > 0)
132 #include <dev/bktr/bktr_i2c.h>
133 #endif
134
135 #include <sys/sysctl.h>
136 int bt848_card = -1;
137 int bt848_tuner = -1;
138 int bt848_reverse_mute = -1;
139 int bt848_format = -1;
140 int bt848_slow_msp_audio = -1;
141
142 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
143 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
144 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
145 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
146 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
147 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
148 #endif
149
150 #if (__FreeBSD__ == 2)
151 #define PCIR_REVID PCI_CLASS_REG
152 #endif
153
154
155 /****************/
156 /* *** BSDI *** */
157 /****************/
158 #ifdef __bsdi__
159 #endif /* __bsdi__ */
160
161
162 /**************************/
163 /* *** OpenBSD/NetBSD *** */
164 /**************************/
165 #if defined(__NetBSD__) || defined(__OpenBSD__)
166 #include <sys/device.h>
167 #include <dev/pci/pcivar.h>
168 #include <dev/pci/pcireg.h>
169 #include <dev/pci/pcidevs.h>
170
171 #define BKTR_DEBUG
172 #ifdef BKTR_DEBUG
173 int bktr_debug = 0;
174 #define DPR(x) (bktr_debug ? printf x : 0)
175 #else
176 #define DPR(x)
177 #endif
178 #endif /* __NetBSD__ || __OpenBSD__ */
179
180
181
182 /****************************/
183 /* *** FreeBSD 4.x code *** */
184 /****************************/
185 #if (__FreeBSD_version >= 400000)
186
187 static int bktr_probe( device_t dev );
188 static int bktr_attach( device_t dev );
189 static int bktr_detach( device_t dev );
190 static int bktr_shutdown( device_t dev );
191 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
192
193 static device_method_t bktr_methods[] = {
194 /* Device interface */
195 DEVMETHOD(device_probe, bktr_probe),
196 DEVMETHOD(device_attach, bktr_attach),
197 DEVMETHOD(device_detach, bktr_detach),
198 DEVMETHOD(device_shutdown, bktr_shutdown),
199
200 { 0, 0 }
201 };
202
203 static driver_t bktr_driver = {
204 "bktr",
205 bktr_methods,
206 sizeof(struct bktr_softc),
207 };
208
209 static devclass_t bktr_devclass;
210
211 static d_open_t bktr_open;
212 static d_close_t bktr_close;
213 static d_read_t bktr_read;
214 static d_write_t bktr_write;
215 static d_ioctl_t bktr_ioctl;
216 static d_mmap_t bktr_mmap;
217 static d_poll_t bktr_poll;
218
219 #define CDEV_MAJOR 92
220 static struct cdevsw bktr_cdevsw = {
221 /* open */ bktr_open,
222 /* close */ bktr_close,
223 /* read */ bktr_read,
224 /* write */ bktr_write,
225 /* ioctl */ bktr_ioctl,
226 /* poll */ bktr_poll,
227 /* mmap */ bktr_mmap,
228 /* strategy */ nostrategy,
229 /* name */ "bktr",
230 /* maj */ CDEV_MAJOR,
231 /* dump */ nodump,
232 /* psize */ nopsize,
233 /* flags */ 0,
234 /* bmaj */ -1
235 };
236
237 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
238
239
240 /*
241 * the boot time probe routine.
242 */
243 static int
244 bktr_probe( device_t dev )
245 {
246 unsigned int type = pci_get_devid(dev);
247 unsigned int rev = pci_get_revid(dev);
248
249 switch (type) {
250 case BROOKTREE_848_PCI_ID:
251 if (rev == 0x12) device_set_desc(dev, "BrookTree 848A");
252 else device_set_desc(dev, "BrookTree 848");
253 return 0;
254 case BROOKTREE_849_PCI_ID:
255 device_set_desc(dev, "BrookTree 849A");
256 return 0;
257 case BROOKTREE_878_PCI_ID:
258 device_set_desc(dev, "BrookTree 878");
259 return 0;
260 case BROOKTREE_879_PCI_ID:
261 device_set_desc(dev, "BrookTree 879");
262 return 0;
263 };
264
265 return ENXIO;
266 }
267
268
269 /*
270 * the attach routine.
271 */
272 static int
273 bktr_attach( device_t dev )
274 {
275 u_long latency;
276 u_long fun;
277 u_long val;
278 unsigned int rev;
279 unsigned int unit;
280 int error = 0;
281 int rid;
282 #ifdef BROOKTREE_IRQ
283 u_long old_irq, new_irq;
284 #endif
285
286 struct bktr_softc *bktr = device_get_softc(dev);
287
288 unit = device_get_unit(dev);
289
290 /*
291 * Enable bus mastering and Memory Mapped device
292 */
293 val = pci_read_config(dev, PCIR_COMMAND, 4);
294 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
295 pci_write_config(dev, PCIR_COMMAND, val, 4);
296
297 /*
298 * Map control/status registers.
299 */
300 rid = PCI_MAP_REG_START;
301 bktr->res_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
302 0, ~0, 1, RF_ACTIVE);
303
304 if (!bktr->res_mem) {
305 device_printf(dev, "could not map memory\n");
306 error = ENXIO;
307 goto fail;
308 }
309 bktr->memt = rman_get_bustag(bktr->res_mem);
310 bktr->memh = rman_get_bushandle(bktr->res_mem);
311
312
313 /*
314 * Disable the brooktree device
315 */
316 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
317 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
318
319
320 #ifdef BROOKTREE_IRQ /* from the configuration file */
321 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
322 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
323 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
324 printf("bktr%d: attach: irq changed from %d to %d\n",
325 unit, (old_irq & 0xff), (new_irq & 0xff));
326 #endif
327
328 /*
329 * Allocate our interrupt.
330 */
331 rid = 0;
332 bktr->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
333 RF_SHAREABLE | RF_ACTIVE);
334 if (bktr->res_irq == NULL) {
335 device_printf(dev, "could not map interrupt\n");
336 error = ENXIO;
337 goto fail;
338 }
339
340 error = bus_setup_intr(dev, bktr->res_irq, INTR_TYPE_TTY,
341 bktr_intr, bktr, &bktr->res_ih);
342 if (error) {
343 device_printf(dev, "could not setup irq\n");
344 goto fail;
345
346 }
347
348
349 /* Update the Device Control Register */
350 /* on Bt878 and Bt879 cards */
351 fun = pci_read_config( dev, 0x40, 2);
352 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
353
354 #if defined( BKTR_430_FX_MODE )
355 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
356 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
357 #endif
358
359 #if defined( BKTR_SIS_VIA_MODE )
360 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
361 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
362 OPTi chipset motherboards too */
363 #endif
364 pci_write_config(dev, 0x40, fun, 2);
365
366
367 /* XXX call bt848_i2c dependent attach() routine */
368 #if (NSMBUS > 0)
369 if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
370 printf("bktr%d: i2c_attach: can't attach\n", unit);
371 #endif
372
373
374 /*
375 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
376 * you have more than four, then 16 would probably be a better value.
377 */
378 #ifndef BROOKTREE_DEF_LATENCY_VALUE
379 #define BROOKTREE_DEF_LATENCY_VALUE 10
380 #endif
381 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
382 latency = (latency >> 8) & 0xff;
383 if ( bootverbose ) {
384 if (latency)
385 printf("brooktree%d: PCI bus latency is", unit);
386 else
387 printf("brooktree%d: PCI bus latency was 0 changing to",
388 unit);
389 }
390 if ( !latency ) {
391 latency = BROOKTREE_DEF_LATENCY_VALUE;
392 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
393 }
394 if ( bootverbose ) {
395 printf(" %d.\n", (int) latency);
396 }
397
398 /* read the pci device id and revision id */
399 fun = pci_get_devid(dev);
400 rev = pci_get_revid(dev);
401
402 /* call the common attach code */
403 common_bktr_attach( bktr, unit, fun, rev );
404
405 make_dev(&bktr_cdevsw, unit, 0, 0, 0444, "bktr%d", unit);
406 make_dev(&bktr_cdevsw, unit+16, 0, 0, 0444, "tuner%d", unit);
407 make_dev(&bktr_cdevsw, unit+32, 0, 0, 0444, "vbi%d", unit);
408
409 return 0;
410
411 fail:
412 return error;
413
414 }
415
416 /*
417 * the detach routine.
418 */
419 static int
420 bktr_detach( device_t dev )
421 {
422 struct bktr_softc *bktr = device_get_softc(dev);
423
424 /* Disable the brooktree device */
425 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
426 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
427
428 /* FIXME - Free memory for RISC programs, grab buffer, vbi buffers */
429
430 /*
431 * Deallocate resources.
432 */
433 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
434 bus_release_resource(dev, SYS_RES_IRQ, 0, bktr->res_irq);
435 bus_release_resource(dev, SYS_RES_MEMORY, PCI_MAP_REG_START, bktr->res_mem);
436
437 return 0;
438 }
439
440 /*
441 * the shutdown routine.
442 */
443 static int
444 bktr_shutdown( device_t dev )
445 {
446 struct bktr_softc *bktr = device_get_softc(dev);
447
448 /* Disable the brooktree device */
449 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
450 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
451
452 return 0;
453 }
454
455
456 /*
457 * Special Memory Allocation
458 */
459 vm_offset_t
460 get_bktr_mem( int unit, unsigned size )
461 {
462 vm_offset_t addr = 0;
463
464 addr = vm_page_alloc_contig(size, 0, 0xffffffff, 1<<24);
465 if (addr == 0)
466 addr = vm_page_alloc_contig(size, 0, 0xffffffff, PAGE_SIZE);
467 if (addr == 0) {
468 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
469 unit, size);
470 }
471
472 return( addr );
473 }
474
475
476 /*---------------------------------------------------------
477 **
478 ** BrookTree 848 character device driver routines
479 **
480 **---------------------------------------------------------
481 */
482
483 #define VIDEO_DEV 0x00
484 #define TUNER_DEV 0x01
485 #define VBI_DEV 0x02
486
487 #define UNIT(x) ((x) & 0x0f)
488 #define FUNCTION(x) (x >> 4)
489
490 /*
491 *
492 */
493 int
494 bktr_open( dev_t dev, int flags, int fmt, struct proc *p )
495 {
496 bktr_ptr_t bktr;
497 int unit;
498 int result;
499
500 unit = UNIT( minor(dev) );
501
502 /* Get the device data */
503 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
504 if (bktr == NULL) {
505 /* the device is no longer valid/functioning */
506 return (ENXIO);
507 }
508
509 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
510 return( ENXIO );
511
512 /* Record that the device is now busy */
513 device_busy(devclass_get_device(bktr_devclass, unit));
514
515
516 if (bt848_card != -1) {
517 if ((bt848_card >> 8 == unit ) &&
518 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
519 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
520 bktr->bt848_card = (bt848_card & 0xff);
521 probeCard(bktr, FALSE, unit);
522 }
523 }
524 }
525
526 if (bt848_tuner != -1) {
527 if ((bt848_tuner >> 8 == unit ) &&
528 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
529 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
530 bktr->bt848_tuner = (bt848_tuner & 0xff);
531 probeCard(bktr, FALSE, unit);
532 }
533 }
534 }
535
536 if (bt848_reverse_mute != -1) {
537 if ((bt848_reverse_mute >> 8) == unit ) {
538 bktr->reverse_mute = bt848_reverse_mute & 0xff;
539 }
540 }
541
542 if (bt848_slow_msp_audio != -1) {
543 if ((bt848_slow_msp_audio >> 8) == unit ) {
544 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
545 }
546 }
547
548 switch ( FUNCTION( minor(dev) ) ) {
549 case VIDEO_DEV:
550 result = video_open( bktr );
551 break;
552 case TUNER_DEV:
553 result = tuner_open( bktr );
554 break;
555 case VBI_DEV:
556 result = vbi_open( bktr );
557 break;
558 default:
559 result = ENXIO;
560 break;
561 }
562
563 /* If there was an error opening the device, undo the busy status */
564 if (result != 0)
565 device_unbusy(devclass_get_device(bktr_devclass, unit));
566 return( result );
567 }
568
569
570 /*
571 *
572 */
573 int
574 bktr_close( dev_t dev, int flags, int fmt, struct proc *p )
575 {
576 bktr_ptr_t bktr;
577 int unit;
578 int result;
579
580 unit = UNIT( minor(dev) );
581
582 /* Get the device data */
583 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
584 if (bktr == NULL) {
585 /* the device is no longer valid/functioning */
586 return (ENXIO);
587 }
588
589 switch ( FUNCTION( minor(dev) ) ) {
590 case VIDEO_DEV:
591 result = video_close( bktr );
592 break;
593 case TUNER_DEV:
594 result = tuner_close( bktr );
595 break;
596 case VBI_DEV:
597 result = vbi_close( bktr );
598 break;
599 default:
600 return (ENXIO);
601 break;
602 }
603
604 device_unbusy(devclass_get_device(bktr_devclass, unit));
605 return( result );
606 }
607
608
609 /*
610 *
611 */
612 int
613 bktr_read( dev_t dev, struct uio *uio, int ioflag )
614 {
615 bktr_ptr_t bktr;
616 int unit;
617
618 unit = UNIT(minor(dev));
619
620 /* Get the device data */
621 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
622 if (bktr == NULL) {
623 /* the device is no longer valid/functioning */
624 return (ENXIO);
625 }
626
627 switch ( FUNCTION( minor(dev) ) ) {
628 case VIDEO_DEV:
629 return( video_read( bktr, unit, dev, uio ) );
630 case VBI_DEV:
631 return( vbi_read( bktr, uio, ioflag ) );
632 }
633 return( ENXIO );
634 }
635
636
637 /*
638 *
639 */
640 int
641 bktr_write( dev_t dev, struct uio *uio, int ioflag )
642 {
643 return( EINVAL ); /* XXX or ENXIO ? */
644 }
645
646
647 /*
648 *
649 */
650 int
651 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr )
652 {
653 bktr_ptr_t bktr;
654 int unit;
655
656 unit = UNIT(minor(dev));
657
658 /* Get the device data */
659 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
660 if (bktr == NULL) {
661 /* the device is no longer valid/functioning */
662 return (ENXIO);
663 }
664
665 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
666 return( ENOMEM );
667
668 switch ( FUNCTION( minor(dev) ) ) {
669 case VIDEO_DEV:
670 return( video_ioctl( bktr, unit, cmd, arg, pr ) );
671 case TUNER_DEV:
672 return( tuner_ioctl( bktr, unit, cmd, arg, pr ) );
673 }
674
675 return( ENXIO );
676 }
677
678
679 /*
680 *
681 */
682 int
683 bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
684 {
685 int unit;
686 bktr_ptr_t bktr;
687
688 unit = UNIT(minor(dev));
689
690 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
691 return( -1 );
692
693 /* Get the device data */
694 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
695 if (bktr == NULL) {
696 /* the device is no longer valid/functioning */
697 return (ENXIO);
698 }
699
700 if (nprot & PROT_EXEC)
701 return( -1 );
702
703 if (offset < 0)
704 return( -1 );
705
706 if (offset >= bktr->alloc_pages * PAGE_SIZE)
707 return( -1 );
708
709 return( atop(vtophys(bktr->bigbuf) + offset) );
710 }
711
712 int bktr_poll( dev_t dev, int events, struct proc *p)
713 {
714 int unit;
715 bktr_ptr_t bktr;
716 int revents = 0;
717 DECLARE_INTR_MASK(s);
718
719 unit = UNIT(minor(dev));
720
721 /* Get the device data */
722 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
723 if (bktr == NULL) {
724 /* the device is no longer valid/functioning */
725 return (ENXIO);
726 }
727
728 DISABLE_INTR(s);
729
730 if (events & (POLLIN | POLLRDNORM)) {
731
732 switch ( FUNCTION( minor(dev) ) ) {
733 case VBI_DEV:
734 if(bktr->vbisize == 0)
735 selrecord(p, &bktr->vbi_select);
736 else
737 revents |= events & (POLLIN | POLLRDNORM);
738 break;
739 }
740 }
741
742 ENABLE_INTR(s);
743
744 return (revents);
745 }
746
747 #endif /* FreeBSD 4.x specific kernel interface routines */
748
749 /**********************************/
750 /* *** FreeBSD 2.2.x and 3.x *** */
751 /**********************************/
752
753 #if ((__FreeBSD__ == 2) || (__FreeBSD__ == 3))
754
755 static bktr_reg_t brooktree[ NBKTR ];
756
757 static const char* bktr_probe( pcici_t tag, pcidi_t type );
758 static void bktr_attach( pcici_t tag, int unit );
759 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
760
761 static u_long bktr_count;
762
763 static struct pci_device bktr_device = {
764 "bktr",
765 bktr_probe,
766 bktr_attach,
767 &bktr_count
768 };
769
770 DATA_SET (pcidevice_set, bktr_device);
771
772 static d_open_t bktr_open;
773 static d_close_t bktr_close;
774 static d_read_t bktr_read;
775 static d_write_t bktr_write;
776 static d_ioctl_t bktr_ioctl;
777 static d_mmap_t bktr_mmap;
778 static d_poll_t bktr_poll;
779
780 #define CDEV_MAJOR 92
781 static struct cdevsw bktr_cdevsw =
782 {
783 bktr_open, bktr_close, bktr_read, bktr_write,
784 bktr_ioctl, nostop, nullreset, nodevtotty,
785 bktr_poll, bktr_mmap, NULL, "bktr",
786 NULL, -1
787 };
788
789 static int bktr_devsw_installed;
790
791 static void
792 bktr_drvinit( void *unused )
793 {
794 dev_t dev;
795
796 if ( ! bktr_devsw_installed ) {
797 dev = makedev(CDEV_MAJOR, 0);
798 cdevsw_add(&dev,&bktr_cdevsw, NULL);
799 bktr_devsw_installed = 1;
800 }
801 }
802
803 SYSINIT(bktrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bktr_drvinit,NULL)
804
805 /*
806 * the boot time probe routine.
807 */
808 static const char*
809 bktr_probe( pcici_t tag, pcidi_t type )
810 {
811 unsigned int rev = pci_conf_read( tag, PCIR_REVID) & 0x000000ff;
812
813 switch (type) {
814 case BROOKTREE_848_PCI_ID:
815 if (rev == 0x12) return("BrookTree 848A");
816 else return("BrookTree 848");
817 case BROOKTREE_849_PCI_ID:
818 return("BrookTree 849A");
819 case BROOKTREE_878_PCI_ID:
820 return("BrookTree 878");
821 case BROOKTREE_879_PCI_ID:
822 return("BrookTree 879");
823 };
824
825 return ((char *)0);
826 }
827
828 /*
829 * the attach routine.
830 */
831 static void
832 bktr_attach( pcici_t tag, int unit )
833 {
834 bktr_ptr_t bktr;
835 u_long latency;
836 u_long fun;
837 unsigned int rev;
838 unsigned long base;
839 #ifdef BROOKTREE_IRQ
840 u_long old_irq, new_irq;
841 #endif
842
843 bktr = &brooktree[unit];
844
845 if (unit >= NBKTR) {
846 printf("brooktree%d: attach: only %d units configured.\n",
847 unit, NBKTR);
848 printf("brooktree%d: attach: invalid unit number.\n", unit);
849 return;
850 }
851
852 /* Enable Memory Mapping */
853 fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
854 pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 2);
855
856 /* Enable Bus Mastering */
857 fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
858 pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 4);
859
860 bktr->tag = tag;
861
862
863 /*
864 * Map control/status registers
865 */
866 pci_map_mem( tag, PCI_MAP_REG_START, (vm_offset_t *) &base,
867 &bktr->phys_base );
868 #if (__FreeBSD_version >= 300000)
869 bktr->memt = I386_BUS_SPACE_MEM; /* XXX should use proper bus space */
870 bktr->memh = (bus_space_handle_t)base; /* XXX functions here */
871 #endif
872
873 /*
874 * Disable the brooktree device
875 */
876 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
877 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
878
879 #ifdef BROOKTREE_IRQ /* from the configuration file */
880 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
881 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
882 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
883 printf("bktr%d: attach: irq changed from %d to %d\n",
884 unit, (old_irq & 0xff), (new_irq & 0xff));
885 #endif
886
887 /*
888 * setup the interrupt handling routine
889 */
890 pci_map_int(tag, bktr_intr, (void*) bktr, &tty_imask);
891
892
893 /* Update the Device Control Register */
894 /* on Bt878 and Bt879 cards */
895 fun = pci_conf_read(tag, 0x40);
896 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
897
898 #if defined( BKTR_430_FX_MODE )
899 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
900 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
901 #endif
902
903 #if defined( BKTR_SIS_VIA_MODE )
904 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
905 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
906 OPTi chipset motherboards too */
907 #endif
908 pci_conf_write(tag, 0x40, fun);
909
910
911 /* XXX call bt848_i2c dependent attach() routine */
912 #if (NSMBUS > 0)
913 if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
914 printf("bktr%d: i2c_attach: can't attach\n", unit);
915 #endif
916
917
918 /*
919 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
920 * you have more than four, then 16 would probably be a better value.
921 */
922 #ifndef BROOKTREE_DEF_LATENCY_VALUE
923 #define BROOKTREE_DEF_LATENCY_VALUE 10
924 #endif
925 latency = pci_conf_read(tag, PCI_LATENCY_TIMER);
926 latency = (latency >> 8) & 0xff;
927 if ( bootverbose ) {
928 if (latency)
929 printf("brooktree%d: PCI bus latency is", unit);
930 else
931 printf("brooktree%d: PCI bus latency was 0 changing to",
932 unit);
933 }
934 if ( !latency ) {
935 latency = BROOKTREE_DEF_LATENCY_VALUE;
936 pci_conf_write(tag, PCI_LATENCY_TIMER, latency<<8);
937 }
938 if ( bootverbose ) {
939 printf(" %d.\n", (int) latency);
940 }
941
942
943 /* read the pci device id and revision id */
944 fun = pci_conf_read(tag, PCI_ID_REG);
945 rev = pci_conf_read(tag, PCIR_REVID) & 0x000000ff;
946
947 /* call the common attach code */
948 common_bktr_attach( bktr, unit, fun, rev );
949
950 #ifdef DEVFS
951 /* XXX This just throw away the token, which should probably be fixed when
952 DEVFS is finally made really operational. */
953 devfs_add_devswf(&bktr_cdevsw, unit, DV_CHR, 0, 0, 0444, "bktr%d", unit);
954 devfs_add_devswf(&bktr_cdevsw, unit+16, DV_CHR, 0, 0, 0444, "tuner%d", unit);
955 devfs_add_devswf(&bktr_cdevsw, unit+32, DV_CHR, 0, 0, 0444, "vbi%d", unit);
956 #endif /* DEVFS */
957
958 }
959
960
961 /*
962 * Special Memory Allocation
963 */
964 vm_offset_t
965 get_bktr_mem( int unit, unsigned size )
966 {
967 vm_offset_t addr = 0;
968
969 addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff, 1<<24);
970 if (addr == 0)
971 addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff,
972 PAGE_SIZE);
973 if (addr == 0) {
974 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
975 unit, size);
976 }
977
978 return( addr );
979 }
980
981 /*---------------------------------------------------------
982 **
983 ** BrookTree 848 character device driver routines
984 **
985 **---------------------------------------------------------
986 */
987
988
989 #define VIDEO_DEV 0x00
990 #define TUNER_DEV 0x01
991 #define VBI_DEV 0x02
992
993 #define UNIT(x) ((x) & 0x0f)
994 #define FUNCTION(x) ((x >> 4) & 0x0f)
995
996
997 /*
998 *
999 */
1000 int
1001 bktr_open( dev_t dev, int flags, int fmt, struct proc *p )
1002 {
1003 bktr_ptr_t bktr;
1004 int unit;
1005
1006 unit = UNIT( minor(dev) );
1007 if (unit >= NBKTR) /* unit out of range */
1008 return( ENXIO );
1009
1010 bktr = &(brooktree[ unit ]);
1011
1012 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
1013 return( ENXIO );
1014
1015
1016 if (bt848_card != -1) {
1017 if ((bt848_card >> 8 == unit ) &&
1018 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
1019 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
1020 bktr->bt848_card = (bt848_card & 0xff);
1021 probeCard(bktr, FALSE, unit);
1022 }
1023 }
1024 }
1025
1026 if (bt848_tuner != -1) {
1027 if ((bt848_tuner >> 8 == unit ) &&
1028 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
1029 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
1030 bktr->bt848_tuner = (bt848_tuner & 0xff);
1031 probeCard(bktr, FALSE, unit);
1032 }
1033 }
1034 }
1035
1036 if (bt848_reverse_mute != -1) {
1037 if ((bt848_reverse_mute >> 8) == unit ) {
1038 bktr->reverse_mute = bt848_reverse_mute & 0xff;
1039 }
1040 }
1041
1042 if (bt848_slow_msp_audio != -1) {
1043 if ((bt848_slow_msp_audio >> 8) == unit ) {
1044 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
1045 }
1046 }
1047
1048 switch ( FUNCTION( minor(dev) ) ) {
1049 case VIDEO_DEV:
1050 return( video_open( bktr ) );
1051 case TUNER_DEV:
1052 return( tuner_open( bktr ) );
1053 case VBI_DEV:
1054 return( vbi_open( bktr ) );
1055 }
1056 return( ENXIO );
1057 }
1058
1059
1060 /*
1061 *
1062 */
1063 int
1064 bktr_close( dev_t dev, int flags, int fmt, struct proc *p )
1065 {
1066 bktr_ptr_t bktr;
1067 int unit;
1068
1069 unit = UNIT( minor(dev) );
1070 if (unit >= NBKTR) /* unit out of range */
1071 return( ENXIO );
1072
1073 bktr = &(brooktree[ unit ]);
1074
1075 switch ( FUNCTION( minor(dev) ) ) {
1076 case VIDEO_DEV:
1077 return( video_close( bktr ) );
1078 case TUNER_DEV:
1079 return( tuner_close( bktr ) );
1080 case VBI_DEV:
1081 return( vbi_close( bktr ) );
1082 }
1083
1084 return( ENXIO );
1085 }
1086
1087 /*
1088 *
1089 */
1090 int
1091 bktr_read( dev_t dev, struct uio *uio, int ioflag )
1092 {
1093 bktr_ptr_t bktr;
1094 int unit;
1095
1096 unit = UNIT(minor(dev));
1097 if (unit >= NBKTR) /* unit out of range */
1098 return( ENXIO );
1099
1100 bktr = &(brooktree[unit]);
1101
1102 switch ( FUNCTION( minor(dev) ) ) {
1103 case VIDEO_DEV:
1104 return( video_read( bktr, unit, dev, uio ) );
1105 case VBI_DEV:
1106 return( vbi_read( bktr, uio, ioflag ) );
1107 }
1108 return( ENXIO );
1109 }
1110
1111
1112 /*
1113 *
1114 */
1115 int
1116 bktr_write( dev_t dev, struct uio *uio, int ioflag )
1117 {
1118 return( EINVAL ); /* XXX or ENXIO ? */
1119 }
1120
1121 /*
1122 *
1123 */
1124 int
1125 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr )
1126 {
1127 bktr_ptr_t bktr;
1128 int unit;
1129
1130 unit = UNIT(minor(dev));
1131 if (unit >= NBKTR) /* unit out of range */
1132 return( ENXIO );
1133
1134 bktr = &(brooktree[ unit ]);
1135
1136 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
1137 return( ENOMEM );
1138
1139 switch ( FUNCTION( minor(dev) ) ) {
1140 case VIDEO_DEV:
1141 return( video_ioctl( bktr, unit, cmd, arg, pr ) );
1142 case TUNER_DEV:
1143 return( tuner_ioctl( bktr, unit, cmd, arg, pr ) );
1144 }
1145
1146 return( ENXIO );
1147 }
1148
1149 /*
1150 * bktr_mmap.
1151 * Note: 2.2.5/2.2.6/2.2.7/3.0 users must manually
1152 * edit the line below and change "vm_offset_t" to "int"
1153 */
1154 int bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
1155
1156 {
1157 int unit;
1158 bktr_ptr_t bktr;
1159
1160 unit = UNIT(minor(dev));
1161
1162 if (unit >= NBKTR || FUNCTION(minor(dev)) > 0)
1163 return( -1 );
1164
1165 bktr = &(brooktree[ unit ]);
1166
1167 if (nprot & PROT_EXEC)
1168 return( -1 );
1169
1170 if (offset < 0)
1171 return( -1 );
1172
1173 if (offset >= bktr->alloc_pages * PAGE_SIZE)
1174 return( -1 );
1175
1176 return( i386_btop(vtophys(bktr->bigbuf) + offset) );
1177 }
1178
1179 int bktr_poll( dev_t dev, int events, struct proc *p)
1180 {
1181 int unit;
1182 bktr_ptr_t bktr;
1183 int revents = 0;
1184
1185 unit = UNIT(minor(dev));
1186
1187 if (unit >= NBKTR)
1188 return( -1 );
1189
1190 bktr = &(brooktree[ unit ]);
1191
1192 disable_intr();
1193
1194 if (events & (POLLIN | POLLRDNORM)) {
1195
1196 switch ( FUNCTION( minor(dev) ) ) {
1197 case VBI_DEV:
1198 if(bktr->vbisize == 0)
1199 selrecord(p, &bktr->vbi_select);
1200 else
1201 revents |= events & (POLLIN | POLLRDNORM);
1202 break;
1203 }
1204 }
1205
1206 enable_intr();
1207
1208 return (revents);
1209 }
1210
1211
1212 #endif /* FreeBSD 2.2.x and 3.x specific kernel interface routines */
1213
1214
1215 /*****************/
1216 /* *** BSDI *** */
1217 /*****************/
1218
1219 #if defined(__bsdi__)
1220 #endif /* __bsdi__ BSDI specific kernel interface routines */
1221
1222
1223 /*****************************/
1224 /* *** OpenBSD / NetBSD *** */
1225 /*****************************/
1226 #if defined(__NetBSD__) || defined(__OpenBSD__)
1227
1228 #define IPL_VIDEO IPL_BIO /* XXX */
1229
1230 static int bktr_intr(void *arg) { return common_bktr_intr(arg); }
1231
1232 #define bktr_open bktropen
1233 #define bktr_close bktrclose
1234 #define bktr_read bktrread
1235 #define bktr_write bktrwrite
1236 #define bktr_ioctl bktrioctl
1237 #define bktr_mmap bktrmmap
1238
1239 int bktr_open __P((dev_t, int, int, struct proc *));
1240 int bktr_close __P((dev_t, int, int, struct proc *));
1241 int bktr_read __P((dev_t, struct uio *, int));
1242 int bktr_write __P((dev_t, struct uio *, int));
1243 int bktr_ioctl __P((dev_t, ioctl_cmd_t, caddr_t, int, struct proc*));
1244 int bktr_mmap __P((dev_t, int, int));
1245
1246 vm_offset_t vm_page_alloc_contig(vm_offset_t, vm_offset_t,
1247 vm_offset_t, vm_offset_t);
1248
1249 #if defined(__OpenBSD__)
1250 static int bktr_probe __P((struct device *, void *, void *));
1251 #else
1252 static int bktr_probe __P((struct device *, struct cfdata *, void *));
1253 #endif
1254 static void bktr_attach __P((struct device *, struct device *, void *));
1255
1256 struct cfattach bktr_ca = {
1257 sizeof(struct bktr_softc), bktr_probe, bktr_attach
1258 };
1259
1260 #if defined(__NetBSD__)
1261 extern struct cfdriver bktr_cd;
1262 #else
1263 struct cfdriver bktr_cd = {
1264 NULL, "bktr", DV_DULL
1265 };
1266 #endif
1267
1268 int
1269 bktr_probe(parent, match, aux)
1270 struct device *parent;
1271 #if defined(__OpenBSD__)
1272 void *match;
1273 #else
1274 struct cfdata *match;
1275 #endif
1276 void *aux;
1277 {
1278 struct pci_attach_args *pa = aux;
1279
1280 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROOKTREE &&
1281 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT848 ||
1282 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT849 ||
1283 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT878 ||
1284 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT879))
1285 return 1;
1286
1287 return 0;
1288 }
1289
1290
1291 /*
1292 * the attach routine.
1293 */
1294 static void
1295 bktr_attach(struct device *parent, struct device *self, void *aux)
1296 {
1297 bktr_ptr_t bktr;
1298 u_long latency;
1299 u_long fun;
1300 unsigned int rev;
1301
1302 #if defined(__OpenBSD__)
1303 struct pci_attach_args *pa = aux;
1304 pci_chipset_tag_t pc = pa->pa_pc;
1305
1306 pci_intr_handle_t ih;
1307 const char *intrstr;
1308 int retval;
1309 int unit;
1310
1311 bktr = (bktr_ptr_t)self;
1312 unit = bktr->bktr_dev.dv_unit;
1313
1314 bktr->pc = pa->pa_pc;
1315 bktr->tag = pa->pa_tag;
1316 bktr->dmat = pa->pa_dmat;
1317
1318 /*
1319 * map memory
1320 */
1321 bktr->memt = pa->pa_memt;
1322 retval = pci_mem_find(pc, pa->pa_tag, PCI_MAPREG_START,
1323 &bktr->phys_base, &bktr->obmemsz, NULL);
1324 if (!retval)
1325 retval = bus_space_map(pa->pa_memt, bktr->phys_base,
1326 bktr->obmemsz, 0, &bktr->memh);
1327 if (retval) {
1328 printf(": couldn't map memory\n");
1329 return;
1330 }
1331
1332
1333 /*
1334 * map interrupt
1335 */
1336 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1337 pa->pa_intrline, &ih)) {
1338 printf(": couldn't map interrupt\n");
1339 return;
1340 }
1341 intrstr = pci_intr_string(pa->pa_pc, ih);
1342
1343 bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
1344 bktr_intr, bktr, bktr->bktr_dev.dv_xname);
1345 if (bktr->ih == NULL) {
1346 printf(": couldn't establish interrupt");
1347 if (intrstr != NULL)
1348 printf(" at %s", intrstr);
1349 printf("\n");
1350 return;
1351 }
1352
1353 if (intrstr != NULL)
1354 printf(": %s\n", intrstr);
1355 #endif /* __OpenBSD__ */
1356
1357 #if defined(__NetBSD__)
1358 struct pci_attach_args *pa = aux;
1359 pci_intr_handle_t ih;
1360 const char *intrstr;
1361 int retval;
1362 int unit;
1363
1364 bktr = (bktr_ptr_t)self;
1365 unit = bktr->bktr_dev.dv_unit;
1366 bktr->dmat = pa->pa_dmat;
1367
1368 printf("\n");
1369
1370 /*
1371 * map memory
1372 */
1373 retval = pci_mapreg_map(pa, PCI_MAPREG_START,
1374 PCI_MAPREG_TYPE_MEM
1375 | PCI_MAPREG_MEM_TYPE_32BIT, 0,
1376 &bktr->memt, &bktr->memh, NULL,
1377 &bktr->obmemsz);
1378 DPR(("pci_mapreg_map: memt %x, memh %x, size %x\n",
1379 bktr->memt, (u_int)bktr->memh, (u_int)bktr->obmemsz));
1380 if (retval) {
1381 printf("%s: couldn't map memory\n",
1382 bktr->bktr_dev.dv_xname);
1383 return;
1384 }
1385
1386 /*
1387 * Disable the brooktree device
1388 */
1389 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1390 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
1391
1392 /*
1393 * map interrupt
1394 */
1395 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1396 pa->pa_intrline, &ih)) {
1397 printf("%s: couldn't map interrupt\n",
1398 bktr->bktr_dev.dv_xname);
1399 return;
1400 }
1401 intrstr = pci_intr_string(pa->pa_pc, ih);
1402 bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
1403 bktr_intr, bktr);
1404 if (bktr->ih == NULL) {
1405 printf("%s: couldn't establish interrupt",
1406 bktr->bktr_dev.dv_xname);
1407 if (intrstr != NULL)
1408 printf(" at %s", intrstr);
1409 printf("\n");
1410 return;
1411 }
1412 if (intrstr != NULL)
1413 printf("%s: interrupting at %s\n", bktr->bktr_dev.dv_xname,
1414 intrstr);
1415 #endif /* __NetBSD__ */
1416
1417 /*
1418 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
1419 * you have more than four, then 16 would probably be a better value.
1420 */
1421 #ifndef BROOKTREE_DEF_LATENCY_VALUE
1422 #define BROOKTREE_DEF_LATENCY_VALUE 10
1423 #endif
1424 latency = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_LATENCY_TIMER);
1425 latency = (latency >> 8) & 0xff;
1426
1427 if (!latency) {
1428 if (bootverbose) {
1429 printf("%s: PCI bus latency was 0 changing to %d",
1430 bktr->bktr_dev.dv_xname,
1431 BROOKTREE_DEF_LATENCY_VALUE);
1432 }
1433 latency = BROOKTREE_DEF_LATENCY_VALUE;
1434 pci_conf_write(pa->pa_pc, pa->pa_tag,
1435 PCI_LATENCY_TIMER, latency<<8);
1436 }
1437
1438
1439 /* Enabled Bus Master
1440 XXX: check if all old DMA is stopped first (e.g. after warm
1441 boot) */
1442 fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1443 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1444 fun | PCI_COMMAND_MASTER_ENABLE);
1445
1446 /* read the pci id and determine the card type */
1447 fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
1448 rev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG) & 0x000000ff;
1449
1450 common_bktr_attach(bktr, unit, fun, rev);
1451 }
1452
1453
1454 /*
1455 * Special Memory Allocation
1456 */
1457 vm_offset_t
1458 get_bktr_mem(bktr, dmapp, size)
1459 bktr_ptr_t bktr;
1460 bus_dmamap_t *dmapp;
1461 unsigned int size;
1462 {
1463 bus_dma_tag_t dmat = bktr->dmat;
1464 bus_dma_segment_t seg;
1465 bus_size_t align;
1466 int rseg;
1467 caddr_t kva;
1468
1469 /*
1470 * Allocate a DMA area
1471 */
1472 align = 1 << 24;
1473 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
1474 &rseg, BUS_DMA_NOWAIT)) {
1475 align = PAGE_SIZE;
1476 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
1477 &rseg, BUS_DMA_NOWAIT)) {
1478 printf("bktr%d: Unable to dmamem_alloc of %d bytes\n",
1479 bktr->bktr_dev.dv_unit, size);
1480 return 0;
1481 }
1482 }
1483 if (bus_dmamem_map(dmat, &seg, rseg, size,
1484 &kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
1485 printf("bktr%d: Unable to dmamem_map of %d bytes\n",
1486 bktr->bktr_dev.dv_unit, size);
1487 bus_dmamem_free(dmat, &seg, rseg);
1488 return 0;
1489 }
1490 #ifdef __OpenBSD__
1491 bktr->dm_mapsize = size;
1492 #endif
1493 /*
1494 * Create and locd the DMA map for the DMA area
1495 */
1496 if (bus_dmamap_create(dmat, size, 1, size, 0, BUS_DMA_NOWAIT, dmapp)) {
1497 printf("bktr%d: Unable to dmamap_create of %d bytes\n",
1498 bktr->bktr_dev.dv_unit, size);
1499 bus_dmamem_unmap(dmat, kva, size);
1500 bus_dmamem_free(dmat, &seg, rseg);
1501 return 0;
1502 }
1503 if (bus_dmamap_load(dmat, *dmapp, kva, size, NULL, BUS_DMA_NOWAIT)) {
1504 printf("bktr%d: Unable to dmamap_load of %d bytes\n",
1505 bktr->bktr_dev.dv_unit, size);
1506 bus_dmamem_unmap(dmat, kva, size);
1507 bus_dmamem_free(dmat, &seg, rseg);
1508 bus_dmamap_destroy(dmat, *dmapp);
1509 return 0;
1510 }
1511 return (vm_offset_t)kva;
1512 }
1513
1514 void
1515 free_bktr_mem(bktr, dmap, kva)
1516 bktr_ptr_t bktr;
1517 bus_dmamap_t dmap;
1518 vm_offset_t kva;
1519 {
1520 bus_dma_tag_t dmat = bktr->dmat;
1521
1522 #ifdef __NetBSD__
1523 bus_dmamem_unmap(dmat, (caddr_t)kva, dmap->dm_mapsize);
1524 #else
1525 bus_dmamem_unmap(dmat, (caddr_t)kva, bktr->dm_mapsize);
1526 #endif
1527 bus_dmamem_free(dmat, dmap->dm_segs, 1);
1528 bus_dmamap_destroy(dmat, dmap);
1529 }
1530
1531
1532 /*---------------------------------------------------------
1533 **
1534 ** BrookTree 848 character device driver routines
1535 **
1536 **---------------------------------------------------------
1537 */
1538
1539
1540 #define VIDEO_DEV 0x00
1541 #define TUNER_DEV 0x01
1542 #define VBI_DEV 0x02
1543
1544 #define UNIT(x) (minor((x) & 0x0f))
1545 #define FUNCTION(x) (minor((x >> 4) & 0x0f))
1546
1547 /*
1548 *
1549 */
1550 int
1551 bktr_open(dev_t dev, int flags, int fmt, struct proc *p)
1552 {
1553 bktr_ptr_t bktr;
1554 int unit;
1555
1556 unit = UNIT(dev);
1557
1558 /* unit out of range */
1559 if ((unit > bktr_cd.cd_ndevs) || (bktr_cd.cd_devs[unit] == NULL))
1560 return(ENXIO);
1561
1562 bktr = bktr_cd.cd_devs[unit];
1563
1564 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
1565 return(ENXIO);
1566
1567 switch (FUNCTION(dev)) {
1568 case VIDEO_DEV:
1569 return(video_open(bktr));
1570 case TUNER_DEV:
1571 return(tuner_open(bktr));
1572 case VBI_DEV:
1573 return(vbi_open(bktr));
1574 }
1575
1576 return(ENXIO);
1577 }
1578
1579
1580 /*
1581 *
1582 */
1583 int
1584 bktr_close(dev_t dev, int flags, int fmt, struct proc *p)
1585 {
1586 bktr_ptr_t bktr;
1587 int unit;
1588
1589 unit = UNIT(dev);
1590
1591 bktr = bktr_cd.cd_devs[unit];
1592
1593 switch (FUNCTION(dev)) {
1594 case VIDEO_DEV:
1595 return(video_close(bktr));
1596 case TUNER_DEV:
1597 return(tuner_close(bktr));
1598 case VBI_DEV:
1599 return(vbi_close(bktr));
1600 }
1601
1602 return(ENXIO);
1603 }
1604
1605 /*
1606 *
1607 */
1608 int
1609 bktr_read(dev_t dev, struct uio *uio, int ioflag)
1610 {
1611 bktr_ptr_t bktr;
1612 int unit;
1613
1614 unit = UNIT(dev);
1615
1616 bktr = bktr_cd.cd_devs[unit];
1617
1618 switch (FUNCTION(dev)) {
1619 case VIDEO_DEV:
1620 return(video_read(bktr, unit, dev, uio));
1621 case VBI_DEV:
1622 return(vbi_read(bktr, uio, ioflag));
1623 }
1624
1625 return(ENXIO);
1626 }
1627
1628
1629 /*
1630 *
1631 */
1632 int
1633 bktr_write(dev_t dev, struct uio *uio, int ioflag)
1634 {
1635 /* operation not supported */
1636 return(EOPNOTSUPP);
1637 }
1638
1639 /*
1640 *
1641 */
1642 int
1643 bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr)
1644 {
1645 bktr_ptr_t bktr;
1646 int unit;
1647
1648 unit = UNIT(dev);
1649
1650 bktr = bktr_cd.cd_devs[unit];
1651
1652 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
1653 return(ENOMEM);
1654
1655 switch (FUNCTION(dev)) {
1656 case VIDEO_DEV:
1657 return(video_ioctl(bktr, unit, cmd, arg, pr));
1658 case TUNER_DEV:
1659 return(tuner_ioctl(bktr, unit, cmd, arg, pr));
1660 }
1661
1662 return(ENXIO);
1663 }
1664
1665 /*
1666 *
1667 */
1668 int
1669 bktr_mmap(dev_t dev, int offset, int nprot)
1670 {
1671 int unit;
1672 bktr_ptr_t bktr;
1673
1674 unit = UNIT(dev);
1675
1676 if (FUNCTION(dev) > 0) /* only allow mmap on /dev/bktr[n] */
1677 return(-1);
1678
1679 bktr = bktr_cd.cd_devs[unit];
1680
1681 if ((vaddr_t)offset < 0)
1682 return(-1);
1683
1684 if ((vaddr_t)offset >= bktr->alloc_pages * PAGE_SIZE)
1685 return(-1);
1686
1687 #ifdef __NetBSD__
1688 return (bus_dmamem_mmap(bktr->dmat, bktr->dm_mem->dm_segs, 1,
1689 (vaddr_t)offset, nprot, BUS_DMA_WAITOK));
1690 #else
1691 return(i386_btop(vtophys(bktr->bigbuf) + offset));
1692 #endif
1693 }
1694
1695 #endif /* __NetBSD__ || __OpenBSD__ */
1696