aboutsummaryrefslogtreecommitdiff
path: root/drivers/net/phy/teranetics.c
blob: 91e1bec6079fafc20665a3d26949250da2817a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Driver for Teranetics PHY
 *
 * Author: Shaohui Xie <Shaohui.Xie@freescale.com>
 *
 * Copyright 2015 Freescale Semiconductor, Inc.
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2.  This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mii.h>
#include <linux/ethtool.h>
#include <linux/mdio.h>
#include <linux/phy.h>

MODULE_DESCRIPTION("Teranetics PHY driver");
MODULE_AUTHOR("Shaohui Xie <Shaohui.Xie@freescale.com>");
MODULE_LICENSE("GPL v2");

#define PHY_ID_TN2020	0x00a19410
#define MDIO_PHYXS_LNSTAT_SYNC0	0x0001
#define MDIO_PHYXS_LNSTAT_SYNC1	0x0002
#define MDIO_PHYXS_LNSTAT_SYNC2	0x0004
#define MDIO_PHYXS_LNSTAT_SYNC3	0x0008
#define MDIO_PHYXS_LNSTAT_ALIGN 0x1000

#define MDIO_PHYXS_LANE_READY	(MDIO_PHYXS_LNSTAT_SYNC0 | \
				MDIO_PHYXS_LNSTAT_SYNC1 | \
				MDIO_PHYXS_LNSTAT_SYNC2 | \
				MDIO_PHYXS_LNSTAT_SYNC3 | \
				MDIO_PHYXS_LNSTAT_ALIGN)

static int teranetics_config_init(struct phy_device *phydev)
{
	phydev->supported = SUPPORTED_10000baseT_Full;
	phydev->advertising = SUPPORTED_10000baseT_Full;

	return 0;
}

static int teranetics_soft_reset(struct phy_device *phydev)
{
	return 0;
}

static int teranetics_aneg_done(struct phy_device *phydev)
{
	int reg;

	/* auto negotiation state can only be checked when using copper
	 * port, if using fiber port, just lie it's done.
	 */
	if (!phy_read_mmd(phydev, MDIO_MMD_VEND1, 93)) {
		reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
		return (reg < 0) ? reg : (reg & BMSR_ANEGCOMPLETE);
	}

	return 1;
}

static int teranetics_config_aneg(struct phy_device *phydev)
{
	return 0;
}

static int teranetics_read_status(struct phy_device *phydev)
{
	int reg;

	phydev->link = 1;

	phydev->speed = SPEED_10000;
	phydev->duplex = DUPLEX_FULL;

	if (!phy_read_mmd(phydev, MDIO_MMD_VEND1, 93)) {
		reg = phy_read_mmd(phydev, MDIO_MMD_PHYXS, MDIO_PHYXS_LNSTAT);
		if (reg < 0 ||
		    !((reg & MDIO_PHYXS_LANE_READY) == MDIO_PHYXS_LANE_READY)) {
			phydev->link = 0;
			return 0;
		}

		reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
		if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
			phydev->link = 0;
	}

	return 0;
}

static int teranetics_match_phy_device(struct phy_device *phydev)
{
	return phydev->c45_ids.device_ids[3] == PHY_ID_TN2020;
}

static struct phy_driver teranetics_driver[] = {
{
	.phy_id		= PHY_ID_TN2020,
	.phy_id_mask	= 0xffffffff,
	.name		= "Teranetics TN2020",
	.soft_reset	= teranetics_soft_reset,
	.aneg_done	= teranetics_aneg_done,
	.config_init    = teranetics_config_init,
	.config_aneg    = teranetics_config_aneg,
	.read_status	= teranetics_read_status,
	.match_phy_device = teranetics_match_phy_device,
	.driver		= { .owner = THIS_MODULE,},
},
};

static int __init teranetics_init(void)
{
	return phy_drivers_register(teranetics_driver,
				    ARRAY_SIZE(teranetics_driver));
}

static void __exit teranetics_exit(void)
{
	return phy_drivers_unregister(teranetics_driver,
				      ARRAY_SIZE(teranetics_driver));
}

module_init(teranetics_init);
module_exit(teranetics_exit);

static struct mdio_device_id __maybe_unused teranetics_tbl[] = {
	{ PHY_ID_TN2020, 0xffffffff },
	{ }
};

MODULE_DEVICE_TABLE(mdio, teranetics_tbl);