]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/brcm80211/util/nicpci.c
staging: brcm80211: fix 'ERROR: "foo * bar" should be "foo *bar"'
[net-next-2.6.git] / drivers / staging / brcm80211 / util / nicpci.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
17 #include <typedefs.h>
18 #include <bcmdefs.h>
19 #include <osl.h>
20 #include <bcmutils.h>
21 #include <siutils.h>
22 #include <hndsoc.h>
23 #include <bcmdevs.h>
24 #include <sbchipc.h>
25 #include <pci_core.h>
26 #include <pcie_core.h>
27 #include <nicpci.h>
28 #include <pcicfg.h>
29
30 typedef struct {
31         union {
32                 sbpcieregs_t *pcieregs;
33                 sbpciregs_t *pciregs;
34         } regs;                 /* Memory mapped register to the core */
35
36         si_t *sih;              /* System interconnect handle */
37         osl_t *osh;             /* OSL handle */
38         uint8 pciecap_lcreg_offset;     /* PCIE capability LCreg offset in the config space */
39         bool pcie_pr42767;
40         uint8 pcie_polarity;
41         uint8 pcie_war_aspm_ovr;        /* Override ASPM/Clkreq settings */
42
43         uint8 pmecap_offset;    /* PM Capability offset in the config space */
44         bool pmecap;            /* Capable of generating PME */
45 } pcicore_info_t;
46
47 /* debug/trace */
48 #define PCI_ERROR(args)
49 #define PCIE_PUB(sih) ((BUSTYPE((sih)->bustype) == PCI_BUS) && ((sih)->buscoretype == PCIE_CORE_ID))
50
51 /* routines to access mdio slave device registers */
52 static bool pcie_mdiosetblock(pcicore_info_t *pi, uint blk);
53 static int pcie_mdioop(pcicore_info_t *pi, uint physmedia, uint regaddr,
54                        bool write, uint *val);
55 static int pcie_mdiowrite(pcicore_info_t *pi, uint physmedia, uint readdr,
56                           uint val);
57 static int pcie_mdioread(pcicore_info_t *pi, uint physmedia, uint readdr,
58                          uint *ret_val);
59
60 static void pcie_extendL1timer(pcicore_info_t *pi, bool extend);
61 static void pcie_clkreq_upd(pcicore_info_t *pi, uint state);
62
63 static void pcie_war_aspm_clkreq(pcicore_info_t *pi);
64 static void pcie_war_serdes(pcicore_info_t *pi);
65 static void pcie_war_noplldown(pcicore_info_t *pi);
66 static void pcie_war_polarity(pcicore_info_t *pi);
67 static void pcie_war_pci_setup(pcicore_info_t *pi);
68
69 static bool pcicore_pmecap(pcicore_info_t *pi);
70
71 #define PCIE_ASPM(sih)  ((PCIE_PUB(sih)) && (((sih)->buscorerev >= 3) && ((sih)->buscorerev <= 5)))
72
73 #define DWORD_ALIGN(x)  (x & ~(0x03))
74 #define BYTE_POS(x) (x & 0x3)
75 #define WORD_POS(x) (x & 0x1)
76
77 #define BYTE_SHIFT(x)  (8 * BYTE_POS(x))
78 #define WORD_SHIFT(x)  (16 * WORD_POS(x))
79
80 #define BYTE_VAL(a, x) ((a >> BYTE_SHIFT(x)) & 0xFF)
81 #define WORD_VAL(a, x) ((a >> WORD_SHIFT(x)) & 0xFFFF)
82
83 #define read_pci_cfg_byte(a) \
84         (BYTE_VAL(OSL_PCI_READ_CONFIG(osh, DWORD_ALIGN(a), 4), a) & 0xff)
85
86 #define read_pci_cfg_word(a) \
87         (WORD_VAL(OSL_PCI_READ_CONFIG(osh, DWORD_ALIGN(a), 4), a) & 0xffff)
88
89 #define write_pci_cfg_byte(a, val) do { \
90         uint32 tmpval; \
91         tmpval = (OSL_PCI_READ_CONFIG(osh, DWORD_ALIGN(a), 4) & ~0xFF << BYTE_POS(a)) | \
92                 val << BYTE_POS(a); \
93         OSL_PCI_WRITE_CONFIG(osh, DWORD_ALIGN(a), 4, tmpval); \
94         } while (0)
95
96 #define write_pci_cfg_word(a, val) do { \
97         uint32 tmpval; \
98         tmpval = (OSL_PCI_READ_CONFIG(osh, DWORD_ALIGN(a), 4) & ~0xFFFF << WORD_POS(a)) | \
99                 val << WORD_POS(a); \
100         OSL_PCI_WRITE_CONFIG(osh, DWORD_ALIGN(a), 4, tmpval); \
101         } while (0)
102
103 /* delay needed between the mdio control/ mdiodata register data access */
104 #define PR28829_DELAY() OSL_DELAY(10)
105
106 /* Initialize the PCI core. It's caller's responsibility to make sure that this is done
107  * only once
108  */
109 void *pcicore_init(si_t *sih, osl_t *osh, void *regs)
110 {
111         pcicore_info_t *pi;
112
113         ASSERT(sih->bustype == PCI_BUS);
114
115         /* alloc pcicore_info_t */
116         if ((pi = MALLOC(osh, sizeof(pcicore_info_t))) == NULL) {
117                 PCI_ERROR(("pci_attach: malloc failed! malloced %d bytes\n",
118                            MALLOCED(osh)));
119                 return (NULL);
120         }
121
122         bzero(pi, sizeof(pcicore_info_t));
123
124         pi->sih = sih;
125         pi->osh = osh;
126
127         if (sih->buscoretype == PCIE_CORE_ID) {
128                 uint8 cap_ptr;
129                 pi->regs.pcieregs = (sbpcieregs_t *) regs;
130                 cap_ptr =
131                     pcicore_find_pci_capability(pi->osh, PCI_CAP_PCIECAP_ID,
132                                                 NULL, NULL);
133                 ASSERT(cap_ptr);
134                 pi->pciecap_lcreg_offset = cap_ptr + PCIE_CAP_LINKCTRL_OFFSET;
135         } else
136                 pi->regs.pciregs = (sbpciregs_t *) regs;
137
138         return pi;
139 }
140
141 void pcicore_deinit(void *pch)
142 {
143         pcicore_info_t *pi = (pcicore_info_t *) pch;
144
145         if (pi == NULL)
146                 return;
147         MFREE(pi->osh, pi, sizeof(pcicore_info_t));
148 }
149
150 /* return cap_offset if requested capability exists in the PCI config space */
151 /* Note that it's caller's responsibility to make sure it's a pci bus */
152 uint8
153 pcicore_find_pci_capability(osl_t *osh, uint8 req_cap_id, uchar *buf,
154                             uint32 *buflen)
155 {
156         uint8 cap_id;
157         uint8 cap_ptr = 0;
158         uint32 bufsize;
159         uint8 byte_val;
160
161         /* check for Header type 0 */
162         byte_val = read_pci_cfg_byte(PCI_CFG_HDR);
163         if ((byte_val & 0x7f) != PCI_HEADER_NORMAL)
164                 goto end;
165
166         /* check if the capability pointer field exists */
167         byte_val = read_pci_cfg_byte(PCI_CFG_STAT);
168         if (!(byte_val & PCI_CAPPTR_PRESENT))
169                 goto end;
170
171         cap_ptr = read_pci_cfg_byte(PCI_CFG_CAPPTR);
172         /* check if the capability pointer is 0x00 */
173         if (cap_ptr == 0x00)
174                 goto end;
175
176         /* loop thr'u the capability list and see if the pcie capabilty exists */
177
178         cap_id = read_pci_cfg_byte(cap_ptr);
179
180         while (cap_id != req_cap_id) {
181                 cap_ptr = read_pci_cfg_byte((cap_ptr + 1));
182                 if (cap_ptr == 0x00)
183                         break;
184                 cap_id = read_pci_cfg_byte(cap_ptr);
185         }
186         if (cap_id != req_cap_id) {
187                 goto end;
188         }
189         /* found the caller requested capability */
190         if ((buf != NULL) && (buflen != NULL)) {
191                 uint8 cap_data;
192
193                 bufsize = *buflen;
194                 if (!bufsize)
195                         goto end;
196                 *buflen = 0;
197                 /* copy the cpability data excluding cap ID and next ptr */
198                 cap_data = cap_ptr + 2;
199                 if ((bufsize + cap_data) > SZPCR)
200                         bufsize = SZPCR - cap_data;
201                 *buflen = bufsize;
202                 while (bufsize--) {
203                         *buf = read_pci_cfg_byte(cap_data);
204                         cap_data++;
205                         buf++;
206                 }
207         }
208  end:
209         return cap_ptr;
210 }
211
212 /* ***** Register Access API */
213 uint
214 pcie_readreg(osl_t *osh, sbpcieregs_t *pcieregs, uint addrtype, uint offset)
215 {
216         uint retval = 0xFFFFFFFF;
217
218         ASSERT(pcieregs != NULL);
219
220         switch (addrtype) {
221         case PCIE_CONFIGREGS:
222                 W_REG(osh, (&pcieregs->configaddr), offset);
223                 (void)R_REG(osh, (&pcieregs->configaddr));
224                 retval = R_REG(osh, &(pcieregs->configdata));
225                 break;
226         case PCIE_PCIEREGS:
227                 W_REG(osh, &(pcieregs->pcieindaddr), offset);
228                 (void)R_REG(osh, (&pcieregs->pcieindaddr));
229                 retval = R_REG(osh, &(pcieregs->pcieinddata));
230                 break;
231         default:
232                 ASSERT(0);
233                 break;
234         }
235
236         return retval;
237 }
238
239 uint
240 pcie_writereg(osl_t *osh, sbpcieregs_t *pcieregs, uint addrtype, uint offset,
241               uint val)
242 {
243         ASSERT(pcieregs != NULL);
244
245         switch (addrtype) {
246         case PCIE_CONFIGREGS:
247                 W_REG(osh, (&pcieregs->configaddr), offset);
248                 W_REG(osh, (&pcieregs->configdata), val);
249                 break;
250         case PCIE_PCIEREGS:
251                 W_REG(osh, (&pcieregs->pcieindaddr), offset);
252                 W_REG(osh, (&pcieregs->pcieinddata), val);
253                 break;
254         default:
255                 ASSERT(0);
256                 break;
257         }
258         return 0;
259 }
260
261 static bool pcie_mdiosetblock(pcicore_info_t *pi, uint blk)
262 {
263         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
264         uint mdiodata, i = 0;
265         uint pcie_serdes_spinwait = 200;
266
267         mdiodata =
268             MDIODATA_START | MDIODATA_WRITE | (MDIODATA_DEV_ADDR <<
269                                                MDIODATA_DEVADDR_SHF) |
270             (MDIODATA_BLK_ADDR << MDIODATA_REGADDR_SHF) | MDIODATA_TA | (blk <<
271                                                                          4);
272         W_REG(pi->osh, &pcieregs->mdiodata, mdiodata);
273
274         PR28829_DELAY();
275         /* retry till the transaction is complete */
276         while (i < pcie_serdes_spinwait) {
277                 if (R_REG(pi->osh, &(pcieregs->mdiocontrol)) &
278                     MDIOCTL_ACCESS_DONE) {
279                         break;
280                 }
281                 OSL_DELAY(1000);
282                 i++;
283         }
284
285         if (i >= pcie_serdes_spinwait) {
286                 PCI_ERROR(("pcie_mdiosetblock: timed out\n"));
287                 return FALSE;
288         }
289
290         return TRUE;
291 }
292
293 static int
294 pcie_mdioop(pcicore_info_t *pi, uint physmedia, uint regaddr, bool write,
295             uint *val)
296 {
297         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
298         uint mdiodata;
299         uint i = 0;
300         uint pcie_serdes_spinwait = 10;
301
302         /* enable mdio access to SERDES */
303         W_REG(pi->osh, (&pcieregs->mdiocontrol),
304               MDIOCTL_PREAM_EN | MDIOCTL_DIVISOR_VAL);
305
306         if (pi->sih->buscorerev >= 10) {
307                 /* new serdes is slower in rw, using two layers of reg address mapping */
308                 if (!pcie_mdiosetblock(pi, physmedia))
309                         return 1;
310                 mdiodata = (MDIODATA_DEV_ADDR << MDIODATA_DEVADDR_SHF) |
311                     (regaddr << MDIODATA_REGADDR_SHF);
312                 pcie_serdes_spinwait *= 20;
313         } else {
314                 mdiodata = (physmedia << MDIODATA_DEVADDR_SHF_OLD) |
315                     (regaddr << MDIODATA_REGADDR_SHF_OLD);
316         }
317
318         if (!write)
319                 mdiodata |= (MDIODATA_START | MDIODATA_READ | MDIODATA_TA);
320         else
321                 mdiodata |=
322                     (MDIODATA_START | MDIODATA_WRITE | MDIODATA_TA | *val);
323
324         W_REG(pi->osh, &pcieregs->mdiodata, mdiodata);
325
326         PR28829_DELAY();
327
328         /* retry till the transaction is complete */
329         while (i < pcie_serdes_spinwait) {
330                 if (R_REG(pi->osh, &(pcieregs->mdiocontrol)) &
331                     MDIOCTL_ACCESS_DONE) {
332                         if (!write) {
333                                 PR28829_DELAY();
334                                 *val =
335                                     (R_REG(pi->osh, &(pcieregs->mdiodata)) &
336                                      MDIODATA_MASK);
337                         }
338                         /* Disable mdio access to SERDES */
339                         W_REG(pi->osh, (&pcieregs->mdiocontrol), 0);
340                         return 0;
341                 }
342                 OSL_DELAY(1000);
343                 i++;
344         }
345
346         PCI_ERROR(("pcie_mdioop: timed out op: %d\n", write));
347         /* Disable mdio access to SERDES */
348         W_REG(pi->osh, (&pcieregs->mdiocontrol), 0);
349         return 1;
350 }
351
352 /* use the mdio interface to read from mdio slaves */
353 static int
354 pcie_mdioread(pcicore_info_t *pi, uint physmedia, uint regaddr, uint *regval)
355 {
356         return pcie_mdioop(pi, physmedia, regaddr, FALSE, regval);
357 }
358
359 /* use the mdio interface to write to mdio slaves */
360 static int
361 pcie_mdiowrite(pcicore_info_t *pi, uint physmedia, uint regaddr, uint val)
362 {
363         return pcie_mdioop(pi, physmedia, regaddr, TRUE, &val);
364 }
365
366 /* ***** Support functions ***** */
367 uint8 pcie_clkreq(void *pch, uint32 mask, uint32 val)
368 {
369         pcicore_info_t *pi = (pcicore_info_t *) pch;
370         uint32 reg_val;
371         uint8 offset;
372
373         offset = pi->pciecap_lcreg_offset;
374         if (!offset)
375                 return 0;
376
377         reg_val = OSL_PCI_READ_CONFIG(pi->osh, offset, sizeof(uint32));
378         /* set operation */
379         if (mask) {
380                 if (val)
381                         reg_val |= PCIE_CLKREQ_ENAB;
382                 else
383                         reg_val &= ~PCIE_CLKREQ_ENAB;
384                 OSL_PCI_WRITE_CONFIG(pi->osh, offset, sizeof(uint32), reg_val);
385                 reg_val = OSL_PCI_READ_CONFIG(pi->osh, offset, sizeof(uint32));
386         }
387         if (reg_val & PCIE_CLKREQ_ENAB)
388                 return 1;
389         else
390                 return 0;
391 }
392
393 static void pcie_extendL1timer(pcicore_info_t *pi, bool extend)
394 {
395         uint32 w;
396         si_t *sih = pi->sih;
397         osl_t *osh = pi->osh;
398         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
399
400         if (!PCIE_PUB(sih) || sih->buscorerev < 7)
401                 return;
402
403         w = pcie_readreg(osh, pcieregs, PCIE_PCIEREGS, PCIE_DLLP_PMTHRESHREG);
404         if (extend)
405                 w |= PCIE_ASPMTIMER_EXTEND;
406         else
407                 w &= ~PCIE_ASPMTIMER_EXTEND;
408         pcie_writereg(osh, pcieregs, PCIE_PCIEREGS, PCIE_DLLP_PMTHRESHREG, w);
409         w = pcie_readreg(osh, pcieregs, PCIE_PCIEREGS, PCIE_DLLP_PMTHRESHREG);
410 }
411
412 /* centralized clkreq control policy */
413 static void pcie_clkreq_upd(pcicore_info_t *pi, uint state)
414 {
415         si_t *sih = pi->sih;
416         ASSERT(PCIE_PUB(sih));
417
418         switch (state) {
419         case SI_DOATTACH:
420                 if (PCIE_ASPM(sih))
421                         pcie_clkreq((void *)pi, 1, 0);
422                 break;
423         case SI_PCIDOWN:
424                 if (sih->buscorerev == 6) {     /* turn on serdes PLL down */
425                         si_corereg(sih, SI_CC_IDX,
426                                    OFFSETOF(chipcregs_t, chipcontrol_addr), ~0,
427                                    0);
428                         si_corereg(sih, SI_CC_IDX,
429                                    OFFSETOF(chipcregs_t, chipcontrol_data),
430                                    ~0x40, 0);
431                 } else if (pi->pcie_pr42767) {
432                         pcie_clkreq((void *)pi, 1, 1);
433                 }
434                 break;
435         case SI_PCIUP:
436                 if (sih->buscorerev == 6) {     /* turn off serdes PLL down */
437                         si_corereg(sih, SI_CC_IDX,
438                                    OFFSETOF(chipcregs_t, chipcontrol_addr), ~0,
439                                    0);
440                         si_corereg(sih, SI_CC_IDX,
441                                    OFFSETOF(chipcregs_t, chipcontrol_data),
442                                    ~0x40, 0x40);
443                 } else if (PCIE_ASPM(sih)) {    /* disable clkreq */
444                         pcie_clkreq((void *)pi, 1, 0);
445                 }
446                 break;
447         default:
448                 ASSERT(0);
449                 break;
450         }
451 }
452
453 /* ***** PCI core WARs ***** */
454 /* Done only once at attach time */
455 static void pcie_war_polarity(pcicore_info_t *pi)
456 {
457         uint32 w;
458
459         if (pi->pcie_polarity != 0)
460                 return;
461
462         w = pcie_readreg(pi->osh, pi->regs.pcieregs, PCIE_PCIEREGS,
463                          PCIE_PLP_STATUSREG);
464
465         /* Detect the current polarity at attach and force that polarity and
466          * disable changing the polarity
467          */
468         if ((w & PCIE_PLP_POLARITYINV_STAT) == 0)
469                 pi->pcie_polarity = (SERDES_RX_CTRL_FORCE);
470         else
471                 pi->pcie_polarity =
472                     (SERDES_RX_CTRL_FORCE | SERDES_RX_CTRL_POLARITY);
473 }
474
475 /* enable ASPM and CLKREQ if srom doesn't have it */
476 /* Needs to happen when update to shadow SROM is needed
477  *   : Coming out of 'standby'/'hibernate'
478  *   : If pcie_war_aspm_ovr state changed
479  */
480 static void pcie_war_aspm_clkreq(pcicore_info_t *pi)
481 {
482         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
483         si_t *sih = pi->sih;
484         uint16 val16, *reg16;
485         uint32 w;
486
487         if (!PCIE_ASPM(sih))
488                 return;
489
490         /* bypass this on QT or VSIM */
491         if (!ISSIM_ENAB(sih)) {
492
493                 reg16 = &pcieregs->sprom[SRSH_ASPM_OFFSET];
494                 val16 = R_REG(pi->osh, reg16);
495
496                 val16 &= ~SRSH_ASPM_ENB;
497                 if (pi->pcie_war_aspm_ovr == PCIE_ASPM_ENAB)
498                         val16 |= SRSH_ASPM_ENB;
499                 else if (pi->pcie_war_aspm_ovr == PCIE_ASPM_L1_ENAB)
500                         val16 |= SRSH_ASPM_L1_ENB;
501                 else if (pi->pcie_war_aspm_ovr == PCIE_ASPM_L0s_ENAB)
502                         val16 |= SRSH_ASPM_L0s_ENB;
503
504                 W_REG(pi->osh, reg16, val16);
505
506                 w = OSL_PCI_READ_CONFIG(pi->osh, pi->pciecap_lcreg_offset,
507                                         sizeof(uint32));
508                 w &= ~PCIE_ASPM_ENAB;
509                 w |= pi->pcie_war_aspm_ovr;
510                 OSL_PCI_WRITE_CONFIG(pi->osh, pi->pciecap_lcreg_offset,
511                                      sizeof(uint32), w);
512         }
513
514         reg16 = &pcieregs->sprom[SRSH_CLKREQ_OFFSET_REV5];
515         val16 = R_REG(pi->osh, reg16);
516
517         if (pi->pcie_war_aspm_ovr != PCIE_ASPM_DISAB) {
518                 val16 |= SRSH_CLKREQ_ENB;
519                 pi->pcie_pr42767 = TRUE;
520         } else
521                 val16 &= ~SRSH_CLKREQ_ENB;
522
523         W_REG(pi->osh, reg16, val16);
524 }
525
526 /* Apply the polarity determined at the start */
527 /* Needs to happen when coming out of 'standby'/'hibernate' */
528 static void pcie_war_serdes(pcicore_info_t *pi)
529 {
530         uint32 w = 0;
531
532         if (pi->pcie_polarity != 0)
533                 pcie_mdiowrite(pi, MDIODATA_DEV_RX, SERDES_RX_CTRL,
534                                pi->pcie_polarity);
535
536         pcie_mdioread(pi, MDIODATA_DEV_PLL, SERDES_PLL_CTRL, &w);
537         if (w & PLL_CTRL_FREQDET_EN) {
538                 w &= ~PLL_CTRL_FREQDET_EN;
539                 pcie_mdiowrite(pi, MDIODATA_DEV_PLL, SERDES_PLL_CTRL, w);
540         }
541 }
542
543 /* Fix MISC config to allow coming out of L2/L3-Ready state w/o PRST */
544 /* Needs to happen when coming out of 'standby'/'hibernate' */
545 static void BCMINITFN(pcie_misc_config_fixup) (pcicore_info_t *pi) {
546         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
547         uint16 val16, *reg16;
548
549         reg16 = &pcieregs->sprom[SRSH_PCIE_MISC_CONFIG];
550         val16 = R_REG(pi->osh, reg16);
551
552         if ((val16 & SRSH_L23READY_EXIT_NOPERST) == 0) {
553                 val16 |= SRSH_L23READY_EXIT_NOPERST;
554                 W_REG(pi->osh, reg16, val16);
555         }
556 }
557
558 /* quick hack for testing */
559 /* Needs to happen when coming out of 'standby'/'hibernate' */
560 static void pcie_war_noplldown(pcicore_info_t *pi)
561 {
562         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
563         uint16 *reg16;
564
565         ASSERT(pi->sih->buscorerev == 7);
566
567         /* turn off serdes PLL down */
568         si_corereg(pi->sih, SI_CC_IDX, OFFSETOF(chipcregs_t, chipcontrol),
569                    CHIPCTRL_4321_PLL_DOWN, CHIPCTRL_4321_PLL_DOWN);
570
571         /*  clear srom shadow backdoor */
572         reg16 = &pcieregs->sprom[SRSH_BD_OFFSET];
573         W_REG(pi->osh, reg16, 0);
574 }
575
576 /* Needs to happen when coming out of 'standby'/'hibernate' */
577 static void pcie_war_pci_setup(pcicore_info_t *pi)
578 {
579         si_t *sih = pi->sih;
580         osl_t *osh = pi->osh;
581         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
582         uint32 w;
583
584         if ((sih->buscorerev == 0) || (sih->buscorerev == 1)) {
585                 w = pcie_readreg(osh, pcieregs, PCIE_PCIEREGS,
586                                  PCIE_TLP_WORKAROUNDSREG);
587                 w |= 0x8;
588                 pcie_writereg(osh, pcieregs, PCIE_PCIEREGS,
589                               PCIE_TLP_WORKAROUNDSREG, w);
590         }
591
592         if (sih->buscorerev == 1) {
593                 w = pcie_readreg(osh, pcieregs, PCIE_PCIEREGS, PCIE_DLLP_LCREG);
594                 w |= (0x40);
595                 pcie_writereg(osh, pcieregs, PCIE_PCIEREGS, PCIE_DLLP_LCREG, w);
596         }
597
598         if (sih->buscorerev == 0) {
599                 pcie_mdiowrite(pi, MDIODATA_DEV_RX, SERDES_RX_TIMER1, 0x8128);
600                 pcie_mdiowrite(pi, MDIODATA_DEV_RX, SERDES_RX_CDR, 0x0100);
601                 pcie_mdiowrite(pi, MDIODATA_DEV_RX, SERDES_RX_CDRBW, 0x1466);
602         } else if (PCIE_ASPM(sih)) {
603                 /* Change the L1 threshold for better performance */
604                 w = pcie_readreg(osh, pcieregs, PCIE_PCIEREGS,
605                                  PCIE_DLLP_PMTHRESHREG);
606                 w &= ~(PCIE_L1THRESHOLDTIME_MASK);
607                 w |= (PCIE_L1THRESHOLD_WARVAL << PCIE_L1THRESHOLDTIME_SHIFT);
608                 pcie_writereg(osh, pcieregs, PCIE_PCIEREGS,
609                               PCIE_DLLP_PMTHRESHREG, w);
610
611                 pcie_war_serdes(pi);
612
613                 pcie_war_aspm_clkreq(pi);
614         } else if (pi->sih->buscorerev == 7)
615                 pcie_war_noplldown(pi);
616
617         /* Note that the fix is actually in the SROM, that's why this is open-ended */
618         if (pi->sih->buscorerev >= 6)
619                 pcie_misc_config_fixup(pi);
620 }
621
622 void pcie_war_ovr_aspm_update(void *pch, uint8 aspm)
623 {
624         pcicore_info_t *pi = (pcicore_info_t *) pch;
625
626         if (!PCIE_ASPM(pi->sih))
627                 return;
628
629         /* Validate */
630         if (aspm > PCIE_ASPM_ENAB)
631                 return;
632
633         pi->pcie_war_aspm_ovr = aspm;
634
635         /* Update the current state */
636         pcie_war_aspm_clkreq(pi);
637 }
638
639 /* ***** Functions called during driver state changes ***** */
640 void BCMATTACHFN(pcicore_attach) (void *pch, char *pvars, int state) {
641         pcicore_info_t *pi = (pcicore_info_t *) pch;
642         si_t *sih = pi->sih;
643
644         /* Determine if this board needs override */
645         if (PCIE_ASPM(sih)) {
646                 if ((uint32) getintvar(pvars, "boardflags2") & BFL2_PCIEWAR_OVR) {
647                         pi->pcie_war_aspm_ovr = PCIE_ASPM_DISAB;
648                 } else {
649                         pi->pcie_war_aspm_ovr = PCIE_ASPM_ENAB;
650                 }
651         }
652
653         /* These need to happen in this order only */
654         pcie_war_polarity(pi);
655
656         pcie_war_serdes(pi);
657
658         pcie_war_aspm_clkreq(pi);
659
660         pcie_clkreq_upd(pi, state);
661
662 }
663
664 void pcicore_hwup(void *pch)
665 {
666         pcicore_info_t *pi = (pcicore_info_t *) pch;
667
668         if (!pi || !PCIE_PUB(pi->sih))
669                 return;
670
671         pcie_war_pci_setup(pi);
672 }
673
674 void pcicore_up(void *pch, int state)
675 {
676         pcicore_info_t *pi = (pcicore_info_t *) pch;
677
678         if (!pi || !PCIE_PUB(pi->sih))
679                 return;
680
681         /* Restore L1 timer for better performance */
682         pcie_extendL1timer(pi, TRUE);
683
684         pcie_clkreq_upd(pi, state);
685 }
686
687 /* When the device is going to enter D3 state (or the system is going to enter S3/S4 states */
688 void pcicore_sleep(void *pch)
689 {
690         pcicore_info_t *pi = (pcicore_info_t *) pch;
691         uint32 w;
692
693         if (!pi || !PCIE_ASPM(pi->sih))
694                 return;
695
696         w = OSL_PCI_READ_CONFIG(pi->osh, pi->pciecap_lcreg_offset,
697                                 sizeof(uint32));
698         w &= ~PCIE_CAP_LCREG_ASPML1;
699         OSL_PCI_WRITE_CONFIG(pi->osh, pi->pciecap_lcreg_offset, sizeof(uint32),
700                              w);
701
702         pi->pcie_pr42767 = FALSE;
703 }
704
705 void pcicore_down(void *pch, int state)
706 {
707         pcicore_info_t *pi = (pcicore_info_t *) pch;
708
709         if (!pi || !PCIE_PUB(pi->sih))
710                 return;
711
712         pcie_clkreq_upd(pi, state);
713
714         /* Reduce L1 timer for better power savings */
715         pcie_extendL1timer(pi, FALSE);
716 }
717
718 /* ***** Wake-on-wireless-LAN (WOWL) support functions ***** */
719 /* Just uses PCI config accesses to find out, when needed before sb_attach is done */
720 bool pcicore_pmecap_fast(osl_t *osh)
721 {
722         uint8 cap_ptr;
723         uint32 pmecap;
724
725         cap_ptr =
726             pcicore_find_pci_capability(osh, PCI_CAP_POWERMGMTCAP_ID, NULL,
727                                         NULL);
728
729         if (!cap_ptr)
730                 return FALSE;
731
732         pmecap = OSL_PCI_READ_CONFIG(osh, cap_ptr, sizeof(uint32));
733
734         return ((pmecap & PME_CAP_PM_STATES) != 0);
735 }
736
737 /* return TRUE if PM capability exists in the pci config space
738  * Uses and caches the information using core handle
739  */
740 static bool pcicore_pmecap(pcicore_info_t *pi)
741 {
742         uint8 cap_ptr;
743         uint32 pmecap;
744
745         if (!pi->pmecap_offset) {
746                 cap_ptr =
747                     pcicore_find_pci_capability(pi->osh,
748                                                 PCI_CAP_POWERMGMTCAP_ID, NULL,
749                                                 NULL);
750                 if (!cap_ptr)
751                         return FALSE;
752
753                 pi->pmecap_offset = cap_ptr;
754
755                 pmecap =
756                     OSL_PCI_READ_CONFIG(pi->osh, pi->pmecap_offset,
757                                         sizeof(uint32));
758
759                 /* At least one state can generate PME */
760                 pi->pmecap = (pmecap & PME_CAP_PM_STATES) != 0;
761         }
762
763         return (pi->pmecap);
764 }
765
766 /* Enable PME generation */
767 void pcicore_pmeen(void *pch)
768 {
769         pcicore_info_t *pi = (pcicore_info_t *) pch;
770         uint32 w;
771
772         /* if not pmecapable return */
773         if (!pcicore_pmecap(pi))
774                 return;
775
776         w = OSL_PCI_READ_CONFIG(pi->osh, pi->pmecap_offset + PME_CSR_OFFSET,
777                                 sizeof(uint32));
778         w |= (PME_CSR_PME_EN);
779         OSL_PCI_WRITE_CONFIG(pi->osh, pi->pmecap_offset + PME_CSR_OFFSET,
780                              sizeof(uint32), w);
781 }
782
783 /*
784  * Return TRUE if PME status set
785  */
786 bool pcicore_pmestat(void *pch)
787 {
788         pcicore_info_t *pi = (pcicore_info_t *) pch;
789         uint32 w;
790
791         if (!pcicore_pmecap(pi))
792                 return FALSE;
793
794         w = OSL_PCI_READ_CONFIG(pi->osh, pi->pmecap_offset + PME_CSR_OFFSET,
795                                 sizeof(uint32));
796
797         return (w & PME_CSR_PME_STAT) == PME_CSR_PME_STAT;
798 }
799
800 /* Disable PME generation, clear the PME status bit if set
801  */
802 void pcicore_pmeclr(void *pch)
803 {
804         pcicore_info_t *pi = (pcicore_info_t *) pch;
805         uint32 w;
806
807         if (!pcicore_pmecap(pi))
808                 return;
809
810         w = OSL_PCI_READ_CONFIG(pi->osh, pi->pmecap_offset + PME_CSR_OFFSET,
811                                 sizeof(uint32));
812
813         PCI_ERROR(("pcicore_pci_pmeclr PMECSR : 0x%x\n", w));
814
815         /* PMESTAT is cleared by writing 1 to it */
816         w &= ~(PME_CSR_PME_EN);
817
818         OSL_PCI_WRITE_CONFIG(pi->osh, pi->pmecap_offset + PME_CSR_OFFSET,
819                              sizeof(uint32), w);
820 }
821
822 uint32 pcie_lcreg(void *pch, uint32 mask, uint32 val)
823 {
824         pcicore_info_t *pi = (pcicore_info_t *) pch;
825         uint8 offset;
826
827         offset = pi->pciecap_lcreg_offset;
828         if (!offset)
829                 return 0;
830
831         /* set operation */
832         if (mask)
833                 OSL_PCI_WRITE_CONFIG(pi->osh, offset, sizeof(uint32), val);
834
835         return OSL_PCI_READ_CONFIG(pi->osh, offset, sizeof(uint32));
836 }
837
838 uint32
839 pcicore_pciereg(void *pch, uint32 offset, uint32 mask, uint32 val, uint type)
840 {
841         uint32 reg_val = 0;
842         pcicore_info_t *pi = (pcicore_info_t *) pch;
843         sbpcieregs_t *pcieregs = pi->regs.pcieregs;
844         osl_t *osh = pi->osh;
845
846         if (mask) {
847                 PCI_ERROR(("PCIEREG: 0x%x writeval  0x%x\n", offset, val));
848                 pcie_writereg(osh, pcieregs, type, offset, val);
849         }
850
851         /* Should not read register 0x154 */
852         if (pi->sih->buscorerev <= 5 && offset == PCIE_DLLP_PCIE11
853             && type == PCIE_PCIEREGS)
854                 return reg_val;
855
856         reg_val = pcie_readreg(osh, pcieregs, type, offset);
857         PCI_ERROR(("PCIEREG: 0x%x readval is 0x%x\n", offset, reg_val));
858
859         return reg_val;
860 }
861
862 uint32
863 pcicore_pcieserdesreg(void *pch, uint32 mdioslave, uint32 offset, uint32 mask,
864                       uint32 val)
865 {
866         uint32 reg_val = 0;
867         pcicore_info_t *pi = (pcicore_info_t *) pch;
868
869         if (mask) {
870                 PCI_ERROR(("PCIEMDIOREG: 0x%x writeval  0x%x\n", offset, val));
871                 pcie_mdiowrite(pi, mdioslave, offset, val);
872         }
873
874         if (pcie_mdioread(pi, mdioslave, offset, &reg_val))
875                 reg_val = 0xFFFFFFFF;
876         PCI_ERROR(("PCIEMDIOREG: dev 0x%x offset 0x%x read 0x%x\n", mdioslave,
877                    offset, reg_val));
878
879         return reg_val;
880 }