wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Cirrus Logic CS8900A Ethernet |
| 3 | * |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 4 | * (C) 2009 Ben Warren , biggerbadderben@gmail.com |
| 5 | * Converted to use CONFIG_NET_MULTI API |
| 6 | * |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 7 | * (C) 2003 Wolfgang Denk, wd@denx.de |
| 8 | * Extension to synchronize ethaddr environment variable |
| 9 | * against value in EEPROM |
| 10 | * |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | * (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 Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 44 | #include <asm/io.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | #include <net.h> |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 46 | #include <malloc.h> |
| 47 | #include "cs8900.h" |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 49 | #undef DEBUG |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | |
| 51 | /* packet page register access functions */ |
| 52 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_CS8900_BUS32 |
| 54 | |
| 55 | #define REG_WRITE(v, a) writel((v),(a)) |
| 56 | #define REG_READ(a) readl((a)) |
| 57 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | /* we don't need 16 bit initialisation on 32 bit bus */ |
Ben Warren | 3b6be93 | 2009-11-09 11:43:18 -0800 | [diff] [blame] | 59 | #define get_reg_init_bus(r,d) get_reg((r),(d)) |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 60 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 61 | #else |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 62 | |
| 63 | #define REG_WRITE(v, a) writew((v),(a)) |
| 64 | #define REG_READ(a) readw((a)) |
| 65 | |
| 66 | static u16 get_reg_init_bus(struct eth_device *dev, int regno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 67 | { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 68 | /* force 16 bit busmode */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 69 | struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv); |
| 70 | uint8_t volatile * const iob = (uint8_t volatile * const)dev->iobase; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 71 | |
Anatolij Gustschin | ab1e585 | 2011-11-19 13:12:14 +0000 | [diff] [blame] | 72 | readb(iob); |
| 73 | readb(iob + 1); |
| 74 | readb(iob); |
| 75 | readb(iob + 1); |
| 76 | readb(iob); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 77 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 78 | REG_WRITE(regno, &priv->regs->pptr); |
| 79 | return REG_READ(&priv->regs->pdata); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | } |
| 81 | #endif |
| 82 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 83 | static u16 get_reg(struct eth_device *dev, int regno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 84 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 85 | struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv); |
| 86 | REG_WRITE(regno, &priv->regs->pptr); |
| 87 | return REG_READ(&priv->regs->pdata); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 91 | static void put_reg(struct eth_device *dev, int regno, u16 val) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 93 | struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv); |
| 94 | REG_WRITE(regno, &priv->regs->pptr); |
| 95 | REG_WRITE(val, &priv->regs->pdata); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 98 | static void cs8900_reset(struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 100 | int tmo; |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 101 | u16 us; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 103 | /* reset NIC */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 104 | put_reg(dev, PP_SelfCTL, get_reg(dev, PP_SelfCTL) | PP_SelfCTL_Reset); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 106 | /* wait for 200ms */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 107 | udelay(200000); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 108 | /* Wait until the chip is reset */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 110 | 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)) |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 113 | /*NOP*/; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 116 | static void cs8900_reginit(struct eth_device *dev) |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 117 | { |
| 118 | /* receive only error free packets addressed to this card */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 119 | put_reg(dev, PP_RxCTL, |
| 120 | PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK); |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 121 | /* do not generate any interrupts on receive operations */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 122 | put_reg(dev, PP_RxCFG, 0); |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 123 | /* do not generate any interrupts on transmit operations */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 124 | put_reg(dev, PP_TxCFG, 0); |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 125 | /* do not generate any interrupts on buffer operations */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 126 | put_reg(dev, PP_BufCFG, 0); |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 127 | /* enable transmitter/receiver mode */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 128 | put_reg(dev, PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx); |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 131 | void cs8900_get_enetaddr(struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 133 | int i; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 134 | |
| 135 | /* verify chip id */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 136 | if (get_reg_init_bus(dev, PP_ChipID) != 0x630e) |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 137 | return; |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 138 | 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)) { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 142 | |
| 143 | /* Load the MAC from EEPROM */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 144 | for (i = 0; i < 3; i++) { |
| 145 | u32 Addr; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 146 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 147 | Addr = get_reg(dev, PP_IA + i * 2); |
| 148 | dev->enetaddr[i * 2] = Addr & 0xFF; |
| 149 | dev->enetaddr[i * 2 + 1] = Addr >> 8; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 150 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 151 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 154 | void cs8900_halt(struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 156 | /* disable transmitter/receiver mode */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 157 | put_reg(dev, PP_LineCTL, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 159 | /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 160 | get_reg_init_bus(dev, PP_ChipID); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 163 | static int cs8900_init(struct eth_device *dev, bd_t * bd) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 164 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 165 | uchar *enetaddr = dev->enetaddr; |
| 166 | u16 id; |
Mike Frysinger | ca76813 | 2009-02-11 19:06:09 -0500 | [diff] [blame] | 167 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 168 | /* verify chip id */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 169 | 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; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 174 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 175 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 176 | cs8900_reset (dev); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 177 | /* set the ethernet address */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 178 | 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)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 181 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 182 | cs8900_reginit(dev); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 183 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* Get a data block via Ethernet */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 187 | static int cs8900_recv(struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 188 | { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 189 | int i; |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 190 | u16 rxlen; |
| 191 | u16 *addr; |
| 192 | u16 status; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 193 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 194 | struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv); |
| 195 | |
| 196 | status = get_reg(dev, PP_RER); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 198 | if ((status & PP_RER_RxOK) == 0) |
| 199 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 201 | status = REG_READ(&priv->regs->rtdata); |
| 202 | rxlen = REG_READ(&priv->regs->rtdata); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 204 | if (rxlen > PKTSIZE_ALIGN + PKTALIGN) |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 205 | debug("packet too big!\n"); |
| 206 | for (addr = (u16 *) NetRxPackets[0], i = rxlen >> 1; i > 0; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 207 | i--) |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 208 | *addr++ = REG_READ(&priv->regs->rtdata); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 209 | if (rxlen & 1) |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 210 | *addr++ = REG_READ(&priv->regs->rtdata); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 211 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 212 | /* Pass the packet up to the protocol layers. */ |
| 213 | NetReceive (NetRxPackets[0], rxlen); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 214 | return rxlen; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* Send a data block via Ethernet. */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 218 | static int cs8900_send(struct eth_device *dev, |
| 219 | volatile void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 221 | volatile u16 *addr; |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 222 | int tmo; |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 223 | u16 s; |
| 224 | struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 225 | |
| 226 | retry: |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 227 | /* initiate a transmit sequence */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 228 | REG_WRITE(PP_TxCmd_TxStart_Full, &priv->regs->txcmd); |
| 229 | REG_WRITE(length, &priv->regs->txlen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 230 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 231 | /* Test to see if the chip has allocated memory for the packet */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 232 | if ((get_reg(dev, PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) { |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 233 | /* Oops... this should not happen! */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 234 | debug("cs: unable to send packet; retrying...\n"); |
| 235 | for (tmo = get_timer(0) + 5 * CONFIG_SYS_HZ; |
| 236 | get_timer(0) < tmo;) |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 237 | /*NOP*/; |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 238 | cs8900_reset(dev); |
| 239 | cs8900_reginit(dev); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 240 | goto retry; |
| 241 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 242 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 243 | /* Write the contents of the packet */ |
| 244 | /* assume even number of bytes */ |
| 245 | for (addr = packet; length > 0; length -= 2) |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 246 | REG_WRITE(*addr++, &priv->regs->rtdata); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 248 | /* wait for transfer to succeed */ |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 249 | tmo = get_timer(0) + 5 * CONFIG_SYS_HZ; |
| 250 | while ((s = get_reg(dev, PP_TER) & ~0x1F) == 0) { |
| 251 | if (get_timer(0) >= tmo) |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 252 | break; |
| 253 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 254 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 255 | /* nothing */ ; |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 256 | if((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) { |
| 257 | debug("\ntransmission error %#x\n", s); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 258 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 260 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 263 | static void cs8900_e2prom_ready(struct eth_device *dev) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 264 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 265 | while (get_reg(dev, PP_SelfSTAT) & SI_BUSY) |
Guennadi Liakhovetski | fddcc0c | 2008-04-03 13:36:18 +0200 | [diff] [blame] | 266 | ; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /***********************************************************/ |
| 270 | /* read a 16-bit word out of the EEPROM */ |
| 271 | /***********************************************************/ |
| 272 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 273 | int cs8900_e2prom_read(struct eth_device *dev, |
| 274 | u8 addr, u16 *value) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 275 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 276 | cs8900_e2prom_ready(dev); |
| 277 | put_reg(dev, PP_EECMD, EEPROM_READ_CMD | addr); |
| 278 | cs8900_e2prom_ready(dev); |
| 279 | *value = get_reg(dev, PP_EEData); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /***********************************************************/ |
| 286 | /* write a 16-bit word into the EEPROM */ |
| 287 | /***********************************************************/ |
| 288 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 289 | int cs8900_e2prom_write(struct eth_device *dev, u8 addr, u16 value) |
| 290 | { |
| 291 | cs8900_e2prom_ready(dev); |
| 292 | put_reg(dev, PP_EECMD, EEPROM_WRITE_EN); |
| 293 | cs8900_e2prom_ready(dev); |
| 294 | put_reg(dev, PP_EEData, value); |
| 295 | put_reg(dev, PP_EECMD, EEPROM_WRITE_CMD | addr); |
| 296 | cs8900_e2prom_ready(dev); |
| 297 | put_reg(dev, PP_EECMD, EEPROM_WRITE_DIS); |
| 298 | cs8900_e2prom_ready(dev); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | int cs8900_initialize(u8 dev_num, int base_addr) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 304 | { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 305 | struct eth_device *dev; |
| 306 | struct cs8900_priv *priv; |
| 307 | |
| 308 | dev = malloc(sizeof(*dev)); |
| 309 | if (!dev) { |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | memset(dev, 0, sizeof(*dev)); |
| 313 | |
| 314 | priv = malloc(sizeof(*priv)); |
| 315 | if (!priv) { |
Matthias Kaehlcke | 034c161 | 2010-01-21 22:16:34 +0100 | [diff] [blame] | 316 | free(dev); |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | memset(priv, 0, sizeof(*priv)); |
| 320 | priv->regs = (struct cs8900_regs *)base_addr; |
| 321 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 322 | dev->iobase = base_addr; |
| 323 | dev->priv = priv; |
| 324 | dev->init = cs8900_init; |
| 325 | dev->halt = cs8900_halt; |
| 326 | dev->send = cs8900_send; |
| 327 | dev->recv = cs8900_recv; |
Hui.Tang | 9007f3c | 2009-11-05 09:58:44 +0800 | [diff] [blame] | 328 | |
| 329 | /* Load MAC address from EEPROM */ |
| 330 | cs8900_get_enetaddr(dev); |
| 331 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 332 | sprintf(dev->name, "%s-%hu", CS8900_DRIVERNAME, dev_num); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 333 | |
Ben Warren | 3bf5d83 | 2009-08-25 13:09:37 -0700 | [diff] [blame] | 334 | eth_register(dev); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 335 | return 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 336 | } |