]> git.sur5r.net Git - u-boot/blob - drivers/net/mvpp2.c
net: mvpp2: handle register mapping and access for PPv2.2
[u-boot] / drivers / net / mvpp2.c
1 /*
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:
9  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
10  *
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>
17 #include <dm.h>
18 #include <dm/device-internal.h>
19 #include <dm/lists.h>
20 #include <net.h>
21 #include <netdev.h>
22 #include <config.h>
23 #include <malloc.h>
24 #include <asm/io.h>
25 #include <linux/errno.h>
26 #include <phy.h>
27 #include <miiphy.h>
28 #include <watchdog.h>
29 #include <asm/arch/cpu.h>
30 #include <asm/arch/soc.h>
31 #include <linux/compat.h>
32 #include <linux/mbus.h>
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 /* Some linux -> U-Boot compatibility stuff */
37 #define netdev_err(dev, fmt, args...)           \
38         printf(fmt, ##args)
39 #define netdev_warn(dev, fmt, args...)          \
40         printf(fmt, ##args)
41 #define netdev_info(dev, fmt, args...)          \
42         printf(fmt, ##args)
43 #define netdev_dbg(dev, fmt, args...)           \
44         printf(fmt, ##args)
45
46 #define ETH_ALEN        6               /* Octets in one ethernet addr  */
47
48 #define __verify_pcpu_ptr(ptr)                                          \
49 do {                                                                    \
50         const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL;    \
51         (void)__vpp_verify;                                             \
52 } while (0)
53
54 #define VERIFY_PERCPU_PTR(__p)                                          \
55 ({                                                                      \
56         __verify_pcpu_ptr(__p);                                         \
57         (typeof(*(__p)) __kernel __force *)(__p);                       \
58 })
59
60 #define per_cpu_ptr(ptr, cpu)   ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
61 #define smp_processor_id()      0
62 #define num_present_cpus()      1
63 #define for_each_present_cpu(cpu)                       \
64         for ((cpu) = 0; (cpu) < 1; (cpu)++)
65
66 #define NET_SKB_PAD     max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
67
68 #define CONFIG_NR_CPUS          1
69 #define ETH_HLEN                ETHER_HDR_SIZE  /* Total octets in header */
70
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
76 #define MVPP2_SMI_TIMEOUT                       10000
77
78 /* RX Fifo Registers */
79 #define MVPP2_RX_DATA_FIFO_SIZE_REG(port)       (0x00 + 4 * (port))
80 #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port)       (0x20 + 4 * (port))
81 #define MVPP2_RX_MIN_PKT_SIZE_REG               0x60
82 #define MVPP2_RX_FIFO_INIT_REG                  0x64
83
84 /* RX DMA Top Registers */
85 #define MVPP2_RX_CTRL_REG(port)                 (0x140 + 4 * (port))
86 #define     MVPP2_RX_LOW_LATENCY_PKT_SIZE(s)    (((s) & 0xfff) << 16)
87 #define     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK   BIT(31)
88 #define MVPP2_POOL_BUF_SIZE_REG(pool)           (0x180 + 4 * (pool))
89 #define     MVPP2_POOL_BUF_SIZE_OFFSET          5
90 #define MVPP2_RXQ_CONFIG_REG(rxq)               (0x800 + 4 * (rxq))
91 #define     MVPP2_SNOOP_PKT_SIZE_MASK           0x1ff
92 #define     MVPP2_SNOOP_BUF_HDR_MASK            BIT(9)
93 #define     MVPP2_RXQ_POOL_SHORT_OFFS           20
94 #define     MVPP21_RXQ_POOL_SHORT_MASK          0x700000
95 #define     MVPP22_RXQ_POOL_SHORT_MASK          0xf00000
96 #define     MVPP2_RXQ_POOL_LONG_OFFS            24
97 #define     MVPP21_RXQ_POOL_LONG_MASK           0x7000000
98 #define     MVPP22_RXQ_POOL_LONG_MASK           0xf000000
99 #define     MVPP2_RXQ_PACKET_OFFSET_OFFS        28
100 #define     MVPP2_RXQ_PACKET_OFFSET_MASK        0x70000000
101 #define     MVPP2_RXQ_DISABLE_MASK              BIT(31)
102
103 /* Parser Registers */
104 #define MVPP2_PRS_INIT_LOOKUP_REG               0x1000
105 #define     MVPP2_PRS_PORT_LU_MAX               0xf
106 #define     MVPP2_PRS_PORT_LU_MASK(port)        (0xff << ((port) * 4))
107 #define     MVPP2_PRS_PORT_LU_VAL(port, val)    ((val) << ((port) * 4))
108 #define MVPP2_PRS_INIT_OFFS_REG(port)           (0x1004 + ((port) & 4))
109 #define     MVPP2_PRS_INIT_OFF_MASK(port)       (0x3f << (((port) % 4) * 8))
110 #define     MVPP2_PRS_INIT_OFF_VAL(port, val)   ((val) << (((port) % 4) * 8))
111 #define MVPP2_PRS_MAX_LOOP_REG(port)            (0x100c + ((port) & 4))
112 #define     MVPP2_PRS_MAX_LOOP_MASK(port)       (0xff << (((port) % 4) * 8))
113 #define     MVPP2_PRS_MAX_LOOP_VAL(port, val)   ((val) << (((port) % 4) * 8))
114 #define MVPP2_PRS_TCAM_IDX_REG                  0x1100
115 #define MVPP2_PRS_TCAM_DATA_REG(idx)            (0x1104 + (idx) * 4)
116 #define     MVPP2_PRS_TCAM_INV_MASK             BIT(31)
117 #define MVPP2_PRS_SRAM_IDX_REG                  0x1200
118 #define MVPP2_PRS_SRAM_DATA_REG(idx)            (0x1204 + (idx) * 4)
119 #define MVPP2_PRS_TCAM_CTRL_REG                 0x1230
120 #define     MVPP2_PRS_TCAM_EN_MASK              BIT(0)
121
122 /* Classifier Registers */
123 #define MVPP2_CLS_MODE_REG                      0x1800
124 #define     MVPP2_CLS_MODE_ACTIVE_MASK          BIT(0)
125 #define MVPP2_CLS_PORT_WAY_REG                  0x1810
126 #define     MVPP2_CLS_PORT_WAY_MASK(port)       (1 << (port))
127 #define MVPP2_CLS_LKP_INDEX_REG                 0x1814
128 #define     MVPP2_CLS_LKP_INDEX_WAY_OFFS        6
129 #define MVPP2_CLS_LKP_TBL_REG                   0x1818
130 #define     MVPP2_CLS_LKP_TBL_RXQ_MASK          0xff
131 #define     MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK    BIT(25)
132 #define MVPP2_CLS_FLOW_INDEX_REG                0x1820
133 #define MVPP2_CLS_FLOW_TBL0_REG                 0x1824
134 #define MVPP2_CLS_FLOW_TBL1_REG                 0x1828
135 #define MVPP2_CLS_FLOW_TBL2_REG                 0x182c
136 #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port)    (0x1980 + ((port) * 4))
137 #define     MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS     3
138 #define     MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK     0x7
139 #define MVPP2_CLS_SWFWD_P2HQ_REG(port)          (0x19b0 + ((port) * 4))
140 #define MVPP2_CLS_SWFWD_PCTRL_REG               0x19d0
141 #define     MVPP2_CLS_SWFWD_PCTRL_MASK(port)    (1 << (port))
142
143 /* Descriptor Manager Top Registers */
144 #define MVPP2_RXQ_NUM_REG                       0x2040
145 #define MVPP2_RXQ_DESC_ADDR_REG                 0x2044
146 #define     MVPP22_DESC_ADDR_OFFS               8
147 #define MVPP2_RXQ_DESC_SIZE_REG                 0x2048
148 #define     MVPP2_RXQ_DESC_SIZE_MASK            0x3ff0
149 #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq)        (0x3000 + 4 * (rxq))
150 #define     MVPP2_RXQ_NUM_PROCESSED_OFFSET      0
151 #define     MVPP2_RXQ_NUM_NEW_OFFSET            16
152 #define MVPP2_RXQ_STATUS_REG(rxq)               (0x3400 + 4 * (rxq))
153 #define     MVPP2_RXQ_OCCUPIED_MASK             0x3fff
154 #define     MVPP2_RXQ_NON_OCCUPIED_OFFSET       16
155 #define     MVPP2_RXQ_NON_OCCUPIED_MASK         0x3fff0000
156 #define MVPP2_RXQ_THRESH_REG                    0x204c
157 #define     MVPP2_OCCUPIED_THRESH_OFFSET        0
158 #define     MVPP2_OCCUPIED_THRESH_MASK          0x3fff
159 #define MVPP2_RXQ_INDEX_REG                     0x2050
160 #define MVPP2_TXQ_NUM_REG                       0x2080
161 #define MVPP2_TXQ_DESC_ADDR_REG                 0x2084
162 #define MVPP2_TXQ_DESC_SIZE_REG                 0x2088
163 #define     MVPP2_TXQ_DESC_SIZE_MASK            0x3ff0
164 #define MVPP2_AGGR_TXQ_UPDATE_REG               0x2090
165 #define MVPP2_TXQ_THRESH_REG                    0x2094
166 #define     MVPP2_TRANSMITTED_THRESH_OFFSET     16
167 #define     MVPP2_TRANSMITTED_THRESH_MASK       0x3fff0000
168 #define MVPP2_TXQ_INDEX_REG                     0x2098
169 #define MVPP2_TXQ_PREF_BUF_REG                  0x209c
170 #define     MVPP2_PREF_BUF_PTR(desc)            ((desc) & 0xfff)
171 #define     MVPP2_PREF_BUF_SIZE_4               (BIT(12) | BIT(13))
172 #define     MVPP2_PREF_BUF_SIZE_16              (BIT(12) | BIT(14))
173 #define     MVPP2_PREF_BUF_THRESH(val)          ((val) << 17)
174 #define     MVPP2_TXQ_DRAIN_EN_MASK             BIT(31)
175 #define MVPP2_TXQ_PENDING_REG                   0x20a0
176 #define     MVPP2_TXQ_PENDING_MASK              0x3fff
177 #define MVPP2_TXQ_INT_STATUS_REG                0x20a4
178 #define MVPP2_TXQ_SENT_REG(txq)                 (0x3c00 + 4 * (txq))
179 #define     MVPP2_TRANSMITTED_COUNT_OFFSET      16
180 #define     MVPP2_TRANSMITTED_COUNT_MASK        0x3fff0000
181 #define MVPP2_TXQ_RSVD_REQ_REG                  0x20b0
182 #define     MVPP2_TXQ_RSVD_REQ_Q_OFFSET         16
183 #define MVPP2_TXQ_RSVD_RSLT_REG                 0x20b4
184 #define     MVPP2_TXQ_RSVD_RSLT_MASK            0x3fff
185 #define MVPP2_TXQ_RSVD_CLR_REG                  0x20b8
186 #define     MVPP2_TXQ_RSVD_CLR_OFFSET           16
187 #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu)       (0x2100 + 4 * (cpu))
188 #define     MVPP22_AGGR_TXQ_DESC_ADDR_OFFS      8
189 #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu)       (0x2140 + 4 * (cpu))
190 #define     MVPP2_AGGR_TXQ_DESC_SIZE_MASK       0x3ff0
191 #define MVPP2_AGGR_TXQ_STATUS_REG(cpu)          (0x2180 + 4 * (cpu))
192 #define     MVPP2_AGGR_TXQ_PENDING_MASK         0x3fff
193 #define MVPP2_AGGR_TXQ_INDEX_REG(cpu)           (0x21c0 + 4 * (cpu))
194
195 /* MBUS bridge registers */
196 #define MVPP2_WIN_BASE(w)                       (0x4000 + ((w) << 2))
197 #define MVPP2_WIN_SIZE(w)                       (0x4020 + ((w) << 2))
198 #define MVPP2_WIN_REMAP(w)                      (0x4040 + ((w) << 2))
199 #define MVPP2_BASE_ADDR_ENABLE                  0x4060
200
201 /* Interrupt Cause and Mask registers */
202 #define MVPP2_ISR_RX_THRESHOLD_REG(rxq)         (0x5200 + 4 * (rxq))
203 #define MVPP2_ISR_RXQ_GROUP_REG(rxq)            (0x5400 + 4 * (rxq))
204 #define MVPP2_ISR_ENABLE_REG(port)              (0x5420 + 4 * (port))
205 #define     MVPP2_ISR_ENABLE_INTERRUPT(mask)    ((mask) & 0xffff)
206 #define     MVPP2_ISR_DISABLE_INTERRUPT(mask)   (((mask) << 16) & 0xffff0000)
207 #define MVPP2_ISR_RX_TX_CAUSE_REG(port)         (0x5480 + 4 * (port))
208 #define     MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
209 #define     MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
210 #define     MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK    BIT(24)
211 #define     MVPP2_CAUSE_FCS_ERR_MASK            BIT(25)
212 #define     MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK   BIT(26)
213 #define     MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK   BIT(29)
214 #define     MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK   BIT(30)
215 #define     MVPP2_CAUSE_MISC_SUM_MASK           BIT(31)
216 #define MVPP2_ISR_RX_TX_MASK_REG(port)          (0x54a0 + 4 * (port))
217 #define MVPP2_ISR_PON_RX_TX_MASK_REG            0x54bc
218 #define     MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK     0xffff
219 #define     MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK     0x3fc00000
220 #define     MVPP2_PON_CAUSE_MISC_SUM_MASK               BIT(31)
221 #define MVPP2_ISR_MISC_CAUSE_REG                0x55b0
222
223 /* Buffer Manager registers */
224 #define MVPP2_BM_POOL_BASE_REG(pool)            (0x6000 + ((pool) * 4))
225 #define     MVPP2_BM_POOL_BASE_ADDR_MASK        0xfffff80
226 #define MVPP2_BM_POOL_SIZE_REG(pool)            (0x6040 + ((pool) * 4))
227 #define     MVPP2_BM_POOL_SIZE_MASK             0xfff0
228 #define MVPP2_BM_POOL_READ_PTR_REG(pool)        (0x6080 + ((pool) * 4))
229 #define     MVPP2_BM_POOL_GET_READ_PTR_MASK     0xfff0
230 #define MVPP2_BM_POOL_PTRS_NUM_REG(pool)        (0x60c0 + ((pool) * 4))
231 #define     MVPP2_BM_POOL_PTRS_NUM_MASK         0xfff0
232 #define MVPP2_BM_BPPI_READ_PTR_REG(pool)        (0x6100 + ((pool) * 4))
233 #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool)        (0x6140 + ((pool) * 4))
234 #define     MVPP2_BM_BPPI_PTR_NUM_MASK          0x7ff
235 #define     MVPP2_BM_BPPI_PREFETCH_FULL_MASK    BIT(16)
236 #define MVPP2_BM_POOL_CTRL_REG(pool)            (0x6200 + ((pool) * 4))
237 #define     MVPP2_BM_START_MASK                 BIT(0)
238 #define     MVPP2_BM_STOP_MASK                  BIT(1)
239 #define     MVPP2_BM_STATE_MASK                 BIT(4)
240 #define     MVPP2_BM_LOW_THRESH_OFFS            8
241 #define     MVPP2_BM_LOW_THRESH_MASK            0x7f00
242 #define     MVPP2_BM_LOW_THRESH_VALUE(val)      ((val) << \
243                                                 MVPP2_BM_LOW_THRESH_OFFS)
244 #define     MVPP2_BM_HIGH_THRESH_OFFS           16
245 #define     MVPP2_BM_HIGH_THRESH_MASK           0x7f0000
246 #define     MVPP2_BM_HIGH_THRESH_VALUE(val)     ((val) << \
247                                                 MVPP2_BM_HIGH_THRESH_OFFS)
248 #define MVPP2_BM_INTR_CAUSE_REG(pool)           (0x6240 + ((pool) * 4))
249 #define     MVPP2_BM_RELEASED_DELAY_MASK        BIT(0)
250 #define     MVPP2_BM_ALLOC_FAILED_MASK          BIT(1)
251 #define     MVPP2_BM_BPPE_EMPTY_MASK            BIT(2)
252 #define     MVPP2_BM_BPPE_FULL_MASK             BIT(3)
253 #define     MVPP2_BM_AVAILABLE_BP_LOW_MASK      BIT(4)
254 #define MVPP2_BM_INTR_MASK_REG(pool)            (0x6280 + ((pool) * 4))
255 #define MVPP2_BM_PHY_ALLOC_REG(pool)            (0x6400 + ((pool) * 4))
256 #define     MVPP2_BM_PHY_ALLOC_GRNTD_MASK       BIT(0)
257 #define MVPP2_BM_VIRT_ALLOC_REG                 0x6440
258 #define MVPP2_BM_ADDR_HIGH_ALLOC                0x6444
259 #define     MVPP2_BM_ADDR_HIGH_PHYS_MASK        0xff
260 #define     MVPP2_BM_ADDR_HIGH_VIRT_MASK        0xff00
261 #define     MVPP2_BM_ADDR_HIGH_VIRT_SHIFT       8
262 #define MVPP2_BM_PHY_RLS_REG(pool)              (0x6480 + ((pool) * 4))
263 #define     MVPP2_BM_PHY_RLS_MC_BUFF_MASK       BIT(0)
264 #define     MVPP2_BM_PHY_RLS_PRIO_EN_MASK       BIT(1)
265 #define     MVPP2_BM_PHY_RLS_GRNTD_MASK         BIT(2)
266 #define MVPP2_BM_VIRT_RLS_REG                   0x64c0
267 #define MVPP21_BM_MC_RLS_REG                    0x64c4
268 #define     MVPP2_BM_MC_ID_MASK                 0xfff
269 #define     MVPP2_BM_FORCE_RELEASE_MASK         BIT(12)
270 #define MVPP22_BM_ADDR_HIGH_RLS_REG             0x64c4
271 #define     MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK   0xff
272 #define     MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK   0xff00
273 #define     MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT  8
274 #define MVPP22_BM_MC_RLS_REG                    0x64d4
275
276 /* TX Scheduler registers */
277 #define MVPP2_TXP_SCHED_PORT_INDEX_REG          0x8000
278 #define MVPP2_TXP_SCHED_Q_CMD_REG               0x8004
279 #define     MVPP2_TXP_SCHED_ENQ_MASK            0xff
280 #define     MVPP2_TXP_SCHED_DISQ_OFFSET         8
281 #define MVPP2_TXP_SCHED_CMD_1_REG               0x8010
282 #define MVPP2_TXP_SCHED_PERIOD_REG              0x8018
283 #define MVPP2_TXP_SCHED_MTU_REG                 0x801c
284 #define     MVPP2_TXP_MTU_MAX                   0x7FFFF
285 #define MVPP2_TXP_SCHED_REFILL_REG              0x8020
286 #define     MVPP2_TXP_REFILL_TOKENS_ALL_MASK    0x7ffff
287 #define     MVPP2_TXP_REFILL_PERIOD_ALL_MASK    0x3ff00000
288 #define     MVPP2_TXP_REFILL_PERIOD_MASK(v)     ((v) << 20)
289 #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG          0x8024
290 #define     MVPP2_TXP_TOKEN_SIZE_MAX            0xffffffff
291 #define MVPP2_TXQ_SCHED_REFILL_REG(q)           (0x8040 + ((q) << 2))
292 #define     MVPP2_TXQ_REFILL_TOKENS_ALL_MASK    0x7ffff
293 #define     MVPP2_TXQ_REFILL_PERIOD_ALL_MASK    0x3ff00000
294 #define     MVPP2_TXQ_REFILL_PERIOD_MASK(v)     ((v) << 20)
295 #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q)       (0x8060 + ((q) << 2))
296 #define     MVPP2_TXQ_TOKEN_SIZE_MAX            0x7fffffff
297 #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q)       (0x8080 + ((q) << 2))
298 #define     MVPP2_TXQ_TOKEN_CNTR_MAX            0xffffffff
299
300 /* TX general registers */
301 #define MVPP2_TX_SNOOP_REG                      0x8800
302 #define MVPP2_TX_PORT_FLUSH_REG                 0x8810
303 #define     MVPP2_TX_PORT_FLUSH_MASK(port)      (1 << (port))
304
305 /* LMS registers */
306 #define MVPP2_SRC_ADDR_MIDDLE                   0x24
307 #define MVPP2_SRC_ADDR_HIGH                     0x28
308 #define MVPP2_PHY_AN_CFG0_REG                   0x34
309 #define     MVPP2_PHY_AN_STOP_SMI0_MASK         BIT(7)
310 #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG      0x305c
311 #define     MVPP2_EXT_GLOBAL_CTRL_DEFAULT       0x27
312
313 /* Per-port registers */
314 #define MVPP2_GMAC_CTRL_0_REG                   0x0
315 #define      MVPP2_GMAC_PORT_EN_MASK            BIT(0)
316 #define      MVPP2_GMAC_MAX_RX_SIZE_OFFS        2
317 #define      MVPP2_GMAC_MAX_RX_SIZE_MASK        0x7ffc
318 #define      MVPP2_GMAC_MIB_CNTR_EN_MASK        BIT(15)
319 #define MVPP2_GMAC_CTRL_1_REG                   0x4
320 #define      MVPP2_GMAC_PERIODIC_XON_EN_MASK    BIT(1)
321 #define      MVPP2_GMAC_GMII_LB_EN_MASK         BIT(5)
322 #define      MVPP2_GMAC_PCS_LB_EN_BIT           6
323 #define      MVPP2_GMAC_PCS_LB_EN_MASK          BIT(6)
324 #define      MVPP2_GMAC_SA_LOW_OFFS             7
325 #define MVPP2_GMAC_CTRL_2_REG                   0x8
326 #define      MVPP2_GMAC_INBAND_AN_MASK          BIT(0)
327 #define      MVPP2_GMAC_PCS_ENABLE_MASK         BIT(3)
328 #define      MVPP2_GMAC_PORT_RGMII_MASK         BIT(4)
329 #define      MVPP2_GMAC_PORT_RESET_MASK         BIT(6)
330 #define MVPP2_GMAC_AUTONEG_CONFIG               0xc
331 #define      MVPP2_GMAC_FORCE_LINK_DOWN         BIT(0)
332 #define      MVPP2_GMAC_FORCE_LINK_PASS         BIT(1)
333 #define      MVPP2_GMAC_CONFIG_MII_SPEED        BIT(5)
334 #define      MVPP2_GMAC_CONFIG_GMII_SPEED       BIT(6)
335 #define      MVPP2_GMAC_AN_SPEED_EN             BIT(7)
336 #define      MVPP2_GMAC_FC_ADV_EN               BIT(9)
337 #define      MVPP2_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
338 #define      MVPP2_GMAC_AN_DUPLEX_EN            BIT(13)
339 #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG          0x1c
340 #define      MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS     6
341 #define      MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
342 #define      MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v)  (((v) << 6) & \
343                                         MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
344
345 #define MVPP22_PORT_BASE                        0x30e00
346 #define MVPP22_PORT_OFFSET                      0x1000
347
348 #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK      0xff
349
350 /* Descriptor ring Macros */
351 #define MVPP2_QUEUE_NEXT_DESC(q, index) \
352         (((index) < (q)->last_desc) ? ((index) + 1) : 0)
353
354 /* SMI: 0xc0054 -> offset 0x54 to lms_base */
355 #define MVPP2_SMI                               0x0054
356 #define     MVPP2_PHY_REG_MASK                  0x1f
357 /* SMI register fields */
358 #define     MVPP2_SMI_DATA_OFFS                 0       /* Data */
359 #define     MVPP2_SMI_DATA_MASK                 (0xffff << MVPP2_SMI_DATA_OFFS)
360 #define     MVPP2_SMI_DEV_ADDR_OFFS             16      /* PHY device address */
361 #define     MVPP2_SMI_REG_ADDR_OFFS             21      /* PHY device reg addr*/
362 #define     MVPP2_SMI_OPCODE_OFFS               26      /* Write/Read opcode */
363 #define     MVPP2_SMI_OPCODE_READ               (1 << MVPP2_SMI_OPCODE_OFFS)
364 #define     MVPP2_SMI_READ_VALID                (1 << 27)       /* Read Valid */
365 #define     MVPP2_SMI_BUSY                      (1 << 28)       /* Busy */
366
367 #define     MVPP2_PHY_ADDR_MASK                 0x1f
368 #define     MVPP2_PHY_REG_MASK                  0x1f
369
370 /* Various constants */
371
372 /* Coalescing */
373 #define MVPP2_TXDONE_COAL_PKTS_THRESH   15
374 #define MVPP2_TXDONE_HRTIMER_PERIOD_NS  1000000UL
375 #define MVPP2_RX_COAL_PKTS              32
376 #define MVPP2_RX_COAL_USEC              100
377
378 /* The two bytes Marvell header. Either contains a special value used
379  * by Marvell switches when a specific hardware mode is enabled (not
380  * supported by this driver) or is filled automatically by zeroes on
381  * the RX side. Those two bytes being at the front of the Ethernet
382  * header, they allow to have the IP header aligned on a 4 bytes
383  * boundary automatically: the hardware skips those two bytes on its
384  * own.
385  */
386 #define MVPP2_MH_SIZE                   2
387 #define MVPP2_ETH_TYPE_LEN              2
388 #define MVPP2_PPPOE_HDR_SIZE            8
389 #define MVPP2_VLAN_TAG_LEN              4
390
391 /* Lbtd 802.3 type */
392 #define MVPP2_IP_LBDT_TYPE              0xfffa
393
394 #define MVPP2_CPU_D_CACHE_LINE_SIZE     32
395 #define MVPP2_TX_CSUM_MAX_SIZE          9800
396
397 /* Timeout constants */
398 #define MVPP2_TX_DISABLE_TIMEOUT_MSEC   1000
399 #define MVPP2_TX_PENDING_TIMEOUT_MSEC   1000
400
401 #define MVPP2_TX_MTU_MAX                0x7ffff
402
403 /* Maximum number of T-CONTs of PON port */
404 #define MVPP2_MAX_TCONT                 16
405
406 /* Maximum number of supported ports */
407 #define MVPP2_MAX_PORTS                 4
408
409 /* Maximum number of TXQs used by single port */
410 #define MVPP2_MAX_TXQ                   8
411
412 /* Maximum number of RXQs used by single port */
413 #define MVPP2_MAX_RXQ                   8
414
415 /* Default number of TXQs in use */
416 #define MVPP2_DEFAULT_TXQ               1
417
418 /* Dfault number of RXQs in use */
419 #define MVPP2_DEFAULT_RXQ               1
420 #define CONFIG_MV_ETH_RXQ               8       /* increment by 8 */
421
422 /* Total number of RXQs available to all ports */
423 #define MVPP2_RXQ_TOTAL_NUM             (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
424
425 /* Max number of Rx descriptors */
426 #define MVPP2_MAX_RXD                   16
427
428 /* Max number of Tx descriptors */
429 #define MVPP2_MAX_TXD                   16
430
431 /* Amount of Tx descriptors that can be reserved at once by CPU */
432 #define MVPP2_CPU_DESC_CHUNK            64
433
434 /* Max number of Tx descriptors in each aggregated queue */
435 #define MVPP2_AGGR_TXQ_SIZE             256
436
437 /* Descriptor aligned size */
438 #define MVPP2_DESC_ALIGNED_SIZE         32
439
440 /* Descriptor alignment mask */
441 #define MVPP2_TX_DESC_ALIGN             (MVPP2_DESC_ALIGNED_SIZE - 1)
442
443 /* RX FIFO constants */
444 #define MVPP2_RX_FIFO_PORT_DATA_SIZE    0x2000
445 #define MVPP2_RX_FIFO_PORT_ATTR_SIZE    0x80
446 #define MVPP2_RX_FIFO_PORT_MIN_PKT      0x80
447
448 /* RX buffer constants */
449 #define MVPP2_SKB_SHINFO_SIZE \
450         0
451
452 #define MVPP2_RX_PKT_SIZE(mtu) \
453         ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
454               ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
455
456 #define MVPP2_RX_BUF_SIZE(pkt_size)     ((pkt_size) + NET_SKB_PAD)
457 #define MVPP2_RX_TOTAL_SIZE(buf_size)   ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
458 #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
459         ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
460
461 #define MVPP2_BIT_TO_BYTE(bit)          ((bit) / 8)
462
463 /* IPv6 max L3 address size */
464 #define MVPP2_MAX_L3_ADDR_SIZE          16
465
466 /* Port flags */
467 #define MVPP2_F_LOOPBACK                BIT(0)
468
469 /* Marvell tag types */
470 enum mvpp2_tag_type {
471         MVPP2_TAG_TYPE_NONE = 0,
472         MVPP2_TAG_TYPE_MH   = 1,
473         MVPP2_TAG_TYPE_DSA  = 2,
474         MVPP2_TAG_TYPE_EDSA = 3,
475         MVPP2_TAG_TYPE_VLAN = 4,
476         MVPP2_TAG_TYPE_LAST = 5
477 };
478
479 /* Parser constants */
480 #define MVPP2_PRS_TCAM_SRAM_SIZE        256
481 #define MVPP2_PRS_TCAM_WORDS            6
482 #define MVPP2_PRS_SRAM_WORDS            4
483 #define MVPP2_PRS_FLOW_ID_SIZE          64
484 #define MVPP2_PRS_FLOW_ID_MASK          0x3f
485 #define MVPP2_PRS_TCAM_ENTRY_INVALID    1
486 #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT   BIT(5)
487 #define MVPP2_PRS_IPV4_HEAD             0x40
488 #define MVPP2_PRS_IPV4_HEAD_MASK        0xf0
489 #define MVPP2_PRS_IPV4_MC               0xe0
490 #define MVPP2_PRS_IPV4_MC_MASK          0xf0
491 #define MVPP2_PRS_IPV4_BC_MASK          0xff
492 #define MVPP2_PRS_IPV4_IHL              0x5
493 #define MVPP2_PRS_IPV4_IHL_MASK         0xf
494 #define MVPP2_PRS_IPV6_MC               0xff
495 #define MVPP2_PRS_IPV6_MC_MASK          0xff
496 #define MVPP2_PRS_IPV6_HOP_MASK         0xff
497 #define MVPP2_PRS_TCAM_PROTO_MASK       0xff
498 #define MVPP2_PRS_TCAM_PROTO_MASK_L     0x3f
499 #define MVPP2_PRS_DBL_VLANS_MAX         100
500
501 /* Tcam structure:
502  * - lookup ID - 4 bits
503  * - port ID - 1 byte
504  * - additional information - 1 byte
505  * - header data - 8 bytes
506  * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
507  */
508 #define MVPP2_PRS_AI_BITS                       8
509 #define MVPP2_PRS_PORT_MASK                     0xff
510 #define MVPP2_PRS_LU_MASK                       0xf
511 #define MVPP2_PRS_TCAM_DATA_BYTE(offs)          \
512                                     (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
513 #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)       \
514                                               (((offs) * 2) - ((offs) % 2)  + 2)
515 #define MVPP2_PRS_TCAM_AI_BYTE                  16
516 #define MVPP2_PRS_TCAM_PORT_BYTE                17
517 #define MVPP2_PRS_TCAM_LU_BYTE                  20
518 #define MVPP2_PRS_TCAM_EN_OFFS(offs)            ((offs) + 2)
519 #define MVPP2_PRS_TCAM_INV_WORD                 5
520 /* Tcam entries ID */
521 #define MVPP2_PE_DROP_ALL               0
522 #define MVPP2_PE_FIRST_FREE_TID         1
523 #define MVPP2_PE_LAST_FREE_TID          (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
524 #define MVPP2_PE_IP6_EXT_PROTO_UN       (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
525 #define MVPP2_PE_MAC_MC_IP6             (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
526 #define MVPP2_PE_IP6_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
527 #define MVPP2_PE_IP4_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
528 #define MVPP2_PE_LAST_DEFAULT_FLOW      (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
529 #define MVPP2_PE_FIRST_DEFAULT_FLOW     (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
530 #define MVPP2_PE_EDSA_TAGGED            (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
531 #define MVPP2_PE_EDSA_UNTAGGED          (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
532 #define MVPP2_PE_DSA_TAGGED             (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
533 #define MVPP2_PE_DSA_UNTAGGED           (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
534 #define MVPP2_PE_ETYPE_EDSA_TAGGED      (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
535 #define MVPP2_PE_ETYPE_EDSA_UNTAGGED    (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
536 #define MVPP2_PE_ETYPE_DSA_TAGGED       (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
537 #define MVPP2_PE_ETYPE_DSA_UNTAGGED     (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
538 #define MVPP2_PE_MH_DEFAULT             (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
539 #define MVPP2_PE_DSA_DEFAULT            (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
540 #define MVPP2_PE_IP6_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
541 #define MVPP2_PE_IP4_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
542 #define MVPP2_PE_ETH_TYPE_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
543 #define MVPP2_PE_VLAN_DBL               (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
544 #define MVPP2_PE_VLAN_NONE              (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
545 #define MVPP2_PE_MAC_MC_ALL             (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
546 #define MVPP2_PE_MAC_PROMISCUOUS        (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
547 #define MVPP2_PE_MAC_NON_PROMISCUOUS    (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
548
549 /* Sram structure
550  * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
551  */
552 #define MVPP2_PRS_SRAM_RI_OFFS                  0
553 #define MVPP2_PRS_SRAM_RI_WORD                  0
554 #define MVPP2_PRS_SRAM_RI_CTRL_OFFS             32
555 #define MVPP2_PRS_SRAM_RI_CTRL_WORD             1
556 #define MVPP2_PRS_SRAM_RI_CTRL_BITS             32
557 #define MVPP2_PRS_SRAM_SHIFT_OFFS               64
558 #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT           72
559 #define MVPP2_PRS_SRAM_UDF_OFFS                 73
560 #define MVPP2_PRS_SRAM_UDF_BITS                 8
561 #define MVPP2_PRS_SRAM_UDF_MASK                 0xff
562 #define MVPP2_PRS_SRAM_UDF_SIGN_BIT             81
563 #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS            82
564 #define MVPP2_PRS_SRAM_UDF_TYPE_MASK            0x7
565 #define MVPP2_PRS_SRAM_UDF_TYPE_L3              1
566 #define MVPP2_PRS_SRAM_UDF_TYPE_L4              4
567 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS        85
568 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK        0x3
569 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD         1
570 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD     2
571 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD     3
572 #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS          87
573 #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS          2
574 #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK          0x3
575 #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD           0
576 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD       2
577 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD       3
578 #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS         89
579 #define MVPP2_PRS_SRAM_AI_OFFS                  90
580 #define MVPP2_PRS_SRAM_AI_CTRL_OFFS             98
581 #define MVPP2_PRS_SRAM_AI_CTRL_BITS             8
582 #define MVPP2_PRS_SRAM_AI_MASK                  0xff
583 #define MVPP2_PRS_SRAM_NEXT_LU_OFFS             106
584 #define MVPP2_PRS_SRAM_NEXT_LU_MASK             0xf
585 #define MVPP2_PRS_SRAM_LU_DONE_BIT              110
586 #define MVPP2_PRS_SRAM_LU_GEN_BIT               111
587
588 /* Sram result info bits assignment */
589 #define MVPP2_PRS_RI_MAC_ME_MASK                0x1
590 #define MVPP2_PRS_RI_DSA_MASK                   0x2
591 #define MVPP2_PRS_RI_VLAN_MASK                  (BIT(2) | BIT(3))
592 #define MVPP2_PRS_RI_VLAN_NONE                  0x0
593 #define MVPP2_PRS_RI_VLAN_SINGLE                BIT(2)
594 #define MVPP2_PRS_RI_VLAN_DOUBLE                BIT(3)
595 #define MVPP2_PRS_RI_VLAN_TRIPLE                (BIT(2) | BIT(3))
596 #define MVPP2_PRS_RI_CPU_CODE_MASK              0x70
597 #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC           BIT(4)
598 #define MVPP2_PRS_RI_L2_CAST_MASK               (BIT(9) | BIT(10))
599 #define MVPP2_PRS_RI_L2_UCAST                   0x0
600 #define MVPP2_PRS_RI_L2_MCAST                   BIT(9)
601 #define MVPP2_PRS_RI_L2_BCAST                   BIT(10)
602 #define MVPP2_PRS_RI_PPPOE_MASK                 0x800
603 #define MVPP2_PRS_RI_L3_PROTO_MASK              (BIT(12) | BIT(13) | BIT(14))
604 #define MVPP2_PRS_RI_L3_UN                      0x0
605 #define MVPP2_PRS_RI_L3_IP4                     BIT(12)
606 #define MVPP2_PRS_RI_L3_IP4_OPT                 BIT(13)
607 #define MVPP2_PRS_RI_L3_IP4_OTHER               (BIT(12) | BIT(13))
608 #define MVPP2_PRS_RI_L3_IP6                     BIT(14)
609 #define MVPP2_PRS_RI_L3_IP6_EXT                 (BIT(12) | BIT(14))
610 #define MVPP2_PRS_RI_L3_ARP                     (BIT(13) | BIT(14))
611 #define MVPP2_PRS_RI_L3_ADDR_MASK               (BIT(15) | BIT(16))
612 #define MVPP2_PRS_RI_L3_UCAST                   0x0
613 #define MVPP2_PRS_RI_L3_MCAST                   BIT(15)
614 #define MVPP2_PRS_RI_L3_BCAST                   (BIT(15) | BIT(16))
615 #define MVPP2_PRS_RI_IP_FRAG_MASK               0x20000
616 #define MVPP2_PRS_RI_UDF3_MASK                  0x300000
617 #define MVPP2_PRS_RI_UDF3_RX_SPECIAL            BIT(21)
618 #define MVPP2_PRS_RI_L4_PROTO_MASK              0x1c00000
619 #define MVPP2_PRS_RI_L4_TCP                     BIT(22)
620 #define MVPP2_PRS_RI_L4_UDP                     BIT(23)
621 #define MVPP2_PRS_RI_L4_OTHER                   (BIT(22) | BIT(23))
622 #define MVPP2_PRS_RI_UDF7_MASK                  0x60000000
623 #define MVPP2_PRS_RI_UDF7_IP6_LITE              BIT(29)
624 #define MVPP2_PRS_RI_DROP_MASK                  0x80000000
625
626 /* Sram additional info bits assignment */
627 #define MVPP2_PRS_IPV4_DIP_AI_BIT               BIT(0)
628 #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT            BIT(0)
629 #define MVPP2_PRS_IPV6_EXT_AI_BIT               BIT(1)
630 #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT            BIT(2)
631 #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT        BIT(3)
632 #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT         BIT(4)
633 #define MVPP2_PRS_SINGLE_VLAN_AI                0
634 #define MVPP2_PRS_DBL_VLAN_AI_BIT               BIT(7)
635
636 /* DSA/EDSA type */
637 #define MVPP2_PRS_TAGGED                true
638 #define MVPP2_PRS_UNTAGGED              false
639 #define MVPP2_PRS_EDSA                  true
640 #define MVPP2_PRS_DSA                   false
641
642 /* MAC entries, shadow udf */
643 enum mvpp2_prs_udf {
644         MVPP2_PRS_UDF_MAC_DEF,
645         MVPP2_PRS_UDF_MAC_RANGE,
646         MVPP2_PRS_UDF_L2_DEF,
647         MVPP2_PRS_UDF_L2_DEF_COPY,
648         MVPP2_PRS_UDF_L2_USER,
649 };
650
651 /* Lookup ID */
652 enum mvpp2_prs_lookup {
653         MVPP2_PRS_LU_MH,
654         MVPP2_PRS_LU_MAC,
655         MVPP2_PRS_LU_DSA,
656         MVPP2_PRS_LU_VLAN,
657         MVPP2_PRS_LU_L2,
658         MVPP2_PRS_LU_PPPOE,
659         MVPP2_PRS_LU_IP4,
660         MVPP2_PRS_LU_IP6,
661         MVPP2_PRS_LU_FLOWS,
662         MVPP2_PRS_LU_LAST,
663 };
664
665 /* L3 cast enum */
666 enum mvpp2_prs_l3_cast {
667         MVPP2_PRS_L3_UNI_CAST,
668         MVPP2_PRS_L3_MULTI_CAST,
669         MVPP2_PRS_L3_BROAD_CAST
670 };
671
672 /* Classifier constants */
673 #define MVPP2_CLS_FLOWS_TBL_SIZE        512
674 #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS  3
675 #define MVPP2_CLS_LKP_TBL_SIZE          64
676
677 /* BM constants */
678 #define MVPP2_BM_POOLS_NUM              1
679 #define MVPP2_BM_LONG_BUF_NUM           16
680 #define MVPP2_BM_SHORT_BUF_NUM          16
681 #define MVPP2_BM_POOL_SIZE_MAX          (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
682 #define MVPP2_BM_POOL_PTR_ALIGN         128
683 #define MVPP2_BM_SWF_LONG_POOL(port)    0
684
685 /* BM cookie (32 bits) definition */
686 #define MVPP2_BM_COOKIE_POOL_OFFS       8
687 #define MVPP2_BM_COOKIE_CPU_OFFS        24
688
689 /* BM short pool packet size
690  * These value assure that for SWF the total number
691  * of bytes allocated for each buffer will be 512
692  */
693 #define MVPP2_BM_SHORT_PKT_SIZE         MVPP2_RX_MAX_PKT_SIZE(512)
694
695 enum mvpp2_bm_type {
696         MVPP2_BM_FREE,
697         MVPP2_BM_SWF_LONG,
698         MVPP2_BM_SWF_SHORT
699 };
700
701 /* Definitions */
702
703 /* Shared Packet Processor resources */
704 struct mvpp2 {
705         /* Shared registers' base addresses */
706         void __iomem *base;
707         void __iomem *lms_base;
708         void __iomem *iface_base;
709
710         /* List of pointers to port structures */
711         struct mvpp2_port **port_list;
712
713         /* Aggregated TXQs */
714         struct mvpp2_tx_queue *aggr_txqs;
715
716         /* BM pools */
717         struct mvpp2_bm_pool *bm_pools;
718
719         /* PRS shadow table */
720         struct mvpp2_prs_shadow *prs_shadow;
721         /* PRS auxiliary table for double vlan entries control */
722         bool *prs_double_vlans;
723
724         /* Tclk value */
725         u32 tclk;
726
727         /* HW version */
728         enum { MVPP21, MVPP22 } hw_version;
729
730         struct mii_dev *bus;
731 };
732
733 struct mvpp2_pcpu_stats {
734         u64     rx_packets;
735         u64     rx_bytes;
736         u64     tx_packets;
737         u64     tx_bytes;
738 };
739
740 struct mvpp2_port {
741         u8 id;
742
743         /* Index of the port from the "group of ports" complex point
744          * of view
745          */
746         int gop_id;
747
748         int irq;
749
750         struct mvpp2 *priv;
751
752         /* Per-port registers' base address */
753         void __iomem *base;
754
755         struct mvpp2_rx_queue **rxqs;
756         struct mvpp2_tx_queue **txqs;
757
758         int pkt_size;
759
760         u32 pending_cause_rx;
761
762         /* Per-CPU port control */
763         struct mvpp2_port_pcpu __percpu *pcpu;
764
765         /* Flags */
766         unsigned long flags;
767
768         u16 tx_ring_size;
769         u16 rx_ring_size;
770         struct mvpp2_pcpu_stats __percpu *stats;
771
772         struct phy_device *phy_dev;
773         phy_interface_t phy_interface;
774         int phy_node;
775         int phyaddr;
776         int init;
777         unsigned int link;
778         unsigned int duplex;
779         unsigned int speed;
780
781         struct mvpp2_bm_pool *pool_long;
782         struct mvpp2_bm_pool *pool_short;
783
784         /* Index of first port's physical RXQ */
785         u8 first_rxq;
786
787         u8 dev_addr[ETH_ALEN];
788 };
789
790 /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
791  * layout of the transmit and reception DMA descriptors, and their
792  * layout is therefore defined by the hardware design
793  */
794
795 #define MVPP2_TXD_L3_OFF_SHIFT          0
796 #define MVPP2_TXD_IP_HLEN_SHIFT         8
797 #define MVPP2_TXD_L4_CSUM_FRAG          BIT(13)
798 #define MVPP2_TXD_L4_CSUM_NOT           BIT(14)
799 #define MVPP2_TXD_IP_CSUM_DISABLE       BIT(15)
800 #define MVPP2_TXD_PADDING_DISABLE       BIT(23)
801 #define MVPP2_TXD_L4_UDP                BIT(24)
802 #define MVPP2_TXD_L3_IP6                BIT(26)
803 #define MVPP2_TXD_L_DESC                BIT(28)
804 #define MVPP2_TXD_F_DESC                BIT(29)
805
806 #define MVPP2_RXD_ERR_SUMMARY           BIT(15)
807 #define MVPP2_RXD_ERR_CODE_MASK         (BIT(13) | BIT(14))
808 #define MVPP2_RXD_ERR_CRC               0x0
809 #define MVPP2_RXD_ERR_OVERRUN           BIT(13)
810 #define MVPP2_RXD_ERR_RESOURCE          (BIT(13) | BIT(14))
811 #define MVPP2_RXD_BM_POOL_ID_OFFS       16
812 #define MVPP2_RXD_BM_POOL_ID_MASK       (BIT(16) | BIT(17) | BIT(18))
813 #define MVPP2_RXD_HWF_SYNC              BIT(21)
814 #define MVPP2_RXD_L4_CSUM_OK            BIT(22)
815 #define MVPP2_RXD_IP4_HEADER_ERR        BIT(24)
816 #define MVPP2_RXD_L4_TCP                BIT(25)
817 #define MVPP2_RXD_L4_UDP                BIT(26)
818 #define MVPP2_RXD_L3_IP4                BIT(28)
819 #define MVPP2_RXD_L3_IP6                BIT(30)
820 #define MVPP2_RXD_BUF_HDR               BIT(31)
821
822 /* HW TX descriptor for PPv2.1 */
823 struct mvpp21_tx_desc {
824         u32 command;            /* Options used by HW for packet transmitting.*/
825         u8  packet_offset;      /* the offset from the buffer beginning */
826         u8  phys_txq;           /* destination queue ID                 */
827         u16 data_size;          /* data size of transmitted packet in bytes */
828         u32 buf_dma_addr;       /* physical addr of transmitted buffer  */
829         u32 buf_cookie;         /* cookie for access to TX buffer in tx path */
830         u32 reserved1[3];       /* hw_cmd (for future use, BM, PON, PNC) */
831         u32 reserved2;          /* reserved (for future use)            */
832 };
833
834 /* HW RX descriptor for PPv2.1 */
835 struct mvpp21_rx_desc {
836         u32 status;             /* info about received packet           */
837         u16 reserved1;          /* parser_info (for future use, PnC)    */
838         u16 data_size;          /* size of received packet in bytes     */
839         u32 buf_dma_addr;       /* physical address of the buffer       */
840         u32 buf_cookie;         /* cookie for access to RX buffer in rx path */
841         u16 reserved2;          /* gem_port_id (for future use, PON)    */
842         u16 reserved3;          /* csum_l4 (for future use, PnC)        */
843         u8  reserved4;          /* bm_qset (for future use, BM)         */
844         u8  reserved5;
845         u16 reserved6;          /* classify_info (for future use, PnC)  */
846         u32 reserved7;          /* flow_id (for future use, PnC) */
847         u32 reserved8;
848 };
849
850 /* HW TX descriptor for PPv2.2 */
851 struct mvpp22_tx_desc {
852         u32 command;
853         u8  packet_offset;
854         u8  phys_txq;
855         u16 data_size;
856         u64 reserved1;
857         u64 buf_dma_addr_ptp;
858         u64 buf_cookie_misc;
859 };
860
861 /* HW RX descriptor for PPv2.2 */
862 struct mvpp22_rx_desc {
863         u32 status;
864         u16 reserved1;
865         u16 data_size;
866         u32 reserved2;
867         u32 reserved3;
868         u64 buf_dma_addr_key_hash;
869         u64 buf_cookie_misc;
870 };
871
872 /* Opaque type used by the driver to manipulate the HW TX and RX
873  * descriptors
874  */
875 struct mvpp2_tx_desc {
876         union {
877                 struct mvpp21_tx_desc pp21;
878                 struct mvpp22_tx_desc pp22;
879         };
880 };
881
882 struct mvpp2_rx_desc {
883         union {
884                 struct mvpp21_rx_desc pp21;
885                 struct mvpp22_rx_desc pp22;
886         };
887 };
888
889 /* Per-CPU Tx queue control */
890 struct mvpp2_txq_pcpu {
891         int cpu;
892
893         /* Number of Tx DMA descriptors in the descriptor ring */
894         int size;
895
896         /* Number of currently used Tx DMA descriptor in the
897          * descriptor ring
898          */
899         int count;
900
901         /* Number of Tx DMA descriptors reserved for each CPU */
902         int reserved_num;
903
904         /* Index of last TX DMA descriptor that was inserted */
905         int txq_put_index;
906
907         /* Index of the TX DMA descriptor to be cleaned up */
908         int txq_get_index;
909 };
910
911 struct mvpp2_tx_queue {
912         /* Physical number of this Tx queue */
913         u8 id;
914
915         /* Logical number of this Tx queue */
916         u8 log_id;
917
918         /* Number of Tx DMA descriptors in the descriptor ring */
919         int size;
920
921         /* Number of currently used Tx DMA descriptor in the descriptor ring */
922         int count;
923
924         /* Per-CPU control of physical Tx queues */
925         struct mvpp2_txq_pcpu __percpu *pcpu;
926
927         u32 done_pkts_coal;
928
929         /* Virtual address of thex Tx DMA descriptors array */
930         struct mvpp2_tx_desc *descs;
931
932         /* DMA address of the Tx DMA descriptors array */
933         dma_addr_t descs_dma;
934
935         /* Index of the last Tx DMA descriptor */
936         int last_desc;
937
938         /* Index of the next Tx DMA descriptor to process */
939         int next_desc_to_proc;
940 };
941
942 struct mvpp2_rx_queue {
943         /* RX queue number, in the range 0-31 for physical RXQs */
944         u8 id;
945
946         /* Num of rx descriptors in the rx descriptor ring */
947         int size;
948
949         u32 pkts_coal;
950         u32 time_coal;
951
952         /* Virtual address of the RX DMA descriptors array */
953         struct mvpp2_rx_desc *descs;
954
955         /* DMA address of the RX DMA descriptors array */
956         dma_addr_t descs_dma;
957
958         /* Index of the last RX DMA descriptor */
959         int last_desc;
960
961         /* Index of the next RX DMA descriptor to process */
962         int next_desc_to_proc;
963
964         /* ID of port to which physical RXQ is mapped */
965         int port;
966
967         /* Port's logic RXQ number to which physical RXQ is mapped */
968         int logic_rxq;
969 };
970
971 union mvpp2_prs_tcam_entry {
972         u32 word[MVPP2_PRS_TCAM_WORDS];
973         u8  byte[MVPP2_PRS_TCAM_WORDS * 4];
974 };
975
976 union mvpp2_prs_sram_entry {
977         u32 word[MVPP2_PRS_SRAM_WORDS];
978         u8  byte[MVPP2_PRS_SRAM_WORDS * 4];
979 };
980
981 struct mvpp2_prs_entry {
982         u32 index;
983         union mvpp2_prs_tcam_entry tcam;
984         union mvpp2_prs_sram_entry sram;
985 };
986
987 struct mvpp2_prs_shadow {
988         bool valid;
989         bool finish;
990
991         /* Lookup ID */
992         int lu;
993
994         /* User defined offset */
995         int udf;
996
997         /* Result info */
998         u32 ri;
999         u32 ri_mask;
1000 };
1001
1002 struct mvpp2_cls_flow_entry {
1003         u32 index;
1004         u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1005 };
1006
1007 struct mvpp2_cls_lookup_entry {
1008         u32 lkpid;
1009         u32 way;
1010         u32 data;
1011 };
1012
1013 struct mvpp2_bm_pool {
1014         /* Pool number in the range 0-7 */
1015         int id;
1016         enum mvpp2_bm_type type;
1017
1018         /* Buffer Pointers Pool External (BPPE) size */
1019         int size;
1020         /* Number of buffers for this pool */
1021         int buf_num;
1022         /* Pool buffer size */
1023         int buf_size;
1024         /* Packet size */
1025         int pkt_size;
1026
1027         /* BPPE virtual base address */
1028         unsigned long *virt_addr;
1029         /* BPPE DMA base address */
1030         dma_addr_t dma_addr;
1031
1032         /* Ports using BM pool */
1033         u32 port_map;
1034
1035         /* Occupied buffers indicator */
1036         int in_use_thresh;
1037 };
1038
1039 /* Static declaractions */
1040
1041 /* Number of RXQs used by single port */
1042 static int rxq_number = MVPP2_DEFAULT_RXQ;
1043 /* Number of TXQs used by single port */
1044 static int txq_number = MVPP2_DEFAULT_TXQ;
1045
1046 #define MVPP2_DRIVER_NAME "mvpp2"
1047 #define MVPP2_DRIVER_VERSION "1.0"
1048
1049 /*
1050  * U-Boot internal data, mostly uncached buffers for descriptors and data
1051  */
1052 struct buffer_location {
1053         struct mvpp2_tx_desc *aggr_tx_descs;
1054         struct mvpp2_tx_desc *tx_descs;
1055         struct mvpp2_rx_desc *rx_descs;
1056         unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
1057         unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
1058         int first_rxq;
1059 };
1060
1061 /*
1062  * All 4 interfaces use the same global buffer, since only one interface
1063  * can be enabled at once
1064  */
1065 static struct buffer_location buffer_loc;
1066
1067 /*
1068  * Page table entries are set to 1MB, or multiples of 1MB
1069  * (not < 1MB). driver uses less bd's so use 1MB bdspace.
1070  */
1071 #define BD_SPACE        (1 << 20)
1072
1073 /* Utility/helper methods */
1074
1075 static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1076 {
1077         writel(data, priv->base + offset);
1078 }
1079
1080 static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1081 {
1082         return readl(priv->base + offset);
1083 }
1084
1085 static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1086                                       struct mvpp2_tx_desc *tx_desc,
1087                                       dma_addr_t dma_addr)
1088 {
1089         if (port->priv->hw_version == MVPP21) {
1090                 tx_desc->pp21.buf_dma_addr = dma_addr;
1091         } else {
1092                 u64 val = (u64)dma_addr;
1093
1094                 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1095                 tx_desc->pp22.buf_dma_addr_ptp |= val;
1096         }
1097 }
1098
1099 static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1100                                   struct mvpp2_tx_desc *tx_desc,
1101                                   size_t size)
1102 {
1103         if (port->priv->hw_version == MVPP21)
1104                 tx_desc->pp21.data_size = size;
1105         else
1106                 tx_desc->pp22.data_size = size;
1107 }
1108
1109 static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1110                                  struct mvpp2_tx_desc *tx_desc,
1111                                  unsigned int txq)
1112 {
1113         if (port->priv->hw_version == MVPP21)
1114                 tx_desc->pp21.phys_txq = txq;
1115         else
1116                 tx_desc->pp22.phys_txq = txq;
1117 }
1118
1119 static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1120                                  struct mvpp2_tx_desc *tx_desc,
1121                                  unsigned int command)
1122 {
1123         if (port->priv->hw_version == MVPP21)
1124                 tx_desc->pp21.command = command;
1125         else
1126                 tx_desc->pp22.command = command;
1127 }
1128
1129 static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
1130                                     struct mvpp2_tx_desc *tx_desc,
1131                                     unsigned int offset)
1132 {
1133         if (port->priv->hw_version == MVPP21)
1134                 tx_desc->pp21.packet_offset = offset;
1135         else
1136                 tx_desc->pp22.packet_offset = offset;
1137 }
1138
1139 static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1140                                             struct mvpp2_rx_desc *rx_desc)
1141 {
1142         if (port->priv->hw_version == MVPP21)
1143                 return rx_desc->pp21.buf_dma_addr;
1144         else
1145                 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
1146 }
1147
1148 static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1149                                              struct mvpp2_rx_desc *rx_desc)
1150 {
1151         if (port->priv->hw_version == MVPP21)
1152                 return rx_desc->pp21.buf_cookie;
1153         else
1154                 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
1155 }
1156
1157 static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1158                                     struct mvpp2_rx_desc *rx_desc)
1159 {
1160         if (port->priv->hw_version == MVPP21)
1161                 return rx_desc->pp21.data_size;
1162         else
1163                 return rx_desc->pp22.data_size;
1164 }
1165
1166 static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1167                                    struct mvpp2_rx_desc *rx_desc)
1168 {
1169         if (port->priv->hw_version == MVPP21)
1170                 return rx_desc->pp21.status;
1171         else
1172                 return rx_desc->pp22.status;
1173 }
1174
1175 static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1176 {
1177         txq_pcpu->txq_get_index++;
1178         if (txq_pcpu->txq_get_index == txq_pcpu->size)
1179                 txq_pcpu->txq_get_index = 0;
1180 }
1181
1182 /* Get number of physical egress port */
1183 static inline int mvpp2_egress_port(struct mvpp2_port *port)
1184 {
1185         return MVPP2_MAX_TCONT + port->id;
1186 }
1187
1188 /* Get number of physical TXQ */
1189 static inline int mvpp2_txq_phys(int port, int txq)
1190 {
1191         return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1192 }
1193
1194 /* Parser configuration routines */
1195
1196 /* Update parser tcam and sram hw entries */
1197 static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1198 {
1199         int i;
1200
1201         if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1202                 return -EINVAL;
1203
1204         /* Clear entry invalidation bit */
1205         pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1206
1207         /* Write tcam index - indirect access */
1208         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1209         for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1210                 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1211
1212         /* Write sram index - indirect access */
1213         mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1214         for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1215                 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1216
1217         return 0;
1218 }
1219
1220 /* Read tcam entry from hw */
1221 static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1222 {
1223         int i;
1224
1225         if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1226                 return -EINVAL;
1227
1228         /* Write tcam index - indirect access */
1229         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1230
1231         pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1232                               MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1233         if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1234                 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1235
1236         for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1237                 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1238
1239         /* Write sram index - indirect access */
1240         mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1241         for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1242                 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1243
1244         return 0;
1245 }
1246
1247 /* Invalidate tcam hw entry */
1248 static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1249 {
1250         /* Write index - indirect access */
1251         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1252         mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1253                     MVPP2_PRS_TCAM_INV_MASK);
1254 }
1255
1256 /* Enable shadow table entry and set its lookup ID */
1257 static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1258 {
1259         priv->prs_shadow[index].valid = true;
1260         priv->prs_shadow[index].lu = lu;
1261 }
1262
1263 /* Update ri fields in shadow table entry */
1264 static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1265                                     unsigned int ri, unsigned int ri_mask)
1266 {
1267         priv->prs_shadow[index].ri_mask = ri_mask;
1268         priv->prs_shadow[index].ri = ri;
1269 }
1270
1271 /* Update lookup field in tcam sw entry */
1272 static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1273 {
1274         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1275
1276         pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1277         pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1278 }
1279
1280 /* Update mask for single port in tcam sw entry */
1281 static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1282                                     unsigned int port, bool add)
1283 {
1284         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1285
1286         if (add)
1287                 pe->tcam.byte[enable_off] &= ~(1 << port);
1288         else
1289                 pe->tcam.byte[enable_off] |= 1 << port;
1290 }
1291
1292 /* Update port map in tcam sw entry */
1293 static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1294                                         unsigned int ports)
1295 {
1296         unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1297         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1298
1299         pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1300         pe->tcam.byte[enable_off] &= ~port_mask;
1301         pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1302 }
1303
1304 /* Obtain port map from tcam sw entry */
1305 static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1306 {
1307         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1308
1309         return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1310 }
1311
1312 /* Set byte of data and its enable bits in tcam sw entry */
1313 static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1314                                          unsigned int offs, unsigned char byte,
1315                                          unsigned char enable)
1316 {
1317         pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1318         pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1319 }
1320
1321 /* Get byte of data and its enable bits from tcam sw entry */
1322 static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1323                                          unsigned int offs, unsigned char *byte,
1324                                          unsigned char *enable)
1325 {
1326         *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1327         *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1328 }
1329
1330 /* Set ethertype in tcam sw entry */
1331 static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1332                                   unsigned short ethertype)
1333 {
1334         mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1335         mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1336 }
1337
1338 /* Set bits in sram sw entry */
1339 static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1340                                     int val)
1341 {
1342         pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1343 }
1344
1345 /* Clear bits in sram sw entry */
1346 static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1347                                       int val)
1348 {
1349         pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1350 }
1351
1352 /* Update ri bits in sram sw entry */
1353 static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1354                                      unsigned int bits, unsigned int mask)
1355 {
1356         unsigned int i;
1357
1358         for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1359                 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1360
1361                 if (!(mask & BIT(i)))
1362                         continue;
1363
1364                 if (bits & BIT(i))
1365                         mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1366                 else
1367                         mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1368
1369                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1370         }
1371 }
1372
1373 /* Update ai bits in sram sw entry */
1374 static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1375                                      unsigned int bits, unsigned int mask)
1376 {
1377         unsigned int i;
1378         int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1379
1380         for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1381
1382                 if (!(mask & BIT(i)))
1383                         continue;
1384
1385                 if (bits & BIT(i))
1386                         mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1387                 else
1388                         mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1389
1390                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1391         }
1392 }
1393
1394 /* Read ai bits from sram sw entry */
1395 static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1396 {
1397         u8 bits;
1398         int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1399         int ai_en_off = ai_off + 1;
1400         int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1401
1402         bits = (pe->sram.byte[ai_off] >> ai_shift) |
1403                (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1404
1405         return bits;
1406 }
1407
1408 /* In sram sw entry set lookup ID field of the tcam key to be used in the next
1409  * lookup interation
1410  */
1411 static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1412                                        unsigned int lu)
1413 {
1414         int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1415
1416         mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1417                                   MVPP2_PRS_SRAM_NEXT_LU_MASK);
1418         mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1419 }
1420
1421 /* In the sram sw entry set sign and value of the next lookup offset
1422  * and the offset value generated to the classifier
1423  */
1424 static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1425                                      unsigned int op)
1426 {
1427         /* Set sign */
1428         if (shift < 0) {
1429                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1430                 shift = 0 - shift;
1431         } else {
1432                 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1433         }
1434
1435         /* Set value */
1436         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1437                                                            (unsigned char)shift;
1438
1439         /* Reset and set operation */
1440         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1441                                   MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1442         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1443
1444         /* Set base offset as current */
1445         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1446 }
1447
1448 /* In the sram sw entry set sign and value of the user defined offset
1449  * generated to the classifier
1450  */
1451 static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1452                                       unsigned int type, int offset,
1453                                       unsigned int op)
1454 {
1455         /* Set sign */
1456         if (offset < 0) {
1457                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1458                 offset = 0 - offset;
1459         } else {
1460                 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1461         }
1462
1463         /* Set value */
1464         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1465                                   MVPP2_PRS_SRAM_UDF_MASK);
1466         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1467         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1468                                         MVPP2_PRS_SRAM_UDF_BITS)] &=
1469               ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1470         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1471                                         MVPP2_PRS_SRAM_UDF_BITS)] |=
1472                                 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1473
1474         /* Set offset type */
1475         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1476                                   MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1477         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1478
1479         /* Set offset operation */
1480         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1481                                   MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1482         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1483
1484         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1485                                         MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1486                                              ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1487                                     (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1488
1489         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1490                                         MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1491                              (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1492
1493         /* Set base offset as current */
1494         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1495 }
1496
1497 /* Find parser flow entry */
1498 static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1499 {
1500         struct mvpp2_prs_entry *pe;
1501         int tid;
1502
1503         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1504         if (!pe)
1505                 return NULL;
1506         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1507
1508         /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1509         for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1510                 u8 bits;
1511
1512                 if (!priv->prs_shadow[tid].valid ||
1513                     priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1514                         continue;
1515
1516                 pe->index = tid;
1517                 mvpp2_prs_hw_read(priv, pe);
1518                 bits = mvpp2_prs_sram_ai_get(pe);
1519
1520                 /* Sram store classification lookup ID in AI bits [5:0] */
1521                 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1522                         return pe;
1523         }
1524         kfree(pe);
1525
1526         return NULL;
1527 }
1528
1529 /* Return first free tcam index, seeking from start to end */
1530 static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1531                                      unsigned char end)
1532 {
1533         int tid;
1534
1535         if (start > end)
1536                 swap(start, end);
1537
1538         if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1539                 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1540
1541         for (tid = start; tid <= end; tid++) {
1542                 if (!priv->prs_shadow[tid].valid)
1543                         return tid;
1544         }
1545
1546         return -EINVAL;
1547 }
1548
1549 /* Enable/disable dropping all mac da's */
1550 static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1551 {
1552         struct mvpp2_prs_entry pe;
1553
1554         if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1555                 /* Entry exist - update port only */
1556                 pe.index = MVPP2_PE_DROP_ALL;
1557                 mvpp2_prs_hw_read(priv, &pe);
1558         } else {
1559                 /* Entry doesn't exist - create new */
1560                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1561                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1562                 pe.index = MVPP2_PE_DROP_ALL;
1563
1564                 /* Non-promiscuous mode for all ports - DROP unknown packets */
1565                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1566                                          MVPP2_PRS_RI_DROP_MASK);
1567
1568                 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1569                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1570
1571                 /* Update shadow table */
1572                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1573
1574                 /* Mask all ports */
1575                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1576         }
1577
1578         /* Update port mask */
1579         mvpp2_prs_tcam_port_set(&pe, port, add);
1580
1581         mvpp2_prs_hw_write(priv, &pe);
1582 }
1583
1584 /* Set port to promiscuous mode */
1585 static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1586 {
1587         struct mvpp2_prs_entry pe;
1588
1589         /* Promiscuous mode - Accept unknown packets */
1590
1591         if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1592                 /* Entry exist - update port only */
1593                 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1594                 mvpp2_prs_hw_read(priv, &pe);
1595         } else {
1596                 /* Entry doesn't exist - create new */
1597                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1598                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1599                 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1600
1601                 /* Continue - set next lookup */
1602                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1603
1604                 /* Set result info bits */
1605                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1606                                          MVPP2_PRS_RI_L2_CAST_MASK);
1607
1608                 /* Shift to ethertype */
1609                 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1610                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1611
1612                 /* Mask all ports */
1613                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1614
1615                 /* Update shadow table */
1616                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1617         }
1618
1619         /* Update port mask */
1620         mvpp2_prs_tcam_port_set(&pe, port, add);
1621
1622         mvpp2_prs_hw_write(priv, &pe);
1623 }
1624
1625 /* Accept multicast */
1626 static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1627                                     bool add)
1628 {
1629         struct mvpp2_prs_entry pe;
1630         unsigned char da_mc;
1631
1632         /* Ethernet multicast address first byte is
1633          * 0x01 for IPv4 and 0x33 for IPv6
1634          */
1635         da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1636
1637         if (priv->prs_shadow[index].valid) {
1638                 /* Entry exist - update port only */
1639                 pe.index = index;
1640                 mvpp2_prs_hw_read(priv, &pe);
1641         } else {
1642                 /* Entry doesn't exist - create new */
1643                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1644                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1645                 pe.index = index;
1646
1647                 /* Continue - set next lookup */
1648                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1649
1650                 /* Set result info bits */
1651                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1652                                          MVPP2_PRS_RI_L2_CAST_MASK);
1653
1654                 /* Update tcam entry data first byte */
1655                 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1656
1657                 /* Shift to ethertype */
1658                 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1659                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1660
1661                 /* Mask all ports */
1662                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1663
1664                 /* Update shadow table */
1665                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1666         }
1667
1668         /* Update port mask */
1669         mvpp2_prs_tcam_port_set(&pe, port, add);
1670
1671         mvpp2_prs_hw_write(priv, &pe);
1672 }
1673
1674 /* Parser per-port initialization */
1675 static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
1676                                    int lu_max, int offset)
1677 {
1678         u32 val;
1679
1680         /* Set lookup ID */
1681         val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
1682         val &= ~MVPP2_PRS_PORT_LU_MASK(port);
1683         val |=  MVPP2_PRS_PORT_LU_VAL(port, lu_first);
1684         mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
1685
1686         /* Set maximum number of loops for packet received from port */
1687         val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
1688         val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
1689         val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
1690         mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
1691
1692         /* Set initial offset for packet header extraction for the first
1693          * searching loop
1694          */
1695         val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
1696         val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
1697         val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
1698         mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
1699 }
1700
1701 /* Default flow entries initialization for all ports */
1702 static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
1703 {
1704         struct mvpp2_prs_entry pe;
1705         int port;
1706
1707         for (port = 0; port < MVPP2_MAX_PORTS; port++) {
1708                 memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1709                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1710                 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
1711
1712                 /* Mask all ports */
1713                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1714
1715                 /* Set flow ID*/
1716                 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
1717                 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
1718
1719                 /* Update shadow table and hw entry */
1720                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
1721                 mvpp2_prs_hw_write(priv, &pe);
1722         }
1723 }
1724
1725 /* Set default entry for Marvell Header field */
1726 static void mvpp2_prs_mh_init(struct mvpp2 *priv)
1727 {
1728         struct mvpp2_prs_entry pe;
1729
1730         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1731
1732         pe.index = MVPP2_PE_MH_DEFAULT;
1733         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
1734         mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
1735                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1736         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
1737
1738         /* Unmask all ports */
1739         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1740
1741         /* Update shadow table and hw entry */
1742         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
1743         mvpp2_prs_hw_write(priv, &pe);
1744 }
1745
1746 /* Set default entires (place holder) for promiscuous, non-promiscuous and
1747  * multicast MAC addresses
1748  */
1749 static void mvpp2_prs_mac_init(struct mvpp2 *priv)
1750 {
1751         struct mvpp2_prs_entry pe;
1752
1753         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1754
1755         /* Non-promiscuous mode for all ports - DROP unknown packets */
1756         pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
1757         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1758
1759         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1760                                  MVPP2_PRS_RI_DROP_MASK);
1761         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1762         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1763
1764         /* Unmask all ports */
1765         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1766
1767         /* Update shadow table and hw entry */
1768         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1769         mvpp2_prs_hw_write(priv, &pe);
1770
1771         /* place holders only - no ports */
1772         mvpp2_prs_mac_drop_all_set(priv, 0, false);
1773         mvpp2_prs_mac_promisc_set(priv, 0, false);
1774         mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
1775         mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
1776 }
1777
1778 /* Match basic ethertypes */
1779 static int mvpp2_prs_etype_init(struct mvpp2 *priv)
1780 {
1781         struct mvpp2_prs_entry pe;
1782         int tid;
1783
1784         /* Ethertype: PPPoE */
1785         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1786                                         MVPP2_PE_LAST_FREE_TID);
1787         if (tid < 0)
1788                 return tid;
1789
1790         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1791         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1792         pe.index = tid;
1793
1794         mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
1795
1796         mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
1797                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1798         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
1799         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
1800                                  MVPP2_PRS_RI_PPPOE_MASK);
1801
1802         /* Update shadow table and hw entry */
1803         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1804         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1805         priv->prs_shadow[pe.index].finish = false;
1806         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
1807                                 MVPP2_PRS_RI_PPPOE_MASK);
1808         mvpp2_prs_hw_write(priv, &pe);
1809
1810         /* Ethertype: ARP */
1811         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1812                                         MVPP2_PE_LAST_FREE_TID);
1813         if (tid < 0)
1814                 return tid;
1815
1816         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1817         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1818         pe.index = tid;
1819
1820         mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
1821
1822         /* Generate flow in the next iteration*/
1823         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1824         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1825         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
1826                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1827         /* Set L3 offset */
1828         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1829                                   MVPP2_ETH_TYPE_LEN,
1830                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1831
1832         /* Update shadow table and hw entry */
1833         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1834         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1835         priv->prs_shadow[pe.index].finish = true;
1836         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
1837                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1838         mvpp2_prs_hw_write(priv, &pe);
1839
1840         /* Ethertype: LBTD */
1841         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1842                                         MVPP2_PE_LAST_FREE_TID);
1843         if (tid < 0)
1844                 return tid;
1845
1846         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1847         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1848         pe.index = tid;
1849
1850         mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
1851
1852         /* Generate flow in the next iteration*/
1853         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1854         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1855         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1856                                  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1857                                  MVPP2_PRS_RI_CPU_CODE_MASK |
1858                                  MVPP2_PRS_RI_UDF3_MASK);
1859         /* Set L3 offset */
1860         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1861                                   MVPP2_ETH_TYPE_LEN,
1862                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1863
1864         /* Update shadow table and hw entry */
1865         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1866         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1867         priv->prs_shadow[pe.index].finish = true;
1868         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
1869                                 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
1870                                 MVPP2_PRS_RI_CPU_CODE_MASK |
1871                                 MVPP2_PRS_RI_UDF3_MASK);
1872         mvpp2_prs_hw_write(priv, &pe);
1873
1874         /* Ethertype: IPv4 without options */
1875         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1876                                         MVPP2_PE_LAST_FREE_TID);
1877         if (tid < 0)
1878                 return tid;
1879
1880         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1881         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1882         pe.index = tid;
1883
1884         mvpp2_prs_match_etype(&pe, 0, PROT_IP);
1885         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1886                                      MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
1887                                      MVPP2_PRS_IPV4_HEAD_MASK |
1888                                      MVPP2_PRS_IPV4_IHL_MASK);
1889
1890         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
1891         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
1892                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1893         /* Skip eth_type + 4 bytes of IP header */
1894         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
1895                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1896         /* Set L3 offset */
1897         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1898                                   MVPP2_ETH_TYPE_LEN,
1899                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1900
1901         /* Update shadow table and hw entry */
1902         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1903         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1904         priv->prs_shadow[pe.index].finish = false;
1905         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
1906                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1907         mvpp2_prs_hw_write(priv, &pe);
1908
1909         /* Ethertype: IPv4 with options */
1910         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1911                                         MVPP2_PE_LAST_FREE_TID);
1912         if (tid < 0)
1913                 return tid;
1914
1915         pe.index = tid;
1916
1917         /* Clear tcam data before updating */
1918         pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
1919         pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
1920
1921         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
1922                                      MVPP2_PRS_IPV4_HEAD,
1923                                      MVPP2_PRS_IPV4_HEAD_MASK);
1924
1925         /* Clear ri before updating */
1926         pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
1927         pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
1928         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
1929                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1930
1931         /* Update shadow table and hw entry */
1932         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1933         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1934         priv->prs_shadow[pe.index].finish = false;
1935         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
1936                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1937         mvpp2_prs_hw_write(priv, &pe);
1938
1939         /* Ethertype: IPv6 without options */
1940         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
1941                                         MVPP2_PE_LAST_FREE_TID);
1942         if (tid < 0)
1943                 return tid;
1944
1945         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1946         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1947         pe.index = tid;
1948
1949         mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
1950
1951         /* Skip DIP of IPV6 header */
1952         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
1953                                  MVPP2_MAX_L3_ADDR_SIZE,
1954                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1955         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
1956         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
1957                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1958         /* Set L3 offset */
1959         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1960                                   MVPP2_ETH_TYPE_LEN,
1961                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1962
1963         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1964         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1965         priv->prs_shadow[pe.index].finish = false;
1966         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
1967                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1968         mvpp2_prs_hw_write(priv, &pe);
1969
1970         /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
1971         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
1972         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
1973         pe.index = MVPP2_PE_ETH_TYPE_UN;
1974
1975         /* Unmask all ports */
1976         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
1977
1978         /* Generate flow in the next iteration*/
1979         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1980         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1981         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
1982                                  MVPP2_PRS_RI_L3_PROTO_MASK);
1983         /* Set L3 offset even it's unknown L3 */
1984         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
1985                                   MVPP2_ETH_TYPE_LEN,
1986                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
1987
1988         /* Update shadow table and hw entry */
1989         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
1990         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
1991         priv->prs_shadow[pe.index].finish = true;
1992         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
1993                                 MVPP2_PRS_RI_L3_PROTO_MASK);
1994         mvpp2_prs_hw_write(priv, &pe);
1995
1996         return 0;
1997 }
1998
1999 /* Parser default initialization */
2000 static int mvpp2_prs_default_init(struct udevice *dev,
2001                                   struct mvpp2 *priv)
2002 {
2003         int err, index, i;
2004
2005         /* Enable tcam table */
2006         mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
2007
2008         /* Clear all tcam and sram entries */
2009         for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
2010                 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
2011                 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
2012                         mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
2013
2014                 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
2015                 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
2016                         mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
2017         }
2018
2019         /* Invalidate all tcam entries */
2020         for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
2021                 mvpp2_prs_hw_inv(priv, index);
2022
2023         priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
2024                                         sizeof(struct mvpp2_prs_shadow),
2025                                         GFP_KERNEL);
2026         if (!priv->prs_shadow)
2027                 return -ENOMEM;
2028
2029         /* Always start from lookup = 0 */
2030         for (index = 0; index < MVPP2_MAX_PORTS; index++)
2031                 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
2032                                        MVPP2_PRS_PORT_LU_MAX, 0);
2033
2034         mvpp2_prs_def_flow_init(priv);
2035
2036         mvpp2_prs_mh_init(priv);
2037
2038         mvpp2_prs_mac_init(priv);
2039
2040         err = mvpp2_prs_etype_init(priv);
2041         if (err)
2042                 return err;
2043
2044         return 0;
2045 }
2046
2047 /* Compare MAC DA with tcam entry data */
2048 static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
2049                                        const u8 *da, unsigned char *mask)
2050 {
2051         unsigned char tcam_byte, tcam_mask;
2052         int index;
2053
2054         for (index = 0; index < ETH_ALEN; index++) {
2055                 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
2056                 if (tcam_mask != mask[index])
2057                         return false;
2058
2059                 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
2060                         return false;
2061         }
2062
2063         return true;
2064 }
2065
2066 /* Find tcam entry with matched pair <MAC DA, port> */
2067 static struct mvpp2_prs_entry *
2068 mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
2069                             unsigned char *mask, int udf_type)
2070 {
2071         struct mvpp2_prs_entry *pe;
2072         int tid;
2073
2074         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2075         if (!pe)
2076                 return NULL;
2077         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2078
2079         /* Go through the all entires with MVPP2_PRS_LU_MAC */
2080         for (tid = MVPP2_PE_FIRST_FREE_TID;
2081              tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2082                 unsigned int entry_pmap;
2083
2084                 if (!priv->prs_shadow[tid].valid ||
2085                     (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
2086                     (priv->prs_shadow[tid].udf != udf_type))
2087                         continue;
2088
2089                 pe->index = tid;
2090                 mvpp2_prs_hw_read(priv, pe);
2091                 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
2092
2093                 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
2094                     entry_pmap == pmap)
2095                         return pe;
2096         }
2097         kfree(pe);
2098
2099         return NULL;
2100 }
2101
2102 /* Update parser's mac da entry */
2103 static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
2104                                    const u8 *da, bool add)
2105 {
2106         struct mvpp2_prs_entry *pe;
2107         unsigned int pmap, len, ri;
2108         unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2109         int tid;
2110
2111         /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
2112         pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
2113                                          MVPP2_PRS_UDF_MAC_DEF);
2114
2115         /* No such entry */
2116         if (!pe) {
2117                 if (!add)
2118                         return 0;
2119
2120                 /* Create new TCAM entry */
2121                 /* Find first range mac entry*/
2122                 for (tid = MVPP2_PE_FIRST_FREE_TID;
2123                      tid <= MVPP2_PE_LAST_FREE_TID; tid++)
2124                         if (priv->prs_shadow[tid].valid &&
2125                             (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
2126                             (priv->prs_shadow[tid].udf ==
2127                                                        MVPP2_PRS_UDF_MAC_RANGE))
2128                                 break;
2129
2130                 /* Go through the all entries from first to last */
2131                 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2132                                                 tid - 1);
2133                 if (tid < 0)
2134                         return tid;
2135
2136                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2137                 if (!pe)
2138                         return -1;
2139                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
2140                 pe->index = tid;
2141
2142                 /* Mask all ports */
2143                 mvpp2_prs_tcam_port_map_set(pe, 0);
2144         }
2145
2146         /* Update port mask */
2147         mvpp2_prs_tcam_port_set(pe, port, add);
2148
2149         /* Invalidate the entry if no ports are left enabled */
2150         pmap = mvpp2_prs_tcam_port_map_get(pe);
2151         if (pmap == 0) {
2152                 if (add) {
2153                         kfree(pe);
2154                         return -1;
2155                 }
2156                 mvpp2_prs_hw_inv(priv, pe->index);
2157                 priv->prs_shadow[pe->index].valid = false;
2158                 kfree(pe);
2159                 return 0;
2160         }
2161
2162         /* Continue - set next lookup */
2163         mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
2164
2165         /* Set match on DA */
2166         len = ETH_ALEN;
2167         while (len--)
2168                 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
2169
2170         /* Set result info bits */
2171         ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
2172
2173         mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2174                                  MVPP2_PRS_RI_MAC_ME_MASK);
2175         mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
2176                                 MVPP2_PRS_RI_MAC_ME_MASK);
2177
2178         /* Shift to ethertype */
2179         mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
2180                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2181
2182         /* Update shadow table and hw entry */
2183         priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
2184         mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
2185         mvpp2_prs_hw_write(priv, pe);
2186
2187         kfree(pe);
2188
2189         return 0;
2190 }
2191
2192 static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
2193 {
2194         int err;
2195
2196         /* Remove old parser entry */
2197         err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
2198                                       false);
2199         if (err)
2200                 return err;
2201
2202         /* Add new parser entry */
2203         err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
2204         if (err)
2205                 return err;
2206
2207         /* Set addr in the device */
2208         memcpy(port->dev_addr, da, ETH_ALEN);
2209
2210         return 0;
2211 }
2212
2213 /* Set prs flow for the port */
2214 static int mvpp2_prs_def_flow(struct mvpp2_port *port)
2215 {
2216         struct mvpp2_prs_entry *pe;
2217         int tid;
2218
2219         pe = mvpp2_prs_flow_find(port->priv, port->id);
2220
2221         /* Such entry not exist */
2222         if (!pe) {
2223                 /* Go through the all entires from last to first */
2224                 tid = mvpp2_prs_tcam_first_free(port->priv,
2225                                                 MVPP2_PE_LAST_FREE_TID,
2226                                                MVPP2_PE_FIRST_FREE_TID);
2227                 if (tid < 0)
2228                         return tid;
2229
2230                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2231                 if (!pe)
2232                         return -ENOMEM;
2233
2234                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
2235                 pe->index = tid;
2236
2237                 /* Set flow ID*/
2238                 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
2239                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2240
2241                 /* Update shadow table */
2242                 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
2243         }
2244
2245         mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
2246         mvpp2_prs_hw_write(port->priv, pe);
2247         kfree(pe);
2248
2249         return 0;
2250 }
2251
2252 /* Classifier configuration routines */
2253
2254 /* Update classification flow table registers */
2255 static void mvpp2_cls_flow_write(struct mvpp2 *priv,
2256                                  struct mvpp2_cls_flow_entry *fe)
2257 {
2258         mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
2259         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG,  fe->data[0]);
2260         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG,  fe->data[1]);
2261         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG,  fe->data[2]);
2262 }
2263
2264 /* Update classification lookup table register */
2265 static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
2266                                    struct mvpp2_cls_lookup_entry *le)
2267 {
2268         u32 val;
2269
2270         val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
2271         mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
2272         mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
2273 }
2274
2275 /* Classifier default initialization */
2276 static void mvpp2_cls_init(struct mvpp2 *priv)
2277 {
2278         struct mvpp2_cls_lookup_entry le;
2279         struct mvpp2_cls_flow_entry fe;
2280         int index;
2281
2282         /* Enable classifier */
2283         mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
2284
2285         /* Clear classifier flow table */
2286         memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
2287         for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
2288                 fe.index = index;
2289                 mvpp2_cls_flow_write(priv, &fe);
2290         }
2291
2292         /* Clear classifier lookup table */
2293         le.data = 0;
2294         for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
2295                 le.lkpid = index;
2296                 le.way = 0;
2297                 mvpp2_cls_lookup_write(priv, &le);
2298
2299                 le.way = 1;
2300                 mvpp2_cls_lookup_write(priv, &le);
2301         }
2302 }
2303
2304 static void mvpp2_cls_port_config(struct mvpp2_port *port)
2305 {
2306         struct mvpp2_cls_lookup_entry le;
2307         u32 val;
2308
2309         /* Set way for the port */
2310         val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
2311         val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
2312         mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
2313
2314         /* Pick the entry to be accessed in lookup ID decoding table
2315          * according to the way and lkpid.
2316          */
2317         le.lkpid = port->id;
2318         le.way = 0;
2319         le.data = 0;
2320
2321         /* Set initial CPU queue for receiving packets */
2322         le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
2323         le.data |= port->first_rxq;
2324
2325         /* Disable classification engines */
2326         le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
2327
2328         /* Update lookup ID table entry */
2329         mvpp2_cls_lookup_write(port->priv, &le);
2330 }
2331
2332 /* Set CPU queue number for oversize packets */
2333 static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
2334 {
2335         u32 val;
2336
2337         mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
2338                     port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
2339
2340         mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
2341                     (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
2342
2343         val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
2344         val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
2345         mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
2346 }
2347
2348 /* Buffer Manager configuration routines */
2349
2350 /* Create pool */
2351 static int mvpp2_bm_pool_create(struct udevice *dev,
2352                                 struct mvpp2 *priv,
2353                                 struct mvpp2_bm_pool *bm_pool, int size)
2354 {
2355         u32 val;
2356
2357         /* Number of buffer pointers must be a multiple of 16, as per
2358          * hardware constraints
2359          */
2360         if (!IS_ALIGNED(size, 16))
2361                 return -EINVAL;
2362
2363         bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
2364         bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
2365         if (!bm_pool->virt_addr)
2366                 return -ENOMEM;
2367
2368         if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
2369                         MVPP2_BM_POOL_PTR_ALIGN)) {
2370                 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
2371                         bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
2372                 return -ENOMEM;
2373         }
2374
2375         mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
2376                     lower_32_bits(bm_pool->dma_addr));
2377         mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
2378
2379         val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2380         val |= MVPP2_BM_START_MASK;
2381         mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2382
2383         bm_pool->type = MVPP2_BM_FREE;
2384         bm_pool->size = size;
2385         bm_pool->pkt_size = 0;
2386         bm_pool->buf_num = 0;
2387
2388         return 0;
2389 }
2390
2391 /* Set pool buffer size */
2392 static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
2393                                       struct mvpp2_bm_pool *bm_pool,
2394                                       int buf_size)
2395 {
2396         u32 val;
2397
2398         bm_pool->buf_size = buf_size;
2399
2400         val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
2401         mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
2402 }
2403
2404 /* Free all buffers from the pool */
2405 static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
2406                                struct mvpp2_bm_pool *bm_pool)
2407 {
2408         bm_pool->buf_num = 0;
2409 }
2410
2411 /* Cleanup pool */
2412 static int mvpp2_bm_pool_destroy(struct udevice *dev,
2413                                  struct mvpp2 *priv,
2414                                  struct mvpp2_bm_pool *bm_pool)
2415 {
2416         u32 val;
2417
2418         mvpp2_bm_bufs_free(dev, priv, bm_pool);
2419         if (bm_pool->buf_num) {
2420                 dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
2421                 return 0;
2422         }
2423
2424         val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
2425         val |= MVPP2_BM_STOP_MASK;
2426         mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
2427
2428         return 0;
2429 }
2430
2431 static int mvpp2_bm_pools_init(struct udevice *dev,
2432                                struct mvpp2 *priv)
2433 {
2434         int i, err, size;
2435         struct mvpp2_bm_pool *bm_pool;
2436
2437         /* Create all pools with maximum size */
2438         size = MVPP2_BM_POOL_SIZE_MAX;
2439         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2440                 bm_pool = &priv->bm_pools[i];
2441                 bm_pool->id = i;
2442                 err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
2443                 if (err)
2444                         goto err_unroll_pools;
2445                 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
2446         }
2447         return 0;
2448
2449 err_unroll_pools:
2450         dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
2451         for (i = i - 1; i >= 0; i--)
2452                 mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
2453         return err;
2454 }
2455
2456 static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
2457 {
2458         int i, err;
2459
2460         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
2461                 /* Mask BM all interrupts */
2462                 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
2463                 /* Clear BM cause register */
2464                 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
2465         }
2466
2467         /* Allocate and initialize BM pools */
2468         priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
2469                                      sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
2470         if (!priv->bm_pools)
2471                 return -ENOMEM;
2472
2473         err = mvpp2_bm_pools_init(dev, priv);
2474         if (err < 0)
2475                 return err;
2476         return 0;
2477 }
2478
2479 /* Attach long pool to rxq */
2480 static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
2481                                     int lrxq, int long_pool)
2482 {
2483         u32 val, mask;
2484         int prxq;
2485
2486         /* Get queue physical ID */
2487         prxq = port->rxqs[lrxq]->id;
2488
2489         if (port->priv->hw_version == MVPP21)
2490                 mask = MVPP21_RXQ_POOL_LONG_MASK;
2491         else
2492                 mask = MVPP22_RXQ_POOL_LONG_MASK;
2493
2494         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2495         val &= ~mask;
2496         val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
2497         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2498 }
2499
2500 /* Set pool number in a BM cookie */
2501 static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
2502 {
2503         u32 bm;
2504
2505         bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
2506         bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
2507
2508         return bm;
2509 }
2510
2511 /* Get pool number from a BM cookie */
2512 static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
2513 {
2514         return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
2515 }
2516
2517 /* Release buffer to BM */
2518 static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
2519                                      dma_addr_t buf_dma_addr,
2520                                      unsigned long buf_phys_addr)
2521 {
2522         if (port->priv->hw_version == MVPP22) {
2523                 u32 val = 0;
2524
2525                 if (sizeof(dma_addr_t) == 8)
2526                         val |= upper_32_bits(buf_dma_addr) &
2527                                 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
2528
2529                 if (sizeof(phys_addr_t) == 8)
2530                         val |= (upper_32_bits(buf_phys_addr)
2531                                 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
2532                                 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
2533
2534                 mvpp2_write(port->priv, MVPP22_BM_ADDR_HIGH_RLS_REG, val);
2535         }
2536
2537         /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
2538          * returned in the "cookie" field of the RX
2539          * descriptor. Instead of storing the virtual address, we
2540          * store the physical address
2541          */
2542         mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
2543         mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
2544 }
2545
2546 /* Refill BM pool */
2547 static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
2548                               dma_addr_t dma_addr,
2549                               phys_addr_t phys_addr)
2550 {
2551         int pool = mvpp2_bm_cookie_pool_get(bm);
2552
2553         mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
2554 }
2555
2556 /* Allocate buffers for the pool */
2557 static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
2558                              struct mvpp2_bm_pool *bm_pool, int buf_num)
2559 {
2560         int i;
2561
2562         if (buf_num < 0 ||
2563             (buf_num + bm_pool->buf_num > bm_pool->size)) {
2564                 netdev_err(port->dev,
2565                            "cannot allocate %d buffers for pool %d\n",
2566                            buf_num, bm_pool->id);
2567                 return 0;
2568         }
2569
2570         for (i = 0; i < buf_num; i++) {
2571                 mvpp2_bm_pool_put(port, bm_pool->id,
2572                                   (dma_addr_t)buffer_loc.rx_buffer[i],
2573                                   (unsigned long)buffer_loc.rx_buffer[i]);
2574
2575         }
2576
2577         /* Update BM driver with number of buffers added to pool */
2578         bm_pool->buf_num += i;
2579         bm_pool->in_use_thresh = bm_pool->buf_num / 4;
2580
2581         return i;
2582 }
2583
2584 /* Notify the driver that BM pool is being used as specific type and return the
2585  * pool pointer on success
2586  */
2587 static struct mvpp2_bm_pool *
2588 mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
2589                   int pkt_size)
2590 {
2591         struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
2592         int num;
2593
2594         if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
2595                 netdev_err(port->dev, "mixing pool types is forbidden\n");
2596                 return NULL;
2597         }
2598
2599         if (new_pool->type == MVPP2_BM_FREE)
2600                 new_pool->type = type;
2601
2602         /* Allocate buffers in case BM pool is used as long pool, but packet
2603          * size doesn't match MTU or BM pool hasn't being used yet
2604          */
2605         if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
2606             (new_pool->pkt_size == 0)) {
2607                 int pkts_num;
2608
2609                 /* Set default buffer number or free all the buffers in case
2610                  * the pool is not empty
2611                  */
2612                 pkts_num = new_pool->buf_num;
2613                 if (pkts_num == 0)
2614                         pkts_num = type == MVPP2_BM_SWF_LONG ?
2615                                    MVPP2_BM_LONG_BUF_NUM :
2616                                    MVPP2_BM_SHORT_BUF_NUM;
2617                 else
2618                         mvpp2_bm_bufs_free(NULL,
2619                                            port->priv, new_pool);
2620
2621                 new_pool->pkt_size = pkt_size;
2622
2623                 /* Allocate buffers for this pool */
2624                 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
2625                 if (num != pkts_num) {
2626                         dev_err(dev, "pool %d: %d of %d allocated\n",
2627                                 new_pool->id, num, pkts_num);
2628                         return NULL;
2629                 }
2630         }
2631
2632         mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
2633                                   MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
2634
2635         return new_pool;
2636 }
2637
2638 /* Initialize pools for swf */
2639 static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
2640 {
2641         int rxq;
2642
2643         if (!port->pool_long) {
2644                 port->pool_long =
2645                        mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
2646                                          MVPP2_BM_SWF_LONG,
2647                                          port->pkt_size);
2648                 if (!port->pool_long)
2649                         return -ENOMEM;
2650
2651                 port->pool_long->port_map |= (1 << port->id);
2652
2653                 for (rxq = 0; rxq < rxq_number; rxq++)
2654                         mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
2655         }
2656
2657         return 0;
2658 }
2659
2660 /* Port configuration routines */
2661
2662 static void mvpp2_port_mii_set(struct mvpp2_port *port)
2663 {
2664         u32 val;
2665
2666         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
2667
2668         switch (port->phy_interface) {
2669         case PHY_INTERFACE_MODE_SGMII:
2670                 val |= MVPP2_GMAC_INBAND_AN_MASK;
2671                 break;
2672         case PHY_INTERFACE_MODE_RGMII:
2673                 val |= MVPP2_GMAC_PORT_RGMII_MASK;
2674         default:
2675                 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
2676         }
2677
2678         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2679 }
2680
2681 static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
2682 {
2683         u32 val;
2684
2685         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2686         val |= MVPP2_GMAC_FC_ADV_EN;
2687         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
2688 }
2689
2690 static void mvpp2_port_enable(struct mvpp2_port *port)
2691 {
2692         u32 val;
2693
2694         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2695         val |= MVPP2_GMAC_PORT_EN_MASK;
2696         val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
2697         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2698 }
2699
2700 static void mvpp2_port_disable(struct mvpp2_port *port)
2701 {
2702         u32 val;
2703
2704         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2705         val &= ~(MVPP2_GMAC_PORT_EN_MASK);
2706         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2707 }
2708
2709 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
2710 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
2711 {
2712         u32 val;
2713
2714         val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
2715                     ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
2716         writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2717 }
2718
2719 /* Configure loopback port */
2720 static void mvpp2_port_loopback_set(struct mvpp2_port *port)
2721 {
2722         u32 val;
2723
2724         val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
2725
2726         if (port->speed == 1000)
2727                 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
2728         else
2729                 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
2730
2731         if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
2732                 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
2733         else
2734                 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
2735
2736         writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
2737 }
2738
2739 static void mvpp2_port_reset(struct mvpp2_port *port)
2740 {
2741         u32 val;
2742
2743         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2744                     ~MVPP2_GMAC_PORT_RESET_MASK;
2745         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2746
2747         while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
2748                MVPP2_GMAC_PORT_RESET_MASK)
2749                 continue;
2750 }
2751
2752 /* Change maximum receive size of the port */
2753 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
2754 {
2755         u32 val;
2756
2757         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2758         val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
2759         val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2760                     MVPP2_GMAC_MAX_RX_SIZE_OFFS);
2761         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2762 }
2763
2764 /* Set defaults to the MVPP2 port */
2765 static void mvpp2_defaults_set(struct mvpp2_port *port)
2766 {
2767         int tx_port_num, val, queue, ptxq, lrxq;
2768
2769         if (port->priv->hw_version == MVPP21) {
2770                 /* Configure port to loopback if needed */
2771                 if (port->flags & MVPP2_F_LOOPBACK)
2772                         mvpp2_port_loopback_set(port);
2773
2774                 /* Update TX FIFO MIN Threshold */
2775                 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2776                 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
2777                 /* Min. TX threshold must be less than minimal packet length */
2778                 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
2779                 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2780         }
2781
2782         /* Disable Legacy WRR, Disable EJP, Release from reset */
2783         tx_port_num = mvpp2_egress_port(port);
2784         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
2785                     tx_port_num);
2786         mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
2787
2788         /* Close bandwidth for all queues */
2789         for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
2790                 ptxq = mvpp2_txq_phys(port->id, queue);
2791                 mvpp2_write(port->priv,
2792                             MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
2793         }
2794
2795         /* Set refill period to 1 usec, refill tokens
2796          * and bucket size to maximum
2797          */
2798         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
2799         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
2800         val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
2801         val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
2802         val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
2803         mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
2804         val = MVPP2_TXP_TOKEN_SIZE_MAX;
2805         mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2806
2807         /* Set MaximumLowLatencyPacketSize value to 256 */
2808         mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
2809                     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
2810                     MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
2811
2812         /* Enable Rx cache snoop */
2813         for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2814                 queue = port->rxqs[lrxq]->id;
2815                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2816                 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
2817                            MVPP2_SNOOP_BUF_HDR_MASK;
2818                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2819         }
2820 }
2821
2822 /* Enable/disable receiving packets */
2823 static void mvpp2_ingress_enable(struct mvpp2_port *port)
2824 {
2825         u32 val;
2826         int lrxq, queue;
2827
2828         for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2829                 queue = port->rxqs[lrxq]->id;
2830                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2831                 val &= ~MVPP2_RXQ_DISABLE_MASK;
2832                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2833         }
2834 }
2835
2836 static void mvpp2_ingress_disable(struct mvpp2_port *port)
2837 {
2838         u32 val;
2839         int lrxq, queue;
2840
2841         for (lrxq = 0; lrxq < rxq_number; lrxq++) {
2842                 queue = port->rxqs[lrxq]->id;
2843                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2844                 val |= MVPP2_RXQ_DISABLE_MASK;
2845                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2846         }
2847 }
2848
2849 /* Enable transmit via physical egress queue
2850  * - HW starts take descriptors from DRAM
2851  */
2852 static void mvpp2_egress_enable(struct mvpp2_port *port)
2853 {
2854         u32 qmap;
2855         int queue;
2856         int tx_port_num = mvpp2_egress_port(port);
2857
2858         /* Enable all initialized TXs. */
2859         qmap = 0;
2860         for (queue = 0; queue < txq_number; queue++) {
2861                 struct mvpp2_tx_queue *txq = port->txqs[queue];
2862
2863                 if (txq->descs != NULL)
2864                         qmap |= (1 << queue);
2865         }
2866
2867         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2868         mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
2869 }
2870
2871 /* Disable transmit via physical egress queue
2872  * - HW doesn't take descriptors from DRAM
2873  */
2874 static void mvpp2_egress_disable(struct mvpp2_port *port)
2875 {
2876         u32 reg_data;
2877         int delay;
2878         int tx_port_num = mvpp2_egress_port(port);
2879
2880         /* Issue stop command for active channels only */
2881         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2882         reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
2883                     MVPP2_TXP_SCHED_ENQ_MASK;
2884         if (reg_data != 0)
2885                 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
2886                             (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
2887
2888         /* Wait for all Tx activity to terminate. */
2889         delay = 0;
2890         do {
2891                 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
2892                         netdev_warn(port->dev,
2893                                     "Tx stop timed out, status=0x%08x\n",
2894                                     reg_data);
2895                         break;
2896                 }
2897                 mdelay(1);
2898                 delay++;
2899
2900                 /* Check port TX Command register that all
2901                  * Tx queues are stopped
2902                  */
2903                 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
2904         } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
2905 }
2906
2907 /* Rx descriptors helper methods */
2908
2909 /* Get number of Rx descriptors occupied by received packets */
2910 static inline int
2911 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
2912 {
2913         u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
2914
2915         return val & MVPP2_RXQ_OCCUPIED_MASK;
2916 }
2917
2918 /* Update Rx queue status with the number of occupied and available
2919  * Rx descriptor slots.
2920  */
2921 static inline void
2922 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
2923                         int used_count, int free_count)
2924 {
2925         /* Decrement the number of used descriptors and increment count
2926          * increment the number of free descriptors.
2927          */
2928         u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
2929
2930         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
2931 }
2932
2933 /* Get pointer to next RX descriptor to be processed by SW */
2934 static inline struct mvpp2_rx_desc *
2935 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
2936 {
2937         int rx_desc = rxq->next_desc_to_proc;
2938
2939         rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
2940         prefetch(rxq->descs + rxq->next_desc_to_proc);
2941         return rxq->descs + rx_desc;
2942 }
2943
2944 /* Set rx queue offset */
2945 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
2946                                  int prxq, int offset)
2947 {
2948         u32 val;
2949
2950         /* Convert offset from bytes to units of 32 bytes */
2951         offset = offset >> 5;
2952
2953         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2954         val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
2955
2956         /* Offset is in */
2957         val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
2958                     MVPP2_RXQ_PACKET_OFFSET_MASK);
2959
2960         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2961 }
2962
2963 /* Obtain BM cookie information from descriptor */
2964 static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
2965                                  struct mvpp2_rx_desc *rx_desc)
2966 {
2967         int cpu = smp_processor_id();
2968         int pool;
2969
2970         pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
2971                 MVPP2_RXD_BM_POOL_ID_MASK) >>
2972                 MVPP2_RXD_BM_POOL_ID_OFFS;
2973
2974         return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
2975                ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
2976 }
2977
2978 /* Tx descriptors helper methods */
2979
2980 /* Get number of Tx descriptors waiting to be transmitted by HW */
2981 static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
2982                                        struct mvpp2_tx_queue *txq)
2983 {
2984         u32 val;
2985
2986         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
2987         val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
2988
2989         return val & MVPP2_TXQ_PENDING_MASK;
2990 }
2991
2992 /* Get pointer to next Tx descriptor to be processed (send) by HW */
2993 static struct mvpp2_tx_desc *
2994 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
2995 {
2996         int tx_desc = txq->next_desc_to_proc;
2997
2998         txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
2999         return txq->descs + tx_desc;
3000 }
3001
3002 /* Update HW with number of aggregated Tx descriptors to be sent */
3003 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
3004 {
3005         /* aggregated access - relevant TXQ number is written in TX desc */
3006         mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
3007 }
3008
3009 /* Get number of sent descriptors and decrement counter.
3010  * The number of sent descriptors is returned.
3011  * Per-CPU access
3012  */
3013 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
3014                                            struct mvpp2_tx_queue *txq)
3015 {
3016         u32 val;
3017
3018         /* Reading status reg resets transmitted descriptor counter */
3019         val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
3020
3021         return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
3022                 MVPP2_TRANSMITTED_COUNT_OFFSET;
3023 }
3024
3025 static void mvpp2_txq_sent_counter_clear(void *arg)
3026 {
3027         struct mvpp2_port *port = arg;
3028         int queue;
3029
3030         for (queue = 0; queue < txq_number; queue++) {
3031                 int id = port->txqs[queue]->id;
3032
3033                 mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
3034         }
3035 }
3036
3037 /* Set max sizes for Tx queues */
3038 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
3039 {
3040         u32     val, size, mtu;
3041         int     txq, tx_port_num;
3042
3043         mtu = port->pkt_size * 8;
3044         if (mtu > MVPP2_TXP_MTU_MAX)
3045                 mtu = MVPP2_TXP_MTU_MAX;
3046
3047         /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
3048         mtu = 3 * mtu;
3049
3050         /* Indirect access to registers */
3051         tx_port_num = mvpp2_egress_port(port);
3052         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3053
3054         /* Set MTU */
3055         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
3056         val &= ~MVPP2_TXP_MTU_MAX;
3057         val |= mtu;
3058         mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
3059
3060         /* TXP token size and all TXQs token size must be larger that MTU */
3061         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
3062         size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
3063         if (size < mtu) {
3064                 size = mtu;
3065                 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
3066                 val |= size;
3067                 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
3068         }
3069
3070         for (txq = 0; txq < txq_number; txq++) {
3071                 val = mvpp2_read(port->priv,
3072                                  MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
3073                 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
3074
3075                 if (size < mtu) {
3076                         size = mtu;
3077                         val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
3078                         val |= size;
3079                         mvpp2_write(port->priv,
3080                                     MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
3081                                     val);
3082                 }
3083         }
3084 }
3085
3086 /* Free Tx queue skbuffs */
3087 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
3088                                 struct mvpp2_tx_queue *txq,
3089                                 struct mvpp2_txq_pcpu *txq_pcpu, int num)
3090 {
3091         int i;
3092
3093         for (i = 0; i < num; i++)
3094                 mvpp2_txq_inc_get(txq_pcpu);
3095 }
3096
3097 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
3098                                                         u32 cause)
3099 {
3100         int queue = fls(cause) - 1;
3101
3102         return port->rxqs[queue];
3103 }
3104
3105 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
3106                                                         u32 cause)
3107 {
3108         int queue = fls(cause) - 1;
3109
3110         return port->txqs[queue];
3111 }
3112
3113 /* Rx/Tx queue initialization/cleanup methods */
3114
3115 /* Allocate and initialize descriptors for aggr TXQ */
3116 static int mvpp2_aggr_txq_init(struct udevice *dev,
3117                                struct mvpp2_tx_queue *aggr_txq,
3118                                int desc_num, int cpu,
3119                                struct mvpp2 *priv)
3120 {
3121         u32 txq_dma;
3122
3123         /* Allocate memory for TX descriptors */
3124         aggr_txq->descs = buffer_loc.aggr_tx_descs;
3125         aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
3126         if (!aggr_txq->descs)
3127                 return -ENOMEM;
3128
3129         /* Make sure descriptor address is cache line size aligned  */
3130         BUG_ON(aggr_txq->descs !=
3131                PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3132
3133         aggr_txq->last_desc = aggr_txq->size - 1;
3134
3135         /* Aggr TXQ no reset WA */
3136         aggr_txq->next_desc_to_proc = mvpp2_read(priv,
3137                                                  MVPP2_AGGR_TXQ_INDEX_REG(cpu));
3138
3139         /* Set Tx descriptors queue starting address indirect
3140          * access
3141          */
3142         if (priv->hw_version == MVPP21)
3143                 txq_dma = aggr_txq->descs_dma;
3144         else
3145                 txq_dma = aggr_txq->descs_dma >>
3146                         MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
3147
3148         mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
3149         mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
3150
3151         return 0;
3152 }
3153
3154 /* Create a specified Rx queue */
3155 static int mvpp2_rxq_init(struct mvpp2_port *port,
3156                           struct mvpp2_rx_queue *rxq)
3157
3158 {
3159         u32 rxq_dma;
3160
3161         rxq->size = port->rx_ring_size;
3162
3163         /* Allocate memory for RX descriptors */
3164         rxq->descs = buffer_loc.rx_descs;
3165         rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
3166         if (!rxq->descs)
3167                 return -ENOMEM;
3168
3169         BUG_ON(rxq->descs !=
3170                PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3171
3172         rxq->last_desc = rxq->size - 1;
3173
3174         /* Zero occupied and non-occupied counters - direct access */
3175         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3176
3177         /* Set Rx descriptors queue starting address - indirect access */
3178         mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
3179         if (port->priv->hw_version == MVPP21)
3180                 rxq_dma = rxq->descs_dma;
3181         else
3182                 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
3183         mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
3184         mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
3185         mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
3186
3187         /* Set Offset */
3188         mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
3189
3190         /* Add number of descriptors ready for receiving packets */
3191         mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
3192
3193         return 0;
3194 }
3195
3196 /* Push packets received by the RXQ to BM pool */
3197 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
3198                                 struct mvpp2_rx_queue *rxq)
3199 {
3200         int rx_received, i;
3201
3202         rx_received = mvpp2_rxq_received(port, rxq->id);
3203         if (!rx_received)
3204                 return;
3205
3206         for (i = 0; i < rx_received; i++) {
3207                 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3208                 u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
3209
3210                 mvpp2_pool_refill(port, bm,
3211                                   mvpp2_rxdesc_dma_addr_get(port, rx_desc),
3212                                   mvpp2_rxdesc_cookie_get(port, rx_desc));
3213         }
3214         mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
3215 }
3216
3217 /* Cleanup Rx queue */
3218 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
3219                              struct mvpp2_rx_queue *rxq)
3220 {
3221         mvpp2_rxq_drop_pkts(port, rxq);
3222
3223         rxq->descs             = NULL;
3224         rxq->last_desc         = 0;
3225         rxq->next_desc_to_proc = 0;
3226         rxq->descs_dma         = 0;
3227
3228         /* Clear Rx descriptors queue starting address and size;
3229          * free descriptor number
3230          */
3231         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3232         mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
3233         mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
3234         mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
3235 }
3236
3237 /* Create and initialize a Tx queue */
3238 static int mvpp2_txq_init(struct mvpp2_port *port,
3239                           struct mvpp2_tx_queue *txq)
3240 {
3241         u32 val;
3242         int cpu, desc, desc_per_txq, tx_port_num;
3243         struct mvpp2_txq_pcpu *txq_pcpu;
3244
3245         txq->size = port->tx_ring_size;
3246
3247         /* Allocate memory for Tx descriptors */
3248         txq->descs = buffer_loc.tx_descs;
3249         txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
3250         if (!txq->descs)
3251                 return -ENOMEM;
3252
3253         /* Make sure descriptor address is cache line size aligned  */
3254         BUG_ON(txq->descs !=
3255                PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
3256
3257         txq->last_desc = txq->size - 1;
3258
3259         /* Set Tx descriptors queue starting address - indirect access */
3260         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3261         mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
3262         mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
3263                                              MVPP2_TXQ_DESC_SIZE_MASK);
3264         mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
3265         mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
3266                     txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
3267         val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
3268         val &= ~MVPP2_TXQ_PENDING_MASK;
3269         mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
3270
3271         /* Calculate base address in prefetch buffer. We reserve 16 descriptors
3272          * for each existing TXQ.
3273          * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
3274          * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
3275          */
3276         desc_per_txq = 16;
3277         desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
3278                (txq->log_id * desc_per_txq);
3279
3280         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
3281                     MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
3282                     MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
3283
3284         /* WRR / EJP configuration - indirect access */
3285         tx_port_num = mvpp2_egress_port(port);
3286         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3287
3288         val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
3289         val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
3290         val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
3291         val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
3292         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
3293
3294         val = MVPP2_TXQ_TOKEN_SIZE_MAX;
3295         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
3296                     val);
3297
3298         for_each_present_cpu(cpu) {
3299                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3300                 txq_pcpu->size = txq->size;
3301         }
3302
3303         return 0;
3304 }
3305
3306 /* Free allocated TXQ resources */
3307 static void mvpp2_txq_deinit(struct mvpp2_port *port,
3308                              struct mvpp2_tx_queue *txq)
3309 {
3310         txq->descs             = NULL;
3311         txq->last_desc         = 0;
3312         txq->next_desc_to_proc = 0;
3313         txq->descs_dma         = 0;
3314
3315         /* Set minimum bandwidth for disabled TXQs */
3316         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
3317
3318         /* Set Tx descriptors queue starting address and size */
3319         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3320         mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
3321         mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
3322 }
3323
3324 /* Cleanup Tx ports */
3325 static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
3326 {
3327         struct mvpp2_txq_pcpu *txq_pcpu;
3328         int delay, pending, cpu;
3329         u32 val;
3330
3331         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
3332         val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
3333         val |= MVPP2_TXQ_DRAIN_EN_MASK;
3334         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
3335
3336         /* The napi queue has been stopped so wait for all packets
3337          * to be transmitted.
3338          */
3339         delay = 0;
3340         do {
3341                 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
3342                         netdev_warn(port->dev,
3343                                     "port %d: cleaning queue %d timed out\n",
3344                                     port->id, txq->log_id);
3345                         break;
3346                 }
3347                 mdelay(1);
3348                 delay++;
3349
3350                 pending = mvpp2_txq_pend_desc_num_get(port, txq);
3351         } while (pending);
3352
3353         val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
3354         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
3355
3356         for_each_present_cpu(cpu) {
3357                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3358
3359                 /* Release all packets */
3360                 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
3361
3362                 /* Reset queue */
3363                 txq_pcpu->count = 0;
3364                 txq_pcpu->txq_put_index = 0;
3365                 txq_pcpu->txq_get_index = 0;
3366         }
3367 }
3368
3369 /* Cleanup all Tx queues */
3370 static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
3371 {
3372         struct mvpp2_tx_queue *txq;
3373         int queue;
3374         u32 val;
3375
3376         val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
3377
3378         /* Reset Tx ports and delete Tx queues */
3379         val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
3380         mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3381
3382         for (queue = 0; queue < txq_number; queue++) {
3383                 txq = port->txqs[queue];
3384                 mvpp2_txq_clean(port, txq);
3385                 mvpp2_txq_deinit(port, txq);
3386         }
3387
3388         mvpp2_txq_sent_counter_clear(port);
3389
3390         val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
3391         mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3392 }
3393
3394 /* Cleanup all Rx queues */
3395 static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
3396 {
3397         int queue;
3398
3399         for (queue = 0; queue < rxq_number; queue++)
3400                 mvpp2_rxq_deinit(port, port->rxqs[queue]);
3401 }
3402
3403 /* Init all Rx queues for port */
3404 static int mvpp2_setup_rxqs(struct mvpp2_port *port)
3405 {
3406         int queue, err;
3407
3408         for (queue = 0; queue < rxq_number; queue++) {
3409                 err = mvpp2_rxq_init(port, port->rxqs[queue]);
3410                 if (err)
3411                         goto err_cleanup;
3412         }
3413         return 0;
3414
3415 err_cleanup:
3416         mvpp2_cleanup_rxqs(port);
3417         return err;
3418 }
3419
3420 /* Init all tx queues for port */
3421 static int mvpp2_setup_txqs(struct mvpp2_port *port)
3422 {
3423         struct mvpp2_tx_queue *txq;
3424         int queue, err;
3425
3426         for (queue = 0; queue < txq_number; queue++) {
3427                 txq = port->txqs[queue];
3428                 err = mvpp2_txq_init(port, txq);
3429                 if (err)
3430                         goto err_cleanup;
3431         }
3432
3433         mvpp2_txq_sent_counter_clear(port);
3434         return 0;
3435
3436 err_cleanup:
3437         mvpp2_cleanup_txqs(port);
3438         return err;
3439 }
3440
3441 /* Adjust link */
3442 static void mvpp2_link_event(struct mvpp2_port *port)
3443 {
3444         struct phy_device *phydev = port->phy_dev;
3445         int status_change = 0;
3446         u32 val;
3447
3448         if (phydev->link) {
3449                 if ((port->speed != phydev->speed) ||
3450                     (port->duplex != phydev->duplex)) {
3451                         u32 val;
3452
3453                         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3454                         val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
3455                                  MVPP2_GMAC_CONFIG_GMII_SPEED |
3456                                  MVPP2_GMAC_CONFIG_FULL_DUPLEX |
3457                                  MVPP2_GMAC_AN_SPEED_EN |
3458                                  MVPP2_GMAC_AN_DUPLEX_EN);
3459
3460                         if (phydev->duplex)
3461                                 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
3462
3463                         if (phydev->speed == SPEED_1000)
3464                                 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
3465                         else if (phydev->speed == SPEED_100)
3466                                 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
3467
3468                         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3469
3470                         port->duplex = phydev->duplex;
3471                         port->speed  = phydev->speed;
3472                 }
3473         }
3474
3475         if (phydev->link != port->link) {
3476                 if (!phydev->link) {
3477                         port->duplex = -1;
3478                         port->speed = 0;
3479                 }
3480
3481                 port->link = phydev->link;
3482                 status_change = 1;
3483         }
3484
3485         if (status_change) {
3486                 if (phydev->link) {
3487                         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3488                         val |= (MVPP2_GMAC_FORCE_LINK_PASS |
3489                                 MVPP2_GMAC_FORCE_LINK_DOWN);
3490                         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
3491                         mvpp2_egress_enable(port);
3492                         mvpp2_ingress_enable(port);
3493                 } else {
3494                         mvpp2_ingress_disable(port);
3495                         mvpp2_egress_disable(port);
3496                 }
3497         }
3498 }
3499
3500 /* Main RX/TX processing routines */
3501
3502 /* Display more error info */
3503 static void mvpp2_rx_error(struct mvpp2_port *port,
3504                            struct mvpp2_rx_desc *rx_desc)
3505 {
3506         u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3507         size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3508
3509         switch (status & MVPP2_RXD_ERR_CODE_MASK) {
3510         case MVPP2_RXD_ERR_CRC:
3511                 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
3512                            status, sz);
3513                 break;
3514         case MVPP2_RXD_ERR_OVERRUN:
3515                 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
3516                            status, sz);
3517                 break;
3518         case MVPP2_RXD_ERR_RESOURCE:
3519                 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
3520                            status, sz);
3521                 break;
3522         }
3523 }
3524
3525 /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
3526 static int mvpp2_rx_refill(struct mvpp2_port *port,
3527                            struct mvpp2_bm_pool *bm_pool,
3528                            u32 bm, dma_addr_t dma_addr)
3529 {
3530         mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
3531         return 0;
3532 }
3533
3534 /* Set hw internals when starting port */
3535 static void mvpp2_start_dev(struct mvpp2_port *port)
3536 {
3537         mvpp2_gmac_max_rx_size_set(port);
3538         mvpp2_txp_max_tx_size_set(port);
3539
3540         mvpp2_port_enable(port);
3541 }
3542
3543 /* Set hw internals when stopping port */
3544 static void mvpp2_stop_dev(struct mvpp2_port *port)
3545 {
3546         /* Stop new packets from arriving to RXQs */
3547         mvpp2_ingress_disable(port);
3548
3549         mvpp2_egress_disable(port);
3550         mvpp2_port_disable(port);
3551 }
3552
3553 static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
3554 {
3555         struct phy_device *phy_dev;
3556
3557         if (!port->init || port->link == 0) {
3558                 phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
3559                                       port->phy_interface);
3560                 port->phy_dev = phy_dev;
3561                 if (!phy_dev) {
3562                         netdev_err(port->dev, "cannot connect to phy\n");
3563                         return -ENODEV;
3564                 }
3565                 phy_dev->supported &= PHY_GBIT_FEATURES;
3566                 phy_dev->advertising = phy_dev->supported;
3567
3568                 port->phy_dev = phy_dev;
3569                 port->link    = 0;
3570                 port->duplex  = 0;
3571                 port->speed   = 0;
3572
3573                 phy_config(phy_dev);
3574                 phy_startup(phy_dev);
3575                 if (!phy_dev->link) {
3576                         printf("%s: No link\n", phy_dev->dev->name);
3577                         return -1;
3578                 }
3579
3580                 port->init = 1;
3581         } else {
3582                 mvpp2_egress_enable(port);
3583                 mvpp2_ingress_enable(port);
3584         }
3585
3586         return 0;
3587 }
3588
3589 static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
3590 {
3591         unsigned char mac_bcast[ETH_ALEN] = {
3592                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3593         int err;
3594
3595         err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
3596         if (err) {
3597                 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
3598                 return err;
3599         }
3600         err = mvpp2_prs_mac_da_accept(port->priv, port->id,
3601                                       port->dev_addr, true);
3602         if (err) {
3603                 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
3604                 return err;
3605         }
3606         err = mvpp2_prs_def_flow(port);
3607         if (err) {
3608                 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
3609                 return err;
3610         }
3611
3612         /* Allocate the Rx/Tx queues */
3613         err = mvpp2_setup_rxqs(port);
3614         if (err) {
3615                 netdev_err(port->dev, "cannot allocate Rx queues\n");
3616                 return err;
3617         }
3618
3619         err = mvpp2_setup_txqs(port);
3620         if (err) {
3621                 netdev_err(port->dev, "cannot allocate Tx queues\n");
3622                 return err;
3623         }
3624
3625         err = mvpp2_phy_connect(dev, port);
3626         if (err < 0)
3627                 return err;
3628
3629         mvpp2_link_event(port);
3630
3631         mvpp2_start_dev(port);
3632
3633         return 0;
3634 }
3635
3636 /* No Device ops here in U-Boot */
3637
3638 /* Driver initialization */
3639
3640 static void mvpp2_port_power_up(struct mvpp2_port *port)
3641 {
3642         mvpp2_port_mii_set(port);
3643         mvpp2_port_periodic_xon_disable(port);
3644         mvpp2_port_fc_adv_enable(port);
3645         mvpp2_port_reset(port);
3646 }
3647
3648 /* Initialize port HW */
3649 static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
3650 {
3651         struct mvpp2 *priv = port->priv;
3652         struct mvpp2_txq_pcpu *txq_pcpu;
3653         int queue, cpu, err;
3654
3655         if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
3656                 return -EINVAL;
3657
3658         /* Disable port */
3659         mvpp2_egress_disable(port);
3660         mvpp2_port_disable(port);
3661
3662         port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
3663                                   GFP_KERNEL);
3664         if (!port->txqs)
3665                 return -ENOMEM;
3666
3667         /* Associate physical Tx queues to this port and initialize.
3668          * The mapping is predefined.
3669          */
3670         for (queue = 0; queue < txq_number; queue++) {
3671                 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
3672                 struct mvpp2_tx_queue *txq;
3673
3674                 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
3675                 if (!txq)
3676                         return -ENOMEM;
3677
3678                 txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
3679                                          GFP_KERNEL);
3680                 if (!txq->pcpu)
3681                         return -ENOMEM;
3682
3683                 txq->id = queue_phy_id;
3684                 txq->log_id = queue;
3685                 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
3686                 for_each_present_cpu(cpu) {
3687                         txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
3688                         txq_pcpu->cpu = cpu;
3689                 }
3690
3691                 port->txqs[queue] = txq;
3692         }
3693
3694         port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
3695                                   GFP_KERNEL);
3696         if (!port->rxqs)
3697                 return -ENOMEM;
3698
3699         /* Allocate and initialize Rx queue for this port */
3700         for (queue = 0; queue < rxq_number; queue++) {
3701                 struct mvpp2_rx_queue *rxq;
3702
3703                 /* Map physical Rx queue to port's logical Rx queue */
3704                 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
3705                 if (!rxq)
3706                         return -ENOMEM;
3707                 /* Map this Rx queue to a physical queue */
3708                 rxq->id = port->first_rxq + queue;
3709                 rxq->port = port->id;
3710                 rxq->logic_rxq = queue;
3711
3712                 port->rxqs[queue] = rxq;
3713         }
3714
3715         /* Configure Rx queue group interrupt for this port */
3716         mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
3717
3718         /* Create Rx descriptor rings */
3719         for (queue = 0; queue < rxq_number; queue++) {
3720                 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
3721
3722                 rxq->size = port->rx_ring_size;
3723                 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
3724                 rxq->time_coal = MVPP2_RX_COAL_USEC;
3725         }
3726
3727         mvpp2_ingress_disable(port);
3728
3729         /* Port default configuration */
3730         mvpp2_defaults_set(port);
3731
3732         /* Port's classifier configuration */
3733         mvpp2_cls_oversize_rxq_set(port);
3734         mvpp2_cls_port_config(port);
3735
3736         /* Provide an initial Rx packet size */
3737         port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
3738
3739         /* Initialize pools for swf */
3740         err = mvpp2_swf_bm_pool_init(port);
3741         if (err)
3742                 return err;
3743
3744         return 0;
3745 }
3746
3747 /* Ports initialization */
3748 static int mvpp2_port_probe(struct udevice *dev,
3749                             struct mvpp2_port *port,
3750                             int port_node,
3751                             struct mvpp2 *priv,
3752                             int *next_first_rxq)
3753 {
3754         int phy_node;
3755         u32 id;
3756         u32 phyaddr;
3757         const char *phy_mode_str;
3758         int phy_mode = -1;
3759         int priv_common_regs_num = 2;
3760         int err;
3761
3762         phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
3763         if (phy_node < 0) {
3764                 dev_err(&pdev->dev, "missing phy\n");
3765                 return -ENODEV;
3766         }
3767
3768         phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
3769         if (phy_mode_str)
3770                 phy_mode = phy_get_interface_by_name(phy_mode_str);
3771         if (phy_mode == -1) {
3772                 dev_err(&pdev->dev, "incorrect phy mode\n");
3773                 return -EINVAL;
3774         }
3775
3776         id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
3777         if (id == -1) {
3778                 dev_err(&pdev->dev, "missing port-id value\n");
3779                 return -EINVAL;
3780         }
3781
3782         phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
3783
3784         port->priv = priv;
3785         port->id = id;
3786         port->first_rxq = *next_first_rxq;
3787         port->phy_node = phy_node;
3788         port->phy_interface = phy_mode;
3789         port->phyaddr = phyaddr;
3790
3791         if (priv->hw_version == MVPP21) {
3792                 port->base = (void __iomem *)dev_get_addr_index(
3793                         dev->parent, priv_common_regs_num + id);
3794                 if (IS_ERR(port->base))
3795                         return PTR_ERR(port->base);
3796         } else {
3797                 u32 gop_id;
3798
3799                 gop_id = fdtdec_get_int(gd->fdt_blob, port_node,
3800                                         "gop-port-id", -1);
3801                 if (id == -1) {
3802                         dev_err(&pdev->dev, "missing gop-port-id value\n");
3803                         return -EINVAL;
3804                 }
3805
3806                 port->base = priv->iface_base + MVPP22_PORT_BASE +
3807                         gop_id * MVPP22_PORT_OFFSET;
3808         }
3809
3810         port->tx_ring_size = MVPP2_MAX_TXD;
3811         port->rx_ring_size = MVPP2_MAX_RXD;
3812
3813         err = mvpp2_port_init(dev, port);
3814         if (err < 0) {
3815                 dev_err(&pdev->dev, "failed to init port %d\n", id);
3816                 return err;
3817         }
3818         mvpp2_port_power_up(port);
3819
3820         /* Increment the first Rx queue number to be used by the next port */
3821         *next_first_rxq += CONFIG_MV_ETH_RXQ;
3822         priv->port_list[id] = port;
3823         return 0;
3824 }
3825
3826 /* Initialize decoding windows */
3827 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
3828                                     struct mvpp2 *priv)
3829 {
3830         u32 win_enable;
3831         int i;
3832
3833         for (i = 0; i < 6; i++) {
3834                 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
3835                 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
3836
3837                 if (i < 4)
3838                         mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
3839         }
3840
3841         win_enable = 0;
3842
3843         for (i = 0; i < dram->num_cs; i++) {
3844                 const struct mbus_dram_window *cs = dram->cs + i;
3845
3846                 mvpp2_write(priv, MVPP2_WIN_BASE(i),
3847                             (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
3848                             dram->mbus_dram_target_id);
3849
3850                 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
3851                             (cs->size - 1) & 0xffff0000);
3852
3853                 win_enable |= (1 << i);
3854         }
3855
3856         mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
3857 }
3858
3859 /* Initialize Rx FIFO's */
3860 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
3861 {
3862         int port;
3863
3864         for (port = 0; port < MVPP2_MAX_PORTS; port++) {
3865                 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
3866                             MVPP2_RX_FIFO_PORT_DATA_SIZE);
3867                 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
3868                             MVPP2_RX_FIFO_PORT_ATTR_SIZE);
3869         }
3870
3871         mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
3872                     MVPP2_RX_FIFO_PORT_MIN_PKT);
3873         mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
3874 }
3875
3876 /* Initialize network controller common part HW */
3877 static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
3878 {
3879         const struct mbus_dram_target_info *dram_target_info;
3880         int err, i;
3881         u32 val;
3882
3883         /* Checks for hardware constraints (U-Boot uses only one rxq) */
3884         if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
3885                 dev_err(&pdev->dev, "invalid queue size parameter\n");
3886                 return -EINVAL;
3887         }
3888
3889         /* MBUS windows configuration */
3890         dram_target_info = mvebu_mbus_dram_info();
3891         if (dram_target_info)
3892                 mvpp2_conf_mbus_windows(dram_target_info, priv);
3893
3894         /* Disable HW PHY polling */
3895         val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
3896         val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
3897         writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
3898
3899         /* Allocate and initialize aggregated TXQs */
3900         priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
3901                                        sizeof(struct mvpp2_tx_queue),
3902                                        GFP_KERNEL);
3903         if (!priv->aggr_txqs)
3904                 return -ENOMEM;
3905
3906         for_each_present_cpu(i) {
3907                 priv->aggr_txqs[i].id = i;
3908                 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
3909                 err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
3910                                           MVPP2_AGGR_TXQ_SIZE, i, priv);
3911                 if (err < 0)
3912                         return err;
3913         }
3914
3915         /* Rx Fifo Init */
3916         mvpp2_rx_fifo_init(priv);
3917
3918         /* Reset Rx queue group interrupt configuration */
3919         for (i = 0; i < MVPP2_MAX_PORTS; i++)
3920                 mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
3921                             CONFIG_MV_ETH_RXQ);
3922
3923         writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
3924                priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
3925
3926         /* Allow cache snoop when transmiting packets */
3927         mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
3928
3929         /* Buffer Manager initialization */
3930         err = mvpp2_bm_init(dev, priv);
3931         if (err < 0)
3932                 return err;
3933
3934         /* Parser default initialization */
3935         err = mvpp2_prs_default_init(dev, priv);
3936         if (err < 0)
3937                 return err;
3938
3939         /* Classifier default initialization */
3940         mvpp2_cls_init(priv);
3941
3942         return 0;
3943 }
3944
3945 /* SMI / MDIO functions */
3946
3947 static int smi_wait_ready(struct mvpp2 *priv)
3948 {
3949         u32 timeout = MVPP2_SMI_TIMEOUT;
3950         u32 smi_reg;
3951
3952         /* wait till the SMI is not busy */
3953         do {
3954                 /* read smi register */
3955                 smi_reg = readl(priv->lms_base + MVPP2_SMI);
3956                 if (timeout-- == 0) {
3957                         printf("Error: SMI busy timeout\n");
3958                         return -EFAULT;
3959                 }
3960         } while (smi_reg & MVPP2_SMI_BUSY);
3961
3962         return 0;
3963 }
3964
3965 /*
3966  * mpp2_mdio_read - miiphy_read callback function.
3967  *
3968  * Returns 16bit phy register value, or 0xffff on error
3969  */
3970 static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
3971 {
3972         struct mvpp2 *priv = bus->priv;
3973         u32 smi_reg;
3974         u32 timeout;
3975
3976         /* check parameters */
3977         if (addr > MVPP2_PHY_ADDR_MASK) {
3978                 printf("Error: Invalid PHY address %d\n", addr);
3979                 return -EFAULT;
3980         }
3981
3982         if (reg > MVPP2_PHY_REG_MASK) {
3983                 printf("Err: Invalid register offset %d\n", reg);
3984                 return -EFAULT;
3985         }
3986
3987         /* wait till the SMI is not busy */
3988         if (smi_wait_ready(priv) < 0)
3989                 return -EFAULT;
3990
3991         /* fill the phy address and regiser offset and read opcode */
3992         smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
3993                 | (reg << MVPP2_SMI_REG_ADDR_OFFS)
3994                 | MVPP2_SMI_OPCODE_READ;
3995
3996         /* write the smi register */
3997         writel(smi_reg, priv->lms_base + MVPP2_SMI);
3998
3999         /* wait till read value is ready */
4000         timeout = MVPP2_SMI_TIMEOUT;
4001
4002         do {
4003                 /* read smi register */
4004                 smi_reg = readl(priv->lms_base + MVPP2_SMI);
4005                 if (timeout-- == 0) {
4006                         printf("Err: SMI read ready timeout\n");
4007                         return -EFAULT;
4008                 }
4009         } while (!(smi_reg & MVPP2_SMI_READ_VALID));
4010
4011         /* Wait for the data to update in the SMI register */
4012         for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
4013                 ;
4014
4015         return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
4016 }
4017
4018 /*
4019  * mpp2_mdio_write - miiphy_write callback function.
4020  *
4021  * Returns 0 if write succeed, -EINVAL on bad parameters
4022  * -ETIME on timeout
4023  */
4024 static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
4025                            u16 value)
4026 {
4027         struct mvpp2 *priv = bus->priv;
4028         u32 smi_reg;
4029
4030         /* check parameters */
4031         if (addr > MVPP2_PHY_ADDR_MASK) {
4032                 printf("Error: Invalid PHY address %d\n", addr);
4033                 return -EFAULT;
4034         }
4035
4036         if (reg > MVPP2_PHY_REG_MASK) {
4037                 printf("Err: Invalid register offset %d\n", reg);
4038                 return -EFAULT;
4039         }
4040
4041         /* wait till the SMI is not busy */
4042         if (smi_wait_ready(priv) < 0)
4043                 return -EFAULT;
4044
4045         /* fill the phy addr and reg offset and write opcode and data */
4046         smi_reg = value << MVPP2_SMI_DATA_OFFS;
4047         smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
4048                 | (reg << MVPP2_SMI_REG_ADDR_OFFS);
4049         smi_reg &= ~MVPP2_SMI_OPCODE_READ;
4050
4051         /* write the smi register */
4052         writel(smi_reg, priv->lms_base + MVPP2_SMI);
4053
4054         return 0;
4055 }
4056
4057 static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
4058 {
4059         struct mvpp2_port *port = dev_get_priv(dev);
4060         struct mvpp2_rx_desc *rx_desc;
4061         struct mvpp2_bm_pool *bm_pool;
4062         dma_addr_t dma_addr;
4063         u32 bm, rx_status;
4064         int pool, rx_bytes, err;
4065         int rx_received;
4066         struct mvpp2_rx_queue *rxq;
4067         u32 cause_rx_tx, cause_rx, cause_misc;
4068         u8 *data;
4069
4070         cause_rx_tx = mvpp2_read(port->priv,
4071                                  MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
4072         cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
4073         cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
4074         if (!cause_rx_tx && !cause_misc)
4075                 return 0;
4076
4077         cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4078
4079         /* Process RX packets */
4080         cause_rx |= port->pending_cause_rx;
4081         rxq = mvpp2_get_rx_queue(port, cause_rx);
4082
4083         /* Get number of received packets and clamp the to-do */
4084         rx_received = mvpp2_rxq_received(port, rxq->id);
4085
4086         /* Return if no packets are received */
4087         if (!rx_received)
4088                 return 0;
4089
4090         rx_desc = mvpp2_rxq_next_desc_get(rxq);
4091         rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
4092         rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
4093         rx_bytes -= MVPP2_MH_SIZE;
4094         dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
4095
4096         bm = mvpp2_bm_cookie_build(port, rx_desc);
4097         pool = mvpp2_bm_cookie_pool_get(bm);
4098         bm_pool = &port->priv->bm_pools[pool];
4099
4100         /* In case of an error, release the requested buffer pointer
4101          * to the Buffer Manager. This request process is controlled
4102          * by the hardware, and the information about the buffer is
4103          * comprised by the RX descriptor.
4104          */
4105         if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
4106                 mvpp2_rx_error(port, rx_desc);
4107                 /* Return the buffer to the pool */
4108                 mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
4109                 return 0;
4110         }
4111
4112         err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
4113         if (err) {
4114                 netdev_err(port->dev, "failed to refill BM pools\n");
4115                 return 0;
4116         }
4117
4118         /* Update Rx queue management counters */
4119         mb();
4120         mvpp2_rxq_status_update(port, rxq->id, 1, 1);
4121
4122         /* give packet to stack - skip on first n bytes */
4123         data = (u8 *)dma_addr + 2 + 32;
4124
4125         if (rx_bytes <= 0)
4126                 return 0;
4127
4128         /*
4129          * No cache invalidation needed here, since the rx_buffer's are
4130          * located in a uncached memory region
4131          */
4132         *packetp = data;
4133
4134         return rx_bytes;
4135 }
4136
4137 /* Drain Txq */
4138 static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4139                             int enable)
4140 {
4141         u32 val;
4142
4143         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4144         val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
4145         if (enable)
4146                 val |= MVPP2_TXQ_DRAIN_EN_MASK;
4147         else
4148                 val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
4149         mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
4150 }
4151
4152 static int mvpp2_send(struct udevice *dev, void *packet, int length)
4153 {
4154         struct mvpp2_port *port = dev_get_priv(dev);
4155         struct mvpp2_tx_queue *txq, *aggr_txq;
4156         struct mvpp2_tx_desc *tx_desc;
4157         int tx_done;
4158         int timeout;
4159
4160         txq = port->txqs[0];
4161         aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
4162
4163         /* Get a descriptor for the first part of the packet */
4164         tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4165         mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4166         mvpp2_txdesc_size_set(port, tx_desc, length);
4167         mvpp2_txdesc_offset_set(port, tx_desc,
4168                                 (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
4169         mvpp2_txdesc_dma_addr_set(port, tx_desc,
4170                                   (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
4171         /* First and Last descriptor */
4172         mvpp2_txdesc_cmd_set(port, tx_desc,
4173                              MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
4174                              | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
4175
4176         /* Flush tx data */
4177         flush_dcache_range((unsigned long)packet,
4178                            (unsigned long)packet + ALIGN(length, PKTALIGN));
4179
4180         /* Enable transmit */
4181         mb();
4182         mvpp2_aggr_txq_pend_desc_add(port, 1);
4183
4184         mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
4185
4186         timeout = 0;
4187         do {
4188                 if (timeout++ > 10000) {
4189                         printf("timeout: packet not sent from aggregated to phys TXQ\n");
4190                         return 0;
4191                 }
4192                 tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
4193         } while (tx_done);
4194
4195         /* Enable TXQ drain */
4196         mvpp2_txq_drain(port, txq, 1);
4197
4198         timeout = 0;
4199         do {
4200                 if (timeout++ > 10000) {
4201                         printf("timeout: packet not sent\n");
4202                         return 0;
4203                 }
4204                 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
4205         } while (!tx_done);
4206
4207         /* Disable TXQ drain */
4208         mvpp2_txq_drain(port, txq, 0);
4209
4210         return 0;
4211 }
4212
4213 static int mvpp2_start(struct udevice *dev)
4214 {
4215         struct eth_pdata *pdata = dev_get_platdata(dev);
4216         struct mvpp2_port *port = dev_get_priv(dev);
4217
4218         /* Load current MAC address */
4219         memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
4220
4221         /* Reconfigure parser accept the original MAC address */
4222         mvpp2_prs_update_mac_da(port, port->dev_addr);
4223
4224         mvpp2_port_power_up(port);
4225
4226         mvpp2_open(dev, port);
4227
4228         return 0;
4229 }
4230
4231 static void mvpp2_stop(struct udevice *dev)
4232 {
4233         struct mvpp2_port *port = dev_get_priv(dev);
4234
4235         mvpp2_stop_dev(port);
4236         mvpp2_cleanup_rxqs(port);
4237         mvpp2_cleanup_txqs(port);
4238 }
4239
4240 static int mvpp2_probe(struct udevice *dev)
4241 {
4242         struct mvpp2_port *port = dev_get_priv(dev);
4243         struct mvpp2 *priv = dev_get_priv(dev->parent);
4244         int err;
4245
4246         /* Initialize network controller */
4247         err = mvpp2_init(dev, priv);
4248         if (err < 0) {
4249                 dev_err(&pdev->dev, "failed to initialize controller\n");
4250                 return err;
4251         }
4252
4253         return mvpp2_port_probe(dev, port, dev_of_offset(dev), priv,
4254                                 &buffer_loc.first_rxq);
4255 }
4256
4257 static const struct eth_ops mvpp2_ops = {
4258         .start          = mvpp2_start,
4259         .send           = mvpp2_send,
4260         .recv           = mvpp2_recv,
4261         .stop           = mvpp2_stop,
4262 };
4263
4264 static struct driver mvpp2_driver = {
4265         .name   = "mvpp2",
4266         .id     = UCLASS_ETH,
4267         .probe  = mvpp2_probe,
4268         .ops    = &mvpp2_ops,
4269         .priv_auto_alloc_size = sizeof(struct mvpp2_port),
4270         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
4271 };
4272
4273 /*
4274  * Use a MISC device to bind the n instances (child nodes) of the
4275  * network base controller in UCLASS_ETH.
4276  */
4277 static int mvpp2_base_probe(struct udevice *dev)
4278 {
4279         struct mvpp2 *priv = dev_get_priv(dev);
4280         struct mii_dev *bus;
4281         void *bd_space;
4282         u32 size = 0;
4283         int i;
4284
4285         /* Save hw-version */
4286         priv->hw_version = dev_get_driver_data(dev);
4287
4288         /*
4289          * U-Boot special buffer handling:
4290          *
4291          * Allocate buffer area for descs and rx_buffers. This is only
4292          * done once for all interfaces. As only one interface can
4293          * be active. Make this area DMA-safe by disabling the D-cache
4294          */
4295
4296         /* Align buffer area for descs and rx_buffers to 1MiB */
4297         bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
4298         mmu_set_region_dcache_behaviour((unsigned long)bd_space,
4299                                         BD_SPACE, DCACHE_OFF);
4300
4301         buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
4302         size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
4303
4304         buffer_loc.tx_descs =
4305                 (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
4306         size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
4307
4308         buffer_loc.rx_descs =
4309                 (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
4310         size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
4311
4312         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4313                 buffer_loc.bm_pool[i] =
4314                         (unsigned long *)((unsigned long)bd_space + size);
4315                 if (priv->hw_version == MVPP21)
4316                         size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u32);
4317                 else
4318                         size += MVPP2_BM_POOL_SIZE_MAX * 2 * sizeof(u64);
4319         }
4320
4321         for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
4322                 buffer_loc.rx_buffer[i] =
4323                         (unsigned long *)((unsigned long)bd_space + size);
4324                 size += RX_BUFFER_SIZE;
4325         }
4326
4327         /* Save base addresses for later use */
4328         priv->base = (void *)dev_get_addr_index(dev, 0);
4329         if (IS_ERR(priv->base))
4330                 return PTR_ERR(priv->base);
4331
4332         if (priv->hw_version == MVPP21) {
4333                 priv->lms_base = (void *)dev_get_addr_index(dev, 1);
4334                 if (IS_ERR(priv->lms_base))
4335                         return PTR_ERR(priv->lms_base);
4336         } else {
4337                 priv->iface_base = (void *)dev_get_addr_index(dev, 1);
4338                 if (IS_ERR(priv->iface_base))
4339                         return PTR_ERR(priv->iface_base);
4340         }
4341
4342         /* Finally create and register the MDIO bus driver */
4343         bus = mdio_alloc();
4344         if (!bus) {
4345                 printf("Failed to allocate MDIO bus\n");
4346                 return -ENOMEM;
4347         }
4348
4349         bus->read = mpp2_mdio_read;
4350         bus->write = mpp2_mdio_write;
4351         snprintf(bus->name, sizeof(bus->name), dev->name);
4352         bus->priv = (void *)priv;
4353         priv->bus = bus;
4354
4355         return mdio_register(bus);
4356 }
4357
4358 static int mvpp2_base_bind(struct udevice *parent)
4359 {
4360         const void *blob = gd->fdt_blob;
4361         int node = dev_of_offset(parent);
4362         struct uclass_driver *drv;
4363         struct udevice *dev;
4364         struct eth_pdata *plat;
4365         char *name;
4366         int subnode;
4367         u32 id;
4368
4369         /* Lookup eth driver */
4370         drv = lists_uclass_lookup(UCLASS_ETH);
4371         if (!drv) {
4372                 puts("Cannot find eth driver\n");
4373                 return -ENOENT;
4374         }
4375
4376         fdt_for_each_subnode(subnode, blob, node) {
4377                 /* Skip disabled ports */
4378                 if (!fdtdec_get_is_enabled(blob, subnode))
4379                         continue;
4380
4381                 plat = calloc(1, sizeof(*plat));
4382                 if (!plat)
4383                         return -ENOMEM;
4384
4385                 id = fdtdec_get_int(blob, subnode, "port-id", -1);
4386
4387                 name = calloc(1, 16);
4388                 sprintf(name, "mvpp2-%d", id);
4389
4390                 /* Create child device UCLASS_ETH and bind it */
4391                 device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
4392                 dev_set_of_offset(dev, subnode);
4393         }
4394
4395         return 0;
4396 }
4397
4398 static const struct udevice_id mvpp2_ids[] = {
4399         {
4400                 .compatible = "marvell,armada-375-pp2",
4401                 .data = MVPP21,
4402         },
4403         { }
4404 };
4405
4406 U_BOOT_DRIVER(mvpp2_base) = {
4407         .name   = "mvpp2_base",
4408         .id     = UCLASS_MISC,
4409         .of_match = mvpp2_ids,
4410         .bind   = mvpp2_base_bind,
4411         .probe  = mvpp2_base_probe,
4412         .priv_auto_alloc_size = sizeof(struct mvpp2),
4413 };