]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/iodelay-config.git/blob - iodelay-autogen.py
304b9061026128b7031b375a1ef7a5e4864dd014
[glsdk/iodelay-config.git] / iodelay-autogen.py
1 #!/usr/bin/python
2 # Python script to automatically generate the IO pad and delay data
3 # Author:- Nikhil Devshatwar
5 # This script uses regdump of the pad registers to figure out the muxmodes
6 # Based on the pad and muxmode, it find out all possible virtual/manual modes
7 # by referring to the PCT XML model. Using the, selection file, script
8 # decides which modes to be selected. Finally it dumps the required array entries
10 # This script depends on following files:-
11 # * XML data files      - Shipped with the DRA7xx PCT tool
12 # * Pad register dump   - Generate using omapconf dump 0x4a003400 0x4a00380c
13 # * selection file      - List of selected virtual/manual modes to be used
15 import xml.etree.ElementTree as ET
16 import re
17 import argparse
19 pad_data_xml = "XMLFiles/CTRL_MODULE_CORE.xml"
20 iod_data_xml = "XMLFiles/IODELAYCONFIG.xml"
21 model_data_xml = "XMLFiles/model_DRA75x_DRA74x_SR1.1_v1.0.7.xml"
22 modehelp_file = "guidelines.txt"
24 pad_file = "ctrl-core.dump"
25 sel_file = "selected-modes.txt"
27 # Handle the command line arguments
28 parser = argparse.ArgumentParser(prog='iodelay-autogen.py',
29         description='Python script to generate the IOdelay data',
30         epilog='Generate the pad and delay data using pad regdump')
32 parser.add_argument('-d', '--debug', dest='debug',
33         action='store', type=int, choices=[0, 1, 2], default=0,
34         help='set the debug level')
36 parser.add_argument('-i', '--interactive', dest='interactive',
37         action='store', type=int, choices=[0, 1], default=1,
38         help='run interactively with menus for resolving conflicts')
40 parser.add_argument('-f', '--format', dest='format',
41         action='store', type=str, choices=["linux", "uboot"], default="uboot",
42         help='select the output format to be used')
44 parser.add_argument('-m', '--module', dest='module',
45         action='store', type=str, default="",
46         help='generate only for this module')
48 parser.add_argument('-g', '--gpio', dest='gpio',
49         action='store_true',
50         help='generate script to read pad data from GPIO')
52 parser.add_argument('-c', '--check', dest='check_xml',
53         action='store_true',
54         help='check consistency of the XML files and other data')
56 args = parser.parse_args()
57 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
59 # Read the XML file database for pad and delay registers
60 pad_xml = ET.parse(pad_data_xml).getroot()
61 iod_xml = ET.parse(iod_data_xml).getroot()
62 model_xml = ET.parse(model_data_xml).getroot()
64 # Functions to parse the input files and build data structures
65 def read_guidelines(fname):
66         modehelp = {}
67         pattern = re.compile('(.+\w)\t+(.+)')
68         f = open(fname)
69         for line in f.readlines():
70                 list = re.match(pattern, line)
71                 if ( list == None):
72                         continue
73                 mode = list.groups(0)[0]
74                 info = list.groups(0)[1]
75                 modehelp[mode] = info
76                 if (args.debug >= 2):
77                         print "Help: Mode '%s' is used for '%s'" % (mode, info)
78         return modehelp
80 def read_selmodes(fname):
81         sel = {}
82         pattern = re.compile('(\w+)\W*,\W*(\w*)')
83         f = open(fname)
84         for line in f.readlines():
85                 list = re.match(pattern, line)
86                 if ( list == None):
87                         continue
88                 module = list.groups(0)[0]
89                 mode = list.groups(0)[1]
90                 sel[module] = mode
91                 if (args.debug >= 2):
92                         print "INFO: Select '%s' for '%s'" % (mode, module)
93         return sel
95 def read_pad_dump(file):
96         regmap = {}
97         pattern = re.compile('.*(0x[0-9A-F]+).*(0x[0-9A-F]+).*')
98         f = open(file, 'r')
99         for line in f:
100                 list = re.match(pattern, line)
101                 if ( list == None):
102                         continue
103                 addr = int(list.groups(0)[0], 16)
104                 val = int(list.groups(0)[1], 16)
105                 #print "PADXML: Addr = %08x Value = %08x" % (addr, val)
106                 regmap[addr] = val
107         return regmap
108 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
110 # Helper functions to parse XML nodes and get the desired data
111 # Hugely dependent on the XML format of the PCT files
112 # Uses XPath to search elements with queries and filters
113 # Parse the nodes to generate data in desired format
115 def xml_pad_get_name(offset):
116         padlist = pad_xml.findall("register[@offset='0x%X']" % offset)
117         if (len(padlist) == 0):
118                 return "Unknown"
119         else:
120                 return padlist[0].get("id")
122 def xml_pad_get_pin(offset, muxmode):
123         padlist = pad_xml.findall("register[@offset='0x%X']" % offset)
124         if (len(padlist) == 0):
125                 return "Unknown"
126         else:
127                 mux_field = padlist[0].findall("./bitfield[@end='0']")
128                 if (len(mux_field) == 0):
129                         return "Unknown"
130                 modes = mux_field[0].findall("./bitenum[@value='%d']" % muxmode)
131                 if (len(modes) == 0):
132                         return "Unknown"
133                 return modes[0].get("id")
135 def xml_iodelay_get_reg(name):
136         delaylist = iod_xml.findall("register[@id='%s']" % name)
137         if (len(delaylist) == 0):
138                 return 0
139         else:
140                 offset = delaylist[0].get("offset")
141                 #print "IOD: Name = %s Offset = %s" % (del_reg, offset)
142                 return int(offset, 16)
144 def xml_get_virtual(mode):
145         vmodes = {}
146         signal = mode.findtext("signal")
147         for virt in mode.findall("virtualmode/mode"):
148                 virt_mode = virt.findtext("value")
149                 virt_name = virt.findtext("name")
150                 if (virt_name == "NA"):
151                         continue
152                 virt_mode = int(virt_mode)
153                 vmodes[virt_name] = virt_mode
154                 if (args.debug >= 1):
155                         print ("    * VIRTUAL: %s [%s] => delaymode = %d" % (signal, virt_name, virt_mode))
156         return vmodes
158 def xml_get_manual(mode):
159         mmodes = {}
160         signal = mode.findtext("signal")
161         for man in mode.findall("manualmode"):
162                 regname = man.findtext("cfgreg/name")
163                 for sel in man.findall("cfgreg/mode"):
164                         adel = sel.findtext("adelay")
165                         gdel = sel.findtext("gdelay")
166                         man_name = sel.findtext("name")
167                         if (man_name == "NA"):
168                                 continue
169                         adel = int(adel)
170                         gdel = int(gdel)
171                         if (man_name not in mmodes.keys()):
172                                 mmodes[man_name] = []
173                         mmodes[man_name].append((regname, adel, gdel))
174                         if (args.debug >= 1):
175                                 print ("    * MANUAL: %s [%s] => delay[%s] = %d, %d" % (signal, man_name, regname, adel, gdel))
176         return mmodes
178 def xml_find_delaymodes(pad_name, muxmode):
179         padlist = model_xml.findall("padDB/clockNode/type/pad")
180         for pad in padlist:
181                 if (pad_name == pad.findtext("confregisters/regbit/register")):
182                         muxmodes = pad.findall("muxmode")
183                         break
184         if not muxmodes:
185                 return None
186         for mode in muxmodes:
187                 if ("%d" % muxmode == mode.findtext("mode")):
188                         virt = xml_get_virtual(mode)
189                         man = xml_get_manual(mode)
190                         return (virt, man)
191         return None
193 def xml_check_correctness(modehelp):
194         if(args.check_xml == False):
195                 return
197         print "###########################################"
198         print "XML correctness checks:"
199         padcore_list = []
200         padmodel_list = []
201         modes = {}
203         # Fint all the pads in core XML
204         pad_list = pad_xml.findall("register")
205         for i in pad_list:
206                 name = i.get("id")
207                 if (re.match("CTRL_CORE_PAD_.*", name) == None):
208                         continue
209                 padcore_list.append(name)
210         print ">> Total pads defined in core XML = %d" % len(padcore_list)
212         # Find all the pads in model XML
213         pad_list = model_xml.findall("padDB/clockNode/type/pad/confregisters/regbit")
214         for i in pad_list:
215                 name = i.findtext("register")
216                 padmodel_list.append(name)
217         print ">> Total pads defined in model XML = %d" % len(padmodel_list)
219         # Find all the manual modes
220         manmode_list = model_xml.findall("padDB/clockNode/type/pad/muxmode/manualmode/cfgreg/mode")
221         for i in manmode_list:
222                 man_name = i.findtext("name")
223                 modes[man_name] = "manual"
224         # Find all the virtual modes
225         virtmode_list = model_xml.findall("padDB/clockNode/type/pad/muxmode/virtualmode/mode")
226         for i in virtmode_list:
227                 virt_name = i.findtext("name")
228                 modes[virt_name] = "virtual"
229         print ">> Total delaymodes defined in model XML = %d" % len(modes)
231         # Print out all the errors found in the tests
232         print "== Pads defined in core XML but not in model XML =="
233         for i in padcore_list:
234                 if (i not in padmodel_list):
235                         print "\t%s" % i
236         print "== Pads defined in model XML but not in core XML =="
237         for i in padmodel_list:
238                 if (i not in padcore_list):
239                         print "\t%s" % i
241         # Check if description for each mode is available
242         print "== Delaymodes missing description in the guidelines.txt =="
243         for i in modes:
244                 if (i not in modehelp):
245                         print "\t%s" % i
247         print "###########################################"
249 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
251 # Decision point of the script
252 # For each pad/pin, decide what delay method is used
253 # Lookup in the selection file or ask user with a menu
254 def select_mode(pad, pin, module, virt, man, modehelp, sel):
255         modelist = []
256         for modename in virt.keys():
257                 modelist.append(modename)
259         for modename in man.keys():
260                 modelist.append(modename)
262         if (len(modelist) == 0):
263                 return "LEGACY"
265         if (module in sel.keys()):
266                 mode = sel[module]
267                 if (mode == "SKIP" or mode in modelist):
268                         return mode
269                 else:
270                         print("ERR: Invalid delaymode '%s' for module '%s'" % (mode, module))
272         if (args.interactive == 0):
273                 print "WARN: No delay modes for %s found, skipping" % module
274                 mode = "SKIP"
275                 sel[module] = mode
276                 return mode
278         # Display the menu and ask user to select the 'right' mode
279         while True:
280                 print ""
281                 print "MENU: Select delay mode for %s -> %s" % (pad, pin)
282                 i = 0
283                 print "MENU: %d: %s \t\t\t%s" % (i, "SKIP", "Skips this module for now")
284                 for mode in modelist:
285                         i += 1
286                         if (mode in modehelp):
287                                 helptext = modehelp[mode]
288                         else:
289                                 helptext = "Unknown"
290                         print "MENU: %d: %s \t\t%s" % (i, mode, helptext)
291                 try:
292                         choice = int(raw_input("Select delay mode #> "))
293                         if (choice == 0):
294                                 mode = "SKIP"
295                                 break
296                         elif (choice >=0 and choice <= len(modelist)):
297                                 mode = modelist[choice - 1]
298                                 break
299                         print "ERROR: Invalid choice"
300                 except ValueError:
301                         print "ERROR: Invalid choice"
302         print "MENU: Selected mode is %s" % mode
303         print ""
305         # Remember the selection for that module, for later reuse
306         sel[module] = mode
307         return mode
308 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
310 # Pad read using GPIO - Specific to linux only
311 # As each pad is muxed with a SoC gpio (for most of the pads)
312 # It's possible to read the pad data using gpio datain registers
313 # Dump the linux commands to read the gpio registers and read the pad data
314 def pad_dump_gpio(padconf, per_padconf):
315         if(args.gpio == False):
316                 return
318         print "# Read pad signals using GPIO DATA-IN registers"
319         print "# Works only for the input signals"
320         gpio_data_addrs = (0, 0x4ae10138, 0x48055138, 0x48057138, 0x48059138, 0x4805b138, 0x4805d138, 0x48051138, 0x48053138)
321         for i in padconf:
322                 (pad_name, pin_name, addr, val, delayinfo) = i
324                 offset = addr - 0x4a002000
325                 gpio_name = xml_pad_get_pin(offset, 14)
326                 matchlist = re.match("GPIO(.)_(.)", gpio_name)
327                 if (matchlist == None):
328                         print "ERR: No GPIO for pad %s" % pad_name
329                         continue
331                 inst = int(matchlist.groups(0)[0])
332                 bit = int(matchlist.groups(0)[1])
333                 gpio_addr = gpio_data_addrs[inst]
335                 print "#Read GPIO%d_%d to sample %s" % (inst, bit, pin_name)
336                 print " echo %d > /sys/class/gpio/export 2>/dev/null;" % ((inst - 1) * 32)
337                 print " val=`omapconf read 0x%x 2>&1 | grep -v \"support\"`;" % gpio_addr
338                 print " status=$((0x$val >> %d & 0x1));" % bit
339                 print " echo -e \"%s (%s)\t = $status\";" % (pin_name, gpio_name)
340                 print ""
341         print ""
342 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
344 # Generate the final pad and delay data in the required format
345 def format_pad_regs(padconf, per_padconf):
346         print "###########################################"
347         print "Pad data for u-boot (with the delay modes):"
348         print "###########################################"
349         if (args.format == "uboot"):
350                 uboot_format_pad_regs(padconf, per_padconf)
351         elif (args.format == "linux"):
352                 linux_format_pad_regs(padconf, per_padconf)
353         else:
354                 print "ERR: Format %s not suppported" % args.format
356 def format_delay_regs(delayconf, per_delayconf):
357         print "#########################################"
358         print "Delay data for u-boot (for manual modes):"
359         print "#########################################"
360         if (args.format == "uboot"):
361                 uboot_format_delay_regs(delayconf, per_delayconf)
362         elif (args.format == "linux"):
363                 linux_format_delay_regs(delayconf, per_delayconf)
364         else:
365                 print "ERR: Format %s not suppported" % args.format
367 def get_pin_info(val):
368         inp_en = (val >> 18) & 0x1
369         pulltype = (val >> 17) & 0x1
370         pull_dis = (val >> 16) & 0x1
371         pin = "PIN"
372         if (inp_en):
373                 pin += "_INPUT"
374         else:
375                 pin += "_OUTPUT"
376         if (pull_dis == 0):
377                 if (pulltype):
378                         pin += "_PULLUP"
379                 else:
380                         pin += "_PULLDOWN"
381         return pin
383 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
385 # Generate output in u-boot format
386 def uboot_format_pad_regs(padconf, per_padconf):
387         print "\nconst struct pad_conf_entry dra74x_core_padconf_array[] = {"
388         for i in padconf:
389                 (pad_name, pin_name, addr, val, delayinfo) = i
391                 if (re.match("MMC.*", pin_name) != None):
392                         print "WARN: Skipping MMC padconf in uboot"
393                         continue
395                 muxmode = val & 0xf
396                 (method, data) = delayinfo
397                 if (method == "LEGACY"):
398                         extramode = ""
399                 elif (method == "VIRTUAL"):
400                         delaymode = data
401                         extramode = " | VIRTUAL_MODE%d" % delaymode
402                 elif (method == "MANUAL"):
403                         extramode = " | MANUAL_MODE"
404                 else:
405                         print "ERR: Invalid method %s" % method
407                 pin_info = get_pin_info(val) + extramode
408                 pad_short = re.sub("CTRL_CORE_PAD_", "", pad_name)
409                 comment = (pad_short + "." + pin_name).lower()
411                 print "\t{ %s, (M%d | %s) },\t/* %s */" % (pad_short, muxmode, pin_info, comment)
412         print "};\n"
414 def uboot_format_delay_regs(delayconf, per_delayconf):
415         print "\nconst struct iodelay_cfg_entry dra742_iodelay_cfg_array[] = {"
416         for i in delayconf:
417                 (pad_name, pin_name, regname, del_offset, man_name, adel, gdel) = i
419                 if (re.match("MMC.*", pin_name) != None):
420                         print "WARN: Skipping MMC padconf in uboot"
421                         continue
423                 print "\t{ 0x%04X, %5d, %5d },\t/* %s */" % (del_offset, adel, gdel, regname)
424         print "};\n"
425 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
427 # Generate output in linux format
428 def linux_format_pad_regs(padconf, per_padconf):
429         print "&dra7_pmx_core {"
430         for per in per_padconf:
432                 if (re.match("MMC.*", per) == None):
433                         print "ERR: Only MMC padconf is recommended in kernel"
434                         continue
436                 dtsnode = ("%s_pins_default" % per).lower()
437                 print ""
438                 print "\t%s: %s {" % (dtsnode, dtsnode)
439                 print "\t\tpinctrl-single,pins = <"
440                 for i in per_padconf[per]:
441                         (pad_name, pin_name, addr, val, delayinfo) = i
443                         muxmode = val & 0xf
444                         (method, data) = delayinfo
445                         if (method == "LEGACY"):
446                                 extramode = ""
447                         elif (method == "VIRTUAL"):
448                                 delaymode = data
449                                 extramode = " | VIRTUAL_MODE%d" % delaymode
450                         elif (method == "MANUAL"):
451                                 extramode = " | MANUAL_MODE"
452                         else:
453                                 print "ERR: Invalid method %s" % method
455                         pin_info = get_pin_info(val) + extramode
456                         pad_short = re.sub("CTRL_CORE_PAD_", "", pad_name)
457                         offset = addr - 0x4a003400
458                         comment = (pad_short + "." + pin_name).lower()
460                         print "\t\t\t0x%03X\t(%s | MUM_MODE%d)\t/* %s */" % (offset, pin_info, muxmode, comment)
461                 print "\t\t>;"
462                 print "\t};"
463         print "};\n"
466 def linux_format_delay_regs(delayconf, per_delayconf):
467         print "&dra7_iodelay_core {"
468         for per in per_delayconf.keys():
470                 if (re.match("MMC.*", per) == None):
471                         print "ERR: Only MMC delayconf is recommended in kernel"
472                         continue
474                 # Get the mode from the first entry
475                 (pad_name, pin_name, regname, del_offset, mode, adel, gdel) = per_delayconf[per][0]
477                 dtsnode = ("%s_iodelay_%s_conf" % (per, mode)).lower()
478                 print "\t%s: %s {" % (dtsnode, dtsnode)
479                 print "\t\tpinctrl-single,pins = <"
480                 for i in per_delayconf[per]:
481                         (pad_name, pin_name, regname, del_offset, mode, adel, gdel) = i
483                         print "\t\t\t0x%03X A_DELAY(%d) | G_DELAY(%d)\t/* %s */" % (del_offset, adel, gdel, regname)
484                 print "\t\t>;"
485                 print "\t};"
486         print "};\n"
488 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
490 # Main execution starts here
491 # * Parse all the XML files - Get the pad data and IOdelay data
492 # * Iterate over the pad register dumps
493 # * For each pad, find out the muxmode being used and the pad direction, etc
494 # * For the muxmode being used, find out available virtual/modes
495 #   - Dump the virtual/manual modes available (based on options)
496 #   - Build up the gpio script to read the pad data using GPIO module
497 # * Select the correct mode to be used
498 #   - Check if the mode is described for the peripheral module, (from selected-modes.txt)
499 #   - Otherwise ask the user (with a menu) to decide the delaymode
500 #     => Update the selection for the whole of the peripheral module
501 #   - Worst case, skip the delaymode for this pad
502 # * Build the pad and delay register information
503 # * Create the dump for pad and delay data in linux/u-boot format
504 # * Dump the delaymode selection for each peripheral
505 # * Dump the GPIO script for each peripheral (all pads grouped together)
508 # Read all the input files and parse the required info
509 modehelp = read_guidelines(modehelp_file)
510 sel = read_selmodes(sel_file)
511 regmap = read_pad_dump(pad_file)
512 reglist = regmap.keys()
514 # List of all the pads being configured
515 padconf = []
516 # List of all the delay registers being configured (for manual mode)
517 delayconf = []
518 # Dictionary of pads grouped by peripheral/module
519 per_padconf = {}
520 # Dictionary of delay registers grouped by peripheral/module
521 per_delayconf = {}
523 xml_check_correctness(modehelp)
524 # Start iterating over each pad
525 for i in range(0, 260):
526         # Find out the muxmode and pad direction, etc
527         addr = 0x4a003400 + i * 4
528         offset = 0x1400 + i * 4
529         dts_offset = offset - 0x1400
531         if (addr not in reglist):
532                 print "WARN: Register dump for pad 0x%X not found" % addr
533                 continue
534         val = regmap[addr]
535         muxmode = val & 0xf
537         # Find out the pin based on the muxmode
538         pad_name = xml_pad_get_name(offset)
539         pin_name = xml_pad_get_pin(offset, muxmode)
540         if (pad_name == "Unknown" or pin_name == "Unknown"):
541                 print "WARN: Cannot find out Pad/Pin name for PAD address 0x%X (%s[%s] = %s)" \
542                 % (addr, pad_name, muxmode, pin_name)
543                 continue
544         if (pin_name == "DRIVER OFF"):
545                 continue
547         # Find out if the delaymode for this module is already selected
548         module = re.match("([^_]+)_.*", pin_name).groups(0)[0]
550         if (args.module != "" and args.module != module):
551                 continue
553         if (args.debug >= 1):
554                 print "\nPAD: %s: Addr= 0x%08X Value = 0x%08X \"%s\"" \
555                 % (pad_name, addr, val, pin_name)
557         # Find out all the possible virtual, manual modes fot the specific pad -> pin combination
558         (virt, man) = xml_find_delaymodes(pad_name, muxmode)
560         # Need to select one out of allowed modes
561         mode = select_mode(pad_name, pin_name, module, virt, man, modehelp, sel)
562         if (mode == "SKIP"):
563                 continue
565         if (mode == "LEGACY"):
566                 delayinfo = ("LEGACY", "")
567         elif (mode in virt):
568                 delayinfo = ("VIRTUAL", virt[mode])
569         elif (mode in man):
570                 delayinfo = ("MANUAL", man[mode])
571         else:
572                 print "ERR: Unknown mode '%s' selected" % mode
573                 continue
575         if (args.debug >= 1):
576                 print "  => Using mode %s" % mode
577         paddata = (pad_name, pin_name, addr, val, delayinfo)
578         if (module not in per_padconf.keys()):
579                 per_padconf[module] = []
580         padconf.append(paddata)
581         per_padconf[module].append(paddata)
583         if(mode not in man):
584                 continue
586         # Add the delay data if using manual mode
587         for regval in man[mode]:
588                 (regname, adel, gdel) = regval
589                 del_offset = xml_iodelay_get_reg(regname)
590                 if (del_offset == 0):
591                         print "WARN: Can't find delay offset of register %s" % regname
592                         break
593                 if (args.debug >= 1):
594                         print "  => Manual delay[%s] = (%d, %d)" % (regname, adel, gdel)
595                 delaydata = (pad_name, pin_name, regname, del_offset, mode, adel, gdel)
596                 if (module not in per_delayconf.keys()):
597                         per_delayconf[module] = []
598                 delayconf.append(delaydata)
599                 per_delayconf[module].append(delaydata)
601 print ""
602 print "Total PAD registers: %d" % len(padconf)
603 print "Total DELAY registers: %d" % len(delayconf)
604 print ""
606 format_pad_regs(padconf, per_padconf)
607 format_delay_regs(delayconf, per_delayconf)
608 pad_dump_gpio(padconf, per_padconf)
610 # Dump the final selection for reuse
611 print "Selected modes for each peripheral module\n"
612 for group in sel:
613         print "%s,\t%s" % (group, sel[group])
614 print ""
615 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #