rtl81x9var.h revision 1.56 1 1.56 jmcneill /* $NetBSD: rtl81x9var.h,v 1.56 2017/04/19 00:20:02 jmcneill Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*
4 1.1 tsutsui * Copyright (c) 1997, 1998
5 1.1 tsutsui * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
8 1.1 tsutsui * modification, are permitted provided that the following conditions
9 1.1 tsutsui * are met:
10 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
11 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
12 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
14 1.1 tsutsui * documentation and/or other materials provided with the distribution.
15 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software
16 1.1 tsutsui * must display the following acknowledgement:
17 1.1 tsutsui * This product includes software developed by Bill Paul.
18 1.1 tsutsui * 4. Neither the name of the author nor the names of any co-contributors
19 1.1 tsutsui * may be used to endorse or promote products derived from this software
20 1.1 tsutsui * without specific prior written permission.
21 1.1 tsutsui *
22 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 1.1 tsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 tsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 tsutsui * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 tsutsui * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 tsutsui *
34 1.1 tsutsui * FreeBSD Id: if_rlreg.h,v 1.9 1999/06/20 18:56:09 wpaul Exp
35 1.1 tsutsui */
36 1.1 tsutsui
37 1.55 riastrad #include <sys/rndsource.h>
38 1.16 dan
39 1.32 tsutsui #define RTK_ETHER_ALIGN 2
40 1.32 tsutsui #define RTK_RXSTAT_LEN 4
41 1.32 tsutsui
42 1.22 tsutsui #ifdef __NO_STRICT_ALIGNMENT
43 1.32 tsutsui /*
44 1.32 tsutsui * XXX According to PR kern/33763, some 8168 and variants can't DMA
45 1.32 tsutsui * XXX RX packet data into unaligned buffer. This means such chips will
46 1.32 tsutsui * XXX never work on !__NO_STRICT_ALIGNMENT hosts without copying buffer.
47 1.32 tsutsui */
48 1.32 tsutsui #define RE_ETHER_ALIGN 0
49 1.21 christos #else
50 1.32 tsutsui #define RE_ETHER_ALIGN 2
51 1.21 christos #endif
52 1.1 tsutsui
53 1.4 thorpej struct rtk_type {
54 1.24 tsutsui uint16_t rtk_vid;
55 1.24 tsutsui uint16_t rtk_did;
56 1.11 fvdl int rtk_basetype;
57 1.30 tsutsui #define RTK_8129 1
58 1.30 tsutsui #define RTK_8139 2
59 1.30 tsutsui #define RTK_8139CPLUS 3
60 1.30 tsutsui #define RTK_8169 4
61 1.39 tsutsui #define RTK_8168 5
62 1.39 tsutsui #define RTK_8101E 6
63 1.39 tsutsui const char *rtk_name;
64 1.39 tsutsui };
65 1.30 tsutsui
66 1.4 thorpej struct rtk_mii_frame {
67 1.24 tsutsui uint8_t mii_stdelim;
68 1.24 tsutsui uint8_t mii_opcode;
69 1.24 tsutsui uint8_t mii_phyaddr;
70 1.24 tsutsui uint8_t mii_regaddr;
71 1.24 tsutsui uint8_t mii_turnaround;
72 1.24 tsutsui uint16_t mii_data;
73 1.1 tsutsui };
74 1.1 tsutsui
75 1.1 tsutsui /*
76 1.1 tsutsui * MII constants
77 1.1 tsutsui */
78 1.5 tsutsui #define RTK_MII_STARTDELIM 0x01
79 1.5 tsutsui #define RTK_MII_READOP 0x02
80 1.5 tsutsui #define RTK_MII_WRITEOP 0x01
81 1.5 tsutsui #define RTK_MII_TURNAROUND 0x02
82 1.1 tsutsui
83 1.11 fvdl
84 1.30 tsutsui /*
85 1.30 tsutsui * The RealTek doesn't use a fragment-based descriptor mechanism.
86 1.30 tsutsui * Instead, there are only four register sets, each or which represents
87 1.30 tsutsui * one 'descriptor.' Basically, each TX descriptor is just a contiguous
88 1.30 tsutsui * packet buffer (32-bit aligned!) and we place the buffer addresses in
89 1.49 tsutsui * the registers so the chip knows where they are.
90 1.49 tsutsui *
91 1.30 tsutsui * We can sort of kludge together the same kind of buffer management
92 1.30 tsutsui * used in previous drivers, but we have to do buffer copies almost all
93 1.30 tsutsui * the time, so it doesn't really buy us much.
94 1.49 tsutsui *
95 1.30 tsutsui * For reception, there's just one large buffer where the chip stores
96 1.30 tsutsui * all received packets.
97 1.30 tsutsui */
98 1.1 tsutsui
99 1.30 tsutsui #ifdef dreamcast
100 1.32 tsutsui /*
101 1.32 tsutsui * XXX dreamcast has only 32KB DMA'able memory on its PCI bridge.
102 1.32 tsutsui * XXX Maybe this should be handled by prop_dictionary, or
103 1.32 tsutsui * XXX some other new API which returns available DMA resources.
104 1.32 tsutsui */
105 1.30 tsutsui #define RTK_RX_BUF_SZ RTK_RXBUF_16
106 1.30 tsutsui #else
107 1.30 tsutsui #define RTK_RX_BUF_SZ RTK_RXBUF_64
108 1.30 tsutsui #endif
109 1.30 tsutsui #define RTK_RXBUFLEN RTK_RXBUF_LEN(RTK_RX_BUF_SZ)
110 1.30 tsutsui #define RTK_TX_LIST_CNT 4
111 1.18 yamt
112 1.12 toshii /*
113 1.30 tsutsui * The 8139C+ and 8169 gigE chips support descriptor-based TX
114 1.12 toshii * and RX. In fact, they even support TCP large send. Descriptors
115 1.12 toshii * must be allocated in contiguous blocks that are aligned on a
116 1.30 tsutsui * 256-byte boundary. The RX rings can hold a maximum of 64 descriptors.
117 1.30 tsutsui * The TX rings can hold upto 64 descriptors on 8139C+, and
118 1.30 tsutsui * 1024 descriptors on 8169 gigE chips.
119 1.12 toshii */
120 1.32 tsutsui #define RE_RING_ALIGN 256
121 1.30 tsutsui
122 1.36 tsutsui /*
123 1.36 tsutsui * Size of descriptors and TX queue.
124 1.36 tsutsui * These numbers must be power of two to simplify RE_NEXT_*() macro.
125 1.36 tsutsui */
126 1.32 tsutsui #define RE_RX_DESC_CNT 64
127 1.32 tsutsui #define RE_TX_DESC_CNT_8139 64
128 1.32 tsutsui #define RE_TX_DESC_CNT_8169 1024
129 1.32 tsutsui #define RE_TX_QLEN 64
130 1.12 toshii
131 1.34 tsutsui #define RE_NTXDESC_RSVD 4
132 1.34 tsutsui
133 1.32 tsutsui struct re_rxsoft {
134 1.28 tsutsui struct mbuf *rxs_mbuf;
135 1.28 tsutsui bus_dmamap_t rxs_dmamap;
136 1.28 tsutsui };
137 1.28 tsutsui
138 1.33 tsutsui struct re_txq {
139 1.33 tsutsui struct mbuf *txq_mbuf;
140 1.33 tsutsui bus_dmamap_t txq_dmamap;
141 1.33 tsutsui int txq_descidx;
142 1.35 tsutsui int txq_nsegs;
143 1.33 tsutsui };
144 1.33 tsutsui
145 1.32 tsutsui struct re_list_data {
146 1.33 tsutsui struct re_txq re_txq[RE_TX_QLEN];
147 1.32 tsutsui int re_txq_considx;
148 1.32 tsutsui int re_txq_prodidx;
149 1.34 tsutsui int re_txq_free;
150 1.33 tsutsui
151 1.32 tsutsui bus_dmamap_t re_tx_list_map;
152 1.32 tsutsui struct re_desc *re_tx_list;
153 1.32 tsutsui int re_tx_free; /* # of free descriptors */
154 1.32 tsutsui int re_tx_nextfree; /* next descriptor to use */
155 1.32 tsutsui int re_tx_desc_cnt; /* # of descriptors */
156 1.49 tsutsui bus_dma_segment_t re_tx_listseg;
157 1.32 tsutsui int re_tx_listnseg;
158 1.32 tsutsui
159 1.32 tsutsui struct re_rxsoft re_rxsoft[RE_RX_DESC_CNT];
160 1.32 tsutsui bus_dmamap_t re_rx_list_map;
161 1.32 tsutsui struct re_desc *re_rx_list;
162 1.33 tsutsui int re_rx_prodidx;
163 1.49 tsutsui bus_dma_segment_t re_rx_listseg;
164 1.32 tsutsui int re_rx_listnseg;
165 1.12 toshii };
166 1.32 tsutsui
167 1.9 thorpej struct rtk_tx_desc {
168 1.9 thorpej SIMPLEQ_ENTRY(rtk_tx_desc) txd_q;
169 1.9 thorpej struct mbuf *txd_mbuf;
170 1.9 thorpej bus_dmamap_t txd_dmamap;
171 1.9 thorpej bus_addr_t txd_txaddr;
172 1.9 thorpej bus_addr_t txd_txstat;
173 1.9 thorpej };
174 1.9 thorpej
175 1.4 thorpej struct rtk_softc {
176 1.50 jakllsch device_t sc_dev;
177 1.5 tsutsui struct ethercom ethercom; /* interface info */
178 1.1 tsutsui struct mii_data mii;
179 1.4 thorpej struct callout rtk_tick_ch; /* tick callout */
180 1.50 jakllsch bus_space_tag_t rtk_btag; /* bus space tag */
181 1.4 thorpej bus_space_handle_t rtk_bhandle; /* bus space handle */
182 1.50 jakllsch bus_size_t rtk_bsize; /* bus space mapping size */
183 1.39 tsutsui u_int sc_quirk; /* chip quirks */
184 1.39 tsutsui #define RTKQ_8129 0x00000001 /* 8129 */
185 1.39 tsutsui #define RTKQ_8139CPLUS 0x00000002 /* 8139C+ */
186 1.39 tsutsui #define RTKQ_8169NONS 0x00000004 /* old non-single 8169 */
187 1.39 tsutsui #define RTKQ_PCIE 0x00000008 /* PCIe variants */
188 1.42 tsutsui #define RTKQ_MACLDPS 0x00000010 /* has LDPS register */
189 1.43 tsutsui #define RTKQ_DESCV2 0x00000020 /* has V2 TX/RX descriptor */
190 1.44 tsutsui #define RTKQ_NOJUMBO 0x00000040 /* no jumbo MTU support */
191 1.45 tsutsui #define RTKQ_NOEECMD 0x00000080 /* unusable EEPROM command */
192 1.47 tsutsui #define RTKQ_MACSTAT 0x00000100 /* set MACSTAT_DIS on init */
193 1.47 tsutsui #define RTKQ_CMDSTOP 0x00000200 /* set STOPREQ on stop */
194 1.52 garbled #define RTKQ_PHYWAKE_PM 0x00000400 /* wake PHY from power down */
195 1.54 christos #define RTKQ_RXDV_GATED 0x00000800
196 1.56 jmcneill #define RTKQ_IM_HW 0x00001000 /* HW interrupt mitigation */
197 1.39 tsutsui
198 1.49 tsutsui bus_dma_tag_t sc_dmat;
199 1.8 thorpej
200 1.49 tsutsui bus_dma_segment_t sc_dmaseg; /* for rtk(4) */
201 1.32 tsutsui int sc_dmanseg; /* for rtk(4) */
202 1.32 tsutsui
203 1.49 tsutsui bus_dmamap_t recv_dmamap; /* for rtk(4) */
204 1.46 tsutsui uint8_t *rtk_rx_buf;
205 1.8 thorpej
206 1.9 thorpej struct rtk_tx_desc rtk_tx_descs[RTK_TX_LIST_CNT];
207 1.9 thorpej SIMPLEQ_HEAD(, rtk_tx_desc) rtk_tx_free;
208 1.9 thorpej SIMPLEQ_HEAD(, rtk_tx_desc) rtk_tx_dirty;
209 1.32 tsutsui
210 1.32 tsutsui struct re_list_data re_ldata;
211 1.32 tsutsui struct mbuf *re_head;
212 1.32 tsutsui struct mbuf *re_tail;
213 1.32 tsutsui uint32_t re_rxlenmask;
214 1.32 tsutsui int re_testmode;
215 1.5 tsutsui
216 1.5 tsutsui int sc_flags; /* misc flags */
217 1.39 tsutsui #define RTK_ATTACHED 0x00000001 /* attach has succeeded */
218 1.39 tsutsui #define RTK_ENABLED 0x00000002 /* chip is enabled */
219 1.39 tsutsui #define RTK_IS_ENABLED(sc) ((sc)->sc_flags & RTK_ENABLED)
220 1.39 tsutsui
221 1.10 kanaoka int sc_txthresh; /* Early tx threshold */
222 1.39 tsutsui int sc_rev; /* MII revision */
223 1.10 kanaoka
224 1.5 tsutsui /* Power management hooks. */
225 1.17 perry int (*sc_enable) (struct rtk_softc *);
226 1.17 perry void (*sc_disable) (struct rtk_softc *);
227 1.53 tls
228 1.51 tls krndsource_t rnd_source;
229 1.1 tsutsui };
230 1.1 tsutsui
231 1.32 tsutsui #define RE_TX_DESC_CNT(sc) ((sc)->re_ldata.re_tx_desc_cnt)
232 1.32 tsutsui #define RE_TX_LIST_SZ(sc) (RE_TX_DESC_CNT(sc) * sizeof(struct re_desc))
233 1.36 tsutsui #define RE_NEXT_TX_DESC(sc, x) (((x) + 1) & (RE_TX_DESC_CNT(sc) - 1))
234 1.30 tsutsui
235 1.32 tsutsui #define RE_RX_LIST_SZ (RE_RX_DESC_CNT * sizeof(struct re_desc))
236 1.36 tsutsui #define RE_NEXT_RX_DESC(sc, x) (((x) + 1) & (RE_RX_DESC_CNT - 1))
237 1.30 tsutsui
238 1.36 tsutsui #define RE_NEXT_TXQ(sc, x) (((x) + 1) & (RE_TX_QLEN - 1))
239 1.18 yamt
240 1.32 tsutsui #define RE_TXDESCSYNC(sc, idx, ops) \
241 1.23 tsutsui bus_dmamap_sync((sc)->sc_dmat, \
242 1.32 tsutsui (sc)->re_ldata.re_tx_list_map, \
243 1.32 tsutsui sizeof(struct re_desc) * (idx), \
244 1.32 tsutsui sizeof(struct re_desc), \
245 1.23 tsutsui (ops))
246 1.32 tsutsui #define RE_RXDESCSYNC(sc, idx, ops) \
247 1.23 tsutsui bus_dmamap_sync((sc)->sc_dmat, \
248 1.32 tsutsui (sc)->re_ldata.re_rx_list_map, \
249 1.32 tsutsui sizeof(struct re_desc) * (idx), \
250 1.32 tsutsui sizeof(struct re_desc), \
251 1.23 tsutsui (ops))
252 1.23 tsutsui
253 1.35 tsutsui /*
254 1.35 tsutsui * re(4) hardware ip4csum-tx could be mangled with 28 byte or less IP packets
255 1.35 tsutsui */
256 1.49 tsutsui #define RE_IP4CSUMTX_MINLEN 28
257 1.35 tsutsui #define RE_IP4CSUMTX_PADLEN (ETHER_HDR_LEN + RE_IP4CSUMTX_MINLEN)
258 1.35 tsutsui /*
259 1.35 tsutsui * XXX
260 1.35 tsutsui * We are allocating pad DMA buffer after RX DMA descs for now
261 1.35 tsutsui * because RE_TX_LIST_SZ(sc) always occupies whole page but
262 1.35 tsutsui * RE_RX_LIST_SZ is less than PAGE_SIZE so there is some unused region.
263 1.35 tsutsui */
264 1.37 tsutsui #define RE_RX_DMAMEM_SZ (RE_RX_LIST_SZ + RE_IP4CSUMTX_PADLEN)
265 1.35 tsutsui #define RE_TXPADOFF RE_RX_LIST_SZ
266 1.35 tsutsui #define RE_TXPADDADDR(sc) \
267 1.35 tsutsui ((sc)->re_ldata.re_rx_list_map->dm_segs[0].ds_addr + RE_TXPADOFF)
268 1.35 tsutsui
269 1.35 tsutsui
270 1.31 tsutsui #define RTK_TXTH_MAX RTK_TXTH_1536
271 1.10 kanaoka
272 1.1 tsutsui /*
273 1.1 tsutsui * register space access macros
274 1.1 tsutsui */
275 1.1 tsutsui #define CSR_WRITE_4(sc, reg, val) \
276 1.4 thorpej bus_space_write_4(sc->rtk_btag, sc->rtk_bhandle, reg, val)
277 1.1 tsutsui #define CSR_WRITE_2(sc, reg, val) \
278 1.4 thorpej bus_space_write_2(sc->rtk_btag, sc->rtk_bhandle, reg, val)
279 1.1 tsutsui #define CSR_WRITE_1(sc, reg, val) \
280 1.4 thorpej bus_space_write_1(sc->rtk_btag, sc->rtk_bhandle, reg, val)
281 1.1 tsutsui
282 1.1 tsutsui #define CSR_READ_4(sc, reg) \
283 1.4 thorpej bus_space_read_4(sc->rtk_btag, sc->rtk_bhandle, reg)
284 1.1 tsutsui #define CSR_READ_2(sc, reg) \
285 1.4 thorpej bus_space_read_2(sc->rtk_btag, sc->rtk_bhandle, reg)
286 1.1 tsutsui #define CSR_READ_1(sc, reg) \
287 1.4 thorpej bus_space_read_1(sc->rtk_btag, sc->rtk_bhandle, reg)
288 1.1 tsutsui
289 1.5 tsutsui #define RTK_TIMEOUT 1000
290 1.1 tsutsui
291 1.1 tsutsui /*
292 1.26 tsutsui * PCI low memory base and low I/O base registers
293 1.1 tsutsui */
294 1.1 tsutsui
295 1.5 tsutsui #define RTK_PCI_LOIO 0x10
296 1.5 tsutsui #define RTK_PCI_LOMEM 0x14
297 1.1 tsutsui
298 1.1 tsutsui #ifdef _KERNEL
299 1.24 tsutsui uint16_t rtk_read_eeprom(struct rtk_softc *, int, int);
300 1.17 perry void rtk_setmulti(struct rtk_softc *);
301 1.17 perry void rtk_attach(struct rtk_softc *);
302 1.17 perry int rtk_detach(struct rtk_softc *);
303 1.48 cegger int rtk_activate(device_t, enum devact);
304 1.17 perry int rtk_intr(void *);
305 1.1 tsutsui #endif /* _KERNEL */
306