Module Name:    src
Committed By:   thorpej
Date:           Wed Jan 19 05:05:45 UTC 2022

Modified Files:
        src/sys/arch/evbarm/mpcsa: mpcsa_leds.c
        src/sys/dev/spi: m25p.c mcp23xxxgpio_spi.c mcp3k.c mcp48x1.c oj6sh.c
            scmdspi.c ssdfb_spi.c tmp121.c

Log Message:
Probe / match routines should not have side-effects: do the spi_configure()
calls in the attach routines.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/evbarm/mpcsa/mpcsa_leds.c
cvs rdiff -u -r1.18 -r1.19 src/sys/dev/spi/m25p.c
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/spi/mcp23xxxgpio_spi.c \
    src/sys/dev/spi/mcp3k.c
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/spi/mcp48x1.c src/sys/dev/spi/scmdspi.c
cvs rdiff -u -r1.9 -r1.10 src/sys/dev/spi/oj6sh.c
cvs rdiff -u -r1.11 -r1.12 src/sys/dev/spi/ssdfb_spi.c
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/spi/tmp121.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/evbarm/mpcsa/mpcsa_leds.c
diff -u src/sys/arch/evbarm/mpcsa/mpcsa_leds.c:1.7 src/sys/arch/evbarm/mpcsa/mpcsa_leds.c:1.8
--- src/sys/arch/evbarm/mpcsa/mpcsa_leds.c:1.7	Sat Aug  7 16:18:50 2021
+++ src/sys/arch/evbarm/mpcsa/mpcsa_leds.c	Wed Jan 19 05:05:45 2022
@@ -1,5 +1,5 @@
-/*	$Id: mpcsa_leds.c,v 1.7 2021/08/07 16:18:50 thorpej Exp $	*/
-/*	$NetBSD: mpcsa_leds.c,v 1.7 2021/08/07 16:18:50 thorpej Exp $	*/
+/*	$Id: mpcsa_leds.c,v 1.8 2022/01/19 05:05:45 thorpej Exp $	*/
+/*	$NetBSD: mpcsa_leds.c,v 1.8 2022/01/19 05:05:45 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mpcsa_leds.c,v 1.7 2021/08/07 16:18:50 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mpcsa_leds.c,v 1.8 2022/01/19 05:05:45 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -118,14 +118,10 @@ static struct mpcsa_leds_softc *mpcsa_le
 static int
 mpcsa_leds_match(device_t parent, cfdata_t match, void *aux)
 {
-	struct spi_attach_args *sa = aux;
 
 	if (strcmp(match->cf_name, "mpcsa_leds"))
 		return 0;
 
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
-		return 0;
-
 	return 2;
 }
 
@@ -138,10 +134,18 @@ mpcsa_leds_attach(device_t parent, devic
 	struct gpiobus_attach_args gba;
 #endif
 	int n;
+	int error;
 
 	aprint_naive(": output buffer\n");
 	aprint_normal(": 74HC595 or compatible shift register(s)\n");
 
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 10MHz, error=%d\n", error);
+		return;
+	}
+
 	sc->sc_sh = sa->sa_handle;
 	sc->sc_pinstate = 0xffff;
 	callout_init(&sc->sc_c, 0);

Index: src/sys/dev/spi/m25p.c
diff -u src/sys/dev/spi/m25p.c:1.18 src/sys/dev/spi/m25p.c:1.19
--- src/sys/dev/spi/m25p.c:1.18	Fri May 14 09:25:14 2021
+++ src/sys/dev/spi/m25p.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: m25p.c,v 1.18 2021/05/14 09:25:14 jmcneill Exp $ */
+/* $NetBSD: m25p.c,v 1.19 2022/01/19 05:05:45 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.18 2021/05/14 09:25:14 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.19 2022/01/19 05:05:45 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -125,17 +125,8 @@ static int
 m25p_match(device_t parent, cfdata_t cf, void *aux)
 {
 	struct spi_attach_args *sa = aux;
-	int res;
 
-	res = spi_compatible_match(sa, cf, compat_data);
-	if (!res)
-		return res;
-
-	/* configure for 20MHz, which is the max for normal reads */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
-		res = 0;
-
-	return res;
+	return spi_compatible_match(sa, cf, compat_data);
 }
 
 static void
@@ -143,12 +134,21 @@ m25p_attach(device_t parent, device_t se
 {
 	struct m25p_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
+	int error;
 
 	sc->sc_sh = sa->sa_handle;
 
 	aprint_normal("\n");
 	aprint_naive("\n");
 
+	/* configure for 20MHz, which is the max for normal reads */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 20000000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 20MHz, error=%d\n", error);
+		return;
+	}
+
 	config_interrupts(self, m25p_doattach);
 }
 

Index: src/sys/dev/spi/mcp23xxxgpio_spi.c
diff -u src/sys/dev/spi/mcp23xxxgpio_spi.c:1.2 src/sys/dev/spi/mcp23xxxgpio_spi.c:1.3
--- src/sys/dev/spi/mcp23xxxgpio_spi.c:1.2	Mon Jan 17 19:36:54 2022
+++ src/sys/dev/spi/mcp23xxxgpio_spi.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/*      $NetBSD: mcp23xxxgpio_spi.c,v 1.2 2022/01/17 19:36:54 thorpej Exp $ */
+/*      $NetBSD: mcp23xxxgpio_spi.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014, 2022 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_spi.c,v 1.2 2022/01/17 19:36:54 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_spi.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $");
 
 /* 
  * Driver for Microchip serial I/O expanders:
@@ -175,14 +175,9 @@ static const struct mcpgpio_accessops mc
 static int
 mcpgpio_spi_match(device_t parent, cfdata_t cf, void *aux)
 {
-	struct spi_attach_args *sa = aux;
 
 	/* MCP23S17 has no way to detect it! */
 
-	/* run at 10MHz */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
-		return 0;
-
 	return 1;
 }
 
@@ -207,6 +202,14 @@ mcpgpio_spi_attach(device_t parent, devi
 	aprint_naive("\n");	
 	aprint_normal(": %s I/O Expander\n", sc->sc_variant->name);
 
+	/* run at 10MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 10MHz, error=%d\n", error);
+		return;
+	}
+
 	/*
 	 * Before we decode the topology information, ensure each
 	 * chip has IOCON.HAEN set so that it will actually decode
Index: src/sys/dev/spi/mcp3k.c
diff -u src/sys/dev/spi/mcp3k.c:1.2 src/sys/dev/spi/mcp3k.c:1.3
--- src/sys/dev/spi/mcp3k.c:1.2	Sun Nov 20 12:38:04 2016
+++ src/sys/dev/spi/mcp3k.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: mcp3k.c,v 1.2 2016/11/20 12:38:04 phx Exp $ */
+/*	$NetBSD: mcp3k.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -174,15 +174,10 @@ static struct mcp3kadc_model mcp3k_model
 static int
 mcp3kadc_match(device_t parent, cfdata_t cf, void *aux)
 {
-	struct spi_attach_args *sa = aux;
 
 	if (strcmp(cf->cf_name, "mcp3kadc") != 0)
 		return 0;
 
-	/* configure for 1MHz */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
-		return 0;
-
 	return 1;
 }
 
@@ -193,7 +188,7 @@ mcp3kadc_attach(device_t parent, device_
 	struct spi_attach_args *sa;
 	struct mcp3kadc_softc *sc;
 	struct mcp3kadc_model *model;
-	int ch, i;
+	int error, ch, i;
 
 	sa = aux;
 	sc = device_private(self);
@@ -209,6 +204,14 @@ mcp3kadc_attach(device_t parent, device_
 	    (unsigned)model->name, (unsigned)model->channels,
 	    (unsigned)model->bits);
 
+	/* configure for 1MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 1MHz, error=%d\n", error);
+		return;
+	}
+
 	/* set a default Vref in mV according to the chip's ADC resolution */
 	sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
 	    model->bits - 1 : model->bits);

Index: src/sys/dev/spi/mcp48x1.c
diff -u src/sys/dev/spi/mcp48x1.c:1.1 src/sys/dev/spi/mcp48x1.c:1.2
--- src/sys/dev/spi/mcp48x1.c:1.1	Tue Feb 25 20:09:37 2014
+++ src/sys/dev/spi/mcp48x1.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/*      $NetBSD: mcp48x1.c,v 1.1 2014/02/25 20:09:37 rkujawa Exp $ */
+/*      $NetBSD: mcp48x1.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mcp48x1.c,v 1.1 2014/02/25 20:09:37 rkujawa Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mcp48x1.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $");
 
 /* 
  * Driver for Microchip MCP4801/MCP4811/MCP4821 DAC. 
@@ -115,13 +115,9 @@ static struct mcp48x1dac_model mcp48x1_m
 static int
 mcp48x1dac_match(device_t parent, cfdata_t cf, void *aux)
 {
-	struct spi_attach_args *sa = aux;
 
 	/* MCP48x1 is a write-only device, so no way to detect it! */
 
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
-		return 0;
-
 	return 1;
 }
 
@@ -130,7 +126,7 @@ mcp48x1dac_attach(device_t parent, devic
 {
 	struct mcp48x1dac_softc *sc;
 	struct spi_attach_args *sa;
-	int cf_flags;
+	int error, cf_flags;
 
 	aprint_naive(": Digital to Analog converter\n");	
 	aprint_normal(": MCP48x1 DAC\n");
@@ -143,6 +139,13 @@ mcp48x1dac_attach(device_t parent, devic
 
 	sc->sc_dm = &mcp48x1_models[cf_flags]; /* flag value defines model */
 
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 20000000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 20MHz, error=%d\n", error);
+		return;
+	}
+
 	if(!mcp48x1dac_envsys_attach(sc)) {
 		aprint_error_dev(sc->sc_dev, "failed to attach envsys\n");
 		return;
Index: src/sys/dev/spi/scmdspi.c
diff -u src/sys/dev/spi/scmdspi.c:1.1 src/sys/dev/spi/scmdspi.c:1.2
--- src/sys/dev/spi/scmdspi.c:1.1	Tue Dec  7 17:39:54 2021
+++ src/sys/dev/spi/scmdspi.c	Wed Jan 19 05:05:45 2022
@@ -1,5 +1,5 @@
 
-/*	$NetBSD: scmdspi.c,v 1.1 2021/12/07 17:39:54 brad Exp $	*/
+/*	$NetBSD: scmdspi.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2021 Brad Spencer <b...@anduin.eldar.org>
@@ -18,7 +18,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: scmdspi.c,v 1.1 2021/12/07 17:39:54 brad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: scmdspi.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $");
 
 /*
  * SPI driver for the Sparkfun Serial motor controller.
@@ -174,10 +174,6 @@ scmdspi_match(device_t parent, cfdata_t 
 		printf("Trying to match\n");
 	}
 
-	/* configure for 1MHz and SPI mode 0 according to the data sheet */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
-		return 0;
-
 	return 1;
 }
 
@@ -186,6 +182,7 @@ scmdspi_attach(device_t parent, device_t
 {
 	struct scmd_sc *sc;
 	struct spi_attach_args *sa;
+	int error;
 
 	sa = aux;
 	sc = device_private(self);
@@ -207,6 +204,14 @@ scmdspi_attach(device_t parent, device_t
 	cv_init(&sc->sc_condvar, "scmdspicv");
 	cv_init(&sc->sc_cond_dying, "scmdspidc");
 
+	/* configure for 1MHz and SPI mode 0 according to the data sheet */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
+	if (error) {
+		aprint_error(": failed to set Mode 0 @ 1MHz, error=%d\n",
+		    error);
+		return;
+	}
+
 	/* Please note that if the pins are not set up for SPI, the attachment
 	 * will work, but it will not figure out that there are slave modules.
 	 * It is likely required that a re-enumeration be performed after the pins

Index: src/sys/dev/spi/oj6sh.c
diff -u src/sys/dev/spi/oj6sh.c:1.9 src/sys/dev/spi/oj6sh.c:1.10
--- src/sys/dev/spi/oj6sh.c:1.9	Sat Aug  7 16:19:16 2021
+++ src/sys/dev/spi/oj6sh.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: oj6sh.c,v 1.9 2021/08/07 16:19:16 thorpej Exp $	*/
+/*	$NetBSD: oj6sh.c,v 1.10 2022/01/19 05:05:45 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.9 2021/08/07 16:19:16 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.10 2022/01/19 05:05:45 thorpej Exp $");
 
 #include "opt_oj6sh.h"
 
@@ -138,8 +138,6 @@ oj6sh_match(device_t parent, cfdata_t cf
 
 	if (spi_compatible_match(sa, cf, compat_data) == 0)
 		return 0;
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 2500000))
-		return 0;
 
 	return 2;
 }
@@ -184,10 +182,18 @@ oj6sh_attach(device_t parent, device_t s
 	struct oj6sh_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
 	struct wsmousedev_attach_args a;
+	int error;
 
 	aprint_naive("\n");
 	aprint_normal(": OJ6SH-T25 Optical Joystick\n");
 
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 2500000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 2.5MHz, error=%d\n", error);
+		return;
+	}
+
 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
 
 	sc->sc_dev = self;

Index: src/sys/dev/spi/ssdfb_spi.c
diff -u src/sys/dev/spi/ssdfb_spi.c:1.11 src/sys/dev/spi/ssdfb_spi.c:1.12
--- src/sys/dev/spi/ssdfb_spi.c:1.11	Thu Aug 19 17:50:18 2021
+++ src/sys/dev/spi/ssdfb_spi.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: ssdfb_spi.c,v 1.11 2021/08/19 17:50:18 tnn Exp $ */
+/* $NetBSD: ssdfb_spi.c,v 1.12 2022/01/19 05:05:45 thorpej Exp $ */
 
 /*
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.11 2021/08/19 17:50:18 tnn Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.12 2022/01/19 05:05:45 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -102,19 +102,8 @@ static int
 ssdfb_spi_match(device_t parent, cfdata_t match, void *aux)
 {
 	struct spi_attach_args *sa = aux;
-	int res;
 
-	res = spi_compatible_match(sa, match, compat_data);
-	if (!res)
-		return res;
-
-	/*
-	 * SSD1306 and SSD1322 data sheets specify 100ns cycle time.
-	 */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
-		res = 0;
-
-	return res;
+	return spi_compatible_match(sa, match, compat_data);
 }
 
 static void
@@ -124,6 +113,7 @@ ssdfb_spi_attach(device_t parent, device
 	struct cfdata *cf = device_cfdata(self);
 	struct spi_attach_args *sa = aux;
 	int flags = cf->cf_flags;
+	int error;
 
 	sc->sc.sc_dev = self;
 	sc->sc_sh = sa->sa_handle;
@@ -136,6 +126,17 @@ ssdfb_spi_attach(device_t parent, device
 		else
 			flags |= SSDFB_PRODUCT_SSD1322_GENERIC;
 	}
+
+	/*
+	 * SSD1306 and SSD1322 data sheets specify 100ns cycle time.
+	 */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
+	if (error) {
+		aprint_error(": failed to set Mode 0 @ 10MHz, error=%d\n",
+		    error);
+		return;
+	}
+
 	/*
 	 * Note on interface modes.
 	 *

Index: src/sys/dev/spi/tmp121.c
diff -u src/sys/dev/spi/tmp121.c:1.5 src/sys/dev/spi/tmp121.c:1.6
--- src/sys/dev/spi/tmp121.c:1.5	Mon Jun 20 17:31:37 2011
+++ src/sys/dev/spi/tmp121.c	Wed Jan 19 05:05:45 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: tmp121.c,v 1.5 2011/06/20 17:31:37 pgoyette Exp $ */
+/* $NetBSD: tmp121.c,v 1.6 2022/01/19 05:05:45 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.5 2011/06/20 17:31:37 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.6 2022/01/19 05:05:45 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -71,12 +71,6 @@ CFATTACH_DECL_NEW(tmp121temp, sizeof(str
 static int
 tmp121temp_match(device_t parent, cfdata_t cf, void *aux)
 {
-	struct spi_attach_args *sa = aux;
-
-	/* configure for 10MHz */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
-		return 0;
-
 	return 1;
 }
 
@@ -85,10 +79,19 @@ tmp121temp_attach(device_t parent, devic
 {
 	struct tmp121temp_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
+	int error;
 
 	aprint_naive(": Temperature Sensor\n");	
 	aprint_normal(": TI TMP121 Temperature Sensor\n");
 
+	/* configure for 10MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
+	if (error) {
+		aprint_error_dev(self,
+		    "failed to set Mode 0 @ 10MHz, error=%d\n", error);
+		return;
+	}
+
 	sc->sc_sh = sa->sa_handle;
 
 	sc->sc_sme = sysmon_envsys_create();

Reply via email to