Skip to contentSkip to conversation

LiteX SoC controller and LiteUART serial driver

PATCH Superseded Mateusz Holenko 4 participants 22 messages Sep 23, 2020 – Oct 6, 2020

Subsystems (3)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Mateusz Holenko

author maintainer
#0 lore raw

v11 cover LiteX SoC controller and LiteUART serial driver

This patchset introduces support for LiteX SoC Controller and LiteUART - serial device from LiteX SoC builder
(https://github.com/enjoy-digital/litex).

In the following patchset I will add
a new mor1kx-based (OpenRISC) platform that
uses this device.

Later I plan to extend this platform by
adding support for more devices from LiteX suite.

Changes in v11:
- added Reviewed-by tag
- reformatted some comments
- switched to WARN instead of BUG on CSR validation fail

Changes in v10:
- added casting to avoid sparse warnings in the SoC Controller's driver

Changes in v9:
- fixed the reg node notation in the DT example
- added exporting of the litex_set_reg/litex_get_reg symbols

Changes in v8:
- fixed help messages in LiteUART's KConfig
- removed dependency between LiteUART and LiteX SoC drivers
- removed litex_check_accessors() helper function
- added crashing (BUG) on the failed LiteX CSR access test

Changes in v7:
- added missing include directive in UART's driver

Changes in v6:
- changed accessors in SoC Controller's driver
- reworked UART driver

Changes in v5:
- added Reviewed-by tag
- removed custom accessors from SoC Controller's driver
- fixed error checking in SoC Controller's driver

Changes in v4:
- fixed copyright headers
- fixed SoC Controller's yaml
- simplified SoC Controller's driver

Changes in v3:
- added Acked-by and Reviewed-by tags
- introduced LiteX SoC Controller driver
- removed endianness detection (handled now by LiteX SoC Controller driver)
- modified litex.h header
- DTS aliases for LiteUART made optional
- renamed SERIAL_LITEUART_NR_PORTS to SERIAL_LITEUART_MAX_PORTS
- changed PORT_LITEUART from 122 to 123

Changes in v2:
- binding description rewritten to a yaml schema file
- added litex.h header with common register access functions

Filip Kokosinski (3):
dt-bindings: vendor: add vendor prefix for LiteX
dt-bindings: serial: document LiteUART bindings drivers/tty/serial: add LiteUART driver

Pawel Czarnecki (2):
dt-bindings: soc: document LiteX SoC Controller bindings drivers/soc/litex: add LiteX SoC Controller driver

 .../bindings/serial/litex,liteuart.yaml       |  38 ++
 .../soc/litex/litex,soc-controller.yaml       |  39 ++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 MAINTAINERS                                   |   9 +
 drivers/soc/Kconfig                           |   1 +
 drivers/soc/Makefile                          |   1 +
 drivers/soc/litex/Kconfig                     |  15 +
 drivers/soc/litex/Makefile                    |   3 +
 drivers/soc/litex/litex_soc_ctrl.c            | 194 +++++++++
 drivers/tty/serial/Kconfig                    |  32 ++
 drivers/tty/serial/Makefile                   |   1 +
 drivers/tty/serial/liteuart.c                 | 402 ++++++++++++++++++
 include/linux/litex.h                         |  24 ++
 13 files changed, 761 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/litex,liteuart.yaml
 create mode 100644 Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml
 create mode 100644 drivers/soc/litex/Kconfig
 create mode 100644 drivers/soc/litex/Makefile
 create mode 100644 drivers/soc/litex/litex_soc_ctrl.c
 create mode 100644 drivers/tty/serial/liteuart.c
 create mode 100644 include/linux/litex.h
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v11 1/5 dt-bindings: vendor: add vendor prefix for LiteX

From: Filip Kokosinski <[email protected]>

Add vendor prefix for LiteX SoC builder.

---

Notes:
No changes in v11.

No changes in v10.

No changes in v9.

No changes in v8.

No changes in v7.

No changes in v6.

No changes in v5.

No changes in v4.

Changes in v3:
- added Acked-by tag

No changes in v2.

 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index d3891386d671..9aae6c56d7a3 100644
@@ -571,6 +571,8 @@ patternProperties:
     description: Linux-specific binding
   "^linx,.*":
     description: Linx Technologies
+  "^litex,.*":
+    description: LiteX SoC builder
   "^lltc,.*":
     description: Linear Technology Corporation
   "^logicpd,.*":
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH v11 2/5 dt-bindings: soc: document LiteX SoC Controller bindings

In reply to Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

Add documentation for LiteX SoC Controller bindings.

---

Notes:
No changes in v11.

No changes in v10.

Changes in v9:
- fixed the reg node notation in the DT example

No changes in v8.

No changes in v7.

Changes in v6:
- fixed license header

Changes in v5:
- added reviewed-by tag

Changes in v4:
- changes compatible to "litex,soc-controller"
- fixed yaml's header
- removed unnecessary sections from yaml
- fixed indentation in yaml

This commit has been introduced in v3 of the patchset.

 .../soc/litex/litex,soc-controller.yaml       | 39 +++++++++++++++++++
 MAINTAINERS                                   |  6 +++
 2 files changed, 45 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +39 -0
new file mode 100644
index 000000000000..53121c1fbe4d
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+# Copyright 2020 Antmicro <www.antmicro.com>
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/soc/litex/litex,soc-controller.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: LiteX SoC Controller driver
+
+description: |
+  This is the SoC Controller driver for the LiteX SoC Builder.
+  It's purpose is to verify LiteX CSR (Control&Status Register) access
+  operations and provide function for other drivers to read/write CSRs
+  and to check if those accessors are ready to use.
+
+maintainers:
+  - Karol Gugala <[email protected]>
+  - Mateusz Holenko <[email protected]>
+
+properties:
+  compatible:
+    const: litex,soc-controller
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    soc_ctrl0: soc-controller@f0000000 {
+        compatible = "litex,soc-controller";
+        reg = <0xf0000000 0xC>;
+        status = "okay";
+    };
+
+...
MAINTAINERS +6 -0
index 7b58ca29cc80..39be98db7418 100644
@@ -9835,6 +9835,12 @@ L:	[email protected]
 S:	Maintained
 F:	lib/list-test.c
 
+LITEX PLATFORM
+M:	Karol Gugala <[email protected]>
+M:	Mateusz Holenko <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/*/litex,*.yaml
+
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>
 M:	Jiri Kosina <[email protected]>
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v11 2/5] dt-bindings: soc: document LiteX SoC Controller bindings

Hi Mateusz,

Quoted 7 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

Add documentation for LiteX SoC Controller bindings.

Thanks for your patch!

Quoted 5 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +2 -0
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+# Copyright 2020 Antmicro <www.antmicro.com>

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 12

Quoted 10 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +10 -0
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/soc/litex/litex,soc-controller.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: LiteX SoC Controller driver
+
+description: |
+  This is the SoC Controller driver for the LiteX SoC Builder.
+  It's purpose is to verify LiteX CSR (Control&Status Register) access

Its

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 13

Quoted 1 line
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +1 -0
+  operations and provide function for other drivers to read/write CSRs

functions

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 14

Quoted 1 line
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +1 -0
+  and to check if those accessors are ready to use.

be used

Quoted 11 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +11 -0
+
+maintainers:
+  - Karol Gugala <[email protected]>
+  - Mateusz Holenko <[email protected]>
+
+properties:
+  compatible:
+    const: litex,soc-controller
+
+  reg:
+    maxItems: 1

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 35

Quoted 10 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +10 -0
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    soc_ctrl0: soc-controller@f0000000 {
+        compatible = "litex,soc-controller";
+        reg = <0xf0000000 0xC>;

Please be consistent w.r.t. lower/upper case: "0xc".

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 37

Quoted 2 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +2 -0
+        status = "okay";
+    };

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author maintainer
#4 lore raw

Re: [PATCH v11 2/5] dt-bindings: soc: document LiteX SoC Controller bindings

Hi Geert,

Quoted 6 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 2 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

Quoted 7 lines
Quoted 5 lines

Add documentation for LiteX SoC Controller bindings.

Thanks for your patch!

Thanks for your comments!

Quoted 18 lines
Quoted 5 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +2 -0
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+# Copyright 2020 Antmicro <www.antmicro.com>

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 12

Quoted 10 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +10 -0
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/soc/litex/litex,soc-controller.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: LiteX SoC Controller driver
+
+description: |
+  This is the SoC Controller driver for the LiteX SoC Builder.
+  It's purpose is to verify LiteX CSR (Control&Status Register) access

Its

Quoted 7 lines

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 13

Quoted 1 line
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +1 -0
+  operations and provide function for other drivers to read/write CSRs

functions

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 14

Quoted 1 line
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +1 -0
+  and to check if those accessors are ready to use.

be used

I will fix the description as suggested.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +15 -0
+
+maintainers:
+  - Karol Gugala <[email protected]>
+  - Mateusz Holenko <[email protected]>
+
+properties:
+  compatible:
+    const: litex,soc-controller
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
Quoted 8 lines

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 35

Quoted 6 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +6 -0
+
+examples:
+  - |
+    soc_ctrl0: soc-controller@f0000000 {
+        compatible = "litex,soc-controller";
+        reg = <0xf0000000 0xC>;

Please be consistent w.r.t. lower/upper case: "0xc".

Sure, I will use lowercase everywhere.

Quoted 8 lines

Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml line 37

Quoted 2 lines
Documentation/devicetree/bindings/soc/litex/litex,soc-controller.yaml new file +2 -0
+        status = "okay";
+    };

Gr{oetje,eeting}s,

Geert

Quoted 6 lines

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Best regards,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland

author maintainer
#5 lore raw

PATCH v11 3/5 drivers/soc/litex: add LiteX SoC Controller driver

In reply to Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

---

Notes:
Changes in v11:
- removed an unnecessary comment left over from previous version
- changed a multi-line comment to comply with the formatting rules
- use WARN instad of BUG on a failed CSR validation

Changes in v10:
- added casting to avoid sparse warnings in the SoC Controller's driver

Changes in v9:
- added exporting of the litex_set_reg/litex_get_reg symbols

Changes in v8:
- removed litex_check_accessors() helper function
- added crashing (BUG) on the failed LiteX CSR access test

No changes in v7.

Changes in v6:
- added dependency on OF || COMPILE_TEST
- used le32_to_cpu(readl(addr)) instead of __raw_readl and writel(cpu_to_le32(value), addr) instead of __raw_writel to take advantage of memory barriers provided by readl/writel

Changes in v5:
- removed helper accessors and used __raw_readl/__raw_writel instead
- fixed checking for errors in litex_soc_ctrl_probe

Changes in v4:
- fixed indent in Kconfig's help section
- fixed copyright header
- changed compatible to "litex,soc-controller"
- simplified litex_soc_ctrl_probe
- removed unnecessary litex_soc_ctrl_remove

This commit has been introduced in v3 of the patchset.

It includes a simplified version of common 'litex.h' header introduced in v2 of the patchset.

 MAINTAINERS                        |   2 +
 drivers/soc/Kconfig                |   1 +
 drivers/soc/Makefile               |   1 +
 drivers/soc/litex/Kconfig          |  15 +++
 drivers/soc/litex/Makefile         |   3 +
 drivers/soc/litex/litex_soc_ctrl.c | 194 +++++++++++++++++++++++++++++
 include/linux/litex.h              |  24 ++++
 7 files changed, 240 insertions(+)
 create mode 100644 drivers/soc/litex/Kconfig
 create mode 100644 drivers/soc/litex/Makefile
 create mode 100644 drivers/soc/litex/litex_soc_ctrl.c
 create mode 100644 include/linux/litex.h
MAINTAINERS +2 -0
index 39be98db7418..4d70a1b22a87 100644
@@ -9840,6 +9840,8 @@ M:	Karol Gugala <[email protected]>
 M:	Mateusz Holenko <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/*/litex,*.yaml
+F:	drivers/soc/litex/litex_soc_ctrl.c
+F:	include/linux/litex.h
 
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>
drivers/soc/Kconfig +1 -0
index 425ab6f7e375..d097d070f579 100644
@@ -9,6 +9,7 @@ source "drivers/soc/bcm/Kconfig"
 source "drivers/soc/fsl/Kconfig"
 source "drivers/soc/imx/Kconfig"
 source "drivers/soc/ixp4xx/Kconfig"
+source "drivers/soc/litex/Kconfig"
 source "drivers/soc/mediatek/Kconfig"
 source "drivers/soc/qcom/Kconfig"
 source "drivers/soc/renesas/Kconfig"
drivers/soc/Makefile +1 -0
index 36452bed86ef..0b16108823ef 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_ARCH_GEMINI)	+= gemini/
 obj-y				+= imx/
 obj-$(CONFIG_ARCH_IXP4XX)	+= ixp4xx/
 obj-$(CONFIG_SOC_XWAY)		+= lantiq/
+obj-$(CONFIG_LITEX_SOC_CONTROLLER) += litex/
 obj-y				+= mediatek/
 obj-y				+= amlogic/
 obj-y				+= qcom/
drivers/soc/litex/Kconfig new file +15 -0
new file mode 100644
index 000000000000..c974ec3846bc
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"
+
+config LITEX_SOC_CONTROLLER
+	tristate "Enable LiteX SoC Controller driver"
+	depends on OF || COMPILE_TEST
+	help
+	  This option enables the SoC Controller Driver which verifies
+	  LiteX CSR access and provides common litex_get_reg/litex_set_reg
+	  accessors.
+	  All drivers that use functions from litex.h must depend on
+	  LITEX_SOC_CONTROLLER.
+
+endmenu
drivers/soc/litex/Makefile new file +3 -0
new file mode 100644
index 000000000000..98ff7325b1c0
@@ -0,0 +1,3 @@
+# SPDX-License_Identifier: GPL-2.0
+
+obj-$(CONFIG_LITEX_SOC_CONTROLLER)	+= litex_soc_ctrl.o
drivers/soc/litex/litex_soc_ctrl.c new file +194 -0

194 lines not on this page · raw message

include/linux/litex.h new file +24 -0
new file mode 100644
index 000000000000..72061018c172
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+
+#ifndef _LINUX_LITEX_H
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
+#include <linux/compiler_types.h>
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);
+
+
+#endif /* _LINUX_LITEX_H */
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

Quoted 8 lines · On Wed, 23 Sep 2020 12:09:06 +0200 Mateusz Holenko <[email protected]>…

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

A few little things inline, but looks fine in general to me.

Main request is for more formal documentation of parameters to the read and write functions.

Jonathan

Quoted 131 lines

---

Notes:
Changes in v11:
- removed an unnecessary comment left over from previous version
- changed a multi-line comment to comply with the formatting rules
- use WARN instad of BUG on a failed CSR validation

Changes in v10:
- added casting to avoid sparse warnings in the SoC Controller's driver

Changes in v9:
- added exporting of the litex_set_reg/litex_get_reg symbols

Changes in v8:
- removed litex_check_accessors() helper function
- added crashing (BUG) on the failed LiteX CSR access test

No changes in v7.

Changes in v6:
- added dependency on OF || COMPILE_TEST
- used le32_to_cpu(readl(addr)) instead of __raw_readl and writel(cpu_to_le32(value), addr) instead of __raw_writel to take advantage of memory barriers provided by readl/writel

Changes in v5:
- removed helper accessors and used __raw_readl/__raw_writel instead
- fixed checking for errors in litex_soc_ctrl_probe

Changes in v4:
- fixed indent in Kconfig's help section
- fixed copyright header
- changed compatible to "litex,soc-controller"
- simplified litex_soc_ctrl_probe
- removed unnecessary litex_soc_ctrl_remove

This commit has been introduced in v3 of the patchset.

It includes a simplified version of common 'litex.h' header introduced in v2 of the patchset.

 MAINTAINERS                        |   2 +
 drivers/soc/Kconfig                |   1 +
 drivers/soc/Makefile               |   1 +
 drivers/soc/litex/Kconfig          |  15 +++
 drivers/soc/litex/Makefile         |   3 +
 drivers/soc/litex/litex_soc_ctrl.c | 194 +++++++++++++++++++++++++++++
 include/linux/litex.h              |  24 ++++
 7 files changed, 240 insertions(+)
 create mode 100644 drivers/soc/litex/Kconfig
 create mode 100644 drivers/soc/litex/Makefile
 create mode 100644 drivers/soc/litex/litex_soc_ctrl.c
 create mode 100644 include/linux/litex.h
MAINTAINERS +2 -0
index 39be98db7418..4d70a1b22a87 100644
@@ -9840,6 +9840,8 @@ M:	Karol Gugala <[email protected]>
 M:	Mateusz Holenko <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/*/litex,*.yaml
+F:	drivers/soc/litex/litex_soc_ctrl.c
+F:	include/linux/litex.h
 
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>
drivers/soc/Kconfig +1 -0
index 425ab6f7e375..d097d070f579 100644
@@ -9,6 +9,7 @@ source "drivers/soc/bcm/Kconfig"
 source "drivers/soc/fsl/Kconfig"
 source "drivers/soc/imx/Kconfig"
 source "drivers/soc/ixp4xx/Kconfig"
+source "drivers/soc/litex/Kconfig"
 source "drivers/soc/mediatek/Kconfig"
 source "drivers/soc/qcom/Kconfig"
 source "drivers/soc/renesas/Kconfig"
drivers/soc/Makefile +1 -0
index 36452bed86ef..0b16108823ef 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_ARCH_GEMINI)	+= gemini/
 obj-y				+= imx/
 obj-$(CONFIG_ARCH_IXP4XX)	+= ixp4xx/
 obj-$(CONFIG_SOC_XWAY)		+= lantiq/
+obj-$(CONFIG_LITEX_SOC_CONTROLLER) += litex/
 obj-y				+= mediatek/
 obj-y				+= amlogic/
 obj-y				+= qcom/
drivers/soc/litex/Kconfig new file +15 -0
new file mode 100644
index 000000000000..c974ec3846bc
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"
+
+config LITEX_SOC_CONTROLLER
+	tristate "Enable LiteX SoC Controller driver"
+	depends on OF || COMPILE_TEST
+	help
+	  This option enables the SoC Controller Driver which verifies
+	  LiteX CSR access and provides common litex_get_reg/litex_set_reg
+	  accessors.
+	  All drivers that use functions from litex.h must depend on
+	  LITEX_SOC_CONTROLLER.
+
+endmenu
drivers/soc/litex/Makefile new file +3 -0
new file mode 100644
index 000000000000..98ff7325b1c0
@@ -0,0 +1,3 @@
+# SPDX-License_Identifier: GPL-2.0
+
+obj-$(CONFIG_LITEX_SOC_CONTROLLER)	+= litex_soc_ctrl.o
drivers/soc/litex/litex_soc_ctrl.c new file +3 -0
new file mode 100644
index 000000000000..08330c9872b0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LiteX SoC Controller Driver

drivers/soc/litex/litex_soc_ctrl.c line 13

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+ *
+ * Copyright (C) 2020 Antmicro <www.antmicro.com>
+ *
+ */
+
+#include <linux/litex.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>

I'm not seeing anything from this header in use in here yet.

Quoted 25 lines
drivers/soc/litex/litex_soc_ctrl.c new file +25 -0
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+
+/*
+ * The parameters below are true for LiteX SoC
+ * configured for 8-bit CSR Bus, 32-bit aligned.
+ *
+ * Supporting other configurations will require
+ * extending the logic in this header.
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,

drivers/soc/litex/litex_soc_ctrl.c line 48

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */

I would argue in favor of formal kernel-doc for these. Even with this explanation the exact meaning of parameters isn't particularly clear.

Quoted 164 lines
drivers/soc/litex/litex_soc_ctrl.c new file +146 -0
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+		    unsigned long val)
+{
+	unsigned long shifted_data, shift, i;
+	unsigned long flags;
+
+	spin_lock_irqsave(&csr_lock, flags);
+
+	for (i = 0; i < reg_size; ++i) {
+		shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+		shifted_data = val >> shift;
+
+		writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+	}
+
+	spin_unlock_irqrestore(&csr_lock, flags);
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_size)
+{
+	unsigned long shifted_data, shift, i;
+	unsigned long result = 0;
+	unsigned long flags;
+
+	spin_lock_irqsave(&csr_lock, flags);
+
+	for (i = 0; i < reg_size; ++i) {
+		shifted_data = le32_to_cpu((__le32 __force)readl(reg + (LITEX_REG_SIZE * i)));
+
+		shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+		result |= (shifted_data << shift);
+	}
+
+	spin_unlock_irqrestore(&csr_lock, flags);
+
+	return result;
+}
+EXPORT_SYMBOL_GPL(litex_get_reg);
+
+#define SCRATCH_REG_OFF         0x04
+#define SCRATCH_REG_SIZE        4
+#define SCRATCH_REG_VALUE       0x12345678
+#define SCRATCH_TEST_VALUE      0xdeadbeef
+
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+	unsigned long reg;
+
+	reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+	if (reg != SCRATCH_REG_VALUE) {
+		panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+			SCRATCH_REG_VALUE, reg);
+		return -EINVAL;
+	}
+
+	litex_set_reg(reg_addr + SCRATCH_REG_OFF,
+		SCRATCH_REG_SIZE, SCRATCH_TEST_VALUE);
+	reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+	if (reg != SCRATCH_TEST_VALUE) {
+		panic("Scratch register write error! Expected: 0x%x but got: 0x%lx",
+			SCRATCH_TEST_VALUE, reg);
+		return -EINVAL;
+	}
+
+	/* restore original value of the SCRATCH register */
+	litex_set_reg(reg_addr + SCRATCH_REG_OFF,
+		SCRATCH_REG_SIZE, SCRATCH_REG_VALUE);
+
+	pr_info("LiteX SoC Controller driver initialized");
+
+	return 0;
+}
+
+struct litex_soc_ctrl_device {
+	void __iomem *base;
+};
+
+static const struct of_device_id litex_soc_ctrl_of_match[] = {
+	{.compatible = "litex,soc-controller"},
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, litex_soc_ctrl_of_match);
+
+static int litex_soc_ctrl_probe(struct platform_device *pdev)
+{
+	int result;
+	struct device *dev;
+	struct device_node *node;
+	struct litex_soc_ctrl_device *soc_ctrl_dev;
+
+	dev = &pdev->dev;
+	node = dev->of_node;
+	if (!node)
+		return -ENODEV;
+
+	soc_ctrl_dev = devm_kzalloc(dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
+	if (!soc_ctrl_dev)
+		return -ENOMEM;
+
+	soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(soc_ctrl_dev->base))
+		return PTR_ERR(soc_ctrl_dev->base);
+
+	result = litex_check_csr_access(soc_ctrl_dev->base);
+	if (result) {
+		/* LiteX CSRs access is broken which means that
+		 * none of LiteX drivers will most probably
+		 * operate correctly
+		 */
+		WARN(1, "Failed to validate CSR registers, the system is probably broken.\n");
+	}
+
+	return result;
+}
+
+static struct platform_driver litex_soc_ctrl_driver = {
+	.driver = {
+		.name = "litex-soc-controller",
+		.of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
+	},
+	.probe = litex_soc_ctrl_probe,
+};
+
+module_platform_driver(litex_soc_ctrl_driver);
+MODULE_DESCRIPTION("LiteX SoC Controller driver");
+MODULE_AUTHOR("Antmicro <www.antmicro.com>");
+MODULE_LICENSE("GPL v2");
include/linux/litex.h new file +12 -0
new file mode 100644
index 000000000000..72061018c172
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+
+#ifndef _LINUX_LITEX_H

include/linux/litex.h line 22

Quoted 10 lines
include/linux/litex.h new file +10 -0
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
+#include <linux/compiler_types.h>
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);
+

Nitpick. One blank line is almost always enough!

Quoted 2 lines
include/linux/litex.h new file +2 -0
+
+#endif /* _LINUX_LITEX_H */
author maintainer
#7 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

Hi Jonathan,

thanks for your review!

Quoted 9 lines · Jonathan Cameron
Quoted 5 lines · On Wed, 23 Sep 2020 12:09:06 +0200 Mateusz Holenko <[email protected]>…

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

Quoted 8 lines
Quoted 3 lines

A few little things inline, but looks fine in general to me.

Main request is for more formal documentation of parameters to the read and write functions.

You are right, the parameters should be better documented - I'll take a look at that.
There are some other comments that suggest to replace the loop-based generic functions
with a bunch of separate accessors - perhaps going this way would also make it easier to understand
the code.

Quoted 137 lines

Jonathan

Quoted 135 lines

---

Notes:
Changes in v11:
- removed an unnecessary comment left over from previous version
- changed a multi-line comment to comply with the formatting rules
- use WARN instad of BUG on a failed CSR validation

Changes in v10:
- added casting to avoid sparse warnings in the SoC Controller's driver

Changes in v9:
- added exporting of the litex_set_reg/litex_get_reg symbols

Changes in v8:
- removed litex_check_accessors() helper function
- added crashing (BUG) on the failed LiteX CSR access test

No changes in v7.

Changes in v6:
- added dependency on OF || COMPILE_TEST
- used le32_to_cpu(readl(addr)) instead of __raw_readl and writel(cpu_to_le32(value), addr) instead of __raw_writel to take advantage of memory barriers provided by readl/writel

Changes in v5:
- removed helper accessors and used __raw_readl/__raw_writel instead
- fixed checking for errors in litex_soc_ctrl_probe

Changes in v4:
- fixed indent in Kconfig's help section
- fixed copyright header
- changed compatible to "litex,soc-controller"
- simplified litex_soc_ctrl_probe
- removed unnecessary litex_soc_ctrl_remove

This commit has been introduced in v3 of the patchset.

It includes a simplified version of common 'litex.h' header introduced in v2 of the patchset.

 MAINTAINERS                        |   2 +
 drivers/soc/Kconfig                |   1 +
 drivers/soc/Makefile               |   1 +
 drivers/soc/litex/Kconfig          |  15 +++
 drivers/soc/litex/Makefile         |   3 +
 drivers/soc/litex/litex_soc_ctrl.c | 194 +++++++++++++++++++++++++++++
 include/linux/litex.h              |  24 ++++
 7 files changed, 240 insertions(+)
 create mode 100644 drivers/soc/litex/Kconfig
 create mode 100644 drivers/soc/litex/Makefile
 create mode 100644 drivers/soc/litex/litex_soc_ctrl.c
 create mode 100644 include/linux/litex.h
MAINTAINERS +2 -0
index 39be98db7418..4d70a1b22a87 100644
@@ -9840,6 +9840,8 @@ M:      Karol Gugala <[email protected]>
 M:   Mateusz Holenko <[email protected]>
 S:   Maintained
 F:   Documentation/devicetree/bindings/*/litex,*.yaml
+F:   drivers/soc/litex/litex_soc_ctrl.c
+F:   include/linux/litex.h
 
 LIVE PATCHING
 M:   Josh Poimboeuf <[email protected]>
drivers/soc/Kconfig +1 -0
index 425ab6f7e375..d097d070f579 100644
@@ -9,6 +9,7 @@ source "drivers/soc/bcm/Kconfig"
 source "drivers/soc/fsl/Kconfig"
 source "drivers/soc/imx/Kconfig"
 source "drivers/soc/ixp4xx/Kconfig"
+source "drivers/soc/litex/Kconfig"
 source "drivers/soc/mediatek/Kconfig"
 source "drivers/soc/qcom/Kconfig"
 source "drivers/soc/renesas/Kconfig"
drivers/soc/Makefile +1 -0
index 36452bed86ef..0b16108823ef 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_ARCH_GEMINI)   += gemini/
 obj-y                                += imx/
 obj-$(CONFIG_ARCH_IXP4XX)    += ixp4xx/
 obj-$(CONFIG_SOC_XWAY)               += lantiq/
+obj-$(CONFIG_LITEX_SOC_CONTROLLER) += litex/
 obj-y                                += mediatek/
 obj-y                                += amlogic/
 obj-y                                += qcom/
drivers/soc/litex/Kconfig new file +15 -0
new file mode 100644
index 000000000000..c974ec3846bc
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"
+
+config LITEX_SOC_CONTROLLER
+     tristate "Enable LiteX SoC Controller driver"
+     depends on OF || COMPILE_TEST
+     help
+       This option enables the SoC Controller Driver which verifies
+       LiteX CSR access and provides common litex_get_reg/litex_set_reg
+       accessors.
+       All drivers that use functions from litex.h must depend on
+       LITEX_SOC_CONTROLLER.
+
+endmenu
drivers/soc/litex/Makefile new file +3 -0
new file mode 100644
index 000000000000..98ff7325b1c0
@@ -0,0 +1,3 @@
+# SPDX-License_Identifier: GPL-2.0
+
+obj-$(CONFIG_LITEX_SOC_CONTROLLER)   += litex_soc_ctrl.o
drivers/soc/litex/litex_soc_ctrl.c new file +7 -0
new file mode 100644
index 000000000000..08330c9872b0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LiteX SoC Controller Driver
+ *
+ * Copyright (C) 2020 Antmicro <www.antmicro.com>
+ *
+ */
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 13

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+#include <linux/litex.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>

I'm not seeing anything from this header in use in here yet.

I'll check and remove if this is not needed.

Quoted 31 lines
Quoted 31 lines
drivers/soc/litex/litex_soc_ctrl.c new file +31 -0
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+
+/*
+ * The parameters below are true for LiteX SoC
+ * configured for 8-bit CSR Bus, 32-bit aligned.
+ *
+ * Supporting other configurations will require
+ * extending the logic in this header.
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 48

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */

I would argue in favor of formal kernel-doc for these. Even with this explanation the exact meaning of parameters isn't particularly clear.

As stated above, I'll make it clearer - either by adding a formal doc or by splitting this function into several easier-to-understand ones.

Quoted 168 lines
Quoted 168 lines
drivers/soc/litex/litex_soc_ctrl.c new file +146 -0
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                 unsigned long val)
+{
+     unsigned long shifted_data, shift, i;
+     unsigned long flags;
+
+     spin_lock_irqsave(&csr_lock, flags);
+
+     for (i = 0; i < reg_size; ++i) {
+             shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+             shifted_data = val >> shift;
+
+             writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+     }
+
+     spin_unlock_irqrestore(&csr_lock, flags);
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_size)
+{
+     unsigned long shifted_data, shift, i;
+     unsigned long result = 0;
+     unsigned long flags;
+
+     spin_lock_irqsave(&csr_lock, flags);
+
+     for (i = 0; i < reg_size; ++i) {
+             shifted_data = le32_to_cpu((__le32 __force)readl(reg + (LITEX_REG_SIZE * i)));
+
+             shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+             result |= (shifted_data << shift);
+     }
+
+     spin_unlock_irqrestore(&csr_lock, flags);
+
+     return result;
+}
+EXPORT_SYMBOL_GPL(litex_get_reg);
+
+#define SCRATCH_REG_OFF         0x04
+#define SCRATCH_REG_SIZE        4
+#define SCRATCH_REG_VALUE       0x12345678
+#define SCRATCH_TEST_VALUE      0xdeadbeef
+
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+     unsigned long reg;
+
+     reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+     if (reg != SCRATCH_REG_VALUE) {
+             panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                     SCRATCH_REG_VALUE, reg);
+             return -EINVAL;
+     }
+
+     litex_set_reg(reg_addr + SCRATCH_REG_OFF,
+             SCRATCH_REG_SIZE, SCRATCH_TEST_VALUE);
+     reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+     if (reg != SCRATCH_TEST_VALUE) {
+             panic("Scratch register write error! Expected: 0x%x but got: 0x%lx",
+                     SCRATCH_TEST_VALUE, reg);
+             return -EINVAL;
+     }
+
+     /* restore original value of the SCRATCH register */
+     litex_set_reg(reg_addr + SCRATCH_REG_OFF,
+             SCRATCH_REG_SIZE, SCRATCH_REG_VALUE);
+
+     pr_info("LiteX SoC Controller driver initialized");
+
+     return 0;
+}
+
+struct litex_soc_ctrl_device {
+     void __iomem *base;
+};
+
+static const struct of_device_id litex_soc_ctrl_of_match[] = {
+     {.compatible = "litex,soc-controller"},
+     {},
+};
+
+MODULE_DEVICE_TABLE(of, litex_soc_ctrl_of_match);
+
+static int litex_soc_ctrl_probe(struct platform_device *pdev)
+{
+     int result;
+     struct device *dev;
+     struct device_node *node;
+     struct litex_soc_ctrl_device *soc_ctrl_dev;
+
+     dev = &pdev->dev;
+     node = dev->of_node;
+     if (!node)
+             return -ENODEV;
+
+     soc_ctrl_dev = devm_kzalloc(dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
+     if (!soc_ctrl_dev)
+             return -ENOMEM;
+
+     soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(soc_ctrl_dev->base))
+             return PTR_ERR(soc_ctrl_dev->base);
+
+     result = litex_check_csr_access(soc_ctrl_dev->base);
+     if (result) {
+             /* LiteX CSRs access is broken which means that
+              * none of LiteX drivers will most probably
+              * operate correctly
+              */
+             WARN(1, "Failed to validate CSR registers, the system is probably broken.\n");
+     }
+
+     return result;
+}
+
+static struct platform_driver litex_soc_ctrl_driver = {
+     .driver = {
+             .name = "litex-soc-controller",
+             .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
+     },
+     .probe = litex_soc_ctrl_probe,
+};
+
+module_platform_driver(litex_soc_ctrl_driver);
+MODULE_DESCRIPTION("LiteX SoC Controller driver");
+MODULE_AUTHOR("Antmicro <www.antmicro.com>");
+MODULE_LICENSE("GPL v2");
include/linux/litex.h new file +16 -0
new file mode 100644
index 000000000000..72061018c172
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+
+#ifndef _LINUX_LITEX_H
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
Quoted 8 lines

include/linux/litex.h line 22

Quoted 6 lines
include/linux/litex.h new file +6 -0
+#include <linux/compiler_types.h>
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);
+

Nitpick. One blank line is almost always enough!

Right, one blank line ought to be enough for anyone.

Quoted 4 lines

include/linux/litex.h line 24

Quoted 2 lines
include/linux/litex.h new file +2 -0
+
+#endif /* _LINUX_LITEX_H */

Best regards,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland

#8 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

In reply to Mateusz Holenko

Hi Mateusz,

Quoted 8 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

Thanks for your patch!

Quoted 6 lines
drivers/soc/litex/Kconfig new file +3 -0
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"

drivers/soc/litex/Kconfig line 13

Quoted 10 lines
drivers/soc/litex/Kconfig new file +10 -0
+
+config LITEX_SOC_CONTROLLER
+       tristate "Enable LiteX SoC Controller driver"
+       depends on OF || COMPILE_TEST
+       help
+         This option enables the SoC Controller Driver which verifies
+         LiteX CSR access and provides common litex_get_reg/litex_set_reg
+         accessors.
+         All drivers that use functions from litex.h must depend on
+         LITEX_SOC_CONTROLLER.

I'm wondering if it makes sense to have them depend on a "simpler" symbol instead, e.g. LITEX?

Currently the SoC controller is limited to I/O accessors and a simple register compatibility check, but you may want to extend it with more features later, so you probably want to keep the LITEX_SOC_CONTROLLER. Hence you could add

config LITEX
bool

and let LITEX_SOC_CONTROLLER select LITEX.

Quoted 14 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LiteX SoC Controller Driver
+ *
+ * Copyright (C) 2020 Antmicro <www.antmicro.com>
+ *
+ */
+
+#include <linux/litex.h>
+#include <linux/device.h>
+#include <linux/errno.h>

drivers/soc/litex/litex_soc_ctrl.c line 21

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+
+/*
+ * The parameters below are true for LiteX SoC

SoCs

drivers/soc/litex/litex_soc_ctrl.c line 25

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+ * configured for 8-bit CSR Bus, 32-bit aligned.
+ *
+ * Supporting other configurations will require
+ * extending the logic in this header.

This is no longer a header file.

Quoted 31 lines
drivers/soc/litex/litex_soc_ctrl.c new file +31 -0
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+

drivers/soc/litex/litex_soc_ctrl.c line 66

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

The spinlock access will probably become the source of lock contention later, especially when considering SMP variants.

Quoted 14 lines
drivers/soc/litex/litex_soc_ctrl.c new file +14 -0
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.

drivers/soc/litex/litex_soc_ctrl.c line 117

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+       unsigned long reg;
+
+       reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+       if (reg != SCRATCH_REG_VALUE) {
+               panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                       SCRATCH_REG_VALUE, reg);

Do you think the user will ever see this panic message? (see below)

drivers/soc/litex/litex_soc_ctrl.c line 91

Quoted 1 line
drivers/soc/litex/litex_soc_ctrl.c new file +1 -0
+               return -EINVAL;

Good ;-) All of BUG()/WARN()/panic() may be compiled out, depending on config options, so the system may continue running beyond the panic() call.

drivers/soc/litex/litex_soc_ctrl.c line 161

Quoted 11 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
+static int litex_soc_ctrl_probe(struct platform_device *pdev)
+{
+       int result;
+       struct device *dev;
+       struct device_node *node;
+       struct litex_soc_ctrl_device *soc_ctrl_dev;
+
+       dev = &pdev->dev;
+       node = dev->of_node;
+       if (!node)
+               return -ENODEV;

FYI, this cannot happen.

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       soc_ctrl_dev = devm_kzalloc(dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
+       if (!soc_ctrl_dev)
+               return -ENOMEM;
+
+       soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);

drivers/soc/litex/litex_soc_ctrl.c line 177

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+       if (IS_ERR(soc_ctrl_dev->base))
+               return PTR_ERR(soc_ctrl_dev->base);
+
+       result = litex_check_csr_access(soc_ctrl_dev->base);
+       if (result) {
+               /* LiteX CSRs access is broken which means that
+                * none of LiteX drivers will most probably
+                * operate correctly
+                */
+               WARN(1, "Failed to validate CSR registers, the system is probably broken.\n");

WARN(result, ...)

But is this WARN() needed? You have already called panic() before.

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+       }
+
+       return result;
+}

drivers/soc/litex/litex_soc_ctrl.c line 191

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+
+static struct platform_driver litex_soc_ctrl_driver = {
+       .driver = {
+               .name = "litex-soc-controller",
+               .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
+       },
+       .probe = litex_soc_ctrl_probe,
+};
+
+module_platform_driver(litex_soc_ctrl_driver);

module_platform_driver() means this driver is probed quite late in the boot sequence. Currently the only other LiteX driver is liteuart, which is probed at more or less the same time, but I can envision more early drivers to be added later (typically interrupt/clock controllers and timers not integrated into the main CPU core).
Note that even liteuart will run earlier, and thus access CSR registers before the check has run, when using e.g. earlycon...

Quoted 14 lines
include/linux/litex.h new file +11 -0
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+

include/linux/litex.h line 21

Quoted 10 lines
include/linux/litex.h new file +10 -0
+#ifndef _LINUX_LITEX_H
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
+#include <linux/compiler_types.h>
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);

Perhaps you can add static inline litex_{read,write}{8,16,32}() wrappers, so drivers don't have to pass the reg_sz parameter explicitly, and to make it look more like accessors of other bus types?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#9 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

Hi Geert, Mateusz,

Quoted 117 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 8 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

Thanks for your patch!

Quoted 6 lines
drivers/soc/litex/Kconfig new file +3 -0
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"

drivers/soc/litex/Kconfig line 13

Quoted 10 lines
drivers/soc/litex/Kconfig new file +10 -0
+
+config LITEX_SOC_CONTROLLER
+       tristate "Enable LiteX SoC Controller driver"
+       depends on OF || COMPILE_TEST
+       help
+         This option enables the SoC Controller Driver which verifies
+         LiteX CSR access and provides common litex_get_reg/litex_set_reg
+         accessors.
+         All drivers that use functions from litex.h must depend on
+         LITEX_SOC_CONTROLLER.

I'm wondering if it makes sense to have them depend on a "simpler" symbol instead, e.g. LITEX?

Currently the SoC controller is limited to I/O accessors and a simple register compatibility check, but you may want to extend it with more features later, so you probably want to keep the LITEX_SOC_CONTROLLER. Hence you could add

config LITEX
bool

and let LITEX_SOC_CONTROLLER select LITEX.

Quoted 14 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LiteX SoC Controller Driver
+ *
+ * Copyright (C) 2020 Antmicro <www.antmicro.com>
+ *
+ */
+
+#include <linux/litex.h>
+#include <linux/device.h>
+#include <linux/errno.h>

drivers/soc/litex/litex_soc_ctrl.c line 21

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+
+/*
+ * The parameters below are true for LiteX SoC

SoCs

drivers/soc/litex/litex_soc_ctrl.c line 25

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+ * configured for 8-bit CSR Bus, 32-bit aligned.
+ *
+ * Supporting other configurations will require
+ * extending the logic in this header.

This is no longer a header file.

Quoted 39 lines
drivers/soc/litex/litex_soc_ctrl.c new file +39 -0
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 66

Quoted 2 lines
drivers/soc/litex/litex_soc_ctrl.c new file +2 -0
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

There's been talk within the LiteX dev community to standardize on a LITEX_SUBREG_SIZE of 0x4 (i.e., using all 32 bits of a 32-bit (LITEX_REG_SIZE) aligned MMIO location). Early 32-bit (vexriscv based) Linux capable LiteX designs started out with only the 8 LSBits used within a 32-bit MMIO location, but 64-bit (Rocket chip) based LiteX SoCs use 4-byte aligned, fully populated MMIO registers (i.e., both LITEX_SUBREG_SIZE *and* LITEX_REG_SIZE are 4). There's also been talk of deprecating LITEX_SUBREG_SIZE == 0x1 for "linux-capable LiteX builds", but nothing definitive yet AFAIK.

As long as adding LITEX_SUBREG_SIZE 0x4 (either as a config option, or as a hard-coded default in a subsequent version) won't break things, we should be safe going forward afaict.

Geert: note that LiteX has wider-than-32-bit registers spread across multiple 32-bit aligned, 8- or 32-bit wide "subregisters", so looping and shifting will still be necessary, even with LITEX_SUBREG_SIZE 0x4.

Quoted 114 lines

The spinlock access will probably become the source of lock contention later, especially when considering SMP variants.

Quoted 14 lines
drivers/soc/litex/litex_soc_ctrl.c new file +14 -0
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.

drivers/soc/litex/litex_soc_ctrl.c line 117

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+       unsigned long reg;
+
+       reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+       if (reg != SCRATCH_REG_VALUE) {
+               panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                       SCRATCH_REG_VALUE, reg);

Do you think the user will ever see this panic message? (see below)

drivers/soc/litex/litex_soc_ctrl.c line 91

Quoted 1 line
drivers/soc/litex/litex_soc_ctrl.c new file +1 -0
+               return -EINVAL;

Good ;-) All of BUG()/WARN()/panic() may be compiled out, depending on config options, so the system may continue running beyond the panic() call.

drivers/soc/litex/litex_soc_ctrl.c line 161

Quoted 11 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
+static int litex_soc_ctrl_probe(struct platform_device *pdev)
+{
+       int result;
+       struct device *dev;
+       struct device_node *node;
+       struct litex_soc_ctrl_device *soc_ctrl_dev;
+
+       dev = &pdev->dev;
+       node = dev->of_node;
+       if (!node)
+               return -ENODEV;

FYI, this cannot happen.

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       soc_ctrl_dev = devm_kzalloc(dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
+       if (!soc_ctrl_dev)
+               return -ENOMEM;
+
+       soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);

drivers/soc/litex/litex_soc_ctrl.c line 177

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+       if (IS_ERR(soc_ctrl_dev->base))
+               return PTR_ERR(soc_ctrl_dev->base);
+
+       result = litex_check_csr_access(soc_ctrl_dev->base);
+       if (result) {
+               /* LiteX CSRs access is broken which means that
+                * none of LiteX drivers will most probably
+                * operate correctly
+                */
+               WARN(1, "Failed to validate CSR registers, the system is probably broken.\n");

WARN(result, ...)

But is this WARN() needed? You have already called panic() before.

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+       }
+
+       return result;
+}

drivers/soc/litex/litex_soc_ctrl.c line 191

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+
+static struct platform_driver litex_soc_ctrl_driver = {
+       .driver = {
+               .name = "litex-soc-controller",
+               .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
+       },
+       .probe = litex_soc_ctrl_probe,
+};
+
+module_platform_driver(litex_soc_ctrl_driver);

module_platform_driver() means this driver is probed quite late in the boot sequence. Currently the only other LiteX driver is liteuart, which is probed at more or less the same time, but I can envision more early drivers to be added later (typically interrupt/clock controllers and timers not integrated into the main CPU core).
Note that even liteuart will run earlier, and thus access CSR registers before the check has run, when using e.g. earlycon...

Quoted 20 lines
include/linux/litex.h new file +17 -0
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+
+#ifndef _LINUX_LITEX_H
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
+#include <linux/compiler_types.h>
Quoted 8 lines

include/linux/litex.h line 21

Quoted 4 lines
include/linux/litex.h new file +4 -0
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);

Perhaps you can add static inline litex_{read,write}{8,16,32}() wrappers, so drivers don't have to pass the reg_sz parameter explicitly, and to make it look more like accessors of other bus types?

Seconded -- perhaps simply cut'n'paste and/or adapt from
https://github.com/litex-hub/linux/blob/litex-rocket-rebase/include/linux/litex.h#L78
(from the 64-bit port of the LiteX linux patch set)

Cheers,
--Gabriel

#10 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

Hi Gabriel,

Quoted 50 lines · Gabriel L. Somlo
Quoted 40 lines · Geert Uytterhoeven
Quoted 39 lines · Mateusz Holenko

+ */

drivers/soc/litex/litex_soc_ctrl.c new file +38 -0
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

Quoted 9 lines

There's been talk within the LiteX dev community to standardize on a LITEX_SUBREG_SIZE of 0x4 (i.e., using all 32 bits of a 32-bit (LITEX_REG_SIZE) aligned MMIO location). Early 32-bit (vexriscv based) Linux capable LiteX designs started out with only the 8 LSBits used within a 32-bit MMIO location, but 64-bit (Rocket chip) based LiteX SoCs use 4-byte aligned, fully populated MMIO registers (i.e., both LITEX_SUBREG_SIZE *and* LITEX_REG_SIZE are 4). There's also been talk of deprecating LITEX_SUBREG_SIZE == 0x1 for "linux-capable LiteX builds", but nothing definitive yet AFAIK.

That sounds like a good idea to me.
Having 8-bit accesses may be worthwhile on a small microcontroller, but a full-fledge Linux system can use more and wider MMIO.

Quoted 3 lines

Geert: note that LiteX has wider-than-32-bit registers spread across multiple 32-bit aligned, 8- or 32-bit wide "subregisters", so looping and shifting will still be necessary, even with LITEX_SUBREG_SIZE 0x4.

Can these be different than 64-bit (and 128-bit)?
That's not unlike accessors on other 32-bit platforms. Still, no loop needed, just doing two (or four) 32-bit accesses in a row is fine (but requires using inlines instead of your current single out-of-line function).

Quoted 11 lines
Quoted 10 lines

include/linux/litex.h line 21

Quoted 6 lines
include/linux/litex.h new file +3 -0
 
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);

Perhaps you can add static inline litex_{read,write}{8,16,32}() wrappers, so drivers don't have to pass the reg_sz parameter explicitly, and to make it look more like accessors of other bus types?

Quoted 3 lines

Seconded -- perhaps simply cut'n'paste and/or adapt from
https://github.com/litex-hub/linux/blob/litex-rocket-rebase/include/linux/litex.h#L78
(from the 64-bit port of the LiteX linux patch set)

Yes, you definitely want the 32-bit and 64-bit ports to agree ;-) Note that these are using the "old" "bwlq" convention (with "l" predating 64-bit long on 64-bit platforms) instead of the more modern explicit {8,16,32,64}, but that's a minor detail.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author maintainer
#11 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

In reply to Gabriel L. Somlo

Hi Gabriel,

Quoted 130 lines · Gabriel L. Somlo

Hi Geert, Mateusz,

Quoted 117 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 8 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

Thanks for your patch!

Quoted 6 lines
drivers/soc/litex/Kconfig new file +3 -0
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"

drivers/soc/litex/Kconfig line 13

Quoted 10 lines
drivers/soc/litex/Kconfig new file +10 -0
+
+config LITEX_SOC_CONTROLLER
+       tristate "Enable LiteX SoC Controller driver"
+       depends on OF || COMPILE_TEST
+       help
+         This option enables the SoC Controller Driver which verifies
+         LiteX CSR access and provides common litex_get_reg/litex_set_reg
+         accessors.
+         All drivers that use functions from litex.h must depend on
+         LITEX_SOC_CONTROLLER.

I'm wondering if it makes sense to have them depend on a "simpler" symbol instead, e.g. LITEX?

Currently the SoC controller is limited to I/O accessors and a simple register compatibility check, but you may want to extend it with more features later, so you probably want to keep the LITEX_SOC_CONTROLLER. Hence you could add

config LITEX
bool

and let LITEX_SOC_CONTROLLER select LITEX.

Quoted 14 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LiteX SoC Controller Driver
+ *
+ * Copyright (C) 2020 Antmicro <www.antmicro.com>
+ *
+ */
+
+#include <linux/litex.h>
+#include <linux/device.h>
+#include <linux/errno.h>

drivers/soc/litex/litex_soc_ctrl.c line 21

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+
+/*
+ * The parameters below are true for LiteX SoC

SoCs

drivers/soc/litex/litex_soc_ctrl.c line 25

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+ * configured for 8-bit CSR Bus, 32-bit aligned.
+ *
+ * Supporting other configurations will require
+ * extending the logic in this header.

This is no longer a header file.

Quoted 39 lines
drivers/soc/litex/litex_soc_ctrl.c new file +39 -0
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 66

Quoted 2 lines
drivers/soc/litex/litex_soc_ctrl.c new file +2 -0
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

Quoted 9 lines

There's been talk within the LiteX dev community to standardize on a LITEX_SUBREG_SIZE of 0x4 (i.e., using all 32 bits of a 32-bit (LITEX_REG_SIZE) aligned MMIO location). Early 32-bit (vexriscv based) Linux capable LiteX designs started out with only the 8 LSBits used within a 32-bit MMIO location, but 64-bit (Rocket chip) based LiteX SoCs use 4-byte aligned, fully populated MMIO registers (i.e., both LITEX_SUBREG_SIZE *and* LITEX_REG_SIZE are 4). There's also been talk of deprecating LITEX_SUBREG_SIZE == 0x1 for "linux-capable LiteX builds", but nothing definitive yet AFAIK.

I agree that having LITEX_SUBREG_SIZE equal to 4 would make the code much simpler.

I believe, however, that at the moment LiteX (for 32-bit CPUs) and both main 32-bit Linux capable LiteX-based platforms
(https://github.com/litex-hub/linux-on-litex-vexriscv and
https://github.com/timvideos/litex-buildenv) by default generate SoC with 8-bit data width and that's why the driver currently targets this configuration.

Quoted 3 lines

As long as adding LITEX_SUBREG_SIZE 0x4 (either as a config option, or as a hard-coded default in a subsequent version) won't break things, we should be safe going forward afaict.

I'm totally open for extending/changing the default LITEX_SUBREG_SIZE value for this driver once the default configuration for LiteX platforms changes.

Quoted 3 lines

Geert: note that LiteX has wider-than-32-bit registers spread across multiple 32-bit aligned, 8- or 32-bit wide "subregisters", so looping and shifting will still be necessary, even with LITEX_SUBREG_SIZE 0x4.

There are also situations (like the GPIO controller) where the layout of registers is dynamic and depends on the LiteX configuration (number of supported pins).
In this situation we need to read the configuration from DT and keep the driver flexible by using dynamic, loop-based CSR accessors.

That's why I believe we could have both - fast, non-loop based accessors for common registers widths 8/32/64/etc. and the current implementation where runtime flexibility is required (at the cost of performance).

Quoted 123 lines
Quoted 114 lines

The spinlock access will probably become the source of lock contention later, especially when considering SMP variants.

Quoted 14 lines
drivers/soc/litex/litex_soc_ctrl.c new file +14 -0
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.

drivers/soc/litex/litex_soc_ctrl.c line 117

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+       unsigned long reg;
+
+       reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+       if (reg != SCRATCH_REG_VALUE) {
+               panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                       SCRATCH_REG_VALUE, reg);

Do you think the user will ever see this panic message? (see below)

drivers/soc/litex/litex_soc_ctrl.c line 91

Quoted 1 line
drivers/soc/litex/litex_soc_ctrl.c new file +1 -0
+               return -EINVAL;

Good ;-) All of BUG()/WARN()/panic() may be compiled out, depending on config options, so the system may continue running beyond the panic() call.

drivers/soc/litex/litex_soc_ctrl.c line 161

Quoted 11 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
+static int litex_soc_ctrl_probe(struct platform_device *pdev)
+{
+       int result;
+       struct device *dev;
+       struct device_node *node;
+       struct litex_soc_ctrl_device *soc_ctrl_dev;
+
+       dev = &pdev->dev;
+       node = dev->of_node;
+       if (!node)
+               return -ENODEV;

FYI, this cannot happen.

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       soc_ctrl_dev = devm_kzalloc(dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
+       if (!soc_ctrl_dev)
+               return -ENOMEM;
+
+       soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);

drivers/soc/litex/litex_soc_ctrl.c line 177

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+       if (IS_ERR(soc_ctrl_dev->base))
+               return PTR_ERR(soc_ctrl_dev->base);
+
+       result = litex_check_csr_access(soc_ctrl_dev->base);
+       if (result) {
+               /* LiteX CSRs access is broken which means that
+                * none of LiteX drivers will most probably
+                * operate correctly
+                */
+               WARN(1, "Failed to validate CSR registers, the system is probably broken.\n");

WARN(result, ...)

But is this WARN() needed? You have already called panic() before.

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+       }
+
+       return result;
+}

drivers/soc/litex/litex_soc_ctrl.c line 191

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+
+static struct platform_driver litex_soc_ctrl_driver = {
+       .driver = {
+               .name = "litex-soc-controller",
+               .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
+       },
+       .probe = litex_soc_ctrl_probe,
+};
+
+module_platform_driver(litex_soc_ctrl_driver);

module_platform_driver() means this driver is probed quite late in the boot sequence. Currently the only other LiteX driver is liteuart, which is probed at more or less the same time, but I can envision more early drivers to be added later (typically interrupt/clock controllers and timers not integrated into the main CPU core).
Note that even liteuart will run earlier, and thus access CSR registers before the check has run, when using e.g. earlycon...

Quoted 20 lines
include/linux/litex.h new file +17 -0
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+
+#ifndef _LINUX_LITEX_H
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
+#include <linux/compiler_types.h>
Quoted 8 lines

include/linux/litex.h line 21

Quoted 4 lines
include/linux/litex.h new file +4 -0
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);

Perhaps you can add static inline litex_{read,write}{8,16,32}() wrappers, so drivers don't have to pass the reg_sz parameter explicitly, and to make it look more like accessors of other bus types?

Quoted 6 lines

Seconded -- perhaps simply cut'n'paste and/or adapt from
https://github.com/litex-hub/linux/blob/litex-rocket-rebase/include/linux/litex.h#L78
(from the 64-bit port of the LiteX linux patch set)

Cheers,
--Gabriel

Best regards,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland

author maintainer
#12 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

In reply to Geert Uytterhoeven

Hi Geert,

Quoted 6 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 2 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

Quoted 8 lines
Quoted 6 lines

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

Thanks for your patch!

Thanks for your review!

Quoted 25 lines
Quoted 6 lines
drivers/soc/litex/Kconfig new file +3 -0
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"

drivers/soc/litex/Kconfig line 13

Quoted 10 lines
drivers/soc/litex/Kconfig new file +10 -0
+
+config LITEX_SOC_CONTROLLER
+       tristate "Enable LiteX SoC Controller driver"
+       depends on OF || COMPILE_TEST
+       help
+         This option enables the SoC Controller Driver which verifies
+         LiteX CSR access and provides common litex_get_reg/litex_set_reg
+         accessors.
+         All drivers that use functions from litex.h must depend on
+         LITEX_SOC_CONTROLLER.

I'm wondering if it makes sense to have them depend on a "simpler" symbol instead, e.g. LITEX?

Currently the SoC controller is limited to I/O accessors and a simple register compatibility check, but you may want to extend it with more features later, so you probably want to keep the LITEX_SOC_CONTROLLER. Hence you could add

Quoted 4 lines

config LITEX
bool

and let LITEX_SOC_CONTROLLER select LITEX.

But then if other drivers depend just on LITEX, it would not automatically mean that the LITEX_SOC_CONTROLLER is selected, right?. And if it's not selected litex_{g,s}et_reg() are not available and the compilation would fail.

I could move the implementation of those functions directly to the litex.h header
and avoid this KConfig dependency, but I'm not sure if they are not too big to become a static inline.
What do you think?

Quoted 18 lines
Quoted 18 lines
drivers/soc/litex/litex_soc_ctrl.c new file +15 -0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LiteX SoC Controller Driver
+ *
+ * Copyright (C) 2020 Antmicro <www.antmicro.com>
+ *
+ */
+
+#include <linux/litex.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 21

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+
+/*
+ * The parameters below are true for LiteX SoC

SoCs

Right, I will fix that.

Quoted 6 lines

drivers/soc/litex/litex_soc_ctrl.c line 25

Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+ * configured for 8-bit CSR Bus, 32-bit aligned.
+ *
+ * Supporting other configurations will require
+ * extending the logic in this header.

This is no longer a header file.

It's not - you are correct. I will rephrase it to "extending the logic in this file".

Quoted 39 lines
Quoted 39 lines
drivers/soc/litex/litex_soc_ctrl.c new file +39 -0
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 66

Quoted 2 lines
drivers/soc/litex/litex_soc_ctrl.c new file +2 -0
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

Since the amount of possible reg_size is practically limited we could add explicit 8/32/64/128 accessors to eliminate loops.
As for multiple writel/readl I don't really see an option to avoid them for the 8-bit bus width.

Quoted 2 lines

The spinlock access will probably become the source of lock contention later, especially when considering SMP variants.

Do you have any suggestions on how to handle this?
Dropping locks could lead to the situation when two cores write at the same time leaving a wrong (mixed) value in the CSR.

Quoted 18 lines
Quoted 18 lines
drivers/soc/litex/litex_soc_ctrl.c new file +18 -0
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+       unsigned long reg;
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 117

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+       if (reg != SCRATCH_REG_VALUE) {
+               panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                       SCRATCH_REG_VALUE, reg);

Do you think the user will ever see this panic message? (see below)

On UART most probably not, as broken CSRs mean broken UART driver as well. But I believe you can retrieve logs from the memory and analyze them post-mortem, isn't that right?

Quoted 6 lines

drivers/soc/litex/litex_soc_ctrl.c line 91

Quoted 1 line
drivers/soc/litex/litex_soc_ctrl.c new file +1 -0
+               return -EINVAL;

Good ;-) All of BUG()/WARN()/panic() may be compiled out, depending on config options, so the system may continue running beyond the panic() call.

Quoted 13 lines

drivers/soc/litex/litex_soc_ctrl.c line 161

Quoted 11 lines
drivers/soc/litex/litex_soc_ctrl.c new file +11 -0
+static int litex_soc_ctrl_probe(struct platform_device *pdev)
+{
+       int result;
+       struct device *dev;
+       struct device_node *node;
+       struct litex_soc_ctrl_device *soc_ctrl_dev;
+
+       dev = &pdev->dev;
+       node = dev->of_node;
+       if (!node)
+               return -ENODEV;

FYI, this cannot happen.

Right, this check is not necessary - I will remove it.

Quoted 19 lines
Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       soc_ctrl_dev = devm_kzalloc(dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
+       if (!soc_ctrl_dev)
+               return -ENOMEM;
+
+       soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);

drivers/soc/litex/litex_soc_ctrl.c line 177

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+       if (IS_ERR(soc_ctrl_dev->base))
+               return PTR_ERR(soc_ctrl_dev->base);
+
+       result = litex_check_csr_access(soc_ctrl_dev->base);
+       if (result) {
+               /* LiteX CSRs access is broken which means that
+                * none of LiteX drivers will most probably
+                * operate correctly
+                */
+               WARN(1, "Failed to validate CSR registers, the system is probably broken.\n");

WARN(result, ...)

Quoted 1 line

But is this WARN() needed? You have already called panic() before.

You are right - in both cases (read/write) we call panic before returning a non-0 value from litex_check_csr_access().
In this context additional WARN is probably not necessary. I'll extend the panic message to indicate that read/write failure means a broken system and drop this WARN.

Quoted 15 lines
Quoted 4 lines
drivers/soc/litex/litex_soc_ctrl.c new file +4 -0
+       }
+
+       return result;
+}

drivers/soc/litex/litex_soc_ctrl.c line 191

Quoted 10 lines
drivers/soc/litex/litex_soc_ctrl.c new file +10 -0
+
+static struct platform_driver litex_soc_ctrl_driver = {
+       .driver = {
+               .name = "litex-soc-controller",
+               .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
+       },
+       .probe = litex_soc_ctrl_probe,
+};
+
+module_platform_driver(litex_soc_ctrl_driver);
Quoted 7 lines

module_platform_driver() means this driver is probed quite late in the boot sequence. Currently the only other LiteX driver is liteuart, which is probed at more or less the same time, but I can envision more early drivers to be added later (typically interrupt/clock controllers and timers not integrated into the main CPU core).
Note that even liteuart will run earlier, and thus access CSR registers before the check has run, when using e.g. earlycon...

Yes, this is an interesting point.
We have already covered it in some previous version of the patchset by testing if the LiteX SoC Controller driver was probed and returning -EDEFER in the UART driver otherwise
(to make sure CSR access is verified before UART uses it).
With this approach, however, all future LiteX drivers would have to follow the pattern and we would require the LiteX SoC Controller to be part of the system (which is not the case for the Microwatt platform for example - it uses just an eth core from LiteX).

That's why we finally decided to go with the relaxed version - where we allow the SoC controller driver not to be the first LiteX driver to be probed (and possibly detect a problem after other drivers encounter it).

Quoted 20 lines
Quoted 20 lines
include/linux/litex.h new file +17 -0
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Common LiteX header providing
+ * helper functions for accessing CSRs.
+ *
+ * Implementation of the functions is provided by
+ * the LiteX SoC Controller driver.
+ *
+ * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
+ */
+
+#ifndef _LINUX_LITEX_H
+#define _LINUX_LITEX_H
+
+#include <linux/io.h>
+#include <linux/types.h>
+#include <linux/compiler_types.h>
Quoted 8 lines

include/linux/litex.h line 21

Quoted 4 lines
include/linux/litex.h new file +4 -0
+
+void litex_set_reg(void __iomem *reg, unsigned long reg_sz, unsigned long val);
+
+unsigned long litex_get_reg(void __iomem *reg, unsigned long reg_sz);

Perhaps you can add static inline litex_{read,write}{8,16,32}() wrappers, so drivers don't have to pass the reg_sz parameter explicitly, and to make it look more like accessors of other bus types?

Yes, this is definitely doable.

Quoted 10 lines

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Best regards,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland

#13 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

Hi Mateusz,

Quoted 36 lines · Mateusz Holenko
Quoted 35 lines · Geert Uytterhoeven
Quoted 15 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

drivers/soc/litex/Kconfig new file +3 -0
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"

drivers/soc/litex/Kconfig line 13

Quoted 10 lines
drivers/soc/litex/Kconfig new file +10 -0
+
+config LITEX_SOC_CONTROLLER
+       tristate "Enable LiteX SoC Controller driver"
+       depends on OF || COMPILE_TEST
+       help
+         This option enables the SoC Controller Driver which verifies
+         LiteX CSR access and provides common litex_get_reg/litex_set_reg
+         accessors.
+         All drivers that use functions from litex.h must depend on
+         LITEX_SOC_CONTROLLER.

I'm wondering if it makes sense to have them depend on a "simpler" symbol instead, e.g. LITEX?

Currently the SoC controller is limited to I/O accessors and a simple register compatibility check, but you may want to extend it with more features later, so you probably want to keep the LITEX_SOC_CONTROLLER. Hence you could add

Quoted 8 lines
Quoted 4 lines

config LITEX
bool

and let LITEX_SOC_CONTROLLER select LITEX.

But then if other drivers depend just on LITEX, it would not automatically mean that the LITEX_SOC_CONTROLLER is selected, right?. And if it's not selected litex_{g,s}et_reg() are not available and the compilation would fail.

As the LITEX config symbol above uses plain "bool", without a description, it is invisible. Hence it cannot be enabled by the user, only be selected by other symbols.
If LITEX_SOC_CONTROLLER is the only symbol selecting LITEX, the dependency is met.

Quoted 5 lines

I could move the implementation of those functions directly to the litex.h header
and avoid this KConfig dependency, but I'm not sure if they are not too big to become a static inline.
What do you think?

With the spinlock and the loop, they're too large to be inlined, IMHO.

Quoted 51 lines
Quoted 42 lines
Quoted 42 lines
drivers/soc/litex/litex_soc_ctrl.c new file +39 -0
 
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c

Quoted 2 lines
drivers/soc/litex/litex_soc_ctrl.c new file +2 -0
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

Quoted 2 lines

Since the amount of possible reg_size is practically limited we could add explicit 8/32/64/128 accessors to eliminate loops.

Good.

(assuming 32-bit physical reg accesses below)

Quoted 2 lines

As for multiple writel/readl I don't really see an option to avoid them for the 8-bit bus width.

Sure, 64-bit register accesses consist of two 32-bit accesses on other 32-bit platforms, too.

Quoted 6 lines
Quoted 2 lines

The spinlock access will probably become the source of lock contention later, especially when considering SMP variants.

Do you have any suggestions on how to handle this?
Dropping locks could lead to the situation when two cores write at the same time leaving a wrong (mixed) value in the CSR.

Is this due to the CSR bus or due to the CSR register?
I mean can two 64-bit accesses to different CSR registers be done as four interleaved 32-bit accesses, or must they not be interleaved?

If (hopefully) they can be interleaved, you just need serialization of accesses to the same 64-bit register. As the same register is usually not accessed from multiple drivers, you can handle the serialization inside the driver, if it can ever happen at all (e.g. main driver operation and interrupt handler accessing the same register). That avoids the need for the spinlock in the generic register accessors.

If they must not be interleaved, you indeed need serialization at the bus level, but only for the 64-bit accesses? And I would strongly suggest to look into changing the CSR bus behavior at the hardware level, if possible...

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
drivers/soc/litex/litex_soc_ctrl.c new file +18 -0
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+       unsigned long reg;
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 117

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+       if (reg != SCRATCH_REG_VALUE) {
+               panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                       SCRATCH_REG_VALUE, reg);

Do you think the user will ever see this panic message? (see below)

Quoted 3 lines

On UART most probably not, as broken CSRs mean broken UART driver as well. But I believe you can retrieve logs from the memory and analyze them post-mortem, isn't that right?

Sure. Been there, done that ;-)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#14 lore raw

Re: [PATCH v11 3/5] drivers/soc/litex: add LiteX SoC Controller driver

Quoted 55 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 36 lines · Mateusz Holenko
Quoted 35 lines · Geert Uytterhoeven
Quoted 15 lines · Mateusz Holenko

From: Pawel Czarnecki <[email protected]>

This commit adds driver for the FPGA-based LiteX SoC Controller from LiteX SoC builder.

drivers/soc/litex/Kconfig new file +3 -0
@@ -0,0 +1,15 @@
+# SPDX-License_Identifier: GPL-2.0
+
+menu "Enable LiteX SoC Builder specific drivers"

drivers/soc/litex/Kconfig line 13

Quoted 10 lines
drivers/soc/litex/Kconfig new file +10 -0
+
+config LITEX_SOC_CONTROLLER
+       tristate "Enable LiteX SoC Controller driver"
+       depends on OF || COMPILE_TEST
+       help
+         This option enables the SoC Controller Driver which verifies
+         LiteX CSR access and provides common litex_get_reg/litex_set_reg
+         accessors.
+         All drivers that use functions from litex.h must depend on
+         LITEX_SOC_CONTROLLER.

I'm wondering if it makes sense to have them depend on a "simpler" symbol instead, e.g. LITEX?

Currently the SoC controller is limited to I/O accessors and a simple register compatibility check, but you may want to extend it with more features later, so you probably want to keep the LITEX_SOC_CONTROLLER. Hence you could add

Quoted 8 lines
Quoted 4 lines

config LITEX
bool

and let LITEX_SOC_CONTROLLER select LITEX.

But then if other drivers depend just on LITEX, it would not automatically mean that the LITEX_SOC_CONTROLLER is selected, right?. And if it's not selected litex_{g,s}et_reg() are not available and the compilation would fail.

As the LITEX config symbol above uses plain "bool", without a description, it is invisible. Hence it cannot be enabled by the user, only be selected by other symbols.
If LITEX_SOC_CONTROLLER is the only symbol selecting LITEX, the dependency is met.

Quoted 7 lines
Quoted 5 lines

I could move the implementation of those functions directly to the litex.h header
and avoid this KConfig dependency, but I'm not sure if they are not too big to become a static inline.
What do you think?

With the spinlock and the loop, they're too large to be inlined, IMHO.

Sure.

Quoted 75 lines
Quoted 51 lines
Quoted 42 lines
Quoted 42 lines
drivers/soc/litex/litex_soc_ctrl.c new file +39 -0
 
+ */
+#define LITEX_REG_SIZE             0x4
+#define LITEX_SUBREG_SIZE          0x1
+#define LITEX_SUBREG_SIZE_BIT      (LITEX_SUBREG_SIZE * 8)
+
+static DEFINE_SPINLOCK(csr_lock);
+
+/*
+ * LiteX SoC Generator, depending on the configuration,
+ * can split a single logical CSR (Control & Status Register)
+ * into a series of consecutive physical registers.
+ *
+ * For example, in the configuration with 8-bit CSR Bus,
+ * 32-bit aligned (the default one for 32-bit CPUs) a 32-bit
+ * logical CSR will be generated as four 32-bit physical registers,
+ * each one containing one byte of meaningful data.
+ *
+ * For details see: https://github.com/enjoy-digital/litex/wiki/CSR-Bus
+ *
+ * The purpose of `litex_set_reg`/`litex_get_reg` is to implement
+ * the logic of writing to/reading from the LiteX CSR in a single
+ * place that can be then reused by all LiteX drivers.
+ */
+void litex_set_reg(void __iomem *reg, unsigned long reg_size,
+                   unsigned long val)
+{
+       unsigned long shifted_data, shift, i;
+       unsigned long flags;
+
+       spin_lock_irqsave(&csr_lock, flags);
+
+       for (i = 0; i < reg_size; ++i) {
+               shift = ((reg_size - i - 1) * LITEX_SUBREG_SIZE_BIT);
+               shifted_data = val >> shift;
+
+               writel((u32 __force)cpu_to_le32(shifted_data), reg + (LITEX_REG_SIZE * i));
+       }
+
+       spin_unlock_irqrestore(&csr_lock, flags);
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c

Quoted 2 lines
drivers/soc/litex/litex_soc_ctrl.c new file +2 -0
+}
+EXPORT_SYMBOL_GPL(litex_set_reg);

I'm still wondering about the overhead of loops and multiple accesses, and the need for them (see also BenH's earlier comment).
If e.g. the register widths change for LiteUART (currently they're hardcoded to one), would you still consider it using the same programming interface, and thus compatible with "litex,liteuart"?

Quoted 2 lines

Since the amount of possible reg_size is practically limited we could add explicit 8/32/64/128 accessors to eliminate loops.

Good.

(assuming 32-bit physical reg accesses below)

Quoted 2 lines

As for multiple writel/readl I don't really see an option to avoid them for the 8-bit bus width.

Sure, 64-bit register accesses consist of two 32-bit accesses on other 32-bit platforms, too.

Quoted 6 lines
Quoted 2 lines

The spinlock access will probably become the source of lock contention later, especially when considering SMP variants.

Do you have any suggestions on how to handle this?
Dropping locks could lead to the situation when two cores write at the same time leaving a wrong (mixed) value in the CSR.

Is this due to the CSR bus or due to the CSR register?
I mean can two 64-bit accesses to different CSR registers be done as four interleaved 32-bit accesses, or must they not be interleaved?

Quoted 10 lines

If (hopefully) they can be interleaved, you just need serialization of accesses to the same 64-bit register. As the same register is usually not accessed from multiple drivers, you can handle the serialization inside the driver, if it can ever happen at all (e.g. main driver operation and interrupt handler accessing the same register). That avoids the need for the spinlock in the generic register accessors. If they must not be interleaved, you indeed need serialization at the bus level, but only for the 64-bit accesses? And I would strongly suggest to look into changing the CSR bus behavior at the hardware level, if possible...

Good point. I was thinking about the single CSR register consistency, not the whole bus.

Changing the CSR behaviour at the LiteX level is something that is currently discussed by the developers, but I'm not aware of any timeline.
I believe we can always simplify the driver's code in the future, once the HW becomes better suited for Linux support in the default configuration.

Quoted 35 lines
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
drivers/soc/litex/litex_soc_ctrl.c new file +18 -0
+/*
+ * Check LiteX CSR read/write access
+ *
+ * This function reads and writes a scratch register in order
+ * to verify if CSR access works.
+ *
+ * In case any problems are detected, the driver should panic.
+ *
+ * Access to the LiteX CSR is, by design, done in CPU native
+ * endianness. The driver should not dynamically configure
+ * access functions when the endianness mismatch is detected.
+ * Such situation indicates problems in the soft SoC design
+ * and should be solved at the LiteX generator level,
+ * not in the software.
+ */
+static int litex_check_csr_access(void __iomem *reg_addr)
+{
+       unsigned long reg;
Quoted 8 lines

drivers/soc/litex/litex_soc_ctrl.c line 117

Quoted 6 lines
drivers/soc/litex/litex_soc_ctrl.c new file +6 -0
+
+       reg = litex_get_reg(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_SIZE);
+
+       if (reg != SCRATCH_REG_VALUE) {
+               panic("Scratch register read error! Expected: 0x%x but got: 0x%lx",
+                       SCRATCH_REG_VALUE, reg);

Do you think the user will ever see this panic message? (see below)

Quoted 3 lines

On UART most probably not, as broken CSRs mean broken UART driver as well. But I believe you can retrieve logs from the memory and analyze them post-mortem, isn't that right?

Sure. Been there, done that ;-)

Gr{oetje,eeting}s,

Quoted 8 lines

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Best,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland

author maintainer
#15 lore raw

PATCH v11 4/5 dt-bindings: serial: document LiteUART bindings

In reply to Mateusz Holenko

From: Filip Kokosinski <[email protected]>

Add documentation for LiteUART devicetree bindings.

---

Notes:
No changes in v11.

No changes in v10.

No changes in v9.

No changes in v8.

No changes in v7.

Changes in v6:
- fixed license header

No changes in v5.

No changes in v4.

Changes in v3:
- added Reviewed-by tag
- patch number changed from 3 to 4
- removed changes in MAINTAINERS file (moved to patch #2)

Changes in v2:
- binding description rewritten to a yaml schema file
- added interrupt line
- fixed unit address
- patch number changed from 2 to 3

 .../bindings/serial/litex,liteuart.yaml       | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/litex,liteuart.yaml
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +38 -0
new file mode 100644
index 000000000000..69acb222bb57
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/litex,liteuart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LiteUART serial controller
+
+maintainers:
+  - Karol Gugala <[email protected]>
+  - Mateusz Holenko <[email protected]>
+
+description: |
+  LiteUART serial controller is a part of LiteX FPGA SoC builder. It supports
+  multiple CPU architectures, currently including e.g. OpenRISC and RISC-V.
+
+properties:
+  compatible:
+    const: litex,liteuart
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    uart0: serial@e0001800 {
+      compatible = "litex,liteuart";
+      reg = <0xe0001800 0x100>;
+      interrupts = <2>;
+    };
Signature · 1 line
2.25.1
#16 lore raw

Re: [PATCH v11 4/5] dt-bindings: serial: document LiteUART bindings

Hi Mateusz,

Quoted 7 lines · Mateusz Holenko

From: Filip Kokosinski <[email protected]>

Add documentation for LiteUART devicetree bindings.

Thanks for your patch!

Quoted 8 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +5 -0
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/litex,liteuart.yaml#

Documentation/devicetree/bindings/serial/litex,liteuart.yaml line 15

Quoted 10 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +10 -0
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LiteUART serial controller
+
+maintainers:
+  - Karol Gugala <[email protected]>
+  - Mateusz Holenko <[email protected]>
+
+description: |
+  LiteUART serial controller is a part of LiteX FPGA SoC builder. It supports

part of the

Documentation/devicetree/bindings/serial/litex,liteuart.yaml line 20

Quoted 5 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +5 -0
+  multiple CPU architectures, currently including e.g. OpenRISC and RISC-V.
+
+properties:
+  compatible:
+    const: litex,liteuart

Have you already decided how to handle future LiteUART variants that add new features (e.g. CTS/RTS, DMA)?

Quoted 19 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +18 -1
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    uart0: serial@e0001800 {
+      compatible = "litex,liteuart";
+      reg = <0xe0001800 0x100>;
+      interrupts = <2>;
+    };
--
Quoted 2 lines

2.25.1

--
Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author maintainer
#17 lore raw

Re: [PATCH v11 4/5] dt-bindings: serial: document LiteUART bindings

Hi Geert,

Quoted 6 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 2 lines · Mateusz Holenko

From: Filip Kokosinski <[email protected]>

Quoted 7 lines
Quoted 5 lines

Add documentation for LiteUART devicetree bindings.

Thanks for your patch!

Thanks for your review!

Quoted 13 lines
Quoted 13 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +9 -0
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/litex,liteuart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LiteUART serial controller
+
Quoted 8 lines

Documentation/devicetree/bindings/serial/litex,liteuart.yaml line 15

Quoted 6 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +6 -0
+maintainers:
+  - Karol Gugala <[email protected]>
+  - Mateusz Holenko <[email protected]>
+
+description: |
+  LiteUART serial controller is a part of LiteX FPGA SoC builder. It supports

part of the

Right, will fix that.

Quoted 8 lines

Documentation/devicetree/bindings/serial/litex,liteuart.yaml line 20

Quoted 5 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +5 -0
+  multiple CPU architectures, currently including e.g. OpenRISC and RISC-V.
+
+properties:
+  compatible:
+    const: litex,liteuart

Have you already decided how to handle future LiteUART variants that add new features (e.g. CTS/RTS, DMA)?

We were thinking of adding KConfig options, like

[ ] LiteUART serial port support
< > LiteUART DMA support

and using ifdefs in the code.

The other option could be to extend LiteX itself so that the UART core provides information about its configuration via the capabilities register. That way the driver could configure itself automatically at runtime.

This is, however, not decided yet.

Quoted 27 lines
Quoted 20 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +18 -1
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    uart0: serial@e0001800 {
+      compatible = "litex,liteuart";
+      reg = <0xe0001800 0x100>;
+      interrupts = <2>;
+    };
--
Quoted 2 lines

2.25.1

--
Gr{oetje,eeting}s,

Quoted 8 lines

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Best regards,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland

#18 lore raw

Re: [PATCH v11 4/5] dt-bindings: serial: document LiteUART bindings

Hi Mateusz,

Quoted 20 lines · Mateusz Holenko
Quoted 12 lines · Geert Uytterhoeven
Quoted 10 lines · Mateusz Holenko

From: Filip Kokosinski <[email protected]>

Add documentation for LiteUART devicetree bindings.

Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +0 -0
Quoted 6 lines

Documentation/devicetree/bindings/serial/litex,liteuart.yaml line 20

Quoted 3 lines
Documentation/devicetree/bindings/serial/litex,liteuart.yaml new file +3 -0
+properties:
+  compatible:
+    const: litex,liteuart

Have you already decided how to handle future LiteUART variants that add new features (e.g. CTS/RTS, DMA)?

Quoted 6 lines

We were thinking of adding KConfig options, like

[ ] LiteUART serial port support
< > LiteUART DMA support

and using ifdefs in the code.

That is the driver part, not the DT part.
If enabled, the driver still needs to know if the feature is present and to be used, or not.

Quoted 5 lines

The other option could be to extend LiteX itself so that the UART core provides information about its configuration via the capabilities register. That way the driver could configure itself automatically at runtime.

This is, however, not decided yet.

A capabilities register sounds good to me.
That means everything is handled automatically by the driver However, it does mean the DT schema checker cannot validate the use of optional DT properties related to optional features, if any.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#19 lore raw

PATCH v11 5/5 drivers/tty/serial: add LiteUART driver

In reply to Mateusz Holenko

From: Filip Kokosinski <[email protected]>

This commit adds driver for the FPGA-based LiteUART serial controller from LiteX SoC builder.

The current implementation supports LiteUART configured for 32 bit data width and 8 bit CSR bus width.

It does not support IRQ.

---

Notes:
Changes in v11:
- added Reviewed-by tag

No changes in v10.

No changes in v9.

Changes in v8:
- fixed help messages in LiteUART's KConfig
- removed dependency between LiteUART and LiteX SoC drivers

Changed in v7:
- added missing include directive

Changes in v6:
- LiteUART ports now stored in xArray
- removed PORT_LITEUART
- fixed formatting
- removed some unnecessary defines

No changes in v5.

Changes in v4:
- fixed copyright header
- removed a wrong dependency on UARTLITE from Kconfig
- added a dependency on LITEX_SOC_CONTROLLER to LITEUART in Kconfig

Changes in v3:
- aliases made optional
- used litex_get_reg/litex_set_reg functions instead of macros
- SERIAL_LITEUART_NR_PORTS renamed to SERIAL_LITEUART_MAX_PORTS
- PORT_LITEUART changed from 122 to 123
- added dependency on LITEX_SOC_CONTROLLER
- patch number changed from 4 to 5

No changes in v2.

 MAINTAINERS                   |   1 +
 drivers/tty/serial/Kconfig    |  32 +++
 drivers/tty/serial/Makefile   |   1 +
 drivers/tty/serial/liteuart.c | 402 ++++++++++++++++++++++++++++++++++
 4 files changed, 436 insertions(+)
 create mode 100644 drivers/tty/serial/liteuart.c
MAINTAINERS +1 -0
index 4d70a1b22a87..1387cefc63ce 100644
@@ -9841,6 +9841,7 @@ M:	Mateusz Holenko <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/*/litex,*.yaml
 F:	drivers/soc/litex/litex_soc_ctrl.c
+F:	drivers/tty/serial/liteuart.c
 F:	include/linux/litex.h
 
 LIVE PATCHING
drivers/tty/serial/Kconfig +32 -0
index adf9e80e7dc9..17aaf0afb27a 100644
@@ -1562,6 +1562,38 @@ config SERIAL_MILBEAUT_USIO_CONSOLE
 	  receives all kernel messages and warnings and which allows logins in
 	  single user mode).
 
+config SERIAL_LITEUART
+	tristate "LiteUART serial port support"
+	depends on HAS_IOMEM
+	depends on OF || COMPILE_TEST
+	depends on LITEX_SOC_CONTROLLER
+	select SERIAL_CORE
+	help
+	  This driver is for the FPGA-based LiteUART serial controller from LiteX
+	  SoC builder.
+
+	  Say 'Y' or 'M' here if you wish to use the LiteUART serial controller.
+	  Otherwise, say 'N'.
+
+config SERIAL_LITEUART_MAX_PORTS
+	int "Maximum number of LiteUART ports"
+	depends on SERIAL_LITEUART
+	default "1"
+	help
+	  Set this to the maximum number of serial ports you want the driver
+	  to support.
+
+config SERIAL_LITEUART_CONSOLE
+	bool "LiteUART serial port console support"
+	depends on SERIAL_LITEUART=y
+	select SERIAL_CORE_CONSOLE
+	help
+	  Say 'Y' or 'M' here if you wish to use the FPGA-based LiteUART serial
+	  controller from LiteX SoC builder as the system console
+	  (the system console is the device which receives all kernel messages
+	  and warnings and which allows logins in single user mode).
+	  Otherwise, say 'N'.
+
 endmenu
 
 config SERIAL_MCTRL_GPIO
drivers/tty/serial/Makefile +1 -0
index d056ee6cca33..9f8ba419ff3b 100644
@@ -89,6 +89,7 @@ obj-$(CONFIG_SERIAL_OWL)	+= owl-uart.o
 obj-$(CONFIG_SERIAL_RDA)	+= rda-uart.o
 obj-$(CONFIG_SERIAL_MILBEAUT_USIO) += milbeaut_usio.o
 obj-$(CONFIG_SERIAL_SIFIVE)	+= sifive.o
+obj-$(CONFIG_SERIAL_LITEUART) += liteuart.o
 
 # GPIOLIB helpers for modem control lines
 obj-$(CONFIG_SERIAL_MCTRL_GPIO)	+= serial_mctrl_gpio.o
drivers/tty/serial/liteuart.c new file +402 -0

402 lines not on this page · raw message

Signature · 1 line
2.25.1
#20 lore raw

Re: [PATCH v11 5/5] drivers/tty/serial: add LiteUART driver

Hi Mateusz,

Quoted 5 lines · Mateusz Holenko

From: Filip Kokosinski <[email protected]>

This commit adds driver for the FPGA-based LiteUART serial controller from LiteX SoC builder.

Quoted 7 lines

The current implementation supports LiteUART configured for 32 bit data width and 8 bit CSR bus width.

It does not support IRQ.

Thanks for your patch!

Quoted 13 lines
drivers/tty/serial/liteuart.c new file +10 -0
 
+static int liteuart_probe(struct platform_device *pdev)
+{
+       struct device_node *np = pdev->dev.of_node;
+       struct liteuart_port *uart;
+       struct uart_port *port;
+       struct xa_limit limit;
+       int dev_id, ret;
+
+       /* no device tree */
+       if (!np)

drivers/tty/serial/liteuart.c line 264

Quoted 10 lines
drivers/tty/serial/liteuart.c new file +10 -0
+               return -ENODEV;
+
+       /* look for aliases; auto-enumerate for free index if not found */
+       dev_id = of_alias_get_id(np, "serial");
+       if (dev_id < 0)
+               limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
+       else
+               limit = XA_LIMIT(dev_id, dev_id);
+
+       uart = kzalloc(sizeof(struct liteuart_port), GFP_KERNEL);

Who frees this memory? Use devm_kzalloc()?

drivers/tty/serial/liteuart.c line 268

Quoted 4 lines
drivers/tty/serial/liteuart.c new file +4 -0
+       if (!uart)
+               return -ENOMEM;
+
+       ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);

Who frees this entry?

Quoted 40 lines
drivers/tty/serial/liteuart.c new file +39 -0
+       if (ret)
+               return ret;
+
+       port = &uart->port;
+
+       /* get membase */
+       port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
+       if (!port->membase)
+               return -ENXIO;
+
+       /* values not from device tree */
+       port->dev = &pdev->dev;
+       port->iotype = UPIO_MEM;
+       port->flags = UPF_BOOT_AUTOCONF;
+       port->ops = &liteuart_ops;
+       port->regshift = 2;
+       port->fifosize = 16;
+       port->iobase = 1;
+       port->type = PORT_UNKNOWN;
+       port->line = dev_id;
+       spin_lock_init(&port->lock);
+
+       return uart_add_one_port(&liteuart_driver, &uart->port);
+}
 
+static int __init liteuart_init(void)
+{
+       int res;
+
+       res = uart_register_driver(&liteuart_driver);
+       if (res)
+               return res;
+
+       res = platform_driver_register(&liteuart_platform_driver);
+       if (res) {
+               uart_unregister_driver(&liteuart_driver);
+               return res;
+       }
+
+       return 0;

drivers/tty/serial/liteuart.c line 397

Quoted 10 lines
drivers/tty/serial/liteuart.c new file +10 -0
+}
+
+static void __exit liteuart_exit(void)
+{
+       platform_driver_unregister(&liteuart_platform_driver);
+       uart_unregister_driver(&liteuart_driver);
+}
+
+module_init(liteuart_init);
+module_exit(liteuart_exit);

Several drivers call uart_{,un}register_driver() from their .probe() resp. .remove() callbacks, so they can use module_platform_driver() instead of the above boilerplate. Greg, what's your stance on that?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#21 lore raw

Re: [PATCH v11 5/5] drivers/tty/serial: add LiteUART driver

Hi Geert,

Quoted 12 lines · Geert Uytterhoeven

Hi Mateusz,

Quoted 8 lines · Mateusz Holenko

From: Filip Kokosinski <[email protected]>

This commit adds driver for the FPGA-based LiteUART serial controller from LiteX SoC builder.

The current implementation supports LiteUART configured for 32 bit data width and 8 bit CSR bus width.

Quoted 6 lines
Quoted 4 lines

It does not support IRQ.

Thanks for your patch!

Thanks for your review!

Quoted 18 lines
Quoted 18 lines
drivers/tty/serial/liteuart.c new file +14 -0
 
+static int liteuart_probe(struct platform_device *pdev)
+{
+       struct device_node *np = pdev->dev.of_node;
+       struct liteuart_port *uart;
+       struct uart_port *port;
+       struct xa_limit limit;
+       int dev_id, ret;
+
+       /* no device tree */
+       if (!np)
+               return -ENODEV;
+
+       /* look for aliases; auto-enumerate for free index if not found */
+       dev_id = of_alias_get_id(np, "serial");
Quoted 8 lines

drivers/tty/serial/liteuart.c line 264

Quoted 6 lines
drivers/tty/serial/liteuart.c new file +6 -0
+       if (dev_id < 0)
+               limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
+       else
+               limit = XA_LIMIT(dev_id, dev_id);
+
+       uart = kzalloc(sizeof(struct liteuart_port), GFP_KERNEL);

Who frees this memory? Use devm_kzalloc()?

You are right - it leaks right now. We'll switch to devm_kzalloc().

Quoted 6 lines

drivers/tty/serial/liteuart.c line 268

Quoted 4 lines
drivers/tty/serial/liteuart.c new file +4 -0
+       if (!uart)
+               return -ENOMEM;
+
+       ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);

Who frees this entry?

We'll add a call to xa_erase() when removing the driver.

Quoted 46 lines
Quoted 46 lines
drivers/tty/serial/liteuart.c new file +45 -0
+       if (ret)
+               return ret;
+
+       port = &uart->port;
+
+       /* get membase */
+       port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
+       if (!port->membase)
+               return -ENXIO;
+
+       /* values not from device tree */
+       port->dev = &pdev->dev;
+       port->iotype = UPIO_MEM;
+       port->flags = UPF_BOOT_AUTOCONF;
+       port->ops = &liteuart_ops;
+       port->regshift = 2;
+       port->fifosize = 16;
+       port->iobase = 1;
+       port->type = PORT_UNKNOWN;
+       port->line = dev_id;
+       spin_lock_init(&port->lock);
+
+       return uart_add_one_port(&liteuart_driver, &uart->port);
+}
 
+static int __init liteuart_init(void)
+{
+       int res;
+
+       res = uart_register_driver(&liteuart_driver);
+       if (res)
+               return res;
+
+       res = platform_driver_register(&liteuart_platform_driver);
+       if (res) {
+               uart_unregister_driver(&liteuart_driver);
+               return res;
+       }
+
+       return 0;
+}
+
+static void __exit liteuart_exit(void)
+{
+       platform_driver_unregister(&liteuart_platform_driver);
+       uart_unregister_driver(&liteuart_driver);
Quoted 8 lines

drivers/tty/serial/liteuart.c line 397

Quoted 4 lines
drivers/tty/serial/liteuart.c new file +4 -0
+}
+
+module_init(liteuart_init);
+module_exit(liteuart_exit);

Several drivers call uart_{,un}register_driver() from their .probe() resp. .remove() callbacks, so they can use module_platform_driver() instead of the above boilerplate. Greg, what's your stance on that?

I don't have much experience here and can't tell which version is the preferred one.

Quoted 10 lines

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Best regards,
Mateusz

--
Mateusz Holenko
Antmicro Ltd | www.antmicro.com
Roosevelta 22, 60-829 Poznan, Poland