Skip to content

Commit

Permalink
opae.admin: added regmap_debugfs.py
Browse files Browse the repository at this point in the history
Add a tool to help access the debugfs interface to regmaps
created in the linux kernel.

Signed-off-by: Matthew Gerlach <matthew.gerlach@linux.intel.com>
  • Loading branch information
matthew-gerlach committed Oct 28, 2020
1 parent 963438e commit b51c100
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
149 changes: 149 additions & 0 deletions python/opae.admin/opae/admin/tools/regmap_debugfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""
Copyright(c) 2020, Intel Corporation
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""

import argparse
import os.path


class RegmapDebugfs():
"""RegmapDebugfs is a class for accessing the debugfs
inteface of regmaps"""

def __init__(self, regmapdir):
if not os.path.isdir(regmapdir):
raise Exception("bad regmap dir")

_range = os.path.join(regmapdir, 'range')
if not os.path.isfile(_range):
raise Exception("could not find range entry")

_registers = os.path.join(regmapdir, 'registers')
if not os.path.isfile(_registers):
raise Exception("could not find registers entry")

self._registers = _registers

with open(self._registers, 'r') as _file:
line0 = _file.readline()
line1 = _file.readline()
self._record_len = len(line0)
list0 = line0.strip().split(': ')
list1 = line1.strip().split(': ')
self._register_stride = int(list1[0], 16) - int(list0[0], 16)

self.ranges = []
with open(_range, 'r') as _file:
for line in _file.readlines():
list0 = line.strip().split('-')
range_dict = {}
_min = int(list0[0], 16)
_max = int(list0[1], 16)
range_dict['min'] = _min
range_dict['max'] = _max
_nr = ((_max - _min) / self._register_stride) + 1
range_dict['num_records'] = _nr
self.ranges.append(range_dict)

def reg_read(self, addr):
"""reg_read returns the value read from the given
address in the regmap"""

found = False
num_records = 0
for range_dict in self.ranges:
if range_dict['min'] <= addr <= range_dict['max']:
found = True
_range = (addr - range_dict['min'])
num_records = num_records + _range / self._register_stride
break
else:
num_records = num_records + range_dict['num_records']

if not found:
raise Exception("bad address")

with open(self._registers, 'r') as _file:
_file.seek(num_records * self._record_len)
line0 = _file.readline()

list0 = line0.strip().split(': ')
raddr = int(list0[0], 16)

if raddr != addr:
msg = "bad addr read back 0x{:x} != 0x{:x}".format(raddr, addr)
raise Exception(msg)

return int(list0[1], 16)

def reg_write(self, addr, val):
"""reg_write writes val to the addr in the regmap"""

found = False
for range_dict in self.ranges:
if range_dict['min'] <= addr <= range_dict['max']:
found = True
break

if not found:
raise Exception("bad address")

with open(self._registers, 'rb+') as _file:
_file.write("{:x} {:x}".format(addr, val).encode())


def main():
"""main entry point"""

descr = "access to debugfs interface of regmaps"
parser = argparse.ArgumentParser(description=descr)

parser.add_argument('regmap', help='path to debugfs directory of regmap')

parser.add_argument('cmd', help='regmap transaction',
choices=['read', 'write'])

parser.add_argument('addr', help='hex address in regmap to access')

parser.add_argument('val', nargs='?',
help='hex value to write to register')

args = parser.parse_args()

reg_map = RegmapDebugfs(args.regmap)

addr = int(args.addr, 16)

if args.cmd == 'read':
val = reg_map.reg_read(addr)
print("{:x}".format(val))
elif args.cmd == 'write':
reg_map.reg_write(addr, int(args.val, 16))


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion python/opae.admin/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
'fpgaotsu = opae.admin.tools.fpgaotsu:main',
'fpgaport = opae.admin.tools.fpgaport:main',
'bitstreaminfo = opae.admin.tools.bitstream_info:main',
'opaevfio = opae.admin.tools.opaevfio:main'
'opaevfio = opae.admin.tools.opaevfio:main',
'regmap-debugfs = opae.admin.tools.regmap_debugfs:main'
]
},
install_requires=[],
Expand Down

0 comments on commit b51c100

Please sign in to comment.