source: libbsdport/bsd_eth_drivers/if_em/e1000_osdep.h @ 370f3c5

Last change on this file since 370f3c5 was 370f3c5, checked in by Till Straumann <strauman@…>, on Aug 5, 2009 at 9:41:20 PM

2009-08-05 Till Straumann <Till.Straumann@…>

  • if_em/Makefile.am, if_em/e1000_osdep.h, if_em/if_em.c: Changed 'e1000_osdep.h' to provide stdalone I/O methods for select architectures (x86 and PPC -- others fall back on libbsdport/bus.h). This lets the low-level driver (everything except for 'if_em.c/if_em.h') API be independent of BSD networking and libbsdport which is desirable since certain applications may wish to just use the low-level API for implementing dedicated 'raw-ethernet' drivers for BSD-independent, proprietary GigE communication.
  • Property mode set to 100644
File size: 14.2 KB
Line 
1/**************************************************************************
2
3Copyright (c) 2001-2007, Intel Corporation
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice,
10    this list of conditions and the following disclaimer.
11
12 2. Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15
16 3. Neither the name of the Intel Corporation nor the names of its
17    contributors may be used to endorse or promote products derived from
18    this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30POSSIBILITY OF SUCH DAMAGE.
31
32***************************************************************************/
33/*$FreeBSD: src/sys/dev/em/e1000_osdep.h,v 1.3 2007/05/16 00:14:23 jfv Exp $*/
34
35
36#ifndef _FREEBSD_OS_H_
37#define _FREEBSD_OS_H_
38
39#include <rtems.h>
40#include <bsp.h>
41#include <rtems/pci.h>
42#include <vm/vm.h> /* for non-_KERNEL boolean_t :-( */
43
44#ifdef   _KERNEL
45#include <rtems/rtems_bsdnet_internal.h>
46
47#include <sys/types.h>
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/mbuf.h>
51#include <sys/protosw.h>
52#include <sys/socket.h>
53#include <sys/malloc.h>
54#include <sys/kernel.h>
55#include <sys/bus.h>
56
57#define ASSERT(x) if(!(x)) panic("EM: x")
58
59/* The happy-fun DELAY macro is defined in /usr/src/sys/i386/include/clock.h */
60#define usec_delay(x) DELAY(x)
61#define msec_delay(x) DELAY(1000*(x))
62/* TODO: Should we be paranoid about delaying in interrupt context? */
63#define msec_delay_irq(x) DELAY(1000*(x))
64#include <rtems_udelay.h>
65
66#define MSGOUT(S, A, B)     printf(S "\n", A, B)
67#define DEBUGFUNC(F)        DEBUGOUT(F);
68        #define DEBUGOUT(S)
69        #define DEBUGOUT1(S,A)
70        #define DEBUGOUT2(S,A,B)
71        #define DEBUGOUT3(S,A,B,C)
72        #define DEBUGOUT7(S,A,B,C,D,E,F,G)
73
74#include <devicet.h>
75
76struct e1000_osdep
77{
78        uint32_t mem_bus_space_handle;
79        uint32_t io_bus_space_handle;
80        uint32_t flash_bus_space_handle;
81        /* these are currently unused; present for freebsd compatibility only */
82        uint32_t mem_bus_space_tag;
83        uint32_t io_bus_space_tag;
84        uint32_t flash_bus_space_tag;
85        device_t dev;   
86};
87
88#define STATIC                          static
89#endif
90
91#ifndef FALSE
92#define FALSE               0
93#endif
94#ifndef TRUE
95#define TRUE                1
96#endif
97
98#define CMD_MEM_WRT_INVALIDATE          0x0010  /* BIT_4 */
99#define PCI_COMMAND_REGISTER            PCIR_COMMAND
100
101/*
102** These typedefs are necessary due to the new
103** shared code, they are native to Linux.
104*/
105typedef uint64_t        u64;
106typedef uint32_t        u32;
107typedef uint16_t        u16;
108typedef uint8_t         u8 ;
109typedef int64_t         s64;
110typedef int32_t         s32;
111typedef int16_t         s16;
112typedef int8_t          s8 ;
113
114typedef volatile uint32_t __uint32_va_t __attribute__((may_alias));
115typedef volatile uint16_t __uint16_va_t __attribute__((may_alias));
116
117#ifdef NO_82542_SUPPORT
118#define E1000_REGISTER(hw, reg) reg
119#else
120#define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
121    ? reg : e1000_translate_register_82542(reg))
122#endif
123
124#define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
125
126/* Provide our own I/O so that the low-level driver API can
127 * be used independently from the BSD stuff.
128 * This is useful for people who want to use an e1000 adapter
129 * for special ethernet links that do not use BSD TCP/IP.
130 */
131#ifdef __PPC__
132
133#include <libcpu/io.h>
134
135static inline uint16_t __in_le16(uint8_t *base, uint32_t offset)
136{
137uint16_t     rval;
138    __asm__ __volatile__(
139        "lhbrx %0,%2,%1; eieio\n"
140            : "=r" (rval)
141            : "r"(base), "b"(offset), "m"(*(__uint16_va_t*)(base + offset))
142    );
143    return rval;
144}
145
146static inline __out_le16(uint8_t *base, uint32_t offset, uint16_t val)
147{
148    __asm__ __volatile__(
149        "sthbrx %1,%3,%2; eieio"
150            : "=o"(*(__uint16_va_t*)(base+offset))
151            : "r"(val), "r"(base), "b"(offset)
152    );
153}
154
155static inline uint32_t __in_le32(uint8_t *base, uint32_t offset)
156{
157uint32_t     rval;
158    __asm__ __volatile__(
159        "lwbrx %0,%2,%1; eieio\n"
160            : "=r" (rval)
161            : "r"(base), "b"(offset), "m"(*(__uint32_va_t*)(base + offset))
162    );
163    return rval;
164}
165
166static inline __out_le32(uint8_t *base, uint32_t offset, uint32_t val)
167{
168    __asm__ __volatile__(
169        "stwbrx %1,%3,%2; eieio"
170            : "=o"(*(__uint32_va_t*)(base+offset))
171            : "r"(val), "r"(base), "b"(offset)
172    );
173}
174
175#ifdef _IO_BASE
176static inline void __outport_dword(unsigned long base, uint32_t off, uint32_t val)
177{
178        __out_le32((uint8_t*)(_IO_BASE+base), off, val);
179}
180#else
181#error "_IO_BASE needs to be defined by BSP (bsp.h)"
182#endif
183
184#elif defined(__i386__)
185#include <libcpu/cpu.h>
186
187static inline uint16_t __in_le16(uint8_t *base, uint32_t offset)
188{
189        return *(__uint16_va_t*)(base + offset);
190}
191
192static inline void __out_le16(uint8_t *base, uint32_t offset, uint16_t val)
193{
194        *(__uint16_va_t*)(base + offset) = val;
195}
196
197static inline uint32_t __in_le32(uint8_t *base, uint32_t offset)
198{
199        return *(__uint32_va_t*)(base + offset);
200}
201
202static inline void __out_le32(uint8_t *base, uint32_t offset, uint32_t val)
203{
204        *(__uint32_va_t*)(base + offset) = val;
205}
206
207static inline void __outport_dword(unsigned long base, uint32_t off, uint32_t val)
208{
209        i386_outport_long( (base + off), val );
210}
211
212#else
213#warning "not ported to this CPU architecture yet -- using libbsdport I/O"
214#define  USE_LIBBSDPORT_IO
215#endif
216
217#ifdef USE_LIBBSDPORT_IO
218
219#define USE_EXPLICIT_BUSTAGS
220
221#ifdef USE_EXPLICIT_BUSTAGS /* Help compiler by specifying explicit bus tags */
222
223/* Read from an absolute offset in the adapter's memory space */
224#define E1000_READ_OFFSET(hw, offset) \
225    bus_space_read_4(bus_space_mem, \
226    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
227
228/* Write to an absolute offset in the adapter's memory space */
229#define E1000_WRITE_OFFSET(hw, offset, value) \
230    bus_space_write_4(bus_space_mem, \
231    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
232
233/* Register READ/WRITE macros */
234
235#define E1000_READ_REG(hw, reg) \
236    bus_space_read_4(bus_space_mem, \
237        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
238        E1000_REGISTER(hw, reg))
239
240#define E1000_WRITE_REG(hw, reg, value) \
241    bus_space_write_4(bus_space_mem, \
242        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
243        E1000_REGISTER(hw, reg), value)
244
245#define E1000_READ_REG_ARRAY(hw, reg, index) \
246    bus_space_read_4(bus_space_mem, \
247        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
248        E1000_REGISTER(hw, reg) + ((index)<< 2))
249
250#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
251    bus_space_write_4(bus_space_mem, \
252        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
253        E1000_REGISTER(hw, reg) + ((index)<< 2), value)
254
255#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
256#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
257
258#define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
259    bus_space_read_1(bus_space_mem, \
260        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
261        E1000_REGISTER(hw, reg) + index)
262
263#define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
264    bus_space_write_1(bus_space_mem, \
265        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
266        E1000_REGISTER(hw, reg) + index, value)
267
268#define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
269    bus_space_write_2(bus_space_mem, \
270        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
271        E1000_REGISTER(hw, reg) + (index << 1), value)
272
273#define E1000_WRITE_REG_IO(hw, reg, value) do {\
274    bus_space_write_4(bus_space_io, \
275        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
276        (hw)->io_base, reg); \
277    bus_space_write_4(bus_space_io, \
278        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
279        (hw)->io_base + 4, value); } while (0)
280
281#define E1000_READ_FLASH_REG(hw, reg) \
282    bus_space_read_4(bus_space_mem, \
283        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
284
285#define E1000_READ_FLASH_REG16(hw, reg) \
286    bus_space_read_2(bus_space_mem, \
287        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
288
289#define E1000_WRITE_FLASH_REG(hw, reg, value) \
290    bus_space_write_4(bus_space_mem, \
291        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
292
293#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
294    bus_space_write_2(bus_space_mem, \
295        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
296
297#else /* USE_EXPLICIT_BUSTAGS */
298
299/* Read from an absolute offset in the adapter's memory space */
300#define E1000_READ_OFFSET(hw, offset) \
301    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
302    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
303
304/* Write to an absolute offset in the adapter's memory space */
305#define E1000_WRITE_OFFSET(hw, offset, value) \
306    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
307    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
308
309/* Register READ/WRITE macros */
310
311#define E1000_READ_REG(hw, reg) \
312    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
313        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
314        E1000_REGISTER(hw, reg))
315
316#define E1000_WRITE_REG(hw, reg, value) \
317    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
318        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
319        E1000_REGISTER(hw, reg), value)
320
321#define E1000_READ_REG_ARRAY(hw, reg, index) \
322    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
323        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
324        E1000_REGISTER(hw, reg) + ((index)<< 2))
325
326#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
327    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
328        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
329        E1000_REGISTER(hw, reg) + ((index)<< 2), value)
330
331#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
332#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
333
334#define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
335    bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
336        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
337        E1000_REGISTER(hw, reg) + index)
338
339#define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
340    bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
341        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
342        E1000_REGISTER(hw, reg) + index, value)
343
344#define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
345    bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
346        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
347        E1000_REGISTER(hw, reg) + (index << 1), value)
348
349#define E1000_WRITE_REG_IO(hw, reg, value) do {\
350    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
351        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
352        (hw)->io_base, reg); \
353    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
354        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
355        (hw)->io_base + 4, value); } while (0)
356
357#define E1000_READ_FLASH_REG(hw, reg) \
358    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
359        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
360
361#define E1000_READ_FLASH_REG16(hw, reg) \
362    bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
363        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
364
365#define E1000_WRITE_FLASH_REG(hw, reg, value) \
366    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
367        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
368
369#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
370    bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
371        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
372#endif /* USE_EXPLICIT_BUSTAGS */
373
374#else /* USE_LIBBSDPORT_IO */
375
376/* Read from an absolute offset in the adapter's memory space */
377#define E1000_READ_OFFSET(hw, offset) \
378        __in_le32((hw)->hw_addr, offset)
379
380/* Write to an absolute offset in the adapter's memory space */
381#define E1000_WRITE_OFFSET(hw, offset, value) \
382        __out_le32((hw)->hw_addr, offset, value)
383
384/* Register READ/WRITE macros */
385
386#define E1000_READ_REG(hw, reg) \
387        __in_le32((hw)->hw_addr, E1000_REGISTER(hw, reg))
388
389#define E1000_WRITE_REG(hw, reg, value) \
390        __out_le32((hw)->hw_addr, E1000_REGISTER(hw, reg), value)
391
392#define E1000_READ_REG_ARRAY(hw, reg, index) \
393        __in_le32((hw)->hw_addr, E1000_REGISTER(hw, reg) + ((index)<< 2))
394
395#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
396        __out_le32((hw)->hw_addr, E1000_REGISTER(hw, reg) + ((index)<< 2), value)
397
398#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
399#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
400
401#define E1000_WRITE_REG_IO(hw, reg, value) do { \
402        __outport_dword((hw)->io_base, 0, reg);     \
403        __outport_dword((hw)->io_base, 4, value);   \
404        } while (0)
405
406#define E1000_READ_FLASH_REG(hw, reg) \
407        __in_le32( (hw)->flash_address, reg )
408
409#define E1000_READ_FLASH_REG16(hw, reg) \
410        __in_le16( (hw)->flash_address, reg )
411
412#define E1000_WRITE_FLASH_REG(hw, reg, value) \
413        __out_le32( (hw)->flash_address, reg, value )
414
415#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
416        __out_le16( (hw)->flash_address, reg, value )
417
418#endif /* USE_LIBBSDPORT_IO */
419
420
421#endif  /* _FREEBSD_OS_H_ */
422
Note: See TracBrowser for help on using the repository browser.