Home | History | Annotate | Line # | Download | only in fdt
fdt_port.c revision 1.1
      1  1.1  bouyer /*	$NetBSD: fdt_port.c,v 1.1 2018/04/03 12:40:20 bouyer Exp $	*/
      2  1.1  bouyer 
      3  1.1  bouyer /*-
      4  1.1  bouyer  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1  bouyer  * All rights reserved.
      6  1.1  bouyer  *
      7  1.1  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  bouyer  * by Manuel Bouyer.
      9  1.1  bouyer  *
     10  1.1  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.1  bouyer  * modification, are permitted provided that the following conditions
     12  1.1  bouyer  * are met:
     13  1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.1  bouyer  *
     19  1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  bouyer  */
     31  1.1  bouyer 
     32  1.1  bouyer /*
     33  1.1  bouyer  * ports and endpoints management. from
     34  1.1  bouyer  * linux/Documentation/devicetree/bindings/graph.txt
     35  1.1  bouyer  * Given a device and its node, it enumerates all ports and endpoints for this
     36  1.1  bouyer  * device, and register connections with the remote endpoints.
     37  1.1  bouyer  */
     38  1.1  bouyer 
     39  1.1  bouyer #include <sys/cdefs.h>
     40  1.1  bouyer 
     41  1.1  bouyer __KERNEL_RCSID(1, "$NetBSD: fdt_port.c,v 1.1 2018/04/03 12:40:20 bouyer Exp $");
     42  1.1  bouyer 
     43  1.1  bouyer #include <sys/param.h>
     44  1.1  bouyer #include <sys/systm.h>
     45  1.1  bouyer #include <sys/device.h>
     46  1.1  bouyer #include <sys/bus.h>
     47  1.1  bouyer #include <sys/kmem.h>
     48  1.1  bouyer 
     49  1.1  bouyer #include <dev/fdt/fdtvar.h>
     50  1.1  bouyer #include <dev/fdt/fdt_port.h>
     51  1.1  bouyer 
     52  1.1  bouyer struct fdt_endpoint;
     53  1.1  bouyer 
     54  1.1  bouyer struct fdt_port {
     55  1.1  bouyer 	int	port_id;
     56  1.1  bouyer 	int	port_phandle; /* port's node */
     57  1.1  bouyer 	struct fdt_endpoint *port_ep; /* this port's endpoints */
     58  1.1  bouyer 	int	port_nep; /* number of endpoints for this port */
     59  1.1  bouyer 	struct fdt_device_ports *port_dp; /* this port's device */
     60  1.1  bouyer };
     61  1.1  bouyer 
     62  1.1  bouyer struct fdt_endpoint {
     63  1.1  bouyer 	int		ep_id;
     64  1.1  bouyer 	enum endpoint_type ep_type;
     65  1.1  bouyer 	int		ep_phandle;
     66  1.1  bouyer 	struct fdt_port	*ep_port; /* parent of this endpoint */
     67  1.1  bouyer 	int		ep_rphandle; /* report endpoint */
     68  1.1  bouyer 	struct fdt_endpoint *ep_rep;
     69  1.1  bouyer 	bool		ep_active;
     70  1.1  bouyer 	bool		ep_enabled;
     71  1.1  bouyer };
     72  1.1  bouyer 
     73  1.1  bouyer SLIST_HEAD(, fdt_device_ports) fdt_port_devices =
     74  1.1  bouyer     SLIST_HEAD_INITIALIZER(&fdt_port_devices);
     75  1.1  bouyer 
     76  1.1  bouyer static void fdt_endpoints_register(int, struct fdt_port *, enum endpoint_type);
     77  1.1  bouyer static const char *ep_name(struct fdt_endpoint *, char *, int);
     78  1.1  bouyer 
     79  1.1  bouyer struct fdt_endpoint *
     80  1.1  bouyer fdt_endpoint_get_from_phandle(int rphandle)
     81  1.1  bouyer {
     82  1.1  bouyer 	struct fdt_device_ports *ports;
     83  1.1  bouyer 	int p, e;
     84  1.1  bouyer 
     85  1.1  bouyer 	if (rphandle < 0)
     86  1.1  bouyer 		return NULL;
     87  1.1  bouyer 
     88  1.1  bouyer 	SLIST_FOREACH(ports, &fdt_port_devices, dp_list) {
     89  1.1  bouyer 		for (p = 0; p < ports->dp_nports; p++) {
     90  1.1  bouyer 			struct fdt_port *port = &ports->dp_port[p];
     91  1.1  bouyer 			for (e = 0; e < port->port_nep; e++) {
     92  1.1  bouyer 				struct fdt_endpoint *ep = &port->port_ep[e];
     93  1.1  bouyer 				if (ep->ep_phandle == rphandle)
     94  1.1  bouyer 					return ep;
     95  1.1  bouyer 			}
     96  1.1  bouyer 		}
     97  1.1  bouyer 	}
     98  1.1  bouyer 	return NULL;
     99  1.1  bouyer 
    100  1.1  bouyer }
    101  1.1  bouyer 
    102  1.1  bouyer struct fdt_endpoint *
    103  1.1  bouyer fdt_endpoint_get_from_index(struct fdt_device_ports *device_ports,
    104  1.1  bouyer     int port_index, int ep_index)
    105  1.1  bouyer {
    106  1.1  bouyer 	int p, e;
    107  1.1  bouyer 	for (p = 0; p < device_ports->dp_nports; p++) {
    108  1.1  bouyer 		struct fdt_port *port = &device_ports->dp_port[p];
    109  1.1  bouyer 		if (port->port_id != port_index)
    110  1.1  bouyer 			continue;
    111  1.1  bouyer 		for (e = 0; e < port->port_nep; e++) {
    112  1.1  bouyer 			struct fdt_endpoint *ep = &port->port_ep[e];
    113  1.1  bouyer 			if (ep->ep_id == ep_index) {
    114  1.1  bouyer 				return ep;
    115  1.1  bouyer 			}
    116  1.1  bouyer 		}
    117  1.1  bouyer 	}
    118  1.1  bouyer 	return NULL;
    119  1.1  bouyer }
    120  1.1  bouyer 
    121  1.1  bouyer struct fdt_endpoint *
    122  1.1  bouyer fdt_endpoint_remote(struct fdt_endpoint *ep)
    123  1.1  bouyer {
    124  1.1  bouyer 	return ep->ep_rep;
    125  1.1  bouyer }
    126  1.1  bouyer 
    127  1.1  bouyer int
    128  1.1  bouyer fdt_endpoint_port_index(struct fdt_endpoint *ep)
    129  1.1  bouyer {
    130  1.1  bouyer 	return ep->ep_port->port_id;
    131  1.1  bouyer }
    132  1.1  bouyer 
    133  1.1  bouyer int
    134  1.1  bouyer fdt_endpoint_index(struct fdt_endpoint *ep)
    135  1.1  bouyer {
    136  1.1  bouyer 	return ep->ep_id;
    137  1.1  bouyer }
    138  1.1  bouyer 
    139  1.1  bouyer device_t
    140  1.1  bouyer fdt_endpoint_device(struct fdt_endpoint *ep)
    141  1.1  bouyer {
    142  1.1  bouyer 	return ep->ep_port->port_dp->dp_dev;
    143  1.1  bouyer }
    144  1.1  bouyer 
    145  1.1  bouyer bool
    146  1.1  bouyer fdt_endpoint_is_active(struct fdt_endpoint *ep)
    147  1.1  bouyer {
    148  1.1  bouyer 	return ep->ep_active;
    149  1.1  bouyer }
    150  1.1  bouyer 
    151  1.1  bouyer bool
    152  1.1  bouyer fdt_endpoint_is_enabled(struct fdt_endpoint *ep)
    153  1.1  bouyer {
    154  1.1  bouyer 	return ep->ep_enabled;
    155  1.1  bouyer }
    156  1.1  bouyer 
    157  1.1  bouyer int
    158  1.1  bouyer fdt_endpoint_activate(struct fdt_endpoint *ep, bool activate)
    159  1.1  bouyer {
    160  1.1  bouyer 	struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
    161  1.1  bouyer 	struct fdt_device_ports *rdp;
    162  1.1  bouyer 	int error = 0;
    163  1.1  bouyer 
    164  1.1  bouyer 	if (rep == NULL)
    165  1.1  bouyer 		return ENODEV;
    166  1.1  bouyer 
    167  1.1  bouyer 	KASSERT(ep->ep_active == rep->ep_active);
    168  1.1  bouyer 	KASSERT(ep->ep_enabled == rep->ep_enabled);
    169  1.1  bouyer 	if (!activate && ep->ep_enabled)
    170  1.1  bouyer 		return EBUSY;
    171  1.1  bouyer 
    172  1.1  bouyer 	rdp = rep->ep_port->port_dp;
    173  1.1  bouyer 	if (rdp->dp_ep_activate)
    174  1.1  bouyer 		error = rdp->dp_ep_activate(rdp->dp_dev, rep, activate);
    175  1.1  bouyer 
    176  1.1  bouyer 	if (error == 0)
    177  1.1  bouyer 		rep->ep_active = ep->ep_active = activate;
    178  1.1  bouyer 	return error;
    179  1.1  bouyer }
    180  1.1  bouyer 
    181  1.1  bouyer int
    182  1.1  bouyer fdt_endpoint_enable(struct fdt_endpoint *ep, bool enable)
    183  1.1  bouyer {
    184  1.1  bouyer 	struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
    185  1.1  bouyer 	struct fdt_device_ports *rdp;
    186  1.1  bouyer 	int error = 0;
    187  1.1  bouyer 
    188  1.1  bouyer 	if (rep == NULL)
    189  1.1  bouyer 		return EINVAL;
    190  1.1  bouyer 
    191  1.1  bouyer 	KASSERT(ep->ep_active == rep->ep_active);
    192  1.1  bouyer 	KASSERT(ep->ep_enabled == rep->ep_enabled);
    193  1.1  bouyer 	if (ep->ep_active == false)
    194  1.1  bouyer 		return EINVAL;
    195  1.1  bouyer 
    196  1.1  bouyer 	rdp = rep->ep_port->port_dp;
    197  1.1  bouyer 	if (rdp->dp_ep_enable)
    198  1.1  bouyer 		error = rdp->dp_ep_enable(rdp->dp_dev, rep, enable);
    199  1.1  bouyer 
    200  1.1  bouyer 	if (error == 0)
    201  1.1  bouyer 		rep->ep_enabled = ep->ep_enabled = enable;
    202  1.1  bouyer 	return error;
    203  1.1  bouyer }
    204  1.1  bouyer 
    205  1.1  bouyer void *
    206  1.1  bouyer fdt_endpoint_get_data(struct fdt_endpoint *ep)
    207  1.1  bouyer {
    208  1.1  bouyer 	struct fdt_device_ports *dp = ep->ep_port->port_dp;
    209  1.1  bouyer 
    210  1.1  bouyer 	if (dp->dp_ep_get_data)
    211  1.1  bouyer 		return dp->dp_ep_get_data(dp->dp_dev, ep);
    212  1.1  bouyer 
    213  1.1  bouyer 	return NULL;
    214  1.1  bouyer }
    215  1.1  bouyer 
    216  1.1  bouyer int
    217  1.1  bouyer fdt_ports_register(struct fdt_device_ports *ports, device_t self,
    218  1.1  bouyer     int phandle, enum endpoint_type type)
    219  1.1  bouyer {
    220  1.1  bouyer 	int port_phandle, child;
    221  1.1  bouyer 	int i;
    222  1.1  bouyer 	char buf[20];
    223  1.1  bouyer 	uint64_t id;
    224  1.1  bouyer 
    225  1.1  bouyer 	ports->dp_dev = self;
    226  1.1  bouyer 	SLIST_INSERT_HEAD(&fdt_port_devices, ports, dp_list);
    227  1.1  bouyer 
    228  1.1  bouyer 	/*
    229  1.1  bouyer 	 * walk the childs looking for ports. ports may be grouped under
    230  1.1  bouyer 	 * an optional ports node
    231  1.1  bouyer 	 */
    232  1.1  bouyer 	port_phandle = phandle;
    233  1.1  bouyer again:
    234  1.1  bouyer 	ports->dp_nports = 0;
    235  1.1  bouyer 	for (child = OF_child(port_phandle); child; child = OF_peer(child)) {
    236  1.1  bouyer 		if (OF_getprop(child, "name", buf, sizeof(buf)) <= 0)
    237  1.1  bouyer 			continue;
    238  1.1  bouyer 		if (strcmp(buf, "ports") == 0) {
    239  1.1  bouyer 			port_phandle = child;
    240  1.1  bouyer 			goto again;
    241  1.1  bouyer 		}
    242  1.1  bouyer 		if (strcmp(buf, "port") != 0)
    243  1.1  bouyer 			continue;
    244  1.1  bouyer 		ports->dp_nports++;
    245  1.1  bouyer 	}
    246  1.1  bouyer 	if (ports->dp_nports == 0)
    247  1.1  bouyer 		return 0;
    248  1.1  bouyer 
    249  1.1  bouyer 	ports->dp_port =
    250  1.1  bouyer 	    kmem_zalloc(sizeof(struct fdt_port) * ports->dp_nports, KM_SLEEP);
    251  1.1  bouyer 	KASSERT(ports->dp_port != NULL);
    252  1.1  bouyer 	/* now scan again ports, looking for endpoints */
    253  1.1  bouyer 	for (child = OF_child(port_phandle), i = 0; child;
    254  1.1  bouyer 	    child = OF_peer(child)) {
    255  1.1  bouyer 		if (OF_getprop(child, "name", buf, sizeof(buf)) <= 0)
    256  1.1  bouyer 			continue;
    257  1.1  bouyer 		if (strcmp(buf, "ports") == 0) {
    258  1.1  bouyer 			panic("fdt_ports_register: undetected ports");
    259  1.1  bouyer 		}
    260  1.1  bouyer 		if (strcmp(buf, "port") != 0)
    261  1.1  bouyer 			continue;
    262  1.1  bouyer 		if (fdtbus_get_reg64(child, 0, &id, NULL) != 0) {
    263  1.1  bouyer 			if (ports->dp_nports > 1)
    264  1.1  bouyer 				aprint_error_dev(self,
    265  1.1  bouyer 				    "%s: missing reg property",
    266  1.1  bouyer 				    fdtbus_get_string(child, "name"));
    267  1.1  bouyer 			id = i;
    268  1.1  bouyer 		}
    269  1.1  bouyer 		ports->dp_port[i].port_id = id;
    270  1.1  bouyer 		ports->dp_port[i].port_phandle = child;
    271  1.1  bouyer 		ports->dp_port[i].port_dp = ports;
    272  1.1  bouyer 		fdt_endpoints_register(child, &ports->dp_port[i], type);
    273  1.1  bouyer 		i++;
    274  1.1  bouyer 	}
    275  1.1  bouyer 	KASSERT(i == ports->dp_nports);
    276  1.1  bouyer 	return 0;
    277  1.1  bouyer }
    278  1.1  bouyer 
    279  1.1  bouyer 
    280  1.1  bouyer static void
    281  1.1  bouyer fdt_endpoints_register(int phandle, struct fdt_port *port,
    282  1.1  bouyer     enum endpoint_type type)
    283  1.1  bouyer {
    284  1.1  bouyer 	int child;
    285  1.1  bouyer 	int i;
    286  1.1  bouyer 	char buf[128];
    287  1.1  bouyer 	uint64_t id;
    288  1.1  bouyer 	struct fdt_endpoint *ep, *rep;
    289  1.1  bouyer 	struct fdt_device_ports *dp;
    290  1.1  bouyer 
    291  1.1  bouyer 	port->port_nep = 0;
    292  1.1  bouyer 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    293  1.1  bouyer 		if (OF_getprop(child, "name", buf, sizeof(buf)) <= 0)
    294  1.1  bouyer 			continue;
    295  1.1  bouyer 		if (strcmp(buf, "endpoint") != 0)
    296  1.1  bouyer 			continue;
    297  1.1  bouyer 		port->port_nep++;
    298  1.1  bouyer 	}
    299  1.1  bouyer 	if (port->port_nep == 0) {
    300  1.1  bouyer 		port->port_ep = NULL;
    301  1.1  bouyer 		return;
    302  1.1  bouyer 	}
    303  1.1  bouyer 
    304  1.1  bouyer 	port->port_ep =
    305  1.1  bouyer 	    kmem_zalloc(sizeof(struct fdt_endpoint) * port->port_nep, KM_SLEEP);
    306  1.1  bouyer 	KASSERT(port->port_ep != NULL);
    307  1.1  bouyer 	/* now scan again ports, looking for endpoints */
    308  1.1  bouyer 	for (child = OF_child(phandle), i = 0; child; child = OF_peer(child)) {
    309  1.1  bouyer 		if (OF_getprop(child, "name", buf, sizeof(buf)) <= 0)
    310  1.1  bouyer 			continue;
    311  1.1  bouyer 		if (strcmp(buf, "endpoint") != 0)
    312  1.1  bouyer 			continue;
    313  1.1  bouyer 		if (fdtbus_get_reg64(child, 0, &id, NULL) != 0) {
    314  1.1  bouyer 			if (port->port_nep > 1)
    315  1.1  bouyer 				aprint_error_dev(port->port_dp->dp_dev,
    316  1.1  bouyer 				    "%s: missing reg property",
    317  1.1  bouyer 				    fdtbus_get_string(child, "name"));
    318  1.1  bouyer 			id = i;
    319  1.1  bouyer 		}
    320  1.1  bouyer 		ep = &port->port_ep[i];
    321  1.1  bouyer 		ep->ep_id = id;
    322  1.1  bouyer 		ep->ep_type = type;
    323  1.1  bouyer 		ep->ep_phandle = child;
    324  1.1  bouyer 		ep->ep_port = port;
    325  1.1  bouyer 		ep->ep_rphandle = fdtbus_get_phandle(child, "remote-endpoint");
    326  1.1  bouyer 		ep->ep_rep = fdt_endpoint_get_from_phandle(
    327  1.1  bouyer 		    port->port_ep[i].ep_rphandle);
    328  1.1  bouyer 		rep = ep->ep_rep;
    329  1.1  bouyer 		if (rep != NULL && rep->ep_rep != NULL) {
    330  1.1  bouyer 			aprint_error("%s: ", ep_name(ep, buf, sizeof(buf)));
    331  1.1  bouyer 			aprint_error("remote endpoint %s ",
    332  1.1  bouyer 			    ep_name(rep, buf, sizeof(buf)));
    333  1.1  bouyer 			aprint_error("already connected to %s\n",
    334  1.1  bouyer 			    ep_name(rep->ep_rep, buf, sizeof(buf)));
    335  1.1  bouyer 		} else if (rep != NULL) {
    336  1.1  bouyer 			rep->ep_rep = ep;
    337  1.1  bouyer 			rep->ep_rphandle = child;
    338  1.1  bouyer 			aprint_verbose("%s ", ep_name(ep, buf, sizeof(buf)));
    339  1.1  bouyer 			aprint_verbose("connected to %s\n",
    340  1.1  bouyer 			    ep_name(rep, buf, sizeof(buf)));
    341  1.1  bouyer 			if (rep->ep_type == EP_OTHER)
    342  1.1  bouyer 				rep->ep_type = ep->ep_type;
    343  1.1  bouyer 			else if (ep->ep_type == EP_OTHER)
    344  1.1  bouyer 				ep->ep_type = rep->ep_type;
    345  1.1  bouyer 			dp = port->port_dp;
    346  1.1  bouyer 			if (dp->dp_ep_connect)
    347  1.1  bouyer 				dp->dp_ep_connect(dp->dp_dev, ep, true);
    348  1.1  bouyer 			dp = rep->ep_port->port_dp;
    349  1.1  bouyer 			if (dp->dp_ep_connect)
    350  1.1  bouyer 				dp->dp_ep_connect(dp->dp_dev, rep, true);
    351  1.1  bouyer 		}
    352  1.1  bouyer 		i++;
    353  1.1  bouyer 	}
    354  1.1  bouyer 	KASSERT(i == port->port_nep);
    355  1.1  bouyer }
    356  1.1  bouyer 
    357  1.1  bouyer static const char *
    358  1.1  bouyer ep_name(struct fdt_endpoint *ep, char *buf, int size)
    359  1.1  bouyer {
    360  1.1  bouyer 	int a;
    361  1.1  bouyer 
    362  1.1  bouyer 	a = snprintf(&buf[0], size, "%s",
    363  1.1  bouyer 	    device_xname(ep->ep_port->port_dp->dp_dev));
    364  1.1  bouyer 	if (ep->ep_port->port_id >= 0 && a < size)
    365  1.1  bouyer 		a += snprintf(&buf[a], size - a, " port %d",
    366  1.1  bouyer 		    ep->ep_port->port_id);
    367  1.1  bouyer 	if (ep->ep_id >= 0 && a < size)
    368  1.1  bouyer 		snprintf(&buf[a], size - a, " endpoint %d", ep->ep_id);
    369  1.1  bouyer 	return buf;
    370  1.1  bouyer }
    371