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