GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / powerpc / sysdev / tsi108_pci.c
1 /*
2  * Common routines for Tundra Semiconductor TSI108 host bridge.
3  *
4  * 2004-2005 (c) Tundra Semiconductor Corp.
5  * Author: Alex Bounine (alexandreb@tundra.com)
6  * Author: Roy Zang (tie-fei.zang@freescale.com)
7  *         Add pci interrupt router host
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/pci.h>
27 #include <linux/irq.h>
28 #include <linux/interrupt.h>
29
30 #include <asm/byteorder.h>
31 #include <asm/io.h>
32 #include <asm/irq.h>
33 #include <linux/uaccess.h>
34 #include <asm/machdep.h>
35 #include <asm/pci-bridge.h>
36 #include <asm/tsi108.h>
37 #include <asm/tsi108_pci.h>
38 #include <asm/tsi108_irq.h>
39 #include <asm/prom.h>
40
41 #undef DEBUG
42 #ifdef DEBUG
43 #define DBG(x...) printk(x)
44 #else
45 #define DBG(x...)
46 #endif
47
48 #define tsi_mk_config_addr(bus, devfunc, offset) \
49         ((((bus)<<16) | ((devfunc)<<8) | (offset & 0xfc)) + tsi108_pci_cfg_base)
50
51 u32 tsi108_pci_cfg_base;
52 static u32 tsi108_pci_cfg_phys;
53 u32 tsi108_csr_vir_base;
54 static struct irq_domain *pci_irq_host;
55
56 extern u32 get_vir_csrbase(void);
57 extern u32 tsi108_read_reg(u32 reg_offset);
58 extern void tsi108_write_reg(u32 reg_offset, u32 val);
59
60 int
61 tsi108_direct_write_config(struct pci_bus *bus, unsigned int devfunc,
62                            int offset, int len, u32 val)
63 {
64         volatile unsigned char *cfg_addr;
65         struct pci_controller *hose = pci_bus_to_host(bus);
66
67         if (ppc_md.pci_exclude_device)
68                 if (ppc_md.pci_exclude_device(hose, bus->number, devfunc))
69                         return PCIBIOS_DEVICE_NOT_FOUND;
70
71         cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
72                                                         devfunc, offset) |
73                                                         (offset & 0x03));
74
75 #ifdef DEBUG
76         printk("PCI CFG write : ");
77         printk("%d:0x%x:0x%x ", bus->number, devfunc, offset);
78         printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
79         printk("data = 0x%08x\n", val);
80 #endif
81
82         switch (len) {
83         case 1:
84                 out_8((u8 *) cfg_addr, val);
85                 break;
86         case 2:
87                 out_le16((u16 *) cfg_addr, val);
88                 break;
89         default:
90                 out_le32((u32 *) cfg_addr, val);
91                 break;
92         }
93
94         return PCIBIOS_SUCCESSFUL;
95 }
96
97 void tsi108_clear_pci_error(u32 pci_cfg_base)
98 {
99         u32 err_stat, err_addr, pci_stat;
100
101         /*
102          * Quietly clear PB and PCI error flags set as result
103          * of PCI/X configuration read requests.
104          */
105
106         /* Read PB Error Log Registers */
107
108         err_stat = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS);
109         err_addr = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_AERR);
110
111         if (err_stat & TSI108_PB_ERRCS_ES) {
112                 /* Clear error flag */
113                 tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS,
114                                  TSI108_PB_ERRCS_ES);
115
116                 /* Clear read error reported in PB_ISR */
117                 tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ISR,
118                                  TSI108_PB_ISR_PBS_RD_ERR);
119
120                 /* Clear PCI/X bus cfg errors if applicable */
121                 if ((err_addr & 0xFF000000) == pci_cfg_base) {
122                         pci_stat =
123                             tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR);
124                         tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR,
125                                          pci_stat);
126                 }
127         }
128
129         return;
130 }
131
132 #define __tsi108_read_pci_config(x, addr, op)           \
133         __asm__ __volatile__(                           \
134                 "       "op" %0,0,%1\n"         \
135                 "1:     eieio\n"                        \
136                 "2:\n"                                  \
137                 ".section .fixup,\"ax\"\n"              \
138                 "3:     li %0,-1\n"                     \
139                 "       b 2b\n"                         \
140                 ".previous\n"                           \
141                 EX_TABLE(1b, 3b)                        \
142                 : "=r"(x) : "r"(addr))
143
144 int
145 tsi108_direct_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
146                           int len, u32 * val)
147 {
148         volatile unsigned char *cfg_addr;
149         struct pci_controller *hose = pci_bus_to_host(bus);
150         u32 temp;
151
152         if (ppc_md.pci_exclude_device)
153                 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
154                         return PCIBIOS_DEVICE_NOT_FOUND;
155
156         cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
157                                                         devfn,
158                                                         offset) | (offset &
159                                                                    0x03));
160
161         switch (len) {
162         case 1:
163                 __tsi108_read_pci_config(temp, cfg_addr, "lbzx");
164                 break;
165         case 2:
166                 __tsi108_read_pci_config(temp, cfg_addr, "lhbrx");
167                 break;
168         default:
169                 __tsi108_read_pci_config(temp, cfg_addr, "lwbrx");
170                 break;
171         }
172
173         *val = temp;
174
175 #ifdef DEBUG
176         if ((0xFFFFFFFF != temp) && (0xFFFF != temp) && (0xFF != temp)) {
177                 printk("PCI CFG read : ");
178                 printk("%d:0x%x:0x%x ", bus->number, devfn, offset);
179                 printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
180                 printk("data = 0x%x\n", *val);
181         }
182 #endif
183         return PCIBIOS_SUCCESSFUL;
184 }
185
186 void tsi108_clear_pci_cfg_error(void)
187 {
188         tsi108_clear_pci_error(tsi108_pci_cfg_phys);
189 }
190
191 static struct pci_ops tsi108_direct_pci_ops = {
192         .read = tsi108_direct_read_config,
193         .write = tsi108_direct_write_config,
194 };
195
196 int __init tsi108_setup_pci(struct device_node *dev, u32 cfg_phys, int primary)
197 {
198         int len;
199         struct pci_controller *hose;
200         struct resource rsrc;
201         const int *bus_range;
202         int has_address = 0;
203
204         /* PCI Config mapping */
205         tsi108_pci_cfg_base = (u32)ioremap(cfg_phys, TSI108_PCI_CFG_SIZE);
206         tsi108_pci_cfg_phys = cfg_phys;
207         DBG("TSI_PCI: %s tsi108_pci_cfg_base=0x%x\n", __func__,
208             tsi108_pci_cfg_base);
209
210         /* Fetch host bridge registers address */
211         has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
212
213         /* Get bus range if any */
214         bus_range = of_get_property(dev, "bus-range", &len);
215         if (bus_range == NULL || len < 2 * sizeof(int)) {
216                 printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
217                        " bus 0\n", dev);
218         }
219
220         hose = pcibios_alloc_controller(dev);
221
222         if (!hose) {
223                 printk("PCI Host bridge init failed\n");
224                 return -ENOMEM;
225         }
226
227         hose->first_busno = bus_range ? bus_range[0] : 0;
228         hose->last_busno = bus_range ? bus_range[1] : 0xff;
229
230         (hose)->ops = &tsi108_direct_pci_ops;
231
232         pr_info("Found tsi108 PCI host bridge at 0x%pa. Firmware bus number: %d->%d\n",
233                 &rsrc.start, hose->first_busno, hose->last_busno);
234
235         /* Interpret the "ranges" property */
236         /* This also maps the I/O region and sets isa_io/mem_base */
237         pci_process_bridge_OF_ranges(hose, dev, primary);
238         return 0;
239 }
240
241 /*
242  * Low level utility functions
243  */
244
245 static void tsi108_pci_int_mask(u_int irq)
246 {
247         u_int irp_cfg;
248         int int_line = (irq - IRQ_PCI_INTAD_BASE);
249
250         irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
251         mb();
252         irp_cfg |= (1 << int_line);     /* INTx_DIR = output */
253         irp_cfg &= ~(3 << (8 + (int_line * 2)));        /* INTx_TYPE = unused */
254         tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
255         mb();
256         irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
257 }
258
259 static void tsi108_pci_int_unmask(u_int irq)
260 {
261         u_int irp_cfg;
262         int int_line = (irq - IRQ_PCI_INTAD_BASE);
263
264         irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
265         mb();
266         irp_cfg &= ~(1 << int_line);
267         irp_cfg |= (3 << (8 + (int_line * 2)));
268         tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
269         mb();
270 }
271
272 static void init_pci_source(void)
273 {
274         tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL,
275                         0x0000ff00);
276         tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
277                         TSI108_PCI_IRP_ENABLE_P_INT);
278         mb();
279 }
280
281 static inline unsigned int get_pci_source(void)
282 {
283         u_int temp = 0;
284         int irq = -1;
285         int i;
286         u_int pci_irp_stat;
287         static int mask = 0;
288
289         /* Read PCI/X block interrupt status register */
290         pci_irp_stat = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
291         mb();
292
293         if (pci_irp_stat & TSI108_PCI_IRP_STAT_P_INT) {
294                 /* Process Interrupt from PCI bus INTA# - INTD# lines */
295                 temp =
296                     tsi108_read_reg(TSI108_PCI_OFFSET +
297                                     TSI108_PCI_IRP_INTAD) & 0xf;
298                 mb();
299                 for (i = 0; i < 4; i++, mask++) {
300                         if (temp & (1 << mask % 4)) {
301                                 irq = IRQ_PCI_INTA + mask % 4;
302                                 mask++;
303                                 break;
304                         }
305                 }
306
307                 /* Disable interrupts from PCI block */
308                 temp = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
309                 tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
310                                 temp & ~TSI108_PCI_IRP_ENABLE_P_INT);
311                 mb();
312                 (void)tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
313                 mb();
314         }
315 #ifdef DEBUG
316         else {
317                 printk("TSI108_PIC: error in TSI108_PCI_IRP_STAT\n");
318                 pci_irp_stat =
319                     tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
320                 temp =
321                     tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_INTAD);
322                 mb();
323                 printk(">> stat=0x%08x intad=0x%08x ", pci_irp_stat, temp);
324                 temp =
325                     tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
326                 mb();
327                 printk("cfg_ctl=0x%08x ", temp);
328                 temp =
329                     tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
330                 mb();
331                 printk("irp_enable=0x%08x\n", temp);
332         }
333 #endif  /* end of DEBUG */
334
335         return irq;
336 }
337
338
339 /*
340  * Linux descriptor level callbacks
341  */
342
343 static void tsi108_pci_irq_unmask(struct irq_data *d)
344 {
345         tsi108_pci_int_unmask(d->irq);
346
347         /* Enable interrupts from PCI block */
348         tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
349                          tsi108_read_reg(TSI108_PCI_OFFSET +
350                                          TSI108_PCI_IRP_ENABLE) |
351                          TSI108_PCI_IRP_ENABLE_P_INT);
352         mb();
353 }
354
355 static void tsi108_pci_irq_mask(struct irq_data *d)
356 {
357         tsi108_pci_int_mask(d->irq);
358 }
359
360 static void tsi108_pci_irq_ack(struct irq_data *d)
361 {
362         tsi108_pci_int_mask(d->irq);
363 }
364
365 /*
366  * Interrupt controller descriptor for cascaded PCI interrupt controller.
367  */
368
369 static struct irq_chip tsi108_pci_irq = {
370         .name = "tsi108_PCI_int",
371         .irq_mask = tsi108_pci_irq_mask,
372         .irq_ack = tsi108_pci_irq_ack,
373         .irq_unmask = tsi108_pci_irq_unmask,
374 };
375
376 static int pci_irq_host_xlate(struct irq_domain *h, struct device_node *ct,
377                             const u32 *intspec, unsigned int intsize,
378                             irq_hw_number_t *out_hwirq, unsigned int *out_flags)
379 {
380         *out_hwirq = intspec[0];
381         *out_flags = IRQ_TYPE_LEVEL_HIGH;
382         return 0;
383 }
384
385 static int pci_irq_host_map(struct irq_domain *h, unsigned int virq,
386                           irq_hw_number_t hw)
387 {       unsigned int irq;
388         DBG("%s(%d, 0x%lx)\n", __func__, virq, hw);
389         if ((virq >= 1) && (virq <= 4)){
390                 irq = virq + IRQ_PCI_INTAD_BASE - 1;
391                 irq_set_status_flags(irq, IRQ_LEVEL);
392                 irq_set_chip(irq, &tsi108_pci_irq);
393         }
394         return 0;
395 }
396
397 static const struct irq_domain_ops pci_irq_domain_ops = {
398         .map = pci_irq_host_map,
399         .xlate = pci_irq_host_xlate,
400 };
401
402 /*
403  * Exported functions
404  */
405
406 /*
407  * The Tsi108 PCI interrupts initialization routine.
408  *
409  * The INTA# - INTD# interrupts on the PCI bus are reported by the PCI block
410  * to the MPIC using single interrupt source (IRQ_TSI108_PCI). Therefore the
411  * PCI block has to be treated as a cascaded interrupt controller connected
412  * to the MPIC.
413  */
414
415 void __init tsi108_pci_int_init(struct device_node *node)
416 {
417         DBG("Tsi108_pci_int_init: initializing PCI interrupts\n");
418
419         pci_irq_host = irq_domain_add_legacy_isa(node, &pci_irq_domain_ops, NULL);
420         if (pci_irq_host == NULL) {
421                 printk(KERN_ERR "pci_irq_host: failed to allocate irq domain!\n");
422                 return;
423         }
424
425         init_pci_source();
426 }
427
428 void tsi108_irq_cascade(struct irq_desc *desc)
429 {
430         struct irq_chip *chip = irq_desc_get_chip(desc);
431         unsigned int cascade_irq = get_pci_source();
432
433         if (cascade_irq)
434                 generic_handle_irq(cascade_irq);
435
436         chip->irq_eoi(&desc->irq_data);
437 }