Module Name:    src
Committed By:   tnn
Date:           Tue Aug  6 18:17:52 UTC 2019

Modified Files:
        src/sys/dev/ofw: ofw_subr.c openfirm.h

Log Message:
ofw: add of_enter_spi_devs helper method

For translating OFW child nodes of SPI controller into "spi-child-devices"
property. In similar spirit to the existing of_enter_i2c_devs method.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/sys/dev/ofw/ofw_subr.c
cvs rdiff -u -r1.37 -r1.38 src/sys/dev/ofw/openfirm.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/ofw/ofw_subr.c
diff -u src/sys/dev/ofw/ofw_subr.c:1.33 src/sys/dev/ofw/ofw_subr.c:1.34
--- src/sys/dev/ofw/ofw_subr.c:1.33	Wed Sep 26 20:03:36 2018
+++ src/sys/dev/ofw/ofw_subr.c	Tue Aug  6 18:17:52 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: ofw_subr.c,v 1.33 2018/09/26 20:03:36 jakllsch Exp $	*/
+/*	$NetBSD: ofw_subr.c,v 1.34 2019/08/06 18:17:52 tnn Exp $	*/
 
 /*
  * Copyright 1998
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.33 2018/09/26 20:03:36 jakllsch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.34 2019/08/06 18:17:52 tnn Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -503,6 +503,64 @@ of_enter_i2c_devs(prop_dictionary_t prop
 	}
 }
 
+void
+of_enter_spi_devs(prop_dictionary_t props, int ofnode, size_t cell_size)
+{
+	int node, len;
+	char name[32];
+	uint64_t reg64;
+	uint32_t reg32;
+	uint32_t slave;
+	u_int32_t maxfreq;
+	prop_array_t array = NULL;
+	prop_dictionary_t dev;
+	int mode;
+
+	for (node = OF_child(ofnode); node; node = OF_peer(node)) {
+		if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
+			continue;
+		len = OF_getproplen(node, "reg");
+		slave = 0;
+		if (cell_size == 8 && len >= sizeof(reg64)) {
+			if (OF_getprop(node, "reg", &reg64, sizeof(reg64))
+			    < sizeof(reg64))
+				continue;
+			slave = be64toh(reg64);
+		} else if (cell_size == 4 && len >= sizeof(reg32)) {
+			if (OF_getprop(node, "reg", &reg32, sizeof(reg32))
+			    < sizeof(reg32))
+				continue;
+			slave = be32toh(reg32);
+		} else {
+			continue;
+		}
+		if (of_getprop_uint32(node, "spi-max-frequency", &maxfreq)) {
+			maxfreq = 0;
+		}
+		mode = ((int)of_hasprop(node, "cpol") << 1) | (int)of_hasprop(node, "cpha");
+
+		if (array == NULL)
+			array = prop_array_create();
+
+		dev = prop_dictionary_create();
+		prop_dictionary_set_cstring(dev, "name", name);
+		prop_dictionary_set_uint32(dev, "slave", slave);
+		prop_dictionary_set_uint32(dev, "mode", mode);
+		if (maxfreq > 0)
+			prop_dictionary_set_uint32(dev, "spi-max-frequency", maxfreq);
+		prop_dictionary_set_uint64(dev, "cookie", node);
+		of_to_dataprop(dev, node, "compatible", "compatible");
+		prop_array_add(array, dev);
+		prop_object_release(dev);
+	}
+
+	if (array != NULL) {
+		prop_dictionary_set(props, "spi-child-devices", array);
+		prop_object_release(array);
+	}
+}
+
+
 /*
  * Returns true if the specified property is present.
  */

Index: src/sys/dev/ofw/openfirm.h
diff -u src/sys/dev/ofw/openfirm.h:1.37 src/sys/dev/ofw/openfirm.h:1.38
--- src/sys/dev/ofw/openfirm.h:1.37	Thu Aug 23 13:24:44 2018
+++ src/sys/dev/ofw/openfirm.h	Tue Aug  6 18:17:52 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: openfirm.h,v 1.37 2018/08/23 13:24:44 jmcneill Exp $	*/
+/*	$NetBSD: openfirm.h,v 1.38 2019/08/06 18:17:52 tnn Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
@@ -126,6 +126,7 @@ int	*of_network_decode_media(int, int *,
 char	*of_get_mode_string(char *, int);
 
 void	of_enter_i2c_devs(prop_dictionary_t, int, size_t, int);
+void	of_enter_spi_devs(prop_dictionary_t, int, size_t);
 
 bool	of_hasprop(int, const char *);
 #define of_getprop_bool	of_hasprop

Reply via email to