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