1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  NS16550 UART driver (PCI)	File: dev_ns16550_pci.c
5    *
6    *  This is a console device driver for a PCI NS16550 UART
7    *
8    *  Author:  Mitch Lichtenberg
9    *
10    *********************************************************************
11    *
12    *  Copyright 2000,2001,2002,2003
13    *  Broadcom Corporation. All rights reserved.
14    *
15    *  This software is furnished under license and may be used and
16    *  copied only in accordance with the following terms and
17    *  conditions.  Subject to these conditions, you may download,
18    *  copy, install, use, modify and distribute modified or unmodified
19    *  copies of this software in source and/or binary form.  No title
20    *  or ownership is transferred hereby.
21    *
22    *  1) Any source code used, modified or distributed must reproduce
23    *     and retain this copyright notice and list of conditions
24    *     as they appear in the source file.
25    *
26    *  2) No right is granted to use any trade name, trademark, or
27    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
28    *     name may not be used to endorse or promote products derived
29    *     from this software without the prior written permission of
30    *     Broadcom Corporation.
31    *
32    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44    *     THE POSSIBILITY OF SUCH DAMAGE.
45    ********************************************************************* */
46
47
48#include "cfe.h"
49
50#include "pcivar.h"
51#include "pcireg.h"
52
53
54/* Assumed reference clock for PCI UARTs. */
55#undef NS16550_HZ
56#define NS16550_HZ	1843200
57
58/* Probe routine for real UART driver. */
59extern void ns16550_uart_probe(cfe_driver_t *drv,
60			       unsigned long probe_a, unsigned long probe_b,
61			       void *probe_ptr);
62
63/* Probe routine for this UART driver. */
64static void ns16550pci_uart_probe(cfe_driver_t *drv,
65			       unsigned long probe_a, unsigned long probe_b,
66			       void *probe_ptr);
67
68/* We just glom onto the dispatch table in the real driver */
69extern const cfe_devdisp_t ns16550_uart_dispatch;
70
71const cfe_driver_t ns16550pci_uart = {
72    "PCI NS16550 UART",
73    "uart",
74    CFE_DEV_SERIAL,
75    &ns16550_uart_dispatch,
76    ns16550pci_uart_probe
77};
78
79
80static void
81ns16550pci_uart_probe(cfe_driver_t *drv,
82		      unsigned long probe_a, unsigned long probe_b,
83		      void *probe_ptr)
84{
85    int index;
86
87    /*
88     * NS16550-compatible UART on the PCI bus
89     * probe_a, probe_b and probe_ptr are unused.
90     *
91     * This is for generic 16550-like UARTs. We use the PCI class and
92     * interface codes to identify compatible hardware.  We require
93     * 16550 or better compatibility. The 16{6,7,8,9}50 parts are
94     * claimed to be 16550 compatible but are as yet untested.
95     */
96
97    for (index = 0; ; index++) {
98	pcitag_t tag;
99	pcireg_t cr;
100	phys_addr_t pa;
101
102	if (pci_find_class(PCI_CLASS_COMMUNICATIONS, index, &tag) != 0) {
103	    break;
104	    }
105
106	cr = pci_conf_read(tag, PCI_CLASS_REG);
107	if (PCI_SUBCLASS(cr) == PCI_SUBCLASS_COMMUNICATIONS_SERIAL
108	    && PCI_INTERFACE(cr) >= 0x02          /* 16550 .. */
109	    && PCI_INTERFACE(cr) <= 0x06) {       /* 16950 */
110	    if (pci_map_io(tag, PCI_MAPREG(0), PCI_MATCH_BYTES, &pa) == 0)
111		ns16550_uart_probe(drv, pa, NS16550_HZ, NULL);
112	    else
113	    if (pci_map_mem(tag, PCI_MAPREG(0), PCI_MATCH_BYTES, &pa) == 0)
114		ns16550_uart_probe(drv, pa, NS16550_HZ, NULL);
115	    }
116	}
117}
118