2 * Copyright (C) 2006, 2007 OpenWrt.org
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.
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.
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
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>
26 #define VLYNQ_PCI_SLOTS 2
28 struct vlynq_reg_config
{
33 struct vlynq_pci_config
{
36 struct vlynq_mapping rx_mapping
[4];
42 struct vlynq_reg_config regs
[10];
45 struct vlynq_pci_private
{
50 struct vlynq_pci_config
*config
;
53 static struct vlynq_pci_config known_devices
[] = {
55 .chip_id
= 0x00000009, .name
= "TI TNETW1130",
57 { .size
= 0x22000, .offset
= 0xf0000000 },
58 { .size
= 0x40000, .offset
= 0xc0000000 },
59 { .size
= 0x0, .offset
= 0x0 },
60 { .size
= 0x0, .offset
= 0x0 },
62 .irq
= 0, .chip
= 0x9066104c,
63 .irq_type
= IRQ_TYPE_EDGE_RISING
,
64 .class = PCI_CLASS_NETWORK_OTHER
,
69 .value
= (0xd0000000 - PHYS_OFFSET
)
73 .value
= (0xd0000000 - PHYS_OFFSET
)
75 { .offset
= 0x740, .value
= 0 },
76 { .offset
= 0x744, .value
= 0x00010000 },
77 { .offset
= 0x764, .value
= 0x00010000 },
81 .chip_id
= 0x00000029, .name
= "TI TNETW1350",
83 { .size
= 0x100000, .offset
= 0x00300000 },
84 { .size
= 0x80000, .offset
= 0x00000000 },
85 { .size
= 0x0, .offset
= 0x0 },
86 { .size
= 0x0, .offset
= 0x0 },
88 .irq
= 0, .chip
= 0x9066104c,
89 .irq_type
= IRQ_TYPE_EDGE_RISING
,
90 .class = PCI_CLASS_NETWORK_OTHER
,
95 .value
= (0x60000000 - PHYS_OFFSET
)
99 .value
= (0x60000000 - PHYS_OFFSET
)
101 { .offset
= 0x740, .value
= 0 },
102 { .offset
= 0x744, .value
= 0x00010000 },
103 { .offset
= 0x764, .value
= 0x00010000 },
108 static struct vlynq_device
*slots
[VLYNQ_PCI_SLOTS
] = { NULL
, };
110 static struct resource vlynq_io_resource
= {
113 .name
= "pci IO space",
114 .flags
= IORESOURCE_IO
117 static struct resource vlynq_mem_resource
= {
120 .name
= "pci memory space",
121 .flags
= IORESOURCE_MEM
124 static inline u32
vlynq_get_mapped(struct vlynq_device
*dev
, int res
)
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
;
136 static inline u32
vlynq_read(u32 val
, int size
)
147 static int vlynq_config_read(struct pci_bus
*bus
, unsigned int devfn
,
148 int where
, int size
, u32
*val
)
150 struct vlynq_device
*dev
;
151 struct vlynq_pci_private
*priv
;
152 int resno
, slot
= PCI_SLOT(devfn
);
154 if ((size
== 2) && (where
& 1))
155 return PCIBIOS_BAD_REGISTER_NUMBER
;
156 else if ((size
== 4) && (where
& 3))
157 return PCIBIOS_BAD_REGISTER_NUMBER
;
159 if (slot
>= VLYNQ_PCI_SLOTS
)
160 return PCIBIOS_DEVICE_NOT_FOUND
;
164 if (!dev
|| (PCI_FUNC(devfn
) > 0))
165 return PCIBIOS_DEVICE_NOT_FOUND
;
171 *val
= vlynq_read(priv
->config
->chip
, size
);
174 *val
= priv
->config
->chip
& 0xffff;
176 *val
= priv
->command
;
178 /* *val = PCI_STATUS_CAP_LIST;*/
181 case PCI_CLASS_REVISION
:
182 *val
= priv
->config
->class;
184 case PCI_LATENCY_TIMER
:
185 *val
= priv
->latency
;
187 case PCI_HEADER_TYPE
:
188 *val
= PCI_HEADER_TYPE_NORMAL
;
190 case PCI_CACHE_LINE_SIZE
:
191 *val
= priv
->cache_line
;
193 case PCI_BASE_ADDRESS_0
:
194 case PCI_BASE_ADDRESS_1
:
195 case PCI_BASE_ADDRESS_2
:
196 case PCI_BASE_ADDRESS_3
:
197 resno
= (where
- PCI_BASE_ADDRESS_0
) >> 2;
198 if (priv
->sz_mask
& (1 << resno
)) {
199 priv
->sz_mask
&= ~(1 << resno
);
200 *val
= priv
->config
->rx_mapping
[resno
].size
;
202 *val
= vlynq_get_mapped(dev
, resno
);
205 case PCI_BASE_ADDRESS_4
:
206 case PCI_BASE_ADDRESS_5
:
207 case PCI_SUBSYSTEM_VENDOR_ID
:
208 case PCI_SUBSYSTEM_ID
:
209 case PCI_ROM_ADDRESS
:
210 case PCI_INTERRUPT_LINE
:
211 case PCI_CARDBUS_CIS
:
212 case PCI_CAPABILITY_LIST
:
215 case PCI_INTERRUPT_PIN
:
219 printk(KERN_NOTICE
"%s: Read of unknown register 0x%x "
220 "(size %d)\n", dev
->dev
.bus_id
, where
, size
);
221 return PCIBIOS_BAD_REGISTER_NUMBER
;
223 return PCIBIOS_SUCCESSFUL
;
226 static int vlynq_config_write(struct pci_bus
*bus
, unsigned int devfn
,
227 int where
, int size
, u32 val
)
229 struct vlynq_device
*dev
;
230 struct vlynq_pci_private
*priv
;
231 int resno
, slot
= PCI_SLOT(devfn
);
233 if ((size
== 2) && (where
& 1))
234 return PCIBIOS_BAD_REGISTER_NUMBER
;
235 else if ((size
== 4) && (where
& 3))
236 return PCIBIOS_BAD_REGISTER_NUMBER
;
238 if (slot
>= VLYNQ_PCI_SLOTS
)
239 return PCIBIOS_DEVICE_NOT_FOUND
;
243 if (!dev
|| (PCI_FUNC(devfn
) > 0))
244 return PCIBIOS_DEVICE_NOT_FOUND
;
252 case PCI_CLASS_REVISION
:
253 case PCI_HEADER_TYPE
:
254 case PCI_CACHE_LINE_SIZE
:
255 case PCI_SUBSYSTEM_VENDOR_ID
:
256 case PCI_SUBSYSTEM_ID
:
257 case PCI_INTERRUPT_LINE
:
258 case PCI_INTERRUPT_PIN
:
259 case PCI_CARDBUS_CIS
:
260 case PCI_CAPABILITY_LIST
:
261 return PCIBIOS_FUNC_NOT_SUPPORTED
;
264 case PCI_LATENCY_TIMER
:
267 case PCI_BASE_ADDRESS_0
:
268 case PCI_BASE_ADDRESS_1
:
269 case PCI_BASE_ADDRESS_2
:
270 case PCI_BASE_ADDRESS_3
:
271 if (val
== 0xffffffff) {
272 resno
= (where
- PCI_BASE_ADDRESS_0
) >> 2;
273 priv
->sz_mask
|= (1 << resno
);
276 case PCI_BASE_ADDRESS_4
:
277 case PCI_BASE_ADDRESS_5
:
278 case PCI_ROM_ADDRESS
:
281 printk(KERN_NOTICE
"%s: Write to unknown register 0x%x "
282 "(size %d) value=0x%x\n", dev
->dev
.bus_id
, where
, size
,
284 return PCIBIOS_BAD_REGISTER_NUMBER
;
286 return PCIBIOS_SUCCESSFUL
;
289 static struct pci_ops vlynq_pci_ops
= {
294 static struct pci_controller vlynq_controller
= {
295 .pci_ops
= &vlynq_pci_ops
,
296 .io_resource
= &vlynq_io_resource
,
297 .mem_resource
= &vlynq_mem_resource
,
300 static int vlynq_pci_probe(struct vlynq_device
*dev
)
304 struct vlynq_pci_private
*priv
;
305 struct vlynq_mapping mapping
[4] = { { 0, }, };
306 struct vlynq_pci_config
*config
= NULL
;
308 result
= vlynq_set_local_irq(dev
, 31);
312 result
= vlynq_set_remote_irq(dev
, 30);
316 dev
->divisor
= vlynq_div_auto
;
317 result
= vlynq_device_enable(dev
);
321 chip_id
= vlynq_remote_id(dev
);
322 for (i
= 0; i
< ARRAY_SIZE(known_devices
); i
++)
323 if (chip_id
== known_devices
[i
].chip_id
)
324 config
= &known_devices
[i
];
327 printk(KERN_DEBUG
"vlynq-pci: skipping unknown device "
328 "%04x:%04x at %s\n", chip_id
>> 16,
329 chip_id
& 0xffff, dev
->dev
.bus_id
);
334 printk(KERN_NOTICE
"vlynq-pci: attaching device %s at %s\n",
335 config
->name
, dev
->dev
.bus_id
);
337 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
339 printk(KERN_ERR
"%s: failed to allocate private data\n",
346 priv
->cache_line
= 32;
347 priv
->config
= config
;
349 mapping
[0].offset
= ARCH_PFN_OFFSET
<< PAGE_SHIFT
;
350 mapping
[0].size
= 0x02000000;
351 vlynq_set_local_mapping(dev
, dev
->mem_start
, mapping
);
352 vlynq_set_remote_mapping(dev
, 0, config
->rx_mapping
);
354 set_irq_type(vlynq_virq_to_irq(dev
, config
->irq
), config
->irq_type
);
356 addr
= (u32
)ioremap_nocache(dev
->mem_start
, 0x10000);
358 printk(KERN_ERR
"%s: failed to remap io memory\n",
364 for (i
= 0; i
< config
->num_regs
; i
++)
365 iowrite32(config
->regs
[i
].value
,
366 (u32
*)(addr
+ config
->regs
[i
].offset
));
369 for (i
= 0; i
< VLYNQ_PCI_SLOTS
; i
++) {
379 vlynq_device_disable(dev
);
384 static int vlynq_pci_remove(struct vlynq_device
*dev
)
387 struct vlynq_pci_private
*priv
= dev
->priv
;
389 for (i
= 0; i
< VLYNQ_PCI_SLOTS
; i
++)
393 vlynq_device_disable(dev
);
399 static struct vlynq_driver vlynq_pci
= {
400 .name
= "PCI over VLYNQ emulation",
401 .probe
= vlynq_pci_probe
,
402 .remove
= vlynq_pci_remove
,
405 int vlynq_pci_init(void)
408 res
= vlynq_register_driver(&vlynq_pci
);
412 register_pci_controller(&vlynq_controller
);
417 int pcibios_map_irq(struct pci_dev
*pdev
, u8 slot
, u8 pin
)
419 struct vlynq_device
*dev
;
420 struct vlynq_pci_private
*priv
;
429 return vlynq_virq_to_irq(dev
, priv
->config
->irq
);
432 /* Do platform specific device initialization at pci_enable_device() time */
433 int pcibios_plat_dev_init(struct pci_dev
*dev
)