brcm80211: Remove bogus memcpy in ai_detach
[pandora-kernel.git] / drivers / net / wireless / brcm80211 / brcmsmac / aiutils.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * File contents: support functions for PCI/PCIe
17  */
18
19 #include <linux/delay.h>
20 #include <linux/pci.h>
21
22 #include <defs.h>
23 #include <chipcommon.h>
24 #include <brcmu_utils.h>
25 #include <brcm_hw_ids.h>
26 #include <soc.h>
27 #include "types.h"
28 #include "pub.h"
29 #include "pmu.h"
30 #include "srom.h"
31 #include "nicpci.h"
32 #include "aiutils.h"
33
34 /* slow_clk_ctl */
35  /* slow clock source mask */
36 #define SCC_SS_MASK             0x00000007
37  /* source of slow clock is LPO */
38 #define SCC_SS_LPO              0x00000000
39  /* source of slow clock is crystal */
40 #define SCC_SS_XTAL             0x00000001
41  /* source of slow clock is PCI */
42 #define SCC_SS_PCI              0x00000002
43  /* LPOFreqSel, 1: 160Khz, 0: 32KHz */
44 #define SCC_LF                  0x00000200
45  /* LPOPowerDown, 1: LPO is disabled, 0: LPO is enabled */
46 #define SCC_LP                  0x00000400
47  /* ForceSlowClk, 1: sb/cores running on slow clock, 0: power logic control */
48 #define SCC_FS                  0x00000800
49  /* IgnorePllOffReq, 1/0:
50   *  power logic ignores/honors PLL clock disable requests from core
51   */
52 #define SCC_IP                  0x00001000
53  /* XtalControlEn, 1/0:
54   *  power logic does/doesn't disable crystal when appropriate
55   */
56 #define SCC_XC                  0x00002000
57  /* XtalPU (RO), 1/0: crystal running/disabled */
58 #define SCC_XP                  0x00004000
59  /* ClockDivider (SlowClk = 1/(4+divisor)) */
60 #define SCC_CD_MASK             0xffff0000
61 #define SCC_CD_SHIFT            16
62
63 /* system_clk_ctl */
64  /* ILPen: Enable Idle Low Power */
65 #define SYCC_IE                 0x00000001
66  /* ALPen: Enable Active Low Power */
67 #define SYCC_AE                 0x00000002
68  /* ForcePLLOn */
69 #define SYCC_FP                 0x00000004
70  /* Force ALP (or HT if ALPen is not set */
71 #define SYCC_AR                 0x00000008
72  /* Force HT */
73 #define SYCC_HR                 0x00000010
74  /* ClkDiv  (ILP = 1/(4 * (divisor + 1)) */
75 #define SYCC_CD_MASK            0xffff0000
76 #define SYCC_CD_SHIFT           16
77
78 #define CST4329_SPROM_OTP_SEL_MASK      0x00000003
79  /* OTP is powered up, use def. CIS, no SPROM */
80 #define CST4329_DEFCIS_SEL              0
81  /* OTP is powered up, SPROM is present */
82 #define CST4329_SPROM_SEL               1
83  /* OTP is powered up, no SPROM */
84 #define CST4329_OTP_SEL                 2
85  /* OTP is powered down, SPROM is present */
86 #define CST4329_OTP_PWRDN               3
87
88 #define CST4329_SPI_SDIO_MODE_MASK      0x00000004
89 #define CST4329_SPI_SDIO_MODE_SHIFT     2
90
91 /* 43224 chip-specific ChipControl register bits */
92 #define CCTRL43224_GPIO_TOGGLE          0x8000
93  /* 12 mA drive strength */
94 #define CCTRL_43224A0_12MA_LED_DRIVE    0x00F000F0
95  /* 12 mA drive strength for later 43224s */
96 #define CCTRL_43224B0_12MA_LED_DRIVE    0xF0
97
98 /* 43236 Chip specific ChipStatus register bits */
99 #define CST43236_SFLASH_MASK            0x00000040
100 #define CST43236_OTP_MASK               0x00000080
101 #define CST43236_HSIC_MASK              0x00000100      /* USB/HSIC */
102 #define CST43236_BP_CLK                 0x00000200      /* 120/96Mbps */
103 #define CST43236_BOOT_MASK              0x00001800
104 #define CST43236_BOOT_SHIFT             11
105 #define CST43236_BOOT_FROM_SRAM         0 /* boot from SRAM, ARM in reset */
106 #define CST43236_BOOT_FROM_ROM          1 /* boot from ROM */
107 #define CST43236_BOOT_FROM_FLASH        2 /* boot from FLASH */
108 #define CST43236_BOOT_FROM_INVALID      3
109
110 /* 4331 chip-specific ChipControl register bits */
111  /* 0 disable */
112 #define CCTRL4331_BT_COEXIST            (1<<0)
113  /* 0 SECI is disabled (JTAG functional) */
114 #define CCTRL4331_SECI                  (1<<1)
115  /* 0 disable */
116 #define CCTRL4331_EXT_LNA               (1<<2)
117  /* sprom/gpio13-15 mux */
118 #define CCTRL4331_SPROM_GPIO13_15       (1<<3)
119  /* 0 ext pa disable, 1 ext pa enabled */
120 #define CCTRL4331_EXTPA_EN              (1<<4)
121  /* set drive out GPIO_CLK on sprom_cs pin */
122 #define CCTRL4331_GPIOCLK_ON_SPROMCS    (1<<5)
123  /* use sprom_cs pin as PCIE mdio interface */
124 #define CCTRL4331_PCIE_MDIO_ON_SPROMCS  (1<<6)
125  /* aband extpa will be at gpio2/5 and sprom_dout */
126 #define CCTRL4331_EXTPA_ON_GPIO2_5      (1<<7)
127  /* override core control on pipe_AuxClkEnable */
128 #define CCTRL4331_OVR_PIPEAUXCLKEN      (1<<8)
129  /* override core control on pipe_AuxPowerDown */
130 #define CCTRL4331_OVR_PIPEAUXPWRDOWN    (1<<9)
131  /* pcie_auxclkenable */
132 #define CCTRL4331_PCIE_AUXCLKEN         (1<<10)
133  /* pcie_pipe_pllpowerdown */
134 #define CCTRL4331_PCIE_PIPE_PLLDOWN     (1<<11)
135  /* enable bt_shd0 at gpio4 */
136 #define CCTRL4331_BT_SHD0_ON_GPIO4      (1<<16)
137  /* enable bt_shd1 at gpio5 */
138 #define CCTRL4331_BT_SHD1_ON_GPIO5      (1<<17)
139
140 /* 4331 Chip specific ChipStatus register bits */
141  /* crystal frequency 20/40Mhz */
142 #define CST4331_XTAL_FREQ               0x00000001
143 #define CST4331_SPROM_PRESENT           0x00000002
144 #define CST4331_OTP_PRESENT             0x00000004
145 #define CST4331_LDO_RF                  0x00000008
146 #define CST4331_LDO_PAR                 0x00000010
147
148 /* 4319 chip-specific ChipStatus register bits */
149 #define CST4319_SPI_CPULESSUSB          0x00000001
150 #define CST4319_SPI_CLK_POL             0x00000002
151 #define CST4319_SPI_CLK_PH              0x00000008
152  /* gpio [7:6], SDIO CIS selection */
153 #define CST4319_SPROM_OTP_SEL_MASK      0x000000c0
154 #define CST4319_SPROM_OTP_SEL_SHIFT     6
155  /* use default CIS, OTP is powered up */
156 #define CST4319_DEFCIS_SEL              0x00000000
157  /* use SPROM, OTP is powered up */
158 #define CST4319_SPROM_SEL               0x00000040
159  /* use OTP, OTP is powered up */
160 #define CST4319_OTP_SEL                 0x00000080
161  /* use SPROM, OTP is powered down */
162 #define CST4319_OTP_PWRDN               0x000000c0
163  /* gpio [8], sdio/usb mode */
164 #define CST4319_SDIO_USB_MODE           0x00000100
165 #define CST4319_REMAP_SEL_MASK          0x00000600
166 #define CST4319_ILPDIV_EN               0x00000800
167 #define CST4319_XTAL_PD_POL             0x00001000
168 #define CST4319_LPO_SEL                 0x00002000
169 #define CST4319_RES_INIT_MODE           0x0000c000
170  /* PALDO is configured with external PNP */
171 #define CST4319_PALDO_EXTPNP            0x00010000
172 #define CST4319_CBUCK_MODE_MASK         0x00060000
173 #define CST4319_CBUCK_MODE_BURST        0x00020000
174 #define CST4319_CBUCK_MODE_LPBURST      0x00060000
175 #define CST4319_RCAL_VALID              0x01000000
176 #define CST4319_RCAL_VALUE_MASK         0x3e000000
177 #define CST4319_RCAL_VALUE_SHIFT        25
178
179 /* 4336 chip-specific ChipStatus register bits */
180 #define CST4336_SPI_MODE_MASK           0x00000001
181 #define CST4336_SPROM_PRESENT           0x00000002
182 #define CST4336_OTP_PRESENT             0x00000004
183 #define CST4336_ARMREMAP_0              0x00000008
184 #define CST4336_ILPDIV_EN_MASK          0x00000010
185 #define CST4336_ILPDIV_EN_SHIFT         4
186 #define CST4336_XTAL_PD_POL_MASK        0x00000020
187 #define CST4336_XTAL_PD_POL_SHIFT       5
188 #define CST4336_LPO_SEL_MASK            0x00000040
189 #define CST4336_LPO_SEL_SHIFT           6
190 #define CST4336_RES_INIT_MODE_MASK      0x00000180
191 #define CST4336_RES_INIT_MODE_SHIFT     7
192 #define CST4336_CBUCK_MODE_MASK         0x00000600
193 #define CST4336_CBUCK_MODE_SHIFT        9
194
195 /* 4313 chip-specific ChipStatus register bits */
196 #define CST4313_SPROM_PRESENT                   1
197 #define CST4313_OTP_PRESENT                     2
198 #define CST4313_SPROM_OTP_SEL_MASK              0x00000002
199 #define CST4313_SPROM_OTP_SEL_SHIFT             0
200
201 /* 4313 Chip specific ChipControl register bits */
202  /* 12 mA drive strengh for later 4313 */
203 #define CCTRL_4313_12MA_LED_DRIVE    0x00000007
204
205 /* Manufacturer Ids */
206 #define MFGID_ARM               0x43b
207 #define MFGID_BRCM              0x4bf
208 #define MFGID_MIPS              0x4a7
209
210 /* Enumeration ROM registers */
211 #define ER_EROMENTRY            0x000
212 #define ER_REMAPCONTROL         0xe00
213 #define ER_REMAPSELECT          0xe04
214 #define ER_MASTERSELECT         0xe10
215 #define ER_ITCR                 0xf00
216 #define ER_ITIP                 0xf04
217
218 /* Erom entries */
219 #define ER_TAG                  0xe
220 #define ER_TAG1                 0x6
221 #define ER_VALID                1
222 #define ER_CI                   0
223 #define ER_MP                   2
224 #define ER_ADD                  4
225 #define ER_END                  0xe
226 #define ER_BAD                  0xffffffff
227
228 /* EROM CompIdentA */
229 #define CIA_MFG_MASK            0xfff00000
230 #define CIA_MFG_SHIFT           20
231 #define CIA_CID_MASK            0x000fff00
232 #define CIA_CID_SHIFT           8
233 #define CIA_CCL_MASK            0x000000f0
234 #define CIA_CCL_SHIFT           4
235
236 /* EROM CompIdentB */
237 #define CIB_REV_MASK            0xff000000
238 #define CIB_REV_SHIFT           24
239 #define CIB_NSW_MASK            0x00f80000
240 #define CIB_NSW_SHIFT           19
241 #define CIB_NMW_MASK            0x0007c000
242 #define CIB_NMW_SHIFT           14
243 #define CIB_NSP_MASK            0x00003e00
244 #define CIB_NSP_SHIFT           9
245 #define CIB_NMP_MASK            0x000001f0
246 #define CIB_NMP_SHIFT           4
247
248 /* EROM AddrDesc */
249 #define AD_ADDR_MASK            0xfffff000
250 #define AD_SP_MASK              0x00000f00
251 #define AD_SP_SHIFT             8
252 #define AD_ST_MASK              0x000000c0
253 #define AD_ST_SHIFT             6
254 #define AD_ST_SLAVE             0x00000000
255 #define AD_ST_BRIDGE            0x00000040
256 #define AD_ST_SWRAP             0x00000080
257 #define AD_ST_MWRAP             0x000000c0
258 #define AD_SZ_MASK              0x00000030
259 #define AD_SZ_SHIFT             4
260 #define AD_SZ_4K                0x00000000
261 #define AD_SZ_8K                0x00000010
262 #define AD_SZ_16K               0x00000020
263 #define AD_SZ_SZD               0x00000030
264 #define AD_AG32                 0x00000008
265 #define AD_ADDR_ALIGN           0x00000fff
266 #define AD_SZ_BASE              0x00001000      /* 4KB */
267
268 /* EROM SizeDesc */
269 #define SD_SZ_MASK              0xfffff000
270 #define SD_SG32                 0x00000008
271 #define SD_SZ_ALIGN             0x00000fff
272
273 /* PCI config space bit 4 for 4306c0 slow clock source */
274 #define PCI_CFG_GPIO_SCS        0x10
275 /* PCI config space GPIO 14 for Xtal power-up */
276 #define PCI_CFG_GPIO_XTAL       0x40
277 /* PCI config space GPIO 15 for PLL power-down */
278 #define PCI_CFG_GPIO_PLL        0x80
279
280 /* power control defines */
281 #define PLL_DELAY               150     /* us pll on delay */
282 #define FREF_DELAY              200     /* us fref change delay */
283 #define XTAL_ON_DELAY           1000    /* us crystal power-on delay */
284
285 /* resetctrl */
286 #define AIRC_RESET              1
287
288 #define NOREV           -1      /* Invalid rev */
289
290 /* GPIO Based LED powersave defines */
291 #define DEFAULT_GPIO_ONTIME     10      /* Default: 10% on */
292 #define DEFAULT_GPIO_OFFTIME    90      /* Default: 10% on */
293
294 /* When Srom support present, fields in sromcontrol */
295 #define SRC_START               0x80000000
296 #define SRC_BUSY                0x80000000
297 #define SRC_OPCODE              0x60000000
298 #define SRC_OP_READ             0x00000000
299 #define SRC_OP_WRITE            0x20000000
300 #define SRC_OP_WRDIS            0x40000000
301 #define SRC_OP_WREN             0x60000000
302 #define SRC_OTPSEL              0x00000010
303 #define SRC_LOCK                0x00000008
304 #define SRC_SIZE_MASK           0x00000006
305 #define SRC_SIZE_1K             0x00000000
306 #define SRC_SIZE_4K             0x00000002
307 #define SRC_SIZE_16K            0x00000004
308 #define SRC_SIZE_SHIFT          1
309 #define SRC_PRESENT             0x00000001
310
311 /* External PA enable mask */
312 #define GPIO_CTRL_EPA_EN_MASK 0x40
313
314 #define DEFAULT_GPIOTIMERVAL \
315         ((DEFAULT_GPIO_ONTIME << GPIO_ONTIME_SHIFT) | DEFAULT_GPIO_OFFTIME)
316
317 #define BADIDX          (SI_MAXCORES + 1)
318
319 /* Newer chips can access PCI/PCIE and CC core without requiring to change
320  * PCI BAR0 WIN
321  */
322 #define SI_FAST(si) (((si)->pub.buscoretype == PCIE_CORE_ID) || \
323                      (((si)->pub.buscoretype == PCI_CORE_ID) && \
324                       (si)->pub.buscorerev >= 13))
325
326 #define CCREGS_FAST(si) (((char __iomem *)((si)->curmap) + \
327                           PCI_16KB0_CCREGS_OFFSET))
328
329 #define IS_SIM(chippkg) \
330         ((chippkg == HDLSIM_PKG_ID) || (chippkg == HWSIM_PKG_ID))
331
332 /*
333  * Macros to disable/restore function core(D11, ENET, ILINE20, etc) interrupts
334  * before after core switching to avoid invalid register accesss inside ISR.
335  */
336 #define INTR_OFF(si, intr_val) \
337         if ((si)->intrsoff_fn && \
338             (si)->coreid[(si)->curidx] == (si)->dev_coreid) \
339                 intr_val = (*(si)->intrsoff_fn)((si)->intr_arg)
340
341 #define INTR_RESTORE(si, intr_val) \
342         if ((si)->intrsrestore_fn && \
343             (si)->coreid[(si)->curidx] == (si)->dev_coreid) \
344                 (*(si)->intrsrestore_fn)((si)->intr_arg, intr_val)
345
346 #define PCI(si)         ((si)->pub.buscoretype == PCI_CORE_ID)
347 #define PCIE(si)        ((si)->pub.buscoretype == PCIE_CORE_ID)
348
349 #define PCI_FORCEHT(si) (PCIE(si) && (si->pub.chip == BCM4716_CHIP_ID))
350
351 #ifdef BCMDBG
352 #define SI_MSG(args)    printk args
353 #else
354 #define SI_MSG(args)
355 #endif                          /* BCMDBG */
356
357 #define GOODCOREADDR(x, b) \
358         (((x) >= (b)) && ((x) < ((b) + SI_MAXCORES * SI_CORE_SIZE)) && \
359                 IS_ALIGNED((x), SI_CORE_SIZE))
360
361 #define PCIEREGS(si) ((__iomem char *)((si)->curmap) + \
362                         PCI_16KB0_PCIREGS_OFFSET)
363
364 struct aidmp {
365         u32 oobselina30;        /* 0x000 */
366         u32 oobselina74;        /* 0x004 */
367         u32 PAD[6];
368         u32 oobselinb30;        /* 0x020 */
369         u32 oobselinb74;        /* 0x024 */
370         u32 PAD[6];
371         u32 oobselinc30;        /* 0x040 */
372         u32 oobselinc74;        /* 0x044 */
373         u32 PAD[6];
374         u32 oobselind30;        /* 0x060 */
375         u32 oobselind74;        /* 0x064 */
376         u32 PAD[38];
377         u32 oobselouta30;       /* 0x100 */
378         u32 oobselouta74;       /* 0x104 */
379         u32 PAD[6];
380         u32 oobseloutb30;       /* 0x120 */
381         u32 oobseloutb74;       /* 0x124 */
382         u32 PAD[6];
383         u32 oobseloutc30;       /* 0x140 */
384         u32 oobseloutc74;       /* 0x144 */
385         u32 PAD[6];
386         u32 oobseloutd30;       /* 0x160 */
387         u32 oobseloutd74;       /* 0x164 */
388         u32 PAD[38];
389         u32 oobsynca;   /* 0x200 */
390         u32 oobseloutaen;       /* 0x204 */
391         u32 PAD[6];
392         u32 oobsyncb;   /* 0x220 */
393         u32 oobseloutben;       /* 0x224 */
394         u32 PAD[6];
395         u32 oobsyncc;   /* 0x240 */
396         u32 oobseloutcen;       /* 0x244 */
397         u32 PAD[6];
398         u32 oobsyncd;   /* 0x260 */
399         u32 oobseloutden;       /* 0x264 */
400         u32 PAD[38];
401         u32 oobaextwidth;       /* 0x300 */
402         u32 oobainwidth;        /* 0x304 */
403         u32 oobaoutwidth;       /* 0x308 */
404         u32 PAD[5];
405         u32 oobbextwidth;       /* 0x320 */
406         u32 oobbinwidth;        /* 0x324 */
407         u32 oobboutwidth;       /* 0x328 */
408         u32 PAD[5];
409         u32 oobcextwidth;       /* 0x340 */
410         u32 oobcinwidth;        /* 0x344 */
411         u32 oobcoutwidth;       /* 0x348 */
412         u32 PAD[5];
413         u32 oobdextwidth;       /* 0x360 */
414         u32 oobdinwidth;        /* 0x364 */
415         u32 oobdoutwidth;       /* 0x368 */
416         u32 PAD[37];
417         u32 ioctrlset;  /* 0x400 */
418         u32 ioctrlclear;        /* 0x404 */
419         u32 ioctrl;             /* 0x408 */
420         u32 PAD[61];
421         u32 iostatus;   /* 0x500 */
422         u32 PAD[127];
423         u32 ioctrlwidth;        /* 0x700 */
424         u32 iostatuswidth;      /* 0x704 */
425         u32 PAD[62];
426         u32 resetctrl;  /* 0x800 */
427         u32 resetstatus;        /* 0x804 */
428         u32 resetreadid;        /* 0x808 */
429         u32 resetwriteid;       /* 0x80c */
430         u32 PAD[60];
431         u32 errlogctrl; /* 0x900 */
432         u32 errlogdone; /* 0x904 */
433         u32 errlogstatus;       /* 0x908 */
434         u32 errlogaddrlo;       /* 0x90c */
435         u32 errlogaddrhi;       /* 0x910 */
436         u32 errlogid;   /* 0x914 */
437         u32 errloguser; /* 0x918 */
438         u32 errlogflags;        /* 0x91c */
439         u32 PAD[56];
440         u32 intstatus;  /* 0xa00 */
441         u32 PAD[127];
442         u32 config;             /* 0xe00 */
443         u32 PAD[63];
444         u32 itcr;               /* 0xf00 */
445         u32 PAD[3];
446         u32 itipooba;   /* 0xf10 */
447         u32 itipoobb;   /* 0xf14 */
448         u32 itipoobc;   /* 0xf18 */
449         u32 itipoobd;   /* 0xf1c */
450         u32 PAD[4];
451         u32 itipoobaout;        /* 0xf30 */
452         u32 itipoobbout;        /* 0xf34 */
453         u32 itipoobcout;        /* 0xf38 */
454         u32 itipoobdout;        /* 0xf3c */
455         u32 PAD[4];
456         u32 itopooba;   /* 0xf50 */
457         u32 itopoobb;   /* 0xf54 */
458         u32 itopoobc;   /* 0xf58 */
459         u32 itopoobd;   /* 0xf5c */
460         u32 PAD[4];
461         u32 itopoobain; /* 0xf70 */
462         u32 itopoobbin; /* 0xf74 */
463         u32 itopoobcin; /* 0xf78 */
464         u32 itopoobdin; /* 0xf7c */
465         u32 PAD[4];
466         u32 itopreset;  /* 0xf90 */
467         u32 PAD[15];
468         u32 peripherialid4;     /* 0xfd0 */
469         u32 peripherialid5;     /* 0xfd4 */
470         u32 peripherialid6;     /* 0xfd8 */
471         u32 peripherialid7;     /* 0xfdc */
472         u32 peripherialid0;     /* 0xfe0 */
473         u32 peripherialid1;     /* 0xfe4 */
474         u32 peripherialid2;     /* 0xfe8 */
475         u32 peripherialid3;     /* 0xfec */
476         u32 componentid0;       /* 0xff0 */
477         u32 componentid1;       /* 0xff4 */
478         u32 componentid2;       /* 0xff8 */
479         u32 componentid3;       /* 0xffc */
480 };
481
482 /* EROM parsing */
483
484 static u32
485 get_erom_ent(struct si_pub *sih, u32 __iomem **eromptr, u32 mask, u32 match)
486 {
487         u32 ent;
488         uint inv = 0, nom = 0;
489
490         while (true) {
491                 ent = R_REG(*eromptr);
492                 (*eromptr)++;
493
494                 if (mask == 0)
495                         break;
496
497                 if ((ent & ER_VALID) == 0) {
498                         inv++;
499                         continue;
500                 }
501
502                 if (ent == (ER_END | ER_VALID))
503                         break;
504
505                 if ((ent & mask) == match)
506                         break;
507
508                 nom++;
509         }
510
511         return ent;
512 }
513
514 static u32
515 get_asd(struct si_pub *sih, u32 __iomem **eromptr, uint sp, uint ad, uint st,
516         u32 *addrl, u32 *addrh, u32 *sizel, u32 *sizeh)
517 {
518         u32 asd, sz, szd;
519
520         asd = get_erom_ent(sih, eromptr, ER_VALID, ER_VALID);
521         if (((asd & ER_TAG1) != ER_ADD) ||
522             (((asd & AD_SP_MASK) >> AD_SP_SHIFT) != sp) ||
523             ((asd & AD_ST_MASK) != st)) {
524                 /* This is not what we want, "push" it back */
525                 (*eromptr)--;
526                 return 0;
527         }
528         *addrl = asd & AD_ADDR_MASK;
529         if (asd & AD_AG32)
530                 *addrh = get_erom_ent(sih, eromptr, 0, 0);
531         else
532                 *addrh = 0;
533         *sizeh = 0;
534         sz = asd & AD_SZ_MASK;
535         if (sz == AD_SZ_SZD) {
536                 szd = get_erom_ent(sih, eromptr, 0, 0);
537                 *sizel = szd & SD_SZ_MASK;
538                 if (szd & SD_SG32)
539                         *sizeh = get_erom_ent(sih, eromptr, 0, 0);
540         } else
541                 *sizel = AD_SZ_BASE << (sz >> AD_SZ_SHIFT);
542
543         return asd;
544 }
545
546 static void ai_hwfixup(struct si_info *sii)
547 {
548 }
549
550 /* parse the enumeration rom to identify all cores */
551 static void ai_scan(struct si_pub *sih, struct chipcregs __iomem *cc)
552 {
553         struct si_info *sii = (struct si_info *)sih;
554
555         u32 erombase;
556         u32 __iomem *eromptr, *eromlim;
557         void __iomem *regs = cc;
558
559         erombase = R_REG(&cc->eromptr);
560
561         /* Set wrappers address */
562         sii->curwrap = (void *)((unsigned long)cc + SI_CORE_SIZE);
563
564         /* Now point the window at the erom */
565         pci_write_config_dword(sii->pbus, PCI_BAR0_WIN, erombase);
566         eromptr = regs;
567         eromlim = eromptr + (ER_REMAPCONTROL / sizeof(u32));
568
569         while (eromptr < eromlim) {
570                 u32 cia, cib, cid, mfg, crev, nmw, nsw, nmp, nsp;
571                 u32 mpd, asd, addrl, addrh, sizel, sizeh;
572                 u32 __iomem *base;
573                 uint i, j, idx;
574                 bool br;
575
576                 br = false;
577
578                 /* Grok a component */
579                 cia = get_erom_ent(sih, &eromptr, ER_TAG, ER_CI);
580                 if (cia == (ER_END | ER_VALID)) {
581                         /*  Found END of erom */
582                         ai_hwfixup(sii);
583                         return;
584                 }
585                 base = eromptr - 1;
586                 cib = get_erom_ent(sih, &eromptr, 0, 0);
587
588                 if ((cib & ER_TAG) != ER_CI) {
589                         /* CIA not followed by CIB */
590                         goto error;
591                 }
592
593                 cid = (cia & CIA_CID_MASK) >> CIA_CID_SHIFT;
594                 mfg = (cia & CIA_MFG_MASK) >> CIA_MFG_SHIFT;
595                 crev = (cib & CIB_REV_MASK) >> CIB_REV_SHIFT;
596                 nmw = (cib & CIB_NMW_MASK) >> CIB_NMW_SHIFT;
597                 nsw = (cib & CIB_NSW_MASK) >> CIB_NSW_SHIFT;
598                 nmp = (cib & CIB_NMP_MASK) >> CIB_NMP_SHIFT;
599                 nsp = (cib & CIB_NSP_MASK) >> CIB_NSP_SHIFT;
600
601                 if (((mfg == MFGID_ARM) && (cid == DEF_AI_COMP)) || (nsp == 0))
602                         continue;
603                 if ((nmw + nsw == 0)) {
604                         /* A component which is not a core */
605                         if (cid == OOB_ROUTER_CORE_ID) {
606                                 asd = get_asd(sih, &eromptr, 0, 0, AD_ST_SLAVE,
607                                               &addrl, &addrh, &sizel, &sizeh);
608                                 if (asd != 0)
609                                         sii->oob_router = addrl;
610                         }
611                         continue;
612                 }
613
614                 idx = sii->numcores;
615 /*              sii->eromptr[idx] = base; */
616                 sii->cia[idx] = cia;
617                 sii->cib[idx] = cib;
618                 sii->coreid[idx] = cid;
619
620                 for (i = 0; i < nmp; i++) {
621                         mpd = get_erom_ent(sih, &eromptr, ER_VALID, ER_VALID);
622                         if ((mpd & ER_TAG) != ER_MP) {
623                                 /* Not enough MP entries for component */
624                                 goto error;
625                         }
626                 }
627
628                 /* First Slave Address Descriptor should be port 0:
629                  * the main register space for the core
630                  */
631                 asd =
632                     get_asd(sih, &eromptr, 0, 0, AD_ST_SLAVE, &addrl, &addrh,
633                             &sizel, &sizeh);
634                 if (asd == 0) {
635                         /* Try again to see if it is a bridge */
636                         asd =
637                             get_asd(sih, &eromptr, 0, 0, AD_ST_BRIDGE, &addrl,
638                                     &addrh, &sizel, &sizeh);
639                         if (asd != 0)
640                                 br = true;
641                         else if ((addrh != 0) || (sizeh != 0)
642                                  || (sizel != SI_CORE_SIZE)) {
643                                 /* First Slave ASD for core malformed */
644                                 goto error;
645                         }
646                 }
647                 sii->coresba[idx] = addrl;
648                 sii->coresba_size[idx] = sizel;
649                 /* Get any more ASDs in port 0 */
650                 j = 1;
651                 do {
652                         asd =
653                             get_asd(sih, &eromptr, 0, j, AD_ST_SLAVE, &addrl,
654                                     &addrh, &sizel, &sizeh);
655                         if ((asd != 0) && (j == 1) && (sizel == SI_CORE_SIZE)) {
656                                 sii->coresba2[idx] = addrl;
657                                 sii->coresba2_size[idx] = sizel;
658                         }
659                         j++;
660                 } while (asd != 0);
661
662                 /* Go through the ASDs for other slave ports */
663                 for (i = 1; i < nsp; i++) {
664                         j = 0;
665                         do {
666                                 asd =
667                                     get_asd(sih, &eromptr, i, j++, AD_ST_SLAVE,
668                                             &addrl, &addrh, &sizel, &sizeh);
669                         } while (asd != 0);
670                         if (j == 0) {
671                                 /* SP has no address descriptors */
672                                 goto error;
673                         }
674                 }
675
676                 /* Now get master wrappers */
677                 for (i = 0; i < nmw; i++) {
678                         asd =
679                             get_asd(sih, &eromptr, i, 0, AD_ST_MWRAP, &addrl,
680                                     &addrh, &sizel, &sizeh);
681                         if (asd == 0) {
682                                 /* Missing descriptor for MW */
683                                 goto error;
684                         }
685                         if ((sizeh != 0) || (sizel != SI_CORE_SIZE)) {
686                                 /* Master wrapper %d is not 4KB */
687                                 goto error;
688                         }
689                         if (i == 0)
690                                 sii->wrapba[idx] = addrl;
691                 }
692
693                 /* And finally slave wrappers */
694                 for (i = 0; i < nsw; i++) {
695                         uint fwp = (nsp == 1) ? 0 : 1;
696                         asd =
697                             get_asd(sih, &eromptr, fwp + i, 0, AD_ST_SWRAP,
698                                     &addrl, &addrh, &sizel, &sizeh);
699                         if (asd == 0) {
700                                 /* Missing descriptor for SW */
701                                 goto error;
702                         }
703                         if ((sizeh != 0) || (sizel != SI_CORE_SIZE)) {
704                                 /* Slave wrapper is not 4KB */
705                                 goto error;
706                         }
707                         if ((nmw == 0) && (i == 0))
708                                 sii->wrapba[idx] = addrl;
709                 }
710
711                 /* Don't record bridges */
712                 if (br)
713                         continue;
714
715                 /* Done with core */
716                 sii->numcores++;
717         }
718
719  error:
720         /* Reached end of erom without finding END */
721         sii->numcores = 0;
722         return;
723 }
724
725 /*
726  * This function changes the logical "focus" to the indicated core.
727  * Return the current core's virtual address. Since each core starts with the
728  * same set of registers (BIST, clock control, etc), the returned address
729  * contains the first register of this 'common' register block (not to be
730  * confused with 'common core').
731  */
732 void __iomem *ai_setcoreidx(struct si_pub *sih, uint coreidx)
733 {
734         struct si_info *sii = (struct si_info *)sih;
735         u32 addr = sii->coresba[coreidx];
736         u32 wrap = sii->wrapba[coreidx];
737
738         if (coreidx >= sii->numcores)
739                 return NULL;
740
741         /* point bar0 window */
742         pci_write_config_dword(sii->pbus, PCI_BAR0_WIN, addr);
743         /* point bar0 2nd 4KB window */
744         pci_write_config_dword(sii->pbus, PCI_BAR0_WIN2, wrap);
745         sii->curidx = coreidx;
746
747         return sii->curmap;
748 }
749
750 /* Return the number of address spaces in current core */
751 int ai_numaddrspaces(struct si_pub *sih)
752 {
753         return 2;
754 }
755
756 /* Return the address of the nth address space in the current core */
757 u32 ai_addrspace(struct si_pub *sih, uint asidx)
758 {
759         struct si_info *sii;
760         uint cidx;
761
762         sii = (struct si_info *)sih;
763         cidx = sii->curidx;
764
765         if (asidx == 0)
766                 return sii->coresba[cidx];
767         else if (asidx == 1)
768                 return sii->coresba2[cidx];
769         else {
770                 /* Need to parse the erom again to find addr space */
771                 return 0;
772         }
773 }
774
775 /* Return the size of the nth address space in the current core */
776 u32 ai_addrspacesize(struct si_pub *sih, uint asidx)
777 {
778         struct si_info *sii;
779         uint cidx;
780
781         sii = (struct si_info *)sih;
782         cidx = sii->curidx;
783
784         if (asidx == 0)
785                 return sii->coresba_size[cidx];
786         else if (asidx == 1)
787                 return sii->coresba2_size[cidx];
788         else {
789                 /* Need to parse the erom again to find addr */
790                 return 0;
791         }
792 }
793
794 uint ai_flag(struct si_pub *sih)
795 {
796         struct si_info *sii;
797         struct aidmp *ai;
798
799         sii = (struct si_info *)sih;
800         ai = sii->curwrap;
801
802         return R_REG(&ai->oobselouta30) & 0x1f;
803 }
804
805 void ai_setint(struct si_pub *sih, int siflag)
806 {
807 }
808
809 uint ai_corevendor(struct si_pub *sih)
810 {
811         struct si_info *sii;
812         u32 cia;
813
814         sii = (struct si_info *)sih;
815         cia = sii->cia[sii->curidx];
816         return (cia & CIA_MFG_MASK) >> CIA_MFG_SHIFT;
817 }
818
819 uint ai_corerev(struct si_pub *sih)
820 {
821         struct si_info *sii;
822         u32 cib;
823
824         sii = (struct si_info *)sih;
825         cib = sii->cib[sii->curidx];
826         return (cib & CIB_REV_MASK) >> CIB_REV_SHIFT;
827 }
828
829 bool ai_iscoreup(struct si_pub *sih)
830 {
831         struct si_info *sii;
832         struct aidmp *ai;
833
834         sii = (struct si_info *)sih;
835         ai = sii->curwrap;
836
837         return (((R_REG(&ai->ioctrl) & (SICF_FGC | SICF_CLOCK_EN)) ==
838                  SICF_CLOCK_EN)
839                 && ((R_REG(&ai->resetctrl) & AIRC_RESET) == 0));
840 }
841
842 void ai_core_cflags_wo(struct si_pub *sih, u32 mask, u32 val)
843 {
844         struct si_info *sii;
845         struct aidmp *ai;
846         u32 w;
847
848         sii = (struct si_info *)sih;
849
850         ai = sii->curwrap;
851
852         if (mask || val) {
853                 w = ((R_REG(&ai->ioctrl) & ~mask) | val);
854                 W_REG(&ai->ioctrl, w);
855         }
856 }
857
858 u32 ai_core_cflags(struct si_pub *sih, u32 mask, u32 val)
859 {
860         struct si_info *sii;
861         struct aidmp *ai;
862         u32 w;
863
864         sii = (struct si_info *)sih;
865         ai = sii->curwrap;
866
867         if (mask || val) {
868                 w = ((R_REG(&ai->ioctrl) & ~mask) | val);
869                 W_REG(&ai->ioctrl, w);
870         }
871
872         return R_REG(&ai->ioctrl);
873 }
874
875 /* return true if PCIE capability exists in the pci config space */
876 static bool ai_ispcie(struct si_info *sii)
877 {
878         u8 cap_ptr;
879
880         cap_ptr =
881             pcicore_find_pci_capability(sii->pbus, PCI_CAP_ID_EXP, NULL,
882                                         NULL);
883         if (!cap_ptr)
884                 return false;
885
886         return true;
887 }
888
889 static bool ai_buscore_prep(struct si_info *sii)
890 {
891         /* kludge to enable the clock on the 4306 which lacks a slowclock */
892         if (!ai_ispcie(sii))
893                 ai_clkctl_xtal(&sii->pub, XTAL | PLL, ON);
894         return true;
895 }
896
897 u32 ai_core_sflags(struct si_pub *sih, u32 mask, u32 val)
898 {
899         struct si_info *sii;
900         struct aidmp *ai;
901         u32 w;
902
903         sii = (struct si_info *)sih;
904         ai = sii->curwrap;
905
906         if (mask || val) {
907                 w = ((R_REG(&ai->iostatus) & ~mask) | val);
908                 W_REG(&ai->iostatus, w);
909         }
910
911         return R_REG(&ai->iostatus);
912 }
913
914 static bool
915 ai_buscore_setup(struct si_info *sii, u32 savewin, uint *origidx)
916 {
917         bool pci, pcie;
918         uint i;
919         uint pciidx, pcieidx, pcirev, pcierev;
920         struct chipcregs __iomem *cc;
921
922         cc = ai_setcoreidx(&sii->pub, SI_CC_IDX);
923
924         /* get chipcommon rev */
925         sii->pub.ccrev = (int)ai_corerev(&sii->pub);
926
927         /* get chipcommon chipstatus */
928         if (sii->pub.ccrev >= 11)
929                 sii->pub.chipst = R_REG(&cc->chipstatus);
930
931         /* get chipcommon capabilites */
932         sii->pub.cccaps = R_REG(&cc->capabilities);
933         /* get chipcommon extended capabilities */
934
935         if (sii->pub.ccrev >= 35)
936                 sii->pub.cccaps_ext = R_REG(&cc->capabilities_ext);
937
938         /* get pmu rev and caps */
939         if (sii->pub.cccaps & CC_CAP_PMU) {
940                 sii->pub.pmucaps = R_REG(&cc->pmucapabilities);
941                 sii->pub.pmurev = sii->pub.pmucaps & PCAP_REV_MASK;
942         }
943
944         /* figure out bus/orignal core idx */
945         sii->pub.buscoretype = NODEV_CORE_ID;
946         sii->pub.buscorerev = NOREV;
947         sii->pub.buscoreidx = BADIDX;
948
949         pci = pcie = false;
950         pcirev = pcierev = NOREV;
951         pciidx = pcieidx = BADIDX;
952
953         for (i = 0; i < sii->numcores; i++) {
954                 uint cid, crev;
955
956                 ai_setcoreidx(&sii->pub, i);
957                 cid = ai_coreid(&sii->pub);
958                 crev = ai_corerev(&sii->pub);
959
960                 if (cid == PCI_CORE_ID) {
961                         pciidx = i;
962                         pcirev = crev;
963                         pci = true;
964                 } else if (cid == PCIE_CORE_ID) {
965                         pcieidx = i;
966                         pcierev = crev;
967                         pcie = true;
968                 }
969
970                 /* find the core idx before entering this func. */
971                 if ((savewin && (savewin == sii->coresba[i])) ||
972                     (cc == sii->regs[i]))
973                         *origidx = i;
974         }
975
976         if (pci && pcie) {
977                 if (ai_ispcie(sii))
978                         pci = false;
979                 else
980                         pcie = false;
981         }
982         if (pci) {
983                 sii->pub.buscoretype = PCI_CORE_ID;
984                 sii->pub.buscorerev = pcirev;
985                 sii->pub.buscoreidx = pciidx;
986         } else if (pcie) {
987                 sii->pub.buscoretype = PCIE_CORE_ID;
988                 sii->pub.buscorerev = pcierev;
989                 sii->pub.buscoreidx = pcieidx;
990         }
991
992         /* fixup necessary chip/core configurations */
993         if (SI_FAST(sii)) {
994                 if (!sii->pch) {
995                         sii->pch = pcicore_init(&sii->pub, sii->pbus,
996                                                 (__iomem void *)PCIEREGS(sii));
997                         if (sii->pch == NULL)
998                                 return false;
999                 }
1000         }
1001         if (ai_pci_fixcfg(&sii->pub)) {
1002                 /* si_doattach: si_pci_fixcfg failed */
1003                 return false;
1004         }
1005
1006         /* return to the original core */
1007         ai_setcoreidx(&sii->pub, *origidx);
1008
1009         return true;
1010 }
1011
1012 /*
1013  * get boardtype and boardrev
1014  */
1015 static __used void ai_nvram_process(struct si_info *sii)
1016 {
1017         uint w = 0;
1018
1019         /* do a pci config read to get subsystem id and subvendor id */
1020         pci_read_config_dword(sii->pbus, PCI_SUBSYSTEM_VENDOR_ID, &w);
1021
1022         sii->pub.boardvendor = w & 0xffff;
1023         sii->pub.boardtype = (w >> 16) & 0xffff;
1024         sii->pub.boardflags = getintvar(&sii->pub, BRCMS_SROM_BOARDFLAGS);
1025 }
1026
1027 static struct si_info *ai_doattach(struct si_info *sii,
1028                                    void __iomem *regs, struct pci_dev *pbus)
1029 {
1030         struct si_pub *sih = &sii->pub;
1031         u32 w, savewin;
1032         struct chipcregs __iomem *cc;
1033         uint socitype;
1034         uint origidx;
1035
1036         memset((unsigned char *) sii, 0, sizeof(struct si_info));
1037
1038         savewin = 0;
1039
1040         sih->buscoreidx = BADIDX;
1041
1042         sii->curmap = regs;
1043         sii->pbus = pbus;
1044
1045         /* find Chipcommon address */
1046         pci_read_config_dword(sii->pbus, PCI_BAR0_WIN, &savewin);
1047         if (!GOODCOREADDR(savewin, SI_ENUM_BASE))
1048                 savewin = SI_ENUM_BASE;
1049
1050         pci_write_config_dword(sii->pbus, PCI_BAR0_WIN,
1051                                SI_ENUM_BASE);
1052         cc = (struct chipcregs __iomem *) regs;
1053
1054         /* bus/core/clk setup for register access */
1055         if (!ai_buscore_prep(sii))
1056                 return NULL;
1057
1058         /*
1059          * ChipID recognition.
1060          *   We assume we can read chipid at offset 0 from the regs arg.
1061          *   If we add other chiptypes (or if we need to support old sdio
1062          *   hosts w/o chipcommon), some way of recognizing them needs to
1063          *   be added here.
1064          */
1065         w = R_REG(&cc->chipid);
1066         socitype = (w & CID_TYPE_MASK) >> CID_TYPE_SHIFT;
1067         /* Might as wll fill in chip id rev & pkg */
1068         sih->chip = w & CID_ID_MASK;
1069         sih->chiprev = (w & CID_REV_MASK) >> CID_REV_SHIFT;
1070         sih->chippkg = (w & CID_PKG_MASK) >> CID_PKG_SHIFT;
1071
1072         sih->issim = false;
1073
1074         /* scan for cores */
1075         if (socitype == SOCI_AI) {
1076                 SI_MSG(("Found chip type AI (0x%08x)\n", w));
1077                 /* pass chipc address instead of original core base */
1078                 ai_scan(&sii->pub, cc);
1079         } else {
1080                 /* Found chip of unknown type */
1081                 return NULL;
1082         }
1083         /* no cores found, bail out */
1084         if (sii->numcores == 0)
1085                 return NULL;
1086
1087         /* bus/core/clk setup */
1088         origidx = SI_CC_IDX;
1089         if (!ai_buscore_setup(sii, savewin, &origidx))
1090                 goto exit;
1091
1092         /* Init nvram from sprom/otp if they exist */
1093         if (srom_var_init(&sii->pub, cc))
1094                 goto exit;
1095
1096         ai_nvram_process(sii);
1097
1098         /* === NVRAM, clock is ready === */
1099         cc = (struct chipcregs __iomem *) ai_setcore(sih, CC_CORE_ID, 0);
1100         W_REG(&cc->gpiopullup, 0);
1101         W_REG(&cc->gpiopulldown, 0);
1102         ai_setcoreidx(sih, origidx);
1103
1104         /* PMU specific initializations */
1105         if (sih->cccaps & CC_CAP_PMU) {
1106                 u32 xtalfreq;
1107                 si_pmu_init(sih);
1108                 si_pmu_chip_init(sih);
1109
1110                 xtalfreq = si_pmu_measure_alpclk(sih);
1111                 si_pmu_pll_init(sih, xtalfreq);
1112                 si_pmu_res_init(sih);
1113                 si_pmu_swreg_init(sih);
1114         }
1115
1116         /* setup the GPIO based LED powersave register */
1117         w = getintvar(sih, BRCMS_SROM_LEDDC);
1118         if (w == 0)
1119                 w = DEFAULT_GPIOTIMERVAL;
1120         ai_corereg(sih, SI_CC_IDX, offsetof(struct chipcregs, gpiotimerval),
1121                    ~0, w);
1122
1123         if (PCIE(sii))
1124                 pcicore_attach(sii->pch, SI_DOATTACH);
1125
1126         if (sih->chip == BCM43224_CHIP_ID) {
1127                 /*
1128                  * enable 12 mA drive strenth for 43224 and
1129                  * set chipControl register bit 15
1130                  */
1131                 if (sih->chiprev == 0) {
1132                         SI_MSG(("Applying 43224A0 WARs\n"));
1133                         ai_corereg(sih, SI_CC_IDX,
1134                                    offsetof(struct chipcregs, chipcontrol),
1135                                    CCTRL43224_GPIO_TOGGLE,
1136                                    CCTRL43224_GPIO_TOGGLE);
1137                         si_pmu_chipcontrol(sih, 0, CCTRL_43224A0_12MA_LED_DRIVE,
1138                                            CCTRL_43224A0_12MA_LED_DRIVE);
1139                 }
1140                 if (sih->chiprev >= 1) {
1141                         SI_MSG(("Applying 43224B0+ WARs\n"));
1142                         si_pmu_chipcontrol(sih, 0, CCTRL_43224B0_12MA_LED_DRIVE,
1143                                            CCTRL_43224B0_12MA_LED_DRIVE);
1144                 }
1145         }
1146
1147         if (sih->chip == BCM4313_CHIP_ID) {
1148                 /*
1149                  * enable 12 mA drive strenth for 4313 and
1150                  * set chipControl register bit 1
1151                  */
1152                 SI_MSG(("Applying 4313 WARs\n"));
1153                 si_pmu_chipcontrol(sih, 0, CCTRL_4313_12MA_LED_DRIVE,
1154                                    CCTRL_4313_12MA_LED_DRIVE);
1155         }
1156
1157         return sii;
1158
1159  exit:
1160         if (sii->pch)
1161                 pcicore_deinit(sii->pch);
1162         sii->pch = NULL;
1163
1164         return NULL;
1165 }
1166
1167 /*
1168  * Allocate a si handle.
1169  * devid - pci device id (used to determine chip#)
1170  * osh - opaque OS handle
1171  * regs - virtual address of initial core registers
1172  */
1173 struct si_pub *
1174 ai_attach(void __iomem *regs, struct pci_dev *sdh)
1175 {
1176         struct si_info *sii;
1177
1178         /* alloc struct si_info */
1179         sii = kmalloc(sizeof(struct si_info), GFP_ATOMIC);
1180         if (sii == NULL)
1181                 return NULL;
1182
1183         if (ai_doattach(sii, regs, sdh) == NULL) {
1184                 kfree(sii);
1185                 return NULL;
1186         }
1187
1188         return (struct si_pub *) sii;
1189 }
1190
1191 /* may be called with core in reset */
1192 void ai_detach(struct si_pub *sih)
1193 {
1194         struct si_info *sii;
1195
1196         sii = (struct si_info *)sih;
1197
1198         if (sii == NULL)
1199                 return;
1200
1201         if (sii->pch)
1202                 pcicore_deinit(sii->pch);
1203         sii->pch = NULL;
1204
1205         srom_free_vars(sih);
1206         kfree(sii);
1207 }
1208
1209 /* register driver interrupt disabling and restoring callback functions */
1210 void
1211 ai_register_intr_callback(struct si_pub *sih, void *intrsoff_fn,
1212                           void *intrsrestore_fn,
1213                           void *intrsenabled_fn, void *intr_arg)
1214 {
1215         struct si_info *sii;
1216
1217         sii = (struct si_info *)sih;
1218         sii->intr_arg = intr_arg;
1219         sii->intrsoff_fn = (u32 (*)(void *)) intrsoff_fn;
1220         sii->intrsrestore_fn = (void (*) (void *, u32)) intrsrestore_fn;
1221         sii->intrsenabled_fn = (bool (*)(void *)) intrsenabled_fn;
1222         /* save current core id.  when this function called, the current core
1223          * must be the core which provides driver functions(il, et, wl, etc.)
1224          */
1225         sii->dev_coreid = sii->coreid[sii->curidx];
1226 }
1227
1228 void ai_deregister_intr_callback(struct si_pub *sih)
1229 {
1230         struct si_info *sii;
1231
1232         sii = (struct si_info *)sih;
1233         sii->intrsoff_fn = NULL;
1234 }
1235
1236 uint ai_coreid(struct si_pub *sih)
1237 {
1238         struct si_info *sii;
1239
1240         sii = (struct si_info *)sih;
1241         return sii->coreid[sii->curidx];
1242 }
1243
1244 uint ai_coreidx(struct si_pub *sih)
1245 {
1246         struct si_info *sii;
1247
1248         sii = (struct si_info *)sih;
1249         return sii->curidx;
1250 }
1251
1252 bool ai_backplane64(struct si_pub *sih)
1253 {
1254         return (sih->cccaps & CC_CAP_BKPLN64) != 0;
1255 }
1256
1257 /* return index of coreid or BADIDX if not found */
1258 uint ai_findcoreidx(struct si_pub *sih, uint coreid, uint coreunit)
1259 {
1260         struct si_info *sii;
1261         uint found;
1262         uint i;
1263
1264         sii = (struct si_info *)sih;
1265
1266         found = 0;
1267
1268         for (i = 0; i < sii->numcores; i++)
1269                 if (sii->coreid[i] == coreid) {
1270                         if (found == coreunit)
1271                                 return i;
1272                         found++;
1273                 }
1274
1275         return BADIDX;
1276 }
1277
1278 /*
1279  * This function changes logical "focus" to the indicated core;
1280  * must be called with interrupts off.
1281  * Moreover, callers should keep interrupts off during switching
1282  * out of and back to d11 core.
1283  */
1284 void __iomem *ai_setcore(struct si_pub *sih, uint coreid, uint coreunit)
1285 {
1286         uint idx;
1287
1288         idx = ai_findcoreidx(sih, coreid, coreunit);
1289         if (idx >= SI_MAXCORES)
1290                 return NULL;
1291
1292         return ai_setcoreidx(sih, idx);
1293 }
1294
1295 /* Turn off interrupt as required by ai_setcore, before switch core */
1296 void __iomem *ai_switch_core(struct si_pub *sih, uint coreid, uint *origidx,
1297                              uint *intr_val)
1298 {
1299         void __iomem *cc;
1300         struct si_info *sii;
1301
1302         sii = (struct si_info *)sih;
1303
1304         if (SI_FAST(sii)) {
1305                 /* Overloading the origidx variable to remember the coreid,
1306                  * this works because the core ids cannot be confused with
1307                  * core indices.
1308                  */
1309                 *origidx = coreid;
1310                 if (coreid == CC_CORE_ID)
1311                         return CCREGS_FAST(sii);
1312                 else if (coreid == sih->buscoretype)
1313                         return PCIEREGS(sii);
1314         }
1315         INTR_OFF(sii, *intr_val);
1316         *origidx = sii->curidx;
1317         cc = ai_setcore(sih, coreid, 0);
1318         return cc;
1319 }
1320
1321 /* restore coreidx and restore interrupt */
1322 void ai_restore_core(struct si_pub *sih, uint coreid, uint intr_val)
1323 {
1324         struct si_info *sii;
1325
1326         sii = (struct si_info *)sih;
1327         if (SI_FAST(sii)
1328             && ((coreid == CC_CORE_ID) || (coreid == sih->buscoretype)))
1329                 return;
1330
1331         ai_setcoreidx(sih, coreid);
1332         INTR_RESTORE(sii, intr_val);
1333 }
1334
1335 void ai_write_wrapperreg(struct si_pub *sih, u32 offset, u32 val)
1336 {
1337         struct si_info *sii = (struct si_info *)sih;
1338         u32 *w = (u32 *) sii->curwrap;
1339         W_REG(w + (offset / 4), val);
1340         return;
1341 }
1342
1343 /*
1344  * Switch to 'coreidx', issue a single arbitrary 32bit register mask&set
1345  * operation, switch back to the original core, and return the new value.
1346  *
1347  * When using the silicon backplane, no fiddling with interrupts or core
1348  * switches is needed.
1349  *
1350  * Also, when using pci/pcie, we can optimize away the core switching for pci
1351  * registers and (on newer pci cores) chipcommon registers.
1352  */
1353 uint ai_corereg(struct si_pub *sih, uint coreidx, uint regoff, uint mask,
1354                 uint val)
1355 {
1356         uint origidx = 0;
1357         u32 __iomem *r = NULL;
1358         uint w;
1359         uint intr_val = 0;
1360         bool fast = false;
1361         struct si_info *sii;
1362
1363         sii = (struct si_info *)sih;
1364
1365         if (coreidx >= SI_MAXCORES)
1366                 return 0;
1367
1368         /*
1369          * If pci/pcie, we can get at pci/pcie regs
1370          * and on newer cores to chipc
1371          */
1372         if ((sii->coreid[coreidx] == CC_CORE_ID) && SI_FAST(sii)) {
1373                 /* Chipc registers are mapped at 12KB */
1374                 fast = true;
1375                 r = (u32 __iomem *)((__iomem char *)sii->curmap +
1376                                     PCI_16KB0_CCREGS_OFFSET + regoff);
1377         } else if (sii->pub.buscoreidx == coreidx) {
1378                 /*
1379                  * pci registers are at either in the last 2KB of
1380                  * an 8KB window or, in pcie and pci rev 13 at 8KB
1381                  */
1382                 fast = true;
1383                 if (SI_FAST(sii))
1384                         r = (u32 __iomem *)((__iomem char *)sii->curmap +
1385                                     PCI_16KB0_PCIREGS_OFFSET + regoff);
1386                 else
1387                         r = (u32 __iomem *)((__iomem char *)sii->curmap +
1388                                     ((regoff >= SBCONFIGOFF) ?
1389                                       PCI_BAR0_PCISBR_OFFSET :
1390                                       PCI_BAR0_PCIREGS_OFFSET) + regoff);
1391         }
1392
1393         if (!fast) {
1394                 INTR_OFF(sii, intr_val);
1395
1396                 /* save current core index */
1397                 origidx = ai_coreidx(&sii->pub);
1398
1399                 /* switch core */
1400                 r = (u32 __iomem *) ((unsigned char __iomem *)
1401                         ai_setcoreidx(&sii->pub, coreidx) + regoff);
1402         }
1403
1404         /* mask and set */
1405         if (mask || val) {
1406                 w = (R_REG(r) & ~mask) | val;
1407                 W_REG(r, w);
1408         }
1409
1410         /* readback */
1411         w = R_REG(r);
1412
1413         if (!fast) {
1414                 /* restore core index */
1415                 if (origidx != coreidx)
1416                         ai_setcoreidx(&sii->pub, origidx);
1417
1418                 INTR_RESTORE(sii, intr_val);
1419         }
1420
1421         return w;
1422 }
1423
1424 void ai_core_disable(struct si_pub *sih, u32 bits)
1425 {
1426         struct si_info *sii;
1427         u32 dummy;
1428         struct aidmp *ai;
1429
1430         sii = (struct si_info *)sih;
1431
1432         ai = sii->curwrap;
1433
1434         /* if core is already in reset, just return */
1435         if (R_REG(&ai->resetctrl) & AIRC_RESET)
1436                 return;
1437
1438         W_REG(&ai->ioctrl, bits);
1439         dummy = R_REG(&ai->ioctrl);
1440         udelay(10);
1441
1442         W_REG(&ai->resetctrl, AIRC_RESET);
1443         udelay(1);
1444 }
1445
1446 /* reset and re-enable a core
1447  * inputs:
1448  * bits - core specific bits that are set during and after reset sequence
1449  * resetbits - core specific bits that are set only during reset sequence
1450  */
1451 void ai_core_reset(struct si_pub *sih, u32 bits, u32 resetbits)
1452 {
1453         struct si_info *sii;
1454         struct aidmp *ai;
1455         u32 dummy;
1456
1457         sii = (struct si_info *)sih;
1458         ai = sii->curwrap;
1459
1460         /*
1461          * Must do the disable sequence first to work
1462          * for arbitrary current core state.
1463          */
1464         ai_core_disable(sih, (bits | resetbits));
1465
1466         /*
1467          * Now do the initialization sequence.
1468          */
1469         W_REG(&ai->ioctrl, (bits | SICF_FGC | SICF_CLOCK_EN));
1470         dummy = R_REG(&ai->ioctrl);
1471         W_REG(&ai->resetctrl, 0);
1472         udelay(1);
1473
1474         W_REG(&ai->ioctrl, (bits | SICF_CLOCK_EN));
1475         dummy = R_REG(&ai->ioctrl);
1476         udelay(1);
1477 }
1478
1479 /* return the slow clock source - LPO, XTAL, or PCI */
1480 static uint ai_slowclk_src(struct si_info *sii)
1481 {
1482         struct chipcregs __iomem *cc;
1483         u32 val;
1484
1485         if (sii->pub.ccrev < 6) {
1486                 pci_read_config_dword(sii->pbus, PCI_GPIO_OUT,
1487                                       &val);
1488                 if (val & PCI_CFG_GPIO_SCS)
1489                         return SCC_SS_PCI;
1490                 return SCC_SS_XTAL;
1491         } else if (sii->pub.ccrev < 10) {
1492                 cc = (struct chipcregs __iomem *)
1493                         ai_setcoreidx(&sii->pub, sii->curidx);
1494                 return R_REG(&cc->slow_clk_ctl) & SCC_SS_MASK;
1495         } else                  /* Insta-clock */
1496                 return SCC_SS_XTAL;
1497 }
1498
1499 /*
1500 * return the ILP (slowclock) min or max frequency
1501 * precondition: we've established the chip has dynamic clk control
1502 */
1503 static uint ai_slowclk_freq(struct si_info *sii, bool max_freq,
1504                             struct chipcregs __iomem *cc)
1505 {
1506         u32 slowclk;
1507         uint div;
1508
1509         slowclk = ai_slowclk_src(sii);
1510         if (sii->pub.ccrev < 6) {
1511                 if (slowclk == SCC_SS_PCI)
1512                         return max_freq ? (PCIMAXFREQ / 64)
1513                                 : (PCIMINFREQ / 64);
1514                 else
1515                         return max_freq ? (XTALMAXFREQ / 32)
1516                                 : (XTALMINFREQ / 32);
1517         } else if (sii->pub.ccrev < 10) {
1518                 div = 4 *
1519                     (((R_REG(&cc->slow_clk_ctl) & SCC_CD_MASK) >>
1520                       SCC_CD_SHIFT) + 1);
1521                 if (slowclk == SCC_SS_LPO)
1522                         return max_freq ? LPOMAXFREQ : LPOMINFREQ;
1523                 else if (slowclk == SCC_SS_XTAL)
1524                         return max_freq ? (XTALMAXFREQ / div)
1525                                 : (XTALMINFREQ / div);
1526                 else if (slowclk == SCC_SS_PCI)
1527                         return max_freq ? (PCIMAXFREQ / div)
1528                                 : (PCIMINFREQ / div);
1529         } else {
1530                 /* Chipc rev 10 is InstaClock */
1531                 div = R_REG(&cc->system_clk_ctl) >> SYCC_CD_SHIFT;
1532                 div = 4 * (div + 1);
1533                 return max_freq ? XTALMAXFREQ : (XTALMINFREQ / div);
1534         }
1535         return 0;
1536 }
1537
1538 static void
1539 ai_clkctl_setdelay(struct si_info *sii, struct chipcregs __iomem *cc)
1540 {
1541         uint slowmaxfreq, pll_delay, slowclk;
1542         uint pll_on_delay, fref_sel_delay;
1543
1544         pll_delay = PLL_DELAY;
1545
1546         /*
1547          * If the slow clock is not sourced by the xtal then
1548          * add the xtal_on_delay since the xtal will also be
1549          * powered down by dynamic clk control logic.
1550          */
1551
1552         slowclk = ai_slowclk_src(sii);
1553         if (slowclk != SCC_SS_XTAL)
1554                 pll_delay += XTAL_ON_DELAY;
1555
1556         /* Starting with 4318 it is ILP that is used for the delays */
1557         slowmaxfreq =
1558             ai_slowclk_freq(sii, (sii->pub.ccrev >= 10) ? false : true, cc);
1559
1560         pll_on_delay = ((slowmaxfreq * pll_delay) + 999999) / 1000000;
1561         fref_sel_delay = ((slowmaxfreq * FREF_DELAY) + 999999) / 1000000;
1562
1563         W_REG(&cc->pll_on_delay, pll_on_delay);
1564         W_REG(&cc->fref_sel_delay, fref_sel_delay);
1565 }
1566
1567 /* initialize power control delay registers */
1568 void ai_clkctl_init(struct si_pub *sih)
1569 {
1570         struct si_info *sii;
1571         uint origidx = 0;
1572         struct chipcregs __iomem *cc;
1573         bool fast;
1574
1575         if (!(sih->cccaps & CC_CAP_PWR_CTL))
1576                 return;
1577
1578         sii = (struct si_info *)sih;
1579         fast = SI_FAST(sii);
1580         if (!fast) {
1581                 origidx = sii->curidx;
1582                 cc = (struct chipcregs __iomem *)
1583                         ai_setcore(sih, CC_CORE_ID, 0);
1584                 if (cc == NULL)
1585                         return;
1586         } else {
1587                 cc = (struct chipcregs __iomem *) CCREGS_FAST(sii);
1588                 if (cc == NULL)
1589                         return;
1590         }
1591
1592         /* set all Instaclk chip ILP to 1 MHz */
1593         if (sih->ccrev >= 10)
1594                 SET_REG(&cc->system_clk_ctl, SYCC_CD_MASK,
1595                         (ILP_DIV_1MHZ << SYCC_CD_SHIFT));
1596
1597         ai_clkctl_setdelay(sii, cc);
1598
1599         if (!fast)
1600                 ai_setcoreidx(sih, origidx);
1601 }
1602
1603 /*
1604  * return the value suitable for writing to the
1605  * dot11 core FAST_PWRUP_DELAY register
1606  */
1607 u16 ai_clkctl_fast_pwrup_delay(struct si_pub *sih)
1608 {
1609         struct si_info *sii;
1610         uint origidx = 0;
1611         struct chipcregs __iomem *cc;
1612         uint slowminfreq;
1613         u16 fpdelay;
1614         uint intr_val = 0;
1615         bool fast;
1616
1617         sii = (struct si_info *)sih;
1618         if (sih->cccaps & CC_CAP_PMU) {
1619                 INTR_OFF(sii, intr_val);
1620                 fpdelay = si_pmu_fast_pwrup_delay(sih);
1621                 INTR_RESTORE(sii, intr_val);
1622                 return fpdelay;
1623         }
1624
1625         if (!(sih->cccaps & CC_CAP_PWR_CTL))
1626                 return 0;
1627
1628         fast = SI_FAST(sii);
1629         fpdelay = 0;
1630         if (!fast) {
1631                 origidx = sii->curidx;
1632                 INTR_OFF(sii, intr_val);
1633                 cc = (struct chipcregs __iomem *)
1634                         ai_setcore(sih, CC_CORE_ID, 0);
1635                 if (cc == NULL)
1636                         goto done;
1637         } else {
1638                 cc = (struct chipcregs __iomem *) CCREGS_FAST(sii);
1639                 if (cc == NULL)
1640                         goto done;
1641         }
1642
1643         slowminfreq = ai_slowclk_freq(sii, false, cc);
1644         fpdelay = (((R_REG(&cc->pll_on_delay) + 2) * 1000000) +
1645                    (slowminfreq - 1)) / slowminfreq;
1646
1647  done:
1648         if (!fast) {
1649                 ai_setcoreidx(sih, origidx);
1650                 INTR_RESTORE(sii, intr_val);
1651         }
1652         return fpdelay;
1653 }
1654
1655 /* turn primary xtal and/or pll off/on */
1656 int ai_clkctl_xtal(struct si_pub *sih, uint what, bool on)
1657 {
1658         struct si_info *sii;
1659         u32 in, out, outen;
1660
1661         sii = (struct si_info *)sih;
1662
1663         /* pcie core doesn't have any mapping to control the xtal pu */
1664         if (PCIE(sii))
1665                 return -1;
1666
1667         pci_read_config_dword(sii->pbus, PCI_GPIO_IN, &in);
1668         pci_read_config_dword(sii->pbus, PCI_GPIO_OUT, &out);
1669         pci_read_config_dword(sii->pbus, PCI_GPIO_OUTEN, &outen);
1670
1671         /*
1672          * Avoid glitching the clock if GPRS is already using it.
1673          * We can't actually read the state of the PLLPD so we infer it
1674          * by the value of XTAL_PU which *is* readable via gpioin.
1675          */
1676         if (on && (in & PCI_CFG_GPIO_XTAL))
1677                 return 0;
1678
1679         if (what & XTAL)
1680                 outen |= PCI_CFG_GPIO_XTAL;
1681         if (what & PLL)
1682                 outen |= PCI_CFG_GPIO_PLL;
1683
1684         if (on) {
1685                 /* turn primary xtal on */
1686                 if (what & XTAL) {
1687                         out |= PCI_CFG_GPIO_XTAL;
1688                         if (what & PLL)
1689                                 out |= PCI_CFG_GPIO_PLL;
1690                         pci_write_config_dword(sii->pbus,
1691                                                PCI_GPIO_OUT, out);
1692                         pci_write_config_dword(sii->pbus,
1693                                                PCI_GPIO_OUTEN, outen);
1694                         udelay(XTAL_ON_DELAY);
1695                 }
1696
1697                 /* turn pll on */
1698                 if (what & PLL) {
1699                         out &= ~PCI_CFG_GPIO_PLL;
1700                         pci_write_config_dword(sii->pbus,
1701                                                PCI_GPIO_OUT, out);
1702                         mdelay(2);
1703                 }
1704         } else {
1705                 if (what & XTAL)
1706                         out &= ~PCI_CFG_GPIO_XTAL;
1707                 if (what & PLL)
1708                         out |= PCI_CFG_GPIO_PLL;
1709                 pci_write_config_dword(sii->pbus,
1710                                        PCI_GPIO_OUT, out);
1711                 pci_write_config_dword(sii->pbus,
1712                                        PCI_GPIO_OUTEN, outen);
1713         }
1714
1715         return 0;
1716 }
1717
1718 /* clk control mechanism through chipcommon, no policy checking */
1719 static bool _ai_clkctl_cc(struct si_info *sii, uint mode)
1720 {
1721         uint origidx = 0;
1722         struct chipcregs __iomem *cc;
1723         u32 scc;
1724         uint intr_val = 0;
1725         bool fast = SI_FAST(sii);
1726
1727         /* chipcommon cores prior to rev6 don't support dynamic clock control */
1728         if (sii->pub.ccrev < 6)
1729                 return false;
1730
1731         if (!fast) {
1732                 INTR_OFF(sii, intr_val);
1733                 origidx = sii->curidx;
1734                 cc = (struct chipcregs __iomem *)
1735                                         ai_setcore(&sii->pub, CC_CORE_ID, 0);
1736         } else {
1737                 cc = (struct chipcregs __iomem *) CCREGS_FAST(sii);
1738                 if (cc == NULL)
1739                         goto done;
1740         }
1741
1742         if (!(sii->pub.cccaps & CC_CAP_PWR_CTL) && (sii->pub.ccrev < 20))
1743                 goto done;
1744
1745         switch (mode) {
1746         case CLK_FAST:          /* FORCEHT, fast (pll) clock */
1747                 if (sii->pub.ccrev < 10) {
1748                         /*
1749                          * don't forget to force xtal back
1750                          * on before we clear SCC_DYN_XTAL..
1751                          */
1752                         ai_clkctl_xtal(&sii->pub, XTAL, ON);
1753                         SET_REG(&cc->slow_clk_ctl,
1754                                 (SCC_XC | SCC_FS | SCC_IP), SCC_IP);
1755                 } else if (sii->pub.ccrev < 20) {
1756                         OR_REG(&cc->system_clk_ctl, SYCC_HR);
1757                 } else {
1758                         OR_REG(&cc->clk_ctl_st, CCS_FORCEHT);
1759                 }
1760
1761                 /* wait for the PLL */
1762                 if (sii->pub.cccaps & CC_CAP_PMU) {
1763                         u32 htavail = CCS_HTAVAIL;
1764                         SPINWAIT(((R_REG(&cc->clk_ctl_st) & htavail)
1765                                   == 0), PMU_MAX_TRANSITION_DLY);
1766                 } else {
1767                         udelay(PLL_DELAY);
1768                 }
1769                 break;
1770
1771         case CLK_DYNAMIC:       /* enable dynamic clock control */
1772                 if (sii->pub.ccrev < 10) {
1773                         scc = R_REG(&cc->slow_clk_ctl);
1774                         scc &= ~(SCC_FS | SCC_IP | SCC_XC);
1775                         if ((scc & SCC_SS_MASK) != SCC_SS_XTAL)
1776                                 scc |= SCC_XC;
1777                         W_REG(&cc->slow_clk_ctl, scc);
1778
1779                         /*
1780                          * for dynamic control, we have to
1781                          * release our xtal_pu "force on"
1782                          */
1783                         if (scc & SCC_XC)
1784                                 ai_clkctl_xtal(&sii->pub, XTAL, OFF);
1785                 } else if (sii->pub.ccrev < 20) {
1786                         /* Instaclock */
1787                         AND_REG(&cc->system_clk_ctl, ~SYCC_HR);
1788                 } else {
1789                         AND_REG(&cc->clk_ctl_st, ~CCS_FORCEHT);
1790                 }
1791                 break;
1792
1793         default:
1794                 break;
1795         }
1796
1797  done:
1798         if (!fast) {
1799                 ai_setcoreidx(&sii->pub, origidx);
1800                 INTR_RESTORE(sii, intr_val);
1801         }
1802         return mode == CLK_FAST;
1803 }
1804
1805 /*
1806  *  clock control policy function throught chipcommon
1807  *
1808  *    set dynamic clk control mode (forceslow, forcefast, dynamic)
1809  *    returns true if we are forcing fast clock
1810  *    this is a wrapper over the next internal function
1811  *      to allow flexible policy settings for outside caller
1812  */
1813 bool ai_clkctl_cc(struct si_pub *sih, uint mode)
1814 {
1815         struct si_info *sii;
1816
1817         sii = (struct si_info *)sih;
1818
1819         /* chipcommon cores prior to rev6 don't support dynamic clock control */
1820         if (sih->ccrev < 6)
1821                 return false;
1822
1823         if (PCI_FORCEHT(sii))
1824                 return mode == CLK_FAST;
1825
1826         return _ai_clkctl_cc(sii, mode);
1827 }
1828
1829 /* Build device path */
1830 int ai_devpath(struct si_pub *sih, char *path, int size)
1831 {
1832         int slen;
1833
1834         if (!path || size <= 0)
1835                 return -1;
1836
1837         slen = snprintf(path, (size_t) size, "pci/%u/%u/",
1838                 ((struct si_info *)sih)->pbus->bus->number,
1839                 PCI_SLOT(((struct pci_dev *)
1840                                 (((struct si_info *)(sih))->pbus))->devfn));
1841
1842         if (slen < 0 || slen >= size) {
1843                 path[0] = '\0';
1844                 return -1;
1845         }
1846
1847         return 0;
1848 }
1849
1850 void ai_pci_up(struct si_pub *sih)
1851 {
1852         struct si_info *sii;
1853
1854         sii = (struct si_info *)sih;
1855
1856         if (PCI_FORCEHT(sii))
1857                 _ai_clkctl_cc(sii, CLK_FAST);
1858
1859         if (PCIE(sii))
1860                 pcicore_up(sii->pch, SI_PCIUP);
1861
1862 }
1863
1864 /* Unconfigure and/or apply various WARs when system is going to sleep mode */
1865 void ai_pci_sleep(struct si_pub *sih)
1866 {
1867         struct si_info *sii;
1868
1869         sii = (struct si_info *)sih;
1870
1871         pcicore_sleep(sii->pch);
1872 }
1873
1874 /* Unconfigure and/or apply various WARs when going down */
1875 void ai_pci_down(struct si_pub *sih)
1876 {
1877         struct si_info *sii;
1878
1879         sii = (struct si_info *)sih;
1880
1881         /* release FORCEHT since chip is going to "down" state */
1882         if (PCI_FORCEHT(sii))
1883                 _ai_clkctl_cc(sii, CLK_DYNAMIC);
1884
1885         pcicore_down(sii->pch, SI_PCIDOWN);
1886 }
1887
1888 /*
1889  * Configure the pci core for pci client (NIC) action
1890  * coremask is the bitvec of cores by index to be enabled.
1891  */
1892 void ai_pci_setup(struct si_pub *sih, uint coremask)
1893 {
1894         struct si_info *sii;
1895         struct sbpciregs __iomem *regs = NULL;
1896         u32 siflag = 0, w;
1897         uint idx = 0;
1898
1899         sii = (struct si_info *)sih;
1900
1901         if (PCI(sii)) {
1902                 /* get current core index */
1903                 idx = sii->curidx;
1904
1905                 /* we interrupt on this backplane flag number */
1906                 siflag = ai_flag(sih);
1907
1908                 /* switch over to pci core */
1909                 regs = ai_setcoreidx(sih, sii->pub.buscoreidx);
1910         }
1911
1912         /*
1913          * Enable sb->pci interrupts.  Assume
1914          * PCI rev 2.3 support was added in pci core rev 6 and things changed..
1915          */
1916         if (PCIE(sii) || (PCI(sii) && ((sii->pub.buscorerev) >= 6))) {
1917                 /* pci config write to set this core bit in PCIIntMask */
1918                 pci_read_config_dword(sii->pbus, PCI_INT_MASK, &w);
1919                 w |= (coremask << PCI_SBIM_SHIFT);
1920                 pci_write_config_dword(sii->pbus, PCI_INT_MASK, w);
1921         } else {
1922                 /* set sbintvec bit for our flag number */
1923                 ai_setint(sih, siflag);
1924         }
1925
1926         if (PCI(sii)) {
1927                 pcicore_pci_setup(sii->pch, regs);
1928
1929                 /* switch back to previous core */
1930                 ai_setcoreidx(sih, idx);
1931         }
1932 }
1933
1934 /*
1935  * Fixup SROMless PCI device's configuration.
1936  * The current core may be changed upon return.
1937  */
1938 int ai_pci_fixcfg(struct si_pub *sih)
1939 {
1940         uint origidx;
1941         void __iomem *regs = NULL;
1942         struct si_info *sii = (struct si_info *)sih;
1943
1944         /* Fixup PI in SROM shadow area to enable the correct PCI core access */
1945         /* save the current index */
1946         origidx = ai_coreidx(&sii->pub);
1947
1948         /* check 'pi' is correct and fix it if not */
1949         regs = ai_setcore(&sii->pub, sii->pub.buscoretype, 0);
1950         if (sii->pub.buscoretype == PCIE_CORE_ID)
1951                 pcicore_fixcfg_pcie(sii->pch,
1952                                     (struct sbpcieregs __iomem *)regs);
1953         else if (sii->pub.buscoretype == PCI_CORE_ID)
1954                 pcicore_fixcfg_pci(sii->pch, (struct sbpciregs __iomem *)regs);
1955
1956         /* restore the original index */
1957         ai_setcoreidx(&sii->pub, origidx);
1958
1959         pcicore_hwup(sii->pch);
1960         return 0;
1961 }
1962
1963 /* mask&set gpiocontrol bits */
1964 u32 ai_gpiocontrol(struct si_pub *sih, u32 mask, u32 val, u8 priority)
1965 {
1966         uint regoff;
1967
1968         regoff = offsetof(struct chipcregs, gpiocontrol);
1969         return ai_corereg(sih, SI_CC_IDX, regoff, mask, val);
1970 }
1971
1972 void ai_chipcontrl_epa4331(struct si_pub *sih, bool on)
1973 {
1974         struct si_info *sii;
1975         struct chipcregs __iomem *cc;
1976         uint origidx;
1977         u32 val;
1978
1979         sii = (struct si_info *)sih;
1980         origidx = ai_coreidx(sih);
1981
1982         cc = (struct chipcregs __iomem *) ai_setcore(sih, CC_CORE_ID, 0);
1983
1984         val = R_REG(&cc->chipcontrol);
1985
1986         if (on) {
1987                 if (sih->chippkg == 9 || sih->chippkg == 0xb)
1988                         /* Ext PA Controls for 4331 12x9 Package */
1989                         W_REG(&cc->chipcontrol, val |
1990                               CCTRL4331_EXTPA_EN |
1991                               CCTRL4331_EXTPA_ON_GPIO2_5);
1992                 else
1993                         /* Ext PA Controls for 4331 12x12 Package */
1994                         W_REG(&cc->chipcontrol,
1995                               val | CCTRL4331_EXTPA_EN);
1996         } else {
1997                 val &= ~(CCTRL4331_EXTPA_EN | CCTRL4331_EXTPA_ON_GPIO2_5);
1998                 W_REG(&cc->chipcontrol, val);
1999         }
2000
2001         ai_setcoreidx(sih, origidx);
2002 }
2003
2004 /* Enable BT-COEX & Ex-PA for 4313 */
2005 void ai_epa_4313war(struct si_pub *sih)
2006 {
2007         struct si_info *sii;
2008         struct chipcregs __iomem *cc;
2009         uint origidx;
2010
2011         sii = (struct si_info *)sih;
2012         origidx = ai_coreidx(sih);
2013
2014         cc = ai_setcore(sih, CC_CORE_ID, 0);
2015
2016         /* EPA Fix */
2017         W_REG(&cc->gpiocontrol,
2018               R_REG(&cc->gpiocontrol) | GPIO_CTRL_EPA_EN_MASK);
2019
2020         ai_setcoreidx(sih, origidx);
2021 }
2022
2023 /* check if the device is removed */
2024 bool ai_deviceremoved(struct si_pub *sih)
2025 {
2026         u32 w;
2027         struct si_info *sii;
2028
2029         sii = (struct si_info *)sih;
2030
2031         pci_read_config_dword(sii->pbus, PCI_VENDOR_ID, &w);
2032         if ((w & 0xFFFF) != PCI_VENDOR_ID_BROADCOM)
2033                 return true;
2034
2035         return false;
2036 }
2037
2038 bool ai_is_sprom_available(struct si_pub *sih)
2039 {
2040         if (sih->ccrev >= 31) {
2041                 struct si_info *sii;
2042                 uint origidx;
2043                 struct chipcregs __iomem *cc;
2044                 u32 sromctrl;
2045
2046                 if ((sih->cccaps & CC_CAP_SROM) == 0)
2047                         return false;
2048
2049                 sii = (struct si_info *)sih;
2050                 origidx = sii->curidx;
2051                 cc = ai_setcoreidx(sih, SI_CC_IDX);
2052                 sromctrl = R_REG(&cc->sromcontrol);
2053                 ai_setcoreidx(sih, origidx);
2054                 return sromctrl & SRC_PRESENT;
2055         }
2056
2057         switch (sih->chip) {
2058         case BCM4313_CHIP_ID:
2059                 return (sih->chipst & CST4313_SPROM_PRESENT) != 0;
2060         default:
2061                 return true;
2062         }
2063 }
2064
2065 bool ai_is_otp_disabled(struct si_pub *sih)
2066 {
2067         switch (sih->chip) {
2068         case BCM4313_CHIP_ID:
2069                 return (sih->chipst & CST4313_OTP_PRESENT) == 0;
2070                 /* These chips always have their OTP on */
2071         case BCM43224_CHIP_ID:
2072         case BCM43225_CHIP_ID:
2073         default:
2074                 return false;
2075         }
2076 }