Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1696552rwd; Fri, 9 Jun 2023 00:22:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6wGMP3/g/tVb2KXaoGYUeZH7JhhcfmiLQ60LzL4eLmEvAyeBp5i1usugc9EvxsQjup5Gm3 X-Received: by 2002:a05:6a00:2313:b0:636:f5f4:5308 with SMTP id h19-20020a056a00231300b00636f5f45308mr532968pfh.7.1686295369770; Fri, 09 Jun 2023 00:22:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686295369; cv=none; d=google.com; s=arc-20160816; b=G18pcaI7A9zwBnw2/ODhJ5OvsNjIaRU2kfXcIqn2Xa/GIAefhtNTEIbPCQ3xzY6R2L +3B5hRyymaQ+FIk6aN381fQy1iEW7AA3WvwApPg8nsdor0LDkBmWWSZBGg2c0ntvSyVG ZM/88HgAtfvfPIIeBoALUscfqZtNu5ub7Keab+gtnd9sTxQ35+Jq7QimKEnevrnH0kRn BZYQVYdLmo6/ADpFPy1zcY2f3hOkzniAnOGMxN4INLFYOgvEufeTcLiDIP9oPYyM8Zym HmlLO3J8M1smKIxobOfAii+M84z0EQ9r2s112tNYF5scnZI66byUl8QB6cZxAMwJqpts Y1YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=JPOvhoCuXmCk6U8hOxH+XjLUKfopqLqCV3dtzg73RMA=; b=N4ShZdISfOMMmgHiWtJOnf3RaPi9idEBGl6ffTwWNDCI3MuTYkz1IDY/dOufEq8JS6 5wmR1zTnKzNAz1PkRxzFlRQq3J/K2WXkzRM7RlIYOWen0aJyNbszeW/WbAWbHDdklfdF pK8KoEdIe4/Drj2+rWKVc09JPTKYoxyW1qRwyNrL3z6ZgnlTCtNoaTY7PV5Gelvg6jCS tZiqo22esU/5jSKu+RyoKj+bD0pAgp0dhh58Bl3nBuuWWewsEboHrxZDGNWV2xj9dOQ2 ULpllr+BPoE87KOjcLT6bTYWjkIhV2RA2IEMH+otC12aO8jb/PamYrHQXVLJxopTio3o Hbeg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 24-20020a631258000000b0053481057d3asi2210070pgs.75.2023.06.09.00.22.34; Fri, 09 Jun 2023 00:22:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236824AbjFIHKX (ORCPT + 99 others); Fri, 9 Jun 2023 03:10:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236923AbjFIHKQ (ORCPT ); Fri, 9 Jun 2023 03:10:16 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A46D930D1; Fri, 9 Jun 2023 00:10:08 -0700 (PDT) Received: from loongson.cn (unknown [10.20.42.35]) by gateway (Coremail) with SMTP id _____8CxNulP0IJkCPIAAA--.1048S3; Fri, 09 Jun 2023 15:10:07 +0800 (CST) Received: from [10.20.42.35] (unknown [10.20.42.35]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxK8pO0IJkWxQKAA--.22402S3; Fri, 09 Jun 2023 15:10:06 +0800 (CST) Subject: Re: [PATCH v12 2/2] spi: loongson: add bus driver for the loongson spi controller To: Andy Shevchenko Cc: Mark Brown , Rob Herring , Krzysztof Kozlowski , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Jianmin Lv , wanghongliang@loongson.cn, Liu Peibao , loongson-kernel@lists.loongnix.cn, zhuyinbo@loongson.cn References: <20230608072819.25930-1-zhuyinbo@loongson.cn> <20230608072819.25930-3-zhuyinbo@loongson.cn> From: zhuyinbo Message-ID: <44239068-e0ac-1dc8-337e-fb44f5266097@loongson.cn> Date: Fri, 9 Jun 2023 15:10:05 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8DxK8pO0IJkWxQKAA--.22402S3 X-CM-SenderInfo: 52kx5xhqerqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 在 2023/6/8 下午6:15, Andy Shevchenko 写道: > On Thu, Jun 8, 2023 at 10:28 AM Yinbo Zhu wrote: >> >> This bus driver supports the Loongson SPI hardware controller in the >> Loongson platforms and supports to use DTS and PCI framework to > > the use okay, I got it. > >> register SPI device resources. > > Thank you for an update. I have a few nit-picks below, but in general > this version is good (esp. if you address them) > Reviewed-by: Andy Shevchenko > You're welcome, I will add the reviewed-by in v13. ... >> +static void loongson_spi_set_cs(struct spi_device *spi, bool val) >> +{ >> + int cs; >> + struct loongson_spi *loongson_spi = spi_controller_get_devdata(spi->controller); >> + >> + cs = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_SFCS_REG) >> + & ~(0x11 << spi_get_chipselect(spi, 0)); >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SFCS_REG, >> + (val ? (0x11 << spi_get_chipselect(spi, 0)) : >> + (0x1 << spi_get_chipselect(spi, 0))) | cs); > > Can be done as > > static void loongson_spi_set_cs(struct spi_device *spi, bool en) > > unsigned char mask = (BIT(4) | BIT(0)) << spi_get_chipselect(spi, 0); > unsigned char val = en ? mask : (BIT(0) << spi_get_chipselect(spi, 0)); > > cs = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_SFCS_REG) & ~mask; > loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SFCS_REG, val | cs); okay, I will do it. > > (Renamed variables to be consistent with the other uses in the driver below) sorry, I don't got it. Are you referring to the following changes ? loongson_spi_set_cs(spi, 1) => loongson_spi_set_cs(spi, true) > >> +} >> + >> +static void loongson_spi_set_clk(struct loongson_spi *loongson_spi, unsigned int hz) >> +{ >> + unsigned char val; >> + unsigned int div, div_tmp; >> + static const char rdiv[12] = {0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10, 11}; >> + >> + div = clamp_val(DIV_ROUND_UP_ULL(loongson_spi->clk_rate, hz), 2, 4096); >> + div_tmp = rdiv[fls(div - 1)]; >> + loongson_spi->spcr = (div_tmp & GENMASK(1, 0)) >> 0; >> + loongson_spi->sper = (div_tmp & GENMASK(3, 2)) >> 2; >> + val = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_SPCR_REG); > > val &= GENMASK(1, 0); This seems to be "val &= ~GENMASK(1, 0);" > >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SPCR_REG, (val & ~3) | >> + loongson_spi->spcr); > > loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SPCR_REG, val > | loongson_spi->spcr); okay, I got it. > >> + val = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_SPER_REG); > > val &= GENMASK(1, 0); This seems to be "val &= ~GENMASK(1, 0);" > >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SPER_REG, (val & ~3) | >> + loongson_spi->sper); > > loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SPER_REG, val > | loongson_spi->sper); > okay, I got it. >> + loongson_spi->hz = hz; >> +} >> + >> +static void loongson_spi_set_mode(struct loongson_spi *loongson_spi, >> + struct spi_device *spi) >> +{ >> + unsigned char val; >> + >> + val = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_SPCR_REG); >> + val &= ~(LOONGSON_SPI_SPCR_CPOL | LOONGSON_SPI_SPCR_CPHA); >> + if (spi->mode & SPI_CPOL) >> + val |= LOONGSON_SPI_SPCR_CPOL; >> + if (spi->mode & SPI_CPHA) >> + val |= LOONGSON_SPI_SPCR_CPHA; >> + >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_SPCR_REG, val); >> + loongson_spi->mode |= spi->mode; >> +} >> + >> +static int loongson_spi_update_state(struct loongson_spi *loongson_spi, >> + struct spi_device *spi, struct spi_transfer *t) >> +{ >> + if (t && loongson_spi->hz != t->speed_hz) >> + loongson_spi_set_clk(loongson_spi, t->speed_hz); >> + >> + if ((spi->mode ^ loongson_spi->mode) & SPI_MODE_X_MASK) >> + loongson_spi_set_mode(loongson_spi, spi); >> + >> + return 0; >> +} >> + >> +static int loongson_spi_setup(struct spi_device *spi) >> +{ >> + struct loongson_spi *loongson_spi; >> + >> + loongson_spi = spi_controller_get_devdata(spi->controller); >> + if (spi->bits_per_word % 8) >> + return -EINVAL; >> + >> + if (spi_get_chipselect(spi, 0) >= spi->controller->num_chipselect) >> + return -EINVAL; >> + >> + loongson_spi->hz = 0; >> + loongson_spi_set_cs(spi, 1); >> + >> + return 0; >> +} >> + >> +static int loongson_spi_write_read_8bit(struct spi_device *spi, const u8 **tx_buf, >> + u8 **rx_buf, unsigned int num) >> +{ >> + int ret; >> + struct loongson_spi *loongson_spi = spi_controller_get_devdata(spi->controller); >> + >> + if (tx_buf && *tx_buf) >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_FIFO_REG, *((*tx_buf)++)); >> + else >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_FIFO_REG, 0); > > + Blank line okay, I will add the blank line. > >> + ret = readb_poll_timeout(loongson_spi->base + LOONGSON_SPI_SPSR_REG, loongson_spi->spsr, >> + (loongson_spi->spsr & 0x1) != LOONGSON_SPI_SPSR_RFEMPTY, 1, MSEC_PER_SEC); > > (loongson_spi->spsr & > LOONGSON_SPI_SPSR_RFEMPTY) != LOONGSON_SPI_SPSR_RFEMPTY, > 1, MSEC_PER_SEC); okay, I got it. > >> + if (rx_buf && *rx_buf) >> + *(*rx_buf)++ = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_FIFO_REG); >> + else >> + loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_FIFO_REG); >> + >> + return ret; >> +} >> + >> +static int loongson_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer) >> +{ >> + int ret; >> + unsigned int count; >> + const u8 *tx = xfer->tx_buf; >> + u8 *rx = xfer->rx_buf; >> + >> + count = xfer->len; > >> + > > Unneeded blank line. okay, I will remove the blank line. > >> + do { >> + ret = loongson_spi_write_read_8bit(spi, &tx, &rx, count); >> + if (ret) >> + break; >> + } while (--count); >> + >> + return ret; >> +} >> + >> +static int loongson_spi_prepare_message(struct spi_controller *ctlr, struct spi_message *m) >> +{ >> + struct loongson_spi *loongson_spi = spi_controller_get_devdata(ctlr); > >> + loongson_spi->para = loongson_spi_read_reg(loongson_spi, LOONGSON_SPI_PARA_REG); > >> + loongson_spi_write_reg(loongson_spi, LOONGSON_SPI_PARA_REG, loongson_spi->para & ~1); > > BIT(0) ? > LOONGSON_SPI_PARA_MEM_EN ? I will use LOONGSON_SPI_PARA_MEM_EN. > >> + return 0; >> +} >> + ... >> +int loongson_spi_init_controller(struct device *dev, void __iomem *regs) >> +{ >> + struct spi_controller *controller; >> + struct loongson_spi *spi; >> + struct clk *clk; >> + >> + controller = devm_spi_alloc_host(dev, sizeof(struct loongson_spi)); >> + if (controller == NULL) >> + return -ENOMEM; >> + >> + controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; > > ... = SPI_MODE_X_MASK | SPI_CS_HIGH; okay, I got it. Thanks, Yinbo