1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
8 #include <smsc_sio1007.h>
10 static inline u8 sio1007_read(int port, int reg)
17 static inline void sio1007_write(int port, int reg, int val)
23 static inline void sio1007_clrsetbits(int port, int reg, u8 clr, u8 set)
25 sio1007_write(port, reg, (sio1007_read(port, reg) & ~clr) | set);
28 void sio1007_enable_serial(int port, int num, int iobase, int irq)
30 if (num < 0 || num > SIO1007_UART_NUM)
33 /* enter configuration state */
36 /* power on serial port and set up its i/o base & irq */
38 sio1007_clrsetbits(port, DEV_POWER_CTRL, 0, UART1_POWER_ON);
39 sio1007_clrsetbits(port, UART1_IOBASE, 0xfe, iobase >> 2);
40 sio1007_clrsetbits(port, UART_IRQ, 0xf0, irq << 4);
42 sio1007_clrsetbits(port, DEV_POWER_CTRL, 0, UART2_POWER_ON);
43 sio1007_clrsetbits(port, UART2_IOBASE, 0xfe, iobase >> 2);
44 sio1007_clrsetbits(port, UART_IRQ, 0x0f, irq);
47 /* exit configuration state */
51 void sio1007_enable_runtime(int port, int iobase)
53 /* enter configuration state */
56 /* set i/o base for the runtime register block */
57 sio1007_clrsetbits(port, RTR_IOBASE_LOW, 0, iobase >> 4);
58 sio1007_clrsetbits(port, RTR_IOBASE_HIGH, 0, iobase >> 12);
59 /* turn on address decoding for this block */
60 sio1007_clrsetbits(port, DEV_ACTIVATE, 0, RTR_EN);
62 /* exit configuration state */
66 void sio1007_gpio_config(int port, int gpio, int dir, int pol, int type)
70 if (gpio < 0 || gpio > SIO1007_GPIO_NUM)
72 if (gpio >= GPIO_NUM_PER_GROUP) {
74 gpio -= GPIO_NUM_PER_GROUP;
77 /* enter configuration state */
80 /* set gpio pin direction, polority and type */
81 sio1007_clrsetbits(port, reg, 1 << gpio, dir << gpio);
82 sio1007_clrsetbits(port, reg + 1, 1 << gpio, pol << gpio);
83 sio1007_clrsetbits(port, reg + 2, 1 << gpio, type << gpio);
85 /* exit configuration state */
89 int sio1007_gpio_get_value(int port, int gpio)
94 if (gpio < 0 || gpio > SIO1007_GPIO_NUM)
96 if (gpio >= GPIO_NUM_PER_GROUP) {
98 gpio -= GPIO_NUM_PER_GROUP;
101 val = inb(port + reg);
102 if (val & (1 << gpio))
108 void sio1007_gpio_set_value(int port, int gpio, int val)
110 int reg = GPIO0_DATA;
113 if (gpio < 0 || gpio > SIO1007_GPIO_NUM)
115 if (gpio >= GPIO_NUM_PER_GROUP) {
117 gpio -= GPIO_NUM_PER_GROUP;
120 data = inb(port + reg);
121 data &= ~(1 << gpio);
122 data |= (val << gpio);
123 outb(data, port + reg);