tgpio.h 11.1 KB
/**    
    @file     tgpio.h
    @date     2010/1/4
    @author   오재경 freefrug@falinux.com  FALinux.Co.,Ltd.
    @brief    gpio 를 드라이버없이 mmap를 이용하여 제어한다.
              Ver 0.6.0
             
              
    @modify   
    @todo     
    @bug     
    @remark   
    @warning 
*/
//----------------------------------------------------------------------------


#ifndef _TGPIO_HEADER_
#define _TGPIO_HEADER_

#define  MCU_AM3874        3874
#define  MCU_S5PV210       210
#define  MCU_S3C6410       6410
#define  MCU_S3C2440       2440
#define  MCU_PXA270        270
#define  MCU_PXA255        255
#define  MCU_IXP420        420
#define  MCU_IXP430        430
#define  MCU_IXP435        435
#define  MCU_iMX6	       1006
#define  MCU_iMX6_STR     "imx6"


#define  MCU_iMX6_GPIO_GRP_CNT         7
#define  MCU_iMX6_GPIO1_PHYS           0x0209C000
#define  MCU_iMX6_GPIO2_PHYS           0x020A0000
#define  MCU_iMX6_GPIO3_PHYS           0x020A4000
#define  MCU_iMX6_GPIO4_PHYS           0x020A8000
#define  MCU_iMX6_GPIO5_PHYS           0x020AC000
#define  MCU_iMX6_GPIO6_PHYS           0x020B0000
#define  MCU_iMX6_GPIO7_PHYS           0x020B4000
#define  MCU_iMX6_GPIO_PHYS_SIZE       0x1000

#define  MCU_iMX6_GPIO_DAT_OFS         0x00
#define  MCU_iMX6_GPIO_DIR_OFS         0x04
#define  iMX6_GPIO_DATPORT(base)       *(volatile unsigned long *)( (int)(base) + MCU_iMX6_GPIO_DAT_OFS  )
#define  iMX6_GPIO_DIRPORT(base)       *(volatile unsigned long *)( (int)(base) + MCU_iMX6_GPIO_DIR_OFS  )
#define  MCU_iMX6_GPIO_DIR_INPUT       0
#define  MCU_iMX6_GPIO_DIR_OUTPUT      1


#define  MCU_AM3874_GPIO_GRP_CNT	   4
#define  MCU_AM3874_GPIO0_PHYS		   0x48032000
#define  MCU_AM3874_GPIO1_PHYS		   0x4804C000
#define  MCU_AM3874_GPIO2_PHYS		   0x481AC000
#define  MCU_AM3874_GPIO3_PHYS		   0x481AE000
#define  MCU_AM3874_MMAP_GPIO_SIZE     0x1000

#define  MCU_AM3874_GPIO_SYSCONF	   0x010
#define  MCU_AM3874_GPIO_CON		   0x130
#define  MCU_AM3874_GPIO_OE	           0x134
#define  MCU_AM3874_GPIO_DATAIN        0x138
#define  MCU_AM3874_GPIO_DATAOUT       0x13C
#define  AM3874_GPIO_SYSCONF(base)     *(volatile unsigned long *)( (int)(base) + MCU_AM3874_GPIO_SYSCONF )
#define  AM3874_GPIO_CTRL(base)        *(volatile unsigned long *)( (int)(base) + MCU_AM3874_GPIO_CON     )
#define  AM3874_GPIO_OE(base)          *(volatile unsigned long *)( (int)(base) + MCU_AM3874_GPIO_OE      )
#define  AM3874_GPIO_DATAIN(base)      *(volatile unsigned long *)( (int)(base) + MCU_AM3874_GPIO_DATAIN  )
#define  AM3874_GPIO_DATAOUT(base)     *(volatile unsigned long *)( (int)(base) + MCU_AM3874_GPIO_DATAOUT )

#define  MCU_AM3874_PIN_MUX_PHYS	   0x48140800
#define  MCU_AM3874_PIN_MUX_SIZE	   0x8000
#define  MCU_AM3874_PIN_MUX_GPIO       (0x80)
#define  MCU_AM3874_PIN_MUX_IDIS       (0 << 18)
#define  MCU_AM3874_PIN_MUX_IEN        (1 << 18)
#define  MCU_AM3874_PIN_MUX_IPD        (0 << 16)
#define  MCU_AM3874_PIN_MUX_IPU        (2 << 16)
#define  MCU_AM3874_PIN_MUX_IPDIS      (1 << 16)
#define  AM3874_PIN_CTRL(base,pin)     *(volatile unsigned long *)( (int)(base) + (pin-1)*4 )

#define  MCU_S5PV210_MMAP_GPIO_PHYS    0xe0200000
#define  MCU_S5PV210_MMAP_GPIO_SIZE    0x1000

#define  MCU_S3C6410_MMAP_GPIO_PHYS    0x7f008000
#define  MCU_S3C6410_MMAP_GPIO_SIZE    0x1000

#define  MCU_S3C2440_MMAP_GPIO_PHYS    0x56000000
#define  MCU_S3C2440_MMAP_GPIO_SIZE    0x100

#define  MCU_PXA270_MMAP_GPIO_PHYS     0x40E00000
#define  MCU_PXA270_MMAP_GPIO_SIZE     0x200

#define  MCU_PXA255_MMAP_GPIO_PHYS     0x40E00000
#define  MCU_PXA255_MMAP_GPIO_SIZE     0x200

#define  MCU_IXP4XX_MMAP_GPIO_PHYS	   0xC8004000
#define  MCU_IXP4XX_MMAP_GPIO_SIZE	   0x10


/// PXA 관련 레지스터 
#define  PXA_GPIO_bit(x)         (1 << ((x) & 0x1f))
#define  PXA_REG_OFFSET(x)       ( ((x) < 96) ? ((x)/32)*4 : 0x100  )

#define  PXA_GPIO_INOUT(nr)     *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + GAFR0_L + PXA_REG_OFFSET(nr*2) ) &= ~(3<<(((nr)&0xf)*2))
#define  PXA_DIR_OUT(nr)        *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + GPDR0   + PXA_REG_OFFSET(nr)   ) |=  PXA_GPIO_bit(nr)
#define  PXA_DIR_IN(nr)         *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + GPDR0   + PXA_REG_OFFSET(nr)   ) &= ~PXA_GPIO_bit(nr)
#define  PXA_OUT_CLR(nr)        *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + GPCR0   + PXA_REG_OFFSET(nr)   )  =  PXA_GPIO_bit(nr)
#define  PXA_OUT_SET(nr)        *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + GPSR0   + PXA_REG_OFFSET(nr)   )  =  PXA_GPIO_bit(nr)
#define  PXA_IN_DAT(nr)       ( *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + GPLR0   + PXA_REG_OFFSET(nr)   ) & PXA_GPIO_bit(nr) )


#define  GPLR0     0x000     // GPIO Pin-Level Register GPIO<31:0> 
#define  GPLR1     0x004     // GPIO Pin-Level Register GPIO<63:32> 
#define  GPLR2     0x008     // GPIO Pin-Level Register GPIO<80:64> 
                             // 
#define  GPDR0     0x00C     // GPIO Pin Direction Register GPIO<31:0> 
#define  GPDR1     0x010     // GPIO Pin Direction Register GPIO<63:32> 
#define  GPDR2     0x014     // GPIO Pin Direction Register GPIO<80:64> 
                             // 
#define  GPSR0     0x018     // GPIO Pin Output Set Register GPIO<31:0> 
#define  GPSR1     0x01C     // GPIO Pin Output Set Register GPIO<63:32> 
#define  GPSR2     0x020     // GPIO Pin Output Set Register GPIO<80:64> 
                             // 
#define  GPCR0     0x024     // GPIO Pin Output Clear Register GPIO<31:0> 
#define  GPCR1     0x028     // GPIO Pin Output Clear Register GPIO <63:32> 
#define  GPCR2     0x02C     // GPIO Pin Output Clear Register GPIO <80:64> 
                             // 
#define  GRER0     0x030     // GPIO Rising-Edge Detect Register GPIO<31:0> 
#define  GRER1     0x034     // GPIO Rising-Edge Detect Register GPIO<63:32> 
#define  GRER2     0x038     // GPIO Rising-Edge Detect Register GPIO<80:64> 
                             // 
#define  GFER0     0x03C     // GPIO Falling-Edge Detect Register GPIO<31:0> 
#define  GFER1     0x040     // GPIO Falling-Edge Detect Register GPIO<63:32>
#define  GFER2     0x044     // GPIO Falling-Edge Detect Register GPIO<80:64>
                             // 
#define  GEDR0     0x048     // GPIO Edge Detect Status Register GPIO<31:0>
#define  GEDR1     0x04C     // GPIO Edge Detect Status Register GPIO<63:32>
#define  GEDR2     0x050     // GPIO Edge Detect Status Register GPIO<80:64> 
                             // 
#define  GAFR0_L   0x054     // GPIO Alternate Function Select Register GPIO<15:0> 
#define  GAFR0_U   0x058     // GPIO Alternate Function Select Register GPIO<31:16> 
#define  GAFR1_L   0x05C     // GPIO Alternate Function Select Register GPIO<47:32> 
#define  GAFR1_U   0x060     // GPIO Alternate Function Select Register GPIO<63:48> 
#define  GAFR2_L   0x064     // GPIO Alternate Function Select Register GPIO<79:64> 
#define  GAFR2_U   0x068     // GPIO Alternate Function Select Register GPIO<95-80> 
#define  GAFR3_L   0x06C     // GPIO Alternate Function Select Register GPIO<111:96> 
#define  GAFR3_U   0x070     // GPIO Alternate Function Select Register GPIO<127:112>
                             // 
#define  GPLR3     0x100     // GPIO Pin-Level Register GPIO<127:96> 
#define  GPDR3     0x10C     // GPIO Pin Direction Register GPIO<127:96> 
#define  GPSR3     0x118     // GPIO Pin Output Set Register GPIO<127:96>
#define  GPCR3     0x124     // GPIO Pin Output Clear Register GPIO<127:96> 
#define  GRER3     0x130     // GPIO Rising-Edge Detect Register GPIO<127:96> 
#define  GFER3     0x13C     // GPIO Falling-Edge Detect Register GPIO<127:96>
#define  GEDR3     0x148     // GPIO Edge Detect Status Register GPIO<127:96> 

/// IXP 관련 레지스터 
#define IXP4XX_GPIO_GPOUTR		*(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + IXP4XX_GPIO_GPOUTR_OFFSET )
#define IXP4XX_GPIO_GPOER       *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + IXP4XX_GPIO_GPOER_OFFSET  )
#define IXP4XX_GPIO_GPINR       *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + IXP4XX_GPIO_GPINR_OFFSET  )
#define IXP4XX_GPIO_GPISR       *(volatile unsigned long *)( (int)(mcu_gpio.mmap_base) + IXP4XX_GPIO_GPISR_OFFSET  )

#define IXP4XX_GPIO_GPOUTR_OFFSET    0x00
#define IXP4XX_GPIO_GPOER_OFFSET     0x04
#define IXP4XX_GPIO_GPINR_OFFSET     0x08
#define IXP4XX_GPIO_GPISR_OFFSET     0x0C



//======================================================================================================================

/// tgpio_dir_input() 함수 사용시  pull_up 옵션
typedef enum {	
	GPIO_PULL_NONE = 0,
	GPIO_PULL_UP,
	GPIO_PULL_DN
} gpio_pulled_t;


/// GPIO 그룹이 GRP0, GRP1 등으로 나뉠때  번호로써 구분한다.
#define GP_NR_GRP		1000

#include <famap.h>

/// gpio 관련 내부관리 구조체
typedef struct {

	int   mcu_nr;
	char  grp_begin;
	char  grp_end;
	unsigned long  phys_base;
	unsigned long  phys_size;

	mmap_alloc_t   map_info;		// 0.7.0 부터 추가
	unsigned long  mmap_base;
	
	void  (*close     )( void );
	void  (*dir_input )( char grp, int gp_nr, gpio_pulled_t pull_up );
	void  (*dir_output)( char grp, int gp_nr, int gp_val  );
	int   (*input     )( char grp, int gp_nr );
	void  (*output    )( char grp, int gp_nr, int gp_val  );

	unsigned long  (*get_input_port  )( char grp, int gp_nr );
	unsigned long  (*get_output_port )( char grp, int gp_nr );

	void  *private;

} mcu_gpio_fops_t;

/// gpio 관련 iMX 레지스터 구조체
typedef struct 
{
	unsigned long  phys;
	mmap_alloc_t   map_gpio;
	unsigned long  mmap_base;

} imx6_gpio_register_t;


/// gpio 관련 AM3xxx 레지스터 구조체
typedef struct {

	unsigned long  phys;
	mmap_alloc_t   map_gpio;
	unsigned long  mmap_base;
	unsigned short pin_ctrl[32];	// 1 ~ n

} am3x_gpio_register_t;

/// gpio 관련 s3c 레지스터 구조체
typedef struct {

	int            gpcnt;
	int            con_mask_bitcnt;
	unsigned long  ofs_con;
	unsigned long  ofs_dat;
	unsigned long  ofs_pup;

} s3c_gpio_register_t;


/// gpio 관련 pxa 정보 구조체
typedef struct {

	int            gpcnt;

} pxa_gpio_info_t;


/// gpio 번호 구조체
typedef struct {

	char            grp;
	unsigned short  nr;

} gpio_number_t;

#endif //


#ifdef __cplusplus 
extern "C" { 
#endif 	

/// @{
/// @brief   tgpio.h 에서 제공하는 함수 리스트 

extern int   tgpio_open      ( int mcu ); /// mcu 를 선택한 후 mmap 메모리를 맵핑한다.
extern void  tgpio_close     ( void    ); /// gpio 맵핑을 해제한다.
extern void  tgpio_dir_input ( char grp, int gp_nr, gpio_pulled_t pull_up ); /// gpio 를 입력으로 설정한다.
extern void  tgpio_dir_output( char grp, int gp_nr, int gp_val  ); /// gpio 를 출력으로 설정한다.
extern int   tgpio_input     ( char grp, int gp_nr              ); /// gpio 의 입력값을 읽는다.
extern void  tgpio_output    ( char grp, int gp_nr, int gp_val  ); /// gpio 에 값을 쓴다

extern unsigned long tgpio_get_input_port ( char grp, int gp_nr ); /// gpio 의 입력 포트 포인터를 얻는다.
extern unsigned long tgpio_get_output_port( char grp, int gp_nr ); /// gpio 의 출력 포트 포인터를 얻는다.

/// @}

#ifdef __cplusplus 
}
#endif