From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-15 03:14:17
This series adds a driver for the GPIO controller used in the Nintendo
Wii game console.
The driver itself, and the related devicetree work should be pretty
uncontroversial, but due to the system architecture of the Wii, I also
had to extend an old resource allocation hack to kernel/resource.c: On
the Wii, there are two separate RAM ranges, with MMIO right in the
middle, but AFAIK, Linux on PPC32 doesn't support discontiguous memory
properly. So the hack is to allocate one big RAM range with a hole
(marked as reserved memory) for MMIO in the middle.
Because this series touches different subsystems (GPIO, DT, core
resource management), I guess it should be picked up patch-by-patch by
the different maintainers.
Jonathan Neuschäfer (6):
resource: Extend the PPC32 reserved memory hack
powerpc: wii: Explicitly configure GPIO owner for poweroff pin
gpio: Add GPIO driver for Nintendo Wii
dt-bindings: gpio: Add binding for Wii GPIO controller
powerpc: wii.dts: Add ngpios property
powerpc: wii.dts: Add GPIO line names
.../bindings/gpio/nintendo,hollywood-gpio.txt | 27 +++
.../devicetree/bindings/powerpc/nintendo/wii.txt | 9 +-
arch/powerpc/boot/dts/wii.dts | 9 +
arch/powerpc/platforms/embedded6xx/wii.c | 7 +
drivers/gpio/Kconfig | 8 +
drivers/gpio/Makefile | 1 +
drivers/gpio/gpio-hlwd.c | 183 +++++++++++++++++++++
kernel/resource.c | 21 ++-
8 files changed, 256 insertions(+), 9 deletions(-)
create mode 100644 Documentation/devicetree/bindings/gpio/nintendo,hollywood-gpio.txt
create mode 100644 drivers/gpio/gpio-hlwd.c
--
2.15.1
From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-15 03:14:32
The Hollywood chipset's GPIO controller has two sets of registers: One
for access by the PowerPC CPU, and one for access by the ARM coprocessor
(but both are accessible from the PPC because the memory firewall
(AHBPROT) is usually disabled when booting Linux, today).
The wii_power_off function currently assumes that the poweroff GPIO pin
is configured for use via the ARM side, but the upcoming GPIO driver
configures all pins for use via the PPC side, breaking poweroff.
Configure the owner register explicitly in wii_power_off to make
wii_power_off work with and without the new GPIO driver.
I think the Wii can be switched to the generic gpio-poweroff driver,
after the GPIO driver is merged.
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
---
arch/powerpc/platforms/embedded6xx/wii.c | 7 +++++++
1 file changed, 7 insertions(+)
@@ -177,6 +178,12 @@ static void wii_power_off(void)local_irq_disable();if(hw_gpio){+/*+*settheowneroftheshutdownpintoARM,becauseitis+*accessedthroughtheregistersfortheARM,below+*/+clrbits32(hw_gpio+HW_GPIO_OWNER,HW_GPIO_SHUTDOWN);+/* make sure that the poweroff GPIO is configured as output */setbits32(hw_gpio+HW_GPIO_DIR(1),HW_GPIO_SHUTDOWN);
From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-15 03:14:38
On the Nintendo Wii, there are two ranges of physical memory, and MMIO
in between, but Linux on ppc32 doesn't support discontiguous memory.
Therefore a hack was introduced in commit c5df7f775148 ("powerpc: allow
ioremap within reserved memory regions") and commit de32400dd26e ("wii:
use both mem1 and mem2 as ram"):
- Treat the area from the start of the first memory area (MEM1) to the
end of the second (MEM2) as one big memory area, but mark the part
that doesn't belong to MEM1 or MEM2 as reserved.
- Only on the Wii, allow ioremap to be used on reserved memory.
This hack, however, doesn't account for the "resource"-based API in
kernel/resource.c, because __request_region performs its own checks.
Extend the hack to kernel/resource.c, to allow more drivers to allocate
their MMIO regions on the Wii.
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
---
kernel/resource.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-15 03:14:39
The Nintendo Wii's chipset (called "Hollywood") has a GPIO controller
that supports a configurable number of pins (up to 32), interrupts, and
some special mechanisms to share the controller between the system's
security processor (an ARM926) and the PowerPC CPU. Pin multiplexing is
not supported.
This patch adds a basic driver for this GPIO controller. Interrupt
support will come in a later patch.
This patch is based on code developed by Albert Herranz and the GameCube
Linux Team, file arch/powerpc/platforms/embedded6xx/hlwd-gpio.c,
available at https://github.com/DeltaResero/GC-Wii-Linux-Kernels, but
has grown quite dissimilar.
To compare this version of the driver against the original code:
$ git fetch https://github.com/DeltaResero/GC-Wii-Linux-Kernels
$ git co FETCH_HEAD -- arch/powerpc/platforms/embedded6xx/hlwd-gpio.c
$ diff -u arch/powerpc/platforms/embedded6xx/hlwd-gpio.c \
drivers/gpio/gpio-hlwd.c
Cc: Albert Herranz <redacted>
Cc: Segher Boessenkool <redacted>
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
---
This driver currently uses __raw_readl and __raw_writel to access the
GPIO controller's MMIO registers. I wonder if readl/writel plus explicit
byte-swapping would be more correct, because it could be independent of
the CPU's endianness. That said, this hardware only exists in two
big-endian machines (Wii and Wii U).
---
drivers/gpio/Kconfig | 8 +++
drivers/gpio/Makefile | 1 +
drivers/gpio/gpio-hlwd.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 192 insertions(+)
create mode 100644 drivers/gpio/gpio-hlwd.c
@@ -0,0 +1,183 @@+// SPDX-License-Identifier: GPL-2.0++// Copyright (C) 2008-2009 The GameCube Linux Team+// Copyright (C) 2008,2009 Albert Herranz+// Copyright (C) 2017-2018 Jonathan Neuschäfer+//+// Nintendo Wii (Hollywood) GPIO driver++#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt++#include<linux/io.h>+#include<linux/kernel.h>+#include<linux/of.h>+#include<linux/of_gpio.h>+#include<linux/of_platform.h>+#include<linux/slab.h>+#include<linux/module.h>+#include<linux/gpio/driver.h>+#include<linux/spinlock.h>++/*+*RegisternamesandoffsetscourtesyofWiiBrew:+*https://wiibrew.org/wiki/Hardware/Hollywood_GPIOs+*+*Notethatformostregisters,therearetwoversions:+*-HW_GPIOB_*IsalwaysaccessiblebytheBroadwayPowerPCcore,butdoes+*alwaysgiveaccesstoallGPIOlines+*-HW_GPIO_*IsonlyaccessiblebytheBroadwayPowerPCcodeifthememory+*firewall(AHBPROT)intheHollywoodchipsethasbeenconfiguredtoallow+*suchaccess.+*+*TheownershipofeachGPIOlinecanbeconfiguredintheHW_GPIO_OWNER+*register:AonebitconfiguresthelineforaccessviatheHW_GPIOB_*+*registers,azerobitindicatesaccessviaHW_GPIO_*.Thisdriveruses+*HW_GPIOB_*.+*/+#define HW_GPIOB_OUT 0x00+#define HW_GPIOB_DIR 0x04+#define HW_GPIOB_IN 0x08+#define HW_GPIOB_INTLVL 0x0c+#define HW_GPIOB_INTFLAG 0x10+#define HW_GPIOB_INTMASK 0x14+#define HW_GPIOB_INMIR 0x18+#define HW_GPIO_ENABLE 0x1c+#define HW_GPIO_OUT 0x20+#define HW_GPIO_DIR 0x24+#define HW_GPIO_IN 0x28+#define HW_GPIO_INTLVL 0x2c+#define HW_GPIO_INTFLAG 0x30+#define HW_GPIO_INTMASK 0x34+#define HW_GPIO_INMIR 0x38+#define HW_GPIO_OWNER 0x3c+++structhlwd_gpio{+structgpio_chipgpioc;+void__iomem*regs;+spinlock_tlock;+};++/*+*Updatethebitwiththegivenbitoffsetinthegivenregistertoagiven+*value+*/+staticvoidhlwd_gpio_update_bit(structgpio_chip*gc,unsignedintreg,+intoffset,intvalue)+{+structhlwd_gpio*hlwd=gpiochip_get_data(gc);+unsignedlongflags;+u32bit=1UL<<offset;+u32tmp;++spin_lock_irqsave(&hlwd->lock,flags);+tmp=__raw_readl(hlwd->regs+reg);+if(value)+__raw_writel(tmp|bit,hlwd->regs+reg);+else+__raw_writel(tmp&~bit,hlwd->regs+reg);+spin_unlock_irqrestore(&hlwd->lock,flags);+}++/* Read the bit with the given bit offset in the given register */+staticinthlwd_gpio_read_bit(structgpio_chip*gc,unsignedintreg,+unsignedintoffset)+{+structhlwd_gpio*hlwd=gpiochip_get_data(gc);+unsignedlongflags;+u32bit=1UL<<offset;+u32tmp;++spin_lock_irqsave(&hlwd->lock,flags);+tmp=__raw_readl(hlwd->regs+reg);+spin_unlock_irqrestore(&hlwd->lock,flags);++return!!(tmp&bit);+}++staticinthlwd_gpio_get(structgpio_chip*gc,unsignedintoffset)+{+returnhlwd_gpio_read_bit(gc,HW_GPIOB_IN,offset);+}++staticvoidhlwd_gpio_set(structgpio_chip*gc,unsignedintoffset,intval)+{+hlwd_gpio_update_bit(gc,HW_GPIOB_OUT,offset,val);+}++staticinthlwd_gpio_dir_in(structgpio_chip*gc,unsignedintoffset)+{+hlwd_gpio_update_bit(gc,HW_GPIOB_DIR,offset,0);++return0;+}++staticinthlwd_gpio_dir_out(structgpio_chip*gc,+unsignedintoffset,intval)+{+/* Set the GPIO value, and then set the direction */+hlwd_gpio_set(gc,offset,val);+hlwd_gpio_update_bit(gc,HW_GPIOB_DIR,offset,1);++return0;+}++staticinthlwd_gpio_probe(structplatform_device*pdev)+{+structhlwd_gpio*hlwd;+structresource*regs_resource;+u32ngpios;++hlwd=devm_kzalloc(&pdev->dev,sizeof(*hlwd),GFP_KERNEL);+if(!hlwd)+return-ENOMEM;++regs_resource=platform_get_resource(pdev,IORESOURCE_MEM,0);+if(IS_ERR(regs_resource))+returnPTR_ERR(regs_resource);++hlwd->regs=devm_ioremap_resource(&pdev->dev,regs_resource);+if(IS_ERR(hlwd->regs))+returnPTR_ERR(hlwd->regs);++/*+*ClaimallGPIOsusingtheOWNERregister.Thiswillnotworkon+*systemswheretheAHBPROTmemoryfirewallhasn'tbeenconfiguredto+*permitPPCaccesstoHW_GPIO_*.+*/+__raw_writel(0xffffffff,hlwd->regs+HW_GPIO_OWNER);++spin_lock_init(&hlwd->lock);++hlwd->gpioc.label=dev_name(&pdev->dev);+hlwd->gpioc.parent=&pdev->dev;+hlwd->gpioc.owner=THIS_MODULE;+hlwd->gpioc.direction_input=hlwd_gpio_dir_in;+hlwd->gpioc.direction_output=hlwd_gpio_dir_out;+hlwd->gpioc.get=hlwd_gpio_get;+hlwd->gpioc.set=hlwd_gpio_set;++if(of_property_read_u32(pdev->dev.of_node,"ngpios",&ngpios))+ngpios=32;+hlwd->gpioc.ngpio=ngpios;++returndevm_gpiochip_add_data(&pdev->dev,&hlwd->gpioc,hlwd);+}++staticconststructof_device_idhlwd_gpio_match[]={+{.compatible="nintendo,hollywood-gpio",},+{},+};+MODULE_DEVICE_TABLE(of,hlwd_gpio_match);++staticstructplatform_driverhlwd_gpio_driver={+.driver={+.name="hlwd_gpio",+.of_match_table=hlwd_gpio_match,+},+.probe=hlwd_gpio_probe,+};+module_platform_driver(hlwd_gpio_driver);++MODULE_AUTHOR("Jonathan Neuschäfer <j.neuschaefer@gmx.net>");+MODULE_DESCRIPTION("Nintendo Wii GPIO driver");+MODULE_LICENSE("GPL");
From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-15 03:14:45
These are the GPIO line names on a Nintendo Wii, as documented in:
https://wiibrew.org/wiki/Hardware/Hollywood_GPIOs
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
---
arch/powerpc/boot/dts/wii.dts | 8 ++++++++
1 file changed, 8 insertions(+)
From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-15 03:14:50
The Hollywood GPIO controller supports 32 GPIOs, but on the Wii, only 24
are used.
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
---
arch/powerpc/boot/dts/wii.dts | 1 +
1 file changed, 1 insertion(+)
@@ -0,0 +1,27 @@+Nintendo Wii (Hollywood) GPIO controller++Required properties:+- compatible: "nintendo,hollywood-gpio+- reg: Physical base address and length of the controller's registers.+- gpio-controller: Marks the device node as a GPIO controller.+- #gpio-cells: Should be <2>. The first cell is the pin number and the+ second cell is used to specify optional parameters:+ - bit 0 specifies polarity (0 for normal, 1 for inverted).++Optional properties:+- ngpios: see Documentation/devicetree/bindings/gpio/gpio.txt+- interrupt-controller: Marks the device node as an interrupt controller.+- #interrupt-cells: Should be two.+- interrupts: Interrupt specifier for the controller's Broadway (PowerPC)+ interrupt.+- interrupt-parent: phandle of the parent interrupt controller.++Example:++ GPIO: gpio@0d8000c0 {+ #gpio-cells = <2>;+ compatible = "nintendo,hollywood-gpio";+ reg = <0x0d8000c0 0x40>;+ gpio-controller;+ ngpios = <24>;+ }
@@ -152,14 +152,7 @@ Nintendo Wii device tree 1.l) The General Purpose I/O (GPIO) controller node- Represents the dual access 32 GPIO controller interface.-- Required properties:-- - #gpio-cells : <2>- - compatible : should be "nintendo,hollywood-gpio"- - reg : should contain the IPC registers location and length- - gpio-controller+ see Documentation/devicetree/bindings/gpio/nintendo,hollywood-gpio.txt 1.m) The control node
On Mon, Jan 15, 2018 at 4:13 AM, Jonathan Neusch=C3=A4fer
[off-list ref] wrote:
This patch is based on code developed by Albert Herranz and the GameCube
Linux Team, file arch/powerpc/platforms/embedded6xx/hlwd-gpio.c,
available at https://github.com/DeltaResero/GC-Wii-Linux-Kernels, but
has grown quite dissimilar.
I'm impressed by this effort. As with all reverse engineering.
This driver currently uses __raw_readl and __raw_writel to access the
GPIO controller's MMIO registers. I wonder if readl/writel plus explicit
byte-swapping would be more correct, because it could be independent of
the CPU's endianness. That said, this hardware only exists in two
big-endian machines (Wii and Wii U).
I don't know about PPC but I think you're supposed to use
ioread32be() and iowrite32be() to do explicit BE access.
But when I look at it, I think you can just use the gpio-mmio library
for this driver and cut down code cosiderably.
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Can't you just save a pointer to struct device *dev in the
state container and use dev_info(state->dev, ...) etc instead
of this?
+#include <linux/of_gpio.h>
This include should not be needed.
+/*
+ * Update the bit with the given bit offset in the given register to a g=
iven
+ * value
+ */
+static void hlwd_gpio_update_bit(struct gpio_chip *gc, unsigned int reg,
+ int offset, int value)
+{
+ struct hlwd_gpio *hlwd =3D gpiochip_get_data(gc);
+ unsigned long flags;
+ u32 bit =3D 1UL << offset;
#include <linux/bitops.h>
u32 bit =3D BIT(offset);
This looks very much like it is reimplementing the stuff we already
have in drivers/gpio/gpio-mmio.h.
There is even a big endian access flag for the library.
And you get so much for free with gpio-mmio.
select GPIO_GENERIC
in Kconfig
the helpers come in from <linux/gpio/driver.h>
Look at other drivers for inspiration:
git grep bgpio_init
If you need IRQ support you should probably have your own file
for this driver, but it will be just a few lines of wrapper using
bgpio_init() and BGPIOF_BIG_ENDIAN and/or possibly
BGPIOF_BIG_ENDIAN_BYTE_ORDER.
See the other drivers.
Yours,
Linus Walleij
From: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Date: 2018-01-16 21:58:57
On Tue, Jan 16, 2018 at 10:42:54AM +0100, Linus Walleij wrote:
On Mon, Jan 15, 2018 at 4:13 AM, Jonathan Neuschäfer
[off-list ref] wrote:
quoted
This patch is based on code developed by Albert Herranz and the GameCube
Linux Team, file arch/powerpc/platforms/embedded6xx/hlwd-gpio.c,
available at https://github.com/DeltaResero/GC-Wii-Linux-Kernels, but
has grown quite dissimilar.
I'm impressed by this effort. As with all reverse engineering.
quoted
This driver currently uses __raw_readl and __raw_writel to access the
GPIO controller's MMIO registers. I wonder if readl/writel plus explicit
byte-swapping would be more correct, because it could be independent of
the CPU's endianness. That said, this hardware only exists in two
big-endian machines (Wii and Wii U).
I don't know about PPC but I think you're supposed to use
ioread32be() and iowrite32be() to do explicit BE access.
Ah, that's the name! I didn't find ioread32*/iowrite32* in the
documentation or source code.
But when I look at it, I think you can just use the gpio-mmio library
for this driver and cut down code cosiderably.
I'll look into it. So far it looks good (drivers/gpio/gpio-iop.c has
just 60 lines).
quoted
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Can't you just save a pointer to struct device *dev in the
state container and use dev_info(state->dev, ...) etc instead
of this?
Makes sense. I'll try this out.
quoted
+#include <linux/of_gpio.h>
This include should not be needed.
Okay.
quoted
+/*
+ * Update the bit with the given bit offset in the given register to a given
+ * value
+ */
+static void hlwd_gpio_update_bit(struct gpio_chip *gc, unsigned int reg,
+ int offset, int value)
+{
+ struct hlwd_gpio *hlwd = gpiochip_get_data(gc);
+ unsigned long flags;
+ u32 bit = 1UL << offset;
This looks very much like it is reimplementing the stuff we already
have in drivers/gpio/gpio-mmio.h.
There is even a big endian access flag for the library.
And you get so much for free with gpio-mmio.
select GPIO_GENERIC
in Kconfig
the helpers come in from <linux/gpio/driver.h>
Look at other drivers for inspiration:
git grep bgpio_init
If you need IRQ support you should probably have your own file
for this driver, but it will be just a few lines of wrapper using
bgpio_init() and BGPIOF_BIG_ENDIAN and/or possibly
BGPIOF_BIG_ENDIAN_BYTE_ORDER.
Yes, I plan to add IRQ support in a later patch.
See the other drivers.
Yep, gpio-mmio looks like a good option, thanks for the pointer!
Thanks,
Jonathan Neuschäfer
@@ -0,0 +1,27 @@+Nintendo Wii (Hollywood) GPIO controller++Required properties:+- compatible: "nintendo,hollywood-gpio+- reg: Physical base address and length of the controller's registers.+- gpio-controller: Marks the device node as a GPIO controller.+- #gpio-cells: Should be <2>. The first cell is the pin number and the+ second cell is used to specify optional parameters:+ - bit 0 specifies polarity (0 for normal, 1 for inverted).++Optional properties:+- ngpios: see Documentation/devicetree/bindings/gpio/gpio.txt+- interrupt-controller: Marks the device node as an interrupt controller.+- #interrupt-cells: Should be two.+- interrupts: Interrupt specifier for the controller's Broadway (PowerPC)+ interrupt.+- interrupt-parent: phandle of the parent interrupt controller.++Example:++ GPIO: gpio@0d8000c0 {
Drop the leading 0.
With that,
Reviewed-by: Rob Herring <robh@kernel.org>