summaryrefslogtreecommitdiff
path: root/board/ti/logic/logic-gpio.c
blob: 1b6318397888c196bed724dcf8111937bf9dc8f2 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * (C) Copyright 2008
 * Logic Product Development, <www.logicpd.com>
 * Peter Barada <peter.barada@logicpd.com>
 *
 * 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/arch/cpu.h>
#include <asm/io.h>
// #include <asm/arch/bits.h>
#include <asm/arch/mux.h>
#include <asm/arch/sys_proto.h>
// #include <asm/arch/sys_info.h>
#include <asm/arch/clocks.h>
#include <asm/arch/mem.h>
#include <i2c.h>
#include <asm/mach-types.h>
#include "logic-gpio.h"

#define NUM_OF_BITS_IN_REG                 32
#define MAX_GPIO_PINS                      192
#define GPIO_PIN                           1
#define GPIO_MAX_MODULES			       6

/* GPIO base address */
#define	GPIO1_MODULE_BA                    0x48310000
#define	GPIO2_MODULE_BA                    0x49050000
#define GPIO3_MODULE_BA                    0x49052000
#define GPIO4_MODULE_BA                    0x49054000
#define GPIO5_MODULE_BA                    0x49056000
#define GPIO6_MODULE_BA                    0x49058000

#define GPIO_DATAIN			((uint32_t)0x038)
#define GPIO_DATAOUT			((uint32_t)0x03C)
#define GPIO_OE				((uint32_t)0x034)

/* int and long both fit to 32 bits */
typedef volatile uint32_t*  PREG_U32;
typedef volatile int32_t*  PREG_S32;

#define in_regl(offSet)			(*(PREG_U32)(offSet))
#define out_regl(offSet, value)       (*(PREG_U32)(offSet) = (uint32_t)(value))

static uint32_t g_gpio_module_base_address[GPIO_MAX_MODULES]
				= {GPIO1_MODULE_BA, GPIO2_MODULE_BA, GPIO3_MODULE_BA,
					 GPIO4_MODULE_BA, GPIO5_MODULE_BA, GPIO6_MODULE_BA};

uint32_t check_gpio_pin_num(uint32_t pin_num)
{
  return (pin_num > MAX_GPIO_PINS);
}

uint32_t get_module_pin_mask(uint32_t pin_num, uint32_t *module_num, uint32_t *offset, uint32_t *pinmask)
{
  uint32_t snum, ret_val;

  *module_num = pin_num / NUM_OF_BITS_IN_REG + 1;
  snum = (*module_num-1)*NUM_OF_BITS_IN_REG;
  *offset = pin_num - snum;
  ret_val = check_gpio_pin_num(pin_num);
  if (ret_val)
    return ret_val;

  *pinmask = GPIO_PIN<<*offset;
  return 0;
}


void gpio_write_output_pin(int module_num, uint32_t pin_mask, uint32_t data)
{
  uint32_t temp, gpio_data_out_reg;

  gpio_data_out_reg = (g_gpio_module_base_address[module_num-1]+GPIO_DATAOUT);

  temp = in_regl(gpio_data_out_reg);
  temp = temp & ~pin_mask;

  out_regl(gpio_data_out_reg, (temp | (data & pin_mask)));
}

void gpio_read_input_pin(uint32_t module_num, uint32_t pin_mask, uint32_t *data)
{
  uint32_t gpio_data_in_reg, temp;

  gpio_data_in_reg = (g_gpio_module_base_address[module_num-1]+GPIO_DATAIN);

  temp = in_regl(gpio_data_in_reg);
  *data = temp & pin_mask;
}

uint32_t pin_get_gpio_input(uint32_t pin_num)
{
  uint32_t module_num, pinmask, offset, data;

  get_module_pin_mask(pin_num, &module_num, &offset, &pinmask);

  gpio_read_input_pin(module_num, (1<<offset), &data);
  data >>= offset;

  // printf("%s:%d pin %d data %d\n", __FUNCTION__, __LINE__, pin_num, data);

  return data;
}


uint32_t pin_set_gpio_dataout(uint32_t pin_num, uint32_t set)
{
  uint32_t module_num, pinmask, offset, ret_val;

  // printf("%s:%d pin %d set %d\n", __FUNCTION__, __LINE__, pin_num, set);

  ret_val = get_module_pin_mask(pin_num, &module_num, &offset, &pinmask);

  if (set)
    gpio_write_output_pin(module_num, (1<<offset), (1<<offset));
  else
    gpio_write_output_pin(module_num, (1<<offset), (0<<offset));

  return ret_val;
}

uint32_t set_gpio_in_out(uint32_t module_num, uint32_t pin_mask, uint32_t io_mask)
{
  uint32_t temp_oe, gpio_pin_output_en_reg;

  gpio_pin_output_en_reg = (g_gpio_module_base_address[module_num-1]+GPIO_OE);

  temp_oe = in_regl(gpio_pin_output_en_reg);
  temp_oe &= ~pin_mask;
  temp_oe |= io_mask;

  out_regl(gpio_pin_output_en_reg, temp_oe);

  return 0;
}

uint32_t pin_init_gpio(uint32_t pin_num, uint32_t in_out)
{
	uint32_t module_num, pinmask, offset, ret_val;

	ret_val = get_module_pin_mask(pin_num, &module_num, &offset, &pinmask);

	set_gpio_in_out(module_num, pinmask, in_out<<offset);

	return ret_val;
}