xref: /netbsd-src/sys/arch/hpcmips/dev/mq200_pci.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: mq200_pci.c,v 1.5 2012/10/27 17:17:52 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 TAKEMURA Shin
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mq200_pci.c,v 1.5 2012/10/27 17:17:52 chs Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcidevs.h>
44 #include <dev/pci/pciio.h>
45 
46 #include <hpcmips/dev/mq200var.h>
47 
48 struct mq200_pci_softc {
49 	struct mq200_softc	sc_mq200;
50 	pci_chipset_tag_t sc_pc;
51 	pcitag_t sc_pcitag;
52 };
53 
54 int	mq200_pci_match(device_t, cfdata_t, void *);
55 void	mq200_pci_attach(device_t, device_t, void *);
56 
57 CFATTACH_DECL_NEW(mqvideo_pci, sizeof(struct mq200_pci_softc),
58     mq200_pci_match, mq200_pci_attach, NULL, NULL);
59 
60 int
61 mq200_pci_match(device_t parent, cfdata_t match, void *aux)
62 {
63 	struct pci_attach_args *pa = aux;
64 
65 	/* check vendor id and product id */
66 	if (pa->pa_id !=
67 	    PCI_ID_CODE(PCI_VENDOR_MEDIAQ, PCI_PRODUCT_MEDIAQ_MQ200))
68 		return (0);
69 
70 	return (1);
71 }
72 
73 void
74 mq200_pci_attach(device_t parent, device_t self, void *aux)
75 {
76 	struct mq200_pci_softc *psc = device_private(self);
77 	struct mq200_softc *sc = &psc->sc_mq200;
78 	struct pci_attach_args *pa = aux;
79 	int res;
80 
81 	sc->sc_dev = self;
82 	psc->sc_pc = pa->pa_pc;
83 	psc->sc_pcitag = pa->pa_tag;
84 
85 	/* check whether it is disabled by firmware */
86 	if (!(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) &
87 	    PCI_COMMAND_MEM_ENABLE)) {
88 		printf("%s: disabled\n", device_xname(sc->sc_dev));
89 		return;
90 	}
91 
92 	/* Base Address Register 0: base address of control registers */
93 	res = pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM,
94 	    0, &sc->sc_iot, &sc->sc_ioh, NULL, NULL);
95 	if (res != 0) {
96 		printf("%s: can't map registers\n", device_xname(sc->sc_dev));
97 		return;
98 	}
99 
100 	/* Base Address Register 1: base address of frame buffer */
101 	res = pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, PCI_MAPREG_START+4,
102 	    PCI_MAPREG_TYPE_MEM, &sc->sc_baseaddr, NULL, NULL);
103 	if (res != 0) {
104 		printf("%s: can't map frame buffer\n", device_xname(sc->sc_dev));
105 		return;
106 	}
107 
108 	mq200_attach(sc);
109 }
110