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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [wbspiflash.v] - Blame information for rev 78

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

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbspiflash.v
4
//
5
// Project:     XuLA2 board
6
//
7
// Purpose:     Access a SPI flash via a WISHBONE interface.  This
8
//              includes both read and write (and erase) commands to the SPI
9
//              flash.  All read/write commands are accomplished using the
10
//              high speed (4-bit) interface.  Further, the device will be
11
//              left/kept in the 4-bit read interface mode between accesses,
12
//              for a minimum read latency.
13
//
14
//      Wishbone Registers (See spec sheet for more detail):
15
//      0: local config(r) / erase commands(w) / deep power down cmds / etc.
16
//      R: (Write in Progress), (dirty-block), (spi_port_busy), 1'b0, 9'h00,
17
//              { last_erased_sector, 14'h00 } if (WIP)
18
//              else { current_sector_being_erased, 14'h00 }
19
//              current if write in progress, last if written
20
//      W: (1'b1 to erase), (12'h ignored), next_erased_block, 14'h ignored)
21
//      1: Configuration register
22
//      2: Status register (R/w)
23
//      3: Read ID (read only)
24
//      (19 bits): Data (R/w, but expect writes to take a while)
25
//              
26
//
27
// Creator:     Dan Gisselquist
28
//              Gisselquist Technology, LLC
29
//
30
///////////////////////////////////////////////////////////////////////////
31
//
32
// Copyright (C) 2015, Gisselquist Technology, LLC
33
//
34
// This program is free software (firmware): you can redistribute it and/or
35
// modify it under the terms of  the GNU General Public License as published
36
// by the Free Software Foundation, either version 3 of the License, or (at
37
// your option) any later version.
38
//
39
// This program is distributed in the hope that it will be useful, but WITHOUT
40
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
41
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
42
// for more details.
43
//
44
// You should have received a copy of the GNU General Public License along
45
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
46
// target there if the PDF file isn't present.)  If not, see
47
// <http://www.gnu.org/licenses/> for a copy.
48
//
49
// License:     GPL, v3, as defined and found on www.gnu.org,
50
//              http://www.gnu.org/licenses/gpl.html
51
//
52
//
53
///////////////////////////////////////////////////////////////////////////
54
`define WBSPI_RESET             6'd0
55
// `define      WBSPI_RESET_QUADMODE    1
56
`define WBSPI_IDLE              6'd2
57
// `define      WBQSPI_RDIDLE           3       // Idle, but in fast read mode
58
// `define      WBSPI_WBDECODE          4
59
`define WBSPI_WAIT_WIP_CLEAR    6'd5
60
`define WBSPI_CHECK_WIP_CLEAR   6'd6
61
`define WBSPI_CHECK_WIP_DONE    6'd7
62
`define WBSPI_WEN               6'd8
63
`define WBSPI_PP                6'd9    // Program page
64
// `define      WBSPI_QPP               10      // Program page, 4 bit mode
65
`define WBSPI_WR_DATA           6'd11
66
`define WBSPI_WR_BUS_CYCLE      6'd12
67
`define WBSPI_RD_DUMMY          6'd13
68
// `define      WBSPI_QRD_ADDRESS       14
69
// `define      WBSPI_QRD_DUMMY 15
70
`define WBSPI_READ_CMD          6'd16
71
`define WBSPI_READ_DATA         6'd17
72
//`define       WBSPI_WAIT_TIL_RDIDLE   18
73
`define WBSPI_READ_ID_CMD       6'd19
74
`define WBSPI_READ_ID           6'd20
75
`define WBSPI_READ_STATUS       6'd21
76
`define WBSPI_READ_CONFIG       6'd22
77
`define WBSPI_WRITE_STATUS      6'd23
78
`define WBSPI_WRITE_CONFIG      6'd24
79
`define WBSPI_ERASE_WEN         6'd25
80
`define WBSPI_ERASE_CMD         6'd26
81
`define WBSPI_ERASE_BLOCK       6'd27
82
`define WBSPI_CLEAR_STATUS      6'd28
83
`define WBSPI_IDLE_CHECK_WIP    6'd29
84
`define WBSPI_WAIT_TIL_IDLE     6'd30
85
//
86
module  wbspiflash(i_clk_100mhz,
87
                // Internal wishbone connections
88
                i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we,
89
                i_wb_addr, i_wb_data,
90
                // Wishbone return values
91
                o_wb_ack, o_wb_stall, o_wb_data,
92
                // Quad Spi connections to the external device
93
                o_spi_sck, o_spi_cs_n, i_spi_cs_n, o_spi_mosi, i_spi_miso,
94 74 dgisselq
                o_interrupt,
95
                i_bus_grant);
96 2 dgisselq
        parameter       AW=18, // Address width, -2 for 32-bit word access
97
                        PW=6,   // Page address width (256 bytes,64 words)
98
                        SW=10;  // Sector address width (4kB, 1kW)
99
        // parameter    AW, PW, Sw; // Address, page, and sector width(s)
100
        input                   i_clk_100mhz;
101
        // Wishbone, inputs first
102
        input                   i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we;
103
        input   [(AW-1):0]       i_wb_addr;
104
        input           [31:0]   i_wb_data;
105
        // then outputs
106
        output  reg             o_wb_ack;
107
        output  reg             o_wb_stall;
108
        output  reg     [31:0]   o_wb_data;
109
        // Quad SPI control wires
110
        output  wire            o_spi_sck;
111
        output  wire            o_spi_cs_n;
112
        input                   i_spi_cs_n;
113
        output  wire            o_spi_mosi;
114
        input                   i_spi_miso;
115
        // Interrupt line
116
        output  reg             o_interrupt;
117 74 dgisselq
        // Do we own the bus?
118
        input                   i_bus_grant;
119
 
120 2 dgisselq
        // output       wire    [31:0]  o_debug;
121
 
122
        reg             spi_wr, spi_hold;
123
        reg     [31:0]   spi_in;
124
        reg     [1:0]    spi_len;
125
        wire    [31:0]   spi_out;
126
        wire            spi_valid, spi_busy;
127
        wire            w_spi_sck, w_spi_cs_n;
128
        wire            w_spi_mosi;
129
        // wire [22:0]  spi_dbg;
130
        lldspi  lldriver(i_clk_100mhz,
131
                        spi_wr, spi_hold, spi_in, spi_len,
132
                                spi_out, spi_valid, spi_busy,
133
                        w_spi_sck, w_spi_cs_n, i_spi_cs_n, w_spi_mosi,
134 74 dgisselq
                                i_spi_miso,
135
                        i_bus_grant);
136 2 dgisselq
 
137
        // Erase status tracking
138
        reg             write_in_progress, write_protect;
139
        reg [(AW-1-SW):0] erased_sector;
140
        reg             dirty_sector;
141
        initial begin
142
                write_in_progress = 1'b0;
143
                erased_sector = 0;
144
                dirty_sector  = 1'b1;
145
                write_protect = 1'b1;
146
        end
147
 
148
        reg     [7:0]    last_status;
149
        reg             spif_cmd, spif_override;
150
        reg [(AW-1):0]   spif_addr;
151
        reg     [31:0]   spif_data;
152
        reg     [5:0]    state;
153
        reg             spif_ctrl, spif_req;
154
        wire    [(AW-1-SW):0]    spif_sector;
155
        assign  spif_sector = spif_addr[(AW-1):SW];
156
 
157
        // assign       o_debug = { spi_wr, spi_hold, state, spi_dbg };
158
 
159
        initial state = `WBSPI_RESET;
160
        initial o_wb_ack   = 1'b0;
161
        initial o_wb_stall = 1'b1;
162
        initial spi_wr     = 1'b0;
163
        initial spi_len    = 2'b00;
164
        initial o_interrupt= 1'b0;
165
        always @(posedge i_clk_100mhz)
166
        begin
167
        if (state == `WBSPI_RESET)
168
        begin
169
                // From a reset, we should
170
                //      Enable the Quad I/O mode
171
                //      Disable the Write protection bits in the status register
172
                //      Chip should already be up and running, so we can start
173
                //      immediately ....
174
                o_wb_ack <= 1'b0;
175
                o_wb_stall <= 1'b1;
176
                spi_wr   <= 1'b0;
177
                spi_hold <= 1'b0;
178
                last_status <= 8'h00;
179
                state <= `WBSPI_IDLE;
180
                spif_req <= 1'b0;
181
        end else if (state == `WBSPI_IDLE)
182
        begin
183
                o_interrupt <= 1'b0;
184
                o_wb_stall <= 1'b0;
185
                o_wb_ack <= 1'b0;
186
                spif_cmd   <= i_wb_we;
187
                spif_addr  <= i_wb_addr;
188
                spif_data  <= i_wb_data;
189
                spif_ctrl  <= (i_wb_ctrl_stb)&&(~i_wb_data_stb);
190
                spif_req   <= (i_wb_ctrl_stb)||(i_wb_data_stb);
191
                spi_wr   <= 1'b0; // Keep the port idle, unless told otherwise
192
                spi_hold <= 1'b0;
193
                // Data register access
194
                if ((i_wb_data_stb)&&(i_wb_cyc))
195
                begin
196
 
197
                        if (i_wb_we) // Request to write a page
198
                        begin
199
                                if((~write_protect)&&(~write_in_progress))
200
                                begin // 00
201
                                        spi_wr <= 1'b1;
202
                                        spi_len <= 2'b00; // 8 bits
203
                                        // Send a write enable command
204
                                        spi_in <= { 8'h06, 24'h00 };
205
                                        state <= `WBSPI_WEN;
206
 
207
                                        o_wb_ack <= 1'b0;
208
                                        o_wb_stall <= 1'b1;
209
                                end else if (write_protect)
210
                                begin // whether or not write-in_progress ...
211
                                        // Do nothing on a write protect
212
                                        // violation
213
                                        //
214
                                        o_wb_ack <= 1'b1;
215
                                        o_wb_stall <= 1'b0;
216
                                end else begin // write is in progress, wait
217
                                        // for it to complete
218
                                        state <= `WBSPI_WAIT_WIP_CLEAR;
219
                                        o_wb_ack <= 1'b0;
220
                                        o_wb_stall <= 1'b1;
221
                                end
222
                        end else if (~write_in_progress)
223
                        begin // Read access, normal mode(s)
224
                                o_wb_ack   <= 1'b0;
225
                                o_wb_stall <= 1'b1;
226
                                spi_wr     <= 1'b1;     // Write cmd to device
227
                                spi_in <= { 8'h0b,
228
                                        {(22-AW){1'b0}},
229
                                        i_wb_addr[(AW-1):0], 2'b00 };
230
                                state <= `WBSPI_RD_DUMMY;
231
                                spi_len    <= 2'b11; // cmd+addr,32bits
232
                        end else begin
233
                                // A write is in progress ... need to stall
234
                                // the bus until the write is complete.
235
                                state <= `WBSPI_WAIT_WIP_CLEAR;
236
                                o_wb_ack   <= 1'b0;
237
                                o_wb_stall <= 1'b1;
238
                        end
239
                end else if ((i_wb_cyc)&&(i_wb_ctrl_stb)&&(i_wb_we))
240
                begin
241
                        o_wb_stall <= 1'b1;
242
                        case(i_wb_addr[1:0])
243
                        2'b00: begin // Erase command register
244
                                write_protect <= ~i_wb_data[28];
245
                                o_wb_stall <= 1'b0;
246
 
247
                                if((i_wb_data[31])&&(~write_in_progress))
248
                                begin
249
                                        // Command an erase--ack it immediately
250
 
251
                                        o_wb_ack <= 1'b1;
252
                                        o_wb_stall <= 1'b0;
253
 
254
                                        if ((i_wb_data[31])&&(~write_protect))
255
                                        begin
256
                                                spi_wr <= 1'b1;
257
                                                spi_len <= 2'b00;
258
                                                // Send a write enable command
259
                                                spi_in <= { 8'h06, 24'h00 };
260
                                                state <= `WBSPI_ERASE_CMD;
261
                                                o_wb_stall <= 1'b1;
262
                                        end
263
                                end else if (i_wb_data[31])
264
                                begin
265
                                        state <= `WBSPI_WAIT_WIP_CLEAR;
266
                                        o_wb_ack   <= 1'b1;
267
                                        o_wb_stall <= 1'b1;
268
                                end else
269
                                        o_wb_ack   <= 1'b1;
270
                                        o_wb_stall <= 1'b0;
271
                                end
272
                        2'b01: begin
273
                                /*
274
                                // Write the configuration register
275
                                o_wb_ack <= 1'b1;
276
                                o_wb_stall <= 1'b1;
277
 
278
                                // Need to send a write enable command first
279
                                spi_wr <= 1'b1;
280
                                spi_len <= 2'b00; // 8 bits
281
                                // Send a write enable command
282
                                spi_in <= { 8'h06, 24'h00 };
283
                                state <= `WBSPI_WRITE_CONFIG;
284
                                */
285
                                o_wb_ack <= 1'b1; // Ack immediately
286
                                o_wb_stall <= 1'b0; // No stall, but do nothing
287
                                end
288
                        2'b10: begin
289
                                /*
290
                                // Write the status register
291
                                o_wb_ack <= 1'b1; // Ack immediately
292
                                o_wb_stall <= 1'b1; // Stall other cmds
293
                                // Need to send a write enable command first
294
                                spi_wr <= 1'b1;
295
                                spi_len <= 2'b00; // 8 bits
296
                                // Send a write enable command
297
                                spi_in <= { 8'h06, 24'h00 };
298
                                state <= `WBSPI_WRITE_STATUS;
299
                                */
300
                                o_wb_ack <= 1'b1; // Ack immediately
301
                                o_wb_stall <= 1'b0; // No stall, but do nothing
302
                                end
303
                        2'b11: begin // Write the ID register??? makes no sense
304
                                o_wb_ack <= 1'b1;
305
                                o_wb_stall <= 1'b0;
306
                                end
307
                        endcase
308
                end else if ((i_wb_cyc)&&(i_wb_ctrl_stb)) // &&(~i_wb_we))
309
                begin
310
                        case(i_wb_addr[1:0])
311
                        2'b00: begin // Read local register
312
                                if (write_in_progress) // Read status
313
                                begin// register, is write still in progress?
314
                                        state <= `WBSPI_READ_STATUS;
315
                                        spi_wr <= 1'b1;
316
                                        spi_len <= 2'b01;// 8 bits out, 8 bits in
317
                                        spi_in <= { 8'h05, 24'h00};
318
 
319
                                        o_wb_ack <= 1'b0;
320
                                        o_wb_stall <= 1'b1;
321
                                end else begin // Return w/o talking to device
322
                                        o_wb_ack <= 1'b1;
323
                                        o_wb_stall <= 1'b0;
324
                                        o_wb_data <= { write_in_progress,
325
                                                dirty_sector, spi_busy,
326
                                                ~write_protect,
327
                                                {(28-AW){1'b0}},
328
                                                erased_sector, {(SW){1'b0}} };
329
                                end end
330
                        2'b01: begin // Read configuration register
331
                                state <= `WBSPI_READ_CONFIG;
332
                                spi_wr <= 1'b1;
333
                                spi_len <= 2'b01;
334
                                spi_in <= { 8'h35, 24'h00};
335
 
336
                                o_wb_ack <= 1'b0;
337
                                o_wb_stall <= 1'b1;
338
                                end
339
                        2'b10: begin // Read status register
340
                                state <= `WBSPI_READ_STATUS;
341
                                spi_wr <= 1'b1;
342
                                spi_len <= 2'b01; // 8 bits out, 8 bits in
343
                                spi_in <= { 8'h05, 24'h00};
344
 
345
                                o_wb_ack <= 1'b0;
346
                                o_wb_stall <= 1'b1;
347
                                end
348
                        2'b11: begin // Read ID register
349
                                state <= `WBSPI_READ_ID_CMD;
350
                                spi_wr <= 1'b1;
351
                                spi_len <= 2'b00;
352
                                spi_in <= { 8'h9f, 24'h00};
353
 
354
                                o_wb_ack <= 1'b0;
355
                                o_wb_stall <= 1'b1;
356
                                end
357
                        endcase
358
                end else if ((~i_wb_cyc)&&(write_in_progress))
359
                begin
360
                        state <= `WBSPI_IDLE_CHECK_WIP;
361
                        spi_wr <= 1'b1;
362
                        spi_len <= 2'b01; // 8 bits out, 8 bits in
363
                        spi_in <= { 8'h05, 24'h00};
364
 
365
                        o_wb_ack <= 1'b0;
366
                        o_wb_stall <= 1'b1;
367
                end
368
        end else if (state == `WBSPI_WAIT_WIP_CLEAR)
369
        begin
370
                o_wb_stall <= 1'b1;
371
                o_wb_ack   <= 1'b0;
372
                spi_wr <= 1'b0;
373
                spif_req<= (spif_req) && (i_wb_cyc);
374
                if (~spi_busy)
375
                begin
376
                        spi_wr   <= 1'b1;
377
                        spi_in   <= { 8'h05, 24'h0000 };
378
                        spi_hold <= 1'b1;
379
                        spi_len  <= 2'b01; // 16 bits write, so we can read 8
380
                        state <= `WBSPI_CHECK_WIP_CLEAR;
381
                end
382
        end else if (state == `WBSPI_CHECK_WIP_CLEAR)
383
        begin
384
                o_wb_stall <= 1'b1;
385
                o_wb_ack   <= 1'b0;
386
                // Repeat as often as necessary until we are clear
387
                spi_wr <= 1'b1;
388
                spi_in <= 32'h0000; // Values here are actually irrelevant
389
                spi_hold <= 1'b1;
390
                spi_len <= 2'b00; // One byte at a time
391
                spif_req<= (spif_req) && (i_wb_cyc);
392
                if ((spi_valid)&&(~spi_out[0]))
393
                begin
394
                        state <= `WBSPI_CHECK_WIP_DONE;
395
                        spi_wr   <= 1'b0;
396
                        spi_hold <= 1'b0;
397
                        write_in_progress <= 1'b0;
398
                        last_status <= spi_out[7:0];
399
                end
400
        end else if (state == `WBSPI_CHECK_WIP_DONE)
401
        begin
402
                o_wb_stall <= 1'b1;
403
                o_wb_ack   <= 1'b0;
404
                // Let's let the SPI port come back to a full idle,
405
                // and the chip select line go low before continuing
406
                spi_wr   <= 1'b0;
407
                spi_len  <= 2'b00;
408
                spi_hold <= 1'b0;
409
                spif_req<= (spif_req) && (i_wb_cyc);
410
                if ((o_spi_cs_n)&&(~spi_busy)) // Chip select line is high, we can continue
411
                begin
412
                        spi_wr   <= 1'b0;
413
                        spi_hold <= 1'b0;
414
 
415
                        casez({ spif_cmd, spif_ctrl, spif_addr[1:0] })
416
                        4'b00??: begin // Read data from ... somewhere
417
                                spi_wr     <= 1'b1;     // Write cmd to device
418
                                spi_in <= { 8'h0b, {(22-AW){1'b0}},
419
                                        spif_addr[(AW-1):0], 2'b00 };
420
                                state <= `WBSPI_RD_DUMMY;
421
                                spi_len    <= 2'b11; // Send cmd and addr
422
                                end
423
                        4'b10??: begin // Write data to ... anywhere
424
                                spi_wr <= 1'b1;
425
                                spi_len <= 2'b00; // 8 bits
426
                                // Send a write enable command
427
                                spi_in <= { 8'h06, 24'h00 };
428
                                state <= `WBSPI_WEN;
429
                                end
430
                        4'b0110: begin // Read status register
431
                                state <= `WBSPI_READ_STATUS;
432
                                spi_wr <= 1'b1;
433
                                spi_len <= 2'b01; // 8 bits out, 8 bits in
434
                                spi_in <= { 8'h05, 24'h00};
435
                                end
436
                        4'b0111: begin
437
                                state <= `WBSPI_READ_ID_CMD;
438
                                spi_wr <= 1'b1;
439
                                spi_len <= 2'b00;
440
                                spi_in <= { 8'h9f, 24'h00};
441
                                end
442
                        default: begin //
443
                                o_wb_stall <= 1'b1;
444
                                o_wb_ack <= spif_req;
445
                                state <= `WBSPI_WAIT_TIL_IDLE;
446
                                end
447
                        endcase
448
                // spif_cmd   <= i_wb_we;
449
                // spif_addr  <= i_wb_addr;
450
                // spif_data  <= i_wb_data;
451
                // spif_ctrl  <= (i_wb_ctrl_stb)&&(~i_wb_data_stb);
452
                // spi_wr <= 1'b0; // Keep the port idle, unless told otherwise
453
                end
454
        end else if (state == `WBSPI_WEN)
455
        begin // We came here after issuing a write enable command
456
                spi_wr <= 1'b0;
457
                o_wb_ack <= 1'b0;
458
                o_wb_stall <= 1'b1;
459
                spif_req<= (spif_req) && (i_wb_cyc);
460
                if ((~spi_busy)&&(o_spi_cs_n)&&(~spi_wr)) // Let's come to a full stop
461
                        state <= `WBSPI_PP;
462
        end else if (state == `WBSPI_PP)
463
        begin // We come here under a full stop / full port idle mode
464
                // Issue our command immediately
465
                spi_wr <= 1'b1;
466
                spi_in <= { 8'h02, {(22-AW){1'b0}}, spif_addr, 2'b00 };
467
                spi_len <= 2'b11;
468
                spi_hold <= 1'b1;
469
                spif_req<= (spif_req) && (i_wb_cyc);
470
 
471
                // Once we get busy, move on
472
                if (spi_busy)
473
                        state <= `WBSPI_WR_DATA;
474
                if (spif_sector == erased_sector)
475
                        dirty_sector <= 1'b1;
476
        end else if (state == `WBSPI_WR_DATA)
477
        begin
478
                o_wb_stall <= 1'b1;
479
                o_wb_ack   <= 1'b0;
480
                spi_wr   <= 1'b1; // write without waiting
481
                spi_in   <= {
482
                        spif_data[ 7: 0],
483
                        spif_data[15: 8],
484
                        spif_data[23:16],
485
                        spif_data[31:24] };
486
                spi_len  <= 2'b11; // Write 4 bytes
487
                spi_hold <= 1'b1;
488
                if (~spi_busy)
489
                begin
490
                        o_wb_ack <= spif_req; // Ack when command given
491
                        state <= `WBSPI_WR_BUS_CYCLE;
492
                end
493
                spif_req<= (spif_req) && (i_wb_cyc);
494
        end else if (state == `WBSPI_WR_BUS_CYCLE)
495
        begin
496
                o_wb_ack <= 1'b0; // Turn off our ack and stall flags
497
                o_wb_stall <= 1'b1;
498
                spi_wr <= 1'b0;
499
                spi_hold <= 1'b1;
500
                write_in_progress <= 1'b1;
501
                spif_req<= (spif_req) && (i_wb_cyc);
502
                if (~i_wb_cyc)
503
                begin
504
                        state <= `WBSPI_WAIT_TIL_IDLE;
505
                        spi_hold <= 1'b0;
506
                end else if (spi_wr)
507
                begin // Give the SPI a chance to get busy on the last write
508
                        // Do nothing here.
509
                end else if ((i_wb_data_stb)&&(i_wb_we)
510
                                &&(i_wb_addr == (spif_addr+1))
511
                                &&(i_wb_addr[(AW-1):PW]==spif_addr[(AW-1):PW]))
512
                begin
513
// CHECK ME!
514
                        spif_cmd  <= 1'b1;
515
                        spif_data <= i_wb_data;
516
                        spif_addr <= i_wb_addr;
517
                        spif_ctrl  <= 1'b0;
518
                        spif_req<= 1'b1;
519
                        // We'll keep the bus stalled on this request
520
                        // for a while
521
                        state <= `WBSPI_WR_DATA;
522
                        o_wb_ack   <= 1'b0;
523
                        o_wb_stall <= 1'b0;
524
                end else if ((i_wb_data_stb|i_wb_ctrl_stb)&&(~o_wb_ack)) // Writing out of bounds
525
                begin
526
                        spi_hold <= 1'b0;
527
                        spi_wr   <= 1'b0;
528
                        state <= `WBSPI_WAIT_TIL_IDLE;
529
                end // Otherwise we stay here
530
        end else if (state == `WBSPI_RD_DUMMY)
531
        begin
532
                o_wb_ack   <= 1'b0;
533
                o_wb_stall <= 1'b1;
534
 
535
                spi_wr <= 1'b1; // Non-stop
536
                // Need to read one byte of dummy data,
537
                // just to consume 8 clocks
538
                spi_in <= { 8'h00, 24'h00 };
539
                spi_len <= 2'b00; // Read 8 bits
540
                spi_hold <= 1'b0;
541
                spif_req<= (spif_req) && (i_wb_cyc);
542
 
543
                if ((~spi_busy)&&(~o_spi_cs_n))
544
                        // Our command was accepted
545
                        state <= `WBSPI_READ_CMD;
546
        end else if (state == `WBSPI_READ_CMD)
547
        begin // Issue our first command to read 32 bits.
548
                o_wb_ack   <= 1'b0;
549
                o_wb_stall <= 1'b1;
550
 
551
                spi_wr <= 1'b1;
552
                spi_in <= { 8'hff, 24'h00 }; // Empty
553
                spi_len <= 2'b11; // Read 32 bits
554
                spi_hold <= 1'b0;
555
                spif_req<= (spif_req) && (i_wb_cyc);
556
                if ((spi_valid)&&(spi_len == 2'b11))
557
                        state <= `WBSPI_READ_DATA;
558
        end else if (state == `WBSPI_READ_DATA)
559
        begin
560
                // Pipelined read support
561
                spi_wr <=((i_wb_cyc)&&(i_wb_data_stb)&&(~i_wb_we)&&(i_wb_addr== (spif_addr+1)));
562
                spi_in <= 32'h00;
563
                spi_len <= 2'b11;
564
                // Don't let the device go to idle until the bus cycle ends.
565
                //      This actually prevents a *really* nasty race condition,
566
                //      where the strobe comes in after the lower level device
567
                //      has decided to stop waiting.  The write is then issued,
568
                //      but no one is listening.  By leaving the device open,
569
                //      the device is kept in a state where a valid strobe
570
                //      here will be useful.  Of course, we don't accept
571
                //      all commands, just reads.  Further, the strobe needs
572
                //      to be high for two clocks cycles without changing
573
                //      anything on the bus--one for us to notice it and pull
574
                //      our head out of the sand, and a second for whoever
575
                //      owns the bus to realize their command went through.
576
                spi_hold <= 1'b1;
577
                spif_req<= (spif_req) && (i_wb_cyc);
578
                if ((spi_valid)&&(~spi_in[31]))
579
                begin // Single pulse acknowledge and write data out
580
                        o_wb_ack <= spif_req;
581
                        o_wb_stall <= (~spi_wr);
582
                        // adjust endian-ness to match the PC
583
                        o_wb_data <= { spi_out[7:0], spi_out[15:8],
584
                                spi_out[23:16], spi_out[31:24] };
585
                        state <= (spi_wr)?`WBSPI_READ_DATA
586
                                : `WBSPI_WAIT_TIL_IDLE;
587
                        spif_req <= spi_wr;
588
                        spi_hold <= (~spi_wr);
589
                        if (spi_wr)
590
                                spif_addr <= i_wb_addr;
591
                end else if (~i_wb_cyc)
592
                begin // FAIL SAFE: If the bus cycle ends, forget why we're
593
                        // here, just go back to idle
594
                        state <= `WBSPI_WAIT_TIL_IDLE;
595
                        spi_hold <= 1'b0;
596
                        o_wb_ack <= 1'b0;
597
                        o_wb_stall <= 1'b1;
598
                end else begin
599
                        o_wb_ack <= 1'b0;
600
                        o_wb_stall <= 1'b1;
601
                end
602
        end else if (state == `WBSPI_READ_ID_CMD)
603
        begin // We came into here immediately after issuing a 0x9f command
604
                // Now we need to read 32 bits of data.  Result should be
605
                // 0x0102154d (8'h manufacture ID, 16'h device ID, followed
606
                // by the number of extended bytes available 8'h4d).
607
                o_wb_ack <= 1'b0;
608
                o_wb_stall<= 1'b1;
609
 
610
                spi_wr <= 1'b1; // No data to send, but need four bytes, since
611
                spi_len <= 2'b11; // 32 bits of data are ... useful
612
                spi_in <= 32'h00; // Irrelevant
613
                spi_hold <= 1'b0;
614
                spif_req <= (spif_req) && (i_wb_cyc);
615
                if ((~spi_busy)&&(~o_spi_cs_n)&&(spi_len == 2'b11))
616
                        // Our command was accepted, now go read the result
617
                        state <= `WBSPI_READ_ID;
618
        end else if (state == `WBSPI_READ_ID)
619
        begin
620
                o_wb_ack <= 1'b0; // Assuming we're still waiting
621
                o_wb_stall <= 1'b1;
622
 
623
                spi_wr <= 1'b0; // No more writes, we've already written the cmd
624
                spi_hold <= 1'b0;
625
                spif_req <= (spif_req) && (i_wb_cyc);
626
 
627
                // Here, we just wait until the result comes back
628
                // The problem is, the result may be the previous result.
629
                // So we use spi_len as an indicator
630
                spi_len <= 2'b00;
631
                if((spi_valid)&&(spi_len==2'b00))
632
                begin // Put the results out as soon as possible
633
                        o_wb_data <= spi_out[31:0];
634
                        o_wb_ack <= spif_req;
635
                        spif_req <= 1'b0;
636
                end else if ((~spi_busy)&&(o_spi_cs_n))
637
                begin
638
                        state <= `WBSPI_IDLE;
639
                        o_wb_stall <= 1'b0;
640
                end
641
        end else if (state == `WBSPI_READ_STATUS)
642
        begin // We enter after the command has been given, for now just
643
                // read and return
644
                spi_wr <= 1'b0;
645
                o_wb_ack <= 1'b0;
646
                spi_hold <= 1'b0;
647
                spif_req <= (spif_req) && (i_wb_cyc);
648
                if (spi_valid)
649
                begin
650
                        o_wb_ack <= spif_req;
651
                        o_wb_stall <= 1'b1;
652
                        spif_req <= 1'b0;
653
                        last_status <= spi_out[7:0];
654
                        write_in_progress <= spi_out[0];
655
                        if (spif_addr[1:0] == 2'b00) // Local read, checking
656
                        begin // status, 'cause we're writing
657
                                o_wb_data <= { spi_out[0],
658
                                        dirty_sector, spi_busy,
659
                                        ~write_protect,
660
                                        {(28-AW){1'b0}},
661
                                        erased_sector, {(SW){1'b0}} };
662
                        end else begin
663
                                o_wb_data <= { 24'h00, spi_out[7:0] };
664
                        end
665
                end
666
 
667
                if ((~spi_busy)&&(~spi_wr))
668
                        state <= `WBSPI_IDLE;
669
        end else if (state == `WBSPI_READ_CONFIG)
670
        begin // We enter after the command has been given, for now just
671
                // read and return
672
                spi_wr <= 1'b0;
673
                o_wb_ack <= 1'b0;
674
                o_wb_stall <= 1'b1;
675
                spi_hold <= 1'b0;
676
                spif_req <= (spif_req) && (i_wb_cyc);
677
 
678
                if (spi_valid)
679
                        o_wb_data <= { 24'h00, spi_out[7:0] };
680
 
681
                if ((~spi_busy)&&(~spi_wr))
682
                begin
683
                        state <= `WBSPI_IDLE;
684
                        o_wb_ack   <= spif_req;
685
                        o_wb_stall <= 1'b0;
686
                        spif_req <= 1'b0;
687
                end
688
        end else if (state == `WBSPI_WRITE_CONFIG)
689
        begin // We enter immediately after commanding a WEN
690
                o_wb_ack   <= 1'b0;
691
                o_wb_stall <= 1'b1;
692
 
693
                spi_len <= 2'b10;
694
                spi_in <= { 8'h01, last_status,
695
                                spif_data[7:2], 1'b0,spif_data[0], 8'h00 };
696
                spi_wr <= 1'b0;
697
                spi_hold <= 1'b0;
698
                spif_req <= (spif_req) && (i_wb_cyc);
699
                if ((~spi_busy)&&(~spi_wr))
700
                begin
701
                        spi_wr <= 1'b1;
702
                        state <= `WBSPI_WAIT_TIL_IDLE;
703
                        write_in_progress <= 1'b1;
704
                end
705
        end else if (state == `WBSPI_WRITE_STATUS)
706
        begin // We enter immediately after commanding a WEN
707
                o_wb_ack   <= 1'b0;
708
                o_wb_stall <= 1'b1;
709
 
710
                spi_len <= 2'b01;
711
                spi_in <= { 8'h01, spif_data[7:0], 16'h00 };
712
                // last_status <= i_wb_data[7:0]; // We'll read this in a moment
713
                spi_wr <= 1'b0;
714
                spi_hold <= 1'b0;
715
                spif_req <= (spif_req) && (i_wb_cyc);
716
                if ((~spi_busy)&&(~spi_wr))
717
                begin
718
                        spi_wr <= 1'b1;
719
                        last_status <= spif_data[7:0];
720
                        write_in_progress <= 1'b1;
721
                        if(((last_status[6])||(last_status[5]))
722
                                &&((~spif_data[6])&&(~spif_data[5])))
723
                                state <= `WBSPI_CLEAR_STATUS;
724
                        else
725
                                state <= `WBSPI_WAIT_TIL_IDLE;
726
                end
727
        end else if (state == `WBSPI_ERASE_CMD)
728
        begin // Know that WIP is clear on entry, WEN has just been commanded
729
                spi_wr     <= 1'b0;
730
                o_wb_ack   <= 1'b0;
731
                o_wb_stall <= 1'b1;
732
                spi_hold   <= 1'b0;
733
                spif_req <= (spif_req) && (i_wb_cyc);
734
 
735
                // Here's the erase command
736
                //spi_in <= { 8'hd8, 4'h0, spif_data[17:14], 14'h000, 2'b00 };
737
                spi_in <= { 8'h20, 4'h0, spif_data[(AW-1):SW],
738
                                {(SW){1'b0}}, 2'b00 };
739
                spi_len <= 2'b11; // 32 bit write
740
                // together with setting our copy of the WIP bit
741
                write_in_progress <= 1'b1;
742
                // keeping track of which sector we just erased
743
                erased_sector <= spif_data[(AW-1):SW];
744
                // and marking this erase sector as no longer dirty
745
                dirty_sector <= 1'b0;
746
 
747
                // Wait for a full stop before issuing this command
748
                if ((~spi_busy)&&(~spi_wr)&&(o_spi_cs_n))
749
                begin // When our command is accepted, move to the next state
750
                        spi_wr <= 1'b1;
751
                        state <= `WBSPI_ERASE_BLOCK;
752
                end
753
        end else if (state == `WBSPI_ERASE_BLOCK)
754
        begin
755
                spi_wr     <= 1'b0;
756
                spi_hold   <= 1'b0;
757
                o_wb_stall <= 1'b1;
758
                o_wb_ack   <= 1'b0;
759
                spif_req <= (spif_req) && (i_wb_cyc);
760
                // When the port clears, we can head back to idle
761
                if ((~spi_busy)&&(~spi_wr))
762
                begin
763
                        o_wb_ack <= spif_req;
764
                        state <= `WBSPI_IDLE;
765
                end
766
        end else if (state == `WBSPI_CLEAR_STATUS)
767
        begin // Issue a clear status command
768
                spi_wr <= 1'b1;
769
                spi_hold <= 1'b0;
770
                spi_len <= 2'b00; // 8 bit command
771
                spi_in <= { 8'h30, 24'h00 };
772
                last_status[6:5] <= 2'b00;
773
                spif_req <= (spif_req) && (i_wb_cyc);
774
                if ((spi_wr)&&(~spi_busy))
775
                        state <= `WBSPI_WAIT_TIL_IDLE;
776
        end else if (state == `WBSPI_IDLE_CHECK_WIP)
777
        begin // We are now in read status register mode
778
 
779
                // No bus commands have (yet) been given
780
                o_wb_stall <= 1'b1;
781
                o_wb_ack   <= 1'b0;
782
                spif_req <= (spif_req) && (i_wb_cyc);
783
 
784
                // Stay in this mode unless/until we get a command, or
785
                //      the write is over
786
                spi_wr <= (((~i_wb_cyc)||((~i_wb_data_stb)&&(~i_wb_ctrl_stb)))
787
                                &&(write_in_progress));
788
                spi_len <= 2'b00; // 8 bit reads
789
                if (spi_valid)
790
                begin
791
                        write_in_progress <= spi_out[0];
792
                        if ((~spi_out[0])&&(write_in_progress))
793
                                o_interrupt <= 1'b1;
794
                end else
795
                        o_interrupt <= 1'b0;
796
 
797
                if ((~spi_wr)&&(~spi_busy)&&(o_spi_cs_n))
798
                begin // We can now go to idle and process a command
799
                        o_wb_stall <= 1'b0;
800
                        o_wb_ack   <= 1'b0;
801
                        state <= `WBSPI_IDLE;
802
                end
803
        end else // if (state == `WBSPI_WAIT_TIL_IDLE) or anything else
804
        begin
805
                spi_wr     <= 1'b0;
806
                spi_hold   <= 1'b0;
807
                o_wb_stall <= 1'b1;
808
                o_wb_ack   <= 1'b0;
809
                spif_req   <= 1'b0;
810
                if ((~spi_busy)&&(o_spi_cs_n)&&(~spi_wr)) // Wait for a full
811
                begin // clearing of the SPI port before moving on
812
                        state <= `WBSPI_IDLE;
813
                        o_wb_stall <= 1'b0;
814
                        o_wb_ack   <= 1'b0; // Shouldn't be acking anything here
815
                end
816
        end
817
        end
818
 
819
        // Command and control during the reset sequence
820
        assign  o_spi_cs_n = w_spi_cs_n;
821
        assign  o_spi_sck  = w_spi_sck;
822
        assign  o_spi_mosi = w_spi_mosi;
823
endmodule

powered by: WebSVN 2.1.0

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