add pinmux cell connections
[pinmux.git] / src / bsv / peripheral_gen.py
1 import types
2 from copy import deepcopy
3
4
5 class PBase(object):
6 def __init__(self, name):
7 self.name = name
8
9 def axibase(self, name, ifacenum):
10 name = name.upper()
11 return "%(name)s%(ifacenum)dBase" % locals()
12
13 def axiend(self, name, ifacenum):
14 name = name.upper()
15 return "%(name)s%(ifacenum)dEnd" % locals()
16
17 def axi_reg_def(self, start, name, ifacenum):
18 name = name.upper()
19 offs = self.num_axi_regs32() * 4 * 16
20 end = start + offs - 1
21 bname = self.axibase(name, ifacenum)
22 bend = self.axiend(name, ifacenum)
23 comment = "%d 32-bit regs" % self.num_axi_regs32()
24 return (" `define%(bname)s 'h%(start)08X\n"
25 " `define%(bend)s 'h%(end)08X // %(comment)s" % locals(),
26 offs)
27
28 def axi_slave_name(self, name, ifacenum):
29 name = name.upper()
30 return "{0}{1}_slave_num".format(name, ifacenum)
31
32 def axi_slave_idx(self, idx, name, ifacenum):
33 name = self.axi_slave_name(name, ifacenum)
34 return ("typedef {0} {1};".format(idx, name), 1)
35
36 def axi_addr_map(self, name, ifacenum):
37 bname = self.axibase(name, ifacenum)
38 bend = self.axiend(name, ifacenum)
39 name = self.axi_slave_name(name, ifacenum)
40 return """\
41 if(addr>=`{0} && addr<=`{1})
42 return tuple2(True,fromInteger(valueOf({2})));
43 else""".format(bname, bend, name)
44
45 def mkslow_peripheral(self):
46 return ''
47
48 def __mk_connection(self, con, aname):
49 txt = " mkConnection (slow_fabric.v_to_slaves\n" + \
50 " [fromInteger(valueOf({1}))],\n" + \
51 " {0});"
52
53 print "PBase __mk_connection", self.name, aname
54 if not con:
55 return ''
56 return txt.format(con, aname)
57
58 def mk_connection(self, count, name=None):
59 if name is None:
60 name = self.name
61 print "PBase mk_conn", self.name, count
62 aname = self.axi_slave_name(name, count)
63 con = self._mk_connection(name).format(count, aname)
64 return self.__mk_connection(con, aname)
65
66 def _mk_connection(self, name=None):
67 return ''
68
69
70 class uart(PBase):
71
72 def slowimport(self):
73 return " import Uart16550 :: *;"
74
75 def slowifdecl(self):
76 return " interface RS232_PHY_Ifc uart{0}_coe;\n" + \
77 " method Bit#(1) uart{0}_intr;"
78
79 def num_axi_regs32(self):
80 return 8
81
82 def mkslow_peripheral(self):
83 return " Uart16550_AXI4_Lite_Ifc uart{0} <- \n" + \
84 " mkUart16550(clocked_by uart_clock,\n" + \
85 " reset_by uart_reset, sp_clock, sp_reset);"
86
87 def _mk_connection(self, name=None):
88 return "uart{0}.slave_axi_uart"
89
90
91
92 class rs232(PBase):
93
94 def slowimport(self):
95 return " import Uart_bs::*;\n" + \
96 " import RS232_modified::*;"
97
98 def slowifdecl(self):
99 return " interface RS232 uart{0}_coe;"
100
101 def num_axi_regs32(self):
102 return 2
103
104 def mkslow_peripheral(self):
105 return " //Ifc_Uart_bs uart{0} <-" + \
106 " // mkUart_bs(clocked_by uart_clock,\n" + \
107 " // reset_by uart_reset,sp_clock, sp_reset);" +\
108 " Ifc_Uart_bs uart{0} <-" + \
109 " mkUart_bs(clocked_by sp_clock,\n" + \
110 " reset_by sp_reset, sp_clock, sp_reset);"
111
112 def _mk_connection(self, name=None):
113 return "uart{0}.slave_axi_uart"
114
115
116 class twi(PBase):
117
118 def slowimport(self):
119 return " import I2C_top :: *;"
120
121 def slowifdecl(self):
122 return " interface I2C_out i2c{0}_out;\n" + \
123 " method Bit#(1) i2c{0}_isint;"
124
125 def num_axi_regs32(self):
126 return 8
127
128 def mkslow_peripheral(self):
129 return " I2C_IFC i2c{0} <- mkI2CController();"
130
131 def _mk_connection(self, name=None):
132 return "i2c{0}.slave_i2c_axi"
133
134
135 class qspi(PBase):
136
137 def slowimport(self):
138 return " import qspi :: *;"
139
140 def slowifdecl(self):
141 return " interface QSPI_out qspi{0}_out;\n" + \
142 " method Bit#(1) qspi{0}_isint;"
143
144 def num_axi_regs32(self):
145 return 13
146
147 def mkslow_peripheral(self):
148 return " Ifc_qspi qspi{0} <- mkqspi();"
149
150 def _mk_connection(self, name=None):
151 return "qspi{0}.slave"
152
153
154 class pwm(PBase):
155
156 def slowimport(self):
157 return " import pwm::*;"
158
159 def slowifdecl(self):
160 return " interface PWMIO pwm{0}_o;"
161
162 def num_axi_regs32(self):
163 return 4
164
165 def mkslow_peripheral(self):
166 return " Ifc_PWM_bus pwm{0}_bus <- mkPWM_bus(sp_clock);"
167
168 def _mk_connection(self, name=None):
169 return "pwm{0}_bus.axi4_slave"
170
171
172 class gpio(PBase):
173
174 def slowimport(self):
175 return " import pinmux::*;\n" + \
176 " import mux::*;\n" + \
177 " import gpio::*;\n"
178
179 def slowifdecl(self):
180 return " interface GPIO_config#({1}) pad_config{0};"
181
182 def num_axi_regs32(self):
183 return 2
184
185 def axi_slave_idx(self, idx, name, ifacenum):
186 """ generates AXI slave number definition, except
187 GPIO also has a muxer per bank
188 """
189 name = name.upper()
190 (ret, x) = PBase.axi_slave_idx(self, idx, name, ifacenum)
191 (ret2, x) = PBase.axi_slave_idx(self, idx, "mux", ifacenum)
192 return ("%s\n%s" % (ret, ret2), 2)
193
194 def mkslow_peripheral(self):
195 return " MUX#(%(name)s) mux{0} <- mkmux();\n" + \
196 " GPIO#(%(name)s) gpio{0} <- mkgpio();" % \
197 {'name': self.name}
198
199 def mk_connection(self, count):
200 print "GPIO mk_conn", self.name, count
201 res = []
202 for i, n in enumerate(['gpio', 'mux']):
203 res.append(PBase.mk_connection(self, count, n))
204 return '\n'.join(res)
205
206 def _mk_connection(self, name=None):
207 if name.startswith('gpio'):
208 return "gpio{0}.axi_slave"
209 if name.startswith('mux'):
210 return "mux{0}.axi_slave"
211
212
213 axi_slave_declarations = """\
214 typedef 0 SlowMaster;
215 {0}
216 typedef TAdd#(LastGen_slave_num,`ifdef CLINT 1 `else 0 `endif )
217 CLINT_slave_num;
218 typedef TAdd#(CLINT_slave_num ,`ifdef PLIC 1 `else 0 `endif )
219 Plic_slave_num;
220 typedef TAdd#(Plic_slave_num ,`ifdef AXIEXP 1 `else 0 `endif )
221 AxiExp1_slave_num;
222 typedef TAdd#(AxiExp1_slave_num,1) Num_Slow_Slaves;
223 """
224
225 pinmux_cellrule = """\
226 rule connect_select_lines_pinmux;
227 {0}
228 endrule
229 """
230
231
232 class CallFn(object):
233 def __init__(self, peripheral, name):
234 self.peripheral = peripheral
235 self.name = name
236
237 def __call__(self, *args):
238 #print "__call__", self.name, self.peripheral.slow, args
239 if not self.peripheral.slow:
240 return ''
241 return getattr(self.peripheral.slow, self.name)(*args[1:])
242
243
244 class PeripheralIface(object):
245 def __init__(self, ifacename):
246 self.slow = None
247 slow = slowfactory.getcls(ifacename)
248 print "Iface", ifacename, slow
249 if slow:
250 self.slow = slow(ifacename)
251 for fname in ['slowimport', 'slowifdecl', 'mkslow_peripheral',
252 'mk_connection']:
253 fn = CallFn(self, fname)
254 setattr(self, fname, types.MethodType(fn, self))
255
256 #print "PeripheralIface"
257 #print dir(self)
258
259 def axi_reg_def(self, start, count):
260 if not self.slow:
261 return ('', 0)
262 return self.slow.axi_reg_def(start, self.ifacename, count)
263
264 def axi_slave_idx(self, start, count):
265 if not self.slow:
266 return ('', 0)
267 return self.slow.axi_slave_idx(start, self.ifacename, count)
268
269 def axi_addr_map(self, count):
270 if not self.slow:
271 return ''
272 return self.slow.axi_addr_map(self.ifacename, count)
273
274
275 class PeripheralInterfaces(object):
276 def __init__(self):
277 pass
278
279 def slowimport(self, *args):
280 ret = []
281 for (name, count) in self.ifacecount:
282 #print "slowimport", name, self.data[name].slowimport
283 ret.append(self.data[name].slowimport())
284 return '\n'.join(list(filter(None, ret)))
285
286 def slowifdecl(self, *args):
287 ret = []
288 for (name, count) in self.ifacecount:
289 for i in range(count):
290 ret.append(self.data[name].slowifdecl().format(i, name))
291 return '\n'.join(list(filter(None, ret)))
292
293 def axi_reg_def(self, *args):
294 ret = []
295 start = 0x00011100 # start of AXI peripherals address
296 for (name, count) in self.ifacecount:
297 for i in range(count):
298 x = self.data[name].axi_reg_def(start, i)
299 #print ("ifc", name, x)
300 (rdef, offs) = x
301 ret.append(rdef)
302 start += offs
303 return '\n'.join(list(filter(None, ret)))
304
305 def axi_slave_idx(self, *args):
306 ret = []
307 start = 0
308 for (name, count) in self.ifacecount:
309 for i in range(count):
310 (rdef, offs) = self.data[name].axi_slave_idx(start, i)
311 #print ("ifc", name, rdef, offs)
312 ret.append(rdef)
313 start += offs
314 ret.append("typedef %d LastGen_slave_num" % (start - 1))
315 decls = '\n'.join(list(filter(None, ret)))
316 return axi_slave_declarations.format(decls)
317
318 def axi_addr_map(self, *args):
319 ret = []
320 for (name, count) in self.ifacecount:
321 for i in range(count):
322 ret.append(self.data[name].axi_addr_map(i))
323 return '\n'.join(list(filter(None, ret)))
324
325 def mkslow_peripheral(self, *args):
326 ret = []
327 for (name, count) in self.ifacecount:
328 for i in range(count):
329 x = self.data[name].mkslow_peripheral()
330 print name, count, x
331 ret.append(x.format(i))
332 return '\n'.join(list(filter(None, ret)))
333
334 def mk_connection(self, *args):
335 ret = []
336 for (name, count) in self.ifacecount:
337 for i in range(count):
338 print "mk_conn", name, i
339 txt = self.data[name].mk_connection(i)
340 if name == 'gpioa':
341 print "txt", txt
342 print self.data[name].mk_connection
343 ret.append(txt)
344 return '\n'.join(list(filter(None, ret)))
345
346 def mk_cellconn(self):
347 ret = []
348 txt = " pinmux.mux_lines.cell{0}_mux(muxa.mux_config.mux[{0}]);"
349 for (name, count) in self.ifacecount:
350 for i in range(count):
351 ret.append(txt.format(i))
352 ret = '\n'.join(list(filter(None, ret)))
353 return pinmux_cellrule.format(ret)
354
355 class PFactory(object):
356 def getcls(self, name):
357 for k, v in {'uart': uart,
358 'rs232': rs232,
359 'twi': twi,
360 'qspi': qspi,
361 'pwm': pwm,
362 'gpio': gpio
363 }.items():
364 if name.startswith(k):
365 return v
366 return None
367
368
369 slowfactory = PFactory()
370
371 if __name__ == '__main__':
372 p = uart('uart')
373 print p.slowimport()
374 print p.slowifdecl()
375 i = PeripheralIface('uart')
376 print i, i.slow
377 i = PeripheralIface('gpioa')
378 print i, i.slow