blob: 6ef8c9943b3aa0d5475acdb4c7a9981d2b8253da [file] [log] [blame]
developer69bcd592024-03-25 14:26:39 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2023 Airoha Inc.
4 */
5
6#include "an8855.h"
7#include "an8855_regs.h"
8
9struct an8855_mapping an8855_def_mapping[] = {
10 {
11 .name = "llllw",
12 .pvids = { 1, 1, 1, 1, 2, 1 },
13 .members = { 0, 0x2f, 0x30 },
14 .etags = { 0, 0, 0x20 },
15 .vids = { 0, 1, 2 },
16 }, {
17 .name = "wllll",
18 .pvids = { 2, 1, 1, 1, 1, 1 },
19 .members = { 0, 0x3e, 0x21 },
20 .etags = { 0, 0, 0x20 },
21 .vids = { 0, 1, 2 },
22 }, {
23 .name = "lwlll",
24 .pvids = { 1, 2, 1, 1, 1, 1 },
25 .members = { 0, 0x3d, 0x22 },
26 .etags = { 0, 0, 0x20 },
27 .vids = { 0, 1, 2 },
28 }, {
29 .name = "lllll",
30 .pvids = { 1, 1, 1, 1, 1, 1 },
31 .members = { 0, 0x3f },
32 .etags = { 0, 0 },
33 .vids = { 0, 1 },
34 },
35};
36
37void an8855_vlan_ctrl(struct gsw_an8855 *gsw, u32 cmd, u32 val)
38{
39 int i;
40
41 an8855_reg_write(gsw, VTCR,
42 VTCR_BUSY | ((cmd << VTCR_FUNC_S) & VTCR_FUNC_M) |
43 (val & VTCR_VID_M));
44
45 for (i = 0; i < 300; i++) {
46 u32 val = an8855_reg_read(gsw, VTCR);
47
48 if ((val & VTCR_BUSY) == 0)
49 break;
50
51 usleep_range(1000, 1100);
52 }
53
54 if (i == 300)
55 dev_info(gsw->dev, "vtcr timeout\n");
56}
57
58static void an8855_write_vlan_entry(struct gsw_an8855 *gsw, int vlan, u16 vid,
59 u8 ports, u8 etags)
60{
61 int port;
62 u32 val;
63
64 /* vlan port membership */
65 if (ports) {
66 val = IVL_MAC | VTAG_EN | VENTRY_VALID
67 | ((ports << PORT_MEM_S) & PORT_MEM_M);
68 /* egress mode */
69 for (port = 0; port < AN8855_NUM_PORTS; port++) {
70 if (etags & BIT(port))
71 val |= (ETAG_CTRL_TAG << PORT_ETAG_S(port));
72 else
73 val |= (ETAG_CTRL_UNTAG << PORT_ETAG_S(port));
74 }
75 an8855_reg_write(gsw, VAWD0, val);
76 } else {
77 an8855_reg_write(gsw, VAWD0, 0);
78 }
79
80 if (ports & 0x40)
81 an8855_reg_write(gsw, VAWD1, 0x1);
82 else
83 an8855_reg_write(gsw, VAWD1, 0x0);
84
85 /* write to vlan table */
86 an8855_vlan_ctrl(gsw, VTCR_WRITE_VLAN_ENTRY, vid);
87}
88
89void an8855_apply_vlan_config(struct gsw_an8855 *gsw)
90{
91 int i, j;
92 u8 tag_ports;
93 u8 untag_ports;
94 u32 val;
95
96 /* set all ports as security mode */
97 for (i = 0; i < AN8855_NUM_PORTS; i++) {
98 val = an8855_reg_read(gsw, PCR(i));
99 an8855_reg_write(gsw, PCR(i), val | SECURITY_MODE);
100 an8855_reg_write(gsw, PORTMATRIX(i), PORT_MATRIX_M);
101 }
102
103 /* check if a port is used in tag/untag vlan egress mode */
104 tag_ports = 0;
105 untag_ports = 0;
106
107 for (i = 0; i < AN8855_NUM_VLANS; i++) {
108 u8 member = gsw->vlan_entries[i].member;
109 u8 etags = gsw->vlan_entries[i].etags;
110
111 if (!member)
112 continue;
113
114 for (j = 0; j < AN8855_NUM_PORTS; j++) {
115 if (!(member & BIT(j)))
116 continue;
117
118 if (etags & BIT(j))
119 tag_ports |= 1u << j;
120 else
121 untag_ports |= 1u << j;
122 }
123 }
124
125 /* set all untag-only ports as transparent and the rest as user port */
126 for (i = 0; i < AN8855_NUM_PORTS; i++) {
127 u32 pvc_mode = 0x8100 << STAG_VPID_S;
128
129 if (untag_ports & BIT(i) && !(tag_ports & BIT(i)))
130 pvc_mode = (0x8100 << STAG_VPID_S) |
131 (VA_TRANSPARENT_PORT << VLAN_ATTR_S);
132
133 if (gsw->port5_cfg.stag_on && i == 5)
134 pvc_mode = (u32)((0x8100 << STAG_VPID_S) | PVC_PORT_STAG
135 | PVC_STAG_REPLACE);
136
137 an8855_reg_write(gsw, PVC(i), pvc_mode);
138 }
139
140 /* first clear the switch vlan table */
141 for (i = 0; i < AN8855_NUM_VLANS; i++)
142 an8855_write_vlan_entry(gsw, i, i, 0, 0);
143
144 /* now program only vlans with members to avoid
145 * clobbering remapped entries in later iterations
146 */
147 for (i = 0; i < AN8855_NUM_VLANS; i++) {
148 u16 vid = gsw->vlan_entries[i].vid;
149 u8 member = gsw->vlan_entries[i].member;
150 u8 etags = gsw->vlan_entries[i].etags;
151
152 if (member)
153 an8855_write_vlan_entry(gsw, i, vid, member, etags);
154 }
155
156 /* Port Default PVID */
157 for (i = 0; i < AN8855_NUM_PORTS; i++) {
158 int vlan = gsw->port_entries[i].pvid;
159 u16 pvid = 0;
160 u32 val;
161
162 if (vlan < AN8855_NUM_VLANS && gsw->vlan_entries[vlan].member)
163 pvid = gsw->vlan_entries[vlan].vid;
164
165 val = an8855_reg_read(gsw, PVID(i));
166 val &= ~GRP_PORT_VID_M;
167 val |= pvid;
168 an8855_reg_write(gsw, PVID(i), val);
169 }
170}
171
172struct an8855_mapping *an8855_find_mapping(struct device_node *np)
173{
174 const char *map;
175 int i;
176
177 if (of_property_read_string(np, "airoha,portmap", &map))
178 return NULL;
179
180 for (i = 0; i < ARRAY_SIZE(an8855_def_mapping); i++)
181 if (!strcmp(map, an8855_def_mapping[i].name))
182 return &an8855_def_mapping[i];
183
184 return NULL;
185}
186
187void an8855_apply_mapping(struct gsw_an8855 *gsw, struct an8855_mapping *map)
188{
189 int i = 0;
190
191 for (i = 0; i < AN8855_NUM_PORTS; i++)
192 gsw->port_entries[i].pvid = map->pvids[i];
193
194 for (i = 0; i < AN8855_NUM_VLANS; i++) {
195 gsw->vlan_entries[i].member = map->members[i];
196 gsw->vlan_entries[i].etags = map->etags[i];
197 gsw->vlan_entries[i].vid = map->vids[i];
198 }
199}