wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Realtek 8019AS Ethernet |
| 3 | * (C) Copyright 2002-2003 |
| 4 | * Xue Ligong(lgxue@hotmail.com),Wang Kehao, ESLAB, whut.edu.cn |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * This code works in 8bit mode. |
| 27 | * If you need to work in 16bit mode, PLS change it! |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <command.h> |
| 32 | #include "rtl8019.h" |
| 33 | #include <net.h> |
| 34 | |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 35 | /* packet page register access functions */ |
| 36 | |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 37 | static unsigned char get_reg (unsigned int regno) |
| 38 | { |
| 39 | return (*(unsigned char *) regno); |
| 40 | } |
| 41 | |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 42 | static void put_reg (unsigned int regno, unsigned char val) |
| 43 | { |
| 44 | *(volatile unsigned char *) regno = val; |
| 45 | } |
| 46 | |
| 47 | static void eth_reset (void) |
| 48 | { |
| 49 | unsigned char ucTemp; |
| 50 | |
| 51 | /* reset NIC */ |
| 52 | ucTemp = get_reg (RTL8019_RESET); |
| 53 | put_reg (RTL8019_RESET, ucTemp); |
| 54 | put_reg (RTL8019_INTERRUPTSTATUS, 0xff); |
| 55 | udelay (2000); /* wait for 2ms */ |
| 56 | } |
| 57 | |
| 58 | void rtl8019_get_enetaddr (uchar * addr) |
| 59 | { |
| 60 | unsigned char i; |
| 61 | unsigned char temp; |
| 62 | |
| 63 | eth_reset (); |
| 64 | |
| 65 | put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD); |
| 66 | put_reg (RTL8019_DATACONFIGURATION, 0x48); |
| 67 | put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00); |
| 68 | put_reg (RTL8019_REMOTESTARTADDRESS1, 0x00); |
| 69 | put_reg (RTL8019_REMOTEBYTECOUNT0, 12); |
| 70 | put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00); |
| 71 | put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD); |
| 72 | printf ("MAC: "); |
| 73 | for (i = 0; i < 6; i++) { |
| 74 | temp = get_reg (RTL8019_DMA_DATA); |
| 75 | *addr++ = temp; |
| 76 | temp = get_reg (RTL8019_DMA_DATA); |
| 77 | printf ("%x:", temp); |
| 78 | } |
| 79 | |
| 80 | while ((!get_reg (RTL8019_INTERRUPTSTATUS) & 0x40)); |
| 81 | printf ("\b \n"); |
| 82 | put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00); |
| 83 | put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00); |
| 84 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0); |
| 85 | } |
| 86 | |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 87 | void eth_halt (void) |
| 88 | { |
| 89 | put_reg (RTL8019_COMMAND, 0x01); |
| 90 | } |
| 91 | |
| 92 | int eth_init (bd_t * bd) |
| 93 | { |
Mike Frysinger | b203965 | 2009-02-11 19:01:26 -0500 | [diff] [blame] | 94 | uchar enetaddr[6]; |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 95 | eth_reset (); |
| 96 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP); |
| 97 | put_reg (RTL8019_DATACONFIGURATION, 0x48); |
| 98 | put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00); |
| 99 | put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00); |
| 100 | put_reg (RTL8019_RECEIVECONFIGURATION, 0x00); /*00; */ |
| 101 | put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART); |
| 102 | put_reg (RTL8019_TRANSMITCONFIGURATION, 0x02); |
| 103 | put_reg (RTL8019_PAGESTART, RTL8019_PSTART); |
| 104 | put_reg (RTL8019_BOUNDARY, RTL8019_PSTART); |
| 105 | put_reg (RTL8019_PAGESTOP, RTL8019_PSTOP); |
| 106 | put_reg (RTL8019_INTERRUPTSTATUS, 0xff); |
| 107 | put_reg (RTL8019_INTERRUPTMASK, 0x11); /*b; */ |
| 108 | put_reg (RTL8019_COMMAND, RTL8019_PAGE1STOP); |
Mike Frysinger | b203965 | 2009-02-11 19:01:26 -0500 | [diff] [blame] | 109 | eth_getenv_enetaddr("ethaddr", enetaddr); |
| 110 | put_reg (RTL8019_PHYSICALADDRESS0, enetaddr[0]); |
| 111 | put_reg (RTL8019_PHYSICALADDRESS1, enetaddr[1]); |
| 112 | put_reg (RTL8019_PHYSICALADDRESS2, enetaddr[2]); |
| 113 | put_reg (RTL8019_PHYSICALADDRESS3, enetaddr[3]); |
| 114 | put_reg (RTL8019_PHYSICALADDRESS4, enetaddr[4]); |
| 115 | put_reg (RTL8019_PHYSICALADDRESS5, enetaddr[5]); |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 116 | put_reg (RTL8019_MULTIADDRESS0, 0x00); |
| 117 | put_reg (RTL8019_MULTIADDRESS1, 0x00); |
| 118 | put_reg (RTL8019_MULTIADDRESS2, 0x00); |
| 119 | put_reg (RTL8019_MULTIADDRESS3, 0x00); |
| 120 | put_reg (RTL8019_MULTIADDRESS4, 0x00); |
| 121 | put_reg (RTL8019_MULTIADDRESS5, 0x00); |
| 122 | put_reg (RTL8019_MULTIADDRESS6, 0x00); |
| 123 | put_reg (RTL8019_MULTIADDRESS7, 0x00); |
| 124 | put_reg (RTL8019_CURRENT, RTL8019_PSTART); |
| 125 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0); |
| 126 | put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0); /*58; */ |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
wdenk | 5d5317e | 2003-12-07 00:46:27 +0000 | [diff] [blame] | 131 | static unsigned char nic_to_pc (void) |
| 132 | { |
| 133 | unsigned char rec_head_status; |
| 134 | unsigned char next_packet_pointer; |
| 135 | unsigned char packet_length0; |
| 136 | unsigned char packet_length1; |
| 137 | unsigned short rxlen = 0; |
| 138 | unsigned int i = 4; |
| 139 | unsigned char current_point; |
| 140 | unsigned char *addr; |
| 141 | |
| 142 | /* |
| 143 | * The RTL8019's first 4B is packet status,page of next packet |
| 144 | * and packet length(2B).So we receive the fist 4B. |
| 145 | */ |
| 146 | put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY)); |
| 147 | put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00); |
| 148 | put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00); |
| 149 | put_reg (RTL8019_REMOTEBYTECOUNT0, 0x04); |
| 150 | |
| 151 | put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD); |
| 152 | |
| 153 | rec_head_status = get_reg (RTL8019_DMA_DATA); |
| 154 | next_packet_pointer = get_reg (RTL8019_DMA_DATA); |
| 155 | packet_length0 = get_reg (RTL8019_DMA_DATA); |
| 156 | packet_length1 = get_reg (RTL8019_DMA_DATA); |
| 157 | |
| 158 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0); |
| 159 | /*Packet length is in two 8bit registers */ |
| 160 | rxlen = packet_length1; |
| 161 | rxlen = (((rxlen << 8) & 0xff00) + packet_length0); |
| 162 | rxlen -= 4; |
| 163 | |
| 164 | if (rxlen > PKTSIZE_ALIGN + PKTALIGN) |
| 165 | printf ("packet too big!\n"); |
| 166 | |
| 167 | /*Receive the packet */ |
| 168 | put_reg (RTL8019_REMOTESTARTADDRESS0, 0x04); |
| 169 | put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY)); |
| 170 | |
| 171 | put_reg (RTL8019_REMOTEBYTECOUNT0, (rxlen & 0xff)); |
| 172 | put_reg (RTL8019_REMOTEBYTECOUNT1, ((rxlen >> 8) & 0xff)); |
| 173 | |
| 174 | |
| 175 | put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD); |
| 176 | |
| 177 | for (addr = (unsigned char *) NetRxPackets[0], i = rxlen; i > 0; i--) |
| 178 | *addr++ = get_reg (RTL8019_DMA_DATA); |
| 179 | /* Pass the packet up to the protocol layers. */ |
| 180 | NetReceive (NetRxPackets[0], rxlen); |
| 181 | |
| 182 | while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40); /* wait for the op. */ |
| 183 | |
| 184 | /* |
| 185 | * To test whether the packets are all received,get the |
| 186 | * location of current point |
| 187 | */ |
| 188 | put_reg (RTL8019_COMMAND, RTL8019_PAGE1); |
| 189 | current_point = get_reg (RTL8019_CURRENT); |
| 190 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0); |
| 191 | put_reg (RTL8019_BOUNDARY, next_packet_pointer); |
| 192 | return current_point; |
| 193 | } |
| 194 | |
| 195 | /* Get a data block via Ethernet */ |
| 196 | extern int eth_rx (void) |
| 197 | { |
| 198 | unsigned char temp, current_point; |
| 199 | |
| 200 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0); |
| 201 | |
| 202 | while (1) { |
| 203 | temp = get_reg (RTL8019_INTERRUPTSTATUS); |
| 204 | |
| 205 | if (temp & 0x90) { |
| 206 | /*overflow */ |
| 207 | put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP); |
| 208 | udelay (2000); |
| 209 | put_reg (RTL8019_REMOTEBYTECOUNT0, 0); |
| 210 | put_reg (RTL8019_REMOTEBYTECOUNT1, 0); |
| 211 | put_reg (RTL8019_TRANSMITCONFIGURATION, 2); |
| 212 | do { |
| 213 | current_point = nic_to_pc (); |
| 214 | } while (get_reg (RTL8019_BOUNDARY) != current_point); |
| 215 | |
| 216 | put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0); |
| 217 | } |
| 218 | |
| 219 | if (temp & 0x1) { |
| 220 | /*packet received */ |
| 221 | do { |
| 222 | put_reg (RTL8019_INTERRUPTSTATUS, 0x01); |
| 223 | current_point = nic_to_pc (); |
| 224 | } while (get_reg (RTL8019_BOUNDARY) != current_point); |
| 225 | } |
| 226 | |
| 227 | if (!(temp & 0x1)) |
| 228 | return 0; |
| 229 | /* done and exit. */ |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* Send a data block via Ethernet. */ |
| 234 | extern int eth_send (volatile void *packet, int length) |
| 235 | { |
| 236 | volatile unsigned char *p; |
| 237 | unsigned int pn; |
| 238 | |
| 239 | pn = length; |
| 240 | p = (volatile unsigned char *) packet; |
| 241 | |
| 242 | while (get_reg (RTL8019_COMMAND) == RTL8019_TRANSMIT); |
| 243 | |
| 244 | put_reg (RTL8019_REMOTESTARTADDRESS0, 0); |
| 245 | put_reg (RTL8019_REMOTESTARTADDRESS1, RTL8019_TPSTART); |
| 246 | put_reg (RTL8019_REMOTEBYTECOUNT0, (pn & 0xff)); |
| 247 | put_reg (RTL8019_REMOTEBYTECOUNT1, ((pn >> 8) & 0xff)); |
| 248 | |
| 249 | put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMAWR); |
| 250 | while (pn > 0) { |
| 251 | put_reg (RTL8019_DMA_DATA, *p++); |
| 252 | pn--; |
| 253 | } |
| 254 | |
| 255 | pn = length; |
| 256 | |
| 257 | while (pn < 60) { /*Padding */ |
| 258 | put_reg (RTL8019_DMA_DATA, 0); |
| 259 | pn++; |
| 260 | } |
| 261 | |
| 262 | while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40); |
| 263 | |
| 264 | put_reg (RTL8019_INTERRUPTSTATUS, 0x40); |
| 265 | put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART); |
| 266 | put_reg (RTL8019_TRANSMITBYTECOUNT0, (pn & 0xff)); |
| 267 | put_reg (RTL8019_TRANSMITBYTECOUNT1, ((pn >> 8 & 0xff))); |
| 268 | put_reg (RTL8019_COMMAND, RTL8019_TRANSMIT); |
| 269 | |
| 270 | return 0; |
| 271 | } |