devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] serial: 8250_dw: set clock rate
@ 2013-05-09 10:29 James Hogan
  2013-05-09 12:58 ` James Hogan
  0 siblings, 1 reply; 2+ messages in thread
From: James Hogan @ 2013-05-09 10:29 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-serial, devicetree-discuss, linux-doc, linux-kernel,
	James Hogan, Grant Likely, Rob Herring, Rob Landley, Jiri Slaby,
	Heikki Krogerus, Alan Cox, Jamie Iles, Bill Pemberton

If the uart clock provided to the 8250_dw driver is adjustable it may
not be set to the desired rate. Therefore if both a uart clock and a
clock frequency is specified (e.g. via device tree), try and update the
clock to match the frequency.

Unfortunately if the resulting frequency is rounded down (which is the
default behaviour of the generic clk-divider), the 8250 core won't allow
the highest baud rate to be used, so if an explicit frequency is
specified we always report that to the 8250 core.

The device tree bindings document is also updated accordingly.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Grant Likely <grant.likely@linaro.org>
Cc: Rob Herring <rob.herring@calxeda.com>
Cc: Rob Landley <rob@landley.net>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jiri Slaby <jslaby@suse.cz>
Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: Alan Cox <alan@linux.intel.com>
Cc: Jamie Iles <jamie@jamieiles.com>
Cc: Bill Pemberton <wfp5p@virginia.edu>
---
 .../bindings/tty/serial/snps-dw-apb-uart.txt       |  4 +-
 drivers/tty/serial/8250/8250_dw.c                  | 47 +++++++++++++++++-----
 2 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/Documentation/devicetree/bindings/tty/serial/snps-dw-apb-uart.txt b/Documentation/devicetree/bindings/tty/serial/snps-dw-apb-uart.txt
index f13f1c5..e0cfc47 100644
--- a/Documentation/devicetree/bindings/tty/serial/snps-dw-apb-uart.txt
+++ b/Documentation/devicetree/bindings/tty/serial/snps-dw-apb-uart.txt
@@ -4,9 +4,11 @@ Required properties:
 - compatible : "snps,dw-apb-uart"
 - reg : offset and length of the register set for the device.
 - interrupts : should contain uart interrupt.
-- clock-frequency : the input clock frequency for the UART.
 
 Optional properties:
+- clock-frequency : the input clock frequency for the UART. If specified in
+  addition to clocks, the clock rate will be set to this frequency.
+- clocks : the input clock specifier for the UART.
 - reg-shift : quantity to shift the register offsets by.  If this property is
   not present then the register offsets are not shifted.
 - reg-io-width : the size (in bytes) of the IO accesses that should be
diff --git a/drivers/tty/serial/8250/8250_dw.c b/drivers/tty/serial/8250/8250_dw.c
index beaa283..85b3b63 100644
--- a/drivers/tty/serial/8250/8250_dw.c
+++ b/drivers/tty/serial/8250/8250_dw.c
@@ -128,6 +128,7 @@ dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
 static int dw8250_probe_of(struct uart_port *p)
 {
 	struct device_node	*np = p->dev->of_node;
+	struct dw8250_data	*d = p->private_data;
 	u32			val;
 
 	if (!of_property_read_u32(np, "reg-io-width", &val)) {
@@ -148,16 +149,13 @@ static int dw8250_probe_of(struct uart_port *p)
 	if (!of_property_read_u32(np, "reg-shift", &val))
 		p->regshift = val;
 
-	/* clock got configured through clk api, all done */
-	if (p->uartclk)
-		return 0;
-
-	/* try to find out clock frequency from DT as fallback */
-	if (of_property_read_u32(np, "clock-frequency", &val)) {
+	/* try to find out clock frequency from DT */
+	if (!of_property_read_u32(np, "clock-frequency", &val)) {
+		p->uartclk = val;
+	} else if (IS_ERR(d->clk)) {
 		dev_err(p->dev, "clk or clock-frequency not defined\n");
 		return -EINVAL;
 	}
-	p->uartclk = val;
 
 	return 0;
 }
@@ -235,6 +233,7 @@ static int dw8250_probe(struct platform_device *pdev)
 	struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 	struct dw8250_data *data;
 	int err;
+	unsigned long clk_rate;
 
 	if (!regs || !irq) {
 		dev_err(&pdev->dev, "no registers/irq defined\n");
@@ -260,10 +259,6 @@ static int dw8250_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	data->clk = devm_clk_get(&pdev->dev, NULL);
-	if (!IS_ERR(data->clk)) {
-		clk_prepare_enable(data->clk);
-		uart.port.uartclk = clk_get_rate(data->clk);
-	}
 
 	uart.port.iotype = UPIO_MEM;
 	uart.port.serial_in = dw8250_serial_in;
@@ -284,6 +279,36 @@ static int dw8250_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
+	/* Read the clock rate from the clock */
+	if (!IS_ERR(data->clk)) {
+		clk_prepare_enable(data->clk);
+		clk_rate = clk_get_rate(data->clk);
+		/*
+		 * If uartclk hasn't been explicitly specified (e.g. from device
+		 * tree), use the rate from the clock instead.
+		 */
+		if (!uart.port.uartclk)
+			uart.port.uartclk = clk_rate;
+		/*
+		 * If the current clock rate differs from the rate specified,
+		 * try and set the clock rate.
+		 */
+		if (uart.port.uartclk != clk_rate) {
+			if (!clk_set_rate(data->clk, uart.port.uartclk))
+				clk_rate = clk_get_rate(data->clk);
+			dev_info(&pdev->dev,
+				 "uartclk at %lu Hz (%u Hz requested)\n",
+				 clk_rate, uart.port.uartclk);
+			/*
+			 * Don't update uartclk. If the rate was rounded down by
+			 * clk_set_rate() the 8250 core won't allow the highest
+			 * baud rate to be used.
+			 */
+		} else {
+			dev_info(&pdev->dev, "uartclk at %lu Hz\n", clk_rate);
+		}
+	}
+
 	data->line = serial8250_register_8250_port(&uart);
 	if (data->line < 0)
 		return data->line;
-- 
1.8.1.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] serial: 8250_dw: set clock rate
  2013-05-09 10:29 [PATCH] serial: 8250_dw: set clock rate James Hogan
@ 2013-05-09 12:58 ` James Hogan
  0 siblings, 0 replies; 2+ messages in thread
From: James Hogan @ 2013-05-09 12:58 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: James Hogan, linux-serial, devicetree-discuss, linux-doc,
	linux-kernel, Grant Likely, Rob Herring, Rob Landley, Jiri Slaby,
	Heikki Krogerus, Alan Cox, Jamie Iles, Bill Pemberton

On 09/05/13 11:29, James Hogan wrote:
> If the uart clock provided to the 8250_dw driver is adjustable it may
> not be set to the desired rate. Therefore if both a uart clock and a
> clock frequency is specified (e.g. via device tree), try and update the
> clock to match the frequency.
> 
> Unfortunately if the resulting frequency is rounded down (which is the
> default behaviour of the generic clk-divider), the 8250 core won't allow
> the highest baud rate to be used, so if an explicit frequency is
> specified we always report that to the 8250 core.

Hi,

Sorry, please ignore this patch.

I've realised that a larger (e.g. non-divided) source clock can just be
provided directly to the uart and it appears to have enough of an
internal divider for the driver to do the right thing without changing
the input clock rate, although of course that clock rate still needs
setting somewhere. I'm not sure why we didn't do this all along as it
gives a closer frequency anyway.

Thanks
James


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-05-09 12:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-09 10:29 [PATCH] serial: 8250_dw: set clock rate James Hogan
2013-05-09 12:58 ` James Hogan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).