1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Michalec
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef RTL8139_DRIVER_H_
|
---|
30 | #define RTL8139_DRIVER_H_
|
---|
31 |
|
---|
32 | #include <stddef.h>
|
---|
33 | #include <stdint.h>
|
---|
34 | #include "defs.h"
|
---|
35 | #include "general.h"
|
---|
36 |
|
---|
37 | /** The driver name */
|
---|
38 | #define NAME "rtl8139"
|
---|
39 |
|
---|
40 | /** Transmittion buffers count */
|
---|
41 | #define TX_BUFF_COUNT 4
|
---|
42 |
|
---|
43 | /** Size of buffer for one frame (2kB) */
|
---|
44 | #define TX_BUFF_SIZE (2 * 1024)
|
---|
45 |
|
---|
46 | /** Number of pages to allocate for TxBuffers */
|
---|
47 | #define TX_PAGES 2
|
---|
48 |
|
---|
49 | /** Size of the CRC after the received frame in the receiver buffer */
|
---|
50 | #define RTL8139_CRC_SIZE 4
|
---|
51 |
|
---|
52 | /** The default mode of accepting unicast frames */
|
---|
53 | #define RTL8139_RCR_UCAST_DEFAULT RCR_ACCEPT_PHYS_MATCH
|
---|
54 |
|
---|
55 | /** The default mode of accepting multicast frames */
|
---|
56 | #define RTL8139_RCR_MCAST_DEFAULT 0
|
---|
57 |
|
---|
58 | /** The default mode of accepting broadcast frames */
|
---|
59 | #define RTL8139_RCR_BCAST_DEFAULT RCR_ACCEPT_BROADCAST
|
---|
60 |
|
---|
61 | /** The default mode of accepting defect frames */
|
---|
62 | #define RTL8139_RCR_DEFECT_DEFAULT 0
|
---|
63 |
|
---|
64 | /** Mask for accepting all multicast */
|
---|
65 | #define RTL8139_MCAST_MASK_PROMISC UINT64_MAX
|
---|
66 |
|
---|
67 | /** Data */
|
---|
68 | struct rtl8139_rcr_data {
|
---|
69 | /** Configuration part of RCR */
|
---|
70 | uint32_t rcr_base;
|
---|
71 | /** Mask of unicast */
|
---|
72 | uint8_t ucast_mask;
|
---|
73 | /** Mask of multicast */
|
---|
74 | uint8_t mcast_mask;
|
---|
75 | /** Mask of broadcast */
|
---|
76 | uint8_t bcast_mask;
|
---|
77 | /** Mask of defective */
|
---|
78 | uint8_t defect_mask;
|
---|
79 | };
|
---|
80 |
|
---|
81 | /** Power management related data */
|
---|
82 | typedef struct rtl8139_pm {
|
---|
83 | /** Count of used activities which needs PMEn bit set */
|
---|
84 | int active;
|
---|
85 | } rtl8139_pm_t;
|
---|
86 |
|
---|
87 | /** RTL8139 device data */
|
---|
88 | typedef struct rtl8139_data {
|
---|
89 | /** DDF device */
|
---|
90 | ddf_dev_t *dev;
|
---|
91 | /** Parent session */
|
---|
92 | async_sess_t *parent_sess;
|
---|
93 | /** I/O address of the device */
|
---|
94 | void *io_addr;
|
---|
95 | /** Mapped I/O port */
|
---|
96 | void *io_port;
|
---|
97 | /** The irq assigned */
|
---|
98 | int irq;
|
---|
99 |
|
---|
100 | /** Mask of the turned interupts (IMR value) */
|
---|
101 | uint16_t int_mask;
|
---|
102 |
|
---|
103 | /** The memory allocated for the transmittion buffers
|
---|
104 | * Each buffer takes 2kB
|
---|
105 | */
|
---|
106 | uintptr_t tx_buff_phys;
|
---|
107 | void *tx_buff_virt;
|
---|
108 |
|
---|
109 | /** Virtual addresses of the Tx buffers */
|
---|
110 | void *tx_buff[TX_BUFF_COUNT];
|
---|
111 |
|
---|
112 | /** The nubmer of the next buffer to use, index = tx_next % TX_BUFF_COUNT */
|
---|
113 | size_t tx_next;
|
---|
114 | /** The number of the first used buffer in the row
|
---|
115 | *
|
---|
116 | * tx_used is in the interval tx_next - TX_BUFF_COUNT and tx_next:
|
---|
117 | * tx_next - TX_BUFF_COUNT: there is no useable Tx descriptor
|
---|
118 | * tx_next: all Tx descriptors are can be used
|
---|
119 | */
|
---|
120 | size_t tx_used;
|
---|
121 |
|
---|
122 | /** Buffer for receiving frames */
|
---|
123 | uintptr_t rx_buff_phys;
|
---|
124 | void *rx_buff_virt;
|
---|
125 |
|
---|
126 | /** Receiver control register data */
|
---|
127 | struct rtl8139_rcr_data rcr_data;
|
---|
128 |
|
---|
129 | /** Power management information */
|
---|
130 | rtl8139_pm_t pm;
|
---|
131 |
|
---|
132 | /** Lock for receiver */
|
---|
133 | fibril_mutex_t rx_lock;
|
---|
134 | /** Lock for transmitter */
|
---|
135 | fibril_mutex_t tx_lock;
|
---|
136 |
|
---|
137 | /** Polling mode information */
|
---|
138 | rtl8139_timer_act_t poll_timer;
|
---|
139 |
|
---|
140 | /** Backward pointer to nic_data */
|
---|
141 | nic_t *nic_data;
|
---|
142 |
|
---|
143 | /** Version of RT8139 controller */
|
---|
144 | rtl8139_version_id_t hw_version;
|
---|
145 | } rtl8139_t;
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Pointers casting - for both amd64 and ia32
|
---|
149 | */
|
---|
150 |
|
---|
151 | /** Cast pointer to uint32_t
|
---|
152 | *
|
---|
153 | * @param ptr The pointer to cast
|
---|
154 | * @return The uint32_t pointer representation. The low 32 bit is taken
|
---|
155 | * in the case of the 64 bit pointers
|
---|
156 | */
|
---|
157 | #define PTR2U32(ptr) ((uint32_t)((size_t)(ptr)))
|
---|
158 |
|
---|
159 | /** Check if the pointer can be cast to uint32_t without the data lost
|
---|
160 | *
|
---|
161 | * @param ptr The pointer to check
|
---|
162 | * @return The true value if the pointer can be cast, false if not
|
---|
163 | */
|
---|
164 | #define PTR_IS_32(ptr) ((size_t)PTR2U32(ptr) == (size_t)(ptr))
|
---|
165 |
|
---|
166 | /** Cast the ioaddr part to the void*
|
---|
167 | *
|
---|
168 | * @param ioaddr The ioaddr value
|
---|
169 | */
|
---|
170 | #define IOADDR_TO_PTR(ioaddr) ((void*)((size_t)(ioaddr)))
|
---|
171 |
|
---|
172 | /* ***** Bit operation macros ***** */
|
---|
173 |
|
---|
174 | /** Set the bits specified by the given bit mask to the different values
|
---|
175 | *
|
---|
176 | * The bit from the src is used if the corresponding bit in the mask is 0,
|
---|
177 | * the bit from the value is used if the corresponding bit in the mask is 1
|
---|
178 | *
|
---|
179 | * @param src The original value
|
---|
180 | * @param value The new bit value
|
---|
181 | * @param mask The mask to specify modified bits
|
---|
182 | * @param type The values type
|
---|
183 | *
|
---|
184 | * @return New value
|
---|
185 | */
|
---|
186 | #define bit_set_part_g(src, value, mask, type) \
|
---|
187 | ((type)(((src) & ~((type)(mask))) | ((value) & (type)(mask))))
|
---|
188 |
|
---|
189 | /** Set the bits specified by the given bit mask to the different values
|
---|
190 | *
|
---|
191 | * The version of the uint32_t
|
---|
192 | *
|
---|
193 | * @see bit_set_part_g
|
---|
194 | */
|
---|
195 |
|
---|
196 | #define bit_set_part_32(src, value, mask) bit_set_part_g(src, value, mask, uint32_t)
|
---|
197 | /** Set the bits specified by the given bit mask to the different values
|
---|
198 | *
|
---|
199 | * The version of the uint16_t
|
---|
200 | *
|
---|
201 | * @see bit_set_part_g
|
---|
202 | */
|
---|
203 |
|
---|
204 | #define bit_set_part_16(src, value, mask) bit_set_part_g(src, value, mask, uint16_t)
|
---|
205 |
|
---|
206 | /** Set the bits specified by the given bit mask to the different values
|
---|
207 | *
|
---|
208 | * The version of the uint8_t
|
---|
209 | *
|
---|
210 | * @see bit_set_part_g
|
---|
211 | */
|
---|
212 | #define bit_set_part_8(src, value, mask) bit_set_part_g(src, value, mask, uint8_t)
|
---|
213 |
|
---|
214 | /** Clear specified bits in the value
|
---|
215 | *
|
---|
216 | * @param src Original value
|
---|
217 | * @param clear_mask Bits to clear mask
|
---|
218 | * @param type The values type
|
---|
219 | */
|
---|
220 | #define bit_clear_g(src, clear_mask, type) ((type)((src) & ~((type)(clear_mask))))
|
---|
221 |
|
---|
222 | /** Clear specified bits in the value, 32bit version
|
---|
223 | *
|
---|
224 | * @see bit_clear_g
|
---|
225 | */
|
---|
226 | #define bit_clear_32(src, clear_mask) bit_clear_g(src, clear_mask, uint32_t)
|
---|
227 | /** Clear specified bits in the value, 16bit version
|
---|
228 | *
|
---|
229 | * @see bit_clear_g
|
---|
230 | */
|
---|
231 | #define bit_clear_16(src, clear_mask) bit_clear_g(src, clear_mask, uint16_t)
|
---|
232 | /** Clear specified bits in the value, 8bit version
|
---|
233 | *
|
---|
234 | * @see bit_clear_g
|
---|
235 | */
|
---|
236 | #define bit_clear_8(src, clear_mask) bit_clear_g(src, clear_mask, uint8_t)
|
---|
237 |
|
---|
238 | /** Obtain value of the TSD register with size part modified
|
---|
239 | *
|
---|
240 | * @param tsd_value Old value of the TSD
|
---|
241 | * @param size The size to set
|
---|
242 | */
|
---|
243 | #define rtl8139_tsd_set_size(tsd_value, size) \
|
---|
244 | bit_set_part_32(tsd_value, (size) << TSD_SIZE_SHIFT, TSD_SIZE_MASK << TSD_SIZE_SHIFT)
|
---|
245 |
|
---|
246 | #endif
|
---|