blob: 70bf65e8493db2258d538fe5dd5875cd1586ecd8 [file] [log] [blame]
Matt Venn08cd6eb2020-11-16 12:01:14 +01001`default_nettype none
shalanfd13eb52020-08-21 16:48:07 +02002module gpio_wb # (
3 parameter BASE_ADR = 32'h 2100_0000,
4 parameter GPIO_DATA = 8'h 00,
5 parameter GPIO_ENA = 8'h 04,
6 parameter GPIO_PU = 8'h 08,
7 parameter GPIO_PD = 8'h 0c
8) (
9 input wb_clk_i,
10 input wb_rst_i,
11
12 input [31:0] wb_dat_i,
13 input [31:0] wb_adr_i,
14 input [3:0] wb_sel_i,
15 input wb_cyc_i,
16 input wb_stb_i,
17 input wb_we_i,
18
19 output [31:0] wb_dat_o,
20 output wb_ack_o,
21
Tim Edwards04ba17f2020-10-02 22:27:50 -040022 input gpio_in_pad,
23 output gpio,
24 output gpio_oeb,
25 output gpio_pu,
26 output gpio_pd
shalanfd13eb52020-08-21 16:48:07 +020027);
28
29 wire resetn;
30 wire valid;
31 wire ready;
32 wire [3:0] iomem_we;
33
34 assign resetn = ~wb_rst_i;
35 assign valid = wb_stb_i && wb_cyc_i;
36
37 assign iomem_we = wb_sel_i & {4{wb_we_i}};
38 assign wb_ack_o = ready;
39
40 gpio #(
41 .BASE_ADR(BASE_ADR),
42 .GPIO_DATA(GPIO_DATA),
43 .GPIO_ENA(GPIO_ENA),
44 .GPIO_PD(GPIO_PD),
45 .GPIO_PU(GPIO_PU)
46 ) gpio_ctrl (
47 .clk(wb_clk_i),
48 .resetn(resetn),
49
50 .gpio_in_pad(gpio_in_pad),
51
52 .iomem_addr(wb_adr_i),
53 .iomem_valid(valid),
Tim Edwards04ba17f2020-10-02 22:27:50 -040054 .iomem_wstrb(iomem_we[0]),
shalanfd13eb52020-08-21 16:48:07 +020055 .iomem_wdata(wb_dat_i),
56 .iomem_rdata(wb_dat_o),
57 .iomem_ready(ready),
58
59 .gpio(gpio),
60 .gpio_oeb(gpio_oeb),
61 .gpio_pu(gpio_pu),
62 .gpio_pd(gpio_pd)
63 );
64
65endmodule
66
67module gpio #(
68 parameter BASE_ADR = 32'h 2100_0000,
69 parameter GPIO_DATA = 8'h 00,
70 parameter GPIO_ENA = 8'h 04,
71 parameter GPIO_PU = 8'h 08,
72 parameter GPIO_PD = 8'h 0c
73) (
74 input clk,
75 input resetn,
76
Tim Edwards04ba17f2020-10-02 22:27:50 -040077 input gpio_in_pad,
shalanfd13eb52020-08-21 16:48:07 +020078
79 input [31:0] iomem_addr,
80 input iomem_valid,
Tim Edwards04ba17f2020-10-02 22:27:50 -040081 input iomem_wstrb,
shalanfd13eb52020-08-21 16:48:07 +020082 input [31:0] iomem_wdata,
83 output reg [31:0] iomem_rdata,
84 output reg iomem_ready,
85
Tim Edwards04ba17f2020-10-02 22:27:50 -040086 output gpio,
87 output gpio_oeb,
88 output gpio_pu,
89 output gpio_pd
shalanfd13eb52020-08-21 16:48:07 +020090);
91
Tim Edwards04ba17f2020-10-02 22:27:50 -040092 reg gpio; // GPIO output data
93 reg gpio_pu; // GPIO pull-up enable
94 reg gpio_pd; // GPIO pull-down enable
95 reg gpio_oeb; // GPIO output enable (sense negative)
shalanfd13eb52020-08-21 16:48:07 +020096
97 wire gpio_sel;
98 wire gpio_oeb_sel;
99 wire gpio_pu_sel;
100 wire gpio_pd_sel;
101
102 assign gpio_sel = (iomem_addr[7:0] == GPIO_DATA);
103 assign gpio_oeb_sel = (iomem_addr[7:0] == GPIO_ENA);
104 assign gpio_pu_sel = (iomem_addr[7:0] == GPIO_PU);
105 assign gpio_pd_sel = (iomem_addr[7:0] == GPIO_PD);
106
107 always @(posedge clk) begin
108 if (!resetn) begin
109 gpio <= 0;
110 gpio_oeb <= 16'hffff;
111 gpio_pu <= 0;
112 gpio_pd <= 0;
113 end else begin
114 iomem_ready <= 0;
115 if (iomem_valid && !iomem_ready && iomem_addr[31:8] == BASE_ADR[31:8]) begin
116 iomem_ready <= 1'b 1;
117
118 if (gpio_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400119 iomem_rdata <= {30'd0, gpio, gpio_in_pad};
120 if (iomem_wstrb) gpio <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200121
122 end else if (gpio_oeb_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400123 iomem_rdata <= {31'd0, gpio_oeb};
124 if (iomem_wstrb) gpio_oeb <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200125
126 end else if (gpio_pu_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400127 iomem_rdata <= {31'd0, gpio_pu};
128 if (iomem_wstrb) gpio_pu <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200129
130 end else if (gpio_pd_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400131 iomem_rdata <= {31'd0, gpio_pd};
132 if (iomem_wstrb) gpio_pd <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200133
134 end
135
136 end
137 end
138 end
139
Tim Edwardsc5265b82020-09-25 17:08:59 -0400140endmodule
Tim Edwards581068f2020-11-19 12:45:25 -0500141`default_nettype wire