ffcfa62a17707854a2847e9831a71b0130879dfb
[openwrt.git] / target / linux / ar7 / files / arch / mips / ar7 / vlynq-pci.c
1 /*
2 * Copyright (C) 2006, 2007 OpenWrt.org
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <linux/types.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/irq.h>
24 #include <asm/ar7/vlynq.h>
25
26 #define VLYNQ_PCI_SLOTS 2
27
28 struct vlynq_reg_config {
29 u32 offset;
30 u32 value;
31 };
32
33 struct vlynq_pci_config {
34 u32 chip_id;
35 char name[32];
36 struct vlynq_mapping rx_mapping[4];
37 int irq;
38 int irq_type;
39 u32 chip;
40 u32 class;
41 int num_regs;
42 struct vlynq_reg_config regs[10];
43 };
44
45 struct vlynq_pci_private {
46 u32 latency;
47 u32 cache_line;
48 u32 command;
49 u32 sz_mask;
50 struct vlynq_pci_config *config;
51 };
52
53 static struct vlynq_pci_config known_devices[] = {
54 {
55 .chip_id = 0x00000009, .name = "TI TNETW1130",
56 .rx_mapping = {
57 { .size = 0x22000, .offset = 0xf0000000 },
58 { .size = 0x40000, .offset = 0xc0000000 },
59 { .size = 0x0, .offset = 0x0 },
60 { .size = 0x0, .offset = 0x0 },
61 },
62 .irq = 0, .chip = 0x9066104c,
63 .irq_type = IRQ_TYPE_EDGE_RISING,
64 .class = PCI_CLASS_NETWORK_OTHER,
65 .num_regs = 5,
66 .regs = {
67 {
68 .offset = 0x790,
69 .value = (0xd0000000 - PHYS_OFFSET)
70 },
71 {
72 .offset = 0x794,
73 .value = (0xd0000000 - PHYS_OFFSET)
74 },
75 { .offset = 0x740, .value = 0 },
76 { .offset = 0x744, .value = 0x00010000 },
77 { .offset = 0x764, .value = 0x00010000 },
78 },
79 },
80 {
81 .chip_id = 0x00000029, .name = "TI TNETW1350",
82 .rx_mapping = {
83 { .size = 0x100000, .offset = 0x00300000 },
84 { .size = 0x80000, .offset = 0x00000000 },
85 { .size = 0x0, .offset = 0x0 },
86 { .size = 0x0, .offset = 0x0 },
87 },
88 .irq = 0, .chip = 0x9066104c,
89 .irq_type = IRQ_TYPE_EDGE_RISING,
90 .class = PCI_CLASS_NETWORK_OTHER,
91 .num_regs = 5,
92 .regs = {
93 {
94 .offset = 0x790,
95 .value = (0x60000000 - PHYS_OFFSET)
96 },
97 {
98 .offset = 0x794,
99 .value = (0x60000000 - PHYS_OFFSET)
100 },
101 { .offset = 0x740, .value = 0 },
102 { .offset = 0x744, .value = 0x00010000 },
103 { .offset = 0x764, .value = 0x00010000 },
104 },
105 },
106 };
107
108 static struct vlynq_device *slots[VLYNQ_PCI_SLOTS] = { NULL, };
109
110 static struct resource vlynq_io_resource = {
111 .start = 0x00000000,
112 .end = 0x00000000,
113 .name = "pci IO space",
114 .flags = IORESOURCE_IO
115 };
116
117 static struct resource vlynq_mem_resource = {
118 .start = 0x00000000,
119 .end = 0x00000000,
120 .name = "pci memory space",
121 .flags = IORESOURCE_MEM
122 };
123
124 static inline u32 vlynq_get_mapped(struct vlynq_device *dev, int res)
125 {
126 int i;
127 struct vlynq_pci_private *priv = dev->priv;
128 u32 ret = dev->mem_start;
129 if (!priv->config->rx_mapping[res].size) return 0;
130 for (i = 0; i < res; i++)
131 ret += priv->config->rx_mapping[i].size;
132
133 return ret;
134 }
135
136 static inline u32 vlynq_read(u32 val, int size) {
137 switch (size) {
138 case 1:
139 return *(u8 *)&val;
140 case 2:
141 return *(u16 *)&val;
142 }
143 return val;
144 }
145
146 static int vlynq_config_read(struct pci_bus *bus, unsigned int devfn,
147 int where, int size, u32 *val)
148 {
149 struct vlynq_device *dev;
150 struct vlynq_pci_private *priv;
151 int resno, slot = PCI_SLOT(devfn);
152
153 if ((size == 2) && (where & 1))
154 return PCIBIOS_BAD_REGISTER_NUMBER;
155 else if ((size == 4) && (where & 3))
156 return PCIBIOS_BAD_REGISTER_NUMBER;
157
158 if (slot >= VLYNQ_PCI_SLOTS)
159 return PCIBIOS_DEVICE_NOT_FOUND;
160
161 dev = slots[slot];
162
163 if (!dev || (PCI_FUNC(devfn) > 0))
164 return PCIBIOS_DEVICE_NOT_FOUND;
165
166 priv = dev->priv;
167
168 switch (where) {
169 case PCI_VENDOR_ID:
170 *val = vlynq_read(priv->config->chip, size);
171 break;
172 case PCI_DEVICE_ID:
173 *val = priv->config->chip & 0xffff;
174 case PCI_COMMAND:
175 *val = priv->command;
176 case PCI_STATUS:
177 /* *val = PCI_STATUS_CAP_LIST;*/
178 *val = 0;
179 break;
180 case PCI_CLASS_REVISION:
181 *val = priv->config->class;
182 break;
183 case PCI_LATENCY_TIMER:
184 *val = priv->latency;
185 break;
186 case PCI_HEADER_TYPE:
187 *val = PCI_HEADER_TYPE_NORMAL;
188 break;
189 case PCI_CACHE_LINE_SIZE:
190 *val = priv->cache_line;
191 break;
192 case PCI_BASE_ADDRESS_0:
193 case PCI_BASE_ADDRESS_1:
194 case PCI_BASE_ADDRESS_2:
195 case PCI_BASE_ADDRESS_3:
196 resno = (where - PCI_BASE_ADDRESS_0) >> 2;
197 if (priv->sz_mask & (1 << resno)) {
198 priv->sz_mask &= ~(1 << resno);
199 *val = priv->config->rx_mapping[resno].size;
200 } else {
201 *val = vlynq_get_mapped(dev, resno);
202 }
203 break;
204 case PCI_BASE_ADDRESS_4:
205 case PCI_BASE_ADDRESS_5:
206 case PCI_SUBSYSTEM_VENDOR_ID:
207 case PCI_SUBSYSTEM_ID:
208 case PCI_ROM_ADDRESS:
209 case PCI_INTERRUPT_LINE:
210 case PCI_CARDBUS_CIS:
211 case PCI_CAPABILITY_LIST:
212 *val = 0;
213 break;
214 case PCI_INTERRUPT_PIN:
215 *val = 1;
216 break;
217 default:
218 printk(KERN_NOTICE "%s: Read of unknown register 0x%x "
219 "(size %d)\n", dev->dev.bus_id, where, size);
220 return PCIBIOS_BAD_REGISTER_NUMBER;
221 }
222 return PCIBIOS_SUCCESSFUL;
223 }
224
225 static int vlynq_config_write(struct pci_bus *bus, unsigned int devfn,
226 int where, int size, u32 val)
227 {
228 struct vlynq_device *dev;
229 struct vlynq_pci_private *priv;
230 int resno, slot = PCI_SLOT(devfn);
231
232 if ((size == 2) && (where & 1))
233 return PCIBIOS_BAD_REGISTER_NUMBER;
234 else if ((size == 4) && (where & 3))
235 return PCIBIOS_BAD_REGISTER_NUMBER;
236
237 if (slot >= VLYNQ_PCI_SLOTS)
238 return PCIBIOS_DEVICE_NOT_FOUND;
239
240 dev = slots[slot];
241
242 if (!dev || (PCI_FUNC(devfn) > 0))
243 return PCIBIOS_DEVICE_NOT_FOUND;
244
245 priv = dev->priv;
246
247 switch (where) {
248 case PCI_VENDOR_ID:
249 case PCI_DEVICE_ID:
250 case PCI_STATUS:
251 case PCI_CLASS_REVISION:
252 case PCI_HEADER_TYPE:
253 case PCI_CACHE_LINE_SIZE:
254 case PCI_SUBSYSTEM_VENDOR_ID:
255 case PCI_SUBSYSTEM_ID:
256 case PCI_INTERRUPT_LINE:
257 case PCI_INTERRUPT_PIN:
258 case PCI_CARDBUS_CIS:
259 case PCI_CAPABILITY_LIST:
260 return PCIBIOS_FUNC_NOT_SUPPORTED;
261 case PCI_COMMAND:
262 priv->command = val;
263 case PCI_LATENCY_TIMER:
264 priv->latency = val;
265 break;
266 case PCI_BASE_ADDRESS_0:
267 case PCI_BASE_ADDRESS_1:
268 case PCI_BASE_ADDRESS_2:
269 case PCI_BASE_ADDRESS_3:
270 if (val == 0xffffffff) {
271 resno = (where - PCI_BASE_ADDRESS_0) >> 2;
272 priv->sz_mask |= (1 << resno);
273 break;
274 }
275 case PCI_BASE_ADDRESS_4:
276 case PCI_BASE_ADDRESS_5:
277 case PCI_ROM_ADDRESS:
278 break;
279 default:
280 printk(KERN_NOTICE "%s: Write to unknown register 0x%x "
281 "(size %d) value=0x%x\n", dev->dev.bus_id, where, size,
282 val);
283 return PCIBIOS_BAD_REGISTER_NUMBER;
284 }
285 return PCIBIOS_SUCCESSFUL;
286 }
287
288 static struct pci_ops vlynq_pci_ops = {
289 vlynq_config_read,
290 vlynq_config_write
291 };
292
293 static struct pci_controller vlynq_controller = {
294 .pci_ops = &vlynq_pci_ops,
295 .io_resource = &vlynq_io_resource,
296 .mem_resource = &vlynq_mem_resource,
297 };
298
299 static int vlynq_pci_probe(struct vlynq_device *dev)
300 {
301 int result, i;
302 u32 chip_id, addr;
303 struct vlynq_pci_private *priv;
304 struct vlynq_mapping mapping[4] = { { 0, }, };
305 struct vlynq_pci_config *config = NULL;
306
307 result = vlynq_set_local_irq(dev, 31);
308 if (result)
309 return result;
310
311 result = vlynq_set_remote_irq(dev, 30);
312 if (result)
313 return result;
314
315 dev->divisor = vlynq_ldiv4;
316 result = vlynq_device_enable(dev);
317 if (result)
318 return result;
319
320 chip_id = vlynq_remote_id(dev);
321 for (i = 0; i < ARRAY_SIZE(known_devices); i++)
322 if (chip_id == known_devices[i].chip_id)
323 config = &known_devices[i];
324
325 if (!config) {
326 printk(KERN_DEBUG "vlynq-pci: skipping unknown device "
327 "%04x:%04x at %s\n", chip_id >> 16,
328 chip_id & 0xffff, dev->dev.bus_id);
329 result = -ENODEV;
330 goto fail;
331 }
332
333 printk(KERN_NOTICE "vlynq-pci: attaching device %s at %s\n",
334 config->name, dev->dev.bus_id);
335
336 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
337 if (!priv) {
338 printk(KERN_ERR "%s: failed to allocate private data\n",
339 dev->dev.bus_id);
340 result = -ENOMEM;
341 goto fail;
342 }
343
344 priv->latency = 64;
345 priv->cache_line = 32;
346 priv->config = config;
347
348 mapping[0].offset = ARCH_PFN_OFFSET << PAGE_SHIFT;
349 mapping[0].size = 0x02000000;
350 vlynq_set_local_mapping(dev, dev->mem_start, mapping);
351 vlynq_set_remote_mapping(dev, 0, config->rx_mapping);
352
353 set_irq_type(vlynq_virq_to_irq(dev, config->irq), config->irq_type);
354
355 addr = (u32)ioremap_nocache(dev->mem_start, 0x10000);
356 if (!addr) {
357 printk(KERN_ERR "%s: failed to remap io memory\n",
358 dev->dev.bus_id);
359 result = -ENXIO;
360 goto fail;
361 }
362
363 for (i = 0; i < config->num_regs; i++)
364 iowrite32(config->regs[i].value,
365 (u32 *)(addr + config->regs[i].offset));
366
367 dev->priv = priv;
368 for (i = 0; i < VLYNQ_PCI_SLOTS; i++) {
369 if (!slots[i]) {
370 slots[i] = dev;
371 break;
372 }
373 }
374
375 return 0;
376
377 fail:
378 vlynq_device_disable(dev);
379
380 return result;
381 }
382
383 static int vlynq_pci_remove(struct vlynq_device *dev)
384 {
385 int i;
386 struct vlynq_pci_private *priv = dev->priv;
387
388 for (i = 0; i < VLYNQ_PCI_SLOTS; i++)
389 if (slots[i] == dev)
390 slots[i] = NULL;
391
392 vlynq_device_disable(dev);
393 kfree(priv);
394
395 return 0;
396 }
397
398 static struct vlynq_driver vlynq_pci = {
399 .name = "PCI over VLYNQ emulation",
400 .probe = vlynq_pci_probe,
401 .remove = vlynq_pci_remove,
402 };
403
404 int vlynq_pci_init(void)
405 {
406 int res;
407 res = vlynq_register_driver(&vlynq_pci);
408 if (res)
409 return res;
410
411 register_pci_controller(&vlynq_controller);
412
413 return 0;
414 }
415
416 int pcibios_map_irq(struct pci_dev *pdev, u8 slot, u8 pin)
417 {
418 struct vlynq_device *dev;
419 struct vlynq_pci_private *priv;
420
421 dev = slots[slot];
422
423 if (!dev)
424 return 0;
425
426 priv = dev->priv;
427
428 return vlynq_virq_to_irq(dev, priv->config->irq);
429 }
430
431 /* Do platform specific device initialization at pci_enable_device() time */
432 int pcibios_plat_dev_init(struct pci_dev *dev)
433 {
434 return 0;
435 }
This page took 0.065709 seconds and 3 git commands to generate.