summaryrefslogtreecommitdiffstats
path: root/arch/mips/ddb5476/pci.c
blob: c31113c584d4aa68c85a3d8f4823324ea522d59f (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 *  arch/mips/ddb5476/pci.c -- NEC DDB Vrc-5074 PCI access routines
 *
 *  Copyright (C) 2000 Geert Uytterhoeven <geert@sonycom.com>
 *                     Albert Dorofeev <albert@sonycom.com>
 *                     Sony Software Development Center Europe (SDCE), Brussels
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/ioport.h>

#include <asm-mips/nile4.h>


static u32 nile4_pre_pci_access0(int slot_num)
{
	u32 pci_addr = 0;
	u32 virt_addr = NILE4_PCI_CFG_BASE;

	/* work around the bug for Vrc5476 */
	if (slot_num == 13)
		return NILE4_BASE + NILE4_PCI_BASE;

	/* Set window 1 address 08000000 - 32 bit - 128 MB (PCI config space) */
	nile4_set_pdar(NILE4_PCIW1, PHYSADDR(virt_addr), 0x08000000, 32, 0,
		       0);

	// [jsun] we start scanning from addr:10, 
	// with 128M we can go up to addr:26 (slot 16)
	if (slot_num <= 16) {
		virt_addr += 0x00000400 << slot_num;
	} else {
		/* for high slot, we have to set higher PCI base addr */
		pci_addr = 0x00000400 << slot_num;
	}
	nile4_set_pmr(NILE4_PCIINIT1, NILE4_PCICMD_CFG, pci_addr);
	return virt_addr;
}

static void nile4_post_pci_access0(void)
{
	/*
	 * Set window 1 back to address 08000000 - 32 bit - 128 MB
	 * (PCI IO space)
	 */
	nile4_set_pdar(NILE4_PCIW1, PHYSADDR(NILE4_PCI_MEM_BASE),
		       0x08000000, 32, 1, 1);
	// nile4_set_pmr(NILE4_PCIINIT1, NILE4_PCICMD_MEM, 0);
	nile4_set_pmr(NILE4_PCIINIT1, NILE4_PCICMD_MEM, 0x08000000);
}


static int nile4_pci_read_config_dword(struct pci_dev *dev,
				       int where, u32 * val)
{
	int slot_num, func_num;
	u32 base;
	u32 addr;

	/*
	 *  Do we need to generate type 1 configure transaction?
	 */
	if (dev->bus->number) {
		/* FIXME - not working yet */
		return PCIBIOS_FUNC_NOT_SUPPORTED;

		/*
		 * the largest type 1 configuration addr is 16M, < 256M
		 * config space
		 */
		slot_num = 0;
		addr =
		    (dev->bus->number << 16) | (dev->devfn <
						8) | where | 1;
	} else {
		slot_num = PCI_SLOT(dev->devfn);
		func_num = PCI_FUNC(dev->devfn);
		addr = (func_num << 8) + where;
	}

	base = nile4_pre_pci_access0(slot_num);
	*val = *(volatile u32 *) (base + addr);
	nile4_post_pci_access0();
	return PCIBIOS_SUCCESSFUL;
}

static int nile4_pci_write_config_dword(struct pci_dev *dev, int where,
					u32 val)
{
	int slot_num, func_num;
	u32 base;
	u32 addr;

	/*
	 *  Do we need to generate type 1 configure transaction?
	 */
	if (dev->bus->number) {
		/* FIXME - not working yet */
		return PCIBIOS_FUNC_NOT_SUPPORTED;

		/* the largest type 1 configuration addr is 16M, < 256M config space */
		slot_num = 0;
		addr =
		    (dev->bus->number << 16) | (dev->devfn <
						8) | where | 1;
	} else {
		slot_num = PCI_SLOT(dev->devfn);
		func_num = PCI_FUNC(dev->devfn);
		addr = (func_num << 8) + where;
	}

	base = nile4_pre_pci_access0(slot_num);
	*(volatile u32 *) (base + addr) = val;
	nile4_post_pci_access0();
	return PCIBIOS_SUCCESSFUL;
}

static int nile4_pci_read_config_word(struct pci_dev *dev, int where,
				      u16 * val)
{
	int status;
	u32 result;

	status = nile4_pci_read_config_dword(dev, where & ~3, &result);
	if (status != PCIBIOS_SUCCESSFUL)
		return status;
	if (where & 2)
		result >>= 16;
	*val = result & 0xffff;
	return PCIBIOS_SUCCESSFUL;
}

static int nile4_pci_read_config_byte(struct pci_dev *dev, int where,
				      u8 * val)
{
	int status;
	u32 result;

	status = nile4_pci_read_config_dword(dev, where & ~3, &result);
	if (status != PCIBIOS_SUCCESSFUL)
		return status;
	if (where & 1)
		result >>= 8;
	if (where & 2)
		result >>= 16;
	*val = result & 0xff;
	return PCIBIOS_SUCCESSFUL;
}

static int nile4_pci_write_config_word(struct pci_dev *dev, int where,
				       u16 val)
{
	int status, shift = 0;
	u32 result;

	status = nile4_pci_read_config_dword(dev, where & ~3, &result);
	if (status != PCIBIOS_SUCCESSFUL)
		return status;
	if (where & 2)
		shift += 16;
	result &= ~(0xffff << shift);
	result |= val << shift;
	return nile4_pci_write_config_dword(dev, where & ~3, result);
}

static int nile4_pci_write_config_byte(struct pci_dev *dev, int where,
				       u8 val)
{
	int status, shift = 0;
	u32 result;

	status = nile4_pci_read_config_dword(dev, where & ~3, &result);
	if (status != PCIBIOS_SUCCESSFUL)
		return status;
	if (where & 2)
		shift += 16;
	if (where & 1)
		shift += 8;
	result &= ~(0xff << shift);
	result |= val << shift;
	return nile4_pci_write_config_dword(dev, where & ~3, result);
}

struct pci_ops nile4_pci_ops = {
	nile4_pci_read_config_byte,
	nile4_pci_read_config_word,
	nile4_pci_read_config_dword,
	nile4_pci_write_config_byte,
	nile4_pci_write_config_word,
	nile4_pci_write_config_dword
};

struct {
	struct resource ram;
	struct resource flash;
	struct resource isa_io;
	struct resource pci_io;
	struct resource isa_mem;
	struct resource pci_mem;
	struct resource nile4;
	struct resource boot;
} ddb5476_resources = {
	// { "RAM", 0x00000000, 0x03ffffff, IORESOURCE_MEM | PCI_BASE_ADDRESS_MEM_TYPE_64 },
	{
	"RAM", 0x00000000, 0x03ffffff, IORESOURCE_MEM}, {
	"Flash ROM", 0x04000000, 0x043fffff}, {
	"Nile4 ISA I/O", 0x06000000, 0x060fffff}, {
	"Nile4 PCI I/O", 0x06100000, 0x07ffffff}, {
	"Nile4 ISA mem", 0x08000000, 0x08ffffff, IORESOURCE_MEM}, {
	"Nile4 PCI mem", 0x09000000, 0x0fffffff, IORESOURCE_MEM},
	    // { "Nile4 ctrl", 0x1fa00000, 0x1fbfffff, IORESOURCE_MEM | PCI_BASE_ADDRESS_MEM_TYPE_64 },
	{
	"Nile4 ctrl", 0x1fa00000, 0x1fbfffff, IORESOURCE_MEM}, {
	"Boot ROM", 0x1fc00000, 0x1fffffff}
};

struct resource M5229_resources[5] = {
	{"M5229 BAR0", 0x1f0, 0x1f3, IORESOURCE_IO},
	{"M5229 BAR1", 0x3f4, 0x3f7, IORESOURCE_IO},
	{"M5229 BAR2", 0x170, 0x173, IORESOURCE_IO},
	{"M5229 BAR3", 0x374, 0x377, IORESOURCE_IO},
	{"M5229 BAR4", 0xf000, 0xf00f, IORESOURCE_IO}
};

static void __init ddb5476_pci_fixup(void)
{
	struct pci_dev *dev;

	pci_for_each_dev(dev) {
		if (dev->vendor == PCI_VENDOR_ID_NEC &&
		    dev->device == PCI_DEVICE_ID_NEC_VRC5476) {
			/*
			 * The first 64-bit PCI base register should point to
			 * the Nile4 control registers. Unfortunately this
			 * isn't the case, so we fix it ourselves. This allows
			 * the serial driver to find the UART.
			 */
			dev->resource[0] = ddb5476_resources.nile4;
			request_resource(&iomem_resource,
					 &dev->resource[0]);
			/*
			 * The second 64-bit PCI base register points to the
			 * first memory bank. Unfortunately the address is
			 * wrong, so we fix it (again).
			 */

			/* [jsun] We cannot request the resource anymore,
			 * because kernel/setup.c has already reserved "System 
			 * RAM" resource at the same spot.  
			 * The fundamental problem here is that PCI host 
			 * controller should not put system RAM mapping in BAR
			 * and make subject to PCI resource assignement.
			 * Current fix is a total hack.  We set parent to 1 so
                         * so that PCI resource assignement code is fooled to 
                         * think the resource is assigned, and will not attempt
                         * to mess with it.
			 */
			dev->resource[2] = ddb5476_resources.ram;
			if (request_resource(&iomem_resource,
			                     &dev->resource[2]) ) {
				dev->resource[2].parent = 0x1;
                        }

		} else if (dev->vendor == PCI_VENDOR_ID_AL
			   && dev->device == PCI_DEVICE_ID_AL_M7101) {
			/*
			 * It's nice to have the LEDs on the GPIO pins
			 * available for debugging
			 */
			extern struct pci_dev *pci_pmu;
			u8 t8;

			pci_pmu = dev;	/* for LEDs D2 and D3 */
			/* Program the lines for LEDs D2 and D3 to output */
			nile4_pci_read_config_byte(dev, 0x7d, &t8);
			t8 |= 0xc0;
			nile4_pci_write_config_byte(dev, 0x7d, t8);
			/* Turn LEDs D2 and D3 off */
			nile4_pci_read_config_byte(dev, 0x7e, &t8);
			t8 |= 0xc0;
			nile4_pci_write_config_byte(dev, 0x7e, t8);
		} else if (dev->vendor == PCI_VENDOR_ID_AL &&
			   dev->device == 0x5229) {
			int i;
			for (i = 0; i < 5; i++) {
				dev->resource[i] = M5229_resources[i];
				request_resource(&ioport_resource,
						 &dev->resource[i]);
			}
		}
	}
}

static void __init pcibios_fixup_irqs(void)
{
	struct pci_dev *dev;
	int slot_num;

	pci_for_each_dev(dev) {
		slot_num = PCI_SLOT(dev->devfn);
		switch (slot_num) {
		case 3:	/* re-programmed to USB */
			dev->irq = 9;	/* hard-coded; see irq.c */
			break;
		case 4:	/* re-programmed to PMU */
			dev->irq = 10;	/* hard-coded; see irq.c */
			break;
		case 6:	/* on-board pci-pci bridge */
			dev->irq = 0xff;
			break;
		case 7:	/* on-board ether */
			dev->irq = nile4_to_irq(NILE4_INT_INTB);
			break;
		case 8:	/* ISA-PCI bridge  */
			dev->irq = nile4_to_irq(NILE4_INT_INTC);
			break;
		case 9:	/* ext slot #3 */
			dev->irq = nile4_to_irq(NILE4_INT_INTD);
			break;
		case 10:	/* ext slot #4 */
			dev->irq = nile4_to_irq(NILE4_INT_INTA);
			break;
		case 13:	/* Vrc5476 */
			dev->irq = 0xff;
			break;
		case 14:	/* HD controller, M5229 */
			dev->irq = 14;
			break;
		default:
			printk
			    ("JSUN : in pcibios_fixup_irqs - unkown slot %d\n",
			     slot_num);
			panic
			    ("JSUN : in pcibios_fixup_irqs - unkown slot.\n");
		}
	}
}

void __init pcibios_init(void)
{
	printk("PCI: Emulate bios initialization \n");
	/* [jsun] we need to set BAR0 so that SDRAM 0 appears at 0x0 in PCI */
	*(long *) (NILE4_BASE + NILE4_BAR0) = 0x8;

	printk("PCI: Probing PCI hardware\n");
	ioport_resource.end = 0x1ffffff;	/*  32 MB */
	iomem_resource.end = 0x1fffffff;	/* 512 MB */

	/* `ram' and `nile4' are requested through the Nile4 pci_dev */
	request_resource(&iomem_resource, &ddb5476_resources.flash);
	request_resource(&iomem_resource, &ddb5476_resources.isa_io);
	request_resource(&iomem_resource, &ddb5476_resources.pci_io);
	request_resource(&iomem_resource, &ddb5476_resources.isa_mem);
	request_resource(&iomem_resource, &ddb5476_resources.pci_mem);
	request_resource(&iomem_resource, &ddb5476_resources.boot);

	pci_scan_bus(0, &nile4_pci_ops, NULL);
	ddb5476_pci_fixup();
	pci_assign_unassigned_resources();
	pcibios_fixup_irqs();
}

void __init pcibios_fixup_bus(struct pci_bus *bus)
{
	/* [jsun] we don't know how to fix sub-buses yet */
	if (bus->number == 0) {
		bus->resource[1] = &ddb5476_resources.pci_mem;
	}
}

char *pcibios_setup(char *str)
{
	return str;
}

void __init pcibios_update_irq(struct pci_dev *dev, int irq)
{
	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
}

void __init pcibios_fixup_pbus_ranges(struct pci_bus *bus,
				      struct pbus_set_ranges_data *ranges)
{
	/*
	 * our caller figure out range by going through the dev structures.  
	 * I guess this is the place to fix things up if the bus is using a 
	 * different view of the addressing space.
	 */

#if 0	/*  original DDB5074 code */
	   if (bus->number == 0) {
	   ranges->io_start -= bus->resource[0]->start;
	   ranges->io_end -= bus->resource[0]->start;
	   ranges->mem_start -= bus->resource[1]->start;
	   ranges->mem_end -= bus->resource[1]->start;
	   }
#endif
}

int pcibios_enable_resources(struct pci_dev *dev)
{
	u16 cmd, old_cmd;
	int idx;
	struct resource *r;

	/*
	 *  Don't touch the Nile 4
	 */
	if (dev->vendor == PCI_VENDOR_ID_NEC &&
	    dev->device == PCI_DEVICE_ID_NEC_VRC5476) return 0;

	pci_read_config_word(dev, PCI_COMMAND, &cmd);
	old_cmd = cmd;
	for (idx = 0; idx < 6; idx++) {
		r = &dev->resource[idx];
		if (!r->start && r->end) {
			printk(KERN_ERR "PCI: Device %s not available because "
			       "of resource collisions\n", dev->slot_name);
			return -EINVAL;
		}
		if (r->flags & IORESOURCE_IO)
			cmd |= PCI_COMMAND_IO;
		if (r->flags & IORESOURCE_MEM)
			cmd |= PCI_COMMAND_MEMORY;
	}
	if (cmd != old_cmd) {
		printk("PCI: Enabling device %s (%04x -> %04x)\n",
		       dev->slot_name, old_cmd, cmd);
		pci_write_config_word(dev, PCI_COMMAND, cmd);
	}
	return 0;
}

int pcibios_enable_device(struct pci_dev *dev)
{
	return pcibios_enable_resources(dev);
}

void pcibios_update_resource(struct pci_dev *dev, struct resource *root,
			     struct resource *res, int resource)
{
	u32 new, check;
	int reg;

	new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
	if (resource < 6) {
		reg = PCI_BASE_ADDRESS_0 + 4 * resource;
	} else if (resource == PCI_ROM_RESOURCE) {
		res->flags |= PCI_ROM_ADDRESS_ENABLE;
		reg = dev->rom_base_reg;
	} else {
		/*
		 * Somebody might have asked allocation of a non-standard
		 * resource
		 */
		return;
	}

	pci_write_config_dword(dev, reg, new);
	pci_read_config_dword(dev, reg, &check);
	if ((new ^ check) &
	    ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK :
	     PCI_BASE_ADDRESS_MEM_MASK)) {
		printk(KERN_ERR "PCI: Error while updating region "
		       "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
		       new, check);
	}
}

void pcibios_align_resource(void *data, struct resource *res,
			    unsigned long size)
{
	struct pci_dev *dev = data;

	if (res->flags & IORESOURCE_IO) {
		unsigned long start = res->start;

		/* We need to avoid collisions with `mirrored' VGA ports
		   and other strange ISA hardware, so we always want the
		   addresses kilobyte aligned.  */
		if (size > 0x100) {
			printk(KERN_ERR "PCI: I/O Region %s/%d too large"
			       " (%ld bytes)\n", dev->slot_name,
			       dev->resource - res, size);
		}

		start = (start + 1024 - 1) & ~(1024 - 1);
		res->start = start;
	}
}

struct pci_fixup pcibios_fixups[] = { {0} };