to make u-boot work for fat32 filesystem
[jz_uboot.git] / drivers / cs8900.c
blob6a83c707af2fe5c0da6abe3ebe2f8b465682f787
1 /*
2 * Cirrus Logic CS8900A Ethernet
4 * (C) 2003 Wolfgang Denk, wd@denx.de
5 * Extension to synchronize ethaddr environment variable
6 * against value in EEPROM
8 * (C) Copyright 2002
9 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Marius Groeger <mgroeger@sysgo.de>
12 * Copyright (C) 1999 Ben Williamson <benw@pobox.com>
14 * See file CREDITS for list of people who contributed to this
15 * project.
17 * This program is loaded into SRAM in bootstrap mode, where it waits
18 * for commands on UART1 to read and write memory, jump to code etc.
19 * A design goal for this program is to be entirely independent of the
20 * target board. Anything with a CL-PS7111 or EP7211 should be able to run
21 * this code in bootstrap mode. All the board specifics can be handled on
22 * the host.
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <common.h>
40 #include <command.h>
41 #include "cs8900.h"
42 #include <net.h>
43 #include "asm-mips/jz4740.h"
45 #ifdef CONFIG_DRIVER_CS8900
47 #if (CONFIG_COMMANDS & CFG_CMD_NET)
49 #undef DEBUG
51 /* packet page register access functions */
53 #ifdef CS8900_BUS32
54 /* we don't need 16 bit initialisation on 32 bit bus */
55 #define get_reg_init_bus(x) get_reg((x))
56 #else
57 static unsigned short get_reg_init_bus (int regno)
59 /* force 16 bit busmode */
60 volatile unsigned char c;
62 c = CS8900_BUS16_0;
63 c = CS8900_BUS16_1;
64 c = CS8900_BUS16_0;
65 c = CS8900_BUS16_1;
66 c = CS8900_BUS16_0;
68 CS8900_PPTR = regno;
69 return (unsigned short) CS8900_PDATA;
71 #endif
73 static unsigned short get_reg (int regno)
75 CS8900_PPTR = regno;
76 return (unsigned short) CS8900_PDATA;
80 static void put_reg (int regno, unsigned short val)
82 CS8900_PPTR = regno;
83 CS8900_PDATA = val;
86 static void eth_reset (void)
88 int tmo;
89 unsigned short us;
91 /* reset NIC */
92 put_reg (PP_SelfCTL, get_reg (PP_SelfCTL) | PP_SelfCTL_Reset);
94 /* wait for 200ms */
95 udelay (200000);
96 /* Wait until the chip is reset */
98 tmo = get_timer (0) + 1 * CFG_HZ;
99 while ((((us = get_reg_init_bus (PP_SelfSTAT)) & PP_SelfSTAT_InitD) == 0)
100 && tmo < get_timer (0))
101 /*NOP*/;
104 static void eth_reginit (void)
106 /* receive only error free packets addressed to this card */
107 put_reg (PP_RxCTL, PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
108 /* do not generate any interrupts on receive operations */
109 put_reg (PP_RxCFG, 0);
110 /* do not generate any interrupts on transmit operations */
111 put_reg (PP_TxCFG, 0);
112 /* do not generate any interrupts on buffer operations */
113 put_reg (PP_BufCFG, 0);
114 /* enable transmitter/receiver mode */
115 put_reg (PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
118 void cs8900_get_enetaddr (uchar * addr)
120 int i;
121 unsigned char env_enetaddr[6];
122 char *tmp = getenv ("ethaddr");
123 char *end;
125 for (i=0; i<6; i++) {
126 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
127 if (tmp)
128 tmp = (*end) ? end+1 : end;
131 /* verify chip id */
132 if (get_reg_init_bus (PP_ChipID) != 0x630e)
133 return;
134 eth_reset ();
135 if ((get_reg (PP_SelfST) & (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
136 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
138 /* Load the MAC from EEPROM */
139 for (i = 0; i < 6 / 2; i++) {
140 unsigned int Addr;
142 Addr = get_reg (PP_IA + i * 2);
143 addr[i * 2] = Addr & 0xFF;
144 addr[i * 2 + 1] = Addr >> 8;
147 if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6) != 0 &&
148 memcmp(env_enetaddr, addr, 6) != 0) {
149 printf ("\nWarning: MAC addresses don't match:\n");
150 printf ("\tHW MAC address: "
151 "%02X:%02X:%02X:%02X:%02X:%02X\n",
152 addr[0], addr[1],
153 addr[2], addr[3],
154 addr[4], addr[5] );
155 printf ("\t\"ethaddr\" value: "
156 "%02X:%02X:%02X:%02X:%02X:%02X\n",
157 env_enetaddr[0], env_enetaddr[1],
158 env_enetaddr[2], env_enetaddr[3],
159 env_enetaddr[4], env_enetaddr[5]) ;
160 debug ("### Set MAC addr from environment\n");
161 memcpy (addr, env_enetaddr, 6);
163 if (!tmp) {
164 char ethaddr[20];
165 sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
166 addr[0], addr[1],
167 addr[2], addr[3],
168 addr[4], addr[5]) ;
169 debug ("### Set environment from HW MAC addr = \"%s\"\n", ethaddr);
170 setenv ("ethaddr", ethaddr);
176 void eth_halt (void)
178 /* disable transmitter/receiver mode */
179 put_reg (PP_LineCTL, 0);
181 /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
182 get_reg_init_bus (PP_ChipID);
185 void gpio_init_cs8900(void)
187 __gpio_as_func0(60); //cs4
188 __gpio_as_func0(61); //cs4
189 __gpio_as_func0(62); //cs4
190 __gpio_as_irq_high_level(59); //irq
191 __gpio_disable_pull(59); //disable pull
192 REG_EMC_SMCR4 |= (1 << 6); //16bit
195 int eth_init (bd_t * bd)
197 gpio_init_cs8900();
198 /* verify chip id */
199 if (get_reg_init_bus (PP_ChipID) != 0x630e) {
200 printf ("CS8900 Ethernet chip not found?!\n");
201 return 0;
204 eth_reset ();
205 /* set the ethernet address */
206 put_reg (PP_IA + 0, bd->bi_enetaddr[0] | (bd->bi_enetaddr[1] << 8));
207 put_reg (PP_IA + 2, bd->bi_enetaddr[2] | (bd->bi_enetaddr[3] << 8));
208 put_reg (PP_IA + 4, bd->bi_enetaddr[4] | (bd->bi_enetaddr[5] << 8));
210 eth_reginit ();
211 return 0;
214 /* Get a data block via Ethernet */
215 extern int eth_rx (void)
217 int i;
218 unsigned short rxlen;
219 unsigned short *addr;
220 unsigned short status;
222 status = get_reg (PP_RER);
224 if ((status & PP_RER_RxOK) == 0)
225 return 0;
227 status = CS8900_RTDATA; /* stat */
228 rxlen = CS8900_RTDATA; /* len */
230 #ifdef DEBUG
231 if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
232 printf ("packet too big!\n");
233 #endif
234 for (addr = (unsigned short *) NetRxPackets[0], i = rxlen >> 1; i > 0;
235 i--)
236 *addr++ = CS8900_RTDATA;
237 if (rxlen & 1)
238 *addr++ = CS8900_RTDATA;
240 /* Pass the packet up to the protocol layers. */
241 NetReceive (NetRxPackets[0], rxlen);
243 return rxlen;
246 /* Send a data block via Ethernet. */
247 extern int eth_send (volatile void *packet, int length)
249 volatile unsigned short *addr;
250 int tmo;
251 unsigned short s;
253 retry:
254 /* initiate a transmit sequence */
255 CS8900_TxCMD = PP_TxCmd_TxStart_Full;
256 CS8900_TxLEN = length;
258 /* Test to see if the chip has allocated memory for the packet */
259 if ((get_reg (PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
260 /* Oops... this should not happen! */
261 #ifdef DEBUG
262 printf ("cs: unable to send packet; retrying...\n");
263 #endif
264 for (tmo = get_timer (0) + 5 * CFG_HZ; get_timer (0) < tmo;)
265 /*NOP*/;
266 eth_reset ();
267 eth_reginit ();
268 goto retry;
271 /* Write the contents of the packet */
272 /* assume even number of bytes */
273 for (addr = packet; length > 0; length -= 2)
274 CS8900_RTDATA = *addr++;
276 /* wait for transfer to succeed */
277 tmo = get_timer (0) + 5 * CFG_HZ;
278 while ((s = get_reg (PP_TER) & ~0x1F) == 0) {
279 if (get_timer (0) >= tmo)
280 break;
283 /* nothing */ ;
284 if ((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
285 #ifdef DEBUG
286 printf ("\ntransmission error %#x\n", s);
287 #endif
290 return 0;
293 static void cs8900_e2prom_ready(void)
295 while(get_reg(PP_SelfST) & SI_BUSY);
298 /***********************************************************/
299 /* read a 16-bit word out of the EEPROM */
300 /***********************************************************/
302 int cs8900_e2prom_read(unsigned char addr, unsigned short *value)
304 cs8900_e2prom_ready();
305 put_reg(PP_EECMD, EEPROM_READ_CMD | addr);
306 cs8900_e2prom_ready();
307 *value = get_reg(PP_EEData);
309 return 0;
313 /***********************************************************/
314 /* write a 16-bit word into the EEPROM */
315 /***********************************************************/
317 int cs8900_e2prom_write(unsigned char addr, unsigned short value)
319 cs8900_e2prom_ready();
320 put_reg(PP_EECMD, EEPROM_WRITE_EN);
321 cs8900_e2prom_ready();
322 put_reg(PP_EEData, value);
323 put_reg(PP_EECMD, EEPROM_WRITE_CMD | addr);
324 cs8900_e2prom_ready();
325 put_reg(PP_EECMD, EEPROM_WRITE_DIS);
326 cs8900_e2prom_ready();
328 return 0;
331 #endif /* COMMANDS & CFG_NET */
333 #endif /* CONFIG_DRIVER_CS8900 */