remove stale programmer.py
[litex.git] / software / videomixer / config.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <generated/mem.h>
4
5 #include "config.h"
6
7 #define FLASH_BLOCK_SIZE (128*1024)
8 #define FLASH_OFFSET_CONFIG (FLASH_BOOT_ADDRESS + FLASH_BLOCK_SIZE)
9
10 static volatile unsigned short *flash_config = (unsigned short *)(0x80000000 | FLASH_OFFSET_CONFIG);
11
12 static void wait_program(void)
13 {
14 while(!(*flash_config & 0x0080)); /* Read status register */
15 *flash_config = 0x0050; /* Clear status register */
16 *flash_config = 0x00ff; /* Go to Read Array mode */
17 }
18
19 static void config_erase_block(void)
20 {
21 *flash_config = 0x0020; /* Setup Erase */
22 *flash_config = 0x00d0; /* Confirm Erase */
23 wait_program();
24 }
25
26 static void config_write(int offset, unsigned short data)
27 {
28 flash_config[offset] = 0x0040; /* Word Program */
29 flash_config[offset] = data;
30 wait_program();
31 }
32
33 static const unsigned char config_defaults[CONFIG_KEY_COUNT] = CONFIG_DEFAULTS;
34 static int config_record_count;
35 static unsigned char config_values[CONFIG_KEY_COUNT];
36
37 static int config_process_record(unsigned char key, unsigned char value)
38 {
39 if(key >= CONFIG_KEY_COUNT)
40 return 0;
41 config_record_count++;
42 config_values[key] = value;
43 return 1;
44 }
45
46 void config_init(void)
47 {
48 volatile unsigned int *flash_config32 = (unsigned int *)flash_config;
49 int i;
50 unsigned int flash_word;
51
52 memcpy(config_values, config_defaults, CONFIG_KEY_COUNT);
53
54 for(i=0;i<FLASH_BLOCK_SIZE/4;i++) {
55 flash_word = flash_config32[i];
56 if(!config_process_record((flash_word >> 24) & 0xff, (flash_word >> 16) & 0xff))
57 break;
58 if(!config_process_record((flash_word >> 8) & 0xff, flash_word & 0xff))
59 break;
60 }
61 }
62
63 void config_write_all(void)
64 {
65 int i;
66
67 config_erase_block();
68 config_record_count = 0;
69 for(i=0;i<CONFIG_KEY_COUNT;i++) {
70 if(config_values[i] != config_defaults[i]) {
71 config_write(config_record_count, (i << 8) | config_values[i]);
72 config_record_count++;
73 }
74 }
75 }
76
77 unsigned char config_get(unsigned char key)
78 {
79 return config_values[key];
80 }
81
82 void config_set(unsigned char key, unsigned char value)
83 {
84 if(config_values[key] == value)
85 return;
86 config_values[key] = value;
87 if(config_record_count < FLASH_BLOCK_SIZE/2)
88 config_write(config_record_count++, (key << 8) | value);
89 else
90 config_write_all();
91 }