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_address.h>
21 #include <linux/of_device.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
26 #include <asm/mpc52xx.h>
28 #include <linux/fsl/bestcomm/sram.h>
29 #include <linux/fsl/bestcomm/bestcomm_priv.h>
30 #include "linux/fsl/bestcomm/bestcomm.h"
32 #define DRIVER_NAME "bestcomm-core"
34 /* MPC5200 device tree match tables */
35 static const struct of_device_id mpc52xx_sram_ids[] = {
36 { .compatible = "fsl,mpc5200-sram", },
37 { .compatible = "mpc5200-sram", },
42 struct bcom_engine *bcom_eng = NULL;
43 EXPORT_SYMBOL_GPL(bcom_eng); /* needed for inline functions */
45 /* ======================================================================== */
46 /* Public and private API */
47 /* ======================================================================== */
52 bcom_task_alloc(int bd_count, int bd_size, int priv_size)
55 struct bcom_task *tsk;
57 /* Don't try to do anything if bestcomm init failed */
61 /* Get and reserve a task num */
62 spin_lock(&bcom_eng->lock);
64 for (i=0; i<BCOM_MAX_TASKS; i++)
65 if (!bcom_eng->tdt[i].stop) { /* we use stop as a marker */
66 bcom_eng->tdt[i].stop = 0xfffffffful; /* dummy addr */
71 spin_unlock(&bcom_eng->lock);
76 /* Allocate our structure */
77 tsk = kzalloc(sizeof(struct bcom_task) + priv_size, GFP_KERNEL);
81 tsk->tasknum = tasknum;
83 tsk->priv = (void*)tsk + sizeof(struct bcom_task);
85 /* Get IRQ of that task */
86 tsk->irq = irq_of_parse_and_map(bcom_eng->ofnode, tsk->tasknum);
90 /* Init the BDs, if needed */
92 tsk->cookie = kmalloc_array(bd_count, sizeof(void *),
97 tsk->bd = bcom_sram_alloc(bd_count * bd_size, 4, &tsk->bd_pa);
100 memset_io(tsk->bd, 0x00, bd_count * bd_size);
102 tsk->num_bd = bd_count;
103 tsk->bd_size = bd_size;
111 irq_dispose_mapping(tsk->irq);
112 bcom_sram_free(tsk->bd);
117 bcom_eng->tdt[tasknum].stop = 0;
121 EXPORT_SYMBOL_GPL(bcom_task_alloc);
124 bcom_task_free(struct bcom_task *tsk)
127 bcom_disable_task(tsk->tasknum);
130 bcom_eng->tdt[tsk->tasknum].start = 0;
131 bcom_eng->tdt[tsk->tasknum].stop = 0;
133 /* Free everything */
134 irq_dispose_mapping(tsk->irq);
135 bcom_sram_free(tsk->bd);
139 EXPORT_SYMBOL_GPL(bcom_task_free);
142 bcom_load_image(int task, u32 *task_image)
144 struct bcom_task_header *hdr = (struct bcom_task_header *)task_image;
145 struct bcom_tdt *tdt;
146 u32 *desc, *var, *inc;
147 u32 *desc_src, *var_src, *inc_src;
150 if (hdr->magic != BCOM_TASK_MAGIC) {
151 printk(KERN_ERR DRIVER_NAME
152 ": Trying to load invalid microcode\n");
156 if ((task < 0) || (task >= BCOM_MAX_TASKS)) {
157 printk(KERN_ERR DRIVER_NAME
158 ": Trying to load invalid task %d\n", task);
162 /* Initial load or reload */
163 tdt = &bcom_eng->tdt[task];
166 desc = bcom_task_desc(task);
167 if (hdr->desc_size != bcom_task_num_descs(task)) {
168 printk(KERN_ERR DRIVER_NAME
169 ": Trying to reload wrong task image "
170 "(%d size %d/%d)!\n",
173 bcom_task_num_descs(task));
177 phys_addr_t start_pa;
179 desc = bcom_sram_alloc(hdr->desc_size * sizeof(u32), 4, &start_pa);
183 tdt->start = start_pa;
184 tdt->stop = start_pa + ((hdr->desc_size-1) * sizeof(u32));
187 var = bcom_task_var(task);
188 inc = bcom_task_inc(task);
191 memset_io(var, 0x00, BCOM_VAR_SIZE);
192 memset_io(inc, 0x00, BCOM_INC_SIZE);
194 desc_src = (u32 *)(hdr + 1);
195 var_src = desc_src + hdr->desc_size;
196 inc_src = var_src + hdr->var_size;
198 memcpy_toio(desc, desc_src, hdr->desc_size * sizeof(u32));
199 memcpy_toio(var + hdr->first_var, var_src, hdr->var_size * sizeof(u32));
200 memcpy_toio(inc, inc_src, hdr->inc_size * sizeof(u32));
204 EXPORT_SYMBOL_GPL(bcom_load_image);
207 bcom_set_initiator(int task, int initiator)
212 int next_drd_has_initiator;
214 bcom_set_tcr_initiator(task, initiator);
216 /* Just setting tcr is apparently not enough due to some problem */
217 /* with it. So we just go thru all the microcode and replace in */
218 /* the DRD directly */
220 desc = bcom_task_desc(task);
221 next_drd_has_initiator = 1;
222 num_descs = bcom_task_num_descs(task);
224 for (i=0; i<num_descs; i++, desc++) {
225 if (!bcom_desc_is_drd(*desc))
227 if (next_drd_has_initiator)
228 if (bcom_desc_initiator(*desc) != BCOM_INITIATOR_ALWAYS)
229 bcom_set_desc_initiator(desc, initiator);
230 next_drd_has_initiator = !bcom_drd_is_extended(*desc);
233 EXPORT_SYMBOL_GPL(bcom_set_initiator);
239 bcom_enable(struct bcom_task *tsk)
241 bcom_enable_task(tsk->tasknum);
243 EXPORT_SYMBOL_GPL(bcom_enable);
246 bcom_disable(struct bcom_task *tsk)
248 bcom_disable_task(tsk->tasknum);
250 EXPORT_SYMBOL_GPL(bcom_disable);
253 /* ======================================================================== */
254 /* Engine init/cleanup */
255 /* ======================================================================== */
257 /* Function Descriptor table */
258 /* this will need to be updated if Freescale changes their task code FDT */
259 static u32 fdt_ops[] = {
260 0xa0045670, /* FDT[48] - load_acc() */
261 0x80045670, /* FDT[49] - unload_acc() */
262 0x21800000, /* FDT[50] - and() */
263 0x21e00000, /* FDT[51] - or() */
264 0x21500000, /* FDT[52] - xor() */
265 0x21400000, /* FDT[53] - andn() */
266 0x21500000, /* FDT[54] - not() */
267 0x20400000, /* FDT[55] - add() */
268 0x20500000, /* FDT[56] - sub() */
269 0x20800000, /* FDT[57] - lsh() */
270 0x20a00000, /* FDT[58] - rsh() */
271 0xc0170000, /* FDT[59] - crc8() */
272 0xc0145670, /* FDT[60] - crc16() */
273 0xc0345670, /* FDT[61] - crc32() */
274 0xa0076540, /* FDT[62] - endian32() */
275 0xa0000760, /* FDT[63] - endian16() */
279 static int bcom_engine_init(void)
282 phys_addr_t tdt_pa, ctx_pa, var_pa, fdt_pa;
283 unsigned int tdt_size, ctx_size, var_size, fdt_size;
285 /* Allocate & clear SRAM zones for FDT, TDTs, contexts and vars/incs */
286 tdt_size = BCOM_MAX_TASKS * sizeof(struct bcom_tdt);
287 ctx_size = BCOM_MAX_TASKS * BCOM_CTX_SIZE;
288 var_size = BCOM_MAX_TASKS * (BCOM_VAR_SIZE + BCOM_INC_SIZE);
289 fdt_size = BCOM_FDT_SIZE;
291 bcom_eng->tdt = bcom_sram_alloc(tdt_size, sizeof(u32), &tdt_pa);
292 bcom_eng->ctx = bcom_sram_alloc(ctx_size, BCOM_CTX_ALIGN, &ctx_pa);
293 bcom_eng->var = bcom_sram_alloc(var_size, BCOM_VAR_ALIGN, &var_pa);
294 bcom_eng->fdt = bcom_sram_alloc(fdt_size, BCOM_FDT_ALIGN, &fdt_pa);
296 if (!bcom_eng->tdt || !bcom_eng->ctx || !bcom_eng->var || !bcom_eng->fdt) {
297 printk(KERN_ERR "DMA: SRAM alloc failed in engine init !\n");
299 bcom_sram_free(bcom_eng->tdt);
300 bcom_sram_free(bcom_eng->ctx);
301 bcom_sram_free(bcom_eng->var);
302 bcom_sram_free(bcom_eng->fdt);
307 memset_io(bcom_eng->tdt, 0x00, tdt_size);
308 memset_io(bcom_eng->ctx, 0x00, ctx_size);
309 memset_io(bcom_eng->var, 0x00, var_size);
310 memset_io(bcom_eng->fdt, 0x00, fdt_size);
312 /* Copy the FDT for the EU#3 */
313 memcpy_toio(&bcom_eng->fdt[48], fdt_ops, sizeof(fdt_ops));
315 /* Initialize Task base structure */
316 for (task=0; task<BCOM_MAX_TASKS; task++)
318 out_be16(&bcom_eng->regs->tcr[task], 0);
319 out_8(&bcom_eng->regs->ipr[task], 0);
321 bcom_eng->tdt[task].context = ctx_pa;
322 bcom_eng->tdt[task].var = var_pa;
323 bcom_eng->tdt[task].fdt = fdt_pa;
325 var_pa += BCOM_VAR_SIZE + BCOM_INC_SIZE;
326 ctx_pa += BCOM_CTX_SIZE;
329 out_be32(&bcom_eng->regs->taskBar, tdt_pa);
331 /* Init 'always' initiator */
332 out_8(&bcom_eng->regs->ipr[BCOM_INITIATOR_ALWAYS], BCOM_IPR_ALWAYS);
334 /* Disable COMM Bus Prefetch on the original 5200; it's broken */
335 if ((mfspr(SPRN_SVR) & MPC5200_SVR_MASK) == MPC5200_SVR)
336 bcom_disable_prefetch();
339 spin_lock_init(&bcom_eng->lock);
345 bcom_engine_cleanup(void)
350 for (task=0; task<BCOM_MAX_TASKS; task++)
352 out_be16(&bcom_eng->regs->tcr[task], 0);
353 out_8(&bcom_eng->regs->ipr[task], 0);
356 out_be32(&bcom_eng->regs->taskBar, 0ul);
358 /* Release the SRAM zones */
359 bcom_sram_free(bcom_eng->tdt);
360 bcom_sram_free(bcom_eng->ctx);
361 bcom_sram_free(bcom_eng->var);
362 bcom_sram_free(bcom_eng->fdt);
366 /* ======================================================================== */
367 /* OF platform driver */
368 /* ======================================================================== */
370 static int mpc52xx_bcom_probe(struct platform_device *op)
372 struct device_node *ofn_sram;
373 struct resource res_bcom;
377 /* Inform user we're ok so far */
378 printk(KERN_INFO "DMA: MPC52xx BestComm driver\n");
380 /* Get the bestcomm node */
381 of_node_get(op->dev.of_node);
384 ofn_sram = of_find_matching_node(NULL, mpc52xx_sram_ids);
386 printk(KERN_ERR DRIVER_NAME ": "
387 "No SRAM found in device tree\n");
391 rv = bcom_sram_init(ofn_sram, DRIVER_NAME);
392 of_node_put(ofn_sram);
395 printk(KERN_ERR DRIVER_NAME ": "
396 "Error in SRAM init\n");
400 /* Get a clean struct */
401 bcom_eng = kzalloc(sizeof(struct bcom_engine), GFP_KERNEL);
404 goto error_sramclean;
408 bcom_eng->ofnode = op->dev.of_node;
410 /* Get, reserve & map io */
411 if (of_address_to_resource(op->dev.of_node, 0, &res_bcom)) {
412 printk(KERN_ERR DRIVER_NAME ": "
413 "Can't get resource\n");
415 goto error_sramclean;
418 if (!request_mem_region(res_bcom.start, resource_size(&res_bcom),
420 printk(KERN_ERR DRIVER_NAME ": "
421 "Can't request registers region\n");
423 goto error_sramclean;
426 bcom_eng->regs_base = res_bcom.start;
427 bcom_eng->regs = ioremap(res_bcom.start, sizeof(struct mpc52xx_sdma));
428 if (!bcom_eng->regs) {
429 printk(KERN_ERR DRIVER_NAME ": "
430 "Can't map registers\n");
435 /* Now, do the real init */
436 rv = bcom_engine_init();
441 printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",
442 (long)bcom_eng->regs_base);
448 iounmap(bcom_eng->regs);
450 release_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma));
455 of_node_put(op->dev.of_node);
457 printk(KERN_ERR "DMA: MPC52xx BestComm init failed !\n");
463 static int mpc52xx_bcom_remove(struct platform_device *op)
465 /* Clean up the engine */
466 bcom_engine_cleanup();
472 iounmap(bcom_eng->regs);
473 release_mem_region(bcom_eng->regs_base, sizeof(struct mpc52xx_sdma));
475 /* Release the node */
476 of_node_put(bcom_eng->ofnode);
485 static const struct of_device_id mpc52xx_bcom_of_match[] = {
486 { .compatible = "fsl,mpc5200-bestcomm", },
487 { .compatible = "mpc5200-bestcomm", },
491 MODULE_DEVICE_TABLE(of, mpc52xx_bcom_of_match);
494 static struct platform_driver mpc52xx_bcom_of_platform_driver = {
495 .probe = mpc52xx_bcom_probe,
496 .remove = mpc52xx_bcom_remove,
499 .of_match_table = mpc52xx_bcom_of_match,
504 /* ======================================================================== */
506 /* ======================================================================== */
509 mpc52xx_bcom_init(void)
511 return platform_driver_register(&mpc52xx_bcom_of_platform_driver);
515 mpc52xx_bcom_exit(void)
517 platform_driver_unregister(&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");