summaryrefslogtreecommitdiffstats
path: root/drivers/net/miiphy.c
blob: 8f810f54d1c55782a63efa9bfaee7194db698007 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <common.h>
#include <driver.h>
#include <init.h>
#include <mii_phy.h>

int miiphy_restart_aneg(struct miiphy_device *mdev)
{
	uint16_t status;
	int timeout;

	/*
	 * Reset PHY, then delay 300ns
	 */
	mdev->write(mdev, mdev->address, PHY_BMCR, PHY_BMCR_RESET);
	udelay(1000);

	if (mdev->flags & MIIPHY_FORCE_10) {
		printf("Forcing 10 Mbps ethernet link... ");
		mdev->read(mdev, mdev->address, PHY_BMSR, &status);
		mdev->write(mdev, mdev->address, PHY_BMCR, PHY_BMCR_DPLX | PHY_BMCR_COL_TST);

		timeout = 20;
		do {	/* wait for link status to go down */
			udelay(10000);
			if ((timeout--) == 0) {
#if (DEBUG & 0x2)
				printf("hmmm, should not have waited...");
#endif
				break;
			}
			mdev->read(mdev, mdev->address, PHY_BMSR, &status);
		} while (status & PHY_BMSR_LS);

	} else {	/* MII100 */
		/*
		 * Set the auto-negotiation advertisement register bits
		 */
		mdev->write(mdev, mdev->address, PHY_ANAR, 0x01e1);

		mdev->write(mdev, mdev->address, PHY_BMCR, PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
	}

	return 0;
}

int miiphy_wait_aneg(struct miiphy_device *mdev)
{
	int timeout = 1;
	uint16_t status;

	/*
	 * Wait for AN completion
	 */
	timeout = 5000;
	do {
		udelay(1000);

		if (!timeout--) {
			printf("%s: Autonegotiation timeout\n", mdev->dev.id);
			return -1;
		}

		if (mdev->read(mdev, mdev->address, PHY_BMSR, &status)) {
			printf("%s: Autonegotiation failed. status: 0x%04x\n", mdev->dev.id, status);
			return -1;
		}
	} while (!(status & PHY_BMSR_LS));

	return 0;
}

int miiphy_print_status(struct miiphy_device *mdev)
{
	uint16_t bmsr, bmcr, anlpar;
	char *duplex;
	int speed;

	if (mdev->read(mdev, mdev->address, PHY_BMSR, &bmsr) != 0)
		goto err_out;
	if (mdev->read(mdev, mdev->address, PHY_BMCR, &bmcr) != 0)
		goto err_out;
	if (mdev->read(mdev, mdev->address, PHY_ANLPAR, &anlpar) != 0)
		goto err_out;

	printf("%s: Link is %s", mdev->dev.id,
			bmsr & PHY_BMSR_LS ? "up" : "down");

	if (bmcr & PHY_BMCR_AUTON) {
		duplex = (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD) ? "Full" : "Half";
		speed = anlpar & PHY_ANLPAR_100 ? 100 : 10;
	} else {
		duplex = bmcr & PHY_BMCR_DPLX ? "Full" : "Half";
		speed = bmcr & PHY_BMCR_100MB ? 100 : 10;
	}

	printf(" - %d/%s\n", speed, duplex);

	return 0;
err_out:
	printf("%s: failed to read\n", mdev->dev.id);
	return -1;
}

ssize_t miiphy_read(struct device_d *dev, void *_buf, size_t count, ulong offset, ulong flags)
{
	int i = count;
	uint16_t *buf = _buf;
	struct miiphy_device *mdev = dev->priv;

	while (i > 1) {
		mdev->read(mdev, mdev->address, offset, buf);
		buf++;
		i -= 2;
		offset++;
	}

	return count;
}

ssize_t miiphy_write(struct device_d *dev, const void *_buf, size_t count, ulong offset, ulong flags)
{
	int i = count;
	const uint16_t *buf = _buf;
	struct miiphy_device *mdev = dev->priv;

	while (i > 1) {
		mdev->write(mdev, mdev->address, offset, *buf);
		buf++;
		i -= 2;
		offset++;
	}

	return count;
}

static int miiphy_probe(struct device_d *dev)
{
	return 0;
}

int miiphy_register(struct miiphy_device *mdev)
{
	strcpy(mdev->dev.name, "miiphy");
	get_free_deviceid(mdev->dev.id, "phy");
	mdev->dev.type = DEVICE_TYPE_MIIPHY;
	mdev->dev.priv = mdev;
	mdev->dev.size = 32;

	return register_device(&mdev->dev);
}

static struct driver_d miiphy_drv = {
        .name  = "miiphy",
        .probe = miiphy_probe,
	.read  = miiphy_read,
	.write = miiphy_write,
	.type  = DEVICE_TYPE_MIIPHY,
};

static int miiphy_init(void)
{
	register_driver(&miiphy_drv);
	return 0;
}

device_initcall(miiphy_init);