kobs-ng: add new package
[openwrt/staging/wigyori.git] / package / boot / uboot-lantiq / patches / 0020-net-switchlib-add-driver-for-Lantiq-PSB697X-switch-f.patch
1 From b8c666eda693906488637c414db9db35b6760e4a Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Wed, 29 Aug 2012 22:08:15 +0200
4 Subject: net: switchlib: add driver for Lantiq PSB697X switch family
5
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
7
8 --- a/drivers/net/switch/Makefile
9 +++ b/drivers/net/switch/Makefile
10 @@ -11,6 +11,7 @@ include $(TOPDIR)/config.mk
11 LIB := $(obj)libswitch.o
12
13 COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
14 +COBJS-$(CONFIG_SWITCH_PSB697X) += psb697x.o
15
16 COBJS := $(COBJS-y)
17 SRCS := $(COBJS:.o=.c)
18 --- /dev/null
19 +++ b/drivers/net/switch/psb697x.c
20 @@ -0,0 +1,119 @@
21 +/*
22 + * Copyright (C) 2011-2012 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
23 + *
24 + * This file is released under the terms of GPL v2 and any later version.
25 + * See the file COPYING in the root directory of the source tree for details.
26 + */
27 +
28 +#include <common.h>
29 +#include <malloc.h>
30 +#include <switch.h>
31 +#include <miiphy.h>
32 +
33 +#define PSB697X_CHIPID1 0x2599
34 +#define PSB697X_PORT_COUNT 7
35 +
36 +#define PSB697X_PORT_BASE(p) (p * 0x20)
37 +#define PSB697X_REG_PS(p) (PSB697X_PORT_BASE(p) + 0x00)
38 +#define PSB697X_REG_PBC(p) (PSB697X_PORT_BASE(p) + 0x01)
39 +#define PSB697X_REG_PEC(p) (PSB697X_PORT_BASE(p) + 0x02)
40 +
41 +#define PSB697X_REG_SGC1 0x0E0 /* Switch Global Control Register 1 */
42 +#define PSB697X_REG_SGC2 0x0E1 /* Switch Global Control Register 2 */
43 +#define PSB697X_REG_CMH 0x0E2 /* CPU Port & Mirror Control */
44 +#define PSB697X_REG_MIICR 0x0F5 /* MII Port Control */
45 +#define PSB697X_REG_CI0 0x100 /* Chip Identifier 0 */
46 +#define PSB697X_REG_CI1 0x101 /* Chip Identifier 1 */
47 +#define PSB697X_REG_MIIAC 0x120 /* MII Indirect Access Control */
48 +#define PSB697X_REG_MIIWD 0x121 /* MII Indirect Write Data */
49 +#define PSB697X_REG_MIIRD 0x122 /* MII Indirect Read Data */
50 +
51 +#define PSB697X_REG_PORT_FLP (1 << 2) /* Force link up */
52 +#define PSB697X_REG_PORT_FLD (1 << 1) /* Force link down */
53 +
54 +#define PSB697X_REG_SGC2_SE (1 << 15) /* Switch enable */
55 +
56 +#define PSB697X_REG_CMH_CPN_MASK 0x7
57 +#define PSB697X_REG_CMH_CPN_SHIFT 5
58 +
59 +
60 +static inline int psb697x_mii_read(struct mii_dev *bus, u16 reg)
61 +{
62 + int ret;
63 +
64 + ret = bus->read(bus, (reg >> 5) & 0x1f, MDIO_DEVAD_NONE, reg & 0x1f);
65 +
66 + return ret;
67 +}
68 +
69 +static inline int psb697x_mii_write(struct mii_dev *bus, u16 reg, u16 val)
70 +{
71 + int ret;
72 +
73 + ret = bus->write(bus, (reg >> 5) & 0x1f, MDIO_DEVAD_NONE,
74 + reg & 0x1f, val);
75 +
76 + return ret;
77 +}
78 +
79 +static int psb697x_probe(struct switch_device *dev)
80 +{
81 + struct mii_dev *bus = dev->bus;
82 + int ci1;
83 +
84 + ci1 = psb697x_mii_read(bus, PSB697X_REG_CI1);
85 +
86 + if (ci1 == PSB697X_CHIPID1)
87 + return 0;
88 +
89 + return 1;
90 +}
91 +
92 +static void psb697x_setup(struct switch_device *dev)
93 +{
94 + struct mii_dev *bus = dev->bus;
95 + int i, state;
96 +
97 + /* Enable switch */
98 + psb697x_mii_write(bus, PSB697X_REG_SGC2, PSB697X_REG_SGC2_SE);
99 +
100 + /*
101 + * Force 100 Mbps as default value for CPU ports 5 and 6 to get
102 + * full speed.
103 + */
104 + psb697x_mii_write(bus, PSB697X_REG_MIICR, 0x0773);
105 +
106 + for (i = 0; i < PSB697X_PORT_COUNT; i++) {
107 + state = dev->port_mask & (1 << i);
108 +
109 + /*
110 + * Software workaround from Errata Sheet:
111 + * Force link down and reset internal PHY, keep that state
112 + * for all unconnected ports and disable force link down
113 + * for all connected ports
114 + */
115 + psb697x_mii_write(bus, PSB697X_REG_PBC(i),
116 + PSB697X_REG_PORT_FLD);
117 +
118 + if (i == dev->cpu_port)
119 + /* Force link up for CPU port */
120 + psb697x_mii_write(bus, PSB697X_REG_PBC(i),
121 + PSB697X_REG_PORT_FLP);
122 + else if (state)
123 + /* Disable force link down for active LAN ports */
124 + psb697x_mii_write(bus, PSB697X_REG_PBC(i), 0);
125 + }
126 +}
127 +
128 +static struct switch_driver psb697x_drv = {
129 + .name = "psb697x",
130 +};
131 +
132 +void switch_psb697x_init(void)
133 +{
134 + /* For archs with manual relocation */
135 + psb697x_drv.probe = psb697x_probe;
136 + psb697x_drv.setup = psb697x_setup;
137 +
138 + switch_driver_register(&psb697x_drv);
139 +}
140 --- a/drivers/net/switch/switch.c
141 +++ b/drivers/net/switch/switch.c
142 @@ -18,6 +18,10 @@ void switch_init(void)
143 INIT_LIST_HEAD(&switch_drivers);
144 INIT_LIST_HEAD(&switch_devices);
145
146 +#if defined(CONFIG_SWITCH_PSB697X)
147 + switch_psb697x_init();
148 +#endif
149 +
150 board_switch_init();
151 }
152
153 --- a/include/switch.h
154 +++ b/include/switch.h
155 @@ -90,6 +90,7 @@ static inline void switch_setup(struct s
156 }
157
158 /* Init functions for supported Switch drivers */
159 +extern void switch_psb697x_init(void);
160
161 #endif /* __SWITCH_H */
162