1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * BCM6348 PCI Controller Driver
5 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
6 * Copyright (C) 2015 Jonas Gorski <jonas.gorski@gmail.com>
7 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
10 #include <linux/clk.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/memblock.h>
16 #include <linux/of_address.h>
17 #include <linux/of_gpio.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_pci.h>
20 #include <linux/of_platform.h>
21 #include <linux/pci.h>
22 #include <linux/reset.h>
23 #include <linux/types.h>
24 #include <linux/version.h>
25 #include <linux/vmalloc.h>
29 #define CARDBUS_DUMMY_ID 0x6348
30 #define CARDBUS_PCI_IDSEL 0x8
31 #define FAKE_CB_BRIDGE_SLOT 0x1e
33 #define BCMPCI_REG_TIMERS 0x40
34 #define REG_TIMER_TRDY_SHIFT 0
35 #define REG_TIMER_TRDY_MASK (0xff << REG_TIMER_TRDY_SHIFT)
36 #define REG_TIMER_RETRY_SHIFT 8
37 #define REG_TIMER_RETRY_MASK (0xff << REG_TIMER_RETRY_SHIFT)
39 #define MPI_SP0_RANGE_REG 0x100
40 #define MPI_SP0_REMAP_REG 0x104
41 #define MPI_SP0_REMAP_ENABLE_MASK (1 << 0)
42 #define MPI_SP1_RANGE_REG 0x10C
43 #define MPI_SP1_REMAP_REG 0x110
44 #define MPI_SP1_REMAP_ENABLE_MASK (1 << 0)
46 #define MPI_L2PCFG_REG 0x11c
47 #define MPI_L2PCFG_CFG_TYPE_SHIFT 0
48 #define MPI_L2PCFG_CFG_TYPE_MASK (0x3 << MPI_L2PCFG_CFG_TYPE_SHIFT)
49 #define MPI_L2PCFG_REG_SHIFT 2
50 #define MPI_L2PCFG_REG_MASK (0x3f << MPI_L2PCFG_REG_SHIFT)
51 #define MPI_L2PCFG_FUNC_SHIFT 8
52 #define MPI_L2PCFG_FUNC_MASK (0x7 << MPI_L2PCFG_FUNC_SHIFT)
53 #define MPI_L2PCFG_DEVNUM_SHIFT 11
54 #define MPI_L2PCFG_DEVNUM_MASK (0x1f << MPI_L2PCFG_DEVNUM_SHIFT)
55 #define MPI_L2PCFG_CFG_USEREG_MASK (1 << 30)
56 #define MPI_L2PCFG_CFG_SEL_MASK (1 << 31)
58 #define MPI_L2PMEMRANGE1_REG 0x120
59 #define MPI_L2PMEMBASE1_REG 0x124
60 #define MPI_L2PMEMREMAP1_REG 0x128
61 #define MPI_L2PMEMRANGE2_REG 0x12C
62 #define MPI_L2PMEMBASE2_REG 0x130
63 #define MPI_L2PMEMREMAP2_REG 0x134
64 #define MPI_L2PIORANGE_REG 0x138
65 #define MPI_L2PIOBASE_REG 0x13C
66 #define MPI_L2PIOREMAP_REG 0x140
67 #define MPI_L2P_BASE_MASK (0xffff8000)
68 #define MPI_L2PREMAP_ENABLED_MASK (1 << 0)
69 #define MPI_L2PREMAP_IS_CARDBUS_MASK (1 << 2)
71 #define MPI_PCIMODESEL_REG 0x144
72 #define MPI_PCIMODESEL_BAR1_NOSWAP_MASK (1 << 0)
73 #define MPI_PCIMODESEL_BAR2_NOSWAP_MASK (1 << 1)
74 #define MPI_PCIMODESEL_EXT_ARB_MASK (1 << 2)
75 #define MPI_PCIMODESEL_PREFETCH_SHIFT 4
76 #define MPI_PCIMODESEL_PREFETCH_MASK (0xf << MPI_PCIMODESEL_PREFETCH_SHIFT)
78 #define MPI_LOCBUSCTL_REG 0x14c
79 #define MPI_LOCBUSCTL_EN_PCI_GPIO_MASK (1 << 0)
80 #define MPI_LOCBUSCTL_U2P_NOSWAP_MASK (1 << 1)
82 #define MPI_LOCINT_REG 0x150
83 #define MPI_LOCINT_MASK(x) (1 << (x + 16))
84 #define MPI_LOCINT_STAT(x) (1 << (x))
85 #define MPI_LOCINT_DIR_FAILED 6
86 #define MPI_LOCINT_EXT_PCI_INT 7
87 #define MPI_LOCINT_SERR 8
88 #define MPI_LOCINT_CSERR 9
90 #define MPI_PCICFGCTL_REG 0x178
91 #define MPI_PCICFGCTL_CFGADDR_SHIFT 2
92 #define MPI_PCICFGCTL_CFGADDR_MASK (0x1f << MPI_PCICFGCTL_CFGADDR_SHIFT)
93 #define MPI_PCICFGCTL_WRITEEN_MASK (1 << 7)
95 #define MPI_PCICFGDATA_REG 0x17c
97 #define PCMCIA_OFFSET 0x54
99 #define PCMCIA_C1_REG 0x0
100 #define PCMCIA_C1_CD1_MASK (1 << 0)
101 #define PCMCIA_C1_CD2_MASK (1 << 1)
102 #define PCMCIA_C1_VS1_MASK (1 << 2)
103 #define PCMCIA_C1_VS2_MASK (1 << 3)
104 #define PCMCIA_C1_VS1OE_MASK (1 << 6)
105 #define PCMCIA_C1_VS2OE_MASK (1 << 7)
106 #define PCMCIA_C1_CBIDSEL_SHIFT (8)
107 #define PCMCIA_C1_CBIDSEL_MASK (0x1f << PCMCIA_C1_CBIDSEL_SHIFT)
108 #define PCMCIA_C1_EN_PCMCIA_GPIO_MASK (1 << 13)
109 #define PCMCIA_C1_EN_PCMCIA_MASK (1 << 14)
110 #define PCMCIA_C1_EN_CARDBUS_MASK (1 << 15)
111 #define PCMCIA_C1_RESET_MASK (1 << 18)
113 #ifdef CONFIG_CARDBUS
133 #endif /* CONFIG_CARDBUS */
137 void __iomem
*pcmcia
;
140 struct reset_control
*reset
;
142 #ifdef CONFIG_CARDBUS
143 struct bcm6348_cb cb
;
145 #endif /* CONFIG_CARDBUS */
148 static struct bcm6348_pci bcm6348_pci
;
150 extern int bmips_pci_irq
;
152 static u32
bcm6348_int_cfg_readl(u32 reg
)
154 struct bcm6348_pci
*priv
= &bcm6348_pci
;
157 tmp
= reg
& MPI_PCICFGCTL_CFGADDR_MASK
;
158 tmp
|= MPI_PCICFGCTL_WRITEEN_MASK
;
159 __raw_writel(tmp
, priv
->pci
+ MPI_PCICFGCTL_REG
);
161 return __raw_readl(priv
->pci
+ MPI_PCICFGDATA_REG
);
164 static void bcm6348_int_cfg_writel(u32 val
, u32 reg
)
166 struct bcm6348_pci
*priv
= &bcm6348_pci
;
169 tmp
= reg
& MPI_PCICFGCTL_CFGADDR_MASK
;
170 tmp
|= MPI_PCICFGCTL_WRITEEN_MASK
;
171 __raw_writel(tmp
, priv
->pci
+ MPI_PCICFGCTL_REG
);
172 __raw_writel(val
, priv
->pci
+ MPI_PCICFGDATA_REG
);
176 * swizzle 32bits data to return only the needed part
178 static int postprocess_read(u32 data
, int where
, unsigned int size
)
184 ret
= (data
>> ((where
& 3) << 3)) & 0xff;
187 ret
= (data
>> ((where
& 3) << 3)) & 0xffff;
197 static int preprocess_write(u32 orig_data
, u32 val
, int where
,
204 ret
= (orig_data
& ~(0xff << ((where
& 3) << 3))) |
205 (val
<< ((where
& 3) << 3));
208 ret
= (orig_data
& ~(0xffff << ((where
& 3) << 3))) |
209 (val
<< ((where
& 3) << 3));
219 static int bcm6348_setup_cfg_access(int type
, unsigned int busn
,
220 unsigned int devfn
, int where
)
222 struct bcm6348_pci
*priv
= &bcm6348_pci
;
223 unsigned int slot
, func
, reg
;
226 slot
= PCI_SLOT(devfn
);
227 func
= PCI_FUNC(devfn
);
231 if (slot
> (MPI_L2PCFG_DEVNUM_MASK
>> MPI_L2PCFG_DEVNUM_SHIFT
))
234 if (func
> (MPI_L2PCFG_FUNC_MASK
>> MPI_L2PCFG_FUNC_SHIFT
))
237 if (reg
> (MPI_L2PCFG_REG_MASK
>> MPI_L2PCFG_REG_SHIFT
))
240 /* ok, setup config access */
241 val
= (reg
<< MPI_L2PCFG_REG_SHIFT
);
242 val
|= (func
<< MPI_L2PCFG_FUNC_SHIFT
);
243 val
|= (slot
<< MPI_L2PCFG_DEVNUM_SHIFT
);
244 val
|= MPI_L2PCFG_CFG_USEREG_MASK
;
245 val
|= MPI_L2PCFG_CFG_SEL_MASK
;
246 /* type 0 cycle for local bus, type 1 cycle for anything else */
248 /* FIXME: how to specify bus ??? */
249 val
|= (1 << MPI_L2PCFG_CFG_TYPE_SHIFT
);
251 __raw_writel(val
, priv
->pci
+ MPI_L2PCFG_REG
);
257 static int bcm6348_do_cfg_read(int type
, unsigned int busn
,
258 unsigned int devfn
, int where
, int size
,
261 struct bcm6348_pci
*priv
= &bcm6348_pci
;
264 /* two phase cycle, first we write address, then read data at
265 * another location, caller already has a spinlock so no need
267 if (bcm6348_setup_cfg_access(type
, busn
, devfn
, where
))
268 return PCIBIOS_DEVICE_NOT_FOUND
;
270 data
= le32_to_cpu(__raw_readl(priv
->io
));
271 /* restore IO space normal behaviour */
272 __raw_writel(0, priv
->pci
+ MPI_L2PCFG_REG
);
274 *val
= postprocess_read(data
, where
, size
);
276 return PCIBIOS_SUCCESSFUL
;
279 static int bcm6348_do_cfg_write(int type
, unsigned int busn
,
280 unsigned int devfn
, int where
, int size
,
283 struct bcm6348_pci
*priv
= &bcm6348_pci
;
286 /* two phase cycle, first we write address, then write data to
287 * another location, caller already has a spinlock so no need
289 if (bcm6348_setup_cfg_access(type
, busn
, devfn
, where
))
290 return PCIBIOS_DEVICE_NOT_FOUND
;
293 data
= le32_to_cpu(__raw_readl(priv
->io
));
294 data
= preprocess_write(data
, val
, where
, size
);
296 __raw_writel(cpu_to_le32(data
), priv
->io
);
298 /* no way to know the access is done, we have to wait */
300 /* restore IO space normal behaviour */
301 __raw_writel(0, priv
->pci
+ MPI_L2PCFG_REG
);
303 return PCIBIOS_SUCCESSFUL
;
306 static int bcm6348_pci_read(struct pci_bus
*bus
, unsigned int devfn
,
307 int where
, int size
, u32
*val
)
311 type
= bus
->parent
? 1 : 0;
313 if (type
== 0 && PCI_SLOT(devfn
) == CARDBUS_PCI_IDSEL
)
314 return PCIBIOS_DEVICE_NOT_FOUND
;
316 return bcm6348_do_cfg_read(type
, bus
->number
, devfn
,
320 static int bcm6348_pci_write(struct pci_bus
*bus
, unsigned int devfn
,
321 int where
, int size
, u32 val
)
325 type
= bus
->parent
? 1 : 0;
327 if (type
== 0 && PCI_SLOT(devfn
) == CARDBUS_PCI_IDSEL
)
328 return PCIBIOS_DEVICE_NOT_FOUND
;
330 return bcm6348_do_cfg_write(type
, bus
->number
, devfn
,
334 static struct pci_ops bcm6348_pci_ops
= {
335 .read
= bcm6348_pci_read
,
336 .write
= bcm6348_pci_write
,
339 static struct resource bcm6348_pci_io_resource
= {
340 .name
= "BCM6348 PCI IO space",
341 .flags
= IORESOURCE_IO
,
343 static struct resource bcm6348_pci_mem_resource
;
344 static struct resource bcm6348_pci_busn_resource
;
346 static struct pci_controller bcm6348_pci_controller
= {
347 .pci_ops
= &bcm6348_pci_ops
,
348 .io_resource
= &bcm6348_pci_io_resource
,
349 .mem_resource
= &bcm6348_pci_mem_resource
,
350 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,13,0)
351 .busn_resource
= &bcm6348_pci_busn_resource
,
355 #ifdef CONFIG_CARDBUS
356 static int bcm6348_cb_bridge_read(int where
, int size
, u32
*val
)
358 struct bcm6348_cb
*cb
= &bcm6348_pci
.cb
;
365 case (PCI_VENDOR_ID
>> 2):
366 case (PCI_CB_SUBSYSTEM_VENDOR_ID
>> 2):
367 /* create dummy vendor/device id from our cpu id */
368 data
= (CARDBUS_DUMMY_ID
<< 16) | PCI_VENDOR_ID_BROADCOM
;
371 case (PCI_COMMAND
>> 2):
372 data
= (PCI_STATUS_DEVSEL_SLOW
<< 16);
373 data
|= cb
->pci_command
;
376 case (PCI_CLASS_REVISION
>> 2):
377 data
= (PCI_CLASS_BRIDGE_CARDBUS
<< 16);
380 case (PCI_CACHE_LINE_SIZE
>> 2):
381 data
= (PCI_HEADER_TYPE_CARDBUS
<< 16);
384 case (PCI_INTERRUPT_LINE
>> 2):
386 data
= (cb
->bridge_control
<< 16);
387 /* pin:intA line:0xff */
388 data
|= (0x1 << 8) | 0xff;
391 case (PCI_CB_PRIMARY_BUS
>> 2):
392 data
= (cb
->cb_latency
<< 24);
393 data
|= (cb
->subordinate_busn
<< 16);
394 data
|= (cb
->cardbus_busn
<< 8);
395 data
|= cb
->pci_busn
;
398 case (PCI_CB_MEMORY_BASE_0
>> 2):
399 data
= cb
->mem_base0
;
402 case (PCI_CB_MEMORY_LIMIT_0
>> 2):
403 data
= cb
->mem_limit0
;
406 case (PCI_CB_MEMORY_BASE_1
>> 2):
407 data
= cb
->mem_base1
;
410 case (PCI_CB_MEMORY_LIMIT_1
>> 2):
411 data
= cb
->mem_limit1
;
414 case (PCI_CB_IO_BASE_0
>> 2):
415 /* | 1 for 32bits io support */
416 data
= cb
->io_base0
| 0x1;
419 case (PCI_CB_IO_LIMIT_0
>> 2):
420 data
= cb
->io_limit0
;
423 case (PCI_CB_IO_BASE_1
>> 2):
424 /* | 1 for 32bits io support */
425 data
= cb
->io_base1
| 0x1;
428 case (PCI_CB_IO_LIMIT_1
>> 2):
429 data
= cb
->io_limit1
;
433 *val
= postprocess_read(data
, where
, size
);
434 return PCIBIOS_SUCCESSFUL
;
438 * emulate configuration write access on a cardbus bridge
440 static int bcm6348_cb_bridge_write(int where
, int size
, u32 val
)
442 struct bcm6348_cb
*cb
= &bcm6348_pci
.cb
;
447 ret
= bcm6348_cb_bridge_read((where
& ~0x3), 4, &data
);
448 if (ret
!= PCIBIOS_SUCCESSFUL
)
451 data
= preprocess_write(data
, val
, where
, size
);
455 case (PCI_COMMAND
>> 2):
456 cb
->pci_command
= (data
& 0xffff);
459 case (PCI_CB_PRIMARY_BUS
>> 2):
460 cb
->cb_latency
= (data
>> 24) & 0xff;
461 cb
->subordinate_busn
= (data
>> 16) & 0xff;
462 cb
->cardbus_busn
= (data
>> 8) & 0xff;
463 cb
->pci_busn
= data
& 0xff;
464 if (cb
->cardbus_busn
)
465 cb
->bus_assigned
= 1;
468 case (PCI_INTERRUPT_LINE
>> 2):
469 tmp
= (data
>> 16) & 0xffff;
470 /* Disable memory prefetch support */
471 tmp
&= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
472 tmp
&= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1
;
473 cb
->bridge_control
= tmp
;
476 case (PCI_CB_MEMORY_BASE_0
>> 2):
477 cb
->mem_base0
= data
;
480 case (PCI_CB_MEMORY_LIMIT_0
>> 2):
481 cb
->mem_limit0
= data
;
484 case (PCI_CB_MEMORY_BASE_1
>> 2):
485 cb
->mem_base1
= data
;
488 case (PCI_CB_MEMORY_LIMIT_1
>> 2):
489 cb
->mem_limit1
= data
;
492 case (PCI_CB_IO_BASE_0
>> 2):
496 case (PCI_CB_IO_LIMIT_0
>> 2):
497 cb
->io_limit0
= data
;
500 case (PCI_CB_IO_BASE_1
>> 2):
504 case (PCI_CB_IO_LIMIT_1
>> 2):
505 cb
->io_limit1
= data
;
509 return PCIBIOS_SUCCESSFUL
;
512 static int bcm6348_cb_read(struct pci_bus
*bus
, unsigned int devfn
,
513 int where
, int size
, u32
*val
)
515 struct bcm6348_pci
*priv
= &bcm6348_pci
;
516 struct bcm6348_cb
*cb
= &priv
->cb
;
518 /* Snoop access to slot 0x1e on root bus, we fake a cardbus
519 * bridge at this location */
520 if (!bus
->parent
&& PCI_SLOT(devfn
) == FAKE_CB_BRIDGE_SLOT
) {
521 priv
->cb_bus
= bus
->number
;
522 return bcm6348_cb_bridge_read(where
, size
, val
);
525 /* A configuration cycle for the device behind the cardbus
526 * bridge is actually done as a type 0 cycle on the primary
527 * bus. This means that only one device can be on the cardbus
529 if (cb
->bus_assigned
&&
530 bus
->number
== cb
->cardbus_busn
&&
531 PCI_SLOT(devfn
) == 0)
532 return bcm6348_do_cfg_read(0, 0,
533 PCI_DEVFN(CARDBUS_PCI_IDSEL
, 0),
536 return PCIBIOS_DEVICE_NOT_FOUND
;
539 static int bcm6348_cb_write(struct pci_bus
*bus
, unsigned int devfn
,
540 int where
, int size
, u32 val
)
542 struct bcm6348_pci
*priv
= &bcm6348_pci
;
543 struct bcm6348_cb
*cb
= &priv
->cb
;
545 if (!bus
->parent
&& PCI_SLOT(devfn
) == FAKE_CB_BRIDGE_SLOT
) {
546 priv
->cb_bus
= bus
->number
;
547 return bcm6348_cb_bridge_write(where
, size
, val
);
550 if (cb
->bus_assigned
&&
551 bus
->number
== cb
->cardbus_busn
&&
552 PCI_SLOT(devfn
) == 0)
553 return bcm6348_do_cfg_write(0, 0,
554 PCI_DEVFN(CARDBUS_PCI_IDSEL
, 0),
557 return PCIBIOS_DEVICE_NOT_FOUND
;
560 static struct pci_ops bcm6348_cb_ops
= {
561 .read
= bcm6348_cb_read
,
562 .write
= bcm6348_cb_write
,
566 * only one IO window, so it cannot be shared by PCI and cardbus, use
567 * fixup to choose and detect unhandled configuration
569 static void bcm6348_pci_fixup(struct pci_dev
*dev
)
571 struct bcm6348_pci
*priv
= &bcm6348_pci
;
572 struct bcm6348_cb
*cb
= &priv
->cb
;
573 static int io_window
= -1;
574 int i
, found
, new_io_window
;
577 /* look for any io resource */
579 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
580 if (pci_resource_flags(dev
, i
) & IORESOURCE_IO
) {
589 /* skip our fake bus with only cardbus bridge on it */
590 if (dev
->bus
->number
== priv
->cb_bus
)
593 /* find on which bus the device is */
594 if (cb
->bus_assigned
&&
595 dev
->bus
->number
== cb
->cardbus_busn
&&
596 PCI_SLOT(dev
->devfn
) == 0)
601 if (new_io_window
== io_window
)
604 if (io_window
!= -1) {
605 pr_err("bcm63xx: both PCI and cardbus devices "
606 "need IO, which hardware cannot do\n");
610 pr_info("bcm63xx: PCI IO window assigned to %s\n",
611 (new_io_window
== 0) ? "PCI" : "cardbus");
613 val
= __raw_readl(priv
->pci
+ MPI_L2PIOREMAP_REG
);
615 val
|= MPI_L2PREMAP_IS_CARDBUS_MASK
;
617 val
&= ~MPI_L2PREMAP_IS_CARDBUS_MASK
;
618 __raw_writel(val
, priv
->pci
+ MPI_L2PIOREMAP_REG
);
620 io_window
= new_io_window
;
622 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID
, PCI_ANY_ID
, bcm6348_pci_fixup
);
624 static struct resource bcm6348_cb_io_resource
= {
625 .name
= "bcm6348 CB IO space",
626 .flags
= IORESOURCE_IO
,
628 static struct resource bcm6348_cb_mem_resource
;
630 static struct pci_controller bcm6348_cb_controller
= {
631 .pci_ops
= &bcm6348_cb_ops
,
632 .io_resource
= &bcm6348_cb_io_resource
,
633 .mem_resource
= &bcm6348_cb_mem_resource
,
635 #endif /* CONFIG_CARDBUS */
637 static void bcm6348_pci_setup(struct bcm6348_pci
*priv
)
641 /* Setup local bus to PCI access (PCI memory) */
642 val
= bcm6348_pci_mem_resource
.start
& MPI_L2P_BASE_MASK
;
643 __raw_writel(val
, priv
->pci
+ MPI_L2PMEMBASE1_REG
);
644 __raw_writel(~(resource_size(&bcm6348_pci_mem_resource
) - 1),
645 priv
->pci
+ MPI_L2PMEMRANGE1_REG
);
646 __raw_writel(val
| MPI_L2PREMAP_ENABLED_MASK
,
647 priv
->pci
+ MPI_L2PMEMREMAP1_REG
);
649 /* Set Cardbus IDSEL (type 0 cfg access on primary bus for
650 * this IDSEL will be done on Cardbus instead) */
651 val
= __raw_readl(priv
->pcmcia
+ PCMCIA_C1_REG
);
652 val
&= ~PCMCIA_C1_CBIDSEL_MASK
;
653 val
|= (CARDBUS_PCI_IDSEL
<< PCMCIA_C1_CBIDSEL_SHIFT
);
654 __raw_writel(val
, priv
->pcmcia
+ PCMCIA_C1_REG
);
656 #ifdef CONFIG_CARDBUS
657 /* setup local bus to PCI access (Cardbus memory) */
658 val
= bcm6348_cb_mem_resource
.start
& MPI_L2P_BASE_MASK
;
659 __raw_writel(val
, priv
->pci
+ MPI_L2PMEMBASE2_REG
);
660 __raw_writel(~(resource_size(&bcm6348_cb_mem_resource
) - 1),
661 priv
->pci
+ MPI_L2PMEMRANGE2_REG
);
662 val
|= MPI_L2PREMAP_ENABLED_MASK
| MPI_L2PREMAP_IS_CARDBUS_MASK
;
663 __raw_writel(val
, priv
->pci
+ MPI_L2PMEMREMAP2_REG
);
665 /* disable second access windows */
666 __raw_writel(0, priv
->pci
+ MPI_L2PMEMREMAP2_REG
);
669 /* setup local bus to PCI access (IO memory), we have only 1
670 * IO window for both PCI and cardbus, but it cannot handle
671 * both at the same time, assume standard PCI for now, if
672 * cardbus card has IO zone, PCI fixup will change window to
674 val
= bcm6348_pci_io_resource
.start
& MPI_L2P_BASE_MASK
;
675 __raw_writel(val
, priv
->pci
+ MPI_L2PIOBASE_REG
);
676 __raw_writel(~(resource_size(&bcm6348_pci_io_resource
) - 1),
677 priv
->pci
+ MPI_L2PIORANGE_REG
);
678 __raw_writel(val
| MPI_L2PREMAP_ENABLED_MASK
,
679 priv
->pci
+ MPI_L2PIOREMAP_REG
);
681 /* Enable PCI related GPIO pins */
682 __raw_writel(MPI_LOCBUSCTL_EN_PCI_GPIO_MASK
,
683 priv
->pci
+ MPI_LOCBUSCTL_REG
);
685 /* Setup PCI to local bus access, used by PCI device to target
686 * local RAM while bus mastering */
687 bcm6348_int_cfg_writel(0, PCI_BASE_ADDRESS_3
);
689 val
= MPI_SP0_REMAP_ENABLE_MASK
;
692 __raw_writel(val
, priv
->pci
+ MPI_SP0_REMAP_REG
);
694 bcm6348_int_cfg_writel(0, PCI_BASE_ADDRESS_4
);
695 __raw_writel(0, priv
->pci
+ MPI_SP1_REMAP_REG
);
697 /* Setup sp0 range to local RAM size */
698 __raw_writel(~(memblock_phys_mem_size() - 1),
699 priv
->pci
+ MPI_SP0_RANGE_REG
);
700 __raw_writel(0, priv
->pci
+ MPI_SP1_RANGE_REG
);
702 /* Change host bridge retry counter to infinite number of
703 * retries, needed for some broadcom wifi cards with Silicon
704 * Backplane bus where access to srom seems very slow */
705 val
= bcm6348_int_cfg_readl(BCMPCI_REG_TIMERS
);
706 val
&= ~REG_TIMER_RETRY_MASK
;
707 bcm6348_int_cfg_writel(val
, BCMPCI_REG_TIMERS
);
709 /* EEnable memory decoder and bus mastering */
710 val
= bcm6348_int_cfg_readl(PCI_COMMAND
);
711 val
|= (PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
712 bcm6348_int_cfg_writel(val
, PCI_COMMAND
);
714 /* Enable read prefetching & disable byte swapping for bus
715 * mastering transfers */
716 val
= __raw_readl(priv
->pci
+ MPI_PCIMODESEL_REG
);
717 val
&= ~MPI_PCIMODESEL_BAR1_NOSWAP_MASK
;
718 val
&= ~MPI_PCIMODESEL_BAR2_NOSWAP_MASK
;
719 val
&= ~MPI_PCIMODESEL_PREFETCH_MASK
;
720 val
|= (8 << MPI_PCIMODESEL_PREFETCH_SHIFT
);
721 __raw_writel(val
, priv
->pci
+ MPI_PCIMODESEL_REG
);
723 /* Enable pci interrupt */
724 val
= __raw_readl(priv
->pci
+ MPI_LOCINT_REG
);
725 val
|= MPI_LOCINT_MASK(MPI_LOCINT_EXT_PCI_INT
);
726 __raw_writel(val
, priv
->pci
+ MPI_LOCINT_REG
);
729 static int bcm6348_pci_probe(struct platform_device
*pdev
)
731 struct device
*dev
= &pdev
->dev
;
732 struct device_node
*np
= dev
->of_node
;
733 struct bcm6348_pci
*priv
= &bcm6348_pci
;
734 struct resource
*res
;
735 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,13,0)
736 LIST_HEAD(resources
);
739 of_pci_check_probe_only();
741 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pci");
742 priv
->pci
= devm_ioremap_resource(dev
, res
);
743 if (IS_ERR(priv
->pci
))
744 return PTR_ERR(priv
->pci
);
746 priv
->pcmcia
= priv
->pci
+ PCMCIA_OFFSET
;
748 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "pci-io");
751 #ifdef CONFIG_CARDBUS
752 bcm6348_pci_io_resource
.start
= res
->start
;
753 bcm6348_pci_io_resource
.end
= res
->end
- (resource_size(res
) >> 1);
754 bcm6348_cb_io_resource
.start
= res
->start
+ (resource_size(res
) >> 1);
755 bcm6348_cb_io_resource
.end
= res
->end
;
757 bcm6348_pci_io_resource
.start
= res
->start
;
758 bcm6348_pci_io_resource
.end
= res
->end
;
761 priv
->irq
= platform_get_irq(pdev
, 0);
765 bmips_pci_irq
= priv
->irq
;
767 priv
->reset
= devm_reset_control_get(dev
, "pci");
768 if (IS_ERR(priv
->reset
))
769 return PTR_ERR(priv
->reset
);
771 priv
->remap
= of_property_read_bool(np
, "brcm,remap");
773 reset_control_reset(priv
->reset
);
775 pci_load_of_ranges(&bcm6348_pci_controller
, np
);
776 if (!bcm6348_pci_mem_resource
.start
)
779 of_pci_parse_bus_range(np
, &bcm6348_pci_busn_resource
);
780 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,13,0)
781 pci_add_resource(&resources
, &bcm6348_pci_busn_resource
);
785 * Configuration accesses are done through IO space, remap 4
786 * first bytes to access it from CPU.
788 * This means that no IO access from CPU should happen while
789 * we do a configuration cycle, but there's no way we can add
790 * a spinlock for each io access, so this is currently kind of
793 priv
->io
= ioremap(bcm6348_pci_io_resource
.start
, sizeof(u32
));
797 bcm6348_pci_setup(priv
);
799 register_pci_controller(&bcm6348_pci_controller
);
801 #ifdef CONFIG_CARDBUS
803 register_pci_controller(&bcm6348_cb_controller
);
804 #endif /* CONFIG_CARDBUS */
806 /* Mark memory space used for IO mapping as reserved */
807 request_mem_region(bcm6348_pci_io_resource
.start
,
808 resource_size(&bcm6348_pci_io_resource
),
809 "BCM6348 PCI IO space");
814 static const struct of_device_id bcm6348_pci_of_match
[] = {
815 { .compatible
= "brcm,bcm6348-pci", },
819 static struct platform_driver bcm6348_pci_driver
= {
820 .probe
= bcm6348_pci_probe
,
822 .name
= "bcm6348-pci",
823 .of_match_table
= bcm6348_pci_of_match
,
827 int __init
bcm6348_pci_init(void)
829 int ret
= platform_driver_register(&bcm6348_pci_driver
);
831 pr_err("pci-bcm6348: Error registering platform driver!\n");
834 late_initcall_sync(bcm6348_pci_init
);