2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <brcmu_utils.h>
26 #include <chipcommon.h>
29 /* ********** from siutils.c *********** */
36 #define SCC_SS_MASK 0x00000007 /* slow clock source mask */
37 #define SCC_SS_LPO 0x00000000 /* source of slow clock is LPO */
38 #define SCC_SS_XTAL 0x00000001 /* source of slow clock is crystal */
39 #define SCC_SS_PCI 0x00000002 /* source of slow clock is PCI */
40 #define SCC_LF 0x00000200 /* LPOFreqSel, 1: 160Khz, 0: 32KHz */
41 #define SCC_LP 0x00000400 /* LPOPowerDown, 1: LPO is disabled,
44 #define SCC_FS 0x00000800 /* ForceSlowClk, 1: sb/cores running on slow clock,
45 * 0: power logic control
47 #define SCC_IP 0x00001000 /* IgnorePllOffReq, 1/0: power logic ignores/honors
48 * PLL clock disable requests from core
50 #define SCC_XC 0x00002000 /* XtalControlEn, 1/0: power logic does/doesn't
51 * disable crystal when appropriate
53 #define SCC_XP 0x00004000 /* XtalPU (RO), 1/0: crystal running/disabled */
54 #define SCC_CD_MASK 0xffff0000 /* ClockDivider (SlowClk = 1/(4+divisor)) */
55 #define SCC_CD_SHIFT 16
58 #define SYCC_IE 0x00000001 /* ILPen: Enable Idle Low Power */
59 #define SYCC_AE 0x00000002 /* ALPen: Enable Active Low Power */
60 #define SYCC_FP 0x00000004 /* ForcePLLOn */
61 #define SYCC_AR 0x00000008 /* Force ALP (or HT if ALPen is not set */
62 #define SYCC_HR 0x00000010 /* Force HT */
63 #define SYCC_CD_MASK 0xffff0000 /* ClkDiv (ILP = 1/(4 * (divisor + 1)) */
64 #define SYCC_CD_SHIFT 16
66 #define CST4329_SPROM_OTP_SEL_MASK 0x00000003
67 #define CST4329_DEFCIS_SEL 0 /* OTP is powered up, use def. CIS, no SPROM */
68 #define CST4329_SPROM_SEL 1 /* OTP is powered up, SPROM is present */
69 #define CST4329_OTP_SEL 2 /* OTP is powered up, no SPROM */
70 #define CST4329_OTP_PWRDN 3 /* OTP is powered down, SPROM is present */
71 #define CST4329_SPI_SDIO_MODE_MASK 0x00000004
72 #define CST4329_SPI_SDIO_MODE_SHIFT 2
74 /* 43224 chip-specific ChipControl register bits */
75 #define CCTRL43224_GPIO_TOGGLE 0x8000
76 #define CCTRL_43224A0_12MA_LED_DRIVE 0x00F000F0 /* 12 mA drive strength */
77 #define CCTRL_43224B0_12MA_LED_DRIVE 0xF0 /* 12 mA drive strength for later 43224s */
79 /* 43236 Chip specific ChipStatus register bits */
80 #define CST43236_SFLASH_MASK 0x00000040
81 #define CST43236_OTP_MASK 0x00000080
82 #define CST43236_HSIC_MASK 0x00000100 /* USB/HSIC */
83 #define CST43236_BP_CLK 0x00000200 /* 120/96Mbps */
84 #define CST43236_BOOT_MASK 0x00001800
85 #define CST43236_BOOT_SHIFT 11
86 #define CST43236_BOOT_FROM_SRAM 0 /* boot from SRAM, ARM in reset */
87 #define CST43236_BOOT_FROM_ROM 1 /* boot from ROM */
88 #define CST43236_BOOT_FROM_FLASH 2 /* boot from FLASH */
89 #define CST43236_BOOT_FROM_INVALID 3
91 /* 4331 chip-specific ChipControl register bits */
92 #define CCTRL4331_BT_COEXIST (1<<0) /* 0 disable */
93 #define CCTRL4331_SECI (1<<1) /* 0 SECI is disabled (JATG functional) */
94 #define CCTRL4331_EXT_LNA (1<<2) /* 0 disable */
95 #define CCTRL4331_SPROM_GPIO13_15 (1<<3) /* sprom/gpio13-15 mux */
96 #define CCTRL4331_EXTPA_EN (1<<4) /* 0 ext pa disable, 1 ext pa enabled */
97 #define CCTRL4331_GPIOCLK_ON_SPROMCS (1<<5) /* set drive out GPIO_CLK on sprom_cs pin */
98 #define CCTRL4331_PCIE_MDIO_ON_SPROMCS (1<<6) /* use sprom_cs pin as PCIE mdio interface */
99 #define CCTRL4331_EXTPA_ON_GPIO2_5 (1<<7) /* aband extpa will be at gpio2/5 and sprom_dout */
100 #define CCTRL4331_OVR_PIPEAUXCLKEN (1<<8) /* override core control on pipe_AuxClkEnable */
101 #define CCTRL4331_OVR_PIPEAUXPWRDOWN (1<<9) /* override core control on pipe_AuxPowerDown */
102 #define CCTRL4331_PCIE_AUXCLKEN (1<<10) /* pcie_auxclkenable */
103 #define CCTRL4331_PCIE_PIPE_PLLDOWN (1<<11) /* pcie_pipe_pllpowerdown */
104 #define CCTRL4331_BT_SHD0_ON_GPIO4 (1<<16) /* enable bt_shd0 at gpio4 */
105 #define CCTRL4331_BT_SHD1_ON_GPIO5 (1<<17) /* enable bt_shd1 at gpio5 */
107 /* 4331 Chip specific ChipStatus register bits */
108 #define CST4331_XTAL_FREQ 0x00000001 /* crystal frequency 20/40Mhz */
109 #define CST4331_SPROM_PRESENT 0x00000002
110 #define CST4331_OTP_PRESENT 0x00000004
111 #define CST4331_LDO_RF 0x00000008
112 #define CST4331_LDO_PAR 0x00000010
114 /* 4319 chip-specific ChipStatus register bits */
115 #define CST4319_SPI_CPULESSUSB 0x00000001
116 #define CST4319_SPI_CLK_POL 0x00000002
117 #define CST4319_SPI_CLK_PH 0x00000008
118 #define CST4319_SPROM_OTP_SEL_MASK 0x000000c0 /* gpio [7:6], SDIO CIS selection */
119 #define CST4319_SPROM_OTP_SEL_SHIFT 6
120 #define CST4319_DEFCIS_SEL 0x00000000 /* use default CIS, OTP is powered up */
121 #define CST4319_SPROM_SEL 0x00000040 /* use SPROM, OTP is powered up */
122 #define CST4319_OTP_SEL 0x00000080 /* use OTP, OTP is powered up */
123 #define CST4319_OTP_PWRDN 0x000000c0 /* use SPROM, OTP is powered down */
124 #define CST4319_SDIO_USB_MODE 0x00000100 /* gpio [8], sdio/usb mode */
125 #define CST4319_REMAP_SEL_MASK 0x00000600
126 #define CST4319_ILPDIV_EN 0x00000800
127 #define CST4319_XTAL_PD_POL 0x00001000
128 #define CST4319_LPO_SEL 0x00002000
129 #define CST4319_RES_INIT_MODE 0x0000c000
130 #define CST4319_PALDO_EXTPNP 0x00010000 /* PALDO is configured with external PNP */
131 #define CST4319_CBUCK_MODE_MASK 0x00060000
132 #define CST4319_CBUCK_MODE_BURST 0x00020000
133 #define CST4319_CBUCK_MODE_LPBURST 0x00060000
134 #define CST4319_RCAL_VALID 0x01000000
135 #define CST4319_RCAL_VALUE_MASK 0x3e000000
136 #define CST4319_RCAL_VALUE_SHIFT 25
138 /* 4336 chip-specific ChipStatus register bits */
139 #define CST4336_SPI_MODE_MASK 0x00000001
140 #define CST4336_SPROM_PRESENT 0x00000002
141 #define CST4336_OTP_PRESENT 0x00000004
142 #define CST4336_ARMREMAP_0 0x00000008
143 #define CST4336_ILPDIV_EN_MASK 0x00000010
144 #define CST4336_ILPDIV_EN_SHIFT 4
145 #define CST4336_XTAL_PD_POL_MASK 0x00000020
146 #define CST4336_XTAL_PD_POL_SHIFT 5
147 #define CST4336_LPO_SEL_MASK 0x00000040
148 #define CST4336_LPO_SEL_SHIFT 6
149 #define CST4336_RES_INIT_MODE_MASK 0x00000180
150 #define CST4336_RES_INIT_MODE_SHIFT 7
151 #define CST4336_CBUCK_MODE_MASK 0x00000600
152 #define CST4336_CBUCK_MODE_SHIFT 9
154 /* 4313 chip-specific ChipStatus register bits */
155 #define CST4313_SPROM_PRESENT 1
156 #define CST4313_OTP_PRESENT 2
157 #define CST4313_SPROM_OTP_SEL_MASK 0x00000002
158 #define CST4313_SPROM_OTP_SEL_SHIFT 0
160 /* 4313 Chip specific ChipControl register bits */
161 #define CCTRL_4313_12MA_LED_DRIVE 0x00000007 /* 12 mA drive strengh for later 4313 */
163 #define BCM47162_DMP() ((sih->chip == BCM47162_CHIP_ID) && \
164 (sih->chiprev == 0) && \
165 (sii->coreid[sii->curidx] == MIPS74K_CORE_ID))
170 get_erom_ent(struct si_pub *sih, u32 **eromptr, u32 mask, u32 match)
173 uint inv = 0, nom = 0;
176 ent = R_REG(*eromptr);
182 if ((ent & ER_VALID) == 0) {
187 if (ent == (ER_END | ER_VALID))
190 if ((ent & mask) == match)
196 SI_VMSG(("%s: Returning ent 0x%08x\n", __func__, ent));
198 SI_VMSG((" after %d invalid and %d non-matching entries\n",
205 get_asd(struct si_pub *sih, u32 **eromptr, uint sp, uint ad, uint st,
206 u32 *addrl, u32 *addrh, u32 *sizel, u32 *sizeh)
210 asd = get_erom_ent(sih, eromptr, ER_VALID, ER_VALID);
211 if (((asd & ER_TAG1) != ER_ADD) ||
212 (((asd & AD_SP_MASK) >> AD_SP_SHIFT) != sp) ||
213 ((asd & AD_ST_MASK) != st)) {
214 /* This is not what we want, "push" it back */
218 *addrl = asd & AD_ADDR_MASK;
220 *addrh = get_erom_ent(sih, eromptr, 0, 0);
224 sz = asd & AD_SZ_MASK;
225 if (sz == AD_SZ_SZD) {
226 szd = get_erom_ent(sih, eromptr, 0, 0);
227 *sizel = szd & SD_SZ_MASK;
229 *sizeh = get_erom_ent(sih, eromptr, 0, 0);
231 *sizel = AD_SZ_BASE << (sz >> AD_SZ_SHIFT);
233 SI_VMSG((" SP %d, ad %d: st = %d, 0x%08x_0x%08x @ 0x%08x_0x%08x\n",
234 sp, ad, st, *sizeh, *sizel, *addrh, *addrl));
239 static void ai_hwfixup(si_info_t *sii)
243 /* parse the enumeration rom to identify all cores */
244 void ai_scan(struct si_pub *sih, void *regs, uint devid)
246 si_info_t *sii = SI_INFO(sih);
247 chipcregs_t *cc = (chipcregs_t *) regs;
248 u32 erombase, *eromptr, *eromlim;
250 erombase = R_REG(&cc->eromptr);
252 switch (sih->bustype) {
254 eromptr = (u32 *) REG_MAP(erombase, SI_CORE_SIZE);
258 /* Set wrappers address */
259 sii->curwrap = (void *)((unsigned long)regs + SI_CORE_SIZE);
261 /* Now point the window at the erom */
262 pci_write_config_dword(sii->pbus, PCI_BAR0_WIN, erombase);
268 eromptr = (u32 *)(unsigned long)erombase;
272 SI_ERROR(("Don't know how to do AXI enumertion on bus %d\n",
276 eromlim = eromptr + (ER_REMAPCONTROL / sizeof(u32));
278 SI_VMSG(("ai_scan: regs = 0x%p, erombase = 0x%08x, eromptr = 0x%p, eromlim = 0x%p\n", regs, erombase, eromptr, eromlim));
279 while (eromptr < eromlim) {
280 u32 cia, cib, cid, mfg, crev, nmw, nsw, nmp, nsp;
281 u32 mpd, asd, addrl, addrh, sizel, sizeh;
288 /* Grok a component */
289 cia = get_erom_ent(sih, &eromptr, ER_TAG, ER_CI);
290 if (cia == (ER_END | ER_VALID)) {
291 SI_VMSG(("Found END of erom after %d cores\n",
297 cib = get_erom_ent(sih, &eromptr, 0, 0);
299 if ((cib & ER_TAG) != ER_CI) {
300 SI_ERROR(("CIA not followed by CIB\n"));
304 cid = (cia & CIA_CID_MASK) >> CIA_CID_SHIFT;
305 mfg = (cia & CIA_MFG_MASK) >> CIA_MFG_SHIFT;
306 crev = (cib & CIB_REV_MASK) >> CIB_REV_SHIFT;
307 nmw = (cib & CIB_NMW_MASK) >> CIB_NMW_SHIFT;
308 nsw = (cib & CIB_NSW_MASK) >> CIB_NSW_SHIFT;
309 nmp = (cib & CIB_NMP_MASK) >> CIB_NMP_SHIFT;
310 nsp = (cib & CIB_NSP_MASK) >> CIB_NSP_SHIFT;
312 SI_VMSG(("Found component 0x%04x/0x%04x rev %d at erom addr 0x%p, with nmw = %d, " "nsw = %d, nmp = %d & nsp = %d\n", mfg, cid, crev, base, nmw, nsw, nmp, nsp));
314 if (((mfg == MFGID_ARM) && (cid == DEF_AI_COMP)) || (nsp == 0))
316 if ((nmw + nsw == 0)) {
317 /* A component which is not a core */
318 if (cid == OOB_ROUTER_CORE_ID) {
319 asd = get_asd(sih, &eromptr, 0, 0, AD_ST_SLAVE,
320 &addrl, &addrh, &sizel, &sizeh);
322 sii->oob_router = addrl;
329 /* sii->eromptr[idx] = base; */
332 sii->coreid[idx] = cid;
334 for (i = 0; i < nmp; i++) {
335 mpd = get_erom_ent(sih, &eromptr, ER_VALID, ER_VALID);
336 if ((mpd & ER_TAG) != ER_MP) {
337 SI_ERROR(("Not enough MP entries for component 0x%x\n", cid));
340 SI_VMSG((" Master port %d, mp: %d id: %d\n", i,
341 (mpd & MPD_MP_MASK) >> MPD_MP_SHIFT,
342 (mpd & MPD_MUI_MASK) >> MPD_MUI_SHIFT));
345 /* First Slave Address Descriptor should be port 0:
346 * the main register space for the core
349 get_asd(sih, &eromptr, 0, 0, AD_ST_SLAVE, &addrl, &addrh,
352 /* Try again to see if it is a bridge */
354 get_asd(sih, &eromptr, 0, 0, AD_ST_BRIDGE, &addrl,
355 &addrh, &sizel, &sizeh);
358 else if ((addrh != 0) || (sizeh != 0)
359 || (sizel != SI_CORE_SIZE)) {
360 SI_ERROR(("First Slave ASD for core 0x%04x malformed " "(0x%08x)\n", cid, asd));
364 sii->coresba[idx] = addrl;
365 sii->coresba_size[idx] = sizel;
366 /* Get any more ASDs in port 0 */
370 get_asd(sih, &eromptr, 0, j, AD_ST_SLAVE, &addrl,
371 &addrh, &sizel, &sizeh);
372 if ((asd != 0) && (j == 1) && (sizel == SI_CORE_SIZE)) {
373 sii->coresba2[idx] = addrl;
374 sii->coresba2_size[idx] = sizel;
379 /* Go through the ASDs for other slave ports */
380 for (i = 1; i < nsp; i++) {
384 get_asd(sih, &eromptr, i, j++, AD_ST_SLAVE,
385 &addrl, &addrh, &sizel, &sizeh);
388 SI_ERROR((" SP %d has no address descriptors\n",
394 /* Now get master wrappers */
395 for (i = 0; i < nmw; i++) {
397 get_asd(sih, &eromptr, i, 0, AD_ST_MWRAP, &addrl,
398 &addrh, &sizel, &sizeh);
400 SI_ERROR(("Missing descriptor for MW %d\n", i));
403 if ((sizeh != 0) || (sizel != SI_CORE_SIZE)) {
404 SI_ERROR(("Master wrapper %d is not 4KB\n", i));
408 sii->wrapba[idx] = addrl;
411 /* And finally slave wrappers */
412 for (i = 0; i < nsw; i++) {
413 uint fwp = (nsp == 1) ? 0 : 1;
415 get_asd(sih, &eromptr, fwp + i, 0, AD_ST_SWRAP,
416 &addrl, &addrh, &sizel, &sizeh);
418 SI_ERROR(("Missing descriptor for SW %d\n", i));
421 if ((sizeh != 0) || (sizel != SI_CORE_SIZE)) {
422 SI_ERROR(("Slave wrapper %d is not 4KB\n", i));
425 if ((nmw == 0) && (i == 0))
426 sii->wrapba[idx] = addrl;
429 /* Don't record bridges */
437 SI_ERROR(("Reached end of erom without finding END"));
444 /* This function changes the logical "focus" to the indicated core.
445 * Return the current core's virtual address.
447 void *ai_setcoreidx(struct si_pub *sih, uint coreidx)
449 si_info_t *sii = SI_INFO(sih);
450 u32 addr = sii->coresba[coreidx];
451 u32 wrap = sii->wrapba[coreidx];
454 if (coreidx >= sii->numcores)
457 switch (sih->bustype) {
460 if (!sii->regs[coreidx]) {
461 sii->regs[coreidx] = REG_MAP(addr, SI_CORE_SIZE);
463 sii->curmap = regs = sii->regs[coreidx];
464 if (!sii->wrappers[coreidx]) {
465 sii->wrappers[coreidx] = REG_MAP(wrap, SI_CORE_SIZE);
467 sii->curwrap = sii->wrappers[coreidx];
471 /* point bar0 window */
472 pci_write_config_dword(sii->pbus, PCI_BAR0_WIN, addr);
474 /* point bar0 2nd 4KB window */
475 pci_write_config_dword(sii->pbus, PCI_BAR0_WIN2, wrap);
480 sii->curmap = regs = (void *)(unsigned long)addr;
481 sii->curwrap = (void *)(unsigned long)wrap;
490 sii->curidx = coreidx;
495 /* Return the number of address spaces in current core */
496 int ai_numaddrspaces(struct si_pub *sih)
501 /* Return the address of the nth address space in the current core */
502 u32 ai_addrspace(struct si_pub *sih, uint asidx)
511 return sii->coresba[cidx];
513 return sii->coresba2[cidx];
515 SI_ERROR(("%s: Need to parse the erom again to find addr space %d\n", __func__, asidx));
520 /* Return the size of the nth address space in the current core */
521 u32 ai_addrspacesize(struct si_pub *sih, uint asidx)
530 return sii->coresba_size[cidx];
532 return sii->coresba2_size[cidx];
534 SI_ERROR(("%s: Need to parse the erom again to find addr space %d\n", __func__, asidx));
539 uint ai_flag(struct si_pub *sih)
545 if (BCM47162_DMP()) {
546 SI_ERROR(("%s: Attempting to read MIPS DMP registers on 47162a0", __func__));
551 return R_REG(&ai->oobselouta30) & 0x1f;
554 void ai_setint(struct si_pub *sih, int siflag)
558 uint ai_corevendor(struct si_pub *sih)
564 cia = sii->cia[sii->curidx];
565 return (cia & CIA_MFG_MASK) >> CIA_MFG_SHIFT;
568 uint ai_corerev(struct si_pub *sih)
574 cib = sii->cib[sii->curidx];
575 return (cib & CIB_REV_MASK) >> CIB_REV_SHIFT;
578 bool ai_iscoreup(struct si_pub *sih)
586 return (((R_REG(&ai->ioctrl) & (SICF_FGC | SICF_CLOCK_EN)) ==
588 && ((R_REG(&ai->resetctrl) & AIRC_RESET) == 0));
591 void ai_core_cflags_wo(struct si_pub *sih, u32 mask, u32 val)
599 if (BCM47162_DMP()) {
600 SI_ERROR(("%s: Accessing MIPS DMP register (ioctrl) on 47162a0",
608 w = ((R_REG(&ai->ioctrl) & ~mask) | val);
609 W_REG(&ai->ioctrl, w);
613 u32 ai_core_cflags(struct si_pub *sih, u32 mask, u32 val)
620 if (BCM47162_DMP()) {
621 SI_ERROR(("%s: Accessing MIPS DMP register (ioctrl) on 47162a0",
629 w = ((R_REG(&ai->ioctrl) & ~mask) | val);
630 W_REG(&ai->ioctrl, w);
633 return R_REG(&ai->ioctrl);
636 u32 ai_core_sflags(struct si_pub *sih, u32 mask, u32 val)
643 if (BCM47162_DMP()) {
644 SI_ERROR(("%s: Accessing MIPS DMP register (iostatus) on 47162a0", __func__));
651 w = ((R_REG(&ai->iostatus) & ~mask) | val);
652 W_REG(&ai->iostatus, w);
655 return R_REG(&ai->iostatus);
658 /* *************** from siutils.c ************** */
659 /* local prototypes */
660 static si_info_t *ai_doattach(si_info_t *sii, uint devid, void *regs,
661 uint bustype, void *sdh, char **vars,
663 static bool ai_buscore_prep(si_info_t *sii, uint bustype, uint devid,
665 static bool ai_buscore_setup(si_info_t *sii, chipcregs_t *cc, uint bustype,
666 u32 savewin, uint *origidx, void *regs);
667 static void ai_nvram_process(si_info_t *sii, char *pvars);
669 /* dev path concatenation util */
670 static char *ai_devpathvar(struct si_pub *sih, char *var, int len,
672 static bool _ai_clkctl_cc(si_info_t *sii, uint mode);
673 static bool ai_ispcie(si_info_t *sii);
675 /* global variable to indicate reservation/release of gpio's */
676 static u32 ai_gpioreservation;
679 * Allocate a si handle.
680 * devid - pci device id (used to determine chip#)
681 * osh - opaque OS handle
682 * regs - virtual address of initial core registers
683 * bustype - pci/sb/sdio/etc
684 * vars - pointer to a pointer area for "environment" variables
685 * varsz - pointer to int to return the size of the vars
687 struct si_pub *ai_attach(uint devid, void *regs, uint bustype,
688 void *sdh, char **vars, uint *varsz)
692 /* alloc si_info_t */
693 sii = kmalloc(sizeof(si_info_t), GFP_ATOMIC);
695 SI_ERROR(("si_attach: malloc failed!\n"));
699 if (ai_doattach(sii, devid, regs, bustype, sdh, vars, varsz) ==
704 sii->vars = vars ? *vars : NULL;
705 sii->varsz = varsz ? *varsz : 0;
707 return (struct si_pub *) sii;
710 /* global kernel resource */
711 static si_info_t ksii;
713 static bool ai_buscore_prep(si_info_t *sii, uint bustype, uint devid,
716 /* kludge to enable the clock on the 4306 which lacks a slowclock */
717 if (bustype == PCI_BUS && !ai_ispcie(sii))
718 ai_clkctl_xtal(&sii->pub, XTAL | PLL, ON);
722 static bool ai_buscore_setup(si_info_t *sii, chipcregs_t *cc, uint bustype,
723 u32 savewin, uint *origidx, void *regs)
727 uint pciidx, pcieidx, pcirev, pcierev;
729 cc = ai_setcoreidx(&sii->pub, SI_CC_IDX);
731 /* get chipcommon rev */
732 sii->pub.ccrev = (int)ai_corerev(&sii->pub);
734 /* get chipcommon chipstatus */
735 if (sii->pub.ccrev >= 11)
736 sii->pub.chipst = R_REG(&cc->chipstatus);
738 /* get chipcommon capabilites */
739 sii->pub.cccaps = R_REG(&cc->capabilities);
740 /* get chipcommon extended capabilities */
742 if (sii->pub.ccrev >= 35)
743 sii->pub.cccaps_ext = R_REG(&cc->capabilities_ext);
745 /* get pmu rev and caps */
746 if (sii->pub.cccaps & CC_CAP_PMU) {
747 sii->pub.pmucaps = R_REG(&cc->pmucapabilities);
748 sii->pub.pmurev = sii->pub.pmucaps & PCAP_REV_MASK;
751 /* figure out bus/orignal core idx */
752 sii->pub.buscoretype = NODEV_CORE_ID;
753 sii->pub.buscorerev = NOREV;
754 sii->pub.buscoreidx = BADIDX;
757 pcirev = pcierev = NOREV;
758 pciidx = pcieidx = BADIDX;
760 for (i = 0; i < sii->numcores; i++) {
763 ai_setcoreidx(&sii->pub, i);
764 cid = ai_coreid(&sii->pub);
765 crev = ai_corerev(&sii->pub);
767 /* Display cores found */
768 SI_VMSG(("CORE[%d]: id 0x%x rev %d base 0x%x regs 0x%p\n",
769 i, cid, crev, sii->coresba[i], sii->regs[i]));
771 if (bustype == PCI_BUS) {
772 if (cid == PCI_CORE_ID) {
776 } else if (cid == PCIE_CORE_ID) {
783 /* find the core idx before entering this func. */
784 if ((savewin && (savewin == sii->coresba[i])) ||
785 (regs == sii->regs[i]))
796 sii->pub.buscoretype = PCI_CORE_ID;
797 sii->pub.buscorerev = pcirev;
798 sii->pub.buscoreidx = pciidx;
800 sii->pub.buscoretype = PCIE_CORE_ID;
801 sii->pub.buscorerev = pcierev;
802 sii->pub.buscoreidx = pcieidx;
805 SI_VMSG(("Buscore id/type/rev %d/0x%x/%d\n", sii->pub.buscoreidx,
806 sii->pub.buscoretype, sii->pub.buscorerev));
808 /* fixup necessary chip/core configurations */
809 if (sii->pub.bustype == PCI_BUS) {
812 sii->pch = (void *)pcicore_init(
813 &sii->pub, sii->pbus,
814 (void *)PCIEREGS(sii));
815 if (sii->pch == NULL)
819 if (ai_pci_fixcfg(&sii->pub)) {
820 SI_ERROR(("si_doattach: si_pci_fixcfg failed\n"));
825 /* return to the original core */
826 ai_setcoreidx(&sii->pub, *origidx);
831 static __used void ai_nvram_process(si_info_t *sii, char *pvars)
835 /* get boardtype and boardrev */
836 switch (sii->pub.bustype) {
838 /* do a pci config read to get subsystem id and subvendor id */
839 pci_read_config_dword(sii->pbus, PCI_SUBSYSTEM_VENDOR_ID, &w);
840 /* Let nvram variables override subsystem Vend/ID */
841 sii->pub.boardvendor = (u16)ai_getdevpathintvar(&sii->pub,
843 if (sii->pub.boardvendor == 0)
844 sii->pub.boardvendor = w & 0xffff;
846 SI_ERROR(("Overriding boardvendor: 0x%x instead of "
847 "0x%x\n", sii->pub.boardvendor, w & 0xffff));
848 sii->pub.boardtype = (u16)ai_getdevpathintvar(&sii->pub,
850 if (sii->pub.boardtype == 0)
851 sii->pub.boardtype = (w >> 16) & 0xffff;
853 SI_ERROR(("Overriding boardtype: 0x%x instead of 0x%x\n"
854 , sii->pub.boardtype, (w >> 16) & 0xffff));
857 sii->pub.boardvendor = getintvar(pvars, "manfid");
858 sii->pub.boardtype = getintvar(pvars, "prodid");
863 sii->pub.boardvendor = PCI_VENDOR_ID_BROADCOM;
864 sii->pub.boardtype = getintvar(pvars, "prodid");
865 if (pvars == NULL || (sii->pub.boardtype == 0)) {
866 sii->pub.boardtype = getintvar(NULL, "boardtype");
867 if (sii->pub.boardtype == 0)
868 sii->pub.boardtype = 0xffff;
873 if (sii->pub.boardtype == 0) {
874 SI_ERROR(("si_doattach: unknown board type\n"));
877 sii->pub.boardflags = getintvar(pvars, "boardflags");
880 static si_info_t *ai_doattach(si_info_t *sii, uint devid,
881 void *regs, uint bustype, void *pbus,
882 char **vars, uint *varsz)
884 struct si_pub *sih = &sii->pub;
891 memset((unsigned char *) sii, 0, sizeof(si_info_t));
895 sih->buscoreidx = BADIDX;
900 /* check to see if we are a si core mimic'ing a pci core */
901 if (bustype == PCI_BUS) {
902 pci_read_config_dword(sii->pbus, PCI_SPROM_CONTROL, &w);
903 if (w == 0xffffffff) {
904 SI_ERROR(("%s: incoming bus is PCI but it's a lie, "
905 " switching to SI devid:0x%x\n",
911 /* find Chipcommon address */
912 if (bustype == PCI_BUS) {
913 pci_read_config_dword(sii->pbus, PCI_BAR0_WIN, &savewin);
914 if (!GOODCOREADDR(savewin, SI_ENUM_BASE))
915 savewin = SI_ENUM_BASE;
916 pci_write_config_dword(sii->pbus, PCI_BAR0_WIN,
918 cc = (chipcregs_t *) regs;
920 cc = (chipcregs_t *) REG_MAP(SI_ENUM_BASE, SI_CORE_SIZE);
923 sih->bustype = bustype;
925 /* bus/core/clk setup for register access */
926 if (!ai_buscore_prep(sii, bustype, devid, pbus)) {
927 SI_ERROR(("si_doattach: si_core_clk_prep failed %d\n",
933 * ChipID recognition.
934 * We assume we can read chipid at offset 0 from the regs arg.
935 * If we add other chiptypes (or if we need to support old sdio
936 * hosts w/o chipcommon), some way of recognizing them needs to
939 w = R_REG(&cc->chipid);
940 socitype = (w & CID_TYPE_MASK) >> CID_TYPE_SHIFT;
941 /* Might as wll fill in chip id rev & pkg */
942 sih->chip = w & CID_ID_MASK;
943 sih->chiprev = (w & CID_REV_MASK) >> CID_REV_SHIFT;
944 sih->chippkg = (w & CID_PKG_MASK) >> CID_PKG_SHIFT;
946 sih->issim = IS_SIM(sih->chippkg);
949 if (socitype == SOCI_AI) {
950 SI_MSG(("Found chip type AI (0x%08x)\n", w));
951 /* pass chipc address instead of original core base */
952 ai_scan(&sii->pub, (void *)cc, devid);
954 SI_ERROR(("Found chip of unknown type (0x%08x)\n", w));
957 /* no cores found, bail out */
958 if (sii->numcores == 0) {
959 SI_ERROR(("si_doattach: could not find any cores\n"));
962 /* bus/core/clk setup */
964 if (!ai_buscore_setup(sii, cc, bustype, savewin, &origidx, regs)) {
965 SI_ERROR(("si_doattach: si_buscore_setup failed\n"));
969 /* assume current core is CC */
970 if ((sii->pub.ccrev == 0x25)
972 ((sih->chip == BCM43236_CHIP_ID
973 || sih->chip == BCM43235_CHIP_ID
974 || sih->chip == BCM43238_CHIP_ID)
975 && (sii->pub.chiprev <= 2))) {
977 if ((cc->chipstatus & CST43236_BP_CLK) != 0) {
979 clkdiv = R_REG(&cc->clkdiv);
980 /* otp_clk_div is even number, 120/14 < 9mhz */
981 clkdiv = (clkdiv & ~CLKD_OTP) | (14 << CLKD_OTP_SHIFT);
982 W_REG(&cc->clkdiv, clkdiv);
983 SI_ERROR(("%s: set clkdiv to %x\n", __func__, clkdiv));
988 /* Init nvram from flash if it exists */
991 /* Init nvram from sprom/otp if they exist */
993 (&sii->pub, bustype, regs, vars, varsz)) {
994 SI_ERROR(("si_doattach: srom_var_init failed: bad srom\n"));
997 pvars = vars ? *vars : NULL;
998 ai_nvram_process(sii, pvars);
1000 /* === NVRAM, clock is ready === */
1001 cc = (chipcregs_t *) ai_setcore(sih, CC_CORE_ID, 0);
1002 W_REG(&cc->gpiopullup, 0);
1003 W_REG(&cc->gpiopulldown, 0);
1004 ai_setcoreidx(sih, origidx);
1006 /* PMU specific initializations */
1007 if (PMUCTL_ENAB(sih)) {
1010 si_pmu_chip_init(sih);
1011 xtalfreq = getintvar(pvars, "xtalfreq");
1012 /* If xtalfreq var not available, try to measure it */
1014 xtalfreq = si_pmu_measure_alpclk(sih);
1015 si_pmu_pll_init(sih, xtalfreq);
1016 si_pmu_res_init(sih);
1017 si_pmu_swreg_init(sih);
1020 /* setup the GPIO based LED powersave register */
1021 w = getintvar(pvars, "leddc");
1023 w = DEFAULT_GPIOTIMERVAL;
1024 ai_corereg(sih, SI_CC_IDX, offsetof(chipcregs_t, gpiotimerval), ~0, w);
1027 pcicore_attach(sii->pch, pvars, SI_DOATTACH);
1030 if ((sih->chip == BCM43224_CHIP_ID) ||
1031 (sih->chip == BCM43421_CHIP_ID)) {
1033 * enable 12 mA drive strenth for 43224 and
1034 * set chipControl register bit 15
1036 if (sih->chiprev == 0) {
1037 SI_MSG(("Applying 43224A0 WARs\n"));
1038 ai_corereg(sih, SI_CC_IDX,
1039 offsetof(chipcregs_t, chipcontrol),
1040 CCTRL43224_GPIO_TOGGLE,
1041 CCTRL43224_GPIO_TOGGLE);
1042 si_pmu_chipcontrol(sih, 0, CCTRL_43224A0_12MA_LED_DRIVE,
1043 CCTRL_43224A0_12MA_LED_DRIVE);
1045 if (sih->chiprev >= 1) {
1046 SI_MSG(("Applying 43224B0+ WARs\n"));
1047 si_pmu_chipcontrol(sih, 0, CCTRL_43224B0_12MA_LED_DRIVE,
1048 CCTRL_43224B0_12MA_LED_DRIVE);
1052 if (sih->chip == BCM4313_CHIP_ID) {
1054 * enable 12 mA drive strenth for 4313 and
1055 * set chipControl register bit 1
1057 SI_MSG(("Applying 4313 WARs\n"));
1058 si_pmu_chipcontrol(sih, 0, CCTRL_4313_12MA_LED_DRIVE,
1059 CCTRL_4313_12MA_LED_DRIVE);
1062 if (sih->chip == BCM4331_CHIP_ID) {
1063 /* Enable Ext PA lines depending on chip package option */
1064 ai_chipcontrl_epa4331(sih, true);
1069 if (sih->bustype == PCI_BUS) {
1071 pcicore_deinit(sii->pch);
1078 /* may be called with core in reset */
1079 void ai_detach(struct si_pub *sih)
1084 struct si_pub *si_local = NULL;
1085 memcpy(&si_local, &sih, sizeof(struct si_pub **));
1092 if (sih->bustype == SI_BUS)
1093 for (idx = 0; idx < SI_MAXCORES; idx++)
1094 if (sii->regs[idx]) {
1095 iounmap(sii->regs[idx]);
1096 sii->regs[idx] = NULL;
1099 nvram_exit(); /* free up nvram buffers */
1101 if (sih->bustype == PCI_BUS) {
1103 pcicore_deinit(sii->pch);
1111 /* register driver interrupt disabling and restoring callback functions */
1113 ai_register_intr_callback(struct si_pub *sih, void *intrsoff_fn,
1114 void *intrsrestore_fn,
1115 void *intrsenabled_fn, void *intr_arg)
1120 sii->intr_arg = intr_arg;
1121 sii->intrsoff_fn = (si_intrsoff_t) intrsoff_fn;
1122 sii->intrsrestore_fn = (si_intrsrestore_t) intrsrestore_fn;
1123 sii->intrsenabled_fn = (si_intrsenabled_t) intrsenabled_fn;
1124 /* save current core id. when this function called, the current core
1125 * must be the core which provides driver functions(il, et, wl, etc.)
1127 sii->dev_coreid = sii->coreid[sii->curidx];
1130 void ai_deregister_intr_callback(struct si_pub *sih)
1135 sii->intrsoff_fn = NULL;
1138 uint ai_coreid(struct si_pub *sih)
1143 return sii->coreid[sii->curidx];
1146 uint ai_coreidx(struct si_pub *sih)
1154 bool ai_backplane64(struct si_pub *sih)
1156 return (sih->cccaps & CC_CAP_BKPLN64) != 0;
1159 /* return index of coreid or BADIDX if not found */
1160 uint ai_findcoreidx(struct si_pub *sih, uint coreid, uint coreunit)
1170 for (i = 0; i < sii->numcores; i++)
1171 if (sii->coreid[i] == coreid) {
1172 if (found == coreunit)
1181 * This function changes logical "focus" to the indicated core;
1182 * must be called with interrupts off.
1183 * Moreover, callers should keep interrupts off during switching
1184 * out of and back to d11 core.
1186 void *ai_setcore(struct si_pub *sih, uint coreid, uint coreunit)
1190 idx = ai_findcoreidx(sih, coreid, coreunit);
1194 return ai_setcoreidx(sih, idx);
1197 /* Turn off interrupt as required by ai_setcore, before switch core */
1198 void *ai_switch_core(struct si_pub *sih, uint coreid, uint *origidx,
1207 /* Overloading the origidx variable to remember the coreid,
1208 * this works because the core ids cannot be confused with
1212 if (coreid == CC_CORE_ID)
1213 return (void *)CCREGS_FAST(sii);
1214 else if (coreid == sih->buscoretype)
1215 return (void *)PCIEREGS(sii);
1217 INTR_OFF(sii, *intr_val);
1218 *origidx = sii->curidx;
1219 cc = ai_setcore(sih, coreid, 0);
1223 /* restore coreidx and restore interrupt */
1224 void ai_restore_core(struct si_pub *sih, uint coreid, uint intr_val)
1230 && ((coreid == CC_CORE_ID) || (coreid == sih->buscoretype)))
1233 ai_setcoreidx(sih, coreid);
1234 INTR_RESTORE(sii, intr_val);
1237 void ai_write_wrapperreg(struct si_pub *sih, u32 offset, u32 val)
1239 si_info_t *sii = SI_INFO(sih);
1240 u32 *w = (u32 *) sii->curwrap;
1241 W_REG(w + (offset / 4), val);
1246 * Switch to 'coreidx', issue a single arbitrary 32bit register mask&set
1247 * operation, switch back to the original core, and return the new value.
1249 * When using the silicon backplane, no fiddling with interrupts or core
1250 * switches is needed.
1252 * Also, when using pci/pcie, we can optimize away the core switching for pci
1253 * registers and (on newer pci cores) chipcommon registers.
1255 uint ai_corereg(struct si_pub *sih, uint coreidx, uint regoff, uint mask,
1267 if (coreidx >= SI_MAXCORES)
1270 if (sih->bustype == SI_BUS) {
1271 /* If internal bus, we can always get at everything */
1273 /* map if does not exist */
1274 if (!sii->regs[coreidx]) {
1275 sii->regs[coreidx] = REG_MAP(sii->coresba[coreidx],
1278 r = (u32 *) ((unsigned char *) sii->regs[coreidx] + regoff);
1279 } else if (sih->bustype == PCI_BUS) {
1281 * If pci/pcie, we can get at pci/pcie regs
1282 * and on newer cores to chipc
1284 if ((sii->coreid[coreidx] == CC_CORE_ID) && SI_FAST(sii)) {
1285 /* Chipc registers are mapped at 12KB */
1288 r = (u32 *) ((char *)sii->curmap +
1289 PCI_16KB0_CCREGS_OFFSET + regoff);
1290 } else if (sii->pub.buscoreidx == coreidx) {
1292 * pci registers are at either in the last 2KB of
1293 * an 8KB window or, in pcie and pci rev 13 at 8KB
1297 r = (u32 *) ((char *)sii->curmap +
1298 PCI_16KB0_PCIREGS_OFFSET +
1301 r = (u32 *) ((char *)sii->curmap +
1302 ((regoff >= SBCONFIGOFF) ?
1303 PCI_BAR0_PCISBR_OFFSET :
1304 PCI_BAR0_PCIREGS_OFFSET) +
1310 INTR_OFF(sii, intr_val);
1312 /* save current core index */
1313 origidx = ai_coreidx(&sii->pub);
1316 r = (u32 *) ((unsigned char *) ai_setcoreidx(&sii->pub, coreidx)
1322 w = (R_REG(r) & ~mask) | val;
1330 /* restore core index */
1331 if (origidx != coreidx)
1332 ai_setcoreidx(&sii->pub, origidx);
1334 INTR_RESTORE(sii, intr_val);
1340 void ai_core_disable(struct si_pub *sih, u32 bits)
1350 /* if core is already in reset, just return */
1351 if (R_REG(&ai->resetctrl) & AIRC_RESET)
1354 W_REG(&ai->ioctrl, bits);
1355 dummy = R_REG(&ai->ioctrl);
1358 W_REG(&ai->resetctrl, AIRC_RESET);
1362 /* reset and re-enable a core
1364 * bits - core specific bits that are set during and after reset sequence
1365 * resetbits - core specific bits that are set only during reset sequence
1367 void ai_core_reset(struct si_pub *sih, u32 bits, u32 resetbits)
1377 * Must do the disable sequence first to work
1378 * for arbitrary current core state.
1380 ai_core_disable(sih, (bits | resetbits));
1383 * Now do the initialization sequence.
1385 W_REG(&ai->ioctrl, (bits | SICF_FGC | SICF_CLOCK_EN));
1386 dummy = R_REG(&ai->ioctrl);
1387 W_REG(&ai->resetctrl, 0);
1390 W_REG(&ai->ioctrl, (bits | SICF_CLOCK_EN));
1391 dummy = R_REG(&ai->ioctrl);
1395 /* return the slow clock source - LPO, XTAL, or PCI */
1396 static uint ai_slowclk_src(si_info_t *sii)
1401 if (sii->pub.ccrev < 6) {
1402 if (sii->pub.bustype == PCI_BUS) {
1403 pci_read_config_dword(sii->pbus, PCI_GPIO_OUT,
1405 if (val & PCI_CFG_GPIO_SCS)
1409 } else if (sii->pub.ccrev < 10) {
1410 cc = (chipcregs_t *) ai_setcoreidx(&sii->pub, sii->curidx);
1411 return R_REG(&cc->slow_clk_ctl) & SCC_SS_MASK;
1412 } else /* Insta-clock */
1417 * return the ILP (slowclock) min or max frequency
1418 * precondition: we've established the chip has dynamic clk control
1420 static uint ai_slowclk_freq(si_info_t *sii, bool max_freq, chipcregs_t *cc)
1425 slowclk = ai_slowclk_src(sii);
1426 if (sii->pub.ccrev < 6) {
1427 if (slowclk == SCC_SS_PCI)
1428 return max_freq ? (PCIMAXFREQ / 64)
1429 : (PCIMINFREQ / 64);
1431 return max_freq ? (XTALMAXFREQ / 32)
1432 : (XTALMINFREQ / 32);
1433 } else if (sii->pub.ccrev < 10) {
1435 (((R_REG(&cc->slow_clk_ctl) & SCC_CD_MASK) >>
1437 if (slowclk == SCC_SS_LPO)
1438 return max_freq ? LPOMAXFREQ : LPOMINFREQ;
1439 else if (slowclk == SCC_SS_XTAL)
1440 return max_freq ? (XTALMAXFREQ / div)
1441 : (XTALMINFREQ / div);
1442 else if (slowclk == SCC_SS_PCI)
1443 return max_freq ? (PCIMAXFREQ / div)
1444 : (PCIMINFREQ / div);
1446 /* Chipc rev 10 is InstaClock */
1447 div = R_REG(&cc->system_clk_ctl) >> SYCC_CD_SHIFT;
1448 div = 4 * (div + 1);
1449 return max_freq ? XTALMAXFREQ : (XTALMINFREQ / div);
1454 static void ai_clkctl_setdelay(si_info_t *sii, void *chipcregs)
1456 chipcregs_t *cc = (chipcregs_t *) chipcregs;
1457 uint slowmaxfreq, pll_delay, slowclk;
1458 uint pll_on_delay, fref_sel_delay;
1460 pll_delay = PLL_DELAY;
1463 * If the slow clock is not sourced by the xtal then
1464 * add the xtal_on_delay since the xtal will also be
1465 * powered down by dynamic clk control logic.
1468 slowclk = ai_slowclk_src(sii);
1469 if (slowclk != SCC_SS_XTAL)
1470 pll_delay += XTAL_ON_DELAY;
1472 /* Starting with 4318 it is ILP that is used for the delays */
1474 ai_slowclk_freq(sii, (sii->pub.ccrev >= 10) ? false : true, cc);
1476 pll_on_delay = ((slowmaxfreq * pll_delay) + 999999) / 1000000;
1477 fref_sel_delay = ((slowmaxfreq * FREF_DELAY) + 999999) / 1000000;
1479 W_REG(&cc->pll_on_delay, pll_on_delay);
1480 W_REG(&cc->fref_sel_delay, fref_sel_delay);
1483 /* initialize power control delay registers */
1484 void ai_clkctl_init(struct si_pub *sih)
1491 if (!CCCTL_ENAB(sih))
1495 fast = SI_FAST(sii);
1497 origidx = sii->curidx;
1498 cc = (chipcregs_t *) ai_setcore(sih, CC_CORE_ID, 0);
1502 cc = (chipcregs_t *) CCREGS_FAST(sii);
1507 /* set all Instaclk chip ILP to 1 MHz */
1508 if (sih->ccrev >= 10)
1509 SET_REG(&cc->system_clk_ctl, SYCC_CD_MASK,
1510 (ILP_DIV_1MHZ << SYCC_CD_SHIFT));
1512 ai_clkctl_setdelay(sii, (void *)cc);
1515 ai_setcoreidx(sih, origidx);
1519 * return the value suitable for writing to the
1520 * dot11 core FAST_PWRUP_DELAY register
1522 u16 ai_clkctl_fast_pwrup_delay(struct si_pub *sih)
1533 if (PMUCTL_ENAB(sih)) {
1534 INTR_OFF(sii, intr_val);
1535 fpdelay = si_pmu_fast_pwrup_delay(sih);
1536 INTR_RESTORE(sii, intr_val);
1540 if (!CCCTL_ENAB(sih))
1543 fast = SI_FAST(sii);
1546 origidx = sii->curidx;
1547 INTR_OFF(sii, intr_val);
1548 cc = (chipcregs_t *) ai_setcore(sih, CC_CORE_ID, 0);
1552 cc = (chipcregs_t *) CCREGS_FAST(sii);
1557 slowminfreq = ai_slowclk_freq(sii, false, cc);
1558 fpdelay = (((R_REG(&cc->pll_on_delay) + 2) * 1000000) +
1559 (slowminfreq - 1)) / slowminfreq;
1563 ai_setcoreidx(sih, origidx);
1564 INTR_RESTORE(sii, intr_val);
1569 /* turn primary xtal and/or pll off/on */
1570 int ai_clkctl_xtal(struct si_pub *sih, uint what, bool on)
1577 switch (sih->bustype) {
1580 /* pcie core doesn't have any mapping to control the xtal pu */
1584 pci_read_config_dword(sii->pbus, PCI_GPIO_IN, &in);
1585 pci_read_config_dword(sii->pbus, PCI_GPIO_OUT, &out);
1586 pci_read_config_dword(sii->pbus, PCI_GPIO_OUTEN, &outen);
1589 * Avoid glitching the clock if GPRS is already using it.
1590 * We can't actually read the state of the PLLPD so we infer it
1591 * by the value of XTAL_PU which *is* readable via gpioin.
1593 if (on && (in & PCI_CFG_GPIO_XTAL))
1597 outen |= PCI_CFG_GPIO_XTAL;
1599 outen |= PCI_CFG_GPIO_PLL;
1602 /* turn primary xtal on */
1604 out |= PCI_CFG_GPIO_XTAL;
1606 out |= PCI_CFG_GPIO_PLL;
1607 pci_write_config_dword(sii->pbus,
1609 pci_write_config_dword(sii->pbus,
1610 PCI_GPIO_OUTEN, outen);
1611 udelay(XTAL_ON_DELAY);
1616 out &= ~PCI_CFG_GPIO_PLL;
1617 pci_write_config_dword(sii->pbus,
1623 out &= ~PCI_CFG_GPIO_XTAL;
1625 out |= PCI_CFG_GPIO_PLL;
1626 pci_write_config_dword(sii->pbus,
1628 pci_write_config_dword(sii->pbus,
1629 PCI_GPIO_OUTEN, outen);
1640 * clock control policy function throught chipcommon
1642 * set dynamic clk control mode (forceslow, forcefast, dynamic)
1643 * returns true if we are forcing fast clock
1644 * this is a wrapper over the next internal function
1645 * to allow flexible policy settings for outside caller
1647 bool ai_clkctl_cc(struct si_pub *sih, uint mode)
1653 /* chipcommon cores prior to rev6 don't support dynamic clock control */
1657 if (PCI_FORCEHT(sii))
1658 return mode == CLK_FAST;
1660 return _ai_clkctl_cc(sii, mode);
1663 /* clk control mechanism through chipcommon, no policy checking */
1664 static bool _ai_clkctl_cc(si_info_t *sii, uint mode)
1670 bool fast = SI_FAST(sii);
1672 /* chipcommon cores prior to rev6 don't support dynamic clock control */
1673 if (sii->pub.ccrev < 6)
1677 INTR_OFF(sii, intr_val);
1678 origidx = sii->curidx;
1680 if ((sii->pub.bustype == SI_BUS) &&
1681 ai_setcore(&sii->pub, MIPS33_CORE_ID, 0) &&
1682 (ai_corerev(&sii->pub) <= 7) && (sii->pub.ccrev >= 10))
1685 cc = (chipcregs_t *) ai_setcore(&sii->pub, CC_CORE_ID, 0);
1687 cc = (chipcregs_t *) CCREGS_FAST(sii);
1692 if (!CCCTL_ENAB(&sii->pub) && (sii->pub.ccrev < 20))
1696 case CLK_FAST: /* FORCEHT, fast (pll) clock */
1697 if (sii->pub.ccrev < 10) {
1699 * don't forget to force xtal back
1700 * on before we clear SCC_DYN_XTAL..
1702 ai_clkctl_xtal(&sii->pub, XTAL, ON);
1703 SET_REG(&cc->slow_clk_ctl,
1704 (SCC_XC | SCC_FS | SCC_IP), SCC_IP);
1705 } else if (sii->pub.ccrev < 20) {
1706 OR_REG(&cc->system_clk_ctl, SYCC_HR);
1708 OR_REG(&cc->clk_ctl_st, CCS_FORCEHT);
1711 /* wait for the PLL */
1712 if (PMUCTL_ENAB(&sii->pub)) {
1713 u32 htavail = CCS_HTAVAIL;
1714 SPINWAIT(((R_REG(&cc->clk_ctl_st) & htavail)
1715 == 0), PMU_MAX_TRANSITION_DLY);
1721 case CLK_DYNAMIC: /* enable dynamic clock control */
1722 if (sii->pub.ccrev < 10) {
1723 scc = R_REG(&cc->slow_clk_ctl);
1724 scc &= ~(SCC_FS | SCC_IP | SCC_XC);
1725 if ((scc & SCC_SS_MASK) != SCC_SS_XTAL)
1727 W_REG(&cc->slow_clk_ctl, scc);
1730 * for dynamic control, we have to
1731 * release our xtal_pu "force on"
1734 ai_clkctl_xtal(&sii->pub, XTAL, OFF);
1735 } else if (sii->pub.ccrev < 20) {
1737 AND_REG(&cc->system_clk_ctl, ~SYCC_HR);
1739 AND_REG(&cc->clk_ctl_st, ~CCS_FORCEHT);
1749 ai_setcoreidx(&sii->pub, origidx);
1750 INTR_RESTORE(sii, intr_val);
1752 return mode == CLK_FAST;
1755 /* Build device path. Support SI, PCI, and JTAG for now. */
1756 int ai_devpath(struct si_pub *sih, char *path, int size)
1760 if (!path || size <= 0)
1763 switch (sih->bustype) {
1766 slen = snprintf(path, (size_t) size, "sb/%u/", ai_coreidx(sih));
1769 slen = snprintf(path, (size_t) size, "pci/%u/%u/",
1770 ((struct pci_dev *)((SI_INFO(sih))->pbus))->bus->number,
1772 ((struct pci_dev *)((SI_INFO(sih))->pbus))->devfn));
1780 if (slen < 0 || slen >= size) {
1788 /* Get a variable, but only if it has a devpath prefix */
1789 char *ai_getdevpathvar(struct si_pub *sih, const char *name)
1791 char varname[SI_DEVPATH_BUFSZ + 32];
1793 ai_devpathvar(sih, varname, sizeof(varname), name);
1795 return getvar(NULL, varname);
1798 /* Get a variable, but only if it has a devpath prefix */
1799 int ai_getdevpathintvar(struct si_pub *sih, const char *name)
1801 #if defined(BCMBUSTYPE) && (BCMBUSTYPE == SI_BUS)
1802 return getintvar(NULL, name);
1804 char varname[SI_DEVPATH_BUFSZ + 32];
1806 ai_devpathvar(sih, varname, sizeof(varname), name);
1808 return getintvar(NULL, varname);
1812 char *ai_getnvramflvar(struct si_pub *sih, const char *name)
1814 return getvar(NULL, name);
1817 /* Concatenate the dev path with a varname into the given 'var' buffer
1818 * and return the 'var' pointer. Nothing is done to the arguments if
1819 * len == 0 or var is NULL, var is still returned. On overflow, the
1820 * first char will be set to '\0'.
1822 static char *ai_devpathvar(struct si_pub *sih, char *var, int len,
1827 if (!var || len <= 0)
1830 if (ai_devpath(sih, var, len) == 0) {
1831 path_len = strlen(var);
1833 if (strlen(name) + 1 > (uint) (len - path_len))
1836 strncpy(var + path_len, name, len - path_len - 1);
1842 /* return true if PCIE capability exists in the pci config space */
1843 static __used bool ai_ispcie(si_info_t *sii)
1847 if (sii->pub.bustype != PCI_BUS)
1851 pcicore_find_pci_capability(sii->pbus, PCI_CAP_ID_EXP, NULL,
1859 bool ai_pci_war16165(struct si_pub *sih)
1865 return PCI(sii) && (sih->buscorerev <= 10);
1868 void ai_pci_up(struct si_pub *sih)
1874 /* if not pci bus, we're done */
1875 if (sih->bustype != PCI_BUS)
1878 if (PCI_FORCEHT(sii))
1879 _ai_clkctl_cc(sii, CLK_FAST);
1882 pcicore_up(sii->pch, SI_PCIUP);
1886 /* Unconfigure and/or apply various WARs when system is going to sleep mode */
1887 void ai_pci_sleep(struct si_pub *sih)
1893 pcicore_sleep(sii->pch);
1896 /* Unconfigure and/or apply various WARs when going down */
1897 void ai_pci_down(struct si_pub *sih)
1903 /* if not pci bus, we're done */
1904 if (sih->bustype != PCI_BUS)
1907 /* release FORCEHT since chip is going to "down" state */
1908 if (PCI_FORCEHT(sii))
1909 _ai_clkctl_cc(sii, CLK_DYNAMIC);
1911 pcicore_down(sii->pch, SI_PCIDOWN);
1915 * Configure the pci core for pci client (NIC) action
1916 * coremask is the bitvec of cores by index to be enabled.
1918 void ai_pci_setup(struct si_pub *sih, uint coremask)
1927 if (sii->pub.bustype != PCI_BUS)
1931 /* get current core index */
1934 /* we interrupt on this backplane flag number */
1935 siflag = ai_flag(sih);
1937 /* switch over to pci core */
1938 regs = ai_setcoreidx(sih, sii->pub.buscoreidx);
1942 * Enable sb->pci interrupts. Assume
1943 * PCI rev 2.3 support was added in pci core rev 6 and things changed..
1945 if (PCIE(sii) || (PCI(sii) && ((sii->pub.buscorerev) >= 6))) {
1946 /* pci config write to set this core bit in PCIIntMask */
1947 pci_read_config_dword(sii->pbus, PCI_INT_MASK, &w);
1948 w |= (coremask << PCI_SBIM_SHIFT);
1949 pci_write_config_dword(sii->pbus, PCI_INT_MASK, w);
1951 /* set sbintvec bit for our flag number */
1952 ai_setint(sih, siflag);
1956 pcicore_pci_setup(sii->pch, regs);
1958 /* switch back to previous core */
1959 ai_setcoreidx(sih, idx);
1964 * Fixup SROMless PCI device's configuration.
1965 * The current core may be changed upon return.
1967 int ai_pci_fixcfg(struct si_pub *sih)
1972 si_info_t *sii = SI_INFO(sih);
1974 /* Fixup PI in SROM shadow area to enable the correct PCI core access */
1975 /* save the current index */
1976 origidx = ai_coreidx(&sii->pub);
1978 /* check 'pi' is correct and fix it if not */
1979 regs = ai_setcore(&sii->pub, sii->pub.buscoretype, 0);
1980 pcicore_fixcfg(sii->pch, regs);
1982 /* restore the original index */
1983 ai_setcoreidx(&sii->pub, origidx);
1985 pcicore_hwup(sii->pch);
1989 /* mask&set gpiocontrol bits */
1990 u32 ai_gpiocontrol(struct si_pub *sih, u32 mask, u32 val, u8 priority)
1996 /* gpios could be shared on router platforms
1997 * ignore reservation if it's high priority (e.g., test apps)
1999 if ((priority != GPIO_HI_PRIORITY) &&
2000 (sih->bustype == SI_BUS) && (val || mask)) {
2001 mask = priority ? (ai_gpioreservation & mask) :
2002 ((ai_gpioreservation | mask) & ~(ai_gpioreservation));
2006 regoff = offsetof(chipcregs_t, gpiocontrol);
2007 return ai_corereg(sih, SI_CC_IDX, regoff, mask, val);
2010 void ai_chipcontrl_epa4331(struct si_pub *sih, bool on)
2018 origidx = ai_coreidx(sih);
2020 cc = (chipcregs_t *) ai_setcore(sih, CC_CORE_ID, 0);
2022 val = R_REG(&cc->chipcontrol);
2025 if (sih->chippkg == 9 || sih->chippkg == 0xb) {
2026 /* Ext PA Controls for 4331 12x9 Package */
2027 W_REG(&cc->chipcontrol, val |
2028 (CCTRL4331_EXTPA_EN |
2029 CCTRL4331_EXTPA_ON_GPIO2_5));
2031 /* Ext PA Controls for 4331 12x12 Package */
2032 W_REG(&cc->chipcontrol,
2033 val | (CCTRL4331_EXTPA_EN));
2036 val &= ~(CCTRL4331_EXTPA_EN | CCTRL4331_EXTPA_ON_GPIO2_5);
2037 W_REG(&cc->chipcontrol, val);
2040 ai_setcoreidx(sih, origidx);
2043 /* Enable BT-COEX & Ex-PA for 4313 */
2044 void ai_epa_4313war(struct si_pub *sih)
2051 origidx = ai_coreidx(sih);
2053 cc = (chipcregs_t *) ai_setcore(sih, CC_CORE_ID, 0);
2056 W_REG(&cc->gpiocontrol,
2057 R_REG(&cc->gpiocontrol) | GPIO_CTRL_EPA_EN_MASK);
2059 ai_setcoreidx(sih, origidx);
2062 /* check if the device is removed */
2063 bool ai_deviceremoved(struct si_pub *sih)
2070 switch (sih->bustype) {
2072 pci_read_config_dword(sii->pbus, PCI_VENDOR_ID, &w);
2073 if ((w & 0xFFFF) != PCI_VENDOR_ID_BROADCOM)
2080 bool ai_is_sprom_available(struct si_pub *sih)
2082 if (sih->ccrev >= 31) {
2088 if ((sih->cccaps & CC_CAP_SROM) == 0)
2092 origidx = sii->curidx;
2093 cc = ai_setcoreidx(sih, SI_CC_IDX);
2094 sromctrl = R_REG(&cc->sromcontrol);
2095 ai_setcoreidx(sih, origidx);
2096 return sromctrl & SRC_PRESENT;
2099 switch (sih->chip) {
2100 case BCM4329_CHIP_ID:
2101 return (sih->chipst & CST4329_SPROM_SEL) != 0;
2102 case BCM4319_CHIP_ID:
2103 return (sih->chipst & CST4319_SPROM_SEL) != 0;
2104 case BCM4336_CHIP_ID:
2105 return (sih->chipst & CST4336_SPROM_PRESENT) != 0;
2106 case BCM4330_CHIP_ID:
2107 return (sih->chipst & CST4330_SPROM_PRESENT) != 0;
2108 case BCM4313_CHIP_ID:
2109 return (sih->chipst & CST4313_SPROM_PRESENT) != 0;
2110 case BCM4331_CHIP_ID:
2111 return (sih->chipst & CST4331_SPROM_PRESENT) != 0;
2117 bool ai_is_otp_disabled(struct si_pub *sih)
2119 switch (sih->chip) {
2120 case BCM4329_CHIP_ID:
2121 return (sih->chipst & CST4329_SPROM_OTP_SEL_MASK) ==
2123 case BCM4319_CHIP_ID:
2124 return (sih->chipst & CST4319_SPROM_OTP_SEL_MASK) ==
2126 case BCM4336_CHIP_ID:
2127 return (sih->chipst & CST4336_OTP_PRESENT) == 0;
2128 case BCM4330_CHIP_ID:
2129 return (sih->chipst & CST4330_OTP_PRESENT) == 0;
2130 case BCM4313_CHIP_ID:
2131 return (sih->chipst & CST4313_OTP_PRESENT) == 0;
2132 /* These chips always have their OTP on */
2133 case BCM43224_CHIP_ID:
2134 case BCM43225_CHIP_ID:
2135 case BCM43421_CHIP_ID:
2136 case BCM43235_CHIP_ID:
2137 case BCM43236_CHIP_ID:
2138 case BCM43238_CHIP_ID:
2139 case BCM4331_CHIP_ID:
2145 bool ai_is_otp_powered(struct si_pub *sih)
2147 if (PMUCTL_ENAB(sih))
2148 return si_pmu_is_otp_powered(sih);
2152 void ai_otp_power(struct si_pub *sih, bool on)
2154 if (PMUCTL_ENAB(sih))
2155 si_pmu_otp_power(sih, on);