blob: 32eabf5dfa4fcd736ec332087963b6a1be6552eb [file] [log] [blame]
Sascha Hauer8e77da62006-06-19 15:28:20 +01001/*
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>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Sascha Hauer8e77da62006-06-19 15:28:20 +010025
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/hardware.h>
27#include <mach/netx-regs.h>
Sascha Hauer8e77da62006-06-19 15:28:20 +010028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/xc.h>
Sascha Hauer8e77da62006-06-19 15:28:20 +010030
31static DEFINE_MUTEX(xc_lock);
32
33static int xc_in_use = 0;
34
35struct fw_desc {
36 unsigned int ofs;
37 unsigned int size;
38 unsigned int patch_ofs;
39 unsigned int patch_entries;
40};
41
42struct fw_header {
43 unsigned int magic;
44 unsigned int type;
45 unsigned int version;
46 unsigned int reserved[5];
47 struct fw_desc fw_desc[3];
48} __attribute__ ((packed));
49
50int xc_stop(struct xc *x)
51{
52 writel(RPU_HOLD_PC, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
53 writel(TPU_HOLD_PC, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
54 writel(XPU_HOLD_PC, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
55 return 0;
56}
57
58int xc_start(struct xc *x)
59{
60 writel(0, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
61 writel(0, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
62 writel(0, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
63 return 0;
64}
65
66int xc_running(struct xc *x)
67{
68 return (readl(x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS) & RPU_HOLD_PC)
69 || (readl(x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS) & TPU_HOLD_PC)
70 || (readl(x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS) & XPU_HOLD_PC) ?
71 0 : 1;
72}
73
74int xc_reset(struct xc *x)
75{
76 writel(0, x->xpec_base + NETX_XPEC_PC_OFS);
77 return 0;
78}
79
80static int xc_check_ptr(struct xc *x, unsigned long adr, unsigned int size)
81{
82 if (adr >= NETX_PA_XMAC(x->no) &&
83 adr + size < NETX_PA_XMAC(x->no) + XMAC_MEM_SIZE)
84 return 0;
85
86 if (adr >= NETX_PA_XPEC(x->no) &&
87 adr + size < NETX_PA_XPEC(x->no) + XPEC_MEM_SIZE)
88 return 0;
89
90 dev_err(x->dev, "Illegal pointer in firmware found. aborting\n");
91
92 return -1;
93}
94
95static int xc_patch(struct xc *x, void *patch, int count)
96{
97 unsigned int val, adr;
98 unsigned int *data = patch;
99
100 int i;
101 for (i = 0; i < count; i++) {
102 adr = *data++;
103 val = *data++;
104 if (xc_check_ptr(x, adr, 4) < 0)
105 return -EINVAL;
106
107 writel(val, (void __iomem *)io_p2v(adr));
108 }
109 return 0;
110}
111
112int xc_request_firmware(struct xc *x)
113{
114 int ret;
115 char name[16];
116 const struct firmware *fw;
117 struct fw_header *head;
118 unsigned int size;
119 int i;
120 void *src;
121 unsigned long dst;
122
123 sprintf(name, "xc%d.bin", x->no);
124
125 ret = request_firmware(&fw, name, x->dev);
126
127 if (ret < 0) {
128 dev_err(x->dev, "request_firmware failed\n");
129 return ret;
130 }
131
132 head = (struct fw_header *)fw->data;
133 if (head->magic != 0x4e657458) {
134 if (head->magic == 0x5874654e) {
135 dev_err(x->dev,
136 "firmware magic is 'XteN'. Endianess problems?\n");
137 ret = -ENODEV;
138 goto exit_release_firmware;
139 }
140 dev_err(x->dev, "unrecognized firmware magic 0x%08x\n",
141 head->magic);
142 ret = -ENODEV;
143 goto exit_release_firmware;
144 }
145
146 x->type = head->type;
147 x->version = head->version;
148
149 ret = -EINVAL;
150
151 for (i = 0; i < 3; i++) {
152 src = fw->data + head->fw_desc[i].ofs;
153 dst = *(unsigned int *)src;
154 src += sizeof (unsigned int);
155 size = head->fw_desc[i].size - sizeof (unsigned int);
156
157 if (xc_check_ptr(x, dst, size))
158 goto exit_release_firmware;
159
160 memcpy((void *)io_p2v(dst), src, size);
161
162 src = fw->data + head->fw_desc[i].patch_ofs;
163 size = head->fw_desc[i].patch_entries;
164 ret = xc_patch(x, src, size);
165 if (ret < 0)
166 goto exit_release_firmware;
167 }
168
169 ret = 0;
170
171 exit_release_firmware:
172 release_firmware(fw);
173
174 return ret;
175}
176
177struct xc *request_xc(int xcno, struct device *dev)
178{
179 struct xc *x = NULL;
180
181 mutex_lock(&xc_lock);
182
183 if (xcno > 3)
184 goto exit;
185 if (xc_in_use & (1 << xcno))
186 goto exit;
187
188 x = kmalloc(sizeof (struct xc), GFP_KERNEL);
189 if (!x)
190 goto exit;
191
192 if (!request_mem_region
Robert Schwebel93e29a22007-11-05 17:55:24 +0100193 (NETX_PA_XPEC(xcno), XPEC_MEM_SIZE, kobject_name(&dev->kobj)))
Sascha Hauer8e77da62006-06-19 15:28:20 +0100194 goto exit_free;
195
196 if (!request_mem_region
Robert Schwebel93e29a22007-11-05 17:55:24 +0100197 (NETX_PA_XMAC(xcno), XMAC_MEM_SIZE, kobject_name(&dev->kobj)))
Sascha Hauer8e77da62006-06-19 15:28:20 +0100198 goto exit_release_1;
199
200 if (!request_mem_region
Robert Schwebel93e29a22007-11-05 17:55:24 +0100201 (SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE, kobject_name(&dev->kobj)))
Sascha Hauer8e77da62006-06-19 15:28:20 +0100202 goto exit_release_2;
203
204 x->xpec_base = (void * __iomem)io_p2v(NETX_PA_XPEC(xcno));
205 x->xmac_base = (void * __iomem)io_p2v(NETX_PA_XMAC(xcno));
206 x->sram_base = ioremap(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
207 if (!x->sram_base)
208 goto exit_release_3;
209
210 x->irq = NETX_IRQ_XPEC(xcno);
211
212 x->no = xcno;
213 x->dev = dev;
214
215 xc_in_use |= (1 << xcno);
216
217 goto exit;
218
219 exit_release_3:
220 release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
221 exit_release_2:
222 release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
223 exit_release_1:
224 release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
225 exit_free:
226 kfree(x);
227 x = NULL;
228 exit:
229 mutex_unlock(&xc_lock);
230 return x;
231}
232
233void free_xc(struct xc *x)
234{
235 int xcno = x->no;
236
237 mutex_lock(&xc_lock);
238
239 iounmap(x->sram_base);
240 release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
241 release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
242 release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
243 xc_in_use &= ~(1 << x->no);
244 kfree(x);
245
246 mutex_unlock(&xc_lock);
247}
248
249EXPORT_SYMBOL(free_xc);
250EXPORT_SYMBOL(request_xc);
251EXPORT_SYMBOL(xc_request_firmware);
252EXPORT_SYMBOL(xc_reset);
253EXPORT_SYMBOL(xc_running);
254EXPORT_SYMBOL(xc_start);
255EXPORT_SYMBOL(xc_stop);