dm: usb: Drop the legacy USB init sequence
[platform/kernel/u-boot.git] / common / modem.c
1 /*
2  * (C) Copyright 2002-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9
10 /* 'inline' - We have to do it fast */
11 static inline void mdm_readline(char *buf, int bufsiz)
12 {
13         char c;
14         char *p;
15         int n;
16
17         n = 0;
18         p = buf;
19         for(;;) {
20                 c = serial_getc();
21
22                 debug("(%c)", c);
23
24                 switch(c) {
25                 case '\r':
26                         break;
27                 case '\n':
28                         *p = '\0';
29                         return;
30
31                 default:
32                         if(n++ > bufsiz) {
33                                 *p = '\0';
34                                 return; /* sanity check */
35                         }
36                         *p = c;
37                         p++;
38                         break;
39                 }
40         }
41 }
42
43 int mdm_init (void)
44 {
45         char env_str[16];
46         char *init_str;
47         int i;
48         extern void enable_putc(void);
49         extern int hwflow_onoff(int);
50
51         enable_putc(); /* enable serial_putc() */
52
53 #ifdef CONFIG_HWFLOW
54         init_str = getenv("mdm_flow_control");
55         if (init_str && (strcmp(init_str, "rts/cts") == 0))
56                 hwflow_onoff (1);
57         else
58                 hwflow_onoff(-1);
59 #endif
60
61         for (i = 1;;i++) {
62                 sprintf(env_str, "mdm_init%d", i);
63                 if ((init_str = getenv(env_str)) != NULL) {
64                         serial_puts(init_str);
65                         serial_puts("\n");
66                         for(;;) {
67                                 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
68                                 debug("ini%d: [%s]", i, console_buffer);
69
70                                 if ((strcmp(console_buffer, "OK") == 0) ||
71                                         (strcmp(console_buffer, "ERROR") == 0)) {
72                                         debug("ini%d: cmd done", i);
73                                         break;
74                                 } else /* in case we are originating call ... */
75                                         if (strncmp(console_buffer, "CONNECT", 7) == 0) {
76                                                 debug("ini%d: connect", i);
77                                                 return 0;
78                                         }
79                         }
80                 } else
81                         break; /* no init string - stop modem init */
82
83                 udelay(100000);
84         }
85
86         udelay(100000);
87
88         /* final stage - wait for connect */
89         for(;i > 1;) { /* if 'i' > 1 - wait for connection
90                                   message from modem */
91                 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
92                 debug("ini_f: [%s]", console_buffer);
93                 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
94                         debug("ini_f: connected");
95                         return 0;
96                 }
97         }
98
99         return 0;
100 }