net: dsa: ksz9477: add I2C managed mode support
Subsystems (5)
PATCH 1/2 net: dsa: ksz9477: add I2C managed mode support
In this mode the switch device and the internal phys will be managed via I2C interface.
--- drivers/net/dsa/microchip/Kconfig | 6 + drivers/net/dsa/microchip/Makefile | 1 + drivers/net/dsa/microchip/ksz9477_i2c.c | 258 ++++++++++++++++++++++++ 3 files changed, 265 insertions(+) create mode 100644 drivers/net/dsa/microchip/ksz9477_i2c.c
drivers/net/dsa/microchip/Kconfig +6 -0
@@ -14,3 +14,9 @@ config NET_DSA_MICROCHIP_KSZ9477_SPI depends on NET_DSA_MICROCHIP_KSZ9477 && SPI help Select to enable support for registering switches configured through SPI. + +config NET_DSA_MICROCHIP_KSZ9477_I2C + tristate "KSZ series I2C connected switch driver" + depends on NET_DSA_MICROCHIP_KSZ9477 && I2C + help + Select to enable support for registering switches configured through I2C.
drivers/net/dsa/microchip/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_common.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477) += ksz9477.o obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_SPI) += ksz9477_spi.o +obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_I2C) += ksz9477_i2c.o
drivers/net/dsa/microchip/ksz9477_i2c.c new file +258 -0
258 lines not on this page · raw message
Signature · 1 line
2.19.1
PATCH 2/2 dt-bindings: net: dsa: ksz9477: add sample of switch bindings managed in i2c mode
Add device-tree binding example of the ksz9477 switch managed in i2c mode.
--- .../devicetree/bindings/net/dsa/ksz.txt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+)
Documentation/devicetree/bindings/net/dsa/ksz.txt +50 -0
@@ -74,3 +74,53 @@ Ethernet switch connected via SPI to the host, CPU port wired to eth0: }; }; }; + +Ethernet switch connected via I2C to the host, CPU port wired to eth0: + + eth0: ethernet@10001000 { + fixed-link { + speed = <1000>; + full-duplex; + }; + }; + + i2c0: i2c@f8008000 { + ksz9897: ksz9897@0 { + compatible = "microchip,ksz9897"; + reg = <5f>; + + ports { + #address-cells = <1>; + #size-cells = <0>; + port@0 { + reg = <0>; + label = "lan1"; + }; + port@1 { + reg = <1>; + label = "lan2"; + }; + port@2 { + reg = <2>; + label = "lan3"; + }; + port@3 { + reg = <3>; + label = "lan4"; + }; + port@4 { + reg = <4>; + label = "lan5"; + }; + port@6 { + reg = <6>; + label = "cpu"; + ethernet = <ð0>; + fixed-link { + speed = <1000>; + full-duplex; + }; + }; + }; + }; + };
Signature · 1 line
2.19.1
Andrew Lunn
maintainerRe: [PATCH 2/2] dt-bindings: net: dsa: ksz9477: add sample of switch bindings managed in i2c mode
Quoted 18 lines · Sergio Paracuellos
Add device-tree binding example of the ksz9477 switch managed in i2c mode.
Signed-off-by Sergio Paracuellos <[email protected]>--- .../devicetree/bindings/net/dsa/ksz.txt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+)Documentation/devicetree/bindings/net/dsa/ksz.txt +2
-0@@ -74,3 +74,53 @@ Ethernet switch connected via SPI to the host, CPU port wired to eth0: }; }; }; + +Ethernet switch connected via I2C to the host, CPU port wired to eth0:
Documentation/devicetree/bindings/net/dsa/ksz.txt Ethernet switch connected via SPI to the host, CPU port wired to eth0: · line 88
Quoted 10 lines
Documentation/devicetree/bindings/net/dsa/ksz.txt +10
-0+ + eth0: ethernet@10001000 { + fixed-link { + speed = <1000>; + full-duplex; + }; + }; + + i2c0: i2c@f8008000 { + ksz9897: ksz9897@0 {
Hi Sergio
You should use a generic name here. Plus the @X needs to be the same as the reg value. So switch: ksz9897@5f.
Andrew
Re: [PATCH 2/2] dt-bindings: net: dsa: ksz9477: add sample of switch bindings managed in i2c mode
Quoted 24 lines · Andrew Lunn
Quoted 22 lines · Sergio Paracuellos
Add device-tree binding example of the ksz9477 switch managed in i2c mode.
Signed-off-by Sergio Paracuellos <[email protected]>--- .../devicetree/bindings/net/dsa/ksz.txt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+)Documentation/devicetree/bindings/net/dsa/ksz.txt +6
-0@@ -74,3 +74,53 @@ Ethernet switch connected via SPI to the host, CPU port wired to eth0: }; }; }; + +Ethernet switch connected via I2C to the host, CPU port wired to eth0: + + eth0: ethernet@10001000 { + fixed-link { + speed = <1000>;
Quoted 9 lines
Documentation/devicetree/bindings/net/dsa/ksz.txt Ethernet switch connected via SPI to the host, CPU port wired to eth0: · line 88
Quoted 6 lines
Documentation/devicetree/bindings/net/dsa/ksz.txt +6
-0+ full-duplex; + }; + }; + + i2c0: i2c@f8008000 { + ksz9897: ksz9897@0 {Hi Sergio
Hi Andrew,
Thanks for such a fast review :-)
Quoted 2 lines
You should use a generic name here. Plus the @X needs to be the same as the reg value. So switch: ksz9897@5f.
Ok, I'll change this. Copy paste is bad friend sometimes.
Quoted 2 lines
Andrew
Best regards,
Sergio Paracuellos
Andrew Lunn
maintainerdrivers/net/dsa/microchip/ksz9477_i2c.c line 29
Quoted 6 lines · Sergio Paracuellos
drivers/net/dsa/microchip/ksz9477_i2c.c new file +6
-0+static int ksz_i2c_read_reg (struct i2c_client *client,u32 reg,u8 *val, +unsigned int len) +{ +struct i2c_adapter *adapter = client->adapter; +struct i2c_msg msg[2 ]; +u8 txd[2 ];
Hi Sergio
I'm not sure that having the TX buffer on the stack is safe. If the i2c bus master is using DMA, you then DMA from the stack, which some architectures memory models do no allow. You have to use memory which comes from an alloc function.
Quoted 4 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +4
-0+int ret; + + txd[0 ] = (u8)(reg >>8 ); + txd[1 ] = (u8 )reg;
drivers/net/dsa/microchip/ksz9477_i2c.c line 43
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+ + msg[0 ].addr = client->addr; + msg[0 ].flags =0 ; + msg[0 ].len =2 ; + msg[0 ].buf = txd; + + msg[1 ].addr = client->addr; + msg[1 ].flags =I2C_M_RD ; + msg[1 ].len = len; + msg[1 ].buf = val;
You potentially have the same issue with val.
Andrew
Quoted 9 lines · Andrew Lunn
drivers/net/dsa/microchip/ksz9477_i2c.c line 29
Quoted 6 lines · Sergio Paracuellos
drivers/net/dsa/microchip/ksz9477_i2c.c new file +6
-0+static int ksz_i2c_read_reg (struct i2c_client *client,u32 reg,u8 *val, +unsigned int len) +{ +struct i2c_adapter *adapter = client->adapter; +struct i2c_msg msg[2 ]; +u8 txd[2 ];
Hi Andrew,
Quoted 7 lines
Hi Sergio
I'm not sure that having the TX buffer on the stack is safe. If the i2c bus master is using DMA, you then DMA from the stack, which some architectures memory models do no allow. You have to use memory which comes from an alloc function.
Quoted 17 lines
Quoted 4 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +4
-0+int ret; + + txd[0 ] = (u8)(reg >>8 ); + txd[1 ] = (u8 )reg;drivers/net/dsa/microchip/ksz9477_i2c.c line 43
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+ + msg[0 ].addr = client->addr; + msg[0 ].flags =0 ; + msg[0 ].len =2 ; + msg[0 ].buf = txd; + + msg[1 ].addr = client->addr; + msg[1 ].flags =I2C_M_RD ; + msg[1 ].len = len; + msg[1 ].buf = val;You potentially have the same issue with val.
I'll change these two to to get memory from kernel allocators instead of using the stack. Thanks for let me know this.
Quoted 1 line
Andrew
Best regards,
Sergio Paracuellos
Andrew Lunn
maintainerQuoted 2 lines
I'll change these two to to get memory from kernel allocators instead of using the stack. Thanks for let me know this.
There appear to be other cases as well. Please review the whole driver.
Thanks
Andrew
drivers/net/dsa/microchip/ksz9477_i2c.c line 138
Quoted 10 lines · Sergio Paracuellos
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+static int ksz_i2c_write (struct ksz_device *dev,u32 reg,u8 *val, +unsigned int len) +{ +struct i2c_client *client = dev->priv; +unsigned int cnt = len; +int i =0 ; +u8 txb[4 ]; + + do { + txb[i++] = (u8)(*val >> (8 * (cnt - 1)));
^^^^^^^
Can "cnt" be zero from ksz_i2c_set()? If so this loop will corrupt memory.
Quoted 63 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +63
-0+ cnt--; + }while (cnt); + +return ksz_i2c_write_reg (client, reg, txb, len); +} + +static int ksz_i2c_write8 (struct ksz_device *dev,u32 reg,u8 value) +{ +return ksz_i2c_write (dev, reg, &value,1 ); +} + +static int ksz_i2c_write16 (struct ksz_device *dev,u32 reg,u16 value) +{ + value =cpu_to_be16 (value); +return ksz_i2c_write (dev, reg, (u8 *)&value,2 ); +} + +static int ksz_i2c_write24 (struct ksz_device *dev,u32 reg,u32 value) +{ +/* make it to big endian 24bit from MSB */ + value <<=8 ; + value =cpu_to_be32 (value); + +return ksz_i2c_write (dev, reg, (u8 *)&value,3 ); +} + +static int ksz_i2c_write32 (struct ksz_device *dev,u32 reg,u32 value) +{ + value =cpu_to_be32 (value); +return ksz_i2c_write (dev, reg, (u8 *)&value,4 ); +} + +static int ksz_i2c_get (struct ksz_device *dev,u32 reg,void *data,size_t len) +{ +return ksz_i2c_read (dev, reg, data, len); +} + +static int ksz_i2c_set (struct ksz_device *dev,u32 reg,void *data,size_t len) +{ +return ksz_i2c_write (dev, reg, data, len); +} + +static const struct ksz_io_ops ksz_i2c_ops = { + .read8 = ksz_i2c_read8, + .read16 = ksz_i2c_read16, + .read24 = ksz_i2c_read24, + .read32 = ksz_i2c_read32, + .write8 = ksz_i2c_write8, + .write16 = ksz_i2c_write16, + .write24 = ksz_i2c_write24, + .write32 = ksz_i2c_write32, + .get = ksz_i2c_get, + .set = ksz_i2c_set, +}; + +static int ksz_i2c_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct ksz_device *dev; +int ret; + + dev =ksz_switch_alloc (&client->dev, &ksz_i2c_ops, client); +if (!dev)
drivers/net/dsa/microchip/ksz9477_i2c.c line 211
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+return -ENOMEM ; + +if (client->dev.platform_data) + dev->pdata = client->dev.platform_data; + +i2c_set_clientdata (client, dev); + + ret =ksz9477_switch_register (dev); +if (ret) { +dev_err (&client->dev,"registering switch (ret: %d)\n" , ret);
free dev on this error path?
drivers/net/dsa/microchip/ksz9477_i2c.c line 217
Quoted 6 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +6
-0+return ret; + } + +return 0 ; +} +
regards,
dan carpenter
Hi Dan,
Thanks for the feedback.
Quoted 14 lines · Dan Carpenter
drivers/net/dsa/microchip/ksz9477_i2c.c line 138
Quoted 10 lines · Sergio Paracuellos
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+static int ksz_i2c_write (struct ksz_device *dev,u32 reg,u8 *val, +unsigned int len) +{ +struct i2c_client *client = dev->priv; +unsigned int cnt = len; +int i =0 ; +u8 txb[4 ]; + + do { + txb[i++] = (u8)(*val >> (8 * (cnt - 1)));^^^^^^^
Quoted 2 lines
Can "cnt" be zero from
ksz_i2c_set()? If so this loop will corrupt memory.
This get and set interface seems to be introduced recently but it is not being used yet, so
in this moment the answer is not. For me, there is no sense in call 'set' with no len. Should
I add a check for zero len and return EINVAL just in case?
Quoted 69 lines
Quoted 69 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +68
-0+ cnt--; + }while (cnt); + +return ksz_i2c_write_reg (client, reg, txb, len); +} + +static int ksz_i2c_write8 (struct ksz_device *dev,u32 reg,u8 value) +{ +return ksz_i2c_write (dev, reg, &value,1 ); +} + +static int ksz_i2c_write16 (struct ksz_device *dev,u32 reg,u16 value) +{ + value =cpu_to_be16 (value); +return ksz_i2c_write (dev, reg, (u8 *)&value,2 ); +} + +static int ksz_i2c_write24 (struct ksz_device *dev,u32 reg,u32 value) +{ +/* make it to big endian 24bit from MSB */ + value <<=8 ; + value =cpu_to_be32 (value); + +return ksz_i2c_write (dev, reg, (u8 *)&value,3 ); +} + +static int ksz_i2c_write32 (struct ksz_device *dev,u32 reg,u32 value) +{ + value =cpu_to_be32 (value); +return ksz_i2c_write (dev, reg, (u8 *)&value,4 ); +} + +static int ksz_i2c_get (struct ksz_device *dev,u32 reg,void *data,size_t len) +{ +return ksz_i2c_read (dev, reg, data, len); +} + +static int ksz_i2c_set (struct ksz_device *dev,u32 reg,void *data,size_t len) +{ +return ksz_i2c_write (dev, reg, data, len); +} + +static const struct ksz_io_ops ksz_i2c_ops = { + .read8 = ksz_i2c_read8, + .read16 = ksz_i2c_read16, + .read24 = ksz_i2c_read24, + .read32 = ksz_i2c_read32, + .write8 = ksz_i2c_write8, + .write16 = ksz_i2c_write16, + .write24 = ksz_i2c_write24, + .write32 = ksz_i2c_write32, + .get = ksz_i2c_get, + .set = ksz_i2c_set, +}; + +static int ksz_i2c_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct ksz_device *dev; +int ret; + + dev =ksz_switch_alloc (&client->dev, &ksz_i2c_ops, client); +if (!dev) +return -ENOMEM ; + +if (client->dev.platform_data) + dev->pdata = client->dev.platform_data; +
Quoted 8 lines
drivers/net/dsa/microchip/ksz9477_i2c.c line 211
Quoted 5 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +5
-0+i2c_set_clientdata (client, dev); + + ret =ksz9477_switch_register (dev); +if (ret) { +dev_err (&client->dev,"registering switch (ret: %d)\n" , ret);free dev on this error path?
Internally all of this are using devm_* funcions, so I think is there is no need to free anything. Also, the
spi managed driver for this does nothing also about this.
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c line 217
Quoted 7 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +6
-0+return ret; + } + +return 0 ; +} +regards,
dan carpenter
Best regards,
Sergio Paracuellos
Quoted 12 lines · Sergio Paracuellos
Quoted 12 lines
drivers/net/dsa/microchip/ksz9477_i2c.c line 138
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+static int ksz_i2c_write (struct ksz_device *dev,u32 reg,u8 *val, +unsigned int len) +{ +struct i2c_client *client = dev->priv; +unsigned int cnt = len; +int i =0 ; +u8 txb[4 ]; + + do { + txb[i++] = (u8)(*val >> (8 * (cnt - 1)));^^^^^^^
Quoted 9 lines
Quoted 2 lines
Can "cnt" be zero from
ksz_i2c_set()? If so this loop will corrupt memory.This get and set interface seems to be introduced recently but it is not being used yet, so
in this moment the answer is not. For me, there is no sense in call 'set' with no len. Should
I add a check for zero len and return EINVAL just in case?
Yes, please.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +13
-0+static int ksz_i2c_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct ksz_device *dev; +int ret; + + dev =ksz_switch_alloc (&client->dev, &ksz_i2c_ops, client); +if (!dev) +return -ENOMEM ; + +if (client->dev.platform_data) + dev->pdata = client->dev.platform_data; +Quoted 8 lines
drivers/net/dsa/microchip/ksz9477_i2c.c line 211
Quoted 5 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +5
-0+i2c_set_clientdata (client, dev); + + ret =ksz9477_switch_register (dev); +if (ret) { +dev_err (&client->dev,"registering switch (ret: %d)\n" , ret);free dev on this error path?
Quoted 3 lines
Internally all of this are using devm_* funcions, so I think is there is no need to free anything. Also, the
spi managed driver for this does nothing also about this.
You're right. My bad. I should have looked at this.
regards,
dan carpenter
Quoted 17 lines · Dan Carpenter
Quoted 15 lines · Sergio Paracuellos
Quoted 14 lines
drivers/net/dsa/microchip/ksz9477_i2c.c line 138
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+static int ksz_i2c_write (struct ksz_device *dev,u32 reg,u8 *val, +unsigned int len) +{ +struct i2c_client *client = dev->priv; +unsigned int cnt = len; +int i =0 ; +u8 txb[4 ]; + + do { + txb[i++] = (u8)(*val >> (8 * (cnt - 1)));^^^^^^^Can "cnt" be zero from
ksz_i2c_set()? If so this loop will corrupt memory.
Quoted 8 lines
Quoted 6 lines
This get and set interface seems to be introduced recently but it is not being used yet, so
in this moment the answer is not. For me, there is no sense in call 'set' with no len. Should
I add a check for zero len and return EINVAL just in case?Yes, please.
Done. v3 with this change already sent.
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 9 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +8
-0+static int ksz_i2c_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct ksz_device *dev; +int ret; + + dev =ksz_switch_alloc (&client->dev, &ksz_i2c_ops, client); +if (!dev)drivers/net/dsa/microchip/ksz9477_i2c.c line 211
Quoted 10 lines
drivers/net/dsa/microchip/ksz9477_i2c.c new file +10
-0+return -ENOMEM ; + +if (client->dev.platform_data) + dev->pdata = client->dev.platform_data; + +i2c_set_clientdata (client, dev); + + ret =ksz9477_switch_register (dev); +if (ret) { +dev_err (&client->dev,"registering switch (ret: %d)\n" , ret);free dev on this error path?
Quoted 9 lines
Quoted 3 lines
Internally all of this are using devm_* funcions, so I think is there is no need to free anything. Also, the
spi managed driver for this does nothing also about this.You're right. My bad. I should have looked at this.
regards,
dan carpenter
Best regards,
Sergio Paracuellos