Skip to contentSkip to conversation

lib: utils/fdt: Parse for UART "clocks" property if "clock-frequency" is absent

PATCH New Tan En De 3 participants 4 messages Apr 6, 2023 – Apr 12, 2023

lore
Share & tools
lore

Tan En De

author
#0 lore raw

PATCH 1/1 lib: utils/fdt: Parse for UART "clocks" property if "clock-frequency" is absent

For UART node, current implementation of fdt_parse_uart_node_common() checks for "clock-frequency" property only. However, for UART node that uses more than one clock,
"clock-frequency" may need to be parsed from the phandle specified in "clocks" property.
So, this commit adds in logic to parse "clocks" property if "clock-frequency" is absent.

---
 include/sbi_utils/fdt/fdt_helper.h |  6 +++
 lib/utils/fdt/fdt_helper.c         | 65 +++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 2 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +6 -0
index 39d7f3a..d84040e 100644
@@ -32,6 +32,12 @@ struct platform_uart_data {
 	unsigned long reg_shift;
 	unsigned long reg_io_width;
 	unsigned long reg_offset;
+	/*
+	 * For multi-clock UART node.
+	 * clock_index shall be set to zero-indexed main clock's index in the "clocks" list,
+	 * from which freq will be obtained for baud calculation.
+	 */
+	unsigned long clock_index;
 };
 
 const struct fdt_match *fdt_match_node(void *fdt, int nodeoff,
lib/utils/fdt/fdt_helper.c +63 -2

77 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 lib: utils/fdt: Parse for UART "clocks" property if "clock-frequency" is absent

Quoted 5 lines · Tan En De

For UART node, current implementation of fdt_parse_uart_node_common() checks for "clock-frequency" property only. However, for UART node that uses more than one clock,
"clock-frequency" may need to be parsed from the phandle specified in "clocks" property.
So, this commit adds in logic to parse "clocks" property if "clock-frequency" is absent.

FreeBSD?s common FDT UART code doesn?t do this. What specific hardware/bindings do you need this for? If this is for the snps,dw-apb-uart (which I would assume is what it?s for given you?re a StarFive employee) then why? The Linux driver assumes that clock-frequency exists, as does FreeBSD.

Needs further justification, otherwise it?s a NAK from me.

Jess

Quoted 115 lines

---
include/sbi_utils/fdt/fdt_helper.h | 6 +++
lib/utils/fdt/fdt_helper.c | 65 +++++++++++++++++++++++++++++-

2 files changed, 69 insertions(+), 2 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +6 -0
index 39d7f3a..d84040e 100644
@@ -32,6 +32,12 @@ struct platform_uart_data {
 	unsigned long reg_shift;
 	unsigned long reg_io_width;
 	unsigned long reg_offset;
+	/*
+	 * For multi-clock UART node.
+	 * clock_index shall be set to zero-indexed main clock's index in the "clocks" list,
+	 * from which freq will be obtained for baud calculation.
+	 */
+	unsigned long clock_index;
 };
 
 const struct fdt_match *fdt_match_node(void *fdt, int nodeoff,
lib/utils/fdt/fdt_helper.c +63 -2
index 48bc2fe..d68d886 100644
@@ -313,6 +313,62 @@ int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
 	return 0;
 }
 
+/*
+ * For multi-clock UART node.
+ * Parse "clocks" property in UART node to set uart->freq.
+ * "clocks" property is an array of clock phandle and clock specifier pairs.
+ * Before calling this function, uart->clock_index shall be set with
+ * the index of the pair containing the main UART clock's phandle,
+ * which baudrate calculation will be based on.
+ * Return 0 on success, else return -1.
+ */
+static int fdt_parse_uart_clocks_prop(void *fdt, int nodeoffset,
+				struct platform_uart_data *uart)
+{
+	const fdt32_t *clocks_val, *clock_cells_val, *clock_frequency_val;
+	uint32_t clock_phandle, cell_index, clock_cells;
+	int clocks_len, clock_offset, clock_cells_len, clock_frequency_len;
+	unsigned long clock_index = uart->clock_index;
+
+	/* clocks_val is a flattened array of values, made from the phandle and specifier pairs. */
+	clocks_val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clocks", &clocks_len);
+	if (!(clocks_len > 0 && clocks_val))
+		return -1;
+
+	cell_index = 0; /* index into the clocks_val */
+	for (int i = 0; i <= clock_index; i++) {
+		/* In case of out-of-bound access to the clocks_val array, return -1. */
+		if (cell_index + 1 > clocks_len)
+			return -1;
+		/* Else, parse the phandle */
+		clock_phandle = fdt32_to_cpu(clocks_val[cell_index]);
+		clock_offset = fdt_node_offset_by_phandle(fdt, clock_phandle);
+		/*
+		 * "#clock-cells" is a required property,
+		 * it tells how many cells the clock specifier takes.
+		 */
+		clock_cells_val = (fdt32_t *)fdt_getprop(fdt, clock_offset, "#clock-cells", &clock_cells_len);
+		if (clock_cells_len > 0 && clock_cells_val)
+			clock_cells = fdt32_to_cpu(*clock_cells_val);
+		else
+			return -1;
+		/*
+		 * Advance clock_index to get cell index to next phandle.
+		 * Each phandle and clock specifier pair consists of
+		 * 1 phandle and clock_cells number of specifiers.
+		 */
+		cell_index += 1 + clock_cells;
+	}
+	/* Finally, obtain the uart->freq from "clock-freqeuncy" property */
+	clock_frequency_val = (fdt32_t *)fdt_getprop(fdt, clock_offset, "clock-frequency", &clock_frequency_len);
+	if (clock_frequency_len > 0 && clock_frequency_val)
+		uart->freq = fdt32_to_cpu(*clock_frequency_val);
+	else
+		return -1;
+
+	return 0;
+}
+
 static int fdt_parse_uart_node_common(void *fdt, int nodeoffset,
 				      struct platform_uart_data *uart,
 				      unsigned long default_freq,
@@ -338,8 +394,13 @@ static int fdt_parse_uart_node_common(void *fdt, int nodeoffset,
 	val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clock-frequency", &len);
 	if (len > 0 && val)
 		uart->freq = fdt32_to_cpu(*val);
-	else
-		uart->freq = default_freq;
+	else {
+		/* If "clock-frequency" property is absent, try parse for "clocks" property */
+		rc = fdt_parse_uart_clocks_prop(fdt, nodeoffset, uart);
+		/* Fall back to default frequency in case of failure to parse */
+		if (rc < 0)
+			uart->freq = default_freq;
+	}
 
 	val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "current-speed", &len);
 	if (len > 0 && val)
Signature · 7 lines
2.34.1


-- 
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
author
#2 lore raw

PATCH 1/1 lib: utils/fdt: Parse for UART "clocks" property if "clock-frequency" is absent

Hi, taking example UART nodes from snps-dw-apb-uart.yaml
https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/serial/snps-dw-apb-uart.yaml
(The following is just for examples, it's relevant to many other vendor's UART device tree too)
=================================
? // Example with one clock:
? serial at 80230000 {
??? ....
??? clocks = <&baudclk>;
??? ....
??? };

? // Example with two clocks:
? serial at 80230000 {
??? ....
??? clocks = <&baudclk>, <&apb_pclk>;
??? clock-names = "baudclk", "apb_pclk";
?? ....
??? };
=================================

Notice that "clock-frequency" property is absent in the serial node. However, we can get the "clock-frequency" if we parse the phandle in the "clocks" property.
For example, we can use the &bauclk phandle to trace back to baudclk node.
=================================
? baudclk {
??? compatible = "fixed-clock";
??? #clock-cells = <0>;
??? clock-frequency = <uart_clock_frequency_here>;
??? phandle = <phandle_of_baudclk>;
? ? };
=================================

As UART driver vendors have so many ways of presenting the UART device tree node,
here are some possible solutions to cater them:
-------------
Solution 1
-------------
The easiest solution, replicate the "clock-frequency" property into the UART node.
=================================
? serial at 80230000 {
??? ....
??? clocks = <&baudclk>, <&apb_pclk>;
??? clock-names = "baudclk", "apb_pclk";
??? clock-frequency = <uart_clock_frequency_here>;
??? ....
??? };
=================================

-------------
Solution 2
-------------
This is the solution provided in this patch submission.
Use clock index to specify which entry in the "clocks" property contains the main UART clock's phandle, which baudrate calculation will be calculated from.
For example, let's consider a fictitious UART node like this
=================================
? serial at 12341234 {
??? ....
??? clocks = <&internal_clk SOME_SPECIFIER ANOTHER_SPECIFIER>,
<&auxiliary_clk ANOTHER_SPECIFIER>, <&uart_clk>;
??? clock-names = "intclk", "auxclk", "uartclk";
?? ....
?? };
=================================
where internal_clk is indexed 0, auxiliary_clk is indexed 1, uart_clk is indexed 2.
Using the function provided in this patch submission,
since uart_clk is indexed 2, set uart.clock_index = 2,
then call fdt_parse_uart_clocks_prop() to parse the uart_clk and set the uart.freq.

-------------
Solution 3
-------------
Simply leave the fdt parsing mentioned in Solution 2 to the vendor's implementation in the fdt_serial_<vendor>.c
If this is the preferred solution, this patch shall be archived and changes shall be made to fdt_serial_cadence.c instead.

I'm having paradox of choice, let me know what's your opinion, thanks. :-)

Quoted 14 lines · Jessica Clarke
Quoted 4 lines · Tan En De

For UART node, current implementation of fdt_parse_uart_node_common() checks for "clock-frequency" property only. However, for UART node that uses more than one clock,
"clock-frequency" may need to be parsed from the phandle specified in "clocks" property.
So, this commit adds in logic to parse "clocks" property if "clock-frequency" is absent.

FreeBSD?s common FDT UART code doesn?t do this. What specific hardware/bindings do you need this for? If this is for the snps,dw-apb-uart (which I would assume is what it?s for given you?re a StarFive employee) then why? The Linux driver assumes that clock-frequency exists, as does FreeBSD.

Needs further justification, otherwise it?s a NAK from me.

Jess

#3 lore raw

PATCH 1/1 lib: utils/fdt: Parse for UART "clocks" property if "clock-frequency" is absent

In reply to Tan En De

? 2023-04-06???? 14:26 +0800?Tan En De???

Quoted 17 lines

For UART node, current implementation of fdt_parse_uart_node_common() checks for "clock-frequency" property only. However, for UART node that uses more than one clock,
"clock-frequency" may need to be parsed from the phandle specified in "clocks" property.
So, this commit adds in logic to parse "clocks" property if "clock-frequency" is absent.

---
?include/sbi_utils/fdt/fdt_helper.h |? 6 +++ ?lib/utils/fdt/fdt_helper.c???????? | 65 +++++++++++++++++++++++++++++- ?2 files changed, 69 insertions(+), 2 deletions(-)

include/sbi_utils/fdt/fdt_helper.h +0 -0
index 39d7f3a..d84040e 100644

include/sbi_utils/fdt/fdt_helper.h struct platform_uart_data { · line 40

Quoted 9 lines
include/sbi_utils/fdt/fdt_helper.h +6 -0
@@ -32,6 +32,12 @@ struct platform_uart_data {
 ????????unsigned long reg_shift;
 ????????unsigned long reg_io_width;
 ????????unsigned long reg_offset;
+???????/*
+??????? * For multi-clock UART node.
+??????? * clock_index shall be set to zero-indexed main clock's index in the "clocks" list,
+??????? * from which freq will be obtained for baud calculation.
+??????? */
+???????unsigned long clock_index;

Where is clock_index set. Don't see specific driver modifications. It can be sent with the patch to modify the driver

Regards,
Xiang W

Quoted 90 lines
include/sbi_utils/fdt/fdt_helper.h +0 -0
 ?};

?
?const struct fdt_match *fdt_match_node(void *fdt, int nodeoff,

lib/utils/fdt/fdt_helper.c +0 -0
index 48bc2fe..d68d886 100644
@@ -313,6 +313,62 @@ int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
 ????????return 0;
 ?}

?

lib/utils/fdt/fdt_helper.c +56 -0
+/*
+ * For multi-clock UART node.
+ * Parse "clocks" property in UART node to set uart->freq.
+ * "clocks" property is an array of clock phandle and clock specifier pairs.
+ * Before calling this function, uart->clock_index shall be set with
+ * the index of the pair containing the main UART clock's phandle,
+ * which baudrate calculation will be based on.
+ * Return 0 on success, else return -1.
+ */
+static int fdt_parse_uart_clocks_prop(void *fdt, int nodeoffset,
+???????????????????????????????struct platform_uart_data *uart)
+{
+???????const fdt32_t *clocks_val, *clock_cells_val, *clock_frequency_val;
+???????uint32_t clock_phandle, cell_index, clock_cells;
+???????int clocks_len, clock_offset, clock_cells_len, clock_frequency_len;
+???????unsigned long clock_index = uart->clock_index;
+
+???????/* clocks_val is a flattened array of values, made from the phandle and specifier pairs. */
+???????clocks_val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clocks", &clocks_len);
+???????if (!(clocks_len > 0 && clocks_val))
+???????????????return -1;
+
+???????cell_index = 0; /* index into the clocks_val */
+???????for (int i = 0; i <= clock_index; i++) {
+???????????????/* In case of out-of-bound access to the clocks_val array, return -1. */
+???????????????if (cell_index + 1 > clocks_len)
+???????????????????????return -1;
+???????????????/* Else, parse the phandle */
+???????????????clock_phandle = fdt32_to_cpu(clocks_val[cell_index]);
+???????????????clock_offset = fdt_node_offset_by_phandle(fdt, clock_phandle);
+???????????????/*
+??????????????? * "#clock-cells" is a required property,
+??????????????? * it tells how many cells the clock specifier takes.
+??????????????? */
+???????????????clock_cells_val = (fdt32_t *)fdt_getprop(fdt, clock_offset, "#clock-cells", &clock_cells_len);
+???????????????if (clock_cells_len > 0 && clock_cells_val)
+???????????????????????clock_cells = fdt32_to_cpu(*clock_cells_val);
+???????????????else
+???????????????????????return -1;
+???????????????/*
+??????????????? * Advance clock_index to get cell index to next phandle.
+??????????????? * Each phandle and clock specifier pair consists of
+??????????????? * 1 phandle and clock_cells number of specifiers.
+??????????????? */
+???????????????cell_index += 1 + clock_cells;
+???????}
+???????/* Finally, obtain the uart->freq from "clock-freqeuncy" property */
+???????clock_frequency_val = (fdt32_t *)fdt_getprop(fdt, clock_offset, "clock-frequency", &clock_frequency_len);
+???????if (clock_frequency_len > 0 && clock_frequency_val)
+???????????????uart->freq = fdt32_to_cpu(*clock_frequency_val);
+???????else
+???????????????return -1;
+
+???????return 0;
+}
+

?static int fdt_parse_uart_node_common(void *fdt, int nodeoffset, ????????????????????????????????????? struct platform_uart_data *uart, ????????????????????????????????????? unsigned long default_freq,

lib/utils/fdt/fdt_helper.c +7 -2
@@ -338,8 +394,13 @@ static int fdt_parse_uart_node_common(void *fdt, int nodeoffset,
 ????????val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clock-frequency", &len);
 ????????if (len > 0 && val)
 ????????????????uart->freq = fdt32_to_cpu(*val);
-???????else
-???????????????uart->freq = default_freq;
+???????else {
+???????????????/* If "clock-frequency" property is absent, try parse for "clocks" property */
+???????????????rc = fdt_parse_uart_clocks_prop(fdt, nodeoffset, uart);
+???????????????/* Fall back to default frequency in case of failure to parse */
+???????????????if (rc < 0)
+???????????????????????uart->freq = default_freq;
+???????}

?
????????val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "current-speed", &len); ????????if (len > 0 && val)

Signature · 1 line
2.34.1