rt305x_gpio.h revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010 Aleksandr Rybalko.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/11/sys/mips/rt305x/rt305x_gpio.h 330897 2018-03-14 03:19:51Z eadler $
29 */
30#ifndef _RT305X_GPIO_H_
31#define _RT305X_GPIO_H_
32
33#define NGPIO			52
34
35#define RGMII_GPIO_MODE_MASK	(0x0fffULL<<40)
36#define SDRAM_GPIO_MODE_MASK  	(0xffffULL<<24)
37#define MDIO_GPIO_MODE_MASK   	(0x0003ULL<<22)
38#define JTAG_GPIO_MODE_MASK   	(0x001fULL<<17)
39#define UARTL_GPIO_MODE_MASK  	(0x0003ULL<<15)
40#define UARTF_GPIO_MODE_MASK  	(0x00ffULL<<7)
41#define SPI_GPIO_MODE_MASK    	(0x000fULL<<3)
42#define I2C_GPIO_MODE_MASK    	(0x0003ULL<<1)
43
44#define GPIO23_00_INT		0x00 /* Programmed I/O Int Status */
45#define GPIO23_00_EDGE		0x04 /* Programmed I/O Edge Status */
46#define GPIO23_00_RENA		0x08 /* Programmed I/O Int on Rising */
47#define GPIO23_00_FENA		0x0C /* Programmed I/O Int on Falling */
48#define GPIO23_00_DATA		0x20 /* Programmed I/O Data */
49#define GPIO23_00_DIR		0x24 /* Programmed I/O Direction */
50#define GPIO23_00_POL		0x28 /* Programmed I/O Pin Polarity */
51#define GPIO23_00_SET		0x2C /* Set PIO Data Bit */
52#define GPIO23_00_RESET		0x30 /* Clear PIO Data bit */
53#define GPIO23_00_TOG		0x34 /* Toggle PIO Data bit */
54
55#define GPIO39_24_INT		0x38
56#define GPIO39_24_EDGE		0x3c
57#define GPIO39_24_RENA		0x40
58#define GPIO39_24_FENA		0x44
59#define GPIO39_24_DATA		0x48
60#define GPIO39_24_DIR		0x4c
61#define GPIO39_24_POL		0x50
62#define GPIO39_24_SET		0x54
63#define GPIO39_24_RESET		0x58
64#define GPIO39_24_TOG		0x5c
65
66#define GPIO51_40_INT		0x60
67#define GPIO51_40_EDGE		0x64
68#define GPIO51_40_RENA		0x68
69#define GPIO51_40_FENA		0x6C
70#define GPIO51_40_DATA		0x70
71#define GPIO51_40_DIR		0x74
72#define GPIO51_40_POL		0x78
73#define GPIO51_40_SET		0x7C
74#define GPIO51_40_RESET		0x80
75#define GPIO51_40_TOG		0x84
76
77#define GPIO_REG(g, n)							\
78	((g<24)?(GPIO23_00_##n):(g<40)?(GPIO39_24_##n):(GPIO51_40_##n))
79#define GPIO_MASK(g)							\
80	((g<24)?(1<<g):(g<40)?(1<<(g-24)):(1<<(g-40)))
81#define GPIO_BIT_SHIFT(g)	((g<24)?(g):(g<40)?(g-24):(g-40))
82
83#define GPIO_READ(r, g, n) 						\
84	bus_read_4(r->gpio_mem_res, GPIO_REG(g, n))
85#define GPIO_WRITE(r, g, n, v) 						\
86	bus_write_4(r->gpio_mem_res, GPIO_REG(g, n), v)
87#define GPIO_READ_ALL(r, n) 						\
88	(((uint64_t)bus_read_4(r->gpio_mem_res, GPIO23_00_##n)) |	\
89	(((uint64_t)bus_read_4(r->gpio_mem_res, GPIO39_24_##n)) << 24) |\
90	(((uint64_t)bus_read_4(r->gpio_mem_res, GPIO51_40_##n)) << 40))
91#define GPIO_WRITE_ALL(r, n, v) 					\
92	{bus_write_4(r->gpio_mem_res,GPIO23_00_##n, v      &0x00ffffff);\
93	bus_write_4(r->gpio_mem_res, GPIO39_24_##n, (v>>24)&0x0000ffff);\
94	bus_write_4(r->gpio_mem_res, GPIO51_40_##n, (v>>40)&0x00000fff);}
95
96
97#define GPIO_BIT_CLR(r, g, n) 						\
98	bus_write_4(r->gpio_mem_res, GPIO_REG(g, n), 			\
99	    bus_read_4(r->gpio_mem_res, GPIO_REG(g, n)) & ~GPIO_MASK(g))
100#define GPIO_BIT_SET(r, g, n) 						\
101	bus_write_4(r->gpio_mem_res, GPIO_REG(g, n), 			\
102	    bus_read_4(r->gpio_mem_res, GPIO_REG(g, n)) | GPIO_MASK(g))
103
104#define GPIO_BIT_GET(r, g, n)						\
105	((bus_read_4(r->gpio_mem_res, GPIO_REG(g, n)) >> 		\
106	    GPIO_BIT_SHIFT(g)) & 1)
107
108#define GPIO_LOCK(_sc)		mtx_lock(&(_sc)->gpio_mtx)
109#define GPIO_UNLOCK(_sc)	mtx_unlock(&(_sc)->gpio_mtx)
110#define GPIO_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->gpio_mtx, MA_OWNED)
111
112#endif /* _RT305X_GPIO_H_ */
113
114