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

Subversion Repositories qspiflash

[/] [qspiflash/] [trunk/] [rtl/] [eqspiflash.v] - Blame information for rev 14

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

Line No. Rev Author Line
1 9 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    eqspiflash.v
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     Provide access to the flash device on an Arty, via the Extended
8
//              SPI interface.  Reads and writes will use the QuadSPI interface
9
//      (4-bits at a time) all other commands (register and otherwise) will use
10
//      the SPI interface (1 bit at a time).
11
//
12
// Registers:
13
//      0. Erase register control.  Provides status of pending writes, erases,
14
//              and commands (sub)sector erase operations.
15
//         Bit-Fields:
16
//              31. WIP (Write-In-Progress), write a '1' to this bit to command
17
//                      an erase sequence.
18
//              30. WriteEnabled -- set to a '1' to disable write protection and
19
//                      to write a WRITE-ENABLE to the device.  Set to a '0' to
20
//                      disable WRITE-ENABLE-LATCH.  (Key is required to enable
21
//                      writes)
22
//              29. Quad mode read/writes enabled.  (Rest of controller will use
23
//                      extended SPI mode, but reads and writes will use Quad
24
//                      mode.)
25
//              28. Subsector erase bit (set 1 to erase a subsector, 0 to 
26
//                      erase a full sector, maintains last value written from
27
//                      an erase command, starts at '0')
28
//              27. SD ID loaded
29
//              26. Write protect violation--cleared upon any valid write
30
//              25. XIP enabled.  (Leave read mode in XIP, so you can start
31
//                      next read faster.)
32
//              24. Unused
33
//              23..0: Address of erase sector upon erase command
34
//              23..14: Sector address (can only be changed w/ key)
35
//              23..10: Subsector address (can only be changed w/ key)
36
//               9.. 0: write protect KEY bits, always read a '0', write
37
//                      commands, such as WP disable or erase, must always
38
//                      write with a '1be' to activate.
39
//      0. WEL: All writes that do not command an erase will be used
40
//                      to set/clear the write enable latch.
41
//                      Send 0x06, return, if WP is clear (enable writes)
42
//                      Send 0x04, return
43
//      1. STATUS
44
//              Send 0x05, read  1-byte
45
//              Send 0x01, write 1-byte: i_wb_data[7:0]
46
//      2. NV-CONFIG (16-bits)
47
//              Send 0xB5, read  2-bytes
48
//              Send 0xB1, write 2-bytes: i_wb_data[15:0]
49
//      3. V-CONFIG (8-bits)
50
//              Send 0x85, read  1-byte
51
//              Send 0x81, write 1-byte: i_wb_data[7:0]
52
//      4. EV-CONFIG (8-bits)
53
//              Send 0x65, read  1-byte
54
//              Send 0x61, write 1-byte: i_wb_data[7:0]
55
//      5. Lock (send 32-bits, rx 1 byte)
56
//              Send 0xE8, last-sector-addr (3b), read  1-byte
57
//              Send 0xE5, last-sector-addr (3b), write 1-byte: i_wb_data[7:0]
58
//      6. Flag Status
59
//              Send 0x70, read  1-byte
60
//              Send 0x50, to clear, no bytes to write
61
//      7. Asynch Read-ID: Write here to cause controller to read ID into buffer
62
//      8.-12.  ID buffer (20 bytes, 5 words)
63
//              Attempted reads before buffer is full will stall bus until 
64
//              buffer is read.  Writes act like the asynch-Read-ID command,
65
//              and will cause the controller to read the buffer.
66 14 dgisselq
//      13. Reset Enable
67
//      14. Reset Memory
68 9 dgisselq
//      15.     OTP control word
69
//                      Write zero to permanently lock OTP
70
//                      Read to determine if OTP is permanently locked
71
//      16.-31. OTP (64-bytes, 16 words, buffered until write)
72
//              (Send DWP before writing to clear write enable latch)
73
//
74
//
75
// Creator:     Dan Gisselquist, Ph.D.
76
//              Gisselquist Technology, LLC
77
//
78
////////////////////////////////////////////////////////////////////////////////
79
//
80
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
81
//
82
// This program is free software (firmware): you can redistribute it and/or
83
// modify it under the terms of  the GNU General Public License as published
84
// by the Free Software Foundation, either version 3 of the License, or (at
85
// your option) any later version.
86
//
87
// This program is distributed in the hope that it will be useful, but WITHOUT
88
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
89
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90
// for more details.
91
//
92
// You should have received a copy of the GNU General Public License along
93
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
94
// target there if the PDF file isn't present.)  If not, see
95
// <http://www.gnu.org/licenses/> for a copy.
96
//
97
// License:     GPL, v3, as defined and found on www.gnu.org,
98
//              http://www.gnu.org/licenses/gpl.html
99
//
100
//
101
////////////////////////////////////////////////////////////////////////////////
102
//
103
//
104
// `define      QSPI_READ_ONLY
105
module  eqspiflash(i_clk_200mhz, i_rst,
106
                // Incoming wishbone connection(s)
107
                //      The two strobe lines allow the data to live on a
108
                //      separate part of the master bus from the control 
109
                //      registers.  Only one strobe will ever be active at any
110
                //      time, no strobes will ever be active unless i_wb_cyc
111
                //      is also active.
112
                i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we,
113
                i_wb_addr, i_wb_data,
114
                // Outgoing wishbone data
115
                o_wb_ack, o_wb_stall, o_wb_data,
116
                // Quad SPI connections
117
                o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat,
118
                // Interrupt the CPU
119
                o_interrupt, o_cmd_accepted,
120
                // Debug the interface
121
                o_dbg);
122
 
123
        input                   i_clk_200mhz, i_rst;
124
        // Wishbone bus inputs
125
        input                   i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we;
126
        input           [21:0]   i_wb_addr;      // 24 bits of addr space
127
        input           [31:0]   i_wb_data;
128
        // Wishbone bus outputs
129
        output  reg             o_wb_ack;
130
        output  wire            o_wb_stall;
131
        output  reg     [31:0]   o_wb_data;
132
        // Quad SPI connections
133
        output  wire            o_qspi_sck, o_qspi_cs_n;
134
        output  wire    [1:0]    o_qspi_mod;
135
        output  wire    [3:0]    o_qspi_dat;
136
        input   wire    [3:0]    i_qspi_dat;
137
        //
138
        output  reg             o_interrupt;
139
        //
140
        output  reg             o_cmd_accepted;
141
        //
142
        output  wire    [31:0]   o_dbg;
143
 
144
        initial o_cmd_accepted = 1'b0;
145
        always @(posedge i_clk_200mhz)
146
                o_cmd_accepted=((i_wb_data_stb)||(i_wb_ctrl_stb))&&(~o_wb_stall);
147
        //
148 10 dgisselq
        // lleqspi
149 9 dgisselq
        //
150
        //      Providing the low-level SPI interface
151
        //
152
        reg     spi_wr, spi_hold, spi_spd, spi_dir, spi_recycle;
153
        reg     [31:0]   spi_word;
154
        reg     [1:0]    spi_len;
155
        wire    [31:0]   spi_out;
156
        wire            spi_valid, spi_busy, spi_stopped;
157 10 dgisselq
        lleqspi lowlvl(i_clk_200mhz, spi_wr, spi_hold, spi_word, spi_len,
158 9 dgisselq
                        spi_spd, spi_dir, spi_recycle, spi_out, spi_valid, spi_busy,
159
                o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat);
160
        assign  spi_stopped = (o_qspi_cs_n)&&(~spi_busy)&&(~spi_wr);
161
 
162
 
163
        //
164
        // Bus module
165
        //
166
        //      Providing a shared interface to the WB bus
167
        //
168
        // Wishbone data (returns)
169
        wire            bus_wb_ack, bus_wb_stall;
170
        wire    [31:0]   bus_wb_data;
171
        // Latched request data
172
        wire            bus_wr;
173
        wire    [21:0]   bus_addr;
174
        wire    [31:0]   bus_data;
175
        wire    [21:0]   bus_sector;
176
        // Strobe commands
177
        wire    bus_ack;
178
        wire    bus_readreq, bus_piperd, bus_ereq, bus_wreq,
179
                        bus_pipewr, bus_endwr, bus_ctreq, bus_idreq,
180
                        bus_other_req,
181
        // Live parameters
182 14 dgisselq
                        w_xip, w_quad, w_idloaded, w_leave_xip;
183 9 dgisselq
        reg             bus_wip;
184
        qspibus preproc(i_clk_200mhz, i_rst,
185
                        i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb,
186
                                i_wb_we, i_wb_addr, i_wb_data,
187
                                bus_wb_ack, bus_wb_stall, bus_wb_data,
188
                        bus_wr, bus_addr, bus_data, bus_sector,
189
                                bus_readreq, bus_piperd,
190
                                        bus_wreq, bus_ereq,
191
                                        bus_pipewr, bus_endwr,
192
                                bus_ctreq, bus_idreq, bus_other_req, bus_ack,
193
                        w_xip, w_quad, w_idloaded, bus_wip, spi_stopped);
194
 
195
        //
196
        // Read flash module
197
        //
198
        //      Providing a means of (and the logic to support) reading from
199
        //      the flash
200
        //
201
        wire            rd_data_ack;
202
        wire    [31:0]   rd_data;
203
        //
204
        wire            rd_bus_ack;
205
        //
206
        wire            rd_qspi_req;
207
        wire            rd_qspi_grant;
208
        //
209
        wire            rd_spi_wr, rd_spi_hold, rd_spi_spd, rd_spi_dir,
210
                        rd_spi_recycle;
211
        wire    [31:0]   rd_spi_word;
212
        wire    [1:0]    rd_spi_len;
213
        //
214
        readqspi        rdproc(i_clk_200mhz, bus_readreq, bus_piperd,
215
                                        bus_other_req,
216
                                bus_addr, rd_bus_ack,
217
                                rd_qspi_req, rd_qspi_grant,
218
                                rd_spi_wr, rd_spi_hold, rd_spi_word, rd_spi_len,
219
                                rd_spi_spd, rd_spi_dir, rd_spi_recycle,
220
                                        spi_out, spi_valid,
221
                                        spi_busy, spi_stopped, rd_data_ack, rd_data,
222 14 dgisselq
                                        w_quad, w_xip, w_leave_xip);
223 9 dgisselq
 
224
        //
225
        // Write/Erase flash module
226
        //
227
        //      Logic to write (program) and erase the flash.
228
        //
229
        // Wishbone bus return
230
        wire            ew_data_ack;
231
        wire    [31:0]   ew_data;
232
        // Arbiter interaction
233
        wire            ew_qspi_req;
234
        wire            ew_qspi_grant;
235
        // Bus controller return
236
        wire            ew_bus_ack;
237
        // SPI control wires
238
        wire            ew_spi_wr, ew_spi_hold, ew_spi_spd, ew_spi_dir;
239
        wire    [31:0]   ew_spi_word;
240
        wire    [1:0]    ew_spi_len;
241
        //
242
        wire            w_ew_wip;
243
        //
244
        writeqspi       ewproc(i_clk_200mhz, bus_wreq,bus_ereq,
245
                                        bus_pipewr, bus_endwr,
246
                                        bus_addr, bus_data,
247
                                ew_bus_ack, ew_qspi_req, ew_qspi_grant,
248
                                ew_spi_wr, ew_spi_hold, ew_spi_word, ew_spi_len,
249
                                        ew_spi_spd, ew_spi_dir,
250
                                        spi_out, spi_valid, spi_busy, spi_stopped,
251
                                ew_data_ack, w_quad, w_ew_wip);
252
 
253
        //
254
        // Control module
255
        //
256
        //      Logic to read/write status and configuration registers
257
        //
258
        // Wishbone bus return
259
        wire            ct_data_ack;
260
        wire    [31:0]   ct_data;
261
        // Arbiter interaction
262
        wire            ct_qspi_req;
263
        wire            ct_grant;
264
        // Bus controller return
265
        wire            ct_ack;
266
        // SPI control wires
267
        wire            ct_spi_wr, ct_spi_hold, ct_spi_spd, ct_spi_dir;
268
        wire    [31:0]   ct_spi_word;
269
        wire    [1:0]    ct_spi_len;
270
        //
271
        ctrlspi         ctproc(i_clk_200mhz,
272 14 dgisselq
                                bus_ctreq, bus_wr, bus_addr[3:0], bus_data, bus_sector,
273 9 dgisselq
                                ct_qspi_req, ct_grant,
274
                                ct_spi_wr, ct_spi_hold, ct_spi_word, ct_spi_len,
275
                                        ct_spi_spd, ct_spi_dir,
276
                                        spi_out, spi_valid, spi_busy, spi_stopped,
277 14 dgisselq
                                ct_ack, ct_data_ack, ct_data, w_leave_xip, w_xip, w_quad);
278 9 dgisselq
        assign  ct_spi_hold = 1'b0;
279
        assign  ct_spi_spd  = 1'b0;
280
 
281
        //
282
        // ID/OTP module
283
        //
284
        //      Access to ID and One-Time-Programmable registers, but to read
285
        //      and to program (the OTP), and to finally lock (OTP) registers.
286
        //
287
        // Wishbone bus return
288
        wire            id_data_ack;
289
        wire    [31:0]   id_data;
290
        // Arbiter interaction
291
        wire            id_qspi_req;
292
        wire            id_qspi_grant;
293
        // Bus controller return
294
        wire            id_bus_ack;
295
        // SPI control wires
296
        wire            id_spi_wr, id_spi_hold, id_spi_spd, id_spi_dir;
297
        wire    [31:0]   id_spi_word;
298
        wire    [1:0]    id_spi_len;
299
        //
300
        wire            w_id_wip;
301
        //
302
        idotpqspi       idotp(i_clk_200mhz, bus_idreq,
303
                                bus_wr, bus_pipewr, bus_addr[4:0], bus_data, id_bus_ack,
304
                                id_qspi_req, id_qspi_grant,
305
                                id_spi_wr, id_spi_hold, id_spi_word, id_spi_len,
306
                                        id_spi_spd, id_spi_dir,
307
                                        spi_out, spi_valid, spi_busy, spi_stopped,
308
                                id_data_ack, id_data, w_idloaded, w_id_wip);
309
 
310
        // Arbitrator
311
        reg             owned;
312
        reg     [1:0]    owner;
313
        initial         owned = 1'b0;
314
        always @(posedge i_clk_200mhz) // 7 inputs (spi_stopped is the CE)
315
                if ((~owned)&&(spi_stopped))
316
                begin
317
                        casez({rd_qspi_req,ew_qspi_req,id_qspi_req,ct_qspi_req})
318
                        4'b1???: begin owned<= 1'b1; owner <= 2'b00; end
319
                        4'b01??: begin owned<= 1'b1; owner <= 2'b01; end
320
                        4'b001?: begin owned<= 1'b1; owner <= 2'b10; end
321
                        4'b0001: begin owned<= 1'b1; owner <= 2'b11; end
322
                        default: begin owned<= 1'b0; owner <= 2'b00; end
323
                        endcase
324
                end else if ((owned)&&(spi_stopped))
325
                begin
326
                        casez({rd_qspi_req,ew_qspi_req,id_qspi_req,ct_qspi_req,owner})
327
                        6'b0???00: owned<= 1'b0;
328
                        6'b?0??01: owned<= 1'b0;
329
                        6'b??0?10: owned<= 1'b0;
330
                        6'b???011: owned<= 1'b0;
331
                        default: begin ; end
332
                        endcase
333
                end
334
 
335
        assign  rd_qspi_grant = (owned)&&(owner == 2'b00);
336
        assign  ew_qspi_grant = (owned)&&(owner == 2'b01);
337
        assign  id_qspi_grant = (owned)&&(owner == 2'b10);
338
        assign  ct_grant      = (owned)&&(owner == 2'b11);
339
 
340
        // Module controller
341
        always @(posedge i_clk_200mhz)
342
        case(owner)
343
        2'b00: begin
344
                spi_wr      <= (owned)&&(rd_spi_wr);
345
                spi_hold    <= rd_spi_hold;
346
                spi_word    <= rd_spi_word;
347
                spi_len     <= rd_spi_len;
348
                spi_spd     <= rd_spi_spd;
349
                spi_dir     <= rd_spi_dir;
350
                spi_recycle <= rd_spi_recycle;
351
                end
352
        2'b01: begin
353
                spi_wr      <= (owned)&&(ew_spi_wr);
354
                spi_hold    <= ew_spi_hold;
355
                spi_word    <= ew_spi_word;
356
                spi_len     <= ew_spi_len;
357
                spi_spd     <= ew_spi_spd;
358
                spi_dir     <= ew_spi_dir;
359
                spi_recycle <= 1'b1; // Long recycle time
360
                end
361
        2'b10: begin
362
                spi_wr      <= (owned)&&(id_spi_wr);
363
                spi_hold    <= id_spi_hold;
364
                spi_word    <= id_spi_word;
365
                spi_len     <= id_spi_len;
366
                spi_spd     <= id_spi_spd;
367
                spi_dir     <= id_spi_dir;
368
                spi_recycle <= 1'b1; // Long recycle time
369
                end
370
        2'b11: begin
371
                spi_wr      <= (owned)&&(ct_spi_wr);
372
                spi_hold    <= ct_spi_hold;
373
                spi_word    <= ct_spi_word;
374
                spi_len     <= ct_spi_len;
375
                spi_spd     <= ct_spi_spd;
376
                spi_dir     <= ct_spi_dir;
377
                spi_recycle <= 1'b1; // Long recycle time
378
                end
379
        endcase
380
 
381
        reg     last_wip;
382
        initial bus_wip = 1'b0;
383
        initial last_wip = 1'b0;
384
        initial o_interrupt = 1'b0;
385
        always @(posedge i_clk_200mhz)
386
        begin
387
                bus_wip <= w_ew_wip || w_id_wip;
388
                last_wip <= bus_wip;
389
                o_interrupt <= ((~bus_wip)&&(last_wip));
390
        end
391
 
392
 
393
        // Now, let's return values onto the wb bus
394
        always @(posedge i_clk_200mhz)
395
        begin
396
                // Ack our internal bus controller.  This means the command was
397
                // accepted, and the bus can go on to looking for the next 
398
                // command.  It controls the i_wb_stall line, just not the
399
                // i_wb_ack line.
400
 
401
                // Ack the wishbone with any response
402
                o_wb_ack <= (bus_wb_ack)|(rd_data_ack)|(ew_data_ack)|(id_data_ack)|(ct_data_ack);
403
                o_wb_data <= (bus_wb_ack)?bus_wb_data
404
                        : (id_data_ack) ? id_data : spi_out;
405
        end
406
 
407
        assign  o_wb_stall = bus_wb_stall;
408
        assign  bus_ack = (rd_bus_ack|ew_bus_ack|id_bus_ack|ct_ack);
409
 
410
        assign  o_dbg = {
411
                i_wb_cyc, i_wb_ctrl_stb, i_wb_data_stb, o_wb_ack, bus_ack, //5
412
                //
413
                (spi_wr)&&(~spi_busy), spi_valid, spi_word[31:25],
414
                spi_out[7:2],
415
                //
416
                o_qspi_cs_n, o_qspi_sck, o_qspi_mod,    // 4 bits
417
                o_qspi_dat, i_qspi_dat                  // 8 bits
418
                };
419
endmodule
420
 
421
module  qspibus(i_clk, i_rst, i_cyc, i_data_stb, i_ctrl_stb,
422
                i_we, i_addr, i_data,
423
                        o_wb_ack, o_wb_stall, o_wb_data,
424
                o_wr, o_addr, o_data, o_sector,
425
                o_readreq, o_piperd, o_wrreq, o_erreq, o_pipewr, o_endwr,
426
                        o_ctreq, o_idreq, o_other,
427
                i_ack, i_xip, i_quad, i_idloaded, i_wip, i_spi_stopped);
428
        //
429
        input                   i_clk, i_rst;
430
        // Wishbone bus inputs
431
        input                   i_cyc, i_data_stb, i_ctrl_stb, i_we;
432
        input           [21:0]   i_addr;
433
        input           [31:0]   i_data;
434
        // Wishbone bus outputs
435
        output  reg             o_wb_ack;
436
        output  reg             o_wb_stall;
437
        output  wire    [31:0]   o_wb_data;
438
        // Internal signals to the QSPI flash interface
439
        output  reg             o_wr;
440
        output  reg     [21:0]   o_addr;
441
        output  reg     [31:0]   o_data;
442
        output  wire    [21:0]   o_sector;
443
        output  reg             o_readreq, o_piperd, o_wrreq, o_erreq,
444
                                o_pipewr, o_endwr,
445
                                o_ctreq, o_idreq;
446
        output  wire            o_other;
447
        input                   i_ack, i_xip, i_quad, i_idloaded;
448
        input                   i_wip, i_spi_stopped;
449
 
450
 
451
        //
452
        reg     pending, lcl_wrreq, lcl_ctreq, lcl_ack, ack, wp_err, wp;
453
        reg     lcl_reg;
454
        reg     [14:0]   esector;
455
        reg     [21:0]   next_addr;
456
 
457
 
458
        reg     pipeable;
459
        reg     same_page;
460
        always @(posedge i_clk)
461
                same_page <= (i_data_stb)&&(i_we)
462
                        &&(i_addr[21:6] == o_addr[21:6])
463
                        &&(i_addr[5:0] == o_addr[5:0] + 6'h1);
464
 
465
        initial pending = 1'b0;
466
        initial o_readreq = 1'b0;
467
        initial lcl_wrreq = 1'b0;
468
        initial lcl_ctreq = 1'b0;
469
        initial o_ctreq   = 1'b0;
470
        initial o_idreq   = 1'b0;
471
 
472
        initial ack = 1'b0;
473
        always @(posedge i_clk)
474
                ack <= (i_ack)||(lcl_ack);
475
 
476
        // wire [9:0]   key;
477
        // assign       key = 10'h1be;
478
        reg     lcl_key, set_sector, ctreg_stb;
479
        initial lcl_key = 1'b0;
480
        always @(posedge i_clk)
481
                // Write protect "key" to enable the disabling of write protect
482
                lcl_key<= (i_ctrl_stb)&&(~wp)&&(i_we)&&(i_addr[5:0]==6'h00)
483
                                &&(i_data[9:0] == 10'h1be)&&(i_data[31:30]==2'b11);
484
        initial set_sector = 1'b0;
485
        always @(posedge i_clk)
486
                set_sector <= (i_ctrl_stb)&&(~o_wb_stall)
487
                                &&(i_we)&&(i_addr[5:0]==6'h00)
488
                                &&(i_data[9:0] == 10'h1be);
489
 
490
        always @(posedge i_clk)
491
                if (i_ctrl_stb)
492
                        lcl_reg <= (i_addr[3:0] == 4'h00);
493
 
494
        initial ctreg_stb = 1'b0;
495
        initial o_wb_stall = 1'b0;
496
        always @(posedge i_clk)
497
        begin // Inputs: rst, stb, stb, stall, ack, addr[4:0] -- 9
498
                if (i_rst)
499
                        o_wb_stall <= 1'b0;
500
                else
501
                        o_wb_stall <= (((i_data_stb)||(i_ctrl_stb))&&(~o_wb_stall))
502
                                ||((pending)&&(~ack));
503
 
504
                ctreg_stb <= (i_ctrl_stb)&&(~o_wb_stall)&&(i_addr[4:0]==5'h00)&&(~pending)
505
                                ||(pending)&&(ctreg_stb)&&(~lcl_ack)&&(~i_ack);
506
                if (~o_wb_stall)
507
                begin // Bus command accepted!
508 14 dgisselq
                        if (i_data_stb)
509
                                next_addr <= i_addr + 22'h1;
510 9 dgisselq
                        if ((i_data_stb)||(i_ctrl_stb))
511
                        begin
512
                                pending <= 1'b1;
513
                                o_addr <= i_addr;
514
                                o_data <= i_data;
515
                                o_wr   <= i_we;
516
                        end
517
 
518
                        if ((i_data_stb)&&(~i_we))
519
                                o_readreq <= 1'b1;
520
 
521
                        if ((i_data_stb)&&(i_we))
522
                                lcl_wrreq <= 1'b1;
523
                        if ((i_ctrl_stb)&&(~i_addr[4]))
524
                        begin
525
                                casez(i_addr[4:0])
526
                                5'h0: lcl_ctreq<= 1'b1;
527
                                5'h1: lcl_ctreq <= 1'b1;
528
                                5'h2: lcl_ctreq <= 1'b1;
529
                                5'h3: lcl_ctreq <= 1'b1;
530
                                5'h4: lcl_ctreq <= 1'b1;
531
                                5'h5: lcl_ctreq <= 1'b1;
532
                                5'h6: lcl_ctreq <= 1'b1;
533
                                5'h7: lcl_ctreq <= 1'b1;
534 14 dgisselq
                                5'h8: o_idreq  <= 1'b1; // ID[0]
535
                                5'h9: o_idreq  <= 1'b1; // ID[1]
536
                                5'ha: o_idreq  <= 1'b1; // ID[2]
537
                                5'hb: o_idreq  <= 1'b1; // ID[3]
538
                                5'hc: o_idreq  <= 1'b1; // ID[4]
539
                                5'hd: lcl_ctreq <= 1'b1;        //
540
                                5'he: lcl_ctreq <= 1'b1;
541
                                5'hf: o_idreq   <= 1'b1; // Program OTP register
542 9 dgisselq
                                default: begin o_idreq <= 1'b1; end
543
                                endcase
544
                        end else if (i_ctrl_stb)
545
                                o_idreq <= 1'b1;
546
                end else if (ack)
547
                begin
548
                        pending <= 1'b0;
549
                        o_readreq <= 1'b0;
550
                        o_idreq <= 1'b0;
551
                        lcl_ctreq <= 1'b0;
552
                        lcl_wrreq <= 1'b0;
553
                end
554
 
555
                if(i_rst)
556
                begin
557
                        pending <= 1'b0;
558
                        o_readreq <= 1'b0;
559
                        o_idreq <= 1'b0;
560
                        lcl_ctreq <= 1'b0;
561
                        lcl_wrreq <= 1'b0;
562
                end
563
 
564 14 dgisselq
                if ((i_data_stb)&&((~o_wb_stall)||(i_ack)))
565
                        o_piperd <= ((~i_we)&&(pipeable)&&(i_addr == next_addr));
566
                else if ((i_ack)&&(~i_data_stb))
567 9 dgisselq
                        o_piperd <= 1'b0;
568
                if ((i_data_stb)&&(~o_wb_stall))
569
                        pipeable <= (~i_we);
570
                else if ((i_ctrl_stb)&&(~o_wb_stall))
571
                        pipeable <= 1'b0;
572
 
573
                o_pipewr <= (same_page)||(pending)&&(o_pipewr);
574
        end
575
 
576
        reg     r_other, last_wip;
577
 
578
        reg     last_pending;
579
        always @(posedge i_clk)
580
                last_pending <= pending;
581
        always @(posedge i_clk)
582
                last_wip <= i_wip;
583
        wire    new_req;
584
        assign  new_req = (pending)&&(~last_pending);
585
 
586 14 dgisselq
        initial esector   = 15'h00;
587 9 dgisselq
        initial o_wrreq   = 1'b0;
588
        initial o_erreq   = 1'b0;
589
        initial wp_err    = 1'b0;
590
        initial lcl_ack   = 1'b0;
591
        initial r_other   = 1'b0;
592
        initial o_endwr   = 1'b1;
593
        initial wp        = 1'b1;
594
        always @(posedge i_clk)
595
        begin
596
                if (i_ack)
597
                begin
598
                        o_erreq <= 1'b0;
599
                        o_wrreq <= 1'b0;
600
                        o_ctreq <= 1'b0;
601
                        r_other <= 1'b0;
602
                end
603
 
604
                if ((last_wip)&&(~i_wip))
605
                        wp <= 1'b1;
606
 
607
                // o_endwr  <= ((~i_cyc)||(~o_wr)||(o_pipewr))
608
                                // ||(~new_req)&&(o_endwr);
609
                o_endwr <= ((pending)&&(~o_pipewr))||((~pending)&&(~i_cyc));
610
 
611
                // Default ACK is always set to zero, unless the following ...
612
                o_wb_ack <= 1'b0;
613
 
614
                if (set_sector)
615
                begin
616
                        esector[13:0] <= { o_data[23:14], 4'h0 };
617
                        wp <= (o_data[30])&&(new_req)||(wp)&&(~new_req);
618 14 dgisselq
                        esector[14] <= o_data[28]; // Subsector
619 9 dgisselq
                        if (o_data[28])
620
                        begin
621
                                esector[3:0] <= o_data[13:10];
622
                        end
623
                end
624
 
625
                lcl_ack <= 1'b0;
626
                if ((i_wip)&&(new_req)&&(~same_page))
627
                begin
628
                        o_wb_ack <= 1'b1;
629
                        lcl_ack <= 1'b1;
630
                end else if ((ctreg_stb)&&(new_req))
631
                begin // A request of the status register
632
                        // Always ack control register, even on failed attempts
633
                        // to erase.
634
                        o_wb_ack <= 1'b1;
635
                        lcl_ack <= 1'b1;
636
 
637
                        if (lcl_key)
638
                        begin
639
                                o_ctreq <= 1'b0;
640
                                o_erreq <= 1'b1;
641
                                r_other <= 1'b1;
642
                                lcl_ack <= 1'b0;
643
                        end else if ((o_wr)&&(~o_data[31]))
644
                        begin // WEL or WEL disable
645
                                o_ctreq <= (wp == o_data[30]);
646
                                r_other <= (wp == o_data[30]);
647
                                lcl_ack <= (wp != o_data[30]);
648
                                wp <= !o_data[30];
649
                        end else if (~o_wr)
650
                                lcl_ack <= 1'b1;
651
                        wp_err <= (o_data[31])&&(~lcl_key);
652
                end else if ((lcl_ctreq)&&(new_req))
653
                begin
654
                        o_ctreq <= 1'b1;
655
                        r_other <= 1'b1;
656
                end else if ((lcl_wrreq)&&(new_req))
657
                begin
658
                        if (~wp)
659
                        begin
660
                                o_wrreq <= 1'b1;
661
                                r_other <= 1'b1;
662
                                o_endwr  <= 1'b0;
663
                                lcl_ack <= 1'b0;
664
                        end else begin
665
                                o_wb_ack <= 1'b1;
666
                                wp_err <= 1'b1;
667
                                lcl_ack <= 1'b1;
668
                        end
669
                end
670
 
671
                if (i_rst)
672
                begin
673
                        o_ctreq <= 1'b0;
674
                        o_erreq <= 1'b0;
675
                        o_wrreq <= 1'b0;
676
                        r_other <= 1'b0;
677
                end
678
 
679
        end
680
 
681
 
682
        assign o_wb_data[31:0] = { i_wip, ~wp, i_quad, esector[14],
683
                        i_idloaded, wp_err, i_xip, i_spi_stopped,
684
                        esector[13:0], 10'h00 };
685
        assign  o_sector = { esector[13:0], 8'h00 }; // 22 bits
686
        assign  o_other = (r_other)||(o_idreq);
687
 
688
endmodule
689
 
690
 
691
`define RD_IDLE                 4'h0
692
`define RD_IDLE_GET_PORT        4'h1
693
`define RD_SLOW_DUMMY           4'h2
694
`define RD_SLOW_READ_DATA       4'h3
695
`define RD_QUAD_READ_DATA       4'h4
696
`define RD_QUAD_DUMMY           4'h5
697
`define RD_QUAD_ADDRESS         4'h6
698
`define RD_XIP                  4'h7
699
`define RD_GO_TO_IDLE           4'h8
700
`define RD_GO_TO_XIP            4'h9
701
 
702
module  readqspi(i_clk, i_readreq, i_piperd, i_other_req, i_addr, o_bus_ack,
703
                o_qspi_req, i_grant,
704
                        o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
705
                                o_spi_spd, o_spi_dir, o_spi_recycle,
706
                        i_spi_data, i_spi_valid, i_spi_busy, i_spi_stopped,
707 14 dgisselq
                        o_data_ack, o_data, i_quad, i_xip, o_leave_xip);
708 9 dgisselq
        input                   i_clk;
709
        input                   i_readreq, i_piperd, i_other_req;
710
        input           [21:0]   i_addr;
711
        output  reg             o_bus_ack, o_qspi_req;
712
        input   wire            i_grant;
713
        output  reg             o_spi_wr;
714
        output  wire            o_spi_hold;
715
        output  reg     [31:0]   o_spi_word;
716
        output  reg     [1:0]    o_spi_len;
717
        output  reg             o_spi_spd, o_spi_dir, o_spi_recycle;
718
        input           [31:0]   i_spi_data;
719
        input                   i_spi_valid, i_spi_busy, i_spi_stopped;
720
        output  reg             o_data_ack;
721
        output  reg     [31:0]   o_data;
722
        input                   i_quad, i_xip;
723 14 dgisselq
        output  wire            o_leave_xip;
724 9 dgisselq
 
725
        reg     accepted;
726
        initial accepted = 1'b0;
727
        always @(posedge i_clk)
728
                accepted <= (~i_spi_busy)&&(i_grant)&&(o_spi_wr)&&(~accepted);
729
 
730
        reg     [3:0]    rd_state;
731
        reg             r_leave_xip, r_xip, r_quad, r_requested;
732 11 dgisselq
        reg     [3:0]    invalid_ack_pipe;
733 9 dgisselq
        initial rd_state = `RD_IDLE;
734
        initial o_data_ack = 1'b0;
735
        initial o_bus_ack  = 1'b0;
736
        initial o_qspi_req = 1'b0;
737
        always @(posedge i_clk)
738
        begin
739
                o_data_ack <= 1'b0;
740
                o_bus_ack <= 1'b0;
741
                o_spi_recycle <= 1'b0;
742
                if (i_spi_valid)
743
                        o_data <= i_spi_data;
744 11 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
745 9 dgisselq
                case(rd_state)
746
                `RD_IDLE: begin
747
                        r_requested <= 1'b0;
748
                        o_qspi_req <= 1'b0;
749
                        o_spi_word <= { ((i_quad)? 8'h6B: 8'h0b), i_addr, 2'b00 };
750
                        o_spi_wr <= 1'b0;
751
                        o_spi_dir <= 1'b0;
752
                        o_spi_spd <= 1'b0;
753
                        o_spi_len <= 2'b11;
754
                        r_xip <= (i_xip)&&(i_quad);
755
                        r_leave_xip <= 1'b0; // Not in it, so can't leave it
756
                        r_quad <= i_quad;
757
                        if (i_readreq)
758
                        begin
759
                                rd_state <= `RD_IDLE_GET_PORT;
760
                                o_bus_ack <= 1'b1;
761
                        end end
762
                `RD_IDLE_GET_PORT: begin
763
                        o_spi_wr <= 1'b1; // Write the address
764
                        o_qspi_req <= 1'b1;
765
                        if (accepted)
766
                                rd_state <= `RD_SLOW_DUMMY;
767
                        end
768
                `RD_SLOW_DUMMY: begin
769
                        o_spi_wr <= 1'b1; // Write 8 dummy clocks
770
                        o_qspi_req <= 1'b1;
771
                        o_spi_dir <= 1'b0;
772
                        o_spi_spd <= 1'b0;
773
                        o_spi_word[31:24] <= (r_xip) ? 8'h00 : 8'hff;
774
                        o_spi_len  <= 2'b00; // 8 clocks = 8-bits
775
                        if (accepted)
776
                                rd_state <= (r_quad)?`RD_QUAD_READ_DATA
777
                                                : `RD_SLOW_READ_DATA;
778
                        end
779
                `RD_SLOW_READ_DATA: begin
780
                        o_qspi_req <= 1'b1;
781
                        o_spi_dir <= 1'b1;
782
                        o_spi_spd <= 1'b0;
783
                        o_spi_len <= 2'b11;
784
                        o_spi_wr <= (~r_requested)||(i_piperd);
785 11 dgisselq
                        invalid_ack_pipe[0] <= (!r_requested);
786
                        o_data_ack <=  (!invalid_ack_pipe[3])&&(i_spi_valid)&&(r_requested);
787 9 dgisselq
                        o_bus_ack <=   (r_requested)&&(accepted)&&(i_piperd);
788
                        r_requested <= (r_requested)||(accepted);
789
                        if ((i_spi_valid)&&(~o_spi_wr))
790
                                rd_state <= `RD_GO_TO_IDLE;
791
                        end
792
                `RD_QUAD_READ_DATA: begin
793 14 dgisselq
                        o_qspi_req <= 1'b1; // Insist that we keep the port
794
                        o_spi_dir <= 1'b1;  // Read
795
                        o_spi_spd <= 1'b1;  // Read at Quad rates
796
                        o_spi_len <= 2'b11; // Read 4-bytes
797 9 dgisselq
                        o_spi_recycle <= (r_leave_xip)? 1'b1: 1'b0;
798 11 dgisselq
                        invalid_ack_pipe[0] <= (!r_requested);
799 14 dgisselq
                        r_requested <= (r_requested)||(accepted); // Make sure at least one request goes through
800 11 dgisselq
                        o_data_ack <=  (!invalid_ack_pipe[3])&&(i_spi_valid)&&(r_requested)&&(~r_leave_xip);
801 9 dgisselq
                        o_bus_ack  <= (r_requested)&&(accepted)&&(i_piperd)&&(~r_leave_xip);
802
                        o_spi_wr <= (~r_requested)||(i_piperd);
803
                        // if (accepted)
804
                                // o_spi_wr <= (i_piperd);
805 11 dgisselq
                        if (accepted) // only happens if (o_spi_wr)
806 9 dgisselq
                                o_data <= i_spi_data;
807
                        if ((i_spi_valid)&&(~o_spi_wr))
808
                                rd_state <= ((r_leave_xip)||(~r_xip))?`RD_GO_TO_IDLE:`RD_GO_TO_XIP;
809
                        end
810
                `RD_QUAD_ADDRESS: begin
811
                        o_qspi_req <= 1'b1;
812
                        o_spi_wr <= 1'b1;
813
                        o_spi_dir <= 1'b0; // Write the address
814
                        o_spi_spd <= 1'b1; // High speed
815
                        o_spi_word[31:0] <= { i_addr, 2'b00, 8'h00 };
816
                        o_spi_len  <= 2'b10; // 24 bits, High speed, 6 clocks
817
                        if (accepted)
818
                                rd_state <= `RD_QUAD_DUMMY;
819
                        end
820
                `RD_QUAD_DUMMY: begin
821
                        o_qspi_req <= 1'b1;
822
                        o_spi_wr <= 1'b1;
823
                        o_spi_dir <= 1'b0; // Write the dummy
824
                        o_spi_spd <= 1'b1; // High speed
825
                        o_spi_word[31:0] <= (r_xip)? 32'h00 : 32'hffffffff;
826
                        o_spi_len  <= 2'b11; // 8 clocks = 32-bits, quad speed
827
                        if (accepted)
828
                                rd_state <= (r_quad)?`RD_QUAD_READ_DATA
829
                                                : `RD_SLOW_READ_DATA;
830
                        end
831
                `RD_XIP: begin
832
                        r_requested <= 1'b0;
833
                        o_qspi_req <= 1'b1;
834
                        o_spi_word <= { i_addr, 2'b00, 8'h00 };
835
                        o_spi_wr <= 1'b0;
836
                        o_spi_dir <= 1'b0; // Write to SPI
837
                        o_spi_spd <= 1'b1; // High speed
838
                        o_spi_len <= 2'b11;
839
                        r_leave_xip <= i_other_req;
840
                        r_xip <= (~i_other_req);
841
                        o_bus_ack <= 1'b0;
842
                        if ((i_readreq)||(i_other_req))
843
                        begin
844
                                rd_state <= `RD_QUAD_ADDRESS;
845
                                o_bus_ack <= i_readreq;
846
                        end end
847
                `RD_GO_TO_IDLE: begin
848 11 dgisselq
                        if ((!invalid_ack_pipe[3])&&(i_spi_valid)&&(~r_leave_xip))
849
                                o_data_ack <=  1'b1;
850
                        o_spi_wr   <= 1'b0;
851 9 dgisselq
                        o_qspi_req <= 1'b0;
852
                        if ((i_spi_stopped)&&(~i_grant))
853
                                rd_state <= `RD_IDLE;
854
                        end
855
                `RD_GO_TO_XIP: begin
856 11 dgisselq
                        r_requested <= 1'b0;
857
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3]))
858
                                o_data_ack <=  1'b1;
859 9 dgisselq
                        o_qspi_req <= 1'b1;
860
                        o_spi_wr   <= 1'b0;
861
                        if (i_spi_stopped)
862
                                rd_state <= `RD_XIP;
863
                        end
864
                default: begin
865
                        // rd_state <= (i_grant)?`RD_BREAK;
866
                        o_qspi_req <= 1'b0;
867
                        o_spi_wr <= 1'b0;
868
                        if ((i_spi_stopped)&&(~i_grant))
869
                                rd_state <= `RD_IDLE;
870
                        end
871
                endcase
872
        end
873
 
874
        assign  o_spi_hold = 1'b0;
875 14 dgisselq
        assign  o_leave_xip = r_leave_xip;
876 9 dgisselq
 
877
endmodule
878
 
879
module  writeqspi(i_clk, i_wreq, i_ereq, i_pipewr, i_endpipe, i_addr, i_data,
880
                        o_bus_ack, o_qspi_req, i_qspi_grant,
881
                                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
882
                                o_spi_spd, o_spi_dir, i_spi_data, i_spi_valid,
883
                                        i_spi_busy, i_spi_stopped,
884
                                o_data_ack, i_quad, o_wip);
885
        input           i_clk;
886
        //
887
        input           i_wreq, i_ereq, i_pipewr, i_endpipe;
888
        input           [21:0]   i_addr;
889
        input           [31:0]   i_data;
890
        output  reg             o_bus_ack, o_qspi_req;
891
        input                   i_qspi_grant;
892
        output  reg             o_spi_wr, o_spi_hold;
893
        output  reg     [31:0]   o_spi_word;
894
        output  reg     [1:0]    o_spi_len;
895
        output  reg             o_spi_spd, o_spi_dir;
896
        input           [31:0]   i_spi_data;
897
        input                   i_spi_valid;
898
        input                   i_spi_busy, i_spi_stopped;
899
        output  reg             o_data_ack;
900
        input                   i_quad;
901
        output  reg             o_wip;
902
 
903
`ifdef  QSPI_READ_ONLY
904
        always @(posedge i_clk)
905
                o_data_ack <= (i_wreq)||(i_ereq);
906
        always @(posedge i_clk)
907
                o_bus_ack <= (i_wreq)||(i_ereq);
908
 
909
        always @(posedge i_clk)
910
        begin
911
                o_qspi_req <= 1'b0;
912
                o_spi_wr   <= 1'b0;
913
                o_spi_hold <= 1'b0;
914
                o_spi_dir  <= 1'b1; // Read
915
                o_spi_spd  <= i_quad;
916
                o_spi_len  <= 2'b00;
917
                o_spi_word <= 32'h00;
918
                o_wip <= 1'b0;
919
        end
920
`else
921
 
922
`define WR_IDLE                         4'h0
923
`define WR_START_WRITE                  4'h1
924
`define WR_START_QWRITE                 4'h2
925
`define WR_PROGRAM                      4'h3
926
`define WR_PROGRAM_GETNEXT              4'h4
927
`define WR_START_ERASE                  4'h5
928
`define WR_WAIT_ON_STOP                 4'h6
929
`define WR_REQUEST_STATUS               4'h7
930
`define WR_REQUEST_STATUS_NEXT          4'h8
931
`define WR_READ_STATUS                  4'h9
932
`define WR_WAIT_ON_FINAL_STOP           4'ha
933
 
934
        reg     accepted;
935
        initial accepted = 1'b0;
936
        always @(posedge i_clk)
937
                accepted <= (~i_spi_busy)&&(i_qspi_grant)&&(o_spi_wr)&&(~accepted);
938
 
939
 
940 11 dgisselq
        reg             cyc, chk_wip, valid_status;
941 9 dgisselq
        reg     [3:0]    wr_state;
942
        initial wr_state = `WR_IDLE;
943
        initial cyc = 1'b0;
944
        always @(posedge i_clk)
945
        begin
946
                chk_wip <= 1'b0;
947
                o_bus_ack  <= 1'b0;
948
                o_data_ack <= 1'b0;
949
                case(wr_state)
950
                `WR_IDLE: begin
951 11 dgisselq
                        valid_status <= 1'b0;
952 9 dgisselq
                        o_qspi_req <= 1'b0;
953
                        cyc <= 1'b0;
954
                        if (i_ereq)
955
                                wr_state <= `WR_START_ERASE;
956
                        else if (i_wreq)
957
                                wr_state <= (i_quad)?`WR_START_QWRITE
958
                                        : `WR_START_WRITE;
959
                        end
960
                `WR_START_WRITE: begin
961
                        o_wip      <= 1'b1;
962
                        o_qspi_req <= 1'b1;
963
                        o_spi_wr   <= 1'b1;
964
                        o_spi_dir  <= 1'b0;
965
                        o_spi_len  <= 2'b11;
966
                        o_spi_spd  <= 1'b0;
967
                        o_spi_hold <= 1'b1;
968
                        o_spi_word <= { 8'h02, i_addr, 2'b00 };
969
                        cyc <= 1'b1;
970
                        if (accepted)
971
                        begin
972
                                o_bus_ack  <= 1'b1;
973
                                o_data_ack <= 1'b1;
974
                                wr_state <= `WR_PROGRAM;
975
                                o_spi_word <= i_data;
976
                        end end
977
                `WR_START_QWRITE: begin
978
                        o_wip      <= 1'b1;
979
                        o_qspi_req <= 1'b1;
980
                        o_spi_wr   <= 1'b1;
981
                        o_spi_dir  <= 1'b0;
982
                        o_spi_len  <= 2'b11;
983
                        o_spi_spd  <= 1'b0;
984
                        o_spi_hold <= 1'b1;
985
                        o_spi_word <= { 8'h32, i_addr, 2'b00 };
986
                        cyc <= 1'b1;
987
                        if (accepted)
988
                        begin
989
                                o_bus_ack  <= 1'b1;
990
                                o_data_ack <= 1'b1;
991
                                wr_state <= `WR_PROGRAM;
992
                                o_spi_word <= i_data;
993
                        end end
994
                `WR_PROGRAM: begin
995
                        o_wip     <= 1'b1;
996
                        o_qspi_req <= 1'b1;
997
                        o_spi_wr   <= 1'b1;
998
                        o_spi_dir  <= 1'b0;
999
                        o_spi_len  <= 2'b11;
1000
                        o_spi_spd  <= i_quad;
1001
                        o_spi_hold <= 1'b1;
1002
                        // o_spi_word <= i_data;
1003
                        if (accepted)
1004
                                wr_state <= `WR_PROGRAM_GETNEXT;
1005
                        end
1006
                `WR_PROGRAM_GETNEXT: begin
1007
                        o_wip      <= 1'b1;
1008
                        o_qspi_req <= 1'b1;
1009
                        o_spi_wr   <= 1'b0;
1010
                        o_spi_dir  <= 1'b0;
1011
                        o_spi_len  <= 2'b11;
1012
                        o_spi_spd  <= i_quad;
1013
                        o_spi_hold <= 1'b1;
1014
                        o_spi_word <= i_data;
1015 11 dgisselq
                        cyc <= (cyc)&&(~i_endpipe);
1016 9 dgisselq
                        if (~cyc)
1017
                                wr_state <= `WR_WAIT_ON_STOP;
1018
                        else if (i_pipewr)
1019
                        begin
1020
                                o_bus_ack  <= 1'b1;
1021
                                o_data_ack <= 1'b1;
1022
                                wr_state <= `WR_PROGRAM;
1023
                        end end
1024
                `WR_START_ERASE: begin
1025
                        o_wip <= 1'b1;
1026
                        o_qspi_req <= 1'b1;
1027
                        o_spi_wr  <= 1'b1;
1028
                        o_spi_dir <= 1'b0;
1029
                        o_spi_spd <= 1'b0;
1030
                        o_spi_len <= 2'b11;
1031
                        if (i_data[28])
1032
                                // Subsector erase
1033
                                o_spi_word[31:24] <= 8'h20;
1034
                        else
1035
                                // Sector erase
1036
                                o_spi_word[31:24] <= 8'hd8;
1037
                        o_spi_word[23:0] <= { i_data[21:10], 12'h0 };
1038
                        // Data has already been ack'd, so no need to ack
1039
                        // it again.  However, we can now free the QSPI
1040
                        // bus processor to accept another command from the
1041
                        // bus.
1042
                        o_bus_ack <= accepted;
1043
                        if (accepted)
1044
                                wr_state <= `WR_WAIT_ON_STOP;
1045
                        end
1046
                `WR_WAIT_ON_STOP: begin
1047
                        o_wip <= 1'b1;
1048
                        o_qspi_req <= 1'b0;
1049
                        o_spi_wr   <= 1'b0;
1050
                        o_spi_hold <= 1'b0;
1051
                        if (i_spi_stopped)
1052
                                wr_state <= `WR_REQUEST_STATUS;
1053
                        end
1054
                `WR_REQUEST_STATUS: begin
1055
                        o_wip <= 1'b1;
1056
                        o_qspi_req <= 1'b1;
1057
                        o_spi_hold <= 1'b0;
1058
                        o_spi_wr   <= 1'b1;
1059
                        o_spi_spd  <= 1'b0; // Slow speed
1060
                        o_spi_len  <= 2'b00; // 8 bytes
1061
                        o_spi_dir  <= 1'b0; // Write
1062
                        o_spi_word <= { 8'h05, 24'h00 };
1063
                        if (accepted)
1064
                                wr_state <= `WR_REQUEST_STATUS_NEXT;
1065
                        end
1066
                `WR_REQUEST_STATUS_NEXT: begin
1067
                        o_wip <= 1'b1;
1068
                        o_qspi_req <= 1'b1;
1069
                        o_spi_hold <= 1'b0;
1070
                        o_spi_wr   <= 1'b1;
1071
                        o_spi_spd  <= 1'b0; // Slow speed
1072
                        o_spi_len  <= 2'b00; // 8 bytes
1073
                        o_spi_dir  <= 1'b1; // Read
1074
                        o_spi_word <= 32'h00;
1075
                        if (accepted)
1076
                                wr_state <= `WR_READ_STATUS;
1077 11 dgisselq
                        valid_status <= 1'b0;
1078 9 dgisselq
                        end
1079
                `WR_READ_STATUS: begin
1080
                        o_wip <= 1'b1;
1081
                        o_qspi_req <= 1'b1;
1082
                        o_spi_hold <= 1'b0;
1083
                        o_spi_wr   <= 1'b1;
1084
                        o_spi_spd  <= 1'b0; // Slow speed
1085
                        o_spi_len  <= 2'b00; // 8 bytes
1086
                        o_spi_dir  <= 1'b1; // Read
1087
                        o_spi_word <= 32'h00;
1088
                        if (i_spi_valid)
1089 11 dgisselq
                                valid_status <= 1'b1;
1090
                        if ((i_spi_valid)&&(valid_status))
1091 9 dgisselq
                                chk_wip <= 1'b1;
1092
                        if ((chk_wip)&&(~i_spi_data[0]))
1093
                                wr_state <= `WR_WAIT_ON_FINAL_STOP;
1094
                        end
1095
                // `WR_WAIT_ON_FINAL_STOP: // Same as the default
1096
                default: begin
1097
                        o_qspi_req <= 1'b0;
1098
                        o_spi_wr <= 1'b0;
1099
                        o_wip <= 1'b0;
1100
                        if (i_spi_stopped)
1101
                                wr_state <= `WR_IDLE;
1102
                        end
1103
                endcase
1104
        end
1105
`endif
1106
 
1107
endmodule
1108
 
1109
 
1110
`define CT_SAFE
1111
`define CT_IDLE                 3'h0
1112
`define CT_NEXT                 3'h1
1113
`define CT_GRANTED              3'h2
1114
`define CT_DATA                 3'h3
1115
`define CT_READ_DATA            3'h4
1116
`define CT_WAIT_FOR_IDLE        3'h5
1117
 
1118
// CTRL commands:
1119
//      WEL (write-enable latch)
1120
//      Read Status
1121
module  ctrlspi(i_clk, i_req, i_wr, i_addr, i_data, i_sector_address,
1122
                                o_spi_req, i_grant,
1123
                                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
1124
                                        o_spi_spd, o_spi_dir,
1125
                                i_spi_data, i_spi_valid, i_spi_busy,
1126
                                        i_spi_stopped,
1127 14 dgisselq
                                o_bus_ack, o_data_ack, o_data,
1128
                                i_leave_xip, o_xip, o_quad);
1129 9 dgisselq
        input           i_clk;
1130
        // From the WB bus controller
1131
        input                   i_req;
1132
        input                   i_wr;
1133 14 dgisselq
        input           [3:0]    i_addr;
1134 9 dgisselq
        input           [31:0]   i_data;
1135
        input           [21:0]   i_sector_address;
1136
        // To/from the arbiter
1137
        output  reg             o_spi_req;
1138
        input                   i_grant;
1139
        // To/from the low-level SPI driver
1140
        output  reg             o_spi_wr;
1141
        output  wire            o_spi_hold;
1142
        output  reg     [31:0]   o_spi_word;
1143
        output  reg     [1:0]    o_spi_len;
1144
        output  wire            o_spi_spd;
1145
        output  reg             o_spi_dir;
1146
        input           [31:0]   i_spi_data;
1147
        input                   i_spi_valid;
1148
        input                   i_spi_busy, i_spi_stopped;
1149
        // Return data to the bus controller, and the wishbone bus
1150
        output  reg             o_bus_ack, o_data_ack;
1151
        output  reg     [31:0]   o_data;
1152
        // Configuration items that we may have configured.
1153 14 dgisselq
        input   wire            i_leave_xip;
1154 9 dgisselq
        output  reg             o_xip;
1155
        output  wire            o_quad;
1156
 
1157
        // Command registers
1158
        reg     [1:0]    ctcmd_len;
1159
        reg     [31:0]   ctcmd_word;
1160
        // Data stage registers
1161
        reg             ctdat_skip, // Skip the data phase?
1162
                        ctdat_wr;       // Write during data? (or not read)
1163
        wire    [1:0]    ctdat_len;
1164
        reg     [31:0]   ctdat_word;
1165
 
1166
        reg     [2:0]    ctstate;
1167
        reg             accepted;
1168 11 dgisselq
        reg     [3:0]    invalid_ack_pipe;
1169 9 dgisselq
 
1170
 
1171
        initial accepted = 1'b0;
1172
        always @(posedge i_clk)
1173
                accepted <= (~i_spi_busy)&&(i_grant)&&(o_spi_wr)&&(~accepted);
1174
 
1175 11 dgisselq
        reg     r_ctdat_len, ctbus_ack, first_valid;
1176 9 dgisselq
        assign  ctdat_len = { 1'b0, r_ctdat_len };
1177
 
1178
        // First step, calculate the values for our state machine
1179
        initial o_xip = 1'b0;
1180
        // initial o_quad = 1'b0;
1181
        always @(posedge i_clk)
1182
        if (i_req) // A request for us to act from the bus controller
1183
        begin
1184
                ctdat_skip <= 1'b0;
1185
                ctbus_ack  <= 1'b1;
1186
                ctcmd_word[23:0] <= { i_sector_address, 2'b00 };
1187
                ctdat_word <= { i_data[7:0], 24'h00 };
1188
                ctcmd_len <= 2'b00; // 8bit command (for all but Lock regs)
1189
                r_ctdat_len <= 1'b0; // 8bit data (read or write)
1190
                ctdat_wr <= i_wr;
1191 14 dgisselq
                casez({ i_addr[3:0], i_wr, i_data[30] })
1192
                6'b000010: begin // Write Disable
1193 9 dgisselq
                        ctcmd_word[31:24] <= 8'h04;
1194
                        ctdat_skip <= 1'b1;
1195
                        ctbus_ack  <= 1'b0;
1196
                        end
1197 14 dgisselq
                6'b000011: begin // Write enable
1198 9 dgisselq
                        ctcmd_word[31:24] <= 8'h06;
1199
                        ctdat_skip <= 1'b1;
1200
                        ctbus_ack  <= 1'b0;
1201
                        end
1202
                // 4'b0010?: begin // Read Status register
1203
                //      Moved to defaults section
1204 14 dgisselq
                6'b00011?: begin // Write Status register (Requires WEL)
1205 9 dgisselq
                        ctcmd_word[31:24] <= 8'h01;
1206
`ifdef  CT_SAFE
1207
                        ctdat_word <= { 6'h00, i_data[1:0], 24'h00 };
1208
`else
1209
                        ctdat_word <= { i_data[7:0], 24'h00 };
1210
`endif
1211
                        end
1212 14 dgisselq
                6'b00100?: begin // Read NV-Config register (two bytes)
1213 9 dgisselq
                        ctcmd_word[31:24] <= 8'hB5;
1214
                        r_ctdat_len <= 1'b1; // 16-bit data
1215
                        end
1216 14 dgisselq
                6'b00101?: begin // Write NV-Config reg (2 bytes, Requires WEL)
1217 9 dgisselq
                        ctcmd_word[31:24] <= 8'hB1;
1218
                        r_ctdat_len <= 1'b1; // 16-bit data
1219
`ifdef  CT_SAFE
1220
                        ctdat_word <= { 4'h8, 3'h7, 3'h7, i_data[5:1], 1'b1, 16'h00 };
1221
`else
1222
                        ctdat_word <= { i_data[15:0], 16'h00 };
1223
`endif
1224
                        end
1225 14 dgisselq
                6'b00110?: begin // Read V-Config register
1226 9 dgisselq
                        ctcmd_word[31:24] <= 8'h85;
1227
                        end
1228 14 dgisselq
                6'b00111?: begin // Write V-Config register (Requires WEL)
1229 9 dgisselq
                        ctcmd_word[31:24] <= 8'h81;
1230
                        r_ctdat_len <= 1'b0; // 8-bit data
1231
`ifdef  CT_SAFE
1232
                        ctdat_word <= { 4'h8, i_data[3:2], 2'b11, 24'h00 };
1233
`else
1234
                        ctdat_word <= { i_data[7:0], 24'h00 };
1235
`endif
1236 14 dgisselq
                        o_xip <= ~i_data[3];
1237 9 dgisselq
                        end
1238 14 dgisselq
                6'b01000?: begin // Read EV-Config register
1239 9 dgisselq
                        ctcmd_word[31:24] <= 8'h65;
1240
                        end
1241 14 dgisselq
                6'b01001?: begin // Write EV-Config register (Requires WEL)
1242 9 dgisselq
                        ctcmd_word[31:24] <= 8'h61;
1243
                        // o_quad <= (~i_data[7]);
1244
`ifdef  CT_SAFE
1245
                        ctdat_word <= { 1'b1, 3'h5, 4'hf, 24'h00 };
1246
`else
1247
                        ctdat_word <= { i_data[7:0], 24'h00 };
1248
`endif
1249
                        end
1250 14 dgisselq
                6'b01010?: begin // Read Lock register
1251 9 dgisselq
                        ctcmd_word[31:0] <= { 8'he8,  i_sector_address, 2'b00 };
1252
                        ctcmd_len <= 2'b11;
1253
                        ctdat_wr  <= 1'b0;  // Read, not write
1254
                        end
1255 14 dgisselq
                6'b01011?: begin // Write Lock register (Requires WEL)
1256 9 dgisselq
                        ctcmd_word[31:0] <= { 8'he5, i_sector_address, 2'b00 };
1257
                        ctcmd_len <= 2'b11;
1258
                        ctdat_wr  <= 1'b1;  // Write
1259
                        end
1260 14 dgisselq
                6'b01100?: begin // Read Flag Status register
1261 9 dgisselq
                        ctcmd_word[31:24] <= 8'h70;
1262
                        ctdat_wr  <= 1'b0;  // Read, not write
1263
                        end
1264 14 dgisselq
                6'b01101?: begin // Write/Clear Flag Status register (No WEL required)
1265 9 dgisselq
                        ctcmd_word[31:24] <= 8'h50;
1266
                        ctdat_skip <= 1'b1;
1267
                        end
1268 14 dgisselq
                6'b11011?: begin // RESET_ENABLE (when written to)
1269
                        ctcmd_word[31:24] <= 8'h66;
1270
                        ctdat_skip <= 1'b1;
1271
                        end
1272
                6'b11101?: begin // RESET_MEMORY (when written to)
1273
                        ctcmd_word[31:24] <= 8'h99;
1274
                        ctdat_skip <= 1'b1;
1275
                        end
1276 9 dgisselq
                default: begin // Default to reading the status register
1277
                        ctcmd_word[31:24] <= 8'h05;
1278
                        ctdat_wr  <= 1'b0;  // Read, not write
1279
                        r_ctdat_len <= 1'b0; // 8-bit data
1280
                        end
1281
                endcase
1282 14 dgisselq
        end else if (i_leave_xip)
1283
                o_xip <= 1'b0;
1284 9 dgisselq
 
1285 10 dgisselq
        assign  o_quad = 1'b1;
1286 9 dgisselq
 
1287
        reg     nxt_data_ack;
1288
 
1289
        // Second step, actually drive the state machine
1290
        initial ctstate = `CT_IDLE;
1291
        always @(posedge i_clk)
1292
        begin
1293
                o_spi_wr <= 1'b1;
1294
                o_bus_ack <= 1'b0;
1295
                o_data_ack <= 1'b0;
1296 11 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
1297 9 dgisselq
                if (i_spi_valid)
1298
                        o_data <= i_spi_data;
1299
                case(ctstate)
1300
                `CT_IDLE: begin
1301
                        o_spi_req <= 1'b0;
1302
                        o_spi_wr  <= 1'b0;
1303
                        if (i_req) // Need a clock to let the digestion
1304
                                ctstate <= `CT_NEXT; // process complete
1305
                        end
1306
                `CT_NEXT: begin
1307
                        o_spi_wr <= 1'b1;
1308
                        o_spi_req <= 1'b1;
1309
                        o_spi_word <= ctcmd_word;
1310
                        o_spi_len <= ctcmd_len;
1311
                        o_spi_dir <= 1'b0; // Write
1312
                        if (accepted)
1313
                        begin
1314
                                ctstate <= (ctdat_skip)?`CT_WAIT_FOR_IDLE:`CT_DATA;
1315
                                o_bus_ack <= (ctdat_skip);
1316
                                o_data_ack <= (ctdat_skip)&&(ctbus_ack);
1317
                        end end
1318
                `CT_GRANTED: begin
1319
                        o_spi_wr <= 1'b1;
1320
                        if ((accepted)&&(ctdat_skip))
1321
                                ctstate <= `CT_WAIT_FOR_IDLE;
1322
                        else if (accepted)//&&(~ctdat_skip)
1323
                                ctstate <= `CT_DATA;
1324
                        end
1325
                `CT_DATA: begin
1326
                        o_spi_wr   <= 1'b1;
1327
                        o_spi_len  <= ctdat_len;
1328
                        o_spi_dir  <= ~ctdat_wr;
1329
                        o_spi_word <= ctdat_word;
1330
                        if (accepted)
1331
                                o_bus_ack <= 1'b1;
1332
                        if (accepted)
1333
                                ctstate <= (ctdat_wr)?`CT_WAIT_FOR_IDLE:`CT_READ_DATA;
1334
                        if ((accepted)&&(ctdat_wr))
1335
                                o_data_ack <= 1'b1;
1336 11 dgisselq
                        first_valid <= 1'b0;
1337 9 dgisselq
                        end
1338
                `CT_READ_DATA: begin
1339
                        o_spi_wr <= 1'b0; // No more words to go, just to wait
1340
                        o_spi_req <= 1'b1;
1341 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1342
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3])) // for a value to read
1343 9 dgisselq
                        begin
1344
                                o_data_ack <= 1'b1;
1345
                                o_data <= i_spi_data;
1346
                                ctstate <= `CT_WAIT_FOR_IDLE;
1347
                        end end
1348
                default: begin // `CT_WAIT_FOR_IDLE
1349
                        o_spi_wr <= 1'b0;
1350
                        o_spi_req <= 1'b0;
1351
                        if (i_spi_stopped)
1352
                                ctstate <= `CT_IDLE;
1353
                        end
1354
                endcase
1355
        end
1356
 
1357
        // All of this is done in straight SPI mode, so our speed will always be zero
1358
        assign  o_spi_hold = 1'b0;
1359
        assign  o_spi_spd  = 1'b0;
1360
 
1361
endmodule
1362
 
1363
`define ID_IDLE                         5'h00
1364
`define ID_WAIT_ON_START_ID             5'h01
1365
`define ID_WAIT_ON_START_OTP            5'h02
1366
`define ID_WAIT_ON_START_OTP_WRITE      5'h03
1367
`define ID_READ_DATA_COMMAND            5'h04
1368
`define ID_GET_DATA                     5'h05
1369
`define ID_LOADED                       5'h06
1370
`define ID_LOADED_NEXT                  5'h07
1371
`define ID_OTP_SEND_DUMMY               5'h08
1372
`define ID_OTP_CLEAR                    5'h09
1373
`define ID_OTP_GET_DATA                 5'h0a
1374
`define ID_OTP_WRITE                    5'h0b
1375
`define ID_WAIT_ON_STOP                 5'h0c
1376
`define ID_REQ_STATUS                   5'h0d
1377
`define ID_REQ_STATUS_NEXT              5'h0e
1378
`define ID_READ_STATUS                  5'h0f
1379
//
1380
`define ID_FINAL_STOP                   5'h10
1381
 
1382
module  idotpqspi(i_clk, i_req, i_wr, i_pipewr, i_addr, i_data, o_bus_ack,
1383
                o_qspi_req, i_qspi_grant,
1384
                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
1385
                o_spi_spd, o_spi_dir, i_spi_data, i_spi_valid,
1386
                i_spi_busy, i_spi_stopped, o_data_ack, o_data, o_loaded,
1387
                o_wip);
1388
        input                   i_clk;
1389
        input                   i_req, i_wr, i_pipewr;
1390
        input           [4:0]    i_addr;
1391
        input           [31:0]   i_data;
1392
        output  reg             o_bus_ack, o_qspi_req;
1393
        input                   i_qspi_grant;
1394
        output  reg             o_spi_wr, o_spi_hold;
1395
        output  reg     [31:0]   o_spi_word;
1396
        output  reg     [1:0]    o_spi_len;
1397
        output  wire            o_spi_spd;
1398
        output  reg             o_spi_dir;
1399
        input           [31:0]   i_spi_data;
1400
        input                   i_spi_valid, i_spi_busy, i_spi_stopped;
1401
        output  reg             o_data_ack;
1402
        output  reg     [31:0]   o_data;
1403
        output  wire            o_loaded;
1404
        output  reg             o_wip;
1405
 
1406
        reg     id_loaded;
1407
        initial id_loaded = 1'b0;
1408
        assign  o_loaded= id_loaded;
1409
 
1410
/*
1411
        // Only the ID register will be kept in memory, OTP will be read
1412
        // or written upon request
1413
        always @(posedge i_clk)
1414
                if (i_addr[4])
1415
                        o_data <= otpmem[i_addr[3:0]];
1416
                else
1417
                        o_data <= idmem[i_addr[2:0]];
1418
 
1419
        always @(posedge i_clk)
1420
                if ((otp_loaded)&&(i_req)&&(i_addr[4]))
1421
                        o_data_ack <= 1'b1;
1422
                else if ((id_loaded)&&(i_req)&&(~i_addr[4]))
1423
                        o_data_ack <= idmem[i_addr[2:0]];
1424
                else
1425
                        o_data_ack <= 1'b0;
1426
*/
1427
 
1428
        reg     otp_read_request, id_read_request, accepted, otp_wr_request,
1429
                id_read_device, last_id_read;
1430
        reg     [4:0]    req_addr;
1431
        reg     [2:0]    lcl_id_addr;
1432
        reg     [4:0]    id_state;
1433
        always @(posedge i_clk)
1434
        begin
1435
                otp_read_request <= (i_req)&&(~i_wr)&&((i_addr[4])||(i_addr[3:0]==4'hf));
1436
                last_id_read     <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf);
1437
                id_read_request  <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf)&&(~last_id_read);
1438
                id_read_device   <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf)&&(~id_loaded);
1439
                accepted <= (~i_spi_busy)&&(i_qspi_grant)&&(o_spi_wr)&&(~accepted);
1440
 
1441
                otp_wr_request <= (i_req)&&(i_wr)&&((i_addr[4])||(i_addr[3:0]==4'hf));
1442
 
1443
                if (id_state == `ID_IDLE)
1444
                        req_addr <= (i_addr[4:0]==5'h0f) ? 5'h10
1445
                                : { 1'b0, i_addr[3:0] };
1446
        end
1447
 
1448
        reg     last_addr;
1449
        always @(posedge i_clk)
1450
                last_addr <= (lcl_id_addr >= 3'h4);
1451
 
1452
        reg     [31:0]   idmem[0:5];
1453
        reg     [31:0]   r_data;
1454
 
1455
        // Now, quickly, let's deal with the fact that the data from the
1456
        // bus comes one clock later ...
1457
        reg     nxt_data_ack, nxt_data_spi;
1458
        reg     [31:0]   nxt_data;
1459
 
1460 11 dgisselq
        reg     set_val, chk_wip, first_valid;
1461 9 dgisselq
        reg     [2:0]    set_addr;
1462 11 dgisselq
        reg     [3:0]    invalid_ack_pipe;
1463 9 dgisselq
 
1464
        always @(posedge i_clk)
1465
        begin // Depends upon state[4], otp_rd, otp_wr, otp_pipe, id_req, accepted, last_addr
1466
                o_bus_ack <= 1'b0;
1467
                // o_data_ack <= 1'b0;
1468
                o_spi_hold <= 1'b0;
1469
                nxt_data_ack <= 1'b0;
1470
                nxt_data_spi <= 1'b0;
1471
                chk_wip      <= 1'b0;
1472
                set_val <= 1'b0;
1473 11 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
1474 9 dgisselq
                if ((id_loaded)&&(id_read_request))
1475
                begin
1476
                        nxt_data_ack <= 1'b1;
1477
                        o_bus_ack  <= 1'b1;
1478
                end
1479
                nxt_data <= idmem[i_addr[2:0]];
1480
                o_spi_wr <= 1'b0; // By default, we send nothing
1481
                case(id_state)
1482
                `ID_IDLE: begin
1483
                        o_qspi_req <= 1'b0;
1484
                        o_spi_dir <= 1'b0; // Write to SPI
1485
                        lcl_id_addr <= 3'h0;
1486
                        o_spi_word[23:7] <= 17'h00;
1487
                        o_spi_word[6:0] <= { req_addr[4:0], 2'b00 };
1488
                        r_data <= i_data;
1489
                        o_wip <= 1'b0;
1490 11 dgisselq
                        first_valid <= 1'b0;
1491 9 dgisselq
                        if (otp_read_request)
1492
                        begin
1493
                                // o_spi_word <= { 8'h48, 8'h00, 8'h00, 8'h00 };
1494
                                id_state <= `ID_WAIT_ON_START_OTP;
1495
                                o_bus_ack <= 1'b1;
1496
                        end else if (otp_wr_request)
1497
                        begin
1498
                                o_bus_ack <= 1'b1;
1499
                                // o_data_ack <= 1'b1;
1500
                                nxt_data_ack <= 1'b1;
1501
                                id_state <= `ID_WAIT_ON_START_OTP_WRITE;
1502
                        end else if (id_read_device)
1503
                        begin
1504
                                id_state <= `ID_WAIT_ON_START_ID;
1505
                                o_bus_ack <= 1'b0;
1506
                                o_spi_word[31:24] <= 8'h9f;
1507
                        end end
1508
                `ID_WAIT_ON_START_ID: begin
1509
                        o_spi_wr <= 1'b1;
1510
                        o_qspi_req <= 1'b1;
1511
                        o_spi_len <= 2'b0; // 8 bits
1512
                        if (accepted)
1513
                                id_state <= `ID_READ_DATA_COMMAND;
1514
                        end
1515
                `ID_WAIT_ON_START_OTP: begin
1516
                        o_spi_wr <= 1'b1;
1517
                        o_spi_word[31:24] <= 8'h4B;
1518
                        o_qspi_req <= 1'b1;
1519
                        o_spi_len <= 2'b11; // 32 bits
1520
                        o_spi_word[6:0] <= { req_addr[4:0], 2'b00 };
1521
                        if (accepted) // Read OTP command was just sent
1522
                                id_state <= `ID_OTP_SEND_DUMMY;
1523
                        end
1524
                `ID_WAIT_ON_START_OTP_WRITE: begin
1525
                        o_spi_wr <= 1'b1;
1526
                        o_qspi_req <= 1'b1;
1527
                        o_wip <= 1'b1;
1528
                        o_spi_len <= 2'b11; // 32 bits
1529
                        o_spi_word[31:24] <= 8'h42;
1530
                        if (accepted) // Read OTP command was just sent
1531
                                id_state <= `ID_OTP_WRITE;
1532
                        end
1533
                `ID_READ_DATA_COMMAND: begin
1534
                        o_spi_len <= 2'b11; // 32-bits
1535 11 dgisselq
                        o_spi_wr <= 1'b1; // Still transmitting
1536 9 dgisselq
                        o_spi_dir <= 1'b1; // Read from SPI
1537
                        o_qspi_req <= 1'b1;
1538
                        if (accepted)
1539
                                id_state <= `ID_GET_DATA;
1540 11 dgisselq
                        first_valid <= 1'b0;
1541 9 dgisselq
                        end
1542
                `ID_GET_DATA: begin
1543
                        o_spi_len <= 2'b11; // 32-bits
1544
                        o_spi_wr <= (~last_addr); // Still transmitting
1545
                        o_spi_dir <= 1'b1; // Read from SPI
1546
                        o_qspi_req <= 1'b1;
1547 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1548
                        if((i_spi_valid)&&(!invalid_ack_pipe[3]))
1549 9 dgisselq
                        begin
1550
                                set_val <= 1'b1;
1551
                                set_addr <= lcl_id_addr[2:0];
1552
                                // idmem[lcl_id_addr[2:0]] <= i_spi_data;
1553
                                lcl_id_addr <= lcl_id_addr + 3'h1;
1554
                                if (last_addr)
1555
                                        id_state <= `ID_LOADED;
1556
                        end end
1557
                `ID_LOADED: begin
1558
                        id_loaded <= 1'b1;
1559
                        o_bus_ack  <= 1'b1;
1560
                        o_spi_wr   <= 1'b0;
1561
                        nxt_data_ack <= 1'b1;
1562
                        id_state   <= `ID_LOADED_NEXT;
1563
                        end
1564
                `ID_LOADED_NEXT: begin
1565
                        o_spi_len <= 2'b11; // 32-bits
1566
                        o_bus_ack  <= 1'b0;
1567
                        o_spi_wr   <= 1'b0;
1568
                        nxt_data_ack <= 1'b1;
1569
                        id_state   <= `ID_IDLE;
1570
                        end
1571
                `ID_OTP_SEND_DUMMY: begin
1572
                        o_spi_len <= 2'b00; // 1 byte
1573
                        o_spi_wr  <= 1'b1; // Still writing
1574
                        o_spi_dir <= 1'b0; // Write to SPI
1575
                        if (accepted) // Wait for the command to be accepted
1576
                                id_state <= `ID_OTP_CLEAR;
1577
                        end
1578
                `ID_OTP_CLEAR: begin
1579
                        o_spi_wr  <= 1'b1; // Still writing
1580
                        o_spi_dir <= 1'b1; // Read from SPI
1581 11 dgisselq
                        o_spi_len <= 2'b11; // Read 32 bits
1582 9 dgisselq
                        if (accepted)
1583
                                id_state <= `ID_OTP_GET_DATA;
1584
                        end
1585
                `ID_OTP_GET_DATA: begin
1586 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1587
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3]))
1588 9 dgisselq
                        begin
1589
                                id_state <= `ID_FINAL_STOP;
1590
                                nxt_data_ack <= 1'b1;
1591
                                nxt_data_spi <= 1'b1;
1592
                        end end
1593
                `ID_OTP_WRITE: begin
1594
                        o_spi_wr  <= 1'b1;
1595
                        o_spi_len <= 2'b11;
1596
                        o_spi_dir <= 1'b0; // Write to SPI
1597
                        o_spi_word <= r_data;
1598
                        // o_bus_ack  <= (otp_wr_request)&&(accepted)&&(i_pipewr);
1599
                        // o_data_ack <= (otp_wr_request)&&(accepted);
1600
                        if (accepted) // &&(~i_pipewr)
1601
                                id_state <= `ID_WAIT_ON_STOP;
1602
                        else if(accepted)
1603
                        begin
1604
                                o_spi_word <= i_data;
1605
                                r_data <= i_data;
1606
                        end end
1607
                `ID_WAIT_ON_STOP: begin
1608
                        o_spi_wr <= 1'b0;
1609
                        if (i_spi_stopped)
1610
                                id_state <= `ID_REQ_STATUS;
1611
                        end
1612
                `ID_REQ_STATUS: begin
1613
                        o_spi_wr <= 1'b1;
1614
                        o_spi_hold <= 1'b0;
1615
                        o_spi_word[31:24] <= 8'h05;
1616
                        o_spi_dir <= 1'b0;
1617
                        o_spi_len <= 2'b00;
1618
                        if (accepted)
1619
                                id_state <= `ID_REQ_STATUS_NEXT;
1620
                        end
1621
                `ID_REQ_STATUS_NEXT: begin
1622
                        o_spi_wr <= 1'b1;
1623
                        o_spi_hold <= 1'b0;
1624
                        o_spi_dir <= 1'b1; // Read
1625
                        o_spi_len <= 2'b00; // 8 bits
1626
                        // o_spi_word <= dont care
1627
                        if (accepted)
1628
                                id_state <= `ID_READ_STATUS;
1629
                        end
1630
                `ID_READ_STATUS: begin
1631
                        o_spi_wr <= 1'b1;
1632
                        o_spi_hold <= 1'b0;
1633
                        o_spi_dir <= 1'b1; // Read
1634
                        o_spi_len <= 2'b00; // 8 bits
1635
                        // o_spi_word <= dont care
1636 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1637
                        if ((i_spi_valid)&&(~invalid_ack_pipe[3]))
1638 9 dgisselq
                                chk_wip <= 1'b1;
1639
                        if ((chk_wip)&&(~i_spi_data[0]))
1640
                        begin
1641
                                o_wip <= 1'b0;
1642
                                id_state <= `ID_FINAL_STOP;
1643
                        end end
1644
                default: begin // ID_FINAL_STOP
1645
                        o_bus_ack <= 1'b0;
1646
                        nxt_data_ack <= 1'b0;
1647
                        o_qspi_req <= 1'b0;
1648
                        o_spi_wr <= 1'b0;
1649
                        o_spi_hold <= 1'b0;
1650
                        o_spi_dir <= 1'b1; // Read
1651
                        o_spi_len <= 2'b00; // 8 bits
1652
                        // o_spi_word <= dont care
1653
                        if (i_spi_stopped)
1654
                                id_state <= `ID_IDLE;
1655
                        end
1656
                endcase
1657
        end
1658
 
1659
        always @(posedge i_clk)
1660
        begin
1661
                if (nxt_data_ack)
1662
                        o_data <= (nxt_data_spi)?i_spi_data : nxt_data;
1663
                o_data_ack <= nxt_data_ack;
1664
        end
1665
 
1666
        always @(posedge i_clk)
1667
                if (set_val)
1668
                        idmem[set_addr] <= i_spi_data;
1669
 
1670
        assign  o_spi_spd = 1'b0; // Slow, 1-bit at a time
1671
 
1672
endmodule
1673
 
1674
 
1675
 

powered by: WebSVN 2.1.0

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