x86: fix bogus KERN_ALERT on oops
[wrt350n-kernel.git] / arch / powerpc / sysdev / bestcomm / bestcomm.c
blob740ad73ce5cc4e3c58044053e7dcedecc844fd2e
1 /*
2 * Driver for MPC52xx processor BestComm peripheral controller
5 * Copyright (C) 2006-2007 Sylvain Munaut <tnt@246tNt.com>
6 * Copyright (C) 2005 Varma Electronics Oy,
7 * ( by Andrey Volkov <avolkov@varma-el.com> )
8 * Copyright (C) 2003-2004 MontaVista, Software, Inc.
9 * ( by Dale Farnsworth <dfarnsworth@mvista.com> )
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of any
13 * kind, whether express or implied.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/of_platform.h>
22 #include <asm/io.h>
23 #include <asm/irq.h>
24 #include <asm/mpc52xx.h>
26 #include "sram.h"
27 #include "bestcomm_priv.h"
28 #include "bestcomm.h"
30 #define DRIVER_NAME "bestcomm-core"
33 struct bcom_engine *bcom_eng = NULL;
34 EXPORT_SYMBOL_GPL(bcom_eng); /* needed for inline functions */
37 /* ======================================================================== */
38 /* Public and private API */
39 /* ======================================================================== */
41 /* Private API */
43 struct bcom_task *
44 bcom_task_alloc(int bd_count, int bd_size, int priv_size)
46 int i, tasknum = -1;
47 struct bcom_task *tsk;
49 /* Get and reserve a task num */
50 spin_lock(&bcom_eng->lock);
52 for (i=0; i<BCOM_MAX_TASKS; i++)
53 if (!bcom_eng->tdt[i].stop) { /* we use stop as a marker */
54 bcom_eng->tdt[i].stop = 0xfffffffful; /* dummy addr */
55 tasknum = i;
56 break;
59 spin_unlock(&bcom_eng->lock);
61 if (tasknum < 0)
62 return NULL;
64 /* Allocate our structure */
65 tsk = kzalloc(sizeof(struct bcom_task) + priv_size, GFP_KERNEL);
66 if (!tsk)
67 goto error;
69 tsk->tasknum = tasknum;
70 if (priv_size)
71 tsk->priv = (void*)tsk + sizeof(struct bcom_task);
73 /* Get IRQ of that task */
74 tsk->irq = irq_of_parse_and_map(bcom_eng->ofnode, tsk->tasknum);
75 if (tsk->irq == NO_IRQ)
76 goto error;
78 /* Init the BDs, if needed */
79 if (bd_count) {
80 tsk->cookie = kmalloc(sizeof(void*) * bd_count, GFP_KERNEL);
81 if (!tsk->cookie)
82 goto error;
84 tsk->bd = bcom_sram_alloc(bd_count * bd_size, 4, &tsk->bd_pa);
85 if (!tsk->bd)
86 goto error;
87 memset(tsk->bd, 0x00, bd_count * bd_size);
89 tsk->num_bd = bd_count;
90 tsk->bd_size = bd_size;
93 return tsk;
95 error:
96 if (tsk) {
97 if (tsk->irq != NO_IRQ)
98 irq_dispose_mapping(tsk->irq);
99 bcom_sram_free(tsk->bd);
100 kfree(tsk->cookie);
101 kfree(tsk);
104 bcom_eng->tdt[tasknum].stop = 0;
106 return NULL;
108 EXPORT_SYMBOL_GPL(bcom_task_alloc);
110 void
111 bcom_task_free(struct bcom_task *tsk)
113 /* Stop the task */
114 bcom_disable_task(tsk->tasknum);
116 /* Clear TDT */
117 bcom_eng->tdt[tsk->tasknum].start = 0;
118 bcom_eng->tdt[tsk->tasknum].stop = 0;
120 /* Free everything */
121 irq_dispose_mapping(tsk->irq);
122 bcom_sram_free(tsk->bd);
123 kfree(tsk->cookie);
124 kfree(tsk);
126 EXPORT_SYMBOL_GPL(bcom_task_free);
129 bcom_load_image(int task, u32 *task_image)
131 struct bcom_task_header *hdr = (struct bcom_task_header *)task_image;
132 struct bcom_tdt *tdt;
133 u32 *desc, *var, *inc;
134 u32 *desc_src, *var_src, *inc_src;
136 /* Safety checks */
137 if (hdr->magic != BCOM_TASK_MAGIC) {
138 printk(KERN_ERR DRIVER_NAME
139 ": Trying to load invalid microcode\n");
140 return -EINVAL;
143 if ((task < 0) || (task >= BCOM_MAX_TASKS)) {
144 printk(KERN_ERR DRIVER_NAME
145 ": Trying to load invalid task %d\n", task);
146 return -EINVAL;
149 /* Initial load or reload */
150 tdt = &bcom_eng->tdt[task];
152 if (tdt->start) {
153 desc = bcom_task_desc(task);
154 if (hdr->desc_size != bcom_task_num_descs(task)) {
155 printk(KERN_ERR DRIVER_NAME
156 ": Trying to reload wrong task image "
157 "(%d size %d/%d)!\n",
158 task,
159 hdr->desc_size,
160 bcom_task_num_descs(task));
161 return -EINVAL;
163 } else {
164 phys_addr_t start_pa;
166 desc = bcom_sram_alloc(hdr->desc_size * sizeof(u32), 4, &start_pa);
167 if (!desc)
168 return -ENOMEM;
170 tdt->start = start_pa;
171 tdt->stop = start_pa + ((hdr->desc_size-1) * sizeof(u32));
174 var = bcom_task_var(task);
175 inc = bcom_task_inc(task);
177 /* Clear & copy */
178 memset(var, 0x00, BCOM_VAR_SIZE);
179 memset(inc, 0x00, BCOM_INC_SIZE);
181 desc_src = (u32 *)(hdr + 1);
182 var_src = desc_src + hdr->desc_size;
183 inc_src = var_src + hdr->var_size;
185 memcpy(desc, desc_src, hdr->desc_size * sizeof(u32));
186 memcpy(var + hdr->first_var, var_src, hdr->var_size * sizeof(u32));
187 memcpy(inc, inc_src, hdr->inc_size * sizeof(u32));
189 return 0;
191 EXPORT_SYMBOL_GPL(bcom_load_image);
193 void
194 bcom_set_initiator(int task, int initiator)
196 int i;
197 int num_descs;
198 u32 *desc;
199 int next_drd_has_initiator;
201 bcom_set_tcr_initiator(task, initiator);
203 /* Just setting tcr is apparently not enough due to some problem */
204 /* with it. So we just go thru all the microcode and replace in */
205 /* the DRD directly */
207 desc = bcom_task_desc(task);
208 next_drd_has_initiator = 1;
209 num_descs = bcom_task_num_descs(task);
211 for (i=0; i<num_descs; i++, desc++) {
212 if (!bcom_desc_is_drd(*desc))
213 continue;
214 if (next_drd_has_initiator)
215 if (bcom_desc_initiator(*desc) != BCOM_INITIATOR_ALWAYS)
216 bcom_set_desc_initiator(desc, initiator);
217 next_drd_has_initiator = !bcom_drd_is_extended(*desc);
220 EXPORT_SYMBOL_GPL(bcom_set_initiator);
223 /* Public API */
225 void
226 bcom_enable(struct bcom_task *tsk)
228 bcom_enable_task(tsk->tasknum);
230 EXPORT_SYMBOL_GPL(bcom_enable);
232 void
233 bcom_disable(struct bcom_task *tsk)
235 bcom_disable_task(tsk->tasknum);
237 EXPORT_SYMBOL_GPL(bcom_disable);
240 /* ======================================================================== */
241 /* Engine init/cleanup */
242 /* ======================================================================== */
244 /* Function Descriptor table */
245 /* this will need to be updated if Freescale changes their task code FDT */
246 static u32 fdt_ops[] = {
247 0xa0045670, /* FDT[48] - load_acc() */
248 0x80045670, /* FDT[49] - unload_acc() */
249 0x21800000, /* FDT[50] - and() */
250 0x21e00000, /* FDT[51] - or() */
251 0x21500000, /* FDT[52] - xor() */
252 0x21400000, /* FDT[53] - andn() */
253 0x21500000, /* FDT[54] - not() */
254 0x20400000, /* FDT[55] - add() */
255 0x20500000, /* FDT[56] - sub() */
256 0x20800000, /* FDT[57] - lsh() */
257 0x20a00000, /* FDT[58] - rsh() */
258 0xc0170000, /* FDT[59] - crc8() */
259 0xc0145670, /* FDT[60] - crc16() */
260 0xc0345670, /* FDT[61] - crc32() */
261 0xa0076540, /* FDT[62] - endian32() */
262 0xa0000760, /* FDT[63] - endian16() */
266 static int __devinit
267 bcom_engine_init(void)
269 int task;
270 phys_addr_t tdt_pa, ctx_pa, var_pa, fdt_pa;
271 unsigned int tdt_size, ctx_size, var_size, fdt_size;
272 u16 regval;
274 /* Allocate & clear SRAM zones for FDT, TDTs, contexts and vars/incs */
275 tdt_size = BCOM_MAX_TASKS * sizeof(struct bcom_tdt);
276 ctx_size = BCOM_MAX_TASKS * BCOM_CTX_SIZE;
277 var_size = BCOM_MAX_TASKS * (BCOM_VAR_SIZE + BCOM_INC_SIZE);
278 fdt_size = BCOM_FDT_SIZE;
280 bcom_eng->tdt = bcom_sram_alloc(tdt_size, sizeof(u32), &tdt_pa);
281 bcom_eng->ctx = bcom_sram_alloc(ctx_size, BCOM_CTX_ALIGN, &ctx_pa);
282 bcom_eng->var = bcom_sram_alloc(var_size, BCOM_VAR_ALIGN, &var_pa);
283 bcom_eng->fdt = bcom_sram_alloc(fdt_size, BCOM_FDT_ALIGN, &fdt_pa);
285 if (!bcom_eng->tdt || !bcom_eng->ctx || !bcom_eng->var || !bcom_eng->fdt) {
286 printk(KERN_ERR "DMA: SRAM alloc failed in engine init !\n");
288 bcom_sram_free(bcom_eng->tdt);
289 bcom_sram_free(bcom_eng->ctx);
290 bcom_sram_free(bcom_eng->var);
291 bcom_sram_free(bcom_eng->fdt);
293 return -ENOMEM;
296 memset(bcom_eng->tdt, 0x00, tdt_size);
297 memset(bcom_eng->ctx, 0x00, ctx_size);
298 memset(bcom_eng->var, 0x00, var_size);
299 memset(bcom_eng->fdt, 0x00, fdt_size);
301 /* Copy the FDT for the EU#3 */
302 memcpy(&bcom_eng->fdt[48], fdt_ops, sizeof(fdt_ops));
304 /* Initialize Task base structure */
305 for (task=0; task<BCOM_MAX_TASKS; task++)
307 out_be16(&bcom_eng->regs->tcr[task], 0);
308 out_8(&bcom_eng->regs->ipr[task], 0);
310 bcom_eng->tdt[task].context = ctx_pa;
311 bcom_eng->tdt[task].var = var_pa;
312 bcom_eng->tdt[task].fdt = fdt_pa;
314 var_pa += BCOM_VAR_SIZE + BCOM_INC_SIZE;
315 ctx_pa += BCOM_CTX_SIZE;
318 out_be32(&bcom_eng->regs->taskBar, tdt_pa);
320 /* Init 'always' initiator */
321 out_8(&bcom_eng->regs->ipr[BCOM_INITIATOR_ALWAYS], BCOM_IPR_ALWAYS);
323 /* Disable COMM Bus Prefetch on the original 5200; it's broken */
324 if ((mfspr(SPRN_SVR) & MPC5200_SVR_MASK) == MPC5200_SVR) {
325 regval = in_be16(&bcom_eng->regs->PtdCntrl);
326 out_be16(&bcom_eng->regs->PtdCntrl, regval | 1);
329 /* Init lock */
330 spin_lock_init(&bcom_eng->lock);
332 return 0;
335 static void
336 bcom_engine_cleanup(void)
338 int task;
340 /* Stop all tasks */
341 for (task=0; task<BCOM_MAX_TASKS; task++)
343 out_be16(&bcom_eng->regs->tcr[task], 0);
344 out_8(&bcom_eng->regs->ipr[task], 0);
347 out_be32(&bcom_eng->regs->taskBar, 0ul);
349 /* Release the SRAM zones */
350 bcom_sram_free(bcom_eng->tdt);
351 bcom_sram_free(bcom_eng->ctx);
352 bcom_sram_free(bcom_eng->var);
353 bcom_sram_free(bcom_eng->fdt);
357 /* ======================================================================== */
358 /* OF platform driver */
359 /* ======================================================================== */
361 static int __devinit
362 mpc52xx_bcom_probe(struct of_device *op, const struct of_device_id *match)
364 struct device_node *ofn_sram;
365 struct resource res_bcom;
367 int rv;
369 /* Inform user we're ok so far */
370 printk(KERN_INFO "DMA: MPC52xx BestComm driver\n");
372 /* Get the bestcomm node */
373 of_node_get(op->node);
375 /* Prepare SRAM */
376 ofn_sram = of_find_compatible_node(NULL, "sram", "mpc5200-sram");
377 if (!ofn_sram) {
378 printk(KERN_ERR DRIVER_NAME ": "
379 "No SRAM found in device tree\n");
380 rv = -ENODEV;
381 goto error_ofput;
383 rv = bcom_sram_init(ofn_sram, DRIVER_NAME);
384 of_node_put(ofn_sram);
386 if (rv) {
387 printk(KERN_ERR DRIVER_NAME ": "
388 "Error in SRAM init\n");
389 goto error_ofput;
392 /* Get a clean struct */
393 bcom_eng = kzalloc(sizeof(struct bcom_engine), GFP_KERNEL);
394 if (!bcom_eng) {
395 printk(KERN_ERR DRIVER_NAME ": "
396 "Can't allocate state structure\n");
397 rv = -ENOMEM;
398 goto error_sramclean;
401 /* Save the node */
402 bcom_eng->ofnode = op->node;
404 /* Get, reserve & map io */
405 if (of_address_to_resource(op->node, 0, &res_bcom)) {
406 printk(KERN_ERR DRIVER_NAME ": "
407 "Can't get resource\n");
408 rv = -EINVAL;
409 goto error_sramclean;
412 if (!request_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma),
413 DRIVER_NAME)) {
414 printk(KERN_ERR DRIVER_NAME ": "
415 "Can't request registers region\n");
416 rv = -EBUSY;
417 goto error_sramclean;
420 bcom_eng->regs_base = res_bcom.start;
421 bcom_eng->regs = ioremap(res_bcom.start, sizeof(struct mpc52xx_sdma));
422 if (!bcom_eng->regs) {
423 printk(KERN_ERR DRIVER_NAME ": "
424 "Can't map registers\n");
425 rv = -ENOMEM;
426 goto error_release;
429 /* Now, do the real init */
430 rv = bcom_engine_init();
431 if (rv)
432 goto error_unmap;
434 /* Done ! */
435 printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",
436 bcom_eng->regs_base);
438 return 0;
440 /* Error path */
441 error_unmap:
442 iounmap(bcom_eng->regs);
443 error_release:
444 release_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma));
445 error_sramclean:
446 kfree(bcom_eng);
447 bcom_sram_cleanup();
448 error_ofput:
449 of_node_put(op->node);
451 printk(KERN_ERR "DMA: MPC52xx BestComm init failed !\n");
453 return rv;
457 static int
458 mpc52xx_bcom_remove(struct of_device *op)
460 /* Clean up the engine */
461 bcom_engine_cleanup();
463 /* Cleanup SRAM */
464 bcom_sram_cleanup();
466 /* Release regs */
467 iounmap(bcom_eng->regs);
468 release_mem_region(bcom_eng->regs_base, sizeof(struct mpc52xx_sdma));
470 /* Release the node */
471 of_node_put(bcom_eng->ofnode);
473 /* Release memory */
474 kfree(bcom_eng);
475 bcom_eng = NULL;
477 return 0;
480 static struct of_device_id mpc52xx_bcom_of_match[] = {
482 .type = "dma-controller",
483 .compatible = "mpc5200-bestcomm",
488 MODULE_DEVICE_TABLE(of, mpc52xx_bcom_of_match);
491 static struct of_platform_driver mpc52xx_bcom_of_platform_driver = {
492 .owner = THIS_MODULE,
493 .name = DRIVER_NAME,
494 .match_table = mpc52xx_bcom_of_match,
495 .probe = mpc52xx_bcom_probe,
496 .remove = mpc52xx_bcom_remove,
497 .driver = {
498 .name = DRIVER_NAME,
499 .owner = THIS_MODULE,
504 /* ======================================================================== */
505 /* Module */
506 /* ======================================================================== */
508 static int __init
509 mpc52xx_bcom_init(void)
511 return of_register_platform_driver(&mpc52xx_bcom_of_platform_driver);
514 static void __exit
515 mpc52xx_bcom_exit(void)
517 of_unregister_platform_driver(&mpc52xx_bcom_of_platform_driver);
520 /* If we're not a module, we must make sure everything is setup before */
521 /* anyone tries to use us ... that's why we use subsys_initcall instead */
522 /* of module_init. */
523 subsys_initcall(mpc52xx_bcom_init);
524 module_exit(mpc52xx_bcom_exit);
526 MODULE_DESCRIPTION("Freescale MPC52xx BestComm DMA");
527 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
528 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
529 MODULE_AUTHOR("Dale Farnsworth <dfarnsworth@mvista.com>");
530 MODULE_LICENSE("GPL v2");