wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ks8695eth.c -- KS8695 ethernet driver |
| 3 | * |
| 4 | * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | /****************************************************************************/ |
| 22 | |
| 23 | #include <common.h> |
Wolfgang Denk | 7c60908 | 2006-03-12 22:14:20 +0100 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_DRIVER_KS8695ETH |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 26 | #include <malloc.h> |
| 27 | #include <net.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/arch/platform.h> |
| 30 | |
| 31 | /****************************************************************************/ |
| 32 | |
| 33 | /* |
| 34 | * Hardware register access to the KS8695 LAN ethernet port |
| 35 | * (well, it is the 4 port switch really). |
| 36 | */ |
| 37 | #define ks8695_read(a) *((volatile unsigned long *) (KS8695_IO_BASE + (a))) |
| 38 | #define ks8695_write(a,v) *((volatile unsigned long *) (KS8695_IO_BASE + (a))) = (v) |
| 39 | |
| 40 | /****************************************************************************/ |
| 41 | |
| 42 | /* |
| 43 | * Define the descriptor in-memory data structures. |
| 44 | */ |
| 45 | struct ks8695_txdesc { |
| 46 | uint32_t owner; |
| 47 | uint32_t ctrl; |
| 48 | uint32_t addr; |
| 49 | uint32_t next; |
| 50 | }; |
| 51 | |
| 52 | struct ks8695_rxdesc { |
| 53 | uint32_t status; |
| 54 | uint32_t ctrl; |
| 55 | uint32_t addr; |
| 56 | uint32_t next; |
| 57 | }; |
| 58 | |
| 59 | /****************************************************************************/ |
| 60 | |
| 61 | /* |
| 62 | * Allocate local data structures to use for receiving and sending |
| 63 | * packets. Just to keep it all nice and simple. |
| 64 | */ |
| 65 | |
| 66 | #define TXDESCS 4 |
| 67 | #define RXDESCS 4 |
| 68 | #define BUFSIZE 2048 |
| 69 | |
| 70 | volatile struct ks8695_txdesc ks8695_tx[TXDESCS] __attribute__((aligned(256))); |
| 71 | volatile struct ks8695_rxdesc ks8695_rx[RXDESCS] __attribute__((aligned(256))); |
| 72 | volatile uint8_t ks8695_bufs[BUFSIZE*(TXDESCS+RXDESCS)] __attribute__((aligned(2048)));; |
| 73 | |
| 74 | /****************************************************************************/ |
| 75 | |
| 76 | /* |
| 77 | * Ideally we want to use the MAC address stored in flash. |
| 78 | * But we do some sanity checks in case they are not present |
| 79 | * first. |
| 80 | */ |
| 81 | unsigned char eth_mac[] = { |
| 82 | 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00 |
| 83 | }; |
| 84 | |
| 85 | void ks8695_getmac(void) |
| 86 | { |
| 87 | unsigned char *fp; |
| 88 | int i; |
| 89 | |
| 90 | /* Check if flash MAC is valid */ |
| 91 | fp = (unsigned char *) 0x0201c000; |
| 92 | for (i = 0; (i < 6); i++) { |
| 93 | if ((fp[i] != 0) && (fp[i] != 0xff)) |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | /* If we found a valid looking MAC address then use it */ |
| 98 | if (i < 6) |
| 99 | memcpy(ð_mac[0], fp, 6); |
| 100 | } |
| 101 | |
| 102 | /****************************************************************************/ |
| 103 | |
| 104 | void eth_reset(bd_t *bd) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__); |
| 109 | |
| 110 | /* Reset the ethernet engines first */ |
| 111 | ks8695_write(KS8695_LAN_DMA_TX, 0x80000000); |
| 112 | ks8695_write(KS8695_LAN_DMA_RX, 0x80000000); |
| 113 | |
| 114 | ks8695_getmac(); |
| 115 | |
| 116 | /* Set MAC address */ |
| 117 | ks8695_write(KS8695_LAN_MAC_LOW, (eth_mac[5] | (eth_mac[4] << 8) | |
| 118 | (eth_mac[3] << 16) | (eth_mac[2] << 24))); |
| 119 | ks8695_write(KS8695_LAN_MAC_HIGH, (eth_mac[1] | (eth_mac[0] << 8))); |
| 120 | |
| 121 | /* Turn the 4 port switch on */ |
| 122 | i = ks8695_read(KS8695_SWITCH_CTRL0); |
| 123 | ks8695_write(KS8695_SWITCH_CTRL0, (i | 0x1)); |
| 124 | /* ks8695_write(KS8695_WAN_CONTROL, 0x3f000066); */ |
| 125 | |
| 126 | /* Initialize descriptor rings */ |
| 127 | for (i = 0; (i < TXDESCS); i++) { |
| 128 | ks8695_tx[i].owner = 0; |
| 129 | ks8695_tx[i].ctrl = 0; |
| 130 | ks8695_tx[i].addr = (uint32_t) &ks8695_bufs[i*BUFSIZE]; |
| 131 | ks8695_tx[i].next = (uint32_t) &ks8695_tx[i+1]; |
| 132 | } |
| 133 | ks8695_tx[TXDESCS-1].ctrl = 0x02000000; |
| 134 | ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0]; |
| 135 | |
| 136 | for (i = 0; (i < RXDESCS); i++) { |
| 137 | ks8695_rx[i].status = 0x80000000; |
| 138 | ks8695_rx[i].ctrl = BUFSIZE - 4; |
| 139 | ks8695_rx[i].addr = (uint32_t) &ks8695_bufs[(i+TXDESCS)*BUFSIZE]; |
| 140 | ks8695_rx[i].next = (uint32_t) &ks8695_rx[i+1]; |
| 141 | } |
| 142 | ks8695_rx[RXDESCS-1].ctrl |= 0x00080000; |
| 143 | ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0]; |
| 144 | |
| 145 | /* The KS8695 is pretty slow reseting the ethernets... */ |
| 146 | udelay(2000000); |
| 147 | |
| 148 | /* Enable the ethernet engine */ |
| 149 | ks8695_write(KS8695_LAN_TX_LIST, (uint32_t) &ks8695_tx[0]); |
| 150 | ks8695_write(KS8695_LAN_RX_LIST, (uint32_t) &ks8695_rx[0]); |
| 151 | ks8695_write(KS8695_LAN_DMA_TX, 0x3); |
| 152 | ks8695_write(KS8695_LAN_DMA_RX, 0x71); |
| 153 | ks8695_write(KS8695_LAN_DMA_RX_START, 0x1); |
| 154 | |
| 155 | printf("KS8695 ETHERNET: "); |
| 156 | for (i = 0; (i < 5); i++) { |
| 157 | bd->bi_enetaddr[i] = eth_mac[i]; |
| 158 | printf("%02x:", eth_mac[i]); |
| 159 | } |
| 160 | bd->bi_enetaddr[i] = eth_mac[i]; |
| 161 | printf("%02x\n", eth_mac[i]); |
| 162 | } |
| 163 | |
| 164 | /****************************************************************************/ |
| 165 | |
| 166 | int eth_init(bd_t *bd) |
| 167 | { |
| 168 | debug ("%s(%d): eth_init()\n", __FILE__, __LINE__); |
| 169 | |
| 170 | eth_reset(bd); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /****************************************************************************/ |
| 175 | |
| 176 | void eth_halt(void) |
| 177 | { |
| 178 | debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__); |
| 179 | |
| 180 | /* Reset the ethernet engines */ |
| 181 | ks8695_write(KS8695_LAN_DMA_TX, 0x80000000); |
| 182 | ks8695_write(KS8695_LAN_DMA_RX, 0x80000000); |
| 183 | } |
| 184 | |
| 185 | /****************************************************************************/ |
| 186 | |
| 187 | int eth_rx(void) |
| 188 | { |
| 189 | volatile struct ks8695_rxdesc *dp; |
| 190 | int i, len = 0; |
| 191 | |
| 192 | debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__); |
| 193 | |
| 194 | for (i = 0; (i < RXDESCS); i++) { |
| 195 | dp= &ks8695_rx[i]; |
| 196 | if ((dp->status & 0x80000000) == 0) { |
| 197 | len = (dp->status & 0x7ff) - 4; |
| 198 | NetReceive((void *) dp->addr, len); |
| 199 | dp->status = 0x80000000; |
| 200 | ks8695_write(KS8695_LAN_DMA_RX_START, 0x1); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return len; |
| 206 | } |
| 207 | |
| 208 | /****************************************************************************/ |
| 209 | |
| 210 | int eth_send(volatile void *packet, int len) |
| 211 | { |
| 212 | volatile struct ks8695_txdesc *dp; |
| 213 | static int next = 0; |
| 214 | |
| 215 | debug ("%s(%d): eth_send(packet=%x,len=%d)\n", __FILE__, __LINE__, |
| 216 | packet, len); |
| 217 | |
| 218 | dp = &ks8695_tx[next]; |
Wolfgang Denk | 611125a | 2006-03-12 01:23:43 +0100 | [diff] [blame] | 219 | memcpy((void *) dp->addr, (void *) packet, len); |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 220 | |
| 221 | if (len < 64) { |
Wolfgang Denk | 611125a | 2006-03-12 01:23:43 +0100 | [diff] [blame] | 222 | memset((void *) (dp->addr + len), 0, 64-len); |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 223 | len = 64; |
| 224 | } |
| 225 | |
| 226 | dp->ctrl = len | 0xe0000000; |
| 227 | dp->owner = 0x80000000; |
| 228 | |
| 229 | ks8695_write(KS8695_LAN_DMA_TX, 0x3); |
| 230 | ks8695_write(KS8695_LAN_DMA_TX_START, 0x1); |
| 231 | |
| 232 | if (++next >= TXDESCS) |
| 233 | next = 0; |
| 234 | |
| 235 | return len; |
| 236 | } |
| 237 | |
wdenk | 2eab48f | 2005-05-23 10:49:50 +0000 | [diff] [blame] | 238 | #endif /* CONFIG_DRIVER_KS8695ETH */ |