blob: 8eb6024829d8e07d4b64c9b18a245022a68dd34b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Shaohui Xie0e548d72014-12-30 18:32:04 +08002/*
3 * Aquantia PHY drivers
4 *
Shaohui Xie0e548d72014-12-30 18:32:04 +08005 * Copyright 2014 Freescale Semiconductor, Inc.
Cosmin-Florin Alucheneseie4beeb52021-07-21 19:13:11 +03006 * Copyright 2018, 2021 NXP
Shaohui Xie0e548d72014-12-30 18:32:04 +08007 */
8#include <config.h>
9#include <common.h>
Calvin Johnsond76b9b72018-03-08 15:30:23 +053010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Shaohui Xie0e548d72014-12-30 18:32:04 +080013#include <phy.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010016#include <u-boot/crc.h>
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060017#include <malloc.h>
18#include <asm/byteorder.h>
19#include <fs.h>
Shaohui Xie0e548d72014-12-30 18:32:04 +080020
Shaohui Xie0e548d72014-12-30 18:32:04 +080021#define AQUNTIA_10G_CTL 0x20
22#define AQUNTIA_VENDOR_P1 0xc400
23
24#define AQUNTIA_SPEED_LSB_MASK 0x2000
25#define AQUNTIA_SPEED_MSB_MASK 0x40
26
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +000027#define AQUANTIA_SYSTEM_INTERFACE_SR 0xe812
Alex Marginean7a1dbe22019-11-14 18:28:32 +020028#define AQUANTIA_SYSTEM_INTERFACE_SR_READY BIT(0)
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +000029#define AQUANTIA_VENDOR_PROVISIONING_REG 0xC441
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +000030#define AQUANTIA_FIRMWARE_ID 0x20
31#define AQUANTIA_RESERVED_STATUS 0xc885
32#define AQUANTIA_FIRMWARE_MAJOR_MASK 0xff00
33#define AQUANTIA_FIRMWARE_MINOR_MASK 0xff
34#define AQUANTIA_FIRMWARE_BUILD_MASK 0xf0
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +000035
36#define AQUANTIA_USX_AUTONEG_CONTROL_ENA 0x0008
37#define AQUANTIA_SI_IN_USE_MASK 0x0078
38#define AQUANTIA_SI_USXGMII 0x0018
39
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060040/* registers in MDIO_MMD_VEND1 region */
Alex Marginean7a1dbe22019-11-14 18:28:32 +020041#define AQUANTIA_VND1_GLOBAL_SC 0x000
42#define AQUANTIA_VND1_GLOBAL_SC_LP BIT(0xb)
43
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060044#define GLOBAL_FIRMWARE_ID 0x20
45#define GLOBAL_FAULT 0xc850
46#define GLOBAL_RSTATUS_1 0xc885
47
Florin Chiculita40829fa2019-10-14 17:27:07 +030048#define GLOBAL_ALARM_1 0xcc00
49#define SYSTEM_READY_BIT 0x40
50
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060051#define GLOBAL_STANDARD_CONTROL 0x0
52#define SOFT_RESET BIT(15)
53#define LOW_POWER BIT(11)
54
55#define MAILBOX_CONTROL 0x0200
56#define MAILBOX_EXECUTE BIT(15)
57#define MAILBOX_WRITE BIT(14)
58#define MAILBOX_RESET_CRC BIT(12)
59#define MAILBOX_BUSY BIT(8)
60
61#define MAILBOX_CRC 0x0201
62
63#define MAILBOX_ADDR_MSW 0x0202
64#define MAILBOX_ADDR_LSW 0x0203
65
66#define MAILBOX_DATA_MSW 0x0204
67#define MAILBOX_DATA_LSW 0x0205
68
69#define UP_CONTROL 0xc001
70#define UP_RESET BIT(15)
71#define UP_RUN_STALL_OVERRIDE BIT(6)
72#define UP_RUN_STALL BIT(0)
73
Alex Marginean0e65e4c2019-11-14 18:28:33 +020074#define AQUANTIA_PMA_RX_VENDOR_P1 0xe400
75#define AQUANTIA_PMA_RX_VENDOR_P1_MDI_MSK GENMASK(1, 0)
76/* MDI reversal configured through registers */
77#define AQUANTIA_PMA_RX_VENDOR_P1_MDI_CFG BIT(1)
78/* MDI reversal enabled */
79#define AQUANTIA_PMA_RX_VENDOR_P1_MDI_REV BIT(0)
80
Alex Marginean7a1dbe22019-11-14 18:28:32 +020081/*
82 * global start rate, the protocol associated with this speed is used by default
83 * on SI.
84 */
85#define AQUANTIA_VND1_GSTART_RATE 0x31a
86#define AQUANTIA_VND1_GSTART_RATE_OFF 0
87#define AQUANTIA_VND1_GSTART_RATE_100M 1
88#define AQUANTIA_VND1_GSTART_RATE_1G 2
89#define AQUANTIA_VND1_GSTART_RATE_10G 3
90#define AQUANTIA_VND1_GSTART_RATE_2_5G 4
91#define AQUANTIA_VND1_GSTART_RATE_5G 5
92
93/* SYSCFG registers for 100M, 1G, 2.5G, 5G, 10G */
94#define AQUANTIA_VND1_GSYSCFG_BASE 0x31b
95#define AQUANTIA_VND1_GSYSCFG_100M 0
96#define AQUANTIA_VND1_GSYSCFG_1G 1
97#define AQUANTIA_VND1_GSYSCFG_2_5G 2
98#define AQUANTIA_VND1_GSYSCFG_5G 3
99#define AQUANTIA_VND1_GSYSCFG_10G 4
100
Alex Margineanb6d61442019-11-14 18:28:34 +0200101#define AQUANTIA_VND1_SMBUS0 0xc485
102#define AQUANTIA_VND1_SMBUS1 0xc495
103
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600104/* addresses of memory segments in the phy */
105#define DRAM_BASE_ADDR 0x3FFE0000
106#define IRAM_BASE_ADDR 0x40000000
107
108/* firmware image format constants */
109#define VERSION_STRING_SIZE 0x40
110#define VERSION_STRING_OFFSET 0x0200
111#define HEADER_OFFSET 0x300
112
Alex Marginean85330a52019-11-14 18:28:31 +0200113/* driver private data */
114#define AQUANTIA_NA 0
115#define AQUANTIA_GEN1 1
116#define AQUANTIA_GEN2 2
117#define AQUANTIA_GEN3 3
118
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600119#pragma pack(1)
120struct fw_header {
121 u8 padding[4];
122 u8 iram_offset[3];
123 u8 iram_size[3];
124 u8 dram_offset[3];
125 u8 dram_size[3];
126};
127
128#pragma pack()
129
130#if defined(CONFIG_PHY_AQUANTIA_UPLOAD_FW)
131static int aquantia_read_fw(u8 **fw_addr, size_t *fw_length)
132{
133 loff_t length, read;
134 int ret;
135 void *addr = NULL;
136
137 *fw_addr = NULL;
138 *fw_length = 0;
Tim Harvey096dff82022-11-03 14:44:22 -0700139 debug("Loading Aquantia microcode from %s %s\n",
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600140 CONFIG_PHY_AQUANTIA_FW_PART, CONFIG_PHY_AQUANTIA_FW_NAME);
141 ret = fs_set_blk_dev("mmc", CONFIG_PHY_AQUANTIA_FW_PART, FS_TYPE_ANY);
142 if (ret < 0)
143 goto cleanup;
144
145 ret = fs_size(CONFIG_PHY_AQUANTIA_FW_NAME, &length);
146 if (ret < 0)
147 goto cleanup;
148
149 addr = malloc(length);
150 if (!addr) {
151 ret = -ENOMEM;
152 goto cleanup;
153 }
154
155 ret = fs_set_blk_dev("mmc", CONFIG_PHY_AQUANTIA_FW_PART, FS_TYPE_ANY);
156 if (ret < 0)
157 goto cleanup;
158
159 ret = fs_read(CONFIG_PHY_AQUANTIA_FW_NAME, (ulong)addr, 0, length,
160 &read);
161 if (ret < 0)
162 goto cleanup;
163
164 *fw_addr = addr;
165 *fw_length = length;
Tim Harvey096dff82022-11-03 14:44:22 -0700166 debug("Found Aquantia microcode.\n");
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600167
168cleanup:
169 if (ret < 0) {
170 printf("loading firmware file %s %s failed with error %d\n",
171 CONFIG_PHY_AQUANTIA_FW_PART,
172 CONFIG_PHY_AQUANTIA_FW_NAME, ret);
173 free(addr);
174 }
175 return ret;
176}
177
178/* load data into the phy's memory */
179static int aquantia_load_memory(struct phy_device *phydev, u32 addr,
180 const u8 *data, size_t len)
181{
182 size_t pos;
183 u16 crc = 0, up_crc;
184
185 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_CONTROL, MAILBOX_RESET_CRC);
186 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_ADDR_MSW, addr >> 16);
187 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_ADDR_LSW, addr & 0xfffc);
188
189 for (pos = 0; pos < len; pos += min(sizeof(u32), len - pos)) {
190 u32 word = 0;
191
192 memcpy(&word, &data[pos], min(sizeof(u32), len - pos));
193
194 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_DATA_MSW,
195 (word >> 16));
196 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_DATA_LSW,
197 word & 0xffff);
198
199 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_CONTROL,
200 MAILBOX_EXECUTE | MAILBOX_WRITE);
201
202 /* keep a big endian CRC to match the phy processor */
203 word = cpu_to_be32(word);
204 crc = crc16_ccitt(crc, (u8 *)&word, sizeof(word));
205 }
206
207 up_crc = phy_read(phydev, MDIO_MMD_VEND1, MAILBOX_CRC);
208 if (crc != up_crc) {
209 printf("%s crc mismatch: calculated 0x%04hx phy 0x%04hx\n",
210 phydev->dev->name, crc, up_crc);
211 return -EINVAL;
212 }
213 return 0;
214}
215
216static u32 unpack_u24(const u8 *data)
217{
218 return (data[2] << 16) + (data[1] << 8) + data[0];
219}
220
221static int aquantia_upload_firmware(struct phy_device *phydev)
222{
223 int ret;
224 u8 *addr = NULL;
225 size_t fw_length = 0;
226 u16 calculated_crc, read_crc;
227 char version[VERSION_STRING_SIZE];
228 u32 primary_offset, iram_offset, iram_size, dram_offset, dram_size;
229 const struct fw_header *header;
230
231 ret = aquantia_read_fw(&addr, &fw_length);
232 if (ret != 0)
233 return ret;
234
235 read_crc = (addr[fw_length - 2] << 8) | addr[fw_length - 1];
236 calculated_crc = crc16_ccitt(0, addr, fw_length - 2);
237 if (read_crc != calculated_crc) {
238 printf("%s bad firmware crc: file 0x%04x calculated 0x%04x\n",
239 phydev->dev->name, read_crc, calculated_crc);
240 ret = -EINVAL;
241 goto done;
242 }
243
244 /* Find the DRAM and IRAM sections within the firmware file. */
245 primary_offset = ((addr[9] & 0xf) << 8 | addr[8]) << 12;
246
247 header = (struct fw_header *)&addr[primary_offset + HEADER_OFFSET];
248
249 iram_offset = primary_offset + unpack_u24(header->iram_offset);
250 iram_size = unpack_u24(header->iram_size);
251
252 dram_offset = primary_offset + unpack_u24(header->dram_offset);
253 dram_size = unpack_u24(header->dram_size);
254
255 debug("primary %d iram offset=%d size=%d dram offset=%d size=%d\n",
256 primary_offset, iram_offset, iram_size, dram_offset, dram_size);
257
258 strlcpy(version, (char *)&addr[dram_offset + VERSION_STRING_OFFSET],
259 VERSION_STRING_SIZE);
Tim Harvey096dff82022-11-03 14:44:22 -0700260 printf("%s loading firmware version '%s'\n", phydev->dev->name, version);
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600261
262 /* stall the microcprocessor */
263 phy_write(phydev, MDIO_MMD_VEND1, UP_CONTROL,
264 UP_RUN_STALL | UP_RUN_STALL_OVERRIDE);
265
266 debug("loading dram 0x%08x from offset=%d size=%d\n",
267 DRAM_BASE_ADDR, dram_offset, dram_size);
268 ret = aquantia_load_memory(phydev, DRAM_BASE_ADDR, &addr[dram_offset],
269 dram_size);
270 if (ret != 0)
271 goto done;
272
273 debug("loading iram 0x%08x from offset=%d size=%d\n",
274 IRAM_BASE_ADDR, iram_offset, iram_size);
275 ret = aquantia_load_memory(phydev, IRAM_BASE_ADDR, &addr[iram_offset],
276 iram_size);
277 if (ret != 0)
278 goto done;
279
280 /* make sure soft reset and low power mode are clear */
281 phy_write(phydev, MDIO_MMD_VEND1, GLOBAL_STANDARD_CONTROL, 0);
282
283 /* Release the microprocessor. UP_RESET must be held for 100 usec. */
284 phy_write(phydev, MDIO_MMD_VEND1, UP_CONTROL,
285 UP_RUN_STALL | UP_RUN_STALL_OVERRIDE | UP_RESET);
286
287 udelay(100);
288
289 phy_write(phydev, MDIO_MMD_VEND1, UP_CONTROL, UP_RUN_STALL_OVERRIDE);
290
Tim Harvey096dff82022-11-03 14:44:22 -0700291 printf("%s firmware loading done.\n", phydev->dev->name);
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600292done:
293 free(addr);
294 return ret;
295}
296#else
297static int aquantia_upload_firmware(struct phy_device *phydev)
298{
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600299 printf("ERROR %s firmware loading disabled.\n", phydev->dev->name);
300 return -1;
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600301}
302#endif
303
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200304struct {
305 u16 syscfg;
306 int cnt;
307 u16 start_rate;
Marek Behún21a18362022-04-07 00:33:02 +0200308} aquantia_syscfg[PHY_INTERFACE_MODE_MAX] = {
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200309 [PHY_INTERFACE_MODE_SGMII] = {0x04b, AQUANTIA_VND1_GSYSCFG_1G,
310 AQUANTIA_VND1_GSTART_RATE_1G},
Vladimir Oltean6caef972021-09-18 15:32:35 +0300311 [PHY_INTERFACE_MODE_2500BASEX] = {0x144, AQUANTIA_VND1_GSYSCFG_2_5G,
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200312 AQUANTIA_VND1_GSTART_RATE_2_5G},
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300313 [PHY_INTERFACE_MODE_10GBASER] = {0x100, AQUANTIA_VND1_GSYSCFG_10G,
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200314 AQUANTIA_VND1_GSTART_RATE_10G},
315 [PHY_INTERFACE_MODE_USXGMII] = {0x080, AQUANTIA_VND1_GSYSCFG_10G,
316 AQUANTIA_VND1_GSTART_RATE_10G},
317};
318
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200319static int aquantia_set_proto(struct phy_device *phydev,
320 phy_interface_t interface)
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200321{
322 int i;
323
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200324 if (!aquantia_syscfg[interface].cnt)
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200325 return 0;
326
327 /* set the default rate to enable the SI link */
328 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_GSTART_RATE,
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200329 aquantia_syscfg[interface].start_rate);
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200330
331 /* set selected protocol for all relevant line side link speeds */
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200332 for (i = 0; i <= aquantia_syscfg[interface].cnt; i++)
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200333 phy_write(phydev, MDIO_MMD_VEND1,
334 AQUANTIA_VND1_GSYSCFG_BASE + i,
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200335 aquantia_syscfg[interface].syscfg);
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200336 return 0;
337}
338
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200339static int aquantia_dts_config(struct phy_device *phydev)
340{
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200341 ofnode node = phydev->node;
342 u32 prop;
343 u16 reg;
344
345 /* this code only works on gen2 and gen3 PHYs */
346 if (phydev->drv->data != AQUANTIA_GEN2 &&
347 phydev->drv->data != AQUANTIA_GEN3)
348 return -ENOTSUPP;
349
350 if (!ofnode_valid(node))
351 return 0;
352
353 if (!ofnode_read_u32(node, "mdi-reversal", &prop)) {
354 debug("mdi-reversal = %d\n", (int)prop);
355 reg = phy_read(phydev, MDIO_MMD_PMAPMD,
356 AQUANTIA_PMA_RX_VENDOR_P1);
357 reg &= ~AQUANTIA_PMA_RX_VENDOR_P1_MDI_MSK;
358 reg |= AQUANTIA_PMA_RX_VENDOR_P1_MDI_CFG;
359 reg |= prop ? AQUANTIA_PMA_RX_VENDOR_P1_MDI_REV : 0;
360 phy_write(phydev, MDIO_MMD_PMAPMD, AQUANTIA_PMA_RX_VENDOR_P1,
361 reg);
362 }
Alex Margineanb6d61442019-11-14 18:28:34 +0200363 if (!ofnode_read_u32(node, "smb-addr", &prop)) {
364 debug("smb-addr = %x\n", (int)prop);
365 /*
366 * there are two addresses here, normally just one bus would
367 * be in use so we're setting both regs using the same DT
368 * property.
369 */
370 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_SMBUS0,
371 (u16)(prop << 1));
372 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_SMBUS1,
373 (u16)(prop << 1));
374 }
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200375
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200376 return 0;
377}
378
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200379static bool aquantia_link_is_up(struct phy_device *phydev)
380{
381 u16 reg, regmask;
382 int devad, regnum;
383
384 /*
385 * On Gen 2 and 3 we have a bit that indicates that both system and
386 * line side are ready for data, use that if possible.
387 */
388 if (phydev->drv->data == AQUANTIA_GEN2 ||
389 phydev->drv->data == AQUANTIA_GEN3) {
390 devad = MDIO_MMD_PHYXS;
391 regnum = AQUANTIA_SYSTEM_INTERFACE_SR;
392 regmask = AQUANTIA_SYSTEM_INTERFACE_SR_READY;
393 } else {
394 devad = MDIO_MMD_AN;
395 regnum = MDIO_STAT1;
396 regmask = MDIO_AN_STAT1_COMPLETE;
397 }
398 /* the register should be latched, do a double read */
399 phy_read(phydev, devad, regnum);
400 reg = phy_read(phydev, devad, regnum);
401
402 return !!(reg & regmask);
403}
404
Shaohui Xie0e548d72014-12-30 18:32:04 +0800405int aquantia_config(struct phy_device *phydev)
406{
Alex Margineand103efb2019-11-14 18:28:30 +0200407 int interface = phydev->interface;
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600408 u32 val, id, rstatus, fault;
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000409 u32 reg_val1 = 0;
Florin Chiculita40829fa2019-10-14 17:27:07 +0300410 int num_retries = 5;
Alex Margineand103efb2019-11-14 18:28:30 +0200411 int usx_an = 0;
Florin Chiculita40829fa2019-10-14 17:27:07 +0300412
413 /*
414 * check if the system is out of reset and init sequence completed.
415 * chip-wide reset for gen1 quad phys takes longer
416 */
417 while (--num_retries) {
418 rstatus = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_ALARM_1);
419 if (rstatus & SYSTEM_READY_BIT)
420 break;
421 mdelay(10);
422 }
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600423
424 id = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_FIRMWARE_ID);
425 rstatus = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_RSTATUS_1);
426 fault = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_FAULT);
427
428 if (id != 0)
Alex Margineanfd101e12019-12-04 15:32:16 +0200429 debug("%s running firmware version %X.%X.%X\n",
430 phydev->dev->name, (id >> 8), id & 0xff,
431 (rstatus >> 4) & 0xf);
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600432
433 if (fault != 0)
434 printf("%s fault 0x%04x detected\n", phydev->dev->name, fault);
435
436 if (id == 0 || fault != 0) {
437 int ret;
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600438
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600439 ret = aquantia_upload_firmware(phydev);
440 if (ret != 0)
441 return ret;
442 }
Alex Margineand103efb2019-11-14 18:28:30 +0200443 /*
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300444 * for backward compatibility convert XGMII into either 10GBase-R or
445 * USXGMII based on FW config
Alex Margineand103efb2019-11-14 18:28:30 +0200446 */
447 if (interface == PHY_INTERFACE_MODE_XGMII) {
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300448 debug("use 10GBase-R or USXGMII SI protos, XGMII is not valid\n");
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200449
Alex Margineand103efb2019-11-14 18:28:30 +0200450 reg_val1 = phy_read(phydev, MDIO_MMD_PHYXS,
451 AQUANTIA_SYSTEM_INTERFACE_SR);
452 if ((reg_val1 & AQUANTIA_SI_IN_USE_MASK) == AQUANTIA_SI_USXGMII)
453 interface = PHY_INTERFACE_MODE_USXGMII;
454 else
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300455 interface = PHY_INTERFACE_MODE_10GBASER;
Alex Margineand103efb2019-11-14 18:28:30 +0200456 }
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600457
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200458 /*
459 * if link is up already we can just use it, otherwise configure
460 * the protocols in the PHY. If link is down set the system
461 * interface protocol to use based on phydev->interface
462 */
463 if (!aquantia_link_is_up(phydev) &&
464 (phydev->drv->data == AQUANTIA_GEN2 ||
465 phydev->drv->data == AQUANTIA_GEN3)) {
466 /* set PHY in low power mode so we can configure protocols */
467 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_GLOBAL_SC,
468 AQUANTIA_VND1_GLOBAL_SC_LP);
469 mdelay(10);
470
471 /* configure protocol based on phydev->interface */
Alex Marginean3fb44cf2019-12-04 15:32:15 +0200472 aquantia_set_proto(phydev, interface);
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200473 /* apply custom configuration based on DT */
474 aquantia_dts_config(phydev);
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200475
476 /* wake PHY back up */
477 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_GLOBAL_SC, 0);
478 mdelay(10);
479 }
480
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600481 val = phy_read(phydev, MDIO_MMD_PMAPMD, MII_BMCR);
Shaohui Xie0e548d72014-12-30 18:32:04 +0800482
Alex Margineand103efb2019-11-14 18:28:30 +0200483 switch (interface) {
484 case PHY_INTERFACE_MODE_SGMII:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800485 /* 1000BASE-T mode */
486 phydev->advertising = SUPPORTED_1000baseT_Full;
487 phydev->supported = phydev->advertising;
488
489 val = (val & ~AQUNTIA_SPEED_LSB_MASK) | AQUNTIA_SPEED_MSB_MASK;
490 phy_write(phydev, MDIO_MMD_PMAPMD, MII_BMCR, val);
Alex Margineand103efb2019-11-14 18:28:30 +0200491 break;
492 case PHY_INTERFACE_MODE_USXGMII:
493 usx_an = 1;
494 /* FALLTHROUGH */
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300495 case PHY_INTERFACE_MODE_10GBASER:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800496 /* 10GBASE-T mode */
497 phydev->advertising = SUPPORTED_10000baseT_Full;
498 phydev->supported = phydev->advertising;
499
500 if (!(val & AQUNTIA_SPEED_LSB_MASK) ||
501 !(val & AQUNTIA_SPEED_MSB_MASK))
502 phy_write(phydev, MDIO_MMD_PMAPMD, MII_BMCR,
503 AQUNTIA_SPEED_LSB_MASK |
504 AQUNTIA_SPEED_MSB_MASK);
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +0000505
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +0000506 /* If SI is USXGMII then start USXGMII autoneg */
Alex Margineand103efb2019-11-14 18:28:30 +0200507 reg_val1 = phy_read(phydev, MDIO_MMD_PHYXS,
508 AQUANTIA_VENDOR_PROVISIONING_REG);
Valentin-catalin Neacsudeef3112019-02-13 09:14:53 +0000509
Alex Margineand103efb2019-11-14 18:28:30 +0200510 if (usx_an) {
Valentin-catalin Neacsudeef3112019-02-13 09:14:53 +0000511 reg_val1 |= AQUANTIA_USX_AUTONEG_CONTROL_ENA;
Alex Margineanfd101e12019-12-04 15:32:16 +0200512 debug("%s: system interface USXGMII\n",
513 phydev->dev->name);
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000514 } else {
Alex Margineand103efb2019-11-14 18:28:30 +0200515 reg_val1 &= ~AQUANTIA_USX_AUTONEG_CONTROL_ENA;
Vladimir Oltean6a6e4022021-09-18 15:32:34 +0300516 debug("%s: system interface 10GBase-R\n",
Alex Margineanfd101e12019-12-04 15:32:16 +0200517 phydev->dev->name);
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +0000518 }
519
Alex Margineand103efb2019-11-14 18:28:30 +0200520 phy_write(phydev, MDIO_MMD_PHYXS,
521 AQUANTIA_VENDOR_PROVISIONING_REG, reg_val1);
522 break;
Vladimir Oltean6caef972021-09-18 15:32:35 +0300523 case PHY_INTERFACE_MODE_2500BASEX:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800524 /* 2.5GBASE-T mode */
525 phydev->advertising = SUPPORTED_1000baseT_Full;
526 phydev->supported = phydev->advertising;
527
528 phy_write(phydev, MDIO_MMD_AN, AQUNTIA_10G_CTL, 1);
529 phy_write(phydev, MDIO_MMD_AN, AQUNTIA_VENDOR_P1, 0x9440);
Alex Margineand103efb2019-11-14 18:28:30 +0200530 break;
531 case PHY_INTERFACE_MODE_MII:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800532 /* 100BASE-TX mode */
533 phydev->advertising = SUPPORTED_100baseT_Full;
534 phydev->supported = phydev->advertising;
535
536 val = (val & ~AQUNTIA_SPEED_MSB_MASK) | AQUNTIA_SPEED_LSB_MASK;
537 phy_write(phydev, MDIO_MMD_PMAPMD, MII_BMCR, val);
Alex Margineand103efb2019-11-14 18:28:30 +0200538 break;
539 };
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000540
541 val = phy_read(phydev, MDIO_MMD_VEND1, AQUANTIA_RESERVED_STATUS);
542 reg_val1 = phy_read(phydev, MDIO_MMD_VEND1, AQUANTIA_FIRMWARE_ID);
543
Alex Margineanfd101e12019-12-04 15:32:16 +0200544 debug("%s: %s Firmware Version %x.%x.%x\n", phydev->dev->name,
545 phydev->drv->name,
546 (reg_val1 & AQUANTIA_FIRMWARE_MAJOR_MASK) >> 8,
547 reg_val1 & AQUANTIA_FIRMWARE_MINOR_MASK,
548 (val & AQUANTIA_FIRMWARE_BUILD_MASK) >> 4);
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000549
Shaohui Xie0e548d72014-12-30 18:32:04 +0800550 return 0;
551}
552
553int aquantia_startup(struct phy_device *phydev)
554{
Cosmin-Florin Alucheneseie4beeb52021-07-21 19:13:11 +0300555 u32 speed;
Shaohui Xie0e548d72014-12-30 18:32:04 +0800556 int i = 0;
Cosmin-Florin Alucheneseie4beeb52021-07-21 19:13:11 +0300557 int reg;
Shaohui Xie0e548d72014-12-30 18:32:04 +0800558
559 phydev->duplex = DUPLEX_FULL;
560
561 /* if the AN is still in progress, wait till timeout. */
Alex Margineanca332892019-11-14 18:28:35 +0200562 if (!aquantia_link_is_up(phydev)) {
Shaohui Xie0e548d72014-12-30 18:32:04 +0800563 printf("%s Waiting for PHY auto negotiation to complete",
564 phydev->dev->name);
565 do {
566 udelay(1000);
Shaohui Xie0e548d72014-12-30 18:32:04 +0800567 if ((i++ % 500) == 0)
568 printf(".");
Alex Margineanca332892019-11-14 18:28:35 +0200569 } while (!aquantia_link_is_up(phydev) &&
Shaohui Xie0e548d72014-12-30 18:32:04 +0800570 i < (4 * PHY_ANEG_TIMEOUT));
571
572 if (i > PHY_ANEG_TIMEOUT)
573 printf(" TIMEOUT !\n");
574 }
575
576 /* Read twice because link state is latched and a
577 * read moves the current state into the register */
578 phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
579 reg = phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
580 if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
581 phydev->link = 0;
582 else
583 phydev->link = 1;
584
585 speed = phy_read(phydev, MDIO_MMD_PMAPMD, MII_BMCR);
586 if (speed & AQUNTIA_SPEED_MSB_MASK) {
587 if (speed & AQUNTIA_SPEED_LSB_MASK)
588 phydev->speed = SPEED_10000;
589 else
590 phydev->speed = SPEED_1000;
591 } else {
592 if (speed & AQUNTIA_SPEED_LSB_MASK)
593 phydev->speed = SPEED_100;
594 else
595 phydev->speed = SPEED_10;
596 }
597
598 return 0;
599}
600
601struct phy_driver aq1202_driver = {
602 .name = "Aquantia AQ1202",
603 .uid = 0x3a1b445,
604 .mask = 0xfffffff0,
605 .features = PHY_10G_FEATURES,
606 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
607 MDIO_MMD_PHYXS | MDIO_MMD_AN |
608 MDIO_MMD_VEND1),
609 .config = &aquantia_config,
610 .startup = &aquantia_startup,
611 .shutdown = &gen10g_shutdown,
612};
613
614struct phy_driver aq2104_driver = {
615 .name = "Aquantia AQ2104",
616 .uid = 0x3a1b460,
617 .mask = 0xfffffff0,
618 .features = PHY_10G_FEATURES,
619 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
620 MDIO_MMD_PHYXS | MDIO_MMD_AN |
621 MDIO_MMD_VEND1),
622 .config = &aquantia_config,
623 .startup = &aquantia_startup,
624 .shutdown = &gen10g_shutdown,
625};
626
627struct phy_driver aqr105_driver = {
628 .name = "Aquantia AQR105",
629 .uid = 0x3a1b4a2,
630 .mask = 0xfffffff0,
631 .features = PHY_10G_FEATURES,
632 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
633 MDIO_MMD_PHYXS | MDIO_MMD_AN |
634 MDIO_MMD_VEND1),
635 .config = &aquantia_config,
636 .startup = &aquantia_startup,
637 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200638 .data = AQUANTIA_GEN1,
Shaohui Xie0e548d72014-12-30 18:32:04 +0800639};
Shaohui Xief6a0e732015-11-10 19:16:33 +0800640
Mingkai Hu602e9b52016-07-01 19:03:23 +0800641struct phy_driver aqr106_driver = {
642 .name = "Aquantia AQR106",
643 .uid = 0x3a1b4d0,
644 .mask = 0xfffffff0,
645 .features = PHY_10G_FEATURES,
646 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
647 MDIO_MMD_PHYXS | MDIO_MMD_AN |
648 MDIO_MMD_VEND1),
649 .config = &aquantia_config,
650 .startup = &aquantia_startup,
651 .shutdown = &gen10g_shutdown,
652};
653
654struct phy_driver aqr107_driver = {
655 .name = "Aquantia AQR107",
656 .uid = 0x3a1b4e0,
657 .mask = 0xfffffff0,
658 .features = PHY_10G_FEATURES,
659 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
660 MDIO_MMD_PHYXS | MDIO_MMD_AN |
661 MDIO_MMD_VEND1),
662 .config = &aquantia_config,
663 .startup = &aquantia_startup,
664 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200665 .data = AQUANTIA_GEN2,
Mingkai Hu602e9b52016-07-01 19:03:23 +0800666};
667
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000668struct phy_driver aqr112_driver = {
669 .name = "Aquantia AQR112",
670 .uid = 0x3a1b660,
671 .mask = 0xfffffff0,
672 .features = PHY_10G_FEATURES,
673 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS |
674 MDIO_MMD_PHYXS | MDIO_MMD_AN |
675 MDIO_MMD_VEND1),
676 .config = &aquantia_config,
677 .startup = &aquantia_startup,
678 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200679 .data = AQUANTIA_GEN3,
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000680};
681
Madalin Bucur537458b2020-09-10 13:23:39 +0300682struct phy_driver aqr113c_driver = {
683 .name = "Aquantia AQR113C",
684 .uid = 0x31c31c12,
685 .mask = 0xfffffff0,
686 .features = PHY_10G_FEATURES,
687 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS |
688 MDIO_MMD_PHYXS | MDIO_MMD_AN |
689 MDIO_MMD_VEND1),
690 .config = &aquantia_config,
691 .startup = &aquantia_startup,
692 .shutdown = &gen10g_shutdown,
693 .data = AQUANTIA_GEN3,
694};
695
Shaohui Xief6a0e732015-11-10 19:16:33 +0800696struct phy_driver aqr405_driver = {
697 .name = "Aquantia AQR405",
698 .uid = 0x3a1b4b2,
699 .mask = 0xfffffff0,
700 .features = PHY_10G_FEATURES,
701 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
702 MDIO_MMD_PHYXS | MDIO_MMD_AN |
703 MDIO_MMD_VEND1),
704 .config = &aquantia_config,
705 .startup = &aquantia_startup,
706 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200707 .data = AQUANTIA_GEN1,
Shaohui Xief6a0e732015-11-10 19:16:33 +0800708};
709
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000710struct phy_driver aqr412_driver = {
711 .name = "Aquantia AQR412",
712 .uid = 0x3a1b710,
713 .mask = 0xfffffff0,
714 .features = PHY_10G_FEATURES,
715 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS |
716 MDIO_MMD_PHYXS | MDIO_MMD_AN |
717 MDIO_MMD_VEND1),
718 .config = &aquantia_config,
719 .startup = &aquantia_startup,
720 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200721 .data = AQUANTIA_GEN3,
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000722};
723
Shaohui Xie0e548d72014-12-30 18:32:04 +0800724int phy_aquantia_init(void)
725{
726 phy_register(&aq1202_driver);
727 phy_register(&aq2104_driver);
728 phy_register(&aqr105_driver);
Mingkai Hu602e9b52016-07-01 19:03:23 +0800729 phy_register(&aqr106_driver);
730 phy_register(&aqr107_driver);
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000731 phy_register(&aqr112_driver);
Madalin Bucur537458b2020-09-10 13:23:39 +0300732 phy_register(&aqr113c_driver);
Shaohui Xief6a0e732015-11-10 19:16:33 +0800733 phy_register(&aqr405_driver);
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000734 phy_register(&aqr412_driver);
Shaohui Xie0e548d72014-12-30 18:32:04 +0800735
736 return 0;
737}