blob: b4904b68e5e676c138f2eec723735a5974136e9a [file] [log] [blame]
wdenk3a574cb2005-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
29/****************************************************************************/
30
31/*
32 * Hardware register access to the KS8695 LAN ethernet port
33 * (well, it is the 4 port switch really).
34 */
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)
37
38/****************************************************************************/
39
40/*
41 * Define the descriptor in-memory data structures.
42 */
43struct ks8695_txdesc {
44 uint32_t owner;
45 uint32_t ctrl;
46 uint32_t addr;
47 uint32_t next;
48};
49
50struct ks8695_rxdesc {
51 uint32_t status;
52 uint32_t ctrl;
53 uint32_t addr;
54 uint32_t next;
55};
56
57/****************************************************************************/
58
59/*
60 * Allocate local data structures to use for receiving and sending
61 * packets. Just to keep it all nice and simple.
62 */
63
64#define TXDESCS 4
65#define RXDESCS 4
66#define BUFSIZE 2048
67
68volatile struct ks8695_txdesc ks8695_tx[TXDESCS] __attribute__((aligned(256)));
69volatile struct ks8695_rxdesc ks8695_rx[RXDESCS] __attribute__((aligned(256)));
70volatile uint8_t ks8695_bufs[BUFSIZE*(TXDESCS+RXDESCS)] __attribute__((aligned(2048)));;
71
72/****************************************************************************/
73
74/*
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
77 * first.
78 */
79unsigned char eth_mac[] = {
80 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00
81};
82
83void ks8695_getmac(void)
84{
85 unsigned char *fp;
86 int i;
87
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))
92 break;
93 }
94
95 /* If we found a valid looking MAC address then use it */
96 if (i < 6)
97 memcpy(&eth_mac[0], fp, 6);
98}
99
100/****************************************************************************/
101
Greg Ungerer1e8ff712011-09-10 18:40:34 +1000102static int ks8695_eth_init(struct eth_device *dev, bd_t *bd)
wdenk3a574cb2005-05-19 22:39:42 +0000103{
104 int i;
105
106 debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__);
107
108 /* Reset the ethernet engines first */
109 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
110 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
111
112 ks8695_getmac();
113
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)));
118
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); */
123
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];
130 }
131 ks8695_tx[TXDESCS-1].ctrl = 0x02000000;
132 ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0];
133
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];
139 }
140 ks8695_rx[RXDESCS-1].ctrl |= 0x00080000;
141 ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0];
142
143 /* The KS8695 is pretty slow reseting the ethernets... */
144 udelay(2000000);
145
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);
152
Mike Frysingerd3f87142009-02-11 19:01:26 -0500153 printf("KS8695 ETHERNET: %pM\n", eth_mac);
wdenk3a574cb2005-05-19 22:39:42 +0000154 return 0;
155}
156
157/****************************************************************************/
158
Greg Ungerer1e8ff712011-09-10 18:40:34 +1000159static void ks8695_eth_halt(struct eth_device *dev)
wdenk3a574cb2005-05-19 22:39:42 +0000160{
161 debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
162
163 /* Reset the ethernet engines */
164 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
165 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
166}
167
168/****************************************************************************/
169
Greg Ungerer1e8ff712011-09-10 18:40:34 +1000170static int ks8695_eth_recv(struct eth_device *dev)
wdenk3a574cb2005-05-19 22:39:42 +0000171{
172 volatile struct ks8695_rxdesc *dp;
173 int i, len = 0;
174
175 debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
176
177 for (i = 0; (i < RXDESCS); i++) {
178 dp= &ks8695_rx[i];
179 if ((dp->status & 0x80000000) == 0) {
180 len = (dp->status & 0x7ff) - 4;
181 NetReceive((void *) dp->addr, len);
182 dp->status = 0x80000000;
183 ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
184 break;
185 }
186 }
187
188 return len;
189}
190
191/****************************************************************************/
192
Joe Hershberger78a7c172012-05-21 14:45:29 +0000193static int ks8695_eth_send(struct eth_device *dev, void *packet, int len)
wdenk3a574cb2005-05-19 22:39:42 +0000194{
195 volatile struct ks8695_txdesc *dp;
196 static int next = 0;
197
Marek Vasut99724a22011-10-24 23:40:01 +0000198 debug ("%s(%d): eth_send(packet=%p,len=%d)\n", __FILE__, __LINE__,
wdenk3a574cb2005-05-19 22:39:42 +0000199 packet, len);
200
201 dp = &ks8695_tx[next];
Wolfgang Denk611125a2006-03-12 01:23:43 +0100202 memcpy((void *) dp->addr, (void *) packet, len);
wdenk3a574cb2005-05-19 22:39:42 +0000203
204 if (len < 64) {
Wolfgang Denk611125a2006-03-12 01:23:43 +0100205 memset((void *) (dp->addr + len), 0, 64-len);
wdenk3a574cb2005-05-19 22:39:42 +0000206 len = 64;
207 }
208
209 dp->ctrl = len | 0xe0000000;
210 dp->owner = 0x80000000;
211
212 ks8695_write(KS8695_LAN_DMA_TX, 0x3);
213 ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
214
215 if (++next >= TXDESCS)
216 next = 0;
217
Greg Ungerer1e8ff712011-09-10 18:40:34 +1000218 return 0;
219}
220
221/****************************************************************************/
222
223int ks8695_eth_initialize(void)
224{
225 struct eth_device *dev;
226
227 dev = malloc(sizeof(*dev));
228 if (dev == NULL)
229 return -1;
230 memset(dev, 0, sizeof(*dev));
231
232 dev->iobase = KS8695_IO_BASE + KS8695_LAN_DMA_TX;
233 dev->init = ks8695_eth_init;
234 dev->halt = ks8695_eth_halt;
235 dev->send = ks8695_eth_send;
236 dev->recv = ks8695_eth_recv;
237 strcpy(dev->name, "ks8695eth");
238
239 eth_register(dev);
240 return 0;
wdenk3a574cb2005-05-19 22:39:42 +0000241}