2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
9 #include <linux/types.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
16 #include "pci-bcm63xx.h"
19 * swizzle 32bits data to return only the needed part
21 static int postprocess_read(u32 data
, int where
, unsigned int size
)
28 ret
= (data
>> ((where
& 3) << 3)) & 0xff;
31 ret
= (data
>> ((where
& 3) << 3)) & 0xffff;
40 static int preprocess_write(u32 orig_data
, u32 val
, int where
,
48 ret
= (orig_data
& ~(0xff << ((where
& 3) << 3))) |
49 (val
<< ((where
& 3) << 3));
52 ret
= (orig_data
& ~(0xffff << ((where
& 3) << 3))) |
53 (val
<< ((where
& 3) << 3));
63 * setup hardware for a configuration cycle with given parameters
65 static int bcm63xx_setup_cfg_access(int type
, unsigned int busn
,
66 unsigned int devfn
, int where
)
68 unsigned int slot
, func
, reg
;
71 slot
= PCI_SLOT(devfn
);
72 func
= PCI_FUNC(devfn
);
76 if (slot
> (MPI_L2PCFG_DEVNUM_MASK
>> MPI_L2PCFG_DEVNUM_SHIFT
))
79 if (func
> (MPI_L2PCFG_FUNC_MASK
>> MPI_L2PCFG_FUNC_SHIFT
))
82 if (reg
> (MPI_L2PCFG_REG_MASK
>> MPI_L2PCFG_REG_SHIFT
))
85 /* ok, setup config access */
86 val
= (reg
<< MPI_L2PCFG_REG_SHIFT
);
87 val
|= (func
<< MPI_L2PCFG_FUNC_SHIFT
);
88 val
|= (slot
<< MPI_L2PCFG_DEVNUM_SHIFT
);
89 val
|= MPI_L2PCFG_CFG_USEREG_MASK
;
90 val
|= MPI_L2PCFG_CFG_SEL_MASK
;
91 /* type 0 cycle for local bus, type 1 cycle for anything else */
93 /* FIXME: how to specify bus ??? */
94 val
|= (1 << MPI_L2PCFG_CFG_TYPE_SHIFT
);
96 bcm_mpi_writel(val
, MPI_L2PCFG_REG
);
101 static int bcm63xx_do_cfg_read(int type
, unsigned int busn
,
102 unsigned int devfn
, int where
, int size
,
107 /* two phase cycle, first we write address, then read data at
108 * another location, caller already has a spinlock so no need
110 if (bcm63xx_setup_cfg_access(type
, busn
, devfn
, where
))
111 return PCIBIOS_DEVICE_NOT_FOUND
;
113 data
= le32_to_cpu(__raw_readl(pci_iospace_start
));
114 /* restore IO space normal behaviour */
115 bcm_mpi_writel(0, MPI_L2PCFG_REG
);
117 *val
= postprocess_read(data
, where
, size
);
119 return PCIBIOS_SUCCESSFUL
;
122 static int bcm63xx_do_cfg_write(int type
, unsigned int busn
,
123 unsigned int devfn
, int where
, int size
,
128 /* two phase cycle, first we write address, then write data to
129 * another location, caller already has a spinlock so no need
131 if (bcm63xx_setup_cfg_access(type
, busn
, devfn
, where
))
132 return PCIBIOS_DEVICE_NOT_FOUND
;
135 data
= le32_to_cpu(__raw_readl(pci_iospace_start
));
136 data
= preprocess_write(data
, val
, where
, size
);
138 __raw_writel(cpu_to_le32(data
), pci_iospace_start
);
140 /* no way to know the access is done, we have to wait */
142 /* restore IO space normal behaviour */
143 bcm_mpi_writel(0, MPI_L2PCFG_REG
);
145 return PCIBIOS_SUCCESSFUL
;
148 static int bcm63xx_pci_read(struct pci_bus
*bus
, unsigned int devfn
,
149 int where
, int size
, u32
*val
)
153 type
= bus
->parent
? 1 : 0;
155 if (type
== 0 && PCI_SLOT(devfn
) == CARDBUS_PCI_IDSEL
)
156 return PCIBIOS_DEVICE_NOT_FOUND
;
158 return bcm63xx_do_cfg_read(type
, bus
->number
, devfn
,
162 static int bcm63xx_pci_write(struct pci_bus
*bus
, unsigned int devfn
,
163 int where
, int size
, u32 val
)
167 type
= bus
->parent
? 1 : 0;
169 if (type
== 0 && PCI_SLOT(devfn
) == CARDBUS_PCI_IDSEL
)
170 return PCIBIOS_DEVICE_NOT_FOUND
;
172 return bcm63xx_do_cfg_write(type
, bus
->number
, devfn
,
176 struct pci_ops bcm63xx_pci_ops
= {
177 .read
= bcm63xx_pci_read
,
178 .write
= bcm63xx_pci_write
181 #ifdef CONFIG_CARDBUS
183 * emulate configuration read access on a cardbus bridge
185 #define FAKE_CB_BRIDGE_SLOT 0x1e
187 static int fake_cb_bridge_bus_number
= -1;
207 } fake_cb_bridge_regs
;
209 static int fake_cb_bridge_read(int where
, int size
, u32
*val
)
217 case (PCI_VENDOR_ID
>> 2):
218 case (PCI_CB_SUBSYSTEM_VENDOR_ID
>> 2):
219 /* create dummy vendor/device id from our cpu id */
220 data
= (bcm63xx_get_cpu_id() << 16) | PCI_VENDOR_ID_BROADCOM
;
223 case (PCI_COMMAND
>> 2):
224 data
= (PCI_STATUS_DEVSEL_SLOW
<< 16);
225 data
|= fake_cb_bridge_regs
.pci_command
;
228 case (PCI_CLASS_REVISION
>> 2):
229 data
= (PCI_CLASS_BRIDGE_CARDBUS
<< 16);
232 case (PCI_CACHE_LINE_SIZE
>> 2):
233 data
= (PCI_HEADER_TYPE_CARDBUS
<< 16);
236 case (PCI_INTERRUPT_LINE
>> 2):
238 data
= (fake_cb_bridge_regs
.bridge_control
<< 16);
239 /* pin:intA line:0xff */
240 data
|= (0x1 << 8) | 0xff;
243 case (PCI_CB_PRIMARY_BUS
>> 2):
244 data
= (fake_cb_bridge_regs
.cb_latency
<< 24);
245 data
|= (fake_cb_bridge_regs
.subordinate_busn
<< 16);
246 data
|= (fake_cb_bridge_regs
.cardbus_busn
<< 8);
247 data
|= fake_cb_bridge_regs
.pci_busn
;
250 case (PCI_CB_MEMORY_BASE_0
>> 2):
251 data
= fake_cb_bridge_regs
.mem_base0
;
254 case (PCI_CB_MEMORY_LIMIT_0
>> 2):
255 data
= fake_cb_bridge_regs
.mem_limit0
;
258 case (PCI_CB_MEMORY_BASE_1
>> 2):
259 data
= fake_cb_bridge_regs
.mem_base1
;
262 case (PCI_CB_MEMORY_LIMIT_1
>> 2):
263 data
= fake_cb_bridge_regs
.mem_limit1
;
266 case (PCI_CB_IO_BASE_0
>> 2):
267 /* | 1 for 32bits io support */
268 data
= fake_cb_bridge_regs
.io_base0
| 0x1;
271 case (PCI_CB_IO_LIMIT_0
>> 2):
272 data
= fake_cb_bridge_regs
.io_limit0
;
275 case (PCI_CB_IO_BASE_1
>> 2):
276 /* | 1 for 32bits io support */
277 data
= fake_cb_bridge_regs
.io_base1
| 0x1;
280 case (PCI_CB_IO_LIMIT_1
>> 2):
281 data
= fake_cb_bridge_regs
.io_limit1
;
285 *val
= postprocess_read(data
, where
, size
);
286 return PCIBIOS_SUCCESSFUL
;
290 * emulate configuration write access on a cardbus bridge
292 static int fake_cb_bridge_write(int where
, int size
, u32 val
)
298 ret
= fake_cb_bridge_read((where
& ~0x3), 4, &data
);
299 if (ret
!= PCIBIOS_SUCCESSFUL
)
302 data
= preprocess_write(data
, val
, where
, size
);
306 case (PCI_COMMAND
>> 2):
307 fake_cb_bridge_regs
.pci_command
= (data
& 0xffff);
310 case (PCI_CB_PRIMARY_BUS
>> 2):
311 fake_cb_bridge_regs
.cb_latency
= (data
>> 24) & 0xff;
312 fake_cb_bridge_regs
.subordinate_busn
= (data
>> 16) & 0xff;
313 fake_cb_bridge_regs
.cardbus_busn
= (data
>> 8) & 0xff;
314 fake_cb_bridge_regs
.pci_busn
= data
& 0xff;
315 if (fake_cb_bridge_regs
.cardbus_busn
)
316 fake_cb_bridge_regs
.bus_assigned
= 1;
319 case (PCI_INTERRUPT_LINE
>> 2):
320 tmp
= (data
>> 16) & 0xffff;
321 /* disable memory prefetch support */
322 tmp
&= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0
;
323 tmp
&= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1
;
324 fake_cb_bridge_regs
.bridge_control
= tmp
;
327 case (PCI_CB_MEMORY_BASE_0
>> 2):
328 fake_cb_bridge_regs
.mem_base0
= data
;
331 case (PCI_CB_MEMORY_LIMIT_0
>> 2):
332 fake_cb_bridge_regs
.mem_limit0
= data
;
335 case (PCI_CB_MEMORY_BASE_1
>> 2):
336 fake_cb_bridge_regs
.mem_base1
= data
;
339 case (PCI_CB_MEMORY_LIMIT_1
>> 2):
340 fake_cb_bridge_regs
.mem_limit1
= data
;
343 case (PCI_CB_IO_BASE_0
>> 2):
344 fake_cb_bridge_regs
.io_base0
= data
;
347 case (PCI_CB_IO_LIMIT_0
>> 2):
348 fake_cb_bridge_regs
.io_limit0
= data
;
351 case (PCI_CB_IO_BASE_1
>> 2):
352 fake_cb_bridge_regs
.io_base1
= data
;
355 case (PCI_CB_IO_LIMIT_1
>> 2):
356 fake_cb_bridge_regs
.io_limit1
= data
;
360 return PCIBIOS_SUCCESSFUL
;
363 static int bcm63xx_cb_read(struct pci_bus
*bus
, unsigned int devfn
,
364 int where
, int size
, u32
*val
)
366 /* snoop access to slot 0x1e on root bus, we fake a cardbus
367 * bridge at this location */
368 if (!bus
->parent
&& PCI_SLOT(devfn
) == FAKE_CB_BRIDGE_SLOT
) {
369 fake_cb_bridge_bus_number
= bus
->number
;
370 return fake_cb_bridge_read(where
, size
, val
);
373 /* a configuration cycle for the device behind the cardbus
374 * bridge is actually done as a type 0 cycle on the primary
375 * bus. This means that only one device can be on the cardbus
377 if (fake_cb_bridge_regs
.bus_assigned
&&
378 bus
->number
== fake_cb_bridge_regs
.cardbus_busn
&&
379 PCI_SLOT(devfn
) == 0)
380 return bcm63xx_do_cfg_read(0, 0,
381 PCI_DEVFN(CARDBUS_PCI_IDSEL
, 0),
384 return PCIBIOS_DEVICE_NOT_FOUND
;
387 static int bcm63xx_cb_write(struct pci_bus
*bus
, unsigned int devfn
,
388 int where
, int size
, u32 val
)
390 if (!bus
->parent
&& PCI_SLOT(devfn
) == FAKE_CB_BRIDGE_SLOT
) {
391 fake_cb_bridge_bus_number
= bus
->number
;
392 return fake_cb_bridge_write(where
, size
, val
);
395 if (fake_cb_bridge_regs
.bus_assigned
&&
396 bus
->number
== fake_cb_bridge_regs
.cardbus_busn
&&
397 PCI_SLOT(devfn
) == 0)
398 return bcm63xx_do_cfg_write(0, 0,
399 PCI_DEVFN(CARDBUS_PCI_IDSEL
, 0),
402 return PCIBIOS_DEVICE_NOT_FOUND
;
405 struct pci_ops bcm63xx_cb_ops
= {
406 .read
= bcm63xx_cb_read
,
407 .write
= bcm63xx_cb_write
,
411 * only one IO window, so it cannot be shared by PCI and cardbus, use
412 * fixup to choose and detect unhandled configuration
414 static void __devinit
bcm63xx_fixup(struct pci_dev
*dev
)
416 static int io_window
= -1;
417 int i
, found
, new_io_window
;
420 /* look for any io resource */
422 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
423 if (pci_resource_flags(dev
, i
) & IORESOURCE_IO
) {
432 /* skip our fake bus with only cardbus bridge on it */
433 if (dev
->bus
->number
== fake_cb_bridge_bus_number
)
436 /* find on which bus the device is */
437 if (fake_cb_bridge_regs
.bus_assigned
&&
438 dev
->bus
->number
== fake_cb_bridge_regs
.cardbus_busn
&&
439 PCI_SLOT(dev
->devfn
) == 0)
444 if (new_io_window
== io_window
)
447 if (io_window
!= -1) {
448 printk(KERN_ERR
"bcm63xx: both PCI and cardbus devices "
449 "need IO, which hardware cannot do\n");
453 printk(KERN_INFO
"bcm63xx: PCI IO window assigned to %s\n",
454 (new_io_window
== 0) ? "PCI" : "cardbus");
456 val
= bcm_mpi_readl(MPI_L2PIOREMAP_REG
);
458 val
|= MPI_L2PREMAP_IS_CARDBUS_MASK
;
460 val
&= ~MPI_L2PREMAP_IS_CARDBUS_MASK
;
461 bcm_mpi_writel(val
, MPI_L2PIOREMAP_REG
);
463 io_window
= new_io_window
;
466 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID
, PCI_ANY_ID
, bcm63xx_fixup
);
469 static int bcm63xx_pcie_can_access(struct pci_bus
*bus
, int devfn
)
471 switch (bus
->number
) {
472 case PCIE_BUS_BRIDGE
:
473 return (PCI_SLOT(devfn
) == 0);
474 case PCIE_BUS_DEVICE
:
475 if (PCI_SLOT(devfn
) == 0)
476 return bcm_pcie_readl(PCIE_DLSTATUS_REG
)
477 & DLSTATUS_PHYLINKUP
;
483 static int bcm63xx_pcie_read(struct pci_bus
*bus
, unsigned int devfn
,
484 int where
, int size
, u32
*val
)
487 u32 reg
= where
& ~3;
489 if (!bcm63xx_pcie_can_access(bus
, devfn
))
490 return PCIBIOS_DEVICE_NOT_FOUND
;
492 if (bus
->number
== PCIE_BUS_DEVICE
)
493 reg
+= PCIE_DEVICE_OFFSET
;
495 data
= bcm_pcie_readl(reg
);
497 *val
= postprocess_read(data
, where
, size
);
499 return PCIBIOS_SUCCESSFUL
;
503 static int bcm63xx_pcie_write(struct pci_bus
*bus
, unsigned int devfn
,
504 int where
, int size
, u32 val
)
507 u32 reg
= where
& ~3;
509 if (!bcm63xx_pcie_can_access(bus
, devfn
))
510 return PCIBIOS_DEVICE_NOT_FOUND
;
512 if (bus
->number
== PCIE_BUS_DEVICE
)
513 reg
+= PCIE_DEVICE_OFFSET
;
516 data
= bcm_pcie_readl(reg
);
518 data
= preprocess_write(data
, val
, where
, size
);
519 bcm_pcie_writel(data
, reg
);
521 return PCIBIOS_SUCCESSFUL
;
525 struct pci_ops bcm63xx_pcie_ops
= {
526 .read
= bcm63xx_pcie_read
,
527 .write
= bcm63xx_pcie_write