# --- T2-COPYRIGHT-BEGIN --- # t2/package/*/linux/ppc-pmac-nubus.patch.powerpc # Copyright (C) 2026 The T2 SDE Project # SPDX-License-Identifier: GPL-2.0 or patched project license # --- T2-COPYRIGHT-END --- From 58d1fb2075502a7c6808ed05c68d0ee2429d97f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Rebe?= Date: Mon, 16 Nov 2026 15:11:35 +0100 Subject: [PATCH 3/3] powerpc: Add NuBus Power Macintosh support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the first Power Macintosh generation with NuBus slots, based on the nubus-pmac port (Takashi Oe, Tobias Netzel), MkLinux osfmk and the m68k Macintosh code: the PDM family (6100, 7100, 8100, Workgroup Server; needs a 601 kernel), the Performa/Power Macintosh 5200/6200 and the M2 PowerBooks (5300, 1400, Duo 2300). Device trees are built in and selected by the Gestalt machine ID, as BootX passes none on these machines. Platform: - PDM AMIC interrupt controller, in level mode as in MkLinux, using its latched mode only while a disabled source without a hardware mask is asserted - Performa Capella and M2 interrupt controller with the Baboon cascade, all pending sources dispatched at once as the CPU interrupt is latched - Cuda (ADB, RTC, restart, power off) and M2 PMU, ADB probing on this platform - 603 timebase measured with VIA timer 1, shared 256 KB AMIC DMA buffer area - NuBus bus core with arch hooks for the slot space and bus presence, machine check recovered hwreg_present(), slot interrupts through the device tree; mac8390 enabled, m68k behaviour unchanged Drivers: - serial: pmac_zilog through its platform path, selected at run time - SCSI: amic_esp (PDM 53C94/53CF94, AMIC DMA through a bounce buffer), performa_esp (53C96, PIO by default), mac_scsi on the M2 - IDE: pata_of_platform for the F108 and Baboon ports - Ethernet: amic_mace (MACE with AMIC DMA) - graphics: pdmfb (PDM on-board with mode setting and CLUT; boot mode and CLUT on the HPV and AV cards), valkyriefb on the Performa - sound: amic_awacs (PDM), primetime_asc (Performa, playback only) - floppy: swim3 with AMIC DMA, the macio code behind accessors and DMA ops Boot: - BootX entry on NuBus, also without PPC_PMAC (PPC_BOOTX): built-in device tree, memory, boot display and initrd from the boot infos, which move to a memory bank that holds them - book3s/32 kernel above physical 0 with the linear map at 0, the default for these machines at 2 MB: the PDM on-board video scans RAM at 0x100000, which is reserved no-map; the exception vectors are redirected in head - nubus_pmac_defconfig (601, 6.4 MiB including bss, for the 8 MB bank 0), nubus_pmac_603_defconfig, nubus_pmac_8mb_defconfig Generic fixes included: - prom_init: reset the length modifier for each conversion - BootX: sizes instead of end addresses in the memory reserve map, and an 8 byte aligned flattened device tree - prom: align the memory size to 16 MB only for book3s/64 (fixes 5ca096161cdc "powerpc/mm: Align memory_limit value specified using mem= kernel parameter") - esp_scsi: set the DMA length of identity mapped sg entries - pmac_zilog: unregister the console before disposing the ports - book3s/32: 64 KB early hash table without KASAN, physical address conversions by PAGE_OFFSET - boot wrapper: decompress at the kernel's physical start, refuse a compression wrapper.a cannot decompress Tested in MAME pmac8100/pmac6100 (601) and pmac6200 (603) with an ELF quickload and a BootX hand-over stub, with a high level Cuda model; the M2 drivers on a test machine modelled from the same sources as the drivers, which validates the software paths only. QEMU 40p, g3beige regressions. Nothing has run on real hardware yet. To check there: - AMIC latched mode, floppy DMA and the blank bit (MAME only); Performa ATA interrupt routing (MAME and MkLinux differ), EASC version and rate - machine check on empty NuBus slots, which also taints the kernel - M2: interrupt register latch and encoding, VIA2 and Baboon flag behaviour, PMU protocol and firmware, IDE byte order, 5380 pseudo DMA offsets - where a real BootX places the kernel and boot infos on these machines Signed-off-by: René Rebe Co-Authored-By: Claude Opus 5.5 --- arch/m68k/Kconfig.bus | 5 - arch/m68k/include/asm/nubus.h | 8 + arch/powerpc/Kbuild | 3 + arch/powerpc/Kconfig | 19 +- arch/powerpc/Makefile | 9 + arch/powerpc/boot/Makefile | 1 + arch/powerpc/boot/dts/Makefile | 4 + arch/powerpc/boot/dts/nubus-m2-2300.dts | 16 + arch/powerpc/boot/dts/nubus-m2.dts | 115 +++ arch/powerpc/boot/dts/nubus-pdm.dts | 201 ++++ arch/powerpc/boot/dts/nubus-performa.dts | 110 +++ arch/powerpc/boot/elf.h | 1 + arch/powerpc/boot/elf_util.c | 3 + arch/powerpc/boot/main.c | 21 +- arch/powerpc/boot/wrapper | 26 +- arch/powerpc/configs/nubus-603.config | 10 + arch/powerpc/configs/nubus-8mb.config | 4 + arch/powerpc/configs/nubus_pmac_defconfig | 115 +++ arch/powerpc/include/asm/amic_dma.h | 39 + arch/powerpc/include/asm/book3s/32/mmu-hash.h | 10 + arch/powerpc/include/asm/hwtest.h | 12 + arch/powerpc/include/asm/kdump.h | 34 - arch/powerpc/include/asm/nubus.h | 29 + arch/powerpc/include/asm/page.h | 10 +- arch/powerpc/include/asm/task_size_32.h | 2 +- arch/powerpc/kernel/cpu_setup_6xx.S | 3 +- arch/powerpc/kernel/crash_dump.c | 55 +- arch/powerpc/kernel/head_book3s_32.S | 103 +- arch/powerpc/kernel/prom.c | 11 +- arch/powerpc/kernel/prom_init.c | 51 +- arch/powerpc/kernel/setup_32.c | 2 - arch/powerpc/kernel/setup_64.c | 6 - arch/powerpc/kernel/swsusp_32.S | 2 +- arch/powerpc/mm/book3s32/hash_low.S | 4 +- arch/powerpc/mm/book3s32/mmu.c | 9 +- arch/powerpc/platforms/52xx/lite5200_sleep.S | 12 +- arch/powerpc/platforms/52xx/mpc52xx_pm.c | 2 +- arch/powerpc/platforms/Kconfig | 1 + arch/powerpc/platforms/Makefile | 5 +- arch/powerpc/platforms/nubus/Kconfig | 17 + arch/powerpc/platforms/nubus/Makefile | 4 + arch/powerpc/platforms/nubus/amic.c | 275 ++++++ arch/powerpc/platforms/nubus/amic_dma.c | 156 +++ arch/powerpc/platforms/nubus/bus.c | 89 ++ arch/powerpc/platforms/nubus/hwtest.S | 31 + arch/powerpc/platforms/nubus/iosb_pic.c | 325 ++++++ arch/powerpc/platforms/nubus/nubus.h | 20 + arch/powerpc/platforms/nubus/setup.c | 349 +++++++ arch/powerpc/platforms/nubus/udbg_scc.c | 124 +++ arch/powerpc/platforms/powermac/Makefile | 5 +- arch/powerpc/platforms/powermac/bootx_init.c | 609 +++++++++++- arch/powerpc/platforms/powermac/sleep.S | 3 +- drivers/Kconfig | 1 + drivers/block/Kconfig | 5 +- drivers/block/swim3.c | 663 +++++++++---- drivers/macintosh/Kconfig | 7 +- drivers/macintosh/adb.c | 7 +- drivers/macintosh/adbhid.c | 4 +- drivers/macintosh/via-pmu.c | 136 ++- drivers/net/ethernet/8390/Kconfig | 2 +- drivers/net/ethernet/8390/mac8390.c | 16 +- drivers/net/ethernet/apple/Kconfig | 13 +- drivers/net/ethernet/apple/Makefile | 1 + drivers/net/ethernet/apple/amic_mace.c | 665 +++++++++++++ drivers/nubus/Kconfig | 11 + drivers/nubus/bus.c | 2 + drivers/nubus/nubus.c | 51 +- drivers/nubus/proc.c | 13 +- drivers/scsi/Kconfig | 32 +- drivers/scsi/Makefile | 2 + drivers/scsi/amic_esp.c | 392 ++++++++ drivers/scsi/esp_scsi.c | 2 + drivers/scsi/mac_scsi.c | 115 ++- drivers/scsi/performa_esp.c | 334 +++++++ drivers/tty/serial/Kconfig | 2 +- drivers/tty/serial/pmac_zilog.c | 134 ++- drivers/tty/serial/pmac_zilog.h | 13 +- drivers/video/fbdev/Kconfig | 12 +- drivers/video/fbdev/Makefile | 1 + drivers/video/fbdev/pdmfb.c | 930 ++++++++++++++++++ drivers/video/fbdev/valkyriefb.c | 117 ++- drivers/video/fbdev/valkyriefb.h | 48 +- include/linux/nubus.h | 24 + include/uapi/linux/pmu.h | 2 + sound/ppc/Kconfig | 23 + sound/ppc/Makefile | 4 + sound/ppc/amic_awacs.c | 843 ++++++++++++++++ sound/ppc/primetime_asc.c | 461 +++++++++ 88 files changed, 7553 insertions(+), 615 deletions(-) create mode 100644 arch/powerpc/boot/dts/nubus-m2-2300.dts create mode 100644 arch/powerpc/boot/dts/nubus-m2.dts create mode 100644 arch/powerpc/boot/dts/nubus-pdm.dts create mode 100644 arch/powerpc/boot/dts/nubus-performa.dts create mode 100644 arch/powerpc/configs/nubus-603.config create mode 100644 arch/powerpc/configs/nubus-8mb.config create mode 100644 arch/powerpc/configs/nubus_pmac_defconfig create mode 100644 arch/powerpc/include/asm/amic_dma.h create mode 100644 arch/powerpc/include/asm/hwtest.h create mode 100644 arch/powerpc/include/asm/nubus.h create mode 100644 arch/powerpc/platforms/nubus/Kconfig create mode 100644 arch/powerpc/platforms/nubus/Makefile create mode 100644 arch/powerpc/platforms/nubus/amic.c create mode 100644 arch/powerpc/platforms/nubus/amic_dma.c create mode 100644 arch/powerpc/platforms/nubus/bus.c create mode 100644 arch/powerpc/platforms/nubus/hwtest.S create mode 100644 arch/powerpc/platforms/nubus/iosb_pic.c create mode 100644 arch/powerpc/platforms/nubus/nubus.h create mode 100644 arch/powerpc/platforms/nubus/setup.c create mode 100644 arch/powerpc/platforms/nubus/udbg_scc.c create mode 100644 drivers/net/ethernet/apple/amic_mace.c create mode 100644 drivers/nubus/Kconfig create mode 100644 drivers/scsi/amic_esp.c create mode 100644 drivers/scsi/performa_esp.c create mode 100644 drivers/video/fbdev/pdmfb.c create mode 100644 sound/ppc/amic_awacs.c create mode 100644 sound/ppc/primetime_asc.c diff --git a/arch/m68k/Kconfig.bus b/arch/m68k/Kconfig.bus index d5c0b2990ee1..4a6fae039dc6 100644 --- a/arch/m68k/Kconfig.bus +++ b/arch/m68k/Kconfig.bus @@ -12,11 +12,6 @@ config DIO HP300 machines. If you are using such a system you almost certainly want this. -config NUBUS - bool - depends on MAC - default y - config ZORRO bool "Amiga Zorro (AutoConfig) bus support" depends on AMIGA diff --git a/arch/m68k/include/asm/nubus.h b/arch/m68k/include/asm/nubus.h index c2281da6c51a..ba0efd59b7f7 100644 --- a/arch/m68k/include/asm/nubus.h +++ b/arch/m68k/include/asm/nubus.h @@ -1,9 +1,17 @@ /* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 René Rebe */ #ifndef _ASM_M68K_NUBUS_H #define _ASM_M68K_NUBUS_H #include #include +#include +#include + +static inline bool nubus_present(void) +{ + return MACH_IS_MAC; +} #define nubus_readb raw_inb #define nubus_readw raw_inw diff --git a/arch/powerpc/Kbuild b/arch/powerpc/Kbuild index b010ccb071b6..d1bdec0d4d22 100644 --- a/arch/powerpc/Kbuild +++ b/arch/powerpc/Kbuild @@ -18,5 +18,8 @@ obj-$(CONFIG_PERF_EVENTS) += perf/ obj-$(CONFIG_KEXEC_CORE) += kexec/ obj-$(CONFIG_KEXEC_FILE) += purgatory/ +# device trees built into the kernel +obj-$(CONFIG_PPC_NUBUS_PMAC) += boot/dts/ + # for cleaning subdir- += boot tools diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 93d07d216970..568037f8c82a 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -1247,7 +1247,7 @@ config PAGE_OFFSET config KERNEL_START_BOOL bool "Set custom kernel base address" - depends on ADVANCED_OPTIONS + depends on ADVANCED_OPTIONS && !PPC_BOOK3S_32 help This option allows you to set the kernel virtual address at which the kernel will be loaded. Normally this should match PAGE_OFFSET @@ -1256,28 +1256,41 @@ config KERNEL_START_BOOL Say N here unless you know what you are doing. +# book3s/32 derives the kernel base from PAGE_OFFSET and PHYSICAL_START config KERNEL_START hex "Virtual address of kernel base" if KERNEL_START_BOOL + depends on !PPC_BOOK3S_32 default PAGE_OFFSET if PAGE_OFFSET_BOOL - default "0xc2000000" if CRASH_DUMP && !NONSTATIC_KERNEL default "0xc0000000" config PHYSICAL_START_BOOL bool "Set physical address where the kernel is loaded" - depends on ADVANCED_OPTIONS && FLATMEM && PPC_85xx + depends on FLATMEM + depends on (ADVANCED_OPTIONS && PPC_85xx) || \ + (PPC_BOOK3S_32 && !SMP && !CRASH_DUMP) + default y if PPC_NUBUS_PMAC help This gives the physical address where the kernel is loaded. + On book3s/32 the kernel is then linked at PAGE_OFFSET plus this + address, the RAM below it remains usable and exceptions are + redirected from the vectors at 0. This keeps fixed low memory + ranges, like the frame buffer of the NuBus Power Macintosh PDM, + free of the kernel image. + Say N here unless you know what you are doing. config PHYSICAL_START hex "Physical address where the kernel is loaded" if PHYSICAL_START_BOOL + range 0x0 0x02000000 if PPC_BOOK3S_32 default "0x02000000" if PPC_BOOK3S && CRASH_DUMP && !NONSTATIC_KERNEL + default "0x00200000" if PPC_BOOK3S_32 && PHYSICAL_START_BOOL default "0x00000000" config PHYSICAL_ALIGN hex default "0x04000000" if PPC_85xx + default "0x00200000" if PPC_BOOK3S_32 help This value puts the alignment restrictions on physical address where kernel is loaded and run from. Kernel is compiled for an diff --git a/arch/powerpc/Makefile b/arch/powerpc/Makefile index 9385db478c59..5add8959756a 100644 --- a/arch/powerpc/Makefile +++ b/arch/powerpc/Makefile @@ -8,6 +8,7 @@ # Copyright (C) 1994 by Linus Torvalds # Changes for PPC by Gary Thomas # Rewritten by Cort Dougan and Paul Mackerras +# Copyright (C) 2026 René Rebe # ifdef cross_compiling @@ -327,6 +328,14 @@ generated_configs += ppc_defconfig ppc_defconfig: $(call merge_into_defconfig,book3s_32.config,) +generated_configs += nubus_pmac_603_defconfig +nubus_pmac_603_defconfig: + $(call merge_into_defconfig_override,nubus_pmac_defconfig,nubus-603) + +generated_configs += nubus_pmac_8mb_defconfig +nubus_pmac_8mb_defconfig: + $(call merge_into_defconfig_override,nubus_pmac_defconfig,nubus-8mb) + generated_configs += ppc64le_allmodconfig ppc64le_allmodconfig: $(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/powerpc/configs/le.config \ diff --git a/arch/powerpc/boot/Makefile b/arch/powerpc/boot/Makefile index ded50e698d4f..9a9c947b7c39 100644 --- a/arch/powerpc/boot/Makefile +++ b/arch/powerpc/boot/Makefile @@ -178,6 +178,7 @@ src-plat-$(CONFIG_MVME7100) += motload-head.S mvme7100.c src-plat-$(CONFIG_PPC_MICROWATT) += fixed-head.S microwatt.c src-plat-$(CONFIG_PPC_PREP) += prep-head.S prep.c +src-plat-$(CONFIG_PPC_NUBUS_PMAC) += fixed-head.S simpleboot.c src-wlib := $(sort $(src-wlib-y)) src-plat := $(sort $(src-plat-y)) diff --git a/arch/powerpc/boot/dts/Makefile b/arch/powerpc/boot/dts/Makefile index 1e61a951cebe..c08b5bbae398 100644 --- a/arch/powerpc/boot/dts/Makefile +++ b/arch/powerpc/boot/dts/Makefile @@ -4,6 +4,10 @@ subdir-y += fsl dtb-$(CONFIG_OF_ALL_DTBS) := $(patsubst $(src)/%.dts,%.dtb, $(wildcard $(src)/*.dts)) +# for BootX on NuBus Power Macintoshes, which has no device tree +obj-$(CONFIG_PPC_NUBUS_PMAC) += nubus-pdm.dtb.o nubus-performa.dtb.o nubus-m2.dtb.o \ + nubus-m2-2300.dtb.o + # PPC44x platforms dtb-$(CONFIG_PPC44x_SIMPLE) += arches.dtb bamboo.dtb bluestone.dtb glacier.dtb dtb-$(CONFIG_PPC44x_SIMPLE) += eiger.dtb katmai.dtb rainier.dtb redwood.dtb diff --git a/arch/powerpc/boot/dts/nubus-m2-2300.dts b/arch/powerpc/boot/dts/nubus-m2-2300.dts new file mode 100644 index 000000000000..16406e7036b0 --- /dev/null +++ b/arch/powerpc/boot/dts/nubus-m2-2300.dts @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * PowerBook Duo 2300: M2 PowerBook with the Baboon flags on slot bit 0. + * + * Copyright (C) 2026 René Rebe + */ + +/include/ "nubus-m2.dts" + +/ { + model = "PowerBook Duo 2300"; +}; + +&pic { + apple,baboon-slot = <0>; +}; diff --git a/arch/powerpc/boot/dts/nubus-m2.dts b/arch/powerpc/boot/dts/nubus-m2.dts new file mode 100644 index 000000000000..9430057ad5ce --- /dev/null +++ b/arch/powerpc/boot/dts/nubus-m2.dts @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NuBus Power Macintosh, M2 PowerBooks with Baboon IDE (5300, 1400, 2300; + * Whitney I/O). The memory size and the timebase are set by the boot + * loader resp. measured by the kernel. + * + * Addresses from MkLinux (osfmk powermac_m2.h) and the 2001 nubus-pmac + * NuBusPowerBook.dts. + * + * Copyright (C) 2026 René Rebe + */ + +/dts-v1/; + +/ { + model = "PowerBook 5300/1400/2300"; + compatible = "apple,m2", "apple,nubus-pmac"; + interrupt-parent = <&pic>; + #address-cells = <1>; + #size-cells = <1>; + + cpus { + #address-cells = <1>; + #size-cells = <0>; + + cpu@0 { + device_type = "cpu"; + reg = <0>; + d-cache-line-size = <32>; + i-cache-line-size = <32>; + }; + }; + + memory@0 { + device_type = "memory"; + reg = <0 0>; + }; + + chosen { + stdout-path = &scc; + }; + + whitney-io { + compatible = "apple,whitney-io", "simple-bus"; + #address-cells = <1>; + #size-cells = <1>; + ranges; + + /* + * M2 ICR, VIA1, pseudo VIA2, Baboon interrupt flags; hwirq 4 SCC, + * 7 NMI, 8-14 VIA1 (10 PMU shift register, 12 PMU CB1), 16-22 VIA2, + * 24-30 slots, 32-34 Baboon (32 media bay IDE, 33 internal IDE) + */ + pic: interrupt-controller@50f2a003 { + compatible = "apple,m2-pic"; + reg = <0x50f2a003 0x1>, <0x50f00000 0x2000>, + <0x50f02000 0x2000>, <0x50f1a0d9 0x1>; + interrupt-controller; + #interrupt-cells = <1>; + /* Baboon cascade slot bit, 0 on the Duo 2300 */ + apple,baboon-slot = <3>; + }; + + /* + * PMU on the VIA1 shift register, handshake on VIA2 port B; + * interrupts: shift register, CB1 + */ + via-pmu@50f00000 { + compatible = "apple,m2-pmu"; + device_type = "pmu"; + reg = <0x50f00000 0x4000>; + interrupts = <10>, <12>; + }; + + /* Z85C30, byte registers at even addresses */ + scc: serial@50f04000 { + compatible = "apple,nubus-escc"; + reg = <0x50f04000 0x8>; + interrupts = <4>; + clock-frequency = <3686400>; + current-speed = <9600>; + }; + + /* + * NCR 5380 at register stride 16, pseudo DMA with DRQ handshake + * (as the m68k LC); interrupt in VIA2, the DRQ (hwirq 16) unused + */ + scsi@50f10000 { + compatible = "apple,m2-ncr5380"; + reg = <0x50f10000 0x2000>, <0x50f06000 0x2000>; + interrupts = <19>; + }; + + /* + * Baboon ATA ports, task file at stride 4, control at +0x38, + * 16 bit data (MkLinux wd.c, the nubus-pmac nbpmacide) + */ + ata@50f1a000 { + compatible = "apple,baboon-ata", "ata-generic"; + reg = <0x50f1a000 0x38>, <0x50f1a038 0x4>; + reg-shift = <2>; + ata-generic,use16bit; + interrupts = <33>; + }; + + /* expansion (media) bay */ + ata@50f1a080 { + compatible = "apple,baboon-ata", "ata-generic"; + reg = <0x50f1a080 0x38>, <0x50f1a0b8 0x4>; + reg-shift = <2>; + ata-generic,use16bit; + interrupts = <32>; + }; + }; +}; diff --git a/arch/powerpc/boot/dts/nubus-pdm.dts b/arch/powerpc/boot/dts/nubus-pdm.dts new file mode 100644 index 000000000000..da82beaccc28 --- /dev/null +++ b/arch/powerpc/boot/dts/nubus-pdm.dts @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NuBus Power Macintosh, PDM (Piltdown Man) family: 6100, 7100, 8100, + * Workgroup Server 6150, 8150, 9150. The memory size is set by the boot + * loader. + * + * Based on the NuBusPowerMac.dts of the nubus-pmac project. + * + * Copyright (C) 2026 René Rebe + */ + +/dts-v1/; + +/ { + model = "Power Macintosh (PDM)"; + compatible = "apple,pdm", "apple,nubus-pmac"; + interrupt-parent = <&pic>; + #address-cells = <1>; + #size-cells = <1>; + + cpus { + #address-cells = <1>; + #size-cells = <0>; + + cpu@0 { + device_type = "cpu"; + reg = <0>; + clock-frequency = <80000000>; + bus-frequency = <40000000>; + /* the 601 RTC and decrementer count nanoseconds */ + timebase-frequency = <1000000000>; + d-cache-line-size = <32>; + i-cache-line-size = <32>; + }; + }; + + memory@0 { + device_type = "memory"; + reg = <0 0>; + }; + + reserved-memory { + #address-cells = <1>; + #size-cells = <1>; + ranges; + + /* the on-board video scans main memory from this fixed address */ + vram: framebuffer@100000 { + reg = <0x100000 0x96000>; + no-map; + }; + }; + + chosen { + stdout-path = &scc; + }; + + amic-io { + compatible = "apple,amic-io", "simple-bus"; + #address-cells = <1>; + #size-cells = <1>; + ranges; + + /* + * AMIC interrupt control and the pseudo VIA2, hwirq numbers as in + * MkLinux: 0-5 ICR, 16-22 VIA2, 26-30 NuBus slots/VBL, 32-38 DMA, + * 40-41 audio DMA + */ + pic: interrupt-controller@50f2a000 { + compatible = "apple,amic-pic"; + reg = <0x50f2a000 0x10>, <0x50f26000 0x20>; + interrupt-controller; + #interrupt-cells = <1>; + }; + + via-cuda@50f00000 { + compatible = "apple,pdm-cuda"; + device_type = "cuda"; + reg = <0x50f00000 0x2000>; + interrupts = <0>; + }; + + /* + * 53C94 and, on the 8100 only, the fast 53CF94 (probed like + * MkLinux does), registers at a 16 byte stride; AMIC DMA + * base address and control registers of the channel + */ + scsi@50f10000 { + compatible = "apple,amic-esp"; + reg = <0x50f10000 0x100>, <0x50f32000 0x4>, + <0x50f32008 0x1>; + interrupts = <19>; + clock-frequency = <20000000>; + }; + + scsi@50f11000 { + compatible = "apple,amic-esp"; + reg = <0x50f11000 0x100>, <0x50f32004 0x4>, + <0x50f32009 0x1>; + interrupts = <22>; + clock-frequency = <40000000>; + }; + + /* Z85C30, byte registers at even addresses */ + scc: serial@50f04000 { + compatible = "apple,nubus-escc"; + reg = <0x50f04000 0x8>; + interrupts = <2>; + clock-frequency = <3686400>; + current-speed = <9600>; + }; + + /* + * On-board video: CLUT DAC, video control and the DRAM frame + * buffer (HMC vbase clear), vertical blank interrupt. BootX + * support adds the boot display properties. The HPV and AV + * cards are not described here: BootX support creates their + * nodes when Mac OS used one as the boot display. + */ + video@50f24000 { + compatible = "apple,pdm-video"; + device_type = "display"; + reg = <0x50f24000 0x4>, <0x50f28000 0x8>, + <0x00100000 0x96000>; + interrupts = <30>; + }; + + /* base address of the 256 KB DMA buffer area */ + amic-dma@50f31000 { + compatible = "apple,amic-dma"; + reg = <0x50f31000 0x4>; + }; + + /* + * Am79C940 MACE with the MAC address PROM, the AMIC transmit DMA + * control and the receive DMA control, ring pointers and transmit + * byte counts + */ + ethernet@50f0a000 { + compatible = "apple,amic-mace"; + reg = <0x50f0a000 0x200>, <0x50f08000 0x80>, + <0x50f31c20 0x1>, <0x50f32028 0x30>; + reg-names = "mace", "prom", "txdma", "rxdma"; + interrupts = <3>, <36>, <37>; + interrupt-names = "mace", "rxdma", "txdma"; + }; + + /* AWACS, sound DMA from the AMIC area, output and input DMA interrupts */ + sound@50f14000 { + compatible = "apple,amic-awacs"; + reg = <0x50f14000 0x20>; + interrupts = <41>, <40>; + interrupt-names = "out", "in"; + }; + + /* + * SWIM3, registers at a 512 byte stride, and the AMIC floppy DMA + * address, byte count and control registers + */ + floppy@50f16000 { + compatible = "apple,amic-swim3"; + reg = <0x50f16000 0x2000>, <0x50f32060 0x10>; + interrupts = <21>, <38>; + interrupt-names = "swim3", "dma"; + }; + }; + + /* + * BART NuBus controller. The children are the NuBus slots by their + * standard slot space address 0xFs000000, slots B-E have an interrupt + * in the VIA2 slot register. The 6100 PDS adapter is slot E, the 7100 + * and 8100 have slots C-E (MAME macpdm.cpp). + */ + nubus@f0000000 { + compatible = "apple,pdm-nubus", "apple,nubus"; + reg = <0xf0000000 0x80>; + #address-cells = <1>; + #size-cells = <1>; + ranges = <0xfb000000 0xfb000000 0x04000000>; + + slot@fb000000 { + reg = <0xfb000000 0x01000000>; + interrupts = <26>; + }; + + slot@fc000000 { + reg = <0xfc000000 0x01000000>; + interrupts = <27>; + }; + + slot@fd000000 { + reg = <0xfd000000 0x01000000>; + interrupts = <28>; + }; + + slot@fe000000 { + reg = <0xfe000000 0x01000000>; + interrupts = <29>; + }; + }; +}; diff --git a/arch/powerpc/boot/dts/nubus-performa.dts b/arch/powerpc/boot/dts/nubus-performa.dts new file mode 100644 index 000000000000..db99c6d38368 --- /dev/null +++ b/arch/powerpc/boot/dts/nubus-performa.dts @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NuBus Power Macintosh, Performa/Power Macintosh 5200/6200 family + * ("Cordyceps": Capella bridge, PrimeTime II, F108). The memory size and + * the timebase are set by the boot loader resp. measured by the kernel. + * + * Addresses from MkLinux (osfmk powermac_performa.h), the 6200 ROM decoder + * table and MAME. + * + * Copyright (C) 2026 René Rebe + */ + +/dts-v1/; + +/ { + model = "Power Macintosh 5200/6200"; + compatible = "apple,performa-5200", "apple,nubus-pmac"; + interrupt-parent = <&pic>; + #address-cells = <1>; + #size-cells = <1>; + + cpus { + #address-cells = <1>; + #size-cells = <0>; + + cpu@0 { + device_type = "cpu"; + reg = <0>; + clock-frequency = <75000000>; + bus-frequency = <37500000>; + d-cache-line-size = <32>; + i-cache-line-size = <32>; + }; + }; + + memory@0 { + device_type = "memory"; + reg = <0 0>; + }; + + chosen { + stdout-path = &scc; + }; + + primetime-io { + compatible = "apple,primetime-io", "simple-bus"; + #address-cells = <1>; + #size-cells = <1>; + ranges; + + /* + * Capella IPL/acknowledge, VIA1, pseudo VIA2; hwirq 1 Cuda (VIA1), + * 4 SCC, 7 NMI, 16-22 VIA2, 24-30 slots (28 ATA, 30 VBL) + */ + pic: interrupt-controller@53000000 { + compatible = "apple,capella-pic"; + reg = <0x53000000 0x28>, <0x50f00000 0x2000>, + <0x50f02000 0x2000>; + interrupt-controller; + #interrupt-cells = <1>; + }; + + via-cuda@50f00000 { + compatible = "apple,performa-cuda"; + device_type = "cuda"; + reg = <0x50f00000 0x2000>; + interrupts = <1>; + }; + + /* Z85C30 in the F108, byte registers at even addresses */ + scc: serial@50f0c020 { + compatible = "apple,nubus-escc"; + reg = <0x50f0c020 0x8>; + interrupts = <4>; + clock-frequency = <3686400>; + current-speed = <9600>; + }; + + /* NCR 53C96 in the F108, PrimeTime II pseudo DMA port */ + scsi@50f10000 { + compatible = "apple,performa-esp"; + reg = <0x50f10000 0x100>, <0x50f10100 0x4>; + interrupts = <19>; + clock-frequency = <16500000>; + }; + + /* F108 ATA, task file at a 4 byte stride */ + ata@50f1a000 { + compatible = "apple,f108-ata", "ata-generic"; + reg = <0x50f1a000 0x20>, <0x50f1a038 0x4>; + reg-shift = <2>; + interrupts = <28>; + }; + + /* Apple Sound Chip FIFOs, interrupt on VIA2 CB1 */ + sound@50f14000 { + compatible = "apple,primetime-asc"; + reg = <0x50f14000 0x1000>; + interrupts = <20>; + }; + + /* frame buffer, CLUT, registers */ + valkyrie@f9000000 { + compatible = "apple,valkyrie"; + reg = <0xf9000000 0x100000>, <0x50f24000 0x1000>, + <0x50f2a000 0x1000>; + interrupts = <30>; + }; + }; +}; diff --git a/arch/powerpc/boot/elf.h b/arch/powerpc/boot/elf.h index f6aa7c20fcaf..8f0ea3001f03 100644 --- a/arch/powerpc/boot/elf.h +++ b/arch/powerpc/boot/elf.h @@ -148,6 +148,7 @@ typedef struct elf64_phdr { #define ELFOSABI_LINUX 3 struct elf_info { + unsigned long loadaddr; unsigned long loadsize; unsigned long memsize; unsigned long elfoffset; diff --git a/arch/powerpc/boot/elf_util.c b/arch/powerpc/boot/elf_util.c index 9e6cbdfdc172..8c7c0ffb81aa 100644 --- a/arch/powerpc/boot/elf_util.c +++ b/arch/powerpc/boot/elf_util.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) Paul Mackerras 1997. + * Copyright (C) 2026 René Rebe * * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner. */ @@ -40,6 +41,7 @@ int parse_elf64(void *hdr, struct elf_info *info) if (i >= (unsigned int)elf64->e_phnum) return 0; + info->loadaddr = (unsigned long)elf64ph->p_paddr; info->loadsize = (unsigned long)elf64ph->p_filesz; info->memsize = (unsigned long)elf64ph->p_memsz; info->elfoffset = (unsigned long)elf64ph->p_offset; @@ -71,6 +73,7 @@ int parse_elf32(void *hdr, struct elf_info *info) if (i >= elf32->e_phnum) return 0; + info->loadaddr = elf32ph->p_paddr; info->loadsize = elf32ph->p_filesz; info->memsize = elf32ph->p_memsz; info->elfoffset = elf32ph->p_offset; diff --git a/arch/powerpc/boot/main.c b/arch/powerpc/boot/main.c index 2c0e2a1cab01..7f1466a539a6 100644 --- a/arch/powerpc/boot/main.c +++ b/arch/powerpc/boot/main.c @@ -3,6 +3,8 @@ * Copyright (C) Paul Mackerras 1997. * * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner. + * + * Copyright (C) 2026 René Rebe */ #include #include @@ -20,6 +22,9 @@ struct addr_range { #undef DEBUG +/* end of the kernel at its final physical location */ +static unsigned long vmlinux_end; + static struct addr_range prep_kernel(void) { char elfheader[256]; @@ -51,21 +56,25 @@ static struct addr_range prep_kernel(void) * kernel itself) */ printf("Allocating 0x%lx bytes for kernel...\n\r", ei.memsize); + vmlinux_end = ei.loadaddr + ei.memsize; if (platform_ops.vmlinux_alloc) { addr = platform_ops.vmlinux_alloc(ei.memsize); } else { + /* load the kernel at its physical start address */ + addr = (void *)ei.loadaddr; + /* * Check if the kernel image (without bss) would overwrite the * bootwrapper. The device tree has been moved in fdt_init() * to an area allocated with malloc() (somewhere past _end). */ - if ((unsigned long)_start < ei.loadsize) - fatal("Insufficient memory for kernel at address 0!" + if ((unsigned long)_start < ei.loadaddr + ei.loadsize) + fatal("Insufficient memory for kernel at address 0x%lx!" " (_start=%p, uncompressed size=%08lx)\n\r", - _start, ei.loadsize); + ei.loadaddr, _start, ei.loadsize); - if ((unsigned long)_end < ei.memsize) + if ((unsigned long)_end < vmlinux_end) fatal("The final kernel image would overwrite the " "device tree\n\r"); } @@ -123,7 +132,7 @@ static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen, * kernel relocates to its final location. In this case, * allocate a safer place and move it. */ - if (initrd_addr < vmlinux.size) { + if (initrd_addr < vmlinux_end) { void *old_addr = (void *)initrd_addr; printf("Allocating 0x%lx bytes for initrd ...\n\r", @@ -165,7 +174,7 @@ static void prep_esm_blob(struct addr_range vmlinux, void *chosen) * kernel relocates to its final location. In this case, * allocate a safer place and move it. */ - if (esm_blob_addr < vmlinux.size) { + if (esm_blob_addr < vmlinux_end) { void *old_addr = (void *)esm_blob_addr; printf("Allocating 0x%lx bytes for esm_blob ...\n\r", diff --git a/arch/powerpc/boot/wrapper b/arch/powerpc/boot/wrapper index f7616fd05093..3c4d83a5663a 100755 --- a/arch/powerpc/boot/wrapper +++ b/arch/powerpc/boot/wrapper @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-2.0-only # Copyright (C) 2006 Paul Mackerras, IBM Corporation +# Copyright (C) 2026 René Rebe # This script takes a kernel binary and optionally an initrd image # and/or a device-tree blob, and creates a bootable zImage for a @@ -426,14 +427,17 @@ fi vmz="$vmz$compression" +# physical offset of kernel image +membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'` + if [ "$make_space" = "y" ]; then - # Round the size to next higher MB limit - round_size=$(((strip_size + 0xfffff) & 0xfff00000)) + # Round the end of the kernel at its physical offset to next higher MB limit + round_size=$(((membase + strip_size + 0xfffff) & 0xfff00000)) round_size=0x$(printf "%x" $round_size) link_addr=$(printf "%d" $link_address) - if [ $link_addr -lt $strip_size ]; then + if [ $link_addr -lt $((membase + strip_size)) ]; then echo "INFO: Uncompressed kernel (size 0x$(printf "%x\n" $strip_size))" \ "overlaps the address of the wrapper($link_address)" echo "INFO: Fixing the link_address of wrapper to ($round_size)" @@ -445,7 +449,7 @@ fi if [ "$platform" = "prep" ]; then kend=0x$(${CROSS}nm "$kernel" | grep ' _end$' | cut -d' ' -f1) kbase=$(${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $5}') - kmem=$(((kend - kbase + 0xfffff) & ~0xfffff)) + kmem=$(((membase + kend - kbase + 0xfffff) & ~0xfffff)) if [ $(printf "%d" $link_address) -lt $kmem ]; then link_address=0x$(printf "%x" $kmem) fi @@ -459,9 +463,6 @@ if [ -n "$version" ]; then uboot_version="-n Linux-$version" fi -# physical offset of kernel image -membase=`${CROSS}objdump -p "$kernel" | grep -m 1 LOAD | awk '{print $7}'` - case "$platform" in uboot) rm -f "$ofile" @@ -501,6 +502,17 @@ if [ -n "$esm_blob" ]; then fi if [ "$platform" != "miboot" ]; then + # wrapper.a only has the decompressor of the CONFIG_KERNEL_* compression + case "$compression" in + .gz) decomp=zlib_inflate; kconf=CONFIG_KERNEL_GZIP ;; + .xz) decomp=xz_dec_run; kconf=CONFIG_KERNEL_XZ ;; + *) decomp= ;; + esac + if [ -n "$decomp" ] && ! ${CROSS}nm "$object/wrapper.a" | grep -q " T $decomp\$"; then + echo "$object/wrapper.a has no ${compression#.} decompressor, it needs $kconf" >&2 + rm -f "$tmp" + exit 1 + fi if [ -n "$link_address" ] ; then text_start="-Ttext $link_address" fi diff --git a/arch/powerpc/configs/nubus-603.config b/arch/powerpc/configs/nubus-603.config new file mode 100644 index 000000000000..e11e164bca3b --- /dev/null +++ b/arch/powerpc/configs/nubus-603.config @@ -0,0 +1,10 @@ +# 603 kernel for the Performa 5200/6200 and the M2 PowerBooks (5300, 1400, +# Duo 2300), on top of nubus_pmac_defconfig; PPC_PMAC for the M2 PMU +# CONFIG_PPC_BOOK3S_601 is not set +CONFIG_PPC_PMAC=y +CONFIG_ADB_PMU=y +CONFIG_ATA=y +CONFIG_PATA_OF_PLATFORM=y +CONFIG_MAC_SCSI=y +CONFIG_SCSI_PERFORMA_ESP=y +CONFIG_FB_VALKYRIE=y diff --git a/arch/powerpc/configs/nubus-8mb.config b/arch/powerpc/configs/nubus-8mb.config new file mode 100644 index 000000000000..d4adb5624195 --- /dev/null +++ b/arch/powerpc/configs/nubus-8mb.config @@ -0,0 +1,4 @@ +# PDM machine with only the 8 MB on-board RAM (6100 without SIMMs), on top +# of nubus_pmac_defconfig: about 3 MB are left beside the kernel and video +CONFIG_SLUB_TINY=y +CONFIG_LOG_BUF_SHIFT=14 diff --git a/arch/powerpc/configs/nubus_pmac_defconfig b/arch/powerpc/configs/nubus_pmac_defconfig new file mode 100644 index 000000000000..14b581858efe --- /dev/null +++ b/arch/powerpc/configs/nubus_pmac_defconfig @@ -0,0 +1,115 @@ +CONFIG_SYSVIPC=y +CONFIG_POSIX_MQUEUE=y +CONFIG_NO_HZ_IDLE=y +CONFIG_HIGH_RES_TIMERS=y +# CONFIG_PREEMPT_DYNAMIC is not set +CONFIG_IKCONFIG=m +CONFIG_IKCONFIG_PROC=y +CONFIG_LOG_BUF_SHIFT=14 +CONFIG_BLK_DEV_INITRD=y +# CONFIG_RD_BZIP2 is not set +# CONFIG_RD_LZMA is not set +# CONFIG_RD_LZO is not set +# CONFIG_RD_LZ4 is not set +# CONFIG_RD_ZSTD is not set +CONFIG_CC_OPTIMIZE_FOR_SIZE=y +CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y +CONFIG_EXPERT=y +CONFIG_BASE_SMALL=y +# CONFIG_IO_URING is not set +# CONFIG_KALLSYMS is not set +CONFIG_PPC_BOOK3S_601=y +# CONFIG_PPC_CHRP is not set +# CONFIG_PPC_PMAC is not set +CONFIG_PPC_NUBUS_PMAC=y +CONFIG_HZ_100=y +# CONFIG_STACKPROTECTOR_STRONG is not set +# CONFIG_STRICT_KERNEL_RWX is not set +# CONFIG_STRICT_MODULE_RWX is not set +CONFIG_MODULES=y +CONFIG_MODULE_UNLOAD=y +CONFIG_TRIM_UNUSED_KSYMS=y +# CONFIG_BLOCK_LEGACY_AUTOLOAD is not set +CONFIG_PARTITION_ADVANCED=y +CONFIG_MAC_PARTITION=y +# CONFIG_EFI_PARTITION is not set +# CONFIG_MQ_IOSCHED_KYBER is not set +CONFIG_BINFMT_MISC=m +# CONFIG_COMPACTION is not set +CONFIG_NET=y +CONFIG_PACKET=y +CONFIG_UNIX=y +CONFIG_INET=y +CONFIG_IP_MULTICAST=y +CONFIG_SYN_COOKIES=y +CONFIG_INET_TABLE_PERTURB_ORDER=10 +# CONFIG_INET_DIAG is not set +# CONFIG_IPV6 is not set +# CONFIG_WIRELESS is not set +# CONFIG_ETHTOOL_NETLINK is not set +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +CONFIG_FW_LOADER=m +CONFIG_MAC_FLOPPY=m +CONFIG_BLK_DEV_LOOP=m +CONFIG_SCSI=y +# CONFIG_SCSI_PROC_FS is not set +CONFIG_BLK_DEV_SD=y +CONFIG_BLK_DEV_SR=m +CONFIG_CHR_DEV_SG=m +# CONFIG_BLK_DEV_BSG is not set +CONFIG_SCSI_AMIC_ESP=y +CONFIG_MACINTOSH_DRIVERS=y +CONFIG_ADB=y +CONFIG_ADB_CUDA=y +CONFIG_INPUT_ADBHID=y +CONFIG_MAC_EMUMOUSEBTN=m +CONFIG_NETDEVICES=y +CONFIG_AMIC_MACE=m +CONFIG_MAC8390=m +CONFIG_PPP=m +CONFIG_PPP_DEFLATE=m +CONFIG_PPP_ASYNC=m +# CONFIG_WLAN is not set +CONFIG_INPUT_EVDEV=m +# CONFIG_KEYBOARD_ATKBD is not set +# CONFIG_INPUT_MOUSE is not set +# CONFIG_SERIO is not set +# CONFIG_LEGACY_PTYS is not set +CONFIG_SERIAL_PMACZILOG=y +CONFIG_SERIAL_PMACZILOG_CONSOLE=y +# CONFIG_NVRAM is not set +# CONFIG_PTP_1588_CLOCK is not set +# CONFIG_HWMON is not set +CONFIG_FB=y +CONFIG_FB_OF=y +CONFIG_FB_PDM=y +CONFIG_FRAMEBUFFER_CONSOLE=y +CONFIG_SOUND=m +CONFIG_SND=m +CONFIG_SND_AMIC_AWACS=m +# CONFIG_HID_SUPPORT is not set +# CONFIG_USB_SUPPORT is not set +CONFIG_EXT4_FS=y +CONFIG_EXT4_FS_POSIX_ACL=y +CONFIG_AUTOFS_FS=m +CONFIG_ISO9660_FS=m +CONFIG_JOLIET=y +CONFIG_MSDOS_FS=m +CONFIG_VFAT_FS=m +# CONFIG_PROC_PAGE_MONITOR is not set +CONFIG_TMPFS=y +CONFIG_HFS_FS=m +CONFIG_HFSPLUS_FS=m +CONFIG_NFS_FS=m +CONFIG_NFS_V4=m +CONFIG_NFSD=m +CONFIG_NLS_CODEPAGE_437=m +CONFIG_NLS_ISO8859_1=m +CONFIG_NLS_MAC_ROMAN=m +CONFIG_PRINTK_TIME=y +# CONFIG_SYMBOLIC_ERRNAME is not set +# CONFIG_DEBUG_BUGVERBOSE is not set +CONFIG_MAGIC_SYSRQ=y +# CONFIG_SLUB_DEBUG is not set +CONFIG_BOOTX_TEXT=y diff --git a/arch/powerpc/include/asm/amic_dma.h b/arch/powerpc/include/asm/amic_dma.h new file mode 100644 index 000000000000..7d6b91cf7060 --- /dev/null +++ b/arch/powerpc/include/asm/amic_dma.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * DMA buffer area of the PDM (Power Macintosh 6100/7100/8100) AMIC. + * + * Copyright (C) 2026 René Rebe + */ +#ifndef _ASM_POWERPC_AMIC_DMA_H +#define _ASM_POWERPC_AMIC_DMA_H + +#include + +struct device; + +/* fixed sub-buffers of the 256 KB area the AMIC DMA base register selects */ +enum amic_dma_area { + AMIC_DMA_ENET_RX, /* 0x00000, 48 KB ring of 256 byte pages */ + AMIC_DMA_SOUND_IN, /* 0x0c000, two 8 KB halves */ + AMIC_DMA_SOUND_OUT, /* 0x10000, two 8 KB halves */ + AMIC_DMA_ENET_TX, /* 0x14000, two 2 KB transmit sets */ + AMIC_DMA_FLOPPY, /* 0x15000, offset chosen by software */ + AMIC_DMA_SCC_B_TX, /* 0x20000 */ + AMIC_DMA_SCC_A_RX, /* 0x22000 */ + AMIC_DMA_SCC_A_TX, /* 0x24000 */ + AMIC_DMA_SCC_B_RX, /* 0x26000 */ + AMIC_DMA_NR_AREAS +}; + +struct amic_dma_buf { + void __iomem *cpu; /* uncached, use the _io accessors */ + dma_addr_t dma; /* address as seen by the AMIC */ + size_t size; +}; + +int amic_dma_request(enum amic_dma_area area, struct amic_dma_buf *buf); +void amic_dma_release(enum amic_dma_area area); +int devm_amic_dma_request(struct device *dev, enum amic_dma_area area, + struct amic_dma_buf *buf); + +#endif /* _ASM_POWERPC_AMIC_DMA_H */ diff --git a/arch/powerpc/include/asm/book3s/32/mmu-hash.h b/arch/powerpc/include/asm/book3s/32/mmu-hash.h index 387d370c8a35..e11b7f8e61be 100644 --- a/arch/powerpc/include/asm/book3s/32/mmu-hash.h +++ b/arch/powerpc/include/asm/book3s/32/mmu-hash.h @@ -53,6 +53,16 @@ struct ppc_bat { * Hash table */ +/* + * Static hash table until MMU_init_hw() allocates the real one: the minimum + * size is enough for the few early mappings outside the BATs, KASAN needs more + */ +#ifdef CONFIG_KASAN +#define EARLY_HASH_SHIFT 18 +#else +#define EARLY_HASH_SHIFT 16 +#endif + /* Values for PP (assumes Ks=0, Kp=1) */ #define PP_RWXX 0 /* Supervisor read/write, User none */ #define PP_RWRX 1 /* Supervisor read/write, User read */ diff --git a/arch/powerpc/include/asm/hwtest.h b/arch/powerpc/include/asm/hwtest.h new file mode 100644 index 000000000000..83871f9916f3 --- /dev/null +++ b/arch/powerpc/include/asm/hwtest.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Test for the presence of hardware registers, as on m68k. + * + * Copyright (C) 2026 René Rebe + */ +#ifndef _ASM_POWERPC_HWTEST_H +#define _ASM_POWERPC_HWTEST_H + +int hwreg_present(volatile void *regp); + +#endif /* _ASM_POWERPC_HWTEST_H */ diff --git a/arch/powerpc/include/asm/kdump.h b/arch/powerpc/include/asm/kdump.h index 802644178f43..9c1ff917e3d9 100644 --- a/arch/powerpc/include/asm/kdump.h +++ b/arch/powerpc/include/asm/kdump.h @@ -6,42 +6,8 @@ #define KDUMP_KERNELBASE 0x2000000 -/* How many bytes to reserve at zero for kdump. The reserve limit should - * be greater or equal to the trampoline's end address. - * Reserve to the end of the FWNMI area, see head_64.S */ -#define KDUMP_RESERVE_LIMIT 0x10000 /* 64K */ - #ifdef CONFIG_CRASH_DUMP - -/* - * On PPC64 translation is disabled during trampoline setup, so we use - * physical addresses. Though on PPC32 translation is already enabled, - * so we can't do the same. Luckily create_trampoline() creates relative - * branches, so we can just add the PAGE_OFFSET and don't worry about it. - */ -#ifdef __powerpc64__ -#define KDUMP_TRAMPOLINE_START 0x0100 -#define KDUMP_TRAMPOLINE_END 0x3000 -#else -#define KDUMP_TRAMPOLINE_START (0x0100 + PAGE_OFFSET) -#define KDUMP_TRAMPOLINE_END (0x3000 + PAGE_OFFSET) -#endif /* __powerpc64__ */ - #define KDUMP_MIN_TCE_ENTRIES 2048 - #endif /* CONFIG_CRASH_DUMP */ -#ifndef __ASSEMBLER__ - -#if defined(CONFIG_CRASH_DUMP) && !defined(CONFIG_NONSTATIC_KERNEL) -extern void reserve_kdump_trampoline(void); -extern void setup_kdump_trampoline(void); -#else -/* !CRASH_DUMP || !NONSTATIC_KERNEL */ -static inline void reserve_kdump_trampoline(void) { ; } -static inline void setup_kdump_trampoline(void) { ; } -#endif - -#endif /* __ASSEMBLER__ */ - #endif /* __PPC64_KDUMP_H */ diff --git a/arch/powerpc/include/asm/nubus.h b/arch/powerpc/include/asm/nubus.h new file mode 100644 index 000000000000..d8ac6f608986 --- /dev/null +++ b/arch/powerpc/include/asm/nubus.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * NuBus slot access of the NuBus Power Macintosh. + * + * Copyright (C) 2026 René Rebe + */ +#ifndef _ASM_POWERPC_NUBUS_H +#define _ASM_POWERPC_NUBUS_H + +#include +#include + +#define nubus_readb(addr) in_8((const volatile u8 __iomem *)(addr)) +#define nubus_readw(addr) in_be16((const volatile u16 __iomem *)(addr)) +#define nubus_readl(addr) in_be32((const volatile u32 __iomem *)(addr)) + +#define nubus_writeb(val, addr) out_8((volatile u8 __iomem *)(addr), (val)) +#define nubus_writew(val, addr) out_be16((volatile u16 __iomem *)(addr), (val)) +#define nubus_writel(val, addr) out_be32((volatile u32 __iomem *)(addr), (val)) + +bool nubus_present(void); +void *nubus_map_slot(int slot); +void nubus_unmap_slot(void *addr); +int nubus_slot_irq(int slot); + +#define nubus_map_slot nubus_map_slot +#define SLOT2IRQ(slot) nubus_slot_irq(slot) + +#endif /* _ASM_POWERPC_NUBUS_H */ diff --git a/arch/powerpc/include/asm/page.h b/arch/powerpc/include/asm/page.h index 281f25e071a3..3d776e231690 100644 --- a/arch/powerpc/include/asm/page.h +++ b/arch/powerpc/include/asm/page.h @@ -4,6 +4,7 @@ /* * Copyright (C) 2001,2005 IBM Corporation. + * Copyright (C) 2026 René Rebe */ #ifndef __ASSEMBLER__ @@ -54,6 +55,7 @@ extern unsigned int hpage_shift; * * KERNELBASE, PAGE_OFFSET, and PHYSICAL_START are all configurable on * ppc32 and based on how they are set we determine MEMORY_START. + * On book3s/32 MEMORY_START is 0 and KERNELBASE follows PHYSICAL_START. * * For the linear mapping the following equation should be true: * KERNELBASE - PAGE_OFFSET = PHYSICAL_START - MEMORY_START @@ -70,9 +72,15 @@ extern unsigned int hpage_shift; * If you want to test if something's a kernel address, use is_kernel_addr(). */ -#define KERNELBASE ASM_CONST(CONFIG_KERNEL_START) #define PAGE_OFFSET ASM_CONST(CONFIG_PAGE_OFFSET) +#ifdef CONFIG_PPC_BOOK3S_32 +/* lowmem always starts at physical 0, the kernel may be linked above it */ +#define KERNELBASE (PAGE_OFFSET + ASM_CONST(CONFIG_PHYSICAL_START)) +#define LOAD_OFFSET PAGE_OFFSET +#else +#define KERNELBASE ASM_CONST(CONFIG_KERNEL_START) #define LOAD_OFFSET ASM_CONST((CONFIG_KERNEL_START-CONFIG_PHYSICAL_START)) +#endif #if defined(CONFIG_NONSTATIC_KERNEL) #ifndef __ASSEMBLER__ diff --git a/arch/powerpc/include/asm/task_size_32.h b/arch/powerpc/include/asm/task_size_32.h index 725ddbf06217..54ef4a2d0a25 100644 --- a/arch/powerpc/include/asm/task_size_32.h +++ b/arch/powerpc/include/asm/task_size_32.h @@ -4,7 +4,7 @@ #include -#if CONFIG_TASK_SIZE > CONFIG_KERNEL_START +#if defined(CONFIG_KERNEL_START) && CONFIG_TASK_SIZE > CONFIG_KERNEL_START #error User TASK_SIZE overlaps with KERNEL_START address #endif diff --git a/arch/powerpc/kernel/cpu_setup_6xx.S b/arch/powerpc/kernel/cpu_setup_6xx.S index ab3ca74e6730..112cffe53418 100644 --- a/arch/powerpc/kernel/cpu_setup_6xx.S +++ b/arch/powerpc/kernel/cpu_setup_6xx.S @@ -2,6 +2,7 @@ /* * This file contains low level CPU setup functions. * Copyright (C) 2003 Benjamin Herrenschmidt (benh@kernel.crashing.org) + * Copyright (C) 2026 René Rebe */ #include @@ -416,7 +417,7 @@ _GLOBAL(__restore_cpu_setup) mfcr r7 /* Get storage ptr */ - lis r5,(cpu_state_storage-KERNELBASE)@h + lis r5,(cpu_state_storage-PAGE_OFFSET)@h ori r5,r5,cpu_state_storage@l /* Restore HID0 */ diff --git a/arch/powerpc/kernel/crash_dump.c b/arch/powerpc/kernel/crash_dump.c index 103b6605dd68..4a31aef04e40 100644 --- a/arch/powerpc/kernel/crash_dump.c +++ b/arch/powerpc/kernel/crash_dump.c @@ -5,70 +5,19 @@ * Copyright (C) 2005, IBM Corp. * * Created by: Michael Ellerman + * + * Copyright (C) 2026 René Rebe */ -#undef DEBUG - #include #include #include #include -#include -#include #include #include #include -#include #include -#ifdef DEBUG -#include -#define DBG(fmt...) udbg_printf(fmt) -#else -#define DBG(fmt...) -#endif - -#ifndef CONFIG_NONSTATIC_KERNEL -void __init reserve_kdump_trampoline(void) -{ - memblock_reserve(0, KDUMP_RESERVE_LIMIT); -} - -static void __init create_trampoline(unsigned long addr) -{ - u32 *p = (u32 *)addr; - - /* The maximum range of a single instruction branch, is the current - * instruction's address + (32 MB - 4) bytes. For the trampoline we - * need to branch to current address + 32 MB. So we insert a nop at - * the trampoline address, then the next instruction (+ 4 bytes) - * does a branch to (32 MB - 4). The net effect is that when we - * branch to "addr" we jump to ("addr" + 32 MB). Although it requires - * two instructions it doesn't require any registers. - */ - patch_instruction(p, ppc_inst(PPC_RAW_NOP())); - patch_branch(p + 1, addr + PHYSICAL_START, 0); -} - -void __init setup_kdump_trampoline(void) -{ - unsigned long i; - - DBG(" -> setup_kdump_trampoline()\n"); - - for (i = KDUMP_TRAMPOLINE_START; i < KDUMP_TRAMPOLINE_END; i += 8) { - create_trampoline(i); - } - -#ifdef CONFIG_PPC_PSERIES - create_trampoline(__pa(system_reset_fwnmi) - PHYSICAL_START); - create_trampoline(__pa(machine_check_fwnmi) - PHYSICAL_START); -#endif /* CONFIG_PPC_PSERIES */ - - DBG(" <- setup_kdump_trampoline()\n"); -} -#endif /* CONFIG_NONSTATIC_KERNEL */ - ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn, size_t csize, unsigned long offset) { diff --git a/arch/powerpc/kernel/head_book3s_32.S b/arch/powerpc/kernel/head_book3s_32.S index e83a2ed39b78..5d785c606789 100644 --- a/arch/powerpc/kernel/head_book3s_32.S +++ b/arch/powerpc/kernel/head_book3s_32.S @@ -10,6 +10,7 @@ * rewritten by Paul Mackerras. * Copyright (C) 1996 Paul Mackerras. * MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net). + * Copyright (C) 2026 René Rebe * * This file contains the low-level support and setup for the * PowerPC platform, including trap and interrupt dispatch. @@ -136,14 +137,14 @@ __start: * Check for BootX signature when supporting PowerMac and branch to * appropriate trampoline if it's present */ -#ifdef CONFIG_PPC_PMAC +#ifdef CONFIG_PPC_BOOTX 1: lis r31,0x426f ori r31,r31,0x6f58 cmpw 0,r3,r31 bne 1f bl bootx_init trap -#endif /* CONFIG_PPC_PMAC */ +#endif /* CONFIG_PPC_BOOTX */ 1: mr r31,r3 /* save device tree ptr */ li r24,0 /* cpu # */ @@ -188,7 +189,7 @@ __after_mmu_off: /* - * We need to run with _start at physical address 0. + * We need to run with _start at physical address PHYSICAL_START. * On CHRP, we are loaded at 0x10000 since OF on CHRP uses * the exception vectors at 0 (and therefore this copy * overwrites OF's exception vectors with our own). @@ -197,9 +198,7 @@ __after_mmu_off: bl reloc_offset mr r26,r3 addis r4,r3,KERNELBASE@h /* current address of _start */ - lis r5,PHYSICAL_START@h - cmplw 0,r4,r5 /* already running at PHYSICAL_START? */ - bne relocate_kernel + b relocate_kernel /* * we now have the 1st 16M of ram mapped with the bats. * prep needs the mmu to be turned on here, but pmac already has it on. @@ -209,9 +208,6 @@ __after_mmu_off: * off, and in other cases, we now turn it off before changing BATs above. */ turn_on_mmu: -#ifdef CONFIG_PPC_BOOK3S_601 - bl move_fdt_601 -#endif mfmsr r0 ori r0,r0,MSR_DR|MSR_IR|MSR_RI mtspr SPRN_SRR1,r0 @@ -769,11 +765,17 @@ PerformanceMonitor: __HEAD /* - * This code is jumped to from the startup code to copy - * the kernel image to physical address PHYSICAL_START. + * This code is jumped to from the startup code to copy the kernel + * image to physical address PHYSICAL_START unless it already runs + * there, r4 = current address of _start. */ relocate_kernel: lis r3,PHYSICAL_START@h /* Destination base address */ + cmplw 0,r4,r3 + beq 5f +#if CONFIG_PHYSICAL_START != 0 + blt 6f /* loaded below PHYSICAL_START */ +#endif li r6,0 /* Destination offset */ li r5,0x4000 /* # bytes of memory to copy */ bl copy_and_flush /* copy the first 0x4000 bytes */ @@ -783,6 +785,13 @@ relocate_kernel: 4: lis r5,_end-KERNELBASE@h ori r5,r5,_end-KERNELBASE@l bl copy_and_flush /* copy the rest */ +5: +#ifdef CONFIG_PPC_BOOK3S_601 + bl move_fdt_601 +#endif +#if CONFIG_PHYSICAL_START != 0 + bl setup_vector_trampolines +#endif b turn_on_mmu #ifdef CONFIG_PPC_BOOK3S_601 @@ -817,6 +826,72 @@ SYM_FUNC_START_LOCAL(move_fdt_601) SYM_FUNC_END(move_fdt_601) #endif +#if CONFIG_PHYSICAL_START != 0 +/* Overlapping copy upwards: from the end down to the code below, then the rest from the copy */ +6: lis r5,_end-KERNELBASE@h + ori r5,r5,_end-KERNELBASE@l + li r6,relocate_kernel_low - _stext + bl copy_and_flush_down + addi r0,r3,relocate_kernel_low - _stext + mtctr r0 + bctr + + .balign L1_CACHE_BYTES +relocate_kernel_low: + li r5,relocate_kernel_low - _stext + li r6,0 + bl copy_and_flush_down + b 5b + +/* + * copy_and_flush downwards for a destination above the source, r5 = end and + * r6 = start offset, both cache line aligned; on exit r5 = r6. + */ +SYM_FUNC_START_LOCAL(copy_and_flush_down) +4: li r0,L1_CACHE_BYTES/4 + mtctr r0 +3: addi r5,r5,-4 + lwzx r0,r5,r4 + stwx r0,r5,r3 + bdnz 3b + dcbst r5,r3 /* write it to memory */ + sync + icbi r5,r3 /* flush the icache line */ + cmplw 0,r5,r6 + bgt 4b + sync + isync + blr +SYM_FUNC_END(copy_and_flush_down) + +/* + * Exceptions are taken at physical 0x100 - 0x3000, redirect them to the + * vectors at PHYSICAL_START. A nop and a branch per 8 bytes reach 32 MB. + */ +SYM_FUNC_START_LOCAL(setup_vector_trampolines) + lis r5,0x6000 /* nop */ + lis r6,PPC_RAW_BRANCH(PHYSICAL_START - 4)@h + ori r6,r6,PPC_RAW_BRANCH(PHYSICAL_START - 4)@l + li r3,0x100 - 8 + li r0,(0x3000 - 0x100) / 8 + mtctr r0 +1: stwu r5,8(r3) + stw r6,4(r3) + bdnz 1b + li r3,0x100 + li r0,(0x3000 - 0x100) / L1_CACHE_BYTES + mtctr r0 +2: dcbst 0,r3 + sync + icbi 0,r3 + addi r3,r3,L1_CACHE_BYTES + bdnz 2b + sync + isync + blr +SYM_FUNC_END(setup_vector_trampolines) +#endif + /* * Copy routine used to copy the kernel to start at physical address 0 * and flush and invalidate the caches as needed. @@ -875,10 +950,10 @@ __secondary_start: /* Copy some CPU settings from CPU 0 */ bl __restore_cpu_setup - lis r3,-KERNELBASE@h + lis r3,-PAGE_OFFSET@h mr r4,r24 bl call_setup_cpu /* Call setup_cpu for this CPU */ - lis r3,-KERNELBASE@h + lis r3,-PAGE_OFFSET@h bl init_idle_6xx /* get current's stack and current */ @@ -935,7 +1010,7 @@ SYM_FUNC_START_LOCAL(early_hash_table) TLBSYNC /* ... on all CPUs */ /* Load the SDR1 register (hash table base & size) */ lis r6, early_hash - PAGE_OFFSET@h - ori r6, r6, 3 /* 256kB table */ + ori r6, r6, (1 << (EARLY_HASH_SHIFT - 16)) - 1 /* HTABMASK */ mtspr SPRN_SDR1, r6 blr SYM_FUNC_END(early_hash_table) diff --git a/arch/powerpc/kernel/prom.c b/arch/powerpc/kernel/prom.c index d218c8cc1f73..324a990a1cbf 100644 --- a/arch/powerpc/kernel/prom.c +++ b/arch/powerpc/kernel/prom.c @@ -7,6 +7,8 @@ * * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. * {engebret|bergner}@us.ibm.com + * + * Copyright (C) 2026 René Rebe */ #undef DEBUG @@ -851,7 +853,6 @@ void __init early_init_devtree(void *params) #endif if (PHYSICAL_START > MEMORY_START) memblock_reserve(MEMORY_START, int_vector_size); - reserve_kdump_trampoline(); #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP) /* * If we fail to reserve memory for firmware-assisted dump then @@ -865,8 +866,12 @@ void __init early_init_devtree(void *params) if (memory_limit > memblock_phys_mem_size()) memory_limit = 0; - /* Align down to 16 MB which is large page size with hash page translation */ - memory_limit = ALIGN_DOWN(memory_limit ?: memblock_phys_mem_size(), SZ_16M); + /* + * Align down to 16 MB which is large page size with book3s/64 hash + * page translation, other MMUs map the RAM of any size + */ + memory_limit = ALIGN_DOWN(memory_limit ?: memblock_phys_mem_size(), + IS_ENABLED(CONFIG_PPC_BOOK3S_64) ? SZ_16M : PAGE_SIZE); memblock_enforce_memory_limit(memory_limit); #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_PPC_4K_PAGES) diff --git a/arch/powerpc/kernel/prom_init.c b/arch/powerpc/kernel/prom_init.c index e8e071024da5..621a3c85a4e5 100644 --- a/arch/powerpc/kernel/prom_init.c +++ b/arch/powerpc/kernel/prom_init.c @@ -7,6 +7,8 @@ * * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. * {engebret|bergner}@us.ibm.com + * + * Copyright (C) 2026 René Rebe */ #undef DEBUG_PROM @@ -508,7 +510,7 @@ static void __init prom_printf(const char *format, ...) va_list args; unsigned long v; long vs; - int n = 0; + int n; va_start(args, format); for (p = format; *p != 0; p = q) { @@ -527,6 +529,7 @@ static void __init prom_printf(const char *format, ...) ++q; if (*q == 0) break; + n = 0; while (*q == 'l') { ++q; ++n; @@ -1745,6 +1748,11 @@ static void __init prom_init_mem(void) } alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000); +#ifndef CONFIG_NONSTATIC_KERNEL + /* the kernel is copied to PHYSICAL_START later, stay above that copy */ + alloc_bottom = max(alloc_bottom, PAGE_ALIGN(PHYSICAL_START + 0x4000 + + (unsigned long)(_end - _stext))); +#endif /* * If prom_memory_limit is set we reduce the upper limits *except* for @@ -3201,6 +3209,44 @@ static void __init prom_check_initrd(unsigned long r3, unsigned long r4) #endif /* CONFIG_BLK_DEV_INITRD */ } +#if defined(CONFIG_BLK_DEV_INITRD) && !defined(CONFIG_NONSTATIC_KERNEL) +/* The kernel is copied to PHYSICAL_START after prom_init, move an initrd out of its way */ +static void __init prom_move_initrd(void) +{ + unsigned long kend = PHYSICAL_START + (unsigned long)(_end - _stext); + unsigned long size = prom_initrd_end - prom_initrd_start; + unsigned long base; + __be64 val; + int i; + + if (!prom_initrd_start || prom_initrd_start >= kend || + prom_initrd_end <= PHYSICAL_START) + return; + + base = alloc_up(size, PAGE_SIZE); + if (!base) + prom_panic("Can't move the initrd out of the kernel's way\n"); + prom_printf("Moving initrd from 0x%lx to 0x%lx\n", prom_initrd_start, base); + memcpy((void *)base, (void *)prom_initrd_start, size); + + for (i = 0; i < mem_reserve_cnt; i++) + if (be64_to_cpu(mem_reserve_map[i].base) == + ALIGN_DOWN(prom_initrd_start, PAGE_SIZE)) { + mem_reserve_map[i].base = cpu_to_be64(base); + mem_reserve_map[i].size = cpu_to_be64(PAGE_ALIGN(size)); + } + + prom_initrd_start = base; + prom_initrd_end = base + size; + val = cpu_to_be64(prom_initrd_start); + prom_setprop(prom.chosen, "/chosen", "linux,initrd-start", &val, sizeof(val)); + val = cpu_to_be64(prom_initrd_end); + prom_setprop(prom.chosen, "/chosen", "linux,initrd-end", &val, sizeof(val)); +} +#else +static inline void prom_move_initrd(void) { } +#endif + #ifdef CONFIG_PPC_SVM /* * Perform the Enter Secure Mode ultracall. @@ -3300,7 +3346,7 @@ unsigned long __init prom_init(unsigned long r3, unsigned long r4, of_platform = prom_find_machine_type(); prom_printf("Detected machine type: %x\n", of_platform); -#ifndef CONFIG_NONSTATIC_KERNEL +#if defined(CONFIG_CRASH_DUMP) && !defined(CONFIG_NONSTATIC_KERNEL) /* Bail if this is a kdump kernel. */ if (PHYSICAL_START > 0) prom_panic("Error: You can't boot a kdump kernel from OF!\n"); @@ -3335,6 +3381,7 @@ unsigned long __init prom_init(unsigned long r3, unsigned long r4, * Initialize memory management within prom_init */ prom_init_mem(); + prom_move_initrd(); /* * Determine which cpu is actually running right _now_ diff --git a/arch/powerpc/kernel/setup_32.c b/arch/powerpc/kernel/setup_32.c index 5a1bf501fbe1..91a71b3cd568 100644 --- a/arch/powerpc/kernel/setup_32.c +++ b/arch/powerpc/kernel/setup_32.c @@ -95,8 +95,6 @@ notrace void __init machine_init(u64 dt_ptr) early_init_devtree(__va(dt_ptr)); early_init_mmu(); - - setup_kdump_trampoline(); } /* Checks "l2cr=xxxx" command-line option */ diff --git a/arch/powerpc/kernel/setup_64.c b/arch/powerpc/kernel/setup_64.c index 8fd7cbf3bd04..d3fb7578489d 100644 --- a/arch/powerpc/kernel/setup_64.c +++ b/arch/powerpc/kernel/setup_64.c @@ -198,12 +198,6 @@ static void __init fixup_boot_paca(struct paca_struct *boot_paca) static void __init configure_exceptions(void) { - /* - * Setup the trampolines from the lowmem exception vectors - * to the kdump kernel when not using a relocatable kernel. - */ - setup_kdump_trampoline(); - /* Under a PAPR hypervisor, we need hypercalls */ if (firmware_has_feature(FW_FEATURE_SET_MODE)) { /* diff --git a/arch/powerpc/kernel/swsusp_32.S b/arch/powerpc/kernel/swsusp_32.S index ffb79326483c..34384a319104 100644 --- a/arch/powerpc/kernel/swsusp_32.S +++ b/arch/powerpc/kernel/swsusp_32.S @@ -208,7 +208,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) isync /* Load ptr the list of pages to copy in r3 */ - lis r11,(restore_pblist - KERNELBASE)@h + lis r11,(restore_pblist - PAGE_OFFSET)@h ori r11,r11,restore_pblist@l lwz r10,0(r11) diff --git a/arch/powerpc/mm/book3s32/hash_low.S b/arch/powerpc/mm/book3s32/hash_low.S index 3d1e49d24e19..f29a2ffaaa51 100644 --- a/arch/powerpc/mm/book3s32/hash_low.S +++ b/arch/powerpc/mm/book3s32/hash_low.S @@ -8,6 +8,7 @@ * Low-level exception handlers and MMU support * rewritten by Paul Mackerras. * Copyright (C) 1996 Paul Mackerras. + * Copyright (C) 2026 René Rebe * * This file contains low-level assembler routines for managing * the PowerPC MMU hash table. (PPC 8xx processors don't use a @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -294,7 +296,7 @@ _ASM_NOKPROBE_SYMBOL(add_hash_page) * of Hash_base and Hash_bits below are for the early hash table. */ Hash_base = early_hash -Hash_bits = 12 /* e.g. 256kB hash table */ +Hash_bits = EARLY_HASH_SHIFT - 6 /* 64 byte PTEGs */ Hash_msk = (((1 << Hash_bits) - 1) * 64) /* defines for the PTE format for 32-bit PPCs */ diff --git a/arch/powerpc/mm/book3s32/mmu.c b/arch/powerpc/mm/book3s32/mmu.c index e20997fa6a43..c0e14bca4256 100644 --- a/arch/powerpc/mm/book3s32/mmu.c +++ b/arch/powerpc/mm/book3s32/mmu.c @@ -32,7 +32,7 @@ #include -u8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0}; +u8 __initdata early_hash[1 << EARLY_HASH_SHIFT] __aligned(1 << EARLY_HASH_SHIFT) = {0}; static struct hash_pte __initdata *Hash = (struct hash_pte *)early_hash; static unsigned long __initdata Hash_size, Hash_mask; @@ -163,6 +163,7 @@ unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top) { unsigned long done; unsigned long border = (unsigned long)__srwx_boundary - PAGE_OFFSET; + unsigned long kstart = (unsigned long)_stext - PAGE_OFFSET; unsigned long size; /* the unified 601 BATs of the RAM mapping cover the text as well */ @@ -171,6 +172,12 @@ unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top) setibat(0, PAGE_OFFSET, 0, size, PAGE_KERNEL_X); } + /* RAM below a kernel linked above 0 is mapped by pages, BATs start at the kernel */ + for (; base < min(top, kstart); base += PAGE_SIZE) + map_kernel_page(PAGE_OFFSET + base, base, PAGE_KERNEL); + if (base >= top) + return base; + if (debug_pagealloc_enabled_or_kfence()) { pr_debug_once("Read-Write memory mapped without BATs\n"); if (base >= border) diff --git a/arch/powerpc/platforms/52xx/lite5200_sleep.S b/arch/powerpc/platforms/52xx/lite5200_sleep.S index 0ec2522ee4ad..2163de51d84d 100644 --- a/arch/powerpc/platforms/52xx/lite5200_sleep.S +++ b/arch/powerpc/platforms/52xx/lite5200_sleep.S @@ -48,7 +48,7 @@ lite5200_low_power: mr r8, r4 /* save MBAR va */ /* setup wakeup address for u-boot at physical location 0x0 */ - lis r3, CONFIG_KERNEL_START@h + lis r3, CONFIG_PAGE_OFFSET@h lis r4, lite5200_wakeup@h ori r4, r4, lite5200_wakeup@l sub r4, r4, r3 @@ -220,7 +220,7 @@ lite5200_wakeup: rfi mmu_on: /* kernel offset (r4 is still set from restore_registers) */ - addis r4, r4, CONFIG_KERNEL_START@h + addis r4, r4, CONFIG_PAGE_OFFSET@h /* restore MSR */ @@ -245,7 +245,7 @@ mmu_on: /* restore 0xf0 (BDI2000) */ - lis r3, CONFIG_KERNEL_START@h + lis r3, CONFIG_PAGE_OFFSET@h lwz r10, (0x1d*4)(r4) stw r10, 0xf0(r3) @@ -348,7 +348,7 @@ SYM_FUNC_START_LOCAL(restore_regs) ori r4, r4, registers@l /* MMU is not up yet */ - subis r4, r4, CONFIG_KERNEL_START@h + subis r4, r4, CONFIG_PAGE_OFFSET@h lwz r0, 0(r4) lwz r1, 0x4(r4) @@ -412,8 +412,8 @@ SYM_FUNC_END(restore_regs) * Do this by just reading lots of stuff into the cache. */ SYM_FUNC_START_LOCAL(flush_data_cache) - lis r3,CONFIG_KERNEL_START@h - ori r3,r3,CONFIG_KERNEL_START@l + lis r3,CONFIG_PAGE_OFFSET@h + ori r3,r3,CONFIG_PAGE_OFFSET@l li r4,NUM_CACHE_LINES mtctr r4 1: diff --git a/arch/powerpc/platforms/52xx/mpc52xx_pm.c b/arch/powerpc/platforms/52xx/mpc52xx_pm.c index f0c31ae15da5..bad9110f6e61 100644 --- a/arch/powerpc/platforms/52xx/mpc52xx_pm.c +++ b/arch/powerpc/platforms/52xx/mpc52xx_pm.c @@ -117,7 +117,7 @@ int mpc52xx_pm_enter(suspend_state_t state) u32 clk_enables; u32 msr, hid0; u32 intr_main_mask; - void __iomem * irq_0x500 = (void __iomem *)CONFIG_KERNEL_START + 0x500; + void __iomem *irq_0x500 = (void __iomem *)PAGE_OFFSET + 0x500; unsigned long irq_0x500_stop = (unsigned long)irq_0x500 + mpc52xx_ds_cached_size; char saved_0x500[0x600-0x500]; diff --git a/arch/powerpc/platforms/Kconfig b/arch/powerpc/platforms/Kconfig index 5495a82479d7..b4841076a30c 100644 --- a/arch/powerpc/platforms/Kconfig +++ b/arch/powerpc/platforms/Kconfig @@ -8,6 +8,7 @@ source "arch/powerpc/platforms/512x/Kconfig" source "arch/powerpc/platforms/52xx/Kconfig" source "arch/powerpc/platforms/powermac/Kconfig" source "arch/powerpc/platforms/prep/Kconfig" +source "arch/powerpc/platforms/nubus/Kconfig" source "arch/powerpc/platforms/pasemi/Kconfig" source "arch/powerpc/platforms/ps3/Kconfig" source "arch/powerpc/platforms/cell/Kconfig" diff --git a/arch/powerpc/platforms/Makefile b/arch/powerpc/platforms/Makefile index c21a92b87cf3..59240ea08839 100644 --- a/arch/powerpc/platforms/Makefile +++ b/arch/powerpc/platforms/Makefile @@ -2,9 +2,12 @@ obj-$(CONFIG_FSL_ULI1575) += fsl_uli1575.o -obj-$(CONFIG_PPC_PMAC) += powermac/ +ifneq ($(CONFIG_PPC_PMAC)$(CONFIG_PPC_BOOTX),) +obj-y += powermac/ +endif obj-$(CONFIG_PPC_CHRP) += chrp/ obj-$(CONFIG_PPC_PREP) += prep/ +obj-$(CONFIG_PPC_NUBUS_PMAC) += nubus/ obj-$(CONFIG_44x) += 44x/ obj-$(CONFIG_PPC_MPC512x) += 512x/ obj-$(CONFIG_PPC_MPC52xx) += 52xx/ diff --git a/arch/powerpc/platforms/nubus/Kconfig b/arch/powerpc/platforms/nubus/Kconfig new file mode 100644 index 000000000000..296c80783b26 --- /dev/null +++ b/arch/powerpc/platforms/nubus/Kconfig @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +config PPC_NUBUS_PMAC + bool "NuBus Power Macintosh" + depends on PPC_BOOK3S_32 && CPU_BIG_ENDIAN + select PPC_UDBG_SCC_NUBUS + select HARDIRQS_SW_RESEND + help + Support for the first Power Macintosh generation with NuBus slots: + the PDM family (6100, 7100, 8100, needs a 601 kernel), Performa + 5200/6200 and the 5300/1400/2300 PowerBooks. + +config PPC_UDBG_SCC_NUBUS + bool + +# BootX passes the boot infos to the kernel entry, also without PPC_PMAC +config PPC_BOOTX + def_bool PPC32 && (PPC_PMAC || PPC_NUBUS_PMAC) diff --git a/arch/powerpc/platforms/nubus/Makefile b/arch/powerpc/platforms/nubus/Makefile new file mode 100644 index 000000000000..caeaf74a87f7 --- /dev/null +++ b/arch/powerpc/platforms/nubus/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-y += setup.o amic.o amic_dma.o iosb_pic.o hwtest.o +obj-$(CONFIG_PPC_UDBG_SCC_NUBUS) += udbg_scc.o +obj-$(CONFIG_NUBUS) += bus.o diff --git a/arch/powerpc/platforms/nubus/amic.c b/arch/powerpc/platforms/nubus/amic.c new file mode 100644 index 000000000000..da4ed94b4177 --- /dev/null +++ b/arch/powerpc/platforms/nubus/amic.c @@ -0,0 +1,275 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Interrupt controller of the PDM (Power Macintosh 6100/7100/8100) AMIC + * I/O controller. + * + * The AMIC interrupt control register (ICR) collects VIA1, the pseudo VIA2 + * (with the NuBus slot and VBL sub-register), SCC, MACE, the DMA channels + * and NMI. Only the VIA2 sources can be masked in hardware. The hwirq + * numbering follows MkLinux (osfmk interrupt_pdm.c) and the nubus-pmac + * device trees: + * + * 0-5 ICR: VIA1, VIA2 (cascade), SCC, MACE, DMA (cascade), NMI + * 16-22 VIA2 IFR: SCSI-A DRQ, slot (cascade), SCSI-B DRQ, SCSI-A, + * sound, SWIM3, SCSI-B + * 24-30 VIA2 slot IFR (active low): 26-29 NuBus slots B-E, 30 VBL + * 32-38 DMA IFR: SCC-B RX/TX, SCC-A RX/TX, MACE RX/TX, SWIM3 + * 40-41 audio DMA: in, out + * + * VIA1 is owned by the Cuda driver, which masks its sources itself. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "nubus.h" + +#define ICR 0x0 +#define DMA_IFR 0x8 +#define DMA_AUDIO 0xa +#define ICR_ACK 0x80 +#define ICR_LATCH 0x40 +#define ICR_VIA2 0x02 +#define ICR_DMA 0x10 + +#define VIA2_SIFR 0x02 +#define VIA2_IFR 0x03 +#define VIA2_SIER 0x12 +#define VIA2_IER 0x13 +#define VIA_SET 0x80 +#define VIA2_SLOT 0x02 +#define SLOT_VBL 0x40 + +#define HW_ICR 0 +#define HW_VIA2 16 +#define HW_SLOT 24 +#define HW_DMA 32 +#define HW_AUDIO 40 +#define AMIC_NR_IRQS 48 + +static void __iomem *amic_icr; +static void __iomem *amic_via2; +static struct irq_domain *amic_domain; +static DEFINE_RAW_SPINLOCK(amic_lock); +/* software enable masks of the sources without a hardware mask */ +static u8 amic_icr_en, amic_dma_en, amic_audio_en; +/* + * The ICR runs in level mode as in MkLinux. While a disabled source without + * a hardware mask is asserted, it is switched to latched mode (interrupt on + * a change of the sources, acknowledge with the mode bit set; MAME) so that + * this source does not interrupt continuously. + */ +static bool amic_latched; +static struct irq_work amic_rescan_work; + +static u8 amic_icr_mode(void) +{ + return ICR_ACK | (amic_latched ? ICR_LATCH : 0); +} + +static bool amic_masked_pending(u8 icr) +{ + if (icr & 0x3f & ~amic_icr_en & ~(ICR_VIA2 | ICR_DMA)) + return true; + if (!(icr & ICR_DMA)) + return false; + return (in_8(amic_icr + DMA_AUDIO) & 3 & ~amic_audio_en) || + (in_8(amic_icr + DMA_IFR) & 0x7f & ~amic_dma_en); +} + +static void amic_mask(struct irq_data *d) +{ + unsigned int hw = irqd_to_hwirq(d); + unsigned long flags; + + raw_spin_lock_irqsave(&amic_lock, flags); + if (hw < HW_VIA2) + amic_icr_en &= ~BIT(hw - HW_ICR); + else if (hw < HW_SLOT) + out_8(amic_via2 + VIA2_IER, BIT(hw - HW_VIA2)); + else if (hw < HW_DMA) + out_8(amic_via2 + VIA2_SIER, BIT(hw - HW_SLOT)); + else if (hw < HW_AUDIO) + amic_dma_en &= ~BIT(hw - HW_DMA); + else + amic_audio_en &= ~BIT(hw - HW_AUDIO); + raw_spin_unlock_irqrestore(&amic_lock, flags); +} + +static void amic_unmask(struct irq_data *d) +{ + unsigned int hw = irqd_to_hwirq(d); + unsigned long flags; + + raw_spin_lock_irqsave(&amic_lock, flags); + if (hw >= HW_VIA2 && hw < HW_SLOT) { + out_8(amic_via2 + VIA2_IER, VIA_SET | BIT(hw - HW_VIA2)); + } else if (hw >= HW_SLOT && hw < HW_DMA) { + out_8(amic_via2 + VIA2_SIER, VIA_SET | BIT(hw - HW_SLOT)); + out_8(amic_via2 + VIA2_IER, VIA_SET | VIA2_SLOT); + } else { + if (hw < HW_VIA2) + amic_icr_en |= BIT(hw - HW_ICR); + else if (hw < HW_AUDIO) + amic_dma_en |= BIT(hw - HW_DMA); + else + amic_audio_en |= BIT(hw - HW_AUDIO); + /* back to level mode once no disabled source is asserted any more */ + if (amic_latched && !amic_masked_pending(in_8(amic_icr + ICR))) { + amic_latched = false; + out_8(amic_icr + ICR, amic_icr_mode()); + irq_work_queue(&amic_rescan_work); + } + } + raw_spin_unlock_irqrestore(&amic_lock, flags); +} + +static void amic_eoi(struct irq_data *d) +{ + unsigned int hw = irqd_to_hwirq(d); + + /* only the VBL flag of the slot register is writable */ + if (hw == HW_SLOT + 6) + out_8(amic_via2 + VIA2_SIFR, SLOT_VBL); + out_8(amic_icr + ICR, amic_icr_mode()); + /* latched mode does not interrupt again for sources still asserted */ + if (amic_latched) + irq_work_queue(&amic_rescan_work); +} + +static struct irq_chip amic_chip = { + .name = "AMIC", + .irq_mask = amic_mask, + .irq_unmask = amic_unmask, + .irq_eoi = amic_eoi, +}; + +static bool amic_valid_hwirq(irq_hw_number_t hw) +{ + return hw <= 5 || (hw >= HW_VIA2 && hw <= HW_VIA2 + 6) || + (hw >= HW_SLOT + 2 && hw <= HW_SLOT + 6) || + (hw >= HW_DMA && hw <= HW_DMA + 6) || + hw == HW_AUDIO || hw == HW_AUDIO + 1; +} + +static int amic_host_map(struct irq_domain *h, unsigned int virq, + irq_hw_number_t hw) +{ + /* the cascades are decoded in amic_get_irq() */ + if (!amic_valid_hwirq(hw) || hw == 1 || hw == 4 || + hw == HW_VIA2 + 1) + return -EINVAL; + + irq_set_status_flags(virq, IRQ_LEVEL); + irq_set_chip_and_handler(virq, &amic_chip, handle_fasteoi_irq); + return 0; +} + +static const struct irq_domain_ops amic_host_ops = { + .map = amic_host_map, + .xlate = irq_domain_xlate_onecell, +}; + +static unsigned int amic_find(irq_hw_number_t base, u8 bits) +{ + return irq_find_mapping(amic_domain, base + __ffs(bits)); +} + +static unsigned int amic_decode(u8 icr) +{ + u8 bits; + + if (icr & ICR_VIA2) { + bits = in_8(amic_via2 + VIA2_IFR) & in_8(amic_via2 + VIA2_IER); + if (bits & VIA2_SLOT) { + u8 slot = ~in_8(amic_via2 + VIA2_SIFR) & + in_8(amic_via2 + VIA2_SIER) & 0x7c; + + if (slot) + return amic_find(HW_SLOT, slot); + } + bits &= 0x7f & ~VIA2_SLOT; + if (bits) + return amic_find(HW_VIA2, bits); + } + + if (icr & ICR_DMA) { + bits = in_8(amic_icr + DMA_AUDIO) & amic_audio_en; + if (bits) + return amic_find(HW_AUDIO, bits); + bits = in_8(amic_icr + DMA_IFR) & amic_dma_en; + if (bits) + return amic_find(HW_DMA, bits); + } + + bits = icr & amic_icr_en & 0x3f; + if (bits) + return amic_find(HW_ICR, bits); + return 0; +} + +unsigned int amic_get_irq(void) +{ + u8 icr = in_8(amic_icr + ICR); + unsigned int virq; + + if (amic_latched) + out_8(amic_icr + ICR, amic_icr_mode()); + virq = amic_decode(icr); + if (virq) + return virq; + + if (!amic_latched && amic_masked_pending(icr)) + amic_latched = true; + out_8(amic_icr + ICR, amic_icr_mode()); + return 0; +} + +static void amic_rescan(struct irq_work *work) +{ + unsigned long flags; + unsigned int virq; + + local_irq_save(flags); + while ((virq = amic_decode(in_8(amic_icr + ICR)))) + generic_handle_irq(virq); + local_irq_restore(flags); +} + +int __init amic_pic_init(struct device_node *np) +{ + amic_icr = of_iomap(np, 0); + amic_via2 = of_iomap(np, 1); + if (!amic_icr || !amic_via2) { + pr_err("AMIC: cannot map %pOF\n", np); + return -ENOMEM; + } + + /* level mode, acknowledge; mask and clear all VIA2 sources */ + out_8(amic_icr + ICR, ICR_ACK); + out_8(amic_via2 + VIA2_IER, 0x7f); + out_8(amic_via2 + VIA2_IFR, 0x7f); + out_8(amic_via2 + VIA2_SIER, 0x7f); + out_8(amic_via2 + VIA2_SIFR, 0x7f); + + init_irq_work(&amic_rescan_work, amic_rescan); + amic_domain = irq_domain_create_linear(of_fwnode_handle(np), + AMIC_NR_IRQS, &amic_host_ops, + NULL); + if (!amic_domain) + return -ENOMEM; + irq_set_default_domain(amic_domain); + + pr_info("AMIC interrupt controller at %pOF\n", np); + return 0; +} diff --git a/arch/powerpc/platforms/nubus/amic_dma.c b/arch/powerpc/platforms/nubus/amic_dma.c new file mode 100644 index 000000000000..1dd51bb16919 --- /dev/null +++ b/arch/powerpc/platforms/nubus/amic_dma.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * DMA buffer area of the PDM AMIC. + * + * The AMIC DMA base register (0x50f31000) selects one 256 KB, 256 KB + * aligned area of physical memory. Ethernet, sound, floppy and SCC DMA + * use fixed offsets inside it (MkLinux powermac_pdm.h and enet_dma.h, + * MAME amic_enet.cpp and macpdm.cpp). The area is allocated once at boot + * and handed out per channel. AMIC DMA is not coherent with the 601 + * cache (MkLinux maps the area uncached and copies with bcopy_nc), so the + * CPU accesses it through an uncached mapping only. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "nubus.h" + +#define AMIC_DMA_SIZE SZ_256K + +static const struct { + u32 offset; + u32 size; +} amic_dma_layout[AMIC_DMA_NR_AREAS] = { + [AMIC_DMA_ENET_RX] = { 0x00000, 0xc000 }, + [AMIC_DMA_SOUND_IN] = { 0x0c000, 0x4000 }, + [AMIC_DMA_SOUND_OUT] = { 0x10000, 0x4000 }, + [AMIC_DMA_ENET_TX] = { 0x14000, 0x1000 }, + /* MkLinux marks the floppy offset as a guess, the pointer stays below 0x20000 */ + [AMIC_DMA_FLOPPY] = { 0x15000, 0xb000 }, + [AMIC_DMA_SCC_B_TX] = { 0x20000, 0x2000 }, + [AMIC_DMA_SCC_A_RX] = { 0x22000, 0x2000 }, + [AMIC_DMA_SCC_A_TX] = { 0x24000, 0x2000 }, + [AMIC_DMA_SCC_B_RX] = { 0x26000, 0x2000 }, +}; + +static phys_addr_t amic_dma_phys; +static void __iomem *amic_dma_virt; +static unsigned long amic_dma_busy; + +void __init amic_dma_reserve(void) +{ + struct device_node *np; + void *va; + + np = of_find_compatible_node(NULL, NULL, "apple,amic-dma"); + if (!np) + return; + of_node_put(np); + + va = memblock_alloc(AMIC_DMA_SIZE, AMIC_DMA_SIZE); + if (!va) { + pr_err("AMIC DMA: cannot reserve %u KB\n", AMIC_DMA_SIZE / SZ_1K); + return; + } + amic_dma_phys = __pa(va); +} + +static int __init amic_dma_init(void) +{ + unsigned int i, npages = AMIC_DMA_SIZE >> PAGE_SHIFT; + struct device_node *np; + struct page **pages; + void __iomem *base; + void *va; + + if (!amic_dma_phys) + return 0; + + np = of_find_compatible_node(NULL, NULL, "apple,amic-dma"); + base = of_iomap(np, 0); + of_node_put(np); + if (!base) + return -ENOMEM; + + pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); + if (!pages) { + iounmap(base); + return -ENOMEM; + } + for (i = 0; i < npages; i++) + pages[i] = pfn_to_page(PHYS_PFN(amic_dma_phys) + i); + /* write back the cached alias (memblock zeroed it) before DMA */ + flush_dcache_range((unsigned long)__va(amic_dma_phys), + (unsigned long)__va(amic_dma_phys) + AMIC_DMA_SIZE); + va = vmap(pages, npages, VM_MAP, pgprot_noncached(PAGE_KERNEL)); + kfree(pages); + if (!va) { + iounmap(base); + return -ENOMEM; + } + amic_dma_virt = (void __force __iomem *)va; + + /* byte wise, as MkLinux programs it */ + for (i = 0; i < 4; i++) + out_8(base + i, (u32)amic_dma_phys >> (24 - 8 * i)); + iounmap(base); + + pr_info("AMIC DMA: buffer area at 0x%08llx\n", + (unsigned long long)amic_dma_phys); + return 0; +} +machine_arch_initcall(nubus_pmac, amic_dma_init); + +int amic_dma_request(enum amic_dma_area area, struct amic_dma_buf *buf) +{ + if (area >= AMIC_DMA_NR_AREAS) + return -EINVAL; + if (!amic_dma_virt) + return -ENODEV; + if (test_and_set_bit(area, &amic_dma_busy)) + return -EBUSY; + + buf->cpu = amic_dma_virt + amic_dma_layout[area].offset; + buf->dma = amic_dma_phys + amic_dma_layout[area].offset; + buf->size = amic_dma_layout[area].size; + return 0; +} +EXPORT_SYMBOL_GPL(amic_dma_request); + +void amic_dma_release(enum amic_dma_area area) +{ + if (area < AMIC_DMA_NR_AREAS) + clear_bit(area, &amic_dma_busy); +} +EXPORT_SYMBOL_GPL(amic_dma_release); + +static void amic_dma_devm_release(void *data) +{ + amic_dma_release((enum amic_dma_area)(unsigned long)data); +} + +int devm_amic_dma_request(struct device *dev, enum amic_dma_area area, + struct amic_dma_buf *buf) +{ + int ret = amic_dma_request(area, buf); + + if (ret) + return ret; + return devm_add_action_or_reset(dev, amic_dma_devm_release, + (void *)(unsigned long)area); +} +EXPORT_SYMBOL_GPL(devm_amic_dma_request); diff --git a/arch/powerpc/platforms/nubus/bus.c b/arch/powerpc/platforms/nubus/bus.c new file mode 100644 index 000000000000..102235eecf9a --- /dev/null +++ b/arch/powerpc/platforms/nubus/bus.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NuBus slots of the NuBus Power Macintosh. + * + * The slot space is not accessible at its NuBus address 0xFs000000 as on + * m68k. The slots are the children of the "apple,nubus" device tree node, + * addressed by their NuBus slot space address, with their interrupt; each + * is ioremapped for probing. Probing an empty slot raises a machine check. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include + + +#include "nubus.h" + +#define NUBUS_SLOT_SPACE(slot) (0xF0000000 | ((slot) << 24)) + +bool nubus_present(void) +{ + struct device_node *np; + bool present; + + np = of_find_compatible_node(NULL, NULL, "apple,nubus"); + present = np && of_device_is_available(np); + of_node_put(np); + return present; +} + +static struct device_node *nubus_slot_node(int slot) +{ + struct device_node *bus, *np; + u32 addr; + + bus = of_find_compatible_node(NULL, NULL, "apple,nubus"); + if (!bus) + return NULL; + + for_each_child_of_node(bus, np) + if (!of_property_read_u32_index(np, "reg", 0, &addr) && + addr == NUBUS_SLOT_SPACE(slot)) + break; + + of_node_put(bus); + return np; +} + +void *nubus_map_slot(int slot) +{ + struct device_node *np = nubus_slot_node(slot); + void __iomem *addr = NULL; + struct resource r; + + if (!np) + return NULL; + + if (of_address_to_resource(np, 0, &r) || + resource_size(&r) != NUBUS_SLOT_SIZE) + pr_err("%pOF: invalid NuBus slot space\n", np); + else + addr = ioremap(r.start, NUBUS_SLOT_SIZE); + + of_node_put(np); + return (void __force *)addr; +} + +void nubus_unmap_slot(void *addr) +{ + iounmap((void __iomem __force *)addr); +} + +int nubus_slot_irq(int slot) +{ + struct device_node *np = nubus_slot_node(slot); + unsigned int irq; + + if (!np) + return 0; + + irq = irq_of_parse_and_map(np, 0); + of_node_put(np); + return irq; +} +EXPORT_SYMBOL(nubus_slot_irq); diff --git a/arch/powerpc/platforms/nubus/hwtest.S b/arch/powerpc/platforms/nubus/hwtest.S new file mode 100644 index 000000000000..55e485170539 --- /dev/null +++ b/arch/powerpc/platforms/nubus/hwtest.S @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Probe a byte register that raises a machine check (TEA) when absent, like + * an empty NuBus slot. The load is followed by the twi/isync sequence of + * the recoverable I/O accessors, so the machine check is taken inside the + * exception table covered range and recovered by hwreg_machine_check() or + * the generic check_io_access(). + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include + +_GLOBAL(hwreg_present) + sync +0: lbz r4,0(r3) +1: twi 0,r4,0 +2: isync +3: nop + li r3,1 + blr +4: li r3,0 + blr + EX_TABLE(0b, 4b) + EX_TABLE(1b, 4b) + EX_TABLE(2b, 4b) + EX_TABLE(3b, 4b) + .globl hwreg_present_end +hwreg_present_end: +EXPORT_SYMBOL(hwreg_present) diff --git a/arch/powerpc/platforms/nubus/iosb_pic.c b/arch/powerpc/platforms/nubus/iosb_pic.c new file mode 100644 index 000000000000..0c2e58a4e728 --- /dev/null +++ b/arch/powerpc/platforms/nubus/iosb_pic.c @@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Interrupt controller of the Performa 5200/6200 (Capella bridge, PrimeTime + * I/O) and the M2 PowerBooks 5300/1400/2300/500 (Whitney I/O). + * + * Both keep the 68k Macintosh interrupt structure: the sources raise a 68k + * interrupt priority level (1 VIA1, 2 VIA2, 4 SCC, 7 NMI), which the Capella + * resp. the M2 interrupt control register (ICR) turn into the PowerPC + * external interrupt. The CPU interrupt is latched and acknowledged at the + * top level, so all pending sources are dispatched in one pass. VIA2 is a + * pseudo VIA with the 6522 register stride; the NuBus/slot interrupts are + * its port A (active low), the M2 Baboon IDE/media bay flags cascade on + * slot C. References: MkLinux osfmk interrupt_performa.c/interrupt_m2.c, + * the 2001 nubus-pmac M2 driver (Takashi Oe), MAME capella/iosb/pseudovia. + * + * hwirq numbers: + * 1, 4, 7 IPL 1 (VIA1 as a whole, for Cuda), 4 (SCC), 7 (NMI) + * 8-14 VIA1 IFR bits (PMU: 10 shift register, 12 CB1) + * 16-22 VIA2 IFR bits: 16 SCSI DRQ, 17 slot (cascade), 19 SCSI, + * 20 sound, 21 floppy, 22 SCSI 1 + * 24-30 slots: Performa 28 ATA, 29 slot E, 30 VBL; M2 24 PCMCIA, + * 25 CSC, 26 modem, 27 Baboon (cascade), 29 internal expansion + * 32-34 Baboon: 32 media bay IDE, 33 internal IDE, 34 media bay; it + * cascades on slot 27 or the slot bit given by apple,baboon-slot + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "nubus.h" + +#define VIA_IFR 0x1a00 +#define VIA_IER 0x1c00 +#define VIA_ANH 0x1e00 +#define VIA_SET 0x80 +#define VIA_ANY 0x80 +#define VIA2_SLOT 0x02 + +#define CAPELLA_ACK 0x1f +#define CAPELLA_IPL 0x27 +#define M2_PENDING 0x80 +#define M2_ACK 0x80 + +#define HW_LEVEL 0 +#define HW_VIA1 8 +#define HW_VIA2 16 +#define HW_SLOT 24 +#define HW_BABOON 32 +#define HW_CASCADE (IOSB_NR_IRQS - 1) +#define IOSB_NR_IRQS 40 +#define SLOT_BABOON 3 /* 5300, 1400; the Duo 2300 uses slot bit 0 */ + +struct iosb_pic { + void __iomem *top; + void __iomem *via1; + void __iomem *via2; + void __iomem *baboon; + bool capella; + u32 baboon_slot; + struct irq_domain *domain; + unsigned int cascade_virq; + /* software enables of the sources without a hardware mask */ + u8 level_en, slot_en, baboon_en; +}; + +static struct iosb_pic iosb; +static DEFINE_RAW_SPINLOCK(iosb_lock); +static struct irq_work iosb_rescan_work; + +static u8 *iosb_soft_mask(unsigned int hw, u8 *bit) +{ + if (hw < HW_VIA1) { + *bit = BIT(hw - HW_LEVEL); + return &iosb.level_en; + } + if (hw >= HW_BABOON) { + *bit = BIT(hw - HW_BABOON); + return &iosb.baboon_en; + } + if (hw >= HW_SLOT) { + *bit = BIT(hw - HW_SLOT); + return &iosb.slot_en; + } + return NULL; +} + +static u8 iosb_level(void); + +/* live state of a source without a hardware mask */ +static bool iosb_soft_asserted(unsigned int hw) +{ + if (hw < HW_VIA1) + return hw == 1 ? in_8(iosb.via1 + VIA_IFR) & VIA_ANY : + iosb_level() == hw; + if (hw >= HW_BABOON) + return in_8(iosb.baboon) & BIT(hw - HW_BABOON); + return ~in_8(iosb.via2 + VIA_ANH) & BIT(hw - HW_SLOT); +} + +static void iosb_set(struct irq_data *d, bool on) +{ + unsigned int hw = irqd_to_hwirq(d); + unsigned long flags; + u8 *en, bit; + + raw_spin_lock_irqsave(&iosb_lock, flags); + en = iosb_soft_mask(hw, &bit); + if (en) { + *en = on ? *en | bit : *en & ~bit; + /* + * A level source asserted while masked does not interrupt again + * (the summary flag is consumed, level IRQs are not resent) + */ + if (on && iosb_soft_asserted(hw)) + irq_work_queue(&iosb_rescan_work); + } else if (hw < HW_VIA2) + out_8(iosb.via1 + VIA_IER, (on ? VIA_SET : 0) | BIT(hw - HW_VIA1)); + else + out_8(iosb.via2 + VIA_IER, (on ? VIA_SET : 0) | BIT(hw - HW_VIA2)); + raw_spin_unlock_irqrestore(&iosb_lock, flags); +} + +static void iosb_mask(struct irq_data *d) +{ + iosb_set(d, false); +} + +static void iosb_unmask(struct irq_data *d) +{ + iosb_set(d, true); +} + +static void iosb_ack(struct irq_data *d) +{ + unsigned int hw = irqd_to_hwirq(d); + + /* clear the latched flags (CB1, SR, sound); level sources reassert */ + if (hw >= HW_VIA1 && hw < HW_VIA2) + out_8(iosb.via1 + VIA_IFR, BIT(hw - HW_VIA1)); + else if (hw >= HW_VIA2 && hw < HW_SLOT) + out_8(iosb.via2 + VIA_IFR, BIT(hw - HW_VIA2)); + else if (hw >= HW_BABOON) + /* latched, cleared by writing 0 like the nubus-pmac nbpmacide */ + out_8(iosb.baboon, in_8(iosb.baboon) & ~BIT(hw - HW_BABOON)); +} + +static struct irq_chip iosb_chip = { + .name = "IOSB", + .irq_mask = iosb_mask, + .irq_unmask = iosb_unmask, + .irq_ack = iosb_ack, +}; + +static bool iosb_valid_hwirq(irq_hw_number_t hw) +{ + if (hw == 1 || hw == 4 || hw == 7) + return true; + if ((hw >= HW_VIA1 && hw <= HW_VIA1 + 6) || + (hw >= HW_VIA2 && hw <= HW_VIA2 + 6 && hw != HW_VIA2 + 1)) + return true; + if (hw >= HW_SLOT && hw <= HW_SLOT + 6) + return !(iosb.baboon && hw == HW_SLOT + iosb.baboon_slot); + return iosb.baboon && hw >= HW_BABOON && hw <= HW_BABOON + 2; +} + +static int iosb_host_map(struct irq_domain *h, unsigned int virq, + irq_hw_number_t hw) +{ + if (hw == HW_CASCADE) + return 0; + if (!iosb_valid_hwirq(hw)) + return -EINVAL; + + irq_set_status_flags(virq, IRQ_LEVEL); + irq_set_chip_and_handler(virq, &iosb_chip, handle_level_irq); + return 0; +} + +static const struct irq_domain_ops iosb_host_ops = { + .map = iosb_host_map, + .xlate = irq_domain_xlate_onecell, +}; + +static u8 iosb_level(void) +{ + if (iosb.capella) + return ~in_8(iosb.top + CAPELLA_IPL) & 7; + return in_8(iosb.top) & 7; +} + +/* next pending enabled source in 68k priority order, or -1 */ +static int iosb_next(void) +{ + u8 level = iosb_level(), bits, slots; + + if (level == 7 && (iosb.level_en & BIT(7))) + return 7; + if (level == 4 && (iosb.level_en & BIT(4))) + return 4; + + bits = in_8(iosb.via2 + VIA_IFR) & in_8(iosb.via2 + VIA_IER) & 0x7f; + /* + * The slot flag latches a slot line becoming active (6522 CA1): clear + * it before the scan, which also finds lines active since earlier. + */ + if (bits & VIA2_SLOT) + out_8(iosb.via2 + VIA_IFR, VIA2_SLOT); + slots = ~in_8(iosb.via2 + VIA_ANH) & 0x7f; + if (iosb.baboon && (slots & BIT(iosb.baboon_slot))) { + u8 flags = in_8(iosb.baboon) & 7 & iosb.baboon_en; + + if (flags) + return HW_BABOON + __ffs(flags); + } + slots &= iosb.slot_en; + if (slots) + return HW_SLOT + __ffs(slots); + bits &= ~VIA2_SLOT; + if (bits) + return HW_VIA2 + __ffs(bits); + + /* VIA1 handed over as a whole (Cuda) or decoded bit by bit (PMU) */ + if (irq_find_mapping(iosb.domain, 1)) { + if (!(iosb.level_en & BIT(1))) + return -1; + return (in_8(iosb.via1 + VIA_IFR) & VIA_ANY) ? 1 : -1; + } + bits = in_8(iosb.via1 + VIA_IFR) & in_8(iosb.via1 + VIA_IER) & 0x7f; + if (bits) + return HW_VIA1 + __ffs(bits); + + return -1; +} + +static void iosb_dispatch(void) +{ + int hw; + + /* an unmapped source cannot be serviced, leave it pending */ + while ((hw = iosb_next()) >= 0) + if (generic_handle_domain_irq(iosb.domain, hw)) + break; +} + +static void iosb_cascade(struct irq_desc *desc) +{ + iosb_dispatch(); +} + +static void iosb_rescan(struct irq_work *work) +{ + unsigned long flags; + + local_irq_save(flags); + iosb_dispatch(); + local_irq_restore(flags); +} + +unsigned int iosb_get_irq(void) +{ + /* acknowledge first: any later change of the sources interrupts again */ + if (iosb.capella) { + out_8(iosb.top + CAPELLA_ACK, 0); + (void)in_8(iosb.top + CAPELLA_ACK); + } else if (in_8(iosb.top) & M2_PENDING) { + out_8(iosb.top, M2_ACK); + } + return iosb.cascade_virq; +} + +int __init iosb_pic_init(struct device_node *np) +{ + iosb.capella = of_device_is_compatible(np, "apple,capella-pic"); + iosb.top = of_iomap(np, 0); + iosb.via1 = of_iomap(np, 1); + iosb.via2 = of_iomap(np, 2); + /* M2 with Baboon (5300, 1400, 2300) */ + iosb.baboon = of_iomap(np, 3); + iosb.baboon_slot = SLOT_BABOON; + of_property_read_u32(np, "apple,baboon-slot", &iosb.baboon_slot); + if (iosb.baboon_slot > 6) { + pr_err("IOSB PIC: invalid Baboon slot %u\n", iosb.baboon_slot); + return -EINVAL; + } + if (!iosb.top || !iosb.via1 || !iosb.via2) { + pr_err("IOSB PIC: cannot map %pOF\n", np); + return -ENOMEM; + } + + out_8(iosb.via1 + VIA_IER, 0x7f); + out_8(iosb.via1 + VIA_IFR, 0x7f); + out_8(iosb.via2 + VIA_IER, 0x7f); + out_8(iosb.via2 + VIA_IFR, 0x7f); + /* the slot interrupts have no mask register, they are always routed */ + out_8(iosb.via2 + VIA_IER, VIA_SET | VIA2_SLOT); + + init_irq_work(&iosb_rescan_work, iosb_rescan); + iosb.domain = irq_domain_create_linear(of_fwnode_handle(np), + IOSB_NR_IRQS, &iosb_host_ops, + NULL); + if (!iosb.domain) + return -ENOMEM; + irq_set_default_domain(iosb.domain); + + iosb.cascade_virq = irq_create_mapping(iosb.domain, HW_CASCADE); + if (!iosb.cascade_virq) + return -ENOMEM; + irq_set_chip_and_handler_name(iosb.cascade_virq, &dummy_irq_chip, + iosb_cascade, "cascade"); + + iosb_get_irq(); + pr_info("%s interrupt controller at %pOF\n", + iosb.capella ? "Capella" : "M2", np); + return 0; +} diff --git a/arch/powerpc/platforms/nubus/nubus.h b/arch/powerpc/platforms/nubus/nubus.h new file mode 100644 index 000000000000..e029a99524b5 --- /dev/null +++ b/arch/powerpc/platforms/nubus/nubus.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * NuBus Power Macintosh internal declarations. + * + * Copyright (C) 2026 René Rebe + */ +#ifndef _NUBUS_PMAC_H +#define _NUBUS_PMAC_H + +struct device_node; + +void udbg_scc_nubus_init(void); + +int amic_pic_init(struct device_node *np); +unsigned int amic_get_irq(void); +int iosb_pic_init(struct device_node *np); +unsigned int iosb_get_irq(void); +void amic_dma_reserve(void); + +#endif /* _NUBUS_PMAC_H */ diff --git a/arch/powerpc/platforms/nubus/setup.c b/arch/powerpc/platforms/nubus/setup.c new file mode 100644 index 000000000000..4f7420335b48 --- /dev/null +++ b/arch/powerpc/platforms/nubus/setup.c @@ -0,0 +1,349 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NuBus Power Macintosh setup. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "nubus.h" + +#ifndef CONFIG_PPC_PMAC +sys_ctrler_t sys_ctrler = SYS_CTRLER_UNKNOWN; +EXPORT_SYMBOL(sys_ctrler); +#endif + +/* the Z85C30 console ports, set up before the serial driver probes */ +static struct resource scc_a_rsrcs[2], scc_b_rsrcs[2]; + +struct platform_device scc_a_pdev = { + .name = "scc", + .id = 0, +}; +EXPORT_SYMBOL(scc_a_pdev); + +struct platform_device scc_b_pdev = { + .name = "scc", + .id = 1, +}; +EXPORT_SYMBOL(scc_b_pdev); + +static void __init nubus_pmac_scc_init(void) +{ + struct device_node *np; + struct resource r; + int irq; + + np = of_find_compatible_node(NULL, NULL, "apple,nubus-escc"); + if (!np) + return; + irq = irq_of_parse_and_map(np, 0); + if (of_address_to_resource(np, 0, &r) || !irq) { + of_node_put(np); + return; + } + of_node_put(np); + + /* channel B control at +0, channel A control at +2 */ + scc_a_rsrcs[0] = DEFINE_RES_MEM(r.start + 2, 1); + scc_b_rsrcs[0] = DEFINE_RES_MEM(r.start, 1); + scc_a_rsrcs[1] = DEFINE_RES_IRQ(irq); + scc_b_rsrcs[1] = DEFINE_RES_IRQ(irq); + scc_a_pdev.resource = scc_a_rsrcs; + scc_a_pdev.num_resources = ARRAY_SIZE(scc_a_rsrcs); + scc_b_pdev.resource = scc_b_rsrcs; + scc_b_pdev.num_resources = ARRAY_SIZE(scc_b_rsrcs); +} + +static int __init nubus_pmac_scc_register(void) +{ + if (!scc_a_pdev.num_resources) + return 0; + platform_device_register(&scc_a_pdev); + platform_device_register(&scc_b_pdev); + return 0; +} +machine_arch_initcall(nubus_pmac, nubus_pmac_scc_register); + +static const struct of_device_id nubus_pmac_bus_ids[] __initconst = { + { .compatible = "simple-bus", }, + {}, +}; + +static int __init nubus_pmac_declare_devices(void) +{ + return of_platform_bus_probe(NULL, nubus_pmac_bus_ids, NULL); +} +machine_device_initcall(nubus_pmac, nubus_pmac_declare_devices); + +static void __init nubus_pmac_setup_arch(void) +{ + if (IS_ENABLED(CONFIG_PPC_UDBG_SCC_NUBUS)) + udbg_scc_nubus_init(); + amic_dma_reserve(); +} + +/* VIA timer 1 counts at C7M/10 on all NuBus Power Macintoshes */ +#define VIA_T1_HZ 783360 +#define VIA_RS 0x200 +#define VIA_T1CL (4 * VIA_RS) +#define VIA_T1CH (5 * VIA_RS) +#define VIA_T1LL (6 * VIA_RS) +#define VIA_T1LH (7 * VIA_RS) +#define VIA_ACR (11 * VIA_RS) +#define VIA_IFR (13 * VIA_RS) +#define VIA_T1_CONT 0x40 +#define VIA_T1_MODE 0xc0 +#define VIA_T1_INT 0x40 + +/* measure the timebase of 603/604 machines over 10 ms of VIA timer 1 */ +static bool __init nubus_pmac_via_calibrate(void) +{ + unsigned int count = VIA_T1_HZ / 100, dstart, dend; + struct device_node *np; + struct resource r; + u8 __iomem *via; + + np = of_find_node_by_name(NULL, "via-cuda"); + if (!np) + np = of_find_node_by_name(NULL, "via-pmu"); + if (!np || of_address_to_resource(np, 0, &r)) { + of_node_put(np); + return false; + } + of_node_put(np); + via = early_ioremap(r.start, resource_size(&r)); + if (!via) + return false; + + out_8(via + VIA_ACR, (in_8(via + VIA_ACR) & ~VIA_T1_MODE) | VIA_T1_CONT); + out_8(via + VIA_T1CH, 2); + out_8(via + VIA_T1LL, count); + out_8(via + VIA_T1LH, count >> 8); + while (!(in_8(via + VIA_IFR) & VIA_T1_INT)) + ; + dstart = get_dec(); + in_8(via + VIA_T1CL); + while (!(in_8(via + VIA_IFR) & VIA_T1_INT)) + ; + dend = get_dec(); + early_iounmap((void __force *)via, resource_size(&r)); + + ppc_tb_freq = (dstart - dend) * 100; + pr_info("NuBus PowerMac: timebase %lu Hz measured with VIA timer 1\n", + ppc_tb_freq); + return true; +} + +static void __init nubus_pmac_calibrate_decr(void) +{ + struct device_node *cpu = of_find_node_by_type(NULL, "cpu"); + u32 clock; + + /* the 601 RTC is fixed in the device tree, the 603 timebase is bus/4 */ + if (of_property_present(cpu, "timebase-frequency") || + !nubus_pmac_via_calibrate()) { + generic_calibrate_decr(); + } else if (!of_property_read_u32(cpu, "clock-frequency", &clock)) { + ppc_proc_freq = clock; + } + of_node_put(cpu); +} + +static unsigned int nubus_pmac_no_irq(void) +{ + return 0; +} + +static void __init nubus_pmac_init_IRQ(void) +{ + struct device_node *np; + + static const struct of_device_id pics[] = { + { .compatible = "apple,amic-pic", .data = amic_pic_init }, + { .compatible = "apple,capella-pic", .data = iosb_pic_init }, + { .compatible = "apple,m2-pic", .data = iosb_pic_init }, + {} + }; + const struct of_device_id *match; + int (*init)(struct device_node *np); + + np = of_find_matching_node_and_match(NULL, pics, &match); + init = match ? match->data : NULL; + if (np && init && !init(np)) { + ppc_md.get_irq = init == amic_pic_init ? amic_get_irq : + iosb_get_irq; + } else { + pr_err("NuBus PowerMac: no supported interrupt controller\n"); + ppc_md.get_irq = nubus_pmac_no_irq; + } + of_node_put(np); + + nubus_pmac_scc_init(); + if (IS_ENABLED(CONFIG_ADB_CUDA)) + find_via_cuda(); + if (IS_ENABLED(CONFIG_ADB_PMU)) + find_via_pmu(); +} + +extern const char hwreg_present_end[]; + +/* recover the machine check of a hwreg_present() probe */ +static bool hwreg_machine_check(struct pt_regs *regs) +{ + const struct exception_table_entry *entry; + + if (regs->nip < (unsigned long)hwreg_present || + regs->nip >= (unsigned long)hwreg_present_end) + return false; + + entry = search_exception_tables(regs->nip); + if (!entry) + return false; + + regs_set_recoverable(regs); + regs_set_return_ip(regs, extable_fixup(entry)); + return true; +} + +static int nubus_pmac_machine_check(struct pt_regs *regs) +{ + if (hwreg_machine_check(regs)) + return 1; + + return cur_cpu_spec->machine_check ? cur_cpu_spec->machine_check(regs) : 0; +} + +static void nubus_pmac_show_cpuinfo(struct seq_file *m) +{ + struct device_node *root = of_find_node_by_path("/"); + const char *model = NULL; + + if (root) + of_property_read_string(root, "model", &model); + seq_printf(m, "machine\t\t: %s\n", model ?: "NuBus Power Macintosh"); + of_node_put(root); +} + +static bool nubus_pmac_has_cuda(void) +{ + return IS_ENABLED(CONFIG_ADB_CUDA) && sys_ctrler == SYS_CTRLER_CUDA; +} + +static bool nubus_pmac_has_pmu(void) +{ + return IS_ENABLED(CONFIG_ADB_PMU) && sys_ctrler == SYS_CTRLER_PMU; +} + +static void __noreturn nubus_pmac_cuda_cmd(u8 cmd) +{ +#ifdef CONFIG_ADB_CUDA + struct adb_request req; + + cuda_request(&req, NULL, 2, CUDA_PACKET, cmd); + for (;;) + cuda_poll(); +#else + for (;;) + ; +#endif +} + +static void __noreturn nubus_pmac_halt(void) +{ + local_irq_disable(); + for (;;) + ; +} + +static void __noreturn nubus_pmac_restart(char *cmd) +{ + local_irq_disable(); + if (nubus_pmac_has_cuda()) + nubus_pmac_cuda_cmd(CUDA_RESET_SYSTEM); + if (nubus_pmac_has_pmu()) + pmu_restart(); + nubus_pmac_halt(); +} + +static void __noreturn nubus_pmac_power_off(void) +{ + local_irq_disable(); + if (nubus_pmac_has_cuda()) + nubus_pmac_cuda_cmd(CUDA_POWERDOWN); + if (nubus_pmac_has_pmu()) + pmu_shutdown(); + nubus_pmac_halt(); +} + +#if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU) +static time64_t nubus_pmac_get_boot_time(void) +{ + if (nubus_pmac_has_cuda()) + return cuda_get_time(); + if (nubus_pmac_has_pmu()) + return pmu_get_time(); + return 0; +} + +static void nubus_pmac_get_rtc_time(struct rtc_time *tm) +{ + rtc_time64_to_tm(nubus_pmac_get_boot_time(), tm); +} + +static int nubus_pmac_set_rtc_time(struct rtc_time *tm) +{ + if (nubus_pmac_has_cuda()) + return cuda_set_rtc_time(tm); + if (nubus_pmac_has_pmu()) + return pmu_set_rtc_time(tm); + return -ENODEV; +} +#endif + +static int __init nubus_pmac_late_init(void) +{ + if (!pm_power_off) + pm_power_off = nubus_pmac_power_off; + return 0; +} +machine_late_initcall(nubus_pmac, nubus_pmac_late_init); + +define_machine(nubus_pmac) { + .name = "NuBus PowerMac", + .compatible = "apple,nubus-pmac", + .setup_arch = nubus_pmac_setup_arch, + .show_cpuinfo = nubus_pmac_show_cpuinfo, + .init_IRQ = nubus_pmac_init_IRQ, + .machine_check_exception = nubus_pmac_machine_check, + .calibrate_decr = nubus_pmac_calibrate_decr, +#if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU) + .get_boot_time = nubus_pmac_get_boot_time, + .get_rtc_time = nubus_pmac_get_rtc_time, + .set_rtc_time = nubus_pmac_set_rtc_time, +#endif + .restart = nubus_pmac_restart, + .halt = nubus_pmac_halt, + .progress = udbg_progress, +}; diff --git a/arch/powerpc/platforms/nubus/udbg_scc.c b/arch/powerpc/platforms/nubus/udbg_scc.c new file mode 100644 index 000000000000..8f828be913e6 --- /dev/null +++ b/arch/powerpc/platforms/nubus/udbg_scc.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * udbg for the Z85C30 SCC of the NuBus Power Macintosh. + * + * The SCC is a byte device at even addresses: channel B control at +0, + * channel A control at +2, the data registers 4 bytes after control. + * Based on arch/powerpc/platforms/powermac/udbg_scc.c and the "pre_pci" + * support of the nubus-pmac project. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include + +#include +#include + +#include "nubus.h" + +#define SCC_TXRDY 4 +#define SCC_RXRDY 1 + +static u8 __iomem *sccc; +static u8 __iomem *sccd; +static void __iomem *scc_early; +static struct resource scc_res; + +static void udbg_scc_nubus_putc(char c) +{ + while (!(in_8(sccc) & SCC_TXRDY)) + ; + out_8(sccd, c); + if (c == '\n') + udbg_scc_nubus_putc('\r'); +} + +static int udbg_scc_nubus_getc_poll(void) +{ + if (in_8(sccc) & SCC_RXRDY) + return in_8(sccd); + return -1; +} + +static int udbg_scc_nubus_getc(void) +{ + while (!(in_8(sccc) & SCC_RXRDY)) + ; + return in_8(sccd); +} + +void __init udbg_scc_nubus_init(void) +{ + static u8 inittab[] = { + 9, 0xc0, /* hardware reset */ + 4, 0x44, /* x16 clock, 1 stop */ + 3, 0xc0, /* rx 8 bits */ + 5, 0x60, /* tx 8 bits */ + 11, 0x50, /* clocks = br gen */ + 12, 0, /* baud rate divisor, low */ + 13, 0, /* high */ + 14, 1, /* baud rate gen enable, src=rtxc */ + 3, 0xc1, /* rx enable */ + 5, 0xea, /* tx enable, assert DTR & RTS */ + }; + struct device_node *np; + u32 clk = 3686400, speed = 9600, tc; + unsigned int i; + int err; + + np = of_find_compatible_node(NULL, NULL, "apple,nubus-escc"); + if (!np) + return; + of_property_read_u32(np, "clock-frequency", &clk); + of_property_read_u32(np, "current-speed", &speed); + err = of_address_to_resource(np, 0, &scc_res); + of_node_put(np); + if (err) + return; + + /* slab is not up yet, remapped by udbg_scc_nubus_remap() */ + scc_early = early_ioremap(scc_res.start, resource_size(&scc_res)); + if (!scc_early) + return; + + /* channel A, the modem port */ + sccc = scc_early + 2; + sccd = sccc + 4; + + /* baud rate generator time constant for x16 clock mode */ + tc = clk / (2 * 16 * speed) - 2; + inittab[11] = tc & 0xff; + inittab[13] = tc >> 8; + + in_8(sccc); /* reset the register pointer */ + for (i = 0; i < sizeof(inittab); i++) + out_8(sccc, inittab[i]); + + udbg_putc = udbg_scc_nubus_putc; + udbg_getc = udbg_scc_nubus_getc; + udbg_getc_poll = udbg_scc_nubus_getc_poll; + register_early_udbg_console(); +} + +static int __init udbg_scc_nubus_remap(void) +{ + void __iomem *base; + + if (!scc_early) + return 0; + + base = ioremap(scc_res.start, resource_size(&scc_res)); + if (WARN_ON(!base)) + return -ENOMEM; + + sccc = base + 2; + sccd = sccc + 4; + early_iounmap(scc_early, resource_size(&scc_res)); + scc_early = NULL; + return 0; +} +early_initcall(udbg_scc_nubus_remap); diff --git a/arch/powerpc/platforms/powermac/Makefile b/arch/powerpc/platforms/powermac/Makefile index cf85f0662d0d..7d965c337d43 100644 --- a/arch/powerpc/platforms/powermac/Makefile +++ b/arch/powerpc/platforms/powermac/Makefile @@ -13,6 +13,7 @@ ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_bootx_init.o = $(CC_FLAGS_FTRACE) endif +ifdef CONFIG_PPC_PMAC obj-y += pic.o setup.o time.o feature.o pci.o \ sleep.o low_i2c.o cache.o pfunc_core.o \ pfunc_base.o udbg_scc.o udbg_adb.o @@ -21,5 +22,7 @@ obj-$(CONFIG_PMAC_BACKLIGHT) += backlight.o # need this to be a bool. Cheat here and pretend CONFIG_NVRAM=m is really # CONFIG_NVRAM=y obj-$(CONFIG_NVRAM:m=y) += nvram.o -obj-$(CONFIG_PPC32) += bootx_init.o obj-$(CONFIG_SMP) += smp.o +endif +# also the BootX entry of NuBus kernels without PPC_PMAC +obj-$(CONFIG_PPC_BOOTX) += bootx_init.o diff --git a/arch/powerpc/platforms/powermac/bootx_init.c b/arch/powerpc/platforms/powermac/bootx_init.c index abceae91dfde..1b532c605ad3 100644 --- a/arch/powerpc/platforms/powermac/bootx_init.c +++ b/arch/powerpc/platforms/powermac/bootx_init.c @@ -3,6 +3,7 @@ * Early boot support code for BootX bootloader * * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org) + * Copyright (C) 2026 René Rebe */ #include @@ -41,6 +42,9 @@ static char __initdata bootx_disp_path[256]; #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2) #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4) +#define BOOT_ARCH_NUBUS_CLASS \ + (BOOT_ARCH_NUBUS_PDM | BOOT_ARCH_NUBUS_PERFORMA | BOOT_ARCH_NUBUS_POWERBOOK) + #ifdef CONFIG_BOOTX_TEXT static void __init bootx_printf(const char *format, ...) { @@ -83,6 +87,18 @@ static void __init bootx_printf(const char *format, ...) } va_end(args); } + +/* + * A frame buffer in main memory, like the PDM on-board video, can be inside + * the kernel as loaded, before it is copied to PHYSICAL_START + */ +static bool __init bootx_disp_in_kernel(boot_infos_t *bi) +{ + unsigned long base = (unsigned long)bi->logicalDisplayBase; + unsigned long end = base + bi->dispDeviceRect[3] * bi->dispDeviceRowBytes; + + return base < (unsigned long)_end && end > (unsigned long)_stext; +} #else /* CONFIG_BOOTX_TEXT */ static void __init bootx_printf(const char *format, ...) {} #endif /* CONFIG_BOOTX_TEXT */ @@ -114,7 +130,7 @@ static void * __init bootx_early_getprop(unsigned long base, *(mem) += 4; \ } while(0) -static unsigned long __init bootx_dt_find_string(char *str) +static unsigned long __init bootx_dt_find_string(const char *str) { char *s, *os; @@ -128,28 +144,37 @@ static unsigned long __init bootx_dt_find_string(char *str) return 0; } -static void __init bootx_dt_add_prop(char *name, void *data, int size, - unsigned long *mem_end) +static bool __init bootx_dt_add_prop_head(const char *name, int size, + unsigned long *mem_end) { unsigned long soff = bootx_dt_find_string(name); - if (data == NULL) - size = 0; + if (soff == 0) { bootx_printf("WARNING: Can't find string index for <%s>\n", name); - return; + return false; } if (size > 0x20000) { bootx_printf("WARNING: ignoring large property "); bootx_printf("%s length 0x%x\n", name, size); - return; + return false; } dt_push_token(OF_DT_PROP, mem_end); dt_push_token(size, mem_end); dt_push_token(soff, mem_end); + return true; +} + +static void __init bootx_dt_add_prop(const char *name, const void *data, + int size, unsigned long *mem_end) +{ + if (data == NULL) + size = 0; + if (!bootx_dt_add_prop_head(name, size, mem_end)) + return; /* push property content */ - if (size && data) { + if (size) { memcpy((void *)*mem_end, data, size); *mem_end = ALIGN(*mem_end + size, 4); } @@ -178,6 +203,19 @@ static void __init bootx_add_chosen_props(unsigned long base, strlen(bootx_disp_path) + 1, mem_end); } +/* the frame buffer address of the visible display rectangle */ +static u32 __init bootx_disp_addr(void) +{ + boot_infos_t *bi = bootx_info; + u32 addr = (u32)bi->dispDeviceBase; + + if (addr == 0) + addr = (u32)bi->logicalDisplayBase; + addr += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes; + addr += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8); + return addr; +} + static void __init bootx_add_display_props(unsigned long base, unsigned long *mem_end, int has_real_node) @@ -199,21 +237,32 @@ static void __init bootx_add_display_props(unsigned long base, bootx_dt_add_prop("linux,bootx-height", &tmp, 4, mem_end); tmp = bi->dispDeviceRowBytes; bootx_dt_add_prop("linux,bootx-linebytes", &tmp, 4, mem_end); - tmp = (u32)bi->dispDeviceBase; - if (tmp == 0) - tmp = (u32)bi->logicalDisplayBase; - tmp += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes; - tmp += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8); + tmp = bootx_disp_addr(); bootx_dt_add_prop("linux,bootx-addr", &tmp, 4, mem_end); } -static void __init bootx_dt_add_string(char *s, unsigned long *mem_end) +static void __init bootx_dt_add_string(const char *s, unsigned long *mem_end) { unsigned int l = strlen(s) + 1; memcpy((void *)*mem_end, s, l); bootx_dt_strend = *mem_end = *mem_end + l; } +static void __init bootx_dt_need_string(const char *s, unsigned long *mem_end) +{ + if (!bootx_dt_find_string(s)) + bootx_dt_add_string(s, mem_end); +} + +static void __init bootx_dt_begin_node(const char *name, unsigned long *mem_end) +{ + unsigned int l = strlen(name) + 1; + + dt_push_token(OF_DT_BEGIN_NODE, mem_end); + memcpy((void *)*mem_end, name, l); + *mem_end = ALIGN(*mem_end + l, 4); +} + static void __init bootx_scan_dt_build_strings(unsigned long base, unsigned long node, unsigned long *mem_end) @@ -344,24 +393,334 @@ static void __init bootx_scan_dt_build_struct(unsigned long base, dt_push_token(OF_DT_END_NODE, mem_end); } -static unsigned long __init bootx_flatten_dt(unsigned long start) +static const u32 * __init bootx_fdt_next(const u32 *p) +{ + switch (*p) { + case OF_DT_BEGIN_NODE: + return (const u32 *)ALIGN((unsigned long)(p + 1) + + strlen((const char *)(p + 1)) + 1, 4); + case OF_DT_PROP: + return p + 3 + ALIGN(p[1], 4) / 4; + } + return p + 1; +} + +static const void * __init bootx_fdt_getprop(const void *dtb, const u32 *node, + const char *name, int *len) +{ + const struct boot_param_header *fdt = dtb; + const char *strings = dtb + fdt->off_dt_strings; + const u32 *p; + + for (p = bootx_fdt_next(node); *p == OF_DT_PROP || *p == OF_DT_NOP; + p = bootx_fdt_next(p)) { + if (*p == OF_DT_PROP && !strcmp(strings + p[2], name)) { + if (len) + *len = p[1]; + return p + 3; + } + } + return NULL; +} + +static const u32 * __init bootx_fdt_find_type(const void *dtb, const u32 *from, + const char *type) +{ + const char *t; + const u32 *p; + + for (p = bootx_fdt_next(from); *p != OF_DT_END; p = bootx_fdt_next(p)) { + if (*p != OF_DT_BEGIN_NODE) + continue; + t = bootx_fdt_getprop(dtb, p, "device_type", NULL); + if (t && !strcmp(t, type)) + return p; + } + return NULL; +} + +static u32 __init bootx_fdt_cells(const void *dtb, const u32 *node, + const char *name, u32 def) +{ + const u32 *cells; + int len; + + cells = bootx_fdt_getprop(dtb, node, name, &len); + return cells && len == 4 && *cells ? *cells : def; +} + +/* NuBus slot and AV card video by framebuffer address, as MkLinux does */ +static const char * __init bootx_nubus_card_video(u32 base) +{ + if (base >= 0xf0000000) + return "nubus-video"; + if ((base & 0xfff00000) == 0xe0100000) + return "av-video"; + return NULL; +} + +static bool __init bootx_fdt_is_compatible(const void *dtb, const u32 *node, + const char *compat) +{ + const char *p; + int len, l; + + p = bootx_fdt_getprop(dtb, node, "compatible", &len); + for (; p && len > 0; p += l, len -= l) { + l = strlen(p) + 1; + if (!strcmp(p, compat)) + return true; + } + return false; +} + +static void __init bootx_add_reg(u32 addr, u32 size, u32 acells, u32 scells, + unsigned long *mem_end) +{ + u32 i; + + for (i = 1; i < acells; i++) + dt_push_token(0, mem_end); + dt_push_token(addr, mem_end); + for (i = 1; i < scells; i++) + dt_push_token(0, mem_end); + dt_push_token(size, mem_end); +} + +/* + * The PDM HPV and AV card registers (MkLinux video_hpv.c, video_av.c) and + * the boot frame buffer; a Radius card at 0xfb000000 gets no driver node. + */ +static void __init bootx_add_pdm_video_props(u32 acells, u32 scells, + unsigned long *mem_end) +{ + boot_infos_t *bi = bootx_info; + u32 base = (u32)bi->dispDeviceBase; + u32 fb_size = bi->dispDeviceRowBytes * + (bi->dispDeviceRect[3] - bi->dispDeviceRect[1]); + const char *compat; + int n; + + if (base == 0xfb000000) + return; + if (base >= 0xf0000000) { + compat = "apple,pdm-hpv"; + n = 2; + } else { + compat = "apple,pdm-av"; + n = 3; + } + bootx_dt_add_prop("compatible", compat, strlen(compat) + 1, mem_end); + if (!bootx_dt_add_prop_head("reg", n * (acells + scells) * 4, mem_end)) + return; + if (n == 2) { + bootx_add_reg(0xfec00000, 0x1000, acells, scells, mem_end); + } else { + bootx_add_reg(0xe0036000, 0x1000, acells, scells, mem_end); + bootx_add_reg(0xe0f30000, 0x20, acells, scells, mem_end); + } + bootx_add_reg(bootx_disp_addr(), fb_size, acells, scells, mem_end); +} + +/* the built-in device trees only describe the on-board video */ +static const u32 * __init bootx_fdt_find_display(const void *dtb, + const u32 *root) +{ + const u32 *p, *addr; + int len; + + for (p = root; (p = bootx_fdt_find_type(dtb, p, "display")); ) { + addr = bootx_fdt_getprop(dtb, p, "address", &len); + if (!addr || (len == 4 && + *addr == (u32)bootx_info->dispDeviceBase)) + return p; + } + return NULL; +} + +static void __init bootx_add_memory_props(u32 acells, u32 scells, + unsigned long *mem_end) +{ + boot_infos_t *bi = bootx_info; + u32 i, j, n, banks = 0; + + n = min_t(u32, bi->physMemoryMapSize, MAX_MEM_MAP_SIZE); + for (i = 0; i < n; i++) + if (bi->physMemoryMap[i].size) + banks++; + if (!bootx_dt_add_prop_head("reg", banks * (acells + scells) * 4, + mem_end)) + return; + for (i = 0; i < n; i++) { + if (!bi->physMemoryMap[i].size) + continue; + for (j = 1; j < acells; j++) + dt_push_token(0, mem_end); + dt_push_token(bi->physMemoryMap[i].physAddr, mem_end); + for (j = 1; j < scells; j++) + dt_push_token(0, mem_end); + dt_push_token(bi->physMemoryMap[i].size, mem_end); + } +} + +static void __init bootx_fdt_add_chosen_props(unsigned long *mem_end) +{ + u32 id = bootx_info->machineID; + + bootx_add_chosen_props(0, mem_end); + bootx_dt_add_prop("linux,bootx-machine-id", &id, 4, mem_end); +} + +static void __init bootx_fdt_build_strings(const void *dtb, + unsigned long *mem_end) +{ + const struct boot_param_header *fdt = dtb; + + memcpy((void *)*mem_end, dtb + fdt->off_dt_strings, + fdt->dt_strings_size); + bootx_dt_strend = *mem_end = *mem_end + fdt->dt_strings_size; + + bootx_dt_need_string("device_type", mem_end); + bootx_dt_need_string("compatible", mem_end); + bootx_dt_need_string("reg", mem_end); + bootx_dt_need_string("bootargs", mem_end); + bootx_dt_need_string("linux,bootx", mem_end); + bootx_dt_need_string("linux,bootx-machine-id", mem_end); + bootx_dt_need_string("linux,initrd-start", mem_end); + bootx_dt_need_string("linux,initrd-end", mem_end); + bootx_dt_need_string("linux,boot-display", mem_end); + bootx_dt_need_string("linux,opened", mem_end); +} + +/* + * Copy the built-in tree, replacing the memory, /chosen and boot display + * properties and adding the nodes it lacks. + */ +static void __init bootx_fdt_build_struct(const void *dtb, + unsigned long *mem_end) +{ + const struct boot_param_header *fdt = dtb; + const char *strings = dtb + fdt->off_dt_strings; + const u32 *root = dtb + fdt->off_dt_struct; + const u32 *memory, *display = NULL, *p; + const char *video = NULL, *name; + u32 acells, scells, base; + enum { NONE, MEMORY, CHOSEN, DISPLAY } node = NONE; + bool has_chosen = false, pdm_card = false; + int depth = 0; + + acells = bootx_fdt_cells(dtb, root, "#address-cells", 2); + scells = bootx_fdt_cells(dtb, root, "#size-cells", 1); + memory = bootx_fdt_find_type(dtb, root, "memory"); + + base = (u32)bootx_info->dispDeviceBase; + if (base) { + video = bootx_nubus_card_video(base); + pdm_card = video && + bootx_fdt_is_compatible(dtb, root, "apple,pdm"); + if (!video) + display = bootx_fdt_find_display(dtb, root); + if (!video && !display) + video = "video"; + } + + for (p = root; *p != OF_DT_END; p = bootx_fdt_next(p)) { + if (*p == OF_DT_PROP) { + name = strings + p[2]; + if (node == MEMORY && !strcmp(name, "reg")) + continue; + if (node == CHOSEN && + (!strcmp(name, "bootargs") || + !strncmp(name, "linux,initrd-", 13))) + continue; + bootx_dt_add_prop(name, p + 3, p[1], mem_end); + continue; + } + if (*p != OF_DT_BEGIN_NODE && *p != OF_DT_END_NODE) + continue; + + /* properties end at the first child or the end of a node */ + if (node == MEMORY) + bootx_add_memory_props(acells, scells, mem_end); + else if (node == CHOSEN) + bootx_fdt_add_chosen_props(mem_end); + else if (node == DISPLAY) + bootx_add_display_props(0, mem_end, 1); + node = NONE; + + if (*p == OF_DT_BEGIN_NODE) { + name = (const char *)(p + 1); + bootx_dt_begin_node(name, mem_end); + if (p == memory) { + node = MEMORY; + } else if (p == display) { + node = DISPLAY; + } else if (depth == 1 && !strcmp(name, "chosen")) { + node = CHOSEN; + has_chosen = true; + } + depth++; + continue; + } + + if (--depth == 0) { + if (!memory) { + bootx_dt_begin_node("memory", mem_end); + bootx_dt_add_prop("device_type", "memory", + sizeof("memory"), mem_end); + bootx_add_memory_props(acells, scells, mem_end); + dt_push_token(OF_DT_END_NODE, mem_end); + } + if (!has_chosen) { + bootx_dt_begin_node("chosen", mem_end); + bootx_fdt_add_chosen_props(mem_end); + dt_push_token(OF_DT_END_NODE, mem_end); + } + if (video) { + bootx_dt_begin_node(video, mem_end); + bootx_dt_add_prop("device_type", "display", + sizeof("display"), mem_end); + if (pdm_card) + bootx_add_pdm_video_props(acells, + scells, + mem_end); + bootx_add_display_props(0, mem_end, 1); + dt_push_token(OF_DT_END_NODE, mem_end); + } + } + dt_push_token(OF_DT_END_NODE, mem_end); + } +} + +static unsigned long __init bootx_flatten_dt(unsigned long start, + const void *dtb) { boot_infos_t *bi = bootx_info; unsigned long mem_start, mem_end; + const struct boot_param_header *fdt = dtb; + const u64 *fdt_rsvmap = NULL; struct boot_param_header *hdr; unsigned long base; + int i, nrsv = 0; u64 *rsvmap; + /* keep the reserve map of a built-in tree */ + if (dtb) { + fdt_rsvmap = dtb + fdt->off_mem_rsvmap; + while (fdt_rsvmap[2 * nrsv + 1]) + nrsv++; + } + /* Start using memory after the big blob passed by BootX, get * some space for the header */ - mem_start = mem_end = ALIGN(((unsigned long)bi) + start, 4); + mem_start = mem_end = ALIGN(((unsigned long)bi) + start, 8); DBG("Boot params header at: %x\n", mem_start); hdr = (struct boot_param_header *)mem_start; mem_end += sizeof(struct boot_param_header); rsvmap = (u64 *)(ALIGN(mem_end, 8)); hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start; - mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64); + mem_end = (unsigned long)(rsvmap + 2 * (nrsv + 3)); /* Get base of tree */ base = ((unsigned long)bi) + bi->deviceTreeOffset; @@ -372,7 +731,10 @@ static unsigned long __init bootx_flatten_dt(unsigned long start) bootx_dt_strbase = mem_end; mem_end += 4; bootx_dt_strend = mem_end; - bootx_scan_dt_build_strings(base, 4, &mem_end); + if (dtb) + bootx_fdt_build_strings(dtb, &mem_end); + else + bootx_scan_dt_build_strings(base, 4, &mem_end); /* Add some strings */ bootx_dt_add_string("linux,bootx-noscreen", &mem_end); bootx_dt_add_string("linux,bootx-depth", &mem_end); @@ -388,7 +750,10 @@ static unsigned long __init bootx_flatten_dt(unsigned long start) mem_end = ALIGN(mem_end, 16); DBG("Building device tree structure at: %x\n", mem_end); hdr->off_dt_struct = mem_end - mem_start; - bootx_scan_dt_build_struct(base, 4, &mem_end); + if (dtb) + bootx_fdt_build_struct(dtb, &mem_end); + else + bootx_scan_dt_build_struct(base, 4, &mem_end); dt_push_token(OF_DT_END, &mem_end); /* Finish header */ @@ -405,11 +770,13 @@ static unsigned long __init bootx_flatten_dt(unsigned long start) */ mem_end = ALIGN(mem_end, PAGE_SIZE); DBG("End of boot params: %x\n", mem_end); + for (i = 0; i < 2 * nrsv; i++) + *rsvmap++ = fdt_rsvmap[i]; rsvmap[0] = mem_start; - rsvmap[1] = mem_end; + rsvmap[1] = mem_end - mem_start; if (bootx_info->ramDisk) { rsvmap[2] = ((unsigned long)bootx_info) + bootx_info->ramDisk; - rsvmap[3] = rsvmap[2] + bootx_info->ramDiskSize; + rsvmap[3] = bootx_info->ramDiskSize; rsvmap[4] = 0; rsvmap[5] = 0; } else { @@ -434,6 +801,8 @@ static void __init btext_welcome(boot_infos_t *bi) bootx_printf(" (log)"); bootx_printf("\nklimit : 0x%x",(unsigned long)_end); bootx_printf("\nboot_info at : 0x%x", bi); + if (bi->architecture & BOOT_ARCH_NUBUS) + bootx_printf("\nmachine ID : 0x%x", bi->machineID); __asm__ __volatile__ ("mfmsr %0" : "=r" (flags)); bootx_printf("\nMSR : 0x%x", flags); __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr)); @@ -458,12 +827,161 @@ static void __init btext_welcome(boot_infos_t *bi) } #endif /* CONFIG_BOOTX_TEXT */ +/* lowest page aligned RAM at or above @from with @need bytes in one bank */ +static unsigned long __init bootx_find_ram(boot_infos_t *bi, unsigned long from, + unsigned long need) +{ + u32 i, n = min_t(u32, bi->physMemoryMapSize, MAX_MEM_MAP_SIZE); + unsigned long best = 0; + + for (i = 0; i < n; i++) { + unsigned long base = bi->physMemoryMap[i].physAddr; + unsigned long end = base + bi->physMemoryMap[i].size; + unsigned long p = PAGE_ALIGN(max(base, from)); + + if (p < end && end - p >= need && (!best || p < best)) + best = p; + } + return best; +} + +#ifdef CONFIG_PPC_NUBUS_PMAC +extern const u8 __dtb_nubus_pdm_begin[], __dtb_nubus_performa_begin[]; +extern const u8 __dtb_nubus_m2_begin[], __dtb_nubus_m2_2300_begin[]; + +struct bootx_nubus_machine { + u16 id; + u8 arch; +}; + +/* Mac OS Gestalt machine types, MkLinux powermac_gestalt.h */ +static const struct bootx_nubus_machine bootx_nubus_machines[] __initconst = { + { 12, BOOT_ARCH_NUBUS_PDM }, /* Power Macintosh 8100/120 */ + { 31, BOOT_ARCH_NUBUS_PDM }, /* 7100/66 */ + { 39, BOOT_ARCH_NUBUS_PDM }, /* Workgroup Server 9150/80 */ + { 40, BOOT_ARCH_NUBUS_PDM }, /* 8100/110 */ + { 41, BOOT_ARCH_NUBUS_PERFORMA }, /* 5200 */ + { 42, BOOT_ARCH_NUBUS_PERFORMA }, /* 6200 */ + { 47, BOOT_ARCH_NUBUS_PDM }, /* 7100/80 */ + { 55, BOOT_ARCH_NUBUS_PDM }, /* 8100/100 */ + { 57, BOOT_ARCH_NUBUS_PDM }, /* Workgroup Server 9150/120 */ + { 64, BOOT_ARCH_NUBUS_PDM }, /* PDM 100F */ + { 65, BOOT_ARCH_NUBUS_PDM }, /* 8100/80 */ + { 66, BOOT_ARCH_NUBUS_PDM }, /* PDM 66F */ + { 72, BOOT_ARCH_NUBUS_POWERBOOK }, /* PowerBook 520/540 */ + { 75, BOOT_ARCH_NUBUS_PDM }, /* 6100/60 */ + { 100, BOOT_ARCH_NUBUS_PDM }, /* 6100/66 */ + { 101, BOOT_ARCH_NUBUS_PDM }, /* 6100/80 */ + { 111, BOOT_ARCH_NUBUS_PDM }, /* PDM 80 WLCD */ + { 112, BOOT_ARCH_NUBUS_PDM }, /* 7100/66 */ + { 113, BOOT_ARCH_NUBUS_PDM }, /* 7100/80 clock chipped */ + { 124, BOOT_ARCH_NUBUS_POWERBOOK }, /* PowerBook Duo 2300 */ + { 126, BOOT_ARCH_NUBUS_POWERBOOK }, /* PowerBook 500 PPC upgrade */ + { 128, BOOT_ARCH_NUBUS_POWERBOOK }, /* PowerBook 5300 */ + { 310, BOOT_ARCH_NUBUS_POWERBOOK }, /* PowerBook 1400 */ +}; + +static const void * __init bootx_nubus_dtb(u32 arch, u32 id) +{ + switch (arch) { + case BOOT_ARCH_NUBUS_PDM: + return __dtb_nubus_pdm_begin; + case BOOT_ARCH_NUBUS_PERFORMA: + return __dtb_nubus_performa_begin; + case BOOT_ARCH_NUBUS_POWERBOOK: + /* M2 (Whitney) only, not the 68k PowerBook 500 PPC upgrades */ + if (id == 124) + return __dtb_nubus_m2_2300_begin; + if (id == 128 || id == 310) + return __dtb_nubus_m2_begin; + break; + } + return NULL; +} + +/* + * Sort and merge the DRAM banks: the kernel limits its early allocations + * to the first memory range. + */ +static u32 __init bootx_nubus_memory(boot_infos_t *bi) +{ + boot_info_map_entry_t *map = bi->physMemoryMap, tmp; + u32 i, j, n = 0, count; + + count = min_t(u32, bi->physMemoryMapSize, MAX_MEM_MAP_SIZE); + for (i = 0; i < count; i++) { + if (!map[i].size) + continue; + tmp = map[i]; + for (j = n; j > 0 && map[j - 1].physAddr > tmp.physAddr; j--) + map[j] = map[j - 1]; + map[j] = tmp; + n++; + } + for (i = 1, j = 0; i < n; i++) { + if (map[j].physAddr + map[j].size == map[i].physAddr) + map[j].size += map[i].size; + else + map[++j] = map[i]; + } + bi->physMemoryMapSize = n ? j + 1 : 0; + return bi->physMemoryMapSize; +} + +static const void * __init bootx_nubus_find_dtb(boot_infos_t *bi) +{ + const struct boot_param_header *fdt; + u32 i, arch = 0; + + if (!BOOT_INFO_IS_V4_COMPATIBLE(bi)) { + bootx_printf(" !!! NuBus needs boot info version 4\n"); + return NULL; + } + for (i = 0; i < ARRAY_SIZE(bootx_nubus_machines); i++) + if (bootx_nubus_machines[i].id == bi->machineID) + arch = bootx_nubus_machines[i].arch; + if (!arch) { + bootx_printf(" !!! Unknown NuBus machine ID 0x%x\n", + bi->machineID); + return NULL; + } + if ((bi->architecture & BOOT_ARCH_NUBUS_CLASS) && + !(bi->architecture & arch)) + bootx_printf("WARNING: architecture 0x%x does not match machine ID 0x%x\n", + bi->architecture, bi->machineID); + + fdt = bootx_nubus_dtb(arch, bi->machineID); + if (!fdt) { + bootx_printf(" !!! No device tree for NuBus machine ID 0x%x\n", + bi->machineID); + return NULL; + } + if (fdt->magic != OF_DT_HEADER || fdt->version < OF_DT_VERSION) { + bootx_printf(" !!! Invalid built-in device tree\n"); + return NULL; + } + if (!bootx_nubus_memory(bi)) { + bootx_printf(" !!! No physical memory map\n"); + return NULL; + } + return fdt; +} +#else +static const void * __init bootx_nubus_find_dtb(boot_infos_t *bi) +{ + bootx_printf(" !!! NuBus Power Macintosh support not built in\n"); + return NULL; +} +#endif /* CONFIG_PPC_NUBUS_PMAC */ + void __init bootx_init(unsigned long r3, unsigned long r4) { boot_infos_t *bi = (boot_infos_t *) r4; + const void *dtb = NULL; unsigned long hdr; unsigned long space; unsigned long ptr; + unsigned long kend; char *model; unsigned long offset = reloc_offset(); @@ -490,12 +1008,14 @@ void __init bootx_init(unsigned long r3, unsigned long r4) ptr = (unsigned long)bi->logicalDisplayBase; ptr += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes; ptr += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8); - btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0], - bi->dispDeviceRect[3] - bi->dispDeviceRect[1], - bi->dispDeviceDepth, bi->dispDeviceRowBytes, - (unsigned long)bi->logicalDisplayBase); - btext_clearscreen(); - btext_flushscreen(); + if (!bootx_disp_in_kernel(bi)) { + btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0], + bi->dispDeviceRect[3] - bi->dispDeviceRect[1], + bi->dispDeviceDepth, bi->dispDeviceRowBytes, + (unsigned long)bi->logicalDisplayBase); + btext_clearscreen(); + btext_flushscreen(); + } #endif /* CONFIG_BOOTX_TEXT */ /* @@ -513,7 +1033,12 @@ void __init bootx_init(unsigned long r3, unsigned long r4) for (;;) ; } - if (bi->architecture != BOOT_ARCH_PCI) { + if (bi->architecture & BOOT_ARCH_NUBUS) { + dtb = bootx_nubus_find_dtb(bi); + if (!dtb) + for (;;) + ; + } else if (bi->architecture != BOOT_ARCH_PCI) { bootx_printf(" !!! WARNING - Unsupported machine" " architecture !\n"); for (;;) @@ -550,9 +1075,35 @@ void __init bootx_init(unsigned long r3, unsigned long r4) space = bi->ramDisk + bi->ramDiskSize; } else space = bi->totalParamsSize; + /* BootX 4 has no device tree after the arguments on NuBus */ + space = max_t(unsigned long, space, sizeof(*bi)); + if (bi->kernelParamsOffset) + space = max_t(unsigned long, space, bi->kernelParamsOffset + + strlen((char *)bi + bi->kernelParamsOffset) + 1); bootx_printf("Total space used by parameters & ramdisk: 0x%x\n", space); + /* the kernel is copied to PHYSICAL_START, keep the boot infos above it */ + kend = PHYSICAL_START + (unsigned long)(_end - _stext); + if ((unsigned long)bi < kend) { + const struct boot_param_header *fdt = dtb; + /* room for the flattened tree, built right after the boot infos */ + unsigned long need = space + PAGE_SIZE + 2 * bi->deviceTreeSize; + + if (fdt) + need += 2 * be32_to_cpu(fdt->totalsize); + + kend = max(kend, (unsigned long)_end); + ptr = bootx_find_ram(bi, kend, need); + if (!ptr) { + bootx_printf("No RAM bank holds the boot infos above the kernel\n"); + ptr = PAGE_ALIGN(kend); + } + bootx_printf("Moving boot infos to 0x%x\n", ptr); + memmove((void *)ptr, bi, space); + bootx_info = bi = (boot_infos_t *)ptr; + } + /* New BootX will have flushed all TLBs and enters kernel with * MMU switched OFF, so this should not be useful anymore. */ @@ -576,7 +1127,7 @@ void __init bootx_init(unsigned long r3, unsigned long r4) */ bootx_printf("Preparing boot params...\n"); - hdr = bootx_flatten_dt(space); + hdr = bootx_flatten_dt(space, dtb); #ifdef CONFIG_BOOTX_TEXT #ifdef SET_BOOT_BAT diff --git a/arch/powerpc/platforms/powermac/sleep.S b/arch/powerpc/platforms/powermac/sleep.S index 822ed70cdcbf..cb13fbc43f93 100644 --- a/arch/powerpc/platforms/powermac/sleep.S +++ b/arch/powerpc/platforms/powermac/sleep.S @@ -3,6 +3,7 @@ * This file contains sleep low-level functions for PowerBook G3. * Copyright (C) 1999 Benjamin Herrenschmidt (benh@kernel.crashing.org) * and Paul Mackerras (paulus@samba.org). + * Copyright (C) 2026 René Rebe */ #include @@ -184,7 +185,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS) addi r5,r5,grackle_wake_up@l tophys(r5,r5) stw r5,SL_PC(r11) - lis r4,KERNELBASE@h + lis r4,PAGE_OFFSET@h tophys(r5,r11) addi r5,r5,SL_PC lis r6,MAGIC@ha diff --git a/drivers/Kconfig b/drivers/Kconfig index f2bed2ddeb66..e0b3d307eeff 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -5,6 +5,7 @@ menu "Device Drivers" source "drivers/amba/Kconfig" source "drivers/eisa/Kconfig" +source "drivers/nubus/Kconfig" source "drivers/pci/Kconfig" source "drivers/cxl/Kconfig" source "drivers/pcmcia/Kconfig" diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index 858320b6ebb7..271be491b0ce 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -60,10 +60,11 @@ config ATARI_FLOPPY config MAC_FLOPPY tristate "Support for PowerMac floppy" - depends on PPC_PMAC && !PPC_PMAC64 + depends on (PPC_PMAC && !PPC_PMAC64) || PPC_NUBUS_PMAC help If you have a SWIM-3 (Super Woz Integrated Machine 3; from Apple) - floppy controller, say Y here. Most commonly found in PowerMacs. + floppy controller, say Y here. Most commonly found in PowerMacs, + including the NuBus PDM models (6100, 7100, 8100). config BLK_DEV_SWIM tristate "Support for SWIM Macintosh floppy" diff --git a/drivers/block/swim3.c b/drivers/block/swim3.c index a03de4c4d35f..4d23c675be82 100644 --- a/drivers/block/swim3.c +++ b/drivers/block/swim3.c @@ -4,6 +4,7 @@ * floppy controller found on Power Macintoshes. * * Copyright (C) 1996 Paul Mackerras. + * Copyright (C) 2026 René Rebe */ /* @@ -28,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -53,33 +56,32 @@ enum swim_state { ejecting }; -#define REG(x) unsigned char x; char x ## _pad[15]; - /* * The names for these registers mostly represent speculation on my part. * It will be interesting to see how close they are to the names Apple uses. + * The registers are 16 bytes apart on PCI Macs and 512 bytes on the PDM. */ -struct swim3 { - REG(data); - REG(timer); /* counts down at 1MHz */ - REG(error); - REG(mode); - REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */ - REG(setup); - REG(control); /* writing bits clears them */ - REG(status); /* writing bits sets them in control */ - REG(intr); - REG(nseek); /* # tracks to seek */ - REG(ctrack); /* current track number */ - REG(csect); /* current sector number */ - REG(gap3); /* size of gap 3 in track format */ - REG(sector); /* sector # to read or write */ - REG(nsect); /* # sectors to read or write */ - REG(intr_enable); +enum swim3_reg { + SW_DATA, + SW_TIMER, /* counts down at 1MHz */ + SW_ERROR, + SW_MODE, + SW_SELECT, /* controls CA0, CA1, CA2 and LSTRB signals */ + SW_SETUP, + SW_CONTROL, /* writing bits clears them */ + SW_STATUS, /* writing bits sets them in control */ + SW_INTR, + SW_NSEEK, /* # tracks to seek */ + SW_CTRACK, /* current track number */ + SW_CSECT, /* current sector number */ + SW_GAP3, /* size of gap 3 in track format */ + SW_SECTOR, /* sector # to read or write */ + SW_NSECT, /* # sectors to read or write */ + SW_INTR_ENABLE, }; -#define control_bic control -#define control_bis status +#define SW_CONTROL_BIC SW_CONTROL +#define SW_CONTROL_BIS SW_STATUS /* Bits in select register */ #define CA_MASK 7 @@ -173,10 +175,27 @@ struct swim3 { #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc" #define MFM_GAP_LEN 12 +struct floppy_state; + +/* DBDMA on PCI Macs, AMIC DMA on the PDM */ +struct swim3_dma_ops { + void (*prepare)(struct floppy_state *fs, struct request *req, int n); + void (*start)(struct floppy_state *fs, bool write); + /* data bytes left on an error interrupt, else 0 or -EIO */ + int (*finish)(struct floppy_state *fs, struct request *req, + bool error); + void (*abort)(struct floppy_state *fs); +}; + struct floppy_state { enum swim_state state; - struct swim3 __iomem *swim3; /* hardware registers */ + void __iomem *swim3; /* hardware registers */ + unsigned int reg_shift; /* log2 of the register stride */ + const struct swim3_dma_ops *dma_ops; + struct device *dev; struct dbdma_regs __iomem *dma; /* DMA controller registers */ + void __iomem *amic; /* AMIC floppy DMA registers */ + struct amic_dma_buf amic_buf; int swim3_intr; /* interrupt number for SWIM3 */ int dma_intr; /* interrupt number for DMA channel */ int cur_cyl; /* cylinder head is on, or -1 */ @@ -206,12 +225,12 @@ struct floppy_state { struct blk_mq_tag_set tag_set; }; -#define swim3_err(fmt, arg...) dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) -#define swim3_warn(fmt, arg...) dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) -#define swim3_info(fmt, arg...) dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) +#define swim3_err(fmt, arg...) dev_err(fs->dev, "[fd%d] " fmt, fs->index, arg) +#define swim3_warn(fmt, arg...) dev_warn(fs->dev, "[fd%d] " fmt, fs->index, arg) +#define swim3_info(fmt, arg...) dev_info(fs->dev, "[fd%d] " fmt, fs->index, arg) #ifdef DEBUG -#define swim3_dbg(fmt, arg...) dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg) +#define swim3_dbg(fmt, arg...) dev_dbg(fs->dev, "[fd%d] " fmt, fs->index, arg) #else #define swim3_dbg(fmt, arg...) do { } while(0) #endif @@ -253,6 +272,26 @@ static unsigned int floppy_check_events(struct gendisk *disk, unsigned int clearing); static int floppy_revalidate(struct gendisk *disk); +static u8 swim3_in(struct floppy_state *fs, enum swim3_reg reg) +{ + return in_8(fs->swim3 + (reg << fs->reg_shift)); +} + +static void swim3_out(struct floppy_state *fs, enum swim3_reg reg, u8 val) +{ + out_8(fs->swim3 + (reg << fs->reg_shift), val); +} + +static bool swim3_bay_absent(struct floppy_state *fs) +{ +#ifdef CONFIG_PPC_PMAC + return fs->mdev && fs->mdev->media_bay && + check_media_bay(fs->mdev->media_bay) != MB_FD; +#else + return false; +#endif +} + static bool swim3_end_request(struct floppy_state *fs, blk_status_t err, unsigned int nr_bytes) { struct request *req = fs->cur_req; @@ -271,36 +310,31 @@ static bool swim3_end_request(struct floppy_state *fs, blk_status_t err, unsigne static void swim3_select(struct floppy_state *fs, int sel) { - struct swim3 __iomem *sw = fs->swim3; - - out_8(&sw->select, RELAX); + swim3_out(fs, SW_SELECT, RELAX); if (sel & 8) - out_8(&sw->control_bis, SELECT); + swim3_out(fs, SW_CONTROL_BIS, SELECT); else - out_8(&sw->control_bic, SELECT); - out_8(&sw->select, sel & CA_MASK); + swim3_out(fs, SW_CONTROL_BIC, SELECT); + swim3_out(fs, SW_SELECT, sel & CA_MASK); } static void swim3_action(struct floppy_state *fs, int action) { - struct swim3 __iomem *sw = fs->swim3; - swim3_select(fs, action); udelay(1); - out_8(&sw->select, sw->select | LSTRB); + swim3_out(fs, SW_SELECT, swim3_in(fs, SW_SELECT) | LSTRB); udelay(2); - out_8(&sw->select, sw->select & ~LSTRB); + swim3_out(fs, SW_SELECT, swim3_in(fs, SW_SELECT) & ~LSTRB); udelay(1); } static int swim3_readbit(struct floppy_state *fs, int bit) { - struct swim3 __iomem *sw = fs->swim3; int stat; swim3_select(fs, bit); udelay(1); - stat = in_8(&sw->status); + stat = swim3_in(fs, SW_STATUS); return (stat & DATA) == 0; } @@ -318,8 +352,7 @@ static blk_status_t swim3_queue_rq(struct blk_mq_hw_ctx *hctx, } blk_mq_start_request(req); fs->cur_req = req; - if (fs->mdev->media_bay && - check_media_bay(fs->mdev->media_bay) != MB_FD) { + if (swim3_bay_absent(fs)) { swim3_dbg("%s", " media bay absent, dropping req\n"); swim3_end_request(fs, BLK_STS_IOERR, 0); goto out; @@ -371,69 +404,37 @@ static void set_timeout(struct floppy_state *fs, int nticks, static inline void scan_track(struct floppy_state *fs) { - struct swim3 __iomem *sw = fs->swim3; - swim3_select(fs, READ_DATA_0); - in_8(&sw->intr); /* clear SEEN_SECTOR bit */ - in_8(&sw->error); - out_8(&sw->intr_enable, SEEN_SECTOR); - out_8(&sw->control_bis, DO_ACTION); + swim3_in(fs, SW_INTR); /* clear SEEN_SECTOR bit */ + swim3_in(fs, SW_ERROR); + swim3_out(fs, SW_INTR_ENABLE, SEEN_SECTOR); + swim3_out(fs, SW_CONTROL_BIS, DO_ACTION); /* enable intr when track found */ set_timeout(fs, HZ, scan_timeout); /* enable timeout */ } static inline void seek_track(struct floppy_state *fs, int n) { - struct swim3 __iomem *sw = fs->swim3; - if (n >= 0) { swim3_action(fs, SEEK_POSITIVE); - sw->nseek = n; + swim3_out(fs, SW_NSEEK, n); } else { swim3_action(fs, SEEK_NEGATIVE); - sw->nseek = -n; + swim3_out(fs, SW_NSEEK, -n); } fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1; swim3_select(fs, STEP); - in_8(&sw->error); + swim3_in(fs, SW_ERROR); /* enable intr when seek finished */ - out_8(&sw->intr_enable, SEEK_DONE); - out_8(&sw->control_bis, DO_SEEK); + swim3_out(fs, SW_INTR_ENABLE, SEEK_DONE); + swim3_out(fs, SW_CONTROL_BIS, DO_SEEK); set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */ fs->settle_time = 0; } -/* - * XXX: this is a horrible hack, but at least allows ppc32 to get - * out of defining virt_to_bus, and this driver out of using the - * deprecated block layer bounce buffering for highmem addresses - * for no good reason. - */ -static unsigned long swim3_phys_to_bus(phys_addr_t paddr) -{ - return paddr + PCI_DRAM_OFFSET; -} - -static phys_addr_t swim3_bio_phys(struct bio *bio) -{ - return page_to_phys(bio_page(bio)) + bio_offset(bio); -} - -static inline void init_dma(struct dbdma_cmd *cp, int cmd, - phys_addr_t paddr, int count) -{ - cp->req_count = cpu_to_le16(count); - cp->command = cpu_to_le16(cmd); - cp->phy_addr = cpu_to_le32(swim3_phys_to_bus(paddr)); - cp->xfer_status = 0; -} - static inline void setup_transfer(struct floppy_state *fs) { int n; - struct swim3 __iomem *sw = fs->swim3; - struct dbdma_cmd *cp = fs->dma_cmd; - struct dbdma_regs __iomem *dr = fs->dma; struct request *req = fs->cur_req; if (blk_rq_cur_sectors(req) <= 0) { @@ -453,34 +454,20 @@ static inline void setup_transfer(struct floppy_state *fs) fs->scount = n; swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0); - out_8(&sw->sector, fs->req_sector); - out_8(&sw->nsect, n); - out_8(&sw->gap3, 0); - out_le32(&dr->cmdptr, swim3_phys_to_bus(virt_to_phys(cp))); - if (rq_data_dir(req) == WRITE) { - /* Set up 3 dma commands: write preamble, data, postamble */ - init_dma(cp, OUTPUT_MORE, virt_to_phys(write_preamble), - sizeof(write_preamble)); - ++cp; - init_dma(cp, OUTPUT_MORE, swim3_bio_phys(req->bio), 512); - ++cp; - init_dma(cp, OUTPUT_LAST, virt_to_phys(write_postamble), - sizeof(write_postamble)); - } else { - init_dma(cp, INPUT_LAST, swim3_bio_phys(req->bio), n * 512); - } - ++cp; - out_le16(&cp->command, DBDMA_STOP); - out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); - in_8(&sw->error); - out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); + swim3_out(fs, SW_SECTOR, fs->req_sector); + swim3_out(fs, SW_NSECT, n); + swim3_out(fs, SW_GAP3, 0); + fs->dma_ops->prepare(fs, req, n); + swim3_out(fs, SW_CONTROL_BIC, DO_ACTION | WRITE_SECTORS); + swim3_in(fs, SW_ERROR); + swim3_out(fs, SW_CONTROL_BIC, DO_ACTION | WRITE_SECTORS); if (rq_data_dir(req) == WRITE) - out_8(&sw->control_bis, WRITE_SECTORS); - in_8(&sw->intr); - out_le32(&dr->control, (RUN << 16) | RUN); + swim3_out(fs, SW_CONTROL_BIS, WRITE_SECTORS); + swim3_in(fs, SW_INTR); + fs->dma_ops->start(fs, rq_data_dir(req) == WRITE); /* enable intr when transfer complete */ - out_8(&sw->intr_enable, TRANSFER_DONE); - out_8(&sw->control_bis, DO_ACTION); + swim3_out(fs, SW_INTR_ENABLE, TRANSFER_DONE); + swim3_out(fs, SW_CONTROL_BIS, DO_ACTION); set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */ } @@ -556,16 +543,15 @@ static void act(struct floppy_state *fs) static void scan_timeout(struct timer_list *t) { struct floppy_state *fs = timer_container_of(fs, t, timeout); - struct swim3 __iomem *sw = fs->swim3; unsigned long flags; swim3_dbg("* scan timeout, state=%d\n", fs->state); spin_lock_irqsave(&swim3_lock, flags); fs->timeout_pending = 0; - out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); - out_8(&sw->select, RELAX); - out_8(&sw->intr_enable, 0); + swim3_out(fs, SW_CONTROL_BIC, DO_ACTION | WRITE_SECTORS); + swim3_out(fs, SW_SELECT, RELAX); + swim3_out(fs, SW_INTR_ENABLE, 0); fs->cur_cyl = -1; if (fs->retries > 5) { swim3_end_request(fs, BLK_STS_IOERR, 0); @@ -580,16 +566,15 @@ static void scan_timeout(struct timer_list *t) static void seek_timeout(struct timer_list *t) { struct floppy_state *fs = timer_container_of(fs, t, timeout); - struct swim3 __iomem *sw = fs->swim3; unsigned long flags; swim3_dbg("* seek timeout, state=%d\n", fs->state); spin_lock_irqsave(&swim3_lock, flags); fs->timeout_pending = 0; - out_8(&sw->control_bic, DO_SEEK); - out_8(&sw->select, RELAX); - out_8(&sw->intr_enable, 0); + swim3_out(fs, SW_CONTROL_BIC, DO_SEEK); + swim3_out(fs, SW_SELECT, RELAX); + swim3_out(fs, SW_INTR_ENABLE, 0); swim3_err("%s", "Seek timeout\n"); swim3_end_request(fs, BLK_STS_IOERR, 0); fs->state = idle; @@ -599,7 +584,6 @@ static void seek_timeout(struct timer_list *t) static void settle_timeout(struct timer_list *t) { struct floppy_state *fs = timer_container_of(fs, t, timeout); - struct swim3 __iomem *sw = fs->swim3; unsigned long flags; swim3_dbg("* settle timeout, state=%d\n", fs->state); @@ -607,12 +591,12 @@ static void settle_timeout(struct timer_list *t) spin_lock_irqsave(&swim3_lock, flags); fs->timeout_pending = 0; if (swim3_readbit(fs, SEEK_COMPLETE)) { - out_8(&sw->select, RELAX); + swim3_out(fs, SW_SELECT, RELAX); fs->state = locating; act(fs); goto unlock; } - out_8(&sw->select, RELAX); + swim3_out(fs, SW_SELECT, RELAX); if (fs->settle_time < 2*HZ) { ++fs->settle_time; set_timeout(fs, 1, settle_timeout); @@ -628,22 +612,16 @@ static void settle_timeout(struct timer_list *t) static void xfer_timeout(struct timer_list *t) { struct floppy_state *fs = timer_container_of(fs, t, timeout); - struct swim3 __iomem *sw = fs->swim3; - struct dbdma_regs __iomem *dr = fs->dma; unsigned long flags; - int n; swim3_dbg("* xfer timeout, state=%d\n", fs->state); spin_lock_irqsave(&swim3_lock, flags); fs->timeout_pending = 0; - out_le32(&dr->control, RUN << 16); - /* We must wait a bit for dbdma to stop */ - for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++) - udelay(1); - out_8(&sw->intr_enable, 0); - out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); - out_8(&sw->select, RELAX); + fs->dma_ops->abort(fs); + swim3_out(fs, SW_INTR_ENABLE, 0); + swim3_out(fs, SW_CONTROL_BIC, WRITE_SECTORS | DO_ACTION); + swim3_out(fs, SW_SELECT, RELAX); swim3_err("Timeout %sing sector %ld\n", (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"), (long)blk_rq_pos(fs->cur_req)); @@ -655,31 +633,28 @@ static void xfer_timeout(struct timer_list *t) static irqreturn_t swim3_interrupt(int irq, void *dev_id) { struct floppy_state *fs = (struct floppy_state *) dev_id; - struct swim3 __iomem *sw = fs->swim3; int intr, err, n; - int stat, resid; - struct dbdma_regs __iomem *dr; - struct dbdma_cmd *cp; + int resid; unsigned long flags; struct request *req = fs->cur_req; swim3_dbg("* interrupt, state=%d\n", fs->state); spin_lock_irqsave(&swim3_lock, flags); - intr = in_8(&sw->intr); - err = (intr & ERROR_INTR)? in_8(&sw->error): 0; + intr = swim3_in(fs, SW_INTR); + err = (intr & ERROR_INTR)? swim3_in(fs, SW_ERROR): 0; if ((intr & ERROR_INTR) && fs->state != do_transfer) swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n", fs->state, rq_data_dir(req), intr, err); switch (fs->state) { case locating: if (intr & SEEN_SECTOR) { - out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS); - out_8(&sw->select, RELAX); - out_8(&sw->intr_enable, 0); + swim3_out(fs, SW_CONTROL_BIC, DO_ACTION | WRITE_SECTORS); + swim3_out(fs, SW_SELECT, RELAX); + swim3_out(fs, SW_INTR_ENABLE, 0); timer_delete(&fs->timeout); fs->timeout_pending = 0; - if (sw->ctrack == 0xff) { + if (swim3_in(fs, SW_CTRACK) == 0xff) { swim3_err("%s", "Seen sector but cyl=ff?\n"); fs->cur_cyl = -1; if (fs->retries > 5) { @@ -691,8 +666,8 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id) } break; } - fs->cur_cyl = sw->ctrack; - fs->cur_sector = sw->csect; + fs->cur_cyl = swim3_in(fs, SW_CTRACK); + fs->cur_sector = swim3_in(fs, SW_CSECT); if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl) swim3_err("Expected cyl %d, got %d\n", fs->expect_cyl, fs->cur_cyl); @@ -702,10 +677,11 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id) break; case seeking: case jogging: - if (sw->nseek == 0) { - out_8(&sw->control_bic, DO_SEEK); - out_8(&sw->select, RELAX); - out_8(&sw->intr_enable, 0); + /* MkLinux waits for the interrupt, PCI Macs read back zero */ + if ((intr & SEEK_DONE) || swim3_in(fs, SW_NSEEK) == 0) { + swim3_out(fs, SW_CONTROL_BIC, DO_SEEK); + swim3_out(fs, SW_SELECT, RELAX); + swim3_out(fs, SW_INTR_ENABLE, 0); timer_delete(&fs->timeout); fs->timeout_pending = 0; if (fs->state == seeking) @@ -715,7 +691,7 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id) } break; case settling: - out_8(&sw->intr_enable, 0); + swim3_out(fs, SW_INTR_ENABLE, 0); timer_delete(&fs->timeout); fs->timeout_pending = 0; act(fs); @@ -723,35 +699,12 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id) case do_transfer: if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0) break; - out_8(&sw->intr_enable, 0); - out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION); - out_8(&sw->select, RELAX); + swim3_out(fs, SW_INTR_ENABLE, 0); + swim3_out(fs, SW_CONTROL_BIC, WRITE_SECTORS | DO_ACTION); + swim3_out(fs, SW_SELECT, RELAX); timer_delete(&fs->timeout); fs->timeout_pending = 0; - dr = fs->dma; - cp = fs->dma_cmd; - if (rq_data_dir(req) == WRITE) - ++cp; - /* - * Check that the main data transfer has finished. - * On writing, the swim3 sometimes doesn't use - * up all the bytes of the postamble, so we can still - * see DMA active here. That doesn't matter as long - * as all the sector data has been transferred. - */ - if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) { - /* wait a little while for DMA to complete */ - for (n = 0; n < 100; ++n) { - if (cp->xfer_status != 0) - break; - udelay(1); - barrier(); - } - } - /* turn off DMA */ - out_le32(&dr->control, (RUN | PAUSE) << 16); - stat = le16_to_cpu(cp->xfer_status); - resid = le16_to_cpu(cp->res_count); + resid = fs->dma_ops->finish(fs, req, intr & ERROR_INTR); if (intr & ERROR_INTR) { n = fs->scount - 1 - resid / 512; if (n > 0) { @@ -769,9 +722,8 @@ static irqreturn_t swim3_interrupt(int irq, void *dev_id) fs->state = idle; } } else { - if ((stat & ACTIVE) == 0 || resid != 0) { + if (resid) { /* musta been an error */ - swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid); swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n", fs->state, rq_data_dir(req), intr, err); swim3_end_request(fs, BLK_STS_IOERR, 0); @@ -891,8 +843,7 @@ static int floppy_locked_ioctl(struct block_device *bdev, blk_mode_t mode, if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)) return -EPERM; - if (fs->mdev->media_bay && - check_media_bay(fs->mdev->media_bay) != MB_FD) + if (swim3_bay_absent(fs)) return -ENXIO; switch (cmd) { @@ -924,19 +875,17 @@ static int floppy_ioctl(struct block_device *bdev, blk_mode_t mode, static int floppy_open(struct gendisk *disk, blk_mode_t mode) { struct floppy_state *fs = disk->private_data; - struct swim3 __iomem *sw = fs->swim3; int n, err = 0; if (fs->ref_count == 0) { - if (fs->mdev->media_bay && - check_media_bay(fs->mdev->media_bay) != MB_FD) + if (swim3_bay_absent(fs)) return -ENXIO; - out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2); - out_8(&sw->control_bic, 0xff); - out_8(&sw->mode, 0x95); + swim3_out(fs, SW_SETUP, S_IBM_DRIVE | S_FCLK_DIV2); + swim3_out(fs, SW_CONTROL_BIC, 0xff); + swim3_out(fs, SW_MODE, 0x95); udelay(10); - out_8(&sw->intr_enable, 0); - out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE); + swim3_out(fs, SW_INTR_ENABLE, 0); + swim3_out(fs, SW_CONTROL_BIS, DRIVE_ENABLE | INTR_ENABLE); swim3_action(fs, MOTOR_ON); fs->write_prot = -1; fs->cur_cyl = -1; @@ -977,7 +926,7 @@ static int floppy_open(struct gendisk *disk, blk_mode_t mode) if (err) { if (fs->ref_count == 0) { swim3_action(fs, MOTOR_OFF); - out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE); + swim3_out(fs, SW_CONTROL_BIC, DRIVE_ENABLE | INTR_ENABLE); swim3_select(fs, RELAX); } return err; @@ -1005,7 +954,6 @@ static int floppy_unlocked_open(struct gendisk *disk, blk_mode_t mode) static void floppy_release(struct gendisk *disk) { struct floppy_state *fs = disk->private_data; - struct swim3 __iomem *sw = fs->swim3; mutex_lock(&swim3_mutex); if (fs->ref_count > 0) @@ -1014,7 +962,7 @@ static void floppy_release(struct gendisk *disk) fs->ref_count = 0; if (fs->ref_count == 0) { swim3_action(fs, MOTOR_OFF); - out_8(&sw->control_bic, 0xff); + swim3_out(fs, SW_CONTROL_BIC, 0xff); swim3_select(fs, RELAX); } mutex_unlock(&swim3_mutex); @@ -1030,17 +978,14 @@ static unsigned int floppy_check_events(struct gendisk *disk, static int floppy_revalidate(struct gendisk *disk) { struct floppy_state *fs = disk->private_data; - struct swim3 __iomem *sw; int ret, n; - if (fs->mdev->media_bay && - check_media_bay(fs->mdev->media_bay) != MB_FD) + if (swim3_bay_absent(fs)) return -ENXIO; - sw = fs->swim3; grab_drive(fs, revalidating, 0); - out_8(&sw->intr_enable, 0); - out_8(&sw->control_bis, DRIVE_ENABLE); + swim3_out(fs, SW_INTR_ENABLE, 0); + swim3_out(fs, SW_CONTROL_BIS, DRIVE_ENABLE); swim3_action(fs, MOTOR_ON); /* necessary? */ fs->write_prot = -1; fs->cur_cyl = -1; @@ -1078,33 +1023,155 @@ static const struct blk_mq_ops swim3_mq_ops = { .queue_rq = swim3_queue_rq, }; +static void swim3_clear_intr(struct floppy_state *fs) +{ + swim3_out(fs, SW_INTR_ENABLE, 0); + swim3_in(fs, SW_INTR); + swim3_in(fs, SW_ERROR); +} + +static void swim3_init_state(struct floppy_state *fs) +{ + fs->state = idle; + fs->cur_cyl = -1; + fs->cur_sector = -1; + fs->secpercyl = 36; + fs->secpertrack = 18; + fs->total_secs = 2880; + init_waitqueue_head(&fs->wait); +} + +#ifdef CONFIG_PPC_PMAC +/* + * XXX: this is a horrible hack, but at least allows ppc32 to get + * out of defining virt_to_bus, and this driver out of using the + * deprecated block layer bounce buffering for highmem addresses + * for no good reason. + */ +static unsigned long swim3_phys_to_bus(phys_addr_t paddr) +{ + return paddr + PCI_DRAM_OFFSET; +} + +static phys_addr_t swim3_bio_phys(struct bio *bio) +{ + return page_to_phys(bio_page(bio)) + bio_offset(bio); +} + +static inline void init_dma(struct dbdma_cmd *cp, int cmd, + phys_addr_t paddr, int count) +{ + cp->req_count = cpu_to_le16(count); + cp->command = cpu_to_le16(cmd); + cp->phy_addr = cpu_to_le32(swim3_phys_to_bus(paddr)); + cp->xfer_status = 0; +} + + +static void swim3_dbdma_prepare(struct floppy_state *fs, struct request *req, + int n) +{ + struct dbdma_cmd *cp = fs->dma_cmd; + struct dbdma_regs __iomem *dr = fs->dma; + + out_le32(&dr->cmdptr, swim3_phys_to_bus(virt_to_phys(cp))); + if (rq_data_dir(req) == WRITE) { + /* Set up 3 dma commands: write preamble, data, postamble */ + init_dma(cp, OUTPUT_MORE, virt_to_phys(write_preamble), + sizeof(write_preamble)); + ++cp; + init_dma(cp, OUTPUT_MORE, swim3_bio_phys(req->bio), 512); + ++cp; + init_dma(cp, OUTPUT_LAST, virt_to_phys(write_postamble), + sizeof(write_postamble)); + } else { + init_dma(cp, INPUT_LAST, swim3_bio_phys(req->bio), n * 512); + } + ++cp; + out_le16(&cp->command, DBDMA_STOP); +} + +static void swim3_dbdma_start(struct floppy_state *fs, bool write) +{ + out_le32(&fs->dma->control, (RUN << 16) | RUN); +} + +static int swim3_dbdma_finish(struct floppy_state *fs, struct request *req, + bool error) +{ + struct dbdma_cmd *cp = fs->dma_cmd; + int n, stat, resid; + + if (rq_data_dir(req) == WRITE) + ++cp; + /* + * Check that the main data transfer has finished. + * On writing, the swim3 sometimes doesn't use + * up all the bytes of the postamble, so we can still + * see DMA active here. That doesn't matter as long + * as all the sector data has been transferred. + */ + if (!error && cp->xfer_status == 0) { + /* wait a little while for DMA to complete */ + for (n = 0; n < 100; ++n) { + if (cp->xfer_status != 0) + break; + udelay(1); + barrier(); + } + } + /* turn off DMA */ + out_le32(&fs->dma->control, (RUN | PAUSE) << 16); + stat = le16_to_cpu(cp->xfer_status); + resid = le16_to_cpu(cp->res_count); + if (!error && ((stat & ACTIVE) == 0 || resid != 0)) { + swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid); + return -EIO; + } + return resid; +} + +static void swim3_dbdma_abort(struct floppy_state *fs) +{ + struct dbdma_regs __iomem *dr = fs->dma; + int n; + + out_le32(&dr->control, RUN << 16); + /* We must wait a bit for dbdma to stop */ + for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++) + udelay(1); +} + +static const struct swim3_dma_ops swim3_dbdma_ops = { + .prepare = swim3_dbdma_prepare, + .start = swim3_dbdma_start, + .finish = swim3_dbdma_finish, + .abort = swim3_dbdma_abort, +}; + static void swim3_mb_event(struct macio_dev* mdev, int mb_state) { struct floppy_state *fs = macio_get_drvdata(mdev); - struct swim3 __iomem *sw; if (!fs) return; - sw = fs->swim3; - if (mb_state != MB_FD) return; /* Clear state */ - out_8(&sw->intr_enable, 0); - in_8(&sw->intr); - in_8(&sw->error); + swim3_clear_intr(fs); } -static int swim3_add_device(struct macio_dev *mdev, int index) +static int swim3_add_device(struct floppy_state *fs, void *data) { + struct macio_dev *mdev = data; struct device_node *swim = mdev->ofdev.dev.of_node; - struct floppy_state *fs = &floppy_states[index]; int rc = -EBUSY; fs->mdev = mdev; - fs->index = index; + fs->reg_shift = 4; + fs->dma_ops = &swim3_dbdma_ops; /* Check & Request resources */ if (macio_resource_count(mdev) < 2) { @@ -1129,9 +1196,7 @@ static int swim3_add_device(struct macio_dev *mdev, int index) if (mdev->media_bay == NULL) pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1); - fs->state = idle; - fs->swim3 = (struct swim3 __iomem *) - ioremap(macio_resource_start(mdev, 0), 0x200); + fs->swim3 = ioremap(macio_resource_start(mdev, 0), 0x200); if (fs->swim3 == NULL) { swim3_err("%s", "Couldn't map mmio registers\n"); rc = -ENOMEM; @@ -1147,12 +1212,7 @@ static int swim3_add_device(struct macio_dev *mdev, int index) } fs->swim3_intr = macio_irq(mdev, 0); fs->dma_intr = macio_irq(mdev, 1); - fs->cur_cyl = -1; - fs->cur_sector = -1; - fs->secpercyl = 36; - fs->secpertrack = 18; - fs->total_secs = 2880; - init_waitqueue_head(&fs->wait); + swim3_init_state(fs); fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space); memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd)); @@ -1184,9 +1244,146 @@ static int swim3_add_device(struct macio_dev *mdev, int index) return rc; } +#endif /* CONFIG_PPC_PMAC */ -static int swim3_attach(struct macio_dev *mdev, - const struct of_device_id *match) +#ifdef CONFIG_PPC_NUBUS_PMAC +/* + * AMIC floppy DMA of the PDM (MkLinux floppy_amic.c, its register bits are + * marked as guesses there, and MAME macpdm.cpp). It transfers to and from + * the floppy part of the AMIC DMA area, the upper half of its address + * follows the AMIC DMA base. The DMA interrupt is not used, the SWIM3 + * interrupt ends a transfer. + */ +#define AMIC_FD_ADDR 0x0 +#define AMIC_FD_COUNT 0x4 +#define AMIC_FD_CTRL 0x8 +#define AMIC_FD_DONE 0x80 /* write 1 to clear */ +#define AMIC_FD_WRITE 0x40 /* memory to SWIM3 */ +#define AMIC_FD_RUN 0x02 +#define AMIC_FD_RESET 0x01 + +static void swim3_amic_copy(struct floppy_state *fs, struct request *req, + unsigned int off, unsigned int len, bool to_dma) +{ + struct bio_vec bv = bio_iovec(req->bio); + void *p = bvec_kmap_local(&bv); + + if (to_dma) + memcpy_toio(fs->amic_buf.cpu + off, p, len); + else + memcpy_fromio(p, fs->amic_buf.cpu + off, len); + kunmap_local(p); +} + +static void swim3_amic_prepare(struct floppy_state *fs, struct request *req, + int n) +{ + void __iomem *buf = fs->amic_buf.cpu; + unsigned int i, len = n * 512; + + if (rq_data_dir(req) == WRITE) { + memcpy_toio(buf, write_preamble, sizeof(write_preamble)); + swim3_amic_copy(fs, req, sizeof(write_preamble), 512, true); + memcpy_toio(buf + sizeof(write_preamble) + 512, write_postamble, + sizeof(write_postamble)); + len = sizeof(write_preamble) + 512 + sizeof(write_postamble); + } + out_8(fs->amic + AMIC_FD_CTRL, AMIC_FD_RESET); + /* byte wise, as MkLinux programs it */ + for (i = 0; i < 4; i++) + out_8(fs->amic + AMIC_FD_ADDR + i, + (u32)fs->amic_buf.dma >> (24 - 8 * i)); + out_8(fs->amic + AMIC_FD_COUNT, len >> 8); + out_8(fs->amic + AMIC_FD_COUNT + 1, len & 0xff); +} + +static void swim3_amic_start(struct floppy_state *fs, bool write) +{ + out_8(fs->amic + AMIC_FD_CTRL, AMIC_FD_RUN | (write ? AMIC_FD_WRITE : 0)); +} + +static void swim3_amic_abort(struct floppy_state *fs) +{ + int n; + + out_8(fs->amic + AMIC_FD_CTRL, 0); + for (n = 0; (in_8(fs->amic + AMIC_FD_CTRL) & AMIC_FD_RUN) && n < 1000; n++) + udelay(1); + out_8(fs->amic + AMIC_FD_CTRL, AMIC_FD_DONE); +} + +static int swim3_amic_finish(struct floppy_state *fs, struct request *req, + bool error) +{ + int resid, len; + + swim3_amic_abort(fs); + resid = in_8(fs->amic + AMIC_FD_COUNT) << 8 | + in_8(fs->amic + AMIC_FD_COUNT + 1); + if (rq_data_dir(req) == WRITE) { + /* the SWIM3 stops reading at the end of the postamble */ + resid = clamp_t(int, resid - (int)sizeof(write_postamble), 0, 512); + } else { + len = (fs->scount * 512 - resid) & ~511; + if (len > 0) + swim3_amic_copy(fs, req, 0, len, false); + } + if (!error && resid) { + swim3_err("fd dma error: resid=%d\n", resid); + return -EIO; + } + return resid; +} + +static const struct swim3_dma_ops swim3_amic_ops = { + .prepare = swim3_amic_prepare, + .start = swim3_amic_start, + .finish = swim3_amic_finish, + .abort = swim3_amic_abort, +}; + +static int swim3_amic_add_device(struct floppy_state *fs, void *data) +{ + struct platform_device *pdev = data; + struct device *dev = &pdev->dev; + int rc; + + fs->reg_shift = 9; + fs->dma_ops = &swim3_amic_ops; + fs->swim3 = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(fs->swim3)) + return PTR_ERR(fs->swim3); + fs->amic = devm_platform_ioremap_resource(pdev, 1); + if (IS_ERR(fs->amic)) + return PTR_ERR(fs->amic); + swim3_init_state(fs); + rc = devm_amic_dma_request(dev, AMIC_DMA_FLOPPY, &fs->amic_buf); + if (rc) + return dev_err_probe(dev, rc, "no floppy DMA buffer\n"); + if (fs->amic_buf.size < fs->secpertrack * 512) + return -ENOMEM; + fs->swim3_intr = platform_get_irq(pdev, 0); + if (fs->swim3_intr < 0) + return fs->swim3_intr; + + swim3_amic_abort(fs); + swim3_clear_intr(fs); + rc = devm_request_irq(dev, fs->swim3_intr, swim3_interrupt, 0, "SWIM3", + fs); + if (rc) + return rc; + timer_setup(&fs->timeout, NULL, 0); + platform_set_drvdata(pdev, fs); + + swim3_info("%s", "SWIM3 floppy controller with AMIC DMA\n"); + return 0; +} +#endif /* CONFIG_PPC_NUBUS_PMAC */ + +static int swim3_attach_common(struct device *dev, + int (*add_device)(struct floppy_state *fs, + void *data), + void *data) { struct queue_limits lim = { .features = BLK_FEAT_ROTATIONAL, @@ -1206,6 +1403,8 @@ static int swim3_attach(struct macio_dev *mdev, fs = &floppy_states[floppy_count]; memset(fs, 0, sizeof(*fs)); + fs->dev = dev; + fs->index = floppy_count; rc = blk_mq_alloc_sq_tag_set(&fs->tag_set, &swim3_mq_ops, 2, 0); if (rc) @@ -1217,7 +1416,7 @@ static int swim3_attach(struct macio_dev *mdev, goto out_free_tag_set; } - rc = swim3_add_device(mdev, floppy_count); + rc = add_device(fs, data); if (rc) goto out_cleanup_disk; @@ -1247,6 +1446,13 @@ static int swim3_attach(struct macio_dev *mdev, return rc; } +#ifdef CONFIG_PPC_PMAC +static int swim3_attach(struct macio_dev *mdev, + const struct of_device_id *match) +{ + return swim3_attach_common(&mdev->ofdev.dev, swim3_add_device, mdev); +} + static const struct of_device_id swim3_match[] = { { @@ -1278,9 +1484,38 @@ static struct macio_driver swim3_driver = }; +#endif /* CONFIG_PPC_PMAC */ + +#ifdef CONFIG_PPC_NUBUS_PMAC +static int swim3_amic_probe(struct platform_device *pdev) +{ + return swim3_attach_common(&pdev->dev, swim3_amic_add_device, pdev); +} + +static const struct of_device_id swim3_amic_match[] = { + { .compatible = "apple,amic-swim3" }, + { } +}; +MODULE_DEVICE_TABLE(of, swim3_amic_match); + +static struct platform_driver swim3_amic_driver = { + .probe = swim3_amic_probe, + .driver = { + .name = "swim3-amic", + .of_match_table = swim3_amic_match, + .suppress_bind_attrs = true, + }, +}; +#endif /* CONFIG_PPC_NUBUS_PMAC */ + static int swim3_init(void) { +#ifdef CONFIG_PPC_PMAC macio_register_driver(&swim3_driver); +#endif +#ifdef CONFIG_PPC_NUBUS_PMAC + platform_driver_register(&swim3_amic_driver); +#endif return 0; } diff --git a/drivers/macintosh/Kconfig b/drivers/macintosh/Kconfig index d00e713c1092..659d8777ae31 100644 --- a/drivers/macintosh/Kconfig +++ b/drivers/macintosh/Kconfig @@ -14,7 +14,7 @@ if MACINTOSH_DRIVERS config ADB bool "Apple Desktop Bus (ADB) support" - depends on MAC || (PPC_PMAC && PPC32) + depends on MAC || (PPC_PMAC && PPC32) || PPC_NUBUS_PMAC help Apple Desktop Bus (ADB) support is for support of devices which are connected to an ADB port. ADB devices tend to have 4 pins. @@ -43,7 +43,7 @@ config ADB_IOP # we want to change this to something like CONFIG_SYSCTRL_CUDA/PMU config ADB_CUDA bool "Support for Cuda/Egret based Macs and PowerMacs" - depends on (ADB || PPC_PMAC) && !PPC_PMAC64 + depends on (ADB || PPC_PMAC || PPC_NUBUS_PMAC) && !PPC_PMAC64 select RTC_LIB help This provides support for Cuda/Egret based Macintosh and @@ -69,6 +69,9 @@ config ADB_PMU this device; you should do so if your machine is one of those mentioned above. + The NuBus PowerBooks 5300, 1400, 2300 and 500 PPC (M2) are + supported with the PowerMac platform (PPC_PMAC) enabled. + config ADB_PMU_EVENT def_bool y depends on ADB_PMU && INPUT=y diff --git a/drivers/macintosh/adb.c b/drivers/macintosh/adb.c index fe150125e099..26697f69a765 100644 --- a/drivers/macintosh/adb.c +++ b/drivers/macintosh/adb.c @@ -4,6 +4,7 @@ * and the /dev/adb device on macintoshes. * * Copyright (C) 1996 Paul Mackerras. + * Copyright (C) 2026 René Rebe * * Modified to declare controllers as structures, added * client notification of bus reset and handles PowerBook @@ -312,7 +313,8 @@ static int __init adb_init(void) int i; #ifdef CONFIG_PPC32 - if (!machine_is(chrp) && !machine_is(powermac)) + if (!machine_is(chrp) && !machine_is(powermac) && + !machine_is(nubus_pmac)) return 0; #endif #ifdef CONFIG_MAC @@ -342,7 +344,8 @@ static int __init adb_init(void) } else { #ifdef CONFIG_PPC if (of_machine_is_compatible("AAPL,PowerBook1998") || - of_machine_is_compatible("PowerBook1,1")) + of_machine_is_compatible("PowerBook1,1") || + of_machine_is_compatible("apple,m2")) sleepy_trackpad = 1; #endif /* CONFIG_PPC */ diff --git a/drivers/macintosh/adbhid.c b/drivers/macintosh/adbhid.c index 6155da58091b..8d3b1d0f668b 100644 --- a/drivers/macintosh/adbhid.c +++ b/drivers/macintosh/adbhid.c @@ -9,6 +9,7 @@ * with considerable contributions from Ben Herrenschmidt and others. * * Copyright (C) 2000 Franz Sirl. + * Copyright (C) 2026 René Rebe * * Adapted to ADB changes and support for more devices by * Benjamin Herrenschmidt. Adapted from code in MkLinux @@ -1251,7 +1252,8 @@ init_ms_a3(int id) static int __init adbhid_init(void) { #ifndef CONFIG_MAC - if (!machine_is(chrp) && !machine_is(powermac)) + if (!machine_is(chrp) && !machine_is(powermac) && + !machine_is(nubus_pmac)) return 0; #endif diff --git a/drivers/macintosh/via-pmu.c b/drivers/macintosh/via-pmu.c index 8c652c15018c..1aafd26ae4ab 100644 --- a/drivers/macintosh/via-pmu.c +++ b/drivers/macintosh/via-pmu.c @@ -12,6 +12,7 @@ * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi. * Copyright (C) 2001-2002 Benjamin Herrenschmidt * Copyright (C) 2006-2007 Johannes Berg + * Copyright (C) 2026 René Rebe * * THIS DRIVER IS BECOMING A TOTAL MESS ! * - Cleanup atomically disabling reply to PMU events after @@ -100,8 +101,11 @@ static DEFINE_MUTEX(pmu_info_proc_mutex); /* Bits in B data register: both active low */ #ifdef CONFIG_PPC_PMAC -#define TACK 0x08 /* Transfer acknowledge (input) */ -#define TREQ 0x10 /* Transfer request (output) */ +/* the M2 PowerBooks use the bits of the 68k PowerBooks */ +static u8 pmu_tack __ro_after_init = 0x08; +static u8 pmu_treq __ro_after_init = 0x10; +#define TACK pmu_tack /* Transfer acknowledge (input) */ +#define TREQ pmu_treq /* Transfer request (output) */ #else #define TACK 0x02 #define TREQ 0x04 @@ -155,6 +159,11 @@ static volatile unsigned char __iomem *via1; static volatile unsigned char __iomem *via2; static struct device_node *vias; static struct device_node *gpio_node; +/* M2: the interrupt controller dispatches the VIA1 flags individually */ +static unsigned int pmu_sr_irq, pmu_cb1_irq; +#else +#define pmu_sr_irq IRQ_MAC_ADB_SR +#define pmu_cb1_irq IRQ_MAC_ADB_CL #endif static unsigned char __iomem *gpio_reg; static int gpio_irq = 0; @@ -280,7 +289,8 @@ static char *pbook_type[] = { "PowerBook 2400/3400/3500(G3)", "PowerBook G3 Series", "1999 PowerBook G3", - "Core99" + "Core99", + [PMU_M2_BASED] = "PowerBook 5300/1400/2300/500 PPC (M2)", }; int __init find_via_pmu(void) @@ -310,8 +320,10 @@ int __init find_via_pmu(void) PMU_INT_ADB | PMU_INT_TICK; - if (of_node_name_eq(vias->parent, "ohare") || - of_device_is_compatible(vias->parent, "ohare")) + if (of_device_is_compatible(vias, "apple,m2-pmu")) + pmu_kind = PMU_M2_BASED; + else if (of_node_name_eq(vias->parent, "ohare") || + of_device_is_compatible(vias->parent, "ohare")) pmu_kind = PMU_OHARE_BASED; else if (of_device_is_compatible(vias->parent, "paddington")) pmu_kind = PMU_PADDINGTON_BASED; @@ -345,14 +357,30 @@ int __init find_via_pmu(void) } else pmu_kind = PMU_UNKNOWN; - via1 = via2 = ioremap(taddr, 0x2000); + if (pmu_kind == PMU_M2_BASED) { + pmu_sr_irq = irq_of_parse_and_map(vias, 0); + pmu_cb1_irq = irq_of_parse_and_map(vias, 1); + if (!pmu_sr_irq || !pmu_cb1_irq) { + printk(KERN_ERR "via-pmu: Can't map interrupts !\n"); + goto fail; + } + /* handshake on VIA2 port B, VIA1 IER/IFR owned by the PIC */ + via1 = ioremap(taddr, 0x4000); + via2 = via1 ? via1 + 0x2000 : NULL; + pmu_tack = 0x02; + pmu_treq = 0x04; + } else { + via1 = via2 = ioremap(taddr, 0x2000); + } if (via1 == NULL) { printk(KERN_ERR "via-pmu: Can't map address !\n"); goto fail_via_remap; } - - out_8(&via1[IER], IER_CLR | 0x7f); /* disable all intrs */ - out_8(&via1[IFR], 0x7f); /* clear IFR */ + + if (pmu_kind != PMU_M2_BASED) { + out_8(&via1[IER], IER_CLR | 0x7f); /* disable all intrs */ + out_8(&via1[IFR], 0x7f); /* clear IFR */ + } pmu_state = idle; @@ -372,6 +400,7 @@ int __init find_via_pmu(void) fail: of_node_put(vias); vias = NULL; + pmu_sr_irq = pmu_cb1_irq = 0; pmu_state = uninitialized; return 0; #else @@ -410,23 +439,11 @@ static int pmu_init(void) } #endif /* CONFIG_ADB */ -/* - * We can't wait until pmu_init gets called, that happens too late. - * It happens after IDE and SCSI initialization, which can take a few - * seconds, and by that time the PMU could have given up on us and - * turned us off. - * Thus this is called with arch_initcall rather than device_initcall. - */ -static int __init via_pmu_start(void) +#ifdef CONFIG_PPC_PMAC +static int __init via_pmu_request_irq(void) { - unsigned int __maybe_unused irq; - - if (pmu_state == uninitialized) - return -ENODEV; - - batt_req.complete = 1; + unsigned int irq; -#ifdef CONFIG_PPC_PMAC irq = irq_of_parse_and_map(vias, 0); if (!irq) { printk(KERN_ERR "via-pmu: can't map interrupt\n"); @@ -463,19 +480,55 @@ static int __init via_pmu_start(void) /* Enable interrupts */ out_8(&via1[IER], IER_SET | SR_INT | CB1_INT); + + return 0; +} #else - if (request_irq(IRQ_MAC_ADB_SR, via_pmu_interrupt, IRQF_NO_SUSPEND, +static inline int via_pmu_request_irq(void) +{ + return -ENODEV; +} +#endif /* !CONFIG_PPC_PMAC */ + +/* 68k PowerBooks and M2: the SR and CB1 flags are separate interrupts */ +static int __init via_pmu_request_split_irqs(void) +{ + if (request_irq(pmu_sr_irq, via_pmu_interrupt, IRQF_NO_SUSPEND, "VIA-PMU-SR", NULL)) { pr_err("%s: couldn't get SR irq\n", __func__); return -ENODEV; } - if (request_irq(IRQ_MAC_ADB_CL, via_pmu_interrupt, IRQF_NO_SUSPEND, + if (request_irq(pmu_cb1_irq, via_pmu_interrupt, IRQF_NO_SUSPEND, "VIA-PMU-CL", NULL)) { pr_err("%s: couldn't get CL irq\n", __func__); - free_irq(IRQ_MAC_ADB_SR, NULL); + free_irq(pmu_sr_irq, NULL); return -ENODEV; } -#endif /* !CONFIG_PPC_PMAC */ + return 0; +} + +/* + * We can't wait until pmu_init gets called, that happens too late. + * It happens after IDE and SCSI initialization, which can take a few + * seconds, and by that time the PMU could have given up on us and + * turned us off. + * Thus this is called with arch_initcall rather than device_initcall. + */ +static int __init via_pmu_start(void) +{ + int err; + + if (pmu_state == uninitialized) + return -ENODEV; + + batt_req.complete = 1; + + if (pmu_sr_irq) + err = via_pmu_request_split_irqs(); + else + err = via_pmu_request_irq(); + if (err) + return err; pmu_fully_inited = 1; @@ -574,7 +627,9 @@ init_pmu(void) /* Negate TREQ. Set TACK to input and TREQ to output. */ out_8(&via2[B], in_8(&via2[B]) | TREQ); - out_8(&via2[DIRB], (in_8(&via2[DIRB]) | TREQ) & ~TACK); + /* M2: the pseudo VIA2 port B is used as set up, like MkLinux does */ + if (pmu_kind != PMU_M2_BASED) + out_8(&via2[DIRB], (in_8(&via2[DIRB]) | TREQ) & ~TACK); pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask); timeout = 100000; @@ -1583,25 +1638,19 @@ via_pmu_interrupt(int irq, void *arg) ++disable_poll; for (;;) { - /* On 68k Macs, VIA interrupts are dispatched individually. - * Unless we are polling, the relevant IRQ flag has already - * been cleared. + /* On 68k Macs and the M2 PowerBooks, VIA interrupts are + * dispatched individually. Unless we are polling, the + * relevant IRQ flag has already been cleared. */ intr = 0; - if (IS_ENABLED(CONFIG_PPC_PMAC) || !irq) { + if (!pmu_sr_irq || !irq) { intr = in_8(&via1[IFR]) & (SR_INT | CB1_INT); out_8(&via1[IFR], intr); - } -#ifndef CONFIG_PPC_PMAC - switch (irq) { - case IRQ_MAC_ADB_CL: + } else if (irq == pmu_cb1_irq) { intr = CB1_INT; - break; - case IRQ_MAC_ADB_SR: + } else if (irq == pmu_sr_irq) { intr = SR_INT; - break; } -#endif if (intr == 0) break; handled = 1; @@ -1620,9 +1669,8 @@ via_pmu_interrupt(int irq, void *arg) if (req) break; } -#ifndef CONFIG_PPC_PMAC - break; -#endif + if (pmu_sr_irq) + break; } recheck: diff --git a/drivers/net/ethernet/8390/Kconfig b/drivers/net/ethernet/8390/Kconfig index 57d73a7747ec..3e9655e44d26 100644 --- a/drivers/net/ethernet/8390/Kconfig +++ b/drivers/net/ethernet/8390/Kconfig @@ -67,7 +67,7 @@ config ARM_ETHERH config MAC8390 tristate "Macintosh NS 8390 based ethernet cards" - depends on MAC + depends on NUBUS select CRC32 help If you want to include a driver to support Nubus or LC-PDS diff --git a/drivers/net/ethernet/8390/mac8390.c b/drivers/net/ethernet/8390/mac8390.c index 4a0a095a1a8a..2353e4139b59 100644 --- a/drivers/net/ethernet/8390/mac8390.c +++ b/drivers/net/ethernet/8390/mac8390.c @@ -14,6 +14,8 @@ * by rayk@knightsmanor.org */ /* 2002-12-30: Try to support more cards, some clues from NetBSD driver */ /* 2003-12-26: Make sure Asante cards always work. */ +/* 2026: NuBus Power Macintosh support, + * Copyright (C) 2026 René Rebe */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt @@ -37,16 +39,15 @@ #include #include -#include static char version[] = "v0.4 2001-05-15 David Huggins-Daines and others\n"; #define EI_SHIFT(x) (ei_local->reg_offset[x]) -#define ei_inb(port) in_8(port) -#define ei_outb(val, port) out_8(port, val) -#define ei_inb_p(port) in_8(port) -#define ei_outb_p(val, port) out_8(port, val) +#define ei_inb(port) in_8((u8 __iomem *)(port)) +#define ei_outb(val, port) out_8((u8 __iomem *)(port), val) +#define ei_inb_p(port) in_8((u8 __iomem *)(port)) +#define ei_outb_p(val, port) out_8((u8 __iomem *)(port), val) #include "lib8390.c" @@ -294,7 +295,7 @@ static bool mac8390_rsrc_init(struct net_device *dev, dev->irq = SLOT2IRQ(board->slot); /* This is getting to be a habit */ - dev->base_addr = board->slot_addr | ((board->slot & 0xf) << 20); + dev->base_addr = board->slot_addr + ((board->slot & 0xf) << 20); /* * Get some Nubus info - we will trust the card's idea @@ -637,7 +638,8 @@ static void mac8390_no_reset(struct net_device *dev) static void interlan_reset(struct net_device *dev) { - unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq)); + unsigned char *target = (unsigned char *)dev->base_addr - + INTERLAN_8390_BASE; struct ei_device *ei_local = netdev_priv(dev); netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...", diff --git a/drivers/net/ethernet/apple/Kconfig b/drivers/net/ethernet/apple/Kconfig index a4176d2ecec6..9553d66bfe43 100644 --- a/drivers/net/ethernet/apple/Kconfig +++ b/drivers/net/ethernet/apple/Kconfig @@ -6,7 +6,7 @@ config NET_VENDOR_APPLE bool "Apple devices" default y - depends on (PPC_PMAC && PPC32) || MAC + depends on (PPC_PMAC && PPC32) || MAC || PPC_NUBUS_PMAC help If you have a network (Ethernet) card belonging to this class, say Y. @@ -60,4 +60,15 @@ config MACMACE the 660AV and 840AV Macintosh. If you have one of these Macintoshes say Y here. +config AMIC_MACE + tristate "PDM Power Macintosh onboard MACE ethernet" + depends on PPC_NUBUS_PMAC + select CRC32 + help + Support for the onboard AMD 79C940 MACE Ethernet controller with + AMIC DMA of the NuBus Power Macintosh 6100, 7100 and 8100. + + To compile this driver as a module, choose M here: the module + will be called amic_mace. + endif # NET_VENDOR_APPLE diff --git a/drivers/net/ethernet/apple/Makefile b/drivers/net/ethernet/apple/Makefile index 322457027546..d41b24cc8f38 100644 --- a/drivers/net/ethernet/apple/Makefile +++ b/drivers/net/ethernet/apple/Makefile @@ -6,3 +6,4 @@ obj-$(CONFIG_MACE) += mace.o obj-$(CONFIG_BMAC) += bmac.o obj-$(CONFIG_MACMACE) += macmace.o +obj-$(CONFIG_AMIC_MACE) += amic_mace.o diff --git a/drivers/net/ethernet/apple/amic_mace.c b/drivers/net/ethernet/apple/amic_mace.c new file mode 100644 index 000000000000..4e9008cb7ea8 --- /dev/null +++ b/drivers/net/ethernet/apple/amic_mace.c @@ -0,0 +1,665 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Am79C940 MACE Ethernet with AMIC DMA of the PDM Power Macintosh + * (6100, 7100, 8100). + * + * Receive DMA fills a ring of 256 byte pages in the AMIC DMA area: every + * frame starts on a page with an 8 byte header holding the MACE receive + * status, the AMIC advances the head page, the driver the tail page. + * Transmit DMA sends from two fixed 2 KB buffers; like MkLinux only one of + * them is used, MkLinux reports lock ups with both. Register usage + * follows MkLinux (osfmk if_mace.c, enet_dma.h) and MAME (amic_enet.cpp). + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "mace.h" + +#define DRV_NAME "amic-mace" + +/* AMIC Ethernet DMA registers, relative to the "rxdma" block */ +#define RCVCS 0x00 +#define RCVHP 0x08 +#define RCVTP 0x0c +#define XMTBC_HI(set) (0x1c + 0x10 * (set)) +#define XMTBC_LO(set) (0x1d + 0x10 * (set)) + +/* receive and transmit DMA control/status */ +#define DMA_IF 0x80 +#define DMA_OVERRUN 0x40 +#define DMA_IE 0x08 +#define DMA_RUN 0x02 +#define DMA_RST 0x01 + +#define RX_PAGE 256 +#define RX_HDR 8 +#define TX_SET 1 +#define TX_SET_SIZE 0x800 + +#define PROM_STRIDE 0x10 +#define PROM_BYTES 8 + +#define BROKEN_ADDRCHG_REV 0x0941 +#define TX_TIMEOUT (2 * HZ) + +struct amic_mace { + struct net_device *ndev; + struct mace __iomem *mace; + u8 __iomem *prom; + u8 __iomem *txdma; + u8 __iomem *rxdma; + struct amic_dma_buf rx, tx; + unsigned int rx_pages; + unsigned int tx_len; + bool rx_overrun; /* stopped by OVERRUN, draining */ + u16 chipid; + spinlock_t lock; /* MACE and DMA registers, transmit buffer */ + struct napi_struct napi; + struct work_struct reset_work; +}; + +static int amic_mace_read_prom(struct amic_mace *mp, u8 *addr) +{ + u8 v, sum = 0; + int i; + + /* bit reversed bytes, all eight XOR to 0xff */ + for (i = 0; i < PROM_BYTES; i++) { + v = bitrev8(in_8(mp->prom + 1 + i * PROM_STRIDE)); + if (i < ETH_ALEN) + addr[i] = v; + sum ^= v; + } + return sum == 0xff ? 0 : -EIO; +} + +/* load the physical address or the multicast filter */ +static void amic_mace_load_addr(struct amic_mace *mp, u8 which, const u8 *p) +{ + struct mace __iomem *mb = mp->mace; + int i, n = which == PHYADDR ? ETH_ALEN : 8; + + if (mp->chipid == BROKEN_ADDRCHG_REV) { + out_8(&mb->iac, which); + } else { + out_8(&mb->iac, ADDRCHG | which); + /* pending while a frame is being received */ + for (i = 0; i < 2000 && (in_8(&mb->iac) & ADDRCHG); i++) + udelay(1); + } + for (i = 0; i < n; i++) + out_8(which == PHYADDR ? &mb->padr : &mb->ladrf, p[i]); + if (mp->chipid != BROKEN_ADDRCHG_REV) + out_8(&mb->iac, 0); +} + +/* call with the lock held, the caller restores MACCC */ +static void __amic_mace_set_rx_mode(struct amic_mace *mp) +{ + struct net_device *ndev = mp->ndev; + struct netdev_hw_addr *ha; + u8 filter[8] = {}; + u32 crc; + + if (ndev->flags & IFF_ALLMULTI) { + memset(filter, 0xff, sizeof(filter)); + } else { + netdev_for_each_mc_addr(ha, ndev) { + crc = ether_crc_le(ETH_ALEN, ha->addr) >> 26; + filter[crc >> 3] |= BIT(crc & 7); + } + } + amic_mace_load_addr(mp, LOGADDR, filter); +} + +static u8 amic_mace_maccc(struct amic_mace *mp) +{ + return ENXMT | ENRCV | (mp->ndev->flags & IFF_PROMISC ? PROM : 0); +} + +static void amic_mace_set_rx_mode(struct net_device *ndev) +{ + struct amic_mace *mp = netdev_priv(ndev); + unsigned long flags; + + if (!netif_running(ndev)) + return; + spin_lock_irqsave(&mp->lock, flags); + __amic_mace_set_rx_mode(mp); + out_8(&mp->mace->maccc, amic_mace_maccc(mp)); + spin_unlock_irqrestore(&mp->lock, flags); +} + +static int amic_mace_set_mac_address(struct net_device *ndev, void *addr) +{ + struct amic_mace *mp = netdev_priv(ndev); + unsigned long flags; + int ret; + + ret = eth_mac_addr(ndev, addr); + if (ret || !netif_running(ndev)) + return ret; + + spin_lock_irqsave(&mp->lock, flags); + amic_mace_load_addr(mp, PHYADDR, ndev->dev_addr); + out_8(&mp->mace->maccc, amic_mace_maccc(mp)); + spin_unlock_irqrestore(&mp->lock, flags); + return 0; +} + +static u8 amic_mace_utr(struct amic_mace *mp) +{ + return RTRD | (mp->ndev->features & NETIF_F_LOOPBACK ? LOOP_INT : 0); +} + +static void amic_mace_reset(struct amic_mace *mp) +{ + struct mace __iomem *mb = mp->mace; + int i; + + out_8(mp->rxdma + RCVCS, DMA_RST); + out_8(mp->txdma, DMA_RST); + + out_8(&mb->maccc, 0); + out_8(&mb->biucc, SWRST); + for (i = 0; i < 200 && (in_8(&mb->biucc) & SWRST); i++) + udelay(10); + if (i == 200) + netdev_err(mp->ndev, "MACE does not leave reset\n"); + out_8(&mb->imr, 0xff); + in_8(&mb->ir); +} + +/* call with the lock held */ +static void amic_mace_hw_init(struct amic_mace *mp) +{ + struct mace __iomem *mb = mp->mace; + u8 __iomem *rx = mp->rxdma; + + amic_mace_reset(mp); + + out_8(&mb->utr, amic_mace_utr(mp)); + out_8(&mb->biucc, XMTSP_64); + out_8(&mb->xmtfc, AUTO_PAD_XMIT); + out_8(&mb->rcvfc, 0); + out_8(&mb->fifocc, XMTFW_16 | RCVFW_64 | XMTFWU | RCVFWU); + out_8(&mb->plscc, PORTSEL_AUI); + + amic_mace_load_addr(mp, PHYADDR, mp->ndev->dev_addr); + __amic_mace_set_rx_mode(mp); + + out_8(rx + RCVCS, DMA_IF | DMA_OVERRUN); + out_8(mp->txdma, DMA_IF); + out_8(rx + RCVTP, in_8(rx + RCVHP)); + mp->rx_overrun = false; + out_8(rx + RCVCS, DMA_RUN | DMA_IE); + + out_8(&mb->maccc, amic_mace_maccc(mp)); + /* receive is signalled by the DMA */ + out_8(&mb->imr, RCVINT); +} + +/* restart the receiver, MkLinux does this on any bad receive status */ +static void amic_mace_rx_reset(struct amic_mace *mp) +{ + struct mace __iomem *mb = mp->mace; + u8 __iomem *rx = mp->rxdma; + unsigned long flags; + u8 maccc; + + spin_lock_irqsave(&mp->lock, flags); + maccc = in_8(&mb->maccc); + out_8(&mb->maccc, maccc & ~ENRCV); + out_8(rx + RCVCS, DMA_RST); + out_8(&mb->fifocc, (in_8(&mb->fifocc) & ~XMTFWU) | RCVFWU); + out_8(rx + RCVCS, DMA_IF | DMA_OVERRUN); + out_8(rx + RCVTP, in_8(rx + RCVHP)); + mp->rx_overrun = false; + out_8(&mb->maccc, maccc); + out_8(rx + RCVCS, DMA_RUN); + spin_unlock_irqrestore(&mp->lock, flags); +} + +static void amic_mace_rx_error(struct net_device *ndev, u16 status) +{ + DEV_STATS_INC(ndev, rx_errors); + if (status & RS_OFLO) + DEV_STATS_INC(ndev, rx_fifo_errors); + if (status & RS_CLSN) + DEV_STATS_INC(ndev, collisions); + if (status & RS_FRAMERR) + DEV_STATS_INC(ndev, rx_frame_errors); + if (status & RS_FCSERR) + DEV_STATS_INC(ndev, rx_crc_errors); + if (!(status & (RS_OFLO | RS_CLSN | RS_FRAMERR | RS_FCSERR))) + DEV_STATS_INC(ndev, rx_length_errors); +} + +static int amic_mace_rx(struct amic_mace *mp, int budget, bool full) +{ + struct net_device *ndev = mp->ndev; + unsigned int ring = mp->rx_pages * RX_PAGE; + unsigned int tail, off, len, first; + struct sk_buff *skb; + int done = 0; + u16 status; + + tail = in_8(mp->rxdma + RCVTP); + /* with OVERRUN set head == tail means the ring is full */ + while (done < budget && (full || tail != in_8(mp->rxdma + RCVHP))) { + full = false; + off = tail * RX_PAGE; + status = tail < mp->rx_pages ? in_be16(mp->rx.cpu + off) : 0; + len = status & RS_COUNT; + if ((status & ~RS_COUNT) || len < ETH_HLEN + ETH_FCS_LEN) { + if (net_ratelimit()) + netdev_dbg(ndev, "receive status %04x, restarting\n", + status); + amic_mace_rx_error(ndev, status); + amic_mace_rx_reset(mp); + break; + } + + len -= ETH_FCS_LEN; + skb = napi_alloc_skb(&mp->napi, len); + if (skb) { + /* the frame may wrap at the end of the ring */ + off += RX_HDR; + first = min(len, ring - off); + memcpy_fromio(skb_put(skb, len), mp->rx.cpu + off, first); + if (len > first) + memcpy_fromio(skb->data + first, mp->rx.cpu, + len - first); + skb->protocol = eth_type_trans(skb, ndev); + DEV_STATS_INC(ndev, rx_packets); + DEV_STATS_ADD(ndev, rx_bytes, len); + napi_gro_receive(&mp->napi, skb); + } else { + DEV_STATS_INC(ndev, rx_dropped); + } + + tail = (tail + DIV_ROUND_UP(RX_HDR + len + ETH_FCS_LEN, + RX_PAGE)) % mp->rx_pages; + out_8(mp->rxdma + RCVTP, tail); + done++; + } + return done; +} + +static int amic_mace_poll(struct napi_struct *napi, int budget) +{ + struct amic_mace *mp = container_of(napi, struct amic_mace, napi); + u8 cs = in_8(mp->rxdma + RCVCS); + bool full = false; + int done; + + /* head == tail is a full ring only before the first frame is taken */ + if ((cs & DMA_OVERRUN) && !mp->rx_overrun) { + mp->rx_overrun = true; + full = true; + } + done = amic_mace_rx(mp, budget, full); + if (done < budget && napi_complete_done(napi, done)) { + /* the AMIC stops on a full ring, resume after it was drained */ + cs = in_8(mp->rxdma + RCVCS) & DMA_OVERRUN; + if (cs) + DEV_STATS_INC(mp->ndev, rx_over_errors); + mp->rx_overrun = false; + out_8(mp->rxdma + RCVCS, cs | DMA_RUN | DMA_IE); + } + return done; +} + +static irqreturn_t amic_mace_rxdma_irq(int irq, void *dev_id) +{ + struct amic_mace *mp = dev_id; + u8 cs = in_8(mp->rxdma + RCVCS); + + if (!(cs & DMA_IF) || !(cs & DMA_IE)) + return IRQ_NONE; + + /* acknowledge, interrupts stay off until the ring is drained */ + out_8(mp->rxdma + RCVCS, DMA_IF | (cs & DMA_RUN)); + napi_schedule(&mp->napi); + return IRQ_HANDLED; +} + +static irqreturn_t amic_mace_txdma_irq(int irq, void *dev_id) +{ + struct amic_mace *mp = dev_id; + struct net_device *ndev = mp->ndev; + u8 cs; + + spin_lock(&mp->lock); + cs = in_8(mp->txdma); + if (!(cs & DMA_IF) || !(cs & DMA_IE)) { + spin_unlock(&mp->lock); + return IRQ_NONE; + } + + /* the frame is in the MACE FIFO, the buffer is free again */ + out_8(mp->txdma, DMA_IF); + out_8(mp->rxdma + XMTBC_HI(TX_SET), 0); + out_8(mp->rxdma + XMTBC_LO(TX_SET), 0); + DEV_STATS_INC(ndev, tx_packets); + DEV_STATS_ADD(ndev, tx_bytes, mp->tx_len); + netif_wake_queue(ndev); + spin_unlock(&mp->lock); + return IRQ_HANDLED; +} + +static void amic_mace_tx_status(struct net_device *ndev, u8 fs, u8 rc) +{ + if (fs & (ONE | MORE)) + DEV_STATS_ADD(ndev, collisions, rc & RETRY_MASK); + if (!(fs & (UFLO | LCOL | LCAR | RTRY))) + return; + + DEV_STATS_INC(ndev, tx_errors); + if (fs & LCAR) + DEV_STATS_INC(ndev, tx_carrier_errors); + if (fs & LCOL) + DEV_STATS_INC(ndev, tx_window_errors); + if (fs & RTRY) + DEV_STATS_INC(ndev, tx_aborted_errors); + if (fs & UFLO) + DEV_STATS_INC(ndev, tx_fifo_errors); +} + +static irqreturn_t amic_mace_irq(int irq, void *dev_id) +{ + struct amic_mace *mp = dev_id; + struct net_device *ndev = mp->ndev; + struct mace __iomem *mb = mp->mace; + u8 ir, fs, rc; + + spin_lock(&mp->lock); + ir = in_8(&mb->ir) & ~in_8(&mb->imr); + if (!ir) { + spin_unlock(&mp->lock); + return IRQ_NONE; + } + + /* the counters clear on read */ + if (ir & MPCO) + DEV_STATS_ADD(ndev, rx_missed_errors, 256); + DEV_STATS_ADD(ndev, rx_missed_errors, in_8(&mb->mpc)); + if (ir & RNTPCO) + DEV_STATS_ADD(ndev, rx_length_errors, 256); + DEV_STATS_ADD(ndev, rx_length_errors, in_8(&mb->rntpc)); + if (ir & RCVCCO) + DEV_STATS_ADD(ndev, collisions, 256); + DEV_STATS_ADD(ndev, collisions, in_8(&mb->rcvcc)); + if (ir & CERR) + DEV_STATS_INC(ndev, tx_heartbeat_errors); + if (ir & (BABBLE | JABBER) && net_ratelimit()) + netdev_warn(ndev, "%s\n", ir & BABBLE ? "babbling transmitter" : + "jabbering transceiver"); + + /* the MACE holds the status of two frames, read XMTRC first */ + while (in_8(&mb->pr) & XMTSV) { + rc = in_8(&mb->xmtrc); + fs = in_8(&mb->xmtfs); + amic_mace_tx_status(ndev, fs, rc); + } + spin_unlock(&mp->lock); + return IRQ_HANDLED; +} + +static netdev_tx_t amic_mace_start_xmit(struct sk_buff *skb, + struct net_device *ndev) +{ + struct amic_mace *mp = netdev_priv(ndev); + unsigned long flags; + unsigned int len; + + if (skb_put_padto(skb, ETH_ZLEN)) { + DEV_STATS_INC(ndev, tx_dropped); + return NETDEV_TX_OK; + } + len = skb->len; + if (len > TX_SET_SIZE) { + DEV_STATS_INC(ndev, tx_dropped); + dev_kfree_skb_any(skb); + return NETDEV_TX_OK; + } + + spin_lock_irqsave(&mp->lock, flags); + netif_stop_queue(ndev); + memcpy_toio(mp->tx.cpu + TX_SET * TX_SET_SIZE, skb->data, len); + out_8(mp->rxdma + XMTBC_HI(TX_SET), len >> 8); + out_8(mp->rxdma + XMTBC_LO(TX_SET), len & 0xff); + mp->tx_len = len; + out_8(mp->txdma, DMA_RUN | DMA_IE); + spin_unlock_irqrestore(&mp->lock, flags); + + skb_tx_timestamp(skb); + dev_consume_skb_any(skb); + return NETDEV_TX_OK; +} + +static int amic_mace_open(struct net_device *ndev) +{ + struct amic_mace *mp = netdev_priv(ndev); + unsigned long flags; + + napi_enable(&mp->napi); + spin_lock_irqsave(&mp->lock, flags); + amic_mace_hw_init(mp); + spin_unlock_irqrestore(&mp->lock, flags); + + /* the AAUI port has no link indication */ + netif_carrier_on(ndev); + netif_start_queue(ndev); + return 0; +} + +static int amic_mace_stop(struct net_device *ndev) +{ + struct amic_mace *mp = netdev_priv(ndev); + unsigned long flags; + + netif_stop_queue(ndev); + napi_disable(&mp->napi); + spin_lock_irqsave(&mp->lock, flags); + amic_mace_reset(mp); + spin_unlock_irqrestore(&mp->lock, flags); + netif_carrier_off(ndev); + return 0; +} + +static void amic_mace_reset_work(struct work_struct *work) +{ + struct amic_mace *mp = container_of(work, struct amic_mace, + reset_work); + struct net_device *ndev = mp->ndev; + unsigned long flags; + + rtnl_lock(); + if (netif_running(ndev)) { + netif_tx_disable(ndev); + napi_disable(&mp->napi); + spin_lock_irqsave(&mp->lock, flags); + amic_mace_hw_init(mp); + spin_unlock_irqrestore(&mp->lock, flags); + napi_enable(&mp->napi); + netif_wake_queue(ndev); + } + rtnl_unlock(); +} + +static void amic_mace_tx_timeout(struct net_device *ndev, unsigned int txq) +{ + struct amic_mace *mp = netdev_priv(ndev); + + netdev_err(ndev, "transmit timeout (DMA %02x, FIFO frames %02x), resetting\n", + in_8(mp->txdma), in_8(&mp->mace->fifofc)); + DEV_STATS_INC(ndev, tx_errors); + schedule_work(&mp->reset_work); +} + +static int amic_mace_set_features(struct net_device *ndev, + netdev_features_t features) +{ + struct amic_mace *mp = netdev_priv(ndev); + struct mace __iomem *mb = mp->mace; + unsigned long flags; + u8 maccc; + + if (!((ndev->features ^ features) & NETIF_F_LOOPBACK)) + return 0; + + ndev->features = features; + if (!netif_running(ndev)) + return 0; + + spin_lock_irqsave(&mp->lock, flags); + maccc = in_8(&mb->maccc); + out_8(&mb->maccc, 0); + out_8(&mb->utr, amic_mace_utr(mp)); + out_8(&mb->maccc, maccc); + spin_unlock_irqrestore(&mp->lock, flags); + return 0; +} + +static const struct net_device_ops amic_mace_netdev_ops = { + .ndo_open = amic_mace_open, + .ndo_stop = amic_mace_stop, + .ndo_start_xmit = amic_mace_start_xmit, + .ndo_tx_timeout = amic_mace_tx_timeout, + .ndo_set_rx_mode = amic_mace_set_rx_mode, + .ndo_set_mac_address = amic_mace_set_mac_address, + .ndo_set_features = amic_mace_set_features, + .ndo_validate_addr = eth_validate_addr, +}; + +static int amic_mace_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + static const char * const irq_names[] = { "mace", "rxdma", "txdma" }; + static const irq_handler_t handlers[] = { + amic_mace_irq, amic_mace_rxdma_irq, amic_mace_txdma_irq + }; + struct net_device *ndev; + struct amic_mace *mp; + u8 addr[ETH_ALEN]; + int i, irq, ret; + + ndev = devm_alloc_etherdev(dev, sizeof(*mp)); + if (!ndev) + return -ENOMEM; + SET_NETDEV_DEV(ndev, dev); + mp = netdev_priv(ndev); + mp->ndev = ndev; + spin_lock_init(&mp->lock); + INIT_WORK(&mp->reset_work, amic_mace_reset_work); + + mp->mace = devm_platform_ioremap_resource_byname(pdev, "mace"); + if (IS_ERR((void __force *)mp->mace)) + return PTR_ERR((void __force *)mp->mace); + mp->prom = devm_platform_ioremap_resource_byname(pdev, "prom"); + if (IS_ERR(mp->prom)) + return PTR_ERR(mp->prom); + mp->txdma = devm_platform_ioremap_resource_byname(pdev, "txdma"); + if (IS_ERR(mp->txdma)) + return PTR_ERR(mp->txdma); + mp->rxdma = devm_platform_ioremap_resource_byname(pdev, "rxdma"); + if (IS_ERR(mp->rxdma)) + return PTR_ERR(mp->rxdma); + + ret = devm_amic_dma_request(dev, AMIC_DMA_ENET_RX, &mp->rx); + if (ret) + return dev_err_probe(dev, ret, "no receive DMA buffer\n"); + ret = devm_amic_dma_request(dev, AMIC_DMA_ENET_TX, &mp->tx); + if (ret) + return dev_err_probe(dev, ret, "no transmit DMA buffer\n"); + mp->rx_pages = mp->rx.size / RX_PAGE; + + mp->chipid = in_8(&mp->mace->chipid_hi) << 8 | + in_8(&mp->mace->chipid_lo); + + if (amic_mace_read_prom(mp, addr) || !is_valid_ether_addr(addr)) { + dev_warn(dev, "invalid address PROM %pM, using a random address\n", + addr); + eth_hw_addr_random(ndev); + } else { + eth_hw_addr_set(ndev, addr); + } + + amic_mace_reset(mp); + + for (i = 0; i < ARRAY_SIZE(irq_names); i++) { + irq = platform_get_irq_byname(pdev, irq_names[i]); + if (irq < 0) + return irq; + ret = devm_request_irq(dev, irq, handlers[i], 0, + dev_name(dev), mp); + if (ret) + return ret; + if (!i) + ndev->irq = irq; + } + + ndev->netdev_ops = &amic_mace_netdev_ops; + ndev->watchdog_timeo = TX_TIMEOUT; + ndev->hw_features = NETIF_F_LOOPBACK; + ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE; + netif_napi_add(ndev, &mp->napi, amic_mace_poll); + netif_carrier_off(ndev); + + platform_set_drvdata(pdev, ndev); + ret = register_netdev(ndev); + if (ret) { + netif_napi_del(&mp->napi); + return ret; + } + + netdev_info(ndev, "MACE %04x, address %pM, DMA rx %pad tx %pad\n", + mp->chipid, ndev->dev_addr, &mp->rx.dma, &mp->tx.dma); + return 0; +} + +static void amic_mace_remove(struct platform_device *pdev) +{ + struct net_device *ndev = platform_get_drvdata(pdev); + struct amic_mace *mp = netdev_priv(ndev); + + unregister_netdev(ndev); + cancel_work_sync(&mp->reset_work); + netif_napi_del(&mp->napi); +} + +static const struct of_device_id amic_mace_of_match[] = { + { .compatible = "apple,amic-mace" }, + { } +}; +MODULE_DEVICE_TABLE(of, amic_mace_of_match); + +static struct platform_driver amic_mace_driver = { + .probe = amic_mace_probe, + .remove = amic_mace_remove, + .driver = { + .name = DRV_NAME, + .of_match_table = amic_mace_of_match, + }, +}; +module_platform_driver(amic_mace_driver); + +MODULE_AUTHOR("René Rebe "); +MODULE_DESCRIPTION("PDM Power Macintosh MACE Ethernet with AMIC DMA"); +MODULE_LICENSE("GPL"); diff --git a/drivers/nubus/Kconfig b/drivers/nubus/Kconfig new file mode 100644 index 000000000000..6b6cbf78a9bf --- /dev/null +++ b/drivers/nubus/Kconfig @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2026 René Rebe + +config NUBUS + bool "NuBus support" if PPC_NUBUS_PMAC + depends on MAC || PPC_NUBUS_PMAC + default y + help + Support for the NuBus expansion slots of the 68k Macintosh and of + the NuBus Power Macintosh, required by NuBus card drivers. diff --git a/drivers/nubus/bus.c b/drivers/nubus/bus.c index b7d417385d98..051d0b0c711a 100644 --- a/drivers/nubus/bus.c +++ b/drivers/nubus/bus.c @@ -3,6 +3,7 @@ // Bus implementation for the NuBus subsystem. // // Copyright (C) 2017 Finn Thain +// Copyright (C) 2026 René Rebe #include #include @@ -67,6 +68,7 @@ static void nubus_device_release(struct device *dev) list_del(&fres->list); kfree(fres); } + nubus_unmap_slot((void *)board->slot_addr); kfree(board); } diff --git a/drivers/nubus/nubus.c b/drivers/nubus/nubus.c index 40ce4991c356..83ed4dbbb4a5 100644 --- a/drivers/nubus/nubus.c +++ b/drivers/nubus/nubus.c @@ -6,6 +6,8 @@ * * Mostly rewritten by David Huggins-Daines, C. Scott Ananian, * and others. + * + * Copyright (C) 2026 René Rebe */ #include @@ -17,7 +19,6 @@ #include #include #include -#include #include #include @@ -86,7 +87,8 @@ static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map) v <<= 8; while (not_useful(p, map)) p++; - v |= *p++; + v |= nubus_readb(p); + p++; len--; } *ptr = p; @@ -119,16 +121,15 @@ static void nubus_advance(unsigned char **ptr, int len, int map) *ptr = p; } -static void nubus_move(unsigned char **ptr, int len, int map) +static void nubus_move(unsigned char **ptr, int len, int map, + const struct nubus_board *board) { - unsigned long slot_space = (unsigned long)*ptr & 0xFF000000; - if (len > 0) nubus_advance(ptr, len, map); else if (len < 0) nubus_rewind(ptr, -len, map); - if (((unsigned long)*ptr & 0xFF000000) != slot_space) + if ((unsigned long)*ptr - board->slot_addr >= NUBUS_SLOT_SIZE) pr_err("%s: moved out of slot address space!\n", __func__); } @@ -146,13 +147,13 @@ static inline long nubus_expand32(long foo) return foo; } -static inline void *nubus_rom_addr(int slot) +static inline unsigned char *nubus_rom_addr(void *slot_addr) { /* * Returns the first byte after the card. We then walk * backwards to get the lane register and the config */ - return (void *)(0xF1000000 + (slot << 24)); + return (unsigned char *)slot_addr + NUBUS_SLOT_SIZE; } unsigned char *nubus_dirptr(const struct nubus_dirent *nd) @@ -161,7 +162,7 @@ unsigned char *nubus_dirptr(const struct nubus_dirent *nd) /* Essentially, just step over the bytelanes using whatever offset we might have found */ - nubus_move(&p, nubus_expand32(nd->data), nd->mask); + nubus_move(&p, nubus_expand32(nd->data), nd->mask, nd->board); /* And return the value */ return p; } @@ -231,6 +232,7 @@ int nubus_get_root_dir(const struct nubus_board *board, dir->ptr = dir->base = board->directory; dir->done = 0; dir->mask = board->lanes; + dir->board = board; return 0; } EXPORT_SYMBOL(nubus_get_root_dir); @@ -241,6 +243,7 @@ int nubus_get_func_dir(const struct nubus_rsrc *fres, struct nubus_dir *dir) dir->ptr = dir->base = fres->directory; dir->done = 0; dir->mask = fres->board->lanes; + dir->board = fres->board; return 0; } EXPORT_SYMBOL(nubus_get_func_dir); @@ -253,6 +256,7 @@ int nubus_get_board_dir(const struct nubus_board *board, dir->ptr = dir->base = board->directory; dir->done = 0; dir->mask = board->lanes; + dir->board = board; /* Now dereference it (the first directory is always the board directory) */ @@ -270,6 +274,7 @@ int nubus_get_subdir(const struct nubus_dirent *ent, dir->ptr = dir->base = nubus_dirptr(ent); dir->done = 0; dir->mask = ent->mask; + dir->board = ent->board; return 0; } EXPORT_SYMBOL(nubus_get_subdir); @@ -299,6 +304,7 @@ int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent) /* Low 3 bytes might contain data (or might not) */ ent->data = resid & 0xffffff; ent->mask = nd->mask; + ent->board = nd->board; return 0; } EXPORT_SYMBOL(nubus_readdir); @@ -723,7 +729,7 @@ static int __init nubus_get_board_resource(struct nubus_board *board, int slot, return 0; } -static void __init nubus_add_board(int slot, int bytelanes) +static void __init nubus_add_board(int slot, int bytelanes, void *slot_addr) { struct nubus_board *board; unsigned char *rp; @@ -733,12 +739,14 @@ static void __init nubus_add_board(int slot, int bytelanes) int prev_resid = -1; /* Move to the start of the format block */ - rp = nubus_rom_addr(slot); + rp = nubus_rom_addr(slot_addr); nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes); /* Actually we should probably panic if this fails */ - if ((board = kzalloc_obj(*board, GFP_ATOMIC)) == NULL) + if ((board = kzalloc_obj(*board, GFP_ATOMIC)) == NULL) { + nubus_unmap_slot(slot_addr); return; + } board->fblock = rp; /* Dump the format block for debugging purposes */ @@ -754,7 +762,7 @@ static void __init nubus_add_board(int slot, int bytelanes) rp = board->fblock; board->slot = slot; - board->slot_addr = (unsigned long)nubus_slot_addr(slot); + board->slot_addr = (unsigned long)slot_addr; board->doffset = nubus_get_rom(&rp, 4, bytelanes); /* rom_length is *supposed* to be the total length of the * ROM. In practice it is the "amount of ROM used to compute @@ -785,7 +793,7 @@ static void __init nubus_add_board(int slot, int bytelanes) /* Set up the directory pointer */ board->directory = board->fblock; nubus_move(&board->directory, nubus_expand32(board->doffset), - board->lanes); + board->lanes, board); nubus_get_root_dir(board, &dir); @@ -802,6 +810,7 @@ static void __init nubus_add_board(int slot, int bytelanes) /* We can't have this! */ pr_err("Slot %X: Board resource not found!\n", slot); kfree(board); + nubus_unmap_slot(slot_addr); return; } @@ -839,15 +848,20 @@ static void __init nubus_probe_slot(int slot) { unsigned char dp; unsigned char *rp; + void *slot_addr; int i; - rp = nubus_rom_addr(slot); + slot_addr = nubus_map_slot(slot); + if (!slot_addr) + return; + + rp = nubus_rom_addr(slot_addr); for (i = 4; i; i--) { rp--; if (!hwreg_present(rp)) continue; - dp = *rp; + dp = nubus_readb(rp); /* The last byte of the format block consists of two nybbles which are "mirror images" of each other. @@ -860,10 +874,11 @@ static void __init nubus_probe_slot(int slot) continue; /* Looks promising. Let's put it on the list. */ - nubus_add_board(slot, dp); + nubus_add_board(slot, dp, slot_addr); return; } + nubus_unmap_slot(slot_addr); } static void __init nubus_scan_bus(void) @@ -878,7 +893,7 @@ static void __init nubus_scan_bus(void) static int __init nubus_init(void) { - if (!MACH_IS_MAC) + if (!nubus_present()) return 0; nubus_proc_init(); diff --git a/drivers/nubus/proc.c b/drivers/nubus/proc.c index 3ea3d1738eae..bd990021be30 100644 --- a/drivers/nubus/proc.c +++ b/drivers/nubus/proc.c @@ -16,6 +16,8 @@ resources are empty files, since every resource ID requires a special case (e.g. if the resource ID implies a directory or block, then its value has to be interpreted as a slot ROM pointer etc.). + + Copyright (C) 2026 René Rebe */ #include @@ -89,10 +91,12 @@ struct proc_dir_entry *nubus_proc_add_rsrc_dir(struct proc_dir_entry *procdir, struct nubus_proc_pde_data { unsigned char *res_ptr; unsigned int res_size; + const struct nubus_board *board; }; static struct nubus_proc_pde_data * -nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size) +nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size, + const struct nubus_board *board) { struct nubus_proc_pde_data *pded; @@ -102,6 +106,7 @@ nubus_proc_alloc_pde_data(unsigned char *ptr, unsigned int size) pded->res_ptr = ptr; pded->res_size = size; + pded->board = board; return pded; } @@ -127,6 +132,7 @@ static int nubus_proc_rsrc_show(struct seq_file *m, void *v) ent.mask = lanes; ent.base = pded->res_ptr; ent.data = 0; + ent.board = pded->board; nubus_seq_write_rsrc_mem(m, &ent, pded->res_size); } else { unsigned int data = (unsigned int)pded->res_ptr; @@ -162,7 +168,8 @@ void nubus_proc_add_rsrc_mem(struct proc_dir_entry *procdir, snprintf(name, sizeof(name), "%x", ent->type); if (size) - pded = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size); + pded = nubus_proc_alloc_pde_data(nubus_dirptr(ent), size, + ent->board); else pded = NULL; remove_proc_subtree(name, procdir); @@ -183,7 +190,7 @@ void nubus_proc_add_rsrc(struct proc_dir_entry *procdir, remove_proc_subtree(name, procdir); proc_create_data(name, S_IFREG | 0444, procdir, &nubus_rsrc_proc_ops, - nubus_proc_alloc_pde_data(data, 0)); + nubus_proc_alloc_pde_data(data, 0, NULL)); } /* diff --git a/drivers/scsi/Kconfig b/drivers/scsi/Kconfig index 1eec66195cf4..f3dc52414656 100644 --- a/drivers/scsi/Kconfig +++ b/drivers/scsi/Kconfig @@ -1387,13 +1387,13 @@ config ATARI_SCSI config MAC_SCSI tristate "Macintosh NCR5380 SCSI" - depends on MAC && SCSI + depends on (MAC || PPC_NUBUS_PMAC) && SCSI select SCSI_SPI_ATTRS help This is the NCR 5380 SCSI controller included on most of the 68030 - based Macintoshes. If you have one of these say Y and read the - SCSI-HOWTO, available from - . + based Macintoshes and on the NuBus PowerBooks with a PowerPC (M2). + If you have one of these say Y and read the SCSI-HOWTO, available + from . config SCSI_MAC_ESP tristate "Macintosh NCR53c9[46] SCSI" @@ -1407,6 +1407,30 @@ config SCSI_MAC_ESP To compile this driver as a module, choose M here: the module will be called mac_esp. +config SCSI_AMIC_ESP + tristate "NuBus Power Macintosh 53C94/53CF94 SCSI" + depends on PPC_NUBUS_PMAC && SCSI + select SCSI_SPI_ATTRS + select SCSI_ESP_PIO + help + The NCR 53C94 and 53CF94 SCSI controllers with AMIC DMA of the + NuBus Power Macintosh 6100, 7100 and 8100. + + To compile this driver as a module, choose M here: the module + will be called amic_esp. + +config SCSI_PERFORMA_ESP + tristate "Performa 5200/6200 53C96 SCSI" + depends on PPC_NUBUS_PMAC && SCSI + select SCSI_SPI_ATTRS + select SCSI_ESP_PIO + help + The NCR 53C96 SCSI controller with pseudo DMA of the Power + Macintosh and Performa 5200/6200 family. + + To compile this driver as a module, choose M here: the module + will be called performa_esp. + config MVME147_SCSI bool "WD33C93 SCSI driver for MVME147" depends on MVME147 && SCSI=y diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile index 5b9328dc3b62..9d68a13b4e8d 100644 --- a/drivers/scsi/Makefile +++ b/drivers/scsi/Makefile @@ -61,6 +61,8 @@ obj-$(CONFIG_SGIWD93_SCSI) += sgiwd93.o wd33c93.o obj-$(CONFIG_ATARI_SCSI) += atari_scsi.o obj-$(CONFIG_MAC_SCSI) += mac_scsi.o obj-$(CONFIG_SCSI_MAC_ESP) += esp_scsi.o mac_esp.o +obj-$(CONFIG_SCSI_AMIC_ESP) += esp_scsi.o amic_esp.o +obj-$(CONFIG_SCSI_PERFORMA_ESP) += esp_scsi.o performa_esp.o obj-$(CONFIG_SUN3_SCSI) += sun3_scsi.o sun3_scsi_vme.o obj-$(CONFIG_MVME16x_SCSI) += 53c700.o mvme16x_scsi.o obj-$(CONFIG_BVME6000_SCSI) += 53c700.o bvme6000_scsi.o diff --git a/drivers/scsi/amic_esp.c b/drivers/scsi/amic_esp.c new file mode 100644 index 000000000000..71d78bb571f5 --- /dev/null +++ b/drivers/scsi/amic_esp.c @@ -0,0 +1,392 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ESP front-end for the NCR 53C94 and 53CF94 SCSI controllers of the NuBus + * Power Macintosh PDM family (6100, 7100, 8100) using the AMIC DMA channels. + * + * Based on the MkLinux osfmk scsi_amic.c and scsi_53C94_hdw.c. The AMIC + * moves 16-bit words from the 53C9x into a 64-bit buffer and writes whole + * quadwords to 8-byte aligned memory; a partially filled quadword is only + * written out by a flush, again as a whole quadword. Transfers therefore go + * through a per controller bounce buffer, which also keeps the caches in sync + * as MkLinux treats AMIC DMA as not cache coherent. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include "esp_scsi.h" + +#define DRV_MODULE_NAME "amic_esp" + +/* AMIC SCSI DMA control, from the big-endian bit numbers of MkLinux */ +#define AMIC_DMA_RESET 0x01 +#define AMIC_DMA_RUN 0x02 +#define AMIC_DMA_FLUSH 0x10 +#define AMIC_DMA_DIR 0x40 /* memory to SCSI */ + +#define AMIC_DMA_TIMEOUT_US 10000 +#define AMIC_ESP_BOUNCE_SIZE SZ_64K +/* MkLinux keeps the 16-bit transfer count clear of the 0 = 64K encoding */ +#define AMIC_ESP_MAX_XFER (SZ_64K - SZ_4K) + +/* DMA config 3 of MkLinux: threshold 8, alternate DMA, save residual byte */ +#define AMIC_ESP_DMA_CFG3 (ESP_CONFIG3_TEM | ESP_CONFIG3_ADMA | \ + ESP_CONFIG3_SRB) + +struct amic_esp_priv { + struct esp *esp; + void __iomem *dma_base; + void __iomem *dma_ctrl; + void *bounce; + dma_addr_t bounce_dma; + u8 *buf; + u32 len; + bool to_mem; + bool error; +}; + +static bool dma = true; +module_param(dma, bool, 0444); +MODULE_PARM_DESC(dma, "use AMIC DMA (default), otherwise programmed I/O"); + +static inline struct amic_esp_priv *amic_esp_priv(struct esp *esp) +{ + return dev_get_drvdata(esp->dev); +} + +static void amic_esp_write8(struct esp *esp, u8 val, unsigned long reg) +{ + out_8(esp->regs + reg * 16, val); +} + +static u8 amic_esp_read8(struct esp *esp, unsigned long reg) +{ + return in_8(esp->regs + reg * 16); +} + +#define esp_read8(REG) amic_esp_read8(esp, REG) +#define esp_write8(VAL, REG) amic_esp_write8(esp, VAL, REG) + +static int amic_esp_irq_pending(struct esp *esp) +{ + return !!(esp_read8(ESP_STATUS) & ESP_STAT_INTR); +} + +static u32 amic_esp_dma_length_limit(struct esp *esp, u32 dma_addr, + u32 dma_len) +{ + return min_t(u32, dma_len, AMIC_ESP_MAX_XFER); +} + +static int amic_dma_wait_clear(struct amic_esp_priv *p, u8 bits) +{ + int i; + + for (i = 0; i < AMIC_DMA_TIMEOUT_US; i++) { + if (!(in_8(p->dma_ctrl) & bits)) + return 0; + udelay(1); + } + dev_err(p->esp->dev, "AMIC DMA timeout, ctrl %02x\n", + in_8(p->dma_ctrl)); + p->error = true; + return -ETIMEDOUT; +} + +static void amic_dma_stop(struct amic_esp_priv *p) +{ + out_8(p->dma_ctrl, in_8(p->dma_ctrl) & ~AMIC_DMA_RUN); + amic_dma_wait_clear(p, AMIC_DMA_RUN); +} + +static void amic_esp_reset_dma(struct esp *esp) +{ + struct amic_esp_priv *p = amic_esp_priv(esp); + + out_8(p->dma_ctrl, AMIC_DMA_RESET); + amic_dma_wait_clear(p, AMIC_DMA_RUN); + p->len = 0; +} + +static void amic_esp_dma_drain(struct esp *esp) +{ + struct amic_esp_priv *p = amic_esp_priv(esp); + u32 resid = 0, fifo, done; + + if (!p->len || !p->to_mem) + return; + + /* write out the partially filled quadword */ + out_8(p->dma_ctrl, in_8(p->dma_ctrl) | AMIC_DMA_FLUSH); + amic_dma_wait_clear(p, AMIC_DMA_FLUSH); + amic_dma_stop(p); + + if (!(esp->sreg & ESP_STAT_TCNT)) + resid = esp_read8(ESP_TCLOW) | (esp_read8(ESP_TCMED) << 8); + resid = min(resid, p->len); + fifo = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES; + done = p->len - resid - min(fifo, p->len - resid); + + flush_dcache_range((unsigned long)p->bounce, + (unsigned long)p->bounce + ALIGN(done, 8)); + memcpy(p->buf, p->bounce, done); + + /* an odd last byte stays in the FIFO with 16-bit DMA */ + while (done < p->len - resid) + p->buf[done++] = esp_read8(ESP_FDATA); + p->len = 0; +} + +static void amic_esp_dma_invalidate(struct esp *esp) +{ + struct amic_esp_priv *p = amic_esp_priv(esp); + + amic_dma_stop(p); + esp_write8(esp->prev_cfg3, ESP_CFG3); + p->len = 0; +} + +static void amic_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count, + u32 dma_count, int write, u8 cmd) +{ + struct amic_esp_priv *p = amic_esp_priv(esp); + u32 phys = p->bounce_dma; + + p->buf = (u8 *)(uintptr_t)addr; + p->len = dma_count; + p->to_mem = write; + p->error = false; + + if (!write) + memcpy(p->bounce, p->buf, dma_count); + /* write back the data to send, drop cached lines over received data */ + flush_dcache_range((unsigned long)p->bounce, + (unsigned long)p->bounce + ALIGN(dma_count, 8)); + + out_8(p->dma_ctrl, in_8(p->dma_ctrl) | AMIC_DMA_RESET); + amic_dma_wait_clear(p, AMIC_DMA_RUN); + out_8(p->dma_base + 0, phys >> 24); + out_8(p->dma_base + 1, phys >> 16); + out_8(p->dma_base + 2, phys >> 8); + out_8(p->dma_base + 3, phys); + /* run before the command so the FIFO drains while still in data phase */ + out_8(p->dma_ctrl, write ? AMIC_DMA_RUN : AMIC_DMA_DIR | AMIC_DMA_RUN); + + esp_write8(esp->prev_cfg3 | AMIC_ESP_DMA_CFG3, ESP_CFG3); + esp_write8(esp_count & 0xff, ESP_TCLOW); + esp_write8((esp_count >> 8) & 0xff, ESP_TCMED); + scsi_esp_cmd(esp, cmd); +} + +static int amic_esp_dma_error(struct esp *esp) +{ + return amic_esp_priv(esp)->error; +} + +static const struct esp_driver_ops amic_esp_dma_ops = { + .esp_write8 = amic_esp_write8, + .esp_read8 = amic_esp_read8, + .irq_pending = amic_esp_irq_pending, + .dma_length_limit = amic_esp_dma_length_limit, + .reset_dma = amic_esp_reset_dma, + .dma_drain = amic_esp_dma_drain, + .dma_invalidate = amic_esp_dma_invalidate, + .send_dma_cmd = amic_esp_send_dma_cmd, + .dma_error = amic_esp_dma_error, +}; + +static void amic_esp_pio_nop(struct esp *esp) +{ +} + +static int amic_esp_pio_error(struct esp *esp) +{ + return esp->send_cmd_error; +} + +static const struct esp_driver_ops amic_esp_pio_ops = { + .esp_write8 = amic_esp_write8, + .esp_read8 = amic_esp_read8, + .irq_pending = amic_esp_irq_pending, + .dma_length_limit = amic_esp_dma_length_limit, + .reset_dma = amic_esp_pio_nop, + .dma_drain = amic_esp_pio_nop, + .dma_invalidate = amic_esp_pio_nop, + .send_dma_cmd = esp_send_pio_cmd, + .dma_error = amic_esp_pio_error, +}; + +/* FIFO loopback as in MkLinux, only 8100 class machines have the 53CF94 */ +static bool amic_esp_present(struct esp *esp) +{ + bool ok; + + esp_write8(ESP_CMD_FLUSH, ESP_CMD); + udelay(100); + esp_write8(0x55, ESP_FDATA); + esp_write8(0xaa, ESP_FDATA); + ok = (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES) == 2 && + esp_read8(ESP_FDATA) == 0x55 && esp_read8(ESP_FDATA) == 0xaa; + esp_write8(ESP_CMD_FLUSH, ESP_CMD); + return ok; +} + +static int amic_esp_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct amic_esp_priv *p; + struct Scsi_Host *host; + struct esp *esp; + u32 cfreq; + int err; + + if (of_property_read_u32(dev->of_node, "clock-frequency", &cfreq)) + return dev_err_probe(dev, -EINVAL, "no clock-frequency\n"); + + p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); + if (!p) + return -ENOMEM; + + host = scsi_host_alloc(&scsi_esp_template, sizeof(struct esp)); + if (!host) + return -ENOMEM; + host->max_id = 8; + + esp = shost_priv(host); + esp->host = host; + esp->dev = dev; + esp->cfreq = cfreq; + esp->scsi_id = 7; + host->this_id = esp->scsi_id; + esp->scsi_id_mask = BIT(esp->scsi_id); + p->esp = esp; + dev_set_drvdata(dev, p); + + esp->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(esp->regs)) { + err = PTR_ERR(esp->regs); + goto fail_host; + } + esp->fifo_reg = esp->regs + ESP_FDATA * 16; + + if (!amic_esp_present(esp)) { + err = -ENODEV; + goto fail_host; + } + + if (dma) { + p->dma_base = devm_platform_ioremap_resource(pdev, 1); + p->dma_ctrl = devm_platform_ioremap_resource(pdev, 2); + if (IS_ERR(p->dma_base) || IS_ERR(p->dma_ctrl)) { + dev_warn(dev, "no AMIC DMA registers\n"); + p->dma_base = NULL; + } + } + + if (p->dma_base) { + err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); + if (err) + goto fail_host; + p->bounce = dma_alloc_coherent(dev, AMIC_ESP_BOUNCE_SIZE, + &p->bounce_dma, GFP_KERNEL); + if (!p->bounce) { + err = -ENOMEM; + goto fail_host; + } + esp->dma_regs = p->dma_ctrl; + esp->ops = &amic_esp_dma_ops; + amic_esp_reset_dma(esp); + } else { + esp->ops = &amic_esp_pio_ops; + /* PIO leaves the transfer counter untouched */ + esp_write8(0, ESP_TCLOW); + esp_write8(0, ESP_TCMED); + } + + /* the core passes virtual addresses, all transfers are bounced or PIO */ + esp->flags = ESP_FLAG_NO_DMA_MAP | ESP_FLAG_USE_FIFO | + ESP_FLAG_DISABLE_SYNC; + + esp->command_block = kzalloc(16, GFP_KERNEL); + if (!esp->command_block) { + err = -ENOMEM; + goto fail_bounce; + } + esp->command_block_dma = (uintptr_t)esp->command_block; + + err = platform_get_irq(pdev, 0); + if (err < 0) + goto fail_cmd; + host->irq = err; + err = request_irq(host->irq, scsi_esp_intr, 0, DRV_MODULE_NAME, esp); + if (err) + goto fail_cmd; + + dev_info(dev, "%s\n", p->dma_base ? "AMIC DMA" : "PIO"); + + err = scsi_esp_register(esp); + if (err) + goto fail_irq; + return 0; + +fail_irq: + free_irq(host->irq, esp); +fail_cmd: + kfree(esp->command_block); +fail_bounce: + if (p->bounce) + dma_free_coherent(dev, AMIC_ESP_BOUNCE_SIZE, p->bounce, + p->bounce_dma); +fail_host: + scsi_host_put(host); + return err; +} + +static void amic_esp_remove(struct platform_device *pdev) +{ + struct amic_esp_priv *p = platform_get_drvdata(pdev); + struct esp *esp = p->esp; + + scsi_esp_unregister(esp); + free_irq(esp->host->irq, esp); + if (p->bounce) { + amic_esp_reset_dma(esp); + dma_free_coherent(esp->dev, AMIC_ESP_BOUNCE_SIZE, p->bounce, + p->bounce_dma); + } + kfree(esp->command_block); + scsi_host_put(esp->host); +} + +static const struct of_device_id amic_esp_match[] = { + { .compatible = "apple,amic-esp" }, + { } +}; +MODULE_DEVICE_TABLE(of, amic_esp_match); + +static struct platform_driver amic_esp_driver = { + .probe = amic_esp_probe, + .remove = amic_esp_remove, + .driver = { + .name = DRV_MODULE_NAME, + .of_match_table = amic_esp_match, + }, +}; +module_platform_driver(amic_esp_driver); + +MODULE_DESCRIPTION("NuBus Power Macintosh 53C94/53CF94 ESP SCSI driver"); +MODULE_AUTHOR("René Rebe "); +MODULE_LICENSE("GPL"); diff --git a/drivers/scsi/esp_scsi.c b/drivers/scsi/esp_scsi.c index 9993b0ae9c4f..db1fd90b6a53 100644 --- a/drivers/scsi/esp_scsi.c +++ b/drivers/scsi/esp_scsi.c @@ -2,6 +2,7 @@ /* esp_scsi.c: ESP SCSI driver. * * Copyright (C) 2007 David S. Miller (davem@davemloft.net) + * Copyright (C) 2026 René Rebe */ #include @@ -388,6 +389,7 @@ static void esp_map_dma(struct esp *esp, struct scsi_cmnd *cmd) scsi_for_each_sg(cmd, s, spriv->num_sg, i) { s->dma_address = (uintptr_t)sg_virt(s); + sg_dma_len(s) = s->length; total += sg_dma_len(s); } } else { diff --git a/drivers/scsi/mac_scsi.c b/drivers/scsi/mac_scsi.c index a86bd839d08e..93a06f53aebd 100644 --- a/drivers/scsi/mac_scsi.c +++ b/drivers/scsi/mac_scsi.c @@ -5,6 +5,7 @@ * Copyright 1998, Michael Schmitz * * Copyright 2019 Finn Thain + * Copyright (C) 2026 René Rebe * * derived in part from: */ @@ -21,13 +22,16 @@ #include #include #include +#include #include #include #include +#ifdef CONFIG_MAC #include #include #include +#endif #include @@ -94,6 +98,7 @@ static int __init mac_scsi_setup(char *str) __setup("mac5380=", mac_scsi_setup); #endif /* !MODULE */ +#ifdef CONFIG_M68K /* * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to * specify the number of bytes between the delays expected from a SCSI target. @@ -212,7 +217,8 @@ __setup("mac5380=", mac_scsi_setup); ".previous \n" \ : "+a" (addr), "+r" (n), "+r" (result) : "a" (io)) -static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n) +static inline int mac_pdma_recv(struct NCR5380_hostdata *hostdata, + void __iomem *io, unsigned char *start, int n) { unsigned char *addr = start; int result = 0; @@ -239,7 +245,8 @@ static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n) return addr - start; } -static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n) +static inline int mac_pdma_send(struct NCR5380_hostdata *hostdata, + unsigned char *start, void __iomem *io, int n) { unsigned char *addr = start; int result = 0; @@ -264,6 +271,46 @@ static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n) MOVE_BYTE("%0@,%3@"); return addr - start; } +#else +/* + * Without bus error recovery the pseudo DMA window is only accessed while + * the 5380 asserts DRQ, so the glue never waits for a slow target. + */ +static inline int mac_pdma_recv(struct NCR5380_hostdata *hostdata, + void __iomem *io, unsigned char *start, int n) +{ + int i; + + for (i = 0; i < n; i++) { + if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) + break; + start[i] = in_8(io); + } + return i; +} + +static inline int mac_pdma_send(struct NCR5380_hostdata *hostdata, + unsigned char *start, void __iomem *io, int n) +{ + int i; + + for (i = 0; i < n; i++) { + if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) + break; + out_8(io, start[i]); + } + return i; +} +#endif /* !CONFIG_M68K */ + +static inline bool mac_scsi_iifx(void) +{ +#ifdef CONFIG_MAC + return macintosh_config->ident == MAC_MODEL_IIFX; +#else + return false; +#endif +} /* The "SCSI DMA" chip on the IIfx implements this register. */ #define CTRL_REG 0x8 @@ -272,7 +319,7 @@ static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n) static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value) { - out_be32(hostdata->io + (CTRL_REG << 4), value); + out_be32((u32 __iomem *)(hostdata->io + (CTRL_REG << 4)), value); } static inline int macscsi_wait_for_drq(struct NCR5380_hostdata *hostdata) @@ -316,14 +363,14 @@ static inline int macscsi_pread(struct NCR5380_hostdata *hostdata, while (macscsi_wait_for_drq(hostdata) == 0) { int bytes, chunk_bytes; - if (macintosh_config->ident == MAC_MODEL_IIFX) + if (mac_scsi_iifx()) write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE | CTRL_INTERRUPTS_ENABLE); chunk_bytes = min(hostdata->pdma_residual, 512); - bytes = mac_pdma_recv(s, d, chunk_bytes); + bytes = mac_pdma_recv(hostdata, s, d, chunk_bytes); - if (macintosh_config->ident == MAC_MODEL_IIFX) + if (mac_scsi_iifx()) write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE); if (bytes > 0) { @@ -364,14 +411,14 @@ static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata, while (macscsi_wait_for_drq(hostdata) == 0) { int bytes, chunk_bytes; - if (macintosh_config->ident == MAC_MODEL_IIFX) + if (mac_scsi_iifx()) write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE | CTRL_INTERRUPTS_ENABLE); chunk_bytes = min(hostdata->pdma_residual, 512); - bytes = mac_pdma_send(s, d, chunk_bytes); + bytes = mac_pdma_send(hostdata, s, d, chunk_bytes); - if (macintosh_config->ident == MAC_MODEL_IIFX) + if (mac_scsi_iifx()) write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE); if (bytes > 0) { @@ -439,13 +486,25 @@ static struct scsi_host_template mac_scsi_template = { .max_sectors = 128, }; +static void __iomem *mac_scsi_map(struct device *dev, struct resource *res) +{ +#ifdef CONFIG_MAC + /* the 68k Macintosh I/O space is mapped 1:1 */ + return (void __iomem *)res->start; +#else + return devm_ioremap(dev, res->start, resource_size(res)); +#endif +} + static int __init mac_scsi_probe(struct platform_device *pdev) { struct Scsi_Host *instance; struct NCR5380_hostdata *hostdata; int error; int host_flags = 0; - struct resource *irq, *pio_mem, *pdma_mem = NULL; + int irq; + struct resource *pio_mem, *pdma_mem = NULL; + u8 __iomem *io, *pdma_io = NULL; pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!pio_mem) @@ -453,14 +512,23 @@ static int __init mac_scsi_probe(struct platform_device *pdev) pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); - irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); + irq = platform_get_irq_optional(pdev, 0); + + io = mac_scsi_map(&pdev->dev, pio_mem); + if (!io) + return -ENOMEM; - if (!hwreg_present((unsigned char *)pio_mem->start + - (STATUS_REG << 4))) { + if (!hwreg_present((__force void *)(io + (STATUS_REG << 4)))) { pr_info(PFX "no device detected at %pap\n", &pio_mem->start); return -ENODEV; } + if (pdma_mem && setup_use_pdma) { + pdma_io = mac_scsi_map(&pdev->dev, pdma_mem); + if (!pdma_io) + return -ENOMEM; + } + if (setup_can_queue > 0) mac_scsi_template.can_queue = setup_can_queue; if (setup_cmd_per_lun > 0) @@ -470,7 +538,7 @@ static int __init mac_scsi_probe(struct platform_device *pdev) if (setup_hostid >= 0) mac_scsi_template.this_id = setup_hostid & 7; - if (macintosh_config->ident == MAC_MODEL_IIFX) + if (mac_scsi_iifx()) mac_scsi_template.sg_tablesize = 1; instance = scsi_host_alloc(&mac_scsi_template, @@ -478,18 +546,16 @@ static int __init mac_scsi_probe(struct platform_device *pdev) if (!instance) return -ENOMEM; - if (irq) - instance->irq = irq->start; + if (irq > 0) + instance->irq = irq; else instance->irq = NO_IRQ; hostdata = shost_priv(instance); hostdata->base = pio_mem->start; - hostdata->io = (u8 __iomem *)pio_mem->start; - - if (pdma_mem && setup_use_pdma) - hostdata->pdma_io = (u8 __iomem *)pdma_mem->start; - else + hostdata->io = io; + hostdata->pdma_io = pdma_io; + if (!pdma_io) host_flags |= FLAG_NO_PSEUDO_DMA; host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; @@ -540,6 +606,12 @@ static void __exit mac_scsi_remove(struct platform_device *pdev) scsi_host_put(instance); } +static const struct of_device_id mac_scsi_of_match[] __maybe_unused = { + { .compatible = "apple,m2-ncr5380" }, + {} +}; +MODULE_DEVICE_TABLE(of, mac_scsi_of_match); + /* * mac_scsi_remove() lives in .exit.text. For drivers registered via * module_platform_driver_probe() this is ok because they cannot get unbound at @@ -550,6 +622,7 @@ static struct platform_driver mac_scsi_driver __refdata = { .remove = __exit_p(mac_scsi_remove), .driver = { .name = DRV_MODULE_NAME, + .of_match_table = of_match_ptr(mac_scsi_of_match), }, }; diff --git a/drivers/scsi/performa_esp.c b/drivers/scsi/performa_esp.c new file mode 100644 index 000000000000..abd83d6607d1 --- /dev/null +++ b/drivers/scsi/performa_esp.c @@ -0,0 +1,334 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ESP front-end for the NCR 53C96 of the Power Macintosh/Performa 5200/6200 + * family (F108, PrimeTime II "TurboSCSI" pseudo DMA). + * + * MkLinux drives the controller by programmed I/O, which is the default. + * As on the Quadra 630 (mac_esp) the 16-bit DMA handshake of the 53C96 can + * also be served by CPU accesses to the pseudo DMA port, which the I/O + * controller holds off until DREQ. To never stall on a target that changed + * phase or disconnected, the port is only accessed for the bytes resp. the + * space the FIFO flags report. An odd last received byte is kept in the + * FIFO (save residual byte) and read from the FIFO register. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "esp_scsi.h" + +#define DRV_MODULE_NAME "performa_esp" + +#define PERFORMA_ESP_FIFO_SIZE 16 +#define PERFORMA_ESP_TIMEOUT_US 500000 +#define PERFORMA_ESP_MAX_XFER (SZ_64K - SZ_4K) + +struct performa_esp_priv { + struct esp *esp; + void __iomem *pdma; + u8 *buf; + u32 len; + u32 done; + bool to_mem; + bool error; +}; + +static bool pdma; +module_param(pdma, bool, 0444); +MODULE_PARM_DESC(pdma, "use pseudo DMA, default programmed I/O"); + +static inline struct performa_esp_priv *performa_esp_priv(struct esp *esp) +{ + return dev_get_drvdata(esp->dev); +} + +static void performa_esp_write8(struct esp *esp, u8 val, unsigned long reg) +{ + out_8(esp->regs + reg * 16, val); +} + +static u8 performa_esp_read8(struct esp *esp, unsigned long reg) +{ + return in_8(esp->regs + reg * 16); +} + +#define esp_read8(REG) performa_esp_read8(esp, REG) +#define esp_write8(VAL, REG) performa_esp_write8(esp, VAL, REG) + +static int performa_esp_irq_pending(struct esp *esp) +{ + return !!(esp_read8(ESP_STATUS) & ESP_STAT_INTR); +} + +static u32 performa_esp_dma_length_limit(struct esp *esp, u32 dma_addr, + u32 dma_len) +{ + return min_t(u32, dma_len, PERFORMA_ESP_MAX_XFER); +} + +static void performa_esp_reset_dma(struct esp *esp) +{ + performa_esp_priv(esp)->len = 0; +} + +/* move the bytes still in the FIFO, at most up to the received count */ +static void performa_esp_dma_drain(struct esp *esp) +{ + struct performa_esp_priv *p = performa_esp_priv(esp); + u32 resid = 0, fifo; + + if (!p->len || !p->to_mem) + return; + + if (!(esp->sreg & ESP_STAT_TCNT)) + resid = esp_read8(ESP_TCLOW) | (esp_read8(ESP_TCMED) << 8); + resid = min(resid, p->len); + fifo = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES; + while (fifo-- && p->done < p->len - resid) + p->buf[p->done++] = esp_read8(ESP_FDATA); + if (p->done != p->len - resid) { + dev_err(esp->dev, "data in: %u of %u bytes moved\n", p->done, + p->len - resid); + p->error = true; + } + p->len = 0; +} + +static void performa_esp_dma_invalidate(struct esp *esp) +{ + esp_write8(esp->prev_cfg3, ESP_CFG3); + performa_esp_priv(esp)->len = 0; +} + +static void performa_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count, + u32 dma_count, int write, u8 cmd) +{ + struct performa_esp_priv *p = performa_esp_priv(esp); + unsigned int timeout = PERFORMA_ESP_TIMEOUT_US; + u8 *buf = (u8 *)(uintptr_t)addr; + u32 left = dma_count, n; + + p->buf = buf; + p->len = dma_count; + p->to_mem = write; + p->error = false; + + if (write) + esp_write8(esp->prev_cfg3 | ESP_CONFIG3_SRB, ESP_CFG3); + else + scsi_esp_cmd(esp, ESP_CMD_FLUSH); + esp_write8(esp_count & 0xff, ESP_TCLOW); + esp_write8((esp_count >> 8) & 0xff, ESP_TCMED); + scsi_esp_cmd(esp, cmd); + + while (left) { + n = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES; + /* received bytes resp. free space, DREQ needs a word of it */ + if (!write) + n = PERFORMA_ESP_FIFO_SIZE - n; + + if (n >= 2 && left >= 2) { + n = min(n, left) >> 1; + left -= n << 1; + for (; n; n--, buf += 2) { + if (write) { + u16 w = in_be16(p->pdma); + + buf[0] = w >> 8; + buf[1] = w; + } else { + out_be16(p->pdma, buf[0] << 8 | buf[1]); + } + } + timeout = PERFORMA_ESP_TIMEOUT_US; + continue; + } + if (!write && n >= 2) { + out_8(p->pdma, *buf++); + left--; + continue; + } + /* the odd last byte is read from the FIFO by the drain */ + if (write && left == 1) + break; + if (esp_read8(ESP_STATUS) & ESP_STAT_INTR) + break; + if (!--timeout) { + dev_err(esp->dev, "pseudo DMA timeout, %u of %u bytes left\n", + left, dma_count); + p->error = true; + break; + } + udelay(1); + } + p->done = dma_count - left; +} + +static int performa_esp_dma_error(struct esp *esp) +{ + return performa_esp_priv(esp)->error; +} + +static const struct esp_driver_ops performa_esp_pdma_ops = { + .esp_write8 = performa_esp_write8, + .esp_read8 = performa_esp_read8, + .irq_pending = performa_esp_irq_pending, + .dma_length_limit = performa_esp_dma_length_limit, + .reset_dma = performa_esp_reset_dma, + .dma_drain = performa_esp_dma_drain, + .dma_invalidate = performa_esp_dma_invalidate, + .send_dma_cmd = performa_esp_send_dma_cmd, + .dma_error = performa_esp_dma_error, +}; + +static void performa_esp_pio_nop(struct esp *esp) +{ +} + +static int performa_esp_pio_error(struct esp *esp) +{ + return esp->send_cmd_error; +} + +static const struct esp_driver_ops performa_esp_pio_ops = { + .esp_write8 = performa_esp_write8, + .esp_read8 = performa_esp_read8, + .irq_pending = performa_esp_irq_pending, + .dma_length_limit = performa_esp_dma_length_limit, + .reset_dma = performa_esp_pio_nop, + .dma_drain = performa_esp_pio_nop, + .dma_invalidate = performa_esp_pio_nop, + .send_dma_cmd = esp_send_pio_cmd, + .dma_error = performa_esp_pio_error, +}; + +static int performa_esp_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct performa_esp_priv *p; + struct Scsi_Host *host; + struct esp *esp; + u32 cfreq; + int err; + + if (of_property_read_u32(dev->of_node, "clock-frequency", &cfreq)) + return dev_err_probe(dev, -EINVAL, "no clock-frequency\n"); + + p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL); + if (!p) + return -ENOMEM; + + host = scsi_host_alloc(&scsi_esp_template, sizeof(struct esp)); + if (!host) + return -ENOMEM; + host->max_id = 8; + + esp = shost_priv(host); + esp->host = host; + esp->dev = dev; + esp->cfreq = cfreq; + esp->scsi_id = 7; + host->this_id = esp->scsi_id; + esp->scsi_id_mask = BIT(esp->scsi_id); + p->esp = esp; + dev_set_drvdata(dev, p); + + esp->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(esp->regs)) { + err = PTR_ERR(esp->regs); + goto fail_host; + } + esp->fifo_reg = esp->regs + ESP_FDATA * 16; + + if (pdma) { + p->pdma = devm_platform_ioremap_resource(pdev, 1); + if (IS_ERR(p->pdma)) { + dev_warn(dev, "no pseudo DMA port\n"); + p->pdma = NULL; + } + } + + if (p->pdma) { + esp->ops = &performa_esp_pdma_ops; + } else { + esp->ops = &performa_esp_pio_ops; + /* PIO leaves the transfer counter untouched */ + esp_write8(0, ESP_TCLOW); + esp_write8(0, ESP_TCMED); + } + + /* the core passes virtual addresses, the CPU moves all data */ + esp->flags = ESP_FLAG_NO_DMA_MAP | ESP_FLAG_USE_FIFO | + ESP_FLAG_DISABLE_SYNC; + + esp->command_block = kzalloc(16, GFP_KERNEL); + if (!esp->command_block) { + err = -ENOMEM; + goto fail_host; + } + esp->command_block_dma = (uintptr_t)esp->command_block; + + err = platform_get_irq(pdev, 0); + if (err < 0) + goto fail_cmd; + host->irq = err; + err = request_irq(host->irq, scsi_esp_intr, 0, DRV_MODULE_NAME, esp); + if (err) + goto fail_cmd; + + dev_info(dev, "%s\n", p->pdma ? "pseudo DMA" : "PIO"); + + err = scsi_esp_register(esp); + if (err) + goto fail_irq; + return 0; + +fail_irq: + free_irq(host->irq, esp); +fail_cmd: + kfree(esp->command_block); +fail_host: + scsi_host_put(host); + return err; +} + +static void performa_esp_remove(struct platform_device *pdev) +{ + struct performa_esp_priv *p = platform_get_drvdata(pdev); + struct esp *esp = p->esp; + + scsi_esp_unregister(esp); + free_irq(esp->host->irq, esp); + kfree(esp->command_block); + scsi_host_put(esp->host); +} + +static const struct of_device_id performa_esp_match[] = { + { .compatible = "apple,performa-esp" }, + { } +}; +MODULE_DEVICE_TABLE(of, performa_esp_match); + +static struct platform_driver performa_esp_driver = { + .probe = performa_esp_probe, + .remove = performa_esp_remove, + .driver = { + .name = DRV_MODULE_NAME, + .of_match_table = performa_esp_match, + }, +}; +module_platform_driver(performa_esp_driver); + +MODULE_DESCRIPTION("Performa 5200/6200 53C96 ESP SCSI driver"); +MODULE_AUTHOR("René Rebe "); +MODULE_LICENSE("GPL"); diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig index 3864cef711a6..edb9f2b144cc 100644 --- a/drivers/tty/serial/Kconfig +++ b/drivers/tty/serial/Kconfig @@ -735,7 +735,7 @@ config SERIAL_MCF_CONSOLE config SERIAL_PMACZILOG tristate "Mac or PowerMac z85c30 ESCC support" - depends on (M68K && MAC) || PPC_PMAC + depends on (M68K && MAC) || PPC_PMAC || PPC_NUBUS_PMAC select SERIAL_CORE help This driver supports the Zilog z85C30 serial ports found on diff --git a/drivers/tty/serial/pmac_zilog.c b/drivers/tty/serial/pmac_zilog.c index a612f1db9f18..b3f9649ed73c 100644 --- a/drivers/tty/serial/pmac_zilog.c +++ b/drivers/tty/serial/pmac_zilog.c @@ -4,6 +4,7 @@ * "macio" ASICs of various PowerMac models * * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org) + * Copyright (C) 2026 René Rebe * * Derived from drivers/macintosh/macserial.c by Paul Mackerras * and drivers/serial/sunzilog.c by David S. Miller @@ -53,8 +54,9 @@ #include #include #include -#else +#endif #include +#ifndef CONFIG_PPC #define of_machine_is_compatible(x) (0) #endif @@ -1608,7 +1610,9 @@ static int __init pmz_probe(void) return 0; } -#else +#endif /* CONFIG_PPC_PMAC */ + +#if PMZ_PLATFORM /* On PCI PowerMacs, pmz_probe() does an explicit search of the OpenFirmware * tree to obtain the device_nodes needed to start the console before the @@ -1617,7 +1621,7 @@ static int __init pmz_probe(void) */ extern struct platform_device scc_a_pdev, scc_b_pdev; -static int __init pmz_init_port(struct uart_pmac_port *uap) +static int __init pmz_plat_init_port(struct uart_pmac_port *uap) { struct resource *r_ports; int irq; @@ -1631,7 +1635,14 @@ static int __init pmz_init_port(struct uart_pmac_port *uap) return irq; uap->port.mapbase = r_ports->start; +#ifdef CONFIG_PPC + /* NuBus PowerMac: control register, data register at +4 */ + uap->port.membase = ioremap(r_ports->start, 8); + if (!uap->port.membase) + return -ENOMEM; +#else uap->port.membase = (unsigned char __iomem *) r_ports->start; +#endif uap->port.iotype = UPIO_MEM; uap->port.irq = irq; uap->port.uartclk = ZS_CLOCK; @@ -1650,7 +1661,7 @@ static int __init pmz_init_port(struct uart_pmac_port *uap) return 0; } -static int __init pmz_probe(void) +static int __init pmz_plat_probe(void) { int err; @@ -1659,7 +1670,7 @@ static int __init pmz_probe(void) pmz_ports[0].port.line = 0; pmz_ports[0].flags = PMACZILOG_FLAG_IS_CHANNEL_A; pmz_ports[0].pdev = &scc_a_pdev; - err = pmz_init_port(&pmz_ports[0]); + err = pmz_plat_init_port(&pmz_ports[0]); if (err) return err; pmz_ports_count++; @@ -1669,7 +1680,7 @@ static int __init pmz_probe(void) pmz_ports[1].port.line = 1; pmz_ports[1].flags = 0; pmz_ports[1].pdev = &scc_b_pdev; - err = pmz_init_port(&pmz_ports[1]); + err = pmz_plat_init_port(&pmz_ports[1]); if (err) return err; pmz_ports_count++; @@ -1677,12 +1688,12 @@ static int __init pmz_probe(void) return 0; } -static void pmz_dispose_port(struct uart_pmac_port *uap) +static void pmz_plat_dispose_port(struct uart_pmac_port *uap) { memset(uap, 0, sizeof(struct uart_pmac_port)); } -static int pmz_attach(struct platform_device *pdev) +static int pmz_plat_attach(struct platform_device *pdev) { struct uart_pmac_port *uap; int i; @@ -1701,7 +1712,7 @@ static int pmz_attach(struct platform_device *pdev) return uart_add_one_port(&pmz_uart_reg, &uap->port); } -static void pmz_detach(struct platform_device *pdev) +static void pmz_plat_detach(struct platform_device *pdev) { struct uart_pmac_port *uap = platform_get_drvdata(pdev); @@ -1710,7 +1721,53 @@ static void pmz_detach(struct platform_device *pdev) uap->port.dev = NULL; } -#endif /* !CONFIG_PPC_PMAC */ +#endif /* PMZ_PLATFORM */ + +/* NuBus PowerMacs use the platform path in kernels that also support PCI ones */ +static bool pmz_platform(void) +{ + if (!IS_ENABLED(CONFIG_PPC_PMAC)) + return true; + return PMZ_PLATFORM && of_machine_is_compatible("apple,nubus-pmac"); +} + +static int __init pmz_probe_ports(void) +{ +#if PMZ_PLATFORM + if (pmz_platform()) + return pmz_plat_probe(); +#endif +#ifdef CONFIG_PPC_PMAC + return pmz_probe(); +#else + return -ENODEV; +#endif +} + +static bool pmz_port_present(struct uart_pmac_port *uap) +{ +#ifdef CONFIG_PPC_PMAC + if (uap->node) + return true; +#endif +#if PMZ_PLATFORM + if (uap->pdev) + return true; +#endif + return false; +} + +static void pmz_dispose_any(struct uart_pmac_port *uap) +{ +#ifdef CONFIG_PPC_PMAC + if (uap->node) + pmz_dispose_port(uap); +#endif +#if PMZ_PLATFORM + if (uap->pdev) + pmz_plat_dispose_port(uap); +#endif +} #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE @@ -1773,17 +1830,17 @@ static struct macio_driver pmz_driver = { .resume = pmz_resume, }; -#else +#endif /* CONFIG_PPC_PMAC */ -static struct platform_driver pmz_driver = { - .probe = pmz_attach, - .remove = pmz_detach, +#if PMZ_PLATFORM +static struct platform_driver pmz_plat_driver = { + .probe = pmz_plat_attach, + .remove = pmz_plat_detach, .driver = { .name = "scc", }, }; - -#endif /* !CONFIG_PPC_PMAC */ +#endif static int __init init_pmz(void) { @@ -1797,7 +1854,7 @@ static int __init init_pmz(void) * uart_register_driver() */ if (pmz_ports_count == 0) - pmz_probe(); + pmz_probe_ports(); /* * Bail early if no port found @@ -1813,19 +1870,28 @@ static int __init init_pmz(void) printk(KERN_ERR "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n" "pmac_zilog: Did another serial driver already claim the minors?\n"); + /* the early console must not use the ports after disposal */ +#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE + unregister_console(&pmz_console); +#endif /* effectively "pmz_unprobe()" */ for (i=0; i < pmz_ports_count; i++) - pmz_dispose_port(&pmz_ports[i]); + pmz_dispose_any(&pmz_ports[i]); + pmz_ports_count = 0; return rc; } /* * Then we register the macio driver itself */ +#if PMZ_PLATFORM + if (pmz_platform()) + return platform_driver_register(&pmz_plat_driver); +#endif #ifdef CONFIG_PPC_PMAC return macio_register_driver(&pmz_driver); #else - return platform_driver_register(&pmz_driver); + return -ENODEV; #endif } @@ -1833,23 +1899,18 @@ static void __exit exit_pmz(void) { int i; +#if PMZ_PLATFORM + if (pmz_platform()) + platform_driver_unregister(&pmz_plat_driver); +#endif #ifdef CONFIG_PPC_PMAC /* Get rid of macio-driver (detach from macio) */ - macio_unregister_driver(&pmz_driver); -#else - platform_driver_unregister(&pmz_driver); + if (!pmz_platform()) + macio_unregister_driver(&pmz_driver); #endif - for (i = 0; i < pmz_ports_count; i++) { - struct uart_pmac_port *uport = &pmz_ports[i]; -#ifdef CONFIG_PPC_PMAC - if (uport->node != NULL) - pmz_dispose_port(uport); -#else - if (uport->pdev != NULL) - pmz_dispose_port(uport); -#endif - } + for (i = 0; i < pmz_ports_count; i++) + pmz_dispose_any(&pmz_ports[i]); /* Unregister UART driver */ uart_unregister_driver(&pmz_uart_reg); } @@ -1920,13 +1981,8 @@ static int __init pmz_console_setup(struct console *co, char *options) if (co->index >= pmz_ports_count) co->index = 0; uap = &pmz_ports[co->index]; -#ifdef CONFIG_PPC_PMAC - if (uap->node == NULL) + if (!pmz_port_present(uap)) return -ENODEV; -#else - if (uap->pdev == NULL) - return -ENODEV; -#endif port = &uap->port; /* @@ -1955,7 +2011,7 @@ static int __init pmz_console_setup(struct console *co, char *options) static int __init pmz_console_init(void) { /* Probe ports */ - pmz_probe(); + pmz_probe_ports(); if (pmz_ports_count == 0) return -ENODEV; diff --git a/drivers/tty/serial/pmac_zilog.h b/drivers/tty/serial/pmac_zilog.h index 837b97ca0a90..82eb0c942b29 100644 --- a/drivers/tty/serial/pmac_zilog.h +++ b/drivers/tty/serial/pmac_zilog.h @@ -1,7 +1,17 @@ /* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2026 René Rebe + */ #ifndef __PMAC_ZILOG_H__ #define __PMAC_ZILOG_H__ +/* platform device ports: m68k Macs and NuBus Power Macintosh */ +#if !defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_NUBUS_PMAC) +#define PMZ_PLATFORM 1 +#else +#define PMZ_PLATFORM 0 +#endif + /* * At most 2 ESCCs with 2 ports each */ @@ -25,7 +35,8 @@ struct uart_pmac_port { * of "escc" node (ie. ch-a or ch-b) */ struct device_node *node; -#else +#endif +#if PMZ_PLATFORM struct platform_device *pdev; #endif diff --git a/drivers/video/fbdev/Kconfig b/drivers/video/fbdev/Kconfig index ad5edbe58a23..e5a20b845535 100644 --- a/drivers/video/fbdev/Kconfig +++ b/drivers/video/fbdev/Kconfig @@ -298,13 +298,23 @@ config FB_PLATINUM config FB_VALKYRIE bool "Apple \"valkyrie\" display support" - depends on (FB = y) && (MAC || (PPC_PMAC && PPC32)) + depends on (FB = y) && (MAC || (PPC_PMAC && PPC32) || PPC_NUBUS_PMAC) select FB_IOMEM_HELPERS select FB_MACMODES help This driver supports a frame buffer for the "valkyrie" graphics adapter in some Power Macintoshes. +config FB_PDM + tristate "NuBus Power Macintosh PDM display support" + depends on FB && PPC_NUBUS_PMAC + select FB_IOMEM_HELPERS + help + This driver supports the displays of the NuBus Power Macintosh + 6100, 7100 and 8100: the on-board video, which senses the monitor + and sets its modes itself, and the HPV and AV video cards in the + display mode set up by Mac OS and handed over by the boot loader. + config FB_CT65550 bool "Chips 65550 display support" depends on (FB = y) && PPC32 && PCI diff --git a/drivers/video/fbdev/Makefile b/drivers/video/fbdev/Makefile index 0b17c878154d..53f30c689827 100644 --- a/drivers/video/fbdev/Makefile +++ b/drivers/video/fbdev/Makefile @@ -37,6 +37,7 @@ obj-$(CONFIG_FB_3DFX) += tdfxfb.o obj-$(CONFIG_FB_CONTROL) += controlfb.o obj-$(CONFIG_FB_PLATINUM) += platinumfb.o obj-$(CONFIG_FB_VALKYRIE) += valkyriefb.o +obj-$(CONFIG_FB_PDM) += pdmfb.o obj-$(CONFIG_FB_CT65550) += chipsfb.o obj-$(CONFIG_FB_IMSTT) += imsttfb.o obj-$(CONFIG_FB_FM2) += fm2fb.o diff --git a/drivers/video/fbdev/pdmfb.c b/drivers/video/fbdev/pdmfb.c new file mode 100644 index 000000000000..ec4f92793b16 --- /dev/null +++ b/drivers/video/fbdev/pdmfb.c @@ -0,0 +1,930 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Frame buffer driver for the NuBus Power Macintosh PDM family (6100, 7100, + * 8100): the on-board DRAM video with monitor sense and mode setting, and + * the HPV (video direct slot) and AV cards in the display mode Mac OS left. + * + * Registers, monitor sense and mode tables follow MkLinux osfmk + * (video_pdm.c, video_hpv.c, video_av.c by Michael Burg); the mode timings + * and the low depth CLUT layout follow the MAME PDM/Sonora video emulation. + * + * The boot loader hands over the boot display with the BootX properties + * linux,bootx-{addr,width,height,depth,linebytes} on the display node: the + * on-board video node of the built-in device tree, or the HPV or AV card + * node BootX support creates by the frame buffer address. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* on-board: CLUT DAC */ +#define DAC_ADDR 0x0 +#define DAC_DATA 0x1 +#define DAC_CTRL 0x2 + +/* on-board: video control */ +#define VC_MODE 0x0 +#define VC_DEPTH 0x1 +#define VC_SENSE 0x2 +#define VC_MODE_BLANK 0x80 /* MAME only */ + +#define SENSE_RESET 0x07 +#define SENSE_DRIVE_A 0x03 +#define SENSE_DRIVE_B 0x05 +#define SENSE_DRIVE_C 0x06 + +/* HPV: 6 bit registers, the upper bits go through the overflow register */ +#define HPV_OVERFLOW 0x0f0 +#define HPV_INT_MASK 0x108 +#define HPV_INT_STATUS 0x110 +#define HPV_VBL_CLEAR 0x128 +#define HPV_CLUT_ADDR 0x207 +#define HPV_CLUT_DATA 0x237 +#define HPV_INT_VBL 0x01 + +/* AV: control registers and CLUT */ +#define AV_VBL_STATUS 0x000 +#define AV_VBL_CLEAR 0x120 +#define AV_CLUT_ADDR 0x00 +#define AV_CLUT_DATA 0x10 + +#define PDMFB_CLUT_SIZE 256 +#define PDMFB_VBL_TIMEOUT_US 100000 + +enum pdm_display_type { + DISPLAY_NONE, + DISPLAY_12INCH, + DISPLAY_STANDARD, + DISPLAY_PORTRAIT, + DISPLAY_PORTRAIT_MONO, + DISPLAY_16INCH, + DISPLAY_19INCH, + DISPLAY_21INCH, + DISPLAY_21INCH_MONO, + DISPLAY_VGA, + DISPLAY_NTSC, + DISPLAY_PAL, + DISPLAY_MSB1, + DISPLAY_MSB2, + DISPLAY_MSB3, +}; + +enum pdm_screen_id { + SCREEN_512x384_60, + SCREEN_640x480_60_VGA, + SCREEN_640x480_67, + SCREEN_640x870_75, + SCREEN_832x624_75, +}; + +struct pdm_screen { + u8 value; /* video mode register */ + u8 maxdepth; + struct fb_videomode mode; +}; + +static const struct pdm_screen pdm_screens[] = { + [SCREEN_512x384_60] = { 0x02, 16, + { "512x384-60", 60, 512, 384, 63826, 80, 16, 19, 1, 32, 3 } }, + [SCREEN_640x480_60_VGA] = { 0x0b, 8, + { "640x480-60", 60, 640, 480, 39722, 48, 16, 33, 10, 96, 2 } }, + [SCREEN_640x480_67] = { 0x06, 16, + { "640x480-67", 67, 640, 480, 31913, 112, 80, 39, 3, 64, 3 } }, + [SCREEN_640x870_75] = { 0x01, 8, + { "640x870-75", 75, 640, 870, 17457, 80, 32, 42, 3, 80, 3 } }, + [SCREEN_832x624_75] = { 0x09, 8, + { "832x624-75", 75, 832, 624, 17457, 224, 32, 39, 1, 64, 3 } }, +}; + +/* pixel depth register value is the index, CLUT control the value */ +static const struct { + u8 depth; + u8 control; +} pdm_depths[] = { + { 1, 0x08 }, { 2, 0x09 }, { 4, 0x0a }, { 8, 0x0b }, { 16, 0x0c }, +}; + +struct pdm_display { + const char *name; + const u8 *screens; /* preferred first */ + unsigned int nscreens; +}; + +#define PDM_SCREENS(...) \ + .screens = (const u8 []){ __VA_ARGS__ }, \ + .nscreens = sizeof((const u8 []){ __VA_ARGS__ }) + +static const struct pdm_display pdm_displays[] = { + [DISPLAY_NONE] = { "no display" }, + [DISPLAY_12INCH] = { "12\" RGB", PDM_SCREENS(SCREEN_512x384_60) }, + [DISPLAY_STANDARD] = { "12\"/13\"/14\" RGB", + PDM_SCREENS(SCREEN_640x480_67) }, + [DISPLAY_PORTRAIT] = { "15\" Portrait RGB", + PDM_SCREENS(SCREEN_640x870_75) }, + [DISPLAY_PORTRAIT_MONO] = { "15\" Portrait Mono", + PDM_SCREENS(SCREEN_640x870_75) }, + [DISPLAY_16INCH] = { "16\" RGB", PDM_SCREENS(SCREEN_832x624_75) }, + [DISPLAY_19INCH] = { "19\" RGB" }, + [DISPLAY_21INCH] = { "21\" RGB" }, + [DISPLAY_21INCH_MONO] = { "21\" Mono" }, + [DISPLAY_VGA] = { "VGA", PDM_SCREENS(SCREEN_640x480_60_VGA) }, + [DISPLAY_NTSC] = { "NTSC" }, + [DISPLAY_PAL] = { "PAL" }, + [DISPLAY_MSB1] = { "MultiScan Band-1", + PDM_SCREENS(SCREEN_640x480_67, + SCREEN_512x384_60, + SCREEN_832x624_75) }, + [DISPLAY_MSB2] = { "MultiScan Band-2", + PDM_SCREENS(SCREEN_832x624_75, + SCREEN_640x480_67) }, + [DISPLAY_MSB3] = { "MultiScan Band-3", + PDM_SCREENS(SCREEN_832x624_75, + SCREEN_640x480_67) }, +}; + +/* base sense code, extended sense code, display */ +static const u8 pdm_monitors[][3] = { + { 0, 0x00, DISPLAY_21INCH }, + { 1, 0x14, DISPLAY_PORTRAIT_MONO }, + { 2, 0x21, DISPLAY_12INCH }, + { 3, 0x31, DISPLAY_21INCH }, + { 3, 0x34, DISPLAY_21INCH_MONO }, + { 3, 0x35, DISPLAY_21INCH_MONO }, + { 4, 0x0a, DISPLAY_NTSC }, + { 5, 0x1e, DISPLAY_PORTRAIT }, + { 6, 0x03, DISPLAY_MSB1 }, + { 6, 0x0b, DISPLAY_MSB2 }, + { 6, 0x23, DISPLAY_MSB3 }, + { 6, 0x2b, DISPLAY_STANDARD }, + { 7, 0x00, DISPLAY_PAL }, + { 7, 0x14, DISPLAY_NTSC }, + { 7, 0x17, DISPLAY_VGA }, + { 7, 0x2d, DISPLAY_16INCH }, + { 7, 0x30, DISPLAY_PAL }, + { 7, 0x3a, DISPLAY_19INCH }, + { 7, 0x3f, DISPLAY_NONE }, +}; + +struct pdmfb_par; + +struct pdmfb_variant { + const char *name; + const struct fb_ops *ops; + unsigned int fb_index; /* reg entry of the frame buffer */ + int (*init)(struct pdmfb_par *par, struct platform_device *pdev); + /* writes the dirty CLUT range, called with the lock held */ + void (*load_clut)(struct pdmfb_par *par); +}; + +struct pdmfb_par { + const struct pdmfb_variant *variant; + struct device *dev; + void __iomem *regs; + void __iomem *clut; + spinlock_t lock; + u8 cmap[PDMFB_CLUT_SIZE][3]; + unsigned int dirty_lo, dirty_hi; + int irq; /* on-board VBL, CLUT loaded in its handler */ + struct fb_var_screeninfo boot_var; + u32 boot_linebytes; + bool has_boot_var; + /* on-board */ + const struct pdm_display *display; + struct fb_videomode *modedb; + unsigned int modedb_len; + u8 mode_value, depth_index; + bool blanked; + u32 pseudo_palette[16]; +}; + +static void pdmfb_set_format(struct fb_var_screeninfo *var, unsigned int bpp) +{ + var->bits_per_pixel = bpp; + var->transp.offset = var->transp.length = 0; + if (bpp <= 8) { + var->red.offset = var->green.offset = var->blue.offset = 0; + var->red.length = var->green.length = var->blue.length = bpp; + } else if (bpp == 16) { + /* x1r5g5b5 */ + var->red.offset = 10; + var->green.offset = 5; + var->blue.offset = 0; + var->red.length = var->green.length = var->blue.length = 5; + } else { + /* x8r8g8b8 */ + var->red.offset = 16; + var->green.offset = 8; + var->blue.offset = 0; + var->red.length = var->green.length = var->blue.length = 8; + } + var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0; +} + +static void pdmfb_update_fix(struct fb_info *info, u32 line_length) +{ + info->fix.visual = info->var.bits_per_pixel <= 8 ? + FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR; + info->fix.line_length = line_length; +} + +/* boot display as BootX describes it on the display node */ +static bool pdmfb_get_boot_display(struct device_node *np, u32 *addr, + struct fb_var_screeninfo *var, + u32 *linebytes) +{ + u32 width, height, depth; + + if (of_property_read_u32(np, "linux,bootx-addr", addr) || + of_property_read_u32(np, "linux,bootx-width", &width) || + of_property_read_u32(np, "linux,bootx-height", &height) || + of_property_read_u32(np, "linux,bootx-depth", &depth) || + of_property_read_u32(np, "linux,bootx-linebytes", linebytes)) + return false; + + if (depth == 15) + depth = 16; + if (!width || !height || *linebytes < width * depth / 8 || + !(depth == 1 || depth == 2 || depth == 4 || depth == 8 || + depth == 16 || depth == 32)) { + pr_warn("%pOF: invalid boot display %ux%u-%u, %u bytes per line\n", + np, width, height, depth, *linebytes); + return false; + } + + memset(var, 0, sizeof(*var)); + var->xres = var->xres_virtual = width; + var->yres = var->yres_virtual = height; + var->height = var->width = -1; + var->vmode = FB_VMODE_NONINTERLACED; + pdmfb_set_format(var, depth); + return true; +} + +/* the CLUT index of a color register, low depths use the top CLUT bits */ +static unsigned int pdmfb_clut_index(struct fb_info *info, unsigned int regno) +{ + unsigned int bpp = info->var.bits_per_pixel; + + return bpp < 8 ? (regno << (8 - bpp)) | (0xff >> bpp) : regno; +} + +static void pdmfb_flush(struct pdmfb_par *par) +{ + if (par->dirty_lo > par->dirty_hi || par->irq > 0) + return; + par->variant->load_clut(par); +} + +static void pdmfb_store(struct pdmfb_par *par, unsigned int index, + u16 red, u16 green, u16 blue) +{ + par->cmap[index][0] = red >> 8; + par->cmap[index][1] = green >> 8; + par->cmap[index][2] = blue >> 8; + par->dirty_lo = min(par->dirty_lo, index); + par->dirty_hi = max(par->dirty_hi, index); +} + +static void pdmfb_clean(struct pdmfb_par *par) +{ + par->dirty_lo = PDMFB_CLUT_SIZE; + par->dirty_hi = 0; +} + +static int pdmfb_setcolreg(unsigned int regno, unsigned int red, + unsigned int green, unsigned int blue, + unsigned int transp, struct fb_info *info) +{ + struct pdmfb_par *par = info->par; + struct fb_var_screeninfo *var = &info->var; + unsigned long flags; + + if (info->fix.visual == FB_VISUAL_TRUECOLOR) { + if (regno >= 16) + return -EINVAL; + par->pseudo_palette[regno] = + (red >> (16 - var->red.length)) << var->red.offset | + (green >> (16 - var->green.length)) << var->green.offset | + (blue >> (16 - var->blue.length)) << var->blue.offset; + return 0; + } + + if (regno >= 1U << var->bits_per_pixel) + return -EINVAL; + + spin_lock_irqsave(&par->lock, flags); + pdmfb_store(par, pdmfb_clut_index(info, regno), red, green, blue); + pdmfb_flush(par); + spin_unlock_irqrestore(&par->lock, flags); + return 0; +} + +static int pdmfb_setcmap(struct fb_cmap *cmap, struct fb_info *info) +{ + struct pdmfb_par *par = info->par; + unsigned int i, regno; + unsigned long flags; + + if (info->fix.visual == FB_VISUAL_TRUECOLOR) { + for (i = 0; i < cmap->len && cmap->start + i < 16; i++) + pdmfb_setcolreg(cmap->start + i, cmap->red[i], + cmap->green[i], cmap->blue[i], 0, info); + return 0; + } + + /* a whole palette is written in one vertical blank */ + spin_lock_irqsave(&par->lock, flags); + for (i = 0; i < cmap->len; i++) { + regno = cmap->start + i; + if (regno >= 1U << info->var.bits_per_pixel) + break; + pdmfb_store(par, pdmfb_clut_index(info, regno), cmap->red[i], + cmap->green[i], cmap->blue[i]); + } + pdmfb_flush(par); + spin_unlock_irqrestore(&par->lock, flags); + return 0; +} + +/* only the display mode the boot loader handed over */ +static int pdmfb_boot_check_var(struct fb_var_screeninfo *var, + struct fb_info *info) +{ + struct pdmfb_par *par = info->par; + u32 activate = var->activate; + + if (!par->has_boot_var || var->xres > par->boot_var.xres || + var->yres > par->boot_var.yres || + var->bits_per_pixel > par->boot_var.bits_per_pixel) + return -EINVAL; + + *var = par->boot_var; + var->activate = activate; + return 0; +} + +/* on-board video */ + +static u8 pdm_sense_line(struct pdmfb_par *par, u8 drive) +{ + out_8(par->regs + VC_SENSE, drive); + udelay(3); + return (in_8(par->regs + VC_SENSE) >> 4) & 7; +} + +static const struct pdm_display *pdm_sense_monitor(struct pdmfb_par *par) +{ + u8 sense, a, b, c, ext; + unsigned int i; + + sense = pdm_sense_line(par, SENSE_RESET); + a = pdm_sense_line(par, SENSE_DRIVE_A); + b = pdm_sense_line(par, SENSE_DRIVE_B); + c = pdm_sense_line(par, SENSE_DRIVE_C); + pdm_sense_line(par, SENSE_RESET); + + ext = ((a << 4) & 0x30) | ((b >> 2) << 3) | ((b & 1) << 2) | + ((c >> 1) & 3); + + for (i = 0; i < ARRAY_SIZE(pdm_monitors); i++) + if (pdm_monitors[i][0] == sense && pdm_monitors[i][1] == ext) { + dev_info(par->dev, "monitor sense %u/0x%02x: %s\n", + sense, ext, + pdm_displays[pdm_monitors[i][2]].name); + return &pdm_displays[pdm_monitors[i][2]]; + } + + dev_info(par->dev, "unknown monitor sense %u/0x%02x\n", sense, ext); + return NULL; +} + +static int pdm_depth_index(unsigned int bpp) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(pdm_depths); i++) + if (pdm_depths[i].depth == bpp) + return i; + return -EINVAL; +} + +/* a mode the controller can set for the sensed monitor */ +static const struct pdm_screen *pdm_screen_for_var(struct pdmfb_par *par, + unsigned int xres, + unsigned int yres, + unsigned int bpp) +{ + const struct pdm_screen *s; + unsigned int i; + + if (!par->display) + return NULL; + for (i = 0; i < par->display->nscreens; i++) { + s = &pdm_screens[par->display->screens[i]]; + if (s->mode.xres == xres && s->mode.yres == yres && + bpp <= s->maxdepth) + return s; + } + return NULL; +} + +static int pdmfb_onboard_check_var(struct fb_var_screeninfo *var, + struct fb_info *info) +{ + struct pdmfb_par *par = info->par; + const struct pdm_screen *s; + unsigned int bpp; + + bpp = var->bits_per_pixel; + bpp = bpp <= 1 ? 1 : bpp <= 2 ? 2 : bpp <= 4 ? 4 : bpp <= 8 ? 8 : 16; + s = pdm_screen_for_var(par, var->xres, var->yres, bpp); + if (!s || var->bits_per_pixel > 16 || + s->mode.xres * s->mode.yres * bpp / 8 > info->fix.smem_len) + return pdmfb_boot_check_var(var, info); + + fb_videomode_to_var(var, &s->mode); + var->xoffset = var->yoffset = 0; + var->height = var->width = -1; + pdmfb_set_format(var, bpp); + return 0; +} + +static void pdm_write_mode(struct pdmfb_par *par) +{ + out_8(par->regs + VC_MODE, + par->mode_value | (par->blanked ? VC_MODE_BLANK : 0)); +} + +static int pdmfb_onboard_set_par(struct fb_info *info) +{ + struct pdmfb_par *par = info->par; + const struct pdm_screen *s; + unsigned long flags; + int index; + + s = pdm_screen_for_var(par, info->var.xres, info->var.yres, + info->var.bits_per_pixel); + index = pdm_depth_index(info->var.bits_per_pixel); + + if (s && index >= 0) { + if (s->value != par->mode_value || index != par->depth_index) { + /* a boot mode we cannot set is gone from now on */ + par->has_boot_var = false; + par->mode_value = s->value; + par->depth_index = index; + pdm_write_mode(par); + out_8(par->regs + VC_DEPTH, index); + udelay(2); + out_8(par->clut + DAC_CTRL, pdm_depths[index].control); + dev_dbg(par->dev, "mode %s, %u bpp\n", s->mode.name, + info->var.bits_per_pixel); + } + /* no line pitch register, lines are packed */ + pdmfb_update_fix(info, info->var.xres * + info->var.bits_per_pixel / 8); + } else { + pdmfb_update_fix(info, par->boot_linebytes); + } + + /* the CLUT index layout depends on the depth, 16 bpp leaves it alone */ + if (info->fix.visual != FB_VISUAL_PSEUDOCOLOR) + return 0; + spin_lock_irqsave(&par->lock, flags); + par->dirty_lo = 0; + par->dirty_hi = PDMFB_CLUT_SIZE - 1; + pdmfb_flush(par); + spin_unlock_irqrestore(&par->lock, flags); + return 0; +} + +static int pdmfb_onboard_blank(int blank, struct fb_info *info) +{ + struct pdmfb_par *par = info->par; + + par->blanked = blank != FB_BLANK_UNBLANK; + if (par->mode_value != 0xff) + pdm_write_mode(par); + return 0; +} + +static void pdm_load_clut(struct pdmfb_par *par) +{ + unsigned int i; + + out_8(par->clut + DAC_ADDR, par->dirty_lo); + ndelay(260); + for (i = par->dirty_lo; i <= par->dirty_hi; i++) { + out_8(par->clut + DAC_DATA, par->cmap[i][0]); + out_8(par->clut + DAC_DATA, par->cmap[i][1]); + out_8(par->clut + DAC_DATA, par->cmap[i][2]); + ndelay(260); + } + pdmfb_clean(par); +} + +static irqreturn_t pdmfb_vbl_irq(int irq, void *data) +{ + struct pdmfb_par *par = data; + + spin_lock(&par->lock); + if (par->dirty_lo <= par->dirty_hi) + pdm_load_clut(par); + spin_unlock(&par->lock); + return IRQ_HANDLED; +} + +static int pdm_init(struct pdmfb_par *par, struct platform_device *pdev) +{ + par->clut = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(par->clut)) + return PTR_ERR(par->clut); + par->regs = devm_platform_ioremap_resource(pdev, 1); + if (IS_ERR(par->regs)) + return PTR_ERR(par->regs); + + par->display = pdm_sense_monitor(par); + if (par->display && !par->display->nscreens) + par->display = NULL; + return 0; +} + +/* the active mode as the video controller reports it */ +static bool pdm_get_hw_mode(struct pdmfb_par *par, + struct fb_var_screeninfo *var) +{ + u8 value = in_8(par->regs + VC_MODE); + u8 control = in_8(par->clut + DAC_CTRL) & 0x7f; + const struct pdm_screen *s = NULL; + unsigned int i; + int index = -1; + + for (i = 0; i < ARRAY_SIZE(pdm_screens); i++) + if (pdm_screens[i].value == value) + s = &pdm_screens[i]; + for (i = 0; i < ARRAY_SIZE(pdm_depths); i++) + if (pdm_depths[i].control == control) + index = i; + if (!s || index < 0) + return false; + + par->mode_value = value; + par->depth_index = index; + memset(var, 0, sizeof(*var)); + fb_videomode_to_var(var, &s->mode); + var->height = var->width = -1; + pdmfb_set_format(var, pdm_depths[index].depth); + return true; +} + +/* HPV */ + +static void hpv_writereg(struct pdmfb_par *par, unsigned int reg, u16 value) +{ + out_8(par->regs + HPV_OVERFLOW, (value >> 6) & 0x3f); + out_8(par->regs + reg, value & 0x3f); +} + +static u16 hpv_readreg(struct pdmfb_par *par, unsigned int reg) +{ + u16 value = in_8(par->regs + reg); + + return value | in_8(par->regs + HPV_OVERFLOW) << 6; +} + +static void hpv_load_clut(struct pdmfb_par *par) +{ + unsigned int i; + u16 status; + + hpv_writereg(par, HPV_INT_MASK, HPV_INT_VBL); + in_8(par->regs + HPV_VBL_CLEAR); + if (read_poll_timeout_atomic(hpv_readreg, status, + status & HPV_INT_VBL, 1, + PDMFB_VBL_TIMEOUT_US, false, + par, HPV_INT_STATUS)) + dev_warn_once(par->dev, "no vertical blank\n"); + hpv_writereg(par, HPV_INT_MASK, 0); + + out_8(par->regs + HPV_CLUT_ADDR, par->dirty_lo); + for (i = par->dirty_lo; i <= par->dirty_hi; i++) { + out_8(par->regs + HPV_CLUT_DATA, par->cmap[i][0]); + out_8(par->regs + HPV_CLUT_DATA, par->cmap[i][1]); + out_8(par->regs + HPV_CLUT_DATA, par->cmap[i][2]); + } + pdmfb_clean(par); +} + +static int hpv_init(struct pdmfb_par *par, struct platform_device *pdev) +{ + par->regs = devm_platform_ioremap_resource(pdev, 0); + return PTR_ERR_OR_ZERO(par->regs); +} + +/* AV */ + +static u8 av_vbl_status(struct pdmfb_par *par) +{ + return in_8(par->regs + AV_VBL_STATUS) & 1; +} + +static void av_load_clut(struct pdmfb_par *par) +{ + unsigned int i; + u8 status; + + out_8(par->regs + AV_VBL_CLEAR, 0); + out_8(par->regs + AV_VBL_CLEAR, 1); + if (read_poll_timeout_atomic(av_vbl_status, status, status, 1, + PDMFB_VBL_TIMEOUT_US, false, par)) + dev_warn_once(par->dev, "no vertical blank\n"); + + for (i = par->dirty_lo; i <= par->dirty_hi; i++) { + ndelay(260); + out_8(par->clut + AV_CLUT_ADDR, i); + out_8(par->clut + AV_CLUT_DATA, par->cmap[i][0]); + out_8(par->clut + AV_CLUT_DATA, par->cmap[i][1]); + out_8(par->clut + AV_CLUT_DATA, par->cmap[i][2]); + out_8(par->clut + AV_CLUT_DATA, 0); + } + pdmfb_clean(par); +} + +static int av_init(struct pdmfb_par *par, struct platform_device *pdev) +{ + par->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(par->regs)) + return PTR_ERR(par->regs); + par->clut = devm_platform_ioremap_resource(pdev, 1); + return PTR_ERR_OR_ZERO(par->clut); +} + +static const struct fb_ops pdmfb_onboard_ops = { + .owner = THIS_MODULE, + FB_DEFAULT_IOMEM_OPS, + .fb_check_var = pdmfb_onboard_check_var, + .fb_set_par = pdmfb_onboard_set_par, + .fb_setcolreg = pdmfb_setcolreg, + .fb_setcmap = pdmfb_setcmap, + .fb_blank = pdmfb_onboard_blank, +}; + +static const struct fb_ops pdmfb_card_ops = { + .owner = THIS_MODULE, + FB_DEFAULT_IOMEM_OPS, + .fb_check_var = pdmfb_boot_check_var, + .fb_setcolreg = pdmfb_setcolreg, + .fb_setcmap = pdmfb_setcmap, +}; + +static const struct pdmfb_variant pdmfb_onboard = { + .name = "PDM on-board", + .ops = &pdmfb_onboard_ops, + .fb_index = 2, + .init = pdm_init, + .load_clut = pdm_load_clut, +}; + +static const struct pdmfb_variant pdmfb_hpv = { + .name = "PDM HPV", + .ops = &pdmfb_card_ops, + .fb_index = 1, + .init = hpv_init, + .load_clut = hpv_load_clut, +}; + +static const struct pdmfb_variant pdmfb_av = { + .name = "PDM AV", + .ops = &pdmfb_card_ops, + .fb_index = 2, + .init = av_init, + .load_clut = av_load_clut, +}; + +static void pdmfb_release(void *data) +{ + framebuffer_release(data); +} + +static void pdmfb_iounmap(void *data) +{ + iounmap((void __iomem *)data); +} + +static int pdmfb_onboard_setup(struct fb_info *info, bool boot, + const char *option) +{ + struct pdmfb_par *par = info->par; + struct fb_var_screeninfo var; + unsigned int i; + bool set_mode; + int ret; + + par->mode_value = 0xff; + par->depth_index = 0xff; + + if (par->display) { + par->modedb_len = par->display->nscreens; + par->modedb = devm_kcalloc(par->dev, par->modedb_len, + sizeof(*par->modedb), GFP_KERNEL); + if (!par->modedb) + return -ENOMEM; + for (i = 0; i < par->modedb_len; i++) + par->modedb[i] = + pdm_screens[par->display->screens[i]].mode; + fb_videomode_to_modelist(par->modedb, par->modedb_len, + &info->modelist); + } + + if (pdm_get_hw_mode(par, &var) && !boot) { + par->boot_var = var; + par->boot_linebytes = var.xres * var.bits_per_pixel / 8; + par->has_boot_var = true; + } + + set_mode = !par->has_boot_var || (option && *option); + if (set_mode) { + if (!par->modedb_len) { + dev_info(par->dev, "no supported monitor\n"); + return -ENODEV; + } + if (!fb_find_mode(&var, info, option, par->modedb, + par->modedb_len, NULL, 8)) + return -EINVAL; + } else { + var = par->boot_var; + } + + ret = info->fbops->fb_check_var(&var, info); + if (ret) + return ret; + info->var = var; + ret = pdmfb_onboard_set_par(info); + if (ret) + return ret; + if (set_mode) + fb_memset_io(info->screen_base, 0, info->fix.smem_len); + return 0; +} + +static int pdmfb_probe(struct platform_device *pdev) +{ + const struct pdmfb_variant *variant; + struct device *dev = &pdev->dev; + struct fb_var_screeninfo boot_var; + u32 boot_addr, boot_linebytes; + struct pdmfb_par *par; + struct fb_info *info; + struct resource *fb; + char *option __free(kfree) = NULL; + bool boot; + int ret; + + if (fb_get_options("pdmfb", &option)) + return -ENODEV; + + variant = device_get_match_data(dev); + fb = platform_get_resource(pdev, IORESOURCE_MEM, variant->fb_index); + if (!fb) + return -EINVAL; + + boot = pdmfb_get_boot_display(dev->of_node, &boot_addr, &boot_var, + &boot_linebytes); + if (boot && (boot_addr != fb->start || + boot_linebytes * boot_var.yres > resource_size(fb) || + (variant == &pdmfb_onboard && + boot_var.bits_per_pixel > 16))) { + dev_warn(dev, "ignoring boot display at 0x%08x\n", boot_addr); + boot = false; + } + if (!boot && variant != &pdmfb_onboard) + return -ENODEV; + + info = framebuffer_alloc(sizeof(*par), dev); + if (!info) + return -ENOMEM; + ret = devm_add_action_or_reset(dev, pdmfb_release, info); + if (ret) + return ret; + + par = info->par; + par->variant = variant; + par->dev = dev; + spin_lock_init(&par->lock); + pdmfb_clean(par); + if (boot) { + par->boot_var = boot_var; + par->boot_linebytes = boot_linebytes; + par->has_boot_var = true; + } + + ret = variant->init(par, pdev); + if (ret) + return ret; + + /* write-through: the video fetch reads memory, the CPU reads cached */ + info->screen_base = ioremap_wt(fb->start, resource_size(fb)); + if (!info->screen_base) { + dev_err(dev, "cannot map frame buffer %pR\n", fb); + return -ENOMEM; + } + ret = devm_add_action_or_reset(dev, pdmfb_iounmap, + (void __force *)info->screen_base); + if (ret) + return ret; + + strscpy(info->fix.id, variant->name, sizeof(info->fix.id)); + info->fix.type = FB_TYPE_PACKED_PIXELS; + info->fix.accel = FB_ACCEL_NONE; + info->fix.smem_start = fb->start; + info->fix.smem_len = resource_size(fb); + info->fbops = variant->ops; + info->pseudo_palette = par->pseudo_palette; + INIT_LIST_HEAD(&info->modelist); + + ret = fb_alloc_cmap(&info->cmap, PDMFB_CLUT_SIZE, 0); + if (ret) + return ret; + + if (variant == &pdmfb_onboard) { + par->irq = platform_get_irq_optional(pdev, 0); + if (par->irq > 0) { + ret = devm_request_irq(dev, par->irq, pdmfb_vbl_irq, 0, + "pdmfb VBL", par); + if (ret) + goto err_cmap; + } + + ret = pdmfb_onboard_setup(info, boot, option); + if (ret) + goto err_cmap; + } else { + if (boot_var.bits_per_pixel < 8) { + dev_err(dev, "unsupported depth %u\n", + boot_var.bits_per_pixel); + ret = -EINVAL; + goto err_cmap; + } + info->var = boot_var; + pdmfb_update_fix(info, boot_linebytes); + } + + ret = fb_set_cmap(&info->cmap, info); + if (ret) + goto err_cmap; + + ret = register_framebuffer(info); + if (ret) + goto err_cmap; + + platform_set_drvdata(pdev, info); + fb_info(info, "%s, %ux%u-%u at %pa%s\n", variant->name, + info->var.xres, info->var.yres, info->var.bits_per_pixel, + &info->fix.smem_start, boot ? " (boot display)" : ""); + return 0; + +err_cmap: + fb_destroy_modelist(&info->modelist); + fb_dealloc_cmap(&info->cmap); + return ret; +} + +static void pdmfb_remove(struct platform_device *pdev) +{ + struct fb_info *info = platform_get_drvdata(pdev); + + unregister_framebuffer(info); + fb_dealloc_cmap(&info->cmap); +} + +static const struct of_device_id pdmfb_of_match[] = { + { .compatible = "apple,pdm-video", .data = &pdmfb_onboard }, + { .compatible = "apple,pdm-hpv", .data = &pdmfb_hpv }, + { .compatible = "apple,pdm-av", .data = &pdmfb_av }, + { } +}; +MODULE_DEVICE_TABLE(of, pdmfb_of_match); + +static struct platform_driver pdmfb_driver = { + .driver = { + .name = "pdmfb", + .of_match_table = pdmfb_of_match, + }, + .probe = pdmfb_probe, + .remove = pdmfb_remove, +}; +module_platform_driver(pdmfb_driver); + +MODULE_AUTHOR("René Rebe "); +MODULE_DESCRIPTION("NuBus Power Macintosh PDM frame buffer driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/video/fbdev/valkyriefb.c b/drivers/video/fbdev/valkyriefb.c index ec3cf4589056..d3d08527d830 100644 --- a/drivers/video/fbdev/valkyriefb.c +++ b/drivers/video/fbdev/valkyriefb.c @@ -9,6 +9,8 @@ * * Ported to m68k Macintosh by David Huggins-Daines * + * Copyright (C) 2026 René Rebe + * * Derived directly from: * * controlfb.c -- frame buffer device for the PowerMac 'control' display @@ -75,11 +77,12 @@ struct fb_par_valkyrie { struct fb_info_valkyrie { struct fb_info info; struct fb_par_valkyrie par; - struct cmap_regs __iomem *cmap_regs; + u8 __iomem *cmap_regs; unsigned long cmap_regs_phys; - struct valkyrie_regs __iomem *valkyrie_regs; + u8 __iomem *valkyrie_regs; unsigned long valkyrie_regs_phys; + unsigned int reg_shift; __u8 __iomem *frame_buffer; unsigned long frame_buffer_phys; @@ -108,6 +111,21 @@ static int valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p); static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix); static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p); +static void valkyrie_write(struct fb_info_valkyrie *p, int reg, u8 val) +{ + out_8(p->valkyrie_regs + (reg << p->reg_shift), val); +} + +static u8 valkyrie_read(struct fb_info_valkyrie *p, int reg) +{ + return in_8(p->valkyrie_regs + (reg << p->reg_shift)); +} + +static void valkyrie_cmap_write(struct fb_info_valkyrie *p, int reg, u8 val) +{ + out_8(p->cmap_regs + (reg << p->reg_shift), val); +} + static const struct fb_ops valkyriefb_ops = { .owner = THIS_MODULE, FB_DEFAULT_IOMEM_OPS, @@ -122,7 +140,6 @@ static int valkyriefb_set_par(struct fb_info *info) { struct fb_info_valkyrie *p = container_of(info, struct fb_info_valkyrie, info); - volatile struct valkyrie_regs __iomem *valkyrie_regs = p->valkyrie_regs; struct fb_par_valkyrie *par = info->par; struct valkyrie_regvals *init; int err; @@ -133,18 +150,18 @@ static int valkyriefb_set_par(struct fb_info *info) valkyrie_par_to_fix(par, &info->fix); /* Reset the valkyrie */ - out_8(&valkyrie_regs->status.r, 0); + valkyrie_write(p, VALKYRIE_STATUS, 0); udelay(100); /* Initialize display timing registers */ init = par->init; - out_8(&valkyrie_regs->mode.r, init->mode | 0x80); - out_8(&valkyrie_regs->depth.r, par->cmode + 3); + valkyrie_write(p, VALKYRIE_MODE, init->mode | 0x80); + valkyrie_write(p, VALKYRIE_DEPTH, par->cmode + 3); set_valkyrie_clock(init->clock_params); udelay(100); /* Turn on display */ - out_8(&valkyrie_regs->mode.r, init->mode); + valkyrie_write(p, VALKYRIE_MODE, init->mode); return 0; } @@ -189,7 +206,7 @@ static int valkyriefb_blank(int blank_mode, struct fb_info *info) switch (blank_mode) { case FB_BLANK_UNBLANK: /* unblank */ - out_8(&p->valkyrie_regs->mode.r, init->mode); + valkyrie_write(p, VALKYRIE_MODE, init->mode); break; case FB_BLANK_NORMAL: return 1; /* get caller to set CLUT to all black */ @@ -200,10 +217,10 @@ static int valkyriefb_blank(int blank_mode, struct fb_info *info) * whether this bit disables hsync or vsync, or * whether the hardware can do the other as well. */ - out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40); + valkyrie_write(p, VALKYRIE_MODE, init->mode | 0x40); break; case FB_BLANK_POWERDOWN: - out_8(&p->valkyrie_regs->mode.r, 0x66); + valkyrie_write(p, VALKYRIE_MODE, 0x66); break; } return 0; @@ -214,7 +231,6 @@ static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, { struct fb_info_valkyrie *p = container_of(info, struct fb_info_valkyrie, info); - volatile struct cmap_regs __iomem *cmap_regs = p->cmap_regs; struct fb_par_valkyrie *par = info->par; if (regno > 255) @@ -224,12 +240,12 @@ static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, blue >>= 8; /* tell clut which address to fill */ - out_8(&p->cmap_regs->addr, regno); + valkyrie_cmap_write(p, VALKYRIE_CMAP_ADDR, regno); udelay(1); /* send one color channel at a time */ - out_8(&cmap_regs->lut, red); - out_8(&cmap_regs->lut, green); - out_8(&cmap_regs->lut, blue); + valkyrie_cmap_write(p, VALKYRIE_CMAP_LUT, red); + valkyrie_cmap_write(p, VALKYRIE_CMAP_LUT, green); + valkyrie_cmap_write(p, VALKYRIE_CMAP_LUT, blue); if (regno < 16 && par->cmode == CMODE_16) ((u32 *)info->pseudo_palette)[regno] = @@ -299,7 +315,8 @@ static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p) static int __init valkyriefb_init(void) { struct fb_info_valkyrie *p; - unsigned long frame_buffer_phys, cmap_regs_phys; + unsigned long frame_buffer_phys, cmap_regs_phys, valkyrie_regs_phys; + unsigned int reg_shift; int err; char *option = NULL; @@ -318,24 +335,45 @@ static int __init valkyriefb_init(void) /* Hardcoded addresses... welcome to 68k Macintosh country :-) */ frame_buffer_phys = 0xf9000000; cmap_regs_phys = 0x50f24000; + valkyrie_regs_phys = cmap_regs_phys + 0x6000; + reg_shift = 2; #else /* ppc (!CONFIG_MAC) */ { struct device_node *dp; - struct resource r; - - dp = of_find_node_by_name(NULL, "valkyrie"); - if (!dp) - return 0; + struct resource r, rc, rv; + + /* NuBus Power Macintosh: frame buffer, CLUT and registers */ + dp = of_find_compatible_node(NULL, NULL, "apple,valkyrie"); + if (dp) { + if (of_address_to_resource(dp, 0, &r) || + of_address_to_resource(dp, 1, &rc) || + of_address_to_resource(dp, 2, &rv)) { + pr_err("can't find addresses for %pOF\n", dp); + of_node_put(dp); + return 0; + } + of_node_put(dp); + frame_buffer_phys = r.start; + cmap_regs_phys = rc.start; + valkyrie_regs_phys = rv.start; + reg_shift = 2; + } else { + dp = of_find_node_by_name(NULL, "valkyrie"); + if (!dp) + return 0; + + if (of_address_to_resource(dp, 0, &r)) { + printk(KERN_ERR "can't find address for valkyrie\n"); + of_node_put(dp); + return 0; + } - if (of_address_to_resource(dp, 0, &r)) { - printk(KERN_ERR "can't find address for valkyrie\n"); + frame_buffer_phys = r.start; + cmap_regs_phys = r.start + 0x304000; + valkyrie_regs_phys = cmap_regs_phys + 0x6000; + reg_shift = 3; of_node_put(dp); - return 0; } - - frame_buffer_phys = r.start; - cmap_regs_phys = r.start + 0x304000; - of_node_put(dp); } #endif /* ppc (!CONFIG_MAC) */ @@ -355,9 +393,10 @@ static int __init valkyriefb_init(void) #else p->frame_buffer = ioremap_wt(frame_buffer_phys, p->total_vram); #endif + p->reg_shift = reg_shift; p->cmap_regs_phys = cmap_regs_phys; p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000); - p->valkyrie_regs_phys = cmap_regs_phys+0x6000; + p->valkyrie_regs_phys = valkyrie_regs_phys; p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000); err = -ENOMEM; if (p->frame_buffer == NULL || p->cmap_regs == NULL @@ -402,22 +441,22 @@ static int read_valkyrie_sense(struct fb_info_valkyrie *p) { int sense, in; - out_8(&p->valkyrie_regs->msense.r, 0); /* release all lines */ + valkyrie_write(p, VALKYRIE_MSENSE, 0); /* release all lines */ __delay(20000); - sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4; + sense = ((in = valkyrie_read(p, VALKYRIE_MSENSE)) & 0x70) << 4; /* drive each sense line low in turn and collect the other 2 */ - out_8(&p->valkyrie_regs->msense.r, 4); /* drive A low */ + valkyrie_write(p, VALKYRIE_MSENSE, 4); /* drive A low */ __delay(20000); - sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30); - out_8(&p->valkyrie_regs->msense.r, 2); /* drive B low */ + sense |= ((in = valkyrie_read(p, VALKYRIE_MSENSE)) & 0x30); + valkyrie_write(p, VALKYRIE_MSENSE, 2); /* drive B low */ __delay(20000); - sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3; + sense |= ((in = valkyrie_read(p, VALKYRIE_MSENSE)) & 0x40) >> 3; sense |= (in & 0x10) >> 2; - out_8(&p->valkyrie_regs->msense.r, 1); /* drive C low */ + valkyrie_write(p, VALKYRIE_MSENSE, 1); /* drive C low */ __delay(20000); - sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5; + sense |= ((in = valkyrie_read(p, VALKYRIE_MSENSE)) & 0x60) >> 5; - out_8(&p->valkyrie_regs->msense.r, 7); + valkyrie_write(p, VALKYRIE_MSENSE, 7); return sense; } @@ -507,7 +546,7 @@ static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valk memset(fix, 0, sizeof(*fix)); strcpy(fix->id, "valkyrie"); fix->mmio_start = p->valkyrie_regs_phys; - fix->mmio_len = sizeof(struct valkyrie_regs); + fix->mmio_len = VALKYRIE_NR_REGS << p->reg_shift; fix->type = FB_TYPE_PACKED_PIXELS; fix->smem_start = p->frame_buffer_phys + 0x1000; fix->smem_len = p->total_vram; diff --git a/drivers/video/fbdev/valkyriefb.h b/drivers/video/fbdev/valkyriefb.h index bad24eec785b..634c0e76519f 100644 --- a/drivers/video/fbdev/valkyriefb.h +++ b/drivers/video/fbdev/valkyriefb.h @@ -12,6 +12,8 @@ * * Ported to 68k Macintosh by David Huggins-Daines * + * Copyright (C) 2026 René Rebe + * * Based directly on: * * controlfb.h: Constants of all sorts for controlfb @@ -35,42 +37,20 @@ * Copyright (C) 1998 Jon Howell */ -#ifdef CONFIG_MAC -/* Valkyrie registers are word-aligned on m68k */ -#define VALKYRIE_REG_PADSIZE 3 -#else -#define VALKYRIE_REG_PADSIZE 7 -#endif - -/* - * Structure of the registers for the Valkyrie colormap registers. - */ -struct cmap_regs { - unsigned char addr; - char pad1[VALKYRIE_REG_PADSIZE]; - unsigned char lut; -}; - /* - * Structure of the registers for the "valkyrie" display adaptor. + * Byte wide registers, word aligned on the Quadra 630/Performa (m68k and + * NuBus Power Macintosh), doubleword aligned on the PCI Power Macintosh. */ - -struct vpreg { /* padded register */ - unsigned char r; - char pad[VALKYRIE_REG_PADSIZE]; -}; - - -struct valkyrie_regs { - struct vpreg mode; - struct vpreg depth; - struct vpreg status; - struct vpreg reg3; - struct vpreg intr; - struct vpreg reg5; - struct vpreg intr_enb; - struct vpreg msense; -}; +#define VALKYRIE_CMAP_ADDR 0 +#define VALKYRIE_CMAP_LUT 1 + +#define VALKYRIE_MODE 0 +#define VALKYRIE_DEPTH 1 +#define VALKYRIE_STATUS 2 +#define VALKYRIE_INTR 4 +#define VALKYRIE_INTR_ENB 6 +#define VALKYRIE_MSENSE 7 +#define VALKYRIE_NR_REGS 8 /* * Register initialization tables for the valkyrie display. diff --git a/include/linux/nubus.h b/include/linux/nubus.h index b8710c825d64..0525dc8eff6a 100644 --- a/include/linux/nubus.h +++ b/include/linux/nubus.h @@ -5,6 +5,8 @@ Originally written by Alan Cox. Hacked to death by C. Scott Ananian and David Huggins-Daines. + + Copyright (C) 2026 René Rebe */ #ifndef LINUX_NUBUS_H @@ -17,11 +19,14 @@ struct proc_dir_entry; struct seq_file; +struct nubus_board; + struct nubus_dir { unsigned char *base; unsigned char *ptr; int done; int mask; + const struct nubus_board *board; struct proc_dir_entry *procdir; }; @@ -30,6 +35,7 @@ struct nubus_dirent { unsigned char type; __u32 data; /* Actually 24 bits used */ int mask; + const struct nubus_board *board; }; struct nubus_board { @@ -177,10 +183,28 @@ static inline void *nubus_get_drvdata(struct nubus_board *board) return dev_get_drvdata(&board->dev); } +/* Size of the "standard" slot space, the declaration ROM ends at its top */ +#define NUBUS_SLOT_SIZE 0x01000000 + +/* + * Architectures that cannot access the slot space at its NuBus address + * provide nubus_map_slot() and nubus_unmap_slot() in . + */ +#ifndef nubus_map_slot /* Returns a pointer to the "standard" slot space. */ static inline void *nubus_slot_addr(int slot) { return (void *)(0xF0000000 | (slot << 24)); } +static inline void *nubus_map_slot(int slot) +{ + return nubus_slot_addr(slot); +} + +static inline void nubus_unmap_slot(void *addr) +{ +} +#endif + #endif /* LINUX_NUBUS_H */ diff --git a/include/uapi/linux/pmu.h b/include/uapi/linux/pmu.h index f2fc1bd80017..e8b339981cd0 100644 --- a/include/uapi/linux/pmu.h +++ b/include/uapi/linux/pmu.h @@ -5,6 +5,7 @@ * and 2400 models as well as the RTC and various other things. * * Copyright (C) 1998 Paul Mackerras. + * Copyright (C) 2026 René Rebe */ #ifndef _UAPI_LINUX_PMU_H @@ -97,6 +98,7 @@ enum { PMU_KEYLARGO_BASED, /* Core99 motherboard (PMU99) */ PMU_68K_V1, /* Unused/deprecated */ PMU_68K_V2, /* Unused/deprecated */ + PMU_M2_BASED, /* PowerBook 5300/1400/2300/500 PPC (M2) */ }; /* PMU PMU_POWER_EVENTS commands */ diff --git a/sound/ppc/Kconfig b/sound/ppc/Kconfig index 8789cb5034ac..9e3515cd05af 100644 --- a/sound/ppc/Kconfig +++ b/sound/ppc/Kconfig @@ -34,6 +34,29 @@ config SND_POWERMAC_AUTO_DRC Note that you can turn on/off DRC manually even without this option. +config SND_AMIC_AWACS + tristate "NuBus Power Macintosh AWACS" + depends on PPC_NUBUS_PMAC + select SND_PCM + help + Say Y here to include support for the AWACS sound chip of the + PDM NuBus Power Macintosh (6100, 7100, 8100) with AMIC sound DMA. + + To compile this driver as a module, choose M here: the module + will be called snd-amic-awacs. + +config SND_PRIMETIME_ASC + tristate "Performa 5200/6200 Apple Sound Chip" + depends on PPC_NUBUS_PMAC + select SND_PCM + help + Say Y here to include support for the sound output of the + Performa/Power Macintosh 5200/6200, the Apple Sound Chip FIFOs + of the PrimeTime II I/O ASIC. + + To compile this driver as a module, choose M here: the module + will be called snd-primetime-asc. + config SND_PS3 tristate "PS3 Audio support" depends on PS3_PS3AV diff --git a/sound/ppc/Makefile b/sound/ppc/Makefile index 655bcffba843..924da024f894 100644 --- a/sound/ppc/Makefile +++ b/sound/ppc/Makefile @@ -5,7 +5,11 @@ # snd-powermac-y := powermac.o pmac.o awacs.o burgundy.o daca.o tumbler.o keywest.o beep.o +snd-amic-awacs-y := amic_awacs.o +snd-primetime-asc-y := primetime_asc.o # Toplevel Module Dependency obj-$(CONFIG_SND_POWERMAC) += snd-powermac.o +obj-$(CONFIG_SND_AMIC_AWACS) += snd-amic-awacs.o +obj-$(CONFIG_SND_PRIMETIME_ASC) += snd-primetime-asc.o obj-$(CONFIG_SND_PS3) += snd_ps3.o diff --git a/sound/ppc/amic_awacs.c b/sound/ppc/amic_awacs.c new file mode 100644 index 000000000000..fb1c4eb4859c --- /dev/null +++ b/sound/ppc/amic_awacs.c @@ -0,0 +1,843 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * AWACS sound of the PDM Power Macintosh (6100, 7100, 8100). + * + * The PDM AWACS has byte wide registers and transfers its samples to and + * from fixed buffers in the AMIC DMA area: output from two halves at + * 0x10000/0x12000, input to 0xc000/0xe000. It plays or records one half + * while the other one is refilled and interrupts for every finished half. + * Both directions share the half size and the sample rate. The driver + * copies whole ALSA periods between the ALSA buffer and the halves. + * Register usage follows MkLinux (osfmk awacs_pdm.c, awacs_pdm_hw.h) and + * MAME (awacs.cpp, macpdm.cpp); the codec registers are those of awacs.h. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include "awacs.h" + +#define DRV_NAME "amic-awacs" + +#define AW_CODEC_CTRL 0x00 /* 3 bytes: command, address/data */ +#define AW_CODEC_STAT 0x04 /* 3 bytes */ +#define AW_BUF_SIZE 0x08 /* 2 bytes, frames per half */ +#define AW_SOUND_CTRL 0x10 /* 2 bytes */ +#define AW_DMA_IN 0x14 +#define AW_DMA_OUT 0x18 + +/* first codec control byte, the expand bit reads back set while busy */ +#define CODEC_EXPAND 0x80 +#define CODEC_COMMAND 0x40 + +/* sound control: rate and output enable in the first byte */ +#define SC_OUT_ENABLE 0x01 +#define SC_RATE_SHIFT 1 +#define SC_RATE_MASK (3 << SC_RATE_SHIFT) +/* second byte: subframes; record enable as emulated by MAME */ +#define SC_IN_ENABLE 0x80 +#define SC_OUT_SUBFRAME0 0x04 +#define SC_IN_SUBFRAME0 0x00 + +/* DMA in/out control: write 1 to clear the flags, enables are plain bits */ +#define DMA_IF1 0x80 +#define DMA_IF0 0x40 +#define DMA_FLAGS 0xf0 +#define DMA_IE_HALVES 0x0c + +#define BUF_SIZE_MAX 0x7fc /* frames, multiple of 4 */ +#define FRAME_BYTES 4 +#define SENSE_POLL (HZ / 2) + +#define AWACS_REGS 5 +#define DEFAULT_GAIN 0xbb +#define DEFAULT_ATTEN 6 + +static int index = SNDRV_DEFAULT_IDX1; +static char *id = SNDRV_DEFAULT_STR1; +module_param(index, int, 0444); +MODULE_PARM_DESC(index, "Index value for the PDM AWACS."); +module_param(id, charp, 0444); +MODULE_PARM_DESC(id, "ID string for the PDM AWACS."); + +static const unsigned int awacs_rates[] = { 22050, 29400, 44100 }; + +static const struct snd_pcm_hw_constraint_list awacs_rate_list = { + .count = ARRAY_SIZE(awacs_rates), + .list = awacs_rates, +}; + +struct amic_awacs; + +struct awacs_stream { + struct amic_awacs *chip; + struct snd_pcm_substream *substream; + struct amic_dma_buf buf; + void __iomem *half[2]; + unsigned int dma_reg; + int irq; + unsigned int rate; /* set while hw_params are in effect */ + unsigned int period_frames; + ktime_t idle; /* the hardware is done with the last half */ + /* below protected by the PCM stream lock */ + bool running; /* also under the chip lock */ + unsigned int next; /* half the hardware finishes next */ + bool queued[2]; /* half holds a period to play */ + snd_pcm_uframes_t hw_off; /* frames done by the hardware */ + snd_pcm_uframes_t fill_ptr; /* next frame to copy, as appl_ptr */ +}; + +struct amic_awacs { + struct snd_card *card; + void __iomem *regs; + spinlock_t lock; /* sound control, stream parameters */ + struct awacs_stream stream[2]; + u8 sc[2]; + + struct mutex codec_lock; /* codec registers and their copy */ + u16 codec[AWACS_REGS]; + bool auto_mute; + int sense; + struct snd_kcontrol *master_sw; + struct snd_kcontrol *speaker_sw; + struct delayed_work sense_work; +}; + +static void awacs_write_sc(struct amic_awacs *chip) +{ + out_8(chip->regs + AW_SOUND_CTRL, chip->sc[0]); + out_8(chip->regs + AW_SOUND_CTRL + 1, chip->sc[1]); +} + +static void awacs_codec_write(struct amic_awacs *chip, int reg, u16 val) +{ + u16 cmd = (reg << 12) | (val & MASK_EXMODEDATA); + u8 busy; + + lockdep_assert_held(&chip->codec_lock); + chip->codec[reg] = val; + out_8(chip->regs + AW_CODEC_CTRL, CODEC_EXPAND | CODEC_COMMAND); + out_8(chip->regs + AW_CODEC_CTRL + 1, cmd >> 8); + out_8(chip->regs + AW_CODEC_CTRL + 2, cmd & 0xff); + /* MkLinux waits for the busy bit, a command takes one sample frame */ + if (readx_poll_timeout_atomic(in_8, chip->regs + AW_CODEC_CTRL, busy, + !(busy & CODEC_EXPAND), 1, 1000)) + dev_warn_once(chip->card->dev, "codec command not taken\n"); +} + +static void awacs_codec_update(struct amic_awacs *chip, int reg, u16 mask, + u16 val) +{ + val = (chip->codec[reg] & ~mask) | (val & mask); + if (val != chip->codec[reg]) + awacs_codec_write(chip, reg, val); +} + +/* like MkLinux, mute the speaker while headphones are plugged in */ +static bool awacs_apply_sense(struct amic_awacs *chip) +{ + u16 old = chip->codec[1]; + int hp; + + lockdep_assert_held(&chip->codec_lock); + hp = !!(in_8(chip->regs + AW_CODEC_STAT + 2) & MASK_HDPCONN); + if (hp == chip->sense) + return false; + chip->sense = hp; + awacs_codec_update(chip, 1, MASK_AMUTE | MASK_CMUTE, + hp ? MASK_CMUTE : MASK_AMUTE); + return chip->codec[1] != old; +} + +static void awacs_sense_work(struct work_struct *work) +{ + struct amic_awacs *chip = container_of(work, struct amic_awacs, + sense_work.work); + bool changed = false; + + scoped_guard(mutex, &chip->codec_lock) { + if (!chip->auto_mute) + return; + changed = awacs_apply_sense(chip); + } + if (changed) { + snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, + &chip->master_sw->id); + snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, + &chip->speaker_sw->id); + } + schedule_delayed_work(&chip->sense_work, SENSE_POLL); +} + +/* PCM */ + +static const struct snd_pcm_hardware awacs_pcm_hw = { + .info = SNDRV_PCM_INFO_MMAP | + SNDRV_PCM_INFO_MMAP_VALID | + SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_BLOCK_TRANSFER | + SNDRV_PCM_INFO_BATCH | + SNDRV_PCM_INFO_SYNC_APPLPTR, + .formats = SNDRV_PCM_FMTBIT_S16_BE, + .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | + SNDRV_PCM_RATE_KNOT, + .rate_min = 22050, + .rate_max = 44100, + .channels_min = 2, + .channels_max = 2, + .buffer_bytes_max = 64 * 1024, + .period_bytes_min = 64 * FRAME_BYTES, + .period_bytes_max = BUF_SIZE_MAX * FRAME_BYTES, + .periods_min = 2, + .periods_max = 1024, +}; + +static struct awacs_stream *awacs_stream(struct snd_pcm_substream *ss) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + + return &chip->stream[ss->stream]; +} + +/* rate and half size are common to both directions */ +static int awacs_rule_shared(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct awacs_stream *other = rule->private; + struct snd_interval v = { .integer = 1 }; + + scoped_guard(spinlock_irq, &other->chip->lock) + v.min = rule->var == SNDRV_PCM_HW_PARAM_RATE ? + other->rate : other->period_frames; + if (!v.min) + return 0; + v.max = v.min; + return snd_interval_refine(hw_param_interval(params, rule->var), &v); +} + +static int awacs_pcm_open(struct snd_pcm_substream *ss) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + struct awacs_stream *other = &chip->stream[!ss->stream]; + struct snd_pcm_runtime *rt = ss->runtime; + int ret; + + rt->hw = awacs_pcm_hw; + ret = snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE, + &awacs_rate_list); + if (ret < 0) + return ret; + ret = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS); + if (ret < 0) + return ret; + ret = snd_pcm_hw_constraint_step(rt, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, + 4); + if (ret < 0) + return ret; + ret = snd_pcm_hw_rule_add(rt, 0, SNDRV_PCM_HW_PARAM_RATE, + awacs_rule_shared, other, + SNDRV_PCM_HW_PARAM_RATE, -1); + if (ret < 0) + return ret; + ret = snd_pcm_hw_rule_add(rt, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, + awacs_rule_shared, other, + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); + if (ret < 0) + return ret; + + guard(spinlock_irq)(&chip->lock); + awacs_stream(ss)->substream = ss; + return 0; +} + +static int awacs_pcm_close(struct snd_pcm_substream *ss) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + + guard(spinlock_irq)(&chip->lock); + awacs_stream(ss)->substream = NULL; + return 0; +} + +static int awacs_pcm_hw_params(struct snd_pcm_substream *ss, + struct snd_pcm_hw_params *params) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + struct awacs_stream *s = awacs_stream(ss); + struct awacs_stream *other = &chip->stream[!ss->stream]; + unsigned int rate = params_rate(params); + unsigned int frames = params_period_size(params); + + guard(spinlock_irq)(&chip->lock); + if (other->rate && (other->rate != rate || + other->period_frames != frames)) + return -EBUSY; + s->rate = rate; + s->period_frames = frames; + return 0; +} + +static int awacs_pcm_hw_free(struct snd_pcm_substream *ss) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + struct awacs_stream *s = awacs_stream(ss); + + guard(spinlock_irq)(&chip->lock); + s->rate = 0; + s->period_frames = 0; + return 0; +} + +static int awacs_pcm_prepare(struct snd_pcm_substream *ss) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + struct awacs_stream *s = awacs_stream(ss); + struct awacs_stream *other = &chip->stream[!ss->stream]; + unsigned int i; + s64 delta; + + for (i = 0; i < ARRAY_SIZE(awacs_rates); i++) + if (awacs_rates[i] == ss->runtime->rate) + break; + if (i == ARRAY_SIZE(awacs_rates)) + return -EINVAL; + + /* a stopped direction ends its half first (MAME), then restarts at half 0 */ + delta = ktime_us_delta(s->idle, ktime_get()); + if (delta > 0) + fsleep(delta); + memset_io(s->buf.cpu, 0, s->buf.size); + guard(spinlock_irq)(&chip->lock); + if (!other->running) { + chip->sc[0] = (chip->sc[0] & ~SC_RATE_MASK) | + (i << SC_RATE_SHIFT); + awacs_write_sc(chip); + out_8(chip->regs + AW_BUF_SIZE, s->period_frames >> 8); + out_8(chip->regs + AW_BUF_SIZE + 1, s->period_frames & 0xff); + } + return 0; +} + +/* application frames at fill_ptr, up to a period */ +static snd_pcm_uframes_t awacs_avail(struct snd_pcm_runtime *rt, + struct awacs_stream *s) +{ + snd_pcm_sframes_t avail = rt->control->appl_ptr - s->fill_ptr; + + if (avail < 0) + avail += rt->boundary; + if (avail > rt->buffer_size) + return 0; + return min_t(snd_pcm_uframes_t, avail, rt->period_size); +} + +/* the period at fill_ptr is complete, or the stream drains */ +static bool awacs_period_ready(struct snd_pcm_runtime *rt, + struct awacs_stream *s) +{ + return rt->state == SNDRV_PCM_STATE_DRAINING || + awacs_avail(rt, s) == rt->period_size; +} + +static void awacs_advance(struct snd_pcm_runtime *rt, snd_pcm_uframes_t *ptr, + snd_pcm_uframes_t wrap) +{ + *ptr += rt->period_size; + if (*ptr >= wrap) + *ptr -= wrap; +} + +static void awacs_fill(struct awacs_stream *s, unsigned int half) +{ + struct snd_pcm_runtime *rt = s->substream->runtime; + snd_pcm_uframes_t off = s->fill_ptr % rt->buffer_size; + size_t len = frames_to_bytes(rt, awacs_avail(rt, s)); + size_t period = frames_to_bytes(rt, rt->period_size); + + /* silence beyond the application data, at the start or end */ + memcpy_toio(s->half[half], rt->dma_area + frames_to_bytes(rt, off), + len); + memset_io(s->half[half] + len, 0, period - len); + awacs_advance(rt, &s->fill_ptr, rt->boundary); + s->queued[half] = true; +} + +/* playback half done: count it, refill it or silence it until data comes */ +static void awacs_play_done(struct awacs_stream *s, unsigned int half) +{ + struct snd_pcm_runtime *rt = s->substream->runtime; + + awacs_advance(rt, &s->hw_off, rt->buffer_size); + s->queued[half] = false; + if (!s->queued[half ^ 1]) { + /* the hardware plays the silent half, skip its period */ + awacs_advance(rt, &s->fill_ptr, rt->boundary); + s->queued[half ^ 1] = true; + } + if (awacs_period_ready(rt, s)) + awacs_fill(s, half); + else + memset_io(s->half[half], 0, frames_to_bytes(rt, rt->period_size)); +} + +static void awacs_capture_done(struct awacs_stream *s, unsigned int half) +{ + struct snd_pcm_runtime *rt = s->substream->runtime; + + memcpy_fromio(rt->dma_area + frames_to_bytes(rt, s->hw_off), + s->half[half], frames_to_bytes(rt, rt->period_size)); + awacs_advance(rt, &s->hw_off, rt->buffer_size); +} + +static int awacs_pcm_trigger(struct snd_pcm_substream *ss, int cmd) +{ + struct amic_awacs *chip = snd_pcm_substream_chip(ss); + struct awacs_stream *s = awacs_stream(ss); + struct snd_pcm_runtime *rt = ss->runtime; + bool play = ss->stream == SNDRV_PCM_STREAM_PLAYBACK; + u8 *sc = play ? &chip->sc[0] : &chip->sc[1]; + u8 enable = play ? SC_OUT_ENABLE : SC_IN_ENABLE; + + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + s->next = 0; + s->hw_off = rt->status->hw_ptr % rt->buffer_size; + s->fill_ptr = rt->status->hw_ptr; + if (play) { + /* the first half plays at once, whatever is there */ + awacs_fill(s, 0); + s->queued[1] = false; + if (awacs_period_ready(rt, s)) + awacs_fill(s, 1); + else + memset_io(s->half[1], 0, + frames_to_bytes(rt, rt->period_size)); + } + scoped_guard(spinlock, &chip->lock) { + s->running = true; + out_8(chip->regs + s->dma_reg, DMA_FLAGS | DMA_IE_HALVES); + *sc |= enable; + awacs_write_sc(chip); + } + return 0; + case SNDRV_PCM_TRIGGER_STOP: + scoped_guard(spinlock, &chip->lock) { + s->running = false; + s->idle = ktime_add_us(ktime_get(), + div_u64((u64)rt->period_size * + USEC_PER_SEC, rt->rate) + + USEC_PER_MSEC); + *sc &= ~enable; + awacs_write_sc(chip); + out_8(chip->regs + s->dma_reg, DMA_FLAGS); + } + /* silence the rest of the half still playing */ + if (play) + memset_io(s->buf.cpu, 0, s->buf.size); + return 0; + } + return -EINVAL; +} + +/* fill the free playback half as soon as the application provides data */ +static int awacs_pcm_ack(struct snd_pcm_substream *ss) +{ + struct awacs_stream *s = awacs_stream(ss); + unsigned int half = s->next ^ 1; + u8 pending; + + if (ss->stream != SNDRV_PCM_STREAM_PLAYBACK || !s->running || + s->queued[half] || !awacs_period_ready(ss->runtime, s)) + return 0; + /* not if the hardware already moved on to it */ + pending = in_8(s->chip->regs + s->dma_reg); + if (pending & (s->next ? DMA_IF1 : DMA_IF0)) + return 0; + awacs_fill(s, half); + return 0; +} + +static snd_pcm_uframes_t awacs_pcm_pointer(struct snd_pcm_substream *ss) +{ + return awacs_stream(ss)->hw_off; +} + +static int awacs_pcm_sync_stop(struct snd_pcm_substream *ss) +{ + synchronize_irq(awacs_stream(ss)->irq); + return 0; +} + +static const struct snd_pcm_ops awacs_pcm_ops = { + .open = awacs_pcm_open, + .close = awacs_pcm_close, + .hw_params = awacs_pcm_hw_params, + .hw_free = awacs_pcm_hw_free, + .prepare = awacs_pcm_prepare, + .trigger = awacs_pcm_trigger, + .sync_stop = awacs_pcm_sync_stop, + .pointer = awacs_pcm_pointer, + .ack = awacs_pcm_ack, +}; + +static irqreturn_t awacs_dma_irq(int irq, void *dev_id) +{ + struct awacs_stream *s = dev_id; + struct amic_awacs *chip = s->chip; + struct snd_pcm_substream *ss; + bool running, periods = false; + u8 stat, done, bit; + + scoped_guard(spinlock, &chip->lock) { + stat = in_8(chip->regs + s->dma_reg); + if (!(stat & DMA_FLAGS)) + return IRQ_NONE; + running = s->running; + out_8(chip->regs + s->dma_reg, (stat & DMA_FLAGS) | + (running ? DMA_IE_HALVES : 0)); + } + /* the substream stays valid while running, see sync_stop */ + if (!running) + return IRQ_HANDLED; + + ss = s->substream; + snd_pcm_stream_lock(ss); + /* the flags tell which half is done, resynchronize on them */ + done = s->running ? stat & (DMA_IF0 | DMA_IF1) : 0; + while (done) { + bit = s->next ? DMA_IF1 : DMA_IF0; + if (!(done & bit)) { + s->next ^= 1; + bit ^= DMA_IF0 | DMA_IF1; + } + if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) + awacs_play_done(s, s->next); + else + awacs_capture_done(s, s->next); + s->next ^= 1; + done &= ~bit; + periods = true; + } + if (periods) + snd_pcm_period_elapsed_under_stream_lock(ss); + snd_pcm_stream_unlock(ss); + return IRQ_HANDLED; +} + +/* mixer: private_value is reg | shift << 8 | invert << 16 */ + +#define AWACS_PV(reg, shift, inv) ((reg) | ((shift) << 8) | ((inv) << 16)) +#define PV_REG(pv) ((pv) & 0xff) +#define PV_SHIFT(pv) (((pv) >> 8) & 0xff) +#define PV_INV(pv) (((pv) >> 16) & 1) + +static int awacs_volume_info(struct snd_kcontrol *kc, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 2; + uinfo->value.integer.min = 0; + uinfo->value.integer.max = 15; + return 0; +} + +/* left channel at the shift, right channel in the low four bits */ +static int awacs_volume_get(struct snd_kcontrol *kc, + struct snd_ctl_elem_value *uc) +{ + struct amic_awacs *chip = snd_kcontrol_chip(kc); + unsigned long pv = kc->private_value; + unsigned int l, r; + + scoped_guard(mutex, &chip->codec_lock) { + l = (chip->codec[PV_REG(pv)] >> PV_SHIFT(pv)) & 0xf; + r = chip->codec[PV_REG(pv)] & 0xf; + } + if (PV_INV(pv)) { + l = 15 - l; + r = 15 - r; + } + uc->value.integer.value[0] = l; + uc->value.integer.value[1] = r; + return 0; +} + +static int awacs_volume_put(struct snd_kcontrol *kc, + struct snd_ctl_elem_value *uc) +{ + struct amic_awacs *chip = snd_kcontrol_chip(kc); + unsigned long pv = kc->private_value; + long l = uc->value.integer.value[0]; + long r = uc->value.integer.value[1]; + u16 old; + + if (l < 0 || l > 15 || r < 0 || r > 15) + return -EINVAL; + if (PV_INV(pv)) { + l = 15 - l; + r = 15 - r; + } + guard(mutex)(&chip->codec_lock); + old = chip->codec[PV_REG(pv)]; + awacs_codec_update(chip, PV_REG(pv), 0xf << PV_SHIFT(pv) | 0xf, + l << PV_SHIFT(pv) | r); + return old != chip->codec[PV_REG(pv)]; +} + +static int awacs_switch_get(struct snd_kcontrol *kc, + struct snd_ctl_elem_value *uc) +{ + struct amic_awacs *chip = snd_kcontrol_chip(kc); + unsigned long pv = kc->private_value; + + guard(mutex)(&chip->codec_lock); + uc->value.integer.value[0] = + ((chip->codec[PV_REG(pv)] >> PV_SHIFT(pv)) & 1) ^ PV_INV(pv); + return 0; +} + +static int awacs_switch_put(struct snd_kcontrol *kc, + struct snd_ctl_elem_value *uc) +{ + struct amic_awacs *chip = snd_kcontrol_chip(kc); + unsigned long pv = kc->private_value; + u16 bit = BIT(PV_SHIFT(pv)); + u16 old; + + guard(mutex)(&chip->codec_lock); + old = chip->codec[PV_REG(pv)]; + awacs_codec_update(chip, PV_REG(pv), bit, + (!!uc->value.integer.value[0] ^ PV_INV(pv)) ? bit : 0); + return old != chip->codec[PV_REG(pv)]; +} + +#define AWACS_VOLUME(xname, reg, shift, inv) { \ + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ + .info = awacs_volume_info, .get = awacs_volume_get, \ + .put = awacs_volume_put, .private_value = AWACS_PV(reg, shift, inv) } + +#define AWACS_SWITCH(xname, reg, shift, inv) { \ + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ + .info = snd_ctl_boolean_mono_info, .get = awacs_switch_get, \ + .put = awacs_switch_put, .private_value = AWACS_PV(reg, shift, inv) } + +static int awacs_auto_mute_get(struct snd_kcontrol *kc, + struct snd_ctl_elem_value *uc) +{ + struct amic_awacs *chip = snd_kcontrol_chip(kc); + + guard(mutex)(&chip->codec_lock); + uc->value.integer.value[0] = chip->auto_mute; + return 0; +} + +static int awacs_auto_mute_put(struct snd_kcontrol *kc, + struct snd_ctl_elem_value *uc) +{ + struct amic_awacs *chip = snd_kcontrol_chip(kc); + bool on = uc->value.integer.value[0]; + + scoped_guard(mutex, &chip->codec_lock) { + if (on == chip->auto_mute) + return 0; + chip->auto_mute = on; + chip->sense = -1; + } + if (on) + mod_delayed_work(system_wq, &chip->sense_work, 0); + return 1; +} + +static const struct snd_kcontrol_new awacs_controls[] = { + AWACS_VOLUME("Master Playback Volume", 2, 6, 1), + AWACS_VOLUME("Speaker Playback Volume", 4, 6, 1), + AWACS_VOLUME("Master Capture Volume", 0, 4, 0), + AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0), + AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0), + AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0), + AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0), + AWACS_SWITCH("Mic Boost Capture Switch", 0, SHIFT_GAINLINE, 1), + { + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, + .name = "Auto Mute Switch", + .info = snd_ctl_boolean_mono_info, + .get = awacs_auto_mute_get, + .put = awacs_auto_mute_put, + }, +}; + +static const struct snd_kcontrol_new awacs_master_sw = + AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1); +static const struct snd_kcontrol_new awacs_speaker_sw = + AWACS_SWITCH("Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1); + +static int awacs_mixer_new(struct amic_awacs *chip) +{ + struct snd_card *card = chip->card; + unsigned int i; + int ret; + + for (i = 0; i < ARRAY_SIZE(awacs_controls); i++) { + ret = snd_ctl_add(card, snd_ctl_new1(&awacs_controls[i], chip)); + if (ret < 0) + return ret; + } + chip->master_sw = snd_ctl_new1(&awacs_master_sw, chip); + ret = snd_ctl_add(card, chip->master_sw); + if (ret < 0) + return ret; + chip->speaker_sw = snd_ctl_new1(&awacs_speaker_sw, chip); + return snd_ctl_add(card, chip->speaker_sw); +} + +/* initial setup as MkLinux: CD input with loop through, 22050 Hz */ +static void awacs_hw_init(struct amic_awacs *chip) +{ + chip->sc[0] = 0; + chip->sc[1] = SC_OUT_SUBFRAME0 | SC_IN_SUBFRAME0; + awacs_write_sc(chip); + out_8(chip->regs + AW_DMA_OUT, DMA_FLAGS); + out_8(chip->regs + AW_DMA_IN, DMA_FLAGS); + + guard(mutex)(&chip->codec_lock); + awacs_codec_write(chip, 0, MASK_MUX_CD | DEFAULT_GAIN); + awacs_codec_write(chip, 1, MASK_LOOPTHRU | MASK_PAROUT0); + awacs_codec_write(chip, 2, DEFAULT_ATTEN << 6 | DEFAULT_ATTEN); + awacs_codec_write(chip, 4, DEFAULT_ATTEN << 6 | DEFAULT_ATTEN); + chip->auto_mute = true; + chip->sense = -1; + awacs_apply_sense(chip); +} + +static void awacs_card_free(struct snd_card *card) +{ + struct amic_awacs *chip = card->private_data; + + cancel_delayed_work_sync(&chip->sense_work); + scoped_guard(spinlock_irq, &chip->lock) { + chip->sc[0] &= ~SC_OUT_ENABLE; + chip->sc[1] &= ~SC_IN_ENABLE; + awacs_write_sc(chip); + out_8(chip->regs + AW_DMA_OUT, DMA_FLAGS); + out_8(chip->regs + AW_DMA_IN, DMA_FLAGS); + } +} + +static int amic_awacs_probe(struct platform_device *pdev) +{ + static const char * const irq_names[] = { "out", "in" }; + static const enum amic_dma_area areas[] = { + AMIC_DMA_SOUND_OUT, AMIC_DMA_SOUND_IN + }; + static const unsigned int dma_regs[] = { AW_DMA_OUT, AW_DMA_IN }; + struct device *dev = &pdev->dev; + struct amic_awacs *chip; + struct snd_card *card; + struct snd_pcm *pcm; + int i, irq, ret; + + ret = snd_devm_card_new(dev, index, id, THIS_MODULE, sizeof(*chip), + &card); + if (ret < 0) + return ret; + chip = card->private_data; + chip->card = card; + spin_lock_init(&chip->lock); + mutex_init(&chip->codec_lock); + INIT_DELAYED_WORK(&chip->sense_work, awacs_sense_work); + + chip->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(chip->regs)) + return PTR_ERR(chip->regs); + + BUILD_BUG_ON(SNDRV_PCM_STREAM_PLAYBACK != 0); + for (i = 0; i < 2; i++) { + struct awacs_stream *s = &chip->stream[i]; + + s->chip = chip; + s->dma_reg = dma_regs[i]; + ret = devm_amic_dma_request(dev, areas[i], &s->buf); + if (ret) + return dev_err_probe(dev, ret, "no %s DMA buffer\n", + irq_names[i]); + s->half[0] = s->buf.cpu; + s->half[1] = s->buf.cpu + s->buf.size / 2; + } + + awacs_hw_init(chip); + card->private_free = awacs_card_free; + + for (i = 0; i < 2; i++) { + irq = platform_get_irq_byname(pdev, irq_names[i]); + if (irq < 0) + return irq; + ret = devm_request_irq(dev, irq, awacs_dma_irq, 0, + dev_name(dev), &chip->stream[i]); + if (ret) + return ret; + chip->stream[i].irq = irq; + } + + ret = snd_pcm_new(card, "AWACS", 0, 1, 1, &pcm); + if (ret < 0) + return ret; + pcm->private_data = chip; + strscpy(pcm->name, "AWACS"); + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &awacs_pcm_ops); + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &awacs_pcm_ops); + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); + + ret = awacs_mixer_new(chip); + if (ret < 0) + return ret; + + strscpy(card->driver, "AMIC AWACS"); + strscpy(card->shortname, "PDM AWACS"); + strscpy(card->mixername, "AWACS"); + snprintf(card->longname, sizeof(card->longname), + "AWACS with AMIC DMA at %pOF", dev->of_node); + + ret = snd_card_register(card); + if (ret < 0) + return ret; + schedule_delayed_work(&chip->sense_work, SENSE_POLL); + platform_set_drvdata(pdev, card); + return 0; +} + +static const struct of_device_id amic_awacs_of_match[] = { + { .compatible = "apple,amic-awacs" }, + { } +}; +MODULE_DEVICE_TABLE(of, amic_awacs_of_match); + +static struct platform_driver amic_awacs_driver = { + .probe = amic_awacs_probe, + .driver = { + .name = DRV_NAME, + .of_match_table = amic_awacs_of_match, + }, +}; +module_platform_driver(amic_awacs_driver); + +MODULE_AUTHOR("René Rebe "); +MODULE_DESCRIPTION("PDM Power Macintosh AWACS sound with AMIC DMA"); +MODULE_LICENSE("GPL"); diff --git a/sound/ppc/primetime_asc.c b/sound/ppc/primetime_asc.c new file mode 100644 index 000000000000..2e1b4816321c --- /dev/null +++ b/sound/ppc/primetime_asc.c @@ -0,0 +1,461 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Apple Sound Chip (ASC) of the PrimeTime II I/O ASIC in the Performa/Power + * Macintosh 5200/6200. + * + * The ASC plays unsigned 8-bit samples from a 1 KB FIFO per channel (A + * left, B right), written byte by byte at the FIFO address, and interrupts + * through the pseudo VIA2 (CB1) when channel B drops below half full. The + * driver then copies the next 512 frames from the ALSA buffer into both + * FIFOs by CPU, converting the sample format. The EASC variant (version + * 0xb0, which MAME emulates for PrimeTime II) plays at 44.1 kHz and steps + * through the FIFOs at lower rates with its sample rate converter; other + * variants are driven at the fixed 22254.5 Hz Macintosh rate, which is not + * verified on PrimeTime II hardware. Register usage follows MAME (asc.cpp, + * iosb.cpp) and arch/m68k/mac/macboing.c. Recording and the DFAC II + * volume/input control (Cuda I2C) are not supported, the firmware settings + * of the DFAC II are kept. + * + * Copyright (C) 2026 René Rebe + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define DRV_NAME "primetime-asc" + +#define ASC_FIFO_A 0x000 +#define ASC_FIFO_B 0x400 +#define ASC_VERSION 0x800 +#define ASC_MODE 0x801 +#define ASC_FIFO_MODE 0x803 +#define ASC_FIFO_STAT 0x804 +#define ASC_PLAY_REC_A 0x80a +#define EASC_SRC_A 0xf04 /* 2 bytes */ +#define EASC_FIFO_CTRL_A 0xf08 +#define ASC_IRQ_CTRL_A 0xf09 +#define EASC_SRC_B 0xf24 +#define EASC_FIFO_CTRL_B 0xf28 +#define ASC_IRQ_CTRL_B 0xf29 + +#define MODE_FIFO 0x01 +#define FIFO_CLEAR 0x80 +#define STAT_HALF_B 0x04 /* channel B below half full */ +#define STAT_EMPTY_B 0x08 /* channel B empty (or full) */ +#define FIFO_CTRL_SRC 0x80 +#define IRQ_DISABLE 0x01 + +#define VERSION_EASC 0xb0 +#define EASC_RATE 44100 +#define MAC_RATE 22254 +#define FIFO_SIZE 1024 +#define FIFO_HALF (FIFO_SIZE / 2) +#define SILENCE 0x80 +#define MAX_QUEUE (4 * FIFO_SIZE) /* per interrupt */ + +static int index = SNDRV_DEFAULT_IDX1; +static char *id = SNDRV_DEFAULT_STR1; +module_param(index, int, 0444); +MODULE_PARM_DESC(index, "Index value for the PrimeTime ASC."); +module_param(id, charp, 0444); +MODULE_PARM_DESC(id, "ID string for the PrimeTime ASC."); + +struct primetime_asc { + struct snd_card *card; + void __iomem *regs; + int irq; + bool easc; + u8 version; + + spinlock_t lock; /* hardware and the state below */ + struct snd_pcm_substream *substream; + bool running; /* the FIFOs play the ALSA buffer */ + bool feeding; /* the FIFO interrupt is enabled */ + unsigned int silence; /* frames of silence still to queue */ + /* below also protected by the PCM stream lock while running */ + snd_pcm_uframes_t pos; /* next frame to queue, as appl_ptr */ + snd_pcm_uframes_t period_pos; /* frames queued of the period */ + u8 fifo[2][FIFO_HALF]; +}; + +static void asc_clear_fifos(struct primetime_asc *chip) +{ + out_8(chip->regs + ASC_FIFO_MODE, FIFO_CLEAR); + out_8(chip->regs + ASC_FIFO_MODE, 0); +} + +static void asc_stop_irq(struct primetime_asc *chip) +{ + out_8(chip->regs + ASC_IRQ_CTRL_B, IRQ_DISABLE); + chip->feeding = false; +} + +static void asc_write_fifos(struct primetime_asc *chip) +{ + writesb(chip->regs + ASC_FIFO_A, chip->fifo[0], FIFO_HALF); + writesb(chip->regs + ASC_FIFO_B, chip->fifo[1], FIFO_HALF); +} + +static void asc_queue_silence(struct primetime_asc *chip) +{ + memset(chip->fifo, SILENCE, sizeof(chip->fifo)); + asc_write_fifos(chip); + chip->silence -= min_t(unsigned int, chip->silence, FIFO_HALF); +} + +static u8 asc_sample(const u8 *p, snd_pcm_format_t format) +{ + int v; + + switch (format) { + case SNDRV_PCM_FORMAT_U8: + return p[0]; + case SNDRV_PCM_FORMAT_S8: + return p[0] ^ 0x80; + case SNDRV_PCM_FORMAT_S16_BE: + v = (s16)be16_to_cpup((const __be16 *)p); + break; + default: + v = (s16)le16_to_cpup((const __le16 *)p); + break; + } + /* round to 8 bits */ + v = min((v + 0x80) >> 8, 127); + return v ^ 0x80; +} + +/* queues half a FIFO from the ALSA buffer, silence beyond the application data */ +static void asc_queue(struct primetime_asc *chip, struct snd_pcm_runtime *rt) +{ + unsigned int width = snd_pcm_format_physical_width(rt->format) / 8; + snd_pcm_sframes_t avail = rt->control->appl_ptr - chip->pos; + snd_pcm_uframes_t off = chip->pos % rt->buffer_size; + unsigned int i, n; + const u8 *p; + + if (avail < 0) + avail += rt->boundary; + n = avail > rt->buffer_size ? 0 : min_t(snd_pcm_sframes_t, avail, FIFO_HALF); + for (i = 0; i < n; i++) { + p = rt->dma_area + frames_to_bytes(rt, off); + chip->fifo[0][i] = asc_sample(p, rt->format); + chip->fifo[1][i] = rt->channels > 1 ? + asc_sample(p + width, rt->format) : + chip->fifo[0][i]; + if (++off == rt->buffer_size) + off = 0; + } + memset(chip->fifo[0] + n, SILENCE, FIFO_HALF - n); + memset(chip->fifo[1] + n, SILENCE, FIFO_HALF - n); + asc_write_fifos(chip); + + chip->pos += FIFO_HALF; + if (chip->pos >= rt->boundary) + chip->pos -= rt->boundary; + chip->period_pos += FIFO_HALF; +} + +static irqreturn_t asc_irq(int irq, void *dev_id) +{ + struct primetime_asc *chip = dev_id; + struct snd_pcm_substream *ss = NULL; + bool elapsed = false; + unsigned int chunks, queued = 0; + u8 stat; + + scoped_guard(spinlock, &chip->lock) { + if (!chip->feeding) + return IRQ_NONE; + if (chip->running) + ss = chip->substream; + } + /* the substream stays valid while running, see sync_stop */ + if (ss) + snd_pcm_stream_lock(ss); + spin_lock(&chip->lock); + /* + * The interrupt is edge latched in VIA2: refill until channel B is + * above half full, the final status read releases the ASC interrupt. + */ + while (chip->feeding) { + stat = in_8(chip->regs + ASC_FIFO_STAT); + if (!(stat & STAT_HALF_B)) + break; + if (queued >= MAX_QUEUE) { + dev_err_ratelimited(chip->card->dev, + "FIFO stays below half full\n"); + asc_stop_irq(chip); + break; + } + /* below half and empty: there is room for the whole FIFO */ + for (chunks = stat & STAT_EMPTY_B ? 2 : 1; chunks; chunks--) { + if (ss && chip->running) { + asc_queue(chip, ss->runtime); + } else if (chip->running || chip->silence) { + asc_queue_silence(chip); + } else { + asc_stop_irq(chip); + break; + } + queued += FIFO_HALF; + } + } + if (ss && chip->running && + chip->period_pos >= ss->runtime->period_size) { + chip->period_pos %= ss->runtime->period_size; + elapsed = true; + } + spin_unlock(&chip->lock); + if (elapsed) + snd_pcm_period_elapsed_under_stream_lock(ss); + if (ss) + snd_pcm_stream_unlock(ss); + return IRQ_HANDLED; +} + +/* PCM */ + +static const struct snd_pcm_hardware asc_pcm_hw = { + .info = SNDRV_PCM_INFO_MMAP | + SNDRV_PCM_INFO_MMAP_VALID | + SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_BLOCK_TRANSFER | + SNDRV_PCM_INFO_BATCH, + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | + SNDRV_PCM_FMTBIT_S16_BE | + SNDRV_PCM_FMTBIT_S16_LE, + .channels_min = 1, + .channels_max = 2, + .buffer_bytes_max = 128 * 1024, + .period_bytes_min = FIFO_HALF, + .period_bytes_max = 64 * 1024, + .periods_min = 2, + .periods_max = 1024, +}; + +static int asc_pcm_open(struct snd_pcm_substream *ss) +{ + struct primetime_asc *chip = snd_pcm_substream_chip(ss); + struct snd_pcm_runtime *rt = ss->runtime; + int ret; + + rt->hw = asc_pcm_hw; + if (chip->easc) { + rt->hw.rates = SNDRV_PCM_RATE_CONTINUOUS | + SNDRV_PCM_RATE_8000_44100; + rt->hw.rate_min = 8000; + rt->hw.rate_max = EASC_RATE; + } else { + rt->hw.rates = SNDRV_PCM_RATE_KNOT; + rt->hw.rate_min = MAC_RATE; + rt->hw.rate_max = MAC_RATE; + } + ret = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS); + if (ret < 0) + return ret; + /* a period takes at least one refill, the FIFO runs a period ahead */ + ret = snd_pcm_hw_constraint_minmax(rt, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, + FIFO_HALF, UINT_MAX); + if (ret < 0) + return ret; + ret = snd_pcm_hw_constraint_minmax(rt, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, + 2 * FIFO_SIZE, UINT_MAX); + if (ret < 0) + return ret; + + guard(spinlock_irq)(&chip->lock); + chip->substream = ss; + return 0; +} + +static int asc_pcm_close(struct snd_pcm_substream *ss) +{ + struct primetime_asc *chip = snd_pcm_substream_chip(ss); + + guard(spinlock_irq)(&chip->lock); + chip->substream = NULL; + return 0; +} + +static int asc_pcm_prepare(struct snd_pcm_substream *ss) +{ + struct primetime_asc *chip = snd_pcm_substream_chip(ss); + unsigned int rate = ss->runtime->rate; + u16 step; + u8 ctrl; + + if (!chip->easc) + return 0; + /* 16.16 FIFO step per 44.1 kHz output sample, minus one */ + step = DIV_ROUND_CLOSEST(rate << 16, EASC_RATE) - 1; + ctrl = rate < EASC_RATE ? FIFO_CTRL_SRC : 0; + + guard(spinlock_irq)(&chip->lock); + out_8(chip->regs + EASC_SRC_A, step >> 8); + out_8(chip->regs + EASC_SRC_A + 1, step & 0xff); + out_8(chip->regs + EASC_SRC_B, step >> 8); + out_8(chip->regs + EASC_SRC_B + 1, step & 0xff); + out_8(chip->regs + EASC_FIFO_CTRL_A, ctrl); + out_8(chip->regs + EASC_FIFO_CTRL_B, ctrl); + return 0; +} + +static int asc_pcm_trigger(struct snd_pcm_substream *ss, int cmd) +{ + struct primetime_asc *chip = snd_pcm_substream_chip(ss); + struct snd_pcm_runtime *rt = ss->runtime; + + guard(spinlock)(&chip->lock); + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + chip->pos = rt->status->hw_ptr; + chip->period_pos = chip->pos % rt->period_size; + chip->running = true; + /* a FIFO still playing silence is refilled by the interrupt */ + if (!chip->feeding) { + asc_clear_fifos(chip); + asc_queue(chip, rt); + chip->feeding = true; + out_8(chip->regs + ASC_IRQ_CTRL_B, 0); + } + return 0; + case SNDRV_PCM_TRIGGER_STOP: + /* the queued frames play out, followed by a FIFO of silence */ + chip->running = false; + chip->silence = FIFO_SIZE; + return 0; + } + return -EINVAL; +} + +static snd_pcm_uframes_t asc_pcm_pointer(struct snd_pcm_substream *ss) +{ + struct primetime_asc *chip = snd_pcm_substream_chip(ss); + struct snd_pcm_runtime *rt = ss->runtime; + + /* at least half a FIFO is queued beyond the pointer */ + rt->delay = FIFO_HALF; + return chip->pos % rt->buffer_size; +} + +static int asc_pcm_sync_stop(struct snd_pcm_substream *ss) +{ + struct primetime_asc *chip = snd_pcm_substream_chip(ss); + + synchronize_irq(chip->irq); + return 0; +} + +static const struct snd_pcm_ops asc_pcm_ops = { + .open = asc_pcm_open, + .close = asc_pcm_close, + .prepare = asc_pcm_prepare, + .trigger = asc_pcm_trigger, + .sync_stop = asc_pcm_sync_stop, + .pointer = asc_pcm_pointer, +}; + +static void asc_hw_init(struct primetime_asc *chip) +{ + out_8(chip->regs + ASC_IRQ_CTRL_A, IRQ_DISABLE); + out_8(chip->regs + ASC_IRQ_CTRL_B, IRQ_DISABLE); + out_8(chip->regs + ASC_MODE, MODE_FIFO); + out_8(chip->regs + ASC_PLAY_REC_A, 0); + if (chip->easc) { + out_8(chip->regs + EASC_FIFO_CTRL_A, 0); + out_8(chip->regs + EASC_FIFO_CTRL_B, 0); + } + /* start from silence, the FIFOs play out and stay idle */ + asc_clear_fifos(chip); + chip->silence = FIFO_SIZE; + while (chip->silence) + asc_queue_silence(chip); +} + +static void asc_card_free(struct snd_card *card) +{ + struct primetime_asc *chip = card->private_data; + + guard(spinlock_irq)(&chip->lock); + asc_stop_irq(chip); +} + +static int primetime_asc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct primetime_asc *chip; + struct snd_card *card; + struct snd_pcm *pcm; + int ret; + + ret = snd_devm_card_new(dev, index, id, THIS_MODULE, sizeof(*chip), + &card); + if (ret < 0) + return ret; + chip = card->private_data; + chip->card = card; + spin_lock_init(&chip->lock); + + chip->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(chip->regs)) + return PTR_ERR(chip->regs); + chip->irq = platform_get_irq(pdev, 0); + if (chip->irq < 0) + return chip->irq; + + chip->version = in_8(chip->regs + ASC_VERSION); + chip->easc = chip->version == VERSION_EASC; + asc_hw_init(chip); + card->private_free = asc_card_free; + + ret = devm_request_irq(dev, chip->irq, asc_irq, 0, dev_name(dev), chip); + if (ret) + return ret; + + ret = snd_pcm_new(card, "ASC", 0, 1, 0, &pcm); + if (ret < 0) + return ret; + pcm->private_data = chip; + strscpy(pcm->name, "ASC"); + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &asc_pcm_ops); + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); + + strscpy(card->driver, "PrimeTime ASC"); + strscpy(card->shortname, "PrimeTime ASC"); + snprintf(card->longname, sizeof(card->longname), + "Apple Sound Chip version 0x%02x at %pOF, %s", chip->version, + dev->of_node, chip->easc ? "8000-44100 Hz" : "22254 Hz"); + + ret = snd_card_register(card); + if (ret < 0) + return ret; + platform_set_drvdata(pdev, card); + return 0; +} + +static const struct of_device_id primetime_asc_of_match[] = { + { .compatible = "apple,primetime-asc" }, + { } +}; +MODULE_DEVICE_TABLE(of, primetime_asc_of_match); + +static struct platform_driver primetime_asc_driver = { + .probe = primetime_asc_probe, + .driver = { + .name = DRV_NAME, + .of_match_table = primetime_asc_of_match, + }, +}; +module_platform_driver(primetime_asc_driver); + +MODULE_AUTHOR("René Rebe "); +MODULE_DESCRIPTION("Performa 5200/6200 PrimeTime II Apple Sound Chip"); +MODULE_LICENSE("GPL"); -- 2.54.0