blob: a0f5be81dd551e6756d580b59ee79b9fb5b76819 [file] [log] [blame]
wdenka21ad7b2005-05-19 22:39:42 +00001/*
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>
24#include <malloc.h>
25#include <net.h>
26#include <asm/io.h>
27#include <asm/arch/platform.h>
28
Wolfgang Denk37648b72006-03-12 01:40:01 +010029#ifdef CONFIG_DRIVER_KS8695ETH
wdenka21ad7b2005-05-19 22:39:42 +000030/****************************************************************************/
31
32/*
33 * Hardware register access to the KS8695 LAN ethernet port
34 * (well, it is the 4 port switch really).
35 */
36#define ks8695_read(a) *((volatile unsigned long *) (KS8695_IO_BASE + (a)))
37#define ks8695_write(a,v) *((volatile unsigned long *) (KS8695_IO_BASE + (a))) = (v)
38
39/****************************************************************************/
40
41/*
42 * Define the descriptor in-memory data structures.
43 */
44struct ks8695_txdesc {
45 uint32_t owner;
46 uint32_t ctrl;
47 uint32_t addr;
48 uint32_t next;
49};
50
51struct ks8695_rxdesc {
52 uint32_t status;
53 uint32_t ctrl;
54 uint32_t addr;
55 uint32_t next;
56};
57
58/****************************************************************************/
59
60/*
61 * Allocate local data structures to use for receiving and sending
62 * packets. Just to keep it all nice and simple.
63 */
64
65#define TXDESCS 4
66#define RXDESCS 4
67#define BUFSIZE 2048
68
69volatile struct ks8695_txdesc ks8695_tx[TXDESCS] __attribute__((aligned(256)));
70volatile struct ks8695_rxdesc ks8695_rx[RXDESCS] __attribute__((aligned(256)));
71volatile uint8_t ks8695_bufs[BUFSIZE*(TXDESCS+RXDESCS)] __attribute__((aligned(2048)));;
72
73/****************************************************************************/
74
75/*
76 * Ideally we want to use the MAC address stored in flash.
77 * But we do some sanity checks in case they are not present
78 * first.
79 */
80unsigned char eth_mac[] = {
81 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00
82};
83
84void ks8695_getmac(void)
85{
86 unsigned char *fp;
87 int i;
88
89 /* Check if flash MAC is valid */
90 fp = (unsigned char *) 0x0201c000;
91 for (i = 0; (i < 6); i++) {
92 if ((fp[i] != 0) && (fp[i] != 0xff))
93 break;
94 }
95
96 /* If we found a valid looking MAC address then use it */
97 if (i < 6)
98 memcpy(&eth_mac[0], fp, 6);
99}
100
101/****************************************************************************/
102
103void eth_reset(bd_t *bd)
104{
105 int i;
106
107 debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__);
108
109 /* Reset the ethernet engines first */
110 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
111 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
112
113 ks8695_getmac();
114
115 /* Set MAC address */
116 ks8695_write(KS8695_LAN_MAC_LOW, (eth_mac[5] | (eth_mac[4] << 8) |
117 (eth_mac[3] << 16) | (eth_mac[2] << 24)));
118 ks8695_write(KS8695_LAN_MAC_HIGH, (eth_mac[1] | (eth_mac[0] << 8)));
119
120 /* Turn the 4 port switch on */
121 i = ks8695_read(KS8695_SWITCH_CTRL0);
122 ks8695_write(KS8695_SWITCH_CTRL0, (i | 0x1));
123 /* ks8695_write(KS8695_WAN_CONTROL, 0x3f000066); */
124
125 /* Initialize descriptor rings */
126 for (i = 0; (i < TXDESCS); i++) {
127 ks8695_tx[i].owner = 0;
128 ks8695_tx[i].ctrl = 0;
129 ks8695_tx[i].addr = (uint32_t) &ks8695_bufs[i*BUFSIZE];
130 ks8695_tx[i].next = (uint32_t) &ks8695_tx[i+1];
131 }
132 ks8695_tx[TXDESCS-1].ctrl = 0x02000000;
133 ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0];
134
135 for (i = 0; (i < RXDESCS); i++) {
136 ks8695_rx[i].status = 0x80000000;
137 ks8695_rx[i].ctrl = BUFSIZE - 4;
138 ks8695_rx[i].addr = (uint32_t) &ks8695_bufs[(i+TXDESCS)*BUFSIZE];
139 ks8695_rx[i].next = (uint32_t) &ks8695_rx[i+1];
140 }
141 ks8695_rx[RXDESCS-1].ctrl |= 0x00080000;
142 ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0];
143
144 /* The KS8695 is pretty slow reseting the ethernets... */
145 udelay(2000000);
146
147 /* Enable the ethernet engine */
148 ks8695_write(KS8695_LAN_TX_LIST, (uint32_t) &ks8695_tx[0]);
149 ks8695_write(KS8695_LAN_RX_LIST, (uint32_t) &ks8695_rx[0]);
150 ks8695_write(KS8695_LAN_DMA_TX, 0x3);
151 ks8695_write(KS8695_LAN_DMA_RX, 0x71);
152 ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
153
154 printf("KS8695 ETHERNET: ");
155 for (i = 0; (i < 5); i++) {
156 bd->bi_enetaddr[i] = eth_mac[i];
157 printf("%02x:", eth_mac[i]);
158 }
159 bd->bi_enetaddr[i] = eth_mac[i];
160 printf("%02x\n", eth_mac[i]);
161}
162
163/****************************************************************************/
164
165int eth_init(bd_t *bd)
166{
167 debug ("%s(%d): eth_init()\n", __FILE__, __LINE__);
168
169 eth_reset(bd);
170 return 0;
171}
172
173/****************************************************************************/
174
175void eth_halt(void)
176{
177 debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
178
179 /* Reset the ethernet engines */
180 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
181 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
182}
183
184/****************************************************************************/
185
186int eth_rx(void)
187{
188 volatile struct ks8695_rxdesc *dp;
189 int i, len = 0;
190
191 debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
192
193 for (i = 0; (i < RXDESCS); i++) {
194 dp= &ks8695_rx[i];
195 if ((dp->status & 0x80000000) == 0) {
196 len = (dp->status & 0x7ff) - 4;
197 NetReceive((void *) dp->addr, len);
198 dp->status = 0x80000000;
199 ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
200 break;
201 }
202 }
203
204 return len;
205}
206
207/****************************************************************************/
208
209int eth_send(volatile void *packet, int len)
210{
211 volatile struct ks8695_txdesc *dp;
212 static int next = 0;
213
214 debug ("%s(%d): eth_send(packet=%x,len=%d)\n", __FILE__, __LINE__,
215 packet, len);
216
217 dp = &ks8695_tx[next];
Wolfgang Denk3383bd12006-03-12 01:23:43 +0100218 memcpy((void *) dp->addr, (void *) packet, len);
wdenka21ad7b2005-05-19 22:39:42 +0000219
220 if (len < 64) {
Wolfgang Denk3383bd12006-03-12 01:23:43 +0100221 memset((void *) (dp->addr + len), 0, 64-len);
wdenka21ad7b2005-05-19 22:39:42 +0000222 len = 64;
223 }
224
225 dp->ctrl = len | 0xe0000000;
226 dp->owner = 0x80000000;
227
228 ks8695_write(KS8695_LAN_DMA_TX, 0x3);
229 ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
230
231 if (++next >= TXDESCS)
232 next = 0;
233
234 return len;
235}
236
wdenkf593f462005-05-23 10:49:50 +0000237#endif /* CONFIG_DRIVER_KS8695ETH */