source: rtems/c/src/lib/libbsp/powerpc/beatnik/network/porting/rtemscompat.h @ b925ae7

5
Last change on this file since b925ae7 was b925ae7, checked in by Sebastian Huber <sebastian.huber@…>, on 03/06/17 at 10:10:06

bsp/beatnik: Update due to API changes

The device_t typedef is already provided by <sys/types.h> if _KERNEL is
defined.

  • Property mode set to 100644
File size: 13.1 KB
Line 
1#ifndef RTEMS_COMPAT_BSD_NET_H
2#define RTEMS_COMPAT_BSD_NET_H
3
4/* BSD -> rtems wrappers; stuff that must be defined
5 *        prior to including most BSD headers
6 */
7
8/* Copyright: Till Straumann <strauman@slac.stanford.edu>, 2005;
9 * License:   see LICENSE file.
10 */
11
12#include <rtems.h>
13#include <sys/errno.h>
14#include <sys/types.h>
15
16#include <stdlib.h>
17
18/* Check for RTEMS version; true if >= ma.mi.re */
19#define ISMINVERSION(ma,mi,re) \
20        (    __RTEMS_MAJOR__  > (ma)    \
21         || (__RTEMS_MAJOR__ == (ma) && __RTEMS_MINOR__  > (mi))        \
22         || (__RTEMS_MAJOR__ == (ma) && __RTEMS_MINOR__ == (mi) && __RTEMS_REVISION__ >= (re)) \
23    )
24
25/* 'align' ARG is evaluated more than once */
26#define _DO_ALIGN(addr, align) (((uint32_t)(addr) + (align) - 1) & ~((align)-1))
27
28
29/* malloc/free are redefined :-( */
30static inline void *the_real_malloc(size_t n)
31{
32        return malloc(n);
33}
34
35static inline void  the_real_free(void *p)
36{
37        return free(p);
38}
39
40#define __INSIDE_RTEMS_BSD_TCPIP_STACK__
41#include <rtems/rtems_bsdnet.h>
42#include <rtems/rtems_bsdnet_internal.h>
43#ifdef __i386__
44#include <libcpu/cpu.h>
45#elif defined(__PPC__)
46#include <libcpu/io.h>
47#else
48#error "dunno what IO ops to use on this architecture"
49#endif
50#include <rtems/bspIo.h>
51
52#include "rtemscompat_defs.h"
53
54#define NET_EMB(x,y,z) x ## y ## z
55#define NET_EMBEMB(x,y,z) NET_EMB(x,y,z)
56
57#define NET_STR(arg)    #arg
58#define NET_STRSTR(arg) NET_STR(arg)
59
60#define NET_SOFTC       NET_EMBEMB(,NETDRIVER_PREFIX,_softc)
61
62#define METHODS     NET_EMBEMB(rtems_,NETDRIVER_PREFIX,_methods)
63extern struct _net_drv_tbl METHODS;
64
65#ifdef DEBUG_MODULAR
66#define METHODSPTR  NET_EMBEMB(rtems_,NETDRIVER_PREFIX,_methods_p)
67extern struct _net_drv_tbl *volatile METHODSPTR;
68#else
69#define METHODSPTR  (&METHODS)
70#endif
71
72#if defined(__LITTLE_ENDIAN__) || (__i386__)
73static inline uint16_t  htole16(uint16_t  v) { return v; }
74static inline uint32_t  htole32(uint32_t  v) { return v; }
75static inline uint64_t  htole64(uint64_t  v) { return v; }
76static inline uint16_t  le16toh(uint16_t  v) { return v; }
77static inline uint32_t  le32toh(uint32_t  v) { return v; }
78static inline uint64_t  le64toh(uint64_t  v) { return v; }
79#elif defined(__BIG_ENDIAN__)
80#ifdef __PPC__
81#include <libcpu/byteorder.h>
82
83/* Different RTEMS versions use different types
84 * for 32-bit (unsigned vs. unsigned long which
85 * always cause gcc warnings and possible alias
86 * violations, sigh).
87 */
88
89#if ISMINVERSION(4,8,0)
90typedef uint32_t rtemscompat_32_t;
91#else
92typedef unsigned rtemscompat_32_t;
93#endif
94
95static inline uint16_t
96htole16(uint16_t v)
97{
98uint16_t rval;
99        st_le16(&rval,v);
100        return rval;
101}
102
103static inline uint16_t
104le16toh(uint16_t v)
105{
106        return ld_le16((unsigned short*)&v);
107}
108
109static inline uint32_t
110htole32(uint32_t v)
111{
112rtemscompat_32_t rval;
113        st_le32(&rval,v);
114        return rval;
115}
116
117static inline uint32_t
118le32toh(uint32_t v)
119{
120rtemscompat_32_t vv = v;
121        return ld_le32(&vv);
122}
123
124/* Compiler generated floating point instructions for this
125 * and rtems_bsdnet_newproc()-generated tasks are non-FP
126 * :-(
127 */
128static inline uint64_t
129htole64(uint64_t  v)
130{
131union {
132        rtemscompat_32_t tmp[2];
133        uint64_t          rval;
134} u;
135
136        st_le32( &u.tmp[0], (unsigned)(v&0xffffffff) );
137        st_le32( &u.tmp[1], (unsigned)((v>>32)&0xffffffff) );
138
139        return u.rval;
140}
141
142#else
143#error "need htoleXX() implementation for this CPU arch"
144#endif
145
146#else
147#error "Unknown CPU endianness"
148#endif
149
150
151
152#ifdef __PPC__
153#define _out_byte(a,v) out_8((volatile uint8_t*)(a),(v))
154#define _inp_byte(a)   in_8((volatile uint8_t*)(a))
155#ifdef NET_CHIP_LE
156#define _out_word(a,v) out_le16((volatile uint16_t*)(a),(v))
157#define _out_long(a,v) out_le32((volatile uint32_t *)(a),(v))
158#define _inp_word(a)   in_le16((volatile uint16_t*)(a))
159#define _inp_long(a)   in_le32((volatile uint32_t *)(a))
160#elif defined(NET_CHIP_BE)
161#define _out_word(a,v) out_be16((volatile uint16_t*)(a),(v))
162#define _out_long(a,v) out_be32((volatile uint32_t *)(a),(v))
163#define _inp_word(a)   in_be16((volatile uint16_t*)(a))
164#define _inp_long(a)   in_be32((volatile uint32_t *)(a))
165#else
166#error rtemscompat_defs.h must define either NET_CHIP_LE or NET_CHIP_BE
167#endif
168static inline void wrle32(unsigned *a, unsigned v)
169{
170        asm volatile("stwbrx %1,0,%2":"=m"(*a):"r"(v),"r"(a));
171}
172static inline unsigned rdle32(unsigned *a)
173{
174        asm volatile("lwbrx %0,0,%0":"=r"(a):"0"(a),"m"(*a));
175        return (unsigned)a;
176}
177static inline void orle32(unsigned *a,unsigned v) { wrle32(a, rdle32(a) | v); }
178static inline void anle32(unsigned *a,unsigned v) { wrle32(a, rdle32(a) & v); }
179
180static inline void wrle16(unsigned short *a, unsigned short v)
181{
182        asm volatile("sthbrx %1,0,%2":"=m"(*a):"r"(v),"r"(a));
183}
184static inline unsigned short rdle16(unsigned short *a)
185{
186        asm volatile("lhbrx %0,0,%0":"=r"(a):"0"(a),"m"(*a));
187        return (unsigned short)(unsigned)a;
188}
189static inline void orle16(unsigned short *a,unsigned short v) { wrle16(a, rdle16(a) | v); }
190static inline void anle16(unsigned short *a,unsigned short v) { wrle16(a, rdle16(a) & v); }
191#endif
192
193#ifdef __i386__
194#ifdef NET_CHIP_BE
195#error dunno how to output BE data
196#endif
197
198static inline void wrle32(volatile unsigned *p, unsigned v) { *p  = v; }
199static inline void orle32(volatile unsigned *p, unsigned v) { *p |= v; }
200static inline void anle32(volatile unsigned *p, unsigned v) { *p &= v; }
201static inline unsigned rdle32(volatile unsigned *p) { return *p; }
202
203static inline void wrle16(volatile unsigned short *p, unsigned short v) { *p  = v; }
204static inline void orle16(volatile unsigned short *p, unsigned short v) { *p |= v; }
205static inline void anle16(volatile unsigned short *p, unsigned short v) { *p &= v; }
206static inline unsigned short rdle16(volatile unsigned short *p) { return *p; }
207
208#ifdef NET_CHIP_MEM_IO
209
210#ifdef __i386__
211static inline void           _out_byte(unsigned a, unsigned char v)  {        *(volatile unsigned char*)a = v; }
212static inline unsigned char  _inp_byte(unsigned a)                   { return *(volatile unsigned char*)a;     }
213#ifdef NET_CHIP_LE
214static inline void           _out_word(unsigned a, unsigned short v) {        *(volatile unsigned short*)a = v; }
215static inline unsigned short _inp_word(unsigned a)                   { return *(volatile unsigned short*)a;     }
216static inline void           _out_long(unsigned a, unsigned v)       {        *(volatile unsigned      *)a = v; }
217static inline unsigned       _inp_long(unsigned a)                   { return *(volatile unsigned      *)a; }
218#elif defined(NET_CHIP_BE)
219#error "BE memory IO not implemented for i386 yet"
220#else
221#error rtemscompat_defs.h must define either NET_CHIP_LE or NET_CHIP_BE
222#endif
223#else
224
225#error "Memory IO not implemented for this CPU architecture yet"
226
227#endif
228#elif defined(NET_CHIP_PORT_IO)
229#define _out_byte(addr,val) outport_byte((addr),(val))
230#define _out_word(addr,val) outport_word((addr),(val))
231#define _out_long(addr,val) outport_long((addr),(val))
232
233static inline u_int8_t _inp_byte(volatile unsigned char *a)
234{
235register u_int8_t rval;
236        inport_byte((unsigned short)(unsigned)a,rval);
237        return rval;
238}
239static inline u_int16_t _inp_word(volatile unsigned short *a)
240{
241register u_int16_t rval;
242        inport_word((unsigned short)(unsigned)a,rval);
243        return rval;
244}
245static inline u_int32_t _inp_long(volatile unsigned *a)
246{
247register u_int32_t rval;
248        inport_long((unsigned short)(unsigned)a,rval);
249        return rval;
250}
251#else
252#error either NET_CHIP_MEM_IO or NET_CHIP_PORT_IO must be defined
253#endif
254#endif
255
256#ifndef __FBSDID
257#define __FBSDID(arg)
258#endif
259
260#define _KERNEL
261
262#define device_printf(device,format,args...) printk(format,## args)
263
264static inline u_int8_t bus_space_do_read_1(u_long handle, unsigned reg)
265{
266        return _inp_byte((volatile unsigned char*)((handle)+(reg)));
267}
268
269static inline u_int16_t bus_space_do_read_2(u_long handle, unsigned reg)
270{
271        return _inp_word((volatile unsigned short*)((handle)+(reg)));
272}
273
274static inline u_int32_t bus_space_do_read_4(u_long handle, unsigned reg)
275{
276        return _inp_long((volatile unsigned *)((handle)+(reg)));
277}
278
279#define bus_space_read_1(tag,handle,reg) bus_space_do_read_1((handle),(reg))
280#define bus_space_read_2(tag,handle,reg) bus_space_do_read_2((handle),(reg))
281#define bus_space_read_4(tag,handle,reg) bus_space_do_read_4((handle),(reg))
282
283static inline void bus_space_do_write_multi_1(u_long handle, unsigned reg, unsigned char *addr, int cnt)
284{
285        int i; for (i=0; i<cnt; i++) _out_byte( (handle) + (reg), (addr)[i]);
286}
287
288static inline void bus_space_do_write_multi_2(u_long handle, unsigned reg, unsigned short *addr, int cnt)
289{
290        int i; for (i=0; i<cnt; i++) _out_word( (handle) + (reg), (addr)[i]);
291}
292
293static inline void bus_space_do_write_multi_4(u_long handle, unsigned reg, unsigned long *addr, int cnt)
294{
295        int i; for (i=0; i<cnt; i++) _out_long( (handle) + (reg), (addr)[i]);
296}
297
298
299#define bus_space_write_multi_1(tag, handle, reg, addr, cnt) \
300                bus_space_do_write_multi_1(handle, reg, addr, cnt)
301#define bus_space_write_multi_2(tag, handle, reg, addr, cnt) \
302                bus_space_do_write_multi_2(handle, reg, addr, cnt)
303#define bus_space_write_multi_4(tag, handle, reg, addr, cnt) \
304                bus_space_do_write_multi_4(handle, reg, addr, cnt)
305
306static inline void bus_space_do_read_multi_1(u_long handle, unsigned reg, unsigned char *addr, int cnt)
307{
308        int i; for (i=0; i<cnt; i++)
309                                (addr)[i] = _inp_byte((volatile unsigned char*)((handle)+(reg)));
310}
311
312static inline void bus_space_do_read_multi_2(u_long handle, unsigned reg, unsigned short *addr, int cnt)
313{
314        int i; for (i=0; i<cnt; i++)
315                                (addr)[i] = _inp_word((volatile unsigned short*)((handle)+(reg)));
316}
317
318static inline void bus_space_do_read_multi_4(u_long handle, unsigned reg, unsigned long *addr, int cnt)
319{
320        int i; for (i=0; i<cnt; i++)
321                                (addr)[i] = _inp_long((volatile unsigned *)((handle)+(reg)));
322}
323
324#define bus_space_read_multi_1(tag, handle, reg, addr, cnt) \
325                bus_space_do_read_multi_1(handle, reg, addr, cnt)
326#define bus_space_read_multi_2(tag, handle, reg, addr, cnt) \
327                bus_space_do_read_multi_2(handle, reg, addr, cnt)
328#define bus_space_read_multi_4(tag, handle, reg, addr, cnt) \
329                bus_space_do_read_multi_4(handle, reg, addr, cnt)
330
331
332
333#define bus_space_write_1(tag, handle, reg, val) \
334        do { _out_byte( (handle) + (reg), (val)); } while (0)
335
336#define bus_space_write_2(tag, handle, reg, val) \
337        do { _out_word( (handle) + (reg), (val)); } while (0)
338
339#define bus_space_write_4(tag, handle, reg, val) \
340        do { _out_long( (handle) + (reg), (val)); } while (0)
341
342#define BPF_MTAP(a,b)   do { } while (0)
343
344extern unsigned net_driver_ticks_per_sec;
345
346#ifdef __PPC__
347/* PPC has a timebase - based delay */
348#define DELAY(n) do {           \
349        if ( (n) > 10000 )              \
350                rtems_task_wake_after((((n)*net_driver_ticks_per_sec)/1000000) + 1);    \
351        else                                    \
352                rtems_bsp_delay(n);     \
353        } while (0)
354#else
355#warning "Have no good usec delay implementation"
356#define DELAY(n) do {           \
357                rtems_task_wake_after((((n)*net_driver_ticks_per_sec)/1000000) + 1);    \
358        } while (0)
359#endif
360
361
362#define IRQ_LOCKED(code) \
363        do { unsigned long _xtre_irq_flags; \
364                rtems_interrupt_disable(_xtre_irq_flags); \
365                        do { code } while(0);   \
366                rtems_interrupt_enable(_xtre_irq_flags); \
367        } while (0)
368
369typedef void (driver_intr_t)(void *);
370
371#define if_xname if_name
372
373/* need to replace those with LOCAL2PCI() and make sure the bus handle is initialized
374 * (on most BSPs we get away with PCI_DRAM_OFFSET [no bus handle needed at all]
375 */
376#ifndef PCI_DRAM_OFFSET
377#define PCI_DRAM_OFFSET 0
378#endif
379
380#ifndef PCI_MEM_BASE
381#define PCI_MEM_BASE    0
382#endif
383
384#define kvtop(a)                        ((unsigned long)(a) + PCI_DRAM_OFFSET)
385#define vtophys(a)                      ((unsigned long)(a) + PCI_DRAM_OFFSET)
386
387#define PCI2LOCAL(a,bus)        ((unsigned long)(a) + PCI_MEM_BASE)
388
389#ifdef PCI0_IO_BASE /* special mvme5500 hack :-( */
390#define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + PCI0_IO_BASE)
391#elif defined(PCI_IO_BASE)
392#define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + PCI_IO_BASE)
393#elif defined(_IO_BASE)
394#define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a) + _IO_BASE)
395#else
396#warning "Unable to determine base address of PCI IO space; using ZERO"
397#define PCI_IO_2LOCAL(a,bus) ((unsigned long)(a))
398#endif
399
400#define if_printf(if,fmt,args...)  printf("%s:"fmt,(if)->if_name,args)
401
402#ifndef BUS_PROBE_DEFAULT
403#define BUS_PROBE_DEFAULT 0
404#endif
405
406static inline void *
407contigmalloc(
408        unsigned long size,
409        int type,
410        int flags,
411        unsigned long lo,
412        unsigned long hi,
413        unsigned long align,
414        unsigned long bound)
415{
416void *ptr  = rtems_bsdnet_malloc(size + sizeof(ptr) + align-1, type, flags);
417char *rval = 0;
418        if ( ptr ) {
419                unsigned tmp = (unsigned)ptr + align - 1;
420                tmp -= tmp % align;
421                rval = (char*)tmp;
422                /* save backlink */
423                *(void**)(rval+size) =  ptr;
424        }
425        return rval;
426}
427
428static inline void
429contigfree(void *ptr, size_t size, int type)
430{
431        rtems_bsdnet_free( *(void**)((unsigned)ptr + size), type);
432}
433
434/* callout stuff */
435#define callout_init(args...) do {} while (0);
436#define callout_reset(args...) do {} while (0);
437#define callout_stop(args...) do {} while (0);
438
439#define IFQ_DRV_IS_EMPTY(q) (0 == (q)->ifq_head)
440#define IFQ_DRV_DEQUEUE(q,m) IF_DEQUEUE((q),(m))
441#define IFQ_DRV_PREPEND(q,m) IF_PREPEND((q),(m))
442
443#define DO_ETHER_INPUT_SKIPPING_ETHER_HEADER(ifp,m)                             \
444                {       struct ether_header *eh;                                                        \
445                        eh                                = mtod(m, struct ether_header*);      \
446                m->m_data        += sizeof(struct ether_header);        \
447                m->m_len         -= sizeof(struct ether_header);        \
448                m->m_pkthdr.len  -= sizeof(struct ether_header);        \
449                        m->m_pkthdr.rcvif = ifp;                                                        \
450                ether_input(ifp, eh, m);                                                        \
451                } while (0)
452
453
454#ifndef __KERNEL_RCSID
455#define __KERNEL_RCSID(arg...)
456#endif
457
458#endif
Note: See TracBrowser for help on using the repository browser.