Debian Codegraph @

contrib/ports/old/c16x/netif/cs8900if.c

raw symbols SCIP
1 /** @file
2 *
3 * Ethernet network driver for IP
4 */
5 /*
6 * Copyright (c) 2001-2003 Leon Woestenberg <[email protected]>
7 * Copyright (c) 2001-2003 Axon Digital Design B.V., The Netherlands.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Leon Woestenberg <[email protected]>
35 *
36 * This is a device driver for the Crystal Semiconductor CS8900
37 * chip in combination with the lwIP stack.
38 *
39 * This is work under development. Please coordinate changes
40 * and requests with Leon Woestenberg <[email protected]>
41 *
42 * The Swedish Institute of Computer Science and Adam Dunkels
43 * are specifically granted permission to redistribute this
44 * source code under any conditions they seem fit.
45 *
46 * A quick function roadmap:
47 *
48 * cs8900_*() are low level, cs8900 hardware specific functions.
49 * These are declared static in the device driver source and
50 * SHOULD NOT need to be called from outside this source.
51 *
52 * cs8900if_*() are the lwIP network interface functions.
53 *
54 * cs8900_interrupt() is an early interrupt service routine (ISR).
55 * It merely sets a flag to indicate the cs8900 needs servicing.
56 * (This function MAY be tied to an interrupt vector, IF present).
57 *
58 * cs8900_service() is the actual interrupt event service routine.
59 * It must be called whenever the cs8900 needs servicing. It MAY
60 * be polled safely (so, you do NOT NEED interrupt support.)
61 *
62 * cs8900_init() sets up the cs8900, using its register set. When
63 * using the driver on your particular hardware platform, make sure
64 * the register setups match.
65 * Function is called from cs8900if_init().
66 *
67 * cs8900_input() transfers a received packet from the chip.
68 * Function is called from cs8900if_input().
69 *
70 * cs8900_output() transfers a packet to the chip for transmission.
71 * Function is called as netif->linkoutput from etharp_output().
72 *
73 * cs8900if_init() initializes the lwIP network interface, and
74 * calls cs8900_init() to initialize the hardware.
75 * Function is called from lwIP.
76 *
77 * cs8900if_service() is the service routine, which must be called
78 * upon the need for service, or on a regular basis, in order to
79 * service the Ethernet chip.
80 *
81 * cs8900if_input() calls cs8900_input() to get a received packet
82 * and then forwards the packet to protocol(s) handler(s).
83 * Function is called from cs8900_service().
84 *
85 * netif->output() resolves the hardware address, then
86 * calls cs8900_output() (as netif->linkoutput()) to transfer the packet.
87 * Function is called from lwIP.
88 *
89 * Future development:
90 *
91 * Split the generic Ethernet functionality (a lot of the
92 * cs8900if_*() functions) and the actual cs8900a dependencies.
93 *
94 * Enhance the interrupt handler to service the Ethernet
95 * chip (to decrease latency); support early packet
96 * inspection (during reception) to early drop unwanted
97 * packets, minimize chip buffer use and maximize throughput.
98 *
99 * Statistics gathering, currently under development.
100 * SNMP support, currently under development.
101 *
102 */
103
104 #include "lwip/opt.h"
105 #include "lwip/def.h"
106 #include "lwip/err.h"
107 #include "lwip/mem.h"
108 #include "lwip/pbuf.h"
109 #include "lwip/stats.h"
110 #include "lwip/sys.h"
111 #include "netif/etharp.h"
112
113 #if 0
114 // include some debugging help
115 # define LWIP_DBG_LEVEL 1
116 # include "leds.h"
117 # include "display.h"
118 //# include "page.h"
119 # define LED_NEED_SERVICE LED_FP1
120 #else
121 // no debugging
122 # define leds_on()
123 # define leds_off()
124 #endif
125
126 /**
127 * Dependend on physical layer. This is a safe minimum for 802.3 10base5/T.
128 * @sa RFC1042
129 */
130 #define ETH_MIN_FRAME_LEN 60
131
132 #include "cs8900if.h"
133 #include "lwip/snmp.h"
134
135 // Define those to better describe your network interface
136 #define IFNAME0 'e'
137 #define IFNAME1 'n'
138
139 // Forward declarations
140 static err_t cs8900_output(struct netif *netif, struct pbuf *p);
141 static struct pbuf *cs8900_input(struct netif *netif);
142 static void cs8900_service(struct netif *netif);
143 static u32_t cs8900_chksum(void *dataptr, s16_t len);
144 static void cs8900_reset(struct netif *netif);
145
146 // Define these to match your hardware setup
147 #define MEM_BASE 0x00E000
148 #define IO_BASE 0x800
149 #define INT_NR 0x00
150
151 #define RXTXREG *((volatile u16_t *)(MEM_BASE + IO_BASE))
152 #define TXCMD *((volatile u16_t *)(MEM_BASE + IO_BASE + 0x04))
153 #define TXLENGTH *((volatile u16_t *)(MEM_BASE + IO_BASE + 0x06))
154 #define ISQ *((volatile u16_t *)(MEM_BASE + IO_BASE + 0x08))
155 #define PACKETPP *((volatile u16_t *)(MEM_BASE + IO_BASE + 0x0A))
156 #define PPDATA *((volatile u16_t *)(MEM_BASE + IO_BASE + 0x0C))
157
158 // CS8900 PacketPage register offsets
159 #define CS_PP_EISA 0x0000 // EISA Registration number of CS8900
160 #define CS_PP_PRODID 0x0002 // Product ID Number
161 #define CS_PP_IOBASE 0x0020 // I/O Base Address
162 #define CS_PP_INTNUM 0x0022 // Interrupt number (0,1,2, or 3)
163 #define CS_PP_RXCFG 0x0102 // Receiver Configuration
164 #define CS_PP_RXCTL 0x0104 // Receiver Control
165 #define CS_PP_TXCFG 0x0106 // Transmit Configuration
166 #define CS_PP_BUFCFG 0x010A // Buffer Configuration
167 #define CS_PP_LINECTL 0x0112 // Line Control Register offset
168 #define CS_PP_SELFCTL 0x0114 // Self Control
169 #define CS_PP_BUSCTL 0x0116 // Bus Control
170 #define CS_PP_TESTCTL 0x0118 // Test Control
171 #define CS_PP_ISQ 0x0120 // Interrupt status queue
172 #define CS_PP_RXEVENT 0x0124 // Receiver Event
173 #define CS_PP_TX_EVENT 0x0128 // Transmitter Event
174 #define CS_PP_BUF_EVENT 0x012C // Buffer Event
175 #define CS_PP_RXMISS 0x0130 // Receiver Miss Counter
176 #define CS_PP_TXCOL 0x0132 // Transmit Collision Counter
177 #define CS_PP_LINESTATUS 0x0134 // Line Status
178 #define CS_PP_SELFTEST 0x0136 // Self Status
179 #define CS_PP_BUSSTATUS 0x0138 // Bus Status
180 #define CS_PP_TXCMD 0x0144 // Transmit Command Request
181 #define CS_PP_TXLEN 0x0146 // Transmit Length
182 #define CS_PP_IA1 0x0158 // Individual Address (IA)
183 #define CS_PP_IA2 0x015A // Individual Address (IA)
184 #define CS_PP_IA3 0x015C // Individual Address (IA)
185
186 #define CS_PP_RXSTATUS 0x0400 // Receive Status
187 #define CS_PP_RXLEN 0x0402 // Receive Length
188 #define CS_PP_RXFRAME 0x0404 // Receive Frame Location
189 #define CS_PP_TXFRAME 0x0A00 // Transmit Frame Location
190
191
192 // removed interrupt from library
193 #if 0
194 // hardware interrupt vector handler
195 _interrupt(0x18) void cs8900_interrupt(void)
196 {
197 struct cs8900if *cs8900if = cs8900if_netif->state;
198 // network interface is configured?
199 if (cs8900if != NULL)
200 {
201 // chip needs service
202 cs8900if->needs_service = 1;
203 #if (CS8900_STATS > 0)
204 cs8900if->interrupts++;
205 #endif
206 }
207 }
208 #endif
209
210 /**
211 * Reset the CS8900A chip-wide using a soft reset
212 *
213 * @note You MUST wait 30 ms before accessing the CS8900
214 * after calling this function.
215 */
216 static void cs8900_reset(struct netif *netif)
217 {
218 (void)netif;
219 /* set RESET bit */
220 PACKETPP = CS_PP_SELFCTL;
221 PPDATA = 0x0055U;
222 }
223
224 // cs8900_init()
225 //
226 // initializes the CS8900A chip
227 //
228 static err_t cs8900_init(struct netif *netif)
229 {
230 #ifdef LED_NEED_SERVICE
231 leds_off(LED_NEED_SERVICE);
232 #endif
233
234 // { the RESET bit will be cleared by the cs8900a
235 // as a result of a hardware reset - wait for it}
236 // RESET bit cleared?
237 while ((PPDATA & 0x0040U) != 0); // TODO: add timeout
238
239 // { after full initialization of the cs8900a
240 // the INITD bit will be set }
241
242 PACKETPP = CS_PP_SELFTEST;
243 // INITD bit still clear?
244 while ((PPDATA & 0x0080U) == 0); // TODO: add timeout
245 // { INITD bit is set }
246
247 // SIBUSY bit still set?
248 while ((PPDATA & 0x0100U) == 0x0100); // TODO: add timeout
249 // { SIBUSY bit clear }
250
251 #if 1
252 {
253 u16_t dummy;
254 // datasheet section 3.3.3
255 dummy = *(u16_t *)(MEM_BASE + IO_BASE + 0x0D);
256 // Dummy read, put chip in 16-bit mode
257 dummy = *(u16_t *)(MEM_BASE + IO_BASE + 0x0D);
258 }
259 #endif
260
261 // Set MAC address
262 PACKETPP = CS_PP_IA1;
263 PPDATA = (u16_t)(netif->hwaddr[0]) | (u16_t)(netif->hwaddr[1] << 8U);
264 PACKETPP = CS_PP_IA2;
265 PPDATA = (u16_t)(netif->hwaddr[2]) | (u16_t)(netif->hwaddr[3] << 8U);
266 PACKETPP = CS_PP_IA3;
267 PPDATA = (u16_t)(netif->hwaddr[4]) | (u16_t)(netif->hwaddr[5] << 8U);
268
269 // accept valid unicast or broadcast frames
270 PACKETPP = CS_PP_RXCTL;
271 PPDATA = (0x0005U | 0x0800U/*broadcast*/ | 0x0400U/*individual*/ | 0x0100U/*RxOK*/);
272
273 // enable receive interrupt
274 PACKETPP = CS_PP_RXCFG;
275 PPDATA = (0x0003U | 0x0100U/*RXIRQ*/);
276
277 // disable transmit interrupt (is default)
278 PACKETPP = CS_PP_TXCFG;
279 PPDATA = (0x0007U | 0);
280
281 // use interrupt number 0
282 PACKETPP = CS_PP_INTNUM;
283 PPDATA = (0x0000U);
284
285 // generate interrupt event on:
286 // - the RxMISS counter reaches 0x200, or
287 // - a received frame is lost
288 PACKETPP = CS_PP_BUFCFG;
289 PPDATA = (0x000bU |
290 #if (CS8900_STATS > 0) // interrupt before counter overflow
291 (0x2000U/*MissOvfloiE*/ | 0x1000U/*TxColOvfloiE*/) |
292 #endif
293 #if (CS8900_STATS > 1) // interrupt on counter increment
294 (0x0400U/*RxMissiE*/) |
295 #endif
296 0x0000);
297
298 // enable interrupt generation
299 PACKETPP = CS_PP_BUSCTL;
300 PPDATA = (0x0017U | 0x8000U/*EnableIRQ*/);
301
302 // enable:
303 // - receiver
304 // - transmitter
305 PACKETPP = CS_PP_LINECTL;
306 PPDATA = (0x0013U | 0x0080U/*SerTxOn*/ | 0x0040U/*SerRxOn*/);
307
308 return ERR_OK;
309 }
310
311 /**
312 *
313 *
314 * @return error code
315 * - ERR_OK: packet transferred to hardware
316 * - ERR_CONN: no link or link failure
317 * - ERR_IF: could not transfer to link (hardware buffer full?)
318 */
319 static err_t cs8900_output(struct netif *netif, struct pbuf *p)
320 {
321 s16_t tries = 0;
322 err_t result;
323
324 // exit if link has failed
325 PACKETPP = CS_PP_LINESTATUS;
326 if ((PPDATA & 0x0080U/*LinkOK*/) == 0) return ERR_CONN; // no Ethernet link
327
328 result = ERR_OK;
329 /* TODO: should this occur AFTER setting TXLENGTH??? */
330 /* drop the padding word */
331 #if ETH_PAD_SIZE
332 pbuf_header(p, -ETH_PAD_SIZE);
333 #endif
334
335 /* issue 'transmit' command to CS8900 */
336 TXCMD = 0x00C9U;
337 /* send length (in bytes) of packet to send, but at least minimum frame length */
338 TXLENGTH = (p->tot_len < ETH_MIN_FRAME_LEN? ETH_MIN_FRAME_LEN: p->tot_len);
339
340 PACKETPP = CS_PP_BUSSTATUS;
341 // not ready for transmission and still within 100 retries?
342 while (((PPDATA & 0x0100U/*Rdy4TxNOW*/) == 0) && (tries++ < 100))
343 {
344 // throw away the last committed received frame
345 PACKETPP = CS_PP_RXCFG;
346 PPDATA = (0x0003U | 0x0040U/*Skip_1*/ | 0x0100U/*RxOKiE*/);
347 PACKETPP = CS_PP_BUSSTATUS;
348 /* cs8900if->dropped++; // CHECK: we do not know if we actually will drop a frame here */
349 }
350 // ready to transmit?
351 if ((PPDATA & 0x0100U/*Rdy4TxNOW*/) != 0)
352 {
353 u16_t sent_bytes = 0;
354 /* q traverses through linked list of pbuf's
355 * This list MUST consist of a single packet ONLY */
356 struct pbuf *q;
357 u16_t pbuf_index = 0;
358 u8_t word_index = 0;
359 u8_t word[2];
360 q = p;
361 /* Write data into CS8900, two bytes at a time
362 * Handling pbuf's with odd number of bytes correctly
363 * No attempt to optimize for speed has been made */
364 while (q)
365 {
366 if (pbuf_index < q->len)
367 {
368 word[word_index++] = ((u8_t*)q->payload)[pbuf_index++];
369 if (word_index == 2)
370 {
371 RXTXREG = (word[1] << 8) | word[0];
372 word_index = 0;
373 sent_bytes += 2;
374 }
375 }
376 else
377 {
378 q = q->next;
379 pbuf_index = 0;
380 }
381 }
382 /* One byte could still be unsent */
383 if (word_index == 1)
384 {
385 RXTXREG = word[0];
386 sent_bytes += 2;
387 }
388 /* provide any additional padding to comply with minimum Ethernet
389 * frame length (RFC10242) */
390 while (sent_bytes < ETH_MIN_FRAME_LEN)
391 {
392 RXTXREG = 0x0000;
393 sent_bytes += 2;
394 }
395 /* { the packet has been sent } */
396 #if (CS8900_STATS > 0)
397 ((struct cs8900if *)netif->state)->sentpackets++;
398 ((struct cs8900if *)netif->state)->sentbytes += sent_bytes;
399 #endif
400 snmp_add_ifoutoctets(netif,sent_bytes);
401 }
402 else
403 {
404 // { not ready to transmit!? }
405 snmp_inc_ifoutdiscards(netif);
406 /* return not connected */
407 result = ERR_IF;
408 }
409 #if ETH_PAD_SIZE
410 /* reclaim the padding word */
411 pbuf_header(p, ETH_PAD_SIZE);
412 #endif
413 return result;
414 }
415
416 /**
417 * Move a received packet from the cs8900 into a new pbuf.
418 *
419 * Must be called after reading an ISQ event containing the
420 * "Receiver Event" register, before reading new ISQ events.
421 *
422 * This function copies a frame from the CS8900A.
423 * It is designed failsafe:
424 * - It does not assume a frame is actually present.
425 * - It checks for non-zero length
426 * - It does not overflow the frame buffer
427 */
428 static struct pbuf *cs8900_input(struct netif *netif)
429 {
430 volatile u16_t* rxtx_reg;
431 volatile u32_t rxtx_num = (MEM_BASE + IO_BASE);
432 u16_t* ptr = NULL;
433 struct pbuf *p = NULL, *q = NULL;
434 u16_t len = 0;
435 u16_t event_type;
436 u16_t i;
437
438 /* optimized register mapping for Tasking c166 7.5 (default optimalisation setting)
439 Using RXTXREG directly produces inefficient code with many const address loads. */
440 rxtx_reg = ((volatile u16_t *)(rxtx_num));
441 // read RxStatus
442 event_type = *rxtx_reg;
443
444 // correctly received frame, either broadcast or individual address?
445 // TODO: maybe defer these conditions to cs8900_input()
446 if ((event_type & 0x0100U/*RxOK*/) && (event_type & 0x0c00U/*Broadcast | Individual*/))
447 {
448 #if LWIP_SNMP > 0
449 // update number of received MAC-unicast and non-MAC-unicast packets
450 if (event_type & 0x0400U/*Individual*/)
451 {
452 snmp_inc_ifinucastpkts(netif);
453 }
454 else
455 {
456 snmp_inc_ifinnucastpkts(netif);
457 }
458 #endif
459 event_type = 0;
460 // read RxLength
461 len = *rxtx_reg;
462 LWIP_DEBUGF(NETIF_DEBUG, ("cs8900_input: packet len %"U16_F"\n", len));
463 snmp_add_ifinoctets(netif,len);
464 // positive length?
465 if (len > 0)
466 {
467 // allocate a pbuf chain with total length 'len + ETH_PAD_SIZE'
468 p = pbuf_alloc(PBUF_RAW, len + ETH_PAD_SIZE, PBUF_POOL);
469 if (p != NULL)
470 {
471 #if ETH_PAD_SIZE
472 /* drop the padding word */
473 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
474 #endif
475 for (q = p; q != 0; q = q->next)
476 {
477 LWIP_DEBUGF(NETIF_DEBUG, ("cs8900_input: pbuf @%p tot_len %"U16_F" len %"U16_F"\n", q, q->tot_len, q->len));
478
479 /* read 8 bytes per iteration */
480 ptr = q->payload;
481 i = q->len / 8;
482 while(i > 0)
483 {
484 *ptr = *rxtx_reg;
485 ptr++;
486 *ptr = *rxtx_reg;
487 ptr++;
488 *ptr = *rxtx_reg;
489 ptr++;
490 *ptr = *rxtx_reg;
491 ptr++;
492 i--;
493 }
494 /* read remainder */
495 i = ((q->len % 8) + 1) / 2;
496 while(i > 0)
497 {
498 *ptr = *rxtx_reg;
499 ptr++;
500 i--;
501 }
502 }
503 #if ETH_PAD_SIZE
504 /* reclaim the padding word */
505 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
506 #endif
507 }
508 // could not allocate a pbuf
509 else
510 {
511 // skip received frame
512 // TODO: maybe do not skip the frame at this point in time?
513 PACKETPP = CS_PP_RXCFG;
514 PPDATA = (0x0003U | 0x0100U/*RxOKiE*/ | 0x0040U/*Skip_1*/);
515 #if (CS8900_STATS > 0)
516 ((struct cs8900if *)netif->state)->dropped++;
517 #endif
518 snmp_inc_ifindiscards(netif);
519 len = 0;
520 }
521 }
522 // length was zero
523 else
524 {
525 }
526 }
527 return p;
528 }
529
530
531 /**
532 * To be called when the cs8900a needs service. Does
533 * not assume the cs8900a needs service. Does test the
534 * cs8900a whether it needs service.
535 *
536 * As such, may be used robustly called as a deferred
537 * (or "late") interrupt handler, or may be called in
538 * a loop to implement polling, or both.
539 *
540 * Use cs8900if_service() from your application instead
541 * of this function.
542 */
543
544 static void cs8900_service(struct netif *netif)
545 {
546 u8_t events2service = CS8900_EVTS2SRV;
547 #if (CS8900_STATS > 0)
548 u16_t miss_count = 0, coll_count = 0;
549 #endif
550 // NOTES:
551 // static, so only initialized to zero at program start.
552 // irq_status will always hold the last ISQ event register that
553 // still needs service. As such, we may leave this function if
554 // we encounter an event we cannot service yet, and return later
555 // to try to service it.
556 static u16_t irq_status = 0x0000U;
557
558 // The "cs8900_needs_service" flag indicates whether any events
559 // still need to be serviced.
560 // clear flag here.
561 // a receive interrupt can, *concurrently with this function*,
562 // set this flag on new ISQ event occurences.
563 // we will re-evaluate the correct setting of this flag at
564 // function exit (below).
565 ((struct cs8900if *)netif->state)->needs_service = 0;
566 #ifdef LED_NEED_SERVICE
567 leds_off(LED_NEED_SERVICE);
568 #endif
569 /* no unhandled irq_status left? */
570 if (irq_status == 0x0000U)
571 {
572 /* read ISQ register */
573 irq_status = ISQ;
574 }
575 /* ISQ interrupt event, and allowed to service in this loop? */
576 while ((irq_status != 0x0000U) && (events2service-- > 0))
577 {
578 /* investigate event */
579 if ((irq_status & 0x003fU) == 0x0004U/*Receiver Event*/)
580 {
581 /* correctly received frame, either broadcast or individual address */
582 /* TODO: think where these checks should appear: here or in cs8900_input() */
583 if ((irq_status & 0x0100U/*RxOK*/) && (irq_status & 0x0c00U/*Broadcast | Individual*/))
584 {
585 /* read the frame from the cs8900a */
586 cs8900if_input(netif);
587 }
588 else
589 {
590 /* skip this frame */
591 PACKETPP = CS_PP_RXCFG;
592 PPDATA |= 0x0040U/*Skip_1*/;
593 #if (CS8900_STATS > 0)
594 ((struct cs8900if *)netif->state)->dropped++;
595 #endif
596 }
597 }
598 #if (CS8900_STATS > 0)
599 else if ((irq_status & 0x003fU) == 0x0010U/*RxMISS Event*/)
600 {
601 miss_count += (irq_status >> 6);
602 }
603 else if ((irq_status & 0x003fU) == 0x0012U/*TxCOL Event*/)
604 {
605 coll_count += (irq_status >> 6);
606 }
607 #endif
608 /* read ISQ register */
609 irq_status = ISQ;
610 }
611
612 /* we did not deplete the ISQ? */
613 if (irq_status != 0x0000U)
614 {
615 /* the cs8900a still needs service */
616 ((struct cs8900if *)netif->state)->needs_service = 1;
617 #ifdef LED_NEED_SERVICE
618 leds_on(LED_NEED_SERVICE);
619 #endif
620 }
621 #if (CS8900_STATS > 1) /* follow misses and collisions on a per-packet basis? */
622 /* read RxMiss Counter (zeroes itself upon read) */
623 PACKETPP = CS_PP_RXMISS;
624 miss_count += (PPDATA >> 6);
625 /* read RxCol Counter (zeroes itself upon read) */
626 PACKETPP = CS_PP_TXCOL;
627 coll_count += (PPDATA >> 6);
628 #endif
629 #if (CS8900_STATS > 0)
630 /* copy statistics counters into netif state fields */
631 ((struct cs8900if *)netif->state)->missed += miss_count;
632 if (miss_count > 0) LWIP_DEBUGF(NETIF_DEBUG | 1, ("cs8900_input: %"U16_F" missed packets due to rx buffer overrun\n", miss_count));
633
634 ((struct cs8900if *)netif->state)->collisions += coll_count;
635 if (coll_count > 0) LWIP_DEBUGF(NETIF_DEBUG | 1, ("cs8900_input: %"U16_F" packet collisions\n", coll_count));
636 #endif
637 }
638
639 /**
640 * Service the CS8900.
641 *
642 * Can be called in a polling manner, or only after the CS8900 has raised
643 * an interrupt request.
644 *
645 * @param netif The lwIP network interface data structure belonging to this device.
646 *
647 */
648 void cs8900if_service(struct netif *netif)
649 {
650 // is there a reason to call the service routine?
651 if ((((struct cs8900if *)netif->state)->needs_service) ||
652 (((struct cs8900if *)netif->state)->use_polling))
653 {
654 cs8900_service(netif);
655 }
656 }
657
658 /**
659 * Read a received packet from the CS8900.
660 *
661 * This function should be called when a packet is received by the CS8900
662 * and is fully available to read. It moves the received packet to a pbuf
663 * which is forwarded to the IP network layer or ARP module. It transmits
664 * a resulting ARP reply or queued packet.
665 *
666 * @param netif The lwIP network interface to read from.
667 *
668 * @internal Uses cs8900_input() to move the packet from the CS8900 to a
669 * newly allocated pbuf.
670 *
671 */
672 void cs8900if_input(struct netif *netif)
673 {
674 struct eth_hdr *ethhdr = NULL;
675 struct pbuf *p = NULL;
676
677 /* move received packet into a new pbuf */
678 p = cs8900_input(netif);
679 /* no packet could be read */
680 if (p == NULL) {
681 /* silently ignore this */
682 return;
683 }
684 /* points to packet payload, which starts with an Ethernet header */
685 ethhdr = p->payload;
686
687 switch (htons(ethhdr->type)) {
688 /* IP packet? */
689 case ETHTYPE_IP:
690 #if 0
691 /* CSi disabled ARP table update on ingress IP packets.
692 This seems to work but needs thorough testing. */
693 /* update ARP table */
694 etharp_ip_input(netif, p);
695 #endif
696 /* skip Ethernet header */
697 pbuf_header(p, -(s16_t)sizeof(struct eth_hdr));
698 LWIP_DEBUGF(NETIF_DEBUG, ("cs8900_input: passing packet up to IP\n"));
699 /* pass to network layer */
700 netif->input(p, netif);
701 break;
702 /* ARP packet? */
703 case ETHTYPE_ARP:
704 /* pass p to ARP module */
705 etharp_arp_input(netif, (struct eth_addr *)&netif->hwaddr, p);
706 break;
707 /* unsupported Ethernet packet type */
708 default:
709 /* free pbuf */
710 pbuf_free(p);
711 p = NULL;
712 break;
713 }
714 }
715
716 /**
717 * Reset the CS8900 Ethernet MAC/PHY chip.
718 *
719 * @param netif The lwIP network interface data structure belonging to this device.
720 * MAY be NULL as we do not support multiple devices yet.
721 * @note You SHOULD call cs8900if_init() afterwards to
722 * initialize and configure the chip.
723 */
724 void cs8900if_reset(struct netif *netif)
725 {
726 /* reset the cs8900a chip */
727 cs8900_reset(netif);
728 }
729
730 /**
731 * Initialize the CS8900 Ethernet MAC/PHY and its device driver.
732 *
733 * @param netif The lwIP network interface data structure belonging to this device.
734 * MAY be NULL as we do not support multiple devices yet.
735 *
736 */
737 err_t cs8900if_init(struct netif *netif)
738 {
739 struct cs8900if *cs8900if;
740
741 cs8900if = mem_malloc(sizeof(struct cs8900if));
742 if (cs8900if == NULL)
743 {
744 LWIP_DEBUGF(NETIF_DEBUG, ("cs8900_input: out of memory for cs8900if\n"));
745 return ERR_MEM;
746 }
747 /* initialize lwip network interface ... */
748 #if LWIP_SNMP
749 /* ifType ethernetCsmacd(6) */
750 netif->link_type = 6;
751 netif->link_speed = 10000000;
752 netif->ts = 0;
753 netif->ifinoctets = 0;
754 netif->ifinucastpkts = 0;
755 netif->ifinnucastpkts = 0;
756 netif->ifindiscards = 0;
757 netif->ifoutoctets = 0;
758 netif->ifoutucastpkts = 0;
759 netif->ifoutnucastpkts = 0;
760 netif->ifoutdiscards = 0;
761 #endif
762
763 /* administrative details */
764 netif->name[0] = IFNAME0;
765 netif->name[1] = IFNAME1;
766
767 /* downward functions */
768 netif->output = etharp_output;
769 netif->linkoutput = cs8900_output;
770
771 /* initialize cs8900 specific interface state data pointer */
772 netif->state = cs8900if;
773
774 /* maximum transfer unit */
775 netif->mtu = 1500;
776
777 /* broadcast capability */
778 netif->flags = NETIF_FLAG_BROADCAST;
779
780 /* hardware address length */
781 netif->hwaddr_len = 6;
782
783 /* initially assume no ISQ event */
784 cs8900if->needs_service = 0;
785 /* set to 1 if polling method is used */
786 cs8900if->use_polling = 0;
787
788 #if (CS8900_STATS > 0)
789 /* number of interrupt service routine calls */
790 cs8900if->interrupts = 0;
791 cs8900if->missed = 0;
792 cs8900if->dropped = 0;
793 cs8900if->collisions = 0;
794 cs8900if->sentpackets = 0;
795 cs8900if->sentbytes = 0;
796 #endif
797
798 /* intialize the cs8900a chip */
799 return cs8900_init(netif);
800 }
801
802 #if 1
803 /**
804 * Dump an array of bytes inside a UDP message's data field.
805 *
806 * It is a self-contained function, independent of higher protocol layers or other
807 * functions, so it allows you to debug these higher layers, such as lwIP.
808 *
809 * @param p pointer to an array of bytes, at least with length 'len'
810 * @param len number of bytes available at the address pointed to by 'p'
811 */
812 void cs8900_send_debug(u8_t *p, u16_t len)
813 {
814 s16_t tries = 0, i;
815
816 // network interface state
817 extern struct netif *ethif;
818
819 // exit if link has failed
820 PACKETPP = CS_PP_LINESTATUS;
821 if ((PPDATA & 0x0080U/*LinkOK*/) == 0) return; // TODO: find a correct error code
822
823 // transmit command
824 TXCMD = 0x00C9U;
825 // send at least 60 bytes
826 TXLENGTH = (14 + 20 + 8 + len < 60) ? 60 : (14 + 20 + 8 + len);
827
828 PACKETPP = CS_PP_BUSSTATUS;
829 // not ready for transmission and still within 100 retries?
830 while (((PPDATA & 0x0100U/*Rdy4TxNOW*/) == 0) && (tries++ < 100))
831 {
832 // throw away the last committed received frame
833 PACKETPP = CS_PP_RXCFG;
834 PPDATA = (0x0003U | 0x0040U/*Skip_1*/ | 0x0100U/*RxOKiE*/);
835 PACKETPP = CS_PP_BUSSTATUS;
836 /* cs8900if->dropped++; CHECK: we do not know if we actually will drop a frame here, do we? */
837 }
838 // ready to transmit?
839 if ((PPDATA & 0x0100U/*Rdy4TxNOW*/) != 0)
840 {
841 u16_t data, checksum = 0;
842 u32_t udp_checksum = 0;
843
844 // destination Ethernet address
845 RXTXREG = 0xa000U;
846 RXTXREG = 0xc524U;
847 RXTXREG = 0x6d72U;
848 // source Ethernet address
849 RXTXREG = htons(((u16_t)ethif->hwaddr[0] << 8U) | (u16_t)ethif->hwaddr[1]);
850 RXTXREG = htons(((u16_t)ethif->hwaddr[2] << 8U) | (u16_t)ethif->hwaddr[3]);
851 RXTXREG = htons(((u16_t)ethif->hwaddr[4] << 8U) | (u16_t)ethif->hwaddr[5]);
852 // frame type
853 RXTXREG = htons(0x0800);
854 // TOS, version
855 RXTXREG = htons(data = ((0x40 | 0x05) << 8) | 0x00);
856 checksum += data;
857 // length
858 RXTXREG = htons(data = 20 + 8 + len);
859 checksum += data;
860 // identifier
861 RXTXREG = htons(data = 0);
862 checksum += data;
863 // fragment offset
864 RXTXREG = htons(data = 0);
865 checksum += data;
866 // TTL, UDP protocol
867 RXTXREG = htons(data = (255U << 8) | 17U);
868 checksum += data;
869
870 checksum += (htonl(ethif->ip_addr.addr) & 0xffff0000U) >> 16;
871 checksum += (htonl(ethif->ip_addr.addr) & 0x0000ffffU);
872 checksum += 0xc0a8U;
873 checksum += 0x0001U;
874 checksum += 6; // LW: kludge/hack: checksum calculation seems to be wrong somehow
875 // LW: this seems (?) to fix it
876 // checksum
877 RXTXREG = htons(~checksum);
878
879 // source IP
880 RXTXREG = htons((htonl(ethif->ip_addr.addr) & 0xffff0000U) >> 16);
881 // source IP
882 RXTXREG = htons( htonl(ethif->ip_addr.addr) & 0x0000ffffU);
883 // destination IP
884 RXTXREG = htons(0xc0a8U);
885 // destination IP
886 RXTXREG = htons(0x0001U);
887 // source port 3000
888 RXTXREG = htons(3000U);
889 // destination port 3000
890 RXTXREG = htons(3000U);
891 // UDP length
892 RXTXREG = htons(len);
893 // UDP checksum (not present)
894
895 udp_checksum = (htonl(ethif->ip_addr.addr) & 0xffff0000U) >> 16;
896 udp_checksum += (htonl(ethif->ip_addr.addr) & 0x0000ffffU);
897 udp_checksum += 0xc0a8U;
898 udp_checksum += 0x0001U;
899 udp_checksum += 0x0011U;
900 udp_checksum += (8 + len);
901 udp_checksum += 3000;
902 udp_checksum += 3000;
903 udp_checksum += (8 + len);
904 udp_checksum += cs8900_chksum(p, len);
905 while (udp_checksum >> 16) {
906 udp_checksum = (udp_checksum & 0xffffUL) + (udp_checksum >> 16);
907 }
908
909 RXTXREG = htons(~(udp_checksum & 0xffff));
910 for (i = 0; i < len; i += 2)
911 {
912 RXTXREG = htons((p[i] << 8) | p[i + 1]);
913 }
914 // pad to 60 bytes
915 while (i < 60)
916 {
917 RXTXREG = 0;
918 i += 2;
919 }
920 }
921 }
922
923 static u32_t cs8900_chksum(void *dataptr, s16_t len)
924 {
925 u32_t acc = 0;
926 u16_t *ptr = (u16_t *)dataptr;
927
928 for(acc = 0; len > 1; len -= 2) {
929 acc += *ptr;
930 ptr++;
931 }
932 /* add up any odd byte */
933 if (len == 1) {
934 acc += htons((u16_t)((*(u8_t *)ptr) & 0xffU) << 8);
935 }
936 return acc;
937 }
938
939 #endif