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