blob: e05a4542f04210b4733ff5e42d26ce8a6fd39d03 [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
Harald Welteec0837c2017-03-03 01:33:24 +010061 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010062 /* 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 */
Harald Welteec0837c2017-03-03 01:33:24 +010071 TRACE_INFO("Verifying EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010072 for (i = 0; i < 256; i++) {
73 int byte = eeprom_read_byte(0x50, i);
Harald Welteec0837c2017-03-03 01:33:24 +010074 TRACE_INFO("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +010075 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +010076 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +010077 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 '?':
Harald Welteec0837c2017-03-03 01:33:24 +010093 printf("\t?\thelp\n\r");
94 printf("\tE\tprogram EEPROM\n\r");
95 printf("\tR\treset SAM3\n\r");
96 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
97 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
98 printf("\tH\tRelease HUB RESET (high)\n\r");
99 printf("\th\tAssert HUB RESET (low)\n\r");
100 printf("\tw\tWrite single byte in EEPROM\n\r");
101 printf("\tr\tRead single byte from EEPROM\n\r");
102 printf("\tX\tRelease peer SAM3 from reset\n\r");
103 printf("\tx\tAssert peer SAM3 reset\n\r");
104 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
105 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
106 printf("\tU\tProceed to USB Initialization\n\r");
107 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
108 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100109 break;
110 case 'E':
111 write_hub_eeprom();
112 break;
113 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100114 printf("Asking NVIC to reset us\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100115 USBD_Disconnect();
Harald Welteaa3b8672017-02-10 19:21:10 +0100116 NVIC_SystemReset();
117 break;
118 case 'O':
Harald Welteec0837c2017-03-03 01:33:24 +0100119 printf("Setting PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100120 PIO_Set(&pin_hubpwr_override);
121 break;
122 case 'o':
Harald Welteec0837c2017-03-03 01:33:24 +0100123 printf("Clearing PRTPWR_OVERRIDE\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100124 PIO_Clear(&pin_hubpwr_override);
125 break;
126 case 'H':
Harald Welteec0837c2017-03-03 01:33:24 +0100127 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100128 PIO_Clear(&pin_hub_rst);
129 break;
130 case 'h':
131 /* high level drives transistor -> HUB_RESET low */
Harald Welteec0837c2017-03-03 01:33:24 +0100132 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100133 PIO_Set(&pin_hub_rst);
134 break;
135 case 'w':
136 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
Harald Welteec0837c2017-03-03 01:33:24 +0100137 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
138 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100139 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100140 printf("Please enter EEPROM value:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100141 UART_GetIntegerMinMax(&val, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100142 printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr);
Harald Welteaa3b8672017-02-10 19:21:10 +0100143 eeprom_write_byte(0x50, addr, val);
144 break;
145 case 'r':
Harald Welteec0837c2017-03-03 01:33:24 +0100146 printf("Please enter EEPROM offset:\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100147 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welteec0837c2017-03-03 01:33:24 +0100148 printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
Harald Welteaa3b8672017-02-10 19:21:10 +0100149 break;
150 case 'X':
Harald Welteec0837c2017-03-03 01:33:24 +0100151 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100152 PIO_Clear(&pin_peer_rst);
153 break;
154 case 'x':
Harald Welteec0837c2017-03-03 01:33:24 +0100155 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100156 PIO_Set(&pin_peer_rst);
157 break;
158 case 'Y':
Harald Welteec0837c2017-03-03 01:33:24 +0100159 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100160 PIO_Clear(&pin_peer_erase);
161 break;
162 case 'y':
Harald Welteec0837c2017-03-03 01:33:24 +0100163 printf("Seetting SIMTRACExx_ERASE (active)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100164 PIO_Set(&pin_peer_erase);
165 break;
166 case '1':
Harald Welteec0837c2017-03-03 01:33:24 +0100167 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100168 wwan_perst_do_reset(1);
169 break;
170 case '2':
Harald Welteec0837c2017-03-03 01:33:24 +0100171 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100172 wwan_perst_do_reset(2);
173 break;
174 default:
Harald Welteec0837c2017-03-03 01:33:24 +0100175 printf("Unknown command '%c'\n\r", ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100176 break;
177 }
178}
179
180void board_main_top(void)
181{
182 wwan_led_init();
183 wwan_perst_init();
184
185 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
186 * pull-up on the input to keep SIMTRACE12 alive */
187 PIO_Configure(&pin_hubpwr_override, 1);
188 PIO_Configure(&pin_hub_rst, 1);
189 PIO_Configure(&pin_1234_detect, 1);
190 PIO_Configure(&pin_peer_rst, 1);
191 PIO_Configure(&pin_peer_erase, 1);
192 i2c_pin_init();
193
194 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100195 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100196 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100197 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100198 }
199
200 /* Obtain the circuit board version (currently just prints voltage */
201 get_board_version_adc();
202}