e6be00a4d60dd6eb2e28bc5998e5b1f99ae857a3
[openwrt-10.03/.git] / target / linux / ar71xx / files / arch / mips / ar71xx / dev-ap94-pci.c
1 /*
2  *  Atheros AP94 reference board PCI initialization
3  *
4  *  Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #include <linux/pci.h>
12 #include <linux/ath9k_platform.h>
13 #include <linux/delay.h>
14
15 #include <asm/mach-ar71xx/ar71xx.h>
16 #include <asm/mach-ar71xx/pci.h>
17
18 #include "dev-ap94-pci.h"
19
20 static struct ath9k_platform_data ap94_wmac0_data = {
21         .led_pin = -1,
22 };
23 static struct ath9k_platform_data ap94_wmac1_data = {
24         .led_pin = -1,
25 };
26 static char ap94_wmac0_mac[6];
27 static char ap94_wmac1_mac[6];
28 static int ap94_pci_fixup_enabled;
29
30 static struct ar71xx_pci_irq ap94_pci_irqs[] __initdata = {
31         {
32                 .slot   = 0,
33                 .pin    = 1,
34                 .irq    = AR71XX_PCI_IRQ_DEV0,
35         }, {
36                 .slot   = 1,
37                 .pin    = 1,
38                 .irq    = AR71XX_PCI_IRQ_DEV1,
39         }
40 };
41
42 static int ap94_pci_plat_dev_init(struct pci_dev *dev)
43 {
44         switch(PCI_SLOT(dev->devfn)) {
45         case 17:
46                 dev->dev.platform_data = &ap94_wmac0_data;
47                 break;
48
49         case 18:
50                 dev->dev.platform_data = &ap94_wmac1_data;
51                 break;
52         }
53
54         return 0;
55 }
56
57 static void ap94_pci_fixup(struct pci_dev *dev)
58 {
59         void __iomem *mem;
60         u16 *cal_data;
61         u16 cmd;
62         u32 bar0;
63         u32 val;
64
65         if (!ap94_pci_fixup_enabled)
66                 return;
67
68         switch (PCI_SLOT(dev->devfn)) {
69         case 17:
70                 cal_data = ap94_wmac0_data.eeprom_data;
71                 break;
72         case 18:
73                 cal_data = ap94_wmac1_data.eeprom_data;
74                 break;
75         default:
76                 return;
77         }
78
79         if (*cal_data != 0xa55a) {
80                 printk(KERN_ERR "PCI: no calibration data found for %s\n",
81                        pci_name(dev));
82                 return;
83         }
84
85         mem = ioremap(AR71XX_PCI_MEM_BASE, 0x10000);
86         if (!mem) {
87                 printk(KERN_ERR "PCI: ioremap error for device %s\n",
88                        pci_name(dev));
89                 return;
90         }
91
92         printk(KERN_INFO "PCI: fixup device %s\n", pci_name(dev));
93
94         pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &bar0);
95
96         /* Setup the PCI device to allow access to the internal registers */
97         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, AR71XX_PCI_MEM_BASE);
98         pci_read_config_word(dev, PCI_COMMAND, &cmd);
99         cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
100         pci_write_config_word(dev, PCI_COMMAND, cmd);
101
102         /* set pointer to first reg address */
103         cal_data += 3;
104         while (*cal_data != 0xffff) {
105                 u32 reg;
106                 reg = *cal_data++;
107                 val = *cal_data++;
108                 val |= (*cal_data++) << 16;
109
110                 __raw_writel(val, mem + reg);
111                 udelay(100);
112         }
113
114         pci_read_config_dword(dev, PCI_VENDOR_ID, &val);
115         dev->vendor = val & 0xffff;
116         dev->device = (val >> 16) & 0xffff;
117
118         pci_read_config_dword(dev, PCI_CLASS_REVISION, &val);
119         dev->revision = val & 0xff;
120         dev->class = val >> 8; /* upper 3 bytes */
121
122         pci_read_config_word(dev, PCI_COMMAND, &cmd);
123         cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
124         pci_write_config_word(dev, PCI_COMMAND, cmd);
125
126         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
127
128         iounmap(mem);
129 }
130 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_ATHEROS, PCI_ANY_ID, ap94_pci_fixup);
131
132 void __init ap94_pci_enable_quirk_wndr3700(void)
133 {
134         /* WNDR3700 uses GPIO 6-9 for antenna configuration */
135
136         ap94_wmac0_data.led_pin = 5;
137         ap94_wmac0_data.gpio_mask = (0xf << 6);
138         /* 2.4 GHz uses the first fixed antenna group (0, 1, 0, 1) */
139         ap94_wmac0_data.gpio_val = (10 << 6);
140
141         ap94_wmac1_data.led_pin = 5;
142         ap94_wmac1_data.gpio_mask = (0xf << 6);
143         /* 5 GHz uses the second fixed antenna group (0, 1, 1, 0) */
144         ap94_wmac1_data.gpio_val = (6 << 6);
145 }
146
147 void __init ap94_pci_init(u8 *cal_data0, u8 *mac_addr0,
148                           u8 *cal_data1, u8 *mac_addr1)
149 {
150         if (cal_data0)
151                 memcpy(ap94_wmac0_data.eeprom_data, cal_data0,
152                        sizeof(ap94_wmac0_data.eeprom_data));
153
154         if (cal_data1)
155                 memcpy(ap94_wmac1_data.eeprom_data, cal_data1,
156                        sizeof(ap94_wmac1_data.eeprom_data));
157
158         if (mac_addr0) {
159                 memcpy(ap94_wmac0_mac, mac_addr0, sizeof(ap94_wmac0_mac));
160                 ap94_wmac0_data.macaddr = ap94_wmac0_mac;
161         }
162
163         if (mac_addr1) {
164                 memcpy(ap94_wmac1_mac, mac_addr1, sizeof(ap94_wmac1_mac));
165                 ap94_wmac1_data.macaddr = ap94_wmac1_mac;
166         }
167
168         ar71xx_pci_plat_dev_init = ap94_pci_plat_dev_init;
169         ar71xx_pci_init(ARRAY_SIZE(ap94_pci_irqs), ap94_pci_irqs);
170
171         ap94_pci_fixup_enabled = 1;
172 }