1 /*
2 * Copyright (C) NEC Electronics Corporation 2004-2006
3 *
4 * This file is based on the arch/mips/pci/ops-vr41xx.c
5 *
6 * Copyright 2001 MontaVista Software Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include <linux/pci.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26
27 #include <asm/addrspace.h>
28
29 #include <asm/emma/emma2rh.h>
30
31 #define RTABORT (0x1<<9)
32 #define RMABORT (0x1<<10)
33 #define EMMA2RH_PCI_SLOT_NUM 9 /* 0000:09.0 is final PCI device */
34
35 /*
36 * access config space
37 */
38
check_args(struct pci_bus * bus,u32 devfn,u32 * bus_num)39 static int check_args(struct pci_bus *bus, u32 devfn, u32 * bus_num)
40 {
41 /* check if the bus is top-level */
42 if (bus->parent != NULL)
43 *bus_num = bus->number;
44 else
45 *bus_num = 0;
46
47 if (*bus_num == 0) {
48 /* Type 0 */
49 if (PCI_SLOT(devfn) >= 10)
50 return PCIBIOS_DEVICE_NOT_FOUND;
51 } else {
52 /* Type 1 */
53 if ((*bus_num >= 64) || (PCI_SLOT(devfn) >= 16))
54 return PCIBIOS_DEVICE_NOT_FOUND;
55 }
56 return 0;
57 }
58
set_pci_configuration_address(unsigned char bus_num,unsigned int devfn,int where)59 static inline int set_pci_configuration_address(unsigned char bus_num,
60 unsigned int devfn, int where)
61 {
62 u32 config_win0;
63
64 emma2rh_out32(EMMA2RH_PCI_INT, ~RMABORT);
65 if (bus_num == 0)
66 /*
67 * Type 0 configuration
68 */
69 config_win0 = (1 << (22 + PCI_SLOT(devfn))) | (5 << 9);
70 else
71 /*
72 * Type 1 configuration
73 */
74 config_win0 = (bus_num << 26) | (PCI_SLOT(devfn) << 22) |
75 (1 << 15) | (5 << 9);
76
77 emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, config_win0);
78
79 return 0;
80 }
81
pci_config_read(struct pci_bus * bus,unsigned int devfn,int where,int size,uint32_t * val)82 static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where,
83 int size, uint32_t * val)
84 {
85 u32 bus_num;
86 u32 base = KSEG1ADDR(EMMA2RH_PCI_CONFIG_BASE);
87 u32 backup_win0;
88 u32 data;
89
90 *val = 0xffffffffU;
91
92 if (check_args(bus, devfn, &bus_num) == PCIBIOS_DEVICE_NOT_FOUND)
93 return PCIBIOS_DEVICE_NOT_FOUND;
94
95 backup_win0 = emma2rh_in32(EMMA2RH_PCI_IWIN0_CTR);
96
97 if (set_pci_configuration_address(bus_num, devfn, where) < 0)
98 return PCIBIOS_DEVICE_NOT_FOUND;
99
100 data =
101 *(volatile u32 *)(base + (PCI_FUNC(devfn) << 8) +
102 (where & 0xfffffffc));
103
104 switch (size) {
105 case 1:
106 *val = (data >> ((where & 3) << 3)) & 0xffU;
107 break;
108 case 2:
109 *val = (data >> ((where & 2) << 3)) & 0xffffU;
110 break;
111 case 4:
112 *val = data;
113 break;
114 default:
115 emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
116 return PCIBIOS_FUNC_NOT_SUPPORTED;
117 }
118
119 emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
120
121 if (emma2rh_in32(EMMA2RH_PCI_INT) & RMABORT)
122 return PCIBIOS_DEVICE_NOT_FOUND;
123
124 return PCIBIOS_SUCCESSFUL;
125 }
126
pci_config_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)127 static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where,
128 int size, u32 val)
129 {
130 u32 bus_num;
131 u32 base = KSEG1ADDR(EMMA2RH_PCI_CONFIG_BASE);
132 u32 backup_win0;
133 u32 data;
134 int shift;
135
136 if (check_args(bus, devfn, &bus_num) == PCIBIOS_DEVICE_NOT_FOUND)
137 return PCIBIOS_DEVICE_NOT_FOUND;
138
139 backup_win0 = emma2rh_in32(EMMA2RH_PCI_IWIN0_CTR);
140
141 if (set_pci_configuration_address(bus_num, devfn, where) < 0)
142 return PCIBIOS_DEVICE_NOT_FOUND;
143
144 /* read modify write */
145 data =
146 *(volatile u32 *)(base + (PCI_FUNC(devfn) << 8) +
147 (where & 0xfffffffc));
148
149 switch (size) {
150 case 1:
151 shift = (where & 3) << 3;
152 data &= ~(0xffU << shift);
153 data |= ((val & 0xffU) << shift);
154 break;
155 case 2:
156 shift = (where & 2) << 3;
157 data &= ~(0xffffU << shift);
158 data |= ((val & 0xffffU) << shift);
159 break;
160 case 4:
161 data = val;
162 break;
163 default:
164 emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
165 return PCIBIOS_FUNC_NOT_SUPPORTED;
166 }
167 *(volatile u32 *)(base + (PCI_FUNC(devfn) << 8) +
168 (where & 0xfffffffc)) = data;
169
170 emma2rh_out32(EMMA2RH_PCI_IWIN0_CTR, backup_win0);
171 if (emma2rh_in32(EMMA2RH_PCI_INT) & RMABORT)
172 return PCIBIOS_DEVICE_NOT_FOUND;
173
174 return PCIBIOS_SUCCESSFUL;
175 }
176
177 struct pci_ops emma2rh_pci_ops = {
178 .read = pci_config_read,
179 .write = pci_config_write,
180 };
181