blob: ebf35d3053217af539173560f62c9c02ef42abbc [file] [log] [blame]
Martyn Welch658bcda2016-07-07 19:51:52 +01001/*
2 * Fake VME bridge support.
3 *
4 * This drive provides a fake VME bridge chip, this enables debugging of the
5 * VME framework in the absence of a VME system.
6 *
7 * This driver has to do a number of things in software that would be driven
8 * by hardware if it was available, it will also result in extra overhead at
9 * times when compared with driving actual hardware.
10 *
11 * Author: Martyn Welch <martyn@welches.me.uk>
12 * Copyright (c) 2014 Martyn Welch
13 *
14 * Based on vme_tsi148.c:
15 *
16 * Author: Martyn Welch <martyn.welch@ge.com>
17 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
18 *
19 * Based on work by Tom Armistead and Ajit Prem
20 * Copyright 2004 Motorola Inc.
21 *
22 * This program is free software; you can redistribute it and/or modify it
23 * under the terms of the GNU General Public License as published by the
24 * Free Software Foundation; either version 2 of the License, or (at your
25 * option) any later version.
26 */
27
28#include <linux/device.h>
29#include <linux/errno.h>
30#include <linux/interrupt.h>
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/types.h>
36#include <linux/vme.h>
37
38#include "../vme_bridge.h"
39
40/*
41 * Define the number of each that the fake driver supports.
42 */
43#define FAKE_MAX_MASTER 8 /* Max Master Windows */
44#define FAKE_MAX_SLAVE 8 /* Max Slave Windows */
45
46/* Structures to hold information normally held in device registers */
47struct fake_slave_window {
48 int enabled;
49 unsigned long long vme_base;
50 unsigned long long size;
Arnd Bergmann4b7b9482016-09-06 14:59:41 +020051 void *buf_base;
Martyn Welch658bcda2016-07-07 19:51:52 +010052 u32 aspace;
53 u32 cycle;
54};
55
56struct fake_master_window {
57 int enabled;
58 unsigned long long vme_base;
59 unsigned long long size;
60 u32 aspace;
61 u32 cycle;
62 u32 dwidth;
63};
64
65/* Structure used to hold driver specific information */
66struct fake_driver {
67 struct vme_bridge *parent;
68 struct fake_slave_window slaves[FAKE_MAX_SLAVE];
69 struct fake_master_window masters[FAKE_MAX_MASTER];
70 u32 lm_enabled;
71 unsigned long long lm_base;
72 u32 lm_aspace;
73 u32 lm_cycle;
74 void (*lm_callback[4])(void *);
75 void *lm_data[4];
76 struct tasklet_struct int_tasklet;
77 int int_level;
78 int int_statid;
79 void *crcsr_kernel;
80 dma_addr_t crcsr_bus;
81 /* Only one VME interrupt can be generated at a time, provide locking */
82 struct mutex vme_int;
83};
84
85/* Module parameter */
86static int geoid;
87
88static const char driver_name[] = "vme_fake";
89
90static struct vme_bridge *exit_pointer;
91
92static struct device *vme_root;
93
94/*
95 * Calling VME bus interrupt callback if provided.
96 */
97static void fake_VIRQ_tasklet(unsigned long data)
98{
99 struct vme_bridge *fake_bridge;
100 struct fake_driver *bridge;
101
102 fake_bridge = (struct vme_bridge *) data;
103 bridge = fake_bridge->driver_priv;
104
105 vme_irq_handler(fake_bridge, bridge->int_level, bridge->int_statid);
106}
107
108/*
109 * Configure VME interrupt
110 */
111static void fake_irq_set(struct vme_bridge *fake_bridge, int level,
112 int state, int sync)
113{
114 /* Nothing to do */
115}
116
Arnd Bergmann4b7b9482016-09-06 14:59:41 +0200117static void *fake_pci_to_ptr(dma_addr_t addr)
118{
119 return (void *)(uintptr_t)addr;
120}
121
122static dma_addr_t fake_ptr_to_pci(void *addr)
123{
124 return (dma_addr_t)(uintptr_t)addr;
125}
126
Martyn Welch658bcda2016-07-07 19:51:52 +0100127/*
128 * Generate a VME bus interrupt at the requested level & vector. Wait for
129 * interrupt to be acked.
130 */
131static int fake_irq_generate(struct vme_bridge *fake_bridge, int level,
132 int statid)
133{
134 struct fake_driver *bridge;
135
136 bridge = fake_bridge->driver_priv;
137
138 mutex_lock(&bridge->vme_int);
139
140 bridge->int_level = level;
141
142 bridge->int_statid = statid;
143
144 /*
145 * Schedule tasklet to run VME handler to emulate normal VME interrupt
146 * handler behaviour.
147 */
148 tasklet_schedule(&bridge->int_tasklet);
149
150 mutex_unlock(&bridge->vme_int);
151
152 return 0;
153}
154
155/*
156 * Initialize a slave window with the requested attributes.
157 */
158static int fake_slave_set(struct vme_slave_resource *image, int enabled,
159 unsigned long long vme_base, unsigned long long size,
160 dma_addr_t buf_base, u32 aspace, u32 cycle)
161{
162 unsigned int i, granularity = 0;
163 unsigned long long vme_bound;
164 struct vme_bridge *fake_bridge;
165 struct fake_driver *bridge;
166
167 fake_bridge = image->parent;
168 bridge = fake_bridge->driver_priv;
169
170 i = image->number;
171
172 switch (aspace) {
173 case VME_A16:
174 granularity = 0x10;
175 break;
176 case VME_A24:
177 granularity = 0x1000;
178 break;
179 case VME_A32:
180 granularity = 0x10000;
181 break;
182 case VME_A64:
183 granularity = 0x10000;
184 break;
185 case VME_CRCSR:
186 case VME_USER1:
187 case VME_USER2:
188 case VME_USER3:
189 case VME_USER4:
190 default:
191 pr_err("Invalid address space\n");
192 return -EINVAL;
193 }
194
195 /*
196 * Bound address is a valid address for the window, adjust
197 * accordingly
198 */
199 vme_bound = vme_base + size - granularity;
200
201 if (vme_base & (granularity - 1)) {
202 pr_err("Invalid VME base alignment\n");
203 return -EINVAL;
204 }
205 if (vme_bound & (granularity - 1)) {
206 pr_err("Invalid VME bound alignment\n");
207 return -EINVAL;
208 }
209
210 mutex_lock(&image->mtx);
211
212 bridge->slaves[i].enabled = enabled;
213 bridge->slaves[i].vme_base = vme_base;
214 bridge->slaves[i].size = size;
Arnd Bergmann4b7b9482016-09-06 14:59:41 +0200215 bridge->slaves[i].buf_base = fake_pci_to_ptr(buf_base);
Martyn Welch658bcda2016-07-07 19:51:52 +0100216 bridge->slaves[i].aspace = aspace;
217 bridge->slaves[i].cycle = cycle;
218
219 mutex_unlock(&image->mtx);
220
221 return 0;
222}
223
224/*
225 * Get slave window configuration.
226 */
227static int fake_slave_get(struct vme_slave_resource *image, int *enabled,
228 unsigned long long *vme_base, unsigned long long *size,
229 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
230{
231 unsigned int i;
232 struct fake_driver *bridge;
233
234 bridge = image->parent->driver_priv;
235
236 i = image->number;
237
238 mutex_lock(&image->mtx);
239
240 *enabled = bridge->slaves[i].enabled;
241 *vme_base = bridge->slaves[i].vme_base;
242 *size = bridge->slaves[i].size;
Arnd Bergmann4b7b9482016-09-06 14:59:41 +0200243 *buf_base = fake_ptr_to_pci(bridge->slaves[i].buf_base);
Martyn Welch658bcda2016-07-07 19:51:52 +0100244 *aspace = bridge->slaves[i].aspace;
245 *cycle = bridge->slaves[i].cycle;
246
247 mutex_unlock(&image->mtx);
248
249 return 0;
250}
251
252/*
253 * Set the attributes of an outbound window.
254 */
255static int fake_master_set(struct vme_master_resource *image, int enabled,
256 unsigned long long vme_base, unsigned long long size,
257 u32 aspace, u32 cycle, u32 dwidth)
258{
259 int retval = 0;
260 unsigned int i;
261 struct vme_bridge *fake_bridge;
262 struct fake_driver *bridge;
263
264 fake_bridge = image->parent;
265
266 bridge = fake_bridge->driver_priv;
267
268 /* Verify input data */
269 if (vme_base & 0xFFFF) {
270 pr_err("Invalid VME Window alignment\n");
271 retval = -EINVAL;
272 goto err_window;
273 }
274
275 if (size & 0xFFFF) {
276 spin_unlock(&image->lock);
277 pr_err("Invalid size alignment\n");
278 retval = -EINVAL;
279 goto err_window;
280 }
281
282 if ((size == 0) && (enabled != 0)) {
283 pr_err("Size must be non-zero for enabled windows\n");
284 retval = -EINVAL;
285 goto err_window;
286 }
287
288 /* Setup data width */
289 switch (dwidth) {
290 case VME_D8:
291 case VME_D16:
292 case VME_D32:
293 break;
294 default:
295 spin_unlock(&image->lock);
296 pr_err("Invalid data width\n");
297 retval = -EINVAL;
298 goto err_dwidth;
299 }
300
301 /* Setup address space */
302 switch (aspace) {
303 case VME_A16:
304 case VME_A24:
305 case VME_A32:
306 case VME_A64:
307 case VME_CRCSR:
308 case VME_USER1:
309 case VME_USER2:
310 case VME_USER3:
311 case VME_USER4:
312 break;
313 default:
314 spin_unlock(&image->lock);
315 pr_err("Invalid address space\n");
316 retval = -EINVAL;
317 goto err_aspace;
318 }
319
320 spin_lock(&image->lock);
321
322 i = image->number;
323
324 bridge->masters[i].enabled = enabled;
325 bridge->masters[i].vme_base = vme_base;
326 bridge->masters[i].size = size;
327 bridge->masters[i].aspace = aspace;
328 bridge->masters[i].cycle = cycle;
329 bridge->masters[i].dwidth = dwidth;
330
331 spin_unlock(&image->lock);
332
333 return 0;
334
335err_aspace:
336err_dwidth:
337err_window:
338 return retval;
339
340}
341
342/*
343 * Set the attributes of an outbound window.
344 */
345static int __fake_master_get(struct vme_master_resource *image, int *enabled,
346 unsigned long long *vme_base, unsigned long long *size,
347 u32 *aspace, u32 *cycle, u32 *dwidth)
348{
349 unsigned int i;
350 struct fake_driver *bridge;
351
352 bridge = image->parent->driver_priv;
353
354 i = image->number;
355
356 *enabled = bridge->masters[i].enabled;
357 *vme_base = bridge->masters[i].vme_base;
358 *size = bridge->masters[i].size;
359 *aspace = bridge->masters[i].aspace;
360 *cycle = bridge->masters[i].cycle;
361 *dwidth = bridge->masters[i].dwidth;
362
363 return 0;
364}
365
366
367static int fake_master_get(struct vme_master_resource *image, int *enabled,
368 unsigned long long *vme_base, unsigned long long *size,
369 u32 *aspace, u32 *cycle, u32 *dwidth)
370{
371 int retval;
372
373 spin_lock(&image->lock);
374
375 retval = __fake_master_get(image, enabled, vme_base, size, aspace,
376 cycle, dwidth);
377
378 spin_unlock(&image->lock);
379
380 return retval;
381}
382
383
384void fake_lm_check(struct fake_driver *bridge, unsigned long long addr,
385 u32 aspace, u32 cycle)
386{
387 struct vme_bridge *fake_bridge;
388 unsigned long long lm_base;
389 u32 lm_aspace, lm_cycle;
390 int i;
391 struct vme_lm_resource *lm;
392 struct list_head *pos = NULL, *n;
393
394 /* Get vme_bridge */
395 fake_bridge = bridge->parent;
396
397 /* Loop through each location monitor resource */
398 list_for_each_safe(pos, n, &fake_bridge->lm_resources) {
399 lm = list_entry(pos, struct vme_lm_resource, list);
400
401 /* If disabled, we're done */
402 if (bridge->lm_enabled == 0)
403 return;
404
405 lm_base = bridge->lm_base;
406 lm_aspace = bridge->lm_aspace;
407 lm_cycle = bridge->lm_cycle;
408
409 /* First make sure that the cycle and address space match */
410 if ((lm_aspace == aspace) && (lm_cycle == cycle)) {
411 for (i = 0; i < lm->monitors; i++) {
412 /* Each location monitor covers 8 bytes */
413 if (((lm_base + (8 * i)) <= addr) &&
414 ((lm_base + (8 * i) + 8) > addr)) {
415 if (bridge->lm_callback[i] != NULL)
416 bridge->lm_callback[i](
417 bridge->lm_data[i]);
418 }
419 }
420 }
421 }
422}
423
424static u8 fake_vmeread8(struct fake_driver *bridge, unsigned long long addr,
425 u32 aspace, u32 cycle)
426{
427 u8 retval = 0xff;
428 int i;
429 unsigned long long start, end, offset;
430 u8 *loc;
431
432 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
433 start = bridge->slaves[i].vme_base;
434 end = bridge->slaves[i].vme_base + bridge->slaves[i].size;
435
436 if (aspace != bridge->slaves[i].aspace)
437 continue;
438
439 if (cycle != bridge->slaves[i].cycle)
440 continue;
441
442 if ((addr >= start) && (addr < end)) {
443 offset = addr - bridge->slaves[i].vme_base;
Arnd Bergmann4b7b9482016-09-06 14:59:41 +0200444 loc = (u8 *)(bridge->slaves[i].buf_base + offset);
Martyn Welch658bcda2016-07-07 19:51:52 +0100445 retval = *loc;
446
447 break;
448 }
449 }
450
451 fake_lm_check(bridge, addr, aspace, cycle);
452
453 return retval;
454}
455
456static u16 fake_vmeread16(struct fake_driver *bridge, unsigned long long addr,
457 u32 aspace, u32 cycle)
458{
459 u16 retval = 0xffff;
460 int i;
461 unsigned long long start, end, offset;
462 u16 *loc;
463
464 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
465 if (aspace != bridge->slaves[i].aspace)
466 continue;
467
468 if (cycle != bridge->slaves[i].cycle)
469 continue;
470
471 start = bridge->slaves[i].vme_base;
472 end = bridge->slaves[i].vme_base + bridge->slaves[i].size;
473
474 if ((addr >= start) && ((addr + 1) < end)) {
475 offset = addr - bridge->slaves[i].vme_base;
Arnd Bergmann4b7b9482016-09-06 14:59:41 +0200476 loc = (u16 *)(bridge->slaves[i].buf_base + offset);
Martyn Welch658bcda2016-07-07 19:51:52 +0100477 retval = *loc;
478
479 break;
480 }
481 }
482
483 fake_lm_check(bridge, addr, aspace, cycle);
484
485 return retval;
486}
487
488static u32 fake_vmeread32(struct fake_driver *bridge, unsigned long long addr,
489 u32 aspace, u32 cycle)
490{
491 u32 retval = 0xffffffff;
492 int i;
493 unsigned long long start, end, offset;
494 u32 *loc;
495
496 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
497 if (aspace != bridge->slaves[i].aspace)
498 continue;
499
500 if (cycle != bridge->slaves[i].cycle)
501 continue;
502
503 start = bridge->slaves[i].vme_base;
504 end = bridge->slaves[i].vme_base + bridge->slaves[i].size;
505
506 if ((addr >= start) && ((addr + 3) < end)) {
507 offset = addr - bridge->slaves[i].vme_base;
Arnd Bergmann4b7b9482016-09-06 14:59:41 +0200508 loc = (u32 *)(bridge->slaves[i].buf_base + offset);
Martyn Welch658bcda2016-07-07 19:51:52 +0100509 retval = *loc;
510
511 break;
512 }
513 }
514
515 fake_lm_check(bridge, addr, aspace, cycle);
516
517 return retval;
518}
519
520static ssize_t fake_master_read(struct vme_master_resource *image, void *buf,
521 size_t count, loff_t offset)
522{
523 int retval;
524 u32 aspace, cycle, dwidth;
525 struct vme_bridge *fake_bridge;
526 struct fake_driver *priv;
527 int i;
528 unsigned long long addr;
529 unsigned int done = 0;
530 unsigned int count32;
531
532 fake_bridge = image->parent;
533
534 priv = fake_bridge->driver_priv;
535
536 i = image->number;
537
538 addr = (unsigned long long)priv->masters[i].vme_base + offset;
539 aspace = priv->masters[i].aspace;
540 cycle = priv->masters[i].cycle;
541 dwidth = priv->masters[i].dwidth;
542
543 spin_lock(&image->lock);
544
545 /* The following code handles VME address alignment. We cannot use
546 * memcpy_xxx here because it may cut data transfers in to 8-bit
547 * cycles when D16 or D32 cycles are required on the VME bus.
548 * On the other hand, the bridge itself assures that the maximum data
549 * cycle configured for the transfer is used and splits it
550 * automatically for non-aligned addresses, so we don't want the
551 * overhead of needlessly forcing small transfers for the entire cycle.
552 */
553 if (addr & 0x1) {
554 *(u8 *)buf = fake_vmeread8(priv, addr, aspace, cycle);
555 done += 1;
556 if (done == count)
557 goto out;
558 }
559 if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
560 if ((addr + done) & 0x2) {
561 if ((count - done) < 2) {
562 *(u8 *)(buf + done) = fake_vmeread8(priv,
563 addr + done, aspace, cycle);
564 done += 1;
565 goto out;
566 } else {
567 *(u16 *)(buf + done) = fake_vmeread16(priv,
568 addr + done, aspace, cycle);
569 done += 2;
570 }
571 }
572 }
573
574 if (dwidth == VME_D32) {
575 count32 = (count - done) & ~0x3;
576 while (done < count32) {
577 *(u32 *)(buf + done) = fake_vmeread32(priv, addr + done,
578 aspace, cycle);
579 done += 4;
580 }
581 } else if (dwidth == VME_D16) {
582 count32 = (count - done) & ~0x3;
583 while (done < count32) {
584 *(u16 *)(buf + done) = fake_vmeread16(priv, addr + done,
585 aspace, cycle);
586 done += 2;
587 }
588 } else if (dwidth == VME_D8) {
589 count32 = (count - done);
590 while (done < count32) {
591 *(u8 *)(buf + done) = fake_vmeread8(priv, addr + done,
592 aspace, cycle);
593 done += 1;
594 }
595
596 }
597
598 if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
599 if ((count - done) & 0x2) {
600 *(u16 *)(buf + done) = fake_vmeread16(priv, addr + done,
601 aspace, cycle);
602 done += 2;
603 }
604 }
605 if ((count - done) & 0x1) {
606 *(u8 *)(buf + done) = fake_vmeread8(priv, addr + done, aspace,
607 cycle);
608 done += 1;
609 }
610
611out:
612 retval = count;
613
614 spin_unlock(&image->lock);
615
616 return retval;
617}
618
619void fake_vmewrite8(struct fake_driver *bridge, u8 *buf,
620 unsigned long long addr, u32 aspace, u32 cycle)
621{
622 int i;
623 unsigned long long start, end, offset;
624 u8 *loc;
625
626 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
627 if (aspace != bridge->slaves[i].aspace)
628 continue;
629
630 if (cycle != bridge->slaves[i].cycle)
631 continue;
632
633 start = bridge->slaves[i].vme_base;
634 end = bridge->slaves[i].vme_base + bridge->slaves[i].size;
635
636 if ((addr >= start) && (addr < end)) {
637 offset = addr - bridge->slaves[i].vme_base;
638 loc = (u8 *)((void *)bridge->slaves[i].buf_base + offset);
639 *loc = *buf;
640
641 break;
642 }
643 }
644
645 fake_lm_check(bridge, addr, aspace, cycle);
646
647}
648
649void fake_vmewrite16(struct fake_driver *bridge, u16 *buf,
650 unsigned long long addr, u32 aspace, u32 cycle)
651{
652 int i;
653 unsigned long long start, end, offset;
654 u16 *loc;
655
656 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
657 if (aspace != bridge->slaves[i].aspace)
658 continue;
659
660 if (cycle != bridge->slaves[i].cycle)
661 continue;
662
663 start = bridge->slaves[i].vme_base;
664 end = bridge->slaves[i].vme_base + bridge->slaves[i].size;
665
666 if ((addr >= start) && ((addr + 1) < end)) {
667 offset = addr - bridge->slaves[i].vme_base;
668 loc = (u16 *)((void *)bridge->slaves[i].buf_base + offset);
669 *loc = *buf;
670
671 break;
672 }
673 }
674
675 fake_lm_check(bridge, addr, aspace, cycle);
676
677}
678
679void fake_vmewrite32(struct fake_driver *bridge, u32 *buf,
680 unsigned long long addr, u32 aspace, u32 cycle)
681{
682 int i;
683 unsigned long long start, end, offset;
684 u32 *loc;
685
686 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
687 if (aspace != bridge->slaves[i].aspace)
688 continue;
689
690 if (cycle != bridge->slaves[i].cycle)
691 continue;
692
693 start = bridge->slaves[i].vme_base;
694 end = bridge->slaves[i].vme_base + bridge->slaves[i].size;
695
696 if ((addr >= start) && ((addr + 3) < end)) {
697 offset = addr - bridge->slaves[i].vme_base;
698 loc = (u32 *)((void *)bridge->slaves[i].buf_base + offset);
699 *loc = *buf;
700
701 break;
702 }
703 }
704
705 fake_lm_check(bridge, addr, aspace, cycle);
706
707}
708
709static ssize_t fake_master_write(struct vme_master_resource *image, void *buf,
710 size_t count, loff_t offset)
711{
712 int retval = 0;
713 u32 aspace, cycle, dwidth;
714 unsigned long long addr;
715 int i;
716 unsigned int done = 0;
717 unsigned int count32;
718
719 struct vme_bridge *fake_bridge;
720 struct fake_driver *bridge;
721
722 fake_bridge = image->parent;
723
724 bridge = fake_bridge->driver_priv;
725
726 i = image->number;
727
728 addr = bridge->masters[i].vme_base + offset;
729 aspace = bridge->masters[i].aspace;
730 cycle = bridge->masters[i].cycle;
731 dwidth = bridge->masters[i].dwidth;
732
733 spin_lock(&image->lock);
734
735 /* Here we apply for the same strategy we do in master_read
736 * function in order to assure the correct cycles.
737 */
738 if (addr & 0x1) {
739 fake_vmewrite8(bridge, (u8 *)buf, addr, aspace, cycle);
740 done += 1;
741 if (done == count)
742 goto out;
743 }
744
745 if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
746 if ((addr + done) & 0x2) {
747 if ((count - done) < 2) {
748 fake_vmewrite8(bridge, (u8 *)(buf + done),
749 addr + done, aspace, cycle);
750 done += 1;
751 goto out;
752 } else {
753 fake_vmewrite16(bridge, (u16 *)(buf + done),
754 addr + done, aspace, cycle);
755 done += 2;
756 }
757 }
758 }
759
760 if (dwidth == VME_D32) {
761 count32 = (count - done) & ~0x3;
762 while (done < count32) {
763 fake_vmewrite32(bridge, (u32 *)(buf + done),
764 addr + done, aspace, cycle);
765 done += 4;
766 }
767 } else if (dwidth == VME_D16) {
768 count32 = (count - done) & ~0x3;
769 while (done < count32) {
770 fake_vmewrite16(bridge, (u16 *)(buf + done),
771 addr + done, aspace, cycle);
772 done += 2;
773 }
774 } else if (dwidth == VME_D8) {
775 count32 = (count - done);
776 while (done < count32) {
777 fake_vmewrite8(bridge, (u8 *)(buf + done), addr + done,
778 aspace, cycle);
779 done += 1;
780 }
781
782 }
783
784 if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
785 if ((count - done) & 0x2) {
786 fake_vmewrite16(bridge, (u16 *)(buf + done),
787 addr + done, aspace, cycle);
788 done += 2;
789 }
790 }
791
792 if ((count - done) & 0x1) {
793 fake_vmewrite8(bridge, (u8 *)(buf + done), addr + done, aspace,
794 cycle);
795 done += 1;
796 }
797
798out:
799 retval = count;
800
801 spin_unlock(&image->lock);
802
803 return retval;
804}
805
806/*
807 * Perform an RMW cycle on the VME bus.
808 *
809 * Requires a previously configured master window, returns final value.
810 */
811static unsigned int fake_master_rmw(struct vme_master_resource *image,
812 unsigned int mask, unsigned int compare, unsigned int swap,
813 loff_t offset)
814{
815 u32 tmp, base;
816 u32 aspace, cycle;
817 int i;
818 struct fake_driver *bridge;
819
820 bridge = image->parent->driver_priv;
821
822 /* Find the PCI address that maps to the desired VME address */
823 i = image->number;
824
825 base = bridge->masters[i].vme_base;
826 aspace = bridge->masters[i].aspace;
827 cycle = bridge->masters[i].cycle;
828
829 /* Lock image */
830 spin_lock(&image->lock);
831
832 /* Read existing value */
833 tmp = fake_vmeread32(bridge, base + offset, aspace, cycle);
834
835 /* Perform check */
836 if ((tmp && mask) == (compare && mask)) {
837 tmp = tmp | (mask | swap);
838 tmp = tmp & (~mask | swap);
839
840 /* Write back */
841 fake_vmewrite32(bridge, &tmp, base + offset, aspace, cycle);
842 }
843
844 /* Unlock image */
845 spin_unlock(&image->lock);
846
847 return tmp;
848}
849
850/*
851 * All 4 location monitors reside at the same base - this is therefore a
852 * system wide configuration.
853 *
854 * This does not enable the LM monitor - that should be done when the first
855 * callback is attached and disabled when the last callback is removed.
856 */
857static int fake_lm_set(struct vme_lm_resource *lm, unsigned long long lm_base,
858 u32 aspace, u32 cycle)
859{
860 int i;
861 struct vme_bridge *fake_bridge;
862 struct fake_driver *bridge;
863
864 fake_bridge = lm->parent;
865
866 bridge = fake_bridge->driver_priv;
867
868 mutex_lock(&lm->mtx);
869
870 /* If we already have a callback attached, we can't move it! */
871 for (i = 0; i < lm->monitors; i++) {
872 if (bridge->lm_callback[i] != NULL) {
873 mutex_unlock(&lm->mtx);
874 pr_err("Location monitor callback attached, can't reset\n");
875 return -EBUSY;
876 }
877 }
878
879 switch (aspace) {
880 case VME_A16:
881 case VME_A24:
882 case VME_A32:
883 case VME_A64:
884 break;
885 default:
886 mutex_unlock(&lm->mtx);
887 pr_err("Invalid address space\n");
888 return -EINVAL;
889 }
890
891 bridge->lm_base = lm_base;
892 bridge->lm_aspace = aspace;
893 bridge->lm_cycle = cycle;
894
895 mutex_unlock(&lm->mtx);
896
897 return 0;
898}
899
900/* Get configuration of the callback monitor and return whether it is enabled
901 * or disabled.
902 */
903static int fake_lm_get(struct vme_lm_resource *lm,
904 unsigned long long *lm_base, u32 *aspace, u32 *cycle)
905{
906 struct fake_driver *bridge;
907
908 bridge = lm->parent->driver_priv;
909
910 mutex_lock(&lm->mtx);
911
912 *lm_base = bridge->lm_base;
913 *aspace = bridge->lm_aspace;
914 *cycle = bridge->lm_cycle;
915
916 mutex_unlock(&lm->mtx);
917
918 return bridge->lm_enabled;
919}
920
921/*
922 * Attach a callback to a specific location monitor.
923 *
924 * Callback will be passed the monitor triggered.
925 */
926static int fake_lm_attach(struct vme_lm_resource *lm, int monitor,
927 void (*callback)(void *), void *data)
928{
929 struct vme_bridge *fake_bridge;
930 struct fake_driver *bridge;
931
932 fake_bridge = lm->parent;
933
934 bridge = fake_bridge->driver_priv;
935
936 mutex_lock(&lm->mtx);
937
938 /* Ensure that the location monitor is configured - need PGM or DATA */
939 if (bridge->lm_cycle == 0) {
940 mutex_unlock(&lm->mtx);
941 pr_err("Location monitor not properly configured\n");
942 return -EINVAL;
943 }
944
945 /* Check that a callback isn't already attached */
946 if (bridge->lm_callback[monitor] != NULL) {
947 mutex_unlock(&lm->mtx);
948 pr_err("Existing callback attached\n");
949 return -EBUSY;
950 }
951
952 /* Attach callback */
953 bridge->lm_callback[monitor] = callback;
954 bridge->lm_data[monitor] = data;
955
956 /* Ensure that global Location Monitor Enable set */
957 bridge->lm_enabled = 1;
958
959 mutex_unlock(&lm->mtx);
960
961 return 0;
962}
963
964/*
965 * Detach a callback function forn a specific location monitor.
966 */
967static int fake_lm_detach(struct vme_lm_resource *lm, int monitor)
968{
969 u32 tmp;
970 int i;
971 struct fake_driver *bridge;
972
973 bridge = lm->parent->driver_priv;
974
975 mutex_lock(&lm->mtx);
976
977 /* Detach callback */
978 bridge->lm_callback[monitor] = NULL;
979 bridge->lm_data[monitor] = NULL;
980
981 /* If all location monitors disabled, disable global Location Monitor */
982 tmp = 0;
983 for (i = 0; i < lm->monitors; i++) {
984 if (bridge->lm_callback[i] != NULL)
985 tmp = 1;
986 }
987
988 if (tmp == 0)
989 bridge->lm_enabled = 0;
990
991 mutex_unlock(&lm->mtx);
992
993 return 0;
994}
995
996/*
997 * Determine Geographical Addressing
998 */
999static int fake_slot_get(struct vme_bridge *fake_bridge)
1000{
1001 return geoid;
1002}
1003
1004static void *fake_alloc_consistent(struct device *parent, size_t size,
1005 dma_addr_t *dma)
1006{
1007 void *alloc = kmalloc(size, GFP_KERNEL);
1008
1009 if (alloc != NULL)
Arnd Bergmann4b7b9482016-09-06 14:59:41 +02001010 *dma = fake_ptr_to_pci(alloc);
Martyn Welch658bcda2016-07-07 19:51:52 +01001011
1012 return alloc;
1013}
1014
1015static void fake_free_consistent(struct device *parent, size_t size,
1016 void *vaddr, dma_addr_t dma)
1017{
1018 kfree(vaddr);
1019/*
1020 dma_free_coherent(parent, size, vaddr, dma);
1021*/
1022}
1023
1024/*
1025 * Configure CR/CSR space
1026 *
1027 * Access to the CR/CSR can be configured at power-up. The location of the
1028 * CR/CSR registers in the CR/CSR address space is determined by the boards
1029 * Geographic address.
1030 *
1031 * Each board has a 512kB window, with the highest 4kB being used for the
1032 * boards registers, this means there is a fix length 508kB window which must
1033 * be mapped onto PCI memory.
1034 */
1035static int fake_crcsr_init(struct vme_bridge *fake_bridge)
1036{
1037 u32 vstat;
1038 struct fake_driver *bridge;
1039
1040 bridge = fake_bridge->driver_priv;
1041
1042 /* Allocate mem for CR/CSR image */
1043 bridge->crcsr_kernel = kzalloc(VME_CRCSR_BUF_SIZE, GFP_KERNEL);
Arnd Bergmann4b7b9482016-09-06 14:59:41 +02001044 bridge->crcsr_bus = fake_ptr_to_pci(bridge->crcsr_kernel);
Martyn Welch658bcda2016-07-07 19:51:52 +01001045 if (bridge->crcsr_kernel == NULL)
1046 return -ENOMEM;
1047
1048 vstat = fake_slot_get(fake_bridge);
1049
1050 pr_info("CR/CSR Offset: %d\n", vstat);
1051
1052 return 0;
1053}
1054
1055static void fake_crcsr_exit(struct vme_bridge *fake_bridge)
1056{
1057 struct fake_driver *bridge;
1058
1059 bridge = fake_bridge->driver_priv;
1060
1061 kfree(bridge->crcsr_kernel);
1062}
1063
1064
1065static int __init fake_init(void)
1066{
1067 int retval, i;
1068 struct list_head *pos = NULL, *n;
1069 struct vme_bridge *fake_bridge;
1070 struct fake_driver *fake_device;
1071 struct vme_master_resource *master_image;
1072 struct vme_slave_resource *slave_image;
1073 struct vme_lm_resource *lm;
1074
1075 /* We need a fake parent device */
1076 vme_root = __root_device_register("vme", THIS_MODULE);
1077
1078 /* If we want to support more than one bridge at some point, we need to
1079 * dynamically allocate this so we get one per device.
1080 */
1081 fake_bridge = kzalloc(sizeof(struct vme_bridge), GFP_KERNEL);
1082 if (fake_bridge == NULL) {
1083 retval = -ENOMEM;
1084 goto err_struct;
1085 }
1086
1087 fake_device = kzalloc(sizeof(struct fake_driver), GFP_KERNEL);
1088 if (fake_device == NULL) {
1089 retval = -ENOMEM;
1090 goto err_driver;
1091 }
1092
1093 fake_bridge->driver_priv = fake_device;
1094
1095 fake_bridge->parent = vme_root;
1096
1097 fake_device->parent = fake_bridge;
1098
1099 /* Initialize wait queues & mutual exclusion flags */
1100 mutex_init(&fake_device->vme_int);
1101 mutex_init(&fake_bridge->irq_mtx);
1102 tasklet_init(&fake_device->int_tasklet, fake_VIRQ_tasklet,
1103 (unsigned long) fake_bridge);
1104
1105 strcpy(fake_bridge->name, driver_name);
1106
1107 /* Add master windows to list */
1108 INIT_LIST_HEAD(&fake_bridge->master_resources);
1109 for (i = 0; i < FAKE_MAX_MASTER; i++) {
1110 master_image = kmalloc(sizeof(struct vme_master_resource),
1111 GFP_KERNEL);
1112 if (master_image == NULL) {
1113 retval = -ENOMEM;
1114 goto err_master;
1115 }
1116 master_image->parent = fake_bridge;
1117 spin_lock_init(&master_image->lock);
1118 master_image->locked = 0;
1119 master_image->number = i;
1120 master_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
1121 VME_A64;
1122 master_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
1123 VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
1124 VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
1125 VME_PROG | VME_DATA;
1126 master_image->width_attr = VME_D16 | VME_D32;
1127 memset(&master_image->bus_resource, 0,
1128 sizeof(struct resource));
1129 master_image->kern_base = NULL;
1130 list_add_tail(&master_image->list,
1131 &fake_bridge->master_resources);
1132 }
1133
1134 /* Add slave windows to list */
1135 INIT_LIST_HEAD(&fake_bridge->slave_resources);
1136 for (i = 0; i < FAKE_MAX_SLAVE; i++) {
1137 slave_image = kmalloc(sizeof(struct vme_slave_resource),
1138 GFP_KERNEL);
1139 if (slave_image == NULL) {
1140 retval = -ENOMEM;
1141 goto err_slave;
1142 }
1143 slave_image->parent = fake_bridge;
1144 mutex_init(&slave_image->mtx);
1145 slave_image->locked = 0;
1146 slave_image->number = i;
1147 slave_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
1148 VME_A64 | VME_CRCSR | VME_USER1 | VME_USER2 |
1149 VME_USER3 | VME_USER4;
1150 slave_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
1151 VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
1152 VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
1153 VME_PROG | VME_DATA;
1154 list_add_tail(&slave_image->list,
1155 &fake_bridge->slave_resources);
1156 }
1157
1158 /* Add location monitor to list */
1159 INIT_LIST_HEAD(&fake_bridge->lm_resources);
1160 lm = kmalloc(sizeof(struct vme_lm_resource), GFP_KERNEL);
1161 if (lm == NULL) {
1162 pr_err("Failed to allocate memory for location monitor resource structure\n");
1163 retval = -ENOMEM;
1164 goto err_lm;
1165 }
1166 lm->parent = fake_bridge;
1167 mutex_init(&lm->mtx);
1168 lm->locked = 0;
1169 lm->number = 1;
1170 lm->monitors = 4;
1171 list_add_tail(&lm->list, &fake_bridge->lm_resources);
1172
1173 fake_bridge->slave_get = fake_slave_get;
1174 fake_bridge->slave_set = fake_slave_set;
1175 fake_bridge->master_get = fake_master_get;
1176 fake_bridge->master_set = fake_master_set;
1177 fake_bridge->master_read = fake_master_read;
1178 fake_bridge->master_write = fake_master_write;
1179 fake_bridge->master_rmw = fake_master_rmw;
1180 fake_bridge->irq_set = fake_irq_set;
1181 fake_bridge->irq_generate = fake_irq_generate;
1182 fake_bridge->lm_set = fake_lm_set;
1183 fake_bridge->lm_get = fake_lm_get;
1184 fake_bridge->lm_attach = fake_lm_attach;
1185 fake_bridge->lm_detach = fake_lm_detach;
1186 fake_bridge->slot_get = fake_slot_get;
1187 fake_bridge->alloc_consistent = fake_alloc_consistent;
1188 fake_bridge->free_consistent = fake_free_consistent;
1189
1190 pr_info("Board is%s the VME system controller\n",
1191 (geoid == 1) ? "" : " not");
1192
1193 pr_info("VME geographical address is set to %d\n", geoid);
1194
1195 retval = fake_crcsr_init(fake_bridge);
1196 if (retval) {
1197 pr_err("CR/CSR configuration failed.\n");
1198 goto err_crcsr;
1199 }
1200
1201 retval = vme_register_bridge(fake_bridge);
1202 if (retval != 0) {
1203 pr_err("Chip Registration failed.\n");
1204 goto err_reg;
1205 }
1206
1207 exit_pointer = fake_bridge;
1208
1209 return 0;
1210
1211err_reg:
1212 fake_crcsr_exit(fake_bridge);
1213err_crcsr:
1214err_lm:
1215 /* resources are stored in link list */
1216 list_for_each_safe(pos, n, &fake_bridge->lm_resources) {
1217 lm = list_entry(pos, struct vme_lm_resource, list);
1218 list_del(pos);
1219 kfree(lm);
1220 }
1221err_slave:
1222 /* resources are stored in link list */
1223 list_for_each_safe(pos, n, &fake_bridge->slave_resources) {
1224 slave_image = list_entry(pos, struct vme_slave_resource, list);
1225 list_del(pos);
1226 kfree(slave_image);
1227 }
1228err_master:
1229 /* resources are stored in link list */
1230 list_for_each_safe(pos, n, &fake_bridge->master_resources) {
1231 master_image = list_entry(pos, struct vme_master_resource,
1232 list);
1233 list_del(pos);
1234 kfree(master_image);
1235 }
1236
1237 kfree(fake_device);
1238err_driver:
1239 kfree(fake_bridge);
1240err_struct:
1241 return retval;
1242
1243}
1244
1245
1246static void __exit fake_exit(void)
1247{
1248 struct list_head *pos = NULL;
1249 struct list_head *tmplist;
1250 struct vme_master_resource *master_image;
1251 struct vme_slave_resource *slave_image;
1252 int i;
1253 struct vme_bridge *fake_bridge;
1254 struct fake_driver *bridge;
1255
1256 fake_bridge = exit_pointer;
1257
1258 bridge = fake_bridge->driver_priv;
1259
1260 pr_debug("Driver is being unloaded.\n");
1261
1262 /*
1263 * Shutdown all inbound and outbound windows.
1264 */
1265 for (i = 0; i < FAKE_MAX_MASTER; i++)
1266 bridge->masters[i].enabled = 0;
1267
1268 for (i = 0; i < FAKE_MAX_SLAVE; i++)
1269 bridge->slaves[i].enabled = 0;
1270
1271 /*
1272 * Shutdown Location monitor.
1273 */
1274 bridge->lm_enabled = 0;
1275
1276 vme_unregister_bridge(fake_bridge);
1277
1278 fake_crcsr_exit(fake_bridge);
1279 /* resources are stored in link list */
1280 list_for_each_safe(pos, tmplist, &fake_bridge->slave_resources) {
1281 slave_image = list_entry(pos, struct vme_slave_resource, list);
1282 list_del(pos);
1283 kfree(slave_image);
1284 }
1285
1286 /* resources are stored in link list */
1287 list_for_each_safe(pos, tmplist, &fake_bridge->master_resources) {
1288 master_image = list_entry(pos, struct vme_master_resource,
1289 list);
1290 list_del(pos);
1291 kfree(master_image);
1292 }
1293
1294 kfree(fake_bridge->driver_priv);
1295
1296 kfree(fake_bridge);
1297
1298 root_device_unregister(vme_root);
1299}
1300
1301
1302MODULE_PARM_DESC(geoid, "Set geographical addressing");
1303module_param(geoid, int, 0);
1304
1305MODULE_DESCRIPTION("Fake VME bridge driver");
1306MODULE_LICENSE("GPL");
1307
1308module_init(fake_init);
1309module_exit(fake_exit);