blob: d3cde6b9d2df68046eaa41512676ed50fea78c66 [file] [log] [blame]
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001/*
2 * CHRP pci routines.
3 */
4
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10005#include <linux/kernel.h>
6#include <linux/pci.h>
7#include <linux/delay.h>
8#include <linux/string.h>
9#include <linux/init.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100010
11#include <asm/io.h>
12#include <asm/pgtable.h>
13#include <asm/irq.h>
14#include <asm/hydra.h>
15#include <asm/prom.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100016#include <asm/machdep.h>
17#include <asm/sections.h>
18#include <asm/pci-bridge.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100019#include <asm/grackle.h>
20#include <asm/rtas.h>
21
Paul Mackerrasb86756a2006-04-03 16:37:23 +100022#include "chrp.h"
Kumar Gala33d71d22007-08-20 08:50:28 -050023#include "gg2.h"
Paul Mackerrasb86756a2006-04-03 16:37:23 +100024
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100025/* LongTrail */
26void __iomem *gg2_pci_config_base;
27
28/*
29 * The VLSI Golden Gate II has only 512K of PCI configuration space, so we
30 * limit the bus number to 3 bits
31 */
32
33int gg2_read_config(struct pci_bus *bus, unsigned int devfn, int off,
34 int len, u32 *val)
35{
36 volatile void __iomem *cfg_data;
37 struct pci_controller *hose = bus->sysdata;
38
39 if (bus->number > 7)
40 return PCIBIOS_DEVICE_NOT_FOUND;
41 /*
42 * Note: the caller has already checked that off is
43 * suitably aligned and that len is 1, 2 or 4.
44 */
45 cfg_data = hose->cfg_data + ((bus->number<<16) | (devfn<<8) | off);
46 switch (len) {
47 case 1:
48 *val = in_8(cfg_data);
49 break;
50 case 2:
51 *val = in_le16(cfg_data);
52 break;
53 default:
54 *val = in_le32(cfg_data);
55 break;
56 }
57 return PCIBIOS_SUCCESSFUL;
58}
59
60int gg2_write_config(struct pci_bus *bus, unsigned int devfn, int off,
61 int len, u32 val)
62{
63 volatile void __iomem *cfg_data;
64 struct pci_controller *hose = bus->sysdata;
65
66 if (bus->number > 7)
67 return PCIBIOS_DEVICE_NOT_FOUND;
68 /*
69 * Note: the caller has already checked that off is
70 * suitably aligned and that len is 1, 2 or 4.
71 */
72 cfg_data = hose->cfg_data + ((bus->number<<16) | (devfn<<8) | off);
73 switch (len) {
74 case 1:
75 out_8(cfg_data, val);
76 break;
77 case 2:
78 out_le16(cfg_data, val);
79 break;
80 default:
81 out_le32(cfg_data, val);
82 break;
83 }
84 return PCIBIOS_SUCCESSFUL;
85}
86
87static struct pci_ops gg2_pci_ops =
88{
Nathan Lynchf1d645f2007-08-10 05:18:44 +100089 .read = gg2_read_config,
90 .write = gg2_write_config,
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100091};
92
93/*
94 * Access functions for PCI config space using RTAS calls.
95 */
96int rtas_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
97 int len, u32 *val)
98{
99 struct pci_controller *hose = bus->sysdata;
100 unsigned long addr = (offset & 0xff) | ((devfn & 0xff) << 8)
101 | (((bus->number - hose->first_busno) & 0xff) << 16)
Kumar Gala5516b542007-06-27 01:17:57 -0500102 | (hose->global_number << 24);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000103 int ret = -1;
104 int rval;
105
106 rval = rtas_call(rtas_token("read-pci-config"), 2, 2, &ret, addr, len);
107 *val = ret;
108 return rval? PCIBIOS_DEVICE_NOT_FOUND: PCIBIOS_SUCCESSFUL;
109}
110
111int rtas_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
112 int len, u32 val)
113{
114 struct pci_controller *hose = bus->sysdata;
115 unsigned long addr = (offset & 0xff) | ((devfn & 0xff) << 8)
116 | (((bus->number - hose->first_busno) & 0xff) << 16)
Kumar Gala5516b542007-06-27 01:17:57 -0500117 | (hose->global_number << 24);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000118 int rval;
119
120 rval = rtas_call(rtas_token("write-pci-config"), 3, 1, NULL,
121 addr, len, val);
122 return rval? PCIBIOS_DEVICE_NOT_FOUND: PCIBIOS_SUCCESSFUL;
123}
124
125static struct pci_ops rtas_pci_ops =
126{
Nathan Lynchf1d645f2007-08-10 05:18:44 +1000127 .read = rtas_read_config,
128 .write = rtas_write_config,
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000129};
130
131volatile struct Hydra __iomem *Hydra = NULL;
132
133int __init
134hydra_init(void)
135{
136 struct device_node *np;
David Woodhouse575e3212006-01-14 00:13:49 +0000137 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000138
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000139 np = of_find_node_by_name(NULL, "mac-io");
140 if (np == NULL || of_address_to_resource(np, 0, &r)) {
141 of_node_put(np);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000142 return 0;
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000143 }
David Woodhouse575e3212006-01-14 00:13:49 +0000144 Hydra = ioremap(r.start, r.end-r.start);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700145 printk("Hydra Mac I/O at %llx\n", (unsigned long long)r.start);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000146 printk("Hydra Feature_Control was %x",
147 in_le32(&Hydra->Feature_Control));
148 out_le32(&Hydra->Feature_Control, (HYDRA_FC_SCC_CELL_EN |
149 HYDRA_FC_SCSI_CELL_EN |
150 HYDRA_FC_SCCA_ENABLE |
151 HYDRA_FC_SCCB_ENABLE |
152 HYDRA_FC_ARB_BYPASS |
153 HYDRA_FC_MPIC_ENABLE |
154 HYDRA_FC_SLOW_SCC_PCLK |
155 HYDRA_FC_MPIC_IS_MASTER));
156 printk(", now %x\n", in_le32(&Hydra->Feature_Control));
157 return 1;
158}
159
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000160#define PRG_CL_RESET_VALID 0x00010000
161
162static void __init
163setup_python(struct pci_controller *hose, struct device_node *dev)
164{
165 u32 __iomem *reg;
166 u32 val;
David Woodhouse575e3212006-01-14 00:13:49 +0000167 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000168
David Woodhouse575e3212006-01-14 00:13:49 +0000169 if (of_address_to_resource(dev, 0, &r)) {
170 printk(KERN_ERR "No address for Python PCI controller\n");
171 return;
172 }
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000173
174 /* Clear the magic go-slow bit */
David Woodhouse575e3212006-01-14 00:13:49 +0000175 reg = ioremap(r.start + 0xf6000, 0x40);
176 BUG_ON(!reg);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000177 val = in_be32(&reg[12]);
178 if (val & PRG_CL_RESET_VALID) {
179 out_be32(&reg[12], val & ~PRG_CL_RESET_VALID);
180 in_be32(&reg[12]);
181 }
182 iounmap(reg);
David Woodhouse575e3212006-01-14 00:13:49 +0000183
Kumar Gala2e56ff22007-07-19 16:07:35 -0500184 setup_indirect_pci(hose, r.start + 0xf8000, r.start + 0xf8010, 0);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000185}
186
187/* Marvell Discovery II based Pegasos 2 */
188static void __init setup_peg2(struct pci_controller *hose, struct device_node *dev)
189{
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000190 struct device_node *root = of_find_node_by_path("/");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000191 struct device_node *rtas;
192
193 rtas = of_find_node_by_name (root, "rtas");
194 if (rtas) {
195 hose->ops = &rtas_pci_ops;
Olaf Heringd60dcd92006-02-04 12:55:41 +0100196 of_node_put(rtas);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000197 } else {
198 printk ("RTAS supporting Pegasos OF not found, please upgrade"
199 " your firmware\n");
200 }
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +1100201 ppc_pci_flags |= PPC_PCI_REASSIGN_ALL_BUS;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000202 /* keep the reference to the root node */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000203}
204
205void __init
206chrp_find_bridges(void)
207{
208 struct device_node *dev;
Jeremy Kerrae6b4102006-07-12 15:40:05 +1000209 const int *bus_range;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000210 int len, index = -1;
211 struct pci_controller *hose;
Jeremy Kerrae6b4102006-07-12 15:40:05 +1000212 const unsigned int *dma;
213 const char *model, *machine;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000214 int is_longtrail = 0, is_mot = 0, is_pegasos = 0;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000215 struct device_node *root = of_find_node_by_path("/");
David Woodhouse575e3212006-01-14 00:13:49 +0000216 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000217 /*
218 * The PCI host bridge nodes on some machines don't have
219 * properties to adequately identify them, so we have to
220 * look at what sort of machine this is as well.
221 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000222 machine = of_get_property(root, "model", NULL);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000223 if (machine != NULL) {
224 is_longtrail = strncmp(machine, "IBM,LongTrail", 13) == 0;
225 is_mot = strncmp(machine, "MOT", 3) == 0;
226 if (strncmp(machine, "Pegasos2", 8) == 0)
227 is_pegasos = 2;
228 else if (strncmp(machine, "Pegasos", 7) == 0)
229 is_pegasos = 1;
230 }
231 for (dev = root->child; dev != NULL; dev = dev->sibling) {
232 if (dev->type == NULL || strcmp(dev->type, "pci") != 0)
233 continue;
234 ++index;
235 /* The GG2 bridge on the LongTrail doesn't have an address */
David Woodhouse575e3212006-01-14 00:13:49 +0000236 if (of_address_to_resource(dev, 0, &r) && !is_longtrail) {
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000237 printk(KERN_WARNING "Can't use %s: no address\n",
238 dev->full_name);
239 continue;
240 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000241 bus_range = of_get_property(dev, "bus-range", &len);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000242 if (bus_range == NULL || len < 2 * sizeof(int)) {
243 printk(KERN_WARNING "Can't get bus-range for %s\n",
244 dev->full_name);
245 continue;
246 }
247 if (bus_range[1] == bus_range[0])
248 printk(KERN_INFO "PCI bus %d", bus_range[0]);
249 else
250 printk(KERN_INFO "PCI buses %d..%d",
251 bus_range[0], bus_range[1]);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000252 printk(" controlled by %s", dev->full_name);
David Woodhouse575e3212006-01-14 00:13:49 +0000253 if (!is_longtrail)
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700254 printk(" at %llx", (unsigned long long)r.start);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000255 printk("\n");
256
Kumar Galadbf84712007-06-27 01:56:50 -0500257 hose = pcibios_alloc_controller(dev);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000258 if (!hose) {
259 printk("Can't allocate PCI controller structure for %s\n",
260 dev->full_name);
261 continue;
262 }
Benjamin Herrenschmidtee673ea2008-10-13 20:49:47 +0000263 hose->first_busno = hose->self_busno = bus_range[0];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000264 hose->last_busno = bus_range[1];
265
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000266 model = of_get_property(dev, "model", NULL);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000267 if (model == NULL)
268 model = "<none>";
Benjamin Herrenschmidt22007a12008-10-13 20:14:09 +0000269 if (strncmp(model, "IBM, Python", 11) == 0) {
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000270 setup_python(hose, dev);
271 } else if (is_mot
272 || strncmp(model, "Motorola, Grackle", 17) == 0) {
273 setup_grackle(hose);
274 } else if (is_longtrail) {
275 void __iomem *p = ioremap(GG2_PCI_CONFIG_BASE, 0x80000);
276 hose->ops = &gg2_pci_ops;
277 hose->cfg_data = p;
278 gg2_pci_config_base = p;
279 } else if (is_pegasos == 1) {
Kumar Gala2e56ff22007-07-19 16:07:35 -0500280 setup_indirect_pci(hose, 0xfec00cf8, 0xfee00cfc, 0);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000281 } else if (is_pegasos == 2) {
282 setup_peg2(hose, dev);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000283 } else if (!strncmp(model, "IBM,CPC710", 10)) {
284 setup_indirect_pci(hose,
285 r.start + 0x000f8000,
Kumar Gala2e56ff22007-07-19 16:07:35 -0500286 r.start + 0x000f8010,
287 0);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000288 if (index == 0) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000289 dma = of_get_property(dev, "system-dma-base",
290 &len);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000291 if (dma && len >= sizeof(*dma)) {
292 dma = (unsigned int *)
293 (((unsigned long)dma) +
294 len - sizeof(*dma));
295 pci_dram_offset = *dma;
296 }
297 }
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000298 } else {
299 printk("No methods for %s (model %s), using RTAS\n",
300 dev->full_name, model);
301 hose->ops = &rtas_pci_ops;
302 }
303
304 pci_process_bridge_OF_ranges(hose, dev, index == 0);
305
306 /* check the first bridge for a property that we can
307 use to set pci_dram_offset */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000308 dma = of_get_property(dev, "ibm,dma-ranges", &len);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000309 if (index == 0 && dma != NULL && len >= 6 * sizeof(*dma)) {
310 pci_dram_offset = dma[2] - dma[3];
311 printk("pci_dram_offset = %lx\n", pci_dram_offset);
312 }
313 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000314 of_node_put(root);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000315}
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000316
317/* SL82C105 IDE Control/Status Register */
318#define SL82C105_IDECSR 0x40
319
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +1100320/* Fixup for Winbond ATA quirk, required for briq mostly because the
321 * 8259 is configured for level sensitive IRQ 14 and so wants the
322 * ATA controller to be set to fully native mode or bad things
323 * will happen.
324 */
325static void __devinit chrp_pci_fixup_winbond_ata(struct pci_dev *sl82c105)
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000326{
327 u8 progif;
328
329 /* If non-briq machines need that fixup too, please speak up */
330 if (!machine_is(chrp) || _chrp_type != _CHRP_briq)
331 return;
332
333 if ((sl82c105->class & 5) != 5) {
334 printk("W83C553: Switching SL82C105 IDE to PCI native mode\n");
335 /* Enable SL82C105 PCI native IDE mode */
336 pci_read_config_byte(sl82c105, PCI_CLASS_PROG, &progif);
337 pci_write_config_byte(sl82c105, PCI_CLASS_PROG, progif | 0x05);
338 sl82c105->class |= 0x05;
339 /* Disable SL82C105 second port */
340 pci_write_config_word(sl82c105, SL82C105_IDECSR, 0x0003);
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +1100341 /* Clear IO BARs, they will be reassigned */
342 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_0, 0);
343 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_1, 0);
344 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_2, 0);
345 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_3, 0);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000346 }
347}
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +1100348DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105,
349 chrp_pci_fixup_winbond_ata);
Olaf Hering556ecf92007-08-18 04:27:17 +1000350
351/* Pegasos2 firmware version 20040810 configures the built-in IDE controller
352 * in legacy mode, but sets the PCI registers to PCI native mode.
353 * The chip can only operate in legacy mode, so force the PCI class into legacy
354 * mode as well. The same fixup must be done to the class-code property in
355 * the IDE node /pci@80000000/ide@C,1
356 */
Olaf Hering092ca5b2008-01-21 20:39:30 +1100357static void chrp_pci_fixup_vt8231_ata(struct pci_dev *viaide)
Olaf Hering556ecf92007-08-18 04:27:17 +1000358{
359 u8 progif;
360 struct pci_dev *viaisa;
361
362 if (!machine_is(chrp) || _chrp_type != _CHRP_Pegasos)
363 return;
364 if (viaide->irq != 14)
365 return;
366
367 viaisa = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231, NULL);
368 if (!viaisa)
369 return;
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200370 dev_info(&viaide->dev, "Fixing VIA IDE, force legacy mode on\n");
Olaf Hering556ecf92007-08-18 04:27:17 +1000371
372 pci_read_config_byte(viaide, PCI_CLASS_PROG, &progif);
373 pci_write_config_byte(viaide, PCI_CLASS_PROG, progif & ~0x5);
374 viaide->class &= ~0x5;
375
376 pci_dev_put(viaisa);
377}
Olaf Hering092ca5b2008-01-21 20:39:30 +1100378DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_1, chrp_pci_fixup_vt8231_ata);