Skip to contentSkip to conversation

using mii-bitbang on different processor ports

PATCH Superseded Sergej Stepanov 3 participants 5 messages Oct 30, 2007 – Nov 2, 2007 Network drivers

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 using mii-bitbang on different processor ports

The patch makes possible to have mdio and mdc pins on different physical ports also for CONFIG_PPC_CPM_NEW_BINDING.
To setup it in the device tree:
reg = <10d40 14 10d60 14>; // mdc-offset: 0x10d40, mdio-offset: 0x10d60 or
reg = <10d40 14>; // mdc and mdio have the same offset 0x10d40 The approach was taken from older version.

--

drivers/net/fs_enet/mii-bitbang.c +47 -25
index b8e4a73..86b73ea 100644
@@ -29,12 +29,16 @@
 
 #include "fs_enet.h"
 
-struct bb_info {
-	struct mdiobb_ctrl ctrl;
+struct bb_port {
 	__be32 __iomem *dir;
 	__be32 __iomem *dat;
-	u32 mdio_msk;
-	u32 mdc_msk;
+	u32 msk;
+};
+
+struct bb_info {
+	struct mdiobb_ctrl ctrl;
+	struct bb_port mdc;
+	struct bb_port mdio;
 };
 
 /* FIXME: If any other users of GPIO crop up, then these will have to
@@ -62,18 +66,18 @@ static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
 	struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
 
 	if (dir)
-		bb_set(bitbang->dir, bitbang->mdio_msk);
+		bb_set(bitbang->mdio.dir, bitbang->mdio.msk);
 	else
-		bb_clr(bitbang->dir, bitbang->mdio_msk);
+		bb_clr(bitbang->mdio.dir, bitbang->mdio.msk);
 
 	/* Read back to flush the write. */
-	in_be32(bitbang->dir);
+	in_be32(bitbang->mdio.dir);
 }
 
 static inline int mdio_read(struct mdiobb_ctrl *ctrl)
 {
 	struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
-	return bb_read(bitbang->dat, bitbang->mdio_msk);
+	return bb_read(bitbang->mdio.dat, bitbang->mdio.msk);
 }
 
 static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
@@ -81,12 +85,12 @@ static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
 	struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
 
 	if (what)
-		bb_set(bitbang->dat, bitbang->mdio_msk);
+		bb_set(bitbang->mdio.dat, bitbang->mdio.msk);
 	else
-		bb_clr(bitbang->dat, bitbang->mdio_msk);
+		bb_clr(bitbang->mdio.dat, bitbang->mdio.msk);
 
 	/* Read back to flush the write. */
-	in_be32(bitbang->dat);
+	in_be32(bitbang->mdio.dat);
 }
 
 static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
@@ -94,12 +98,12 @@ static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
 	struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
 
 	if (what)
-		bb_set(bitbang->dat, bitbang->mdc_msk);
+		bb_set(bitbang->mdc.dat, bitbang->mdc.msk);
 	else
-		bb_clr(bitbang->dat, bitbang->mdc_msk);
+		bb_clr(bitbang->mdc.dat, bitbang->mdc.msk);
 
 	/* Read back to flush the write. */
-	in_be32(bitbang->dat);
+	in_be32(bitbang->mdc.dat);
 }
 
 static struct mdiobb_ops bb_ops = {
@@ -114,23 +118,23 @@ static struct mdiobb_ops bb_ops = {
 static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
                                          struct device_node *np)
 {
-	struct resource res;
+	struct resource res[2];
 	const u32 *data;
 	int mdio_pin, mdc_pin, len;
 	struct bb_info *bitbang = bus->priv;
 
-	int ret = of_address_to_resource(np, 0, &res);
+	int ret = of_address_to_resource(np, 0, &res[0]);
 	if (ret)
 		return ret;
 
-	if (res.end - res.start < 13)
+	if (res[0].end - res[0].start < 13)
 		return -ENODEV;
 
 	/* This should really encode the pin number as well, but all
 	 * we get is an int, and the odds of multiple bitbang mdio buses
 	 * is low enough that it's not worth going too crazy.
 	 */
-	bus->id = res.start;
+	bus->id = res[0].start;
 
 	data = of_get_property(np, "fsl,mdio-pin", &len);
 	if (!data || len != 4)
@@ -142,13 +146,27 @@ static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
 		return -ENODEV;
 	mdc_pin = *data;
 
-	bitbang->dir = ioremap(res.start, res.end - res.start + 1);
-	if (!bitbang->dir)
+	bitbang->mdc.dir = ioremap(res[0].start, res[0].end - res[0].start + 1);
+	if (!bitbang->mdc.dir)
 		return -ENOMEM;
 
-	bitbang->dat = bitbang->dir + 4;
-	bitbang->mdio_msk = 1 << (31 - mdio_pin);
-	bitbang->mdc_msk = 1 << (31 - mdc_pin);
+	bitbang->mdc.dat = bitbang->mdc.dir + 4;
+	if( !of_address_to_resource(np, 1, &res[1]))
+	{
+		bitbang->mdio.dir = ioremap(res[1].start, res[1].end - res[1].start + 1);
+		if (!bitbang->mdio.dir)
+		{
+			iounmap(bitbang->mdc.dir);
+			return -ENOMEM;
+		}
+		bitbang->mdio.dat = bitbang->mdio.dir + 4;
+	}
+	else{
+		bitbang->mdio.dir = bitbang->mdc.dir;
+		bitbang->mdio.dat = bitbang->mdc.dat;
+	}
+	bitbang->mdio.msk = 1 << (31 - mdio_pin);
+	bitbang->mdc.msk = 1 << (31 - mdc_pin);
 
 	return 0;
 }
@@ -220,7 +238,9 @@ out_free_irqs:
 	dev_set_drvdata(&ofdev->dev, NULL);
 	kfree(new_bus->irq);
 out_unmap_regs:
-	iounmap(bitbang->dir);
+	if ( bitbang->mdio.dir != bitbang->mdc.dir)
+		iounmap(bitbang->mdio.dir);
+	iounmap(bitbang->mdc.dir);
 out_free_bus:
 	kfree(new_bus);
 out_free_priv:
@@ -238,7 +258,9 @@ static int fs_enet_mdio_remove(struct of_device *ofdev)
 	free_mdio_bitbang(bus);
 	dev_set_drvdata(&ofdev->dev, NULL);
 	kfree(bus->irq);
-	iounmap(bitbang->dir);
+	if ( bitbang->mdio.dir != bitbang->mdc.dir)
+		iounmap(bitbang->mdio.dir);
+	iounmap(bitbang->mdc.dir);
 	kfree(bitbang);
 	kfree(bus);
#2 lore raw
In reply to Scott Wood

Hello Scott.
Thank you for reply.
Am Dienstag, den 30.10.2007, 11:32 -0500 schrieb Scott Wood:

Quoted 4 lines · Sergej Stepanov

You could just use of_iomap() for the second one, since we don't need the physical address for bus->id.

Nice tip.
Than it would be needless-------

				|
				\/
Quoted 5 lines

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 160

Quoted 2 lines
drivers/net/fs_enet/mii-bitbang.c +2 -0
+			iounmap(bitbang->mdc.dir);
+			return -ENOMEM;

Please use the goto-style error handling that's used elsewhere in the function.

Regards
Sergej.

#3 lore raw
Quoted 15 lines · Sergej Stepanov

Hello Scott.
Thank you for reply.
Am Dienstag, den 30.10.2007, 11:32 -0500 schrieb Scott Wood:

Quoted 4 lines · Sergej Stepanov

You could just use of_iomap() for the second one, since we don't need the physical address for bus->id.

Nice tip.
Than it would be needless-------

				|
				\/
Quoted 4 lines

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 160

Quoted 2 lines
drivers/net/fs_enet/mii-bitbang.c +2 -0
+			iounmap(bitbang->mdc.dir);
+			return -ENOMEM;

Please use the goto-style error handling that's used elsewhere in the function.

Hmm... in this case, it'd be impossible to tell using of_iomap() whether a failure was due to reg only having one resource (and thus meaning the same one should be used for both), or due to ioremap() failure. Maybe we should keep it separate.

-Scott

#4 lore raw
In reply to Sergej Stepanov

drivers/net/fs_enet/mii-bitbang.c static struct mdiobb_ops bb_ops = { · line 121

Quoted 6 lines · On Tue, 30 Oct 2007 17:09:19 +0100 Sergej Stepanov <[email protected]>…

static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
struct device_node *np)
{

drivers/net/fs_enet/mii-bitbang.c +1 -1
-	struct resource res;
+	struct resource res[2];

Why not just reuse the same resource structure? You don't seem to need them both at the same time.

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 164

Quoted 11 lines
drivers/net/fs_enet/mii-bitbang.c +11 -0
+	if( !of_address_to_resource(np, 1, &res[1]))
+	{
+		bitbang->mdio.dir = ioremap(res[1].start, res[1].end - res[1].start + 1);
+		if (!bitbang->mdio.dir)
+		{
+			iounmap(bitbang->mdc.dir);
+			return -ENOMEM;
+		}
+		bitbang->mdio.dat = bitbang->mdio.dir + 4;
+	}
+	else{

Formatting:

	if () {
	} else {
	}

Review

Quoted 1 line
(quoted diff) +1 -0
+	if ( bitbang->mdio.dir != bitbang->mdc.dir)

No spaces after (, please. Here and elsewhere.

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/
#1 lore raw
In reply to Sergej Stepanov
Quoted 7 lines · Sergej Stepanov

The patch makes possible to have mdio and mdc pins on different physical ports also for CONFIG_PPC_CPM_NEW_BINDING.
To setup it in the device tree:
reg = <10d40 14 10d60 14>; // mdc-offset: 0x10d40, mdio-offset: 0x10d60 or
reg = <10d40 14>; // mdc and mdio have the same offset 0x10d40 The approach was taken from older version.

There are some formatting issues in fs_mii_bitbang_init(), but otherwise it looks good. It'll need to be sent to Jeff Garzik and the netdev list, not just linuxppc-dev, though.

Also, please update Documentation/powerpc/booting-without-of.txt (probably in a separate patch, since that one would go through Paul).

Quoted 5 lines
drivers/net/fs_enet/mii-bitbang.c +0 -1
@@ -142,13 +146,27 @@ static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
 		return -ENODEV;
 	mdc_pin = *data;
 
-	bitbang->dir = ioremap(res.start, res.end - res.start + 1);

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 154

Quoted 10 lines
drivers/net/fs_enet/mii-bitbang.c +4 -4
-	if (!bitbang->dir)
+	bitbang->mdc.dir = ioremap(res[0].start, res[0].end - res[0].start + 1);
+	if (!bitbang->mdc.dir)
 		return -ENOMEM;
 
-	bitbang->dat = bitbang->dir + 4;
-	bitbang->mdio_msk = 1 << (31 - mdio_pin);
-	bitbang->mdc_msk = 1 << (31 - mdc_pin);
+	bitbang->mdc.dat = bitbang->mdc.dir + 4;
+	if( !of_address_to_resource(np, 1, &res[1]))

Space before the '(', not after. A newline after the previous line would be nice, too.

Review static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 155

Quoted 1 line
(quoted diff) +1 -0
+	{

Brace at the end of the previous line.

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 158

Quoted 3 lines
drivers/net/fs_enet/mii-bitbang.c +3 -0
+		bitbang->mdio.dir = ioremap(res[1].start, res[1].end - res[1].start + 1);
+		if (!bitbang->mdio.dir)
+		{

Likewise.

You could just use of_iomap() for the second one, since we don't need the physical address for bus->id.

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 160

Quoted 2 lines
drivers/net/fs_enet/mii-bitbang.c +2 -0
+			iounmap(bitbang->mdc.dir);
+			return -ENOMEM;

Please use the goto-style error handling that's used elsewhere in the function.

drivers/net/fs_enet/mii-bitbang.c static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, · line 164

Quoted 4 lines
drivers/net/fs_enet/mii-bitbang.c +4 -0
+		}
+		bitbang->mdio.dat = bitbang->mdio.dir + 4;
+	}
+	else{

} else {

Quoted 9 lines
drivers/net/fs_enet/mii-bitbang.c +3 -1
 out_unmap_regs:
-	iounmap(bitbang->dir);
+	if ( bitbang->mdio.dir != bitbang->mdc.dir)
+		iounmap(bitbang->mdio.dir);
+	iounmap(bitbang->mdc.dir);
 out_free_bus:
 	kfree(new_bus);
 out_free_priv:

drivers/net/fs_enet/mii-bitbang.c static int fs_enet_mdio_remove(struct of_device *ofdev) · line 265

Quoted 9 lines
drivers/net/fs_enet/mii-bitbang.c +3 -1
@@ -238,7 +258,9 @@ static int fs_enet_mdio_remove(struct of_device *ofdev)
 	free_mdio_bitbang(bus);
 	dev_set_drvdata(&ofdev->dev, NULL);
 	kfree(bus->irq);
-	iounmap(bitbang->dir);
+	if ( bitbang->mdio.dir != bitbang->mdc.dir)
+		iounmap(bitbang->mdio.dir);
+	iounmap(bitbang->mdc.dir);
 	kfree(bitbang);
 	kfree(bus);

"if (bitbang", not "if ( bitbang".

-Scott