]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/ti/ti_hwmods.c
MFV r330102: ntp 4.2.8p11
[FreeBSD/FreeBSD.git] / sys / arm / ti / ti_hwmods.c
1 /*-
2  * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40
41 #include <machine/bus.h>
42 #include <machine/fdt.h>
43
44 #include <arm/ti/ti_prcm.h>
45 #include <arm/ti/ti_hwmods.h>
46
47 struct hwmod {
48         const char      *name;
49         int             clock_id;
50 };
51
52 struct hwmod ti_hwmods[] = {
53         {"i2c1",        I2C1_CLK},
54         {"i2c2",        I2C2_CLK},
55         {"i2c3",        I2C3_CLK},
56         {"i2c4",        I2C4_CLK},
57         {"i2c5",        I2C5_CLK},
58
59         {"gpio1",       GPIO1_CLK},
60         {"gpio2",       GPIO2_CLK},
61         {"gpio3",       GPIO3_CLK},
62         {"gpio4",       GPIO4_CLK},
63         {"gpio5",       GPIO5_CLK},
64         {"gpio6",       GPIO6_CLK},
65         {"gpio7",       GPIO7_CLK},
66
67         {"mmc1",        MMC1_CLK},
68         {"mmc2",        MMC2_CLK},
69         {"mmc3",        MMC3_CLK},
70         {"mmc4",        MMC4_CLK},
71         {"mmc5",        MMC5_CLK},
72         {"mmc6",        MMC6_CLK},
73
74         {"epwmss0",     PWMSS0_CLK},
75         {"epwmss1",     PWMSS1_CLK},
76         {"epwmss2",     PWMSS2_CLK},
77
78         {"spi0",        SPI0_CLK},
79         {"spi1",        SPI1_CLK},
80
81         {"timer1",      TIMER1_CLK},
82         {"timer2",      TIMER2_CLK},
83         {"timer3",      TIMER3_CLK},
84         {"timer4",      TIMER4_CLK},
85         {"timer5",      TIMER5_CLK},
86         {"timer6",      TIMER6_CLK},
87         {"timer7",      TIMER7_CLK},
88
89         {"uart1",       UART1_CLK},
90         {"uart2",       UART2_CLK},
91         {"uart3",       UART3_CLK},
92         {"uart4",       UART4_CLK},
93         {"uart5",       UART5_CLK},
94         {"uart6",       UART6_CLK},
95         {"uart7",       UART7_CLK},
96
97         {NULL,          0}
98 };
99
100 clk_ident_t
101 ti_hwmods_get_clock(device_t dev)
102 {
103         phandle_t node;
104         int len, l;
105         char *name;
106         char *buf;
107         int clk;
108         struct hwmod *hw;
109
110         if ((node = ofw_bus_get_node(dev)) == 0)
111                 return (INVALID_CLK_IDENT);
112
113         if ((len = OF_getprop_alloc(node, "ti,hwmods", 1, (void**)&name)) <= 0)
114                 return (INVALID_CLK_IDENT);
115
116         buf = name;
117
118         clk = INVALID_CLK_IDENT;
119         while ((len > 0) && (clk == INVALID_CLK_IDENT)) {
120                 for (hw = ti_hwmods; hw->name != NULL; ++hw) {
121                         if (strcmp(hw->name, name) == 0) {
122                                 clk = hw->clock_id;
123                                 break;
124                         }
125                 }
126
127                 /* Slide to the next sub-string. */
128                 l = strlen(name) + 1;
129                 name += l;
130                 len -= l;
131         }
132
133         if (len > 0)
134                 device_printf(dev, "WARNING: more than one ti,hwmod \n");
135
136         OF_prop_free(buf);
137         return (clk);
138 }
139
140 int ti_hwmods_contains(device_t dev, const char *hwmod)
141 {
142         phandle_t node;
143         int len, l;
144         char *name;
145         char *buf;
146         int result;
147
148         if ((node = ofw_bus_get_node(dev)) == 0)
149                 return (0);
150
151         if ((len = OF_getprop_alloc(node, "ti,hwmods", 1, (void**)&name)) <= 0)
152                 return (0);
153
154         buf = name;
155
156         result = 0;
157         while (len > 0) {
158                 if (strcmp(name, hwmod) == 0) {
159                         result = 1;
160                         break;
161                 }
162
163                 /* Slide to the next sub-string. */
164                 l = strlen(name) + 1;
165                 name += l;
166                 len -= l;
167         }
168
169         OF_prop_free(buf);
170
171         return (result);
172 }
173
174 int 
175 ti_hwmods_get_unit(device_t dev, const char *hwmod)
176 {
177         phandle_t node;
178         int l, len, hwmodlen, result;
179         char *name;
180         char *buf;
181
182         if ((node = ofw_bus_get_node(dev)) == 0)
183                 return (0);
184
185         if ((len = OF_getprop_alloc(node, "ti,hwmods", 1, (void**)&name)) <= 0)
186                 return (0);
187
188         buf = name;
189         hwmodlen = strlen(hwmod);
190         result = 0;
191         while (len > 0) {
192                 if (strncmp(name, hwmod, hwmodlen) == 0) {
193                         result = (int)strtoul(name + hwmodlen, NULL, 10);
194                         break;
195                 }
196                 /* Slide to the next sub-string. */
197                 l = strlen(name) + 1;
198                 name += l;
199                 len -= l;
200         }
201
202         OF_prop_free(buf);
203         return (result);
204 }