1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/arch/arm/mach-omap1/serial.c 4 * 5 * OMAP1 serial support. 6 */ 7 #include <linux/gpio/machine.h> 8 #include <linux/gpio/consumer.h> 9 #include <linux/module.h> 10 #include <linux/kernel.h> 11 #include <linux/init.h> 12 #include <linux/irq.h> 13 #include <linux/delay.h> 14 #include <linux/serial.h> 15 #include <linux/tty.h> 16 #include <linux/serial_8250.h> 17 #include <linux/serial_reg.h> 18 #include <linux/clk.h> 19 #include <linux/io.h> 20 21 #include <asm/mach-types.h> 22 23 #include "serial.h" 24 #include "mux.h" 25 #include "pm.h" 26 #include "soc.h" 27 28 static struct clk * uart1_ck; 29 static struct clk * uart2_ck; 30 static struct clk * uart3_ck; 31 32 static inline unsigned int omap_serial_in(struct plat_serial8250_port *up, 33 int offset) 34 { 35 offset <<= up->regshift; 36 return (unsigned int)__raw_readb(up->membase + offset); 37 } 38 39 static inline void omap_serial_outp(struct plat_serial8250_port *p, int offset, 40 int value) 41 { 42 offset <<= p->regshift; 43 __raw_writeb(value, p->membase + offset); 44 } 45 46 /* 47 * Internal UARTs need to be initialized for the 8250 autoconfig to work 48 * properly. Note that the TX watermark initialization may not be needed 49 * once the 8250.c watermark handling code is merged. 50 */ 51 static void __init omap_serial_reset(struct plat_serial8250_port *p) 52 { 53 omap_serial_outp(p, UART_OMAP_MDR1, 54 UART_OMAP_MDR1_DISABLE); /* disable UART */ 55 omap_serial_outp(p, UART_OMAP_SCR, 0x08); /* TX watermark */ 56 omap_serial_outp(p, UART_OMAP_MDR1, 57 UART_OMAP_MDR1_16X_MODE); /* enable UART */ 58 59 if (!cpu_is_omap15xx()) { 60 omap_serial_outp(p, UART_OMAP_SYSC, 0x01); 61 while (!(omap_serial_in(p, UART_OMAP_SYSC) & 0x01)); 62 } 63 } 64 65 static struct plat_serial8250_port serial_platform_data[] = { 66 { 67 .mapbase = OMAP1_UART1_BASE, 68 .irq = INT_UART1, 69 .flags = UPF_BOOT_AUTOCONF, 70 .iotype = UPIO_MEM, 71 .regshift = 2, 72 .uartclk = OMAP16XX_BASE_BAUD * 16, 73 }, 74 { 75 .mapbase = OMAP1_UART2_BASE, 76 .irq = INT_UART2, 77 .flags = UPF_BOOT_AUTOCONF, 78 .iotype = UPIO_MEM, 79 .regshift = 2, 80 .uartclk = OMAP16XX_BASE_BAUD * 16, 81 }, 82 { 83 .mapbase = OMAP1_UART3_BASE, 84 .irq = INT_UART3, 85 .flags = UPF_BOOT_AUTOCONF, 86 .iotype = UPIO_MEM, 87 .regshift = 2, 88 .uartclk = OMAP16XX_BASE_BAUD * 16, 89 }, 90 { }, 91 }; 92 93 static struct platform_device serial_device = { 94 .name = "serial8250", 95 .id = PLAT8250_DEV_PLATFORM, 96 .dev = { 97 .platform_data = serial_platform_data, 98 }, 99 }; 100 101 /* 102 * Note that on Innovator-1510 UART2 pins conflict with USB2. 103 * By default UART2 does not work on Innovator-1510 if you have 104 * USB OHCI enabled. To use UART2, you must disable USB2 first. 105 */ 106 void __init omap_serial_init(void) 107 { 108 int i; 109 110 if (cpu_is_omap15xx()) { 111 serial_platform_data[0].uartclk = OMAP1510_BASE_BAUD * 16; 112 serial_platform_data[1].uartclk = OMAP1510_BASE_BAUD * 16; 113 serial_platform_data[2].uartclk = OMAP1510_BASE_BAUD * 16; 114 } 115 116 for (i = 0; i < ARRAY_SIZE(serial_platform_data) - 1; i++) { 117 /* Static mapping, never released */ 118 serial_platform_data[i].membase = 119 ioremap(serial_platform_data[i].mapbase, SZ_2K); 120 if (!serial_platform_data[i].membase) { 121 printk(KERN_ERR "Could not ioremap uart%i\n", i); 122 continue; 123 } 124 switch (i) { 125 case 0: 126 uart1_ck = clk_get(NULL, "uart1_ck"); 127 if (IS_ERR(uart1_ck)) 128 printk("Could not get uart1_ck\n"); 129 else { 130 clk_prepare_enable(uart1_ck); 131 if (cpu_is_omap15xx()) 132 clk_set_rate(uart1_ck, 12000000); 133 } 134 break; 135 case 1: 136 uart2_ck = clk_get(NULL, "uart2_ck"); 137 if (IS_ERR(uart2_ck)) 138 printk("Could not get uart2_ck\n"); 139 else { 140 clk_prepare_enable(uart2_ck); 141 if (cpu_is_omap15xx()) 142 clk_set_rate(uart2_ck, 12000000); 143 else 144 clk_set_rate(uart2_ck, 48000000); 145 } 146 break; 147 case 2: 148 uart3_ck = clk_get(NULL, "uart3_ck"); 149 if (IS_ERR(uart3_ck)) 150 printk("Could not get uart3_ck\n"); 151 else { 152 clk_prepare_enable(uart3_ck); 153 if (cpu_is_omap15xx()) 154 clk_set_rate(uart3_ck, 12000000); 155 } 156 break; 157 } 158 omap_serial_reset(&serial_platform_data[i]); 159 } 160 } 161 162 #ifdef CONFIG_OMAP_SERIAL_WAKE 163 164 static irqreturn_t omap_serial_wake_interrupt(int irq, void *dev_id) 165 { 166 /* Need to do something with serial port right after wake-up? */ 167 return IRQ_HANDLED; 168 } 169 170 /* 171 * Reroutes serial RX lines to GPIO lines for the duration of 172 * sleep to allow waking up the device from serial port even 173 * in deep sleep. 174 */ 175 void omap_serial_wake_trigger(int enable) 176 { 177 if (!cpu_is_omap16xx()) 178 return; 179 180 if (uart1_ck != NULL) { 181 if (enable) 182 omap_cfg_reg(V14_16XX_GPIO37); 183 else 184 omap_cfg_reg(V14_16XX_UART1_RX); 185 } 186 if (uart2_ck != NULL) { 187 if (enable) 188 omap_cfg_reg(R9_16XX_GPIO18); 189 else 190 omap_cfg_reg(R9_16XX_UART2_RX); 191 } 192 if (uart3_ck != NULL) { 193 if (enable) 194 omap_cfg_reg(L14_16XX_GPIO49); 195 else 196 omap_cfg_reg(L14_16XX_UART3_RX); 197 } 198 } 199 200 static void __init omap_serial_set_port_wakeup(int idx) 201 { 202 struct gpio_desc *d; 203 int ret; 204 205 d = gpiod_get_index(NULL, "wakeup", idx, GPIOD_IN); 206 if (IS_ERR(d)) { 207 pr_err("Unable to get UART wakeup GPIO descriptor\n"); 208 return; 209 } 210 ret = request_irq(gpiod_to_irq(d), &omap_serial_wake_interrupt, 211 IRQF_TRIGGER_RISING, "serial wakeup", NULL); 212 if (ret) { 213 gpiod_put(d); 214 pr_err("No interrupt for UART%d wake GPIO\n", idx + 1); 215 return; 216 } 217 enable_irq_wake(gpiod_to_irq(d)); 218 } 219 220 221 int __init omap_serial_wakeup_init(void) 222 { 223 if (!cpu_is_omap16xx()) 224 return 0; 225 226 if (uart1_ck != NULL) 227 omap_serial_set_port_wakeup(0); 228 if (uart2_ck != NULL) 229 omap_serial_set_port_wakeup(1); 230 if (uart3_ck != NULL) 231 omap_serial_set_port_wakeup(2); 232 233 return 0; 234 } 235 236 #endif /* CONFIG_OMAP_SERIAL_WAKE */ 237 238 static int __init omap_init(void) 239 { 240 if (!cpu_class_is_omap1()) 241 return -ENODEV; 242 243 return platform_device_register(&serial_device); 244 } 245 arch_initcall(omap_init); 246