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>
20 #include <linux/of_device.h>
21 #include <linux/of_platform.h>
24 #include <asm/mpc52xx.h>
27 #include "bestcomm_priv.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 /* ======================================================================== */
44 bcom_task_alloc(int bd_count, int bd_size, int priv_size)
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 */
59 spin_unlock(&bcom_eng->lock);
64 /* Allocate our structure */
65 tsk = kzalloc(sizeof(struct bcom_task) + priv_size, GFP_KERNEL);
69 tsk->tasknum = tasknum;
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)
78 /* Init the BDs, if needed */
80 tsk->cookie = kmalloc(sizeof(void*) * bd_count, GFP_KERNEL);
84 tsk->bd = bcom_sram_alloc(bd_count * bd_size, 4, &tsk->bd_pa);
87 memset(tsk->bd, 0x00, bd_count * bd_size);
89 tsk->num_bd = bd_count;
90 tsk->bd_size = bd_size;
97 if (tsk->irq != NO_IRQ)
98 irq_dispose_mapping(tsk->irq);
99 bcom_sram_free(tsk->bd);
104 bcom_eng->tdt[tasknum].stop = 0;
108 EXPORT_SYMBOL_GPL(bcom_task_alloc);
111 bcom_task_free(struct bcom_task *tsk)
114 bcom_disable_task(tsk->tasknum);
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);
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;
137 if (hdr->magic != BCOM_TASK_MAGIC) {
138 printk(KERN_ERR DRIVER_NAME
139 ": Trying to load invalid microcode\n");
143 if ((task < 0) || (task >= BCOM_MAX_TASKS)) {
144 printk(KERN_ERR DRIVER_NAME
145 ": Trying to load invalid task %d\n", task);
149 /* Initial load or reload */
150 tdt = &bcom_eng->tdt[task];
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",
160 bcom_task_num_descs(task));
164 phys_addr_t start_pa;
166 desc = bcom_sram_alloc(hdr->desc_size * sizeof(u32), 4, &start_pa);
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);
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));
191 EXPORT_SYMBOL_GPL(bcom_load_image);
194 bcom_set_initiator(int task, int initiator)
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))
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);
226 bcom_enable(struct bcom_task *tsk)
228 bcom_enable_task(tsk->tasknum);
230 EXPORT_SYMBOL_GPL(bcom_enable);
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() */
267 bcom_engine_init(void)
270 phys_addr_t tdt_pa, ctx_pa, var_pa, fdt_pa;
271 unsigned int tdt_size, ctx_size, var_size, fdt_size;
273 /* Allocate & clear SRAM zones for FDT, TDTs, contexts and vars/incs */
274 tdt_size = BCOM_MAX_TASKS * sizeof(struct bcom_tdt);
275 ctx_size = BCOM_MAX_TASKS * BCOM_CTX_SIZE;
276 var_size = BCOM_MAX_TASKS * (BCOM_VAR_SIZE + BCOM_INC_SIZE);
277 fdt_size = BCOM_FDT_SIZE;
279 bcom_eng->tdt = bcom_sram_alloc(tdt_size, sizeof(u32), &tdt_pa);
280 bcom_eng->ctx = bcom_sram_alloc(ctx_size, BCOM_CTX_ALIGN, &ctx_pa);
281 bcom_eng->var = bcom_sram_alloc(var_size, BCOM_VAR_ALIGN, &var_pa);
282 bcom_eng->fdt = bcom_sram_alloc(fdt_size, BCOM_FDT_ALIGN, &fdt_pa);
284 if (!bcom_eng->tdt || !bcom_eng->ctx || !bcom_eng->var || !bcom_eng->fdt) {
285 printk(KERN_ERR "DMA: SRAM alloc failed in engine init !\n");
287 bcom_sram_free(bcom_eng->tdt);
288 bcom_sram_free(bcom_eng->ctx);
289 bcom_sram_free(bcom_eng->var);
290 bcom_sram_free(bcom_eng->fdt);
295 memset(bcom_eng->tdt, 0x00, tdt_size);
296 memset(bcom_eng->ctx, 0x00, ctx_size);
297 memset(bcom_eng->var, 0x00, var_size);
298 memset(bcom_eng->fdt, 0x00, fdt_size);
300 /* Copy the FDT for the EU#3 */
301 memcpy(&bcom_eng->fdt[48], fdt_ops, sizeof(fdt_ops));
303 /* Initialize Task base structure */
304 for (task=0; task<BCOM_MAX_TASKS; task++)
306 out_be16(&bcom_eng->regs->tcr[task], 0);
307 out_8(&bcom_eng->regs->ipr[task], 0);
309 bcom_eng->tdt[task].context = ctx_pa;
310 bcom_eng->tdt[task].var = var_pa;
311 bcom_eng->tdt[task].fdt = fdt_pa;
313 var_pa += BCOM_VAR_SIZE + BCOM_INC_SIZE;
314 ctx_pa += BCOM_CTX_SIZE;
317 out_be32(&bcom_eng->regs->taskBar, tdt_pa);
319 /* Init 'always' initiator */
320 out_8(&bcom_eng->regs->ipr[BCOM_INITIATOR_ALWAYS], BCOM_IPR_ALWAYS);
322 /* Disable COMM Bus Prefetch, apparently it's not reliable yet */
323 /* FIXME: This should be done on 5200 and not 5200B ... */
324 out_be16(&bcom_eng->regs->PtdCntrl, in_be16(&bcom_eng->regs->PtdCntrl) | 1);
327 spin_lock_init(&bcom_eng->lock);
333 bcom_engine_cleanup(void)
338 for (task=0; task<BCOM_MAX_TASKS; task++)
340 out_be16(&bcom_eng->regs->tcr[task], 0);
341 out_8(&bcom_eng->regs->ipr[task], 0);
344 out_be32(&bcom_eng->regs->taskBar, 0ul);
346 /* Release the SRAM zones */
347 bcom_sram_free(bcom_eng->tdt);
348 bcom_sram_free(bcom_eng->ctx);
349 bcom_sram_free(bcom_eng->var);
350 bcom_sram_free(bcom_eng->fdt);
354 /* ======================================================================== */
355 /* OF platform driver */
356 /* ======================================================================== */
359 mpc52xx_bcom_probe(struct of_device *op, const struct of_device_id *match)
361 struct device_node *ofn_sram;
362 struct resource res_bcom;
366 /* Inform user we're ok so far */
367 printk(KERN_INFO "DMA: MPC52xx BestComm driver\n");
369 /* Get the bestcomm node */
370 of_node_get(op->node);
373 ofn_sram = of_find_compatible_node(NULL, "sram", "mpc5200-sram");
375 printk(KERN_ERR DRIVER_NAME ": "
376 "No SRAM found in device tree\n");
380 rv = bcom_sram_init(ofn_sram, DRIVER_NAME);
381 of_node_put(ofn_sram);
384 printk(KERN_ERR DRIVER_NAME ": "
385 "Error in SRAM init\n");
389 /* Get a clean struct */
390 bcom_eng = kzalloc(sizeof(struct bcom_engine), GFP_KERNEL);
392 printk(KERN_ERR DRIVER_NAME ": "
393 "Can't allocate state structure\n");
395 goto error_sramclean;
399 bcom_eng->ofnode = op->node;
401 /* Get, reserve & map io */
402 if (of_address_to_resource(op->node, 0, &res_bcom)) {
403 printk(KERN_ERR DRIVER_NAME ": "
404 "Can't get resource\n");
406 goto error_sramclean;
409 if (!request_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma),
411 printk(KERN_ERR DRIVER_NAME ": "
412 "Can't request registers region\n");
414 goto error_sramclean;
417 bcom_eng->regs_base = res_bcom.start;
418 bcom_eng->regs = ioremap(res_bcom.start, sizeof(struct mpc52xx_sdma));
419 if (!bcom_eng->regs) {
420 printk(KERN_ERR DRIVER_NAME ": "
421 "Can't map registers\n");
426 /* Now, do the real init */
427 rv = bcom_engine_init();
432 printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",
433 bcom_eng->regs_base);
439 iounmap(bcom_eng->regs);
441 release_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma));
446 of_node_put(op->node);
448 printk(KERN_ERR "DMA: MPC52xx BestComm init failed !\n");
455 mpc52xx_bcom_remove(struct of_device *op)
457 /* Clean up the engine */
458 bcom_engine_cleanup();
464 iounmap(bcom_eng->regs);
465 release_mem_region(bcom_eng->regs_base, sizeof(struct mpc52xx_sdma));
467 /* Release the node */
468 of_node_put(bcom_eng->ofnode);
477 static struct of_device_id mpc52xx_bcom_of_match[] = {
479 .type = "dma-controller",
480 .compatible = "mpc5200-bestcomm",
485 MODULE_DEVICE_TABLE(of, mpc52xx_bcom_of_match);
488 static struct of_platform_driver mpc52xx_bcom_of_platform_driver = {
489 .owner = THIS_MODULE,
491 .match_table = mpc52xx_bcom_of_match,
492 .probe = mpc52xx_bcom_probe,
493 .remove = mpc52xx_bcom_remove,
496 .owner = THIS_MODULE,
501 /* ======================================================================== */
503 /* ======================================================================== */
506 mpc52xx_bcom_init(void)
508 return of_register_platform_driver(&mpc52xx_bcom_of_platform_driver);
512 mpc52xx_bcom_exit(void)
514 of_unregister_platform_driver(&mpc52xx_bcom_of_platform_driver);
517 /* If we're not a module, we must make sure everything is setup before */
518 /* anyone tries to use us ... that's why we use subsys_initcall instead */
519 /* of module_init. */
520 subsys_initcall(mpc52xx_bcom_init);
521 module_exit(mpc52xx_bcom_exit);
523 MODULE_DESCRIPTION("Freescale MPC52xx BestComm DMA");
524 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
525 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
526 MODULE_AUTHOR("Dale Farnsworth <dfarnsworth@mvista.com>");
527 MODULE_LICENSE("GPL v2");