summaryrefslogtreecommitdiffstats
path: root/u-boot/board/goldelico/gta04/gps.c
blob: 3ac542435a545176db42dfe6576adf645b1bdb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* u-boot driver for the GTA04 LEDs and Buttons
 *
 * Copyright (C) 2010 by Golden Delicious Computers GmbH&Co. KG
 * Author: H. Nikolaus Schaller <hns@goldelico.com>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */

#include <common.h>
#include <asm/errno.h>
#include <asm/io.h>
#include <asm/arch/mux.h>
#include <asm/arch/sys_proto.h>
#include <asm/arch/gpio.h>
#include <asm/mach-types.h>
#include <ns16550.h>
#include <twl4030.h>
#include "gps.h"

#ifdef CONFIG_OMAP3_GTA04

#define GPIO_GPS_EXT	144		// external GPS antenna plugged in
#define GPIO_GPS_ON		145		// reset for GPS module
#define GPIO_GPS_PPS	114		// Pulse per Second interrupt

#else /* Beagle Hybrid */

#define GPIO_GPS_EXT	144		// external GPS antenna plugged in
#define GPIO_GPS_ON		156
#define GPIO_GPS_PPS	138		// Pulse per Second interrupt

#endif

int gps_init(void)
{
	extern int get_board_revision(void);
#define REVISION_XM 0
	if(get_board_revision() == REVISION_XM) {
		/* Set VAUX1 to 3.3V for GTA04E display board */
		twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VAUX1_DEDICATED,
							/*TWL4030_PM_RECEIVER_VAUX1_VSEL_33*/ 0x07,
							TWL4030_PM_RECEIVER_VAUX1_DEV_GRP,
							TWL4030_PM_RECEIVER_DEV_GRP_P1);
		udelay(5000);
	}
#ifdef CONFIG_OMAP3_GTA04
	/* ext. GPS Ant VSIM = 2.8 V (3.0V) */
	twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VSIM_DEDICATED,
							/*TWL4030_PM_RECEIVER_VSIM_VSEL_28*/ 0x04 /* 0x05 */,
							TWL4030_PM_RECEIVER_VSIM_DEV_GRP,
							TWL4030_PM_RECEIVER_DEV_GRP_P1);
	udelay(5000);
#endif
	
	omap_request_gpio(GPIO_GPS_ON);
	omap_set_gpio_direction(GPIO_GPS_ON, 0);		// output
	omap_request_gpio(GPIO_GPS_EXT);
	omap_set_gpio_direction(GPIO_GPS_EXT, 1);		// input
	omap_request_gpio(GPIO_GPS_PPS);
	omap_set_gpio_direction(GPIO_GPS_PPS, 1);		// input
	return 0;
}

void gps_on(void)
{
	omap_set_gpio_dataout(GPIO_GPS_ON, 1);
	if(omap_get_gpio_datain(GPIO_GPS_EXT))
		printf("external antenna\n");
	else
		printf("internal antenna\n");
}

void gps_off(void)
{
	omap_set_gpio_dataout(GPIO_GPS_ON, 0);
}

static int lastant=-1;
static int lastpps=-1;
static long timer;

#define TIMEOUT 2	// in seconds

void gps_echo(void)
{
	#define MODE_X_DIV 16
	int baudrate=9600;
	int divisor=(CONFIG_SYS_NS16550_CLK + (baudrate * (MODE_X_DIV / 2))) / (MODE_X_DIV * baudrate);
	NS16550_reinit((NS16550_t)CONFIG_SYS_NS16550_COM2, divisor);	// initialize UART
	while (1)
		{ // echo in both directions
			int ant=omap_get_gpio_datain(GPIO_GPS_EXT);
			int pps=omap_get_gpio_datain(GPIO_GPS_PPS);
			if(ant != lastant)
				{ // changed
				if(ant)
					printf("external antenna\n");
				else
					printf("internal antenna\n");
				lastant=ant;
				}
			if(pps != lastpps)
				{ // comes only with >= 5 satellites
				if(lastpps >= 0)
					printf("PPS\n");
				lastpps=pps;
				}
			if(NS16550_tstc((NS16550_t)CONFIG_SYS_NS16550_COM2))
				{
				putc(NS16550_getc((NS16550_t)CONFIG_SYS_NS16550_COM2));	// from GPS to console
				timer=0;	// data received
				}
			if(tstc())
				{
				int c=getc();
				if(c == 0x03)	// ctrl-C
					break;
				// NS16550_putc((NS16550_t)CONFIG_SYS_NS16550_COM2, c);
				break;
				}
			if(timer++ > 2*10000)
				{ // timeout - try to wakeup/reset the chip
					printf("no data: on-off impulse\n");
					omap_set_gpio_dataout(GPIO_GPS_ON, 1);
					udelay(5000);
					omap_set_gpio_dataout(GPIO_GPS_ON, 0);
					timer=0;
				}
			udelay(100);	// 10 kHz @ 9 kbit/s
		}
	printf("\n");
}