1 /*
2  * Support for indirect PCI bridges.
3  *
4  * Copyright (C) 1998 Gabriel Paubert.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/init.h>
17 #include <linux/bootmem.h>
18 
19 #include <asm/io.h>
20 #include <asm/prom.h>
21 #include <asm/pci-bridge.h>
22 #include <asm/machdep.h>
23 
24 #define cfg_read(val, addr, type, op)	*val = op((type)(addr))
25 #define cfg_write(val, addr, type, op)	op((type *)(addr), (val))
26 
27 #ifdef CONFIG_PPC_INDIRECT_PCI_BE
28 #define PCI_CFG_OUT out_be32
29 #else
30 #define PCI_CFG_OUT out_le32
31 #endif
32 
33 #define INDIRECT_PCI_OP(rw, size, type, op, mask)			 \
34 static int								 \
35 indirect_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
36 {									 \
37 	struct pci_controller *hose = dev->sysdata;			 \
38 	u8 cfg_type = 0;						 \
39 									 \
40 	if (ppc_md.pci_exclude_device)					 \
41 		if (ppc_md.pci_exclude_device(dev->bus->number, dev->devfn)) \
42 			return PCIBIOS_DEVICE_NOT_FOUND;		 \
43 									 \
44 	if (hose->set_cfg_type)					 	 \
45 		if (dev->bus->number != hose->first_busno)		 \
46 			cfg_type = 1;					 \
47 									 \
48 	PCI_CFG_OUT(hose->cfg_addr, 					 \
49 		 (0x80000000 | ((dev->bus->number - hose->bus_offset) << 16) \
50 		  | (dev->devfn << 8) | ((offset & 0xfc) | cfg_type)));	 \
51 	cfg_##rw(val, hose->cfg_data + (offset & mask), type, op);	 \
52 	return PCIBIOS_SUCCESSFUL;    					 \
53 }
54 
55 INDIRECT_PCI_OP(read, byte, u8 *, in_8, 3)
56 INDIRECT_PCI_OP(read, word, u16 *, in_le16, 2)
57 INDIRECT_PCI_OP(read, dword, u32 *, in_le32, 0)
58 INDIRECT_PCI_OP(write, byte, u8, out_8, 3)
59 INDIRECT_PCI_OP(write, word, u16, out_le16, 2)
60 INDIRECT_PCI_OP(write, dword, u32, out_le32, 0)
61 
62 static struct pci_ops indirect_pci_ops =
63 {
64 	indirect_read_config_byte,
65 	indirect_read_config_word,
66 	indirect_read_config_dword,
67 	indirect_write_config_byte,
68 	indirect_write_config_word,
69 	indirect_write_config_dword
70 };
71 
72 void __init
setup_indirect_pci(struct pci_controller * hose,u32 cfg_addr,u32 cfg_data)73 setup_indirect_pci(struct pci_controller* hose, u32 cfg_addr, u32 cfg_data)
74 {
75 	hose->ops = &indirect_pci_ops;
76 	hose->cfg_addr = (unsigned int *) ioremap(cfg_addr, 4);
77 	hose->cfg_data = (unsigned char *) ioremap(cfg_data, 4);
78 }
79 
80