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