RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / powerpc / platforms / celleb / scc_epci.c
blobc4b011094bd678a64b92a00ff70cc277e5cda5cb
1 /*
2 * Support for SCC external PCI
4 * (C) Copyright 2004-2007 TOSHIBA CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #undef DEBUG
23 #include <linux/kernel.h>
24 #include <linux/threads.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/pci_regs.h>
28 #include <linux/bootmem.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/prom.h>
33 #include <asm/machdep.h>
34 #include <asm/pci-bridge.h>
35 #include <asm/ppc-pci.h>
37 #include "scc.h"
38 #include "pci.h"
39 #include "interrupt.h"
41 #define MAX_PCI_DEVICES 32
42 #define MAX_PCI_FUNCTIONS 8
44 #define iob() __asm__ __volatile__("eieio; sync":::"memory")
46 static inline volatile void __iomem *celleb_epci_get_epci_base(
47 struct pci_controller *hose)
50 * Note:
51 * Celleb epci uses cfg_addr as a base address for
52 * epci control registers.
55 return hose->cfg_addr;
58 static inline volatile void __iomem *celleb_epci_get_epci_cfg(
59 struct pci_controller *hose)
62 * Note:
63 * Celleb epci uses cfg_data as a base address for
64 * configuration area for epci devices.
67 return hose->cfg_data;
70 #if 0 /* test code for epci dummy read */
71 static void celleb_epci_dummy_read(struct pci_dev *dev)
73 volatile void __iomem *epci_base;
74 struct device_node *node;
75 struct pci_controller *hose;
76 u32 val;
78 node = (struct device_node *)dev->bus->sysdata;
79 hose = pci_find_hose_for_OF_device(node);
81 if (!hose)
82 return;
84 epci_base = celleb_epci_get_epci_base(hose);
86 val = in_be32(epci_base + SCC_EPCI_WATRP);
87 iosync();
89 return;
91 #endif
93 static inline void clear_and_disable_master_abort_interrupt(
94 struct pci_controller *hose)
96 volatile void __iomem *epci_base, *reg;
97 epci_base = celleb_epci_get_epci_base(hose);
98 reg = epci_base + PCI_COMMAND;
99 out_be32(reg, in_be32(reg) | (PCI_STATUS_REC_MASTER_ABORT << 16));
102 static int celleb_epci_check_abort(struct pci_controller *hose,
103 volatile void __iomem *addr)
105 volatile void __iomem *reg, *epci_base;
106 u32 val;
108 iob();
109 epci_base = celleb_epci_get_epci_base(hose);
111 reg = epci_base + PCI_COMMAND;
112 val = in_be32(reg);
114 if (val & (PCI_STATUS_REC_MASTER_ABORT << 16)) {
115 out_be32(reg,
116 (val & 0xffff) | (PCI_STATUS_REC_MASTER_ABORT << 16));
118 /* clear PCI Controller error, FRE, PMFE */
119 reg = epci_base + SCC_EPCI_STATUS;
120 out_be32(reg, SCC_EPCI_INT_PAI);
122 reg = epci_base + SCC_EPCI_VCSR;
123 val = in_be32(reg) & 0xffff;
124 val |= SCC_EPCI_VCSR_FRE;
125 out_be32(reg, val);
127 reg = epci_base + SCC_EPCI_VISTAT;
128 out_be32(reg, SCC_EPCI_VISTAT_PMFE);
129 return PCIBIOS_DEVICE_NOT_FOUND;
132 return PCIBIOS_SUCCESSFUL;
135 static volatile void __iomem *celleb_epci_make_config_addr(
136 struct pci_bus *bus,
137 struct pci_controller *hose,
138 unsigned int devfn, int where)
140 volatile void __iomem *addr;
142 if (bus != hose->bus)
143 addr = celleb_epci_get_epci_cfg(hose) +
144 (((bus->number & 0xff) << 16)
145 | ((devfn & 0xff) << 8)
146 | (where & 0xff)
147 | 0x01000000);
148 else
149 addr = celleb_epci_get_epci_cfg(hose) +
150 (((devfn & 0xff) << 8) | (where & 0xff));
152 pr_debug("EPCI: config_addr = 0x%p\n", addr);
154 return addr;
157 static int celleb_epci_read_config(struct pci_bus *bus,
158 unsigned int devfn, int where, int size, u32 * val)
160 volatile void __iomem *epci_base, *addr;
161 struct device_node *node;
162 struct pci_controller *hose;
164 /* allignment check */
165 BUG_ON(where % size);
167 node = (struct device_node *)bus->sysdata;
168 hose = pci_find_hose_for_OF_device(node);
170 if (!celleb_epci_get_epci_cfg(hose))
171 return PCIBIOS_DEVICE_NOT_FOUND;
173 if (bus->number == hose->first_busno && devfn == 0) {
174 /* EPCI controller self */
176 epci_base = celleb_epci_get_epci_base(hose);
177 addr = epci_base + where;
179 switch (size) {
180 case 1:
181 *val = in_8(addr);
182 break;
183 case 2:
184 *val = in_be16(addr);
185 break;
186 case 4:
187 *val = in_be32(addr);
188 break;
189 default:
190 return PCIBIOS_DEVICE_NOT_FOUND;
193 } else {
195 clear_and_disable_master_abort_interrupt(hose);
196 addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
198 switch (size) {
199 case 1:
200 *val = in_8(addr);
201 break;
202 case 2:
203 *val = in_le16(addr);
204 break;
205 case 4:
206 *val = in_le32(addr);
207 break;
208 default:
209 return PCIBIOS_DEVICE_NOT_FOUND;
213 pr_debug("EPCI: "
214 "addr=0x%p, devfn=0x%x, where=0x%x, size=0x%x, val=0x%x\n",
215 addr, devfn, where, size, *val);
217 return celleb_epci_check_abort(hose, NULL);
220 static int celleb_epci_write_config(struct pci_bus *bus,
221 unsigned int devfn, int where, int size, u32 val)
223 volatile void __iomem *epci_base, *addr;
224 struct device_node *node;
225 struct pci_controller *hose;
227 /* allignment check */
228 BUG_ON(where % size);
230 node = (struct device_node *)bus->sysdata;
231 hose = pci_find_hose_for_OF_device(node);
234 if (!celleb_epci_get_epci_cfg(hose))
235 return PCIBIOS_DEVICE_NOT_FOUND;
237 if (bus->number == hose->first_busno && devfn == 0) {
238 /* EPCI controller self */
240 epci_base = celleb_epci_get_epci_base(hose);
241 addr = epci_base + where;
243 switch (size) {
244 case 1:
245 out_8(addr, val);
246 break;
247 case 2:
248 out_be16(addr, val);
249 break;
250 case 4:
251 out_be32(addr, val);
252 break;
253 default:
254 return PCIBIOS_DEVICE_NOT_FOUND;
257 } else {
259 clear_and_disable_master_abort_interrupt(hose);
260 addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
262 switch (size) {
263 case 1:
264 out_8(addr, val);
265 break;
266 case 2:
267 out_le16(addr, val);
268 break;
269 case 4:
270 out_le32(addr, val);
271 break;
272 default:
273 return PCIBIOS_DEVICE_NOT_FOUND;
277 return celleb_epci_check_abort(hose, addr);
280 struct pci_ops celleb_epci_ops = {
281 celleb_epci_read_config,
282 celleb_epci_write_config,
285 /* to be moved in FW */
286 static int __devinit celleb_epci_init(struct pci_controller *hose)
288 u32 val;
289 volatile void __iomem *reg, *epci_base;
290 int hwres = 0;
292 epci_base = celleb_epci_get_epci_base(hose);
294 /* PCI core reset(Internal bus and PCI clock) */
295 reg = epci_base + SCC_EPCI_CKCTRL;
296 val = in_be32(reg);
297 if (val == 0x00030101)
298 hwres = 1;
299 else {
300 val &= ~(SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
301 out_be32(reg, val);
303 /* set PCI core clock */
304 val = in_be32(reg);
305 val |= (SCC_EPCI_CKCTRL_OCLKEN | SCC_EPCI_CKCTRL_LCLKEN);
306 out_be32(reg, val);
308 /* release PCI core reset (internal bus) */
309 val = in_be32(reg);
310 val |= SCC_EPCI_CKCTRL_CRST0;
311 out_be32(reg, val);
313 /* set PCI clock select */
314 reg = epci_base + SCC_EPCI_CLKRST;
315 val = in_be32(reg);
316 val &= ~SCC_EPCI_CLKRST_CKS_MASK;
317 val |= SCC_EPCI_CLKRST_CKS_2;
318 out_be32(reg, val);
320 /* set arbiter */
321 reg = epci_base + SCC_EPCI_ABTSET;
322 out_be32(reg, 0x0f1f001f); /* temporary value */
324 /* buffer on */
325 reg = epci_base + SCC_EPCI_CLKRST;
326 val = in_be32(reg);
327 val |= SCC_EPCI_CLKRST_BC;
328 out_be32(reg, val);
330 /* PCI clock enable */
331 val = in_be32(reg);
332 val |= SCC_EPCI_CLKRST_PCKEN;
333 out_be32(reg, val);
335 /* release PCI core reset (all) */
336 reg = epci_base + SCC_EPCI_CKCTRL;
337 val = in_be32(reg);
338 val |= (SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
339 out_be32(reg, val);
341 /* set base translation registers. (already set by Beat) */
343 /* set base address masks. (already set by Beat) */
346 /* release interrupt masks and clear all interrupts */
347 reg = epci_base + SCC_EPCI_INTSET;
348 out_be32(reg, 0x013f011f); /* all interrupts enable */
349 reg = epci_base + SCC_EPCI_VIENAB;
350 val = SCC_EPCI_VIENAB_PMPEE | SCC_EPCI_VIENAB_PMFEE;
351 out_be32(reg, val);
352 reg = epci_base + SCC_EPCI_STATUS;
353 out_be32(reg, 0xffffffff);
354 reg = epci_base + SCC_EPCI_VISTAT;
355 out_be32(reg, 0xffffffff);
357 /* disable PCI->IB address translation */
358 reg = epci_base + SCC_EPCI_VCSR;
359 val = in_be32(reg);
360 val &= ~(SCC_EPCI_VCSR_DR | SCC_EPCI_VCSR_AT);
361 out_be32(reg, val);
363 /* set base addresses. (no need to set?) */
365 /* memory space, bus master enable */
366 reg = epci_base + PCI_COMMAND;
367 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
368 out_be32(reg, val);
370 /* endian mode setup */
371 reg = epci_base + SCC_EPCI_ECMODE;
372 val = 0x00550155;
373 out_be32(reg, val);
375 /* set control option */
376 reg = epci_base + SCC_EPCI_CNTOPT;
377 val = in_be32(reg);
378 val |= SCC_EPCI_CNTOPT_O2PMB;
379 out_be32(reg, val);
381 /* XXX: temporay: set registers for address conversion setup */
382 reg = epci_base + SCC_EPCI_CNF10_REG;
383 out_be32(reg, 0x80000008);
384 reg = epci_base + SCC_EPCI_CNF14_REG;
385 out_be32(reg, 0x40000008);
387 reg = epci_base + SCC_EPCI_BAM0;
388 out_be32(reg, 0x80000000);
389 reg = epci_base + SCC_EPCI_BAM1;
390 out_be32(reg, 0xe0000000);
392 reg = epci_base + SCC_EPCI_PVBAT;
393 out_be32(reg, 0x80000000);
395 if (!hwres) {
396 /* release external PCI reset */
397 reg = epci_base + SCC_EPCI_CLKRST;
398 val = in_be32(reg);
399 val |= SCC_EPCI_CLKRST_PCIRST;
400 out_be32(reg, val);
403 return 0;
406 int __devinit celleb_setup_epci(struct device_node *node,
407 struct pci_controller *hose)
409 struct resource r;
411 pr_debug("PCI: celleb_setup_epci()\n");
414 * Note:
415 * Celleb epci uses cfg_addr and cfg_data member of
416 * pci_controller structure in irregular way.
418 * cfg_addr is used to map for control registers of
419 * celleb epci.
421 * cfg_data is used for configuration area of devices
422 * on Celleb epci buses.
425 if (of_address_to_resource(node, 0, &r))
426 goto error;
427 hose->cfg_addr = ioremap(r.start, (r.end - r.start + 1));
428 if (!hose->cfg_addr)
429 goto error;
430 pr_debug("EPCI: cfg_addr map 0x%016lx->0x%016lx + 0x%016lx\n",
431 r.start, (unsigned long)hose->cfg_addr,
432 (r.end - r.start + 1));
434 if (of_address_to_resource(node, 2, &r))
435 goto error;
436 hose->cfg_data = ioremap(r.start, (r.end - r.start + 1));
437 if (!hose->cfg_data)
438 goto error;
439 pr_debug("EPCI: cfg_data map 0x%016lx->0x%016lx + 0x%016lx\n",
440 r.start, (unsigned long)hose->cfg_data,
441 (r.end - r.start + 1));
443 celleb_epci_init(hose);
445 return 0;
447 error:
448 return 1;