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_SRESET_RESET BIT(0)
22 #define SGMII_SRESET_RTRESET BIT(1)
24 #define SGMII_REG_STATUS_LOCK BIT(4)
25 #define SGMII_REG_STATUS_LINK BIT(0)
26 #define SGMII_REG_STATUS_AUTONEG BIT(2)
27 #define SGMII_REG_CONTROL_AUTONEG BIT(0)
29 #define SGMII23_OFFSET(x) ((x - 2) * 0x100)
30 #define SGMII_OFFSET(x) ((x <= 1) ? (x * 0x100) : (SGMII23_OFFSET(x)))
33 #define SGMII_SRESET_REG(x) (SGMII_OFFSET(x) + 0x004)
34 #define SGMII_CTL_REG(x) (SGMII_OFFSET(x) + 0x010)
35 #define SGMII_STATUS_REG(x) (SGMII_OFFSET(x) + 0x014)
36 #define SGMII_MRADV_REG(x) (SGMII_OFFSET(x) + 0x018)
38 static void sgmii_write_reg(void __iomem
*base
, int reg
, u32 val
)
40 writel(val
, base
+ reg
);
43 static u32
sgmii_read_reg(void __iomem
*base
, int reg
)
45 return readl(base
+ reg
);
48 static void sgmii_write_reg_bit(void __iomem
*base
, int reg
, u32 val
)
50 writel((readl(base
+ reg
) | val
), base
+ reg
);
54 int netcp_sgmii_reset(void __iomem
*sgmii_ofs
, int port
)
57 sgmii_write_reg_bit(sgmii_ofs
, SGMII_SRESET_REG(port
),
60 while ((sgmii_read_reg(sgmii_ofs
, SGMII_SRESET_REG(port
)) &
61 SGMII_SRESET_RESET
) != 0x0)
68 bool netcp_sgmii_rtreset(void __iomem
*sgmii_ofs
, int port
, bool set
)
73 /* Initiate a soft reset */
74 reg
= sgmii_read_reg(sgmii_ofs
, SGMII_SRESET_REG(port
));
75 oldval
= (reg
& SGMII_SRESET_RTRESET
) != 0x0;
77 reg
|= SGMII_SRESET_RTRESET
;
79 reg
&= ~SGMII_SRESET_RTRESET
;
80 sgmii_write_reg(sgmii_ofs
, SGMII_SRESET_REG(port
), reg
);
86 int netcp_sgmii_get_port_link(void __iomem
*sgmii_ofs
, int port
)
88 u32 status
= 0, link
= 0;
90 status
= sgmii_read_reg(sgmii_ofs
, SGMII_STATUS_REG(port
));
91 if ((status
& SGMII_REG_STATUS_LINK
) != 0)
96 int netcp_sgmii_config(void __iomem
*sgmii_ofs
, int port
, u32 interface
)
98 unsigned int i
, status
, mask
;
103 case SGMII_LINK_MAC_MAC_AUTONEG
:
104 mr_adv_ability
= 0x9801;
108 case SGMII_LINK_MAC_PHY
:
109 case SGMII_LINK_MAC_PHY_NO_MDIO
:
114 case SGMII_LINK_MAC_MAC_FORCED
:
115 mr_adv_ability
= 0x9801;
119 case SGMII_LINK_MAC_FIBER
:
120 mr_adv_ability
= 0x20;
125 WARN_ONCE(1, "Invalid sgmii interface: %d\n", interface
);
129 sgmii_write_reg(sgmii_ofs
, SGMII_CTL_REG(port
), 0);
131 /* Wait for the SerDes pll to lock */
132 for (i
= 0; i
< 1000; i
++) {
133 usleep_range(1000, 2000);
134 status
= sgmii_read_reg(sgmii_ofs
, SGMII_STATUS_REG(port
));
135 if ((status
& SGMII_REG_STATUS_LOCK
) != 0)
139 if ((status
& SGMII_REG_STATUS_LOCK
) == 0)
140 pr_err("serdes PLL not locked\n");
142 sgmii_write_reg(sgmii_ofs
, SGMII_MRADV_REG(port
), mr_adv_ability
);
143 sgmii_write_reg(sgmii_ofs
, SGMII_CTL_REG(port
), control
);
145 mask
= SGMII_REG_STATUS_LINK
;
146 if (control
& SGMII_REG_CONTROL_AUTONEG
)
147 mask
|= SGMII_REG_STATUS_AUTONEG
;
149 for (i
= 0; i
< 1000; i
++) {
150 usleep_range(200, 500);
151 status
= sgmii_read_reg(sgmii_ofs
, SGMII_STATUS_REG(port
));
152 if ((status
& mask
) == mask
)