1 // SPDX-License-Identifier: GPL-2.0
3 * 8250_mid.c - Driver for UART on Intel Penwell and various other Intel SOCs
5 * Copyright (C) 2015 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@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/dma/hsu.h>
15 #include <linux/8250_pci.h>
19 #define PCI_DEVICE_ID_INTEL_PNW_UART1 0x081b
20 #define PCI_DEVICE_ID_INTEL_PNW_UART2 0x081c
21 #define PCI_DEVICE_ID_INTEL_PNW_UART3 0x081d
22 #define PCI_DEVICE_ID_INTEL_TNG_UART 0x1191
23 #define PCI_DEVICE_ID_INTEL_CDF_UART 0x18d8
24 #define PCI_DEVICE_ID_INTEL_DNV_UART 0x19d8
26 /* Intel MID Specific registers */
27 #define INTEL_MID_UART_FISR 0x08
28 #define INTEL_MID_UART_PS 0x30
29 #define INTEL_MID_UART_MUL 0x34
30 #define INTEL_MID_UART_DIV 0x38
34 struct mid8250_board
{
37 unsigned int base_baud
;
38 int (*setup
)(struct mid8250
*, struct uart_port
*p
);
39 void (*exit
)(struct mid8250
*);
45 struct pci_dev
*dma_dev
;
46 struct uart_8250_dma dma
;
47 struct mid8250_board
*board
;
48 struct hsu_dma_chip dma_chip
;
51 /*****************************************************************************/
53 static int pnw_setup(struct mid8250
*mid
, struct uart_port
*p
)
55 struct pci_dev
*pdev
= to_pci_dev(p
->dev
);
57 switch (pdev
->device
) {
58 case PCI_DEVICE_ID_INTEL_PNW_UART1
:
61 case PCI_DEVICE_ID_INTEL_PNW_UART2
:
64 case PCI_DEVICE_ID_INTEL_PNW_UART3
:
71 mid
->dma_dev
= pci_get_slot(pdev
->bus
,
72 PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 3));
76 static int tng_handle_irq(struct uart_port
*p
)
78 struct mid8250
*mid
= p
->private_data
;
79 struct uart_8250_port
*up
= up_to_u8250p(p
);
80 struct hsu_dma_chip
*chip
;
85 chip
= pci_get_drvdata(mid
->dma_dev
);
88 err
= hsu_dma_get_status(chip
, mid
->dma_index
* 2 + 1, &status
);
90 serial8250_rx_dma_flush(up
);
93 ret
|= hsu_dma_do_irq(chip
, mid
->dma_index
* 2 + 1, status
);
96 err
= hsu_dma_get_status(chip
, mid
->dma_index
* 2, &status
);
100 ret
|= hsu_dma_do_irq(chip
, mid
->dma_index
* 2, status
);
103 ret
|= serial8250_handle_irq(p
, serial_port_in(p
, UART_IIR
));
104 return IRQ_RETVAL(ret
);
107 static int tng_setup(struct mid8250
*mid
, struct uart_port
*p
)
109 struct pci_dev
*pdev
= to_pci_dev(p
->dev
);
110 int index
= PCI_FUNC(pdev
->devfn
);
113 * Device 0000:00:04.0 is not a real HSU port. It provides a global
114 * register set for all HSU ports, although it has the same PCI ID.
120 mid
->dma_index
= index
;
121 mid
->dma_dev
= pci_get_slot(pdev
->bus
, PCI_DEVFN(5, 0));
123 p
->handle_irq
= tng_handle_irq
;
127 static int dnv_handle_irq(struct uart_port
*p
)
129 struct mid8250
*mid
= p
->private_data
;
130 struct uart_8250_port
*up
= up_to_u8250p(p
);
131 unsigned int fisr
= serial_port_in(p
, INTEL_MID_UART_FISR
);
137 err
= hsu_dma_get_status(&mid
->dma_chip
, 1, &status
);
139 serial8250_rx_dma_flush(up
);
142 ret
|= hsu_dma_do_irq(&mid
->dma_chip
, 1, status
);
145 err
= hsu_dma_get_status(&mid
->dma_chip
, 0, &status
);
149 ret
|= hsu_dma_do_irq(&mid
->dma_chip
, 0, status
);
152 ret
|= serial8250_handle_irq(p
, serial_port_in(p
, UART_IIR
));
153 return IRQ_RETVAL(ret
);
156 #define DNV_DMA_CHAN_OFFSET 0x80
158 static int dnv_setup(struct mid8250
*mid
, struct uart_port
*p
)
160 struct hsu_dma_chip
*chip
= &mid
->dma_chip
;
161 struct pci_dev
*pdev
= to_pci_dev(p
->dev
);
162 unsigned int bar
= FL_GET_BASE(mid
->board
->flags
);
165 pci_set_master(pdev
);
167 ret
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_ALL_TYPES
);
171 p
->irq
= pci_irq_vector(pdev
, 0);
173 chip
->dev
= &pdev
->dev
;
174 chip
->irq
= pci_irq_vector(pdev
, 0);
175 chip
->regs
= p
->membase
;
176 chip
->length
= pci_resource_len(pdev
, bar
);
177 chip
->offset
= DNV_DMA_CHAN_OFFSET
;
179 /* Falling back to PIO mode if DMA probing fails */
180 ret
= hsu_dma_probe(chip
);
186 p
->handle_irq
= dnv_handle_irq
;
190 static void dnv_exit(struct mid8250
*mid
)
194 hsu_dma_remove(&mid
->dma_chip
);
197 /*****************************************************************************/
199 static void mid8250_set_termios(struct uart_port
*p
,
200 struct ktermios
*termios
,
201 struct ktermios
*old
)
203 unsigned int baud
= tty_termios_baud_rate(termios
);
204 struct mid8250
*mid
= p
->private_data
;
205 unsigned short ps
= 16;
206 unsigned long fuart
= baud
* ps
;
207 unsigned long w
= BIT(24) - 1;
208 unsigned long mul
, div
;
210 /* Gracefully handle the B0 case: fall back to B9600 */
211 fuart
= fuart
? fuart
: 9600 * 16;
213 if (mid
->board
->freq
< fuart
) {
214 /* Find prescaler value that satisfies Fuart < Fref */
215 if (mid
->board
->freq
> baud
)
216 ps
= mid
->board
->freq
/ baud
; /* baud rate too high */
218 ps
= 1; /* PLL case */
221 /* Get Fuart closer to Fref */
222 fuart
*= rounddown_pow_of_two(mid
->board
->freq
/ fuart
);
225 rational_best_approximation(fuart
, mid
->board
->freq
, w
, w
, &mul
, &div
);
226 p
->uartclk
= fuart
* 16 / ps
; /* core uses ps = 16 always */
228 writel(ps
, p
->membase
+ INTEL_MID_UART_PS
); /* set PS */
229 writel(mul
, p
->membase
+ INTEL_MID_UART_MUL
); /* set MUL */
230 writel(div
, p
->membase
+ INTEL_MID_UART_DIV
);
232 serial8250_do_set_termios(p
, termios
, old
);
235 static bool mid8250_dma_filter(struct dma_chan
*chan
, void *param
)
237 struct hsu_dma_slave
*s
= param
;
239 if (s
->dma_dev
!= chan
->device
->dev
|| s
->chan_id
!= chan
->chan_id
)
246 static int mid8250_dma_setup(struct mid8250
*mid
, struct uart_8250_port
*port
)
248 struct uart_8250_dma
*dma
= &mid
->dma
;
249 struct device
*dev
= port
->port
.dev
;
250 struct hsu_dma_slave
*rx_param
;
251 struct hsu_dma_slave
*tx_param
;
256 rx_param
= devm_kzalloc(dev
, sizeof(*rx_param
), GFP_KERNEL
);
260 tx_param
= devm_kzalloc(dev
, sizeof(*tx_param
), GFP_KERNEL
);
264 rx_param
->chan_id
= mid
->dma_index
* 2 + 1;
265 tx_param
->chan_id
= mid
->dma_index
* 2;
267 dma
->rxconf
.src_maxburst
= 64;
268 dma
->txconf
.dst_maxburst
= 64;
270 rx_param
->dma_dev
= &mid
->dma_dev
->dev
;
271 tx_param
->dma_dev
= &mid
->dma_dev
->dev
;
273 dma
->fn
= mid8250_dma_filter
;
274 dma
->rx_param
= rx_param
;
275 dma
->tx_param
= tx_param
;
281 static int mid8250_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
283 struct uart_8250_port uart
;
288 ret
= pcim_enable_device(pdev
);
292 mid
= devm_kzalloc(&pdev
->dev
, sizeof(*mid
), GFP_KERNEL
);
296 mid
->board
= (struct mid8250_board
*)id
->driver_data
;
297 bar
= FL_GET_BASE(mid
->board
->flags
);
299 memset(&uart
, 0, sizeof(struct uart_8250_port
));
301 uart
.port
.dev
= &pdev
->dev
;
302 uart
.port
.irq
= pdev
->irq
;
303 uart
.port
.private_data
= mid
;
304 uart
.port
.type
= PORT_16750
;
305 uart
.port
.iotype
= UPIO_MEM
;
306 uart
.port
.uartclk
= mid
->board
->base_baud
* 16;
307 uart
.port
.flags
= UPF_SHARE_IRQ
| UPF_FIXED_PORT
| UPF_FIXED_TYPE
;
308 uart
.port
.set_termios
= mid8250_set_termios
;
310 uart
.port
.mapbase
= pci_resource_start(pdev
, bar
);
311 uart
.port
.membase
= pcim_iomap(pdev
, bar
, 0);
312 if (!uart
.port
.membase
)
315 if (mid
->board
->setup
) {
316 ret
= mid
->board
->setup(mid
, &uart
.port
);
321 ret
= mid8250_dma_setup(mid
, &uart
);
325 ret
= serial8250_register_8250_port(&uart
);
331 pci_set_drvdata(pdev
, mid
);
334 if (mid
->board
->exit
)
335 mid
->board
->exit(mid
);
339 static void mid8250_remove(struct pci_dev
*pdev
)
341 struct mid8250
*mid
= pci_get_drvdata(pdev
);
343 serial8250_unregister_port(mid
->line
);
345 if (mid
->board
->exit
)
346 mid
->board
->exit(mid
);
349 static const struct mid8250_board pnw_board
= {
356 static const struct mid8250_board tng_board
= {
359 .base_baud
= 1843200,
363 static const struct mid8250_board dnv_board
= {
371 #define MID_DEVICE(id, board) { PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&board }
373 static const struct pci_device_id pci_ids
[] = {
374 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART1
, pnw_board
),
375 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART2
, pnw_board
),
376 MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART3
, pnw_board
),
377 MID_DEVICE(PCI_DEVICE_ID_INTEL_TNG_UART
, tng_board
),
378 MID_DEVICE(PCI_DEVICE_ID_INTEL_CDF_UART
, dnv_board
),
379 MID_DEVICE(PCI_DEVICE_ID_INTEL_DNV_UART
, dnv_board
),
382 MODULE_DEVICE_TABLE(pci
, pci_ids
);
384 static struct pci_driver mid8250_pci_driver
= {
387 .probe
= mid8250_probe
,
388 .remove
= mid8250_remove
,
391 module_pci_driver(mid8250_pci_driver
);
393 MODULE_AUTHOR("Intel Corporation");
394 MODULE_LICENSE("GPL v2");
395 MODULE_DESCRIPTION("Intel MID UART driver");