blob: a5392bb6e8f5259f3cc7dd7c85aa04e9e9b6581a [file] [log] [blame]
Harald Welteaa3b8672017-02-10 19:21:10 +01001/* Quad-modem speciic application code */
2/* (C) 2016-2016 by Harald Welte <laforge@gnumonks.org> */
3
4#include "board.h"
5#include "simtrace.h"
6#include "utils.h"
7#include "req_ctx.h"
8#include "wwan_led.h"
9#include "wwan_perst.h"
10#include "boardver_adc.h"
11#include "osmocom/core/timer.h"
12
13static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
14static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
15static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP};
16static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
17static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
18
19
20static int qmod_sam3_is_12(void)
21{
22 if (PIO_Get(&pin_1234_detect) == 0)
23 return 1;
24 else
25 return 0;
26}
27
28const unsigned char __eeprom_bin[256] = {
29 0x23, 0x42, 0x17, 0x25, 0x00, 0x00, 0x9b, 0x20, 0x01, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
30 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
31 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
32 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
33 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
34 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
35 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
45};
46
47#include "i2c.h"
48static int write_hub_eeprom(void)
49{
50 const unsigned int __eeprom_bin_len = 256;
51
52 int i;
53
54 /* wait */
55 volatile int v;
56 /* 440ns per cycle here */
57 for (i = 0; i < 1000000; i++) {
58 v = 0;
59 }
60
Harald Welteec0837c2017-03-03 01:33:24 +010061 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010062 /* write the EEPROM once */
63 for (i = 0; i < 256; i++) {
64 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
65 /* if the result was negative, repeat that write */
66 if (rc < 0)
67 i--;
68 }
69
70 /* then pursue re-reading it again and again */
Harald Welteec0837c2017-03-03 01:33:24 +010071 TRACE_INFO("Verifying EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010072 for (i = 0; i < 256; i++) {
73 int byte = eeprom_read_byte(0x50, i);
Harald Welteec0837c2017-03-03 01:33:24 +010074 TRACE_INFO("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +010075 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +010076 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +010077 i, __eeprom_bin[i], byte);
78 }
79
80 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
81 * again powering us up */
82
83 return 0;
84}
85
86/* returns '1' in case we should break any endless loop */
87void board_exec_dbg_cmd(int ch)
88{
89 uint32_t addr, val;
90
91 switch (ch) {
92 case '?':
Harald Welteec0837c2017-03-03 01:33:24 +010093 printf("\t?\thelp\n\r");
94 printf("\tE\tprogram EEPROM\n\r");
95 printf("\tR\treset SAM3\n\r");
96 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
97 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
98 printf("\tH\tRelease HUB RESET (high)\n\r");
99 printf("\th\tAssert HUB RESET (low)\n\r");
100 printf("\tw\tWrite single byte in EEPROM\n\r");
101 printf("\tr\tRead single byte from EEPROM\n\r");
102 printf("\tX\tRelease peer SAM3 from reset\n\r");
103 printf("\tx\tAssert peer SAM3 reset\n\r");
104 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
105 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
106 printf("\tU\tProceed to USB Initialization\n\r");
107 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
108 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100109 break;
110 case 'E':
111 write_hub_eeprom();
112 break;
113 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100114 printf("Asking NVIC to reset us\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100115 NVIC_SystemReset();
116 break;
117 case 'O':
Harald Welteec0837c2017-03-03 01:33:24 +0100118 printf("Setting PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100119 PIO_Set(&pin_hubpwr_override);
120 break;
121 case 'o':
Harald Welteec0837c2017-03-03 01:33:24 +0100122 printf("Clearing PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100123 PIO_Clear(&pin_hubpwr_override);
124 break;
125 case 'H':
Harald Welteec0837c2017-03-03 01:33:24 +0100126 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100127 PIO_Clear(&pin_hub_rst);
128 break;
129 case 'h':
130 /* high level drives transistor -> HUB_RESET low */
Harald Welteec0837c2017-03-03 01:33:24 +0100131 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100132 PIO_Set(&pin_hub_rst);
133 break;
134 case 'w':
135 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
Harald Welteec0837c2017-03-03 01:33:24 +0100136 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
137 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100138 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100139 printf("Please enter EEPROM value:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100140 UART_GetIntegerMinMax(&val, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100141 printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr);
Harald Welteaa3b8672017-02-10 19:21:10 +0100142 eeprom_write_byte(0x50, addr, val);
143 break;
144 case 'r':
Harald Welteec0837c2017-03-03 01:33:24 +0100145 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100146 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100147 printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
Harald Welteaa3b8672017-02-10 19:21:10 +0100148 break;
149 case 'X':
Harald Welteec0837c2017-03-03 01:33:24 +0100150 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100151 PIO_Clear(&pin_peer_rst);
152 break;
153 case 'x':
Harald Welteec0837c2017-03-03 01:33:24 +0100154 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100155 PIO_Set(&pin_peer_rst);
156 break;
157 case 'Y':
Harald Welteec0837c2017-03-03 01:33:24 +0100158 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100159 PIO_Clear(&pin_peer_erase);
160 break;
161 case 'y':
Harald Welteec0837c2017-03-03 01:33:24 +0100162 printf("Seetting SIMTRACExx_ERASE (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100163 PIO_Set(&pin_peer_erase);
164 break;
165 case '1':
Harald Welteec0837c2017-03-03 01:33:24 +0100166 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100167 wwan_perst_do_reset(1);
168 break;
169 case '2':
Harald Welteec0837c2017-03-03 01:33:24 +0100170 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100171 wwan_perst_do_reset(2);
172 break;
173 default:
Harald Welteec0837c2017-03-03 01:33:24 +0100174 printf("Unknown command '%c'\n\r", ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100175 break;
176 }
177}
178
179void board_main_top(void)
180{
181 wwan_led_init();
182 wwan_perst_init();
183
184 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
185 * pull-up on the input to keep SIMTRACE12 alive */
186 PIO_Configure(&pin_hubpwr_override, 1);
187 PIO_Configure(&pin_hub_rst, 1);
188 PIO_Configure(&pin_1234_detect, 1);
189 PIO_Configure(&pin_peer_rst, 1);
190 PIO_Configure(&pin_peer_erase, 1);
191 i2c_pin_init();
192
193 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100194 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100195 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100196 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100197 }
198
199 /* Obtain the circuit board version (currently just prints voltage */
200 get_board_version_adc();
201}