aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ar7-2.6/files/arch/mips/ar7/vlynq-pci.c
blob: eb11a3b00238e779f3d0cef8b3d303ba5a7642ba (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
/*
 * $Id$
 * 
 * Copyright (C) 2006, 2007 OpenWrt.org
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/types.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/ar7/vlynq.h>

#define VLYNQ_PCI_SLOTS 2

struct vlynq_reg_config {
	u32 offset;
	u32 value;
};

struct vlynq_pci_config {
	u32 chip_id;
	char name[32];
	struct vlynq_mapping rx_mapping[4];
	int irq;
	int irq_type;
	u32 chip;
	u32 class;
	int num_regs;
	struct vlynq_reg_config regs[10];
};

struct vlynq_pci_private {
	u32 latency;
	u32 cache_line;
	u32 command;
	u32 sz_mask;
	struct vlynq_pci_config *config;
};

static struct vlynq_pci_config known_devices[] = {
	{
		.chip_id = 0x00000009, .name = "TI ACX111",
		.rx_mapping = {
			{ .size = 0x22000, .offset = 0xf0000000 },
			{ .size = 0x40000, .offset = 0xc0000000 },
			{ .size = 0x0, .offset = 0x0 },
			{ .size = 0x0, .offset = 0x0 },
		},
		.irq = 0, .chip = 0x9066104c,
		.irq_type = IRQ_TYPE_EDGE_RISING,
		.class = PCI_CLASS_NETWORK_OTHER,
		.num_regs = 5,
		.regs = { 
			{ .offset = 0x790, .value = (0xd0000000 - (ARCH_PFN_OFFSET << PAGE_SHIFT)) },
			{ .offset = 0x794, .value = (0xd0000000 - (ARCH_PFN_OFFSET << PAGE_SHIFT)) },
			{ .offset = 0x740, .value = 0 },
			{ .offset = 0x744, .value = 0x00010000 },
			{ .offset = 0x764, .value = 0x00010000 },
		},
	},
};

static struct vlynq_device *slots[VLYNQ_PCI_SLOTS] = { NULL, };

static struct resource vlynq_io_resource = {
	.start	= 0x00000000,
	.end	= 0x00000000,
	.name	= "pci IO space",
	.flags	= IORESOURCE_IO
};

static struct resource vlynq_mem_resource = {
	.start	= 0x00000000,
	.end	= 0x00000000,
	.name	= "pci memory space",
	.flags	= IORESOURCE_MEM
};

static inline u32 vlynq_get_mapped(struct vlynq_device *dev, int res)
{
	int i;
	struct vlynq_pci_private *priv = dev->priv;
	u32 ret = dev->mem_start;
	if (!priv->config->rx_mapping[res].size) return 0;
	for (i = 0; i < res; i++)
		ret += priv->config->rx_mapping[i].size;

	return ret;
}

static inline u32 vlynq_read(u32 val, int size) {
	switch (size) {
	case 1:
		return *(u8 *)&val;
	case 2:
		return *(u16 *)&val;
	}
	return val;
}

static int vlynq_config_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val)
{
	struct vlynq_device *dev;
	struct vlynq_pci_private *priv;
	int resno, slot = PCI_SLOT(devfn);

	if ((size == 2) && (where & 1))
		return PCIBIOS_BAD_REGISTER_NUMBER;
	else if ((size == 4) && (where & 3))
		return PCIBIOS_BAD_REGISTER_NUMBER;

	if (slot >= VLYNQ_PCI_SLOTS)
		return PCIBIOS_DEVICE_NOT_FOUND;

	dev = slots[slot];

	if (!dev || (PCI_FUNC(devfn) > 0))
		return PCIBIOS_DEVICE_NOT_FOUND;

	priv = dev->priv;

	switch (where) {
	case PCI_VENDOR_ID:
		*val = vlynq_read(priv->config->chip, size);
		break;
	case PCI_DEVICE_ID:
		*val = priv->config->chip & 0xffff;
	case PCI_COMMAND:
		*val = priv->command;
	case PCI_STATUS:
/*		*val = PCI_STATUS_CAP_LIST;*/
		*val = 0;
		break;
	case PCI_CLASS_REVISION:
		*val = priv->config->class;
		break;
	case PCI_LATENCY_TIMER:
		*val = priv->latency;
		break;
	case PCI_HEADER_TYPE:
		*val = PCI_HEADER_TYPE_NORMAL;
		break;
	case PCI_CACHE_LINE_SIZE:
		*val = priv->cache_line;
		break;
	case PCI_BASE_ADDRESS_0:
	case PCI_BASE_ADDRESS_1:
	case PCI_BASE_ADDRESS_2:
	case PCI_BASE_ADDRESS_3:
		resno = (where - PCI_BASE_ADDRESS_0) >> 2;
		if (priv->sz_mask & (1 << resno)) {
		        priv->sz_mask &= ~(1 << resno);
			*val = priv->config->rx_mapping[resno].size;
		} else {
			*val = vlynq_get_mapped(dev, resno);
		}
		break;
	case PCI_BASE_ADDRESS_4:
	case PCI_BASE_ADDRESS_5:
	case PCI_SUBSYSTEM_VENDOR_ID:
	case PCI_SUBSYSTEM_ID:
	case PCI_ROM_ADDRESS:
	case PCI_INTERRUPT_LINE:
	case PCI_CARDBUS_CIS:
	case PCI_CAPABILITY_LIST:
		*val = 0;
		break;
	case PCI_INTERRUPT_PIN:
		*val = 1;
		break;
	default:
		printk("%s: Read of unknown register 0x%x (size %d)\n", 
		       dev->dev.bus_id, where, size);
		return PCIBIOS_BAD_REGISTER_NUMBER;
	}
	return PCIBIOS_SUCCESSFUL;
}

static int vlynq_config_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
{
	struct vlynq_device *dev;
	struct vlynq_pci_private *priv;
	int resno, slot = PCI_SLOT(devfn);

	if ((size == 2) && (where & 1))
		return PCIBIOS_BAD_REGISTER_NUMBER;
	else if ((size == 4) && (where & 3))
		return PCIBIOS_BAD_REGISTER_NUMBER;

	if (slot >= VLYNQ_PCI_SLOTS)
		return PCIBIOS_DEVICE_NOT_FOUND;

	dev = slots[slot];

	if (!dev || (PCI_FUNC(devfn) > 0))
		return PCIBIOS_DEVICE_NOT_FOUND;

	priv = dev->priv;

	switch (where) {
	case PCI_VENDOR_ID:
	case PCI_DEVICE_ID:
	case PCI_STATUS:
	case PCI_CLASS_REVISION:
	case PCI_HEADER_TYPE:
	case PCI_CACHE_LINE_SIZE:
	case PCI_SUBSYSTEM_VENDOR_ID:
	case PCI_SUBSYSTEM_ID:
	case PCI_INTERRUPT_LINE:
	case PCI_INTERRUPT_PIN:
	case PCI_CARDBUS_CIS:
	case PCI_CAPABILITY_LIST:
		return PCIBIOS_FUNC_NOT_SUPPORTED;
	case PCI_COMMAND:
		priv->command = val;
	case PCI_LATENCY_TIMER:
		priv->latency = val;
		break;
	case PCI_BASE_ADDRESS_0:
	case PCI_BASE_ADDRESS_1:
	case PCI_BASE_ADDRESS_2:
	case PCI_BASE_ADDRESS_3:
		if (val == 0xffffffff) {
			resno = (where - PCI_BASE_ADDRESS_0) >> 2;
			priv->sz_mask |= (1 << resno);
			break;
		}
	case PCI_BASE_ADDRESS_4:
	case PCI_BASE_ADDRESS_5:
	case PCI_ROM_ADDRESS:
		break;
	default:
		printk("%s: Write to unknown register 0x%x (size %d) value=0x%x\n", 
		       dev->dev.bus_id, where, size, val);
		return PCIBIOS_BAD_REGISTER_NUMBER;
	}
	return PCIBIOS_SUCCESSFUL;
}

static struct pci_ops vlynq_pci_ops = {
	vlynq_config_read,
	vlynq_config_write
};

static struct pci_controller vlynq_controller = {
	.pci_ops	= &vlynq_pci_ops,
	.io_resource	= &vlynq_io_resource,
	.mem_resource	= &vlynq_mem_resource,
};

static int vlynq_pci_probe(struct vlynq_device *dev)
{
	int result, i;
	u32 chip_id, addr;
	struct vlynq_pci_private *priv;
	struct vlynq_mapping mapping[4] = { { 0, }, };
	struct vlynq_pci_config *config = NULL;

	result = vlynq_set_local_irq(dev, 31);
	if (result)
		return result;

	result = vlynq_set_remote_irq(dev, 30);
	if (result)
		return result;

	result = vlynq_device_enable(dev);
	if (result)
		return result;

	chip_id = vlynq_remote_id(dev);
	for (i = 0; i < ARRAY_SIZE(known_devices); i++)
		if (chip_id == known_devices[i].chip_id)
			config = &known_devices[i];

	if (!config) {
		printk("vlynq-pci: skipping unknown device "
		       "%04x:%04x at %s\n", chip_id >> 16, 
		       chip_id & 0xffff, dev->dev.bus_id);
		result = -ENODEV;
		goto fail;
	}

	printk("vlynq-pci: attaching device %s at %s\n",
	       config->name, dev->dev.bus_id);

	priv = kmalloc(sizeof(struct vlynq_pci_private), GFP_KERNEL);
	if (!priv) {
		printk(KERN_ERR "%s: failed to allocate private data\n",
		       dev->dev.bus_id);
		result = -ENOMEM;
		goto fail;
	}

	memset(priv, 0, sizeof(struct vlynq_pci_private));
	priv->latency = 64;
	priv->cache_line = 32;
	priv->config = config;

	mapping[0].offset = ARCH_PFN_OFFSET << PAGE_SHIFT;
	mapping[0].size = 0x02000000;
	vlynq_set_local_mapping(dev, dev->mem_start, mapping);
	vlynq_set_remote_mapping(dev, 0, config->rx_mapping);

	set_irq_type(vlynq_virq_to_irq(dev, config->irq), config->irq_type);

	addr = (u32)ioremap_nocache(dev->mem_start, 0x10000);
	if (!addr) {
		printk(KERN_ERR "%s: failed to remap io memory\n",
		       dev->dev.bus_id);
		result = -ENXIO;
		goto fail;
	}

	for (i = 0; i < config->num_regs; i++)
		*(volatile u32 *)(addr + config->regs[i].offset) =
			config->regs[i].value;

	dev->priv = priv;
	for (i = 0; i < VLYNQ_PCI_SLOTS; i++) {
		if (!slots[i]) {
			slots[i] = dev;
			break;
		}
	}

	return 0;

fail:
	vlynq_device_disable(dev);

	return result;
}

static int vlynq_pci_remove(struct vlynq_device *dev)
{
	int i;
	struct vlynq_pci_private *priv = dev->priv;

	for (i = 0; i < VLYNQ_PCI_SLOTS; i++)
		if (slots[i] == dev)
			slots[i] = NULL;

	vlynq_device_disable(dev);
	kfree(priv);

	return 0;
}

static struct vlynq_driver vlynq_pci = {
	.name = "PCI over VLYNQ emulation",
	.probe = vlynq_pci_probe,
	.remove = vlynq_pci_remove,
};

int vlynq_pci_init(void)
{
	int res;
	res = vlynq_register_driver(&vlynq_pci);
	if (res) 
		return res;

	register_pci_controller(&vlynq_controller);

	return 0;
}

int pcibios_map_irq(struct pci_dev *pdev, u8 slot, u8 pin)
{
	struct vlynq_device *dev;
	struct vlynq_pci_private *priv;

	dev = slots[slot];

	if (!dev)
		return 0;

	priv = dev->priv;

	return vlynq_virq_to_irq(dev, priv->config->irq);
}
                                                                        
/* Do platform specific device initialization at pci_enable_device() time */
int pcibios_plat_dev_init(struct pci_dev *dev)
{
	return 0;
}