aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/adm8668/files/arch/mips/adm8668/pci.c
blob: 2bf119297225aea668b2bced8fb9ccb83a20c2a4 (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
/*
 * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
 *
 * 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.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/types.h>
#include <asm/byteorder.h>
#include <asm/pci.h>
#include <adm8668.h>

volatile u32* pci_config_address_reg = (volatile u32*)KSEG1ADDR(PCICFG_BASE);
volatile u32* pci_config_data_reg = (volatile u32*)KSEG1ADDR(PCIDAT_BASE);

#define PCI_ENABLE			0x80000000
#define	ADMPCI_IO_BASE			0x12600000
#define	ADMPCI_IO_SIZE			0x1fffff
#define	ADMPCI_MEM_BASE			0x16000000
#define	ADMPCI_MEM_SIZE			0x7ffffff
#define PCI_CMM_IOACC_EN		0x1
#define PCI_CMM_MEMACC_EN		0x2
#define PCI_CMM_MASTER_EN		0x4
#define PCI_CMM_DEF			(PCI_CMM_IOACC_EN | PCI_CMM_MEMACC_EN | PCI_CMM_MASTER_EN)

#define PCI_DEF_CACHE_LINE_SZ	0
#define PCI_DEF_LATENCY_TIMER	0x20
#define PCI_DEF_CACHE_LATENCY	((PCI_DEF_LATENCY_TIMER << 8) | PCI_DEF_CACHE_LINE_SZ)


#define cfgaddr(bus, devfn, where)	(	\
				(bus ? ((bus->number & 0xff) << 0x10) : 0) | \
					 ((devfn & 0xff) << 0x08) | \
					 (where & 0xfc)) | PCI_ENABLE

/* assumed little endian */
static int adm8668_read_config(struct pci_bus *bus, unsigned int devfn,
				int where, int size, u32 *val)
{
    	switch (size)
	{
	case 1:
		*pci_config_address_reg = cfgaddr(bus, devfn, where);
		*val = (le32_to_cpu(*pci_config_data_reg) >> ((where&3)<<3)) & 0xff;
		break;
	case 2:
		if (where & 1)
			return PCIBIOS_BAD_REGISTER_NUMBER;
		*pci_config_address_reg = cfgaddr(bus, devfn, where);
		*val = (le32_to_cpu(*pci_config_data_reg) >> ((where&3)<<3)) & 0xffff;
		break;
	case 4:
		if (where & 3)
			return PCIBIOS_BAD_REGISTER_NUMBER;
		*pci_config_address_reg = cfgaddr(bus, devfn, where);
		*val = le32_to_cpu(*pci_config_data_reg);
		break;
	}

	return PCIBIOS_SUCCESSFUL;
}

static int adm8668_write_config(struct pci_bus *bus, unsigned int devfn,
				int where, int size, u32 val)
{
	switch (size)
	{
	case 1:
		*pci_config_address_reg = cfgaddr(bus, devfn, where);
		*(volatile u8 *)(((int)pci_config_data_reg) + (where & 3)) = val;
		break;
	case 2:
		if (where & 1)
			return PCIBIOS_BAD_REGISTER_NUMBER;
		*pci_config_address_reg = cfgaddr(bus, devfn, where);
		*(volatile u16 *)(((int)pci_config_data_reg) + (where & 2)) = val;
		break;
	case 4:
		if (where & 3)
			return PCIBIOS_BAD_REGISTER_NUMBER;
		*pci_config_address_reg = cfgaddr(bus, devfn, where);
		*pci_config_data_reg = (val);
	}

	return PCIBIOS_SUCCESSFUL;
}


struct pci_ops adm8668_pci_ops = {
	.read = adm8668_read_config,
	.write = adm8668_write_config
};


struct resource pciioport_resource = {
	.name	= "adm8668_pci",
	.start	= ADMPCI_IO_BASE,
	.end	= ADMPCI_IO_BASE + ADMPCI_IO_SIZE,
	.flags	= IORESOURCE_IO
};


struct resource pciiomem_resource = {
	.name	= "adm8668_pci",
	.start	= ADMPCI_MEM_BASE,
	.end	= ADMPCI_MEM_BASE + ADMPCI_MEM_SIZE,
	.flags	= IORESOURCE_MEM
};

#ifdef CONFIG_ADM8668_DISABLE_PCI
struct pci_controller mips_pci_channels[] = {
	{ NULL, NULL, NULL , NULL , NULL}
};
#else
struct pci_controller mips_pci_channels = {
	.pci_ops = &adm8668_pci_ops,
	.io_resource = &pciioport_resource,
	.mem_resource = &pciiomem_resource,
};
#endif

int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
{
	switch (slot)
	{
		case 1:
			return 14;
		case 2:
			return 13;
		case 3:
			return 12;
		default:
			return dev->irq;
	}
}

int pcibios_plat_dev_init(struct pci_dev *dev)
{
	return 0;
}

static int __init adm8668_pci_init(void)
{
	void __iomem *io_map_base;

	printk("adm8668_pci_init()\n");

	/* what's an io port? this is MIPS... *shrug* */
	ioport_resource.start   = ADMPCI_IO_BASE;
	ioport_resource.end     = ADMPCI_IO_BASE + ADMPCI_IO_SIZE;

	io_map_base = ioremap(ADMPCI_IO_BASE, ADMPCI_IO_SIZE);
	if (!io_map_base)
		printk("io_map_base didn't work.\n");
	mips_pci_channels.io_map_base = (unsigned long)io_map_base;
	register_pci_controller(&mips_pci_channels);

	/* this needed? linksys' gpl 2.4 did it... */
	adm8668_write_config(NULL, 0, PCI_CACHE_LINE_SIZE, 2, 0);
	adm8668_write_config(NULL, 0, PCI_BASE_ADDRESS_0, 4, 0);
	adm8668_write_config(NULL, 0, PCI_BASE_ADDRESS_1, 4, 0);
	adm8668_write_config(NULL, 0, PCI_COMMAND, 4, PCI_CMM_DEF);

	return 0;
}

arch_initcall(adm8668_pci_init);