CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-18 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Tue May 18 23:26:46 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
Correct a comment.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.6 -r1.78.2.7 src/sys/dev/i2c/i2c.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/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78.2.6 src/sys/dev/i2c/i2c.c:1.78.2.7
--- src/sys/dev/i2c/i2c.c:1.78.2.6	Mon May 17 00:05:56 2021
+++ src/sys/dev/i2c/i2c.c	Tue May 18 23:26:46 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.78.2.6 2021/05/17 00:05:56 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.7 2021/05/18 23:26:46 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.6 2021/05/17 00:05:56 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.7 2021/05/18 23:26:46 thorpej Exp $");
 
 #include 
 #include 
@@ -678,9 +678,8 @@ iic_attach(device_t parent, device_t sel
 		aprint_error_dev(self, "couldn't establish power handler\n");
 
 	/*
-	 * Attempt to enumerate the devices on the bus.  If
-	 * there is no enumeration method, or no devices are
-	 * found, then we will attempt indirect configuration.
+	 * Attempt to enumerate the devices on the bus.  If there is no
+	 * enumeration method, then we will attempt indirect configuration.
 	 */
 	struct i2c_enumerate_devices_args enumargs;
 	struct i2c_attach_args ia;



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-18 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Tue May 18 23:26:46 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
Correct a comment.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.6 -r1.78.2.7 src/sys/dev/i2c/i2c.c

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



Re: CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-17 Thread Erik Fair
I wonder if a summary report of detected RAM might be worthwhile, e.g., “4 RAM 
slots, 2 RAM DIMMs detected” - it’d be better still if the kernel could report 
the relationship between RAM present and memory controller channels (e.g., does 
it appear that RAM is spread across them for interleaving?).

just a thought while the code is being tilled,

Erik


> On May 16, 2021, at 14:07, Jason Thorpe  wrote:
> 
>> 
>> On May 16, 2021, at 1:06 PM, matthew green  wrote:
>> 
>>> Modified Files:
>>> src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c
>>> 
>>> Log Message:
>>> The last change had an unfortunate side-effect on empty DIMM slots, so
>>> roll that back.  Instead, if we used direct config, then probe for the
>>> module in the attach routine and report if the module is not present,
>>> rather than assuming that it is.
>> 
>> this seems odd to me.
>> 
>> if we're using direct config, shouldn't match() be fully
>> able to determine present or not, avoiding having to have
>> an attach() that fake-fails?
> 
> The problem is if a device tree has a node for a SPD DIMM, but the DIMM 
> *isn’t actually present* (i.e. the node is for “DIMM might live here, go 
> check!”).  Returning “no match” in this situation would result in a “… not 
> configured” message for a non-existent DIMM.
> 
>> 
>> thanks.
>> 
>> 
>> .mrg.
> 
> -- thorpej



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Mon May 17 00:05:56 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c i2cvar.h

Log Message:
Remove "count" from the device enumeration callback arguments.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.5 -r1.78.2.6 src/sys/dev/i2c/i2c.c
cvs rdiff -u -r1.24.2.4 -r1.24.2.5 src/sys/dev/i2c/i2cvar.h

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

Modified files:

Index: src/sys/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78.2.5 src/sys/dev/i2c/i2c.c:1.78.2.6
--- src/sys/dev/i2c/i2c.c:1.78.2.5	Sun May 16 21:04:59 2021
+++ src/sys/dev/i2c/i2c.c	Mon May 17 00:05:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.78.2.5 2021/05/16 21:04:59 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.6 2021/05/17 00:05:56 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.5 2021/05/16 21:04:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.6 2021/05/17 00:05:56 thorpej Exp $");
 
 #include 
 #include 
@@ -618,8 +618,6 @@ iic_enumerate_devices_callback(device_t 
 	device_t newdev;
 	bool rv __diagused;
 
-	args->count++;
-
 	loc[IICCF_ADDR] = args->ia->ia_addr;
 
 	if (args->ia->ia_addr > I2C_MAX_ADDR) {

Index: src/sys/dev/i2c/i2cvar.h
diff -u src/sys/dev/i2c/i2cvar.h:1.24.2.4 src/sys/dev/i2c/i2cvar.h:1.24.2.5
--- src/sys/dev/i2c/i2cvar.h:1.24.2.4	Fri May 14 01:08:53 2021
+++ src/sys/dev/i2c/i2cvar.h	Mon May 17 00:05:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cvar.h,v 1.24.2.4 2021/05/14 01:08:53 thorpej Exp $	*/
+/*	$NetBSD: i2cvar.h,v 1.24.2.5 2021/05/17 00:05:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -170,7 +170,6 @@ struct i2c_attach_args {
 struct i2c_enumerate_devices_args {
 	struct i2c_attach_args *ia;
 	bool (*callback)(device_t, struct i2c_enumerate_devices_args *);
-	unsigned int count;
 };
 
 /*



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Mon May 17 00:05:56 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c i2cvar.h

Log Message:
Remove "count" from the device enumeration callback arguments.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.5 -r1.78.2.6 src/sys/dev/i2c/i2c.c
cvs rdiff -u -r1.24.2.4 -r1.24.2.5 src/sys/dev/i2c/i2cvar.h

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 22:56:48 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
Tweak previous to deal better with "device tree node describes empty
DIMM slot" problem.


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.4 -r1.21.4.5 src/sys/dev/i2c/spdmem_i2c.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/dev/i2c/spdmem_i2c.c
diff -u src/sys/dev/i2c/spdmem_i2c.c:1.21.4.4 src/sys/dev/i2c/spdmem_i2c.c:1.21.4.5
--- src/sys/dev/i2c/spdmem_i2c.c:1.21.4.4	Sun May 16 22:53:57 2021
+++ src/sys/dev/i2c/spdmem_i2c.c	Sun May 16 22:56:47 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: spdmem_i2c.c,v 1.21.4.4 2021/05/16 22:53:57 thorpej Exp $ */
+/* $NetBSD: spdmem_i2c.c,v 1.21.4.5 2021/05/16 22:56:47 thorpej Exp $ */
 
 /*
  * Copyright (c) 2007 Nicolas Joly
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.4 2021/05/16 22:53:57 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.5 2021/05/16 22:56:47 thorpej Exp $");
 
 #include 
 #include 
@@ -243,7 +243,7 @@ spdmem_i2c_match(device_t parent, cfdata
 	sc.sc_page0 = SPDCTL_SPA0;
 	sc.sc_page1 = SPDCTL_SPA1;
 	sc.sc_base.sc_read = spdmem_i2c_read;
-	match_result = SPDMEM_HIGH_CONFIDENCE_MATCH;
+	match_result = 0;
 
  do_probe:
 	/* Check the bank and reset to the page 0 */
@@ -254,8 +254,6 @@ spdmem_i2c_match(device_t parent, cfdata
 		if (match_result < SPDMEM_HIGH_CONFIDENCE_MATCH) {
 			match_result = SPDMEM_HIGH_CONFIDENCE_MATCH;
 		}
-	} else {
-		match_result = 0;
 	}
 
 	return match_result;



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 22:56:48 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
Tweak previous to deal better with "device tree node describes empty
DIMM slot" problem.


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.4 -r1.21.4.5 src/sys/dev/i2c/spdmem_i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 22:53:57 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
Go back to probing in the direct config case, and return a high confidence
match value if the SPD probe succeeds.  This ensures that we beat a generic
i2c serial eeprom driver if the contents looks like they contain SPD data.


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.3 -r1.21.4.4 src/sys/dev/i2c/spdmem_i2c.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/dev/i2c/spdmem_i2c.c
diff -u src/sys/dev/i2c/spdmem_i2c.c:1.21.4.3 src/sys/dev/i2c/spdmem_i2c.c:1.21.4.4
--- src/sys/dev/i2c/spdmem_i2c.c:1.21.4.3	Sun May 16 15:40:20 2021
+++ src/sys/dev/i2c/spdmem_i2c.c	Sun May 16 22:53:57 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: spdmem_i2c.c,v 1.21.4.3 2021/05/16 15:40:20 thorpej Exp $ */
+/* $NetBSD: spdmem_i2c.c,v 1.21.4.4 2021/05/16 22:53:57 thorpej Exp $ */
 
 /*
  * Copyright (c) 2007 Nicolas Joly
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.3 2021/05/16 15:40:20 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.4 2021/05/16 22:53:57 thorpej Exp $");
 
 #include 
 #include 
@@ -190,6 +190,8 @@ static const struct device_compatible_en
 	DEVICE_COMPAT_EOL
 };
 
+#define	SPDMEM_HIGH_CONFIDENCE_MATCH	(I2C_MATCH_DIRECT_COMPATIBLE + 20)
+
 static bool
 spdmem_i2c_use_name_match(const struct i2c_attach_args *ia, int *match_resultp)
 {
@@ -197,7 +199,7 @@ spdmem_i2c_use_name_match(const struct i
 
 	if (name != NULL) {
 		*match_resultp = device_compatible_match(, 1, name_data)
-		? I2C_MATCH_DIRECT_COMPATIBLE
+		? SPDMEM_HIGH_CONFIDENCE_MATCH
 		: 0;
 		return true;
 	}
@@ -209,9 +211,6 @@ spdmem_i2c_use_direct_match(const struct
 			const struct device_compatible_entry *cdata,
 			int *match_resultp)
 {
-	if (iic_use_direct_match(ia, cf, cdata, match_resultp))
-		return true;
-
 	/*
 	 * Matching by name is not ideal, but some device trees only
 	 * have a name and no "compatible" property.
@@ -219,6 +218,9 @@ spdmem_i2c_use_direct_match(const struct
 	if (spdmem_i2c_use_name_match(ia, match_resultp))
 		return true;
 
+	if (iic_use_direct_match(ia, cf, cdata, match_resultp))
+		return true;
+
 	return false;
 }
 
@@ -230,7 +232,7 @@ spdmem_i2c_match(device_t parent, cfdata
 	int match_result;
 
 	if (spdmem_i2c_use_direct_match(ia, match, compat_data, _result))
-		return match_result;
+		goto do_probe;
 
 	/* Filter out by address when not using direct config. */
 	if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
@@ -241,14 +243,22 @@ spdmem_i2c_match(device_t parent, cfdata
 	sc.sc_page0 = SPDCTL_SPA0;
 	sc.sc_page1 = SPDCTL_SPA1;
 	sc.sc_base.sc_read = spdmem_i2c_read;
+	match_result = SPDMEM_HIGH_CONFIDENCE_MATCH;
 
+ do_probe:
 	/* Check the bank and reset to the page 0 */
 	if (spdmem_reset_page() != 0)
 		return 0;
 
-	return spdmem_common_probe(_base)
-	? I2C_MATCH_ADDRESS_AND_PROBE
-	: 0;
+	if (spdmem_common_probe(_base)) {
+		if (match_result < SPDMEM_HIGH_CONFIDENCE_MATCH) {
+			match_result = SPDMEM_HIGH_CONFIDENCE_MATCH;
+		}
+	} else {
+		match_result = 0;
+	}
+
+	return match_result;
 }
 
 static void



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 22:53:57 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
Go back to probing in the direct config case, and return a high confidence
match value if the SPD probe succeeds.  This ensures that we beat a generic
i2c serial eeprom driver if the contents looks like they contain SPD data.


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.3 -r1.21.4.4 src/sys/dev/i2c/spdmem_i2c.c

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



Re: CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason Thorpe


> On May 16, 2021, at 1:06 PM, matthew green  wrote:
> 
>> Modified Files:
>>  src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c
>> 
>> Log Message:
>> The last change had an unfortunate side-effect on empty DIMM slots, so
>> roll that back.  Instead, if we used direct config, then probe for the
>> module in the attach routine and report if the module is not present,
>> rather than assuming that it is.
> 
> this seems odd to me.
> 
> if we're using direct config, shouldn't match() be fully
> able to determine present or not, avoiding having to have
> an attach() that fake-fails?

The problem is if a device tree has a node for a SPD DIMM, but the DIMM *isn’t 
actually present* (i.e. the node is for “DIMM might live here, go check!”).  
Returning “no match” in this situation would result in a “… not configured” 
message for a non-existent DIMM.

> 
> thanks.
> 
> 
> .mrg.

-- thorpej



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 21:04:59 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
Check for errors from iic_acquire_bus() in iic_ioctl_exec().


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.4 -r1.78.2.5 src/sys/dev/i2c/i2c.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/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78.2.4 src/sys/dev/i2c/i2c.c:1.78.2.5
--- src/sys/dev/i2c/i2c.c:1.78.2.4	Sun May 16 21:03:38 2021
+++ src/sys/dev/i2c/i2c.c	Sun May 16 21:04:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.78.2.4 2021/05/16 21:03:38 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.5 2021/05/16 21:04:59 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.4 2021/05/16 21:03:38 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.5 2021/05/16 21:04:59 thorpej Exp $");
 
 #include 
 #include 
@@ -1005,7 +1005,9 @@ iic_ioctl_exec(struct iic_softc *sc, i2c
 			goto out;
 	}
 
-	iic_acquire_bus(ic, 0);
+	if ((error = iic_acquire_bus(ic, 0)) != 0) {
+		goto out;
+	}
 	error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen,
 	buf, iie->iie_buflen, 0);
 	iic_release_bus(ic, 0);



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 21:04:59 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
Check for errors from iic_acquire_bus() in iic_ioctl_exec().


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.4 -r1.78.2.5 src/sys/dev/i2c/i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 21:03:38 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
- Set D_MCLOSE in the iic_cdevsw so that we get a d_close call for
  each close so that the module ref counting works properly.
- Rearrange things a little to avoid holding the iic_mtx a long as
  previously done.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.3 -r1.78.2.4 src/sys/dev/i2c/i2c.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/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78.2.3 src/sys/dev/i2c/i2c.c:1.78.2.4
--- src/sys/dev/i2c/i2c.c:1.78.2.3	Sun May 16 04:40:08 2021
+++ src/sys/dev/i2c/i2c.c	Sun May 16 21:03:38 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.78.2.3 2021/05/16 04:40:08 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.4 2021/05/16 21:03:38 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.3 2021/05/16 04:40:08 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.4 2021/05/16 21:03:38 thorpej Exp $");
 
 #include 
 #include 
@@ -115,13 +115,6 @@ static dev_type_open(iic_open);
 static dev_type_close(iic_close);
 static dev_type_ioctl(iic_ioctl);
 
-int iic_init(void);
-
-kmutex_t iic_mtx;
-int iic_refcnt;
-
-ONCE_DECL(iic_once);
-
 const struct cdevsw iic_cdevsw = {
 	.d_open = iic_open,
 	.d_close = iic_close,
@@ -134,11 +127,16 @@ const struct cdevsw iic_cdevsw = {
 	.d_mmap = nommap,
 	.d_kqfilter = nokqfilter,
 	.d_discard = nodiscard,
-	.d_flag = D_OTHER
+	.d_flag = D_OTHER | D_MCLOSE,
 };
 
 static void	iic_smbus_intr_thread(void *);
 
+static kmutex_t iic_mtx;
+static int iic_refcnt;
+static bool iic_unloading;
+static ONCE_DECL(iic_once);
+
 static struct i2c_device_link *
 iic_devslot_lookup(struct iic_softc *sc, i2c_addr_t addr)
 {
@@ -928,22 +926,43 @@ iic_use_direct_match(const struct i2c_at
 static int
 iic_open(dev_t dev, int flag, int fmt, lwp_t *l)
 {
-	struct iic_softc *sc = device_lookup_private(_cd, minor(dev));
+	struct iic_softc *sc;
 
 	mutex_enter(_mtx);
-	if (sc == NULL) {
+
+	if (iic_unloading) {
 		mutex_exit(_mtx);
 		return ENXIO;
 	}
+
+	/* Hold a refrence while we look up the softc. */
+	if (iic_refcnt == INT_MAX) {
+		mutex_exit(_mtx);
+		return EBUSY;
+	}
 	iic_refcnt++;
+
 	mutex_exit(_mtx);
 
+	sc = device_lookup_private(_cd, minor(dev));
+
+	if (sc == NULL) {
+		mutex_enter(_mtx);
+		iic_refcnt--;
+		mutex_exit(_mtx);
+		return ENXIO;
+	}
+
 	return 0;
 }
 
 static int
 iic_close(dev_t dev, int flag, int fmt, lwp_t *l)
 {
+	struct iic_softc *sc = device_lookup_private(_cd, minor(dev));;
+
+	KASSERT(iic_refcnt != 0);
+	KASSERT(sc != NULL);
 
 	mutex_enter(_mtx);
 	iic_refcnt--;
@@ -1037,7 +1056,7 @@ MODULE(MODULE_CLASS_DRIVER, iic, "i2cexe
 #include "ioconf.c"
 #endif
 
-int
+static int
 iic_init(void)
 {
 
@@ -1084,19 +1103,26 @@ iic_modcmd(modcmd_t cmd, void *opaque)
 			mutex_exit(_mtx);
 			return EBUSY;
 		}
+		iic_unloading = true;
+		mutex_exit(_mtx);
 #ifdef _MODULE
 		error = config_fini_component(cfdriver_ioconf_iic,
 		cfattach_ioconf_iic, cfdata_ioconf_iic);
 		if (error != 0) {
+			mutex_enter(_mtx);
+			iic_unloading = false;
 			mutex_exit(_mtx);
 			break;
 		}
 		error = devsw_detach(NULL, _cdevsw);
-		if (error != 0)
+		if (error != 0) {
 			config_init_component(cfdriver_ioconf_iic,
 			cfattach_ioconf_iic, cfdata_ioconf_iic);
+			mutex_enter(_mtx);
+			iic_unloading = false;
+			mutex_exit(_mtx);
+		}
 #endif
-		mutex_exit(_mtx);
 		break;
 	default:
 		error = ENOTTY;



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 21:03:38 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
- Set D_MCLOSE in the iic_cdevsw so that we get a d_close call for
  each close so that the module ref counting works properly.
- Rearrange things a little to avoid holding the iic_mtx a long as
  previously done.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.3 -r1.78.2.4 src/sys/dev/i2c/i2c.c

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



re: CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread matthew green
> Modified Files:
>   src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c
>
> Log Message:
> The last change had an unfortunate side-effect on empty DIMM slots, so
> roll that back.  Instead, if we used direct config, then probe for the
> module in the attach routine and report if the module is not present,
> rather than assuming that it is.

this seems odd to me.

if we're using direct config, shouldn't match() be fully
able to determine present or not, avoiding having to have
an attach() that fake-fails?

thanks.


.mrg.


CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 15:40:20 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
Check for errors from iic_acquire_bus().


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.2 -r1.21.4.3 src/sys/dev/i2c/spdmem_i2c.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/dev/i2c/spdmem_i2c.c
diff -u src/sys/dev/i2c/spdmem_i2c.c:1.21.4.2 src/sys/dev/i2c/spdmem_i2c.c:1.21.4.3
--- src/sys/dev/i2c/spdmem_i2c.c:1.21.4.2	Sun May 16 15:27:12 2021
+++ src/sys/dev/i2c/spdmem_i2c.c	Sun May 16 15:40:20 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: spdmem_i2c.c,v 1.21.4.2 2021/05/16 15:27:12 thorpej Exp $ */
+/* $NetBSD: spdmem_i2c.c,v 1.21.4.3 2021/05/16 15:40:20 thorpej Exp $ */
 
 /*
  * Copyright (c) 2007 Nicolas Joly
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.2 2021/05/16 15:27:12 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.3 2021/05/16 15:40:20 thorpej Exp $");
 
 #include 
 #include 
@@ -107,7 +107,9 @@ spdmem_reset_page(struct spdmem_i2c_soft
 
 	reg = 0;
 
-	iic_acquire_bus(sc->sc_tag, 0);
+	if ((rv = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
+		return rv;
+	}
 
 	/*
 	 * Try to read byte 0 and 2. If it failed, it's not spdmem or a device
@@ -306,7 +308,8 @@ spdmem_i2c_read(struct spdmem_softc *sof
 
 	reg = addr & 0xff;
 
-	iic_acquire_bus(sc->sc_tag, 0);
+	if ((rv = iic_acquire_bus(sc->sc_tag, 0)) != 0)
+		return rv;
 
 	if (addr & 0x100) {
 		rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_page1,



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 15:40:20 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
Check for errors from iic_acquire_bus().


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.2 -r1.21.4.3 src/sys/dev/i2c/spdmem_i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 15:27:12 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
The last change had an unfortunate side-effect on empty DIMM slots, so
roll that back.  Instead, if we used direct config, then probe for the
module in the attach routine and report if the module is not present,
rather than assuming that it is.

Encapsulate the direct config logic for SPD into one place for clarity.


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.1 -r1.21.4.2 src/sys/dev/i2c/spdmem_i2c.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/dev/i2c/spdmem_i2c.c
diff -u src/sys/dev/i2c/spdmem_i2c.c:1.21.4.1 src/sys/dev/i2c/spdmem_i2c.c:1.21.4.2
--- src/sys/dev/i2c/spdmem_i2c.c:1.21.4.1	Sun May 16 05:16:21 2021
+++ src/sys/dev/i2c/spdmem_i2c.c	Sun May 16 15:27:12 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: spdmem_i2c.c,v 1.21.4.1 2021/05/16 05:16:21 thorpej Exp $ */
+/* $NetBSD: spdmem_i2c.c,v 1.21.4.2 2021/05/16 15:27:12 thorpej Exp $ */
 
 /*
  * Copyright (c) 2007 Nicolas Joly
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.1 2021/05/16 05:16:21 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.2 2021/05/16 15:27:12 thorpej Exp $");
 
 #include 
 #include 
@@ -178,6 +178,48 @@ static const struct device_compatible_en
 	DEVICE_COMPAT_EOL
 };
 
+/*
+ * Some device trees don't have real "compatible" entries, so we
+ * end up having to match by name.
+ */
+static const struct device_compatible_entry name_data[] = {
+	{ .compat = "dimm-spd" },
+	{ .compat = "dimm" },
+	DEVICE_COMPAT_EOL
+};
+
+static bool
+spdmem_i2c_use_name_match(const struct i2c_attach_args *ia, int *match_resultp)
+{
+	const char *name = ia->ia_name;
+
+	if (name != NULL) {
+		*match_resultp = device_compatible_match(, 1, name_data)
+		? I2C_MATCH_DIRECT_COMPATIBLE
+		: 0;
+		return true;
+	}
+	return false;
+}
+
+static bool
+spdmem_i2c_use_direct_match(const struct i2c_attach_args *ia, const cfdata_t cf,
+			const struct device_compatible_entry *cdata,
+			int *match_resultp)
+{
+	if (iic_use_direct_match(ia, cf, cdata, match_resultp))
+		return true;
+
+	/*
+	 * Matching by name is not ideal, but some device trees only
+	 * have a name and no "compatible" property.
+	 */
+	if (spdmem_i2c_use_name_match(ia, match_resultp))
+		return true;
+
+	return false;
+}
+
 static int
 spdmem_i2c_match(device_t parent, cfdata_t match, void *aux)
 {
@@ -185,45 +227,13 @@ spdmem_i2c_match(device_t parent, cfdata
 	struct spdmem_i2c_softc sc;
 	int match_result;
 
-	/*
-	 * SPD stands for "Serial Presence Detect".  This implies that
-	 * if we're using direct configuration that we should treat
-	 * that as a *hint*... it's entirely possible that a device
-	 * tree lists locations where SPD memory can be found, not
-	 * necessarily where memory is known to be present.
-	 *
-	 * Accordingly, if we get a direct configuration match based
-	 * on compatible data or device name, we still check to see
-	 * if the device is there.
-	 */
-
-	if (iic_use_direct_match(ia, match, compat_data, _result)) {
-		if (match_result != 0) {
-			goto do_probe;
-		}
-		return 0;
-	}
+	if (spdmem_i2c_use_direct_match(ia, match, compat_data, _result))
+		return match_result;
 
-	if (ia->ia_name) {
-		/* add other names as we find more firmware variations */
-		if (strcmp(ia->ia_name, "dimm-spd") == 0 ||
-		strcmp(ia->ia_name, "dimm") == 0) {
-			match_result = I2C_MATCH_DIRECT_SPECIFIC;
-			goto do_probe;
-		}
+	/* Filter out by address when not using direct config. */
+	if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
 		return 0;
-	}
 
-	/* As a last resort, filter out invalid addresses. */
-	if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) == SPDMEM_I2C_ADDR) {
-		match_result = I2C_MATCH_ADDRESS_AND_PROBE;
-		goto do_probe;
-	}
-	
-	/* Not a candidate address. */
-	return 0;
-
- do_probe:
 	sc.sc_tag = ia->ia_tag;
 	sc.sc_addr = ia->ia_addr;
 	sc.sc_page0 = SPDCTL_SPA0;
@@ -234,10 +244,9 @@ spdmem_i2c_match(device_t parent, cfdata
 	if (spdmem_reset_page() != 0)
 		return 0;
 
-	if (spdmem_common_probe(_base)) {
-		return match_result;
-	}
-	return 0;
+	return spdmem_common_probe(_base)
+	? I2C_MATCH_ADDRESS_AND_PROBE
+	: 0;
 }
 
 static void
@@ -245,6 +254,7 @@ spdmem_i2c_attach(device_t parent, devic
 {
 	struct spdmem_i2c_softc *sc = device_private(self);
 	struct i2c_attach_args *ia = aux;
+	int match_result;
 
 	sc->sc_tag = ia->ia_tag;
 	sc->sc_addr = ia->ia_addr;
@@ -252,10 +262,28 @@ spdmem_i2c_attach(device_t parent, devic
 	sc->sc_page1 = SPDCTL_SPA1;
 	sc->sc_base.sc_read = spdmem_i2c_read;
 
-	if (!pmf_device_register(self, NULL, NULL))
-		aprint_error_dev(self, "couldn't establish power handler\n");
+	/*
+	 * SPD stands for "Serial Presence Detect".  If we're using
+	 * direct 

CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-16 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 15:27:12 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
The last change had an unfortunate side-effect on empty DIMM slots, so
roll that back.  Instead, if we used direct config, then probe for the
module in the attach routine and report if the module is not present,
rather than assuming that it is.

Encapsulate the direct config logic for SPD into one place for clarity.


To generate a diff of this commit:
cvs rdiff -u -r1.21.4.1 -r1.21.4.2 src/sys/dev/i2c/spdmem_i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-15 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 05:16:21 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
SPD stands for "Serial Presence Detect".  This implies that
if we're using direct configuration that we should treat
that as a *hint*... it's entirely possible that a device
tree lists locations where SPD memory can be found, not
necessarily where memory is known to be present.

Accordingly, if we get a direct configuration match based
on compatible data or device name, we still check to see
if the device is there.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.21.4.1 src/sys/dev/i2c/spdmem_i2c.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/dev/i2c/spdmem_i2c.c
diff -u src/sys/dev/i2c/spdmem_i2c.c:1.21 src/sys/dev/i2c/spdmem_i2c.c:1.21.4.1
--- src/sys/dev/i2c/spdmem_i2c.c:1.21	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/spdmem_i2c.c	Sun May 16 05:16:21 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: spdmem_i2c.c,v 1.21 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: spdmem_i2c.c,v 1.21.4.1 2021/05/16 05:16:21 thorpej Exp $ */
 
 /*
  * Copyright (c) 2007 Nicolas Joly
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.21.4.1 2021/05/16 05:16:21 thorpej Exp $");
 
 #include 
 #include 
@@ -185,31 +185,45 @@ spdmem_i2c_match(device_t parent, cfdata
 	struct spdmem_i2c_softc sc;
 	int match_result;
 
-	if (iic_use_direct_match(ia, match, compat_data, _result))
-		return match_result;
-
 	/*
-	 * XXXJRT
-	 * Should do this with "compatible" strings.  There are also
-	 * other problems with this "match" routine.  Specifically, if
-	 * we are doing direct-config, we know the device is already
-	 * there aren't do need to probe.  I'll leave the logic for
-	 * now and let someone who knows better clean it later.
+	 * SPD stands for "Serial Presence Detect".  This implies that
+	 * if we're using direct configuration that we should treat
+	 * that as a *hint*... it's entirely possible that a device
+	 * tree lists locations where SPD memory can be found, not
+	 * necessarily where memory is known to be present.
+	 *
+	 * Accordingly, if we get a direct configuration match based
+	 * on compatible data or device name, we still check to see
+	 * if the device is there.
 	 */
 
+	if (iic_use_direct_match(ia, match, compat_data, _result)) {
+		if (match_result != 0) {
+			goto do_probe;
+		}
+		return 0;
+	}
+
 	if (ia->ia_name) {
 		/* add other names as we find more firmware variations */
-		if (strcmp(ia->ia_name, "dimm-spd") &&
-		strcmp(ia->ia_name, "dimm"))
-			return 0;
+		if (strcmp(ia->ia_name, "dimm-spd") == 0 ||
+		strcmp(ia->ia_name, "dimm") == 0) {
+			match_result = I2C_MATCH_DIRECT_SPECIFIC;
+			goto do_probe;
+		}
+		return 0;
 	}
 
-	/* only do this lame test when not using direct config */
-	if (ia->ia_name == NULL) {
-		if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
-			return 0;
+	/* As a last resort, filter out invalid addresses. */
+	if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) == SPDMEM_I2C_ADDR) {
+		match_result = I2C_MATCH_ADDRESS_AND_PROBE;
+		goto do_probe;
 	}
+	
+	/* Not a candidate address. */
+	return 0;
 
+ do_probe:
 	sc.sc_tag = ia->ia_tag;
 	sc.sc_addr = ia->ia_addr;
 	sc.sc_page0 = SPDCTL_SPA0;
@@ -221,8 +235,7 @@ spdmem_i2c_match(device_t parent, cfdata
 		return 0;
 
 	if (spdmem_common_probe(_base)) {
-		return ia->ia_name ? I2C_MATCH_DIRECT_SPECIFIC
-   : I2C_MATCH_ADDRESS_AND_PROBE;
+		return match_result;
 	}
 	return 0;
 }



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-15 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 05:16:21 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: spdmem_i2c.c

Log Message:
SPD stands for "Serial Presence Detect".  This implies that
if we're using direct configuration that we should treat
that as a *hint*... it's entirely possible that a device
tree lists locations where SPD memory can be found, not
necessarily where memory is known to be present.

Accordingly, if we get a direct configuration match based
on compatible data or device name, we still check to see
if the device is there.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.21.4.1 src/sys/dev/i2c/spdmem_i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-15 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 04:40:08 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
Rather than allocating 8KB (!!) of space per i2c bus for a sparsely
populated array of child devices, use a sorted list instead, optimized
a bit for the common usage pattern.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.2 -r1.78.2.3 src/sys/dev/i2c/i2c.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/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78.2.2 src/sys/dev/i2c/i2c.c:1.78.2.3
--- src/sys/dev/i2c/i2c.c:1.78.2.2	Sat May  8 11:34:38 2021
+++ src/sys/dev/i2c/i2c.c	Sun May 16 04:40:08 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.78.2.2 2021/05/08 11:34:38 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.3 2021/05/16 04:40:08 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.2 2021/05/08 11:34:38 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.3 2021/05/16 04:40:08 thorpej Exp $");
 
 #include 
 #include 
@@ -95,10 +95,20 @@ __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78
 #define I2C_MAX_ADDR	0x3ff	/* 10-bit address, max */
 #endif
 
+struct i2c_device_link {
+	TAILQ_ENTRY(i2c_device_link) l_list;
+	device_t		l_device;
+	i2c_addr_t		l_addr;
+};
+
+TAILQ_HEAD(i2c_devlist_head, i2c_device_link);
+
 struct iic_softc {
 	device_t sc_dev;
 	i2c_tag_t sc_tag;
-	device_t sc_devices[I2C_MAX_ADDR + 1];
+
+	kmutex_t sc_devlist_lock;
+	struct i2c_devlist_head sc_devlist;
 };
 
 static dev_type_open(iic_open);
@@ -129,6 +139,179 @@ const struct cdevsw iic_cdevsw = {
 
 static void	iic_smbus_intr_thread(void *);
 
+static struct i2c_device_link *
+iic_devslot_lookup(struct iic_softc *sc, i2c_addr_t addr)
+{
+	struct i2c_device_link *link;
+
+	KASSERT(mutex_owned(>sc_devlist_lock));
+
+	/*
+	 * A common pattern is "reserve then insert or delete", and
+	 * this is often done in increasing address order.  So check
+	 * if the last entry is the one we're looking for before we
+	 * search the list from the front.
+	 */
+	link = TAILQ_LAST(>sc_devlist, i2c_devlist_head);
+	if (link == NULL) {
+		/* List is empty. */
+		return NULL;
+	}
+	if (link->l_addr == addr) {
+		return link;
+	}
+
+	TAILQ_FOREACH(link, >sc_devlist, l_list) {
+		/*
+		 * The list is sorted, so if the current list element
+		 * has an address larger than the one we're looking
+		 * for, then it's not in the list.
+		 */
+		if (link->l_addr > addr) {
+			break;
+		}
+		if (link->l_addr == addr) {
+			return link;
+		}
+	}
+	return NULL;
+}
+
+static bool
+iic_devslot_reserve(struct iic_softc *sc, i2c_addr_t addr)
+{
+	struct i2c_device_link *link, *new_link;
+
+	new_link = kmem_zalloc(sizeof(*new_link), KM_SLEEP);
+	new_link->l_addr = addr;
+
+	mutex_enter(>sc_devlist_lock);
+
+	/* Optimize for reserving in increasing i2c address order. */
+	link = TAILQ_LAST(>sc_devlist, i2c_devlist_head);
+	if (link == NULL || link->l_addr < new_link->l_addr) {
+		TAILQ_INSERT_TAIL(>sc_devlist, new_link, l_list);
+		new_link = NULL;
+		goto done;
+	}
+	KASSERT(!TAILQ_EMPTY(>sc_devlist));
+
+	/* Sort the new entry into the list. */
+	TAILQ_FOREACH(link, >sc_devlist, l_list) {
+		if (link->l_addr < new_link->l_addr) {
+			continue;
+		}
+		if (link->l_addr == new_link->l_addr) {
+			/* Address is already reserved / in-use. */
+			goto done;
+		}
+		/*
+		 * If we get here, we know we should be inserted
+		 * before this element, because we checked to see
+		 * if we should be the last entry before entering
+		 * the loop.
+		 */
+		KASSERT(link->l_addr > new_link->l_addr);
+		TAILQ_INSERT_BEFORE(link, new_link, l_list);
+		new_link = NULL;
+		break;
+	}
+	/*
+	 * Because we checked for an empty list early, if we got
+	 * here it means we inserted before "link".
+	 */
+	KASSERT(link != NULL);
+	KASSERT(TAILQ_NEXT(new_link, l_list) == link);
+
+ done:
+	mutex_exit(>sc_devlist_lock);
+
+	if (new_link != NULL) {
+		kmem_free(new_link, sizeof(*new_link));
+		return false;
+	}
+	return true;
+}
+
+static bool
+iic_devslot_insert(struct iic_softc *sc, device_t dev, i2c_addr_t addr)
+{
+	struct i2c_device_link *link;
+	bool rv = false;
+
+	mutex_enter(>sc_devlist_lock);
+
+	link = iic_devslot_lookup(sc, addr);
+	if (link != NULL) {
+		if (link->l_device == NULL) {
+			link->l_device = dev;
+			rv = true;
+		}
+	}
+
+	mutex_exit(>sc_devlist_lock);
+
+	return rv;
+}
+
+static bool
+iic_devslot_remove(struct iic_softc *sc, device_t dev, i2c_addr_t addr)
+{
+	struct i2c_device_link *link;
+	bool rv = false;
+
+	mutex_enter(>sc_devlist_lock);
+
+	link = iic_devslot_lookup(sc, addr);
+	if (link != NULL) {
+		if (link->l_device == dev) {
+			TAILQ_REMOVE(>sc_devlist, link, l_list);
+			rv = true;
+		} else {
+			link = NULL;
+		}
+	}
+
+	mutex_exit(>sc_devlist_lock);
+
+	if (link != NULL) {
+		

CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-15 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May 16 04:40:08 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
Rather than allocating 8KB (!!) of space per i2c bus for a sparsely
populated array of child devices, use a sorted list instead, optimized
a bit for the common usage pattern.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.2 -r1.78.2.3 src/sys/dev/i2c/i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-13 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Fri May 14 03:57:52 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: pcai2cmux.c

Log Message:
No need to device_set_handle(); it's done for us by the i2c enumeration
code.


To generate a diff of this commit:
cvs rdiff -u -r1.8.4.2 -r1.8.4.3 src/sys/dev/i2c/pcai2cmux.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/dev/i2c/pcai2cmux.c
diff -u src/sys/dev/i2c/pcai2cmux.c:1.8.4.2 src/sys/dev/i2c/pcai2cmux.c:1.8.4.3
--- src/sys/dev/i2c/pcai2cmux.c:1.8.4.2	Sun May  9 23:26:53 2021
+++ src/sys/dev/i2c/pcai2cmux.c	Fri May 14 03:57:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcai2cmux.c,v 1.8.4.2 2021/05/09 23:26:53 thorpej Exp $	*/
+/*	$NetBSD: pcai2cmux.c,v 1.8.4.3 2021/05/14 03:57:51 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.8.4.2 2021/05/09 23:26:53 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.8.4.3 2021/05/14 03:57:51 thorpej Exp $");
 
 /*
  * Driver for NXP PCA954x / PCA984x I2C switches and multiplexers.
@@ -334,11 +334,8 @@ pcaiicmux_attach(device_t parent, device
 {
 	struct pcaiicmux_softc * const sc = device_private(self);
 	struct i2c_attach_args * const ia = aux;
-	devhandle_t devhandle = ia->ia_devhandle;
 	int error;
 
-	device_set_handle(self, devhandle);
-
 	sc->sc_iicmux.sc_dev = self;
 	sc->sc_iicmux.sc_config = _config;
 	sc->sc_iicmux.sc_i2c_parent = ia->ia_tag;
@@ -352,6 +349,7 @@ pcaiicmux_attach(device_t parent, device
 	sc->sc_type->enable_bit ? "mux" : "switch");
 
 #if defined(I2CMUX_USE_FDT)
+	devhandle_t devhandle = device_handle(self);
 	if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
 		const int phandle = devhandle_to_of(devhandle);
 		if (of_hasprop(phandle, "i2c-mux-idle-disconnect")) {



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-13 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Fri May 14 03:57:52 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: pcai2cmux.c

Log Message:
No need to device_set_handle(); it's done for us by the i2c enumeration
code.


To generate a diff of this commit:
cvs rdiff -u -r1.8.4.2 -r1.8.4.3 src/sys/dev/i2c/pcai2cmux.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-09 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May  9 23:26:53 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: pcai2cmux.c

Log Message:
Don't include fdtvar.h directly here.


To generate a diff of this commit:
cvs rdiff -u -r1.8.4.1 -r1.8.4.2 src/sys/dev/i2c/pcai2cmux.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-09 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May  9 23:26:53 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: pcai2cmux.c

Log Message:
Don't include fdtvar.h directly here.


To generate a diff of this commit:
cvs rdiff -u -r1.8.4.1 -r1.8.4.2 src/sys/dev/i2c/pcai2cmux.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/dev/i2c/pcai2cmux.c
diff -u src/sys/dev/i2c/pcai2cmux.c:1.8.4.1 src/sys/dev/i2c/pcai2cmux.c:1.8.4.2
--- src/sys/dev/i2c/pcai2cmux.c:1.8.4.1	Sat May  8 02:44:22 2021
+++ src/sys/dev/i2c/pcai2cmux.c	Sun May  9 23:26:53 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcai2cmux.c,v 1.8.4.1 2021/05/08 02:44:22 thorpej Exp $	*/
+/*	$NetBSD: pcai2cmux.c,v 1.8.4.2 2021/05/09 23:26:53 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.8.4.1 2021/05/08 02:44:22 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.8.4.2 2021/05/09 23:26:53 thorpej Exp $");
 
 /*
  * Driver for NXP PCA954x / PCA984x I2C switches and multiplexers.
@@ -56,7 +56,6 @@ __KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,
 #include 
 #include 
 
-#include 
 #include 
 
 /* There are a maximum of 8 busses supported. */



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-09 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May  9 23:19:10 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2cmuxvar.h

Log Message:
Revert most of previous -- some consumers of this file still have
ACPI-specific requirements.


To generate a diff of this commit:
cvs rdiff -u -r1.3.4.2 -r1.3.4.3 src/sys/dev/i2c/i2cmuxvar.h

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-09 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun May  9 23:19:10 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2cmuxvar.h

Log Message:
Revert most of previous -- some consumers of this file still have
ACPI-specific requirements.


To generate a diff of this commit:
cvs rdiff -u -r1.3.4.2 -r1.3.4.3 src/sys/dev/i2c/i2cmuxvar.h

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

Modified files:

Index: src/sys/dev/i2c/i2cmuxvar.h
diff -u src/sys/dev/i2c/i2cmuxvar.h:1.3.4.2 src/sys/dev/i2c/i2cmuxvar.h:1.3.4.3
--- src/sys/dev/i2c/i2cmuxvar.h:1.3.4.2	Sat May  8 15:10:44 2021
+++ src/sys/dev/i2c/i2cmuxvar.h	Sun May  9 23:19:10 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cmuxvar.h,v 1.3.4.2 2021/05/08 15:10:44 thorpej Exp $	*/
+/*	$NetBSD: i2cmuxvar.h,v 1.3.4.3 2021/05/09 23:19:10 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -36,6 +36,23 @@
 
 /* XXX This is not ideal, but... XXX */
 
+#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
+
+#ifdef _KERNEL_OPT
+#include "acpica.h"
+
+#if NACPICA > 0
+#define	I2CMUX_USE_ACPI
+#endif
+
+#else /* ! _KERNEL_OPT */
+
+#define	I2CMUX_USE_ACPI
+
+#endif /* _KERNEL_OPT */
+
+#endif /* __i386__ || __amd64__ || __aarch64__ */
+
 #if defined(__arm__) || defined(__aarch64__)
 
 #ifdef _KERNEL_OPT
@@ -55,6 +72,10 @@
 
 /* XXX ^^^ XXX */
 
+#if defined(I2CMUX_USE_ACPI)
+#include 
+#endif
+
 #if defined(I2CMUX_USE_FDT)
 #include 
 #endif



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 16:56:10 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: adadc.c fcu.c

Log Message:
ia->ia_cookie -> devhandle_to_of(device_handle(self))


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.10.4.1 src/sys/dev/i2c/adadc.c
cvs rdiff -u -r1.12 -r1.12.4.1 src/sys/dev/i2c/fcu.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/dev/i2c/adadc.c
diff -u src/sys/dev/i2c/adadc.c:1.10 src/sys/dev/i2c/adadc.c:1.10.4.1
--- src/sys/dev/i2c/adadc.c:1.10	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/adadc.c	Sat May  8 16:56:10 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: adadc.c,v 1.10 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: adadc.c,v 1.10.4.1 2021/05/08 16:56:10 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2018 Michael Lorenz
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: adadc.c,v 1.10 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: adadc.c,v 1.10.4.1 2021/05/08 16:56:10 thorpej Exp $");
 
 #include 
 #include 
@@ -128,6 +128,7 @@ adadc_attach(device_t parent, device_t s
 	struct i2c_attach_args *ia = aux;
 	envsys_data_t *s;
 	int error, ch;
+	int phandle;
 	uint32_t eeprom[40];
 	char loc[256];
 	int which_cpu;
@@ -151,8 +152,8 @@ adadc_attach(device_t parent, device_t s
 	 * should probably just expose the temperature and four ENVSYS_INTEGERs
 	 */
 	which_cpu = 0;
-	ch = OF_child(ia->ia_cookie);
-	while (ch != 0) {
+	phandle = devhandle_to_of(device_handle(self));
+	for (ch = OF_child(phandle); ch != 0; ch = OF_peer(ch)) {
 		if (OF_getprop(ch, "location", loc, 32) > 0) {
 			int reg = 0;
 			OF_getprop(ch, "reg", , sizeof(reg));
@@ -185,7 +186,6 @@ adadc_attach(device_t parent, device_t s
 			sysmon_envsys_sensor_attach(sc->sc_sme, s);
 			sc->sc_nsensors++;
 		}
-		ch = OF_peer(ch);
 	}
 	aprint_debug_dev(self, "monitoring CPU %d\n", which_cpu);
 	error = get_cpuid(which_cpu, (uint8_t *)eeprom);

Index: src/sys/dev/i2c/fcu.c
diff -u src/sys/dev/i2c/fcu.c:1.12 src/sys/dev/i2c/fcu.c:1.12.4.1
--- src/sys/dev/i2c/fcu.c:1.12	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/fcu.c	Sat May  8 16:56:10 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: fcu.c,v 1.12 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: fcu.c,v 1.12.4.1 2021/05/08 16:56:10 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2018 Michael Lorenz
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.12 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.12.4.1 2021/05/08 16:56:10 thorpej Exp $");
 
 #include 
 #include 
@@ -141,6 +141,7 @@ fcu_attach(device_t parent, device_t sel
 	struct fcu_softc *sc = device_private(self);
 	struct i2c_attach_args *ia = aux;
 	int have_eeprom1 = 1;
+	int phandle;
 
 	sc->sc_dev = self;
 	sc->sc_i2c = ia->ia_tag;
@@ -185,11 +186,11 @@ fcu_attach(device_t parent, device_t sel
 	sc->sc_nfans = 0;
 
 	/* round up sensors */
+	phandle = devhandle_to_of(device_handle(self));
 	int ch;
 
 	sc->sc_nsensors = 0;
-	ch = OF_child(ia->ia_cookie);
-	while (ch != 0) {
+	for (ch = OF_child(phandle); ch != 0; ch = OF_peer(ch)) {
 		char type[32], descr[32];
 		uint32_t reg;
 
@@ -198,7 +199,7 @@ fcu_attach(device_t parent, device_t sel
 		s->state = ENVSYS_SINVALID;
 
 		if (OF_getprop(ch, "device_type", type, 32) <= 0)
-			goto next;
+			continue;
 
 		if (strcmp(type, "fan-rpm-control") == 0) {
 			s->units = ENVSYS_SFANRPM;
@@ -214,15 +215,15 @@ fcu_attach(device_t parent, device_t sel
 			s->units = ENVSYS_INDICATOR;
 		} else {
 			/* ignore other types for now */
-			goto next;
+			continue;
 		}
 
 		if (OF_getprop(ch, "reg", , sizeof(reg)) <= 0)
-			goto next;
+			continue;
 		s->private = reg;
 
 		if (OF_getprop(ch, "location", descr, 32) <= 0)
-			goto next;
+			continue;
 		strcpy(s->desc, descr);
 
 		if (s->units == ENVSYS_SFANRPM) {
@@ -304,8 +305,6 @@ fcu_attach(device_t parent, device_t sel
 		}
 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
 		sc->sc_nsensors++;
-next:
-		ch = OF_peer(ch);
 	}		
 	sysmon_envsys_register(sc->sc_sme);
 



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 16:56:10 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: adadc.c fcu.c

Log Message:
ia->ia_cookie -> devhandle_to_of(device_handle(self))


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.10.4.1 src/sys/dev/i2c/adadc.c
cvs rdiff -u -r1.12 -r1.12.4.1 src/sys/dev/i2c/fcu.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 16:46:43 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: ac100.c anxedp.c as3722.c
axp20x.c axppmic.c cwfg.c es8316ac.c fan53555.c max77620.c
pca9685.c pcf8563.c rkpmic.c sy8106a.c tcagpio.c tcakp.c tda19988.c
tps65217pmic.c twl4030.c

Log Message:
ia->ia_cookie -> devhandle_to_of(device_handle(self))


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.7.4.1 src/sys/dev/i2c/ac100.c \
src/sys/dev/i2c/tda19988.c
cvs rdiff -u -r1.6 -r1.6.4.1 src/sys/dev/i2c/anxedp.c \
src/sys/dev/i2c/pca9685.c src/sys/dev/i2c/twl4030.c
cvs rdiff -u -r1.23 -r1.23.2.1 src/sys/dev/i2c/as3722.c
cvs rdiff -u -r1.20 -r1.20.2.1 src/sys/dev/i2c/axp20x.c
cvs rdiff -u -r1.34 -r1.34.2.1 src/sys/dev/i2c/axppmic.c
cvs rdiff -u -r1.4 -r1.4.4.1 src/sys/dev/i2c/cwfg.c
cvs rdiff -u -r1.5 -r1.5.4.1 src/sys/dev/i2c/es8316ac.c
cvs rdiff -u -r1.9 -r1.9.4.1 src/sys/dev/i2c/fan53555.c
cvs rdiff -u -r1.11 -r1.11.4.1 src/sys/dev/i2c/max77620.c
cvs rdiff -u -r1.15 -r1.15.4.1 src/sys/dev/i2c/pcf8563.c
cvs rdiff -u -r1.13 -r1.13.2.1 src/sys/dev/i2c/rkpmic.c
cvs rdiff -u -r1.8 -r1.8.4.1 src/sys/dev/i2c/sy8106a.c \
src/sys/dev/i2c/tcagpio.c
cvs rdiff -u -r1.16 -r1.16.2.1 src/sys/dev/i2c/tcakp.c
cvs rdiff -u -r1.19 -r1.19.2.1 src/sys/dev/i2c/tps65217pmic.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 16:46:43 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: ac100.c anxedp.c as3722.c
axp20x.c axppmic.c cwfg.c es8316ac.c fan53555.c max77620.c
pca9685.c pcf8563.c rkpmic.c sy8106a.c tcagpio.c tcakp.c tda19988.c
tps65217pmic.c twl4030.c

Log Message:
ia->ia_cookie -> devhandle_to_of(device_handle(self))


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.7.4.1 src/sys/dev/i2c/ac100.c \
src/sys/dev/i2c/tda19988.c
cvs rdiff -u -r1.6 -r1.6.4.1 src/sys/dev/i2c/anxedp.c \
src/sys/dev/i2c/pca9685.c src/sys/dev/i2c/twl4030.c
cvs rdiff -u -r1.23 -r1.23.2.1 src/sys/dev/i2c/as3722.c
cvs rdiff -u -r1.20 -r1.20.2.1 src/sys/dev/i2c/axp20x.c
cvs rdiff -u -r1.34 -r1.34.2.1 src/sys/dev/i2c/axppmic.c
cvs rdiff -u -r1.4 -r1.4.4.1 src/sys/dev/i2c/cwfg.c
cvs rdiff -u -r1.5 -r1.5.4.1 src/sys/dev/i2c/es8316ac.c
cvs rdiff -u -r1.9 -r1.9.4.1 src/sys/dev/i2c/fan53555.c
cvs rdiff -u -r1.11 -r1.11.4.1 src/sys/dev/i2c/max77620.c
cvs rdiff -u -r1.15 -r1.15.4.1 src/sys/dev/i2c/pcf8563.c
cvs rdiff -u -r1.13 -r1.13.2.1 src/sys/dev/i2c/rkpmic.c
cvs rdiff -u -r1.8 -r1.8.4.1 src/sys/dev/i2c/sy8106a.c \
src/sys/dev/i2c/tcagpio.c
cvs rdiff -u -r1.16 -r1.16.2.1 src/sys/dev/i2c/tcakp.c
cvs rdiff -u -r1.19 -r1.19.2.1 src/sys/dev/i2c/tps65217pmic.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/dev/i2c/ac100.c
diff -u src/sys/dev/i2c/ac100.c:1.7 src/sys/dev/i2c/ac100.c:1.7.4.1
--- src/sys/dev/i2c/ac100.c:1.7	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/ac100.c	Sat May  8 16:46:43 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: ac100.c,v 1.7 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: ac100.c,v 1.7.4.1 2021/05/08 16:46:43 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014 Jared D. McNeill 
@@ -29,7 +29,7 @@
 #include "opt_fdt.h"
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ac100.c,v 1.7 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ac100.c,v 1.7.4.1 2021/05/08 16:46:43 thorpej Exp $");
 
 #include 
 #include 
@@ -153,7 +153,7 @@ ac100_attach(device_t parent, device_t s
 	sc->sc_todr.cookie = sc;
 
 #ifdef FDT
-	const int phandle = ia->ia_cookie;
+	const int phandle = devhandle_to_of(device_handle(self));
 	const int rtc_phandle = of_find_firstchild_byname(phandle, "rtc");
 	if (rtc_phandle > 0)
 		fdtbus_todr_attach(self, rtc_phandle, >sc_todr);
Index: src/sys/dev/i2c/tda19988.c
diff -u src/sys/dev/i2c/tda19988.c:1.7 src/sys/dev/i2c/tda19988.c:1.7.4.1
--- src/sys/dev/i2c/tda19988.c:1.7	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/tda19988.c	Sat May  8 16:46:43 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: tda19988.c,v 1.7 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: tda19988.c,v 1.7.4.1 2021/05/08 16:46:43 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2015 Oleksandr Tymoshenko 
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: tda19988.c,v 1.7 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tda19988.c,v 1.7.4.1 2021/05/08 16:46:43 thorpej Exp $");
 
 /*
 * NXP TDA19988 HDMI encoder 
@@ -904,7 +904,7 @@ tda19988_attach(device_t parent, device_
 {
 	struct tda19988_softc *sc = device_private(self);
 	struct i2c_attach_args * const ia = aux;
-	const int phandle = ia->ia_cookie;
+	const int phandle = devhandle_to_of(device_handle(self));
 
 	sc->sc_dev = self;
 	sc->sc_phandle = phandle;

Index: src/sys/dev/i2c/anxedp.c
diff -u src/sys/dev/i2c/anxedp.c:1.6 src/sys/dev/i2c/anxedp.c:1.6.4.1
--- src/sys/dev/i2c/anxedp.c:1.6	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/anxedp.c	Sat May  8 16:46:43 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: anxedp.c,v 1.6 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: anxedp.c,v 1.6.4.1 2021/05/08 16:46:43 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2019 Jared D. McNeill 
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: anxedp.c,v 1.6 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: anxedp.c,v 1.6.4.1 2021/05/08 16:46:43 thorpej Exp $");
 
 #include 
 #include 
@@ -434,7 +434,7 @@ anxedp_attach(device_t parent, device_t 
 	sc->sc_dev = self;
 	sc->sc_i2c = ia->ia_tag;
 	sc->sc_addr = ia->ia_addr;
-	sc->sc_phandle = ia->ia_cookie;
+	sc->sc_phandle = devhandle_to_of(device_handle(self));
 
 	aprint_naive("\n");
 	aprint_normal(": eDP TX\n");
Index: src/sys/dev/i2c/pca9685.c
diff -u src/sys/dev/i2c/pca9685.c:1.6 src/sys/dev/i2c/pca9685.c:1.6.4.1
--- src/sys/dev/i2c/pca9685.c:1.6	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/pca9685.c	Sat May  8 16:46:43 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: pca9685.c,v 1.6 2021/01/27 02:29:48 thorpej Exp $ */
+/* $NetBSD: pca9685.c,v 1.6.4.1 2021/05/08 16:46:43 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2018, 2019 Jason R. Thorpe
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pca9685.c,v 1.6 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pca9685.c,v 1.6.4.1 

CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 15:44:12 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: ihidev.c ihidev.h

Log Message:
Rather than using the "cookie" from the i2c_attach_args, use the
device handle that's already associated with our device_t.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.19.2.1 src/sys/dev/i2c/ihidev.c
cvs rdiff -u -r1.4 -r1.4.10.1 src/sys/dev/i2c/ihidev.h

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 15:44:12 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: ihidev.c ihidev.h

Log Message:
Rather than using the "cookie" from the i2c_attach_args, use the
device handle that's already associated with our device_t.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.19.2.1 src/sys/dev/i2c/ihidev.c
cvs rdiff -u -r1.4 -r1.4.10.1 src/sys/dev/i2c/ihidev.h

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

Modified files:

Index: src/sys/dev/i2c/ihidev.c
diff -u src/sys/dev/i2c/ihidev.c:1.19 src/sys/dev/i2c/ihidev.c:1.19.2.1
--- src/sys/dev/i2c/ihidev.c:1.19	Sat Apr 24 23:36:54 2021
+++ src/sys/dev/i2c/ihidev.c	Sat May  8 15:44:12 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: ihidev.c,v 1.19 2021/04/24 23:36:54 thorpej Exp $ */
+/* $NetBSD: ihidev.c,v 1.19.2.1 2021/05/08 15:44:12 thorpej Exp $ */
 /* $OpenBSD ihidev.c,v 1.13 2017/04/08 02:57:23 deraadt Exp $ */
 
 /*-
@@ -54,7 +54,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.19 2021/04/24 23:36:54 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.19.2.1 2021/05/08 15:44:12 thorpej Exp $");
 
 #include 
 #include 
@@ -162,8 +162,7 @@ ihidev_attach(device_t parent, device_t 
 	sc->sc_addr = ia->ia_addr;
 	mutex_init(>sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
 
-	sc->sc_phandle = ia->ia_cookie;
-	if (ia->ia_cookietype != I2C_COOKIE_ACPI) {
+	if (devhandle_type(device_handle(self)) != DEVHANDLE_TYPE_ACPI) {
 		aprint_error(": unsupported device tree type\n");
 		return;
 	}
@@ -651,7 +650,7 @@ static bool
 ihiddev_intr_init(struct ihidev_softc *sc)
 {
 #if NACPICA > 0
-	ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
+	ACPI_HANDLE hdl = devhandle_to_acpi(device_handle(sc->sc_dev));
 	struct acpi_resources res;
 	ACPI_STATUS rv;
 	char buf[100];
@@ -675,7 +674,7 @@ ihiddev_intr_init(struct ihidev_softc *s
 
 	acpi_resource_cleanup();
 
-	sc->sc_ih = acpi_intr_establish(sc->sc_dev, sc->sc_phandle, IPL_TTY,
+	sc->sc_ih = acpi_intr_establish(sc->sc_dev, (uint64_t)hdl, IPL_TTY,
 	false, ihidev_intr, sc, device_xname(sc->sc_dev));
 	if (sc->sc_ih == NULL) {
 		aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
@@ -977,7 +976,7 @@ ihidev_set_report(struct device *dev, in
 static bool
 ihidev_acpi_get_info(struct ihidev_softc *sc)
 {   
-	ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
+	ACPI_HANDLE hdl = devhandle_to_acpi(device_handle(sc->sc_dev));
 	ACPI_STATUS status;
 	ACPI_INTEGER val;
 

Index: src/sys/dev/i2c/ihidev.h
diff -u src/sys/dev/i2c/ihidev.h:1.4 src/sys/dev/i2c/ihidev.h:1.4.10.1
--- src/sys/dev/i2c/ihidev.h:1.4	Thu Jan  9 04:04:01 2020
+++ src/sys/dev/i2c/ihidev.h	Sat May  8 15:44:12 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: ihidev.h,v 1.4 2020/01/09 04:04:01 thorpej Exp $ */
+/* $NetBSD: ihidev.h,v 1.4.10.1 2021/05/08 15:44:12 thorpej Exp $ */
 /* $OpenBSD ihidev.h,v 1.4 2016/01/31 18:24:35 jcs Exp $ */
 
 /*-
@@ -97,7 +97,6 @@ struct ihidev_softc {
 	device_t	sc_dev;
 	i2c_tag_t	sc_tag;
 	i2c_addr_t	sc_addr;
-	uint64_t	sc_phandle;
 
 	void *		sc_ih;
 	void *		sc_sih;



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 15:10:44 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2cmux.c i2cmuxvar.h

Log Message:
Use device_enumerate_children() to eliminate all of the ACPI-specific
code and most of the FDT-specific code from the iicmux driver.


To generate a diff of this commit:
cvs rdiff -u -r1.5.2.2 -r1.5.2.3 src/sys/dev/i2c/i2cmux.c
cvs rdiff -u -r1.3.4.1 -r1.3.4.2 src/sys/dev/i2c/i2cmuxvar.h

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

Modified files:

Index: src/sys/dev/i2c/i2cmux.c
diff -u src/sys/dev/i2c/i2cmux.c:1.5.2.2 src/sys/dev/i2c/i2cmux.c:1.5.2.3
--- src/sys/dev/i2c/i2cmux.c:1.5.2.2	Sat May  8 14:23:15 2021
+++ src/sys/dev/i2c/i2cmux.c	Sat May  8 15:10:44 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 thorpej Exp $	*/
+/*	$NetBSD: i2cmux.c,v 1.5.2.3 2021/05/08 15:10:44 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.3 2021/05/08 15:10:44 thorpej Exp $");
 
 #include 
 #include 
@@ -157,98 +157,53 @@ iicmux_attach_bus(struct iicmux_softc * 
 	CFARG_EOL);
 }
 
-#if defined(I2CMUX_USE_FDT)
-static int
-iicmux_fdt_count_children(struct iicmux_softc * const sc)
+static bool
+iicmux_count_busses_callback(device_t self, devhandle_t devhandle,
+void *v __unused)
 {
-	int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
-	char name[32];
-	int child, count;
-
- restart:
-	for (child = OF_child(phandle), count = 0; child;
-	 child = OF_peer(child)) {
-		if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
-			continue;
+	struct iicmux_softc *sc = device_private(self);
+
+#if defined(I2CMUX_USE_FDT)
+	if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
+		char name[32];
+
+		if (OF_getprop(devhandle_to_of(devhandle), "name", name,
+			   sizeof(name)) <= 0) {
+			/* Skip this DT node (shouldn't happen). */
+			return true;	/* keep enumerating */
 		}
 		if (strcmp(name, "i2c-mux") == 0) {
-			phandle = child;
-			goto restart;
+			/*
+			 * This DT node is the actual mux node; reset the
+			 * our devhandle and restart enumeration.
+			 */
+			device_set_handle(self, devhandle);
+			sc->sc_nbusses = -1;
+			return false;	/* stop enumerating */
 		}
-		count++;
 	}
+#endif /* I2CMUX_USE_FDT */
 
-	/* phandle may have changed. */
-	sc->sc_i2c_mux_devhandle = devhandle_from_of(phandle);
-	return count;
+	sc->sc_nbusses++;
+	return true;			/* keep enumerating */
 }
 
-static void
-iicmux_attach_fdt(struct iicmux_softc * const sc)
+static bool
+iicmux_attach_busses_callback(device_t self, devhandle_t devhandle, void *v)
 {
-	int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
-
-	sc->sc_nbusses = iicmux_fdt_count_children(sc);
-	if (sc->sc_nbusses == 0) {
-		return;
-	}
+	struct iicmux_softc *sc = device_private(self);
+	int * const idxp = v;
 
-	/* sc_i2c_mux_devhandle may have changed. */
-	phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
+	KASSERT(*idxp < sc->sc_nbusses);
+	iicmux_attach_bus(sc, devhandle, (*idxp)++);
 
-	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
-	KM_SLEEP);
-
-	int child, idx;
-	for (child = OF_child(phandle), idx = 0; child;
-	 child = OF_peer(child), idx++) {
-		KASSERT(idx < sc->sc_nbusses);
-		iicmux_attach_bus(sc, devhandle_from_of(child), idx);
-	}
+	return true;			/* keep enumerating */
 }
-#endif /* I2CMUX_USE_FDT */
-
-#if defined(I2CMUX_USE_ACPI)
-static void
-iicmux_attach_acpi(struct iicmux_softc * const sc)
-{
-	ACPI_HANDLE hdl = devhandle_to_acpi(sc->sc_i2c_mux_devhandle);
-	struct acpi_devnode *devnode, *ad;
-	int idx;
-
-	devnode = acpi_match_node(hdl);
-	KASSERT(devnode != NULL);
-
-	/* Count child busses */
-	sc->sc_nbusses = 0;
-	SIMPLEQ_FOREACH(ad, >ad_child_head, ad_child_list) {
-		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
-		!acpi_device_present(ad->ad_handle)) {
-			continue;
-		}
-		sc->sc_nbusses++;
-	}
-
-	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
-	KM_SLEEP);
-
-	/* Attach child busses */
-	idx = 0;
-	SIMPLEQ_FOREACH(ad, >ad_child_head, ad_child_list) {
-		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
-		!acpi_device_present(ad->ad_handle)) {
-			continue;
-		}
-		iicmux_attach_bus(sc, devhandle_from_acpi(ad->ad_handle), idx);
-		idx++;
-	}
-}
-#endif /* I2CMUX_USE_ACPI */
 
 void
 iicmux_attach(struct iicmux_softc * const sc)
 {
-	devhandle_t devhandle = device_handle(sc->sc_dev);
+	int error, idx;
 
 	/*
 	 * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
@@ -258,14 +213,6 @@ iicmux_attach(struct iicmux_softc * cons
 	KASSERT(sc->sc_i2c_parent != NULL);
 
 	/*
-	 * We start out assuming that the i2c bus nodes are children of
-	 * our own node.  We'll adjust later 

CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 15:10:44 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2cmux.c i2cmuxvar.h

Log Message:
Use device_enumerate_children() to eliminate all of the ACPI-specific
code and most of the FDT-specific code from the iicmux driver.


To generate a diff of this commit:
cvs rdiff -u -r1.5.2.2 -r1.5.2.3 src/sys/dev/i2c/i2cmux.c
cvs rdiff -u -r1.3.4.1 -r1.3.4.2 src/sys/dev/i2c/i2cmuxvar.h

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 14:23:15 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: files.i2c i2c_subr.c i2cmux.c
i2cvar.h

Log Message:
- Add an optional bus number to i2cbus_attach_args, and a corresponding
  optional "bus" locator to the i2cbus interface attribute.
- Add a iicbus_print_multi() routine, which is like iicbus_print(),
  but also prints the bus number.
- Use these new features in the iicmux driver rather than winging it.


To generate a diff of this commit:
cvs rdiff -u -r1.115 -r1.115.4.1 src/sys/dev/i2c/files.i2c
cvs rdiff -u -r1.1 -r1.1.72.1 src/sys/dev/i2c/i2c_subr.c
cvs rdiff -u -r1.5.2.1 -r1.5.2.2 src/sys/dev/i2c/i2cmux.c
cvs rdiff -u -r1.24.2.1 -r1.24.2.2 src/sys/dev/i2c/i2cvar.h

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

Modified files:

Index: src/sys/dev/i2c/files.i2c
diff -u src/sys/dev/i2c/files.i2c:1.115 src/sys/dev/i2c/files.i2c:1.115.4.1
--- src/sys/dev/i2c/files.i2c:1.115	Mon Jan  4 22:09:35 2021
+++ src/sys/dev/i2c/files.i2c	Sat May  8 14:23:15 2021
@@ -1,7 +1,7 @@
-#	$NetBSD: files.i2c,v 1.115 2021/01/04 22:09:35 thorpej Exp $
+#	$NetBSD: files.i2c,v 1.115.4.1 2021/05/08 14:23:15 thorpej Exp $
 
 obsolete defflag	opt_i2cbus.h		I2C_SCAN
-define	i2cbus { }
+define	i2cbus { [bus = -1] }
 define	i2cexec
 
 device	iic { [addr = -1] } : i2c_bitbang

Index: src/sys/dev/i2c/i2c_subr.c
diff -u src/sys/dev/i2c/i2c_subr.c:1.1 src/sys/dev/i2c/i2c_subr.c:1.1.72.1
--- src/sys/dev/i2c/i2c_subr.c:1.1	Mon Oct  3 22:27:23 2011
+++ src/sys/dev/i2c/i2c_subr.c	Sat May  8 14:23:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c_subr.c,v 1.1 2011/10/03 22:27:23 jmcneill Exp $	*/
+/*	$NetBSD: i2c_subr.c,v 1.1.72.1 2021/05/08 14:23:15 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c_subr.c,v 1.1 2011/10/03 22:27:23 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c_subr.c,v 1.1.72.1 2021/05/08 14:23:15 thorpej Exp $");
 
 #include 
 #include 
@@ -46,9 +46,22 @@ __KERNEL_RCSID(0, "$NetBSD: i2c_subr.c,v
 int
 iicbus_print(void *aux, const char *pnp)
 {
+	/* struct i2cbus_attach_args * const iba = aux; */
 
 	if (pnp != NULL)
 		aprint_normal("iic at %s", pnp);
 
 	return UNCONF;
 }
+
+int
+iicbus_print_multi(void *aux, const char *pnp)
+{
+	struct i2cbus_attach_args * const iba = aux;
+
+	if (pnp != NULL)
+		aprint_normal("iic at %s", pnp);
+	aprint_normal(" bus %d", iba->iba_bus);
+
+	return UNCONF;
+}

Index: src/sys/dev/i2c/i2cmux.c
diff -u src/sys/dev/i2c/i2cmux.c:1.5.2.1 src/sys/dev/i2c/i2cmux.c:1.5.2.2
--- src/sys/dev/i2c/i2cmux.c:1.5.2.1	Sat May  8 02:44:22 2021
+++ src/sys/dev/i2c/i2cmux.c	Sat May  8 14:23:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cmux.c,v 1.5.2.1 2021/05/08 02:44:22 thorpej Exp $	*/
+/*	$NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.1 2021/05/08 02:44:22 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 thorpej Exp $");
 
 #include 
 #include 
@@ -40,6 +40,8 @@ __KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1
 #include 
 #include 
 
+#include "locators.h"
+
 /*
  * i2c mux
  *
@@ -110,20 +112,6 @@ iicmux_exec(void * const v, i2c_op_t con
 
 /*/
 
-/* XXX iicbus_print() should be able to do this. */
-static int
-iicmux_print(void * const aux, const char * const pnp)
-{
-	i2c_tag_t const tag = aux;
-	struct iicmux_bus * const bus = tag->ic_cookie;
-	int rv;
-
-	rv = iicbus_print(aux, pnp);
-	aprint_normal(" bus %d", bus->busidx);
-
-	return rv;
-}
-
 static void
 iicmux_attach_bus(struct iicmux_softc * const sc, devhandle_t devhandle,
 int const busidx)
@@ -147,31 +135,26 @@ iicmux_attach_bus(struct iicmux_softc * 
 	bus->controller.ic_release_bus = iicmux_release_bus;
 	bus->controller.ic_exec = iicmux_exec;
 
-	switch (devhandle_type(devhandle)) {
 #if defined(I2CMUX_USE_FDT)
-	case DEVHANDLE_TYPE_OF:
+	if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
 		fdtbus_register_i2c_controller(>controller,
 		devhandle_to_of(devhandle));
-
-		fdtbus_attach_i2cbus(sc->sc_dev, devhandle_to_of(devhandle),
-		>controller, iicmux_print);
-		break;
+	}
 #endif /* I2CMUX_USE_FDT */
 
-	case DEVHANDLE_TYPE_INVALID:
-		aprint_error_dev(sc->sc_dev, "invalid bus device handle\n");
-		return;
-
-	default: {
-		struct i2cbus_attach_args iba = {
-			.iba_tag = >controller,
-		};
-		config_found(sc->sc_dev, , iicmux_print,
-		CFARG_DEVHANDLE, devhandle,
-		CFARG_EOL);
-		break;
-	}
-	}
+	struct i2cbus_attach_args iba = {
+		.iba_tag = >controller,
+		.iba_bus = bus->busidx,
+	};
+
+	int locs[I2CBUSCF_NLOCS];
+	locs[I2CBUSCF_BUS] = bus->busidx;
+
+	config_found(sc->sc_dev, , 

CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 14:23:15 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: files.i2c i2c_subr.c i2cmux.c
i2cvar.h

Log Message:
- Add an optional bus number to i2cbus_attach_args, and a corresponding
  optional "bus" locator to the i2cbus interface attribute.
- Add a iicbus_print_multi() routine, which is like iicbus_print(),
  but also prints the bus number.
- Use these new features in the iicmux driver rather than winging it.


To generate a diff of this commit:
cvs rdiff -u -r1.115 -r1.115.4.1 src/sys/dev/i2c/files.i2c
cvs rdiff -u -r1.1 -r1.1.72.1 src/sys/dev/i2c/i2c_subr.c
cvs rdiff -u -r1.5.2.1 -r1.5.2.2 src/sys/dev/i2c/i2cmux.c
cvs rdiff -u -r1.24.2.1 -r1.24.2.2 src/sys/dev/i2c/i2cvar.h

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 11:34:38 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
iic_print_direct(): In the "not configured" case, parenthetically print
the first element of the compat list, if we got a compat list.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.1 -r1.78.2.2 src/sys/dev/i2c/i2c.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/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78.2.1 src/sys/dev/i2c/i2c.c:1.78.2.2
--- src/sys/dev/i2c/i2c.c:1.78.2.1	Sun Apr 25 21:45:15 2021
+++ src/sys/dev/i2c/i2c.c	Sat May  8 11:34:38 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2c.c,v 1.78.2.1 2021/04/25 21:45:15 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.2 2021/05/08 11:34:38 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.1 2021/04/25 21:45:15 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.2 2021/05/08 11:34:38 thorpej Exp $");
 
 #include 
 #include 
@@ -135,8 +135,11 @@ iic_print_direct(void *aux, const char *
 	struct i2c_attach_args *ia = aux;
 
 	if (pnp != NULL)
-		aprint_normal("%s at %s addr 0x%02x",
+		aprint_normal("%s%s%s%s at %s addr 0x%02x",
 			  ia->ia_name ? ia->ia_name : "(unknown)",
+			  ia->ia_clist ? " (" : "",
+			  ia->ia_clist ? ia->ia_clist : "",
+			  ia->ia_clist ? ")" : "",
 			  pnp, ia->ia_addr);
 	else
 		aprint_normal(" addr 0x%02x", ia->ia_addr);



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-05-08 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sat May  8 11:34:38 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c

Log Message:
iic_print_direct(): In the "not configured" case, parenthetically print
the first element of the compat list, if we got a compat list.


To generate a diff of this commit:
cvs rdiff -u -r1.78.2.1 -r1.78.2.2 src/sys/dev/i2c/i2c.c

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



CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-04-25 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun Apr 25 21:45:16 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c i2cvar.h

Log Message:
Stop using a prop_array of child device data to perform direct
configuration of i2c devices.  Instead, introduce and use a new
device call "i2c-enumerate-devices" that can be implemented by
devhandle providers that have I2C bindings.  This device call
gathers up the data into i2c_attach_args and invoked a callback
(back to the i2c layer) to attach the device.  The I2C controller
device supplies its devhandle (or the appropriate devhandle for
the I2C bus) when attaching the i2c bus instance using CFARG_DEVHANDLE.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.78.2.1 src/sys/dev/i2c/i2c.c
cvs rdiff -u -r1.24 -r1.24.2.1 src/sys/dev/i2c/i2cvar.h

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

Modified files:

Index: src/sys/dev/i2c/i2c.c
diff -u src/sys/dev/i2c/i2c.c:1.78 src/sys/dev/i2c/i2c.c:1.78.2.1
--- src/sys/dev/i2c/i2c.c:1.78	Sat Apr 24 23:36:54 2021
+++ src/sys/dev/i2c/i2c.c	Sun Apr 25 21:45:15 2021
@@ -1,4 +1,33 @@
-/*	$NetBSD: i2c.c,v 1.78 2021/04/24 23:36:54 thorpej Exp $	*/
+/*	$NetBSD: i2c.c,v 1.78.2.1 2021/04/25 21:45:15 thorpej Exp $	*/
+
+/*-
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -40,7 +69,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78 2021/04/24 23:36:54 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.78.2.1 2021/04/25 21:45:15 thorpej Exp $");
 
 #include 
 #include 
@@ -99,8 +128,6 @@ const struct cdevsw iic_cdevsw = {
 };
 
 static void	iic_smbus_intr_thread(void *);
-static void	iic_fill_compat(struct i2c_attach_args*, const char*,
-			size_t, char **);
 
 static int
 iic_print_direct(void *aux, const char *pnp)
@@ -287,8 +314,8 @@ iic_search(device_t parent, cfdata_t cf,
 	ia.ia_tag = sc->sc_tag;
 
 	ia.ia_name = NULL;
-	ia.ia_ncompat = 0;
-	ia.ia_compat = NULL;
+	ia.ia_clist = NULL;
+	ia.ia_clist_size = 0;
 	ia.ia_prop = NULL;
 
 	if (cf->cf_loc[IICCF_ADDR] == IICCF_ADDR_DEFAULT) {
@@ -387,6 +414,34 @@ iic_rescan(device_t self, const char *if
 	return 0;
 }
 
+static bool
+iic_enumerate_devices_callback(device_t self,
+struct i2c_enumerate_devices_args *args)
+{
+	struct iic_softc *sc = device_private(self);
+	int loc[IICCF_NLOCS] = { 0 };
+
+	args->count++;
+
+	loc[IICCF_ADDR] = args->ia->ia_addr;
+
+	if (args->ia->ia_addr > I2C_MAX_ADDR) {
+		aprint_error_dev(self,
+		"WARNING: ignoring bad device address @ 0x%02x\n",
+		args->ia->ia_addr);
+		return true;			/* keep enumerating */
+	}
+	if (sc->sc_devices[args->ia->ia_addr] == NULL) {
+		sc->sc_devices[args->ia->ia_addr] =
+		config_found(self, args->ia, iic_print_direct,
+			/* CFARG_SUBMATCH, config_stdsubmatch, XXX */
+			CFARG_LOCATORS, loc,
+			CFARG_DEVHANDLE, args->ia->ia_devhandle,
+			CFARG_EOL);
+	}
+	return true;/* keep enumerating */
+}
+
 static int
 iic_match(device_t parent, cfdata_t cf, void *aux)
 {
@@ -399,18 +454,15 @@ iic_attach(device_t parent, device_t sel
 {
 	struct iic_softc *sc = device_private(self);
 	struct i2cbus_attach_args *iba = aux;
-	prop_array_t child_devices;
-	prop_dictionary_t props;
-	char *buf;
 	i2c_tag_t ic;
 	int rv;
-	bool no_indirect_config = false;
 
 	aprint_naive("\n");
 	aprint_normal(": I2C bus\n");
 
 	sc->sc_dev = self;
 	sc->sc_tag = iba->iba_tag;
+
 	ic = sc->sc_tag;
 	

CVS commit: [thorpej-i2c-spi-conf] src/sys/dev/i2c

2021-04-25 Thread Jason R Thorpe
Module Name:src
Committed By:   thorpej
Date:   Sun Apr 25 21:45:16 UTC 2021

Modified Files:
src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2c.c i2cvar.h

Log Message:
Stop using a prop_array of child device data to perform direct
configuration of i2c devices.  Instead, introduce and use a new
device call "i2c-enumerate-devices" that can be implemented by
devhandle providers that have I2C bindings.  This device call
gathers up the data into i2c_attach_args and invoked a callback
(back to the i2c layer) to attach the device.  The I2C controller
device supplies its devhandle (or the appropriate devhandle for
the I2C bus) when attaching the i2c bus instance using CFARG_DEVHANDLE.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.78.2.1 src/sys/dev/i2c/i2c.c
cvs rdiff -u -r1.24 -r1.24.2.1 src/sys/dev/i2c/i2cvar.h

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