sandbox: spi: Adjust 'sf test' to work on sandbox
[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                 /*              dbg("(%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 extern void  dbg(const char *fmt, ...);
44 int mdm_init (void)
45 {
46         char env_str[16];
47         char *init_str;
48         int i;
49         extern void enable_putc(void);
50         extern int hwflow_onoff(int);
51
52         enable_putc(); /* enable serial_putc() */
53
54 #ifdef CONFIG_HWFLOW
55         init_str = getenv("mdm_flow_control");
56         if (init_str && (strcmp(init_str, "rts/cts") == 0))
57                 hwflow_onoff (1);
58         else
59                 hwflow_onoff(-1);
60 #endif
61
62         for (i = 1;;i++) {
63                 sprintf(env_str, "mdm_init%d", i);
64                 if ((init_str = getenv(env_str)) != NULL) {
65                         serial_puts(init_str);
66                         serial_puts("\n");
67                         for(;;) {
68                                 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
69                                 dbg("ini%d: [%s]", i, console_buffer);
70
71                                 if ((strcmp(console_buffer, "OK") == 0) ||
72                                         (strcmp(console_buffer, "ERROR") == 0)) {
73                                         dbg("ini%d: cmd done", i);
74                                         break;
75                                 } else /* in case we are originating call ... */
76                                         if (strncmp(console_buffer, "CONNECT", 7) == 0) {
77                                                 dbg("ini%d: connect", i);
78                                                 return 0;
79                                         }
80                         }
81                 } else
82                         break; /* no init string - stop modem init */
83
84                 udelay(100000);
85         }
86
87         udelay(100000);
88
89         /* final stage - wait for connect */
90         for(;i > 1;) { /* if 'i' > 1 - wait for connection
91                                   message from modem */
92                 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
93                 dbg("ini_f: [%s]", console_buffer);
94                 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
95                         dbg("ini_f: connected");
96                         return 0;
97                 }
98         }
99
100         return 0;
101 }