2022-01-21 22:45:49 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2022 Icenowy Zheng <icenowy@aosc.io>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
2022-06-03 06:28:38 -04:00
|
|
|
import os
|
2022-01-21 22:45:49 -05:00
|
|
|
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-03 12:41:18 -04:00
|
|
|
from litex_boards.platforms import sipeed_tang_nano_9k
|
2022-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
from litex.soc.cores.clock.gowin_gw1n import GW1NPLL
|
|
|
|
from litex.soc.integration.soc_core import *
|
|
|
|
from litex.soc.integration.soc import SoCRegion
|
|
|
|
from litex.soc.integration.builder import *
|
|
|
|
from litex.soc.cores.led import LedChaser
|
|
|
|
from litex.soc.cores.video import *
|
|
|
|
|
2022-03-01 03:10:19 -05:00
|
|
|
from litex.soc.cores.hyperbus import HyperRAM
|
2022-01-22 14:17:57 -05:00
|
|
|
|
2022-01-21 22:45:49 -05:00
|
|
|
kB = 1024
|
|
|
|
mB = 1024*kB
|
|
|
|
|
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
class _CRG(LiteXModule):
|
2022-01-21 22:45:49 -05: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()
|
2022-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
# Clk / Rst
|
|
|
|
clk27 = platform.request("clk27")
|
|
|
|
rst_n = platform.request("user_btn", 0)
|
|
|
|
|
|
|
|
# PLL
|
2022-10-27 10:58:55 -04:00
|
|
|
self.pll = pll = GW1NPLL(devicename=platform.devicename, device=platform.device)
|
2022-01-21 22:45:49 -05:00
|
|
|
self.comb += pll.reset.eq(~rst_n)
|
|
|
|
pll.register_clkin(clk27, 27e6)
|
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq)
|
|
|
|
|
2022-05-14 22:53:09 -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)
|
2022-05-14 22:53:09 -04:00
|
|
|
self.comb += video_pll.reset.eq(~rst_n)
|
|
|
|
video_pll.register_clkin(clk27, 27e6)
|
2022-10-27 10:58:55 -04:00
|
|
|
self.cd_hdmi = ClockDomain()
|
|
|
|
self.cd_hdmi5x = ClockDomain()
|
2022-05-14 22:53:09 -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
|
|
|
|
)
|
|
|
|
|
2022-01-21 22:45:49 -05:00
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2022-11-08 05:54:17 -05:00
|
|
|
def __init__(self, sys_clk_freq=27e6, bios_flash_offset=0x0,
|
2022-11-08 06:29:11 -05:00
|
|
|
with_led_chaser = True,
|
|
|
|
with_video_terminal = False,
|
|
|
|
**kwargs):
|
2022-05-03 12:41:18 -04:00
|
|
|
platform = sipeed_tang_nano_9k.Platform()
|
2022-01-21 22:45:49 -05: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-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
2022-04-21 06:17:26 -04:00
|
|
|
# Disable Integrated ROM
|
|
|
|
kwargs["integrated_rom_size"] = 0
|
2022-06-03 06:28:38 -04:00
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on Tang Nano 9K", **kwargs)
|
2022-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
|
|
|
from litespi.modules import W25Q32
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
|
|
|
self.add_spi_flash(mode="1x", module=W25Q32(Codes.READ_1_1_1), with_master=False)
|
|
|
|
|
|
|
|
# Add ROM linker region --------------------------------------------------------------------
|
|
|
|
self.bus.add_region("rom", SoCRegion(
|
|
|
|
origin = self.bus.regions["spiflash"].origin + bios_flash_offset,
|
|
|
|
size = 64*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
|
|
|
|
2022-01-24 12:35:12 -05:00
|
|
|
# HyperRAM ---------------------------------------------------------------------------------
|
|
|
|
if not self.integrated_main_ram_size:
|
|
|
|
# TODO: Use second 32Mbit PSRAM chip.
|
|
|
|
dq = platform.request("IO_psram_dq")
|
|
|
|
rwds = platform.request("IO_psram_rwds")
|
|
|
|
reset_n = platform.request("O_psram_reset_n")
|
|
|
|
cs_n = platform.request("O_psram_cs_n")
|
|
|
|
ck = platform.request("O_psram_ck")
|
|
|
|
ck_n = platform.request("O_psram_ck_n")
|
|
|
|
class HyperRAMPads:
|
|
|
|
def __init__(self, n):
|
|
|
|
self.clk = Signal()
|
|
|
|
self.rst_n = reset_n[n]
|
|
|
|
self.dq = dq[8*n:8*(n+1)]
|
|
|
|
self.cs_n = cs_n[n]
|
|
|
|
self.rwds = rwds[n]
|
|
|
|
|
|
|
|
hyperram_pads = HyperRAMPads(0)
|
|
|
|
self.comb += ck[0].eq(hyperram_pads.clk)
|
|
|
|
self.comb += ck_n[0].eq(~hyperram_pads.clk)
|
2022-06-03 06:28:38 -04:00
|
|
|
# FIXME: Issue with upstream HyperRAM core, so use old one. Need to investigate.
|
|
|
|
if not os.path.exists("hyperbus.py"):
|
|
|
|
os.system("wget https://github.com/litex-hub/litex-boards/files/8831568/hyperbus.py.txt")
|
|
|
|
os.system("mv hyperbus.py.txt hyperbus.py")
|
|
|
|
from hyperbus import HyperRAM
|
2022-10-27 10:58:55 -04:00
|
|
|
self.hyperram = HyperRAM(hyperram_pads)
|
2022-01-24 12:35:12 -05:00
|
|
|
self.bus.add_slave("main_ram", slave=self.hyperram.bus, region=SoCRegion(origin=self.mem_map["main_ram"], size=4*mB))
|
2022-01-22 14:17:57 -05:00
|
|
|
|
2022-05-14 22:53:09 -04:00
|
|
|
# Video ------------------------------------------------------------------------------------
|
|
|
|
if with_video_terminal:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.videophy = VideoGowinHDMIPHY(platform.request("hdmi"), clock_domain="hdmi")
|
2022-05-14 22:53:09 -04:00
|
|
|
self.add_video_colorbars(phy=self.videophy, timings="640x480@60Hz", clock_domain="hdmi")
|
|
|
|
#self.add_video_terminal(phy=self.videophy, timings="640x480@75Hz", clock_domain="hdmi") # FIXME: Free up BRAMs.
|
|
|
|
|
|
|
|
|
2022-01-21 22:45:49 -05:00
|
|
|
# Leds -------------------------------------------------------------------------------------
|
|
|
|
if with_led_chaser:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(
|
2022-01-21 22:45:49 -05: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_9k.Platform, description="LiteX SoC on Tang Nano 9K.")
|
2022-11-05 03:07:14 -04:00
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream.")
|
2022-11-08 04:41:35 -05:00
|
|
|
parser.add_target_argument("--sys-clk-freq", default=27e6, type=float, help="System clock frequency.")
|
2022-11-05 03:07:14 -04:00
|
|
|
parser.add_target_argument("--bios-flash-offset", default="0x0", help="BIOS offset in SPI Flash.")
|
|
|
|
parser.add_target_argument("--with-spi-sdcard", action="store_true", help="Enable SPI-mode SDCard support.")
|
|
|
|
parser.add_target_argument("--with-video-terminal", action="store_true", help="Enable Video Terminal (HDMI).")
|
|
|
|
parser.add_target_argument("--prog-kit", default="openfpgaloader", help="Programmer select from Gowin/openFPGALoader.")
|
2022-01-21 22:45:49 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2022-11-08 04:41:35 -05:00
|
|
|
sys_clk_freq = args.sys_clk_freq,
|
2022-06-03 06:28:38 -04:00
|
|
|
bios_flash_offset = int(args.bios_flash_offset, 0),
|
|
|
|
with_video_terminal = args.with_video_terminal,
|
2022-11-07 02:43:26 -05:00
|
|
|
**parser.soc_argdict
|
2022-01-21 22:45:49 -05:00
|
|
|
)
|
|
|
|
|
2022-01-22 14:17:57 -05:00
|
|
|
if args.with_spi_sdcard:
|
|
|
|
soc.add_spi_sdcard()
|
|
|
|
|
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)
|
2022-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
if args.load:
|
2022-03-08 00:24:56 -05:00
|
|
|
prog = soc.platform.create_programmer(kit=args.prog_kit)
|
2022-03-17 04:40:10 -04:00
|
|
|
prog.load_bitstream(builder.get_bitstream_filename(mode="sram"))
|
2022-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
if args.flash:
|
2022-03-08 00:24:56 -05:00
|
|
|
prog = soc.platform.create_programmer(kit=args.prog_kit)
|
2022-03-17 04:40:10 -04:00
|
|
|
prog.flash(0, builder.get_bitstream_filename(mode="flash", ext=".fs")) # FIXME
|
|
|
|
# Axternal SPI programming not supported by gowin 'programmer_cli' now!
|
2022-03-08 00:24:56 -05:00
|
|
|
# if needed, use openFPGALoader or Gowin programmer GUI
|
|
|
|
if args.prog_kit == "openfpgaloader":
|
2022-03-17 04:21:05 -04:00
|
|
|
prog.flash(int(args.bios_flash_offset, 0), builder.get_bios_filename(), external=True)
|
2022-01-21 22:45:49 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|