linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
@ 2021-10-14 22:05 Emmanuel Gil Peyrot
  2021-10-27 16:45 ` Alexandre Belloni
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
  0 siblings, 2 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-14 22:05 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, linux-kernel, linux-rtc

These three consoles share a device, the MX23L4005, which contains a
clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
(similar to SPI) on channel 0, device 1.  This driver allows it to be
used as a Linux RTC device, where time can be read and set.

The hardware also exposes two timers, one which shuts down the console
and one which powers it on, but these aren’t supported currently.

On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
order to avoid having to implement all that, this driver assumes a
bootloader will parse this XML file and write the bias into the SRAM, at
the same location the other two consoles have it.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/rtc/Kconfig        |  10 ++
 drivers/rtc/Makefile       |   1 +
 drivers/rtc/rtc-nintendo.c | 305 +++++++++++++++++++++++++++++++++++++
 3 files changed, 316 insertions(+)
 create mode 100644 drivers/rtc/rtc-nintendo.c

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 914497abeef9..f2a15429e4b1 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -1214,6 +1214,16 @@ config RTC_DRV_V3020
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-v3020.
 
+config RTC_DRV_NINTENDO
+	tristate "Nintendo GameCube, Wii and Wii U RTC"
+	depends on GAMECUBE || WII || WIIU || COMPILE_TEST
+	help
+	  If you say yes here you will get support for the RTC subsystem
+	  of the Nintendo GameCube, Wii and Wii U.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called "rtc-nintendo".
+
 config RTC_DRV_WM831X
 	tristate "Wolfson Microelectronics WM831x RTC"
 	depends on MFD_WM831X
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 2dd0dd956b0e..0c7c0e7d9637 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -108,6 +108,7 @@ obj-$(CONFIG_RTC_DRV_MT7622)	+= rtc-mt7622.o
 obj-$(CONFIG_RTC_DRV_MV)	+= rtc-mv.o
 obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
 obj-$(CONFIG_RTC_DRV_MXC_V2)	+= rtc-mxc_v2.o
+obj-$(CONFIG_RTC_DRV_NINTENDO)	+= rtc-nintendo.o
 obj-$(CONFIG_RTC_DRV_NTXEC)	+= rtc-ntxec.o
 obj-$(CONFIG_RTC_DRV_OMAP)	+= rtc-omap.o
 obj-$(CONFIG_RTC_DRV_OPAL)	+= rtc-opal.o
diff --git a/drivers/rtc/rtc-nintendo.c b/drivers/rtc/rtc-nintendo.c
new file mode 100644
index 000000000000..11bea40ca13d
--- /dev/null
+++ b/drivers/rtc/rtc-nintendo.c
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nintendo GameCube, Wii and Wii U RTC driver
+ *
+ * This driver is for the MX23L4005, more specifically its real-time clock and
+ * SRAM storage.  The value returned by the RTC counter must be added with the
+ * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
+ * /config/rtc.xml (on the Wii U).  The latter being very impractical to access
+ * from Linux, this driver assumes the bootloader has read it and stored it in
+ * SRAM like for the other two consoles.
+ *
+ * This device sits on a bus named EXI (which is similar to SPI), channel 0,
+ * device 1.  This driver assumes no other user of the EXI bus, which is
+ * currently the case but would have to be reworked to add support for other
+ * GameCube hardware exposed on this bus.
+ *
+ * Note that this device is very much not Y2k38 aware.
+ *
+ * References:
+ * - https://wiiubrew.org/wiki/Hardware/RTC
+ * - https://wiibrew.org/wiki/MX23L4005
+ *
+ * Copyright (C) 2018 rw-r-r-0644
+ * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ *
+ * Based on rtc-gcn.c
+ * Copyright (C) 2004-2009 The GameCube Linux Team
+ * Copyright (C) 2005,2008,2009 Albert Herranz
+ * Based on gamecube_time.c from Torben Nielsen.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/time.h>
+
+/* EXI registers */
+#define EXICSR	0
+#define EXICR	12
+#define EXIDATA	16
+
+/* EXI register values */
+#define EXICSR_DEV		0x380
+	#define EXICSR_DEV1	0x100
+#define EXICSR_CLK		0x070
+	#define EXICSR_CLK_1MHZ	0x000
+	#define EXICSR_CLK_2MHZ	0x010
+	#define EXICSR_CLK_4MHZ	0x020
+	#define EXICSR_CLK_8MHZ	0x030
+	#define EXICSR_CLK_16MHZ 0x040
+	#define EXICSR_CLK_32MHZ 0x050
+#define EXICSR_INT		0x008
+	#define EXICSR_INTSET	0x008
+
+#define EXICR_TSTART		0x001
+#define EXICR_TRSMODE		0x002
+	#define EXICR_TRSMODE_IMM 0x000
+#define EXICR_TRSTYPE		0x00C
+	#define EXICR_TRSTYPE_R	0x000
+	#define EXICR_TRSTYPE_W	0x004
+#define EXICR_TLEN		0x030
+	#define EXICR_TLEN32	0x030
+
+/* EXI registers values to access the RTC */
+#define RTC_EXICSR	(EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
+#define RTC_EXICR_W	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
+#define RTC_EXICR_R	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
+#define RTC_EXIDATA_W	0x80000000
+
+/* RTC registers */
+#define RTC_COUNTER	0x20000000
+#define RTC_SRAM	0x20000100
+#define RTC_SRAM_BIAS	0x20000400
+#define RTC_SNAPSHOT	0x20400000
+#define RTC_ONTMR	0x21000000
+#define RTC_OFFTMR	0x21000100
+#define RTC_TEST0	0x21000400
+#define RTC_TEST1	0x21000500
+#define RTC_TEST2	0x21000600
+#define RTC_TEST3	0x21000700
+#define RTC_CONTROL0	0x21000C00
+#define RTC_CONTROL1	0x21000D00
+
+struct nintendo_rtc_drvdata {
+	void __iomem *iob;
+	u32 rtc_bias;
+};
+
+static u32 exi_read(void __iomem *iob, u32 reg)
+{
+	u32 data;
+
+	/* The spin loops here loop about 15~16 times each, so there is no need
+	 * to use a more expensive sleep method. */
+
+	/* Write register offset */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(reg, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Read data */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(RTC_EXICR_R, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+	data = ioread32be(iob + EXIDATA);
+
+	/* Clear channel parameters */
+	iowrite32be(0, iob + EXICSR);
+
+	return data;
+}
+
+static void exi_write(void __iomem *iob, u32 reg, u32 data)
+{
+	/* The spin loops here loop about 15~16 times each, so there is no need
+	 * to use a more expensive sleep method. */
+
+	/* Write register offset */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(reg | RTC_EXIDATA_W, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Write data */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(data, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Clear channel parameters */
+	iowrite32be(0, iob + EXICSR);
+}
+
+static int nintendo_rtc_read_time(struct device *dev, struct rtc_time *t)
+{
+	time64_t timestamp;
+	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
+
+	/* Add the counter and the bias to obtain the timestamp */
+	timestamp = exi_read(d->iob, RTC_COUNTER) + d->rtc_bias;
+	rtc_time64_to_tm(timestamp, t);
+
+	return 0;
+}
+
+static int nintendo_rtc_set_time(struct device *dev, struct rtc_time *t)
+{
+	time64_t timestamp;
+	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
+
+	/* Subtract the timestamp and the bias to obtain the counter value */
+	timestamp = rtc_tm_to_time64(t);
+	exi_write(d->iob, RTC_COUNTER, timestamp - d->rtc_bias);
+
+	return 0;
+}
+
+static const struct rtc_class_ops nintendo_rtc_ops = {
+	.read_time      = nintendo_rtc_read_time,
+	.set_time       = nintendo_rtc_set_time,
+};
+
+#ifdef DEBUG
+static void nintendo_rtc_dumpregs(void __iomem *iob)
+{
+	int i;
+	u32 sram_addr = RTC_SRAM;
+
+	printk("RTC_COUNTER:  %08X\n", exi_read(iob, RTC_COUNTER));
+	printk("RTC_SNAPSHOT: %08X\n", exi_read(iob, RTC_SNAPSHOT));
+	printk("RTC_ONTMR:    %08X\n", exi_read(iob, RTC_ONTMR));
+	printk("RTC_OFFTMR:   %08X\n", exi_read(iob, RTC_OFFTMR));
+	printk("RTC_TEST0:    %08X\n", exi_read(iob, RTC_TEST0));
+	printk("RTC_TEST1:    %08X\n", exi_read(iob, RTC_TEST1));
+	printk("RTC_TEST2:    %08X\n", exi_read(iob, RTC_TEST2));
+	printk("RTC_TEST3:    %08X\n", exi_read(iob, RTC_TEST3));
+	printk("RTC_CONTROL0: %08X\n", exi_read(iob, RTC_CONTROL0));
+	printk("RTC_CONTROL1: %08X\n", exi_read(iob, RTC_CONTROL1));
+	printk("RTC_SRAM:\n");
+	for(i = 0; i < 4; i++) {
+		printk("%08X %08X %08X %08X\n",
+		       exi_read(iob, sram_addr + 0x100 * 0),
+		       exi_read(iob, sram_addr + 0x100 * 1),
+		       exi_read(iob, sram_addr + 0x100 * 2),
+		       exi_read(iob, sram_addr + 0x100 * 3));
+		sram_addr += 0x400;
+	}
+}
+#endif
+
+static int nintendo_rtc_read_offset_from_sram(struct nintendo_rtc_drvdata *d)
+{
+	struct device_node *np;
+	int ret;
+	struct resource res;
+	void __iomem *hw_srnprot;
+	u32 old;
+
+	np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
+	if (!np) {
+		pr_err("HW_SRNPROT not found\n");
+		return -1;
+	}
+
+	ret = of_address_to_resource(np, 0, &res);
+	if (ret) {
+		pr_err("no io memory range found\n");
+		return -1;
+	}
+
+	hw_srnprot = ioremap(res.start, resource_size(&res));
+	old = ioread32be(hw_srnprot);
+
+	/* TODO: figure out why we use this magic constant.  I obtained it by
+	 * reading the leftover value after boot, after IOSU already ran.
+	 *
+	 * On my Wii U, setting this register to 1 prevents the console from
+	 * rebooting properly, so wiiubrew.org must be missing something.
+	 *
+	 * See https://wiiubrew.org/wiki/Hardware/Latte_registers
+	 */
+	if (old != 0x7bf)
+		iowrite32be(0x7bf, hw_srnprot);
+
+#ifdef DEBUG
+	nintendo_rtc_dumpregs(d->iob);
+#endif
+
+	/* Get the offset from RTC SRAM.
+	 *
+	 * Its default location on the GameCube and on the Wii is in the SRAM,
+	 * while on the Wii U the bootloader needs to fill it with the contents
+	 * of /config/rtc.xml on the SLC (the eMMC).  We don’t do that from
+	 * Linux since it requires implementing a proprietary filesystem and do
+	 * file decryption, instead we require the bootloader to fill the same
+	 * SRAM address as on previous consoles.
+	 */
+	d->rtc_bias = exi_read(d->iob, RTC_SRAM_BIAS);
+
+	/* Reset SRAM access to how it was before, our job here is done. */
+	iowrite32be(old, hw_srnprot);
+	iounmap(hw_srnprot);
+
+	return 0;
+}
+
+static int nintendo_rtc_probe(struct platform_device *pdev) {
+	struct device *dev = &pdev->dev;
+	struct rtc_device *rtc;
+	struct resource	*res;
+	struct nintendo_rtc_drvdata *d;
+	int ret;
+
+	d = devm_kzalloc(dev, sizeof(struct nintendo_rtc_drvdata), GFP_KERNEL);
+	if (IS_ERR(d))
+		return PTR_ERR(d);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	d->iob = devm_ioremap_resource(dev, res);
+	if (IS_ERR(d->iob))
+		return PTR_ERR(d->iob);
+
+	ret = nintendo_rtc_read_offset_from_sram(d);
+	if (ret)
+		return ret;
+	dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
+
+	dev_set_drvdata(dev, d);
+
+	rtc = devm_rtc_device_register(dev, dev_name(dev), &nintendo_rtc_ops,
+				       THIS_MODULE);
+	if (IS_ERR(rtc))
+		return PTR_ERR(rtc);
+
+	return 0;
+}
+
+static const struct of_device_id nintendo_rtc_of_match[] = {
+	{.compatible = "nintendo,latte-exi" },
+	{.compatible = "nintendo,hollywood-exi" },
+	{.compatible = "nintendo,flipper-exi" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, nintendo_rtc_of_match);
+
+static struct platform_driver nintendo_rtc_driver = {
+	.probe		= nintendo_rtc_probe,
+	.driver		= {
+		.name	= "rtc-nintendo",
+		.of_match_table	= nintendo_rtc_of_match,
+	},
+};
+module_platform_driver(nintendo_rtc_driver);
+
+MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
+MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
+MODULE_LICENSE("GPL");
-- 
2.33.1


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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-14 22:05 [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
@ 2021-10-27 16:45 ` Alexandre Belloni
  2021-10-27 17:05   ` Emmanuel Gil Peyrot
  2021-10-28 20:32   ` Jonathan Neuschäfer
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
  1 sibling, 2 replies; 28+ messages in thread
From: Alexandre Belloni @ 2021-10-27 16:45 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, linux-kernel, linux-rtc

Hello Emmanuel,

Thanks for the patch!

On 15/10/2021 00:05:24+0200, Emmanuel Gil Peyrot wrote:
> These three consoles share a device, the MX23L4005, which contains a
> clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> (similar to SPI) on channel 0, device 1.  This driver allows it to be
> used as a Linux RTC device, where time can be read and set.
> 
> The hardware also exposes two timers, one which shuts down the console
> and one which powers it on, but these aren’t supported currently.
> 
> On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
> encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
> order to avoid having to implement all that, this driver assumes a
> bootloader will parse this XML file and write the bias into the SRAM, at
> the same location the other two consoles have it.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---
>  drivers/rtc/Kconfig        |  10 ++
>  drivers/rtc/Makefile       |   1 +
>  drivers/rtc/rtc-nintendo.c | 305 +++++++++++++++++++++++++++++++++++++

I'm not convinced this is a good name, seeing that the switch will
certainly not use this driver (neither is the snes mini).

>  3 files changed, 316 insertions(+)
>  create mode 100644 drivers/rtc/rtc-nintendo.c
> 
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index 914497abeef9..f2a15429e4b1 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -1214,6 +1214,16 @@ config RTC_DRV_V3020
>  	  This driver can also be built as a module. If so, the module
>  	  will be called rtc-v3020.
>  
> +config RTC_DRV_NINTENDO
> +	tristate "Nintendo GameCube, Wii and Wii U RTC"
> +	depends on GAMECUBE || WII || WIIU || COMPILE_TEST

CONFIG_WIIU doesn't seem to exist or am I missing something?

> +	help
> +	  If you say yes here you will get support for the RTC subsystem
> +	  of the Nintendo GameCube, Wii and Wii U.
> +
> +	  This driver can also be built as a module. If so, the module
> +	  will be called "rtc-nintendo".
> +
>  config RTC_DRV_WM831X
>  	tristate "Wolfson Microelectronics WM831x RTC"
>  	depends on MFD_WM831X
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> index 2dd0dd956b0e..0c7c0e7d9637 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -108,6 +108,7 @@ obj-$(CONFIG_RTC_DRV_MT7622)	+= rtc-mt7622.o
>  obj-$(CONFIG_RTC_DRV_MV)	+= rtc-mv.o
>  obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
>  obj-$(CONFIG_RTC_DRV_MXC_V2)	+= rtc-mxc_v2.o
> +obj-$(CONFIG_RTC_DRV_NINTENDO)	+= rtc-nintendo.o
>  obj-$(CONFIG_RTC_DRV_NTXEC)	+= rtc-ntxec.o
>  obj-$(CONFIG_RTC_DRV_OMAP)	+= rtc-omap.o
>  obj-$(CONFIG_RTC_DRV_OPAL)	+= rtc-opal.o
> diff --git a/drivers/rtc/rtc-nintendo.c b/drivers/rtc/rtc-nintendo.c
> new file mode 100644
> index 000000000000..11bea40ca13d
> --- /dev/null
> +++ b/drivers/rtc/rtc-nintendo.c

You have a bunch of checkpatch warnings, can you fix those?

> @@ -0,0 +1,305 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Nintendo GameCube, Wii and Wii U RTC driver
> + *
> + * This driver is for the MX23L4005, more specifically its real-time clock and
> + * SRAM storage.  The value returned by the RTC counter must be added with the
> + * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
> + * /config/rtc.xml (on the Wii U).  The latter being very impractical to access
> + * from Linux, this driver assumes the bootloader has read it and stored it in
> + * SRAM like for the other two consoles.
> + *
> + * This device sits on a bus named EXI (which is similar to SPI), channel 0,
> + * device 1.  This driver assumes no other user of the EXI bus, which is
> + * currently the case but would have to be reworked to add support for other
> + * GameCube hardware exposed on this bus.
> + *
> + * Note that this device is very much not Y2k38 aware.
> + *
> + * References:
> + * - https://wiiubrew.org/wiki/Hardware/RTC
> + * - https://wiibrew.org/wiki/MX23L4005
> + *
> + * Copyright (C) 2018 rw-r-r-0644
> + * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> + *
> + * Based on rtc-gcn.c
> + * Copyright (C) 2004-2009 The GameCube Linux Team
> + * Copyright (C) 2005,2008,2009 Albert Herranz
> + * Based on gamecube_time.c from Torben Nielsen.
> + */
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_address.h>
> +#include <linux/platform_device.h>
> +#include <linux/rtc.h>
> +#include <linux/time.h>
> +
> +/* EXI registers */
> +#define EXICSR	0
> +#define EXICR	12
> +#define EXIDATA	16
> +
> +/* EXI register values */
> +#define EXICSR_DEV		0x380
> +	#define EXICSR_DEV1	0x100
> +#define EXICSR_CLK		0x070
> +	#define EXICSR_CLK_1MHZ	0x000
> +	#define EXICSR_CLK_2MHZ	0x010
> +	#define EXICSR_CLK_4MHZ	0x020
> +	#define EXICSR_CLK_8MHZ	0x030
> +	#define EXICSR_CLK_16MHZ 0x040
> +	#define EXICSR_CLK_32MHZ 0x050
> +#define EXICSR_INT		0x008
> +	#define EXICSR_INTSET	0x008
> +
> +#define EXICR_TSTART		0x001
> +#define EXICR_TRSMODE		0x002
> +	#define EXICR_TRSMODE_IMM 0x000
> +#define EXICR_TRSTYPE		0x00C
> +	#define EXICR_TRSTYPE_R	0x000
> +	#define EXICR_TRSTYPE_W	0x004
> +#define EXICR_TLEN		0x030
> +	#define EXICR_TLEN32	0x030
> +
> +/* EXI registers values to access the RTC */
> +#define RTC_EXICSR	(EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
> +#define RTC_EXICR_W	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
> +#define RTC_EXICR_R	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
> +#define RTC_EXIDATA_W	0x80000000
> +
> +/* RTC registers */
> +#define RTC_COUNTER	0x20000000
> +#define RTC_SRAM	0x20000100
> +#define RTC_SRAM_BIAS	0x20000400
> +#define RTC_SNAPSHOT	0x20400000
> +#define RTC_ONTMR	0x21000000
> +#define RTC_OFFTMR	0x21000100
> +#define RTC_TEST0	0x21000400
> +#define RTC_TEST1	0x21000500
> +#define RTC_TEST2	0x21000600
> +#define RTC_TEST3	0x21000700
> +#define RTC_CONTROL0	0x21000C00
> +#define RTC_CONTROL1	0x21000D00
> +
> +struct nintendo_rtc_drvdata {
> +	void __iomem *iob;
> +	u32 rtc_bias;
> +};
> +
> +static u32 exi_read(void __iomem *iob, u32 reg)
> +{
> +	u32 data;
> +
> +	/* The spin loops here loop about 15~16 times each, so there is no need
> +	 * to use a more expensive sleep method. */
> +
> +	/* Write register offset */
> +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> +	iowrite32be(reg, iob + EXIDATA);
> +	iowrite32be(RTC_EXICR_W, iob + EXICR);
> +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> +		cpu_relax();
> +
> +	/* Read data */
> +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> +	iowrite32be(RTC_EXICR_R, iob + EXICR);
> +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> +		cpu_relax();
> +	data = ioread32be(iob + EXIDATA);
> +
> +	/* Clear channel parameters */
> +	iowrite32be(0, iob + EXICSR);
> +
> +	return data;
> +}
> +
> +static void exi_write(void __iomem *iob, u32 reg, u32 data)
> +{
> +	/* The spin loops here loop about 15~16 times each, so there is no need
> +	 * to use a more expensive sleep method. */
> +
> +	/* Write register offset */
> +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> +	iowrite32be(reg | RTC_EXIDATA_W, iob + EXIDATA);
> +	iowrite32be(RTC_EXICR_W, iob + EXICR);
> +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> +		cpu_relax();
> +
> +	/* Write data */
> +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> +	iowrite32be(data, iob + EXIDATA);
> +	iowrite32be(RTC_EXICR_W, iob + EXICR);
> +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> +		cpu_relax();
> +
> +	/* Clear channel parameters */
> +	iowrite32be(0, iob + EXICSR);
> +}
> +
> +static int nintendo_rtc_read_time(struct device *dev, struct rtc_time *t)
> +{
> +	time64_t timestamp;
> +	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
> +
> +	/* Add the counter and the bias to obtain the timestamp */
> +	timestamp = exi_read(d->iob, RTC_COUNTER) + d->rtc_bias;
> +	rtc_time64_to_tm(timestamp, t);
> +
> +	return 0;
> +}
> +
> +static int nintendo_rtc_set_time(struct device *dev, struct rtc_time *t)
> +{
> +	time64_t timestamp;
> +	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
> +
> +	/* Subtract the timestamp and the bias to obtain the counter value */
> +	timestamp = rtc_tm_to_time64(t);
> +	exi_write(d->iob, RTC_COUNTER, timestamp - d->rtc_bias);

As you are able to update RTC_COUNTER, I'm not sure why you actually
need rtc_bias.

> +
> +	return 0;
> +}
> +
> +static const struct rtc_class_ops nintendo_rtc_ops = {
> +	.read_time      = nintendo_rtc_read_time,
> +	.set_time       = nintendo_rtc_set_time,
> +};
> +
> +#ifdef DEBUG
> +static void nintendo_rtc_dumpregs(void __iomem *iob)
> +{
> +	int i;
> +	u32 sram_addr = RTC_SRAM;
> +
> +	printk("RTC_COUNTER:  %08X\n", exi_read(iob, RTC_COUNTER));
> +	printk("RTC_SNAPSHOT: %08X\n", exi_read(iob, RTC_SNAPSHOT));
> +	printk("RTC_ONTMR:    %08X\n", exi_read(iob, RTC_ONTMR));
> +	printk("RTC_OFFTMR:   %08X\n", exi_read(iob, RTC_OFFTMR));
> +	printk("RTC_TEST0:    %08X\n", exi_read(iob, RTC_TEST0));
> +	printk("RTC_TEST1:    %08X\n", exi_read(iob, RTC_TEST1));
> +	printk("RTC_TEST2:    %08X\n", exi_read(iob, RTC_TEST2));
> +	printk("RTC_TEST3:    %08X\n", exi_read(iob, RTC_TEST3));
> +	printk("RTC_CONTROL0: %08X\n", exi_read(iob, RTC_CONTROL0));
> +	printk("RTC_CONTROL1: %08X\n", exi_read(iob, RTC_CONTROL1));
> +	printk("RTC_SRAM:\n");
> +	for(i = 0; i < 4; i++) {
> +		printk("%08X %08X %08X %08X\n",
> +		       exi_read(iob, sram_addr + 0x100 * 0),
> +		       exi_read(iob, sram_addr + 0x100 * 1),
> +		       exi_read(iob, sram_addr + 0x100 * 2),
> +		       exi_read(iob, sram_addr + 0x100 * 3));
> +		sram_addr += 0x400;

Something great to do would be to convert the driver to regmap, provding
custom regmap_read and regmap_write functions to access the EXI bus.
Then you'd get this directly in debugfs. And this could be split ou once
other drivers need access to the bus (I guess power/reset at some
point).

> +	}
> +}
> +#endif
> +
> +static int nintendo_rtc_read_offset_from_sram(struct nintendo_rtc_drvdata *d)
> +{
> +	struct device_node *np;
> +	int ret;
> +	struct resource res;
> +	void __iomem *hw_srnprot;
> +	u32 old;
> +
> +	np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
> +	if (!np) {
> +		pr_err("HW_SRNPROT not found\n");
> +		return -1;
> +	}
> +
> +	ret = of_address_to_resource(np, 0, &res);
> +	if (ret) {
> +		pr_err("no io memory range found\n");
> +		return -1;
> +	}
> +
> +	hw_srnprot = ioremap(res.start, resource_size(&res));
> +	old = ioread32be(hw_srnprot);
> +
> +	/* TODO: figure out why we use this magic constant.  I obtained it by
> +	 * reading the leftover value after boot, after IOSU already ran.
> +	 *
> +	 * On my Wii U, setting this register to 1 prevents the console from
> +	 * rebooting properly, so wiiubrew.org must be missing something.
> +	 *
> +	 * See https://wiiubrew.org/wiki/Hardware/Latte_registers
> +	 */
> +	if (old != 0x7bf)
> +		iowrite32be(0x7bf, hw_srnprot);
> +
> +#ifdef DEBUG
> +	nintendo_rtc_dumpregs(d->iob);
> +#endif
> +
> +	/* Get the offset from RTC SRAM.
> +	 *
> +	 * Its default location on the GameCube and on the Wii is in the SRAM,
> +	 * while on the Wii U the bootloader needs to fill it with the contents
> +	 * of /config/rtc.xml on the SLC (the eMMC).  We don’t do that from
> +	 * Linux since it requires implementing a proprietary filesystem and do
> +	 * file decryption, instead we require the bootloader to fill the same
> +	 * SRAM address as on previous consoles.
> +	 */
> +	d->rtc_bias = exi_read(d->iob, RTC_SRAM_BIAS);
> +
> +	/* Reset SRAM access to how it was before, our job here is done. */
> +	iowrite32be(old, hw_srnprot);
> +	iounmap(hw_srnprot);
> +
> +	return 0;
> +}
> +
> +static int nintendo_rtc_probe(struct platform_device *pdev) {
> +	struct device *dev = &pdev->dev;
> +	struct rtc_device *rtc;
> +	struct resource	*res;
> +	struct nintendo_rtc_drvdata *d;
> +	int ret;
> +
> +	d = devm_kzalloc(dev, sizeof(struct nintendo_rtc_drvdata), GFP_KERNEL);
> +	if (IS_ERR(d))
> +		return PTR_ERR(d);
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	d->iob = devm_ioremap_resource(dev, res);
> +	if (IS_ERR(d->iob))
> +		return PTR_ERR(d->iob);
> +
> +	ret = nintendo_rtc_read_offset_from_sram(d);
> +	if (ret)
> +		return ret;
> +	dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
> +
> +	dev_set_drvdata(dev, d);
> +
> +	rtc = devm_rtc_device_register(dev, dev_name(dev), &nintendo_rtc_ops,
> +				       THIS_MODULE);
> +	if (IS_ERR(rtc))
> +		return PTR_ERR(rtc);

Please use devm_rtc_allocate_device and devm_rtc_register_device and set
the RTC range properly. You claim it is not Y2038 aware but it seems to
be a 32bit counter which means it will count up to year 2106 (the RTC
value is necessarily unsigned) so I guess you should also fix that
comment.

Ideally, you should also expose LOW_BATT from RTC_CONTROL0 using the
VL_READ ioctl as I'm guessing many console will start to have a depleted
battery.

> +
> +	return 0;
> +}
> +
> +static const struct of_device_id nintendo_rtc_of_match[] = {
> +	{.compatible = "nintendo,latte-exi" },
> +	{.compatible = "nintendo,hollywood-exi" },
> +	{.compatible = "nintendo,flipper-exi" },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, nintendo_rtc_of_match);
> +
> +static struct platform_driver nintendo_rtc_driver = {
> +	.probe		= nintendo_rtc_probe,
> +	.driver		= {
> +		.name	= "rtc-nintendo",
> +		.of_match_table	= nintendo_rtc_of_match,
> +	},
> +};
> +module_platform_driver(nintendo_rtc_driver);
> +
> +MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
> +MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
> +MODULE_LICENSE("GPL");
> -- 
> 2.33.1
> 

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-27 16:45 ` Alexandre Belloni
@ 2021-10-27 17:05   ` Emmanuel Gil Peyrot
  2021-10-27 17:09     ` Emmanuel Gil Peyrot
  2021-10-27 20:32     ` Alexandre Belloni
  2021-10-28 20:32   ` Jonathan Neuschäfer
  1 sibling, 2 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 17:05 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Emmanuel Gil Peyrot, Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, linux-kernel, linux-rtc

[-- Attachment #1: Type: text/plain, Size: 16279 bytes --]

On Wed, Oct 27, 2021 at 06:45:54PM +0200, Alexandre Belloni wrote:
> Hello Emmanuel,
> 
> Thanks for the patch!

Hi, and thanks for your review!

> 
> On 15/10/2021 00:05:24+0200, Emmanuel Gil Peyrot wrote:
> > These three consoles share a device, the MX23L4005, which contains a
> > clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> > (similar to SPI) on channel 0, device 1.  This driver allows it to be
> > used as a Linux RTC device, where time can be read and set.
> > 
> > The hardware also exposes two timers, one which shuts down the console
> > and one which powers it on, but these aren’t supported currently.
> > 
> > On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
> > encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
> > order to avoid having to implement all that, this driver assumes a
> > bootloader will parse this XML file and write the bias into the SRAM, at
> > the same location the other two consoles have it.
> > 
> > Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> > ---
> >  drivers/rtc/Kconfig        |  10 ++
> >  drivers/rtc/Makefile       |   1 +
> >  drivers/rtc/rtc-nintendo.c | 305 +++++++++++++++++++++++++++++++++++++
> 
> I'm not convinced this is a good name, seeing that the switch will
> certainly not use this driver (neither is the snes mini).

Other subsystem maintainers have requested this to be changed, so I
reflected it here too.  For instance hid requested a Wii U-specific
driver to be merged with the Switch one.

Would rtc-gamecube be fine then?  So far I have only tested on Wii U,
but this driver is expected to support all three generations of
GameCube, Wii and Wii U.

> 
> >  3 files changed, 316 insertions(+)
> >  create mode 100644 drivers/rtc/rtc-nintendo.c
> > 
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index 914497abeef9..f2a15429e4b1 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -1214,6 +1214,16 @@ config RTC_DRV_V3020
> >  	  This driver can also be built as a module. If so, the module
> >  	  will be called rtc-v3020.
> >  
> > +config RTC_DRV_NINTENDO
> > +	tristate "Nintendo GameCube, Wii and Wii U RTC"
> > +	depends on GAMECUBE || WII || WIIU || COMPILE_TEST
> 
> CONFIG_WIIU doesn't seem to exist or am I missing something?

Indeed, we haven’t submitted the platform yet, only some of the self-
contained drivers.  I will remove it for now, and will add it again once
CONFIG_WIIU is added.

> 
> > +	help
> > +	  If you say yes here you will get support for the RTC subsystem
> > +	  of the Nintendo GameCube, Wii and Wii U.
> > +
> > +	  This driver can also be built as a module. If so, the module
> > +	  will be called "rtc-nintendo".
> > +
> >  config RTC_DRV_WM831X
> >  	tristate "Wolfson Microelectronics WM831x RTC"
> >  	depends on MFD_WM831X
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > index 2dd0dd956b0e..0c7c0e7d9637 100644
> > --- a/drivers/rtc/Makefile
> > +++ b/drivers/rtc/Makefile
> > @@ -108,6 +108,7 @@ obj-$(CONFIG_RTC_DRV_MT7622)	+= rtc-mt7622.o
> >  obj-$(CONFIG_RTC_DRV_MV)	+= rtc-mv.o
> >  obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
> >  obj-$(CONFIG_RTC_DRV_MXC_V2)	+= rtc-mxc_v2.o
> > +obj-$(CONFIG_RTC_DRV_NINTENDO)	+= rtc-nintendo.o
> >  obj-$(CONFIG_RTC_DRV_NTXEC)	+= rtc-ntxec.o
> >  obj-$(CONFIG_RTC_DRV_OMAP)	+= rtc-omap.o
> >  obj-$(CONFIG_RTC_DRV_OPAL)	+= rtc-opal.o
> > diff --git a/drivers/rtc/rtc-nintendo.c b/drivers/rtc/rtc-nintendo.c
> > new file mode 100644
> > index 000000000000..11bea40ca13d
> > --- /dev/null
> > +++ b/drivers/rtc/rtc-nintendo.c
> 
> You have a bunch of checkpatch warnings, can you fix those?

Will do, sorry about that.

> 
> > @@ -0,0 +1,305 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Nintendo GameCube, Wii and Wii U RTC driver
> > + *
> > + * This driver is for the MX23L4005, more specifically its real-time clock and
> > + * SRAM storage.  The value returned by the RTC counter must be added with the
> > + * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
> > + * /config/rtc.xml (on the Wii U).  The latter being very impractical to access
> > + * from Linux, this driver assumes the bootloader has read it and stored it in
> > + * SRAM like for the other two consoles.
> > + *
> > + * This device sits on a bus named EXI (which is similar to SPI), channel 0,
> > + * device 1.  This driver assumes no other user of the EXI bus, which is
> > + * currently the case but would have to be reworked to add support for other
> > + * GameCube hardware exposed on this bus.
> > + *
> > + * Note that this device is very much not Y2k38 aware.
> > + *
> > + * References:
> > + * - https://wiiubrew.org/wiki/Hardware/RTC
> > + * - https://wiibrew.org/wiki/MX23L4005
> > + *
> > + * Copyright (C) 2018 rw-r-r-0644
> > + * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> > + *
> > + * Based on rtc-gcn.c
> > + * Copyright (C) 2004-2009 The GameCube Linux Team
> > + * Copyright (C) 2005,2008,2009 Albert Herranz
> > + * Based on gamecube_time.c from Torben Nielsen.
> > + */
> > +
> > +#include <linux/init.h>
> > +#include <linux/module.h>
> > +#include <linux/of.h>
> > +#include <linux/of_address.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/rtc.h>
> > +#include <linux/time.h>
> > +
> > +/* EXI registers */
> > +#define EXICSR	0
> > +#define EXICR	12
> > +#define EXIDATA	16
> > +
> > +/* EXI register values */
> > +#define EXICSR_DEV		0x380
> > +	#define EXICSR_DEV1	0x100
> > +#define EXICSR_CLK		0x070
> > +	#define EXICSR_CLK_1MHZ	0x000
> > +	#define EXICSR_CLK_2MHZ	0x010
> > +	#define EXICSR_CLK_4MHZ	0x020
> > +	#define EXICSR_CLK_8MHZ	0x030
> > +	#define EXICSR_CLK_16MHZ 0x040
> > +	#define EXICSR_CLK_32MHZ 0x050
> > +#define EXICSR_INT		0x008
> > +	#define EXICSR_INTSET	0x008
> > +
> > +#define EXICR_TSTART		0x001
> > +#define EXICR_TRSMODE		0x002
> > +	#define EXICR_TRSMODE_IMM 0x000
> > +#define EXICR_TRSTYPE		0x00C
> > +	#define EXICR_TRSTYPE_R	0x000
> > +	#define EXICR_TRSTYPE_W	0x004
> > +#define EXICR_TLEN		0x030
> > +	#define EXICR_TLEN32	0x030
> > +
> > +/* EXI registers values to access the RTC */
> > +#define RTC_EXICSR	(EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
> > +#define RTC_EXICR_W	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
> > +#define RTC_EXICR_R	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
> > +#define RTC_EXIDATA_W	0x80000000
> > +
> > +/* RTC registers */
> > +#define RTC_COUNTER	0x20000000
> > +#define RTC_SRAM	0x20000100
> > +#define RTC_SRAM_BIAS	0x20000400
> > +#define RTC_SNAPSHOT	0x20400000
> > +#define RTC_ONTMR	0x21000000
> > +#define RTC_OFFTMR	0x21000100
> > +#define RTC_TEST0	0x21000400
> > +#define RTC_TEST1	0x21000500
> > +#define RTC_TEST2	0x21000600
> > +#define RTC_TEST3	0x21000700
> > +#define RTC_CONTROL0	0x21000C00
> > +#define RTC_CONTROL1	0x21000D00
> > +
> > +struct nintendo_rtc_drvdata {
> > +	void __iomem *iob;
> > +	u32 rtc_bias;
> > +};
> > +
> > +static u32 exi_read(void __iomem *iob, u32 reg)
> > +{
> > +	u32 data;
> > +
> > +	/* The spin loops here loop about 15~16 times each, so there is no need
> > +	 * to use a more expensive sleep method. */
> > +
> > +	/* Write register offset */
> > +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> > +	iowrite32be(reg, iob + EXIDATA);
> > +	iowrite32be(RTC_EXICR_W, iob + EXICR);
> > +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> > +		cpu_relax();
> > +
> > +	/* Read data */
> > +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> > +	iowrite32be(RTC_EXICR_R, iob + EXICR);
> > +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> > +		cpu_relax();
> > +	data = ioread32be(iob + EXIDATA);
> > +
> > +	/* Clear channel parameters */
> > +	iowrite32be(0, iob + EXICSR);
> > +
> > +	return data;
> > +}
> > +
> > +static void exi_write(void __iomem *iob, u32 reg, u32 data)
> > +{
> > +	/* The spin loops here loop about 15~16 times each, so there is no need
> > +	 * to use a more expensive sleep method. */
> > +
> > +	/* Write register offset */
> > +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> > +	iowrite32be(reg | RTC_EXIDATA_W, iob + EXIDATA);
> > +	iowrite32be(RTC_EXICR_W, iob + EXICR);
> > +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> > +		cpu_relax();
> > +
> > +	/* Write data */
> > +	iowrite32be(RTC_EXICSR, iob + EXICSR);
> > +	iowrite32be(data, iob + EXIDATA);
> > +	iowrite32be(RTC_EXICR_W, iob + EXICR);
> > +	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
> > +		cpu_relax();
> > +
> > +	/* Clear channel parameters */
> > +	iowrite32be(0, iob + EXICSR);
> > +}
> > +
> > +static int nintendo_rtc_read_time(struct device *dev, struct rtc_time *t)
> > +{
> > +	time64_t timestamp;
> > +	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
> > +
> > +	/* Add the counter and the bias to obtain the timestamp */
> > +	timestamp = exi_read(d->iob, RTC_COUNTER) + d->rtc_bias;
> > +	rtc_time64_to_tm(timestamp, t);
> > +
> > +	return 0;
> > +}
> > +
> > +static int nintendo_rtc_set_time(struct device *dev, struct rtc_time *t)
> > +{
> > +	time64_t timestamp;
> > +	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
> > +
> > +	/* Subtract the timestamp and the bias to obtain the counter value */
> > +	timestamp = rtc_tm_to_time64(t);
> > +	exi_write(d->iob, RTC_COUNTER, timestamp - d->rtc_bias);
> 
> As you are able to update RTC_COUNTER, I'm not sure why you actually
> need rtc_bias.

The proprietary firmware sets the counter based on the bias, so if we
want to get the correct time out of the box we have to sum them.  It
wouldn’t even be possible to set bias on all of the consoles from Linux,
for instance on the Wii U it is stored in an XML file, in a proprietary
filesystem, and encrypted (using keys for which the drivers are already
in mainline at least).

> 
> > +
> > +	return 0;
> > +}
> > +
> > +static const struct rtc_class_ops nintendo_rtc_ops = {
> > +	.read_time      = nintendo_rtc_read_time,
> > +	.set_time       = nintendo_rtc_set_time,
> > +};
> > +
> > +#ifdef DEBUG
> > +static void nintendo_rtc_dumpregs(void __iomem *iob)
> > +{
> > +	int i;
> > +	u32 sram_addr = RTC_SRAM;
> > +
> > +	printk("RTC_COUNTER:  %08X\n", exi_read(iob, RTC_COUNTER));
> > +	printk("RTC_SNAPSHOT: %08X\n", exi_read(iob, RTC_SNAPSHOT));
> > +	printk("RTC_ONTMR:    %08X\n", exi_read(iob, RTC_ONTMR));
> > +	printk("RTC_OFFTMR:   %08X\n", exi_read(iob, RTC_OFFTMR));
> > +	printk("RTC_TEST0:    %08X\n", exi_read(iob, RTC_TEST0));
> > +	printk("RTC_TEST1:    %08X\n", exi_read(iob, RTC_TEST1));
> > +	printk("RTC_TEST2:    %08X\n", exi_read(iob, RTC_TEST2));
> > +	printk("RTC_TEST3:    %08X\n", exi_read(iob, RTC_TEST3));
> > +	printk("RTC_CONTROL0: %08X\n", exi_read(iob, RTC_CONTROL0));
> > +	printk("RTC_CONTROL1: %08X\n", exi_read(iob, RTC_CONTROL1));
> > +	printk("RTC_SRAM:\n");
> > +	for(i = 0; i < 4; i++) {
> > +		printk("%08X %08X %08X %08X\n",
> > +		       exi_read(iob, sram_addr + 0x100 * 0),
> > +		       exi_read(iob, sram_addr + 0x100 * 1),
> > +		       exi_read(iob, sram_addr + 0x100 * 2),
> > +		       exi_read(iob, sram_addr + 0x100 * 3));
> > +		sram_addr += 0x400;
> 
> Something great to do would be to convert the driver to regmap, provding
> custom regmap_read and regmap_write functions to access the EXI bus.
> Then you'd get this directly in debugfs. And this could be split ou once
> other drivers need access to the bus (I guess power/reset at some
> point).

Will do, I wasn’t aware of regmap, thanks!

> 
> > +	}
> > +}
> > +#endif
> > +
> > +static int nintendo_rtc_read_offset_from_sram(struct nintendo_rtc_drvdata *d)
> > +{
> > +	struct device_node *np;
> > +	int ret;
> > +	struct resource res;
> > +	void __iomem *hw_srnprot;
> > +	u32 old;
> > +
> > +	np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
> > +	if (!np) {
> > +		pr_err("HW_SRNPROT not found\n");
> > +		return -1;
> > +	}
> > +
> > +	ret = of_address_to_resource(np, 0, &res);
> > +	if (ret) {
> > +		pr_err("no io memory range found\n");
> > +		return -1;
> > +	}
> > +
> > +	hw_srnprot = ioremap(res.start, resource_size(&res));
> > +	old = ioread32be(hw_srnprot);
> > +
> > +	/* TODO: figure out why we use this magic constant.  I obtained it by
> > +	 * reading the leftover value after boot, after IOSU already ran.
> > +	 *
> > +	 * On my Wii U, setting this register to 1 prevents the console from
> > +	 * rebooting properly, so wiiubrew.org must be missing something.
> > +	 *
> > +	 * See https://wiiubrew.org/wiki/Hardware/Latte_registers
> > +	 */
> > +	if (old != 0x7bf)
> > +		iowrite32be(0x7bf, hw_srnprot);
> > +
> > +#ifdef DEBUG
> > +	nintendo_rtc_dumpregs(d->iob);
> > +#endif
> > +
> > +	/* Get the offset from RTC SRAM.
> > +	 *
> > +	 * Its default location on the GameCube and on the Wii is in the SRAM,
> > +	 * while on the Wii U the bootloader needs to fill it with the contents
> > +	 * of /config/rtc.xml on the SLC (the eMMC).  We don’t do that from
> > +	 * Linux since it requires implementing a proprietary filesystem and do
> > +	 * file decryption, instead we require the bootloader to fill the same
> > +	 * SRAM address as on previous consoles.
> > +	 */
> > +	d->rtc_bias = exi_read(d->iob, RTC_SRAM_BIAS);
> > +
> > +	/* Reset SRAM access to how it was before, our job here is done. */
> > +	iowrite32be(old, hw_srnprot);
> > +	iounmap(hw_srnprot);
> > +
> > +	return 0;
> > +}
> > +
> > +static int nintendo_rtc_probe(struct platform_device *pdev) {
> > +	struct device *dev = &pdev->dev;
> > +	struct rtc_device *rtc;
> > +	struct resource	*res;
> > +	struct nintendo_rtc_drvdata *d;
> > +	int ret;
> > +
> > +	d = devm_kzalloc(dev, sizeof(struct nintendo_rtc_drvdata), GFP_KERNEL);
> > +	if (IS_ERR(d))
> > +		return PTR_ERR(d);
> > +
> > +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > +	d->iob = devm_ioremap_resource(dev, res);
> > +	if (IS_ERR(d->iob))
> > +		return PTR_ERR(d->iob);
> > +
> > +	ret = nintendo_rtc_read_offset_from_sram(d);
> > +	if (ret)
> > +		return ret;
> > +	dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
> > +
> > +	dev_set_drvdata(dev, d);
> > +
> > +	rtc = devm_rtc_device_register(dev, dev_name(dev), &nintendo_rtc_ops,
> > +				       THIS_MODULE);
> > +	if (IS_ERR(rtc))
> > +		return PTR_ERR(rtc);
> 
> Please use devm_rtc_allocate_device and devm_rtc_register_device and set
> the RTC range properly. You claim it is not Y2038 aware but it seems to
> be a 32bit counter which means it will count up to year 2106 (the RTC
> value is necessarily unsigned) so I guess you should also fix that
> comment.

Hmm, indeed, I’ll fix that.

> 
> Ideally, you should also expose LOW_BATT from RTC_CONTROL0 using the
> VL_READ ioctl as I'm guessing many console will start to have a depleted
> battery.

I won’t be able to test that yet, but I’ll try to implement it as it is
documented.

> 
> > +
> > +	return 0;
> > +}
> > +
> > +static const struct of_device_id nintendo_rtc_of_match[] = {
> > +	{.compatible = "nintendo,latte-exi" },
> > +	{.compatible = "nintendo,hollywood-exi" },
> > +	{.compatible = "nintendo,flipper-exi" },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(of, nintendo_rtc_of_match);
> > +
> > +static struct platform_driver nintendo_rtc_driver = {
> > +	.probe		= nintendo_rtc_probe,
> > +	.driver		= {
> > +		.name	= "rtc-nintendo",
> > +		.of_match_table	= nintendo_rtc_of_match,
> > +	},
> > +};
> > +module_platform_driver(nintendo_rtc_driver);
> > +
> > +MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
> > +MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
> > +MODULE_LICENSE("GPL");
> > -- 
> > 2.33.1
> > 
> 
> -- 
> Alexandre Belloni, co-owner and COO, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-27 17:05   ` Emmanuel Gil Peyrot
@ 2021-10-27 17:09     ` Emmanuel Gil Peyrot
  2021-10-27 20:32     ` Alexandre Belloni
  1 sibling, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 17:09 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Alexandre Belloni, Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, linux-kernel, linux-rtc

[-- Attachment #1: Type: text/plain, Size: 990 bytes --]

On Wed, Oct 27, 2021 at 07:05:27PM +0200, Emmanuel Gil Peyrot wrote:
> On Wed, Oct 27, 2021 at 06:45:54PM +0200, Alexandre Belloni wrote:
[…]
> > >  drivers/rtc/rtc-nintendo.c | 305 +++++++++++++++++++++++++++++++++++++
> > 
> > I'm not convinced this is a good name, seeing that the switch will
> > certainly not use this driver (neither is the snes mini).
> 
> Other subsystem maintainers have requested this to be changed, so I
> reflected it here too.  For instance hid requested a Wii U-specific
> driver to be merged with the Switch one.
> 
> Would rtc-gamecube be fine then?  So far I have only tested on Wii U,
> but this driver is expected to support all three generations of
> GameCube, Wii and Wii U.
> 

Another thing to note is that the Switch would be a Nvidia platform, and
the NES Mini an Allwinner platform.  This would leave the DS/DSi/3DS to
be a custom SoC with their own RTC implementation different from this
one.

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-27 17:05   ` Emmanuel Gil Peyrot
  2021-10-27 17:09     ` Emmanuel Gil Peyrot
@ 2021-10-27 20:32     ` Alexandre Belloni
  1 sibling, 0 replies; 28+ messages in thread
From: Alexandre Belloni @ 2021-10-27 20:32 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, linux-kernel, linux-rtc

On 27/10/2021 19:05:27+0200, Emmanuel Gil Peyrot wrote:
> > On 15/10/2021 00:05:24+0200, Emmanuel Gil Peyrot wrote:
> > > These three consoles share a device, the MX23L4005, which contains a
> > > clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> > > (similar to SPI) on channel 0, device 1.  This driver allows it to be
> > > used as a Linux RTC device, where time can be read and set.
> > > 
> > > The hardware also exposes two timers, one which shuts down the console
> > > and one which powers it on, but these aren’t supported currently.
> > > 
> > > On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
> > > encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
> > > order to avoid having to implement all that, this driver assumes a
> > > bootloader will parse this XML file and write the bias into the SRAM, at
> > > the same location the other two consoles have it.
> > > 
> > > Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> > > ---
> > >  drivers/rtc/Kconfig        |  10 ++
> > >  drivers/rtc/Makefile       |   1 +
> > >  drivers/rtc/rtc-nintendo.c | 305 +++++++++++++++++++++++++++++++++++++
> > 
> > I'm not convinced this is a good name, seeing that the switch will
> > certainly not use this driver (neither is the snes mini).
> 
> Other subsystem maintainers have requested this to be changed, so I
> reflected it here too.  For instance hid requested a Wii U-specific
> driver to be merged with the Switch one.
> 
> Would rtc-gamecube be fine then?  So far I have only tested on Wii U,
> but this driver is expected to support all three generations of
> GameCube, Wii and Wii U.
> 

I think this is appropriate, that would have been my suggestion.

> > > +static int nintendo_rtc_set_time(struct device *dev, struct rtc_time *t)
> > > +{
> > > +	time64_t timestamp;
> > > +	struct nintendo_rtc_drvdata *d = dev_get_drvdata(dev);
> > > +
> > > +	/* Subtract the timestamp and the bias to obtain the counter value */
> > > +	timestamp = rtc_tm_to_time64(t);
> > > +	exi_write(d->iob, RTC_COUNTER, timestamp - d->rtc_bias);
> > 
> > As you are able to update RTC_COUNTER, I'm not sure why you actually
> > need rtc_bias.
> 
> The proprietary firmware sets the counter based on the bias, so if we
> want to get the correct time out of the box we have to sum them.  It
> wouldn’t even be possible to set bias on all of the consoles from Linux,
> for instance on the Wii U it is stored in an XML file, in a proprietary
> filesystem, and encrypted (using keys for which the drivers are already
> in mainline at least).
> 

Ok, fine by me.

> > > +#ifdef DEBUG
> > > +static void nintendo_rtc_dumpregs(void __iomem *iob)
> > > +{
> > > +	int i;
> > > +	u32 sram_addr = RTC_SRAM;
> > > +
> > > +	printk("RTC_COUNTER:  %08X\n", exi_read(iob, RTC_COUNTER));
> > > +	printk("RTC_SNAPSHOT: %08X\n", exi_read(iob, RTC_SNAPSHOT));
> > > +	printk("RTC_ONTMR:    %08X\n", exi_read(iob, RTC_ONTMR));
> > > +	printk("RTC_OFFTMR:   %08X\n", exi_read(iob, RTC_OFFTMR));
> > > +	printk("RTC_TEST0:    %08X\n", exi_read(iob, RTC_TEST0));
> > > +	printk("RTC_TEST1:    %08X\n", exi_read(iob, RTC_TEST1));
> > > +	printk("RTC_TEST2:    %08X\n", exi_read(iob, RTC_TEST2));
> > > +	printk("RTC_TEST3:    %08X\n", exi_read(iob, RTC_TEST3));
> > > +	printk("RTC_CONTROL0: %08X\n", exi_read(iob, RTC_CONTROL0));
> > > +	printk("RTC_CONTROL1: %08X\n", exi_read(iob, RTC_CONTROL1));
> > > +	printk("RTC_SRAM:\n");
> > > +	for(i = 0; i < 4; i++) {
> > > +		printk("%08X %08X %08X %08X\n",
> > > +		       exi_read(iob, sram_addr + 0x100 * 0),
> > > +		       exi_read(iob, sram_addr + 0x100 * 1),
> > > +		       exi_read(iob, sram_addr + 0x100 * 2),
> > > +		       exi_read(iob, sram_addr + 0x100 * 3));
> > > +		sram_addr += 0x400;
> > 
> > Something great to do would be to convert the driver to regmap, provding
> > custom regmap_read and regmap_write functions to access the EXI bus.
> > Then you'd get this directly in debugfs. And this could be split ou once
> > other drivers need access to the bus (I guess power/reset at some
> > point).
> 
> Will do, I wasn’t aware of regmap, thanks!
> 

This is feature creep pushed to the max, I would take your driver even
if you don't do that.

> > 
> > Ideally, you should also expose LOW_BATT from RTC_CONTROL0 using the
> > VL_READ ioctl as I'm guessing many console will start to have a depleted
> > battery.
> 
> I won’t be able to test that yet, but I’ll try to implement it as it is
> documented.
> 

This can also come in a second patch. I'll try to boot up my own
(platinum edition) GC, I'm wondering what is the battery status :)


-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* [PATCH v2 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-14 22:05 [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
  2021-10-27 16:45 ` Alexandre Belloni
@ 2021-10-27 22:35 ` Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
                     ` (5 more replies)
  1 sibling, 6 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 22:35 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

These three consoles share a device, the MX23L4005, which contains a
clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
(similar to SPI) on channel 0, device 1.  This driver allows it to be
used as a Linux RTC device, where time can be read and set.

The hardware also exposes two timers, one which shuts down the console
and one which powers it on, but these aren’t supported currently.

On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
order to avoid having to implement all that, this driver assumes a
bootloader will parse this XML file and write the bias into the SRAM, at
the same location the other two consoles have it.

Changes since v1:
- Rename the driver to rtc-gamecube.
- Switch to the regmap API for debugfs support.
- Report low battery and unstable power as invalid data.
- Remove Wii U support in Kconfig, nothing specific to this console
  needs to be changed in the code.
- Don’t attempt to change HW_SRNPROT on the GameCube, this register
  doesn’t exist so we can use SRAM just fine without doing anything.
- Add needed changes to the wii device tree.
- Enable this driver on the gamecube and wii platforms.

Emmanuel Gil Peyrot (5):
  rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
  rtc: gamecube: Report low battery as invalid data
  powerpc: wii.dts: Expose HW_SRNPROT on this platform
  powerpc: gamecube_defconfig: Enable the RTC driver
  powerpc: wii_defconfig: Enable the RTC driver

 arch/powerpc/boot/dts/wii.dts           |   5 +
 arch/powerpc/configs/gamecube_defconfig |   2 +-
 arch/powerpc/configs/wii_defconfig      |   2 +-
 drivers/rtc/Kconfig                     |  11 +
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-gamecube.c              | 377 ++++++++++++++++++++++++
 6 files changed, 396 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-gamecube.c

-- 
2.33.1


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

* [PATCH v2 1/5] rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
@ 2021-10-27 22:35   ` Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 22:35 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

These three consoles share a device, the MX23L4005, which contains a
clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
(similar to SPI) on channel 0, device 1.  This driver allows it to be
used as a Linux RTC device, where time can be read and set.

The hardware also exposes two timers, one which shuts down the console
and one which powers it on, but these aren’t supported currently.

On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
order to avoid having to implement all that, this driver assumes a
bootloader will parse this XML file and write the bias into the SRAM, at
the same location the other two consoles have it.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/rtc/Kconfig        |  11 ++
 drivers/rtc/Makefile       |   1 +
 drivers/rtc/rtc-gamecube.c | 347 +++++++++++++++++++++++++++++++++++++
 3 files changed, 359 insertions(+)
 create mode 100644 drivers/rtc/rtc-gamecube.c

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 914497abeef9..503089ca370d 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -1214,6 +1214,17 @@ config RTC_DRV_V3020
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-v3020.
 
+config RTC_DRV_GAMECUBE
+	tristate "Nintendo GameCube, Wii and Wii U RTC"
+	depends on GAMECUBE || WII || COMPILE_TEST
+	select REGMAP
+	help
+	  If you say yes here you will get support for the RTC subsystem
+	  of the Nintendo GameCube, Wii and Wii U.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called "rtc-gamecube".
+
 config RTC_DRV_WM831X
 	tristate "Wolfson Microelectronics WM831x RTC"
 	depends on MFD_WM831X
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 2dd0dd956b0e..d781aaf0909c 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -108,6 +108,7 @@ obj-$(CONFIG_RTC_DRV_MT7622)	+= rtc-mt7622.o
 obj-$(CONFIG_RTC_DRV_MV)	+= rtc-mv.o
 obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
 obj-$(CONFIG_RTC_DRV_MXC_V2)	+= rtc-mxc_v2.o
+obj-$(CONFIG_RTC_DRV_GAMECUBE)	+= rtc-gamecube.o
 obj-$(CONFIG_RTC_DRV_NTXEC)	+= rtc-ntxec.o
 obj-$(CONFIG_RTC_DRV_OMAP)	+= rtc-omap.o
 obj-$(CONFIG_RTC_DRV_OPAL)	+= rtc-opal.o
diff --git a/drivers/rtc/rtc-gamecube.c b/drivers/rtc/rtc-gamecube.c
new file mode 100644
index 000000000000..e8260c82c07d
--- /dev/null
+++ b/drivers/rtc/rtc-gamecube.c
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nintendo GameCube, Wii and Wii U RTC driver
+ *
+ * This driver is for the MX23L4005, more specifically its real-time clock and
+ * SRAM storage.  The value returned by the RTC counter must be added with the
+ * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
+ * /config/rtc.xml (on the Wii U).  The latter being very impractical to access
+ * from Linux, this driver assumes the bootloader has read it and stored it in
+ * SRAM like for the other two consoles.
+ *
+ * This device sits on a bus named EXI (which is similar to SPI), channel 0,
+ * device 1.  This driver assumes no other user of the EXI bus, which is
+ * currently the case but would have to be reworked to add support for other
+ * GameCube hardware exposed on this bus.
+ *
+ * References:
+ * - https://wiiubrew.org/wiki/Hardware/RTC
+ * - https://wiibrew.org/wiki/MX23L4005
+ *
+ * Copyright (C) 2018 rw-r-r-0644
+ * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ *
+ * Based on rtc-gcn.c
+ * Copyright (C) 2004-2009 The GameCube Linux Team
+ * Copyright (C) 2005,2008,2009 Albert Herranz
+ * Based on gamecube_time.c from Torben Nielsen.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/rtc.h>
+#include <linux/time.h>
+
+/* EXI registers */
+#define EXICSR	0
+#define EXICR	12
+#define EXIDATA	16
+
+/* EXI register values */
+#define EXICSR_DEV		0x380
+	#define EXICSR_DEV1	0x100
+#define EXICSR_CLK		0x070
+	#define EXICSR_CLK_1MHZ	0x000
+	#define EXICSR_CLK_2MHZ	0x010
+	#define EXICSR_CLK_4MHZ	0x020
+	#define EXICSR_CLK_8MHZ	0x030
+	#define EXICSR_CLK_16MHZ 0x040
+	#define EXICSR_CLK_32MHZ 0x050
+#define EXICSR_INT		0x008
+	#define EXICSR_INTSET	0x008
+
+#define EXICR_TSTART		0x001
+#define EXICR_TRSMODE		0x002
+	#define EXICR_TRSMODE_IMM 0x000
+#define EXICR_TRSTYPE		0x00C
+	#define EXICR_TRSTYPE_R	0x000
+	#define EXICR_TRSTYPE_W	0x004
+#define EXICR_TLEN		0x030
+	#define EXICR_TLEN32	0x030
+
+/* EXI registers values to access the RTC */
+#define RTC_EXICSR	(EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
+#define RTC_EXICR_W	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
+#define RTC_EXICR_R	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
+#define RTC_EXIDATA_W	0x80000000
+
+/* RTC registers */
+#define RTC_COUNTER	0x200000
+#define RTC_SRAM	0x200001
+#define RTC_SRAM_BIAS	0x200004
+#define RTC_SNAPSHOT	0x204000
+#define RTC_ONTMR	0x210000
+#define RTC_OFFTMR	0x210001
+#define RTC_TEST0	0x210004
+#define RTC_TEST1	0x210005
+#define RTC_TEST2	0x210006
+#define RTC_TEST3	0x210007
+#define RTC_CONTROL0	0x21000c
+#define RTC_CONTROL1	0x21000d
+
+struct priv {
+	struct regmap *regmap;
+	void __iomem *iob;
+	u32 rtc_bias;
+};
+
+static int exi_read(void *context, u32 reg, u32 *data)
+{
+	struct priv *d = (struct priv *)context;
+	void __iomem *iob = d->iob;
+
+	/* The spin loops here loop about 15~16 times each, so there is no need
+	 * to use a more expensive sleep method.
+	 */
+
+	/* Write register offset */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(reg << 8, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Read data */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(RTC_EXICR_R, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+	*data = ioread32be(iob + EXIDATA);
+
+	/* Clear channel parameters */
+	iowrite32be(0, iob + EXICSR);
+
+	return 0;
+}
+
+static int exi_write(void *context, u32 reg, u32 data)
+{
+	struct priv *d = (struct priv *)context;
+	void __iomem *iob = d->iob;
+
+	/* The spin loops here loop about 15~16 times each, so there is no need
+	 * to use a more expensive sleep method.
+	 */
+
+	/* Write register offset */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(RTC_EXIDATA_W | (reg << 8), iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Write data */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(data, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Clear channel parameters */
+	iowrite32be(0, iob + EXICSR);
+
+	return 0;
+}
+
+static const struct regmap_bus exi_bus = {
+	/* TODO: is that true?  Not that it matters here, but still. */
+	.fast_io = true,
+	.reg_read = exi_read,
+	.reg_write = exi_write,
+};
+
+static int gamecube_rtc_read_time(struct device *dev, struct rtc_time *t)
+{
+	struct priv *d = dev_get_drvdata(dev);
+	int ret;
+	u32 counter;
+	time64_t timestamp;
+
+	ret = regmap_read(d->regmap, RTC_COUNTER, &counter);
+	if (ret)
+		return ret;
+
+	/* Add the counter and the bias to obtain the timestamp */
+	timestamp = (time64_t)d->rtc_bias + counter;
+	rtc_time64_to_tm(timestamp, t);
+
+	return 0;
+}
+
+static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
+{
+	struct priv *d = dev_get_drvdata(dev);
+	time64_t timestamp;
+
+	/* Subtract the timestamp and the bias to obtain the counter value */
+	timestamp = rtc_tm_to_time64(t);
+	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
+}
+
+static const struct rtc_class_ops gamecube_rtc_ops = {
+	.read_time	= gamecube_rtc_read_time,
+	.set_time	= gamecube_rtc_set_time,
+};
+
+static int gamecube_rtc_read_offset_from_sram(struct priv *d)
+{
+	struct device_node *np;
+	int ret;
+	struct resource res;
+	void __iomem *hw_srnprot;
+	u32 old;
+
+	np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
+	if (!np)
+		np = of_find_compatible_node(NULL, NULL,
+					     "nintendo,hollywood-srnprot");
+	if (!np) {
+		pr_info("HW_SRNPROT not found, assuming a GameCube\n");
+		return regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
+	}
+
+	ret = of_address_to_resource(np, 0, &res);
+	if (ret) {
+		pr_err("no io memory range found\n");
+		return -1;
+	}
+
+	hw_srnprot = ioremap(res.start, resource_size(&res));
+	old = ioread32be(hw_srnprot);
+
+	/* TODO: figure out why we use this magic constant.  I obtained it by
+	 * reading the leftover value after boot, after IOSU already ran.
+	 *
+	 * On my Wii U, setting this register to 1 prevents the console from
+	 * rebooting properly, so wiiubrew.org must be missing something.
+	 *
+	 * See https://wiiubrew.org/wiki/Hardware/Latte_registers
+	 */
+	if (old != 0x7bf)
+		iowrite32be(0x7bf, hw_srnprot);
+
+	/* Get the offset from RTC SRAM.
+	 *
+	 * Its default location on the GameCube and on the Wii is in the SRAM,
+	 * while on the Wii U the bootloader needs to fill it with the contents
+	 * of /config/rtc.xml on the SLC (the eMMC).  We don’t do that from
+	 * Linux since it requires implementing a proprietary filesystem and do
+	 * file decryption, instead we require the bootloader to fill the same
+	 * SRAM address as on previous consoles.
+	 */
+	ret = regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
+	if (ret) {
+		pr_err("failed to get the RTC bias\n");
+		return -1;
+	}
+
+	/* Reset SRAM access to how it was before, our job here is done. */
+	if (old != 0x7bf)
+		iowrite32be(old, hw_srnprot);
+	iounmap(hw_srnprot);
+
+	return 0;
+}
+
+static const struct regmap_range rtc_rd_ranges[] = {
+	regmap_reg_range(0x200000, 0x200010),
+	regmap_reg_range(0x204000, 0x204000),
+	regmap_reg_range(0x210000, 0x210001),
+	regmap_reg_range(0x210004, 0x210007),
+	regmap_reg_range(0x21000c, 0x21000d),
+};
+
+static const struct regmap_access_table rtc_rd_regs = {
+	.yes_ranges =	rtc_rd_ranges,
+	.n_yes_ranges =	ARRAY_SIZE(rtc_rd_ranges),
+};
+
+static const struct regmap_range rtc_wr_ranges[] = {
+	regmap_reg_range(0x200000, 0x200010),
+	regmap_reg_range(0x204000, 0x204000),
+	regmap_reg_range(0x210000, 0x210001),
+	regmap_reg_range(0x21000d, 0x21000d),
+};
+
+static const struct regmap_access_table rtc_wr_regs = {
+	.yes_ranges =	rtc_wr_ranges,
+	.n_yes_ranges =	ARRAY_SIZE(rtc_wr_ranges),
+};
+
+static const struct regmap_config gamecube_rtc_regmap_config = {
+	.reg_bits = 24,
+	.val_bits = 32,
+	.rd_table = &rtc_rd_regs,
+	.wr_table = &rtc_wr_regs,
+	.max_register = 0x21000d,
+	.name = "gamecube-rtc",
+};
+
+static int gamecube_rtc_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct rtc_device *rtc;
+	struct priv *d;
+	int ret;
+
+	d = devm_kzalloc(dev, sizeof(struct priv), GFP_KERNEL);
+	if (IS_ERR(d))
+		return PTR_ERR(d);
+
+	d->iob = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(d->iob))
+		return PTR_ERR(d->iob);
+
+	d->regmap = devm_regmap_init(dev, &exi_bus, d,
+				     &gamecube_rtc_regmap_config);
+	if (IS_ERR(d->regmap))
+		return PTR_ERR(d->regmap);
+
+	ret = gamecube_rtc_read_offset_from_sram(d);
+	if (ret)
+		return ret;
+	dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
+
+	dev_set_drvdata(dev, d);
+
+	rtc = devm_rtc_allocate_device(dev);
+	if (IS_ERR(rtc))
+		return PTR_ERR(rtc);
+
+	/* We can represent further than that, but it depends on the stored
+	 * bias and we can’t modify it persistently on all supported consoles,
+	 * so here we pretend to be limited to 2106.
+	 */
+	rtc->range_min = 0;
+	rtc->range_max = U32_MAX;
+	rtc->ops = &gamecube_rtc_ops;
+
+	devm_rtc_register_device(rtc);
+
+	return 0;
+}
+
+static const struct of_device_id gamecube_rtc_of_match[] = {
+	{.compatible = "nintendo,latte-exi" },
+	{.compatible = "nintendo,hollywood-exi" },
+	{.compatible = "nintendo,flipper-exi" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, gamecube_rtc_of_match);
+
+static struct platform_driver gamecube_rtc_driver = {
+	.probe		= gamecube_rtc_probe,
+	.driver		= {
+		.name	= "rtc-gamecube",
+		.of_match_table	= gamecube_rtc_of_match,
+	},
+};
+module_platform_driver(gamecube_rtc_driver);
+
+MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
+MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
+MODULE_LICENSE("GPL");
-- 
2.33.1


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

* [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
@ 2021-10-27 22:35   ` Emmanuel Gil Peyrot
  2021-11-30 22:45     ` Alexandre Belloni
  2021-10-27 22:35   ` [PATCH v2 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform Emmanuel Gil Peyrot
                     ` (3 subsequent siblings)
  5 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 22:35 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

I haven’t been able to test this patch as all of my consoles have a
working RTC battery, but according to the documentation it should work
like that.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/rtc/rtc-gamecube.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/drivers/rtc/rtc-gamecube.c b/drivers/rtc/rtc-gamecube.c
index e8260c82c07d..1932c6fe1301 100644
--- a/drivers/rtc/rtc-gamecube.c
+++ b/drivers/rtc/rtc-gamecube.c
@@ -83,6 +83,10 @@
 #define RTC_CONTROL0	0x21000c
 #define RTC_CONTROL1	0x21000d
 
+/* RTC flags */
+#define RTC_CONTROL0_UNSTABLE_POWER	0x00000800
+#define RTC_CONTROL0_LOW_BATTERY	0x00000200
+
 struct priv {
 	struct regmap *regmap;
 	void __iomem *iob;
@@ -182,9 +186,35 @@ static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
 	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
 }
 
+static int gamecube_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct priv *d = dev_get_drvdata(dev);
+	int value;
+	int control0;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(d->regmap, RTC_CONTROL0, &control0);
+		if (ret)
+			return ret;
+
+		value = 0;
+		if (control0 & RTC_CONTROL0_UNSTABLE_POWER)
+			value |= RTC_VL_DATA_INVALID;
+		if (control0 & RTC_CONTROL0_LOW_BATTERY)
+			value |= RTC_VL_DATA_INVALID;
+		return put_user(value, (unsigned int __user *)arg);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct rtc_class_ops gamecube_rtc_ops = {
 	.read_time	= gamecube_rtc_read_time,
 	.set_time	= gamecube_rtc_set_time,
+	.ioctl		= gamecube_rtc_ioctl,
 };
 
 static int gamecube_rtc_read_offset_from_sram(struct priv *d)
-- 
2.33.1


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

* [PATCH v2 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
@ 2021-10-27 22:35   ` Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 4/5] powerpc: gamecube_defconfig: Enable the RTC driver Emmanuel Gil Peyrot
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 22:35 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

This Hollywood register isn’t properly understood, but can allow or
reject access to the SRAM, which we need to set for RTC usage if it
isn’t previously set correctly beforehand.

See https://wiibrew.org/wiki/Hardware/Hollywood_Registers#HW_SRNPROT

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index c5720fdd0686..34d9732d5910 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -175,6 +175,11 @@ PIC1: pic1@d800030 {
 			interrupts = <14>;
 		};
 
+		srnprot@d800060 {
+			compatible = "nintendo,hollywood-srnprot";
+			reg = <0x0d800060 0x4>;
+		};
+
 		GPIO: gpio@d8000c0 {
 			#gpio-cells = <2>;
 			compatible = "nintendo,hollywood-gpio";
-- 
2.33.1


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

* [PATCH v2 4/5] powerpc: gamecube_defconfig: Enable the RTC driver
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
                     ` (2 preceding siblings ...)
  2021-10-27 22:35   ` [PATCH v2 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform Emmanuel Gil Peyrot
@ 2021-10-27 22:35   ` Emmanuel Gil Peyrot
  2021-10-27 22:35   ` [PATCH v2 5/5] powerpc: wii_defconfig: " Emmanuel Gil Peyrot
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 22:35 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

This selects the rtc-gamecube driver, which provides a real-time clock
on this platform.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/gamecube_defconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/configs/gamecube_defconfig b/arch/powerpc/configs/gamecube_defconfig
index 24c0e0ea5aeb..91a1b99f4e8f 100644
--- a/arch/powerpc/configs/gamecube_defconfig
+++ b/arch/powerpc/configs/gamecube_defconfig
@@ -68,7 +68,7 @@ CONFIG_SND_SEQUENCER=y
 CONFIG_SND_SEQUENCER_OSS=y
 # CONFIG_USB_SUPPORT is not set
 CONFIG_RTC_CLASS=y
-CONFIG_RTC_DRV_GENERIC=y
+CONFIG_RTC_DRV_GAMECUBE=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_ISO9660_FS=y
-- 
2.33.1


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

* [PATCH v2 5/5] powerpc: wii_defconfig: Enable the RTC driver
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
                     ` (3 preceding siblings ...)
  2021-10-27 22:35   ` [PATCH v2 4/5] powerpc: gamecube_defconfig: Enable the RTC driver Emmanuel Gil Peyrot
@ 2021-10-27 22:35   ` Emmanuel Gil Peyrot
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-27 22:35 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

This selects the rtc-gamecube driver, which provides a real-time clock
on this platform.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/wii_defconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/configs/wii_defconfig b/arch/powerpc/configs/wii_defconfig
index 752e081d28d0..ad4302a12fd7 100644
--- a/arch/powerpc/configs/wii_defconfig
+++ b/arch/powerpc/configs/wii_defconfig
@@ -98,7 +98,7 @@ CONFIG_LEDS_TRIGGERS=y
 CONFIG_LEDS_TRIGGER_HEARTBEAT=y
 CONFIG_LEDS_TRIGGER_PANIC=y
 CONFIG_RTC_CLASS=y
-CONFIG_RTC_DRV_GENERIC=y
+CONFIG_RTC_DRV_GAMECUBE=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_FUSE_FS=m
-- 
2.33.1


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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-27 16:45 ` Alexandre Belloni
  2021-10-27 17:05   ` Emmanuel Gil Peyrot
@ 2021-10-28 20:32   ` Jonathan Neuschäfer
  2021-10-28 21:34     ` Emmanuel Gil Peyrot
  1 sibling, 1 reply; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-10-28 20:32 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Emmanuel Gil Peyrot, Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, linux-kernel, linux-rtc

[-- Attachment #1: Type: text/plain, Size: 819 bytes --]

Hi,

On Wed, Oct 27, 2021 at 06:45:54PM +0200, Alexandre Belloni wrote:
> Hello Emmanuel,
> 
> Thanks for the patch!
> 
> On 15/10/2021 00:05:24+0200, Emmanuel Gil Peyrot wrote:
[...]
> Something great to do would be to convert the driver to regmap, provding
> custom regmap_read and regmap_write functions to access the EXI bus.
> Then you'd get this directly in debugfs. And this could be split ou once
> other drivers need access to the bus (I guess power/reset at some
> point).

Ultimately, a proper bus driver might be even better. Another popular
(but 3rd-party) device on the EXI bus is the "USB Gecko", a serial port
adapter.

(But I realize it might be significantly more work. Not sure.)



Best regards, and in any case still thanks for working on the GC/Wii/U
platforms,
Jonathan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-28 20:32   ` Jonathan Neuschäfer
@ 2021-10-28 21:34     ` Emmanuel Gil Peyrot
  2021-10-29 11:06       ` Jonathan Neuschäfer
  0 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-10-28 21:34 UTC (permalink / raw)
  To: Jonathan Neuschäfer
  Cc: Alexandre Belloni, Emmanuel Gil Peyrot, Alessandro Zummo,
	rw-r-r-0644, Ash Logan, linux-kernel, linux-rtc

[-- Attachment #1: Type: text/plain, Size: 1323 bytes --]

On Thu, Oct 28, 2021 at 08:32:00PM +0000, Jonathan Neuschäfer wrote:
> Hi,
> 
> On Wed, Oct 27, 2021 at 06:45:54PM +0200, Alexandre Belloni wrote:
> > Hello Emmanuel,
> > 
> > Thanks for the patch!
> > 
> > On 15/10/2021 00:05:24+0200, Emmanuel Gil Peyrot wrote:
> [...]
> > Something great to do would be to convert the driver to regmap, provding
> > custom regmap_read and regmap_write functions to access the EXI bus.
> > Then you'd get this directly in debugfs. And this could be split ou once
> > other drivers need access to the bus (I guess power/reset at some
> > point).
> 
> Ultimately, a proper bus driver might be even better. Another popular
> (but 3rd-party) device on the EXI bus is the "USB Gecko", a serial port
> adapter.

The gc-linux project had such a bus driver, and I think also an
USB Gecko driver, but from what I remember they got rejected in the
kernel.  I’d have to do some archeology to figure out why though.

Still, maybe it would be sensible to port their 2.6.32-era driver?

I have one such USB Gecko btw, so I should be able to test against it.

> 
> (But I realize it might be significantly more work. Not sure.)
> 
> 
> 
> Best regards, and in any case still thanks for working on the GC/Wii/U
> platforms,
> Jonathan

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-28 21:34     ` Emmanuel Gil Peyrot
@ 2021-10-29 11:06       ` Jonathan Neuschäfer
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-10-29 11:06 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Jonathan Neuschäfer, Alexandre Belloni, Alessandro Zummo,
	rw-r-r-0644, Ash Logan, linux-kernel, linux-rtc

[-- Attachment #1: Type: text/plain, Size: 965 bytes --]

On Thu, Oct 28, 2021 at 11:34:13PM +0200, Emmanuel Gil Peyrot wrote:
> On Thu, Oct 28, 2021 at 08:32:00PM +0000, Jonathan Neuschäfer wrote:
[...]
> The gc-linux project had such a bus driver, and I think also an
> USB Gecko driver, but from what I remember they got rejected in the
> kernel.  I’d have to do some archeology to figure out why though.

Interesting, I wasn't aware that upstreaming had been attempted at all.

> Still, maybe it would be sensible to port their 2.6.32-era driver?

Possibly, but I think it requires a good deal of cleaning up.

I have a working patchset on top of Linux 5.x, but it's not in an
upstream-ready shape:

	https://github.com/neuschaefer/linux/commits/wii

Unfortunately, I don't currently have the time/motivation to do
significant work in this area, so I probably can't help much.


> I have one such USB Gecko btw, so I should be able to test against it.

Great!


Best regards,
Jonathan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data
  2021-10-27 22:35   ` [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
@ 2021-11-30 22:45     ` Alexandre Belloni
  2021-12-15 17:52       ` Emmanuel Gil Peyrot
  0 siblings, 1 reply; 28+ messages in thread
From: Alexandre Belloni @ 2021-11-30 22:45 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

Hello,

On 28/10/2021 00:35:12+0200, Emmanuel Gil Peyrot wrote:
> I haven’t been able to test this patch as all of my consoles have a
> working RTC battery, but according to the documentation it should work
> like that.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---
>  drivers/rtc/rtc-gamecube.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/drivers/rtc/rtc-gamecube.c b/drivers/rtc/rtc-gamecube.c
> index e8260c82c07d..1932c6fe1301 100644
> --- a/drivers/rtc/rtc-gamecube.c
> +++ b/drivers/rtc/rtc-gamecube.c
> @@ -83,6 +83,10 @@
>  #define RTC_CONTROL0	0x21000c
>  #define RTC_CONTROL1	0x21000d
>  
> +/* RTC flags */
> +#define RTC_CONTROL0_UNSTABLE_POWER	0x00000800
> +#define RTC_CONTROL0_LOW_BATTERY	0x00000200
> +
>  struct priv {
>  	struct regmap *regmap;
>  	void __iomem *iob;
> @@ -182,9 +186,35 @@ static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
>  	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
>  }
>  
> +static int gamecube_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
> +{
> +	struct priv *d = dev_get_drvdata(dev);
> +	int value;
> +	int control0;
> +	int ret;
> +
> +	switch (cmd) {
> +	case RTC_VL_READ:
> +		ret = regmap_read(d->regmap, RTC_CONTROL0, &control0);
> +		if (ret)
> +			return ret;
> +
> +		value = 0;
> +		if (control0 & RTC_CONTROL0_UNSTABLE_POWER)
> +			value |= RTC_VL_DATA_INVALID;
> +		if (control0 & RTC_CONTROL0_LOW_BATTERY)
> +			value |= RTC_VL_DATA_INVALID;

Shouldn't that one be RTC_VL_BACKUP_LOW?

Else, the driver is great, I'm ready to apply it.

> +		return put_user(value, (unsigned int __user *)arg);
> +
> +	default:
> +		return -ENOIOCTLCMD;
> +	}
> +}
> +
>  static const struct rtc_class_ops gamecube_rtc_ops = {
>  	.read_time	= gamecube_rtc_read_time,
>  	.set_time	= gamecube_rtc_set_time,
> +	.ioctl		= gamecube_rtc_ioctl,
>  };
>  
>  static int gamecube_rtc_read_offset_from_sram(struct priv *d)
> -- 
> 2.33.1
> 

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data
  2021-11-30 22:45     ` Alexandre Belloni
@ 2021-12-15 17:52       ` Emmanuel Gil Peyrot
  0 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:52 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Emmanuel Gil Peyrot, Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

[-- Attachment #1: Type: text/plain, Size: 2567 bytes --]

On Tue, Nov 30, 2021 at 11:45:18PM +0100, Alexandre Belloni wrote:
> Hello,

Hi,

> 
> On 28/10/2021 00:35:12+0200, Emmanuel Gil Peyrot wrote:
> > I haven’t been able to test this patch as all of my consoles have a
> > working RTC battery, but according to the documentation it should work
> > like that.
> > 
> > Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> > ---
> >  drivers/rtc/rtc-gamecube.c | 30 ++++++++++++++++++++++++++++++
> >  1 file changed, 30 insertions(+)
> > 
> > diff --git a/drivers/rtc/rtc-gamecube.c b/drivers/rtc/rtc-gamecube.c
> > index e8260c82c07d..1932c6fe1301 100644
> > --- a/drivers/rtc/rtc-gamecube.c
> > +++ b/drivers/rtc/rtc-gamecube.c
> > @@ -83,6 +83,10 @@
> >  #define RTC_CONTROL0	0x21000c
> >  #define RTC_CONTROL1	0x21000d
> >  
> > +/* RTC flags */
> > +#define RTC_CONTROL0_UNSTABLE_POWER	0x00000800
> > +#define RTC_CONTROL0_LOW_BATTERY	0x00000200
> > +
> >  struct priv {
> >  	struct regmap *regmap;
> >  	void __iomem *iob;
> > @@ -182,9 +186,35 @@ static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
> >  	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
> >  }
> >  
> > +static int gamecube_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
> > +{
> > +	struct priv *d = dev_get_drvdata(dev);
> > +	int value;
> > +	int control0;
> > +	int ret;
> > +
> > +	switch (cmd) {
> > +	case RTC_VL_READ:
> > +		ret = regmap_read(d->regmap, RTC_CONTROL0, &control0);
> > +		if (ret)
> > +			return ret;
> > +
> > +		value = 0;
> > +		if (control0 & RTC_CONTROL0_UNSTABLE_POWER)
> > +			value |= RTC_VL_DATA_INVALID;
> > +		if (control0 & RTC_CONTROL0_LOW_BATTERY)
> > +			value |= RTC_VL_DATA_INVALID;
> 
> Shouldn't that one be RTC_VL_BACKUP_LOW?

Oops indeed, that seems like a better report, I’ll send a v3 with this
change only.

> 
> Else, the driver is great, I'm ready to apply it.

Perfect!

> 
> > +		return put_user(value, (unsigned int __user *)arg);
> > +
> > +	default:
> > +		return -ENOIOCTLCMD;
> > +	}
> > +}
> > +
> >  static const struct rtc_class_ops gamecube_rtc_ops = {
> >  	.read_time	= gamecube_rtc_read_time,
> >  	.set_time	= gamecube_rtc_set_time,
> > +	.ioctl		= gamecube_rtc_ioctl,
> >  };
> >  
> >  static int gamecube_rtc_read_offset_from_sram(struct priv *d)
> > -- 
> > 2.33.1
> > 
> 
> -- 
> Alexandre Belloni, co-owner and COO, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
                     ` (4 preceding siblings ...)
  2021-10-27 22:35   ` [PATCH v2 5/5] powerpc: wii_defconfig: " Emmanuel Gil Peyrot
@ 2021-12-15 17:54   ` Emmanuel Gil Peyrot
  2021-12-15 17:54     ` [PATCH v3 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
                       ` (6 more replies)
  5 siblings, 7 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:54 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

These three consoles share a device, the MX23L4005, which contains a
clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
(similar to SPI) on channel 0, device 1.  This driver allows it to be
used as a Linux RTC device, where time can be read and set.

The hardware also exposes two timers, one which shuts down the console
and one which powers it on, but these aren’t supported currently.

On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
order to avoid having to implement all that, this driver assumes a
bootloader will parse this XML file and write the bias into the SRAM, at
the same location the other two consoles have it.

Changes since v1:
- Rename the driver to rtc-gamecube.
- Switch to the regmap API for debugfs support.
- Report low battery and unstable power as invalid data.
- Remove Wii U support in Kconfig, nothing specific to this console
  needs to be changed in the code.
- Don’t attempt to change HW_SRNPROT on the GameCube, this register
  doesn’t exist so we can use SRAM just fine without doing anything.
- Add needed changes to the wii device tree.
- Enable this driver on the gamecube and wii platforms.

Changes since v2:
- Report low battery correctly.

Emmanuel Gil Peyrot (5):
  rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
  rtc: gamecube: Report low battery as invalid data
  powerpc: wii.dts: Expose HW_SRNPROT on this platform
  powerpc: gamecube_defconfig: Enable the RTC driver
  powerpc: wii_defconfig: Enable the RTC driver

 arch/powerpc/boot/dts/wii.dts           |   5 +
 arch/powerpc/configs/gamecube_defconfig |   2 +-
 arch/powerpc/configs/wii_defconfig      |   2 +-
 drivers/rtc/Kconfig                     |  11 +
 drivers/rtc/Makefile                    |   1 +
 drivers/rtc/rtc-gamecube.c              | 377 ++++++++++++++++++++++++
 6 files changed, 396 insertions(+), 2 deletions(-)
 create mode 100644 drivers/rtc/rtc-gamecube.c

-- 
2.34.1


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

* [PATCH v3 1/5] rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
@ 2021-12-15 17:54     ` Emmanuel Gil Peyrot
  2021-12-15 17:54     ` [PATCH v3 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
                       ` (5 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:54 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

These three consoles share a device, the MX23L4005, which contains a
clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
(similar to SPI) on channel 0, device 1.  This driver allows it to be
used as a Linux RTC device, where time can be read and set.

The hardware also exposes two timers, one which shuts down the console
and one which powers it on, but these aren’t supported currently.

On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
order to avoid having to implement all that, this driver assumes a
bootloader will parse this XML file and write the bias into the SRAM, at
the same location the other two consoles have it.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/rtc/Kconfig        |  11 ++
 drivers/rtc/Makefile       |   1 +
 drivers/rtc/rtc-gamecube.c | 347 +++++++++++++++++++++++++++++++++++++
 3 files changed, 359 insertions(+)
 create mode 100644 drivers/rtc/rtc-gamecube.c

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 914497abeef9..503089ca370d 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -1214,6 +1214,17 @@ config RTC_DRV_V3020
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-v3020.
 
+config RTC_DRV_GAMECUBE
+	tristate "Nintendo GameCube, Wii and Wii U RTC"
+	depends on GAMECUBE || WII || COMPILE_TEST
+	select REGMAP
+	help
+	  If you say yes here you will get support for the RTC subsystem
+	  of the Nintendo GameCube, Wii and Wii U.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called "rtc-gamecube".
+
 config RTC_DRV_WM831X
 	tristate "Wolfson Microelectronics WM831x RTC"
 	depends on MFD_WM831X
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 2dd0dd956b0e..d781aaf0909c 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -108,6 +108,7 @@ obj-$(CONFIG_RTC_DRV_MT7622)	+= rtc-mt7622.o
 obj-$(CONFIG_RTC_DRV_MV)	+= rtc-mv.o
 obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
 obj-$(CONFIG_RTC_DRV_MXC_V2)	+= rtc-mxc_v2.o
+obj-$(CONFIG_RTC_DRV_GAMECUBE)	+= rtc-gamecube.o
 obj-$(CONFIG_RTC_DRV_NTXEC)	+= rtc-ntxec.o
 obj-$(CONFIG_RTC_DRV_OMAP)	+= rtc-omap.o
 obj-$(CONFIG_RTC_DRV_OPAL)	+= rtc-opal.o
diff --git a/drivers/rtc/rtc-gamecube.c b/drivers/rtc/rtc-gamecube.c
new file mode 100644
index 000000000000..e8260c82c07d
--- /dev/null
+++ b/drivers/rtc/rtc-gamecube.c
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nintendo GameCube, Wii and Wii U RTC driver
+ *
+ * This driver is for the MX23L4005, more specifically its real-time clock and
+ * SRAM storage.  The value returned by the RTC counter must be added with the
+ * offset stored in a bias register in SRAM (on the GameCube and Wii) or in
+ * /config/rtc.xml (on the Wii U).  The latter being very impractical to access
+ * from Linux, this driver assumes the bootloader has read it and stored it in
+ * SRAM like for the other two consoles.
+ *
+ * This device sits on a bus named EXI (which is similar to SPI), channel 0,
+ * device 1.  This driver assumes no other user of the EXI bus, which is
+ * currently the case but would have to be reworked to add support for other
+ * GameCube hardware exposed on this bus.
+ *
+ * References:
+ * - https://wiiubrew.org/wiki/Hardware/RTC
+ * - https://wiibrew.org/wiki/MX23L4005
+ *
+ * Copyright (C) 2018 rw-r-r-0644
+ * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ *
+ * Based on rtc-gcn.c
+ * Copyright (C) 2004-2009 The GameCube Linux Team
+ * Copyright (C) 2005,2008,2009 Albert Herranz
+ * Based on gamecube_time.c from Torben Nielsen.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/rtc.h>
+#include <linux/time.h>
+
+/* EXI registers */
+#define EXICSR	0
+#define EXICR	12
+#define EXIDATA	16
+
+/* EXI register values */
+#define EXICSR_DEV		0x380
+	#define EXICSR_DEV1	0x100
+#define EXICSR_CLK		0x070
+	#define EXICSR_CLK_1MHZ	0x000
+	#define EXICSR_CLK_2MHZ	0x010
+	#define EXICSR_CLK_4MHZ	0x020
+	#define EXICSR_CLK_8MHZ	0x030
+	#define EXICSR_CLK_16MHZ 0x040
+	#define EXICSR_CLK_32MHZ 0x050
+#define EXICSR_INT		0x008
+	#define EXICSR_INTSET	0x008
+
+#define EXICR_TSTART		0x001
+#define EXICR_TRSMODE		0x002
+	#define EXICR_TRSMODE_IMM 0x000
+#define EXICR_TRSTYPE		0x00C
+	#define EXICR_TRSTYPE_R	0x000
+	#define EXICR_TRSTYPE_W	0x004
+#define EXICR_TLEN		0x030
+	#define EXICR_TLEN32	0x030
+
+/* EXI registers values to access the RTC */
+#define RTC_EXICSR	(EXICSR_DEV1 | EXICSR_CLK_8MHZ | EXICSR_INTSET)
+#define RTC_EXICR_W	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_W | EXICR_TLEN32)
+#define RTC_EXICR_R	(EXICR_TSTART | EXICR_TRSMODE_IMM | EXICR_TRSTYPE_R | EXICR_TLEN32)
+#define RTC_EXIDATA_W	0x80000000
+
+/* RTC registers */
+#define RTC_COUNTER	0x200000
+#define RTC_SRAM	0x200001
+#define RTC_SRAM_BIAS	0x200004
+#define RTC_SNAPSHOT	0x204000
+#define RTC_ONTMR	0x210000
+#define RTC_OFFTMR	0x210001
+#define RTC_TEST0	0x210004
+#define RTC_TEST1	0x210005
+#define RTC_TEST2	0x210006
+#define RTC_TEST3	0x210007
+#define RTC_CONTROL0	0x21000c
+#define RTC_CONTROL1	0x21000d
+
+struct priv {
+	struct regmap *regmap;
+	void __iomem *iob;
+	u32 rtc_bias;
+};
+
+static int exi_read(void *context, u32 reg, u32 *data)
+{
+	struct priv *d = (struct priv *)context;
+	void __iomem *iob = d->iob;
+
+	/* The spin loops here loop about 15~16 times each, so there is no need
+	 * to use a more expensive sleep method.
+	 */
+
+	/* Write register offset */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(reg << 8, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Read data */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(RTC_EXICR_R, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+	*data = ioread32be(iob + EXIDATA);
+
+	/* Clear channel parameters */
+	iowrite32be(0, iob + EXICSR);
+
+	return 0;
+}
+
+static int exi_write(void *context, u32 reg, u32 data)
+{
+	struct priv *d = (struct priv *)context;
+	void __iomem *iob = d->iob;
+
+	/* The spin loops here loop about 15~16 times each, so there is no need
+	 * to use a more expensive sleep method.
+	 */
+
+	/* Write register offset */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(RTC_EXIDATA_W | (reg << 8), iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Write data */
+	iowrite32be(RTC_EXICSR, iob + EXICSR);
+	iowrite32be(data, iob + EXIDATA);
+	iowrite32be(RTC_EXICR_W, iob + EXICR);
+	while (!(ioread32be(iob + EXICSR) & EXICSR_INTSET))
+		cpu_relax();
+
+	/* Clear channel parameters */
+	iowrite32be(0, iob + EXICSR);
+
+	return 0;
+}
+
+static const struct regmap_bus exi_bus = {
+	/* TODO: is that true?  Not that it matters here, but still. */
+	.fast_io = true,
+	.reg_read = exi_read,
+	.reg_write = exi_write,
+};
+
+static int gamecube_rtc_read_time(struct device *dev, struct rtc_time *t)
+{
+	struct priv *d = dev_get_drvdata(dev);
+	int ret;
+	u32 counter;
+	time64_t timestamp;
+
+	ret = regmap_read(d->regmap, RTC_COUNTER, &counter);
+	if (ret)
+		return ret;
+
+	/* Add the counter and the bias to obtain the timestamp */
+	timestamp = (time64_t)d->rtc_bias + counter;
+	rtc_time64_to_tm(timestamp, t);
+
+	return 0;
+}
+
+static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
+{
+	struct priv *d = dev_get_drvdata(dev);
+	time64_t timestamp;
+
+	/* Subtract the timestamp and the bias to obtain the counter value */
+	timestamp = rtc_tm_to_time64(t);
+	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
+}
+
+static const struct rtc_class_ops gamecube_rtc_ops = {
+	.read_time	= gamecube_rtc_read_time,
+	.set_time	= gamecube_rtc_set_time,
+};
+
+static int gamecube_rtc_read_offset_from_sram(struct priv *d)
+{
+	struct device_node *np;
+	int ret;
+	struct resource res;
+	void __iomem *hw_srnprot;
+	u32 old;
+
+	np = of_find_compatible_node(NULL, NULL, "nintendo,latte-srnprot");
+	if (!np)
+		np = of_find_compatible_node(NULL, NULL,
+					     "nintendo,hollywood-srnprot");
+	if (!np) {
+		pr_info("HW_SRNPROT not found, assuming a GameCube\n");
+		return regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
+	}
+
+	ret = of_address_to_resource(np, 0, &res);
+	if (ret) {
+		pr_err("no io memory range found\n");
+		return -1;
+	}
+
+	hw_srnprot = ioremap(res.start, resource_size(&res));
+	old = ioread32be(hw_srnprot);
+
+	/* TODO: figure out why we use this magic constant.  I obtained it by
+	 * reading the leftover value after boot, after IOSU already ran.
+	 *
+	 * On my Wii U, setting this register to 1 prevents the console from
+	 * rebooting properly, so wiiubrew.org must be missing something.
+	 *
+	 * See https://wiiubrew.org/wiki/Hardware/Latte_registers
+	 */
+	if (old != 0x7bf)
+		iowrite32be(0x7bf, hw_srnprot);
+
+	/* Get the offset from RTC SRAM.
+	 *
+	 * Its default location on the GameCube and on the Wii is in the SRAM,
+	 * while on the Wii U the bootloader needs to fill it with the contents
+	 * of /config/rtc.xml on the SLC (the eMMC).  We don’t do that from
+	 * Linux since it requires implementing a proprietary filesystem and do
+	 * file decryption, instead we require the bootloader to fill the same
+	 * SRAM address as on previous consoles.
+	 */
+	ret = regmap_read(d->regmap, RTC_SRAM_BIAS, &d->rtc_bias);
+	if (ret) {
+		pr_err("failed to get the RTC bias\n");
+		return -1;
+	}
+
+	/* Reset SRAM access to how it was before, our job here is done. */
+	if (old != 0x7bf)
+		iowrite32be(old, hw_srnprot);
+	iounmap(hw_srnprot);
+
+	return 0;
+}
+
+static const struct regmap_range rtc_rd_ranges[] = {
+	regmap_reg_range(0x200000, 0x200010),
+	regmap_reg_range(0x204000, 0x204000),
+	regmap_reg_range(0x210000, 0x210001),
+	regmap_reg_range(0x210004, 0x210007),
+	regmap_reg_range(0x21000c, 0x21000d),
+};
+
+static const struct regmap_access_table rtc_rd_regs = {
+	.yes_ranges =	rtc_rd_ranges,
+	.n_yes_ranges =	ARRAY_SIZE(rtc_rd_ranges),
+};
+
+static const struct regmap_range rtc_wr_ranges[] = {
+	regmap_reg_range(0x200000, 0x200010),
+	regmap_reg_range(0x204000, 0x204000),
+	regmap_reg_range(0x210000, 0x210001),
+	regmap_reg_range(0x21000d, 0x21000d),
+};
+
+static const struct regmap_access_table rtc_wr_regs = {
+	.yes_ranges =	rtc_wr_ranges,
+	.n_yes_ranges =	ARRAY_SIZE(rtc_wr_ranges),
+};
+
+static const struct regmap_config gamecube_rtc_regmap_config = {
+	.reg_bits = 24,
+	.val_bits = 32,
+	.rd_table = &rtc_rd_regs,
+	.wr_table = &rtc_wr_regs,
+	.max_register = 0x21000d,
+	.name = "gamecube-rtc",
+};
+
+static int gamecube_rtc_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct rtc_device *rtc;
+	struct priv *d;
+	int ret;
+
+	d = devm_kzalloc(dev, sizeof(struct priv), GFP_KERNEL);
+	if (IS_ERR(d))
+		return PTR_ERR(d);
+
+	d->iob = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(d->iob))
+		return PTR_ERR(d->iob);
+
+	d->regmap = devm_regmap_init(dev, &exi_bus, d,
+				     &gamecube_rtc_regmap_config);
+	if (IS_ERR(d->regmap))
+		return PTR_ERR(d->regmap);
+
+	ret = gamecube_rtc_read_offset_from_sram(d);
+	if (ret)
+		return ret;
+	dev_dbg(dev, "SRAM bias: 0x%x", d->rtc_bias);
+
+	dev_set_drvdata(dev, d);
+
+	rtc = devm_rtc_allocate_device(dev);
+	if (IS_ERR(rtc))
+		return PTR_ERR(rtc);
+
+	/* We can represent further than that, but it depends on the stored
+	 * bias and we can’t modify it persistently on all supported consoles,
+	 * so here we pretend to be limited to 2106.
+	 */
+	rtc->range_min = 0;
+	rtc->range_max = U32_MAX;
+	rtc->ops = &gamecube_rtc_ops;
+
+	devm_rtc_register_device(rtc);
+
+	return 0;
+}
+
+static const struct of_device_id gamecube_rtc_of_match[] = {
+	{.compatible = "nintendo,latte-exi" },
+	{.compatible = "nintendo,hollywood-exi" },
+	{.compatible = "nintendo,flipper-exi" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, gamecube_rtc_of_match);
+
+static struct platform_driver gamecube_rtc_driver = {
+	.probe		= gamecube_rtc_probe,
+	.driver		= {
+		.name	= "rtc-gamecube",
+		.of_match_table	= gamecube_rtc_of_match,
+	},
+};
+module_platform_driver(gamecube_rtc_driver);
+
+MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
+MODULE_DESCRIPTION("Nintendo GameCube, Wii and Wii U RTC driver");
+MODULE_LICENSE("GPL");
-- 
2.34.1


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

* [PATCH v3 2/5] rtc: gamecube: Report low battery as invalid data
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
  2021-12-15 17:54     ` [PATCH v3 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
@ 2021-12-15 17:54     ` Emmanuel Gil Peyrot
  2021-12-15 17:54     ` [PATCH v3 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform Emmanuel Gil Peyrot
                       ` (4 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:54 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

I haven’t been able to test this patch as all of my consoles have a
working RTC battery, but according to the documentation it should work
like that.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/rtc/rtc-gamecube.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/drivers/rtc/rtc-gamecube.c b/drivers/rtc/rtc-gamecube.c
index e8260c82c07d..98128746171e 100644
--- a/drivers/rtc/rtc-gamecube.c
+++ b/drivers/rtc/rtc-gamecube.c
@@ -83,6 +83,10 @@
 #define RTC_CONTROL0	0x21000c
 #define RTC_CONTROL1	0x21000d
 
+/* RTC flags */
+#define RTC_CONTROL0_UNSTABLE_POWER	0x00000800
+#define RTC_CONTROL0_LOW_BATTERY	0x00000200
+
 struct priv {
 	struct regmap *regmap;
 	void __iomem *iob;
@@ -182,9 +186,35 @@ static int gamecube_rtc_set_time(struct device *dev, struct rtc_time *t)
 	return regmap_write(d->regmap, RTC_COUNTER, timestamp - d->rtc_bias);
 }
 
+static int gamecube_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+	struct priv *d = dev_get_drvdata(dev);
+	int value;
+	int control0;
+	int ret;
+
+	switch (cmd) {
+	case RTC_VL_READ:
+		ret = regmap_read(d->regmap, RTC_CONTROL0, &control0);
+		if (ret)
+			return ret;
+
+		value = 0;
+		if (control0 & RTC_CONTROL0_UNSTABLE_POWER)
+			value |= RTC_VL_DATA_INVALID;
+		if (control0 & RTC_CONTROL0_LOW_BATTERY)
+			value |= RTC_VL_BACKUP_LOW;
+		return put_user(value, (unsigned int __user *)arg);
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct rtc_class_ops gamecube_rtc_ops = {
 	.read_time	= gamecube_rtc_read_time,
 	.set_time	= gamecube_rtc_set_time,
+	.ioctl		= gamecube_rtc_ioctl,
 };
 
 static int gamecube_rtc_read_offset_from_sram(struct priv *d)
-- 
2.34.1


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

* [PATCH v3 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
  2021-12-15 17:54     ` [PATCH v3 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
  2021-12-15 17:54     ` [PATCH v3 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
@ 2021-12-15 17:54     ` Emmanuel Gil Peyrot
  2021-12-15 17:55     ` [PATCH v3 4/5] powerpc: gamecube_defconfig: Enable the RTC driver Emmanuel Gil Peyrot
                       ` (3 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:54 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

This Hollywood register isn’t properly understood, but can allow or
reject access to the SRAM, which we need to set for RTC usage if it
isn’t previously set correctly beforehand.

See https://wiibrew.org/wiki/Hardware/Hollywood_Registers#HW_SRNPROT

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index c5720fdd0686..34d9732d5910 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -175,6 +175,11 @@ PIC1: pic1@d800030 {
 			interrupts = <14>;
 		};
 
+		srnprot@d800060 {
+			compatible = "nintendo,hollywood-srnprot";
+			reg = <0x0d800060 0x4>;
+		};
+
 		GPIO: gpio@d8000c0 {
 			#gpio-cells = <2>;
 			compatible = "nintendo,hollywood-gpio";
-- 
2.34.1


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

* [PATCH v3 4/5] powerpc: gamecube_defconfig: Enable the RTC driver
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
                       ` (2 preceding siblings ...)
  2021-12-15 17:54     ` [PATCH v3 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform Emmanuel Gil Peyrot
@ 2021-12-15 17:55     ` Emmanuel Gil Peyrot
  2021-12-15 17:55     ` [PATCH v3 5/5] powerpc: wii_defconfig: " Emmanuel Gil Peyrot
                       ` (2 subsequent siblings)
  6 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:55 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

This selects the rtc-gamecube driver, which provides a real-time clock
on this platform.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/gamecube_defconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/configs/gamecube_defconfig b/arch/powerpc/configs/gamecube_defconfig
index 24c0e0ea5aeb..91a1b99f4e8f 100644
--- a/arch/powerpc/configs/gamecube_defconfig
+++ b/arch/powerpc/configs/gamecube_defconfig
@@ -68,7 +68,7 @@ CONFIG_SND_SEQUENCER=y
 CONFIG_SND_SEQUENCER_OSS=y
 # CONFIG_USB_SUPPORT is not set
 CONFIG_RTC_CLASS=y
-CONFIG_RTC_DRV_GENERIC=y
+CONFIG_RTC_DRV_GAMECUBE=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_ISO9660_FS=y
-- 
2.34.1


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

* [PATCH v3 5/5] powerpc: wii_defconfig: Enable the RTC driver
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
                       ` (3 preceding siblings ...)
  2021-12-15 17:55     ` [PATCH v3 4/5] powerpc: gamecube_defconfig: Enable the RTC driver Emmanuel Gil Peyrot
@ 2021-12-15 17:55     ` Emmanuel Gil Peyrot
  2021-12-16  4:52     ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Michael Ellerman
  2021-12-16  9:49     ` Alexandre Belloni
  6 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-15 17:55 UTC (permalink / raw)
  To: Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel, linux-rtc,
	linuxppc-dev, devicetree

This selects the rtc-gamecube driver, which provides a real-time clock
on this platform.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/wii_defconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/configs/wii_defconfig b/arch/powerpc/configs/wii_defconfig
index 752e081d28d0..ad4302a12fd7 100644
--- a/arch/powerpc/configs/wii_defconfig
+++ b/arch/powerpc/configs/wii_defconfig
@@ -98,7 +98,7 @@ CONFIG_LEDS_TRIGGERS=y
 CONFIG_LEDS_TRIGGER_HEARTBEAT=y
 CONFIG_LEDS_TRIGGER_PANIC=y
 CONFIG_RTC_CLASS=y
-CONFIG_RTC_DRV_GENERIC=y
+CONFIG_RTC_DRV_GAMECUBE=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_FUSE_FS=m
-- 
2.34.1


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

* Re: [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
                       ` (4 preceding siblings ...)
  2021-12-15 17:55     ` [PATCH v3 5/5] powerpc: wii_defconfig: " Emmanuel Gil Peyrot
@ 2021-12-16  4:52     ` Michael Ellerman
  2021-12-16  9:50       ` Alexandre Belloni
  2021-12-16  9:49     ` Alexandre Belloni
  6 siblings, 1 reply; 28+ messages in thread
From: Michael Ellerman @ 2021-12-16  4:52 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot, Alexandre Belloni, Alessandro Zummo
  Cc: Emmanuel Gil Peyrot, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel, linux-rtc, linuxppc-dev,
	devicetree

Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> writes:
> These three consoles share a device, the MX23L4005, which contains a
> clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> (similar to SPI) on channel 0, device 1.  This driver allows it to be
> used as a Linux RTC device, where time can be read and set.
>
> The hardware also exposes two timers, one which shuts down the console
> and one which powers it on, but these aren’t supported currently.
>
> On the Wii U, the counter bias is stored in a XML file, /config/rtc.xml,
> encrypted in the SLC (eMMC storage), using a proprietary filesystem.  In
> order to avoid having to implement all that, this driver assumes a
> bootloader will parse this XML file and write the bias into the SRAM, at
> the same location the other two consoles have it.
>
> Changes since v1:
> - Rename the driver to rtc-gamecube.
> - Switch to the regmap API for debugfs support.
> - Report low battery and unstable power as invalid data.
> - Remove Wii U support in Kconfig, nothing specific to this console
>   needs to be changed in the code.
> - Don’t attempt to change HW_SRNPROT on the GameCube, this register
>   doesn’t exist so we can use SRAM just fine without doing anything.
> - Add needed changes to the wii device tree.
> - Enable this driver on the gamecube and wii platforms.
>
> Changes since v2:
> - Report low battery correctly.
>
> Emmanuel Gil Peyrot (5):
>   rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
>   rtc: gamecube: Report low battery as invalid data
>   powerpc: wii.dts: Expose HW_SRNPROT on this platform
>   powerpc: gamecube_defconfig: Enable the RTC driver
>   powerpc: wii_defconfig: Enable the RTC driver
>
>  drivers/rtc/Kconfig                     |  11 +
>  drivers/rtc/Makefile                    |   1 +
>  drivers/rtc/rtc-gamecube.c              | 377 ++++++++++++++++++++++++

This is basically an rtc series as far as I'm concerned.

>  arch/powerpc/boot/dts/wii.dts           |   5 +
>  arch/powerpc/configs/gamecube_defconfig |   2 +-
>  arch/powerpc/configs/wii_defconfig      |   2 +-

I have nothing queued in the powerpc tree that touches any of those
files, so conflicts are unlikely.

So I'm happy for this to go via the rtc tree whenever it's ready.

Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)

cheers

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

* Re: [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
                       ` (5 preceding siblings ...)
  2021-12-16  4:52     ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Michael Ellerman
@ 2021-12-16  9:49     ` Alexandre Belloni
  2021-12-16 20:22       ` Emmanuel Gil Peyrot
  6 siblings, 1 reply; 28+ messages in thread
From: Alexandre Belloni @ 2021-12-16  9:49 UTC (permalink / raw)
  To: Alessandro Zummo, Emmanuel Gil Peyrot
  Cc: Alexandre Belloni, Jonathan Neuschäfer, linux-rtc,
	Michael Ellerman, Rob Herring, Benjamin Herrenschmidt,
	linuxppc-dev, Ash Logan, rw-r-r-0644, devicetree, linux-kernel,
	Paul Mackerras

On Wed, 15 Dec 2021 18:54:56 +0100, Emmanuel Gil Peyrot wrote:
> These three consoles share a device, the MX23L4005, which contains a
> clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> (similar to SPI) on channel 0, device 1.  This driver allows it to be
> used as a Linux RTC device, where time can be read and set.
> 
> The hardware also exposes two timers, one which shuts down the console
> and one which powers it on, but these aren’t supported currently.
> 
> [...]

Applied, thanks!

[1/5] rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
      commit: 86559400b3ef9de93ba50523cffe767c35cd531a
[2/5] rtc: gamecube: Report low battery as invalid data
      commit: 322539a014bcd24cbb9281832c09b24e07912237
[3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform
      commit: 5479618e1e2641dd57352a73b7b7b2f6908fbeee
[4/5] powerpc: gamecube_defconfig: Enable the RTC driver
      commit: 57bd7d356506b713d0df8d8e42da7810a18864df
[5/5] powerpc: wii_defconfig: Enable the RTC driver
      commit: 69e8ba80ddda4db31e59facbf2db19773ad3785b

This one didn't apply ceanly but I believe I did the right thing. Can you check?


Best regards,
-- 
Alexandre Belloni <alexandre.belloni@bootlin.com>

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

* Re: [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-16  4:52     ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Michael Ellerman
@ 2021-12-16  9:50       ` Alexandre Belloni
  0 siblings, 0 replies; 28+ messages in thread
From: Alexandre Belloni @ 2021-12-16  9:50 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: Emmanuel Gil Peyrot, Alessandro Zummo, rw-r-r-0644, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel, linux-rtc, linuxppc-dev,
	devicetree

Hello,

On 16/12/2021 15:52:59+1100, Michael Ellerman wrote:
> > Emmanuel Gil Peyrot (5):
> >   rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
> >   rtc: gamecube: Report low battery as invalid data
> >   powerpc: wii.dts: Expose HW_SRNPROT on this platform
> >   powerpc: gamecube_defconfig: Enable the RTC driver
> >   powerpc: wii_defconfig: Enable the RTC driver
> >
> >  drivers/rtc/Kconfig                     |  11 +
> >  drivers/rtc/Makefile                    |   1 +
> >  drivers/rtc/rtc-gamecube.c              | 377 ++++++++++++++++++++++++
> 
> This is basically an rtc series as far as I'm concerned.
> 
> >  arch/powerpc/boot/dts/wii.dts           |   5 +
> >  arch/powerpc/configs/gamecube_defconfig |   2 +-
> >  arch/powerpc/configs/wii_defconfig      |   2 +-
> 
> I have nothing queued in the powerpc tree that touches any of those
> files, so conflicts are unlikely.
> 
> So I'm happy for this to go via the rtc tree whenever it's ready.
> 
> Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)

That's done, thanks.


-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-16  9:49     ` Alexandre Belloni
@ 2021-12-16 20:22       ` Emmanuel Gil Peyrot
  2021-12-16 20:56         ` Alexandre Belloni
  0 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-16 20:22 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Alessandro Zummo, Emmanuel Gil Peyrot, Jonathan Neuschäfer,
	linux-rtc, Michael Ellerman, Rob Herring, Benjamin Herrenschmidt,
	linuxppc-dev, Ash Logan, rw-r-r-0644, devicetree, linux-kernel,
	Paul Mackerras

[-- Attachment #1: Type: text/plain, Size: 1793 bytes --]

On Thu, Dec 16, 2021 at 10:49:44AM +0100, Alexandre Belloni wrote:
> On Wed, 15 Dec 2021 18:54:56 +0100, Emmanuel Gil Peyrot wrote:
> > These three consoles share a device, the MX23L4005, which contains a
> > clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> > (similar to SPI) on channel 0, device 1.  This driver allows it to be
> > used as a Linux RTC device, where time can be read and set.
> > 
> > The hardware also exposes two timers, one which shuts down the console
> > and one which powers it on, but these aren’t supported currently.
> > 
> > [...]
> 
> Applied, thanks!
> 
> [1/5] rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
>       commit: 86559400b3ef9de93ba50523cffe767c35cd531a
> [2/5] rtc: gamecube: Report low battery as invalid data
>       commit: 322539a014bcd24cbb9281832c09b24e07912237
> [3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform
>       commit: 5479618e1e2641dd57352a73b7b7b2f6908fbeee
> [4/5] powerpc: gamecube_defconfig: Enable the RTC driver
>       commit: 57bd7d356506b713d0df8d8e42da7810a18864df
> [5/5] powerpc: wii_defconfig: Enable the RTC driver
>       commit: 69e8ba80ddda4db31e59facbf2db19773ad3785b
> 
> This one didn't apply ceanly but I believe I did the right thing. Can you check?

I believe you didn’t, at least that commit[1] seems to have one “+” too
many in the modified line, whereas the previous one[2] doesn’t.

But thanks for applying them!

> 
> 
> Best regards,
> -- 
> Alexandre Belloni <alexandre.belloni@bootlin.com>

[1] https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?id=69e8ba80dd
[2] https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?id=57bd7d3565

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-16 20:22       ` Emmanuel Gil Peyrot
@ 2021-12-16 20:56         ` Alexandre Belloni
  2021-12-16 20:58           ` Emmanuel Gil Peyrot
  0 siblings, 1 reply; 28+ messages in thread
From: Alexandre Belloni @ 2021-12-16 20:56 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Alessandro Zummo, Jonathan Neuschäfer, linux-rtc,
	Michael Ellerman, Rob Herring, Benjamin Herrenschmidt,
	linuxppc-dev, Ash Logan, rw-r-r-0644, devicetree, linux-kernel,
	Paul Mackerras

On 16/12/2021 21:22:20+0100, Emmanuel Gil Peyrot wrote:
> On Thu, Dec 16, 2021 at 10:49:44AM +0100, Alexandre Belloni wrote:
> > On Wed, 15 Dec 2021 18:54:56 +0100, Emmanuel Gil Peyrot wrote:
> > > These three consoles share a device, the MX23L4005, which contains a
> > > clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> > > (similar to SPI) on channel 0, device 1.  This driver allows it to be
> > > used as a Linux RTC device, where time can be read and set.
> > > 
> > > The hardware also exposes two timers, one which shuts down the console
> > > and one which powers it on, but these aren’t supported currently.
> > > 
> > > [...]
> > 
> > Applied, thanks!
> > 
> > [1/5] rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
> >       commit: 86559400b3ef9de93ba50523cffe767c35cd531a
> > [2/5] rtc: gamecube: Report low battery as invalid data
> >       commit: 322539a014bcd24cbb9281832c09b24e07912237
> > [3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform
> >       commit: 5479618e1e2641dd57352a73b7b7b2f6908fbeee
> > [4/5] powerpc: gamecube_defconfig: Enable the RTC driver
> >       commit: 57bd7d356506b713d0df8d8e42da7810a18864df
> > [5/5] powerpc: wii_defconfig: Enable the RTC driver
> >       commit: 69e8ba80ddda4db31e59facbf2db19773ad3785b
> > 
> > This one didn't apply ceanly but I believe I did the right thing. Can you check?
> 
> I believe you didn’t, at least that commit[1] seems to have one “+” too
> many in the modified line, whereas the previous one[2] doesn’t.
> 

I knew I needed you to check, this is fixed now.

https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?id=c636783d594f6cfc95db51c796761719317ce5eb


-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U
  2021-12-16 20:56         ` Alexandre Belloni
@ 2021-12-16 20:58           ` Emmanuel Gil Peyrot
  0 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-12-16 20:58 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Emmanuel Gil Peyrot, Alessandro Zummo, Jonathan Neuschäfer,
	linux-rtc, Michael Ellerman, Rob Herring, Benjamin Herrenschmidt,
	linuxppc-dev, Ash Logan, rw-r-r-0644, devicetree, linux-kernel,
	Paul Mackerras

[-- Attachment #1: Type: text/plain, Size: 2069 bytes --]

On Thu, Dec 16, 2021 at 09:56:06PM +0100, Alexandre Belloni wrote:
> On 16/12/2021 21:22:20+0100, Emmanuel Gil Peyrot wrote:
> > On Thu, Dec 16, 2021 at 10:49:44AM +0100, Alexandre Belloni wrote:
> > > On Wed, 15 Dec 2021 18:54:56 +0100, Emmanuel Gil Peyrot wrote:
> > > > These three consoles share a device, the MX23L4005, which contains a
> > > > clock and 64 bytes of SRAM storage, and is exposed on the EXI bus
> > > > (similar to SPI) on channel 0, device 1.  This driver allows it to be
> > > > used as a Linux RTC device, where time can be read and set.
> > > > 
> > > > The hardware also exposes two timers, one which shuts down the console
> > > > and one which powers it on, but these aren’t supported currently.
> > > > 
> > > > [...]
> > > 
> > > Applied, thanks!
> > > 
> > > [1/5] rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U
> > >       commit: 86559400b3ef9de93ba50523cffe767c35cd531a
> > > [2/5] rtc: gamecube: Report low battery as invalid data
> > >       commit: 322539a014bcd24cbb9281832c09b24e07912237
> > > [3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform
> > >       commit: 5479618e1e2641dd57352a73b7b7b2f6908fbeee
> > > [4/5] powerpc: gamecube_defconfig: Enable the RTC driver
> > >       commit: 57bd7d356506b713d0df8d8e42da7810a18864df
> > > [5/5] powerpc: wii_defconfig: Enable the RTC driver
> > >       commit: 69e8ba80ddda4db31e59facbf2db19773ad3785b
> > > 
> > > This one didn't apply ceanly but I believe I did the right thing. Can you check?
> > 
> > I believe you didn’t, at least that commit[1] seems to have one “+” too
> > many in the modified line, whereas the previous one[2] doesn’t.
> > 
> 
> I knew I needed you to check, this is fixed now.
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?id=c636783d594f6cfc95db51c796761719317ce5eb

Perfect, thanks a lot!

> 
> 
> -- 
> Alexandre Belloni, co-owner and COO, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2021-12-16 20:58 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14 22:05 [PATCH] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
2021-10-27 16:45 ` Alexandre Belloni
2021-10-27 17:05   ` Emmanuel Gil Peyrot
2021-10-27 17:09     ` Emmanuel Gil Peyrot
2021-10-27 20:32     ` Alexandre Belloni
2021-10-28 20:32   ` Jonathan Neuschäfer
2021-10-28 21:34     ` Emmanuel Gil Peyrot
2021-10-29 11:06       ` Jonathan Neuschäfer
2021-10-27 22:35 ` [PATCH v2 0/5] " Emmanuel Gil Peyrot
2021-10-27 22:35   ` [PATCH v2 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
2021-10-27 22:35   ` [PATCH v2 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
2021-11-30 22:45     ` Alexandre Belloni
2021-12-15 17:52       ` Emmanuel Gil Peyrot
2021-10-27 22:35   ` [PATCH v2 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform Emmanuel Gil Peyrot
2021-10-27 22:35   ` [PATCH v2 4/5] powerpc: gamecube_defconfig: Enable the RTC driver Emmanuel Gil Peyrot
2021-10-27 22:35   ` [PATCH v2 5/5] powerpc: wii_defconfig: " Emmanuel Gil Peyrot
2021-12-15 17:54   ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Emmanuel Gil Peyrot
2021-12-15 17:54     ` [PATCH v3 1/5] rtc: gamecube: " Emmanuel Gil Peyrot
2021-12-15 17:54     ` [PATCH v3 2/5] rtc: gamecube: Report low battery as invalid data Emmanuel Gil Peyrot
2021-12-15 17:54     ` [PATCH v3 3/5] powerpc: wii.dts: Expose HW_SRNPROT on this platform Emmanuel Gil Peyrot
2021-12-15 17:55     ` [PATCH v3 4/5] powerpc: gamecube_defconfig: Enable the RTC driver Emmanuel Gil Peyrot
2021-12-15 17:55     ` [PATCH v3 5/5] powerpc: wii_defconfig: " Emmanuel Gil Peyrot
2021-12-16  4:52     ` [PATCH v3 0/5] rtc: nintendo: Add a RTC driver for the GameCube, Wii and Wii U Michael Ellerman
2021-12-16  9:50       ` Alexandre Belloni
2021-12-16  9:49     ` Alexandre Belloni
2021-12-16 20:22       ` Emmanuel Gil Peyrot
2021-12-16 20:56         ` Alexandre Belloni
2021-12-16 20:58           ` Emmanuel Gil Peyrot

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