source: libbsdport/bsd_eth_drivers/if_em/e1000_osdep.h @ 88f653a

Last change on this file since 88f653a was 88f653a, checked in by Till Straumann <strauman@…>, on Jul 20, 2011 at 5:25:21 PM

2011-07-20 Till Straumann <Till.Straumann@…>

  • if_em/Makefile.am, if_em/e1000_osdep.h, if_em/e1000_osdep.c, if_em/if_em.c: added e1000_osdep.c which implements all os-dependent parts in a bsdnet-agnostic fashion. Hence, the low-level driver can be used w/o bsdnet. FIXES: dependency on bsdnet initialization; PCI bus header was not initialized when not used via bsdnet (lanIpBasic driver crashed).
  • Property mode set to 100644
File size: 14.6 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/* The happy-fun DELAY macro is defined in /usr/src/sys/i386/include/clock.h */
45#define usec_delay(x) e1000_udelay(x)
46#define msec_delay(x) e1000_udelay(1000*(x))
47/* TODO: Should we be paranoid about delaying in interrupt context? */
48#define msec_delay_irq(x) msec_delay(x)
49
50void e1000_udelay(unsigned);
51
52#define MSGOUT(S, A, B)     printf(S "\n", A, B)
53#define DEBUGFUNC(F)        DEBUGOUT(F);
54        #define DEBUGOUT(S)
55        #define DEBUGOUT1(S,A)
56        #define DEBUGOUT2(S,A,B)
57        #define DEBUGOUT3(S,A,B,C)
58        #define DEBUGOUT7(S,A,B,C,D,E,F,G)
59
60
61#define STATIC                          static
62
63#ifndef FALSE
64#define FALSE               0
65#endif
66#ifndef TRUE
67#define TRUE                1
68#endif
69
70#define CMD_MEM_WRT_INVALIDATE          0x0010  /* BIT_4 */
71#define PCI_COMMAND_REGISTER            PCIR_COMMAND
72
73/*
74** These typedefs are necessary due to the new
75** shared code, they are native to Linux.
76*/
77typedef uint64_t        u64;
78typedef uint32_t        u32;
79typedef uint16_t        u16;
80typedef uint8_t         u8 ;
81typedef int64_t         s64;
82typedef int32_t         s32;
83typedef int16_t         s16;
84typedef int8_t          s8 ;
85
86typedef volatile uint32_t __uint32_va_t __attribute__((may_alias));
87typedef volatile uint16_t __uint16_va_t __attribute__((may_alias));
88
89struct e1000_pcisig {
90        uint16_t bus;
91        uint8_t  dev;
92        uint8_t  fun;
93};
94
95#ifdef NO_82542_SUPPORT
96#define E1000_REGISTER(hw, reg) reg
97#else
98#define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
99    ? reg : e1000_translate_register_82542(reg))
100#endif
101
102#define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
103
104/* Provide our own I/O so that the low-level driver API can
105 * be used independently from the BSD stuff.
106 * This is useful for people who want to use an e1000 adapter
107 * for special ethernet links that do not use BSD TCP/IP.
108 */
109#ifdef __PPC__
110
111#include <libcpu/io.h>
112
113static inline uint16_t __in_le16(uint8_t *base, uint32_t offset)
114{
115uint16_t     rval;
116    __asm__ __volatile__(
117        "lhbrx %0,%2,%1; eieio\n"
118            : "=r" (rval)
119            : "r"(base), "b"(offset), "m"(*(__uint16_va_t*)(base + offset))
120    );
121    return rval;
122}
123
124static inline void __out_le16(uint8_t *base, uint32_t offset, uint16_t val)
125{
126    __asm__ __volatile__(
127        "sthbrx %1,%3,%2; eieio"
128            : "=o"(*(__uint16_va_t*)(base+offset))
129            : "r"(val), "r"(base), "b"(offset)
130    );
131}
132
133static inline uint32_t __in_le32(uint8_t *base, uint32_t offset)
134{
135uint32_t     rval;
136    __asm__ __volatile__(
137        "lwbrx %0,%2,%1; eieio\n"
138            : "=r" (rval)
139            : "r"(base), "b"(offset), "m"(*(__uint32_va_t*)(base + offset))
140    );
141    return rval;
142}
143
144static inline void __out_le32(uint8_t *base, uint32_t offset, uint32_t val)
145{
146    __asm__ __volatile__(
147        "stwbrx %1,%3,%2; eieio"
148            : "=o"(*(__uint32_va_t*)(base+offset))
149            : "r"(val), "r"(base), "b"(offset)
150    );
151}
152
153#ifdef _IO_BASE
154static inline void __outport_dword(unsigned long base, uint32_t off, uint32_t val)
155{
156        __out_le32((uint8_t*)(_IO_BASE+base), off, val);
157}
158#else
159#error "_IO_BASE needs to be defined by BSP (bsp.h)"
160#endif
161
162#elif defined(__i386__)
163#include <libcpu/cpu.h>
164
165static inline uint16_t __in_le16(uint8_t *base, uint32_t offset)
166{
167        return *(__uint16_va_t*)(base + offset);
168}
169
170static inline void __out_le16(uint8_t *base, uint32_t offset, uint16_t val)
171{
172        *(__uint16_va_t*)(base + offset) = val;
173}
174
175static inline uint32_t __in_le32(uint8_t *base, uint32_t offset)
176{
177        return *(__uint32_va_t*)(base + offset);
178}
179
180static inline void __out_le32(uint8_t *base, uint32_t offset, uint32_t val)
181{
182        *(__uint32_va_t*)(base + offset) = val;
183}
184
185static inline void __outport_dword(unsigned long base, uint32_t off, uint32_t val)
186{
187        i386_outport_long( (base + off), val );
188}
189
190#else
191#warning "not ported to this CPU architecture yet -- using libbsdport I/O"
192#define  USE_LIBBSDPORT_IO
193#endif
194
195#if defined(USE_LIBBSDPORT_IO) && !defined(_KERNEL)
196#define _KERNEL
197#endif
198
199#ifdef   _KERNEL
200#ifndef __INSIDE_RTEMS_BSD_TCPIP_STACK__
201#define __INSIDE_RTEMS_BSD_TCPIP_STACK__
202#endif
203#include <rtems/rtems_bsdnet.h>
204#include <rtems/rtems_bsdnet_internal.h>
205
206#include <sys/types.h>
207#include <sys/param.h>
208#include <sys/systm.h>
209#include <sys/mbuf.h>
210#include <sys/protosw.h>
211#include <sys/socket.h>
212#include <sys/malloc.h>
213#include <sys/kernel.h>
214#include <sys/bus.h>
215
216#define ASSERT(x) if(!(x)) panic("EM: x")
217
218#include <devicet.h>
219
220struct e1000_osdep
221{
222        /* struct e1000_pcisig MUST be first since
223         * 'back' pointer is cast to (struct e1000_pcisig *)
224         * in e1000_osdep.c!
225         */
226        struct e1000_pcisig pcisig;
227        uint32_t mem_bus_space_handle;
228        uint32_t io_bus_space_handle;
229        uint32_t flash_bus_space_handle;
230        /* these are currently unused; present for freebsd compatibility only */
231        uint32_t mem_bus_space_tag;
232        uint32_t io_bus_space_tag;
233        uint32_t flash_bus_space_tag;
234        device_t dev;   
235};
236#endif
237
238#ifdef USE_LIBBSDPORT_IO
239
240#define USE_EXPLICIT_BUSTAGS
241
242#ifdef USE_EXPLICIT_BUSTAGS /* Help compiler by specifying explicit bus tags */
243
244/* Read from an absolute offset in the adapter's memory space */
245#define E1000_READ_OFFSET(hw, offset) \
246    bus_space_read_4(bus_space_mem, \
247    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
248
249/* Write to an absolute offset in the adapter's memory space */
250#define E1000_WRITE_OFFSET(hw, offset, value) \
251    bus_space_write_4(bus_space_mem, \
252    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
253
254/* Register READ/WRITE macros */
255
256#define E1000_READ_REG(hw, reg) \
257    bus_space_read_4(bus_space_mem, \
258        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
259        E1000_REGISTER(hw, reg))
260
261#define E1000_WRITE_REG(hw, reg, value) \
262    bus_space_write_4(bus_space_mem, \
263        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
264        E1000_REGISTER(hw, reg), value)
265
266#define E1000_READ_REG_ARRAY(hw, reg, index) \
267    bus_space_read_4(bus_space_mem, \
268        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
269        E1000_REGISTER(hw, reg) + ((index)<< 2))
270
271#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
272    bus_space_write_4(bus_space_mem, \
273        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
274        E1000_REGISTER(hw, reg) + ((index)<< 2), value)
275
276#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
277#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
278
279#define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
280    bus_space_read_1(bus_space_mem, \
281        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
282        E1000_REGISTER(hw, reg) + index)
283
284#define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
285    bus_space_write_1(bus_space_mem, \
286        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
287        E1000_REGISTER(hw, reg) + index, value)
288
289#define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
290    bus_space_write_2(bus_space_mem, \
291        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
292        E1000_REGISTER(hw, reg) + (index << 1), value)
293
294#define E1000_WRITE_REG_IO(hw, reg, value) do {\
295    bus_space_write_4(bus_space_io, \
296        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
297        (hw)->io_base, reg); \
298    bus_space_write_4(bus_space_io, \
299        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
300        (hw)->io_base + 4, value); } while (0)
301
302#define E1000_READ_FLASH_REG(hw, reg) \
303    bus_space_read_4(bus_space_mem, \
304        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
305
306#define E1000_READ_FLASH_REG16(hw, reg) \
307    bus_space_read_2(bus_space_mem, \
308        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
309
310#define E1000_WRITE_FLASH_REG(hw, reg, value) \
311    bus_space_write_4(bus_space_mem, \
312        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
313
314#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
315    bus_space_write_2(bus_space_mem, \
316        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
317
318#else /* USE_EXPLICIT_BUSTAGS */
319
320/* Read from an absolute offset in the adapter's memory space */
321#define E1000_READ_OFFSET(hw, offset) \
322    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
323    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
324
325/* Write to an absolute offset in the adapter's memory space */
326#define E1000_WRITE_OFFSET(hw, offset, 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, offset, value)
329
330/* Register READ/WRITE macros */
331
332#define E1000_READ_REG(hw, reg) \
333    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
334        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
335        E1000_REGISTER(hw, reg))
336
337#define E1000_WRITE_REG(hw, reg, value) \
338    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
339        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
340        E1000_REGISTER(hw, reg), value)
341
342#define E1000_READ_REG_ARRAY(hw, reg, index) \
343    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
344        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
345        E1000_REGISTER(hw, reg) + ((index)<< 2))
346
347#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
348    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
349        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
350        E1000_REGISTER(hw, reg) + ((index)<< 2), value)
351
352#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
353#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
354
355#define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
356    bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
357        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
358        E1000_REGISTER(hw, reg) + index)
359
360#define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
361    bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
362        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
363        E1000_REGISTER(hw, reg) + index, value)
364
365#define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
366    bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
367        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
368        E1000_REGISTER(hw, reg) + (index << 1), value)
369
370#define E1000_WRITE_REG_IO(hw, reg, value) do {\
371    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
372        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
373        (hw)->io_base, reg); \
374    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
375        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
376        (hw)->io_base + 4, value); } while (0)
377
378#define E1000_READ_FLASH_REG(hw, reg) \
379    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
380        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
381
382#define E1000_READ_FLASH_REG16(hw, reg) \
383    bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
384        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
385
386#define E1000_WRITE_FLASH_REG(hw, reg, value) \
387    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
388        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
389
390#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
391    bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
392        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
393#endif /* USE_EXPLICIT_BUSTAGS */
394
395#else /* USE_LIBBSDPORT_IO */
396
397/* Read from an absolute offset in the adapter's memory space */
398#define E1000_READ_OFFSET(hw, offset) \
399        __in_le32((hw)->hw_addr, offset)
400
401/* Write to an absolute offset in the adapter's memory space */
402#define E1000_WRITE_OFFSET(hw, offset, value) \
403        __out_le32((hw)->hw_addr, offset, value)
404
405/* Register READ/WRITE macros */
406
407#define E1000_READ_REG(hw, reg) \
408        __in_le32((hw)->hw_addr, E1000_REGISTER(hw, reg))
409
410#define E1000_WRITE_REG(hw, reg, value) \
411        __out_le32((hw)->hw_addr, E1000_REGISTER(hw, reg), value)
412
413#define E1000_READ_REG_ARRAY(hw, reg, index) \
414        __in_le32((hw)->hw_addr, E1000_REGISTER(hw, reg) + ((index)<< 2))
415
416#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
417        __out_le32((hw)->hw_addr, E1000_REGISTER(hw, reg) + ((index)<< 2), value)
418
419#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
420#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
421
422#define E1000_WRITE_REG_IO(hw, reg, value) do { \
423        __outport_dword((hw)->io_base, 0, reg);     \
424        __outport_dword((hw)->io_base, 4, value);   \
425        } while (0)
426
427#define E1000_READ_FLASH_REG(hw, reg) \
428        __in_le32( (hw)->flash_address, reg )
429
430#define E1000_READ_FLASH_REG16(hw, reg) \
431        __in_le16( (hw)->flash_address, reg )
432
433#define E1000_WRITE_FLASH_REG(hw, reg, value) \
434        __out_le32( (hw)->flash_address, reg, value )
435
436#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
437        __out_le16( (hw)->flash_address, reg, value )
438
439#endif /* USE_LIBBSDPORT_IO */
440
441
442#endif  /* _FREEBSD_OS_H_ */
443
Note: See TracBrowser for help on using the repository browser.