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),
27673e2baaSJoe Perches {},
28aab547ceSMichael Buesch };
29aab547ceSMichael Buesch /* MODULE_DEVICE_TABLE(ssb, ssb_gige_tbl); */
30aab547ceSMichael Buesch
31aab547ceSMichael Buesch
gige_read8(struct ssb_gige * dev,u16 offset)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
gige_read16(struct ssb_gige * dev,u16 offset)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
gige_read32(struct ssb_gige * dev,u16 offset)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
gige_write8(struct ssb_gige * dev,u16 offset,u8 value)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
gige_write16(struct ssb_gige * dev,u16 offset,u16 value)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
gige_write32(struct ssb_gige * dev,u16 offset,u32 value)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
gige_pcicfg_read8(struct ssb_gige * dev,unsigned int offset)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
gige_pcicfg_read16(struct ssb_gige * dev,unsigned int offset)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
gige_pcicfg_read32(struct ssb_gige * dev,unsigned int offset)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
gige_pcicfg_write8(struct ssb_gige * dev,unsigned int offset,u8 value)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
gige_pcicfg_write16(struct ssb_gige * dev,unsigned int offset,u16 value)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
gige_pcicfg_write32(struct ssb_gige * dev,unsigned int offset,u32 value)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
ssb_gige_pci_read_config(struct pci_bus * bus,unsigned int devfn,int reg,int size,u32 * val)110163247c1SGreg Kroah-Hartman static int ssb_gige_pci_read_config(struct pci_bus *bus, unsigned int devfn,
111163247c1SGreg Kroah-Hartman int reg, int size, u32 *val)
112aab547ceSMichael Buesch {
113aab547ceSMichael Buesch struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
114aab547ceSMichael Buesch unsigned long flags;
115aab547ceSMichael Buesch
116aab547ceSMichael Buesch if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
117aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND;
118aab547ceSMichael Buesch if (reg >= 256)
119aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND;
120aab547ceSMichael Buesch
121aab547ceSMichael Buesch spin_lock_irqsave(&dev->lock, flags);
122aab547ceSMichael Buesch switch (size) {
123aab547ceSMichael Buesch case 1:
124aab547ceSMichael Buesch *val = gige_pcicfg_read8(dev, reg);
125aab547ceSMichael Buesch break;
126aab547ceSMichael Buesch case 2:
127aab547ceSMichael Buesch *val = gige_pcicfg_read16(dev, reg);
128aab547ceSMichael Buesch break;
129aab547ceSMichael Buesch case 4:
130aab547ceSMichael Buesch *val = gige_pcicfg_read32(dev, reg);
131aab547ceSMichael Buesch break;
132aab547ceSMichael Buesch default:
133aab547ceSMichael Buesch WARN_ON(1);
134aab547ceSMichael Buesch }
135aab547ceSMichael Buesch spin_unlock_irqrestore(&dev->lock, flags);
136aab547ceSMichael Buesch
137aab547ceSMichael Buesch return PCIBIOS_SUCCESSFUL;
138aab547ceSMichael Buesch }
139aab547ceSMichael Buesch
ssb_gige_pci_write_config(struct pci_bus * bus,unsigned int devfn,int reg,int size,u32 val)140163247c1SGreg Kroah-Hartman static int ssb_gige_pci_write_config(struct pci_bus *bus, unsigned int devfn,
141163247c1SGreg Kroah-Hartman int reg, int size, u32 val)
142aab547ceSMichael Buesch {
143aab547ceSMichael Buesch struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
144aab547ceSMichael Buesch unsigned long flags;
145aab547ceSMichael Buesch
146aab547ceSMichael Buesch if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
147aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND;
148aab547ceSMichael Buesch if (reg >= 256)
149aab547ceSMichael Buesch return PCIBIOS_DEVICE_NOT_FOUND;
150aab547ceSMichael Buesch
151aab547ceSMichael Buesch spin_lock_irqsave(&dev->lock, flags);
152aab547ceSMichael Buesch switch (size) {
153aab547ceSMichael Buesch case 1:
154aab547ceSMichael Buesch gige_pcicfg_write8(dev, reg, val);
155aab547ceSMichael Buesch break;
156aab547ceSMichael Buesch case 2:
157aab547ceSMichael Buesch gige_pcicfg_write16(dev, reg, val);
158aab547ceSMichael Buesch break;
159aab547ceSMichael Buesch case 4:
160aab547ceSMichael Buesch gige_pcicfg_write32(dev, reg, val);
161aab547ceSMichael Buesch break;
162aab547ceSMichael Buesch default:
163aab547ceSMichael Buesch WARN_ON(1);
164aab547ceSMichael Buesch }
165aab547ceSMichael Buesch spin_unlock_irqrestore(&dev->lock, flags);
166aab547ceSMichael Buesch
167aab547ceSMichael Buesch return PCIBIOS_SUCCESSFUL;
168aab547ceSMichael Buesch }
169aab547ceSMichael Buesch
ssb_gige_probe(struct ssb_device * sdev,const struct ssb_device_id * id)170163247c1SGreg Kroah-Hartman static int ssb_gige_probe(struct ssb_device *sdev,
171cd155987SHauke Mehrtens const struct ssb_device_id *id)
172aab547ceSMichael Buesch {
173aab547ceSMichael Buesch struct ssb_gige *dev;
174aab547ceSMichael Buesch u32 base, tmslow, tmshigh;
175aab547ceSMichael Buesch
176aab547ceSMichael Buesch dev = kzalloc(sizeof(*dev), GFP_KERNEL);
177aab547ceSMichael Buesch if (!dev)
178aab547ceSMichael Buesch return -ENOMEM;
179aab547ceSMichael Buesch dev->dev = sdev;
180aab547ceSMichael Buesch
181aab547ceSMichael Buesch spin_lock_init(&dev->lock);
182aab547ceSMichael Buesch dev->pci_controller.pci_ops = &dev->pci_ops;
183aab547ceSMichael Buesch dev->pci_controller.io_resource = &dev->io_resource;
184aab547ceSMichael Buesch dev->pci_controller.mem_resource = &dev->mem_resource;
185aab547ceSMichael Buesch dev->pci_controller.io_map_base = 0x800;
186aab547ceSMichael Buesch dev->pci_ops.read = ssb_gige_pci_read_config;
187aab547ceSMichael Buesch dev->pci_ops.write = ssb_gige_pci_write_config;
188aab547ceSMichael Buesch
189aab547ceSMichael Buesch dev->io_resource.name = SSB_GIGE_IO_RES_NAME;
190aab547ceSMichael Buesch dev->io_resource.start = 0x800;
191aab547ceSMichael Buesch dev->io_resource.end = 0x8FF;
192aab547ceSMichael Buesch dev->io_resource.flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED;
193aab547ceSMichael Buesch
194aab547ceSMichael Buesch if (!ssb_device_is_enabled(sdev))
195aab547ceSMichael Buesch ssb_device_enable(sdev, 0);
196aab547ceSMichael Buesch
197aab547ceSMichael Buesch /* Setup BAR0. This is a 64k MMIO region. */
198aab547ceSMichael Buesch base = ssb_admatch_base(ssb_read32(sdev, SSB_ADMATCH1));
199aab547ceSMichael Buesch gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_0, base);
200aab547ceSMichael Buesch gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_1, 0);
201aab547ceSMichael Buesch
202aab547ceSMichael Buesch dev->mem_resource.name = SSB_GIGE_MEM_RES_NAME;
203aab547ceSMichael Buesch dev->mem_resource.start = base;
204aab547ceSMichael Buesch dev->mem_resource.end = base + 0x10000 - 1;
205aab547ceSMichael Buesch dev->mem_resource.flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
206aab547ceSMichael Buesch
207aab547ceSMichael Buesch /* Enable the memory region. */
208aab547ceSMichael Buesch gige_pcicfg_write16(dev, PCI_COMMAND,
209aab547ceSMichael Buesch gige_pcicfg_read16(dev, PCI_COMMAND)
210aab547ceSMichael Buesch | PCI_COMMAND_MEMORY);
211aab547ceSMichael Buesch
212aab547ceSMichael Buesch /* Write flushing is controlled by the Flush Status Control register.
213aab547ceSMichael Buesch * We want to flush every register write with a timeout and we want
214aab547ceSMichael Buesch * to disable the IRQ mask while flushing to avoid concurrency.
215aab547ceSMichael Buesch * Note that automatic write flushing does _not_ work from
216aab547ceSMichael Buesch * an IRQ handler. The driver must flush manually by reading a register.
217aab547ceSMichael Buesch */
218aab547ceSMichael Buesch gige_write32(dev, SSB_GIGE_SHIM_FLUSHSTAT, 0x00000068);
219aab547ceSMichael Buesch
220aab547ceSMichael Buesch /* Check if we have an RGMII or GMII PHY-bus.
221aab547ceSMichael Buesch * On RGMII do not bypass the DLLs */
222aab547ceSMichael Buesch tmslow = ssb_read32(sdev, SSB_TMSLOW);
223aab547ceSMichael Buesch tmshigh = ssb_read32(sdev, SSB_TMSHIGH);
224aab547ceSMichael Buesch if (tmshigh & SSB_GIGE_TMSHIGH_RGMII) {
225aab547ceSMichael Buesch tmslow &= ~SSB_GIGE_TMSLOW_TXBYPASS;
226aab547ceSMichael Buesch tmslow &= ~SSB_GIGE_TMSLOW_RXBYPASS;
227aab547ceSMichael Buesch dev->has_rgmii = 1;
228aab547ceSMichael Buesch } else {
229aab547ceSMichael Buesch tmslow |= SSB_GIGE_TMSLOW_TXBYPASS;
230aab547ceSMichael Buesch tmslow |= SSB_GIGE_TMSLOW_RXBYPASS;
231aab547ceSMichael Buesch dev->has_rgmii = 0;
232aab547ceSMichael Buesch }
233aab547ceSMichael Buesch tmslow |= SSB_GIGE_TMSLOW_DLLEN;
234aab547ceSMichael Buesch ssb_write32(sdev, SSB_TMSLOW, tmslow);
235aab547ceSMichael Buesch
236aab547ceSMichael Buesch ssb_set_drvdata(sdev, dev);
237aab547ceSMichael Buesch register_pci_controller(&dev->pci_controller);
238aab547ceSMichael Buesch
239aab547ceSMichael Buesch return 0;
240aab547ceSMichael Buesch }
241aab547ceSMichael Buesch
pdev_is_ssb_gige_core(struct pci_dev * pdev)242aab547ceSMichael Buesch bool pdev_is_ssb_gige_core(struct pci_dev *pdev)
243aab547ceSMichael Buesch {
244aab547ceSMichael Buesch if (!pdev->resource[0].name)
245*c7743c42SGustavo A. R. Silva return false;
246aab547ceSMichael Buesch return (strcmp(pdev->resource[0].name, SSB_GIGE_MEM_RES_NAME) == 0);
247aab547ceSMichael Buesch }
248aab547ceSMichael Buesch EXPORT_SYMBOL(pdev_is_ssb_gige_core);
249aab547ceSMichael Buesch
ssb_gige_pcibios_plat_dev_init(struct ssb_device * sdev,struct pci_dev * pdev)250aab547ceSMichael Buesch int ssb_gige_pcibios_plat_dev_init(struct ssb_device *sdev,
251aab547ceSMichael Buesch struct pci_dev *pdev)
252aab547ceSMichael Buesch {
253aab547ceSMichael Buesch struct ssb_gige *dev = ssb_get_drvdata(sdev);
254aab547ceSMichael Buesch struct resource *res;
255aab547ceSMichael Buesch
256aab547ceSMichael Buesch if (pdev->bus->ops != &dev->pci_ops) {
257aab547ceSMichael Buesch /* The PCI device is not on this SSB GigE bridge device. */
258aab547ceSMichael Buesch return -ENODEV;
259aab547ceSMichael Buesch }
260aab547ceSMichael Buesch
261aab547ceSMichael Buesch /* Fixup the PCI resources. */
262aab547ceSMichael Buesch res = &(pdev->resource[0]);
263aab547ceSMichael Buesch res->flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
264aab547ceSMichael Buesch res->name = dev->mem_resource.name;
265aab547ceSMichael Buesch res->start = dev->mem_resource.start;
266aab547ceSMichael Buesch res->end = dev->mem_resource.end;
267aab547ceSMichael Buesch
268aab547ceSMichael Buesch /* Fixup interrupt lines. */
269aab547ceSMichael Buesch pdev->irq = ssb_mips_irq(sdev) + 2;
270aab547ceSMichael Buesch pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, pdev->irq);
271aab547ceSMichael Buesch
272aab547ceSMichael Buesch return 0;
273aab547ceSMichael Buesch }
274aab547ceSMichael Buesch
ssb_gige_map_irq(struct ssb_device * sdev,const struct pci_dev * pdev)275aab547ceSMichael Buesch int ssb_gige_map_irq(struct ssb_device *sdev,
276aab547ceSMichael Buesch const struct pci_dev *pdev)
277aab547ceSMichael Buesch {
278aab547ceSMichael Buesch struct ssb_gige *dev = ssb_get_drvdata(sdev);
279aab547ceSMichael Buesch
280aab547ceSMichael Buesch if (pdev->bus->ops != &dev->pci_ops) {
281aab547ceSMichael Buesch /* The PCI device is not on this SSB GigE bridge device. */
282aab547ceSMichael Buesch return -ENODEV;
283aab547ceSMichael Buesch }
284aab547ceSMichael Buesch
285aab547ceSMichael Buesch return ssb_mips_irq(sdev) + 2;
286aab547ceSMichael Buesch }
287aab547ceSMichael Buesch
288aab547ceSMichael Buesch static struct ssb_driver ssb_gige_driver = {
289aab547ceSMichael Buesch .name = "BCM-GigE",
290aab547ceSMichael Buesch .id_table = ssb_gige_tbl,
291aab547ceSMichael Buesch .probe = ssb_gige_probe,
292aab547ceSMichael Buesch };
293aab547ceSMichael Buesch
ssb_gige_init(void)294aab547ceSMichael Buesch int ssb_gige_init(void)
295aab547ceSMichael Buesch {
296aab547ceSMichael Buesch return ssb_driver_register(&ssb_gige_driver);
297aab547ceSMichael Buesch }
298