2008-06-05 17:45:21 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief NS16550 Driver implementation
|
|
|
|
*
|
|
|
|
* FileName: drivers/serial/serial_ns16550.c
|
|
|
|
*
|
|
|
|
* NS16550 support
|
2009-12-15 08:28:28 +00:00
|
|
|
* Modified from u-boot drivers/serial.c and drivers/ns16550.c
|
2008-06-05 17:45:21 +00:00
|
|
|
* originally from linux source (arch/ppc/boot/ns16550.c)
|
|
|
|
* modified to use CFG_ISA_MEM and new defines
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* (C) Copyright 2008
|
|
|
|
* Texas Instruments, <www.ti.com>
|
|
|
|
* Nishanth Menon <x0nishan@ti.com>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2000
|
|
|
|
* Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <driver.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
2011-09-22 17:02:57 +00:00
|
|
|
#include <io.h>
|
2013-07-22 19:42:56 +00:00
|
|
|
#include <linux/clk.h>
|
2008-06-05 17:45:21 +00:00
|
|
|
|
|
|
|
#include "serial_ns16550.h"
|
|
|
|
#include <ns16550.h>
|
|
|
|
|
2013-07-22 19:33:46 +00:00
|
|
|
struct ns16550_priv {
|
|
|
|
struct console_device cdev;
|
|
|
|
struct NS16550_plat plat;
|
|
|
|
int access_width;
|
2013-07-22 19:42:56 +00:00
|
|
|
struct clk *clk;
|
2013-07-22 19:33:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct ns16550_priv *to_ns16550_priv(struct console_device *cdev)
|
|
|
|
{
|
|
|
|
return container_of(cdev, struct ns16550_priv, cdev);
|
|
|
|
}
|
|
|
|
|
2013-11-18 13:57:32 +00:00
|
|
|
struct ns16550_drvdata {
|
|
|
|
void (*init_port)(struct console_device *cdev);
|
2013-11-26 12:08:28 +00:00
|
|
|
const char *linux_console_name;
|
2013-11-18 13:57:32 +00:00
|
|
|
};
|
|
|
|
|
2011-07-29 03:35:50 +00:00
|
|
|
/**
|
|
|
|
* @brief read register
|
|
|
|
*
|
|
|
|
* @param[in] cdev pointer to console device
|
|
|
|
* @param[in] offset
|
|
|
|
*
|
|
|
|
* @return value
|
|
|
|
*/
|
|
|
|
static uint32_t ns16550_read(struct console_device *cdev, uint32_t off)
|
|
|
|
{
|
2013-07-22 19:33:46 +00:00
|
|
|
struct ns16550_priv *priv = to_ns16550_priv(cdev);
|
2011-07-29 03:35:50 +00:00
|
|
|
struct device_d *dev = cdev->dev;
|
2013-07-22 19:33:46 +00:00
|
|
|
struct NS16550_plat *plat = &priv->plat;
|
|
|
|
int width = priv->access_width;
|
2011-07-29 03:35:50 +00:00
|
|
|
|
|
|
|
off <<= plat->shift;
|
|
|
|
|
|
|
|
if (plat->reg_read)
|
|
|
|
return plat->reg_read((unsigned long)dev->priv, off);
|
|
|
|
|
|
|
|
switch (width) {
|
|
|
|
case IORESOURCE_MEM_8BIT:
|
|
|
|
return readb(dev->priv + off);
|
|
|
|
case IORESOURCE_MEM_16BIT:
|
|
|
|
return readw(dev->priv + off);
|
|
|
|
case IORESOURCE_MEM_32BIT:
|
|
|
|
return readl(dev->priv + off);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief write register
|
|
|
|
*
|
|
|
|
* @param[in] cdev pointer to console device
|
|
|
|
* @param[in] offset
|
|
|
|
* @param[in] val
|
|
|
|
*/
|
|
|
|
static void ns16550_write(struct console_device *cdev, uint32_t val,
|
|
|
|
uint32_t off)
|
|
|
|
{
|
2013-07-22 19:33:46 +00:00
|
|
|
struct ns16550_priv *priv = to_ns16550_priv(cdev);
|
2011-07-29 03:35:50 +00:00
|
|
|
struct device_d *dev = cdev->dev;
|
2013-07-22 19:33:46 +00:00
|
|
|
struct NS16550_plat *plat = &priv->plat;
|
|
|
|
int width = priv->access_width;
|
2011-07-29 03:35:50 +00:00
|
|
|
|
|
|
|
off <<= plat->shift;
|
|
|
|
|
|
|
|
if (plat->reg_write) {
|
|
|
|
plat->reg_write(val, (unsigned long)dev->priv, off);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (width) {
|
|
|
|
case IORESOURCE_MEM_8BIT:
|
|
|
|
writeb(val & 0xff, dev->priv + off);
|
|
|
|
break;
|
|
|
|
case IORESOURCE_MEM_16BIT:
|
|
|
|
writew(val & 0xffff, dev->priv + off);
|
|
|
|
break;
|
|
|
|
case IORESOURCE_MEM_32BIT:
|
|
|
|
writel(val, dev->priv + off);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-05 17:45:21 +00:00
|
|
|
/**
|
2009-12-15 08:28:28 +00:00
|
|
|
* @brief Compute the divisor for a baud rate
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
2009-07-31 11:07:03 +00:00
|
|
|
* @param[in] cdev pointer to console device
|
|
|
|
* @param[in] baudrate baud rate
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
|
|
|
* @return divisor to be set
|
|
|
|
*/
|
2012-01-18 06:31:54 +00:00
|
|
|
static inline unsigned int ns16550_calc_divisor(struct console_device *cdev,
|
2008-06-05 17:45:21 +00:00
|
|
|
unsigned int baudrate)
|
|
|
|
{
|
2013-07-22 19:33:46 +00:00
|
|
|
struct ns16550_priv *priv = to_ns16550_priv(cdev);
|
|
|
|
struct NS16550_plat *plat = &priv->plat;
|
2008-06-05 17:45:21 +00:00
|
|
|
unsigned int clk = plat->clock;
|
|
|
|
|
|
|
|
return (clk / MODE_X_DIV / baudrate);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:23:54 +00:00
|
|
|
/**
|
|
|
|
* @brief Set the baudrate for the uart port
|
|
|
|
*
|
|
|
|
* @param[in] cdev console device
|
|
|
|
* @param[in] baud_rate baud rate to set
|
|
|
|
*
|
|
|
|
* @return 0-implied to support the baudrate
|
|
|
|
*/
|
|
|
|
static int ns16550_setbaudrate(struct console_device *cdev, int baud_rate)
|
|
|
|
{
|
|
|
|
unsigned int baud_divisor = ns16550_calc_divisor(cdev, baud_rate);
|
2013-07-22 19:33:46 +00:00
|
|
|
struct ns16550_priv *priv = to_ns16550_priv(cdev);
|
|
|
|
struct NS16550_plat *plat = &priv->plat;
|
2013-07-22 19:23:54 +00:00
|
|
|
|
|
|
|
ns16550_write(cdev, LCR_BKSE, lcr);
|
|
|
|
ns16550_write(cdev, baud_divisor & 0xff, dll);
|
|
|
|
ns16550_write(cdev, (baud_divisor >> 8) & 0xff, dlm);
|
|
|
|
ns16550_write(cdev, LCRVAL, lcr);
|
|
|
|
ns16550_write(cdev, MCRVAL, mcr);
|
|
|
|
|
|
|
|
if (plat->flags & NS16650_FLAG_DISABLE_FIFO)
|
|
|
|
ns16550_write(cdev, FCRVAL & ~FCR_FIFO_EN, fcr);
|
|
|
|
else
|
|
|
|
ns16550_write(cdev, FCRVAL, fcr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-05 17:45:21 +00:00
|
|
|
/**
|
2009-12-15 08:28:28 +00:00
|
|
|
* @brief Initialize the device
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
2009-07-31 11:07:03 +00:00
|
|
|
* @param[in] cdev pointer to console device
|
2008-06-05 17:45:21 +00:00
|
|
|
*/
|
|
|
|
static void ns16550_serial_init_port(struct console_device *cdev)
|
|
|
|
{
|
|
|
|
/* initializing the device for the first time */
|
2012-01-18 06:31:56 +00:00
|
|
|
ns16550_write(cdev, 0x00, lcr); /* select ier reg */
|
2011-07-29 03:35:50 +00:00
|
|
|
ns16550_write(cdev, 0x00, ier);
|
2013-11-18 13:57:32 +00:00
|
|
|
}
|
2012-01-18 06:31:56 +00:00
|
|
|
|
2013-11-18 13:57:32 +00:00
|
|
|
#define omap_mdr1 8
|
2012-01-18 06:31:54 +00:00
|
|
|
|
2013-11-18 13:57:32 +00:00
|
|
|
static void ns16550_omap_init_port(struct console_device *cdev)
|
|
|
|
{
|
|
|
|
ns16550_serial_init_port(cdev);
|
|
|
|
|
|
|
|
ns16550_write(cdev, 0x07, omap_mdr1); /* Disable */
|
|
|
|
ns16550_write(cdev, 0x00, omap_mdr1);
|
2008-06-05 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********** Exposed Functions **********************************/
|
|
|
|
|
|
|
|
/**
|
2009-12-15 08:28:28 +00:00
|
|
|
* @brief Put a character to the serial port
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
2009-07-31 11:07:03 +00:00
|
|
|
* @param[in] cdev pointer to console device
|
|
|
|
* @param[in] c character to put
|
2008-06-05 17:45:21 +00:00
|
|
|
*/
|
|
|
|
static void ns16550_putc(struct console_device *cdev, char c)
|
|
|
|
{
|
|
|
|
/* Loop Doing Nothing */
|
2011-07-29 03:35:50 +00:00
|
|
|
while ((ns16550_read(cdev, lsr) & LSR_THRE) == 0) ;
|
|
|
|
ns16550_write(cdev, c, thr);
|
2008-06-05 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-15 08:28:28 +00:00
|
|
|
* @brief Retrieve a character from serial port
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
2009-07-31 11:07:03 +00:00
|
|
|
* @param[in] cdev pointer to console device
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
|
|
|
* @return return the character read
|
|
|
|
*/
|
|
|
|
static int ns16550_getc(struct console_device *cdev)
|
|
|
|
{
|
|
|
|
/* Loop Doing Nothing */
|
2011-07-29 03:35:50 +00:00
|
|
|
while ((ns16550_read(cdev, lsr) & LSR_DR) == 0) ;
|
|
|
|
return ns16550_read(cdev, rbr);
|
2008-06-05 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-15 08:28:28 +00:00
|
|
|
* @brief Test if character is available
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
2009-07-31 11:07:03 +00:00
|
|
|
* @param[in] cdev pointer to console device
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
|
|
|
* @return - status based on data availability
|
|
|
|
*/
|
|
|
|
static int ns16550_tstc(struct console_device *cdev)
|
|
|
|
{
|
2011-07-29 03:35:50 +00:00
|
|
|
return ((ns16550_read(cdev, lsr) & LSR_DR) != 0);
|
2008-06-05 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 19:55:36 +00:00
|
|
|
static void ns16550_probe_dt(struct device_d *dev, struct ns16550_priv *priv)
|
|
|
|
{
|
|
|
|
struct device_node *np = dev->device_node;
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_OFDEVICE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
of_property_read_u32(np, "reg-shift", &priv->plat.shift);
|
|
|
|
}
|
|
|
|
|
2013-11-18 13:57:32 +00:00
|
|
|
static struct ns16550_drvdata ns16550_drvdata = {
|
|
|
|
.init_port = ns16550_serial_init_port,
|
|
|
|
};
|
|
|
|
|
|
|
|
static __maybe_unused struct ns16550_drvdata omap_drvdata = {
|
|
|
|
.init_port = ns16550_omap_init_port,
|
2013-11-26 12:08:28 +00:00
|
|
|
.linux_console_name = "ttyO",
|
2013-11-18 13:57:32 +00:00
|
|
|
};
|
|
|
|
|
2008-06-05 17:45:21 +00:00
|
|
|
/**
|
2009-12-15 08:28:28 +00:00
|
|
|
* @brief Probe entry point -called on the first match for device
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
2009-07-31 11:07:03 +00:00
|
|
|
* @param[in] dev matched device
|
2008-06-05 17:45:21 +00:00
|
|
|
*
|
|
|
|
* @return EINVAL if platform_data is not populated,
|
|
|
|
* ENOMEM if calloc failed
|
|
|
|
* else return result of console_register
|
|
|
|
*/
|
|
|
|
static int ns16550_probe(struct device_d *dev)
|
|
|
|
{
|
2013-07-22 19:33:46 +00:00
|
|
|
struct ns16550_priv *priv;
|
2008-06-05 17:45:21 +00:00
|
|
|
struct console_device *cdev;
|
|
|
|
struct NS16550_plat *plat = (struct NS16550_plat *)dev->platform_data;
|
2013-11-18 13:57:32 +00:00
|
|
|
struct ns16550_drvdata *devtype;
|
2013-07-22 19:42:56 +00:00
|
|
|
int ret;
|
2008-06-05 17:45:21 +00:00
|
|
|
|
2013-11-18 13:57:32 +00:00
|
|
|
ret = dev_get_drvdata(dev, (unsigned long *)&devtype);
|
|
|
|
if (ret)
|
|
|
|
devtype = &ns16550_drvdata;
|
|
|
|
|
2011-07-29 03:35:50 +00:00
|
|
|
dev->priv = dev_request_mem_region(dev, 0);
|
2008-06-05 17:45:21 +00:00
|
|
|
|
2013-07-22 19:33:46 +00:00
|
|
|
priv = xzalloc(sizeof(*priv));
|
|
|
|
|
2013-07-22 19:55:36 +00:00
|
|
|
if (plat)
|
|
|
|
priv->plat = *plat;
|
|
|
|
else
|
|
|
|
ns16550_probe_dt(dev, priv);
|
|
|
|
|
2013-07-22 19:42:56 +00:00
|
|
|
if (!plat || !plat->clock) {
|
|
|
|
priv->clk = clk_get(dev, NULL);
|
|
|
|
if (IS_ERR(priv->clk)) {
|
|
|
|
ret = PTR_ERR(priv->clk);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
priv->plat.clock = clk_get_rate(priv->clk);
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:28:01 +00:00
|
|
|
if (priv->plat.clock == 0 && IS_ENABLED(CONFIG_OFDEVICE)) {
|
|
|
|
struct device_node *np = dev->device_node;
|
|
|
|
|
|
|
|
of_property_read_u32(np, "clock-frequency", &priv->plat.clock);
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:42:56 +00:00
|
|
|
if (priv->plat.clock == 0) {
|
|
|
|
dev_err(dev, "no valid clockrate\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-07-22 19:33:46 +00:00
|
|
|
priv->access_width = dev->resource[0].flags & IORESOURCE_MEM_TYPE_MASK;
|
2008-06-05 17:45:21 +00:00
|
|
|
|
2013-07-23 15:28:02 +00:00
|
|
|
cdev = &priv->cdev;
|
2008-06-05 17:45:21 +00:00
|
|
|
cdev->dev = dev;
|
|
|
|
cdev->tstc = ns16550_tstc;
|
|
|
|
cdev->putc = ns16550_putc;
|
|
|
|
cdev->getc = ns16550_getc;
|
|
|
|
cdev->setbrg = ns16550_setbaudrate;
|
2013-11-26 12:08:28 +00:00
|
|
|
cdev->linux_console_name = devtype->linux_console_name;
|
2008-06-05 17:45:21 +00:00
|
|
|
|
2013-11-18 13:57:32 +00:00
|
|
|
devtype->init_port(cdev);
|
2008-06-05 17:45:21 +00:00
|
|
|
|
|
|
|
return console_register(cdev);
|
2013-07-22 19:42:56 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
free(priv);
|
|
|
|
|
|
|
|
return ret;
|
2008-06-05 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 19:55:36 +00:00
|
|
|
static struct of_device_id ns16550_serial_dt_ids[] = {
|
|
|
|
{
|
|
|
|
.compatible = "ns16550a",
|
2013-11-18 13:57:32 +00:00
|
|
|
.data = (unsigned long)&ns16550_drvdata,
|
|
|
|
}, {
|
2013-09-11 09:58:16 +00:00
|
|
|
.compatible = "snps,dw-apb-uart",
|
2013-11-18 13:57:32 +00:00
|
|
|
.data = (unsigned long)&ns16550_drvdata,
|
|
|
|
},
|
|
|
|
#if IS_ENABLED(CONFIG_ARCH_OMAP)
|
|
|
|
{
|
|
|
|
.compatible = "ti,omap2-uart",
|
|
|
|
.data = (unsigned long)&omap_drvdata,
|
|
|
|
}, {
|
|
|
|
.compatible = "ti,omap3-uart",
|
|
|
|
.data = (unsigned long)&omap_drvdata,
|
|
|
|
}, {
|
|
|
|
.compatible = "ti,omap4-uart",
|
|
|
|
.data = (unsigned long)&omap_drvdata,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* sentinel */
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static __maybe_unused struct platform_device_id ns16550_serial_ids[] = {
|
|
|
|
{
|
|
|
|
.name = "omap-uart",
|
|
|
|
.driver_data = (unsigned long)&omap_drvdata,
|
2013-07-22 19:55:36 +00:00
|
|
|
}, {
|
|
|
|
/* sentinel */
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2008-06-05 17:45:21 +00:00
|
|
|
/**
|
|
|
|
* @brief Driver registration structure
|
|
|
|
*/
|
|
|
|
static struct driver_d ns16550_serial_driver = {
|
serial_ns16550: change the driver's name
The name "serial_ns16550" is not handy because the devices
will have the names like "serial_ns165500", "serial_ns165501",
"serial_ns165502" etc.
The names like "ns16550_serial0" and "ns16550_serial1" look
much better.
Also it is reasonable to make serial driver's names unification.
E.g. see the names for drivers in drivers/serial directory:
"netx_serial",
"mpc5xxx_serial",
"altera_serial",
"s3c_serial",
"imx_serial",
"pxa_serial",
"blackfin_serial",
"stm_serial",
"pl010_serial",
and even "g_serial" in ./drivers/usb/gadget/serial.c
Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2012-07-20 05:46:49 +00:00
|
|
|
.name = "ns16550_serial",
|
2008-06-05 17:45:21 +00:00
|
|
|
.probe = ns16550_probe,
|
2013-07-22 19:55:36 +00:00
|
|
|
.of_compatible = DRV_OF_COMPAT(ns16550_serial_dt_ids),
|
2013-11-18 13:57:32 +00:00
|
|
|
#if IS_ENABLED(CONFIG_ARCH_OMAP)
|
|
|
|
.id_table = ns16550_serial_ids,
|
|
|
|
#endif
|
2008-06-05 17:45:21 +00:00
|
|
|
};
|
2013-03-10 06:41:32 +00:00
|
|
|
console_platform_driver(ns16550_serial_driver);
|