Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Meson GXL Internal PHY Driver |
| 4 | * |
| 5 | * Copyright (C) 2015 Amlogic, Inc. All rights reserved. |
| 6 | * Copyright (C) 2016 BayLibre, SAS. All rights reserved. |
| 7 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 8 | */ |
| 9 | #include <config.h> |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Jerome Brunet | 97be817 | 2017-12-12 16:03:55 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 12 | #include <phy.h> |
| 13 | |
Jerome Brunet | 97be817 | 2017-12-12 16:03:55 +0100 | [diff] [blame] | 14 | /* This function is provided to cope with the possible failures of this phy |
| 15 | * during aneg process. When aneg fails, the PHY reports that aneg is done |
| 16 | * but the value found in MII_LPA is wrong: |
| 17 | * - Early failures: MII_LPA is just 0x0001. if MII_EXPANSION reports that |
| 18 | * the link partner (LP) supports aneg but the LP never acked our base |
| 19 | * code word, it is likely that we never sent it to begin with. |
| 20 | * - Late failures: MII_LPA is filled with a value which seems to make sense |
| 21 | * but it actually is not what the LP is advertising. It seems that we |
| 22 | * can detect this using a magic bit in the WOL bank (reg 12 - bit 12). |
| 23 | * If this particular bit is not set when aneg is reported being done, |
| 24 | * it means MII_LPA is likely to be wrong. |
| 25 | * |
| 26 | * In both case, forcing a restart of the aneg process solve the problem. |
| 27 | * When this failure happens, the first retry is usually successful but, |
| 28 | * in some cases, it may take up to 6 retries to get a decent result |
| 29 | */ |
| 30 | int meson_gxl_startup(struct phy_device *phydev) |
| 31 | { |
| 32 | unsigned int retries = 10; |
| 33 | int ret, wol, lpa, exp; |
| 34 | |
| 35 | restart_aneg: |
| 36 | ret = genphy_update_link(phydev); |
| 37 | if (ret) |
| 38 | return ret; |
| 39 | |
| 40 | if (phydev->autoneg == AUTONEG_ENABLE) { |
| 41 | /* Need to access WOL bank, make sure the access is open */ |
| 42 | ret = phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0000); |
| 43 | if (ret) |
| 44 | return ret; |
| 45 | ret = phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0400); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | ret = phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0000); |
| 49 | if (ret) |
| 50 | return ret; |
| 51 | ret = phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0400); |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | |
| 55 | /* Request LPI_STATUS WOL register */ |
| 56 | ret = phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x8D80); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | /* Read LPI_STATUS value */ |
| 61 | wol = phy_read(phydev, MDIO_DEVAD_NONE, 0x15); |
| 62 | if (wol < 0) |
| 63 | return wol; |
| 64 | |
| 65 | lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA); |
| 66 | if (lpa < 0) |
| 67 | return lpa; |
| 68 | |
| 69 | exp = phy_read(phydev, MDIO_DEVAD_NONE, MII_EXPANSION); |
| 70 | if (exp < 0) |
| 71 | return exp; |
| 72 | |
| 73 | if (!(wol & BIT(12)) || |
| 74 | ((exp & EXPANSION_NWAY) && !(lpa & LPA_LPACK))) { |
Wolfgang Denk | 9d328a6 | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 75 | |
Jerome Brunet | 97be817 | 2017-12-12 16:03:55 +0100 | [diff] [blame] | 76 | /* Looks like aneg failed after all */ |
| 77 | if (!retries) { |
| 78 | printf("%s LPA corruption max attempts\n", |
| 79 | phydev->dev->name); |
| 80 | return -ETIMEDOUT; |
| 81 | } |
| 82 | |
| 83 | printf("%s LPA corruption - aneg restart\n", |
| 84 | phydev->dev->name); |
| 85 | |
| 86 | ret = genphy_restart_aneg(phydev); |
| 87 | if (ret) |
| 88 | return ret; |
| 89 | |
| 90 | --retries; |
| 91 | |
| 92 | goto restart_aneg; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return genphy_parse_link(phydev); |
| 97 | } |
| 98 | |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 99 | static int meson_gxl_phy_config(struct phy_device *phydev) |
| 100 | { |
| 101 | /* Enable Analog and DSP register Bank access by */ |
| 102 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0000); |
| 103 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0400); |
| 104 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0000); |
| 105 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x0400); |
| 106 | |
| 107 | /* Write Analog register 23 */ |
| 108 | phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0x8E0D); |
| 109 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x4417); |
| 110 | |
| 111 | /* Enable fractional PLL */ |
| 112 | phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0x0005); |
| 113 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x5C1B); |
| 114 | |
| 115 | /* Program fraction FR_PLL_DIV1 */ |
| 116 | phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0x029A); |
| 117 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x5C1D); |
| 118 | |
| 119 | /* Program fraction FR_PLL_DIV1 */ |
| 120 | phy_write(phydev, MDIO_DEVAD_NONE, 0x17, 0xAAAA); |
| 121 | phy_write(phydev, MDIO_DEVAD_NONE, 0x14, 0x5C1C); |
| 122 | |
| 123 | return genphy_config(phydev); |
| 124 | } |
| 125 | |
Marek Vasut | 9e20316 | 2023-03-19 18:02:57 +0100 | [diff] [blame] | 126 | U_BOOT_PHY_DRIVER(meson_gxl_phy) = { |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 127 | .name = "Meson GXL Internal PHY", |
| 128 | .uid = 0x01814400, |
| 129 | .mask = 0xfffffff0, |
| 130 | .features = PHY_BASIC_FEATURES, |
| 131 | .config = &meson_gxl_phy_config, |
Jerome Brunet | 97be817 | 2017-12-12 16:03:55 +0100 | [diff] [blame] | 132 | .startup = &meson_gxl_startup, |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 133 | .shutdown = &genphy_shutdown, |
| 134 | }; |