Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6.git] / drivers / bcma / sprom.c
1 /*
2  * Broadcom specific AMBA
3  * SPROM reading
4  *
5  * Licensed under the GNU/GPL. See COPYING for details.
6  */
7
8 #include "bcma_private.h"
9
10 #include <linux/bcma/bcma.h>
11 #include <linux/bcma/bcma_regs.h>
12 #include <linux/pci.h>
13 #include <linux/io.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/slab.h>
16
17 #define SPOFF(offset)   ((offset) / sizeof(u16))
18
19 /**************************************************
20  * R/W ops.
21  **************************************************/
22
23 static void bcma_sprom_read(struct bcma_bus *bus, u16 offset, u16 *sprom)
24 {
25         int i;
26         for (i = 0; i < SSB_SPROMSIZE_WORDS_R4; i++)
27                 sprom[i] = bcma_read16(bus->drv_cc.core,
28                                        offset + (i * 2));
29 }
30
31 /**************************************************
32  * Validation.
33  **************************************************/
34
35 static inline u8 bcma_crc8(u8 crc, u8 data)
36 {
37         /* Polynomial:   x^8 + x^7 + x^6 + x^4 + x^2 + 1   */
38         static const u8 t[] = {
39                 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B,
40                 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21,
41                 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF,
42                 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5,
43                 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14,
44                 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E,
45                 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80,
46                 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA,
47                 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95,
48                 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF,
49                 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01,
50                 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B,
51                 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA,
52                 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0,
53                 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E,
54                 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34,
55                 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0,
56                 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A,
57                 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54,
58                 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E,
59                 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF,
60                 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5,
61                 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B,
62                 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61,
63                 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E,
64                 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74,
65                 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA,
66                 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0,
67                 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41,
68                 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B,
69                 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5,
70                 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F,
71         };
72         return t[crc ^ data];
73 }
74
75 static u8 bcma_sprom_crc(const u16 *sprom)
76 {
77         int word;
78         u8 crc = 0xFF;
79
80         for (word = 0; word < SSB_SPROMSIZE_WORDS_R4 - 1; word++) {
81                 crc = bcma_crc8(crc, sprom[word] & 0x00FF);
82                 crc = bcma_crc8(crc, (sprom[word] & 0xFF00) >> 8);
83         }
84         crc = bcma_crc8(crc, sprom[SSB_SPROMSIZE_WORDS_R4 - 1] & 0x00FF);
85         crc ^= 0xFF;
86
87         return crc;
88 }
89
90 static int bcma_sprom_check_crc(const u16 *sprom)
91 {
92         u8 crc;
93         u8 expected_crc;
94         u16 tmp;
95
96         crc = bcma_sprom_crc(sprom);
97         tmp = sprom[SSB_SPROMSIZE_WORDS_R4 - 1] & SSB_SPROM_REVISION_CRC;
98         expected_crc = tmp >> SSB_SPROM_REVISION_CRC_SHIFT;
99         if (crc != expected_crc)
100                 return -EPROTO;
101
102         return 0;
103 }
104
105 static int bcma_sprom_valid(const u16 *sprom)
106 {
107         u16 revision;
108         int err;
109
110         err = bcma_sprom_check_crc(sprom);
111         if (err)
112                 return err;
113
114         revision = sprom[SSB_SPROMSIZE_WORDS_R4 - 1] & SSB_SPROM_REVISION_REV;
115         if (revision != 8 && revision != 9) {
116                 pr_err("Unsupported SPROM revision: %d\n", revision);
117                 return -ENOENT;
118         }
119
120         return 0;
121 }
122
123 /**************************************************
124  * SPROM extraction.
125  **************************************************/
126
127 static void bcma_sprom_extract_r8(struct bcma_bus *bus, const u16 *sprom)
128 {
129         u16 v;
130         int i;
131
132         bus->sprom.revision = sprom[SSB_SPROMSIZE_WORDS_R4 - 1] &
133                 SSB_SPROM_REVISION_REV;
134
135         for (i = 0; i < 3; i++) {
136                 v = sprom[SPOFF(SSB_SPROM8_IL0MAC) + i];
137                 *(((__be16 *)bus->sprom.il0mac) + i) = cpu_to_be16(v);
138         }
139
140         bus->sprom.board_rev = sprom[SPOFF(SSB_SPROM8_BOARDREV)];
141
142         bus->sprom.txpid2g[0] = (sprom[SPOFF(SSB_SPROM4_TXPID2G01)] &
143              SSB_SPROM4_TXPID2G0) >> SSB_SPROM4_TXPID2G0_SHIFT;
144         bus->sprom.txpid2g[1] = (sprom[SPOFF(SSB_SPROM4_TXPID2G01)] &
145              SSB_SPROM4_TXPID2G1) >> SSB_SPROM4_TXPID2G1_SHIFT;
146         bus->sprom.txpid2g[2] = (sprom[SPOFF(SSB_SPROM4_TXPID2G23)] &
147              SSB_SPROM4_TXPID2G2) >> SSB_SPROM4_TXPID2G2_SHIFT;
148         bus->sprom.txpid2g[3] = (sprom[SPOFF(SSB_SPROM4_TXPID2G23)] &
149              SSB_SPROM4_TXPID2G3) >> SSB_SPROM4_TXPID2G3_SHIFT;
150
151         bus->sprom.txpid5gl[0] = (sprom[SPOFF(SSB_SPROM4_TXPID5GL01)] &
152              SSB_SPROM4_TXPID5GL0) >> SSB_SPROM4_TXPID5GL0_SHIFT;
153         bus->sprom.txpid5gl[1] = (sprom[SPOFF(SSB_SPROM4_TXPID5GL01)] &
154              SSB_SPROM4_TXPID5GL1) >> SSB_SPROM4_TXPID5GL1_SHIFT;
155         bus->sprom.txpid5gl[2] = (sprom[SPOFF(SSB_SPROM4_TXPID5GL23)] &
156              SSB_SPROM4_TXPID5GL2) >> SSB_SPROM4_TXPID5GL2_SHIFT;
157         bus->sprom.txpid5gl[3] = (sprom[SPOFF(SSB_SPROM4_TXPID5GL23)] &
158              SSB_SPROM4_TXPID5GL3) >> SSB_SPROM4_TXPID5GL3_SHIFT;
159
160         bus->sprom.txpid5g[0] = (sprom[SPOFF(SSB_SPROM4_TXPID5G01)] &
161              SSB_SPROM4_TXPID5G0) >> SSB_SPROM4_TXPID5G0_SHIFT;
162         bus->sprom.txpid5g[1] = (sprom[SPOFF(SSB_SPROM4_TXPID5G01)] &
163              SSB_SPROM4_TXPID5G1) >> SSB_SPROM4_TXPID5G1_SHIFT;
164         bus->sprom.txpid5g[2] = (sprom[SPOFF(SSB_SPROM4_TXPID5G23)] &
165              SSB_SPROM4_TXPID5G2) >> SSB_SPROM4_TXPID5G2_SHIFT;
166         bus->sprom.txpid5g[3] = (sprom[SPOFF(SSB_SPROM4_TXPID5G23)] &
167              SSB_SPROM4_TXPID5G3) >> SSB_SPROM4_TXPID5G3_SHIFT;
168
169         bus->sprom.txpid5gh[0] = (sprom[SPOFF(SSB_SPROM4_TXPID5GH01)] &
170              SSB_SPROM4_TXPID5GH0) >> SSB_SPROM4_TXPID5GH0_SHIFT;
171         bus->sprom.txpid5gh[1] = (sprom[SPOFF(SSB_SPROM4_TXPID5GH01)] &
172              SSB_SPROM4_TXPID5GH1) >> SSB_SPROM4_TXPID5GH1_SHIFT;
173         bus->sprom.txpid5gh[2] = (sprom[SPOFF(SSB_SPROM4_TXPID5GH23)] &
174              SSB_SPROM4_TXPID5GH2) >> SSB_SPROM4_TXPID5GH2_SHIFT;
175         bus->sprom.txpid5gh[3] = (sprom[SPOFF(SSB_SPROM4_TXPID5GH23)] &
176              SSB_SPROM4_TXPID5GH3) >> SSB_SPROM4_TXPID5GH3_SHIFT;
177
178         bus->sprom.boardflags_lo = sprom[SPOFF(SSB_SPROM8_BFLLO)];
179         bus->sprom.boardflags_hi = sprom[SPOFF(SSB_SPROM8_BFLHI)];
180         bus->sprom.boardflags2_lo = sprom[SPOFF(SSB_SPROM8_BFL2LO)];
181         bus->sprom.boardflags2_hi = sprom[SPOFF(SSB_SPROM8_BFL2HI)];
182
183         bus->sprom.country_code = sprom[SPOFF(SSB_SPROM8_CCODE)];
184
185         bus->sprom.fem.ghz2.tssipos = (sprom[SPOFF(SSB_SPROM8_FEM2G)] &
186                 SSB_SROM8_FEM_TSSIPOS) >> SSB_SROM8_FEM_TSSIPOS_SHIFT;
187         bus->sprom.fem.ghz2.extpa_gain = (sprom[SPOFF(SSB_SPROM8_FEM2G)] &
188                 SSB_SROM8_FEM_EXTPA_GAIN) >> SSB_SROM8_FEM_EXTPA_GAIN_SHIFT;
189         bus->sprom.fem.ghz2.pdet_range = (sprom[SPOFF(SSB_SPROM8_FEM2G)] &
190                 SSB_SROM8_FEM_PDET_RANGE) >> SSB_SROM8_FEM_PDET_RANGE_SHIFT;
191         bus->sprom.fem.ghz2.tr_iso = (sprom[SPOFF(SSB_SPROM8_FEM2G)] &
192                 SSB_SROM8_FEM_TR_ISO) >> SSB_SROM8_FEM_TR_ISO_SHIFT;
193         bus->sprom.fem.ghz2.antswlut = (sprom[SPOFF(SSB_SPROM8_FEM2G)] &
194                 SSB_SROM8_FEM_ANTSWLUT) >> SSB_SROM8_FEM_ANTSWLUT_SHIFT;
195
196         bus->sprom.fem.ghz5.tssipos = (sprom[SPOFF(SSB_SPROM8_FEM5G)] &
197                 SSB_SROM8_FEM_TSSIPOS) >> SSB_SROM8_FEM_TSSIPOS_SHIFT;
198         bus->sprom.fem.ghz5.extpa_gain = (sprom[SPOFF(SSB_SPROM8_FEM5G)] &
199                 SSB_SROM8_FEM_EXTPA_GAIN) >> SSB_SROM8_FEM_EXTPA_GAIN_SHIFT;
200         bus->sprom.fem.ghz5.pdet_range = (sprom[SPOFF(SSB_SPROM8_FEM5G)] &
201                 SSB_SROM8_FEM_PDET_RANGE) >> SSB_SROM8_FEM_PDET_RANGE_SHIFT;
202         bus->sprom.fem.ghz5.tr_iso = (sprom[SPOFF(SSB_SPROM8_FEM5G)] &
203                 SSB_SROM8_FEM_TR_ISO) >> SSB_SROM8_FEM_TR_ISO_SHIFT;
204         bus->sprom.fem.ghz5.antswlut = (sprom[SPOFF(SSB_SPROM8_FEM5G)] &
205                 SSB_SROM8_FEM_ANTSWLUT) >> SSB_SROM8_FEM_ANTSWLUT_SHIFT;
206 }
207
208 int bcma_sprom_get(struct bcma_bus *bus)
209 {
210         u16 offset;
211         u16 *sprom;
212         int err = 0;
213
214         if (!bus->drv_cc.core)
215                 return -EOPNOTSUPP;
216
217         if (!(bus->drv_cc.capabilities & BCMA_CC_CAP_SPROM))
218                 return -ENOENT;
219
220         sprom = kcalloc(SSB_SPROMSIZE_WORDS_R4, sizeof(u16),
221                         GFP_KERNEL);
222         if (!sprom)
223                 return -ENOMEM;
224
225         if (bus->chipinfo.id == 0x4331)
226                 bcma_chipco_bcm4331_ext_pa_lines_ctl(&bus->drv_cc, false);
227
228         /* Most cards have SPROM moved by additional offset 0x30 (48 dwords).
229          * According to brcm80211 this applies to cards with PCIe rev >= 6
230          * TODO: understand this condition and use it */
231         offset = (bus->chipinfo.id == 0x4331) ? BCMA_CC_SPROM :
232                 BCMA_CC_SPROM_PCIE6;
233         bcma_sprom_read(bus, offset, sprom);
234
235         if (bus->chipinfo.id == 0x4331)
236                 bcma_chipco_bcm4331_ext_pa_lines_ctl(&bus->drv_cc, true);
237
238         err = bcma_sprom_valid(sprom);
239         if (err)
240                 goto out;
241
242         bcma_sprom_extract_r8(bus, sprom);
243
244 out:
245         kfree(sprom);
246         return err;
247 }