blob: 15a7ee3859dd7dd74aee31593876b422093eff04 [file] [log] [blame]
Michael Buesch61e115a2007-09-18 15:12:50 -04001/*
2 * Sonics Silicon Backplane
3 * Broadcom PCI-core driver
4 *
5 * Copyright 2005, Broadcom Corporation
Michael Büscheb032b92011-07-04 20:50:05 +02006 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
Michael Buesch61e115a2007-09-18 15:12:50 -04007 *
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
10
11#include <linux/ssb/ssb.h>
12#include <linux/pci.h>
Paul Gortmaker1014c222011-07-27 22:07:02 -040013#include <linux/export.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040014#include <linux/delay.h>
Michael Buesch7cb44612008-02-19 17:46:48 +010015#include <linux/ssb/ssb_embedded.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040016
17#include "ssb_private.h"
18
Rafał Miłeckiccc7c282011-04-01 13:26:52 +020019static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address);
20static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data);
21static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address);
22static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
23 u8 address, u16 data);
Michael Buesch61e115a2007-09-18 15:12:50 -040024
25static inline
26u32 pcicore_read32(struct ssb_pcicore *pc, u16 offset)
27{
28 return ssb_read32(pc->dev, offset);
29}
30
31static inline
32void pcicore_write32(struct ssb_pcicore *pc, u16 offset, u32 value)
33{
34 ssb_write32(pc->dev, offset, value);
35}
36
Michael Buesch7cb44612008-02-19 17:46:48 +010037static inline
38u16 pcicore_read16(struct ssb_pcicore *pc, u16 offset)
39{
40 return ssb_read16(pc->dev, offset);
41}
42
43static inline
44void pcicore_write16(struct ssb_pcicore *pc, u16 offset, u16 value)
45{
46 ssb_write16(pc->dev, offset, value);
47}
48
Michael Buesch61e115a2007-09-18 15:12:50 -040049/**************************************************
50 * Code for hostmode operation.
51 **************************************************/
52
53#ifdef CONFIG_SSB_PCICORE_HOSTMODE
54
55#include <asm/paccess.h>
56/* Probe a 32bit value on the bus and catch bus exceptions.
57 * Returns nonzero on a bus exception.
58 * This is MIPS specific */
59#define mips_busprobe32(val, addr) get_dbe((val), ((u32 *)(addr)))
60
61/* Assume one-hot slot wiring */
62#define SSB_PCI_SLOT_MAX 16
63
64/* Global lock is OK, as we won't have more than one extpci anyway. */
65static DEFINE_SPINLOCK(cfgspace_lock);
66/* Core to access the external PCI config space. Can only have one. */
67static struct ssb_pcicore *extpci_core;
68
Michael Buesch61e115a2007-09-18 15:12:50 -040069
70static u32 get_cfgspace_addr(struct ssb_pcicore *pc,
71 unsigned int bus, unsigned int dev,
72 unsigned int func, unsigned int off)
73{
74 u32 addr = 0;
75 u32 tmp;
76
Michael Buesch7cb44612008-02-19 17:46:48 +010077 /* We do only have one cardbus device behind the bridge. */
Hauke Mehrtensa6c84622012-02-01 00:13:53 +010078 if (pc->cardbusmode && (dev > 1))
Michael Buesch61e115a2007-09-18 15:12:50 -040079 goto out;
Michael Buesch7cb44612008-02-19 17:46:48 +010080
Michael Buesch61e115a2007-09-18 15:12:50 -040081 if (bus == 0) {
82 /* Type 0 transaction */
83 if (unlikely(dev >= SSB_PCI_SLOT_MAX))
84 goto out;
85 /* Slide the window */
86 tmp = SSB_PCICORE_SBTOPCI_CFG0;
87 tmp |= ((1 << (dev + 16)) & SSB_PCICORE_SBTOPCI1_MASK);
88 pcicore_write32(pc, SSB_PCICORE_SBTOPCI1, tmp);
89 /* Calculate the address */
90 addr = SSB_PCI_CFG;
91 addr |= ((1 << (dev + 16)) & ~SSB_PCICORE_SBTOPCI1_MASK);
92 addr |= (func << 8);
93 addr |= (off & ~3);
94 } else {
95 /* Type 1 transaction */
96 pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
97 SSB_PCICORE_SBTOPCI_CFG1);
98 /* Calculate the address */
99 addr = SSB_PCI_CFG;
100 addr |= (bus << 16);
101 addr |= (dev << 11);
102 addr |= (func << 8);
103 addr |= (off & ~3);
104 }
105out:
106 return addr;
107}
108
109static int ssb_extpci_read_config(struct ssb_pcicore *pc,
110 unsigned int bus, unsigned int dev,
111 unsigned int func, unsigned int off,
112 void *buf, int len)
113{
114 int err = -EINVAL;
115 u32 addr, val;
116 void __iomem *mmio;
117
118 SSB_WARN_ON(!pc->hostmode);
119 if (unlikely(len != 1 && len != 2 && len != 4))
120 goto out;
121 addr = get_cfgspace_addr(pc, bus, dev, func, off);
122 if (unlikely(!addr))
123 goto out;
124 err = -ENOMEM;
125 mmio = ioremap_nocache(addr, len);
126 if (!mmio)
127 goto out;
128
129 if (mips_busprobe32(val, mmio)) {
130 val = 0xffffffff;
131 goto unmap;
132 }
133
134 val = readl(mmio);
135 val >>= (8 * (off & 3));
136
137 switch (len) {
138 case 1:
139 *((u8 *)buf) = (u8)val;
140 break;
141 case 2:
142 *((u16 *)buf) = (u16)val;
143 break;
144 case 4:
145 *((u32 *)buf) = (u32)val;
146 break;
147 }
148 err = 0;
149unmap:
150 iounmap(mmio);
151out:
152 return err;
153}
154
155static int ssb_extpci_write_config(struct ssb_pcicore *pc,
156 unsigned int bus, unsigned int dev,
157 unsigned int func, unsigned int off,
158 const void *buf, int len)
159{
160 int err = -EINVAL;
161 u32 addr, val = 0;
162 void __iomem *mmio;
163
164 SSB_WARN_ON(!pc->hostmode);
165 if (unlikely(len != 1 && len != 2 && len != 4))
166 goto out;
167 addr = get_cfgspace_addr(pc, bus, dev, func, off);
168 if (unlikely(!addr))
169 goto out;
170 err = -ENOMEM;
171 mmio = ioremap_nocache(addr, len);
172 if (!mmio)
173 goto out;
174
175 if (mips_busprobe32(val, mmio)) {
176 val = 0xffffffff;
177 goto unmap;
178 }
179
180 switch (len) {
181 case 1:
182 val = readl(mmio);
183 val &= ~(0xFF << (8 * (off & 3)));
184 val |= *((const u8 *)buf) << (8 * (off & 3));
185 break;
186 case 2:
187 val = readl(mmio);
188 val &= ~(0xFFFF << (8 * (off & 3)));
189 val |= *((const u16 *)buf) << (8 * (off & 3));
190 break;
191 case 4:
192 val = *((const u32 *)buf);
193 break;
194 }
195 writel(val, mmio);
196
197 err = 0;
198unmap:
199 iounmap(mmio);
200out:
201 return err;
202}
203
204static int ssb_pcicore_read_config(struct pci_bus *bus, unsigned int devfn,
205 int reg, int size, u32 *val)
206{
207 unsigned long flags;
208 int err;
209
210 spin_lock_irqsave(&cfgspace_lock, flags);
211 err = ssb_extpci_read_config(extpci_core, bus->number, PCI_SLOT(devfn),
212 PCI_FUNC(devfn), reg, val, size);
213 spin_unlock_irqrestore(&cfgspace_lock, flags);
214
215 return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
216}
217
218static int ssb_pcicore_write_config(struct pci_bus *bus, unsigned int devfn,
219 int reg, int size, u32 val)
220{
221 unsigned long flags;
222 int err;
223
224 spin_lock_irqsave(&cfgspace_lock, flags);
225 err = ssb_extpci_write_config(extpci_core, bus->number, PCI_SLOT(devfn),
226 PCI_FUNC(devfn), reg, &val, size);
227 spin_unlock_irqrestore(&cfgspace_lock, flags);
228
229 return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
230}
231
232static struct pci_ops ssb_pcicore_pciops = {
233 .read = ssb_pcicore_read_config,
234 .write = ssb_pcicore_write_config,
235};
236
237static struct resource ssb_pcicore_mem_resource = {
238 .name = "SSB PCIcore external memory",
239 .start = SSB_PCI_DMA,
240 .end = SSB_PCI_DMA + SSB_PCI_DMA_SZ - 1,
Michael Bueschfc71acc2008-02-16 18:13:36 +0100241 .flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED,
Michael Buesch61e115a2007-09-18 15:12:50 -0400242};
243
244static struct resource ssb_pcicore_io_resource = {
245 .name = "SSB PCIcore external I/O",
246 .start = 0x100,
247 .end = 0x7FF,
Michael Bueschfc71acc2008-02-16 18:13:36 +0100248 .flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED,
Michael Buesch61e115a2007-09-18 15:12:50 -0400249};
250
251static struct pci_controller ssb_pcicore_controller = {
252 .pci_ops = &ssb_pcicore_pciops,
253 .io_resource = &ssb_pcicore_io_resource,
254 .mem_resource = &ssb_pcicore_mem_resource,
Michael Buesch61e115a2007-09-18 15:12:50 -0400255};
256
Michael Bueschaab547c2008-02-29 11:36:12 +0100257/* This function is called when doing a pci_enable_device().
258 * We must first check if the device is a device on the PCI-core bridge. */
259int ssb_pcicore_plat_dev_init(struct pci_dev *d)
260{
Michael Bueschaab547c2008-02-29 11:36:12 +0100261 if (d->bus->ops != &ssb_pcicore_pciops) {
262 /* This is not a device on the PCI-core bridge. */
263 return -ENODEV;
264 }
265
Joe Perches33a606a2013-02-20 12:16:13 -0800266 ssb_info("PCI: Fixing up device %s\n", pci_name(d));
Michael Bueschaab547c2008-02-29 11:36:12 +0100267
Michael Bueschaab547c2008-02-29 11:36:12 +0100268 /* Fix up interrupt lines */
269 d->irq = ssb_mips_irq(extpci_core->dev) + 2;
270 pci_write_config_byte(d, PCI_INTERRUPT_LINE, d->irq);
271
272 return 0;
273}
274
275/* Early PCI fixup for a device on the PCI-core bridge. */
276static void ssb_pcicore_fixup_pcibridge(struct pci_dev *dev)
277{
278 u8 lat;
279
280 if (dev->bus->ops != &ssb_pcicore_pciops) {
281 /* This is not a device on the PCI-core bridge. */
282 return;
283 }
284 if (dev->bus->number != 0 || PCI_SLOT(dev->devfn) != 0)
285 return;
286
Joe Perches33a606a2013-02-20 12:16:13 -0800287 ssb_info("PCI: Fixing up bridge %s\n", pci_name(dev));
Michael Bueschaab547c2008-02-29 11:36:12 +0100288
289 /* Enable PCI bridge bus mastering and memory space */
290 pci_set_master(dev);
291 if (pcibios_enable_device(dev, ~0) < 0) {
Joe Perches33a606a2013-02-20 12:16:13 -0800292 ssb_err("PCI: SSB bridge enable failed\n");
Michael Bueschaab547c2008-02-29 11:36:12 +0100293 return;
294 }
295
296 /* Enable PCI bridge BAR1 prefetch and burst */
297 pci_write_config_dword(dev, SSB_BAR1_CONTROL, 3);
298
299 /* Make sure our latency is high enough to handle the devices behind us */
300 lat = 168;
Joe Perches33a606a2013-02-20 12:16:13 -0800301 ssb_info("PCI: Fixing latency timer of device %s to %u\n",
302 pci_name(dev), lat);
Michael Bueschaab547c2008-02-29 11:36:12 +0100303 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
304}
305DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, ssb_pcicore_fixup_pcibridge);
306
307/* PCI device IRQ mapping. */
308int ssb_pcicore_pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
309{
310 if (dev->bus->ops != &ssb_pcicore_pciops) {
311 /* This is not a device on the PCI-core bridge. */
312 return -ENODEV;
313 }
314 return ssb_mips_irq(extpci_core->dev) + 2;
315}
316
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -0800317static void ssb_pcicore_init_hostmode(struct ssb_pcicore *pc)
Michael Buesch61e115a2007-09-18 15:12:50 -0400318{
319 u32 val;
320
321 if (WARN_ON(extpci_core))
322 return;
323 extpci_core = pc;
324
Joe Perches33a606a2013-02-20 12:16:13 -0800325 ssb_dbg("PCIcore in host mode found\n");
Michael Buesch61e115a2007-09-18 15:12:50 -0400326 /* Reset devices on the external PCI bus */
327 val = SSB_PCICORE_CTL_RST_OE;
328 val |= SSB_PCICORE_CTL_CLK_OE;
329 pcicore_write32(pc, SSB_PCICORE_CTL, val);
330 val |= SSB_PCICORE_CTL_CLK; /* Clock on */
331 pcicore_write32(pc, SSB_PCICORE_CTL, val);
332 udelay(150); /* Assertion time demanded by the PCI standard */
333 val |= SSB_PCICORE_CTL_RST; /* Deassert RST# */
334 pcicore_write32(pc, SSB_PCICORE_CTL, val);
335 val = SSB_PCICORE_ARBCTL_INTERN;
336 pcicore_write32(pc, SSB_PCICORE_ARBCTL, val);
337 udelay(1); /* Assertion time demanded by the PCI standard */
338
Michael Buesch7cb44612008-02-19 17:46:48 +0100339 if (pc->dev->bus->has_cardbus_slot) {
Joe Perches33a606a2013-02-20 12:16:13 -0800340 ssb_dbg("CardBus slot detected\n");
Michael Buesch7cb44612008-02-19 17:46:48 +0100341 pc->cardbusmode = 1;
342 /* GPIO 1 resets the bridge */
343 ssb_gpio_out(pc->dev->bus, 1, 1);
344 ssb_gpio_outen(pc->dev->bus, 1, 1);
345 pcicore_write16(pc, SSB_PCICORE_SPROM(0),
346 pcicore_read16(pc, SSB_PCICORE_SPROM(0))
347 | 0x0400);
348 }
Michael Buesch61e115a2007-09-18 15:12:50 -0400349
350 /* 64MB I/O window */
351 pcicore_write32(pc, SSB_PCICORE_SBTOPCI0,
352 SSB_PCICORE_SBTOPCI_IO);
353 /* 64MB config space */
354 pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
355 SSB_PCICORE_SBTOPCI_CFG0);
356 /* 1GB memory window */
357 pcicore_write32(pc, SSB_PCICORE_SBTOPCI2,
358 SSB_PCICORE_SBTOPCI_MEM | SSB_PCI_DMA);
359
Rafał Miłeckif56d9e22015-04-05 18:39:27 +0200360 /*
361 * Accessing PCI config without a proper delay after devices reset (not
362 * GPIO reset) was causing reboots on WRT300N v1.0.
363 * Tested delay 850 us lowered reboot chance to 50-80%, 1000 us fixed it
364 * completely. Flushing all writes was also tested but with no luck.
365 */
366 if (pc->dev->bus->chip_id == 0x4704)
367 usleep_range(1000, 2000);
368
Michael Buesch61e115a2007-09-18 15:12:50 -0400369 /* Enable PCI bridge BAR0 prefetch and burst */
370 val = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
371 ssb_extpci_write_config(pc, 0, 0, 0, PCI_COMMAND, &val, 2);
372 /* Clear error conditions */
373 val = 0;
374 ssb_extpci_write_config(pc, 0, 0, 0, PCI_STATUS, &val, 2);
375
376 /* Enable PCI interrupts */
377 pcicore_write32(pc, SSB_PCICORE_IMASK,
378 SSB_PCICORE_IMASK_INTA);
379
380 /* Ok, ready to run, register it to the system.
381 * The following needs change, if we want to port hostmode
382 * to non-MIPS platform. */
Michael Bueschfc71acc2008-02-16 18:13:36 +0100383 ssb_pcicore_controller.io_map_base = (unsigned long)ioremap_nocache(SSB_PCI_MEM, 0x04000000);
384 set_io_port_base(ssb_pcicore_controller.io_map_base);
Michael Buesch61e115a2007-09-18 15:12:50 -0400385 /* Give some time to the PCI controller to configure itself with the new
386 * values. Not waiting at this point causes crashes of the machine. */
387 mdelay(10);
388 register_pci_controller(&ssb_pcicore_controller);
389}
390
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -0800391static int pcicore_is_in_hostmode(struct ssb_pcicore *pc)
Michael Buesch61e115a2007-09-18 15:12:50 -0400392{
393 struct ssb_bus *bus = pc->dev->bus;
394 u16 chipid_top;
395 u32 tmp;
396
397 chipid_top = (bus->chip_id & 0xFF00);
398 if (chipid_top != 0x4700 &&
399 chipid_top != 0x5300)
400 return 0;
401
Aurelien Jarno2d8d4fd2008-02-28 15:11:26 +0100402 if (bus->sprom.boardflags_lo & SSB_PCICORE_BFL_NOPCI)
Michael Buesch61e115a2007-09-18 15:12:50 -0400403 return 0;
404
405 /* The 200-pin BCM4712 package does not bond out PCI. Even when
406 * PCI is bonded out, some boards may leave the pins floating. */
407 if (bus->chip_id == 0x4712) {
408 if (bus->chip_package == SSB_CHIPPACK_BCM4712S)
409 return 0;
410 if (bus->chip_package == SSB_CHIPPACK_BCM4712M)
411 return 0;
412 }
413 if (bus->chip_id == 0x5350)
414 return 0;
415
416 return !mips_busprobe32(tmp, (bus->mmio + (pc->dev->core_index * SSB_CORE_SIZE)));
417}
418#endif /* CONFIG_SSB_PCICORE_HOSTMODE */
419
Rafał Miłeckiccc7c282011-04-01 13:26:52 +0200420/**************************************************
421 * Workarounds.
422 **************************************************/
423
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -0800424static void ssb_pcicore_fix_sprom_core_index(struct ssb_pcicore *pc)
Rafał Miłeckiaf335a62011-04-27 18:21:34 +0200425{
426 u16 tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(0));
427 if (((tmp & 0xF000) >> 12) != pc->dev->core_index) {
428 tmp &= ~0xF000;
429 tmp |= (pc->dev->core_index << 12);
430 pcicore_write16(pc, SSB_PCICORE_SPROM(0), tmp);
431 }
432}
433
Rafał Miłeckiccc7c282011-04-01 13:26:52 +0200434static u8 ssb_pcicore_polarity_workaround(struct ssb_pcicore *pc)
435{
436 return (ssb_pcie_read(pc, 0x204) & 0x10) ? 0xC0 : 0x80;
437}
438
439static void ssb_pcicore_serdes_workaround(struct ssb_pcicore *pc)
440{
441 const u8 serdes_pll_device = 0x1D;
442 const u8 serdes_rx_device = 0x1F;
443 u16 tmp;
444
445 ssb_pcie_mdio_write(pc, serdes_rx_device, 1 /* Control */,
446 ssb_pcicore_polarity_workaround(pc));
447 tmp = ssb_pcie_mdio_read(pc, serdes_pll_device, 1 /* Control */);
448 if (tmp & 0x4000)
449 ssb_pcie_mdio_write(pc, serdes_pll_device, 1, tmp & ~0x4000);
450}
Michael Buesch61e115a2007-09-18 15:12:50 -0400451
Rafał Miłecki6e914102011-04-27 17:39:47 +0200452static void ssb_pcicore_pci_setup_workarounds(struct ssb_pcicore *pc)
453{
454 struct ssb_device *pdev = pc->dev;
455 struct ssb_bus *bus = pdev->bus;
456 u32 tmp;
457
458 tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
459 tmp |= SSB_PCICORE_SBTOPCI_PREF;
460 tmp |= SSB_PCICORE_SBTOPCI_BURST;
461 pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
462
463 if (pdev->id.revision < 5) {
464 tmp = ssb_read32(pdev, SSB_IMCFGLO);
465 tmp &= ~SSB_IMCFGLO_SERTO;
466 tmp |= 2;
467 tmp &= ~SSB_IMCFGLO_REQTO;
468 tmp |= 3 << SSB_IMCFGLO_REQTO_SHIFT;
469 ssb_write32(pdev, SSB_IMCFGLO, tmp);
470 ssb_commit_settings(bus);
471 } else if (pdev->id.revision >= 11) {
472 tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
473 tmp |= SSB_PCICORE_SBTOPCI_MRM;
474 pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
475 }
476}
477
478static void ssb_pcicore_pcie_setup_workarounds(struct ssb_pcicore *pc)
479{
Rafał Miłecki6e914102011-04-27 17:39:47 +0200480 u32 tmp;
Rafał Miłecki5890a3c2011-04-27 17:39:48 +0200481 u8 rev = pc->dev->id.revision;
Rafał Miłecki6e914102011-04-27 17:39:47 +0200482
Rafał Miłecki5890a3c2011-04-27 17:39:48 +0200483 if (rev == 0 || rev == 1) {
Rafał Miłecki6e914102011-04-27 17:39:47 +0200484 /* TLP Workaround register. */
485 tmp = ssb_pcie_read(pc, 0x4);
486 tmp |= 0x8;
487 ssb_pcie_write(pc, 0x4, tmp);
488 }
Rafał Miłecki5890a3c2011-04-27 17:39:48 +0200489 if (rev == 1) {
490 /* DLLP Link Control register. */
491 tmp = ssb_pcie_read(pc, 0x100);
492 tmp |= 0x40;
493 ssb_pcie_write(pc, 0x100, tmp);
494 }
495
496 if (rev == 0) {
Rafał Miłecki6e914102011-04-27 17:39:47 +0200497 const u8 serdes_rx_device = 0x1F;
498
499 ssb_pcie_mdio_write(pc, serdes_rx_device,
500 2 /* Timer */, 0x8128);
501 ssb_pcie_mdio_write(pc, serdes_rx_device,
502 6 /* CDR */, 0x0100);
503 ssb_pcie_mdio_write(pc, serdes_rx_device,
504 7 /* CDR BW */, 0x1466);
Rafał Miłecki5890a3c2011-04-27 17:39:48 +0200505 } else if (rev == 3 || rev == 4 || rev == 5) {
506 /* TODO: DLLP Power Management Threshold */
507 ssb_pcicore_serdes_workaround(pc);
508 /* TODO: ASPM */
509 } else if (rev == 7) {
510 /* TODO: No PLL down */
511 }
512
513 if (rev >= 6) {
514 /* Miscellaneous Configuration Fixup */
515 tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(5));
516 if (!(tmp & 0x8000))
517 pcicore_write16(pc, SSB_PCICORE_SPROM(5),
518 tmp | 0x8000);
Rafał Miłecki6e914102011-04-27 17:39:47 +0200519 }
520}
521
Michael Buesch61e115a2007-09-18 15:12:50 -0400522/**************************************************
523 * Generic and Clientmode operation code.
524 **************************************************/
525
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -0800526static void ssb_pcicore_init_clientmode(struct ssb_pcicore *pc)
Michael Buesch61e115a2007-09-18 15:12:50 -0400527{
Hauke Mehrtens329456d2011-12-05 23:19:51 +0100528 struct ssb_device *pdev = pc->dev;
529 struct ssb_bus *bus = pdev->bus;
530
531 if (bus->bustype == SSB_BUSTYPE_PCI)
532 ssb_pcicore_fix_sprom_core_index(pc);
Rafał Miłecki6ae8ec22011-07-05 17:25:32 +0200533
Michael Buesch61e115a2007-09-18 15:12:50 -0400534 /* Disable PCI interrupts. */
Hauke Mehrtens329456d2011-12-05 23:19:51 +0100535 ssb_write32(pdev, SSB_INTVEC, 0);
Rafał Miłecki6ae8ec22011-07-05 17:25:32 +0200536
537 /* Additional PCIe always once-executed workarounds */
538 if (pc->dev->id.coreid == SSB_DEV_PCIE) {
539 ssb_pcicore_serdes_workaround(pc);
540 /* TODO: ASPM */
541 /* TODO: Clock Request Update */
542 }
Michael Buesch61e115a2007-09-18 15:12:50 -0400543}
544
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -0800545void ssb_pcicore_init(struct ssb_pcicore *pc)
Michael Buesch61e115a2007-09-18 15:12:50 -0400546{
547 struct ssb_device *dev = pc->dev;
Michael Buesch61e115a2007-09-18 15:12:50 -0400548
549 if (!dev)
550 return;
Michael Buesch61e115a2007-09-18 15:12:50 -0400551 if (!ssb_device_is_enabled(dev))
552 ssb_device_enable(dev, 0);
553
554#ifdef CONFIG_SSB_PCICORE_HOSTMODE
555 pc->hostmode = pcicore_is_in_hostmode(pc);
556 if (pc->hostmode)
557 ssb_pcicore_init_hostmode(pc);
558#endif /* CONFIG_SSB_PCICORE_HOSTMODE */
559 if (!pc->hostmode)
560 ssb_pcicore_init_clientmode(pc);
561}
562
563static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address)
564{
565 pcicore_write32(pc, 0x130, address);
566 return pcicore_read32(pc, 0x134);
567}
568
569static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data)
570{
571 pcicore_write32(pc, 0x130, address);
572 pcicore_write32(pc, 0x134, data);
573}
574
Rafał Miłecki1b1c7ac2011-04-01 12:07:33 +0200575static void ssb_pcie_mdio_set_phy(struct ssb_pcicore *pc, u8 phy)
576{
577 const u16 mdio_control = 0x128;
578 const u16 mdio_data = 0x12C;
579 u32 v;
580 int i;
581
582 v = (1 << 30); /* Start of Transaction */
583 v |= (1 << 28); /* Write Transaction */
584 v |= (1 << 17); /* Turnaround */
585 v |= (0x1F << 18);
586 v |= (phy << 4);
587 pcicore_write32(pc, mdio_data, v);
588
589 udelay(10);
590 for (i = 0; i < 200; i++) {
591 v = pcicore_read32(pc, mdio_control);
592 if (v & 0x100 /* Trans complete */)
593 break;
594 msleep(1);
595 }
596}
597
Rafał Miłeckiba91d1a2011-04-01 12:07:34 +0200598static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address)
599{
600 const u16 mdio_control = 0x128;
601 const u16 mdio_data = 0x12C;
602 int max_retries = 10;
603 u16 ret = 0;
604 u32 v;
605 int i;
606
607 v = 0x80; /* Enable Preamble Sequence */
608 v |= 0x2; /* MDIO Clock Divisor */
609 pcicore_write32(pc, mdio_control, v);
610
611 if (pc->dev->id.revision >= 10) {
612 max_retries = 200;
613 ssb_pcie_mdio_set_phy(pc, device);
614 }
615
616 v = (1 << 30); /* Start of Transaction */
617 v |= (1 << 29); /* Read Transaction */
618 v |= (1 << 17); /* Turnaround */
619 if (pc->dev->id.revision < 10)
620 v |= (u32)device << 22;
621 v |= (u32)address << 18;
622 pcicore_write32(pc, mdio_data, v);
623 /* Wait for the device to complete the transaction */
624 udelay(10);
Rafał Miłecki9be1cb32011-04-19 22:40:22 +0200625 for (i = 0; i < max_retries; i++) {
Rafał Miłeckiba91d1a2011-04-01 12:07:34 +0200626 v = pcicore_read32(pc, mdio_control);
627 if (v & 0x100 /* Trans complete */) {
628 udelay(10);
629 ret = pcicore_read32(pc, mdio_data);
630 break;
631 }
632 msleep(1);
633 }
634 pcicore_write32(pc, mdio_control, 0);
635 return ret;
636}
Rafał Miłeckiba91d1a2011-04-01 12:07:34 +0200637
Michael Buesch61e115a2007-09-18 15:12:50 -0400638static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
639 u8 address, u16 data)
640{
641 const u16 mdio_control = 0x128;
642 const u16 mdio_data = 0x12C;
Rafał Miłecki1b1c7ac2011-04-01 12:07:33 +0200643 int max_retries = 10;
Michael Buesch61e115a2007-09-18 15:12:50 -0400644 u32 v;
645 int i;
646
647 v = 0x80; /* Enable Preamble Sequence */
648 v |= 0x2; /* MDIO Clock Divisor */
649 pcicore_write32(pc, mdio_control, v);
650
Rafał Miłecki1b1c7ac2011-04-01 12:07:33 +0200651 if (pc->dev->id.revision >= 10) {
652 max_retries = 200;
653 ssb_pcie_mdio_set_phy(pc, device);
654 }
655
Michael Buesch61e115a2007-09-18 15:12:50 -0400656 v = (1 << 30); /* Start of Transaction */
657 v |= (1 << 28); /* Write Transaction */
658 v |= (1 << 17); /* Turnaround */
Rafał Miłecki1b1c7ac2011-04-01 12:07:33 +0200659 if (pc->dev->id.revision < 10)
660 v |= (u32)device << 22;
Michael Buesch61e115a2007-09-18 15:12:50 -0400661 v |= (u32)address << 18;
662 v |= data;
663 pcicore_write32(pc, mdio_data, v);
664 /* Wait for the device to complete the transaction */
665 udelay(10);
Rafał Miłecki1b1c7ac2011-04-01 12:07:33 +0200666 for (i = 0; i < max_retries; i++) {
Michael Buesch61e115a2007-09-18 15:12:50 -0400667 v = pcicore_read32(pc, mdio_control);
668 if (v & 0x100 /* Trans complete */)
669 break;
670 msleep(1);
671 }
672 pcicore_write32(pc, mdio_control, 0);
673}
674
Michael Buesch61e115a2007-09-18 15:12:50 -0400675int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore *pc,
676 struct ssb_device *dev)
677{
678 struct ssb_device *pdev = pc->dev;
679 struct ssb_bus *bus;
680 int err = 0;
681 u32 tmp;
682
Michael Buesch9e095a62008-07-04 23:44:37 +0200683 if (dev->bus->bustype != SSB_BUSTYPE_PCI) {
684 /* This SSB device is not on a PCI host-bus. So the IRQs are
685 * not routed through the PCI core.
686 * So we must not enable routing through the PCI core. */
687 goto out;
688 }
689
Michael Buesch61e115a2007-09-18 15:12:50 -0400690 if (!pdev)
691 goto out;
692 bus = pdev->bus;
693
Michael Buescha3bafee2008-06-02 16:15:23 +0200694 might_sleep_if(pdev->id.coreid != SSB_DEV_PCI);
695
Michael Buesch61e115a2007-09-18 15:12:50 -0400696 /* Enable interrupts for this device. */
Michael Buesch8b454992009-10-09 20:32:10 +0200697 if ((pdev->id.revision >= 6) || (pdev->id.coreid == SSB_DEV_PCIE)) {
Michael Buesch61e115a2007-09-18 15:12:50 -0400698 u32 coremask;
699
700 /* Calculate the "coremask" for the device. */
701 coremask = (1 << dev->core_index);
702
Michael Buesch8b454992009-10-09 20:32:10 +0200703 SSB_WARN_ON(bus->bustype != SSB_BUSTYPE_PCI);
Michael Buesch61e115a2007-09-18 15:12:50 -0400704 err = pci_read_config_dword(bus->host_pci, SSB_PCI_IRQMASK, &tmp);
705 if (err)
706 goto out;
707 tmp |= coremask << 8;
708 err = pci_write_config_dword(bus->host_pci, SSB_PCI_IRQMASK, tmp);
709 if (err)
710 goto out;
711 } else {
712 u32 intvec;
713
714 intvec = ssb_read32(pdev, SSB_INTVEC);
Michael Buesch51e8b882008-04-08 10:31:22 +0200715 tmp = ssb_read32(dev, SSB_TPSFLAG);
716 tmp &= SSB_TPSFLAG_BPFLAG;
717 intvec |= (1 << tmp);
Michael Buesch61e115a2007-09-18 15:12:50 -0400718 ssb_write32(pdev, SSB_INTVEC, intvec);
719 }
720
721 /* Setup PCIcore operation. */
722 if (pc->setup_done)
723 goto out;
724 if (pdev->id.coreid == SSB_DEV_PCI) {
Rafał Miłecki6e914102011-04-27 17:39:47 +0200725 ssb_pcicore_pci_setup_workarounds(pc);
Michael Buesch61e115a2007-09-18 15:12:50 -0400726 } else {
727 WARN_ON(pdev->id.coreid != SSB_DEV_PCIE);
Rafał Miłecki6e914102011-04-27 17:39:47 +0200728 ssb_pcicore_pcie_setup_workarounds(pc);
Michael Buesch61e115a2007-09-18 15:12:50 -0400729 }
730 pc->setup_done = 1;
731out:
732 return err;
733}
734EXPORT_SYMBOL(ssb_pcicore_dev_irqvecs_enable);