1 /*
2  * Copyright (c) 2011 Aeroflex Gaisler
3  *
4  * BSD license:
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 
26 #include <asm-leon/leon.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 /*#define DEBUG_CONFIG*/
32 
33 /* Structure containing address to devices found on the Amba Plug&Play bus */
34 extern amba_confarea_type amba_conf;
35 
36 /*collect apb slaves*/
37 int
amba_get_free_apbslv_devices(int vendor,int device,amba_apb_device * dev,int nr)38 amba_get_free_apbslv_devices (int vendor, int device, amba_apb_device * dev,
39 			      int nr)
40 {
41   unsigned int i, conf, iobar, j = 0;
42 #ifdef DEBUG_CONFIG
43   printf ("Apbslv: search for apdslv devices\n");
44 #endif
45   for (i = 0; i < amba_conf.apbslv.devnr && j < nr; i++)
46     {
47       conf = amba_get_confword (amba_conf.apbslv, i, 0);
48 #ifdef DEBUG_CONFIG
49       printf ("Apbslv: check(%x:%x)==(%x:%x)\n", vendor, device,
50 	      amba_vendor (conf), amba_device (conf));
51 #endif
52       if ((amba_vendor (conf) == vendor) && (amba_device (conf) == device))
53 	{
54 	  if (!(amba_conf.apbslv.allocbits[i / 32] & (1 << (i & (32 - 1)))))
55 	    {
56 #ifdef DEBUG_CONFIG
57 	      printf ("Apbslv: alloc device idx %i (%x:%x)\n",
58 		      j, vendor, device);
59 #endif
60 	      amba_conf.apbslv.allocbits[i / 32] |= (1 << (i & (32 - 1)));
61 	      dev[j].irq = amba_irq (conf);
62 	      iobar = amba_apb_get_membar (amba_conf.apbslv, i);
63 	      dev[j].start =
64 		amba_iobar_start (amba_conf.apbslv.apbmst[i], iobar);
65 #ifdef DEBUG_CONFIG
66 	      printf (" +bar: 0x%x \n", k, dev[j].start);
67 #endif
68 	      j++;
69 	    }
70 	}
71     }
72   return j;
73 }
74 
75 /*collect ahb slaves*/
76 int
amba_get_free_ahbslv_devices(int vendor,int device,amba_ahb_device * dev,int nr)77 amba_get_free_ahbslv_devices (int vendor, int device, amba_ahb_device * dev,
78 			      int nr)
79 {
80   unsigned int addr, i, conf, iobar, j = 0, k;
81 #ifdef DEBUG_CONFIG
82   printf ("Ahbslv: search for ahdslv devices\n");
83 #endif
84   for (i = 0; i < amba_conf.ahbslv.devnr && j < nr; i++)
85     {
86       conf = amba_get_confword (amba_conf.ahbslv, i, 0);
87 #ifdef DEBUG_CONFIG
88       printf ("Ahbslv: check(%x:%x)==(%x:%x)\n", vendor, device,
89 	      amba_vendor (conf), amba_device (conf));
90 #endif
91       if ((amba_vendor (conf) == vendor) && (amba_device (conf) == device))
92 	{
93 	  if (!(amba_conf.ahbslv.allocbits[i / 32] & (1 << (i & (32 - 1)))))
94 	    {
95 #ifdef DEBUG_CONFIG
96 	      printf ("Ahbslv: alloc device idx %i (%x:%x)\n",
97 		      j, vendor, device);
98 #endif
99 	      amba_conf.ahbslv.allocbits[i / 32] |= (1 << (i & (32 - 1)));
100 	      dev[j].irq = amba_irq (conf);
101 	      for (k = 0; k < 4; k++)
102 		{
103 		  iobar = amba_ahb_get_membar (amba_conf.ahbslv, i, k);
104 		  addr = amba_membar_start (iobar);
105 		  if (amba_membar_type (iobar) == AMBA_TYPE_AHBIO)
106 		    {
107 		      addr = AMBA_TYPE_AHBIO_ADDR (addr);
108 		    }
109 		  dev[j].start[k] = addr;
110 #ifdef DEBUG_CONFIG
111 		  printf (" +%i: 0x%x \n", k, dev[j].start[k]);
112 #endif
113 		}
114 	      j++;
115 	    }
116 	}
117     }
118   return j;
119 }
120