From: Wolfgang Denk <wd@denx.de>
To: linuxppc-dev@ozlabs.org
Cc: Wolfgang Denk <wd@denx.de>
Subject: [PATCH RFC] fs_enet/mii-fec.c: fix MII speed calculation
Date: Thu, 11 Jun 2009 22:19:03 +0200 [thread overview]
Message-ID: <1244751543-21977-2-git-send-email-wd@denx.de> (raw)
In-Reply-To: <fa686aa40906080737h64bad3cfv32155e5b819d650b@mail.gmail.com>
The MII speed calculation was incorrectly based on the CPU clock
(ppc_proc_freq) instead of the bus clock; it worked only by chance and
for some CPU clock frequencies.
This patch makes it use the correct clock and adds some error
handling.
Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Grant Likely <grant.likely@secretlab.ca>
Cc: Kumar Gala <galak@kernel.crashing.org>
---
This is mostly a fix for an old bug - it's starnge that this went
unnoticed so far. It worked only because the incorrectly computed
value was truncated due to the fact that the MII_SPEED field in the
rebister is only 6 bits wide - but, depending on the system clock
frequency, non working systems (MII_SPEED set to zero or DIS_PREAMBLE
set to one) might result as well.
This patch is marked a RFC for the following reasons:
1) drivers/net/fs_enet/mii-fec.c now uses mpc5xxx_get_mii_speed()
which makes it 5xxx specific. I don't really like this, but did
not see a clean way to avoid it either.
2) We probably should also use mpc5xxx_get_mii_speed() in
drivers/net/fec_mpc52xx.c and drivers/net/fec_mpc52xx_phy.c, which
also contain code to calculate the MII speed. But then we should
also add some error checking to the code there, and we should make
sure that only the bits that belong to the MII_SPEED field get
written when setting the MII speed.
I'm not sure if such changes are considered necessary, and if, if
they should be added to this patch or handled in a separate one.
arch/powerpc/sysdev/mpc5xxx_clocks.c | 37 ++++++++++++++++++++++++++++++++++
drivers/net/fs_enet/mii-fec.c | 9 ++++++-
2 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/arch/powerpc/sysdev/mpc5xxx_clocks.c b/arch/powerpc/sysdev/mpc5xxx_clocks.c
index 34e12f9..3e9584b 100644
--- a/arch/powerpc/sysdev/mpc5xxx_clocks.c
+++ b/arch/powerpc/sysdev/mpc5xxx_clocks.c
@@ -31,3 +31,40 @@ mpc5xxx_get_bus_frequency(struct device_node *node)
return p_bus_freq ? *p_bus_freq : 0;
}
EXPORT_SYMBOL(mpc5xxx_get_bus_frequency);
+
+/**
+ * mpc5xxx_get_get_mii_speed - Get the MII_SPEED value
+ * @node: device node
+ *
+ * Returns the MII_SPEED value for MPC512x and MPC52xx systems.
+ * The value gets computed such that the resulting MDC frequency
+ * is 2.5 MHz or lower.
+ */
+
+int
+mpc5xxx_get_mii_speed(struct of_device *ofdev)
+{
+ unsigned int clock, speed;
+
+ clock = mpc5xxx_get_bus_frequency(ofdev->node);
+
+ if (!clock) {
+ dev_err(&ofdev->dev, "could not determine IPS/IPB clock\n");
+ return -ENODEV;
+ }
+
+ /* scale for a MII clock <= 2.5 MHz */
+ speed = (clock + 2499999) / 2500000;
+
+ /* only 6 bits available for MII speed */
+ if (speed > 0x3F) {
+ speed = 0x3F;
+ dev_err(&ofdev->dev,
+ "MII clock (%d MHz) exceeds max (2.5 MHz)\n",
+ clock / speed);
+ }
+
+ /* Field is in bits 25:30 of MII_SPEED register */
+ return speed << 1;
+}
+EXPORT_SYMBOL(mpc5xxx_get_mii_speed);
diff --git a/drivers/net/fs_enet/mii-fec.c b/drivers/net/fs_enet/mii-fec.c
index 28077cc..a2693b4 100644
--- a/drivers/net/fs_enet/mii-fec.c
+++ b/drivers/net/fs_enet/mii-fec.c
@@ -36,6 +36,7 @@
#include <asm/pgtable.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
+#include <asm/mpc5xxx.h>
#include "fs_enet.h"
#include "fec.h"
@@ -152,13 +153,17 @@ static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
if (!fec->fecp)
goto out_fec;
- fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1;
+ i = mpc5xxx_get_mii_speed(ofdev);
+ if (i < 0)
+ goto out_unmap_regs;
+
+ fec->mii_speed = i;
setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
FEC_ECNTRL_ETHER_EN);
out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
- out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed);
+ clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
new_bus->phy_mask = ~0;
new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
--
1.6.0.6
next prev parent reply other threads:[~2009-06-11 20:19 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-06 20:20 [PATCH 00/04] Add support for ARIA board Wolfgang Denk
2009-05-06 20:20 ` [PATCH 01/04] mpc5121: prepare support for additional boards Wolfgang Denk
2009-05-06 20:29 ` Grant Likely
2009-05-06 20:40 ` Wolfgang Denk
2009-05-06 21:11 ` Grant Likely
2009-05-06 20:20 ` [PATCH 02/04] ARIA: add device tree source file Wolfgang Denk
2009-05-06 20:21 ` [PATCH 03/04] mpc5121: add support for ARIA board Wolfgang Denk
2009-05-06 20:21 ` [PATCH 04/04] ARIA: add default config file Wolfgang Denk
2009-05-06 20:21 ` [PATCH 05/04] *** NOT FOR RELEASE *** HACK *** Work around MII clock issue *** Wolfgang Denk
2009-05-07 8:26 ` Joakim Tjernlund
2009-05-07 9:19 ` Wolfgang Denk
2009-05-07 9:30 ` Joakim Tjernlund
2009-05-08 2:09 ` John Rigby
2009-06-06 22:16 ` Wolfgang Denk
2009-06-06 22:27 ` John Rigby
2009-06-06 23:21 ` Wolfgang Denk
2009-06-07 0:08 ` John Rigby
2009-06-07 8:20 ` Wolfram Sang
2009-06-07 20:34 ` Wolfgang Denk
2009-06-08 7:46 ` Wolfgang Grandegger
2009-06-08 8:19 ` Wolfgang Denk
2009-06-08 14:39 ` Grant Likely
2009-06-08 14:37 ` Grant Likely
2009-06-11 20:19 ` [PATCH] mpc5xxx_get_bus_frequency(): use common code on MPC512x and MPC52xx Wolfgang Denk
2009-06-17 6:14 ` Grant Likely
2009-06-17 6:21 ` Grant Likely
2009-06-11 20:19 ` Wolfgang Denk [this message]
2009-07-14 13:42 ` [PATCH v2] fs_enet/mii-fec.c: fix MII speed calculation Wolfgang Denk
2009-07-15 15:18 ` [PATCH 1/2 v3] " Wolfgang Denk
2009-07-15 17:17 ` Grant Likely
2009-07-16 21:21 ` Wolfgang Denk
2009-07-16 22:37 ` Grant Likely
2009-07-16 21:42 ` [PATCH 1/2 v4] " Wolfgang Denk
2009-07-16 22:44 ` Grant Likely
2009-07-17 12:24 ` Wolfgang Denk
2009-07-17 9:33 ` Wolfram Sang
2009-07-17 12:32 ` Wolfgang Denk
2009-07-17 12:27 ` [PATCH 1/2 v5] " Wolfgang Denk
2009-07-17 14:41 ` Grant Likely
2009-07-17 16:21 ` David Miller
2009-07-17 16:48 ` David Miller
2009-07-17 12:27 ` [PATCH 2/2 v3] MPC52xx FEC: be more conservative when setting MII_SPEED register Wolfgang Denk
2009-07-17 12:59 ` [PATCH 2/2 v4] " Wolfgang Denk
2009-07-17 14:45 ` Grant Likely
2009-07-17 17:51 ` Wolfgang Denk
2009-07-17 18:31 ` Grant Likely
2009-07-16 21:42 ` [PATCH 2/2 v2] " Wolfgang Denk
2009-07-16 22:48 ` Grant Likely
2009-07-17 12:25 ` Wolfgang Denk
2009-07-17 9:47 ` Wolfram Sang
2009-07-17 12:35 ` Wolfgang Denk
2009-07-15 15:18 ` [PATCH 2/2] " Wolfgang Denk
2009-07-15 17:18 ` Grant Likely
2009-07-16 21:21 ` Wolfgang Denk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1244751543-21977-2-git-send-email-wd@denx.de \
--to=wd@denx.de \
--cc=linuxppc-dev@ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).