more changes on original files
[linux-2.4.git] / arch / mips / momentum / jaguar_atx / pci.c
1 /*
2  * Copyright 2002 Momentum Computer
3  * Author: Matthew Dharm <mdharm@momenco.com>
4  *
5  *  This program is free software; you can redistribute  it and/or modify it
6  *  under  the terms of  the GNU General  Public License as published by the
7  *  Free Software Foundation;  either version 2 of the  License, or (at your
8  *  option) any later version.
9  *
10  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
11  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
12  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
13  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
14  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
15  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
16  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
17  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
18  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
19  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  *  You should have received a copy of the  GNU General Public License along
22  *  with this program; if not, write  to the Free Software Foundation, Inc.,
23  *  675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 #include <linux/config.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/version.h>
31 #include <asm/pci.h>
32 #include <asm/io.h>
33 #include <asm/mv64340.h>
34
35 #include <linux/init.h>
36
37 #ifdef CONFIG_PCI
38
39 #define SELF 0
40
41 /*
42  * These functions and structures provide the BIOS scan and mapping of the PCI
43  * devices.
44  */
45
46 #define MAX_PCI_DEVS 10
47
48 void mv64340_board_pcibios_fixup_bus(struct pci_bus* c);
49
50 /*  Functions to implement "pci ops"  */
51 static int galileo_pcibios_read_config_word(struct pci_dev *dev,
52                                             int offset, u16 * val);
53 static int galileo_pcibios_read_config_byte(struct pci_dev *dev,
54                                             int offset, u8 * val);
55 static int galileo_pcibios_read_config_dword(struct pci_dev *dev,
56                                              int offset, u32 * val);
57 static int galileo_pcibios_write_config_byte(struct pci_dev *dev,
58                                              int offset, u8 val);
59 static int galileo_pcibios_write_config_word(struct pci_dev *dev,
60                                              int offset, u16 val);
61 static int galileo_pcibios_write_config_dword(struct pci_dev *dev,
62                                               int offset, u32 val);
63 static void galileo_pcibios_set_master(struct pci_dev *dev);
64
65 /*
66  *  General-purpose PCI functions.
67  */
68
69
70 /*
71  * pci_range_ck -
72  *
73  * Check if the pci device that are trying to access does really exists
74  * on the evaluation board.  
75  *
76  * Inputs :
77  * bus - bus number (0 for PCI 0 ; 1 for PCI 1)
78  * dev - number of device on the specific pci bus
79  *
80  * Outpus :
81  * 0 - if OK , 1 - if failure
82  */
83 static __inline__ int pci_range_ck(unsigned char bus, unsigned char dev)
84 {
85         /* Accessing device 31 crashes the MV-64340. */
86         if (dev < 5)
87                 return 0;
88         return -1;
89 }
90
91 /*
92  * galileo_pcibios_(read/write)_config_(dword/word/byte) -
93  *
94  * reads/write a dword/word/byte register from the configuration space
95  * of a device.
96  *
97  * Note that bus 0 and bus 1 are local, and we assume all other busses are
98  * bridged from bus 1.  This is a safe assumption, since any other
99  * configuration will require major modifications to the CP7000G
100  *
101  * Inputs :
102  * bus - bus number
103  * dev - device number
104  * offset - register offset in the configuration space
105  * val - value to be written / read
106  *
107  * Outputs :
108  * PCIBIOS_SUCCESSFUL when operation was succesfull
109  * PCIBIOS_DEVICE_NOT_FOUND when the bus or dev is errorneous
110  * PCIBIOS_BAD_REGISTER_NUMBER when accessing non aligned
111  */
112
113 static int galileo_pcibios_read_config_dword(struct pci_dev *device,
114                                               int offset, u32* val)
115 {
116         int dev, bus, func;
117         uint32_t address_reg, data_reg;
118         uint32_t address;
119
120         bus = device->bus->number;
121         dev = PCI_SLOT(device->devfn);
122         func = PCI_FUNC(device->devfn);
123
124         /* verify the range */
125         if (pci_range_ck(bus, dev))
126                 return PCIBIOS_DEVICE_NOT_FOUND;
127
128         /* select the MV-64340 registers to communicate with the PCI bus */
129         if (bus == 0) {
130                 address_reg = MV64340_PCI_0_CONFIG_ADDR;
131                 data_reg = MV64340_PCI_0_CONFIG_DATA_VIRTUAL_REG;
132         } else {
133                 address_reg = MV64340_PCI_1_CONFIG_ADDR;
134                 data_reg = MV64340_PCI_1_CONFIG_DATA_VIRTUAL_REG;
135         }
136
137         address = (bus << 16) | (dev << 11) | (func << 8) |
138                 (offset & 0xfc) | 0x80000000;
139
140         /* start the configuration cycle */
141         MV_WRITE(address_reg, address);
142
143         /* read the data */
144         MV_READ(data_reg, val);
145
146         return PCIBIOS_SUCCESSFUL;
147 }
148
149
150 static int galileo_pcibios_read_config_word(struct pci_dev *device,
151                                              int offset, u16* val)
152 {
153         int dev, bus, func;
154         uint32_t address_reg, data_reg;
155         uint32_t address;
156
157         bus = device->bus->number;
158         dev = PCI_SLOT(device->devfn);
159         func = PCI_FUNC(device->devfn);
160
161         /* verify the range */
162         if (pci_range_ck(bus, dev))
163                 return PCIBIOS_DEVICE_NOT_FOUND;
164
165         /* select the MV-64340 registers to communicate with the PCI bus */
166         if (bus == 0) {
167                 address_reg = MV64340_PCI_0_CONFIG_ADDR;
168                 data_reg = MV64340_PCI_0_CONFIG_DATA_VIRTUAL_REG;
169         } else {
170                 address_reg = MV64340_PCI_1_CONFIG_ADDR;
171                 data_reg = MV64340_PCI_1_CONFIG_DATA_VIRTUAL_REG;
172         }
173
174         address = (bus << 16) | (dev << 11) | (func << 8) |
175                 (offset & 0xfc) | 0x80000000;
176
177         /* start the configuration cycle */
178         MV_WRITE(address_reg, address);
179
180         /* read the data */
181         MV_READ_16(data_reg + (offset & 0x3), val);
182
183         return PCIBIOS_SUCCESSFUL;
184 }
185
186 static int galileo_pcibios_read_config_byte(struct pci_dev *device,
187                                              int offset, u8* val)
188 {
189         int dev, bus, func;
190         uint32_t address_reg, data_reg;
191         uint32_t address;
192
193         bus = device->bus->number;
194         dev = PCI_SLOT(device->devfn);
195         func = PCI_FUNC(device->devfn);
196
197         /* verify the range */
198         if (pci_range_ck(bus, dev))
199                 return PCIBIOS_DEVICE_NOT_FOUND;
200
201         /* select the MV-64340 registers to communicate with the PCI bus */
202         if (bus == 0) {
203                 address_reg = MV64340_PCI_0_CONFIG_ADDR;
204                 data_reg = MV64340_PCI_0_CONFIG_DATA_VIRTUAL_REG;
205         } else {
206                 address_reg = MV64340_PCI_1_CONFIG_ADDR;
207                 data_reg = MV64340_PCI_1_CONFIG_DATA_VIRTUAL_REG;
208         }
209
210         address = (bus << 16) | (dev << 11) | (func << 8) |
211                 (offset & 0xfc) | 0x80000000;
212
213         /* start the configuration cycle */
214         MV_WRITE(address_reg, address);
215
216         /* write the data */
217         MV_READ_8(data_reg + (offset & 0x3), val);
218
219         return PCIBIOS_SUCCESSFUL;
220 }
221
222 static int galileo_pcibios_write_config_dword(struct pci_dev *device,
223                                               int offset, u32 val)
224 {
225         int dev, bus, func;
226         uint32_t address_reg, data_reg;
227         uint32_t address;
228
229         bus = device->bus->number;
230         dev = PCI_SLOT(device->devfn);
231         func = PCI_FUNC(device->devfn);
232
233         /* verify the range */
234         if (pci_range_ck(bus, dev))
235                 return PCIBIOS_DEVICE_NOT_FOUND;
236
237         /* select the MV-64340 registers to communicate with the PCI bus */
238         if (bus == 0) {
239                 address_reg = MV64340_PCI_0_CONFIG_ADDR;
240                 data_reg = MV64340_PCI_0_CONFIG_DATA_VIRTUAL_REG;
241         } else {
242                 address_reg = MV64340_PCI_1_CONFIG_ADDR;
243                 data_reg = MV64340_PCI_1_CONFIG_DATA_VIRTUAL_REG;
244         }
245
246         address = (bus << 16) | (dev << 11) | (func << 8) |
247                 (offset & 0xfc) | 0x80000000;
248
249         /* start the configuration cycle */
250         MV_WRITE(address_reg, address);
251
252         /* write the data */
253         MV_WRITE(data_reg, val);
254
255         return PCIBIOS_SUCCESSFUL;
256 }
257
258
259 static int galileo_pcibios_write_config_word(struct pci_dev *device,
260                                              int offset, u16 val)
261 {
262         int dev, bus, func;
263         uint32_t address_reg, data_reg;
264         uint32_t address;
265
266         bus = device->bus->number;
267         dev = PCI_SLOT(device->devfn);
268         func = PCI_FUNC(device->devfn);
269
270         /* verify the range */
271         if (pci_range_ck(bus, dev))
272                 return PCIBIOS_DEVICE_NOT_FOUND;
273
274         /* select the MV-64340 registers to communicate with the PCI bus */
275         if (bus == 0) {
276                 address_reg = MV64340_PCI_0_CONFIG_ADDR;
277                 data_reg = MV64340_PCI_0_CONFIG_DATA_VIRTUAL_REG;
278         } else {
279                 address_reg = MV64340_PCI_1_CONFIG_ADDR;
280                 data_reg = MV64340_PCI_1_CONFIG_DATA_VIRTUAL_REG;
281         }
282
283         address = (bus << 16) | (dev << 11) | (func << 8) |
284                 (offset & 0xfc) | 0x80000000;
285
286         /* start the configuration cycle */
287         MV_WRITE(address_reg, address);
288
289         /* write the data */
290         MV_WRITE_16(data_reg + (offset & 0x3), val);
291
292         return PCIBIOS_SUCCESSFUL;
293 }
294
295 static int galileo_pcibios_write_config_byte(struct pci_dev *device,
296                                              int offset, u8 val)
297 {
298         int dev, bus, func;
299         uint32_t address_reg, data_reg;
300         uint32_t address;
301
302         bus = device->bus->number;
303         dev = PCI_SLOT(device->devfn);
304         func = PCI_FUNC(device->devfn);
305
306         /* verify the range */
307         if (pci_range_ck(bus, dev))
308                 return PCIBIOS_DEVICE_NOT_FOUND;
309
310         /* select the MV-64340 registers to communicate with the PCI bus */
311         if (bus == 0) {
312                 address_reg = MV64340_PCI_0_CONFIG_ADDR;
313                 data_reg = MV64340_PCI_0_CONFIG_DATA_VIRTUAL_REG;
314         } else {
315                 address_reg = MV64340_PCI_1_CONFIG_ADDR;
316                 data_reg = MV64340_PCI_1_CONFIG_DATA_VIRTUAL_REG;
317         }
318
319         address = (bus << 16) | (dev << 11) | (func << 8) |
320                 (offset & 0xfc) | 0x80000000;
321
322         /* start the configuration cycle */
323         MV_WRITE(address_reg, address);
324
325         /* write the data */
326         MV_WRITE_8(data_reg + (offset & 0x3), val);
327
328         return PCIBIOS_SUCCESSFUL;
329 }
330
331 static void galileo_pcibios_set_master(struct pci_dev *dev)
332 {
333         u16 cmd;
334
335         galileo_pcibios_read_config_word(dev, PCI_COMMAND, &cmd);
336         cmd |= PCI_COMMAND_MASTER;
337         galileo_pcibios_write_config_word(dev, PCI_COMMAND, cmd);
338 }
339
340 /*  Externally-expected functions.  Do not change function names  */
341
342 int pcibios_enable_resources(struct pci_dev *dev)
343 {
344         u16 cmd, old_cmd;
345         u8 tmp1;
346         int idx;
347         struct resource *r;
348
349         galileo_pcibios_read_config_word(dev, PCI_COMMAND, &cmd);
350         old_cmd = cmd;
351         for (idx = 0; idx < 6; idx++) {
352                 r = &dev->resource[idx];
353                 if (!r->start && r->end) {
354                         printk(KERN_ERR
355                                "PCI: Device %s not available because of "
356                                "resource collisions\n", dev->slot_name);
357                         return -EINVAL;
358                 }
359                 if (r->flags & IORESOURCE_IO)
360                         cmd |= PCI_COMMAND_IO;
361                 if (r->flags & IORESOURCE_MEM)
362                         cmd |= PCI_COMMAND_MEMORY;
363         }
364         if (cmd != old_cmd) {
365                 galileo_pcibios_write_config_word(dev, PCI_COMMAND, cmd);
366         }
367
368         /*
369          * Let's fix up the latency timer and cache line size here.  Cache
370          * line size = 32 bytes / sizeof dword (4) = 8.
371          * Latency timer must be > 8.  32 is random but appears to work.
372          */
373         galileo_pcibios_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &tmp1);
374         if (tmp1 != 8) {
375                 printk(KERN_WARNING "PCI setting cache line size to 8 from "
376                        "%d\n", tmp1);
377                 galileo_pcibios_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
378                                                   8);
379         }
380         galileo_pcibios_read_config_byte(dev, PCI_LATENCY_TIMER, &tmp1);
381         if (tmp1 < 32) {
382                 printk(KERN_WARNING "PCI setting latency timer to 32 from %d\n",
383                        tmp1);
384                 galileo_pcibios_write_config_byte(dev, PCI_LATENCY_TIMER,
385                                                   32);
386         }
387
388         return 0;
389 }
390
391 #if 0
392 int pcibios_enable_device(struct pci_dev *dev, int mask)
393 {
394         return pcibios_enable_resources(dev);
395 }
396
397 void pcibios_update_resource(struct pci_dev *dev, struct resource *root,
398                              struct resource *res, int resource)
399 {
400         u32 new, check;
401         int reg;
402
403         return;
404
405         new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
406         if (resource < 6) {
407                 reg = PCI_BASE_ADDRESS_0 + 4 * resource;
408         } else if (resource == PCI_ROM_RESOURCE) {
409                 res->flags |= PCI_ROM_ADDRESS_ENABLE;
410                 reg = dev->rom_base_reg;
411         } else {
412                 /*
413                  * Somebody might have asked allocation of a non-standard
414                  * resource
415                  */
416                 return;
417         }
418
419         pci_write_config_dword(dev, reg, new);
420         pci_read_config_dword(dev, reg, &check);
421         if ((new ^ check) &
422             ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK :
423              PCI_BASE_ADDRESS_MEM_MASK)) {
424                 printk(KERN_ERR "PCI: Error while updating region "
425                        "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
426                        new, check);
427         }
428 }
429
430
431 void pcibios_align_resource(void *data, struct resource *res,
432                             unsigned long size, unsigned long align)
433 {
434         struct pci_dev *dev = data;
435
436         if (res->flags & IORESOURCE_IO) {
437                 unsigned long start = res->start;
438
439                 /* We need to avoid collisions with `mirrored' VGA ports
440                    and other strange ISA hardware, so we always want the
441                    addresses kilobyte aligned.  */
442                 if (size > 0x100) {
443                         printk(KERN_ERR "PCI: I/O Region %s/%d too large"
444                                " (%ld bytes)\n", dev->slot_name,
445                                 dev->resource - res, size);
446                 }
447
448                 start = (start + 1024 - 1) & ~(1024 - 1);
449                 res->start = start;
450         }
451 }
452 #endif
453
454 struct pci_ops galileo_pci_ops = {
455         galileo_pcibios_read_config_byte,
456         galileo_pcibios_read_config_word,
457         galileo_pcibios_read_config_dword,
458         galileo_pcibios_write_config_byte,
459         galileo_pcibios_write_config_word,
460         galileo_pcibios_write_config_dword
461 };
462
463 struct pci_fixup pcibios_fixups[] = {
464         {0}
465 };
466
467 void __init pcibios_fixup_bus(struct pci_bus *c)
468 {
469         mv64340_board_pcibios_fixup_bus(c);
470 }
471
472 void __init pcibios_init(void)
473 {
474         /* Reset PCI I/O and PCI MEM values */
475         ioport_resource.start = 0xe0000000;
476         ioport_resource.end   = 0xe0000000 + 0x20000000 - 1;
477         iomem_resource.start  = 0xc0000000;
478         iomem_resource.end    = 0xc0000000 + 0x20000000 - 1;
479
480         pci_scan_bus(0, &galileo_pci_ops, NULL);
481         pci_scan_bus(1, &galileo_pci_ops, NULL);
482 }
483
484 unsigned __init int pcibios_assign_all_busses(void)
485 {
486         return 1;
487 }
488
489 #endif  /* CONFIG_PCI */