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

Subversion Repositories qspiflash

[/] [qspiflash/] [trunk/] [rtl/] [llqspi.v] - Blame information for rev 4

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

Line No. Rev Author Line
1 3 dgisselq
///////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    llqspi.v
4
//
5 3 dgisselq
// Project:     Wishbone Controlled Quad SPI Flash Controller
6 2 dgisselq
//
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 Tecnology, LLC
15
//
16 3 dgisselq
///////////////////////////////////////////////////////////////////////////
17 2 dgisselq
//
18 3 dgisselq
// Copyright (C) 2015, Gisselquist Technology, LLC
19 2 dgisselq
//
20 3 dgisselq
// 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
///////////////////////////////////////////////////////////////////////////
40 4 dgisselq
`define QSPI_IDLE       3'h0
41
`define QSPI_START      3'h1
42
`define QSPI_BITS       3'h2
43
`define QSPI_READY      3'h3
44
`define QSPI_HOLDING    3'h4
45
`define QSPI_STOP       3'h5
46
`define QSPI_STOP_B     3'h6
47 2 dgisselq
 
48
// Modes
49
`define QSPI_MOD_SPI    2'b00
50
`define QSPI_MOD_QOUT   2'b10
51
`define QSPI_MOD_QIN    2'b11
52
 
53
module  llqspi(i_clk,
54
                // Module interface
55
                i_wr, i_hold, i_word, i_len, i_spd, i_dir,
56
                        o_word, o_valid, o_busy,
57
                // QSPI interface
58 3 dgisselq
                o_sck, o_cs_n, o_mod, o_dat, i_dat);
59 2 dgisselq
        input                   i_clk;
60
        // Chip interface
61
        //      Can send info
62
        //              i_dir = 1, i_spd = 0, i_hold = 0, i_wr = 1,
63
        //                      i_word = { 1'b0, 32'info to send },
64
        //                      i_len = # of bytes in word-1
65
        input                   i_wr, i_hold;
66
        input           [31:0]   i_word;
67
        input           [1:0]    i_len;  // 0=>8bits, 1=>16 bits, 2=>24 bits, 3=>32 bits
68
        input                   i_spd; // 0 -> normal QPI, 1 -> QSPI
69
        input                   i_dir; // 0 -> read, 1 -> write to SPI
70
        output  reg     [31:0]   o_word;
71
        output  reg             o_valid, o_busy;
72
        // Interface with the QSPI lines
73
        output  reg             o_sck;
74
        output  reg             o_cs_n;
75
        output  reg     [1:0]    o_mod;
76
        output  reg     [3:0]    o_dat;
77
        input           [3:0]    i_dat;
78
 
79
        // Timing:
80
        //
81
        //      Tick    Clk     BSY/WR  CS_n    BIT/MO  STATE
82
        //       0      1       0/0     1        -      
83
        //       1      1       0/1     1        -
84
        //       2      1       1/0     0         -      QSPI_START
85
        //       3      0        1/0     0         -      QSPI_START
86
        //       4      0        1/0     0         0      QSPI_BITS
87
        //       5      1       1/0     0         0      QSPI_BITS
88
        //       6      0        1/0     0         1      QSPI_BITS
89
        //       7      1       1/0     0         1      QSPI_BITS
90
        //       8      0        1/0     0         2      QSPI_BITS
91
        //       9      1       1/0     0         2      QSPI_BITS
92
        //      10      0        1/0     0         3      QSPI_BITS
93
        //      11      1       1/0     0         3      QSPI_BITS
94
        //      12      0        1/0     0         4      QSPI_BITS
95
        //      13      1       1/0     0         4      QSPI_BITS
96
        //      14      0        1/0     0         5      QSPI_BITS
97
        //      15      1       1/0     0         5      QSPI_BITS
98
        //      16      0        1/0     0         6      QSPI_BITS
99
        //      17      1       1/1     0         6      QSPI_BITS
100
        //      18      0        1/1     0         7      QSPI_READY
101
        //      19      1       0/1     0         7      QSPI_READY
102
        //      20      0        1/0/V   0         8      QSPI_BITS
103
        //      21      1       1/0     0         8      QSPI_BITS
104
        //      22      0        1/0     0         9      QSPI_BITS
105
        //      23      1       1/0     0         9      QSPI_BITS
106
        //      24      0        1/0     0        10      QSPI_BITS
107
        //      25      1       1/0     0        10      QSPI_BITS
108
        //      26      0        1/0     0        11      QSPI_BITS
109
        //      27      1       1/0     0        11      QSPI_BITS
110
        //      28      0        1/0     0        12      QSPI_BITS
111
        //      29      1       1/0     0        12      QSPI_BITS
112
        //      30      0        1/0     0        13      QSPI_BITS
113
        //      31      1       1/0     0        13      QSPI_BITS
114
        //      32      0        1/0     0        14      QSPI_BITS
115
        //      33      1       1/0     0        14      QSPI_BITS
116
        //      34      0        1/0     0        15      QSPI_READY
117
        //      35      1       1/0     0        15      QSPI_READY
118
        //      36      1       1/0/V   0         -      QSPI_STOP
119
        //      37      1       1/0     0         -      QSPI_STOPB
120
        //      38      1       1/0     1        -      QSPI_IDLE
121
        //      39      1       0/0     1        -
122
        // Now, let's switch from single bit to quad mode
123
        //      40      1       0/0     1        -      QSPI_IDLE
124
        //      41      1       0/1     1        -      QSPI_IDLE
125
        //      42      1       1/0     0         -      QSPI_START
126
        //      43      0        1/0     0         -      QSPI_START
127
        //      44      0        1/0     0         0      QSPI_BITS
128
        //      45      1       1/0     0         0      QSPI_BITS
129
        //      46      0        1/0     0         1      QSPI_BITS
130
        //      47      1       1/0     0         1      QSPI_BITS
131
        //      48      0        1/0     0         2      QSPI_BITS
132
        //      49      1       1/0     0         2      QSPI_BITS
133
        //      50      0        1/0     0         3      QSPI_BITS
134
        //      51      1       1/0     0         3      QSPI_BITS
135
        //      52      0        1/0     0         4      QSPI_BITS
136
        //      53      1       1/0     0         4      QSPI_BITS
137
        //      54      0        1/0     0         5      QSPI_BITS
138
        //      55      1       1/0     0         5      QSPI_BITS
139
        //      56      0        1/0     0         6      QSPI_BITS
140
        //      57      1       1/1/QR  0         6      QSPI_BITS
141
        //      58      0        1/1/QR  0         7      QSPI_READY
142
        //      59      1       0/1/QR  0         7      QSPI_READY
143
        //      60      0        1/0/?/V 0         8-11   QSPI_BITS
144
        //      61      1       1/0/?   0         8-11   QSPI_BITS
145
        //      62      0        1/0/?   0         12-15  QSPI_BITS
146
        //      63      1       1/0/?   0         12-15  QSPI_BITS
147
        //      64      1       1/0/?/V 0        -       QSPI_STOP
148
        //      65      1       1/0/?   0        -       QSPI_STOPB
149
        //      66      1       1/0/?   1       -       QSPI_IDLE
150
        //      67      1       0/0     1       -       QSPI_IDLE
151
        // Now let's try something entirely in Quad read mode, from the
152
        // beginning
153
        //      68      1       0/1/QR  1       -       QSPI_IDLE
154
        //      69      1       1/0     0        -       QSPI_START
155
        //      70      0        1/0     0        -       QSPI_START
156
        //      71      0        1/0     0        0-3     QSPI_BITS
157
        //      72      1       1/0     0        0-3     QSPI_BITS
158
        //      73      0        1/1/QR  0        4-7     QSPI_BITS
159
        //      74      1       0/1/QR  0        4-7     QSPI_BITS
160
        //      75      0        1/?/?/V 0        8-11    QSPI_BITS
161
        //      76      1       1/?/?   0        8-11    QSPI_BITS
162
        //      77      0        1/1/QR  0        12-15   QSPI_BITS
163
        //      78      1       0/1/QR  0        12-15   QSPI_BITS
164
        //      79      0        1/?/?/V 0        16-19   QSPI_BITS
165
        //      80      1       1/0     0        16-19   QSPI_BITS
166
        //      81      0        1/0     0        20-23   QSPI_BITS
167
        //      82      1       1/0     0        20-23   QSPI_BITS
168
        //      83      1       1/0/V   0        -       QSPI_STOP
169
        //      84      1       1/0     0        -       QSPI_STOPB
170
        //      85      1       1/0     1       -       QSPI_IDLE
171
        //      86      1       0/0     1       -       QSPI_IDLE
172
 
173
        wire    i_miso;
174
        assign  i_miso = i_dat[1];
175
 
176
        reg             r_spd, r_dir;
177
        reg     [5:0]    spi_len;
178
        reg     [31:0]   r_word;
179
        reg     [30:0]   r_input;
180
        reg     [2:0]    state;
181
        initial state = `QSPI_IDLE;
182
        initial o_sck   = 1'b1;
183
        initial o_cs_n  = 1'b1;
184
        initial o_dat   = 4'hd;
185
        initial o_valid = 1'b0;
186
        initial o_busy  = 1'b0;
187
        initial r_input = 31'h000;
188
        always @(posedge i_clk)
189
                if ((state == `QSPI_IDLE)&&(o_sck))
190
                begin
191
                        o_cs_n <= 1'b1;
192
                        o_valid <= 1'b0;
193
                        o_busy  <= 1'b0;
194
                        o_mod <= `QSPI_MOD_SPI;
195
                        if (i_wr)
196
                        begin
197
                                r_word <= i_word;
198
                                state <= `QSPI_START;
199
                                r_spd <= i_spd;
200
                                r_dir <= i_dir;
201
                                spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8;
202
                                o_cs_n <= 1'b0;
203
                                o_busy <= 1'b1;
204
                                o_sck <= 1'b1;
205
                        end
206
                end else if (state == `QSPI_START)
207
                begin // We come in here with sck high, stay here 'til sck is low
208
                        o_sck <= 1'b0;
209
                        if (o_sck == 1'b0)
210
                        begin
211
                                state <= `QSPI_BITS;
212
                                spi_len<= spi_len - ( (r_spd)? 6'h4 : 6'h1 );
213
                                if (r_spd)
214
                                        r_word <= { r_word[27:0], 4'h0 };
215
                                else
216
                                        r_word <= { r_word[30:0], 1'b0 };
217
                        end
218
                        o_mod <= (r_spd) ? { 1'b1, r_dir } : `QSPI_MOD_SPI;
219
                        o_cs_n <= 1'b0;
220
                        o_busy <= 1'b1;
221
                        o_valid <= 1'b0;
222
                        if (r_spd)
223
                        begin
224
                                o_dat <= r_word[31:28];
225
                                // r_word <= { r_word[27:0], 4'h0 };
226
                        end else begin
227
                                o_dat <= { 3'b110, r_word[31] };
228
                                // r_word <= { r_word[30:0], 1'b0 };
229
                        end
230
                end else if (~o_sck)
231
                begin
232
                        o_sck <= 1'b1;
233
                        o_busy <= ((state != `QSPI_READY)||(~i_wr));
234
                        o_valid <= 1'b0;
235
                end else if (state == `QSPI_BITS)
236
                begin
237
                        // Should enter into here with at least a spi_len
238
                        // of one, perhaps more
239
                        o_sck <= 1'b0;
240
                        o_busy <= 1'b1;
241
                        if (r_spd)
242
                        begin
243
                                o_dat <= r_word[31:28];
244
                                r_word <= { r_word[27:0], 4'h0 };
245
                                spi_len <= spi_len - 6'h4;
246
                                if (spi_len == 6'h4)
247
                                        state <= `QSPI_READY;
248
                        end else begin
249
                                o_dat <= { 3'b110, r_word[31] };
250
                                r_word <= { r_word[30:0], 1'b0 };
251
                                spi_len <= spi_len - 6'h1;
252
                                if (spi_len == 6'h1)
253
                                        state <= `QSPI_READY;
254
                        end
255
 
256
                        o_valid <= 1'b0;
257
                        if (~o_mod[1])
258
                                r_input <= { r_input[29:0], i_miso };
259
                        else if (o_mod[1])
260
                                r_input <= { r_input[26:0], i_dat };
261
                end else if (state == `QSPI_READY)
262
                begin
263
                        o_valid <= 1'b0;
264
                        o_cs_n <= 1'b0;
265
                        o_busy <= 1'b1;
266
                        // This is the state on the last clock (both low and
267
                        // high clocks) of the data.  Data is valid during
268
                        // this state.  Here we chose to either STOP or
269
                        // continue and transmit more.
270 4 dgisselq
                        o_sck <= (i_hold); // No clocks while holding
271 2 dgisselq
                        if((~o_busy)&&(i_wr))// Acknowledge a new request
272
                        begin
273
                                state <= `QSPI_BITS;
274
                                o_busy <= 1'b1;
275
                                o_sck <= 1'b0;
276
 
277
                                // Read the new request off the bus
278
                                r_spd <= i_spd;
279
                                r_dir <= i_dir;
280
                                // Set up the first bits on the bus
281
                                o_mod <= (i_spd) ? { 1'b1, i_dir } : `QSPI_MOD_SPI;
282
                                if (i_spd)
283
                                begin
284
                                        o_dat <= i_word[31:28];
285
                                        r_word <= { i_word[27:0], 4'h0 };
286
                                        // spi_len <= spi_len - 4;
287
                                        spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8
288
                                                - 6'h4;
289
                                end else begin
290
                                        o_dat <= { 3'b110, i_word[31] };
291
                                        r_word <= { i_word[30:0], 1'b0 };
292
                                        spi_len<= { 1'b0, i_len, 3'b000 } + 6'h8
293
                                                - 6'h1;
294
                                end
295
 
296
                                // Read a bit upon any transition
297
                                o_valid <= 1'b1;
298
                                if (~o_mod[1])
299
                                begin
300
                                        r_input <= { r_input[29:0], i_miso };
301
                                        o_word  <= { r_input[30:0], i_miso };
302
                                end else if (o_mod[1])
303
                                begin
304
                                        r_input <= { r_input[26:0], i_dat };
305
                                        o_word  <= { r_input[27:0], i_dat };
306
                                end
307
                        end else begin
308
                                o_sck <= 1'b1;
309 4 dgisselq
                                state <= (i_hold)?`QSPI_HOLDING : `QSPI_STOP;
310
                                o_busy <= (~i_hold);
311 2 dgisselq
 
312
                                // Read a bit upon any transition
313
                                o_valid <= 1'b1;
314
                                if (~o_mod[1])
315
                                begin
316
                                        r_input <= { r_input[29:0], i_miso };
317
                                        o_word  <= { r_input[30:0], i_miso };
318
                                end else if (o_mod[1])
319
                                begin
320
                                        r_input <= { r_input[26:0], i_dat };
321
                                        o_word  <= { r_input[27:0], i_dat };
322
                                end
323
                        end
324 4 dgisselq
                end else if (state == `QSPI_HOLDING)
325
                begin
326
                        // We need this state so that the o_valid signal
327
                        // can get strobed with our last result.  Otherwise
328
                        // we could just sit in READY waiting for a new command.
329
                        //
330
                        // Incidentally, the change producing this state was
331
                        // the result of a nasty race condition.  See the
332
                        // commends in wbqspiflash for more details.
333
                        //
334
                        o_valid <= 1'b0;
335
                        o_cs_n <= 1'b0;
336
                        o_busy <= 1'b0;
337
                        if((~o_busy)&&(i_wr))// Acknowledge a new request
338
                        begin
339
                                state  <= `QSPI_BITS;
340
                                o_busy <= 1'b1;
341
                                o_sck  <= 1'b0;
342
 
343
                                // Read the new request off the bus
344
                                r_spd <= i_spd;
345
                                r_dir <= i_dir;
346
                                // Set up the first bits on the bus
347
                                o_mod<=(i_spd)?{ 1'b1, i_dir } : `QSPI_MOD_SPI;
348
                                if (i_spd)
349
                                begin
350
                                        o_dat <= i_word[31:28];
351
                                        r_word <= { i_word[27:0], 4'h0 };
352
                                        spi_len<= { 1'b0, i_len, 3'b100 };
353
                                end else begin
354
                                        o_dat <= { 3'b110, i_word[31] };
355
                                        r_word <= { i_word[30:0], 1'b0 };
356
                                        spi_len<= { 1'b0, i_len, 3'b111 };
357
                                end
358
                        end else begin
359
                                o_sck <= 1'b1;
360
                                state <= (i_hold)?`QSPI_HOLDING : `QSPI_STOP;
361
                                o_busy <= (~i_hold);
362
                        end
363 2 dgisselq
                end else if (state == `QSPI_STOP)
364
                begin
365
                        o_sck   <= 1'b1; // Stop the clock
366
                        o_valid <= 1'b0; // Output may have just been valid, but no more
367
                        o_busy  <= 1'b1; // Still busy till port is clear
368
                        state <= `QSPI_STOP_B;
369
                        o_mod <= `QSPI_MOD_SPI;
370
                end else if (state == `QSPI_STOP_B)
371
                begin
372
                        o_cs_n <= 1'b1;
373
                        o_sck <= 1'b1;
374
                        // Do I need this????
375
                        // spi_len <= 3; // Minimum CS high time before next cmd
376
                        state <= `QSPI_IDLE;
377
                        o_valid <= 1'b0;
378
                        o_busy <= 1'b1;
379
                        o_mod <= `QSPI_MOD_SPI;
380
                end else begin // Invalid states, should never get here
381
                        state   <= `QSPI_STOP;
382
                        o_valid <= 1'b0;
383
                        o_busy  <= 1'b1;
384
                        o_cs_n  <= 1'b1;
385
                        o_sck   <= 1'b1;
386
                        o_mod   <= `QSPI_MOD_SPI;
387
                        o_dat   <= 4'hd;
388
                end
389
 
390
endmodule
391
 

powered by: WebSVN 2.1.0

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