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 *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
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
|
|
|
|
|
|
|
from litex_boards.platforms import tang_nano_4k
|
2021-09-16 13:22:30 -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 ----------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class _CRG(Module):
|
2021-09-20 03:32:20 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq, with_video_pll=False):
|
2021-09-08 13:35:31 -04:00
|
|
|
self.rst = Signal()
|
2021-09-20 02:40:19 -04:00
|
|
|
self.clock_domains.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
|
2021-11-29 05:45:13 -05:00
|
|
|
self.submodules.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:
|
2021-11-29 05:45:13 -05:00
|
|
|
self.submodules.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)
|
2021-09-20 03:32:20 -04:00
|
|
|
self.clock_domains.cd_hdmi = ClockDomain()
|
|
|
|
self.clock_domains.cd_hdmi5x = ClockDomain()
|
|
|
|
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):
|
2021-09-08 13:35:31 -04:00
|
|
|
platform = tang_nano_4k.Platform()
|
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# CRG --------------------------------------------------------------------------------------
|
|
|
|
self.submodules.crg = _CRG(platform, sys_clk_freq, with_video_pll=with_video_terminal)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
self.submodules.hyperram = HyperRAM(hyperram_pads)
|
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:
|
|
|
|
self.submodules.videophy = VideoHDMIPHY(platform.request("hdmi"), clock_domain="hdmi")
|
|
|
|
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:
|
|
|
|
self.submodules.leds = LedChaser(
|
|
|
|
pads = platform.request_all("user_led"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
2022-03-21 11:59:40 -04:00
|
|
|
from litex.soc.integration.soc import LiteXSoCArgumentParser
|
|
|
|
parser = LiteXSoCArgumentParser(description="LiteX SoC on Tang Nano 4K")
|
2022-03-21 13:30:10 -04:00
|
|
|
target_group = parser.add_argument_group(title="Target options")
|
|
|
|
target_group.add_argument("--build", action="store_true", help="Build bitstream.")
|
|
|
|
target_group.add_argument("--load", action="store_true", help="Load bitstream.")
|
|
|
|
target_group.add_argument("--flash", action="store_true", help="Flash Bitstream.")
|
|
|
|
target_group.add_argument("--sys-clk-freq",default=27e6, help="System clock frequency.")
|
2021-09-08 13:35:31 -04:00
|
|
|
builder_args(parser)
|
|
|
|
soc_core_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2021-12-08 17:50:14 -05:00
|
|
|
sys_clk_freq=int(float(args.sys_clk_freq)),
|
2021-12-08 18:12:31 -05:00
|
|
|
**soc_core_argdict(args)
|
2021-09-08 13:35:31 -04:00
|
|
|
)
|
|
|
|
|
2021-12-08 18:12:31 -05:00
|
|
|
builder = Builder(soc, **builder_argdict(args))
|
2021-09-08 13:35:31 -04:00
|
|
|
builder.build(run=args.build)
|
|
|
|
|
|
|
|
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()
|