1 /*-
2  * Copyright (c) 2015-2016 Hiroki Mori.
3  * Copyright (c) 2011-2012 Stefan Bethke.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef _DEV_ETHERSWITCH_RTL8366RBVAR_H_
31 #define	_DEV_ETHERSWITCH_RTL8366RBVAR_H_
32 
33 #define	RTL8366RB		0
34 #define	RTL8366SR		1
35 
36 #define RTL8366_IIC_ADDR	0xa8
37 #define RTL_IICBUS_TIMEOUT	100	/* us */
38 #define RTL_IICBUS_READ		1
39 #define	RTL_IICBUS_WRITE	0
40 /* number of times to try and select the chip on the I2C bus */
41 #define RTL_IICBUS_RETRIES	3
42 #define RTL_IICBUS_RETRY_SLEEP	(hz/1000)
43 
44 /* Register definitions */
45 
46 /* Switch Global Configuration */
47 #define RTL8366_SGCR				0x0000
48 #define RTL8366_SGCR_EN_BC_STORM_CTRL		0x0001
49 #define RTL8366_SGCR_MAX_LENGTH_MASK		0x0030
50 #define RTL8366_SGCR_MAX_LENGTH_1522		0x0000
51 #define RTL8366_SGCR_MAX_LENGTH_1536		0x0010
52 #define RTL8366_SGCR_MAX_LENGTH_1552		0x0020
53 #define RTL8366_SGCR_MAX_LENGTH_9216		0x0030
54 #define RTL8366_SGCR_EN_VLAN			0x2000
55 #define RTL8366_SGCR_EN_VLAN_4KTB		0x4000
56 #define RTL8366_SGCR_EN_QOS			0x8000
57 
58 /* Port Enable Control: DISABLE_PORT[5:0] */
59 #define RTL8366_PECR				0x0001
60 
61 /* Switch Security Control 0: DIS_LEARN[5:0] */
62 #define RTL8366_SSCR0				0x0002
63 
64 /* Switch Security Control 1: DIS_AGE[5:0] */
65 #define RTL8366_SSCR1				0x0003
66 
67 /* Switch Security Control 2 */
68 #define RTL8366_SSCR2				0x0004
69 #define RTL8366_SSCR2_DROP_UNKNOWN_DA		0x0001
70 
71 /* Port Link Status: two ports per register */
72 #define RTL8366_PLSR_BASE			(sc->chip_type == 0 ? 0x0014 : 0x0060)
73 #define RTL8366_PLSR_SPEED_MASK	0x03
74 #define RTL8366_PLSR_SPEED_10		0x00
75 #define RTL8366_PLSR_SPEED_100	0x01
76 #define RTL8366_PLSR_SPEED_1000	0x02
77 #define RTL8366_PLSR_FULLDUPLEX	0x04
78 #define RTL8366_PLSR_LINK		0x10
79 #define RTL8366_PLSR_TXPAUSE		0x20
80 #define RTL8366_PLSR_RXPAUSE		0x40
81 #define RTL8366_PLSR_NO_AUTO		0x80
82 
83 /* VLAN Member Configuration, 3 or 2 registers per VLAN */
84 #define RTL8366_VMCR_BASE			(sc->chip_type == 0 ? 0x0020 : 0x0016)
85 #define RTL8366_VMCR_MULT		(sc->chip_type == 0 ? 3 : 2)
86 #define RTL8366_VMCR_DOT1Q_REG	0
87 #define RTL8366_VMCR_DOT1Q_VID_SHIFT	0
88 #define RTL8366_VMCR_DOT1Q_VID_MASK	0x0fff
89 #define RTL8366_VMCR_DOT1Q_PCP_SHIFT	12
90 #define RTL8366_VMCR_DOT1Q_PCP_MASK	0x7000
91 #define RTL8366_VMCR_MU_REG		1
92 #define RTL8366_VMCR_MU_MEMBER_SHIFT	0
93 #define RTL8366_VMCR_MU_MEMBER_MASK	(sc->chip_type == 0 ? 0x00ff : 0x003f)
94 #define RTL8366_VMCR_MU_UNTAG_SHIFT	(sc->chip_type == 0 ? 8 : 6)
95 #define RTL8366_VMCR_MU_UNTAG_MASK	(sc->chip_type == 0 ? 0xff00 : 0x0fc0)
96 #define RTL8366_VMCR_FID_REG		(sc->chip_type == 0 ? 2 : 1)
97 #define RTL8366_VMCR_FID_FID_SHIFT	(sc->chip_type == 0 ? 0 : 12)
98 #define RTL8366_VMCR_FID_FID_MASK	(sc->chip_type == 0 ? 0x0007 : 0x7000)
99 #define RTL8366_VMCR(_reg, _vlan) \
100 	(RTL8366_VMCR_BASE + _reg + _vlan * RTL8366_VMCR_MULT)
101 /* VLAN Identifier */
102 #define RTL8366_VMCR_VID(_r) \
103 	(_r[RTL8366_VMCR_DOT1Q_REG] & RTL8366_VMCR_DOT1Q_VID_MASK)
104 /* Priority Code Point */
105 #define RTL8366_VMCR_PCP(_r) \
106 	((_r[RTL8366_VMCR_DOT1Q_REG] & RTL8366_VMCR_DOT1Q_PCP_MASK) \
107 	>> RTL8366_VMCR_DOT1Q_PCP_SHIFT)
108 /* Member ports */
109 #define RTL8366_VMCR_MEMBER(_r) \
110 	(_r[RTL8366_VMCR_MU_REG] & RTL8366_VMCR_MU_MEMBER_MASK)
111 /* Untagged ports */
112 #define RTL8366_VMCR_UNTAG(_r) \
113 	((_r[RTL8366_VMCR_MU_REG] & RTL8366_VMCR_MU_UNTAG_MASK) \
114 	>> RTL8366_VMCR_MU_UNTAG_SHIFT)
115 /* Forwarding ID */
116 #define RTL8366_VMCR_FID(_r) \
117 	(sc->chip_type == 0 ? (_r[RTL8366_VMCR_FID_REG] & RTL8366_VMCR_FID_FID_MASK) : \
118 		((_r[RTL8366_VMCR_FID_REG] & RTL8366_VMCR_FID_FID_MASK) \
119 		>> RTL8366_VMCR_FID_FID_SHIFT))
120 
121 /*
122  * Port VLAN Control, 4 ports per register
123  * Determines the VID for untagged ingress frames through
124  * index into VMC.
125  */
126 #define RTL8366_PVCR_BASE			(sc->chip_type == 0 ? 0x0063 : 0x0058)
127 #define RTL8366_PVCR_PORT_SHIFT	4
128 #define RTL8366_PVCR_PORT_PERREG	(16 / RTL8366_PVCR_PORT_SHIFT)
129 #define RTL8366_PVCR_PORT_MASK	0x000f
130 #define RTL8366_PVCR_REG(_port) \
131 	(RTL8366_PVCR_BASE + _port / (RTL8366_PVCR_PORT_PERREG))
132 #define RTL8366_PVCR_VAL(_port, _pvlan) \
133 	((_pvlan & RTL8366_PVCR_PORT_MASK) << \
134 	((_port % RTL8366_PVCR_PORT_PERREG) * RTL8366_PVCR_PORT_SHIFT))
135 #define RTL8366_PVCR_GET(_port, _val) \
136 	(((_val) >> ((_port % RTL8366_PVCR_PORT_PERREG) * RTL8366_PVCR_PORT_SHIFT)) & RTL8366_PVCR_PORT_MASK)
137 
138 /* Reset Control */
139 #define RTL8366_RCR				0x0100
140 #define RTL8366_RCR_HARD_RESET	0x0001
141 #define RTL8366_RCR_SOFT_RESET	0x0002
142 
143 /* Chip Version Control: CHIP_VER[3:0] */
144 #define RTL8366_CVCR				(sc->chip_type == 0 ? 0x050A : 0x0104)
145 /* Chip Identifier */
146 #define RTL8366RB_CIR				0x0509
147 #define RTL8366RB_CIR_ID8366RB		0x5937
148 #define RTL8366SR_CIR				0x0105
149 #define RTL8366SR_CIR_ID8366SR		0x8366
150 
151 /* VLAN Ingress Control 2: [5:0] */
152 #define RTL8366_VIC2R				0x037f
153 
154 /* MIB registers */
155 #define RTL8366_MCNT_BASE			0x1000
156 #define RTL8366_MCTLR				(sc->chip_type == 0 ? 0x13f0 : 0x11F0)
157 #define RTL8366_MCTLR_BUSY		0x0001
158 #define RTL8366_MCTLR_RESET		0x0002
159 #define RTL8366_MCTLR_RESET_PORT_MASK	0x00fc
160 #define RTL8366_MCTLR_RESET_ALL	0x0800
161 
162 #define RTL8366_MCNT(_port, _r) \
163 	(RTL8366_MCNT_BASE + 0x50 * (_port) + (_r))
164 #define RTL8366_MCTLR_RESET_PORT(_p) \
165 	(1 << ((_p) + 2))
166 
167 /* PHY Access Control */
168 #define RTL8366_PACR				(sc->chip_type == 0 ? 0x8000 : 0x8028)
169 #define RTL8366_PACR_WRITE		0x0000
170 #define RTL8366_PACR_READ			0x0001
171 
172 /* PHY Access Data */
173 #define	RTL8366_PADR				(sc->chip_type == 0 ? 0x8002 : 0x8029)
174 
175 #define RTL8366_PHYREG(phy, page, reg) \
176 	(0x8000 | (1 << (((phy) & 0x1f) + 9)) | (((page) & (sc->chip_type == 0 ? 0xf : 0x7)) << 5) | ((reg) & 0x1f))
177 
178 /* general characteristics of the chip */
179 #define	RTL8366_NUM_PHYS			5
180 #define	RTL8366_NUM_VLANS			16
181 #define	RTL8366_NUM_PHY_REG			32
182 
183 #endif
184