]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/usb2422.c
Update to arm_atsam wait and timer routines
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / usb / usb2422.c
1 /*
2 Copyright 2018 Massdrop Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "arm_atsam_protocol.h"
19 #include <string.h>
20
21 Usb2422 USB2422_shadow;
22 unsigned char i2c0_buf[34];
23
24 const uint16_t MFRNAME[] = { 'M','a','s','s','d','r','o','p',' ','I','n','c','.' }; //Massdrop Inc.
25 const uint16_t PRDNAME[] = { 'M','a','s','s','d','r','o','p',' ','H','u','b' }; //Massdrop Hub
26 #ifndef MD_BOOTLOADER
27 //Serial number reported stops before first found space character or at last found character
28 const uint16_t SERNAME[] = { 'U','n','a','v','a','i','l','a','b','l','e' }; //Unavailable
29 #else
30 //In production, this field is found, modified, and offset noted as the last 32-bit word in the bootloader space
31 //The offset allows the application to use the factory programmed serial (which may differ from the physical serial label)
32 //Serial number reported stops before first found space character or when max size is reached
33 __attribute__((__aligned__(4)))
34 const uint16_t SERNAME[BOOTLOADER_SERIAL_MAX_SIZE] = { 'M','D','H','U','B','B','O','O','T','L','0','0','0','0','0','0','0','0','0','0' };
35 //NOTE: Serial replacer will not write a string longer than given here as a precaution, so give enough
36 //      space as needed and adjust BOOTLOADER_SERIAL_MAX_SIZE to match amount given
37 #endif //MD_BOOTLOADER
38
39 uint8_t usb_host_port;
40
41 #ifndef MD_BOOTLOADER
42
43 uint8_t usb_extra_state;
44 uint8_t usb_extra_manual;
45 uint8_t usb_gcr_auto;
46
47 #endif //MD_BOOTLOADER
48
49 uint16_t adc_extra;
50
51 void USB_write2422_block(void)
52 {
53     unsigned char *dest = i2c0_buf;
54     unsigned char *src;
55     unsigned char *base = (unsigned char *)&USB2422_shadow;
56
57     DBGC(DC_USB_WRITE2422_BLOCK_BEGIN);
58
59     for (src =  base; src < base + 256; src += 32)
60     {
61         dest[0] = src - base;
62         dest[1] = 32;
63         memcpy(&dest[2], src, 32);
64         i2c0_transmit(USB2422_ADDR, dest, 34, 50000);
65         SERCOM0->I2CM.CTRLB.bit.CMD = 0x03;
66         while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); }
67         wait_us(100);
68     }
69
70     DBGC(DC_USB_WRITE2422_BLOCK_COMPLETE);
71 }
72
73 void USB2422_init(void)
74 {
75     Gclk *pgclk = GCLK;
76     Mclk *pmclk = MCLK;
77     Port *pport = PORT;
78     Oscctrl *posc = OSCCTRL;
79     Usb *pusb = USB;
80
81     DBGC(DC_USB2422_INIT_BEGIN);
82
83     while ((v_5v = adc_get(ADC_5V)) < ADC_5V_START_LEVEL) { DBGC(DC_USB2422_INIT_WAIT_5V_LOW); }
84
85     //setup peripheral and synchronous bus clocks to USB
86     pgclk->PCHCTRL[10].bit.GEN = 0;
87     pgclk->PCHCTRL[10].bit.CHEN = 1;
88     pmclk->AHBMASK.bit.USB_ = 1;
89     pmclk->APBBMASK.bit.USB_ = 1;
90
91     //setup port pins for D-, D+, and SOF_1KHZ
92     pport->Group[0].PMUX[12].reg = 0x77; //PA24, PA25, function column H for USB D-, D+
93     pport->Group[0].PINCFG[24].bit.PMUXEN = 1;
94     pport->Group[0].PINCFG[25].bit.PMUXEN = 1;
95     pport->Group[1].PMUX[11].bit.PMUXE = 7; //PB22, function column H for USB SOF_1KHz output
96     pport->Group[1].PINCFG[22].bit.PMUXEN = 1;
97
98     //configure and enable DFLL for USB clock recovery mode at 48MHz
99     posc->DFLLCTRLA.bit.ENABLE = 0;
100     while (posc->DFLLSYNC.bit.ENABLE) { DBGC(DC_USB2422_INIT_OSC_SYNC_DISABLING); }
101     while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_1); }
102     posc->DFLLCTRLB.bit.USBCRM = 1;
103     while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_2); }
104     posc->DFLLCTRLB.bit.MODE = 1;
105     while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_3); }
106     posc->DFLLCTRLB.bit.QLDIS = 0;
107     while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_4); }
108     posc->DFLLCTRLB.bit.CCDIS = 1;
109     posc->DFLLMUL.bit.MUL = 0xBB80; //4800 x 1KHz
110     while (posc->DFLLSYNC.bit.DFLLMUL) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLMUL); }
111     posc->DFLLCTRLA.bit.ENABLE = 1;
112     while (posc->DFLLSYNC.bit.ENABLE) { DBGC(DC_USB2422_INIT_OSC_SYNC_ENABLING); }
113
114     pusb->DEVICE.CTRLA.bit.SWRST = 1;
115     while (pusb->DEVICE.SYNCBUSY.bit.SWRST) { DBGC(DC_USB2422_INIT_USB_SYNC_SWRST); }
116     while (pusb->DEVICE.CTRLA.bit.SWRST) { DBGC(DC_USB2422_INIT_USB_WAIT_SWRST); }
117     //calibration from factory presets
118     pusb->DEVICE.PADCAL.bit.TRANSN = (USB_FUSES_TRANSN_ADDR >> USB_FUSES_TRANSN_Pos) & USB_FUSES_TRANSN_Msk;
119     pusb->DEVICE.PADCAL.bit.TRANSP = (USB_FUSES_TRANSP_ADDR >> USB_FUSES_TRANSP_Pos) & USB_FUSES_TRANSP_Msk;
120     pusb->DEVICE.PADCAL.bit.TRIM = (USB_FUSES_TRIM_ADDR >> USB_FUSES_TRIM_Pos) & USB_FUSES_TRIM_Msk;
121     //device mode, enabled
122     pusb->DEVICE.CTRLB.bit.SPDCONF = 0; //full speed
123     pusb->DEVICE.CTRLA.bit.MODE = 0;
124     pusb->DEVICE.CTRLA.bit.ENABLE = 1;
125     while (pusb->DEVICE.SYNCBUSY.bit.ENABLE) { DBGC(DC_USB2422_INIT_USB_SYNC_ENABLING); }
126
127     pusb->DEVICE.QOSCTRL.bit.DQOS = 2;
128     pusb->DEVICE.QOSCTRL.bit.CQOS = 2;
129
130     pport->Group[USB2422_HUB_ACTIVE_GROUP].PINCFG[USB2422_HUB_ACTIVE_PIN].bit.INEN = 1;
131
132     i2c0_init(); //IC2 clk must be high at USB2422 reset release time to signal SMB configuration
133
134     sr_exp_data.bit.HUB_CONNECT = 1; //connect signal
135     sr_exp_data.bit.HUB_RESET_N = 1; //reset high
136     SR_EXP_WriteData();
137
138     wait_us(100);
139
140 #ifndef MD_BOOTLOADER
141
142     usb_extra_manual = 0;
143     usb_gcr_auto = 1;
144
145 #endif //MD_BOOTLOADER
146
147     DBGC(DC_USB2422_INIT_COMPLETE);
148 }
149
150 void USB_reset(void)
151 {
152     DBGC(DC_USB_RESET_BEGIN);
153
154     //pulse reset for at least 1 usec
155     sr_exp_data.bit.HUB_RESET_N = 0; //reset low
156     SR_EXP_WriteData();
157     wait_us(2);
158     sr_exp_data.bit.HUB_RESET_N = 1; //reset high to run
159     SR_EXP_WriteData();
160
161     DBGC(DC_USB_RESET_COMPLETE);
162 }
163
164 void USB_configure(void)
165 {
166     Usb2422 *pusb2422 = &USB2422_shadow;
167     memset(pusb2422, 0, sizeof(Usb2422));
168
169     uint16_t *serial_use = (uint16_t *)SERNAME; //Default to use SERNAME from this file
170     uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); //Default to use SERNAME from this file
171 #ifndef MD_BOOTLOADER
172     uint32_t serial_ptrloc = (uint32_t)&_srom - 4;
173 #else //MD_BOOTLOADER
174     uint32_t serial_ptrloc = (uint32_t)&_erom - 4;
175 #endif //MD_BOOTLOADER
176     uint32_t serial_address = *(uint32_t *)serial_ptrloc; //Address of bootloader's serial number if available
177
178     DBGC(DC_USB_CONFIGURE_BEGIN);
179
180     if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) //Check for factory programmed serial address
181     {
182         if ((serial_address & 0xFF) % 4 == 0) //Check alignment
183         {
184             serial_use = (uint16_t *)(serial_address);
185             serial_length = 0;
186             while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) &&
187                    serial_length < BOOTLOADER_SERIAL_MAX_SIZE)
188             {
189                 serial_length++;
190                 DBGC(DC_USB_CONFIGURE_GET_SERIAL);
191             }
192         }
193     }
194
195     //configure Usb2422 registers
196     pusb2422->VID.reg = 0x04D8; // from Microchip 4/19/2018
197     pusb2422->PID.reg = 0xEEC5; // from Microchip 4/19/2018 = Massdrop, Inc. USB Hub
198     pusb2422->DID.reg = 0x0101; // BCD 01.01
199     pusb2422->CFG1.bit.SELF_BUS_PWR = 1; // self powered for now
200     pusb2422->CFG1.bit.HS_DISABLE = 1; // full or high speed
201     //pusb2422->CFG2.bit.COMPOUND = 0; // compound device
202     pusb2422->CFG3.bit.STRING_EN = 1; // strings enabled
203     //pusb2422->NRD.bit.PORT2_NR = 0; // MCU is non-removable
204     pusb2422->MAXPB.reg = 20; // 0mA
205     pusb2422->HCMCB.reg = 20; // 0mA
206     pusb2422->MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t);
207     pusb2422->PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t);
208     pusb2422->SERSL.reg = serial_length;
209     memcpy(pusb2422->MFRSTR, MFRNAME, sizeof(MFRNAME));
210     memcpy(pusb2422->PRDSTR, PRDNAME, sizeof(PRDNAME));
211     memcpy(pusb2422->SERSTR, serial_use, serial_length * sizeof(uint16_t));
212     //pusb2422->BOOSTUP.bit.BOOST=3;    //upstream port
213     //pusb2422->BOOSTDOWN.bit.BOOST1=0; // extra port
214     //pusb2422->BOOSTDOWN.bit.BOOST2=2; //MCU is close
215     pusb2422->STCD.bit.USB_ATTACH = 1;
216     USB_write2422_block();
217
218     adc_extra = 0;
219
220     DBGC(DC_USB_CONFIGURE_COMPLETE);
221 }
222
223 uint16_t USB_active(void)
224 {
225     return (PORT->Group[USB2422_HUB_ACTIVE_GROUP].IN.reg & (1 << USB2422_HUB_ACTIVE_PIN)) != 0;
226 }
227
228 void USB_set_host_by_voltage(void)
229 {
230     //UP is upstream device (HOST)
231     //DN1 is downstream device (EXTRA)
232     //DN2 is keyboard (KEYB)
233
234     DBGC(DC_USB_SET_HOST_BY_VOLTAGE_BEGIN);
235
236     usb_host_port = USB_HOST_PORT_UNKNOWN;
237 #ifndef MD_BOOTLOADER
238     usb_extra_state = USB_EXTRA_STATE_UNKNOWN;
239 #endif //MD_BOOTLOADER
240     sr_exp_data.bit.SRC_1 = 1;       //USBC-1 available for test
241     sr_exp_data.bit.SRC_2 = 1;       //USBC-2 available for test
242     sr_exp_data.bit.E_UP_N = 1;      //HOST disable
243     sr_exp_data.bit.E_DN1_N = 1;     //EXTRA disable
244     sr_exp_data.bit.E_VBUS_1 = 0;    //USBC-1 disable full power I/O
245     sr_exp_data.bit.E_VBUS_2 = 0;    //USBC-2 disable full power I/O
246
247     SR_EXP_WriteData();
248
249     wait_ms(250);
250
251     while ((v_5v = adc_get(ADC_5V)) < ADC_5V_START_LEVEL) { DBGC(DC_USB_SET_HOST_5V_LOW_WAITING); }
252
253     v_con_1 = adc_get(ADC_CON1);
254     v_con_2 = adc_get(ADC_CON2);
255
256     v_con_1_boot = v_con_1;
257     v_con_2_boot = v_con_2;
258
259     if (v_con_1 > v_con_2)
260     {
261         sr_exp_data.bit.S_UP = 0;        //HOST to USBC-1
262         sr_exp_data.bit.S_DN1 = 1;       //EXTRA to USBC-2
263         sr_exp_data.bit.SRC_1 = 1;       //HOST on USBC-1
264         sr_exp_data.bit.SRC_2 = 0;       //EXTRA available on USBC-2
265
266         sr_exp_data.bit.E_VBUS_1 = 1;    //USBC-1 enable full power I/O
267         sr_exp_data.bit.E_VBUS_2 = 0;    //USBC-2 disable full power I/O
268
269         SR_EXP_WriteData();
270
271         sr_exp_data.bit.E_UP_N = 0;      //HOST enable
272
273         SR_EXP_WriteData();
274
275         usb_host_port = USB_HOST_PORT_1;
276     }
277     else
278     {
279         sr_exp_data.bit.S_UP = 1;        //EXTRA to USBC-1
280         sr_exp_data.bit.S_DN1 = 0;       //HOST to USBC-2
281         sr_exp_data.bit.SRC_1 = 0;       //EXTRA available on USBC-1
282         sr_exp_data.bit.SRC_2 = 1;       //HOST on USBC-2
283
284         sr_exp_data.bit.E_VBUS_1 = 0;    //USBC-1 disable full power I/O
285         sr_exp_data.bit.E_VBUS_2 = 1;    //USBC-2 enable full power I/O
286
287         SR_EXP_WriteData();
288
289         sr_exp_data.bit.E_UP_N = 0;      //HOST enable
290
291         SR_EXP_WriteData();
292
293         usb_host_port = USB_HOST_PORT_2;
294     }
295
296 #ifndef MD_BOOTLOADER
297     usb_extra_state = USB_EXTRA_STATE_DISABLED;
298 #endif //MD_BOOTLOADER
299
300     USB_reset();
301     USB_configure();
302
303     DBGC(DC_USB_SET_HOST_BY_VOLTAGE_COMPLETE);
304 }
305
306 uint8_t USB2422_Port_Detect_Init(void)
307 {
308     uint32_t port_detect_retry_ms;
309     uint32_t tmod;
310
311     DBGC(DC_PORT_DETECT_INIT_BEGIN);
312
313     USB_set_host_by_voltage();
314
315     port_detect_retry_ms = timer_read64() + PORT_DETECT_RETRY_INTERVAL;
316
317     while (!USB_active())
318     {
319         tmod = timer_read64() % PORT_DETECT_RETRY_INTERVAL;
320
321         if (v_con_1 > v_con_2) //Values updated from USB_set_host_by_voltage();
322         {
323             //1 flash for port 1 detected
324             if (tmod > 500 && tmod < 600) { DBG_LED_ON; }
325             else { DBG_LED_OFF; }
326         }
327         else if (v_con_2 > v_con_1) //Values updated from USB_set_host_by_voltage();
328         {
329             //2 flash for port 2 detected
330             if (tmod > 500 && tmod < 600) { DBG_LED_ON; }
331             else if (tmod > 700 && tmod < 800) { DBG_LED_ON; }
332             else { DBG_LED_OFF; }
333         }
334
335         if (timer_read64() > port_detect_retry_ms)
336         {
337             DBGC(DC_PORT_DETECT_INIT_FAILED);
338             return 0;
339         }
340     }
341
342     DBGC(DC_PORT_DETECT_INIT_COMPLETE);
343
344     return 1;
345 }
346
347 #ifndef MD_BOOTLOADER
348
349 void USB_ExtraSetState(uint8_t state)
350 {
351     uint8_t state_save = state;
352
353     if (state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG)
354         state = USB_EXTRA_STATE_DISABLED;
355
356     if (usb_host_port == USB_HOST_PORT_1) sr_exp_data.bit.E_VBUS_2 = state;
357     else if (usb_host_port == USB_HOST_PORT_2) sr_exp_data.bit.E_VBUS_1 = state;
358     else return;
359
360     sr_exp_data.bit.E_DN1_N = !state;
361     SR_EXP_WriteData();
362
363     usb_extra_state = state_save;
364
365     if (usb_extra_state == USB_EXTRA_STATE_ENABLED) CDC_print("USB: Extra enabled\r\n");
366     else if (usb_extra_state == USB_EXTRA_STATE_DISABLED)
367     {
368       CDC_print("USB: Extra disabled\r\n");
369       if (led_animation_breathing) gcr_breathe = gcr_desired;
370     }
371     else if (usb_extra_state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) CDC_print("USB: Extra disabled until replug\r\n");
372     else CDC_print("USB: Extra state unknown\r\n");
373 }
374
375 void USB_HandleExtraDevice(void)
376 {
377     uint16_t adcval;
378
379     if (usb_host_port == USB_HOST_PORT_1) adcval = adc_get(ADC_CON2);
380     else if (usb_host_port == USB_HOST_PORT_2) adcval = adc_get(ADC_CON1);
381     else return;
382
383     adc_extra = adc_extra * 0.9 + adcval * 0.1;
384
385     //Check for a forced disable state (such as overload prevention)
386     if (usb_extra_state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG)
387     {
388         //Detect unplug and reset state to disabled
389         if (adc_extra > USB_EXTRA_ADC_THRESHOLD) usb_extra_state = USB_EXTRA_STATE_DISABLED;
390
391         return; //Return even if unplug detected
392     }
393
394     if (usb_extra_manual)
395     {
396         if (usb_extra_state == USB_EXTRA_STATE_DISABLED)
397         USB_ExtraSetState(USB_EXTRA_STATE_ENABLED);
398
399         return;
400     }
401
402     //dpf("a %i %i\r\n",adcval, adc_extra);
403     if (usb_extra_state == USB_EXTRA_STATE_DISABLED && adc_extra < USB_EXTRA_ADC_THRESHOLD) USB_ExtraSetState(USB_EXTRA_STATE_ENABLED);
404     else if (usb_extra_state == USB_EXTRA_STATE_ENABLED && adc_extra > USB_EXTRA_ADC_THRESHOLD) USB_ExtraSetState(USB_EXTRA_STATE_DISABLED);
405 }
406
407 #endif //MD_BOOTLOADER
408