1 // SPDX-License-Identifier: GPL-2.0
3 * 8250_lpss.c - Driver for UART on Intel Braswell and various other Intel SoCs
5 * Copyright (C) 2016 Intel Corporation
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 #include <linux/bitops.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/rational.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma/dw.h>
17 #include "8250_dwlib.h"
19 #define PCI_DEVICE_ID_INTEL_QRK_UARTx 0x0936
21 #define PCI_DEVICE_ID_INTEL_BYT_UART1 0x0f0a
22 #define PCI_DEVICE_ID_INTEL_BYT_UART2 0x0f0c
24 #define PCI_DEVICE_ID_INTEL_BSW_UART1 0x228a
25 #define PCI_DEVICE_ID_INTEL_BSW_UART2 0x228c
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
34 #define PCI_DEVICE_ID_INTEL_BDW_UART1 0x9ce3
35 #define PCI_DEVICE_ID_INTEL_BDW_UART2 0x9ce4
37 /* Intel LPSS specific registers */
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)
45 #define BYT_TX_OVF_INT 0x820
46 #define BYT_TX_OVF_INT_MASK BIT(1)
50 struct lpss8250_board
{
52 unsigned int base_baud
;
53 int (*setup
)(struct lpss8250
*, struct uart_port
*p
);
54 void (*exit
)(struct lpss8250
*);
58 struct dw8250_port_data data
;
59 struct lpss8250_board
*board
;
62 struct dw_dma_chip dma_chip
;
63 struct dw_dma_slave dma_param
;
67 static inline struct lpss8250
*to_lpss8250(struct dw8250_port_data
*data
)
69 return container_of(data
, struct lpss8250
, data
);
72 static void byt_set_termios(struct uart_port
*p
, struct ktermios
*termios
,
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;
82 /* Gracefully handle the B0 case: fall back to B9600 */
83 fuart
= fuart
? fuart
: 9600 * 16;
85 /* Get Fuart closer to Fref */
86 fuart
*= rounddown_pow_of_two(fref
/ fuart
);
89 * For baud rates 0.5M, 1M, 1.5M, 2M, 2.5M, 3M, 3.5M and 4M the
90 * dividers must be adjusted.
92 * uartclk = (m / n) * 100 MHz, where m <= n
94 rational_best_approximation(fuart
, fref
, w
, w
, &m
, &n
);
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
);
103 p
->status
&= ~UPSTAT_AUTOCTS
;
104 if (termios
->c_cflag
& CRTSCTS
)
105 p
->status
|= UPSTAT_AUTOCTS
;
107 serial8250_do_set_termios(p
, termios
, old
);
110 static unsigned int byt_get_mctrl(struct uart_port
*port
)
112 unsigned int ret
= serial8250_do_get_mctrl(port
);
114 /* Force DCD and DSR signals to permanently be reported as active */
115 ret
|= TIOCM_CAR
| TIOCM_DSR
;
120 static int byt_serial_setup(struct lpss8250
*lpss
, struct uart_port
*port
)
122 struct dw_dma_slave
*param
= &lpss
->dma_param
;
123 struct pci_dev
*pdev
= to_pci_dev(port
->dev
);
124 unsigned int dma_devfn
= PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 0);
125 struct pci_dev
*dma_dev
= pci_get_slot(pdev
->bus
, dma_devfn
);
127 switch (pdev
->device
) {
128 case PCI_DEVICE_ID_INTEL_BYT_UART1
:
129 case PCI_DEVICE_ID_INTEL_BSW_UART1
:
130 case PCI_DEVICE_ID_INTEL_BDW_UART1
:
134 case PCI_DEVICE_ID_INTEL_BYT_UART2
:
135 case PCI_DEVICE_ID_INTEL_BSW_UART2
:
136 case PCI_DEVICE_ID_INTEL_BDW_UART2
:
144 param
->dma_dev
= &dma_dev
->dev
;
148 lpss
->dma_maxburst
= 16;
150 port
->set_termios
= byt_set_termios
;
151 port
->get_mctrl
= byt_get_mctrl
;
153 /* Disable TX counter interrupts */
154 writel(BYT_TX_OVF_INT_MASK
, port
->membase
+ BYT_TX_OVF_INT
);
159 static int ehl_serial_setup(struct lpss8250
*lpss
, struct uart_port
*port
)
164 #ifdef CONFIG_SERIAL_8250_DMA
165 static const struct dw_dma_platform_data qrk_serial_dma_pdata
= {
167 .chan_allocation_order
= CHAN_ALLOCATION_ASCENDING
,
168 .chan_priority
= CHAN_PRIORITY_ASCENDING
,
175 static void qrk_serial_setup_dma(struct lpss8250
*lpss
, struct uart_port
*port
)
177 struct uart_8250_dma
*dma
= &lpss
->data
.dma
;
178 struct dw_dma_chip
*chip
= &lpss
->dma_chip
;
179 struct dw_dma_slave
*param
= &lpss
->dma_param
;
180 struct pci_dev
*pdev
= to_pci_dev(port
->dev
);
183 chip
->pdata
= &qrk_serial_dma_pdata
;
184 chip
->dev
= &pdev
->dev
;
185 chip
->id
= pdev
->devfn
;
186 chip
->irq
= pci_irq_vector(pdev
, 0);
187 chip
->regs
= pci_ioremap_bar(pdev
, 1);
191 /* Falling back to PIO mode if DMA probing fails */
192 ret
= dw_dma_probe(chip
);
196 pci_try_set_mwi(pdev
);
198 /* Special DMA address for UART */
199 dma
->rx_dma_addr
= 0xfffff000;
200 dma
->tx_dma_addr
= 0xfffff000;
202 param
->dma_dev
= &pdev
->dev
;
205 param
->hs_polarity
= true;
207 lpss
->dma_maxburst
= 8;
210 static void qrk_serial_exit_dma(struct lpss8250
*lpss
)
212 struct dw_dma_chip
*chip
= &lpss
->dma_chip
;
213 struct dw_dma_slave
*param
= &lpss
->dma_param
;
220 pci_iounmap(to_pci_dev(chip
->dev
), chip
->regs
);
222 #else /* CONFIG_SERIAL_8250_DMA */
223 static void qrk_serial_setup_dma(struct lpss8250
*lpss
, struct uart_port
*port
) {}
224 static void qrk_serial_exit_dma(struct lpss8250
*lpss
) {}
225 #endif /* !CONFIG_SERIAL_8250_DMA */
227 static int qrk_serial_setup(struct lpss8250
*lpss
, struct uart_port
*port
)
229 qrk_serial_setup_dma(lpss
, port
);
233 static void qrk_serial_exit(struct lpss8250
*lpss
)
235 qrk_serial_exit_dma(lpss
);
238 static bool lpss8250_dma_filter(struct dma_chan
*chan
, void *param
)
240 struct dw_dma_slave
*dws
= param
;
242 if (dws
->dma_dev
!= chan
->device
->dev
)
249 static int lpss8250_dma_setup(struct lpss8250
*lpss
, struct uart_8250_port
*port
)
251 struct uart_8250_dma
*dma
= &lpss
->data
.dma
;
252 struct dw_dma_slave
*rx_param
, *tx_param
;
253 struct device
*dev
= port
->port
.dev
;
255 if (!lpss
->dma_param
.dma_dev
)
258 rx_param
= devm_kzalloc(dev
, sizeof(*rx_param
), GFP_KERNEL
);
262 tx_param
= devm_kzalloc(dev
, sizeof(*tx_param
), GFP_KERNEL
);
266 *rx_param
= lpss
->dma_param
;
267 dma
->rxconf
.src_maxburst
= lpss
->dma_maxburst
;
269 *tx_param
= lpss
->dma_param
;
270 dma
->txconf
.dst_maxburst
= lpss
->dma_maxburst
;
272 dma
->fn
= lpss8250_dma_filter
;
273 dma
->rx_param
= rx_param
;
274 dma
->tx_param
= tx_param
;
280 static int lpss8250_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
282 struct uart_8250_port uart
;
283 struct lpss8250
*lpss
;
286 ret
= pcim_enable_device(pdev
);
290 pci_set_master(pdev
);
292 lpss
= devm_kzalloc(&pdev
->dev
, sizeof(*lpss
), GFP_KERNEL
);
296 ret
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_ALL_TYPES
);
300 lpss
->board
= (struct lpss8250_board
*)id
->driver_data
;
302 memset(&uart
, 0, sizeof(struct uart_8250_port
));
304 uart
.port
.dev
= &pdev
->dev
;
305 uart
.port
.irq
= pci_irq_vector(pdev
, 0);
306 uart
.port
.private_data
= &lpss
->data
;
307 uart
.port
.type
= PORT_16550A
;
308 uart
.port
.iotype
= UPIO_MEM
;
309 uart
.port
.regshift
= 2;
310 uart
.port
.uartclk
= lpss
->board
->base_baud
* 16;
311 uart
.port
.flags
= UPF_SHARE_IRQ
| UPF_FIXED_PORT
| UPF_FIXED_TYPE
;
312 uart
.capabilities
= UART_CAP_FIFO
| UART_CAP_AFE
;
313 uart
.port
.mapbase
= pci_resource_start(pdev
, 0);
314 uart
.port
.membase
= pcim_iomap(pdev
, 0, 0);
315 if (!uart
.port
.membase
)
318 ret
= lpss
->board
->setup(lpss
, &uart
.port
);
322 dw8250_setup_port(&uart
.port
);
324 ret
= lpss8250_dma_setup(lpss
, &uart
);
328 ret
= serial8250_register_8250_port(&uart
);
332 lpss
->data
.line
= ret
;
334 pci_set_drvdata(pdev
, lpss
);
338 if (lpss
->board
->exit
)
339 lpss
->board
->exit(lpss
);
340 pci_free_irq_vectors(pdev
);
344 static void lpss8250_remove(struct pci_dev
*pdev
)
346 struct lpss8250
*lpss
= pci_get_drvdata(pdev
);
348 serial8250_unregister_port(lpss
->data
.line
);
350 if (lpss
->board
->exit
)
351 lpss
->board
->exit(lpss
);
352 pci_free_irq_vectors(pdev
);
355 static const struct lpss8250_board byt_board
= {
357 .base_baud
= 2764800,
358 .setup
= byt_serial_setup
,
361 static const struct lpss8250_board ehl_board
= {
363 .base_baud
= 12500000,
364 .setup
= ehl_serial_setup
,
367 static const struct lpss8250_board qrk_board
= {
369 .base_baud
= 2764800,
370 .setup
= qrk_serial_setup
,
371 .exit
= qrk_serial_exit
,
374 static const struct pci_device_id pci_ids
[] = {
375 { PCI_DEVICE_DATA(INTEL
, QRK_UARTx
, &qrk_board
) },
376 { PCI_DEVICE_DATA(INTEL
, EHL_UART0
, &ehl_board
) },
377 { PCI_DEVICE_DATA(INTEL
, EHL_UART1
, &ehl_board
) },
378 { PCI_DEVICE_DATA(INTEL
, EHL_UART2
, &ehl_board
) },
379 { PCI_DEVICE_DATA(INTEL
, EHL_UART3
, &ehl_board
) },
380 { PCI_DEVICE_DATA(INTEL
, EHL_UART4
, &ehl_board
) },
381 { PCI_DEVICE_DATA(INTEL
, EHL_UART5
, &ehl_board
) },
382 { PCI_DEVICE_DATA(INTEL
, BYT_UART1
, &byt_board
) },
383 { PCI_DEVICE_DATA(INTEL
, BYT_UART2
, &byt_board
) },
384 { PCI_DEVICE_DATA(INTEL
, BSW_UART1
, &byt_board
) },
385 { PCI_DEVICE_DATA(INTEL
, BSW_UART2
, &byt_board
) },
386 { PCI_DEVICE_DATA(INTEL
, BDW_UART1
, &byt_board
) },
387 { PCI_DEVICE_DATA(INTEL
, BDW_UART2
, &byt_board
) },
390 MODULE_DEVICE_TABLE(pci
, pci_ids
);
392 static struct pci_driver lpss8250_pci_driver
= {
395 .probe
= lpss8250_probe
,
396 .remove
= lpss8250_remove
,
399 module_pci_driver(lpss8250_pci_driver
);
401 MODULE_AUTHOR("Intel Corporation");
402 MODULE_LICENSE("GPL v2");
403 MODULE_DESCRIPTION("Intel LPSS UART driver");