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

Subversion Repositories qspiflash

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

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 16 dgisselq
// Project:     Wishbone Controlled Quad SPI Flash Controller
6 9 dgisselq
//
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 16 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
94 9 dgisselq
// 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 16 dgisselq
module  eqspiflash(i_clk_82mhz, i_rst,
106 9 dgisselq
                // 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 16 dgisselq
        input                   i_clk_82mhz, i_rst;
124 9 dgisselq
        // 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 16 dgisselq
        always @(posedge i_clk_82mhz)
146 9 dgisselq
                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 16 dgisselq
        lleqspi lowlvl(i_clk_82mhz, 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 16 dgisselq
        qspibus preproc(i_clk_82mhz, i_rst,
185 9 dgisselq
                        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 16 dgisselq
        readqspi        rdproc(i_clk_82mhz, bus_readreq, bus_piperd,
215 9 dgisselq
                                        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 16 dgisselq
        writeqspi       ewproc(i_clk_82mhz, bus_wreq,bus_ereq,
245 9 dgisselq
                                        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 16 dgisselq
        ctrlspi         ctproc(i_clk_82mhz,
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 16 dgisselq
        idotpqspi       idotp(i_clk_82mhz, bus_idreq,
303 9 dgisselq
                                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 16 dgisselq
        always @(posedge i_clk_82mhz) // 7 inputs (spi_stopped is the CE)
315 9 dgisselq
                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 16 dgisselq
        always @(posedge i_clk_82mhz)
342 9 dgisselq
        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 16 dgisselq
        always @(posedge i_clk_82mhz)
386 9 dgisselq
        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 16 dgisselq
        always @(posedge i_clk_82mhz)
395 9 dgisselq
        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 16 dgisselq
        reg     [12:0]   esector;
455 9 dgisselq
        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 16 dgisselq
        initial esector   = 13'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 16 dgisselq
                        esector[11:0] <= { o_data[21:14], 4'h0 };
617 9 dgisselq
                        wp <= (o_data[30])&&(new_req)||(wp)&&(~new_req);
618 16 dgisselq
                        esector[12] <= 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 16 dgisselq
        assign o_wb_data[31:0] = { i_wip, ~wp, i_quad, esector[12],
683 9 dgisselq
                        i_idloaded, wp_err, i_xip, i_spi_stopped,
684 16 dgisselq
                        2'b00, esector[11:0], 10'h00 };
685
        assign  o_sector = { 2'b00, esector[11:0], 8'h00 }; // 22 bits
686 9 dgisselq
        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 16 dgisselq
`define RD_IDLE_QUAD_PORT       4'ha
702 9 dgisselq
 
703
module  readqspi(i_clk, i_readreq, i_piperd, i_other_req, i_addr, o_bus_ack,
704
                o_qspi_req, i_grant,
705
                        o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
706
                                o_spi_spd, o_spi_dir, o_spi_recycle,
707
                        i_spi_data, i_spi_valid, i_spi_busy, i_spi_stopped,
708 14 dgisselq
                        o_data_ack, o_data, i_quad, i_xip, o_leave_xip);
709 9 dgisselq
        input                   i_clk;
710
        input                   i_readreq, i_piperd, i_other_req;
711
        input           [21:0]   i_addr;
712
        output  reg             o_bus_ack, o_qspi_req;
713
        input   wire            i_grant;
714
        output  reg             o_spi_wr;
715
        output  wire            o_spi_hold;
716
        output  reg     [31:0]   o_spi_word;
717
        output  reg     [1:0]    o_spi_len;
718
        output  reg             o_spi_spd, o_spi_dir, o_spi_recycle;
719
        input           [31:0]   i_spi_data;
720
        input                   i_spi_valid, i_spi_busy, i_spi_stopped;
721
        output  reg             o_data_ack;
722
        output  reg     [31:0]   o_data;
723
        input                   i_quad, i_xip;
724 14 dgisselq
        output  wire            o_leave_xip;
725 9 dgisselq
 
726
        reg     accepted;
727
        initial accepted = 1'b0;
728
        always @(posedge i_clk)
729
                accepted <= (~i_spi_busy)&&(i_grant)&&(o_spi_wr)&&(~accepted);
730
 
731
        reg     [3:0]    rd_state;
732
        reg             r_leave_xip, r_xip, r_quad, r_requested;
733 11 dgisselq
        reg     [3:0]    invalid_ack_pipe;
734 9 dgisselq
        initial rd_state = `RD_IDLE;
735
        initial o_data_ack = 1'b0;
736
        initial o_bus_ack  = 1'b0;
737
        initial o_qspi_req = 1'b0;
738
        always @(posedge i_clk)
739
        begin
740
                o_data_ack <= 1'b0;
741
                o_bus_ack <= 1'b0;
742
                o_spi_recycle <= 1'b0;
743
                if (i_spi_valid)
744
                        o_data <= i_spi_data;
745 11 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
746 9 dgisselq
                case(rd_state)
747
                `RD_IDLE: begin
748
                        r_requested <= 1'b0;
749
                        o_qspi_req <= 1'b0;
750 16 dgisselq
                        // 0x0b is a fast read, uses all SPI protocol
751
                        // 0x6b is a Quad output fast read, uses SPI cmd,
752
                        //                      SPI address, QSPI data
753
                        // 0xeb is a Quad I/O fast read, using SPI cmd,
754
                        //                      QSPI address and data
755
                        o_spi_word <= { ((i_quad)? 8'hEB: 8'h0b), i_addr, 2'b00 };
756 9 dgisselq
                        o_spi_wr <= 1'b0;
757
                        o_spi_dir <= 1'b0;
758
                        o_spi_spd <= 1'b0;
759 16 dgisselq
                        o_spi_len <= (i_quad)? 2'b00 : 2'b11;
760 9 dgisselq
                        r_xip <= (i_xip)&&(i_quad);
761
                        r_leave_xip <= 1'b0; // Not in it, so can't leave it
762
                        r_quad <= i_quad;
763
                        if (i_readreq)
764
                        begin
765
                                rd_state <= `RD_IDLE_GET_PORT;
766
                                o_bus_ack <= 1'b1;
767
                        end end
768
                `RD_IDLE_GET_PORT: begin
769
                        o_spi_wr <= 1'b1; // Write the address
770
                        o_qspi_req <= 1'b1;
771
                        if (accepted)
772 16 dgisselq
                        begin
773
                                rd_state <= (r_quad) ? `RD_IDLE_QUAD_PORT : `RD_SLOW_DUMMY;
774
                                o_spi_word[31:8] <= o_spi_word[23:0];
775
                        end end
776
                `RD_IDLE_QUAD_PORT: begin
777
                        o_spi_wr <= 1'b1; // Write the command
778
                        o_qspi_req <= 1'b1;
779
                        o_spi_spd <= 1'b1;
780
                        o_spi_dir <= 1'b0;
781
                        o_spi_len <= 2'b10;
782
 
783
                        // We haven't saved our address any where but in the
784
                        // SPI word we just sent.  Hence, let's just
785
                        // grab it from there.
786
                        if (accepted)
787 9 dgisselq
                                rd_state <= `RD_SLOW_DUMMY;
788
                        end
789
                `RD_SLOW_DUMMY: begin
790 16 dgisselq
                        o_spi_wr <= 1'b1; // Write 8 dummy clocks--this is the same for
791
                        o_qspi_req <= 1'b1; // both Quad I/O, Quad O, and fast-read commands
792 9 dgisselq
                        o_spi_dir <= 1'b0;
793 16 dgisselq
                        o_spi_spd <= r_quad;
794
                        o_spi_word[31:0] <= (r_xip) ? 32'h00 : 32'hffffffff;
795
                        o_spi_len  <= (r_quad)? 2'b11:2'b00; // 8 clocks
796 9 dgisselq
                        if (accepted)
797
                                rd_state <= (r_quad)?`RD_QUAD_READ_DATA
798
                                                : `RD_SLOW_READ_DATA;
799
                        end
800
                `RD_SLOW_READ_DATA: begin
801
                        o_qspi_req <= 1'b1;
802
                        o_spi_dir <= 1'b1;
803
                        o_spi_spd <= 1'b0;
804
                        o_spi_len <= 2'b11;
805
                        o_spi_wr <= (~r_requested)||(i_piperd);
806 11 dgisselq
                        invalid_ack_pipe[0] <= (!r_requested);
807
                        o_data_ack <=  (!invalid_ack_pipe[3])&&(i_spi_valid)&&(r_requested);
808 9 dgisselq
                        o_bus_ack <=   (r_requested)&&(accepted)&&(i_piperd);
809
                        r_requested <= (r_requested)||(accepted);
810
                        if ((i_spi_valid)&&(~o_spi_wr))
811
                                rd_state <= `RD_GO_TO_IDLE;
812
                        end
813
                `RD_QUAD_READ_DATA: begin
814 14 dgisselq
                        o_qspi_req <= 1'b1; // Insist that we keep the port
815
                        o_spi_dir <= 1'b1;  // Read
816
                        o_spi_spd <= 1'b1;  // Read at Quad rates
817
                        o_spi_len <= 2'b11; // Read 4-bytes
818 9 dgisselq
                        o_spi_recycle <= (r_leave_xip)? 1'b1: 1'b0;
819 11 dgisselq
                        invalid_ack_pipe[0] <= (!r_requested);
820 14 dgisselq
                        r_requested <= (r_requested)||(accepted); // Make sure at least one request goes through
821 11 dgisselq
                        o_data_ack <=  (!invalid_ack_pipe[3])&&(i_spi_valid)&&(r_requested)&&(~r_leave_xip);
822 9 dgisselq
                        o_bus_ack  <= (r_requested)&&(accepted)&&(i_piperd)&&(~r_leave_xip);
823
                        o_spi_wr <= (~r_requested)||(i_piperd);
824
                        // if (accepted)
825
                                // o_spi_wr <= (i_piperd);
826 11 dgisselq
                        if (accepted) // only happens if (o_spi_wr)
827 9 dgisselq
                                o_data <= i_spi_data;
828
                        if ((i_spi_valid)&&(~o_spi_wr))
829
                                rd_state <= ((r_leave_xip)||(~r_xip))?`RD_GO_TO_IDLE:`RD_GO_TO_XIP;
830
                        end
831
                `RD_QUAD_ADDRESS: begin
832
                        o_qspi_req <= 1'b1;
833
                        o_spi_wr <= 1'b1;
834
                        o_spi_dir <= 1'b0; // Write the address
835
                        o_spi_spd <= 1'b1; // High speed
836
                        o_spi_word[31:0] <= { i_addr, 2'b00, 8'h00 };
837
                        o_spi_len  <= 2'b10; // 24 bits, High speed, 6 clocks
838
                        if (accepted)
839
                                rd_state <= `RD_QUAD_DUMMY;
840
                        end
841
                `RD_QUAD_DUMMY: begin
842
                        o_qspi_req <= 1'b1;
843
                        o_spi_wr <= 1'b1;
844
                        o_spi_dir <= 1'b0; // Write the dummy
845
                        o_spi_spd <= 1'b1; // High speed
846
                        o_spi_word[31:0] <= (r_xip)? 32'h00 : 32'hffffffff;
847
                        o_spi_len  <= 2'b11; // 8 clocks = 32-bits, quad speed
848
                        if (accepted)
849
                                rd_state <= (r_quad)?`RD_QUAD_READ_DATA
850
                                                : `RD_SLOW_READ_DATA;
851
                        end
852
                `RD_XIP: begin
853
                        r_requested <= 1'b0;
854
                        o_qspi_req <= 1'b1;
855
                        o_spi_word <= { i_addr, 2'b00, 8'h00 };
856
                        o_spi_wr <= 1'b0;
857
                        o_spi_dir <= 1'b0; // Write to SPI
858
                        o_spi_spd <= 1'b1; // High speed
859
                        o_spi_len <= 2'b11;
860
                        r_leave_xip <= i_other_req;
861
                        r_xip <= (~i_other_req);
862
                        o_bus_ack <= 1'b0;
863
                        if ((i_readreq)||(i_other_req))
864
                        begin
865
                                rd_state <= `RD_QUAD_ADDRESS;
866
                                o_bus_ack <= i_readreq;
867
                        end end
868
                `RD_GO_TO_IDLE: begin
869 11 dgisselq
                        if ((!invalid_ack_pipe[3])&&(i_spi_valid)&&(~r_leave_xip))
870
                                o_data_ack <=  1'b1;
871
                        o_spi_wr   <= 1'b0;
872 9 dgisselq
                        o_qspi_req <= 1'b0;
873
                        if ((i_spi_stopped)&&(~i_grant))
874
                                rd_state <= `RD_IDLE;
875
                        end
876
                `RD_GO_TO_XIP: begin
877 11 dgisselq
                        r_requested <= 1'b0;
878
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3]))
879
                                o_data_ack <=  1'b1;
880 9 dgisselq
                        o_qspi_req <= 1'b1;
881
                        o_spi_wr   <= 1'b0;
882
                        if (i_spi_stopped)
883
                                rd_state <= `RD_XIP;
884
                        end
885
                default: begin
886
                        // rd_state <= (i_grant)?`RD_BREAK;
887
                        o_qspi_req <= 1'b0;
888
                        o_spi_wr <= 1'b0;
889
                        if ((i_spi_stopped)&&(~i_grant))
890
                                rd_state <= `RD_IDLE;
891
                        end
892
                endcase
893
        end
894
 
895
        assign  o_spi_hold = 1'b0;
896 14 dgisselq
        assign  o_leave_xip = r_leave_xip;
897 9 dgisselq
 
898
endmodule
899
 
900
module  writeqspi(i_clk, i_wreq, i_ereq, i_pipewr, i_endpipe, i_addr, i_data,
901
                        o_bus_ack, o_qspi_req, i_qspi_grant,
902
                                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
903
                                o_spi_spd, o_spi_dir, i_spi_data, i_spi_valid,
904
                                        i_spi_busy, i_spi_stopped,
905
                                o_data_ack, i_quad, o_wip);
906
        input           i_clk;
907
        //
908
        input           i_wreq, i_ereq, i_pipewr, i_endpipe;
909
        input           [21:0]   i_addr;
910
        input           [31:0]   i_data;
911
        output  reg             o_bus_ack, o_qspi_req;
912
        input                   i_qspi_grant;
913
        output  reg             o_spi_wr, o_spi_hold;
914
        output  reg     [31:0]   o_spi_word;
915
        output  reg     [1:0]    o_spi_len;
916
        output  reg             o_spi_spd, o_spi_dir;
917
        input           [31:0]   i_spi_data;
918
        input                   i_spi_valid;
919
        input                   i_spi_busy, i_spi_stopped;
920
        output  reg             o_data_ack;
921
        input                   i_quad;
922
        output  reg             o_wip;
923
 
924
`ifdef  QSPI_READ_ONLY
925
        always @(posedge i_clk)
926
                o_data_ack <= (i_wreq)||(i_ereq);
927
        always @(posedge i_clk)
928
                o_bus_ack <= (i_wreq)||(i_ereq);
929
 
930
        always @(posedge i_clk)
931
        begin
932
                o_qspi_req <= 1'b0;
933
                o_spi_wr   <= 1'b0;
934
                o_spi_hold <= 1'b0;
935
                o_spi_dir  <= 1'b1; // Read
936
                o_spi_spd  <= i_quad;
937
                o_spi_len  <= 2'b00;
938
                o_spi_word <= 32'h00;
939
                o_wip <= 1'b0;
940
        end
941
`else
942
 
943
`define WR_IDLE                         4'h0
944
`define WR_START_WRITE                  4'h1
945
`define WR_START_QWRITE                 4'h2
946
`define WR_PROGRAM                      4'h3
947
`define WR_PROGRAM_GETNEXT              4'h4
948
`define WR_START_ERASE                  4'h5
949
`define WR_WAIT_ON_STOP                 4'h6
950
`define WR_REQUEST_STATUS               4'h7
951
`define WR_REQUEST_STATUS_NEXT          4'h8
952
`define WR_READ_STATUS                  4'h9
953
`define WR_WAIT_ON_FINAL_STOP           4'ha
954
 
955
        reg     accepted;
956
        initial accepted = 1'b0;
957
        always @(posedge i_clk)
958
                accepted <= (~i_spi_busy)&&(i_qspi_grant)&&(o_spi_wr)&&(~accepted);
959
 
960
 
961 11 dgisselq
        reg             cyc, chk_wip, valid_status;
962 9 dgisselq
        reg     [3:0]    wr_state;
963
        initial wr_state = `WR_IDLE;
964
        initial cyc = 1'b0;
965
        always @(posedge i_clk)
966
        begin
967
                chk_wip <= 1'b0;
968
                o_bus_ack  <= 1'b0;
969
                o_data_ack <= 1'b0;
970
                case(wr_state)
971
                `WR_IDLE: begin
972 11 dgisselq
                        valid_status <= 1'b0;
973 9 dgisselq
                        o_qspi_req <= 1'b0;
974
                        cyc <= 1'b0;
975
                        if (i_ereq)
976
                                wr_state <= `WR_START_ERASE;
977
                        else if (i_wreq)
978
                                wr_state <= (i_quad)?`WR_START_QWRITE
979
                                        : `WR_START_WRITE;
980
                        end
981
                `WR_START_WRITE: begin
982
                        o_wip      <= 1'b1;
983
                        o_qspi_req <= 1'b1;
984
                        o_spi_wr   <= 1'b1;
985
                        o_spi_dir  <= 1'b0;
986
                        o_spi_len  <= 2'b11;
987
                        o_spi_spd  <= 1'b0;
988
                        o_spi_hold <= 1'b1;
989
                        o_spi_word <= { 8'h02, i_addr, 2'b00 };
990
                        cyc <= 1'b1;
991
                        if (accepted)
992
                        begin
993
                                o_bus_ack  <= 1'b1;
994
                                o_data_ack <= 1'b1;
995
                                wr_state <= `WR_PROGRAM;
996
                                o_spi_word <= i_data;
997
                        end end
998
                `WR_START_QWRITE: begin
999
                        o_wip      <= 1'b1;
1000
                        o_qspi_req <= 1'b1;
1001
                        o_spi_wr   <= 1'b1;
1002
                        o_spi_dir  <= 1'b0;
1003
                        o_spi_len  <= 2'b11;
1004
                        o_spi_spd  <= 1'b0;
1005
                        o_spi_hold <= 1'b1;
1006
                        o_spi_word <= { 8'h32, i_addr, 2'b00 };
1007
                        cyc <= 1'b1;
1008
                        if (accepted)
1009
                        begin
1010
                                o_bus_ack  <= 1'b1;
1011
                                o_data_ack <= 1'b1;
1012
                                wr_state <= `WR_PROGRAM;
1013
                                o_spi_word <= i_data;
1014
                        end end
1015
                `WR_PROGRAM: begin
1016
                        o_wip     <= 1'b1;
1017
                        o_qspi_req <= 1'b1;
1018
                        o_spi_wr   <= 1'b1;
1019
                        o_spi_dir  <= 1'b0;
1020
                        o_spi_len  <= 2'b11;
1021
                        o_spi_spd  <= i_quad;
1022
                        o_spi_hold <= 1'b1;
1023
                        // o_spi_word <= i_data;
1024
                        if (accepted)
1025
                                wr_state <= `WR_PROGRAM_GETNEXT;
1026
                        end
1027
                `WR_PROGRAM_GETNEXT: begin
1028
                        o_wip      <= 1'b1;
1029
                        o_qspi_req <= 1'b1;
1030
                        o_spi_wr   <= 1'b0;
1031
                        o_spi_dir  <= 1'b0;
1032
                        o_spi_len  <= 2'b11;
1033
                        o_spi_spd  <= i_quad;
1034
                        o_spi_hold <= 1'b1;
1035
                        o_spi_word <= i_data;
1036 11 dgisselq
                        cyc <= (cyc)&&(~i_endpipe);
1037 9 dgisselq
                        if (~cyc)
1038
                                wr_state <= `WR_WAIT_ON_STOP;
1039
                        else if (i_pipewr)
1040
                        begin
1041
                                o_bus_ack  <= 1'b1;
1042
                                o_data_ack <= 1'b1;
1043
                                wr_state <= `WR_PROGRAM;
1044
                        end end
1045
                `WR_START_ERASE: begin
1046
                        o_wip <= 1'b1;
1047
                        o_qspi_req <= 1'b1;
1048
                        o_spi_wr  <= 1'b1;
1049
                        o_spi_dir <= 1'b0;
1050
                        o_spi_spd <= 1'b0;
1051
                        o_spi_len <= 2'b11;
1052
                        if (i_data[28])
1053
                                // Subsector erase
1054
                                o_spi_word[31:24] <= 8'h20;
1055
                        else
1056
                                // Sector erase
1057
                                o_spi_word[31:24] <= 8'hd8;
1058
                        o_spi_word[23:0] <= { i_data[21:10], 12'h0 };
1059
                        // Data has already been ack'd, so no need to ack
1060
                        // it again.  However, we can now free the QSPI
1061
                        // bus processor to accept another command from the
1062
                        // bus.
1063
                        o_bus_ack <= accepted;
1064
                        if (accepted)
1065
                                wr_state <= `WR_WAIT_ON_STOP;
1066
                        end
1067
                `WR_WAIT_ON_STOP: begin
1068
                        o_wip <= 1'b1;
1069
                        o_qspi_req <= 1'b0;
1070
                        o_spi_wr   <= 1'b0;
1071
                        o_spi_hold <= 1'b0;
1072
                        if (i_spi_stopped)
1073
                                wr_state <= `WR_REQUEST_STATUS;
1074
                        end
1075
                `WR_REQUEST_STATUS: begin
1076
                        o_wip <= 1'b1;
1077
                        o_qspi_req <= 1'b1;
1078
                        o_spi_hold <= 1'b0;
1079
                        o_spi_wr   <= 1'b1;
1080
                        o_spi_spd  <= 1'b0; // Slow speed
1081
                        o_spi_len  <= 2'b00; // 8 bytes
1082
                        o_spi_dir  <= 1'b0; // Write
1083
                        o_spi_word <= { 8'h05, 24'h00 };
1084
                        if (accepted)
1085
                                wr_state <= `WR_REQUEST_STATUS_NEXT;
1086
                        end
1087
                `WR_REQUEST_STATUS_NEXT: begin
1088
                        o_wip <= 1'b1;
1089
                        o_qspi_req <= 1'b1;
1090
                        o_spi_hold <= 1'b0;
1091
                        o_spi_wr   <= 1'b1;
1092
                        o_spi_spd  <= 1'b0; // Slow speed
1093
                        o_spi_len  <= 2'b00; // 8 bytes
1094
                        o_spi_dir  <= 1'b1; // Read
1095
                        o_spi_word <= 32'h00;
1096
                        if (accepted)
1097
                                wr_state <= `WR_READ_STATUS;
1098 11 dgisselq
                        valid_status <= 1'b0;
1099 9 dgisselq
                        end
1100
                `WR_READ_STATUS: begin
1101
                        o_wip <= 1'b1;
1102
                        o_qspi_req <= 1'b1;
1103
                        o_spi_hold <= 1'b0;
1104
                        o_spi_wr   <= 1'b1;
1105
                        o_spi_spd  <= 1'b0; // Slow speed
1106
                        o_spi_len  <= 2'b00; // 8 bytes
1107
                        o_spi_dir  <= 1'b1; // Read
1108
                        o_spi_word <= 32'h00;
1109
                        if (i_spi_valid)
1110 11 dgisselq
                                valid_status <= 1'b1;
1111
                        if ((i_spi_valid)&&(valid_status))
1112 9 dgisselq
                                chk_wip <= 1'b1;
1113
                        if ((chk_wip)&&(~i_spi_data[0]))
1114
                                wr_state <= `WR_WAIT_ON_FINAL_STOP;
1115
                        end
1116
                // `WR_WAIT_ON_FINAL_STOP: // Same as the default
1117
                default: begin
1118
                        o_qspi_req <= 1'b0;
1119
                        o_spi_wr <= 1'b0;
1120
                        o_wip <= 1'b0;
1121
                        if (i_spi_stopped)
1122
                                wr_state <= `WR_IDLE;
1123
                        end
1124
                endcase
1125
        end
1126
`endif
1127
 
1128
endmodule
1129
 
1130
 
1131
`define CT_SAFE
1132
`define CT_IDLE                 3'h0
1133
`define CT_NEXT                 3'h1
1134
`define CT_GRANTED              3'h2
1135
`define CT_DATA                 3'h3
1136
`define CT_READ_DATA            3'h4
1137
`define CT_WAIT_FOR_IDLE        3'h5
1138
 
1139
// CTRL commands:
1140
//      WEL (write-enable latch)
1141
//      Read Status
1142
module  ctrlspi(i_clk, i_req, i_wr, i_addr, i_data, i_sector_address,
1143
                                o_spi_req, i_grant,
1144
                                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
1145
                                        o_spi_spd, o_spi_dir,
1146
                                i_spi_data, i_spi_valid, i_spi_busy,
1147
                                        i_spi_stopped,
1148 14 dgisselq
                                o_bus_ack, o_data_ack, o_data,
1149
                                i_leave_xip, o_xip, o_quad);
1150 9 dgisselq
        input           i_clk;
1151
        // From the WB bus controller
1152
        input                   i_req;
1153
        input                   i_wr;
1154 14 dgisselq
        input           [3:0]    i_addr;
1155 9 dgisselq
        input           [31:0]   i_data;
1156
        input           [21:0]   i_sector_address;
1157
        // To/from the arbiter
1158
        output  reg             o_spi_req;
1159
        input                   i_grant;
1160
        // To/from the low-level SPI driver
1161
        output  reg             o_spi_wr;
1162
        output  wire            o_spi_hold;
1163
        output  reg     [31:0]   o_spi_word;
1164
        output  reg     [1:0]    o_spi_len;
1165
        output  wire            o_spi_spd;
1166
        output  reg             o_spi_dir;
1167
        input           [31:0]   i_spi_data;
1168
        input                   i_spi_valid;
1169
        input                   i_spi_busy, i_spi_stopped;
1170
        // Return data to the bus controller, and the wishbone bus
1171
        output  reg             o_bus_ack, o_data_ack;
1172
        output  reg     [31:0]   o_data;
1173
        // Configuration items that we may have configured.
1174 14 dgisselq
        input   wire            i_leave_xip;
1175 9 dgisselq
        output  reg             o_xip;
1176
        output  wire            o_quad;
1177
 
1178
        // Command registers
1179
        reg     [1:0]    ctcmd_len;
1180
        reg     [31:0]   ctcmd_word;
1181
        // Data stage registers
1182
        reg             ctdat_skip, // Skip the data phase?
1183
                        ctdat_wr;       // Write during data? (or not read)
1184
        wire    [1:0]    ctdat_len;
1185
        reg     [31:0]   ctdat_word;
1186
 
1187
        reg     [2:0]    ctstate;
1188
        reg             accepted;
1189 11 dgisselq
        reg     [3:0]    invalid_ack_pipe;
1190 9 dgisselq
 
1191
 
1192
        initial accepted = 1'b0;
1193
        always @(posedge i_clk)
1194
                accepted <= (~i_spi_busy)&&(i_grant)&&(o_spi_wr)&&(~accepted);
1195
 
1196 11 dgisselq
        reg     r_ctdat_len, ctbus_ack, first_valid;
1197 9 dgisselq
        assign  ctdat_len = { 1'b0, r_ctdat_len };
1198
 
1199
        // First step, calculate the values for our state machine
1200
        initial o_xip = 1'b0;
1201
        // initial o_quad = 1'b0;
1202
        always @(posedge i_clk)
1203
        if (i_req) // A request for us to act from the bus controller
1204
        begin
1205
                ctdat_skip <= 1'b0;
1206
                ctbus_ack  <= 1'b1;
1207
                ctcmd_word[23:0] <= { i_sector_address, 2'b00 };
1208
                ctdat_word <= { i_data[7:0], 24'h00 };
1209
                ctcmd_len <= 2'b00; // 8bit command (for all but Lock regs)
1210
                r_ctdat_len <= 1'b0; // 8bit data (read or write)
1211
                ctdat_wr <= i_wr;
1212 14 dgisselq
                casez({ i_addr[3:0], i_wr, i_data[30] })
1213
                6'b000010: begin // Write Disable
1214 9 dgisselq
                        ctcmd_word[31:24] <= 8'h04;
1215
                        ctdat_skip <= 1'b1;
1216
                        ctbus_ack  <= 1'b0;
1217
                        end
1218 14 dgisselq
                6'b000011: begin // Write enable
1219 9 dgisselq
                        ctcmd_word[31:24] <= 8'h06;
1220
                        ctdat_skip <= 1'b1;
1221
                        ctbus_ack  <= 1'b0;
1222
                        end
1223
                // 4'b0010?: begin // Read Status register
1224
                //      Moved to defaults section
1225 14 dgisselq
                6'b00011?: begin // Write Status register (Requires WEL)
1226 9 dgisselq
                        ctcmd_word[31:24] <= 8'h01;
1227
`ifdef  CT_SAFE
1228
                        ctdat_word <= { 6'h00, i_data[1:0], 24'h00 };
1229
`else
1230
                        ctdat_word <= { i_data[7:0], 24'h00 };
1231
`endif
1232
                        end
1233 14 dgisselq
                6'b00100?: begin // Read NV-Config register (two bytes)
1234 9 dgisselq
                        ctcmd_word[31:24] <= 8'hB5;
1235
                        r_ctdat_len <= 1'b1; // 16-bit data
1236
                        end
1237 14 dgisselq
                6'b00101?: begin // Write NV-Config reg (2 bytes, Requires WEL)
1238 9 dgisselq
                        ctcmd_word[31:24] <= 8'hB1;
1239
                        r_ctdat_len <= 1'b1; // 16-bit data
1240
`ifdef  CT_SAFE
1241
                        ctdat_word <= { 4'h8, 3'h7, 3'h7, i_data[5:1], 1'b1, 16'h00 };
1242
`else
1243
                        ctdat_word <= { i_data[15:0], 16'h00 };
1244
`endif
1245
                        end
1246 14 dgisselq
                6'b00110?: begin // Read V-Config register
1247 9 dgisselq
                        ctcmd_word[31:24] <= 8'h85;
1248
                        end
1249 14 dgisselq
                6'b00111?: begin // Write V-Config register (Requires WEL)
1250 9 dgisselq
                        ctcmd_word[31:24] <= 8'h81;
1251
                        r_ctdat_len <= 1'b0; // 8-bit data
1252
`ifdef  CT_SAFE
1253
                        ctdat_word <= { 4'h8, i_data[3:2], 2'b11, 24'h00 };
1254
`else
1255
                        ctdat_word <= { i_data[7:0], 24'h00 };
1256
`endif
1257 14 dgisselq
                        o_xip <= ~i_data[3];
1258 9 dgisselq
                        end
1259 14 dgisselq
                6'b01000?: begin // Read EV-Config register
1260 9 dgisselq
                        ctcmd_word[31:24] <= 8'h65;
1261
                        end
1262 14 dgisselq
                6'b01001?: begin // Write EV-Config register (Requires WEL)
1263 9 dgisselq
                        ctcmd_word[31:24] <= 8'h61;
1264
                        // o_quad <= (~i_data[7]);
1265
`ifdef  CT_SAFE
1266
                        ctdat_word <= { 1'b1, 3'h5, 4'hf, 24'h00 };
1267
`else
1268
                        ctdat_word <= { i_data[7:0], 24'h00 };
1269
`endif
1270
                        end
1271 14 dgisselq
                6'b01010?: begin // Read Lock register
1272 9 dgisselq
                        ctcmd_word[31:0] <= { 8'he8,  i_sector_address, 2'b00 };
1273
                        ctcmd_len <= 2'b11;
1274
                        ctdat_wr  <= 1'b0;  // Read, not write
1275
                        end
1276 14 dgisselq
                6'b01011?: begin // Write Lock register (Requires WEL)
1277 9 dgisselq
                        ctcmd_word[31:0] <= { 8'he5, i_sector_address, 2'b00 };
1278
                        ctcmd_len <= 2'b11;
1279
                        ctdat_wr  <= 1'b1;  // Write
1280
                        end
1281 14 dgisselq
                6'b01100?: begin // Read Flag Status register
1282 9 dgisselq
                        ctcmd_word[31:24] <= 8'h70;
1283
                        ctdat_wr  <= 1'b0;  // Read, not write
1284
                        end
1285 14 dgisselq
                6'b01101?: begin // Write/Clear Flag Status register (No WEL required)
1286 9 dgisselq
                        ctcmd_word[31:24] <= 8'h50;
1287
                        ctdat_skip <= 1'b1;
1288
                        end
1289 14 dgisselq
                6'b11011?: begin // RESET_ENABLE (when written to)
1290
                        ctcmd_word[31:24] <= 8'h66;
1291
                        ctdat_skip <= 1'b1;
1292
                        end
1293
                6'b11101?: begin // RESET_MEMORY (when written to)
1294
                        ctcmd_word[31:24] <= 8'h99;
1295
                        ctdat_skip <= 1'b1;
1296
                        end
1297 9 dgisselq
                default: begin // Default to reading the status register
1298
                        ctcmd_word[31:24] <= 8'h05;
1299
                        ctdat_wr  <= 1'b0;  // Read, not write
1300
                        r_ctdat_len <= 1'b0; // 8-bit data
1301
                        end
1302
                endcase
1303 14 dgisselq
        end else if (i_leave_xip)
1304
                o_xip <= 1'b0;
1305 9 dgisselq
 
1306 10 dgisselq
        assign  o_quad = 1'b1;
1307 9 dgisselq
 
1308
        reg     nxt_data_ack;
1309
 
1310
        // Second step, actually drive the state machine
1311
        initial ctstate = `CT_IDLE;
1312
        always @(posedge i_clk)
1313
        begin
1314
                o_spi_wr <= 1'b1;
1315
                o_bus_ack <= 1'b0;
1316
                o_data_ack <= 1'b0;
1317 11 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
1318 9 dgisselq
                if (i_spi_valid)
1319
                        o_data <= i_spi_data;
1320
                case(ctstate)
1321
                `CT_IDLE: begin
1322
                        o_spi_req <= 1'b0;
1323
                        o_spi_wr  <= 1'b0;
1324
                        if (i_req) // Need a clock to let the digestion
1325
                                ctstate <= `CT_NEXT; // process complete
1326
                        end
1327
                `CT_NEXT: begin
1328
                        o_spi_wr <= 1'b1;
1329
                        o_spi_req <= 1'b1;
1330
                        o_spi_word <= ctcmd_word;
1331
                        o_spi_len <= ctcmd_len;
1332
                        o_spi_dir <= 1'b0; // Write
1333
                        if (accepted)
1334
                        begin
1335
                                ctstate <= (ctdat_skip)?`CT_WAIT_FOR_IDLE:`CT_DATA;
1336
                                o_bus_ack <= (ctdat_skip);
1337
                                o_data_ack <= (ctdat_skip)&&(ctbus_ack);
1338
                        end end
1339
                `CT_GRANTED: begin
1340
                        o_spi_wr <= 1'b1;
1341
                        if ((accepted)&&(ctdat_skip))
1342
                                ctstate <= `CT_WAIT_FOR_IDLE;
1343
                        else if (accepted)//&&(~ctdat_skip)
1344
                                ctstate <= `CT_DATA;
1345
                        end
1346
                `CT_DATA: begin
1347
                        o_spi_wr   <= 1'b1;
1348
                        o_spi_len  <= ctdat_len;
1349
                        o_spi_dir  <= ~ctdat_wr;
1350
                        o_spi_word <= ctdat_word;
1351
                        if (accepted)
1352
                                o_bus_ack <= 1'b1;
1353
                        if (accepted)
1354
                                ctstate <= (ctdat_wr)?`CT_WAIT_FOR_IDLE:`CT_READ_DATA;
1355
                        if ((accepted)&&(ctdat_wr))
1356
                                o_data_ack <= 1'b1;
1357 11 dgisselq
                        first_valid <= 1'b0;
1358 9 dgisselq
                        end
1359
                `CT_READ_DATA: begin
1360
                        o_spi_wr <= 1'b0; // No more words to go, just to wait
1361
                        o_spi_req <= 1'b1;
1362 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1363
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3])) // for a value to read
1364 9 dgisselq
                        begin
1365
                                o_data_ack <= 1'b1;
1366
                                o_data <= i_spi_data;
1367
                                ctstate <= `CT_WAIT_FOR_IDLE;
1368
                        end end
1369
                default: begin // `CT_WAIT_FOR_IDLE
1370
                        o_spi_wr <= 1'b0;
1371
                        o_spi_req <= 1'b0;
1372
                        if (i_spi_stopped)
1373
                                ctstate <= `CT_IDLE;
1374
                        end
1375
                endcase
1376
        end
1377
 
1378
        // All of this is done in straight SPI mode, so our speed will always be zero
1379
        assign  o_spi_hold = 1'b0;
1380
        assign  o_spi_spd  = 1'b0;
1381
 
1382
endmodule
1383
 
1384
`define ID_IDLE                         5'h00
1385
`define ID_WAIT_ON_START_ID             5'h01
1386
`define ID_WAIT_ON_START_OTP            5'h02
1387
`define ID_WAIT_ON_START_OTP_WRITE      5'h03
1388
`define ID_READ_DATA_COMMAND            5'h04
1389
`define ID_GET_DATA                     5'h05
1390
`define ID_LOADED                       5'h06
1391
`define ID_LOADED_NEXT                  5'h07
1392
`define ID_OTP_SEND_DUMMY               5'h08
1393
`define ID_OTP_CLEAR                    5'h09
1394
`define ID_OTP_GET_DATA                 5'h0a
1395
`define ID_OTP_WRITE                    5'h0b
1396
`define ID_WAIT_ON_STOP                 5'h0c
1397
`define ID_REQ_STATUS                   5'h0d
1398
`define ID_REQ_STATUS_NEXT              5'h0e
1399
`define ID_READ_STATUS                  5'h0f
1400
//
1401
`define ID_FINAL_STOP                   5'h10
1402
 
1403
module  idotpqspi(i_clk, i_req, i_wr, i_pipewr, i_addr, i_data, o_bus_ack,
1404
                o_qspi_req, i_qspi_grant,
1405
                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
1406
                o_spi_spd, o_spi_dir, i_spi_data, i_spi_valid,
1407
                i_spi_busy, i_spi_stopped, o_data_ack, o_data, o_loaded,
1408
                o_wip);
1409
        input                   i_clk;
1410
        input                   i_req, i_wr, i_pipewr;
1411
        input           [4:0]    i_addr;
1412
        input           [31:0]   i_data;
1413
        output  reg             o_bus_ack, o_qspi_req;
1414
        input                   i_qspi_grant;
1415
        output  reg             o_spi_wr, o_spi_hold;
1416
        output  reg     [31:0]   o_spi_word;
1417
        output  reg     [1:0]    o_spi_len;
1418
        output  wire            o_spi_spd;
1419
        output  reg             o_spi_dir;
1420
        input           [31:0]   i_spi_data;
1421
        input                   i_spi_valid, i_spi_busy, i_spi_stopped;
1422
        output  reg             o_data_ack;
1423
        output  reg     [31:0]   o_data;
1424
        output  wire            o_loaded;
1425
        output  reg             o_wip;
1426
 
1427
        reg     id_loaded;
1428
        initial id_loaded = 1'b0;
1429
        assign  o_loaded= id_loaded;
1430
 
1431
/*
1432
        // Only the ID register will be kept in memory, OTP will be read
1433
        // or written upon request
1434
        always @(posedge i_clk)
1435
                if (i_addr[4])
1436
                        o_data <= otpmem[i_addr[3:0]];
1437
                else
1438
                        o_data <= idmem[i_addr[2:0]];
1439
 
1440
        always @(posedge i_clk)
1441
                if ((otp_loaded)&&(i_req)&&(i_addr[4]))
1442
                        o_data_ack <= 1'b1;
1443
                else if ((id_loaded)&&(i_req)&&(~i_addr[4]))
1444
                        o_data_ack <= idmem[i_addr[2:0]];
1445
                else
1446
                        o_data_ack <= 1'b0;
1447
*/
1448
 
1449
        reg     otp_read_request, id_read_request, accepted, otp_wr_request,
1450
                id_read_device, last_id_read;
1451
        reg     [4:0]    req_addr;
1452
        reg     [2:0]    lcl_id_addr;
1453
        reg     [4:0]    id_state;
1454
        always @(posedge i_clk)
1455
        begin
1456
                otp_read_request <= (i_req)&&(~i_wr)&&((i_addr[4])||(i_addr[3:0]==4'hf));
1457
                last_id_read     <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf);
1458
                id_read_request  <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf)&&(~last_id_read);
1459
                id_read_device   <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf)&&(~id_loaded);
1460
                accepted <= (~i_spi_busy)&&(i_qspi_grant)&&(o_spi_wr)&&(~accepted);
1461
 
1462
                otp_wr_request <= (i_req)&&(i_wr)&&((i_addr[4])||(i_addr[3:0]==4'hf));
1463
 
1464
                if (id_state == `ID_IDLE)
1465
                        req_addr <= (i_addr[4:0]==5'h0f) ? 5'h10
1466
                                : { 1'b0, i_addr[3:0] };
1467
        end
1468
 
1469
        reg     last_addr;
1470
        always @(posedge i_clk)
1471
                last_addr <= (lcl_id_addr >= 3'h4);
1472
 
1473
        reg     [31:0]   idmem[0:5];
1474
        reg     [31:0]   r_data;
1475
 
1476
        // Now, quickly, let's deal with the fact that the data from the
1477
        // bus comes one clock later ...
1478
        reg     nxt_data_ack, nxt_data_spi;
1479
        reg     [31:0]   nxt_data;
1480
 
1481 11 dgisselq
        reg     set_val, chk_wip, first_valid;
1482 9 dgisselq
        reg     [2:0]    set_addr;
1483 11 dgisselq
        reg     [3:0]    invalid_ack_pipe;
1484 9 dgisselq
 
1485
        always @(posedge i_clk)
1486
        begin // Depends upon state[4], otp_rd, otp_wr, otp_pipe, id_req, accepted, last_addr
1487
                o_bus_ack <= 1'b0;
1488
                // o_data_ack <= 1'b0;
1489
                o_spi_hold <= 1'b0;
1490
                nxt_data_ack <= 1'b0;
1491
                nxt_data_spi <= 1'b0;
1492
                chk_wip      <= 1'b0;
1493
                set_val <= 1'b0;
1494 11 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
1495 9 dgisselq
                if ((id_loaded)&&(id_read_request))
1496
                begin
1497
                        nxt_data_ack <= 1'b1;
1498
                        o_bus_ack  <= 1'b1;
1499
                end
1500
                nxt_data <= idmem[i_addr[2:0]];
1501
                o_spi_wr <= 1'b0; // By default, we send nothing
1502
                case(id_state)
1503
                `ID_IDLE: begin
1504
                        o_qspi_req <= 1'b0;
1505
                        o_spi_dir <= 1'b0; // Write to SPI
1506
                        lcl_id_addr <= 3'h0;
1507
                        o_spi_word[23:7] <= 17'h00;
1508
                        o_spi_word[6:0] <= { req_addr[4:0], 2'b00 };
1509
                        r_data <= i_data;
1510
                        o_wip <= 1'b0;
1511 11 dgisselq
                        first_valid <= 1'b0;
1512 9 dgisselq
                        if (otp_read_request)
1513
                        begin
1514
                                // o_spi_word <= { 8'h48, 8'h00, 8'h00, 8'h00 };
1515
                                id_state <= `ID_WAIT_ON_START_OTP;
1516
                                o_bus_ack <= 1'b1;
1517
                        end else if (otp_wr_request)
1518
                        begin
1519
                                o_bus_ack <= 1'b1;
1520
                                // o_data_ack <= 1'b1;
1521
                                nxt_data_ack <= 1'b1;
1522
                                id_state <= `ID_WAIT_ON_START_OTP_WRITE;
1523
                        end else if (id_read_device)
1524
                        begin
1525
                                id_state <= `ID_WAIT_ON_START_ID;
1526
                                o_bus_ack <= 1'b0;
1527
                                o_spi_word[31:24] <= 8'h9f;
1528
                        end end
1529
                `ID_WAIT_ON_START_ID: begin
1530
                        o_spi_wr <= 1'b1;
1531
                        o_qspi_req <= 1'b1;
1532
                        o_spi_len <= 2'b0; // 8 bits
1533
                        if (accepted)
1534
                                id_state <= `ID_READ_DATA_COMMAND;
1535
                        end
1536
                `ID_WAIT_ON_START_OTP: begin
1537
                        o_spi_wr <= 1'b1;
1538
                        o_spi_word[31:24] <= 8'h4B;
1539
                        o_qspi_req <= 1'b1;
1540
                        o_spi_len <= 2'b11; // 32 bits
1541
                        o_spi_word[6:0] <= { req_addr[4:0], 2'b00 };
1542
                        if (accepted) // Read OTP command was just sent
1543
                                id_state <= `ID_OTP_SEND_DUMMY;
1544
                        end
1545
                `ID_WAIT_ON_START_OTP_WRITE: begin
1546
                        o_spi_wr <= 1'b1;
1547
                        o_qspi_req <= 1'b1;
1548
                        o_wip <= 1'b1;
1549
                        o_spi_len <= 2'b11; // 32 bits
1550
                        o_spi_word[31:24] <= 8'h42;
1551
                        if (accepted) // Read OTP command was just sent
1552
                                id_state <= `ID_OTP_WRITE;
1553
                        end
1554
                `ID_READ_DATA_COMMAND: begin
1555
                        o_spi_len <= 2'b11; // 32-bits
1556 11 dgisselq
                        o_spi_wr <= 1'b1; // Still transmitting
1557 9 dgisselq
                        o_spi_dir <= 1'b1; // Read from SPI
1558
                        o_qspi_req <= 1'b1;
1559
                        if (accepted)
1560
                                id_state <= `ID_GET_DATA;
1561 11 dgisselq
                        first_valid <= 1'b0;
1562 9 dgisselq
                        end
1563
                `ID_GET_DATA: begin
1564
                        o_spi_len <= 2'b11; // 32-bits
1565
                        o_spi_wr <= (~last_addr); // Still transmitting
1566
                        o_spi_dir <= 1'b1; // Read from SPI
1567
                        o_qspi_req <= 1'b1;
1568 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1569
                        if((i_spi_valid)&&(!invalid_ack_pipe[3]))
1570 9 dgisselq
                        begin
1571
                                set_val <= 1'b1;
1572
                                set_addr <= lcl_id_addr[2:0];
1573
                                // idmem[lcl_id_addr[2:0]] <= i_spi_data;
1574
                                lcl_id_addr <= lcl_id_addr + 3'h1;
1575
                                if (last_addr)
1576
                                        id_state <= `ID_LOADED;
1577
                        end end
1578
                `ID_LOADED: begin
1579
                        id_loaded <= 1'b1;
1580
                        o_bus_ack  <= 1'b1;
1581
                        o_spi_wr   <= 1'b0;
1582
                        nxt_data_ack <= 1'b1;
1583
                        id_state   <= `ID_LOADED_NEXT;
1584
                        end
1585
                `ID_LOADED_NEXT: begin
1586
                        o_spi_len <= 2'b11; // 32-bits
1587
                        o_bus_ack  <= 1'b0;
1588
                        o_spi_wr   <= 1'b0;
1589
                        nxt_data_ack <= 1'b1;
1590
                        id_state   <= `ID_IDLE;
1591
                        end
1592
                `ID_OTP_SEND_DUMMY: begin
1593
                        o_spi_len <= 2'b00; // 1 byte
1594
                        o_spi_wr  <= 1'b1; // Still writing
1595
                        o_spi_dir <= 1'b0; // Write to SPI
1596
                        if (accepted) // Wait for the command to be accepted
1597
                                id_state <= `ID_OTP_CLEAR;
1598
                        end
1599
                `ID_OTP_CLEAR: begin
1600
                        o_spi_wr  <= 1'b1; // Still writing
1601
                        o_spi_dir <= 1'b1; // Read from SPI
1602 11 dgisselq
                        o_spi_len <= 2'b11; // Read 32 bits
1603 9 dgisselq
                        if (accepted)
1604
                                id_state <= `ID_OTP_GET_DATA;
1605
                        end
1606
                `ID_OTP_GET_DATA: begin
1607 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1608
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3]))
1609 9 dgisselq
                        begin
1610
                                id_state <= `ID_FINAL_STOP;
1611
                                nxt_data_ack <= 1'b1;
1612
                                nxt_data_spi <= 1'b1;
1613
                        end end
1614
                `ID_OTP_WRITE: begin
1615
                        o_spi_wr  <= 1'b1;
1616
                        o_spi_len <= 2'b11;
1617
                        o_spi_dir <= 1'b0; // Write to SPI
1618
                        o_spi_word <= r_data;
1619
                        // o_bus_ack  <= (otp_wr_request)&&(accepted)&&(i_pipewr);
1620
                        // o_data_ack <= (otp_wr_request)&&(accepted);
1621
                        if (accepted) // &&(~i_pipewr)
1622
                                id_state <= `ID_WAIT_ON_STOP;
1623
                        else if(accepted)
1624
                        begin
1625
                                o_spi_word <= i_data;
1626
                                r_data <= i_data;
1627
                        end end
1628
                `ID_WAIT_ON_STOP: begin
1629
                        o_spi_wr <= 1'b0;
1630
                        if (i_spi_stopped)
1631
                                id_state <= `ID_REQ_STATUS;
1632
                        end
1633
                `ID_REQ_STATUS: begin
1634
                        o_spi_wr <= 1'b1;
1635
                        o_spi_hold <= 1'b0;
1636
                        o_spi_word[31:24] <= 8'h05;
1637
                        o_spi_dir <= 1'b0;
1638
                        o_spi_len <= 2'b00;
1639
                        if (accepted)
1640
                                id_state <= `ID_REQ_STATUS_NEXT;
1641
                        end
1642
                `ID_REQ_STATUS_NEXT: begin
1643
                        o_spi_wr <= 1'b1;
1644
                        o_spi_hold <= 1'b0;
1645
                        o_spi_dir <= 1'b1; // Read
1646
                        o_spi_len <= 2'b00; // 8 bits
1647
                        // o_spi_word <= dont care
1648
                        if (accepted)
1649
                                id_state <= `ID_READ_STATUS;
1650
                        end
1651
                `ID_READ_STATUS: begin
1652
                        o_spi_wr <= 1'b1;
1653
                        o_spi_hold <= 1'b0;
1654
                        o_spi_dir <= 1'b1; // Read
1655
                        o_spi_len <= 2'b00; // 8 bits
1656
                        // o_spi_word <= dont care
1657 11 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1658
                        if ((i_spi_valid)&&(~invalid_ack_pipe[3]))
1659 9 dgisselq
                                chk_wip <= 1'b1;
1660
                        if ((chk_wip)&&(~i_spi_data[0]))
1661
                        begin
1662
                                o_wip <= 1'b0;
1663
                                id_state <= `ID_FINAL_STOP;
1664
                        end end
1665
                default: begin // ID_FINAL_STOP
1666
                        o_bus_ack <= 1'b0;
1667
                        nxt_data_ack <= 1'b0;
1668
                        o_qspi_req <= 1'b0;
1669
                        o_spi_wr <= 1'b0;
1670
                        o_spi_hold <= 1'b0;
1671
                        o_spi_dir <= 1'b1; // Read
1672
                        o_spi_len <= 2'b00; // 8 bits
1673
                        // o_spi_word <= dont care
1674
                        if (i_spi_stopped)
1675
                                id_state <= `ID_IDLE;
1676
                        end
1677
                endcase
1678
        end
1679
 
1680
        always @(posedge i_clk)
1681
        begin
1682
                if (nxt_data_ack)
1683
                        o_data <= (nxt_data_spi)?i_spi_data : nxt_data;
1684
                o_data_ack <= nxt_data_ack;
1685
        end
1686
 
1687
        always @(posedge i_clk)
1688
                if (set_val)
1689
                        idmem[set_addr] <= i_spi_data;
1690
 
1691
        assign  o_spi_spd = 1'b0; // Slow, 1-bit at a time
1692
 
1693
endmodule
1694
 
1695
 
1696
 

powered by: WebSVN 2.1.0

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