blob: 3a1612253c600b33d2aee10daf4f83d7b360121f [file] [log] [blame]
Paul Mackerrasdaec9622005-10-10 22:25:26 +10001/*
2 * Support for indirect PCI bridges.
3 *
4 * Copyright (C) 1998 Gabriel Paubert.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/pci.h>
14#include <linux/delay.h>
15#include <linux/string.h>
16#include <linux/init.h>
17
18#include <asm/io.h>
19#include <asm/prom.h>
20#include <asm/pci-bridge.h>
21#include <asm/machdep.h>
22
23#ifdef CONFIG_PPC_INDIRECT_PCI_BE
24#define PCI_CFG_OUT out_be32
25#else
26#define PCI_CFG_OUT out_le32
27#endif
28
29static int
30indirect_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
31 int len, u32 *val)
32{
33 struct pci_controller *hose = bus->sysdata;
34 volatile void __iomem *cfg_data;
35 u8 cfg_type = 0;
Kumar Galaab0f9ad2007-06-25 15:19:48 -050036 u32 bus_no, reg;
Paul Mackerrasdaec9622005-10-10 22:25:26 +100037
38 if (ppc_md.pci_exclude_device)
Kumar Gala7d52c7b2007-06-22 00:23:57 -050039 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
Paul Mackerrasdaec9622005-10-10 22:25:26 +100040 return PCIBIOS_DEVICE_NOT_FOUND;
41
Kumar Galaab0f9ad2007-06-25 15:19:48 -050042 if (hose->indirect_type & PPC_INDIRECT_TYPE_SET_CFG_TYPE)
Paul Mackerrasdaec9622005-10-10 22:25:26 +100043 if (bus->number != hose->first_busno)
44 cfg_type = 1;
45
Kumar Gala5ab65ec2007-06-25 13:09:42 -050046 bus_no = (bus->number == hose->first_busno) ?
Kumar Gala0a3786c2007-06-25 13:32:48 -050047 hose->self_busno : bus->number;
Kumar Gala5ab65ec2007-06-25 13:09:42 -050048
Kumar Galaab0f9ad2007-06-25 15:19:48 -050049 if (hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG)
50 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
51 else
52 reg = offset & 0xfc;
53
Kumar Gala7d52c7b2007-06-22 00:23:57 -050054 PCI_CFG_OUT(hose->cfg_addr,
Kumar Gala5ab65ec2007-06-25 13:09:42 -050055 (0x80000000 | (bus_no << 16)
Kumar Galaab0f9ad2007-06-25 15:19:48 -050056 | (devfn << 8) | reg | cfg_type));
Paul Mackerrasdaec9622005-10-10 22:25:26 +100057
58 /*
59 * Note: the caller has already checked that offset is
60 * suitably aligned and that len is 1, 2 or 4.
61 */
62 cfg_data = hose->cfg_data + (offset & 3);
63 switch (len) {
64 case 1:
65 *val = in_8(cfg_data);
66 break;
67 case 2:
68 *val = in_le16(cfg_data);
69 break;
70 default:
71 *val = in_le32(cfg_data);
72 break;
73 }
74 return PCIBIOS_SUCCESSFUL;
75}
76
77static int
78indirect_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
79 int len, u32 val)
80{
81 struct pci_controller *hose = bus->sysdata;
82 volatile void __iomem *cfg_data;
83 u8 cfg_type = 0;
Kumar Galaab0f9ad2007-06-25 15:19:48 -050084 u32 bus_no, reg;
Paul Mackerrasdaec9622005-10-10 22:25:26 +100085
86 if (ppc_md.pci_exclude_device)
Kumar Gala7d52c7b2007-06-22 00:23:57 -050087 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
Paul Mackerrasdaec9622005-10-10 22:25:26 +100088 return PCIBIOS_DEVICE_NOT_FOUND;
89
Kumar Galaab0f9ad2007-06-25 15:19:48 -050090 if (hose->indirect_type & PPC_INDIRECT_TYPE_SET_CFG_TYPE)
Paul Mackerrasdaec9622005-10-10 22:25:26 +100091 if (bus->number != hose->first_busno)
92 cfg_type = 1;
93
Kumar Gala5ab65ec2007-06-25 13:09:42 -050094 bus_no = (bus->number == hose->first_busno) ?
Kumar Gala0a3786c2007-06-25 13:32:48 -050095 hose->self_busno : bus->number;
Kumar Gala5ab65ec2007-06-25 13:09:42 -050096
Kumar Galaab0f9ad2007-06-25 15:19:48 -050097 if (hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG)
98 reg = ((offset & 0xf00) << 16) | (offset & 0xfc);
99 else
100 reg = offset & 0xfc;
101
Kumar Gala7d52c7b2007-06-22 00:23:57 -0500102 PCI_CFG_OUT(hose->cfg_addr,
Kumar Gala5ab65ec2007-06-25 13:09:42 -0500103 (0x80000000 | (bus_no << 16)
Kumar Galaab0f9ad2007-06-25 15:19:48 -0500104 | (devfn << 8) | reg | cfg_type));
Paul Mackerrasdaec9622005-10-10 22:25:26 +1000105
106 /*
107 * Note: the caller has already checked that offset is
108 * suitably aligned and that len is 1, 2 or 4.
109 */
110 cfg_data = hose->cfg_data + (offset & 3);
111 switch (len) {
112 case 1:
113 out_8(cfg_data, val);
114 break;
115 case 2:
116 out_le16(cfg_data, val);
117 break;
118 default:
119 out_le32(cfg_data, val);
120 break;
121 }
122 return PCIBIOS_SUCCESSFUL;
123}
124
125static struct pci_ops indirect_pci_ops =
126{
127 indirect_read_config,
128 indirect_write_config
129};
130
131void __init
132setup_indirect_pci_nomap(struct pci_controller* hose, void __iomem * cfg_addr,
133 void __iomem * cfg_data)
134{
135 hose->cfg_addr = cfg_addr;
136 hose->cfg_data = cfg_data;
137 hose->ops = &indirect_pci_ops;
138}
139
140void __init
141setup_indirect_pci(struct pci_controller* hose, u32 cfg_addr, u32 cfg_data)
142{
143 unsigned long base = cfg_addr & PAGE_MASK;
144 void __iomem *mbase, *addr, *data;
145
146 mbase = ioremap(base, PAGE_SIZE);
147 addr = mbase + (cfg_addr & ~PAGE_MASK);
148 if ((cfg_data & PAGE_MASK) != base)
149 mbase = ioremap(cfg_data & PAGE_MASK, PAGE_SIZE);
150 data = mbase + (cfg_data & ~PAGE_MASK);
151 setup_indirect_pci_nomap(hose, addr, data);
152}