blob: a017b2963af0c5f56bd02cb6b243faa1adbfe8c0 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARD55e0ed62009-04-25 14:57:52 +02001/*
2 * (C) Copyright 2002-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25
26/* 'inline' - We have to do it fast */
27static inline void mdm_readline(char *buf, int bufsiz)
28{
29 char c;
30 char *p;
31 int n;
32
33 n = 0;
34 p = buf;
35 for(;;) {
36 c = serial_getc();
37
38 /* dbg("(%c)", c); */
39
40 switch(c) {
41 case '\r':
42 break;
43 case '\n':
44 *p = '\0';
45 return;
46
47 default:
48 if(n++ > bufsiz) {
49 *p = '\0';
50 return; /* sanity check */
51 }
52 *p = c;
53 p++;
54 break;
55 }
56 }
57}
58
59extern void dbg(const char *fmt, ...);
60int mdm_init (void)
61{
62 char env_str[16];
63 char *init_str;
64 int i;
65 extern char console_buffer[];
66 extern void enable_putc(void);
67 extern int hwflow_onoff(int);
68
69 enable_putc(); /* enable serial_putc() */
70
71#ifdef CONFIG_HWFLOW
72 init_str = getenv("mdm_flow_control");
73 if (init_str && (strcmp(init_str, "rts/cts") == 0))
74 hwflow_onoff (1);
75 else
76 hwflow_onoff(-1);
77#endif
78
79 for (i = 1;;i++) {
80 sprintf(env_str, "mdm_init%d", i);
81 if ((init_str = getenv(env_str)) != NULL) {
82 serial_puts(init_str);
83 serial_puts("\n");
84 for(;;) {
85 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
86 dbg("ini%d: [%s]", i, console_buffer);
87
88 if ((strcmp(console_buffer, "OK") == 0) ||
89 (strcmp(console_buffer, "ERROR") == 0)) {
90 dbg("ini%d: cmd done", i);
91 break;
92 } else /* in case we are originating call ... */
93 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
94 dbg("ini%d: connect", i);
95 return 0;
96 }
97 }
98 } else
99 break; /* no init string - stop modem init */
100
101 udelay(100000);
102 }
103
104 udelay(100000);
105
106 /* final stage - wait for connect */
107 for(;i > 1;) { /* if 'i' > 1 - wait for connection
108 message from modem */
109 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
110 dbg("ini_f: [%s]", console_buffer);
111 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
112 dbg("ini_f: connected");
113 return 0;
114 }
115 }
116
117 return 0;
118}