blob: 1cf522b8fe571d04da34cdede878ed20c15cc91a [file] [log] [blame]
Stefan Roese96c19042016-02-10 07:22:10 +01001/*
2 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
3 *
4 * Copyright (C) 2014 Marvell
5 *
6 * Marcin Wojtas <mw@semihalf.com>
7 *
8 * U-Boot version:
Stefan Roese38801d42017-02-24 10:12:41 +01009 * Copyright (C) 2016-2017 Stefan Roese <sr@denx.de>
Stefan Roese96c19042016-02-10 07:22:10 +010010 *
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.
14 */
15
16#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Stefan Roese96c19042016-02-10 07:22:10 +010018#include <dm.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Stefan Roese96c19042016-02-10 07:22:10 +010021#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070023#include <dm/devres.h>
Stefan Roese96c19042016-02-10 07:22:10 +010024#include <dm/lists.h>
25#include <net.h>
26#include <netdev.h>
27#include <config.h>
28#include <malloc.h>
29#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060030#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060031#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060032#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070033#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090034#include <linux/errno.h>
Stefan Roese96c19042016-02-10 07:22:10 +010035#include <phy.h>
36#include <miiphy.h>
37#include <watchdog.h>
38#include <asm/arch/cpu.h>
39#include <asm/arch/soc.h>
40#include <linux/compat.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060041#include <linux/libfdt.h>
Stefan Roese96c19042016-02-10 07:22:10 +010042#include <linux/mbus.h>
Stefan Chulski0d65eb62017-08-09 10:37:43 +030043#include <asm-generic/gpio.h>
Stefan Chulski4bc456a2017-08-09 10:37:44 +030044#include <fdt_support.h>
Nevo Hed5e975612019-08-15 18:08:44 -040045#include <linux/mdio.h>
Stefan Roese96c19042016-02-10 07:22:10 +010046
47DECLARE_GLOBAL_DATA_PTR;
48
Stefan Roese96c19042016-02-10 07:22:10 +010049#define __verify_pcpu_ptr(ptr) \
50do { \
51 const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
52 (void)__vpp_verify; \
53} while (0)
54
55#define VERIFY_PERCPU_PTR(__p) \
56({ \
57 __verify_pcpu_ptr(__p); \
58 (typeof(*(__p)) __kernel __force *)(__p); \
59})
60
61#define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
62#define smp_processor_id() 0
63#define num_present_cpus() 1
64#define for_each_present_cpu(cpu) \
65 for ((cpu) = 0; (cpu) < 1; (cpu)++)
66
67#define NET_SKB_PAD max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
68
69#define CONFIG_NR_CPUS 1
Stefan Roese96c19042016-02-10 07:22:10 +010070
71/* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
72#define WRAP (2 + ETH_HLEN + 4 + 32)
73#define MTU 1500
74#define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
75
Stefan Roese96c19042016-02-10 07:22:10 +010076/* RX Fifo Registers */
77#define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
78#define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
79#define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
80#define MVPP2_RX_FIFO_INIT_REG 0x64
81
82/* RX DMA Top Registers */
83#define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
84#define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
85#define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
86#define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
87#define MVPP2_POOL_BUF_SIZE_OFFSET 5
88#define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
89#define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
90#define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
91#define MVPP2_RXQ_POOL_SHORT_OFFS 20
Thomas Petazzoni2321c922017-02-16 06:53:51 +010092#define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
93#define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
Stefan Roese96c19042016-02-10 07:22:10 +010094#define MVPP2_RXQ_POOL_LONG_OFFS 24
Thomas Petazzoni2321c922017-02-16 06:53:51 +010095#define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
96#define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
Stefan Roese96c19042016-02-10 07:22:10 +010097#define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
98#define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
99#define MVPP2_RXQ_DISABLE_MASK BIT(31)
100
101/* Parser Registers */
102#define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
103#define MVPP2_PRS_PORT_LU_MAX 0xf
104#define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
105#define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
106#define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
107#define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
108#define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
109#define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
110#define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
111#define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
112#define MVPP2_PRS_TCAM_IDX_REG 0x1100
113#define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
114#define MVPP2_PRS_TCAM_INV_MASK BIT(31)
115#define MVPP2_PRS_SRAM_IDX_REG 0x1200
116#define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
117#define MVPP2_PRS_TCAM_CTRL_REG 0x1230
118#define MVPP2_PRS_TCAM_EN_MASK BIT(0)
119
120/* Classifier Registers */
121#define MVPP2_CLS_MODE_REG 0x1800
122#define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
123#define MVPP2_CLS_PORT_WAY_REG 0x1810
124#define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
125#define MVPP2_CLS_LKP_INDEX_REG 0x1814
126#define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
127#define MVPP2_CLS_LKP_TBL_REG 0x1818
128#define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
129#define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
130#define MVPP2_CLS_FLOW_INDEX_REG 0x1820
131#define MVPP2_CLS_FLOW_TBL0_REG 0x1824
132#define MVPP2_CLS_FLOW_TBL1_REG 0x1828
133#define MVPP2_CLS_FLOW_TBL2_REG 0x182c
134#define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
135#define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
136#define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
137#define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
138#define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
139#define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
140
141/* Descriptor Manager Top Registers */
142#define MVPP2_RXQ_NUM_REG 0x2040
143#define MVPP2_RXQ_DESC_ADDR_REG 0x2044
Thomas Petazzoni7f215c72017-02-20 11:36:57 +0100144#define MVPP22_DESC_ADDR_OFFS 8
Stefan Roese96c19042016-02-10 07:22:10 +0100145#define MVPP2_RXQ_DESC_SIZE_REG 0x2048
146#define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
147#define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
148#define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
149#define MVPP2_RXQ_NUM_NEW_OFFSET 16
150#define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
151#define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
152#define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
153#define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
154#define MVPP2_RXQ_THRESH_REG 0x204c
155#define MVPP2_OCCUPIED_THRESH_OFFSET 0
156#define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
157#define MVPP2_RXQ_INDEX_REG 0x2050
158#define MVPP2_TXQ_NUM_REG 0x2080
159#define MVPP2_TXQ_DESC_ADDR_REG 0x2084
160#define MVPP2_TXQ_DESC_SIZE_REG 0x2088
161#define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
162#define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
163#define MVPP2_TXQ_THRESH_REG 0x2094
164#define MVPP2_TRANSMITTED_THRESH_OFFSET 16
165#define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
166#define MVPP2_TXQ_INDEX_REG 0x2098
167#define MVPP2_TXQ_PREF_BUF_REG 0x209c
168#define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
169#define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
170#define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
171#define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
172#define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
173#define MVPP2_TXQ_PENDING_REG 0x20a0
174#define MVPP2_TXQ_PENDING_MASK 0x3fff
175#define MVPP2_TXQ_INT_STATUS_REG 0x20a4
176#define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
177#define MVPP2_TRANSMITTED_COUNT_OFFSET 16
178#define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
179#define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
180#define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
181#define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
182#define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
183#define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
184#define MVPP2_TXQ_RSVD_CLR_OFFSET 16
185#define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
Thomas Petazzoni7f215c72017-02-20 11:36:57 +0100186#define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
Stefan Roese96c19042016-02-10 07:22:10 +0100187#define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
188#define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
189#define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
190#define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
191#define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
192
193/* MBUS bridge registers */
194#define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
195#define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
196#define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
197#define MVPP2_BASE_ADDR_ENABLE 0x4060
198
Thomas Petazzonica560ab2017-02-16 08:41:07 +0100199/* AXI Bridge Registers */
200#define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
201#define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
202#define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
203#define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
204#define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
205#define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
206#define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
207#define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
208#define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
209#define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
210#define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
211#define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
212
213/* Values for AXI Bridge registers */
214#define MVPP22_AXI_ATTR_CACHE_OFFS 0
215#define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
216
217#define MVPP22_AXI_CODE_CACHE_OFFS 0
218#define MVPP22_AXI_CODE_DOMAIN_OFFS 4
219
220#define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
221#define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
222#define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
223
224#define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
225#define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
226
Stefan Roese96c19042016-02-10 07:22:10 +0100227/* Interrupt Cause and Mask registers */
228#define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
Thomas Petazzonif1077472017-02-16 08:46:37 +0100229#define MVPP21_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
230
231#define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
232#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
233#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
234#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
235
236#define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
237#define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
238
239#define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
240#define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
241#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
242#define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
243
Stefan Roese96c19042016-02-10 07:22:10 +0100244#define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
245#define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
246#define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
247#define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
248#define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
249#define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
250#define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
251#define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
252#define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
253#define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
254#define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
255#define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
256#define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
257#define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
258#define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
259#define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
260#define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
261#define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
262
263/* Buffer Manager registers */
264#define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
265#define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
266#define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
267#define MVPP2_BM_POOL_SIZE_MASK 0xfff0
268#define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
269#define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
270#define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
271#define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
272#define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
273#define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
274#define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
275#define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
276#define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
277#define MVPP2_BM_START_MASK BIT(0)
278#define MVPP2_BM_STOP_MASK BIT(1)
279#define MVPP2_BM_STATE_MASK BIT(4)
280#define MVPP2_BM_LOW_THRESH_OFFS 8
281#define MVPP2_BM_LOW_THRESH_MASK 0x7f00
282#define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
283 MVPP2_BM_LOW_THRESH_OFFS)
284#define MVPP2_BM_HIGH_THRESH_OFFS 16
285#define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
286#define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
287 MVPP2_BM_HIGH_THRESH_OFFS)
288#define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
289#define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
290#define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
291#define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
292#define MVPP2_BM_BPPE_FULL_MASK BIT(3)
293#define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
294#define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
295#define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
296#define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
297#define MVPP2_BM_VIRT_ALLOC_REG 0x6440
Thomas Petazzoni3520a332017-02-20 11:29:16 +0100298#define MVPP2_BM_ADDR_HIGH_ALLOC 0x6444
299#define MVPP2_BM_ADDR_HIGH_PHYS_MASK 0xff
300#define MVPP2_BM_ADDR_HIGH_VIRT_MASK 0xff00
301#define MVPP2_BM_ADDR_HIGH_VIRT_SHIFT 8
Stefan Roese96c19042016-02-10 07:22:10 +0100302#define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
303#define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
304#define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
305#define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
306#define MVPP2_BM_VIRT_RLS_REG 0x64c0
Thomas Petazzoni3520a332017-02-20 11:29:16 +0100307#define MVPP21_BM_MC_RLS_REG 0x64c4
Stefan Roese96c19042016-02-10 07:22:10 +0100308#define MVPP2_BM_MC_ID_MASK 0xfff
309#define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
Thomas Petazzoni3520a332017-02-20 11:29:16 +0100310#define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
311#define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
312#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
313#define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
314#define MVPP22_BM_MC_RLS_REG 0x64d4
Stefan Chulski115f76f2017-08-09 10:37:50 +0300315#define MVPP22_BM_POOL_BASE_HIGH_REG 0x6310
316#define MVPP22_BM_POOL_BASE_HIGH_MASK 0xff
Stefan Roese96c19042016-02-10 07:22:10 +0100317
318/* TX Scheduler registers */
319#define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
320#define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
321#define MVPP2_TXP_SCHED_ENQ_MASK 0xff
322#define MVPP2_TXP_SCHED_DISQ_OFFSET 8
323#define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
324#define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
325#define MVPP2_TXP_SCHED_MTU_REG 0x801c
326#define MVPP2_TXP_MTU_MAX 0x7FFFF
327#define MVPP2_TXP_SCHED_REFILL_REG 0x8020
328#define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
329#define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
330#define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
331#define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
332#define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
333#define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
334#define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
335#define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
336#define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
337#define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
338#define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
339#define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
340#define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
341
342/* TX general registers */
343#define MVPP2_TX_SNOOP_REG 0x8800
344#define MVPP2_TX_PORT_FLUSH_REG 0x8810
345#define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
346
347/* LMS registers */
348#define MVPP2_SRC_ADDR_MIDDLE 0x24
349#define MVPP2_SRC_ADDR_HIGH 0x28
350#define MVPP2_PHY_AN_CFG0_REG 0x34
351#define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
Stefan Roese96c19042016-02-10 07:22:10 +0100352#define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
Thomas Petazzoniebbe76f2017-02-15 12:16:23 +0100353#define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
Stefan Roese96c19042016-02-10 07:22:10 +0100354
355/* Per-port registers */
356#define MVPP2_GMAC_CTRL_0_REG 0x0
357#define MVPP2_GMAC_PORT_EN_MASK BIT(0)
Stefan Roese40e749b2017-03-22 15:07:30 +0100358#define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
Stefan Roese96c19042016-02-10 07:22:10 +0100359#define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
360#define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
361#define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
362#define MVPP2_GMAC_CTRL_1_REG 0x4
363#define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
364#define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
365#define MVPP2_GMAC_PCS_LB_EN_BIT 6
366#define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
367#define MVPP2_GMAC_SA_LOW_OFFS 7
368#define MVPP2_GMAC_CTRL_2_REG 0x8
369#define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
Stefan Roese40e749b2017-03-22 15:07:30 +0100370#define MVPP2_GMAC_SGMII_MODE_MASK BIT(0)
Stefan Roese96c19042016-02-10 07:22:10 +0100371#define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
372#define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
Stefan Roese40e749b2017-03-22 15:07:30 +0100373#define MVPP2_GMAC_PORT_DIS_PADING_MASK BIT(5)
Stefan Roese96c19042016-02-10 07:22:10 +0100374#define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
Stefan Roese40e749b2017-03-22 15:07:30 +0100375#define MVPP2_GMAC_CLK_125_BYPS_EN_MASK BIT(9)
Stefan Roese96c19042016-02-10 07:22:10 +0100376#define MVPP2_GMAC_AUTONEG_CONFIG 0xc
377#define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
378#define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
Stefan Roese40e749b2017-03-22 15:07:30 +0100379#define MVPP2_GMAC_EN_PCS_AN BIT(2)
380#define MVPP2_GMAC_AN_BYPASS_EN BIT(3)
Stefan Roese96c19042016-02-10 07:22:10 +0100381#define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
382#define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
383#define MVPP2_GMAC_AN_SPEED_EN BIT(7)
384#define MVPP2_GMAC_FC_ADV_EN BIT(9)
Stefan Roese40e749b2017-03-22 15:07:30 +0100385#define MVPP2_GMAC_EN_FC_AN BIT(11)
Stefan Roese96c19042016-02-10 07:22:10 +0100386#define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
387#define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
Stefan Roese40e749b2017-03-22 15:07:30 +0100388#define MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG BIT(15)
Stefan Roese96c19042016-02-10 07:22:10 +0100389#define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
390#define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
391#define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
392#define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
393 MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
Stefan Roese40e749b2017-03-22 15:07:30 +0100394#define MVPP2_GMAC_CTRL_4_REG 0x90
395#define MVPP2_GMAC_CTRL4_EXT_PIN_GMII_SEL_MASK BIT(0)
396#define MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK BIT(5)
397#define MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK BIT(6)
398#define MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK BIT(7)
Stefan Roese96c19042016-02-10 07:22:10 +0100399
Stefan Roese40e749b2017-03-22 15:07:30 +0100400/*
401 * Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
402 * relative to port->base.
403 */
404
405/* Port Mac Control0 */
406#define MVPP22_XLG_CTRL0_REG 0x100
407#define MVPP22_XLG_PORT_EN BIT(0)
408#define MVPP22_XLG_MAC_RESETN BIT(1)
409#define MVPP22_XLG_RX_FC_EN BIT(7)
410#define MVPP22_XLG_MIBCNT_DIS BIT(13)
411/* Port Mac Control1 */
412#define MVPP22_XLG_CTRL1_REG 0x104
413#define MVPP22_XLG_MAX_RX_SIZE_OFFS 0
414#define MVPP22_XLG_MAX_RX_SIZE_MASK 0x1fff
415/* Port Interrupt Mask */
416#define MVPP22_XLG_INTERRUPT_MASK_REG 0x118
417#define MVPP22_XLG_INTERRUPT_LINK_CHANGE BIT(1)
418/* Port Mac Control3 */
419#define MVPP22_XLG_CTRL3_REG 0x11c
420#define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
421#define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
422#define MVPP22_XLG_CTRL3_MACMODESELECT_10GMAC (1 << 13)
423/* Port Mac Control4 */
424#define MVPP22_XLG_CTRL4_REG 0x184
425#define MVPP22_XLG_FORWARD_802_3X_FC_EN BIT(5)
426#define MVPP22_XLG_FORWARD_PFC_EN BIT(6)
427#define MVPP22_XLG_MODE_DMA_1G BIT(12)
428#define MVPP22_XLG_EN_IDLE_CHECK_FOR_LINK BIT(14)
429
430/* XPCS registers */
431
432/* Global Configuration 0 */
433#define MVPP22_XPCS_GLOBAL_CFG_0_REG 0x0
434#define MVPP22_XPCS_PCSRESET BIT(0)
435#define MVPP22_XPCS_PCSMODE_OFFS 3
436#define MVPP22_XPCS_PCSMODE_MASK (0x3 << \
437 MVPP22_XPCS_PCSMODE_OFFS)
438#define MVPP22_XPCS_LANEACTIVE_OFFS 5
439#define MVPP22_XPCS_LANEACTIVE_MASK (0x3 << \
440 MVPP22_XPCS_LANEACTIVE_OFFS)
441
442/* MPCS registers */
443
444#define PCS40G_COMMON_CONTROL 0x14
Stefan Chulskia27adcb2017-04-06 15:39:08 +0200445#define FORWARD_ERROR_CORRECTION_MASK BIT(10)
Stefan Roese40e749b2017-03-22 15:07:30 +0100446
447#define PCS_CLOCK_RESET 0x14c
448#define TX_SD_CLK_RESET_MASK BIT(0)
449#define RX_SD_CLK_RESET_MASK BIT(1)
450#define MAC_CLK_RESET_MASK BIT(2)
451#define CLK_DIVISION_RATIO_OFFS 4
452#define CLK_DIVISION_RATIO_MASK (0x7 << CLK_DIVISION_RATIO_OFFS)
453#define CLK_DIV_PHASE_SET_MASK BIT(11)
454
455/* System Soft Reset 1 */
456#define GOP_SOFT_RESET_1_REG 0x108
457#define NETC_GOP_SOFT_RESET_OFFS 6
458#define NETC_GOP_SOFT_RESET_MASK (0x1 << \
459 NETC_GOP_SOFT_RESET_OFFS)
460
461/* Ports Control 0 */
462#define NETCOMP_PORTS_CONTROL_0_REG 0x110
463#define NETC_BUS_WIDTH_SELECT_OFFS 1
464#define NETC_BUS_WIDTH_SELECT_MASK (0x1 << \
465 NETC_BUS_WIDTH_SELECT_OFFS)
466#define NETC_GIG_RX_DATA_SAMPLE_OFFS 29
467#define NETC_GIG_RX_DATA_SAMPLE_MASK (0x1 << \
468 NETC_GIG_RX_DATA_SAMPLE_OFFS)
469#define NETC_CLK_DIV_PHASE_OFFS 31
470#define NETC_CLK_DIV_PHASE_MASK (0x1 << NETC_CLK_DIV_PHASE_OFFS)
471/* Ports Control 1 */
472#define NETCOMP_PORTS_CONTROL_1_REG 0x114
473#define NETC_PORTS_ACTIVE_OFFSET(p) (0 + p)
474#define NETC_PORTS_ACTIVE_MASK(p) (0x1 << \
475 NETC_PORTS_ACTIVE_OFFSET(p))
476#define NETC_PORT_GIG_RF_RESET_OFFS(p) (28 + p)
477#define NETC_PORT_GIG_RF_RESET_MASK(p) (0x1 << \
478 NETC_PORT_GIG_RF_RESET_OFFS(p))
479#define NETCOMP_CONTROL_0_REG 0x120
480#define NETC_GBE_PORT0_SGMII_MODE_OFFS 0
481#define NETC_GBE_PORT0_SGMII_MODE_MASK (0x1 << \
482 NETC_GBE_PORT0_SGMII_MODE_OFFS)
483#define NETC_GBE_PORT1_SGMII_MODE_OFFS 1
484#define NETC_GBE_PORT1_SGMII_MODE_MASK (0x1 << \
485 NETC_GBE_PORT1_SGMII_MODE_OFFS)
486#define NETC_GBE_PORT1_MII_MODE_OFFS 2
487#define NETC_GBE_PORT1_MII_MODE_MASK (0x1 << \
488 NETC_GBE_PORT1_MII_MODE_OFFS)
489
490#define MVPP22_SMI_MISC_CFG_REG (MVPP22_SMI + 0x04)
Thomas Petazzonicc2445f2017-02-20 11:42:51 +0100491#define MVPP22_SMI_POLLING_EN BIT(10)
492
Stefan Roese40e749b2017-03-22 15:07:30 +0100493#define MVPP22_SMI_PHY_ADDR_REG(port) (MVPP22_SMI + 0x04 + \
494 (0x4 * (port)))
Thomas Petazzoni5555f072017-02-16 08:03:37 +0100495
Stefan Roese96c19042016-02-10 07:22:10 +0100496#define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
497
498/* Descriptor ring Macros */
499#define MVPP2_QUEUE_NEXT_DESC(q, index) \
500 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
501
Stefan Roeseb71c2a32017-02-16 08:31:32 +0100502/* PP2.2: SMI: 0x12a200 -> offset 0x1200 to iface_base */
503#define MVPP22_SMI 0x1200
Stefan Roese96c19042016-02-10 07:22:10 +0100504
Stefan Roese40e749b2017-03-22 15:07:30 +0100505/* Additional PPv2.2 offsets */
506#define MVPP22_MPCS 0x007000
507#define MVPP22_XPCS 0x007400
508#define MVPP22_PORT_BASE 0x007e00
509#define MVPP22_PORT_OFFSET 0x001000
510#define MVPP22_RFU1 0x318000
511
512/* Maximum number of ports */
513#define MVPP22_GOP_MAC_NUM 4
514
515/* Sets the field located at the specified in data */
516#define MVPP2_RGMII_TX_FIFO_MIN_TH 0x41
517#define MVPP2_SGMII_TX_FIFO_MIN_TH 0x5
518#define MVPP2_SGMII2_5_TX_FIFO_MIN_TH 0xb
519
520/* Net Complex */
521enum mv_netc_topology {
522 MV_NETC_GE_MAC2_SGMII = BIT(0),
523 MV_NETC_GE_MAC3_SGMII = BIT(1),
524 MV_NETC_GE_MAC3_RGMII = BIT(2),
525};
526
527enum mv_netc_phase {
528 MV_NETC_FIRST_PHASE,
529 MV_NETC_SECOND_PHASE,
530};
531
532enum mv_netc_sgmii_xmi_mode {
533 MV_NETC_GBE_SGMII,
534 MV_NETC_GBE_XMII,
535};
536
537enum mv_netc_mii_mode {
538 MV_NETC_GBE_RGMII,
539 MV_NETC_GBE_MII,
540};
541
542enum mv_netc_lanes {
543 MV_NETC_LANE_23,
544 MV_NETC_LANE_45,
545};
546
Stefan Roese96c19042016-02-10 07:22:10 +0100547/* Various constants */
548
549/* Coalescing */
550#define MVPP2_TXDONE_COAL_PKTS_THRESH 15
551#define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
552#define MVPP2_RX_COAL_PKTS 32
553#define MVPP2_RX_COAL_USEC 100
554
555/* The two bytes Marvell header. Either contains a special value used
556 * by Marvell switches when a specific hardware mode is enabled (not
557 * supported by this driver) or is filled automatically by zeroes on
558 * the RX side. Those two bytes being at the front of the Ethernet
559 * header, they allow to have the IP header aligned on a 4 bytes
560 * boundary automatically: the hardware skips those two bytes on its
561 * own.
562 */
563#define MVPP2_MH_SIZE 2
564#define MVPP2_ETH_TYPE_LEN 2
565#define MVPP2_PPPOE_HDR_SIZE 8
566#define MVPP2_VLAN_TAG_LEN 4
567
568/* Lbtd 802.3 type */
569#define MVPP2_IP_LBDT_TYPE 0xfffa
570
571#define MVPP2_CPU_D_CACHE_LINE_SIZE 32
572#define MVPP2_TX_CSUM_MAX_SIZE 9800
573
574/* Timeout constants */
575#define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
576#define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
577
578#define MVPP2_TX_MTU_MAX 0x7ffff
579
580/* Maximum number of T-CONTs of PON port */
581#define MVPP2_MAX_TCONT 16
582
583/* Maximum number of supported ports */
584#define MVPP2_MAX_PORTS 4
585
586/* Maximum number of TXQs used by single port */
587#define MVPP2_MAX_TXQ 8
588
Stefan Roese96c19042016-02-10 07:22:10 +0100589/* Default number of TXQs in use */
590#define MVPP2_DEFAULT_TXQ 1
591
Flavio Suligoi50b91ce2020-01-29 09:38:56 +0100592/* Default number of RXQs in use */
Stefan Roese96c19042016-02-10 07:22:10 +0100593#define MVPP2_DEFAULT_RXQ 1
594#define CONFIG_MV_ETH_RXQ 8 /* increment by 8 */
595
Stefan Roese96c19042016-02-10 07:22:10 +0100596/* Max number of Rx descriptors */
597#define MVPP2_MAX_RXD 16
598
599/* Max number of Tx descriptors */
600#define MVPP2_MAX_TXD 16
601
602/* Amount of Tx descriptors that can be reserved at once by CPU */
Stefan Chulskib528d122017-08-09 10:37:47 +0300603#define MVPP2_CPU_DESC_CHUNK 16
Stefan Roese96c19042016-02-10 07:22:10 +0100604
605/* Max number of Tx descriptors in each aggregated queue */
Stefan Chulskib528d122017-08-09 10:37:47 +0300606#define MVPP2_AGGR_TXQ_SIZE 16
Stefan Roese96c19042016-02-10 07:22:10 +0100607
608/* Descriptor aligned size */
609#define MVPP2_DESC_ALIGNED_SIZE 32
610
611/* Descriptor alignment mask */
612#define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
613
614/* RX FIFO constants */
Stefan Roesea8801ed2017-03-01 13:09:42 +0100615#define MVPP21_RX_FIFO_PORT_DATA_SIZE 0x2000
616#define MVPP21_RX_FIFO_PORT_ATTR_SIZE 0x80
617#define MVPP22_RX_FIFO_10GB_PORT_DATA_SIZE 0x8000
618#define MVPP22_RX_FIFO_2_5GB_PORT_DATA_SIZE 0x2000
619#define MVPP22_RX_FIFO_1GB_PORT_DATA_SIZE 0x1000
620#define MVPP22_RX_FIFO_10GB_PORT_ATTR_SIZE 0x200
621#define MVPP22_RX_FIFO_2_5GB_PORT_ATTR_SIZE 0x80
622#define MVPP22_RX_FIFO_1GB_PORT_ATTR_SIZE 0x40
623#define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
624
625/* TX general registers */
626#define MVPP22_TX_FIFO_SIZE_REG(eth_tx_port) (0x8860 + ((eth_tx_port) << 2))
627#define MVPP22_TX_FIFO_SIZE_MASK 0xf
628
629/* TX FIFO constants */
630#define MVPP2_TX_FIFO_DATA_SIZE_10KB 0xa
631#define MVPP2_TX_FIFO_DATA_SIZE_3KB 0x3
Stefan Roese96c19042016-02-10 07:22:10 +0100632
633/* RX buffer constants */
634#define MVPP2_SKB_SHINFO_SIZE \
635 0
636
637#define MVPP2_RX_PKT_SIZE(mtu) \
638 ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
639 ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
640
641#define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
642#define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
643#define MVPP2_RX_MAX_PKT_SIZE(total_size) \
644 ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
645
646#define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
647
648/* IPv6 max L3 address size */
649#define MVPP2_MAX_L3_ADDR_SIZE 16
650
651/* Port flags */
652#define MVPP2_F_LOOPBACK BIT(0)
653
654/* Marvell tag types */
655enum mvpp2_tag_type {
656 MVPP2_TAG_TYPE_NONE = 0,
657 MVPP2_TAG_TYPE_MH = 1,
658 MVPP2_TAG_TYPE_DSA = 2,
659 MVPP2_TAG_TYPE_EDSA = 3,
660 MVPP2_TAG_TYPE_VLAN = 4,
661 MVPP2_TAG_TYPE_LAST = 5
662};
663
664/* Parser constants */
665#define MVPP2_PRS_TCAM_SRAM_SIZE 256
666#define MVPP2_PRS_TCAM_WORDS 6
667#define MVPP2_PRS_SRAM_WORDS 4
668#define MVPP2_PRS_FLOW_ID_SIZE 64
669#define MVPP2_PRS_FLOW_ID_MASK 0x3f
670#define MVPP2_PRS_TCAM_ENTRY_INVALID 1
671#define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
672#define MVPP2_PRS_IPV4_HEAD 0x40
673#define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
674#define MVPP2_PRS_IPV4_MC 0xe0
675#define MVPP2_PRS_IPV4_MC_MASK 0xf0
676#define MVPP2_PRS_IPV4_BC_MASK 0xff
677#define MVPP2_PRS_IPV4_IHL 0x5
678#define MVPP2_PRS_IPV4_IHL_MASK 0xf
679#define MVPP2_PRS_IPV6_MC 0xff
680#define MVPP2_PRS_IPV6_MC_MASK 0xff
681#define MVPP2_PRS_IPV6_HOP_MASK 0xff
682#define MVPP2_PRS_TCAM_PROTO_MASK 0xff
683#define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
684#define MVPP2_PRS_DBL_VLANS_MAX 100
685
686/* Tcam structure:
687 * - lookup ID - 4 bits
688 * - port ID - 1 byte
689 * - additional information - 1 byte
690 * - header data - 8 bytes
691 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
692 */
693#define MVPP2_PRS_AI_BITS 8
694#define MVPP2_PRS_PORT_MASK 0xff
695#define MVPP2_PRS_LU_MASK 0xf
696#define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
697 (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
698#define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
699 (((offs) * 2) - ((offs) % 2) + 2)
700#define MVPP2_PRS_TCAM_AI_BYTE 16
701#define MVPP2_PRS_TCAM_PORT_BYTE 17
702#define MVPP2_PRS_TCAM_LU_BYTE 20
703#define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
704#define MVPP2_PRS_TCAM_INV_WORD 5
705/* Tcam entries ID */
706#define MVPP2_PE_DROP_ALL 0
707#define MVPP2_PE_FIRST_FREE_TID 1
708#define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
709#define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
710#define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
711#define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
712#define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
713#define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
714#define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
715#define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
716#define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
717#define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
718#define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
719#define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
720#define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
721#define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
722#define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
723#define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
724#define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
725#define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
726#define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
727#define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
728#define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
729#define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
730#define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
731#define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
732#define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
733
734/* Sram structure
735 * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
736 */
737#define MVPP2_PRS_SRAM_RI_OFFS 0
738#define MVPP2_PRS_SRAM_RI_WORD 0
739#define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
740#define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
741#define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
742#define MVPP2_PRS_SRAM_SHIFT_OFFS 64
743#define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
744#define MVPP2_PRS_SRAM_UDF_OFFS 73
745#define MVPP2_PRS_SRAM_UDF_BITS 8
746#define MVPP2_PRS_SRAM_UDF_MASK 0xff
747#define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
748#define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
749#define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
750#define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
751#define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
752#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
753#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
754#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
755#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
756#define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
757#define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
758#define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
759#define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
760#define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
761#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
762#define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
763#define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
764#define MVPP2_PRS_SRAM_AI_OFFS 90
765#define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
766#define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
767#define MVPP2_PRS_SRAM_AI_MASK 0xff
768#define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
769#define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
770#define MVPP2_PRS_SRAM_LU_DONE_BIT 110
771#define MVPP2_PRS_SRAM_LU_GEN_BIT 111
772
773/* Sram result info bits assignment */
774#define MVPP2_PRS_RI_MAC_ME_MASK 0x1
775#define MVPP2_PRS_RI_DSA_MASK 0x2
Thomas Petazzoni265b3c62017-02-15 12:19:36 +0100776#define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
777#define MVPP2_PRS_RI_VLAN_NONE 0x0
Stefan Roese96c19042016-02-10 07:22:10 +0100778#define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
779#define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
780#define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
781#define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
782#define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
Thomas Petazzoni265b3c62017-02-15 12:19:36 +0100783#define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
784#define MVPP2_PRS_RI_L2_UCAST 0x0
Stefan Roese96c19042016-02-10 07:22:10 +0100785#define MVPP2_PRS_RI_L2_MCAST BIT(9)
786#define MVPP2_PRS_RI_L2_BCAST BIT(10)
787#define MVPP2_PRS_RI_PPPOE_MASK 0x800
Thomas Petazzoni265b3c62017-02-15 12:19:36 +0100788#define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
789#define MVPP2_PRS_RI_L3_UN 0x0
Stefan Roese96c19042016-02-10 07:22:10 +0100790#define MVPP2_PRS_RI_L3_IP4 BIT(12)
791#define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
792#define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
793#define MVPP2_PRS_RI_L3_IP6 BIT(14)
794#define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
795#define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
Thomas Petazzoni265b3c62017-02-15 12:19:36 +0100796#define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
797#define MVPP2_PRS_RI_L3_UCAST 0x0
Stefan Roese96c19042016-02-10 07:22:10 +0100798#define MVPP2_PRS_RI_L3_MCAST BIT(15)
799#define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
800#define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
801#define MVPP2_PRS_RI_UDF3_MASK 0x300000
802#define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
803#define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
804#define MVPP2_PRS_RI_L4_TCP BIT(22)
805#define MVPP2_PRS_RI_L4_UDP BIT(23)
806#define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
807#define MVPP2_PRS_RI_UDF7_MASK 0x60000000
808#define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
809#define MVPP2_PRS_RI_DROP_MASK 0x80000000
810
811/* Sram additional info bits assignment */
812#define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
813#define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
814#define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
815#define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
816#define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
817#define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
818#define MVPP2_PRS_SINGLE_VLAN_AI 0
819#define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
820
821/* DSA/EDSA type */
822#define MVPP2_PRS_TAGGED true
823#define MVPP2_PRS_UNTAGGED false
824#define MVPP2_PRS_EDSA true
825#define MVPP2_PRS_DSA false
826
827/* MAC entries, shadow udf */
828enum mvpp2_prs_udf {
829 MVPP2_PRS_UDF_MAC_DEF,
830 MVPP2_PRS_UDF_MAC_RANGE,
831 MVPP2_PRS_UDF_L2_DEF,
832 MVPP2_PRS_UDF_L2_DEF_COPY,
833 MVPP2_PRS_UDF_L2_USER,
834};
835
836/* Lookup ID */
837enum mvpp2_prs_lookup {
838 MVPP2_PRS_LU_MH,
839 MVPP2_PRS_LU_MAC,
840 MVPP2_PRS_LU_DSA,
841 MVPP2_PRS_LU_VLAN,
842 MVPP2_PRS_LU_L2,
843 MVPP2_PRS_LU_PPPOE,
844 MVPP2_PRS_LU_IP4,
845 MVPP2_PRS_LU_IP6,
846 MVPP2_PRS_LU_FLOWS,
847 MVPP2_PRS_LU_LAST,
848};
849
850/* L3 cast enum */
851enum mvpp2_prs_l3_cast {
852 MVPP2_PRS_L3_UNI_CAST,
853 MVPP2_PRS_L3_MULTI_CAST,
854 MVPP2_PRS_L3_BROAD_CAST
855};
856
857/* Classifier constants */
858#define MVPP2_CLS_FLOWS_TBL_SIZE 512
859#define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
860#define MVPP2_CLS_LKP_TBL_SIZE 64
861
862/* BM constants */
863#define MVPP2_BM_POOLS_NUM 1
864#define MVPP2_BM_LONG_BUF_NUM 16
865#define MVPP2_BM_SHORT_BUF_NUM 16
866#define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
867#define MVPP2_BM_POOL_PTR_ALIGN 128
868#define MVPP2_BM_SWF_LONG_POOL(port) 0
869
870/* BM cookie (32 bits) definition */
871#define MVPP2_BM_COOKIE_POOL_OFFS 8
872#define MVPP2_BM_COOKIE_CPU_OFFS 24
873
874/* BM short pool packet size
875 * These value assure that for SWF the total number
876 * of bytes allocated for each buffer will be 512
877 */
878#define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
879
880enum mvpp2_bm_type {
881 MVPP2_BM_FREE,
882 MVPP2_BM_SWF_LONG,
883 MVPP2_BM_SWF_SHORT
884};
885
886/* Definitions */
887
888/* Shared Packet Processor resources */
889struct mvpp2 {
890 /* Shared registers' base addresses */
891 void __iomem *base;
892 void __iomem *lms_base;
Thomas Petazzoni5555f072017-02-16 08:03:37 +0100893 void __iomem *iface_base;
Stefan Roese96c19042016-02-10 07:22:10 +0100894
Stefan Roese40e749b2017-03-22 15:07:30 +0100895 void __iomem *mpcs_base;
896 void __iomem *xpcs_base;
897 void __iomem *rfu1_base;
898
899 u32 netc_config;
900
Stefan Roese96c19042016-02-10 07:22:10 +0100901 /* List of pointers to port structures */
902 struct mvpp2_port **port_list;
903
904 /* Aggregated TXQs */
905 struct mvpp2_tx_queue *aggr_txqs;
906
907 /* BM pools */
908 struct mvpp2_bm_pool *bm_pools;
909
910 /* PRS shadow table */
911 struct mvpp2_prs_shadow *prs_shadow;
912 /* PRS auxiliary table for double vlan entries control */
913 bool *prs_double_vlans;
914
915 /* Tclk value */
916 u32 tclk;
917
Thomas Petazzoni51ccb412017-02-15 14:08:59 +0100918 /* HW version */
919 enum { MVPP21, MVPP22 } hw_version;
920
Thomas Petazzoni38a23282017-02-16 09:03:16 +0100921 /* Maximum number of RXQs per port */
922 unsigned int max_port_rxqs;
923
Stefan Roesed017cdf2017-02-16 15:26:06 +0100924 int probe_done;
Stefan Chulski75872182017-08-09 10:37:46 +0300925 u8 num_ports;
Stefan Roese96c19042016-02-10 07:22:10 +0100926};
927
928struct mvpp2_pcpu_stats {
929 u64 rx_packets;
930 u64 rx_bytes;
931 u64 tx_packets;
932 u64 tx_bytes;
933};
934
935struct mvpp2_port {
936 u8 id;
937
Thomas Petazzoni5555f072017-02-16 08:03:37 +0100938 /* Index of the port from the "group of ports" complex point
939 * of view
940 */
941 int gop_id;
942
Stefan Roese96c19042016-02-10 07:22:10 +0100943 int irq;
944
945 struct mvpp2 *priv;
946
947 /* Per-port registers' base address */
948 void __iomem *base;
949
950 struct mvpp2_rx_queue **rxqs;
951 struct mvpp2_tx_queue **txqs;
952
953 int pkt_size;
954
955 u32 pending_cause_rx;
956
957 /* Per-CPU port control */
958 struct mvpp2_port_pcpu __percpu *pcpu;
959
960 /* Flags */
961 unsigned long flags;
962
963 u16 tx_ring_size;
964 u16 rx_ring_size;
965 struct mvpp2_pcpu_stats __percpu *stats;
966
967 struct phy_device *phy_dev;
968 phy_interface_t phy_interface;
Stefan Roese96c19042016-02-10 07:22:10 +0100969 int phyaddr;
Nevo Hed5e975612019-08-15 18:08:44 -0400970 struct udevice *mdio_dev;
Simon Glassfa4689a2019-12-06 21:41:35 -0700971 struct mii_dev *bus;
972#if CONFIG_IS_ENABLED(DM_GPIO)
Stefan Chulski0d65eb62017-08-09 10:37:43 +0300973 struct gpio_desc phy_reset_gpio;
974 struct gpio_desc phy_tx_disable_gpio;
975#endif
Stefan Roese96c19042016-02-10 07:22:10 +0100976 int init;
977 unsigned int link;
978 unsigned int duplex;
979 unsigned int speed;
980
Stefan Roese3651bfc2017-03-22 14:15:40 +0100981 unsigned int phy_speed; /* SGMII 1Gbps vs 2.5Gbps */
982
Stefan Roese96c19042016-02-10 07:22:10 +0100983 struct mvpp2_bm_pool *pool_long;
984 struct mvpp2_bm_pool *pool_short;
985
986 /* Index of first port's physical RXQ */
987 u8 first_rxq;
988
989 u8 dev_addr[ETH_ALEN];
990};
991
992/* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
993 * layout of the transmit and reception DMA descriptors, and their
994 * layout is therefore defined by the hardware design
995 */
996
997#define MVPP2_TXD_L3_OFF_SHIFT 0
998#define MVPP2_TXD_IP_HLEN_SHIFT 8
999#define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
1000#define MVPP2_TXD_L4_CSUM_NOT BIT(14)
1001#define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
1002#define MVPP2_TXD_PADDING_DISABLE BIT(23)
1003#define MVPP2_TXD_L4_UDP BIT(24)
1004#define MVPP2_TXD_L3_IP6 BIT(26)
1005#define MVPP2_TXD_L_DESC BIT(28)
1006#define MVPP2_TXD_F_DESC BIT(29)
1007
1008#define MVPP2_RXD_ERR_SUMMARY BIT(15)
1009#define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
1010#define MVPP2_RXD_ERR_CRC 0x0
1011#define MVPP2_RXD_ERR_OVERRUN BIT(13)
1012#define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
1013#define MVPP2_RXD_BM_POOL_ID_OFFS 16
1014#define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
1015#define MVPP2_RXD_HWF_SYNC BIT(21)
1016#define MVPP2_RXD_L4_CSUM_OK BIT(22)
1017#define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
1018#define MVPP2_RXD_L4_TCP BIT(25)
1019#define MVPP2_RXD_L4_UDP BIT(26)
1020#define MVPP2_RXD_L3_IP4 BIT(28)
1021#define MVPP2_RXD_L3_IP6 BIT(30)
1022#define MVPP2_RXD_BUF_HDR BIT(31)
1023
Thomas Petazzonie3645a02017-02-15 16:25:53 +01001024/* HW TX descriptor for PPv2.1 */
1025struct mvpp21_tx_desc {
Stefan Roese96c19042016-02-10 07:22:10 +01001026 u32 command; /* Options used by HW for packet transmitting.*/
1027 u8 packet_offset; /* the offset from the buffer beginning */
1028 u8 phys_txq; /* destination queue ID */
1029 u16 data_size; /* data size of transmitted packet in bytes */
Thomas Petazzonic49aff22017-02-20 10:27:51 +01001030 u32 buf_dma_addr; /* physical addr of transmitted buffer */
Stefan Roese96c19042016-02-10 07:22:10 +01001031 u32 buf_cookie; /* cookie for access to TX buffer in tx path */
1032 u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
1033 u32 reserved2; /* reserved (for future use) */
1034};
1035
Thomas Petazzonie3645a02017-02-15 16:25:53 +01001036/* HW RX descriptor for PPv2.1 */
1037struct mvpp21_rx_desc {
Stefan Roese96c19042016-02-10 07:22:10 +01001038 u32 status; /* info about received packet */
1039 u16 reserved1; /* parser_info (for future use, PnC) */
1040 u16 data_size; /* size of received packet in bytes */
Thomas Petazzonic49aff22017-02-20 10:27:51 +01001041 u32 buf_dma_addr; /* physical address of the buffer */
Stefan Roese96c19042016-02-10 07:22:10 +01001042 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
1043 u16 reserved2; /* gem_port_id (for future use, PON) */
1044 u16 reserved3; /* csum_l4 (for future use, PnC) */
1045 u8 reserved4; /* bm_qset (for future use, BM) */
1046 u8 reserved5;
1047 u16 reserved6; /* classify_info (for future use, PnC) */
1048 u32 reserved7; /* flow_id (for future use, PnC) */
1049 u32 reserved8;
1050};
1051
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001052/* HW TX descriptor for PPv2.2 */
1053struct mvpp22_tx_desc {
1054 u32 command;
1055 u8 packet_offset;
1056 u8 phys_txq;
1057 u16 data_size;
1058 u64 reserved1;
1059 u64 buf_dma_addr_ptp;
1060 u64 buf_cookie_misc;
1061};
1062
1063/* HW RX descriptor for PPv2.2 */
1064struct mvpp22_rx_desc {
1065 u32 status;
1066 u16 reserved1;
1067 u16 data_size;
1068 u32 reserved2;
1069 u32 reserved3;
1070 u64 buf_dma_addr_key_hash;
1071 u64 buf_cookie_misc;
1072};
1073
Thomas Petazzonie3645a02017-02-15 16:25:53 +01001074/* Opaque type used by the driver to manipulate the HW TX and RX
1075 * descriptors
1076 */
1077struct mvpp2_tx_desc {
1078 union {
1079 struct mvpp21_tx_desc pp21;
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001080 struct mvpp22_tx_desc pp22;
Thomas Petazzonie3645a02017-02-15 16:25:53 +01001081 };
1082};
1083
1084struct mvpp2_rx_desc {
1085 union {
1086 struct mvpp21_rx_desc pp21;
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001087 struct mvpp22_rx_desc pp22;
Thomas Petazzonie3645a02017-02-15 16:25:53 +01001088 };
1089};
1090
Stefan Roese96c19042016-02-10 07:22:10 +01001091/* Per-CPU Tx queue control */
1092struct mvpp2_txq_pcpu {
1093 int cpu;
1094
1095 /* Number of Tx DMA descriptors in the descriptor ring */
1096 int size;
1097
1098 /* Number of currently used Tx DMA descriptor in the
1099 * descriptor ring
1100 */
1101 int count;
1102
1103 /* Number of Tx DMA descriptors reserved for each CPU */
1104 int reserved_num;
1105
1106 /* Index of last TX DMA descriptor that was inserted */
1107 int txq_put_index;
1108
1109 /* Index of the TX DMA descriptor to be cleaned up */
1110 int txq_get_index;
1111};
1112
1113struct mvpp2_tx_queue {
1114 /* Physical number of this Tx queue */
1115 u8 id;
1116
1117 /* Logical number of this Tx queue */
1118 u8 log_id;
1119
1120 /* Number of Tx DMA descriptors in the descriptor ring */
1121 int size;
1122
1123 /* Number of currently used Tx DMA descriptor in the descriptor ring */
1124 int count;
1125
1126 /* Per-CPU control of physical Tx queues */
1127 struct mvpp2_txq_pcpu __percpu *pcpu;
1128
1129 u32 done_pkts_coal;
1130
1131 /* Virtual address of thex Tx DMA descriptors array */
1132 struct mvpp2_tx_desc *descs;
1133
1134 /* DMA address of the Tx DMA descriptors array */
Thomas Petazzonic49aff22017-02-20 10:27:51 +01001135 dma_addr_t descs_dma;
Stefan Roese96c19042016-02-10 07:22:10 +01001136
1137 /* Index of the last Tx DMA descriptor */
1138 int last_desc;
1139
1140 /* Index of the next Tx DMA descriptor to process */
1141 int next_desc_to_proc;
1142};
1143
1144struct mvpp2_rx_queue {
1145 /* RX queue number, in the range 0-31 for physical RXQs */
1146 u8 id;
1147
1148 /* Num of rx descriptors in the rx descriptor ring */
1149 int size;
1150
1151 u32 pkts_coal;
1152 u32 time_coal;
1153
1154 /* Virtual address of the RX DMA descriptors array */
1155 struct mvpp2_rx_desc *descs;
1156
1157 /* DMA address of the RX DMA descriptors array */
Thomas Petazzonic49aff22017-02-20 10:27:51 +01001158 dma_addr_t descs_dma;
Stefan Roese96c19042016-02-10 07:22:10 +01001159
1160 /* Index of the last RX DMA descriptor */
1161 int last_desc;
1162
1163 /* Index of the next RX DMA descriptor to process */
1164 int next_desc_to_proc;
1165
1166 /* ID of port to which physical RXQ is mapped */
1167 int port;
1168
1169 /* Port's logic RXQ number to which physical RXQ is mapped */
1170 int logic_rxq;
1171};
1172
1173union mvpp2_prs_tcam_entry {
1174 u32 word[MVPP2_PRS_TCAM_WORDS];
1175 u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
1176};
1177
1178union mvpp2_prs_sram_entry {
1179 u32 word[MVPP2_PRS_SRAM_WORDS];
1180 u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
1181};
1182
1183struct mvpp2_prs_entry {
1184 u32 index;
1185 union mvpp2_prs_tcam_entry tcam;
1186 union mvpp2_prs_sram_entry sram;
1187};
1188
1189struct mvpp2_prs_shadow {
1190 bool valid;
1191 bool finish;
1192
1193 /* Lookup ID */
1194 int lu;
1195
1196 /* User defined offset */
1197 int udf;
1198
1199 /* Result info */
1200 u32 ri;
1201 u32 ri_mask;
1202};
1203
1204struct mvpp2_cls_flow_entry {
1205 u32 index;
1206 u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1207};
1208
1209struct mvpp2_cls_lookup_entry {
1210 u32 lkpid;
1211 u32 way;
1212 u32 data;
1213};
1214
1215struct mvpp2_bm_pool {
1216 /* Pool number in the range 0-7 */
1217 int id;
1218 enum mvpp2_bm_type type;
1219
1220 /* Buffer Pointers Pool External (BPPE) size */
1221 int size;
1222 /* Number of buffers for this pool */
1223 int buf_num;
1224 /* Pool buffer size */
1225 int buf_size;
1226 /* Packet size */
1227 int pkt_size;
1228
1229 /* BPPE virtual base address */
Stefan Roesefeb0b332017-02-15 12:46:18 +01001230 unsigned long *virt_addr;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01001231 /* BPPE DMA base address */
1232 dma_addr_t dma_addr;
Stefan Roese96c19042016-02-10 07:22:10 +01001233
1234 /* Ports using BM pool */
1235 u32 port_map;
Stefan Roese96c19042016-02-10 07:22:10 +01001236};
1237
Stefan Roese96c19042016-02-10 07:22:10 +01001238/* Static declaractions */
1239
1240/* Number of RXQs used by single port */
1241static int rxq_number = MVPP2_DEFAULT_RXQ;
1242/* Number of TXQs used by single port */
1243static int txq_number = MVPP2_DEFAULT_TXQ;
1244
Stefan Roese38801d42017-02-24 10:12:41 +01001245static int base_id;
1246
Stefan Roese96c19042016-02-10 07:22:10 +01001247#define MVPP2_DRIVER_NAME "mvpp2"
1248#define MVPP2_DRIVER_VERSION "1.0"
1249
1250/*
1251 * U-Boot internal data, mostly uncached buffers for descriptors and data
1252 */
1253struct buffer_location {
1254 struct mvpp2_tx_desc *aggr_tx_descs;
1255 struct mvpp2_tx_desc *tx_descs;
1256 struct mvpp2_rx_desc *rx_descs;
Stefan Roesefeb0b332017-02-15 12:46:18 +01001257 unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
1258 unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
Stefan Roese96c19042016-02-10 07:22:10 +01001259 int first_rxq;
1260};
1261
1262/*
1263 * All 4 interfaces use the same global buffer, since only one interface
1264 * can be enabled at once
1265 */
1266static struct buffer_location buffer_loc;
Sven Auhagena50bca12020-07-01 17:43:43 +02001267static int buffer_loc_init;
Stefan Roese96c19042016-02-10 07:22:10 +01001268
1269/*
1270 * Page table entries are set to 1MB, or multiples of 1MB
1271 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
1272 */
1273#define BD_SPACE (1 << 20)
1274
1275/* Utility/helper methods */
1276
1277static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1278{
1279 writel(data, priv->base + offset);
1280}
1281
1282static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1283{
1284 return readl(priv->base + offset);
1285}
1286
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001287static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1288 struct mvpp2_tx_desc *tx_desc,
1289 dma_addr_t dma_addr)
1290{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001291 if (port->priv->hw_version == MVPP21) {
1292 tx_desc->pp21.buf_dma_addr = dma_addr;
1293 } else {
1294 u64 val = (u64)dma_addr;
1295
1296 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1297 tx_desc->pp22.buf_dma_addr_ptp |= val;
1298 }
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001299}
1300
1301static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1302 struct mvpp2_tx_desc *tx_desc,
1303 size_t size)
1304{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001305 if (port->priv->hw_version == MVPP21)
1306 tx_desc->pp21.data_size = size;
1307 else
1308 tx_desc->pp22.data_size = size;
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001309}
1310
1311static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1312 struct mvpp2_tx_desc *tx_desc,
1313 unsigned int txq)
1314{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001315 if (port->priv->hw_version == MVPP21)
1316 tx_desc->pp21.phys_txq = txq;
1317 else
1318 tx_desc->pp22.phys_txq = txq;
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001319}
1320
1321static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1322 struct mvpp2_tx_desc *tx_desc,
1323 unsigned int command)
1324{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001325 if (port->priv->hw_version == MVPP21)
1326 tx_desc->pp21.command = command;
1327 else
1328 tx_desc->pp22.command = command;
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001329}
1330
1331static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1332 struct mvpp2_tx_desc *tx_desc,
1333 unsigned int offset)
1334{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001335 if (port->priv->hw_version == MVPP21)
1336 tx_desc->pp21.packet_offset = offset;
1337 else
1338 tx_desc->pp22.packet_offset = offset;
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001339}
1340
1341static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1342 struct mvpp2_rx_desc *rx_desc)
1343{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001344 if (port->priv->hw_version == MVPP21)
1345 return rx_desc->pp21.buf_dma_addr;
1346 else
1347 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001348}
1349
1350static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1351 struct mvpp2_rx_desc *rx_desc)
1352{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001353 if (port->priv->hw_version == MVPP21)
1354 return rx_desc->pp21.buf_cookie;
1355 else
1356 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001357}
1358
1359static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1360 struct mvpp2_rx_desc *rx_desc)
1361{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001362 if (port->priv->hw_version == MVPP21)
1363 return rx_desc->pp21.data_size;
1364 else
1365 return rx_desc->pp22.data_size;
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001366}
1367
1368static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1369 struct mvpp2_rx_desc *rx_desc)
1370{
Thomas Petazzoni56563ad2017-02-20 11:08:46 +01001371 if (port->priv->hw_version == MVPP21)
1372 return rx_desc->pp21.status;
1373 else
1374 return rx_desc->pp22.status;
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01001375}
1376
Stefan Roese96c19042016-02-10 07:22:10 +01001377static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1378{
1379 txq_pcpu->txq_get_index++;
1380 if (txq_pcpu->txq_get_index == txq_pcpu->size)
1381 txq_pcpu->txq_get_index = 0;
1382}
1383
1384/* Get number of physical egress port */
1385static inline int mvpp2_egress_port(struct mvpp2_port *port)
1386{
1387 return MVPP2_MAX_TCONT + port->id;
1388}
1389
1390/* Get number of physical TXQ */
1391static inline int mvpp2_txq_phys(int port, int txq)
1392{
1393 return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1394}
1395
1396/* Parser configuration routines */
1397
1398/* Update parser tcam and sram hw entries */
1399static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1400{
1401 int i;
1402
1403 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1404 return -EINVAL;
1405
1406 /* Clear entry invalidation bit */
1407 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1408
1409 /* Write tcam index - indirect access */
1410 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1411 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1412 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1413
1414 /* Write sram index - indirect access */
1415 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1416 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1417 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1418
1419 return 0;
1420}
1421
1422/* Read tcam entry from hw */
1423static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1424{
1425 int i;
1426
1427 if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1428 return -EINVAL;
1429
1430 /* Write tcam index - indirect access */
1431 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1432
1433 pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1434 MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1435 if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1436 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1437
1438 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1439 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1440
1441 /* Write sram index - indirect access */
1442 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1443 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1444 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1445
1446 return 0;
1447}
1448
1449/* Invalidate tcam hw entry */
1450static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1451{
1452 /* Write index - indirect access */
1453 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1454 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1455 MVPP2_PRS_TCAM_INV_MASK);
1456}
1457
1458/* Enable shadow table entry and set its lookup ID */
1459static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1460{
1461 priv->prs_shadow[index].valid = true;
1462 priv->prs_shadow[index].lu = lu;
1463}
1464
1465/* Update ri fields in shadow table entry */
1466static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1467 unsigned int ri, unsigned int ri_mask)
1468{
1469 priv->prs_shadow[index].ri_mask = ri_mask;
1470 priv->prs_shadow[index].ri = ri;
1471}
1472
1473/* Update lookup field in tcam sw entry */
1474static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1475{
1476 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1477
1478 pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1479 pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1480}
1481
1482/* Update mask for single port in tcam sw entry */
1483static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1484 unsigned int port, bool add)
1485{
1486 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1487
1488 if (add)
1489 pe->tcam.byte[enable_off] &= ~(1 << port);
1490 else
1491 pe->tcam.byte[enable_off] |= 1 << port;
1492}
1493
1494/* Update port map in tcam sw entry */
1495static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1496 unsigned int ports)
1497{
1498 unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1499 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1500
1501 pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1502 pe->tcam.byte[enable_off] &= ~port_mask;
1503 pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1504}
1505
1506/* Obtain port map from tcam sw entry */
1507static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1508{
1509 int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1510
1511 return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1512}
1513
1514/* Set byte of data and its enable bits in tcam sw entry */
1515static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1516 unsigned int offs, unsigned char byte,
1517 unsigned char enable)
1518{
1519 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1520 pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1521}
1522
1523/* Get byte of data and its enable bits from tcam sw entry */
1524static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1525 unsigned int offs, unsigned char *byte,
1526 unsigned char *enable)
1527{
1528 *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1529 *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1530}
1531
1532/* Set ethertype in tcam sw entry */
1533static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1534 unsigned short ethertype)
1535{
1536 mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1537 mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1538}
1539
1540/* Set bits in sram sw entry */
1541static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1542 int val)
1543{
1544 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1545}
1546
1547/* Clear bits in sram sw entry */
1548static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1549 int val)
1550{
1551 pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1552}
1553
1554/* Update ri bits in sram sw entry */
1555static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1556 unsigned int bits, unsigned int mask)
1557{
1558 unsigned int i;
1559
1560 for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1561 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1562
1563 if (!(mask & BIT(i)))
1564 continue;
1565
1566 if (bits & BIT(i))
1567 mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1568 else
1569 mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1570
1571 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1572 }
1573}
1574
1575/* Update ai bits in sram sw entry */
1576static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1577 unsigned int bits, unsigned int mask)
1578{
1579 unsigned int i;
1580 int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1581
1582 for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1583
1584 if (!(mask & BIT(i)))
1585 continue;
1586
1587 if (bits & BIT(i))
1588 mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1589 else
1590 mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1591
1592 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1593 }
1594}
1595
1596/* Read ai bits from sram sw entry */
1597static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1598{
1599 u8 bits;
1600 int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1601 int ai_en_off = ai_off + 1;
1602 int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1603
1604 bits = (pe->sram.byte[ai_off] >> ai_shift) |
1605 (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1606
1607 return bits;
1608}
1609
1610/* In sram sw entry set lookup ID field of the tcam key to be used in the next
1611 * lookup interation
1612 */
1613static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1614 unsigned int lu)
1615{
1616 int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1617
1618 mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1619 MVPP2_PRS_SRAM_NEXT_LU_MASK);
1620 mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1621}
1622
1623/* In the sram sw entry set sign and value of the next lookup offset
1624 * and the offset value generated to the classifier
1625 */
1626static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1627 unsigned int op)
1628{
1629 /* Set sign */
1630 if (shift < 0) {
1631 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1632 shift = 0 - shift;
1633 } else {
1634 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1635 }
1636
1637 /* Set value */
1638 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1639 (unsigned char)shift;
1640
1641 /* Reset and set operation */
1642 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1643 MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1644 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1645
1646 /* Set base offset as current */
1647 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1648}
1649
1650/* In the sram sw entry set sign and value of the user defined offset
1651 * generated to the classifier
1652 */
1653static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1654 unsigned int type, int offset,
1655 unsigned int op)
1656{
1657 /* Set sign */
1658 if (offset < 0) {
1659 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1660 offset = 0 - offset;
1661 } else {
1662 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1663 }
1664
1665 /* Set value */
1666 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1667 MVPP2_PRS_SRAM_UDF_MASK);
1668 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1669 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1670 MVPP2_PRS_SRAM_UDF_BITS)] &=
1671 ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1672 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1673 MVPP2_PRS_SRAM_UDF_BITS)] |=
1674 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1675
1676 /* Set offset type */
1677 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1678 MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1679 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1680
1681 /* Set offset operation */
1682 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1683 MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1684 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1685
1686 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1687 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1688 ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1689 (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1690
1691 pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1692 MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1693 (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1694
1695 /* Set base offset as current */
1696 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1697}
1698
1699/* Find parser flow entry */
1700static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1701{
1702 struct mvpp2_prs_entry *pe;
1703 int tid;
1704
1705 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1706 if (!pe)
1707 return NULL;
1708 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1709
1710 /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1711 for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1712 u8 bits;
1713
1714 if (!priv->prs_shadow[tid].valid ||
1715 priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1716 continue;
1717
1718 pe->index = tid;
1719 mvpp2_prs_hw_read(priv, pe);
1720 bits = mvpp2_prs_sram_ai_get(pe);
1721
1722 /* Sram store classification lookup ID in AI bits [5:0] */
1723 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1724 return pe;
1725 }
1726 kfree(pe);
1727
1728 return NULL;
1729}
1730
1731/* Return first free tcam index, seeking from start to end */
1732static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1733 unsigned char end)
1734{
1735 int tid;
1736
1737 if (start > end)
1738 swap(start, end);
1739
1740 if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1741 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1742
1743 for (tid = start; tid <= end; tid++) {
1744 if (!priv->prs_shadow[tid].valid)
1745 return tid;
1746 }
1747
1748 return -EINVAL;
1749}
1750
1751/* Enable/disable dropping all mac da's */
1752static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1753{
1754 struct mvpp2_prs_entry pe;
1755
1756 if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1757 /* Entry exist - update port only */
1758 pe.index = MVPP2_PE_DROP_ALL;
1759 mvpp2_prs_hw_read(priv, &pe);
1760 } else {
1761 /* Entry doesn't exist - create new */
1762 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1763 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1764 pe.index = MVPP2_PE_DROP_ALL;
1765
1766 /* Non-promiscuous mode for all ports - DROP unknown packets */
1767 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1768 MVPP2_PRS_RI_DROP_MASK);
1769
1770 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1771 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1772
1773 /* Update shadow table */
1774 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1775
1776 /* Mask all ports */
1777 mvpp2_prs_tcam_port_map_set(&pe, 0);
1778 }
1779
1780 /* Update port mask */
1781 mvpp2_prs_tcam_port_set(&pe, port, add);
1782
1783 mvpp2_prs_hw_write(priv, &pe);
1784}
1785
1786/* Set port to promiscuous mode */
1787static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1788{
1789 struct mvpp2_prs_entry pe;
1790
1791 /* Promiscuous mode - Accept unknown packets */
1792
1793 if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1794 /* Entry exist - update port only */
1795 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1796 mvpp2_prs_hw_read(priv, &pe);
1797 } else {
1798 /* Entry doesn't exist - create new */
1799 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1800 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1801 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1802
1803 /* Continue - set next lookup */
1804 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1805
1806 /* Set result info bits */
1807 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1808 MVPP2_PRS_RI_L2_CAST_MASK);
1809
1810 /* Shift to ethertype */
1811 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1812 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1813
1814 /* Mask all ports */
1815 mvpp2_prs_tcam_port_map_set(&pe, 0);
1816
1817 /* Update shadow table */
1818 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1819 }
1820
1821 /* Update port mask */
1822 mvpp2_prs_tcam_port_set(&pe, port, add);
1823
1824 mvpp2_prs_hw_write(priv, &pe);
1825}
1826
1827/* Accept multicast */
1828static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1829 bool add)
1830{
1831 struct mvpp2_prs_entry pe;
1832 unsigned char da_mc;
1833
1834 /* Ethernet multicast address first byte is
1835 * 0x01 for IPv4 and 0x33 for IPv6
1836 */
1837 da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1838
1839 if (priv->prs_shadow[index].valid) {
1840 /* Entry exist - update port only */
1841 pe.index = index;
1842 mvpp2_prs_hw_read(priv, &pe);
1843 } else {
1844 /* Entry doesn't exist - create new */
1845 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1846 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1847 pe.index = index;
1848
1849 /* Continue - set next lookup */
1850 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1851
1852 /* Set result info bits */
1853 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1854 MVPP2_PRS_RI_L2_CAST_MASK);
1855
1856 /* Update tcam entry data first byte */
1857 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1858
1859 /* Shift to ethertype */
1860 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1861 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1862
1863 /* Mask all ports */
1864 mvpp2_prs_tcam_port_map_set(&pe, 0);
1865
1866 /* Update shadow table */
1867 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1868 }
1869
1870 /* Update port mask */
1871 mvpp2_prs_tcam_port_set(&pe, port, add);
1872
1873 mvpp2_prs_hw_write(priv, &pe);
1874}
1875
1876/* Parser per-port initialization */
1877static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
1878 int lu_max, int offset)
1879{
1880 u32 val;
1881
1882 /* Set lookup ID */
1883 val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
1884 val &= ~MVPP2_PRS_PORT_LU_MASK(port);
1885 val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
1886 mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
1887
1888 /* Set maximum number of loops for packet received from port */
1889 val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
1890 val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
1891 val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
1892 mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
1893
1894 /* Set initial offset for packet header extraction for the first
1895 * searching loop
1896 */
1897 val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
1898 val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
1899 val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
1900 mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
1901}
1902
1903/* Default flow entries initialization for all ports */
1904static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
1905{
1906 struct mvpp2_prs_entry pe;
1907 int port;
1908
1909 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
1910 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1911 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1912 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
1913
1914 /* Mask all ports */
1915 mvpp2_prs_tcam_port_map_set(&pe, 0);
1916
1917 /* Set flow ID*/
1918 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
1919 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
1920
1921 /* Update shadow table and hw entry */
1922 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
1923 mvpp2_prs_hw_write(priv, &pe);
1924 }
1925}
1926
1927/* Set default entry for Marvell Header field */
1928static void mvpp2_prs_mh_init(struct mvpp2 *priv)
1929{
1930 struct mvpp2_prs_entry pe;
1931
1932 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1933
1934 pe.index = MVPP2_PE_MH_DEFAULT;
1935 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
1936 mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
1937 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1938 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
1939
1940 /* Unmask all ports */
1941 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1942
1943 /* Update shadow table and hw entry */
1944 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
1945 mvpp2_prs_hw_write(priv, &pe);
1946}
1947
1948/* Set default entires (place holder) for promiscuous, non-promiscuous and
1949 * multicast MAC addresses
1950 */
1951static void mvpp2_prs_mac_init(struct mvpp2 *priv)
1952{
1953 struct mvpp2_prs_entry pe;
1954
1955 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1956
1957 /* Non-promiscuous mode for all ports - DROP unknown packets */
1958 pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
1959 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1960
1961 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1962 MVPP2_PRS_RI_DROP_MASK);
1963 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1964 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1965
1966 /* Unmask all ports */
1967 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1968
1969 /* Update shadow table and hw entry */
1970 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1971 mvpp2_prs_hw_write(priv, &pe);
1972
1973 /* place holders only - no ports */
1974 mvpp2_prs_mac_drop_all_set(priv, 0, false);
1975 mvpp2_prs_mac_promisc_set(priv, 0, false);
1976 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
1977 mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
1978}
1979
1980/* Match basic ethertypes */
1981static int mvpp2_prs_etype_init(struct mvpp2 *priv)
1982{
1983 struct mvpp2_prs_entry pe;
1984 int tid;
1985
1986 /* Ethertype: PPPoE */
1987 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1988 MVPP2_PE_LAST_FREE_TID);
1989 if (tid < 0)
1990 return tid;
1991
1992 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1993 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1994 pe.index = tid;
1995
1996 mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
1997
1998 mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
1999 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2000 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2001 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2002 MVPP2_PRS_RI_PPPOE_MASK);
2003
2004 /* Update shadow table and hw entry */
2005 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2006 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2007 priv->prs_shadow[pe.index].finish = false;
2008 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2009 MVPP2_PRS_RI_PPPOE_MASK);
2010 mvpp2_prs_hw_write(priv, &pe);
2011
2012 /* Ethertype: ARP */
2013 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2014 MVPP2_PE_LAST_FREE_TID);
2015 if (tid < 0)
2016 return tid;
2017
2018 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2019 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2020 pe.index = tid;
2021
2022 mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
2023
2024 /* Generate flow in the next iteration*/
2025 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2026 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2027 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2028 MVPP2_PRS_RI_L3_PROTO_MASK);
2029 /* Set L3 offset */
2030 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2031 MVPP2_ETH_TYPE_LEN,
2032 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2033
2034 /* Update shadow table and hw entry */
2035 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2036 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2037 priv->prs_shadow[pe.index].finish = true;
2038 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2039 MVPP2_PRS_RI_L3_PROTO_MASK);
2040 mvpp2_prs_hw_write(priv, &pe);
2041
2042 /* Ethertype: LBTD */
2043 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2044 MVPP2_PE_LAST_FREE_TID);
2045 if (tid < 0)
2046 return tid;
2047
2048 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2049 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2050 pe.index = tid;
2051
2052 mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2053
2054 /* Generate flow in the next iteration*/
2055 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2056 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2057 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2058 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2059 MVPP2_PRS_RI_CPU_CODE_MASK |
2060 MVPP2_PRS_RI_UDF3_MASK);
2061 /* Set L3 offset */
2062 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2063 MVPP2_ETH_TYPE_LEN,
2064 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2065
2066 /* Update shadow table and hw entry */
2067 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2068 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2069 priv->prs_shadow[pe.index].finish = true;
2070 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2071 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2072 MVPP2_PRS_RI_CPU_CODE_MASK |
2073 MVPP2_PRS_RI_UDF3_MASK);
2074 mvpp2_prs_hw_write(priv, &pe);
2075
2076 /* Ethertype: IPv4 without options */
2077 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2078 MVPP2_PE_LAST_FREE_TID);
2079 if (tid < 0)
2080 return tid;
2081
2082 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2083 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2084 pe.index = tid;
2085
2086 mvpp2_prs_match_etype(&pe, 0, PROT_IP);
2087 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2088 MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2089 MVPP2_PRS_IPV4_HEAD_MASK |
2090 MVPP2_PRS_IPV4_IHL_MASK);
2091
2092 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2093 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2094 MVPP2_PRS_RI_L3_PROTO_MASK);
2095 /* Skip eth_type + 4 bytes of IP header */
2096 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2097 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2098 /* Set L3 offset */
2099 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2100 MVPP2_ETH_TYPE_LEN,
2101 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2102
2103 /* Update shadow table and hw entry */
2104 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2105 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2106 priv->prs_shadow[pe.index].finish = false;
2107 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2108 MVPP2_PRS_RI_L3_PROTO_MASK);
2109 mvpp2_prs_hw_write(priv, &pe);
2110
2111 /* Ethertype: IPv4 with options */
2112 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2113 MVPP2_PE_LAST_FREE_TID);
2114 if (tid < 0)
2115 return tid;
2116
2117 pe.index = tid;
2118
2119 /* Clear tcam data before updating */
2120 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2121 pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2122
2123 mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2124 MVPP2_PRS_IPV4_HEAD,
2125 MVPP2_PRS_IPV4_HEAD_MASK);
2126
2127 /* Clear ri before updating */
2128 pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2129 pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2130 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2131 MVPP2_PRS_RI_L3_PROTO_MASK);
2132
2133 /* Update shadow table and hw entry */
2134 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2135 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2136 priv->prs_shadow[pe.index].finish = false;
2137 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2138 MVPP2_PRS_RI_L3_PROTO_MASK);
2139 mvpp2_prs_hw_write(priv, &pe);
2140
2141 /* Ethertype: IPv6 without options */
2142 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2143 MVPP2_PE_LAST_FREE_TID);
2144 if (tid < 0)
2145 return tid;
2146
2147 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2148 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2149 pe.index = tid;
2150
2151 mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
2152
2153 /* Skip DIP of IPV6 header */
2154 mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2155 MVPP2_MAX_L3_ADDR_SIZE,
2156 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2157 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2158 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2159 MVPP2_PRS_RI_L3_PROTO_MASK);
2160 /* Set L3 offset */
2161 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2162 MVPP2_ETH_TYPE_LEN,
2163 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2164
2165 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2166 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2167 priv->prs_shadow[pe.index].finish = false;
2168 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2169 MVPP2_PRS_RI_L3_PROTO_MASK);
2170 mvpp2_prs_hw_write(priv, &pe);
2171
2172 /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2173 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2174 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2175 pe.index = MVPP2_PE_ETH_TYPE_UN;
2176
2177 /* Unmask all ports */
2178 mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2179
2180 /* Generate flow in the next iteration*/
2181 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2182 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2183 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2184 MVPP2_PRS_RI_L3_PROTO_MASK);
2185 /* Set L3 offset even it's unknown L3 */
2186 mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2187 MVPP2_ETH_TYPE_LEN,
2188 MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2189
2190 /* Update shadow table and hw entry */
2191 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2192 priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2193 priv->prs_shadow[pe.index].finish = true;
2194 mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2195 MVPP2_PRS_RI_L3_PROTO_MASK);
2196 mvpp2_prs_hw_write(priv, &pe);
2197
2198 return 0;
2199}
2200
2201/* Parser default initialization */
2202static int mvpp2_prs_default_init(struct udevice *dev,
2203 struct mvpp2 *priv)
2204{
2205 int err, index, i;
2206
2207 /* Enable tcam table */
2208 mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
2209
2210 /* Clear all tcam and sram entries */
2211 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
2212 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
2213 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
2214 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
2215
2216 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
2217 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
2218 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
2219 }
2220
2221 /* Invalidate all tcam entries */
2222 for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
2223 mvpp2_prs_hw_inv(priv, index);
2224
2225 priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
2226 sizeof(struct mvpp2_prs_shadow),
2227 GFP_KERNEL);
2228 if (!priv->prs_shadow)
2229 return -ENOMEM;
2230
2231 /* Always start from lookup = 0 */
2232 for (index = 0; index < MVPP2_MAX_PORTS; index++)
2233 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
2234 MVPP2_PRS_PORT_LU_MAX, 0);
2235
2236 mvpp2_prs_def_flow_init(priv);
2237
2238 mvpp2_prs_mh_init(priv);
2239
2240 mvpp2_prs_mac_init(priv);
2241
2242 err = mvpp2_prs_etype_init(priv);
2243 if (err)
2244 return err;
2245
2246 return 0;
2247}
2248
2249/* Compare MAC DA with tcam entry data */
2250static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
2251 const u8 *da, unsigned char *mask)
2252{
2253 unsigned char tcam_byte, tcam_mask;
2254 int index;
2255
2256 for (index = 0; index < ETH_ALEN; index++) {
2257 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
2258 if (tcam_mask != mask[index])
2259 return false;
2260
2261 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
2262 return false;
2263 }
2264
2265 return true;
2266}
2267
2268/* Find tcam entry with matched pair <MAC DA, port> */
2269static struct mvpp2_prs_entry *
2270mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
2271 unsigned char *mask, int udf_type)
2272{
2273 struct mvpp2_prs_entry *pe;
2274 int tid;
2275
2276 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2277 if (!pe)
2278 return NULL;
2279 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2280
2281 /* Go through the all entires with MVPP2_PRS_LU_MAC */
2282 for (tid = MVPP2_PE_FIRST_FREE_TID;
2283 tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2284 unsigned int entry_pmap;
2285
2286 if (!priv->prs_shadow[tid].valid ||
2287 (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
2288 (priv->prs_shadow[tid].udf != udf_type))
2289 continue;
2290
2291 pe->index = tid;
2292 mvpp2_prs_hw_read(priv, pe);
2293 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
2294
2295 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
2296 entry_pmap == pmap)
2297 return pe;
2298 }
2299 kfree(pe);
2300
2301 return NULL;
2302}
2303
2304/* Update parser's mac da entry */
2305static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
2306 const u8 *da, bool add)
2307{
2308 struct mvpp2_prs_entry *pe;
2309 unsigned int pmap, len, ri;
2310 unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2311 int tid;
2312
2313 /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
2314 pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
2315 MVPP2_PRS_UDF_MAC_DEF);
2316
2317 /* No such entry */
2318 if (!pe) {
2319 if (!add)
2320 return 0;
2321
2322 /* Create new TCAM entry */
2323 /* Find first range mac entry*/
2324 for (tid = MVPP2_PE_FIRST_FREE_TID;
2325 tid <= MVPP2_PE_LAST_FREE_TID; tid++)
2326 if (priv->prs_shadow[tid].valid &&
2327 (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
2328 (priv->prs_shadow[tid].udf ==
2329 MVPP2_PRS_UDF_MAC_RANGE))
2330 break;
2331
2332 /* Go through the all entries from first to last */
2333 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2334 tid - 1);
2335 if (tid < 0)
2336 return tid;
2337
2338 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2339 if (!pe)
2340 return -1;
2341 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2342 pe->index = tid;
2343
2344 /* Mask all ports */
2345 mvpp2_prs_tcam_port_map_set(pe, 0);
2346 }
2347
2348 /* Update port mask */
2349 mvpp2_prs_tcam_port_set(pe, port, add);
2350
2351 /* Invalidate the entry if no ports are left enabled */
2352 pmap = mvpp2_prs_tcam_port_map_get(pe);
2353 if (pmap == 0) {
2354 if (add) {
2355 kfree(pe);
2356 return -1;
2357 }
2358 mvpp2_prs_hw_inv(priv, pe->index);
2359 priv->prs_shadow[pe->index].valid = false;
2360 kfree(pe);
2361 return 0;
2362 }
2363
2364 /* Continue - set next lookup */
2365 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
2366
2367 /* Set match on DA */
2368 len = ETH_ALEN;
2369 while (len--)
2370 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
2371
2372 /* Set result info bits */
2373 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
2374
2375 mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2376 MVPP2_PRS_RI_MAC_ME_MASK);
2377 mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2378 MVPP2_PRS_RI_MAC_ME_MASK);
2379
2380 /* Shift to ethertype */
2381 mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
2382 MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2383
2384 /* Update shadow table and hw entry */
2385 priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
2386 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
2387 mvpp2_prs_hw_write(priv, pe);
2388
2389 kfree(pe);
2390
2391 return 0;
2392}
2393
2394static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
2395{
2396 int err;
2397
2398 /* Remove old parser entry */
2399 err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
2400 false);
2401 if (err)
2402 return err;
2403
2404 /* Add new parser entry */
2405 err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
2406 if (err)
2407 return err;
2408
2409 /* Set addr in the device */
2410 memcpy(port->dev_addr, da, ETH_ALEN);
2411
2412 return 0;
2413}
2414
2415/* Set prs flow for the port */
2416static int mvpp2_prs_def_flow(struct mvpp2_port *port)
2417{
2418 struct mvpp2_prs_entry *pe;
2419 int tid;
2420
2421 pe = mvpp2_prs_flow_find(port->priv, port->id);
2422
2423 /* Such entry not exist */
2424 if (!pe) {
2425 /* Go through the all entires from last to first */
2426 tid = mvpp2_prs_tcam_first_free(port->priv,
2427 MVPP2_PE_LAST_FREE_TID,
2428 MVPP2_PE_FIRST_FREE_TID);
2429 if (tid < 0)
2430 return tid;
2431
2432 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2433 if (!pe)
2434 return -ENOMEM;
2435
2436 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
2437 pe->index = tid;
2438
2439 /* Set flow ID*/
2440 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
2441 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2442
2443 /* Update shadow table */
2444 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
2445 }
2446
2447 mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
2448 mvpp2_prs_hw_write(port->priv, pe);
2449 kfree(pe);
2450
2451 return 0;
2452}
2453
2454/* Classifier configuration routines */
2455
2456/* Update classification flow table registers */
2457static void mvpp2_cls_flow_write(struct mvpp2 *priv,
2458 struct mvpp2_cls_flow_entry *fe)
2459{
2460 mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
2461 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
2462 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
2463 mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
2464}
2465
2466/* Update classification lookup table register */
2467static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
2468 struct mvpp2_cls_lookup_entry *le)
2469{
2470 u32 val;
2471
2472 val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
2473 mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
2474 mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
2475}
2476
2477/* Classifier default initialization */
2478static void mvpp2_cls_init(struct mvpp2 *priv)
2479{
2480 struct mvpp2_cls_lookup_entry le;
2481 struct mvpp2_cls_flow_entry fe;
2482 int index;
2483
2484 /* Enable classifier */
2485 mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
2486
2487 /* Clear classifier flow table */
2488 memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
2489 for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
2490 fe.index = index;
2491 mvpp2_cls_flow_write(priv, &fe);
2492 }
2493
2494 /* Clear classifier lookup table */
2495 le.data = 0;
2496 for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
2497 le.lkpid = index;
2498 le.way = 0;
2499 mvpp2_cls_lookup_write(priv, &le);
2500
2501 le.way = 1;
2502 mvpp2_cls_lookup_write(priv, &le);
2503 }
2504}
2505
2506static void mvpp2_cls_port_config(struct mvpp2_port *port)
2507{
2508 struct mvpp2_cls_lookup_entry le;
2509 u32 val;
2510
2511 /* Set way for the port */
2512 val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
2513 val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
2514 mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
2515
2516 /* Pick the entry to be accessed in lookup ID decoding table
2517 * according to the way and lkpid.
2518 */
2519 le.lkpid = port->id;
2520 le.way = 0;
2521 le.data = 0;
2522
2523 /* Set initial CPU queue for receiving packets */
2524 le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
2525 le.data |= port->first_rxq;
2526
2527 /* Disable classification engines */
2528 le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
2529
2530 /* Update lookup ID table entry */
2531 mvpp2_cls_lookup_write(port->priv, &le);
2532}
2533
2534/* Set CPU queue number for oversize packets */
2535static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
2536{
2537 u32 val;
2538
2539 mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
2540 port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
2541
2542 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
2543 (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
2544
2545 val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
2546 val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
2547 mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
2548}
2549
2550/* Buffer Manager configuration routines */
2551
2552/* Create pool */
2553static int mvpp2_bm_pool_create(struct udevice *dev,
2554 struct mvpp2 *priv,
2555 struct mvpp2_bm_pool *bm_pool, int size)
2556{
2557 u32 val;
2558
Thomas Petazzoni3520a332017-02-20 11:29:16 +01002559 /* Number of buffer pointers must be a multiple of 16, as per
2560 * hardware constraints
2561 */
2562 if (!IS_ALIGNED(size, 16))
2563 return -EINVAL;
2564
Stefan Roese96c19042016-02-10 07:22:10 +01002565 bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
Thomas Petazzonic49aff22017-02-20 10:27:51 +01002566 bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
Stefan Roese96c19042016-02-10 07:22:10 +01002567 if (!bm_pool->virt_addr)
2568 return -ENOMEM;
2569
Thomas Petazzonia3c988f2017-02-15 12:31:53 +01002570 if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
2571 MVPP2_BM_POOL_PTR_ALIGN)) {
Sean Anderson77a88792020-09-15 10:44:56 -04002572 dev_err(dev, "BM pool %d is not %d bytes aligned\n",
Stefan Roese96c19042016-02-10 07:22:10 +01002573 bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
2574 return -ENOMEM;
2575 }
2576
2577 mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
Thomas Petazzoni3520a332017-02-20 11:29:16 +01002578 lower_32_bits(bm_pool->dma_addr));
Stefan Chulski115f76f2017-08-09 10:37:50 +03002579 if (priv->hw_version == MVPP22)
2580 mvpp2_write(priv, MVPP22_BM_POOL_BASE_HIGH_REG,
2581 (upper_32_bits(bm_pool->dma_addr) &
2582 MVPP22_BM_POOL_BASE_HIGH_MASK));
Stefan Roese96c19042016-02-10 07:22:10 +01002583 mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
2584
2585 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2586 val |= MVPP2_BM_START_MASK;
2587 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2588
2589 bm_pool->type = MVPP2_BM_FREE;
2590 bm_pool->size = size;
2591 bm_pool->pkt_size = 0;
2592 bm_pool->buf_num = 0;
2593
2594 return 0;
2595}
2596
2597/* Set pool buffer size */
2598static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
2599 struct mvpp2_bm_pool *bm_pool,
2600 int buf_size)
2601{
2602 u32 val;
2603
2604 bm_pool->buf_size = buf_size;
2605
2606 val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
2607 mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
2608}
2609
2610/* Free all buffers from the pool */
2611static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
2612 struct mvpp2_bm_pool *bm_pool)
2613{
Stefan Roese380b3232017-03-23 17:01:59 +01002614 int i;
2615
2616 for (i = 0; i < bm_pool->buf_num; i++) {
2617 /* Allocate buffer back from the buffer manager */
2618 mvpp2_read(priv, MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
2619 }
2620
Stefan Roese96c19042016-02-10 07:22:10 +01002621 bm_pool->buf_num = 0;
2622}
2623
2624/* Cleanup pool */
2625static int mvpp2_bm_pool_destroy(struct udevice *dev,
2626 struct mvpp2 *priv,
2627 struct mvpp2_bm_pool *bm_pool)
2628{
2629 u32 val;
2630
2631 mvpp2_bm_bufs_free(dev, priv, bm_pool);
2632 if (bm_pool->buf_num) {
2633 dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
2634 return 0;
2635 }
2636
2637 val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2638 val |= MVPP2_BM_STOP_MASK;
2639 mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2640
2641 return 0;
2642}
2643
2644static int mvpp2_bm_pools_init(struct udevice *dev,
2645 struct mvpp2 *priv)
2646{
2647 int i, err, size;
2648 struct mvpp2_bm_pool *bm_pool;
2649
2650 /* Create all pools with maximum size */
2651 size = MVPP2_BM_POOL_SIZE_MAX;
2652 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2653 bm_pool = &priv->bm_pools[i];
2654 bm_pool->id = i;
2655 err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
2656 if (err)
2657 goto err_unroll_pools;
Stefan Chulski36eb98a2017-08-09 10:37:52 +03002658 mvpp2_bm_pool_bufsize_set(priv, bm_pool, RX_BUFFER_SIZE);
Stefan Roese96c19042016-02-10 07:22:10 +01002659 }
2660 return 0;
2661
2662err_unroll_pools:
Sean Anderson77a88792020-09-15 10:44:56 -04002663 dev_err(dev, "failed to create BM pool %d, size %d\n", i, size);
Stefan Roese96c19042016-02-10 07:22:10 +01002664 for (i = i - 1; i >= 0; i--)
2665 mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
2666 return err;
2667}
2668
2669static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
2670{
2671 int i, err;
2672
2673 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2674 /* Mask BM all interrupts */
2675 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
2676 /* Clear BM cause register */
2677 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
2678 }
2679
2680 /* Allocate and initialize BM pools */
2681 priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
2682 sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
2683 if (!priv->bm_pools)
2684 return -ENOMEM;
2685
2686 err = mvpp2_bm_pools_init(dev, priv);
2687 if (err < 0)
2688 return err;
2689 return 0;
2690}
2691
2692/* Attach long pool to rxq */
2693static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
2694 int lrxq, int long_pool)
2695{
Thomas Petazzoni2321c922017-02-16 06:53:51 +01002696 u32 val, mask;
Stefan Roese96c19042016-02-10 07:22:10 +01002697 int prxq;
2698
2699 /* Get queue physical ID */
2700 prxq = port->rxqs[lrxq]->id;
2701
Thomas Petazzoni2321c922017-02-16 06:53:51 +01002702 if (port->priv->hw_version == MVPP21)
2703 mask = MVPP21_RXQ_POOL_LONG_MASK;
2704 else
2705 mask = MVPP22_RXQ_POOL_LONG_MASK;
Stefan Roese96c19042016-02-10 07:22:10 +01002706
Thomas Petazzoni2321c922017-02-16 06:53:51 +01002707 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2708 val &= ~mask;
2709 val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
Stefan Roese96c19042016-02-10 07:22:10 +01002710 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2711}
2712
2713/* Set pool number in a BM cookie */
2714static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
2715{
2716 u32 bm;
2717
2718 bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
2719 bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
2720
2721 return bm;
2722}
2723
2724/* Get pool number from a BM cookie */
Thomas Petazzonia3c988f2017-02-15 12:31:53 +01002725static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
Stefan Roese96c19042016-02-10 07:22:10 +01002726{
2727 return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
2728}
2729
2730/* Release buffer to BM */
2731static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
Thomas Petazzonic49aff22017-02-20 10:27:51 +01002732 dma_addr_t buf_dma_addr,
Thomas Petazzoni09831762017-02-20 10:37:59 +01002733 unsigned long buf_phys_addr)
Stefan Roese96c19042016-02-10 07:22:10 +01002734{
Thomas Petazzoni3520a332017-02-20 11:29:16 +01002735 if (port->priv->hw_version == MVPP22) {
2736 u32 val = 0;
2737
2738 if (sizeof(dma_addr_t) == 8)
2739 val |= upper_32_bits(buf_dma_addr) &
2740 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
2741
2742 if (sizeof(phys_addr_t) == 8)
2743 val |= (upper_32_bits(buf_phys_addr)
2744 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
2745 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
2746
2747 mvpp2_write(port->priv, MVPP22_BM_ADDR_HIGH_RLS_REG, val);
2748 }
2749
Thomas Petazzoni09831762017-02-20 10:37:59 +01002750 /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
2751 * returned in the "cookie" field of the RX
2752 * descriptor. Instead of storing the virtual address, we
2753 * store the physical address
2754 */
2755 mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
Thomas Petazzonic49aff22017-02-20 10:27:51 +01002756 mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
Stefan Roese96c19042016-02-10 07:22:10 +01002757}
2758
2759/* Refill BM pool */
2760static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
Thomas Petazzonic49aff22017-02-20 10:27:51 +01002761 dma_addr_t dma_addr,
Thomas Petazzoni09831762017-02-20 10:37:59 +01002762 phys_addr_t phys_addr)
Stefan Roese96c19042016-02-10 07:22:10 +01002763{
2764 int pool = mvpp2_bm_cookie_pool_get(bm);
2765
Thomas Petazzoni09831762017-02-20 10:37:59 +01002766 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
Stefan Roese96c19042016-02-10 07:22:10 +01002767}
2768
2769/* Allocate buffers for the pool */
2770static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
2771 struct mvpp2_bm_pool *bm_pool, int buf_num)
2772{
2773 int i;
Stefan Roese96c19042016-02-10 07:22:10 +01002774
2775 if (buf_num < 0 ||
2776 (buf_num + bm_pool->buf_num > bm_pool->size)) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04002777 dev_err(port->phy_dev->dev,
2778 "cannot allocate %d buffers for pool %d\n", buf_num,
2779 bm_pool->id);
Stefan Roese96c19042016-02-10 07:22:10 +01002780 return 0;
2781 }
2782
Stefan Roese96c19042016-02-10 07:22:10 +01002783 for (i = 0; i < buf_num; i++) {
Thomas Petazzoni1b085c52017-02-15 12:13:43 +01002784 mvpp2_bm_pool_put(port, bm_pool->id,
Thomas Petazzonia3c988f2017-02-15 12:31:53 +01002785 (dma_addr_t)buffer_loc.rx_buffer[i],
2786 (unsigned long)buffer_loc.rx_buffer[i]);
Thomas Petazzoni1b085c52017-02-15 12:13:43 +01002787
Stefan Roese96c19042016-02-10 07:22:10 +01002788 }
2789
2790 /* Update BM driver with number of buffers added to pool */
2791 bm_pool->buf_num += i;
Stefan Roese96c19042016-02-10 07:22:10 +01002792
2793 return i;
2794}
2795
2796/* Notify the driver that BM pool is being used as specific type and return the
2797 * pool pointer on success
2798 */
2799static struct mvpp2_bm_pool *
2800mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
2801 int pkt_size)
2802{
2803 struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
2804 int num;
2805
2806 if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04002807 dev_err(port->phy_dev->dev, "mixing pool types is forbidden\n");
Stefan Roese96c19042016-02-10 07:22:10 +01002808 return NULL;
2809 }
2810
2811 if (new_pool->type == MVPP2_BM_FREE)
2812 new_pool->type = type;
2813
2814 /* Allocate buffers in case BM pool is used as long pool, but packet
2815 * size doesn't match MTU or BM pool hasn't being used yet
2816 */
2817 if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
2818 (new_pool->pkt_size == 0)) {
2819 int pkts_num;
2820
2821 /* Set default buffer number or free all the buffers in case
2822 * the pool is not empty
2823 */
2824 pkts_num = new_pool->buf_num;
2825 if (pkts_num == 0)
2826 pkts_num = type == MVPP2_BM_SWF_LONG ?
2827 MVPP2_BM_LONG_BUF_NUM :
2828 MVPP2_BM_SHORT_BUF_NUM;
2829 else
2830 mvpp2_bm_bufs_free(NULL,
2831 port->priv, new_pool);
2832
2833 new_pool->pkt_size = pkt_size;
2834
2835 /* Allocate buffers for this pool */
2836 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
2837 if (num != pkts_num) {
Sean Anderson77a88792020-09-15 10:44:56 -04002838 dev_err(port->phy_dev->dev,
2839 "pool %d: %d of %d allocated\n", new_pool->id,
2840 num, pkts_num);
Stefan Roese96c19042016-02-10 07:22:10 +01002841 return NULL;
2842 }
2843 }
2844
Stefan Roese96c19042016-02-10 07:22:10 +01002845 return new_pool;
2846}
2847
2848/* Initialize pools for swf */
2849static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
2850{
2851 int rxq;
2852
2853 if (!port->pool_long) {
2854 port->pool_long =
2855 mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
2856 MVPP2_BM_SWF_LONG,
2857 port->pkt_size);
2858 if (!port->pool_long)
2859 return -ENOMEM;
2860
2861 port->pool_long->port_map |= (1 << port->id);
2862
2863 for (rxq = 0; rxq < rxq_number; rxq++)
2864 mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
2865 }
2866
2867 return 0;
2868}
2869
2870/* Port configuration routines */
2871
2872static void mvpp2_port_mii_set(struct mvpp2_port *port)
2873{
2874 u32 val;
2875
2876 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
2877
2878 switch (port->phy_interface) {
2879 case PHY_INTERFACE_MODE_SGMII:
2880 val |= MVPP2_GMAC_INBAND_AN_MASK;
2881 break;
2882 case PHY_INTERFACE_MODE_RGMII:
Stefan Roesec20e4e42017-03-22 15:11:00 +01002883 case PHY_INTERFACE_MODE_RGMII_ID:
Stefan Roese96c19042016-02-10 07:22:10 +01002884 val |= MVPP2_GMAC_PORT_RGMII_MASK;
2885 default:
2886 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
2887 }
2888
2889 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2890}
2891
2892static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
2893{
2894 u32 val;
2895
2896 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2897 val |= MVPP2_GMAC_FC_ADV_EN;
2898 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2899}
2900
2901static void mvpp2_port_enable(struct mvpp2_port *port)
2902{
2903 u32 val;
2904
2905 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2906 val |= MVPP2_GMAC_PORT_EN_MASK;
2907 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
2908 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2909}
2910
2911static void mvpp2_port_disable(struct mvpp2_port *port)
2912{
2913 u32 val;
2914
2915 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2916 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
2917 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2918}
2919
2920/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
2921static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
2922{
2923 u32 val;
2924
2925 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
2926 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
2927 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2928}
2929
2930/* Configure loopback port */
2931static void mvpp2_port_loopback_set(struct mvpp2_port *port)
2932{
2933 u32 val;
2934
2935 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
2936
2937 if (port->speed == 1000)
2938 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
2939 else
2940 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
2941
2942 if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
2943 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
2944 else
2945 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
2946
2947 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2948}
2949
2950static void mvpp2_port_reset(struct mvpp2_port *port)
2951{
2952 u32 val;
2953
2954 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2955 ~MVPP2_GMAC_PORT_RESET_MASK;
2956 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2957
2958 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2959 MVPP2_GMAC_PORT_RESET_MASK)
2960 continue;
2961}
2962
2963/* Change maximum receive size of the port */
2964static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
2965{
2966 u32 val;
2967
2968 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2969 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
2970 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2971 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
2972 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2973}
2974
Stefan Roese40e749b2017-03-22 15:07:30 +01002975/* PPv2.2 GoP/GMAC config */
2976
2977/* Set the MAC to reset or exit from reset */
2978static int gop_gmac_reset(struct mvpp2_port *port, int reset)
2979{
2980 u32 val;
2981
2982 /* read - modify - write */
2983 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
2984 if (reset)
2985 val |= MVPP2_GMAC_PORT_RESET_MASK;
2986 else
2987 val &= ~MVPP2_GMAC_PORT_RESET_MASK;
2988 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2989
2990 return 0;
2991}
2992
2993/*
2994 * gop_gpcs_mode_cfg
2995 *
2996 * Configure port to working with Gig PCS or don't.
2997 */
2998static int gop_gpcs_mode_cfg(struct mvpp2_port *port, int en)
2999{
3000 u32 val;
3001
3002 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
3003 if (en)
3004 val |= MVPP2_GMAC_PCS_ENABLE_MASK;
3005 else
3006 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
3007 /* enable / disable PCS on this port */
3008 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
3009
3010 return 0;
3011}
3012
3013static int gop_bypass_clk_cfg(struct mvpp2_port *port, int en)
3014{
3015 u32 val;
3016
3017 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
3018 if (en)
3019 val |= MVPP2_GMAC_CLK_125_BYPS_EN_MASK;
3020 else
3021 val &= ~MVPP2_GMAC_CLK_125_BYPS_EN_MASK;
3022 /* enable / disable PCS on this port */
3023 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
3024
3025 return 0;
3026}
3027
3028static void gop_gmac_sgmii2_5_cfg(struct mvpp2_port *port)
3029{
3030 u32 val, thresh;
3031
3032 /*
3033 * Configure minimal level of the Tx FIFO before the lower part
3034 * starts to read a packet
3035 */
3036 thresh = MVPP2_SGMII2_5_TX_FIFO_MIN_TH;
3037 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3038 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
3039 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(thresh);
3040 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3041
3042 /* Disable bypass of sync module */
3043 val = readl(port->base + MVPP2_GMAC_CTRL_4_REG);
3044 val |= MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK;
3045 /* configure DP clock select according to mode */
3046 val |= MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK;
3047 /* configure QSGMII bypass according to mode */
3048 val |= MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK;
3049 writel(val, port->base + MVPP2_GMAC_CTRL_4_REG);
3050
Stefan Roese40e749b2017-03-22 15:07:30 +01003051 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3052 /*
3053 * Configure GIG MAC to 1000Base-X mode connected to a fiber
3054 * transceiver
3055 */
3056 val |= MVPP2_GMAC_PORT_TYPE_MASK;
3057 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3058
3059 /* configure AN 0x9268 */
3060 val = MVPP2_GMAC_EN_PCS_AN |
3061 MVPP2_GMAC_AN_BYPASS_EN |
3062 MVPP2_GMAC_CONFIG_MII_SPEED |
3063 MVPP2_GMAC_CONFIG_GMII_SPEED |
3064 MVPP2_GMAC_FC_ADV_EN |
3065 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
3066 MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG;
3067 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3068}
3069
3070static void gop_gmac_sgmii_cfg(struct mvpp2_port *port)
3071{
3072 u32 val, thresh;
3073
3074 /*
3075 * Configure minimal level of the Tx FIFO before the lower part
3076 * starts to read a packet
3077 */
3078 thresh = MVPP2_SGMII_TX_FIFO_MIN_TH;
3079 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3080 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
3081 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(thresh);
3082 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3083
3084 /* Disable bypass of sync module */
3085 val = readl(port->base + MVPP2_GMAC_CTRL_4_REG);
3086 val |= MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK;
3087 /* configure DP clock select according to mode */
3088 val &= ~MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK;
3089 /* configure QSGMII bypass according to mode */
3090 val |= MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK;
3091 writel(val, port->base + MVPP2_GMAC_CTRL_4_REG);
3092
Stefan Roese40e749b2017-03-22 15:07:30 +01003093 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3094 /* configure GIG MAC to SGMII mode */
3095 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
3096 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3097
3098 /* configure AN */
3099 val = MVPP2_GMAC_EN_PCS_AN |
3100 MVPP2_GMAC_AN_BYPASS_EN |
3101 MVPP2_GMAC_AN_SPEED_EN |
3102 MVPP2_GMAC_EN_FC_AN |
3103 MVPP2_GMAC_AN_DUPLEX_EN |
3104 MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG;
3105 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3106}
3107
3108static void gop_gmac_rgmii_cfg(struct mvpp2_port *port)
3109{
3110 u32 val, thresh;
3111
3112 /*
3113 * Configure minimal level of the Tx FIFO before the lower part
3114 * starts to read a packet
3115 */
3116 thresh = MVPP2_RGMII_TX_FIFO_MIN_TH;
3117 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3118 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
3119 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(thresh);
3120 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3121
3122 /* Disable bypass of sync module */
3123 val = readl(port->base + MVPP2_GMAC_CTRL_4_REG);
3124 val |= MVPP2_GMAC_CTRL4_SYNC_BYPASS_MASK;
3125 /* configure DP clock select according to mode */
3126 val &= ~MVPP2_GMAC_CTRL4_DP_CLK_SEL_MASK;
3127 val |= MVPP2_GMAC_CTRL4_QSGMII_BYPASS_ACTIVE_MASK;
3128 val |= MVPP2_GMAC_CTRL4_EXT_PIN_GMII_SEL_MASK;
3129 writel(val, port->base + MVPP2_GMAC_CTRL_4_REG);
3130
Stefan Roese40e749b2017-03-22 15:07:30 +01003131 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3132 /* configure GIG MAC to SGMII mode */
3133 val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
3134 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3135
3136 /* configure AN 0xb8e8 */
3137 val = MVPP2_GMAC_AN_BYPASS_EN |
3138 MVPP2_GMAC_AN_SPEED_EN |
3139 MVPP2_GMAC_EN_FC_AN |
3140 MVPP2_GMAC_AN_DUPLEX_EN |
3141 MVPP2_GMAC_CHOOSE_SAMPLE_TX_CONFIG;
3142 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3143}
3144
3145/* Set the internal mux's to the required MAC in the GOP */
3146static int gop_gmac_mode_cfg(struct mvpp2_port *port)
3147{
3148 u32 val;
3149
3150 /* Set TX FIFO thresholds */
3151 switch (port->phy_interface) {
3152 case PHY_INTERFACE_MODE_SGMII:
3153 if (port->phy_speed == 2500)
3154 gop_gmac_sgmii2_5_cfg(port);
3155 else
3156 gop_gmac_sgmii_cfg(port);
3157 break;
3158
3159 case PHY_INTERFACE_MODE_RGMII:
3160 case PHY_INTERFACE_MODE_RGMII_ID:
3161 gop_gmac_rgmii_cfg(port);
3162 break;
3163
3164 default:
3165 return -1;
3166 }
3167
3168 /* Jumbo frame support - 0x1400*2= 0x2800 bytes */
3169 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
3170 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
3171 val |= 0x1400 << MVPP2_GMAC_MAX_RX_SIZE_OFFS;
3172 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
3173
3174 /* PeriodicXonEn disable */
3175 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
3176 val &= ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
3177 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
3178
3179 return 0;
3180}
3181
3182static void gop_xlg_2_gig_mac_cfg(struct mvpp2_port *port)
3183{
3184 u32 val;
3185
3186 /* relevant only for MAC0 (XLG0 and GMAC0) */
3187 if (port->gop_id > 0)
3188 return;
3189
3190 /* configure 1Gig MAC mode */
3191 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
3192 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
3193 val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
3194 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
3195}
3196
3197static int gop_gpcs_reset(struct mvpp2_port *port, int reset)
3198{
3199 u32 val;
3200
3201 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
3202 if (reset)
3203 val &= ~MVPP2_GMAC_SGMII_MODE_MASK;
3204 else
3205 val |= MVPP2_GMAC_SGMII_MODE_MASK;
3206 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
3207
3208 return 0;
3209}
3210
Stefan Roese01647642017-03-22 15:09:38 +01003211/* Set the internal mux's to the required PCS in the PI */
3212static int gop_xpcs_mode(struct mvpp2_port *port, int num_of_lanes)
3213{
3214 u32 val;
3215 int lane;
3216
3217 switch (num_of_lanes) {
3218 case 1:
3219 lane = 0;
3220 break;
3221 case 2:
3222 lane = 1;
3223 break;
3224 case 4:
3225 lane = 2;
3226 break;
3227 default:
3228 return -1;
3229 }
3230
3231 /* configure XG MAC mode */
3232 val = readl(port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
Stefan Chulskia27adcb2017-04-06 15:39:08 +02003233 val &= ~MVPP22_XPCS_PCSMODE_MASK;
Stefan Roese01647642017-03-22 15:09:38 +01003234 val &= ~MVPP22_XPCS_LANEACTIVE_MASK;
3235 val |= (2 * lane) << MVPP22_XPCS_LANEACTIVE_OFFS;
3236 writel(val, port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
3237
3238 return 0;
3239}
3240
3241static int gop_mpcs_mode(struct mvpp2_port *port)
3242{
3243 u32 val;
3244
3245 /* configure PCS40G COMMON CONTROL */
3246 val = readl(port->priv->mpcs_base + PCS40G_COMMON_CONTROL);
3247 val &= ~FORWARD_ERROR_CORRECTION_MASK;
3248 writel(val, port->priv->mpcs_base + PCS40G_COMMON_CONTROL);
3249
3250 /* configure PCS CLOCK RESET */
3251 val = readl(port->priv->mpcs_base + PCS_CLOCK_RESET);
3252 val &= ~CLK_DIVISION_RATIO_MASK;
3253 val |= 1 << CLK_DIVISION_RATIO_OFFS;
3254 writel(val, port->priv->mpcs_base + PCS_CLOCK_RESET);
3255
3256 val &= ~CLK_DIV_PHASE_SET_MASK;
3257 val |= MAC_CLK_RESET_MASK;
3258 val |= RX_SD_CLK_RESET_MASK;
3259 val |= TX_SD_CLK_RESET_MASK;
3260 writel(val, port->priv->mpcs_base + PCS_CLOCK_RESET);
3261
3262 return 0;
3263}
3264
3265/* Set the internal mux's to the required MAC in the GOP */
3266static int gop_xlg_mac_mode_cfg(struct mvpp2_port *port, int num_of_act_lanes)
3267{
3268 u32 val;
3269
3270 /* configure 10G MAC mode */
3271 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
3272 val |= MVPP22_XLG_RX_FC_EN;
3273 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
3274
3275 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
3276 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
3277 val |= MVPP22_XLG_CTRL3_MACMODESELECT_10GMAC;
3278 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
3279
3280 /* read - modify - write */
3281 val = readl(port->base + MVPP22_XLG_CTRL4_REG);
3282 val &= ~MVPP22_XLG_MODE_DMA_1G;
3283 val |= MVPP22_XLG_FORWARD_PFC_EN;
3284 val |= MVPP22_XLG_FORWARD_802_3X_FC_EN;
3285 val &= ~MVPP22_XLG_EN_IDLE_CHECK_FOR_LINK;
3286 writel(val, port->base + MVPP22_XLG_CTRL4_REG);
3287
3288 /* Jumbo frame support: 0x1400 * 2 = 0x2800 bytes */
3289 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
3290 val &= ~MVPP22_XLG_MAX_RX_SIZE_MASK;
3291 val |= 0x1400 << MVPP22_XLG_MAX_RX_SIZE_OFFS;
3292 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
3293
3294 /* unmask link change interrupt */
3295 val = readl(port->base + MVPP22_XLG_INTERRUPT_MASK_REG);
3296 val |= MVPP22_XLG_INTERRUPT_LINK_CHANGE;
3297 val |= 1; /* unmask summary bit */
3298 writel(val, port->base + MVPP22_XLG_INTERRUPT_MASK_REG);
3299
3300 return 0;
3301}
3302
3303/* Set PCS to reset or exit from reset */
3304static int gop_xpcs_reset(struct mvpp2_port *port, int reset)
3305{
3306 u32 val;
3307
3308 /* read - modify - write */
3309 val = readl(port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
3310 if (reset)
3311 val &= ~MVPP22_XPCS_PCSRESET;
3312 else
3313 val |= MVPP22_XPCS_PCSRESET;
3314 writel(val, port->priv->xpcs_base + MVPP22_XPCS_GLOBAL_CFG_0_REG);
3315
3316 return 0;
3317}
3318
3319/* Set the MAC to reset or exit from reset */
3320static int gop_xlg_mac_reset(struct mvpp2_port *port, int reset)
3321{
3322 u32 val;
3323
3324 /* read - modify - write */
3325 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
3326 if (reset)
3327 val &= ~MVPP22_XLG_MAC_RESETN;
3328 else
3329 val |= MVPP22_XLG_MAC_RESETN;
3330 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
3331
3332 return 0;
3333}
3334
Stefan Roese40e749b2017-03-22 15:07:30 +01003335/*
3336 * gop_port_init
3337 *
3338 * Init physical port. Configures the port mode and all it's elements
3339 * accordingly.
3340 * Does not verify that the selected mode/port number is valid at the
3341 * core level.
3342 */
3343static int gop_port_init(struct mvpp2_port *port)
3344{
3345 int mac_num = port->gop_id;
Stefan Roese01647642017-03-22 15:09:38 +01003346 int num_of_act_lanes;
Stefan Roese40e749b2017-03-22 15:07:30 +01003347
3348 if (mac_num >= MVPP22_GOP_MAC_NUM) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04003349 log_err("illegal port number %d", mac_num);
Stefan Roese40e749b2017-03-22 15:07:30 +01003350 return -1;
3351 }
3352
3353 switch (port->phy_interface) {
3354 case PHY_INTERFACE_MODE_RGMII:
3355 case PHY_INTERFACE_MODE_RGMII_ID:
3356 gop_gmac_reset(port, 1);
3357
3358 /* configure PCS */
3359 gop_gpcs_mode_cfg(port, 0);
3360 gop_bypass_clk_cfg(port, 1);
3361
3362 /* configure MAC */
3363 gop_gmac_mode_cfg(port);
3364 /* pcs unreset */
3365 gop_gpcs_reset(port, 0);
3366
3367 /* mac unreset */
3368 gop_gmac_reset(port, 0);
3369 break;
3370
3371 case PHY_INTERFACE_MODE_SGMII:
3372 /* configure PCS */
3373 gop_gpcs_mode_cfg(port, 1);
3374
3375 /* configure MAC */
3376 gop_gmac_mode_cfg(port);
3377 /* select proper Mac mode */
3378 gop_xlg_2_gig_mac_cfg(port);
3379
3380 /* pcs unreset */
3381 gop_gpcs_reset(port, 0);
3382 /* mac unreset */
3383 gop_gmac_reset(port, 0);
3384 break;
3385
Stefan Roese01647642017-03-22 15:09:38 +01003386 case PHY_INTERFACE_MODE_SFI:
3387 num_of_act_lanes = 2;
3388 mac_num = 0;
3389 /* configure PCS */
3390 gop_xpcs_mode(port, num_of_act_lanes);
3391 gop_mpcs_mode(port);
3392 /* configure MAC */
3393 gop_xlg_mac_mode_cfg(port, num_of_act_lanes);
3394
3395 /* pcs unreset */
3396 gop_xpcs_reset(port, 0);
3397
3398 /* mac unreset */
3399 gop_xlg_mac_reset(port, 0);
3400 break;
3401
Stefan Roese40e749b2017-03-22 15:07:30 +01003402 default:
Sean Andersonc7cbf092020-09-15 10:44:57 -04003403 log_err("Requested port mode (%d) not supported\n",
3404 port->phy_interface);
Stefan Roese40e749b2017-03-22 15:07:30 +01003405 return -1;
3406 }
3407
3408 return 0;
3409}
3410
Stefan Roese01647642017-03-22 15:09:38 +01003411static void gop_xlg_mac_port_enable(struct mvpp2_port *port, int enable)
3412{
3413 u32 val;
3414
3415 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
3416 if (enable) {
3417 /* Enable port and MIB counters update */
3418 val |= MVPP22_XLG_PORT_EN;
3419 val &= ~MVPP22_XLG_MIBCNT_DIS;
3420 } else {
3421 /* Disable port */
3422 val &= ~MVPP22_XLG_PORT_EN;
3423 }
3424 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
3425}
3426
Stefan Roese40e749b2017-03-22 15:07:30 +01003427static void gop_port_enable(struct mvpp2_port *port, int enable)
3428{
3429 switch (port->phy_interface) {
3430 case PHY_INTERFACE_MODE_RGMII:
3431 case PHY_INTERFACE_MODE_RGMII_ID:
3432 case PHY_INTERFACE_MODE_SGMII:
3433 if (enable)
3434 mvpp2_port_enable(port);
3435 else
3436 mvpp2_port_disable(port);
3437 break;
3438
Stefan Roese01647642017-03-22 15:09:38 +01003439 case PHY_INTERFACE_MODE_SFI:
3440 gop_xlg_mac_port_enable(port, enable);
3441
3442 break;
Stefan Roese40e749b2017-03-22 15:07:30 +01003443 default:
Sean Andersonc7cbf092020-09-15 10:44:57 -04003444 log_err("%s: Wrong port mode (%d)\n", __func__,
3445 port->phy_interface);
Stefan Roese40e749b2017-03-22 15:07:30 +01003446 return;
3447 }
3448}
3449
3450/* RFU1 functions */
3451static inline u32 gop_rfu1_read(struct mvpp2 *priv, u32 offset)
3452{
3453 return readl(priv->rfu1_base + offset);
3454}
3455
3456static inline void gop_rfu1_write(struct mvpp2 *priv, u32 offset, u32 data)
3457{
3458 writel(data, priv->rfu1_base + offset);
3459}
3460
3461static u32 mvpp2_netc_cfg_create(int gop_id, phy_interface_t phy_type)
3462{
3463 u32 val = 0;
3464
3465 if (gop_id == 2) {
3466 if (phy_type == PHY_INTERFACE_MODE_SGMII)
3467 val |= MV_NETC_GE_MAC2_SGMII;
3468 }
3469
3470 if (gop_id == 3) {
3471 if (phy_type == PHY_INTERFACE_MODE_SGMII)
3472 val |= MV_NETC_GE_MAC3_SGMII;
3473 else if (phy_type == PHY_INTERFACE_MODE_RGMII ||
3474 phy_type == PHY_INTERFACE_MODE_RGMII_ID)
3475 val |= MV_NETC_GE_MAC3_RGMII;
3476 }
3477
3478 return val;
3479}
3480
3481static void gop_netc_active_port(struct mvpp2 *priv, int gop_id, u32 val)
3482{
3483 u32 reg;
3484
3485 reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_1_REG);
3486 reg &= ~(NETC_PORTS_ACTIVE_MASK(gop_id));
3487
3488 val <<= NETC_PORTS_ACTIVE_OFFSET(gop_id);
3489 val &= NETC_PORTS_ACTIVE_MASK(gop_id);
3490
3491 reg |= val;
3492
3493 gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_1_REG, reg);
3494}
3495
3496static void gop_netc_mii_mode(struct mvpp2 *priv, int gop_id, u32 val)
3497{
3498 u32 reg;
3499
3500 reg = gop_rfu1_read(priv, NETCOMP_CONTROL_0_REG);
3501 reg &= ~NETC_GBE_PORT1_MII_MODE_MASK;
3502
3503 val <<= NETC_GBE_PORT1_MII_MODE_OFFS;
3504 val &= NETC_GBE_PORT1_MII_MODE_MASK;
3505
3506 reg |= val;
3507
3508 gop_rfu1_write(priv, NETCOMP_CONTROL_0_REG, reg);
3509}
3510
3511static void gop_netc_gop_reset(struct mvpp2 *priv, u32 val)
3512{
3513 u32 reg;
3514
3515 reg = gop_rfu1_read(priv, GOP_SOFT_RESET_1_REG);
3516 reg &= ~NETC_GOP_SOFT_RESET_MASK;
3517
3518 val <<= NETC_GOP_SOFT_RESET_OFFS;
3519 val &= NETC_GOP_SOFT_RESET_MASK;
3520
3521 reg |= val;
3522
3523 gop_rfu1_write(priv, GOP_SOFT_RESET_1_REG, reg);
3524}
3525
3526static void gop_netc_gop_clock_logic_set(struct mvpp2 *priv, u32 val)
3527{
3528 u32 reg;
3529
3530 reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_0_REG);
3531 reg &= ~NETC_CLK_DIV_PHASE_MASK;
3532
3533 val <<= NETC_CLK_DIV_PHASE_OFFS;
3534 val &= NETC_CLK_DIV_PHASE_MASK;
3535
3536 reg |= val;
3537
3538 gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_0_REG, reg);
3539}
3540
3541static void gop_netc_port_rf_reset(struct mvpp2 *priv, int gop_id, u32 val)
3542{
3543 u32 reg;
3544
3545 reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_1_REG);
3546 reg &= ~(NETC_PORT_GIG_RF_RESET_MASK(gop_id));
3547
3548 val <<= NETC_PORT_GIG_RF_RESET_OFFS(gop_id);
3549 val &= NETC_PORT_GIG_RF_RESET_MASK(gop_id);
3550
3551 reg |= val;
3552
3553 gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_1_REG, reg);
3554}
3555
3556static void gop_netc_gbe_sgmii_mode_select(struct mvpp2 *priv, int gop_id,
3557 u32 val)
3558{
3559 u32 reg, mask, offset;
3560
3561 if (gop_id == 2) {
3562 mask = NETC_GBE_PORT0_SGMII_MODE_MASK;
3563 offset = NETC_GBE_PORT0_SGMII_MODE_OFFS;
3564 } else {
3565 mask = NETC_GBE_PORT1_SGMII_MODE_MASK;
3566 offset = NETC_GBE_PORT1_SGMII_MODE_OFFS;
3567 }
3568 reg = gop_rfu1_read(priv, NETCOMP_CONTROL_0_REG);
3569 reg &= ~mask;
3570
3571 val <<= offset;
3572 val &= mask;
3573
3574 reg |= val;
3575
3576 gop_rfu1_write(priv, NETCOMP_CONTROL_0_REG, reg);
3577}
3578
3579static void gop_netc_bus_width_select(struct mvpp2 *priv, u32 val)
3580{
3581 u32 reg;
3582
3583 reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_0_REG);
3584 reg &= ~NETC_BUS_WIDTH_SELECT_MASK;
3585
3586 val <<= NETC_BUS_WIDTH_SELECT_OFFS;
3587 val &= NETC_BUS_WIDTH_SELECT_MASK;
3588
3589 reg |= val;
3590
3591 gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_0_REG, reg);
3592}
3593
3594static void gop_netc_sample_stages_timing(struct mvpp2 *priv, u32 val)
3595{
3596 u32 reg;
3597
3598 reg = gop_rfu1_read(priv, NETCOMP_PORTS_CONTROL_0_REG);
3599 reg &= ~NETC_GIG_RX_DATA_SAMPLE_MASK;
3600
3601 val <<= NETC_GIG_RX_DATA_SAMPLE_OFFS;
3602 val &= NETC_GIG_RX_DATA_SAMPLE_MASK;
3603
3604 reg |= val;
3605
3606 gop_rfu1_write(priv, NETCOMP_PORTS_CONTROL_0_REG, reg);
3607}
3608
3609static void gop_netc_mac_to_xgmii(struct mvpp2 *priv, int gop_id,
3610 enum mv_netc_phase phase)
3611{
3612 switch (phase) {
3613 case MV_NETC_FIRST_PHASE:
3614 /* Set Bus Width to HB mode = 1 */
3615 gop_netc_bus_width_select(priv, 1);
3616 /* Select RGMII mode */
3617 gop_netc_gbe_sgmii_mode_select(priv, gop_id, MV_NETC_GBE_XMII);
3618 break;
3619
3620 case MV_NETC_SECOND_PHASE:
3621 /* De-assert the relevant port HB reset */
3622 gop_netc_port_rf_reset(priv, gop_id, 1);
3623 break;
3624 }
3625}
3626
3627static void gop_netc_mac_to_sgmii(struct mvpp2 *priv, int gop_id,
3628 enum mv_netc_phase phase)
3629{
3630 switch (phase) {
3631 case MV_NETC_FIRST_PHASE:
3632 /* Set Bus Width to HB mode = 1 */
3633 gop_netc_bus_width_select(priv, 1);
3634 /* Select SGMII mode */
3635 if (gop_id >= 1) {
3636 gop_netc_gbe_sgmii_mode_select(priv, gop_id,
3637 MV_NETC_GBE_SGMII);
3638 }
3639
3640 /* Configure the sample stages */
3641 gop_netc_sample_stages_timing(priv, 0);
3642 /* Configure the ComPhy Selector */
3643 /* gop_netc_com_phy_selector_config(netComplex); */
3644 break;
3645
3646 case MV_NETC_SECOND_PHASE:
3647 /* De-assert the relevant port HB reset */
3648 gop_netc_port_rf_reset(priv, gop_id, 1);
3649 break;
3650 }
3651}
3652
3653static int gop_netc_init(struct mvpp2 *priv, enum mv_netc_phase phase)
3654{
3655 u32 c = priv->netc_config;
3656
3657 if (c & MV_NETC_GE_MAC2_SGMII)
3658 gop_netc_mac_to_sgmii(priv, 2, phase);
3659 else
3660 gop_netc_mac_to_xgmii(priv, 2, phase);
3661
3662 if (c & MV_NETC_GE_MAC3_SGMII) {
3663 gop_netc_mac_to_sgmii(priv, 3, phase);
3664 } else {
3665 gop_netc_mac_to_xgmii(priv, 3, phase);
3666 if (c & MV_NETC_GE_MAC3_RGMII)
3667 gop_netc_mii_mode(priv, 3, MV_NETC_GBE_RGMII);
3668 else
3669 gop_netc_mii_mode(priv, 3, MV_NETC_GBE_MII);
3670 }
3671
3672 /* Activate gop ports 0, 2, 3 */
3673 gop_netc_active_port(priv, 0, 1);
3674 gop_netc_active_port(priv, 2, 1);
3675 gop_netc_active_port(priv, 3, 1);
3676
3677 if (phase == MV_NETC_SECOND_PHASE) {
3678 /* Enable the GOP internal clock logic */
3679 gop_netc_gop_clock_logic_set(priv, 1);
3680 /* De-assert GOP unit reset */
3681 gop_netc_gop_reset(priv, 1);
3682 }
3683
3684 return 0;
3685}
3686
Stefan Roese96c19042016-02-10 07:22:10 +01003687/* Set defaults to the MVPP2 port */
3688static void mvpp2_defaults_set(struct mvpp2_port *port)
3689{
3690 int tx_port_num, val, queue, ptxq, lrxq;
3691
Thomas Petazzoni58159ee2017-02-16 06:57:24 +01003692 if (port->priv->hw_version == MVPP21) {
3693 /* Configure port to loopback if needed */
3694 if (port->flags & MVPP2_F_LOOPBACK)
3695 mvpp2_port_loopback_set(port);
Stefan Roese96c19042016-02-10 07:22:10 +01003696
Thomas Petazzoni58159ee2017-02-16 06:57:24 +01003697 /* Update TX FIFO MIN Threshold */
3698 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3699 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
3700 /* Min. TX threshold must be less than minimal packet length */
3701 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
3702 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
3703 }
Stefan Roese96c19042016-02-10 07:22:10 +01003704
3705 /* Disable Legacy WRR, Disable EJP, Release from reset */
3706 tx_port_num = mvpp2_egress_port(port);
3707 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
3708 tx_port_num);
3709 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
3710
3711 /* Close bandwidth for all queues */
3712 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
3713 ptxq = mvpp2_txq_phys(port->id, queue);
3714 mvpp2_write(port->priv,
3715 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
3716 }
3717
3718 /* Set refill period to 1 usec, refill tokens
3719 * and bucket size to maximum
3720 */
3721 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
3722 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
3723 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
3724 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
3725 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
3726 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
3727 val = MVPP2_TXP_TOKEN_SIZE_MAX;
3728 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
3729
3730 /* Set MaximumLowLatencyPacketSize value to 256 */
3731 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
3732 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
3733 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
3734
3735 /* Enable Rx cache snoop */
3736 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
3737 queue = port->rxqs[lrxq]->id;
3738 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
3739 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
3740 MVPP2_SNOOP_BUF_HDR_MASK;
3741 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
3742 }
3743}
3744
3745/* Enable/disable receiving packets */
3746static void mvpp2_ingress_enable(struct mvpp2_port *port)
3747{
3748 u32 val;
3749 int lrxq, queue;
3750
3751 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
3752 queue = port->rxqs[lrxq]->id;
3753 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
3754 val &= ~MVPP2_RXQ_DISABLE_MASK;
3755 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
3756 }
3757}
3758
3759static void mvpp2_ingress_disable(struct mvpp2_port *port)
3760{
3761 u32 val;
3762 int lrxq, queue;
3763
3764 for (lrxq = 0; lrxq < rxq_number; lrxq++) {
3765 queue = port->rxqs[lrxq]->id;
3766 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
3767 val |= MVPP2_RXQ_DISABLE_MASK;
3768 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
3769 }
3770}
3771
3772/* Enable transmit via physical egress queue
3773 * - HW starts take descriptors from DRAM
3774 */
3775static void mvpp2_egress_enable(struct mvpp2_port *port)
3776{
3777 u32 qmap;
3778 int queue;
3779 int tx_port_num = mvpp2_egress_port(port);
3780
3781 /* Enable all initialized TXs. */
3782 qmap = 0;
3783 for (queue = 0; queue < txq_number; queue++) {
3784 struct mvpp2_tx_queue *txq = port->txqs[queue];
3785
3786 if (txq->descs != NULL)
3787 qmap |= (1 << queue);
3788 }
3789
3790 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3791 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
3792}
3793
3794/* Disable transmit via physical egress queue
3795 * - HW doesn't take descriptors from DRAM
3796 */
3797static void mvpp2_egress_disable(struct mvpp2_port *port)
3798{
3799 u32 reg_data;
3800 int delay;
3801 int tx_port_num = mvpp2_egress_port(port);
3802
3803 /* Issue stop command for active channels only */
3804 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3805 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
3806 MVPP2_TXP_SCHED_ENQ_MASK;
3807 if (reg_data != 0)
3808 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
3809 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
3810
3811 /* Wait for all Tx activity to terminate. */
3812 delay = 0;
3813 do {
3814 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04003815 dev_warn(port->phy_dev->dev,
3816 "Tx stop timed out, status=0x%08x\n",
3817 reg_data);
Stefan Roese96c19042016-02-10 07:22:10 +01003818 break;
3819 }
3820 mdelay(1);
3821 delay++;
3822
3823 /* Check port TX Command register that all
3824 * Tx queues are stopped
3825 */
3826 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
3827 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
3828}
3829
3830/* Rx descriptors helper methods */
3831
3832/* Get number of Rx descriptors occupied by received packets */
3833static inline int
3834mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
3835{
3836 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
3837
3838 return val & MVPP2_RXQ_OCCUPIED_MASK;
3839}
3840
3841/* Update Rx queue status with the number of occupied and available
3842 * Rx descriptor slots.
3843 */
3844static inline void
3845mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
3846 int used_count, int free_count)
3847{
3848 /* Decrement the number of used descriptors and increment count
3849 * increment the number of free descriptors.
3850 */
3851 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
3852
3853 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
3854}
3855
3856/* Get pointer to next RX descriptor to be processed by SW */
3857static inline struct mvpp2_rx_desc *
3858mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
3859{
3860 int rx_desc = rxq->next_desc_to_proc;
3861
3862 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
3863 prefetch(rxq->descs + rxq->next_desc_to_proc);
3864 return rxq->descs + rx_desc;
3865}
3866
3867/* Set rx queue offset */
3868static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
3869 int prxq, int offset)
3870{
3871 u32 val;
3872
3873 /* Convert offset from bytes to units of 32 bytes */
3874 offset = offset >> 5;
3875
3876 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
3877 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
3878
3879 /* Offset is in */
3880 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
3881 MVPP2_RXQ_PACKET_OFFSET_MASK);
3882
3883 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
3884}
3885
3886/* Obtain BM cookie information from descriptor */
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01003887static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
3888 struct mvpp2_rx_desc *rx_desc)
Stefan Roese96c19042016-02-10 07:22:10 +01003889{
Stefan Roese96c19042016-02-10 07:22:10 +01003890 int cpu = smp_processor_id();
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01003891 int pool;
3892
3893 pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
3894 MVPP2_RXD_BM_POOL_ID_MASK) >>
3895 MVPP2_RXD_BM_POOL_ID_OFFS;
Stefan Roese96c19042016-02-10 07:22:10 +01003896
3897 return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
3898 ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
3899}
3900
3901/* Tx descriptors helper methods */
3902
3903/* Get number of Tx descriptors waiting to be transmitted by HW */
3904static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
3905 struct mvpp2_tx_queue *txq)
3906{
3907 u32 val;
3908
3909 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3910 val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
3911
3912 return val & MVPP2_TXQ_PENDING_MASK;
3913}
3914
3915/* Get pointer to next Tx descriptor to be processed (send) by HW */
3916static struct mvpp2_tx_desc *
3917mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
3918{
3919 int tx_desc = txq->next_desc_to_proc;
3920
3921 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
3922 return txq->descs + tx_desc;
3923}
3924
3925/* Update HW with number of aggregated Tx descriptors to be sent */
3926static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
3927{
3928 /* aggregated access - relevant TXQ number is written in TX desc */
3929 mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
3930}
3931
3932/* Get number of sent descriptors and decrement counter.
3933 * The number of sent descriptors is returned.
3934 * Per-CPU access
3935 */
3936static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
3937 struct mvpp2_tx_queue *txq)
3938{
3939 u32 val;
3940
3941 /* Reading status reg resets transmitted descriptor counter */
3942 val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
3943
3944 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
3945 MVPP2_TRANSMITTED_COUNT_OFFSET;
3946}
3947
3948static void mvpp2_txq_sent_counter_clear(void *arg)
3949{
3950 struct mvpp2_port *port = arg;
3951 int queue;
3952
3953 for (queue = 0; queue < txq_number; queue++) {
3954 int id = port->txqs[queue]->id;
3955
3956 mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
3957 }
3958}
3959
3960/* Set max sizes for Tx queues */
3961static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
3962{
3963 u32 val, size, mtu;
3964 int txq, tx_port_num;
3965
3966 mtu = port->pkt_size * 8;
3967 if (mtu > MVPP2_TXP_MTU_MAX)
3968 mtu = MVPP2_TXP_MTU_MAX;
3969
3970 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
3971 mtu = 3 * mtu;
3972
3973 /* Indirect access to registers */
3974 tx_port_num = mvpp2_egress_port(port);
3975 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3976
3977 /* Set MTU */
3978 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
3979 val &= ~MVPP2_TXP_MTU_MAX;
3980 val |= mtu;
3981 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
3982
3983 /* TXP token size and all TXQs token size must be larger that MTU */
3984 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
3985 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
3986 if (size < mtu) {
3987 size = mtu;
3988 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
3989 val |= size;
3990 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
3991 }
3992
3993 for (txq = 0; txq < txq_number; txq++) {
3994 val = mvpp2_read(port->priv,
3995 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
3996 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
3997
3998 if (size < mtu) {
3999 size = mtu;
4000 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
4001 val |= size;
4002 mvpp2_write(port->priv,
4003 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
4004 val);
4005 }
4006 }
4007}
4008
4009/* Free Tx queue skbuffs */
4010static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
4011 struct mvpp2_tx_queue *txq,
4012 struct mvpp2_txq_pcpu *txq_pcpu, int num)
4013{
4014 int i;
4015
4016 for (i = 0; i < num; i++)
4017 mvpp2_txq_inc_get(txq_pcpu);
4018}
4019
4020static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
4021 u32 cause)
4022{
4023 int queue = fls(cause) - 1;
4024
4025 return port->rxqs[queue];
4026}
4027
4028static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
4029 u32 cause)
4030{
4031 int queue = fls(cause) - 1;
4032
4033 return port->txqs[queue];
4034}
4035
4036/* Rx/Tx queue initialization/cleanup methods */
4037
4038/* Allocate and initialize descriptors for aggr TXQ */
4039static int mvpp2_aggr_txq_init(struct udevice *dev,
4040 struct mvpp2_tx_queue *aggr_txq,
4041 int desc_num, int cpu,
4042 struct mvpp2 *priv)
4043{
Thomas Petazzoni7f215c72017-02-20 11:36:57 +01004044 u32 txq_dma;
4045
Stefan Roese96c19042016-02-10 07:22:10 +01004046 /* Allocate memory for TX descriptors */
4047 aggr_txq->descs = buffer_loc.aggr_tx_descs;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004048 aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
Stefan Roese96c19042016-02-10 07:22:10 +01004049 if (!aggr_txq->descs)
4050 return -ENOMEM;
4051
4052 /* Make sure descriptor address is cache line size aligned */
4053 BUG_ON(aggr_txq->descs !=
4054 PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
4055
4056 aggr_txq->last_desc = aggr_txq->size - 1;
4057
4058 /* Aggr TXQ no reset WA */
4059 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
4060 MVPP2_AGGR_TXQ_INDEX_REG(cpu));
4061
Thomas Petazzoni7f215c72017-02-20 11:36:57 +01004062 /* Set Tx descriptors queue starting address indirect
4063 * access
4064 */
4065 if (priv->hw_version == MVPP21)
4066 txq_dma = aggr_txq->descs_dma;
4067 else
4068 txq_dma = aggr_txq->descs_dma >>
4069 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
4070
4071 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
Stefan Roese96c19042016-02-10 07:22:10 +01004072 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
4073
4074 return 0;
4075}
4076
4077/* Create a specified Rx queue */
4078static int mvpp2_rxq_init(struct mvpp2_port *port,
4079 struct mvpp2_rx_queue *rxq)
4080
4081{
Thomas Petazzoni7f215c72017-02-20 11:36:57 +01004082 u32 rxq_dma;
4083
Stefan Roese96c19042016-02-10 07:22:10 +01004084 rxq->size = port->rx_ring_size;
4085
4086 /* Allocate memory for RX descriptors */
4087 rxq->descs = buffer_loc.rx_descs;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004088 rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
Stefan Roese96c19042016-02-10 07:22:10 +01004089 if (!rxq->descs)
4090 return -ENOMEM;
4091
4092 BUG_ON(rxq->descs !=
4093 PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
4094
4095 rxq->last_desc = rxq->size - 1;
4096
4097 /* Zero occupied and non-occupied counters - direct access */
4098 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4099
4100 /* Set Rx descriptors queue starting address - indirect access */
4101 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
Thomas Petazzoni7f215c72017-02-20 11:36:57 +01004102 if (port->priv->hw_version == MVPP21)
4103 rxq_dma = rxq->descs_dma;
4104 else
4105 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
4106 mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
Stefan Roese96c19042016-02-10 07:22:10 +01004107 mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
4108 mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
4109
4110 /* Set Offset */
4111 mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
4112
4113 /* Add number of descriptors ready for receiving packets */
4114 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
4115
4116 return 0;
4117}
4118
4119/* Push packets received by the RXQ to BM pool */
4120static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
4121 struct mvpp2_rx_queue *rxq)
4122{
4123 int rx_received, i;
4124
4125 rx_received = mvpp2_rxq_received(port, rxq->id);
4126 if (!rx_received)
4127 return;
4128
4129 for (i = 0; i < rx_received; i++) {
4130 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01004131 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
Stefan Roese96c19042016-02-10 07:22:10 +01004132
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01004133 mvpp2_pool_refill(port, bm,
4134 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
4135 mvpp2_rxdesc_cookie_get(port, rx_desc));
Stefan Roese96c19042016-02-10 07:22:10 +01004136 }
4137 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
4138}
4139
4140/* Cleanup Rx queue */
4141static void mvpp2_rxq_deinit(struct mvpp2_port *port,
4142 struct mvpp2_rx_queue *rxq)
4143{
4144 mvpp2_rxq_drop_pkts(port, rxq);
4145
4146 rxq->descs = NULL;
4147 rxq->last_desc = 0;
4148 rxq->next_desc_to_proc = 0;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004149 rxq->descs_dma = 0;
Stefan Roese96c19042016-02-10 07:22:10 +01004150
4151 /* Clear Rx descriptors queue starting address and size;
4152 * free descriptor number
4153 */
4154 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
4155 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
4156 mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
4157 mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
4158}
4159
4160/* Create and initialize a Tx queue */
4161static int mvpp2_txq_init(struct mvpp2_port *port,
4162 struct mvpp2_tx_queue *txq)
4163{
4164 u32 val;
4165 int cpu, desc, desc_per_txq, tx_port_num;
4166 struct mvpp2_txq_pcpu *txq_pcpu;
4167
4168 txq->size = port->tx_ring_size;
4169
4170 /* Allocate memory for Tx descriptors */
4171 txq->descs = buffer_loc.tx_descs;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004172 txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
Stefan Roese96c19042016-02-10 07:22:10 +01004173 if (!txq->descs)
4174 return -ENOMEM;
4175
4176 /* Make sure descriptor address is cache line size aligned */
4177 BUG_ON(txq->descs !=
4178 PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
4179
4180 txq->last_desc = txq->size - 1;
4181
4182 /* Set Tx descriptors queue starting address - indirect access */
4183 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004184 mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
Stefan Roese96c19042016-02-10 07:22:10 +01004185 mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
4186 MVPP2_TXQ_DESC_SIZE_MASK);
4187 mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
4188 mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
4189 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
4190 val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
4191 val &= ~MVPP2_TXQ_PENDING_MASK;
4192 mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
4193
4194 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
4195 * for each existing TXQ.
4196 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
4197 * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
4198 */
4199 desc_per_txq = 16;
4200 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
4201 (txq->log_id * desc_per_txq);
4202
4203 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
4204 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
Thomas Petazzoni5555f072017-02-16 08:03:37 +01004205 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
Stefan Roese96c19042016-02-10 07:22:10 +01004206
4207 /* WRR / EJP configuration - indirect access */
4208 tx_port_num = mvpp2_egress_port(port);
4209 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
4210
4211 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
4212 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
4213 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
4214 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
4215 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
4216
4217 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
4218 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
4219 val);
4220
4221 for_each_present_cpu(cpu) {
4222 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4223 txq_pcpu->size = txq->size;
4224 }
4225
4226 return 0;
4227}
4228
4229/* Free allocated TXQ resources */
4230static void mvpp2_txq_deinit(struct mvpp2_port *port,
4231 struct mvpp2_tx_queue *txq)
4232{
4233 txq->descs = NULL;
4234 txq->last_desc = 0;
4235 txq->next_desc_to_proc = 0;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004236 txq->descs_dma = 0;
Stefan Roese96c19042016-02-10 07:22:10 +01004237
4238 /* Set minimum bandwidth for disabled TXQs */
4239 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
4240
4241 /* Set Tx descriptors queue starting address and size */
4242 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4243 mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
4244 mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
4245}
4246
4247/* Cleanup Tx ports */
4248static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
4249{
4250 struct mvpp2_txq_pcpu *txq_pcpu;
4251 int delay, pending, cpu;
4252 u32 val;
4253
4254 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4255 val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
4256 val |= MVPP2_TXQ_DRAIN_EN_MASK;
4257 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4258
4259 /* The napi queue has been stopped so wait for all packets
4260 * to be transmitted.
4261 */
4262 delay = 0;
4263 do {
4264 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004265 dev_warn(port->phy_dev->dev,
4266 "port %d: cleaning queue %d timed out\n",
4267 port->id, txq->log_id);
Stefan Roese96c19042016-02-10 07:22:10 +01004268 break;
4269 }
4270 mdelay(1);
4271 delay++;
4272
4273 pending = mvpp2_txq_pend_desc_num_get(port, txq);
4274 } while (pending);
4275
4276 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
4277 mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4278
4279 for_each_present_cpu(cpu) {
4280 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4281
4282 /* Release all packets */
4283 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
4284
4285 /* Reset queue */
4286 txq_pcpu->count = 0;
4287 txq_pcpu->txq_put_index = 0;
4288 txq_pcpu->txq_get_index = 0;
4289 }
4290}
4291
4292/* Cleanup all Tx queues */
4293static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
4294{
4295 struct mvpp2_tx_queue *txq;
4296 int queue;
4297 u32 val;
4298
4299 val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
4300
4301 /* Reset Tx ports and delete Tx queues */
4302 val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
4303 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
4304
4305 for (queue = 0; queue < txq_number; queue++) {
4306 txq = port->txqs[queue];
4307 mvpp2_txq_clean(port, txq);
4308 mvpp2_txq_deinit(port, txq);
4309 }
4310
4311 mvpp2_txq_sent_counter_clear(port);
4312
4313 val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
4314 mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
4315}
4316
4317/* Cleanup all Rx queues */
4318static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
4319{
4320 int queue;
4321
4322 for (queue = 0; queue < rxq_number; queue++)
4323 mvpp2_rxq_deinit(port, port->rxqs[queue]);
4324}
4325
4326/* Init all Rx queues for port */
4327static int mvpp2_setup_rxqs(struct mvpp2_port *port)
4328{
4329 int queue, err;
4330
4331 for (queue = 0; queue < rxq_number; queue++) {
4332 err = mvpp2_rxq_init(port, port->rxqs[queue]);
4333 if (err)
4334 goto err_cleanup;
4335 }
4336 return 0;
4337
4338err_cleanup:
4339 mvpp2_cleanup_rxqs(port);
4340 return err;
4341}
4342
4343/* Init all tx queues for port */
4344static int mvpp2_setup_txqs(struct mvpp2_port *port)
4345{
4346 struct mvpp2_tx_queue *txq;
4347 int queue, err;
4348
4349 for (queue = 0; queue < txq_number; queue++) {
4350 txq = port->txqs[queue];
4351 err = mvpp2_txq_init(port, txq);
4352 if (err)
4353 goto err_cleanup;
4354 }
4355
4356 mvpp2_txq_sent_counter_clear(port);
4357 return 0;
4358
4359err_cleanup:
4360 mvpp2_cleanup_txqs(port);
4361 return err;
4362}
4363
4364/* Adjust link */
4365static void mvpp2_link_event(struct mvpp2_port *port)
4366{
4367 struct phy_device *phydev = port->phy_dev;
4368 int status_change = 0;
4369 u32 val;
4370
4371 if (phydev->link) {
4372 if ((port->speed != phydev->speed) ||
4373 (port->duplex != phydev->duplex)) {
4374 u32 val;
4375
4376 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4377 val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
4378 MVPP2_GMAC_CONFIG_GMII_SPEED |
4379 MVPP2_GMAC_CONFIG_FULL_DUPLEX |
4380 MVPP2_GMAC_AN_SPEED_EN |
4381 MVPP2_GMAC_AN_DUPLEX_EN);
4382
4383 if (phydev->duplex)
4384 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
4385
4386 if (phydev->speed == SPEED_1000)
4387 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
4388 else if (phydev->speed == SPEED_100)
4389 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
4390
4391 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4392
4393 port->duplex = phydev->duplex;
4394 port->speed = phydev->speed;
4395 }
4396 }
4397
4398 if (phydev->link != port->link) {
4399 if (!phydev->link) {
4400 port->duplex = -1;
4401 port->speed = 0;
4402 }
4403
4404 port->link = phydev->link;
4405 status_change = 1;
4406 }
4407
4408 if (status_change) {
4409 if (phydev->link) {
4410 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4411 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
4412 MVPP2_GMAC_FORCE_LINK_DOWN);
4413 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4414 mvpp2_egress_enable(port);
4415 mvpp2_ingress_enable(port);
4416 } else {
4417 mvpp2_ingress_disable(port);
4418 mvpp2_egress_disable(port);
4419 }
4420 }
4421}
4422
4423/* Main RX/TX processing routines */
4424
4425/* Display more error info */
4426static void mvpp2_rx_error(struct mvpp2_port *port,
4427 struct mvpp2_rx_desc *rx_desc)
4428{
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01004429 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
4430 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
Stefan Roese96c19042016-02-10 07:22:10 +01004431
4432 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
4433 case MVPP2_RXD_ERR_CRC:
Sean Andersonc7cbf092020-09-15 10:44:57 -04004434 dev_err(port->phy_dev->dev,
4435 "bad rx status %08x (crc error), size=%zu\n", status,
4436 sz);
Stefan Roese96c19042016-02-10 07:22:10 +01004437 break;
4438 case MVPP2_RXD_ERR_OVERRUN:
Sean Andersonc7cbf092020-09-15 10:44:57 -04004439 dev_err(port->phy_dev->dev,
4440 "bad rx status %08x (overrun error), size=%zu\n",
4441 status, sz);
Stefan Roese96c19042016-02-10 07:22:10 +01004442 break;
4443 case MVPP2_RXD_ERR_RESOURCE:
Sean Andersonc7cbf092020-09-15 10:44:57 -04004444 dev_err(port->phy_dev->dev,
4445 "bad rx status %08x (resource error), size=%zu\n",
4446 status, sz);
Stefan Roese96c19042016-02-10 07:22:10 +01004447 break;
4448 }
4449}
4450
4451/* Reuse skb if possible, or allocate a new skb and add it to BM pool */
4452static int mvpp2_rx_refill(struct mvpp2_port *port,
4453 struct mvpp2_bm_pool *bm_pool,
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004454 u32 bm, dma_addr_t dma_addr)
Stefan Roese96c19042016-02-10 07:22:10 +01004455{
Thomas Petazzonic49aff22017-02-20 10:27:51 +01004456 mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
Stefan Roese96c19042016-02-10 07:22:10 +01004457 return 0;
4458}
4459
4460/* Set hw internals when starting port */
4461static void mvpp2_start_dev(struct mvpp2_port *port)
4462{
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004463 switch (port->phy_interface) {
4464 case PHY_INTERFACE_MODE_RGMII:
4465 case PHY_INTERFACE_MODE_RGMII_ID:
4466 case PHY_INTERFACE_MODE_SGMII:
4467 mvpp2_gmac_max_rx_size_set(port);
4468 default:
4469 break;
4470 }
4471
Stefan Roese96c19042016-02-10 07:22:10 +01004472 mvpp2_txp_max_tx_size_set(port);
4473
Stefan Roese40e749b2017-03-22 15:07:30 +01004474 if (port->priv->hw_version == MVPP21)
4475 mvpp2_port_enable(port);
4476 else
4477 gop_port_enable(port, 1);
Stefan Roese96c19042016-02-10 07:22:10 +01004478}
4479
4480/* Set hw internals when stopping port */
4481static void mvpp2_stop_dev(struct mvpp2_port *port)
4482{
4483 /* Stop new packets from arriving to RXQs */
4484 mvpp2_ingress_disable(port);
4485
4486 mvpp2_egress_disable(port);
Stefan Roese40e749b2017-03-22 15:07:30 +01004487
4488 if (port->priv->hw_version == MVPP21)
4489 mvpp2_port_disable(port);
4490 else
4491 gop_port_enable(port, 0);
Stefan Roese96c19042016-02-10 07:22:10 +01004492}
4493
Stefan Chulskib261d1b2019-08-15 18:08:41 -04004494static void mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
Stefan Roese96c19042016-02-10 07:22:10 +01004495{
4496 struct phy_device *phy_dev;
4497
4498 if (!port->init || port->link == 0) {
Nevo Hed5e975612019-08-15 18:08:44 -04004499 phy_dev = dm_mdio_phy_connect(port->mdio_dev, port->phyaddr,
4500 dev, port->phy_interface);
Grzegorz Jaszczyka531afd2019-08-15 18:08:42 -04004501
4502 /*
4503 * If the phy doesn't match with any existing u-boot drivers the
4504 * phy framework will connect it to generic one which
4505 * uid == 0xffffffff. In this case act as if the phy wouldn't be
4506 * declared in dts. Otherwise in case of 3310 (for which the
4507 * driver doesn't exist) the link will not be correctly
4508 * detected. Removing phy entry from dts in case of 3310 is not
4509 * an option because it is required for the phy_fw_down
4510 * procedure.
4511 */
4512 if (phy_dev &&
4513 phy_dev->drv->uid == 0xffffffff) {/* Generic phy */
Sean Andersonc7cbf092020-09-15 10:44:57 -04004514 dev_warn(port->phy_dev->dev,
4515 "Marking phy as invalid, link will not be checked\n");
Grzegorz Jaszczyka531afd2019-08-15 18:08:42 -04004516 /* set phy_addr to invalid value */
4517 port->phyaddr = PHY_MAX_ADDR;
4518 mvpp2_egress_enable(port);
4519 mvpp2_ingress_enable(port);
4520
4521 return;
4522 }
4523
Stefan Roese96c19042016-02-10 07:22:10 +01004524 port->phy_dev = phy_dev;
4525 if (!phy_dev) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004526 dev_err(port->phy_dev->dev, "cannot connect to phy\n");
Stefan Chulskib261d1b2019-08-15 18:08:41 -04004527 return;
Stefan Roese96c19042016-02-10 07:22:10 +01004528 }
4529 phy_dev->supported &= PHY_GBIT_FEATURES;
4530 phy_dev->advertising = phy_dev->supported;
4531
4532 port->phy_dev = phy_dev;
4533 port->link = 0;
4534 port->duplex = 0;
4535 port->speed = 0;
4536
4537 phy_config(phy_dev);
4538 phy_startup(phy_dev);
Stefan Chulskib261d1b2019-08-15 18:08:41 -04004539 if (!phy_dev->link)
Stefan Roese96c19042016-02-10 07:22:10 +01004540 printf("%s: No link\n", phy_dev->dev->name);
Stefan Chulskib261d1b2019-08-15 18:08:41 -04004541 else
4542 port->init = 1;
Stefan Roese96c19042016-02-10 07:22:10 +01004543 } else {
4544 mvpp2_egress_enable(port);
4545 mvpp2_ingress_enable(port);
4546 }
Stefan Roese96c19042016-02-10 07:22:10 +01004547}
4548
4549static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
4550{
4551 unsigned char mac_bcast[ETH_ALEN] = {
4552 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4553 int err;
4554
4555 err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
4556 if (err) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004557 dev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004558 return err;
4559 }
4560 err = mvpp2_prs_mac_da_accept(port->priv, port->id,
4561 port->dev_addr, true);
4562 if (err) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004563 dev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004564 return err;
4565 }
4566 err = mvpp2_prs_def_flow(port);
4567 if (err) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004568 dev_err(dev, "mvpp2_prs_def_flow failed\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004569 return err;
4570 }
4571
4572 /* Allocate the Rx/Tx queues */
4573 err = mvpp2_setup_rxqs(port);
4574 if (err) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004575 dev_err(port->phy_dev->dev, "cannot allocate Rx queues\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004576 return err;
4577 }
4578
4579 err = mvpp2_setup_txqs(port);
4580 if (err) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04004581 dev_err(port->phy_dev->dev, "cannot allocate Tx queues\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004582 return err;
4583 }
4584
Nevo Hed5e975612019-08-15 18:08:44 -04004585 if (port->phyaddr < PHY_MAX_ADDR) {
Stefan Chulskib261d1b2019-08-15 18:08:41 -04004586 mvpp2_phy_connect(dev, port);
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004587 mvpp2_link_event(port);
4588 } else {
4589 mvpp2_egress_enable(port);
4590 mvpp2_ingress_enable(port);
4591 }
Stefan Roese96c19042016-02-10 07:22:10 +01004592
4593 mvpp2_start_dev(port);
4594
4595 return 0;
4596}
4597
4598/* No Device ops here in U-Boot */
4599
4600/* Driver initialization */
4601
4602static void mvpp2_port_power_up(struct mvpp2_port *port)
4603{
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01004604 struct mvpp2 *priv = port->priv;
4605
Stefan Roese40e749b2017-03-22 15:07:30 +01004606 /* On PPv2.2 the GoP / interface configuration has already been done */
4607 if (priv->hw_version == MVPP21)
4608 mvpp2_port_mii_set(port);
Stefan Roese96c19042016-02-10 07:22:10 +01004609 mvpp2_port_periodic_xon_disable(port);
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01004610 if (priv->hw_version == MVPP21)
4611 mvpp2_port_fc_adv_enable(port);
Stefan Roese96c19042016-02-10 07:22:10 +01004612 mvpp2_port_reset(port);
4613}
4614
4615/* Initialize port HW */
4616static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
4617{
4618 struct mvpp2 *priv = port->priv;
4619 struct mvpp2_txq_pcpu *txq_pcpu;
4620 int queue, cpu, err;
4621
Thomas Petazzoni38a23282017-02-16 09:03:16 +01004622 if (port->first_rxq + rxq_number >
4623 MVPP2_MAX_PORTS * priv->max_port_rxqs)
Stefan Roese96c19042016-02-10 07:22:10 +01004624 return -EINVAL;
4625
4626 /* Disable port */
4627 mvpp2_egress_disable(port);
Stefan Roese40e749b2017-03-22 15:07:30 +01004628 if (priv->hw_version == MVPP21)
4629 mvpp2_port_disable(port);
4630 else
4631 gop_port_enable(port, 0);
Stefan Roese96c19042016-02-10 07:22:10 +01004632
4633 port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
4634 GFP_KERNEL);
4635 if (!port->txqs)
4636 return -ENOMEM;
4637
4638 /* Associate physical Tx queues to this port and initialize.
4639 * The mapping is predefined.
4640 */
4641 for (queue = 0; queue < txq_number; queue++) {
4642 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
4643 struct mvpp2_tx_queue *txq;
4644
4645 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
4646 if (!txq)
4647 return -ENOMEM;
4648
4649 txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
4650 GFP_KERNEL);
4651 if (!txq->pcpu)
4652 return -ENOMEM;
4653
4654 txq->id = queue_phy_id;
4655 txq->log_id = queue;
4656 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
4657 for_each_present_cpu(cpu) {
4658 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4659 txq_pcpu->cpu = cpu;
4660 }
4661
4662 port->txqs[queue] = txq;
4663 }
4664
4665 port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
4666 GFP_KERNEL);
4667 if (!port->rxqs)
4668 return -ENOMEM;
4669
4670 /* Allocate and initialize Rx queue for this port */
4671 for (queue = 0; queue < rxq_number; queue++) {
4672 struct mvpp2_rx_queue *rxq;
4673
4674 /* Map physical Rx queue to port's logical Rx queue */
4675 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
4676 if (!rxq)
4677 return -ENOMEM;
4678 /* Map this Rx queue to a physical queue */
4679 rxq->id = port->first_rxq + queue;
4680 rxq->port = port->id;
4681 rxq->logic_rxq = queue;
4682
4683 port->rxqs[queue] = rxq;
4684 }
4685
Stefan Roese96c19042016-02-10 07:22:10 +01004686
4687 /* Create Rx descriptor rings */
4688 for (queue = 0; queue < rxq_number; queue++) {
4689 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
4690
4691 rxq->size = port->rx_ring_size;
4692 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
4693 rxq->time_coal = MVPP2_RX_COAL_USEC;
4694 }
4695
4696 mvpp2_ingress_disable(port);
4697
4698 /* Port default configuration */
4699 mvpp2_defaults_set(port);
4700
4701 /* Port's classifier configuration */
4702 mvpp2_cls_oversize_rxq_set(port);
4703 mvpp2_cls_port_config(port);
4704
4705 /* Provide an initial Rx packet size */
4706 port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
4707
4708 /* Initialize pools for swf */
4709 err = mvpp2_swf_bm_pool_init(port);
4710 if (err)
4711 return err;
4712
4713 return 0;
4714}
4715
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004716static int phy_info_parse(struct udevice *dev, struct mvpp2_port *port)
Stefan Roese96c19042016-02-10 07:22:10 +01004717{
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004718 int port_node = dev_of_offset(dev);
4719 const char *phy_mode_str;
Baruch Siach4f42c862018-11-21 13:05:33 +02004720 int phy_node;
Stefan Roese96c19042016-02-10 07:22:10 +01004721 u32 id;
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004722 u32 phyaddr = 0;
Stefan Roese96c19042016-02-10 07:22:10 +01004723 int phy_mode = -1;
Nevo Hed5e975612019-08-15 18:08:44 -04004724 int ret;
Baruch Siach3cf0f2e2018-11-21 13:05:34 +02004725
Stefan Roese96c19042016-02-10 07:22:10 +01004726 phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004727
4728 if (phy_node > 0) {
Nevo Hed5e975612019-08-15 18:08:44 -04004729 int parent;
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004730 phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
4731 if (phyaddr < 0) {
Sean Anderson77a88792020-09-15 10:44:56 -04004732 dev_err(dev, "could not find phy address\n");
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004733 return -1;
4734 }
Nevo Hed5e975612019-08-15 18:08:44 -04004735 parent = fdt_parent_offset(gd->fdt_blob, phy_node);
4736 ret = uclass_get_device_by_of_offset(UCLASS_MDIO, parent,
4737 &port->mdio_dev);
4738 if (ret)
4739 return ret;
Stefan Chulskia27adcb2017-04-06 15:39:08 +02004740 } else {
Nevo Hed5e975612019-08-15 18:08:44 -04004741 /* phy_addr is set to invalid value */
4742 phyaddr = PHY_MAX_ADDR;
Stefan Roese96c19042016-02-10 07:22:10 +01004743 }
4744
4745 phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
4746 if (phy_mode_str)
4747 phy_mode = phy_get_interface_by_name(phy_mode_str);
4748 if (phy_mode == -1) {
Sean Anderson77a88792020-09-15 10:44:56 -04004749 dev_err(dev, "incorrect phy mode\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004750 return -EINVAL;
4751 }
4752
4753 id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
4754 if (id == -1) {
Sean Anderson77a88792020-09-15 10:44:56 -04004755 dev_err(dev, "missing port-id value\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004756 return -EINVAL;
4757 }
4758
Simon Glassfa4689a2019-12-06 21:41:35 -07004759#if CONFIG_IS_ENABLED(DM_GPIO)
Stefan Chulski0d65eb62017-08-09 10:37:43 +03004760 gpio_request_by_name(dev, "phy-reset-gpios", 0,
4761 &port->phy_reset_gpio, GPIOD_IS_OUT);
4762 gpio_request_by_name(dev, "marvell,sfp-tx-disable-gpio", 0,
4763 &port->phy_tx_disable_gpio, GPIOD_IS_OUT);
4764#endif
4765
Stefan Roese3651bfc2017-03-22 14:15:40 +01004766 /*
4767 * ToDo:
4768 * Not sure if this DT property "phy-speed" will get accepted, so
4769 * this might change later
4770 */
4771 /* Get phy-speed for SGMII 2.5Gbps vs 1Gbps setup */
4772 port->phy_speed = fdtdec_get_int(gd->fdt_blob, port_node,
4773 "phy-speed", 1000);
4774
Stefan Roese96c19042016-02-10 07:22:10 +01004775 port->id = id;
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004776 if (port->priv->hw_version == MVPP21)
Thomas Petazzoni38a23282017-02-16 09:03:16 +01004777 port->first_rxq = port->id * rxq_number;
4778 else
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004779 port->first_rxq = port->id * port->priv->max_port_rxqs;
Stefan Roese96c19042016-02-10 07:22:10 +01004780 port->phy_interface = phy_mode;
4781 port->phyaddr = phyaddr;
4782
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004783 return 0;
4784}
Thomas Petazzoni5555f072017-02-16 08:03:37 +01004785
Simon Glassfa4689a2019-12-06 21:41:35 -07004786#if CONFIG_IS_ENABLED(DM_GPIO)
Stefan Chulski0d65eb62017-08-09 10:37:43 +03004787/* Port GPIO initialization */
4788static void mvpp2_gpio_init(struct mvpp2_port *port)
4789{
4790 if (dm_gpio_is_valid(&port->phy_reset_gpio)) {
Stefan Chulski0d65eb62017-08-09 10:37:43 +03004791 dm_gpio_set_value(&port->phy_reset_gpio, 1);
Baruch Siach67674e42018-10-15 13:16:48 +03004792 mdelay(10);
Baruch Siach187dced2018-10-15 13:16:47 +03004793 dm_gpio_set_value(&port->phy_reset_gpio, 0);
Stefan Chulski0d65eb62017-08-09 10:37:43 +03004794 }
4795
4796 if (dm_gpio_is_valid(&port->phy_tx_disable_gpio))
4797 dm_gpio_set_value(&port->phy_tx_disable_gpio, 0);
4798}
4799#endif
4800
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004801/* Ports initialization */
4802static int mvpp2_port_probe(struct udevice *dev,
4803 struct mvpp2_port *port,
4804 int port_node,
4805 struct mvpp2 *priv)
4806{
4807 int err;
Stefan Roese96c19042016-02-10 07:22:10 +01004808
4809 port->tx_ring_size = MVPP2_MAX_TXD;
4810 port->rx_ring_size = MVPP2_MAX_RXD;
4811
4812 err = mvpp2_port_init(dev, port);
4813 if (err < 0) {
Sean Anderson77a88792020-09-15 10:44:56 -04004814 dev_err(dev, "failed to init port %d\n", port->id);
Stefan Roese96c19042016-02-10 07:22:10 +01004815 return err;
4816 }
4817 mvpp2_port_power_up(port);
4818
Simon Glassfa4689a2019-12-06 21:41:35 -07004819#if CONFIG_IS_ENABLED(DM_GPIO)
Stefan Chulski0d65eb62017-08-09 10:37:43 +03004820 mvpp2_gpio_init(port);
4821#endif
4822
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01004823 priv->port_list[port->id] = port;
Stefan Chulski75872182017-08-09 10:37:46 +03004824 priv->num_ports++;
Stefan Roese96c19042016-02-10 07:22:10 +01004825 return 0;
4826}
4827
4828/* Initialize decoding windows */
4829static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
4830 struct mvpp2 *priv)
4831{
4832 u32 win_enable;
4833 int i;
4834
4835 for (i = 0; i < 6; i++) {
4836 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
4837 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
4838
4839 if (i < 4)
4840 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
4841 }
4842
4843 win_enable = 0;
4844
4845 for (i = 0; i < dram->num_cs; i++) {
4846 const struct mbus_dram_window *cs = dram->cs + i;
4847
4848 mvpp2_write(priv, MVPP2_WIN_BASE(i),
4849 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
4850 dram->mbus_dram_target_id);
4851
4852 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
4853 (cs->size - 1) & 0xffff0000);
4854
4855 win_enable |= (1 << i);
4856 }
4857
4858 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
4859}
4860
4861/* Initialize Rx FIFO's */
4862static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
4863{
4864 int port;
4865
4866 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
Stefan Roesea8801ed2017-03-01 13:09:42 +01004867 if (priv->hw_version == MVPP22) {
4868 if (port == 0) {
4869 mvpp2_write(priv,
4870 MVPP2_RX_DATA_FIFO_SIZE_REG(port),
4871 MVPP22_RX_FIFO_10GB_PORT_DATA_SIZE);
4872 mvpp2_write(priv,
4873 MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
4874 MVPP22_RX_FIFO_10GB_PORT_ATTR_SIZE);
4875 } else if (port == 1) {
4876 mvpp2_write(priv,
4877 MVPP2_RX_DATA_FIFO_SIZE_REG(port),
4878 MVPP22_RX_FIFO_2_5GB_PORT_DATA_SIZE);
4879 mvpp2_write(priv,
4880 MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
4881 MVPP22_RX_FIFO_2_5GB_PORT_ATTR_SIZE);
4882 } else {
4883 mvpp2_write(priv,
4884 MVPP2_RX_DATA_FIFO_SIZE_REG(port),
4885 MVPP22_RX_FIFO_1GB_PORT_DATA_SIZE);
4886 mvpp2_write(priv,
4887 MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
4888 MVPP22_RX_FIFO_1GB_PORT_ATTR_SIZE);
4889 }
4890 } else {
4891 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
4892 MVPP21_RX_FIFO_PORT_DATA_SIZE);
4893 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
4894 MVPP21_RX_FIFO_PORT_ATTR_SIZE);
4895 }
Stefan Roese96c19042016-02-10 07:22:10 +01004896 }
4897
4898 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
4899 MVPP2_RX_FIFO_PORT_MIN_PKT);
4900 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
4901}
4902
Stefan Roesea8801ed2017-03-01 13:09:42 +01004903/* Initialize Tx FIFO's */
4904static void mvpp2_tx_fifo_init(struct mvpp2 *priv)
4905{
4906 int port, val;
4907
4908 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
4909 /* Port 0 supports 10KB TX FIFO */
4910 if (port == 0) {
4911 val = MVPP2_TX_FIFO_DATA_SIZE_10KB &
4912 MVPP22_TX_FIFO_SIZE_MASK;
4913 } else {
4914 val = MVPP2_TX_FIFO_DATA_SIZE_3KB &
4915 MVPP22_TX_FIFO_SIZE_MASK;
4916 }
4917 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), val);
4918 }
4919}
4920
Thomas Petazzonica560ab2017-02-16 08:41:07 +01004921static void mvpp2_axi_init(struct mvpp2 *priv)
4922{
4923 u32 val, rdval, wrval;
4924
4925 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
4926
4927 /* AXI Bridge Configuration */
4928
4929 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
4930 << MVPP22_AXI_ATTR_CACHE_OFFS;
4931 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
4932 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
4933
4934 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
4935 << MVPP22_AXI_ATTR_CACHE_OFFS;
4936 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
4937 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
4938
4939 /* BM */
4940 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
4941 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
4942
4943 /* Descriptors */
4944 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
4945 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
4946 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
4947 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
4948
4949 /* Buffer Data */
4950 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
4951 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
4952
4953 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
4954 << MVPP22_AXI_CODE_CACHE_OFFS;
4955 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
4956 << MVPP22_AXI_CODE_DOMAIN_OFFS;
4957 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
4958 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
4959
4960 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
4961 << MVPP22_AXI_CODE_CACHE_OFFS;
4962 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
4963 << MVPP22_AXI_CODE_DOMAIN_OFFS;
4964
4965 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
4966
4967 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
4968 << MVPP22_AXI_CODE_CACHE_OFFS;
4969 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
4970 << MVPP22_AXI_CODE_DOMAIN_OFFS;
4971
4972 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
4973}
4974
Stefan Roese96c19042016-02-10 07:22:10 +01004975/* Initialize network controller common part HW */
4976static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
4977{
4978 const struct mbus_dram_target_info *dram_target_info;
4979 int err, i;
4980 u32 val;
4981
4982 /* Checks for hardware constraints (U-Boot uses only one rxq) */
Thomas Petazzoni38a23282017-02-16 09:03:16 +01004983 if ((rxq_number > priv->max_port_rxqs) ||
4984 (txq_number > MVPP2_MAX_TXQ)) {
Sean Anderson77a88792020-09-15 10:44:56 -04004985 dev_err(dev, "invalid queue size parameter\n");
Stefan Roese96c19042016-02-10 07:22:10 +01004986 return -EINVAL;
4987 }
4988
Thomas Petazzonica560ab2017-02-16 08:41:07 +01004989 if (priv->hw_version == MVPP22)
4990 mvpp2_axi_init(priv);
Stefan Chulskicaa97bf2017-08-09 10:37:48 +03004991 else {
4992 /* MBUS windows configuration */
4993 dram_target_info = mvebu_mbus_dram_info();
4994 if (dram_target_info)
4995 mvpp2_conf_mbus_windows(dram_target_info, priv);
4996 }
Thomas Petazzonica560ab2017-02-16 08:41:07 +01004997
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01004998 if (priv->hw_version == MVPP21) {
Stefan Roeseb851e402017-03-09 12:01:57 +01004999 /* Disable HW PHY polling */
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01005000 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
5001 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
5002 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
5003 } else {
Stefan Roeseb851e402017-03-09 12:01:57 +01005004 /* Enable HW PHY polling */
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01005005 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
Stefan Roeseb851e402017-03-09 12:01:57 +01005006 val |= MVPP22_SMI_POLLING_EN;
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01005007 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
5008 }
Stefan Roese96c19042016-02-10 07:22:10 +01005009
5010 /* Allocate and initialize aggregated TXQs */
5011 priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
5012 sizeof(struct mvpp2_tx_queue),
5013 GFP_KERNEL);
5014 if (!priv->aggr_txqs)
5015 return -ENOMEM;
5016
5017 for_each_present_cpu(i) {
5018 priv->aggr_txqs[i].id = i;
5019 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
5020 err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
5021 MVPP2_AGGR_TXQ_SIZE, i, priv);
5022 if (err < 0)
5023 return err;
5024 }
5025
5026 /* Rx Fifo Init */
5027 mvpp2_rx_fifo_init(priv);
5028
Stefan Roesea8801ed2017-03-01 13:09:42 +01005029 /* Tx Fifo Init */
5030 if (priv->hw_version == MVPP22)
5031 mvpp2_tx_fifo_init(priv);
5032
Thomas Petazzonicc2445f2017-02-20 11:42:51 +01005033 if (priv->hw_version == MVPP21)
5034 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
5035 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
Stefan Roese96c19042016-02-10 07:22:10 +01005036
5037 /* Allow cache snoop when transmiting packets */
5038 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
5039
5040 /* Buffer Manager initialization */
5041 err = mvpp2_bm_init(dev, priv);
5042 if (err < 0)
5043 return err;
5044
5045 /* Parser default initialization */
5046 err = mvpp2_prs_default_init(dev, priv);
5047 if (err < 0)
5048 return err;
5049
5050 /* Classifier default initialization */
5051 mvpp2_cls_init(priv);
5052
5053 return 0;
5054}
5055
Stefan Roese96c19042016-02-10 07:22:10 +01005056static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
5057{
5058 struct mvpp2_port *port = dev_get_priv(dev);
5059 struct mvpp2_rx_desc *rx_desc;
5060 struct mvpp2_bm_pool *bm_pool;
Thomas Petazzonic49aff22017-02-20 10:27:51 +01005061 dma_addr_t dma_addr;
Stefan Roese96c19042016-02-10 07:22:10 +01005062 u32 bm, rx_status;
5063 int pool, rx_bytes, err;
5064 int rx_received;
5065 struct mvpp2_rx_queue *rxq;
Stefan Roese96c19042016-02-10 07:22:10 +01005066 u8 *data;
5067
Nevo Hed5e975612019-08-15 18:08:44 -04005068 if (port->phyaddr < PHY_MAX_ADDR)
Stefan Chulskib261d1b2019-08-15 18:08:41 -04005069 if (!port->phy_dev->link)
5070 return 0;
5071
Stefan Roese96c19042016-02-10 07:22:10 +01005072 /* Process RX packets */
Stefan Chulskib4025532017-08-09 10:37:49 +03005073 rxq = port->rxqs[0];
Stefan Roese96c19042016-02-10 07:22:10 +01005074
5075 /* Get number of received packets and clamp the to-do */
5076 rx_received = mvpp2_rxq_received(port, rxq->id);
5077
5078 /* Return if no packets are received */
5079 if (!rx_received)
5080 return 0;
5081
5082 rx_desc = mvpp2_rxq_next_desc_get(rxq);
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01005083 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
5084 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
5085 rx_bytes -= MVPP2_MH_SIZE;
5086 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
Stefan Roese96c19042016-02-10 07:22:10 +01005087
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01005088 bm = mvpp2_bm_cookie_build(port, rx_desc);
Stefan Roese96c19042016-02-10 07:22:10 +01005089 pool = mvpp2_bm_cookie_pool_get(bm);
5090 bm_pool = &port->priv->bm_pools[pool];
5091
Stefan Roese96c19042016-02-10 07:22:10 +01005092 /* In case of an error, release the requested buffer pointer
5093 * to the Buffer Manager. This request process is controlled
5094 * by the hardware, and the information about the buffer is
5095 * comprised by the RX descriptor.
5096 */
5097 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
5098 mvpp2_rx_error(port, rx_desc);
5099 /* Return the buffer to the pool */
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01005100 mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
Stefan Roese96c19042016-02-10 07:22:10 +01005101 return 0;
5102 }
5103
Thomas Petazzonic49aff22017-02-20 10:27:51 +01005104 err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
Stefan Roese96c19042016-02-10 07:22:10 +01005105 if (err) {
Sean Andersonc7cbf092020-09-15 10:44:57 -04005106 dev_err(port->phy_dev->dev, "failed to refill BM pools\n");
Stefan Roese96c19042016-02-10 07:22:10 +01005107 return 0;
5108 }
5109
5110 /* Update Rx queue management counters */
5111 mb();
5112 mvpp2_rxq_status_update(port, rxq->id, 1, 1);
5113
5114 /* give packet to stack - skip on first n bytes */
Thomas Petazzonic49aff22017-02-20 10:27:51 +01005115 data = (u8 *)dma_addr + 2 + 32;
Stefan Roese96c19042016-02-10 07:22:10 +01005116
5117 if (rx_bytes <= 0)
5118 return 0;
5119
5120 /*
5121 * No cache invalidation needed here, since the rx_buffer's are
5122 * located in a uncached memory region
5123 */
5124 *packetp = data;
5125
5126 return rx_bytes;
5127}
5128
Stefan Roese96c19042016-02-10 07:22:10 +01005129static int mvpp2_send(struct udevice *dev, void *packet, int length)
5130{
5131 struct mvpp2_port *port = dev_get_priv(dev);
5132 struct mvpp2_tx_queue *txq, *aggr_txq;
5133 struct mvpp2_tx_desc *tx_desc;
5134 int tx_done;
5135 int timeout;
5136
Nevo Hed5e975612019-08-15 18:08:44 -04005137 if (port->phyaddr < PHY_MAX_ADDR)
Stefan Chulskib261d1b2019-08-15 18:08:41 -04005138 if (!port->phy_dev->link)
5139 return 0;
5140
Stefan Roese96c19042016-02-10 07:22:10 +01005141 txq = port->txqs[0];
5142 aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
5143
5144 /* Get a descriptor for the first part of the packet */
5145 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01005146 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
5147 mvpp2_txdesc_size_set(port, tx_desc, length);
5148 mvpp2_txdesc_offset_set(port, tx_desc,
5149 (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
5150 mvpp2_txdesc_dma_addr_set(port, tx_desc,
5151 (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
Stefan Roese96c19042016-02-10 07:22:10 +01005152 /* First and Last descriptor */
Thomas Petazzonifb3a7bb2017-02-15 15:35:00 +01005153 mvpp2_txdesc_cmd_set(port, tx_desc,
5154 MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
5155 | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
Stefan Roese96c19042016-02-10 07:22:10 +01005156
5157 /* Flush tx data */
Stefan Roeseb4268e22017-02-16 13:58:37 +01005158 flush_dcache_range((unsigned long)packet,
5159 (unsigned long)packet + ALIGN(length, PKTALIGN));
Stefan Roese96c19042016-02-10 07:22:10 +01005160
5161 /* Enable transmit */
5162 mb();
5163 mvpp2_aggr_txq_pend_desc_add(port, 1);
5164
5165 mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
5166
5167 timeout = 0;
5168 do {
5169 if (timeout++ > 10000) {
5170 printf("timeout: packet not sent from aggregated to phys TXQ\n");
5171 return 0;
5172 }
5173 tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
5174 } while (tx_done);
5175
Stefan Roese96c19042016-02-10 07:22:10 +01005176 timeout = 0;
5177 do {
5178 if (timeout++ > 10000) {
5179 printf("timeout: packet not sent\n");
5180 return 0;
5181 }
5182 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5183 } while (!tx_done);
5184
Stefan Roese96c19042016-02-10 07:22:10 +01005185 return 0;
5186}
5187
5188static int mvpp2_start(struct udevice *dev)
5189{
Simon Glassfa20e932020-12-03 16:55:20 -07005190 struct eth_pdata *pdata = dev_get_plat(dev);
Stefan Roese96c19042016-02-10 07:22:10 +01005191 struct mvpp2_port *port = dev_get_priv(dev);
5192
5193 /* Load current MAC address */
5194 memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
5195
5196 /* Reconfigure parser accept the original MAC address */
5197 mvpp2_prs_update_mac_da(port, port->dev_addr);
5198
Stefan Chulskia27adcb2017-04-06 15:39:08 +02005199 switch (port->phy_interface) {
5200 case PHY_INTERFACE_MODE_RGMII:
5201 case PHY_INTERFACE_MODE_RGMII_ID:
5202 case PHY_INTERFACE_MODE_SGMII:
5203 mvpp2_port_power_up(port);
5204 default:
5205 break;
5206 }
Stefan Roese96c19042016-02-10 07:22:10 +01005207
5208 mvpp2_open(dev, port);
5209
5210 return 0;
5211}
5212
5213static void mvpp2_stop(struct udevice *dev)
5214{
5215 struct mvpp2_port *port = dev_get_priv(dev);
5216
5217 mvpp2_stop_dev(port);
5218 mvpp2_cleanup_rxqs(port);
5219 mvpp2_cleanup_txqs(port);
5220}
5221
Matt Pelland5712d7d2019-07-30 09:40:24 -04005222static int mvpp2_write_hwaddr(struct udevice *dev)
5223{
5224 struct mvpp2_port *port = dev_get_priv(dev);
5225
5226 return mvpp2_prs_update_mac_da(port, port->dev_addr);
5227}
5228
Stefan Roese95b27412017-03-10 06:07:45 +01005229static int mvpp22_smi_phy_addr_cfg(struct mvpp2_port *port)
5230{
5231 writel(port->phyaddr, port->priv->iface_base +
5232 MVPP22_SMI_PHY_ADDR_REG(port->gop_id));
5233
5234 return 0;
5235}
5236
Stefan Roese96c19042016-02-10 07:22:10 +01005237static int mvpp2_base_probe(struct udevice *dev)
5238{
5239 struct mvpp2 *priv = dev_get_priv(dev);
Stefan Roese96c19042016-02-10 07:22:10 +01005240 void *bd_space;
5241 u32 size = 0;
5242 int i;
5243
Thomas Petazzoni51ccb412017-02-15 14:08:59 +01005244 /* Save hw-version */
5245 priv->hw_version = dev_get_driver_data(dev);
5246
Stefan Roese96c19042016-02-10 07:22:10 +01005247 /*
5248 * U-Boot special buffer handling:
5249 *
5250 * Allocate buffer area for descs and rx_buffers. This is only
5251 * done once for all interfaces. As only one interface can
5252 * be active. Make this area DMA-safe by disabling the D-cache
5253 */
5254
Sven Auhagena50bca12020-07-01 17:43:43 +02005255 if (!buffer_loc_init) {
5256 /* Align buffer area for descs and rx_buffers to 1MiB */
5257 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
5258 mmu_set_region_dcache_behaviour((unsigned long)bd_space,
5259 BD_SPACE, DCACHE_OFF);
Stefan Roese96c19042016-02-10 07:22:10 +01005260
Sven Auhagena50bca12020-07-01 17:43:43 +02005261 buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
5262 size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
Stefan Roese96c19042016-02-10 07:22:10 +01005263
Sven Auhagena50bca12020-07-01 17:43:43 +02005264 buffer_loc.tx_descs =
5265 (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
5266 size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
Stefan Roese96c19042016-02-10 07:22:10 +01005267
Sven Auhagena50bca12020-07-01 17:43:43 +02005268 buffer_loc.rx_descs =
5269 (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
5270 size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
Stefan Roese96c19042016-02-10 07:22:10 +01005271
Sven Auhagena50bca12020-07-01 17:43:43 +02005272 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
5273 buffer_loc.bm_pool[i] =
5274 (unsigned long *)((unsigned long)bd_space + size);
5275 if (priv->hw_version == MVPP21)
5276 size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u32);
5277 else
5278 size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u64);
5279 }
Stefan Roese96c19042016-02-10 07:22:10 +01005280
Sven Auhagena50bca12020-07-01 17:43:43 +02005281 for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
5282 buffer_loc.rx_buffer[i] =
5283 (unsigned long *)((unsigned long)bd_space + size);
5284 size += RX_BUFFER_SIZE;
5285 }
Stefan Roese96c19042016-02-10 07:22:10 +01005286
Sven Auhagena50bca12020-07-01 17:43:43 +02005287 /* Clear the complete area so that all descriptors are cleared */
5288 memset(bd_space, 0, size);
5289
5290 buffer_loc_init = 1;
5291 }
Stefan Roese4cc75412017-02-16 13:29:08 +01005292
Stefan Roese96c19042016-02-10 07:22:10 +01005293 /* Save base addresses for later use */
Simon Glassba1dea42017-05-17 17:18:05 -06005294 priv->base = (void *)devfdt_get_addr_index(dev, 0);
Stefan Roese96c19042016-02-10 07:22:10 +01005295 if (IS_ERR(priv->base))
5296 return PTR_ERR(priv->base);
5297
Thomas Petazzoni5555f072017-02-16 08:03:37 +01005298 if (priv->hw_version == MVPP21) {
Simon Glassba1dea42017-05-17 17:18:05 -06005299 priv->lms_base = (void *)devfdt_get_addr_index(dev, 1);
Thomas Petazzoni5555f072017-02-16 08:03:37 +01005300 if (IS_ERR(priv->lms_base))
5301 return PTR_ERR(priv->lms_base);
5302 } else {
Simon Glassba1dea42017-05-17 17:18:05 -06005303 priv->iface_base = (void *)devfdt_get_addr_index(dev, 1);
Thomas Petazzoni5555f072017-02-16 08:03:37 +01005304 if (IS_ERR(priv->iface_base))
5305 return PTR_ERR(priv->iface_base);
Stefan Roeseb71c2a32017-02-16 08:31:32 +01005306
Stefan Roese40e749b2017-03-22 15:07:30 +01005307 /* Store common base addresses for all ports */
5308 priv->mpcs_base = priv->iface_base + MVPP22_MPCS;
5309 priv->xpcs_base = priv->iface_base + MVPP22_XPCS;
5310 priv->rfu1_base = priv->iface_base + MVPP22_RFU1;
Thomas Petazzoni5555f072017-02-16 08:03:37 +01005311 }
Stefan Roese96c19042016-02-10 07:22:10 +01005312
Thomas Petazzoni38a23282017-02-16 09:03:16 +01005313 if (priv->hw_version == MVPP21)
5314 priv->max_port_rxqs = 8;
5315 else
5316 priv->max_port_rxqs = 32;
5317
Baruch Siach3cf0f2e2018-11-21 13:05:34 +02005318 return 0;
Stefan Roese96c19042016-02-10 07:22:10 +01005319}
5320
Stefan Roesed017cdf2017-02-16 15:26:06 +01005321static int mvpp2_probe(struct udevice *dev)
5322{
5323 struct mvpp2_port *port = dev_get_priv(dev);
5324 struct mvpp2 *priv = dev_get_priv(dev->parent);
5325 int err;
5326
5327 /* Only call the probe function for the parent once */
Stefan Chulski75872182017-08-09 10:37:46 +03005328 if (!priv->probe_done)
Stefan Roesed017cdf2017-02-16 15:26:06 +01005329 err = mvpp2_base_probe(dev->parent);
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01005330
Nevo Hed5e975612019-08-15 18:08:44 -04005331 port->priv = priv;
Baruch Siach3cf0f2e2018-11-21 13:05:34 +02005332
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01005333 err = phy_info_parse(dev, port);
5334 if (err)
5335 return err;
5336
5337 /*
5338 * We need the port specific io base addresses at this stage, since
5339 * gop_port_init() accesses these registers
5340 */
5341 if (priv->hw_version == MVPP21) {
5342 int priv_common_regs_num = 2;
5343
Simon Glassba1dea42017-05-17 17:18:05 -06005344 port->base = (void __iomem *)devfdt_get_addr_index(
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01005345 dev->parent, priv_common_regs_num + port->id);
5346 if (IS_ERR(port->base))
5347 return PTR_ERR(port->base);
5348 } else {
5349 port->gop_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
5350 "gop-port-id", -1);
5351 if (port->id == -1) {
Sean Anderson77a88792020-09-15 10:44:56 -04005352 dev_err(dev, "missing gop-port-id value\n");
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01005353 return -EINVAL;
5354 }
5355
5356 port->base = priv->iface_base + MVPP22_PORT_BASE +
5357 port->gop_id * MVPP22_PORT_OFFSET;
Stefan Roese40e749b2017-03-22 15:07:30 +01005358
Stefan Roese95b27412017-03-10 06:07:45 +01005359 /* Set phy address of the port */
Nevo Hed5e975612019-08-15 18:08:44 -04005360 if (port->phyaddr < PHY_MAX_ADDR)
Stefan Chulskia27adcb2017-04-06 15:39:08 +02005361 mvpp22_smi_phy_addr_cfg(port);
Stefan Roese95b27412017-03-10 06:07:45 +01005362
Stefan Roese40e749b2017-03-22 15:07:30 +01005363 /* GoP Init */
5364 gop_port_init(port);
Stefan Roese8ba5f0b2017-03-22 14:11:16 +01005365 }
5366
Stefan Chulski75872182017-08-09 10:37:46 +03005367 if (!priv->probe_done) {
5368 /* Initialize network controller */
5369 err = mvpp2_init(dev, priv);
5370 if (err < 0) {
Sean Anderson77a88792020-09-15 10:44:56 -04005371 dev_err(dev, "failed to initialize controller\n");
Stefan Chulski75872182017-08-09 10:37:46 +03005372 return err;
5373 }
5374 priv->num_ports = 0;
5375 priv->probe_done = 1;
Stefan Roesed017cdf2017-02-16 15:26:06 +01005376 }
5377
Stefan Roese40e749b2017-03-22 15:07:30 +01005378 err = mvpp2_port_probe(dev, port, dev_of_offset(dev), priv);
5379 if (err)
5380 return err;
5381
5382 if (priv->hw_version == MVPP22) {
5383 priv->netc_config |= mvpp2_netc_cfg_create(port->gop_id,
5384 port->phy_interface);
5385
5386 /* Netcomplex configurations for all ports */
5387 gop_netc_init(priv, MV_NETC_FIRST_PHASE);
5388 gop_netc_init(priv, MV_NETC_SECOND_PHASE);
5389 }
5390
5391 return 0;
Stefan Roesed017cdf2017-02-16 15:26:06 +01005392}
5393
Stefan Roese380b3232017-03-23 17:01:59 +01005394/*
5395 * Empty BM pool and stop its activity before the OS is started
5396 */
5397static int mvpp2_remove(struct udevice *dev)
5398{
5399 struct mvpp2_port *port = dev_get_priv(dev);
5400 struct mvpp2 *priv = port->priv;
5401 int i;
5402
Stefan Chulski75872182017-08-09 10:37:46 +03005403 priv->num_ports--;
5404
5405 if (priv->num_ports)
5406 return 0;
5407
Stefan Roese380b3232017-03-23 17:01:59 +01005408 for (i = 0; i < MVPP2_BM_POOLS_NUM; i++)
5409 mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
5410
5411 return 0;
5412}
5413
Stefan Roesed017cdf2017-02-16 15:26:06 +01005414static const struct eth_ops mvpp2_ops = {
5415 .start = mvpp2_start,
5416 .send = mvpp2_send,
5417 .recv = mvpp2_recv,
5418 .stop = mvpp2_stop,
Matt Pelland5712d7d2019-07-30 09:40:24 -04005419 .write_hwaddr = mvpp2_write_hwaddr
Stefan Roesed017cdf2017-02-16 15:26:06 +01005420};
5421
5422static struct driver mvpp2_driver = {
5423 .name = "mvpp2",
5424 .id = UCLASS_ETH,
5425 .probe = mvpp2_probe,
Stefan Roese380b3232017-03-23 17:01:59 +01005426 .remove = mvpp2_remove,
Stefan Roesed017cdf2017-02-16 15:26:06 +01005427 .ops = &mvpp2_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07005428 .priv_auto = sizeof(struct mvpp2_port),
Simon Glass71fa5b42020-12-03 16:55:18 -07005429 .plat_auto = sizeof(struct eth_pdata),
Stefan Roese380b3232017-03-23 17:01:59 +01005430 .flags = DM_FLAG_ACTIVE_DMA,
Stefan Roesed017cdf2017-02-16 15:26:06 +01005431};
5432
5433/*
5434 * Use a MISC device to bind the n instances (child nodes) of the
5435 * network base controller in UCLASS_ETH.
5436 */
Stefan Roese96c19042016-02-10 07:22:10 +01005437static int mvpp2_base_bind(struct udevice *parent)
5438{
5439 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07005440 int node = dev_of_offset(parent);
Stefan Roese96c19042016-02-10 07:22:10 +01005441 struct uclass_driver *drv;
5442 struct udevice *dev;
5443 struct eth_pdata *plat;
5444 char *name;
5445 int subnode;
5446 u32 id;
Stefan Roese38801d42017-02-24 10:12:41 +01005447 int base_id_add;
Stefan Roese96c19042016-02-10 07:22:10 +01005448
5449 /* Lookup eth driver */
5450 drv = lists_uclass_lookup(UCLASS_ETH);
5451 if (!drv) {
5452 puts("Cannot find eth driver\n");
5453 return -ENOENT;
5454 }
5455
Stefan Roese38801d42017-02-24 10:12:41 +01005456 base_id_add = base_id;
5457
Simon Glass499c29e2016-10-02 17:59:29 -06005458 fdt_for_each_subnode(subnode, blob, node) {
Stefan Roese38801d42017-02-24 10:12:41 +01005459 /* Increment base_id for all subnodes, also the disabled ones */
5460 base_id++;
5461
Stefan Roese96c19042016-02-10 07:22:10 +01005462 /* Skip disabled ports */
5463 if (!fdtdec_get_is_enabled(blob, subnode))
5464 continue;
5465
5466 plat = calloc(1, sizeof(*plat));
5467 if (!plat)
5468 return -ENOMEM;
5469
5470 id = fdtdec_get_int(blob, subnode, "port-id", -1);
Stefan Roese38801d42017-02-24 10:12:41 +01005471 id += base_id_add;
Stefan Roese96c19042016-02-10 07:22:10 +01005472
5473 name = calloc(1, 16);
Heinrich Schuchardtd0c42d72018-03-07 03:39:04 +01005474 if (!name) {
5475 free(plat);
5476 return -ENOMEM;
5477 }
Stefan Roese96c19042016-02-10 07:22:10 +01005478 sprintf(name, "mvpp2-%d", id);
5479
5480 /* Create child device UCLASS_ETH and bind it */
Simon Glass6996c662020-11-28 17:50:03 -07005481 device_bind(parent, &mvpp2_driver, name, plat,
5482 offset_to_ofnode(subnode), &dev);
Stefan Roese96c19042016-02-10 07:22:10 +01005483 }
5484
5485 return 0;
5486}
5487
5488static const struct udevice_id mvpp2_ids[] = {
Thomas Petazzoni51ccb412017-02-15 14:08:59 +01005489 {
5490 .compatible = "marvell,armada-375-pp2",
5491 .data = MVPP21,
5492 },
Thomas Petazzonie595a232017-02-20 11:54:31 +01005493 {
5494 .compatible = "marvell,armada-7k-pp22",
5495 .data = MVPP22,
5496 },
Stefan Roese96c19042016-02-10 07:22:10 +01005497 { }
5498};
5499
5500U_BOOT_DRIVER(mvpp2_base) = {
5501 .name = "mvpp2_base",
5502 .id = UCLASS_MISC,
5503 .of_match = mvpp2_ids,
5504 .bind = mvpp2_base_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -07005505 .priv_auto = sizeof(struct mvpp2),
Stefan Roese96c19042016-02-10 07:22:10 +01005506};