/* SPDX-License-Identifier: GPL-2.0-only */ /* * Copyright (C) 2002 Roman Zippel */ %{ #include #include #include #include #include #include #include "lkc.h" #include "internal.h" #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt) #define PRINTD 0x0001 #define DEBUG_PARSE 0x0002 int cdebug = PRINTD; static void yyerror(const char *err); static void zconfprint(const char *err, ...); static void zconf_error(const char *err, ...); static bool zconf_endtoken(const char *tokenname, const char *expected_tokenname); struct symbol *symbol_hash[SYMBOL_HASHSIZE]; struct menu *current_menu, *current_entry; %} %union { char *string; struct symbol *symbol; struct expr *expr; struct menu *menu; enum symbol_type type; enum variable_flavor flavor; } %token T_HELPTEXT %token T_WORD %token T_WORD_QUOTE %token T_BOOL %token T_CHOICE %token T_CLOSE_PAREN %token T_COLON_EQUAL %token T_COMMENT %token T_CONFIG %token T_DEFAULT %token T_DEF_BOOL %token T_DEF_TRISTATE %token T_DEPENDS %token T_ENDCHOICE %token T_ENDIF %token T_ENDMENU %token T_HELP %token T_HEX %token T_IF %token T_IMPLY %token T_INT %token T_MAINMENU %token T_MENU %token T_MENUCONFIG %token T_MODULES %token T_ON %token T_OPEN_PAREN %token T_OPTIONAL %token T_PLUS_EQUAL %token T_PROMPT %token T_RANGE %token T_RESET %token T_SELECT %token T_SOURCE %token T_STRING %token T_TRISTATE %token T_VISIBLE %token T_EOL %token T_ASSIGN_VAL %left T_OR %left T_AND %left T_EQUAL T_UNEQUAL %left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL %nonassoc T_NOT %type nonconst_symbol %type symbol %type type logic_type default %type expr %type if_expr %type end %type if_entry menu_entry choice_entry %type word_opt assign_val %type assign_op %destructor { fprintf(stderr, "%s:%d: missing end statement for this entry\n", $$->file->name, $$->lineno); if (current_menu == $$) menu_end_menu(); } if_entry menu_entry choice_entry %% input: mainmenu_stmt stmt_list | stmt_list; /* mainmenu entry */ mainmenu_stmt: T_MAINMENU T_WORD_QUOTE T_EOL { menu_add_prompt(P_MENU, $2, NULL); }; stmt_list: /* empty */ | stmt_list assignment_stmt | stmt_list choice_stmt | stmt_list comment_stmt | stmt_list config_stmt | stmt_list if_stmt | stmt_list menu_stmt | stmt_list menuconfig_stmt | stmt_list source_stmt | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); } | stmt_list error T_EOL { zconf_error("invalid statement"); } ; stmt_list_in_choice: /* empty */ | stmt_list_in_choice comment_stmt | stmt_list_in_choice config_stmt | stmt_list_in_choice if_stmt_in_choice | stmt_list_in_choice error T_EOL { zconf_error("invalid statement"); } ; /* config/menuconfig entry */ config_entry_start: T_CONFIG nonconst_symbol T_EOL { $2->flags |= SYMBOL_OPTIONAL; menu_add_entry($2); printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name); }; config_stmt: config_entry_start config_option_list { printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno()); }; menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL { $2->flags |= SYMBOL_OPTIONAL; menu_add_entry($2); printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name); }; menuconfig_stmt: menuconfig_entry_start config_option_list { if (current_entry->prompt) current_entry->prompt->type = P_MENU; else zconfprint("warning: menuconfig statement without prompt"); printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno()); }; config_option_list: /* empty */ | config_option_list config_option | config_option_list depends | config_option_list help ; config_option: type prompt_stmt_opt T_EOL { menu_set_type($1); printd(DEBUG_PARSE, "%s:%d:type(%u)\n", zconf_curname(), zconf_lineno(), $1); }; config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL { menu_add_prompt(P_PROMPT, $2, $3); printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno()); }; config_option: default expr if_expr T_EOL { menu_add_expr(P_DEFAULT, $2, $3); if ($1 != S_UNKNOWN) menu_set_type($1); printd(DEBUG_PARSE, "%s:%d:default(%u)\n", zconf_curname(), zconf_lineno(), $1); }; config_option: T_SELECT nonconst_symbol if_expr T_EOL { menu_add_symbol(P_SELECT, $2, $3); printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno()); }; config_option: T_IMPLY nonconst_symbol if_expr T_EOL { menu_add_symbol(P_IMPLY, $2, $3); printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno()); }; config_option: T_RANGE symbol symbol if_expr T_EOL { menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4); printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno()); }; config_option: T_MODULES T_EOL { if (modules_sym) zconf_error("symbol '%s' redefines option 'modules' already defined by symbol '%s'", current_entry->sym->name, modules_sym->name); modules_sym = current_entry->sym; }; /* choice entry */ choice: T_CHOICE word_opt T_EOL { struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE); sym->flags |= SYMBOL_NO_WRITE; menu_add_entry(sym); menu_add_expr(P_CHOICE, NULL, NULL); free($2); printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno()); }; choice_entry: choice choice_option_list { $$ = menu_add_menu(); }; choice_end: end { if (zconf_endtoken($1, "choice")) { menu_end_menu(); printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno()); } }; choice_stmt: choice_entry stmt_list_in_choice choice_end ; choice_option_list: /* empty */ | choice_option_list choice_option | choice_option_list depends | choice_option_list help ; choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL { menu_add_prompt(P_PROMPT, $2, $3); printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno()); }; choice_option: logic_type prompt_stmt_opt T_EOL { menu_set_type($1); printd(DEBUG_PARSE, "%s:%d:type(%u)\n", zconf_curname(), zconf_lineno(), $1); }; choice_option: T_OPTIONAL T_EOL { current_entry->sym->flags |= SYMBOL_OPTIONAL; printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno()); }; choice_option: T_RESET if_expr T_EOL { menu_add_prop(P_RESET, NULL, $2); }; choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL { menu_add_symbol(P_DEFAULT, $2, $3); printd(DEBUG_PARSE, "%s:%d:default\n", zconf_curname(), zconf_lineno()); }; type: logic_type | T_INT { $$ = S_INT; } | T_HEX { $$ = S_HEX; } | T_STRING { $$ = S_STRING; } logic_type: T_BOOL { $$ = S_BOOLEAN; } | T_TRISTATE { $$ = S_TRISTATE; } default: T_DEFAULT { $$ = S_UNKNOWN; } | T_DEF_BOOL { $$ = S_BOOLEAN; } | T_DEF_TRISTATE { $$ = S_TRISTATE; } /* if entry */ if_entry: T_IF expr T_EOL { printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno()); menu_add_entry(NULL); menu_add_dep($2); $$ = menu_add_menu(); }; if_end: end { if (zconf_endtoken($1, "if")) { menu_end_menu(); printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno()); } }; if_stmt: if_entry stmt_list if_end ; if_stmt_in_choice: if_entry stmt_list_in_choice if_end ; /* menu entry */ menu: T_MENU T_WORD_QUOTE T_EOL { menu_add_entry(NULL); menu_add_prompt(P_MENU, $2, NULL); printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno()); }; menu_entry: menu menu_option_list { $$ = menu_add_menu(); }; menu_end: end { if (zconf_endtoken($1, "menu")) { menu_end_menu(); printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno()); } }; menu_stmt: menu_entry stmt_list menu_end ; menu_option_list: /* empty */ | menu_option_list visible | menu_option_list depends ; source_stmt: T_SOURCE T_WORD_QUOTE T_EOL { printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2); zconf_nextfile($2); free($2); }; /* comment entry */ comment: T_COMMENT T_WORD_QUOTE T_EOL { menu_add_entry(NULL); menu_add_prompt(P_COMMENT, $2, NULL); printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno()); }; comment_stmt: comment comment_option_list ; comment_option_list: /* empty */ | comment_option_list depends ; /* help option */ help_start: T_HELP T_EOL { printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno()); zconf_starthelp(); }; help: help_start T_HELPTEXT { /* Is the help text empty or all whitespace? */ if ($2[strspn($2, " \f\n\r\t\v")] == '\0') zconfprint("warning: '%s' defined with blank help text", current_entry->sym->name ?: ""); current_entry->help = $2; }; /* depends option */ depends: T_DEPENDS T_ON expr T_EOL { menu_add_dep($3); printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno()); }; /* visibility option */ visible: T_VISIBLE if_expr T_EOL { menu_add_visibility($2); }; /* prompt statement */ prompt_stmt_opt: /* empty */ | T_WORD_QUOTE if_expr { menu_add_prompt(P_PROMPT, $1, $2); }; end: T_ENDMENU T_EOL { $$ = "menu"; } | T_ENDCHOICE T_EOL { $$ = "choice"; } | T_ENDIF T_EOL { $$ = "if"; } ; if_expr: /* empty */ { $$ = NULL; } | T_IF expr { $$ = $2; } ; expr: symbol { $$ = expr_alloc_symbol($1); } | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); } | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); } | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); } | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); } | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); } | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); } | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; } | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); } | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); } | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); } ; /* For symbol definitions, selects, etc., where quotes are not accepted */ nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); }; symbol: nonconst_symbol | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); } ; word_opt: /* empty */ { $$ = NULL; } | T_WORD /* assignment statement */ assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); } assign_op: T_EQUAL { $$ = VAR_RECURSIVE; } | T_COLON_EQUAL { $$ = VAR_SIMPLE; } | T_PLUS_EQUAL { $$ = VAR_APPEND; } ; assign_val: /* empty */ { $$ = xstrdup(""); }; | T_ASSIGN_VAL ; %% void conf_parse(const char *name) { struct symbol *sym; int i; zconf_initscan(name); _menu_init(); #if YYDEBUG if (getenv("ZCONF_DEBUG")) yydebug = 1; #endif yyparse(); /* Variables are expanded in the parse phase. We can free them here. */ variable_all_del(); if (yynerrs) exit(1); if (!modules_sym) modules_sym = sym_find( "n" ); if (!menu_has_prompt(&rootmenu)) { current_entry = &rootmenu; menu_add_prompt(P_MENU, "Main menu", NULL); } menu_finalize(&rootmenu); for_all_symbols(i, sym) { if (sym_check_deps(sym)) yynerrs++; } if (yynerrs) exit(1); conf_set_changed(true); } static bool zconf_endtoken(const char *tokenname, const char *expected_tokenname) { if (strcmp(tokenname, expected_tokenname)) { zconf_error("unexpected '%s' within %s block", tokenname, expected_tokenname); yynerrs++; return false; } if (current_menu->file != current_file) { zconf_error("'%s' in different file than '%s'", tokenname, expected_tokenname); fprintf(stderr, "%s:%d: location of the '%s'\n", current_menu->file->name, current_menu->lineno, expected_tokenname); yynerrs++; return false; } return true; } static void zconfprint(const char *err, ...) { va_list ap; fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno()); va_start(ap, err); vfprintf(stderr, err, ap); va_end(ap); fprintf(stderr, "\n"); } static void zconf_error(const char *err, ...) { va_list ap; yynerrs++; fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno()); va_start(ap, err); vfprintf(stderr, err, ap); va_end(ap); fprintf(stderr, "\n"); } static void yyerror(const char *err) { fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err); } static void print_quoted_string(FILE *out, const char *str) { const char *p; int len; putc('"', out); while ((p = strchr(str, '"'))) { len = p - str; if (len) fprintf(out, "%.*s", len, str); fputs("\\\"", out); str = p + 1; } fputs(str, out); putc('"', out); } static void print_symbol(FILE *out, struct menu *menu) { struct symbol *sym = menu->sym; struct property *prop; if (sym_is_choice(sym)) fprintf(out, "\nchoice\n"); else fprintf(out, "\nconfig %s\n", sym->name); switch (sym->type) { case S_BOOLEAN: fputs(" bool\n", out); break; case S_TRISTATE: fputs(" tristate\n", out); break; case S_STRING: fputs(" string\n", out); break; case S_INT: fputs(" integer\n", out); break; case S_HEX: fputs(" hex\n", out); break; default: fputs(" ???\n", out); break; } for (prop = sym->prop; prop; prop = prop->next) { if (prop->menu != menu) continue; switch (prop->type) { case P_PROMPT: fputs(" prompt ", out); print_quoted_string(out, prop->text); if (!expr_is_yes(prop->visible.expr)) { fputs(" if ", out); expr_fprint(prop->visible.expr, out); } fputc('\n', out); break; case P_DEFAULT: fputs( " default ", out); expr_fprint(prop->expr, out); if (!expr_is_yes(prop->visible.expr)) { fputs(" if ", out); expr_fprint(prop->visible.expr, out); } fputc('\n', out); break; case P_CHOICE: fputs(" #choice value\n", out); break; case P_SELECT: fputs( " select ", out); expr_fprint(prop->expr, out); fputc('\n', out); break; case P_IMPLY: fputs( " imply ", out); expr_fprint(prop->expr, out); fputc('\n', out); break; case P_RANGE: fputs( " range ", out); expr_fprint(prop->expr, out); fputc('\n', out); break; case P_MENU: fputs( " menu ", out); print_quoted_string(out, prop->text); fputc('\n', out); break; case P_SYMBOL: fputs( " symbol ", out); fprintf(out, "%s\n", prop->menu->sym->name); break; default: fprintf(out, " unknown prop %d!\n", prop->type); break; } } if (menu->help) { int len = strlen(menu->help); while (menu->help[--len] == '\n') menu->help[len] = 0; fprintf(out, " help\n%s\n", menu->help); } } void zconfdump(FILE *out) { struct property *prop; struct symbol *sym; struct menu *menu; menu = rootmenu.list; while (menu) { if ((sym = menu->sym)) print_symbol(out, menu); else if ((prop = menu->prompt)) { switch (prop->type) { case P_COMMENT: fputs("\ncomment ", out); print_quoted_string(out, prop->text); fputs("\n", out); break; case P_MENU: fputs("\nmenu ", out); print_quoted_string(out, prop->text); fputs("\n", out); break; default: ; } if (!expr_is_yes(prop->visible.expr)) { fputs(" depends ", out); expr_fprint(prop->visible.expr, out); fputc('\n', out); } } if (menu->list) menu = menu->list; else if (menu->next) menu = menu->next; else while ((menu = menu->parent)) { if (menu->prompt && menu->prompt->type == P_MENU) fputs("\nendmenu\n", out); if (menu->next) { menu = menu->next; break; } } } } #n333'>333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
/*
 * ar8216.h: AR8216 switch driver
 *
 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef __AR8216_H
#define __AR8216_H

#define BITS(_s, _n)	(((1UL << (_n)) - 1) << _s)

#define AR8XXX_CAP_GIGE			BIT(0)
#define AR8XXX_CAP_MIB_COUNTERS		BIT(1)

#define AR8XXX_NUM_PHYS 	5
#define AR8216_PORT_CPU	0
#define AR8216_NUM_PORTS	6
#define AR8216_NUM_VLANS	16
#define AR8316_NUM_VLANS	4096

/* size of the vlan table */
#define AR8X16_MAX_VLANS	128
#define AR8X16_PROBE_RETRIES	10
#define AR8X16_MAX_PORTS	8

/* Atheros specific MII registers */
#define MII_ATH_MMD_ADDR		0x0d
#define MII_ATH_MMD_DATA		0x0e
#define MII_ATH_DBG_ADDR		0x1d
#define MII_ATH_DBG_DATA		0x1e

#define AR8216_REG_CTRL			0x0000
#define   AR8216_CTRL_REVISION		BITS(0, 8)
#define   AR8216_CTRL_REVISION_S	0
#define   AR8216_CTRL_VERSION		BITS(8, 8)
#define   AR8216_CTRL_VERSION_S		8
#define   AR8216_CTRL_RESET		BIT(31)

#define AR8216_REG_FLOOD_MASK		0x002C
#define   AR8216_FM_UNI_DEST_PORTS	BITS(0, 6)
#define   AR8216_FM_MULTI_DEST_PORTS	BITS(16, 6)
#define   AR8236_FM_CPU_BROADCAST_EN	BIT(26)
#define   AR8236_FM_CPU_BCAST_FWD_EN	BIT(25)

#define AR8216_REG_GLOBAL_CTRL		0x0030
#define   AR8216_GCTRL_MTU		BITS(0, 11)
#define   AR8236_GCTRL_MTU		BITS(0, 14)
#define   AR8316_GCTRL_MTU		BITS(0, 14)

#define AR8216_REG_VTU			0x0040
#define   AR8216_VTU_OP			BITS(0, 3)
#define   AR8216_VTU_OP_NOOP		0x0
#define   AR8216_VTU_OP_FLUSH		0x1
#define   AR8216_VTU_OP_LOAD		0x2
#define   AR8216_VTU_OP_PURGE		0x3
#define   AR8216_VTU_OP_REMOVE_PORT	0x4
#define   AR8216_VTU_ACTIVE		BIT(3)
#define   AR8216_VTU_FULL		BIT(4)
#define   AR8216_VTU_PORT		BITS(8, 4)
#define   AR8216_VTU_PORT_S		8
#define   AR8216_VTU_VID		BITS(16, 12)
#define   AR8216_VTU_VID_S		16
#define   AR8216_VTU_PRIO		BITS(28, 3)
#define   AR8216_VTU_PRIO_S		28
#define   AR8216_VTU_PRIO_EN		BIT(31)

#define AR8216_REG_VTU_DATA		0x0044
#define   AR8216_VTUDATA_MEMBER		BITS(0, 10)
#define   AR8236_VTUDATA_MEMBER		BITS(0, 7)
#define   AR8216_VTUDATA_VALID		BIT(11)

#define AR8216_REG_ATU			0x0050
#define   AR8216_ATU_OP			BITS(0, 3)
#define   AR8216_ATU_OP_NOOP		0x0
#define   AR8216_ATU_OP_FLUSH		0x1
#define   AR8216_ATU_OP_LOAD		0x2
#define   AR8216_ATU_OP_PURGE		0x3
#define   AR8216_ATU_OP_FLUSH_LOCKED	0x4
#define   AR8216_ATU_OP_FLUSH_UNICAST	0x5
#define   AR8216_ATU_OP_GET_NEXT	0x6
#define   AR8216_ATU_ACTIVE		BIT(3)
#define   AR8216_ATU_PORT_NUM		BITS(8, 4)
#define   AR8216_ATU_FULL_VIO		BIT(12)
#define   AR8216_ATU_ADDR4		BITS(16, 8)
#define   AR8216_ATU_ADDR5		BITS(24, 8)

#define AR8216_REG_ATU_DATA		0x0054
#define   AR8216_ATU_ADDR3		BITS(0, 8)
#define   AR8216_ATU_ADDR2		BITS(8, 8)
#define   AR8216_ATU_ADDR1		BITS(16, 8)
#define   AR8216_ATU_ADDR0		BITS(24, 8)

#define AR8216_REG_ATU_CTRL		0x005C
#define   AR8216_ATU_CTRL_AGE_EN	BIT(17)
#define   AR8216_ATU_CTRL_AGE_TIME	BITS(0, 16)
#define   AR8216_ATU_CTRL_AGE_TIME_S	0
#define   AR8236_ATU_CTRL_RES		BIT(20)

#define AR8216_REG_MIB_FUNC		0x0080
#define   AR8216_MIB_TIMER		BITS(0, 16)
#define   AR8216_MIB_AT_HALF_EN		BIT(16)
#define   AR8216_MIB_BUSY		BIT(17)
#define   AR8216_MIB_FUNC		BITS(24, 3)
#define   AR8216_MIB_FUNC_S		24
#define   AR8216_MIB_FUNC_NO_OP		0x0
#define   AR8216_MIB_FUNC_FLUSH		0x1
#define   AR8216_MIB_FUNC_CAPTURE	0x3
#define   AR8236_MIB_EN			BIT(30)

#define AR8216_REG_GLOBAL_CPUPORT		0x0078
#define   AR8216_GLOBAL_CPUPORT_MIRROR_PORT	BITS(4, 4)
#define   AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S	4

#define AR8216_PORT_OFFSET(_i)		(0x0100 * (_i + 1))
#define AR8216_REG_PORT_STATUS(_i)	(AR8216_PORT_OFFSET(_i) + 0x0000)
#define   AR8216_PORT_STATUS_SPEED	BITS(0,2)
#define   AR8216_PORT_STATUS_SPEED_S	0
#define   AR8216_PORT_STATUS_TXMAC	BIT(2)
#define   AR8216_PORT_STATUS_RXMAC	BIT(3)
#define   AR8216_PORT_STATUS_TXFLOW	BIT(4)
#define   AR8216_PORT_STATUS_RXFLOW	BIT(5)
#define   AR8216_PORT_STATUS_DUPLEX	BIT(6)
#define   AR8216_PORT_STATUS_LINK_UP	BIT(8)
#define   AR8216_PORT_STATUS_LINK_AUTO	BIT(9)
#define   AR8216_PORT_STATUS_LINK_PAUSE	BIT(10)

#define AR8216_REG_PORT_CTRL(_i)	(AR8216_PORT_OFFSET(_i) + 0x0004)

/* port forwarding state */
#define   AR8216_PORT_CTRL_STATE	BITS(0, 3)
#define   AR8216_PORT_CTRL_STATE_S	0

#define   AR8216_PORT_CTRL_LEARN_LOCK	BIT(7)

/* egress 802.1q mode */
#define   AR8216_PORT_CTRL_VLAN_MODE	BITS(8, 2)
#define   AR8216_PORT_CTRL_VLAN_MODE_S	8

#define   AR8216_PORT_CTRL_IGMP_SNOOP	BIT(10)
#define   AR8216_PORT_CTRL_HEADER	BIT(11)
#define   AR8216_PORT_CTRL_MAC_LOOP	BIT(12)
#define   AR8216_PORT_CTRL_SINGLE_VLAN	BIT(13)
#define   AR8216_PORT_CTRL_LEARN	BIT(14)
#define   AR8216_PORT_CTRL_MIRROR_TX	BIT(16)
#define   AR8216_PORT_CTRL_MIRROR_RX	BIT(17)

#define AR8216_REG_PORT_VLAN(_i)	(AR8216_PORT_OFFSET(_i) + 0x0008)

#define   AR8216_PORT_VLAN_DEFAULT_ID	BITS(0, 12)
#define   AR8216_PORT_VLAN_DEFAULT_ID_S	0

#define   AR8216_PORT_VLAN_DEST_PORTS	BITS(16, 9)
#define   AR8216_PORT_VLAN_DEST_PORTS_S	16

/* bit0 added to the priority field of egress frames */
#define   AR8216_PORT_VLAN_TX_PRIO	BIT(27)

/* port default priority */
#define   AR8216_PORT_VLAN_PRIORITY	BITS(28, 2)
#define   AR8216_PORT_VLAN_PRIORITY_S	28

/* ingress 802.1q mode */
#define   AR8216_PORT_VLAN_MODE		BITS(30, 2)
#define   AR8216_PORT_VLAN_MODE_S	30

#define AR8216_REG_PORT_RATE(_i)	(AR8216_PORT_OFFSET(_i) + 0x000c)
#define AR8216_REG_PORT_PRIO(_i)	(AR8216_PORT_OFFSET(_i) + 0x0010)

#define AR8216_STATS_RXBROAD		0x00
#define AR8216_STATS_RXPAUSE		0x04
#define AR8216_STATS_RXMULTI		0x08
#define AR8216_STATS_RXFCSERR		0x0c
#define AR8216_STATS_RXALIGNERR		0x10
#define AR8216_STATS_RXRUNT		0x14
#define AR8216_STATS_RXFRAGMENT		0x18
#define AR8216_STATS_RX64BYTE		0x1c
#define AR8216_STATS_RX128BYTE		0x20
#define AR8216_STATS_RX256BYTE		0x24
#define AR8216_STATS_RX512BYTE		0x28
#define AR8216_STATS_RX1024BYTE		0x2c
#define AR8216_STATS_RXMAXBYTE		0x30
#define AR8216_STATS_RXTOOLONG		0x34
#define AR8216_STATS_RXGOODBYTE		0x38
#define AR8216_STATS_RXBADBYTE		0x40
#define AR8216_STATS_RXOVERFLOW		0x48
#define AR8216_STATS_FILTERED		0x4c
#define AR8216_STATS_TXBROAD		0x50
#define AR8216_STATS_TXPAUSE		0x54
#define AR8216_STATS_TXMULTI		0x58
#define AR8216_STATS_TXUNDERRUN		0x5c
#define AR8216_STATS_TX64BYTE		0x60
#define AR8216_STATS_TX128BYTE		0x64
#define AR8216_STATS_TX256BYTE		0x68
#define AR8216_STATS_TX512BYTE		0x6c
#define AR8216_STATS_TX1024BYTE		0x70
#define AR8216_STATS_TXMAXBYTE		0x74
#define AR8216_STATS_TXOVERSIZE		0x78
#define AR8216_STATS_TXBYTE		0x7c
#define AR8216_STATS_TXCOLLISION	0x84
#define AR8216_STATS_TXABORTCOL		0x88
#define AR8216_STATS_TXMULTICOL		0x8c
#define AR8216_STATS_TXSINGLECOL	0x90
#define AR8216_STATS_TXEXCDEFER		0x94
#define AR8216_STATS_TXDEFER		0x98
#define AR8216_STATS_TXLATECOL		0x9c

#define AR8236_REG_PORT_VLAN(_i)	(AR8216_PORT_OFFSET((_i)) + 0x0008)
#define   AR8236_PORT_VLAN_DEFAULT_ID	BITS(16, 12)
#define   AR8236_PORT_VLAN_DEFAULT_ID_S	16
#define   AR8236_PORT_VLAN_PRIORITY	BITS(29, 3)
#define   AR8236_PORT_VLAN_PRIORITY_S	28

#define AR8236_REG_PORT_VLAN2(_i)	(AR8216_PORT_OFFSET((_i)) + 0x000c)
#define   AR8236_PORT_VLAN2_MEMBER	BITS(16, 7)
#define   AR8236_PORT_VLAN2_MEMBER_S	16
#define   AR8236_PORT_VLAN2_TX_PRIO	BIT(23)
#define   AR8236_PORT_VLAN2_VLAN_MODE	BITS(30, 2)
#define   AR8236_PORT_VLAN2_VLAN_MODE_S	30

#define AR8236_STATS_RXBROAD		0x00
#define AR8236_STATS_RXPAUSE		0x04
#define AR8236_STATS_RXMULTI		0x08
#define AR8236_STATS_RXFCSERR		0x0c
#define AR8236_STATS_RXALIGNERR		0x10
#define AR8236_STATS_RXRUNT		0x14
#define AR8236_STATS_RXFRAGMENT		0x18
#define AR8236_STATS_RX64BYTE		0x1c
#define AR8236_STATS_RX128BYTE		0x20
#define AR8236_STATS_RX256BYTE		0x24
#define AR8236_STATS_RX512BYTE		0x28
#define AR8236_STATS_RX1024BYTE		0x2c
#define AR8236_STATS_RX1518BYTE		0x30
#define AR8236_STATS_RXMAXBYTE		0x34
#define AR8236_STATS_RXTOOLONG		0x38
#define AR8236_STATS_RXGOODBYTE		0x3c
#define AR8236_STATS_RXBADBYTE		0x44
#define AR8236_STATS_RXOVERFLOW		0x4c
#define AR8236_STATS_FILTERED		0x50
#define AR8236_STATS_TXBROAD		0x54
#define AR8236_STATS_TXPAUSE		0x58
#define AR8236_STATS_TXMULTI		0x5c
#define AR8236_STATS_TXUNDERRUN		0x60
#define AR8236_STATS_TX64BYTE		0x64
#define AR8236_STATS_TX128BYTE		0x68
#define AR8236_STATS_TX256BYTE		0x6c
#define AR8236_STATS_TX512BYTE		0x70
#define AR8236_STATS_TX1024BYTE		0x74
#define AR8236_STATS_TX1518BYTE		0x78
#define AR8236_STATS_TXMAXBYTE		0x7c
#define AR8236_STATS_TXOVERSIZE		0x80
#define AR8236_STATS_TXBYTE		0x84
#define AR8236_STATS_TXCOLLISION	0x8c
#define AR8236_STATS_TXABORTCOL		0x90
#define AR8236_STATS_TXMULTICOL		0x94
#define AR8236_STATS_TXSINGLECOL	0x98
#define AR8236_STATS_TXEXCDEFER		0x9c
#define AR8236_STATS_TXDEFER		0xa0
#define AR8236_STATS_TXLATECOL		0xa4

#define AR8316_REG_POSTRIP			0x0008
#define   AR8316_POSTRIP_MAC0_GMII_EN		BIT(0)
#define   AR8316_POSTRIP_MAC0_RGMII_EN		BIT(1)
#define   AR8316_POSTRIP_PHY4_GMII_EN		BIT(2)
#define   AR8316_POSTRIP_PHY4_RGMII_EN		BIT(3)
#define   AR8316_POSTRIP_MAC0_MAC_MODE		BIT(4)
#define   AR8316_POSTRIP_RTL_MODE		BIT(5)
#define   AR8316_POSTRIP_RGMII_RXCLK_DELAY_EN	BIT(6)
#define   AR8316_POSTRIP_RGMII_TXCLK_DELAY_EN	BIT(7)
#define   AR8316_POSTRIP_SERDES_EN		BIT(8)
#define   AR8316_POSTRIP_SEL_ANA_RST		BIT(9)
#define   AR8316_POSTRIP_GATE_25M_EN		BIT(10)
#define   AR8316_POSTRIP_SEL_CLK25M		BIT(11)
#define   AR8316_POSTRIP_HIB_PULSE_HW		BIT(12)
#define   AR8316_POSTRIP_DBG_MODE_I		BIT(13)
#define   AR8316_POSTRIP_MAC5_MAC_MODE		BIT(14)
#define   AR8316_POSTRIP_MAC5_PHY_MODE		BIT(15)
#define   AR8316_POSTRIP_POWER_DOWN_HW		BIT(16)
#define   AR8316_POSTRIP_LPW_STATE_EN		BIT(17)
#define   AR8316_POSTRIP_MAN_EN			BIT(18)
#define   AR8316_POSTRIP_PHY_PLL_ON		BIT(19)
#define   AR8316_POSTRIP_LPW_EXIT		BIT(20)
#define   AR8316_POSTRIP_TXDELAY_S0		BIT(21)
#define   AR8316_POSTRIP_TXDELAY_S1		BIT(22)
#define   AR8316_POSTRIP_RXDELAY_S0		BIT(23)
#define   AR8316_POSTRIP_LED_OPEN_EN		BIT(24)
#define   AR8316_POSTRIP_SPI_EN			BIT(25)
#define   AR8316_POSTRIP_RXDELAY_S1		BIT(26)
#define   AR8316_POSTRIP_POWER_ON_SEL		BIT(31)

/* port speed */
enum {
        AR8216_PORT_SPEED_10M = 0,
        AR8216_PORT_SPEED_100M = 1,
        AR8216_PORT_SPEED_1000M = 2,
        AR8216_PORT_SPEED_ERR = 3,
};

/* ingress 802.1q mode */
enum {
	AR8216_IN_PORT_ONLY = 0,
	AR8216_IN_PORT_FALLBACK = 1,
	AR8216_IN_VLAN_ONLY = 2,
	AR8216_IN_SECURE = 3
};

/* egress 802.1q mode */
enum {
	AR8216_OUT_KEEP = 0,
	AR8216_OUT_STRIP_VLAN = 1,
	AR8216_OUT_ADD_VLAN = 2
};

/* port forwarding state */
enum {
	AR8216_PORT_STATE_DISABLED = 0,
	AR8216_PORT_STATE_BLOCK = 1,
	AR8216_PORT_STATE_LISTEN = 2,
	AR8216_PORT_STATE_LEARN = 3,
	AR8216_PORT_STATE_FORWARD = 4
};

enum {
	AR8XXX_VER_AR8216 = 0x01,
	AR8XXX_VER_AR8236 = 0x03,
	AR8XXX_VER_AR8316 = 0x10,
	AR8XXX_VER_AR8327 = 0x12,
	AR8XXX_VER_AR8337 = 0x13,
};

#define AR8XXX_NUM_ARL_RECORDS	100

enum arl_op {
	AR8XXX_ARL_INITIALIZE,
	AR8XXX_ARL_GET_NEXT
};

struct arl_entry {
	u8 port;
	u8 mac[6];
};

struct ar8xxx_priv;

struct ar8xxx_mib_desc {
	unsigned int size;
	unsigned int offset;
	const char *name;
};

struct ar8xxx_chip {
	unsigned long caps;
	bool config_at_probe;
	bool mii_lo_first;

	/* parameters to calculate REG_PORT_STATS_BASE */
	unsigned reg_port_stats_start;
	unsigned reg_port_stats_length;

	int (*hw_init)(struct ar8xxx_priv *priv);
	void (*cleanup)(struct ar8xxx_priv *priv);

	const char *name;
	int vlans;
	int ports;
	const struct switch_dev_ops *swops;

	void (*init_globals)(struct ar8xxx_priv *priv);
	void (*init_port)(struct ar8xxx_priv *priv, int port);
	void (*setup_port)(struct ar8xxx_priv *priv, int port, u32 members);
	u32 (*read_port_status)(struct ar8xxx_priv *priv, int port);
	u32 (*read_port_eee_status)(struct ar8xxx_priv *priv, int port);
	int (*atu_flush)(struct ar8xxx_priv *priv);
	void (*vtu_flush)(struct ar8xxx_priv *priv);
	void (*vtu_load_vlan)(struct ar8xxx_priv *priv, u32 vid, u32 port_mask);
	void (*phy_fixup)(struct ar8xxx_priv *priv, int phy);
	void (*set_mirror_regs)(struct ar8xxx_priv *priv);
	void (*get_arl_entry)(struct ar8xxx_priv *priv, struct arl_entry *a,
			      u32 *status, enum arl_op op);
	int (*sw_hw_apply)(struct switch_dev *dev);

	const struct ar8xxx_mib_desc *mib_decs;
	unsigned num_mibs;
	unsigned mib_func;
};

struct ar8xxx_priv {
	struct switch_dev dev;
	struct mii_bus *mii_bus;
	struct phy_device *phy;

	int (*get_port_link)(unsigned port);

	const struct net_device_ops *ndo_old;
	struct net_device_ops ndo;
	struct mutex reg_mutex;
	u8 chip_ver;
	u8 chip_rev;
	const struct ar8xxx_chip *chip;
	void *chip_data;
	bool initialized;
	bool port4_phy;
	char buf[2048];
	struct arl_entry arl_table[AR8XXX_NUM_ARL_RECORDS];
	char arl_buf[AR8XXX_NUM_ARL_RECORDS * 32 + 256];
	bool link_up[AR8X16_MAX_PORTS];

	bool init;

	struct mutex mib_lock;
	struct delayed_work mib_work;
	int mib_next_port;
	u64 *mib_stats;

	struct list_head list;
	unsigned int use_count;

	/* all fields below are cleared on reset */
	bool vlan;
	u16 vlan_id[AR8X16_MAX_VLANS];
	u8 vlan_table[AR8X16_MAX_VLANS];
	u8 vlan_tagged;
	u16 pvid[AR8X16_MAX_PORTS];

	/* mirroring */
	bool mirror_rx;
	bool mirror_tx;
	int source_port;
	int monitor_port;
};

u32
ar8xxx_mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum);
void
ar8xxx_mii_write32(struct ar8xxx_priv *priv, int phy_id, int regnum, u32 val);
u32
ar8xxx_read(struct ar8xxx_priv *priv, int reg);
void
ar8xxx_write(struct ar8xxx_priv *priv, int reg, u32 val);
u32
ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val);

void
ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
		     u16 dbg_addr, u16 dbg_data);
void
ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 data);
u16
ar8xxx_phy_mmd_read(struct ar8xxx_priv *priv, int phy_addr, u16 addr);
void
ar8xxx_phy_init(struct ar8xxx_priv *priv);
int
ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
		   struct switch_val *val);
int
ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
		   struct switch_val *val);
int
ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
			 const struct switch_attr *attr,
			 struct switch_val *val);
int
ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
			       const struct switch_attr *attr,
			       struct switch_val *val);
int
ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
			       const struct switch_attr *attr,
			       struct switch_val *val);
int
ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
			       const struct switch_attr *attr,
			       struct switch_val *val);
int
ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
			       const struct switch_attr *attr,
			       struct switch_val *val);
int
ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
				  const struct switch_attr *attr,
				  struct switch_val *val);
int
ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
				  const struct switch_attr *attr,
				  struct switch_val *val);
int
ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
				 const struct switch_attr *attr,
				 struct switch_val *val);
int
ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
				 const struct switch_attr *attr,
				 struct switch_val *val);
int
ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan);
int
ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan);
int
ar8xxx_sw_hw_apply(struct switch_dev *dev);
int
ar8xxx_sw_reset_switch(struct switch_dev *dev);
int
ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
			struct switch_port_link *link);
int
ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
                             const struct switch_attr *attr,
                             struct switch_val *val);
int
ar8xxx_sw_get_port_mib(struct switch_dev *dev,
                       const struct switch_attr *attr,
                       struct switch_val *val);
int
ar8xxx_sw_get_arl_table(struct switch_dev *dev,
			const struct switch_attr *attr,
			struct switch_val *val);
int
ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val);

static inline struct ar8xxx_priv *
swdev_to_ar8xxx(struct switch_dev *swdev)
{
	return container_of(swdev, struct ar8xxx_priv, dev);
}

static inline bool ar8xxx_has_gige(struct ar8xxx_priv *priv)
{
	return priv->chip->caps & AR8XXX_CAP_GIGE;
}

static inline bool ar8xxx_has_mib_counters(struct ar8xxx_priv *priv)
{
	return priv->chip->caps & AR8XXX_CAP_MIB_COUNTERS;
}

static inline bool chip_is_ar8216(struct ar8xxx_priv *priv)
{
	return priv->chip_ver == AR8XXX_VER_AR8216;
}

static inline bool chip_is_ar8236(struct ar8xxx_priv *priv)
{
	return priv->chip_ver == AR8XXX_VER_AR8236;
}

static inline bool chip_is_ar8316(struct ar8xxx_priv *priv)
{
	return priv->chip_ver == AR8XXX_VER_AR8316;
}

static inline bool chip_is_ar8327(struct ar8xxx_priv *priv)
{
	return priv->chip_ver == AR8XXX_VER_AR8327;
}

static inline bool chip_is_ar8337(struct ar8xxx_priv *priv)
{
	return priv->chip_ver == AR8XXX_VER_AR8337;
}

static inline void
ar8xxx_reg_set(struct ar8xxx_priv *priv, int reg, u32 val)
{
	ar8xxx_rmw(priv, reg, 0, val);
}

static inline void
ar8xxx_reg_clear(struct ar8xxx_priv *priv, int reg, u32 val)
{
	ar8xxx_rmw(priv, reg, val, 0);
}

static inline void
split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
{
	regaddr >>= 1;
	*r1 = regaddr & 0x1e;

	regaddr >>= 5;
	*r2 = regaddr & 0x7;

	regaddr >>= 3;
	*page = regaddr & 0x1ff;
}

static inline void
wait_for_page_switch(void)
{
	udelay(5);
}

#endif