xref: /netbsd-src/sys/arch/hppa/dev/sti_pci_machdep.c (revision 6d3ceb1d619615401b17c9aa3e4bc674a1cb048b)
1*6d3ceb1dSskrll /*	$NetBSD: sti_pci_machdep.c,v 1.1 2014/02/24 07:23:43 skrll Exp $	*/
2*6d3ceb1dSskrll 
3*6d3ceb1dSskrll /*	$OpenBSD: sti_pci_machdep.c,v 1.2 2009/04/10 17:11:27 miod Exp $	*/
4*6d3ceb1dSskrll 
5*6d3ceb1dSskrll /*
6*6d3ceb1dSskrll  * Copyright (c) 2007, 2009 Miodrag Vallat.
7*6d3ceb1dSskrll  *
8*6d3ceb1dSskrll  * Permission to use, copy, modify, and distribute this software for any
9*6d3ceb1dSskrll  * purpose with or without fee is hereby granted, provided that the above
10*6d3ceb1dSskrll  * copyright notice, this permission notice, and the disclaimer below
11*6d3ceb1dSskrll  * appear in all copies.
12*6d3ceb1dSskrll  *
13*6d3ceb1dSskrll  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14*6d3ceb1dSskrll  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*6d3ceb1dSskrll  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16*6d3ceb1dSskrll  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*6d3ceb1dSskrll  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*6d3ceb1dSskrll  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19*6d3ceb1dSskrll  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20*6d3ceb1dSskrll  */
21*6d3ceb1dSskrll 
22*6d3ceb1dSskrll #include <sys/param.h>
23*6d3ceb1dSskrll #include <sys/systm.h>
24*6d3ceb1dSskrll #include <sys/device.h>
25*6d3ceb1dSskrll 
26*6d3ceb1dSskrll #include <machine/iomod.h>
27*6d3ceb1dSskrll #include <machine/autoconf.h>
28*6d3ceb1dSskrll 
29*6d3ceb1dSskrll #include <dev/pci/pcivar.h>
30*6d3ceb1dSskrll 
31*6d3ceb1dSskrll #include <hppa/hppa/machdep.h>
32*6d3ceb1dSskrll 
33*6d3ceb1dSskrll int	sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
34*6d3ceb1dSskrll 
35*6d3ceb1dSskrll int
sti_pci_is_console(struct pci_attach_args * paa,bus_addr_t * bases)36*6d3ceb1dSskrll sti_pci_is_console(struct pci_attach_args *paa, bus_addr_t *bases)
37*6d3ceb1dSskrll {
38*6d3ceb1dSskrll 	hppa_hpa_t consaddr;
39*6d3ceb1dSskrll 	uint32_t cf;
40*6d3ceb1dSskrll 	int pagezero_cookie;
41*6d3ceb1dSskrll 	int bar;
42*6d3ceb1dSskrll 	int rc;
43*6d3ceb1dSskrll 
44*6d3ceb1dSskrll 	KASSERT(paa != NULL);
45*6d3ceb1dSskrll 
46*6d3ceb1dSskrll 	pagezero_cookie = hppa_pagezero_map();
47*6d3ceb1dSskrll 	consaddr = (hppa_hpa_t)PAGE0->mem_cons.pz_hpa;
48*6d3ceb1dSskrll 	hppa_pagezero_unmap(pagezero_cookie);
49*6d3ceb1dSskrll 	/*
50*6d3ceb1dSskrll 	 * PAGE0 console information will point to one of our BARs,
51*6d3ceb1dSskrll 	 * but depending on the particular sti model, this might not
52*6d3ceb1dSskrll 	 * be the BAR mapping the rom (region #0).
53*6d3ceb1dSskrll 	 *
54*6d3ceb1dSskrll 	 * For example, on Visualize FXe, regions #0, #2 and #3 are
55*6d3ceb1dSskrll 	 * mapped by BAR 0x18, while region #1 is mapped by BAR 0x10,
56*6d3ceb1dSskrll 	 * which matches PAGE0 console address.
57*6d3ceb1dSskrll 	 *
58*6d3ceb1dSskrll 	 * Rather than trying to be smart, reread the region->BAR array
59*6d3ceb1dSskrll 	 * again, and compare the BAR mapping region #1 against PAGE0
60*6d3ceb1dSskrll 	 * values, we simply try all the valid BARs; if any of them
61*6d3ceb1dSskrll 	 * matches what PAGE0 says, then we are the console, and it
62*6d3ceb1dSskrll 	 * doesn't matter which BAR matched.
63*6d3ceb1dSskrll 	 */
64*6d3ceb1dSskrll 	for (bar = PCI_MAPREG_START; bar <= PCI_MAPREG_PPB_END; ) {
65*6d3ceb1dSskrll 		bus_addr_t addr;
66*6d3ceb1dSskrll 		bus_size_t size;
67*6d3ceb1dSskrll 
68*6d3ceb1dSskrll 		cf = pci_conf_read(paa->pa_pc, paa->pa_tag, bar);
69*6d3ceb1dSskrll 
70*6d3ceb1dSskrll 		rc = pci_mapreg_info(paa->pa_pc, paa->pa_tag, bar,
71*6d3ceb1dSskrll 		    PCI_MAPREG_TYPE(cf), &addr, &size, NULL);
72*6d3ceb1dSskrll 
73*6d3ceb1dSskrll 		if (PCI_MAPREG_TYPE(cf) == PCI_MAPREG_TYPE_IO) {
74*6d3ceb1dSskrll 			bar += 4;
75*6d3ceb1dSskrll 		} else {
76*6d3ceb1dSskrll 			if (PCI_MAPREG_MEM_TYPE(cf) ==
77*6d3ceb1dSskrll 			    PCI_MAPREG_MEM_TYPE_64BIT)
78*6d3ceb1dSskrll 				bar += 8;
79*6d3ceb1dSskrll 			else
80*6d3ceb1dSskrll 				bar += 4;
81*6d3ceb1dSskrll 		}
82*6d3ceb1dSskrll 
83*6d3ceb1dSskrll 		if (rc == 0 && (hppa_hpa_t)addr == consaddr)
84*6d3ceb1dSskrll 			return 1;
85*6d3ceb1dSskrll 	}
86*6d3ceb1dSskrll 
87*6d3ceb1dSskrll 	return 0;
88*6d3ceb1dSskrll }
89