blob: 3e6ec52f58054d113f6a557079384a7b8cad3e57 [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] = {
Harald Weltec6e482d2017-03-05 16:24:29 +010029 USB_VENDOR_OPENMOKO & 0xff,
30 USB_VENDOR_OPENMOKO >> 8,
31 USB_PRODUCT_QMOD_HUB & 0xff,
32 USB_PRODUCT_QMOD_HUB >> 8,
33 0x00, 0x00, 0x9b, 0x20, 0x01, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
Harald Welteaa3b8672017-02-10 19:21:10 +010034 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
35 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
36 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
37 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
38 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
39 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
49};
50
51#include "i2c.h"
52static int write_hub_eeprom(void)
53{
54 const unsigned int __eeprom_bin_len = 256;
55
56 int i;
57
58 /* wait */
59 volatile int v;
60 /* 440ns per cycle here */
61 for (i = 0; i < 1000000; i++) {
62 v = 0;
63 }
64
Harald Welteec0837c2017-03-03 01:33:24 +010065 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010066 /* write the EEPROM once */
67 for (i = 0; i < 256; i++) {
68 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
69 /* if the result was negative, repeat that write */
70 if (rc < 0)
71 i--;
72 }
73
74 /* then pursue re-reading it again and again */
Harald Welteec0837c2017-03-03 01:33:24 +010075 TRACE_INFO("Verifying EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010076 for (i = 0; i < 256; i++) {
77 int byte = eeprom_read_byte(0x50, i);
Harald Welteec0837c2017-03-03 01:33:24 +010078 TRACE_INFO("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +010079 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +010080 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +010081 i, __eeprom_bin[i], byte);
82 }
83
84 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
85 * again powering us up */
86
87 return 0;
88}
89
90/* returns '1' in case we should break any endless loop */
91void board_exec_dbg_cmd(int ch)
92{
93 uint32_t addr, val;
94
95 switch (ch) {
96 case '?':
Harald Welteec0837c2017-03-03 01:33:24 +010097 printf("\t?\thelp\n\r");
98 printf("\tE\tprogram EEPROM\n\r");
99 printf("\tR\treset SAM3\n\r");
100 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
101 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
102 printf("\tH\tRelease HUB RESET (high)\n\r");
103 printf("\th\tAssert HUB RESET (low)\n\r");
104 printf("\tw\tWrite single byte in EEPROM\n\r");
105 printf("\tr\tRead single byte from EEPROM\n\r");
106 printf("\tX\tRelease peer SAM3 from reset\n\r");
107 printf("\tx\tAssert peer SAM3 reset\n\r");
108 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
109 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
110 printf("\tU\tProceed to USB Initialization\n\r");
111 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
112 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100113 break;
114 case 'E':
115 write_hub_eeprom();
116 break;
117 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100118 printf("Asking NVIC to reset us\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100119 USBD_Disconnect();
Harald Welteaa3b8672017-02-10 19:21:10 +0100120 NVIC_SystemReset();
121 break;
122 case 'O':
Harald Welteec0837c2017-03-03 01:33:24 +0100123 printf("Setting PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100124 PIO_Set(&pin_hubpwr_override);
125 break;
126 case 'o':
Harald Welteec0837c2017-03-03 01:33:24 +0100127 printf("Clearing PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100128 PIO_Clear(&pin_hubpwr_override);
129 break;
130 case 'H':
Harald Welteec0837c2017-03-03 01:33:24 +0100131 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100132 PIO_Clear(&pin_hub_rst);
133 break;
134 case 'h':
135 /* high level drives transistor -> HUB_RESET low */
Harald Welteec0837c2017-03-03 01:33:24 +0100136 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100137 PIO_Set(&pin_hub_rst);
138 break;
139 case 'w':
140 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
Harald Welteec0837c2017-03-03 01:33:24 +0100141 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
142 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100143 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100144 printf("Please enter EEPROM value:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100145 UART_GetIntegerMinMax(&val, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100146 printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr);
Harald Welteaa3b8672017-02-10 19:21:10 +0100147 eeprom_write_byte(0x50, addr, val);
148 break;
149 case 'r':
Harald Welteec0837c2017-03-03 01:33:24 +0100150 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100151 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100152 printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
Harald Welteaa3b8672017-02-10 19:21:10 +0100153 break;
154 case 'X':
Harald Welteec0837c2017-03-03 01:33:24 +0100155 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100156 PIO_Clear(&pin_peer_rst);
157 break;
158 case 'x':
Harald Welteec0837c2017-03-03 01:33:24 +0100159 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100160 PIO_Set(&pin_peer_rst);
161 break;
162 case 'Y':
Harald Welteec0837c2017-03-03 01:33:24 +0100163 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100164 PIO_Clear(&pin_peer_erase);
165 break;
166 case 'y':
Harald Welteec0837c2017-03-03 01:33:24 +0100167 printf("Seetting SIMTRACExx_ERASE (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100168 PIO_Set(&pin_peer_erase);
169 break;
170 case '1':
Harald Welteec0837c2017-03-03 01:33:24 +0100171 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100172 wwan_perst_do_reset(1);
173 break;
174 case '2':
Harald Welteec0837c2017-03-03 01:33:24 +0100175 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100176 wwan_perst_do_reset(2);
177 break;
178 default:
Harald Welteec0837c2017-03-03 01:33:24 +0100179 printf("Unknown command '%c'\n\r", ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100180 break;
181 }
182}
183
184void board_main_top(void)
185{
186 wwan_led_init();
187 wwan_perst_init();
188
189 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
190 * pull-up on the input to keep SIMTRACE12 alive */
191 PIO_Configure(&pin_hubpwr_override, 1);
192 PIO_Configure(&pin_hub_rst, 1);
193 PIO_Configure(&pin_1234_detect, 1);
194 PIO_Configure(&pin_peer_rst, 1);
195 PIO_Configure(&pin_peer_erase, 1);
196 i2c_pin_init();
197
198 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100199 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100200 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100201 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100202 }
203
204 /* Obtain the circuit board version (currently just prints voltage */
205 get_board_version_adc();
206}