2 * B53 register access through Switch Register Access Bridge Registers
4 * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_data/b53.h>
28 #include "b53_serdes.h"
30 /* command and status register of the SRAB */
31 #define B53_SRAB_CMDSTAT 0x2c
32 #define B53_SRAB_CMDSTAT_RST BIT(2)
33 #define B53_SRAB_CMDSTAT_WRITE BIT(1)
34 #define B53_SRAB_CMDSTAT_GORDYN BIT(0)
35 #define B53_SRAB_CMDSTAT_PAGE 24
36 #define B53_SRAB_CMDSTAT_REG 16
38 /* high order word of write data to switch registe */
39 #define B53_SRAB_WD_H 0x30
41 /* low order word of write data to switch registe */
42 #define B53_SRAB_WD_L 0x34
44 /* high order word of read data from switch register */
45 #define B53_SRAB_RD_H 0x38
47 /* low order word of read data from switch register */
48 #define B53_SRAB_RD_L 0x3c
50 /* command and status register of the SRAB */
51 #define B53_SRAB_CTRLS 0x40
52 #define B53_SRAB_CTRLS_HOST_INTR BIT(1)
53 #define B53_SRAB_CTRLS_RCAREQ BIT(3)
54 #define B53_SRAB_CTRLS_RCAGNT BIT(4)
55 #define B53_SRAB_CTRLS_SW_INIT_DONE BIT(6)
57 /* the register captures interrupt pulses from the switch */
58 #define B53_SRAB_INTR 0x44
59 #define B53_SRAB_INTR_P(x) BIT(x)
60 #define B53_SRAB_SWITCH_PHY BIT(8)
61 #define B53_SRAB_1588_SYNC BIT(9)
62 #define B53_SRAB_IMP1_SLEEP_TIMER BIT(10)
63 #define B53_SRAB_P7_SLEEP_TIMER BIT(11)
64 #define B53_SRAB_IMP0_SLEEP_TIMER BIT(12)
66 /* Port mux configuration registers */
67 #define B53_MUX_CONFIG_P5 0x00
68 #define MUX_CONFIG_SGMII 0
69 #define MUX_CONFIG_MII_LITE 1
70 #define MUX_CONFIG_RGMII 2
71 #define MUX_CONFIG_GMII 3
72 #define MUX_CONFIG_GPHY 4
73 #define MUX_CONFIG_INTERNAL 5
74 #define MUX_CONFIG_MASK 0x7
75 #define B53_MUX_CONFIG_P4 0x04
77 struct b53_srab_port_priv {
80 struct b53_device *dev;
85 struct b53_srab_priv {
87 void __iomem *mux_config;
88 struct b53_srab_port_priv port_intrs[B53_N_PORTS];
91 static int b53_srab_request_grant(struct b53_device *dev)
93 struct b53_srab_priv *priv = dev->priv;
94 u8 __iomem *regs = priv->regs;
98 ctrls = readl(regs + B53_SRAB_CTRLS);
99 ctrls |= B53_SRAB_CTRLS_RCAREQ;
100 writel(ctrls, regs + B53_SRAB_CTRLS);
102 for (i = 0; i < 20; i++) {
103 ctrls = readl(regs + B53_SRAB_CTRLS);
104 if (ctrls & B53_SRAB_CTRLS_RCAGNT)
106 usleep_range(10, 100);
114 static void b53_srab_release_grant(struct b53_device *dev)
116 struct b53_srab_priv *priv = dev->priv;
117 u8 __iomem *regs = priv->regs;
120 ctrls = readl(regs + B53_SRAB_CTRLS);
121 ctrls &= ~B53_SRAB_CTRLS_RCAREQ;
122 writel(ctrls, regs + B53_SRAB_CTRLS);
125 static int b53_srab_op(struct b53_device *dev, u8 page, u8 reg, u32 op)
127 struct b53_srab_priv *priv = dev->priv;
128 u8 __iomem *regs = priv->regs;
132 /* set register address */
133 cmdstat = (page << B53_SRAB_CMDSTAT_PAGE) |
134 (reg << B53_SRAB_CMDSTAT_REG) |
135 B53_SRAB_CMDSTAT_GORDYN |
137 writel(cmdstat, regs + B53_SRAB_CMDSTAT);
139 /* check if operation completed */
140 for (i = 0; i < 5; ++i) {
141 cmdstat = readl(regs + B53_SRAB_CMDSTAT);
142 if (!(cmdstat & B53_SRAB_CMDSTAT_GORDYN))
144 usleep_range(10, 100);
153 static int b53_srab_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
155 struct b53_srab_priv *priv = dev->priv;
156 u8 __iomem *regs = priv->regs;
159 ret = b53_srab_request_grant(dev);
163 ret = b53_srab_op(dev, page, reg, 0);
167 *val = readl(regs + B53_SRAB_RD_L) & 0xff;
170 b53_srab_release_grant(dev);
175 static int b53_srab_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
177 struct b53_srab_priv *priv = dev->priv;
178 u8 __iomem *regs = priv->regs;
181 ret = b53_srab_request_grant(dev);
185 ret = b53_srab_op(dev, page, reg, 0);
189 *val = readl(regs + B53_SRAB_RD_L) & 0xffff;
192 b53_srab_release_grant(dev);
197 static int b53_srab_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
199 struct b53_srab_priv *priv = dev->priv;
200 u8 __iomem *regs = priv->regs;
203 ret = b53_srab_request_grant(dev);
207 ret = b53_srab_op(dev, page, reg, 0);
211 *val = readl(regs + B53_SRAB_RD_L);
214 b53_srab_release_grant(dev);
219 static int b53_srab_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
221 struct b53_srab_priv *priv = dev->priv;
222 u8 __iomem *regs = priv->regs;
225 ret = b53_srab_request_grant(dev);
229 ret = b53_srab_op(dev, page, reg, 0);
233 *val = readl(regs + B53_SRAB_RD_L);
234 *val += ((u64)readl(regs + B53_SRAB_RD_H) & 0xffff) << 32;
237 b53_srab_release_grant(dev);
242 static int b53_srab_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
244 struct b53_srab_priv *priv = dev->priv;
245 u8 __iomem *regs = priv->regs;
248 ret = b53_srab_request_grant(dev);
252 ret = b53_srab_op(dev, page, reg, 0);
256 *val = readl(regs + B53_SRAB_RD_L);
257 *val += (u64)readl(regs + B53_SRAB_RD_H) << 32;
260 b53_srab_release_grant(dev);
265 static int b53_srab_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
267 struct b53_srab_priv *priv = dev->priv;
268 u8 __iomem *regs = priv->regs;
271 ret = b53_srab_request_grant(dev);
275 writel(value, regs + B53_SRAB_WD_L);
277 ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
280 b53_srab_release_grant(dev);
285 static int b53_srab_write16(struct b53_device *dev, u8 page, u8 reg,
288 struct b53_srab_priv *priv = dev->priv;
289 u8 __iomem *regs = priv->regs;
292 ret = b53_srab_request_grant(dev);
296 writel(value, regs + B53_SRAB_WD_L);
298 ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
301 b53_srab_release_grant(dev);
306 static int b53_srab_write32(struct b53_device *dev, u8 page, u8 reg,
309 struct b53_srab_priv *priv = dev->priv;
310 u8 __iomem *regs = priv->regs;
313 ret = b53_srab_request_grant(dev);
317 writel(value, regs + B53_SRAB_WD_L);
319 ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
322 b53_srab_release_grant(dev);
327 static int b53_srab_write48(struct b53_device *dev, u8 page, u8 reg,
330 struct b53_srab_priv *priv = dev->priv;
331 u8 __iomem *regs = priv->regs;
334 ret = b53_srab_request_grant(dev);
338 writel((u32)value, regs + B53_SRAB_WD_L);
339 writel((u16)(value >> 32), regs + B53_SRAB_WD_H);
341 ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
344 b53_srab_release_grant(dev);
349 static int b53_srab_write64(struct b53_device *dev, u8 page, u8 reg,
352 struct b53_srab_priv *priv = dev->priv;
353 u8 __iomem *regs = priv->regs;
356 ret = b53_srab_request_grant(dev);
360 writel((u32)value, regs + B53_SRAB_WD_L);
361 writel((u32)(value >> 32), regs + B53_SRAB_WD_H);
363 ret = b53_srab_op(dev, page, reg, B53_SRAB_CMDSTAT_WRITE);
366 b53_srab_release_grant(dev);
371 static irqreturn_t b53_srab_port_thread(int irq, void *dev_id)
373 struct b53_srab_port_priv *port = dev_id;
374 struct b53_device *dev = port->dev;
376 if (port->mode == PHY_INTERFACE_MODE_SGMII)
377 b53_port_event(dev->ds, port->num);
382 static irqreturn_t b53_srab_port_isr(int irq, void *dev_id)
384 struct b53_srab_port_priv *port = dev_id;
385 struct b53_device *dev = port->dev;
386 struct b53_srab_priv *priv = dev->priv;
388 /* Acknowledge the interrupt */
389 writel(BIT(port->num), priv->regs + B53_SRAB_INTR);
391 return IRQ_WAKE_THREAD;
394 #if IS_ENABLED(CONFIG_B53_SERDES)
395 static u8 b53_srab_serdes_map_lane(struct b53_device *dev, int port)
397 struct b53_srab_priv *priv = dev->priv;
398 struct b53_srab_port_priv *p = &priv->port_intrs[port];
400 if (p->mode != PHY_INTERFACE_MODE_SGMII)
401 return B53_INVALID_LANE;
409 return B53_INVALID_LANE;
414 static int b53_srab_irq_enable(struct b53_device *dev, int port)
416 struct b53_srab_priv *priv = dev->priv;
417 struct b53_srab_port_priv *p = &priv->port_intrs[port];
420 /* Interrupt is optional and was not specified, do not make
423 if (p->irq == -ENXIO)
426 ret = request_threaded_irq(p->irq, b53_srab_port_isr,
427 b53_srab_port_thread, 0,
428 dev_name(dev->dev), p);
430 p->irq_enabled = true;
435 static void b53_srab_irq_disable(struct b53_device *dev, int port)
437 struct b53_srab_priv *priv = dev->priv;
438 struct b53_srab_port_priv *p = &priv->port_intrs[port];
440 if (p->irq_enabled) {
442 p->irq_enabled = false;
446 static const struct b53_io_ops b53_srab_ops = {
447 .read8 = b53_srab_read8,
448 .read16 = b53_srab_read16,
449 .read32 = b53_srab_read32,
450 .read48 = b53_srab_read48,
451 .read64 = b53_srab_read64,
452 .write8 = b53_srab_write8,
453 .write16 = b53_srab_write16,
454 .write32 = b53_srab_write32,
455 .write48 = b53_srab_write48,
456 .write64 = b53_srab_write64,
457 .irq_enable = b53_srab_irq_enable,
458 .irq_disable = b53_srab_irq_disable,
459 #if IS_ENABLED(CONFIG_B53_SERDES)
460 .serdes_map_lane = b53_srab_serdes_map_lane,
461 .serdes_link_state = b53_serdes_link_state,
462 .serdes_config = b53_serdes_config,
463 .serdes_an_restart = b53_serdes_an_restart,
464 .serdes_link_set = b53_serdes_link_set,
465 .serdes_phylink_validate = b53_serdes_phylink_validate,
469 static const struct of_device_id b53_srab_of_match[] = {
470 { .compatible = "brcm,bcm53010-srab" },
471 { .compatible = "brcm,bcm53011-srab" },
472 { .compatible = "brcm,bcm53012-srab" },
473 { .compatible = "brcm,bcm53018-srab" },
474 { .compatible = "brcm,bcm53019-srab" },
475 { .compatible = "brcm,bcm5301x-srab" },
476 { .compatible = "brcm,bcm11360-srab", .data = (void *)BCM583XX_DEVICE_ID },
477 { .compatible = "brcm,bcm58522-srab", .data = (void *)BCM58XX_DEVICE_ID },
478 { .compatible = "brcm,bcm58525-srab", .data = (void *)BCM58XX_DEVICE_ID },
479 { .compatible = "brcm,bcm58535-srab", .data = (void *)BCM58XX_DEVICE_ID },
480 { .compatible = "brcm,bcm58622-srab", .data = (void *)BCM58XX_DEVICE_ID },
481 { .compatible = "brcm,bcm58623-srab", .data = (void *)BCM58XX_DEVICE_ID },
482 { .compatible = "brcm,bcm58625-srab", .data = (void *)BCM58XX_DEVICE_ID },
483 { .compatible = "brcm,bcm88312-srab", .data = (void *)BCM58XX_DEVICE_ID },
484 { .compatible = "brcm,cygnus-srab", .data = (void *)BCM583XX_DEVICE_ID },
485 { .compatible = "brcm,nsp-srab", .data = (void *)BCM58XX_DEVICE_ID },
486 { .compatible = "brcm,omega-srab", .data = (void *)BCM583XX_DEVICE_ID },
489 MODULE_DEVICE_TABLE(of, b53_srab_of_match);
491 static void b53_srab_intr_set(struct b53_srab_priv *priv, bool set)
495 reg = readl(priv->regs + B53_SRAB_CTRLS);
497 reg |= B53_SRAB_CTRLS_HOST_INTR;
499 reg &= ~B53_SRAB_CTRLS_HOST_INTR;
500 writel(reg, priv->regs + B53_SRAB_CTRLS);
503 static void b53_srab_prepare_irq(struct platform_device *pdev)
505 struct b53_device *dev = platform_get_drvdata(pdev);
506 struct b53_srab_priv *priv = dev->priv;
507 struct b53_srab_port_priv *port;
511 /* Clear all pending interrupts */
512 writel(0xffffffff, priv->regs + B53_SRAB_INTR);
514 for (i = 0; i < B53_N_PORTS; i++) {
515 port = &priv->port_intrs[i];
517 /* There is no port 6 */
521 name = kasprintf(GFP_KERNEL, "link_state_p%d", i);
527 port->irq = platform_get_irq_byname_optional(pdev, name);
531 b53_srab_intr_set(priv, true);
534 static void b53_srab_mux_init(struct platform_device *pdev)
536 struct b53_device *dev = platform_get_drvdata(pdev);
537 struct b53_srab_priv *priv = dev->priv;
538 struct b53_srab_port_priv *p;
543 if (dev->pdata && dev->pdata->chip_id != BCM58XX_DEVICE_ID)
546 priv->mux_config = devm_platform_ioremap_resource(pdev, 1);
547 if (IS_ERR(priv->mux_config))
550 /* Obtain the port mux configuration so we know which lanes
551 * actually map to SerDes lanes
553 for (port = 5; port > 3; port--, off += 4) {
554 p = &priv->port_intrs[port];
556 reg = readl(priv->mux_config + B53_MUX_CONFIG_P5 + off);
557 switch (reg & MUX_CONFIG_MASK) {
558 case MUX_CONFIG_SGMII:
559 p->mode = PHY_INTERFACE_MODE_SGMII;
560 ret = b53_serdes_init(dev, port);
564 case MUX_CONFIG_MII_LITE:
565 p->mode = PHY_INTERFACE_MODE_MII;
567 case MUX_CONFIG_GMII:
568 p->mode = PHY_INTERFACE_MODE_GMII;
570 case MUX_CONFIG_RGMII:
571 p->mode = PHY_INTERFACE_MODE_RGMII;
573 case MUX_CONFIG_INTERNAL:
574 p->mode = PHY_INTERFACE_MODE_INTERNAL;
577 p->mode = PHY_INTERFACE_MODE_NA;
581 if (p->mode != PHY_INTERFACE_MODE_NA)
582 dev_info(&pdev->dev, "Port %d mode: %s\n",
583 port, phy_modes(p->mode));
587 static int b53_srab_probe(struct platform_device *pdev)
589 struct b53_platform_data *pdata = pdev->dev.platform_data;
590 struct device_node *dn = pdev->dev.of_node;
591 const struct of_device_id *of_id = NULL;
592 struct b53_srab_priv *priv;
593 struct b53_device *dev;
596 of_id = of_match_node(b53_srab_of_match, dn);
599 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
603 pdata->chip_id = (u32)(unsigned long)of_id->data;
606 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
610 priv->regs = devm_platform_ioremap_resource(pdev, 0);
611 if (IS_ERR(priv->regs))
612 return PTR_ERR(priv->regs);
614 dev = b53_switch_alloc(&pdev->dev, &b53_srab_ops, priv);
621 platform_set_drvdata(pdev, dev);
623 b53_srab_prepare_irq(pdev);
624 b53_srab_mux_init(pdev);
626 return b53_switch_register(dev);
629 static int b53_srab_remove(struct platform_device *pdev)
631 struct b53_device *dev = platform_get_drvdata(pdev);
636 b53_srab_intr_set(dev->priv, false);
637 b53_switch_remove(dev);
639 platform_set_drvdata(pdev, NULL);
644 static void b53_srab_shutdown(struct platform_device *pdev)
646 struct b53_device *dev = platform_get_drvdata(pdev);
651 b53_switch_shutdown(dev);
653 platform_set_drvdata(pdev, NULL);
656 static struct platform_driver b53_srab_driver = {
657 .probe = b53_srab_probe,
658 .remove = b53_srab_remove,
659 .shutdown = b53_srab_shutdown,
661 .name = "b53-srab-switch",
662 .of_match_table = b53_srab_of_match,
666 module_platform_driver(b53_srab_driver);
667 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
668 MODULE_DESCRIPTION("B53 Switch Register Access Bridge Registers (SRAB) access driver");
669 MODULE_LICENSE("Dual BSD/GPL");