blob: 10ea1b34546a85fdc1e2015e7ab03855e276189b [file] [log] [blame]
Tim Edwards44bab472020-10-04 22:09:54 -04001//-------------------------------------
2// SPI controller for Caravel (PicoSoC)
3//-------------------------------------
4// Written by Tim Edwards
5// efabless, inc. September 27, 2020
6//-------------------------------------
7
8//-----------------------------------------------------------
9// This is a standalone slave SPI for the caravel chip that is
10// intended to be independent of the picosoc and independent
11// of all IP blocks except the power-on-reset. This SPI has
12// register outputs controlling the functions that critically
13// affect operation of the picosoc and so cannot be accessed
14// from the picosoc itself. This includes the PLL enables
15// and trim, and the crystal oscillator enable. It also has
16// a general reset for the picosoc, an IRQ input, a bypass for
17// the entire crystal oscillator and PLL chain, the
18// manufacturer and product IDs and product revision number.
19// To be independent of the 1.8V regulator, the slave SPI is
20// synthesized with the 3V digital library and runs off of
21// the 3V supply.
22//
23// This module is designed to be decoupled from the chip
24// padframe and redirected to the wishbone bus under
25// register control from the management SoC, such that the
26// contents can be accessed from the management core via the
27// SPI master.
28//
29//-----------------------------------------------------------
30
31//------------------------------------------------------------
32// Caravel defined registers:
33// Register 0: SPI status and control (unused & reserved)
34// Register 1 and 2: Manufacturer ID (0x0456) (readonly)
Tim Edwards81153202020-10-09 19:57:04 -040035// Register 3: Product ID (= 16) (readonly)
Tim Edwards44bab472020-10-04 22:09:54 -040036// Register 4-7: Mask revision (readonly) --- Externally programmed
37// with via programming. Via programmed with a script to match
38// each customer ID.
39//
Tim Edwards3245e2f2020-10-10 14:02:11 -040040// Register 8: PLL enables (2 bits)
Tim Edwards44bab472020-10-04 22:09:54 -040041// Register 9: PLL bypass (1 bit)
42// Register 10: IRQ (1 bit)
43// Register 11: reset (1 bit)
44// Register 12: trap (1 bit) (readonly)
45// Register 13-16: PLL trim (26 bits)
Tim Edwards3245e2f2020-10-10 14:02:11 -040046// Register 17: PLL output divider (3 bits)
47// Register 18: PLL feedback divider (5 bits)
Tim Edwards44bab472020-10-04 22:09:54 -040048//------------------------------------------------------------
49
50module housekeeping_spi(
51`ifdef LVS
52 vdd, vss,
53`endif
54 RSTB, SCK, SDI, CSB, SDO, sdo_enb,
Tim Edwards3245e2f2020-10-10 14:02:11 -040055 pll_ena, pll_dco_ena, pll_div, pll_sel,
Tim Edwards44bab472020-10-04 22:09:54 -040056 pll_trim, pll_bypass, irq, reset, trap,
57 mask_rev_in, pass_thru_reset,
58 pass_thru_mgmt_sck, pass_thru_mgmt_csb,
59 pass_thru_mgmt_sdi, pass_thru_mgmt_sdo,
60 pass_thru_user_sck, pass_thru_user_csb,
61 pass_thru_user_sdi, pass_thru_user_sdo
62);
63
64`ifdef LVS
65 inout vdd; // 3.3V supply
66 inout vss; // common ground
67`endif
68
69 input RSTB; // from padframe
70
71 input SCK; // from padframe
72 input SDI; // from padframe
73 input CSB; // from padframe
74 output SDO; // to padframe
75 output sdo_enb; // to padframe
76
Tim Edwards3245e2f2020-10-10 14:02:11 -040077 output pll_ena;
Tim Edwards44bab472020-10-04 22:09:54 -040078 output pll_dco_ena;
79 output [4:0] pll_div;
80 output [2:0] pll_sel;
81 output [25:0] pll_trim;
82 output pll_bypass;
83 output irq;
84 output reset;
85 input trap;
86 input [31:0] mask_rev_in; // metal programmed; 3.3V domain
87
88 // Pass-through programming mode for management area SPI flash
89 output pass_thru_reset;
90 output pass_thru_mgmt_sck;
91 output pass_thru_mgmt_csb;
92 output pass_thru_mgmt_sdi;
93 input pass_thru_mgmt_sdo;
94
95 // Pass-through programming mode for user area SPI flash
96 output pass_thru_user_sck;
97 output pass_thru_user_csb;
98 output pass_thru_user_sdi;
99 input pass_thru_user_sdo;
100
101 reg [25:0] pll_trim;
102 reg [4:0] pll_div;
103 reg [2:0] pll_sel;
104 reg pll_dco_ena;
Tim Edwards3245e2f2020-10-10 14:02:11 -0400105 reg pll_ena;
Tim Edwards44bab472020-10-04 22:09:54 -0400106 reg pll_bypass;
107 reg reset_reg;
108 reg irq;
109
110 wire [7:0] odata;
111 wire [7:0] idata;
112 wire [7:0] iaddr;
113
114 wire trap;
115 wire rdstb;
116 wire wrstb;
117 wire pass_thru_mgmt; // Mode detected by spi_slave
118 wire pass_thru_mgmt_delay;
119 wire pass_thru_user; // Mode detected by spi_slave
120 wire pass_thru_user_delay;
Tim Edwards44bab472020-10-04 22:09:54 -0400121 wire loc_sdo;
Tim Edwards44bab472020-10-04 22:09:54 -0400122
Tim Edwards856b0922020-10-09 16:30:22 -0400123 // Pass-through mode handling. Signals may only be applied when the
124 // core processor is in reset.
Tim Edwards44bab472020-10-04 22:09:54 -0400125
Tim Edwards856b0922020-10-09 16:30:22 -0400126 assign pass_thru_mgmt_csb = reset ? ~pass_thru_mgmt_delay : 1'bz;
127 assign pass_thru_mgmt_sck = reset ? (pass_thru_mgmt ? SCK : 1'b0) : 1'bz;
128 assign pass_thru_mgmt_sdi = reset ? (pass_thru_mgmt ? SDI : 1'b0) : 1'bz;
Tim Edwards44bab472020-10-04 22:09:54 -0400129
Tim Edwards856b0922020-10-09 16:30:22 -0400130 assign pass_thru_user_csb = reset ? ~pass_thru_user_delay : 1'bz;
131 assign pass_thru_user_sck = reset ? (pass_thru_user ? SCK : 1'b0) : 1'bz;
132 assign pass_thru_user_sdi = reset ? (pass_thru_user ? SDI : 1'b0) : 1'bz;
Tim Edwards44bab472020-10-04 22:09:54 -0400133
134 assign SDO = pass_thru_mgmt ? pass_thru_mgmt_sdo :
135 pass_thru_user ? pass_thru_user_sdo : loc_sdo;
136 assign reset = pass_thru_reset ? 1'b1 : reset_reg;
137
138 // Instantiate the SPI slave module
139
140 housekeeping_spi_slave U1 (
141 .reset(~RSTB),
Tim Edwardsca2f3182020-10-06 10:05:11 -0400142 .SCK(SCK),
143 .SDI(SDI),
144 .CSB(CSB),
Tim Edwards44bab472020-10-04 22:09:54 -0400145 .SDO(loc_sdo),
Tim Edwardsca2f3182020-10-06 10:05:11 -0400146 .sdoenb(sdo_enb),
Tim Edwards44bab472020-10-04 22:09:54 -0400147 .idata(odata),
148 .odata(idata),
149 .oaddr(iaddr),
150 .rdstb(rdstb),
151 .wrstb(wrstb),
152 .pass_thru_mgmt(pass_thru_mgmt),
153 .pass_thru_mgmt_delay(pass_thru_mgmt_delay),
154 .pass_thru_user(pass_thru_user),
155 .pass_thru_user_delay(pass_thru_user_delay),
156 .pass_thru_reset(pass_thru_reset)
157 );
158
159 wire [11:0] mfgr_id;
160 wire [7:0] prod_id;
161 wire [31:0] mask_rev;
162
163 assign mfgr_id = 12'h456; // Hard-coded
164 assign prod_id = 8'h10; // Hard-coded
165 assign mask_rev = mask_rev_in; // Copy in to out.
166
167 // Send register contents to odata on SPI read command
168 // All values are 1-4 bits and no shadow registers are required.
169
170 assign odata =
171 (iaddr == 8'h00) ? 8'h00 : // SPI status (fixed)
172 (iaddr == 8'h01) ? {4'h0, mfgr_id[11:8]} : // Manufacturer ID (fixed)
173 (iaddr == 8'h02) ? mfgr_id[7:0] : // Manufacturer ID (fixed)
174 (iaddr == 8'h03) ? prod_id : // Product ID (fixed)
175 (iaddr == 8'h04) ? mask_rev[31:24] : // Mask rev (metal programmed)
176 (iaddr == 8'h05) ? mask_rev[23:16] : // Mask rev (metal programmed)
177 (iaddr == 8'h06) ? mask_rev[15:8] : // Mask rev (metal programmed)
178 (iaddr == 8'h07) ? mask_rev[7:0] : // Mask rev (metal programmed)
179
Tim Edwardsef2b68d2020-10-11 17:00:44 -0400180 (iaddr == 8'h08) ? {6'b000000, pll_dco_ena, pll_ena} :
Tim Edwards44bab472020-10-04 22:09:54 -0400181 (iaddr == 8'h09) ? {7'b0000000, pll_bypass} :
182 (iaddr == 8'h0a) ? {7'b0000000, irq} :
183 (iaddr == 8'h0b) ? {7'b0000000, reset} :
184 (iaddr == 8'h0c) ? {7'b0000000, trap} :
185 (iaddr == 8'h0d) ? pll_trim[7:0] :
186 (iaddr == 8'h0e) ? pll_trim[15:8] :
187 (iaddr == 8'h0f) ? pll_trim[23:16] :
188 (iaddr == 8'h10) ? {6'b000000, pll_trim[25:24]} :
189 (iaddr == 8'h11) ? {5'b00000, pll_sel} :
190 (iaddr == 8'h12) ? {3'b000, pll_div} :
191 8'h00; // Default
192
193 // Register mapping and I/O to slave module
194
195 always @(posedge SCK or negedge RSTB) begin
196 if (RSTB == 1'b0) begin
197 // Set trim for PLL at (almost) slowest rate (~90MHz). However,
198 // pll_trim[12] must be set to zero for proper startup.
199 pll_trim <= 26'b11111111111110111111111111;
Tim Edwards3245e2f2020-10-10 14:02:11 -0400200 pll_sel <= 3'b010; // Default output divider divide-by-2
201 pll_div <= 5'b00100; // Default feedback divider divide-by-8
Tim Edwards44bab472020-10-04 22:09:54 -0400202 pll_dco_ena <= 1'b1; // Default free-running PLL
Tim Edwards3245e2f2020-10-10 14:02:11 -0400203 pll_ena <= 1'b0; // Default PLL turned off
204 pll_bypass <= 1'b1; // Default bypass mode (don't use PLL)
Tim Edwards44bab472020-10-04 22:09:54 -0400205 irq <= 1'b0;
206 reset_reg <= 1'b0;
207 end else if (wrstb == 1'b1) begin
208 case (iaddr)
209 8'h08: begin
Tim Edwards3245e2f2020-10-10 14:02:11 -0400210 pll_ena <= idata[0];
211 pll_dco_ena <= idata[1];
Tim Edwards44bab472020-10-04 22:09:54 -0400212 end
213 8'h09: begin
214 pll_bypass <= idata[0];
215 end
216 8'h0a: begin
217 irq <= idata[0];
218 end
219 8'h0b: begin
220 reset_reg <= idata[0];
221 end
222 // Register 0xc is read-only
223 8'h0d: begin
224 pll_trim[7:0] <= idata;
225 end
226 8'h0e: begin
227 pll_trim[15:8] <= idata;
228 end
229 8'h0f: begin
230 pll_trim[23:16] <= idata;
231 end
232 8'h10: begin
233 pll_trim[25:24] <= idata[1:0];
234 end
235 8'h11: begin
236 pll_sel <= idata[2:0];
237 end
238 8'h12: begin
239 pll_div <= idata[4:0];
240 end
241 endcase // (iaddr)
242 end
243 end
244endmodule // housekeeping_spi
245
246//------------------------------------------------------
247// housekeeping_spi_slave.v
248//------------------------------------------------------
249// General purpose SPI slave module for the Caravel chip
250//------------------------------------------------------
251// Written by Tim Edwards
252// efabless, inc., September 28, 2020
253//------------------------------------------------
254// This file is distributed free and open source
255//------------------------------------------------
256
257// SCK --- Clock input
258// SDI --- Data input
259// SDO --- Data output
260// CSB --- Chip select (sense negative)
261// idata --- Data from chip to transmit out, in 8 bits
262// odata --- Input data to chip, in 8 bits
263// addr --- Decoded address to upstream circuits
264// rdstb --- Read strobe, tells upstream circuit to supply next byte to idata
265// wrstb --- Write strobe, tells upstream circuit to latch odata.
266
267// Data format (general purpose):
268// 8 bit format
269// 1st byte: Command word (see below)
270// 2nd byte: Address word (register 0 to 255)
271// 3rd byte: Data word (value 0 to 255)
272
273// Command format:
274// 00000000 No operation
275// 10000000 Write until CSB raised
276// 01000000 Read until CSB raised
277// 11000000 Simultaneous read/write until CSB raised
278// 11000100 Pass-through read/write to management area flash SPI until CSB raised
Tim Edwardsb78e1c12020-10-09 11:51:48 -0400279// 11000010 Pass-through read/write to user area flash SPI until CSB raised
Tim Edwards44bab472020-10-04 22:09:54 -0400280// wrnnn000 Read/write as above, for nnn = 1 to 7 bytes, then terminate
281
282// Lower three bits are reserved for future use.
283// All serial bytes are read and written msb first.
284
285// Fixed control and status registers
286
287// Address 0 is reserved and contains flags for SPI mode. This is
288// currently undefined and is always value 0.
289// Address 1 is reserved and contains manufacturer ID low 8 bits.
290// Address 2 is reserved and contains manufacturer ID high 4 bits.
291// Address 3 is reserved and contains product ID (8 bits).
292// Addresses 4 to 7 are reserved and contain the mask ID (32 bits).
293// Addresses 8 to 255 are available for general purpose use.
294
295`define COMMAND 3'b000
296`define ADDRESS 3'b001
297`define DATA 3'b010
298`define USERPASS 3'b100
299`define MGMTPASS 3'b101
300
301module housekeeping_spi_slave(reset, SCK, SDI, CSB, SDO,
302 sdoenb, idata, odata, oaddr, rdstb, wrstb,
303 pass_thru_mgmt, pass_thru_mgmt_delay,
304 pass_thru_user, pass_thru_user_delay, pass_thru_reset);
305
306 input reset;
307 input SCK;
308 input SDI;
309 input CSB;
310 output SDO;
311 output sdoenb;
312 input [7:0] idata;
313 output [7:0] odata;
314 output [7:0] oaddr;
315 output rdstb;
316 output wrstb;
317 output pass_thru_mgmt;
318 output pass_thru_mgmt_delay;
319 output pass_thru_user;
320 output pass_thru_user_delay;
321 output pass_thru_reset;
322
323 reg [7:0] addr;
324 reg wrstb;
325 reg rdstb;
326 reg sdoenb;
327 reg [2:0] state;
328 reg [2:0] count;
329 reg writemode;
330 reg readmode;
331 reg [2:0] fixed;
332 wire [7:0] odata;
333 reg [6:0] predata;
334 wire [7:0] oaddr;
335 reg [7:0] ldata;
336 reg pass_thru_mgmt;
337 reg pass_thru_mgmt_delay;
338 reg pre_pass_thru_mgmt;
339 reg pass_thru_user;
340 reg pass_thru_user_delay;
341 reg pre_pass_thru_user;
342 wire csb_reset;
343
344 assign odata = {predata, SDI};
345 assign oaddr = (state == `ADDRESS) ? {addr[6:0], SDI} : addr;
346 assign SDO = ldata[7];
347 assign csb_reset = CSB | reset;
348 assign pass_thru_reset = pass_thru_mgmt_delay | pre_pass_thru_mgmt;
349
350 // Readback data is captured on the falling edge of SCK so that
351 // it is guaranteed valid at the next rising edge.
352 always @(negedge SCK or posedge csb_reset) begin
353 if (csb_reset == 1'b1) begin
354 wrstb <= 1'b0;
355 ldata <= 8'b00000000;
356 sdoenb <= 1'b1;
357 end else begin
358
359 // After CSB low, 1st SCK starts command
360
361 if (state == `DATA) begin
362 if (readmode == 1'b1) begin
363 sdoenb <= 1'b0;
364 if (count == 3'b000) begin
365 ldata <= idata;
366 end else begin
367 ldata <= {ldata[6:0], 1'b0}; // Shift out
368 end
369 end else begin
370 sdoenb <= 1'b1;
371 end
372
373 // Apply write strobe on SCK negative edge on the next-to-last
374 // data bit so that it updates data on the rising edge of SCK
375 // on the last data bit.
376
377 if (count == 3'b111) begin
378 if (writemode == 1'b1) begin
379 wrstb <= 1'b1;
380 end
381 end else begin
382 wrstb <= 1'b0;
383 end
384 end else if (state == `MGMTPASS || state == `USERPASS) begin
385 wrstb <= 1'b0;
386 sdoenb <= 1'b0;
387 end else begin
388 wrstb <= 1'b0;
389 sdoenb <= 1'b1;
390 end // ! state `DATA
391 end // ! csb_reset
392 end // always @ ~SCK
393
394 always @(posedge SCK or posedge csb_reset) begin
395 if (csb_reset == 1'b1) begin
396 // Default state on reset
397 addr <= 8'h00;
398 rdstb <= 1'b0;
399 predata <= 7'b0000000;
400 state <= `COMMAND;
401 count <= 3'b000;
402 readmode <= 1'b0;
403 writemode <= 1'b0;
404 fixed <= 3'b000;
405 pass_thru_mgmt <= 1'b0;
406 pass_thru_mgmt_delay <= 1'b0;
407 pre_pass_thru_mgmt <= 1'b0;
408 pass_thru_user = 1'b0;
409 pass_thru_user_delay <= 1'b0;
410 pre_pass_thru_user <= 1'b0;
411 end else begin
412 // After csb_reset low, 1st SCK starts command
413 if (state == `COMMAND) begin
414 rdstb <= 1'b0;
415 count <= count + 1;
416 if (count == 3'b000) begin
417 writemode <= SDI;
418 end else if (count == 3'b001) begin
419 readmode <= SDI;
420 end else if (count < 3'b101) begin
421 fixed <= {fixed[1:0], SDI};
Tim Edwardsb78e1c12020-10-09 11:51:48 -0400422 end else if (count == 3'b101) begin
423 pre_pass_thru_mgmt <= SDI;
424 end else if (count == 3'b110) begin
425 pre_pass_thru_user <= SDI;
Tim Edwards44bab472020-10-04 22:09:54 -0400426 pass_thru_mgmt_delay <= pre_pass_thru_mgmt;
Tim Edwards44bab472020-10-04 22:09:54 -0400427 end else if (count == 3'b111) begin
Tim Edwardsb78e1c12020-10-09 11:51:48 -0400428 pass_thru_user_delay <= pre_pass_thru_user;
Tim Edwards44bab472020-10-04 22:09:54 -0400429 if (pre_pass_thru_mgmt == 1'b1) begin
430 state <= `MGMTPASS;
431 pre_pass_thru_mgmt <= 1'b0;
432 end else if (pre_pass_thru_user == 1'b1) begin
433 state <= `USERPASS;
434 pre_pass_thru_user <= 1'b0;
435 end else begin
436 state <= `ADDRESS;
437 end
438 end
439 end else if (state == `ADDRESS) begin
440 count <= count + 1;
441 addr <= {addr[6:0], SDI};
442 if (count == 3'b111) begin
443 if (readmode == 1'b1) begin
444 rdstb <= 1'b1;
445 end
446 state <= `DATA;
447 end else begin
448 rdstb <= 1'b0;
449 end
450 end else if (state == `DATA) begin
451 predata <= {predata[6:0], SDI};
452 count <= count + 1;
453 if (count == 3'b111) begin
454 if (fixed == 3'b001) begin
455 state <= `COMMAND;
456 end else if (fixed != 3'b000) begin
457 fixed <= fixed - 1;
458 addr <= addr + 1; // Auto increment address (fixed)
459 end else begin
460 addr <= addr + 1; // Auto increment address (streaming)
461 end
462 end else begin
463 rdstb <= 1'b0;
464 end
Tim Edwardsb78e1c12020-10-09 11:51:48 -0400465 end else if (state == `MGMTPASS) begin
466 pass_thru_mgmt <= 1'b1;
467 end else if (state == `USERPASS) begin
468 pass_thru_user <= 1'b1;
469 end // ! state `DATA | `MGMTPASS | `USERPASS
Tim Edwards44bab472020-10-04 22:09:54 -0400470 end // ! csb_reset
471 end // always @ SCK
472
473endmodule // housekeeping_spi_slave