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 --- a/arch/mips/bcm63xx/Kconfig
24 +++ b/arch/mips/bcm63xx/Kconfig
25 @@ -3,7 +3,9 @@ menu "CPU support"
27 config BCM63XX_CPU_6348
28 bool "support 6348 CPU"
31 config BCM63XX_CPU_6358
32 bool "support 6358 CPU"
35 --- a/arch/mips/bcm63xx/setup.c
36 +++ b/arch/mips/bcm63xx/setup.c
37 @@ -105,4 +105,6 @@ void __init plat_mem_setup(void)
38 pm_power_off = bcm63xx_machine_halt;
41 + ioport_resource.start = 0;
42 + ioport_resource.end = ~0;
44 --- a/arch/mips/pci/Makefile
45 +++ b/arch/mips/pci/Makefile
46 @@ -16,6 +16,8 @@ obj-$(CONFIG_PCI_VR41XX) += ops-vr41xx.o
47 obj-$(CONFIG_MARKEINS) += ops-emma2rh.o pci-emma2rh.o fixup-emma2rh.o
48 obj-$(CONFIG_PCI_TX4927) += ops-tx4927.o
49 obj-$(CONFIG_BCM47XX) += pci-bcm47xx.o
50 +obj-$(CONFIG_BCM63XX) += pci-bcm63xx.o fixup-bcm63xx.o \
54 # These are still pretty much in the old state, watch, go blind.
56 +++ b/arch/mips/pci/fixup-bcm63xx.c
59 + * This file is subject to the terms and conditions of the GNU General Public
60 + * License. See the file "COPYING" in the main directory of this archive
63 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
66 +#include <linux/types.h>
67 +#include <linux/pci.h>
68 +#include <bcm63xx_cpu.h>
70 +int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
72 + return bcm63xx_get_irq_number(IRQ_PCI);
75 +int pcibios_plat_dev_init(struct pci_dev *dev)
80 +++ b/arch/mips/pci/ops-bcm63xx.c
83 + * This file is subject to the terms and conditions of the GNU General Public
84 + * License. See the file "COPYING" in the main directory of this archive
87 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
90 +#include <linux/types.h>
91 +#include <linux/pci.h>
92 +#include <linux/kernel.h>
93 +#include <linux/init.h>
94 +#include <linux/delay.h>
95 +#include <linux/io.h>
97 +#include "pci-bcm63xx.h"
100 + * swizzle 32bits data to return only the needed part
102 +static int postprocess_read(u32 data, int where, unsigned int size)
109 + ret = (data >> ((where & 3) << 3)) & 0xff;
112 + ret = (data >> ((where & 3) << 3)) & 0xffff;
121 +static int preprocess_write(u32 orig_data, u32 val, int where,
129 + ret = (orig_data & ~(0xff << ((where & 3) << 3))) |
130 + (val << ((where & 3) << 3));
133 + ret = (orig_data & ~(0xffff << ((where & 3) << 3))) |
134 + (val << ((where & 3) << 3));
144 + * setup hardware for a configuration cycle with given parameters
146 +static int bcm63xx_setup_cfg_access(int type, unsigned int busn,
147 + unsigned int devfn, int where)
149 + unsigned int slot, func, reg;
152 + slot = PCI_SLOT(devfn);
153 + func = PCI_FUNC(devfn);
157 + if (slot > (MPI_L2PCFG_DEVNUM_MASK >> MPI_L2PCFG_DEVNUM_SHIFT))
160 + if (func > (MPI_L2PCFG_FUNC_MASK >> MPI_L2PCFG_FUNC_SHIFT))
163 + if (reg > (MPI_L2PCFG_REG_MASK >> MPI_L2PCFG_REG_SHIFT))
166 + /* ok, setup config access */
167 + val = (reg << MPI_L2PCFG_REG_SHIFT);
168 + val |= (func << MPI_L2PCFG_FUNC_SHIFT);
169 + val |= (slot << MPI_L2PCFG_DEVNUM_SHIFT);
170 + val |= MPI_L2PCFG_CFG_USEREG_MASK;
171 + val |= MPI_L2PCFG_CFG_SEL_MASK;
172 + /* type 0 cycle for local bus, type 1 cycle for anything else */
174 + /* FIXME: how to specify bus ??? */
175 + val |= (1 << MPI_L2PCFG_CFG_TYPE_SHIFT);
177 + bcm_mpi_writel(val, MPI_L2PCFG_REG);
182 +static int bcm63xx_do_cfg_read(int type, unsigned int busn,
183 + unsigned int devfn, int where, int size,
188 + /* two phase cycle, first we write address, then read data at
189 + * another location, caller already has a spinlock so no need
190 + * to add one here */
191 + if (bcm63xx_setup_cfg_access(type, busn, devfn, where))
192 + return PCIBIOS_DEVICE_NOT_FOUND;
194 + data = le32_to_cpu(__raw_readl(pci_iospace_start));
195 + /* restore IO space normal behaviour */
196 + bcm_mpi_writel(0, MPI_L2PCFG_REG);
198 + *val = postprocess_read(data, where, size);
200 + return PCIBIOS_SUCCESSFUL;
203 +static int bcm63xx_do_cfg_write(int type, unsigned int busn,
204 + unsigned int devfn, int where, int size,
209 + /* two phase cycle, first we write address, then write data to
210 + * another location, caller already has a spinlock so no need
211 + * to add one here */
212 + if (bcm63xx_setup_cfg_access(type, busn, devfn, where))
213 + return PCIBIOS_DEVICE_NOT_FOUND;
216 + data = le32_to_cpu(__raw_readl(pci_iospace_start));
217 + data = preprocess_write(data, val, where, size);
219 + __raw_writel(cpu_to_le32(data), pci_iospace_start);
221 + /* no way to know the access is done, we have to wait */
223 + /* restore IO space normal behaviour */
224 + bcm_mpi_writel(0, MPI_L2PCFG_REG);
226 + return PCIBIOS_SUCCESSFUL;
229 +static int bcm63xx_pci_read(struct pci_bus *bus, unsigned int devfn,
230 + int where, int size, u32 *val)
234 + type = bus->parent ? 1 : 0;
236 + if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
237 + return PCIBIOS_DEVICE_NOT_FOUND;
239 + return bcm63xx_do_cfg_read(type, bus->number, devfn,
243 +static int bcm63xx_pci_write(struct pci_bus *bus, unsigned int devfn,
244 + int where, int size, u32 val)
248 + type = bus->parent ? 1 : 0;
250 + if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
251 + return PCIBIOS_DEVICE_NOT_FOUND;
253 + return bcm63xx_do_cfg_write(type, bus->number, devfn,
257 +struct pci_ops bcm63xx_pci_ops = {
258 + .read = bcm63xx_pci_read,
259 + .write = bcm63xx_pci_write
262 +++ b/arch/mips/pci/pci-bcm63xx.c
265 + * This file is subject to the terms and conditions of the GNU General Public
266 + * License. See the file "COPYING" in the main directory of this archive
267 + * for more details.
269 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
272 +#include <linux/types.h>
273 +#include <linux/pci.h>
274 +#include <linux/kernel.h>
275 +#include <linux/init.h>
276 +#include <asm/bootinfo.h>
278 +#include "pci-bcm63xx.h"
280 +/* allow PCI to be disabled at runtime depending on board nvram
282 +int bcm63xx_pci_enabled = 0;
284 +static struct resource bcm_pci_mem_resource = {
285 + .name = "bcm63xx PCI memory space",
286 + .start = BCM_PCI_MEM_BASE_PA,
287 + .end = BCM_PCI_MEM_END_PA,
288 + .flags = IORESOURCE_MEM
291 +static struct resource bcm_pci_io_resource = {
292 + .name = "bcm63xx PCI IO space",
293 + .start = BCM_PCI_IO_BASE_PA,
294 + .end = BCM_PCI_IO_END_PA,
295 + .flags = IORESOURCE_IO
298 +struct pci_controller bcm63xx_controller = {
299 + .pci_ops = &bcm63xx_pci_ops,
300 + .io_resource = &bcm_pci_io_resource,
301 + .mem_resource = &bcm_pci_mem_resource,
304 +static u32 bcm63xx_int_cfg_readl(u32 reg)
308 + tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
309 + tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
310 + bcm_mpi_writel(tmp, MPI_PCICFGCTL_REG);
312 + return bcm_mpi_readl(MPI_PCICFGDATA_REG);
315 +static void bcm63xx_int_cfg_writel(u32 val, u32 reg)
319 + tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
320 + tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
321 + bcm_mpi_writel(tmp, MPI_PCICFGCTL_REG);
322 + bcm_mpi_writel(val, MPI_PCICFGDATA_REG);
325 +void __iomem *pci_iospace_start;
327 +static int __init bcm63xx_pci_init(void)
329 + unsigned int mem_size;
332 + if (!BCMCPU_IS_6348() && !BCMCPU_IS_6358())
335 + if (!bcm63xx_pci_enabled)
339 + * configuration access are done through IO space, remap 4
340 + * first bytes to access it from CPU.
342 + * this means that no io access from CPU should happen while
343 + * we do a configuration cycle, but there's no way we can add
344 + * a spinlock for each io access, so this is currently kind of
347 + pci_iospace_start = ioremap_nocache(BCM_PCI_IO_BASE_PA, 4);
348 + if (!pci_iospace_start)
351 + /* setup local bus to PCI access (PCI memory) */
352 + val = BCM_PCI_MEM_BASE_PA & MPI_L2P_BASE_MASK;
353 + bcm_mpi_writel(val, MPI_L2PMEMBASE1_REG);
354 + bcm_mpi_writel(~(BCM_PCI_MEM_SIZE - 1), MPI_L2PMEMRANGE1_REG);
355 + bcm_mpi_writel(val | MPI_L2PREMAP_ENABLED_MASK, MPI_L2PMEMREMAP1_REG);
357 + /* set Cardbus IDSEL (type 0 cfg access on primary bus for
358 + * this IDSEL will be done on Cardbus instead) */
359 + val = bcm_pcmcia_readl(PCMCIA_C1_REG);
360 + val &= ~PCMCIA_C1_CBIDSEL_MASK;
361 + val |= (CARDBUS_PCI_IDSEL << PCMCIA_C1_CBIDSEL_SHIFT);
362 + bcm_pcmcia_writel(val, PCMCIA_C1_REG);
364 + /* disable second access windows */
365 + bcm_mpi_writel(0, MPI_L2PMEMREMAP2_REG);
367 + /* setup local bus to PCI access (IO memory), we have only 1
368 + * IO window for both PCI and cardbus, but it cannot handle
369 + * both at the same time, assume standard PCI for now, if
370 + * cardbus card has IO zone, PCI fixup will change window to
372 + val = BCM_PCI_IO_BASE_PA & MPI_L2P_BASE_MASK;
373 + bcm_mpi_writel(val, MPI_L2PIOBASE_REG);
374 + bcm_mpi_writel(~(BCM_PCI_IO_SIZE - 1), MPI_L2PIORANGE_REG);
375 + bcm_mpi_writel(val | MPI_L2PREMAP_ENABLED_MASK, MPI_L2PIOREMAP_REG);
377 + /* enable PCI related GPIO pins */
378 + bcm_mpi_writel(MPI_LOCBUSCTL_EN_PCI_GPIO_MASK, MPI_LOCBUSCTL_REG);
380 + /* setup PCI to local bus access, used by PCI device to target
381 + * local RAM while bus mastering */
382 + bcm63xx_int_cfg_writel(0, PCI_BASE_ADDRESS_3);
383 + if (BCMCPU_IS_6358())
384 + val = MPI_SP0_REMAP_ENABLE_MASK;
387 + bcm_mpi_writel(val, MPI_SP0_REMAP_REG);
389 + bcm63xx_int_cfg_writel(0x0, PCI_BASE_ADDRESS_4);
390 + bcm_mpi_writel(0, MPI_SP1_REMAP_REG);
392 + mem_size = bcm63xx_get_memory_size();
394 + /* 6348 before rev b0 exposes only 16 MB of RAM memory through
395 + * PCI, throw a warning if we have more memory */
396 + if (BCMCPU_IS_6348() && (bcm63xx_get_cpu_rev() & 0xf0) == 0xa0) {
397 + if (mem_size > (16 * 1024 * 1024))
398 + printk(KERN_WARNING "bcm63xx: this CPU "
399 + "revision cannot handle more than 16MB "
400 + "of RAM for PCI bus mastering\n");
402 + /* setup sp0 range to local RAM size */
403 + bcm_mpi_writel(~(mem_size - 1), MPI_SP0_RANGE_REG);
404 + bcm_mpi_writel(0, MPI_SP1_RANGE_REG);
407 + /* change host bridge retry counter to infinite number of
408 + * retry, needed for some broadcom wifi cards with Silicon
409 + * Backplane bus where access to srom seems very slow */
410 + val = bcm63xx_int_cfg_readl(BCMPCI_REG_TIMERS);
411 + val &= ~REG_TIMER_RETRY_MASK;
412 + bcm63xx_int_cfg_writel(val, BCMPCI_REG_TIMERS);
414 + /* enable memory decoder and bus mastering */
415 + val = bcm63xx_int_cfg_readl(PCI_COMMAND);
416 + val |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
417 + bcm63xx_int_cfg_writel(val, PCI_COMMAND);
419 + /* enable read prefetching & disable byte swapping for bus
420 + * mastering transfers */
421 + val = bcm_mpi_readl(MPI_PCIMODESEL_REG);
422 + val &= ~MPI_PCIMODESEL_BAR1_NOSWAP_MASK;
423 + val &= ~MPI_PCIMODESEL_BAR2_NOSWAP_MASK;
424 + val &= ~MPI_PCIMODESEL_PREFETCH_MASK;
425 + val |= (8 << MPI_PCIMODESEL_PREFETCH_SHIFT);
426 + bcm_mpi_writel(val, MPI_PCIMODESEL_REG);
428 + /* enable pci interrupt */
429 + val = bcm_mpi_readl(MPI_LOCINT_REG);
430 + val |= MPI_LOCINT_MASK(MPI_LOCINT_EXT_PCI_INT);
431 + bcm_mpi_writel(val, MPI_LOCINT_REG);
433 + register_pci_controller(&bcm63xx_controller);
435 + /* mark memory space used for IO mapping as reserved */
436 + request_mem_region(BCM_PCI_IO_BASE_PA, BCM_PCI_IO_SIZE,
437 + "bcm63xx PCI IO space");
441 +arch_initcall(bcm63xx_pci_init);
443 +++ b/arch/mips/pci/pci-bcm63xx.h
445 +#ifndef PCI_BCM63XX_H_
446 +#define PCI_BCM63XX_H_
448 +#include <bcm63xx_cpu.h>
449 +#include <bcm63xx_io.h>
450 +#include <bcm63xx_regs.h>
451 +#include <bcm63xx_dev_pci.h>
454 + * Cardbus shares the PCI bus, but has no IDSEL, so a special id is
455 + * reserved for it. If you have a standard PCI device at this id, you
456 + * need to change the following definition.
458 +#define CARDBUS_PCI_IDSEL 0x8
461 + * defined in ops-bcm63xx.c
463 +extern struct pci_ops bcm63xx_pci_ops;
464 +extern struct pci_ops bcm63xx_cb_ops;
467 + * defined in pci-bcm63xx.c
469 +extern void __iomem *pci_iospace_start;
471 +#endif /* ! PCI_BCM63XX_H_ */
473 +++ b/include/asm-mips/mach-bcm63xx/bcm63xx_dev_pci.h
475 +#ifndef BCM63XX_DEV_PCI_H_
476 +#define BCM63XX_DEV_PCI_H_
478 +extern int bcm63xx_pci_enabled;
480 +#endif /* BCM63XX_DEV_PCI_H_ */