top.v 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * top.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. //`define NO_PLL
  35. module top (
  36. // nano-PMOD
  37. output wire clk_lp,
  38. output wire clk_hs_p,
  39. output wire clk_hs_n,
  40. output wire dat_lp,
  41. output wire dat_hs_p,
  42. output wire dat_hs_n,
  43. output wire lcd_reset_n,
  44. output wire bl_pwm,
  45. // SPI
  46. input wire spi_mosi,
  47. output wire spi_miso,
  48. input wire spi_cs_n,
  49. input wire spi_clk,
  50. // LED
  51. output wire [2:0] rgb,
  52. // Clock
  53. input wire clk_12m
  54. );
  55. localparam integer AWIDTH = 10;
  56. // Signals
  57. // -------
  58. // SPI 'simple-bus'
  59. wire [7:0] sb_addr;
  60. wire [7:0] sb_data;
  61. wire sb_first;
  62. wire sb_last;
  63. wire sb_stb;
  64. // SPI Packets
  65. wire [7:0] spf_wr_data;
  66. wire spf_wr_last;
  67. wire spf_wr_ena;
  68. wire spf_full;
  69. wire [7:0] spf_rd_data;
  70. wire spf_rd_last;
  71. wire spf_rd_ena;
  72. wire spf_empty;
  73. // MIPI
  74. wire [7:0] cfg_dsi_hs_prep;
  75. wire [7:0] cfg_dsi_hs_zero;
  76. wire [7:0] cfg_dsi_hs_trail;
  77. wire hs_clk_req;
  78. wire hs_clk_rdy;
  79. wire hs_clk_sync;
  80. wire hs_start;
  81. wire [7:0] hs_data;
  82. wire hs_last;
  83. wire hs_ack;
  84. wire hs_rdy;
  85. // LCD Control
  86. wire [15:0] cfg_lcd_csr;
  87. wire bl_pwm_i;
  88. // LED debug
  89. wire [2:0] rgb_pwm;
  90. // Clock / Reset logic
  91. `ifdef NO_PLL
  92. reg [7:0] rst_cnt = 8'h00;
  93. wire rst_i;
  94. `endif
  95. wire clk;
  96. wire rst;
  97. // Slave SPI interface
  98. // -------------------
  99. `ifdef SPI_FAST
  100. spi_fast spi_I (
  101. `else
  102. spi_simple spi_I (
  103. `endif
  104. .spi_mosi(spi_mosi),
  105. .spi_miso(spi_miso),
  106. .spi_cs_n(spi_cs_n),
  107. .spi_clk(spi_clk),
  108. .addr(sb_addr),
  109. .data(sb_data),
  110. .first(sb_first),
  111. .last(sb_last),
  112. .strobe(sb_stb),
  113. .clk(clk),
  114. .rst(rst)
  115. );
  116. // Packet handling
  117. // ---------------
  118. // SPI Packet writer
  119. pkt_spi_write #(
  120. .BASE(8'h20)
  121. ) write_I (
  122. .sb_addr(sb_addr),
  123. .sb_data(sb_data),
  124. .sb_first(sb_first),
  125. .sb_last(sb_last),
  126. .sb_strobe(sb_stb),
  127. .fifo_data(spf_wr_data),
  128. .fifo_last(spf_wr_last),
  129. .fifo_wren(spf_wr_ena),
  130. .fifo_full(spf_full),
  131. .clk(clk),
  132. .rst(rst)
  133. );
  134. // SPI packet FIFO
  135. pkt_fifo #(
  136. .AWIDTH(AWIDTH)
  137. ) spi_packet_fifo_I (
  138. .wr_data(spf_wr_data),
  139. .wr_last(spf_wr_last),
  140. .wr_ena(spf_wr_ena),
  141. .full(spf_full),
  142. .rd_data(spf_rd_data),
  143. .rd_last(spf_rd_last),
  144. .rd_ena(spf_rd_ena),
  145. .empty(spf_empty),
  146. .clk(clk),
  147. .rst(rst)
  148. );
  149. // Packet reader
  150. reg reading;
  151. assign hs_start = ~spf_empty & ~reading;
  152. assign hs_data = spf_rd_data;
  153. assign hs_last = spf_rd_last;
  154. assign spf_rd_ena = hs_ack;
  155. always @(posedge clk or posedge rst)
  156. if (rst)
  157. reading <= 1'b0;
  158. else
  159. reading <= (reading | hs_start) & ~(hs_last & hs_ack);
  160. // MIPI-DSI
  161. // --------
  162. // Config registers
  163. spi_reg #(
  164. .ADDR(8'h10),
  165. .BYTES(1)
  166. ) reg_dsi_hs_prep_I (
  167. .addr(sb_addr),
  168. .data(sb_data),
  169. .first(sb_first),
  170. .strobe(sb_stb),
  171. .rst_val(8'h04),
  172. .out_val(cfg_dsi_hs_prep),
  173. .out_stb(),
  174. .clk(clk),
  175. .rst(rst)
  176. );
  177. spi_reg #(
  178. .ADDR(8'h11),
  179. .BYTES(1)
  180. ) reg_dsi_hs_zero_I (
  181. .addr(sb_addr),
  182. .data(sb_data),
  183. .first(sb_first),
  184. .strobe(sb_stb),
  185. .rst_val(8'h04),
  186. .out_val(cfg_dsi_hs_zero),
  187. .out_stb(),
  188. .clk(clk),
  189. .rst(rst)
  190. );
  191. spi_reg #(
  192. .ADDR(8'h12),
  193. .BYTES(1)
  194. ) reg_dsi_hs_trail_I (
  195. .addr(sb_addr),
  196. .data(sb_data),
  197. .first(sb_first),
  198. .strobe(sb_stb),
  199. .rst_val(8'h04),
  200. .out_val(cfg_dsi_hs_trail),
  201. .out_stb(),
  202. .clk(clk),
  203. .rst(rst)
  204. );
  205. // MIPI DSI cores
  206. nano_dsi_clk dsi_clk_I (
  207. .clk_lp(clk_lp),
  208. .clk_hs_p(clk_hs_p),
  209. .clk_hs_n(clk_hs_n),
  210. .hs_req(hs_clk_req),
  211. .hs_rdy(hs_clk_rdy),
  212. .clk_sync(hs_clk_sync),
  213. .cfg_hs_prep(cfg_dsi_hs_prep),
  214. .cfg_hs_zero(cfg_dsi_hs_zero),
  215. .cfg_hs_trail(cfg_dsi_hs_trail),
  216. .clk(clk),
  217. .rst(rst)
  218. );
  219. nano_dsi_data dsi_data_I (
  220. .data_lp(dat_lp),
  221. .data_hs_p(dat_hs_p),
  222. .data_hs_n(dat_hs_n),
  223. .hs_start(hs_start),
  224. .hs_data(hs_data),
  225. .hs_last(hs_last),
  226. .hs_ack(hs_ack),
  227. .hs_rdy(hs_rdy),
  228. .clk_sync(hs_clk_sync),
  229. .cfg_hs_prep(cfg_dsi_hs_prep),
  230. .cfg_hs_zero(cfg_dsi_hs_zero),
  231. .cfg_hs_trail(cfg_dsi_hs_trail),
  232. .clk(clk),
  233. .rst(rst)
  234. );
  235. // LCD misc
  236. // --------
  237. // Config registers
  238. spi_reg #(
  239. .ADDR(8'h00),
  240. .BYTES(2)
  241. ) reg_lcd_csr_I (
  242. .addr(sb_addr),
  243. .data(sb_data),
  244. .first(sb_first),
  245. .strobe(sb_stb),
  246. .rst_val(16'h000f),
  247. .out_val(cfg_lcd_csr),
  248. .out_stb(),
  249. .clk(clk),
  250. .rst(rst)
  251. );
  252. // Back Light PWM
  253. pwm #(
  254. .WIDTH(10)
  255. ) bl_pwm_I (
  256. .pwm(bl_pwm_i),
  257. .cfg_val(cfg_lcd_csr[9:0]),
  258. .clk(clk),
  259. .rst(rst)
  260. );
  261. assign bl_pwm = bl_pwm_i;
  262. // Reset
  263. assign lcd_reset_n = cfg_lcd_csr[15] ? 1'b0 : 1'bz;
  264. // HS clock enable
  265. assign hs_clk_req = cfg_lcd_csr[14];
  266. // LED debug
  267. // ---------
  268. //
  269. assign rgb_pwm[0] = bl_pwm_i;
  270. assign rgb_pwm[1] = ~spf_empty;
  271. assign rgb_pwm[2] = hs_clk_rdy;
  272. // Driver
  273. SB_RGBA_DRV #(
  274. .CURRENT_MODE("0b1"),
  275. .RGB0_CURRENT("0b000001"),
  276. .RGB1_CURRENT("0b000001"),
  277. .RGB2_CURRENT("0b000001")
  278. ) rgb_drv_I (
  279. .RGBLEDEN(1'b1),
  280. .RGB0PWM(rgb_pwm[0]),
  281. .RGB1PWM(rgb_pwm[1]),
  282. .RGB2PWM(rgb_pwm[2]),
  283. .CURREN(1'b1),
  284. .RGB0(rgb[0]),
  285. .RGB1(rgb[1]),
  286. .RGB2(rgb[2])
  287. );
  288. // Clock / Reset
  289. // -------------
  290. `ifdef NO_PLL
  291. always @(posedge clk)
  292. if (~rst_cnt[7])
  293. rst_cnt <= rst_cnt + 1;
  294. wire rst_i = ~rst_cnt[7];
  295. SB_GB clk_gbuf_I (
  296. .USER_SIGNAL_TO_GLOBAL_BUFFER(clk_12m),
  297. .GLOBAL_BUFFER_OUTPUT(clk)
  298. );
  299. SB_GB rst_gbuf_I (
  300. .USER_SIGNAL_TO_GLOBAL_BUFFER(rst_i),
  301. .GLOBAL_BUFFER_OUTPUT(rst)
  302. );
  303. `else
  304. sysmgr sys_mgr_I (
  305. .clk_in(clk_12m),
  306. .rst_in(1'b0),
  307. .clk_out(clk),
  308. .rst_out(rst)
  309. );
  310. `endif
  311. endmodule // top