2 * fuloong2e specific PCI support.
4 * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
6 * Authors: Carsten Langgaard <carstenl@mips.com>
7 * Maciej W. Rozycki <macro@mips.com>
9 * Copyright (C) 2009 Lemote Inc.
10 * Author: Wu Zhangjin <wuzj@lemote.com>
12 * This program is free software; you can distribute it and/or modify it
13 * under the terms of the GNU General Public License (Version 2) as
14 * published by the Free Software Foundation.
16 #include <linux/types.h>
17 #include <linux/pci.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
23 #define PCI_ACCESS_READ 0
24 #define PCI_ACCESS_WRITE 1
26 #define CFG_SPACE_REG(offset) \
27 (void *)CKSEG1ADDR(LOONGSON_PCICFG_BASE | (offset))
28 #define ID_SEL_BEGIN 11
29 #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
32 static int loongson_pcibios_config_access(unsigned char access_type,
34 unsigned int devfn, int where,
37 u32 busnum = bus->number;
41 int device = PCI_SLOT(devfn);
42 int function = PCI_FUNC(devfn);
46 /* Type 0 configuration for onboard PCI bus */
47 if (device > MAX_DEV_NUM)
50 addr = (1 << (device + ID_SEL_BEGIN)) | (function << 8) | reg;
53 /* Type 1 configuration for offboard PCI bus */
54 addr = (busnum << 16) | (device << 11) | (function << 8) | reg;
59 LOONGSON_PCICMD |= LOONGSON_PCICMD_MABORT_CLR | \
60 LOONGSON_PCICMD_MTABORT_CLR;
62 LOONGSON_PCIMAP_CFG = (addr >> 16) | type;
64 /* Flush Bonito register block */
65 dummy = LOONGSON_PCIMAP_CFG;
68 addrp = CFG_SPACE_REG(addr & 0xffff);
69 if (access_type == PCI_ACCESS_WRITE)
70 writel(cpu_to_le32(*data), addrp);
72 *data = le32_to_cpu(readl(addrp));
74 /* Detect Master/Target abort */
75 if (LOONGSON_PCICMD & (LOONGSON_PCICMD_MABORT_CLR |
76 LOONGSON_PCICMD_MTABORT_CLR)) {
80 LOONGSON_PCICMD |= (LOONGSON_PCICMD_MABORT_CLR |
81 LOONGSON_PCICMD_MTABORT_CLR);
92 * We can't address 8 and 16 bit words directly. Instead we have to
93 * read/write a 32bit word and mask/modify the data we actually want.
95 static int loongson_pcibios_read(struct pci_bus *bus, unsigned int devfn,
96 int where, int size, u32 *val)
100 if ((size == 2) && (where & 1))
101 return PCIBIOS_BAD_REGISTER_NUMBER;
102 else if ((size == 4) && (where & 3))
103 return PCIBIOS_BAD_REGISTER_NUMBER;
105 if (loongson_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
110 *val = (data >> ((where & 3) << 3)) & 0xff;
112 *val = (data >> ((where & 3) << 3)) & 0xffff;
116 return PCIBIOS_SUCCESSFUL;
119 static int loongson_pcibios_write(struct pci_bus *bus, unsigned int devfn,
120 int where, int size, u32 val)
124 if ((size == 2) && (where & 1))
125 return PCIBIOS_BAD_REGISTER_NUMBER;
126 else if ((size == 4) && (where & 3))
127 return PCIBIOS_BAD_REGISTER_NUMBER;
132 if (loongson_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
137 data = (data & ~(0xff << ((where & 3) << 3))) |
138 (val << ((where & 3) << 3));
140 data = (data & ~(0xffff << ((where & 3) << 3))) |
141 (val << ((where & 3) << 3));
144 if (loongson_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
148 return PCIBIOS_SUCCESSFUL;
151 struct pci_ops loongson_pci_ops = {
152 .read = loongson_pcibios_read,
153 .write = loongson_pcibios_write