Apple M1 laptop keyboard support
Subsystems (3)
cover Apple M1 laptop keyboard support
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
Signature · 1 line
2.34.1
PATCH 1/2 spi: apple: Add driver for Apple SPI controller
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.
--- 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
arch/arm/Kconfig +2 -0
@@ -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
drivers/spi/Kconfig +7 -0
@@ -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
drivers/spi/Makefile +1 -0
@@ -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
drivers/spi/apple_spi.c new file +283 -0
283 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 1/2] spi: apple: Add driver for Apple SPI controller
Hi Mark,
Quoted 5 lines · 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.
Quoted 8 lines
Signed-off-by Mark Kettenis <[email protected]>--- 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
Tested on: Macbook Air M1
with nits below
Shouldn't you enable SPI flash and 'sspi' and 'sf' commands as well?
Quoted 158 lines
arch/arm/Kconfig +2
-0@@ -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_WATCHDOGdrivers/spi/Kconfig +7
-0@@ -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" helpdrivers/spi/Makefile +1
-0@@ -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.odrivers/spi/apple_spi.c new file +101
-0@@ -0,0 +1,283 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2021 Mark Kettenis <[email protected]> +* 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)
drivers/spi/apple_spi.c line 111
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-0+#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)
drivers/spi/apple_spi.c line 120
Quoted 9 lines
drivers/spi/apple_spi.c new file +9
-0+}; + +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
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-0+{ +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 ;
drivers/spi/apple_spi.c line 140
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-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
Quoted 17 lines
drivers/spi/apple_spi.c new file +17
-0+{ +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; +}
drivers/spi/apple_spi.c line 167
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-0+ +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//
drivers/spi/apple_spi.c line 174
Quoted 7 lines
drivers/spi/apple_spi.c new file +7
-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?
Quoted 5 lines
drivers/spi/apple_spi.c new file +5
-0+/* Reset FIFOs */ +writel (APPLE_SPI_CTRL_RX_RESET |APPLE_SPI_CTRL_TX_RESET , + priv->base +APPLE_SPI_CTRL ); + +/* Set the transfer length */
drivers/spi/apple_spi.c line 189
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-0+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
Quoted 95 lines
drivers/spi/apple_spi.c new file +94
-1+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, +}; --
Quoted 2 lines
2.34.1
Regards,
Simon
Re: [PATCH 1/2] spi: apple: Add driver for Apple SPI controller
Quoted 2 lines
From: Simon Glass <[email protected]>
Date: Fri, 21 Jan 2022 18:40:25 -0700
Hi Simon,
Quoted 17 lines
Hi Mark,
Quoted 5 lines · 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.
Quoted 8 lines
Signed-off-by Mark Kettenis <[email protected]>--- 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
Quoted 7 lines
Reviewed-by Simon Glass <[email protected]>Tested on: Macbook Air M1
Tested-by Simon Glass <[email protected]>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).
Quoted 162 lines
Quoted 162 lines
arch/arm/Kconfig +2
-0@@ -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_WATCHDOGdrivers/spi/Kconfig +7
-0@@ -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" helpdrivers/spi/Makefile +1
-0@@ -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.odrivers/spi/apple_spi.c new file +105
-0@@ -0,0 +1,283 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2021 Mark Kettenis <[email protected]> +* 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
Quoted 8 lines
drivers/spi/apple_spi.c line 111
Quoted 6 lines
drivers/spi/apple_spi.c new file +6
-0+ +#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.
Quoted 57 lines
drivers/spi/apple_spi.c line 120
Quoted 10 lines
drivers/spi/apple_spi.c new file +9
-0+}; + +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
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-0+{ +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 ;drivers/spi/apple_spi.c line 140
Quoted 10 lines
drivers/spi/apple_spi.c new file +10
-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
Quoted 21 lines
drivers/spi/apple_spi.c new file +21
-0+{ +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) +{
Quoted 8 lines
drivers/spi/apple_spi.c line 167
Quoted 6 lines
drivers/spi/apple_spi.c new file +6
-0+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.
Quoted 10 lines
drivers/spi/apple_spi.c line 174
Quoted 8 lines
drivers/spi/apple_spi.c new file +7
-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?
It is an unsigned variable, so no!
Quoted 9 lines
Quoted 9 lines
drivers/spi/apple_spi.c new file +9
-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 */
Quoted 8 lines
drivers/spi/apple_spi.c line 189
Quoted 6 lines
drivers/spi/apple_spi.c new file +6
-0+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 ;)
Quoted 101 lines
Quoted 95 lines
drivers/spi/apple_spi.c new file +94
-1+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, +}; --Quoted 2 lines
2.34.1
Regards,
Simon
PATCH 2/2 input: apple: Add support for Apple SPI keyboard
In reply to Mark KettenisThis 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.
--- 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
configs/apple_m1_defconfig +1 -0
@@ -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
drivers/input/Kconfig +8 -0
@@ -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
drivers/input/Makefile +1 -0
@@ -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
drivers/input/apple_spi_kbd.c new file +255 -0
255 lines not on this page · raw message
include/configs/apple.h +1 -1
@@ -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"
Signature · 1 line
2.34.1
Re: [PATCH 2/2] input: apple: Add support for Apple SPI keyboard
Hi Mark,
Quoted 11 lines · 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 <[email protected]>
Quoted 8 lines
--- 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
Tested on: Macbook Air M1
Various comments below
Doesn't this keyboard only existing on the laptop?
Quoted 102 lines
configs/apple_m1_defconfig +1
-0@@ -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 setdrivers/input/Kconfig +8
-0@@ -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 INPUTdrivers/input/Makefile +1
-0@@ -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.odrivers/input/apple_spi_kbd.c new file +53
-0@@ -0,0 +1,255 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2021 Mark Kettenis <[email protected]> +*/ + +#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 ,
drivers/input/apple_spi_kbd.c line 63
Quoted 10 lines
drivers/input/apple_spi_kbd.c new file +10
-0+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 ?
drivers/input/apple_spi_kbd.c line 75
Quoted 12 lines
drivers/input/apple_spi_kbd.c new file +12
-0+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
Quoted 6 lines
drivers/input/apple_spi_kbd.c new file +6
-0+ +/* Keyboard device. */ +#define KBD_DEVICE 0x01 + +struct apple_spi_kbd_packet { +u8 flags;
drivers/input/apple_spi_kbd.c line 91
Quoted 10 lines
drivers/input/apple_spi_kbd.c new file +10
-0+#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?
Quoted 73 lines
drivers/input/apple_spi_kbd.c new file +73
-0+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));
drivers/input/apple_spi_kbd.c line 174
Quoted 10 lines
drivers/input/apple_spi_kbd.c new file +10
-0+ + 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
drivers/input/apple_spi_kbd.c line 186
Quoted 12 lines
drivers/input/apple_spi_kbd.c new file +12
-0+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?
Quoted 34 lines
drivers/input/apple_spi_kbd.c new file +34
-0+ + 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;
drivers/input/apple_spi_kbd.c line 230
Quoted 10 lines
drivers/input/apple_spi_kbd.c new file +10
-0+ +/* 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?
Quoted 39 lines
drivers/input/apple_spi_kbd.c new file +25
-0+ + 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, +};include/configs/apple.h +1
-2@@ -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" --
Quoted 2 lines
2.34.1
Regards,
Simon
Re: [PATCH 2/2] input: apple: Add support for Apple SPI keyboard
Quoted 2 lines
From: Simon Glass <[email protected]>
Date: Fri, 21 Jan 2022 18:40:27 -0700
Hi Simon,
Quoted 23 lines
Hi Mark,
Quoted 11 lines · 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 <[email protected]>Quoted 8 lines
--- 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
Quoted 7 lines
Reviewed-by Simon Glass <[email protected]>Tested on: Macbook Air M1
Tested-by Simon Glass <[email protected]>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.
Quoted 105 lines
Quoted 105 lines
configs/apple_m1_defconfig +1
-0@@ -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 setdrivers/input/Kconfig +8
-0@@ -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 INPUTdrivers/input/Makefile +1
-0@@ -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.odrivers/input/apple_spi_kbd.c new file +57
-0@@ -0,0 +1,255 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2021 Mark Kettenis <[email protected]> +*/ + +#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 ,
Quoted 8 lines
drivers/input/apple_spi_kbd.c line 63
Quoted 6 lines
drivers/input/apple_spi_kbd.c new file +6
-0+}; + +/* 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.
Quoted 25 lines
drivers/input/apple_spi_kbd.c line 75
Quoted 12 lines
drivers/input/apple_spi_kbd.c new file +12
-0+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
Quoted 10 lines
drivers/input/apple_spi_kbd.c new file +10
-0+ +/* Keyboard device. */ +#define KBD_DEVICE 0x01 + +struct apple_spi_kbd_packet { +u8 flags; +#define PACKET_READ 0x20 +u8 device; +u16 offset; +u16 remaining;
Quoted 8 lines
drivers/input/apple_spi_kbd.c line 91
Quoted 6 lines
drivers/input/apple_spi_kbd.c new file +6
-0+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.
Quoted 78 lines
Quoted 78 lines
drivers/input/apple_spi_kbd.c new file +77
-0+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;
Quoted 8 lines
drivers/input/apple_spi_kbd.c line 174
Quoted 6 lines
drivers/input/apple_spi_kbd.c new file +6
-0+ +/* +* 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.
Quoted 6 lines
Quoted 6 lines
drivers/input/apple_spi_kbd.c new file +6
-0+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);
Quoted 8 lines
drivers/input/apple_spi_kbd.c line 186
Quoted 6 lines
drivers/input/apple_spi_kbd.c new file +6
-0+ +/* +* 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.
Quoted 39 lines
Quoted 39 lines
drivers/input/apple_spi_kbd.c new file +39
-0+ + 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 );
Quoted 8 lines
drivers/input/apple_spi_kbd.c line 230
Quoted 5 lines
drivers/input/apple_spi_kbd.c new file +5
-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.
Quoted 45 lines
Quoted 39 lines
drivers/input/apple_spi_kbd.c new file +25
-0+ + 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, +};include/configs/apple.h +1
-2@@ -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" --Quoted 2 lines
2.34.1
Regards,
Simon