/*1* net/dsa/mv88e6123_61_65.c - Marvell 88e6123/6161/6165 switch chip support2* Copyright (c) 2008-2009 Marvell Semiconductor3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*/910#include <linux/list.h>11#include <linux/netdevice.h>12#include <linux/phy.h>13#include "dsa_priv.h"14#include "mv88e6xxx.h"1516static char *mv88e6123_61_65_probe(struct mii_bus *bus, int sw_addr)17{18int ret;1920ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), 0x03);21if (ret >= 0) {22ret &= 0xfff0;23if (ret == 0x1210)24return "Marvell 88E6123";25if (ret == 0x1610)26return "Marvell 88E6161";27if (ret == 0x1650)28return "Marvell 88E6165";29}3031return NULL;32}3334static int mv88e6123_61_65_switch_reset(struct dsa_switch *ds)35{36int i;37int ret;3839/*40* Set all ports to the disabled state.41*/42for (i = 0; i < 8; i++) {43ret = REG_READ(REG_PORT(i), 0x04);44REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);45}4647/*48* Wait for transmit queues to drain.49*/50msleep(2);5152/*53* Reset the switch.54*/55REG_WRITE(REG_GLOBAL, 0x04, 0xc400);5657/*58* Wait up to one second for reset to complete.59*/60for (i = 0; i < 1000; i++) {61ret = REG_READ(REG_GLOBAL, 0x00);62if ((ret & 0xc800) == 0xc800)63break;6465msleep(1);66}67if (i == 1000)68return -ETIMEDOUT;6970return 0;71}7273static int mv88e6123_61_65_setup_global(struct dsa_switch *ds)74{75int ret;76int i;7778/*79* Disable the PHY polling unit (since there won't be any80* external PHYs to poll), don't discard packets with81* excessive collisions, and mask all interrupt sources.82*/83REG_WRITE(REG_GLOBAL, 0x04, 0x0000);8485/*86* Set the default address aging time to 5 minutes, and87* enable address learn messages to be sent to all message88* ports.89*/90REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);9192/*93* Configure the priority mapping registers.94*/95ret = mv88e6xxx_config_prio(ds);96if (ret < 0)97return ret;9899/*100* Configure the upstream port, and configure the upstream101* port as the port to which ingress and egress monitor frames102* are to be sent.103*/104REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));105106/*107* Disable remote management for now, and set the switch's108* DSA device number.109*/110REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);111112/*113* Send all frames with destination addresses matching114* 01:80:c2:00:00:2x to the CPU port.115*/116REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);117118/*119* Send all frames with destination addresses matching120* 01:80:c2:00:00:0x to the CPU port.121*/122REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);123124/*125* Disable the loopback filter, disable flow control126* messages, disable flood broadcast override, disable127* removing of provider tags, disable ATU age violation128* interrupts, disable tag flow control, force flow129* control priority to the highest, and send all special130* multicast frames to the CPU at the highest priority.131*/132REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);133134/*135* Program the DSA routing table.136*/137for (i = 0; i < 32; i++) {138int nexthop;139140nexthop = 0x1f;141if (i != ds->index && i < ds->dst->pd->nr_chips)142nexthop = ds->pd->rtable[i] & 0x1f;143144REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | nexthop);145}146147/*148* Clear all trunk masks.149*/150for (i = 0; i < 8; i++)151REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0xff);152153/*154* Clear all trunk mappings.155*/156for (i = 0; i < 16; i++)157REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));158159/*160* Disable ingress rate limiting by resetting all ingress161* rate limit registers to their initial state.162*/163for (i = 0; i < 6; i++)164REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));165166/*167* Initialise cross-chip port VLAN table to reset defaults.168*/169REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);170171/*172* Clear the priority override table.173*/174for (i = 0; i < 16; i++)175REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));176177/* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */178179return 0;180}181182static int mv88e6123_61_65_setup_port(struct dsa_switch *ds, int p)183{184int addr = REG_PORT(p);185u16 val;186187/*188* MAC Forcing register: don't force link, speed, duplex189* or flow control state to any particular values on physical190* ports, but force the CPU port and all DSA ports to 1000 Mb/s191* full duplex.192*/193if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p))194REG_WRITE(addr, 0x01, 0x003e);195else196REG_WRITE(addr, 0x01, 0x0003);197198/*199* Do not limit the period of time that this port can be200* paused for by the remote end or the period of time that201* this port can pause the remote end.202*/203REG_WRITE(addr, 0x02, 0x0000);204205/*206* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,207* disable Header mode, enable IGMP/MLD snooping, disable VLAN208* tunneling, determine priority by looking at 802.1p and IP209* priority fields (IP prio has precedence), and set STP state210* to Forwarding.211*212* If this is the CPU link, use DSA or EDSA tagging depending213* on which tagging mode was configured.214*215* If this is a link to another switch, use DSA tagging mode.216*217* If this is the upstream port for this switch, enable218* forwarding of unknown unicasts and multicasts.219*/220val = 0x0433;221if (dsa_is_cpu_port(ds, p)) {222if (ds->dst->tag_protocol == htons(ETH_P_EDSA))223val |= 0x3300;224else225val |= 0x0100;226}227if (ds->dsa_port_mask & (1 << p))228val |= 0x0100;229if (p == dsa_upstream_port(ds))230val |= 0x000c;231REG_WRITE(addr, 0x04, val);232233/*234* Port Control 1: disable trunking. Also, if this is the235* CPU port, enable learn messages to be sent to this port.236*/237REG_WRITE(addr, 0x05, dsa_is_cpu_port(ds, p) ? 0x8000 : 0x0000);238239/*240* Port based VLAN map: give each port its own address241* database, allow the CPU port to talk to each of the 'real'242* ports, and allow each of the 'real' ports to only talk to243* the upstream port.244*/245val = (p & 0xf) << 12;246if (dsa_is_cpu_port(ds, p))247val |= ds->phys_port_mask;248else249val |= 1 << dsa_upstream_port(ds);250REG_WRITE(addr, 0x06, val);251252/*253* Default VLAN ID and priority: don't set a default VLAN254* ID, and set the default packet priority to zero.255*/256REG_WRITE(addr, 0x07, 0x0000);257258/*259* Port Control 2: don't force a good FCS, set the maximum260* frame size to 10240 bytes, don't let the switch add or261* strip 802.1q tags, don't discard tagged or untagged frames262* on this port, do a destination address lookup on all263* received packets as usual, disable ARP mirroring and don't264* send a copy of all transmitted/received frames on this port265* to the CPU.266*/267REG_WRITE(addr, 0x08, 0x2080);268269/*270* Egress rate control: disable egress rate control.271*/272REG_WRITE(addr, 0x09, 0x0001);273274/*275* Egress rate control 2: disable egress rate control.276*/277REG_WRITE(addr, 0x0a, 0x0000);278279/*280* Port Association Vector: when learning source addresses281* of packets, add the address to the address database using282* a port bitmap that has only the bit for this port set and283* the other bits clear.284*/285REG_WRITE(addr, 0x0b, 1 << p);286287/*288* Port ATU control: disable limiting the number of address289* database entries that this port is allowed to use.290*/291REG_WRITE(addr, 0x0c, 0x0000);292293/*294* Priorit Override: disable DA, SA and VTU priority override.295*/296REG_WRITE(addr, 0x0d, 0x0000);297298/*299* Port Ethertype: use the Ethertype DSA Ethertype value.300*/301REG_WRITE(addr, 0x0f, ETH_P_EDSA);302303/*304* Tag Remap: use an identity 802.1p prio -> switch prio305* mapping.306*/307REG_WRITE(addr, 0x18, 0x3210);308309/*310* Tag Remap 2: use an identity 802.1p prio -> switch prio311* mapping.312*/313REG_WRITE(addr, 0x19, 0x7654);314315return 0;316}317318static int mv88e6123_61_65_setup(struct dsa_switch *ds)319{320struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);321int i;322int ret;323324mutex_init(&ps->smi_mutex);325mutex_init(&ps->stats_mutex);326327ret = mv88e6123_61_65_switch_reset(ds);328if (ret < 0)329return ret;330331/* @@@ initialise vtu and atu */332333ret = mv88e6123_61_65_setup_global(ds);334if (ret < 0)335return ret;336337for (i = 0; i < 6; i++) {338ret = mv88e6123_61_65_setup_port(ds, i);339if (ret < 0)340return ret;341}342343return 0;344}345346static int mv88e6123_61_65_port_to_phy_addr(int port)347{348if (port >= 0 && port <= 4)349return port;350return -1;351}352353static int354mv88e6123_61_65_phy_read(struct dsa_switch *ds, int port, int regnum)355{356int addr = mv88e6123_61_65_port_to_phy_addr(port);357return mv88e6xxx_phy_read(ds, addr, regnum);358}359360static int361mv88e6123_61_65_phy_write(struct dsa_switch *ds,362int port, int regnum, u16 val)363{364int addr = mv88e6123_61_65_port_to_phy_addr(port);365return mv88e6xxx_phy_write(ds, addr, regnum, val);366}367368static struct mv88e6xxx_hw_stat mv88e6123_61_65_hw_stats[] = {369{ "in_good_octets", 8, 0x00, },370{ "in_bad_octets", 4, 0x02, },371{ "in_unicast", 4, 0x04, },372{ "in_broadcasts", 4, 0x06, },373{ "in_multicasts", 4, 0x07, },374{ "in_pause", 4, 0x16, },375{ "in_undersize", 4, 0x18, },376{ "in_fragments", 4, 0x19, },377{ "in_oversize", 4, 0x1a, },378{ "in_jabber", 4, 0x1b, },379{ "in_rx_error", 4, 0x1c, },380{ "in_fcs_error", 4, 0x1d, },381{ "out_octets", 8, 0x0e, },382{ "out_unicast", 4, 0x10, },383{ "out_broadcasts", 4, 0x13, },384{ "out_multicasts", 4, 0x12, },385{ "out_pause", 4, 0x15, },386{ "excessive", 4, 0x11, },387{ "collisions", 4, 0x1e, },388{ "deferred", 4, 0x05, },389{ "single", 4, 0x14, },390{ "multiple", 4, 0x17, },391{ "out_fcs_error", 4, 0x03, },392{ "late", 4, 0x1f, },393{ "hist_64bytes", 4, 0x08, },394{ "hist_65_127bytes", 4, 0x09, },395{ "hist_128_255bytes", 4, 0x0a, },396{ "hist_256_511bytes", 4, 0x0b, },397{ "hist_512_1023bytes", 4, 0x0c, },398{ "hist_1024_max_bytes", 4, 0x0d, },399};400401static void402mv88e6123_61_65_get_strings(struct dsa_switch *ds, int port, uint8_t *data)403{404mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6123_61_65_hw_stats),405mv88e6123_61_65_hw_stats, port, data);406}407408static void409mv88e6123_61_65_get_ethtool_stats(struct dsa_switch *ds,410int port, uint64_t *data)411{412mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6123_61_65_hw_stats),413mv88e6123_61_65_hw_stats, port, data);414}415416static int mv88e6123_61_65_get_sset_count(struct dsa_switch *ds)417{418return ARRAY_SIZE(mv88e6123_61_65_hw_stats);419}420421static struct dsa_switch_driver mv88e6123_61_65_switch_driver = {422.tag_protocol = cpu_to_be16(ETH_P_EDSA),423.priv_size = sizeof(struct mv88e6xxx_priv_state),424.probe = mv88e6123_61_65_probe,425.setup = mv88e6123_61_65_setup,426.set_addr = mv88e6xxx_set_addr_indirect,427.phy_read = mv88e6123_61_65_phy_read,428.phy_write = mv88e6123_61_65_phy_write,429.poll_link = mv88e6xxx_poll_link,430.get_strings = mv88e6123_61_65_get_strings,431.get_ethtool_stats = mv88e6123_61_65_get_ethtool_stats,432.get_sset_count = mv88e6123_61_65_get_sset_count,433};434435static int __init mv88e6123_61_65_init(void)436{437register_switch_driver(&mv88e6123_61_65_switch_driver);438return 0;439}440module_init(mv88e6123_61_65_init);441442static void __exit mv88e6123_61_65_cleanup(void)443{444unregister_switch_driver(&mv88e6123_61_65_switch_driver);445}446module_exit(mv88e6123_61_65_cleanup);447448449