2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/platform_device.h>
27 #include <linux/of_device.h>
28 #include <linux/clk.h>
29 #include <linux/of_net.h>
30 #include <linux/of_mdio.h>
31 #include <linux/of_irq.h>
32 #include <linux/of_address.h>
33 #include <linux/switch.h>
35 #include <asm/mach-ralink/ralink_regs.h>
37 #include "ralink_soc_eth.h"
39 #include <linux/ioport.h>
40 #include <linux/switch.h>
41 #include <linux/mii.h>
43 #include <ralink_regs.h>
44 #include <asm/mach-ralink/mt7620.h>
46 #include "ralink_soc_eth.h"
47 #include "gsw_mt7620a.h"
51 #define GSW_REG_PHY_TIMEOUT (5 * HZ)
53 #define MT7620A_GSW_REG_PIAC 0x7004
55 #define GSW_NUM_VLANS 16
56 #define GSW_NUM_VIDS 4096
57 #define GSW_NUM_PORTS 7
60 #define GSW_MDIO_ACCESS BIT(31)
61 #define GSW_MDIO_READ BIT(19)
62 #define GSW_MDIO_WRITE BIT(18)
63 #define GSW_MDIO_START BIT(16)
64 #define GSW_MDIO_ADDR_SHIFT 20
65 #define GSW_MDIO_REG_SHIFT 25
67 #define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
68 #define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
69 #define GSW_REG_SMACCR0 0x3fE4
70 #define GSW_REG_SMACCR1 0x3fE8
71 #define GSW_REG_CKGCR 0x3ff0
73 #define GSW_REG_IMR 0x7008
74 #define GSW_REG_ISR 0x700c
75 #define GSW_REG_GPC1 0x7014
76 #define GSW_PHY1_DISABLE BIT(25)
78 #define SYSC_REG_CFG1 0x14
80 #define PORT_IRQ_ST_CHG 0x7f
84 #define ESW_PHY_POLLING 0x7000
86 #define PMCR_IPG BIT(18)
87 #define PMCR_MAC_MODE BIT(16)
88 #define PMCR_FORCE BIT(15)
89 #define PMCR_TX_EN BIT(14)
90 #define PMCR_RX_EN BIT(13)
91 #define PMCR_BACKOFF BIT(9)
92 #define PMCR_BACKPRES BIT(8)
93 #define PMCR_RX_FC BIT(5)
94 #define PMCR_TX_FC BIT(4)
95 #define PMCR_SPEED(_x) (_x << 2)
96 #define PMCR_DUPLEX BIT(1)
97 #define PMCR_LINK BIT(0)
99 #define PHY_AN_EN BIT(31)
100 #define PHY_PRE_EN BIT(30)
101 #define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
104 /* Global attributes. */
105 GSW_ATTR_ENABLE_VLAN,
106 /* Port attributes. */
120 long unsigned int autopoll;
123 static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
125 iowrite32(val, gsw->base + reg);
128 static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
130 return ioread32(gsw->base + reg);
133 static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
135 unsigned long t_start = jiffies;
138 if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
140 if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
145 printk(KERN_ERR "mdio: MDIO timeout\n");
149 static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
152 if (mt7620_mii_busy_wait(gsw))
155 write_data &= 0xffff;
157 gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
158 (phy_register << GSW_MDIO_REG_SHIFT) |
159 (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
160 MT7620A_GSW_REG_PIAC);
162 if (mt7620_mii_busy_wait(gsw))
168 static u32 _mt7620_mii_read(struct mt7620_gsw *gsw, int phy_addr, int phy_reg)
172 if (mt7620_mii_busy_wait(gsw))
175 gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
176 (phy_reg << GSW_MDIO_REG_SHIFT) |
177 (phy_addr << GSW_MDIO_ADDR_SHIFT),
178 MT7620A_GSW_REG_PIAC);
180 if (mt7620_mii_busy_wait(gsw))
183 d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
188 int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
190 struct fe_priv *priv = bus->priv;
191 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
193 return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
196 int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
198 struct fe_priv *priv = bus->priv;
199 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
201 return _mt7620_mii_read(gsw, phy_addr, phy_reg);
204 static unsigned char *fe_speed_str(int speed)
221 int mt7620a_has_carrier(struct fe_priv *priv)
223 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
226 for (i = 0; i < GSW_PORT6; i++)
227 if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
232 static void mt7620a_handle_carrier(struct fe_priv *priv)
237 if (mt7620a_has_carrier(priv))
238 netif_carrier_on(priv->netdev);
240 netif_carrier_off(priv->netdev);
243 void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
245 if (priv->link[port])
246 netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
247 port, fe_speed_str(priv->phy->speed[port]),
248 (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
250 netdev_info(priv->netdev, "port %d link down\n", port);
251 mt7620a_handle_carrier(priv);
254 static irqreturn_t gsw_interrupt(int irq, void *_priv)
256 struct fe_priv *priv = (struct fe_priv *) _priv;
257 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
259 int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
261 status = gsw_r32(gsw, GSW_REG_ISR);
262 if (status & PORT_IRQ_ST_CHG)
263 for (i = 0; i <= max; i++) {
264 u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
265 int link = status & 0x1;
267 if (link != priv->link[i]) {
269 netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
270 i, fe_speed_str((status >> 2) & 3),
271 (status & 0x2) ? "Full" : "Half");
273 netdev_info(priv->netdev, "port %d link down\n", i);
276 priv->link[i] = link;
278 mt7620a_handle_carrier(priv);
280 gsw_w32(gsw, status, GSW_REG_ISR);
285 static int mt7620_is_bga(void)
287 u32 bga = rt_sysc_r32(0x0c);
289 return (bga >> 16) & 1;
292 static void gsw_auto_poll(struct mt7620_gsw *gsw)
295 int lsb = -1, msb = 0;
297 for_each_set_bit(phy, &gsw->autopoll, 32) {
303 gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
306 void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
308 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
309 const __be32 *_id = of_get_property(np, "reg", NULL);
310 int phy_mode, size, id;
313 int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
315 if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
317 pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
319 pr_err("%s: invalid port id\n", np->name);
323 id = be32_to_cpu(*_id);
328 priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
329 if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
330 pr_err("%s: invalid fixed link property\n", np->name);
331 priv->phy->phy_fixed[id] = NULL;
335 phy_mode = of_get_phy_mode(np);
337 case PHY_INTERFACE_MODE_RGMII:
340 case PHY_INTERFACE_MODE_MII:
343 case PHY_INTERFACE_MODE_RMII:
347 dev_err(priv->device, "port %d - invalid phy mode\n", id);
351 priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
352 if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
355 val = rt_sysc_r32(SYSCFG1);
356 val &= ~(3 << shift);
357 val |= mask << shift;
358 rt_sysc_w32(val, SYSCFG1);
360 if (priv->phy->phy_fixed[id]) {
361 const __be32 *link = priv->phy->phy_fixed[id];
365 priv->phy->speed[id] = be32_to_cpup(link++);
366 tx_fc = be32_to_cpup(link++);
367 rx_fc = be32_to_cpup(link++);
368 priv->phy->duplex[id] = be32_to_cpup(link++);
371 switch (priv->phy->speed[id]) {
382 dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
383 priv->phy->phy_fixed[id] = 0;
386 val = PMCR_SPEED(val);
387 val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
388 PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
393 if (priv->phy->duplex[id])
395 gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
396 dev_info(priv->device, "using fixed link parameters\n");
400 if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
401 u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
402 PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
404 gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
405 fe_connect_phy_node(priv, priv->phy->phy_node[id]);
406 gsw->autopoll |= BIT(id);
412 static void gsw_hw_init(struct mt7620_gsw *gsw)
414 u32 is_BGA = mt7620_is_bga();
416 rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
417 gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
419 /* EPHY1 fixup - only run if the ephy is enabled */
420 if (gsw_r32(gsw, GSW_REG_GPC1) & GSW_PHY1_DISABLE == GSW_PHY1_DISABLE) {
421 /*correct PHY setting L3.0 BGA*/
422 _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
424 _mt7620_mii_write(gsw, 1, 17, 0x7444);
426 _mt7620_mii_write(gsw, 1, 19, 0x0114);
428 _mt7620_mii_write(gsw, 1, 19, 0x0117);
430 _mt7620_mii_write(gsw, 1, 22, 0x10cf);
431 _mt7620_mii_write(gsw, 1, 25, 0x6212);
432 _mt7620_mii_write(gsw, 1, 26, 0x0777);
433 _mt7620_mii_write(gsw, 1, 29, 0x4000);
434 _mt7620_mii_write(gsw, 1, 28, 0xc077);
435 _mt7620_mii_write(gsw, 1, 24, 0x0000);
437 _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
438 _mt7620_mii_write(gsw, 1, 17, 0x4838);
440 _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
442 _mt7620_mii_write(gsw, 1, 21, 0x0515);
443 _mt7620_mii_write(gsw, 1, 22, 0x0053);
444 _mt7620_mii_write(gsw, 1, 23, 0x00bf);
445 _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
446 _mt7620_mii_write(gsw, 1, 25, 0x0fad);
447 _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
449 _mt7620_mii_write(gsw, 1, 21, 0x0517);
450 _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
451 _mt7620_mii_write(gsw, 1, 23, 0x00bf);
452 _mt7620_mii_write(gsw, 1, 24, 0x0aab);
453 _mt7620_mii_write(gsw, 1, 25, 0x00ae);
454 _mt7620_mii_write(gsw, 1, 26, 0x0fff);
456 _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
457 _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
460 _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
461 _mt7620_mii_write(gsw, 0, 30, 0xa000);
462 _mt7620_mii_write(gsw, 1, 30, 0xa000);
463 _mt7620_mii_write(gsw, 2, 30, 0xa000);
464 _mt7620_mii_write(gsw, 3, 30, 0xa000);
466 _mt7620_mii_write(gsw, 0, 4, 0x05e1);
467 _mt7620_mii_write(gsw, 1, 4, 0x05e1);
468 _mt7620_mii_write(gsw, 2, 4, 0x05e1);
469 _mt7620_mii_write(gsw, 3, 4, 0x05e1);
471 _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
472 _mt7620_mii_write(gsw, 0, 16, 0x1111);
473 _mt7620_mii_write(gsw, 1, 16, 0x1010);
474 _mt7620_mii_write(gsw, 2, 16, 0x1515);
475 _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
477 /* CPU Port6 Force Link 1G, FC ON */
478 gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
479 /* Set Port6 CPU Port */
480 gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
483 if (gsw->port4 == PORT4_EPHY) {
484 u32 val = rt_sysc_r32(SYSCFG1);
486 rt_sysc_w32(val, SYSCFG1);
487 _mt7620_mii_write(gsw, 4, 30, 0xa000);
488 _mt7620_mii_write(gsw, 4, 4, 0x05e1);
489 _mt7620_mii_write(gsw, 4, 16, 0x1313);
490 pr_info("gsw: setting port4 to ephy mode\n");
494 void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
496 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
499 spin_lock_irqsave(&priv->page_lock, flags);
500 gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
501 gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
503 spin_unlock_irqrestore(&priv->page_lock, flags);
506 static struct of_device_id gsw_match[] = {
507 { .compatible = "ralink,mt7620a-gsw" },
511 int mt7620_gsw_config(struct fe_priv *priv)
513 struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
515 /* is the mt7530 internal or external */
516 if (priv->mii_bus && priv->mii_bus->phy_map[0x1f]) {
517 mt7530_probe(priv->device, gsw->base, NULL, 0);
518 mt7530_probe(priv->device, NULL, priv->mii_bus, 1);
520 mt7530_probe(priv->device, gsw->base, NULL, 1);
526 int mt7620_gsw_probe(struct fe_priv *priv)
528 struct mt7620_gsw *gsw;
529 struct device_node *np;
530 const char *port4 = NULL;
532 np = of_find_matching_node(NULL, gsw_match);
534 dev_err(priv->device, "no gsw node found\n");
537 np = of_node_get(np);
539 gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
541 dev_err(priv->device, "no gsw memory for private data\n");
545 gsw->irq = irq_of_parse_and_map(np, 0);
547 dev_err(priv->device, "no gsw irq resource found\n");
551 gsw->base = of_iomap(np, 0);
553 dev_err(priv->device, "gsw ioremap failed\n");
557 gsw->dev = priv->device;
558 priv->soc->swpriv = gsw;
560 of_property_read_string(np, "ralink,port4", &port4);
561 if (port4 && !strcmp(port4, "ephy"))
562 gsw->port4 = PORT4_EPHY;
563 else if (port4 && !strcmp(port4, "gmac"))
564 gsw->port4 = PORT4_EXT;
570 gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
571 request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);