ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / syslib / indirect_pci.c
1 /*
2  * Support for indirect PCI bridges.
3  *
4  * Copyright (C) 1998 Gabriel Paubert.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/init.h>
17 #include <linux/bootmem.h>
18
19 #include <asm/io.h>
20 #include <asm/prom.h>
21 #include <asm/pci-bridge.h>
22 #include <asm/machdep.h>
23
24 #ifdef CONFIG_PPC_INDIRECT_PCI_BE
25 #define PCI_CFG_OUT out_be32
26 #else
27 #define PCI_CFG_OUT out_le32
28 #endif
29
30 static int
31 indirect_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
32                      int len, u32 *val)
33 {
34         struct pci_controller *hose = bus->sysdata;
35         volatile unsigned char *cfg_data;
36         u8 cfg_type = 0;
37
38         if (ppc_md.pci_exclude_device)
39                 if (ppc_md.pci_exclude_device(bus->number, devfn))
40                         return PCIBIOS_DEVICE_NOT_FOUND;
41         
42         if (hose->set_cfg_type)
43                 if (bus->number != hose->first_busno)
44                         cfg_type = 1;
45
46         PCI_CFG_OUT(hose->cfg_addr,                                      
47                  (0x80000000 | ((bus->number - hose->bus_offset) << 16)
48                   | (devfn << 8) | ((offset & 0xfc) | cfg_type)));
49
50         /*
51          * Note: the caller has already checked that offset is
52          * suitably aligned and that len is 1, 2 or 4.
53          */
54         cfg_data = hose->cfg_data + (offset & 3);
55         switch (len) {
56         case 1:
57                 *val = in_8((u8 *)cfg_data);
58                 break;
59         case 2:
60                 *val = in_le16((u16 *)cfg_data);
61                 break;
62         default:
63                 *val = in_le32((u32 *)cfg_data);
64                 break;
65         }
66         return PCIBIOS_SUCCESSFUL;
67 }
68
69 static int
70 indirect_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
71                       int len, u32 val)
72 {
73         struct pci_controller *hose = bus->sysdata;
74         volatile unsigned char *cfg_data;
75         u8 cfg_type = 0;
76
77         if (ppc_md.pci_exclude_device)
78                 if (ppc_md.pci_exclude_device(bus->number, devfn))
79                         return PCIBIOS_DEVICE_NOT_FOUND;
80
81         if (hose->set_cfg_type)
82                 if (bus->number != hose->first_busno)
83                         cfg_type = 1;
84
85         PCI_CFG_OUT(hose->cfg_addr,                                      
86                  (0x80000000 | ((bus->number - hose->bus_offset) << 16)
87                   | (devfn << 8) | ((offset & 0xfc) | cfg_type)));
88
89         /*
90          * Note: the caller has already checked that offset is
91          * suitably aligned and that len is 1, 2 or 4.
92          */
93         cfg_data = hose->cfg_data + (offset & 3);
94         switch (len) {
95         case 1:
96                 out_8((u8 *)cfg_data, val);
97                 break;
98         case 2:
99                 out_le16((u16 *)cfg_data, val);
100                 break;
101         default:
102                 out_le32((u32 *)cfg_data, val);
103                 break;
104         }
105         return PCIBIOS_SUCCESSFUL;
106 }
107
108 static struct pci_ops indirect_pci_ops =
109 {
110         indirect_read_config,
111         indirect_write_config
112 };
113
114 void __init
115 setup_indirect_pci(struct pci_controller* hose, u32 cfg_addr, u32 cfg_data)
116 {
117         unsigned long base = cfg_addr & PAGE_MASK;
118         char *mbase;
119
120         mbase = ioremap(base, PAGE_SIZE);
121         hose->cfg_addr = (unsigned int *)(mbase + (cfg_addr & ~PAGE_MASK));
122         if ((cfg_data & PAGE_MASK) != base)
123                 mbase = ioremap(cfg_data & PAGE_MASK, PAGE_SIZE);
124         hose->cfg_data = (unsigned char *)(mbase + (cfg_data & ~PAGE_MASK));
125         hose->ops = &indirect_pci_ops;
126 }