blob: a450c4062031cc2e002e96a0e20a41eece0e3adf [file] [log] [blame]
Ralf Baechle23fbee92005-07-25 22:45:45 +00001/*
2 * Define the pci_ops for the Toshiba rbtx4938
3 * Copyright (C) 2000-2001 Toshiba Corporation
4 *
5 * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the
6 * terms of the GNU General Public License version 2. This program is
7 * licensed "as is" without any warranty of any kind, whether express
8 * or implied.
9 *
10 * Support for TX4938 in 2.6 - Manish Lachwani (mlachwani@mvista.com)
11 */
12#include <linux/types.h>
13#include <linux/pci.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16
17#include <asm/addrspace.h>
18#include <asm/tx4938/rbtx4938.h>
19
20/* initialize in setup */
21struct resource pci_io_resource = {
22 .name = "pci IO space",
23 .start = 0,
24 .end = 0,
25 .flags = IORESOURCE_IO
26};
27
28/* initialize in setup */
29struct resource pci_mem_resource = {
30 .name = "pci memory space",
31 .start = 0,
32 .end = 0,
33 .flags = IORESOURCE_MEM
34};
35
36struct resource tx4938_pcic1_pci_io_resource = {
Ralf Baechlea3dddd52006-03-11 08:18:41 +000037 .name = "PCI1 IO",
38 .start = 0,
39 .end = 0,
40 .flags = IORESOURCE_IO
Ralf Baechle23fbee92005-07-25 22:45:45 +000041};
42struct resource tx4938_pcic1_pci_mem_resource = {
Ralf Baechlea3dddd52006-03-11 08:18:41 +000043 .name = "PCI1 mem",
44 .start = 0,
45 .end = 0,
46 .flags = IORESOURCE_MEM
Ralf Baechle23fbee92005-07-25 22:45:45 +000047};
48
Atsushi Nemoto2db30152007-07-02 22:43:06 +090049static int mkaddr(int bus, int dev_fn, int where,
50 struct tx4938_pcic_reg *pcicptr)
Ralf Baechle23fbee92005-07-25 22:45:45 +000051{
52 if (bus > 0) {
53 /* Type 1 configuration */
Atsushi Nemoto2db30152007-07-02 22:43:06 +090054 pcicptr->g2pcfgadrs = ((bus & 0xff) << 0x10) |
Ralf Baechle23fbee92005-07-25 22:45:45 +000055 ((dev_fn & 0xff) << 0x08) | (where & 0xfc) | 1;
56 } else {
57 if (dev_fn >= PCI_DEVFN(TX4938_PCIC_MAX_DEVNU, 0))
58 return -1;
59
60 /* Type 0 configuration */
Atsushi Nemoto2db30152007-07-02 22:43:06 +090061 pcicptr->g2pcfgadrs = ((bus & 0xff) << 0x10) |
Ralf Baechle23fbee92005-07-25 22:45:45 +000062 ((dev_fn & 0xff) << 0x08) | (where & 0xfc);
63 }
64 /* clear M_ABORT and Disable M_ABORT Int. */
Atsushi Nemoto2db30152007-07-02 22:43:06 +090065 pcicptr->pcistatus =
66 (pcicptr->pcistatus & 0x0000ffff) |
Ralf Baechle23fbee92005-07-25 22:45:45 +000067 (PCI_STATUS_REC_MASTER_ABORT << 16);
Atsushi Nemoto2db30152007-07-02 22:43:06 +090068 pcicptr->pcimask &= ~PCI_STATUS_REC_MASTER_ABORT;
Ralf Baechle23fbee92005-07-25 22:45:45 +000069
70 return 0;
71}
72
Atsushi Nemoto2db30152007-07-02 22:43:06 +090073static int check_abort(struct tx4938_pcic_reg *pcicptr)
Ralf Baechle23fbee92005-07-25 22:45:45 +000074{
75 int code = PCIBIOS_SUCCESSFUL;
76 /* wait write cycle completion before checking error status */
Atsushi Nemoto2db30152007-07-02 22:43:06 +090077 while (pcicptr->pcicstatus & TX4938_PCIC_PCICSTATUS_IWB)
Ralf Baechle23fbee92005-07-25 22:45:45 +000078 ;
Atsushi Nemoto2db30152007-07-02 22:43:06 +090079 if (pcicptr->pcistatus & (PCI_STATUS_REC_MASTER_ABORT << 16)) {
80 pcicptr->pcistatus =
81 (pcicptr->
Ralf Baechle23fbee92005-07-25 22:45:45 +000082 pcistatus & 0x0000ffff) | (PCI_STATUS_REC_MASTER_ABORT
83 << 16);
Atsushi Nemoto2db30152007-07-02 22:43:06 +090084 pcicptr->pcimask |= PCI_STATUS_REC_MASTER_ABORT;
Ralf Baechle23fbee92005-07-25 22:45:45 +000085 code = PCIBIOS_DEVICE_NOT_FOUND;
86 }
87 return code;
88}
89
Atsushi Nemoto2db30152007-07-02 22:43:06 +090090extern struct pci_controller tx4938_pci_controller[];
91extern struct tx4938_pcic_reg *get_tx4938_pcicptr(int ch);
92
93static struct tx4938_pcic_reg *pci_bus_to_pcicptr(struct pci_bus *bus)
94{
95 struct pci_controller *channel = bus->sysdata;
96 return get_tx4938_pcicptr(channel - &tx4938_pci_controller[0]);
97}
98
Ralf Baechle23fbee92005-07-25 22:45:45 +000099static int tx4938_pcibios_read_config(struct pci_bus *bus, unsigned int devfn,
100 int where, int size, u32 * val)
101{
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900102 int retval, dev, busno, func;
103 struct tx4938_pcic_reg *pcicptr = pci_bus_to_pcicptr(bus);
104 void __iomem *cfgdata =
105 (void __iomem *)(unsigned long)&pcicptr->g2pcfgdata;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000106
107 dev = PCI_SLOT(devfn);
108 func = PCI_FUNC(devfn);
109
110 /* check if the bus is top-level */
111 if (bus->parent != NULL)
112 busno = bus->number;
113 else {
114 busno = 0;
115 }
116
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900117 if (mkaddr(busno, devfn, where, pcicptr))
Ralf Baechle23fbee92005-07-25 22:45:45 +0000118 return -1;
119
120 switch (size) {
121 case 1:
Ralf Baechle23fbee92005-07-25 22:45:45 +0000122#ifdef __BIG_ENDIAN
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900123 cfgdata += (where & 3) ^ 3;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000124#else
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900125 cfgdata += where & 3;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000126#endif
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900127 *val = __raw_readb(cfgdata);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000128 break;
129 case 2:
Ralf Baechle23fbee92005-07-25 22:45:45 +0000130#ifdef __BIG_ENDIAN
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900131 cfgdata += (where & 2) ^ 2;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000132#else
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900133 cfgdata += where & 2;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000134#endif
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900135 *val = __raw_readw(cfgdata);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000136 break;
137 case 4:
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900138 *val = __raw_readl(cfgdata);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000139 break;
140 }
141
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900142 retval = check_abort(pcicptr);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000143 if (retval == PCIBIOS_DEVICE_NOT_FOUND)
144 *val = 0xffffffff;
145
146 return retval;
147}
148
149static int tx4938_pcibios_write_config(struct pci_bus *bus, unsigned int devfn, int where,
150 int size, u32 val)
151{
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900152 int dev, busno, func;
153 struct tx4938_pcic_reg *pcicptr = pci_bus_to_pcicptr(bus);
154 void __iomem *cfgdata =
155 (void __iomem *)(unsigned long)&pcicptr->g2pcfgdata;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000156
157 busno = bus->number;
158 dev = PCI_SLOT(devfn);
159 func = PCI_FUNC(devfn);
160
161 /* check if the bus is top-level */
162 if (bus->parent != NULL) {
163 busno = bus->number;
164 } else {
165 busno = 0;
166 }
167
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900168 if (mkaddr(busno, devfn, where, pcicptr))
Ralf Baechle23fbee92005-07-25 22:45:45 +0000169 return -1;
170
171 switch (size) {
172 case 1:
Ralf Baechle23fbee92005-07-25 22:45:45 +0000173#ifdef __BIG_ENDIAN
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900174 cfgdata += (where & 3) ^ 3;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000175#else
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900176 cfgdata += where & 3;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000177#endif
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900178 __raw_writeb(val, cfgdata);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000179 break;
180 case 2:
Ralf Baechle23fbee92005-07-25 22:45:45 +0000181#ifdef __BIG_ENDIAN
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900182 cfgdata += (where & 2) ^ 2;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000183#else
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900184 cfgdata += where & 2;
Ralf Baechle23fbee92005-07-25 22:45:45 +0000185#endif
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900186 __raw_writew(val, cfgdata);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000187 break;
188 case 4:
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900189 __raw_writel(val, cfgdata);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000190 break;
191 }
192
Atsushi Nemoto2db30152007-07-02 22:43:06 +0900193 return check_abort(pcicptr);
Ralf Baechle23fbee92005-07-25 22:45:45 +0000194}
195
196struct pci_ops tx4938_pci_ops = {
197 tx4938_pcibios_read_config,
198 tx4938_pcibios_write_config
199};
200
201struct pci_controller tx4938_pci_controller[] = {
202 /* h/w only supports devices 0x00 to 0x14 */
203 {
204 .pci_ops = &tx4938_pci_ops,
205 .io_resource = &pci_io_resource,
206 .mem_resource = &pci_mem_resource,
207 },
208 /* h/w only supports devices 0x00 to 0x14 */
209 {
210 .pci_ops = &tx4938_pci_ops,
211 .io_resource = &tx4938_pcic1_pci_io_resource,
212 .mem_resource = &tx4938_pcic1_pci_mem_resource,
213 }
214};