blob: 9424fb2bb69cc697ea0fb3ab1a1dc54f511d7aec [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * Cirrus Logic CS8900A Ethernet
3 *
Ben Warren3bf5d832009-08-25 13:09:37 -07004 * (C) 2009 Ben Warren , biggerbadderben@gmail.com
5 * Converted to use CONFIG_NET_MULTI API
6 *
wdenk4fc95692003-02-28 00:49:47 +00007 * (C) 2003 Wolfgang Denk, wd@denx.de
8 * Extension to synchronize ethaddr environment variable
9 * against value in EEPROM
10 *
wdenkc6097192002-11-03 00:24:07 +000011 * (C) Copyright 2002
12 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Marius Groeger <mgroeger@sysgo.de>
14 *
15 * Copyright (C) 1999 Ben Williamson <benw@pobox.com>
16 *
17 * See file CREDITS for list of people who contributed to this
18 * project.
19 *
20 * This program is loaded into SRAM in bootstrap mode, where it waits
21 * for commands on UART1 to read and write memory, jump to code etc.
22 * A design goal for this program is to be entirely independent of the
23 * target board. Anything with a CL-PS7111 or EP7211 should be able to run
24 * this code in bootstrap mode. All the board specifics can be handled on
25 * the host.
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40 */
41
42#include <common.h>
43#include <command.h>
Ben Warren3bf5d832009-08-25 13:09:37 -070044#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000045#include <net.h>
Ben Warren3bf5d832009-08-25 13:09:37 -070046#include <malloc.h>
47#include "cs8900.h"
wdenkc6097192002-11-03 00:24:07 +000048
wdenk4ea537d2003-12-07 18:32:37 +000049#undef DEBUG
wdenkc6097192002-11-03 00:24:07 +000050
51/* packet page register access functions */
52
Ben Warren3bf5d832009-08-25 13:09:37 -070053#ifdef CONFIG_CS8900_BUS32
54
55#define REG_WRITE(v, a) writel((v),(a))
56#define REG_READ(a) readl((a))
57
wdenkc6097192002-11-03 00:24:07 +000058/* we don't need 16 bit initialisation on 32 bit bus */
Ben Warren3b6be932009-11-09 11:43:18 -080059#define get_reg_init_bus(r,d) get_reg((r),(d))
Ben Warren3bf5d832009-08-25 13:09:37 -070060
wdenkc6097192002-11-03 00:24:07 +000061#else
Ben Warren3bf5d832009-08-25 13:09:37 -070062
63#define REG_WRITE(v, a) writew((v),(a))
64#define REG_READ(a) readw((a))
65
66static u16 get_reg_init_bus(struct eth_device *dev, int regno)
wdenkc6097192002-11-03 00:24:07 +000067{
wdenk4fc95692003-02-28 00:49:47 +000068 /* force 16 bit busmode */
Ben Warren3bf5d832009-08-25 13:09:37 -070069 volatile u8 c;
70 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
71 uint8_t volatile * const iob = (uint8_t volatile * const)dev->iobase;
wdenk4fc95692003-02-28 00:49:47 +000072
Ben Warren3bf5d832009-08-25 13:09:37 -070073 c = readb(iob);
74 c = readb(iob + 1);
75 c = readb(iob);
76 c = readb(iob + 1);
77 c = readb(iob);
wdenkc6097192002-11-03 00:24:07 +000078
Ben Warren3bf5d832009-08-25 13:09:37 -070079 REG_WRITE(regno, &priv->regs->pptr);
80 return REG_READ(&priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000081}
82#endif
83
Ben Warren3bf5d832009-08-25 13:09:37 -070084static u16 get_reg(struct eth_device *dev, int regno)
wdenkc6097192002-11-03 00:24:07 +000085{
Ben Warren3bf5d832009-08-25 13:09:37 -070086 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
87 REG_WRITE(regno, &priv->regs->pptr);
88 return REG_READ(&priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000089}
90
91
Ben Warren3bf5d832009-08-25 13:09:37 -070092static void put_reg(struct eth_device *dev, int regno, u16 val)
wdenkc6097192002-11-03 00:24:07 +000093{
Ben Warren3bf5d832009-08-25 13:09:37 -070094 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
95 REG_WRITE(regno, &priv->regs->pptr);
96 REG_WRITE(val, &priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000097}
98
Ben Warren3bf5d832009-08-25 13:09:37 -070099static void cs8900_reset(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000100{
wdenk4fc95692003-02-28 00:49:47 +0000101 int tmo;
Ben Warren3bf5d832009-08-25 13:09:37 -0700102 u16 us;
wdenkc6097192002-11-03 00:24:07 +0000103
wdenk4fc95692003-02-28 00:49:47 +0000104 /* reset NIC */
Ben Warren3bf5d832009-08-25 13:09:37 -0700105 put_reg(dev, PP_SelfCTL, get_reg(dev, PP_SelfCTL) | PP_SelfCTL_Reset);
wdenkc6097192002-11-03 00:24:07 +0000106
wdenk4fc95692003-02-28 00:49:47 +0000107 /* wait for 200ms */
Ben Warren3bf5d832009-08-25 13:09:37 -0700108 udelay(200000);
wdenk4fc95692003-02-28 00:49:47 +0000109 /* Wait until the chip is reset */
wdenkc6097192002-11-03 00:24:07 +0000110
Ben Warren3bf5d832009-08-25 13:09:37 -0700111 tmo = get_timer(0) + 1 * CONFIG_SYS_HZ;
112 while ((((us = get_reg_init_bus(dev, PP_SelfSTAT)) &
113 PP_SelfSTAT_InitD) == 0) && tmo < get_timer(0))
wdenk4fc95692003-02-28 00:49:47 +0000114 /*NOP*/;
wdenkc6097192002-11-03 00:24:07 +0000115}
116
Ben Warren3bf5d832009-08-25 13:09:37 -0700117static void cs8900_reginit(struct eth_device *dev)
wdenk4ea537d2003-12-07 18:32:37 +0000118{
119 /* receive only error free packets addressed to this card */
Ben Warren3bf5d832009-08-25 13:09:37 -0700120 put_reg(dev, PP_RxCTL,
121 PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
wdenk4ea537d2003-12-07 18:32:37 +0000122 /* do not generate any interrupts on receive operations */
Ben Warren3bf5d832009-08-25 13:09:37 -0700123 put_reg(dev, PP_RxCFG, 0);
wdenk4ea537d2003-12-07 18:32:37 +0000124 /* do not generate any interrupts on transmit operations */
Ben Warren3bf5d832009-08-25 13:09:37 -0700125 put_reg(dev, PP_TxCFG, 0);
wdenk4ea537d2003-12-07 18:32:37 +0000126 /* do not generate any interrupts on buffer operations */
Ben Warren3bf5d832009-08-25 13:09:37 -0700127 put_reg(dev, PP_BufCFG, 0);
wdenk4ea537d2003-12-07 18:32:37 +0000128 /* enable transmitter/receiver mode */
Ben Warren3bf5d832009-08-25 13:09:37 -0700129 put_reg(dev, PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
wdenk4ea537d2003-12-07 18:32:37 +0000130}
131
Ben Warren3bf5d832009-08-25 13:09:37 -0700132void cs8900_get_enetaddr(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000133{
wdenk4fc95692003-02-28 00:49:47 +0000134 int i;
wdenk4fc95692003-02-28 00:49:47 +0000135
136 /* verify chip id */
Ben Warren3bf5d832009-08-25 13:09:37 -0700137 if (get_reg_init_bus(dev, PP_ChipID) != 0x630e)
wdenk4fc95692003-02-28 00:49:47 +0000138 return;
Ben Warren3bf5d832009-08-25 13:09:37 -0700139 cs8900_reset(dev);
140 if ((get_reg(dev, PP_SelfSTAT) &
141 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
142 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
wdenk4fc95692003-02-28 00:49:47 +0000143
144 /* Load the MAC from EEPROM */
Ben Warren3bf5d832009-08-25 13:09:37 -0700145 for (i = 0; i < 3; i++) {
146 u32 Addr;
wdenk4fc95692003-02-28 00:49:47 +0000147
Ben Warren3bf5d832009-08-25 13:09:37 -0700148 Addr = get_reg(dev, PP_IA + i * 2);
149 dev->enetaddr[i * 2] = Addr & 0xFF;
150 dev->enetaddr[i * 2 + 1] = Addr >> 8;
wdenk4fc95692003-02-28 00:49:47 +0000151 }
wdenkc6097192002-11-03 00:24:07 +0000152 }
wdenkc6097192002-11-03 00:24:07 +0000153}
154
Ben Warren3bf5d832009-08-25 13:09:37 -0700155void cs8900_halt(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000156{
wdenk4fc95692003-02-28 00:49:47 +0000157 /* disable transmitter/receiver mode */
Ben Warren3bf5d832009-08-25 13:09:37 -0700158 put_reg(dev, PP_LineCTL, 0);
wdenkc6097192002-11-03 00:24:07 +0000159
wdenk4fc95692003-02-28 00:49:47 +0000160 /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
Ben Warren3bf5d832009-08-25 13:09:37 -0700161 get_reg_init_bus(dev, PP_ChipID);
wdenkc6097192002-11-03 00:24:07 +0000162}
163
Ben Warren3bf5d832009-08-25 13:09:37 -0700164static int cs8900_init(struct eth_device *dev, bd_t * bd)
wdenkc6097192002-11-03 00:24:07 +0000165{
Ben Warren3bf5d832009-08-25 13:09:37 -0700166 uchar *enetaddr = dev->enetaddr;
167 u16 id;
Mike Frysingerca768132009-02-11 19:06:09 -0500168
wdenk4fc95692003-02-28 00:49:47 +0000169 /* verify chip id */
Ben Warren3bf5d832009-08-25 13:09:37 -0700170 id = get_reg_init_bus(dev, PP_ChipID);
171 if (id != 0x630e) {
172 printf ("CS8900 Ethernet chip not found: "
173 "ID=0x%04x instead 0x%04x\n", id, 0x630e);
174 return 1;
wdenk4fc95692003-02-28 00:49:47 +0000175 }
wdenkc6097192002-11-03 00:24:07 +0000176
Ben Warren3bf5d832009-08-25 13:09:37 -0700177 cs8900_reset (dev);
wdenk4fc95692003-02-28 00:49:47 +0000178 /* set the ethernet address */
Ben Warren3bf5d832009-08-25 13:09:37 -0700179 put_reg(dev, PP_IA + 0, enetaddr[0] | (enetaddr[1] << 8));
180 put_reg(dev, PP_IA + 2, enetaddr[2] | (enetaddr[3] << 8));
181 put_reg(dev, PP_IA + 4, enetaddr[4] | (enetaddr[5] << 8));
wdenkc6097192002-11-03 00:24:07 +0000182
Ben Warren3bf5d832009-08-25 13:09:37 -0700183 cs8900_reginit(dev);
wdenk4fc95692003-02-28 00:49:47 +0000184 return 0;
wdenkc6097192002-11-03 00:24:07 +0000185}
186
187/* Get a data block via Ethernet */
Ben Warren3bf5d832009-08-25 13:09:37 -0700188static int cs8900_recv(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000189{
wdenk4fc95692003-02-28 00:49:47 +0000190 int i;
Ben Warren3bf5d832009-08-25 13:09:37 -0700191 u16 rxlen;
192 u16 *addr;
193 u16 status;
wdenkc6097192002-11-03 00:24:07 +0000194
Ben Warren3bf5d832009-08-25 13:09:37 -0700195 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
196
197 status = get_reg(dev, PP_RER);
wdenkc6097192002-11-03 00:24:07 +0000198
wdenk4fc95692003-02-28 00:49:47 +0000199 if ((status & PP_RER_RxOK) == 0)
200 return 0;
wdenkc6097192002-11-03 00:24:07 +0000201
Ben Warren3bf5d832009-08-25 13:09:37 -0700202 status = REG_READ(&priv->regs->rtdata);
203 rxlen = REG_READ(&priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000204
wdenk4fc95692003-02-28 00:49:47 +0000205 if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
Ben Warren3bf5d832009-08-25 13:09:37 -0700206 debug("packet too big!\n");
207 for (addr = (u16 *) NetRxPackets[0], i = rxlen >> 1; i > 0;
wdenk4fc95692003-02-28 00:49:47 +0000208 i--)
Ben Warren3bf5d832009-08-25 13:09:37 -0700209 *addr++ = REG_READ(&priv->regs->rtdata);
wdenk4fc95692003-02-28 00:49:47 +0000210 if (rxlen & 1)
Ben Warren3bf5d832009-08-25 13:09:37 -0700211 *addr++ = REG_READ(&priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000212
wdenk4fc95692003-02-28 00:49:47 +0000213 /* Pass the packet up to the protocol layers. */
214 NetReceive (NetRxPackets[0], rxlen);
wdenk4fc95692003-02-28 00:49:47 +0000215 return rxlen;
wdenkc6097192002-11-03 00:24:07 +0000216}
217
218/* Send a data block via Ethernet. */
Ben Warren3bf5d832009-08-25 13:09:37 -0700219static int cs8900_send(struct eth_device *dev,
220 volatile void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000221{
Ben Warren3bf5d832009-08-25 13:09:37 -0700222 volatile u16 *addr;
wdenk4fc95692003-02-28 00:49:47 +0000223 int tmo;
Ben Warren3bf5d832009-08-25 13:09:37 -0700224 u16 s;
225 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
wdenkc6097192002-11-03 00:24:07 +0000226
227retry:
wdenk4fc95692003-02-28 00:49:47 +0000228 /* initiate a transmit sequence */
Ben Warren3bf5d832009-08-25 13:09:37 -0700229 REG_WRITE(PP_TxCmd_TxStart_Full, &priv->regs->txcmd);
230 REG_WRITE(length, &priv->regs->txlen);
wdenkc6097192002-11-03 00:24:07 +0000231
wdenk4fc95692003-02-28 00:49:47 +0000232 /* Test to see if the chip has allocated memory for the packet */
Ben Warren3bf5d832009-08-25 13:09:37 -0700233 if ((get_reg(dev, PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
wdenk4fc95692003-02-28 00:49:47 +0000234 /* Oops... this should not happen! */
Ben Warren3bf5d832009-08-25 13:09:37 -0700235 debug("cs: unable to send packet; retrying...\n");
236 for (tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
237 get_timer(0) < tmo;)
wdenk4fc95692003-02-28 00:49:47 +0000238 /*NOP*/;
Ben Warren3bf5d832009-08-25 13:09:37 -0700239 cs8900_reset(dev);
240 cs8900_reginit(dev);
wdenk4fc95692003-02-28 00:49:47 +0000241 goto retry;
242 }
wdenkc6097192002-11-03 00:24:07 +0000243
wdenk4fc95692003-02-28 00:49:47 +0000244 /* Write the contents of the packet */
245 /* assume even number of bytes */
246 for (addr = packet; length > 0; length -= 2)
Ben Warren3bf5d832009-08-25 13:09:37 -0700247 REG_WRITE(*addr++, &priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000248
wdenk4fc95692003-02-28 00:49:47 +0000249 /* wait for transfer to succeed */
Ben Warren3bf5d832009-08-25 13:09:37 -0700250 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
251 while ((s = get_reg(dev, PP_TER) & ~0x1F) == 0) {
252 if (get_timer(0) >= tmo)
wdenk4fc95692003-02-28 00:49:47 +0000253 break;
254 }
wdenkc6097192002-11-03 00:24:07 +0000255
wdenk4fc95692003-02-28 00:49:47 +0000256 /* nothing */ ;
Ben Warren3bf5d832009-08-25 13:09:37 -0700257 if((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
258 debug("\ntransmission error %#x\n", s);
wdenk4fc95692003-02-28 00:49:47 +0000259 }
wdenkc6097192002-11-03 00:24:07 +0000260
wdenk4fc95692003-02-28 00:49:47 +0000261 return 0;
wdenkc6097192002-11-03 00:24:07 +0000262}
263
Ben Warren3bf5d832009-08-25 13:09:37 -0700264static void cs8900_e2prom_ready(struct eth_device *dev)
wdenk1fe2c702003-03-06 21:55:29 +0000265{
Ben Warren3bf5d832009-08-25 13:09:37 -0700266 while (get_reg(dev, PP_SelfSTAT) & SI_BUSY)
Guennadi Liakhovetskifddcc0c2008-04-03 13:36:18 +0200267 ;
wdenk1fe2c702003-03-06 21:55:29 +0000268}
269
270/***********************************************************/
271/* read a 16-bit word out of the EEPROM */
272/***********************************************************/
273
Ben Warren3bf5d832009-08-25 13:09:37 -0700274int cs8900_e2prom_read(struct eth_device *dev,
275 u8 addr, u16 *value)
wdenk1fe2c702003-03-06 21:55:29 +0000276{
Ben Warren3bf5d832009-08-25 13:09:37 -0700277 cs8900_e2prom_ready(dev);
278 put_reg(dev, PP_EECMD, EEPROM_READ_CMD | addr);
279 cs8900_e2prom_ready(dev);
280 *value = get_reg(dev, PP_EEData);
wdenk1fe2c702003-03-06 21:55:29 +0000281
282 return 0;
283}
284
285
286/***********************************************************/
287/* write a 16-bit word into the EEPROM */
288/***********************************************************/
289
Ben Warren3bf5d832009-08-25 13:09:37 -0700290int cs8900_e2prom_write(struct eth_device *dev, u8 addr, u16 value)
291{
292 cs8900_e2prom_ready(dev);
293 put_reg(dev, PP_EECMD, EEPROM_WRITE_EN);
294 cs8900_e2prom_ready(dev);
295 put_reg(dev, PP_EEData, value);
296 put_reg(dev, PP_EECMD, EEPROM_WRITE_CMD | addr);
297 cs8900_e2prom_ready(dev);
298 put_reg(dev, PP_EECMD, EEPROM_WRITE_DIS);
299 cs8900_e2prom_ready(dev);
300
301 return 0;
302}
303
304int cs8900_initialize(u8 dev_num, int base_addr)
wdenk1fe2c702003-03-06 21:55:29 +0000305{
Ben Warren3bf5d832009-08-25 13:09:37 -0700306 struct eth_device *dev;
307 struct cs8900_priv *priv;
308
309 dev = malloc(sizeof(*dev));
310 if (!dev) {
Ben Warren3bf5d832009-08-25 13:09:37 -0700311 return 0;
312 }
313 memset(dev, 0, sizeof(*dev));
314
315 priv = malloc(sizeof(*priv));
316 if (!priv) {
Matthias Kaehlcke034c1612010-01-21 22:16:34 +0100317 free(dev);
Ben Warren3bf5d832009-08-25 13:09:37 -0700318 return 0;
319 }
320 memset(priv, 0, sizeof(*priv));
321 priv->regs = (struct cs8900_regs *)base_addr;
322
Ben Warren3bf5d832009-08-25 13:09:37 -0700323 dev->iobase = base_addr;
324 dev->priv = priv;
325 dev->init = cs8900_init;
326 dev->halt = cs8900_halt;
327 dev->send = cs8900_send;
328 dev->recv = cs8900_recv;
Hui.Tang9007f3c2009-11-05 09:58:44 +0800329
330 /* Load MAC address from EEPROM */
331 cs8900_get_enetaddr(dev);
332
Ben Warren3bf5d832009-08-25 13:09:37 -0700333 sprintf(dev->name, "%s-%hu", CS8900_DRIVERNAME, dev_num);
wdenk1fe2c702003-03-06 21:55:29 +0000334
Ben Warren3bf5d832009-08-25 13:09:37 -0700335 eth_register(dev);
wdenk6b58f332003-03-14 20:47:52 +0000336 return 0;
wdenk1fe2c702003-03-06 21:55:29 +0000337}