aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ia64/sn/io/sn2/pcibr/pcibr_config.c
blob: d3f3913b05de9653d6e9bfb96830bdcbbaa5db57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2001-2002 Silicon Graphics, Inc. All rights reserved.
 */

#include <linux/types.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/byteorder/swab.h>
#include <asm/sn/sgi.h>
#include <asm/sn/sn_cpuid.h>
#include <asm/sn/addrs.h>
#include <asm/sn/arch.h>
#include <asm/sn/iograph.h>
#include <asm/sn/invent.h>
#include <asm/sn/hcl.h>
#include <asm/sn/labelcl.h>
#include <asm/sn/xtalk/xwidget.h>
#include <asm/sn/pci/bridge.h>
#include <asm/sn/pci/pciio.h>
#include <asm/sn/pci/pcibr.h>
#include <asm/sn/pci/pcibr_private.h>
#include <asm/sn/pci/pci_defs.h>
#include <asm/sn/prio.h>
#include <asm/sn/xtalk/xbow.h>
#include <asm/sn/ioc3.h>
#include <asm/sn/eeprom.h>
#include <asm/sn/io.h>
#include <asm/sn/sn_private.h>

extern pcibr_info_t      pcibr_info_get(devfs_handle_t);

uint64_t          pcibr_config_get(devfs_handle_t, unsigned, unsigned);
uint64_t          do_pcibr_config_get(int, cfg_p, unsigned, unsigned);
void              pcibr_config_set(devfs_handle_t, unsigned, unsigned, uint64_t);
void       	  do_pcibr_config_set(int, cfg_p, unsigned, unsigned, uint64_t);
static void	  swap_do_pcibr_config_set(cfg_p, unsigned, unsigned, uint64_t);

#ifdef LITTLE_ENDIAN
/*
 * on sn-ia we need to twiddle the the addresses going out
 * the pci bus because we use the unswizzled synergy space
 * (the alternative is to use the swizzled synergy space
 * and byte swap the data)
 */
#define CB(b,r) (((volatile uint8_t *) b)[((r)^4)])
#define CS(b,r) (((volatile uint16_t *) b)[((r^4)/2)])
#define CW(b,r) (((volatile uint32_t *) b)[((r^4)/4)])

#define	CBP(b,r) (((volatile uint8_t *) b)[(r)^3])
#define	CSP(b,r) (((volatile uint16_t *) b)[((r)/2)^1])
#define	CWP(b,r) (((volatile uint32_t *) b)[(r)/4])

#define SCB(b,r) (((volatile uint8_t *) b)[((r)^3)])
#define SCS(b,r) (((volatile uint16_t *) b)[((r^2)/2)])
#define SCW(b,r) (((volatile uint32_t *) b)[((r)/4)])
#else
#define	CB(b,r)	(((volatile uint8_t *) cfgbase)[(r)^3])
#define	CS(b,r)	(((volatile uint16_t *) cfgbase)[((r)/2)^1])
#define	CW(b,r)	(((volatile uint32_t *) cfgbase)[(r)/4])
#endif

/*
 * Return a config space address for given slot / func / offset.  Note the
 * returned pointer is a 32bit word (ie. cfg_p) aligned pointer pointing to
 * the 32bit word that contains the "offset" byte.
 */
cfg_p
pcibr_func_config_addr(bridge_t *bridge, pciio_bus_t bus, pciio_slot_t slot, 
					pciio_function_t func, int offset)
{
	/*
	 * Type 1 config space
	 */
	if (bus > 0) {
		bridge->b_pci_cfg = ((bus << 16) | (slot << 11));
		return &bridge->b_type1_cfg.f[func].l[(offset)];
	}

	/*
	 * Type 0 config space
	 */
	if (is_pic(bridge))
		slot++;
	return &bridge->b_type0_cfg_dev[slot].f[func].l[offset];
}

/*
 * Return config space address for given slot / offset.  Note the returned
 * pointer is a 32bit word (ie. cfg_p) aligned pointer pointing to the
 * 32bit word that contains the "offset" byte.
 */
cfg_p
pcibr_slot_config_addr(bridge_t *bridge, pciio_slot_t slot, int offset)
{
	return pcibr_func_config_addr(bridge, 0, slot, 0, offset);
}

/*
 * Return config space data for given slot / offset
 */
unsigned
pcibr_slot_config_get(bridge_t *bridge, pciio_slot_t slot, int offset)
{
	cfg_p  cfg_base;
	
	cfg_base = pcibr_slot_config_addr(bridge, slot, 0);
	return (do_pcibr_config_get(is_pic(bridge), cfg_base, offset, sizeof(unsigned)));
}

/*
 * Return config space data for given slot / func / offset
 */
unsigned
pcibr_func_config_get(bridge_t *bridge, pciio_slot_t slot, 
					pciio_function_t func, int offset)
{
	cfg_p  cfg_base;

	cfg_base = pcibr_func_config_addr(bridge, 0, slot, func, 0);
	return (do_pcibr_config_get(is_pic(bridge), cfg_base, offset, sizeof(unsigned)));
}

/*
 * Set config space data for given slot / offset
 */
void
pcibr_slot_config_set(bridge_t *bridge, pciio_slot_t slot, 
					int offset, unsigned val)
{
	cfg_p  cfg_base;

	cfg_base = pcibr_slot_config_addr(bridge, slot, 0);
	do_pcibr_config_set(is_pic(bridge), cfg_base, offset, sizeof(unsigned), val);
}

/*
 * Set config space data for given slot / func / offset
 */
void
pcibr_func_config_set(bridge_t *bridge, pciio_slot_t slot, 
			pciio_function_t func, int offset, unsigned val)
{
	cfg_p  cfg_base;

	cfg_base = pcibr_func_config_addr(bridge, 0, slot, func, 0);
	do_pcibr_config_set(is_pic(bridge), cfg_base, offset, sizeof(unsigned), val);
}

int pcibr_config_debug = 0;

cfg_p
pcibr_config_addr(devfs_handle_t conn,
		  unsigned reg)
{
    pcibr_info_t            pcibr_info;
    pciio_bus_t		    pciio_bus;
    pciio_slot_t            pciio_slot;
    pciio_function_t        pciio_func;
    pcibr_soft_t            pcibr_soft;
    bridge_t               *bridge;
    cfg_p                   cfgbase = (cfg_p)0;
    pciio_info_t	    pciio_info;

    pciio_info = pciio_info_get(conn);
    pcibr_info = pcibr_info_get(conn);

    /*
     * Determine the PCI bus/slot/func to generate a config address for.
     */

    if (pciio_info_type1_get(pciio_info)) {
	/*
	 * Conn is a vhdl which uses TYPE 1 addressing explicitly passed 
	 * in reg.
	 */
	pciio_bus = PCI_TYPE1_BUS(reg);
	pciio_slot = PCI_TYPE1_SLOT(reg);
	pciio_func = PCI_TYPE1_FUNC(reg);

	ASSERT(pciio_bus != 0);
#if 0
    } else if (conn != pciio_info_hostdev_get(pciio_info)) {
	/*
	 * Conn is on a subordinate bus, so get bus/slot/func directly from
	 * its pciio_info_t structure.
	 */
	pciio_bus = pciio_info->c_bus;
	pciio_slot = pciio_info->c_slot;
	pciio_func = pciio_info->c_func;
	if (pciio_func == PCIIO_FUNC_NONE) {
		pciio_func = 0;
	}
#endif
    } else {
	/*
	 * Conn is directly connected to the host bus.  PCI bus number is
	 * hardcoded to 0 (even though it may have a logical bus number != 0)
	 * and slot/function are derived from the pcibr_info_t associated
	 * with the device.
	 */
	pciio_bus = 0;

    pciio_slot = PCIBR_INFO_SLOT_GET_INT(pcibr_info);
    if (pciio_slot == PCIIO_SLOT_NONE)
	pciio_slot = PCI_TYPE1_SLOT(reg);

    pciio_func = pcibr_info->f_func;
    if (pciio_func == PCIIO_FUNC_NONE)
	pciio_func = PCI_TYPE1_FUNC(reg);
    }

    pcibr_soft = (pcibr_soft_t) pcibr_info->f_mfast;

    bridge = pcibr_soft->bs_base;

    cfgbase = pcibr_func_config_addr(bridge,
			pciio_bus, pciio_slot, pciio_func, 0);

    return cfgbase;
}

extern unsigned char Is_pic_on_this_nasid[];
uint64_t
pcibr_config_get(devfs_handle_t conn,
		 unsigned reg,
		 unsigned size)
{
    if ( !Is_pic_on_this_nasid[ NASID_GET((pcibr_config_addr(conn, reg)))] )
    	return do_pcibr_config_get(0, pcibr_config_addr(conn, reg),
				PCI_TYPE1_REG(reg), size);
    else
    	return do_pcibr_config_get(1, pcibr_config_addr(conn, reg),
				PCI_TYPE1_REG(reg), size);
}

uint64_t
do_pcibr_config_get(
		       int pic,
		       cfg_p cfgbase,
		       unsigned reg,
		       unsigned size)
{
    unsigned                value;

    if ( pic ) {
	value = CWP(cfgbase, reg);
    }
    else {
	if ( io_get_sh_swapper(NASID_GET(cfgbase)) ) {
	    /*
	     * Shub Swapper on - 0 returns PCI Offset 0 but byte swapped!
	     * Do not swizzle address and byte swap the result.
	     */
	    value = SCW(cfgbase, reg);
	    value = __swab32(value);
	} else {
    	    value = CW(cfgbase, reg);
	}
    }
    if (reg & 3)
	value >>= 8 * (reg & 3);
    if (size < 4)
	value &= (1 << (8 * size)) - 1;
    return value;
}

void
pcibr_config_set(devfs_handle_t conn,
		 unsigned reg,
		 unsigned size,
		 uint64_t value)
{
    if ( Is_pic_on_this_nasid[ NASID_GET((pcibr_config_addr(conn, reg)))] )
    	do_pcibr_config_set(1, pcibr_config_addr(conn, reg),
			PCI_TYPE1_REG(reg), size, value);
    else
	swap_do_pcibr_config_set(pcibr_config_addr(conn, reg),
			PCI_TYPE1_REG(reg), size, value);
}

void
do_pcibr_config_set(int pic,
		    cfg_p cfgbase,
		    unsigned reg,
		    unsigned size,
		    uint64_t value)
{
	if ( pic ) {
		switch (size) {
		case 1:
			CBP(cfgbase, reg) = value;
			break;
		case 2:
			if (reg & 1) {
				CBP(cfgbase, reg) = value;
				CBP(cfgbase, reg + 1) = value >> 8;
			} else
				CSP(cfgbase, reg) = value;
			break;
		case 3:
			if (reg & 1) {
				CBP(cfgbase, reg) = value;
				CSP(cfgbase, (reg + 1)) = value >> 8;
			} else {
				CSP(cfgbase, reg) = value;
				CBP(cfgbase, reg + 2) = value >> 16;
			}
			break;
		case 4:
			CWP(cfgbase, reg) = value;
			break;
   		}
	}
	else {
		switch (size) {
		case 1:
			CB(cfgbase, reg) = value;
			break;
		case 2:
			if (reg & 1) {
				CB(cfgbase, reg) = value;
				CB(cfgbase, reg + 1) = value >> 8;
			} else
				CS(cfgbase, reg) = value;
			break;
		case 3:
			if (reg & 1) {
				CB(cfgbase, reg) = value;
				CS(cfgbase, (reg + 1)) = value >> 8;
			} else {
				CS(cfgbase, reg) = value;
				CB(cfgbase, reg + 2) = value >> 16;
			}
			break;
		case 4:
			CW(cfgbase, reg) = value;
			break;
   		}
	}
}

void
swap_do_pcibr_config_set(cfg_p cfgbase,
                    unsigned reg,
                    unsigned size,
                    uint64_t value)
{

    uint64_t temp_value = 0;

    switch (size) {
    case 1:
        SCB(cfgbase, reg) = value;
        break;
    case 2:
	temp_value = __swab16(value);
        if (reg & 1) {
            SCB(cfgbase, reg) = temp_value;
            SCB(cfgbase, reg + 1) = temp_value >> 8;
        } else
            SCS(cfgbase, reg) = temp_value;
        break;
    case 3:
	BUG();
        break;

    case 4:
	temp_value = __swab32(value);
        SCW(cfgbase, reg) = temp_value;
        break;
    }
}