blob: 22dc9fca6dcb7aa5e3e7e4a784c05efc4f958165 [file] [log] [blame]
Rafał Miłecki8369ae32011-05-09 18:56:46 +02001/*
2 * Broadcom specific AMBA
3 * PCI Core
4 *
5 * Copyright 2005, Broadcom Corporation
Michael Büscheb032b92011-07-04 20:50:05 +02006 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +01007 * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de>
Rafał Miłecki8369ae32011-05-09 18:56:46 +02008 *
9 * Licensed under the GNU/GPL. See COPYING for details.
10 */
11
12#include "bcma_private.h"
Paul Gortmaker44a8e372011-07-27 21:21:04 -040013#include <linux/export.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020014#include <linux/bcma/bcma.h>
15
16/**************************************************
17 * R/W ops.
18 **************************************************/
19
Hauke Mehrtens4b259a52012-01-31 00:03:33 +010020u32 bcma_pcie_read(struct bcma_drv_pci *pc, u32 address)
Rafał Miłecki8369ae32011-05-09 18:56:46 +020021{
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010022 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);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020025}
26
27#if 0
28static void bcma_pcie_write(struct bcma_drv_pci *pc, u32 address, u32 data)
29{
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010030 pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_ADDR, address);
31 pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_ADDR);
32 pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_DATA, data);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020033}
34#endif
35
36static void bcma_pcie_mdio_set_phy(struct bcma_drv_pci *pc, u8 phy)
37{
Rafał Miłecki8369ae32011-05-09 18:56:46 +020038 u32 v;
39 int i;
40
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010041 v = BCMA_CORE_PCI_MDIODATA_START;
42 v |= BCMA_CORE_PCI_MDIODATA_WRITE;
43 v |= (BCMA_CORE_PCI_MDIODATA_DEV_ADDR <<
44 BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF);
45 v |= (BCMA_CORE_PCI_MDIODATA_BLK_ADDR <<
46 BCMA_CORE_PCI_MDIODATA_REGADDR_SHF);
47 v |= BCMA_CORE_PCI_MDIODATA_TA;
Rafał Miłecki8369ae32011-05-09 18:56:46 +020048 v |= (phy << 4);
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010049 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020050
51 udelay(10);
52 for (i = 0; i < 200; i++) {
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010053 v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL);
54 if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE)
Rafał Miłecki8369ae32011-05-09 18:56:46 +020055 break;
56 msleep(1);
57 }
58}
59
60static u16 bcma_pcie_mdio_read(struct bcma_drv_pci *pc, u8 device, u8 address)
61{
Rafał Miłecki8369ae32011-05-09 18:56:46 +020062 int max_retries = 10;
63 u16 ret = 0;
64 u32 v;
65 int i;
66
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010067 /* enable mdio access to SERDES */
68 v = BCMA_CORE_PCI_MDIOCTL_PREAM_EN;
69 v |= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL;
70 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, v);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020071
72 if (pc->core->id.rev >= 10) {
73 max_retries = 200;
74 bcma_pcie_mdio_set_phy(pc, device);
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010075 v = (BCMA_CORE_PCI_MDIODATA_DEV_ADDR <<
76 BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF);
77 v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF);
78 } else {
79 v = (device << BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD);
80 v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020081 }
82
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010083 v = BCMA_CORE_PCI_MDIODATA_START;
84 v |= BCMA_CORE_PCI_MDIODATA_READ;
85 v |= BCMA_CORE_PCI_MDIODATA_TA;
86
87 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020088 /* Wait for the device to complete the transaction */
89 udelay(10);
Rafał Miłeckif1a9c1e2011-05-12 00:01:47 +020090 for (i = 0; i < max_retries; i++) {
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010091 v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL);
92 if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE) {
Rafał Miłecki8369ae32011-05-09 18:56:46 +020093 udelay(10);
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010094 ret = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_DATA);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020095 break;
96 }
97 msleep(1);
98 }
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +010099 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, 0);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200100 return ret;
101}
102
103static void bcma_pcie_mdio_write(struct bcma_drv_pci *pc, u8 device,
104 u8 address, u16 data)
105{
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200106 int max_retries = 10;
107 u32 v;
108 int i;
109
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100110 /* enable mdio access to SERDES */
111 v = BCMA_CORE_PCI_MDIOCTL_PREAM_EN;
112 v |= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL;
113 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, v);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200114
115 if (pc->core->id.rev >= 10) {
116 max_retries = 200;
117 bcma_pcie_mdio_set_phy(pc, device);
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100118 v = (BCMA_CORE_PCI_MDIODATA_DEV_ADDR <<
119 BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF);
120 v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF);
121 } else {
122 v = (device << BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD);
123 v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200124 }
125
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100126 v = BCMA_CORE_PCI_MDIODATA_START;
127 v |= BCMA_CORE_PCI_MDIODATA_WRITE;
128 v |= BCMA_CORE_PCI_MDIODATA_TA;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200129 v |= data;
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100130 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200131 /* Wait for the device to complete the transaction */
132 udelay(10);
133 for (i = 0; i < max_retries; i++) {
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100134 v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL);
135 if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200136 break;
137 msleep(1);
138 }
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100139 pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, 0);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200140}
141
142/**************************************************
143 * Workarounds.
144 **************************************************/
145
146static u8 bcma_pcicore_polarity_workaround(struct bcma_drv_pci *pc)
147{
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100148 u32 tmp;
149
150 tmp = bcma_pcie_read(pc, BCMA_CORE_PCI_PLP_STATUSREG);
151 if (tmp & BCMA_CORE_PCI_PLP_POLARITYINV_STAT)
152 return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE |
153 BCMA_CORE_PCI_SERDES_RX_CTRL_POLARITY;
154 else
155 return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200156}
157
158static void bcma_pcicore_serdes_workaround(struct bcma_drv_pci *pc)
159{
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200160 u16 tmp;
161
Hauke Mehrtens2be25ca2012-01-31 00:03:32 +0100162 bcma_pcie_mdio_write(pc, BCMA_CORE_PCI_MDIODATA_DEV_RX,
163 BCMA_CORE_PCI_SERDES_RX_CTRL,
164 bcma_pcicore_polarity_workaround(pc));
165 tmp = bcma_pcie_mdio_read(pc, BCMA_CORE_PCI_MDIODATA_DEV_PLL,
166 BCMA_CORE_PCI_SERDES_PLL_CTRL);
167 if (tmp & BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN)
168 bcma_pcie_mdio_write(pc, BCMA_CORE_PCI_MDIODATA_DEV_PLL,
169 BCMA_CORE_PCI_SERDES_PLL_CTRL,
170 tmp & ~BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200171}
172
173/**************************************************
174 * Init.
175 **************************************************/
176
Rafał Miłecki9352f692011-07-05 19:48:26 +0200177static void bcma_core_pci_clientmode_init(struct bcma_drv_pci *pc)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200178{
179 bcma_pcicore_serdes_workaround(pc);
180}
Rafał Miłecki1de520f2011-05-19 14:08:22 +0200181
Rafał Miłecki9352f692011-07-05 19:48:26 +0200182static bool bcma_core_pci_is_in_hostmode(struct bcma_drv_pci *pc)
183{
184 struct bcma_bus *bus = pc->core->bus;
185 u16 chipid_top;
186
187 chipid_top = (bus->chipinfo.id & 0xFF00);
188 if (chipid_top != 0x4700 &&
189 chipid_top != 0x5300)
190 return false;
191
John W. Linville6e6e8c52011-07-22 16:22:14 -0400192#ifdef CONFIG_SSB_DRIVER_PCICORE
Rafał Miłecki26f26222011-07-23 11:10:12 +0200193 if (bus->sprom.boardflags_lo & SSB_BFL_NOPCI)
Rafał Miłecki9352f692011-07-05 19:48:26 +0200194 return false;
John W. Linville6e6e8c52011-07-22 16:22:14 -0400195#endif /* CONFIG_SSB_DRIVER_PCICORE */
Rafał Miłecki9352f692011-07-05 19:48:26 +0200196
197#if 0
198 /* TODO: on BCMA we use address from EROM instead of magic formula */
199 u32 tmp;
200 return !mips_busprobe32(tmp, (bus->mmio +
201 (pc->core->core_index * BCMA_CORE_SIZE)));
202#endif
203
204 return true;
205}
206
207void bcma_core_pci_init(struct bcma_drv_pci *pc)
208{
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200209 if (pc->setup_done)
210 return;
211
Rafał Miłecki9352f692011-07-05 19:48:26 +0200212 if (bcma_core_pci_is_in_hostmode(pc)) {
213#ifdef CONFIG_BCMA_DRIVER_PCI_HOSTMODE
214 bcma_core_pci_hostmode_init(pc);
215#else
216 pr_err("Driver compiled without support for hostmode PCI\n");
217#endif /* CONFIG_BCMA_DRIVER_PCI_HOSTMODE */
218 } else {
219 bcma_core_pci_clientmode_init(pc);
220 }
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200221
222 pc->setup_done = true;
Rafał Miłecki9352f692011-07-05 19:48:26 +0200223}
224
Rafał Miłecki1de520f2011-05-19 14:08:22 +0200225int bcma_core_pci_irq_ctl(struct bcma_drv_pci *pc, struct bcma_device *core,
226 bool enable)
227{
228 struct pci_dev *pdev = pc->core->bus->host_pci;
229 u32 coremask, tmp;
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200230 int err = 0;
231
232 if (core->bus->hosttype != BCMA_HOSTTYPE_PCI) {
233 /* This bcma device is not on a PCI host-bus. So the IRQs are
234 * not routed through the PCI core.
235 * So we must not enable routing through the PCI core. */
236 goto out;
237 }
Rafał Miłecki1de520f2011-05-19 14:08:22 +0200238
239 err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp);
240 if (err)
241 goto out;
242
243 coremask = BIT(core->core_index) << 8;
244 if (enable)
245 tmp |= coremask;
246 else
247 tmp &= ~coremask;
248
249 err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp);
250
251out:
252 return err;
253}
Rafał Miłecki440ca982011-06-18 01:01:59 +0200254EXPORT_SYMBOL_GPL(bcma_core_pci_irq_ctl);