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
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
from litex.gen import LiteXModule
|
|
|
|
|
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-17 09:57:55 -04:00
|
|
|
kB = 1024
|
2022-01-24 12:46:51 -05:00
|
|
|
mB = 1024*kB
|
2021-09-17 09:57:55 -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",
|
|
|
|
p_DIV_MODE= "5",
|
|
|
|
i_RESETN = rst_n,
|
|
|
|
i_HCLKIN = self.cd_hdmi5x.clk,
|
|
|
|
o_CLKOUT = self.cd_hdmi.clk
|
|
|
|
)
|
|
|
|
|
2021-09-08 13:35:31 -04:00
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2022-01-24 12:46:51 -05:00
|
|
|
def __init__(self, sys_clk_freq=int(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["with_uart"] = False # CPU has own UART
|
|
|
|
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'
|
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# Gowin EMCU Integration -------------------------------------------------------------------
|
2022-01-07 04:34:47 -05:00
|
|
|
if self.cpu_type == "gowin_emcu":
|
|
|
|
self.cpu.connect_uart(platform.request("serial"))
|
2022-01-23 07:05:51 -05:00
|
|
|
self.bus.add_region("sram", SoCRegion(
|
|
|
|
origin=self.cpu.mem_map["sram"],
|
2022-01-23 10:10:46 -05:00
|
|
|
size=16 * kB)
|
2022-01-23 07:05:51 -05:00
|
|
|
)
|
|
|
|
self.bus.add_region("rom", SoCRegion(
|
|
|
|
origin=self.cpu.mem_map["rom"],
|
2022-01-23 10:10:46 -05:00
|
|
|
size=32 * kB,
|
2022-01-23 07:05:51 -05:00
|
|
|
linker=True)
|
|
|
|
)
|
2021-12-08 18:12:31 -05:00
|
|
|
else:
|
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)
|
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,
|
|
|
|
size = 32*kB,
|
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)
|
2022-01-24 12:46:51 -05:00
|
|
|
self.bus.add_slave("main_ram", slave=self.hyperram.bus, region=SoCRegion(origin=0x40000000, size=8*mB))
|
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-05 03:07:14 -04:00
|
|
|
parser = LiteXArgumentParser(platform=sipeed_tang_nano_4k.Platform, description="LiteX SoC on Tang Nano 4K")
|
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream.")
|
|
|
|
parser.add_target_argument("--sys-clk-freq",default=27e6, help="System clock frequency.")
|
|
|
|
parser.add_target_argument("--with-video-terminal",action="store_true", help="System clock frequency.")
|
2021-09-08 13:35:31 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2021-12-08 17:50:14 -05:00
|
|
|
sys_clk_freq=int(float(args.sys_clk_freq)),
|
2022-09-06 13:08:01 -04:00
|
|
|
with_video_terminal=args.with_video_terminal,
|
2022-11-05 03:07:14 -04:00
|
|
|
**parser.soc_core_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:
|
|
|
|
prog = soc.platform.create_programmer()
|
2022-03-17 04:40:10 -04:00
|
|
|
prog.flash(0, builder.get_bitstream_filename(mode="flash", ext=".fs")) # FIXME
|
2022-03-17 04:21:05 -04:00
|
|
|
prog.flash(0, builder.get_bios_filename(), external=True)
|
2021-09-09 05:23:20 -04:00
|
|
|
|
2022-01-23 07:05:51 -05:00
|
|
|
|
2021-09-08 13:35:31 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|