From patchwork Tue Mar 29 19:30:08 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Fleming X-Patchwork-Id: 88846 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id BDB8DB6EFE for ; Wed, 30 Mar 2011 06:46:19 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CCFBE280BC; Tue, 29 Mar 2011 21:46:03 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Pa22Us02hhdr; Tue, 29 Mar 2011 21:46:03 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0BFD4280BE; Tue, 29 Mar 2011 21:45:48 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 536E32809E for ; Tue, 29 Mar 2011 21:45:43 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fAXe+zCb6lZq for ; Tue, 29 Mar 2011 21:45:42 +0200 (CEST) X-Greylist: delayed 906 seconds by postgrey-1.27 at theia; Tue, 29 Mar 2011 21:45:32 CEST X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from VA3EHSOBE002.bigfish.com (va3ehsobe002.messaging.microsoft.com [216.32.180.12]) by theia.denx.de (Postfix) with ESMTPS id 04FC528098 for ; Tue, 29 Mar 2011 21:45:31 +0200 (CEST) Received: from mail38-va3-R.bigfish.com (10.7.14.249) by VA3EHSOBE002.bigfish.com (10.7.40.22) with Microsoft SMTP Server id 14.1.225.8; Tue, 29 Mar 2011 19:30:24 +0000 Received: from mail38-va3 (localhost.localdomain [127.0.0.1]) by mail38-va3-R.bigfish.com (Postfix) with ESMTP id ED14313B041D; Tue, 29 Mar 2011 19:30:23 +0000 (UTC) X-SpamScore: 3 X-BigFish: VS3(zzc8kzz1202hzz8275bhz2dh2a8h668h61h) X-Spam-TCS-SCL: 0:0 X-Forefront-Antispam-Report: KIP:(null); UIP:(null); IPVD:NLI; H:mail.freescale.net; RD:none; EFVD:NLI Received: from mail38-va3 (localhost.localdomain [127.0.0.1]) by mail38-va3 (MessageSwitch) id 1301427023492742_21212; Tue, 29 Mar 2011 19:30:23 +0000 (UTC) Received: from VA3EHSMHS008.bigfish.com (unknown [10.7.14.252]) by mail38-va3.bigfish.com (Postfix) with ESMTP id 67DB710D8053; Tue, 29 Mar 2011 19:30:23 +0000 (UTC) Received: from mail.freescale.net (70.37.183.190) by VA3EHSMHS008.bigfish.com (10.7.99.18) with Microsoft SMTP Server (TLS) id 14.1.225.8; Tue, 29 Mar 2011 19:30:15 +0000 Received: from az33smr01.freescale.net (10.64.34.199) by 039-SN1MMR1-002.039d.mgd.msft.net (10.84.1.15) with Microsoft SMTP Server id 14.1.270.2; Tue, 29 Mar 2011 14:30:14 -0500 Received: from localhost (firefly.am.freescale.net [10.82.123.6]) by az33smr01.freescale.net (8.13.1/8.13.0) with ESMTP id p2TJUDbd018581; Tue, 29 Mar 2011 14:30:13 -0500 (CDT) From: Andy Fleming To: Date: Tue, 29 Mar 2011 14:30:08 -0500 Message-ID: <1301427010-7429-6-git-send-email-afleming@freescale.com> X-Mailer: git-send-email 1.6.5.2.g6ff9a In-Reply-To: <1301427010-7429-5-git-send-email-afleming@freescale.com> References: <1301427010-7429-1-git-send-email-afleming@freescale.com> <1301427010-7429-2-git-send-email-afleming@freescale.com> <1301427010-7429-3-git-send-email-afleming@freescale.com> <1301427010-7429-4-git-send-email-afleming@freescale.com> <1301427010-7429-5-git-send-email-afleming@freescale.com> MIME-Version: 1.0 X-OriginatorOrg: freescale.com Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH 5/7] Add mdio command for new PHY infrastructure X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de The new mdio command doesn't have all of the features of the mii command, but it provides the necessary read/write primitives, and allows users to interact with 10G PHYs, and other PHYs which use Clause 45 of 802.3. This means that the mdio command requires a "Device Address" argument, though for clause 22 PHYs, the argument can be "-". Signed-off-by: Andy Fleming Acked-by: Detlev Zundel --- common/Makefile | 3 + common/cmd_mdio.c | 293 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 296 insertions(+), 0 deletions(-) create mode 100644 common/cmd_mdio.c diff --git a/common/Makefile b/common/Makefile index 00847ef..a644627 100644 --- a/common/Makefile +++ b/common/Makefile @@ -116,6 +116,9 @@ COBJS-$(CONFIG_MII) += miiphyutil.o COBJS-$(CONFIG_CMD_MII) += miiphyutil.o COBJS-$(CONFIG_PHYLIB) += miiphyutil.o COBJS-$(CONFIG_CMD_MII) += cmd_mii.o +ifdef CONFIG_PHYLIB +COBJS-$(CONFIG_CMD_MII) += cmd_mdio.o +endif COBJS-$(CONFIG_CMD_MISC) += cmd_misc.o COBJS-$(CONFIG_CMD_MMC) += cmd_mmc.o COBJS-$(CONFIG_MP) += cmd_mp.o diff --git a/common/cmd_mdio.c b/common/cmd_mdio.c new file mode 100644 index 0000000..3ab0bef --- /dev/null +++ b/common/cmd_mdio.c @@ -0,0 +1,293 @@ +/* + * (C) Copyright 2011 Freescale Semiconductor, Inc + * Andy Fleming + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +/* + * MDIO Commands + */ + +#include +#include +#include +#include + + +static char last_op[2]; +static uint last_data; +static uint last_addr_lo; +static uint last_addr_hi; +static uint last_devad_lo; +static uint last_devad_hi; +static uint last_reg_lo; +static uint last_reg_hi; + +static int extract_range(char *input, int *plo, int *phi) +{ + char * end; + *plo = simple_strtol(input, &end, 0); + if (end == input) + return -1; + + if (*end == '-') { + end++; + *phi = simple_strtol(end, NULL, 0); + } else if (*end == '\0') { + *phi = *plo; + } else + return -1; + + return 0; +} + +int mdio_write_ranges(struct mii_dev *bus, int addrlo, + int addrhi, int devadlo, int devadhi, + int reglo, int reghi, unsigned short data) +{ + int addr, devad, reg; + int err = 0; + + for (addr = addrlo; addr <= addrhi; addr++) { + for (devad = devadlo; devad <= devadhi; devad++) { + for (reg = reglo; reg <= reghi; reg++) { + err = bus->write(bus, addr, devad, reg, data); + + if (err) + goto err_out; + } + } + } + +err_out: + return err; +} + +int mdio_read_ranges(struct mii_dev *bus, int addrlo, + int addrhi, int devadlo, int devadhi, + int reglo, int reghi) +{ + int addr, devad, reg; + + printf("Reading from bus %s\n", bus->name); + for (addr = addrlo; addr <= addrhi; addr++) { + printf("PHY at address %d:\n", addr); + + for (devad = devadlo; devad <= devadhi; devad++) { + for (reg = reglo; reg <= reghi; reg++) { + u16 val; + val = bus->read(bus, addr, devad, reg); + + if (val < 0) { + printf("Error\n"); + + return val; + } + + if (devad >= 0) + printf("%d.", devad); + + printf("%d - 0x%x\n", reg, val & 0xffff); + } + } + } + + return 0; +} + +int extract_phy_range(char * const argv[], int argc, struct mii_dev **bus, + int *addrlo, int *addrhi) +{ + struct phy_device *phydev; + char *addrstr; + int addrlen, namelen; + char busname[MDIO_NAME_LEN]; + + if ((argc < 1) || (argc > 2)) + return -1; + + /* If there are two arguments, it's busname addr */ + if (argc == 2) { + *bus = miiphy_get_dev_by_name(argv[0]); + + if (!*bus) + return -1; + + return extract_range(argv[1], addrlo, addrhi); + } + + /* It must be one argument, here */ + + /* + * This argument can be one of three things: + * 1) Ethernet device name + * 2) bus name and address in one string (name:addr) + * 3) Just an address (use the previously-used bus) + * + * We'll assume that no ethernet device will have a name that collides + * with the format for busname:addr. So we check all buses for a PHY + * which is connected to that ethernet device. If none are found, we + * look for a ':'. If it's found, then this is busname:addr. If so, + * we split the string, find the bus, and call extract_range() on the + * addr. If we didn't find ':', then we just call extract_range() on + * the string, and see if it's an address or address range. + */ + phydev = mdio_phydev_for_ethname(argv[0]); + + if (phydev) { + *addrlo = *addrhi = phydev->addr; + *bus = phydev->bus; + + return 0; + } + + /* use strrchr to find the last string after a ':' */ + addrstr = strrchr(argv[0], ':'); + + /* + * If it doesn't exist, or is not a valid range + * check to see if the whole string is a valid range + */ + if (!addrstr || extract_range(++addrstr, addrlo, addrhi)) + return extract_range(argv[0], addrlo, addrhi); + + /* Use the length of the returned string to determine where it starts */ + addrlen = strlen(addrstr); + namelen = strlen(argv[0]) - addrlen - 1; + + if (namelen >= MDIO_NAME_LEN) + namelen = MDIO_NAME_LEN - 1; + /* Copy the name string, and search for a bus with that name */ + strncpy(busname, argv[0], namelen); + busname[namelen] = '\0'; + + *bus = miiphy_get_dev_by_name(busname); + + if (!*bus) + return -1; + + return 0; +} + +/* ---------------------------------------------------------------- */ +static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + char op[2]; + int addrlo, addrhi, reglo, reghi, devadlo, devadhi; + unsigned short data; + int pos = argc - 1; + struct mii_dev *bus; + + if (argc < 2) + return cmd_usage(cmdtp); + + /* + * We use the last specified parameters, unless new ones are + * entered. + */ + op[0] = argv[1][0]; + addrlo = last_addr_lo; + addrhi = last_addr_hi; + devadlo = last_devad_lo; + devadhi = last_devad_hi; + reglo = last_reg_lo; + reghi = last_reg_hi; + data = last_data; + + bus = mdio_get_current_dev(); + + if (flag & CMD_FLAG_REPEAT) + op[0] = last_op[0]; + + switch (op[0]) { + case 'w': + if (pos > 1) + data = simple_strtoul (argv[pos--], NULL, 16); + case 'r': + if (pos > 1) + if (extract_range(argv[pos--], ®lo, ®hi)) + return -1; + if (pos > 1) { + if (argv[pos][0] == '-') + devadlo = devadhi = MDIO_DEVAD_NONE; + else if (extract_range(argv[pos], &devadlo, &devadhi)) + return -1; + } + default: + if (pos > 2) + if (extract_phy_range(&(argv[2]), pos - 2, &bus, + &addrlo, &addrhi)) + return -1; + + break; + } + + if (op[0] == 'l') { + mdio_list_devices(); + + return 0; + } + + /* Save the chosen bus */ + miiphy_set_current_dev(bus->name); + + switch (op[0]) { + case 'w': + mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi, + reglo, reghi, data); + break; + + case 'r': + mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi, + reglo, reghi); + break; + } + + /* + * Save the parameters for repeats. + */ + last_op[0] = op[0]; + last_addr_lo = addrlo; + last_addr_hi = addrhi; + last_devad_lo = devadlo; + last_devad_hi = devadhi; + last_reg_lo = reglo; + last_reg_hi = reghi; + last_data = data; + + return 0; +} + +/***************************************************/ + +U_BOOT_CMD( + mdio, 7, 1, do_mdio, + "MDIO utility commands", + "list - List MDIO buses\n" + "mdio read - read PHY's register at \n" + "mdio write - write PHY's register at \n" + " may be:\n" + " \n" + " :\n" + " \n" + " \n" + "Addr devad, and reg may be ranges, e.g. 2-7.\n" + "devad should be '-' for standard MII (802.3 Clause 22)." +);