OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [lleqspi.v] - Blame information for rev 48

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 3 dgisselq
//
3
// Filename:    lleqspi.v
4
//
5
// Project:     Wishbone Controlled Quad SPI Flash Controller
6
//
7
// Purpose:     Reads/writes a word (user selectable number of bytes) of data
8
//              to/from a Quad SPI port.  The port is understood to be 
9
//              a normal SPI port unless the driver requests four bit mode.
10
//              When not in use, unlike our previous SPI work, no bits will
11
//              toggle.
12
//
13
// Creator:     Dan Gisselquist
14
//              Gisselquist Technology, LLC
15
//
16 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
17 3 dgisselq
//
18
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
19
//
20
// This program is free software (firmware): you can redistribute it and/or
21
// modify it under the terms of  the GNU General Public License as published
22
// by the Free Software Foundation, either version 3 of the License, or (at
23
// your option) any later version.
24
//
25
// This program is distributed in the hope that it will be useful, but WITHOUT
26
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
27
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28
// for more details.
29
//
30
// You should have received a copy of the GNU General Public License along
31
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
32
// target there if the PDF file isn't present.)  If not, see
33
// <http://www.gnu.org/licenses/> for a copy.
34
//
35
// License:     GPL, v3, as defined and found on www.gnu.org,
36
//              http://www.gnu.org/licenses/gpl.html
37
//
38
//
39 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
40 3 dgisselq
`define EQSPI_IDLE      3'h0
41
`define EQSPI_START     3'h1
42
`define EQSPI_BITS      3'h2
43
`define EQSPI_READY     3'h3
44
`define EQSPI_HOLDING   3'h4
45
`define EQSPI_STOP      3'h5
46
`define EQSPI_STOP_B    3'h6
47
`define EQSPI_RECYCLE   3'h7
48
 
49
// Modes
50
`define EQSPI_MOD_SPI   2'b00
51
`define EQSPI_MOD_QOUT  2'b10   // Write
52
`define EQSPI_MOD_QIN   2'b11   // Read
53
 
54
module  lleqspi(i_clk,
55
                // Module interface
56
                i_wr, i_hold, i_word, i_len, i_spd, i_dir, i_recycle,
57
                        o_word, o_valid, o_busy,
58
                // QSPI interface
59
                o_sck, o_cs_n, o_mod, o_dat, i_dat);
60
        input                   i_clk;
61
        // Chip interface
62
        //      Can send info
63
        //              i_dir = 1, i_spd = 0, i_hold = 0, i_wr = 1,
64
        //                      i_word = { 1'b0, 32'info to send },
65
        //                      i_len = # of bytes in word-1
66
        input                   i_wr, i_hold;
67
        input           [31:0]   i_word;
68
        input           [1:0]    i_len;  // 0=>8bits, 1=>16 bits, 2=>24 bits, 3=>32 bits
69
        input                   i_spd; // 0 -> normal QPI, 1 -> QSPI
70
        input                   i_dir; // 0 -> read, 1 -> write to SPI
71
        input                   i_recycle; // 0 = 20ns, 1 = 50ns
72
        output  reg     [31:0]   o_word;
73 13 dgisselq
        output  reg             o_valid;
74 3 dgisselq
        output  reg             o_busy;
75
        // Interface with the QSPI lines
76
        output  reg             o_sck;
77
        output  reg             o_cs_n;
78
        output  reg     [1:0]    o_mod;
79
        output  reg     [3:0]    o_dat;
80
        input           [3:0]    i_dat;
81
 
82
        // output       wire    [22:0]  o_dbg;
83
        // assign       o_dbg = { state, spi_len,
84
                // o_busy, o_valid, o_cs_n, o_sck, o_mod, o_dat, i_dat };
85
 
86
        wire    i_miso;
87
        assign  i_miso = i_dat[1];
88
 
89
        // These are used in creating a delayed input.
90
        reg             rd_input, rd_spd, rd_valid;
91
 
92
        reg             r_spd, r_dir;
93
        reg     [3:0]    r_recycle;
94
        reg     [5:0]    spi_len;
95
        reg     [31:0]   r_word;
96
        reg     [30:0]   r_input;
97
        reg     [2:0]    state;
98
        initial state = `EQSPI_IDLE;
99
        initial o_sck   = 1'b1;
100
        initial o_cs_n  = 1'b1;
101
        initial o_dat   = 4'hd;
102
        initial rd_valid = 1'b0;
103
        initial o_busy  = 1'b0;
104
        initial r_input = 31'h000;
105
        initial rd_valid = 1'b0;
106
        always @(posedge i_clk)
107
        begin
108
                rd_input <= 1'b0;
109
                rd_spd   <= r_spd;
110
                rd_valid <= 1'b0;
111
 
112
                if ((state == `EQSPI_IDLE)&&(o_sck))
113
                begin
114
                        o_cs_n <= 1'b1;
115
                        o_busy  <= 1'b0;
116
                        o_mod <= `EQSPI_MOD_SPI;
117
                        r_word <= i_word;
118
                        r_spd <= i_spd;
119
                        r_dir <= i_dir;
120
                        o_dat <= 4'hc;
121
                        r_recycle <= (i_recycle)? 4'h8 : 4'h2; // 4'ha : 4'h4
122
                        spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8;
123
                        o_sck <= 1'b1;
124
                        if (i_wr)
125
                        begin
126
                                state <= `EQSPI_START;
127
                                o_cs_n <= 1'b0;
128
                                o_busy <= 1'b1;
129
                        end
130
                end else if (state == `EQSPI_START)
131
                begin // We come in here with sck high, stay here 'til sck is low
132
                        o_sck <= 1'b0;
133
                        if (o_sck == 1'b0)
134
                        begin
135
                                state <= `EQSPI_BITS;
136
                                spi_len<= spi_len - ( (r_spd)? 6'h4 : 6'h1 );
137
                                if (r_spd)
138
                                        r_word <= { r_word[27:0], 4'h0 };
139
                                else
140
                                        r_word <= { r_word[30:0], 1'b0 };
141
                        end
142
                        o_mod <= (r_spd) ? { 1'b1, r_dir } : `EQSPI_MOD_SPI;
143
                        o_cs_n <= 1'b0;
144
                        o_busy <= 1'b1;
145
                        if (r_spd)
146
                                o_dat <= r_word[31:28];
147
                        else
148
                                o_dat <= { 3'b110, r_word[31] };
149
                end else if (~o_sck)
150
                begin
151
                        o_sck <= 1'b1;
152
                        o_busy <= ((state != `EQSPI_READY)||(~i_wr));
153
                end else if (state == `EQSPI_BITS)
154
                begin
155
                        // Should enter into here with at least a spi_len
156
                        // of one, perhaps more
157
                        o_sck <= 1'b0;
158
                        o_busy <= 1'b1;
159
                        if (r_spd)
160
                        begin
161
                                o_dat <= r_word[31:28];
162
                                r_word <= { r_word[27:0], 4'h0 };
163
                                spi_len <= spi_len - 6'h4;
164
                                if (spi_len == 6'h4)
165
                                        state <= `EQSPI_READY;
166
                        end else begin
167
                                o_dat <= { 3'b110, r_word[31] };
168
                                r_word <= { r_word[30:0], 1'b0 };
169
                                spi_len <= spi_len - 6'h1;
170
                                if (spi_len == 6'h1)
171
                                        state <= `EQSPI_READY;
172
                        end
173
 
174
                        rd_input <= 1'b1;
175
                end else if (state == `EQSPI_READY)
176
                begin
177
                        o_cs_n <= 1'b0;
178
                        o_busy <= 1'b1;
179
                        // This is the state on the last clock (both low and
180
                        // high clocks) of the data.  Data is valid during
181
                        // this state.  Here we chose to either STOP or
182
                        // continue and transmit more.
183
                        o_sck <= (i_hold); // No clocks while holding
184
                        if((~o_busy)&&(i_wr))// Acknowledge a new request
185
                        begin
186
                                state <= `EQSPI_BITS;
187
                                o_busy <= 1'b1;
188
                                o_sck <= 1'b0;
189
 
190
                                // Read the new request off the bus
191
                                r_spd <= i_spd;
192
                                r_dir <= i_dir;
193
                                // Set up the first bits on the bus
194
                                o_mod <= (i_spd) ? { 1'b1, i_dir } : `EQSPI_MOD_SPI;
195
                                if (i_spd)
196
                                begin
197
                                        o_dat <= i_word[31:28];
198
                                        r_word <= { i_word[27:0], 4'h0 };
199
                                        // spi_len <= spi_len - 4;
200
                                        spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8
201
                                                - 6'h4;
202
                                end else begin
203
                                        o_dat <= { 3'b110, i_word[31] };
204
                                        r_word <= { i_word[30:0], 1'b0 };
205
                                        spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8
206
                                                - 6'h1;
207
                                end
208
 
209
                                // Read a bit upon any transition
210
                                rd_input <= 1'b1;
211
                                rd_valid <= 1'b1;
212
                        end else begin
213
                                o_sck <= 1'b1;
214
                                state <= (i_hold)?`EQSPI_HOLDING : `EQSPI_STOP;
215
                                o_busy <= (~i_hold);
216
 
217
                                // Read a bit upon any transition
218
                                rd_valid <= 1'b1;
219
                                rd_input <= 1'b1;
220
                        end
221
                end else if (state == `EQSPI_HOLDING)
222
                begin
223
                        // We need this state so that the o_valid signal
224
                        // can get strobed with our last result.  Otherwise
225
                        // we could just sit in READY waiting for a new command.
226
                        //
227
                        // Incidentally, the change producing this state was
228
                        // the result of a nasty race condition.  See the
229
                        // commends in wbqspiflash for more details.
230
                        //
231
                        rd_valid <= 1'b0;
232
                        o_cs_n <= 1'b0;
233
                        o_busy <= 1'b0;
234
                        if((~o_busy)&&(i_wr))// Acknowledge a new request
235
                        begin
236
                                state  <= `EQSPI_BITS;
237
                                o_busy <= 1'b1;
238
                                o_sck  <= 1'b0;
239
 
240
                                // Read the new request off the bus
241
                                r_spd <= i_spd;
242
                                r_dir <= i_dir;
243
                                // Set up the first bits on the bus
244
                                o_mod<=(i_spd)?{ 1'b1, i_dir } : `EQSPI_MOD_SPI;
245
                                if (i_spd)
246
                                begin
247
                                        o_dat <= i_word[31:28];
248
                                        r_word <= { i_word[27:0], 4'h0 };
249
                                        spi_len<= { 1'b0, i_len, 3'b100 };
250
                                end else begin
251
                                        o_dat <= { 3'b110, i_word[31] };
252
                                        r_word <= { i_word[30:0], 1'b0 };
253
                                        spi_len<= { 1'b0, i_len, 3'b111 };
254
                                end
255
                        end else begin
256
                                o_sck <= 1'b1;
257
                                state <= (i_hold)?`EQSPI_HOLDING : `EQSPI_STOP;
258
                                o_busy <= (~i_hold);
259
                        end
260
                end else if (state == `EQSPI_STOP)
261
                begin
262
                        o_sck   <= 1'b1; // Stop the clock
263
                        rd_valid <= 1'b0; // Output may have just been valid, but no more
264
                        o_busy  <= 1'b1; // Still busy till port is clear
265
                        state <= `EQSPI_STOP_B;
266
                        // Can't change modes for at least one cycle
267
                        // o_mod <= `EQSPI_MOD_SPI;
268
                end else if (state == `EQSPI_STOP_B)
269
                begin
270
                        o_cs_n <= 1'b1;
271
                        o_sck <= 1'b1;
272
                        // Do I need this????
273
                        // spi_len <= 3; // Minimum CS high time before next cmd
274
                        state <= `EQSPI_RECYCLE;
275
                        o_busy <= 1'b1;
276
                        o_mod <= `EQSPI_MOD_SPI;
277
                end else begin // Recycle state
278
                        r_recycle <= r_recycle - 1'b1;
279
                        o_cs_n <= 1'b1;
280
                        o_sck <= 1'b1;
281
                        o_busy <= 1'b1;
282
                        o_mod <= `EQSPI_MOD_SPI;
283
                        o_dat <= 4'hc;
284
                        if (r_recycle[3:1] == 3'h0)
285
                                state <= `EQSPI_IDLE;
286
                end
287
                /*
288
                end else begin // Invalid states, should never get here
289
                        state   <= `EQSPI_STOP;
290
                        o_valid <= 1'b0;
291
                        o_busy  <= 1'b1;
292
                        o_cs_n  <= 1'b1;
293
                        o_sck   <= 1'b1;
294
                        o_mod   <= `EQSPI_MOD_SPI;
295
                        o_dat   <= 4'hd;
296
                end
297
                */
298
        end
299
 
300 12 dgisselq
`define EXTRA_DELAY
301 13 dgisselq
        wire    rd_input_N, rd_valid_N, r_spd_N;
302 12 dgisselq
`ifdef EXTRA_DELAY
303 13 dgisselq
        reg     [2:0]    rd_input_p, rd_valid_p, r_spd_p;
304 3 dgisselq
        always @(posedge i_clk)
305 13 dgisselq
                rd_input_p <= { rd_input_p[1:0], rd_input };
306 12 dgisselq
        always @(posedge i_clk)
307 13 dgisselq
                rd_valid_p <= { rd_valid_p[1:0], rd_valid };
308 12 dgisselq
        always @(posedge i_clk)
309 13 dgisselq
                r_spd_p <= { r_spd_p[1:0], r_spd };
310
 
311
        assign  rd_input_N = rd_input_p[2];
312
        assign  rd_valid_N = rd_valid_p[2];
313
        assign  r_spd_N = r_spd_p[2];
314 12 dgisselq
`else
315 13 dgisselq
        assign  rd_input_N = rd_input;
316
        assign  rd_valid_N = rd_valid;
317
        assign  r_spd_N    = rd_spd;
318 12 dgisselq
`endif
319
 
320
 
321
        always @(posedge i_clk)
322 3 dgisselq
        begin
323 13 dgisselq
                // if ((state == `EQSPI_IDLE)||(rd_valid_N))
324
                if (o_valid)
325 3 dgisselq
                        r_input <= 31'h00;
326 13 dgisselq
                if ((rd_input_N)&&(r_spd_N))
327 3 dgisselq
                        r_input <= { r_input[26:0], i_dat };
328 13 dgisselq
                else if (rd_input_N)
329 3 dgisselq
                        r_input <= { r_input[29:0], i_miso };
330
 
331 13 dgisselq
                if ((rd_valid_N)&&(r_spd_N))
332 3 dgisselq
                        o_word  <= { r_input[27:0], i_dat };
333 13 dgisselq
                else if (rd_valid_N)
334 3 dgisselq
                        o_word  <= { r_input[30:0], i_miso };
335 13 dgisselq
                o_valid <= rd_valid_N;
336 3 dgisselq
        end
337
 
338
endmodule
339
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.