2 * SGMI module initialisation
4 * Copyright (C) 2014 Texas Instruments Incorporated
5 * Authors: Sandeep Nair <sandeep_n@ti.com>
6 * Sandeep Paulraj <s-paulraj@ti.com>
7 * Wingman Kwok <w-kwok2@ti.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation version 2.
13 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
14 * kind, whether express or implied; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 #define SGMII_REG_STATUS_LOCK BIT(4)
22 #define SGMII_REG_STATUS_LINK BIT(0)
23 #define SGMII_REG_STATUS_AUTONEG BIT(2)
24 #define SGMII_REG_CONTROL_AUTONEG BIT(0)
26 #define SGMII23_OFFSET(x) ((x - 2) * 0x100)
27 #define SGMII_OFFSET(x) ((x <= 1) ? (x * 0x100) : (SGMII23_OFFSET(x)))
30 #define SGMII_SRESET_REG(x) (SGMII_OFFSET(x) + 0x004)
31 #define SGMII_CTL_REG(x) (SGMII_OFFSET(x) + 0x010)
32 #define SGMII_STATUS_REG(x) (SGMII_OFFSET(x) + 0x014)
33 #define SGMII_MRADV_REG(x) (SGMII_OFFSET(x) + 0x018)
35 static void sgmii_write_reg(void __iomem *base, int reg, u32 val)
37 writel(val, base + reg);
40 static u32 sgmii_read_reg(void __iomem *base, int reg)
42 return readl(base + reg);
45 static void sgmii_write_reg_bit(void __iomem *base, int reg, u32 val)
47 writel((readl(base + reg) | val), base + reg);
51 int netcp_sgmii_reset(void __iomem *sgmii_ofs, int port)
54 sgmii_write_reg_bit(sgmii_ofs, SGMII_SRESET_REG(port), 0x1);
55 while (sgmii_read_reg(sgmii_ofs, SGMII_SRESET_REG(port)) != 0x0)
60 int netcp_sgmii_get_port_link(void __iomem *sgmii_ofs, int port)
62 u32 status = 0, link = 0;
64 status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
65 if ((status & SGMII_REG_STATUS_LINK) != 0)
70 int netcp_sgmii_config(void __iomem *sgmii_ofs, int port, u32 interface)
72 unsigned int i, status, mask;
77 case SGMII_LINK_MAC_MAC_AUTONEG:
78 mr_adv_ability = 0x9801;
82 case SGMII_LINK_MAC_PHY:
83 case SGMII_LINK_MAC_PHY_NO_MDIO:
88 case SGMII_LINK_MAC_MAC_FORCED:
89 mr_adv_ability = 0x9801;
93 case SGMII_LINK_MAC_FIBER:
94 mr_adv_ability = 0x20;
99 WARN_ONCE(1, "Invalid sgmii interface: %d\n", interface);
103 sgmii_write_reg(sgmii_ofs, SGMII_CTL_REG(port), 0);
105 /* Wait for the SerDes pll to lock */
106 for (i = 0; i < 1000; i++) {
107 usleep_range(1000, 2000);
108 status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
109 if ((status & SGMII_REG_STATUS_LOCK) != 0)
113 if ((status & SGMII_REG_STATUS_LOCK) == 0)
114 pr_err("serdes PLL not locked\n");
116 sgmii_write_reg(sgmii_ofs, SGMII_MRADV_REG(port), mr_adv_ability);
117 sgmii_write_reg(sgmii_ofs, SGMII_CTL_REG(port), control);
119 mask = SGMII_REG_STATUS_LINK;
120 if (control & SGMII_REG_CONTROL_AUTONEG)
121 mask |= SGMII_REG_STATUS_AUTONEG;
123 for (i = 0; i < 1000; i++) {
124 usleep_range(200, 500);
125 status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
126 if ((status & mask) == mask)