blob: 9073489e8ad43ef5fbd8e0da6f9d4b93e92b06c5 [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"
Kévin Redon1dbcf622018-09-04 16:15:10 +020022#include "led.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010023#include "wwan_led.h"
24#include "wwan_perst.h"
Harald Welte5c583d32017-05-09 06:46:47 +020025#include "sim_switch.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010026#include "boardver_adc.h"
Harald Welte6d1128e2017-05-05 20:23:10 +020027#include "card_pres.h"
Harald Welte9d90d282018-06-29 22:25:42 +020028#include <osmocom/core/timer.h>
Harald Welte8e7fca32017-05-07 16:14:33 +020029#include "usb_buf.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010030
31static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
32static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
33static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP};
34static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
35static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
36
Harald Welte912b1832017-03-06 09:28:13 +010037/* array of generated USB Strings */
38extern unsigned char *usb_strings[];
39
Harald Welteaa3b8672017-02-10 19:21:10 +010040static int qmod_sam3_is_12(void)
41{
42 if (PIO_Get(&pin_1234_detect) == 0)
43 return 1;
44 else
45 return 0;
46}
47
48const unsigned char __eeprom_bin[256] = {
Harald Weltec6e482d2017-03-05 16:24:29 +010049 USB_VENDOR_OPENMOKO & 0xff,
50 USB_VENDOR_OPENMOKO >> 8,
51 USB_PRODUCT_QMOD_HUB & 0xff,
52 USB_PRODUCT_QMOD_HUB >> 8,
Harald Welte705e8992017-03-05 17:10:26 +010053 0x00, 0x00, 0x9b, 0x20, 0x09, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
Harald Welteaa3b8672017-02-10 19:21:10 +010054 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
55 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
56 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
57 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
58 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
59 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
69};
70
71#include "i2c.h"
72static int write_hub_eeprom(void)
73{
Harald Welteaa3b8672017-02-10 19:21:10 +010074 int i;
75
76 /* wait */
Harald Welte0e295982017-03-05 16:48:47 +010077 mdelay(100);
Harald Welteaa3b8672017-02-10 19:21:10 +010078
Harald Welteec0837c2017-03-03 01:33:24 +010079 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010080 /* write the EEPROM once */
Kévin Redona71a6f42018-07-24 09:54:12 +020081 for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) {
Harald Welteaa3b8672017-02-10 19:21:10 +010082 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
Kévin Redona71a6f42018-07-24 09:54:12 +020083 if (rc < 0) {
84 TRACE_ERROR("Writing EEPROM failed at byte %u: 0x%02x\n\r",
85 i, __eeprom_bin[i]);
86 return 1;
87 }
Harald Welteaa3b8672017-02-10 19:21:10 +010088 }
89
90 /* then pursue re-reading it again and again */
Harald Welteec0837c2017-03-03 01:33:24 +010091 TRACE_INFO("Verifying EEPROM...\n\r");
Kévin Redona71a6f42018-07-24 09:54:12 +020092 for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) {
Harald Welteaa3b8672017-02-10 19:21:10 +010093 int byte = eeprom_read_byte(0x50, i);
Kévin Redona6bd7172018-08-28 19:41:10 +020094 TRACE_DEBUG("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +010095 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +010096 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +010097 i, __eeprom_bin[i], byte);
98 }
Kévin Redona6bd7172018-08-28 19:41:10 +020099 TRACE_INFO("EEPROM written\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100100
101 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
102 * again powering us up */
103
104 return 0;
105}
106
Harald Welte9547e412018-07-03 22:31:16 +0200107static int erase_hub_eeprom(void)
108{
109 int i;
110
111 /* wait */
112 mdelay(100);
113
114 TRACE_INFO("Erasing EEPROM...\n\r");
115 /* write the EEPROM once */
116 for (i = 0; i < 256; i++) {
117 int rc = eeprom_write_byte(0x50, i, 0xff);
Kévin Redonc4285162018-08-07 12:12:47 +0200118 if (rc < 0) {
119 TRACE_ERROR("Erasing EEPROM failed at byte %u: 0x%02x\n\r",
120 i, __eeprom_bin[i]);
121 return 1;
122 }
Harald Welte9547e412018-07-03 22:31:16 +0200123 }
Kévin Redona6bd7172018-08-28 19:41:10 +0200124 TRACE_INFO("EEPROM erased\n\r");
Harald Welte9547e412018-07-03 22:31:16 +0200125
126 return 0;
127}
128
129
Harald Welteed1efc52017-05-07 22:49:45 +0200130static void board_exec_dbg_cmd_st12only(int ch)
Harald Welteaa3b8672017-02-10 19:21:10 +0100131{
132 uint32_t addr, val;
133
Harald Welteed1efc52017-05-07 22:49:45 +0200134 /* functions below only work on primary (ST12) */
135 if (!qmod_sam3_is_12())
136 return;
137
138 switch (ch) {
139 case 'E':
140 write_hub_eeprom();
141 break;
Harald Welte9547e412018-07-03 22:31:16 +0200142 case 'e':
143 erase_hub_eeprom();
144 break;
Harald Welteed1efc52017-05-07 22:49:45 +0200145 case 'O':
146 printf("Setting PRTPWR_OVERRIDE\n\r");
147 PIO_Set(&pin_hubpwr_override);
148 break;
149 case 'o':
150 printf("Clearing PRTPWR_OVERRIDE\n\r");
151 PIO_Clear(&pin_hubpwr_override);
152 break;
153 case 'H':
154 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
155 PIO_Clear(&pin_hub_rst);
156 break;
157 case 'h':
158 /* high level drives transistor -> HUB_RESET low */
159 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
160 PIO_Set(&pin_hub_rst);
161 break;
162 case 'w':
163 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
164 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
165 printf("Please enter EEPROM offset:\n\r");
166 UART_GetIntegerMinMax(&addr, 0, 255);
167 printf("Please enter EEPROM value:\n\r");
168 UART_GetIntegerMinMax(&val, 0, 255);
Kévin Redone5f891a2018-09-04 13:31:26 +0200169 printf("Writing value 0x%02lx to EEPROM offset 0x%02lx\n\r", val, addr);
Harald Welteed1efc52017-05-07 22:49:45 +0200170 eeprom_write_byte(0x50, addr, val);
171 break;
172 case 'r':
173 printf("Please enter EEPROM offset:\n\r");
174 UART_GetIntegerMinMax(&addr, 0, 255);
Kévin Redone5f891a2018-09-04 13:31:26 +0200175 printf("EEPROM[0x%02lx] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
Harald Welteed1efc52017-05-07 22:49:45 +0200176 break;
177 default:
178 printf("Unknown command '%c'\n\r", ch);
179 break;
180 }
181}
182
183/* returns '1' in case we should break any endless loop */
184void board_exec_dbg_cmd(int ch)
185{
Harald Welteaa3b8672017-02-10 19:21:10 +0100186 switch (ch) {
187 case '?':
Harald Welteec0837c2017-03-03 01:33:24 +0100188 printf("\t?\thelp\n\r");
Harald Welteec0837c2017-03-03 01:33:24 +0100189 printf("\tR\treset SAM3\n\r");
Kévin Redon1dbcf622018-09-04 16:15:10 +0200190 printf("\tl\tswitch off LED 1\n\r");
191 printf("\tL\tswitch off LED 1\n\r");
192 printf("\tg\tswitch off LED 2\n\r");
193 printf("\tG\tswitch off LED 2\n\r");
Harald Welte9164a6d2017-05-05 20:55:21 +0200194 if (qmod_sam3_is_12()) {
195 printf("\tE\tprogram EEPROM\n\r");
Harald Welte9547e412018-07-03 22:31:16 +0200196 printf("\te\tErase EEPROM\n\r");
Harald Welte9164a6d2017-05-05 20:55:21 +0200197 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
198 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
199 printf("\tH\tRelease HUB RESET (high)\n\r");
200 printf("\th\tAssert HUB RESET (low)\n\r");
201 printf("\tw\tWrite single byte in EEPROM\n\r");
202 printf("\tr\tRead single byte from EEPROM\n\r");
203 }
Harald Welteec0837c2017-03-03 01:33:24 +0100204 printf("\tX\tRelease peer SAM3 from reset\n\r");
205 printf("\tx\tAssert peer SAM3 reset\n\r");
206 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
207 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
208 printf("\tU\tProceed to USB Initialization\n\r");
209 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
210 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100211 break;
Harald Welteaa3b8672017-02-10 19:21:10 +0100212 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100213 printf("Asking NVIC to reset us\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100214 USBD_Disconnect();
Harald Welteaa3b8672017-02-10 19:21:10 +0100215 NVIC_SystemReset();
216 break;
Kévin Redon1dbcf622018-09-04 16:15:10 +0200217 case 'l':
218 led_blink(LED_GREEN, BLINK_ALWAYS_OFF);
219 printf("LED 1 switched off\n\r");
220 break;
221 case 'L':
222 led_blink(LED_GREEN, BLINK_ALWAYS_ON);
223 printf("LED 1 switched on\n\r");
224 break;
225 case 'g':
226 led_blink(LED_RED, BLINK_ALWAYS_OFF);
227 printf("LED 2 switched off\n\r");
228 break;
229 case 'G':
230 led_blink(LED_RED, BLINK_ALWAYS_ON);
231 printf("LED 2 switched on\n\r");
232 break;
Harald Welte9164a6d2017-05-05 20:55:21 +0200233 case 'X':
234 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
235 PIO_Clear(&pin_peer_rst);
236 break;
237 case 'x':
238 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
239 PIO_Set(&pin_peer_rst);
240 break;
241 case 'Y':
242 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
243 PIO_Clear(&pin_peer_erase);
244 break;
245 case 'y':
246 printf("Seetting SIMTRACExx_ERASE (active)\n\r");
247 PIO_Set(&pin_peer_erase);
248 break;
249 case '1':
250 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welte44622df2017-05-11 00:04:50 +0200251 wwan_perst_do_reset_pulse(0, 300);
Harald Welte9164a6d2017-05-05 20:55:21 +0200252 break;
253 case '2':
254 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welte44622df2017-05-11 00:04:50 +0200255 wwan_perst_do_reset_pulse(1, 300);
Harald Welte9164a6d2017-05-05 20:55:21 +0200256 break;
257 case '!':
Harald Welte44622df2017-05-11 00:04:50 +0200258 sim_switch_use_physical(0, 0);
Harald Welte9164a6d2017-05-05 20:55:21 +0200259 break;
260 case '@':
Harald Welte44622df2017-05-11 00:04:50 +0200261 sim_switch_use_physical(0, 0);
Harald Welte9164a6d2017-05-05 20:55:21 +0200262 break;
263 default:
Harald Welteed1efc52017-05-07 22:49:45 +0200264 if (!qmod_sam3_is_12())
Harald Welte9164a6d2017-05-05 20:55:21 +0200265 printf("Unknown command '%c'\n\r", ch);
Harald Welteed1efc52017-05-07 22:49:45 +0200266 else
267 board_exec_dbg_cmd_st12only(ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100268 break;
269 }
270}
271
272void board_main_top(void)
273{
Harald Welte25399172017-05-11 00:47:29 +0200274#ifndef APPLICATION_dfu
Harald Welte8e7fca32017-05-07 16:14:33 +0200275 usb_buf_init();
276
Harald Welteaa3b8672017-02-10 19:21:10 +0100277 wwan_led_init();
278 wwan_perst_init();
Harald Welte5c583d32017-05-09 06:46:47 +0200279 sim_switch_init();
Harald Welte25399172017-05-11 00:47:29 +0200280#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100281
Harald Welte28174982017-05-07 16:45:10 +0200282 /* make sure we can detect whether running in ST12 or ST34 */
Harald Welteaa3b8672017-02-10 19:21:10 +0100283 PIO_Configure(&pin_1234_detect, 1);
Harald Welte28174982017-05-07 16:45:10 +0200284
285 if (qmod_sam3_is_12()) {
286 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
287 * pull-up on the input to keep SIMTRACE12 alive */
288 PIO_Configure(&pin_hubpwr_override, 1);
289 PIO_Configure(&pin_hub_rst, 1);
290 }
Harald Welteaa3b8672017-02-10 19:21:10 +0100291 PIO_Configure(&pin_peer_rst, 1);
292 PIO_Configure(&pin_peer_erase, 1);
Harald Welte25399172017-05-11 00:47:29 +0200293
294#ifndef APPLICATION_dfu
Harald Welteaa3b8672017-02-10 19:21:10 +0100295 i2c_pin_init();
Harald Welte25399172017-05-11 00:47:29 +0200296#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100297
298 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100299 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100300 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100301 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welte912b1832017-03-06 09:28:13 +0100302 /* make sure we use the second set of USB Strings
303 * calling the interfaces "Modem 3" and "Modem 4" rather
304 * than 1+2 */
305 usb_strings[7] = usb_strings[9];
306 usb_strings[8] = usb_strings[10];
Harald Welteaa3b8672017-02-10 19:21:10 +0100307 }
308
309 /* Obtain the circuit board version (currently just prints voltage */
310 get_board_version_adc();
Harald Welte25399172017-05-11 00:47:29 +0200311#ifndef APPLICATION_dfu
Harald Welte6d1128e2017-05-05 20:23:10 +0200312 /* Initialize checking for card insert/remove events */
313 card_present_init();
Harald Welte25399172017-05-11 00:47:29 +0200314#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100315}
Harald Welte27f5fc62017-11-28 22:15:56 +0100316
317static int uart_has_loopback_jumper(void)
318{
319 unsigned int i;
320 const Pin uart_loopback_pins[] = {
321 {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT},
322 {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
323 };
324
325 /* Configure UART pins as I/O */
326 PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins));
327
Kévin Redond24e9bd2018-07-07 14:56:43 +0200328 /* Send pattern over UART TX and check if it is received on RX
329 * If the loop doesn't get interrupted, RxD always follows TxD and thus a
330 * loopback jumper has been placed on RxD/TxD, and we will boot
331 * into DFU unconditionally
332 */
333 int has_loopback_jumper = 1;
Harald Welte27f5fc62017-11-28 22:15:56 +0100334 for (i = 0; i < 10; i++) {
335 /* Set TxD high; abort if RxD doesn't go high either */
336 PIO_Set(&uart_loopback_pins[1]);
Kévin Redond24e9bd2018-07-07 14:56:43 +0200337 if (!PIO_Get(&uart_loopback_pins[0])) {
338 has_loopback_jumper = 0;
339 break;
340 }
Harald Welte27f5fc62017-11-28 22:15:56 +0100341 /* Set TxD low, abort if RxD doesn't go low either */
342 PIO_Clear(&uart_loopback_pins[1]);
Kévin Redond24e9bd2018-07-07 14:56:43 +0200343 if (PIO_Get(&uart_loopback_pins[0])) {
344 has_loopback_jumper = 0;
345 break;
346 }
Harald Welte27f5fc62017-11-28 22:15:56 +0100347 }
Kévin Redond24e9bd2018-07-07 14:56:43 +0200348
349 /* Put pins back to UART mode */
350 const Pin uart_pins[] = {PINS_UART};
351 PIO_Configure(uart_pins, PIO_LISTSIZE(uart_pins));
352
353 return has_loopback_jumper;
Harald Welte27f5fc62017-11-28 22:15:56 +0100354}
355
356int board_override_enter_dfu(void)
357{
358 /* If the loopback jumper is set, we enter DFU mode */
359 if (uart_has_loopback_jumper())
360 return 1;
361
362 return 0;
363}