Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/device.h>
0003 #include <linux/kernel.h>
0004 #include <linux/module.h>
0005 #include <linux/io.h>
0006 #include <linux/mcb.h>
0007 #include <linux/serial.h>
0008 #include <linux/serial_core.h>
0009 #include <linux/serial_8250.h>
0010 #include <uapi/linux/serial_core.h>
0011 
0012 #define MEN_UART_ID_Z025 0x19
0013 #define MEN_UART_ID_Z057 0x39
0014 #define MEN_UART_ID_Z125 0x7d
0015 
0016 #define MEN_UART_MEM_SIZE 0x10
0017 
0018 struct serial_8250_men_mcb_data {
0019     struct uart_8250_port uart;
0020     int line;
0021 };
0022 
0023 /*
0024  * The Z125 16550-compatible UART has no fixed base clock assigned
0025  * So, depending on the board we're on, we need to adjust the
0026  * parameter in order to really set the correct baudrate, and
0027  * do so if possible without user interaction
0028  */
0029 static u32 men_lookup_uartclk(struct mcb_device *mdev)
0030 {
0031     /* use default value if board is not available below */
0032     u32 clkval = 1041666;
0033 
0034     dev_info(&mdev->dev, "%s on board %s\n",
0035         dev_name(&mdev->dev),
0036         mdev->bus->name);
0037     if  (strncmp(mdev->bus->name, "F075", 4) == 0)
0038         clkval = 1041666;
0039     else if (strncmp(mdev->bus->name, "F216", 4) == 0)
0040         clkval = 1843200;
0041     else if (strncmp(mdev->bus->name, "G215", 4) == 0)
0042         clkval = 1843200;
0043     else if (strncmp(mdev->bus->name, "F210", 4) == 0)
0044         clkval = 115200;
0045     else
0046         dev_info(&mdev->dev,
0047              "board not detected, using default uartclk\n");
0048 
0049     clkval = clkval  << 4;
0050 
0051     return clkval;
0052 }
0053 
0054 static int get_num_ports(struct mcb_device *mdev,
0055                   void __iomem *membase)
0056 {
0057     switch (mdev->id) {
0058     case MEN_UART_ID_Z125:
0059         return 1U;
0060     case MEN_UART_ID_Z025:
0061         return readb(membase) >> 4;
0062     case MEN_UART_ID_Z057:
0063         return 4U;
0064     default:
0065         dev_err(&mdev->dev, "no supported device!\n");
0066         return -ENODEV;
0067     }
0068 }
0069 
0070 static int serial_8250_men_mcb_probe(struct mcb_device *mdev,
0071                      const struct mcb_device_id *id)
0072 {
0073     struct serial_8250_men_mcb_data *data;
0074     struct resource *mem;
0075     int num_ports;
0076     int i;
0077     void __iomem *membase;
0078 
0079     mem = mcb_get_resource(mdev, IORESOURCE_MEM);
0080     if (mem == NULL)
0081         return -ENXIO;
0082     membase = devm_ioremap_resource(&mdev->dev, mem);
0083     if (IS_ERR(membase))
0084         return PTR_ERR_OR_ZERO(membase);
0085 
0086     num_ports = get_num_ports(mdev, membase);
0087 
0088     dev_dbg(&mdev->dev, "found a 16z%03u with %u ports\n",
0089         mdev->id, num_ports);
0090 
0091     if (num_ports <= 0 || num_ports > 4) {
0092         dev_err(&mdev->dev, "unexpected number of ports: %u\n",
0093             num_ports);
0094         return -ENODEV;
0095     }
0096 
0097     data = devm_kcalloc(&mdev->dev, num_ports,
0098                 sizeof(struct serial_8250_men_mcb_data),
0099                 GFP_KERNEL);
0100     if (!data)
0101         return -ENOMEM;
0102 
0103     mcb_set_drvdata(mdev, data);
0104 
0105     for (i = 0; i < num_ports; i++) {
0106         data[i].uart.port.dev = mdev->dma_dev;
0107         spin_lock_init(&data[i].uart.port.lock);
0108 
0109         data[i].uart.port.type = PORT_16550;
0110         data[i].uart.port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ
0111                       | UPF_FIXED_TYPE;
0112         data[i].uart.port.iotype = UPIO_MEM;
0113         data[i].uart.port.uartclk = men_lookup_uartclk(mdev);
0114         data[i].uart.port.regshift = 0;
0115         data[i].uart.port.irq = mcb_get_irq(mdev);
0116         data[i].uart.port.membase = membase;
0117         data[i].uart.port.fifosize = 60;
0118         data[i].uart.port.mapbase = (unsigned long) mem->start
0119                         + i * MEN_UART_MEM_SIZE;
0120         data[i].uart.port.iobase = data[i].uart.port.mapbase;
0121 
0122         /* ok, register the port */
0123         data[i].line = serial8250_register_8250_port(&data[i].uart);
0124         if (data[i].line < 0) {
0125             dev_err(&mdev->dev, "unable to register UART port\n");
0126             return data[i].line;
0127         }
0128         dev_info(&mdev->dev, "found MCB UART: ttyS%d\n", data[i].line);
0129     }
0130 
0131     return 0;
0132 }
0133 
0134 static void serial_8250_men_mcb_remove(struct mcb_device *mdev)
0135 {
0136     int num_ports, i;
0137     struct serial_8250_men_mcb_data *data = mcb_get_drvdata(mdev);
0138 
0139     if (!data)
0140         return;
0141 
0142     num_ports = get_num_ports(mdev, data[0].uart.port.membase);
0143     if (num_ports <= 0 || num_ports > 4) {
0144         dev_err(&mdev->dev, "error retrieving number of ports!\n");
0145         return;
0146     }
0147 
0148     for (i = 0; i < num_ports; i++)
0149         serial8250_unregister_port(data[i].line);
0150 }
0151 
0152 static const struct mcb_device_id serial_8250_men_mcb_ids[] = {
0153     { .device = MEN_UART_ID_Z025 },
0154     { .device = MEN_UART_ID_Z057 },
0155     { .device = MEN_UART_ID_Z125 },
0156     { }
0157 };
0158 MODULE_DEVICE_TABLE(mcb, serial_8250_men_mcb_ids);
0159 
0160 static struct mcb_driver mcb_driver = {
0161     .driver = {
0162         .name = "8250_men_mcb",
0163         .owner = THIS_MODULE,
0164     },
0165     .probe = serial_8250_men_mcb_probe,
0166     .remove = serial_8250_men_mcb_remove,
0167     .id_table = serial_8250_men_mcb_ids,
0168 };
0169 module_mcb_driver(mcb_driver);
0170 
0171 MODULE_LICENSE("GPL v2");
0172 MODULE_DESCRIPTION("MEN 8250 UART driver");
0173 MODULE_AUTHOR("Michael Moese <michael.moese@men.de");
0174 MODULE_ALIAS("mcb:16z125");
0175 MODULE_ALIAS("mcb:16z025");
0176 MODULE_ALIAS("mcb:16z057");
0177 MODULE_IMPORT_NS(MCB);