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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [eqspiflash.v] - Blame information for rev 27

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

Line No. Rev Author Line
1 3 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 20 dgisselq
//      13. Reset Enable
67
//      14. Reset Memory
68 3 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
        // lleqspi
149
        //
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
        lleqspi lowlvl(i_clk_200mhz, spi_wr, spi_hold, spi_word, spi_len,
158
                        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 20 dgisselq
                        w_xip, w_quad, w_idloaded, w_leave_xip;
183 3 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 20 dgisselq
                                        w_quad, w_xip, w_leave_xip);
223 3 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 20 dgisselq
                                bus_ctreq, bus_wr, bus_addr[3:0], bus_data, bus_sector,
273 3 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 20 dgisselq
                                ct_ack, ct_data_ack, ct_data, w_leave_xip, w_xip, w_quad);
278 3 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
                        if ((i_data_stb)||(i_ctrl_stb))
509
                        begin
510
                                pending <= 1'b1;
511
                                o_addr <= i_addr;
512
                                o_data <= i_data;
513
                                o_wr   <= i_we;
514
                                next_addr <= i_addr + 22'h1;
515
                        end
516
 
517
                        if ((i_data_stb)&&(~i_we))
518
                                o_readreq <= 1'b1;
519
 
520
                        if ((i_data_stb)&&(i_we))
521
                                lcl_wrreq <= 1'b1;
522
                        if ((i_ctrl_stb)&&(~i_addr[4]))
523
                        begin
524
                                casez(i_addr[4:0])
525
                                5'h0: lcl_ctreq<= 1'b1;
526
                                5'h1: lcl_ctreq <= 1'b1;
527
                                5'h2: lcl_ctreq <= 1'b1;
528
                                5'h3: lcl_ctreq <= 1'b1;
529
                                5'h4: lcl_ctreq <= 1'b1;
530
                                5'h5: lcl_ctreq <= 1'b1;
531
                                5'h6: lcl_ctreq <= 1'b1;
532
                                5'h7: lcl_ctreq <= 1'b1;
533 20 dgisselq
                                5'h8: o_idreq  <= 1'b1; // ID[0]
534
                                5'h9: o_idreq  <= 1'b1; // ID[1]
535
                                5'ha: o_idreq  <= 1'b1; // ID[2]
536
                                5'hb: o_idreq  <= 1'b1; // ID[3]
537
                                5'hc: o_idreq  <= 1'b1; // ID[4]
538
                                5'hd: lcl_ctreq <= 1'b1;        //
539
                                5'he: lcl_ctreq <= 1'b1;
540
                                5'hf: o_idreq   <= 1'b1; // Program OTP register
541 3 dgisselq
                                default: begin o_idreq <= 1'b1; end
542
                                endcase
543
                        end else if (i_ctrl_stb)
544
                                o_idreq <= 1'b1;
545
                end else if (ack)
546
                begin
547
                        pending <= 1'b0;
548
                        o_readreq <= 1'b0;
549
                        o_idreq <= 1'b0;
550
                        lcl_ctreq <= 1'b0;
551
                        lcl_wrreq <= 1'b0;
552
                end
553
 
554
                if(i_rst)
555
                begin
556
                        pending <= 1'b0;
557
                        o_readreq <= 1'b0;
558
                        o_idreq <= 1'b0;
559
                        lcl_ctreq <= 1'b0;
560
                        lcl_wrreq <= 1'b0;
561
                end
562
 
563
                if ((i_data_stb)&&(~o_wb_stall))
564
                        o_piperd <= ((~i_we)&&(~o_wb_stall)&&(pipeable)&&(i_addr == next_addr));
565
                else if ((i_ack)||(((i_ctrl_stb)||(i_data_stb))&&(~o_wb_stall)))
566
                        o_piperd <= 1'b0;
567
                if ((i_data_stb)&&(~o_wb_stall))
568
                        pipeable <= (~i_we);
569
                else if ((i_ctrl_stb)&&(~o_wb_stall))
570
                        pipeable <= 1'b0;
571
 
572
                o_pipewr <= (same_page)||(pending)&&(o_pipewr);
573
        end
574
 
575
        reg     r_other, last_wip;
576
 
577
        reg     last_pending;
578
        always @(posedge i_clk)
579
                last_pending <= pending;
580
        always @(posedge i_clk)
581
                last_wip <= i_wip;
582
        wire    new_req;
583
        assign  new_req = (pending)&&(~last_pending);
584
 
585 20 dgisselq
        initial esector   = 15'h00;
586 3 dgisselq
        initial o_wrreq   = 1'b0;
587
        initial o_erreq   = 1'b0;
588
        initial wp_err    = 1'b0;
589
        initial lcl_ack   = 1'b0;
590
        initial r_other   = 1'b0;
591
        initial o_endwr   = 1'b1;
592
        initial wp        = 1'b1;
593
        always @(posedge i_clk)
594
        begin
595
                if (i_ack)
596
                begin
597
                        o_erreq <= 1'b0;
598
                        o_wrreq <= 1'b0;
599
                        o_ctreq <= 1'b0;
600
                        r_other <= 1'b0;
601
                end
602
 
603
                if ((last_wip)&&(~i_wip))
604
                        wp <= 1'b1;
605
 
606
                // o_endwr  <= ((~i_cyc)||(~o_wr)||(o_pipewr))
607
                                // ||(~new_req)&&(o_endwr);
608
                o_endwr <= ((pending)&&(~o_pipewr))||((~pending)&&(~i_cyc));
609
 
610
                // Default ACK is always set to zero, unless the following ...
611
                o_wb_ack <= 1'b0;
612
 
613
                if (set_sector)
614
                begin
615
                        esector[13:0] <= { o_data[23:14], 4'h0 };
616
                        wp <= (o_data[30])&&(new_req)||(wp)&&(~new_req);
617 20 dgisselq
                        esector[14] <= o_data[28]; // Subsector
618 3 dgisselq
                        if (o_data[28])
619
                        begin
620
                                esector[3:0] <= o_data[13:10];
621
                        end
622
                end
623
 
624
                lcl_ack <= 1'b0;
625
                if ((i_wip)&&(new_req)&&(~same_page))
626
                begin
627
                        o_wb_ack <= 1'b1;
628
                        lcl_ack <= 1'b1;
629
                end else if ((ctreg_stb)&&(new_req))
630
                begin // A request of the status register
631
                        // Always ack control register, even on failed attempts
632
                        // to erase.
633
                        o_wb_ack <= 1'b1;
634
                        lcl_ack <= 1'b1;
635
 
636
                        if (lcl_key)
637
                        begin
638
                                o_ctreq <= 1'b0;
639
                                o_erreq <= 1'b1;
640
                                r_other <= 1'b1;
641
                                lcl_ack <= 1'b0;
642
                        end else if ((o_wr)&&(~o_data[31]))
643
                        begin // WEL or WEL disable
644
                                o_ctreq <= (wp == o_data[30]);
645
                                r_other <= (wp == o_data[30]);
646
                                lcl_ack <= (wp != o_data[30]);
647
                                wp <= !o_data[30];
648
                        end else if (~o_wr)
649
                                lcl_ack <= 1'b1;
650
                        wp_err <= (o_data[31])&&(~lcl_key);
651
                end else if ((lcl_ctreq)&&(new_req))
652
                begin
653
                        o_ctreq <= 1'b1;
654
                        r_other <= 1'b1;
655
                end else if ((lcl_wrreq)&&(new_req))
656
                begin
657
                        if (~wp)
658
                        begin
659
                                o_wrreq <= 1'b1;
660
                                r_other <= 1'b1;
661
                                o_endwr  <= 1'b0;
662
                                lcl_ack <= 1'b0;
663
                        end else begin
664
                                o_wb_ack <= 1'b1;
665
                                wp_err <= 1'b1;
666
                                lcl_ack <= 1'b1;
667
                        end
668
                end
669
 
670
                if (i_rst)
671
                begin
672
                        o_ctreq <= 1'b0;
673
                        o_erreq <= 1'b0;
674
                        o_wrreq <= 1'b0;
675
                        r_other <= 1'b0;
676
                end
677
 
678
        end
679
 
680
 
681
        assign o_wb_data[31:0] = { i_wip, ~wp, i_quad, esector[14],
682
                        i_idloaded, wp_err, i_xip, i_spi_stopped,
683
                        esector[13:0], 10'h00 };
684
        assign  o_sector = { esector[13:0], 8'h00 }; // 22 bits
685
        assign  o_other = (r_other)||(o_idreq);
686
 
687
endmodule
688
 
689
 
690
`define RD_IDLE                 4'h0
691
`define RD_IDLE_GET_PORT        4'h1
692
`define RD_SLOW_DUMMY           4'h2
693
`define RD_SLOW_READ_DATA       4'h3
694
`define RD_QUAD_READ_DATA       4'h4
695
`define RD_QUAD_DUMMY           4'h5
696
`define RD_QUAD_ADDRESS         4'h6
697
`define RD_XIP                  4'h7
698
`define RD_GO_TO_IDLE           4'h8
699
`define RD_GO_TO_XIP            4'h9
700
 
701
module  readqspi(i_clk, i_readreq, i_piperd, i_other_req, i_addr, o_bus_ack,
702
                o_qspi_req, i_grant,
703
                        o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
704
                                o_spi_spd, o_spi_dir, o_spi_recycle,
705
                        i_spi_data, i_spi_valid, i_spi_busy, i_spi_stopped,
706 20 dgisselq
                        o_data_ack, o_data, i_quad, i_xip, o_leave_xip);
707 3 dgisselq
        input                   i_clk;
708
        input                   i_readreq, i_piperd, i_other_req;
709
        input           [21:0]   i_addr;
710
        output  reg             o_bus_ack, o_qspi_req;
711
        input   wire            i_grant;
712
        output  reg             o_spi_wr;
713
        output  wire            o_spi_hold;
714
        output  reg     [31:0]   o_spi_word;
715
        output  reg     [1:0]    o_spi_len;
716
        output  reg             o_spi_spd, o_spi_dir, o_spi_recycle;
717
        input           [31:0]   i_spi_data;
718
        input                   i_spi_valid, i_spi_busy, i_spi_stopped;
719
        output  reg             o_data_ack;
720
        output  reg     [31:0]   o_data;
721
        input                   i_quad, i_xip;
722 20 dgisselq
        output  wire            o_leave_xip;
723 3 dgisselq
 
724
        reg     accepted;
725
        initial accepted = 1'b0;
726
        always @(posedge i_clk)
727
                accepted <= (~i_spi_busy)&&(i_grant)&&(o_spi_wr)&&(~accepted);
728
 
729
        reg     [3:0]    rd_state;
730 13 dgisselq
        reg             r_leave_xip, r_xip, r_quad, r_requested;
731
        reg     [3:0]    invalid_ack_pipe;
732 3 dgisselq
        initial rd_state = `RD_IDLE;
733
        initial o_data_ack = 1'b0;
734
        initial o_bus_ack  = 1'b0;
735
        initial o_qspi_req = 1'b0;
736
        always @(posedge i_clk)
737
        begin
738
                o_data_ack <= 1'b0;
739
                o_bus_ack <= 1'b0;
740
                o_spi_recycle <= 1'b0;
741
                if (i_spi_valid)
742
                        o_data <= i_spi_data;
743 13 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
744 3 dgisselq
                case(rd_state)
745
                `RD_IDLE: begin
746
                        r_requested <= 1'b0;
747
                        o_qspi_req <= 1'b0;
748
                        o_spi_word <= { ((i_quad)? 8'h6B: 8'h0b), i_addr, 2'b00 };
749
                        o_spi_wr <= 1'b0;
750
                        o_spi_dir <= 1'b0;
751
                        o_spi_spd <= 1'b0;
752
                        o_spi_len <= 2'b11;
753
                        r_xip <= (i_xip)&&(i_quad);
754
                        r_leave_xip <= 1'b0; // Not in it, so can't leave it
755
                        r_quad <= i_quad;
756
                        if (i_readreq)
757
                        begin
758
                                rd_state <= `RD_IDLE_GET_PORT;
759
                                o_bus_ack <= 1'b1;
760
                        end end
761
                `RD_IDLE_GET_PORT: begin
762
                        o_spi_wr <= 1'b1; // Write the address
763
                        o_qspi_req <= 1'b1;
764
                        if (accepted)
765
                                rd_state <= `RD_SLOW_DUMMY;
766
                        end
767
                `RD_SLOW_DUMMY: begin
768
                        o_spi_wr <= 1'b1; // Write 8 dummy clocks
769
                        o_qspi_req <= 1'b1;
770
                        o_spi_dir <= 1'b0;
771
                        o_spi_spd <= 1'b0;
772
                        o_spi_word[31:24] <= (r_xip) ? 8'h00 : 8'hff;
773
                        o_spi_len  <= 2'b00; // 8 clocks = 8-bits
774
                        if (accepted)
775
                                rd_state <= (r_quad)?`RD_QUAD_READ_DATA
776
                                                : `RD_SLOW_READ_DATA;
777
                        end
778
                `RD_SLOW_READ_DATA: begin
779
                        o_qspi_req <= 1'b1;
780
                        o_spi_dir <= 1'b1;
781
                        o_spi_spd <= 1'b0;
782
                        o_spi_len <= 2'b11;
783
                        o_spi_wr <= (~r_requested)||(i_piperd);
784 13 dgisselq
                        invalid_ack_pipe[0] <= (!r_requested);
785
                        o_data_ack <=  (!invalid_ack_pipe[3])&&(i_spi_valid)&&(r_requested);
786 3 dgisselq
                        o_bus_ack <=   (r_requested)&&(accepted)&&(i_piperd);
787
                        r_requested <= (r_requested)||(accepted);
788
                        if ((i_spi_valid)&&(~o_spi_wr))
789
                                rd_state <= `RD_GO_TO_IDLE;
790
                        end
791
                `RD_QUAD_READ_DATA: begin
792
                        o_qspi_req <= 1'b1;
793
                        o_spi_dir <= 1'b1;
794
                        o_spi_spd <= 1'b1;
795
                        o_spi_len <= 2'b11;
796
                        o_spi_recycle <= (r_leave_xip)? 1'b1: 1'b0;
797 13 dgisselq
                        invalid_ack_pipe[0] <= (!r_requested);
798 3 dgisselq
                        r_requested <= (r_requested)||(accepted);
799 13 dgisselq
                        o_data_ack <=  (!invalid_ack_pipe[3])&&(i_spi_valid)&&(r_requested)&&(~r_leave_xip);
800 3 dgisselq
                        o_bus_ack  <= (r_requested)&&(accepted)&&(i_piperd)&&(~r_leave_xip);
801
                        o_spi_wr <= (~r_requested)||(i_piperd);
802
                        // if (accepted)
803
                                // o_spi_wr <= (i_piperd);
804 12 dgisselq
                        if (accepted) // only happens if (o_spi_wr)
805 3 dgisselq
                                o_data <= i_spi_data;
806
                        if ((i_spi_valid)&&(~o_spi_wr))
807
                                rd_state <= ((r_leave_xip)||(~r_xip))?`RD_GO_TO_IDLE:`RD_GO_TO_XIP;
808
                        end
809
                `RD_QUAD_ADDRESS: begin
810
                        o_qspi_req <= 1'b1;
811
                        o_spi_wr <= 1'b1;
812
                        o_spi_dir <= 1'b0; // Write the address
813 20 dgisselq
                        o_spi_spd <= 1'b0;
814 3 dgisselq
                        o_spi_word[31:0] <= { i_addr, 2'b00, 8'h00 };
815
                        o_spi_len  <= 2'b10; // 24 bits, High speed, 6 clocks
816
                        if (accepted)
817
                                rd_state <= `RD_QUAD_DUMMY;
818
                        end
819
                `RD_QUAD_DUMMY: begin
820
                        o_qspi_req <= 1'b1;
821
                        o_spi_wr <= 1'b1;
822
                        o_spi_dir <= 1'b0; // Write the dummy
823
                        o_spi_spd <= 1'b1; // High speed
824
                        o_spi_word[31:0] <= (r_xip)? 32'h00 : 32'hffffffff;
825
                        o_spi_len  <= 2'b11; // 8 clocks = 32-bits, quad speed
826
                        if (accepted)
827
                                rd_state <= (r_quad)?`RD_QUAD_READ_DATA
828
                                                : `RD_SLOW_READ_DATA;
829
                        end
830
                `RD_XIP: begin
831
                        r_requested <= 1'b0;
832
                        o_qspi_req <= 1'b1;
833
                        o_spi_word <= { i_addr, 2'b00, 8'h00 };
834
                        o_spi_wr <= 1'b0;
835
                        o_spi_dir <= 1'b0; // Write to SPI
836 20 dgisselq
                        o_spi_spd <= 1'b0;
837 3 dgisselq
                        o_spi_len <= 2'b11;
838
                        r_leave_xip <= i_other_req;
839
                        r_xip <= (~i_other_req);
840
                        o_bus_ack <= 1'b0;
841
                        if ((i_readreq)||(i_other_req))
842
                        begin
843
                                rd_state <= `RD_QUAD_ADDRESS;
844
                                o_bus_ack <= i_readreq;
845
                        end end
846
                `RD_GO_TO_IDLE: begin
847 13 dgisselq
                        if ((!invalid_ack_pipe[3])&&(i_spi_valid)&&(~r_leave_xip))
848
                                o_data_ack <=  1'b1;
849 12 dgisselq
                        o_spi_wr   <= 1'b0;
850 3 dgisselq
                        o_qspi_req <= 1'b0;
851
                        if ((i_spi_stopped)&&(~i_grant))
852
                                rd_state <= `RD_IDLE;
853
                        end
854
                `RD_GO_TO_XIP: begin
855 12 dgisselq
                        r_requested <= 1'b0;
856 13 dgisselq
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3]))
857
                                o_data_ack <=  1'b1;
858 3 dgisselq
                        o_qspi_req <= 1'b1;
859
                        o_spi_wr   <= 1'b0;
860
                        if (i_spi_stopped)
861
                                rd_state <= `RD_XIP;
862
                        end
863
                default: begin
864
                        // rd_state <= (i_grant)?`RD_BREAK;
865
                        o_qspi_req <= 1'b0;
866
                        o_spi_wr <= 1'b0;
867
                        if ((i_spi_stopped)&&(~i_grant))
868
                                rd_state <= `RD_IDLE;
869
                        end
870
                endcase
871
        end
872
 
873
        assign  o_spi_hold = 1'b0;
874 20 dgisselq
        assign  o_leave_xip = r_leave_xip;
875 3 dgisselq
 
876
endmodule
877
 
878
module  writeqspi(i_clk, i_wreq, i_ereq, i_pipewr, i_endpipe, i_addr, i_data,
879
                        o_bus_ack, o_qspi_req, i_qspi_grant,
880
                                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
881
                                o_spi_spd, o_spi_dir, i_spi_data, i_spi_valid,
882
                                        i_spi_busy, i_spi_stopped,
883
                                o_data_ack, i_quad, o_wip);
884
        input           i_clk;
885
        //
886
        input           i_wreq, i_ereq, i_pipewr, i_endpipe;
887
        input           [21:0]   i_addr;
888
        input           [31:0]   i_data;
889
        output  reg             o_bus_ack, o_qspi_req;
890
        input                   i_qspi_grant;
891
        output  reg             o_spi_wr, o_spi_hold;
892
        output  reg     [31:0]   o_spi_word;
893
        output  reg     [1:0]    o_spi_len;
894
        output  reg             o_spi_spd, o_spi_dir;
895
        input           [31:0]   i_spi_data;
896
        input                   i_spi_valid;
897
        input                   i_spi_busy, i_spi_stopped;
898
        output  reg             o_data_ack;
899
        input                   i_quad;
900
        output  reg             o_wip;
901
 
902
`ifdef  QSPI_READ_ONLY
903
        always @(posedge i_clk)
904
                o_data_ack <= (i_wreq)||(i_ereq);
905
        always @(posedge i_clk)
906
                o_bus_ack <= (i_wreq)||(i_ereq);
907
 
908
        always @(posedge i_clk)
909
        begin
910
                o_qspi_req <= 1'b0;
911
                o_spi_wr   <= 1'b0;
912
                o_spi_hold <= 1'b0;
913
                o_spi_dir  <= 1'b1; // Read
914
                o_spi_spd  <= i_quad;
915
                o_spi_len  <= 2'b00;
916
                o_spi_word <= 32'h00;
917
                o_wip <= 1'b0;
918
        end
919
`else
920
 
921
`define WR_IDLE                         4'h0
922
`define WR_START_WRITE                  4'h1
923
`define WR_START_QWRITE                 4'h2
924
`define WR_PROGRAM                      4'h3
925
`define WR_PROGRAM_GETNEXT              4'h4
926
`define WR_START_ERASE                  4'h5
927
`define WR_WAIT_ON_STOP                 4'h6
928
`define WR_REQUEST_STATUS               4'h7
929
`define WR_REQUEST_STATUS_NEXT          4'h8
930
`define WR_READ_STATUS                  4'h9
931
`define WR_WAIT_ON_FINAL_STOP           4'ha
932
 
933
        reg     accepted;
934
        initial accepted = 1'b0;
935
        always @(posedge i_clk)
936
                accepted <= (~i_spi_busy)&&(i_qspi_grant)&&(o_spi_wr)&&(~accepted);
937
 
938
 
939 12 dgisselq
        reg             cyc, chk_wip, valid_status;
940 3 dgisselq
        reg     [3:0]    wr_state;
941
        initial wr_state = `WR_IDLE;
942
        initial cyc = 1'b0;
943
        always @(posedge i_clk)
944
        begin
945
                chk_wip <= 1'b0;
946
                o_bus_ack  <= 1'b0;
947
                o_data_ack <= 1'b0;
948
                case(wr_state)
949
                `WR_IDLE: begin
950 12 dgisselq
                        valid_status <= 1'b0;
951 3 dgisselq
                        o_qspi_req <= 1'b0;
952
                        cyc <= 1'b0;
953
                        if (i_ereq)
954
                                wr_state <= `WR_START_ERASE;
955
                        else if (i_wreq)
956
                                wr_state <= (i_quad)?`WR_START_QWRITE
957
                                        : `WR_START_WRITE;
958
                        end
959
                `WR_START_WRITE: begin
960
                        o_wip      <= 1'b1;
961
                        o_qspi_req <= 1'b1;
962
                        o_spi_wr   <= 1'b1;
963
                        o_spi_dir  <= 1'b0;
964
                        o_spi_len  <= 2'b11;
965
                        o_spi_spd  <= 1'b0;
966
                        o_spi_hold <= 1'b1;
967
                        o_spi_word <= { 8'h02, i_addr, 2'b00 };
968
                        cyc <= 1'b1;
969
                        if (accepted)
970
                        begin
971
                                o_bus_ack  <= 1'b1;
972
                                o_data_ack <= 1'b1;
973
                                wr_state <= `WR_PROGRAM;
974
                                o_spi_word <= i_data;
975
                        end end
976
                `WR_START_QWRITE: begin
977
                        o_wip      <= 1'b1;
978
                        o_qspi_req <= 1'b1;
979
                        o_spi_wr   <= 1'b1;
980
                        o_spi_dir  <= 1'b0;
981
                        o_spi_len  <= 2'b11;
982
                        o_spi_spd  <= 1'b0;
983
                        o_spi_hold <= 1'b1;
984
                        o_spi_word <= { 8'h32, i_addr, 2'b00 };
985
                        cyc <= 1'b1;
986
                        if (accepted)
987
                        begin
988
                                o_bus_ack  <= 1'b1;
989
                                o_data_ack <= 1'b1;
990
                                wr_state <= `WR_PROGRAM;
991
                                o_spi_word <= i_data;
992
                        end end
993
                `WR_PROGRAM: begin
994
                        o_wip     <= 1'b1;
995
                        o_qspi_req <= 1'b1;
996
                        o_spi_wr   <= 1'b1;
997
                        o_spi_dir  <= 1'b0;
998
                        o_spi_len  <= 2'b11;
999
                        o_spi_spd  <= i_quad;
1000
                        o_spi_hold <= 1'b1;
1001
                        // o_spi_word <= i_data;
1002
                        if (accepted)
1003
                                wr_state <= `WR_PROGRAM_GETNEXT;
1004
                        end
1005
                `WR_PROGRAM_GETNEXT: begin
1006
                        o_wip      <= 1'b1;
1007
                        o_qspi_req <= 1'b1;
1008
                        o_spi_wr   <= 1'b0;
1009
                        o_spi_dir  <= 1'b0;
1010
                        o_spi_len  <= 2'b11;
1011
                        o_spi_spd  <= i_quad;
1012
                        o_spi_hold <= 1'b1;
1013
                        o_spi_word <= i_data;
1014 13 dgisselq
                        cyc <= (cyc)&&(~i_endpipe);
1015 3 dgisselq
                        if (~cyc)
1016
                                wr_state <= `WR_WAIT_ON_STOP;
1017
                        else if (i_pipewr)
1018
                        begin
1019
                                o_bus_ack  <= 1'b1;
1020
                                o_data_ack <= 1'b1;
1021
                                wr_state <= `WR_PROGRAM;
1022
                        end end
1023
                `WR_START_ERASE: begin
1024
                        o_wip <= 1'b1;
1025
                        o_qspi_req <= 1'b1;
1026
                        o_spi_wr  <= 1'b1;
1027
                        o_spi_dir <= 1'b0;
1028
                        o_spi_spd <= 1'b0;
1029
                        o_spi_len <= 2'b11;
1030
                        if (i_data[28])
1031
                                // Subsector erase
1032
                                o_spi_word[31:24] <= 8'h20;
1033
                        else
1034
                                // Sector erase
1035
                                o_spi_word[31:24] <= 8'hd8;
1036
                        o_spi_word[23:0] <= { i_data[21:10], 12'h0 };
1037
                        // Data has already been ack'd, so no need to ack
1038
                        // it again.  However, we can now free the QSPI
1039
                        // bus processor to accept another command from the
1040
                        // bus.
1041
                        o_bus_ack <= accepted;
1042
                        if (accepted)
1043
                                wr_state <= `WR_WAIT_ON_STOP;
1044
                        end
1045
                `WR_WAIT_ON_STOP: begin
1046
                        o_wip <= 1'b1;
1047
                        o_qspi_req <= 1'b0;
1048
                        o_spi_wr   <= 1'b0;
1049
                        o_spi_hold <= 1'b0;
1050
                        if (i_spi_stopped)
1051
                                wr_state <= `WR_REQUEST_STATUS;
1052
                        end
1053
                `WR_REQUEST_STATUS: begin
1054
                        o_wip <= 1'b1;
1055
                        o_qspi_req <= 1'b1;
1056
                        o_spi_hold <= 1'b0;
1057
                        o_spi_wr   <= 1'b1;
1058
                        o_spi_spd  <= 1'b0; // Slow speed
1059
                        o_spi_len  <= 2'b00; // 8 bytes
1060
                        o_spi_dir  <= 1'b0; // Write
1061
                        o_spi_word <= { 8'h05, 24'h00 };
1062
                        if (accepted)
1063
                                wr_state <= `WR_REQUEST_STATUS_NEXT;
1064
                        end
1065
                `WR_REQUEST_STATUS_NEXT: begin
1066
                        o_wip <= 1'b1;
1067
                        o_qspi_req <= 1'b1;
1068
                        o_spi_hold <= 1'b0;
1069
                        o_spi_wr   <= 1'b1;
1070
                        o_spi_spd  <= 1'b0; // Slow speed
1071
                        o_spi_len  <= 2'b00; // 8 bytes
1072
                        o_spi_dir  <= 1'b1; // Read
1073
                        o_spi_word <= 32'h00;
1074
                        if (accepted)
1075
                                wr_state <= `WR_READ_STATUS;
1076 12 dgisselq
                        valid_status <= 1'b0;
1077 3 dgisselq
                        end
1078
                `WR_READ_STATUS: begin
1079
                        o_wip <= 1'b1;
1080
                        o_qspi_req <= 1'b1;
1081
                        o_spi_hold <= 1'b0;
1082
                        o_spi_wr   <= 1'b1;
1083
                        o_spi_spd  <= 1'b0; // Slow speed
1084
                        o_spi_len  <= 2'b00; // 8 bytes
1085
                        o_spi_dir  <= 1'b1; // Read
1086
                        o_spi_word <= 32'h00;
1087
                        if (i_spi_valid)
1088 12 dgisselq
                                valid_status <= 1'b1;
1089
                        if ((i_spi_valid)&&(valid_status))
1090 3 dgisselq
                                chk_wip <= 1'b1;
1091
                        if ((chk_wip)&&(~i_spi_data[0]))
1092
                                wr_state <= `WR_WAIT_ON_FINAL_STOP;
1093
                        end
1094
                // `WR_WAIT_ON_FINAL_STOP: // Same as the default
1095
                default: begin
1096
                        o_qspi_req <= 1'b0;
1097
                        o_spi_wr <= 1'b0;
1098
                        o_wip <= 1'b0;
1099
                        if (i_spi_stopped)
1100
                                wr_state <= `WR_IDLE;
1101
                        end
1102
                endcase
1103
        end
1104
`endif
1105
 
1106
endmodule
1107
 
1108
 
1109
`define CT_SAFE
1110
`define CT_IDLE                 3'h0
1111
`define CT_NEXT                 3'h1
1112
`define CT_GRANTED              3'h2
1113
`define CT_DATA                 3'h3
1114
`define CT_READ_DATA            3'h4
1115
`define CT_WAIT_FOR_IDLE        3'h5
1116
 
1117
// CTRL commands:
1118
//      WEL (write-enable latch)
1119
//      Read Status
1120
module  ctrlspi(i_clk, i_req, i_wr, i_addr, i_data, i_sector_address,
1121
                                o_spi_req, i_grant,
1122
                                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
1123
                                        o_spi_spd, o_spi_dir,
1124
                                i_spi_data, i_spi_valid, i_spi_busy,
1125
                                        i_spi_stopped,
1126 20 dgisselq
                                o_bus_ack, o_data_ack, o_data,
1127
                                i_leave_xip, o_xip, o_quad);
1128 3 dgisselq
        input           i_clk;
1129
        // From the WB bus controller
1130
        input                   i_req;
1131
        input                   i_wr;
1132 20 dgisselq
        input           [3:0]    i_addr;
1133 3 dgisselq
        input           [31:0]   i_data;
1134
        input           [21:0]   i_sector_address;
1135
        // To/from the arbiter
1136
        output  reg             o_spi_req;
1137
        input                   i_grant;
1138
        // To/from the low-level SPI driver
1139
        output  reg             o_spi_wr;
1140
        output  wire            o_spi_hold;
1141
        output  reg     [31:0]   o_spi_word;
1142
        output  reg     [1:0]    o_spi_len;
1143
        output  wire            o_spi_spd;
1144
        output  reg             o_spi_dir;
1145
        input           [31:0]   i_spi_data;
1146
        input                   i_spi_valid;
1147
        input                   i_spi_busy, i_spi_stopped;
1148
        // Return data to the bus controller, and the wishbone bus
1149
        output  reg             o_bus_ack, o_data_ack;
1150
        output  reg     [31:0]   o_data;
1151
        // Configuration items that we may have configured.
1152 20 dgisselq
        input   wire            i_leave_xip;
1153 3 dgisselq
        output  reg             o_xip;
1154
        output  wire            o_quad;
1155
 
1156
        // Command registers
1157
        reg     [1:0]    ctcmd_len;
1158
        reg     [31:0]   ctcmd_word;
1159
        // Data stage registers
1160
        reg             ctdat_skip, // Skip the data phase?
1161
                        ctdat_wr;       // Write during data? (or not read)
1162
        wire    [1:0]    ctdat_len;
1163
        reg     [31:0]   ctdat_word;
1164
 
1165
        reg     [2:0]    ctstate;
1166
        reg             accepted;
1167 13 dgisselq
        reg     [3:0]    invalid_ack_pipe;
1168 3 dgisselq
 
1169
 
1170
        initial accepted = 1'b0;
1171
        always @(posedge i_clk)
1172
                accepted <= (~i_spi_busy)&&(i_grant)&&(o_spi_wr)&&(~accepted);
1173
 
1174 12 dgisselq
        reg     r_ctdat_len, ctbus_ack, first_valid;
1175 3 dgisselq
        assign  ctdat_len = { 1'b0, r_ctdat_len };
1176
 
1177
        // First step, calculate the values for our state machine
1178
        initial o_xip = 1'b0;
1179
        // initial o_quad = 1'b0;
1180
        always @(posedge i_clk)
1181
        if (i_req) // A request for us to act from the bus controller
1182
        begin
1183
                ctdat_skip <= 1'b0;
1184
                ctbus_ack  <= 1'b1;
1185
                ctcmd_word[23:0] <= { i_sector_address, 2'b00 };
1186
                ctdat_word <= { i_data[7:0], 24'h00 };
1187
                ctcmd_len <= 2'b00; // 8bit command (for all but Lock regs)
1188
                r_ctdat_len <= 1'b0; // 8bit data (read or write)
1189
                ctdat_wr <= i_wr;
1190 20 dgisselq
                casez({ i_addr[3:0], i_wr, i_data[30] })
1191
                6'b000010: begin // Write Disable
1192 3 dgisselq
                        ctcmd_word[31:24] <= 8'h04;
1193
                        ctdat_skip <= 1'b1;
1194
                        ctbus_ack  <= 1'b0;
1195
                        end
1196 20 dgisselq
                6'b000011: begin // Write enable
1197 3 dgisselq
                        ctcmd_word[31:24] <= 8'h06;
1198
                        ctdat_skip <= 1'b1;
1199
                        ctbus_ack  <= 1'b0;
1200
                        end
1201
                // 4'b0010?: begin // Read Status register
1202
                //      Moved to defaults section
1203 20 dgisselq
                6'b00011?: begin // Write Status register (Requires WEL)
1204 3 dgisselq
                        ctcmd_word[31:24] <= 8'h01;
1205
`ifdef  CT_SAFE
1206
                        ctdat_word <= { 6'h00, i_data[1:0], 24'h00 };
1207
`else
1208
                        ctdat_word <= { i_data[7:0], 24'h00 };
1209
`endif
1210
                        end
1211 20 dgisselq
                6'b00100?: begin // Read NV-Config register (two bytes)
1212 3 dgisselq
                        ctcmd_word[31:24] <= 8'hB5;
1213
                        r_ctdat_len <= 1'b1; // 16-bit data
1214
                        end
1215 20 dgisselq
                6'b00101?: begin // Write NV-Config reg (2 bytes, Requires WEL)
1216 3 dgisselq
                        ctcmd_word[31:24] <= 8'hB1;
1217
                        r_ctdat_len <= 1'b1; // 16-bit data
1218
`ifdef  CT_SAFE
1219
                        ctdat_word <= { 4'h8, 3'h7, 3'h7, i_data[5:1], 1'b1, 16'h00 };
1220
`else
1221
                        ctdat_word <= { i_data[15:0], 16'h00 };
1222
`endif
1223
                        end
1224 20 dgisselq
                6'b00110?: begin // Read V-Config register
1225 3 dgisselq
                        ctcmd_word[31:24] <= 8'h85;
1226
                        end
1227 20 dgisselq
                6'b00111?: begin // Write V-Config register (Requires WEL)
1228 3 dgisselq
                        ctcmd_word[31:24] <= 8'h81;
1229
                        r_ctdat_len <= 1'b0; // 8-bit data
1230 20 dgisselq
// `ifdef       CT_SAFE
1231
//                      ctdat_word <= { 4'h8, i_data[3:2], 2'b11, 24'h00 };
1232
// `else
1233 3 dgisselq
                        ctdat_word <= { i_data[7:0], 24'h00 };
1234 20 dgisselq
// `endif
1235
                        o_xip <= ~i_data[3];
1236 3 dgisselq
                        end
1237 20 dgisselq
                6'b01000?: begin // Read EV-Config register
1238 3 dgisselq
                        ctcmd_word[31:24] <= 8'h65;
1239
                        end
1240 20 dgisselq
                6'b01001?: begin // Write EV-Config register (Requires WEL)
1241 3 dgisselq
                        ctcmd_word[31:24] <= 8'h61;
1242
                        // o_quad <= (~i_data[7]);
1243
`ifdef  CT_SAFE
1244
                        ctdat_word <= { 1'b1, 3'h5, 4'hf, 24'h00 };
1245
`else
1246
                        ctdat_word <= { i_data[7:0], 24'h00 };
1247
`endif
1248
                        end
1249 20 dgisselq
                6'b01010?: begin // Read Lock register
1250 3 dgisselq
                        ctcmd_word[31:0] <= { 8'he8,  i_sector_address, 2'b00 };
1251
                        ctcmd_len <= 2'b11;
1252
                        ctdat_wr  <= 1'b0;  // Read, not write
1253
                        end
1254 20 dgisselq
                6'b01011?: begin // Write Lock register (Requires WEL)
1255 3 dgisselq
                        ctcmd_word[31:0] <= { 8'he5, i_sector_address, 2'b00 };
1256
                        ctcmd_len <= 2'b11;
1257
                        ctdat_wr  <= 1'b1;  // Write
1258
                        end
1259 20 dgisselq
                6'b01100?: begin // Read Flag Status register
1260 3 dgisselq
                        ctcmd_word[31:24] <= 8'h70;
1261
                        ctdat_wr  <= 1'b0;  // Read, not write
1262
                        end
1263 20 dgisselq
                6'b01101?: begin // Write/Clear Flag Status register (No WEL required)
1264 3 dgisselq
                        ctcmd_word[31:24] <= 8'h50;
1265
                        ctdat_skip <= 1'b1;
1266
                        end
1267 20 dgisselq
                6'b11011?: begin // RESET_ENABLE (when written to)
1268
                        ctcmd_word[31:24] <= 8'h66;
1269
                        ctdat_skip <= 1'b1;
1270
                        end
1271
                6'b11101?: begin // RESET_MEMORY (when written to)
1272
                        ctcmd_word[31:24] <= 8'h99;
1273
                        ctdat_skip <= 1'b1;
1274
                        end
1275 3 dgisselq
                default: begin // Default to reading the status register
1276
                        ctcmd_word[31:24] <= 8'h05;
1277
                        ctdat_wr  <= 1'b0;  // Read, not write
1278
                        r_ctdat_len <= 1'b0; // 8-bit data
1279
                        end
1280
                endcase
1281 20 dgisselq
        end else if (i_leave_xip)
1282
                o_xip <= 1'b0;
1283 3 dgisselq
 
1284
        assign  o_quad = 1'b1;
1285
 
1286
        reg     nxt_data_ack;
1287
 
1288
        // Second step, actually drive the state machine
1289
        initial ctstate = `CT_IDLE;
1290
        always @(posedge i_clk)
1291
        begin
1292
                o_spi_wr <= 1'b1;
1293
                o_bus_ack <= 1'b0;
1294
                o_data_ack <= 1'b0;
1295 13 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
1296 3 dgisselq
                if (i_spi_valid)
1297
                        o_data <= i_spi_data;
1298
                case(ctstate)
1299
                `CT_IDLE: begin
1300
                        o_spi_req <= 1'b0;
1301
                        o_spi_wr  <= 1'b0;
1302
                        if (i_req) // Need a clock to let the digestion
1303
                                ctstate <= `CT_NEXT; // process complete
1304
                        end
1305
                `CT_NEXT: begin
1306
                        o_spi_wr <= 1'b1;
1307
                        o_spi_req <= 1'b1;
1308
                        o_spi_word <= ctcmd_word;
1309
                        o_spi_len <= ctcmd_len;
1310
                        o_spi_dir <= 1'b0; // Write
1311
                        if (accepted)
1312
                        begin
1313
                                ctstate <= (ctdat_skip)?`CT_WAIT_FOR_IDLE:`CT_DATA;
1314
                                o_bus_ack <= (ctdat_skip);
1315
                                o_data_ack <= (ctdat_skip)&&(ctbus_ack);
1316
                        end end
1317
                `CT_GRANTED: begin
1318
                        o_spi_wr <= 1'b1;
1319
                        if ((accepted)&&(ctdat_skip))
1320
                                ctstate <= `CT_WAIT_FOR_IDLE;
1321
                        else if (accepted)//&&(~ctdat_skip)
1322
                                ctstate <= `CT_DATA;
1323
                        end
1324
                `CT_DATA: begin
1325
                        o_spi_wr   <= 1'b1;
1326
                        o_spi_len  <= ctdat_len;
1327
                        o_spi_dir  <= ~ctdat_wr;
1328
                        o_spi_word <= ctdat_word;
1329
                        if (accepted)
1330
                                o_bus_ack <= 1'b1;
1331
                        if (accepted)
1332
                                ctstate <= (ctdat_wr)?`CT_WAIT_FOR_IDLE:`CT_READ_DATA;
1333
                        if ((accepted)&&(ctdat_wr))
1334
                                o_data_ack <= 1'b1;
1335 12 dgisselq
                        first_valid <= 1'b0;
1336 3 dgisselq
                        end
1337
                `CT_READ_DATA: begin
1338
                        o_spi_wr <= 1'b0; // No more words to go, just to wait
1339
                        o_spi_req <= 1'b1;
1340 13 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1341
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3])) // for a value to read
1342 3 dgisselq
                        begin
1343
                                o_data_ack <= 1'b1;
1344
                                o_data <= i_spi_data;
1345
                                ctstate <= `CT_WAIT_FOR_IDLE;
1346
                        end end
1347
                default: begin // `CT_WAIT_FOR_IDLE
1348
                        o_spi_wr <= 1'b0;
1349
                        o_spi_req <= 1'b0;
1350
                        if (i_spi_stopped)
1351
                                ctstate <= `CT_IDLE;
1352
                        end
1353
                endcase
1354
        end
1355
 
1356
        // All of this is done in straight SPI mode, so our speed will always be zero
1357
        assign  o_spi_hold = 1'b0;
1358
        assign  o_spi_spd  = 1'b0;
1359
 
1360
endmodule
1361
 
1362
`define ID_IDLE                         5'h00
1363
`define ID_WAIT_ON_START_ID             5'h01
1364
`define ID_WAIT_ON_START_OTP            5'h02
1365
`define ID_WAIT_ON_START_OTP_WRITE      5'h03
1366
`define ID_READ_DATA_COMMAND            5'h04
1367
`define ID_GET_DATA                     5'h05
1368
`define ID_LOADED                       5'h06
1369
`define ID_LOADED_NEXT                  5'h07
1370
`define ID_OTP_SEND_DUMMY               5'h08
1371
`define ID_OTP_CLEAR                    5'h09
1372
`define ID_OTP_GET_DATA                 5'h0a
1373
`define ID_OTP_WRITE                    5'h0b
1374
`define ID_WAIT_ON_STOP                 5'h0c
1375
`define ID_REQ_STATUS                   5'h0d
1376
`define ID_REQ_STATUS_NEXT              5'h0e
1377
`define ID_READ_STATUS                  5'h0f
1378
//
1379
`define ID_FINAL_STOP                   5'h10
1380
 
1381
module  idotpqspi(i_clk, i_req, i_wr, i_pipewr, i_addr, i_data, o_bus_ack,
1382
                o_qspi_req, i_qspi_grant,
1383
                o_spi_wr, o_spi_hold, o_spi_word, o_spi_len,
1384
                o_spi_spd, o_spi_dir, i_spi_data, i_spi_valid,
1385
                i_spi_busy, i_spi_stopped, o_data_ack, o_data, o_loaded,
1386
                o_wip);
1387
        input                   i_clk;
1388
        input                   i_req, i_wr, i_pipewr;
1389
        input           [4:0]    i_addr;
1390
        input           [31:0]   i_data;
1391
        output  reg             o_bus_ack, o_qspi_req;
1392
        input                   i_qspi_grant;
1393
        output  reg             o_spi_wr, o_spi_hold;
1394
        output  reg     [31:0]   o_spi_word;
1395
        output  reg     [1:0]    o_spi_len;
1396
        output  wire            o_spi_spd;
1397
        output  reg             o_spi_dir;
1398
        input           [31:0]   i_spi_data;
1399
        input                   i_spi_valid, i_spi_busy, i_spi_stopped;
1400
        output  reg             o_data_ack;
1401
        output  reg     [31:0]   o_data;
1402
        output  wire            o_loaded;
1403
        output  reg             o_wip;
1404
 
1405
        reg     id_loaded;
1406
        initial id_loaded = 1'b0;
1407
        assign  o_loaded= id_loaded;
1408
 
1409
/*
1410
        // Only the ID register will be kept in memory, OTP will be read
1411
        // or written upon request
1412
        always @(posedge i_clk)
1413
                if (i_addr[4])
1414
                        o_data <= otpmem[i_addr[3:0]];
1415
                else
1416
                        o_data <= idmem[i_addr[2:0]];
1417
 
1418
        always @(posedge i_clk)
1419
                if ((otp_loaded)&&(i_req)&&(i_addr[4]))
1420
                        o_data_ack <= 1'b1;
1421
                else if ((id_loaded)&&(i_req)&&(~i_addr[4]))
1422
                        o_data_ack <= idmem[i_addr[2:0]];
1423
                else
1424
                        o_data_ack <= 1'b0;
1425
*/
1426
 
1427
        reg     otp_read_request, id_read_request, accepted, otp_wr_request,
1428
                id_read_device, last_id_read;
1429
        reg     [4:0]    req_addr;
1430
        reg     [2:0]    lcl_id_addr;
1431
        reg     [4:0]    id_state;
1432
        always @(posedge i_clk)
1433
        begin
1434
                otp_read_request <= (i_req)&&(~i_wr)&&((i_addr[4])||(i_addr[3:0]==4'hf));
1435
                last_id_read     <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf);
1436
                id_read_request  <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf)&&(~last_id_read);
1437
                id_read_device   <= (i_req)&&(~i_addr[4])&&(i_addr[3:0]!=4'hf)&&(~id_loaded);
1438
                accepted <= (~i_spi_busy)&&(i_qspi_grant)&&(o_spi_wr)&&(~accepted);
1439
 
1440
                otp_wr_request <= (i_req)&&(i_wr)&&((i_addr[4])||(i_addr[3:0]==4'hf));
1441
 
1442
                if (id_state == `ID_IDLE)
1443
                        req_addr <= (i_addr[4:0]==5'h0f) ? 5'h10
1444
                                : { 1'b0, i_addr[3:0] };
1445
        end
1446
 
1447
        reg     last_addr;
1448
        always @(posedge i_clk)
1449
                last_addr <= (lcl_id_addr >= 3'h4);
1450
 
1451
        reg     [31:0]   idmem[0:5];
1452
        reg     [31:0]   r_data;
1453
 
1454
        // Now, quickly, let's deal with the fact that the data from the
1455
        // bus comes one clock later ...
1456
        reg     nxt_data_ack, nxt_data_spi;
1457
        reg     [31:0]   nxt_data;
1458
 
1459 13 dgisselq
        reg     set_val, chk_wip, first_valid;
1460 3 dgisselq
        reg     [2:0]    set_addr;
1461 13 dgisselq
        reg     [3:0]    invalid_ack_pipe;
1462 3 dgisselq
 
1463
        always @(posedge i_clk)
1464
        begin // Depends upon state[4], otp_rd, otp_wr, otp_pipe, id_req, accepted, last_addr
1465
                o_bus_ack <= 1'b0;
1466
                // o_data_ack <= 1'b0;
1467
                o_spi_hold <= 1'b0;
1468
                nxt_data_ack <= 1'b0;
1469
                nxt_data_spi <= 1'b0;
1470
                chk_wip      <= 1'b0;
1471
                set_val <= 1'b0;
1472 13 dgisselq
                invalid_ack_pipe <= { invalid_ack_pipe[2:0], accepted };
1473 3 dgisselq
                if ((id_loaded)&&(id_read_request))
1474
                begin
1475
                        nxt_data_ack <= 1'b1;
1476
                        o_bus_ack  <= 1'b1;
1477
                end
1478
                nxt_data <= idmem[i_addr[2:0]];
1479
                o_spi_wr <= 1'b0; // By default, we send nothing
1480
                case(id_state)
1481
                `ID_IDLE: begin
1482
                        o_qspi_req <= 1'b0;
1483
                        o_spi_dir <= 1'b0; // Write to SPI
1484
                        lcl_id_addr <= 3'h0;
1485
                        o_spi_word[23:7] <= 17'h00;
1486
                        o_spi_word[6:0] <= { req_addr[4:0], 2'b00 };
1487
                        r_data <= i_data;
1488
                        o_wip <= 1'b0;
1489 12 dgisselq
                        first_valid <= 1'b0;
1490 3 dgisselq
                        if (otp_read_request)
1491
                        begin
1492
                                // o_spi_word <= { 8'h48, 8'h00, 8'h00, 8'h00 };
1493
                                id_state <= `ID_WAIT_ON_START_OTP;
1494
                                o_bus_ack <= 1'b1;
1495
                        end else if (otp_wr_request)
1496
                        begin
1497
                                o_bus_ack <= 1'b1;
1498
                                // o_data_ack <= 1'b1;
1499
                                nxt_data_ack <= 1'b1;
1500
                                id_state <= `ID_WAIT_ON_START_OTP_WRITE;
1501
                        end else if (id_read_device)
1502
                        begin
1503
                                id_state <= `ID_WAIT_ON_START_ID;
1504
                                o_bus_ack <= 1'b0;
1505
                                o_spi_word[31:24] <= 8'h9f;
1506
                        end end
1507
                `ID_WAIT_ON_START_ID: begin
1508
                        o_spi_wr <= 1'b1;
1509
                        o_qspi_req <= 1'b1;
1510
                        o_spi_len <= 2'b0; // 8 bits
1511
                        if (accepted)
1512
                                id_state <= `ID_READ_DATA_COMMAND;
1513
                        end
1514
                `ID_WAIT_ON_START_OTP: begin
1515
                        o_spi_wr <= 1'b1;
1516
                        o_spi_word[31:24] <= 8'h4B;
1517
                        o_qspi_req <= 1'b1;
1518
                        o_spi_len <= 2'b11; // 32 bits
1519
                        o_spi_word[6:0] <= { req_addr[4:0], 2'b00 };
1520
                        if (accepted) // Read OTP command was just sent
1521
                                id_state <= `ID_OTP_SEND_DUMMY;
1522
                        end
1523
                `ID_WAIT_ON_START_OTP_WRITE: begin
1524
                        o_spi_wr <= 1'b1;
1525
                        o_qspi_req <= 1'b1;
1526
                        o_wip <= 1'b1;
1527
                        o_spi_len <= 2'b11; // 32 bits
1528
                        o_spi_word[31:24] <= 8'h42;
1529
                        if (accepted) // Read OTP command was just sent
1530
                                id_state <= `ID_OTP_WRITE;
1531
                        end
1532
                `ID_READ_DATA_COMMAND: begin
1533
                        o_spi_len <= 2'b11; // 32-bits
1534 12 dgisselq
                        o_spi_wr <= 1'b1; // Still transmitting
1535 3 dgisselq
                        o_spi_dir <= 1'b1; // Read from SPI
1536
                        o_qspi_req <= 1'b1;
1537
                        if (accepted)
1538
                                id_state <= `ID_GET_DATA;
1539 12 dgisselq
                        first_valid <= 1'b0;
1540 3 dgisselq
                        end
1541
                `ID_GET_DATA: begin
1542
                        o_spi_len <= 2'b11; // 32-bits
1543
                        o_spi_wr <= (~last_addr); // Still transmitting
1544
                        o_spi_dir <= 1'b1; // Read from SPI
1545
                        o_qspi_req <= 1'b1;
1546 13 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1547
                        if((i_spi_valid)&&(!invalid_ack_pipe[3]))
1548 3 dgisselq
                        begin
1549
                                set_val <= 1'b1;
1550
                                set_addr <= lcl_id_addr[2:0];
1551
                                // idmem[lcl_id_addr[2:0]] <= i_spi_data;
1552
                                lcl_id_addr <= lcl_id_addr + 3'h1;
1553
                                if (last_addr)
1554
                                        id_state <= `ID_LOADED;
1555
                        end end
1556
                `ID_LOADED: begin
1557
                        id_loaded <= 1'b1;
1558
                        o_bus_ack  <= 1'b1;
1559
                        o_spi_wr   <= 1'b0;
1560
                        nxt_data_ack <= 1'b1;
1561
                        id_state   <= `ID_LOADED_NEXT;
1562
                        end
1563
                `ID_LOADED_NEXT: begin
1564
                        o_spi_len <= 2'b11; // 32-bits
1565
                        o_bus_ack  <= 1'b0;
1566
                        o_spi_wr   <= 1'b0;
1567
                        nxt_data_ack <= 1'b1;
1568
                        id_state   <= `ID_IDLE;
1569
                        end
1570
                `ID_OTP_SEND_DUMMY: begin
1571
                        o_spi_len <= 2'b00; // 1 byte
1572
                        o_spi_wr  <= 1'b1; // Still writing
1573
                        o_spi_dir <= 1'b0; // Write to SPI
1574
                        if (accepted) // Wait for the command to be accepted
1575
                                id_state <= `ID_OTP_CLEAR;
1576
                        end
1577
                `ID_OTP_CLEAR: begin
1578
                        o_spi_wr  <= 1'b1; // Still writing
1579
                        o_spi_dir <= 1'b1; // Read from SPI
1580 12 dgisselq
                        o_spi_len <= 2'b11; // Read 32 bits
1581 3 dgisselq
                        if (accepted)
1582
                                id_state <= `ID_OTP_GET_DATA;
1583
                        end
1584
                `ID_OTP_GET_DATA: begin
1585 13 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1586
                        if ((i_spi_valid)&&(!invalid_ack_pipe[3]))
1587 3 dgisselq
                        begin
1588
                                id_state <= `ID_FINAL_STOP;
1589
                                nxt_data_ack <= 1'b1;
1590
                                nxt_data_spi <= 1'b1;
1591
                        end end
1592
                `ID_OTP_WRITE: begin
1593
                        o_spi_wr  <= 1'b1;
1594
                        o_spi_len <= 2'b11;
1595
                        o_spi_dir <= 1'b0; // Write to SPI
1596
                        o_spi_word <= r_data;
1597
                        // o_bus_ack  <= (otp_wr_request)&&(accepted)&&(i_pipewr);
1598
                        // o_data_ack <= (otp_wr_request)&&(accepted);
1599
                        if (accepted) // &&(~i_pipewr)
1600
                                id_state <= `ID_WAIT_ON_STOP;
1601
                        else if(accepted)
1602
                        begin
1603
                                o_spi_word <= i_data;
1604
                                r_data <= i_data;
1605
                        end end
1606
                `ID_WAIT_ON_STOP: begin
1607
                        o_spi_wr <= 1'b0;
1608
                        if (i_spi_stopped)
1609
                                id_state <= `ID_REQ_STATUS;
1610
                        end
1611
                `ID_REQ_STATUS: begin
1612
                        o_spi_wr <= 1'b1;
1613
                        o_spi_hold <= 1'b0;
1614
                        o_spi_word[31:24] <= 8'h05;
1615
                        o_spi_dir <= 1'b0;
1616
                        o_spi_len <= 2'b00;
1617
                        if (accepted)
1618
                                id_state <= `ID_REQ_STATUS_NEXT;
1619
                        end
1620
                `ID_REQ_STATUS_NEXT: begin
1621
                        o_spi_wr <= 1'b1;
1622
                        o_spi_hold <= 1'b0;
1623
                        o_spi_dir <= 1'b1; // Read
1624
                        o_spi_len <= 2'b00; // 8 bits
1625
                        // o_spi_word <= dont care
1626
                        if (accepted)
1627
                                id_state <= `ID_READ_STATUS;
1628
                        end
1629
                `ID_READ_STATUS: begin
1630
                        o_spi_wr <= 1'b1;
1631
                        o_spi_hold <= 1'b0;
1632
                        o_spi_dir <= 1'b1; // Read
1633
                        o_spi_len <= 2'b00; // 8 bits
1634
                        // o_spi_word <= dont care
1635 13 dgisselq
                        invalid_ack_pipe[0] <= 1'b0;
1636
                        if ((i_spi_valid)&&(~invalid_ack_pipe[3]))
1637 3 dgisselq
                                chk_wip <= 1'b1;
1638
                        if ((chk_wip)&&(~i_spi_data[0]))
1639
                        begin
1640
                                o_wip <= 1'b0;
1641
                                id_state <= `ID_FINAL_STOP;
1642
                        end end
1643
                default: begin // ID_FINAL_STOP
1644
                        o_bus_ack <= 1'b0;
1645
                        nxt_data_ack <= 1'b0;
1646
                        o_qspi_req <= 1'b0;
1647
                        o_spi_wr <= 1'b0;
1648
                        o_spi_hold <= 1'b0;
1649
                        o_spi_dir <= 1'b1; // Read
1650
                        o_spi_len <= 2'b00; // 8 bits
1651
                        // o_spi_word <= dont care
1652
                        if (i_spi_stopped)
1653
                                id_state <= `ID_IDLE;
1654
                        end
1655
                endcase
1656
        end
1657
 
1658
        always @(posedge i_clk)
1659
        begin
1660
                if (nxt_data_ack)
1661
                        o_data <= (nxt_data_spi)?i_spi_data : nxt_data;
1662
                o_data_ack <= nxt_data_ack;
1663
        end
1664
 
1665
        always @(posedge i_clk)
1666
                if (set_val)
1667
                        idmem[set_addr] <= i_spi_data;
1668
 
1669
        assign  o_spi_spd = 1'b0; // Slow, 1-bit at a time
1670
 
1671
endmodule
1672
 
1673
 
1674
 

powered by: WebSVN 2.1.0

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