summaryrefslogtreecommitdiffstats
path: root/board/bct-brettl2/smsc9303.c
blob: 15eea7a48416084d3de326f8a482f887f122dd5e (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
168
169
170
171
172
173
174
175
176
/*
 * smsc9303.c - routines to initialize SMSC 9303 switch
 *
 * Copyright (c) 2010 BCT Electronic GmbH
 *
 * Licensed under the GPL-2 or later.
 */

#include <common.h>
#include <config.h>
#include <miiphy.h>

#include <asm/blackfin.h>
#include <asm/gpio.h>

static int smc9303i_write_mii(unsigned char addr, unsigned char reg, unsigned short data)
{
	const char *devname = miiphy_get_current_dev();

	if (!devname)
	    return 0;

	if (miiphy_write(devname, addr, reg, data) != 0)
	    return 0;

	return 1;
}

static int smc9303i_write_reg(unsigned short reg, unsigned int data)
{
	const char *devname = miiphy_get_current_dev();
	unsigned char mii_addr = 0x10 | (reg >> 6);
	unsigned char mii_reg = (reg & 0x3c) >> 1;

	if (!devname)
	    return 0;

	if (miiphy_write(devname, mii_addr, mii_reg|0, data & 0xffff) != 0)
	    return 0;

	if (miiphy_write(devname, mii_addr, mii_reg|1, data >> 16) != 0)
	    return 0;

	return 1;
}

static int smc9303i_read_reg(unsigned short reg, unsigned int *data)
{
	const char *devname = miiphy_get_current_dev();
	unsigned char mii_addr = 0x10 | (reg >> 6);
	unsigned char mii_reg = (reg & 0x3c) >> 1;
	unsigned short tmp1, tmp2;

	if (!devname)
	    return 0;

	if (miiphy_read(devname, mii_addr, mii_reg|0, &tmp1) != 0)
	    return 0;

	if (miiphy_read(devname, mii_addr, mii_reg|1, &tmp2) != 0)
	    return 0;

	*data = (tmp2 << 16) | tmp1;

	return 1;
}

#if 0
static int smc9303i_read_mii(unsigned char addr, unsigned char reg, unsigned short *data)
{
	const char *devname = miiphy_get_current_dev();

	if (!devname)
	    return 0;

	if (miiphy_read(devname, addr, reg, data) != 0)
	    return 0;

	return 1;
}
#endif

typedef struct {
	unsigned short reg;
	unsigned int value;
} smsc9303i_config_entry1_t;

static const smsc9303i_config_entry1_t smsc9303i_config_table1[] =
{
	{0x1a0, 0x00000006}, /* Port 1 Manual Flow Control Register */
	{0x1a4, 0x00000006}, /* Port 2 Manual Flow Control Register */
	{0x1a8, 0x00000006}, /* Port 0 Manual Flow Control Register */
};

typedef struct
{
	unsigned char addr;
	unsigned char reg;
	unsigned short value;
} smsc9303i_config_entry2_t;

static const smsc9303i_config_entry2_t smsc9303i_config_table2[] =
{
	{0x01, 0x00, 0x0100}, /* Port0 PHY Basic Control Register */
	{0x02, 0x00, 0x1100}, /* Port1 PHY Basic Control Register */
	{0x03, 0x00, 0x1100}, /* Port2 PHY Basic Control Register */

	{0x01, 0x04, 0x0001}, /* Port0 PHY Auto-Negotiation Advertisement Register */
	{0x02, 0x04, 0x2de1}, /* Port1 PHY Auto-Negotiation Advertisement Register */
	{0x03, 0x04, 0x2de1}, /* Port2 PHY Auto-Negotiation Advertisement Register */

	{0x01, 0x11, 0x0000}, /* Port0 PHY Mode Control/Status Register */
	{0x02, 0x11, 0x0000}, /* Port1 PHY Mode Control/Status Register */
	{0x03, 0x11, 0x0000}, /* Port2 PHY Mode Control/Status Register */

	{0x01, 0x12, 0x0021}, /* Port0 PHY Special Modes Register */
	{0x02, 0x12, 0x00e2}, /* Port1 PHY Special Modes Register */
	{0x03, 0x12, 0x00e3}, /* Port2 PHY Special Modes Register */
	{0x01, 0x1b, 0x0000}, /* Port0 PHY Special Control/Status Indication Register */
	{0x02, 0x1b, 0x0000}, /* Port1 PHY Special Control/Status Indication Register */
	{0x03, 0x1b, 0x0000}, /* Port2 PHY Special Control/Status Indication Register */
	{0x01, 0x1e, 0x0000}, /* Port0 PHY Interrupt Source Flags Register */
	{0x02, 0x1e, 0x0000}, /* Port1 PHY Interrupt Source Flags Register */
	{0x03, 0x1e, 0x0000}, /* Port2 PHY Interrupt Source Flags Register */
};

int init_smsc9303i_mii(void)
{
	unsigned int data;
	unsigned int i;

	printf("       reset SMSC LAN9303i\n");

	gpio_request(GPIO_PG10, "smsc9303");
	gpio_direction_output(GPIO_PG10, 0);
	udelay(10000);
	gpio_direction_output(GPIO_PG10, 1);
	udelay(10000);

	gpio_free(GPIO_PG10);

#if defined(CONFIG_MII_INIT)
	mii_init();
#endif

	printf("       write SMSC LAN9303i configuration\n");

	if (!smc9303i_read_reg(0x50, &data))
		return 0;

	if ((data >> 16) != 0x9303)	{
		/* chip id not found */
		printf("       error identifying SMSC LAN9303i\n");
		return 0;
	}

	for (i = 0; i < ARRAY_SIZE(smsc9303i_config_table1); i++) {
		const smsc9303i_config_entry1_t *entry = &smsc9303i_config_table1[i];

		if (!smc9303i_write_reg(entry->reg, entry->value)) {
			printf("       error writing SMSC LAN9303i configuration\n");
			return 0;
		}
	}

	for (i = 0; i < ARRAY_SIZE(smsc9303i_config_table2); i++) {
		const smsc9303i_config_entry2_t *entry = &smsc9303i_config_table2[i];

		if (!smc9303i_write_mii(entry->addr, entry->reg, entry->value)) {
			printf("       error writing SMSC LAN9303i configuration\n");
			return 0;
		}
	}

	return 1;
}
OpenPOWER on IntegriCloud