blob: 96b10648d869620ddb3e15a819528de0d29ab2e0 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +02001/*
2 * (C) Copyright 2002-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +02006 */
7
8#include <common.h>
9
10/* 'inline' - We have to do it fast */
11static 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
Tom Rini8952ff22014-10-24 20:26:19 -040022 debug("(%c)", c);
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020023
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
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020043int mdm_init (void)
44{
45 char env_str[16];
46 char *init_str;
47 int i;
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020048 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);
Tom Rini8952ff22014-10-24 20:26:19 -040068 debug("ini%d: [%s]", i, console_buffer);
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020069
70 if ((strcmp(console_buffer, "OK") == 0) ||
71 (strcmp(console_buffer, "ERROR") == 0)) {
Tom Rini8952ff22014-10-24 20:26:19 -040072 debug("ini%d: cmd done", i);
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020073 break;
74 } else /* in case we are originating call ... */
75 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
Tom Rini8952ff22014-10-24 20:26:19 -040076 debug("ini%d: connect", i);
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020077 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);
Tom Rini8952ff22014-10-24 20:26:19 -040092 debug("ini_f: [%s]", console_buffer);
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020093 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
Tom Rini8952ff22014-10-24 20:26:19 -040094 debug("ini_f: connected");
Jean-Christophe PLAGNIOL-VILLARDbeb957c2009-04-25 14:57:52 +020095 return 0;
96 }
97 }
98
99 return 0;
100}