blob: 9331cdf9387cebd3209e076f5c37b04bc80cb83f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
3 * Cirrus Logic CS8900A Ethernet
4 *
Ben Warrenb1c0eaa2009-08-25 13:09:37 -07005 * (C) 2009 Ben Warren , biggerbadderben@gmail.com
6 * Converted to use CONFIG_NET_MULTI API
7 *
wdenk6069ff22003-02-28 00:49:47 +00008 * (C) 2003 Wolfgang Denk, wd@denx.de
9 * Extension to synchronize ethaddr environment variable
10 * against value in EEPROM
11 *
wdenkc6097192002-11-03 00:24:07 +000012 * (C) Copyright 2002
13 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Marius Groeger <mgroeger@sysgo.de>
15 *
16 * Copyright (C) 1999 Ben Williamson <benw@pobox.com>
17 *
wdenkc6097192002-11-03 00:24:07 +000018 * This program is loaded into SRAM in bootstrap mode, where it waits
19 * for commands on UART1 to read and write memory, jump to code etc.
20 * A design goal for this program is to be entirely independent of the
21 * target board. Anything with a CL-PS7111 or EP7211 should be able to run
22 * this code in bootstrap mode. All the board specifics can be handled on
23 * the host.
wdenkc6097192002-11-03 00:24:07 +000024 */
25
26#include <common.h>
27#include <command.h>
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070028#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000029#include <net.h>
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070030#include <malloc.h>
31#include "cs8900.h"
wdenkc6097192002-11-03 00:24:07 +000032
wdenka2663ea2003-12-07 18:32:37 +000033#undef DEBUG
wdenkc6097192002-11-03 00:24:07 +000034
35/* packet page register access functions */
36
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070037#ifdef CONFIG_CS8900_BUS32
38
39#define REG_WRITE(v, a) writel((v),(a))
40#define REG_READ(a) readl((a))
41
wdenkc6097192002-11-03 00:24:07 +000042/* we don't need 16 bit initialisation on 32 bit bus */
Ben Warren830c7b62009-11-09 11:43:18 -080043#define get_reg_init_bus(r,d) get_reg((r),(d))
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070044
wdenkc6097192002-11-03 00:24:07 +000045#else
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070046
47#define REG_WRITE(v, a) writew((v),(a))
48#define REG_READ(a) readw((a))
49
50static u16 get_reg_init_bus(struct eth_device *dev, int regno)
wdenkc6097192002-11-03 00:24:07 +000051{
wdenk6069ff22003-02-28 00:49:47 +000052 /* force 16 bit busmode */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070053 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
54 uint8_t volatile * const iob = (uint8_t volatile * const)dev->iobase;
wdenkc6097192002-11-03 00:24:07 +000055
Anatolij Gustschinda227352011-11-19 13:12:14 +000056 readb(iob);
57 readb(iob + 1);
58 readb(iob);
59 readb(iob + 1);
60 readb(iob);
wdenk6069ff22003-02-28 00:49:47 +000061
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070062 REG_WRITE(regno, &priv->regs->pptr);
63 return REG_READ(&priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000064}
65#endif
66
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070067static u16 get_reg(struct eth_device *dev, int regno)
wdenkc6097192002-11-03 00:24:07 +000068{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070069 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
70 REG_WRITE(regno, &priv->regs->pptr);
71 return REG_READ(&priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000072}
73
74
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070075static void put_reg(struct eth_device *dev, int regno, u16 val)
wdenkc6097192002-11-03 00:24:07 +000076{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070077 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
78 REG_WRITE(regno, &priv->regs->pptr);
79 REG_WRITE(val, &priv->regs->pdata);
wdenkc6097192002-11-03 00:24:07 +000080}
81
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070082static void cs8900_reset(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +000083{
wdenk6069ff22003-02-28 00:49:47 +000084 int tmo;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070085 u16 us;
wdenkc6097192002-11-03 00:24:07 +000086
wdenk6069ff22003-02-28 00:49:47 +000087 /* reset NIC */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070088 put_reg(dev, PP_SelfCTL, get_reg(dev, PP_SelfCTL) | PP_SelfCTL_Reset);
wdenkc6097192002-11-03 00:24:07 +000089
wdenk6069ff22003-02-28 00:49:47 +000090 /* wait for 200ms */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070091 udelay(200000);
wdenk6069ff22003-02-28 00:49:47 +000092 /* Wait until the chip is reset */
wdenkc6097192002-11-03 00:24:07 +000093
Ben Warrenb1c0eaa2009-08-25 13:09:37 -070094 tmo = get_timer(0) + 1 * CONFIG_SYS_HZ;
95 while ((((us = get_reg_init_bus(dev, PP_SelfSTAT)) &
96 PP_SelfSTAT_InitD) == 0) && tmo < get_timer(0))
wdenk6069ff22003-02-28 00:49:47 +000097 /*NOP*/;
wdenkc6097192002-11-03 00:24:07 +000098}
99
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700100static void cs8900_reginit(struct eth_device *dev)
wdenka2663ea2003-12-07 18:32:37 +0000101{
102 /* receive only error free packets addressed to this card */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700103 put_reg(dev, PP_RxCTL,
104 PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
wdenka2663ea2003-12-07 18:32:37 +0000105 /* do not generate any interrupts on receive operations */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700106 put_reg(dev, PP_RxCFG, 0);
wdenka2663ea2003-12-07 18:32:37 +0000107 /* do not generate any interrupts on transmit operations */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700108 put_reg(dev, PP_TxCFG, 0);
wdenka2663ea2003-12-07 18:32:37 +0000109 /* do not generate any interrupts on buffer operations */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700110 put_reg(dev, PP_BufCFG, 0);
wdenka2663ea2003-12-07 18:32:37 +0000111 /* enable transmitter/receiver mode */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700112 put_reg(dev, PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
wdenka2663ea2003-12-07 18:32:37 +0000113}
114
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700115void cs8900_get_enetaddr(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000116{
wdenk6069ff22003-02-28 00:49:47 +0000117 int i;
wdenk6069ff22003-02-28 00:49:47 +0000118
119 /* verify chip id */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700120 if (get_reg_init_bus(dev, PP_ChipID) != 0x630e)
wdenk6069ff22003-02-28 00:49:47 +0000121 return;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700122 cs8900_reset(dev);
123 if ((get_reg(dev, PP_SelfSTAT) &
124 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
125 (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
wdenk6069ff22003-02-28 00:49:47 +0000126
127 /* Load the MAC from EEPROM */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700128 for (i = 0; i < 3; i++) {
129 u32 Addr;
wdenk6069ff22003-02-28 00:49:47 +0000130
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700131 Addr = get_reg(dev, PP_IA + i * 2);
132 dev->enetaddr[i * 2] = Addr & 0xFF;
133 dev->enetaddr[i * 2 + 1] = Addr >> 8;
wdenk6069ff22003-02-28 00:49:47 +0000134 }
wdenk6069ff22003-02-28 00:49:47 +0000135 }
wdenkc6097192002-11-03 00:24:07 +0000136}
137
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700138void cs8900_halt(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000139{
wdenk6069ff22003-02-28 00:49:47 +0000140 /* disable transmitter/receiver mode */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700141 put_reg(dev, PP_LineCTL, 0);
wdenkc6097192002-11-03 00:24:07 +0000142
wdenk6069ff22003-02-28 00:49:47 +0000143 /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700144 get_reg_init_bus(dev, PP_ChipID);
wdenkc6097192002-11-03 00:24:07 +0000145}
146
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700147static int cs8900_init(struct eth_device *dev, bd_t * bd)
wdenkc6097192002-11-03 00:24:07 +0000148{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700149 uchar *enetaddr = dev->enetaddr;
150 u16 id;
Mike Frysinger0a5238c2009-02-11 19:06:09 -0500151
wdenk6069ff22003-02-28 00:49:47 +0000152 /* verify chip id */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700153 id = get_reg_init_bus(dev, PP_ChipID);
154 if (id != 0x630e) {
155 printf ("CS8900 Ethernet chip not found: "
156 "ID=0x%04x instead 0x%04x\n", id, 0x630e);
157 return 1;
wdenk6069ff22003-02-28 00:49:47 +0000158 }
159
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700160 cs8900_reset (dev);
wdenk6069ff22003-02-28 00:49:47 +0000161 /* set the ethernet address */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700162 put_reg(dev, PP_IA + 0, enetaddr[0] | (enetaddr[1] << 8));
163 put_reg(dev, PP_IA + 2, enetaddr[2] | (enetaddr[3] << 8));
164 put_reg(dev, PP_IA + 4, enetaddr[4] | (enetaddr[5] << 8));
wdenk6069ff22003-02-28 00:49:47 +0000165
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700166 cs8900_reginit(dev);
wdenkc6097192002-11-03 00:24:07 +0000167 return 0;
wdenkc6097192002-11-03 00:24:07 +0000168}
169
170/* Get a data block via Ethernet */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700171static int cs8900_recv(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000172{
wdenk6069ff22003-02-28 00:49:47 +0000173 int i;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700174 u16 rxlen;
175 u16 *addr;
176 u16 status;
wdenkc6097192002-11-03 00:24:07 +0000177
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700178 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
179
180 status = get_reg(dev, PP_RER);
wdenkc6097192002-11-03 00:24:07 +0000181
wdenk6069ff22003-02-28 00:49:47 +0000182 if ((status & PP_RER_RxOK) == 0)
183 return 0;
wdenkc6097192002-11-03 00:24:07 +0000184
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700185 status = REG_READ(&priv->regs->rtdata);
186 rxlen = REG_READ(&priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000187
wdenk6069ff22003-02-28 00:49:47 +0000188 if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700189 debug("packet too big!\n");
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500190 for (addr = (u16 *)net_rx_packets[0], i = rxlen >> 1; i > 0; i--)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700191 *addr++ = REG_READ(&priv->regs->rtdata);
wdenk6069ff22003-02-28 00:49:47 +0000192 if (rxlen & 1)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700193 *addr++ = REG_READ(&priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000194
wdenk6069ff22003-02-28 00:49:47 +0000195 /* Pass the packet up to the protocol layers. */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500196 net_process_received_packet(net_rx_packets[0], rxlen);
wdenk6069ff22003-02-28 00:49:47 +0000197 return rxlen;
wdenkc6097192002-11-03 00:24:07 +0000198}
199
200/* Send a data block via Ethernet. */
Joe Hershberger9d295172012-05-21 14:45:21 +0000201static int cs8900_send(struct eth_device *dev, void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000202{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700203 volatile u16 *addr;
wdenk6069ff22003-02-28 00:49:47 +0000204 int tmo;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700205 u16 s;
206 struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
wdenkc6097192002-11-03 00:24:07 +0000207
208retry:
wdenk6069ff22003-02-28 00:49:47 +0000209 /* initiate a transmit sequence */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700210 REG_WRITE(PP_TxCmd_TxStart_Full, &priv->regs->txcmd);
211 REG_WRITE(length, &priv->regs->txlen);
wdenkc6097192002-11-03 00:24:07 +0000212
wdenk6069ff22003-02-28 00:49:47 +0000213 /* Test to see if the chip has allocated memory for the packet */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700214 if ((get_reg(dev, PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
wdenk6069ff22003-02-28 00:49:47 +0000215 /* Oops... this should not happen! */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700216 debug("cs: unable to send packet; retrying...\n");
217 for (tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
218 get_timer(0) < tmo;)
wdenk6069ff22003-02-28 00:49:47 +0000219 /*NOP*/;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700220 cs8900_reset(dev);
221 cs8900_reginit(dev);
wdenk6069ff22003-02-28 00:49:47 +0000222 goto retry;
223 }
wdenkc6097192002-11-03 00:24:07 +0000224
wdenk6069ff22003-02-28 00:49:47 +0000225 /* Write the contents of the packet */
226 /* assume even number of bytes */
227 for (addr = packet; length > 0; length -= 2)
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700228 REG_WRITE(*addr++, &priv->regs->rtdata);
wdenkc6097192002-11-03 00:24:07 +0000229
wdenk6069ff22003-02-28 00:49:47 +0000230 /* wait for transfer to succeed */
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700231 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
232 while ((s = get_reg(dev, PP_TER) & ~0x1F) == 0) {
233 if (get_timer(0) >= tmo)
wdenk6069ff22003-02-28 00:49:47 +0000234 break;
235 }
wdenkc6097192002-11-03 00:24:07 +0000236
wdenk6069ff22003-02-28 00:49:47 +0000237 /* nothing */ ;
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700238 if((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
239 debug("\ntransmission error %#x\n", s);
wdenk6069ff22003-02-28 00:49:47 +0000240 }
wdenkc6097192002-11-03 00:24:07 +0000241
wdenk6069ff22003-02-28 00:49:47 +0000242 return 0;
wdenkc6097192002-11-03 00:24:07 +0000243}
244
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700245static void cs8900_e2prom_ready(struct eth_device *dev)
wdenk1cb8e982003-03-06 21:55:29 +0000246{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700247 while (get_reg(dev, PP_SelfSTAT) & SI_BUSY)
Guennadi Liakhovetski13e0b8f2008-04-03 13:36:18 +0200248 ;
wdenk1cb8e982003-03-06 21:55:29 +0000249}
250
251/***********************************************************/
252/* read a 16-bit word out of the EEPROM */
253/***********************************************************/
254
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700255int cs8900_e2prom_read(struct eth_device *dev,
256 u8 addr, u16 *value)
wdenk1cb8e982003-03-06 21:55:29 +0000257{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700258 cs8900_e2prom_ready(dev);
259 put_reg(dev, PP_EECMD, EEPROM_READ_CMD | addr);
260 cs8900_e2prom_ready(dev);
261 *value = get_reg(dev, PP_EEData);
wdenk1cb8e982003-03-06 21:55:29 +0000262
263 return 0;
264}
265
266
267/***********************************************************/
268/* write a 16-bit word into the EEPROM */
269/***********************************************************/
270
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700271int cs8900_e2prom_write(struct eth_device *dev, u8 addr, u16 value)
wdenk1cb8e982003-03-06 21:55:29 +0000272{
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700273 cs8900_e2prom_ready(dev);
274 put_reg(dev, PP_EECMD, EEPROM_WRITE_EN);
275 cs8900_e2prom_ready(dev);
276 put_reg(dev, PP_EEData, value);
277 put_reg(dev, PP_EECMD, EEPROM_WRITE_CMD | addr);
278 cs8900_e2prom_ready(dev);
279 put_reg(dev, PP_EECMD, EEPROM_WRITE_DIS);
280 cs8900_e2prom_ready(dev);
wdenk1cb8e982003-03-06 21:55:29 +0000281
wdenk06d01db2003-03-14 20:47:52 +0000282 return 0;
wdenk1cb8e982003-03-06 21:55:29 +0000283}
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700284
285int cs8900_initialize(u8 dev_num, int base_addr)
286{
287 struct eth_device *dev;
288 struct cs8900_priv *priv;
289
290 dev = malloc(sizeof(*dev));
291 if (!dev) {
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700292 return 0;
293 }
294 memset(dev, 0, sizeof(*dev));
295
296 priv = malloc(sizeof(*priv));
297 if (!priv) {
Matthias Kaehlcke07c96602010-01-21 22:16:34 +0100298 free(dev);
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700299 return 0;
300 }
301 memset(priv, 0, sizeof(*priv));
302 priv->regs = (struct cs8900_regs *)base_addr;
303
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700304 dev->iobase = base_addr;
305 dev->priv = priv;
306 dev->init = cs8900_init;
307 dev->halt = cs8900_halt;
308 dev->send = cs8900_send;
309 dev->recv = cs8900_recv;
Hui.Tang497ab0e2009-11-05 09:58:44 +0800310
311 /* Load MAC address from EEPROM */
312 cs8900_get_enetaddr(dev);
313
Ben Warrenb1c0eaa2009-08-25 13:09:37 -0700314 sprintf(dev->name, "%s-%hu", CS8900_DRIVERNAME, dev_num);
315
316 eth_register(dev);
317 return 0;
318}