2 * Generic library functions for the microengines found on the Intel
3 * IXP2000 series of network processors.
5 * Copyright (C) 2004, 2005 Lennert Buytenhek <buytenh@wantstofly.org>
6 * Dedicated to Marija Kulikova.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/string.h>
20 #include <asm/hardware.h>
21 #include <asm/arch/ixp2000-regs.h>
22 #include <asm/arch/uengine.h>
25 #define USTORE_ADDRESS 0x000
26 #define USTORE_DATA_LOWER 0x004
27 #define USTORE_DATA_UPPER 0x008
28 #define CTX_ENABLES 0x018
29 #define CC_ENABLE 0x01c
30 #define CSR_CTX_POINTER 0x020
31 #define INDIRECT_CTX_STS 0x040
32 #define ACTIVE_CTX_STS 0x044
33 #define INDIRECT_CTX_SIG_EVENTS 0x048
34 #define INDIRECT_CTX_WAKEUP_EVENTS 0x050
37 #define TIMESTAMP_LOW 0x0c0
38 #define TIMESTAMP_HIGH 0x0c4
39 #define T_INDEX_BYTE_INDEX 0x0f4
40 #define LOCAL_CSR_STATUS 0x180
42 u32 ixp2000_uengine_mask;
44 static void *ixp2000_uengine_csr_area(int uengine)
46 return ((void *)IXP2000_UENGINE_CSR_VIRT_BASE) + (uengine << 10);
50 * LOCAL_CSR_STATUS=1 after a read or write to a microengine's CSR
51 * space means that the microengine we tried to access was also trying
52 * to access its own CSR space on the same clock cycle as we did. When
53 * this happens, we lose the arbitration process by default, and the
54 * read or write we tried to do was not actually performed, so we try
55 * again until it succeeds.
57 u32 ixp2000_uengine_csr_read(int uengine, int offset)
60 u32 *local_csr_status;
64 uebase = ixp2000_uengine_csr_area(uengine);
66 local_csr_status = (u32 *)(uebase + LOCAL_CSR_STATUS);
67 reg = (u32 *)(uebase + offset);
69 value = ixp2000_reg_read(reg);
70 } while (ixp2000_reg_read(local_csr_status) & 1);
74 EXPORT_SYMBOL(ixp2000_uengine_csr_read);
76 void ixp2000_uengine_csr_write(int uengine, int offset, u32 value)
79 u32 *local_csr_status;
82 uebase = ixp2000_uengine_csr_area(uengine);
84 local_csr_status = (u32 *)(uebase + LOCAL_CSR_STATUS);
85 reg = (u32 *)(uebase + offset);
87 ixp2000_reg_write(reg, value);
88 } while (ixp2000_reg_read(local_csr_status) & 1);
90 EXPORT_SYMBOL(ixp2000_uengine_csr_write);
92 void ixp2000_uengine_reset(u32 uengine_mask)
94 ixp2000_reg_write(IXP2000_RESET1, uengine_mask & ixp2000_uengine_mask);
95 ixp2000_reg_write(IXP2000_RESET1, 0);
97 EXPORT_SYMBOL(ixp2000_uengine_reset);
99 void ixp2000_uengine_set_mode(int uengine, u32 mode)
102 * CTL_STR_PAR_EN: unconditionally enable parity checking on
106 ixp2000_uengine_csr_write(uengine, CTX_ENABLES, mode);
109 * Enable updating of condition codes.
111 ixp2000_uengine_csr_write(uengine, CC_ENABLE, 0x00002000);
114 * Initialise other per-microengine registers.
116 ixp2000_uengine_csr_write(uengine, NN_PUT, 0x00);
117 ixp2000_uengine_csr_write(uengine, NN_GET, 0x00);
118 ixp2000_uengine_csr_write(uengine, T_INDEX_BYTE_INDEX, 0);
120 EXPORT_SYMBOL(ixp2000_uengine_set_mode);
122 static int make_even_parity(u32 x)
124 return hweight32(x) & 1;
127 static void ustore_write(int uengine, u64 insn)
130 * Generate even parity for top and bottom 20 bits.
132 insn |= (u64)make_even_parity((insn >> 20) & 0x000fffff) << 41;
133 insn |= (u64)make_even_parity(insn & 0x000fffff) << 40;
136 * Write to microstore. The second write auto-increments
137 * the USTORE_ADDRESS index register.
139 ixp2000_uengine_csr_write(uengine, USTORE_DATA_LOWER, (u32)insn);
140 ixp2000_uengine_csr_write(uengine, USTORE_DATA_UPPER, (u32)(insn >> 32));
143 void ixp2000_uengine_load_microcode(int uengine, u8 *ucode, int insns)
148 * Start writing to microstore at address 0.
150 ixp2000_uengine_csr_write(uengine, USTORE_ADDRESS, 0x80000000);
151 for (i = 0; i < insns; i++) {
154 insn = (((u64)ucode[0]) << 32) |
155 (((u64)ucode[1]) << 24) |
156 (((u64)ucode[2]) << 16) |
157 (((u64)ucode[3]) << 8) |
161 ustore_write(uengine, insn);
165 * Pad with a few NOPs at the end (to avoid the microengine
166 * aborting as it prefetches beyond the last instruction), unless
167 * we run off the end of the instruction store first, at which
168 * point the address register will wrap back to zero.
170 for (i = 0; i < 4; i++) {
173 addr = ixp2000_uengine_csr_read(uengine, USTORE_ADDRESS);
174 if (addr == 0x80000000)
176 ustore_write(uengine, 0xf0000c0300ULL);
182 ixp2000_uengine_csr_write(uengine, USTORE_ADDRESS, 0x00000000);
184 EXPORT_SYMBOL(ixp2000_uengine_load_microcode);
186 void ixp2000_uengine_init_context(int uengine, int context, int pc)
189 * Select the right context for indirect access.
191 ixp2000_uengine_csr_write(uengine, CSR_CTX_POINTER, context);
194 * Initialise signal masks to immediately go to Ready state.
196 ixp2000_uengine_csr_write(uengine, INDIRECT_CTX_SIG_EVENTS, 1);
197 ixp2000_uengine_csr_write(uengine, INDIRECT_CTX_WAKEUP_EVENTS, 1);
200 * Set program counter.
202 ixp2000_uengine_csr_write(uengine, INDIRECT_CTX_STS, pc);
204 EXPORT_SYMBOL(ixp2000_uengine_init_context);
206 void ixp2000_uengine_start_contexts(int uengine, u8 ctx_mask)
211 * Enable the specified context to go to Executing state.
213 mask = ixp2000_uengine_csr_read(uengine, CTX_ENABLES);
214 mask |= ctx_mask << 8;
215 ixp2000_uengine_csr_write(uengine, CTX_ENABLES, mask);
217 EXPORT_SYMBOL(ixp2000_uengine_start_contexts);
219 void ixp2000_uengine_stop_contexts(int uengine, u8 ctx_mask)
224 * Disable the Ready->Executing transition. Note that this
225 * does not stop the context until it voluntarily yields.
227 mask = ixp2000_uengine_csr_read(uengine, CTX_ENABLES);
228 mask &= ~(ctx_mask << 8);
229 ixp2000_uengine_csr_write(uengine, CTX_ENABLES, mask);
231 EXPORT_SYMBOL(ixp2000_uengine_stop_contexts);
233 static int check_ixp_type(struct ixp2000_uengine_code *c)
238 product_id = ixp2000_reg_read(IXP2000_PRODUCT_ID);
239 if (((product_id >> 16) & 0x1f) != 0)
242 switch ((product_id >> 8) & 0xff) {
243 case 0: /* IXP2800 */
244 if (!(c->cpu_model_bitmask & 4))
248 case 1: /* IXP2850 */
249 if (!(c->cpu_model_bitmask & 8))
253 case 2: /* IXP2400 */
254 if (!(c->cpu_model_bitmask & 2))
262 rev = product_id & 0xff;
263 if (rev < c->cpu_min_revision || rev > c->cpu_max_revision)
269 static void generate_ucode(u8 *ucode, u32 *gpr_a, u32 *gpr_b)
276 for (i = 0; i < 128; i++) {
282 b3 = (gpr_a[i] >> 24) & 0xff;
283 b2 = (gpr_a[i] >> 16) & 0xff;
284 b1 = (gpr_a[i] >> 8) & 0xff;
285 b0 = gpr_a[i] & 0xff;
287 // immed[@ai, (b1 << 8) | b0]
288 // 11110000 0000VVVV VVVV11VV VVVVVV00 1IIIIIII
289 ucode[offset++] = 0xf0;
290 ucode[offset++] = (b1 >> 4);
291 ucode[offset++] = (b1 << 4) | 0x0c | (b0 >> 6);
292 ucode[offset++] = (b0 << 2);
293 ucode[offset++] = 0x80 | i;
295 // immed_w1[@ai, (b3 << 8) | b2]
296 // 11110100 0100VVVV VVVV11VV VVVVVV00 1IIIIIII
297 ucode[offset++] = 0xf4;
298 ucode[offset++] = 0x40 | (b3 >> 4);
299 ucode[offset++] = (b3 << 4) | 0x0c | (b2 >> 6);
300 ucode[offset++] = (b2 << 2);
301 ucode[offset++] = 0x80 | i;
304 for (i = 0; i < 128; i++) {
310 b3 = (gpr_b[i] >> 24) & 0xff;
311 b2 = (gpr_b[i] >> 16) & 0xff;
312 b1 = (gpr_b[i] >> 8) & 0xff;
313 b0 = gpr_b[i] & 0xff;
315 // immed[@bi, (b1 << 8) | b0]
316 // 11110000 0000VVVV VVVV001I IIIIII11 VVVVVVVV
317 ucode[offset++] = 0xf0;
318 ucode[offset++] = (b1 >> 4);
319 ucode[offset++] = (b1 << 4) | 0x02 | (i >> 6);
320 ucode[offset++] = (i << 2) | 0x03;
321 ucode[offset++] = b0;
323 // immed_w1[@bi, (b3 << 8) | b2]
324 // 11110100 0100VVVV VVVV001I IIIIII11 VVVVVVVV
325 ucode[offset++] = 0xf4;
326 ucode[offset++] = 0x40 | (b3 >> 4);
327 ucode[offset++] = (b3 << 4) | 0x02 | (i >> 6);
328 ucode[offset++] = (i << 2) | 0x03;
329 ucode[offset++] = b2;
333 ucode[offset++] = 0xe0;
334 ucode[offset++] = 0x00;
335 ucode[offset++] = 0x01;
336 ucode[offset++] = 0x00;
337 ucode[offset++] = 0x00;
340 static int set_initial_registers(int uengine, struct ixp2000_uengine_code *c)
348 gpr_a = kmalloc(128 * sizeof(u32), GFP_KERNEL);
349 gpr_b = kmalloc(128 * sizeof(u32), GFP_KERNEL);
350 ucode = kmalloc(513 * 5, GFP_KERNEL);
351 if (gpr_a == NULL || gpr_b == NULL || ucode == NULL) {
359 if (c->uengine_parameters & IXP2000_UENGINE_4_CONTEXTS)
362 memset(gpr_a, 0, sizeof(gpr_a));
363 memset(gpr_b, 0, sizeof(gpr_b));
364 for (i = 0; i < 256; i++) {
365 struct ixp2000_reg_value *r = c->initial_reg_values + i;
373 bank = (r->reg & 0x400) ? gpr_b : gpr_a;
374 inc = (r->reg & 0x80) ? 128 : per_ctx_regs;
383 generate_ucode(ucode, gpr_a, gpr_b);
384 ixp2000_uengine_load_microcode(uengine, ucode, 513);
385 ixp2000_uengine_init_context(uengine, 0, 0);
386 ixp2000_uengine_start_contexts(uengine, 0x01);
387 for (i = 0; i < 100; i++) {
390 status = ixp2000_uengine_csr_read(uengine, ACTIVE_CTX_STS);
391 if (!(status & 0x80000000))
394 ixp2000_uengine_stop_contexts(uengine, 0x01);
403 int ixp2000_uengine_load(int uengine, struct ixp2000_uengine_code *c)
407 if (!check_ixp_type(c))
410 if (!(ixp2000_uengine_mask & (1 << uengine)))
413 ixp2000_uengine_reset(1 << uengine);
414 ixp2000_uengine_set_mode(uengine, c->uengine_parameters);
415 if (set_initial_registers(uengine, c))
417 ixp2000_uengine_load_microcode(uengine, c->insns, c->num_insns);
419 for (ctx = 0; ctx < 8; ctx++)
420 ixp2000_uengine_init_context(uengine, ctx, 0);
424 EXPORT_SYMBOL(ixp2000_uengine_load);
427 static int __init ixp2000_uengine_init(void)
433 * Determine number of microengines present.
435 switch ((ixp2000_reg_read(IXP2000_PRODUCT_ID) >> 8) & 0x1fff) {
436 case 0: /* IXP2800 */
437 case 1: /* IXP2850 */
438 ixp2000_uengine_mask = 0x00ff00ff;
441 case 2: /* IXP2400 */
442 ixp2000_uengine_mask = 0x000f000f;
446 printk(KERN_INFO "Detected unknown IXP2000 model (%.8x)\n",
447 (unsigned int)ixp2000_reg_read(IXP2000_PRODUCT_ID));
448 ixp2000_uengine_mask = 0x00000000;
453 * Reset microengines.
455 ixp2000_reg_write(IXP2000_RESET1, ixp2000_uengine_mask);
456 ixp2000_reg_write(IXP2000_RESET1, 0);
459 * Synchronise timestamp counters across all microengines.
461 value = ixp2000_reg_read(IXP2000_MISC_CONTROL);
462 ixp2000_reg_write(IXP2000_MISC_CONTROL, value & ~0x80);
463 for (uengine = 0; uengine < 32; uengine++) {
464 if (ixp2000_uengine_mask & (1 << uengine)) {
465 ixp2000_uengine_csr_write(uengine, TIMESTAMP_LOW, 0);
466 ixp2000_uengine_csr_write(uengine, TIMESTAMP_HIGH, 0);
469 ixp2000_reg_write(IXP2000_MISC_CONTROL, value | 0x80);
474 subsys_initcall(ixp2000_uengine_init);