1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
9 #include <smsc_sio1007.h>
11 static inline u8 sio1007_read(int port, int reg)
18 static inline void sio1007_write(int port, int reg, int val)
24 static inline void sio1007_clrsetbits(int port, int reg, u8 clr, u8 set)
26 sio1007_write(port, reg, (sio1007_read(port, reg) & ~clr) | set);
29 void sio1007_enable_serial(int port, int num, int iobase, int irq)
31 if (num < 0 || num > SIO1007_UART_NUM)
34 /* enter configuration state */
37 /* power on serial port and set up its i/o base & irq */
39 sio1007_clrsetbits(port, DEV_POWER_CTRL, 0, UART1_POWER_ON);
40 sio1007_clrsetbits(port, UART1_IOBASE, 0xfe, iobase >> 2);
41 sio1007_clrsetbits(port, UART_IRQ, 0xf0, irq << 4);
43 sio1007_clrsetbits(port, DEV_POWER_CTRL, 0, UART2_POWER_ON);
44 sio1007_clrsetbits(port, UART2_IOBASE, 0xfe, iobase >> 2);
45 sio1007_clrsetbits(port, UART_IRQ, 0x0f, irq);
48 /* exit configuration state */
52 void sio1007_enable_runtime(int port, int iobase)
54 /* enter configuration state */
57 /* set i/o base for the runtime register block */
58 sio1007_clrsetbits(port, RTR_IOBASE_LOW, 0, iobase >> 4);
59 sio1007_clrsetbits(port, RTR_IOBASE_HIGH, 0, iobase >> 12);
60 /* turn on address decoding for this block */
61 sio1007_clrsetbits(port, DEV_ACTIVATE, 0, RTR_EN);
63 /* exit configuration state */
67 void sio1007_gpio_config(int port, int gpio, int dir, int pol, int type)
71 if (gpio < 0 || gpio > SIO1007_GPIO_NUM)
73 if (gpio >= GPIO_NUM_PER_GROUP) {
75 gpio -= GPIO_NUM_PER_GROUP;
78 /* enter configuration state */
81 /* set gpio pin direction, polority and type */
82 sio1007_clrsetbits(port, reg, 1 << gpio, dir << gpio);
83 sio1007_clrsetbits(port, reg + 1, 1 << gpio, pol << gpio);
84 sio1007_clrsetbits(port, reg + 2, 1 << gpio, type << gpio);
86 /* exit configuration state */
90 int sio1007_gpio_get_value(int port, int gpio)
95 if (gpio < 0 || gpio > SIO1007_GPIO_NUM)
97 if (gpio >= GPIO_NUM_PER_GROUP) {
99 gpio -= GPIO_NUM_PER_GROUP;
102 val = inb(port + reg);
103 if (val & (1 << gpio))
109 void sio1007_gpio_set_value(int port, int gpio, int val)
111 int reg = GPIO0_DATA;
114 if (gpio < 0 || gpio > SIO1007_GPIO_NUM)
116 if (gpio >= GPIO_NUM_PER_GROUP) {
118 gpio -= GPIO_NUM_PER_GROUP;
121 data = inb(port + reg);
122 data &= ~(1 << gpio);
123 data |= (val << gpio);
124 outb(data, port + reg);