2 * ks8695eth.c -- KS8695 ethernet driver
4 * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com>
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.
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.
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
21 /****************************************************************************/
27 #include <asm/arch/platform.h>
29 /****************************************************************************/
32 * Hardware register access to the KS8695 LAN ethernet port
33 * (well, it is the 4 port switch really).
35 #define ks8695_read(a) *((volatile unsigned long *) (KS8695_IO_BASE + (a)))
36 #define ks8695_write(a,v) *((volatile unsigned long *) (KS8695_IO_BASE + (a))) = (v)
38 /****************************************************************************/
41 * Define the descriptor in-memory data structures.
43 struct ks8695_txdesc {
50 struct ks8695_rxdesc {
57 /****************************************************************************/
60 * Allocate local data structures to use for receiving and sending
61 * packets. Just to keep it all nice and simple.
68 volatile struct ks8695_txdesc ks8695_tx[TXDESCS] __attribute__((aligned(256)));
69 volatile struct ks8695_rxdesc ks8695_rx[RXDESCS] __attribute__((aligned(256)));
70 volatile uint8_t ks8695_bufs[BUFSIZE*(TXDESCS+RXDESCS)] __attribute__((aligned(2048)));;
72 /****************************************************************************/
75 * Ideally we want to use the MAC address stored in flash.
76 * But we do some sanity checks in case they are not present
79 unsigned char eth_mac[] = {
80 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00
83 void ks8695_getmac(void)
88 /* Check if flash MAC is valid */
89 fp = (unsigned char *) 0x0201c000;
90 for (i = 0; (i < 6); i++) {
91 if ((fp[i] != 0) && (fp[i] != 0xff))
95 /* If we found a valid looking MAC address then use it */
97 memcpy(ð_mac[0], fp, 6);
100 /****************************************************************************/
102 void eth_reset(bd_t *bd)
106 debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__);
108 /* Reset the ethernet engines first */
109 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
110 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
114 /* Set MAC address */
115 ks8695_write(KS8695_LAN_MAC_LOW, (eth_mac[5] | (eth_mac[4] << 8) |
116 (eth_mac[3] << 16) | (eth_mac[2] << 24)));
117 ks8695_write(KS8695_LAN_MAC_HIGH, (eth_mac[1] | (eth_mac[0] << 8)));
119 /* Turn the 4 port switch on */
120 i = ks8695_read(KS8695_SWITCH_CTRL0);
121 ks8695_write(KS8695_SWITCH_CTRL0, (i | 0x1));
122 /* ks8695_write(KS8695_WAN_CONTROL, 0x3f000066); */
124 /* Initialize descriptor rings */
125 for (i = 0; (i < TXDESCS); i++) {
126 ks8695_tx[i].owner = 0;
127 ks8695_tx[i].ctrl = 0;
128 ks8695_tx[i].addr = (uint32_t) &ks8695_bufs[i*BUFSIZE];
129 ks8695_tx[i].next = (uint32_t) &ks8695_tx[i+1];
131 ks8695_tx[TXDESCS-1].ctrl = 0x02000000;
132 ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0];
134 for (i = 0; (i < RXDESCS); i++) {
135 ks8695_rx[i].status = 0x80000000;
136 ks8695_rx[i].ctrl = BUFSIZE - 4;
137 ks8695_rx[i].addr = (uint32_t) &ks8695_bufs[(i+TXDESCS)*BUFSIZE];
138 ks8695_rx[i].next = (uint32_t) &ks8695_rx[i+1];
140 ks8695_rx[RXDESCS-1].ctrl |= 0x00080000;
141 ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0];
143 /* The KS8695 is pretty slow reseting the ethernets... */
146 /* Enable the ethernet engine */
147 ks8695_write(KS8695_LAN_TX_LIST, (uint32_t) &ks8695_tx[0]);
148 ks8695_write(KS8695_LAN_RX_LIST, (uint32_t) &ks8695_rx[0]);
149 ks8695_write(KS8695_LAN_DMA_TX, 0x3);
150 ks8695_write(KS8695_LAN_DMA_RX, 0x71);
151 ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
153 printf("KS8695 ETHERNET: ");
154 for (i = 0; (i < 5); i++) {
155 bd->bi_enetaddr[i] = eth_mac[i];
156 printf("%02x:", eth_mac[i]);
158 bd->bi_enetaddr[i] = eth_mac[i];
159 printf("%02x\n", eth_mac[i]);
162 /****************************************************************************/
164 int eth_init(bd_t *bd)
166 debug ("%s(%d): eth_init()\n", __FILE__, __LINE__);
172 /****************************************************************************/
176 debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
178 /* Reset the ethernet engines */
179 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
180 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
183 /****************************************************************************/
187 volatile struct ks8695_rxdesc *dp;
190 debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
192 for (i = 0; (i < RXDESCS); i++) {
194 if ((dp->status & 0x80000000) == 0) {
195 len = (dp->status & 0x7ff) - 4;
196 NetReceive((void *) dp->addr, len);
197 dp->status = 0x80000000;
198 ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
206 /****************************************************************************/
208 int eth_send(volatile void *packet, int len)
210 volatile struct ks8695_txdesc *dp;
213 debug ("%s(%d): eth_send(packet=%x,len=%d)\n", __FILE__, __LINE__,
216 dp = &ks8695_tx[next];
217 memcpy((void *) dp->addr, (void *) packet, len);
220 memset((void *) (dp->addr + len), 0, 64-len);
224 dp->ctrl = len | 0xe0000000;
225 dp->owner = 0x80000000;
227 ks8695_write(KS8695_LAN_DMA_TX, 0x3);
228 ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
230 if (++next >= TXDESCS)