18369ae33SRafał Miłecki /* 28369ae33SRafał Miłecki * Broadcom specific AMBA 38369ae33SRafał Miłecki * PCI Core 48369ae33SRafał Miłecki * 549dc9577SHauke Mehrtens * Copyright 2005, 2011, Broadcom Corporation 6eb032b98SMichael Büsch * Copyright 2006, 2007, Michael Buesch <m@bues.ch> 72be25cacSHauke Mehrtens * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de> 88369ae33SRafał Miłecki * 98369ae33SRafał Miłecki * Licensed under the GNU/GPL. See COPYING for details. 108369ae33SRafał Miłecki */ 118369ae33SRafał Miłecki 128369ae33SRafał Miłecki #include "bcma_private.h" 1344a8e377SPaul Gortmaker #include <linux/export.h> 148369ae33SRafał Miłecki #include <linux/bcma/bcma.h> 158369ae33SRafał Miłecki 168369ae33SRafał Miłecki /************************************************** 178369ae33SRafał Miłecki * R/W ops. 188369ae33SRafał Miłecki **************************************************/ 198369ae33SRafał Miłecki 204b259a5cSHauke Mehrtens u32 bcma_pcie_read(struct bcma_drv_pci *pc, u32 address) 218369ae33SRafał Miłecki { 222be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_ADDR, address); 232be25cacSHauke Mehrtens pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_ADDR); 242be25cacSHauke Mehrtens return pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_DATA); 258369ae33SRafał Miłecki } 268369ae33SRafał Miłecki 278369ae33SRafał Miłecki static void bcma_pcie_write(struct bcma_drv_pci *pc, u32 address, u32 data) 288369ae33SRafał Miłecki { 292be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_ADDR, address); 302be25cacSHauke Mehrtens pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_ADDR); 312be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_DATA, data); 328369ae33SRafał Miłecki } 338369ae33SRafał Miłecki 348369ae33SRafał Miłecki static void bcma_pcie_mdio_set_phy(struct bcma_drv_pci *pc, u8 phy) 358369ae33SRafał Miłecki { 368369ae33SRafał Miłecki u32 v; 378369ae33SRafał Miłecki int i; 388369ae33SRafał Miłecki 392be25cacSHauke Mehrtens v = BCMA_CORE_PCI_MDIODATA_START; 402be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIODATA_WRITE; 412be25cacSHauke Mehrtens v |= (BCMA_CORE_PCI_MDIODATA_DEV_ADDR << 422be25cacSHauke Mehrtens BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF); 432be25cacSHauke Mehrtens v |= (BCMA_CORE_PCI_MDIODATA_BLK_ADDR << 442be25cacSHauke Mehrtens BCMA_CORE_PCI_MDIODATA_REGADDR_SHF); 452be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIODATA_TA; 468369ae33SRafał Miłecki v |= (phy << 4); 472be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v); 488369ae33SRafał Miłecki 498369ae33SRafał Miłecki udelay(10); 508369ae33SRafał Miłecki for (i = 0; i < 200; i++) { 512be25cacSHauke Mehrtens v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL); 522be25cacSHauke Mehrtens if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE) 538369ae33SRafał Miłecki break; 548369ae33SRafał Miłecki msleep(1); 558369ae33SRafał Miłecki } 568369ae33SRafał Miłecki } 578369ae33SRafał Miłecki 588369ae33SRafał Miłecki static u16 bcma_pcie_mdio_read(struct bcma_drv_pci *pc, u8 device, u8 address) 598369ae33SRafał Miłecki { 608369ae33SRafał Miłecki int max_retries = 10; 618369ae33SRafał Miłecki u16 ret = 0; 628369ae33SRafał Miłecki u32 v; 638369ae33SRafał Miłecki int i; 648369ae33SRafał Miłecki 652be25cacSHauke Mehrtens /* enable mdio access to SERDES */ 662be25cacSHauke Mehrtens v = BCMA_CORE_PCI_MDIOCTL_PREAM_EN; 672be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL; 682be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, v); 698369ae33SRafał Miłecki 708369ae33SRafał Miłecki if (pc->core->id.rev >= 10) { 718369ae33SRafał Miłecki max_retries = 200; 728369ae33SRafał Miłecki bcma_pcie_mdio_set_phy(pc, device); 732be25cacSHauke Mehrtens v = (BCMA_CORE_PCI_MDIODATA_DEV_ADDR << 742be25cacSHauke Mehrtens BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF); 752be25cacSHauke Mehrtens v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF); 762be25cacSHauke Mehrtens } else { 772be25cacSHauke Mehrtens v = (device << BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD); 782be25cacSHauke Mehrtens v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD); 798369ae33SRafał Miłecki } 808369ae33SRafał Miłecki 812be25cacSHauke Mehrtens v = BCMA_CORE_PCI_MDIODATA_START; 822be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIODATA_READ; 832be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIODATA_TA; 842be25cacSHauke Mehrtens 852be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v); 868369ae33SRafał Miłecki /* Wait for the device to complete the transaction */ 878369ae33SRafał Miłecki udelay(10); 88f1a9c1e6SRafał Miłecki for (i = 0; i < max_retries; i++) { 892be25cacSHauke Mehrtens v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL); 902be25cacSHauke Mehrtens if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE) { 918369ae33SRafał Miłecki udelay(10); 922be25cacSHauke Mehrtens ret = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_DATA); 938369ae33SRafał Miłecki break; 948369ae33SRafał Miłecki } 958369ae33SRafał Miłecki msleep(1); 968369ae33SRafał Miłecki } 972be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, 0); 988369ae33SRafał Miłecki return ret; 998369ae33SRafał Miłecki } 1008369ae33SRafał Miłecki 1018369ae33SRafał Miłecki static void bcma_pcie_mdio_write(struct bcma_drv_pci *pc, u8 device, 1028369ae33SRafał Miłecki u8 address, u16 data) 1038369ae33SRafał Miłecki { 1048369ae33SRafał Miłecki int max_retries = 10; 1058369ae33SRafał Miłecki u32 v; 1068369ae33SRafał Miłecki int i; 1078369ae33SRafał Miłecki 1082be25cacSHauke Mehrtens /* enable mdio access to SERDES */ 1092be25cacSHauke Mehrtens v = BCMA_CORE_PCI_MDIOCTL_PREAM_EN; 1102be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL; 1112be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, v); 1128369ae33SRafał Miłecki 1138369ae33SRafał Miłecki if (pc->core->id.rev >= 10) { 1148369ae33SRafał Miłecki max_retries = 200; 1158369ae33SRafał Miłecki bcma_pcie_mdio_set_phy(pc, device); 1162be25cacSHauke Mehrtens v = (BCMA_CORE_PCI_MDIODATA_DEV_ADDR << 1172be25cacSHauke Mehrtens BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF); 1182be25cacSHauke Mehrtens v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF); 1192be25cacSHauke Mehrtens } else { 1202be25cacSHauke Mehrtens v = (device << BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD); 1212be25cacSHauke Mehrtens v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD); 1228369ae33SRafał Miłecki } 1238369ae33SRafał Miłecki 1242be25cacSHauke Mehrtens v = BCMA_CORE_PCI_MDIODATA_START; 1252be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIODATA_WRITE; 1262be25cacSHauke Mehrtens v |= BCMA_CORE_PCI_MDIODATA_TA; 1278369ae33SRafał Miłecki v |= data; 1282be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v); 1298369ae33SRafał Miłecki /* Wait for the device to complete the transaction */ 1308369ae33SRafał Miłecki udelay(10); 1318369ae33SRafał Miłecki for (i = 0; i < max_retries; i++) { 1322be25cacSHauke Mehrtens v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL); 1332be25cacSHauke Mehrtens if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE) 1348369ae33SRafał Miłecki break; 1358369ae33SRafał Miłecki msleep(1); 1368369ae33SRafał Miłecki } 1372be25cacSHauke Mehrtens pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, 0); 1388369ae33SRafał Miłecki } 1398369ae33SRafał Miłecki 1408369ae33SRafał Miłecki /************************************************** 1418369ae33SRafał Miłecki * Workarounds. 1428369ae33SRafał Miłecki **************************************************/ 1438369ae33SRafał Miłecki 1448369ae33SRafał Miłecki static u8 bcma_pcicore_polarity_workaround(struct bcma_drv_pci *pc) 1458369ae33SRafał Miłecki { 1462be25cacSHauke Mehrtens u32 tmp; 1472be25cacSHauke Mehrtens 1482be25cacSHauke Mehrtens tmp = bcma_pcie_read(pc, BCMA_CORE_PCI_PLP_STATUSREG); 1492be25cacSHauke Mehrtens if (tmp & BCMA_CORE_PCI_PLP_POLARITYINV_STAT) 1502be25cacSHauke Mehrtens return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE | 1512be25cacSHauke Mehrtens BCMA_CORE_PCI_SERDES_RX_CTRL_POLARITY; 1522be25cacSHauke Mehrtens else 1532be25cacSHauke Mehrtens return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE; 1548369ae33SRafał Miłecki } 1558369ae33SRafał Miłecki 1568369ae33SRafał Miłecki static void bcma_pcicore_serdes_workaround(struct bcma_drv_pci *pc) 1578369ae33SRafał Miłecki { 1588369ae33SRafał Miłecki u16 tmp; 1598369ae33SRafał Miłecki 1602be25cacSHauke Mehrtens bcma_pcie_mdio_write(pc, BCMA_CORE_PCI_MDIODATA_DEV_RX, 1612be25cacSHauke Mehrtens BCMA_CORE_PCI_SERDES_RX_CTRL, 1628369ae33SRafał Miłecki bcma_pcicore_polarity_workaround(pc)); 1632be25cacSHauke Mehrtens tmp = bcma_pcie_mdio_read(pc, BCMA_CORE_PCI_MDIODATA_DEV_PLL, 1642be25cacSHauke Mehrtens BCMA_CORE_PCI_SERDES_PLL_CTRL); 1652be25cacSHauke Mehrtens if (tmp & BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN) 1662be25cacSHauke Mehrtens bcma_pcie_mdio_write(pc, BCMA_CORE_PCI_MDIODATA_DEV_PLL, 1672be25cacSHauke Mehrtens BCMA_CORE_PCI_SERDES_PLL_CTRL, 1682be25cacSHauke Mehrtens tmp & ~BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN); 1698369ae33SRafał Miłecki } 1708369ae33SRafał Miłecki 171ec00f373SHauke Mehrtens static void bcma_core_pci_fixcfg(struct bcma_drv_pci *pc) 172ec00f373SHauke Mehrtens { 173ec00f373SHauke Mehrtens struct bcma_device *core = pc->core; 174ec00f373SHauke Mehrtens u16 val16, core_index; 175ec00f373SHauke Mehrtens uint regoff; 176ec00f373SHauke Mehrtens 177ec00f373SHauke Mehrtens regoff = BCMA_CORE_PCI_SPROM(BCMA_CORE_PCI_SPROM_PI_OFFSET); 178ec00f373SHauke Mehrtens core_index = (u16)core->core_index; 179ec00f373SHauke Mehrtens 180ec00f373SHauke Mehrtens val16 = pcicore_read16(pc, regoff); 181ec00f373SHauke Mehrtens if (((val16 & BCMA_CORE_PCI_SPROM_PI_MASK) >> BCMA_CORE_PCI_SPROM_PI_SHIFT) 182ec00f373SHauke Mehrtens != core_index) { 183ec00f373SHauke Mehrtens val16 = (core_index << BCMA_CORE_PCI_SPROM_PI_SHIFT) | 184ec00f373SHauke Mehrtens (val16 & ~BCMA_CORE_PCI_SPROM_PI_MASK); 185ec00f373SHauke Mehrtens pcicore_write16(pc, regoff, val16); 186ec00f373SHauke Mehrtens } 187ec00f373SHauke Mehrtens } 188ec00f373SHauke Mehrtens 189*2b2715b8SHauke Mehrtens /* Fix MISC config to allow coming out of L2/L3-Ready state w/o PRST */ 190*2b2715b8SHauke Mehrtens /* Needs to happen when coming out of 'standby'/'hibernate' */ 191*2b2715b8SHauke Mehrtens static void bcma_core_pci_config_fixup(struct bcma_drv_pci *pc) 192*2b2715b8SHauke Mehrtens { 193*2b2715b8SHauke Mehrtens u16 val16; 194*2b2715b8SHauke Mehrtens uint regoff; 195*2b2715b8SHauke Mehrtens 196*2b2715b8SHauke Mehrtens regoff = BCMA_CORE_PCI_SPROM(BCMA_CORE_PCI_SPROM_MISC_CONFIG); 197*2b2715b8SHauke Mehrtens 198*2b2715b8SHauke Mehrtens val16 = pcicore_read16(pc, regoff); 199*2b2715b8SHauke Mehrtens 200*2b2715b8SHauke Mehrtens if (!(val16 & BCMA_CORE_PCI_SPROM_L23READY_EXIT_NOPERST)) { 201*2b2715b8SHauke Mehrtens val16 |= BCMA_CORE_PCI_SPROM_L23READY_EXIT_NOPERST; 202*2b2715b8SHauke Mehrtens pcicore_write16(pc, regoff, val16); 203*2b2715b8SHauke Mehrtens } 204*2b2715b8SHauke Mehrtens } 205*2b2715b8SHauke Mehrtens 2068369ae33SRafał Miłecki /************************************************** 2078369ae33SRafał Miłecki * Init. 2088369ae33SRafał Miłecki **************************************************/ 2098369ae33SRafał Miłecki 210d1a7a8e1SHauke Mehrtens static void __devinit bcma_core_pci_clientmode_init(struct bcma_drv_pci *pc) 2118369ae33SRafał Miłecki { 212ec00f373SHauke Mehrtens bcma_core_pci_fixcfg(pc); 2138369ae33SRafał Miłecki bcma_pcicore_serdes_workaround(pc); 214*2b2715b8SHauke Mehrtens bcma_core_pci_config_fixup(pc); 2158369ae33SRafał Miłecki } 2161de520f4SRafał Miłecki 217d1a7a8e1SHauke Mehrtens void __devinit bcma_core_pci_init(struct bcma_drv_pci *pc) 2189352f69cSRafał Miłecki { 219517f43e5SHauke Mehrtens if (pc->setup_done) 220517f43e5SHauke Mehrtens return; 221517f43e5SHauke Mehrtens 2229352f69cSRafał Miłecki #ifdef CONFIG_BCMA_DRIVER_PCI_HOSTMODE 22349dc9577SHauke Mehrtens pc->hostmode = bcma_core_pci_is_in_hostmode(pc); 22449dc9577SHauke Mehrtens if (pc->hostmode) 2259352f69cSRafał Miłecki bcma_core_pci_hostmode_init(pc); 2269352f69cSRafał Miłecki #endif /* CONFIG_BCMA_DRIVER_PCI_HOSTMODE */ 227517f43e5SHauke Mehrtens 22849dc9577SHauke Mehrtens if (!pc->hostmode) 22949dc9577SHauke Mehrtens bcma_core_pci_clientmode_init(pc); 2309352f69cSRafał Miłecki } 2319352f69cSRafał Miłecki 2321de520f4SRafał Miłecki int bcma_core_pci_irq_ctl(struct bcma_drv_pci *pc, struct bcma_device *core, 2331de520f4SRafał Miłecki bool enable) 2341de520f4SRafał Miłecki { 2351de520f4SRafał Miłecki struct pci_dev *pdev = pc->core->bus->host_pci; 2361de520f4SRafał Miłecki u32 coremask, tmp; 237ecd177c2SHauke Mehrtens int err = 0; 238ecd177c2SHauke Mehrtens 239ecd177c2SHauke Mehrtens if (core->bus->hosttype != BCMA_HOSTTYPE_PCI) { 240ecd177c2SHauke Mehrtens /* This bcma device is not on a PCI host-bus. So the IRQs are 241ecd177c2SHauke Mehrtens * not routed through the PCI core. 242ecd177c2SHauke Mehrtens * So we must not enable routing through the PCI core. */ 243ecd177c2SHauke Mehrtens goto out; 244ecd177c2SHauke Mehrtens } 2451de520f4SRafał Miłecki 2461de520f4SRafał Miłecki err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp); 2471de520f4SRafał Miłecki if (err) 2481de520f4SRafał Miłecki goto out; 2491de520f4SRafał Miłecki 2501de520f4SRafał Miłecki coremask = BIT(core->core_index) << 8; 2511de520f4SRafał Miłecki if (enable) 2521de520f4SRafał Miłecki tmp |= coremask; 2531de520f4SRafał Miłecki else 2541de520f4SRafał Miłecki tmp &= ~coremask; 2551de520f4SRafał Miłecki 2561de520f4SRafał Miłecki err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp); 2571de520f4SRafał Miłecki 2581de520f4SRafał Miłecki out: 2591de520f4SRafał Miłecki return err; 2601de520f4SRafał Miłecki } 261440ca98fSRafał Miłecki EXPORT_SYMBOL_GPL(bcma_core_pci_irq_ctl); 26229f6b3d8SHauke Mehrtens 26329f6b3d8SHauke Mehrtens void bcma_core_pci_extend_L1timer(struct bcma_drv_pci *pc, bool extend) 26429f6b3d8SHauke Mehrtens { 26529f6b3d8SHauke Mehrtens u32 w; 26629f6b3d8SHauke Mehrtens 26729f6b3d8SHauke Mehrtens w = bcma_pcie_read(pc, BCMA_CORE_PCI_DLLP_PMTHRESHREG); 26829f6b3d8SHauke Mehrtens if (extend) 26929f6b3d8SHauke Mehrtens w |= BCMA_CORE_PCI_ASPMTIMER_EXTEND; 27029f6b3d8SHauke Mehrtens else 27129f6b3d8SHauke Mehrtens w &= ~BCMA_CORE_PCI_ASPMTIMER_EXTEND; 27229f6b3d8SHauke Mehrtens bcma_pcie_write(pc, BCMA_CORE_PCI_DLLP_PMTHRESHREG, w); 27329f6b3d8SHauke Mehrtens bcma_pcie_read(pc, BCMA_CORE_PCI_DLLP_PMTHRESHREG); 27429f6b3d8SHauke Mehrtens } 27529f6b3d8SHauke Mehrtens EXPORT_SYMBOL_GPL(bcma_core_pci_extend_L1timer); 276