blob: 2191a62e402b721c8a7c6a8151cc20672a346cb8 [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
Harald Welte809e5842017-03-06 09:16:40 +010019static const Pin pin_conn_usim1 = {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT};
20static const Pin pin_conn_usim2 = {PIO_PA28, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT};
21
Harald Welte912b1832017-03-06 09:28:13 +010022/* array of generated USB Strings */
23extern unsigned char *usb_strings[];
24
Harald Welte809e5842017-03-06 09:16:40 +010025static void qmod_use_physical_sim(unsigned int nr, int physical)
26{
27 const Pin *pin;
28
29 TRACE_INFO("Modem %d: %s SIM\n\r", nr,
30 physical ? "physical" : "virtual");
31
32 switch (nr) {
33 case 1:
34 pin = &pin_conn_usim1;
35 break;
36 case 2:
37 pin = &pin_conn_usim2;
38 break;
39 default:
40 TRACE_ERROR("Invalid SIM%u\n\r", nr);
41 return;
42 }
43
44 if (physical)
45 PIO_Set(pin);
46 else
47 PIO_Clear(pin);
48}
Harald Welteaa3b8672017-02-10 19:21:10 +010049
50static int qmod_sam3_is_12(void)
51{
52 if (PIO_Get(&pin_1234_detect) == 0)
53 return 1;
54 else
55 return 0;
56}
57
58const unsigned char __eeprom_bin[256] = {
Harald Weltec6e482d2017-03-05 16:24:29 +010059 USB_VENDOR_OPENMOKO & 0xff,
60 USB_VENDOR_OPENMOKO >> 8,
61 USB_PRODUCT_QMOD_HUB & 0xff,
62 USB_PRODUCT_QMOD_HUB >> 8,
Harald Welte705e8992017-03-05 17:10:26 +010063 0x00, 0x00, 0x9b, 0x20, 0x09, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
Harald Welteaa3b8672017-02-10 19:21:10 +010064 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
65 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
66 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
67 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
68 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
69 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
79};
80
81#include "i2c.h"
82static int write_hub_eeprom(void)
83{
84 const unsigned int __eeprom_bin_len = 256;
85
86 int i;
87
88 /* wait */
Harald Welte0e295982017-03-05 16:48:47 +010089 mdelay(100);
Harald Welteaa3b8672017-02-10 19:21:10 +010090
Harald Welteec0837c2017-03-03 01:33:24 +010091 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010092 /* write the EEPROM once */
93 for (i = 0; i < 256; i++) {
94 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
95 /* if the result was negative, repeat that write */
96 if (rc < 0)
97 i--;
98 }
99
100 /* then pursue re-reading it again and again */
Harald Welteec0837c2017-03-03 01:33:24 +0100101 TRACE_INFO("Verifying EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100102 for (i = 0; i < 256; i++) {
103 int byte = eeprom_read_byte(0x50, i);
Harald Welteec0837c2017-03-03 01:33:24 +0100104 TRACE_INFO("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +0100105 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +0100106 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +0100107 i, __eeprom_bin[i], byte);
108 }
109
110 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
111 * again powering us up */
112
113 return 0;
114}
115
116/* returns '1' in case we should break any endless loop */
117void board_exec_dbg_cmd(int ch)
118{
119 uint32_t addr, val;
120
121 switch (ch) {
122 case '?':
Harald Welteec0837c2017-03-03 01:33:24 +0100123 printf("\t?\thelp\n\r");
124 printf("\tE\tprogram EEPROM\n\r");
125 printf("\tR\treset SAM3\n\r");
126 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
127 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
128 printf("\tH\tRelease HUB RESET (high)\n\r");
129 printf("\th\tAssert HUB RESET (low)\n\r");
130 printf("\tw\tWrite single byte in EEPROM\n\r");
131 printf("\tr\tRead single byte from EEPROM\n\r");
132 printf("\tX\tRelease peer SAM3 from reset\n\r");
133 printf("\tx\tAssert peer SAM3 reset\n\r");
134 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
135 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
136 printf("\tU\tProceed to USB Initialization\n\r");
137 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
138 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100139 break;
140 case 'E':
141 write_hub_eeprom();
142 break;
143 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100144 printf("Asking NVIC to reset us\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100145 USBD_Disconnect();
Harald Welteaa3b8672017-02-10 19:21:10 +0100146 NVIC_SystemReset();
147 break;
148 case 'O':
Harald Welteec0837c2017-03-03 01:33:24 +0100149 printf("Setting PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100150 PIO_Set(&pin_hubpwr_override);
151 break;
152 case 'o':
Harald Welteec0837c2017-03-03 01:33:24 +0100153 printf("Clearing PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100154 PIO_Clear(&pin_hubpwr_override);
155 break;
156 case 'H':
Harald Welteec0837c2017-03-03 01:33:24 +0100157 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100158 PIO_Clear(&pin_hub_rst);
159 break;
160 case 'h':
161 /* high level drives transistor -> HUB_RESET low */
Harald Welteec0837c2017-03-03 01:33:24 +0100162 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100163 PIO_Set(&pin_hub_rst);
164 break;
165 case 'w':
166 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
Harald Welteec0837c2017-03-03 01:33:24 +0100167 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
168 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100169 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100170 printf("Please enter EEPROM value:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100171 UART_GetIntegerMinMax(&val, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100172 printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr);
Harald Welteaa3b8672017-02-10 19:21:10 +0100173 eeprom_write_byte(0x50, addr, val);
174 break;
175 case 'r':
Harald Welteec0837c2017-03-03 01:33:24 +0100176 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100177 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100178 printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
Harald Welteaa3b8672017-02-10 19:21:10 +0100179 break;
180 case 'X':
Harald Welteec0837c2017-03-03 01:33:24 +0100181 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100182 PIO_Clear(&pin_peer_rst);
183 break;
184 case 'x':
Harald Welteec0837c2017-03-03 01:33:24 +0100185 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100186 PIO_Set(&pin_peer_rst);
187 break;
188 case 'Y':
Harald Welteec0837c2017-03-03 01:33:24 +0100189 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100190 PIO_Clear(&pin_peer_erase);
191 break;
192 case 'y':
Harald Welteec0837c2017-03-03 01:33:24 +0100193 printf("Seetting SIMTRACExx_ERASE (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100194 PIO_Set(&pin_peer_erase);
195 break;
196 case '1':
Harald Welteec0837c2017-03-03 01:33:24 +0100197 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100198 wwan_perst_do_reset(1);
199 break;
200 case '2':
Harald Welteec0837c2017-03-03 01:33:24 +0100201 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100202 wwan_perst_do_reset(2);
203 break;
Harald Welte809e5842017-03-06 09:16:40 +0100204 case '!':
205 qmod_use_physical_sim(1, 0);
206 break;
207 case '@':
208 qmod_use_physical_sim(2, 0);
209 break;
Harald Welteaa3b8672017-02-10 19:21:10 +0100210 default:
Harald Welteec0837c2017-03-03 01:33:24 +0100211 printf("Unknown command '%c'\n\r", ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100212 break;
213 }
214}
215
216void board_main_top(void)
217{
218 wwan_led_init();
219 wwan_perst_init();
220
221 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
222 * pull-up on the input to keep SIMTRACE12 alive */
223 PIO_Configure(&pin_hubpwr_override, 1);
224 PIO_Configure(&pin_hub_rst, 1);
225 PIO_Configure(&pin_1234_detect, 1);
226 PIO_Configure(&pin_peer_rst, 1);
227 PIO_Configure(&pin_peer_erase, 1);
Harald Welte809e5842017-03-06 09:16:40 +0100228 PIO_Configure(&pin_conn_usim1, 1);
229 PIO_Configure(&pin_conn_usim2, 1);
Harald Welteaa3b8672017-02-10 19:21:10 +0100230 i2c_pin_init();
231
232 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100233 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100234 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100235 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welte912b1832017-03-06 09:28:13 +0100236 /* make sure we use the second set of USB Strings
237 * calling the interfaces "Modem 3" and "Modem 4" rather
238 * than 1+2 */
239 usb_strings[7] = usb_strings[9];
240 usb_strings[8] = usb_strings[10];
Harald Welteaa3b8672017-02-10 19:21:10 +0100241 }
242
243 /* Obtain the circuit board version (currently just prints voltage */
244 get_board_version_adc();
245}