Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 1 | /* 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 | |
| 13 | static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE; |
| 14 | static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 15 | static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP}; |
| 16 | static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 17 | static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 18 | |
| 19 | |
| 20 | static 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 | |
| 28 | const unsigned char __eeprom_bin[256] = { |
Harald Welte | c6e482d | 2017-03-05 16:24:29 +0100 | [diff] [blame^] | 29 | 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 Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 34 | 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" |
| 52 | static 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 Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 65 | TRACE_INFO("Writing EEPROM...\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 66 | /* 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 Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 75 | TRACE_INFO("Verifying EEPROM...\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 76 | for (i = 0; i < 256; i++) { |
| 77 | int byte = eeprom_read_byte(0x50, i); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 78 | TRACE_INFO("0x%02x: %02x\n\r", i, byte); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 79 | if (byte != __eeprom_bin[i]) |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 80 | TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r", |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 81 | 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 */ |
| 91 | void board_exec_dbg_cmd(int ch) |
| 92 | { |
| 93 | uint32_t addr, val; |
| 94 | |
| 95 | switch (ch) { |
| 96 | case '?': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 97 | 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 Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 113 | break; |
| 114 | case 'E': |
| 115 | write_hub_eeprom(); |
| 116 | break; |
| 117 | case 'R': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 118 | printf("Asking NVIC to reset us\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 119 | USBD_Disconnect(); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 120 | NVIC_SystemReset(); |
| 121 | break; |
| 122 | case 'O': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 123 | printf("Setting PRTPWR_OVERRIDE\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 124 | PIO_Set(&pin_hubpwr_override); |
| 125 | break; |
| 126 | case 'o': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 127 | printf("Clearing PRTPWR_OVERRIDE\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 128 | PIO_Clear(&pin_hubpwr_override); |
| 129 | break; |
| 130 | case 'H': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 131 | printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 132 | PIO_Clear(&pin_hub_rst); |
| 133 | break; |
| 134 | case 'h': |
| 135 | /* high level drives transistor -> HUB_RESET low */ |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 136 | printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 137 | PIO_Set(&pin_hub_rst); |
| 138 | break; |
| 139 | case 'w': |
| 140 | if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0) |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 141 | printf("WARNING: attempting EEPROM access while HUB not in reset\n\r"); |
| 142 | printf("Please enter EEPROM offset:\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 143 | UART_GetIntegerMinMax(&addr, 0, 255); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 144 | printf("Please enter EEPROM value:\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 145 | UART_GetIntegerMinMax(&val, 0, 255); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 146 | printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 147 | eeprom_write_byte(0x50, addr, val); |
| 148 | break; |
| 149 | case 'r': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 150 | printf("Please enter EEPROM offset:\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 151 | UART_GetIntegerMinMax(&addr, 0, 255); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 152 | printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr)); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 153 | break; |
| 154 | case 'X': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 155 | printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 156 | PIO_Clear(&pin_peer_rst); |
| 157 | break; |
| 158 | case 'x': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 159 | printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 160 | PIO_Set(&pin_peer_rst); |
| 161 | break; |
| 162 | case 'Y': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 163 | printf("Clearing SIMTRACExx_ERASE (inactive)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 164 | PIO_Clear(&pin_peer_erase); |
| 165 | break; |
| 166 | case 'y': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 167 | printf("Seetting SIMTRACExx_ERASE (active)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 168 | PIO_Set(&pin_peer_erase); |
| 169 | break; |
| 170 | case '1': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 171 | printf("Resetting Modem 1 (of this SAM3)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 172 | wwan_perst_do_reset(1); |
| 173 | break; |
| 174 | case '2': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 175 | printf("Resetting Modem 2 (of this SAM3)\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 176 | wwan_perst_do_reset(2); |
| 177 | break; |
| 178 | default: |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 179 | printf("Unknown command '%c'\n\r", ch); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void 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 Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 199 | TRACE_INFO("Detected Quad-Modem ST12\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 200 | } else { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 201 | TRACE_INFO("Detected Quad-Modem ST34\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* Obtain the circuit board version (currently just prints voltage */ |
| 205 | get_board_version_adc(); |
| 206 | } |