1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip Sparx5 Switch driver
3  *
4  * Copyright (c) 2023 Microchip Technology Inc. and its subsidiaries.
5  */
6 
7 #include "sparx5_main_regs.h"
8 #include "sparx5_main.h"
9 
10 static int sparx5_policer_service_conf_set(struct sparx5 *sparx5,
11 					   struct sparx5_policer *pol)
12 {
13 	u32 idx, pup_tokens, max_pup_tokens, burst, thres;
14 	struct sparx5_sdlb_group *g;
15 	u64 rate;
16 
17 	g = &sdlb_groups[pol->group];
18 	idx = pol->idx;
19 
20 	rate = pol->rate * 1000;
21 	burst = pol->burst;
22 
23 	pup_tokens = sparx5_sdlb_pup_token_get(sparx5, g->pup_interval, rate);
24 	max_pup_tokens =
25 		sparx5_sdlb_pup_token_get(sparx5, g->pup_interval, g->max_rate);
26 
27 	thres = DIV_ROUND_UP(burst, g->min_burst);
28 
29 	spx5_wr(ANA_AC_SDLB_PUP_TOKENS_PUP_TOKENS_SET(pup_tokens), sparx5,
30 		ANA_AC_SDLB_PUP_TOKENS(idx, 0));
31 
32 	spx5_rmw(ANA_AC_SDLB_INH_CTRL_PUP_TOKENS_MAX_SET(max_pup_tokens),
33 		 ANA_AC_SDLB_INH_CTRL_PUP_TOKENS_MAX, sparx5,
34 		 ANA_AC_SDLB_INH_CTRL(idx, 0));
35 
36 	spx5_rmw(ANA_AC_SDLB_THRES_THRES_SET(thres), ANA_AC_SDLB_THRES_THRES,
37 		 sparx5, ANA_AC_SDLB_THRES(idx, 0));
38 
39 	return 0;
40 }
41 
42 int sparx5_policer_conf_set(struct sparx5 *sparx5, struct sparx5_policer *pol)
43 {
44 	/* More policer types will be added later */
45 	switch (pol->type) {
46 	case SPX5_POL_SERVICE:
47 		return sparx5_policer_service_conf_set(sparx5, pol);
48 	default:
49 		break;
50 	}
51 
52 	return 0;
53 }
54