1274201Sdim/*	$NetBSD$	*/
2274201Sdim
3274201Sdim/*	$OpenBSD: sti_pci_machdep.c,v 1.2 2009/04/10 17:11:27 miod Exp $	*/
4274201Sdim
5274201Sdim/*
6274201Sdim * Copyright (c) 2007, 2009 Miodrag Vallat.
7274201Sdim *
8274201Sdim * Permission to use, copy, modify, and distribute this software for any
9274201Sdim * purpose with or without fee is hereby granted, provided that the above
10274201Sdim * copyright notice, this permission notice, and the disclaimer below
11274201Sdim * appear in all copies.
12274201Sdim *
13274201Sdim * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14276789Sdim * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15276789Sdim * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16274201Sdim * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17276789Sdim * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18276789Sdim * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19274201Sdim * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20274201Sdim */
21274201Sdim
22276789Sdim#include <sys/param.h>
23276789Sdim#include <sys/systm.h>
24276789Sdim#include <sys/device.h>
25276789Sdim
26276789Sdim#include <machine/iomod.h>
27276789Sdim#include <machine/autoconf.h>
28276789Sdim
29276789Sdim#include <dev/pci/pcivar.h>
30276789Sdim
31276789Sdim#include <hp700/hp700/machdep.h>
32276789Sdim
33276789Sdimint	sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
34276789Sdim
35276789Sdimint
36276789Sdimsti_pci_is_console(struct pci_attach_args *paa, bus_addr_t *bases)
37276789Sdim{
38288943Sdim	hppa_hpa_t consaddr;
39288943Sdim	uint32_t cf;
40276789Sdim	int pagezero_cookie;
41276789Sdim	int bar;
42276789Sdim	int rc;
43276789Sdim
44276789Sdim	KASSERT(paa != NULL);
45276789Sdim
46276789Sdim	pagezero_cookie = hp700_pagezero_map();
47276789Sdim	consaddr = (hppa_hpa_t)PAGE0->mem_cons.pz_hpa;
48280031Sdim	hp700_pagezero_unmap(pagezero_cookie);
49276789Sdim	/*
50276789Sdim	 * PAGE0 console information will point to one of our BARs,
51276789Sdim	 * but depending on the particular sti model, this might not
52276789Sdim	 * be the BAR mapping the rom (region #0).
53276789Sdim	 *
54276789Sdim	 * For example, on Visualize FXe, regions #0, #2 and #3 are
55276789Sdim	 * mapped by BAR 0x18, while region #1 is mapped by BAR 0x10,
56276789Sdim	 * which matches PAGE0 console address.
57276789Sdim	 *
58276789Sdim	 * Rather than trying to be smart, reread the region->BAR array
59276789Sdim	 * again, and compare the BAR mapping region #1 against PAGE0
60276789Sdim	 * values, we simply try all the valid BARs; if any of them
61276789Sdim	 * matches what PAGE0 says, then we are the console, and it
62276789Sdim	 * doesn't matter which BAR matched.
63288943Sdim	 */
64276789Sdim	for (bar = PCI_MAPREG_START; bar <= PCI_MAPREG_PPB_END; ) {
65276789Sdim		bus_addr_t addr;
66276789Sdim		bus_size_t size;
67276789Sdim
68288943Sdim		cf = pci_conf_read(paa->pa_pc, paa->pa_tag, bar);
69288943Sdim
70288943Sdim		rc = pci_mapreg_info(paa->pa_pc, paa->pa_tag, bar,
71276789Sdim		    PCI_MAPREG_TYPE(cf), &addr, &size, NULL);
72274201Sdim
73276789Sdim		if (PCI_MAPREG_TYPE(cf) == PCI_MAPREG_TYPE_IO) {
74274201Sdim			bar += 4;
75274201Sdim		} else {
76274201Sdim			if (PCI_MAPREG_MEM_TYPE(cf) ==
77			    PCI_MAPREG_MEM_TYPE_64BIT)
78				bar += 8;
79			else
80				bar += 4;
81		}
82
83		if (rc == 0 && (hppa_hpa_t)addr == consaddr)
84			return 1;
85	}
86
87	return 0;
88}
89