1473c88f9SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2473c88f9SBruce Richardson * Copyright(c) 2010-2019 Intel Corporation
3473c88f9SBruce Richardson */
4473c88f9SBruce Richardson
5473c88f9SBruce Richardson #include "opae_osdep.h"
6473c88f9SBruce Richardson #include "opae_eth_group.h"
7473c88f9SBruce Richardson
8473c88f9SBruce Richardson #define DATA_VAL_INVL 1 /* us */
9473c88f9SBruce Richardson #define DATA_VAL_POLL_TIMEOUT 10 /* us */
10473c88f9SBruce Richardson
eth_type_to_string(u8 type)11473c88f9SBruce Richardson static const char *eth_type_to_string(u8 type)
12473c88f9SBruce Richardson {
13473c88f9SBruce Richardson switch (type) {
14473c88f9SBruce Richardson case ETH_GROUP_PHY:
15473c88f9SBruce Richardson return "phy";
16473c88f9SBruce Richardson case ETH_GROUP_MAC:
17473c88f9SBruce Richardson return "mac";
18473c88f9SBruce Richardson case ETH_GROUP_ETHER:
19473c88f9SBruce Richardson return "ethernet wrapper";
20473c88f9SBruce Richardson }
21473c88f9SBruce Richardson
22473c88f9SBruce Richardson return "unknown";
23473c88f9SBruce Richardson }
24473c88f9SBruce Richardson
eth_group_get_select(struct eth_group_device * dev,u8 type,u8 index,u8 * select)25473c88f9SBruce Richardson static int eth_group_get_select(struct eth_group_device *dev,
26473c88f9SBruce Richardson u8 type, u8 index, u8 *select)
27473c88f9SBruce Richardson {
28473c88f9SBruce Richardson /*
29473c88f9SBruce Richardson * in different speed configuration, the index of
30473c88f9SBruce Richardson * PHY and MAC are different.
31473c88f9SBruce Richardson *
32473c88f9SBruce Richardson * 1 ethernet wrapper -> Device Select 0x0 - fixed value
33473c88f9SBruce Richardson * n PHYs -> Device Select 0x2,4,6,8,A,C,E,10,...
34473c88f9SBruce Richardson * n MACs -> Device Select 0x3,5,7,9,B,D,F,11,...
35473c88f9SBruce Richardson */
36473c88f9SBruce Richardson
37473c88f9SBruce Richardson if (type == ETH_GROUP_PHY && index < dev->phy_num)
38473c88f9SBruce Richardson *select = index * 2 + 2;
39473c88f9SBruce Richardson else if (type == ETH_GROUP_MAC && index < dev->mac_num)
40473c88f9SBruce Richardson *select = index * 2 + 3;
41473c88f9SBruce Richardson else if (type == ETH_GROUP_ETHER && index == 0)
42473c88f9SBruce Richardson *select = 0;
43473c88f9SBruce Richardson else
44473c88f9SBruce Richardson return -EINVAL;
45473c88f9SBruce Richardson
46473c88f9SBruce Richardson return 0;
47473c88f9SBruce Richardson }
48473c88f9SBruce Richardson
eth_group_write_reg(struct eth_group_device * dev,u8 type,u8 index,u16 addr,u32 data)49473c88f9SBruce Richardson int eth_group_write_reg(struct eth_group_device *dev,
50473c88f9SBruce Richardson u8 type, u8 index, u16 addr, u32 data)
51473c88f9SBruce Richardson {
52473c88f9SBruce Richardson u8 dev_select = 0;
53473c88f9SBruce Richardson u64 v = 0;
54473c88f9SBruce Richardson int ret;
55473c88f9SBruce Richardson
56473c88f9SBruce Richardson dev_debug(dev, "%s type %s index %u addr 0x%x\n",
57473c88f9SBruce Richardson __func__, eth_type_to_string(type), index, addr);
58473c88f9SBruce Richardson
59473c88f9SBruce Richardson /* find device select */
60473c88f9SBruce Richardson ret = eth_group_get_select(dev, type, index, &dev_select);
61473c88f9SBruce Richardson if (ret)
62473c88f9SBruce Richardson return ret;
63473c88f9SBruce Richardson
64473c88f9SBruce Richardson v = CMD_WR << CTRL_CMD_SHIT |
65473c88f9SBruce Richardson (u64)dev_select << CTRL_DS_SHIFT |
66473c88f9SBruce Richardson (u64)addr << CTRL_ADDR_SHIFT |
67473c88f9SBruce Richardson (data & CTRL_WR_DATA);
68473c88f9SBruce Richardson
69473c88f9SBruce Richardson /* only PHY has additional feature bit */
70473c88f9SBruce Richardson if (type == ETH_GROUP_PHY)
71473c88f9SBruce Richardson v |= CTRL_FEAT_SELECT;
72473c88f9SBruce Richardson
73473c88f9SBruce Richardson opae_writeq(v, dev->base + ETH_GROUP_CTRL);
74473c88f9SBruce Richardson
75473c88f9SBruce Richardson return 0;
76473c88f9SBruce Richardson }
77473c88f9SBruce Richardson
eth_group_read_reg(struct eth_group_device * dev,u8 type,u8 index,u16 addr,u32 * data)78473c88f9SBruce Richardson int eth_group_read_reg(struct eth_group_device *dev,
79473c88f9SBruce Richardson u8 type, u8 index, u16 addr, u32 *data)
80473c88f9SBruce Richardson {
81473c88f9SBruce Richardson u8 dev_select = 0;
82473c88f9SBruce Richardson u64 v = 0;
83473c88f9SBruce Richardson int ret;
84473c88f9SBruce Richardson
85473c88f9SBruce Richardson dev_debug(dev, "%s type %s index %u addr 0x%x\n",
86473c88f9SBruce Richardson __func__, eth_type_to_string(type), index,
87473c88f9SBruce Richardson addr);
88473c88f9SBruce Richardson
89473c88f9SBruce Richardson /* find device select */
90473c88f9SBruce Richardson ret = eth_group_get_select(dev, type, index, &dev_select);
91473c88f9SBruce Richardson if (ret)
92473c88f9SBruce Richardson return ret;
93473c88f9SBruce Richardson
94473c88f9SBruce Richardson v = CMD_RD << CTRL_CMD_SHIT |
95473c88f9SBruce Richardson (u64)dev_select << CTRL_DS_SHIFT |
96473c88f9SBruce Richardson (u64)addr << CTRL_ADDR_SHIFT;
97473c88f9SBruce Richardson
98473c88f9SBruce Richardson /* only PHY has additional feature bit */
99473c88f9SBruce Richardson if (type == ETH_GROUP_PHY)
100473c88f9SBruce Richardson v |= CTRL_FEAT_SELECT;
101473c88f9SBruce Richardson
102473c88f9SBruce Richardson opae_writeq(v, dev->base + ETH_GROUP_CTRL);
103473c88f9SBruce Richardson
104473c88f9SBruce Richardson if (opae_readq_poll_timeout(dev->base + ETH_GROUP_STAT,
105473c88f9SBruce Richardson v, v & STAT_DATA_VAL, DATA_VAL_INVL,
106473c88f9SBruce Richardson DATA_VAL_POLL_TIMEOUT))
107473c88f9SBruce Richardson return -ETIMEDOUT;
108473c88f9SBruce Richardson
109473c88f9SBruce Richardson *data = (v & STAT_RD_DATA);
110473c88f9SBruce Richardson
111473c88f9SBruce Richardson dev_debug(dev, "%s data 0x%x\n", __func__, *data);
112473c88f9SBruce Richardson
113473c88f9SBruce Richardson return 0;
114473c88f9SBruce Richardson }
115473c88f9SBruce Richardson
eth_group_reset_mac(struct eth_group_device * dev,u8 index,bool enable)116473c88f9SBruce Richardson static int eth_group_reset_mac(struct eth_group_device *dev, u8 index,
117473c88f9SBruce Richardson bool enable)
118473c88f9SBruce Richardson {
119473c88f9SBruce Richardson u32 val;
120473c88f9SBruce Richardson int ret;
121473c88f9SBruce Richardson
122473c88f9SBruce Richardson /*
123473c88f9SBruce Richardson * only support 25G & 40G mac reset for now. It uses internal reset.
124473c88f9SBruce Richardson * as PHY and MAC are integrated together, below action will trigger
125473c88f9SBruce Richardson * PHY reset too.
126473c88f9SBruce Richardson */
127473c88f9SBruce Richardson if (dev->speed != 25 && dev->speed != 40)
128473c88f9SBruce Richardson return 0;
129473c88f9SBruce Richardson
130473c88f9SBruce Richardson ret = eth_group_read_reg(dev, ETH_GROUP_MAC, index, MAC_CONFIG,
131473c88f9SBruce Richardson &val);
132473c88f9SBruce Richardson if (ret) {
133473c88f9SBruce Richardson dev_err(dev, "fail to read PHY_CONFIG: %d\n", ret);
134473c88f9SBruce Richardson return ret;
135473c88f9SBruce Richardson }
136473c88f9SBruce Richardson
137473c88f9SBruce Richardson /* skip if mac is in expected state already */
138473c88f9SBruce Richardson if ((((val & MAC_RESET_MASK) == MAC_RESET_MASK) && enable) ||
139473c88f9SBruce Richardson (((val & MAC_RESET_MASK) == 0) && !enable))
140473c88f9SBruce Richardson return 0;
141473c88f9SBruce Richardson
142473c88f9SBruce Richardson if (enable)
143473c88f9SBruce Richardson val |= MAC_RESET_MASK;
144473c88f9SBruce Richardson else
145473c88f9SBruce Richardson val &= ~MAC_RESET_MASK;
146473c88f9SBruce Richardson
147473c88f9SBruce Richardson ret = eth_group_write_reg(dev, ETH_GROUP_MAC, index, MAC_CONFIG,
148473c88f9SBruce Richardson val);
149473c88f9SBruce Richardson if (ret)
150473c88f9SBruce Richardson dev_err(dev, "fail to write PHY_CONFIG: %d\n", ret);
151473c88f9SBruce Richardson
152473c88f9SBruce Richardson return ret;
153473c88f9SBruce Richardson }
154473c88f9SBruce Richardson
eth_group_mac_uinit(struct eth_group_device * dev)155473c88f9SBruce Richardson static void eth_group_mac_uinit(struct eth_group_device *dev)
156473c88f9SBruce Richardson {
157473c88f9SBruce Richardson u8 i;
158473c88f9SBruce Richardson
159473c88f9SBruce Richardson for (i = 0; i < dev->mac_num; i++) {
160473c88f9SBruce Richardson if (eth_group_reset_mac(dev, i, true))
161473c88f9SBruce Richardson dev_err(dev, "fail to disable mac %d\n", i);
162473c88f9SBruce Richardson }
163473c88f9SBruce Richardson }
164473c88f9SBruce Richardson
eth_group_mac_init(struct eth_group_device * dev)165473c88f9SBruce Richardson static int eth_group_mac_init(struct eth_group_device *dev)
166473c88f9SBruce Richardson {
167473c88f9SBruce Richardson int ret;
168473c88f9SBruce Richardson u8 i;
169473c88f9SBruce Richardson
170473c88f9SBruce Richardson for (i = 0; i < dev->mac_num; i++) {
171473c88f9SBruce Richardson ret = eth_group_reset_mac(dev, i, false);
172473c88f9SBruce Richardson if (ret) {
173473c88f9SBruce Richardson dev_err(dev, "fail to enable mac %d\n", i);
174473c88f9SBruce Richardson goto exit;
175473c88f9SBruce Richardson }
176473c88f9SBruce Richardson }
177473c88f9SBruce Richardson
178473c88f9SBruce Richardson return 0;
179473c88f9SBruce Richardson
180473c88f9SBruce Richardson exit:
181473c88f9SBruce Richardson while (i--)
182473c88f9SBruce Richardson eth_group_reset_mac(dev, i, true);
183473c88f9SBruce Richardson
184473c88f9SBruce Richardson return ret;
185473c88f9SBruce Richardson }
186473c88f9SBruce Richardson
eth_group_reset_phy(struct eth_group_device * dev,u8 index,bool enable)187473c88f9SBruce Richardson static int eth_group_reset_phy(struct eth_group_device *dev, u8 index,
188473c88f9SBruce Richardson bool enable)
189473c88f9SBruce Richardson {
190473c88f9SBruce Richardson u32 val;
191473c88f9SBruce Richardson int ret;
192473c88f9SBruce Richardson
193473c88f9SBruce Richardson /* only support 10G PHY reset for now. It uses external reset. */
194473c88f9SBruce Richardson if (dev->speed != 10)
195473c88f9SBruce Richardson return 0;
196473c88f9SBruce Richardson
197473c88f9SBruce Richardson ret = eth_group_read_reg(dev, ETH_GROUP_PHY, index,
198473c88f9SBruce Richardson ADD_PHY_CTRL, &val);
199473c88f9SBruce Richardson if (ret) {
200473c88f9SBruce Richardson dev_err(dev, "fail to read ADD_PHY_CTRL reg: %d\n", ret);
201473c88f9SBruce Richardson return ret;
202473c88f9SBruce Richardson }
203473c88f9SBruce Richardson
204473c88f9SBruce Richardson /* return if PHY is already in expected state */
205473c88f9SBruce Richardson if ((val & PHY_RESET && enable) || (!(val & PHY_RESET) && !enable))
206473c88f9SBruce Richardson return 0;
207473c88f9SBruce Richardson
208473c88f9SBruce Richardson if (enable)
209473c88f9SBruce Richardson val |= PHY_RESET;
210473c88f9SBruce Richardson else
211473c88f9SBruce Richardson val &= ~PHY_RESET;
212473c88f9SBruce Richardson
213473c88f9SBruce Richardson ret = eth_group_write_reg(dev, ETH_GROUP_PHY, index,
214473c88f9SBruce Richardson ADD_PHY_CTRL, val);
215473c88f9SBruce Richardson if (ret)
216473c88f9SBruce Richardson dev_err(dev, "fail to write ADD_PHY_CTRL reg: %d\n", ret);
217473c88f9SBruce Richardson
218473c88f9SBruce Richardson return ret;
219473c88f9SBruce Richardson }
220473c88f9SBruce Richardson
eth_group_phy_init(struct eth_group_device * dev)221473c88f9SBruce Richardson static int eth_group_phy_init(struct eth_group_device *dev)
222473c88f9SBruce Richardson {
223473c88f9SBruce Richardson int ret;
224473c88f9SBruce Richardson int i;
225473c88f9SBruce Richardson
226473c88f9SBruce Richardson for (i = 0; i < dev->phy_num; i++) {
227473c88f9SBruce Richardson ret = eth_group_reset_phy(dev, i, false);
228473c88f9SBruce Richardson if (ret) {
229473c88f9SBruce Richardson dev_err(dev, "fail to enable phy %d\n", i);
230473c88f9SBruce Richardson goto exit;
231473c88f9SBruce Richardson }
232473c88f9SBruce Richardson }
233473c88f9SBruce Richardson
234473c88f9SBruce Richardson return 0;
235473c88f9SBruce Richardson exit:
236473c88f9SBruce Richardson while (i--)
237473c88f9SBruce Richardson eth_group_reset_phy(dev, i, true);
238473c88f9SBruce Richardson
239473c88f9SBruce Richardson return ret;
240473c88f9SBruce Richardson }
241473c88f9SBruce Richardson
eth_group_phy_uinit(struct eth_group_device * dev)242473c88f9SBruce Richardson static void eth_group_phy_uinit(struct eth_group_device *dev)
243473c88f9SBruce Richardson {
244473c88f9SBruce Richardson int i;
245473c88f9SBruce Richardson
246473c88f9SBruce Richardson for (i = 0; i < dev->phy_num; i++) {
247473c88f9SBruce Richardson if (eth_group_reset_phy(dev, i, true))
248473c88f9SBruce Richardson dev_err(dev, "fail to disable phy %d\n", i);
249473c88f9SBruce Richardson }
250473c88f9SBruce Richardson }
251473c88f9SBruce Richardson
eth_group_hw_init(struct eth_group_device * dev)252473c88f9SBruce Richardson static int eth_group_hw_init(struct eth_group_device *dev)
253473c88f9SBruce Richardson {
254473c88f9SBruce Richardson int ret;
255473c88f9SBruce Richardson
256473c88f9SBruce Richardson ret = eth_group_phy_init(dev);
257473c88f9SBruce Richardson if (ret) {
258473c88f9SBruce Richardson dev_err(dev, "fail to init eth group phys\n");
259473c88f9SBruce Richardson return ret;
260473c88f9SBruce Richardson }
261473c88f9SBruce Richardson
262473c88f9SBruce Richardson ret = eth_group_mac_init(dev);
263473c88f9SBruce Richardson if (ret) {
264473c88f9SBruce Richardson dev_err(priv->dev, "fail to init eth group macs\n");
265473c88f9SBruce Richardson goto phy_exit;
266473c88f9SBruce Richardson }
267473c88f9SBruce Richardson
268473c88f9SBruce Richardson return 0;
269473c88f9SBruce Richardson
270473c88f9SBruce Richardson phy_exit:
271473c88f9SBruce Richardson eth_group_phy_uinit(dev);
272473c88f9SBruce Richardson return ret;
273473c88f9SBruce Richardson }
274473c88f9SBruce Richardson
eth_group_hw_uinit(struct eth_group_device * dev)275473c88f9SBruce Richardson static void eth_group_hw_uinit(struct eth_group_device *dev)
276473c88f9SBruce Richardson {
277473c88f9SBruce Richardson eth_group_mac_uinit(dev);
278473c88f9SBruce Richardson eth_group_phy_uinit(dev);
279473c88f9SBruce Richardson }
280473c88f9SBruce Richardson
eth_group_probe(void * base)281473c88f9SBruce Richardson struct eth_group_device *eth_group_probe(void *base)
282473c88f9SBruce Richardson {
283473c88f9SBruce Richardson struct eth_group_device *dev;
284473c88f9SBruce Richardson
285473c88f9SBruce Richardson dev = opae_malloc(sizeof(*dev));
286473c88f9SBruce Richardson if (!dev)
287473c88f9SBruce Richardson return NULL;
288473c88f9SBruce Richardson
289473c88f9SBruce Richardson dev->base = (u8 *)base;
290473c88f9SBruce Richardson
291473c88f9SBruce Richardson dev->info.info = opae_readq(dev->base + ETH_GROUP_INFO);
292473c88f9SBruce Richardson dev->group_id = dev->info.group_id;
293473c88f9SBruce Richardson dev->phy_num = dev->mac_num = dev->info.num_phys;
294473c88f9SBruce Richardson dev->speed = dev->info.speed;
295473c88f9SBruce Richardson
296473c88f9SBruce Richardson dev->status = ETH_GROUP_DEV_ATTACHED;
297473c88f9SBruce Richardson
298473c88f9SBruce Richardson if (eth_group_hw_init(dev)) {
299473c88f9SBruce Richardson dev_err(dev, "eth group hw init fail\n");
300473c88f9SBruce Richardson return NULL;
301473c88f9SBruce Richardson }
302473c88f9SBruce Richardson
303473c88f9SBruce Richardson dev_info(dev, "eth group device %d probe done: phy_num=mac_num:%d, speed=%d\n",
304473c88f9SBruce Richardson dev->group_id, dev->phy_num, dev->speed);
305473c88f9SBruce Richardson
306473c88f9SBruce Richardson return dev;
307473c88f9SBruce Richardson }
308473c88f9SBruce Richardson
eth_group_release(struct eth_group_device * dev)309473c88f9SBruce Richardson void eth_group_release(struct eth_group_device *dev)
310473c88f9SBruce Richardson {
311473c88f9SBruce Richardson if (dev) {
312*79e9ae58STianfei Zhang eth_group_hw_uinit(dev);
313473c88f9SBruce Richardson dev->status = ETH_GROUP_DEV_NOUSED;
314473c88f9SBruce Richardson opae_free(dev);
315473c88f9SBruce Richardson }
316473c88f9SBruce Richardson }
317