2 * Common routines for Tundra Semiconductor TSI108 host bridge.
4 * 2004-2005 (c) Tundra Semiconductor Corp.
5 * Author: Alex Bounine (alexandreb@tundra.com)
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/slab.h>
26 #include <linux/irq.h>
27 #include <linux/interrupt.h>
30 #include <asm/byteorder.h>
33 #include <asm/uaccess.h>
34 #include <asm/machdep.h>
35 #include <asm/pci-bridge.h>
36 #include <asm/tsi108.h>
37 #include <asm/tsi108_irq.h>
42 #define DBG(x...) printk(x)
47 #define tsi_mk_config_addr(bus, devfunc, offset) \
48 ((((bus)<<16) | ((devfunc)<<8) | (offset & 0xfc)) + tsi108_pci_cfg_base)
50 u32 tsi108_pci_cfg_base
;
51 u32 tsi108_csr_vir_base
;
53 extern u32
get_vir_csrbase(void);
54 extern u32
tsi108_read_reg(u32 reg_offset
);
55 extern void tsi108_write_reg(u32 reg_offset
, u32 val
);
58 tsi108_direct_write_config(struct pci_bus
*bus
, unsigned int devfunc
,
59 int offset
, int len
, u32 val
)
61 volatile unsigned char *cfg_addr
;
63 if (ppc_md
.pci_exclude_device
)
64 if (ppc_md
.pci_exclude_device(bus
->number
, devfunc
))
65 return PCIBIOS_DEVICE_NOT_FOUND
;
67 cfg_addr
= (unsigned char *)(tsi_mk_config_addr(bus
->number
,
72 printk("PCI CFG write : ");
73 printk("%d:0x%x:0x%x ", bus
->number
, devfunc
, offset
);
74 printk("%d ADDR=0x%08x ", len
, (uint
) cfg_addr
);
75 printk("data = 0x%08x\n", val
);
80 out_8((u8
*) cfg_addr
, val
);
83 out_le16((u16
*) cfg_addr
, val
);
86 out_le32((u32
*) cfg_addr
, val
);
90 return PCIBIOS_SUCCESSFUL
;
93 void tsi108_clear_pci_error(u32 pci_cfg_base
)
95 u32 err_stat
, err_addr
, pci_stat
;
98 * Quietly clear PB and PCI error flags set as result
99 * of PCI/X configuration read requests.
102 /* Read PB Error Log Registers */
104 err_stat
= tsi108_read_reg(TSI108_PB_OFFSET
+ TSI108_PB_ERRCS
);
105 err_addr
= tsi108_read_reg(TSI108_PB_OFFSET
+ TSI108_PB_AERR
);
107 if (err_stat
& TSI108_PB_ERRCS_ES
) {
108 /* Clear error flag */
109 tsi108_write_reg(TSI108_PB_OFFSET
+ TSI108_PB_ERRCS
,
112 /* Clear read error reported in PB_ISR */
113 tsi108_write_reg(TSI108_PB_OFFSET
+ TSI108_PB_ISR
,
114 TSI108_PB_ISR_PBS_RD_ERR
);
116 /* Clear PCI/X bus cfg errors if applicable */
117 if ((err_addr
& 0xFF000000) == pci_cfg_base
) {
119 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_CSR
);
120 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_CSR
,
128 #define __tsi108_read_pci_config(x, addr, op) \
129 __asm__ __volatile__( \
133 ".section .fixup,\"ax\"\n" \
136 ".section __ex_table,\"a\"\n" \
140 : "=r"(x) : "r"(addr))
143 tsi108_direct_read_config(struct pci_bus
*bus
, unsigned int devfn
, int offset
,
146 volatile unsigned char *cfg_addr
;
149 if (ppc_md
.pci_exclude_device
)
150 if (ppc_md
.pci_exclude_device(bus
->number
, devfn
))
151 return PCIBIOS_DEVICE_NOT_FOUND
;
153 cfg_addr
= (unsigned char *)(tsi_mk_config_addr(bus
->number
,
160 __tsi108_read_pci_config(temp
, cfg_addr
, "lbzx");
163 __tsi108_read_pci_config(temp
, cfg_addr
, "lhbrx");
166 __tsi108_read_pci_config(temp
, cfg_addr
, "lwbrx");
173 if ((0xFFFFFFFF != temp
) && (0xFFFF != temp
) && (0xFF != temp
)) {
174 printk("PCI CFG read : ");
175 printk("%d:0x%x:0x%x ", bus
->number
, devfn
, offset
);
176 printk("%d ADDR=0x%08x ", len
, (uint
) cfg_addr
);
177 printk("data = 0x%x\n", *val
);
180 return PCIBIOS_SUCCESSFUL
;
183 void tsi108_clear_pci_cfg_error(void)
185 tsi108_clear_pci_error(TSI108_PCI_CFG_BASE_PHYS
);
188 static struct pci_ops tsi108_direct_pci_ops
= {
189 tsi108_direct_read_config
,
190 tsi108_direct_write_config
193 int __init
tsi108_setup_pci(struct device_node
*dev
)
196 struct pci_controller
*hose
;
197 struct resource rsrc
;
199 int primary
= 0, has_address
= 0;
201 /* PCI Config mapping */
202 tsi108_pci_cfg_base
= (u32
)ioremap(TSI108_PCI_CFG_BASE_PHYS
,
203 TSI108_PCI_CFG_SIZE
);
204 DBG("TSI_PCI: %s tsi108_pci_cfg_base=0x%x\n", __FUNCTION__
,
205 tsi108_pci_cfg_base
);
207 /* Fetch host bridge registers address */
208 has_address
= (of_address_to_resource(dev
, 0, &rsrc
) == 0);
210 /* Get bus range if any */
211 bus_range
= (int *)get_property(dev
, "bus-range", &len
);
212 if (bus_range
== NULL
|| len
< 2 * sizeof(int)) {
213 printk(KERN_WARNING
"Can't get bus-range for %s, assume"
214 " bus 0\n", dev
->full_name
);
217 hose
= pcibios_alloc_controller();
220 printk("PCI Host bridge init failed\n");
223 hose
->arch_data
= dev
;
224 hose
->set_cfg_type
= 1;
226 hose
->first_busno
= bus_range
? bus_range
[0] : 0;
227 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
229 (hose
)->ops
= &tsi108_direct_pci_ops
;
231 printk(KERN_INFO
"Found tsi108 PCI host bridge at 0x%08lx. "
232 "Firmware bus number: %d->%d\n",
233 rsrc
.start
, hose
->first_busno
, hose
->last_busno
);
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
);
242 * Low level utility functions
245 static void tsi108_pci_int_mask(u_int irq
)
248 int int_line
= (irq
- IRQ_PCI_INTAD_BASE
);
250 irp_cfg
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
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
);
256 irp_cfg
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
259 static void tsi108_pci_int_unmask(u_int irq
)
262 int int_line
= (irq
- IRQ_PCI_INTAD_BASE
);
264 irp_cfg
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
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
);
272 static void init_pci_source(void)
274 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
,
276 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
,
277 TSI108_PCI_IRP_ENABLE_P_INT
);
281 static inline int get_pci_source(void)
289 /* Read PCI/X block interrupt status register */
290 pci_irp_stat
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_STAT
);
293 if (pci_irp_stat
& TSI108_PCI_IRP_STAT_P_INT
) {
294 /* Process Interrupt from PCI bus INTA# - INTD# lines */
296 tsi108_read_reg(TSI108_PCI_OFFSET
+
297 TSI108_PCI_IRP_INTAD
) & 0xf;
299 for (i
= 0; i
< 4; i
++, mask
++) {
300 if (temp
& (1 << mask
% 4)) {
301 irq
= IRQ_PCI_INTA
+ mask
% 4;
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
);
312 (void)tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
);
317 printk("TSI108_PIC: error in TSI108_PCI_IRP_STAT\n");
319 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_STAT
);
321 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_INTAD
);
323 printk(">> stat=0x%08x intad=0x%08x ", pci_irp_stat
, temp
);
325 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
327 printk("cfg_ctl=0x%08x ", temp
);
329 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
);
331 printk("irp_enable=0x%08x\n", temp
);
333 #endif /* end of DEBUG */
340 * Linux descriptor level callbacks
343 static void tsi108_pci_irq_enable(u_int irq
)
345 tsi108_pci_int_unmask(irq
);
348 static void tsi108_pci_irq_disable(u_int irq
)
350 tsi108_pci_int_mask(irq
);
353 static void tsi108_pci_irq_ack(u_int irq
)
355 tsi108_pci_int_mask(irq
);
358 static void tsi108_pci_irq_end(u_int irq
)
360 tsi108_pci_int_unmask(irq
);
362 /* Enable interrupts from PCI block */
363 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
,
364 tsi108_read_reg(TSI108_PCI_OFFSET
+
365 TSI108_PCI_IRP_ENABLE
) |
366 TSI108_PCI_IRP_ENABLE_P_INT
);
371 * Interrupt controller descriptor for cascaded PCI interrupt controller.
374 struct hw_interrupt_type tsi108_pci_irq
= {
375 .typename
= "tsi108_PCI_int",
376 .enable
= tsi108_pci_irq_enable
,
377 .disable
= tsi108_pci_irq_disable
,
378 .ack
= tsi108_pci_irq_ack
,
379 .end
= tsi108_pci_irq_end
,
387 * The Tsi108 PCI interrupts initialization routine.
389 * The INTA# - INTD# interrupts on the PCI bus are reported by the PCI block
390 * to the MPIC using single interrupt source (IRQ_TSI108_PCI). Therefore the
391 * PCI block has to be treated as a cascaded interrupt controller connected
395 void __init
tsi108_pci_int_init(void)
399 DBG("Tsi108_pci_int_init: initializing PCI interrupts\n");
401 for (i
= 0; i
< NUM_PCI_IRQS
; i
++) {
402 irq_desc
[i
+ IRQ_PCI_INTAD_BASE
].handler
= &tsi108_pci_irq
;
403 irq_desc
[i
+ IRQ_PCI_INTAD_BASE
].status
|= IRQ_LEVEL
;
409 int tsi108_irq_cascade(struct pt_regs
*regs
, void *unused
)
411 return get_pci_source();