1 /*
2  *
3  * SNI64 specific PCI support for SNI IO.
4  *
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License.  See the file "COPYING" in the main directory of this archive
7  * for more details.
8  *
9  * Copyright (c) 1997, 1998, 2000-2003 Silicon Graphics, Inc.  All rights reserved.
10  */
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/config.h>
14 #include <linux/pci.h>
15 #include <asm/sn/types.h>
16 #include <asm/sn/sgi.h>
17 #include <asm/sn/io.h>
18 #include <asm/sn/driver.h>
19 #include <asm/sn/iograph.h>
20 #include <asm/param.h>
21 #include <asm/sn/pio.h>
22 #include <asm/sn/xtalk/xwidget.h>
23 #include <asm/sn/sn_private.h>
24 #include <asm/sn/addrs.h>
25 #include <asm/sn/invent.h>
26 #include <asm/sn/hcl.h>
27 #include <asm/sn/hcl_util.h>
28 #include <asm/sn/pci/pciio.h>
29 #include <asm/sn/pci/pcibr.h>
30 #include <asm/sn/pci/pcibr_private.h>
31 #include <asm/sn/pci/bridge.h>
32 
33 #ifdef DEBUG_CONFIG
34 #define DBG(x...) printk(x)
35 #else
36 #define DBG(x...)
37 #endif
38 
39 
40 
41 #ifdef CONFIG_PCI
42 
43 extern vertex_hdl_t pci_bus_to_vertex(unsigned char);
44 extern vertex_hdl_t devfn_to_vertex(unsigned char bus, unsigned char devfn);
45 
46 /*
47  * snia64_read_config_byte - Read a byte from the config area of the device.
48  */
snia64_read_config_byte(struct pci_dev * dev,int where,unsigned char * val)49 static int snia64_read_config_byte (struct pci_dev *dev,
50                                    int where, unsigned char *val)
51 {
52 	unsigned long res = 0;
53 	unsigned size = 1;
54 	vertex_hdl_t device_vertex;
55 
56 	if ( (dev == (struct pci_dev *)0) || (val == (unsigned char *)0) ) {
57 		return PCIBIOS_DEVICE_NOT_FOUND;
58 	}
59 	device_vertex = devfn_to_vertex(dev->bus->number, dev->devfn);
60 	if (!device_vertex) {
61 		DBG("%s : nonexistent device: bus= 0x%x  slot= 0x%x  func= 0x%x\n",
62 		__FUNCTION__, dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
63 		return(-1);
64 	}
65 	res = pciio_config_get(device_vertex, (unsigned) where, size);
66 	*val = (unsigned char) res;
67 	return PCIBIOS_SUCCESSFUL;
68 }
69 
70 /*
71  * snia64_read_config_word - Read 2 bytes from the config area of the device.
72  */
snia64_read_config_word(struct pci_dev * dev,int where,unsigned short * val)73 static int snia64_read_config_word (struct pci_dev *dev,
74                                    int where, unsigned short *val)
75 {
76 	unsigned long res = 0;
77 	unsigned size = 2; /* 2 bytes */
78 	vertex_hdl_t device_vertex;
79 
80 	if ( (dev == (struct pci_dev *)0) || (val == (unsigned short *)0) ) {
81 		return PCIBIOS_DEVICE_NOT_FOUND;
82 	}
83 	device_vertex = devfn_to_vertex(dev->bus->number, dev->devfn);
84 	if (!device_vertex) {
85 		DBG("%s : nonexistent device: bus= 0x%x  slot= 0x%x  func= 0x%x\n",
86 		__FUNCTION__, dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
87 		return(-1);
88 	}
89 	res = pciio_config_get(device_vertex, (unsigned) where, size);
90 	*val = (unsigned short) res;
91 	return PCIBIOS_SUCCESSFUL;
92 }
93 
94 /*
95  * snia64_read_config_dword - Read 4 bytes from the config area of the device.
96  */
snia64_read_config_dword(struct pci_dev * dev,int where,unsigned int * val)97 static int snia64_read_config_dword (struct pci_dev *dev,
98                                     int where, unsigned int *val)
99 {
100 	unsigned long res = 0;
101 	unsigned size = 4; /* 4 bytes */
102 	vertex_hdl_t device_vertex;
103 
104 	if (where & 3) {
105 		return PCIBIOS_BAD_REGISTER_NUMBER;
106 	}
107 	if ( (dev == (struct pci_dev *)0) || (val == (unsigned int *)0) ) {
108 		return PCIBIOS_DEVICE_NOT_FOUND;
109 	}
110 
111 	device_vertex = devfn_to_vertex(dev->bus->number, dev->devfn);
112 	if (!device_vertex) {
113 		DBG("%s : nonexistent device: bus= 0x%x  slot= 0x%x  func= 0x%x\n",
114 		__FUNCTION__, dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
115 		return(-1);
116 	}
117 	res = pciio_config_get(device_vertex, (unsigned) where, size);
118 	*val = (unsigned int) res;
119 	return PCIBIOS_SUCCESSFUL;
120 }
121 
122 /*
123  * snia64_write_config_byte - Writes 1 byte to the config area of the device.
124  */
snia64_write_config_byte(struct pci_dev * dev,int where,unsigned char val)125 static int snia64_write_config_byte (struct pci_dev *dev,
126                                     int where, unsigned char val)
127 {
128 	vertex_hdl_t device_vertex;
129 
130 	if ( dev == (struct pci_dev *)0 ) {
131 		return PCIBIOS_DEVICE_NOT_FOUND;
132 	}
133 
134 	device_vertex = devfn_to_vertex(dev->bus->number, dev->devfn);
135 	if (!device_vertex) {
136 		DBG("%s : nonexistent device: bus= 0x%x  slot= 0x%x  func= 0x%x\n",
137 		__FUNCTION__, dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
138 		return(-1);
139 	}
140 	pciio_config_set( device_vertex, (unsigned)where, 1, (uint64_t) val);
141 
142 	return PCIBIOS_SUCCESSFUL;
143 }
144 
145 /*
146  * snia64_write_config_word - Writes 2 bytes to the config area of the device.
147  */
snia64_write_config_word(struct pci_dev * dev,int where,unsigned short val)148 static int snia64_write_config_word (struct pci_dev *dev,
149                                     int where, unsigned short val)
150 {
151 	vertex_hdl_t device_vertex = NULL;
152 
153 	if (where & 1) {
154 		return PCIBIOS_BAD_REGISTER_NUMBER;
155 	}
156 	if ( dev == (struct pci_dev *)0 ) {
157 		return PCIBIOS_DEVICE_NOT_FOUND;
158 	}
159 
160 	device_vertex = devfn_to_vertex(dev->bus->number, dev->devfn);
161 	if (!device_vertex) {
162 		DBG("%s : nonexistent device: bus= 0x%x  slot= 0x%x  func= 0x%x\n",
163 		__FUNCTION__, dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
164 		return(-1);
165 	}
166 	pciio_config_set( device_vertex, (unsigned)where, 2, (uint64_t) val);
167 
168 	return PCIBIOS_SUCCESSFUL;
169 }
170 
171 /*
172  * snia64_write_config_dword - Writes 4 bytes to the config area of the device.
173  */
snia64_write_config_dword(struct pci_dev * dev,int where,unsigned int val)174 static int snia64_write_config_dword (struct pci_dev *dev,
175                                      int where, unsigned int val)
176 {
177 	vertex_hdl_t device_vertex;
178 
179 	if (where & 3) {
180 		return PCIBIOS_BAD_REGISTER_NUMBER;
181 	}
182 	if ( dev == (struct pci_dev *)0 ) {
183 		return PCIBIOS_DEVICE_NOT_FOUND;
184 	}
185 
186 	device_vertex = devfn_to_vertex(dev->bus->number, dev->devfn);
187 	if (!device_vertex) {
188 		DBG("%s : nonexistent device: bus= 0x%x  slot= 0x%x  func= 0x%x\n",
189 		__FUNCTION__, dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
190 		return(-1);
191 	}
192 	pciio_config_set( device_vertex, (unsigned)where, 4, (uint64_t) val);
193 
194 	return PCIBIOS_SUCCESSFUL;
195 }
196 
197 static struct pci_ops snia64_pci_ops = {
198 	snia64_read_config_byte,
199 	snia64_read_config_word,
200 	snia64_read_config_dword,
201 	snia64_write_config_byte,
202 	snia64_write_config_word,
203 	snia64_write_config_dword
204 };
205 
206 /*
207  * snia64_pci_find_bios - SNIA64 pci_find_bios() platform specific code.
208  */
209 void __init
sn_pci_find_bios(void)210 sn_pci_find_bios(void)
211 {
212 	extern struct pci_ops *pci_root_ops;
213 	/*
214 	 * Go initialize our IO Infrastructure ..
215 	 */
216 	extern void sgi_master_io_infr_init(void);
217 
218 	sgi_master_io_infr_init();
219 
220 	/* sn_io_infrastructure_init(); */
221 	pci_root_ops = &snia64_pci_ops;
222 }
223 #else
sn_pci_find_bios(void)224 void sn_pci_find_bios(void) {}
225 struct list_head pci_root_buses;
226 struct list_head pci_root_buses;
227 struct list_head pci_devices;
228 
229 #endif /* CONFIG_PCI */
230