xref: /netbsd-src/sys/dev/fdt/fdt_phy.c (revision e1e972ce501a1859fa1c2d2e452ece8179bdd190)
1*e1e972ceSjmcneill /* $NetBSD: fdt_phy.c,v 1.6 2019/10/30 21:37:36 jmcneill Exp $ */
2612ef4c9Sjmcneill 
3612ef4c9Sjmcneill /*-
4612ef4c9Sjmcneill  * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca>
5612ef4c9Sjmcneill  * All rights reserved.
6612ef4c9Sjmcneill  *
7612ef4c9Sjmcneill  * Redistribution and use in source and binary forms, with or without
8612ef4c9Sjmcneill  * modification, are permitted provided that the following conditions
9612ef4c9Sjmcneill  * are met:
10612ef4c9Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11612ef4c9Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12612ef4c9Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13612ef4c9Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14612ef4c9Sjmcneill  *    documentation and/or other materials provided with the distribution.
15612ef4c9Sjmcneill  *
16612ef4c9Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17612ef4c9Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18612ef4c9Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19612ef4c9Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20612ef4c9Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21612ef4c9Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22612ef4c9Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23612ef4c9Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24612ef4c9Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25612ef4c9Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26612ef4c9Sjmcneill  * SUCH DAMAGE.
27612ef4c9Sjmcneill  */
28612ef4c9Sjmcneill 
29612ef4c9Sjmcneill #include <sys/cdefs.h>
30*e1e972ceSjmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_phy.c,v 1.6 2019/10/30 21:37:36 jmcneill Exp $");
31612ef4c9Sjmcneill 
32612ef4c9Sjmcneill #include <sys/param.h>
33612ef4c9Sjmcneill #include <sys/bus.h>
34612ef4c9Sjmcneill #include <sys/kmem.h>
35233c5d85Sjmcneill #include <sys/queue.h>
36612ef4c9Sjmcneill 
37612ef4c9Sjmcneill #include <libfdt.h>
38612ef4c9Sjmcneill #include <dev/fdt/fdtvar.h>
39612ef4c9Sjmcneill 
40612ef4c9Sjmcneill struct fdtbus_phy_controller {
41612ef4c9Sjmcneill 	device_t pc_dev;
42612ef4c9Sjmcneill 	int pc_phandle;
43612ef4c9Sjmcneill 	const struct fdtbus_phy_controller_func *pc_funcs;
44612ef4c9Sjmcneill 
45233c5d85Sjmcneill 	LIST_ENTRY(fdtbus_phy_controller) pc_next;
46612ef4c9Sjmcneill };
47612ef4c9Sjmcneill 
48233c5d85Sjmcneill static LIST_HEAD(, fdtbus_phy_controller) fdtbus_phy_controllers =
49233c5d85Sjmcneill     LIST_HEAD_INITIALIZER(fdtbus_phy_controllers);
50612ef4c9Sjmcneill 
51612ef4c9Sjmcneill int
fdtbus_register_phy_controller(device_t dev,int phandle,const struct fdtbus_phy_controller_func * funcs)52612ef4c9Sjmcneill fdtbus_register_phy_controller(device_t dev, int phandle,
53612ef4c9Sjmcneill     const struct fdtbus_phy_controller_func *funcs)
54612ef4c9Sjmcneill {
55612ef4c9Sjmcneill 	struct fdtbus_phy_controller *pc;
56612ef4c9Sjmcneill 
57612ef4c9Sjmcneill 	pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
58612ef4c9Sjmcneill 	pc->pc_dev = dev;
59612ef4c9Sjmcneill 	pc->pc_phandle = phandle;
60612ef4c9Sjmcneill 	pc->pc_funcs = funcs;
61612ef4c9Sjmcneill 
62233c5d85Sjmcneill 	LIST_INSERT_HEAD(&fdtbus_phy_controllers, pc, pc_next);
63612ef4c9Sjmcneill 
64612ef4c9Sjmcneill 	return 0;
65612ef4c9Sjmcneill }
66612ef4c9Sjmcneill 
67612ef4c9Sjmcneill static struct fdtbus_phy_controller *
fdtbus_get_phy_controller(int phandle)68612ef4c9Sjmcneill fdtbus_get_phy_controller(int phandle)
69612ef4c9Sjmcneill {
70612ef4c9Sjmcneill 	struct fdtbus_phy_controller *pc;
71612ef4c9Sjmcneill 
72233c5d85Sjmcneill 	LIST_FOREACH(pc, &fdtbus_phy_controllers, pc_next) {
73233c5d85Sjmcneill 		if (pc->pc_phandle == phandle)
74612ef4c9Sjmcneill 			return pc;
75612ef4c9Sjmcneill 	}
76612ef4c9Sjmcneill 
77612ef4c9Sjmcneill 	return NULL;
78612ef4c9Sjmcneill }
79612ef4c9Sjmcneill 
80612ef4c9Sjmcneill struct fdtbus_phy *
fdtbus_phy_get_index(int phandle,u_int index)81612ef4c9Sjmcneill fdtbus_phy_get_index(int phandle, u_int index)
82612ef4c9Sjmcneill {
83612ef4c9Sjmcneill 	struct fdtbus_phy_controller *pc;
84612ef4c9Sjmcneill 	struct fdtbus_phy *phy = NULL;
85612ef4c9Sjmcneill 	void *phy_priv = NULL;
86612ef4c9Sjmcneill 	uint32_t *phys = NULL;
87612ef4c9Sjmcneill 	uint32_t *p;
88612ef4c9Sjmcneill 	u_int n, phy_cells;
89612ef4c9Sjmcneill 	int len, resid;
90612ef4c9Sjmcneill 
91612ef4c9Sjmcneill 	len = OF_getproplen(phandle, "phys");
92612ef4c9Sjmcneill 	if (len <= 0)
93612ef4c9Sjmcneill 		return NULL;
94612ef4c9Sjmcneill 
95612ef4c9Sjmcneill 	phys = kmem_alloc(len, KM_SLEEP);
96612ef4c9Sjmcneill 	if (OF_getprop(phandle, "phys", phys, len) != len) {
97612ef4c9Sjmcneill 		kmem_free(phys, len);
98612ef4c9Sjmcneill 		return NULL;
99612ef4c9Sjmcneill 	}
100612ef4c9Sjmcneill 
101612ef4c9Sjmcneill 	p = phys;
102612ef4c9Sjmcneill 	for (n = 0, resid = len; resid > 0; n++) {
103*e1e972ceSjmcneill 		if (p[0] == 0) {
104*e1e972ceSjmcneill 			phy_cells = 0;
105*e1e972ceSjmcneill 			goto next;
106*e1e972ceSjmcneill 		}
107612ef4c9Sjmcneill 		const int pc_phandle =
108612ef4c9Sjmcneill 		    fdtbus_get_phandle_from_native(be32toh(p[0]));
109612ef4c9Sjmcneill 		if (of_getprop_uint32(pc_phandle, "#phy-cells", &phy_cells))
110612ef4c9Sjmcneill 			break;
111612ef4c9Sjmcneill 		if (n == index) {
112612ef4c9Sjmcneill 			pc = fdtbus_get_phy_controller(pc_phandle);
113612ef4c9Sjmcneill 			if (pc == NULL)
114612ef4c9Sjmcneill 				goto done;
115612ef4c9Sjmcneill 			phy_priv = pc->pc_funcs->acquire(pc->pc_dev,
116612ef4c9Sjmcneill 			    phy_cells > 0 ? &p[1] : NULL, phy_cells * 4);
117612ef4c9Sjmcneill 			if (phy_priv) {
118612ef4c9Sjmcneill 				phy = kmem_alloc(sizeof(*phy), KM_SLEEP);
119612ef4c9Sjmcneill 				phy->phy_pc = pc;
120612ef4c9Sjmcneill 				phy->phy_priv = phy_priv;
121612ef4c9Sjmcneill 			}
122612ef4c9Sjmcneill 			break;
123612ef4c9Sjmcneill 		}
124*e1e972ceSjmcneill next:
125612ef4c9Sjmcneill 		resid -= (phy_cells + 1) * 4;
126612ef4c9Sjmcneill 		p += phy_cells + 1;
127612ef4c9Sjmcneill 	}
128612ef4c9Sjmcneill 
129612ef4c9Sjmcneill done:
130612ef4c9Sjmcneill 	if (phys)
131612ef4c9Sjmcneill 		kmem_free(phys, len);
132612ef4c9Sjmcneill 
133612ef4c9Sjmcneill 	return phy;
134612ef4c9Sjmcneill }
135612ef4c9Sjmcneill 
136612ef4c9Sjmcneill struct fdtbus_phy *
fdtbus_phy_get(int phandle,const char * phyname)137612ef4c9Sjmcneill fdtbus_phy_get(int phandle, const char *phyname)
138612ef4c9Sjmcneill {
139612ef4c9Sjmcneill 	u_int index;
14021aceb10Sjakllsch 	int err;
141612ef4c9Sjmcneill 
14221aceb10Sjakllsch 	err = fdtbus_get_index(phandle, "phy-names", phyname, &index);
14321aceb10Sjakllsch 	if (err != 0)
144612ef4c9Sjmcneill 		return NULL;
145612ef4c9Sjmcneill 
14621aceb10Sjakllsch 	return fdtbus_phy_get_index(phandle, index);
147612ef4c9Sjmcneill }
148612ef4c9Sjmcneill 
149612ef4c9Sjmcneill void
fdtbus_phy_put(struct fdtbus_phy * phy)150612ef4c9Sjmcneill fdtbus_phy_put(struct fdtbus_phy *phy)
151612ef4c9Sjmcneill {
152612ef4c9Sjmcneill 	struct fdtbus_phy_controller *pc = phy->phy_pc;
153612ef4c9Sjmcneill 
154612ef4c9Sjmcneill 	pc->pc_funcs->release(pc->pc_dev, phy->phy_priv);
155612ef4c9Sjmcneill 	kmem_free(phy, sizeof(*phy));
156612ef4c9Sjmcneill }
157612ef4c9Sjmcneill 
158a9d03646Sjmcneill device_t
fdtbus_phy_device(struct fdtbus_phy * phy)159a9d03646Sjmcneill fdtbus_phy_device(struct fdtbus_phy *phy)
160a9d03646Sjmcneill {
161a9d03646Sjmcneill 	return phy->phy_pc->pc_dev;
162a9d03646Sjmcneill }
163a9d03646Sjmcneill 
164612ef4c9Sjmcneill int
fdtbus_phy_enable(struct fdtbus_phy * phy,bool enable)165612ef4c9Sjmcneill fdtbus_phy_enable(struct fdtbus_phy *phy, bool enable)
166612ef4c9Sjmcneill {
167612ef4c9Sjmcneill 	struct fdtbus_phy_controller *pc = phy->phy_pc;
168612ef4c9Sjmcneill 
169612ef4c9Sjmcneill 	return pc->pc_funcs->enable(pc->pc_dev, phy->phy_priv, enable);
170612ef4c9Sjmcneill }
171