blob: 2f3d3ecc3a4c37fa84b14a323026382c2f7b5d21 [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
61 TRACE_INFO("Writing EEPROM...\r\n");
62 /* 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 */
71 TRACE_INFO("Verifying EEPROM...\r\n");
72 for (i = 0; i < 256; i++) {
73 int byte = eeprom_read_byte(0x50, i);
74 TRACE_INFO("0x%02x: %02x\r\n", i, byte);
75 if (byte != __eeprom_bin[i])
76 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\r\n",
77 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 '?':
93 printf("\t?\thelp\r\n");
94 printf("\tE\tprogram EEPROM\r\n");
95 printf("\tR\treset SAM3\r\n");
96 printf("\tO\tEnable PRTPWR_OVERRIDE\r\n");
97 printf("\to\tDisable PRTPWR_OVERRIDE\r\n");
98 printf("\tH\tRelease HUB RESET (high)\r\n");
99 printf("\th\tAssert HUB RESET (low)\r\n");
100 printf("\tw\tWrite single byte in EEPROM\r\n");
101 printf("\tr\tRead single byte from EEPROM\r\n");
102 printf("\tX\tRelease peer SAM3 from reset\r\n");
103 printf("\tx\tAssert peer SAM3 reset\r\n");
104 printf("\tY\tRelease peer SAM3 ERASE signal\r\n");
105 printf("\ty\tAssert peer SAM3 ERASE signal\r\n");
106 printf("\tU\tProceed to USB Initialization\r\n");
107 printf("\t1\tGenerate 1ms reset pulse on WWAN1\r\n");
108 printf("\t2\tGenerate 1ms reset pulse on WWAN2\r\n");
109 break;
110 case 'E':
111 write_hub_eeprom();
112 break;
113 case 'R':
114 printf("Asking NVIC to reset us\r\n");
115 NVIC_SystemReset();
116 break;
117 case 'O':
118 printf("Setting PRTPWR_OVERRIDE\r\n");
119 PIO_Set(&pin_hubpwr_override);
120 break;
121 case 'o':
122 printf("Clearing PRTPWR_OVERRIDE\r\n");
123 PIO_Clear(&pin_hubpwr_override);
124 break;
125 case 'H':
126 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\r\n");
127 PIO_Clear(&pin_hub_rst);
128 break;
129 case 'h':
130 /* high level drives transistor -> HUB_RESET low */
131 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\r\n");
132 PIO_Set(&pin_hub_rst);
133 break;
134 case 'w':
135 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
136 printf("WARNING: attempting EEPROM access while HUB not in reset\r\n");
137 printf("Please enter EEPROM offset:\r\n");
138 UART_GetIntegerMinMax(&addr, 0, 255);
139 printf("Please enter EEPROM value:\r\n");
140 UART_GetIntegerMinMax(&val, 0, 255);
141 printf("Writing value 0x%02x to EEPROM offset 0x%02x\r\n", val, addr);
142 eeprom_write_byte(0x50, addr, val);
143 break;
144 case 'r':
145 printf("Please enter EEPROM offset:\r\n");
146 UART_GetIntegerMinMax(&addr, 0, 255);
147 printf("EEPROM[0x%02x] = 0x%02x\r\n", addr, eeprom_read_byte(0x50, addr));
148 break;
149 case 'X':
150 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\r\n");
151 PIO_Clear(&pin_peer_rst);
152 break;
153 case 'x':
154 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\r\n");
155 PIO_Set(&pin_peer_rst);
156 break;
157 case 'Y':
158 printf("Clearing SIMTRACExx_ERASE (inactive)\r\n");
159 PIO_Clear(&pin_peer_erase);
160 break;
161 case 'y':
162 printf("Seetting SIMTRACExx_ERASE (active)\r\n");
163 PIO_Set(&pin_peer_erase);
164 break;
165 case '1':
166 printf("Resetting Modem 1 (of this SAM3)\r\n");
167 wwan_perst_do_reset(1);
168 break;
169 case '2':
170 printf("Resetting Modem 2 (of this SAM3)\r\n");
171 wwan_perst_do_reset(2);
172 break;
173 default:
174 printf("Unknown command '%c'\r\n", ch);
175 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()) {
194 TRACE_INFO("Detected Quad-Modem ST12\r\n");
195 } else {
196 TRACE_INFO("Detected Quad-Modem ST34\r\n");
197 }
198
199 /* Obtain the circuit board version (currently just prints voltage */
200 get_board_version_adc();
201}