1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2d397f7c4SAlexandru Gagniuc /*
3d397f7c4SAlexandru Gagniuc  * Micrel PHY drivers
4d397f7c4SAlexandru Gagniuc  *
5d397f7c4SAlexandru Gagniuc  * Copyright 2010-2011 Freescale Semiconductor, Inc.
6d397f7c4SAlexandru Gagniuc  * author Andy Fleming
7d397f7c4SAlexandru Gagniuc  * (C) 2012 NetModule AG, David Andrey, added KSZ9031
8d397f7c4SAlexandru Gagniuc  */
9d397f7c4SAlexandru Gagniuc #include <common.h>
10d397f7c4SAlexandru Gagniuc #include <dm.h>
11d397f7c4SAlexandru Gagniuc #include <errno.h>
12d397f7c4SAlexandru Gagniuc #include <fdtdec.h>
13d397f7c4SAlexandru Gagniuc #include <micrel.h>
14d397f7c4SAlexandru Gagniuc #include <phy.h>
15d397f7c4SAlexandru Gagniuc 
16d397f7c4SAlexandru Gagniuc static struct phy_driver KSZ804_driver = {
17d397f7c4SAlexandru Gagniuc 	.name = "Micrel KSZ804",
18d397f7c4SAlexandru Gagniuc 	.uid = 0x221510,
19d397f7c4SAlexandru Gagniuc 	.mask = 0xfffff0,
20d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
21d397f7c4SAlexandru Gagniuc 	.config = &genphy_config,
22d397f7c4SAlexandru Gagniuc 	.startup = &genphy_startup,
23d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
24d397f7c4SAlexandru Gagniuc };
25d397f7c4SAlexandru Gagniuc 
26d397f7c4SAlexandru Gagniuc #define MII_KSZPHY_OMSO		0x16
27d397f7c4SAlexandru Gagniuc #define KSZPHY_OMSO_B_CAST_OFF	(1 << 9)
28d397f7c4SAlexandru Gagniuc 
ksz_genconfig_bcastoff(struct phy_device * phydev)29d397f7c4SAlexandru Gagniuc static int ksz_genconfig_bcastoff(struct phy_device *phydev)
30d397f7c4SAlexandru Gagniuc {
31d397f7c4SAlexandru Gagniuc 	int ret;
32d397f7c4SAlexandru Gagniuc 
33d397f7c4SAlexandru Gagniuc 	ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZPHY_OMSO);
34d397f7c4SAlexandru Gagniuc 	if (ret < 0)
35d397f7c4SAlexandru Gagniuc 		return ret;
36d397f7c4SAlexandru Gagniuc 
37d397f7c4SAlexandru Gagniuc 	ret = phy_write(phydev, MDIO_DEVAD_NONE, MII_KSZPHY_OMSO,
38d397f7c4SAlexandru Gagniuc 			ret | KSZPHY_OMSO_B_CAST_OFF);
39d397f7c4SAlexandru Gagniuc 	if (ret < 0)
40d397f7c4SAlexandru Gagniuc 		return ret;
41d397f7c4SAlexandru Gagniuc 
42d397f7c4SAlexandru Gagniuc 	return genphy_config(phydev);
43d397f7c4SAlexandru Gagniuc }
44d397f7c4SAlexandru Gagniuc 
45d397f7c4SAlexandru Gagniuc static struct phy_driver KSZ8031_driver = {
46d397f7c4SAlexandru Gagniuc 	.name = "Micrel KSZ8021/KSZ8031",
47d397f7c4SAlexandru Gagniuc 	.uid = 0x221550,
48d397f7c4SAlexandru Gagniuc 	.mask = 0xfffff0,
49d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
50d397f7c4SAlexandru Gagniuc 	.config = &ksz_genconfig_bcastoff,
51d397f7c4SAlexandru Gagniuc 	.startup = &genphy_startup,
52d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
53d397f7c4SAlexandru Gagniuc };
54d397f7c4SAlexandru Gagniuc 
55d397f7c4SAlexandru Gagniuc /**
56d397f7c4SAlexandru Gagniuc  * KSZ8051
57d397f7c4SAlexandru Gagniuc  */
58d397f7c4SAlexandru Gagniuc #define MII_KSZ8051_PHY_OMSO			0x16
59d397f7c4SAlexandru Gagniuc #define MII_KSZ8051_PHY_OMSO_NAND_TREE_ON	(1 << 5)
60d397f7c4SAlexandru Gagniuc 
ksz8051_config(struct phy_device * phydev)61d397f7c4SAlexandru Gagniuc static int ksz8051_config(struct phy_device *phydev)
62d397f7c4SAlexandru Gagniuc {
63d397f7c4SAlexandru Gagniuc 	unsigned val;
64d397f7c4SAlexandru Gagniuc 
65d397f7c4SAlexandru Gagniuc 	/* Disable NAND-tree */
66d397f7c4SAlexandru Gagniuc 	val = phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ8051_PHY_OMSO);
67d397f7c4SAlexandru Gagniuc 	val &= ~MII_KSZ8051_PHY_OMSO_NAND_TREE_ON;
68d397f7c4SAlexandru Gagniuc 	phy_write(phydev, MDIO_DEVAD_NONE, MII_KSZ8051_PHY_OMSO, val);
69d397f7c4SAlexandru Gagniuc 
70d397f7c4SAlexandru Gagniuc 	return genphy_config(phydev);
71d397f7c4SAlexandru Gagniuc }
72d397f7c4SAlexandru Gagniuc 
73d397f7c4SAlexandru Gagniuc static struct phy_driver KSZ8051_driver = {
74d397f7c4SAlexandru Gagniuc 	.name = "Micrel KSZ8051",
75d397f7c4SAlexandru Gagniuc 	.uid = 0x221550,
76d397f7c4SAlexandru Gagniuc 	.mask = 0xfffff0,
77d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
78d397f7c4SAlexandru Gagniuc 	.config = &ksz8051_config,
79d397f7c4SAlexandru Gagniuc 	.startup = &genphy_startup,
80d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
81d397f7c4SAlexandru Gagniuc };
82d397f7c4SAlexandru Gagniuc 
83d397f7c4SAlexandru Gagniuc static struct phy_driver KSZ8081_driver = {
84d397f7c4SAlexandru Gagniuc 	.name = "Micrel KSZ8081",
85d397f7c4SAlexandru Gagniuc 	.uid = 0x221560,
86d397f7c4SAlexandru Gagniuc 	.mask = 0xfffff0,
87d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
88d397f7c4SAlexandru Gagniuc 	.config = &ksz_genconfig_bcastoff,
89d397f7c4SAlexandru Gagniuc 	.startup = &genphy_startup,
90d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
91d397f7c4SAlexandru Gagniuc };
92d397f7c4SAlexandru Gagniuc 
93d397f7c4SAlexandru Gagniuc /**
94d397f7c4SAlexandru Gagniuc  * KSZ8895
95d397f7c4SAlexandru Gagniuc  */
96d397f7c4SAlexandru Gagniuc 
smireg_to_phy(unsigned short reg)97d397f7c4SAlexandru Gagniuc static unsigned short smireg_to_phy(unsigned short reg)
98d397f7c4SAlexandru Gagniuc {
99d397f7c4SAlexandru Gagniuc 	return ((reg & 0xc0) >> 3) + 0x06 + ((reg & 0x20) >> 5);
100d397f7c4SAlexandru Gagniuc }
101d397f7c4SAlexandru Gagniuc 
smireg_to_reg(unsigned short reg)102d397f7c4SAlexandru Gagniuc static unsigned short smireg_to_reg(unsigned short reg)
103d397f7c4SAlexandru Gagniuc {
104d397f7c4SAlexandru Gagniuc 	return reg & 0x1F;
105d397f7c4SAlexandru Gagniuc }
106d397f7c4SAlexandru Gagniuc 
ksz8895_write_smireg(struct phy_device * phydev,int smireg,int val)107d397f7c4SAlexandru Gagniuc static void ksz8895_write_smireg(struct phy_device *phydev, int smireg, int val)
108d397f7c4SAlexandru Gagniuc {
109d397f7c4SAlexandru Gagniuc 	phydev->bus->write(phydev->bus, smireg_to_phy(smireg), MDIO_DEVAD_NONE,
110d397f7c4SAlexandru Gagniuc 						smireg_to_reg(smireg), val);
111d397f7c4SAlexandru Gagniuc }
112d397f7c4SAlexandru Gagniuc 
113d397f7c4SAlexandru Gagniuc #if 0
114d397f7c4SAlexandru Gagniuc static int ksz8895_read_smireg(struct phy_device *phydev, int smireg)
115d397f7c4SAlexandru Gagniuc {
116d397f7c4SAlexandru Gagniuc 	return phydev->bus->read(phydev->bus, smireg_to_phy(smireg),
117d397f7c4SAlexandru Gagniuc 					MDIO_DEVAD_NONE, smireg_to_reg(smireg));
118d397f7c4SAlexandru Gagniuc }
119d397f7c4SAlexandru Gagniuc #endif
120d397f7c4SAlexandru Gagniuc 
ksz8895_config(struct phy_device * phydev)121d397f7c4SAlexandru Gagniuc int ksz8895_config(struct phy_device *phydev)
122d397f7c4SAlexandru Gagniuc {
123d397f7c4SAlexandru Gagniuc 	/* we are connected directly to the switch without
124d397f7c4SAlexandru Gagniuc 	 * dedicated PHY. SCONF1 == 001 */
125d397f7c4SAlexandru Gagniuc 	phydev->link = 1;
126d397f7c4SAlexandru Gagniuc 	phydev->duplex = DUPLEX_FULL;
127d397f7c4SAlexandru Gagniuc 	phydev->speed = SPEED_100;
128d397f7c4SAlexandru Gagniuc 
129d397f7c4SAlexandru Gagniuc 	/* Force the switch to start */
130d397f7c4SAlexandru Gagniuc 	ksz8895_write_smireg(phydev, 1, 1);
131d397f7c4SAlexandru Gagniuc 
132d397f7c4SAlexandru Gagniuc 	return 0;
133d397f7c4SAlexandru Gagniuc }
134d397f7c4SAlexandru Gagniuc 
ksz8895_startup(struct phy_device * phydev)135d397f7c4SAlexandru Gagniuc static int ksz8895_startup(struct phy_device *phydev)
136d397f7c4SAlexandru Gagniuc {
137d397f7c4SAlexandru Gagniuc 	return 0;
138d397f7c4SAlexandru Gagniuc }
139d397f7c4SAlexandru Gagniuc 
140d397f7c4SAlexandru Gagniuc static struct phy_driver ksz8895_driver = {
141d397f7c4SAlexandru Gagniuc 	.name = "Micrel KSZ8895/KSZ8864",
142d397f7c4SAlexandru Gagniuc 	.uid  = 0x221450,
143d397f7c4SAlexandru Gagniuc 	.mask = 0xffffe1,
144d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
145d397f7c4SAlexandru Gagniuc 	.config   = &ksz8895_config,
146d397f7c4SAlexandru Gagniuc 	.startup  = &ksz8895_startup,
147d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
148d397f7c4SAlexandru Gagniuc };
149d397f7c4SAlexandru Gagniuc 
150fbc120e6SAlexandru Gagniuc /* Micrel used the exact same part number for the KSZ9021. */
151d397f7c4SAlexandru Gagniuc static struct phy_driver KS8721_driver = {
152d397f7c4SAlexandru Gagniuc 	.name = "Micrel KS8721BL",
153d397f7c4SAlexandru Gagniuc 	.uid = 0x221610,
154d397f7c4SAlexandru Gagniuc 	.mask = 0xfffff0,
155d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
156d397f7c4SAlexandru Gagniuc 	.config = &genphy_config,
157d397f7c4SAlexandru Gagniuc 	.startup = &genphy_startup,
158d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
159d397f7c4SAlexandru Gagniuc };
160d397f7c4SAlexandru Gagniuc 
ksz886x_config(struct phy_device * phydev)161d397f7c4SAlexandru Gagniuc int ksz886x_config(struct phy_device *phydev)
162d397f7c4SAlexandru Gagniuc {
163d397f7c4SAlexandru Gagniuc 	/* we are connected directly to the switch without
164d397f7c4SAlexandru Gagniuc 	 * dedicated PHY. */
165d397f7c4SAlexandru Gagniuc 	phydev->link = 1;
166d397f7c4SAlexandru Gagniuc 	phydev->duplex = DUPLEX_FULL;
167d397f7c4SAlexandru Gagniuc 	phydev->speed = SPEED_100;
168d397f7c4SAlexandru Gagniuc 	return 0;
169d397f7c4SAlexandru Gagniuc }
170d397f7c4SAlexandru Gagniuc 
ksz886x_startup(struct phy_device * phydev)171d397f7c4SAlexandru Gagniuc static int ksz886x_startup(struct phy_device *phydev)
172d397f7c4SAlexandru Gagniuc {
173d397f7c4SAlexandru Gagniuc 	return 0;
174d397f7c4SAlexandru Gagniuc }
175d397f7c4SAlexandru Gagniuc 
176d397f7c4SAlexandru Gagniuc static struct phy_driver ksz886x_driver = {
177d397f7c4SAlexandru Gagniuc 	.name = "Micrel KSZ886x Switch",
178d397f7c4SAlexandru Gagniuc 	.uid  = 0x00221430,
179d397f7c4SAlexandru Gagniuc 	.mask = 0xfffff0,
180d397f7c4SAlexandru Gagniuc 	.features = PHY_BASIC_FEATURES,
181d397f7c4SAlexandru Gagniuc 	.config = &ksz886x_config,
182d397f7c4SAlexandru Gagniuc 	.startup = &ksz886x_startup,
183d397f7c4SAlexandru Gagniuc 	.shutdown = &genphy_shutdown,
184d397f7c4SAlexandru Gagniuc };
185d397f7c4SAlexandru Gagniuc 
phy_micrel_ksz8xxx_init(void)186d397f7c4SAlexandru Gagniuc int phy_micrel_ksz8xxx_init(void)
187d397f7c4SAlexandru Gagniuc {
188d397f7c4SAlexandru Gagniuc 	phy_register(&KSZ804_driver);
189d397f7c4SAlexandru Gagniuc 	phy_register(&KSZ8031_driver);
190d397f7c4SAlexandru Gagniuc 	phy_register(&KSZ8051_driver);
191d397f7c4SAlexandru Gagniuc 	phy_register(&KSZ8081_driver);
192d397f7c4SAlexandru Gagniuc 	phy_register(&KS8721_driver);
193d397f7c4SAlexandru Gagniuc 	phy_register(&ksz8895_driver);
194d397f7c4SAlexandru Gagniuc 	phy_register(&ksz886x_driver);
195d397f7c4SAlexandru Gagniuc 	return 0;
196d397f7c4SAlexandru Gagniuc }
197