blob: 9941fc963924cb205a780777c5e789eac8cf6eca [file] [log] [blame]
agorararmard6c766a82020-12-10 18:13:12 +02001// SPDX-FileCopyrightText: 2020 Efabless Corporation
agorararmarde5780bf2020-12-09 21:27:56 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
agorararmardafa96ea2020-12-09 23:37:31 +020014// SPDX-License-Identifier: Apache-2.0
agorararmarde5780bf2020-12-09 21:27:56 +000015
Matt Venn08cd6eb2020-11-16 12:01:14 +010016`default_nettype none
shalanfd13eb52020-08-21 16:48:07 +020017module gpio_wb # (
18 parameter BASE_ADR = 32'h 2100_0000,
19 parameter GPIO_DATA = 8'h 00,
20 parameter GPIO_ENA = 8'h 04,
21 parameter GPIO_PU = 8'h 08,
22 parameter GPIO_PD = 8'h 0c
23) (
24 input wb_clk_i,
25 input wb_rst_i,
26
27 input [31:0] wb_dat_i,
28 input [31:0] wb_adr_i,
29 input [3:0] wb_sel_i,
30 input wb_cyc_i,
31 input wb_stb_i,
32 input wb_we_i,
33
34 output [31:0] wb_dat_o,
35 output wb_ack_o,
36
Tim Edwards04ba17f2020-10-02 22:27:50 -040037 input gpio_in_pad,
38 output gpio,
39 output gpio_oeb,
40 output gpio_pu,
41 output gpio_pd
shalanfd13eb52020-08-21 16:48:07 +020042);
43
44 wire resetn;
45 wire valid;
46 wire ready;
47 wire [3:0] iomem_we;
48
49 assign resetn = ~wb_rst_i;
50 assign valid = wb_stb_i && wb_cyc_i;
51
52 assign iomem_we = wb_sel_i & {4{wb_we_i}};
53 assign wb_ack_o = ready;
54
55 gpio #(
56 .BASE_ADR(BASE_ADR),
57 .GPIO_DATA(GPIO_DATA),
58 .GPIO_ENA(GPIO_ENA),
59 .GPIO_PD(GPIO_PD),
60 .GPIO_PU(GPIO_PU)
61 ) gpio_ctrl (
62 .clk(wb_clk_i),
63 .resetn(resetn),
64
65 .gpio_in_pad(gpio_in_pad),
66
67 .iomem_addr(wb_adr_i),
68 .iomem_valid(valid),
Tim Edwards04ba17f2020-10-02 22:27:50 -040069 .iomem_wstrb(iomem_we[0]),
shalanfd13eb52020-08-21 16:48:07 +020070 .iomem_wdata(wb_dat_i),
71 .iomem_rdata(wb_dat_o),
72 .iomem_ready(ready),
73
74 .gpio(gpio),
75 .gpio_oeb(gpio_oeb),
76 .gpio_pu(gpio_pu),
77 .gpio_pd(gpio_pd)
78 );
79
80endmodule
81
82module gpio #(
83 parameter BASE_ADR = 32'h 2100_0000,
84 parameter GPIO_DATA = 8'h 00,
85 parameter GPIO_ENA = 8'h 04,
86 parameter GPIO_PU = 8'h 08,
87 parameter GPIO_PD = 8'h 0c
88) (
89 input clk,
90 input resetn,
91
Tim Edwards04ba17f2020-10-02 22:27:50 -040092 input gpio_in_pad,
shalanfd13eb52020-08-21 16:48:07 +020093
94 input [31:0] iomem_addr,
95 input iomem_valid,
Tim Edwards04ba17f2020-10-02 22:27:50 -040096 input iomem_wstrb,
shalanfd13eb52020-08-21 16:48:07 +020097 input [31:0] iomem_wdata,
98 output reg [31:0] iomem_rdata,
99 output reg iomem_ready,
100
Tim Edwards04ba17f2020-10-02 22:27:50 -0400101 output gpio,
102 output gpio_oeb,
103 output gpio_pu,
104 output gpio_pd
shalanfd13eb52020-08-21 16:48:07 +0200105);
106
Tim Edwards04ba17f2020-10-02 22:27:50 -0400107 reg gpio; // GPIO output data
108 reg gpio_pu; // GPIO pull-up enable
109 reg gpio_pd; // GPIO pull-down enable
110 reg gpio_oeb; // GPIO output enable (sense negative)
shalanfd13eb52020-08-21 16:48:07 +0200111
112 wire gpio_sel;
113 wire gpio_oeb_sel;
114 wire gpio_pu_sel;
115 wire gpio_pd_sel;
116
117 assign gpio_sel = (iomem_addr[7:0] == GPIO_DATA);
118 assign gpio_oeb_sel = (iomem_addr[7:0] == GPIO_ENA);
119 assign gpio_pu_sel = (iomem_addr[7:0] == GPIO_PU);
120 assign gpio_pd_sel = (iomem_addr[7:0] == GPIO_PD);
121
122 always @(posedge clk) begin
123 if (!resetn) begin
124 gpio <= 0;
125 gpio_oeb <= 16'hffff;
126 gpio_pu <= 0;
127 gpio_pd <= 0;
128 end else begin
129 iomem_ready <= 0;
130 if (iomem_valid && !iomem_ready && iomem_addr[31:8] == BASE_ADR[31:8]) begin
131 iomem_ready <= 1'b 1;
132
133 if (gpio_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400134 iomem_rdata <= {30'd0, gpio, gpio_in_pad};
135 if (iomem_wstrb) gpio <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200136
137 end else if (gpio_oeb_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400138 iomem_rdata <= {31'd0, gpio_oeb};
139 if (iomem_wstrb) gpio_oeb <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200140
141 end else if (gpio_pu_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400142 iomem_rdata <= {31'd0, gpio_pu};
143 if (iomem_wstrb) gpio_pu <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200144
145 end else if (gpio_pd_sel) begin
Tim Edwards04ba17f2020-10-02 22:27:50 -0400146 iomem_rdata <= {31'd0, gpio_pd};
147 if (iomem_wstrb) gpio_pd <= iomem_wdata[0];
shalanfd13eb52020-08-21 16:48:07 +0200148
149 end
150
151 end
152 end
153 end
154
Tim Edwardsc5265b82020-09-25 17:08:59 -0400155endmodule
Tim Edwards581068f2020-11-19 12:45:25 -0500156`default_nettype wire