2021-09-08 13:35:31 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2021 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
|
|
from migen import *
|
2022-05-02 06:42:04 -04:00
|
|
|
|
2023-02-23 03:09:33 -05:00
|
|
|
from litex.gen import *
|
2022-10-27 10:58:55 -04:00
|
|
|
|
2022-05-02 06:42:04 -04:00
|
|
|
from litex_boards.platforms import sipeed_tang_nano_4k
|
2021-09-08 13:35:31 -04:00
|
|
|
|
2021-11-29 05:45:13 -05:00
|
|
|
from litex.soc.cores.clock.gowin_gw1n import GW1NPLL
|
2021-09-08 13:35:31 -04:00
|
|
|
from litex.soc.integration.soc_core import *
|
2021-09-17 09:57:55 -04:00
|
|
|
from litex.soc.integration.soc import SoCRegion
|
2021-09-08 13:35:31 -04:00
|
|
|
from litex.soc.integration.builder import *
|
|
|
|
from litex.soc.cores.led import LedChaser
|
2021-09-20 03:32:20 -04:00
|
|
|
from litex.soc.cores.video import *
|
2021-09-08 13:35:31 -04:00
|
|
|
|
2022-03-01 03:10:19 -05:00
|
|
|
from litex.soc.cores.hyperbus import HyperRAM
|
2021-09-17 10:30:39 -04:00
|
|
|
|
2021-09-08 13:35:31 -04:00
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
class _CRG(LiteXModule):
|
2021-09-20 03:32:20 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq, with_video_pll=False):
|
2022-10-27 10:58:55 -04:00
|
|
|
self.rst = Signal()
|
|
|
|
self.cd_sys = ClockDomain()
|
2021-09-08 13:35:31 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
# Clk / Rst
|
2022-01-24 12:46:51 -05:00
|
|
|
clk27 = platform.request("clk27")
|
2021-09-09 05:23:20 -04:00
|
|
|
rst_n = platform.request("user_btn", 0)
|
2021-09-20 02:40:19 -04:00
|
|
|
|
|
|
|
# PLL
|
2022-10-27 10:58:55 -04:00
|
|
|
self.pll = pll = GW1NPLL(devicename=platform.devicename, device=platform.device)
|
2021-09-20 02:40:19 -04:00
|
|
|
self.comb += pll.reset.eq(~rst_n)
|
2022-01-24 12:46:51 -05:00
|
|
|
pll.register_clkin(clk27, 27e6)
|
2021-09-20 02:40:19 -04:00
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq)
|
2021-09-08 13:35:31 -04:00
|
|
|
|
2021-09-20 03:32:20 -04:00
|
|
|
|
|
|
|
# Video PLL
|
|
|
|
if with_video_pll:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.video_pll = video_pll = GW1NPLL(devicename=platform.devicename, device=platform.device)
|
2021-09-20 03:32:20 -04:00
|
|
|
self.comb += video_pll.reset.eq(~rst_n)
|
2022-01-24 12:46:51 -05:00
|
|
|
video_pll.register_clkin(clk27, 27e6)
|
2022-10-27 10:58:55 -04:00
|
|
|
self.cd_hdmi = ClockDomain()
|
|
|
|
self.cd_hdmi5x = ClockDomain()
|
2021-09-20 03:32:20 -04:00
|
|
|
video_pll.create_clkout(self.cd_hdmi5x, 125e6)
|
|
|
|
self.specials += Instance("CLKDIV",
|
2024-01-02 07:42:56 -05:00
|
|
|
p_DIV_MODE = "5",
|
|
|
|
i_RESETN = rst_n,
|
|
|
|
i_HCLKIN = self.cd_hdmi5x.clk,
|
|
|
|
o_CLKOUT = self.cd_hdmi.clk,
|
2021-09-20 03:32:20 -04:00
|
|
|
)
|
|
|
|
|
2021-09-08 13:35:31 -04:00
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2022-11-08 06:29:11 -05:00
|
|
|
def __init__(self, sys_clk_freq=27e6,
|
|
|
|
with_hyperram = False,
|
|
|
|
with_led_chaser = True,
|
|
|
|
with_video_terminal = True,
|
|
|
|
**kwargs):
|
2022-05-02 06:42:04 -04:00
|
|
|
platform = sipeed_tang_nano_4k.Platform()
|
2021-09-08 13:35:31 -04:00
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# CRG --------------------------------------------------------------------------------------
|
2022-10-27 10:58:55 -04:00
|
|
|
self.crg = _CRG(platform, sys_clk_freq, with_video_pll=with_video_terminal)
|
2022-04-21 06:17:26 -04:00
|
|
|
|
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
2022-01-07 04:34:47 -05:00
|
|
|
if "cpu_type" in kwargs and kwargs["cpu_type"] == "gowin_emcu":
|
|
|
|
kwargs["integrated_sram_size"] = 0 # SRAM is directly attached to CPU
|
|
|
|
kwargs["integrated_rom_size"] = 0 # boot flash directly attached to CPU
|
2021-12-08 17:50:14 -05:00
|
|
|
else:
|
2022-01-07 04:34:47 -05:00
|
|
|
# Disable Integrated ROM
|
2021-12-08 17:50:14 -05:00
|
|
|
kwargs["integrated_rom_size"] = 0
|
2022-04-21 06:17:26 -04:00
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on Tang Nano 4K", **kwargs)
|
2021-09-08 13:35:31 -04:00
|
|
|
|
2021-12-08 17:33:49 -05:00
|
|
|
if self.cpu_type == 'vexriscv':
|
|
|
|
assert self.cpu_variant == 'minimal', 'use --cpu-variant=minimal to fit into number of BSRAMs'
|
|
|
|
|
2024-01-02 07:42:56 -05:00
|
|
|
# Gowin EMCU -------------------------------------------------------------------------------
|
2022-01-07 04:34:47 -05:00
|
|
|
if self.cpu_type == "gowin_emcu":
|
2024-01-02 07:42:56 -05:00
|
|
|
# Use EMCU's SRAM.
|
2022-01-23 07:05:51 -05:00
|
|
|
self.bus.add_region("sram", SoCRegion(
|
2024-01-02 07:42:56 -05:00
|
|
|
origin = self.cpu.mem_map["sram"],
|
|
|
|
size = 16 * kB,
|
|
|
|
))
|
|
|
|
# Use ECMU's FLASH as ROM.
|
2022-01-23 07:05:51 -05:00
|
|
|
self.bus.add_region("rom", SoCRegion(
|
2024-01-02 07:42:56 -05:00
|
|
|
origin = self.cpu.mem_map["rom"],
|
|
|
|
size = 32 * kB,
|
|
|
|
linker = True,
|
|
|
|
))
|
|
|
|
# No Gowin EMCU ----------------------------------------------------------------------------
|
2021-12-08 18:12:31 -05:00
|
|
|
else:
|
2024-01-02 07:42:56 -05:00
|
|
|
# Use SPI-Flash as ROM.
|
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# SPI Flash ----------------------------------------------------------------------------
|
2022-01-24 12:46:51 -05:00
|
|
|
from litespi.modules import W25Q32
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
2022-01-23 10:10:46 -05:00
|
|
|
self.add_spi_flash(mode="1x", module=W25Q32(Codes.READ_1_1_1), with_master=False)
|
2024-01-02 07:42:56 -05:00
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# Add ROM linker region ----------------------------------------------------------------
|
2021-12-08 17:50:14 -05:00
|
|
|
self.bus.add_region("rom", SoCRegion(
|
2022-01-07 04:34:47 -05:00
|
|
|
origin = self.bus.regions["spiflash"].origin,
|
2024-06-13 04:04:19 -04:00
|
|
|
size = 32 * KILOBYTE,
|
2021-12-08 17:50:14 -05:00
|
|
|
linker = True)
|
|
|
|
)
|
2022-01-07 09:19:23 -05:00
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
2021-09-17 09:57:55 -04:00
|
|
|
|
2021-09-17 10:30:39 -04:00
|
|
|
# HyperRAM ---------------------------------------------------------------------------------
|
|
|
|
if with_hyperram:
|
|
|
|
class HyperRAMPads:
|
|
|
|
def __init__(self):
|
|
|
|
self.clk = Signal()
|
|
|
|
self.rst_n = platform.request("O_hpram_reset_n")
|
|
|
|
self.dq = platform.request("IO_hpram_dq")
|
|
|
|
self.cs_n = platform.request("O_hpram_cs_n")
|
|
|
|
self.rwds = platform.request("IO_hpram_rwds")
|
|
|
|
|
|
|
|
hyperram_pads = HyperRAMPads()
|
|
|
|
self.comb += platform.request("O_hpram_ck").eq(hyperram_pads.clk)
|
|
|
|
self.comb += platform.request("O_hpram_ck_n").eq(~hyperram_pads.clk)
|
2022-10-27 10:58:55 -04:00
|
|
|
self.hyperram = HyperRAM(hyperram_pads, sys_clk_freq=sys_clk_freq)
|
2024-06-13 04:04:19 -04:00
|
|
|
self.bus.add_slave("main_ram", slave=self.hyperram.bus, region=SoCRegion(origin=0x40000000, size=8 * MEGABYTE))
|
2021-09-17 10:30:39 -04:00
|
|
|
|
2021-09-20 03:32:20 -04:00
|
|
|
# Video ------------------------------------------------------------------------------------
|
|
|
|
if with_video_terminal:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.videophy = VideoGowinHDMIPHY(platform.request("hdmi"), clock_domain="hdmi")
|
2021-09-20 03:32:20 -04:00
|
|
|
self.add_video_colorbars(phy=self.videophy, timings="640x480@75Hz", clock_domain="hdmi")
|
|
|
|
#self.add_video_terminal(phy=self.videophy, timings="640x480@75Hz", clock_domain="hdmi") # FIXME: Free up BRAMs.
|
|
|
|
|
2021-09-08 13:35:31 -04:00
|
|
|
# Leds -------------------------------------------------------------------------------------
|
|
|
|
if with_led_chaser:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(
|
2021-09-08 13:35:31 -04:00
|
|
|
pads = platform.request_all("user_led"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
2022-11-06 15:39:52 -05:00
|
|
|
from litex.build.parser import LiteXArgumentParser
|
2022-11-08 04:41:35 -05:00
|
|
|
parser = LiteXArgumentParser(platform=sipeed_tang_nano_4k.Platform, description="LiteX SoC on Tang Nano 4K.")
|
2024-01-02 07:42:56 -05:00
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream and BIOS.")
|
|
|
|
parser.add_target_argument("--sys-clk-freq", default=27e6, type=float, help="System clock frequency.")
|
2024-01-11 07:54:44 -05:00
|
|
|
parser.add_target_argument("--with-hyperram", action="store_true", help="Enable HyperRAM.")
|
2024-01-02 07:42:56 -05:00
|
|
|
parser.add_target_argument("--with-video-terminal", action="store_true", help="Enable Video Terminal (HDMI).")
|
2021-09-08 13:35:31 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2022-11-08 04:41:35 -05:00
|
|
|
sys_clk_freq = args.sys_clk_freq,
|
2024-01-11 07:54:44 -05:00
|
|
|
with_hyperram = args.with_hyperram,
|
2022-11-08 04:41:35 -05:00
|
|
|
with_video_terminal = args.with_video_terminal,
|
2022-11-07 02:43:26 -05:00
|
|
|
**parser.soc_argdict
|
2021-09-08 13:35:31 -04:00
|
|
|
)
|
|
|
|
|
2022-11-05 03:07:14 -04:00
|
|
|
builder = Builder(soc, **parser.builder_argdict)
|
2022-05-06 09:14:32 -04:00
|
|
|
if args.build:
|
2022-11-05 03:07:14 -04:00
|
|
|
builder.build(**parser.toolchain_argdict)
|
2021-09-08 13:35:31 -04:00
|
|
|
|
|
|
|
if args.load:
|
|
|
|
prog = soc.platform.create_programmer()
|
2022-03-17 04:40:10 -04:00
|
|
|
prog.load_bitstream(builder.get_bitstream_filename(mode="sram"))
|
2021-09-08 13:35:31 -04:00
|
|
|
|
2021-09-09 05:23:20 -04:00
|
|
|
if args.flash:
|
2024-01-03 07:06:55 -05:00
|
|
|
prog = soc.platform.create_programmer()
|
|
|
|
bitstream_filename = builder.get_bitstream_filename(mode="flash", ext=".fs") # FIXME
|
|
|
|
bios_filename = builder.get_bios_filename()
|
2023-12-14 07:24:19 -05:00
|
|
|
if args.cpu_type != "gowin_emcu":
|
2024-01-03 07:06:55 -05:00
|
|
|
prog.flash(address=0, data_file=bitstream_filename)
|
|
|
|
prog.flash(address=0, data_file=bios_filename, external=True)
|
|
|
|
else:
|
|
|
|
prog.flash(
|
|
|
|
address = 0,
|
|
|
|
data_file = bitstream_filename,
|
|
|
|
mcufw = bios_filename,
|
|
|
|
)
|
2022-01-23 07:05:51 -05:00
|
|
|
|
2021-09-08 13:35:31 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|