MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / nios2nommu / scripts / nios2_system.h / BasicModule.pm
blobe15c26b08a75436ead41cc3ebbb7ec15ce2be1df
1 package BasicModule;
3 require PTF::SystemPTF;
4 require PTF::SystemPTF::Module;
5 use strict;
7 # Description: Prints an error message to stdout. This should prefix each line
8 # with "#error " so that it can be properly read by the C
9 # pre-processor.
10 # Args: $module_name: name of module that was is required by driver
11 # $class_name: name of device class that module belongs to.
12 sub print_error_name_used {
13 my ($class, $module_name, $class_name) = @_;
15 print "#error The kernel requires that the $class->required_class_name device be named as $module_name.\n";
16 print "#error The current hardware has $module_name defined as a(n) $class_name device.\n";
17 print "#error This will cause the kernel to fail.\n";
18 print "#error Please rename the current $module_name device to something else in SOPC Builder.\n";
21 # Description: This casts the base address to a specific data type
22 # By default, it does not cast the base address to
23 # anything.
24 sub base_address_cast {
25 my ($class, $port_name) = @_;
26 return;
29 # Description: This sub-routine prints out a prefix that is shown only once
30 # before any translations take place.
31 sub print_prefix {
32 my ($class, $system) = @_;
33 printf ("\n");
36 # Description: Prints a set of lines to stdout that re-defines all symbols
37 # related to $module_name to the symbols required by the driver.
38 # Typically starts off with "#undefine ..." statements followed
39 # by one or more "#define statements".
40 # Args: $required_module_name: the module name that's expected by the kernel.
41 # $module_name: the name of the module that was found in the PTF file.
42 sub translate {
43 my ($class, $system, $required_module_name, $module_name) = @_;
45 # get the necessary info about the module
46 my $module = $system->getModule ($module_name);
47 my @port_names = $module->getPorts ();
49 my $boolean_base_address_cast = 0;
50 if (scalar (@port_names) > 1) {
51 foreach my $port_name (@port_names) {
52 my $cast = $class->base_address_cast ($port_name);
53 if (defined ($cast)) {
54 $boolean_base_address_cast = 1;
55 last;
58 } else {
59 my $cast = $class->base_address_cast;
60 if (defined ($cast)) {
61 $boolean_base_address_cast = 1;
65 if ($module_name eq $required_module_name &&
66 !$boolean_base_address_cast) {
67 printf ("/* No translation necessary for $module_name */\n\n");
68 return;
71 # undefine the original entries
72 print "/* Redefining $module_name -> $required_module_name */\n";
73 if (scalar (@port_names) == 1) {
74 my $irq = $module->getIRQ ();
75 print "#undef na_" . $module_name . "\n";
76 if (defined ($irq)) {
77 print "#undef na_" . $module_name . "_irq\n";
79 print "\n";
80 } else {
81 foreach my $port_name (@port_names) {
82 print "#undef na_" . $module_name . "_" .
83 $port_name . "\n";
84 my $irq = $module->getIRQ ($port_name);
85 if (defined ($irq)) {
86 print "#undef na_" . $module_name . "_" .
87 $port_name . "_irq\n";
89 print "\n";
93 if (scalar (@port_names) == 1) {
94 # set up a string to pass to printf that will output the correct
95 # #define base address statement.
97 # turn on the high bit for the base address to bypass cache.
98 my $base_address = $module->getBaseAddress ();
99 $base_address = hex ($base_address) | 0x80000000;
101 my $cast = $class->base_address_cast;
102 $class->print_define_line ($required_module_name,
103 undef, "addr", $cast, $base_address);
105 # print out an IRQ define statement if necessary
106 my $irq = $module->getIRQ ();
107 if (defined ($irq)) {
108 $class->print_define_line ($required_module_name,
109 undef, "irq", undef, $irq);
111 printf ("\n");
112 } else {
113 foreach my $port_name (@port_names) {
114 my $cast = $class->base_address_cast ($port_name);
115 my $base_address = $module->getBaseAddress ($port_name);
116 $base_address = hex ($base_address) | 0x80000000;
117 $class->print_define_line ($required_module_name,
118 $port_name, "addr", $cast, $base_address);
120 my $irq = $module->getIRQ ($port_name);
121 if (defined ($irq)) {
122 $class->print_define_line (
123 $required_module_name, $port_name,
124 "irq", undef, $irq);
127 print "\n";
132 # Description: The following sub-routine prints out "undef" or "define"
133 # statements based on the arguments received.
134 # Args: $name: "define" or "undef"
135 # $port: name of port (if applicable)
136 # $type: "addr" or "irq"
137 # $cast: data type to cast base address to (if applicable)
138 # $value: value of symbol to be defined (if applicable)
139 sub print_define_line {
140 my ($class, $name, $port, $type, $cast, $value) = @_;
142 # construct the symbol that is being used
143 my $symbol .= "na_";
144 $symbol .= $name;
146 $symbol .= defined ($port) ? "_" . $port : "";
147 $symbol .= $type eq "irq" ? "_irq" : "";
149 my $string_value;
150 if ($type eq "addr") {
151 $string_value = sprintf ("%#010x", $value);
152 if (defined $cast) {
153 $string_value = "(($cast*) $string_value)";
155 } else {
156 $string_value = $value;
158 printf ("%-41s %30s\n", "#define $symbol", $string_value);
161 # Description: This sub-routine prints out a prefix that is shown only once
162 # after any translations take place.
163 sub print_suffix {
164 my ($class, $system) = @_;
165 # intentionally left empty
168 # Description: The following function allows the class to further determine if
169 # the module is valid. For instance, the timer class requires
170 # that the selected module does not have a fixed period.
171 # This function returns true by default which basically means
172 # that all modules belonging to class are valid.
173 sub is_module_valid {
174 my ($class, $system, $module_name) = @_;
175 return 1;
178 # Description: This sub-routine is required. It is executed by the
179 # "../gen_nios2_system_h.pl" script whenever any devices of type
180 # $class->required_class_name are found in the PTF file.
182 # It looks for any conflicting module names first. If any are
183 # found, "print_error_name_used" is called and this perl module
184 # exits.
186 # It then goes through the list of module names found in the PTF
187 # file that are of type $class->required_class_name and maps them to the
188 # list of unused names in $class->required_module_names.
190 # Finally, it will call the "translate" sub-routine to output the
191 # symbols required by the driver.
192 # Args: $system: a variable containing a reference to the system.ptf file that
193 # provides full access to any information in the file.
194 # @found_module_names: a list of module names that are of type
195 # $class->required_class_name
196 sub run2 {
197 my ($class, $system, @found_module_names) = @_;
199 # initialize a mapping of required module names to actual module names
200 my %module_map;
201 foreach my $module_name ($class->required_module_names) {
202 $module_map{$module_name} = "";
205 # if the required module name is already in use in the PTF file for a
206 # different device class, flag it as an error.
207 my $error_found = 0;
208 foreach my $module_name ($class->required_module_names) {
209 my $module = $system->getModule ($module_name);
211 if (!defined ($module)) {
212 next;
215 my $class_name = $module->getClass ();
216 if ($class_name ne $class->required_class_name) {
217 $class->print_error_name_used ($class, $module_name, $class_name);
218 $error_found = 1;
222 # if errors were found, then there's no point in continuing.
223 if ($error_found == 1) {
224 return;
227 # Run through list of modules that belong to the class and start
228 # mapping each module name to the first unused required module name
229 # as defined above
230 FOUND_MOD_LOOP: foreach my $module_name (@found_module_names) {
232 # If the module name has already been used, then continue
233 # to the next one.
234 foreach my $required_module_name ($class->required_module_names) {
235 if ($module_map{$required_module_name} eq $module_name) {
236 next FOUND_MOD_LOOP;
240 # assertion: $module_name is not mapped yet.
241 foreach my $required_module_name ($class->required_module_names) {
242 if ($module_map{$required_module_name} ne "") {
243 next;
246 if ($class->is_module_valid ($system, $module_name)) {
247 $module_map{$required_module_name} = $module_name;
249 last;
253 $class->print_prefix ($system);
255 # Now that everything's been mapped (or as close as we're going to get
256 # to it being mapped), start printing out the literal translation.
257 foreach my $required_module_name ($class->required_module_names) {
258 my $module_name = $module_map{$required_module_name};
259 if (length ($module_name) > 0) {
260 $class->translate ($system, $required_module_name, $module_name);
264 $class->print_suffix ($system);