blob: 3b036d01c760c2bc8b65df5bd9a5fb61b70102a4 [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.
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +00006 * Copyright 2018 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>
Shaohui Xie0e548d72014-12-30 18:32:04 +080011#include <phy.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010012#include <u-boot/crc.h>
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060013#include <malloc.h>
14#include <asm/byteorder.h>
15#include <fs.h>
Shaohui Xie0e548d72014-12-30 18:32:04 +080016
Shaohui Xie0e548d72014-12-30 18:32:04 +080017#define AQUNTIA_10G_CTL 0x20
18#define AQUNTIA_VENDOR_P1 0xc400
19
20#define AQUNTIA_SPEED_LSB_MASK 0x2000
21#define AQUNTIA_SPEED_MSB_MASK 0x40
22
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +000023#define AQUANTIA_SYSTEM_INTERFACE_SR 0xe812
Alex Marginean7a1dbe22019-11-14 18:28:32 +020024#define AQUANTIA_SYSTEM_INTERFACE_SR_READY BIT(0)
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +000025#define AQUANTIA_VENDOR_PROVISIONING_REG 0xC441
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +000026#define AQUANTIA_FIRMWARE_ID 0x20
27#define AQUANTIA_RESERVED_STATUS 0xc885
28#define AQUANTIA_FIRMWARE_MAJOR_MASK 0xff00
29#define AQUANTIA_FIRMWARE_MINOR_MASK 0xff
30#define AQUANTIA_FIRMWARE_BUILD_MASK 0xf0
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +000031
32#define AQUANTIA_USX_AUTONEG_CONTROL_ENA 0x0008
33#define AQUANTIA_SI_IN_USE_MASK 0x0078
34#define AQUANTIA_SI_USXGMII 0x0018
35
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060036/* registers in MDIO_MMD_VEND1 region */
Alex Marginean7a1dbe22019-11-14 18:28:32 +020037#define AQUANTIA_VND1_GLOBAL_SC 0x000
38#define AQUANTIA_VND1_GLOBAL_SC_LP BIT(0xb)
39
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060040#define GLOBAL_FIRMWARE_ID 0x20
41#define GLOBAL_FAULT 0xc850
42#define GLOBAL_RSTATUS_1 0xc885
43
Florin Chiculita40829fa2019-10-14 17:27:07 +030044#define GLOBAL_ALARM_1 0xcc00
45#define SYSTEM_READY_BIT 0x40
46
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060047#define GLOBAL_STANDARD_CONTROL 0x0
48#define SOFT_RESET BIT(15)
49#define LOW_POWER BIT(11)
50
51#define MAILBOX_CONTROL 0x0200
52#define MAILBOX_EXECUTE BIT(15)
53#define MAILBOX_WRITE BIT(14)
54#define MAILBOX_RESET_CRC BIT(12)
55#define MAILBOX_BUSY BIT(8)
56
57#define MAILBOX_CRC 0x0201
58
59#define MAILBOX_ADDR_MSW 0x0202
60#define MAILBOX_ADDR_LSW 0x0203
61
62#define MAILBOX_DATA_MSW 0x0204
63#define MAILBOX_DATA_LSW 0x0205
64
65#define UP_CONTROL 0xc001
66#define UP_RESET BIT(15)
67#define UP_RUN_STALL_OVERRIDE BIT(6)
68#define UP_RUN_STALL BIT(0)
69
Alex Marginean0e65e4c2019-11-14 18:28:33 +020070#define AQUANTIA_PMA_RX_VENDOR_P1 0xe400
71#define AQUANTIA_PMA_RX_VENDOR_P1_MDI_MSK GENMASK(1, 0)
72/* MDI reversal configured through registers */
73#define AQUANTIA_PMA_RX_VENDOR_P1_MDI_CFG BIT(1)
74/* MDI reversal enabled */
75#define AQUANTIA_PMA_RX_VENDOR_P1_MDI_REV BIT(0)
76
Alex Marginean7a1dbe22019-11-14 18:28:32 +020077/*
78 * global start rate, the protocol associated with this speed is used by default
79 * on SI.
80 */
81#define AQUANTIA_VND1_GSTART_RATE 0x31a
82#define AQUANTIA_VND1_GSTART_RATE_OFF 0
83#define AQUANTIA_VND1_GSTART_RATE_100M 1
84#define AQUANTIA_VND1_GSTART_RATE_1G 2
85#define AQUANTIA_VND1_GSTART_RATE_10G 3
86#define AQUANTIA_VND1_GSTART_RATE_2_5G 4
87#define AQUANTIA_VND1_GSTART_RATE_5G 5
88
89/* SYSCFG registers for 100M, 1G, 2.5G, 5G, 10G */
90#define AQUANTIA_VND1_GSYSCFG_BASE 0x31b
91#define AQUANTIA_VND1_GSYSCFG_100M 0
92#define AQUANTIA_VND1_GSYSCFG_1G 1
93#define AQUANTIA_VND1_GSYSCFG_2_5G 2
94#define AQUANTIA_VND1_GSYSCFG_5G 3
95#define AQUANTIA_VND1_GSYSCFG_10G 4
96
Jeremy Gebbene662c0d2018-09-18 15:49:36 -060097/* addresses of memory segments in the phy */
98#define DRAM_BASE_ADDR 0x3FFE0000
99#define IRAM_BASE_ADDR 0x40000000
100
101/* firmware image format constants */
102#define VERSION_STRING_SIZE 0x40
103#define VERSION_STRING_OFFSET 0x0200
104#define HEADER_OFFSET 0x300
105
Alex Marginean85330a52019-11-14 18:28:31 +0200106/* driver private data */
107#define AQUANTIA_NA 0
108#define AQUANTIA_GEN1 1
109#define AQUANTIA_GEN2 2
110#define AQUANTIA_GEN3 3
111
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600112#pragma pack(1)
113struct fw_header {
114 u8 padding[4];
115 u8 iram_offset[3];
116 u8 iram_size[3];
117 u8 dram_offset[3];
118 u8 dram_size[3];
119};
120
121#pragma pack()
122
123#if defined(CONFIG_PHY_AQUANTIA_UPLOAD_FW)
124static int aquantia_read_fw(u8 **fw_addr, size_t *fw_length)
125{
126 loff_t length, read;
127 int ret;
128 void *addr = NULL;
129
130 *fw_addr = NULL;
131 *fw_length = 0;
132 debug("Loading Acquantia microcode from %s %s\n",
133 CONFIG_PHY_AQUANTIA_FW_PART, CONFIG_PHY_AQUANTIA_FW_NAME);
134 ret = fs_set_blk_dev("mmc", CONFIG_PHY_AQUANTIA_FW_PART, FS_TYPE_ANY);
135 if (ret < 0)
136 goto cleanup;
137
138 ret = fs_size(CONFIG_PHY_AQUANTIA_FW_NAME, &length);
139 if (ret < 0)
140 goto cleanup;
141
142 addr = malloc(length);
143 if (!addr) {
144 ret = -ENOMEM;
145 goto cleanup;
146 }
147
148 ret = fs_set_blk_dev("mmc", CONFIG_PHY_AQUANTIA_FW_PART, FS_TYPE_ANY);
149 if (ret < 0)
150 goto cleanup;
151
152 ret = fs_read(CONFIG_PHY_AQUANTIA_FW_NAME, (ulong)addr, 0, length,
153 &read);
154 if (ret < 0)
155 goto cleanup;
156
157 *fw_addr = addr;
158 *fw_length = length;
159 debug("Found Acquantia microcode.\n");
160
161cleanup:
162 if (ret < 0) {
163 printf("loading firmware file %s %s failed with error %d\n",
164 CONFIG_PHY_AQUANTIA_FW_PART,
165 CONFIG_PHY_AQUANTIA_FW_NAME, ret);
166 free(addr);
167 }
168 return ret;
169}
170
171/* load data into the phy's memory */
172static int aquantia_load_memory(struct phy_device *phydev, u32 addr,
173 const u8 *data, size_t len)
174{
175 size_t pos;
176 u16 crc = 0, up_crc;
177
178 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_CONTROL, MAILBOX_RESET_CRC);
179 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_ADDR_MSW, addr >> 16);
180 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_ADDR_LSW, addr & 0xfffc);
181
182 for (pos = 0; pos < len; pos += min(sizeof(u32), len - pos)) {
183 u32 word = 0;
184
185 memcpy(&word, &data[pos], min(sizeof(u32), len - pos));
186
187 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_DATA_MSW,
188 (word >> 16));
189 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_DATA_LSW,
190 word & 0xffff);
191
192 phy_write(phydev, MDIO_MMD_VEND1, MAILBOX_CONTROL,
193 MAILBOX_EXECUTE | MAILBOX_WRITE);
194
195 /* keep a big endian CRC to match the phy processor */
196 word = cpu_to_be32(word);
197 crc = crc16_ccitt(crc, (u8 *)&word, sizeof(word));
198 }
199
200 up_crc = phy_read(phydev, MDIO_MMD_VEND1, MAILBOX_CRC);
201 if (crc != up_crc) {
202 printf("%s crc mismatch: calculated 0x%04hx phy 0x%04hx\n",
203 phydev->dev->name, crc, up_crc);
204 return -EINVAL;
205 }
206 return 0;
207}
208
209static u32 unpack_u24(const u8 *data)
210{
211 return (data[2] << 16) + (data[1] << 8) + data[0];
212}
213
214static int aquantia_upload_firmware(struct phy_device *phydev)
215{
216 int ret;
217 u8 *addr = NULL;
218 size_t fw_length = 0;
219 u16 calculated_crc, read_crc;
220 char version[VERSION_STRING_SIZE];
221 u32 primary_offset, iram_offset, iram_size, dram_offset, dram_size;
222 const struct fw_header *header;
223
224 ret = aquantia_read_fw(&addr, &fw_length);
225 if (ret != 0)
226 return ret;
227
228 read_crc = (addr[fw_length - 2] << 8) | addr[fw_length - 1];
229 calculated_crc = crc16_ccitt(0, addr, fw_length - 2);
230 if (read_crc != calculated_crc) {
231 printf("%s bad firmware crc: file 0x%04x calculated 0x%04x\n",
232 phydev->dev->name, read_crc, calculated_crc);
233 ret = -EINVAL;
234 goto done;
235 }
236
237 /* Find the DRAM and IRAM sections within the firmware file. */
238 primary_offset = ((addr[9] & 0xf) << 8 | addr[8]) << 12;
239
240 header = (struct fw_header *)&addr[primary_offset + HEADER_OFFSET];
241
242 iram_offset = primary_offset + unpack_u24(header->iram_offset);
243 iram_size = unpack_u24(header->iram_size);
244
245 dram_offset = primary_offset + unpack_u24(header->dram_offset);
246 dram_size = unpack_u24(header->dram_size);
247
248 debug("primary %d iram offset=%d size=%d dram offset=%d size=%d\n",
249 primary_offset, iram_offset, iram_size, dram_offset, dram_size);
250
251 strlcpy(version, (char *)&addr[dram_offset + VERSION_STRING_OFFSET],
252 VERSION_STRING_SIZE);
253 printf("%s loading firmare version '%s'\n", phydev->dev->name, version);
254
255 /* stall the microcprocessor */
256 phy_write(phydev, MDIO_MMD_VEND1, UP_CONTROL,
257 UP_RUN_STALL | UP_RUN_STALL_OVERRIDE);
258
259 debug("loading dram 0x%08x from offset=%d size=%d\n",
260 DRAM_BASE_ADDR, dram_offset, dram_size);
261 ret = aquantia_load_memory(phydev, DRAM_BASE_ADDR, &addr[dram_offset],
262 dram_size);
263 if (ret != 0)
264 goto done;
265
266 debug("loading iram 0x%08x from offset=%d size=%d\n",
267 IRAM_BASE_ADDR, iram_offset, iram_size);
268 ret = aquantia_load_memory(phydev, IRAM_BASE_ADDR, &addr[iram_offset],
269 iram_size);
270 if (ret != 0)
271 goto done;
272
273 /* make sure soft reset and low power mode are clear */
274 phy_write(phydev, MDIO_MMD_VEND1, GLOBAL_STANDARD_CONTROL, 0);
275
276 /* Release the microprocessor. UP_RESET must be held for 100 usec. */
277 phy_write(phydev, MDIO_MMD_VEND1, UP_CONTROL,
278 UP_RUN_STALL | UP_RUN_STALL_OVERRIDE | UP_RESET);
279
280 udelay(100);
281
282 phy_write(phydev, MDIO_MMD_VEND1, UP_CONTROL, UP_RUN_STALL_OVERRIDE);
283
284 printf("%s firmare loading done.\n", phydev->dev->name);
285done:
286 free(addr);
287 return ret;
288}
289#else
290static int aquantia_upload_firmware(struct phy_device *phydev)
291{
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600292 printf("ERROR %s firmware loading disabled.\n", phydev->dev->name);
293 return -1;
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600294}
295#endif
296
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200297struct {
298 u16 syscfg;
299 int cnt;
300 u16 start_rate;
301} aquantia_syscfg[PHY_INTERFACE_MODE_COUNT] = {
302 [PHY_INTERFACE_MODE_SGMII] = {0x04b, AQUANTIA_VND1_GSYSCFG_1G,
303 AQUANTIA_VND1_GSTART_RATE_1G},
304 [PHY_INTERFACE_MODE_SGMII_2500] = {0x144, AQUANTIA_VND1_GSYSCFG_2_5G,
305 AQUANTIA_VND1_GSTART_RATE_2_5G},
306 [PHY_INTERFACE_MODE_XGMII] = {0x100, AQUANTIA_VND1_GSYSCFG_10G,
307 AQUANTIA_VND1_GSTART_RATE_10G},
308 [PHY_INTERFACE_MODE_XFI] = {0x100, AQUANTIA_VND1_GSYSCFG_10G,
309 AQUANTIA_VND1_GSTART_RATE_10G},
310 [PHY_INTERFACE_MODE_USXGMII] = {0x080, AQUANTIA_VND1_GSYSCFG_10G,
311 AQUANTIA_VND1_GSTART_RATE_10G},
312};
313
314static int aquantia_set_proto(struct phy_device *phydev)
315{
316 int i;
317
318 if (!aquantia_syscfg[phydev->interface].cnt)
319 return 0;
320
321 /* set the default rate to enable the SI link */
322 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_GSTART_RATE,
323 aquantia_syscfg[phydev->interface].start_rate);
324
325 /* set selected protocol for all relevant line side link speeds */
326 for (i = 0; i <= aquantia_syscfg[phydev->interface].cnt; i++)
327 phy_write(phydev, MDIO_MMD_VEND1,
328 AQUANTIA_VND1_GSYSCFG_BASE + i,
329 aquantia_syscfg[phydev->interface].syscfg);
330 return 0;
331}
332
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200333static int aquantia_dts_config(struct phy_device *phydev)
334{
335#ifdef CONFIG_DM_ETH
336 ofnode node = phydev->node;
337 u32 prop;
338 u16 reg;
339
340 /* this code only works on gen2 and gen3 PHYs */
341 if (phydev->drv->data != AQUANTIA_GEN2 &&
342 phydev->drv->data != AQUANTIA_GEN3)
343 return -ENOTSUPP;
344
345 if (!ofnode_valid(node))
346 return 0;
347
348 if (!ofnode_read_u32(node, "mdi-reversal", &prop)) {
349 debug("mdi-reversal = %d\n", (int)prop);
350 reg = phy_read(phydev, MDIO_MMD_PMAPMD,
351 AQUANTIA_PMA_RX_VENDOR_P1);
352 reg &= ~AQUANTIA_PMA_RX_VENDOR_P1_MDI_MSK;
353 reg |= AQUANTIA_PMA_RX_VENDOR_P1_MDI_CFG;
354 reg |= prop ? AQUANTIA_PMA_RX_VENDOR_P1_MDI_REV : 0;
355 phy_write(phydev, MDIO_MMD_PMAPMD, AQUANTIA_PMA_RX_VENDOR_P1,
356 reg);
357 }
358
359#endif
360 return 0;
361}
362
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200363static bool aquantia_link_is_up(struct phy_device *phydev)
364{
365 u16 reg, regmask;
366 int devad, regnum;
367
368 /*
369 * On Gen 2 and 3 we have a bit that indicates that both system and
370 * line side are ready for data, use that if possible.
371 */
372 if (phydev->drv->data == AQUANTIA_GEN2 ||
373 phydev->drv->data == AQUANTIA_GEN3) {
374 devad = MDIO_MMD_PHYXS;
375 regnum = AQUANTIA_SYSTEM_INTERFACE_SR;
376 regmask = AQUANTIA_SYSTEM_INTERFACE_SR_READY;
377 } else {
378 devad = MDIO_MMD_AN;
379 regnum = MDIO_STAT1;
380 regmask = MDIO_AN_STAT1_COMPLETE;
381 }
382 /* the register should be latched, do a double read */
383 phy_read(phydev, devad, regnum);
384 reg = phy_read(phydev, devad, regnum);
385
386 return !!(reg & regmask);
387}
388
Shaohui Xie0e548d72014-12-30 18:32:04 +0800389int aquantia_config(struct phy_device *phydev)
390{
Alex Margineand103efb2019-11-14 18:28:30 +0200391 int interface = phydev->interface;
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600392 u32 val, id, rstatus, fault;
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000393 u32 reg_val1 = 0;
Florin Chiculita40829fa2019-10-14 17:27:07 +0300394 int num_retries = 5;
Alex Margineand103efb2019-11-14 18:28:30 +0200395 int usx_an = 0;
Florin Chiculita40829fa2019-10-14 17:27:07 +0300396
397 /*
398 * check if the system is out of reset and init sequence completed.
399 * chip-wide reset for gen1 quad phys takes longer
400 */
401 while (--num_retries) {
402 rstatus = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_ALARM_1);
403 if (rstatus & SYSTEM_READY_BIT)
404 break;
405 mdelay(10);
406 }
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600407
408 id = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_FIRMWARE_ID);
409 rstatus = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_RSTATUS_1);
410 fault = phy_read(phydev, MDIO_MMD_VEND1, GLOBAL_FAULT);
411
412 if (id != 0)
413 printf("%s running firmware version %X.%X.%X\n",
414 phydev->dev->name, (id >> 8), id & 0xff,
415 (rstatus >> 4) & 0xf);
416
417 if (fault != 0)
418 printf("%s fault 0x%04x detected\n", phydev->dev->name, fault);
419
420 if (id == 0 || fault != 0) {
421 int ret;
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600422
Jeremy Gebben6ce0dd82018-09-18 15:49:37 -0600423 ret = aquantia_upload_firmware(phydev);
424 if (ret != 0)
425 return ret;
426 }
Alex Margineand103efb2019-11-14 18:28:30 +0200427 /*
428 * for backward compatibility convert XGMII into either XFI or USX based
429 * on FW config
430 */
431 if (interface == PHY_INTERFACE_MODE_XGMII) {
432 reg_val1 = phy_read(phydev, MDIO_MMD_PHYXS,
433 AQUANTIA_SYSTEM_INTERFACE_SR);
434 if ((reg_val1 & AQUANTIA_SI_IN_USE_MASK) == AQUANTIA_SI_USXGMII)
435 interface = PHY_INTERFACE_MODE_USXGMII;
436 else
437 interface = PHY_INTERFACE_MODE_XFI;
438 }
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600439
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200440 /*
441 * if link is up already we can just use it, otherwise configure
442 * the protocols in the PHY. If link is down set the system
443 * interface protocol to use based on phydev->interface
444 */
445 if (!aquantia_link_is_up(phydev) &&
446 (phydev->drv->data == AQUANTIA_GEN2 ||
447 phydev->drv->data == AQUANTIA_GEN3)) {
448 /* set PHY in low power mode so we can configure protocols */
449 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_GLOBAL_SC,
450 AQUANTIA_VND1_GLOBAL_SC_LP);
451 mdelay(10);
452
453 /* configure protocol based on phydev->interface */
454 aquantia_set_proto(phydev);
Alex Marginean0e65e4c2019-11-14 18:28:33 +0200455 /* apply custom configuration based on DT */
456 aquantia_dts_config(phydev);
Alex Marginean7a1dbe22019-11-14 18:28:32 +0200457
458 /* wake PHY back up */
459 phy_write(phydev, MDIO_MMD_VEND1, AQUANTIA_VND1_GLOBAL_SC, 0);
460 mdelay(10);
461 }
462
Jeremy Gebbene662c0d2018-09-18 15:49:36 -0600463 val = phy_read(phydev, MDIO_MMD_PMAPMD, MII_BMCR);
Shaohui Xie0e548d72014-12-30 18:32:04 +0800464
Alex Margineand103efb2019-11-14 18:28:30 +0200465 switch (interface) {
466 case PHY_INTERFACE_MODE_SGMII:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800467 /* 1000BASE-T mode */
468 phydev->advertising = SUPPORTED_1000baseT_Full;
469 phydev->supported = phydev->advertising;
470
471 val = (val & ~AQUNTIA_SPEED_LSB_MASK) | AQUNTIA_SPEED_MSB_MASK;
472 phy_write(phydev, MDIO_MMD_PMAPMD, MII_BMCR, val);
Alex Margineand103efb2019-11-14 18:28:30 +0200473 break;
474 case PHY_INTERFACE_MODE_USXGMII:
475 usx_an = 1;
476 /* FALLTHROUGH */
477 case PHY_INTERFACE_MODE_XFI:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800478 /* 10GBASE-T mode */
479 phydev->advertising = SUPPORTED_10000baseT_Full;
480 phydev->supported = phydev->advertising;
481
482 if (!(val & AQUNTIA_SPEED_LSB_MASK) ||
483 !(val & AQUNTIA_SPEED_MSB_MASK))
484 phy_write(phydev, MDIO_MMD_PMAPMD, MII_BMCR,
485 AQUNTIA_SPEED_LSB_MASK |
486 AQUNTIA_SPEED_MSB_MASK);
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +0000487
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +0000488 /* If SI is USXGMII then start USXGMII autoneg */
Alex Margineand103efb2019-11-14 18:28:30 +0200489 reg_val1 = phy_read(phydev, MDIO_MMD_PHYXS,
490 AQUANTIA_VENDOR_PROVISIONING_REG);
Valentin-catalin Neacsudeef3112019-02-13 09:14:53 +0000491
Alex Margineand103efb2019-11-14 18:28:30 +0200492 if (usx_an) {
Valentin-catalin Neacsudeef3112019-02-13 09:14:53 +0000493 reg_val1 |= AQUANTIA_USX_AUTONEG_CONTROL_ENA;
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000494 printf("%s: system interface USXGMII\n",
495 phydev->dev->name);
496 } else {
Alex Margineand103efb2019-11-14 18:28:30 +0200497 reg_val1 &= ~AQUANTIA_USX_AUTONEG_CONTROL_ENA;
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000498 printf("%s: system interface XFI\n",
499 phydev->dev->name);
Valentin-catalin Neacsu123b24f2018-10-30 09:54:46 +0000500 }
501
Alex Margineand103efb2019-11-14 18:28:30 +0200502 phy_write(phydev, MDIO_MMD_PHYXS,
503 AQUANTIA_VENDOR_PROVISIONING_REG, reg_val1);
504 break;
505 case PHY_INTERFACE_MODE_SGMII_2500:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800506 /* 2.5GBASE-T mode */
507 phydev->advertising = SUPPORTED_1000baseT_Full;
508 phydev->supported = phydev->advertising;
509
510 phy_write(phydev, MDIO_MMD_AN, AQUNTIA_10G_CTL, 1);
511 phy_write(phydev, MDIO_MMD_AN, AQUNTIA_VENDOR_P1, 0x9440);
Alex Margineand103efb2019-11-14 18:28:30 +0200512 break;
513 case PHY_INTERFACE_MODE_MII:
Shaohui Xie0e548d72014-12-30 18:32:04 +0800514 /* 100BASE-TX mode */
515 phydev->advertising = SUPPORTED_100baseT_Full;
516 phydev->supported = phydev->advertising;
517
518 val = (val & ~AQUNTIA_SPEED_MSB_MASK) | AQUNTIA_SPEED_LSB_MASK;
519 phy_write(phydev, MDIO_MMD_PMAPMD, MII_BMCR, val);
Alex Margineand103efb2019-11-14 18:28:30 +0200520 break;
521 };
Valentin-catalin Neacsu78119192018-11-06 12:16:54 +0000522
523 val = phy_read(phydev, MDIO_MMD_VEND1, AQUANTIA_RESERVED_STATUS);
524 reg_val1 = phy_read(phydev, MDIO_MMD_VEND1, AQUANTIA_FIRMWARE_ID);
525
526 printf("%s: %s Firmware Version %x.%x.%x\n", phydev->dev->name,
527 phydev->drv->name,
528 (reg_val1 & AQUANTIA_FIRMWARE_MAJOR_MASK) >> 8,
529 reg_val1 & AQUANTIA_FIRMWARE_MINOR_MASK,
530 (val & AQUANTIA_FIRMWARE_BUILD_MASK) >> 4);
531
Shaohui Xie0e548d72014-12-30 18:32:04 +0800532 return 0;
533}
534
535int aquantia_startup(struct phy_device *phydev)
536{
537 u32 reg, speed;
538 int i = 0;
539
540 phydev->duplex = DUPLEX_FULL;
541
542 /* if the AN is still in progress, wait till timeout. */
543 phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
544 reg = phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
545 if (!(reg & MDIO_AN_STAT1_COMPLETE)) {
546 printf("%s Waiting for PHY auto negotiation to complete",
547 phydev->dev->name);
548 do {
549 udelay(1000);
550 reg = phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
551 if ((i++ % 500) == 0)
552 printf(".");
553 } while (!(reg & MDIO_AN_STAT1_COMPLETE) &&
554 i < (4 * PHY_ANEG_TIMEOUT));
555
556 if (i > PHY_ANEG_TIMEOUT)
557 printf(" TIMEOUT !\n");
558 }
559
560 /* Read twice because link state is latched and a
561 * read moves the current state into the register */
562 phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
563 reg = phy_read(phydev, MDIO_MMD_AN, MDIO_STAT1);
564 if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
565 phydev->link = 0;
566 else
567 phydev->link = 1;
568
569 speed = phy_read(phydev, MDIO_MMD_PMAPMD, MII_BMCR);
570 if (speed & AQUNTIA_SPEED_MSB_MASK) {
571 if (speed & AQUNTIA_SPEED_LSB_MASK)
572 phydev->speed = SPEED_10000;
573 else
574 phydev->speed = SPEED_1000;
575 } else {
576 if (speed & AQUNTIA_SPEED_LSB_MASK)
577 phydev->speed = SPEED_100;
578 else
579 phydev->speed = SPEED_10;
580 }
581
582 return 0;
583}
584
585struct phy_driver aq1202_driver = {
586 .name = "Aquantia AQ1202",
587 .uid = 0x3a1b445,
588 .mask = 0xfffffff0,
589 .features = PHY_10G_FEATURES,
590 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
591 MDIO_MMD_PHYXS | MDIO_MMD_AN |
592 MDIO_MMD_VEND1),
593 .config = &aquantia_config,
594 .startup = &aquantia_startup,
595 .shutdown = &gen10g_shutdown,
596};
597
598struct phy_driver aq2104_driver = {
599 .name = "Aquantia AQ2104",
600 .uid = 0x3a1b460,
601 .mask = 0xfffffff0,
602 .features = PHY_10G_FEATURES,
603 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
604 MDIO_MMD_PHYXS | MDIO_MMD_AN |
605 MDIO_MMD_VEND1),
606 .config = &aquantia_config,
607 .startup = &aquantia_startup,
608 .shutdown = &gen10g_shutdown,
609};
610
611struct phy_driver aqr105_driver = {
612 .name = "Aquantia AQR105",
613 .uid = 0x3a1b4a2,
614 .mask = 0xfffffff0,
615 .features = PHY_10G_FEATURES,
616 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
617 MDIO_MMD_PHYXS | MDIO_MMD_AN |
618 MDIO_MMD_VEND1),
619 .config = &aquantia_config,
620 .startup = &aquantia_startup,
621 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200622 .data = AQUANTIA_GEN1,
Shaohui Xie0e548d72014-12-30 18:32:04 +0800623};
Shaohui Xief6a0e732015-11-10 19:16:33 +0800624
Mingkai Hu602e9b52016-07-01 19:03:23 +0800625struct phy_driver aqr106_driver = {
626 .name = "Aquantia AQR106",
627 .uid = 0x3a1b4d0,
628 .mask = 0xfffffff0,
629 .features = PHY_10G_FEATURES,
630 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
631 MDIO_MMD_PHYXS | MDIO_MMD_AN |
632 MDIO_MMD_VEND1),
633 .config = &aquantia_config,
634 .startup = &aquantia_startup,
635 .shutdown = &gen10g_shutdown,
636};
637
638struct phy_driver aqr107_driver = {
639 .name = "Aquantia AQR107",
640 .uid = 0x3a1b4e0,
641 .mask = 0xfffffff0,
642 .features = PHY_10G_FEATURES,
643 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
644 MDIO_MMD_PHYXS | MDIO_MMD_AN |
645 MDIO_MMD_VEND1),
646 .config = &aquantia_config,
647 .startup = &aquantia_startup,
648 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200649 .data = AQUANTIA_GEN2,
Mingkai Hu602e9b52016-07-01 19:03:23 +0800650};
651
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000652struct phy_driver aqr112_driver = {
653 .name = "Aquantia AQR112",
654 .uid = 0x3a1b660,
655 .mask = 0xfffffff0,
656 .features = PHY_10G_FEATURES,
657 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS |
658 MDIO_MMD_PHYXS | MDIO_MMD_AN |
659 MDIO_MMD_VEND1),
660 .config = &aquantia_config,
661 .startup = &aquantia_startup,
662 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200663 .data = AQUANTIA_GEN3,
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000664};
665
Shaohui Xief6a0e732015-11-10 19:16:33 +0800666struct phy_driver aqr405_driver = {
667 .name = "Aquantia AQR405",
668 .uid = 0x3a1b4b2,
669 .mask = 0xfffffff0,
670 .features = PHY_10G_FEATURES,
671 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS|
672 MDIO_MMD_PHYXS | MDIO_MMD_AN |
673 MDIO_MMD_VEND1),
674 .config = &aquantia_config,
675 .startup = &aquantia_startup,
676 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200677 .data = AQUANTIA_GEN1,
Shaohui Xief6a0e732015-11-10 19:16:33 +0800678};
679
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000680struct phy_driver aqr412_driver = {
681 .name = "Aquantia AQR412",
682 .uid = 0x3a1b710,
683 .mask = 0xfffffff0,
684 .features = PHY_10G_FEATURES,
685 .mmds = (MDIO_MMD_PMAPMD | MDIO_MMD_PCS |
686 MDIO_MMD_PHYXS | MDIO_MMD_AN |
687 MDIO_MMD_VEND1),
688 .config = &aquantia_config,
689 .startup = &aquantia_startup,
690 .shutdown = &gen10g_shutdown,
Alex Marginean85330a52019-11-14 18:28:31 +0200691 .data = AQUANTIA_GEN3,
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000692};
693
Shaohui Xie0e548d72014-12-30 18:32:04 +0800694int phy_aquantia_init(void)
695{
696 phy_register(&aq1202_driver);
697 phy_register(&aq2104_driver);
698 phy_register(&aqr105_driver);
Mingkai Hu602e9b52016-07-01 19:03:23 +0800699 phy_register(&aqr106_driver);
700 phy_register(&aqr107_driver);
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000701 phy_register(&aqr112_driver);
Shaohui Xief6a0e732015-11-10 19:16:33 +0800702 phy_register(&aqr405_driver);
Alexandru Marginean6ad27442019-06-19 12:53:43 +0000703 phy_register(&aqr412_driver);
Shaohui Xie0e548d72014-12-30 18:32:04 +0800704
705 return 0;
706}