b603c2336041b0cbf5bb6917143c094bbe02eb68
[oweals/openwrt.git] / target / linux / generic / files / drivers / net / phy / rtl8366s.c
1 /*
2  * Platform driver for the Realtek RTL8366S ethernet switch
3  *
4  * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
5  * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published
9  * by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/skbuff.h>
18 #include <linux/rtl8366s.h>
19
20 #include "rtl8366_smi.h"
21
22 #define RTL8366S_DRIVER_DESC    "Realtek RTL8366S ethernet switch driver"
23 #define RTL8366S_DRIVER_VER     "0.2.2"
24
25 #define RTL8366S_PHY_NO_MAX     4
26 #define RTL8366S_PHY_PAGE_MAX   7
27 #define RTL8366S_PHY_ADDR_MAX   31
28
29 /* Switch Global Configuration register */
30 #define RTL8366S_SGCR                           0x0000
31 #define RTL8366S_SGCR_EN_BC_STORM_CTRL          BIT(0)
32 #define RTL8366S_SGCR_MAX_LENGTH(_x)            (_x << 4)
33 #define RTL8366S_SGCR_MAX_LENGTH_MASK           RTL8366S_SGCR_MAX_LENGTH(0x3)
34 #define RTL8366S_SGCR_MAX_LENGTH_1522           RTL8366S_SGCR_MAX_LENGTH(0x0)
35 #define RTL8366S_SGCR_MAX_LENGTH_1536           RTL8366S_SGCR_MAX_LENGTH(0x1)
36 #define RTL8366S_SGCR_MAX_LENGTH_1552           RTL8366S_SGCR_MAX_LENGTH(0x2)
37 #define RTL8366S_SGCR_MAX_LENGTH_16000          RTL8366S_SGCR_MAX_LENGTH(0x3)
38 #define RTL8366S_SGCR_EN_VLAN                   BIT(13)
39
40 /* Port Enable Control register */
41 #define RTL8366S_PECR                           0x0001
42
43 /* Switch Security Control registers */
44 #define RTL8366S_SSCR0                          0x0002
45 #define RTL8366S_SSCR1                          0x0003
46 #define RTL8366S_SSCR2                          0x0004
47 #define RTL8366S_SSCR2_DROP_UNKNOWN_DA          BIT(0)
48
49 #define RTL8366S_RESET_CTRL_REG                 0x0100
50 #define RTL8366S_CHIP_CTRL_RESET_HW             1
51 #define RTL8366S_CHIP_CTRL_RESET_SW             (1 << 1)
52
53 #define RTL8366S_CHIP_VERSION_CTRL_REG          0x0104
54 #define RTL8366S_CHIP_VERSION_MASK              0xf
55 #define RTL8366S_CHIP_ID_REG                    0x0105
56 #define RTL8366S_CHIP_ID_8366                   0x8366
57
58 /* PHY registers control */
59 #define RTL8366S_PHY_ACCESS_CTRL_REG            0x8028
60 #define RTL8366S_PHY_ACCESS_DATA_REG            0x8029
61
62 #define RTL8366S_PHY_CTRL_READ                  1
63 #define RTL8366S_PHY_CTRL_WRITE                 0
64
65 #define RTL8366S_PHY_REG_MASK                   0x1f
66 #define RTL8366S_PHY_PAGE_OFFSET                5
67 #define RTL8366S_PHY_PAGE_MASK                  (0x7 << 5)
68 #define RTL8366S_PHY_NO_OFFSET                  9
69 #define RTL8366S_PHY_NO_MASK                    (0x1f << 9)
70
71 /* LED control registers */
72 #define RTL8366S_LED_BLINKRATE_REG              0x0420
73 #define RTL8366S_LED_BLINKRATE_BIT              0
74 #define RTL8366S_LED_BLINKRATE_MASK             0x0007
75
76 #define RTL8366S_LED_CTRL_REG                   0x0421
77 #define RTL8366S_LED_0_1_CTRL_REG               0x0422
78 #define RTL8366S_LED_2_3_CTRL_REG               0x0423
79
80 #define RTL8366S_MIB_COUNT                      33
81 #define RTL8366S_GLOBAL_MIB_COUNT               1
82 #define RTL8366S_MIB_COUNTER_PORT_OFFSET        0x0040
83 #define RTL8366S_MIB_COUNTER_BASE               0x1000
84 #define RTL8366S_MIB_COUNTER_PORT_OFFSET2       0x0008
85 #define RTL8366S_MIB_COUNTER_BASE2              0x1180
86 #define RTL8366S_MIB_CTRL_REG                   0x11F0
87 #define RTL8366S_MIB_CTRL_USER_MASK             0x01FF
88 #define RTL8366S_MIB_CTRL_BUSY_MASK             0x0001
89 #define RTL8366S_MIB_CTRL_RESET_MASK            0x0002
90
91 #define RTL8366S_MIB_CTRL_GLOBAL_RESET_MASK     0x0004
92 #define RTL8366S_MIB_CTRL_PORT_RESET_BIT        0x0003
93 #define RTL8366S_MIB_CTRL_PORT_RESET_MASK       0x01FC
94
95
96 #define RTL8366S_PORT_VLAN_CTRL_BASE            0x0058
97 #define RTL8366S_PORT_VLAN_CTRL_REG(_p)  \
98                 (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
99 #define RTL8366S_PORT_VLAN_CTRL_MASK            0xf
100 #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p)       (4 * ((_p) % 4))
101
102
103 #define RTL8366S_VLAN_TABLE_READ_BASE           0x018B
104 #define RTL8366S_VLAN_TABLE_WRITE_BASE          0x0185
105
106 #define RTL8366S_VLAN_TB_CTRL_REG               0x010F
107
108 #define RTL8366S_TABLE_ACCESS_CTRL_REG          0x0180
109 #define RTL8366S_TABLE_VLAN_READ_CTRL           0x0E01
110 #define RTL8366S_TABLE_VLAN_WRITE_CTRL          0x0F01
111
112 #define RTL8366S_VLAN_MC_BASE(_x)               (0x0016 + (_x) * 2)
113
114 #define RTL8366S_VLAN_MEMBERINGRESS_REG         0x0379
115
116 #define RTL8366S_PORT_LINK_STATUS_BASE          0x0060
117 #define RTL8366S_PORT_STATUS_SPEED_MASK         0x0003
118 #define RTL8366S_PORT_STATUS_DUPLEX_MASK        0x0004
119 #define RTL8366S_PORT_STATUS_LINK_MASK          0x0010
120 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK       0x0020
121 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK       0x0040
122 #define RTL8366S_PORT_STATUS_AN_MASK            0x0080
123
124
125 #define RTL8366S_PORT_NUM_CPU           5
126 #define RTL8366S_NUM_PORTS              6
127 #define RTL8366S_NUM_VLANS              16
128 #define RTL8366S_NUM_LEDGROUPS          4
129 #define RTL8366S_NUM_VIDS               4096
130 #define RTL8366S_PRIORITYMAX            7
131 #define RTL8366S_FIDMAX                 7
132
133
134 #define RTL8366S_PORT_1                 (1 << 0) /* In userspace port 0 */
135 #define RTL8366S_PORT_2                 (1 << 1) /* In userspace port 1 */
136 #define RTL8366S_PORT_3                 (1 << 2) /* In userspace port 2 */
137 #define RTL8366S_PORT_4                 (1 << 3) /* In userspace port 3 */
138
139 #define RTL8366S_PORT_UNKNOWN           (1 << 4) /* No known connection */
140 #define RTL8366S_PORT_CPU               (1 << 5) /* CPU port */
141
142 #define RTL8366S_PORT_ALL               (RTL8366S_PORT_1 |      \
143                                          RTL8366S_PORT_2 |      \
144                                          RTL8366S_PORT_3 |      \
145                                          RTL8366S_PORT_4 |      \
146                                          RTL8366S_PORT_UNKNOWN | \
147                                          RTL8366S_PORT_CPU)
148
149 #define RTL8366S_PORT_ALL_BUT_CPU       (RTL8366S_PORT_1 |      \
150                                          RTL8366S_PORT_2 |      \
151                                          RTL8366S_PORT_3 |      \
152                                          RTL8366S_PORT_4 |      \
153                                          RTL8366S_PORT_UNKNOWN)
154
155 #define RTL8366S_PORT_ALL_EXTERNAL      (RTL8366S_PORT_1 |      \
156                                          RTL8366S_PORT_2 |      \
157                                          RTL8366S_PORT_3 |      \
158                                          RTL8366S_PORT_4)
159
160 #define RTL8366S_PORT_ALL_INTERNAL      (RTL8366S_PORT_UNKNOWN | \
161                                          RTL8366S_PORT_CPU)
162
163 #define RTL8366S_VLAN_VID_MASK          0xfff
164 #define RTL8366S_VLAN_PRIORITY_SHIFT    12
165 #define RTL8366S_VLAN_PRIORITY_MASK     0x7
166 #define RTL8366S_VLAN_MEMBER_MASK       0x3f
167 #define RTL8366S_VLAN_UNTAG_SHIFT       6
168 #define RTL8366S_VLAN_UNTAG_MASK        0x3f
169 #define RTL8366S_VLAN_FID_SHIFT         12
170 #define RTL8366S_VLAN_FID_MASK          0x7
171
172 static struct rtl8366_mib_counter rtl8366s_mib_counters[] = {
173         { 0,  0, 4, "IfInOctets"                                },
174         { 0,  4, 4, "EtherStatsOctets"                          },
175         { 0,  8, 2, "EtherStatsUnderSizePkts"                   },
176         { 0, 10, 2, "EtherFragments"                            },
177         { 0, 12, 2, "EtherStatsPkts64Octets"                    },
178         { 0, 14, 2, "EtherStatsPkts65to127Octets"               },
179         { 0, 16, 2, "EtherStatsPkts128to255Octets"              },
180         { 0, 18, 2, "EtherStatsPkts256to511Octets"              },
181         { 0, 20, 2, "EtherStatsPkts512to1023Octets"             },
182         { 0, 22, 2, "EtherStatsPkts1024to1518Octets"            },
183         { 0, 24, 2, "EtherOversizeStats"                        },
184         { 0, 26, 2, "EtherStatsJabbers"                         },
185         { 0, 28, 2, "IfInUcastPkts"                             },
186         { 0, 30, 2, "EtherStatsMulticastPkts"                   },
187         { 0, 32, 2, "EtherStatsBroadcastPkts"                   },
188         { 0, 34, 2, "EtherStatsDropEvents"                      },
189         { 0, 36, 2, "Dot3StatsFCSErrors"                        },
190         { 0, 38, 2, "Dot3StatsSymbolErrors"                     },
191         { 0, 40, 2, "Dot3InPauseFrames"                         },
192         { 0, 42, 2, "Dot3ControlInUnknownOpcodes"               },
193         { 0, 44, 4, "IfOutOctets"                               },
194         { 0, 48, 2, "Dot3StatsSingleCollisionFrames"            },
195         { 0, 50, 2, "Dot3StatMultipleCollisionFrames"           },
196         { 0, 52, 2, "Dot3sDeferredTransmissions"                },
197         { 0, 54, 2, "Dot3StatsLateCollisions"                   },
198         { 0, 56, 2, "EtherStatsCollisions"                      },
199         { 0, 58, 2, "Dot3StatsExcessiveCollisions"              },
200         { 0, 60, 2, "Dot3OutPauseFrames"                        },
201         { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards"        },
202
203         /*
204          * The following counters are accessible at a different
205          * base address.
206          */
207         { 1,  0, 2, "Dot1dTpPortInDiscards"                     },
208         { 1,  2, 2, "IfOutUcastPkts"                            },
209         { 1,  4, 2, "IfOutMulticastPkts"                        },
210         { 1,  6, 2, "IfOutBroadcastPkts"                        },
211 };
212
213 #define REG_WR(_smi, _reg, _val)                                        \
214         do {                                                            \
215                 err = rtl8366_smi_write_reg(_smi, _reg, _val);          \
216                 if (err)                                                \
217                         return err;                                     \
218         } while (0)
219
220 #define REG_RMW(_smi, _reg, _mask, _val)                                \
221         do {                                                            \
222                 err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val);        \
223                 if (err)                                                \
224                         return err;                                     \
225         } while (0)
226
227 static int rtl8366s_reset_chip(struct rtl8366_smi *smi)
228 {
229         int timeout = 10;
230         u32 data;
231
232         rtl8366_smi_write_reg(smi, RTL8366S_RESET_CTRL_REG,
233                               RTL8366S_CHIP_CTRL_RESET_HW);
234         do {
235                 msleep(1);
236                 if (rtl8366_smi_read_reg(smi, RTL8366S_RESET_CTRL_REG, &data))
237                         return -EIO;
238
239                 if (!(data & RTL8366S_CHIP_CTRL_RESET_HW))
240                         break;
241         } while (--timeout);
242
243         if (!timeout) {
244                 printk("Timeout waiting for the switch to reset\n");
245                 return -EIO;
246         }
247
248         return 0;
249 }
250
251 static int rtl8366s_hw_init(struct rtl8366_smi *smi)
252 {
253         int err;
254
255         /* set maximum packet length to 1536 bytes */
256         REG_RMW(smi, RTL8366S_SGCR, RTL8366S_SGCR_MAX_LENGTH_MASK,
257                 RTL8366S_SGCR_MAX_LENGTH_1536);
258
259         /* enable all ports */
260         REG_WR(smi, RTL8366S_PECR, 0);
261
262         /* disable learning for all ports */
263         REG_WR(smi, RTL8366S_SSCR0, RTL8366S_PORT_ALL);
264
265         /* disable auto ageing for all ports */
266         REG_WR(smi, RTL8366S_SSCR1, RTL8366S_PORT_ALL);
267
268         /*
269          * discard VLAN tagged packets if the port is not a member of
270          * the VLAN with which the packets is associated.
271          */
272         REG_WR(smi, RTL8366S_VLAN_MEMBERINGRESS_REG, RTL8366S_PORT_ALL);
273
274         /* don't drop packets whose DA has not been learned */
275         REG_RMW(smi, RTL8366S_SSCR2, RTL8366S_SSCR2_DROP_UNKNOWN_DA, 0);
276
277         return 0;
278 }
279
280 static int rtl8366s_read_phy_reg(struct rtl8366_smi *smi,
281                                  u32 phy_no, u32 page, u32 addr, u32 *data)
282 {
283         u32 reg;
284         int ret;
285
286         if (phy_no > RTL8366S_PHY_NO_MAX)
287                 return -EINVAL;
288
289         if (page > RTL8366S_PHY_PAGE_MAX)
290                 return -EINVAL;
291
292         if (addr > RTL8366S_PHY_ADDR_MAX)
293                 return -EINVAL;
294
295         ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
296                                     RTL8366S_PHY_CTRL_READ);
297         if (ret)
298                 return ret;
299
300         reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
301               ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
302               (addr & RTL8366S_PHY_REG_MASK);
303
304         ret = rtl8366_smi_write_reg(smi, reg, 0);
305         if (ret)
306                 return ret;
307
308         ret = rtl8366_smi_read_reg(smi, RTL8366S_PHY_ACCESS_DATA_REG, data);
309         if (ret)
310                 return ret;
311
312         return 0;
313 }
314
315 static int rtl8366s_write_phy_reg(struct rtl8366_smi *smi,
316                                   u32 phy_no, u32 page, u32 addr, u32 data)
317 {
318         u32 reg;
319         int ret;
320
321         if (phy_no > RTL8366S_PHY_NO_MAX)
322                 return -EINVAL;
323
324         if (page > RTL8366S_PHY_PAGE_MAX)
325                 return -EINVAL;
326
327         if (addr > RTL8366S_PHY_ADDR_MAX)
328                 return -EINVAL;
329
330         ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
331                                     RTL8366S_PHY_CTRL_WRITE);
332         if (ret)
333                 return ret;
334
335         reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
336               ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
337               (addr & RTL8366S_PHY_REG_MASK);
338
339         ret = rtl8366_smi_write_reg(smi, reg, data);
340         if (ret)
341                 return ret;
342
343         return 0;
344 }
345
346 static int rtl8366_get_mib_counter(struct rtl8366_smi *smi, int counter,
347                                    int port, unsigned long long *val)
348 {
349         int i;
350         int err;
351         u32 addr, data;
352         u64 mibvalue;
353
354         if (port > RTL8366S_NUM_PORTS || counter >= RTL8366S_MIB_COUNT)
355                 return -EINVAL;
356
357         switch (rtl8366s_mib_counters[counter].base) {
358         case 0:
359                 addr = RTL8366S_MIB_COUNTER_BASE +
360                        RTL8366S_MIB_COUNTER_PORT_OFFSET * port;
361                 break;
362
363         case 1:
364                 addr = RTL8366S_MIB_COUNTER_BASE2 +
365                         RTL8366S_MIB_COUNTER_PORT_OFFSET2 * port;
366                 break;
367
368         default:
369                 return -EINVAL;
370         }
371
372         addr += rtl8366s_mib_counters[counter].offset;
373
374         /*
375          * Writing access counter address first
376          * then ASIC will prepare 64bits counter wait for being retrived
377          */
378         data = 0; /* writing data will be discard by ASIC */
379         err = rtl8366_smi_write_reg(smi, addr, data);
380         if (err)
381                 return err;
382
383         /* read MIB control register */
384         err =  rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
385         if (err)
386                 return err;
387
388         if (data & RTL8366S_MIB_CTRL_BUSY_MASK)
389                 return -EBUSY;
390
391         if (data & RTL8366S_MIB_CTRL_RESET_MASK)
392                 return -EIO;
393
394         mibvalue = 0;
395         for (i = rtl8366s_mib_counters[counter].length; i > 0; i--) {
396                 err = rtl8366_smi_read_reg(smi, addr + (i - 1), &data);
397                 if (err)
398                         return err;
399
400                 mibvalue = (mibvalue << 16) | (data & 0xFFFF);
401         }
402
403         *val = mibvalue;
404         return 0;
405 }
406
407 static int rtl8366s_get_vlan_4k(struct rtl8366_smi *smi, u32 vid,
408                                 struct rtl8366_vlan_4k *vlan4k)
409 {
410         u32 data[2];
411         int err;
412         int i;
413
414         memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
415
416         if (vid >= RTL8366S_NUM_VIDS)
417                 return -EINVAL;
418
419         /* write VID */
420         err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE,
421                                     vid & RTL8366S_VLAN_VID_MASK);
422         if (err)
423                 return err;
424
425         /* write table access control word */
426         err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
427                                     RTL8366S_TABLE_VLAN_READ_CTRL);
428         if (err)
429                 return err;
430
431         for (i = 0; i < 2; i++) {
432                 err = rtl8366_smi_read_reg(smi,
433                                            RTL8366S_VLAN_TABLE_READ_BASE + i,
434                                            &data[i]);
435                 if (err)
436                         return err;
437         }
438
439         vlan4k->vid = vid;
440         vlan4k->untag = (data[1] >> RTL8366S_VLAN_UNTAG_SHIFT) &
441                         RTL8366S_VLAN_UNTAG_MASK;
442         vlan4k->member = data[1] & RTL8366S_VLAN_MEMBER_MASK;
443         vlan4k->fid = (data[1] >> RTL8366S_VLAN_FID_SHIFT) &
444                         RTL8366S_VLAN_FID_MASK;
445
446         return 0;
447 }
448
449 static int rtl8366s_set_vlan_4k(struct rtl8366_smi *smi,
450                                 const struct rtl8366_vlan_4k *vlan4k)
451 {
452         u32 data[2];
453         int err;
454         int i;
455
456         if (vlan4k->vid >= RTL8366S_NUM_VIDS ||
457             vlan4k->member > RTL8366S_PORT_ALL ||
458             vlan4k->untag > RTL8366S_PORT_ALL ||
459             vlan4k->fid > RTL8366S_FIDMAX)
460                 return -EINVAL;
461
462         data[0] = vlan4k->vid & RTL8366S_VLAN_VID_MASK;
463         data[1] = (vlan4k->member & RTL8366S_VLAN_MEMBER_MASK) |
464                   ((vlan4k->untag & RTL8366S_VLAN_UNTAG_MASK) <<
465                         RTL8366S_VLAN_UNTAG_SHIFT) |
466                   ((vlan4k->fid & RTL8366S_VLAN_FID_MASK) <<
467                         RTL8366S_VLAN_FID_SHIFT);
468
469         for (i = 0; i < 2; i++) {
470                 err = rtl8366_smi_write_reg(smi,
471                                             RTL8366S_VLAN_TABLE_WRITE_BASE + i,
472                                             data[i]);
473                 if (err)
474                         return err;
475         }
476
477         /* write table access control word */
478         err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
479                                     RTL8366S_TABLE_VLAN_WRITE_CTRL);
480
481         return err;
482 }
483
484 static int rtl8366s_get_vlan_mc(struct rtl8366_smi *smi, u32 index,
485                                 struct rtl8366_vlan_mc *vlanmc)
486 {
487         u32 data[2];
488         int err;
489         int i;
490
491         memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
492
493         if (index >= RTL8366S_NUM_VLANS)
494                 return -EINVAL;
495
496         for (i = 0; i < 2; i++) {
497                 err = rtl8366_smi_read_reg(smi,
498                                            RTL8366S_VLAN_MC_BASE(index) + i,
499                                            &data[i]);
500                 if (err)
501                         return err;
502         }
503
504         vlanmc->vid = data[0] & RTL8366S_VLAN_VID_MASK;
505         vlanmc->priority = (data[0] >> RTL8366S_VLAN_PRIORITY_SHIFT) &
506                            RTL8366S_VLAN_PRIORITY_MASK;
507         vlanmc->untag = (data[1] >> RTL8366S_VLAN_UNTAG_SHIFT) &
508                         RTL8366S_VLAN_UNTAG_MASK;
509         vlanmc->member = data[1] & RTL8366S_VLAN_MEMBER_MASK;
510         vlanmc->fid = (data[1] >> RTL8366S_VLAN_FID_SHIFT) &
511                       RTL8366S_VLAN_FID_MASK;
512
513         return 0;
514 }
515
516 static int rtl8366s_set_vlan_mc(struct rtl8366_smi *smi, u32 index,
517                                 const struct rtl8366_vlan_mc *vlanmc)
518 {
519         u32 data[2];
520         int err;
521         int i;
522
523         if (index >= RTL8366S_NUM_VLANS ||
524             vlanmc->vid >= RTL8366S_NUM_VIDS ||
525             vlanmc->priority > RTL8366S_PRIORITYMAX ||
526             vlanmc->member > RTL8366S_PORT_ALL ||
527             vlanmc->untag > RTL8366S_PORT_ALL ||
528             vlanmc->fid > RTL8366S_FIDMAX)
529                 return -EINVAL;
530
531         data[0] = (vlanmc->vid & RTL8366S_VLAN_VID_MASK) |
532                   ((vlanmc->priority & RTL8366S_VLAN_PRIORITY_MASK) <<
533                         RTL8366S_VLAN_PRIORITY_SHIFT);
534         data[1] = (vlanmc->member & RTL8366S_VLAN_MEMBER_MASK) |
535                   ((vlanmc->untag & RTL8366S_VLAN_UNTAG_MASK) <<
536                         RTL8366S_VLAN_UNTAG_SHIFT) |
537                   ((vlanmc->fid & RTL8366S_VLAN_FID_MASK) <<
538                         RTL8366S_VLAN_FID_SHIFT);
539
540         for (i = 0; i < 2; i++) {
541                 err = rtl8366_smi_write_reg(smi,
542                                             RTL8366S_VLAN_MC_BASE(index) + i,
543                                             data[i]);
544                 if (err)
545                         return err;
546         }
547
548         return 0;
549 }
550
551 static int rtl8366s_get_mc_index(struct rtl8366_smi *smi, int port, int *val)
552 {
553         u32 data;
554         int err;
555
556         if (port >= RTL8366S_NUM_PORTS)
557                 return -EINVAL;
558
559         err = rtl8366_smi_read_reg(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
560                                    &data);
561         if (err)
562                 return err;
563
564         *val = (data >> RTL8366S_PORT_VLAN_CTRL_SHIFT(port)) &
565                RTL8366S_PORT_VLAN_CTRL_MASK;
566
567         return 0;
568 }
569
570 static int rtl8366s_set_mc_index(struct rtl8366_smi *smi, int port, int index)
571 {
572         if (port >= RTL8366S_NUM_PORTS || index >= RTL8366S_NUM_VLANS)
573                 return -EINVAL;
574
575         return rtl8366_smi_rmwr(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
576                                 RTL8366S_PORT_VLAN_CTRL_MASK <<
577                                         RTL8366S_PORT_VLAN_CTRL_SHIFT(port),
578                                 (index & RTL8366S_PORT_VLAN_CTRL_MASK) <<
579                                         RTL8366S_PORT_VLAN_CTRL_SHIFT(port));
580 }
581
582 static int rtl8366s_vlan_set_vlan(struct rtl8366_smi *smi, int enable)
583 {
584         return rtl8366_smi_rmwr(smi, RTL8366S_SGCR, RTL8366S_SGCR_EN_VLAN,
585                                 (enable) ? RTL8366S_SGCR_EN_VLAN : 0);
586 }
587
588 static int rtl8366s_vlan_set_4ktable(struct rtl8366_smi *smi, int enable)
589 {
590         return rtl8366_smi_rmwr(smi, RTL8366S_VLAN_TB_CTRL_REG,
591                                 1, (enable) ? 1 : 0);
592 }
593
594 static int rtl8366s_sw_reset_mibs(struct switch_dev *dev,
595                                   const struct switch_attr *attr,
596                                   struct switch_val *val)
597 {
598         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
599
600         return rtl8366_smi_rmwr(smi, RTL8366S_MIB_CTRL_REG, 0, (1 << 2));
601 }
602
603 static int rtl8366s_sw_get_vlan_enable(struct switch_dev *dev,
604                                        const struct switch_attr *attr,
605                                        struct switch_val *val)
606 {
607         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
608         u32 data;
609
610         if (attr->ofs == 1) {
611                 rtl8366_smi_read_reg(smi, RTL8366S_SGCR, &data);
612
613                 if (data & RTL8366S_SGCR_EN_VLAN)
614                         val->value.i = 1;
615                 else
616                         val->value.i = 0;
617         } else if (attr->ofs == 2) {
618                 rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TB_CTRL_REG, &data);
619
620                 if (data & 0x0001)
621                         val->value.i = 1;
622                 else
623                         val->value.i = 0;
624         }
625
626         return 0;
627 }
628
629 static int rtl8366s_sw_get_blinkrate(struct switch_dev *dev,
630                                      const struct switch_attr *attr,
631                                      struct switch_val *val)
632 {
633         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
634         u32 data;
635
636         rtl8366_smi_read_reg(smi, RTL8366S_LED_BLINKRATE_REG, &data);
637
638         val->value.i = (data & (RTL8366S_LED_BLINKRATE_MASK));
639
640         return 0;
641 }
642
643 static int rtl8366s_sw_set_blinkrate(struct switch_dev *dev,
644                                     const struct switch_attr *attr,
645                                     struct switch_val *val)
646 {
647         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
648
649         if (val->value.i >= 6)
650                 return -EINVAL;
651
652         return rtl8366_smi_rmwr(smi, RTL8366S_LED_BLINKRATE_REG,
653                                 RTL8366S_LED_BLINKRATE_MASK,
654                                 val->value.i);
655 }
656
657 static int rtl8366s_sw_set_vlan_enable(struct switch_dev *dev,
658                                        const struct switch_attr *attr,
659                                        struct switch_val *val)
660 {
661         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
662
663         if (attr->ofs == 1)
664                 return rtl8366s_vlan_set_vlan(smi, val->value.i);
665         else
666                 return rtl8366s_vlan_set_4ktable(smi, val->value.i);
667 }
668
669 static const char *rtl8366s_speed_str(unsigned speed)
670 {
671         switch (speed) {
672         case 0:
673                 return "10baseT";
674         case 1:
675                 return "100baseT";
676         case 2:
677                 return "1000baseT";
678         }
679
680         return "unknown";
681 }
682
683 static int rtl8366s_sw_get_port_link(struct switch_dev *dev,
684                                      const struct switch_attr *attr,
685                                      struct switch_val *val)
686 {
687         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
688         u32 len = 0, data = 0;
689
690         if (val->port_vlan >= RTL8366S_NUM_PORTS)
691                 return -EINVAL;
692
693         memset(smi->buf, '\0', sizeof(smi->buf));
694         rtl8366_smi_read_reg(smi, RTL8366S_PORT_LINK_STATUS_BASE +
695                              (val->port_vlan / 2), &data);
696
697         if (val->port_vlan % 2)
698                 data = data >> 8;
699
700         if (data & RTL8366S_PORT_STATUS_LINK_MASK) {
701                 len = snprintf(smi->buf, sizeof(smi->buf),
702                                 "port:%d link:up speed:%s %s-duplex %s%s%s",
703                                 val->port_vlan,
704                                 rtl8366s_speed_str(data &
705                                           RTL8366S_PORT_STATUS_SPEED_MASK),
706                                 (data & RTL8366S_PORT_STATUS_DUPLEX_MASK) ?
707                                         "full" : "half",
708                                 (data & RTL8366S_PORT_STATUS_TXPAUSE_MASK) ?
709                                         "tx-pause ": "",
710                                 (data & RTL8366S_PORT_STATUS_RXPAUSE_MASK) ?
711                                         "rx-pause " : "",
712                                 (data & RTL8366S_PORT_STATUS_AN_MASK) ?
713                                         "nway ": "");
714         } else {
715                 len = snprintf(smi->buf, sizeof(smi->buf), "port:%d link: down",
716                                 val->port_vlan);
717         }
718
719         val->value.s = smi->buf;
720         val->len = len;
721
722         return 0;
723 }
724
725 static int rtl8366s_sw_get_vlan_info(struct switch_dev *dev,
726                                      const struct switch_attr *attr,
727                                      struct switch_val *val)
728 {
729         int i;
730         u32 len = 0;
731         struct rtl8366_vlan_4k vlan4k;
732         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
733         char *buf = smi->buf;
734         int err;
735
736         if (val->port_vlan == 0 || val->port_vlan >= RTL8366S_NUM_VLANS)
737                 return -EINVAL;
738
739         memset(buf, '\0', sizeof(smi->buf));
740
741         err = rtl8366s_get_vlan_4k(smi, val->port_vlan, &vlan4k);
742         if (err)
743                 return err;
744
745         len += snprintf(buf + len, sizeof(smi->buf) - len,
746                         "VLAN %d: Ports: '", vlan4k.vid);
747
748         for (i = 0; i < RTL8366S_NUM_PORTS; i++) {
749                 if (!(vlan4k.member & (1 << i)))
750                         continue;
751
752                 len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i,
753                                 (vlan4k.untag & (1 << i)) ? "" : "t");
754         }
755
756         len += snprintf(buf + len, sizeof(smi->buf) - len,
757                         "', members=%04x, untag=%04x, fid=%u",
758                         vlan4k.member, vlan4k.untag, vlan4k.fid);
759
760         val->value.s = buf;
761         val->len = len;
762
763         return 0;
764 }
765
766 static int rtl8366s_sw_set_port_led(struct switch_dev *dev,
767                                     const struct switch_attr *attr,
768                                     struct switch_val *val)
769 {
770         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
771         u32 data;
772         u32 mask;
773         u32 reg;
774
775         if (val->port_vlan >= RTL8366S_NUM_PORTS ||
776             (1 << val->port_vlan) == RTL8366S_PORT_UNKNOWN)
777                 return -EINVAL;
778
779         if (val->port_vlan == RTL8366S_PORT_NUM_CPU) {
780                 reg = RTL8366S_LED_BLINKRATE_REG;
781                 mask = 0xF << 4;
782                 data = val->value.i << 4;
783         } else {
784                 reg = RTL8366S_LED_CTRL_REG;
785                 mask = 0xF << (val->port_vlan * 4),
786                 data = val->value.i << (val->port_vlan * 4);
787         }
788
789         return rtl8366_smi_rmwr(smi, RTL8366S_LED_BLINKRATE_REG, mask, data);
790 }
791
792 static int rtl8366s_sw_get_port_led(struct switch_dev *dev,
793                                     const struct switch_attr *attr,
794                                     struct switch_val *val)
795 {
796         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
797         u32 data = 0;
798
799         if (val->port_vlan >= RTL8366S_NUM_LEDGROUPS)
800                 return -EINVAL;
801
802         rtl8366_smi_read_reg(smi, RTL8366S_LED_CTRL_REG, &data);
803         val->value.i = (data >> (val->port_vlan * 4)) & 0x000F;
804
805         return 0;
806 }
807
808 static int rtl8366s_sw_reset_port_mibs(struct switch_dev *dev,
809                                        const struct switch_attr *attr,
810                                        struct switch_val *val)
811 {
812         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
813
814         if (val->port_vlan >= RTL8366S_NUM_PORTS)
815                 return -EINVAL;
816
817
818         return rtl8366_smi_rmwr(smi, RTL8366S_MIB_CTRL_REG,
819                                 0, (1 << (val->port_vlan + 3)));
820 }
821
822 static int rtl8366s_sw_get_port_mib(struct switch_dev *dev,
823                                     const struct switch_attr *attr,
824                                     struct switch_val *val)
825 {
826         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
827         int i, len = 0;
828         unsigned long long counter = 0;
829         char *buf = smi->buf;
830
831         if (val->port_vlan >= smi->num_ports)
832                 return -EINVAL;
833
834         len += snprintf(buf + len, sizeof(smi->buf) - len,
835                         "Port %d MIB counters\n",
836                         val->port_vlan);
837
838         for (i = 0; i < smi->num_mib_counters; ++i) {
839                 len += snprintf(buf + len, sizeof(smi->buf) - len,
840                                 "%-36s: ", smi->mib_counters[i].name);
841                 if (!smi->ops->get_mib_counter(smi, i, val->port_vlan,
842                                                &counter))
843                         len += snprintf(buf + len, sizeof(smi->buf) - len,
844                                         "%llu\n", counter);
845                 else
846                         len += snprintf(buf + len, sizeof(smi->buf) - len,
847                                         "%s\n", "error");
848         }
849
850         val->value.s = buf;
851         val->len = len;
852         return 0;
853 }
854
855 static int rtl8366s_sw_get_vlan_ports(struct switch_dev *dev,
856                                       struct switch_val *val)
857 {
858         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
859         struct switch_port *port;
860         struct rtl8366_vlan_4k vlan4k;
861         int i;
862
863         if (val->port_vlan == 0 || val->port_vlan >= RTL8366S_NUM_VLANS)
864                 return -EINVAL;
865
866         rtl8366s_get_vlan_4k(smi, val->port_vlan, &vlan4k);
867
868         port = &val->value.ports[0];
869         val->len = 0;
870         for (i = 0; i < RTL8366S_NUM_PORTS; i++) {
871                 if (!(vlan4k.member & BIT(i)))
872                         continue;
873
874                 port->id = i;
875                 port->flags = (vlan4k.untag & BIT(i)) ?
876                                         0 : BIT(SWITCH_PORT_FLAG_TAGGED);
877                 val->len++;
878                 port++;
879         }
880         return 0;
881 }
882
883 static int rtl8366s_sw_set_vlan_ports(struct switch_dev *dev,
884                                       struct switch_val *val)
885 {
886         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
887         struct switch_port *port;
888         u32 member = 0;
889         u32 untag = 0;
890         int i;
891
892         if (val->port_vlan == 0 || val->port_vlan >= RTL8366S_NUM_VLANS)
893                 return -EINVAL;
894
895         port = &val->value.ports[0];
896         for (i = 0; i < val->len; i++, port++) {
897                 member |= BIT(port->id);
898
899                 if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
900                         untag |= BIT(port->id);
901         }
902
903         return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);
904 }
905
906 static int rtl8366s_sw_reset_switch(struct switch_dev *dev)
907 {
908         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
909         int err;
910
911         err = rtl8366s_reset_chip(smi);
912         if (err)
913                 return err;
914
915         err = rtl8366s_hw_init(smi);
916         if (err)
917                 return err;
918
919         return rtl8366_reset_vlan(smi);
920 }
921
922 static struct switch_attr rtl8366s_globals[] = {
923         {
924                 .type = SWITCH_TYPE_INT,
925                 .name = "enable_vlan",
926                 .description = "Enable VLAN mode",
927                 .set = rtl8366s_sw_set_vlan_enable,
928                 .get = rtl8366s_sw_get_vlan_enable,
929                 .max = 1,
930                 .ofs = 1
931         }, {
932                 .type = SWITCH_TYPE_INT,
933                 .name = "enable_vlan4k",
934                 .description = "Enable VLAN 4K mode",
935                 .set = rtl8366s_sw_set_vlan_enable,
936                 .get = rtl8366s_sw_get_vlan_enable,
937                 .max = 1,
938                 .ofs = 2
939         }, {
940                 .type = SWITCH_TYPE_NOVAL,
941                 .name = "reset_mibs",
942                 .description = "Reset all MIB counters",
943                 .set = rtl8366s_sw_reset_mibs,
944         }, {
945                 .type = SWITCH_TYPE_INT,
946                 .name = "blinkrate",
947                 .description = "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
948                 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
949                 .set = rtl8366s_sw_set_blinkrate,
950                 .get = rtl8366s_sw_get_blinkrate,
951                 .max = 5
952         },
953 };
954
955 static struct switch_attr rtl8366s_port[] = {
956         {
957                 .type = SWITCH_TYPE_STRING,
958                 .name = "link",
959                 .description = "Get port link information",
960                 .max = 1,
961                 .set = NULL,
962                 .get = rtl8366s_sw_get_port_link,
963         }, {
964                 .type = SWITCH_TYPE_NOVAL,
965                 .name = "reset_mib",
966                 .description = "Reset single port MIB counters",
967                 .set = rtl8366s_sw_reset_port_mibs,
968         }, {
969                 .type = SWITCH_TYPE_STRING,
970                 .name = "mib",
971                 .description = "Get MIB counters for port",
972                 .max = 33,
973                 .set = NULL,
974                 .get = rtl8366s_sw_get_port_mib,
975         }, {
976                 .type = SWITCH_TYPE_INT,
977                 .name = "led",
978                 .description = "Get/Set port group (0 - 3) led mode (0 - 15)",
979                 .max = 15,
980                 .set = rtl8366s_sw_set_port_led,
981                 .get = rtl8366s_sw_get_port_led,
982         },
983 };
984
985 static struct switch_attr rtl8366s_vlan[] = {
986         {
987                 .type = SWITCH_TYPE_STRING,
988                 .name = "info",
989                 .description = "Get vlan information",
990                 .max = 1,
991                 .set = NULL,
992                 .get = rtl8366s_sw_get_vlan_info,
993         },
994 };
995
996 /* template */
997 static struct switch_dev rtl8366_switch_dev = {
998         .name = "RTL8366S",
999         .cpu_port = RTL8366S_PORT_NUM_CPU,
1000         .ports = RTL8366S_NUM_PORTS,
1001         .vlans = RTL8366S_NUM_VLANS,
1002         .attr_global = {
1003                 .attr = rtl8366s_globals,
1004                 .n_attr = ARRAY_SIZE(rtl8366s_globals),
1005         },
1006         .attr_port = {
1007                 .attr = rtl8366s_port,
1008                 .n_attr = ARRAY_SIZE(rtl8366s_port),
1009         },
1010         .attr_vlan = {
1011                 .attr = rtl8366s_vlan,
1012                 .n_attr = ARRAY_SIZE(rtl8366s_vlan),
1013         },
1014
1015         .get_vlan_ports = rtl8366s_sw_get_vlan_ports,
1016         .set_vlan_ports = rtl8366s_sw_set_vlan_ports,
1017         .get_port_pvid = rtl8366_sw_get_port_pvid,
1018         .set_port_pvid = rtl8366_sw_set_port_pvid,
1019         .reset_switch = rtl8366s_sw_reset_switch,
1020 };
1021
1022 static int rtl8366s_switch_init(struct rtl8366_smi *smi)
1023 {
1024         struct switch_dev *dev = &smi->sw_dev;
1025         int err;
1026
1027         memcpy(dev, &rtl8366_switch_dev, sizeof(struct switch_dev));
1028         dev->priv = smi;
1029         dev->devname = dev_name(smi->parent);
1030
1031         err = register_switch(dev, NULL);
1032         if (err)
1033                 dev_err(smi->parent, "switch registration failed\n");
1034
1035         return err;
1036 }
1037
1038 static void rtl8366s_switch_cleanup(struct rtl8366_smi *smi)
1039 {
1040         unregister_switch(&smi->sw_dev);
1041 }
1042
1043 static int rtl8366s_mii_read(struct mii_bus *bus, int addr, int reg)
1044 {
1045         struct rtl8366_smi *smi = bus->priv;
1046         u32 val = 0;
1047         int err;
1048
1049         err = rtl8366s_read_phy_reg(smi, addr, 0, reg, &val);
1050         if (err)
1051                 return 0xffff;
1052
1053         return val;
1054 }
1055
1056 static int rtl8366s_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
1057 {
1058         struct rtl8366_smi *smi = bus->priv;
1059         u32 t;
1060         int err;
1061
1062         err = rtl8366s_write_phy_reg(smi, addr, 0, reg, val);
1063         /* flush write */
1064         (void) rtl8366s_read_phy_reg(smi, addr, 0, reg, &t);
1065
1066         return err;
1067 }
1068
1069 static int rtl8366s_mii_bus_match(struct mii_bus *bus)
1070 {
1071         return (bus->read == rtl8366s_mii_read &&
1072                 bus->write == rtl8366s_mii_write);
1073 }
1074
1075 static int rtl8366s_setup(struct rtl8366_smi *smi)
1076 {
1077         int ret;
1078
1079         ret = rtl8366s_reset_chip(smi);
1080         if (ret)
1081                 return ret;
1082
1083         ret = rtl8366s_hw_init(smi);
1084         return ret;
1085 }
1086
1087 static int rtl8366s_detect(struct rtl8366_smi *smi)
1088 {
1089         u32 chip_id = 0;
1090         u32 chip_ver = 0;
1091         int ret;
1092
1093         ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_ID_REG, &chip_id);
1094         if (ret) {
1095                 dev_err(smi->parent, "unable to read chip id\n");
1096                 return ret;
1097         }
1098
1099         switch (chip_id) {
1100         case RTL8366S_CHIP_ID_8366:
1101                 break;
1102         default:
1103                 dev_err(smi->parent, "unknown chip id (%04x)\n", chip_id);
1104                 return -ENODEV;
1105         }
1106
1107         ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_VERSION_CTRL_REG,
1108                                    &chip_ver);
1109         if (ret) {
1110                 dev_err(smi->parent, "unable to read chip version\n");
1111                 return ret;
1112         }
1113
1114         dev_info(smi->parent, "RTL%04x ver. %u chip found\n",
1115                  chip_id, chip_ver & RTL8366S_CHIP_VERSION_MASK);
1116
1117         return 0;
1118 }
1119
1120 static struct rtl8366_smi_ops rtl8366s_smi_ops = {
1121         .detect         = rtl8366s_detect,
1122         .setup          = rtl8366s_setup,
1123
1124         .mii_read       = rtl8366s_mii_read,
1125         .mii_write      = rtl8366s_mii_write,
1126
1127         .get_vlan_mc    = rtl8366s_get_vlan_mc,
1128         .set_vlan_mc    = rtl8366s_set_vlan_mc,
1129         .get_vlan_4k    = rtl8366s_get_vlan_4k,
1130         .set_vlan_4k    = rtl8366s_set_vlan_4k,
1131         .get_mc_index   = rtl8366s_get_mc_index,
1132         .set_mc_index   = rtl8366s_set_mc_index,
1133         .get_mib_counter = rtl8366_get_mib_counter,
1134 };
1135
1136 static int __init rtl8366s_probe(struct platform_device *pdev)
1137 {
1138         static int rtl8366_smi_version_printed;
1139         struct rtl8366s_platform_data *pdata;
1140         struct rtl8366_smi *smi;
1141         int err;
1142
1143         if (!rtl8366_smi_version_printed++)
1144                 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1145                        " version " RTL8366S_DRIVER_VER"\n");
1146
1147         pdata = pdev->dev.platform_data;
1148         if (!pdata) {
1149                 dev_err(&pdev->dev, "no platform data specified\n");
1150                 err = -EINVAL;
1151                 goto err_out;
1152         }
1153
1154         smi = rtl8366_smi_alloc(&pdev->dev);
1155         if (!smi) {
1156                 err = -ENOMEM;
1157                 goto err_out;
1158         }
1159
1160         smi->gpio_sda = pdata->gpio_sda;
1161         smi->gpio_sck = pdata->gpio_sck;
1162         smi->ops = &rtl8366s_smi_ops;
1163         smi->cpu_port = RTL8366S_PORT_NUM_CPU;
1164         smi->num_ports = RTL8366S_NUM_PORTS;
1165         smi->num_vlan_mc = RTL8366S_NUM_VLANS;
1166         smi->mib_counters = rtl8366s_mib_counters;
1167         smi->num_mib_counters = ARRAY_SIZE(rtl8366s_mib_counters);
1168
1169         err = rtl8366_smi_init(smi);
1170         if (err)
1171                 goto err_free_smi;
1172
1173         platform_set_drvdata(pdev, smi);
1174
1175         err = rtl8366s_switch_init(smi);
1176         if (err)
1177                 goto err_clear_drvdata;
1178
1179         return 0;
1180
1181  err_clear_drvdata:
1182         platform_set_drvdata(pdev, NULL);
1183         rtl8366_smi_cleanup(smi);
1184  err_free_smi:
1185         kfree(smi);
1186  err_out:
1187         return err;
1188 }
1189
1190 static int rtl8366s_phy_config_init(struct phy_device *phydev)
1191 {
1192         if (!rtl8366s_mii_bus_match(phydev->bus))
1193                 return -EINVAL;
1194
1195         return 0;
1196 }
1197
1198 static int rtl8366s_phy_config_aneg(struct phy_device *phydev)
1199 {
1200         return 0;
1201 }
1202
1203 static struct phy_driver rtl8366s_phy_driver = {
1204         .phy_id         = 0x001cc960,
1205         .name           = "Realtek RTL8366S",
1206         .phy_id_mask    = 0x1ffffff0,
1207         .features       = PHY_GBIT_FEATURES,
1208         .config_aneg    = rtl8366s_phy_config_aneg,
1209         .config_init    = rtl8366s_phy_config_init,
1210         .read_status    = genphy_read_status,
1211         .driver         = {
1212                 .owner = THIS_MODULE,
1213         },
1214 };
1215
1216 static int __devexit rtl8366s_remove(struct platform_device *pdev)
1217 {
1218         struct rtl8366_smi *smi = platform_get_drvdata(pdev);
1219
1220         if (smi) {
1221                 rtl8366s_switch_cleanup(smi);
1222                 platform_set_drvdata(pdev, NULL);
1223                 rtl8366_smi_cleanup(smi);
1224                 kfree(smi);
1225         }
1226
1227         return 0;
1228 }
1229
1230 static struct platform_driver rtl8366s_driver = {
1231         .driver = {
1232                 .name           = RTL8366S_DRIVER_NAME,
1233                 .owner          = THIS_MODULE,
1234         },
1235         .probe          = rtl8366s_probe,
1236         .remove         = __devexit_p(rtl8366s_remove),
1237 };
1238
1239 static int __init rtl8366s_module_init(void)
1240 {
1241         int ret;
1242         ret = platform_driver_register(&rtl8366s_driver);
1243         if (ret)
1244                 return ret;
1245
1246         ret = phy_driver_register(&rtl8366s_phy_driver);
1247         if (ret)
1248                 goto err_platform_unregister;
1249
1250         return 0;
1251
1252  err_platform_unregister:
1253         platform_driver_unregister(&rtl8366s_driver);
1254         return ret;
1255 }
1256 module_init(rtl8366s_module_init);
1257
1258 static void __exit rtl8366s_module_exit(void)
1259 {
1260         phy_driver_unregister(&rtl8366s_phy_driver);
1261         platform_driver_unregister(&rtl8366s_driver);
1262 }
1263 module_exit(rtl8366s_module_exit);
1264
1265 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC);
1266 MODULE_VERSION(RTL8366S_DRIVER_VER);
1267 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1268 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1269 MODULE_LICENSE("GPL v2");
1270 MODULE_ALIAS("platform:" RTL8366S_DRIVER_NAME);