From a013ca80f5a15be97ea3dafb0606527de770bd1e Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 5 Feb 2020 11:28:03 -0700 Subject: [PATCH] Complete rewrite of scratch to support IT5570 --- src/board/system76/lemp9/board.mk | 5 ++ src/board/system76/lemp9/scratch.c | 53 ++++--------------- .../lemp9/scratch/include/scratch/pmc.h | 4 -- src/board/system76/lemp9/scratch/main.c | 24 +++------ src/board/system76/lemp9/scratch/pmc.c | 4 -- src/board/system76/lemp9/scratch/scratch.mk | 22 ++------ src/board/system76/lemp9/scratch/stdio.c | 20 ------- 7 files changed, 23 insertions(+), 109 deletions(-) delete mode 100644 src/board/system76/lemp9/scratch/stdio.c diff --git a/src/board/system76/lemp9/board.mk b/src/board/system76/lemp9/board.mk index 7773bec..4a37e7a 100644 --- a/src/board/system76/lemp9/board.mk +++ b/src/board/system76/lemp9/board.mk @@ -19,6 +19,11 @@ CFLAGS+=-DI2C_DEBUGGER=0x76 # Set battery I2C bus CFLAGS+=-DI2C_SMBUS=I2C_4 +# Set scratch ROM parameters +SCRATCH_OFFSET=1024 +SCRATCH_SIZE=1024 +CFLAGS+=-DSCRATCH_OFFSET=$(SCRATCH_OFFSET) -DSCRATCH_SIZE=$(SCRATCH_SIZE) + # Add scratch ROM source SCRATCH_DIR=$(BOARD_DIR)/scratch SCRATCH_SRC=$(wildcard $(SCRATCH_DIR)/*.c) diff --git a/src/board/system76/lemp9/scratch.c b/src/board/system76/lemp9/scratch.c index 2221b52..ba321a0 100644 --- a/src/board/system76/lemp9/scratch.c +++ b/src/board/system76/lemp9/scratch.c @@ -1,63 +1,28 @@ #include <8051.h> -#include #include #include -// Include scratch ROM, must be less than 4096 bytes, controlled by makefile -uint8_t __code scratch_rom[] = { +// Include scratch ROM +uint8_t __code __at(SCRATCH_OFFSET) scratch_rom[] = { #include }; -// Scratch RAM 0 is 4096 bytes, located at 0x0000 bytes -volatile uint8_t __xdata __at(0x0000) scratch_ram[4096]; - volatile uint8_t __xdata __at(0x1040) SCAR0L; volatile uint8_t __xdata __at(0x1041) SCAR0M; volatile uint8_t __xdata __at(0x1042) SCAR0H; -// Create new segment located at 0x1000, after scratch ROM mapping -static void scratch_start(void) __naked { - __asm - .area SCRATCH (ABS,CODE) - __endasm; - __asm - .org 0x1000 - __endasm; -} - // Enter or exit scratch ROM void scratch_trampoline(void) { - // Uses SCAR0 which is mapped at 0x0000 in data space and are - // 4096 bytes in size. - // Disable interrupts EA = 0; - // Ensure pdata is not used! - int __data i; - // Copy scratch ROM - for (i = 0; i < ARRAY_SIZE(scratch_rom) && i < ARRAY_SIZE(scratch_ram); i++) { - scratch_ram[i] = scratch_rom[i]; - } + // Use DMA mapping to copy flash data + SCAR0H = 0x80; + SCAR0L = (uint8_t)(SCRATCH_OFFSET); + SCAR0M = (uint8_t)(SCRATCH_OFFSET >> 8); + SCAR0H = 0; - // Fill the rest with nop - for (; i < ARRAY_SIZE(scratch_ram); i++) { - scratch_ram[i] = 0x00; - } - - // Set scratch RAM 0 mapping at 0x0000 and enable - SCAR0L = 0x00; - SCAR0M = 0x00; - SCAR0H = 0x00; - - // Jump to reset function - __asm__("ljmp 0"); -} - -// Finish segment located at 0x1000 -static void scratch_end(void) __naked { - __asm - .area CSEG (REL,CODE) - __endasm; + // Jump to scratch reset function + __asm__("ljmp " xstr(SCRATCH_OFFSET)); } diff --git a/src/board/system76/lemp9/scratch/include/scratch/pmc.h b/src/board/system76/lemp9/scratch/include/scratch/pmc.h index 7959f5f..4a23403 100644 --- a/src/board/system76/lemp9/scratch/include/scratch/pmc.h +++ b/src/board/system76/lemp9/scratch/include/scratch/pmc.h @@ -15,11 +15,7 @@ struct Pmc { volatile uint8_t * control; }; -extern struct Pmc __code PMC_1; -extern struct Pmc __code PMC_2; extern struct Pmc __code PMC_3; -extern struct Pmc __code PMC_4; -extern struct Pmc __code PMC_5; #define PMC_STS_OBF (1 << 0) #define PMC_STS_IBF (1 << 1) diff --git a/src/board/system76/lemp9/scratch/main.c b/src/board/system76/lemp9/scratch/main.c index a1199cf..98b94fe 100644 --- a/src/board/system76/lemp9/scratch/main.c +++ b/src/board/system76/lemp9/scratch/main.c @@ -1,5 +1,4 @@ #include -#include #include #include @@ -81,22 +80,12 @@ void pmc_event(struct Pmc * pmc) { state = PMC_STATE_ACPI_WRITE; break; case 0xEC: - printf_tiny("RESET\n"); - // Attempt to trigger watchdog reset - ETWCFG |= (1 << 5); - EWDKEYR = 0; - // Clear processor caches - __asm__("mov 0xf7, #1"); - __asm__("nop"); - __asm__("mov 0xf7, #1"); - __asm__("nop"); - __asm__("mov 0xf7, #1"); - __asm__("nop"); - __asm__("mov 0xf7, #1"); - __asm__("nop"); - // Exit scratch ROM by going through trampoline - __asm__("ljmp 0x1000"); - break; + for (;;) { + // Attempt to trigger watchdog reset + ETWCFG |= (1 << 5); + EWDKEYR = 0; + } + break; } } else { switch (state) { @@ -123,7 +112,6 @@ void pmc_event(struct Pmc * pmc) { // Main program while running in scratch ROM void main(void) { - printf_tiny("SCRATCH\n"); for (;;) { pmc_event(&PMC_3); } diff --git a/src/board/system76/lemp9/scratch/pmc.c b/src/board/system76/lemp9/scratch/pmc.c index bc1777c..6db6fb5 100644 --- a/src/board/system76/lemp9/scratch/pmc.c +++ b/src/board/system76/lemp9/scratch/pmc.c @@ -7,11 +7,7 @@ .control = &PM ## NUM ## CTL, \ } -struct Pmc __code PMC_1 = PMC(1); -struct Pmc __code PMC_2 = PMC(2); struct Pmc __code PMC_3 = PMC(3); -struct Pmc __code PMC_4 = PMC(4); -struct Pmc __code PMC_5 = PMC(5); uint8_t pmc_status(struct Pmc * pmc) { return *(pmc->status); diff --git a/src/board/system76/lemp9/scratch/scratch.mk b/src/board/system76/lemp9/scratch/scratch.mk index e0949f3..8f61c65 100644 --- a/src/board/system76/lemp9/scratch/scratch.mk +++ b/src/board/system76/lemp9/scratch/scratch.mk @@ -1,33 +1,17 @@ -# Enable I2C debugging -SCRATCH_SRC+=\ - src/common/i2c.c \ - src/ec/$(EC)/i2c.c -SCRATCH_INCLUDE+=\ - src/common/include/common/*.h \ - src/ec/$(EC)/include/ec/*.h -SCRATCH_CFLAGS+=\ - -Isrc/common/include \ - -Isrc/ec/$(EC)/include - -# Enable I2C debug on 0x76 -SCRATCH_CFLAGS+=-DI2C_DEBUGGER=0x76 - -# Set battery I2C bus -SCRATCH_CFLAGS+=-DI2C_SMBUS=I2C_4 - SCRATCH_BUILD=$(BUILD)/scratch SCRATCH_OBJ=$(patsubst src/%.c,$(SCRATCH_BUILD)/%.rel,$(SCRATCH_SRC)) SCRATCH_CC=\ sdcc \ -mmcs51 \ --model-small \ - --code-size 4096 \ + --code-loc $(SCRATCH_OFFSET) \ + --code-size $(SCRATCH_SIZE) \ --Werror # Convert from binary file to C header $(BUILD)/include/scratch.h: $(SCRATCH_BUILD)/scratch.rom @mkdir -p $(@D) - xxd --include < $< > $@ + xxd -s $(SCRATCH_OFFSET) --include < $< > $@ # Convert from Intel Hex file to binary file $(SCRATCH_BUILD)/scratch.rom: $(SCRATCH_BUILD)/scratch.ihx diff --git a/src/board/system76/lemp9/scratch/stdio.c b/src/board/system76/lemp9/scratch/stdio.c deleted file mode 100644 index 9bdb177..0000000 --- a/src/board/system76/lemp9/scratch/stdio.c +++ /dev/null @@ -1,20 +0,0 @@ -#include - -#ifdef SERIAL_DEBUGGER - #include -#endif - -#ifdef I2C_DEBUGGER - #include -#endif - -int putchar(int c) { - unsigned char byte = (unsigned char)c; -#ifdef SERIAL_DEBUGGER - SBUF = byte; -#endif -#ifdef I2C_DEBUGGER - i2c_send(&I2C_SMBUS, I2C_DEBUGGER, &byte, 1); -#endif - return (int)byte; -}