23 #include <linux/kernel.h>
24 #include <linux/pci.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
34 #include <asm/iommu.h>
35 #include <asm/byteorder.h>
40 #define PEX_IN(base, off) in_be32((void __iomem *)(base) + (off))
41 #define PEX_OUT(base, off, data) out_be32((void __iomem *)(base) + (off), (data))
43 static void scc_pciex_io_flush(
struct iowa_bus *
bus)
51 #define PCIEX_MMIO_READ(name, ret) \
52 static ret scc_pciex_##name(const PCI_IO_ADDR addr) \
54 ret val = __do_##name(addr); \
55 scc_pciex_io_flush(iowa_mem_find_bus(addr)); \
59 #define PCIEX_MMIO_READ_STR(name) \
60 static void scc_pciex_##name(const PCI_IO_ADDR addr, void *buf, \
61 unsigned long count) \
63 __do_##name(addr, buf, count); \
64 scc_pciex_io_flush(iowa_mem_find_bus(addr)); \
78 static
void scc_pciex_memcpy_fromio(
void *
dest,
const PCI_IO_ADDR
src,
81 __do_memcpy_fromio(dest, src, n);
89 static inline unsigned long get_bus_address(
struct pci_controller *phb,
92 return port - ((
unsigned long)(phb->io_base_virt) -
_IO_BASE);
98 unsigned int byte_enable;
99 unsigned int cmd, shift;
103 BUG_ON(((port & 0x3ul) + size) > 4);
105 addr = get_bus_address(phb, port);
106 shift = addr & 0x3ul;
107 byte_enable = ((1 <<
size) - 1) << shift;
112 ret = (data >> (shift * 8)) & (0xFFFFFFFF >> ((4 -
size) * 8));
114 pr_debug(
"PCIEX:PIO READ:port=0x%lx, addr=0x%lx, size=%d, be=%x,"
115 " cmd=%x, data=%x, ret=%x\n", port, addr, size, byte_enable,
122 unsigned long port,
int size,
u32 val)
124 unsigned int byte_enable;
125 unsigned int cmd, shift;
129 BUG_ON(((port & 0x3ul) + size) > 4);
131 addr = get_bus_address(phb, port);
132 shift = addr & 0x3ul;
133 byte_enable = ((1 <<
size) - 1) << shift;
135 data = (val & (0xFFFFFFFF >> (4 -
size) * 8)) << (shift * 8);
140 pr_debug(
"PCIEX:PIO WRITE:port=0x%lx, addr=%lx, size=%d, val=%x,"
141 " be=%x, cmd=%x, data=%x\n", port, addr, size, val,
142 byte_enable, cmd, data);
147 return (
u8)scc_pciex_read_port(phb, port, 1);
153 if ((port & 0x3ul) < 3)
154 data = scc_pciex_read_port(phb, port, 2);
156 u32 d1 = scc_pciex_read_port(phb, port, 1);
157 u32 d2 = scc_pciex_read_port(phb, port + 1, 1);
158 data = d1 | (d2 << 8);
165 unsigned int mod = port & 0x3ul;
168 data = scc_pciex_read_port(phb, port, 4);
170 u32 d1 = scc_pciex_read_port(phb, port, 4 - mod);
171 u32 d2 = scc_pciex_read_port(phb, port + 1, mod);
172 data = d1 | (d2 << (mod * 8));
178 u8 val,
unsigned long port)
180 scc_pciex_write_port(phb, port, 1, (
u32)val);
184 u16 val,
unsigned long port)
186 if ((port & 0x3ul) < 3)
187 scc_pciex_write_port(phb, port, 2, (
u32)val);
189 u32 d1 = val & 0x000000FF;
190 u32 d2 = (val & 0x0000FF00) >> 8;
191 scc_pciex_write_port(phb, port, 1, d1);
192 scc_pciex_write_port(phb, port + 1, 1, d2);
197 u32 val,
unsigned long port)
199 unsigned int mod = port & 0x3ul;
201 scc_pciex_write_port(phb, port, 4, val);
203 u32 d1 = val & (0xFFFFFFFFul >> (mod * 8));
205 scc_pciex_write_port(phb, port, 4 - mod,
d1);
206 scc_pciex_write_port(phb, port + 1, mod,
d2);
210 #define PCIEX_PIO_FUNC(size, name) \
211 static u##size scc_pciex_in##name(unsigned long port) \
213 struct iowa_bus *bus = iowa_pio_find_bus(port); \
214 u##size data = __scc_pciex_in##name(bus->phb, port); \
215 scc_pciex_io_flush(bus); \
218 static void scc_pciex_ins##name(unsigned long p, void *b, unsigned long c) \
220 struct iowa_bus *bus = iowa_pio_find_bus(p); \
221 __le##size *dst = b; \
222 for (; c != 0; c--, dst++) \
223 *dst = cpu_to_le##size(__scc_pciex_in##name(bus->phb, p)); \
224 scc_pciex_io_flush(bus); \
226 static void scc_pciex_out##name(u##size val, unsigned long port) \
228 struct iowa_bus *bus = iowa_pio_find_bus(port); \
229 __scc_pciex_out##name(bus->phb, val, port); \
231 static void scc_pciex_outs##name(unsigned long p, const void *b, \
234 struct iowa_bus *bus = iowa_pio_find_bus(p); \
235 const __le##size *src = b; \
236 for (; c != 0; c--, src++) \
237 __scc_pciex_out##name(bus->phb, le##size##_to_cpu(*src), p); \
240 #define cpu_to_le8(x) (x)
241 #define le8_to_cpu(x) (x)
244 PCIEX_PIO_FUNC(32,
l)
246 static
struct ppc_pci_io scc_pciex_ops = {
247 .readb = scc_pciex_readb,
248 .readw = scc_pciex_readw,
249 .readl = scc_pciex_readl,
250 .readq = scc_pciex_readq,
251 .readw_be = scc_pciex_readw_be,
252 .readl_be = scc_pciex_readl_be,
253 .readq_be = scc_pciex_readq_be,
254 .readsb = scc_pciex_readsb,
255 .readsw = scc_pciex_readsw,
256 .readsl = scc_pciex_readsl,
257 .memcpy_fromio = scc_pciex_memcpy_fromio,
258 .inb = scc_pciex_inb,
259 .inw = scc_pciex_inw,
260 .inl = scc_pciex_inl,
261 .outb = scc_pciex_outb,
262 .outw = scc_pciex_outw,
263 .outl = scc_pciex_outl,
264 .insb = scc_pciex_insb,
265 .insw = scc_pciex_insw,
266 .insl = scc_pciex_insl,
267 .outsb = scc_pciex_outsb,
268 .outsw = scc_pciex_outsw,
269 .outsl = scc_pciex_outsl,
278 if (!dummy_page_va) {
279 pr_err(
"PCIEX:Alloc dummy_page_va failed\n");
286 pr_err(
"PCIEX:Map dummy page failed.\n");
287 kfree(dummy_page_va);
299 #define MK_PEXDADRS(bus_no, dev_no, func_no, addr) \
300 ((uint32_t)(((addr) & ~0x3UL) | \
301 ((bus_no) << PEXDADRS_BUSNO_SHIFT) | \
302 ((dev_no) << PEXDADRS_DEVNO_SHIFT) | \
303 ((func_no) << PEXDADRS_FUNCNO_SHIFT)))
305 #define MK_PEXDCMND_BYTE_EN(addr, size) \
306 ((((0x1 << (size))-1) << ((addr) & 0x3)) << PEXDCMND_BYTE_EN_SHIFT)
307 #define MK_PEXDCMND(cmd, addr, size) ((cmd) | MK_PEXDCMND_BYTE_EN(addr, size))
321 >> ((off & (4-
size)) * 8)) & ((0x1 << (size * 8)) - 1);
325 static void config_write_pciex_dev(
unsigned int __iomem *base,
uint64_t bus_no,
336 (data & ((0x1 << (size * 8)) - 1)) << ((off & (4-size)) * 8));
339 #define MK_PEXCADRS_BYTE_EN(off, len) \
340 ((((0x1 << (len)) - 1) << ((off) & 0x3)) << PEXCADRS_BYTE_EN_SHIFT)
341 #define MK_PEXCADRS(cmd, addr, size) \
342 ((cmd) | MK_PEXCADRS_BYTE_EN(addr, size) | ((addr) & ~0x3))
348 >> ((where & (4 - size)) * 8)) & ((0x1 << (size * 8)) - 1);
351 static void config_write_pciex_rc(
unsigned int __iomem *base,
uint32_t where,
356 data = (val & ((0x1 << (size * 8)) - 1)) << ((where & (4 -
size)) * 8);
367 static int scc_pciex_read_config(
struct pci_bus *bus,
unsigned int devfn,
368 int where,
int size,
unsigned int *val)
378 *val = config_read_pciex_rc(phb->cfg_addr, where, size);
380 *val = config_read_pciex_dev(phb->cfg_addr, bus->
number,
386 static int scc_pciex_write_config(
struct pci_bus *bus,
unsigned int devfn,
387 int where,
int size,
unsigned int val)
395 config_write_pciex_rc(phb->cfg_addr, where, size, val);
397 config_write_pciex_dev(phb->cfg_addr, bus->
number,
402 static struct pci_ops scc_pciex_pci_ops = {
403 scc_pciex_read_config,
404 scc_pciex_write_config,
407 static void pciex_clear_intr_all(
unsigned int __iomem *base)
415 static void pciex_disable_intr_all(
unsigned int *base)
424 static void pciex_enable_intr_all(
unsigned int __iomem *base)
432 static void pciex_check_status(
unsigned int __iomem *base)
435 uint32_t intsts, aerr, prerr, rcvcp, lenerr;
444 if (intsts || aerr || prerr || rcvcp || lenerr)
447 pr_info(
"PCEXC interrupt!!\n");
448 pr_info(
"PEXINTSTS :0x%08x\n", intsts);
449 pr_info(
"PEXAERRSTS :0x%08x\n", aerr);
450 pr_info(
"PEXPRERRSTS :0x%08x\n", prerr);
451 pr_info(
"PEXRCVCPLIDA :0x%08x\n", rcvcp);
452 pr_info(
"PEXLENERRIDA :0x%08x\n", lenerr);
455 if (intsts & 0x00004000) {
457 for (i = 0; i < 4; i++) {
462 pr_info(
"PEXMAEC%d :0x%08x\n", i, maec);
463 pr_info(
"PEXMAEA%d :0x%08x\n", i, maea);
469 pciex_clear_intr_all(base);
476 pr_debug(
"PCIEX:pciex_handle_internal_irq(irq=%d)\n", irq);
480 pciex_check_status(phb->cfg_addr);
494 pr_err(
"PCIEXC:Failed to get config resource.\n");
497 phb->cfg_addr =
ioremap(
r.start, resource_size(&
r));
498 if (!phb->cfg_addr) {
499 pr_err(
"PCIEXC:Failed to remap SMMIO region.\n");
504 phb->cfg_data =
NULL;
507 phb->
ops = &scc_pciex_pci_ops;
511 pr_err(
"PCIEXC:Failed to map irq\n");
517 0,
"pciex", (
void *)phb)) {
518 pr_err(
"PCIEXC:Failed to request irq\n");
523 pciex_clear_intr_all(phb->cfg_addr);
524 pciex_enable_intr_all(phb->cfg_addr);
530 phb->cfg_data =
NULL;
533 phb->cfg_addr =
NULL;
538 .setup = celleb_setup_pciex,
539 .ops = &scc_pciex_ops,
540 .iowa_init = &scc_pciex_iowa_init,