181a78ba81654bdae53bda56c7ede38faad6063b
[deliverable/linux.git] / arch / arm / mach-netx / xc.c
1 /*
2 * arch/arm/mach-netx/xc.c
3 *
4 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/firmware.h>
23 #include <linux/mutex.h>
24 #include <linux/io.h>
25
26 #include <mach/hardware.h>
27 #include <mach/irqs.h>
28 #include <mach/netx-regs.h>
29
30 #include <mach/xc.h>
31
32 static DEFINE_MUTEX(xc_lock);
33
34 static int xc_in_use = 0;
35
36 struct fw_desc {
37 unsigned int ofs;
38 unsigned int size;
39 unsigned int patch_ofs;
40 unsigned int patch_entries;
41 };
42
43 struct fw_header {
44 unsigned int magic;
45 unsigned int type;
46 unsigned int version;
47 unsigned int reserved[5];
48 struct fw_desc fw_desc[3];
49 } __attribute__ ((packed));
50
51 int xc_stop(struct xc *x)
52 {
53 writel(RPU_HOLD_PC, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
54 writel(TPU_HOLD_PC, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
55 writel(XPU_HOLD_PC, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
56 return 0;
57 }
58
59 int xc_start(struct xc *x)
60 {
61 writel(0, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
62 writel(0, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
63 writel(0, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
64 return 0;
65 }
66
67 int xc_running(struct xc *x)
68 {
69 return (readl(x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS) & RPU_HOLD_PC)
70 || (readl(x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS) & TPU_HOLD_PC)
71 || (readl(x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS) & XPU_HOLD_PC) ?
72 0 : 1;
73 }
74
75 int xc_reset(struct xc *x)
76 {
77 writel(0, x->xpec_base + NETX_XPEC_PC_OFS);
78 return 0;
79 }
80
81 static int xc_check_ptr(struct xc *x, unsigned long adr, unsigned int size)
82 {
83 if (adr >= NETX_PA_XMAC(x->no) &&
84 adr + size < NETX_PA_XMAC(x->no) + XMAC_MEM_SIZE)
85 return 0;
86
87 if (adr >= NETX_PA_XPEC(x->no) &&
88 adr + size < NETX_PA_XPEC(x->no) + XPEC_MEM_SIZE)
89 return 0;
90
91 dev_err(x->dev, "Illegal pointer in firmware found. aborting\n");
92
93 return -1;
94 }
95
96 static int xc_patch(struct xc *x, const void *patch, int count)
97 {
98 unsigned int val, adr;
99 const unsigned int *data = patch;
100
101 int i;
102 for (i = 0; i < count; i++) {
103 adr = *data++;
104 val = *data++;
105 if (xc_check_ptr(x, adr, 4) < 0)
106 return -EINVAL;
107
108 writel(val, (void __iomem *)io_p2v(adr));
109 }
110 return 0;
111 }
112
113 int xc_request_firmware(struct xc *x)
114 {
115 int ret;
116 char name[16];
117 const struct firmware *fw;
118 struct fw_header *head;
119 unsigned int size;
120 int i;
121 const void *src;
122 unsigned long dst;
123
124 sprintf(name, "xc%d.bin", x->no);
125
126 ret = request_firmware(&fw, name, x->dev);
127
128 if (ret < 0) {
129 dev_err(x->dev, "request_firmware failed\n");
130 return ret;
131 }
132
133 head = (struct fw_header *)fw->data;
134 if (head->magic != 0x4e657458) {
135 if (head->magic == 0x5874654e) {
136 dev_err(x->dev,
137 "firmware magic is 'XteN'. Endianess problems?\n");
138 ret = -ENODEV;
139 goto exit_release_firmware;
140 }
141 dev_err(x->dev, "unrecognized firmware magic 0x%08x\n",
142 head->magic);
143 ret = -ENODEV;
144 goto exit_release_firmware;
145 }
146
147 x->type = head->type;
148 x->version = head->version;
149
150 ret = -EINVAL;
151
152 for (i = 0; i < 3; i++) {
153 src = fw->data + head->fw_desc[i].ofs;
154 dst = *(unsigned int *)src;
155 src += sizeof (unsigned int);
156 size = head->fw_desc[i].size - sizeof (unsigned int);
157
158 if (xc_check_ptr(x, dst, size))
159 goto exit_release_firmware;
160
161 memcpy((void *)io_p2v(dst), src, size);
162
163 src = fw->data + head->fw_desc[i].patch_ofs;
164 size = head->fw_desc[i].patch_entries;
165 ret = xc_patch(x, src, size);
166 if (ret < 0)
167 goto exit_release_firmware;
168 }
169
170 ret = 0;
171
172 exit_release_firmware:
173 release_firmware(fw);
174
175 return ret;
176 }
177
178 struct xc *request_xc(int xcno, struct device *dev)
179 {
180 struct xc *x = NULL;
181
182 mutex_lock(&xc_lock);
183
184 if (xcno > 3)
185 goto exit;
186 if (xc_in_use & (1 << xcno))
187 goto exit;
188
189 x = kmalloc(sizeof (struct xc), GFP_KERNEL);
190 if (!x)
191 goto exit;
192
193 if (!request_mem_region
194 (NETX_PA_XPEC(xcno), XPEC_MEM_SIZE, kobject_name(&dev->kobj)))
195 goto exit_free;
196
197 if (!request_mem_region
198 (NETX_PA_XMAC(xcno), XMAC_MEM_SIZE, kobject_name(&dev->kobj)))
199 goto exit_release_1;
200
201 if (!request_mem_region
202 (SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE, kobject_name(&dev->kobj)))
203 goto exit_release_2;
204
205 x->xpec_base = (void * __iomem)io_p2v(NETX_PA_XPEC(xcno));
206 x->xmac_base = (void * __iomem)io_p2v(NETX_PA_XMAC(xcno));
207 x->sram_base = ioremap(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
208 if (!x->sram_base)
209 goto exit_release_3;
210
211 x->irq = NETX_IRQ_XPEC(xcno);
212
213 x->no = xcno;
214 x->dev = dev;
215
216 xc_in_use |= (1 << xcno);
217
218 goto exit;
219
220 exit_release_3:
221 release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
222 exit_release_2:
223 release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
224 exit_release_1:
225 release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
226 exit_free:
227 kfree(x);
228 x = NULL;
229 exit:
230 mutex_unlock(&xc_lock);
231 return x;
232 }
233
234 void free_xc(struct xc *x)
235 {
236 int xcno = x->no;
237
238 mutex_lock(&xc_lock);
239
240 iounmap(x->sram_base);
241 release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
242 release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
243 release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
244 xc_in_use &= ~(1 << x->no);
245 kfree(x);
246
247 mutex_unlock(&xc_lock);
248 }
249
250 EXPORT_SYMBOL(free_xc);
251 EXPORT_SYMBOL(request_xc);
252 EXPORT_SYMBOL(xc_request_firmware);
253 EXPORT_SYMBOL(xc_reset);
254 EXPORT_SYMBOL(xc_running);
255 EXPORT_SYMBOL(xc_start);
256 EXPORT_SYMBOL(xc_stop);
This page took 0.034958 seconds and 5 git commands to generate.