18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Sonics Silicon Backplane
38c2ecf20Sopenharmony_ci * Broadcom Gigabit Ethernet core driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2008, Broadcom Corporation
68c2ecf20Sopenharmony_ci * Copyright 2008, Michael Buesch <m@bues.ch>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Licensed under the GNU/GPL. See COPYING for details.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/ssb/ssb.h>
128c2ecf20Sopenharmony_ci#include <linux/ssb/ssb_driver_gige.h>
138c2ecf20Sopenharmony_ci#include <linux/export.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/pci_regs.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SSB Broadcom Gigabit Ethernet driver");
218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Buesch");
228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
238c2ecf20Sopenharmony_ci*/
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic const struct ssb_device_id ssb_gige_tbl[] = {
268c2ecf20Sopenharmony_ci	SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_ETHERNET_GBIT, SSB_ANY_REV),
278c2ecf20Sopenharmony_ci	{},
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci/* MODULE_DEVICE_TABLE(ssb, ssb_gige_tbl); */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic inline u8 gige_read8(struct ssb_gige *dev, u16 offset)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	return ssb_read8(dev->dev, offset);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic inline u16 gige_read16(struct ssb_gige *dev, u16 offset)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	return ssb_read16(dev->dev, offset);
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic inline u32 gige_read32(struct ssb_gige *dev, u16 offset)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	return ssb_read32(dev->dev, offset);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic inline void gige_write8(struct ssb_gige *dev,
488c2ecf20Sopenharmony_ci			       u16 offset, u8 value)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	ssb_write8(dev->dev, offset, value);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic inline void gige_write16(struct ssb_gige *dev,
548c2ecf20Sopenharmony_ci				u16 offset, u16 value)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	ssb_write16(dev->dev, offset, value);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic inline void gige_write32(struct ssb_gige *dev,
608c2ecf20Sopenharmony_ci				u16 offset, u32 value)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	ssb_write32(dev->dev, offset, value);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic inline
668c2ecf20Sopenharmony_ciu8 gige_pcicfg_read8(struct ssb_gige *dev, unsigned int offset)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	BUG_ON(offset >= 256);
698c2ecf20Sopenharmony_ci	return gige_read8(dev, SSB_GIGE_PCICFG + offset);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic inline
738c2ecf20Sopenharmony_ciu16 gige_pcicfg_read16(struct ssb_gige *dev, unsigned int offset)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	BUG_ON(offset >= 256);
768c2ecf20Sopenharmony_ci	return gige_read16(dev, SSB_GIGE_PCICFG + offset);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic inline
808c2ecf20Sopenharmony_ciu32 gige_pcicfg_read32(struct ssb_gige *dev, unsigned int offset)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	BUG_ON(offset >= 256);
838c2ecf20Sopenharmony_ci	return gige_read32(dev, SSB_GIGE_PCICFG + offset);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic inline
878c2ecf20Sopenharmony_civoid gige_pcicfg_write8(struct ssb_gige *dev,
888c2ecf20Sopenharmony_ci			unsigned int offset, u8 value)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	BUG_ON(offset >= 256);
918c2ecf20Sopenharmony_ci	gige_write8(dev, SSB_GIGE_PCICFG + offset, value);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic inline
958c2ecf20Sopenharmony_civoid gige_pcicfg_write16(struct ssb_gige *dev,
968c2ecf20Sopenharmony_ci			 unsigned int offset, u16 value)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	BUG_ON(offset >= 256);
998c2ecf20Sopenharmony_ci	gige_write16(dev, SSB_GIGE_PCICFG + offset, value);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic inline
1038c2ecf20Sopenharmony_civoid gige_pcicfg_write32(struct ssb_gige *dev,
1048c2ecf20Sopenharmony_ci			 unsigned int offset, u32 value)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	BUG_ON(offset >= 256);
1078c2ecf20Sopenharmony_ci	gige_write32(dev, SSB_GIGE_PCICFG + offset, value);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int ssb_gige_pci_read_config(struct pci_bus *bus, unsigned int devfn,
1118c2ecf20Sopenharmony_ci				    int reg, int size, u32 *val)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
1148c2ecf20Sopenharmony_ci	unsigned long flags;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
1178c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1188c2ecf20Sopenharmony_ci	if (reg >= 256)
1198c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->lock, flags);
1228c2ecf20Sopenharmony_ci	switch (size) {
1238c2ecf20Sopenharmony_ci	case 1:
1248c2ecf20Sopenharmony_ci		*val = gige_pcicfg_read8(dev, reg);
1258c2ecf20Sopenharmony_ci		break;
1268c2ecf20Sopenharmony_ci	case 2:
1278c2ecf20Sopenharmony_ci		*val = gige_pcicfg_read16(dev, reg);
1288c2ecf20Sopenharmony_ci		break;
1298c2ecf20Sopenharmony_ci	case 4:
1308c2ecf20Sopenharmony_ci		*val = gige_pcicfg_read32(dev, reg);
1318c2ecf20Sopenharmony_ci		break;
1328c2ecf20Sopenharmony_ci	default:
1338c2ecf20Sopenharmony_ci		WARN_ON(1);
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->lock, flags);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic int ssb_gige_pci_write_config(struct pci_bus *bus, unsigned int devfn,
1418c2ecf20Sopenharmony_ci				     int reg, int size, u32 val)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
1448c2ecf20Sopenharmony_ci	unsigned long flags;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
1478c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1488c2ecf20Sopenharmony_ci	if (reg >= 256)
1498c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->lock, flags);
1528c2ecf20Sopenharmony_ci	switch (size) {
1538c2ecf20Sopenharmony_ci	case 1:
1548c2ecf20Sopenharmony_ci		gige_pcicfg_write8(dev, reg, val);
1558c2ecf20Sopenharmony_ci		break;
1568c2ecf20Sopenharmony_ci	case 2:
1578c2ecf20Sopenharmony_ci		gige_pcicfg_write16(dev, reg, val);
1588c2ecf20Sopenharmony_ci		break;
1598c2ecf20Sopenharmony_ci	case 4:
1608c2ecf20Sopenharmony_ci		gige_pcicfg_write32(dev, reg, val);
1618c2ecf20Sopenharmony_ci		break;
1628c2ecf20Sopenharmony_ci	default:
1638c2ecf20Sopenharmony_ci		WARN_ON(1);
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->lock, flags);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int ssb_gige_probe(struct ssb_device *sdev,
1718c2ecf20Sopenharmony_ci			  const struct ssb_device_id *id)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct ssb_gige *dev;
1748c2ecf20Sopenharmony_ci	u32 base, tmslow, tmshigh;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1778c2ecf20Sopenharmony_ci	if (!dev)
1788c2ecf20Sopenharmony_ci		return -ENOMEM;
1798c2ecf20Sopenharmony_ci	dev->dev = sdev;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	spin_lock_init(&dev->lock);
1828c2ecf20Sopenharmony_ci	dev->pci_controller.pci_ops = &dev->pci_ops;
1838c2ecf20Sopenharmony_ci	dev->pci_controller.io_resource = &dev->io_resource;
1848c2ecf20Sopenharmony_ci	dev->pci_controller.mem_resource = &dev->mem_resource;
1858c2ecf20Sopenharmony_ci	dev->pci_controller.io_map_base = 0x800;
1868c2ecf20Sopenharmony_ci	dev->pci_ops.read = ssb_gige_pci_read_config;
1878c2ecf20Sopenharmony_ci	dev->pci_ops.write = ssb_gige_pci_write_config;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	dev->io_resource.name = SSB_GIGE_IO_RES_NAME;
1908c2ecf20Sopenharmony_ci	dev->io_resource.start = 0x800;
1918c2ecf20Sopenharmony_ci	dev->io_resource.end = 0x8FF;
1928c2ecf20Sopenharmony_ci	dev->io_resource.flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (!ssb_device_is_enabled(sdev))
1958c2ecf20Sopenharmony_ci		ssb_device_enable(sdev, 0);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Setup BAR0. This is a 64k MMIO region. */
1988c2ecf20Sopenharmony_ci	base = ssb_admatch_base(ssb_read32(sdev, SSB_ADMATCH1));
1998c2ecf20Sopenharmony_ci	gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_0, base);
2008c2ecf20Sopenharmony_ci	gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_1, 0);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	dev->mem_resource.name = SSB_GIGE_MEM_RES_NAME;
2038c2ecf20Sopenharmony_ci	dev->mem_resource.start = base;
2048c2ecf20Sopenharmony_ci	dev->mem_resource.end = base + 0x10000 - 1;
2058c2ecf20Sopenharmony_ci	dev->mem_resource.flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* Enable the memory region. */
2088c2ecf20Sopenharmony_ci	gige_pcicfg_write16(dev, PCI_COMMAND,
2098c2ecf20Sopenharmony_ci			    gige_pcicfg_read16(dev, PCI_COMMAND)
2108c2ecf20Sopenharmony_ci			    | PCI_COMMAND_MEMORY);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/* Write flushing is controlled by the Flush Status Control register.
2138c2ecf20Sopenharmony_ci	 * We want to flush every register write with a timeout and we want
2148c2ecf20Sopenharmony_ci	 * to disable the IRQ mask while flushing to avoid concurrency.
2158c2ecf20Sopenharmony_ci	 * Note that automatic write flushing does _not_ work from
2168c2ecf20Sopenharmony_ci	 * an IRQ handler. The driver must flush manually by reading a register.
2178c2ecf20Sopenharmony_ci	 */
2188c2ecf20Sopenharmony_ci	gige_write32(dev, SSB_GIGE_SHIM_FLUSHSTAT, 0x00000068);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* Check if we have an RGMII or GMII PHY-bus.
2218c2ecf20Sopenharmony_ci	 * On RGMII do not bypass the DLLs */
2228c2ecf20Sopenharmony_ci	tmslow = ssb_read32(sdev, SSB_TMSLOW);
2238c2ecf20Sopenharmony_ci	tmshigh = ssb_read32(sdev, SSB_TMSHIGH);
2248c2ecf20Sopenharmony_ci	if (tmshigh & SSB_GIGE_TMSHIGH_RGMII) {
2258c2ecf20Sopenharmony_ci		tmslow &= ~SSB_GIGE_TMSLOW_TXBYPASS;
2268c2ecf20Sopenharmony_ci		tmslow &= ~SSB_GIGE_TMSLOW_RXBYPASS;
2278c2ecf20Sopenharmony_ci		dev->has_rgmii = 1;
2288c2ecf20Sopenharmony_ci	} else {
2298c2ecf20Sopenharmony_ci		tmslow |= SSB_GIGE_TMSLOW_TXBYPASS;
2308c2ecf20Sopenharmony_ci		tmslow |= SSB_GIGE_TMSLOW_RXBYPASS;
2318c2ecf20Sopenharmony_ci		dev->has_rgmii = 0;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci	tmslow |= SSB_GIGE_TMSLOW_DLLEN;
2348c2ecf20Sopenharmony_ci	ssb_write32(sdev, SSB_TMSLOW, tmslow);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ssb_set_drvdata(sdev, dev);
2378c2ecf20Sopenharmony_ci	register_pci_controller(&dev->pci_controller);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return 0;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cibool pdev_is_ssb_gige_core(struct pci_dev *pdev)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	if (!pdev->resource[0].name)
2458c2ecf20Sopenharmony_ci		return false;
2468c2ecf20Sopenharmony_ci	return (strcmp(pdev->resource[0].name, SSB_GIGE_MEM_RES_NAME) == 0);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pdev_is_ssb_gige_core);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ciint ssb_gige_pcibios_plat_dev_init(struct ssb_device *sdev,
2518c2ecf20Sopenharmony_ci				   struct pci_dev *pdev)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct ssb_gige *dev = ssb_get_drvdata(sdev);
2548c2ecf20Sopenharmony_ci	struct resource *res;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (pdev->bus->ops != &dev->pci_ops) {
2578c2ecf20Sopenharmony_ci		/* The PCI device is not on this SSB GigE bridge device. */
2588c2ecf20Sopenharmony_ci		return -ENODEV;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* Fixup the PCI resources. */
2628c2ecf20Sopenharmony_ci	res = &(pdev->resource[0]);
2638c2ecf20Sopenharmony_ci	res->flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
2648c2ecf20Sopenharmony_ci	res->name = dev->mem_resource.name;
2658c2ecf20Sopenharmony_ci	res->start = dev->mem_resource.start;
2668c2ecf20Sopenharmony_ci	res->end = dev->mem_resource.end;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Fixup interrupt lines. */
2698c2ecf20Sopenharmony_ci	pdev->irq = ssb_mips_irq(sdev) + 2;
2708c2ecf20Sopenharmony_ci	pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, pdev->irq);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ciint ssb_gige_map_irq(struct ssb_device *sdev,
2768c2ecf20Sopenharmony_ci		     const struct pci_dev *pdev)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct ssb_gige *dev = ssb_get_drvdata(sdev);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	if (pdev->bus->ops != &dev->pci_ops) {
2818c2ecf20Sopenharmony_ci		/* The PCI device is not on this SSB GigE bridge device. */
2828c2ecf20Sopenharmony_ci		return -ENODEV;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	return ssb_mips_irq(sdev) + 2;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic struct ssb_driver ssb_gige_driver = {
2898c2ecf20Sopenharmony_ci	.name		= "BCM-GigE",
2908c2ecf20Sopenharmony_ci	.id_table	= ssb_gige_tbl,
2918c2ecf20Sopenharmony_ci	.probe		= ssb_gige_probe,
2928c2ecf20Sopenharmony_ci};
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciint ssb_gige_init(void)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	return ssb_driver_register(&ssb_gige_driver);
2978c2ecf20Sopenharmony_ci}
298