summaryrefslogtreecommitdiff
path: root/drivers/tty/serial/8250/8250_airoha.c
blob: 21f248e20771a636b0f9b7cddd50d427e883811f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: GPL-2.0+
/*
 * Airoha UART driver
 *
 * Copyright (c) 2025 Genexis Sweden AB
 * Author: Benjamin Larsson <benjamin.larsson@genexis.eu>
 *	   Christian Marangi <ansuelsmth@gmail.com>
 */

#include <linux/array_size.h>
#include <linux/bits.h>
#include <linux/bitfield.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/math64.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/platform_device.h>
#include <linux/serial_8250.h>
#include <linux/serial_core.h>
#include <linux/serial_reg.h>
#include <linux/types.h>
#include <linux/units.h>

#include "8250.h"

#define UART_AIROHA_XINCLKDR	10
#define UART_AIROHA_XYD		11
#define   UART_AIROHA_XYD_X	GENMASK(31, 16)
#define   UART_AIROHA_XYD_Y	GENMASK(15, 0)

#define UART_BRD_20M		0x0001

#define XINDIV_CLOCK		(20 * HZ_PER_MHZ)
#define XYD_Y			65000

static const unsigned int airoha_clk_divs[] = { 2, 4, 10 };

static unsigned int airoha_get_divisor(struct uart_port *port,
				       unsigned int baud,
				       unsigned int *frac)
{
	/* Hardware always uses BRDIV = 1. */
	*frac = 0;

	return UART_BRD_20M;
}

/*
 * Airoha UART baud rate calculation logic
 *
 * crystal_clock = 20 MHz (fixed frequency)
 * xindiv_clock = crystal_clock / clock_div
 * (x/y) = XYD, 32 bit register with 16 bits of x and then 16 bits of y
 * clock_div = XINCLK_DIVCNT (default set to 10 (0x4)),
 *           - 3 bit register [ 1, 2, 4, 8, 10, 12, 16, 20 ]
 *
 * baud_rate = ((xindiv_clock) * (x/y)) / ([BRDH,BRDL] * 16)
 *
 * Selecting divider needs to fulfill
 * 1.8432 MHz <= xindiv_clk <= APB clock / 2
 * The clocks are unknown but a divider of value 1 did not result in a valid
 * waveform.
 *
 * XYD_y seems to need to be larger then XYD_x for proper waveform generation.
 * Setting [BRDH,BRDL] to [0,1] and XYD_y to 65000 gives even values
 * for usual baud rates.
 */
static void airoha_set_divisor(struct uart_port *port, unsigned int baud,
			       unsigned int quot, unsigned int quot_frac)
{
	struct uart_8250_port *up = up_to_u8250p(port);
	u32 xindiv_clk;
	u64 xyd_x, nom;
	int div_bit;

	/* Set baud rate calculation defaults (BRDIV [BRDH,BRDL] to 1) */
	serial8250_do_set_divisor(port, baud, UART_BRD_20M);

	/*
	 * Calculate XYD_x and XINCLKDR register by searching
	 * through a table of crystal_clock divisors.
	 */
	nom = (u64)baud * XYD_Y;
	for (div_bit = ARRAY_SIZE(airoha_clk_divs) - 1; div_bit >= 0; div_bit--) {
		unsigned int div = airoha_clk_divs[div_bit];

		xindiv_clk = XINDIV_CLOCK / div;
		xyd_x = div_u64(nom * 16, xindiv_clk);

		/* For the HSUART xyd_x needs to be scaled by a factor of 2 */
		if (port->type == UART_PORT_AIROHA_HS)
			xyd_x /= 2;

		if (xyd_x < XYD_Y)
			break;
	}

	/* Couldn't find a valid xyd_x */
	if (div_bit < 0) {
		dev_err(port->dev, "failed to find suitable clock divisor for baud %u\n",
			baud);
		return;
	}

	serial_port_out(port, UART_AIROHA_XINCLKDR, BIT(div_bit));
	serial_port_out(port, UART_AIROHA_XYD,
			FIELD_PREP(UART_AIROHA_XYD_X, xyd_x) |
			FIELD_PREP(UART_AIROHA_XYD_Y, XYD_Y));

	/* Restore normal register access. */
	serial_port_out(port, UART_LCR, up->lcr);
}

static int airoha_8250_probe(struct platform_device *pdev)
{
	struct uart_8250_port uart = { };
	struct device *dev = &pdev->dev;
	struct resource *res;
	int ret;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return dev_err_probe(dev, -EINVAL, "invalid address\n");

	uart.port.dev = dev;
	if (device_is_compatible(dev, "airoha,an7581-hsuart"))
		uart.port.type = UART_PORT_AIROHA_HS;
	else
		uart.port.type = UART_PORT_AIROHA;
	uart.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
			  UPF_FIXED_TYPE | UPF_IOREMAP;
	uart.port.set_divisor = airoha_set_divisor;
	uart.port.get_divisor = airoha_get_divisor;
	uart.port.mapbase = res->start;
	uart.port.mapsize = resource_size(res);

	ret = uart_read_and_validate_port_properties(&uart.port);
	if (ret)
		return ret;

	struct uart_8250_port *uport = serial8250_register_8250_port(&uart);
	if (IS_ERR(uport))
		return PTR_ERR(uport);

	platform_set_drvdata(pdev, uport);

	return 0;
}

static void airoha_8250_remove(struct platform_device *ofdev)
{
	struct uart_8250_port *uport = platform_get_drvdata(ofdev);

	serial8250_unregister_port(uport);
}

static const struct of_device_id airoha_8250_dt_ids[] = {
	{ .compatible = "airoha,en7523-uart" },
	{ .compatible = "airoha,an7581-hsuart" },
	{ }
};
MODULE_DEVICE_TABLE(of, airoha_8250_dt_ids);

static struct platform_driver airoha_8250_driver = {
	.driver = {
		.name = "8250_airoha",
		.of_match_table = airoha_8250_dt_ids,
	},
	.probe = airoha_8250_probe,
	.remove = airoha_8250_remove,
};
module_platform_driver(airoha_8250_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Airoha UART driver");
MODULE_AUTHOR("Benjamin Larsson <benjamin.larsson@genexis.eu>");
MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");