1/*	$OpenBSD: rbus_machdep.c,v 1.13 2013/08/07 07:29:19 mpi Exp $ */
2/*	$NetBSD: rbus_machdep.c,v 1.2 1999/10/15 06:43:06 haya Exp $	*/
3
4/*
5 * Copyright (c) 1999
6 *     HAYAKAWA Koichi.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31
32#include <machine/bus.h>
33#include <dev/cardbus/rbus.h>
34
35#include <dev/pci/pcivar.h>
36#include <dev/pci/pcidevs.h>
37
38#include <dev/ofw/openfirm.h>
39
40void macppc_cardbus_init(pci_chipset_tag_t pc, pcitag_t tag);
41
42rbus_tag_t
43rbus_pccbb_parent_mem(struct device *self, struct pci_attach_args *pa)
44{
45	macppc_cardbus_init(pa->pa_pc, pa->pa_tag);
46
47	return (rbus_new_root_share(pa->pa_memt, pa->pa_memex,
48	    0x00000000, 0xffffffff));
49}
50
51rbus_tag_t
52rbus_pccbb_parent_io(struct device *self, struct pci_attach_args *pa)
53{
54	return (rbus_new_root_share(pa->pa_iot, pa->pa_ioex,
55	    0x0000, 0xffff));
56}
57
58/*
59 * Big ugly hack to enable bridge/fix interrupts
60 */
61void
62macppc_cardbus_init(pci_chipset_tag_t pc, pcitag_t tag)
63{
64	u_int x;
65	static int initted = 0;
66
67	if (initted)
68		return;
69	initted = 1;
70
71	/* XXX What about other bridges? */
72
73	x = pci_conf_read(pc, tag, PCI_ID_REG);
74	if (PCI_VENDOR(x) == PCI_VENDOR_TI &&
75	    PCI_PRODUCT(x) == PCI_PRODUCT_TI_PCI1211) {
76		/* For CardBus card. */
77		pci_conf_write(pc, tag, 0x18, 0x10010100);
78
79		/* Route INTA to MFUNC0 */
80		x = pci_conf_read(pc, tag, 0x8c);
81		x |= 0x02;
82		pci_conf_write(pc, tag, 0x8c, x);
83
84		tag = pci_make_tag(pc, 0, 0, 0);
85		x = pci_conf_read(pc, tag, PCI_ID_REG);
86		if (PCI_VENDOR(x) == PCI_VENDOR_MOT &&
87		    PCI_PRODUCT(x) == PCI_PRODUCT_MOT_MPC106) {
88			/* Set subordinate bus number to 1. */
89			x = pci_conf_read(pc, tag, 0x40);
90			x |= 1 << 8;
91			pci_conf_write(pc, tag, 0x40, x);
92		}
93	}
94
95	if (PCI_VENDOR(x) == PCI_VENDOR_TI &&
96	    (PCI_PRODUCT(x) == PCI_PRODUCT_TI_PCI1410 ||
97	    PCI_PRODUCT(x) == PCI_PRODUCT_TI_PCI1510)) {
98		/* dont mess with the bus numbers or latency timer */
99
100		/* Route INTA to MFUNC0 */
101		x = pci_conf_read(pc, tag, 0x8c);
102		x |= 0x02;
103		pci_conf_write(pc, tag, 0x8c, x);
104	}
105}
106
107void
108pccbb_attach_hook(struct device *parent, struct device *self,
109    struct pci_attach_args *pa)
110{
111	pci_chipset_tag_t pc = pa->pa_pc;
112	int node = PCITAG_NODE(pa->pa_tag);
113	int bus, busrange[2];
114
115	if (OF_getprop(OF_parent(node), "bus-range", &busrange,
116	    sizeof(busrange)) != sizeof(busrange))
117		return;
118
119	bus = busrange[0] + 1;
120	while (bus < 256 && pc->busnode[bus])
121		bus++;
122	if (bus == 256)
123		return;
124	pc->busnode[bus] = node;
125}
126