2 * Support for SCC external PCI
4 * (C) Copyright 2004-2007 TOSHIBA CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <linux/kernel.h>
24 #include <linux/threads.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/pci_regs.h>
28 #include <linux/bootmem.h>
33 #include <asm/machdep.h>
34 #include <asm/pci-bridge.h>
35 #include <asm/ppc-pci.h>
39 #include "interrupt.h"
41 #define MAX_PCI_DEVICES 32
42 #define MAX_PCI_FUNCTIONS 8
44 #define iob() __asm__ __volatile__("eieio; sync":::"memory")
46 static inline volatile void __iomem *celleb_epci_get_epci_base(
47 struct pci_controller *hose)
51 * Celleb epci uses cfg_addr as a base address for
52 * epci control registers.
55 return hose->cfg_addr;
58 static inline volatile void __iomem *celleb_epci_get_epci_cfg(
59 struct pci_controller *hose)
63 * Celleb epci uses cfg_data as a base address for
64 * configuration area for epci devices.
67 return hose->cfg_data;
70 #if 0 /* test code for epci dummy read */
71 static void celleb_epci_dummy_read(struct pci_dev *dev)
73 volatile void __iomem *epci_base;
74 struct device_node *node;
75 struct pci_controller *hose;
78 node = (struct device_node *)dev->bus->sysdata;
79 hose = pci_find_hose_for_OF_device(node);
84 epci_base = celleb_epci_get_epci_base(hose);
86 val = in_be32(epci_base + SCC_EPCI_WATRP);
93 static inline void clear_and_disable_master_abort_interrupt(
94 struct pci_controller *hose)
96 volatile void __iomem *epci_base, *reg;
97 epci_base = celleb_epci_get_epci_base(hose);
98 reg = epci_base + PCI_COMMAND;
99 out_be32(reg, in_be32(reg) | (PCI_STATUS_REC_MASTER_ABORT << 16));
102 static int celleb_epci_check_abort(struct pci_controller *hose,
103 volatile void __iomem *addr)
105 volatile void __iomem *reg, *epci_base;
109 epci_base = celleb_epci_get_epci_base(hose);
111 reg = epci_base + PCI_COMMAND;
114 if (val & (PCI_STATUS_REC_MASTER_ABORT << 16)) {
116 (val & 0xffff) | (PCI_STATUS_REC_MASTER_ABORT << 16));
118 /* clear PCI Controller error, FRE, PMFE */
119 reg = epci_base + SCC_EPCI_STATUS;
120 out_be32(reg, SCC_EPCI_INT_PAI);
122 reg = epci_base + SCC_EPCI_VCSR;
123 val = in_be32(reg) & 0xffff;
124 val |= SCC_EPCI_VCSR_FRE;
127 reg = epci_base + SCC_EPCI_VISTAT;
128 out_be32(reg, SCC_EPCI_VISTAT_PMFE);
129 return PCIBIOS_DEVICE_NOT_FOUND;
132 return PCIBIOS_SUCCESSFUL;
135 static volatile void __iomem *celleb_epci_make_config_addr(
137 struct pci_controller *hose,
138 unsigned int devfn, int where)
140 volatile void __iomem *addr;
142 if (bus != hose->bus)
143 addr = celleb_epci_get_epci_cfg(hose) +
144 (((bus->number & 0xff) << 16)
145 | ((devfn & 0xff) << 8)
149 addr = celleb_epci_get_epci_cfg(hose) +
150 (((devfn & 0xff) << 8) | (where & 0xff));
152 pr_debug("EPCI: config_addr = 0x%p\n", addr);
157 static int celleb_epci_read_config(struct pci_bus *bus,
158 unsigned int devfn, int where, int size, u32 * val)
160 volatile void __iomem *epci_base, *addr;
161 struct device_node *node;
162 struct pci_controller *hose;
164 /* allignment check */
165 BUG_ON(where % size);
167 node = (struct device_node *)bus->sysdata;
168 hose = pci_find_hose_for_OF_device(node);
170 if (!celleb_epci_get_epci_cfg(hose))
171 return PCIBIOS_DEVICE_NOT_FOUND;
173 if (bus->number == hose->first_busno && devfn == 0) {
174 /* EPCI controller self */
176 epci_base = celleb_epci_get_epci_base(hose);
177 addr = epci_base + where;
184 *val = in_be16(addr);
187 *val = in_be32(addr);
190 return PCIBIOS_DEVICE_NOT_FOUND;
195 clear_and_disable_master_abort_interrupt(hose);
196 addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
203 *val = in_le16(addr);
206 *val = in_le32(addr);
209 return PCIBIOS_DEVICE_NOT_FOUND;
214 "addr=0x%p, devfn=0x%x, where=0x%x, size=0x%x, val=0x%x\n",
215 addr, devfn, where, size, *val);
217 return celleb_epci_check_abort(hose, NULL);
220 static int celleb_epci_write_config(struct pci_bus *bus,
221 unsigned int devfn, int where, int size, u32 val)
223 volatile void __iomem *epci_base, *addr;
224 struct device_node *node;
225 struct pci_controller *hose;
227 /* allignment check */
228 BUG_ON(where % size);
230 node = (struct device_node *)bus->sysdata;
231 hose = pci_find_hose_for_OF_device(node);
234 if (!celleb_epci_get_epci_cfg(hose))
235 return PCIBIOS_DEVICE_NOT_FOUND;
237 if (bus->number == hose->first_busno && devfn == 0) {
238 /* EPCI controller self */
240 epci_base = celleb_epci_get_epci_base(hose);
241 addr = epci_base + where;
254 return PCIBIOS_DEVICE_NOT_FOUND;
259 clear_and_disable_master_abort_interrupt(hose);
260 addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
273 return PCIBIOS_DEVICE_NOT_FOUND;
277 return celleb_epci_check_abort(hose, addr);
280 struct pci_ops celleb_epci_ops = {
281 celleb_epci_read_config,
282 celleb_epci_write_config,
285 /* to be moved in FW */
286 static int __devinit celleb_epci_init(struct pci_controller *hose)
289 volatile void __iomem *reg, *epci_base;
292 epci_base = celleb_epci_get_epci_base(hose);
294 /* PCI core reset(Internal bus and PCI clock) */
295 reg = epci_base + SCC_EPCI_CKCTRL;
297 if (val == 0x00030101)
300 val &= ~(SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
303 /* set PCI core clock */
305 val |= (SCC_EPCI_CKCTRL_OCLKEN | SCC_EPCI_CKCTRL_LCLKEN);
308 /* release PCI core reset (internal bus) */
310 val |= SCC_EPCI_CKCTRL_CRST0;
313 /* set PCI clock select */
314 reg = epci_base + SCC_EPCI_CLKRST;
316 val &= ~SCC_EPCI_CLKRST_CKS_MASK;
317 val |= SCC_EPCI_CLKRST_CKS_2;
321 reg = epci_base + SCC_EPCI_ABTSET;
322 out_be32(reg, 0x0f1f001f); /* temporary value */
325 reg = epci_base + SCC_EPCI_CLKRST;
327 val |= SCC_EPCI_CLKRST_BC;
330 /* PCI clock enable */
332 val |= SCC_EPCI_CLKRST_PCKEN;
335 /* release PCI core reset (all) */
336 reg = epci_base + SCC_EPCI_CKCTRL;
338 val |= (SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
341 /* set base translation registers. (already set by Beat) */
343 /* set base address masks. (already set by Beat) */
346 /* release interrupt masks and clear all interrupts */
347 reg = epci_base + SCC_EPCI_INTSET;
348 out_be32(reg, 0x013f011f); /* all interrupts enable */
349 reg = epci_base + SCC_EPCI_VIENAB;
350 val = SCC_EPCI_VIENAB_PMPEE | SCC_EPCI_VIENAB_PMFEE;
352 reg = epci_base + SCC_EPCI_STATUS;
353 out_be32(reg, 0xffffffff);
354 reg = epci_base + SCC_EPCI_VISTAT;
355 out_be32(reg, 0xffffffff);
357 /* disable PCI->IB address translation */
358 reg = epci_base + SCC_EPCI_VCSR;
360 val &= ~(SCC_EPCI_VCSR_DR | SCC_EPCI_VCSR_AT);
363 /* set base addresses. (no need to set?) */
365 /* memory space, bus master enable */
366 reg = epci_base + PCI_COMMAND;
367 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
370 /* endian mode setup */
371 reg = epci_base + SCC_EPCI_ECMODE;
375 /* set control option */
376 reg = epci_base + SCC_EPCI_CNTOPT;
378 val |= SCC_EPCI_CNTOPT_O2PMB;
381 /* XXX: temporay: set registers for address conversion setup */
382 reg = epci_base + SCC_EPCI_CNF10_REG;
383 out_be32(reg, 0x80000008);
384 reg = epci_base + SCC_EPCI_CNF14_REG;
385 out_be32(reg, 0x40000008);
387 reg = epci_base + SCC_EPCI_BAM0;
388 out_be32(reg, 0x80000000);
389 reg = epci_base + SCC_EPCI_BAM1;
390 out_be32(reg, 0xe0000000);
392 reg = epci_base + SCC_EPCI_PVBAT;
393 out_be32(reg, 0x80000000);
396 /* release external PCI reset */
397 reg = epci_base + SCC_EPCI_CLKRST;
399 val |= SCC_EPCI_CLKRST_PCIRST;
406 int __devinit celleb_setup_epci(struct device_node *node,
407 struct pci_controller *hose)
411 pr_debug("PCI: celleb_setup_epci()\n");
415 * Celleb epci uses cfg_addr and cfg_data member of
416 * pci_controller structure in irregular way.
418 * cfg_addr is used to map for control registers of
421 * cfg_data is used for configuration area of devices
422 * on Celleb epci buses.
425 if (of_address_to_resource(node, 0, &r))
427 hose->cfg_addr = ioremap(r.start, (r.end - r.start + 1));
430 pr_debug("EPCI: cfg_addr map 0x%016lx->0x%016lx + 0x%016lx\n",
431 r.start, (unsigned long)hose->cfg_addr,
432 (r.end - r.start + 1));
434 if (of_address_to_resource(node, 2, &r))
436 hose->cfg_data = ioremap(r.start, (r.end - r.start + 1));
439 pr_debug("EPCI: cfg_data map 0x%016lx->0x%016lx + 0x%016lx\n",
440 r.start, (unsigned long)hose->cfg_data,
441 (r.end - r.start + 1));
443 celleb_epci_init(hose);