~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/staging/brcm80211/util/nicpci.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

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