blob: b020c757d2bf219bcbaac81ea234614f474c5c10 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10002/*
3 * CHRP pci routines.
4 */
5
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10006#include <linux/kernel.h>
7#include <linux/pci.h>
8#include <linux/delay.h>
9#include <linux/string.h>
10#include <linux/init.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100011
12#include <asm/io.h>
13#include <asm/pgtable.h>
14#include <asm/irq.h>
15#include <asm/hydra.h>
16#include <asm/prom.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100017#include <asm/machdep.h>
18#include <asm/sections.h>
19#include <asm/pci-bridge.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100020#include <asm/grackle.h>
21#include <asm/rtas.h>
22
Paul Mackerrasb86756a2006-04-03 16:37:23 +100023#include "chrp.h"
Kumar Gala33d71d22007-08-20 08:50:28 -050024#include "gg2.h"
Paul Mackerrasb86756a2006-04-03 16:37:23 +100025
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100026/* LongTrail */
27void __iomem *gg2_pci_config_base;
28
29/*
30 * The VLSI Golden Gate II has only 512K of PCI configuration space, so we
31 * limit the bus number to 3 bits
32 */
33
Mathieu Malaterre910be6b2018-04-04 22:15:03 +020034static int gg2_read_config(struct pci_bus *bus, unsigned int devfn, int off,
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100035 int len, u32 *val)
36{
37 volatile void __iomem *cfg_data;
Kumar Gala84569932009-04-30 03:10:11 +000038 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100039
40 if (bus->number > 7)
41 return PCIBIOS_DEVICE_NOT_FOUND;
42 /*
43 * Note: the caller has already checked that off is
44 * suitably aligned and that len is 1, 2 or 4.
45 */
46 cfg_data = hose->cfg_data + ((bus->number<<16) | (devfn<<8) | off);
47 switch (len) {
48 case 1:
49 *val = in_8(cfg_data);
50 break;
51 case 2:
52 *val = in_le16(cfg_data);
53 break;
54 default:
55 *val = in_le32(cfg_data);
56 break;
57 }
58 return PCIBIOS_SUCCESSFUL;
59}
60
Mathieu Malaterre910be6b2018-04-04 22:15:03 +020061static int gg2_write_config(struct pci_bus *bus, unsigned int devfn, int off,
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100062 int len, u32 val)
63{
64 volatile void __iomem *cfg_data;
Kumar Gala84569932009-04-30 03:10:11 +000065 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100066
67 if (bus->number > 7)
68 return PCIBIOS_DEVICE_NOT_FOUND;
69 /*
70 * Note: the caller has already checked that off is
71 * suitably aligned and that len is 1, 2 or 4.
72 */
73 cfg_data = hose->cfg_data + ((bus->number<<16) | (devfn<<8) | off);
74 switch (len) {
75 case 1:
76 out_8(cfg_data, val);
77 break;
78 case 2:
79 out_le16(cfg_data, val);
80 break;
81 default:
82 out_le32(cfg_data, val);
83 break;
84 }
85 return PCIBIOS_SUCCESSFUL;
86}
87
88static struct pci_ops gg2_pci_ops =
89{
Nathan Lynchf1d645f2007-08-10 05:18:44 +100090 .read = gg2_read_config,
91 .write = gg2_write_config,
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100092};
93
94/*
95 * Access functions for PCI config space using RTAS calls.
96 */
Mathieu Malaterre910be6b2018-04-04 22:15:03 +020097static int rtas_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
98 int len, u32 *val)
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100099{
Kumar Gala84569932009-04-30 03:10:11 +0000100 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000101 unsigned long addr = (offset & 0xff) | ((devfn & 0xff) << 8)
102 | (((bus->number - hose->first_busno) & 0xff) << 16)
Kumar Gala5516b542007-06-27 01:17:57 -0500103 | (hose->global_number << 24);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000104 int ret = -1;
105 int rval;
106
107 rval = rtas_call(rtas_token("read-pci-config"), 2, 2, &ret, addr, len);
108 *val = ret;
109 return rval? PCIBIOS_DEVICE_NOT_FOUND: PCIBIOS_SUCCESSFUL;
110}
111
Mathieu Malaterre910be6b2018-04-04 22:15:03 +0200112static int rtas_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
113 int len, u32 val)
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000114{
Kumar Gala84569932009-04-30 03:10:11 +0000115 struct pci_controller *hose = pci_bus_to_host(bus);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000116 unsigned long addr = (offset & 0xff) | ((devfn & 0xff) << 8)
117 | (((bus->number - hose->first_busno) & 0xff) << 16)
Kumar Gala5516b542007-06-27 01:17:57 -0500118 | (hose->global_number << 24);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000119 int rval;
120
121 rval = rtas_call(rtas_token("write-pci-config"), 3, 1, NULL,
122 addr, len, val);
123 return rval? PCIBIOS_DEVICE_NOT_FOUND: PCIBIOS_SUCCESSFUL;
124}
125
126static struct pci_ops rtas_pci_ops =
127{
Nathan Lynchf1d645f2007-08-10 05:18:44 +1000128 .read = rtas_read_config,
129 .write = rtas_write_config,
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000130};
131
132volatile struct Hydra __iomem *Hydra = NULL;
133
134int __init
135hydra_init(void)
136{
137 struct device_node *np;
David Woodhouse575e3212006-01-14 00:13:49 +0000138 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000139
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000140 np = of_find_node_by_name(NULL, "mac-io");
141 if (np == NULL || of_address_to_resource(np, 0, &r)) {
142 of_node_put(np);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000143 return 0;
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000144 }
Nicolas Palix29e931c2008-12-02 03:34:46 +0000145 of_node_put(np);
Joe Perches28f65c112011-06-09 09:13:32 -0700146 Hydra = ioremap(r.start, resource_size(&r));
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700147 printk("Hydra Mac I/O at %llx\n", (unsigned long long)r.start);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000148 printk("Hydra Feature_Control was %x",
149 in_le32(&Hydra->Feature_Control));
150 out_le32(&Hydra->Feature_Control, (HYDRA_FC_SCC_CELL_EN |
151 HYDRA_FC_SCSI_CELL_EN |
152 HYDRA_FC_SCCA_ENABLE |
153 HYDRA_FC_SCCB_ENABLE |
154 HYDRA_FC_ARB_BYPASS |
155 HYDRA_FC_MPIC_ENABLE |
156 HYDRA_FC_SLOW_SCC_PCLK |
157 HYDRA_FC_MPIC_IS_MASTER));
158 printk(", now %x\n", in_le32(&Hydra->Feature_Control));
159 return 1;
160}
161
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000162#define PRG_CL_RESET_VALID 0x00010000
163
164static void __init
165setup_python(struct pci_controller *hose, struct device_node *dev)
166{
167 u32 __iomem *reg;
168 u32 val;
David Woodhouse575e3212006-01-14 00:13:49 +0000169 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000170
David Woodhouse575e3212006-01-14 00:13:49 +0000171 if (of_address_to_resource(dev, 0, &r)) {
172 printk(KERN_ERR "No address for Python PCI controller\n");
173 return;
174 }
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000175
176 /* Clear the magic go-slow bit */
David Woodhouse575e3212006-01-14 00:13:49 +0000177 reg = ioremap(r.start + 0xf6000, 0x40);
178 BUG_ON(!reg);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000179 val = in_be32(&reg[12]);
180 if (val & PRG_CL_RESET_VALID) {
181 out_be32(&reg[12], val & ~PRG_CL_RESET_VALID);
182 in_be32(&reg[12]);
183 }
184 iounmap(reg);
David Woodhouse575e3212006-01-14 00:13:49 +0000185
Kumar Gala2e56ff22007-07-19 16:07:35 -0500186 setup_indirect_pci(hose, r.start + 0xf8000, r.start + 0xf8010, 0);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000187}
188
189/* Marvell Discovery II based Pegasos 2 */
190static void __init setup_peg2(struct pci_controller *hose, struct device_node *dev)
191{
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000192 struct device_node *root = of_find_node_by_path("/");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000193 struct device_node *rtas;
194
195 rtas = of_find_node_by_name (root, "rtas");
196 if (rtas) {
197 hose->ops = &rtas_pci_ops;
Olaf Heringd60dcd92006-02-04 12:55:41 +0100198 of_node_put(rtas);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000199 } else {
200 printk ("RTAS supporting Pegasos OF not found, please upgrade"
201 " your firmware\n");
202 }
Rob Herring0e47ff12011-07-12 09:25:51 -0500203 pci_add_flags(PCI_REASSIGN_ALL_BUS);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000204 /* keep the reference to the root node */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000205}
206
207void __init
208chrp_find_bridges(void)
209{
210 struct device_node *dev;
Jeremy Kerrae6b4102006-07-12 15:40:05 +1000211 const int *bus_range;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000212 int len, index = -1;
213 struct pci_controller *hose;
Jeremy Kerrae6b4102006-07-12 15:40:05 +1000214 const unsigned int *dma;
215 const char *model, *machine;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000216 int is_longtrail = 0, is_mot = 0, is_pegasos = 0;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000217 struct device_node *root = of_find_node_by_path("/");
David Woodhouse575e3212006-01-14 00:13:49 +0000218 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000219 /*
220 * The PCI host bridge nodes on some machines don't have
221 * properties to adequately identify them, so we have to
222 * look at what sort of machine this is as well.
223 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000224 machine = of_get_property(root, "model", NULL);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000225 if (machine != NULL) {
226 is_longtrail = strncmp(machine, "IBM,LongTrail", 13) == 0;
227 is_mot = strncmp(machine, "MOT", 3) == 0;
228 if (strncmp(machine, "Pegasos2", 8) == 0)
229 is_pegasos = 2;
230 else if (strncmp(machine, "Pegasos", 7) == 0)
231 is_pegasos = 1;
232 }
Rob Herringe5480bd2018-11-16 16:11:00 -0600233 for_each_child_of_node(root, dev) {
234 if (!of_node_is_type(dev, "pci"))
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000235 continue;
236 ++index;
237 /* The GG2 bridge on the LongTrail doesn't have an address */
David Woodhouse575e3212006-01-14 00:13:49 +0000238 if (of_address_to_resource(dev, 0, &r) && !is_longtrail) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500239 printk(KERN_WARNING "Can't use %pOF: no address\n",
240 dev);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000241 continue;
242 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000243 bus_range = of_get_property(dev, "bus-range", &len);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000244 if (bus_range == NULL || len < 2 * sizeof(int)) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500245 printk(KERN_WARNING "Can't get bus-range for %pOF\n",
246 dev);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000247 continue;
248 }
249 if (bus_range[1] == bus_range[0])
250 printk(KERN_INFO "PCI bus %d", bus_range[0]);
251 else
252 printk(KERN_INFO "PCI buses %d..%d",
253 bus_range[0], bus_range[1]);
Rob Herringb7c670d2017-08-21 10:16:47 -0500254 printk(" controlled by %pOF", dev);
David Woodhouse575e3212006-01-14 00:13:49 +0000255 if (!is_longtrail)
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700256 printk(" at %llx", (unsigned long long)r.start);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000257 printk("\n");
258
Kumar Galadbf84712007-06-27 01:56:50 -0500259 hose = pcibios_alloc_controller(dev);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000260 if (!hose) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500261 printk("Can't allocate PCI controller structure for %pOF\n",
262 dev);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000263 continue;
264 }
Benjamin Herrenschmidtee673ea2008-10-13 20:49:47 +0000265 hose->first_busno = hose->self_busno = bus_range[0];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000266 hose->last_busno = bus_range[1];
267
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000268 model = of_get_property(dev, "model", NULL);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000269 if (model == NULL)
270 model = "<none>";
Benjamin Herrenschmidt22007a12008-10-13 20:14:09 +0000271 if (strncmp(model, "IBM, Python", 11) == 0) {
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000272 setup_python(hose, dev);
273 } else if (is_mot
274 || strncmp(model, "Motorola, Grackle", 17) == 0) {
275 setup_grackle(hose);
276 } else if (is_longtrail) {
277 void __iomem *p = ioremap(GG2_PCI_CONFIG_BASE, 0x80000);
278 hose->ops = &gg2_pci_ops;
279 hose->cfg_data = p;
280 gg2_pci_config_base = p;
281 } else if (is_pegasos == 1) {
Kumar Gala2e56ff22007-07-19 16:07:35 -0500282 setup_indirect_pci(hose, 0xfec00cf8, 0xfee00cfc, 0);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000283 } else if (is_pegasos == 2) {
284 setup_peg2(hose, dev);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000285 } else if (!strncmp(model, "IBM,CPC710", 10)) {
286 setup_indirect_pci(hose,
287 r.start + 0x000f8000,
Kumar Gala2e56ff22007-07-19 16:07:35 -0500288 r.start + 0x000f8010,
289 0);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000290 if (index == 0) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000291 dma = of_get_property(dev, "system-dma-base",
292 &len);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000293 if (dma && len >= sizeof(*dma)) {
294 dma = (unsigned int *)
295 (((unsigned long)dma) +
296 len - sizeof(*dma));
297 pci_dram_offset = *dma;
298 }
299 }
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000300 } else {
Rob Herringb7c670d2017-08-21 10:16:47 -0500301 printk("No methods for %pOF (model %s), using RTAS\n",
302 dev, model);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000303 hose->ops = &rtas_pci_ops;
304 }
305
306 pci_process_bridge_OF_ranges(hose, dev, index == 0);
307
308 /* check the first bridge for a property that we can
309 use to set pci_dram_offset */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000310 dma = of_get_property(dev, "ibm,dma-ranges", &len);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000311 if (index == 0 && dma != NULL && len >= 6 * sizeof(*dma)) {
312 pci_dram_offset = dma[2] - dma[3];
313 printk("pci_dram_offset = %lx\n", pci_dram_offset);
314 }
315 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000316 of_node_put(root);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000317}
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000318
319/* SL82C105 IDE Control/Status Register */
320#define SL82C105_IDECSR 0x40
321
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +1100322/* Fixup for Winbond ATA quirk, required for briq mostly because the
323 * 8259 is configured for level sensitive IRQ 14 and so wants the
324 * ATA controller to be set to fully native mode or bad things
325 * will happen.
326 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800327static void chrp_pci_fixup_winbond_ata(struct pci_dev *sl82c105)
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000328{
329 u8 progif;
330
331 /* If non-briq machines need that fixup too, please speak up */
332 if (!machine_is(chrp) || _chrp_type != _CHRP_briq)
333 return;
334
335 if ((sl82c105->class & 5) != 5) {
336 printk("W83C553: Switching SL82C105 IDE to PCI native mode\n");
337 /* Enable SL82C105 PCI native IDE mode */
338 pci_read_config_byte(sl82c105, PCI_CLASS_PROG, &progif);
339 pci_write_config_byte(sl82c105, PCI_CLASS_PROG, progif | 0x05);
340 sl82c105->class |= 0x05;
341 /* Disable SL82C105 second port */
342 pci_write_config_word(sl82c105, SL82C105_IDECSR, 0x0003);
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +1100343 /* Clear IO BARs, they will be reassigned */
344 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_0, 0);
345 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_1, 0);
346 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_2, 0);
347 pci_write_config_dword(sl82c105, PCI_BASE_ADDRESS_3, 0);
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +1000348 }
349}
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +1100350DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105,
351 chrp_pci_fixup_winbond_ata);
Olaf Hering556ecf92007-08-18 04:27:17 +1000352
353/* Pegasos2 firmware version 20040810 configures the built-in IDE controller
354 * in legacy mode, but sets the PCI registers to PCI native mode.
355 * The chip can only operate in legacy mode, so force the PCI class into legacy
356 * mode as well. The same fixup must be done to the class-code property in
357 * the IDE node /pci@80000000/ide@C,1
358 */
Olaf Hering092ca5b2008-01-21 20:39:30 +1100359static void chrp_pci_fixup_vt8231_ata(struct pci_dev *viaide)
Olaf Hering556ecf92007-08-18 04:27:17 +1000360{
361 u8 progif;
362 struct pci_dev *viaisa;
363
364 if (!machine_is(chrp) || _chrp_type != _CHRP_Pegasos)
365 return;
366 if (viaide->irq != 14)
367 return;
368
369 viaisa = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231, NULL);
370 if (!viaisa)
371 return;
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200372 dev_info(&viaide->dev, "Fixing VIA IDE, force legacy mode on\n");
Olaf Hering556ecf92007-08-18 04:27:17 +1000373
374 pci_read_config_byte(viaide, PCI_CLASS_PROG, &progif);
375 pci_write_config_byte(viaide, PCI_CLASS_PROG, progif & ~0x5);
376 viaide->class &= ~0x5;
377
378 pci_dev_put(viaisa);
379}
Olaf Hering092ca5b2008-01-21 20:39:30 +1100380DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_1, chrp_pci_fixup_vt8231_ata);