1aab547ceSMichael Buesch /* 2aab547ceSMichael Buesch * Sonics Silicon Backplane 3aab547ceSMichael Buesch * Broadcom Gigabit Ethernet core driver 4aab547ceSMichael Buesch * 5aab547ceSMichael Buesch * Copyright 2008, Broadcom Corporation 6aab547ceSMichael Buesch * Copyright 2008, Michael Buesch <mb@bu3sch.de> 7aab547ceSMichael Buesch * 8aab547ceSMichael Buesch * Licensed under the GNU/GPL. See COPYING for details. 9aab547ceSMichael Buesch */ 10aab547ceSMichael Buesch 11aab547ceSMichael Buesch #include <linux/ssb/ssb.h> 12aab547ceSMichael Buesch #include <linux/ssb/ssb_driver_gige.h> 13aab547ceSMichael Buesch #include <linux/pci.h> 14aab547ceSMichael Buesch #include <linux/pci_regs.h> 15aab547ceSMichael Buesch 16aab547ceSMichael Buesch 17aab547ceSMichael Buesch /* 18aab547ceSMichael Buesch MODULE_DESCRIPTION("SSB Broadcom Gigabit Ethernet driver"); 19aab547ceSMichael Buesch MODULE_AUTHOR("Michael Buesch"); 20aab547ceSMichael Buesch MODULE_LICENSE("GPL"); 21aab547ceSMichael Buesch */ 22aab547ceSMichael Buesch 23aab547ceSMichael Buesch static const struct ssb_device_id ssb_gige_tbl[] = { 24aab547ceSMichael Buesch SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_ETHERNET_GBIT, SSB_ANY_REV), 25aab547ceSMichael Buesch SSB_DEVTABLE_END 26aab547ceSMichael Buesch }; 27aab547ceSMichael Buesch /* MODULE_DEVICE_TABLE(ssb, ssb_gige_tbl); */ 28aab547ceSMichael Buesch 29aab547ceSMichael Buesch 30aab547ceSMichael Buesch static inline u8 gige_read8(struct ssb_gige *dev, u16 offset) 31aab547ceSMichael Buesch { 32aab547ceSMichael Buesch return ssb_read8(dev->dev, offset); 33aab547ceSMichael Buesch } 34aab547ceSMichael Buesch 35aab547ceSMichael Buesch static inline u16 gige_read16(struct ssb_gige *dev, u16 offset) 36aab547ceSMichael Buesch { 37aab547ceSMichael Buesch return ssb_read16(dev->dev, offset); 38aab547ceSMichael Buesch } 39aab547ceSMichael Buesch 40aab547ceSMichael Buesch static inline u32 gige_read32(struct ssb_gige *dev, u16 offset) 41aab547ceSMichael Buesch { 42aab547ceSMichael Buesch return ssb_read32(dev->dev, offset); 43aab547ceSMichael Buesch } 44aab547ceSMichael Buesch 45aab547ceSMichael Buesch static inline void gige_write8(struct ssb_gige *dev, 46aab547ceSMichael Buesch u16 offset, u8 value) 47aab547ceSMichael Buesch { 48aab547ceSMichael Buesch ssb_write8(dev->dev, offset, value); 49aab547ceSMichael Buesch } 50aab547ceSMichael Buesch 51aab547ceSMichael Buesch static inline void gige_write16(struct ssb_gige *dev, 52aab547ceSMichael Buesch u16 offset, u16 value) 53aab547ceSMichael Buesch { 54aab547ceSMichael Buesch ssb_write16(dev->dev, offset, value); 55aab547ceSMichael Buesch } 56aab547ceSMichael Buesch 57aab547ceSMichael Buesch static inline void gige_write32(struct ssb_gige *dev, 58aab547ceSMichael Buesch u16 offset, u32 value) 59aab547ceSMichael Buesch { 60aab547ceSMichael Buesch ssb_write32(dev->dev, offset, value); 61aab547ceSMichael Buesch } 62aab547ceSMichael Buesch 63aab547ceSMichael Buesch static inline 64aab547ceSMichael Buesch u8 gige_pcicfg_read8(struct ssb_gige *dev, unsigned int offset) 65aab547ceSMichael Buesch { 66aab547ceSMichael Buesch BUG_ON(offset >= 256); 67aab547ceSMichael Buesch return gige_read8(dev, SSB_GIGE_PCICFG + offset); 68aab547ceSMichael Buesch } 69aab547ceSMichael Buesch 70aab547ceSMichael Buesch static inline 71aab547ceSMichael Buesch u16 gige_pcicfg_read16(struct ssb_gige *dev, unsigned int offset) 72aab547ceSMichael Buesch { 73aab547ceSMichael Buesch BUG_ON(offset >= 256); 74aab547ceSMichael Buesch return gige_read16(dev, SSB_GIGE_PCICFG + offset); 75aab547ceSMichael Buesch } 76aab547ceSMichael Buesch 77aab547ceSMichael Buesch static inline 78aab547ceSMichael Buesch u32 gige_pcicfg_read32(struct ssb_gige *dev, unsigned int offset) 79aab547ceSMichael Buesch { 80aab547ceSMichael Buesch BUG_ON(offset >= 256); 81aab547ceSMichael Buesch return gige_read32(dev, SSB_GIGE_PCICFG + offset); 82aab547ceSMichael Buesch } 83aab547ceSMichael Buesch 84aab547ceSMichael Buesch static inline 85aab547ceSMichael Buesch void gige_pcicfg_write8(struct ssb_gige *dev, 86aab547ceSMichael Buesch unsigned int offset, u8 value) 87aab547ceSMichael Buesch { 88aab547ceSMichael Buesch BUG_ON(offset >= 256); 89aab547ceSMichael Buesch gige_write8(dev, SSB_GIGE_PCICFG + offset, value); 90aab547ceSMichael Buesch } 91aab547ceSMichael Buesch 92aab547ceSMichael Buesch static inline 93aab547ceSMichael Buesch void gige_pcicfg_write16(struct ssb_gige *dev, 94aab547ceSMichael Buesch unsigned int offset, u16 value) 95aab547ceSMichael Buesch { 96aab547ceSMichael Buesch BUG_ON(offset >= 256); 97aab547ceSMichael Buesch gige_write16(dev, SSB_GIGE_PCICFG + offset, value); 98aab547ceSMichael Buesch } 99aab547ceSMichael Buesch 100aab547ceSMichael Buesch static inline 101aab547ceSMichael Buesch void gige_pcicfg_write32(struct ssb_gige *dev, 102aab547ceSMichael Buesch unsigned int offset, u32 value) 103aab547ceSMichael Buesch { 104aab547ceSMichael Buesch BUG_ON(offset >= 256); 105aab547ceSMichael Buesch gige_write32(dev, SSB_GIGE_PCICFG + offset, value); 106aab547ceSMichael Buesch } 107aab547ceSMichael Buesch 108aab547ceSMichael Buesch static int ssb_gige_pci_read_config(struct pci_bus *bus, unsigned int devfn, 109aab547ceSMichael Buesch int reg, int size, u32 *val) 110aab547ceSMichael Buesch { 111aab547ceSMichael Buesch struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops); 112aab547ceSMichael Buesch unsigned long flags; 113aab547ceSMichael Buesch 114aab547ceSMichael Buesch if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0)) 115aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND; 116aab547ceSMichael Buesch if (reg >= 256) 117aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND; 118aab547ceSMichael Buesch 119aab547ceSMichael Buesch spin_lock_irqsave(&dev->lock, flags); 120aab547ceSMichael Buesch switch (size) { 121aab547ceSMichael Buesch case 1: 122aab547ceSMichael Buesch *val = gige_pcicfg_read8(dev, reg); 123aab547ceSMichael Buesch break; 124aab547ceSMichael Buesch case 2: 125aab547ceSMichael Buesch *val = gige_pcicfg_read16(dev, reg); 126aab547ceSMichael Buesch break; 127aab547ceSMichael Buesch case 4: 128aab547ceSMichael Buesch *val = gige_pcicfg_read32(dev, reg); 129aab547ceSMichael Buesch break; 130aab547ceSMichael Buesch default: 131aab547ceSMichael Buesch WARN_ON(1); 132aab547ceSMichael Buesch } 133aab547ceSMichael Buesch spin_unlock_irqrestore(&dev->lock, flags); 134aab547ceSMichael Buesch 135aab547ceSMichael Buesch return PCIBIOS_SUCCESSFUL; 136aab547ceSMichael Buesch } 137aab547ceSMichael Buesch 138aab547ceSMichael Buesch static int ssb_gige_pci_write_config(struct pci_bus *bus, unsigned int devfn, 139aab547ceSMichael Buesch int reg, int size, u32 val) 140aab547ceSMichael Buesch { 141aab547ceSMichael Buesch struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops); 142aab547ceSMichael Buesch unsigned long flags; 143aab547ceSMichael Buesch 144aab547ceSMichael Buesch if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0)) 145aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND; 146aab547ceSMichael Buesch if (reg >= 256) 147aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND; 148aab547ceSMichael Buesch 149aab547ceSMichael Buesch spin_lock_irqsave(&dev->lock, flags); 150aab547ceSMichael Buesch switch (size) { 151aab547ceSMichael Buesch case 1: 152aab547ceSMichael Buesch gige_pcicfg_write8(dev, reg, val); 153aab547ceSMichael Buesch break; 154aab547ceSMichael Buesch case 2: 155aab547ceSMichael Buesch gige_pcicfg_write16(dev, reg, val); 156aab547ceSMichael Buesch break; 157aab547ceSMichael Buesch case 4: 158aab547ceSMichael Buesch gige_pcicfg_write32(dev, reg, val); 159aab547ceSMichael Buesch break; 160aab547ceSMichael Buesch default: 161aab547ceSMichael Buesch WARN_ON(1); 162aab547ceSMichael Buesch } 163aab547ceSMichael Buesch spin_unlock_irqrestore(&dev->lock, flags); 164aab547ceSMichael Buesch 165aab547ceSMichael Buesch return PCIBIOS_SUCCESSFUL; 166aab547ceSMichael Buesch } 167aab547ceSMichael Buesch 168aab547ceSMichael Buesch static int ssb_gige_probe(struct ssb_device *sdev, const struct ssb_device_id *id) 169aab547ceSMichael Buesch { 170aab547ceSMichael Buesch struct ssb_gige *dev; 171aab547ceSMichael Buesch u32 base, tmslow, tmshigh; 172aab547ceSMichael Buesch 173aab547ceSMichael Buesch dev = kzalloc(sizeof(*dev), GFP_KERNEL); 174aab547ceSMichael Buesch if (!dev) 175aab547ceSMichael Buesch return -ENOMEM; 176aab547ceSMichael Buesch dev->dev = sdev; 177aab547ceSMichael Buesch 178aab547ceSMichael Buesch spin_lock_init(&dev->lock); 179aab547ceSMichael Buesch dev->pci_controller.pci_ops = &dev->pci_ops; 180aab547ceSMichael Buesch dev->pci_controller.io_resource = &dev->io_resource; 181aab547ceSMichael Buesch dev->pci_controller.mem_resource = &dev->mem_resource; 182aab547ceSMichael Buesch dev->pci_controller.io_map_base = 0x800; 183aab547ceSMichael Buesch dev->pci_ops.read = ssb_gige_pci_read_config; 184aab547ceSMichael Buesch dev->pci_ops.write = ssb_gige_pci_write_config; 185aab547ceSMichael Buesch 186aab547ceSMichael Buesch dev->io_resource.name = SSB_GIGE_IO_RES_NAME; 187aab547ceSMichael Buesch dev->io_resource.start = 0x800; 188aab547ceSMichael Buesch dev->io_resource.end = 0x8FF; 189aab547ceSMichael Buesch dev->io_resource.flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED; 190aab547ceSMichael Buesch 191aab547ceSMichael Buesch if (!ssb_device_is_enabled(sdev)) 192aab547ceSMichael Buesch ssb_device_enable(sdev, 0); 193aab547ceSMichael Buesch 194aab547ceSMichael Buesch /* Setup BAR0. This is a 64k MMIO region. */ 195aab547ceSMichael Buesch base = ssb_admatch_base(ssb_read32(sdev, SSB_ADMATCH1)); 196aab547ceSMichael Buesch gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_0, base); 197aab547ceSMichael Buesch gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_1, 0); 198aab547ceSMichael Buesch 199aab547ceSMichael Buesch dev->mem_resource.name = SSB_GIGE_MEM_RES_NAME; 200aab547ceSMichael Buesch dev->mem_resource.start = base; 201aab547ceSMichael Buesch dev->mem_resource.end = base + 0x10000 - 1; 202aab547ceSMichael Buesch dev->mem_resource.flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED; 203aab547ceSMichael Buesch 204aab547ceSMichael Buesch /* Enable the memory region. */ 205aab547ceSMichael Buesch gige_pcicfg_write16(dev, PCI_COMMAND, 206aab547ceSMichael Buesch gige_pcicfg_read16(dev, PCI_COMMAND) 207aab547ceSMichael Buesch | PCI_COMMAND_MEMORY); 208aab547ceSMichael Buesch 209aab547ceSMichael Buesch /* Write flushing is controlled by the Flush Status Control register. 210aab547ceSMichael Buesch * We want to flush every register write with a timeout and we want 211aab547ceSMichael Buesch * to disable the IRQ mask while flushing to avoid concurrency. 212aab547ceSMichael Buesch * Note that automatic write flushing does _not_ work from 213aab547ceSMichael Buesch * an IRQ handler. The driver must flush manually by reading a register. 214aab547ceSMichael Buesch */ 215aab547ceSMichael Buesch gige_write32(dev, SSB_GIGE_SHIM_FLUSHSTAT, 0x00000068); 216aab547ceSMichael Buesch 217aab547ceSMichael Buesch /* Check if we have an RGMII or GMII PHY-bus. 218aab547ceSMichael Buesch * On RGMII do not bypass the DLLs */ 219aab547ceSMichael Buesch tmslow = ssb_read32(sdev, SSB_TMSLOW); 220aab547ceSMichael Buesch tmshigh = ssb_read32(sdev, SSB_TMSHIGH); 221aab547ceSMichael Buesch if (tmshigh & SSB_GIGE_TMSHIGH_RGMII) { 222aab547ceSMichael Buesch tmslow &= ~SSB_GIGE_TMSLOW_TXBYPASS; 223aab547ceSMichael Buesch tmslow &= ~SSB_GIGE_TMSLOW_RXBYPASS; 224aab547ceSMichael Buesch dev->has_rgmii = 1; 225aab547ceSMichael Buesch } else { 226aab547ceSMichael Buesch tmslow |= SSB_GIGE_TMSLOW_TXBYPASS; 227aab547ceSMichael Buesch tmslow |= SSB_GIGE_TMSLOW_RXBYPASS; 228aab547ceSMichael Buesch dev->has_rgmii = 0; 229aab547ceSMichael Buesch } 230aab547ceSMichael Buesch tmslow |= SSB_GIGE_TMSLOW_DLLEN; 231aab547ceSMichael Buesch ssb_write32(sdev, SSB_TMSLOW, tmslow); 232aab547ceSMichael Buesch 233aab547ceSMichael Buesch ssb_set_drvdata(sdev, dev); 234aab547ceSMichael Buesch register_pci_controller(&dev->pci_controller); 235aab547ceSMichael Buesch 236aab547ceSMichael Buesch return 0; 237aab547ceSMichael Buesch } 238aab547ceSMichael Buesch 239aab547ceSMichael Buesch bool pdev_is_ssb_gige_core(struct pci_dev *pdev) 240aab547ceSMichael Buesch { 241aab547ceSMichael Buesch if (!pdev->resource[0].name) 242aab547ceSMichael Buesch return 0; 243aab547ceSMichael Buesch return (strcmp(pdev->resource[0].name, SSB_GIGE_MEM_RES_NAME) == 0); 244aab547ceSMichael Buesch } 245aab547ceSMichael Buesch EXPORT_SYMBOL(pdev_is_ssb_gige_core); 246aab547ceSMichael Buesch 247aab547ceSMichael Buesch int ssb_gige_pcibios_plat_dev_init(struct ssb_device *sdev, 248aab547ceSMichael Buesch struct pci_dev *pdev) 249aab547ceSMichael Buesch { 250aab547ceSMichael Buesch struct ssb_gige *dev = ssb_get_drvdata(sdev); 251aab547ceSMichael Buesch struct resource *res; 252aab547ceSMichael Buesch 253aab547ceSMichael Buesch if (pdev->bus->ops != &dev->pci_ops) { 254aab547ceSMichael Buesch /* The PCI device is not on this SSB GigE bridge device. */ 255aab547ceSMichael Buesch return -ENODEV; 256aab547ceSMichael Buesch } 257aab547ceSMichael Buesch 258aab547ceSMichael Buesch /* Fixup the PCI resources. */ 259aab547ceSMichael Buesch res = &(pdev->resource[0]); 260aab547ceSMichael Buesch res->flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED; 261aab547ceSMichael Buesch res->name = dev->mem_resource.name; 262aab547ceSMichael Buesch res->start = dev->mem_resource.start; 263aab547ceSMichael Buesch res->end = dev->mem_resource.end; 264aab547ceSMichael Buesch 265aab547ceSMichael Buesch /* Fixup interrupt lines. */ 266aab547ceSMichael Buesch pdev->irq = ssb_mips_irq(sdev) + 2; 267aab547ceSMichael Buesch pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, pdev->irq); 268aab547ceSMichael Buesch 269aab547ceSMichael Buesch return 0; 270aab547ceSMichael Buesch } 271aab547ceSMichael Buesch 272aab547ceSMichael Buesch int ssb_gige_map_irq(struct ssb_device *sdev, 273aab547ceSMichael Buesch const struct pci_dev *pdev) 274aab547ceSMichael Buesch { 275aab547ceSMichael Buesch struct ssb_gige *dev = ssb_get_drvdata(sdev); 276aab547ceSMichael Buesch 277aab547ceSMichael Buesch if (pdev->bus->ops != &dev->pci_ops) { 278aab547ceSMichael Buesch /* The PCI device is not on this SSB GigE bridge device. */ 279aab547ceSMichael Buesch return -ENODEV; 280aab547ceSMichael Buesch } 281aab547ceSMichael Buesch 282aab547ceSMichael Buesch return ssb_mips_irq(sdev) + 2; 283aab547ceSMichael Buesch } 284aab547ceSMichael Buesch 285aab547ceSMichael Buesch static struct ssb_driver ssb_gige_driver = { 286aab547ceSMichael Buesch .name = "BCM-GigE", 287aab547ceSMichael Buesch .id_table = ssb_gige_tbl, 288aab547ceSMichael Buesch .probe = ssb_gige_probe, 289aab547ceSMichael Buesch }; 290aab547ceSMichael Buesch 291aab547ceSMichael Buesch int ssb_gige_init(void) 292aab547ceSMichael Buesch { 293aab547ceSMichael Buesch return ssb_driver_register(&ssb_gige_driver); 294aab547ceSMichael Buesch } 295