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