From: Marek Vasut Subject: [RFT][PATCH V2 06/10] net: dsa: microchip: Factor out register access opcode generation Date: Fri, 21 Dec 2018 02:58:37 +0100 Message-ID: <20181221015841.6992-7-marex@denx.de> References: <20181221015841.6992-1-marex@denx.de> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: f.fainelli@gmail.com, andrew@lunn.ch, Marek Vasut , Tristram Ha , Woojung Huh To: netdev@vger.kernel.org Return-path: Received: from mail-out.m-online.net ([212.18.0.9]:55262 "EHLO mail-out.m-online.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390765AbeLUB7E (ORCPT ); Thu, 20 Dec 2018 20:59:04 -0500 In-Reply-To: <20181221015841.6992-1-marex@denx.de> Sender: netdev-owner@vger.kernel.org List-ID: Factor out the code which sends out the register read/write opcodes to the switch, since the code differs in single bit between read and write. Signed-off-by: Marek Vasut Cc: Andrew Lunn Cc: Florian Fainelli Cc: Tristram Ha Cc: Woojung Huh --- V2: New patch --- drivers/net/dsa/microchip/ksz9477_spi.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/net/dsa/microchip/ksz9477_spi.c b/drivers/net/dsa/microchip/ksz9477_spi.c index 9ca150a472ea..3eee397f8bb4 100644 --- a/drivers/net/dsa/microchip/ksz9477_spi.c +++ b/drivers/net/dsa/microchip/ksz9477_spi.c @@ -25,19 +25,24 @@ /* Enough to read all switch port registers. */ #define SPI_TX_BUF_LEN 0x100 -static int ksz9477_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val, - unsigned int len) +static u32 ksz9477_spi_cmd(u32 reg, bool read) { u32 txbuf; - int ret; txbuf = reg & SPI_ADDR_MASK; - txbuf |= KS_SPIOP_RD << SPI_ADDR_SHIFT; + txbuf |= (read ? KS_SPIOP_RD : KS_SPIOP_WR) << SPI_ADDR_SHIFT; txbuf <<= SPI_TURNAROUND_SHIFT; txbuf = cpu_to_be32(txbuf); - ret = spi_write_then_read(spi, &txbuf, 4, val, len); - return ret; + return txbuf; +} + +static int ksz9477_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val, + unsigned int len) +{ + u32 txbuf = ksz9477_spi_cmd(reg, true); + + return spi_write_then_read(spi, &txbuf, 4, val, len); } static int ksz9477_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val, @@ -45,10 +50,7 @@ static int ksz9477_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val, { u32 *txbuf = (u32 *)val; - *txbuf = reg & SPI_ADDR_MASK; - *txbuf |= (KS_SPIOP_WR << SPI_ADDR_SHIFT); - *txbuf <<= SPI_TURNAROUND_SHIFT; - *txbuf = cpu_to_be32(*txbuf); + *txbuf = ksz9477_spi_cmd(reg, false); return spi_write(spi, txbuf, 4 + len); } -- 2.19.2