18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell 88e6xxx Ethernet switch PHY and PPU support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2008 Marvell Semiconductor
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2017 Andrew Lunn <andrew@lunn.ch>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/mdio.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "chip.h"
148c2ecf20Sopenharmony_ci#include "phy.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciint mv88e6165_phy_read(struct mv88e6xxx_chip *chip, struct mii_bus *bus,
178c2ecf20Sopenharmony_ci		       int addr, int reg, u16 *val)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	return mv88e6xxx_read(chip, addr, reg, val);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciint mv88e6165_phy_write(struct mv88e6xxx_chip *chip, struct mii_bus *bus,
238c2ecf20Sopenharmony_ci			int addr, int reg, u16 val)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	return mv88e6xxx_write(chip, addr, reg, val);
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciint mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy, int reg, u16 *val)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	int addr = phy; /* PHY devices addresses start at 0x0 */
318c2ecf20Sopenharmony_ci	struct mii_bus *bus;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	bus = mv88e6xxx_default_mdio_bus(chip);
348c2ecf20Sopenharmony_ci	if (!bus)
358c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (!chip->info->ops->phy_read)
388c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return chip->info->ops->phy_read(chip, bus, addr, reg, val);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciint mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy, int reg, u16 val)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	int addr = phy; /* PHY devices addresses start at 0x0 */
468c2ecf20Sopenharmony_ci	struct mii_bus *bus;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	bus = mv88e6xxx_default_mdio_bus(chip);
498c2ecf20Sopenharmony_ci	if (!bus)
508c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (!chip->info->ops->phy_write)
538c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return chip->info->ops->phy_write(chip, bus, addr, reg, val);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return mv88e6xxx_phy_write(chip, phy, MV88E6XXX_PHY_PAGE, page);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	int err;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Restore PHY page Copper 0x0 for access via the registered
688c2ecf20Sopenharmony_ci	 * MDIO bus
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	err = mv88e6xxx_phy_write(chip, phy, MV88E6XXX_PHY_PAGE,
718c2ecf20Sopenharmony_ci				  MV88E6XXX_PHY_PAGE_COPPER);
728c2ecf20Sopenharmony_ci	if (unlikely(err)) {
738c2ecf20Sopenharmony_ci		dev_err(chip->dev,
748c2ecf20Sopenharmony_ci			"failed to restore PHY %d page Copper (%d)\n",
758c2ecf20Sopenharmony_ci			phy, err);
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciint mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
808c2ecf20Sopenharmony_ci			    u8 page, int reg, u16 *val)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	int err;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* There is no paging for registers 22 */
858c2ecf20Sopenharmony_ci	if (reg == MV88E6XXX_PHY_PAGE)
868c2ecf20Sopenharmony_ci		return -EINVAL;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	err = mv88e6xxx_phy_page_get(chip, phy, page);
898c2ecf20Sopenharmony_ci	if (!err) {
908c2ecf20Sopenharmony_ci		err = mv88e6xxx_phy_read(chip, phy, reg, val);
918c2ecf20Sopenharmony_ci		mv88e6xxx_phy_page_put(chip, phy);
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return err;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciint mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
988c2ecf20Sopenharmony_ci			     u8 page, int reg, u16 val)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	int err;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* There is no paging for registers 22 */
1038c2ecf20Sopenharmony_ci	if (reg == MV88E6XXX_PHY_PAGE)
1048c2ecf20Sopenharmony_ci		return -EINVAL;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	err = mv88e6xxx_phy_page_get(chip, phy, page);
1078c2ecf20Sopenharmony_ci	if (!err) {
1088c2ecf20Sopenharmony_ci		err = mv88e6xxx_phy_write(chip, phy, MV88E6XXX_PHY_PAGE, page);
1098c2ecf20Sopenharmony_ci		if (!err)
1108c2ecf20Sopenharmony_ci			err = mv88e6xxx_phy_write(chip, phy, reg, val);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		mv88e6xxx_phy_page_put(chip, phy);
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return err;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int mv88e6xxx_phy_ppu_disable(struct mv88e6xxx_chip *chip)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	if (!chip->info->ops->ppu_disable)
1218c2ecf20Sopenharmony_ci		return 0;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return chip->info->ops->ppu_disable(chip);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int mv88e6xxx_phy_ppu_enable(struct mv88e6xxx_chip *chip)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	if (!chip->info->ops->ppu_enable)
1298c2ecf20Sopenharmony_ci		return 0;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return chip->info->ops->ppu_enable(chip);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic void mv88e6xxx_phy_ppu_reenable_work(struct work_struct *ugly)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct mv88e6xxx_chip *chip;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	mv88e6xxx_reg_lock(chip);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (mutex_trylock(&chip->ppu_mutex)) {
1438c2ecf20Sopenharmony_ci		if (mv88e6xxx_phy_ppu_enable(chip) == 0)
1448c2ecf20Sopenharmony_ci			chip->ppu_disabled = 0;
1458c2ecf20Sopenharmony_ci		mutex_unlock(&chip->ppu_mutex);
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	mv88e6xxx_reg_unlock(chip);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void mv88e6xxx_phy_ppu_reenable_timer(struct timer_list *t)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct mv88e6xxx_chip *chip = from_timer(chip, t, ppu_timer);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	schedule_work(&chip->ppu_work);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int mv88e6xxx_phy_ppu_access_get(struct mv88e6xxx_chip *chip)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	int ret;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	mutex_lock(&chip->ppu_mutex);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* If the PHY polling unit is enabled, disable it so that
1658c2ecf20Sopenharmony_ci	 * we can access the PHY registers.  If it was already
1668c2ecf20Sopenharmony_ci	 * disabled, cancel the timer that is going to re-enable
1678c2ecf20Sopenharmony_ci	 * it.
1688c2ecf20Sopenharmony_ci	 */
1698c2ecf20Sopenharmony_ci	if (!chip->ppu_disabled) {
1708c2ecf20Sopenharmony_ci		ret = mv88e6xxx_phy_ppu_disable(chip);
1718c2ecf20Sopenharmony_ci		if (ret < 0) {
1728c2ecf20Sopenharmony_ci			mutex_unlock(&chip->ppu_mutex);
1738c2ecf20Sopenharmony_ci			return ret;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci		chip->ppu_disabled = 1;
1768c2ecf20Sopenharmony_ci	} else {
1778c2ecf20Sopenharmony_ci		del_timer(&chip->ppu_timer);
1788c2ecf20Sopenharmony_ci		ret = 0;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return ret;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void mv88e6xxx_phy_ppu_access_put(struct mv88e6xxx_chip *chip)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	/* Schedule a timer to re-enable the PHY polling unit. */
1878c2ecf20Sopenharmony_ci	mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
1888c2ecf20Sopenharmony_ci	mutex_unlock(&chip->ppu_mutex);
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic void mv88e6xxx_phy_ppu_state_init(struct mv88e6xxx_chip *chip)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	mutex_init(&chip->ppu_mutex);
1948c2ecf20Sopenharmony_ci	INIT_WORK(&chip->ppu_work, mv88e6xxx_phy_ppu_reenable_work);
1958c2ecf20Sopenharmony_ci	timer_setup(&chip->ppu_timer, mv88e6xxx_phy_ppu_reenable_timer, 0);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic void mv88e6xxx_phy_ppu_state_destroy(struct mv88e6xxx_chip *chip)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	del_timer_sync(&chip->ppu_timer);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciint mv88e6185_phy_ppu_read(struct mv88e6xxx_chip *chip, struct mii_bus *bus,
2048c2ecf20Sopenharmony_ci			   int addr, int reg, u16 *val)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	int err;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	err = mv88e6xxx_phy_ppu_access_get(chip);
2098c2ecf20Sopenharmony_ci	if (!err) {
2108c2ecf20Sopenharmony_ci		err = mv88e6xxx_read(chip, addr, reg, val);
2118c2ecf20Sopenharmony_ci		mv88e6xxx_phy_ppu_access_put(chip);
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	return err;
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ciint mv88e6185_phy_ppu_write(struct mv88e6xxx_chip *chip, struct mii_bus *bus,
2188c2ecf20Sopenharmony_ci			    int addr, int reg, u16 val)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	int err;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	err = mv88e6xxx_phy_ppu_access_get(chip);
2238c2ecf20Sopenharmony_ci	if (!err) {
2248c2ecf20Sopenharmony_ci		err = mv88e6xxx_write(chip, addr, reg, val);
2258c2ecf20Sopenharmony_ci		mv88e6xxx_phy_ppu_access_put(chip);
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	return err;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_civoid mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	if (chip->info->ops->ppu_enable && chip->info->ops->ppu_disable)
2348c2ecf20Sopenharmony_ci		mv88e6xxx_phy_ppu_state_init(chip);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_civoid mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	if (chip->info->ops->ppu_enable && chip->info->ops->ppu_disable)
2408c2ecf20Sopenharmony_ci		mv88e6xxx_phy_ppu_state_destroy(chip);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciint mv88e6xxx_phy_setup(struct mv88e6xxx_chip *chip)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	return mv88e6xxx_phy_ppu_enable(chip);
2468c2ecf20Sopenharmony_ci}
247