1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 8250_lpss.c - Driver for UART on Intel Braswell and various other Intel SoCs
4  *
5  * Copyright (C) 2016 Intel Corporation
6  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/rational.h>
13 
14 #include <linux/dmaengine.h>
15 #include <linux/dma/dw.h>
16 
17 #include "8250_dwlib.h"
18 
19 #define PCI_DEVICE_ID_INTEL_QRK_UARTx	0x0936
20 
21 #define PCI_DEVICE_ID_INTEL_BYT_UART1	0x0f0a
22 #define PCI_DEVICE_ID_INTEL_BYT_UART2	0x0f0c
23 
24 #define PCI_DEVICE_ID_INTEL_BSW_UART1	0x228a
25 #define PCI_DEVICE_ID_INTEL_BSW_UART2	0x228c
26 
27 #define PCI_DEVICE_ID_INTEL_EHL_UART0	0x4b96
28 #define PCI_DEVICE_ID_INTEL_EHL_UART1	0x4b97
29 #define PCI_DEVICE_ID_INTEL_EHL_UART2	0x4b98
30 #define PCI_DEVICE_ID_INTEL_EHL_UART3	0x4b99
31 #define PCI_DEVICE_ID_INTEL_EHL_UART4	0x4b9a
32 #define PCI_DEVICE_ID_INTEL_EHL_UART5	0x4b9b
33 
34 #define PCI_DEVICE_ID_INTEL_BDW_UART1	0x9ce3
35 #define PCI_DEVICE_ID_INTEL_BDW_UART2	0x9ce4
36 
37 /* Intel LPSS specific registers */
38 
39 #define BYT_PRV_CLK			0x800
40 #define BYT_PRV_CLK_EN			BIT(0)
41 #define BYT_PRV_CLK_M_VAL_SHIFT		1
42 #define BYT_PRV_CLK_N_VAL_SHIFT		16
43 #define BYT_PRV_CLK_UPDATE		BIT(31)
44 
45 #define BYT_TX_OVF_INT			0x820
46 #define BYT_TX_OVF_INT_MASK		BIT(1)
47 
48 struct lpss8250;
49 
50 struct lpss8250_board {
51 	unsigned long freq;
52 	unsigned int base_baud;
53 	int (*setup)(struct lpss8250 *, struct uart_port *p);
54 	void (*exit)(struct lpss8250 *);
55 };
56 
57 struct lpss8250 {
58 	struct dw8250_port_data data;
59 	struct lpss8250_board *board;
60 
61 	/* DMA parameters */
62 	struct dw_dma_chip dma_chip;
63 	struct dw_dma_slave dma_param;
64 	u8 dma_maxburst;
65 };
66 
67 static inline struct lpss8250 *to_lpss8250(struct dw8250_port_data *data)
68 {
69 	return container_of(data, struct lpss8250, data);
70 }
71 
72 static void byt_set_termios(struct uart_port *p, struct ktermios *termios,
73 			    struct ktermios *old)
74 {
75 	unsigned int baud = tty_termios_baud_rate(termios);
76 	struct lpss8250 *lpss = to_lpss8250(p->private_data);
77 	unsigned long fref = lpss->board->freq, fuart = baud * 16;
78 	unsigned long w = BIT(15) - 1;
79 	unsigned long m, n;
80 	u32 reg;
81 
82 	/* Gracefully handle the B0 case: fall back to B9600 */
83 	fuart = fuart ? fuart : 9600 * 16;
84 
85 	/* Get Fuart closer to Fref */
86 	fuart *= rounddown_pow_of_two(fref / fuart);
87 
88 	/*
89 	 * For baud rates 0.5M, 1M, 1.5M, 2M, 2.5M, 3M, 3.5M and 4M the
90 	 * dividers must be adjusted.
91 	 *
92 	 * uartclk = (m / n) * 100 MHz, where m <= n
93 	 */
94 	rational_best_approximation(fuart, fref, w, w, &m, &n);
95 	p->uartclk = fuart;
96 
97 	/* Reset the clock */
98 	reg = (m << BYT_PRV_CLK_M_VAL_SHIFT) | (n << BYT_PRV_CLK_N_VAL_SHIFT);
99 	writel(reg, p->membase + BYT_PRV_CLK);
100 	reg |= BYT_PRV_CLK_EN | BYT_PRV_CLK_UPDATE;
101 	writel(reg, p->membase + BYT_PRV_CLK);
102 
103 	dw8250_do_set_termios(p, termios, old);
104 }
105 
106 static unsigned int byt_get_mctrl(struct uart_port *port)
107 {
108 	unsigned int ret = serial8250_do_get_mctrl(port);
109 
110 	/* Force DCD and DSR signals to permanently be reported as active */
111 	ret |= TIOCM_CAR | TIOCM_DSR;
112 
113 	return ret;
114 }
115 
116 static int byt_serial_setup(struct lpss8250 *lpss, struct uart_port *port)
117 {
118 	struct dw_dma_slave *param = &lpss->dma_param;
119 	struct pci_dev *pdev = to_pci_dev(port->dev);
120 	unsigned int dma_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
121 	struct pci_dev *dma_dev = pci_get_slot(pdev->bus, dma_devfn);
122 
123 	switch (pdev->device) {
124 	case PCI_DEVICE_ID_INTEL_BYT_UART1:
125 	case PCI_DEVICE_ID_INTEL_BSW_UART1:
126 	case PCI_DEVICE_ID_INTEL_BDW_UART1:
127 		param->src_id = 3;
128 		param->dst_id = 2;
129 		break;
130 	case PCI_DEVICE_ID_INTEL_BYT_UART2:
131 	case PCI_DEVICE_ID_INTEL_BSW_UART2:
132 	case PCI_DEVICE_ID_INTEL_BDW_UART2:
133 		param->src_id = 5;
134 		param->dst_id = 4;
135 		break;
136 	default:
137 		return -EINVAL;
138 	}
139 
140 	param->dma_dev = &dma_dev->dev;
141 	param->m_master = 0;
142 	param->p_master = 1;
143 
144 	lpss->dma_maxburst = 16;
145 
146 	port->set_termios = byt_set_termios;
147 	port->get_mctrl = byt_get_mctrl;
148 
149 	/* Disable TX counter interrupts */
150 	writel(BYT_TX_OVF_INT_MASK, port->membase + BYT_TX_OVF_INT);
151 
152 	return 0;
153 }
154 
155 static int ehl_serial_setup(struct lpss8250 *lpss, struct uart_port *port)
156 {
157 	struct uart_8250_dma *dma = &lpss->data.dma;
158 	struct uart_8250_port *up = up_to_u8250p(port);
159 
160 	/*
161 	 * This simply makes the checks in the 8250_port to try the DMA
162 	 * channel request which in turn uses the magic of ACPI tables
163 	 * parsing (see drivers/dma/acpi-dma.c for the details) and
164 	 * matching with the registered General Purpose DMA controllers.
165 	 */
166 	up->dma = dma;
167 
168 	port->set_termios = dw8250_do_set_termios;
169 
170 	return 0;
171 }
172 
173 #ifdef CONFIG_SERIAL_8250_DMA
174 static const struct dw_dma_platform_data qrk_serial_dma_pdata = {
175 	.nr_channels = 2,
176 	.chan_allocation_order = CHAN_ALLOCATION_ASCENDING,
177 	.chan_priority = CHAN_PRIORITY_ASCENDING,
178 	.block_size = 4095,
179 	.nr_masters = 1,
180 	.data_width = {4},
181 	.multi_block = {0},
182 };
183 
184 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
185 {
186 	struct uart_8250_dma *dma = &lpss->data.dma;
187 	struct dw_dma_chip *chip = &lpss->dma_chip;
188 	struct dw_dma_slave *param = &lpss->dma_param;
189 	struct pci_dev *pdev = to_pci_dev(port->dev);
190 	int ret;
191 
192 	chip->pdata = &qrk_serial_dma_pdata;
193 	chip->dev = &pdev->dev;
194 	chip->id = pdev->devfn;
195 	chip->irq = pci_irq_vector(pdev, 0);
196 	chip->regs = pci_ioremap_bar(pdev, 1);
197 	if (!chip->regs)
198 		return;
199 
200 	/* Falling back to PIO mode if DMA probing fails */
201 	ret = dw_dma_probe(chip);
202 	if (ret)
203 		return;
204 
205 	pci_try_set_mwi(pdev);
206 
207 	/* Special DMA address for UART */
208 	dma->rx_dma_addr = 0xfffff000;
209 	dma->tx_dma_addr = 0xfffff000;
210 
211 	param->dma_dev = &pdev->dev;
212 	param->src_id = 0;
213 	param->dst_id = 1;
214 	param->hs_polarity = true;
215 
216 	lpss->dma_maxburst = 8;
217 }
218 
219 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
220 {
221 	struct dw_dma_chip *chip = &lpss->dma_chip;
222 	struct dw_dma_slave *param = &lpss->dma_param;
223 
224 	if (!param->dma_dev)
225 		return;
226 
227 	dw_dma_remove(chip);
228 
229 	pci_iounmap(to_pci_dev(chip->dev), chip->regs);
230 }
231 #else	/* CONFIG_SERIAL_8250_DMA */
232 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) {}
233 static void qrk_serial_exit_dma(struct lpss8250 *lpss) {}
234 #endif	/* !CONFIG_SERIAL_8250_DMA */
235 
236 static int qrk_serial_setup(struct lpss8250 *lpss, struct uart_port *port)
237 {
238 	qrk_serial_setup_dma(lpss, port);
239 	return 0;
240 }
241 
242 static void qrk_serial_exit(struct lpss8250 *lpss)
243 {
244 	qrk_serial_exit_dma(lpss);
245 }
246 
247 static bool lpss8250_dma_filter(struct dma_chan *chan, void *param)
248 {
249 	struct dw_dma_slave *dws = param;
250 
251 	if (dws->dma_dev != chan->device->dev)
252 		return false;
253 
254 	chan->private = dws;
255 	return true;
256 }
257 
258 static int lpss8250_dma_setup(struct lpss8250 *lpss, struct uart_8250_port *port)
259 {
260 	struct uart_8250_dma *dma = &lpss->data.dma;
261 	struct dw_dma_slave *rx_param, *tx_param;
262 	struct device *dev = port->port.dev;
263 
264 	if (!lpss->dma_param.dma_dev)
265 		return 0;
266 
267 	rx_param = devm_kzalloc(dev, sizeof(*rx_param), GFP_KERNEL);
268 	if (!rx_param)
269 		return -ENOMEM;
270 
271 	tx_param = devm_kzalloc(dev, sizeof(*tx_param), GFP_KERNEL);
272 	if (!tx_param)
273 		return -ENOMEM;
274 
275 	*rx_param = lpss->dma_param;
276 	dma->rxconf.src_maxburst = lpss->dma_maxburst;
277 
278 	*tx_param = lpss->dma_param;
279 	dma->txconf.dst_maxburst = lpss->dma_maxburst;
280 
281 	dma->fn = lpss8250_dma_filter;
282 	dma->rx_param = rx_param;
283 	dma->tx_param = tx_param;
284 
285 	port->dma = dma;
286 	return 0;
287 }
288 
289 static int lpss8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
290 {
291 	struct uart_8250_port uart;
292 	struct lpss8250 *lpss;
293 	int ret;
294 
295 	ret = pcim_enable_device(pdev);
296 	if (ret)
297 		return ret;
298 
299 	pci_set_master(pdev);
300 
301 	lpss = devm_kzalloc(&pdev->dev, sizeof(*lpss), GFP_KERNEL);
302 	if (!lpss)
303 		return -ENOMEM;
304 
305 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
306 	if (ret < 0)
307 		return ret;
308 
309 	lpss->board = (struct lpss8250_board *)id->driver_data;
310 
311 	memset(&uart, 0, sizeof(struct uart_8250_port));
312 
313 	uart.port.dev = &pdev->dev;
314 	uart.port.irq = pci_irq_vector(pdev, 0);
315 	uart.port.private_data = &lpss->data;
316 	uart.port.type = PORT_16550A;
317 	uart.port.iotype = UPIO_MEM;
318 	uart.port.regshift = 2;
319 	uart.port.uartclk = lpss->board->base_baud * 16;
320 	uart.port.flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE;
321 	uart.capabilities = UART_CAP_FIFO | UART_CAP_AFE;
322 	uart.port.mapbase = pci_resource_start(pdev, 0);
323 	uart.port.membase = pcim_iomap(pdev, 0, 0);
324 	if (!uart.port.membase)
325 		return -ENOMEM;
326 
327 	ret = lpss->board->setup(lpss, &uart.port);
328 	if (ret)
329 		return ret;
330 
331 	dw8250_setup_port(&uart.port);
332 
333 	ret = lpss8250_dma_setup(lpss, &uart);
334 	if (ret)
335 		goto err_exit;
336 
337 	ret = serial8250_register_8250_port(&uart);
338 	if (ret < 0)
339 		goto err_exit;
340 
341 	lpss->data.line = ret;
342 
343 	pci_set_drvdata(pdev, lpss);
344 	return 0;
345 
346 err_exit:
347 	if (lpss->board->exit)
348 		lpss->board->exit(lpss);
349 	pci_free_irq_vectors(pdev);
350 	return ret;
351 }
352 
353 static void lpss8250_remove(struct pci_dev *pdev)
354 {
355 	struct lpss8250 *lpss = pci_get_drvdata(pdev);
356 
357 	serial8250_unregister_port(lpss->data.line);
358 
359 	if (lpss->board->exit)
360 		lpss->board->exit(lpss);
361 	pci_free_irq_vectors(pdev);
362 }
363 
364 static const struct lpss8250_board byt_board = {
365 	.freq = 100000000,
366 	.base_baud = 2764800,
367 	.setup = byt_serial_setup,
368 };
369 
370 static const struct lpss8250_board ehl_board = {
371 	.freq = 200000000,
372 	.base_baud = 12500000,
373 	.setup = ehl_serial_setup,
374 };
375 
376 static const struct lpss8250_board qrk_board = {
377 	.freq = 44236800,
378 	.base_baud = 2764800,
379 	.setup = qrk_serial_setup,
380 	.exit = qrk_serial_exit,
381 };
382 
383 static const struct pci_device_id pci_ids[] = {
384 	{ PCI_DEVICE_DATA(INTEL, QRK_UARTx, &qrk_board) },
385 	{ PCI_DEVICE_DATA(INTEL, EHL_UART0, &ehl_board) },
386 	{ PCI_DEVICE_DATA(INTEL, EHL_UART1, &ehl_board) },
387 	{ PCI_DEVICE_DATA(INTEL, EHL_UART2, &ehl_board) },
388 	{ PCI_DEVICE_DATA(INTEL, EHL_UART3, &ehl_board) },
389 	{ PCI_DEVICE_DATA(INTEL, EHL_UART4, &ehl_board) },
390 	{ PCI_DEVICE_DATA(INTEL, EHL_UART5, &ehl_board) },
391 	{ PCI_DEVICE_DATA(INTEL, BYT_UART1, &byt_board) },
392 	{ PCI_DEVICE_DATA(INTEL, BYT_UART2, &byt_board) },
393 	{ PCI_DEVICE_DATA(INTEL, BSW_UART1, &byt_board) },
394 	{ PCI_DEVICE_DATA(INTEL, BSW_UART2, &byt_board) },
395 	{ PCI_DEVICE_DATA(INTEL, BDW_UART1, &byt_board) },
396 	{ PCI_DEVICE_DATA(INTEL, BDW_UART2, &byt_board) },
397 	{ }
398 };
399 MODULE_DEVICE_TABLE(pci, pci_ids);
400 
401 static struct pci_driver lpss8250_pci_driver = {
402 	.name           = "8250_lpss",
403 	.id_table       = pci_ids,
404 	.probe          = lpss8250_probe,
405 	.remove         = lpss8250_remove,
406 };
407 
408 module_pci_driver(lpss8250_pci_driver);
409 
410 MODULE_AUTHOR("Intel Corporation");
411 MODULE_LICENSE("GPL v2");
412 MODULE_DESCRIPTION("Intel LPSS UART driver");
413