soc.vhdl 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.math_real.all;
  5. use std.textio.all;
  6. use std.env.stop;
  7. library work;
  8. use work.common.all;
  9. use work.utils.all;
  10. use work.wishbone_types.all;
  11. -- Memory map. *** Keep include/microwatt_soc.h updated on changes ***
  12. --
  13. -- Main bus:
  14. -- 0x00000000: Block RAM (MEMORY_SIZE) or DRAM depending on syscon
  15. -- 0x40000000: DRAM (when present)
  16. -- 0x80000000: Block RAM (aliased & repeated)
  17. -- IO Bus:
  18. -- 0xc0000000: SYSCON
  19. -- 0xc0002000: UART0
  20. -- 0xc0003000: UART1 (if any)
  21. -- 0xc0004000: XICS ICP
  22. -- 0xc0005000: XICS ICS
  23. -- 0xc0006000: SPI Flash controller
  24. -- 0xc8nnnnnn: External IO bus
  25. -- 0xf0000000: Flash "ROM" mapping
  26. -- 0xff000000: DRAM init code (if any) or flash ROM (**)
  27. -- External IO bus:
  28. -- 0xc8000000: LiteDRAM control (CSRs)
  29. -- 0xc8020000: LiteEth CSRs (*)
  30. -- 0xc8030000: LiteEth MMIO (*)
  31. -- (*) LiteEth must be a single aligned 32KB block as the CSRs and MMIOs
  32. -- are actually decoded as a single wishbone which LiteEth will
  33. -- internally split based on bit 16.
  34. -- (**) DRAM init code is currently special and goes to the external
  35. -- IO bus, this will be fixed when it's moved out of litedram and
  36. -- into the main SoC once we have a common "firmware".
  37. -- Interrupt numbers:
  38. --
  39. -- 0 : UART0
  40. -- 1 : Ethernet
  41. entity soc is
  42. generic (
  43. MEMORY_SIZE : natural;
  44. RAM_INIT_FILE : string;
  45. CLK_FREQ : positive;
  46. SIM : boolean;
  47. HAS_FPU : boolean := true;
  48. HAS_BTC : boolean := true;
  49. DISABLE_FLATTEN_CORE : boolean := false;
  50. EXTERNAL_CORE : boolean := false;
  51. HAS_DRAM : boolean := false;
  52. SIM_MAIN_BRAM : boolean := false;
  53. SIM_BRAM_CHAINBOOT : positive := 0;
  54. DRAM_SIZE : integer := 0;
  55. RESET_ADDRESS : std_ulogic_vector(63 downto 0) := (others => '0');
  56. -- hack to jump-start alternative (e.g. verilator-loaded linux kernel)
  57. -- RESET_ADDRESS : std_ulogic_vector(63 downto 0) := (22 downto 21 => '1', others => '0');
  58. DRAM_INIT_SIZE : integer := 0;
  59. HAS_SPI_FLASH : boolean := false;
  60. SPI_FLASH_DLINES : positive := 1;
  61. SPI_FLASH_OFFSET : integer := 0;
  62. SPI_FLASH_DEF_CKDV : natural := 2;
  63. SPI_FLASH_DEF_QUAD : boolean := false;
  64. LOG_LENGTH : natural := 512;
  65. HAS_LITEETH : boolean := false;
  66. UART0_IS_16550 : boolean := true;
  67. HAS_UART1 : boolean := false
  68. );
  69. port(
  70. rst : in std_ulogic;
  71. system_clk : in std_ulogic;
  72. -- "Large" (64-bit) DRAM wishbone
  73. wb_dram_in : out wishbone_master_out;
  74. wb_dram_out : in wishbone_slave_out := wishbone_slave_out_init;
  75. -- "Small" (32-bit) external IO wishbone
  76. wb_ext_io_in : out wb_io_master_out;
  77. wb_ext_io_out : in wb_io_slave_out := wb_io_slave_out_init;
  78. wb_ext_is_dram_csr : out std_ulogic;
  79. wb_ext_is_dram_init : out std_ulogic;
  80. wb_ext_is_eth : out std_ulogic;
  81. -- External interrupts
  82. ext_irq_eth : in std_ulogic := '0';
  83. -- BRAM verilator access
  84. bram_we : out std_ulogic;
  85. bram_re : out std_ulogic;
  86. bram_addr : out std_logic_vector(log2ceil(MEMORY_SIZE) - 3- 1 downto 0);
  87. bram_di : out std_logic_vector(63 downto 0);
  88. bram_do : in std_logic_vector(63 downto 0);
  89. bram_sel : out std_logic_vector(7 downto 0);
  90. -- UART0 signals:
  91. uart0_txd : out std_ulogic;
  92. uart0_rxd : in std_ulogic := '0';
  93. -- UART1 signals:
  94. uart1_txd : out std_ulogic;
  95. uart1_rxd : in std_ulogic := '0';
  96. -- SPI Flash signals
  97. spi_flash_sck : out std_ulogic;
  98. spi_flash_cs_n : out std_ulogic;
  99. spi_flash_sdat_o : out std_ulogic_vector(SPI_FLASH_DLINES-1 downto 0);
  100. spi_flash_sdat_oe : out std_ulogic_vector(SPI_FLASH_DLINES-1 downto 0);
  101. spi_flash_sdat_i : in std_ulogic_vector(SPI_FLASH_DLINES-1 downto 0) := (others => '1');
  102. -- DRAM controller signals
  103. alt_reset : in std_ulogic := '0';
  104. -- for verilator debugging
  105. nia_req: out std_ulogic;
  106. nia: out std_ulogic_vector(63 downto 0);
  107. msr_o: out std_ulogic_vector(63 downto 0);
  108. insn: out std_ulogic_vector(31 downto 0);
  109. ldst_req: out std_ulogic;
  110. ldst_addr: out std_ulogic_vector(63 downto 0)
  111. );
  112. end entity soc;
  113. architecture behaviour of soc is
  114. -- Wishbone master signals:
  115. signal wishbone_dcore_in : wishbone_slave_out;
  116. signal wishbone_dcore_out : wishbone_master_out;
  117. signal wishbone_icore_in : wishbone_slave_out;
  118. signal wishbone_icore_out : wishbone_master_out;
  119. signal wishbone_debug_in : wishbone_slave_out;
  120. signal wishbone_debug_out : wishbone_master_out;
  121. -- Arbiter array (ghdl doesnt' support assigning the array
  122. -- elements in the entity instantiation)
  123. constant NUM_WB_MASTERS : positive := 3;
  124. signal wb_masters_out : wishbone_master_out_vector(0 to NUM_WB_MASTERS-1);
  125. signal wb_masters_in : wishbone_slave_out_vector(0 to NUM_WB_MASTERS-1);
  126. -- Wishbone master (output of arbiter):
  127. signal wb_master_in : wishbone_slave_out;
  128. signal wb_master_out : wishbone_master_out;
  129. -- Main "IO" bus, from main slave decoder to the latch
  130. signal wb_io_in : wishbone_master_out;
  131. signal wb_io_out : wishbone_slave_out;
  132. -- Secondary (smaller) IO bus after the IO bus latch
  133. signal wb_sio_out : wb_io_master_out;
  134. signal wb_sio_in : wb_io_slave_out;
  135. -- Syscon signals
  136. signal dram_at_0 : std_ulogic;
  137. signal do_core_reset : std_ulogic;
  138. signal wb_syscon_in : wb_io_master_out;
  139. signal wb_syscon_out : wb_io_slave_out;
  140. -- UART0 signals:
  141. signal wb_uart0_in : wb_io_master_out;
  142. signal wb_uart0_out : wb_io_slave_out;
  143. signal uart0_dat8 : std_ulogic_vector(7 downto 0);
  144. signal uart0_irq : std_ulogic;
  145. -- UART1 signals:
  146. signal wb_uart1_in : wb_io_master_out;
  147. signal wb_uart1_out : wb_io_slave_out;
  148. signal uart1_dat8 : std_ulogic_vector(7 downto 0);
  149. signal uart1_irq : std_ulogic;
  150. -- SPI Flash controller signals:
  151. signal wb_spiflash_in : wb_io_master_out;
  152. signal wb_spiflash_out : wb_io_slave_out;
  153. signal wb_spiflash_is_reg : std_ulogic;
  154. signal wb_spiflash_is_map : std_ulogic;
  155. -- XICS signals:
  156. signal wb_xics_icp_in : wb_io_master_out;
  157. signal wb_xics_icp_out : wb_io_slave_out;
  158. signal wb_xics_ics_in : wb_io_master_out;
  159. signal wb_xics_ics_out : wb_io_slave_out;
  160. signal int_level_in : std_ulogic_vector(15 downto 0);
  161. signal ics_to_icp : ics_to_icp_t;
  162. signal core_ext_irq : std_ulogic;
  163. -- Main memory signals:
  164. signal wb_bram_in : wishbone_master_out;
  165. signal wb_bram_out : wishbone_slave_out;
  166. -- DMI debug bus signals
  167. signal dmi_addr : std_ulogic_vector(7 downto 0);
  168. signal dmi_din : std_ulogic_vector(63 downto 0);
  169. signal dmi_dout : std_ulogic_vector(63 downto 0);
  170. signal dmi_req : std_ulogic;
  171. signal dmi_wr : std_ulogic;
  172. signal dmi_ack : std_ulogic;
  173. -- Per slave DMI signals
  174. signal dmi_wb_dout : std_ulogic_vector(63 downto 0);
  175. signal dmi_wb_req : std_ulogic;
  176. signal dmi_wb_ack : std_ulogic;
  177. signal dmi_core_dout : std_ulogic_vector(63 downto 0);
  178. signal dmi_core_req : std_ulogic;
  179. signal dmi_core_ack : std_ulogic;
  180. -- Delayed/latched resets and alt_reset
  181. signal rst_core : std_ulogic := '1';
  182. signal rst_uart : std_ulogic := '1';
  183. signal rst_xics : std_ulogic := '1';
  184. signal rst_spi : std_ulogic := '1';
  185. signal rst_bram : std_ulogic := '1';
  186. signal rst_dtm : std_ulogic := '1';
  187. signal rst_wbar : std_ulogic := '1';
  188. signal rst_wbdb : std_ulogic := '1';
  189. signal alt_reset_d : std_ulogic;
  190. -- IO branch split:
  191. type slave_io_type is (SLAVE_IO_SYSCON,
  192. SLAVE_IO_UART,
  193. SLAVE_IO_ICP,
  194. SLAVE_IO_ICS,
  195. SLAVE_IO_UART1,
  196. SLAVE_IO_SPI_FLASH_REG,
  197. SLAVE_IO_SPI_FLASH_MAP,
  198. SLAVE_IO_EXTERNAL,
  199. SLAVE_IO_NONE);
  200. signal slave_io_dbg : slave_io_type;
  201. -- This is the component exported by the 16550 compatible
  202. -- UART from FuseSoC.
  203. --
  204. component uart_top port (
  205. wb_clk_i : in std_ulogic;
  206. wb_rst_i : in std_ulogic;
  207. wb_adr_i : in std_ulogic_vector(2 downto 0);
  208. wb_dat_i : in std_ulogic_vector(7 downto 0);
  209. wb_dat_o : out std_ulogic_vector(7 downto 0);
  210. wb_we_i : in std_ulogic;
  211. wb_stb_i : in std_ulogic;
  212. wb_cyc_i : in std_ulogic;
  213. wb_ack_o : out std_ulogic;
  214. int_o : out std_ulogic;
  215. stx_pad_o : out std_ulogic;
  216. srx_pad_i : in std_ulogic;
  217. rts_pad_o : out std_ulogic;
  218. cts_pad_i : in std_ulogic;
  219. dtr_pad_o : out std_ulogic;
  220. dsr_pad_i : in std_ulogic;
  221. ri_pad_i : in std_ulogic;
  222. dcd_pad_i : in std_ulogic
  223. );
  224. end component;
  225. -- use this for external processor core (e.g. mycore.v, not core.vhdl)
  226. component external_core_top port (
  227. clk : in std_ulogic;
  228. rst : in std_ulogic;
  229. alt_reset : in std_ulogic;
  230. wishbone_insn_in : in wishbone_slave_out;
  231. wishbone_insn_out : out wishbone_master_out;
  232. wishbone_data_in : in wishbone_slave_out;
  233. wishbone_data_out : out wishbone_master_out;
  234. dmi_addr : in std_ulogic_vector(3 downto 0) := (others => 'U');
  235. dmi_din : in std_ulogic_vector(63 downto 0) := (others => 'U');
  236. dmi_dout : out std_ulogic_vector(63 downto 0) := (others => 'U');
  237. dmi_req : in std_ulogic := 'U';
  238. dmi_wr : in std_ulogic := 'U';
  239. dmi_ack : out std_ulogic := 'U';
  240. ext_irq : in std_ulogic;
  241. terminated_out : out std_logic := 'U';
  242. -- for verilator debugging
  243. nia_req: out std_ulogic := 'U';
  244. msr_o: out std_ulogic_vector(63 downto 0) := (others => 'U');
  245. nia: out std_ulogic_vector(63 downto 0) := (others => 'U');
  246. insn: out std_ulogic_vector(31 downto 0) := (others => 'U');
  247. ldst_req: out std_ulogic := 'U';
  248. ldst_addr: out std_ulogic_vector(63 downto 0) := (others => 'U')
  249. );
  250. end component;
  251. begin
  252. resets: process(system_clk)
  253. begin
  254. if rising_edge(system_clk) then
  255. rst_core <= rst or do_core_reset;
  256. rst_uart <= rst;
  257. rst_spi <= rst;
  258. rst_xics <= rst;
  259. rst_bram <= rst;
  260. rst_dtm <= rst;
  261. rst_wbar <= rst;
  262. rst_wbdb <= rst;
  263. alt_reset_d <= alt_reset;
  264. end if;
  265. end process;
  266. -- Processor core
  267. processor_internal: if not EXTERNAL_CORE generate
  268. processor: entity work.core
  269. generic map(
  270. SIM => SIM,
  271. HAS_FPU => HAS_FPU,
  272. HAS_BTC => HAS_BTC,
  273. DISABLE_FLATTEN => DISABLE_FLATTEN_CORE,
  274. RESET_ADDRESS => RESET_ADDRESS,
  275. ALT_RESET_ADDRESS => (23 downto 0 => '0', others => '1'),
  276. LOG_LENGTH => LOG_LENGTH
  277. )
  278. port map(
  279. clk => system_clk,
  280. rst => rst_core,
  281. alt_reset => alt_reset_d,
  282. wishbone_insn_in => wishbone_icore_in,
  283. wishbone_insn_out => wishbone_icore_out,
  284. wishbone_data_in => wishbone_dcore_in,
  285. wishbone_data_out => wishbone_dcore_out,
  286. dmi_addr => dmi_addr(3 downto 0),
  287. dmi_dout => dmi_core_dout,
  288. dmi_din => dmi_dout,
  289. dmi_wr => dmi_wr,
  290. dmi_ack => dmi_core_ack,
  291. dmi_req => dmi_core_req,
  292. ext_irq => core_ext_irq,
  293. nia_req => nia_req,
  294. nia => nia,
  295. msr_o => msr_o,
  296. insn => insn,
  297. ldst_req => ldst_req,
  298. ldst_addr => ldst_addr
  299. );
  300. end generate;
  301. processor_external: if EXTERNAL_CORE generate
  302. processor: external_core_top
  303. port map(
  304. clk => system_clk,
  305. rst => rst_core,
  306. alt_reset => alt_reset_d,
  307. wishbone_insn_in => wishbone_icore_in,
  308. wishbone_insn_out => wishbone_icore_out,
  309. wishbone_data_in => wishbone_dcore_in,
  310. wishbone_data_out => wishbone_dcore_out,
  311. dmi_addr => dmi_addr(3 downto 0),
  312. dmi_dout => dmi_core_dout,
  313. dmi_din => dmi_dout,
  314. dmi_wr => dmi_wr,
  315. dmi_ack => dmi_core_ack,
  316. dmi_req => dmi_core_req,
  317. ext_irq => core_ext_irq,
  318. nia_req => nia_req,
  319. nia => nia,
  320. msr_o => msr_o,
  321. insn => insn
  322. );
  323. end generate;
  324. -- Wishbone bus master arbiter & mux
  325. wb_masters_out <= (0 => wishbone_dcore_out,
  326. 1 => wishbone_icore_out,
  327. 2 => wishbone_debug_out);
  328. wishbone_dcore_in <= wb_masters_in(0);
  329. wishbone_icore_in <= wb_masters_in(1);
  330. wishbone_debug_in <= wb_masters_in(2);
  331. wishbone_arbiter_0: entity work.wishbone_arbiter
  332. generic map(
  333. NUM_MASTERS => NUM_WB_MASTERS
  334. )
  335. port map(
  336. clk => system_clk,
  337. rst => rst_wbar,
  338. wb_masters_in => wb_masters_out,
  339. wb_masters_out => wb_masters_in,
  340. wb_slave_out => wb_master_out,
  341. wb_slave_in => wb_master_in
  342. );
  343. -- Top level Wishbone slaves address decoder & mux
  344. --
  345. -- From CPU to BRAM, DRAM, IO, selected on top 3 bits and dram_at_0
  346. -- 0000 - BRAM
  347. -- 0001 - DRAM
  348. -- 01xx - DRAM
  349. -- 10xx - BRAM
  350. -- 11xx - IO
  351. --
  352. slave_top_intercon: process(wb_master_out, wb_bram_out, wb_dram_out, wb_io_out, dram_at_0)
  353. type slave_top_type is (SLAVE_TOP_BRAM,
  354. SLAVE_TOP_DRAM,
  355. SLAVE_TOP_IO);
  356. variable slave_top : slave_top_type;
  357. variable top_decode : std_ulogic_vector(3 downto 0);
  358. begin
  359. -- Top-level address decoder
  360. top_decode := wb_master_out.adr(31 downto 29) & dram_at_0;
  361. slave_top := SLAVE_TOP_BRAM;
  362. if std_match(top_decode, "0000") then
  363. slave_top := SLAVE_TOP_BRAM;
  364. elsif std_match(top_decode, "0001") then
  365. slave_top := SLAVE_TOP_DRAM;
  366. elsif std_match(top_decode, "01--") then
  367. slave_top := SLAVE_TOP_DRAM;
  368. elsif std_match(top_decode, "10--") then
  369. slave_top := SLAVE_TOP_BRAM;
  370. elsif std_match(top_decode, "11--") then
  371. slave_top := SLAVE_TOP_IO;
  372. end if;
  373. -- Top level wishbone muxing.
  374. wb_bram_in <= wb_master_out;
  375. wb_bram_in.cyc <= '0';
  376. wb_dram_in <= wb_master_out;
  377. wb_dram_in.cyc <= '0';
  378. wb_io_in <= wb_master_out;
  379. wb_io_in.cyc <= '0';
  380. case slave_top is
  381. when SLAVE_TOP_BRAM =>
  382. wb_bram_in.cyc <= wb_master_out.cyc;
  383. wb_master_in <= wb_bram_out;
  384. when SLAVE_TOP_DRAM =>
  385. if HAS_DRAM then
  386. wb_dram_in.cyc <= wb_master_out.cyc;
  387. wb_master_in <= wb_dram_out;
  388. else
  389. wb_master_in.ack <= wb_master_out.cyc and wb_master_out.stb;
  390. wb_master_in.dat <= (others => '1');
  391. wb_master_in.stall <= '0';
  392. end if;
  393. when SLAVE_TOP_IO =>
  394. wb_io_in.cyc <= wb_master_out.cyc;
  395. wb_master_in <= wb_io_out;
  396. end case;
  397. end process slave_top_intercon;
  398. -- IO wishbone slave 64->32 bits converter
  399. --
  400. -- For timing reasons, this adds a one cycle latch on the way both
  401. -- in and out. This relaxes timing and routing pressure on the "main"
  402. -- memory bus by moving all simple IOs to a slower 32-bit bus.
  403. --
  404. -- This implementation is rather dumb at the moment, no stash buffer,
  405. -- so we stall whenever that latch is busy. This can be improved.
  406. --
  407. slave_io_latch: process(system_clk)
  408. -- State
  409. type state_t is (IDLE, WAIT_ACK_BOT, WAIT_ACK_TOP);
  410. variable state : state_t;
  411. -- Misc
  412. variable has_top : boolean;
  413. variable has_bot : boolean;
  414. begin
  415. if rising_edge(system_clk) then
  416. if (rst) then
  417. state := IDLE;
  418. wb_io_out.ack <= '0';
  419. wb_io_out.stall <= '0';
  420. wb_sio_out.cyc <= '0';
  421. wb_sio_out.stb <= '0';
  422. has_top := false;
  423. has_bot := false;
  424. else
  425. case state is
  426. when IDLE =>
  427. -- Clear ACK in case it was set
  428. wb_io_out.ack <= '0';
  429. -- Do we have a cycle ?
  430. if wb_io_in.cyc = '1' and wb_io_in.stb = '1' then
  431. -- Stall master until we are done, we are't (yet) pipelining
  432. -- this, it's all slow IOs.
  433. wb_io_out.stall <= '1';
  434. -- Start cycle downstream
  435. wb_sio_out.cyc <= '1';
  436. wb_sio_out.stb <= '1';
  437. -- Copy write enable to IO out, copy address as well
  438. wb_sio_out.we <= wb_io_in.we;
  439. wb_sio_out.adr <= wb_io_in.adr(wb_sio_out.adr'left downto 3) & "000";
  440. -- Do we have a top word and/or a bottom word ?
  441. has_top := wb_io_in.sel(7 downto 4) /= "0000";
  442. has_bot := wb_io_in.sel(3 downto 0) /= "0000";
  443. -- If we have a bottom word, handle it first, otherwise
  444. -- send the top word down. XXX Split the actual mux out
  445. -- and only generate a control signal.
  446. if has_bot then
  447. if wb_io_in.we = '1' then
  448. wb_sio_out.dat <= wb_io_in.dat(31 downto 0);
  449. end if;
  450. wb_sio_out.sel <= wb_io_in.sel(3 downto 0);
  451. -- Wait for ack
  452. state := WAIT_ACK_BOT;
  453. else
  454. if wb_io_in.we = '1' then
  455. wb_sio_out.dat <= wb_io_in.dat(63 downto 32);
  456. end if;
  457. wb_sio_out.sel <= wb_io_in.sel(7 downto 4);
  458. -- Bump address
  459. wb_sio_out.adr(2) <= '1';
  460. -- Wait for ack
  461. state := WAIT_ACK_TOP;
  462. end if;
  463. end if;
  464. when WAIT_ACK_BOT =>
  465. -- If we aren't stalled by the device, clear stb
  466. if wb_sio_in.stall = '0' then
  467. wb_sio_out.stb <= '0';
  468. end if;
  469. -- Handle ack
  470. if wb_sio_in.ack = '1' then
  471. -- If it's a read, latch the data
  472. if wb_sio_out.we = '0' then
  473. wb_io_out.dat(31 downto 0) <= wb_sio_in.dat;
  474. end if;
  475. -- Do we have a "top" part as well ?
  476. if has_top then
  477. -- Latch data & sel
  478. if wb_io_in.we = '1' then
  479. wb_sio_out.dat <= wb_io_in.dat(63 downto 32);
  480. end if;
  481. wb_sio_out.sel <= wb_io_in.sel(7 downto 4);
  482. -- Bump address and set STB
  483. wb_sio_out.adr(2) <= '1';
  484. wb_sio_out.stb <= '1';
  485. -- Wait for new ack
  486. state := WAIT_ACK_TOP;
  487. else
  488. -- We are done, ack up, clear cyc downstram
  489. wb_sio_out.cyc <= '0';
  490. -- And ack & unstall upstream
  491. wb_io_out.ack <= '1';
  492. wb_io_out.stall <= '0';
  493. -- Wait for next one
  494. state := IDLE;
  495. end if;
  496. end if;
  497. when WAIT_ACK_TOP =>
  498. -- If we aren't stalled by the device, clear stb
  499. if wb_sio_in.stall = '0' then
  500. wb_sio_out.stb <= '0';
  501. end if;
  502. -- Handle ack
  503. if wb_sio_in.ack = '1' then
  504. -- If it's a read, latch the data
  505. if wb_sio_out.we = '0' then
  506. wb_io_out.dat(63 downto 32) <= wb_sio_in.dat;
  507. end if;
  508. -- We are done, ack up, clear cyc downstram
  509. wb_sio_out.cyc <= '0';
  510. -- And ack & unstall upstream
  511. wb_io_out.ack <= '1';
  512. wb_io_out.stall <= '0';
  513. -- Wait for next one
  514. state := IDLE;
  515. end if;
  516. end case;
  517. end if;
  518. end if;
  519. end process;
  520. -- IO wishbone slave intercon.
  521. --
  522. slave_io_intercon: process(wb_sio_out, wb_syscon_out, wb_uart0_out, wb_uart1_out,
  523. wb_ext_io_out, wb_xics_icp_out, wb_xics_ics_out,
  524. wb_spiflash_out)
  525. variable slave_io : slave_io_type;
  526. variable match : std_ulogic_vector(31 downto 12);
  527. variable ext_valid : boolean;
  528. begin
  529. -- Simple address decoder.
  530. slave_io := SLAVE_IO_NONE;
  531. match := "11" & wb_sio_out.adr(29 downto 12);
  532. if std_match(match, x"FF---") and HAS_DRAM then
  533. slave_io := SLAVE_IO_EXTERNAL;
  534. elsif std_match(match, x"F----") then
  535. slave_io := SLAVE_IO_SPI_FLASH_MAP;
  536. elsif std_match(match, x"C0000") then
  537. slave_io := SLAVE_IO_SYSCON;
  538. elsif std_match(match, x"C0002") then
  539. slave_io := SLAVE_IO_UART;
  540. elsif std_match(match, x"C0003") then
  541. slave_io := SLAVE_IO_UART1;
  542. elsif std_match(match, x"C8---") then
  543. slave_io := SLAVE_IO_EXTERNAL;
  544. elsif std_match(match, x"C0004") then
  545. slave_io := SLAVE_IO_ICP;
  546. elsif std_match(match, x"C0005") then
  547. slave_io := SLAVE_IO_ICS;
  548. elsif std_match(match, x"C0006") then
  549. slave_io := SLAVE_IO_SPI_FLASH_REG;
  550. end if;
  551. slave_io_dbg <= slave_io;
  552. wb_uart0_in <= wb_sio_out;
  553. wb_uart0_in.cyc <= '0';
  554. wb_uart1_in <= wb_sio_out;
  555. wb_uart1_in.cyc <= '0';
  556. wb_spiflash_in <= wb_sio_out;
  557. wb_spiflash_in.cyc <= '0';
  558. wb_spiflash_is_reg <= '0';
  559. wb_spiflash_is_map <= '0';
  560. -- Only give xics 8 bits of wb addr (for now...)
  561. wb_xics_icp_in <= wb_sio_out;
  562. wb_xics_icp_in.adr <= (others => '0');
  563. wb_xics_icp_in.adr(7 downto 0) <= wb_sio_out.adr(7 downto 0);
  564. wb_xics_icp_in.cyc <= '0';
  565. wb_xics_ics_in <= wb_sio_out;
  566. wb_xics_ics_in.adr <= (others => '0');
  567. wb_xics_ics_in.adr(11 downto 0) <= wb_sio_out.adr(11 downto 0);
  568. wb_xics_ics_in.cyc <= '0';
  569. wb_ext_io_in <= wb_sio_out;
  570. wb_ext_io_in.cyc <= '0';
  571. wb_syscon_in <= wb_sio_out;
  572. wb_syscon_in.cyc <= '0';
  573. wb_ext_is_dram_csr <= '0';
  574. wb_ext_is_dram_init <= '0';
  575. wb_ext_is_eth <= '0';
  576. -- Default response, ack & return all 1's
  577. wb_sio_in.dat <= (others => '1');
  578. wb_sio_in.ack <= wb_sio_out.stb and wb_sio_out.cyc;
  579. wb_sio_in.stall <= '0';
  580. case slave_io is
  581. when SLAVE_IO_EXTERNAL =>
  582. -- Ext IO "chip selects"
  583. --
  584. -- DRAM init is special at 0xFF* so we just test the top
  585. -- bit. Everything else is at 0xC8* so we test only bits
  586. -- 23 downto 16.
  587. --
  588. ext_valid := false;
  589. if wb_sio_out.adr(29) = '1' and HAS_DRAM then -- DRAM init is special
  590. wb_ext_is_dram_init <= '1';
  591. ext_valid := true;
  592. elsif wb_sio_out.adr(23 downto 16) = x"00" and HAS_DRAM then
  593. wb_ext_is_dram_csr <= '1';
  594. ext_valid := true;
  595. elsif wb_sio_out.adr(23 downto 16) = x"02" and HAS_LITEETH then
  596. wb_ext_is_eth <= '1';
  597. ext_valid := true;
  598. elsif wb_sio_out.adr(23 downto 16) = x"03" and HAS_LITEETH then
  599. wb_ext_is_eth <= '1';
  600. ext_valid := true;
  601. end if;
  602. if ext_valid then
  603. wb_ext_io_in.cyc <= wb_sio_out.cyc;
  604. wb_sio_in <= wb_ext_io_out;
  605. end if;
  606. when SLAVE_IO_SYSCON =>
  607. wb_syscon_in.cyc <= wb_sio_out.cyc;
  608. wb_sio_in <= wb_syscon_out;
  609. when SLAVE_IO_UART =>
  610. wb_uart0_in.cyc <= wb_sio_out.cyc;
  611. wb_sio_in <= wb_uart0_out;
  612. when SLAVE_IO_ICP =>
  613. wb_xics_icp_in.cyc <= wb_sio_out.cyc;
  614. wb_sio_in <= wb_xics_icp_out;
  615. when SLAVE_IO_ICS =>
  616. wb_xics_ics_in.cyc <= wb_sio_out.cyc;
  617. wb_sio_in <= wb_xics_ics_out;
  618. when SLAVE_IO_UART1 =>
  619. wb_uart1_in.cyc <= wb_sio_out.cyc;
  620. wb_sio_in <= wb_uart1_out;
  621. when SLAVE_IO_SPI_FLASH_MAP =>
  622. -- Clear top bits so they don't make their way to the
  623. -- fash chip.
  624. wb_spiflash_in.adr(29 downto 28) <= "00";
  625. wb_spiflash_in.cyc <= wb_sio_out.cyc;
  626. wb_sio_in <= wb_spiflash_out;
  627. wb_spiflash_is_map <= '1';
  628. when SLAVE_IO_SPI_FLASH_REG =>
  629. wb_spiflash_in.cyc <= wb_sio_out.cyc;
  630. wb_sio_in <= wb_spiflash_out;
  631. wb_spiflash_is_reg <= '1';
  632. when others =>
  633. end case;
  634. end process;
  635. -- Syscon slave
  636. syscon0: entity work.syscon
  637. generic map(
  638. HAS_UART => true,
  639. HAS_DRAM => HAS_DRAM,
  640. BRAM_SIZE => MEMORY_SIZE,
  641. DRAM_SIZE => DRAM_SIZE,
  642. DRAM_INIT_SIZE => DRAM_INIT_SIZE,
  643. SIM_BRAM_CHAINBOOT => SIM_BRAM_CHAINBOOT,
  644. CLK_FREQ => CLK_FREQ,
  645. HAS_SPI_FLASH => HAS_SPI_FLASH,
  646. SPI_FLASH_OFFSET => SPI_FLASH_OFFSET,
  647. HAS_LITEETH => HAS_LITEETH,
  648. UART0_IS_16550 => UART0_IS_16550,
  649. HAS_UART1 => HAS_UART1
  650. )
  651. port map(
  652. clk => system_clk,
  653. rst => rst,
  654. wishbone_in => wb_syscon_in,
  655. wishbone_out => wb_syscon_out,
  656. dram_at_0 => dram_at_0,
  657. core_reset => do_core_reset,
  658. soc_reset => open -- XXX TODO
  659. );
  660. --
  661. -- UART0
  662. --
  663. -- Either potato (legacy) or 16550
  664. --
  665. uart0_pp: if not UART0_IS_16550 generate
  666. uart0: entity work.pp_soc_uart
  667. generic map(
  668. FIFO_DEPTH => 32
  669. )
  670. port map(
  671. clk => system_clk,
  672. reset => rst_uart,
  673. txd => uart0_txd,
  674. rxd => uart0_rxd,
  675. irq => uart0_irq,
  676. wb_adr_in => wb_uart0_in.adr(11 downto 0),
  677. wb_dat_in => wb_uart0_in.dat(7 downto 0),
  678. wb_dat_out => uart0_dat8,
  679. wb_cyc_in => wb_uart0_in.cyc,
  680. wb_stb_in => wb_uart0_in.stb,
  681. wb_we_in => wb_uart0_in.we,
  682. wb_ack_out => wb_uart0_out.ack
  683. );
  684. end generate;
  685. uart0_16550 : if UART0_IS_16550 generate
  686. signal irq_l : std_ulogic;
  687. begin
  688. uart0: uart_top
  689. port map (
  690. wb_clk_i => system_clk,
  691. wb_rst_i => rst_uart,
  692. wb_adr_i => wb_uart0_in.adr(4 downto 2),
  693. wb_dat_i => wb_uart0_in.dat(7 downto 0),
  694. wb_dat_o => uart0_dat8,
  695. wb_we_i => wb_uart0_in.we,
  696. wb_stb_i => wb_uart0_in.stb,
  697. wb_cyc_i => wb_uart0_in.cyc,
  698. wb_ack_o => wb_uart0_out.ack,
  699. int_o => irq_l,
  700. stx_pad_o => uart0_txd,
  701. srx_pad_i => uart0_rxd,
  702. rts_pad_o => open,
  703. cts_pad_i => '1',
  704. dtr_pad_o => open,
  705. dsr_pad_i => '1',
  706. ri_pad_i => '0',
  707. dcd_pad_i => '1'
  708. );
  709. -- Add a register on the irq out, helps timing
  710. uart0_irq_latch: process(system_clk)
  711. begin
  712. if rising_edge(system_clk) then
  713. uart0_irq <= irq_l;
  714. end if;
  715. end process;
  716. end generate;
  717. wb_uart0_out.dat <= x"000000" & uart0_dat8;
  718. wb_uart0_out.stall <= wb_uart0_in.cyc and not wb_uart0_out.ack;
  719. --
  720. -- UART1
  721. --
  722. -- Always 16550 if it exists
  723. --
  724. uart1: if HAS_UART1 generate
  725. signal irq_l : std_ulogic;
  726. begin
  727. uart1: uart_top
  728. port map (
  729. wb_clk_i => system_clk,
  730. wb_rst_i => rst_uart,
  731. wb_adr_i => wb_uart1_in.adr(4 downto 2),
  732. wb_dat_i => wb_uart1_in.dat(7 downto 0),
  733. wb_dat_o => uart1_dat8,
  734. wb_we_i => wb_uart1_in.we,
  735. wb_stb_i => wb_uart1_in.stb,
  736. wb_cyc_i => wb_uart1_in.cyc,
  737. wb_ack_o => wb_uart1_out.ack,
  738. int_o => irq_l,
  739. stx_pad_o => uart1_txd,
  740. srx_pad_i => uart1_rxd,
  741. rts_pad_o => open,
  742. cts_pad_i => '1',
  743. dtr_pad_o => open,
  744. dsr_pad_i => '1',
  745. ri_pad_i => '0',
  746. dcd_pad_i => '1'
  747. );
  748. -- Add a register on the irq out, helps timing
  749. uart0_irq_latch: process(system_clk)
  750. begin
  751. if rising_edge(system_clk) then
  752. uart1_irq <= irq_l;
  753. end if;
  754. end process;
  755. wb_uart1_out.dat <= x"000000" & uart1_dat8;
  756. wb_uart1_out.stall <= wb_uart1_in.cyc and not wb_uart1_out.ack;
  757. end generate;
  758. no_uart1 : if not HAS_UART1 generate
  759. wb_uart1_out.dat <= x"00000000";
  760. wb_uart1_out.ack <= wb_uart1_in.cyc and wb_uart1_in.stb;
  761. wb_uart1_out.stall <= '0';
  762. uart1_irq <= '0';
  763. end generate;
  764. spiflash_gen: if HAS_SPI_FLASH generate
  765. spiflash: entity work.spi_flash_ctrl
  766. generic map (
  767. DATA_LINES => SPI_FLASH_DLINES,
  768. DEF_CLK_DIV => SPI_FLASH_DEF_CKDV,
  769. DEF_QUAD_READ => SPI_FLASH_DEF_QUAD
  770. )
  771. port map(
  772. rst => rst_spi,
  773. clk => system_clk,
  774. wb_in => wb_spiflash_in,
  775. wb_out => wb_spiflash_out,
  776. wb_sel_reg => wb_spiflash_is_reg,
  777. wb_sel_map => wb_spiflash_is_map,
  778. sck => spi_flash_sck,
  779. cs_n => spi_flash_cs_n,
  780. sdat_o => spi_flash_sdat_o,
  781. sdat_oe => spi_flash_sdat_oe,
  782. sdat_i => spi_flash_sdat_i
  783. );
  784. end generate;
  785. no_spi0_gen: if not HAS_SPI_FLASH generate
  786. wb_spiflash_out.dat <= (others => '1');
  787. wb_spiflash_out.ack <= wb_spiflash_in.cyc and wb_spiflash_in.stb;
  788. wb_spiflash_out.stall <= wb_spiflash_in.cyc and not wb_spiflash_out.ack;
  789. end generate;
  790. xics_icp: entity work.xics_icp
  791. port map(
  792. clk => system_clk,
  793. rst => rst_xics,
  794. wb_in => wb_xics_icp_in,
  795. wb_out => wb_xics_icp_out,
  796. ics_in => ics_to_icp,
  797. core_irq_out => core_ext_irq
  798. );
  799. xics_ics: entity work.xics_ics
  800. generic map(
  801. SRC_NUM => 16,
  802. PRIO_BITS => 3
  803. )
  804. port map(
  805. clk => system_clk,
  806. rst => rst_xics,
  807. wb_in => wb_xics_ics_in,
  808. wb_out => wb_xics_ics_out,
  809. int_level_in => int_level_in,
  810. icp_out => ics_to_icp
  811. );
  812. -- Assign external interrupts
  813. interrupts: process(all)
  814. begin
  815. int_level_in <= (others => '0');
  816. int_level_in(0) <= uart0_irq;
  817. int_level_in(1) <= ext_irq_eth;
  818. int_level_in(2) <= uart1_irq;
  819. end process;
  820. -- BRAM Memory slave
  821. bram: if MEMORY_SIZE /= 0 generate
  822. bram0: entity work.wishbone_bram_wrapper
  823. generic map(
  824. MEMORY_SIZE => MEMORY_SIZE,
  825. RAM_INIT_FILE => RAM_INIT_FILE,
  826. SIM_MAIN_BRAM => SIM_MAIN_BRAM
  827. )
  828. port map(
  829. clk => system_clk,
  830. rst => rst_bram,
  831. wishbone_in => wb_bram_in,
  832. wishbone_out => wb_bram_out,
  833. bram_we => bram_we,
  834. bram_re => bram_re,
  835. bram_addr => bram_addr,
  836. bram_di => bram_di,
  837. bram_do => bram_do,
  838. bram_sel => bram_sel
  839. );
  840. end generate;
  841. no_bram: if MEMORY_SIZE = 0 generate
  842. wb_bram_out.ack <= wb_bram_in.cyc and wb_bram_in.stb;
  843. wb_bram_out.dat <= x"FFFFFFFFFFFFFFFF";
  844. wb_bram_out.stall <= wb_bram_in.cyc and not wb_bram_out.ack;
  845. end generate;
  846. -- DMI(debug bus) <-> JTAG bridge
  847. dtm: entity work.dmi_dtm
  848. generic map(
  849. ABITS => 8,
  850. DBITS => 64
  851. )
  852. port map(
  853. sys_clk => system_clk,
  854. sys_reset => rst_dtm,
  855. dmi_addr => dmi_addr,
  856. dmi_din => dmi_din,
  857. dmi_dout => dmi_dout,
  858. dmi_req => dmi_req,
  859. dmi_wr => dmi_wr,
  860. dmi_ack => dmi_ack
  861. );
  862. -- DMI interconnect
  863. dmi_intercon: process(dmi_addr, dmi_req,
  864. dmi_wb_ack, dmi_wb_dout,
  865. dmi_core_ack, dmi_core_dout)
  866. -- DMI address map (each address is a full 64-bit register)
  867. --
  868. -- Offset: Size: Slave:
  869. -- 0 4 Wishbone
  870. -- 10 16 Core
  871. type slave_type is (SLAVE_WB,
  872. SLAVE_CORE,
  873. SLAVE_NONE);
  874. variable slave : slave_type;
  875. begin
  876. -- Simple address decoder
  877. slave := SLAVE_NONE;
  878. if std_match(dmi_addr, "000000--") then
  879. slave := SLAVE_WB;
  880. elsif std_match(dmi_addr, "0001----") then
  881. slave := SLAVE_CORE;
  882. end if;
  883. -- DMI muxing
  884. dmi_wb_req <= '0';
  885. dmi_core_req <= '0';
  886. case slave is
  887. when SLAVE_WB =>
  888. dmi_wb_req <= dmi_req;
  889. dmi_ack <= dmi_wb_ack;
  890. dmi_din <= dmi_wb_dout;
  891. when SLAVE_CORE =>
  892. dmi_core_req <= dmi_req;
  893. dmi_ack <= dmi_core_ack;
  894. dmi_din <= dmi_core_dout;
  895. when others =>
  896. dmi_ack <= dmi_req;
  897. dmi_din <= (others => '1');
  898. end case;
  899. -- SIM magic exit
  900. if SIM and dmi_req = '1' and dmi_addr = "11111111" and dmi_wr = '1' then
  901. stop;
  902. end if;
  903. end process;
  904. -- Wishbone debug master (TODO: Add a DMI address decoder)
  905. wishbone_debug: entity work.wishbone_debug_master
  906. port map(clk => system_clk,
  907. rst => rst_wbdb,
  908. dmi_addr => dmi_addr(1 downto 0),
  909. dmi_dout => dmi_wb_dout,
  910. dmi_din => dmi_dout,
  911. dmi_wr => dmi_wr,
  912. dmi_ack => dmi_wb_ack,
  913. dmi_req => dmi_wb_req,
  914. wb_in => wishbone_debug_in,
  915. wb_out => wishbone_debug_out);
  916. end architecture behaviour;