aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/kernel/devtree.c
blob: 1f824d2f151e9664e25ddabce563561376647ee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * arch/ubicom32/kernel/devtree.c
 *   Ubicom32 architecture device tree implementation.
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <asm/devtree.h>

/*
 * The device tree.
 */
struct devtree_node *devtree;

/*
 * devtree_print()
 *	Print the device tree.
 */
void devtree_print(void)
{
	struct devtree_node *p = devtree;
	printk(KERN_INFO "Device Tree:\n");
	while (p) {
		if (p->magic != DEVTREE_NODE_MAGIC) {
			printk(KERN_EMERG
			       "device tree has improper node: %p\n", p);
			return;
		}
		printk(KERN_INFO "\t%p: sendirq=%03d, recvirq=%03d, "
		       " name=%s\n", p, p->sendirq, p->recvirq, p->name);
		p = p->next;
	}
}
EXPORT_SYMBOL(devtree_print);

/*
 * devtree_irq()
 *	Return the IRQ(s) associated with devtree node.
 */
int devtree_irq(struct devtree_node *dn,
		unsigned char *sendirq,
		unsigned char *recvirq)
{
	if (dn->magic != DEVTREE_NODE_MAGIC) {
		printk(KERN_EMERG "improper node: %p\n", dn);
		if (sendirq) {
			*sendirq = DEVTREE_IRQ_NONE;
		}
		if (recvirq) {
			*recvirq = DEVTREE_IRQ_NONE;
		}
		return -EFAULT;
	}

	/*
	 * Copy the devtree irq(s) to the output parameters.
	 */
	if (sendirq) {
		*sendirq = dn->sendirq;
	}
	if (recvirq) {
		*recvirq = dn->recvirq;
	}
	return 0;
}
EXPORT_SYMBOL(devtree_irq);

/*
 * devtree_find_next()
 *	Provide an iterator for walking the device tree.
 */
struct devtree_node *devtree_find_next(struct devtree_node **cur)
{
	struct devtree_node *p = *cur;
	if (!p) {
		*cur = devtree;
		return devtree;
	}
	p = p->next;
	*cur = p;
	return p;
}

/*
 * devtree_find_by_irq()
 *	Return the node associated with a given irq.
 */
struct devtree_node *devtree_find_by_irq(uint8_t sendirq, uint8_t recvirq)
{
	struct devtree_node *p = devtree;

	if (sendirq == recvirq) {
		printk(KERN_EMERG "identical request makes no sense sendirq = "
		       "%d, recvirq= %d\n", sendirq, recvirq);
		return NULL;
	}

	while (p) {
		if (p->magic != DEVTREE_NODE_MAGIC) {
			printk(KERN_EMERG
			       "device tree has improper node: %p\n", p);
			return NULL;
		}

		/*
		 * See if we can find a match on the IRQ(s) specified.
		 */
		if ((sendirq == p->sendirq) && (recvirq == p->recvirq)) {
			return p;
		}

		if ((sendirq == DEVTREE_IRQ_DONTCARE) &&
		    (p->recvirq == recvirq)) {
			return p;
		}

		if ((recvirq == DEVTREE_IRQ_DONTCARE) &&
		    (p->sendirq == sendirq)) {
			return p;
		}

		p = p->next;
	}
	return NULL;
}
EXPORT_SYMBOL(devtree_find_by_irq);

/*
 * devtree_find_node()
 *	Find a node in the device tree by name.
 */
struct devtree_node *devtree_find_node(const char *str)
{
	struct devtree_node *p = devtree;
	while (p) {
		if (p->magic != DEVTREE_NODE_MAGIC) {
			printk(KERN_EMERG
			       "device tree has improper node: %p\n", p);
			return NULL;
		}
		if (strcmp(p->name, str) == 0) {
			return p;
		}
		p = p->next;
	}
	return NULL;
}
EXPORT_SYMBOL(devtree_find_node);