# SPDX-License-Identifier: GPL-2.0-or-later

# This is a STM32L4P5G discovery board with a single STM32L4R9AGI6 chip.
# http://www.st.com/en/evaluation-tools/stm32l4p5g-dk.html

# This is for using the onboard STLINK
source [find interface/stlink.cfg]

transport select hla_swd

# increase working area to 96KB
set WORKAREASIZE 0x18000

# enable stmqspi
set OCTOSPI1 1
set OCTOSPI2 0

source [find target/stm32l4x.cfg]

# OCTOSPI initialization
# octo: 8-line mode
proc octospi_init { octo } {
	global a b
	mmw 0x4002104C 0x001001FF 0				;# RCC_AHB2ENR |= OSPIMEN, GPIOAEN-GPIOIEN (enable clocks)
	mmw 0x40021050 0x00000300 0				;# RCC_AHB3ENR |= OSPI2EN, OSPI1EN (enable clocks)
	mmw 0x40021058 0x10000000 0				;# RCC_APB1ENR1 |= PWREN (enable clock)
	sleep 1									;# Wait for clock startup

	mmw 0x40007004 0x00000200 0				;# PWR_CR2 |= IOSV (required for use of GPOIG, cf. RM0432)

	mww 0x50061C04 0x07050333				;# OCTOSPIM_P1CR: assign Port 1 to OCTOSPI2
	mww 0x50061C08 0x03010111				;# OCTOSPIM_P2CR: assign Port 2 to OCTOSPI1

	# PE11: P1_NCS, PE10: P1_CLK, PG06: P1_DQS, PD07: P1_IO7, PC03: P1_IO6, PD05: P1_IO5
	# PD04: P1_IO4, PA06: P1_IO3, PA07: P1_IO2, PE13: P1_IO1, PE11: P1_IO0

	# PA07:AF10:V, PA06:AF10:V, PC03:AF10:V, PD07:AF10:V, PD05:AF10:V, PD04:AF10:V
	# PE13:AF10:V, PE12:AF10:V, PE11:AF10:V, PE10:AF10:V, PG06:AF03:V

	# Port A: PA07:AF10:V, PA06:AF10:V
	mmw 0x48000000 0x0000A000 0x00005000	;# MODER
	mmw 0x48000008 0x0000F000 0x00000000	;# OSPEEDR
	mmw 0x4800000C 0x00000000 0x0000F000	;# PUPDR
	mmw 0x48000020 0xAA000000 0x55000000	;# AFRL
	# Port C: PC03:AF10:V
	mmw 0x48000800 0x00000080 0x00000040	;# MODER
	mmw 0x48000808 0x000000C0 0x00000000	;# OSPEEDR
	mmw 0x4800080C 0x00000000 0x000000C0	;# PUPDR
	mmw 0x48000820 0x0000A000 0x00005000	;# AFRL
	# Port D: PD07:AF10:V, PD05:AF10:V, PD04:AF10:V
	mmw 0x48000C00 0x00008A00 0x00004500	;# MODER
	mmw 0x48000C08 0x0000CF00 0x00000000	;# OSPEEDR
	mmw 0x48000C0C 0x00000000 0x0000CF00	;# PUPDR
	mmw 0x48000C20 0xA0AA0000 0x50550000	;# AFRL
	# Port E: PE13:AF10:V, PE12:AF10:V, PE11:AF10:V, PE10:AF10:V
	mmw 0x48001000 0x0AA00000 0x05500000	;# MODER
	mmw 0x48001008 0x0FF00000 0x00000000	;# OSPEEDR
	mmw 0x4800100C 0x00000000 0x0FF00000	;# PUPDR
	mmw 0x48001024 0x00AAAA00 0x00555500	;# AFRH
	# Port G: PG06:AF03:V
	mmw 0x48001800 0x00002000 0x00001000	;# MODER
	mmw 0x48001808 0x00003000 0x00000000	;# OSPEEDR
	mmw 0x4800180C 0x00000000 0x00003000	;# PUPDR
	mmw 0x48001820 0x03000000 0x0C000000	;# AFRL

	# PG12: P2_NCS, PF04: P2_CLK, PF12: P2_DQS, PG10: P2_IO7, PG09: P2_IO6, PG01: P2_IO5
	# PG00: P2_IO4, PF03: P2_IO3, PF02: P2_IO2, PF01: P2_IO1, PF00: P2_IO0

	# PF12:AF05:V, PF04:AF05:V, PF03:AF05:V, PF02:AF05:V, PF01:AF05:V, PF00:AF05:V
	# PG12:AF05:V, PG10:AF05:V, PG09:AF05:V, PG01:AF05:V, PG00:AF05:V

	# Port F: PF12:AF05:V, PF04:AF05:V, PF03:AF05:V, PF02:AF05:V, PF01:AF05:V, PF00:AF05:V
	mmw 0x48001400 0x020002AA 0x01000155	;# MODER
	mmw 0x48001408 0x030003FF 0x00000000	;# OSPEEDR
	mmw 0x4800140C 0x00000000 0x030003FF	;# PUPDR
	mmw 0x48001420 0x00055555 0x000AAAAA	;# AFRL
	mmw 0x48001424 0x00050000 0x000A0000	;# AFRH
	# Port G: PG12:AF05:V, PG10:AF05:V, PG09:AF05:V, PG01:AF05:V, PG00:AF05:V
	mmw 0x48001800 0x0228000A 0x01140005	;# MODER
	mmw 0x48001808 0x033C000F 0x00000000	;# OSPEEDR
	mmw 0x4800180C 0x00000000 0x033C000F	;# PUPDR
	mmw 0x48001820 0x00000055 0x000000AA	;# AFRL
	mmw 0x48001824 0x00050550 0x000A0AA0	;# AFRH

	# OCTOSPI1: memory-mapped 1-line read mode with 4-byte addresses
	mww 0xA0001130 0x00001000				;# OCTOSPI_LPTR: deactivate CS after 4096 clocks when FIFO is full
	mww 0xA0001000 0x3040000B				;# OCTOSPI_CR: FMODE=0x1, APMS=1, FTHRES=0, FSEL=0, DQM=0, TCEN=0
	mww 0xA0001008 0x01190100				;# OCTOSPI_DCR1: MTYP=0x1, FSIZE=0x19, CSHT=0x01, CKMODE=0, DLYBYP=0
	mww 0xA000100C 0x00000001				;# OCTOSPI_DCR2: PRESCALER=1

	mww 0xA0001108 0x00000000				;# OCTOSPI_TCR: SSHIFT=0, DHQC=0, DCYC=0x0
	mww 0xA0001100 0x01003101				;# OCTOSPI_CCR: DMODE=0x1, ABMODE=0x0, ADSIZE=0x3, ADMODE=0x1, ISIZE=0x0, IMODE=0x1
	mww 0xA0001110 0x00000013				;# OCTOSPI_IR: INSTR=READ4B

	if { $octo == 1 } {
		stmqspi cmd $a 1 0x71 0x00 0x00 0x00 0x00			;# Read Conf. Reg. 2, addr 0x00000000: DOPI, SOPI bits
		stmqspi cmd $a 0 0x06								;# Write Enable
		stmqspi cmd $a 1 0x05								;# Read Status Register
		stmqspi cmd $a 0 0x72 0x00 0x00 0x00 0x00 0x02		;# Write Conf. Reg. 2, addr 0x00000000: DTR OPI enable

		# OCTOSPI1: memory-mapped 8-line read mode with 4-byte addresses
		mww 0xA0001000 0x3040000B				;# OCTOSPI_CR: FMODE=0x3, APMS=1, FTHRES=0, FSEL=0, DQM=0, TCEN=1, EN=1
		mww 0xA0001108 0x10000006				;# OCTOSPI_TCR: SSHIFT=0, DHQC=1, DCYC=0x6
		mww 0xA0001100 0x2C003C1C				;# OCTOSPI_CCR: DTR, DMODE=0x4, ABMODE=0x0, ADSIZE=0x3, ADMODE=0x4, ISIZE=0x1, IMODE=0x4
		mww 0xA0001110 0x0000EE11				;# OCTOSPI_IR: INSTR=OCTA DTR Read

		flash probe $a							;# reload configuration from CR, TCR, CCR, IR register values

		stmqspi cmd $a 0 0x06								;# Write Enable
		stmqspi cmd $a 1 0x05 0x00 0x00 0x00 0x00			;# Read Status Register (note dummy address in 8-line mode)
		stmqspi cmd $a 0 0x04								;# Write Disable
		stmqspi cmd $a 1 0x05 0x00 0x00 0x00 0x00			;# Read Status Register (note dummy address in 8-line mode)
		stmqspi cmd $a 1 0x71 0x00 0x00 0x00 0x00			;# Read Conf. Reg. 2, addr 0x00000000: DOPI, SOPI bits
	}
}

$_TARGETNAME configure -event reset-init {
	mmw 0x40022000 0x00000003 0x0000000C	;# 3 WS for 72 MHz HCLK
	sleep 1
	mmw 0x40021000 0x00000100 0x00000000	;# HSI on
	mww 0x4002100C 0x01002432				;# RCC_PLLCFGR 72 MHz: PLLREN=1, PLLM=4, PLLN=36, PLLR=2, HSI
	mww 0x40021008 0x00008001				;# always HSI, APB1: /1, APB2: /1
	mmw 0x40021000 0x01000000 0x00000000	;# PLL on
	sleep 1
	mmw 0x40021008 0x00000003 0x00000000	;# switch to PLL
	sleep 1

	adapter speed 24000

	octospi_init 1
}

