From patchwork Mon Sep 9 13:00:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 1159734 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GAc+6JgF"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 46RpFS2cNxz9sCJ for ; Mon, 9 Sep 2019 23:01:16 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 11FCAC21E13; Mon, 9 Sep 2019 13:00:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 7A830C21DC1; Mon, 9 Sep 2019 13:00:27 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 19E04C21BE5; Mon, 9 Sep 2019 13:00:25 +0000 (UTC) Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by lists.denx.de (Postfix) with ESMTPS id A9599C21C2C for ; Mon, 9 Sep 2019 13:00:23 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id m9so6510318pls.8 for ; Mon, 09 Sep 2019 06:00:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=iZJKIQvo4Wj5fouH9q3Bdth//liBfjANIrzQxOEWEfU=; b=GAc+6JgFpLW33jTKiBGA45vl3T5YEYePD6ZjirrwA8WPmd6cR40GO5bPzE098QrnJt 6yyo5HSLxeMPkXvrNjyPNN2lvukjQ99ljJucOffg6GCePF0d0NoJ4Wr9UGNv98SeYdav xlyOaZWCqrA74VIJFDBzxxq0pkM/XUrRV02mSgBXz4012v3LmP+wp/+nU4pyduuDTfrZ SdzMkbdiAh51A3ndIxCcjJ7pXCRcxWRalvFCaJztihODruV5Qtau7H5hyKdIxbgUFFDN l2Pfnw1yRBcpZz/JiG8AU7wE0cDMie63LxVJoQ1iOEWpif7MDQjsDOpdYSI/ljmsjfYl jgDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=iZJKIQvo4Wj5fouH9q3Bdth//liBfjANIrzQxOEWEfU=; b=j/sfJX04DUREr2EP/kQ5aoJ8AWvDe3YH6L0WpWFlmyjuvOhnRae+RTVZzIRoUZ/gsm JVow2110fsmvm3mONtYQrg7Jyxj4ikU9RTt7bPsC1FeWkK9xyjZDzmXRQwfUgbSMVl04 dnVQtkDLryjnzXhpyAeKWLZbw0wqsfJPJjGXsiVhi/QRYeRRWFzQbLyyRrUbRlSJXjc6 QOXx3tSmF//ScEiDOrJt8pX2thxo+mNUN4DgWjbsnDdQSpuLATlkNzPPm3VXY42KsfP1 99x95pPAKI1TRJPFRgo/LE9VEw9lGMVwDeggsD0CufeAfPAkhE7FjX/bL7T9u9MmlRoe v9cw== X-Gm-Message-State: APjAAAUfzVZJWsYGsTNYO2fDfP707KBLU2U0XMi8b5ibOZDFXRhPcsAP KeKkXz5pgcqZQdc0ByzCRuQ= X-Google-Smtp-Source: APXvYqzywAcLgSvFlJtsTsvZD41uRcCa2i8kD568qayA7GJI2UFMIE0kGs73M24uH3ZQn5eVWD4csA== X-Received: by 2002:a17:902:848c:: with SMTP id c12mr23942871plo.47.1568034022312; Mon, 09 Sep 2019 06:00:22 -0700 (PDT) Received: from localhost.localdomain (unknown-224-80.windriver.com. [147.11.224.80]) by smtp.gmail.com with ESMTPSA id c23sm14532997pgj.62.2019.09.09.06.00.21 (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 09 Sep 2019 06:00:21 -0700 (PDT) From: Bin Meng To: Jagan Teki , U-Boot Mailing List Date: Mon, 9 Sep 2019 06:00:02 -0700 Message-Id: <1568034003-14675-3-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1568034003-14675-1-git-send-email-bmeng.cn@gmail.com> References: <1568034003-14675-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v2 3/4] dm: spi: Check cs number before accessing slaves X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add chip select number check in spi_find_chip_select(). Signed-off-by: Bin Meng Tested-by: Jagan Teki # SoPine --- Changes in v2: - move the chip select number check to spi_find_chip_select() drivers/spi/spi-uclass.c | 45 ++++++++++++++++++++++++++------------------- include/spi.h | 3 ++- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c index 24de0b5..cdeceb5 100644 --- a/drivers/spi/spi-uclass.c +++ b/drivers/spi/spi-uclass.c @@ -179,7 +179,32 @@ int spi_chip_select(struct udevice *dev) int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp) { + struct dm_spi_ops *ops; + struct spi_cs_info info; struct udevice *dev; + int ret; + + /* + * Ask the driver. For the moment we don't have CS info. + * When we do we could provide the driver with a helper function + * to figure out what chip selects are valid, or just handle the + * request. + */ + ops = spi_get_ops(bus); + if (ops->cs_info) { + ret = ops->cs_info(bus, cs, &info); + } else { + /* + * We could assume there is at least one valid chip select. + * The driver didn't care enough to tell us. + */ + ret = 0; + } + + if (ret) { + printf("Invalid cs %d (err=%d)\n", cs, ret); + return ret; + } for (device_find_first_child(bus, &dev); dev; device_find_next_child(&dev)) { @@ -214,7 +239,6 @@ int spi_cs_is_valid(unsigned int busnum, unsigned int cs) int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info) { struct spi_cs_info local_info; - struct dm_spi_ops *ops; int ret; if (!info) @@ -223,24 +247,7 @@ int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info) /* If there is a device attached, return it */ info->dev = NULL; ret = spi_find_chip_select(bus, cs, &info->dev); - if (!ret) - return 0; - - /* - * Otherwise ask the driver. For the moment we don't have CS info. - * When we do we could provide the driver with a helper function - * to figure out what chip selects are valid, or just handle the - * request. - */ - ops = spi_get_ops(bus); - if (ops->cs_info) - return ops->cs_info(bus, cs, info); - - /* - * We could assume there is at least one valid chip select. - * The driver didn't care enough to tell us. - */ - return 0; + return ret == -ENODEV ? 0 : ret; } int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp, diff --git a/include/spi.h b/include/spi.h index cc344de..6b144bc 100644 --- a/include/spi.h +++ b/include/spi.h @@ -528,7 +528,8 @@ int spi_chip_select(struct udevice *slave); * @bus: SPI bus to search * @cs: Chip select to look for * @devp: Returns the slave device if found - * @return 0 if found, -ENODEV on error + * @return 0 if found, -EINVAL if cs is invalid, -ENODEV if no device attached, + * other -ve value on error */ int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);