1 #include <linux/ioport.h>
3 #include <rt305x_regs.h>
4 #include <rt305x_esw_platform.h>
6 #define RT305X_ESW_REG_FCT0 0x08
7 #define RT305X_ESW_REG_PFC1 0x14
8 #define RT305X_ESW_REG_PVIDC(_n) (0x48 + 4 * (_n))
9 #define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
10 #define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
11 #define RT305X_ESW_REG_FPA 0x84
12 #define RT305X_ESW_REG_SOCPC 0x8c
13 #define RT305X_ESW_REG_POC1 0x90
14 #define RT305X_ESW_REG_POC2 0x94
15 #define RT305X_ESW_REG_POC3 0x98
16 #define RT305X_ESW_REG_SGC 0x9c
17 #define RT305X_ESW_REG_PCR0 0xc0
18 #define RT305X_ESW_REG_PCR1 0xc4
19 #define RT305X_ESW_REG_FPA2 0xc8
20 #define RT305X_ESW_REG_FCT2 0xcc
21 #define RT305X_ESW_REG_SGC2 0xe4
23 #define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
24 #define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
25 #define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
27 #define RT305X_ESW_PCR1_WT_DONE BIT(0)
29 #define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
31 #define RT305X_ESW_VLANI_VID_M 0xfff
32 #define RT305X_ESW_VLANI_VID_S 12
36 struct rt305x_esw_platform_data
*pdata
;
37 spinlock_t reg_rw_lock
;
41 rt305x_esw_wr(struct rt305x_esw
*esw
, u32 val
, unsigned reg
)
43 __raw_writel(val
, esw
->base
+ reg
);
47 rt305x_esw_rr(struct rt305x_esw
*esw
, unsigned reg
)
49 return __raw_readl(esw
->base
+ reg
);
53 rt305x_esw_rmw_raw(struct rt305x_esw
*esw
, unsigned reg
, unsigned long mask
,
58 t
= __raw_readl(esw
->base
+ reg
) & ~mask
;
59 __raw_writel(t
| val
, esw
->base
+ reg
);
63 rt305x_esw_rmw(struct rt305x_esw
*esw
, unsigned reg
, unsigned long mask
,
68 spin_lock_irqsave(&esw
->reg_rw_lock
, flags
);
69 rt305x_esw_rmw_raw(esw
, reg
, mask
, val
);
70 spin_unlock_irqrestore(&esw
->reg_rw_lock
, flags
);
74 rt305x_mii_write(struct rt305x_esw
*esw
, u32 phy_addr
, u32 phy_register
,
77 unsigned long t_start
= jiffies
;
81 if (!(rt305x_esw_rr(esw
, RT305X_ESW_REG_PCR1
) &
82 RT305X_ESW_PCR1_WT_DONE
))
84 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
92 (write_data
<< RT305X_ESW_PCR0_WT_NWAY_DATA_S
) |
93 (phy_register
<< RT305X_ESW_PCR0_CPU_PHY_REG_S
) |
94 (phy_addr
) | RT305X_ESW_PCR0_WT_PHY_CMD
,
99 if (rt305x_esw_rr(esw
, RT305X_ESW_REG_PCR1
) &
100 RT305X_ESW_PCR1_WT_DONE
)
103 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
110 printk(KERN_ERR
"ramips_eth: MDIO timeout\n");
115 rt305x_esw_set_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
, unsigned vid
)
119 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
121 RT305X_ESW_REG_VLANI(vlan
/ 2),
122 RT305X_ESW_VLANI_VID_M
<< s
,
123 (vid
& RT305X_ESW_VLANI_VID_M
) << s
);
127 rt305x_esw_hw_init(struct rt305x_esw
*esw
)
131 /* vodoo from original driver */
132 rt305x_esw_wr(esw
, 0xC8A07850, RT305X_ESW_REG_FCT0
);
133 rt305x_esw_wr(esw
, 0x00000000, RT305X_ESW_REG_SGC2
);
134 rt305x_esw_wr(esw
, 0x00405555, RT305X_ESW_REG_PFC1
);
135 rt305x_esw_wr(esw
, 0x00007f7f, RT305X_ESW_REG_POC1
);
136 rt305x_esw_wr(esw
, 0x00007f3f, RT305X_ESW_REG_POC3
);
137 rt305x_esw_wr(esw
, 0x00d6500c, RT305X_ESW_REG_FCT2
);
138 rt305x_esw_wr(esw
, 0x0008a301, RT305X_ESW_REG_SGC
);
139 rt305x_esw_wr(esw
, 0x02404040, RT305X_ESW_REG_SOCPC
);
140 rt305x_esw_wr(esw
, 0x00001002, RT305X_ESW_REG_PVIDC(2));
141 rt305x_esw_wr(esw
, 0x3f502b28, RT305X_ESW_REG_FPA2
);
142 rt305x_esw_wr(esw
, 0x00000000, RT305X_ESW_REG_FPA
);
144 rt305x_mii_write(esw
, 0, 31, 0x8000);
145 for (i
= 0; i
< 5; i
++) {
146 /* TX10 waveform coefficient */
147 rt305x_mii_write(esw
, i
, 0, 0x3100);
148 /* TX10 waveform coefficient */
149 rt305x_mii_write(esw
, i
, 26, 0x1601);
150 /* TX100/TX10 AD/DA current bias */
151 rt305x_mii_write(esw
, i
, 29, 0x7058);
152 /* TX100 slew rate control */
153 rt305x_mii_write(esw
, i
, 30, 0x0018);
157 /* select global register */
158 rt305x_mii_write(esw
, 0, 31, 0x0);
159 /* tune TP_IDL tail and head waveform */
160 rt305x_mii_write(esw
, 0, 22, 0x052f);
161 /* set TX10 signal amplitude threshold to minimum */
162 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
163 /* set squelch amplitude to higher threshold */
164 rt305x_mii_write(esw
, 0, 18, 0x40ba);
165 /* longer TP_IDL tail length */
166 rt305x_mii_write(esw
, 0, 14, 0x65);
167 /* select local register */
168 rt305x_mii_write(esw
, 0, 31, 0x8000);
170 /* set default vlan */
171 rt305x_esw_set_vlan_id(esw
, 0, 1);
172 rt305x_esw_set_vlan_id(esw
, 1, 2);
173 rt305x_esw_wr(esw
, 0x504f, RT305X_ESW_REG_VMSC(0));
177 rt305x_esw_probe(struct platform_device
*pdev
)
179 struct rt305x_esw_platform_data
*pdata
;
180 struct rt305x_esw
*esw
;
181 struct resource
*res
;
184 pdata
= pdev
->dev
.platform_data
;
188 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
190 dev_err(&pdev
->dev
, "no memory resource found\n");
194 esw
= kzalloc(sizeof(struct rt305x_esw
), GFP_KERNEL
);
196 dev_err(&pdev
->dev
, "no memory for private data\n");
200 esw
->base
= ioremap(res
->start
, resource_size(res
));
202 dev_err(&pdev
->dev
, "ioremap failed\n");
207 platform_set_drvdata(pdev
, esw
);
210 spin_lock_init(&esw
->reg_rw_lock
);
211 rt305x_esw_hw_init(esw
);
221 rt305x_esw_remove(struct platform_device
*pdev
)
223 struct rt305x_esw
*esw
;
225 esw
= platform_get_drvdata(pdev
);
227 platform_set_drvdata(pdev
, NULL
);
235 static struct platform_driver rt305x_esw_driver
= {
236 .probe
= rt305x_esw_probe
,
237 .remove
= rt305x_esw_remove
,
239 .name
= "rt305x-esw",
240 .owner
= THIS_MODULE
,
245 rt305x_esw_init(void)
247 return platform_driver_register(&rt305x_esw_driver
);
251 rt305x_esw_exit(void)
253 platform_driver_unregister(&rt305x_esw_driver
);