wait-for-power.patch
[u-boot-openmoko/mini2440.git] / drivers / net / ks8695eth.c
blobb598dd7f23d2cbd3d79723e1033e16ac6b1994ef
1 /*
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 /****************************************************************************/
23 #include <common.h>
25 #ifdef CONFIG_DRIVER_KS8695ETH
26 #include <malloc.h>
27 #include <net.h>
28 #include <asm/io.h>
29 #include <asm/arch/platform.h>
31 /****************************************************************************/
34 * Hardware register access to the KS8695 LAN ethernet port
35 * (well, it is the 4 port switch really).
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)
40 /****************************************************************************/
43 * Define the descriptor in-memory data structures.
45 struct ks8695_txdesc {
46 uint32_t owner;
47 uint32_t ctrl;
48 uint32_t addr;
49 uint32_t next;
52 struct ks8695_rxdesc {
53 uint32_t status;
54 uint32_t ctrl;
55 uint32_t addr;
56 uint32_t next;
59 /****************************************************************************/
62 * Allocate local data structures to use for receiving and sending
63 * packets. Just to keep it all nice and simple.
66 #define TXDESCS 4
67 #define RXDESCS 4
68 #define BUFSIZE 2048
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)));;
74 /****************************************************************************/
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.
81 unsigned char eth_mac[] = {
82 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00
85 void ks8695_getmac(void)
87 unsigned char *fp;
88 int i;
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;
97 /* If we found a valid looking MAC address then use it */
98 if (i < 6)
99 memcpy(&eth_mac[0], fp, 6);
102 /****************************************************************************/
104 void eth_reset(bd_t *bd)
106 int i;
108 debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__);
110 /* Reset the ethernet engines first */
111 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
112 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
114 ks8695_getmac();
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)));
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); */
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];
133 ks8695_tx[TXDESCS-1].ctrl = 0x02000000;
134 ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0];
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];
142 ks8695_rx[RXDESCS-1].ctrl |= 0x00080000;
143 ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0];
145 /* The KS8695 is pretty slow reseting the ethernets... */
146 udelay(2000000);
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);
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]);
160 bd->bi_enetaddr[i] = eth_mac[i];
161 printf("%02x\n", eth_mac[i]);
164 /****************************************************************************/
166 int eth_init(bd_t *bd)
168 debug ("%s(%d): eth_init()\n", __FILE__, __LINE__);
170 eth_reset(bd);
171 return 0;
174 /****************************************************************************/
176 void eth_halt(void)
178 debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
180 /* Reset the ethernet engines */
181 ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
182 ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
185 /****************************************************************************/
187 int eth_rx(void)
189 volatile struct ks8695_rxdesc *dp;
190 int i, len = 0;
192 debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
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;
205 return len;
208 /****************************************************************************/
210 int eth_send(volatile void *packet, int len)
212 volatile struct ks8695_txdesc *dp;
213 static int next = 0;
215 debug ("%s(%d): eth_send(packet=%x,len=%d)\n", __FILE__, __LINE__,
216 packet, len);
218 dp = &ks8695_tx[next];
219 memcpy((void *) dp->addr, (void *) packet, len);
221 if (len < 64) {
222 memset((void *) (dp->addr + len), 0, 64-len);
223 len = 64;
226 dp->ctrl = len | 0xe0000000;
227 dp->owner = 0x80000000;
229 ks8695_write(KS8695_LAN_DMA_TX, 0x3);
230 ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
232 if (++next >= TXDESCS)
233 next = 0;
235 return len;
238 #endif /* CONFIG_DRIVER_KS8695ETH */