blob: 91eab95eed1e1844384ed34a9438747546d8868a [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>
Marian Balakowicz63ff0042005-10-28 22:30:33 +020026#include <miiphy.h>
David Brownell7168eba2009-06-09 11:14:24 -070027#include <asm/mach-types.h>
wdenk2abbe072003-06-16 23:50:08 +000028
29/* ----- Ethernet Buffer definitions ----- */
30
31typedef struct {
32 unsigned long addr, size;
33} rbf_t;
34
35#define RBF_ADDR 0xfffffffc
36#define RBF_OWNER (1<<0)
37#define RBF_WRAP (1<<1)
38#define RBF_BROADCAST (1<<31)
39#define RBF_MULTICAST (1<<30)
40#define RBF_UNICAST (1<<29)
41#define RBF_EXTERNAL (1<<28)
42#define RBF_UNKOWN (1<<27)
43#define RBF_SIZE 0x07ff
44#define RBF_LOCAL4 (1<<26)
45#define RBF_LOCAL3 (1<<25)
46#define RBF_LOCAL2 (1<<24)
47#define RBF_LOCAL1 (1<<23)
48
Wolfgang Denk95f9dda2005-10-09 00:33:37 +020049#define RBF_FRAMEMAX 64
wdenk2abbe072003-06-16 23:50:08 +000050#define RBF_FRAMELEN 0x600
51
wdenk2abbe072003-06-16 23:50:08 +000052#ifdef CONFIG_DRIVER_ETHER
53
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -050054#if defined(CONFIG_CMD_NET)
wdenk2abbe072003-06-16 23:50:08 +000055
Wolfgang Denk95f9dda2005-10-09 00:33:37 +020056/* alignment as per Errata #11 (64 bytes) is insufficient! */
Peter Tyserf9a109b2009-04-20 11:08:46 -050057rbf_t rbfdt[RBF_FRAMEMAX] __attribute__((aligned(512)));
Wolfgang Denk95f9dda2005-10-09 00:33:37 +020058rbf_t *rbfp;
59
Peter Tyserf9a109b2009-04-20 11:08:46 -050060unsigned char rbf_framebuf[RBF_FRAMEMAX][RBF_FRAMELEN]
61 __attribute__((aligned(4)));
Wolfgang Denk95f9dda2005-10-09 00:33:37 +020062
wdenk2abbe072003-06-16 23:50:08 +000063/* structure to interface the PHY */
wdenk429168e2004-08-02 23:39:03 +000064AT91S_PhyOps PhyOps;
wdenk2abbe072003-06-16 23:50:08 +000065
66AT91PS_EMAC p_mac;
67
wdenk2abbe072003-06-16 23:50:08 +000068/*********** EMAC Phy layer Management functions *************************/
69/*
wdenk8bde7f72003-06-27 21:31:46 +000070 * Name:
wdenk2abbe072003-06-16 23:50:08 +000071 * at91rm9200_EmacEnableMDIO
wdenk8bde7f72003-06-27 21:31:46 +000072 * Description:
wdenk2abbe072003-06-16 23:50:08 +000073 * Enables the MDIO bit in MAC control register
wdenk8bde7f72003-06-27 21:31:46 +000074 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +000075 * p_mac - pointer to struct AT91S_EMAC
wdenk8bde7f72003-06-27 21:31:46 +000076 * Return value:
wdenk2abbe072003-06-16 23:50:08 +000077 * none
78 */
wdenk429168e2004-08-02 23:39:03 +000079void at91rm9200_EmacEnableMDIO (AT91PS_EMAC p_mac)
wdenk2abbe072003-06-16 23:50:08 +000080{
81 /* Mac CTRL reg set for MDIO enable */
82 p_mac->EMAC_CTL |= AT91C_EMAC_MPE; /* Management port enable */
83}
84
85/*
wdenk8bde7f72003-06-27 21:31:46 +000086 * Name:
wdenk2abbe072003-06-16 23:50:08 +000087 * at91rm9200_EmacDisableMDIO
wdenk8bde7f72003-06-27 21:31:46 +000088 * Description:
wdenk2abbe072003-06-16 23:50:08 +000089 * Disables the MDIO bit in MAC control register
wdenk8bde7f72003-06-27 21:31:46 +000090 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +000091 * p_mac - pointer to struct AT91S_EMAC
wdenk8bde7f72003-06-27 21:31:46 +000092 * Return value:
wdenk2abbe072003-06-16 23:50:08 +000093 * none
94 */
wdenk429168e2004-08-02 23:39:03 +000095void at91rm9200_EmacDisableMDIO (AT91PS_EMAC p_mac)
wdenk2abbe072003-06-16 23:50:08 +000096{
97 /* Mac CTRL reg set for MDIO disable */
98 p_mac->EMAC_CTL &= ~AT91C_EMAC_MPE; /* Management port disable */
99}
100
101
102/*
wdenk8bde7f72003-06-27 21:31:46 +0000103 * Name:
wdenk2abbe072003-06-16 23:50:08 +0000104 * at91rm9200_EmacReadPhy
wdenk8bde7f72003-06-27 21:31:46 +0000105 * Description:
wdenk2abbe072003-06-16 23:50:08 +0000106 * Reads data from the PHY register
wdenk8bde7f72003-06-27 21:31:46 +0000107 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +0000108 * dev - pointer to struct net_device
109 * RegisterAddress - unsigned char
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200110 * pInput - pointer to value read from register
wdenk8bde7f72003-06-27 21:31:46 +0000111 * Return value:
wdenk2abbe072003-06-16 23:50:08 +0000112 * TRUE - if data read successfully
113 */
wdenk429168e2004-08-02 23:39:03 +0000114UCHAR at91rm9200_EmacReadPhy (AT91PS_EMAC p_mac,
wdenk2abbe072003-06-16 23:50:08 +0000115 unsigned char RegisterAddress,
116 unsigned short *pInput)
117{
118 p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) |
wdenk074cff02004-02-24 00:16:43 +0000119 (AT91C_EMAC_RW_R) |
120 (RegisterAddress << 18) |
121 (AT91C_EMAC_CODE_802_3);
wdenk2abbe072003-06-16 23:50:08 +0000122
123 udelay (10000);
124
125 *pInput = (unsigned short) p_mac->EMAC_MAN;
126
127 return TRUE;
128}
129
130
131/*
wdenk8bde7f72003-06-27 21:31:46 +0000132 * Name:
wdenk2abbe072003-06-16 23:50:08 +0000133 * at91rm9200_EmacWritePhy
wdenk8bde7f72003-06-27 21:31:46 +0000134 * Description:
wdenk2abbe072003-06-16 23:50:08 +0000135 * Writes data to the PHY register
wdenk8bde7f72003-06-27 21:31:46 +0000136 * Arguments:
wdenk2abbe072003-06-16 23:50:08 +0000137 * dev - pointer to struct net_device
138 * RegisterAddress - unsigned char
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200139 * pOutput - pointer to value to be written in the register
wdenk8bde7f72003-06-27 21:31:46 +0000140 * Return value:
wdenk2abbe072003-06-16 23:50:08 +0000141 * TRUE - if data read successfully
142 */
wdenk429168e2004-08-02 23:39:03 +0000143UCHAR at91rm9200_EmacWritePhy (AT91PS_EMAC p_mac,
wdenka3ad8e22003-10-19 23:22:11 +0000144 unsigned char RegisterAddress,
145 unsigned short *pOutput)
wdenk2abbe072003-06-16 23:50:08 +0000146{
147 p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) |
148 AT91C_EMAC_CODE_802_3 | AT91C_EMAC_RW_W |
wdenka3ad8e22003-10-19 23:22:11 +0000149 (RegisterAddress << 18) | *pOutput;
wdenk2abbe072003-06-16 23:50:08 +0000150
151 udelay (10000);
152
153 return TRUE;
154}
155
wdenk2abbe072003-06-16 23:50:08 +0000156int eth_init (bd_t * bd)
157{
158 int ret;
159 int i;
Mike Frysinger6bacfa62009-02-11 19:18:41 -0500160 uchar enetaddr[6];
wdenk2abbe072003-06-16 23:50:08 +0000161
162 p_mac = AT91C_BASE_EMAC;
163
wdenk0b8fa032004-04-25 14:37:29 +0000164 /* PIO Disable Register */
165 *AT91C_PIOA_PDR = AT91C_PA16_EMDIO | AT91C_PA15_EMDC | AT91C_PA14_ERXER |
166 AT91C_PA13_ERX1 | AT91C_PA12_ERX0 | AT91C_PA11_ECRS_ECRSDV |
167 AT91C_PA10_ETX1 | AT91C_PA9_ETX0 | AT91C_PA8_ETXEN |
168 AT91C_PA7_ETXCK_EREFCK;
wdenk2abbe072003-06-16 23:50:08 +0000169
wdenkea287de2005-04-01 00:25:43 +0000170#ifdef CONFIG_AT91C_USE_RMII
171 *AT91C_PIOB_PDR = AT91C_PB19_ERXCK;
172 *AT91C_PIOB_BSR = AT91C_PB19_ERXCK;
173#else
174 *AT91C_PIOB_PDR = AT91C_PB19_ERXCK | AT91C_PB18_ECOL | AT91C_PB17_ERXDV |
wdenk0b8fa032004-04-25 14:37:29 +0000175 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 */
wdenkea287de2005-04-01 00:25:43 +0000179 *AT91C_PIOB_BSR = AT91C_PB19_ERXCK | AT91C_PB18_ECOL |
wdenk0b8fa032004-04-25 14:37:29 +0000180 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
David Brownell7168eba2009-06-09 11:14:24 -0700188 /* Init Ethernet buffers */
wdenk2abbe072003-06-16 23:50:08 +0000189 for (i = 0; i < RBF_FRAMEMAX; i++) {
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200190 rbfdt[i].addr = (unsigned long)rbf_framebuf[i];
wdenk2abbe072003-06-16 23:50:08 +0000191 rbfdt[i].size = 0;
192 }
193 rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
194 rbfp = &rbfdt[0];
195
Mike Frysinger6bacfa62009-02-11 19:18:41 -0500196 eth_getenv_enetaddr("ethaddr", enetaddr);
David Brownell7168eba2009-06-09 11:14:24 -0700197
198 /* The CSB337 originally used a version of the MicroMonitor bootloader
199 * which saved Ethernet addresses in the "wrong" order. Operating
200 * systems (like Linux) know this, and apply a workaround. Replicate
201 * that MicroMonitor behavior so we avoid needing to make such OS code
202 * care about which bootloader was used.
203 */
204 if (machine_is_csb337()) {
205 p_mac->EMAC_SA2H = (enetaddr[0] << 8) | (enetaddr[1]);
206 p_mac->EMAC_SA2L = (enetaddr[2] << 24) | (enetaddr[3] << 16)
207 | (enetaddr[4] << 8) | (enetaddr[5]);
208 } else {
209 p_mac->EMAC_SA2L = (enetaddr[3] << 24) | (enetaddr[2] << 16)
210 | (enetaddr[1] << 8) | (enetaddr[0]);
211 p_mac->EMAC_SA2H = (enetaddr[5] << 8) | (enetaddr[4]);
212 }
wdenk0b8fa032004-04-25 14:37:29 +0000213
214 p_mac->EMAC_RBQP = (long) (&rbfdt[0]);
215 p_mac->EMAC_RSR &= ~(AT91C_EMAC_RSR_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA);
216
217 p_mac->EMAC_CFG = (p_mac->EMAC_CFG | AT91C_EMAC_CAF | AT91C_EMAC_NBC)
218 & ~AT91C_EMAC_CLK;
219
220#ifdef CONFIG_AT91C_USE_RMII
221 p_mac->EMAC_CFG |= AT91C_EMAC_RMII;
222#endif
223
wdenkba83a302005-04-04 12:23:03 +0000224#if (AT91C_MASTER_CLOCK > 40000000)
225 /* MDIO clock must not exceed 2.5 MHz, so enable MCK divider */
226 p_mac->EMAC_CFG |= AT91C_EMAC_CLK_HCLK_64;
227#endif
228
wdenk0b8fa032004-04-25 14:37:29 +0000229 p_mac->EMAC_CTL |= AT91C_EMAC_TE | AT91C_EMAC_RE;
230
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200231 at91rm9200_GetPhyInterface (& PhyOps);
wdenk2abbe072003-06-16 23:50:08 +0000232
wdenk429168e2004-08-02 23:39:03 +0000233 if (!PhyOps.IsPhyConnected (p_mac))
wdenk2abbe072003-06-16 23:50:08 +0000234 printf ("PHY not connected!!\n\r");
235
236 /* MII management start from here */
237 if (!(p_mac->EMAC_SR & AT91C_EMAC_LINK)) {
wdenk429168e2004-08-02 23:39:03 +0000238 if (!(ret = PhyOps.Init (p_mac))) {
wdenk2abbe072003-06-16 23:50:08 +0000239 printf ("MAC: error during MII initialization\n");
240 return 0;
241 }
242 } else {
243 printf ("No link\n\r");
244 return 0;
245 }
246
wdenk2abbe072003-06-16 23:50:08 +0000247 return 0;
248}
249
250int eth_send (volatile void *packet, int length)
251{
252 while (!(p_mac->EMAC_TSR & AT91C_EMAC_BNQ));
253 p_mac->EMAC_TAR = (long) packet;
254 p_mac->EMAC_TCR = length;
255 while (p_mac->EMAC_TCR & 0x7ff);
256 p_mac->EMAC_TSR |= AT91C_EMAC_COMP;
257 return 0;
258}
259
260int eth_rx (void)
261{
262 int size;
263
264 if (!(rbfp->addr & RBF_OWNER))
265 return 0;
266
267 size = rbfp->size & RBF_SIZE;
268 NetReceive ((volatile uchar *) (rbfp->addr & RBF_ADDR), size);
269
270 rbfp->addr &= ~RBF_OWNER;
271 if (rbfp->addr & RBF_WRAP)
272 rbfp = &rbfdt[0];
273 else
274 rbfp++;
275
276 p_mac->EMAC_RSR |= AT91C_EMAC_REC;
277
278 return size;
279}
280
281void eth_halt (void)
282{
283};
wdenk074cff02004-02-24 00:16:43 +0000284
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500285#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200286int at91rm9200_miiphy_read(char *devname, unsigned char addr,
287 unsigned char reg, unsigned short * value)
wdenk074cff02004-02-24 00:16:43 +0000288{
289 at91rm9200_EmacEnableMDIO (p_mac);
290 at91rm9200_EmacReadPhy (p_mac, reg, value);
291 at91rm9200_EmacDisableMDIO (p_mac);
292 return 0;
293}
294
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200295int at91rm9200_miiphy_write(char *devname, unsigned char addr,
296 unsigned char reg, unsigned short value)
wdenk074cff02004-02-24 00:16:43 +0000297{
298 at91rm9200_EmacEnableMDIO (p_mac);
299 at91rm9200_EmacWritePhy (p_mac, reg, &value);
300 at91rm9200_EmacDisableMDIO (p_mac);
301 return 0;
302}
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200303
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500304#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200305
306int at91rm9200_miiphy_initialize(bd_t *bis)
307{
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500308#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200309 miiphy_register("at91rm9200phy", at91rm9200_miiphy_read, at91rm9200_miiphy_write);
310#endif
311 return 0;
312}
wdenk074cff02004-02-24 00:16:43 +0000313
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500314#endif
wdenk074cff02004-02-24 00:16:43 +0000315
wdenk2abbe072003-06-16 23:50:08 +0000316#endif /* CONFIG_DRIVER_ETHER */