1 From 2a7fa2dbbf68650644f807a50cc2d84ca30835c1 Mon Sep 17 00:00:00 2001
2 From: Maxime Bizon <mbizon@freebox.fr>
3 Date: Sun, 21 Sep 2008 04:47:13 +0200
4 Subject: [PATCH] [MIPS] BCM63XX: Add PCI support.
6 Signed-off-by: Maxime Bizon <mbizon@freebox.fr>
8 arch/mips/bcm63xx/Kconfig | 2 +
9 arch/mips/bcm63xx/setup.c | 2 +
10 arch/mips/pci/Makefile | 2 +
11 arch/mips/pci/fixup-bcm63xx.c | 21 +++
12 arch/mips/pci/ops-bcm63xx.c | 179 +++++++++++++++++++++++
13 arch/mips/pci/pci-bcm63xx.c | 178 ++++++++++++++++++++++
14 arch/mips/pci/pci-bcm63xx.h | 27 ++++
15 include/asm-mips/mach-bcm63xx/bcm63xx_dev_pci.h | 6 +
16 8 files changed, 417 insertions(+), 0 deletions(-)
17 create mode 100644 arch/mips/pci/fixup-bcm63xx.c
18 create mode 100644 arch/mips/pci/ops-bcm63xx.c
19 create mode 100644 arch/mips/pci/pci-bcm63xx.c
20 create mode 100644 arch/mips/pci/pci-bcm63xx.h
21 create mode 100644 include/asm-mips/mach-bcm63xx/bcm63xx_dev_pci.h
23 diff --git a/arch/mips/bcm63xx/Kconfig b/arch/mips/bcm63xx/Kconfig
24 index e6d2699..7ca370a 100644
25 --- a/arch/mips/bcm63xx/Kconfig
26 +++ b/arch/mips/bcm63xx/Kconfig
27 @@ -3,7 +3,9 @@ menu "CPU support"
29 config BCM63XX_CPU_6348
30 bool "support 6348 CPU"
33 config BCM63XX_CPU_6358
34 bool "support 6358 CPU"
37 diff --git a/arch/mips/bcm63xx/setup.c b/arch/mips/bcm63xx/setup.c
38 index 55c51a9..4d8b127 100644
39 --- a/arch/mips/bcm63xx/setup.c
40 +++ b/arch/mips/bcm63xx/setup.c
41 @@ -105,4 +105,6 @@ void __init plat_mem_setup(void)
42 pm_power_off = bcm63xx_machine_halt;
45 + ioport_resource.start = 0;
46 + ioport_resource.end = ~0;
48 diff --git a/arch/mips/pci/Makefile b/arch/mips/pci/Makefile
49 index c8c32f4..0b688a8 100644
50 --- a/arch/mips/pci/Makefile
51 +++ b/arch/mips/pci/Makefile
52 @@ -16,6 +16,8 @@ obj-$(CONFIG_PCI_VR41XX) += ops-vr41xx.o pci-vr41xx.o
53 obj-$(CONFIG_MARKEINS) += ops-emma2rh.o pci-emma2rh.o fixup-emma2rh.o
54 obj-$(CONFIG_PCI_TX4927) += ops-tx4927.o
55 obj-$(CONFIG_BCM47XX) += pci-bcm47xx.o
56 +obj-$(CONFIG_BCM63XX) += pci-bcm63xx.o fixup-bcm63xx.o \
60 # These are still pretty much in the old state, watch, go blind.
61 diff --git a/arch/mips/pci/fixup-bcm63xx.c b/arch/mips/pci/fixup-bcm63xx.c
63 index 0000000..3408630
65 +++ b/arch/mips/pci/fixup-bcm63xx.c
68 + * This file is subject to the terms and conditions of the GNU General Public
69 + * License. See the file "COPYING" in the main directory of this archive
72 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
75 +#include <linux/types.h>
76 +#include <linux/pci.h>
77 +#include <bcm63xx_cpu.h>
79 +int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
81 + return bcm63xx_get_irq_number(IRQ_PCI);
84 +int pcibios_plat_dev_init(struct pci_dev *dev)
88 diff --git a/arch/mips/pci/ops-bcm63xx.c b/arch/mips/pci/ops-bcm63xx.c
90 index 0000000..f8dce9d
92 +++ b/arch/mips/pci/ops-bcm63xx.c
95 + * This file is subject to the terms and conditions of the GNU General Public
96 + * License. See the file "COPYING" in the main directory of this archive
99 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
102 +#include <linux/types.h>
103 +#include <linux/pci.h>
104 +#include <linux/kernel.h>
105 +#include <linux/init.h>
106 +#include <linux/delay.h>
107 +#include <linux/io.h>
109 +#include "pci-bcm63xx.h"
112 + * swizzle 32bits data to return only the needed part
114 +static int postprocess_read(u32 data, int where, unsigned int size)
121 + ret = (data >> ((where & 3) << 3)) & 0xff;
124 + ret = (data >> ((where & 3) << 3)) & 0xffff;
133 +static int preprocess_write(u32 orig_data, u32 val, int where,
141 + ret = (orig_data & ~(0xff << ((where & 3) << 3))) |
142 + (val << ((where & 3) << 3));
145 + ret = (orig_data & ~(0xffff << ((where & 3) << 3))) |
146 + (val << ((where & 3) << 3));
156 + * setup hardware for a configuration cycle with given parameters
158 +static int bcm63xx_setup_cfg_access(int type, unsigned int busn,
159 + unsigned int devfn, int where)
161 + unsigned int slot, func, reg;
164 + slot = PCI_SLOT(devfn);
165 + func = PCI_FUNC(devfn);
169 + if (slot > (MPI_L2PCFG_DEVNUM_MASK >> MPI_L2PCFG_DEVNUM_SHIFT))
172 + if (func > (MPI_L2PCFG_FUNC_MASK >> MPI_L2PCFG_FUNC_SHIFT))
175 + if (reg > (MPI_L2PCFG_REG_MASK >> MPI_L2PCFG_REG_SHIFT))
178 + /* ok, setup config access */
179 + val = (reg << MPI_L2PCFG_REG_SHIFT);
180 + val |= (func << MPI_L2PCFG_FUNC_SHIFT);
181 + val |= (slot << MPI_L2PCFG_DEVNUM_SHIFT);
182 + val |= MPI_L2PCFG_CFG_USEREG_MASK;
183 + val |= MPI_L2PCFG_CFG_SEL_MASK;
184 + /* type 0 cycle for local bus, type 1 cycle for anything else */
186 + /* FIXME: how to specify bus ??? */
187 + val |= (1 << MPI_L2PCFG_CFG_TYPE_SHIFT);
189 + bcm_mpi_writel(val, MPI_L2PCFG_REG);
194 +static int bcm63xx_do_cfg_read(int type, unsigned int busn,
195 + unsigned int devfn, int where, int size,
200 + /* two phase cycle, first we write address, then read data at
201 + * another location, caller already has a spinlock so no need
202 + * to add one here */
203 + if (bcm63xx_setup_cfg_access(type, busn, devfn, where))
204 + return PCIBIOS_DEVICE_NOT_FOUND;
206 + data = le32_to_cpu(__raw_readl(pci_iospace_start));
207 + /* restore IO space normal behaviour */
208 + bcm_mpi_writel(0, MPI_L2PCFG_REG);
210 + *val = postprocess_read(data, where, size);
212 + return PCIBIOS_SUCCESSFUL;
215 +static int bcm63xx_do_cfg_write(int type, unsigned int busn,
216 + unsigned int devfn, int where, int size,
221 + /* two phase cycle, first we write address, then write data to
222 + * another location, caller already has a spinlock so no need
223 + * to add one here */
224 + if (bcm63xx_setup_cfg_access(type, busn, devfn, where))
225 + return PCIBIOS_DEVICE_NOT_FOUND;
228 + data = le32_to_cpu(__raw_readl(pci_iospace_start));
229 + data = preprocess_write(data, val, where, size);
231 + __raw_writel(cpu_to_le32(data), pci_iospace_start);
233 + /* no way to know the access is done, we have to wait */
235 + /* restore IO space normal behaviour */
236 + bcm_mpi_writel(0, MPI_L2PCFG_REG);
238 + return PCIBIOS_SUCCESSFUL;
241 +static int bcm63xx_pci_read(struct pci_bus *bus, unsigned int devfn,
242 + int where, int size, u32 *val)
246 + type = bus->parent ? 1 : 0;
248 + if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
249 + return PCIBIOS_DEVICE_NOT_FOUND;
251 + return bcm63xx_do_cfg_read(type, bus->number, devfn,
255 +static int bcm63xx_pci_write(struct pci_bus *bus, unsigned int devfn,
256 + int where, int size, u32 val)
260 + type = bus->parent ? 1 : 0;
262 + if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
263 + return PCIBIOS_DEVICE_NOT_FOUND;
265 + return bcm63xx_do_cfg_write(type, bus->number, devfn,
269 +struct pci_ops bcm63xx_pci_ops = {
270 + .read = bcm63xx_pci_read,
271 + .write = bcm63xx_pci_write
273 diff --git a/arch/mips/pci/pci-bcm63xx.c b/arch/mips/pci/pci-bcm63xx.c
275 index 0000000..52bac8e
277 +++ b/arch/mips/pci/pci-bcm63xx.c
280 + * This file is subject to the terms and conditions of the GNU General Public
281 + * License. See the file "COPYING" in the main directory of this archive
282 + * for more details.
284 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
287 +#include <linux/types.h>
288 +#include <linux/pci.h>
289 +#include <linux/kernel.h>
290 +#include <linux/init.h>
291 +#include <asm/bootinfo.h>
293 +#include "pci-bcm63xx.h"
295 +/* allow PCI to be disabled at runtime depending on board nvram
297 +int bcm63xx_pci_enabled = 0;
299 +static struct resource bcm_pci_mem_resource = {
300 + .name = "bcm63xx PCI memory space",
301 + .start = BCM_PCI_MEM_BASE_PA,
302 + .end = BCM_PCI_MEM_END_PA,
303 + .flags = IORESOURCE_MEM
306 +static struct resource bcm_pci_io_resource = {
307 + .name = "bcm63xx PCI IO space",
308 + .start = BCM_PCI_IO_BASE_PA,
309 + .end = BCM_PCI_IO_END_PA,
310 + .flags = IORESOURCE_IO
313 +struct pci_controller bcm63xx_controller = {
314 + .pci_ops = &bcm63xx_pci_ops,
315 + .io_resource = &bcm_pci_io_resource,
316 + .mem_resource = &bcm_pci_mem_resource,
319 +static u32 bcm63xx_int_cfg_readl(u32 reg)
323 + tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
324 + tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
325 + bcm_mpi_writel(tmp, MPI_PCICFGCTL_REG);
327 + return bcm_mpi_readl(MPI_PCICFGDATA_REG);
330 +static void bcm63xx_int_cfg_writel(u32 val, u32 reg)
334 + tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
335 + tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
336 + bcm_mpi_writel(tmp, MPI_PCICFGCTL_REG);
337 + bcm_mpi_writel(val, MPI_PCICFGDATA_REG);
340 +void __iomem *pci_iospace_start;
342 +static int __init bcm63xx_pci_init(void)
344 + unsigned int mem_size;
347 + if (!BCMCPU_IS_6348() && !BCMCPU_IS_6358())
350 + if (!bcm63xx_pci_enabled)
354 + * configuration access are done through IO space, remap 4
355 + * first bytes to access it from CPU.
357 + * this means that no io access from CPU should happen while
358 + * we do a configuration cycle, but there's no way we can add
359 + * a spinlock for each io access, so this is currently kind of
362 + pci_iospace_start = ioremap_nocache(BCM_PCI_IO_BASE_PA, 4);
363 + if (!pci_iospace_start)
366 + /* setup local bus to PCI access (PCI memory) */
367 + val = BCM_PCI_MEM_BASE_PA & MPI_L2P_BASE_MASK;
368 + bcm_mpi_writel(val, MPI_L2PMEMBASE1_REG);
369 + bcm_mpi_writel(~(BCM_PCI_MEM_SIZE - 1), MPI_L2PMEMRANGE1_REG);
370 + bcm_mpi_writel(val | MPI_L2PREMAP_ENABLED_MASK, MPI_L2PMEMREMAP1_REG);
372 + /* set Cardbus IDSEL (type 0 cfg access on primary bus for
373 + * this IDSEL will be done on Cardbus instead) */
374 + val = bcm_pcmcia_readl(PCMCIA_C1_REG);
375 + val &= ~PCMCIA_C1_CBIDSEL_MASK;
376 + val |= (CARDBUS_PCI_IDSEL << PCMCIA_C1_CBIDSEL_SHIFT);
377 + bcm_pcmcia_writel(val, PCMCIA_C1_REG);
379 + /* disable second access windows */
380 + bcm_mpi_writel(0, MPI_L2PMEMREMAP2_REG);
382 + /* setup local bus to PCI access (IO memory), we have only 1
383 + * IO window for both PCI and cardbus, but it cannot handle
384 + * both at the same time, assume standard PCI for now, if
385 + * cardbus card has IO zone, PCI fixup will change window to
387 + val = BCM_PCI_IO_BASE_PA & MPI_L2P_BASE_MASK;
388 + bcm_mpi_writel(val, MPI_L2PIOBASE_REG);
389 + bcm_mpi_writel(~(BCM_PCI_IO_SIZE - 1), MPI_L2PIORANGE_REG);
390 + bcm_mpi_writel(val | MPI_L2PREMAP_ENABLED_MASK, MPI_L2PIOREMAP_REG);
392 + /* enable PCI related GPIO pins */
393 + bcm_mpi_writel(MPI_LOCBUSCTL_EN_PCI_GPIO_MASK, MPI_LOCBUSCTL_REG);
395 + /* setup PCI to local bus access, used by PCI device to target
396 + * local RAM while bus mastering */
397 + bcm63xx_int_cfg_writel(0, PCI_BASE_ADDRESS_3);
398 + if (BCMCPU_IS_6358())
399 + val = MPI_SP0_REMAP_ENABLE_MASK;
402 + bcm_mpi_writel(val, MPI_SP0_REMAP_REG);
404 + bcm63xx_int_cfg_writel(0x0, PCI_BASE_ADDRESS_4);
405 + bcm_mpi_writel(0, MPI_SP1_REMAP_REG);
407 + mem_size = bcm63xx_get_memory_size();
409 + /* 6348 before rev b0 exposes only 16 MB of RAM memory through
410 + * PCI, throw a warning if we have more memory */
411 + if (BCMCPU_IS_6348() && (bcm63xx_get_cpu_rev() & 0xf0) == 0xa0) {
412 + if (mem_size > (16 * 1024 * 1024))
413 + printk(KERN_WARNING "bcm63xx: this CPU "
414 + "revision cannot handle more than 16MB "
415 + "of RAM for PCI bus mastering\n");
417 + /* setup sp0 range to local RAM size */
418 + bcm_mpi_writel(~(mem_size - 1), MPI_SP0_RANGE_REG);
419 + bcm_mpi_writel(0, MPI_SP1_RANGE_REG);
422 + /* change host bridge retry counter to infinite number of
423 + * retry, needed for some broadcom wifi cards with Silicon
424 + * Backplane bus where access to srom seems very slow */
425 + val = bcm63xx_int_cfg_readl(BCMPCI_REG_TIMERS);
426 + val &= ~REG_TIMER_RETRY_MASK;
427 + bcm63xx_int_cfg_writel(val, BCMPCI_REG_TIMERS);
429 + /* enable memory decoder and bus mastering */
430 + val = bcm63xx_int_cfg_readl(PCI_COMMAND);
431 + val |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
432 + bcm63xx_int_cfg_writel(val, PCI_COMMAND);
434 + /* enable read prefetching & disable byte swapping for bus
435 + * mastering transfers */
436 + val = bcm_mpi_readl(MPI_PCIMODESEL_REG);
437 + val &= ~MPI_PCIMODESEL_BAR1_NOSWAP_MASK;
438 + val &= ~MPI_PCIMODESEL_BAR2_NOSWAP_MASK;
439 + val &= ~MPI_PCIMODESEL_PREFETCH_MASK;
440 + val |= (8 << MPI_PCIMODESEL_PREFETCH_SHIFT);
441 + bcm_mpi_writel(val, MPI_PCIMODESEL_REG);
443 + /* enable pci interrupt */
444 + val = bcm_mpi_readl(MPI_LOCINT_REG);
445 + val |= MPI_LOCINT_MASK(MPI_LOCINT_EXT_PCI_INT);
446 + bcm_mpi_writel(val, MPI_LOCINT_REG);
448 + register_pci_controller(&bcm63xx_controller);
450 + /* mark memory space used for IO mapping as reserved */
451 + request_mem_region(BCM_PCI_IO_BASE_PA, BCM_PCI_IO_SIZE,
452 + "bcm63xx PCI IO space");
456 +arch_initcall(bcm63xx_pci_init);
457 diff --git a/arch/mips/pci/pci-bcm63xx.h b/arch/mips/pci/pci-bcm63xx.h
459 index 0000000..a6e594e
461 +++ b/arch/mips/pci/pci-bcm63xx.h
463 +#ifndef PCI_BCM63XX_H_
464 +#define PCI_BCM63XX_H_
466 +#include <bcm63xx_cpu.h>
467 +#include <bcm63xx_io.h>
468 +#include <bcm63xx_regs.h>
469 +#include <bcm63xx_dev_pci.h>
472 + * Cardbus shares the PCI bus, but has no IDSEL, so a special id is
473 + * reserved for it. If you have a standard PCI device at this id, you
474 + * need to change the following definition.
476 +#define CARDBUS_PCI_IDSEL 0x8
479 + * defined in ops-bcm63xx.c
481 +extern struct pci_ops bcm63xx_pci_ops;
482 +extern struct pci_ops bcm63xx_cb_ops;
485 + * defined in pci-bcm63xx.c
487 +extern void __iomem *pci_iospace_start;
489 +#endif /* ! PCI_BCM63XX_H_ */
490 diff --git a/include/asm-mips/mach-bcm63xx/bcm63xx_dev_pci.h b/include/asm-mips/mach-bcm63xx/bcm63xx_dev_pci.h
492 index 0000000..c549344
494 +++ b/include/asm-mips/mach-bcm63xx/bcm63xx_dev_pci.h
496 +#ifndef BCM63XX_DEV_PCI_H_
497 +#define BCM63XX_DEV_PCI_H_
499 +extern int bcm63xx_pci_enabled;
501 +#endif /* BCM63XX_DEV_PCI_H_ */