blob: 6aaa0cf49f5453a5c13ef3cbb0835e2d92856bdc [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * Cirrus Logic CS8900A Ethernet
3 *
Ben Warrenb1c0eaa2009-08-25 13:09:37 -07004 * (C) 2009 Ben Warren , biggerbadderben@gmail.com
5 * Converted to use CONFIG_NET_MULTI API
6 *
wdenk6069ff22003-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 Warrenb1c0eaa2009-08-25 13:09:37 -070044#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000045#include <net.h>
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070046#include <malloc.h>
47#include "cs8900.h"
wdenkc6097192002-11-03 00:24:07 +000048
wdenka2663ea2003-12-07 18:32:37 +000049#undef DEBUG
wdenkc6097192002-11-03 00:24:07 +000050
51/* packet page register access functions */
52
Ben Warrenb1c0eaa2009-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 Warren830c7b62009-11-09 11:43:18 -080059#define get_reg_init_bus(r,d) get_reg((r),(d))
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070060
wdenkc6097192002-11-03 00:24:07 +000061#else
Ben Warrenb1c0eaa2009-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{
wdenk6069ff22003-02-28 00:49:47 +000068 /* force 16 bit busmode */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070069 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
70 uint8_t volatile * const iob = (uint8_t volatile * const)dev->iobase;
wdenkc6097192002-11-03 00:24:07 +000071
Anatolij Gustschinda227352011-11-19 13:12:14 +000072 readb(iob);
73 readb(iob + 1);
74 readb(iob);
75 readb(iob + 1);
76 readb(iob);
wdenk6069ff22003-02-28 00:49:47 +000077
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070078 REG_WRITE(regno, &priv->regs->pptr);
79 return REG_READ(&priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000080}
81#endif
82
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070083static u16 get_reg(struct eth_device *dev, int regno)
wdenkc6097192002-11-03 00:24:07 +000084{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070085 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
86 REG_WRITE(regno, &priv->regs->pptr);
87 return REG_READ(&priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000088}
89
90
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070091static void put_reg(struct eth_device *dev, int regno, u16 val)
wdenkc6097192002-11-03 00:24:07 +000092{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070093 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
94 REG_WRITE(regno, &priv->regs->pptr);
95 REG_WRITE(val, &priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000096}
97
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070098static void cs8900_reset(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +000099{
wdenk6069ff22003-02-28 00:49:47 +0000100 int tmo;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700101 u16 us;
wdenkc6097192002-11-03 00:24:07 +0000102
wdenk6069ff22003-02-28 00:49:47 +0000103 /* reset NIC */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700104 put_reg(dev, PP_SelfCTL, get_reg(dev, PP_SelfCTL) | PP_SelfCTL_Reset);
wdenkc6097192002-11-03 00:24:07 +0000105
wdenk6069ff22003-02-28 00:49:47 +0000106 /* wait for 200ms */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700107 udelay(200000);
wdenk6069ff22003-02-28 00:49:47 +0000108 /* Wait until the chip is reset */
wdenkc6097192002-11-03 00:24:07 +0000109
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700110 tmo = get_timer(0) + 1 * CONFIG_SYS_HZ;
111 while ((((us = get_reg_init_bus(dev, PP_SelfSTAT)) &
112 PP_SelfSTAT_InitD) == 0) && tmo < get_timer(0))
wdenk6069ff22003-02-28 00:49:47 +0000113 /*NOP*/;
wdenkc6097192002-11-03 00:24:07 +0000114}
115
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700116static void cs8900_reginit(struct eth_device *dev)
wdenka2663ea2003-12-07 18:32:37 +0000117{
118 /* receive only error free packets addressed to this card */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700119 put_reg(dev, PP_RxCTL,
120 PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
wdenka2663ea2003-12-07 18:32:37 +0000121 /* do not generate any interrupts on receive operations */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700122 put_reg(dev, PP_RxCFG, 0);
wdenka2663ea2003-12-07 18:32:37 +0000123 /* do not generate any interrupts on transmit operations */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700124 put_reg(dev, PP_TxCFG, 0);
wdenka2663ea2003-12-07 18:32:37 +0000125 /* do not generate any interrupts on buffer operations */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700126 put_reg(dev, PP_BufCFG, 0);
wdenka2663ea2003-12-07 18:32:37 +0000127 /* enable transmitter/receiver mode */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700128 put_reg(dev, PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
wdenka2663ea2003-12-07 18:32:37 +0000129}
130
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700131void cs8900_get_enetaddr(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000132{
wdenk6069ff22003-02-28 00:49:47 +0000133 int i;
wdenk6069ff22003-02-28 00:49:47 +0000134
135 /* verify chip id */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700136 if (get_reg_init_bus(dev, PP_ChipID) != 0x630e)
wdenk6069ff22003-02-28 00:49:47 +0000137 return;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700138 cs8900_reset(dev);
139 if ((get_reg(dev, PP_SelfSTAT) &
140 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
141 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
wdenk6069ff22003-02-28 00:49:47 +0000142
143 /* Load the MAC from EEPROM */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700144 for (i = 0; i < 3; i++) {
145 u32 Addr;
wdenk6069ff22003-02-28 00:49:47 +0000146
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700147 Addr = get_reg(dev, PP_IA + i * 2);
148 dev->enetaddr[i * 2] = Addr & 0xFF;
149 dev->enetaddr[i * 2 + 1] = Addr >> 8;
wdenk6069ff22003-02-28 00:49:47 +0000150 }
wdenk6069ff22003-02-28 00:49:47 +0000151 }
wdenkc6097192002-11-03 00:24:07 +0000152}
153
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700154void cs8900_halt(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000155{
wdenk6069ff22003-02-28 00:49:47 +0000156 /* disable transmitter/receiver mode */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700157 put_reg(dev, PP_LineCTL, 0);
wdenkc6097192002-11-03 00:24:07 +0000158
wdenk6069ff22003-02-28 00:49:47 +0000159 /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700160 get_reg_init_bus(dev, PP_ChipID);
wdenkc6097192002-11-03 00:24:07 +0000161}
162
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700163static int cs8900_init(struct eth_device *dev, bd_t * bd)
wdenkc6097192002-11-03 00:24:07 +0000164{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700165 uchar *enetaddr = dev->enetaddr;
166 u16 id;
Mike Frysinger0a5238c2009-02-11 19:06:09 -0500167
wdenk6069ff22003-02-28 00:49:47 +0000168 /* verify chip id */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700169 id = get_reg_init_bus(dev, PP_ChipID);
170 if (id != 0x630e) {
171 printf ("CS8900 Ethernet chip not found: "
172 "ID=0x%04x instead 0x%04x\n", id, 0x630e);
173 return 1;
wdenk6069ff22003-02-28 00:49:47 +0000174 }
175
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700176 cs8900_reset (dev);
wdenk6069ff22003-02-28 00:49:47 +0000177 /* set the ethernet address */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700178 put_reg(dev, PP_IA + 0, enetaddr[0] | (enetaddr[1] << 8));
179 put_reg(dev, PP_IA + 2, enetaddr[2] | (enetaddr[3] << 8));
180 put_reg(dev, PP_IA + 4, enetaddr[4] | (enetaddr[5] << 8));
wdenk6069ff22003-02-28 00:49:47 +0000181
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700182 cs8900_reginit(dev);
wdenkc6097192002-11-03 00:24:07 +0000183 return 0;
wdenkc6097192002-11-03 00:24:07 +0000184}
185
186/* Get a data block via Ethernet */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700187static int cs8900_recv(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000188{
wdenk6069ff22003-02-28 00:49:47 +0000189 int i;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700190 u16 rxlen;
191 u16 *addr;
192 u16 status;
wdenkc6097192002-11-03 00:24:07 +0000193
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700194 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
195
196 status = get_reg(dev, PP_RER);
wdenkc6097192002-11-03 00:24:07 +0000197
wdenk6069ff22003-02-28 00:49:47 +0000198 if ((status & PP_RER_RxOK) == 0)
199 return 0;
wdenkc6097192002-11-03 00:24:07 +0000200
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700201 status = REG_READ(&priv->regs->rtdata);
202 rxlen = REG_READ(&priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000203
wdenk6069ff22003-02-28 00:49:47 +0000204 if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700205 debug("packet too big!\n");
206 for (addr = (u16 *) NetRxPackets[0], i = rxlen >> 1; i > 0;
wdenk6069ff22003-02-28 00:49:47 +0000207 i--)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700208 *addr++ = REG_READ(&priv->regs->rtdata);
wdenk6069ff22003-02-28 00:49:47 +0000209 if (rxlen & 1)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700210 *addr++ = REG_READ(&priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000211
wdenk6069ff22003-02-28 00:49:47 +0000212 /* Pass the packet up to the protocol layers. */
213 NetReceive (NetRxPackets[0], rxlen);
wdenk6069ff22003-02-28 00:49:47 +0000214 return rxlen;
wdenkc6097192002-11-03 00:24:07 +0000215}
216
217/* Send a data block via Ethernet. */
Joe Hershberger9d295172012-05-21 14:45:21 +0000218static int cs8900_send(struct eth_device *dev, void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000219{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700220 volatile u16 *addr;
wdenk6069ff22003-02-28 00:49:47 +0000221 int tmo;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700222 u16 s;
223 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
wdenkc6097192002-11-03 00:24:07 +0000224
225retry:
wdenk6069ff22003-02-28 00:49:47 +0000226 /* initiate a transmit sequence */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700227 REG_WRITE(PP_TxCmd_TxStart_Full, &priv->regs->txcmd);
228 REG_WRITE(length, &priv->regs->txlen);
wdenkc6097192002-11-03 00:24:07 +0000229
wdenk6069ff22003-02-28 00:49:47 +0000230 /* Test to see if the chip has allocated memory for the packet */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700231 if ((get_reg(dev, PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
wdenk6069ff22003-02-28 00:49:47 +0000232 /* Oops... this should not happen! */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700233 debug("cs: unable to send packet; retrying...\n");
234 for (tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
235 get_timer(0) < tmo;)
wdenk6069ff22003-02-28 00:49:47 +0000236 /*NOP*/;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700237 cs8900_reset(dev);
238 cs8900_reginit(dev);
wdenk6069ff22003-02-28 00:49:47 +0000239 goto retry;
240 }
wdenkc6097192002-11-03 00:24:07 +0000241
wdenk6069ff22003-02-28 00:49:47 +0000242 /* Write the contents of the packet */
243 /* assume even number of bytes */
244 for (addr = packet; length > 0; length -= 2)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700245 REG_WRITE(*addr++, &priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000246
wdenk6069ff22003-02-28 00:49:47 +0000247 /* wait for transfer to succeed */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700248 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
249 while ((s = get_reg(dev, PP_TER) & ~0x1F) == 0) {
250 if (get_timer(0) >= tmo)
wdenk6069ff22003-02-28 00:49:47 +0000251 break;
252 }
wdenkc6097192002-11-03 00:24:07 +0000253
wdenk6069ff22003-02-28 00:49:47 +0000254 /* nothing */ ;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700255 if((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
256 debug("\ntransmission error %#x\n", s);
wdenk6069ff22003-02-28 00:49:47 +0000257 }
wdenkc6097192002-11-03 00:24:07 +0000258
wdenk6069ff22003-02-28 00:49:47 +0000259 return 0;
wdenkc6097192002-11-03 00:24:07 +0000260}
261
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700262static void cs8900_e2prom_ready(struct eth_device *dev)
wdenk1cb8e982003-03-06 21:55:29 +0000263{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700264 while (get_reg(dev, PP_SelfSTAT) & SI_BUSY)
Guennadi Liakhovetski13e0b8f2008-04-03 13:36:18 +0200265 ;
wdenk1cb8e982003-03-06 21:55:29 +0000266}
267
268/***********************************************************/
269/* read a 16-bit word out of the EEPROM */
270/***********************************************************/
271
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700272int cs8900_e2prom_read(struct eth_device *dev,
273 u8 addr, u16 *value)
wdenk1cb8e982003-03-06 21:55:29 +0000274{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700275 cs8900_e2prom_ready(dev);
276 put_reg(dev, PP_EECMD, EEPROM_READ_CMD | addr);
277 cs8900_e2prom_ready(dev);
278 *value = get_reg(dev, PP_EEData);
wdenk1cb8e982003-03-06 21:55:29 +0000279
280 return 0;
281}
282
283
284/***********************************************************/
285/* write a 16-bit word into the EEPROM */
286/***********************************************************/
287
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700288int cs8900_e2prom_write(struct eth_device *dev, u8 addr, u16 value)
wdenk1cb8e982003-03-06 21:55:29 +0000289{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700290 cs8900_e2prom_ready(dev);
291 put_reg(dev, PP_EECMD, EEPROM_WRITE_EN);
292 cs8900_e2prom_ready(dev);
293 put_reg(dev, PP_EEData, value);
294 put_reg(dev, PP_EECMD, EEPROM_WRITE_CMD | addr);
295 cs8900_e2prom_ready(dev);
296 put_reg(dev, PP_EECMD, EEPROM_WRITE_DIS);
297 cs8900_e2prom_ready(dev);
wdenk1cb8e982003-03-06 21:55:29 +0000298
wdenk06d01db2003-03-14 20:47:52 +0000299 return 0;
wdenk1cb8e982003-03-06 21:55:29 +0000300}
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700301
302int cs8900_initialize(u8 dev_num, int base_addr)
303{
304 struct eth_device *dev;
305 struct cs8900_priv *priv;
306
307 dev = malloc(sizeof(*dev));
308 if (!dev) {
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700309 return 0;
310 }
311 memset(dev, 0, sizeof(*dev));
312
313 priv = malloc(sizeof(*priv));
314 if (!priv) {
Matthias Kaehlcke07c96602010-01-21 22:16:34 +0100315 free(dev);
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700316 return 0;
317 }
318 memset(priv, 0, sizeof(*priv));
319 priv->regs = (struct cs8900_regs *)base_addr;
320
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700321 dev->iobase = base_addr;
322 dev->priv = priv;
323 dev->init = cs8900_init;
324 dev->halt = cs8900_halt;
325 dev->send = cs8900_send;
326 dev->recv = cs8900_recv;
Hui.Tang497ab0e2009-11-05 09:58:44 +0800327
328 /* Load MAC address from EEPROM */
329 cs8900_get_enetaddr(dev);
330
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700331 sprintf(dev->name, "%s-%hu", CS8900_DRIVERNAME, dev_num);
332
333 eth_register(dev);
334 return 0;
335}