2 * Broadcom specific AMBA
5 * Copyright 2005, 2011, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
7 * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de>
9 * Licensed under the GNU/GPL. See COPYING for details.
12 #include "bcma_private.h"
13 #include <linux/export.h>
14 #include <linux/bcma/bcma.h>
16 /**************************************************
18 **************************************************/
20 u32
bcma_pcie_read(struct bcma_drv_pci
*pc
, u32 address
)
22 pcicore_write32(pc
, BCMA_CORE_PCI_PCIEIND_ADDR
, address
);
23 pcicore_read32(pc
, BCMA_CORE_PCI_PCIEIND_ADDR
);
24 return pcicore_read32(pc
, BCMA_CORE_PCI_PCIEIND_DATA
);
27 static void bcma_pcie_write(struct bcma_drv_pci
*pc
, u32 address
, u32 data
)
29 pcicore_write32(pc
, BCMA_CORE_PCI_PCIEIND_ADDR
, address
);
30 pcicore_read32(pc
, BCMA_CORE_PCI_PCIEIND_ADDR
);
31 pcicore_write32(pc
, BCMA_CORE_PCI_PCIEIND_DATA
, data
);
34 static void bcma_pcie_mdio_set_phy(struct bcma_drv_pci
*pc
, u16 phy
)
39 v
= BCMA_CORE_PCI_MDIODATA_START
;
40 v
|= BCMA_CORE_PCI_MDIODATA_WRITE
;
41 v
|= (BCMA_CORE_PCI_MDIODATA_DEV_ADDR
<<
42 BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF
);
43 v
|= (BCMA_CORE_PCI_MDIODATA_BLK_ADDR
<<
44 BCMA_CORE_PCI_MDIODATA_REGADDR_SHF
);
45 v
|= BCMA_CORE_PCI_MDIODATA_TA
;
47 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_DATA
, v
);
50 for (i
= 0; i
< 200; i
++) {
51 v
= pcicore_read32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
);
52 if (v
& BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE
)
54 usleep_range(1000, 2000);
58 static u16
bcma_pcie_mdio_read(struct bcma_drv_pci
*pc
, u16 device
, u8 address
)
65 /* enable mdio access to SERDES */
66 v
= BCMA_CORE_PCI_MDIOCTL_PREAM_EN
;
67 v
|= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL
;
68 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
, v
);
70 if (pc
->core
->id
.rev
>= 10) {
72 bcma_pcie_mdio_set_phy(pc
, device
);
73 v
= (BCMA_CORE_PCI_MDIODATA_DEV_ADDR
<<
74 BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF
);
75 v
|= (address
<< BCMA_CORE_PCI_MDIODATA_REGADDR_SHF
);
77 v
= (device
<< BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD
);
78 v
|= (address
<< BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD
);
81 v
= BCMA_CORE_PCI_MDIODATA_START
;
82 v
|= BCMA_CORE_PCI_MDIODATA_READ
;
83 v
|= BCMA_CORE_PCI_MDIODATA_TA
;
85 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_DATA
, v
);
86 /* Wait for the device to complete the transaction */
88 for (i
= 0; i
< max_retries
; i
++) {
89 v
= pcicore_read32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
);
90 if (v
& BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE
) {
92 ret
= pcicore_read32(pc
, BCMA_CORE_PCI_MDIO_DATA
);
95 usleep_range(1000, 2000);
97 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
, 0);
101 static void bcma_pcie_mdio_write(struct bcma_drv_pci
*pc
, u16 device
,
102 u8 address
, u16 data
)
104 int max_retries
= 10;
108 /* enable mdio access to SERDES */
109 v
= BCMA_CORE_PCI_MDIOCTL_PREAM_EN
;
110 v
|= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL
;
111 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
, v
);
113 if (pc
->core
->id
.rev
>= 10) {
115 bcma_pcie_mdio_set_phy(pc
, device
);
116 v
= (BCMA_CORE_PCI_MDIODATA_DEV_ADDR
<<
117 BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF
);
118 v
|= (address
<< BCMA_CORE_PCI_MDIODATA_REGADDR_SHF
);
120 v
= (device
<< BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD
);
121 v
|= (address
<< BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD
);
124 v
= BCMA_CORE_PCI_MDIODATA_START
;
125 v
|= BCMA_CORE_PCI_MDIODATA_WRITE
;
126 v
|= BCMA_CORE_PCI_MDIODATA_TA
;
128 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_DATA
, v
);
129 /* Wait for the device to complete the transaction */
131 for (i
= 0; i
< max_retries
; i
++) {
132 v
= pcicore_read32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
);
133 if (v
& BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE
)
135 usleep_range(1000, 2000);
137 pcicore_write32(pc
, BCMA_CORE_PCI_MDIO_CONTROL
, 0);
140 static u16
bcma_pcie_mdio_writeread(struct bcma_drv_pci
*pc
, u16 device
,
141 u8 address
, u16 data
)
143 bcma_pcie_mdio_write(pc
, device
, address
, data
);
144 return bcma_pcie_mdio_read(pc
, device
, address
);
147 /**************************************************
149 **************************************************/
151 static u8
bcma_pcicore_polarity_workaround(struct bcma_drv_pci
*pc
)
155 tmp
= bcma_pcie_read(pc
, BCMA_CORE_PCI_PLP_STATUSREG
);
156 if (tmp
& BCMA_CORE_PCI_PLP_POLARITYINV_STAT
)
157 return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE
|
158 BCMA_CORE_PCI_SERDES_RX_CTRL_POLARITY
;
160 return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE
;
163 static void bcma_pcicore_serdes_workaround(struct bcma_drv_pci
*pc
)
167 bcma_pcie_mdio_write(pc
, BCMA_CORE_PCI_MDIODATA_DEV_RX
,
168 BCMA_CORE_PCI_SERDES_RX_CTRL
,
169 bcma_pcicore_polarity_workaround(pc
));
170 tmp
= bcma_pcie_mdio_read(pc
, BCMA_CORE_PCI_MDIODATA_DEV_PLL
,
171 BCMA_CORE_PCI_SERDES_PLL_CTRL
);
172 if (tmp
& BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN
)
173 bcma_pcie_mdio_write(pc
, BCMA_CORE_PCI_MDIODATA_DEV_PLL
,
174 BCMA_CORE_PCI_SERDES_PLL_CTRL
,
175 tmp
& ~BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN
);
178 static void bcma_core_pci_fixcfg(struct bcma_drv_pci
*pc
)
180 struct bcma_device
*core
= pc
->core
;
181 u16 val16
, core_index
;
184 regoff
= BCMA_CORE_PCI_SPROM(BCMA_CORE_PCI_SPROM_PI_OFFSET
);
185 core_index
= (u16
)core
->core_index
;
187 val16
= pcicore_read16(pc
, regoff
);
188 if (((val16
& BCMA_CORE_PCI_SPROM_PI_MASK
) >> BCMA_CORE_PCI_SPROM_PI_SHIFT
)
190 val16
= (core_index
<< BCMA_CORE_PCI_SPROM_PI_SHIFT
) |
191 (val16
& ~BCMA_CORE_PCI_SPROM_PI_MASK
);
192 pcicore_write16(pc
, regoff
, val16
);
196 /* Fix MISC config to allow coming out of L2/L3-Ready state w/o PRST */
197 /* Needs to happen when coming out of 'standby'/'hibernate' */
198 static void bcma_core_pci_config_fixup(struct bcma_drv_pci
*pc
)
203 regoff
= BCMA_CORE_PCI_SPROM(BCMA_CORE_PCI_SPROM_MISC_CONFIG
);
205 val16
= pcicore_read16(pc
, regoff
);
207 if (!(val16
& BCMA_CORE_PCI_SPROM_L23READY_EXIT_NOPERST
)) {
208 val16
|= BCMA_CORE_PCI_SPROM_L23READY_EXIT_NOPERST
;
209 pcicore_write16(pc
, regoff
, val16
);
213 /**************************************************
215 **************************************************/
217 static void bcma_core_pci_clientmode_init(struct bcma_drv_pci
*pc
)
219 bcma_core_pci_fixcfg(pc
);
220 bcma_pcicore_serdes_workaround(pc
);
221 bcma_core_pci_config_fixup(pc
);
224 void bcma_core_pci_init(struct bcma_drv_pci
*pc
)
229 #ifdef CONFIG_BCMA_DRIVER_PCI_HOSTMODE
230 pc
->hostmode
= bcma_core_pci_is_in_hostmode(pc
);
232 bcma_core_pci_hostmode_init(pc
);
233 #endif /* CONFIG_BCMA_DRIVER_PCI_HOSTMODE */
236 bcma_core_pci_clientmode_init(pc
);
239 void bcma_core_pci_power_save(struct bcma_bus
*bus
, bool up
)
241 struct bcma_drv_pci
*pc
;
244 if (bus
->hosttype
!= BCMA_HOSTTYPE_PCI
)
247 pc
= &bus
->drv_pci
[0];
249 if (pc
->core
->id
.rev
>= 15 && pc
->core
->id
.rev
<= 20) {
250 data
= up
? 0x74 : 0x7C;
251 bcma_pcie_mdio_writeread(pc
, BCMA_CORE_PCI_MDIO_BLK1
,
252 BCMA_CORE_PCI_MDIO_BLK1_MGMT1
, 0x7F64);
253 bcma_pcie_mdio_writeread(pc
, BCMA_CORE_PCI_MDIO_BLK1
,
254 BCMA_CORE_PCI_MDIO_BLK1_MGMT3
, data
);
255 } else if (pc
->core
->id
.rev
>= 21 && pc
->core
->id
.rev
<= 22) {
256 data
= up
? 0x75 : 0x7D;
257 bcma_pcie_mdio_writeread(pc
, BCMA_CORE_PCI_MDIO_BLK1
,
258 BCMA_CORE_PCI_MDIO_BLK1_MGMT1
, 0x7E65);
259 bcma_pcie_mdio_writeread(pc
, BCMA_CORE_PCI_MDIO_BLK1
,
260 BCMA_CORE_PCI_MDIO_BLK1_MGMT3
, data
);
263 EXPORT_SYMBOL_GPL(bcma_core_pci_power_save
);
265 int bcma_core_pci_irq_ctl(struct bcma_drv_pci
*pc
, struct bcma_device
*core
,
268 struct pci_dev
*pdev
;
272 if (!pc
|| core
->bus
->hosttype
!= BCMA_HOSTTYPE_PCI
) {
273 /* This bcma device is not on a PCI host-bus. So the IRQs are
274 * not routed through the PCI core.
275 * So we must not enable routing through the PCI core. */
279 pdev
= pc
->core
->bus
->host_pci
;
281 err
= pci_read_config_dword(pdev
, BCMA_PCI_IRQMASK
, &tmp
);
285 coremask
= BIT(core
->core_index
) << 8;
291 err
= pci_write_config_dword(pdev
, BCMA_PCI_IRQMASK
, tmp
);
296 EXPORT_SYMBOL_GPL(bcma_core_pci_irq_ctl
);
298 static void bcma_core_pci_extend_L1timer(struct bcma_drv_pci
*pc
, bool extend
)
302 w
= bcma_pcie_read(pc
, BCMA_CORE_PCI_DLLP_PMTHRESHREG
);
304 w
|= BCMA_CORE_PCI_ASPMTIMER_EXTEND
;
306 w
&= ~BCMA_CORE_PCI_ASPMTIMER_EXTEND
;
307 bcma_pcie_write(pc
, BCMA_CORE_PCI_DLLP_PMTHRESHREG
, w
);
308 bcma_pcie_read(pc
, BCMA_CORE_PCI_DLLP_PMTHRESHREG
);
311 void bcma_core_pci_up(struct bcma_bus
*bus
)
313 struct bcma_drv_pci
*pc
;
315 if (bus
->hosttype
!= BCMA_HOSTTYPE_PCI
)
318 pc
= &bus
->drv_pci
[0];
320 bcma_core_pci_extend_L1timer(pc
, true);
322 EXPORT_SYMBOL_GPL(bcma_core_pci_up
);
324 void bcma_core_pci_down(struct bcma_bus
*bus
)
326 struct bcma_drv_pci
*pc
;
328 if (bus
->hosttype
!= BCMA_HOSTTYPE_PCI
)
331 pc
= &bus
->drv_pci
[0];
333 bcma_core_pci_extend_L1timer(pc
, false);
335 EXPORT_SYMBOL_GPL(bcma_core_pci_down
);