All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Apple M1 laptop keyboard support
@ 2022-01-16 17:06 Mark Kettenis
  2022-01-16 17:06 ` [PATCH 1/2] spi: apple: Add driver for Apple SPI controller Mark Kettenis
  2022-01-16 17:06 ` [PATCH 2/2] input: apple: Add support for Apple SPI keyboard Mark Kettenis
  0 siblings, 2 replies; 7+ messages in thread
From: Mark Kettenis @ 2022-01-16 17:06 UTC (permalink / raw)
  To: u-boot; +Cc: jagan, marcan, sjg, trini, Mark Kettenis

This short series implements support for the laptop keyboards on the
Apple Silicon Mac laptops.  These keyboards are connected to an SPI
bus, so this adds a driver for the SPI controller integrated on the
SoC, as well as the driver for the keyboard itself.  This makes it
possible to interact with U-Boot and EFI applications without the
need for an external USB keyboard.


Mark Kettenis (2):
  spi: apple: Add driver for Apple SPI controller
  input: apple: Add support for Apple SPI keyboard

 arch/arm/Kconfig              |   2 +
 configs/apple_m1_defconfig    |   1 +
 drivers/input/Kconfig         |   8 +
 drivers/input/Makefile        |   1 +
 drivers/input/apple_spi_kbd.c | 255 ++++++++++++++++++++++++++++++
 drivers/spi/Kconfig           |   7 +
 drivers/spi/Makefile          |   1 +
 drivers/spi/apple_spi.c       | 283 ++++++++++++++++++++++++++++++++++
 include/configs/apple.h       |   2 +-
 9 files changed, 559 insertions(+), 1 deletion(-)
 create mode 100644 drivers/input/apple_spi_kbd.c
 create mode 100644 drivers/spi/apple_spi.c

-- 
2.34.1


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

* [PATCH 1/2] spi: apple: Add driver for Apple SPI controller
  2022-01-16 17:06 [PATCH 0/2] Apple M1 laptop keyboard support Mark Kettenis
@ 2022-01-16 17:06 ` Mark Kettenis
  2022-01-22  1:40   ` Simon Glass
  2022-01-16 17:06 ` [PATCH 2/2] input: apple: Add support for Apple SPI keyboard Mark Kettenis
  1 sibling, 1 reply; 7+ messages in thread
From: Mark Kettenis @ 2022-01-16 17:06 UTC (permalink / raw)
  To: u-boot; +Cc: jagan, marcan, sjg, trini, Mark Kettenis

Add a driver for the SPI controller integrated on Apple SoCs.
This is necessary to support the keyboard on Apple Silicon laopts
since their keyboard uses an Apple-specific HID over SPI protocol.

Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
---
 arch/arm/Kconfig        |   2 +
 drivers/spi/Kconfig     |   7 +
 drivers/spi/Makefile    |   1 +
 drivers/spi/apple_spi.c | 283 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 293 insertions(+)
 create mode 100644 drivers/spi/apple_spi.c

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 04b4a20211..fed993a53d 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -937,6 +937,7 @@ config ARCH_APPLE
 	select DM_MAILBOX
 	select DM_RESET
 	select DM_SERIAL
+	select DM_SPI
 	select DM_USB
 	select DM_VIDEO
 	select IOMMU
@@ -946,6 +947,7 @@ config ARCH_APPLE
 	select POSITION_INDEPENDENT
 	select POWER_DOMAIN
 	select REGMAP
+	select SPI
 	select SYSCON
 	select SYSRESET
 	select SYSRESET_WATCHDOG
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index d07e9a28af..0a6a85f9c4 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -50,6 +50,13 @@ config ALTERA_SPI
 	  IP core. Please find details on the "Embedded Peripherals IP
 	  User Guide" of Altera.
 
+config APPLE_SPI
+	bool "Apple SPI driver"
+	default y if ARCH_APPLE
+	help
+	  Enable the Apple SPI driver. This driver can be used to
+	  access the SPI flash and keyboard on machines based on Apple SoCs.
+
 config ATCSPI200_SPI
 	bool "Andestech ATCSPI200 SPI driver"
 	help
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index d2f24bccef..bea746f3e3 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_SPI_MEM) += spi-mem-nodm.o
 endif
 
 obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
+obj-$(CONFIG_APPLE_SPI) += apple_spi.o
 obj-$(CONFIG_ATH79_SPI) += ath79_spi.o
 obj-$(CONFIG_ATMEL_QSPI) += atmel-quadspi.o
 obj-$(CONFIG_ATMEL_SPI) += atmel_spi.o
diff --git a/drivers/spi/apple_spi.c b/drivers/spi/apple_spi.c
new file mode 100644
index 0000000000..3ae89dc638
--- /dev/null
+++ b/drivers/spi/apple_spi.c
@@ -0,0 +1,283 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
+ * Copyright The Asahi Linux Contributors
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <clk.h>
+#include <spi.h>
+#include <asm/io.h>
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+
+#define APPLE_SPI_CTRL			0x000
+#define APPLE_SPI_CTRL_RUN		BIT(0)
+#define APPLE_SPI_CTRL_TX_RESET		BIT(2)
+#define APPLE_SPI_CTRL_RX_RESET		BIT(3)
+
+#define APPLE_SPI_CFG			0x004
+#define APPLE_SPI_CFG_CPHA		BIT(1)
+#define APPLE_SPI_CFG_CPOL		BIT(2)
+#define APPLE_SPI_CFG_MODE		GENMASK(6, 5)
+#define APPLE_SPI_CFG_MODE_POLLED	0
+#define APPLE_SPI_CFG_MODE_IRQ		1
+#define APPLE_SPI_CFG_MODE_DMA		2
+#define APPLE_SPI_CFG_IE_RXCOMPLETE	BIT(7)
+#define APPLE_SPI_CFG_IE_TXRXTHRESH	BIT(8)
+#define APPLE_SPI_CFG_LSB_FIRST		BIT(13)
+#define APPLE_SPI_CFG_WORD_SIZE		GENMASK(16, 15)
+#define APPLE_SPI_CFG_WORD_SIZE_8B	0
+#define APPLE_SPI_CFG_WORD_SIZE_16B	1
+#define APPLE_SPI_CFG_WORD_SIZE_32B	2
+#define APPLE_SPI_CFG_FIFO_THRESH	GENMASK(18, 17)
+#define APPLE_SPI_CFG_FIFO_THRESH_8B	0
+#define APPLE_SPI_CFG_FIFO_THRESH_4B	1
+#define APPLE_SPI_CFG_FIFO_THRESH_1B	2
+#define APPLE_SPI_CFG_IE_TXCOMPLETE	BIT(21)
+
+#define APPLE_SPI_STATUS		0x008
+#define APPLE_SPI_STATUS_RXCOMPLETE	BIT(0)
+#define APPLE_SPI_STATUS_TXRXTHRESH	BIT(1)
+#define APPLE_SPI_STATUS_TXCOMPLETE	BIT(2)
+
+#define APPLE_SPI_PIN			0x00c
+#define APPLE_SPI_PIN_KEEP_MOSI		BIT(0)
+#define APPLE_SPI_PIN_CS		BIT(1)
+
+#define APPLE_SPI_TXDATA		0x010
+#define APPLE_SPI_RXDATA		0x020
+#define APPLE_SPI_CLKDIV		0x030
+#define APPLE_SPI_CLKDIV_MIN		0x002
+#define APPLE_SPI_CLKDIV_MAX		0x7ff
+#define APPLE_SPI_RXCNT			0x034
+#define APPLE_SPI_WORD_DELAY		0x038
+#define APPLE_SPI_TXCNT			0x04c
+
+#define APPLE_SPI_FIFOSTAT		0x10c
+#define APPLE_SPI_FIFOSTAT_TXFULL	BIT(4)
+#define APPLE_SPI_FIFOSTAT_LEVEL_TX	GENMASK(15, 8)
+#define APPLE_SPI_FIFOSTAT_RXEMPTY	BIT(20)
+#define APPLE_SPI_FIFOSTAT_LEVEL_RX	GENMASK(31, 24)
+
+#define APPLE_SPI_IE_XFER		0x130
+#define APPLE_SPI_IF_XFER		0x134
+#define APPLE_SPI_XFER_RXCOMPLETE	BIT(0)
+#define APPLE_SPI_XFER_TXCOMPLETE	BIT(1)
+
+#define APPLE_SPI_IE_FIFO		0x138
+#define APPLE_SPI_IF_FIFO		0x13c
+#define APPLE_SPI_FIFO_RXTHRESH		BIT(4)
+#define APPLE_SPI_FIFO_TXTHRESH		BIT(5)
+#define APPLE_SPI_FIFO_RXFULL		BIT(8)
+#define APPLE_SPI_FIFO_TXEMPTY		BIT(9)
+#define APPLE_SPI_FIFO_RXUNDERRUN	BIT(16)
+#define APPLE_SPI_FIFO_TXOVERFLOW	BIT(17)
+
+#define APPLE_SPI_SHIFTCFG		0x150
+#define APPLE_SPI_SHIFTCFG_CLK_ENABLE	BIT(0)
+#define APPLE_SPI_SHIFTCFG_CS_ENABLE	BIT(1)
+#define APPLE_SPI_SHIFTCFG_AND_CLK_DATA	BIT(8)
+#define APPLE_SPI_SHIFTCFG_CS_AS_DATA	BIT(9)
+#define APPLE_SPI_SHIFTCFG_TX_ENABLE	BIT(10)
+#define APPLE_SPI_SHIFTCFG_RX_ENABLE	BIT(11)
+#define APPLE_SPI_SHIFTCFG_BITS		GENMASK(21, 16)
+#define APPLE_SPI_SHIFTCFG_OVERRIDE_CS	BIT(24)
+
+#define APPLE_SPI_PINCFG		0x154
+#define APPLE_SPI_PINCFG_KEEP_CLK	BIT(0)
+#define APPLE_SPI_PINCFG_KEEP_CS	BIT(1)
+#define APPLE_SPI_PINCFG_KEEP_MOSI	BIT(2)
+#define APPLE_SPI_PINCFG_CLK_IDLE_VAL	BIT(8)
+#define APPLE_SPI_PINCFG_CS_IDLE_VAL	BIT(9)
+#define APPLE_SPI_PINCFG_MOSI_IDLE_VAL	BIT(10)
+
+#define APPLE_SPI_DELAY_PRE		0x160
+#define APPLE_SPI_DELAY_POST		0x168
+#define APPLE_SPI_DELAY_ENABLE		BIT(0)
+#define APPLE_SPI_DELAY_NO_INTERBYTE	BIT(1)
+#define APPLE_SPI_DELAY_SET_SCK		BIT(4)
+#define APPLE_SPI_DELAY_SET_MOSI	BIT(6)
+#define APPLE_SPI_DELAY_SCK_VAL		BIT(8)
+#define APPLE_SPI_DELAY_MOSI_VAL	BIT(12)
+
+#define APPLE_SPI_FIFO_DEPTH		16
+
+#define APPLE_SPI_TIMEOUT_MS		200
+
+struct apple_spi_priv {
+	void *base;
+	u32 clkfreq;
+};
+
+static void apple_spi_set_cs(struct apple_spi_priv *priv, int on)
+{
+	writel(on ? 0 : APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
+}
+
+static void apple_spi_tx(struct apple_spi_priv *priv, uint *len,
+			 const void **dout)
+{
+	const u8 *out = *dout;
+	u32 data, fifostat;
+	uint count;
+
+	fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
+	count = APPLE_SPI_FIFO_DEPTH -
+		FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_TX, fifostat);
+	while (*len > 0 && count > 0) {
+		data = out ? *out++ : 0;
+		writel(data, priv->base + APPLE_SPI_TXDATA);
+		(*len)--;
+		count--;
+	}
+
+	*dout = out;
+}
+
+static void apple_spi_rx(struct apple_spi_priv *priv, uint *len,
+			 void **din)
+{
+	u8 *in = *din;
+	u32 data, fifostat;
+	uint count;
+
+	fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
+	count = FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_RX, fifostat);
+	while (*len > 0 && count > 0) {
+		data = readl(priv->base + APPLE_SPI_RXDATA);
+		if (in)
+			*in++ = data;
+		(*len)--;
+		count--;
+	}
+
+	*din = in;
+}
+
+static int apple_spi_xfer(struct udevice *dev, unsigned int bitlen,
+			  const void *dout, void *din, unsigned long flags)
+{
+	struct apple_spi_priv *priv = dev_get_priv(dev->parent);
+	unsigned long start = get_timer(0);
+	uint txlen, rxlen;
+	int ret = 0;
+
+	if ((bitlen % 8) != 0)
+		return -EINVAL;
+	txlen = rxlen = bitlen / 8;
+
+	if (flags & SPI_XFER_BEGIN)
+		apple_spi_set_cs(priv, 1);
+
+	if (txlen > 0) {
+		/* Reset FIFOs */
+		writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
+		       priv->base + APPLE_SPI_CTRL);
+
+		/* Set the transfer length */
+		writel(txlen, priv->base + APPLE_SPI_TXCNT);
+		writel(rxlen, priv->base + APPLE_SPI_RXCNT);
+
+		/* Prime transmit FIFO */
+		apple_spi_tx(priv, &txlen, &dout);
+
+		/* Start transfer */
+		writel(APPLE_SPI_CTRL_RUN, priv->base + APPLE_SPI_CTRL);
+
+		while ((txlen > 0 || rxlen > 0)) {
+			apple_spi_rx(priv, &rxlen, &din);
+			apple_spi_tx(priv, &txlen, &dout);
+
+			if (get_timer(start) > APPLE_SPI_TIMEOUT_MS) {
+				ret = -ETIMEDOUT;
+				break;
+			}
+		}
+
+		/* Stop transfer. */
+		writel(0, priv->base + APPLE_SPI_CTRL);
+	}
+
+	if (flags & SPI_XFER_END)
+		apple_spi_set_cs(priv, 0);
+
+	return ret;
+}
+
+static int apple_spi_set_speed(struct udevice *dev, uint speed)
+{
+	struct apple_spi_priv *priv = dev_get_priv(dev);
+	u32 div;
+
+	div = DIV_ROUND_UP(priv->clkfreq, speed);
+	if (div < APPLE_SPI_CLKDIV_MIN)
+		div = APPLE_SPI_CLKDIV_MIN;
+	if (div > APPLE_SPI_CLKDIV_MAX)
+		div = APPLE_SPI_CLKDIV_MAX;
+
+	writel(div, priv->base + APPLE_SPI_CLKDIV);
+
+	return 0;
+}
+
+static int apple_spi_set_mode(struct udevice *bus, uint mode)
+{
+	return 0;
+}
+
+struct dm_spi_ops apple_spi_ops = {
+	.xfer = apple_spi_xfer,
+	.set_speed = apple_spi_set_speed,
+	.set_mode = apple_spi_set_mode,
+};
+
+static int apple_spi_probe(struct udevice *dev)
+{
+	struct apple_spi_priv *priv = dev_get_priv(dev);
+	struct clk clkdev;
+	int ret;
+
+	priv->base = dev_read_addr_ptr(dev);
+	if (!priv->base)
+		return -EINVAL;
+
+	ret = clk_get_by_index(dev, 0, &clkdev);
+	if (ret)
+		return ret;
+	priv->clkfreq = clk_get_rate(&clkdev);
+
+	/* Set CS high (inactive) and disable override and auto-CS */
+	writel(APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
+	writel(readl(priv->base + APPLE_SPI_SHIFTCFG) & ~APPLE_SPI_SHIFTCFG_OVERRIDE_CS,
+	       priv->base + APPLE_SPI_SHIFTCFG);
+	writel((readl(priv->base + APPLE_SPI_PINCFG) & ~APPLE_SPI_PINCFG_CS_IDLE_VAL) |
+	       APPLE_SPI_PINCFG_KEEP_CS, priv->base + APPLE_SPI_PINCFG);
+
+	/* Reset FIFOs */
+	writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
+	       priv->base + APPLE_SPI_CTRL);
+
+	/* Configure defaults */
+	writel(FIELD_PREP(APPLE_SPI_CFG_MODE, APPLE_SPI_CFG_MODE_IRQ) |
+	       FIELD_PREP(APPLE_SPI_CFG_WORD_SIZE, APPLE_SPI_CFG_WORD_SIZE_8B) |
+	       FIELD_PREP(APPLE_SPI_CFG_FIFO_THRESH, APPLE_SPI_CFG_FIFO_THRESH_8B),
+	       priv->base + APPLE_SPI_CFG);
+
+	return 0;
+}
+
+static const struct udevice_id apple_spi_of_match[] = {
+	{ .compatible = "apple,spi" },
+	{ /* sentinel */ }
+};
+
+U_BOOT_DRIVER(apple_spi) = {
+	.name = "apple_spi",
+	.id = UCLASS_SPI,
+	.of_match = apple_spi_of_match,
+	.probe = apple_spi_probe,
+	.priv_auto = sizeof(struct apple_spi_priv),
+	.ops = &apple_spi_ops,
+};
-- 
2.34.1


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

* [PATCH 2/2] input: apple: Add support for Apple SPI keyboard
  2022-01-16 17:06 [PATCH 0/2] Apple M1 laptop keyboard support Mark Kettenis
  2022-01-16 17:06 ` [PATCH 1/2] spi: apple: Add driver for Apple SPI controller Mark Kettenis
@ 2022-01-16 17:06 ` Mark Kettenis
  2022-01-22  1:40   ` Simon Glass
  1 sibling, 1 reply; 7+ messages in thread
From: Mark Kettenis @ 2022-01-16 17:06 UTC (permalink / raw)
  To: u-boot; +Cc: jagan, marcan, sjg, trini, Mark Kettenis

This driver adds support for the keyboard on Apple Silicon laptops.
The controller for this keyboard sits on an SPI bus and uses an
Apple-specific HID over SPI protocol. The packets sent by this
controller for key presses and key releases are fairly simple and
are decoded directly by the code in this driver and converted into
standard Linux keycodes. The same controller handles the touchpad
found on these laptops.  Packets for touchpad events are simply
ignored.

Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
---
 configs/apple_m1_defconfig    |   1 +
 drivers/input/Kconfig         |   8 ++
 drivers/input/Makefile        |   1 +
 drivers/input/apple_spi_kbd.c | 255 ++++++++++++++++++++++++++++++++++
 include/configs/apple.h       |   2 +-
 5 files changed, 266 insertions(+), 1 deletion(-)
 create mode 100644 drivers/input/apple_spi_kbd.c

diff --git a/configs/apple_m1_defconfig b/configs/apple_m1_defconfig
index 1528217b17..433cc62334 100644
--- a/configs/apple_m1_defconfig
+++ b/configs/apple_m1_defconfig
@@ -15,5 +15,6 @@ CONFIG_NVME_APPLE=y
 CONFIG_USB_XHCI_HCD=y
 CONFIG_USB_XHCI_DWC3=y
 CONFIG_USB_KEYBOARD=y
+CONFIG_APPLE_SPI_KEYB=y
 CONFIG_VIDEO_SIMPLE=y
 # CONFIG_GENERATE_SMBIOS_TABLE is not set
diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index 0b753f37bf..2718b3674a 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -47,6 +47,14 @@ config KEYBOARD
 	  and is only used by novena. For new boards, use driver model
 	  instead.
 
+config APPLE_SPI_KEYB
+	bool "Enable Apple SPI keyboard support"
+	depends on DM_KEYBOARD && DM_SPI
+	help
+	  This adds a driver for the keyboards found on various
+	  laptops based on Apple SoCs. These keyboards use an
+	  Apple-specific HID-over-SPI protocol.
+
 config CROS_EC_KEYB
 	bool "Enable Chrome OS EC keyboard support"
 	depends on INPUT
diff --git a/drivers/input/Makefile b/drivers/input/Makefile
index e440c921e4..b1133f772f 100644
--- a/drivers/input/Makefile
+++ b/drivers/input/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_$(SPL_TPL_)DM_KEYBOARD) += input.o keyboard-uclass.o
 
 ifndef CONFIG_SPL_BUILD
 
+obj-$(CONFIG_APPLE_SPI_KEYB) += apple_spi_kbd.o
 obj-$(CONFIG_I8042_KEYB) += i8042.o
 obj-$(CONFIG_TEGRA_KEYBOARD) += input.o tegra-kbc.o
 obj-$(CONFIG_TWL4030_INPUT) += twl4030.o
diff --git a/drivers/input/apple_spi_kbd.c b/drivers/input/apple_spi_kbd.c
new file mode 100644
index 0000000000..7e034d6008
--- /dev/null
+++ b/drivers/input/apple_spi_kbd.c
@@ -0,0 +1,255 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <keyboard.h>
+#include <spi.h>
+#include <stdio_dev.h>
+#include <asm-generic/gpio.h>
+#include <linux/delay.h>
+#include <linux/input.h>
+
+/*
+ * The Apple SPI keyboard controller implements a protocol that
+ * closely resembles HID Keyboard Boot protocol.  The key codes are
+ * mapped according to the HID Keyboard/Keypad Usage Table.
+ */
+
+/* Modifier key bits */
+#define HID_MOD_LEFTCTRL	BIT(0)
+#define HID_MOD_LEFTSHIFT	BIT(1)
+#define HID_MOD_LEFTALT		BIT(2)
+#define HID_MOD_LEFTGUI		BIT(3)
+#define HID_MOD_RIGHTCTRL	BIT(4)
+#define HID_MOD_RIGHTSHIFT	BIT(5)
+#define HID_MOD_RIGHTALT	BIT(6)
+#define HID_MOD_RIGHTGUI	BIT(7)
+
+static const u8 hid_kbd_keymap[] = {
+	KEY_RESERVED, 0xff, 0xff, 0xff,
+	KEY_A, KEY_B, KEY_C, KEY_D,
+	KEY_E, KEY_F, KEY_G, KEY_H,
+	KEY_I, KEY_J, KEY_K, KEY_L,
+	KEY_M, KEY_N, KEY_O, KEY_P,
+	KEY_Q, KEY_R, KEY_S, KEY_T,
+	KEY_U, KEY_V, KEY_W, KEY_X,
+	KEY_Y, KEY_Z, KEY_1, KEY_2,
+	KEY_3, KEY_4, KEY_5, KEY_6,
+	KEY_7, KEY_8, KEY_9, KEY_0,
+	KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB,
+	KEY_SPACE, KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE,
+	KEY_RIGHTBRACE, KEY_BACKSLASH, 0xff, KEY_SEMICOLON,
+	KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT,
+	KEY_SLASH, KEY_CAPSLOCK, KEY_F1, KEY_F2,
+	KEY_F3, KEY_F4, KEY_F5, KEY_F6,
+	KEY_F7, KEY_F8, KEY_F9, KEY_F10,
+	KEY_F11, KEY_F12, KEY_SYSRQ, KEY_SCROLLLOCK,
+	KEY_PAUSE, KEY_INSERT, KEY_HOME, KEY_PAGEUP,
+	KEY_DELETE, KEY_END, KEY_PAGEDOWN, KEY_RIGHT,
+	KEY_LEFT, KEY_DOWN, KEY_UP, KEY_NUMLOCK,
+	KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS, KEY_KPPLUS,
+	KEY_KPENTER, KEY_KP1, KEY_KP2, KEY_KP3,
+	KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7,
+	KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT,
+	KEY_BACKSLASH, KEY_COMPOSE, KEY_POWER, KEY_KPEQUAL,
+};
+
+/* Report ID used for keyboard input reports. */
+#define KBD_REPORTID	0x01
+
+struct apple_spi_kbd_report {
+	u8 reportid;
+	u8 modifiers;
+	u8 reserved;
+	u8 keycode[6];
+	u8 fn;
+};
+
+struct apple_spi_kbd_priv {
+	struct gpio_desc enable;
+	struct apple_spi_kbd_report old;
+	struct apple_spi_kbd_report new;
+};
+
+/* Keyboard device. */
+#define KBD_DEVICE	0x01
+
+struct apple_spi_kbd_packet {
+	u8 flags;
+#define PACKET_READ	0x20
+	u8 device;
+	u16 offset;
+	u16 remaining;
+	u16 len;
+	u8 data[246];
+	u16 crc;
+};
+
+struct apple_spi_kbd_msg {
+	u8 type;
+#define MSG_REPORT	0x10
+	u8 device;
+	u8 unknown;
+	u8 msgid;
+	u16 rsplen;
+	u16 cmdlen;
+	u8 data[0];
+};
+
+static void apple_spi_kbd_service_modifiers(struct input_config *input)
+{
+	struct apple_spi_kbd_priv *priv = dev_get_priv(input->dev);
+	u8 new = priv->new.modifiers;
+	u8 old = priv->old.modifiers;
+
+	if ((new ^ old) & HID_MOD_LEFTCTRL)
+		input_add_keycode(input, KEY_LEFTCTRL,
+				  old & HID_MOD_LEFTCTRL);
+	if ((new ^ old) & HID_MOD_RIGHTCTRL)
+		input_add_keycode(input, KEY_RIGHTCTRL,
+				  old & HID_MOD_RIGHTCTRL);
+	if ((new ^ old) & HID_MOD_LEFTSHIFT)
+		input_add_keycode(input, KEY_LEFTSHIFT,
+				  old & HID_MOD_LEFTSHIFT);
+	if ((new ^ old) & HID_MOD_RIGHTSHIFT)
+		input_add_keycode(input, KEY_RIGHTSHIFT,
+				  old & HID_MOD_RIGHTSHIFT);
+	if ((new ^ old) & HID_MOD_LEFTALT)
+		input_add_keycode(input, KEY_LEFTALT,
+				  old & HID_MOD_LEFTALT);
+	if ((new ^ old) & HID_MOD_RIGHTALT)
+		input_add_keycode(input, KEY_RIGHTALT,
+				  old & HID_MOD_RIGHTALT);
+	if ((new ^ old) & HID_MOD_LEFTGUI)
+		input_add_keycode(input, KEY_LEFTMETA,
+				  old & HID_MOD_LEFTGUI);
+	if ((new ^ old) & HID_MOD_RIGHTGUI)
+		input_add_keycode(input, KEY_RIGHTMETA,
+				  old & HID_MOD_RIGHTGUI);
+}
+
+static void apple_spi_kbd_service_key(struct input_config *input, int i,
+				      int released)
+{
+	struct apple_spi_kbd_priv *priv = dev_get_priv(input->dev);
+	u8 *new;
+	u8 *old;
+
+	if (released) {
+		new = priv->new.keycode;
+		old = priv->old.keycode;
+	} else {
+		new = priv->old.keycode;
+		old = priv->new.keycode;
+	}
+
+	if (memscan(new, old[i], sizeof(priv->new.keycode)) ==
+	    new + sizeof(priv->new.keycode) &&
+	    old[i] < ARRAY_SIZE(hid_kbd_keymap))
+		input_add_keycode(input, hid_kbd_keymap[old[i]], released);
+}
+
+static int apple_spi_kbd_check(struct input_config *input)
+{
+	struct udevice *dev = input->dev;
+	struct apple_spi_kbd_priv *priv = dev_get_priv(dev);
+	struct apple_spi_kbd_packet packet;
+	struct apple_spi_kbd_msg *msg;
+	struct apple_spi_kbd_report *report;
+	int i, ret;
+
+	memset(&packet, 0, sizeof(packet));
+
+	ret = dm_spi_claim_bus(dev);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * The keyboard controller needs delays after asserting CS#
+	 * and before deasserting CS#.
+	 */
+	dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_BEGIN);
+	udelay(100);
+	dm_spi_xfer(dev, sizeof(packet) * 8, NULL, &packet, 0);
+	udelay(100);
+	dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END);
+
+	dm_spi_release_bus(dev);
+
+	/*
+	 * The keyboard controller needs a delay between subsequent
+	 * SPI transfers.
+	 */
+	udelay(250);
+
+	msg = (struct apple_spi_kbd_msg *)packet.data;
+	report = (struct apple_spi_kbd_report *)msg->data;
+	if (packet.flags == PACKET_READ && packet.device == KBD_DEVICE &&
+	    msg->type == MSG_REPORT && msg->device == KBD_DEVICE &&
+	    msg->cmdlen == sizeof(struct apple_spi_kbd_report) &&
+	    report->reportid == KBD_REPORTID) {
+		memcpy(&priv->new, report,
+		       sizeof(struct apple_spi_kbd_report));
+		apple_spi_kbd_service_modifiers(input);
+		for (i = 0; i < sizeof(priv->new.keycode); i++) {
+			apple_spi_kbd_service_key(input, i, 1);
+			apple_spi_kbd_service_key(input, i, 0);
+		}
+		memcpy(&priv->old, &priv->new,
+		       sizeof(struct apple_spi_kbd_report));
+		return 1;
+	}
+
+	return 0;
+}
+
+static int apple_spi_kbd_probe(struct udevice *dev)
+{
+	struct apple_spi_kbd_priv *priv = dev_get_priv(dev);
+	struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
+	struct stdio_dev *sdev = &uc_priv->sdev;
+	struct input_config *input = &uc_priv->input;
+	int ret;
+
+	ret = gpio_request_by_name(dev, "spien-gpios", 0, &priv->enable,
+				   GPIOD_IS_OUT);
+	if (ret < 0)
+		return ret;
+
+	/* Reset the keyboard controller. */
+	dm_gpio_set_value(&priv->enable, 1);
+	udelay(5000);
+	dm_gpio_set_value(&priv->enable, 0);
+	udelay(5000);
+
+	/* Enable the keyboard controller. */
+	dm_gpio_set_value(&priv->enable, 1);
+	udelay(50000);
+
+	input->dev = dev;
+	input->read_keys = apple_spi_kbd_check;
+	input_add_tables(input, false);
+	strcpy(sdev->name, "spikbd");
+
+	return input_stdio_register(sdev);
+}
+
+static const struct keyboard_ops apple_spi_kbd_ops = {
+};
+
+static const struct udevice_id apple_spi_kbd_of_match[] = {
+	{ .compatible = "apple,spi-hid-transport" },
+	{ /* sentinel */ }
+};
+
+U_BOOT_DRIVER(apple_spi_kbd) = {
+	.name = "apple_spi_kbd",
+	.id = UCLASS_KEYBOARD,
+	.of_match = apple_spi_kbd_of_match,
+	.probe = apple_spi_kbd_probe,
+	.priv_auto = sizeof(struct apple_spi_kbd_priv),
+	.ops = &apple_spi_kbd_ops,
+};
diff --git a/include/configs/apple.h b/include/configs/apple.h
index 47faad8150..f12e9bdef5 100644
--- a/include/configs/apple.h
+++ b/include/configs/apple.h
@@ -5,7 +5,7 @@
 
 /* Environment */
 #define ENV_DEVICE_SETTINGS \
-	"stdin=serial,usbkbd\0" \
+	"stdin=serial,usbkbd,spikbd\0" \
 	"stdout=serial,vidconsole\0" \
 	"stderr=serial,vidconsole\0"
 
-- 
2.34.1


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

* Re: [PATCH 1/2] spi: apple: Add driver for Apple SPI controller
  2022-01-16 17:06 ` [PATCH 1/2] spi: apple: Add driver for Apple SPI controller Mark Kettenis
@ 2022-01-22  1:40   ` Simon Glass
  2022-01-22 15:53     ` Mark Kettenis
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Glass @ 2022-01-22  1:40 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: U-Boot Mailing List, Jagan Teki, marcan, Tom Rini

Hi Mark,

On Sun, 16 Jan 2022 at 10:06, Mark Kettenis <kettenis@openbsd.org> wrote:
>
> Add a driver for the SPI controller integrated on Apple SoCs.
> This is necessary to support the keyboard on Apple Silicon laopts
> since their keyboard uses an Apple-specific HID over SPI protocol.
>
> Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
> ---
>  arch/arm/Kconfig        |   2 +
>  drivers/spi/Kconfig     |   7 +
>  drivers/spi/Makefile    |   1 +
>  drivers/spi/apple_spi.c | 283 ++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 293 insertions(+)
>  create mode 100644 drivers/spi/apple_spi.c

Reviewed-by: Simon Glass <sjg@chromium.org>
Tested on: Macbook Air M1
Tested-by: Simon Glass <sjg@chromium.org>

with nits below

Shouldn't you enable SPI flash and 'sspi' and 'sf' commands as well?

>
> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
> index 04b4a20211..fed993a53d 100644
> --- a/arch/arm/Kconfig
> +++ b/arch/arm/Kconfig
> @@ -937,6 +937,7 @@ config ARCH_APPLE
>         select DM_MAILBOX
>         select DM_RESET
>         select DM_SERIAL
> +       select DM_SPI
>         select DM_USB
>         select DM_VIDEO
>         select IOMMU
> @@ -946,6 +947,7 @@ config ARCH_APPLE
>         select POSITION_INDEPENDENT
>         select POWER_DOMAIN
>         select REGMAP
> +       select SPI
>         select SYSCON
>         select SYSRESET
>         select SYSRESET_WATCHDOG
> diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
> index d07e9a28af..0a6a85f9c4 100644
> --- a/drivers/spi/Kconfig
> +++ b/drivers/spi/Kconfig
> @@ -50,6 +50,13 @@ config ALTERA_SPI
>           IP core. Please find details on the "Embedded Peripherals IP
>           User Guide" of Altera.
>
> +config APPLE_SPI
> +       bool "Apple SPI driver"
> +       default y if ARCH_APPLE
> +       help
> +         Enable the Apple SPI driver. This driver can be used to
> +         access the SPI flash and keyboard on machines based on Apple SoCs.
> +
>  config ATCSPI200_SPI
>         bool "Andestech ATCSPI200 SPI driver"
>         help
> diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
> index d2f24bccef..bea746f3e3 100644
> --- a/drivers/spi/Makefile
> +++ b/drivers/spi/Makefile
> @@ -18,6 +18,7 @@ obj-$(CONFIG_SPI_MEM) += spi-mem-nodm.o
>  endif
>
>  obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
> +obj-$(CONFIG_APPLE_SPI) += apple_spi.o
>  obj-$(CONFIG_ATH79_SPI) += ath79_spi.o
>  obj-$(CONFIG_ATMEL_QSPI) += atmel-quadspi.o
>  obj-$(CONFIG_ATMEL_SPI) += atmel_spi.o
> diff --git a/drivers/spi/apple_spi.c b/drivers/spi/apple_spi.c
> new file mode 100644
> index 0000000000..3ae89dc638
> --- /dev/null
> +++ b/drivers/spi/apple_spi.c
> @@ -0,0 +1,283 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
> + * Copyright The Asahi Linux Contributors
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <clk.h>
> +#include <spi.h>
> +#include <asm/io.h>
> +#include <linux/bitfield.h>
> +#include <linux/delay.h>
> +
> +#define APPLE_SPI_CTRL                 0x000
> +#define APPLE_SPI_CTRL_RUN             BIT(0)
> +#define APPLE_SPI_CTRL_TX_RESET                BIT(2)
> +#define APPLE_SPI_CTRL_RX_RESET                BIT(3)
> +
> +#define APPLE_SPI_CFG                  0x004
> +#define APPLE_SPI_CFG_CPHA             BIT(1)
> +#define APPLE_SPI_CFG_CPOL             BIT(2)
> +#define APPLE_SPI_CFG_MODE             GENMASK(6, 5)
> +#define APPLE_SPI_CFG_MODE_POLLED      0
> +#define APPLE_SPI_CFG_MODE_IRQ         1
> +#define APPLE_SPI_CFG_MODE_DMA         2
> +#define APPLE_SPI_CFG_IE_RXCOMPLETE    BIT(7)
> +#define APPLE_SPI_CFG_IE_TXRXTHRESH    BIT(8)
> +#define APPLE_SPI_CFG_LSB_FIRST                BIT(13)
> +#define APPLE_SPI_CFG_WORD_SIZE                GENMASK(16, 15)
> +#define APPLE_SPI_CFG_WORD_SIZE_8B     0
> +#define APPLE_SPI_CFG_WORD_SIZE_16B    1
> +#define APPLE_SPI_CFG_WORD_SIZE_32B    2
> +#define APPLE_SPI_CFG_FIFO_THRESH      GENMASK(18, 17)
> +#define APPLE_SPI_CFG_FIFO_THRESH_8B   0
> +#define APPLE_SPI_CFG_FIFO_THRESH_4B   1
> +#define APPLE_SPI_CFG_FIFO_THRESH_1B   2
> +#define APPLE_SPI_CFG_IE_TXCOMPLETE    BIT(21)
> +
> +#define APPLE_SPI_STATUS               0x008
> +#define APPLE_SPI_STATUS_RXCOMPLETE    BIT(0)
> +#define APPLE_SPI_STATUS_TXRXTHRESH    BIT(1)
> +#define APPLE_SPI_STATUS_TXCOMPLETE    BIT(2)
> +
> +#define APPLE_SPI_PIN                  0x00c
> +#define APPLE_SPI_PIN_KEEP_MOSI                BIT(0)
> +#define APPLE_SPI_PIN_CS               BIT(1)
> +
> +#define APPLE_SPI_TXDATA               0x010
> +#define APPLE_SPI_RXDATA               0x020
> +#define APPLE_SPI_CLKDIV               0x030
> +#define APPLE_SPI_CLKDIV_MIN           0x002
> +#define APPLE_SPI_CLKDIV_MAX           0x7ff
> +#define APPLE_SPI_RXCNT                        0x034
> +#define APPLE_SPI_WORD_DELAY           0x038
> +#define APPLE_SPI_TXCNT                        0x04c
> +
> +#define APPLE_SPI_FIFOSTAT             0x10c
> +#define APPLE_SPI_FIFOSTAT_TXFULL      BIT(4)
> +#define APPLE_SPI_FIFOSTAT_LEVEL_TX    GENMASK(15, 8)
> +#define APPLE_SPI_FIFOSTAT_RXEMPTY     BIT(20)
> +#define APPLE_SPI_FIFOSTAT_LEVEL_RX    GENMASK(31, 24)
> +
> +#define APPLE_SPI_IE_XFER              0x130
> +#define APPLE_SPI_IF_XFER              0x134
> +#define APPLE_SPI_XFER_RXCOMPLETE      BIT(0)
> +#define APPLE_SPI_XFER_TXCOMPLETE      BIT(1)
> +
> +#define APPLE_SPI_IE_FIFO              0x138
> +#define APPLE_SPI_IF_FIFO              0x13c
> +#define APPLE_SPI_FIFO_RXTHRESH                BIT(4)
> +#define APPLE_SPI_FIFO_TXTHRESH                BIT(5)
> +#define APPLE_SPI_FIFO_RXFULL          BIT(8)
> +#define APPLE_SPI_FIFO_TXEMPTY         BIT(9)
> +#define APPLE_SPI_FIFO_RXUNDERRUN      BIT(16)
> +#define APPLE_SPI_FIFO_TXOVERFLOW      BIT(17)
> +
> +#define APPLE_SPI_SHIFTCFG             0x150
> +#define APPLE_SPI_SHIFTCFG_CLK_ENABLE  BIT(0)
> +#define APPLE_SPI_SHIFTCFG_CS_ENABLE   BIT(1)
> +#define APPLE_SPI_SHIFTCFG_AND_CLK_DATA        BIT(8)
> +#define APPLE_SPI_SHIFTCFG_CS_AS_DATA  BIT(9)
> +#define APPLE_SPI_SHIFTCFG_TX_ENABLE   BIT(10)
> +#define APPLE_SPI_SHIFTCFG_RX_ENABLE   BIT(11)
> +#define APPLE_SPI_SHIFTCFG_BITS                GENMASK(21, 16)
> +#define APPLE_SPI_SHIFTCFG_OVERRIDE_CS BIT(24)
> +
> +#define APPLE_SPI_PINCFG               0x154
> +#define APPLE_SPI_PINCFG_KEEP_CLK      BIT(0)
> +#define APPLE_SPI_PINCFG_KEEP_CS       BIT(1)
> +#define APPLE_SPI_PINCFG_KEEP_MOSI     BIT(2)
> +#define APPLE_SPI_PINCFG_CLK_IDLE_VAL  BIT(8)
> +#define APPLE_SPI_PINCFG_CS_IDLE_VAL   BIT(9)
> +#define APPLE_SPI_PINCFG_MOSI_IDLE_VAL BIT(10)
> +
> +#define APPLE_SPI_DELAY_PRE            0x160
> +#define APPLE_SPI_DELAY_POST           0x168
> +#define APPLE_SPI_DELAY_ENABLE         BIT(0)
> +#define APPLE_SPI_DELAY_NO_INTERBYTE   BIT(1)
> +#define APPLE_SPI_DELAY_SET_SCK                BIT(4)
> +#define APPLE_SPI_DELAY_SET_MOSI       BIT(6)
> +#define APPLE_SPI_DELAY_SCK_VAL                BIT(8)
> +#define APPLE_SPI_DELAY_MOSI_VAL       BIT(12)
> +
> +#define APPLE_SPI_FIFO_DEPTH           16
> +
> +#define APPLE_SPI_TIMEOUT_MS           200
> +
> +struct apple_spi_priv {
> +       void *base;
> +       u32 clkfreq;

of the bus or the input peripheral (need scomment)

> +};
> +
> +static void apple_spi_set_cs(struct apple_spi_priv *priv, int on)
> +{
> +       writel(on ? 0 : APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
> +}
> +
> +static void apple_spi_tx(struct apple_spi_priv *priv, uint *len,
> +                        const void **dout)

needs comment

> +{
> +       const u8 *out = *dout;
> +       u32 data, fifostat;
> +       uint count;
> +
> +       fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
> +       count = APPLE_SPI_FIFO_DEPTH -
> +               FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_TX, fifostat);
> +       while (*len > 0 && count > 0) {
> +               data = out ? *out++ : 0;
> +               writel(data, priv->base + APPLE_SPI_TXDATA);
> +               (*len)--;
> +               count--;
> +       }
> +
> +       *dout = out;
> +}
> +
> +static void apple_spi_rx(struct apple_spi_priv *priv, uint *len,
> +                        void **din)

same

> +{
> +       u8 *in = *din;
> +       u32 data, fifostat;
> +       uint count;
> +
> +       fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
> +       count = FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_RX, fifostat);
> +       while (*len > 0 && count > 0) {
> +               data = readl(priv->base + APPLE_SPI_RXDATA);
> +               if (in)
> +                       *in++ = data;
> +               (*len)--;
> +               count--;
> +       }
> +
> +       *din = in;
> +}
> +
> +static int apple_spi_xfer(struct udevice *dev, unsigned int bitlen,
> +                         const void *dout, void *din, unsigned long flags)
> +{
> +       struct apple_spi_priv *priv = dev_get_priv(dev->parent);
> +       unsigned long start = get_timer(0);
> +       uint txlen, rxlen;
> +       int ret = 0;
> +
> +       if ((bitlen % 8) != 0)

s/!= 0//

> +               return -EINVAL;
> +       txlen = rxlen = bitlen / 8;
> +
> +       if (flags & SPI_XFER_BEGIN)
> +               apple_spi_set_cs(priv, 1);
> +
> +       if (txlen > 0) {

Can it be less than zero?

> +               /* Reset FIFOs */
> +               writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
> +                      priv->base + APPLE_SPI_CTRL);
> +
> +               /* Set the transfer length */
> +               writel(txlen, priv->base + APPLE_SPI_TXCNT);
> +               writel(rxlen, priv->base + APPLE_SPI_RXCNT);
> +
> +               /* Prime transmit FIFO */
> +               apple_spi_tx(priv, &txlen, &dout);
> +
> +               /* Start transfer */
> +               writel(APPLE_SPI_CTRL_RUN, priv->base + APPLE_SPI_CTRL);
> +
> +               while ((txlen > 0 || rxlen > 0)) {

same question

> +                       apple_spi_rx(priv, &rxlen, &din);
> +                       apple_spi_tx(priv, &txlen, &dout);
> +
> +                       if (get_timer(start) > APPLE_SPI_TIMEOUT_MS) {
> +                               ret = -ETIMEDOUT;
> +                               break;
> +                       }
> +               }
> +
> +               /* Stop transfer. */
> +               writel(0, priv->base + APPLE_SPI_CTRL);
> +       }
> +
> +       if (flags & SPI_XFER_END)
> +               apple_spi_set_cs(priv, 0);
> +
> +       return ret;
> +}
> +
> +static int apple_spi_set_speed(struct udevice *dev, uint speed)
> +{
> +       struct apple_spi_priv *priv = dev_get_priv(dev);
> +       u32 div;
> +
> +       div = DIV_ROUND_UP(priv->clkfreq, speed);
> +       if (div < APPLE_SPI_CLKDIV_MIN)
> +               div = APPLE_SPI_CLKDIV_MIN;
> +       if (div > APPLE_SPI_CLKDIV_MAX)
> +               div = APPLE_SPI_CLKDIV_MAX;
> +
> +       writel(div, priv->base + APPLE_SPI_CLKDIV);
> +
> +       return 0;
> +}
> +
> +static int apple_spi_set_mode(struct udevice *bus, uint mode)
> +{
> +       return 0;
> +}
> +
> +struct dm_spi_ops apple_spi_ops = {
> +       .xfer = apple_spi_xfer,
> +       .set_speed = apple_spi_set_speed,
> +       .set_mode = apple_spi_set_mode,
> +};
> +
> +static int apple_spi_probe(struct udevice *dev)
> +{
> +       struct apple_spi_priv *priv = dev_get_priv(dev);
> +       struct clk clkdev;
> +       int ret;
> +
> +       priv->base = dev_read_addr_ptr(dev);
> +       if (!priv->base)
> +               return -EINVAL;
> +
> +       ret = clk_get_by_index(dev, 0, &clkdev);
> +       if (ret)
> +               return ret;
> +       priv->clkfreq = clk_get_rate(&clkdev);
> +
> +       /* Set CS high (inactive) and disable override and auto-CS */
> +       writel(APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
> +       writel(readl(priv->base + APPLE_SPI_SHIFTCFG) & ~APPLE_SPI_SHIFTCFG_OVERRIDE_CS,
> +              priv->base + APPLE_SPI_SHIFTCFG);
> +       writel((readl(priv->base + APPLE_SPI_PINCFG) & ~APPLE_SPI_PINCFG_CS_IDLE_VAL) |
> +              APPLE_SPI_PINCFG_KEEP_CS, priv->base + APPLE_SPI_PINCFG);
> +
> +       /* Reset FIFOs */
> +       writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
> +              priv->base + APPLE_SPI_CTRL);
> +
> +       /* Configure defaults */
> +       writel(FIELD_PREP(APPLE_SPI_CFG_MODE, APPLE_SPI_CFG_MODE_IRQ) |
> +              FIELD_PREP(APPLE_SPI_CFG_WORD_SIZE, APPLE_SPI_CFG_WORD_SIZE_8B) |
> +              FIELD_PREP(APPLE_SPI_CFG_FIFO_THRESH, APPLE_SPI_CFG_FIFO_THRESH_8B),
> +              priv->base + APPLE_SPI_CFG);
> +
> +       return 0;
> +}
> +
> +static const struct udevice_id apple_spi_of_match[] = {
> +       { .compatible = "apple,spi" },
> +       { /* sentinel */ }
> +};
> +
> +U_BOOT_DRIVER(apple_spi) = {
> +       .name = "apple_spi",
> +       .id = UCLASS_SPI,
> +       .of_match = apple_spi_of_match,
> +       .probe = apple_spi_probe,
> +       .priv_auto = sizeof(struct apple_spi_priv),
> +       .ops = &apple_spi_ops,
> +};
> --
> 2.34.1
>

Regards,
Simon

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

* Re: [PATCH 2/2] input: apple: Add support for Apple SPI keyboard
  2022-01-16 17:06 ` [PATCH 2/2] input: apple: Add support for Apple SPI keyboard Mark Kettenis
@ 2022-01-22  1:40   ` Simon Glass
  2022-01-22 16:26     ` Mark Kettenis
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Glass @ 2022-01-22  1:40 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: U-Boot Mailing List, Jagan Teki, marcan, Tom Rini

Hi Mark,

On Sun, 16 Jan 2022 at 10:06, Mark Kettenis <kettenis@openbsd.org> wrote:
>
> This driver adds support for the keyboard on Apple Silicon laptops.
> The controller for this keyboard sits on an SPI bus and uses an
> Apple-specific HID over SPI protocol. The packets sent by this
> controller for key presses and key releases are fairly simple and
> are decoded directly by the code in this driver and converted into
> standard Linux keycodes. The same controller handles the touchpad
> found on these laptops.  Packets for touchpad events are simply
> ignored.
>
> Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
> ---
>  configs/apple_m1_defconfig    |   1 +
>  drivers/input/Kconfig         |   8 ++
>  drivers/input/Makefile        |   1 +
>  drivers/input/apple_spi_kbd.c | 255 ++++++++++++++++++++++++++++++++++
>  include/configs/apple.h       |   2 +-
>  5 files changed, 266 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/input/apple_spi_kbd.c

Reviewed-by: Simon Glass <sjg@chromium.org>
Tested on: Macbook Air M1
Tested-by: Simon Glass <sjg@chromium.org>

Various comments below

Doesn't this keyboard only existing on the laptop?

>
> diff --git a/configs/apple_m1_defconfig b/configs/apple_m1_defconfig
> index 1528217b17..433cc62334 100644
> --- a/configs/apple_m1_defconfig
> +++ b/configs/apple_m1_defconfig
> @@ -15,5 +15,6 @@ CONFIG_NVME_APPLE=y
>  CONFIG_USB_XHCI_HCD=y
>  CONFIG_USB_XHCI_DWC3=y
>  CONFIG_USB_KEYBOARD=y
> +CONFIG_APPLE_SPI_KEYB=y
>  CONFIG_VIDEO_SIMPLE=y
>  # CONFIG_GENERATE_SMBIOS_TABLE is not set
> diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
> index 0b753f37bf..2718b3674a 100644
> --- a/drivers/input/Kconfig
> +++ b/drivers/input/Kconfig
> @@ -47,6 +47,14 @@ config KEYBOARD
>           and is only used by novena. For new boards, use driver model
>           instead.
>
> +config APPLE_SPI_KEYB
> +       bool "Enable Apple SPI keyboard support"
> +       depends on DM_KEYBOARD && DM_SPI
> +       help
> +         This adds a driver for the keyboards found on various
> +         laptops based on Apple SoCs. These keyboards use an
> +         Apple-specific HID-over-SPI protocol.
> +
>  config CROS_EC_KEYB
>         bool "Enable Chrome OS EC keyboard support"
>         depends on INPUT
> diff --git a/drivers/input/Makefile b/drivers/input/Makefile
> index e440c921e4..b1133f772f 100644
> --- a/drivers/input/Makefile
> +++ b/drivers/input/Makefile
> @@ -10,6 +10,7 @@ obj-$(CONFIG_$(SPL_TPL_)DM_KEYBOARD) += input.o keyboard-uclass.o
>
>  ifndef CONFIG_SPL_BUILD
>
> +obj-$(CONFIG_APPLE_SPI_KEYB) += apple_spi_kbd.o
>  obj-$(CONFIG_I8042_KEYB) += i8042.o
>  obj-$(CONFIG_TEGRA_KEYBOARD) += input.o tegra-kbc.o
>  obj-$(CONFIG_TWL4030_INPUT) += twl4030.o
> diff --git a/drivers/input/apple_spi_kbd.c b/drivers/input/apple_spi_kbd.c
> new file mode 100644
> index 0000000000..7e034d6008
> --- /dev/null
> +++ b/drivers/input/apple_spi_kbd.c
> @@ -0,0 +1,255 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <keyboard.h>
> +#include <spi.h>
> +#include <stdio_dev.h>
> +#include <asm-generic/gpio.h>
> +#include <linux/delay.h>
> +#include <linux/input.h>
> +
> +/*
> + * The Apple SPI keyboard controller implements a protocol that
> + * closely resembles HID Keyboard Boot protocol.  The key codes are
> + * mapped according to the HID Keyboard/Keypad Usage Table.
> + */
> +
> +/* Modifier key bits */
> +#define HID_MOD_LEFTCTRL       BIT(0)
> +#define HID_MOD_LEFTSHIFT      BIT(1)
> +#define HID_MOD_LEFTALT                BIT(2)
> +#define HID_MOD_LEFTGUI                BIT(3)
> +#define HID_MOD_RIGHTCTRL      BIT(4)
> +#define HID_MOD_RIGHTSHIFT     BIT(5)
> +#define HID_MOD_RIGHTALT       BIT(6)
> +#define HID_MOD_RIGHTGUI       BIT(7)
> +
> +static const u8 hid_kbd_keymap[] = {
> +       KEY_RESERVED, 0xff, 0xff, 0xff,
> +       KEY_A, KEY_B, KEY_C, KEY_D,
> +       KEY_E, KEY_F, KEY_G, KEY_H,
> +       KEY_I, KEY_J, KEY_K, KEY_L,
> +       KEY_M, KEY_N, KEY_O, KEY_P,
> +       KEY_Q, KEY_R, KEY_S, KEY_T,
> +       KEY_U, KEY_V, KEY_W, KEY_X,
> +       KEY_Y, KEY_Z, KEY_1, KEY_2,
> +       KEY_3, KEY_4, KEY_5, KEY_6,
> +       KEY_7, KEY_8, KEY_9, KEY_0,
> +       KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB,
> +       KEY_SPACE, KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE,
> +       KEY_RIGHTBRACE, KEY_BACKSLASH, 0xff, KEY_SEMICOLON,
> +       KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT,
> +       KEY_SLASH, KEY_CAPSLOCK, KEY_F1, KEY_F2,
> +       KEY_F3, KEY_F4, KEY_F5, KEY_F6,
> +       KEY_F7, KEY_F8, KEY_F9, KEY_F10,
> +       KEY_F11, KEY_F12, KEY_SYSRQ, KEY_SCROLLLOCK,
> +       KEY_PAUSE, KEY_INSERT, KEY_HOME, KEY_PAGEUP,
> +       KEY_DELETE, KEY_END, KEY_PAGEDOWN, KEY_RIGHT,
> +       KEY_LEFT, KEY_DOWN, KEY_UP, KEY_NUMLOCK,
> +       KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS, KEY_KPPLUS,
> +       KEY_KPENTER, KEY_KP1, KEY_KP2, KEY_KP3,
> +       KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7,
> +       KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT,
> +       KEY_BACKSLASH, KEY_COMPOSE, KEY_POWER, KEY_KPEQUAL,
> +};
> +
> +/* Report ID used for keyboard input reports. */
> +#define KBD_REPORTID   0x01
> +
> +struct apple_spi_kbd_report {

Is there another kind of keyboard? Perhaps apple_kbd ?

> +       u8 reportid;
> +       u8 modifiers;
> +       u8 reserved;
> +       u8 keycode[6];
> +       u8 fn;
> +};
> +
> +struct apple_spi_kbd_priv {
> +       struct gpio_desc enable;
> +       struct apple_spi_kbd_report old;
> +       struct apple_spi_kbd_report new;
> +};

comment

> +
> +/* Keyboard device. */
> +#define KBD_DEVICE     0x01
> +
> +struct apple_spi_kbd_packet {
> +       u8 flags;
> +#define PACKET_READ    0x20
> +       u8 device;
> +       u16 offset;
> +       u16 remaining;
> +       u16 len;
> +       u8 data[246];
> +       u16 crc;
> +};
> +
> +struct apple_spi_kbd_msg {

what are these two? Need a comment. One is a message and one is a packet?

> +       u8 type;
> +#define MSG_REPORT     0x10
> +       u8 device;
> +       u8 unknown;
> +       u8 msgid;
> +       u16 rsplen;
> +       u16 cmdlen;
> +       u8 data[0];
> +};
> +
> +static void apple_spi_kbd_service_modifiers(struct input_config *input)
> +{
> +       struct apple_spi_kbd_priv *priv = dev_get_priv(input->dev);
> +       u8 new = priv->new.modifiers;
> +       u8 old = priv->old.modifiers;
> +
> +       if ((new ^ old) & HID_MOD_LEFTCTRL)
> +               input_add_keycode(input, KEY_LEFTCTRL,
> +                                 old & HID_MOD_LEFTCTRL);
> +       if ((new ^ old) & HID_MOD_RIGHTCTRL)
> +               input_add_keycode(input, KEY_RIGHTCTRL,
> +                                 old & HID_MOD_RIGHTCTRL);
> +       if ((new ^ old) & HID_MOD_LEFTSHIFT)
> +               input_add_keycode(input, KEY_LEFTSHIFT,
> +                                 old & HID_MOD_LEFTSHIFT);
> +       if ((new ^ old) & HID_MOD_RIGHTSHIFT)
> +               input_add_keycode(input, KEY_RIGHTSHIFT,
> +                                 old & HID_MOD_RIGHTSHIFT);
> +       if ((new ^ old) & HID_MOD_LEFTALT)
> +               input_add_keycode(input, KEY_LEFTALT,
> +                                 old & HID_MOD_LEFTALT);
> +       if ((new ^ old) & HID_MOD_RIGHTALT)
> +               input_add_keycode(input, KEY_RIGHTALT,
> +                                 old & HID_MOD_RIGHTALT);
> +       if ((new ^ old) & HID_MOD_LEFTGUI)
> +               input_add_keycode(input, KEY_LEFTMETA,
> +                                 old & HID_MOD_LEFTGUI);
> +       if ((new ^ old) & HID_MOD_RIGHTGUI)
> +               input_add_keycode(input, KEY_RIGHTMETA,
> +                                 old & HID_MOD_RIGHTGUI);
> +}
> +
> +static void apple_spi_kbd_service_key(struct input_config *input, int i,
> +                                     int released)
> +{
> +       struct apple_spi_kbd_priv *priv = dev_get_priv(input->dev);
> +       u8 *new;
> +       u8 *old;
> +
> +       if (released) {
> +               new = priv->new.keycode;
> +               old = priv->old.keycode;
> +       } else {
> +               new = priv->old.keycode;
> +               old = priv->new.keycode;
> +       }
> +
> +       if (memscan(new, old[i], sizeof(priv->new.keycode)) ==
> +           new + sizeof(priv->new.keycode) &&
> +           old[i] < ARRAY_SIZE(hid_kbd_keymap))
> +               input_add_keycode(input, hid_kbd_keymap[old[i]], released);
> +}
> +
> +static int apple_spi_kbd_check(struct input_config *input)
> +{
> +       struct udevice *dev = input->dev;
> +       struct apple_spi_kbd_priv *priv = dev_get_priv(dev);
> +       struct apple_spi_kbd_packet packet;
> +       struct apple_spi_kbd_msg *msg;
> +       struct apple_spi_kbd_report *report;
> +       int i, ret;
> +
> +       memset(&packet, 0, sizeof(packet));
> +
> +       ret = dm_spi_claim_bus(dev);
> +       if (ret < 0)
> +               return ret;
> +
> +       /*
> +        * The keyboard controller needs delays after asserting CS#
> +        * and before deasserting CS#.
> +        */
> +       dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_BEGIN);

Check error returns in these

> +       udelay(100);
> +       dm_spi_xfer(dev, sizeof(packet) * 8, NULL, &packet, 0);
> +       udelay(100);
> +       dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END);
> +
> +       dm_spi_release_bus(dev);
> +
> +       /*
> +        * The keyboard controller needs a delay between subsequent
> +        * SPI transfers.
> +        */
> +       udelay(250);

Is this the same delay for all models?

> +
> +       msg = (struct apple_spi_kbd_msg *)packet.data;
> +       report = (struct apple_spi_kbd_report *)msg->data;
> +       if (packet.flags == PACKET_READ && packet.device == KBD_DEVICE &&
> +           msg->type == MSG_REPORT && msg->device == KBD_DEVICE &&
> +           msg->cmdlen == sizeof(struct apple_spi_kbd_report) &&
> +           report->reportid == KBD_REPORTID) {
> +               memcpy(&priv->new, report,
> +                      sizeof(struct apple_spi_kbd_report));
> +               apple_spi_kbd_service_modifiers(input);
> +               for (i = 0; i < sizeof(priv->new.keycode); i++) {
> +                       apple_spi_kbd_service_key(input, i, 1);
> +                       apple_spi_kbd_service_key(input, i, 0);
> +               }
> +               memcpy(&priv->old, &priv->new,
> +                      sizeof(struct apple_spi_kbd_report));
> +               return 1;
> +       }
> +
> +       return 0;
> +}
> +
> +static int apple_spi_kbd_probe(struct udevice *dev)
> +{
> +       struct apple_spi_kbd_priv *priv = dev_get_priv(dev);
> +       struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
> +       struct stdio_dev *sdev = &uc_priv->sdev;
> +       struct input_config *input = &uc_priv->input;
> +       int ret;
> +
> +       ret = gpio_request_by_name(dev, "spien-gpios", 0, &priv->enable,
> +                                  GPIOD_IS_OUT);
> +       if (ret < 0)
> +               return ret;
> +
> +       /* Reset the keyboard controller. */
> +       dm_gpio_set_value(&priv->enable, 1);
> +       udelay(5000);
> +       dm_gpio_set_value(&priv->enable, 0);
> +       udelay(5000);
> +
> +       /* Enable the keyboard controller. */
> +       dm_gpio_set_value(&priv->enable, 1);
> +       udelay(50000);

That is a long time. Could we timestamp this instead and then check
when the keyboard is read that enough time has elapsed?

> +
> +       input->dev = dev;
> +       input->read_keys = apple_spi_kbd_check;
> +       input_add_tables(input, false);
> +       strcpy(sdev->name, "spikbd");
> +
> +       return input_stdio_register(sdev);
> +}
> +
> +static const struct keyboard_ops apple_spi_kbd_ops = {
> +};
> +
> +static const struct udevice_id apple_spi_kbd_of_match[] = {
> +       { .compatible = "apple,spi-hid-transport" },
> +       { /* sentinel */ }
> +};
> +
> +U_BOOT_DRIVER(apple_spi_kbd) = {
> +       .name = "apple_spi_kbd",
> +       .id = UCLASS_KEYBOARD,
> +       .of_match = apple_spi_kbd_of_match,
> +       .probe = apple_spi_kbd_probe,
> +       .priv_auto = sizeof(struct apple_spi_kbd_priv),
> +       .ops = &apple_spi_kbd_ops,
> +};
> diff --git a/include/configs/apple.h b/include/configs/apple.h
> index 47faad8150..f12e9bdef5 100644
> --- a/include/configs/apple.h
> +++ b/include/configs/apple.h
> @@ -5,7 +5,7 @@
>
>  /* Environment */
>  #define ENV_DEVICE_SETTINGS \
> -       "stdin=serial,usbkbd\0" \
> +       "stdin=serial,usbkbd,spikbd\0" \
>         "stdout=serial,vidconsole\0" \
>         "stderr=serial,vidconsole\0"
>
> --
> 2.34.1
>

Regards,
Simon

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

* Re: [PATCH 1/2] spi: apple: Add driver for Apple SPI controller
  2022-01-22  1:40   ` Simon Glass
@ 2022-01-22 15:53     ` Mark Kettenis
  0 siblings, 0 replies; 7+ messages in thread
From: Mark Kettenis @ 2022-01-22 15:53 UTC (permalink / raw)
  To: Simon Glass; +Cc: kettenis, u-boot, jagan, marcan, trini

> From: Simon Glass <sjg@chromium.org>
> Date: Fri, 21 Jan 2022 18:40:25 -0700

Hi Simon,

> Hi Mark,
> 
> On Sun, 16 Jan 2022 at 10:06, Mark Kettenis <kettenis@openbsd.org> wrote:
> >
> > Add a driver for the SPI controller integrated on Apple SoCs.
> > This is necessary to support the keyboard on Apple Silicon laopts
> > since their keyboard uses an Apple-specific HID over SPI protocol.
> >
> > Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
> > ---
> >  arch/arm/Kconfig        |   2 +
> >  drivers/spi/Kconfig     |   7 +
> >  drivers/spi/Makefile    |   1 +
> >  drivers/spi/apple_spi.c | 283 ++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 293 insertions(+)
> >  create mode 100644 drivers/spi/apple_spi.c
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> Tested on: Macbook Air M1
> Tested-by: Simon Glass <sjg@chromium.org>
> 
> with nits below
> 
> Shouldn't you enable SPI flash and 'sspi' and 'sf' commands as well?

I don't feel comfortable doing that at this point.  Nothing in U-Boot
needs to access the flash at the moment and those commands could
overwrite the system firmware and brick the machines (although we
suspect that the flash contents can be restored using a DFU restore).

> >
> > diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
> > index 04b4a20211..fed993a53d 100644
> > --- a/arch/arm/Kconfig
> > +++ b/arch/arm/Kconfig
> > @@ -937,6 +937,7 @@ config ARCH_APPLE
> >         select DM_MAILBOX
> >         select DM_RESET
> >         select DM_SERIAL
> > +       select DM_SPI
> >         select DM_USB
> >         select DM_VIDEO
> >         select IOMMU
> > @@ -946,6 +947,7 @@ config ARCH_APPLE
> >         select POSITION_INDEPENDENT
> >         select POWER_DOMAIN
> >         select REGMAP
> > +       select SPI
> >         select SYSCON
> >         select SYSRESET
> >         select SYSRESET_WATCHDOG
> > diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
> > index d07e9a28af..0a6a85f9c4 100644
> > --- a/drivers/spi/Kconfig
> > +++ b/drivers/spi/Kconfig
> > @@ -50,6 +50,13 @@ config ALTERA_SPI
> >           IP core. Please find details on the "Embedded Peripherals IP
> >           User Guide" of Altera.
> >
> > +config APPLE_SPI
> > +       bool "Apple SPI driver"
> > +       default y if ARCH_APPLE
> > +       help
> > +         Enable the Apple SPI driver. This driver can be used to
> > +         access the SPI flash and keyboard on machines based on Apple SoCs.
> > +
> >  config ATCSPI200_SPI
> >         bool "Andestech ATCSPI200 SPI driver"
> >         help
> > diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
> > index d2f24bccef..bea746f3e3 100644
> > --- a/drivers/spi/Makefile
> > +++ b/drivers/spi/Makefile
> > @@ -18,6 +18,7 @@ obj-$(CONFIG_SPI_MEM) += spi-mem-nodm.o
> >  endif
> >
> >  obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
> > +obj-$(CONFIG_APPLE_SPI) += apple_spi.o
> >  obj-$(CONFIG_ATH79_SPI) += ath79_spi.o
> >  obj-$(CONFIG_ATMEL_QSPI) += atmel-quadspi.o
> >  obj-$(CONFIG_ATMEL_SPI) += atmel_spi.o
> > diff --git a/drivers/spi/apple_spi.c b/drivers/spi/apple_spi.c
> > new file mode 100644
> > index 0000000000..3ae89dc638
> > --- /dev/null
> > +++ b/drivers/spi/apple_spi.c
> > @@ -0,0 +1,283 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
> > + * Copyright The Asahi Linux Contributors
> > + */
> > +
> > +#include <common.h>
> > +#include <dm.h>
> > +#include <clk.h>
> > +#include <spi.h>
> > +#include <asm/io.h>
> > +#include <linux/bitfield.h>
> > +#include <linux/delay.h>
> > +
> > +#define APPLE_SPI_CTRL                 0x000
> > +#define APPLE_SPI_CTRL_RUN             BIT(0)
> > +#define APPLE_SPI_CTRL_TX_RESET                BIT(2)
> > +#define APPLE_SPI_CTRL_RX_RESET                BIT(3)
> > +
> > +#define APPLE_SPI_CFG                  0x004
> > +#define APPLE_SPI_CFG_CPHA             BIT(1)
> > +#define APPLE_SPI_CFG_CPOL             BIT(2)
> > +#define APPLE_SPI_CFG_MODE             GENMASK(6, 5)
> > +#define APPLE_SPI_CFG_MODE_POLLED      0
> > +#define APPLE_SPI_CFG_MODE_IRQ         1
> > +#define APPLE_SPI_CFG_MODE_DMA         2
> > +#define APPLE_SPI_CFG_IE_RXCOMPLETE    BIT(7)
> > +#define APPLE_SPI_CFG_IE_TXRXTHRESH    BIT(8)
> > +#define APPLE_SPI_CFG_LSB_FIRST                BIT(13)
> > +#define APPLE_SPI_CFG_WORD_SIZE                GENMASK(16, 15)
> > +#define APPLE_SPI_CFG_WORD_SIZE_8B     0
> > +#define APPLE_SPI_CFG_WORD_SIZE_16B    1
> > +#define APPLE_SPI_CFG_WORD_SIZE_32B    2
> > +#define APPLE_SPI_CFG_FIFO_THRESH      GENMASK(18, 17)
> > +#define APPLE_SPI_CFG_FIFO_THRESH_8B   0
> > +#define APPLE_SPI_CFG_FIFO_THRESH_4B   1
> > +#define APPLE_SPI_CFG_FIFO_THRESH_1B   2
> > +#define APPLE_SPI_CFG_IE_TXCOMPLETE    BIT(21)
> > +
> > +#define APPLE_SPI_STATUS               0x008
> > +#define APPLE_SPI_STATUS_RXCOMPLETE    BIT(0)
> > +#define APPLE_SPI_STATUS_TXRXTHRESH    BIT(1)
> > +#define APPLE_SPI_STATUS_TXCOMPLETE    BIT(2)
> > +
> > +#define APPLE_SPI_PIN                  0x00c
> > +#define APPLE_SPI_PIN_KEEP_MOSI                BIT(0)
> > +#define APPLE_SPI_PIN_CS               BIT(1)
> > +
> > +#define APPLE_SPI_TXDATA               0x010
> > +#define APPLE_SPI_RXDATA               0x020
> > +#define APPLE_SPI_CLKDIV               0x030
> > +#define APPLE_SPI_CLKDIV_MIN           0x002
> > +#define APPLE_SPI_CLKDIV_MAX           0x7ff
> > +#define APPLE_SPI_RXCNT                        0x034
> > +#define APPLE_SPI_WORD_DELAY           0x038
> > +#define APPLE_SPI_TXCNT                        0x04c
> > +
> > +#define APPLE_SPI_FIFOSTAT             0x10c
> > +#define APPLE_SPI_FIFOSTAT_TXFULL      BIT(4)
> > +#define APPLE_SPI_FIFOSTAT_LEVEL_TX    GENMASK(15, 8)
> > +#define APPLE_SPI_FIFOSTAT_RXEMPTY     BIT(20)
> > +#define APPLE_SPI_FIFOSTAT_LEVEL_RX    GENMASK(31, 24)
> > +
> > +#define APPLE_SPI_IE_XFER              0x130
> > +#define APPLE_SPI_IF_XFER              0x134
> > +#define APPLE_SPI_XFER_RXCOMPLETE      BIT(0)
> > +#define APPLE_SPI_XFER_TXCOMPLETE      BIT(1)
> > +
> > +#define APPLE_SPI_IE_FIFO              0x138
> > +#define APPLE_SPI_IF_FIFO              0x13c
> > +#define APPLE_SPI_FIFO_RXTHRESH                BIT(4)
> > +#define APPLE_SPI_FIFO_TXTHRESH                BIT(5)
> > +#define APPLE_SPI_FIFO_RXFULL          BIT(8)
> > +#define APPLE_SPI_FIFO_TXEMPTY         BIT(9)
> > +#define APPLE_SPI_FIFO_RXUNDERRUN      BIT(16)
> > +#define APPLE_SPI_FIFO_TXOVERFLOW      BIT(17)
> > +
> > +#define APPLE_SPI_SHIFTCFG             0x150
> > +#define APPLE_SPI_SHIFTCFG_CLK_ENABLE  BIT(0)
> > +#define APPLE_SPI_SHIFTCFG_CS_ENABLE   BIT(1)
> > +#define APPLE_SPI_SHIFTCFG_AND_CLK_DATA        BIT(8)
> > +#define APPLE_SPI_SHIFTCFG_CS_AS_DATA  BIT(9)
> > +#define APPLE_SPI_SHIFTCFG_TX_ENABLE   BIT(10)
> > +#define APPLE_SPI_SHIFTCFG_RX_ENABLE   BIT(11)
> > +#define APPLE_SPI_SHIFTCFG_BITS                GENMASK(21, 16)
> > +#define APPLE_SPI_SHIFTCFG_OVERRIDE_CS BIT(24)
> > +
> > +#define APPLE_SPI_PINCFG               0x154
> > +#define APPLE_SPI_PINCFG_KEEP_CLK      BIT(0)
> > +#define APPLE_SPI_PINCFG_KEEP_CS       BIT(1)
> > +#define APPLE_SPI_PINCFG_KEEP_MOSI     BIT(2)
> > +#define APPLE_SPI_PINCFG_CLK_IDLE_VAL  BIT(8)
> > +#define APPLE_SPI_PINCFG_CS_IDLE_VAL   BIT(9)
> > +#define APPLE_SPI_PINCFG_MOSI_IDLE_VAL BIT(10)
> > +
> > +#define APPLE_SPI_DELAY_PRE            0x160
> > +#define APPLE_SPI_DELAY_POST           0x168
> > +#define APPLE_SPI_DELAY_ENABLE         BIT(0)
> > +#define APPLE_SPI_DELAY_NO_INTERBYTE   BIT(1)
> > +#define APPLE_SPI_DELAY_SET_SCK                BIT(4)
> > +#define APPLE_SPI_DELAY_SET_MOSI       BIT(6)
> > +#define APPLE_SPI_DELAY_SCK_VAL                BIT(8)
> > +#define APPLE_SPI_DELAY_MOSI_VAL       BIT(12)
> > +
> > +#define APPLE_SPI_FIFO_DEPTH           16
> > +
> > +#define APPLE_SPI_TIMEOUT_MS           200
> > +
> > +struct apple_spi_priv {
> > +       void *base;
> > +       u32 clkfreq;
> 
> of the bus or the input peripheral (need scomment)

Sure; it's the input clock frequency.

> 
> > +};
> > +
> > +static void apple_spi_set_cs(struct apple_spi_priv *priv, int on)
> > +{
> > +       writel(on ? 0 : APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
> > +}
> > +
> > +static void apple_spi_tx(struct apple_spi_priv *priv, uint *len,
> > +                        const void **dout)
> 
> needs comment
> 
> > +{
> > +       const u8 *out = *dout;
> > +       u32 data, fifostat;
> > +       uint count;
> > +
> > +       fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
> > +       count = APPLE_SPI_FIFO_DEPTH -
> > +               FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_TX, fifostat);
> > +       while (*len > 0 && count > 0) {
> > +               data = out ? *out++ : 0;
> > +               writel(data, priv->base + APPLE_SPI_TXDATA);
> > +               (*len)--;
> > +               count--;
> > +       }
> > +
> > +       *dout = out;
> > +}
> > +
> > +static void apple_spi_rx(struct apple_spi_priv *priv, uint *len,
> > +                        void **din)
> 
> same
> 
> > +{
> > +       u8 *in = *din;
> > +       u32 data, fifostat;
> > +       uint count;
> > +
> > +       fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
> > +       count = FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_RX, fifostat);
> > +       while (*len > 0 && count > 0) {
> > +               data = readl(priv->base + APPLE_SPI_RXDATA);
> > +               if (in)
> > +                       *in++ = data;
> > +               (*len)--;
> > +               count--;
> > +       }
> > +
> > +       *din = in;
> > +}
> > +
> > +static int apple_spi_xfer(struct udevice *dev, unsigned int bitlen,
> > +                         const void *dout, void *din, unsigned long flags)
> > +{
> > +       struct apple_spi_priv *priv = dev_get_priv(dev->parent);
> > +       unsigned long start = get_timer(0);
> > +       uint txlen, rxlen;
> > +       int ret = 0;
> > +
> > +       if ((bitlen % 8) != 0)
> 
> s/!= 0//

Hmm, really.  It is a length so an explicit comparison to zero makes
more sense to me.

> 
> > +               return -EINVAL;
> > +       txlen = rxlen = bitlen / 8;
> > +
> > +       if (flags & SPI_XFER_BEGIN)
> > +               apple_spi_set_cs(priv, 1);
> > +
> > +       if (txlen > 0) {
> 
> Can it be less than zero?

It is an unsigned variable, so no!

> > +               /* Reset FIFOs */
> > +               writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
> > +                      priv->base + APPLE_SPI_CTRL);
> > +
> > +               /* Set the transfer length */
> > +               writel(txlen, priv->base + APPLE_SPI_TXCNT);
> > +               writel(rxlen, priv->base + APPLE_SPI_RXCNT);
> > +
> > +               /* Prime transmit FIFO */
> > +               apple_spi_tx(priv, &txlen, &dout);
> > +
> > +               /* Start transfer */
> > +               writel(APPLE_SPI_CTRL_RUN, priv->base + APPLE_SPI_CTRL);
> > +
> > +               while ((txlen > 0 || rxlen > 0)) {
> 
> same question

same answer ;)

> > +                       apple_spi_rx(priv, &rxlen, &din);
> > +                       apple_spi_tx(priv, &txlen, &dout);
> > +
> > +                       if (get_timer(start) > APPLE_SPI_TIMEOUT_MS) {
> > +                               ret = -ETIMEDOUT;
> > +                               break;
> > +                       }
> > +               }
> > +
> > +               /* Stop transfer. */
> > +               writel(0, priv->base + APPLE_SPI_CTRL);
> > +       }
> > +
> > +       if (flags & SPI_XFER_END)
> > +               apple_spi_set_cs(priv, 0);
> > +
> > +       return ret;
> > +}
> > +
> > +static int apple_spi_set_speed(struct udevice *dev, uint speed)
> > +{
> > +       struct apple_spi_priv *priv = dev_get_priv(dev);
> > +       u32 div;
> > +
> > +       div = DIV_ROUND_UP(priv->clkfreq, speed);
> > +       if (div < APPLE_SPI_CLKDIV_MIN)
> > +               div = APPLE_SPI_CLKDIV_MIN;
> > +       if (div > APPLE_SPI_CLKDIV_MAX)
> > +               div = APPLE_SPI_CLKDIV_MAX;
> > +
> > +       writel(div, priv->base + APPLE_SPI_CLKDIV);
> > +
> > +       return 0;
> > +}
> > +
> > +static int apple_spi_set_mode(struct udevice *bus, uint mode)
> > +{
> > +       return 0;
> > +}
> > +
> > +struct dm_spi_ops apple_spi_ops = {
> > +       .xfer = apple_spi_xfer,
> > +       .set_speed = apple_spi_set_speed,
> > +       .set_mode = apple_spi_set_mode,
> > +};
> > +
> > +static int apple_spi_probe(struct udevice *dev)
> > +{
> > +       struct apple_spi_priv *priv = dev_get_priv(dev);
> > +       struct clk clkdev;
> > +       int ret;
> > +
> > +       priv->base = dev_read_addr_ptr(dev);
> > +       if (!priv->base)
> > +               return -EINVAL;
> > +
> > +       ret = clk_get_by_index(dev, 0, &clkdev);
> > +       if (ret)
> > +               return ret;
> > +       priv->clkfreq = clk_get_rate(&clkdev);
> > +
> > +       /* Set CS high (inactive) and disable override and auto-CS */
> > +       writel(APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
> > +       writel(readl(priv->base + APPLE_SPI_SHIFTCFG) & ~APPLE_SPI_SHIFTCFG_OVERRIDE_CS,
> > +              priv->base + APPLE_SPI_SHIFTCFG);
> > +       writel((readl(priv->base + APPLE_SPI_PINCFG) & ~APPLE_SPI_PINCFG_CS_IDLE_VAL) |
> > +              APPLE_SPI_PINCFG_KEEP_CS, priv->base + APPLE_SPI_PINCFG);
> > +
> > +       /* Reset FIFOs */
> > +       writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
> > +              priv->base + APPLE_SPI_CTRL);
> > +
> > +       /* Configure defaults */
> > +       writel(FIELD_PREP(APPLE_SPI_CFG_MODE, APPLE_SPI_CFG_MODE_IRQ) |
> > +              FIELD_PREP(APPLE_SPI_CFG_WORD_SIZE, APPLE_SPI_CFG_WORD_SIZE_8B) |
> > +              FIELD_PREP(APPLE_SPI_CFG_FIFO_THRESH, APPLE_SPI_CFG_FIFO_THRESH_8B),
> > +              priv->base + APPLE_SPI_CFG);
> > +
> > +       return 0;
> > +}
> > +
> > +static const struct udevice_id apple_spi_of_match[] = {
> > +       { .compatible = "apple,spi" },
> > +       { /* sentinel */ }
> > +};
> > +
> > +U_BOOT_DRIVER(apple_spi) = {
> > +       .name = "apple_spi",
> > +       .id = UCLASS_SPI,
> > +       .of_match = apple_spi_of_match,
> > +       .probe = apple_spi_probe,
> > +       .priv_auto = sizeof(struct apple_spi_priv),
> > +       .ops = &apple_spi_ops,
> > +};
> > --
> > 2.34.1
> >
> 
> Regards,
> Simon
> 

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

* Re: [PATCH 2/2] input: apple: Add support for Apple SPI keyboard
  2022-01-22  1:40   ` Simon Glass
@ 2022-01-22 16:26     ` Mark Kettenis
  0 siblings, 0 replies; 7+ messages in thread
From: Mark Kettenis @ 2022-01-22 16:26 UTC (permalink / raw)
  To: Simon Glass; +Cc: kettenis, u-boot, jagan, marcan, trini

> From: Simon Glass <sjg@chromium.org>
> Date: Fri, 21 Jan 2022 18:40:27 -0700

Hi Simon,

> Hi Mark,
> 
> On Sun, 16 Jan 2022 at 10:06, Mark Kettenis <kettenis@openbsd.org> wrote:
> >
> > This driver adds support for the keyboard on Apple Silicon laptops.
> > The controller for this keyboard sits on an SPI bus and uses an
> > Apple-specific HID over SPI protocol. The packets sent by this
> > controller for key presses and key releases are fairly simple and
> > are decoded directly by the code in this driver and converted into
> > standard Linux keycodes. The same controller handles the touchpad
> > found on these laptops.  Packets for touchpad events are simply
> > ignored.
> >
> > Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
> > ---
> >  configs/apple_m1_defconfig    |   1 +
> >  drivers/input/Kconfig         |   8 ++
> >  drivers/input/Makefile        |   1 +
> >  drivers/input/apple_spi_kbd.c | 255 ++++++++++++++++++++++++++++++++++
> >  include/configs/apple.h       |   2 +-
> >  5 files changed, 266 insertions(+), 1 deletion(-)
> >  create mode 100644 drivers/input/apple_spi_kbd.c
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> Tested on: Macbook Air M1
> Tested-by: Simon Glass <sjg@chromium.org>
> 
> Various comments below
> 
> Doesn't this keyboard only existing on the laptop?

It does (and the Kconfig entry says so).  The driver is enabled in
apple_m1_defconfig since these machines have tons of memory and it
doesn't increase the build time in a meaningful way.  So we can use a
single defconfig for all M1 machines.

> > diff --git a/configs/apple_m1_defconfig b/configs/apple_m1_defconfig
> > index 1528217b17..433cc62334 100644
> > --- a/configs/apple_m1_defconfig
> > +++ b/configs/apple_m1_defconfig
> > @@ -15,5 +15,6 @@ CONFIG_NVME_APPLE=y
> >  CONFIG_USB_XHCI_HCD=y
> >  CONFIG_USB_XHCI_DWC3=y
> >  CONFIG_USB_KEYBOARD=y
> > +CONFIG_APPLE_SPI_KEYB=y
> >  CONFIG_VIDEO_SIMPLE=y
> >  # CONFIG_GENERATE_SMBIOS_TABLE is not set
> > diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
> > index 0b753f37bf..2718b3674a 100644
> > --- a/drivers/input/Kconfig
> > +++ b/drivers/input/Kconfig
> > @@ -47,6 +47,14 @@ config KEYBOARD
> >           and is only used by novena. For new boards, use driver model
> >           instead.
> >
> > +config APPLE_SPI_KEYB
> > +       bool "Enable Apple SPI keyboard support"
> > +       depends on DM_KEYBOARD && DM_SPI
> > +       help
> > +         This adds a driver for the keyboards found on various
> > +         laptops based on Apple SoCs. These keyboards use an
> > +         Apple-specific HID-over-SPI protocol.
> > +
> >  config CROS_EC_KEYB
> >         bool "Enable Chrome OS EC keyboard support"
> >         depends on INPUT
> > diff --git a/drivers/input/Makefile b/drivers/input/Makefile
> > index e440c921e4..b1133f772f 100644
> > --- a/drivers/input/Makefile
> > +++ b/drivers/input/Makefile
> > @@ -10,6 +10,7 @@ obj-$(CONFIG_$(SPL_TPL_)DM_KEYBOARD) += input.o keyboard-uclass.o
> >
> >  ifndef CONFIG_SPL_BUILD
> >
> > +obj-$(CONFIG_APPLE_SPI_KEYB) += apple_spi_kbd.o
> >  obj-$(CONFIG_I8042_KEYB) += i8042.o
> >  obj-$(CONFIG_TEGRA_KEYBOARD) += input.o tegra-kbc.o
> >  obj-$(CONFIG_TWL4030_INPUT) += twl4030.o
> > diff --git a/drivers/input/apple_spi_kbd.c b/drivers/input/apple_spi_kbd.c
> > new file mode 100644
> > index 0000000000..7e034d6008
> > --- /dev/null
> > +++ b/drivers/input/apple_spi_kbd.c
> > @@ -0,0 +1,255 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
> > + */
> > +
> > +#include <common.h>
> > +#include <dm.h>
> > +#include <keyboard.h>
> > +#include <spi.h>
> > +#include <stdio_dev.h>
> > +#include <asm-generic/gpio.h>
> > +#include <linux/delay.h>
> > +#include <linux/input.h>
> > +
> > +/*
> > + * The Apple SPI keyboard controller implements a protocol that
> > + * closely resembles HID Keyboard Boot protocol.  The key codes are
> > + * mapped according to the HID Keyboard/Keypad Usage Table.
> > + */
> > +
> > +/* Modifier key bits */
> > +#define HID_MOD_LEFTCTRL       BIT(0)
> > +#define HID_MOD_LEFTSHIFT      BIT(1)
> > +#define HID_MOD_LEFTALT                BIT(2)
> > +#define HID_MOD_LEFTGUI                BIT(3)
> > +#define HID_MOD_RIGHTCTRL      BIT(4)
> > +#define HID_MOD_RIGHTSHIFT     BIT(5)
> > +#define HID_MOD_RIGHTALT       BIT(6)
> > +#define HID_MOD_RIGHTGUI       BIT(7)
> > +
> > +static const u8 hid_kbd_keymap[] = {
> > +       KEY_RESERVED, 0xff, 0xff, 0xff,
> > +       KEY_A, KEY_B, KEY_C, KEY_D,
> > +       KEY_E, KEY_F, KEY_G, KEY_H,
> > +       KEY_I, KEY_J, KEY_K, KEY_L,
> > +       KEY_M, KEY_N, KEY_O, KEY_P,
> > +       KEY_Q, KEY_R, KEY_S, KEY_T,
> > +       KEY_U, KEY_V, KEY_W, KEY_X,
> > +       KEY_Y, KEY_Z, KEY_1, KEY_2,
> > +       KEY_3, KEY_4, KEY_5, KEY_6,
> > +       KEY_7, KEY_8, KEY_9, KEY_0,
> > +       KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB,
> > +       KEY_SPACE, KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE,
> > +       KEY_RIGHTBRACE, KEY_BACKSLASH, 0xff, KEY_SEMICOLON,
> > +       KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT,
> > +       KEY_SLASH, KEY_CAPSLOCK, KEY_F1, KEY_F2,
> > +       KEY_F3, KEY_F4, KEY_F5, KEY_F6,
> > +       KEY_F7, KEY_F8, KEY_F9, KEY_F10,
> > +       KEY_F11, KEY_F12, KEY_SYSRQ, KEY_SCROLLLOCK,
> > +       KEY_PAUSE, KEY_INSERT, KEY_HOME, KEY_PAGEUP,
> > +       KEY_DELETE, KEY_END, KEY_PAGEDOWN, KEY_RIGHT,
> > +       KEY_LEFT, KEY_DOWN, KEY_UP, KEY_NUMLOCK,
> > +       KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS, KEY_KPPLUS,
> > +       KEY_KPENTER, KEY_KP1, KEY_KP2, KEY_KP3,
> > +       KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7,
> > +       KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT,
> > +       KEY_BACKSLASH, KEY_COMPOSE, KEY_POWER, KEY_KPEQUAL,
> > +};
> > +
> > +/* Report ID used for keyboard input reports. */
> > +#define KBD_REPORTID   0x01
> > +
> > +struct apple_spi_kbd_report {
> 
> Is there another kind of keyboard? Perhaps apple_kbd ?

There are Apple USB keyboards that may need quirks.
 
> > +       u8 reportid;
> > +       u8 modifiers;
> > +       u8 reserved;
> > +       u8 keycode[6];
> > +       u8 fn;
> > +};
> > +
> > +struct apple_spi_kbd_priv {
> > +       struct gpio_desc enable;
> > +       struct apple_spi_kbd_report old;
> > +       struct apple_spi_kbd_report new;
> > +};
> 
> comment
> 
> > +
> > +/* Keyboard device. */
> > +#define KBD_DEVICE     0x01
> > +
> > +struct apple_spi_kbd_packet {
> > +       u8 flags;
> > +#define PACKET_READ    0x20
> > +       u8 device;
> > +       u16 offset;
> > +       u16 remaining;
> > +       u16 len;
> > +       u8 data[246];
> > +       u16 crc;
> > +};
> > +
> > +struct apple_spi_kbd_msg {
> 
> what are these two? Need a comment. One is a message and one is a packet?

Yes; the message is embedded in a packet.  Comments added.

> 
> > +       u8 type;
> > +#define MSG_REPORT     0x10
> > +       u8 device;
> > +       u8 unknown;
> > +       u8 msgid;
> > +       u16 rsplen;
> > +       u16 cmdlen;
> > +       u8 data[0];
> > +};
> > +
> > +static void apple_spi_kbd_service_modifiers(struct input_config *input)
> > +{
> > +       struct apple_spi_kbd_priv *priv = dev_get_priv(input->dev);
> > +       u8 new = priv->new.modifiers;
> > +       u8 old = priv->old.modifiers;
> > +
> > +       if ((new ^ old) & HID_MOD_LEFTCTRL)
> > +               input_add_keycode(input, KEY_LEFTCTRL,
> > +                                 old & HID_MOD_LEFTCTRL);
> > +       if ((new ^ old) & HID_MOD_RIGHTCTRL)
> > +               input_add_keycode(input, KEY_RIGHTCTRL,
> > +                                 old & HID_MOD_RIGHTCTRL);
> > +       if ((new ^ old) & HID_MOD_LEFTSHIFT)
> > +               input_add_keycode(input, KEY_LEFTSHIFT,
> > +                                 old & HID_MOD_LEFTSHIFT);
> > +       if ((new ^ old) & HID_MOD_RIGHTSHIFT)
> > +               input_add_keycode(input, KEY_RIGHTSHIFT,
> > +                                 old & HID_MOD_RIGHTSHIFT);
> > +       if ((new ^ old) & HID_MOD_LEFTALT)
> > +               input_add_keycode(input, KEY_LEFTALT,
> > +                                 old & HID_MOD_LEFTALT);
> > +       if ((new ^ old) & HID_MOD_RIGHTALT)
> > +               input_add_keycode(input, KEY_RIGHTALT,
> > +                                 old & HID_MOD_RIGHTALT);
> > +       if ((new ^ old) & HID_MOD_LEFTGUI)
> > +               input_add_keycode(input, KEY_LEFTMETA,
> > +                                 old & HID_MOD_LEFTGUI);
> > +       if ((new ^ old) & HID_MOD_RIGHTGUI)
> > +               input_add_keycode(input, KEY_RIGHTMETA,
> > +                                 old & HID_MOD_RIGHTGUI);
> > +}
> > +
> > +static void apple_spi_kbd_service_key(struct input_config *input, int i,
> > +                                     int released)
> > +{
> > +       struct apple_spi_kbd_priv *priv = dev_get_priv(input->dev);
> > +       u8 *new;
> > +       u8 *old;
> > +
> > +       if (released) {
> > +               new = priv->new.keycode;
> > +               old = priv->old.keycode;
> > +       } else {
> > +               new = priv->old.keycode;
> > +               old = priv->new.keycode;
> > +       }
> > +
> > +       if (memscan(new, old[i], sizeof(priv->new.keycode)) ==
> > +           new + sizeof(priv->new.keycode) &&
> > +           old[i] < ARRAY_SIZE(hid_kbd_keymap))
> > +               input_add_keycode(input, hid_kbd_keymap[old[i]], released);
> > +}
> > +
> > +static int apple_spi_kbd_check(struct input_config *input)
> > +{
> > +       struct udevice *dev = input->dev;
> > +       struct apple_spi_kbd_priv *priv = dev_get_priv(dev);
> > +       struct apple_spi_kbd_packet packet;
> > +       struct apple_spi_kbd_msg *msg;
> > +       struct apple_spi_kbd_report *report;
> > +       int i, ret;
> > +
> > +       memset(&packet, 0, sizeof(packet));
> > +
> > +       ret = dm_spi_claim_bus(dev);
> > +       if (ret < 0)
> > +               return ret;
> > +
> > +       /*
> > +        * The keyboard controller needs delays after asserting CS#
> > +        * and before deasserting CS#.
> > +        */
> > +       dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_BEGIN);
> 
> Check error returns in these

Sure.

> > +       udelay(100);
> > +       dm_spi_xfer(dev, sizeof(packet) * 8, NULL, &packet, 0);
> > +       udelay(100);
> > +       dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END);
> > +
> > +       dm_spi_release_bus(dev);
> > +
> > +       /*
> > +        * The keyboard controller needs a delay between subsequent
> > +        * SPI transfers.
> > +        */
> > +       udelay(250);
> 
> Is this the same delay for all models?

We think so.  The hardware differences between the different models
are really small.  The Linux folks may still refine their device tree
bindings, at which point we can update this code.

> > +
> > +       msg = (struct apple_spi_kbd_msg *)packet.data;
> > +       report = (struct apple_spi_kbd_report *)msg->data;
> > +       if (packet.flags == PACKET_READ && packet.device == KBD_DEVICE &&
> > +           msg->type == MSG_REPORT && msg->device == KBD_DEVICE &&
> > +           msg->cmdlen == sizeof(struct apple_spi_kbd_report) &&
> > +           report->reportid == KBD_REPORTID) {
> > +               memcpy(&priv->new, report,
> > +                      sizeof(struct apple_spi_kbd_report));
> > +               apple_spi_kbd_service_modifiers(input);
> > +               for (i = 0; i < sizeof(priv->new.keycode); i++) {
> > +                       apple_spi_kbd_service_key(input, i, 1);
> > +                       apple_spi_kbd_service_key(input, i, 0);
> > +               }
> > +               memcpy(&priv->old, &priv->new,
> > +                      sizeof(struct apple_spi_kbd_report));
> > +               return 1;
> > +       }
> > +
> > +       return 0;
> > +}
> > +
> > +static int apple_spi_kbd_probe(struct udevice *dev)
> > +{
> > +       struct apple_spi_kbd_priv *priv = dev_get_priv(dev);
> > +       struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
> > +       struct stdio_dev *sdev = &uc_priv->sdev;
> > +       struct input_config *input = &uc_priv->input;
> > +       int ret;
> > +
> > +       ret = gpio_request_by_name(dev, "spien-gpios", 0, &priv->enable,
> > +                                  GPIOD_IS_OUT);
> > +       if (ret < 0)
> > +               return ret;
> > +
> > +       /* Reset the keyboard controller. */
> > +       dm_gpio_set_value(&priv->enable, 1);
> > +       udelay(5000);
> > +       dm_gpio_set_value(&priv->enable, 0);
> > +       udelay(5000);
> > +
> > +       /* Enable the keyboard controller. */
> > +       dm_gpio_set_value(&priv->enable, 1);
> > +       udelay(50000);
> 
> That is a long time. Could we timestamp this instead and then check
> when the keyboard is read that enough time has elapsed?

Actually, I can delete that last delay.  If we poll the keyboard
before the controller is ready nothing bad will happen.

> > +
> > +       input->dev = dev;
> > +       input->read_keys = apple_spi_kbd_check;
> > +       input_add_tables(input, false);
> > +       strcpy(sdev->name, "spikbd");
> > +
> > +       return input_stdio_register(sdev);
> > +}
> > +
> > +static const struct keyboard_ops apple_spi_kbd_ops = {
> > +};
> > +
> > +static const struct udevice_id apple_spi_kbd_of_match[] = {
> > +       { .compatible = "apple,spi-hid-transport" },
> > +       { /* sentinel */ }
> > +};
> > +
> > +U_BOOT_DRIVER(apple_spi_kbd) = {
> > +       .name = "apple_spi_kbd",
> > +       .id = UCLASS_KEYBOARD,
> > +       .of_match = apple_spi_kbd_of_match,
> > +       .probe = apple_spi_kbd_probe,
> > +       .priv_auto = sizeof(struct apple_spi_kbd_priv),
> > +       .ops = &apple_spi_kbd_ops,
> > +};
> > diff --git a/include/configs/apple.h b/include/configs/apple.h
> > index 47faad8150..f12e9bdef5 100644
> > --- a/include/configs/apple.h
> > +++ b/include/configs/apple.h
> > @@ -5,7 +5,7 @@
> >
> >  /* Environment */
> >  #define ENV_DEVICE_SETTINGS \
> > -       "stdin=serial,usbkbd\0" \
> > +       "stdin=serial,usbkbd,spikbd\0" \
> >         "stdout=serial,vidconsole\0" \
> >         "stderr=serial,vidconsole\0"
> >
> > --
> > 2.34.1
> >
> 
> Regards,
> Simon
> 

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

end of thread, other threads:[~2022-01-22 16:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-16 17:06 [PATCH 0/2] Apple M1 laptop keyboard support Mark Kettenis
2022-01-16 17:06 ` [PATCH 1/2] spi: apple: Add driver for Apple SPI controller Mark Kettenis
2022-01-22  1:40   ` Simon Glass
2022-01-22 15:53     ` Mark Kettenis
2022-01-16 17:06 ` [PATCH 2/2] input: apple: Add support for Apple SPI keyboard Mark Kettenis
2022-01-22  1:40   ` Simon Glass
2022-01-22 16:26     ` Mark Kettenis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.