blob: bdc08f92c5ce76d62078b9fd117d00bfa548d067 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* sysmocom quad-modem sysmoQMOD application code
2 *
3 * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
18 */
Harald Welteaa3b8672017-02-10 19:21:10 +010019#include "board.h"
20#include "simtrace.h"
21#include "utils.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010022#include "wwan_led.h"
23#include "wwan_perst.h"
Harald Welte5c583d32017-05-09 06:46:47 +020024#include "sim_switch.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010025#include "boardver_adc.h"
Harald Welte6d1128e2017-05-05 20:23:10 +020026#include "card_pres.h"
Harald Welte9d90d282018-06-29 22:25:42 +020027#include <osmocom/core/timer.h>
Harald Welte8e7fca32017-05-07 16:14:33 +020028#include "usb_buf.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010029
30static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
31static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
32static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP};
33static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
34static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
35
Harald Welte912b1832017-03-06 09:28:13 +010036/* array of generated USB Strings */
37extern unsigned char *usb_strings[];
38
Harald Welteaa3b8672017-02-10 19:21:10 +010039static int qmod_sam3_is_12(void)
40{
41 if (PIO_Get(&pin_1234_detect) == 0)
42 return 1;
43 else
44 return 0;
45}
46
47const unsigned char __eeprom_bin[256] = {
Harald Weltec6e482d2017-03-05 16:24:29 +010048 USB_VENDOR_OPENMOKO & 0xff,
49 USB_VENDOR_OPENMOKO >> 8,
50 USB_PRODUCT_QMOD_HUB & 0xff,
51 USB_PRODUCT_QMOD_HUB >> 8,
Harald Welte705e8992017-03-05 17:10:26 +010052 0x00, 0x00, 0x9b, 0x20, 0x09, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
Harald Welteaa3b8672017-02-10 19:21:10 +010053 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
54 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
55 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
56 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
57 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
58 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
68};
69
70#include "i2c.h"
71static int write_hub_eeprom(void)
72{
Harald Welteaa3b8672017-02-10 19:21:10 +010073 int i;
74
75 /* wait */
Harald Welte0e295982017-03-05 16:48:47 +010076 mdelay(100);
Harald Welteaa3b8672017-02-10 19:21:10 +010077
Harald Welteec0837c2017-03-03 01:33:24 +010078 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010079 /* write the EEPROM once */
Kévin Redona71a6f42018-07-24 09:54:12 +020080 for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) {
Harald Welteaa3b8672017-02-10 19:21:10 +010081 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
Kévin Redona71a6f42018-07-24 09:54:12 +020082 if (rc < 0) {
83 TRACE_ERROR("Writing EEPROM failed at byte %u: 0x%02x\n\r",
84 i, __eeprom_bin[i]);
85 return 1;
86 }
Harald Welteaa3b8672017-02-10 19:21:10 +010087 }
88
89 /* then pursue re-reading it again and again */
Harald Welteec0837c2017-03-03 01:33:24 +010090 TRACE_INFO("Verifying EEPROM...\n\r");
Kévin Redona71a6f42018-07-24 09:54:12 +020091 for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) {
Harald Welteaa3b8672017-02-10 19:21:10 +010092 int byte = eeprom_read_byte(0x50, i);
Harald Welteec0837c2017-03-03 01:33:24 +010093 TRACE_INFO("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +010094 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +010095 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +010096 i, __eeprom_bin[i], byte);
97 }
98
99 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
100 * again powering us up */
101
102 return 0;
103}
104
Harald Welte9547e412018-07-03 22:31:16 +0200105static int erase_hub_eeprom(void)
106{
107 int i;
108
109 /* wait */
110 mdelay(100);
111
112 TRACE_INFO("Erasing EEPROM...\n\r");
113 /* write the EEPROM once */
114 for (i = 0; i < 256; i++) {
115 int rc = eeprom_write_byte(0x50, i, 0xff);
Kévin Redonc4285162018-08-07 12:12:47 +0200116 if (rc < 0) {
117 TRACE_ERROR("Erasing EEPROM failed at byte %u: 0x%02x\n\r",
118 i, __eeprom_bin[i]);
119 return 1;
120 }
Harald Welte9547e412018-07-03 22:31:16 +0200121 }
122
123 return 0;
124}
125
126
Harald Welteed1efc52017-05-07 22:49:45 +0200127static void board_exec_dbg_cmd_st12only(int ch)
Harald Welteaa3b8672017-02-10 19:21:10 +0100128{
129 uint32_t addr, val;
130
Harald Welteed1efc52017-05-07 22:49:45 +0200131 /* functions below only work on primary (ST12) */
132 if (!qmod_sam3_is_12())
133 return;
134
135 switch (ch) {
136 case 'E':
137 write_hub_eeprom();
138 break;
Harald Welte9547e412018-07-03 22:31:16 +0200139 case 'e':
140 erase_hub_eeprom();
141 break;
Harald Welteed1efc52017-05-07 22:49:45 +0200142 case 'O':
143 printf("Setting PRTPWR_OVERRIDE\n\r");
144 PIO_Set(&pin_hubpwr_override);
145 break;
146 case 'o':
147 printf("Clearing PRTPWR_OVERRIDE\n\r");
148 PIO_Clear(&pin_hubpwr_override);
149 break;
150 case 'H':
151 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
152 PIO_Clear(&pin_hub_rst);
153 break;
154 case 'h':
155 /* high level drives transistor -> HUB_RESET low */
156 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
157 PIO_Set(&pin_hub_rst);
158 break;
159 case 'w':
160 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
161 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
162 printf("Please enter EEPROM offset:\n\r");
163 UART_GetIntegerMinMax(&addr, 0, 255);
164 printf("Please enter EEPROM value:\n\r");
165 UART_GetIntegerMinMax(&val, 0, 255);
166 printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr);
167 eeprom_write_byte(0x50, addr, val);
168 break;
169 case 'r':
170 printf("Please enter EEPROM offset:\n\r");
171 UART_GetIntegerMinMax(&addr, 0, 255);
172 printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
173 break;
174 default:
175 printf("Unknown command '%c'\n\r", ch);
176 break;
177 }
178}
179
180/* returns '1' in case we should break any endless loop */
181void board_exec_dbg_cmd(int ch)
182{
Harald Welteaa3b8672017-02-10 19:21:10 +0100183 switch (ch) {
184 case '?':
Harald Welteec0837c2017-03-03 01:33:24 +0100185 printf("\t?\thelp\n\r");
Harald Welteec0837c2017-03-03 01:33:24 +0100186 printf("\tR\treset SAM3\n\r");
Harald Welte9164a6d2017-05-05 20:55:21 +0200187 if (qmod_sam3_is_12()) {
188 printf("\tE\tprogram EEPROM\n\r");
Harald Welte9547e412018-07-03 22:31:16 +0200189 printf("\te\tErase EEPROM\n\r");
Harald Welte9164a6d2017-05-05 20:55:21 +0200190 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
191 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
192 printf("\tH\tRelease HUB RESET (high)\n\r");
193 printf("\th\tAssert HUB RESET (low)\n\r");
194 printf("\tw\tWrite single byte in EEPROM\n\r");
195 printf("\tr\tRead single byte from EEPROM\n\r");
196 }
Harald Welteec0837c2017-03-03 01:33:24 +0100197 printf("\tX\tRelease peer SAM3 from reset\n\r");
198 printf("\tx\tAssert peer SAM3 reset\n\r");
199 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
200 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
201 printf("\tU\tProceed to USB Initialization\n\r");
202 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
203 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100204 break;
Harald Welteaa3b8672017-02-10 19:21:10 +0100205 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100206 printf("Asking NVIC to reset us\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100207 USBD_Disconnect();
Harald Welteaa3b8672017-02-10 19:21:10 +0100208 NVIC_SystemReset();
209 break;
Harald Welte9164a6d2017-05-05 20:55:21 +0200210 case 'X':
211 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
212 PIO_Clear(&pin_peer_rst);
213 break;
214 case 'x':
215 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
216 PIO_Set(&pin_peer_rst);
217 break;
218 case 'Y':
219 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
220 PIO_Clear(&pin_peer_erase);
221 break;
222 case 'y':
223 printf("Seetting SIMTRACExx_ERASE (active)\n\r");
224 PIO_Set(&pin_peer_erase);
225 break;
226 case '1':
227 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welte44622df2017-05-11 00:04:50 +0200228 wwan_perst_do_reset_pulse(0, 300);
Harald Welte9164a6d2017-05-05 20:55:21 +0200229 break;
230 case '2':
231 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welte44622df2017-05-11 00:04:50 +0200232 wwan_perst_do_reset_pulse(1, 300);
Harald Welte9164a6d2017-05-05 20:55:21 +0200233 break;
234 case '!':
Harald Welte44622df2017-05-11 00:04:50 +0200235 sim_switch_use_physical(0, 0);
Harald Welte9164a6d2017-05-05 20:55:21 +0200236 break;
237 case '@':
Harald Welte44622df2017-05-11 00:04:50 +0200238 sim_switch_use_physical(0, 0);
Harald Welte9164a6d2017-05-05 20:55:21 +0200239 break;
240 default:
Harald Welteed1efc52017-05-07 22:49:45 +0200241 if (!qmod_sam3_is_12())
Harald Welte9164a6d2017-05-05 20:55:21 +0200242 printf("Unknown command '%c'\n\r", ch);
Harald Welteed1efc52017-05-07 22:49:45 +0200243 else
244 board_exec_dbg_cmd_st12only(ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100245 break;
246 }
247}
248
249void board_main_top(void)
250{
Harald Welte25399172017-05-11 00:47:29 +0200251#ifndef APPLICATION_dfu
Harald Welte8e7fca32017-05-07 16:14:33 +0200252 usb_buf_init();
253
Harald Welteaa3b8672017-02-10 19:21:10 +0100254 wwan_led_init();
255 wwan_perst_init();
Harald Welte5c583d32017-05-09 06:46:47 +0200256 sim_switch_init();
Harald Welte25399172017-05-11 00:47:29 +0200257#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100258
Harald Welte28174982017-05-07 16:45:10 +0200259 /* make sure we can detect whether running in ST12 or ST34 */
Harald Welteaa3b8672017-02-10 19:21:10 +0100260 PIO_Configure(&pin_1234_detect, 1);
Harald Welte28174982017-05-07 16:45:10 +0200261
262 if (qmod_sam3_is_12()) {
263 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
264 * pull-up on the input to keep SIMTRACE12 alive */
265 PIO_Configure(&pin_hubpwr_override, 1);
266 PIO_Configure(&pin_hub_rst, 1);
267 }
Harald Welteaa3b8672017-02-10 19:21:10 +0100268 PIO_Configure(&pin_peer_rst, 1);
269 PIO_Configure(&pin_peer_erase, 1);
Harald Welte25399172017-05-11 00:47:29 +0200270
271#ifndef APPLICATION_dfu
Harald Welteaa3b8672017-02-10 19:21:10 +0100272 i2c_pin_init();
Harald Welte25399172017-05-11 00:47:29 +0200273#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100274
275 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100276 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100277 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100278 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welte912b1832017-03-06 09:28:13 +0100279 /* make sure we use the second set of USB Strings
280 * calling the interfaces "Modem 3" and "Modem 4" rather
281 * than 1+2 */
282 usb_strings[7] = usb_strings[9];
283 usb_strings[8] = usb_strings[10];
Harald Welteaa3b8672017-02-10 19:21:10 +0100284 }
285
286 /* Obtain the circuit board version (currently just prints voltage */
287 get_board_version_adc();
Harald Welte25399172017-05-11 00:47:29 +0200288#ifndef APPLICATION_dfu
Harald Welte6d1128e2017-05-05 20:23:10 +0200289 /* Initialize checking for card insert/remove events */
290 card_present_init();
Harald Welte25399172017-05-11 00:47:29 +0200291#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100292}
Harald Welte27f5fc62017-11-28 22:15:56 +0100293
294static int uart_has_loopback_jumper(void)
295{
296 unsigned int i;
297 const Pin uart_loopback_pins[] = {
298 {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT},
299 {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
300 };
301
302 /* Configure UART pins as I/O */
303 PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins));
304
Kévin Redond24e9bd2018-07-07 14:56:43 +0200305 /* Send pattern over UART TX and check if it is received on RX
306 * If the loop doesn't get interrupted, RxD always follows TxD and thus a
307 * loopback jumper has been placed on RxD/TxD, and we will boot
308 * into DFU unconditionally
309 */
310 int has_loopback_jumper = 1;
Harald Welte27f5fc62017-11-28 22:15:56 +0100311 for (i = 0; i < 10; i++) {
312 /* Set TxD high; abort if RxD doesn't go high either */
313 PIO_Set(&uart_loopback_pins[1]);
Kévin Redond24e9bd2018-07-07 14:56:43 +0200314 if (!PIO_Get(&uart_loopback_pins[0])) {
315 has_loopback_jumper = 0;
316 break;
317 }
Harald Welte27f5fc62017-11-28 22:15:56 +0100318 /* Set TxD low, abort if RxD doesn't go low either */
319 PIO_Clear(&uart_loopback_pins[1]);
Kévin Redond24e9bd2018-07-07 14:56:43 +0200320 if (PIO_Get(&uart_loopback_pins[0])) {
321 has_loopback_jumper = 0;
322 break;
323 }
Harald Welte27f5fc62017-11-28 22:15:56 +0100324 }
Kévin Redond24e9bd2018-07-07 14:56:43 +0200325
326 /* Put pins back to UART mode */
327 const Pin uart_pins[] = {PINS_UART};
328 PIO_Configure(uart_pins, PIO_LISTSIZE(uart_pins));
329
330 return has_loopback_jumper;
Harald Welte27f5fc62017-11-28 22:15:56 +0100331}
332
333int board_override_enter_dfu(void)
334{
335 /* If the loopback jumper is set, we enter DFU mode */
336 if (uart_has_loopback_jumper())
337 return 1;
338
339 return 0;
340}