3033813ec0b94d7c37bbdf9112a943bdc8191adb
[openwrt.git] / target / linux / generic-2.6 / files / drivers / net / phy / adm6996.c
1 /*
2  * ADM6996 switch driver
3  *
4  * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
5  *
6  * This program is free software; you can redistribute  it and/or modify it
7  * under  the terms of the GNU General Public License v2 as published by the
8  * Free Software Foundation
9  */
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/errno.h>
13 #include <linux/unistd.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
21 #include <linux/spinlock.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/mii.h>
25 #include <linux/ethtool.h>
26 #include <linux/phy.h>
27
28 #include <asm/io.h>
29 #include <asm/irq.h>
30 #include <asm/uaccess.h>
31 #include "adm6996.h"
32
33 MODULE_DESCRIPTION("Infineon ADM6996 Switch");
34 MODULE_AUTHOR("Felix Fietkau");
35 MODULE_LICENSE("GPL");
36
37 struct adm6996_priv {
38         /* use abstraction for regops, we want to add gpio support in the future */
39         u16 (*read)(struct phy_device *phydev, enum admreg reg);
40         void (*write)(struct phy_device *phydev, enum admreg reg, u16 val);
41 };
42
43 #define to_adm(_phy) ((struct adm6996_priv *) (_phy)->priv)
44
45
46 static inline u16
47 r16(struct phy_device *pdev, enum admreg reg)
48 {
49         return to_adm(pdev)->read(pdev, reg);
50 }
51
52 static inline void
53 w16(struct phy_device *pdev, enum admreg reg, u16 val)
54 {
55         to_adm(pdev)->write(pdev, reg, val);
56 }
57
58
59 static u16
60 adm6996_read_mii_reg(struct phy_device *phydev, enum admreg reg)
61 {
62         return phydev->bus->read(phydev->bus, PHYADDR(reg));
63 }
64
65 static void
66 adm6996_write_mii_reg(struct phy_device *phydev, enum admreg reg, u16 val)
67 {
68         phydev->bus->write(phydev->bus, PHYADDR(reg), val);
69 }
70
71
72 static int adm6996_config_init(struct phy_device *pdev)
73 {
74         int i;
75
76         printk("%s: ADM6996 PHY driver attached.\n", pdev->attached_dev->name);
77         pdev->supported = ADVERTISED_100baseT_Full;
78         pdev->advertising = ADVERTISED_100baseT_Full;
79
80         /* initialize port and vlan settings */
81         for (i = 0; i < ADM_PHY_PORTS; i++) {
82                 w16(pdev, adm_portcfg[i], ADM_PORTCFG_INIT |
83                         ADM_PORTCFG_PVID((i == ADM_WAN_PORT) ? 1 : 0));
84         }
85         w16(pdev, adm_portcfg[5], ADM_PORTCFG_CPU);
86
87         /* reset all ports */
88         for (i = 0; i < ADM_PHY_PORTS; i++) {
89                 w16(pdev, ADM_PHY_PORT(i), ADM_PHYCFG_INIT);
90         }
91
92         return 0;
93 }
94
95 static int adm6996_read_status(struct phy_device *phydev)
96 {
97         phydev->speed = SPEED_100;
98         phydev->duplex = DUPLEX_FULL;
99         phydev->state = PHY_UP;
100         return 0;
101 }
102
103 static int adm6996_config_aneg(struct phy_device *phydev)
104 {
105         return 0;
106 }
107
108 static int adm6996_probe(struct phy_device *pdev)
109 {
110         struct adm6996_priv *priv;
111
112         priv = kzalloc(sizeof(struct adm6996_priv), GFP_KERNEL);
113         if (priv == NULL)
114                 return -ENOMEM;
115
116         priv->read = adm6996_read_mii_reg;
117         priv->write = adm6996_write_mii_reg;
118         pdev->priv = priv;
119         return 0;
120 }
121
122 static void adm6996_remove(struct phy_device *pdev)
123 {
124         kfree(pdev->priv);
125 }
126
127 static bool adm6996_detect(struct mii_bus *bus, int addr)
128 {
129         u16 reg;
130
131         /* we only attach to phy id 0 */
132         if (addr != 0)
133                 return false;
134
135         /* look for the switch on the bus */
136         reg = bus->read(bus, PHYADDR(ADM_SIG0)) & ADM_SIG0_MASK;
137         if (reg != ADM_SIG0_VAL)
138                 return false;
139
140         reg = bus->read(bus, PHYADDR(ADM_SIG1)) & ADM_SIG1_MASK;
141         if (reg != ADM_SIG1_VAL)
142                 return false;
143
144         return true;
145 }
146
147 static struct phy_driver adm6996_driver = {
148         .name           = "Infineon ADM6996",
149         .features       = PHY_BASIC_FEATURES,
150         .detect         = adm6996_detect,
151         .probe          = adm6996_probe,
152         .remove         = adm6996_remove,
153         .config_init    = &adm6996_config_init,
154         .config_aneg    = &adm6996_config_aneg,
155         .read_status    = &adm6996_read_status,
156         .driver         = { .owner = THIS_MODULE,},
157 };
158
159 static int __init adm6996_init(void)
160 {
161         return phy_driver_register(&adm6996_driver);
162 }
163
164 static void __exit adm6996_exit(void)
165 {
166         phy_driver_unregister(&adm6996_driver);
167 }
168
169 module_init(adm6996_init);
170 module_exit(adm6996_exit);