]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/powerpc/sysdev/bestcomm/bestcomm.c
[POWERPC] bestcomm: core bestcomm support for Freescale MPC5200
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / sysdev / bestcomm / bestcomm.c
CommitLineData
2f9ea1bd
SM
1/*
2 * Driver for MPC52xx processor BestComm peripheral controller
3 *
4 *
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> )
10 *
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.
14 */
15
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>
25
26#include "sram.h"
27#include "bestcomm_priv.h"
28#include "bestcomm.h"
29
30#define DRIVER_NAME "bestcomm-core"
31
32
33struct bcom_engine *bcom_eng = NULL;
34EXPORT_SYMBOL_GPL(bcom_eng); /* needed for inline functions */
35
36
37/* ======================================================================== */
38/* Public and private API */
39/* ======================================================================== */
40
41/* Private API */
42
43struct bcom_task *
44bcom_task_alloc(int bd_count, int bd_size, int priv_size)
45{
46 int i, tasknum = -1;
47 struct bcom_task *tsk;
48
49 /* Get and reserve a task num */
50 spin_lock(&bcom_eng->lock);
51
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;
57 }
58
59 spin_unlock(&bcom_eng->lock);
60
61 if (tasknum < 0)
62 return NULL;
63
64 /* Allocate our structure */
65 tsk = kzalloc(sizeof(struct bcom_task) + priv_size, GFP_KERNEL);
66 if (!tsk)
67 goto error;
68
69 tsk->tasknum = tasknum;
70 if (priv_size)
71 tsk->priv = (void*)tsk + sizeof(struct bcom_task);
72
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;
77
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;
83
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);
88
89 tsk->num_bd = bd_count;
90 tsk->bd_size = bd_size;
91 }
92
93 return tsk;
94
95error:
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);
102 }
103
104 bcom_eng->tdt[tasknum].stop = 0;
105
106 return NULL;
107}
108EXPORT_SYMBOL_GPL(bcom_task_alloc);
109
110void
111bcom_task_free(struct bcom_task *tsk)
112{
113 /* Stop the task */
114 bcom_disable_task(tsk->tasknum);
115
116 /* Clear TDT */
117 bcom_eng->tdt[tsk->tasknum].start = 0;
118 bcom_eng->tdt[tsk->tasknum].stop = 0;
119
120 /* Free everything */
121 irq_dispose_mapping(tsk->irq);
122 bcom_sram_free(tsk->bd);
123 kfree(tsk->cookie);
124 kfree(tsk);
125}
126EXPORT_SYMBOL_GPL(bcom_task_free);
127
128int
129bcom_load_image(int task, u32 *task_image)
130{
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;
135
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;
141 }
142
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;
147 }
148
149 /* Initial load or reload */
150 tdt = &bcom_eng->tdt[task];
151
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;
162 }
163 } else {
164 phys_addr_t start_pa;
165
166 desc = bcom_sram_alloc(hdr->desc_size * sizeof(u32), 4, &start_pa);
167 if (!desc)
168 return -ENOMEM;
169
170 tdt->start = start_pa;
171 tdt->stop = start_pa + ((hdr->desc_size-1) * sizeof(u32));
172 }
173
174 var = bcom_task_var(task);
175 inc = bcom_task_inc(task);
176
177 /* Clear & copy */
178 memset(var, 0x00, BCOM_VAR_SIZE);
179 memset(inc, 0x00, BCOM_INC_SIZE);
180
181 desc_src = (u32 *)(hdr + 1);
182 var_src = desc_src + hdr->desc_size;
183 inc_src = var_src + hdr->var_size;
184
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));
188
189 return 0;
190}
191EXPORT_SYMBOL_GPL(bcom_load_image);
192
193void
194bcom_set_initiator(int task, int initiator)
195{
196 int i;
197 int num_descs;
198 u32 *desc;
199 int next_drd_has_initiator;
200
201 bcom_set_tcr_initiator(task, initiator);
202
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 */
206
207 desc = bcom_task_desc(task);
208 next_drd_has_initiator = 1;
209 num_descs = bcom_task_num_descs(task);
210
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);
218 }
219}
220EXPORT_SYMBOL_GPL(bcom_set_initiator);
221
222
223/* Public API */
224
225void
226bcom_enable(struct bcom_task *tsk)
227{
228 bcom_enable_task(tsk->tasknum);
229}
230EXPORT_SYMBOL_GPL(bcom_enable);
231
232void
233bcom_disable(struct bcom_task *tsk)
234{
235 bcom_disable_task(tsk->tasknum);
236}
237EXPORT_SYMBOL_GPL(bcom_disable);
238
239
240/* ======================================================================== */
241/* Engine init/cleanup */
242/* ======================================================================== */
243
244/* Function Descriptor table */
245/* this will need to be updated if Freescale changes their task code FDT */
246static 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() */
263};
264
265
266static int __devinit
267bcom_engine_init(void)
268{
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
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;
278
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);
283
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");
286
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);
291
292 return -ENOMEM;
293 }
294
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);
299
300 /* Copy the FDT for the EU#3 */
301 memcpy(&bcom_eng->fdt[48], fdt_ops, sizeof(fdt_ops));
302
303 /* Initialize Task base structure */
304 for (task=0; task<BCOM_MAX_TASKS; task++)
305 {
306 out_be16(&bcom_eng->regs->tcr[task], 0);
307 out_8(&bcom_eng->regs->ipr[task], 0);
308
309 bcom_eng->tdt[task].context = ctx_pa;
310 bcom_eng->tdt[task].var = var_pa;
311 bcom_eng->tdt[task].fdt = fdt_pa;
312
313 var_pa += BCOM_VAR_SIZE + BCOM_INC_SIZE;
314 ctx_pa += BCOM_CTX_SIZE;
315 }
316
317 out_be32(&bcom_eng->regs->taskBar, tdt_pa);
318
319 /* Init 'always' initiator */
320 out_8(&bcom_eng->regs->ipr[BCOM_INITIATOR_ALWAYS], BCOM_IPR_ALWAYS);
321
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);
325
326 /* Init lock */
327 spin_lock_init(&bcom_eng->lock);
328
329 return 0;
330}
331
332static void
333bcom_engine_cleanup(void)
334{
335 int task;
336
337 /* Stop all tasks */
338 for (task=0; task<BCOM_MAX_TASKS; task++)
339 {
340 out_be16(&bcom_eng->regs->tcr[task], 0);
341 out_8(&bcom_eng->regs->ipr[task], 0);
342 }
343
344 out_be32(&bcom_eng->regs->taskBar, 0ul);
345
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);
351}
352
353
354/* ======================================================================== */
355/* OF platform driver */
356/* ======================================================================== */
357
358static int __devinit
359mpc52xx_bcom_probe(struct of_device *op, const struct of_device_id *match)
360{
361 struct device_node *ofn_sram;
362 struct resource res_bcom;
363
364 int rv;
365
366 /* Inform user we're ok so far */
367 printk(KERN_INFO "DMA: MPC52xx BestComm driver\n");
368
369 /* Get the bestcomm node */
370 of_node_get(op->node);
371
372 /* Prepare SRAM */
373 ofn_sram = of_find_compatible_node(NULL, "sram", "mpc5200-sram");
374 if (!ofn_sram) {
375 printk(KERN_ERR DRIVER_NAME ": "
376 "No SRAM found in device tree\n");
377 rv = -ENODEV;
378 goto error_ofput;
379 }
380 rv = bcom_sram_init(ofn_sram, DRIVER_NAME);
381 of_node_put(ofn_sram);
382
383 if (rv) {
384 printk(KERN_ERR DRIVER_NAME ": "
385 "Error in SRAM init\n");
386 goto error_ofput;
387 }
388
389 /* Get a clean struct */
390 bcom_eng = kzalloc(sizeof(struct bcom_engine), GFP_KERNEL);
391 if (!bcom_eng) {
392 printk(KERN_ERR DRIVER_NAME ": "
393 "Can't allocate state structure\n");
394 rv = -ENOMEM;
395 goto error_sramclean;
396 }
397
398 /* Save the node */
399 bcom_eng->ofnode = op->node;
400
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");
405 rv = -EINVAL;
406 goto error_sramclean;
407 }
408
409 if (!request_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma),
410 DRIVER_NAME)) {
411 printk(KERN_ERR DRIVER_NAME ": "
412 "Can't request registers region\n");
413 rv = -EBUSY;
414 goto error_sramclean;
415 }
416
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");
422 rv = -ENOMEM;
423 goto error_release;
424 }
425
426 /* Now, do the real init */
427 rv = bcom_engine_init();
428 if (rv)
429 goto error_unmap;
430
431 /* Done ! */
432 printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",
433 bcom_eng->regs_base);
434
435 return 0;
436
437 /* Error path */
438error_unmap:
439 iounmap(bcom_eng->regs);
440error_release:
441 release_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma));
442error_sramclean:
443 kfree(bcom_eng);
444 bcom_sram_cleanup();
445error_ofput:
446 of_node_put(op->node);
447
448 printk(KERN_ERR "DMA: MPC52xx BestComm init failed !\n");
449
450 return rv;
451}
452
453
454static int
455mpc52xx_bcom_remove(struct of_device *op)
456{
457 /* Clean up the engine */
458 bcom_engine_cleanup();
459
460 /* Cleanup SRAM */
461 bcom_sram_cleanup();
462
463 /* Release regs */
464 iounmap(bcom_eng->regs);
465 release_mem_region(bcom_eng->regs_base, sizeof(struct mpc52xx_sdma));
466
467 /* Release the node */
468 of_node_put(bcom_eng->ofnode);
469
470 /* Release memory */
471 kfree(bcom_eng);
472 bcom_eng = NULL;
473
474 return 0;
475}
476
477static struct of_device_id mpc52xx_bcom_of_match[] = {
478 {
479 .type = "dma-controller",
480 .compatible = "mpc5200-bestcomm",
481 },
482 {},
483};
484
485MODULE_DEVICE_TABLE(of, mpc52xx_bcom_of_match);
486
487
488static struct of_platform_driver mpc52xx_bcom_of_platform_driver = {
489 .owner = THIS_MODULE,
490 .name = DRIVER_NAME,
491 .match_table = mpc52xx_bcom_of_match,
492 .probe = mpc52xx_bcom_probe,
493 .remove = mpc52xx_bcom_remove,
494 .driver = {
495 .name = DRIVER_NAME,
496 .owner = THIS_MODULE,
497 },
498};
499
500
501/* ======================================================================== */
502/* Module */
503/* ======================================================================== */
504
505static int __init
506mpc52xx_bcom_init(void)
507{
508 return of_register_platform_driver(&mpc52xx_bcom_of_platform_driver);
509}
510
511static void __exit
512mpc52xx_bcom_exit(void)
513{
514 of_unregister_platform_driver(&mpc52xx_bcom_of_platform_driver);
515}
516
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. */
520subsys_initcall(mpc52xx_bcom_init);
521module_exit(mpc52xx_bcom_exit);
522
523MODULE_DESCRIPTION("Freescale MPC52xx BestComm DMA");
524MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
525MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
526MODULE_AUTHOR("Dale Farnsworth <dfarnsworth@mvista.com>");
527MODULE_LICENSE("GPL v2");
528