2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
4 * Copyright (C) 2014 Marvell
6 * Marcin Wojtas <mw@semihalf.com>
9 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
18 #include <dm/device-internal.h>
25 #include <linux/errno.h>
29 #include <asm/arch/cpu.h>
30 #include <asm/arch/soc.h>
31 #include <linux/compat.h>
32 #include <linux/mbus.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 /* Some linux -> U-Boot compatibility stuff */
37 #define netdev_err(dev, fmt, args...) \
39 #define netdev_warn(dev, fmt, args...) \
41 #define netdev_info(dev, fmt, args...) \
43 #define netdev_dbg(dev, fmt, args...) \
46 #define ETH_ALEN 6 /* Octets in one ethernet addr */
48 #define __verify_pcpu_ptr(ptr) \
50 const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
54 #define VERIFY_PERCPU_PTR(__p) \
56 __verify_pcpu_ptr(__p); \
57 (typeof(*(__p)) __kernel __force *)(__p); \
60 #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
61 #define smp_processor_id() 0
62 #define num_present_cpus() 1
63 #define for_each_present_cpu(cpu) \
64 for ((cpu) = 0; (cpu) < 1; (cpu)++)
66 #define NET_SKB_PAD max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
68 #define CONFIG_NR_CPUS 1
69 #define ETH_HLEN ETHER_HDR_SIZE /* Total octets in header */
71 /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
72 #define WRAP (2 + ETH_HLEN + 4 + 32)
74 #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
76 #define MVPP2_SMI_TIMEOUT 10000
78 /* RX Fifo Registers */
79 #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
80 #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
81 #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
82 #define MVPP2_RX_FIFO_INIT_REG 0x64
84 /* RX DMA Top Registers */
85 #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
86 #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
87 #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
88 #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
89 #define MVPP2_POOL_BUF_SIZE_OFFSET 5
90 #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
91 #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
92 #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
93 #define MVPP2_RXQ_POOL_SHORT_OFFS 20
94 #define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
95 #define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
96 #define MVPP2_RXQ_POOL_LONG_OFFS 24
97 #define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
98 #define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
99 #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
100 #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
101 #define MVPP2_RXQ_DISABLE_MASK BIT(31)
103 /* Parser Registers */
104 #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
105 #define MVPP2_PRS_PORT_LU_MAX 0xf
106 #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
107 #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
108 #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
109 #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
110 #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
111 #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
112 #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
113 #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
114 #define MVPP2_PRS_TCAM_IDX_REG 0x1100
115 #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
116 #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
117 #define MVPP2_PRS_SRAM_IDX_REG 0x1200
118 #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
119 #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
120 #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
122 /* Classifier Registers */
123 #define MVPP2_CLS_MODE_REG 0x1800
124 #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
125 #define MVPP2_CLS_PORT_WAY_REG 0x1810
126 #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
127 #define MVPP2_CLS_LKP_INDEX_REG 0x1814
128 #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
129 #define MVPP2_CLS_LKP_TBL_REG 0x1818
130 #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
131 #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
132 #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
133 #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
134 #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
135 #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
136 #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
137 #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
138 #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
139 #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
140 #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
141 #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
143 /* Descriptor Manager Top Registers */
144 #define MVPP2_RXQ_NUM_REG 0x2040
145 #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
146 #define MVPP22_DESC_ADDR_OFFS 8
147 #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
148 #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
149 #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
150 #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
151 #define MVPP2_RXQ_NUM_NEW_OFFSET 16
152 #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
153 #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
154 #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
155 #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
156 #define MVPP2_RXQ_THRESH_REG 0x204c
157 #define MVPP2_OCCUPIED_THRESH_OFFSET 0
158 #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
159 #define MVPP2_RXQ_INDEX_REG 0x2050
160 #define MVPP2_TXQ_NUM_REG 0x2080
161 #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
162 #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
163 #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
164 #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
165 #define MVPP2_TXQ_THRESH_REG 0x2094
166 #define MVPP2_TRANSMITTED_THRESH_OFFSET 16
167 #define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
168 #define MVPP2_TXQ_INDEX_REG 0x2098
169 #define MVPP2_TXQ_PREF_BUF_REG 0x209c
170 #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
171 #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
172 #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
173 #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
174 #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
175 #define MVPP2_TXQ_PENDING_REG 0x20a0
176 #define MVPP2_TXQ_PENDING_MASK 0x3fff
177 #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
178 #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
179 #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
180 #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
181 #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
182 #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
183 #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
184 #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
185 #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
186 #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
187 #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
188 #define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
189 #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
190 #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
191 #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
192 #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
193 #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
195 /* MBUS bridge registers */
196 #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
197 #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
198 #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
199 #define MVPP2_BASE_ADDR_ENABLE 0x4060
201 /* Interrupt Cause and Mask registers */
202 #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
203 #define MVPP2_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
204 #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
205 #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
206 #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
207 #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
208 #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
209 #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
210 #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
211 #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
212 #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
213 #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
214 #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
215 #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
216 #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
217 #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
218 #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
219 #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
220 #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
221 #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
223 /* Buffer Manager registers */
224 #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
225 #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
226 #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
227 #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
228 #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
229 #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
230 #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
231 #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
232 #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
233 #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
234 #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
235 #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
236 #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
237 #define MVPP2_BM_START_MASK BIT(0)
238 #define MVPP2_BM_STOP_MASK BIT(1)
239 #define MVPP2_BM_STATE_MASK BIT(4)
240 #define MVPP2_BM_LOW_THRESH_OFFS 8
241 #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
242 #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
243 MVPP2_BM_LOW_THRESH_OFFS)
244 #define MVPP2_BM_HIGH_THRESH_OFFS 16
245 #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
246 #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
247 MVPP2_BM_HIGH_THRESH_OFFS)
248 #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
249 #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
250 #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
251 #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
252 #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
253 #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
254 #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
255 #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
256 #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
257 #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
258 #define MVPP2_BM_ADDR_HIGH_ALLOC 0x6444
259 #define MVPP2_BM_ADDR_HIGH_PHYS_MASK 0xff
260 #define MVPP2_BM_ADDR_HIGH_VIRT_MASK 0xff00
261 #define MVPP2_BM_ADDR_HIGH_VIRT_SHIFT 8
262 #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
263 #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
264 #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
265 #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
266 #define MVPP2_BM_VIRT_RLS_REG 0x64c0
267 #define MVPP21_BM_MC_RLS_REG 0x64c4
268 #define MVPP2_BM_MC_ID_MASK 0xfff
269 #define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
270 #define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
271 #define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
272 #define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
273 #define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
274 #define MVPP22_BM_MC_RLS_REG 0x64d4
276 /* TX Scheduler registers */
277 #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
278 #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
279 #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
280 #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
281 #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
282 #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
283 #define MVPP2_TXP_SCHED_MTU_REG 0x801c
284 #define MVPP2_TXP_MTU_MAX 0x7FFFF
285 #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
286 #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
287 #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
288 #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
289 #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
290 #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
291 #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
292 #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
293 #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
294 #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
295 #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
296 #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
297 #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
298 #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
300 /* TX general registers */
301 #define MVPP2_TX_SNOOP_REG 0x8800
302 #define MVPP2_TX_PORT_FLUSH_REG 0x8810
303 #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
306 #define MVPP2_SRC_ADDR_MIDDLE 0x24
307 #define MVPP2_SRC_ADDR_HIGH 0x28
308 #define MVPP2_PHY_AN_CFG0_REG 0x34
309 #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
310 #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
311 #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
313 /* Per-port registers */
314 #define MVPP2_GMAC_CTRL_0_REG 0x0
315 #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
316 #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
317 #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
318 #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
319 #define MVPP2_GMAC_CTRL_1_REG 0x4
320 #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
321 #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
322 #define MVPP2_GMAC_PCS_LB_EN_BIT 6
323 #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
324 #define MVPP2_GMAC_SA_LOW_OFFS 7
325 #define MVPP2_GMAC_CTRL_2_REG 0x8
326 #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
327 #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
328 #define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
329 #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
330 #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
331 #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
332 #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
333 #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
334 #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
335 #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
336 #define MVPP2_GMAC_FC_ADV_EN BIT(9)
337 #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
338 #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
339 #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
340 #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
341 #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
342 #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
343 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
345 #define MVPP22_PORT_BASE 0x30e00
346 #define MVPP22_PORT_OFFSET 0x1000
348 #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
350 /* Descriptor ring Macros */
351 #define MVPP2_QUEUE_NEXT_DESC(q, index) \
352 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
354 /* SMI: 0xc0054 -> offset 0x54 to lms_base */
355 #define MVPP2_SMI 0x0054
356 #define MVPP2_PHY_REG_MASK 0x1f
357 /* SMI register fields */
358 #define MVPP2_SMI_DATA_OFFS 0 /* Data */
359 #define MVPP2_SMI_DATA_MASK (0xffff << MVPP2_SMI_DATA_OFFS)
360 #define MVPP2_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
361 #define MVPP2_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
362 #define MVPP2_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
363 #define MVPP2_SMI_OPCODE_READ (1 << MVPP2_SMI_OPCODE_OFFS)
364 #define MVPP2_SMI_READ_VALID (1 << 27) /* Read Valid */
365 #define MVPP2_SMI_BUSY (1 << 28) /* Busy */
367 #define MVPP2_PHY_ADDR_MASK 0x1f
368 #define MVPP2_PHY_REG_MASK 0x1f
370 /* Various constants */
373 #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
374 #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
375 #define MVPP2_RX_COAL_PKTS 32
376 #define MVPP2_RX_COAL_USEC 100
378 /* The two bytes Marvell header. Either contains a special value used
379 * by Marvell switches when a specific hardware mode is enabled (not
380 * supported by this driver) or is filled automatically by zeroes on
381 * the RX side. Those two bytes being at the front of the Ethernet
382 * header, they allow to have the IP header aligned on a 4 bytes
383 * boundary automatically: the hardware skips those two bytes on its
386 #define MVPP2_MH_SIZE 2
387 #define MVPP2_ETH_TYPE_LEN 2
388 #define MVPP2_PPPOE_HDR_SIZE 8
389 #define MVPP2_VLAN_TAG_LEN 4
391 /* Lbtd 802.3 type */
392 #define MVPP2_IP_LBDT_TYPE 0xfffa
394 #define MVPP2_CPU_D_CACHE_LINE_SIZE 32
395 #define MVPP2_TX_CSUM_MAX_SIZE 9800
397 /* Timeout constants */
398 #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
399 #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
401 #define MVPP2_TX_MTU_MAX 0x7ffff
403 /* Maximum number of T-CONTs of PON port */
404 #define MVPP2_MAX_TCONT 16
406 /* Maximum number of supported ports */
407 #define MVPP2_MAX_PORTS 4
409 /* Maximum number of TXQs used by single port */
410 #define MVPP2_MAX_TXQ 8
412 /* Maximum number of RXQs used by single port */
413 #define MVPP2_MAX_RXQ 8
415 /* Default number of TXQs in use */
416 #define MVPP2_DEFAULT_TXQ 1
418 /* Dfault number of RXQs in use */
419 #define MVPP2_DEFAULT_RXQ 1
420 #define CONFIG_MV_ETH_RXQ 8 /* increment by 8 */
422 /* Total number of RXQs available to all ports */
423 #define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
425 /* Max number of Rx descriptors */
426 #define MVPP2_MAX_RXD 16
428 /* Max number of Tx descriptors */
429 #define MVPP2_MAX_TXD 16
431 /* Amount of Tx descriptors that can be reserved at once by CPU */
432 #define MVPP2_CPU_DESC_CHUNK 64
434 /* Max number of Tx descriptors in each aggregated queue */
435 #define MVPP2_AGGR_TXQ_SIZE 256
437 /* Descriptor aligned size */
438 #define MVPP2_DESC_ALIGNED_SIZE 32
440 /* Descriptor alignment mask */
441 #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
443 /* RX FIFO constants */
444 #define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
445 #define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
446 #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
448 /* RX buffer constants */
449 #define MVPP2_SKB_SHINFO_SIZE \
452 #define MVPP2_RX_PKT_SIZE(mtu) \
453 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
454 ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
456 #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
457 #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
458 #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
459 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
461 #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
463 /* IPv6 max L3 address size */
464 #define MVPP2_MAX_L3_ADDR_SIZE 16
467 #define MVPP2_F_LOOPBACK BIT(0)
469 /* Marvell tag types */
470 enum mvpp2_tag_type {
471 MVPP2_TAG_TYPE_NONE = 0,
472 MVPP2_TAG_TYPE_MH = 1,
473 MVPP2_TAG_TYPE_DSA = 2,
474 MVPP2_TAG_TYPE_EDSA = 3,
475 MVPP2_TAG_TYPE_VLAN = 4,
476 MVPP2_TAG_TYPE_LAST = 5
479 /* Parser constants */
480 #define MVPP2_PRS_TCAM_SRAM_SIZE 256
481 #define MVPP2_PRS_TCAM_WORDS 6
482 #define MVPP2_PRS_SRAM_WORDS 4
483 #define MVPP2_PRS_FLOW_ID_SIZE 64
484 #define MVPP2_PRS_FLOW_ID_MASK 0x3f
485 #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
486 #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
487 #define MVPP2_PRS_IPV4_HEAD 0x40
488 #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
489 #define MVPP2_PRS_IPV4_MC 0xe0
490 #define MVPP2_PRS_IPV4_MC_MASK 0xf0
491 #define MVPP2_PRS_IPV4_BC_MASK 0xff
492 #define MVPP2_PRS_IPV4_IHL 0x5
493 #define MVPP2_PRS_IPV4_IHL_MASK 0xf
494 #define MVPP2_PRS_IPV6_MC 0xff
495 #define MVPP2_PRS_IPV6_MC_MASK 0xff
496 #define MVPP2_PRS_IPV6_HOP_MASK 0xff
497 #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
498 #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
499 #define MVPP2_PRS_DBL_VLANS_MAX 100
502 * - lookup ID - 4 bits
504 * - additional information - 1 byte
505 * - header data - 8 bytes
506 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
508 #define MVPP2_PRS_AI_BITS 8
509 #define MVPP2_PRS_PORT_MASK 0xff
510 #define MVPP2_PRS_LU_MASK 0xf
511 #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
512 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
513 #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
514 (((offs) * 2) - ((offs) % 2) + 2)
515 #define MVPP2_PRS_TCAM_AI_BYTE 16
516 #define MVPP2_PRS_TCAM_PORT_BYTE 17
517 #define MVPP2_PRS_TCAM_LU_BYTE 20
518 #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
519 #define MVPP2_PRS_TCAM_INV_WORD 5
520 /* Tcam entries ID */
521 #define MVPP2_PE_DROP_ALL 0
522 #define MVPP2_PE_FIRST_FREE_TID 1
523 #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
524 #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
525 #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
526 #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
527 #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
528 #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
529 #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
530 #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
531 #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
532 #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
533 #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
534 #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
535 #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
536 #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
537 #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
538 #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
539 #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
540 #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
541 #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
542 #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
543 #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
544 #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
545 #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
546 #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
547 #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
550 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
552 #define MVPP2_PRS_SRAM_RI_OFFS 0
553 #define MVPP2_PRS_SRAM_RI_WORD 0
554 #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
555 #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
556 #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
557 #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
558 #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
559 #define MVPP2_PRS_SRAM_UDF_OFFS 73
560 #define MVPP2_PRS_SRAM_UDF_BITS 8
561 #define MVPP2_PRS_SRAM_UDF_MASK 0xff
562 #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
563 #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
564 #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
565 #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
566 #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
567 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
568 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
569 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
570 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
571 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
572 #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
573 #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
574 #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
575 #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
576 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
577 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
578 #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
579 #define MVPP2_PRS_SRAM_AI_OFFS 90
580 #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
581 #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
582 #define MVPP2_PRS_SRAM_AI_MASK 0xff
583 #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
584 #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
585 #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
586 #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
588 /* Sram result info bits assignment */
589 #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
590 #define MVPP2_PRS_RI_DSA_MASK 0x2
591 #define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
592 #define MVPP2_PRS_RI_VLAN_NONE 0x0
593 #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
594 #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
595 #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
596 #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
597 #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
598 #define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
599 #define MVPP2_PRS_RI_L2_UCAST 0x0
600 #define MVPP2_PRS_RI_L2_MCAST BIT(9)
601 #define MVPP2_PRS_RI_L2_BCAST BIT(10)
602 #define MVPP2_PRS_RI_PPPOE_MASK 0x800
603 #define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
604 #define MVPP2_PRS_RI_L3_UN 0x0
605 #define MVPP2_PRS_RI_L3_IP4 BIT(12)
606 #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
607 #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
608 #define MVPP2_PRS_RI_L3_IP6 BIT(14)
609 #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
610 #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
611 #define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
612 #define MVPP2_PRS_RI_L3_UCAST 0x0
613 #define MVPP2_PRS_RI_L3_MCAST BIT(15)
614 #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
615 #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
616 #define MVPP2_PRS_RI_UDF3_MASK 0x300000
617 #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
618 #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
619 #define MVPP2_PRS_RI_L4_TCP BIT(22)
620 #define MVPP2_PRS_RI_L4_UDP BIT(23)
621 #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
622 #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
623 #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
624 #define MVPP2_PRS_RI_DROP_MASK 0x80000000
626 /* Sram additional info bits assignment */
627 #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
628 #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
629 #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
630 #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
631 #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
632 #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
633 #define MVPP2_PRS_SINGLE_VLAN_AI 0
634 #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
637 #define MVPP2_PRS_TAGGED true
638 #define MVPP2_PRS_UNTAGGED false
639 #define MVPP2_PRS_EDSA true
640 #define MVPP2_PRS_DSA false
642 /* MAC entries, shadow udf */
644 MVPP2_PRS_UDF_MAC_DEF,
645 MVPP2_PRS_UDF_MAC_RANGE,
646 MVPP2_PRS_UDF_L2_DEF,
647 MVPP2_PRS_UDF_L2_DEF_COPY,
648 MVPP2_PRS_UDF_L2_USER,
652 enum mvpp2_prs_lookup {
666 enum mvpp2_prs_l3_cast {
667 MVPP2_PRS_L3_UNI_CAST,
668 MVPP2_PRS_L3_MULTI_CAST,
669 MVPP2_PRS_L3_BROAD_CAST
672 /* Classifier constants */
673 #define MVPP2_CLS_FLOWS_TBL_SIZE 512
674 #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
675 #define MVPP2_CLS_LKP_TBL_SIZE 64
678 #define MVPP2_BM_POOLS_NUM 1
679 #define MVPP2_BM_LONG_BUF_NUM 16
680 #define MVPP2_BM_SHORT_BUF_NUM 16
681 #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
682 #define MVPP2_BM_POOL_PTR_ALIGN 128
683 #define MVPP2_BM_SWF_LONG_POOL(port) 0
685 /* BM cookie (32 bits) definition */
686 #define MVPP2_BM_COOKIE_POOL_OFFS 8
687 #define MVPP2_BM_COOKIE_CPU_OFFS 24
689 /* BM short pool packet size
690 * These value assure that for SWF the total number
691 * of bytes allocated for each buffer will be 512
693 #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
703 /* Shared Packet Processor resources */
705 /* Shared registers' base addresses */
707 void __iomem *lms_base;
708 void __iomem *iface_base;
710 /* List of pointers to port structures */
711 struct mvpp2_port **port_list;
713 /* Aggregated TXQs */
714 struct mvpp2_tx_queue *aggr_txqs;
717 struct mvpp2_bm_pool *bm_pools;
719 /* PRS shadow table */
720 struct mvpp2_prs_shadow *prs_shadow;
721 /* PRS auxiliary table for double vlan entries control */
722 bool *prs_double_vlans;
728 enum { MVPP21, MVPP22 } hw_version;
733 struct mvpp2_pcpu_stats {
743 /* Index of the port from the "group of ports" complex point
752 /* Per-port registers' base address */
755 struct mvpp2_rx_queue **rxqs;
756 struct mvpp2_tx_queue **txqs;
760 u32 pending_cause_rx;
762 /* Per-CPU port control */
763 struct mvpp2_port_pcpu __percpu *pcpu;
770 struct mvpp2_pcpu_stats __percpu *stats;
772 struct phy_device *phy_dev;
773 phy_interface_t phy_interface;
781 struct mvpp2_bm_pool *pool_long;
782 struct mvpp2_bm_pool *pool_short;
784 /* Index of first port's physical RXQ */
787 u8 dev_addr[ETH_ALEN];
790 /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
791 * layout of the transmit and reception DMA descriptors, and their
792 * layout is therefore defined by the hardware design
795 #define MVPP2_TXD_L3_OFF_SHIFT 0
796 #define MVPP2_TXD_IP_HLEN_SHIFT 8
797 #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
798 #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
799 #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
800 #define MVPP2_TXD_PADDING_DISABLE BIT(23)
801 #define MVPP2_TXD_L4_UDP BIT(24)
802 #define MVPP2_TXD_L3_IP6 BIT(26)
803 #define MVPP2_TXD_L_DESC BIT(28)
804 #define MVPP2_TXD_F_DESC BIT(29)
806 #define MVPP2_RXD_ERR_SUMMARY BIT(15)
807 #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
808 #define MVPP2_RXD_ERR_CRC 0x0
809 #define MVPP2_RXD_ERR_OVERRUN BIT(13)
810 #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
811 #define MVPP2_RXD_BM_POOL_ID_OFFS 16
812 #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
813 #define MVPP2_RXD_HWF_SYNC BIT(21)
814 #define MVPP2_RXD_L4_CSUM_OK BIT(22)
815 #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
816 #define MVPP2_RXD_L4_TCP BIT(25)
817 #define MVPP2_RXD_L4_UDP BIT(26)
818 #define MVPP2_RXD_L3_IP4 BIT(28)
819 #define MVPP2_RXD_L3_IP6 BIT(30)
820 #define MVPP2_RXD_BUF_HDR BIT(31)
822 /* HW TX descriptor for PPv2.1 */
823 struct mvpp21_tx_desc {
824 u32 command; /* Options used by HW for packet transmitting.*/
825 u8 packet_offset; /* the offset from the buffer beginning */
826 u8 phys_txq; /* destination queue ID */
827 u16 data_size; /* data size of transmitted packet in bytes */
828 u32 buf_dma_addr; /* physical addr of transmitted buffer */
829 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
830 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
831 u32 reserved2; /* reserved (for future use) */
834 /* HW RX descriptor for PPv2.1 */
835 struct mvpp21_rx_desc {
836 u32 status; /* info about received packet */
837 u16 reserved1; /* parser_info (for future use, PnC) */
838 u16 data_size; /* size of received packet in bytes */
839 u32 buf_dma_addr; /* physical address of the buffer */
840 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
841 u16 reserved2; /* gem_port_id (for future use, PON) */
842 u16 reserved3; /* csum_l4 (for future use, PnC) */
843 u8 reserved4; /* bm_qset (for future use, BM) */
845 u16 reserved6; /* classify_info (for future use, PnC) */
846 u32 reserved7; /* flow_id (for future use, PnC) */
850 /* HW TX descriptor for PPv2.2 */
851 struct mvpp22_tx_desc {
857 u64 buf_dma_addr_ptp;
861 /* HW RX descriptor for PPv2.2 */
862 struct mvpp22_rx_desc {
868 u64 buf_dma_addr_key_hash;
872 /* Opaque type used by the driver to manipulate the HW TX and RX
875 struct mvpp2_tx_desc {
877 struct mvpp21_tx_desc pp21;
878 struct mvpp22_tx_desc pp22;
882 struct mvpp2_rx_desc {
884 struct mvpp21_rx_desc pp21;
885 struct mvpp22_rx_desc pp22;
889 /* Per-CPU Tx queue control */
890 struct mvpp2_txq_pcpu {
893 /* Number of Tx DMA descriptors in the descriptor ring */
896 /* Number of currently used Tx DMA descriptor in the
901 /* Number of Tx DMA descriptors reserved for each CPU */
904 /* Index of last TX DMA descriptor that was inserted */
907 /* Index of the TX DMA descriptor to be cleaned up */
911 struct mvpp2_tx_queue {
912 /* Physical number of this Tx queue */
915 /* Logical number of this Tx queue */
918 /* Number of Tx DMA descriptors in the descriptor ring */
921 /* Number of currently used Tx DMA descriptor in the descriptor ring */
924 /* Per-CPU control of physical Tx queues */
925 struct mvpp2_txq_pcpu __percpu *pcpu;
929 /* Virtual address of thex Tx DMA descriptors array */
930 struct mvpp2_tx_desc *descs;
932 /* DMA address of the Tx DMA descriptors array */
933 dma_addr_t descs_dma;
935 /* Index of the last Tx DMA descriptor */
938 /* Index of the next Tx DMA descriptor to process */
939 int next_desc_to_proc;
942 struct mvpp2_rx_queue {
943 /* RX queue number, in the range 0-31 for physical RXQs */
946 /* Num of rx descriptors in the rx descriptor ring */
952 /* Virtual address of the RX DMA descriptors array */
953 struct mvpp2_rx_desc *descs;
955 /* DMA address of the RX DMA descriptors array */
956 dma_addr_t descs_dma;
958 /* Index of the last RX DMA descriptor */
961 /* Index of the next RX DMA descriptor to process */
962 int next_desc_to_proc;
964 /* ID of port to which physical RXQ is mapped */
967 /* Port's logic RXQ number to which physical RXQ is mapped */
971 union mvpp2_prs_tcam_entry {
972 u32 word[MVPP2_PRS_TCAM_WORDS];
973 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
976 union mvpp2_prs_sram_entry {
977 u32 word[MVPP2_PRS_SRAM_WORDS];
978 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
981 struct mvpp2_prs_entry {
983 union mvpp2_prs_tcam_entry tcam;
984 union mvpp2_prs_sram_entry sram;
987 struct mvpp2_prs_shadow {
994 /* User defined offset */
1002 struct mvpp2_cls_flow_entry {
1004 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1007 struct mvpp2_cls_lookup_entry {
1013 struct mvpp2_bm_pool {
1014 /* Pool number in the range 0-7 */
1016 enum mvpp2_bm_type type;
1018 /* Buffer Pointers Pool External (BPPE) size */
1020 /* Number of buffers for this pool */
1022 /* Pool buffer size */
1027 /* BPPE virtual base address */
1028 unsigned long *virt_addr;
1029 /* BPPE DMA base address */
1030 dma_addr_t dma_addr;
1032 /* Ports using BM pool */
1035 /* Occupied buffers indicator */
1039 /* Static declaractions */
1041 /* Number of RXQs used by single port */
1042 static int rxq_number = MVPP2_DEFAULT_RXQ;
1043 /* Number of TXQs used by single port */
1044 static int txq_number = MVPP2_DEFAULT_TXQ;
1046 #define MVPP2_DRIVER_NAME "mvpp2"
1047 #define MVPP2_DRIVER_VERSION "1.0"
1050 * U-Boot internal data, mostly uncached buffers for descriptors and data
1052 struct buffer_location {
1053 struct mvpp2_tx_desc *aggr_tx_descs;
1054 struct mvpp2_tx_desc *tx_descs;
1055 struct mvpp2_rx_desc *rx_descs;
1056 unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
1057 unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
1062 * All 4 interfaces use the same global buffer, since only one interface
1063 * can be enabled at once
1065 static struct buffer_location buffer_loc;
1068 * Page table entries are set to 1MB, or multiples of 1MB
1069 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
1071 #define BD_SPACE (1 << 20)
1073 /* Utility/helper methods */
1075 static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1077 writel(data, priv->base + offset);
1080 static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1082 return readl(priv->base + offset);
1085 static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1086 struct mvpp2_tx_desc *tx_desc,
1087 dma_addr_t dma_addr)
1089 if (port->priv->hw_version == MVPP21) {
1090 tx_desc->pp21.buf_dma_addr = dma_addr;
1092 u64 val = (u64)dma_addr;
1094 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1095 tx_desc->pp22.buf_dma_addr_ptp |= val;
1099 static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1100 struct mvpp2_tx_desc *tx_desc,
1103 if (port->priv->hw_version == MVPP21)
1104 tx_desc->pp21.data_size = size;
1106 tx_desc->pp22.data_size = size;
1109 static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1110 struct mvpp2_tx_desc *tx_desc,
1113 if (port->priv->hw_version == MVPP21)
1114 tx_desc->pp21.phys_txq = txq;
1116 tx_desc->pp22.phys_txq = txq;
1119 static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1120 struct mvpp2_tx_desc *tx_desc,
1121 unsigned int command)
1123 if (port->priv->hw_version == MVPP21)
1124 tx_desc->pp21.command = command;
1126 tx_desc->pp22.command = command;
1129 static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1130 struct mvpp2_tx_desc *tx_desc,
1131 unsigned int offset)
1133 if (port->priv->hw_version == MVPP21)
1134 tx_desc->pp21.packet_offset = offset;
1136 tx_desc->pp22.packet_offset = offset;
1139 static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1140 struct mvpp2_rx_desc *rx_desc)
1142 if (port->priv->hw_version == MVPP21)
1143 return rx_desc->pp21.buf_dma_addr;
1145 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
1148 static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1149 struct mvpp2_rx_desc *rx_desc)
1151 if (port->priv->hw_version == MVPP21)
1152 return rx_desc->pp21.buf_cookie;
1154 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
1157 static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1158 struct mvpp2_rx_desc *rx_desc)
1160 if (port->priv->hw_version == MVPP21)
1161 return rx_desc->pp21.data_size;
1163 return rx_desc->pp22.data_size;
1166 static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1167 struct mvpp2_rx_desc *rx_desc)
1169 if (port->priv->hw_version == MVPP21)
1170 return rx_desc->pp21.status;
1172 return rx_desc->pp22.status;
1175 static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1177 txq_pcpu->txq_get_index++;
1178 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1179 txq_pcpu->txq_get_index = 0;
1182 /* Get number of physical egress port */
1183 static inline int mvpp2_egress_port(struct mvpp2_port *port)
1185 return MVPP2_MAX_TCONT + port->id;
1188 /* Get number of physical TXQ */
1189 static inline int mvpp2_txq_phys(int port, int txq)
1191 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1194 /* Parser configuration routines */
1196 /* Update parser tcam and sram hw entries */
1197 static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1201 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1204 /* Clear entry invalidation bit */
1205 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1207 /* Write tcam index - indirect access */
1208 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1209 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1210 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1212 /* Write sram index - indirect access */
1213 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1214 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1215 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1220 /* Read tcam entry from hw */
1221 static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1225 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1228 /* Write tcam index - indirect access */
1229 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1231 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1232 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1233 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1234 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1236 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1237 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1239 /* Write sram index - indirect access */
1240 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1241 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1242 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1247 /* Invalidate tcam hw entry */
1248 static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1250 /* Write index - indirect access */
1251 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1252 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1253 MVPP2_PRS_TCAM_INV_MASK);
1256 /* Enable shadow table entry and set its lookup ID */
1257 static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1259 priv->prs_shadow[index].valid = true;
1260 priv->prs_shadow[index].lu = lu;
1263 /* Update ri fields in shadow table entry */
1264 static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1265 unsigned int ri, unsigned int ri_mask)
1267 priv->prs_shadow[index].ri_mask = ri_mask;
1268 priv->prs_shadow[index].ri = ri;
1271 /* Update lookup field in tcam sw entry */
1272 static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1274 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1276 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1277 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1280 /* Update mask for single port in tcam sw entry */
1281 static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1282 unsigned int port, bool add)
1284 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1287 pe->tcam.byte[enable_off] &= ~(1 << port);
1289 pe->tcam.byte[enable_off] |= 1 << port;
1292 /* Update port map in tcam sw entry */
1293 static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1296 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1297 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1299 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1300 pe->tcam.byte[enable_off] &= ~port_mask;
1301 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1304 /* Obtain port map from tcam sw entry */
1305 static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1307 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1309 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1312 /* Set byte of data and its enable bits in tcam sw entry */
1313 static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1314 unsigned int offs, unsigned char byte,
1315 unsigned char enable)
1317 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1318 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1321 /* Get byte of data and its enable bits from tcam sw entry */
1322 static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1323 unsigned int offs, unsigned char *byte,
1324 unsigned char *enable)
1326 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1327 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1330 /* Set ethertype in tcam sw entry */
1331 static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1332 unsigned short ethertype)
1334 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1335 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1338 /* Set bits in sram sw entry */
1339 static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1342 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1345 /* Clear bits in sram sw entry */
1346 static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1349 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1352 /* Update ri bits in sram sw entry */
1353 static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1354 unsigned int bits, unsigned int mask)
1358 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1359 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1361 if (!(mask & BIT(i)))
1365 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1367 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1369 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1373 /* Update ai bits in sram sw entry */
1374 static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1375 unsigned int bits, unsigned int mask)
1378 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1380 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1382 if (!(mask & BIT(i)))
1386 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1388 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1390 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1394 /* Read ai bits from sram sw entry */
1395 static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1398 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1399 int ai_en_off = ai_off + 1;
1400 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1402 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1403 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1408 /* In sram sw entry set lookup ID field of the tcam key to be used in the next
1411 static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1414 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1416 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1417 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1418 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1421 /* In the sram sw entry set sign and value of the next lookup offset
1422 * and the offset value generated to the classifier
1424 static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1429 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1432 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1436 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1437 (unsigned char)shift;
1439 /* Reset and set operation */
1440 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1441 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1442 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1444 /* Set base offset as current */
1445 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1448 /* In the sram sw entry set sign and value of the user defined offset
1449 * generated to the classifier
1451 static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1452 unsigned int type, int offset,
1457 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1458 offset = 0 - offset;
1460 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1464 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1465 MVPP2_PRS_SRAM_UDF_MASK);
1466 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1467 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1468 MVPP2_PRS_SRAM_UDF_BITS)] &=
1469 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1470 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1471 MVPP2_PRS_SRAM_UDF_BITS)] |=
1472 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1474 /* Set offset type */
1475 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1476 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1477 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1479 /* Set offset operation */
1480 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1481 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1482 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1484 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1485 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1486 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1487 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1489 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1490 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1491 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1493 /* Set base offset as current */
1494 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1497 /* Find parser flow entry */
1498 static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1500 struct mvpp2_prs_entry *pe;
1503 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1506 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1508 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1509 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1512 if (!priv->prs_shadow[tid].valid ||
1513 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1517 mvpp2_prs_hw_read(priv, pe);
1518 bits = mvpp2_prs_sram_ai_get(pe);
1520 /* Sram store classification lookup ID in AI bits [5:0] */
1521 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1529 /* Return first free tcam index, seeking from start to end */
1530 static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1538 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1539 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1541 for (tid = start; tid <= end; tid++) {
1542 if (!priv->prs_shadow[tid].valid)
1549 /* Enable/disable dropping all mac da's */
1550 static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1552 struct mvpp2_prs_entry pe;
1554 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1555 /* Entry exist - update port only */
1556 pe.index = MVPP2_PE_DROP_ALL;
1557 mvpp2_prs_hw_read(priv, &pe);
1559 /* Entry doesn't exist - create new */
1560 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1561 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1562 pe.index = MVPP2_PE_DROP_ALL;
1564 /* Non-promiscuous mode for all ports - DROP unknown packets */
1565 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1566 MVPP2_PRS_RI_DROP_MASK);
1568 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1569 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1571 /* Update shadow table */
1572 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1574 /* Mask all ports */
1575 mvpp2_prs_tcam_port_map_set(&pe, 0);
1578 /* Update port mask */
1579 mvpp2_prs_tcam_port_set(&pe, port, add);
1581 mvpp2_prs_hw_write(priv, &pe);
1584 /* Set port to promiscuous mode */
1585 static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1587 struct mvpp2_prs_entry pe;
1589 /* Promiscuous mode - Accept unknown packets */
1591 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1592 /* Entry exist - update port only */
1593 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1594 mvpp2_prs_hw_read(priv, &pe);
1596 /* Entry doesn't exist - create new */
1597 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1598 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1599 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1601 /* Continue - set next lookup */
1602 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1604 /* Set result info bits */
1605 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1606 MVPP2_PRS_RI_L2_CAST_MASK);
1608 /* Shift to ethertype */
1609 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1610 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1612 /* Mask all ports */
1613 mvpp2_prs_tcam_port_map_set(&pe, 0);
1615 /* Update shadow table */
1616 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1619 /* Update port mask */
1620 mvpp2_prs_tcam_port_set(&pe, port, add);
1622 mvpp2_prs_hw_write(priv, &pe);
1625 /* Accept multicast */
1626 static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1629 struct mvpp2_prs_entry pe;
1630 unsigned char da_mc;
1632 /* Ethernet multicast address first byte is
1633 * 0x01 for IPv4 and 0x33 for IPv6
1635 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1637 if (priv->prs_shadow[index].valid) {
1638 /* Entry exist - update port only */
1640 mvpp2_prs_hw_read(priv, &pe);
1642 /* Entry doesn't exist - create new */
1643 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1644 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1647 /* Continue - set next lookup */
1648 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1650 /* Set result info bits */
1651 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1652 MVPP2_PRS_RI_L2_CAST_MASK);
1654 /* Update tcam entry data first byte */
1655 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1657 /* Shift to ethertype */
1658 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1659 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1661 /* Mask all ports */
1662 mvpp2_prs_tcam_port_map_set(&pe, 0);
1664 /* Update shadow table */
1665 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1668 /* Update port mask */
1669 mvpp2_prs_tcam_port_set(&pe, port, add);
1671 mvpp2_prs_hw_write(priv, &pe);
1674 /* Parser per-port initialization */
1675 static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
1676 int lu_max, int offset)
1681 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
1682 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
1683 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
1684 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
1686 /* Set maximum number of loops for packet received from port */
1687 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
1688 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
1689 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
1690 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
1692 /* Set initial offset for packet header extraction for the first
1695 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
1696 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
1697 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
1698 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
1701 /* Default flow entries initialization for all ports */
1702 static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
1704 struct mvpp2_prs_entry pe;
1707 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
1708 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1709 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1710 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
1712 /* Mask all ports */
1713 mvpp2_prs_tcam_port_map_set(&pe, 0);
1716 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
1717 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
1719 /* Update shadow table and hw entry */
1720 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
1721 mvpp2_prs_hw_write(priv, &pe);
1725 /* Set default entry for Marvell Header field */
1726 static void mvpp2_prs_mh_init(struct mvpp2 *priv)
1728 struct mvpp2_prs_entry pe;
1730 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1732 pe.index = MVPP2_PE_MH_DEFAULT;
1733 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
1734 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
1735 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1736 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
1738 /* Unmask all ports */
1739 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1741 /* Update shadow table and hw entry */
1742 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
1743 mvpp2_prs_hw_write(priv, &pe);
1746 /* Set default entires (place holder) for promiscuous, non-promiscuous and
1747 * multicast MAC addresses
1749 static void mvpp2_prs_mac_init(struct mvpp2 *priv)
1751 struct mvpp2_prs_entry pe;
1753 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1755 /* Non-promiscuous mode for all ports - DROP unknown packets */
1756 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
1757 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1759 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1760 MVPP2_PRS_RI_DROP_MASK);
1761 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1762 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1764 /* Unmask all ports */
1765 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1767 /* Update shadow table and hw entry */
1768 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1769 mvpp2_prs_hw_write(priv, &pe);
1771 /* place holders only - no ports */
1772 mvpp2_prs_mac_drop_all_set(priv, 0, false);
1773 mvpp2_prs_mac_promisc_set(priv, 0, false);
1774 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
1775 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
1778 /* Match basic ethertypes */
1779 static int mvpp2_prs_etype_init(struct mvpp2 *priv)
1781 struct mvpp2_prs_entry pe;
1784 /* Ethertype: PPPoE */
1785 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1786 MVPP2_PE_LAST_FREE_TID);
1790 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1791 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1794 mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
1796 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
1797 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1798 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1799 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
1800 MVPP2_PRS_RI_PPPOE_MASK);
1802 /* Update shadow table and hw entry */
1803 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1804 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1805 priv->prs_shadow[pe.index].finish = false;
1806 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
1807 MVPP2_PRS_RI_PPPOE_MASK);
1808 mvpp2_prs_hw_write(priv, &pe);
1810 /* Ethertype: ARP */
1811 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1812 MVPP2_PE_LAST_FREE_TID);
1816 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1817 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1820 mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
1822 /* Generate flow in the next iteration*/
1823 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1824 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1825 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
1826 MVPP2_PRS_RI_L3_PROTO_MASK);
1828 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1830 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1832 /* Update shadow table and hw entry */
1833 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1834 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1835 priv->prs_shadow[pe.index].finish = true;
1836 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
1837 MVPP2_PRS_RI_L3_PROTO_MASK);
1838 mvpp2_prs_hw_write(priv, &pe);
1840 /* Ethertype: LBTD */
1841 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1842 MVPP2_PE_LAST_FREE_TID);
1846 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1847 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1850 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
1852 /* Generate flow in the next iteration*/
1853 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1854 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1855 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1856 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1857 MVPP2_PRS_RI_CPU_CODE_MASK |
1858 MVPP2_PRS_RI_UDF3_MASK);
1860 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1862 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1864 /* Update shadow table and hw entry */
1865 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1866 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1867 priv->prs_shadow[pe.index].finish = true;
1868 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1869 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1870 MVPP2_PRS_RI_CPU_CODE_MASK |
1871 MVPP2_PRS_RI_UDF3_MASK);
1872 mvpp2_prs_hw_write(priv, &pe);
1874 /* Ethertype: IPv4 without options */
1875 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1876 MVPP2_PE_LAST_FREE_TID);
1880 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1881 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1884 mvpp2_prs_match_etype(&pe, 0, PROT_IP);
1885 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1886 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
1887 MVPP2_PRS_IPV4_HEAD_MASK |
1888 MVPP2_PRS_IPV4_IHL_MASK);
1890 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1891 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
1892 MVPP2_PRS_RI_L3_PROTO_MASK);
1893 /* Skip eth_type + 4 bytes of IP header */
1894 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
1895 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1897 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1899 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1901 /* Update shadow table and hw entry */
1902 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1903 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1904 priv->prs_shadow[pe.index].finish = false;
1905 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
1906 MVPP2_PRS_RI_L3_PROTO_MASK);
1907 mvpp2_prs_hw_write(priv, &pe);
1909 /* Ethertype: IPv4 with options */
1910 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1911 MVPP2_PE_LAST_FREE_TID);
1917 /* Clear tcam data before updating */
1918 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
1919 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
1921 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1922 MVPP2_PRS_IPV4_HEAD,
1923 MVPP2_PRS_IPV4_HEAD_MASK);
1925 /* Clear ri before updating */
1926 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
1927 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
1928 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
1929 MVPP2_PRS_RI_L3_PROTO_MASK);
1931 /* Update shadow table and hw entry */
1932 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1933 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1934 priv->prs_shadow[pe.index].finish = false;
1935 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
1936 MVPP2_PRS_RI_L3_PROTO_MASK);
1937 mvpp2_prs_hw_write(priv, &pe);
1939 /* Ethertype: IPv6 without options */
1940 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1941 MVPP2_PE_LAST_FREE_TID);
1945 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1946 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1949 mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
1951 /* Skip DIP of IPV6 header */
1952 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
1953 MVPP2_MAX_L3_ADDR_SIZE,
1954 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1955 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1956 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
1957 MVPP2_PRS_RI_L3_PROTO_MASK);
1959 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1961 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1963 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1964 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1965 priv->prs_shadow[pe.index].finish = false;
1966 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
1967 MVPP2_PRS_RI_L3_PROTO_MASK);
1968 mvpp2_prs_hw_write(priv, &pe);
1970 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
1971 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1972 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1973 pe.index = MVPP2_PE_ETH_TYPE_UN;
1975 /* Unmask all ports */
1976 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1978 /* Generate flow in the next iteration*/
1979 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1980 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1981 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
1982 MVPP2_PRS_RI_L3_PROTO_MASK);
1983 /* Set L3 offset even it's unknown L3 */
1984 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1986 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1988 /* Update shadow table and hw entry */
1989 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1990 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1991 priv->prs_shadow[pe.index].finish = true;
1992 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
1993 MVPP2_PRS_RI_L3_PROTO_MASK);
1994 mvpp2_prs_hw_write(priv, &pe);
1999 /* Parser default initialization */
2000 static int mvpp2_prs_default_init(struct udevice *dev,
2005 /* Enable tcam table */
2006 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
2008 /* Clear all tcam and sram entries */
2009 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
2010 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
2011 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
2012 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
2014 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
2015 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
2016 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
2019 /* Invalidate all tcam entries */
2020 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
2021 mvpp2_prs_hw_inv(priv, index);
2023 priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
2024 sizeof(struct mvpp2_prs_shadow),
2026 if (!priv->prs_shadow)
2029 /* Always start from lookup = 0 */
2030 for (index = 0; index < MVPP2_MAX_PORTS; index++)
2031 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
2032 MVPP2_PRS_PORT_LU_MAX, 0);
2034 mvpp2_prs_def_flow_init(priv);
2036 mvpp2_prs_mh_init(priv);
2038 mvpp2_prs_mac_init(priv);
2040 err = mvpp2_prs_etype_init(priv);
2047 /* Compare MAC DA with tcam entry data */
2048 static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
2049 const u8 *da, unsigned char *mask)
2051 unsigned char tcam_byte, tcam_mask;
2054 for (index = 0; index < ETH_ALEN; index++) {
2055 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
2056 if (tcam_mask != mask[index])
2059 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
2066 /* Find tcam entry with matched pair <MAC DA, port> */
2067 static struct mvpp2_prs_entry *
2068 mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
2069 unsigned char *mask, int udf_type)
2071 struct mvpp2_prs_entry *pe;
2074 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2077 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2079 /* Go through the all entires with MVPP2_PRS_LU_MAC */
2080 for (tid = MVPP2_PE_FIRST_FREE_TID;
2081 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2082 unsigned int entry_pmap;
2084 if (!priv->prs_shadow[tid].valid ||
2085 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
2086 (priv->prs_shadow[tid].udf != udf_type))
2090 mvpp2_prs_hw_read(priv, pe);
2091 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
2093 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
2102 /* Update parser's mac da entry */
2103 static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
2104 const u8 *da, bool add)
2106 struct mvpp2_prs_entry *pe;
2107 unsigned int pmap, len, ri;
2108 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2111 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
2112 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
2113 MVPP2_PRS_UDF_MAC_DEF);
2120 /* Create new TCAM entry */
2121 /* Find first range mac entry*/
2122 for (tid = MVPP2_PE_FIRST_FREE_TID;
2123 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
2124 if (priv->prs_shadow[tid].valid &&
2125 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
2126 (priv->prs_shadow[tid].udf ==
2127 MVPP2_PRS_UDF_MAC_RANGE))
2130 /* Go through the all entries from first to last */
2131 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2136 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2139 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2142 /* Mask all ports */
2143 mvpp2_prs_tcam_port_map_set(pe, 0);
2146 /* Update port mask */
2147 mvpp2_prs_tcam_port_set(pe, port, add);
2149 /* Invalidate the entry if no ports are left enabled */
2150 pmap = mvpp2_prs_tcam_port_map_get(pe);
2156 mvpp2_prs_hw_inv(priv, pe->index);
2157 priv->prs_shadow[pe->index].valid = false;
2162 /* Continue - set next lookup */
2163 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
2165 /* Set match on DA */
2168 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
2170 /* Set result info bits */
2171 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
2173 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2174 MVPP2_PRS_RI_MAC_ME_MASK);
2175 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2176 MVPP2_PRS_RI_MAC_ME_MASK);
2178 /* Shift to ethertype */
2179 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
2180 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2182 /* Update shadow table and hw entry */
2183 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
2184 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
2185 mvpp2_prs_hw_write(priv, pe);
2192 static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
2196 /* Remove old parser entry */
2197 err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
2202 /* Add new parser entry */
2203 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
2207 /* Set addr in the device */
2208 memcpy(port->dev_addr, da, ETH_ALEN);
2213 /* Set prs flow for the port */
2214 static int mvpp2_prs_def_flow(struct mvpp2_port *port)
2216 struct mvpp2_prs_entry *pe;
2219 pe = mvpp2_prs_flow_find(port->priv, port->id);
2221 /* Such entry not exist */
2223 /* Go through the all entires from last to first */
2224 tid = mvpp2_prs_tcam_first_free(port->priv,
2225 MVPP2_PE_LAST_FREE_TID,
2226 MVPP2_PE_FIRST_FREE_TID);
2230 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2234 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
2238 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
2239 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2241 /* Update shadow table */
2242 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
2245 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
2246 mvpp2_prs_hw_write(port->priv, pe);
2252 /* Classifier configuration routines */
2254 /* Update classification flow table registers */
2255 static void mvpp2_cls_flow_write(struct mvpp2 *priv,
2256 struct mvpp2_cls_flow_entry *fe)
2258 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
2259 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
2260 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
2261 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
2264 /* Update classification lookup table register */
2265 static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
2266 struct mvpp2_cls_lookup_entry *le)
2270 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
2271 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
2272 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
2275 /* Classifier default initialization */
2276 static void mvpp2_cls_init(struct mvpp2 *priv)
2278 struct mvpp2_cls_lookup_entry le;
2279 struct mvpp2_cls_flow_entry fe;
2282 /* Enable classifier */
2283 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
2285 /* Clear classifier flow table */
2286 memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
2287 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
2289 mvpp2_cls_flow_write(priv, &fe);
2292 /* Clear classifier lookup table */
2294 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
2297 mvpp2_cls_lookup_write(priv, &le);
2300 mvpp2_cls_lookup_write(priv, &le);
2304 static void mvpp2_cls_port_config(struct mvpp2_port *port)
2306 struct mvpp2_cls_lookup_entry le;
2309 /* Set way for the port */
2310 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
2311 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
2312 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
2314 /* Pick the entry to be accessed in lookup ID decoding table
2315 * according to the way and lkpid.
2317 le.lkpid = port->id;
2321 /* Set initial CPU queue for receiving packets */
2322 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
2323 le.data |= port->first_rxq;
2325 /* Disable classification engines */
2326 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
2328 /* Update lookup ID table entry */
2329 mvpp2_cls_lookup_write(port->priv, &le);
2332 /* Set CPU queue number for oversize packets */
2333 static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
2337 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
2338 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
2340 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
2341 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
2343 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
2344 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
2345 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
2348 /* Buffer Manager configuration routines */
2351 static int mvpp2_bm_pool_create(struct udevice *dev,
2353 struct mvpp2_bm_pool *bm_pool, int size)
2357 /* Number of buffer pointers must be a multiple of 16, as per
2358 * hardware constraints
2360 if (!IS_ALIGNED(size, 16))
2363 bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
2364 bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
2365 if (!bm_pool->virt_addr)
2368 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
2369 MVPP2_BM_POOL_PTR_ALIGN)) {
2370 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
2371 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
2375 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
2376 lower_32_bits(bm_pool->dma_addr));
2377 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
2379 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2380 val |= MVPP2_BM_START_MASK;
2381 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2383 bm_pool->type = MVPP2_BM_FREE;
2384 bm_pool->size = size;
2385 bm_pool->pkt_size = 0;
2386 bm_pool->buf_num = 0;
2391 /* Set pool buffer size */
2392 static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
2393 struct mvpp2_bm_pool *bm_pool,
2398 bm_pool->buf_size = buf_size;
2400 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
2401 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
2404 /* Free all buffers from the pool */
2405 static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
2406 struct mvpp2_bm_pool *bm_pool)
2408 bm_pool->buf_num = 0;
2412 static int mvpp2_bm_pool_destroy(struct udevice *dev,
2414 struct mvpp2_bm_pool *bm_pool)
2418 mvpp2_bm_bufs_free(dev, priv, bm_pool);
2419 if (bm_pool->buf_num) {
2420 dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
2424 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2425 val |= MVPP2_BM_STOP_MASK;
2426 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2431 static int mvpp2_bm_pools_init(struct udevice *dev,
2435 struct mvpp2_bm_pool *bm_pool;
2437 /* Create all pools with maximum size */
2438 size = MVPP2_BM_POOL_SIZE_MAX;
2439 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2440 bm_pool = &priv->bm_pools[i];
2442 err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
2444 goto err_unroll_pools;
2445 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
2450 dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
2451 for (i = i - 1; i >= 0; i--)
2452 mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
2456 static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
2460 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2461 /* Mask BM all interrupts */
2462 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
2463 /* Clear BM cause register */
2464 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
2467 /* Allocate and initialize BM pools */
2468 priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
2469 sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
2470 if (!priv->bm_pools)
2473 err = mvpp2_bm_pools_init(dev, priv);
2479 /* Attach long pool to rxq */
2480 static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
2481 int lrxq, int long_pool)
2486 /* Get queue physical ID */
2487 prxq = port->rxqs[lrxq]->id;
2489 if (port->priv->hw_version == MVPP21)
2490 mask = MVPP21_RXQ_POOL_LONG_MASK;
2492 mask = MVPP22_RXQ_POOL_LONG_MASK;
2494 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2496 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
2497 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2500 /* Set pool number in a BM cookie */
2501 static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
2505 bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
2506 bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
2511 /* Get pool number from a BM cookie */
2512 static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
2514 return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
2517 /* Release buffer to BM */
2518 static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
2519 dma_addr_t buf_dma_addr,
2520 unsigned long buf_phys_addr)
2522 if (port->priv->hw_version == MVPP22) {
2525 if (sizeof(dma_addr_t) == 8)
2526 val |= upper_32_bits(buf_dma_addr) &
2527 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
2529 if (sizeof(phys_addr_t) == 8)
2530 val |= (upper_32_bits(buf_phys_addr)
2531 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
2532 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
2534 mvpp2_write(port->priv, MVPP22_BM_ADDR_HIGH_RLS_REG, val);
2537 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
2538 * returned in the "cookie" field of the RX
2539 * descriptor. Instead of storing the virtual address, we
2540 * store the physical address
2542 mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
2543 mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
2546 /* Refill BM pool */
2547 static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
2548 dma_addr_t dma_addr,
2549 phys_addr_t phys_addr)
2551 int pool = mvpp2_bm_cookie_pool_get(bm);
2553 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
2556 /* Allocate buffers for the pool */
2557 static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
2558 struct mvpp2_bm_pool *bm_pool, int buf_num)
2563 (buf_num + bm_pool->buf_num > bm_pool->size)) {
2564 netdev_err(port->dev,
2565 "cannot allocate %d buffers for pool %d\n",
2566 buf_num, bm_pool->id);
2570 for (i = 0; i < buf_num; i++) {
2571 mvpp2_bm_pool_put(port, bm_pool->id,
2572 (dma_addr_t)buffer_loc.rx_buffer[i],
2573 (unsigned long)buffer_loc.rx_buffer[i]);
2577 /* Update BM driver with number of buffers added to pool */
2578 bm_pool->buf_num += i;
2579 bm_pool->in_use_thresh = bm_pool->buf_num / 4;
2584 /* Notify the driver that BM pool is being used as specific type and return the
2585 * pool pointer on success
2587 static struct mvpp2_bm_pool *
2588 mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
2591 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
2594 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
2595 netdev_err(port->dev, "mixing pool types is forbidden\n");
2599 if (new_pool->type == MVPP2_BM_FREE)
2600 new_pool->type = type;
2602 /* Allocate buffers in case BM pool is used as long pool, but packet
2603 * size doesn't match MTU or BM pool hasn't being used yet
2605 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
2606 (new_pool->pkt_size == 0)) {
2609 /* Set default buffer number or free all the buffers in case
2610 * the pool is not empty
2612 pkts_num = new_pool->buf_num;
2614 pkts_num = type == MVPP2_BM_SWF_LONG ?
2615 MVPP2_BM_LONG_BUF_NUM :
2616 MVPP2_BM_SHORT_BUF_NUM;
2618 mvpp2_bm_bufs_free(NULL,
2619 port->priv, new_pool);
2621 new_pool->pkt_size = pkt_size;
2623 /* Allocate buffers for this pool */
2624 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
2625 if (num != pkts_num) {
2626 dev_err(dev, "pool %d: %d of %d allocated\n",
2627 new_pool->id, num, pkts_num);
2632 mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
2633 MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
2638 /* Initialize pools for swf */
2639 static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
2643 if (!port->pool_long) {
2645 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
2648 if (!port->pool_long)
2651 port->pool_long->port_map |= (1 << port->id);
2653 for (rxq = 0; rxq < rxq_number; rxq++)
2654 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
2660 /* Port configuration routines */
2662 static void mvpp2_port_mii_set(struct mvpp2_port *port)
2666 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
2668 switch (port->phy_interface) {
2669 case PHY_INTERFACE_MODE_SGMII:
2670 val |= MVPP2_GMAC_INBAND_AN_MASK;
2672 case PHY_INTERFACE_MODE_RGMII:
2673 val |= MVPP2_GMAC_PORT_RGMII_MASK;
2675 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
2678 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2681 static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
2685 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2686 val |= MVPP2_GMAC_FC_ADV_EN;
2687 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2690 static void mvpp2_port_enable(struct mvpp2_port *port)
2694 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2695 val |= MVPP2_GMAC_PORT_EN_MASK;
2696 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
2697 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2700 static void mvpp2_port_disable(struct mvpp2_port *port)
2704 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2705 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
2706 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2709 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
2710 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
2714 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
2715 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
2716 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2719 /* Configure loopback port */
2720 static void mvpp2_port_loopback_set(struct mvpp2_port *port)
2724 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
2726 if (port->speed == 1000)
2727 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
2729 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
2731 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
2732 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
2734 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
2736 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2739 static void mvpp2_port_reset(struct mvpp2_port *port)
2743 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2744 ~MVPP2_GMAC_PORT_RESET_MASK;
2745 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2747 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2748 MVPP2_GMAC_PORT_RESET_MASK)
2752 /* Change maximum receive size of the port */
2753 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
2757 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2758 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
2759 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2760 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
2761 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2764 /* Set defaults to the MVPP2 port */
2765 static void mvpp2_defaults_set(struct mvpp2_port *port)
2767 int tx_port_num, val, queue, ptxq, lrxq;
2769 if (port->priv->hw_version == MVPP21) {
2770 /* Configure port to loopback if needed */
2771 if (port->flags & MVPP2_F_LOOPBACK)
2772 mvpp2_port_loopback_set(port);
2774 /* Update TX FIFO MIN Threshold */
2775 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2776 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
2777 /* Min. TX threshold must be less than minimal packet length */
2778 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
2779 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2782 /* Disable Legacy WRR, Disable EJP, Release from reset */
2783 tx_port_num = mvpp2_egress_port(port);
2784 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
2786 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
2788 /* Close bandwidth for all queues */
2789 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
2790 ptxq = mvpp2_txq_phys(port->id, queue);
2791 mvpp2_write(port->priv,
2792 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
2795 /* Set refill period to 1 usec, refill tokens
2796 * and bucket size to maximum
2798 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
2799 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
2800 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
2801 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
2802 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
2803 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
2804 val = MVPP2_TXP_TOKEN_SIZE_MAX;
2805 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2807 /* Set MaximumLowLatencyPacketSize value to 256 */
2808 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
2809 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
2810 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
2812 /* Enable Rx cache snoop */
2813 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2814 queue = port->rxqs[lrxq]->id;
2815 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2816 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
2817 MVPP2_SNOOP_BUF_HDR_MASK;
2818 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2822 /* Enable/disable receiving packets */
2823 static void mvpp2_ingress_enable(struct mvpp2_port *port)
2828 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2829 queue = port->rxqs[lrxq]->id;
2830 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2831 val &= ~MVPP2_RXQ_DISABLE_MASK;
2832 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2836 static void mvpp2_ingress_disable(struct mvpp2_port *port)
2841 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2842 queue = port->rxqs[lrxq]->id;
2843 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2844 val |= MVPP2_RXQ_DISABLE_MASK;
2845 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2849 /* Enable transmit via physical egress queue
2850 * - HW starts take descriptors from DRAM
2852 static void mvpp2_egress_enable(struct mvpp2_port *port)
2856 int tx_port_num = mvpp2_egress_port(port);
2858 /* Enable all initialized TXs. */
2860 for (queue = 0; queue < txq_number; queue++) {
2861 struct mvpp2_tx_queue *txq = port->txqs[queue];
2863 if (txq->descs != NULL)
2864 qmap |= (1 << queue);
2867 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2868 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
2871 /* Disable transmit via physical egress queue
2872 * - HW doesn't take descriptors from DRAM
2874 static void mvpp2_egress_disable(struct mvpp2_port *port)
2878 int tx_port_num = mvpp2_egress_port(port);
2880 /* Issue stop command for active channels only */
2881 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2882 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
2883 MVPP2_TXP_SCHED_ENQ_MASK;
2885 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
2886 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
2888 /* Wait for all Tx activity to terminate. */
2891 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
2892 netdev_warn(port->dev,
2893 "Tx stop timed out, status=0x%08x\n",
2900 /* Check port TX Command register that all
2901 * Tx queues are stopped
2903 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
2904 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
2907 /* Rx descriptors helper methods */
2909 /* Get number of Rx descriptors occupied by received packets */
2911 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
2913 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
2915 return val & MVPP2_RXQ_OCCUPIED_MASK;
2918 /* Update Rx queue status with the number of occupied and available
2919 * Rx descriptor slots.
2922 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
2923 int used_count, int free_count)
2925 /* Decrement the number of used descriptors and increment count
2926 * increment the number of free descriptors.
2928 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
2930 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
2933 /* Get pointer to next RX descriptor to be processed by SW */
2934 static inline struct mvpp2_rx_desc *
2935 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
2937 int rx_desc = rxq->next_desc_to_proc;
2939 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
2940 prefetch(rxq->descs + rxq->next_desc_to_proc);
2941 return rxq->descs + rx_desc;
2944 /* Set rx queue offset */
2945 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
2946 int prxq, int offset)
2950 /* Convert offset from bytes to units of 32 bytes */
2951 offset = offset >> 5;
2953 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2954 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
2957 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
2958 MVPP2_RXQ_PACKET_OFFSET_MASK);
2960 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2963 /* Obtain BM cookie information from descriptor */
2964 static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
2965 struct mvpp2_rx_desc *rx_desc)
2967 int cpu = smp_processor_id();
2970 pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
2971 MVPP2_RXD_BM_POOL_ID_MASK) >>
2972 MVPP2_RXD_BM_POOL_ID_OFFS;
2974 return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
2975 ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
2978 /* Tx descriptors helper methods */
2980 /* Get number of Tx descriptors waiting to be transmitted by HW */
2981 static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
2982 struct mvpp2_tx_queue *txq)
2986 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
2987 val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
2989 return val & MVPP2_TXQ_PENDING_MASK;
2992 /* Get pointer to next Tx descriptor to be processed (send) by HW */
2993 static struct mvpp2_tx_desc *
2994 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
2996 int tx_desc = txq->next_desc_to_proc;
2998 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
2999 return txq->descs + tx_desc;
3002 /* Update HW with number of aggregated Tx descriptors to be sent */
3003 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
3005 /* aggregated access - relevant TXQ number is written in TX desc */
3006 mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
3009 /* Get number of sent descriptors and decrement counter.
3010 * The number of sent descriptors is returned.
3013 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
3014 struct mvpp2_tx_queue *txq)
3018 /* Reading status reg resets transmitted descriptor counter */
3019 val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
3021 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
3022 MVPP2_TRANSMITTED_COUNT_OFFSET;
3025 static void mvpp2_txq_sent_counter_clear(void *arg)
3027 struct mvpp2_port *port = arg;
3030 for (queue = 0; queue < txq_number; queue++) {
3031 int id = port->txqs[queue]->id;
3033 mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
3037 /* Set max sizes for Tx queues */
3038 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
3041 int txq, tx_port_num;
3043 mtu = port->pkt_size * 8;
3044 if (mtu > MVPP2_TXP_MTU_MAX)
3045 mtu = MVPP2_TXP_MTU_MAX;
3047 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
3050 /* Indirect access to registers */
3051 tx_port_num = mvpp2_egress_port(port);
3052 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3055 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
3056 val &= ~MVPP2_TXP_MTU_MAX;
3058 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
3060 /* TXP token size and all TXQs token size must be larger that MTU */
3061 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
3062 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
3065 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
3067 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
3070 for (txq = 0; txq < txq_number; txq++) {
3071 val = mvpp2_read(port->priv,
3072 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
3073 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
3077 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
3079 mvpp2_write(port->priv,
3080 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
3086 /* Free Tx queue skbuffs */
3087 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
3088 struct mvpp2_tx_queue *txq,
3089 struct mvpp2_txq_pcpu *txq_pcpu, int num)
3093 for (i = 0; i < num; i++)
3094 mvpp2_txq_inc_get(txq_pcpu);
3097 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
3100 int queue = fls(cause) - 1;
3102 return port->rxqs[queue];
3105 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
3108 int queue = fls(cause) - 1;
3110 return port->txqs[queue];
3113 /* Rx/Tx queue initialization/cleanup methods */
3115 /* Allocate and initialize descriptors for aggr TXQ */
3116 static int mvpp2_aggr_txq_init(struct udevice *dev,
3117 struct mvpp2_tx_queue *aggr_txq,
3118 int desc_num, int cpu,
3123 /* Allocate memory for TX descriptors */
3124 aggr_txq->descs = buffer_loc.aggr_tx_descs;
3125 aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
3126 if (!aggr_txq->descs)
3129 /* Make sure descriptor address is cache line size aligned */
3130 BUG_ON(aggr_txq->descs !=
3131 PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3133 aggr_txq->last_desc = aggr_txq->size - 1;
3135 /* Aggr TXQ no reset WA */
3136 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
3137 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
3139 /* Set Tx descriptors queue starting address indirect
3142 if (priv->hw_version == MVPP21)
3143 txq_dma = aggr_txq->descs_dma;
3145 txq_dma = aggr_txq->descs_dma >>
3146 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
3148 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
3149 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
3154 /* Create a specified Rx queue */
3155 static int mvpp2_rxq_init(struct mvpp2_port *port,
3156 struct mvpp2_rx_queue *rxq)
3161 rxq->size = port->rx_ring_size;
3163 /* Allocate memory for RX descriptors */
3164 rxq->descs = buffer_loc.rx_descs;
3165 rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
3169 BUG_ON(rxq->descs !=
3170 PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3172 rxq->last_desc = rxq->size - 1;
3174 /* Zero occupied and non-occupied counters - direct access */
3175 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3177 /* Set Rx descriptors queue starting address - indirect access */
3178 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
3179 if (port->priv->hw_version == MVPP21)
3180 rxq_dma = rxq->descs_dma;
3182 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
3183 mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
3184 mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
3185 mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
3188 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
3190 /* Add number of descriptors ready for receiving packets */
3191 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
3196 /* Push packets received by the RXQ to BM pool */
3197 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
3198 struct mvpp2_rx_queue *rxq)
3202 rx_received = mvpp2_rxq_received(port, rxq->id);
3206 for (i = 0; i < rx_received; i++) {
3207 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3208 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
3210 mvpp2_pool_refill(port, bm,
3211 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
3212 mvpp2_rxdesc_cookie_get(port, rx_desc));
3214 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
3217 /* Cleanup Rx queue */
3218 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
3219 struct mvpp2_rx_queue *rxq)
3221 mvpp2_rxq_drop_pkts(port, rxq);
3225 rxq->next_desc_to_proc = 0;
3228 /* Clear Rx descriptors queue starting address and size;
3229 * free descriptor number
3231 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3232 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
3233 mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
3234 mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
3237 /* Create and initialize a Tx queue */
3238 static int mvpp2_txq_init(struct mvpp2_port *port,
3239 struct mvpp2_tx_queue *txq)
3242 int cpu, desc, desc_per_txq, tx_port_num;
3243 struct mvpp2_txq_pcpu *txq_pcpu;
3245 txq->size = port->tx_ring_size;
3247 /* Allocate memory for Tx descriptors */
3248 txq->descs = buffer_loc.tx_descs;
3249 txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
3253 /* Make sure descriptor address is cache line size aligned */
3254 BUG_ON(txq->descs !=
3255 PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3257 txq->last_desc = txq->size - 1;
3259 /* Set Tx descriptors queue starting address - indirect access */
3260 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3261 mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
3262 mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
3263 MVPP2_TXQ_DESC_SIZE_MASK);
3264 mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
3265 mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
3266 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
3267 val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
3268 val &= ~MVPP2_TXQ_PENDING_MASK;
3269 mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
3271 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
3272 * for each existing TXQ.
3273 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
3274 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
3277 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
3278 (txq->log_id * desc_per_txq);
3280 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
3281 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
3282 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
3284 /* WRR / EJP configuration - indirect access */
3285 tx_port_num = mvpp2_egress_port(port);
3286 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3288 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
3289 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
3290 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
3291 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
3292 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
3294 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
3295 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
3298 for_each_present_cpu(cpu) {
3299 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3300 txq_pcpu->size = txq->size;
3306 /* Free allocated TXQ resources */
3307 static void mvpp2_txq_deinit(struct mvpp2_port *port,
3308 struct mvpp2_tx_queue *txq)
3312 txq->next_desc_to_proc = 0;
3315 /* Set minimum bandwidth for disabled TXQs */
3316 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
3318 /* Set Tx descriptors queue starting address and size */
3319 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3320 mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
3321 mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
3324 /* Cleanup Tx ports */
3325 static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
3327 struct mvpp2_txq_pcpu *txq_pcpu;
3328 int delay, pending, cpu;
3331 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3332 val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
3333 val |= MVPP2_TXQ_DRAIN_EN_MASK;
3334 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
3336 /* The napi queue has been stopped so wait for all packets
3337 * to be transmitted.
3341 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
3342 netdev_warn(port->dev,
3343 "port %d: cleaning queue %d timed out\n",
3344 port->id, txq->log_id);
3350 pending = mvpp2_txq_pend_desc_num_get(port, txq);
3353 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
3354 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
3356 for_each_present_cpu(cpu) {
3357 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3359 /* Release all packets */
3360 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
3363 txq_pcpu->count = 0;
3364 txq_pcpu->txq_put_index = 0;
3365 txq_pcpu->txq_get_index = 0;
3369 /* Cleanup all Tx queues */
3370 static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
3372 struct mvpp2_tx_queue *txq;
3376 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
3378 /* Reset Tx ports and delete Tx queues */
3379 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
3380 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3382 for (queue = 0; queue < txq_number; queue++) {
3383 txq = port->txqs[queue];
3384 mvpp2_txq_clean(port, txq);
3385 mvpp2_txq_deinit(port, txq);
3388 mvpp2_txq_sent_counter_clear(port);
3390 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
3391 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3394 /* Cleanup all Rx queues */
3395 static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
3399 for (queue = 0; queue < rxq_number; queue++)
3400 mvpp2_rxq_deinit(port, port->rxqs[queue]);
3403 /* Init all Rx queues for port */
3404 static int mvpp2_setup_rxqs(struct mvpp2_port *port)
3408 for (queue = 0; queue < rxq_number; queue++) {
3409 err = mvpp2_rxq_init(port, port->rxqs[queue]);
3416 mvpp2_cleanup_rxqs(port);
3420 /* Init all tx queues for port */
3421 static int mvpp2_setup_txqs(struct mvpp2_port *port)
3423 struct mvpp2_tx_queue *txq;
3426 for (queue = 0; queue < txq_number; queue++) {
3427 txq = port->txqs[queue];
3428 err = mvpp2_txq_init(port, txq);
3433 mvpp2_txq_sent_counter_clear(port);
3437 mvpp2_cleanup_txqs(port);
3442 static void mvpp2_link_event(struct mvpp2_port *port)
3444 struct phy_device *phydev = port->phy_dev;
3445 int status_change = 0;
3449 if ((port->speed != phydev->speed) ||
3450 (port->duplex != phydev->duplex)) {
3453 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3454 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
3455 MVPP2_GMAC_CONFIG_GMII_SPEED |
3456 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
3457 MVPP2_GMAC_AN_SPEED_EN |
3458 MVPP2_GMAC_AN_DUPLEX_EN);
3461 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
3463 if (phydev->speed == SPEED_1000)
3464 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
3465 else if (phydev->speed == SPEED_100)
3466 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
3468 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3470 port->duplex = phydev->duplex;
3471 port->speed = phydev->speed;
3475 if (phydev->link != port->link) {
3476 if (!phydev->link) {
3481 port->link = phydev->link;
3485 if (status_change) {
3487 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3488 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
3489 MVPP2_GMAC_FORCE_LINK_DOWN);
3490 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3491 mvpp2_egress_enable(port);
3492 mvpp2_ingress_enable(port);
3494 mvpp2_ingress_disable(port);
3495 mvpp2_egress_disable(port);
3500 /* Main RX/TX processing routines */
3502 /* Display more error info */
3503 static void mvpp2_rx_error(struct mvpp2_port *port,
3504 struct mvpp2_rx_desc *rx_desc)
3506 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3507 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3509 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
3510 case MVPP2_RXD_ERR_CRC:
3511 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
3514 case MVPP2_RXD_ERR_OVERRUN:
3515 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
3518 case MVPP2_RXD_ERR_RESOURCE:
3519 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
3525 /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
3526 static int mvpp2_rx_refill(struct mvpp2_port *port,
3527 struct mvpp2_bm_pool *bm_pool,
3528 u32 bm, dma_addr_t dma_addr)
3530 mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
3534 /* Set hw internals when starting port */
3535 static void mvpp2_start_dev(struct mvpp2_port *port)
3537 mvpp2_gmac_max_rx_size_set(port);
3538 mvpp2_txp_max_tx_size_set(port);
3540 mvpp2_port_enable(port);
3543 /* Set hw internals when stopping port */
3544 static void mvpp2_stop_dev(struct mvpp2_port *port)
3546 /* Stop new packets from arriving to RXQs */
3547 mvpp2_ingress_disable(port);
3549 mvpp2_egress_disable(port);
3550 mvpp2_port_disable(port);
3553 static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
3555 struct phy_device *phy_dev;
3557 if (!port->init || port->link == 0) {
3558 phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
3559 port->phy_interface);
3560 port->phy_dev = phy_dev;
3562 netdev_err(port->dev, "cannot connect to phy\n");
3565 phy_dev->supported &= PHY_GBIT_FEATURES;
3566 phy_dev->advertising = phy_dev->supported;
3568 port->phy_dev = phy_dev;
3573 phy_config(phy_dev);
3574 phy_startup(phy_dev);
3575 if (!phy_dev->link) {
3576 printf("%s: No link\n", phy_dev->dev->name);
3582 mvpp2_egress_enable(port);
3583 mvpp2_ingress_enable(port);
3589 static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
3591 unsigned char mac_bcast[ETH_ALEN] = {
3592 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3595 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
3597 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
3600 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
3601 port->dev_addr, true);
3603 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
3606 err = mvpp2_prs_def_flow(port);
3608 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
3612 /* Allocate the Rx/Tx queues */
3613 err = mvpp2_setup_rxqs(port);
3615 netdev_err(port->dev, "cannot allocate Rx queues\n");
3619 err = mvpp2_setup_txqs(port);
3621 netdev_err(port->dev, "cannot allocate Tx queues\n");
3625 err = mvpp2_phy_connect(dev, port);
3629 mvpp2_link_event(port);
3631 mvpp2_start_dev(port);
3636 /* No Device ops here in U-Boot */
3638 /* Driver initialization */
3640 static void mvpp2_port_power_up(struct mvpp2_port *port)
3642 mvpp2_port_mii_set(port);
3643 mvpp2_port_periodic_xon_disable(port);
3644 mvpp2_port_fc_adv_enable(port);
3645 mvpp2_port_reset(port);
3648 /* Initialize port HW */
3649 static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
3651 struct mvpp2 *priv = port->priv;
3652 struct mvpp2_txq_pcpu *txq_pcpu;
3653 int queue, cpu, err;
3655 if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
3659 mvpp2_egress_disable(port);
3660 mvpp2_port_disable(port);
3662 port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
3667 /* Associate physical Tx queues to this port and initialize.
3668 * The mapping is predefined.
3670 for (queue = 0; queue < txq_number; queue++) {
3671 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
3672 struct mvpp2_tx_queue *txq;
3674 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
3678 txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
3683 txq->id = queue_phy_id;
3684 txq->log_id = queue;
3685 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
3686 for_each_present_cpu(cpu) {
3687 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3688 txq_pcpu->cpu = cpu;
3691 port->txqs[queue] = txq;
3694 port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
3699 /* Allocate and initialize Rx queue for this port */
3700 for (queue = 0; queue < rxq_number; queue++) {
3701 struct mvpp2_rx_queue *rxq;
3703 /* Map physical Rx queue to port's logical Rx queue */
3704 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
3707 /* Map this Rx queue to a physical queue */
3708 rxq->id = port->first_rxq + queue;
3709 rxq->port = port->id;
3710 rxq->logic_rxq = queue;
3712 port->rxqs[queue] = rxq;
3715 /* Configure Rx queue group interrupt for this port */
3716 mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
3718 /* Create Rx descriptor rings */
3719 for (queue = 0; queue < rxq_number; queue++) {
3720 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
3722 rxq->size = port->rx_ring_size;
3723 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
3724 rxq->time_coal = MVPP2_RX_COAL_USEC;
3727 mvpp2_ingress_disable(port);
3729 /* Port default configuration */
3730 mvpp2_defaults_set(port);
3732 /* Port's classifier configuration */
3733 mvpp2_cls_oversize_rxq_set(port);
3734 mvpp2_cls_port_config(port);
3736 /* Provide an initial Rx packet size */
3737 port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
3739 /* Initialize pools for swf */
3740 err = mvpp2_swf_bm_pool_init(port);
3747 /* Ports initialization */
3748 static int mvpp2_port_probe(struct udevice *dev,
3749 struct mvpp2_port *port,
3752 int *next_first_rxq)
3757 const char *phy_mode_str;
3759 int priv_common_regs_num = 2;
3762 phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
3764 dev_err(&pdev->dev, "missing phy\n");
3768 phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
3770 phy_mode = phy_get_interface_by_name(phy_mode_str);
3771 if (phy_mode == -1) {
3772 dev_err(&pdev->dev, "incorrect phy mode\n");
3776 id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
3778 dev_err(&pdev->dev, "missing port-id value\n");
3782 phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
3786 port->first_rxq = *next_first_rxq;
3787 port->phy_node = phy_node;
3788 port->phy_interface = phy_mode;
3789 port->phyaddr = phyaddr;
3791 if (priv->hw_version == MVPP21) {
3792 port->base = (void __iomem *)dev_get_addr_index(
3793 dev->parent, priv_common_regs_num + id);
3794 if (IS_ERR(port->base))
3795 return PTR_ERR(port->base);
3799 gop_id = fdtdec_get_int(gd->fdt_blob, port_node,
3802 dev_err(&pdev->dev, "missing gop-port-id value\n");
3806 port->base = priv->iface_base + MVPP22_PORT_BASE +
3807 gop_id * MVPP22_PORT_OFFSET;
3810 port->tx_ring_size = MVPP2_MAX_TXD;
3811 port->rx_ring_size = MVPP2_MAX_RXD;
3813 err = mvpp2_port_init(dev, port);
3815 dev_err(&pdev->dev, "failed to init port %d\n", id);
3818 mvpp2_port_power_up(port);
3820 /* Increment the first Rx queue number to be used by the next port */
3821 *next_first_rxq += CONFIG_MV_ETH_RXQ;
3822 priv->port_list[id] = port;
3826 /* Initialize decoding windows */
3827 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
3833 for (i = 0; i < 6; i++) {
3834 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
3835 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
3838 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
3843 for (i = 0; i < dram->num_cs; i++) {
3844 const struct mbus_dram_window *cs = dram->cs + i;
3846 mvpp2_write(priv, MVPP2_WIN_BASE(i),
3847 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
3848 dram->mbus_dram_target_id);
3850 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
3851 (cs->size - 1) & 0xffff0000);
3853 win_enable |= (1 << i);
3856 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
3859 /* Initialize Rx FIFO's */
3860 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
3864 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
3865 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
3866 MVPP2_RX_FIFO_PORT_DATA_SIZE);
3867 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
3868 MVPP2_RX_FIFO_PORT_ATTR_SIZE);
3871 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
3872 MVPP2_RX_FIFO_PORT_MIN_PKT);
3873 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
3876 /* Initialize network controller common part HW */
3877 static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
3879 const struct mbus_dram_target_info *dram_target_info;
3883 /* Checks for hardware constraints (U-Boot uses only one rxq) */
3884 if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
3885 dev_err(&pdev->dev, "invalid queue size parameter\n");
3889 /* MBUS windows configuration */
3890 dram_target_info = mvebu_mbus_dram_info();
3891 if (dram_target_info)
3892 mvpp2_conf_mbus_windows(dram_target_info, priv);
3894 /* Disable HW PHY polling */
3895 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
3896 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
3897 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
3899 /* Allocate and initialize aggregated TXQs */
3900 priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
3901 sizeof(struct mvpp2_tx_queue),
3903 if (!priv->aggr_txqs)
3906 for_each_present_cpu(i) {
3907 priv->aggr_txqs[i].id = i;
3908 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
3909 err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
3910 MVPP2_AGGR_TXQ_SIZE, i, priv);
3916 mvpp2_rx_fifo_init(priv);
3918 /* Reset Rx queue group interrupt configuration */
3919 for (i = 0; i < MVPP2_MAX_PORTS; i++)
3920 mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
3923 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
3924 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
3926 /* Allow cache snoop when transmiting packets */
3927 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
3929 /* Buffer Manager initialization */
3930 err = mvpp2_bm_init(dev, priv);
3934 /* Parser default initialization */
3935 err = mvpp2_prs_default_init(dev, priv);
3939 /* Classifier default initialization */
3940 mvpp2_cls_init(priv);
3945 /* SMI / MDIO functions */
3947 static int smi_wait_ready(struct mvpp2 *priv)
3949 u32 timeout = MVPP2_SMI_TIMEOUT;
3952 /* wait till the SMI is not busy */
3954 /* read smi register */
3955 smi_reg = readl(priv->lms_base + MVPP2_SMI);
3956 if (timeout-- == 0) {
3957 printf("Error: SMI busy timeout\n");
3960 } while (smi_reg & MVPP2_SMI_BUSY);
3966 * mpp2_mdio_read - miiphy_read callback function.
3968 * Returns 16bit phy register value, or 0xffff on error
3970 static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
3972 struct mvpp2 *priv = bus->priv;
3976 /* check parameters */
3977 if (addr > MVPP2_PHY_ADDR_MASK) {
3978 printf("Error: Invalid PHY address %d\n", addr);
3982 if (reg > MVPP2_PHY_REG_MASK) {
3983 printf("Err: Invalid register offset %d\n", reg);
3987 /* wait till the SMI is not busy */
3988 if (smi_wait_ready(priv) < 0)
3991 /* fill the phy address and regiser offset and read opcode */
3992 smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
3993 | (reg << MVPP2_SMI_REG_ADDR_OFFS)
3994 | MVPP2_SMI_OPCODE_READ;
3996 /* write the smi register */
3997 writel(smi_reg, priv->lms_base + MVPP2_SMI);
3999 /* wait till read value is ready */
4000 timeout = MVPP2_SMI_TIMEOUT;
4003 /* read smi register */
4004 smi_reg = readl(priv->lms_base + MVPP2_SMI);
4005 if (timeout-- == 0) {
4006 printf("Err: SMI read ready timeout\n");
4009 } while (!(smi_reg & MVPP2_SMI_READ_VALID));
4011 /* Wait for the data to update in the SMI register */
4012 for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
4015 return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
4019 * mpp2_mdio_write - miiphy_write callback function.
4021 * Returns 0 if write succeed, -EINVAL on bad parameters
4024 static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
4027 struct mvpp2 *priv = bus->priv;
4030 /* check parameters */
4031 if (addr > MVPP2_PHY_ADDR_MASK) {
4032 printf("Error: Invalid PHY address %d\n", addr);
4036 if (reg > MVPP2_PHY_REG_MASK) {
4037 printf("Err: Invalid register offset %d\n", reg);
4041 /* wait till the SMI is not busy */
4042 if (smi_wait_ready(priv) < 0)
4045 /* fill the phy addr and reg offset and write opcode and data */
4046 smi_reg = value << MVPP2_SMI_DATA_OFFS;
4047 smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
4048 | (reg << MVPP2_SMI_REG_ADDR_OFFS);
4049 smi_reg &= ~MVPP2_SMI_OPCODE_READ;
4051 /* write the smi register */
4052 writel(smi_reg, priv->lms_base + MVPP2_SMI);
4057 static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
4059 struct mvpp2_port *port = dev_get_priv(dev);
4060 struct mvpp2_rx_desc *rx_desc;
4061 struct mvpp2_bm_pool *bm_pool;
4062 dma_addr_t dma_addr;
4064 int pool, rx_bytes, err;
4066 struct mvpp2_rx_queue *rxq;
4067 u32 cause_rx_tx, cause_rx, cause_misc;
4070 cause_rx_tx = mvpp2_read(port->priv,
4071 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
4072 cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
4073 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
4074 if (!cause_rx_tx && !cause_misc)
4077 cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4079 /* Process RX packets */
4080 cause_rx |= port->pending_cause_rx;
4081 rxq = mvpp2_get_rx_queue(port, cause_rx);
4083 /* Get number of received packets and clamp the to-do */
4084 rx_received = mvpp2_rxq_received(port, rxq->id);
4086 /* Return if no packets are received */
4090 rx_desc = mvpp2_rxq_next_desc_get(rxq);
4091 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
4092 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
4093 rx_bytes -= MVPP2_MH_SIZE;
4094 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
4096 bm = mvpp2_bm_cookie_build(port, rx_desc);
4097 pool = mvpp2_bm_cookie_pool_get(bm);
4098 bm_pool = &port->priv->bm_pools[pool];
4100 /* In case of an error, release the requested buffer pointer
4101 * to the Buffer Manager. This request process is controlled
4102 * by the hardware, and the information about the buffer is
4103 * comprised by the RX descriptor.
4105 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
4106 mvpp2_rx_error(port, rx_desc);
4107 /* Return the buffer to the pool */
4108 mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
4112 err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
4114 netdev_err(port->dev, "failed to refill BM pools\n");
4118 /* Update Rx queue management counters */
4120 mvpp2_rxq_status_update(port, rxq->id, 1, 1);
4122 /* give packet to stack - skip on first n bytes */
4123 data = (u8 *)dma_addr + 2 + 32;
4129 * No cache invalidation needed here, since the rx_buffer's are
4130 * located in a uncached memory region
4138 static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4143 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4144 val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
4146 val |= MVPP2_TXQ_DRAIN_EN_MASK;
4148 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
4149 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4152 static int mvpp2_send(struct udevice *dev, void *packet, int length)
4154 struct mvpp2_port *port = dev_get_priv(dev);
4155 struct mvpp2_tx_queue *txq, *aggr_txq;
4156 struct mvpp2_tx_desc *tx_desc;
4160 txq = port->txqs[0];
4161 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
4163 /* Get a descriptor for the first part of the packet */
4164 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4165 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4166 mvpp2_txdesc_size_set(port, tx_desc, length);
4167 mvpp2_txdesc_offset_set(port, tx_desc,
4168 (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
4169 mvpp2_txdesc_dma_addr_set(port, tx_desc,
4170 (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
4171 /* First and Last descriptor */
4172 mvpp2_txdesc_cmd_set(port, tx_desc,
4173 MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
4174 | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
4177 flush_dcache_range((unsigned long)packet,
4178 (unsigned long)packet + ALIGN(length, PKTALIGN));
4180 /* Enable transmit */
4182 mvpp2_aggr_txq_pend_desc_add(port, 1);
4184 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4188 if (timeout++ > 10000) {
4189 printf("timeout: packet not sent from aggregated to phys TXQ\n");
4192 tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
4195 /* Enable TXQ drain */
4196 mvpp2_txq_drain(port, txq, 1);
4200 if (timeout++ > 10000) {
4201 printf("timeout: packet not sent\n");
4204 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4207 /* Disable TXQ drain */
4208 mvpp2_txq_drain(port, txq, 0);
4213 static int mvpp2_start(struct udevice *dev)
4215 struct eth_pdata *pdata = dev_get_platdata(dev);
4216 struct mvpp2_port *port = dev_get_priv(dev);
4218 /* Load current MAC address */
4219 memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
4221 /* Reconfigure parser accept the original MAC address */
4222 mvpp2_prs_update_mac_da(port, port->dev_addr);
4224 mvpp2_port_power_up(port);
4226 mvpp2_open(dev, port);
4231 static void mvpp2_stop(struct udevice *dev)
4233 struct mvpp2_port *port = dev_get_priv(dev);
4235 mvpp2_stop_dev(port);
4236 mvpp2_cleanup_rxqs(port);
4237 mvpp2_cleanup_txqs(port);
4240 static int mvpp2_probe(struct udevice *dev)
4242 struct mvpp2_port *port = dev_get_priv(dev);
4243 struct mvpp2 *priv = dev_get_priv(dev->parent);
4246 /* Initialize network controller */
4247 err = mvpp2_init(dev, priv);
4249 dev_err(&pdev->dev, "failed to initialize controller\n");
4253 return mvpp2_port_probe(dev, port, dev_of_offset(dev), priv,
4254 &buffer_loc.first_rxq);
4257 static const struct eth_ops mvpp2_ops = {
4258 .start = mvpp2_start,
4264 static struct driver mvpp2_driver = {
4267 .probe = mvpp2_probe,
4269 .priv_auto_alloc_size = sizeof(struct mvpp2_port),
4270 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
4274 * Use a MISC device to bind the n instances (child nodes) of the
4275 * network base controller in UCLASS_ETH.
4277 static int mvpp2_base_probe(struct udevice *dev)
4279 struct mvpp2 *priv = dev_get_priv(dev);
4280 struct mii_dev *bus;
4285 /* Save hw-version */
4286 priv->hw_version = dev_get_driver_data(dev);
4289 * U-Boot special buffer handling:
4291 * Allocate buffer area for descs and rx_buffers. This is only
4292 * done once for all interfaces. As only one interface can
4293 * be active. Make this area DMA-safe by disabling the D-cache
4296 /* Align buffer area for descs and rx_buffers to 1MiB */
4297 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
4298 mmu_set_region_dcache_behaviour((unsigned long)bd_space,
4299 BD_SPACE, DCACHE_OFF);
4301 buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
4302 size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
4304 buffer_loc.tx_descs =
4305 (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
4306 size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
4308 buffer_loc.rx_descs =
4309 (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
4310 size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
4312 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4313 buffer_loc.bm_pool[i] =
4314 (unsigned long *)((unsigned long)bd_space + size);
4315 if (priv->hw_version == MVPP21)
4316 size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u32);
4318 size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u64);
4321 for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
4322 buffer_loc.rx_buffer[i] =
4323 (unsigned long *)((unsigned long)bd_space + size);
4324 size += RX_BUFFER_SIZE;
4327 /* Save base addresses for later use */
4328 priv->base = (void *)dev_get_addr_index(dev, 0);
4329 if (IS_ERR(priv->base))
4330 return PTR_ERR(priv->base);
4332 if (priv->hw_version == MVPP21) {
4333 priv->lms_base = (void *)dev_get_addr_index(dev, 1);
4334 if (IS_ERR(priv->lms_base))
4335 return PTR_ERR(priv->lms_base);
4337 priv->iface_base = (void *)dev_get_addr_index(dev, 1);
4338 if (IS_ERR(priv->iface_base))
4339 return PTR_ERR(priv->iface_base);
4342 /* Finally create and register the MDIO bus driver */
4345 printf("Failed to allocate MDIO bus\n");
4349 bus->read = mpp2_mdio_read;
4350 bus->write = mpp2_mdio_write;
4351 snprintf(bus->name, sizeof(bus->name), dev->name);
4352 bus->priv = (void *)priv;
4355 return mdio_register(bus);
4358 static int mvpp2_base_bind(struct udevice *parent)
4360 const void *blob = gd->fdt_blob;
4361 int node = dev_of_offset(parent);
4362 struct uclass_driver *drv;
4363 struct udevice *dev;
4364 struct eth_pdata *plat;
4369 /* Lookup eth driver */
4370 drv = lists_uclass_lookup(UCLASS_ETH);
4372 puts("Cannot find eth driver\n");
4376 fdt_for_each_subnode(subnode, blob, node) {
4377 /* Skip disabled ports */
4378 if (!fdtdec_get_is_enabled(blob, subnode))
4381 plat = calloc(1, sizeof(*plat));
4385 id = fdtdec_get_int(blob, subnode, "port-id", -1);
4387 name = calloc(1, 16);
4388 sprintf(name, "mvpp2-%d", id);
4390 /* Create child device UCLASS_ETH and bind it */
4391 device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
4392 dev_set_of_offset(dev, subnode);
4398 static const struct udevice_id mvpp2_ids[] = {
4400 .compatible = "marvell,armada-375-pp2",
4406 U_BOOT_DRIVER(mvpp2_base) = {
4407 .name = "mvpp2_base",
4409 .of_match = mvpp2_ids,
4410 .bind = mvpp2_base_bind,
4411 .probe = mvpp2_base_probe,
4412 .priv_auto_alloc_size = sizeof(struct mvpp2),