xref: /linux/drivers/tty/serial/8250/8250_pcilib.c (revision 2697b79a469b68e3ad3640f55284359c1396278d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 8250 PCI library.
4  *
5  * Copyright (C) 2001 Russell King, All Rights Reserved.
6  */
7 #include <linux/errno.h>
8 #include <linux/ioport.h>
9 #include <linux/pci.h>
10 #include <linux/types.h>
11 
12 #include "8250.h"
13 #include "8250_pcilib.h"
14 
15 int serial8250_pci_setup_port(struct pci_dev *dev, struct uart_8250_port *port,
16 		   u8 bar, unsigned int offset, int regshift)
17 {
18 	if (bar >= PCI_STD_NUM_BARS)
19 		return -EINVAL;
20 
21 	if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) {
22 		if (!pcim_iomap(dev, bar, 0) && !pcim_iomap_table(dev))
23 			return -ENOMEM;
24 
25 		port->port.iotype = UPIO_MEM;
26 		port->port.iobase = 0;
27 		port->port.mapbase = pci_resource_start(dev, bar) + offset;
28 		port->port.membase = pcim_iomap_table(dev)[bar] + offset;
29 		port->port.regshift = regshift;
30 	} else {
31 		port->port.iotype = UPIO_PORT;
32 		port->port.iobase = pci_resource_start(dev, bar) + offset;
33 		port->port.mapbase = 0;
34 		port->port.membase = NULL;
35 		port->port.regshift = 0;
36 	}
37 	return 0;
38 }
39 EXPORT_SYMBOL_NS_GPL(serial8250_pci_setup_port, SERIAL_8250_PCI);
40 MODULE_LICENSE("GPL");
41