redo pinmux experiment to get/put interface
[pinmux.git] / src / bsv / bsv_lib / slow_peripherals.bsv
1 package slow_peripherals;
2 /*===== Project imports =====*/
3 import defined_types::*;
4 import AXI4_Lite_Fabric::*;
5 import AXI4_Lite_Types::*;
6 import AXI4_Fabric::*;
7 import AXI4_Types::*;
8 import Semi_FIFOF::*;
9 import AXI4Lite_AXI4_Bridge::*;
10 `include "instance_defines.bsv"
11 /*===========================*/
12 /*=== package imports ===*/
13 import Clocks::*;
14 import GetPut::*;
15 import ClientServer::*;
16 import Connectable::*;
17 import Vector::*;
18 import FIFO::*;
19 /*=======================*/
20 /*===== Import the slow peripherals ====*/
21 `ifdef UART0
22 import Uart16550 :: *;
23 `endif
24 `ifdef UART1
25 import Uart_bs::*;
26 import RS232_modified::*;
27 `endif
28 `ifdef CLINT
29 import clint::*;
30 `endif
31 `ifdef PLIC
32 import plic ::*;
33 `endif
34 `ifdef I2C0
35 import I2C_top :: *;
36 `endif
37 `ifdef QSPI0
38 import qspi :: *;
39 `endif
40 `ifdef AXIEXP
41 import axiexpansion ::*;
42 `endif
43 `ifdef PWM_AXI4Lite
44 import pwm::*;
45 `endif
46 // NEEL EDIT
47 import pinmux::*;
48 import mux::*;
49 import gpio::*;
50 /*=====================================*/
51
52 /*===== interface declaration =====*/
53 interface SP_ios;
54 `ifdef UART0
55 interface RS232_PHY_Ifc uart0_coe;
56 `endif
57 `ifdef UART1
58 interface RS232 uart1_coe;
59 `endif
60 `ifdef I2C0
61 interface I2C_out i2c0_out;
62 `endif
63 `ifdef I2C1
64 interface I2C_out i2c1_out;
65 `endif
66 `ifdef QSPI0
67 interface QSPI_out qspi0_out;
68 `endif
69 `ifdef QSPI1
70 interface QSPI_out qspi1_out;
71 `endif
72 `ifdef AXIEXP
73 interface Get#(Bit#(67)) axiexp1_out;
74 interface Put#(Bit#(67)) axiexp1_in;
75 `endif
76 `ifdef PWM_AXI4Lite
77 interface PWMIO pwm_o;
78 `endif
79 endinterface
80 interface Ifc_slow_peripherals;
81 interface AXI4_Slave_IFC#(`PADDR,`Reg_width,`USERSPACE) axi_slave;
82 interface SP_ios slow_ios;
83 method Action external_int(Bit#(32) in);
84 `ifdef CLINT
85 method Bit#(1) msip_int;
86 method Bit#(1) mtip_int;
87 method Bit#(`Reg_width) mtime;
88 `endif
89 `ifdef PLIC method ActionValue#(Tuple2#(Bool,Bool)) intrpt_note; `endif
90 `ifdef I2C0 method Bit#(1) i2c0_isint; `endif
91 `ifdef I2C1 method Bit#(1) i2c1_isint; `endif
92 `ifdef QSPI0 method Bit#(1) qspi0_isint; `endif
93 `ifdef QSPI1 method Bit#(1) qspi1_isint; `endif
94 `ifdef UART0 method Bit#(1) uart0_intr; `endif
95 // NEEL EDIT
96 interface IOCellSide iocell_side; // mandatory interface
97 interface GPIO_config#(3) pad_configa; // depends on the number of banks
98 // NEEL EDIT OVER
99 endinterface
100 /*================================*/
101
102 function Tuple2#(Bool, Bit#(TLog#(Num_Slow_Slaves))) fn_address_mapping (Bit#(`PADDR) addr);
103 `ifdef UART0
104 if(addr>=`UART0Base && addr<=`UART0End)
105 return tuple2(True,fromInteger(valueOf(Uart0_slave_num)));
106 else
107 `endif
108 `ifdef UART1
109 if(addr>=`UART1Base && addr<=`UART1End)
110 return tuple2(True,fromInteger(valueOf(Uart1_slave_num)));
111 else
112 `endif
113 `ifdef CLINT
114 if(addr>=`ClintBase && addr<=`ClintEnd)
115 return tuple2(True,fromInteger(valueOf(CLINT_slave_num)));
116 else
117 `endif
118 `ifdef PLIC
119 if(addr>=`PLICBase && addr<=`PLICEnd)
120 return tuple2(True,fromInteger(valueOf(Plic_slave_num)));
121 else
122 `endif
123 `ifdef I2C0
124 if(addr>=`I2C0Base && addr<=`I2C0End)
125 return tuple2(True,fromInteger(valueOf(I2c0_slave_num)));
126 else
127 `endif
128 `ifdef I2C1
129 if(addr>=`I2C1Base && addr<=`I2C1End)
130 return tuple2(True,fromInteger(valueOf(I2c1_slave_num)));
131 else
132 `endif
133 `ifdef QSPI0
134 if(addr>=`QSPI0CfgBase && addr<=`QSPI0CfgEnd)
135 return tuple2(True,fromInteger(valueOf(Qspi0_slave_num)));
136 else if(addr>=`QSPI0MemBase && addr<=`QSPI0MemEnd)
137 return tuple2(True,fromInteger(valueOf(Qspi0_slave_num)));
138 else
139 `endif
140 `ifdef QSPI1
141 if(addr>=`QSPI1CfgBase && addr<=`QSPI1CfgEnd)
142 return tuple2(True,fromInteger(valueOf(Qspi1_slave_num)));
143 else if(addr>=`QSPI1MemBase && addr<=`QSPI1MemEnd)
144 return tuple2(True,fromInteger(valueOf(Qspi1_slave_num)));
145 else
146 `endif
147 `ifdef AXIEXP
148 if(addr>=`AxiExp1Base && addr<=`AxiExp1End)
149 return tuple2(True,fromInteger(valueOf(AxiExp1_slave_num)));
150 else
151 `endif
152 `ifdef PWM_AXI4Lite
153 if(addr>=`PWMBase && addr<=`PWMEnd)
154 return tuple2(True,fromInteger(valueOf(Pwm_slave_num)));
155 else
156 `endif
157
158 // NEEL EDIT
159 // give slave number and adress map to whatever peripherals you instantiate on the AXI4_Lite
160 // slave.
161 // NEEL EDIT OVER
162 return tuple2(False,?);
163 endfunction
164
165 (*synthesize*)
166 module mkslow_peripherals#(Clock fast_clock, Reset fast_reset, Clock uart_clock, Reset uart_reset
167 `ifdef PWM_AXI4Lite ,Clock ext_pwm_clock `endif )(Ifc_slow_peripherals);
168 Clock sp_clock <-exposeCurrentClock; // slow peripheral clock
169 Reset sp_reset <-exposeCurrentReset; // slow peripheral reset
170
171 /*======= Module declarations for each peripheral =======*/
172 `ifdef UART0
173 Uart16550_AXI4_Lite_Ifc uart0 <- mkUart16550(clocked_by uart_clock, reset_by uart_reset, sp_clock, sp_reset);
174 `endif
175 `ifdef UART1
176 //Ifc_Uart_bs uart1 <- mkUart_bs(clocked_by uart_clock, reset_by uart_reset,sp_clock, sp_reset);
177 Ifc_Uart_bs uart1 <- mkUart_bs(clocked_by sp_clock, reset_by sp_reset,sp_clock, sp_reset);
178 `endif
179 `ifdef CLINT
180 Ifc_clint clint <- mkclint();
181 `endif
182 `ifdef PLIC
183 Ifc_PLIC_AXI plic <- mkplicperipheral();
184 Wire#(Bit#(TLog#(`INTERRUPT_PINS))) interrupt_id <- mkWire();
185 Vector#(32, FIFO#(bit)) ff_gateway_queue <- replicateM(mkFIFO);
186 `endif
187 `ifdef I2C0
188 I2C_IFC i2c0 <- mkI2CController();
189 `endif
190 `ifdef I2C1
191 I2C_IFC i2c1 <- mkI2CController();
192 `endif
193 `ifdef QSPI0
194 Ifc_qspi qspi0 <- mkqspi();
195 `endif
196 `ifdef QSPI1
197 Ifc_qspi qspi1 <- mkqspi();
198 `endif
199 `ifdef AXIEXP
200 Ifc_AxiExpansion axiexp1 <- mkAxiExpansion();
201 `endif
202 `ifdef PWM_AXI4Lite
203 Ifc_PWM_bus pwm_bus <- mkPWM_bus(ext_pwm_clock);
204 `endif
205 // NEEL EDIT
206 Ifc_pinmux pinmux <- mkpinmux; // mandatory
207 MUX#(3) muxa <- mkmux(); // mandatory. number depends on the number of instances required.
208 GPIO#(3) gpioa <- mkgpio(); // optional. depends the number of IO pins declared before.
209 Wire#(Bit#(32)) wr_interrupt <- mkWire();
210 // NEEL EDIT OVER
211 /*=======================================================*/
212 mkConnection(pinmux.peripheral_side.uart.tx, uart0.io.sout);
213 mkConnection(pinmux.peripheral_side.uart.rx, uart0.io.sin);
214 AXI4_Lite_Fabric_IFC #(1, Num_Slow_Slaves, `PADDR, `Reg_width,`USERSPACE) slow_fabric <-
215 mkAXI4_Lite_Fabric(fn_address_mapping);
216 Ifc_AXI4Lite_AXI4_Bridge bridge <-mkAXI4Lite_AXI4_Bridge(fast_clock,fast_reset);
217
218 mkConnection (bridge.axi4_lite_master, slow_fabric.v_from_masters [0]);
219 /*======= Slave connections to AXI4Lite fabric =========*/
220 `ifdef UART0
221 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(Uart0_slave_num))],
222 uart0.slave_axi_uart);
223 `endif
224 `ifdef UART1
225 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(Uart1_slave_num))],
226 uart1.slave_axi_uart);
227 `endif
228 `ifdef CLINT
229 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(CLINT_slave_num))],
230 clint.axi4_slave);
231 `endif
232 `ifdef PLIC
233 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(Plic_slave_num))],
234 plic.axi4_slave_plic); //
235 `endif
236 `ifdef I2C0
237 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(I2c0_slave_num))],
238 i2c0.slave_i2c_axi);
239 `endif
240 `ifdef I2C1
241 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(I2c1_slave_num))],
242 i2c1.slave_i2c_axi); //
243 `endif
244 `ifdef QSPI0
245 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(Qspi0_slave_num))],
246 qspi0.slave);
247 `endif
248 `ifdef QSPI1
249 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(Qspi1_slave_num))],
250 qspi1.slave);
251 `endif
252 `ifdef AXIEXP
253 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(AxiExp1_slave_num))],
254 axiexp1.axi_slave); //
255 `endif
256 `ifdef PWM_AXI4Lite
257 mkConnection (slow_fabric.v_to_slaves [fromInteger(valueOf(Pwm_slave_num))],
258 pwm_bus.axi4_slave);
259 `endif
260
261 // NEEL EDIT
262 mkConnection (slow_fabric.
263 v_to_slaves[fromInteger(valueOf(Muxa_slave_num))],
264 muxa.axi_slave);
265 mkConnection (slow_fabric.
266 v_to_slaves[fromInteger(valueOf(Gpioa_slave_num))],
267 gpioa.axi_slave);
268 rule connect_select_lines_pinmux;// mandatory
269 pinmux.mux_lines.cell0_mux(muxa.mux_config.mux[0]);
270 pinmux.mux_lines.cell1_mux(muxa.mux_config.mux[1]);
271 pinmux.mux_lines.cell2_mux(muxa.mux_config.mux[2]);
272 endrule
273 rule connect_i2c0_scl;
274 pinmux.peripheral_side.twi_scl_out(i2c0.out.scl_out);
275 pinmux.peripheral_side.twi_scl_outen(pack(i2c0.out.scl_out_en));
276 endrule
277 rule connect_i2c0_scl_in;
278 i2c0.out.scl_in(pinmux.peripheral_side.twi_scl_in);
279 endrule
280 rule connect_i2c0_sda;
281 pinmux.peripheral_side.twi_sda_out(i2c0.out.sda_out);
282 pinmux.peripheral_side.twi_sda_outen(pack(i2c0.out.sda_out_en));
283 endrule
284 rule connect_i2c0_sda_in;
285 i2c0.out.sda_in(pinmux.peripheral_side.twi_sda_in);
286 endrule
287 rule connect_uart1tx;
288 pinmux.peripheral_side.uart_tx(uart1.coe_rs232.sout);
289 endrule
290 rule connect_uart1rx;
291 uart1.coe_rs232.sin(pinmux.peripheral_side.uart_rx);
292 endrule
293 rule connect_gpioa;
294 pinmux.peripheral_side.gpioa_a0_out(gpioa.func.gpio_out[0]);
295 pinmux.peripheral_side.gpioa_a0_outen(gpioa.func.gpio_out_en[0]);
296 pinmux.peripheral_side.gpioa_a1_out(gpioa.func.gpio_out[1]);
297 pinmux.peripheral_side.gpioa_a1_outen(gpioa.func.gpio_out_en[1]);
298 pinmux.peripheral_side.gpioa_a2_out(gpioa.func.gpio_out[2]);
299 pinmux.peripheral_side.gpioa_a2_outen(gpioa.func.gpio_out_en[2]);
300 Vector#(3,Bit#(1)) temp;
301 temp[0]=pinmux.peripheral_side.gpioa_a0_in;
302 temp[1]=pinmux.peripheral_side.gpioa_a1_in;
303 temp[2]=pinmux.peripheral_side.gpioa_a2_in;
304 gpioa.func.gpio_in(temp);
305 endrule
306 rule connect_qspi0_out;
307 let outs=qspi0.out.io_o;
308 let outs_en=qspi0.out.io_enable;
309 pinmux.peripheral_side.qspi_io0_out(outs[0]);
310 pinmux.peripheral_side.qspi_io1_out(outs[1]);
311 pinmux.peripheral_side.qspi_io2_out(outs[2]);
312 pinmux.peripheral_side.qspi_io3_out(outs[3]);
313 pinmux.peripheral_side.qspi_io0_outen(outs_en[0]);
314 pinmux.peripheral_side.qspi_io1_outen(outs_en[1]);
315 pinmux.peripheral_side.qspi_io2_outen(outs_en[2]);
316 pinmux.peripheral_side.qspi_io3_outen(outs_en[3]);
317 endrule
318 rule connect_qspi0_in;
319 qspi0.out.io_i({pinmux.peripheral_side.qspi_io3_in,pinmux.peripheral_side.qspi_io2_in,
320 pinmux.peripheral_side.qspi_io1_in, pinmux.peripheral_side.qspi_io0_in });
321 endrule
322 for(Integer i=0;i<32;i=i+ 1)begin
323 rule connect_int_to_plic(wr_interrupt[i]==1);
324 ff_gateway_queue[i].enq(1);
325 plic.ifc_external_irq[i].irq_frm_gateway(True);
326 endrule
327 end
328 rule rl_completion_msg_from_plic;
329 let id <- plic.intrpt_completion;
330 interrupt_id <= id;
331 `ifdef verbose $display("Dequeing the FIFO -- PLIC Interrupt Serviced id: %d",id); `endif
332 endrule
333
334 for(Integer i=0; i <32; i=i+1) begin
335 rule deq_gateway_queue;
336 if(interrupt_id==fromInteger(i)) begin
337 ff_gateway_queue[i].deq;
338 `ifdef $display($time,"Dequeing the Interrupt request for ID: %d",i); `endif
339 end
340 endrule
341 end
342 /* for connectin inputs from pinmux as itnerrupts
343 rule connect_pinmux_eint;
344 wr_interrupt<= pinmux.peripheral_side.eint_input;
345 endrule
346 */
347 // NEEL EDIT OVER
348 /*=======================================================*/
349 /*=================== PLIC Connections ==================== */
350 `ifdef PLIC_main
351 /*TODO DMA interrupt need to be connected to the plic
352 for(Integer i=1; i<8; i=i+1) begin
353 `ifdef DMA
354 rule rl_connect_dma_interrupts_to_plic;
355 if(dma.interrupt_to_processor[i-1]==1'b1) begin
356 ff_gateway_queue[i].enq(1);
357 plic.ifc_external_irq[i].irq_frm_gateway(True);
358 end
359 endrule
360 `else
361 rule rl_connect_dma_interrupts_to_plic;
362 ff_gateway_queue[i].enq(0);
363 endrule
364 `endif
365 end
366 */
367 rule rl_connect_i2c0_to_plic;
368 `ifdef I2C0
369 if(i2c0.isint()==1'b1) begin
370 ff_gateway_queue[8].enq(1);
371 plic.ifc_external_irq[8].irq_frm_gateway(True);
372 end
373 `else
374 ff_gateway_queue[8].enq(0);
375 `endif
376 endrule
377
378 rule rl_connect_i2c1_to_plic;
379 `ifdef I2C1
380 if(i2c1.isint()==1'b1) begin
381 ff_gateway_queue[9].enq(1);
382 plic.ifc_external_irq[9].irq_frm_gateway(True);
383 end
384 `else
385 ff_gateway_queue[9].enq(0);
386 `endif
387 endrule
388
389 rule rl_connect_i2c0_timerint_to_plic;
390 `ifdef I2C0
391 if(i2c0.timerint()==1'b1) begin
392 ff_gateway_queue[10].enq(1);
393 plic.ifc_external_irq[10].irq_frm_gateway(True);
394 end
395 `else
396 ff_gateway_queue[10].enq(0);
397 `endif
398 endrule
399
400 rule rl_connect_i2c1_timerint_to_plic;
401 `ifdef I2C1
402 if(i2c1.timerint()==1'b1) begin
403 ff_gateway_queue[11].enq(1);
404 plic.ifc_external_irq[11].irq_frm_gateway(True);
405 end
406 `else
407 ff_gateway_queue[11].enq(0);
408 `endif
409 endrule
410
411 rule rl_connect_i2c0_isber_to_plic;
412 `ifdef I2C0
413 if(i2c0.isber()==1'b1) begin
414 ff_gateway_queue[12].enq(1);
415 plic.ifc_external_irq[12].irq_frm_gateway(True);
416 end
417 `else
418 ff_gateway_queue[12].enq(0);
419 `endif
420 endrule
421
422 rule rl_connect_i2c1_isber_to_plic;
423 `ifdef I2C1
424 if(i2c1.isber()==1'b1) begin
425 ff_gateway_queue[13].enq(1);
426 plic.ifc_external_irq[13].irq_frm_gateway(True);
427 end
428 `else
429 ff_gateway_queue[13].enq(0);
430 `endif
431 endrule
432
433 for(Integer i = 14; i < 20; i=i+1) begin
434 rule rl_connect_qspi0_to_plic;
435 `ifdef QSPI0
436 if(qspi0.interrupts()[i-14]==1'b1) begin
437 ff_gateway_queue[i].enq(1);
438 plic.ifc_external_irq[i].irq_frm_gateway(True);
439 end
440 `else
441 ff_gateway_queue[i].enq(0);
442 `endif
443 endrule
444 end
445
446 for(Integer i = 20; i<26; i=i+1) begin
447 rule rl_connect_qspi1_to_plic;
448 `ifdef QSPI1
449 if(qspi1.interrupts()[i-20]==1'b1) begin
450 ff_gateway_queue[i].enq(1);
451 plic.ifc_external_irq[i].irq_frm_gateway(True);
452 end
453 `else
454 ff_gateway_queue[i].enq(0);
455 `endif
456 endrule
457 end
458
459 `ifdef UART0
460 SyncBitIfc#(Bit#(1)) uart0_interrupt <-mkSyncBitToCC(uart_clock,uart_reset);
461 rule synchronize_the_uart0_interrupt;
462 uart0_interrupt.send(uart0.irq);
463 endrule
464 `endif
465 rule rl_connect_uart_to_plic;
466 `ifdef UART0
467 if(uart0_interrupt.read==1'b1) begin
468 ff_gateway_queue[27].enq(1);
469 plic.ifc_external_irq[27].irq_frm_gateway(True);
470 end
471
472 `else
473 ff_gateway_queue[27].enq(0);
474 `endif
475 endrule
476
477 for(Integer i = 28; i<`INTERRUPT_PINS; i=i+1) begin
478 rule rl_raise_interrupts;
479 if((i-28)<`IONum) begin //Peripheral interrupts
480 if(gpio.to_plic[i-28]==1'b1) begin
481 plic.ifc_external_irq[i].irq_frm_gateway(True);
482 ff_gateway_queue[i].enq(1);
483 end
484 end
485 endrule
486 end
487
488 rule rl_completion_msg_from_plic;
489 let id <- plic.intrpt_completion;
490 interrupt_id <= id;
491 `ifdef verbose $display("Dequeing the FIFO -- PLIC Interrupt Serviced id: %d",id); `endif
492 endrule
493
494 for(Integer i=0; i <`INTERRUPT_PINS; i=i+1) begin
495 rule deq_gateway_queue;
496 if(interrupt_id==fromInteger(i)) begin
497 ff_gateway_queue[i].deq;
498 `ifdef $display($time,"Dequeing the Interrupt request for ID: %d",i); `endif
499 end
500 endrule
501 end
502
503
504 `endif
505 /*======================================================= */
506
507 /* ===== interface definition =======*/
508 interface axi_slave=bridge.axi_slave;
509 `ifdef PLIC method intrpt_note = plic.intrpt_note; `endif
510 `ifdef CLINT
511 method msip_int=clint.msip_int;
512 method mtip_int=clint.mtip_int;
513 method mtime=clint.mtime;
514 `endif
515 `ifdef I2C0
516 method i2c0_isint=i2c0.isint;
517 `endif
518 `ifdef I2C1
519 method i2c1_isint=i2c1.isint;
520 `endif
521 `ifdef QSPI0 method qspi0_isint=qspi0.interrupts[5]; `endif
522 `ifdef QSPI1 method qspi1_isint=qspi1.interrupts[5]; `endif
523 `ifdef UART0 method uart0_intr=uart0.irq; `endif
524 interface SP_ios slow_ios;
525 `ifdef UART0
526 interface uart0_coe=uart0.coe_rs232;
527 `endif
528 `ifdef UART1
529 interface uart1_coe=uart1.coe_rs232;
530 `endif
531 `ifdef I2C0
532 interface i2c0_out=i2c0.out;
533 `endif
534 `ifdef I2C1
535 interface i2c1_out=i2c1.out;
536 `endif
537 `ifdef QSPI0
538 interface qspi0_out = qspi0.out;
539 `endif
540 `ifdef QSPI1
541 interface qspi1_out = qspi1.out;
542 `endif
543 `ifdef AXIEXP
544 interface axiexp1_out=axiexp1.slave_out;
545 interface axiexp1_in=axiexp1.slave_in;
546 `endif
547 `ifdef PWM_AXI4Lite
548 interface pwm_o = pwm_bus.pwm_io;
549 `endif
550 endinterface
551 // NEEL EDIT
552 interface iocell_side=pinmux.iocell_side;
553 interface pad_configa= gpioa.pad_config;
554 method Action external_int(Bit#(32) in);
555 wr_interrupt<= in;
556 endmethod
557 // NEEL EDIT OVER
558 /*===================================*/
559 endmodule
560 endpackage