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>
29 #include <asm/byteorder.h>
32 #include <asm/uaccess.h>
33 #include <asm/machdep.h>
34 #include <asm/pci-bridge.h>
35 #include <asm/tsi108.h>
36 #include <asm/tsi108_irq.h>
41 #define DBG(x...) printk(x)
46 #define tsi_mk_config_addr(bus, devfunc, offset) \
47 ((((bus)<<16) | ((devfunc)<<8) | (offset & 0xfc)) + tsi108_pci_cfg_base)
49 u32 tsi108_pci_cfg_base
;
50 u32 tsi108_csr_vir_base
;
52 extern u32
get_vir_csrbase(void);
53 extern u32
tsi108_read_reg(u32 reg_offset
);
54 extern void tsi108_write_reg(u32 reg_offset
, u32 val
);
57 tsi108_direct_write_config(struct pci_bus
*bus
, unsigned int devfunc
,
58 int offset
, int len
, u32 val
)
60 volatile unsigned char *cfg_addr
;
62 if (ppc_md
.pci_exclude_device
)
63 if (ppc_md
.pci_exclude_device(bus
->number
, devfunc
))
64 return PCIBIOS_DEVICE_NOT_FOUND
;
66 cfg_addr
= (unsigned char *)(tsi_mk_config_addr(bus
->number
,
71 printk("PCI CFG write : ");
72 printk("%d:0x%x:0x%x ", bus
->number
, devfunc
, offset
);
73 printk("%d ADDR=0x%08x ", len
, (uint
) cfg_addr
);
74 printk("data = 0x%08x\n", val
);
79 out_8((u8
*) cfg_addr
, val
);
82 out_le16((u16
*) cfg_addr
, val
);
85 out_le32((u32
*) cfg_addr
, val
);
89 return PCIBIOS_SUCCESSFUL
;
92 void tsi108_clear_pci_error(u32 pci_cfg_base
)
94 u32 err_stat
, err_addr
, pci_stat
;
97 * Quietly clear PB and PCI error flags set as result
98 * of PCI/X configuration read requests.
101 /* Read PB Error Log Registers */
103 err_stat
= tsi108_read_reg(TSI108_PB_OFFSET
+ TSI108_PB_ERRCS
);
104 err_addr
= tsi108_read_reg(TSI108_PB_OFFSET
+ TSI108_PB_AERR
);
106 if (err_stat
& TSI108_PB_ERRCS_ES
) {
107 /* Clear error flag */
108 tsi108_write_reg(TSI108_PB_OFFSET
+ TSI108_PB_ERRCS
,
111 /* Clear read error reported in PB_ISR */
112 tsi108_write_reg(TSI108_PB_OFFSET
+ TSI108_PB_ISR
,
113 TSI108_PB_ISR_PBS_RD_ERR
);
115 /* Clear PCI/X bus cfg errors if applicable */
116 if ((err_addr
& 0xFF000000) == pci_cfg_base
) {
118 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_CSR
);
119 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_CSR
,
127 #define __tsi108_read_pci_config(x, addr, op) \
128 __asm__ __volatile__( \
132 ".section .fixup,\"ax\"\n" \
135 ".section __ex_table,\"a\"\n" \
139 : "=r"(x) : "r"(addr))
142 tsi108_direct_read_config(struct pci_bus
*bus
, unsigned int devfn
, int offset
,
145 volatile unsigned char *cfg_addr
;
148 if (ppc_md
.pci_exclude_device
)
149 if (ppc_md
.pci_exclude_device(bus
->number
, devfn
))
150 return PCIBIOS_DEVICE_NOT_FOUND
;
152 cfg_addr
= (unsigned char *)(tsi_mk_config_addr(bus
->number
,
159 __tsi108_read_pci_config(temp
, cfg_addr
, "lbzx");
162 __tsi108_read_pci_config(temp
, cfg_addr
, "lhbrx");
165 __tsi108_read_pci_config(temp
, cfg_addr
, "lwbrx");
172 if ((0xFFFFFFFF != temp
) && (0xFFFF != temp
) && (0xFF != temp
)) {
173 printk("PCI CFG read : ");
174 printk("%d:0x%x:0x%x ", bus
->number
, devfn
, offset
);
175 printk("%d ADDR=0x%08x ", len
, (uint
) cfg_addr
);
176 printk("data = 0x%x\n", *val
);
179 return PCIBIOS_SUCCESSFUL
;
182 void tsi108_clear_pci_cfg_error(void)
184 tsi108_clear_pci_error(TSI108_PCI_CFG_BASE_PHYS
);
187 static struct pci_ops tsi108_direct_pci_ops
= {
188 tsi108_direct_read_config
,
189 tsi108_direct_write_config
192 int __init
tsi108_setup_pci(struct device_node
*dev
)
195 struct pci_controller
*hose
;
196 struct resource rsrc
;
197 const int *bus_range
;
198 int primary
= 0, has_address
= 0;
200 /* PCI Config mapping */
201 tsi108_pci_cfg_base
= (u32
)ioremap(TSI108_PCI_CFG_BASE_PHYS
,
202 TSI108_PCI_CFG_SIZE
);
203 DBG("TSI_PCI: %s tsi108_pci_cfg_base=0x%x\n", __FUNCTION__
,
204 tsi108_pci_cfg_base
);
206 /* Fetch host bridge registers address */
207 has_address
= (of_address_to_resource(dev
, 0, &rsrc
) == 0);
209 /* Get bus range if any */
210 bus_range
= get_property(dev
, "bus-range", &len
);
211 if (bus_range
== NULL
|| len
< 2 * sizeof(int)) {
212 printk(KERN_WARNING
"Can't get bus-range for %s, assume"
213 " bus 0\n", dev
->full_name
);
216 hose
= pcibios_alloc_controller();
219 printk("PCI Host bridge init failed\n");
222 hose
->arch_data
= dev
;
223 hose
->set_cfg_type
= 1;
225 hose
->first_busno
= bus_range
? bus_range
[0] : 0;
226 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
228 (hose
)->ops
= &tsi108_direct_pci_ops
;
230 printk(KERN_INFO
"Found tsi108 PCI host bridge at 0x%08x. "
231 "Firmware bus number: %d->%d\n",
232 rsrc
.start
, hose
->first_busno
, hose
->last_busno
);
234 /* Interpret the "ranges" property */
235 /* This also maps the I/O region and sets isa_io/mem_base */
236 pci_process_bridge_OF_ranges(hose
, dev
, primary
);
241 * Low level utility functions
244 static void tsi108_pci_int_mask(u_int irq
)
247 int int_line
= (irq
- IRQ_PCI_INTAD_BASE
);
249 irp_cfg
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
251 irp_cfg
|= (1 << int_line
); /* INTx_DIR = output */
252 irp_cfg
&= ~(3 << (8 + (int_line
* 2))); /* INTx_TYPE = unused */
253 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
, irp_cfg
);
255 irp_cfg
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
258 static void tsi108_pci_int_unmask(u_int irq
)
261 int int_line
= (irq
- IRQ_PCI_INTAD_BASE
);
263 irp_cfg
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
265 irp_cfg
&= ~(1 << int_line
);
266 irp_cfg
|= (3 << (8 + (int_line
* 2)));
267 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
, irp_cfg
);
271 static void init_pci_source(void)
273 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
,
275 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
,
276 TSI108_PCI_IRP_ENABLE_P_INT
);
280 static inline unsigned int get_pci_source(void)
288 /* Read PCI/X block interrupt status register */
289 pci_irp_stat
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_STAT
);
292 if (pci_irp_stat
& TSI108_PCI_IRP_STAT_P_INT
) {
293 /* Process Interrupt from PCI bus INTA# - INTD# lines */
295 tsi108_read_reg(TSI108_PCI_OFFSET
+
296 TSI108_PCI_IRP_INTAD
) & 0xf;
298 for (i
= 0; i
< 4; i
++, mask
++) {
299 if (temp
& (1 << mask
% 4)) {
300 irq
= IRQ_PCI_INTA
+ mask
% 4;
306 /* Disable interrupts from PCI block */
307 temp
= tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
);
308 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
,
309 temp
& ~TSI108_PCI_IRP_ENABLE_P_INT
);
311 (void)tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
);
316 printk("TSI108_PIC: error in TSI108_PCI_IRP_STAT\n");
318 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_STAT
);
320 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_INTAD
);
322 printk(">> stat=0x%08x intad=0x%08x ", pci_irp_stat
, temp
);
324 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_CFG_CTL
);
326 printk("cfg_ctl=0x%08x ", temp
);
328 tsi108_read_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
);
330 printk("irp_enable=0x%08x\n", temp
);
332 #endif /* end of DEBUG */
339 * Linux descriptor level callbacks
342 static void tsi108_pci_irq_enable(u_int irq
)
344 tsi108_pci_int_unmask(irq
);
347 static void tsi108_pci_irq_disable(u_int irq
)
349 tsi108_pci_int_mask(irq
);
352 static void tsi108_pci_irq_ack(u_int irq
)
354 tsi108_pci_int_mask(irq
);
357 static void tsi108_pci_irq_end(u_int irq
)
359 tsi108_pci_int_unmask(irq
);
361 /* Enable interrupts from PCI block */
362 tsi108_write_reg(TSI108_PCI_OFFSET
+ TSI108_PCI_IRP_ENABLE
,
363 tsi108_read_reg(TSI108_PCI_OFFSET
+
364 TSI108_PCI_IRP_ENABLE
) |
365 TSI108_PCI_IRP_ENABLE_P_INT
);
370 * Interrupt controller descriptor for cascaded PCI interrupt controller.
373 static struct irq_chip tsi108_pci_irq
= {
374 .typename
= "tsi108_PCI_int",
375 .mask
= tsi108_pci_irq_disable
,
376 .ack
= tsi108_pci_irq_ack
,
377 .end
= tsi108_pci_irq_end
,
378 .unmask
= tsi108_pci_irq_enable
,
386 * The Tsi108 PCI interrupts initialization routine.
388 * The INTA# - INTD# interrupts on the PCI bus are reported by the PCI block
389 * to the MPIC using single interrupt source (IRQ_TSI108_PCI). Therefore the
390 * PCI block has to be treated as a cascaded interrupt controller connected
394 void __init
tsi108_pci_int_init(void)
398 DBG("Tsi108_pci_int_init: initializing PCI interrupts\n");
400 for (i
= 0; i
< NUM_PCI_IRQS
; i
++) {
401 irq_desc
[i
+ IRQ_PCI_INTAD_BASE
].chip
= &tsi108_pci_irq
;
402 irq_desc
[i
+ IRQ_PCI_INTAD_BASE
].status
|= IRQ_LEVEL
;
408 void tsi108_irq_cascade(unsigned int irq
, struct irq_desc
*desc
)
410 unsigned int cascade_irq
= get_pci_source();
411 if (cascade_irq
!= NO_IRQ
)
412 generic_handle_irq(cascade_irq
);
413 desc
->chip
->eoi(irq
);