1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation; either version 2 of the License, or (at your
5 * option) any later version.
6 */
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/pci.h>
12
13 #include <asm/pci_channel.h>
14
15 void
pcibios_update_resource(struct pci_dev * dev,struct resource * root,struct resource * res,int resource)16 pcibios_update_resource(struct pci_dev *dev, struct resource *root,
17 struct resource *res, int resource)
18 {
19 u32 new, check;
20 int reg;
21
22 new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
23 if (resource < 6) {
24 reg = PCI_BASE_ADDRESS_0 + 4*resource;
25 } else if (resource == PCI_ROM_RESOURCE) {
26 res->flags |= PCI_ROM_ADDRESS_ENABLE;
27 new |= PCI_ROM_ADDRESS_ENABLE;
28 reg = dev->rom_base_reg;
29 } else {
30 /* Somebody might have asked allocation of a non-standard resource */
31 return;
32 }
33
34 pci_write_config_dword(dev, reg, new);
35 pci_read_config_dword(dev, reg, &check);
36 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
37 printk(KERN_ERR "PCI: Error while updating region "
38 "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
39 new, check);
40 }
41 }
42
43 /*
44 * We need to avoid collisions with `mirrored' VGA ports
45 * and other strange ISA hardware, so we always want the
46 * addresses to be allocated in the 0x000-0x0ff region
47 * modulo 0x400.
48 *
49 * Why? Because some silly external IO cards only decode
50 * the low 10 bits of the IO address. The 0x00-0xff region
51 * is reserved for motherboard devices that decode all 16
52 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
53 * but we want to try to avoid allocating at 0x2900-0x2bff
54 * which might have be mirrored at 0x0100-0x03ff..
55 */
56 void
pcibios_align_resource(void * data,struct resource * res,unsigned long size,unsigned long align)57 pcibios_align_resource(void *data, struct resource *res,
58 unsigned long size, unsigned long align)
59 {
60 if (res->flags & IORESOURCE_IO) {
61 unsigned long start = res->start;
62
63 if (start & 0x300) {
64 start = (start + 0x3ff) & ~0x3ff;
65 res->start = start;
66 }
67 }
68 }
69
70 /*
71 * If we set up a device for bus mastering, we need to check the latency
72 * timer as certain crappy BIOSes forget to set it properly.
73 */
74 unsigned int pcibios_max_latency = 255;
75
pcibios_set_master(struct pci_dev * dev)76 void pcibios_set_master(struct pci_dev *dev)
77 {
78 u8 lat;
79 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
80 if (lat < 16)
81 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
82 else if (lat > pcibios_max_latency)
83 lat = pcibios_max_latency;
84 else
85 return;
86 printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", dev->slot_name, lat);
87 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
88 }
89
pcibios_setup(char * str)90 char * __devinit pcibios_setup(char *str)
91 {
92 return str;
93 }
94
pcibios_enable_resources(struct pci_dev * dev,int mask)95 static int pcibios_enable_resources(struct pci_dev *dev, int mask)
96 {
97 u16 cmd, old_cmd;
98 int idx;
99 struct resource *r;
100
101 pci_read_config_word(dev, PCI_COMMAND, &cmd);
102 old_cmd = cmd;
103 for(idx=0; idx<6; idx++) {
104 /* Only set up the requested stuff */
105 if (!(mask & (1<<idx)))
106 continue;
107
108 r = &dev->resource[idx];
109 if (!r->start && r->end) {
110 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", dev->slot_name);
111 return -EINVAL;
112 }
113 if (r->flags & IORESOURCE_IO)
114 cmd |= PCI_COMMAND_IO;
115 if (r->flags & IORESOURCE_MEM)
116 cmd |= PCI_COMMAND_MEMORY;
117 }
118 if (dev->resource[PCI_ROM_RESOURCE].start)
119 cmd |= PCI_COMMAND_MEMORY;
120 if (cmd != old_cmd) {
121 printk("PCI: Enabling device %s (%04x -> %04x)\n", dev->slot_name, old_cmd, cmd);
122 pci_write_config_word(dev, PCI_COMMAND, cmd);
123 }
124 return 0;
125 }
126
pcibios_enable_device(struct pci_dev * dev,int mask)127 int pcibios_enable_device(struct pci_dev *dev, int mask)
128 {
129 return pcibios_enable_resources(dev, mask);
130 }
131
132 #ifdef CONFIG_PCI_NEW
133 /*
134 * Named PCI new and about to die before it's old :-)
135 *
136 * Copyright 2001 MontaVista Software Inc.
137 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
138 *
139 * Modified to be mips generic, ppopov@mvista.com
140 */
141
142 /*
143 * This file contains common PCI routines meant to be shared for
144 * all MIPS machines.
145 *
146 * Strategies:
147 *
148 * . We rely on pci_auto.c file to assign PCI resources (MEM and IO)
149 * TODO: this shold be optional for some machines where they do have
150 * a real "pcibios" that does resource assignment.
151 *
152 * . We then use pci_scan_bus() to "discover" all the resources for
153 * later use by Linux.
154 *
155 * . We finally reply on a board supplied function, pcibios_fixup_irq(), to
156 * to assign the interrupts. We may use setup-irq.c under drivers/pci
157 * later.
158 *
159 * . Specifically, we will *NOT* use pci_assign_unassigned_resources(),
160 * because we assume all PCI devices should have the resources correctly
161 * assigned and recorded.
162 *
163 * Limitations:
164 *
165 * . We "collapse" all IO and MEM spaces in sub-buses under a top-level bus
166 * into a contiguous range.
167 *
168 * . In the case of Memory space, the rnage is 1:1 mapping with CPU physical
169 * address space.
170 *
171 * . In the case of IO space, it starts from 0, and the beginning address
172 * is mapped to KSEG0ADDR(mips_io_port) in the CPU physical address.
173 *
174 * . These are the current MIPS limitations (by ioremap, etc). In the
175 * future, we may remove them.
176 *
177 * Credits:
178 * Most of the code are derived from the pci routines from PPC and Alpha,
179 * which were mostly writtne by
180 * Cort Dougan, cort@fsmlabs.com
181 * Matt Porter, mporter@mvista.com
182 * Dave Rusling david.rusling@reo.mts.dec.com
183 * David Mosberger davidm@cs.arizona.edu
184 */
185
186 extern void pcibios_fixup(void);
187 extern void pcibios_fixup_irqs(void);
188
189 struct pci_fixup pcibios_fixups[] = {
190 { PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources },
191 { 0 }
192 };
193
194 extern int pciauto_assign_resources(int busno, struct pci_channel * hose);
195
pcibios_init(void)196 void __init pcibios_init(void)
197 {
198 struct pci_channel *p;
199 struct pci_bus *bus;
200 int busno;
201
202 #ifdef CONFIG_PCI_AUTO
203 /* assign resources */
204 busno=0;
205 for (p= mips_pci_channels; p->pci_ops != NULL; p++) {
206 busno = pciauto_assign_resources(busno, p) + 1;
207 }
208 #endif
209
210 /* scan the buses */
211 busno = 0;
212 for (p= mips_pci_channels; p->pci_ops != NULL; p++) {
213 bus = pci_scan_bus(busno, p->pci_ops, p);
214 busno = bus->subordinate+1;
215 }
216
217 /* machine dependent fixups */
218 pcibios_fixup();
219 /* fixup irqs (board specific routines) */
220 pcibios_fixup_irqs();
221 }
222
pci_bridge_check_io(struct pci_dev * bridge)223 unsigned long __init pci_bridge_check_io(struct pci_dev *bridge)
224 {
225 u16 io;
226
227 pci_read_config_word(bridge, PCI_IO_BASE, &io);
228 if (!io) {
229 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
230 pci_read_config_word(bridge, PCI_IO_BASE, &io);
231 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
232 }
233 if (io)
234 return IORESOURCE_IO;
235 printk(KERN_WARNING "PCI: bridge %s does not support I/O forwarding!\n",
236 bridge->name);
237 return 0;
238 }
239
pcibios_fixup_bus(struct pci_bus * bus)240 void __init pcibios_fixup_bus(struct pci_bus *bus)
241 {
242 /* Propogate hose info into the subordinate devices. */
243
244 struct pci_channel *hose = bus->sysdata;
245 struct pci_dev *dev = bus->self;
246
247 if (!dev) {
248 /* Root bus */
249 bus->resource[0] = hose->io_resource;
250 bus->resource[1] = hose->mem_resource;
251 } else {
252 /* This is a bridge. Do not care how it's initialized,
253 just link its resources to the bus ones */
254 int i;
255
256 for(i=0; i<3; i++) {
257 bus->resource[i] =
258 &dev->resource[PCI_BRIDGE_RESOURCES+i];
259 bus->resource[i]->name = bus->name;
260 }
261 bus->resource[0]->flags |= pci_bridge_check_io(dev);
262 bus->resource[1]->flags |= IORESOURCE_MEM;
263 /* For now, propogate hose limits to the bus;
264 we'll adjust them later. */
265 bus->resource[0]->end = hose->io_resource->end;
266 bus->resource[1]->end = hose->mem_resource->end;
267 /* Turn off downstream PF memory address range by default */
268 bus->resource[2]->start = 1024*1024;
269 bus->resource[2]->end = bus->resource[2]->start - 1;
270 }
271 }
272 #endif
273