blob: b6247c92e3093a7de88ca0893e73360bab07d559 [file] [log] [blame]
wdenk2abbe072003-06-16 23:50:08 +00001/*
2 * (C) Copyright 2003
3 * Author : Hamid Ikdoumi (Atmel)
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <at91rm9200_net.h>
25#include <net.h>
26
27/* ----- Ethernet Buffer definitions ----- */
28
29typedef struct {
30 unsigned long addr, size;
31} rbf_t;
32
33#define RBF_ADDR 0xfffffffc
34#define RBF_OWNER (1<<0)
35#define RBF_WRAP (1<<1)
36#define RBF_BROADCAST (1<<31)
37#define RBF_MULTICAST (1<<30)
38#define RBF_UNICAST (1<<29)
39#define RBF_EXTERNAL (1<<28)
40#define RBF_UNKOWN (1<<27)
41#define RBF_SIZE 0x07ff
42#define RBF_LOCAL4 (1<<26)
43#define RBF_LOCAL3 (1<<25)
44#define RBF_LOCAL2 (1<<24)
45#define RBF_LOCAL1 (1<<23)
46
47/* Emac Buffers in last 512KBytes of SDRAM*/
48/* Be careful, buffer size is limited to 512KBytes !!! */
49#define RBF_FRAMEMAX 100
50/*#define RBF_FRAMEMEM 0x200000 */
51#define RBF_FRAMEMEM 0x21F80000
52#define RBF_FRAMELEN 0x600
53
54#define RBF_FRAMEBTD RBF_FRAMEMEM
55#define RBF_FRAMEBUF (RBF_FRAMEMEM + RBF_FRAMEMAX*sizeof(rbf_t))
56
57
58#ifdef CONFIG_DRIVER_ETHER
59
60#if (CONFIG_COMMANDS & CFG_CMD_NET)
61
62/* structure to interface the PHY */
wdenk429168e2004-08-02 23:39:03 +000063AT91S_PhyOps PhyOps;
wdenk2abbe072003-06-16 23:50:08 +000064
65AT91PS_EMAC p_mac;
66
wdenk2abbe072003-06-16 23:50:08 +000067/*********** EMAC Phy layer Management functions *************************/
68/*
wdenk8bde7f72003-06-27 21:31:46 +000069 * Name:
wdenk2abbe072003-06-16 23:50:08 +000070 * at91rm9200_EmacEnableMDIO
wdenk8bde7f72003-06-27 21:31:46 +000071 * Description:
wdenk2abbe072003-06-16 23:50:08 +000072 * Enables the MDIO bit in MAC control register
wdenk8bde7f72003-06-27 21:31:46 +000073 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +000074 * p_mac - pointer to struct AT91S_EMAC
wdenk8bde7f72003-06-27 21:31:46 +000075 * Return value:
wdenk2abbe072003-06-16 23:50:08 +000076 * none
77 */
wdenk429168e2004-08-02 23:39:03 +000078void at91rm9200_EmacEnableMDIO (AT91PS_EMAC p_mac)
wdenk2abbe072003-06-16 23:50:08 +000079{
80 /* Mac CTRL reg set for MDIO enable */
81 p_mac->EMAC_CTL |= AT91C_EMAC_MPE; /* Management port enable */
82}
83
84/*
wdenk8bde7f72003-06-27 21:31:46 +000085 * Name:
wdenk2abbe072003-06-16 23:50:08 +000086 * at91rm9200_EmacDisableMDIO
wdenk8bde7f72003-06-27 21:31:46 +000087 * Description:
wdenk2abbe072003-06-16 23:50:08 +000088 * Disables the MDIO bit in MAC control register
wdenk8bde7f72003-06-27 21:31:46 +000089 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +000090 * p_mac - pointer to struct AT91S_EMAC
wdenk8bde7f72003-06-27 21:31:46 +000091 * Return value:
wdenk2abbe072003-06-16 23:50:08 +000092 * none
93 */
wdenk429168e2004-08-02 23:39:03 +000094void at91rm9200_EmacDisableMDIO (AT91PS_EMAC p_mac)
wdenk2abbe072003-06-16 23:50:08 +000095{
96 /* Mac CTRL reg set for MDIO disable */
97 p_mac->EMAC_CTL &= ~AT91C_EMAC_MPE; /* Management port disable */
98}
99
100
101/*
wdenk8bde7f72003-06-27 21:31:46 +0000102 * Name:
wdenk2abbe072003-06-16 23:50:08 +0000103 * at91rm9200_EmacReadPhy
wdenk8bde7f72003-06-27 21:31:46 +0000104 * Description:
wdenk2abbe072003-06-16 23:50:08 +0000105 * Reads data from the PHY register
wdenk8bde7f72003-06-27 21:31:46 +0000106 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +0000107 * dev - pointer to struct net_device
108 * RegisterAddress - unsigned char
wdenk8bde7f72003-06-27 21:31:46 +0000109 * pInput - pointer to value read from register
110 * Return value:
wdenk2abbe072003-06-16 23:50:08 +0000111 * TRUE - if data read successfully
112 */
wdenk429168e2004-08-02 23:39:03 +0000113UCHAR at91rm9200_EmacReadPhy (AT91PS_EMAC p_mac,
wdenk2abbe072003-06-16 23:50:08 +0000114 unsigned char RegisterAddress,
115 unsigned short *pInput)
116{
117 p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) |
wdenk074cff02004-02-24 00:16:43 +0000118 (AT91C_EMAC_RW_R) |
119 (RegisterAddress << 18) |
120 (AT91C_EMAC_CODE_802_3);
wdenk2abbe072003-06-16 23:50:08 +0000121
122 udelay (10000);
123
124 *pInput = (unsigned short) p_mac->EMAC_MAN;
125
126 return TRUE;
127}
128
129
130/*
wdenk8bde7f72003-06-27 21:31:46 +0000131 * Name:
wdenk2abbe072003-06-16 23:50:08 +0000132 * at91rm9200_EmacWritePhy
wdenk8bde7f72003-06-27 21:31:46 +0000133 * Description:
wdenk2abbe072003-06-16 23:50:08 +0000134 * Writes data to the PHY register
wdenk8bde7f72003-06-27 21:31:46 +0000135 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +0000136 * dev - pointer to struct net_device
137 * RegisterAddress - unsigned char
wdenk8bde7f72003-06-27 21:31:46 +0000138 * pOutput - pointer to value to be written in the register
139 * Return value:
wdenk2abbe072003-06-16 23:50:08 +0000140 * TRUE - if data read successfully
141 */
wdenk429168e2004-08-02 23:39:03 +0000142UCHAR at91rm9200_EmacWritePhy (AT91PS_EMAC p_mac,
wdenka3ad8e22003-10-19 23:22:11 +0000143 unsigned char RegisterAddress,
144 unsigned short *pOutput)
wdenk2abbe072003-06-16 23:50:08 +0000145{
146 p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) |
147 AT91C_EMAC_CODE_802_3 | AT91C_EMAC_RW_W |
wdenka3ad8e22003-10-19 23:22:11 +0000148 (RegisterAddress << 18) | *pOutput;
wdenk2abbe072003-06-16 23:50:08 +0000149
150 udelay (10000);
151
152 return TRUE;
153}
154
wdenk2abbe072003-06-16 23:50:08 +0000155
156rbf_t *rbfdt;
157rbf_t *rbfp;
158
159int eth_init (bd_t * bd)
160{
161 int ret;
162 int i;
163
164 p_mac = AT91C_BASE_EMAC;
165
wdenk0b8fa032004-04-25 14:37:29 +0000166 /* PIO Disable Register */
167 *AT91C_PIOA_PDR = AT91C_PA16_EMDIO | AT91C_PA15_EMDC | AT91C_PA14_ERXER |
168 AT91C_PA13_ERX1 | AT91C_PA12_ERX0 | AT91C_PA11_ECRS_ECRSDV |
169 AT91C_PA10_ETX1 | AT91C_PA9_ETX0 | AT91C_PA8_ETXEN |
170 AT91C_PA7_ETXCK_EREFCK;
wdenk2abbe072003-06-16 23:50:08 +0000171
wdenk9d5028c2004-11-21 00:06:33 +0000172#if defined(CONFIG_AT91C_USE_RMII) && !defined(CONFIG_CMC_PU2)
wdenk2abbe072003-06-16 23:50:08 +0000173 *AT91C_PIOB_PDR = AT91C_PB25_EF100 |
wdenk0b8fa032004-04-25 14:37:29 +0000174 AT91C_PB19_ERXCK | AT91C_PB18_ECOL | AT91C_PB17_ERXDV |
175 AT91C_PB16_ERX3 | AT91C_PB15_ERX2 | AT91C_PB14_ETXER |
176 AT91C_PB13_ETX3 | AT91C_PB12_ETX2;
wdenk2abbe072003-06-16 23:50:08 +0000177
wdenk0b8fa032004-04-25 14:37:29 +0000178 /* Select B Register */
179 *AT91C_PIOB_BSR = AT91C_PB25_EF100 | AT91C_PB19_ERXCK | AT91C_PB18_ECOL |
180 AT91C_PB17_ERXDV | AT91C_PB16_ERX3 | AT91C_PB15_ERX2 |
181 AT91C_PB14_ETXER | AT91C_PB13_ETX3 | AT91C_PB12_ETX2;
wdenk9d5028c2004-11-21 00:06:33 +0000182#endif
wdenk2abbe072003-06-16 23:50:08 +0000183
184 *AT91C_PMC_PCER = 1 << AT91C_ID_EMAC; /* Peripheral Clock Enable Register */
185
186 p_mac->EMAC_CFG |= AT91C_EMAC_CSR; /* Clear statistics */
187
188 /* Init Ehternet buffers */
189 rbfdt = (rbf_t *) RBF_FRAMEBTD;
190 for (i = 0; i < RBF_FRAMEMAX; i++) {
191 rbfdt[i].addr = RBF_FRAMEBUF + RBF_FRAMELEN * i;
192 rbfdt[i].size = 0;
193 }
194 rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
195 rbfp = &rbfdt[0];
196
wdenk0b8fa032004-04-25 14:37:29 +0000197 p_mac->EMAC_SA2L = (bd->bi_enetaddr[3] << 24) | (bd->bi_enetaddr[2] << 16)
198 | (bd->bi_enetaddr[1] << 8) | (bd->bi_enetaddr[0]);
199 p_mac->EMAC_SA2H = (bd->bi_enetaddr[5] << 8) | (bd->bi_enetaddr[4]);
200
201 p_mac->EMAC_RBQP = (long) (&rbfdt[0]);
202 p_mac->EMAC_RSR &= ~(AT91C_EMAC_RSR_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA);
203
204 p_mac->EMAC_CFG = (p_mac->EMAC_CFG | AT91C_EMAC_CAF | AT91C_EMAC_NBC)
205 & ~AT91C_EMAC_CLK;
206
207#ifdef CONFIG_AT91C_USE_RMII
208 p_mac->EMAC_CFG |= AT91C_EMAC_RMII;
209#endif
210
211 p_mac->EMAC_CTL |= AT91C_EMAC_TE | AT91C_EMAC_RE;
212
wdenk429168e2004-08-02 23:39:03 +0000213 at91rm92000_GetPhyInterface (& PhyOps);
wdenk2abbe072003-06-16 23:50:08 +0000214
wdenk429168e2004-08-02 23:39:03 +0000215 if (!PhyOps.IsPhyConnected (p_mac))
wdenk2abbe072003-06-16 23:50:08 +0000216 printf ("PHY not connected!!\n\r");
217
218 /* MII management start from here */
219 if (!(p_mac->EMAC_SR & AT91C_EMAC_LINK)) {
wdenk429168e2004-08-02 23:39:03 +0000220 if (!(ret = PhyOps.Init (p_mac))) {
wdenk2abbe072003-06-16 23:50:08 +0000221 printf ("MAC: error during MII initialization\n");
222 return 0;
223 }
224 } else {
225 printf ("No link\n\r");
226 return 0;
227 }
228
wdenk2abbe072003-06-16 23:50:08 +0000229 return 0;
230}
231
232int eth_send (volatile void *packet, int length)
233{
234 while (!(p_mac->EMAC_TSR & AT91C_EMAC_BNQ));
235 p_mac->EMAC_TAR = (long) packet;
236 p_mac->EMAC_TCR = length;
237 while (p_mac->EMAC_TCR & 0x7ff);
238 p_mac->EMAC_TSR |= AT91C_EMAC_COMP;
239 return 0;
240}
241
242int eth_rx (void)
243{
244 int size;
245
246 if (!(rbfp->addr & RBF_OWNER))
247 return 0;
248
249 size = rbfp->size & RBF_SIZE;
250 NetReceive ((volatile uchar *) (rbfp->addr & RBF_ADDR), size);
251
252 rbfp->addr &= ~RBF_OWNER;
253 if (rbfp->addr & RBF_WRAP)
254 rbfp = &rbfdt[0];
255 else
256 rbfp++;
257
258 p_mac->EMAC_RSR |= AT91C_EMAC_REC;
259
260 return size;
261}
262
263void eth_halt (void)
264{
265};
wdenk074cff02004-02-24 00:16:43 +0000266
267#if (CONFIG_COMMANDS & CFG_CMD_MII)
268int miiphy_read(unsigned char addr, unsigned char reg, unsigned short * value)
269{
270 at91rm9200_EmacEnableMDIO (p_mac);
271 at91rm9200_EmacReadPhy (p_mac, reg, value);
272 at91rm9200_EmacDisableMDIO (p_mac);
273 return 0;
274}
275
276int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
277{
278 at91rm9200_EmacEnableMDIO (p_mac);
279 at91rm9200_EmacWritePhy (p_mac, reg, &value);
280 at91rm9200_EmacDisableMDIO (p_mac);
281 return 0;
282}
283#endif /* CONFIG_COMMANDS & CFG_CMD_MII */
284
wdenk2abbe072003-06-16 23:50:08 +0000285#endif /* CONFIG_COMMANDS & CFG_CMD_NET */
wdenk074cff02004-02-24 00:16:43 +0000286
wdenk2abbe072003-06-16 23:50:08 +0000287#endif /* CONFIG_DRIVER_ETHER */