forked from mchck/programmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadiv5-ap.rb
316 lines (260 loc) · 7.3 KB
/
adiv5-ap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
require 'register'
require 'log'
class Adiv5
class UnalignedAccessError < RuntimeError
end
class DebugDevice
# standard mem registers
include Peripheral
class BytePeripheral
include Peripheral
def initialize(backing)
@backing = backing
end
def get_backing(addr)
4.times.map do |i|
@backing.get_backing(get_address(addr + i * 4))
end.reverse.inject(0){|t,w| (t << 8)|w}
end
end
class PeripheralID < BytePeripheral
default_address 0xfd0
register :PERIPHERAL4, 0 do
unsigned :count_4kb, 7..4
unsigned :jep106_cc, 3..0
end
register :PERIPHERAL0, 4 do
unsigned :revand, 31..28
unsigned :custmod, 27..24
unsigned :revision, 23..20
bool :jep106_used?, 19
unsigned :jep106, 18..12
unsigned :partno, 11..0
end
end
unsigned :COMPONENT, 0xff0, :vector => 4
COMPONENT_PREAMBLE_MASK = 0xffff0fff
COMPONENT_PREAMBLES = [0xb105000d, 0xb102000d]
COMPONENT_CLASS_SHIFT = 12
COMPONENT_CLASS_MASK = 0xf << COMPONENT_CLASS_SHIFT
COMPONENT_CLASSES = Hash.new{|h,k| k}.merge({
0 => :generic_verify,
1 => :rom,
9 => :debug,
11 => :ptb,
13 => :dess,
14 => :generic_ip,
15 => :primecell
}).freeze
def self.probe(memap, base)
dev = new(memap, base)
if dev.component_class == :rom && dev.device_size == 4096
ROMTable.new(memap, base).devs
else
[dev]
end
end
attr_reader :component_class, :device_size
attr_reader :base
def initialize(memap, base)
@base = base
# for Peripheral
@backing = memap
@address = base
Log(:ap, 1){ '%s at %08x' % [self.class, @base] }
comp = 0
@backing.read(get_address(COMPONENT), :count => 4).each_with_index do |v, i|
comp |= (v & 0xff) << (i * 8)
end
if COMPONENT_PREAMBLES.include?(comp & COMPONENT_PREAMBLE_MASK)
@component_class = COMPONENT_CLASSES[(comp & COMPONENT_CLASS_MASK) >> COMPONENT_CLASS_SHIFT]
Log(:ap, 2){ "device component class: #@component_class" }
end
periph = PeripheralID.new(self)
@device_size = 4 * 1024 << periph.PERIPHERAL4.count_4kb
Log(:ap, 2){ 'device size: %d' % @device_size }
end
end
class ROMTable < DebugDevice
register :MEMTYPE, 0xfcc do
bool :sysmem?, 1
end
register :entries, 0, :vector => (0xf00 / 4) do
unsigned :addroffs, 31..12, :absolute => true
bool :format?, 1
bool :present?, 0
end
attr_reader :devs
def initialize(*args)
super(*args)
@sysmem = self.MEMTYPE.sysmem?
if self.entries[0].format?
scan
end
end
def scan
@devs = []
self.entries.each do |entry|
break if entry.to_i == 0
Log(:ap, 2){ 'rom table entry %08x' % entry.to_i }
next if !entry.present?
devbase = (@base + entry.addroffs) % (1 << 32)
devs = DebugDevice.probe(@backing, devbase)
@devs += devs if devs
end
end
end
class AP
include Peripheral
register :IDR, 0xfc do
unsigned :revision, 31..28
unsigned :JEP106, 27..17
bool :mem?, 16
unsigned :variant, 7..4
unsigned :type, 3..0
end
def self.probe(dp, apsel)
ap = new(dp, apsel)
return nil if not ap.valid?
if ap.IDR.mem?
ap = MemAP.new(dp, apsel)
end
ap
rescue StandardError => e
Log(:ap, 1){ "could not probe AP #{apsel}: #{e}\n#{e.backtrace.join("\n")}" }
nil
end
def initialize(dp, apsel)
@dp, @apsel = dp, apsel
end
def valid?
self.IDR.to_i != 0
end
def mem?
self.IDR.mem?
end
def read_ap(addr, opt={})
@dp.ap_select(@apsel, addr)
v = @dp.read(:ap, addr & 0xc, opt)
Log(:ap, 3){ "%d read %08x < %s" % [@apsel, addr, Log.hexary(v)] }
v
end
alias :get_backing :read_ap
def write_ap(addr, val)
Log(:ap, 3){ "%d write %08x = %s" % [@apsel, addr, Log.hexary(val)] }
@dp.ap_select(@apsel, addr)
@dp.write(:ap, addr & 0xc, val)
end
alias :set_backing :write_ap
end
class MemAP < AP
register :CSW, 0x00 do
bool :DbgSwEnable?, 31
unsigned :Prot, 30..24
bool :SPIDEN?, 23
unsigned :Mode, 11..8
bool :TrInProg?, 7
bool :DeviceEn?, 6
enum :AddrInc, 5..4, {
:off => 0b00,
:single => 0b01,
:packed => 0b10
}
enum :Size, 2..0, {
:byte => 0b000,
:halfword => 0b001,
:word => 0b010
}
end
unsigned :TAR, 0x04
unsigned :DRW, 0x0c
unsigned :BD, 0x10, :vector => 4
register :CFG, 0xf4 do
enum :endian, 0, {
:little => 0,
:big => 1
}
end
register :BASE, 0xf8 do
unsigned :BASEADDR, 31..12, :absolute => true
bool :format?, 1
bool :present?, 0
end
def mem?
true
end
attr_reader :endian
def initialize(*args)
super(*args)
Log(:ap, 1){ "initializing memap #@apsel" }
self.CSW.transact do |csw|
csw.Size = @last_size = :word
csw.AddrInc = :single
csw.Mode = 0
end
@endian = self.CFG.endian
end
def devs
return @devs if @devs
base = self.BASE.dup
if base.to_i != 0xffffffff && base.format? && base.present?
@devs = DebugDevice.probe(self, base.BASEADDR)
end
end
def transfer(dir, addr, val_or_opt, opt={})
if Hash === val_or_opt
opt.merge! val_or_opt
end
size = opt[:size]
size ||= :word
align = case size
when :word
4
when :halfword
2
when :byte
1
end
raise UnalignedAccessError if addr % align != 0
count = opt[:count]
count ||= 1
transfer_reg = DRW
if size == :word && count == 1 &&
@last_TAR && @last_TAR & 0xfffffff0 == addr & 0xfffffff0
ofs = addr & 0xf
transfer_reg = BD + ofs
elsif @last_TAR != addr
self.TAR = @last_TAR = addr
end
if @last_size != size
Log(:mem, 2){ "switching AP size to #{size}" }
self.CSW.Size = @last_size = size
end
if transfer_reg == DRW
# clear cache in case we run into an exception and lose track
@last_TAR = nil
end
v = case dir
when :in
read_ap(transfer_reg, val_or_opt)
when :out
write_ap(transfer_reg, val_or_opt)
end
if transfer_reg == DRW
@last_TAR = addr + count * align
end
v
end
def read(addr, opt={})
v = transfer(:in, addr, opt)
Log(:mem, 1){ "read %08x < %s" % [addr, Log.hexary(v)] }
v
end
alias :[] :read
def write(addr, val)
Log(:mem, 1){ "write %08x = %s" % [addr, Log.hexary(val)] }
transfer(:out, addr, val)
end
alias :[]= :write
end
end