diff --git a/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml b/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml index c5fce7d78bfc62..253c1b78f235d0 100644 --- a/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml +++ b/Documentation/devicetree/bindings/arm/apple/apple,pmgr.yaml @@ -44,6 +44,7 @@ properties: - apple,t8132-pmgr - apple,t8140-pmgr - apple,t8142-pmgr + - apple,t8152-pmgr - const: apple,t8103-pmgr - const: syscon - const: simple-mfd diff --git a/Documentation/devicetree/bindings/dma/apple,admac.yaml b/Documentation/devicetree/bindings/dma/apple,admac.yaml index adc94ff3a3b69a..d171827c5fba32 100644 --- a/Documentation/devicetree/bindings/dma/apple,admac.yaml +++ b/Documentation/devicetree/bindings/dma/apple,admac.yaml @@ -40,6 +40,7 @@ properties: - const: apple,t8122-admac - const: apple,t8122-admac - const: apple,t8140-admac + - const: apple,t8152-admac reg: maxItems: 1 diff --git a/Documentation/devicetree/bindings/iommu/apple,dart.yaml b/Documentation/devicetree/bindings/iommu/apple,dart.yaml index 44a0499ada2cbc..ef040278db4270 100644 --- a/Documentation/devicetree/bindings/iommu/apple,dart.yaml +++ b/Documentation/devicetree/bindings/iommu/apple,dart.yaml @@ -28,6 +28,7 @@ properties: - apple,t8103-usb4-dart - apple,t8110-dart - apple,t6000-dart + - apple,t8152-dart - items: - enum: - apple,t6000-dart @@ -42,11 +43,42 @@ properties: - const: apple,t8110-dart reg: - maxItems: 1 + minItems: 1 + maxItems: 2 interrupts: maxItems: 1 + apple,static-topology: + type: boolean + description: | + apple,t8152-dart only. Do not touch the DART registers at probe. Use a + fixed topology and defer the tunables, parameter read, lock check, reset + and fault handling to the first runtime resume (the first consumer). For + DARTs behind a power or availability gate that firmware controls. + + apple,num-streams: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Stream count assumed by apple,static-topology (default 16). + + apple,inherited-roots: + type: boolean + description: | + apple,t8152-dart only. Treat the DART as locked even though its PROTECT + lock bit is clear: the bootloader left the listed streams translating + with page-table roots that hold firmware mappings, so the DART is never + reset, TCR/TTBR are never written, and consumers publish into the vacant + slots of the inherited roots (the path used for locked DARTs). + + apple,owned-streams: + $ref: /schemas/types.yaml#/definitions/uint32-array + description: | + apple,t8152-dart only. The DART is shared with firmware and + coprocessors (the T8152 SBR DART): only these stream ids belong to the + OS. Consumer iommus entries never reference another stream, latched + exceptions of other streams are not acknowledged, and the DART tunables + are left as firmware set them. + clocks: description: Reference to the gate clock phandle if required for this IOMMU. @@ -115,6 +147,22 @@ properties: and filtering state without a separate page-table root. Source and destination must differ, and both must exist on the controller. +allOf: + # Only apple,t8152-dart has a second register region (the shared TLB invalidate doorbell). + - if: + properties: + compatible: + contains: + const: apple,t8152-dart + then: + properties: + reg: + minItems: 2 + else: + properties: + reg: + maxItems: 1 + required: - compatible - reg diff --git a/Documentation/devicetree/bindings/iommu/apple,sart.yaml b/Documentation/devicetree/bindings/iommu/apple,sart.yaml index 472454c3d24d67..10fd165d1903db 100644 --- a/Documentation/devicetree/bindings/iommu/apple,sart.yaml +++ b/Documentation/devicetree/bindings/iommu/apple,sart.yaml @@ -40,6 +40,7 @@ properties: - apple,t6000-sart - apple,t8015-sart - apple,t8103-sart + - apple,t8152-sart reg: maxItems: 1 diff --git a/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml b/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml index 8d66dc77c0a54e..d3338461b93a43 100644 --- a/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml +++ b/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml @@ -65,6 +65,11 @@ properties: AOP setup-port mailbox with a separate register layout. const: apple,t8140-aop-setup-mailbox + - description: + T8152 ASCWrap v8 counted-FIFO mailbox, a plain 0x80-byte + post-office window without wrapper registers. + const: apple,t8152-ascwrap-v8-mailbox + - description: An older ASC mailbox interface found on T2 and A11 that is also used for the NVMe coprocessor and the system management @@ -155,6 +160,23 @@ allOf: - const: send-not-empty - const: recv-empty + - if: + properties: + compatible: + contains: + const: apple,t8152-ascwrap-v8-mailbox + then: + properties: + interrupts: + minItems: 2 + maxItems: 2 + interrupt-names: + items: + - const: send-empty + - const: recv-not-empty + iommus: false + apple,dma-range: false + - if: properties: compatible: @@ -164,6 +186,7 @@ allOf: - apple,t6021-ane-mailbox - apple,t8112-ane-mailbox - apple,t8140-aop-setup-mailbox + - apple,t8152-ascwrap-v8-mailbox then: properties: interrupts: diff --git a/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml b/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml index fc8a4afd5add47..d8a713e6726f60 100644 --- a/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml +++ b/Documentation/devicetree/bindings/nvme/apple,nvme-ans.yaml @@ -29,6 +29,7 @@ properties: - apple,t8112-nvme-ans2 - apple,t6000-nvme-ans2 - const: apple,nvme-ans2 + - const: apple,t8152-nvme-ans2 reg: minItems: 2 @@ -125,13 +126,24 @@ allOf: reg-names: maxItems: 2 + # T8152 has no reset line and no PMGR power domains for the ANS: the ANS is + # a virtual PMGR device there. + - if: + not: + properties: + compatible: + contains: + const: apple,t8152-nvme-ans2 + then: + required: + - resets + - power-domains + - power-domain-names + required: - compatible - reg - reg-names - - resets - - power-domains - - power-domain-names - mboxes - interrupts - apple,sart diff --git a/Documentation/devicetree/bindings/phy/apple,t8152-usb2-phy.yaml b/Documentation/devicetree/bindings/phy/apple,t8152-usb2-phy.yaml new file mode 100644 index 00000000000000..55c8b3b9a3d64c --- /dev/null +++ b/Documentation/devicetree/bindings/phy/apple,t8152-usb2-phy.yaml @@ -0,0 +1,79 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/phy/apple,t8152-usb2-phy.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T8152 USB2 PHY + +maintainers: + - Joshua Warren <816217+joshuaswarren@users.noreply.github.com> + +description: | + The USB2 and eUSB2 register banks support a fixed host or fixed peripheral + connection independently of the ATC USB3 and USB4 lanes. Firmware supplies + device-specific calibration. Both independent USB power domains must stay + active while the PHY runs. + +$defs: + tunable: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + description: | + Register offset, clear mask, and set value triples for the usb2 bank. + The bootloader supplies these at boot; they are not part of the board file. + items: + items: + - description: Aligned byte offset within the usb2 register bank + - description: Bits to clear + - description: Bits to set + +properties: + compatible: + const: apple,t8152-usb2-phy + + reg: + items: + - description: USB2 PHY register bank + - description: eUSB2 PHY register bank + + reg-names: + items: + - const: usb2 + - const: eusb2 + + '#phy-cells': + const: 0 + + power-domains: + items: + - description: USB always-on domain + - description: USB controller domain + + power-domain-names: + items: + - const: usb-aon + - const: usb + + apple,tunable-usb2phy-dflt: + $ref: '#/$defs/tunable' + description: Default calibration, applied before the role calibration. + + apple,tunable-usb2phy-host: + $ref: '#/$defs/tunable' + description: Host calibration, applied before the PHY leaves reset. + + apple,tunable-usb2phy-dev: + $ref: '#/$defs/tunable' + description: | + Device-mode calibration, applied before the PHY leaves reset in peripheral + mode. + +required: + - compatible + - reg + - reg-names + - '#phy-cells' + - power-domains + - power-domain-names + +additionalProperties: false diff --git a/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml b/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml index b8b4a24107cd1b..3a3015ddba3359 100644 --- a/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml +++ b/Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml @@ -25,6 +25,7 @@ properties: - apple,t8122-pinctrl - apple,t8132-pinctrl - apple,t8140-pinctrl + - apple,t8152-pinctrl - const: apple,t8103-pinctrl - items: # Do not add additional SoC to this list. @@ -91,6 +92,11 @@ patternProperties: additionalProperties: false + '^.+-hog(-[0-9]+)?$': + type: object + required: + - gpio-hog + allOf: - $ref: pinctrl.yaml# diff --git a/Documentation/devicetree/bindings/power/apple,pmgr-pwrstate.yaml b/Documentation/devicetree/bindings/power/apple,pmgr-pwrstate.yaml index 7800f0f106f2cd..1479bfea38c3bb 100644 --- a/Documentation/devicetree/bindings/power/apple,pmgr-pwrstate.yaml +++ b/Documentation/devicetree/bindings/power/apple,pmgr-pwrstate.yaml @@ -51,6 +51,7 @@ properties: - apple,t8132-pmgr-pwrstate - apple,t8140-pmgr-pwrstate - apple,t8142-pmgr-pwrstate + - apple,t8152-pmgr-pwrstate - const: apple,t8103-pmgr-pwrstate reg: diff --git a/Documentation/devicetree/bindings/power/apple,t8152-psd.yaml b/Documentation/devicetree/bindings/power/apple,t8152-psd.yaml new file mode 100644 index 00000000000000..4cd4c43122c90a --- /dev/null +++ b/Documentation/devicetree/bindings/power/apple,t8152-psd.yaml @@ -0,0 +1,107 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/apple,t8152-psd.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T8152 PSD service slot power domain + +maintainers: + - Joshua Warren <816217+joshuaswarren@users.noreply.github.com> + +allOf: + - $ref: power-domain.yaml# + +description: | + The T8152 (M6) power state director (PSD) gates some blocks, among them the + USB controllers and their DARTs, behind a per-port service slot. Each + slot has a 0x100 byte register window at the PSD base plus (slot << 8) with + the status (0x80), request (0x90), diagnostic (0xa0) and valid (0xbc) + registers. A consumer must power the parent PMGR domains of the block + first, then request state 0 (running) and wait for the acknowledge. This + node models that as a power domain: the PMGR domains are its parents + (power-domains), powering it on writes the request. Power-off requests + state 6 (off). + + A "apple,t8152-psd-group" node covers several slots that are brought up + together: one reg window per slot, requested in reg order with the active + state given by apple,psd-states, and put back to state 6 in reverse order. + +properties: + compatible: + enum: + - apple,t8152-psd + - apple,t8152-psd-group + + reg: + minItems: 1 + maxItems: 3 + + "#power-domain-cells": + const: 0 + + power-domains: + description: + Parent PMGR power domains, powered before the slot request. Absent when + the block has no PMGR power state. + minItems: 1 + maxItems: 4 + + label: + description: Name of the power domain. + + apple,psd-states: + $ref: /schemas/types.yaml#/definitions/uint32-array + minItems: 1 + maxItems: 3 + description: + Active PSD state requested for each reg window, in reg order (default + 0, running). Each value is 0 to 5. + + apple,psd-timeout-ms: + description: Acknowledge timeout in milliseconds (default 1000). + +required: + - compatible + - reg + - "#power-domain-cells" + +unevaluatedProperties: false + +examples: + - | + soc { + #address-cells = <2>; + #size-cells = <2>; + + ps_usb_aon: power-controller-aon { + #power-domain-cells = <0>; + }; + + ps_usb: power-controller-usb { + #power-domain-cells = <0>; + }; + + power-controller@1000 { + compatible = "apple,t8152-psd"; + reg = <0x0 0x1000 0x0 0x100>; + #power-domain-cells = <0>; + power-domains = <&ps_usb_aon>, <&ps_usb>; + label = "psd_example"; + }; + }; + - | + soc { + #address-cells = <2>; + #size-cells = <2>; + + power-controller@2000 { + compatible = "apple,t8152-psd-group"; + reg = <0x0 0x2000 0x0 0x100>, + <0x0 0x2100 0x0 0x100>, + <0x0 0x2200 0x0 0x100>; + apple,psd-states = <0 2 2>; + #power-domain-cells = <0>; + label = "psd_group_example"; + }; + }; diff --git a/Documentation/devicetree/bindings/reset/apple,t8103-cio-reset.yaml b/Documentation/devicetree/bindings/reset/apple,t8103-cio-reset.yaml index 83f307d3fc178b..8ecad084c866a9 100644 --- a/Documentation/devicetree/bindings/reset/apple,t8103-cio-reset.yaml +++ b/Documentation/devicetree/bindings/reset/apple,t8103-cio-reset.yaml @@ -22,7 +22,11 @@ description: | On t8142, a standalone register page contains a shared request register with alternating request and busy bits for four ports. - In all cases the reset id is the CIO port index on the die. + On t8152 the request and done registers are two adjacent words in the PMGR + region and, as on t600x, are a sub-node of the PMGR syscon; reg is the offset + of the request register and the reset id is the CIO cluster index. + + On the other SoCs the reset id is the CIO port index on the die. properties: compatible: @@ -31,6 +35,7 @@ properties: - apple,t8103-cio-reset - apple,t8142-cio-reset - apple,t6000-cio-reset + - apple,t8152-cio-reset - items: - const: apple,t8112-cio-reset - const: apple,t8103-cio-reset diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml index deeef58299f6d1..176a816547fc48 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml @@ -11,13 +11,16 @@ maintainers: description: The Always-On Processor provides audio and sensor services through firmware - loaded by the bootloader. The application processor uses a primary mailbox - and a separate setup-port mailbox. Each mailbox consumer has its own DMA - allocation window in the inherited AOP DART mappings. + loaded by the bootloader. On T8140 the application processor uses a primary + mailbox and a separate setup-port mailbox; on T8152 the setup port belongs to + the secure world and only the primary mailbox exists. Each mailbox consumer + has its own DMA allocation window in the inherited AOP DART mappings. properties: compatible: - const: apple,t8140-aop + enum: + - apple,t8140-aop + - apple,t8152-aop reg: items: @@ -30,11 +33,13 @@ properties: - const: asc mboxes: + minItems: 1 items: - description: Primary firmware transport - - description: Firmware setup-port transport + - description: Firmware setup-port transport (T8140 only) mbox-names: + minItems: 1 items: - const: mbox - const: setup @@ -61,6 +66,27 @@ properties: type: object $ref: /schemas/sound/apple,t8140-aop-audio.yaml# +allOf: + - if: + properties: + compatible: + const: apple,t8140-aop + then: + properties: + mboxes: + minItems: 2 + mbox-names: + minItems: 2 + required: + - power-domains + else: + properties: + mboxes: + maxItems: 1 + mbox-names: + maxItems: 1 + power-domains: false + required: - compatible - reg @@ -69,6 +95,5 @@ required: - mbox-names - iommus - apple,dma-range - - power-domains additionalProperties: false diff --git a/Documentation/devicetree/bindings/sound/apple,macaudio.yaml b/Documentation/devicetree/bindings/sound/apple,macaudio.yaml index 8e986cca62594a..6a6e911eed5ac0 100644 --- a/Documentation/devicetree/bindings/sound/apple,macaudio.yaml +++ b/Documentation/devicetree/bindings/sound/apple,macaudio.yaml @@ -21,6 +21,7 @@ properties: - apple,j293-macaudio - apple,j314-macaudio - apple,j700-macaudio + - apple,j873-macaudio - const: apple,macaudio "#address-cells": diff --git a/Documentation/devicetree/bindings/sound/apple,t8140-aop-audio.yaml b/Documentation/devicetree/bindings/sound/apple,t8140-aop-audio.yaml index b023080e48c7fc..7eb36c3b929fd1 100644 --- a/Documentation/devicetree/bindings/sound/apple,t8140-aop-audio.yaml +++ b/Documentation/devicetree/bindings/sound/apple,t8140-aop-audio.yaml @@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/sound/apple,t8140-aop-audio.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Apple T8140 AOP audio +title: Apple T8140 and T8152 AOP audio maintainers: - Ryan Murray @@ -20,22 +20,29 @@ description: | The remaining audio data moves over the named ADMAC channels while its AOP service is active. The parent AOP binding describes the firmware transport. -allOf: - - $ref: dai-common.yaml# + On T8152 (Mac mini, J873) the AOP offers only the speaker, jack output and + jack input services; the low-power and array microphones and the speaker tap + answer with kIOReturnNoDevice. The serializers and the audio fabric are + powered by the firmware, so the node has no power domains and no DART stream + of its own, and the sound card is described by a separate machine node. properties: compatible: - const: apple,t8140-aop-audio + enum: + - apple,t8140-aop-audio + - apple,t8152-aop-audio dmas: + minItems: 4 items: - description: Internal speaker output - description: Headset jack output - description: Headset microphone input - description: Speaker output monitor input - - description: High-quality microphone array input + - description: High-quality microphone array input (T8140 only) dma-names: + minItems: 4 items: - const: spkr - const: cout @@ -91,13 +98,37 @@ required: - compatible - dmas - dma-names - - power-domains - - power-domain-names - - iommus - - apple,dma-range - "#sound-dai-cells" - - apple,chassis-name - - apple,machine-kind + +allOf: + - $ref: dai-common.yaml# + - if: + properties: + compatible: + const: apple,t8140-aop-audio + then: + properties: + dmas: + minItems: 5 + dma-names: + minItems: 5 + required: + - power-domains + - power-domain-names + - iommus + - apple,dma-range + - apple,chassis-name + - apple,machine-kind + else: + properties: + dmas: + maxItems: 4 + dma-names: + maxItems: 4 + power-domains: false + power-domain-names: false + iommus: false + apple,dma-range: false unevaluatedProperties: false diff --git a/Documentation/devicetree/bindings/sound/cirrus,cs42l84.yaml b/Documentation/devicetree/bindings/sound/cirrus,cs42l84.yaml index 7f8338e8ae369b..6cc58569bf9dc1 100644 --- a/Documentation/devicetree/bindings/sound/cirrus,cs42l84.yaml +++ b/Documentation/devicetree/bindings/sound/cirrus,cs42l84.yaml @@ -15,6 +15,9 @@ description: | Pro models. It has stereo DAC for playback, mono ADC for capture, and is somewhat similar to CS42L42 but with a different regmap. +allOf: + - $ref: dai-common.yaml# + properties: compatible: enum: @@ -36,7 +39,7 @@ required: - compatible - reg -additionalProperties: false +unevaluatedProperties: false examples: - | diff --git a/Documentation/devicetree/bindings/usb/apple,t8152-dwc3.yaml b/Documentation/devicetree/bindings/usb/apple,t8152-dwc3.yaml new file mode 100644 index 00000000000000..38c1015446c9cd --- /dev/null +++ b/Documentation/devicetree/bindings/usb/apple,t8152-dwc3.yaml @@ -0,0 +1,99 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/usb/apple,t8152-dwc3.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T8152 USB2-only controller + +maintainers: + - Joshua Warren <816217+joshuaswarren@users.noreply.github.com> + +description: | + This USB2-only profile starts a fixed role at probe, with or without a + physical hub child node: host mode, or — for a debug-cable port — fixed + peripheral (gadget) mode selected with dr_mode. The controller never + switches roles at runtime. + The dr_mode and maximum-speed properties must select host or peripheral and + high-speed. + Firmware supplies device-specific calibration records as offset, clear-mask, + set-value triples. Offsets are relative to each native register bank, not + the combined Linux window. The core window ends at 0xcd00. The Apple window + starts at 0xcd00 and covers the debug, configuration, and link banks through + 0x11800. These windows must not overlap. + +allOf: + - $ref: snps,dwc3-common.yaml# + +properties: + compatible: + const: apple,t8152-dwc3 + + reg: + items: + - description: DWC3 core, offsets 0 through 0xccff + - description: Apple registers, offsets 0xcd00 through 0x117ff + - description: PIPE handler (native register bank 3) + - description: Power control (native register bank 5) + - description: Bulk fabric (native register bank 6) + - description: AUSBC debug (native register bank 8) + + reg-names: + items: + - const: dwc3-core + - const: dwc3-apple + - const: pipe-handler + - const: pwr-ctrl + - const: bulk-fabric + - const: ausbc-debug + + interrupts: + maxItems: 1 + + iommus: + minItems: 4 + maxItems: 4 + + power-domains: + maxItems: 1 + + phys: + maxItems: 1 + + phy-names: + items: + - const: usb2-phy + + dr_mode: + enum: [ host, peripheral ] + + maximum-speed: + const: high-speed + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + + +patternProperties: + '^hub@[1-9a-f][0-9a-f]?$': + $ref: /schemas/usb/usb-device.yaml# + '^apple,tunable-(bulk-fabric-default|pipe-handler-default|pipe-handler-host|pwr-ctrl-default|drd-usb31-gbl-default|drd-usb31-gbl-host|drd-cio-regs-default|drd-usb31-dbg-default|drd-usb31-cfg-host|link-regs-default|ausbc-debug-default)$': + $ref: /schemas/types.yaml#/definitions/uint32-array + description: Native bank-relative offset, clear-mask, set-value triples. + +required: + - compatible + - reg + - reg-names + - interrupts + - iommus + - power-domains + - phys + - phy-names + - dr_mode + - maximum-speed + +unevaluatedProperties: false diff --git a/arch/arm64/boot/dts/apple/t8152-j873-aop.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-aop.dtsi new file mode 100644 index 00000000000000..403fa5567f2d18 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-aop.dtsi @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * Always-on processor and its audio service. + * + * The AOP firmware is loaded by the bootloader; the CPU is started through + * the ASC wrapper and RTKit boots on the primary mailbox (there is no setup + * port on this SoC). The audio service has three devices: the speaker, the + * 3.5 mm jack output and the jack input. The audio PMGR devices are all + * firmware-managed, so none of these nodes has a power domain. Windows, + * IRQs and stream ids are from the M6 ADT. + */ + +/ { + soc { + aop_mbox: mailbox@310740080 { + compatible = "apple,t8152-ascwrap-v8-mailbox"; + reg = <0x3 0x10740080 0x0 0x80>; + interrupt-parent = <&aic>; + interrupts = , + ; + interrupt-names = "send-empty", "recv-not-empty"; + #mbox-cells = <0>; + }; + + aop: aop@310c00000 { + compatible = "apple,t8152-aop"; + reg = <0x3 0x10c00000 0x0 0x300000>, + <0x3 0x10600000 0x0 0x4000>; + reg-names = "mmio", "asc"; + mboxes = <&aop_mbox>; + mbox-names = "mbox"; + iommus = <&dart_sbr 0>; + apple,dma-range = /bits/ 64 <0x11000000000 0x1000000000>; + + aop_audio: audio { + compatible = "apple,t8152-aop-audio"; + /* base-ns channels: TX 1 = speaker, TX 5 = jack out, RX 5 = jack in, RX 1 = speaker sense */ + dmas = <&admac_base_ns 2>, <&admac_base_ns 10>, + <&admac_base_ns 11>, <&admac_base_ns 3>; + dma-names = "spkr", "cout", "cin", "spkr-tap"; + #sound-dai-cells = <1>; + }; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-audio-dart.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-audio-dart.dtsi new file mode 100644 index 00000000000000..c43ad3cb9debf3 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-audio-dart.dtsi @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * Audio DMA: the SBR DART and the two non-secure audio DMA controllers. + * + * The SBR DART is shared with firmware: the bootloader leaves its AOP and + * audio-DMA streams translating with page-table roots that hold firmware + * mappings, so it must not be reset (apple,inherited-roots) and only the + * streams Linux uses are ours (apple,owned-streams: 0 = AOP, 1 = the AOP's + * second stream, 40 = leap-ns ADMAC, 42 = base-ns ADMAC). Windows, IRQs, + * tunables and dma-ranges are from the M6 ADT. + */ + +/ { + soc { + dart_sbr: iommu@318100000 { + compatible = "apple,t8152-dart"; + reg = <0x3 0x18100000 0x0 0xc000>, + <0x2 0x00000780 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + apple,inherited-roots; + apple,owned-streams = <0 1 40 42>; + apple,dma-range = /bits/ 64 <0x11000000000 0x2000000000>; + /* ADT dart-tunables-instance-0 as (offset, mask, value); left as firmware set them */ + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x240 0x01000000 0x00000000>, + <0x244 0x01000000 0x01000000>, + <0x248 0x01000000 0x00000000>, + <0x250 0x01000000 0x00000000>, + <0x254 0x01000000 0x01000000>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + /* base-ns: 13 TX and 13 RX channels (Linux channel 2n = TX n, 2n + 1 = RX n) */ + admac_base_ns: dma-controller@313900000 { + compatible = "apple,t8152-admac"; + reg = <0x3 0x13900000 0x0 0xb4000>; + dma-channels = <26>; + #dma-cells = <1>; + /* four IRQ outputs, only the first is connected */ + interrupts-extended = <&aic AIC_IRQ 640 IRQ_TYPE_LEVEL_HIGH>, + <0>, <0>, <0>; + iommus = <&dart_sbr 42>; + apple,dma-range = /bits/ 64 <0x11000000000 0x1000000000>; + }; + + /* leap-ns: 9 TX and 9 RX channels; no ADT user, kept off */ + admac_leap_ns: dma-controller@313300000 { + compatible = "apple,t8152-admac"; + reg = <0x3 0x13300000 0x0 0xa4000>; + dma-channels = <18>; + #dma-cells = <1>; + interrupts-extended = <&aic AIC_IRQ 643 IRQ_TYPE_LEVEL_HIGH>, + <0>, <0>, <0>; + iommus = <&dart_sbr 40>; + apple,dma-range = /bits/ 64 <0x11000000000 0x1000000000>; + status = "disabled"; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-drd1.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-drd1.dtsi new file mode 100644 index 00000000000000..6ba88f24035d4b --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-drd1.dtsi @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * usb-drd1 (ATC1, rear BACK-LEFT port, port-number 2, HPM hpm1 at SPMI 0xa) as a fixed USB2 host. + * Addresses, IRQs and TLBI aperture from the saved M6 ADT: usb-drd1 core 0x40a430000, atc-phy1 0x40a418000 / + * 0x40a448000, dart-usb1 0x40b020000 / 0x40b040000 (sid-count 16, tlbi-aperture-offset 384 = 0x180, irq 1679), + * drd irq 1692. Same node layout as the drd3/drd2 hosts (both DARTs, SIDs 0 and 1). + * + * The m1n1 loader must export the port-1 USB2 PHY and DRD tunables (aliases usb2phy1 / usbdrd1), otherwise the + * PHY fails probe with -ENOENT "Invalid default tunables", exactly as drd2 did before its exporter line. + */ +/ { + aliases { + usb2phy1 = &usb2phy1; + usbdrd1 = &usbdrd1; + }; + + soc { + usb1_dart0: iommu@40b020000 { + compatible = "apple,t8152-dart"; + reg = <0x4 0x0b020000 0x0 0xc000>, + <0x2 0x00000180 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + apple,static-topology; + apple,num-streams = <16>; + power-domains = <&psd_atc1_usb>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + usb1_dart1: iommu@40b040000 { + compatible = "apple,t8152-dart"; + reg = <0x4 0x0b040000 0x0 0xc000>, + <0x2 0x00000180 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + apple,static-topology; + apple,num-streams = <16>; + power-domains = <&psd_atc1_usb>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + usb2phy1: phy@40a418000 { + compatible = "apple,t8152-usb2-phy"; + reg = <0x4 0x0a418000 0x0 0x4000>, + <0x4 0x0a448000 0x0 0x4000>; + reg-names = "usb2", "eusb2"; + #phy-cells = <0>; + power-domains = <&ps_atc1_usb_aon>, <&psd_atc1_usb>; + power-domain-names = "usb-aon", "usb"; + }; + + usbdrd1: usb@40a430000 { + compatible = "apple,t8152-dwc3"; + reg = <0x4 0x0a430000 0x0 0xcd00>, + <0x4 0x0a43cd00 0x0 0x4b00>, + <0x4 0x0a414000 0x0 0x4000>, + <0x4 0x0a410000 0x0 0x4000>, + <0x4 0x0a444000 0x0 0x4000>, + <0x4 0x0a450000 0x0 0x4000>; + reg-names = "dwc3-core", "dwc3-apple", "pipe-handler", + "pwr-ctrl", "bulk-fabric", "ausbc-debug"; + interrupts = ; + iommus = <&usb1_dart0 0>, <&usb1_dart0 1>, <&usb1_dart1 0>, <&usb1_dart1 1>; + power-domains = <&psd_atc1_usb>; + phys = <&usb2phy1>; + phy-names = "usb2-phy"; + dr_mode = "host"; + maximum-speed = "high-speed"; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-drd2.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-drd2.dtsi new file mode 100644 index 00000000000000..9b107ceea8f1b7 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-drd2.dtsi @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/ { + aliases { + usb2phy2 = &usb2phy2; + usbdrd2 = &usbdrd2; + }; + + soc { + usb2_dart0: iommu@383020000 { + compatible = "apple,t8152-dart"; + reg = <3 0x83020000 0 0xc000>, <2 0x200 0 0x10>; + interrupts = ; + #iommu-cells = <1>; + power-domains = <&psd_atc2_usb>; + apple,static-topology; + apple,num-streams = <16>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + usb2_dart1: iommu@383040000 { + compatible = "apple,t8152-dart"; + reg = <3 0x83040000 0 0xc000>, <2 0x200 0 0x10>; + interrupts = ; + #iommu-cells = <1>; + power-domains = <&psd_atc2_usb>; + apple,static-topology; + apple,num-streams = <16>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + usb2phy2: phy@382418000 { + compatible = "apple,t8152-usb2-phy"; + reg = <3 0x82418000 0 0x4000>, <3 0x82448000 0 0x4000>; + reg-names = "usb2", "eusb2"; + #phy-cells = <0>; + power-domains = <&ps_atc2_usb_aon>, <&psd_atc2_usb>; + power-domain-names = "usb-aon", "usb"; + }; + + usbdrd2: usb@382430000 { + compatible = "apple,t8152-dwc3"; + reg = <3 0x82430000 0 0xcd00>, + <3 0x8243cd00 0 0x4b00>, + <3 0x82414000 0 0x4000>, + <3 0x82410000 0 0x4000>, + <3 0x82444000 0 0x4000>, + <3 0x82450000 0 0x4000>; + reg-names = "dwc3-core", "dwc3-apple", "pipe-handler", + "pwr-ctrl", "bulk-fabric", "ausbc-debug"; + interrupts = ; + iommus = <&usb2_dart0 0>, <&usb2_dart0 1>, <&usb2_dart1 0>, <&usb2_dart1 1>; + power-domains = <&psd_atc2_usb>; + phys = <&usb2phy2>; + phy-names = "usb2-phy"; + dr_mode = "host"; + maximum-speed = "high-speed"; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-pmgr.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-pmgr.dtsi new file mode 100644 index 00000000000000..95c91876fc25b0 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-pmgr.dtsi @@ -0,0 +1,374 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * PMGR power domains for the Apple T8152 (M6), generated from the device tree of the boot firmware + * (/arm-io/pmgr-child). Real (non-virtual) devices only; + * virtual ADT devices have no PS register of their own (ANS, DPTX_PHY, ATC*_CIO_USB, ...) + * and are managed by firmware. Every domain is apple,always-on: Linux only keeps them ACTIVE. + */ + +/ { + soc { + pmgr0: power-management@300700000 { + compatible = "apple,t8152-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd"; + #address-cells = <1>; + #size-cells = <1>; + reg = <0x3 0x700000 0 0xd0000>; + }; + pmgr1: power-management@308280000 { + compatible = "apple,t8152-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd"; + #address-cells = <1>; + #size-cells = <1>; + reg = <0x3 0x8280000 0 0x80000>; + }; + }; +}; + +&pmgr0 { + ps_apcie_gp: power-controller@8c0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x8c0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_gp"; + apple,always-on; + }; + + ps_msg: power-controller@b40 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0xb40 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "msg"; + apple,always-on; + }; + + ps_gpio: power-controller@b60 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0xb60 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "gpio"; + apple,always-on; + }; + + ps_postoffice: power-controller@be0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0xbe0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "postoffice"; + apple,always-on; + }; + + ps_fpwm0: power-controller@c40 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0xc40 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "fpwm0"; + apple,always-on; + }; + + ps_apcie_st0: power-controller@ea0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0xea0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_st0"; + apple,always-on; + }; + + ps_apcie_sys_gp: power-controller@ec0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0xec0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_sys_gp"; + power-domains = <&ps_apcie_gp>; + apple,always-on; + }; + + ps_apcie_sys_st0: power-controller@1280 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1280 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_sys_st0"; + power-domains = <&ps_apcie_st0>; + apple,always-on; + }; + + ps_apcie_sysocla: power-controller@12a0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x12a0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_sysocla"; + power-domains = <&ps_apcie_sys_gp>; + apple,always-on; + }; + + ps_apcie_phy_sw: power-controller@15a0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x15a0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_phy_sw"; + power-domains = <&ps_apcie_sys_st0>, <&ps_apcie_sys_gp>; + apple,always-on; + }; + + ps_apcie_st0_ocla: power-controller@15c0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x15c0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_st0_ocla"; + power-domains = <&ps_apcie_sys_st0>; + apple,always-on; + }; + + ps_apcie_aon_aus5: power-controller@1620 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1620 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_aon_aus5"; + power-domains = <&ps_apcie_phy_sw>; + apple,always-on; + }; + + ps_apcie_aon_gp: power-controller@1640 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1640 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_aon_gp"; + power-domains = <&ps_apcie_phy_sw>; + apple,always-on; + }; + + ps_apcie_st0_aus5: power-controller@1660 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1660 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_st0_aus5"; + power-domains = <&ps_apcie_aon_aus5>; + apple,always-on; + }; + + ps_apcie_gp_aus5: power-controller@1680 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1680 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "apcie_gp_aus5"; + power-domains = <&ps_apcie_aon_gp>; + apple,always-on; + }; + +}; + +&pmgr1 { + ps_nub_spmi0: power-controller@180 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x180 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "nub_spmi0"; + apple,always-on; + }; + + ps_nub_spmi1: power-controller@1a0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1a0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "nub_spmi1"; + apple,always-on; + }; + + ps_nub_spmi_a0: power-controller@1c0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1c0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "nub_spmi_a0"; + apple,always-on; + }; + + ps_nub_spmi_a1: power-controller@1e0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x1e0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "nub_spmi_a1"; + apple,always-on; + }; + + ps_atc0_common_dp_0: power-controller@10000 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10000 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc0_common_dp_0"; + apple,always-on; + }; + + ps_atc0_common_dp_1: power-controller@10020 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10020 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc0_common_dp_1"; + apple,always-on; + }; + + ps_atc1_common_dp_0: power-controller@10040 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10040 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc1_common_dp_0"; + apple,always-on; + }; + + ps_atc1_common_dp_1: power-controller@10060 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10060 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc1_common_dp_1"; + apple,always-on; + }; + + ps_atc2_common_dp_0: power-controller@10080 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10080 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc2_common_dp_0"; + apple,always-on; + }; + + ps_atc2_common_dp_1: power-controller@100a0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x100a0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc2_common_dp_1"; + apple,always-on; + }; + + ps_atc3_common_dp_0: power-controller@100c0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x100c0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc3_common_dp_0"; + apple,always-on; + }; + + ps_atc3_common_dp_1: power-controller@100e0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x100e0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc3_common_dp_1"; + apple,always-on; + }; + + ps_atc0_usb_aon: power-controller@10100 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10100 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc0_usb_aon"; + apple,always-on; + }; + + ps_atc0_usb: power-controller@10120 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10120 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc0_usb"; + apple,always-on; + }; + + ps_atc1_usb_aon: power-controller@10140 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10140 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc1_usb_aon"; + apple,always-on; + }; + + ps_atc1_usb: power-controller@10160 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10160 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc1_usb"; + apple,always-on; + }; + + ps_atc2_usb_aon: power-controller@10180 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x10180 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc2_usb_aon"; + apple,always-on; + }; + + ps_atc2_usb: power-controller@101a0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x101a0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc2_usb"; + apple,always-on; + }; + + ps_atc3_usb_aon: power-controller@101c0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x101c0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc3_usb_aon"; + apple,always-on; + }; + + ps_atc3_usb: power-controller@101e0 { + compatible = "apple,t8152-pmgr-pwrstate", "apple,t8103-pmgr-pwrstate"; + reg = <0x101e0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "atc3_usb"; + apple,always-on; + }; + +}; + +/* + * The PSD service slots of the four USB ports sit on top of the PMGR domains of + * their port: genpd powers the parents first, then the slot request is made. + */ +&psd_atc0_usb { + power-domains = <&ps_atc0_usb_aon>, <&ps_atc0_usb>; +}; + +&psd_atc1_usb { + power-domains = <&ps_atc1_usb_aon>, <&ps_atc1_usb>; +}; + +&psd_atc2_usb { + power-domains = <&ps_atc2_usb_aon>, <&ps_atc2_usb>; +}; + +&psd_atc3_usb { + power-domains = <&ps_atc3_usb_aon>, <&ps_atc3_usb>; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-usb0-dart.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-usb0-dart.dtsi new file mode 100644 index 00000000000000..afd610963b005f --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-usb0-dart.dtsi @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* usb-drd0: two DARTBLK instances, SID 1, shared TLBI aperture offset 0x100. + * Windows, IRQ, tunables and dma-range are from the M6 ADT; + * port 3 is the fixed-host reference. */ + +/ { + soc { + usb0_dart0: iommu@403020000 { + compatible = "apple,t8152-dart"; + reg = <0x4 0x03020000 0x0 0xc000>, + <0x2 0x00000100 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + /* no DART MMIO at probe: ADT dart,gen3 manual-availability, see apple-dart.c */ + apple,static-topology; + apple,num-streams = <16>; + power-domains = <&psd_atc0_usb>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + /* ADT dart-tunables-instance-0 as (offset, mask, value) */ + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + usb0_dart1: iommu@403040000 { + compatible = "apple,t8152-dart"; + reg = <0x4 0x03040000 0x0 0xc000>, + <0x2 0x00000100 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + /* no DART MMIO at probe: ADT dart,gen3 manual-availability, see apple-dart.c */ + apple,static-topology; + apple,num-streams = <16>; + power-domains = <&psd_atc0_usb>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + /* ADT dart-tunables-instance-1: byte-identical to instance 0 */ + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-usb0.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-usb0.dtsi new file mode 100644 index 00000000000000..bebc9ce1c948d8 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-usb0.dtsi @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* usb-drd0 as a fixed high-speed peripheral: the debug cable port + * (receptacle 1, ATC0). Windows, IRQ and phy banks are from the M6 ADT; + * the layout mirrors the drd3 host node. */ + +/ { + aliases { + usb2phy0 = &usb2phy0; + usbdrd0 = &usbdrd0; + }; + + soc { + usb2phy0: phy@402418000 { + compatible = "apple,t8152-usb2-phy"; + reg = <4 0x02418000 0 0x4000>, + <4 0x02448000 0 0x4000>; + reg-names = "usb2", "eusb2"; + #phy-cells = <0>; + power-domains = <&ps_atc0_usb_aon>, <&psd_atc0_usb>; + power-domain-names = "usb-aon", "usb"; + }; + + usbdrd0: usb@402430000 { + compatible = "apple,t8152-dwc3"; + reg = <4 0x02430000 0 0xcd00>, + <4 0x0243cd00 0 0x4b00>, + <4 0x02414000 0 0x4000>, + <4 0x02410000 0 0x4000>, + <4 0x02444000 0 0x4000>, + <4 0x02450000 0 0x4000>; + reg-names = "dwc3-core", "dwc3-apple", "pipe-handler", + "pwr-ctrl", "bulk-fabric", "ausbc-debug"; + interrupts = ; + iommus = <&usb0_dart0 0>, <&usb0_dart0 1>, <&usb0_dart1 0>, <&usb0_dart1 1>; + power-domains = <&psd_atc0_usb>; + phys = <&usb2phy0>; + phy-names = "usb2-phy"; + dr_mode = "peripheral"; + maximum-speed = "high-speed"; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-usb2-dart.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-usb2-dart.dtsi new file mode 100644 index 00000000000000..46fa177b0acdab --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-usb2-dart.dtsi @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* usb-drd3: two DARTBLK instances, SID 1, shared TLBI aperture offset 0x280. */ + +/ { + soc { + usb3_dart0: iommu@413020000 { + compatible = "apple,t8152-dart"; + reg = <0x4 0x13020000 0x0 0xc000>, + <0x2 0x00000280 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + /* no DART MMIO at probe: ADT dart,gen3 manual-availability, see apple-dart.c */ + apple,static-topology; + apple,num-streams = <16>; + power-domains = <&psd_atc3_usb>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + /* ADT dart-tunables-instance-0 as (offset, mask, value) */ + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + + usb3_dart1: iommu@413040000 { + compatible = "apple,t8152-dart"; + reg = <0x4 0x13040000 0x0 0xc000>, + <0x2 0x00000280 0x0 0x10>; + interrupt-parent = <&aic>; + interrupts = ; + #iommu-cells = <1>; + /* no DART MMIO at probe: ADT dart,gen3 manual-availability, see apple-dart.c */ + apple,static-topology; + apple,num-streams = <16>; + power-domains = <&psd_atc3_usb>; + apple,dma-range = /bits/ 64 <0x10000004000 0xffff0000>; + /* ADT dart-tunables-instance-1: byte-identical to instance 0 */ + apple,tunable = <0x20c 0x80000121 0x80000121>, + <0x220 0x00000f0f 0x00000f0f>, + <0x224 0x0000ffff 0x00000909>, + <0x300 0x00001f31 0x00000001>, + <0x308 0x3ffffffc 0x10000000>, + <0x310 0x3ffffffc 0x3ffffffc>, + <0x4600 0x00000002 0x00000002>; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-usb2.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-usb2.dtsi new file mode 100644 index 00000000000000..8241b19d03adf3 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-usb2.dtsi @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/ { + aliases { + usb2phy3 = &usb2phy3; + usbdrd3 = &usbdrd3; + }; + + soc { + usb2phy3: phy@412418000 { + compatible = "apple,t8152-usb2-phy"; + reg = <4 0x12418000 0 0x4000>, + <4 0x12448000 0 0x4000>; + reg-names = "usb2", "eusb2"; + #phy-cells = <0>; + power-domains = <&ps_atc3_usb_aon>, <&psd_atc3_usb>; + power-domain-names = "usb-aon", "usb"; + }; + + usbdrd3: usb@412430000 { + compatible = "apple,t8152-dwc3"; + reg = <4 0x12430000 0 0xcd00>, + <4 0x1243cd00 0 0x4b00>, + <4 0x12414000 0 0x4000>, + <4 0x12410000 0 0x4000>, + <4 0x12444000 0 0x4000>, + <4 0x12450000 0 0x4000>; + reg-names = "dwc3-core", "dwc3-apple", "pipe-handler", + "pwr-ctrl", "bulk-fabric", "ausbc-debug"; + interrupts = ; + iommus = <&usb3_dart0 0>, <&usb3_dart0 1>, <&usb3_dart1 0>, <&usb3_dart1 1>; + power-domains = <&psd_atc3_usb>; + phys = <&usb2phy3>; + phy-names = "usb2-phy"; + dr_mode = "host"; + maximum-speed = "high-speed"; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873g-audio.dtsi b/arch/arm64/boot/dts/apple/t8152-j873g-audio.dtsi new file mode 100644 index 00000000000000..0e1eb741d362cb --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873g-audio.dtsi @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * J873g audio: the 24 MHz reference clock, the SIO pinctrl, the two I2C + * audio codecs and the sound card. + * + * ADT facts: + * i2c1 is at 0x331014000 (AIC 1414, service gate 15) with the child + * audio-codec-output (cs42l84, reg 0x4b, interrupt gpio 203, + * reset gpio 204). + * i2c3 is at 0x33101c000 (AIC 1416, service gate 17) with the child + * audio-speaker (sn012776, reg 0x38, interrupt gpio 193, reset + * gpio 202; speaker-config: one channel, amp gain 15, voltage + * sense slot 0, current sense slot 2). + * Service gates below 170 index the PSD slots: slot 15 = SIO_I2C1, + * slot 17 = SIO_I2C3, window 0x303a10000 + (slot << 8). The I2C PMGR + * devices have no power state register, so the PSD slot is the only + * power step. + */ +#include + +/ { + clkref: clock-ref { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = <24000000>; + }; + + soc { + pinctrl_ap: pinctrl@31a000000 { + compatible = "apple,t8152-pinctrl", "apple,t8103-pinctrl"; + reg = <0x3 0x1a000000 0x0 0x100000>; + + gpio-controller; + #gpio-cells = <2>; + gpio-ranges = <&pinctrl_ap 0 0 224>; + apple,npins = <224>; + + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&aic>; + interrupts = , + , + , + , + , + , + ; + }; + + psd_sio_i2c1: power-controller@303a10f00 { + compatible = "apple,t8152-psd"; + reg = <0x3 0x03a10f00 0x0 0x100>; + #power-domain-cells = <0>; + label = "psd_sio_i2c1"; + }; + + psd_sio_i2c3: power-controller@303a11100 { + compatible = "apple,t8152-psd"; + reg = <0x3 0x03a11100 0x0 0x100>; + #power-domain-cells = <0>; + label = "psd_sio_i2c3"; + }; + + i2c1: i2c@331014000 { + compatible = "apple,t8103-i2c", "apple,i2c"; + reg = <0x3 0x31014000 0x0 0x4000>; + clocks = <&clkref>; + interrupt-parent = <&aic>; + interrupts = ; + power-domains = <&psd_sio_i2c1>; + #address-cells = <1>; + #size-cells = <0>; + + jack_codec: codec@4b { + compatible = "cirrus,cs42l84"; + reg = <0x4b>; + reset-gpios = <&pinctrl_ap 204 GPIO_ACTIVE_HIGH>; + interrupts-extended = <&pinctrl_ap 203 IRQ_TYPE_LEVEL_LOW>; + #sound-dai-cells = <0>; + sound-name-prefix = "Jack"; + }; + }; + + i2c3: i2c@33101c000 { + compatible = "apple,t8103-i2c", "apple,i2c"; + reg = <0x3 0x3101c000 0x0 0x4000>; + clocks = <&clkref>; + interrupt-parent = <&aic>; + interrupts = ; + power-domains = <&psd_sio_i2c3>; + #address-cells = <1>; + #size-cells = <0>; + + speaker_codec: codec@38 { + compatible = "ti,sn012776"; + reg = <0x38>; + shutdown-gpios = <&pinctrl_ap 202 GPIO_ACTIVE_HIGH>; + interrupts-extended = <&pinctrl_ap 193 IRQ_TYPE_LEVEL_LOW>; + #sound-dai-cells = <0>; + sound-name-prefix = "Speaker"; + ti,imon-slot-no = <2>; + ti,vmon-slot-no = <0>; + }; + }; + }; + + sound { + compatible = "apple,j873-macaudio", "apple,macaudio"; + model = "Mac mini J873"; + #address-cells = <1>; + #size-cells = <0>; + + dai-link@0 { + reg = <0>; + link-name = "Headphone Jack"; + + cpu { + sound-dai = <&aop_audio 0>; + }; + + codec { + sound-dai = <&jack_codec>; + }; + }; + + /* + * The speaker is protected by the limiter of the AOP audio + * component (ceiling, high-pass and a thermal model), not by a + * user-space daemon: the machine has no sense stream for one. + */ + dai-link@1 { + reg = <1>; + link-name = "Speaker"; + + cpu { + sound-dai = <&aop_audio 4>; + }; + + codec { + sound-dai = <&speaker_codec>; + }; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873g-peripherals.dtsi b/arch/arm64/boot/dts/apple/t8152-j873g-peripherals.dtsi new file mode 100644 index 00000000000000..1099f76d189cf2 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873g-peripherals.dtsi @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * Apple Mac mini (M6), J873g: peripherals that have a driver in this tree. + * + * Only addresses, interrupt numbers and wiring are described here. The boot + * loader adds the register tunables that some of these blocks need. The + * power-management (PMGR) domains are not described yet, so nodes that need + * them are left out. + */ + +#include + +/ { + soc { + /* GPIO controller of the always-on block. Pin 39 releases the reset of the front-port USB hub. */ + nub_pinctrl: pinctrl@3081f0000 { + compatible = "apple,t8152-pinctrl", "apple,t8103-pinctrl"; + reg = <3 0x081f0000 0 0x4000>; + gpio-controller; + #gpio-cells = <2>; + gpio-ranges = <&nub_pinctrl 0 0 40>; + apple,npins = <40>; + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&aic>; + interrupts = , + , + , + , + , + , + ; + + vl822-reset-n-hog { + gpio-hog; + gpios = <39 GPIO_ACTIVE_HIGH>; + output-high; + line-name = "vl822-reset-n"; + }; + }; + + /* Power state director: one service slot per USB port, 0x100 bytes each. */ + psd_atc0_usb: power-controller@303a14700 { + compatible = "apple,t8152-psd"; + reg = <0x3 0x03a14700 0x0 0x100>; + #power-domain-cells = <0>; + label = "psd_atc0_usb"; + }; + + psd_atc1_usb: power-controller@303a14800 { + compatible = "apple,t8152-psd"; + reg = <0x3 0x03a14800 0x0 0x100>; + #power-domain-cells = <0>; + label = "psd_atc1_usb"; + }; + + psd_atc2_usb: power-controller@303a14900 { + compatible = "apple,t8152-psd"; + reg = <0x3 0x03a14900 0x0 0x100>; + #power-domain-cells = <0>; + label = "psd_atc2_usb"; + }; + + psd_atc3_usb: power-controller@303a14a00 { + compatible = "apple,t8152-psd"; + reg = <0x3 0x03a14a00 0x0 0x100>; + #power-domain-cells = <0>; + label = "psd_atc3_usb"; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873g.dts b/arch/arm64/boot/dts/apple/t8152-j873g.dts index 6b5e14036c98af..aec3f54cd27153 100644 --- a/arch/arm64/boot/dts/apple/t8152-j873g.dts +++ b/arch/arm64/boot/dts/apple/t8152-j873g.dts @@ -247,3 +247,15 @@ }; }; }; + +#include "t8152-j873g-peripherals.dtsi" +#include "t8152-j873-pmgr.dtsi" +#include "t8152-j873-usb2-dart.dtsi" +#include "t8152-j873-usb2.dtsi" +#include "t8152-j873-usb0-dart.dtsi" +#include "t8152-j873-usb0.dtsi" +#include "t8152-j873-drd2.dtsi" +#include "t8152-j873-drd1.dtsi" +#include "t8152-j873-audio-dart.dtsi" +#include "t8152-j873-aop.dtsi" +#include "t8152-j873g-audio.dtsi" diff --git a/drivers/dma/apple-admac.c b/drivers/dma/apple-admac.c index dfaeeba9d8e83e..b02e2f525bb32d 100644 --- a/drivers/dma/apple-admac.c +++ b/drivers/dma/apple-admac.c @@ -67,7 +67,15 @@ */ #define REG_CHAN_BASE_LEGACY(ch) (0x8000 + (ch) * 0x200) #define REG_CHAN_BASE_EVOLVED(ch) (((ch) & 1 ? 0xc000 : 0x8000) + ((ch) / 2) * 0x200) -#define REG_CHAN_BASE(ad, ch) ((ad)->evolved_layout ? \ +/* + * T8152 has no channel blocks at 0x8000/0xc000: every channel owns a 16 KiB + * aperture, the TX ones from 0x40000 and the RX ones from 0x80000, with the + * per-channel register layout of the other variants inside. The FIFO ports + * stay at 0x10000. + */ +#define REG_CHAN_BASE_APERTURE(ch) (((ch) & 1 ? 0x80000 : 0x40000) + ((ch) / 2) * 0x4000) +#define REG_CHAN_BASE(ad, ch) ((ad)->chan_aperture ? REG_CHAN_BASE_APERTURE(ch) : \ + (ad)->evolved_layout ? \ REG_CHAN_BASE_EVOLVED(ch) : REG_CHAN_BASE_LEGACY(ch)) #define REG_CHAN_CTL(ad, ch) (REG_CHAN_BASE(ad, ch) + 0x00) @@ -152,6 +160,7 @@ struct admac_data { bool set_unk28; bool evolved_layout; + bool chan_aperture; int irq; int irq_index; int nchannels; @@ -175,6 +184,7 @@ struct admac_tx { struct admac_hw { bool set_unk28; bool evolved_layout; + bool chan_aperture; }; static int admac_alloc_sram_carveout(struct admac_data *ad, @@ -907,6 +917,7 @@ static int admac_probe(struct platform_device *pdev) ad->nchannels = nchannels; ad->set_unk28 = hw->set_unk28; ad->evolved_layout = hw->evolved_layout; + ad->chan_aperture = hw->chan_aperture; mutex_init(&ad->cache_alloc_lock); /* @@ -1034,8 +1045,16 @@ static const struct admac_hw admac_t8140_hw = { .evolved_layout = true, }; +static const struct admac_hw admac_t8152_hw = { + /* macOS writes no global 0x28/0x2c value on this SoC */ + .set_unk28 = false, + .evolved_layout = true, + .chan_aperture = true, +}; + static const struct of_device_id admac_of_match[] = { { .compatible = "apple,t8140-admac", .data = &admac_t8140_hw }, + { .compatible = "apple,t8152-admac", .data = &admac_t8152_hw }, { .compatible = "apple,t8122-admac", .data = &admac_t8122_hw }, { .compatible = "apple,t8103-admac", .data = &admac_t8013_hw }, { .compatible = "apple,admac", .data = &admac_t8013_hw }, diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 73af536772fc20..babed7248c04db 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -179,6 +179,11 @@ #define DART_T8110_DIAG_LOCK 0x210 #define DART_T8110_DIAG_LOCK_ON_ERR BIT(1) +/* T8152 Gen3: low nibble selects the TLBI route for this DART instance */ +#define DART_GEN3_ROUTE 0x4604 +/* AppleGen3DART _DART_ERR_SID_AXI_ID/_AXI_USER: 2 and 8 words per SID record */ +#define DART_GEN3_ERR_SID_AXI_ID(sid) (DART_T8110_ERR_SID(sid) + 0x18) +#define DART_GEN3_ERR_SID_AXI_USER(sid) (DART_T8110_ERR_SID(sid) + 0x20) #define DART_T8110_ENABLE_STREAMS 0xc00 #define DART_T8110_DISABLE_STREAMS 0xc20 @@ -216,6 +221,7 @@ struct apple_dart_hw { irqreturn_t (*irq_handler)(int irq, void *dev); int (*invalidate_tlb)(struct apple_dart_stream_map *stream_map); void (*enable_remap)(struct apple_dart *dart, int sid, int remap_to); + bool gen3; u32 oas; enum io_pgtable_fmt fmt; @@ -288,6 +294,14 @@ struct apple_dart { u32 supports_bypass : 1; u32 four_level : 1; u32 locked : 1; + u32 force_available : 1; + /* + * A DART shared with firmware (the T8152 SBR DART): only the owned + * streams belong to Linux; the others still translate for firmware and + * coprocessors and are never acknowledged or referenced. + */ + u32 has_owned : 1; + DECLARE_BITMAP(owned, DART_MAX_STREAMS); u32 tunneled : 1; u32 per_stream : 1; u32 power_retained : 1; @@ -300,6 +314,18 @@ struct apple_dart { /* Resume restored the translations, but the last steps of its reset failed. */ bool reset_incomplete; struct apple_tunable *tunables; + /* + * Gen3 static topology: no DART MMIO at probe. Topology comes from fixed + * values; tunables, parameter read, lock check, reset and the fault IRQ + * wait for the first runtime resume (first consumer). + */ + u32 static_topo : 1; + u32 hw_ready : 1; + u32 irq_requested : 1; + /* Serialises the first-use init: resume, attach and the fault IRQ can all reach it. */ + spinlock_t late_lock; + /* Gen3 (T8152): shared TLBI doorbell and per-instance route register */ + void __iomem *tlbi; u16 version; DECLARE_BITMAP(remapped, DART_MAX_STREAMS); @@ -468,6 +494,8 @@ struct apple_dart_master_cfg { stream_map = &(base)->stream_maps[++i]) static struct platform_driver apple_dart_driver; +static int apple_dart_cfg_ensure_ready(struct apple_dart_master_cfg *cfg); +static int apple_dart_gen3_ensure_ready(struct apple_dart *dart); static const struct iommu_ops apple_dart_iommu_ops; static struct apple_dart_domain *to_dart_domain(struct iommu_domain *dom) @@ -1246,8 +1274,62 @@ apple_dart_t8110_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) stream_map, DART_T8110_TLB_CMD_OP_FLUSH_SID, false, 0, 0); } +/* + * Gen3 (T8152) DARTs do not take TLB invalidates through TLB_CMD. Apple's SPTM + * (gen3dart_write_tlbi_cmd_payload) stores a 128-bit payload to a TLBI + * aperture, then issues TLBI VALE1OS. Layout as used by m1n1 (accepted on + * DCP SID 23 without an exception): lo = BIT(60) | route << 56 | sid << 48. + * Writes one flush-SID doorbell per stream; no range invalidate. + */ +#ifdef CONFIG_ARM64 +static int apple_dart_gen3_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) +{ + struct apple_dart *dart = stream_map->dart; + u64 route = readl(dart->regs + DART_GEN3_ROUTE) & 0xf; + unsigned long flags; + int sid; + + spin_lock_irqsave(&dart->lock, flags); + for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) { + u64 lo = BIT_ULL(60) | route << 56 | (u64)sid << 48; + u64 hi = 0; + + dma_wmb(); + asm volatile("stp %0, %1, [%2]\n" + "dsb ish\n" + ".inst 0xd50881bf\n" /* tlbi vale1os, xzr */ + "dsb osh\n" + :: "r"(lo), "r"(hi), "r"(dart->tlbi) : "memory"); + } + spin_unlock_irqrestore(&dart->lock, flags); + return 0; +} +#else +/* The doorbell store and TLBI are arm64 instructions; other architectures build only for COMPILE_TEST. */ +static int apple_dart_gen3_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) +{ + return -EOPNOTSUPP; +} +#endif + static const char *apple_dart_t8110_v2_fault_name(u32 exception); +/* The bits of exception word @word that belong to streams Linux owns. */ +static u32 apple_dart_owned_mask(struct apple_dart *dart, unsigned int word) +{ + unsigned int bit; + u32 own = 0; + + if (!dart->has_owned) + return U32_MAX; + + for (bit = 0; bit < 32; bit++) + if (test_bit(word * 32 + bit, dart->owned)) + own |= BIT(bit); + + return own; +} + static void apple_dart_drain_stale_exceptions(struct apple_dart *dart) { unsigned int i, drained = 0; @@ -1257,9 +1339,13 @@ static void apple_dart_drain_stale_exceptions(struct apple_dart *dart) for (i = 0; i < DIV_ROUND_UP(dart->num_streams, 32) && i < 8; i++) { u32 pending = readl(dart->regs + DART_T8110_EXCEPTION(i)); - unsigned long bits = pending; + unsigned long bits; unsigned int bit; + /* Shared DART: the latched exceptions of other streams are not ours. */ + pending &= apple_dart_owned_mask(dart, i); + bits = pending; + if (!pending) continue; @@ -1295,7 +1381,8 @@ static void apple_dart_drain_stale_exceptions(struct apple_dart *dart) readl(dart->regs + DART_T8110_EXCEPTION(0)); } - if (dart->version >= 0x0203) + /* Gen3: 0x704 is not in AppleGen3DART's register capture list; do not touch it. */ + if (dart->version >= 0x0203 && !dart->hw->gen3) readl(dart->regs + DART_T8110_BLOCK_ERROR); if (drained) @@ -1313,6 +1400,13 @@ static int apple_dart_t8110_v2_unlock(struct apple_dart *dart) if (!(readl(dart->regs + DART_T8110_DIAG_LOCK) & DART_T8110_DIAG_LOCK_ON_ERR)) return 0; + /* Gen3 does not take commands through TLB_CMD, so the unlock command cannot be sent there. */ + if (dart->hw->gen3) { + dev_warn_once(dart->dev, + "Gen3 DART: unlock after a lock-on-error exception is not supported\n"); + return 0; + } + /* Unlock after acknowledging a lock-on-error exception. */ __set_bit(0, stream_map.sidmap); return apple_dart_t8110_hw_tlb_command_range(&stream_map, @@ -1407,7 +1501,8 @@ static int apple_dart_hw_finish_reset(struct apple_dart *dart) dart->hw->error); } - if (dart->hw->type == DART_T8110) + /* Gen3: 0x104 is not in AppleGen3DART's register capture list; leave it alone. */ + if (dart->hw->type == DART_T8110 && !dart->hw->gen3) apple_dart_writel(dart, 0, DART_T8110_ERROR_MASK); bitmap_set(stream_map.sidmap, 0, dart->num_streams); @@ -1480,7 +1575,8 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, if (range && (stream.dart->fw_mirror || stream.dart->fw_handoff) && stream.dart->version >= 0x0202) ret = apple_dart_fw_invalidate(&stream, start, end); - else if (range && stream.dart->locked && stream.dart->version >= 0x0202) + else if (range && stream.dart->locked && !stream.dart->hw->gen3 && + stream.dart->version >= 0x0202) ret = apple_dart_t8110_hw_tlb_command_range(&stream, op, true, start, end); else @@ -1959,6 +2055,9 @@ static int apple_dart_attach_dev_paging(struct iommu_domain *domain, for_each_stream_map(i, cfg, stream_map) WARN_ON(pm_runtime_get_sync(stream_map->dart->dev) < 0); + ret = apple_dart_cfg_ensure_ready(cfg); + if (ret) + goto err; ret = apple_dart_finalize_domain(dart_domain, cfg); if (ret) goto err; @@ -2000,6 +2099,12 @@ static int apple_dart_attach_dev_identity(struct iommu_domain *domain, for_each_stream_map(i, cfg, stream_map) WARN_ON(pm_runtime_get_sync(stream_map->dart->dev) < 0); + if (apple_dart_cfg_ensure_ready(cfg)) { + for_each_stream_map(i, cfg, stream_map) + pm_runtime_put(stream_map->dart->dev); + return -ENODEV; + } + for_each_stream_map(i, cfg, stream_map) apple_dart_hw_enable_bypass(stream_map); @@ -2031,6 +2136,12 @@ static int apple_dart_attach_dev_blocked(struct iommu_domain *domain, for_each_stream_map(i, cfg, stream_map) WARN_ON(pm_runtime_get_sync(stream_map->dart->dev) < 0); + if (apple_dart_cfg_ensure_ready(cfg)) { + for_each_stream_map(i, cfg, stream_map) + pm_runtime_put(stream_map->dart->dev); + return -ENODEV; + } + for_each_stream_map(i, cfg, stream_map) apple_dart_hw_disable_dma(stream_map); @@ -2057,10 +2168,16 @@ static struct iommu_device *apple_dart_probe_device(struct device *dev) if (!dev_iommu_fwspec_get(dev) || !cfg) return ERR_PTR(-ENODEV); - for_each_stream_map(i, cfg, stream_map) + for_each_stream_map(i, cfg, stream_map) { device_link_add(dev, stream_map->dart->dev, DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER | DL_FLAG_RPM_ACTIVE); + if (stream_map->dart->static_topo) + dev_dbg(dev, "uses static-topology DART %s, sids %*pbl, hw_ready %d, rpm status %d\n", + dev_name(stream_map->dart->dev), stream_map->dart->num_streams, + stream_map->sidmap, stream_map->dart->hw_ready, + stream_map->dart->dev->power.runtime_status); + } return &cfg->stream_maps[0].dart->iommu; } @@ -2231,6 +2348,8 @@ static int apple_dart_of_xlate(struct device *dev, return i; sid = args->args[0]; + if (dart->has_owned && !test_bit(sid, dart->owned)) + return -EINVAL; if (!cfg) { cfg = kzalloc_obj(*cfg); @@ -2707,7 +2826,7 @@ static bool apple_dart_t8110_v2_block_pending(u16 version, u32 block_error) (block_error & DART_T8110_BLOCK_ERROR_MASK); } -static irqreturn_t apple_dart_t8110_v2_irq(int irq, void *dev) +static irqreturn_t apple_dart_t8110_v2_irq(int irq, void *dev, bool gen3) { struct apple_dart *dart = dev; irqreturn_t ret = IRQ_NONE; @@ -2723,9 +2842,16 @@ static irqreturn_t apple_dart_t8110_v2_irq(int irq, void *dev) */ for (i = 0; i < words && i < 8; i++) { u32 pending = readl(dart->regs + DART_T8110_EXCEPTION(i)); - unsigned long bits = pending; + unsigned long bits; unsigned int bit; + /* + * Shared DART: neither report nor acknowledge the exceptions of + * streams that belong to firmware. + */ + pending &= apple_dart_owned_mask(dart, i); + bits = pending; + if (!pending) continue; @@ -2751,6 +2877,16 @@ static irqreturn_t apple_dart_t8110_v2_irq(int irq, void *dev) "write" : "read", apple_dart_t8110_v2_fault_name(status), addr, status); + if (gen3) + dev_err_ratelimited(dart->dev, + " SID:%u AXI_ID %#x/%#x AXI_USER %#x/%#x raw+4 %#x raw+10 %#x\n", + sid, + readl(dart->regs + DART_GEN3_ERR_SID_AXI_ID(sid)), + readl(dart->regs + DART_GEN3_ERR_SID_AXI_ID(sid) + 4), + readl(dart->regs + DART_GEN3_ERR_SID_AXI_USER(sid)), + readl(dart->regs + DART_GEN3_ERR_SID_AXI_USER(sid) + 4), + readl(dart->regs + DART_T8110_ERR_SID(sid) + 0x4), + readl(dart->regs + DART_T8110_ERR_SID(sid) + 0x10)); } /* @@ -2767,7 +2903,8 @@ static irqreturn_t apple_dart_t8110_v2_irq(int irq, void *dev) readl(dart->regs + DART_T8110_EXCEPTION(0)); } - if (dart->version >= 0x0203) { + /* Gen3: 0x704 is not in AppleGen3DART's register capture list; do not touch it. */ + if (!gen3 && dart->version >= 0x0203) { block_error = readl(dart->regs + DART_T8110_BLOCK_ERROR); if (apple_dart_t8110_v2_block_pending(dart->version, block_error)) { dev_err_ratelimited(dart->dev, @@ -2789,11 +2926,16 @@ static irqreturn_t apple_dart_t8110_irq(int irq, void *dev) struct apple_dart *dart = dev; if (dart->version >= 0x0202) - return apple_dart_t8110_v2_irq(irq, dev); + return apple_dart_t8110_v2_irq(irq, dev, false); return apple_dart_t8110_legacy_irq(irq, dev); } +static irqreturn_t apple_dart_gen3_irq(int irq, void *dev) +{ + return apple_dart_t8110_v2_irq(irq, dev, true); +} + static irqreturn_t apple_dart_irq(int irq, void *dev) { irqreturn_t ret = IRQ_NONE; @@ -2801,6 +2943,17 @@ static irqreturn_t apple_dart_irq(int irq, void *dev) if (READ_ONCE(dart->commands_gated)) return IRQ_NONE; + /* + * Static topology: no MMIO before the first consumer. A powered (runtime-active) DART that + * still raises a fault was never initialised: do the first-use init here, which also + * clears the pending error, rather than leave a level IRQ nobody claims. + */ + if (dart->static_topo && !dart->hw_ready) { + if (!pm_runtime_active(dart->dev) || apple_dart_gen3_ensure_ready(dart)) + return IRQ_NONE; + dev_warn_ratelimited(dart->dev, "fault before first-use init; initialised from the IRQ path\n"); + return IRQ_HANDLED; + } if (pm_runtime_resume_and_get(dart->dev) < 0) return IRQ_NONE; if (!READ_ONCE(dart->commands_gated)) @@ -2850,10 +3003,141 @@ apple_dart_platform_ioremap_np_resource(struct platform_device *pdev, return regs; } -static int apple_dart_probe(struct platform_device *pdev) +static const struct apple_dart_hw apple_dart_hw_t8152; + +static void apple_dart_read_params(struct apple_dart *dart) { - int ret, sid; u32 dart_params[4]; + + dart_params[0] = apple_dart_readl(dart, DART_PARAMS1); + dart_params[1] = apple_dart_readl(dart, DART_PARAMS2); + dart->pgsize = 1 << FIELD_GET(DART_PARAMS1_PAGE_SHIFT, dart_params[0]); + dart->supports_bypass = dart_params[1] & DART_PARAMS2_BYPASS_SUPPORT; + + switch (dart->hw->type) { + case DART_T8020: + case DART_T6000: + dart->ias = 32; + dart->oas = dart->hw->oas; + dart->num_streams = dart->hw->max_sid_count; + break; + + case DART_T8110: + dart_params[2] = apple_dart_readl(dart, DART_T8110_PARAMS3); + dart->ias = FIELD_GET(DART_T8110_PARAMS3_VA_WIDTH, dart_params[2]); + dart->oas = FIELD_GET(DART_T8110_PARAMS3_PA_WIDTH, dart_params[2]); + dart_params[3] = apple_dart_readl(dart, DART_T8110_PARAMS4); + dart->num_streams = FIELD_GET(DART_T8110_PARAMS4_NUM_SIDS, + dart_params[3]); + dart->four_level = dart->ias > 36; + dart->version = dart_params[2] & GENMASK(15, 0); + break; + } +} + +/* + * Gen3 static topology, no MMIO. ADT: page-size 16384 (m1n1 also expects page shift 14 + * and version 0x300), sid-count 16 for the USB DARTs (optional apple,num-streams). The + * address widths are placeholders wide enough for probe-time window checks; the + * first resume re-reads the real PARAMS and re-validates. + */ +static void apple_dart_set_static_params(struct apple_dart *dart) +{ + u32 n = 16; + + of_property_read_u32(dart->dev->of_node, "apple,num-streams", &n); + dart->pgsize = SZ_16K; + dart->supports_bypass = false; + dart->ias = 42; + dart->oas = 42; + dart->num_streams = n; + dart->four_level = true; + dart->version = 0x0300; +} + +/* + * First use of a static-topology Gen3 DART: everything probe skipped, in the safe order. + * Runs from the runtime-resume callback, which is irq-safe here: no sleeping calls. The + * fault IRQ is requested at probe (no MMIO) and ignored until hw_ready. + */ +static int apple_dart_gen3_late_init(struct apple_dart *dart) +{ + struct device *dev = dart->dev; + int ret; + u32 pgsize; + + pgsize = dart->pgsize; + apple_dart_read_params(dart); + if (dart->pgsize != pgsize) + return dev_err_probe(dev, -EINVAL, "page size %#x differs from the assumed %#x\n", + dart->pgsize, pgsize); + if (dart->num_streams > DART_MAX_STREAMS) + return dev_err_probe(dev, -EINVAL, "too many streams (%d)\n", dart->num_streams); + if (of_property_present(dev->of_node, "apple,dma-range")) { + if ((dart->dma_min ^ dart->dma_max) & ~DMA_BIT_MASK(dart->ias)) + return dev_err_probe(dev, -EINVAL, "DMA range does not fit ias=%d\n", dart->ias); + } else { + dart->dma_max = DMA_BIT_MASK(dart->ias); + } + + /* Read the lock before any write. A locked root needs the locked-TTBR path. */ + if (apple_dart_is_locked(dart)) + return dev_err_probe(dev, -EOPNOTSUPP, + "locked Gen3 DART is not supported in static-topology mode\n"); + + apple_dart_apply_tunables(dart); + + ret = apple_dart_hw_reset(dart); + if (ret) + return ret; + dart->hw_ready = true; + dev_info(dev, "Gen3 DART initialised on first use: pagesize %#x, %d streams, AS %d -> %d\n", + dart->pgsize, dart->num_streams, dart->ias, dart->oas); + return 0; +} + +/* + * Run the first-use init exactly once from whichever path gets there first: runtime + * resume, a consumer attach, or a fault IRQ on a powered but never initialised DART. + * The caller guarantees the DART is powered. Safe in atomic context. + */ +static int apple_dart_gen3_ensure_ready(struct apple_dart *dart) +{ + unsigned long flags; + int ret = 0; + + spin_lock_irqsave(&dart->late_lock, flags); + if (!dart->hw_ready) + ret = apple_dart_gen3_late_init(dart); + spin_unlock_irqrestore(&dart->late_lock, flags); + return ret; +} + +/* Every DART in a consumer's iommus list must be initialised before it is programmed. */ +static int apple_dart_cfg_ensure_ready(struct apple_dart_master_cfg *cfg) +{ + struct apple_dart_stream_map *stream_map; + int i, ret; + + for_each_stream_map(i, cfg, stream_map) { + struct apple_dart *dart = stream_map->dart; + + if (!dart->static_topo || dart->hw_ready) + continue; + dev_warn(dart->dev, + "first-use init was not run by runtime resume (rpm status %d); initialising from attach\n", + dart->dev->power.runtime_status); + ret = apple_dart_gen3_ensure_ready(dart); + if (ret) + return ret; + } + return 0; +} + +static int apple_dart_probe(struct platform_device *pdev) +{ + int ret, count; + u32 sid; struct device_node *pd_np; struct resource *res; struct apple_dart *dart; @@ -2869,6 +3153,8 @@ static int apple_dart_probe(struct platform_device *pdev) dart->dev = dev; dart->hw = of_device_get_match_data(dev); + dart->static_topo = dart->hw->gen3 && + of_property_read_bool(dev->of_node, "apple,static-topology"); dart->tunneled = tunneled; dart->per_stream = of_device_is_compatible(dev->of_node, "apple,t8140-dart") || of_device_is_compatible(dev->of_node, "apple,t8142-dart"); @@ -2881,6 +3167,7 @@ static int apple_dart_probe(struct platform_device *pdev) } } spin_lock_init(&dart->lock); + spin_lock_init(&dart->late_lock); platform_set_drvdata(pdev, dart); /* @@ -2905,7 +3192,7 @@ static int apple_dart_probe(struct platform_device *pdev) dev_err(dev, "MMIO region too small (%pr)\n", res); return -EINVAL; } - if (tunneled) { + if (tunneled || dart->hw == &apple_dart_hw_t8152) { dart->tunables = devm_apple_tunable_parse(dev, dev->of_node, "apple,tunable", res); if (IS_ERR(dart->tunables)) { @@ -2921,10 +3208,26 @@ static int apple_dart_probe(struct platform_device *pdev) dart->tunables = NULL; } else { return dev_err_probe(dev, PTR_ERR(dart->tunables), - "failed to parse PCIe-C DART tunables\n"); + "failed to parse DART tunables\n"); } } } + if (dart->hw == &apple_dart_hw_t8152) { + /* TLBI doorbell: reg[1], shared by every Gen3 DART (no mem region claim) */ + struct resource *tr = platform_get_resource(pdev, IORESOURCE_MEM, 1); + + if (!tr || resource_size(tr) < 16) + return dev_err_probe(dev, -EINVAL, "Gen3 DART needs a 16-byte TLBI reg[1]\n"); + /* + * The doorbell uses the normal Device mapping, like the registers of the + * on-SoC DARTs; only the tunneled DARTs need ioremap_np(). Each doorbell + * store is followed by dsb ish, the TLBI and dsb osh (see the invalidate + * function), the vendor sequence. + */ + dart->tlbi = devm_ioremap(dev, tr->start, resource_size(tr)); + if (!dart->tlbi) + return -ENOMEM; + } dart->irq = platform_get_irq(pdev, 0); if (dart->irq < 0) @@ -2950,6 +3253,19 @@ static int apple_dart_probe(struct platform_device *pdev) dev_dbg(dev, "applying %zu PCIe-C DART tunables\n", dart->tunables->sz); apple_dart_apply_tunables(dart); + } else if (dart->hw == &apple_dart_hw_t8152 && !dart->static_topo) { + /* + * Firmware (SPTM) owns a locked Gen3 DART: its protection and tunables are + * already set and the 0x20c group sits in the write-protect block, so decide + * lock state with a read before any write. + */ + if (apple_dart_is_locked(dart)) + dev_info(dev, "Gen3 DART is locked: not applying tunables\n"); + else if (of_property_read_bool(dev->of_node, "apple,inherited-roots") || + of_property_present(dev->of_node, "apple,owned-streams")) + dev_info(dev, "shared Gen3 DART: tunables left as firmware set them\n"); + else + apple_dart_apply_tunables(dart); } if (tunneled) { @@ -2983,30 +3299,10 @@ static int apple_dart_probe(struct platform_device *pdev) goto params_done; } - dart_params[0] = apple_dart_readl(dart, DART_PARAMS1); - dart_params[1] = apple_dart_readl(dart, DART_PARAMS2); - dart->pgsize = 1 << FIELD_GET(DART_PARAMS1_PAGE_SHIFT, dart_params[0]); - dart->supports_bypass = dart_params[1] & DART_PARAMS2_BYPASS_SUPPORT; - - switch (dart->hw->type) { - case DART_T8020: - case DART_T6000: - dart->ias = 32; - dart->oas = dart->hw->oas; - dart->num_streams = dart->hw->max_sid_count; - break; - - case DART_T8110: - dart_params[2] = apple_dart_readl(dart, DART_T8110_PARAMS3); - dart->ias = FIELD_GET(DART_T8110_PARAMS3_VA_WIDTH, dart_params[2]); - dart->oas = FIELD_GET(DART_T8110_PARAMS3_PA_WIDTH, dart_params[2]); - dart_params[3] = apple_dart_readl(dart, DART_T8110_PARAMS4); - dart->num_streams = FIELD_GET(DART_T8110_PARAMS4_NUM_SIDS, - dart_params[3]); - dart->four_level = dart->ias > 36; - dart->version = dart_params[2] & GENMASK(15, 0); - break; - } + if (dart->static_topo) + apple_dart_set_static_params(dart); + else + apple_dart_read_params(dart); params_done: dart->dma_min = 0; @@ -3092,30 +3388,63 @@ static int apple_dart_probe(struct platform_device *pdev) if (ret) goto err_clk_disable; - /* A hot-plug PCIe-C DART has no retained/locked translation context. */ + count = of_property_count_u32_elems(dev->of_node, "apple,owned-streams"); + if (count > 0) { + for (int i = 0; i < count; i++) { + ret = of_property_read_u32_index(dev->of_node, + "apple,owned-streams", + i, &sid); + if (ret || sid >= dart->num_streams) { + ret = ret ?: -ERANGE; + goto err_clk_disable; + } + set_bit(sid, dart->owned); + } + dart->has_owned = true; + } else if (count != -EINVAL) { + ret = count; + goto err_clk_disable; + } + + /* + * A hot-plug PCIe-C DART has no retained/locked translation context. + * A DART the bootloader leaves locked is one that hardware we do not drive + * still translates through (on T8152 Gen3 a display pipe scans out through + * the boot stream the whole time Linux is booting). Letting runtime PM + * suspend it drops the TTBRs underneath that traffic, and the engine then + * faults "TTBR invalid" forever. Hold the reference for the DART's lifetime. + */ if (tunneled) dart->locked = false; else - dart->locked = apple_dart_is_locked(dart); + dart->locked = !dart->static_topo && + (apple_dart_is_locked(dart) || + of_property_read_bool(dev->of_node, "apple,inherited-roots")); + if (dart->hw->gen3 && dart->locked && !dart->force_available) { + dev_info(dev, "locked DART: holding it available for its lifetime\n"); + dart->force_available = 1; + } /* Reserved table-page lifetime requires the matched three-level loader. */ dart->fw_handoff = apple_dart_fw_handoff_enabled(dart); /* Preserve our retained-root path when no reserved-table handoff exists. */ dart->fw_mirror = dart->locked && !dart->fw_handoff && (of_device_is_compatible(dev->of_node, "apple,t8140-dart") || of_device_is_compatible(dev->of_node, "apple,t8142-dart")); - if (!dart->locked) { + if (!dart->locked && !dart->static_topo) { ret = apple_dart_hw_reset(dart); if (ret) goto err_clk_disable; } + if (dart->static_topo) + dev_info(dev, "Gen3 DART static topology: no MMIO until first use\n"); if (dart->locked) apple_dart_drain_stale_exceptions(dart); - ret = request_irq(dart->irq, apple_dart_irq, IRQF_SHARED, "apple-dart fault handler", dart); if (ret) goto err_clk_disable; + dart->irq_requested = true; ret = iommu_device_sysfs_add(&dart->iommu, dev, NULL, "apple-dart.%s", dev_name(&pdev->dev)); @@ -3126,7 +3455,8 @@ static int apple_dart_probe(struct platform_device *pdev) if (ret) goto err_sysfs_remove; - pm_runtime_put(dev); + if (!dart->force_available) + pm_runtime_put(dev); dev_dbg( &pdev->dev, @@ -3138,7 +3468,8 @@ static int apple_dart_probe(struct platform_device *pdev) err_sysfs_remove: iommu_device_sysfs_remove(&dart->iommu); err_free_irq: - free_irq(dart->irq, dart); + if (dart->irq_requested) + free_irq(dart->irq, dart); err_clk_disable: pm_runtime_put(dev); clk_bulk_disable_unprepare(dart->num_clks, dart->clks); @@ -3253,13 +3584,17 @@ static void apple_dart_remove(struct platform_device *pdev) * A command issued then never completes. The next probe resets the * block after that clock is running again. */ - if (!dart->locked && !READ_ONCE(dart->commands_gated)) + if (!dart->locked && (!dart->static_topo || dart->hw_ready) && + !READ_ONCE(dart->commands_gated)) apple_dart_hw_reset(dart); - free_irq(dart->irq, dart); + if (dart->irq_requested) + free_irq(dart->irq, dart); iommu_device_unregister(&dart->iommu); iommu_device_sysfs_remove(&dart->iommu); + if (dart->force_available) + pm_runtime_put_noidle(dart->dev); clk_bulk_disable_unprepare(dart->num_clks, dart->clks); } @@ -3372,12 +3707,43 @@ static const struct apple_dart_hw apple_dart_hw_t8110 = { .ttbr_count = 1, }; +static const struct apple_dart_hw apple_dart_hw_t8152 = { + .type = DART_T8110, + .irq_handler = apple_dart_gen3_irq, + .invalidate_tlb = apple_dart_gen3_hw_invalidate_tlb, + .gen3 = true, + .fmt = APPLE_DART2, + .max_sid_count = 256, + + .enable_streams = DART_T8110_ENABLE_STREAMS, + .lock = DART_T8110_PROTECT, + .lock_bit = DART_T8110_PROTECT_TTBR_TCR, + + .error = DART_T8110_ERROR, + + .tcr = DART_T8110_TCR, + .tcr_enabled = DART_T8110_TCR_TRANSLATE_ENABLE, + .tcr_disabled = 0, + .tcr_bypass = DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART, + .tcr_4level = DART_T8110_TCR_FOUR_LEVEL, + + .ttbr = DART_T8110_TTBR, + .ttbr_valid = DART_T8110_TTBR_VALID, + .ttbr_addr_field_shift = DART_T8110_TTBR_ADDR_FIELD_SHIFT, + .ttbr_shift = DART_T8110_TTBR_SHIFT, + .ttbr_count = 1, +}; + static __maybe_unused int apple_dart_suspend(struct device *dev) { struct apple_dart *dart = dev_get_drvdata(dev); unsigned int sid, idx; dart->tunnel_state_restored = false; + /* A static-topology DART untouched so far has no state and must not be read. */ + if (dart->static_topo && !dart->hw_ready) + return 0; + if (READ_ONCE(dart->commands_gated)) return 0; /* The tunneled DART has no state to save when its domain stays on. */ @@ -3466,6 +3832,10 @@ static __maybe_unused int apple_dart_resume(struct device *dev) unsigned int sid, idx; int ret; + /* First resume of a static-topology DART: do everything probe skipped. */ + if (dart->static_topo && !dart->hw_ready) + return apple_dart_gen3_ensure_ready(dart); + /* PCIe-C restored these mappings before starting its downstream link. */ if (dart->tunnel_state_restored) { dart->tunnel_state_restored = false; @@ -3532,6 +3902,7 @@ static const struct of_device_id apple_dart_of_match[] = { { .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 }, { .compatible = "apple,t8103-usb4-dart", .data = &apple_dart_hw_t8103_usb4 }, { .compatible = "apple,t8110-dart", .data = &apple_dart_hw_t8110 }, + { .compatible = "apple,t8152-dart", .data = &apple_dart_hw_t8152 }, { .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 }, {}, }; diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index d3ef47fa840e49..815b298d3a430c 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -198,6 +198,12 @@ struct apple_nvme_hw { bool has_queue_count; bool has_separate_nvmmu; bool needs_ioq_registers; + /* T8152: LSQ_CTRL and IOQA accept writes but read back 0. */ + bool no_ioq_readback; + /* T8152: the queue setup registers live at different offsets. */ + bool t8152_reg_layout; + /* T8152: the ANS is a virtual PMGR device with no PS register, so there is no reset line. */ + bool no_reset_control; u32 max_queue_depth; unsigned long quirks; }; @@ -262,6 +268,27 @@ struct apple_nvme { struct blk_crypto_profile crypto_profile; }; +/* Map a legacy ANS register offset to the T8152 layout: admin queue +0x4000, IOQ registers +0x4000, + * LSQ_CTRL 0x24908 -> 0x2813c. */ +static u32 apple_nvme_reg(const struct apple_nvme *anv, u32 off) +{ + if (!anv->hw->t8152_reg_layout) + return off; + switch (off) { + case NVME_REG_AQA: + case NVME_REG_ASQ: + case NVME_REG_ACQ: + case APPLE_ANS_IOSQ_REGISTER: + case APPLE_ANS_IOCQ_REGISTER: + case APPLE_ANS_MAX_PEND_CMDS_CTRL: + return off + 0x4000; + case APPLE_ANS_LINEAR_SQ_CTRL: + return 0x2813c; + default: + return off; + } +} + /* Bit in recovery_flags: recovery_work owns the next controller stop. */ #define APPLE_NVME_RECOVERY_PENDING 0 @@ -1711,12 +1738,12 @@ static void apple_nvme_reset_work(struct work_struct *work) * since T6000. */ writel(APPLE_ANS_LINEAR_SQ_EN, - anv->mmio_nvme + APPLE_ANS_LINEAR_SQ_CTRL); + anv->mmio_nvme + apple_nvme_reg(anv, APPLE_ANS_LINEAR_SQ_CTRL)); if (anv->hw->needs_ioq_registers) { u32 lsq_ctrl = readl(anv->mmio_nvme + - APPLE_ANS_LINEAR_SQ_CTRL); + apple_nvme_reg(anv, APPLE_ANS_LINEAR_SQ_CTRL)); - if (!(lsq_ctrl & APPLE_ANS_LINEAR_SQ_EN)) { + if (!(lsq_ctrl & APPLE_ANS_LINEAR_SQ_EN) && !anv->hw->no_ioq_readback) { dev_err(anv->dev, "failed to enable linear submission queues: LSQ_CTRL=0x%08x\n", lsq_ctrl); @@ -1736,9 +1763,9 @@ static void apple_nvme_reset_work(struct work_struct *work) if (anv->hw->needs_ioq_registers) ioqa_depth--; ioqa = ioqa_depth | (ioqa_depth << 16); - writel(ioqa, anv->mmio_nvme + APPLE_ANS_MAX_PEND_CMDS_CTRL); - if (anv->hw->needs_ioq_registers && - readl(anv->mmio_nvme + APPLE_ANS_MAX_PEND_CMDS_CTRL) != ioqa) { + writel(ioqa, anv->mmio_nvme + apple_nvme_reg(anv, APPLE_ANS_MAX_PEND_CMDS_CTRL)); + if (anv->hw->needs_ioq_registers && !anv->hw->no_ioq_readback && + readl(anv->mmio_nvme + apple_nvme_reg(anv, APPLE_ANS_MAX_PEND_CMDS_CTRL)) != ioqa) { dev_err(anv->dev, "failed to program I/O queue aperture\n"); ret = -EIO; goto out; @@ -1752,11 +1779,11 @@ static void apple_nvme_reset_work(struct work_struct *work) /* Setup the admin queue */ aqa = APPLE_NVME_AQ_DEPTH - 1; aqa |= aqa << 16; - writel(aqa, anv->mmio_nvme + NVME_REG_AQA); + writel(aqa, anv->mmio_nvme + apple_nvme_reg(anv, NVME_REG_AQA)); apple_nvme_writeq(anv, anv->adminq.sq_dma_addr, - anv->mmio_nvme + NVME_REG_ASQ); + anv->mmio_nvme + apple_nvme_reg(anv, NVME_REG_ASQ)); apple_nvme_writeq(anv, anv->adminq.cq_dma_addr, - anv->mmio_nvme + NVME_REG_ACQ); + anv->mmio_nvme + apple_nvme_reg(anv, NVME_REG_ACQ)); if (anv->hw->has_lsq_nvmmu) { /* Setup NVMMU for both queues */ @@ -1824,9 +1851,9 @@ static void apple_nvme_reset_work(struct work_struct *work) * reports null I/O SQ and CQ addresses. */ apple_nvme_writeq(anv, anv->ioq.cq_dma_addr, - anv->mmio_nvme + APPLE_ANS_IOCQ_REGISTER); + anv->mmio_nvme + apple_nvme_reg(anv, APPLE_ANS_IOCQ_REGISTER)); apple_nvme_writeq(anv, anv->ioq.sq_dma_addr, - anv->mmio_nvme + APPLE_ANS_IOSQ_REGISTER); + anv->mmio_nvme + apple_nvme_reg(anv, APPLE_ANS_IOSQ_REGISTER)); } nr_io_queues = 1; if (anv->hw->has_queue_count) { @@ -2309,7 +2336,11 @@ static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev) goto out_detach_genpd; } - anv->reset = devm_reset_control_array_get_exclusive(anv->dev); + /* T8152's ANS is a virtual PMGR device with no PS register, so the DT has no reset to give. */ + if (anv->hw->no_reset_control) + anv->reset = NULL; + else + anv->reset = devm_reset_control_array_get_exclusive(anv->dev); if (IS_ERR(anv->reset)) { ret = dev_err_probe(dev, PTR_ERR(anv->reset), "Failed to get reset control"); @@ -2642,11 +2673,27 @@ static const struct apple_nvme_hw apple_nvme_t8140_hw = { .quirks = NVME_QUIRK_BROKEN_FUA, }; +/* T8152: NVMMU registers sit in the 0x60000 NVMe window (no separate region; the T8140 + * alias window does not exist and faults). */ +static const struct apple_nvme_hw apple_nvme_t8152_hw = { + /* T8152 register layout, linear submission queue, 64 queue entries. */ + .has_lsq_nvmmu = true, + .has_queue_count = true, + .has_separate_nvmmu = false, + .no_ioq_readback = true, + .t8152_reg_layout = true, + .no_reset_control = true, + .needs_ioq_registers = true, + .max_queue_depth = 64, + .quirks = NVME_QUIRK_BROKEN_FUA, +}; + static const struct of_device_id apple_nvme_of_match[] = { { .compatible = "apple,t8015-nvme-ans2", .data = &apple_nvme_t8015_hw }, { .compatible = "apple,t8103-nvme-ans2", .data = &apple_nvme_t8103_hw }, { .compatible = "apple,t8132-nvme-ans2", .data = &apple_nvme_t8132_hw }, { .compatible = "apple,t8140-nvme-ans2", .data = &apple_nvme_t8140_hw }, + { .compatible = "apple,t8152-nvme-ans2", .data = &apple_nvme_t8152_hw }, { .compatible = "apple,nvme-ans2", .data = &apple_nvme_t8103_hw }, {}, }; diff --git a/drivers/phy/apple/Kconfig b/drivers/phy/apple/Kconfig index 66c71acb005156..95b31a796e4cf3 100644 --- a/drivers/phy/apple/Kconfig +++ b/drivers/phy/apple/Kconfig @@ -40,3 +40,14 @@ config PHY_APPLE_DPTX such as the M2. This driver provides support for DisplayPort and is used on the Mac mini (M2 and M2 Pro, 2023). + +config PHY_APPLE_T8152_USB2 + tristate "Apple T8152 fixed-host USB2 PHY" + default ARCH_APPLE + depends on ARCH_APPLE || COMPILE_TEST + depends on PM + select GENERIC_PHY + select APPLE_TUNABLE + help + Enable the USB2-only PHY for fixed host ports on Apple T8152. + This does not configure the USB3 or USB4 lanes. diff --git a/drivers/phy/apple/Makefile b/drivers/phy/apple/Makefile index b9e7bf3e4ac170..bc33678c64707a 100644 --- a/drivers/phy/apple/Makefile +++ b/drivers/phy/apple/Makefile @@ -5,3 +5,6 @@ phy-apple-atc-y := atc.o obj-$(CONFIG_PHY_APPLE_DPTX) += phy-apple-dptx.o phy-apple-dptx-y += dptx.o + +obj-$(CONFIG_PHY_APPLE_T8152_USB2) += phy-apple-t8152-usb2.o +phy-apple-t8152-usb2-y := usb2-t8152.o diff --git a/drivers/phy/apple/usb2-t8152.c b/drivers/phy/apple/usb2-t8152.c new file mode 100644 index 00000000000000..6bb54dfe301030 --- /dev/null +++ b/drivers/phy/apple/usb2-t8152.c @@ -0,0 +1,231 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define USB2_CONTROL 0x00 +#define USB2_RESET 0x04 +#define USB2_VBUS 0x08 +#define USB2_VBUS_FORCE GENMASK(3, 0) +#define USB2_HOST_MODE GENMASK(14, 12) +#define USB2_CLOCK 0x1c +#define USB2_MODE GENMASK(2, 0) +#define USB2_RUN 2 +#define USB2_STOP 4 +#define USB2_RESET_ASSERT BIT(0) +#define USB2_PORT_RESET BIT(1) +#define USB2_APB_RESET_N BIT(2) +#define USB2_SIDDQ BIT(3) +#define USB2_REFCLK_GATE BIT(29) +#define USB2_PHYCLOCK_GATE BIT(30) +/* + * Bits that the host path sets. Their function is not documented; they are + * named by position. + */ +#define USB2_VBUS_HOST_BIT2 BIT(2) +#define USB2_VBUS_HOST_BIT3 BIT(3) +#define EUSB2_HOST_ENABLE (BIT(0) | BIT(3)) +/* + * Device-mode value of the misc-tune register from m1n1 src/usb.c + * usb_phy_bringup(), the role-agnostic USB2 device console setup. The word + * leaves the same refclk/APB gate bits clear that the host path clears below. + */ +#define USB2_DEVICE_MISCTUNE 0x008c0813 + +struct apple_usb2 { + void __iomem *usb2; + void __iomem *eusb2; + struct apple_tunable *dflt; + struct apple_tunable *host; + struct apple_tunable *dev; + enum phy_mode mode; +}; + +static void apple_usb2_update(void __iomem *reg, u32 clear, u32 set) +{ + writel((readl(reg) & ~clear) | set, reg); +} + +static int apple_usb2_power_on(struct phy *phy) +{ + struct apple_usb2 *usb = phy_get_drvdata(phy); + + apple_tunable_apply(usb->usb2, usb->dflt); + if (usb->mode == PHY_MODE_USB_DEVICE) { + if (!usb->dev) + return -EINVAL; + apple_tunable_apply(usb->usb2, usb->dev); + } else { + if (!usb->host) + return -EINVAL; + apple_tunable_apply(usb->usb2, usb->host); + } + if (usb->mode == PHY_MODE_USB_DEVICE) { + apple_usb2_update(usb->usb2 + USB2_VBUS, USB2_HOST_MODE, USB2_VBUS_FORCE); + } else { + apple_usb2_update(usb->usb2 + USB2_VBUS, 0, USB2_VBUS_HOST_BIT2); + apple_usb2_update(usb->usb2 + USB2_VBUS, 0, USB2_VBUS_HOST_BIT3); + } + msleep(10); + apple_usb2_update(usb->usb2 + USB2_RESET, USB2_SIDDQ, 0); + udelay(10); + apple_usb2_update(usb->usb2 + USB2_RESET, USB2_RESET_ASSERT, 0); + apple_usb2_update(usb->usb2 + USB2_RESET, USB2_PORT_RESET, 0); + apple_usb2_update(usb->eusb2, 0, EUSB2_HOST_ENABLE); + apple_usb2_update(usb->usb2 + USB2_RESET, 0, USB2_APB_RESET_N); + if (usb->mode == PHY_MODE_USB_DEVICE) + writel(USB2_DEVICE_MISCTUNE, usb->usb2 + USB2_CLOCK); + else { + apple_usb2_update(usb->usb2 + USB2_CLOCK, USB2_REFCLK_GATE, 0); + apple_usb2_update(usb->usb2 + USB2_CLOCK, USB2_PHYCLOCK_GATE, 0); + } + udelay(30); + msleep(5); + apple_usb2_update(usb->usb2 + USB2_CONTROL, USB2_MODE, USB2_RUN); + + return 0; +} + +static int apple_usb2_power_off(struct phy *phy) +{ + struct apple_usb2 *usb = phy_get_drvdata(phy); + + if (!(readl(usb->usb2 + USB2_RESET) & USB2_PORT_RESET)) { + apple_usb2_update(usb->usb2 + USB2_RESET, 0, USB2_PORT_RESET); + msleep(5); + } + apple_usb2_update(usb->usb2 + USB2_CONTROL, USB2_MODE, USB2_STOP); + apple_usb2_update(usb->usb2 + USB2_RESET, 0, USB2_SIDDQ); + apple_usb2_update(usb->usb2 + USB2_RESET, 0, USB2_RESET_ASSERT); + apple_usb2_update(usb->usb2 + USB2_CLOCK, 0, USB2_REFCLK_GATE); + apple_usb2_update(usb->usb2 + USB2_CLOCK, 0, USB2_PHYCLOCK_GATE); + + return 0; +} + +static int apple_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode) +{ + struct apple_usb2 *usb = phy_get_drvdata(phy); + + (void)submode; + if (mode != PHY_MODE_USB_HOST && mode != PHY_MODE_USB_DEVICE) + return -EINVAL; + if (mode == PHY_MODE_USB_DEVICE && !usb->dev) + return -EINVAL; + if (mode == PHY_MODE_USB_HOST && !usb->host) + return -EINVAL; + usb->mode = mode; + + return 0; +} + +static const struct phy_ops apple_usb2_ops = { + .power_on = apple_usb2_power_on, + .power_off = apple_usb2_power_off, + .set_mode = apple_usb2_set_mode, + .owner = THIS_MODULE, +}; + +static int apple_usb2_probe(struct platform_device *pdev) +{ + static const char * const pd_names[] = { "usb-aon", "usb" }; + const struct dev_pm_domain_attach_data pd_data = { + .pd_names = pd_names, + .num_pd_names = ARRAY_SIZE(pd_names), + }; + struct device *dev = &pdev->dev; + struct dev_pm_domain_list *domains; + struct phy_provider *provider; + struct resource *res; + struct apple_usb2 *usb; + struct phy *phy; + int ret; + + if (of_count_phandle_with_args(dev->of_node, "power-domains", + "#power-domain-cells") != ARRAY_SIZE(pd_names)) + return dev_err_probe(dev, -EINVAL, "Both USB2 power domains are required\n"); + + usb = devm_kzalloc(dev, sizeof(*usb), GFP_KERNEL); + if (!usb) + return -ENOMEM; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "usb2"); + usb->usb2 = devm_ioremap_resource(dev, res); + if (IS_ERR(usb->usb2)) + return PTR_ERR(usb->usb2); + if (resource_size(res) < USB2_CLOCK + sizeof(u32)) + return -EINVAL; + + usb->dflt = devm_apple_tunable_parse(dev, dev->of_node, + "apple,tunable-usb2phy-dflt", res); + if (IS_ERR(usb->dflt)) + return dev_err_probe(dev, PTR_ERR(usb->dflt), "Invalid default tunables\n"); + usb->host = devm_apple_tunable_parse(dev, dev->of_node, + "apple,tunable-usb2phy-host", res); + if (IS_ERR(usb->host)) { + if (PTR_ERR(usb->host) != -ENOENT) + return dev_err_probe(dev, PTR_ERR(usb->host), "Invalid host tunables\n"); + usb->host = NULL; + } + usb->dev = devm_apple_tunable_parse(dev, dev->of_node, + "apple,tunable-usb2phy-dev", res); + if (IS_ERR(usb->dev)) { + if (PTR_ERR(usb->dev) != -ENOENT) + return dev_err_probe(dev, PTR_ERR(usb->dev), "Invalid device tunables\n"); + usb->dev = NULL; + } + if (!usb->host && !usb->dev) + return dev_err_probe(dev, -EINVAL, "No USB2 role calibration\n"); + /* Default to the role whose calibration the board actually supplies. */ + usb->mode = usb->host ? PHY_MODE_USB_HOST : PHY_MODE_USB_DEVICE; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eusb2"); + usb->eusb2 = devm_ioremap_resource(dev, res); + if (IS_ERR(usb->eusb2)) + return PTR_ERR(usb->eusb2); + if (resource_size(res) < sizeof(u32)) + return -EINVAL; + + ret = devm_pm_domain_attach_list(dev, &pd_data, &domains); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to attach USB2 power domains\n"); + if (ret != ARRAY_SIZE(pd_names)) + return -ENODEV; + + ret = devm_pm_runtime_enable(dev); + if (ret) + return ret; + + phy = devm_phy_create(dev, NULL, &apple_usb2_ops); + if (IS_ERR(phy)) + return PTR_ERR(phy); + phy_set_drvdata(phy, usb); + + provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); + return PTR_ERR_OR_ZERO(provider); +} + +static const struct of_device_id apple_usb2_match[] = { + { .compatible = "apple,t8152-usb2-phy" }, + { } +}; +MODULE_DEVICE_TABLE(of, apple_usb2_match); + +static struct platform_driver apple_usb2_driver = { + .probe = apple_usb2_probe, + .driver = { + .name = "apple-t8152-usb2-phy", + .of_match_table = apple_usb2_match, + }, +}; +module_platform_driver(apple_usb2_driver); + +MODULE_DESCRIPTION("Apple T8152 USB2 PHY"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/pinctrl/pinctrl-apple-gpio.c b/drivers/pinctrl/pinctrl-apple-gpio.c index eee7f5014ecc18..620d0e57bb2ccb 100644 --- a/drivers/pinctrl/pinctrl-apple-gpio.c +++ b/drivers/pinctrl/pinctrl-apple-gpio.c @@ -597,6 +597,7 @@ static int apple_gpio_pinctrl_probe(struct platform_device *pdev) static const struct of_device_id apple_gpio_pinctrl_of_match[] = { { .compatible = "apple,t8140-pinctrl", .data = &apple_gpio_t8140_hw }, + { .compatible = "apple,t8152-pinctrl", .data = &apple_gpio_t8140_hw }, { .compatible = "apple,t8103-pinctrl", }, { .compatible = "apple,pinctrl", }, { } diff --git a/drivers/pmdomain/apple/Kconfig b/drivers/pmdomain/apple/Kconfig index 5f0a9d95c196c2..2e85b6d5031abe 100644 --- a/drivers/pmdomain/apple/Kconfig +++ b/drivers/pmdomain/apple/Kconfig @@ -25,6 +25,18 @@ config APPLE_PMGR_PWRSTATE_KUNIT_TEST driver's power callbacks against an emulated power state register and need no Apple hardware. +config APPLE_PMGR_PSD + bool "Apple T8152 PSD service slot power domains" + default ARCH_APPLE + depends on OF + depends on PM + select PM_GENERIC_DOMAINS + help + The M6 (T8152) power state director gates the ATC USB blocks behind a + per-port service slot request on top of the PMGR power states. This + driver exposes each slot as a power domain whose parents are the PMGR + domains, so consumers (the USB DARTs and controllers) power both in order. + config APPLE_PMP_REPORT bool "Apple PMP report control" depends on OF diff --git a/drivers/pmdomain/apple/Makefile b/drivers/pmdomain/apple/Makefile index ff7f9192b5e39f..c9f1e412f6ab93 100644 --- a/drivers/pmdomain/apple/Makefile +++ b/drivers/pmdomain/apple/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_APPLE_PMGR_PWRSTATE) += pmgr-pwrstate.o +obj-$(CONFIG_APPLE_PMGR_PSD) += pmgr-psd.o obj-$(CONFIG_APPLE_PMGR_PWRSTATE) += gfx-tvm.o obj-$(CONFIG_APPLE_PMP_REPORT) += pmp-report.o obj-$(CONFIG_APPLE_PMP_REPORT_KUNIT_TEST) += pmp-report-test.o diff --git a/drivers/pmdomain/apple/pmgr-psd.c b/drivers/pmdomain/apple/pmgr-psd.c new file mode 100644 index 00000000000000..9be3506d9ed5a8 --- /dev/null +++ b/drivers/pmdomain/apple/pmgr-psd.c @@ -0,0 +1,266 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple T8152 PSD (power state director) service slot power domains + * + * On the M6 the USB blocks are not usable (even their DART registers hang the bus) + * until two things have happened: the PMGR power states of the block are active, and the + * PSD service slot for the port has been requested to run (state 0) and has + * acknowledged. This driver models the second step as a genpd whose parents are the + * PMGR domains: genpd powers the parents first, then this domain's power_on writes the + * slot request and waits for the ack. + * + * Per-slot window (reg, 0x100 bytes at base + (slot << 8)): + * 0x80 status bits 2:0 = current PSD state + * 0x90 request (write a PSD state) + * 0xa0 diag + * 0xbc valid bit 0 = status is valid + * PSD states: this driver accepts states 0 (running) to 6 (off). + * + * Ack: the valid bit 0 is set and (status & 7) equals the requested state. + * + * Slot groups: a block that needs several slots (the GPU uses several) must bring them up + * together, in a fixed order, with possibly different active states. A node with compatible + * "apple,t8152-psd-group" lists one reg window per slot and apple,psd-states with each slot's + * active state; power_on requests the slots in reg order, power_off requests state 6 in reverse. + * + * Reading STATUS clears the acknowledge, so a request polls VALID first and reads + * STATUS only once VALID is set. + * + * Lifetime: this domain sleeps in power_on (usleep_range polling), so it is not IRQ-safe. + * The platform bus powers a consumer's single power domain on at attach time, before its + * probe runs, which issues the request in process context. A consumer that marks its + * runtime PM irq_safe (the Apple DART does) keeps this domain powered: genpd never powers off + * a sleepable domain holding an irq-safe device and skips the (atomic-context) power_on on + * its runtime resume (drivers/pmdomain/core.c irq_safe_dev_in_sleep_domain users). Other + * consumers may let it power off to state 6. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define APPLE_PSD_STATUS 0x80 +#define APPLE_PSD_REQUEST 0x90 +#define APPLE_PSD_DIAG 0xa0 +#define APPLE_PSD_VALID 0xbc +#define APPLE_PSD_STATE GENMASK(2, 0) +#define APPLE_PSD_VALID_BIT BIT(0) + +#define APPLE_PSD_STATE_RUNNING 0 +#define APPLE_PSD_STATE_OFF 6 + +#define APPLE_PSD_DEFAULT_TIMEOUT_MS 1000 /* default ack timeout, apple,psd-timeout-ms overrides it */ +#define APPLE_PSD_MAX_SLOTS 3 + +struct apple_psd { + struct device *dev; + void __iomem *regs[APPLE_PSD_MAX_SLOTS]; + u32 active[APPLE_PSD_MAX_SLOTS]; + unsigned int nslots; + struct generic_pm_domain genpd; + unsigned int timeout_ms; +}; + +#define to_apple_psd(d) container_of(d, struct apple_psd, genpd) + +static int apple_psd_request(struct apple_psd *psd, unsigned int slot, u32 state) +{ + void __iomem *regs = psd->regs[slot]; + unsigned long timeout = jiffies + msecs_to_jiffies(psd->timeout_ms); + u32 valid = 0, status = 0; + + writel(state, regs + APPLE_PSD_REQUEST); + + for (;;) { + valid = readl(regs + APPLE_PSD_VALID); + if (valid & APPLE_PSD_VALID_BIT) { + /* Reading STATUS clears the acknowledge, so only after VALID. */ + status = readl(regs + APPLE_PSD_STATUS); + if (FIELD_GET(APPLE_PSD_STATE, status) == state) + return 0; + } + if (time_after(jiffies, timeout)) + break; + usleep_range(50, 200); + } + + dev_err(psd->dev, + "PSD slot %u request %u not acknowledged: valid %#x status %#x diag %#x\n", slot, + state, valid, status, readl(regs + APPLE_PSD_DIAG)); + return -ETIMEDOUT; +} + +static int apple_psd_power_on(struct generic_pm_domain *genpd) +{ + struct apple_psd *psd = to_apple_psd(genpd); + int i, ret; + + for (i = 0; i < psd->nslots; i++) { + ret = apple_psd_request(psd, i, psd->active[i]); + if (ret) + goto undo; + } + return 0; + +undo: + /* + * Best effort: put the slots back to idle, newest first. The slot whose request timed + * out is included, because its acknowledge was not seen but it may still start. + */ + for (; i >= 0; i--) + apple_psd_request(psd, i, APPLE_PSD_STATE_OFF); + return ret; +} + +static int apple_psd_power_off(struct generic_pm_domain *genpd) +{ + struct apple_psd *psd = to_apple_psd(genpd); + int i, ret, first = 0; + + for (i = psd->nslots - 1; i >= 0; i--) { + ret = apple_psd_request(psd, i, APPLE_PSD_STATE_OFF); + if (ret && !first) + first = ret; + } + if (!first) + return 0; + + /* + * genpd keeps the domain marked on after a failed power-off, so the next resume does not + * call power_on. Put every slot back to its active state, best effort: the slots that + * did stop, and the one whose off request timed out and may still finish stopping. + */ + for (i = 0; i < psd->nslots; i++) + apple_psd_request(psd, i, psd->active[i]); + return first; +} + +static int apple_psd_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *node = dev->of_node; + struct apple_psd *psd; + const char *name; + u32 status, valid; + unsigned int i; + int ret; + + psd = devm_kzalloc(dev, sizeof(*psd), GFP_KERNEL); + if (!psd) + return -ENOMEM; + psd->dev = dev; + psd->timeout_ms = APPLE_PSD_DEFAULT_TIMEOUT_MS; + of_property_read_u32(node, "apple,psd-timeout-ms", &psd->timeout_ms); + + for (i = 0; i < APPLE_PSD_MAX_SLOTS; i++) { + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, i); + + if (!res) + break; + psd->regs[i] = devm_ioremap_resource(dev, res); + if (IS_ERR(psd->regs[i])) + return PTR_ERR(psd->regs[i]); + psd->nslots++; + } + if (!psd->nslots) + return dev_err_probe(dev, -EINVAL, "no register window\n"); + + /* Per-slot active state, in reg order; default 0 (running). */ + if (of_property_present(node, "apple,psd-states")) { + ret = of_property_read_u32_array(node, "apple,psd-states", psd->active, + psd->nslots); + if (ret) + return dev_err_probe(dev, ret, "apple,psd-states needs one cell per reg\n"); + for (i = 0; i < psd->nslots; i++) + if (psd->active[i] >= APPLE_PSD_STATE_OFF) + return dev_err_probe(dev, -EINVAL, "bad PSD active state %u\n", + psd->active[i]); + } + + ret = of_property_read_string(node, "label", &name); + if (ret) + name = node->name; + psd->genpd.name = name; + psd->genpd.power_on = apple_psd_power_on; + psd->genpd.power_off = apple_psd_power_off; + + /* + * Always start OFF: an inherited status of state 0 without the valid bit is not an + * acknowledged P0, and the parents' state is not known here. The first consumer's + * power_on then issues the request (idempotent if the slot already runs P0). + * A status read is safe (proven on hardware); no write happens at probe. + */ + status = readl(psd->regs[0] + APPLE_PSD_STATUS); + valid = readl(psd->regs[0] + APPLE_PSD_VALID); + + ret = pm_genpd_init(&psd->genpd, NULL, true); + if (ret) + return dev_err_probe(dev, ret, "pm_genpd_init failed\n"); + + ret = of_genpd_add_provider_simple(node, &psd->genpd); + if (ret) { + dev_err(dev, "of_genpd_add_provider_simple failed: %d\n", ret); + goto err_remove; + } + + /* The PMGR parents are brought up before power_on runs. */ + for (i = 0; ; i++) { + struct of_phandle_args parent, child; + + ret = of_parse_phandle_with_args(node, "power-domains", "#power-domain-cells", i, + &parent); + if (ret == -ENOENT) + break; /* no (more) parents */ + if (ret) + goto err_unlink; + child.np = node; + child.args_count = 0; + ret = of_genpd_add_subdomain(&parent, &child); + if (ret < 0 && ret != -EPROBE_DEFER) + dev_err(dev, "failed to add parent domain %pOFn: %d\n", parent.np, ret); + of_node_put(parent.np); + if (ret < 0) + goto err_unlink; + } + ret = 0; + + /* Parent domains are handled through the genpd hierarchy, not as a device. */ + pm_genpd_remove_device(dev); + + dev_info(dev, "PSD slot domain %s, %u slot(s) (status %#x valid %#x at probe, starting off)\n", + name, psd->nslots, status, valid); + return 0; + +err_unlink: + /* pm_genpd_remove() drops any parent links added so far. */ + of_genpd_del_provider(node); +err_remove: + pm_genpd_remove(&psd->genpd); + return ret; +} + +static const struct of_device_id apple_psd_of_match[] = { + { .compatible = "apple,t8152-psd" }, + { .compatible = "apple,t8152-psd-group" }, + { } +}; +MODULE_DEVICE_TABLE(of, apple_psd_of_match); + +static struct platform_driver apple_psd_driver = { + .probe = apple_psd_probe, + .driver = { + .name = "apple-pmgr-psd", + .of_match_table = apple_psd_of_match, + .suppress_bind_attrs = true, + }, +}; +builtin_platform_driver(apple_psd_driver); + +MODULE_DESCRIPTION("Apple T8152 PSD service slot power domains"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/reset/reset-apple-cio.c b/drivers/reset/reset-apple-cio.c index af299473a6c876..ea77c3d8b6438f 100644 --- a/drivers/reset/reset-apple-cio.c +++ b/drivers/reset/reset-apple-cio.c @@ -40,6 +40,14 @@ * On t8142, RegMap 0x62 is a separate MMIO region at 0x380780000. * The t8132 layout has not been decoded. * + * On t8152 (M6) the request and done registers are separate words in the PMGR + * block at 0x300780000: BIT(cluster) is written to the request register at + * +0x68, BIT(cluster) is then polled in the done register at +0x6c (the vendor + * driver allows 10 s), and the done bit is cleared by writing it back. There + * is one cluster per port (the pmgr-child cio-cluster property: devices + * 320..323, CIOn_RECONFIG-V, clusters 0..3), and the node's reg is the offset + * of the request register in the PMGR syscon (0x80068). + * * Reference firmware polls the busy bits, stores the request, then polls * again, with a 192 ms budget. It discards the first poll's result. The * t8142 implementation below instead refuses to issue a request while busy. @@ -62,6 +70,12 @@ #define T8103_CIO_CTRL_INIT_BUSY BIT(1) #define T8103_CIO_CTRL_INIT_DONE BIT(2) +/* The node's reg is the request register; the done register follows it. */ +#define T8152_CIO_REQ 0x0 +#define T8152_CIO_DONE 0x4 +/* The vendor driver waits up to 10 s for a cluster to come online. */ +#define T8152_CIO_TIMEOUT_US 10000000 + #define T6000_CIO_CTRL_INIT_REQ(port) BIT(port) #define T6000_CIO_CTRL_INIT_BUSY(port) BIT(16 + (port)) @@ -197,6 +211,34 @@ static const struct apple_cio_reset_variant apple_t8142_cio_reset = { .deassert = t8142_cio_deassert, }; +static int t8152_cio_deassert(struct apple_cio_reset *priv, unsigned long id) +{ + u32 val; + int ret; + + guard(mutex)(&priv->lock); + + /* A request for this cluster may still be pending; advisory, as on the other SoCs. */ + ret = regmap_read_poll_timeout(priv->regmap, priv->offset + T8152_CIO_REQ, val, + !(val & BIT(id)), APPLE_CIO_RESET_POLL_US, + APPLE_CIO_RESET_TIMEOUT_US); + if (ret) + dev_warn(priv->dev, "CIO %lu still busy before reconfigure request\n", id); + + ret = regmap_write(priv->regmap, priv->offset + T8152_CIO_REQ, BIT(id)); + if (ret) + return ret; + + ret = regmap_read_poll_timeout(priv->regmap, priv->offset + T8152_CIO_DONE, val, + val & BIT(id), APPLE_CIO_RESET_POLL_US, + T8152_CIO_TIMEOUT_US); + if (ret) + return ret; + + /* The done bit is write-one-to-clear. */ + return regmap_write(priv->regmap, priv->offset + T8152_CIO_DONE, BIT(id)); +} + static const struct apple_cio_reset_variant apple_t8103_cio_reset = { .nr_resets = 2, .deassert = t8103_cio_deassert, @@ -208,6 +250,12 @@ static const struct apple_cio_reset_variant apple_t6000_cio_reset = { .deassert = t6000_cio_deassert, }; +static const struct apple_cio_reset_variant apple_t8152_cio_reset = { + .nr_resets = 4, + .pmgr_child = true, + .deassert = t8152_cio_deassert, +}; + static int apple_cio_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id) { struct apple_cio_reset *priv = @@ -293,6 +341,10 @@ static const struct of_device_id apple_cio_reset_match[] = { .compatible = "apple,t6030-cio-reset", .data = &apple_t6000_cio_reset, }, + { + .compatible = "apple,t8152-cio-reset", + .data = &apple_t8152_cio_reset, + }, {}, }; MODULE_DEVICE_TABLE(of, apple_cio_reset_match); diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 6641e1d5013622..689b5462e3f82c 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -2309,6 +2309,25 @@ const HW_CFG_T8140: AopHwConfig = AopHwConfig { required_endpoints: &[0x20, 0x21, 0x22, 0x23, 0x2b], }; +/// T8152 (Mac mini): the firmware is preloaded by the bootloader and the setup +/// mailbox belongs to the secure world, so there is no setup port: the CPU is +/// started through the wrapper and RTKit boots on the primary mailbox. It +/// advertises three AFK endpoints, 0x20 SPUApp, 0x21 wakehint and 0x22 +/// aop-audio, all announced with EPIC sub-header version 2. The firmware only +/// brings its AFK endpoints up once all of them are started, which start_afk +/// does back to back. Calls with the version 2 header on the aop-audio +/// endpoint were all answered, with the tag echoed at the version 2 offset. +const HW_CFG_T8152: AopHwConfig = AopHwConfig { + ec0p: 0, + aopt: 0, + alig: 0, + patch_bootargs: false, + epic_v4: false, + setup_port: false, + quiesce_on_unbind: true, + required_endpoints: &[0x20, 0x21, 0x22], +}; + kernel::of_device_table!( OF_TABLE, MODULE_OF_TABLE, @@ -2319,6 +2338,7 @@ kernel::of_device_table!( (of::DeviceId::new(c_str!("apple,t6000-aop")), &HW_CFG_T6000), (of::DeviceId::new(c_str!("apple,t6020-aop")), &HW_CFG_T6020), (of::DeviceId::new(c_str!("apple,t8140-aop")), &HW_CFG_T8140), + (of::DeviceId::new(c_str!("apple,t8152-aop")), &HW_CFG_T8152), ] ); diff --git a/drivers/soc/apple/mailbox.c b/drivers/soc/apple/mailbox.c index 5be648a40fe03c..f6a15157cde955 100644 --- a/drivers/soc/apple/mailbox.c +++ b/drivers/soc/apple/mailbox.c @@ -117,6 +117,8 @@ struct apple_mbox_hw { unsigned int reg_offset; bool is_ascwrap_v6; bool ap_initializes_mailboxes; + /* The status word counts RX occupancy and free TX slots; a message is one paired access. */ + bool counted_fifo; unsigned int control_full; unsigned int control_empty; @@ -266,6 +268,70 @@ int apple_mbox_ascwrap_v6_stop_cpu(struct apple_mbox *mbox) } EXPORT_SYMBOL_GPL(apple_mbox_ascwrap_v6_stop_cpu); +static bool apple_mbox_tx_full(struct apple_mbox *mbox, u32 status) +{ + if (mbox->hw->counted_fifo) + return !(status & mbox->hw->control_full); + return status & mbox->hw->control_full; +} + +static bool apple_mbox_rx_empty(struct apple_mbox *mbox, u32 status) +{ + if (mbox->hw->counted_fifo) + return !(status & mbox->hw->control_empty); + return status & mbox->hw->control_empty; +} + +static void apple_mbox_write_msg(struct apple_mbox *mbox, const struct apple_mbox_msg msg) +{ + u64 high = FIELD_PREP(APPLE_MBOX_MSG1_MSG, msg.msg1); + +#ifdef CONFIG_ARM64 + if (mbox->hw->counted_fifo) { + u64 low = (__force u64)cpu_to_le64(msg.msg0); + + high = (__force u64)cpu_to_le64(high); + /* Order the message words against the earlier writes to the shared buffers. */ + wmb(); + asm volatile("stp %0, %1, [%2]" + : : "r" (low), "r" (high), + "r" (mbox->regs + mbox->hw->a2i_send0) + : "memory"); + return; + } +#endif + writeq_relaxed(msg.msg0, mbox->regs + mbox->hw->a2i_send0); + writeq_relaxed(high, mbox->regs + mbox->hw->a2i_send1); +} + +static struct apple_mbox_msg apple_mbox_read_msg(struct apple_mbox *mbox) +{ + struct apple_mbox_msg msg; + bool paired = false; + u64 low, high; + +#ifdef CONFIG_ARM64 + if (mbox->hw->counted_fifo) { + asm volatile("ldp %0, %1, [%2]" + : "=&r" (low), "=&r" (high) + : "r" (mbox->regs + mbox->hw->i2a_recv0) + : "memory"); + /* The message words must be read before the buffers they describe. */ + rmb(); + low = le64_to_cpu((__force __le64)low); + high = le64_to_cpu((__force __le64)high); + paired = true; + } +#endif + if (!paired) { + low = readq_relaxed(mbox->regs + mbox->hw->i2a_recv0); + high = readq_relaxed(mbox->regs + mbox->hw->i2a_recv1); + } + msg.msg0 = low; + msg.msg1 = FIELD_GET(APPLE_MBOX_MSG1_MSG, high); + return msg; +} + int apple_mbox_send(struct apple_mbox *mbox, const struct apple_mbox_msg msg, bool atomic) { @@ -281,11 +347,11 @@ int apple_mbox_send(struct apple_mbox *mbox, const struct apple_mbox_msg msg, } mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->a2i_control); - while (mbox_ctrl & mbox->hw->control_full) { + while (apple_mbox_tx_full(mbox, mbox_ctrl)) { if (atomic || mbox->irq_send_empty < 0) { ret = readl_poll_timeout_atomic( mbox->regs + mbox->hw->a2i_control, mbox_ctrl, - !(mbox_ctrl & mbox->hw->control_full), 100, + !apple_mbox_tx_full(mbox, mbox_ctrl), 100, APPLE_MBOX_TX_TIMEOUT * 1000); if (ret) { @@ -337,9 +403,7 @@ int apple_mbox_send(struct apple_mbox *mbox, const struct apple_mbox_msg msg, mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->a2i_control); } - writeq_relaxed(msg.msg0, mbox->regs + mbox->hw->a2i_send0); - writeq_relaxed(FIELD_PREP(APPLE_MBOX_MSG1_MSG, msg.msg1), - mbox->regs + mbox->hw->a2i_send1); + apple_mbox_write_msg(mbox, msg); spin_unlock_irqrestore(&mbox->tx_lock, flags); @@ -380,11 +444,8 @@ static int apple_mbox_poll_locked(struct apple_mbox *mbox) return 0; mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->i2a_control); - while (!(mbox_ctrl & mbox->hw->control_empty)) { - msg.msg0 = readq_relaxed(mbox->regs + mbox->hw->i2a_recv0); - msg.msg1 = FIELD_GET( - APPLE_MBOX_MSG1_MSG, - readq_relaxed(mbox->regs + mbox->hw->i2a_recv1)); + while (!apple_mbox_rx_empty(mbox, mbox_ctrl)) { + msg = apple_mbox_read_msg(mbox); mbox->rx(mbox, msg, mbox->cookie); ret++; @@ -646,6 +707,8 @@ static int apple_mbox_probe(struct platform_device *pdev) mbox->hw = of_device_get_match_data(dev); if (!mbox->hw) return -EINVAL; + if (mbox->hw->counted_fifo && !IS_ENABLED(CONFIG_ARM64)) + return -EOPNOTSUPP; if (mbox->hw->is_ascwrap_v6) { /* @@ -795,6 +858,28 @@ static const struct apple_mbox_hw apple_mbox_t8015_hw = { .has_irq_controls = false, }; +/* + * T8152 ACIO (USB4) co-processor mailbox: RX at +0, TX at +0x10 and the status + * word at +0x20 of the 0x80-byte window. The status word holds the RX occupancy + * (bits 3:0) and the free TX slots (bits 11:8). The interrupts are not + * controlled per mailbox. + */ +static const struct apple_mbox_hw apple_mbox_t8152_acio_hw = { + .counted_fifo = true, + .control_full = GENMASK(11, 8), + .control_empty = GENMASK(3, 0), + + .a2i_control = 0x20, + .a2i_send0 = 0x10, + .a2i_send1 = 0x18, + + .i2a_control = 0x20, + .i2a_recv0 = 0x00, + .i2a_recv1 = 0x08, + + .has_irq_controls = false, +}; + static const struct apple_mbox_hw apple_mbox_asc_hw = { .control_full = APPLE_ASC_MBOX_CONTROL_FULL, .control_empty = APPLE_ASC_MBOX_CONTROL_EMPTY, @@ -890,6 +975,8 @@ static const struct of_device_id apple_mbox_of_match[] = { * kernels without M3 GPU support leave it unbound. */ { .compatible = "apple,t6030-agx-asc-mailbox", .data = &apple_mbox_asc_hw }, + { .compatible = "apple,t8152-acio-mailbox", .data = &apple_mbox_t8152_acio_hw }, + { .compatible = "apple,t8152-ascwrap-v8-mailbox", .data = &apple_mbox_t8152_acio_hw }, { .compatible = "apple,asc-mailbox-v4", .data = &apple_mbox_asc_hw }, { .compatible = "apple,t8015-asc-mailbox", .data = &apple_mbox_t8015_hw }, { .compatible = "apple,m3-mailbox-v2", .data = &apple_mbox_m3_hw }, diff --git a/drivers/soc/apple/sart.c b/drivers/soc/apple/sart.c index b0b208c92741dc..e8777835d738a3 100644 --- a/drivers/soc/apple/sart.c +++ b/drivers/soc/apple/sart.c @@ -62,6 +62,18 @@ #define APPLE_SART3_FLAGS_ALLOW 0xff +/* SARTv4 (CoastGuard, T8152) registers; layout from m1n1 sart4 */ +#define APPLE_SART4_CONFIG(idx) (0x00 + 4 * (idx)) + +#define APPLE_SART4_PADDR(idx) (0x60 + 4 * (idx)) +#define APPLE_SART4_PADDR_SHIFT 12 + +#define APPLE_SART4_SIZE(idx) (0xc0 + 4 * (idx)) +#define APPLE_SART4_SIZE_SHIFT 12 +#define APPLE_SART4_SIZE_MAX GENMASK(29, 0) + +#define APPLE_SART4_FLAGS_ALLOW 0xff + struct apple_sart_ops { void (*get_entry)(struct apple_sart *sart, int index, u8 *flags, phys_addr_t *paddr, size_t *size); @@ -178,6 +190,35 @@ static struct apple_sart_ops sart_ops_v3 = { .size_max = APPLE_SART3_SIZE_MAX, }; + +static void sart4_get_entry(struct apple_sart *sart, int index, u8 *flags, + phys_addr_t *paddr, size_t *size) +{ + phys_addr_t paddr_ = readl(sart->regs + APPLE_SART4_PADDR(index)); + size_t size_ = readl(sart->regs + APPLE_SART4_SIZE(index)); + + *flags = readl(sart->regs + APPLE_SART4_CONFIG(index)); + *size = size_ << APPLE_SART4_SIZE_SHIFT; + *paddr = paddr_ << APPLE_SART4_PADDR_SHIFT; +} + +static void sart4_set_entry(struct apple_sart *sart, int index, u8 flags, + phys_addr_t paddr_shifted, size_t size_shifted) +{ + writel(paddr_shifted, sart->regs + APPLE_SART4_PADDR(index)); + writel(size_shifted, sart->regs + APPLE_SART4_SIZE(index)); + writel(flags, sart->regs + APPLE_SART4_CONFIG(index)); +} + +static struct apple_sart_ops sart_ops_v4 = { + .get_entry = sart4_get_entry, + .set_entry = sart4_set_entry, + .flags_allow = APPLE_SART4_FLAGS_ALLOW, + .size_shift = APPLE_SART4_SIZE_SHIFT, + .paddr_shift = APPLE_SART4_PADDR_SHIFT, + .size_max = APPLE_SART4_SIZE_MAX, +}; + static int apple_sart_probe(struct platform_device *pdev) { int i; @@ -376,6 +417,10 @@ static const struct of_device_id apple_sart_of_match[] = { .compatible = "apple,t6000-sart", .data = &sart_ops_v3, }, + { + .compatible = "apple,t8152-sart", + .data = &sart_ops_v4, + }, { .compatible = "apple,t8103-sart", .data = &sart_ops_v2, diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index f2e6718c3dfc4e..12ee0f33976afe 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -55,6 +55,8 @@ #define USB_PRODUCT_TUSB8041_USB3 0x8140 #define USB_PRODUCT_TUSB8041_USB2 0x8142 #define USB_VENDOR_MICROCHIP 0x0424 +#define USB_VENDOR_APPLE 0x05ac +#define USB_PRODUCT_APPLE_HUB_800F 0x800f #define USB_PRODUCT_USB4913 0x4913 #define USB_PRODUCT_USB4914 0x4914 #define USB_PRODUCT_USB4915 0x4915 @@ -6251,6 +6253,11 @@ static const struct usb_device_id hub_id_table[] = { | USB_DEVICE_ID_MATCH_PRODUCT, .idVendor = USB_VENDOR_VIA_LABS, .idProduct = USB_PRODUCT_VL122, + .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, + { .match_flags = USB_DEVICE_ID_MATCH_VENDOR + | USB_DEVICE_ID_MATCH_PRODUCT, + .idVendor = USB_VENDOR_APPLE, + .idProduct = USB_PRODUCT_APPLE_HUB_800F, .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT, diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig index c862e55ae3a373..4f5be5ecee5626 100644 --- a/drivers/usb/dwc3/Kconfig +++ b/drivers/usb/dwc3/Kconfig @@ -227,6 +227,7 @@ config USB_DWC3_APPLE depends on OF && ARCH_APPLE default USB_DWC3 select USB_ROLE_SWITCH + select APPLE_TUNABLE help Support Apple Silicon SoCs with DesignWare Core USB3 IP. The DesignWare Core USB3 IP has to be used in dual-role diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 464e5d9e691fe3..e10436a84e742e 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -13,8 +13,10 @@ #include #include #include +#include #include #include +#include #include #include "dwc3-apple-state.h" @@ -86,6 +88,13 @@ * dwc3_apple_init(). In host mode xHCI then keeps running in suspend-to-idle when a USB device * may wake the system, as a halted xHC could not signal the wakeup. * + * The T8152 USB2-only profile starts a fixed host, without a physical hub, or — + * with dr_mode = "peripheral" on the debug-cable port — a fixed peripheral. A + * fixed peripheral programs the pipe handler with the m1n1 device-console + * sequence (mux to the dummy backend, DWC3 reset released, device non-selected + * override word) and applies only the default calibration lists plus the USB2 + * PHY device calibration; the host lists stay unused. + * * enum dwc3_apple_state is defined in dwc3-apple-state.h. */ @@ -100,7 +109,11 @@ * sets host mode * @usb3_phy: USB3 PHY, configured after the core is brought up * @role_sw: USB role switch - * @fixed_hub: A fixed hub sits on the controller's USB2 port, the controller stays in host mode + * @fixed_hub: A physical hub child node sits on the controller's USB2 port + * @t8152_usb2: T8152 fixed-role calibration and USB2-only PHY profile + * @fixed_peripheral: T8152 started as a fixed peripheral (dr_mode = "peripheral") + * @tunable_regs: Non-overlapping calibration windows, with the core mapped by dwc3 + * @tunables: Native host calibration lists in application order * @lock: Mutex for synchronizing access * @state: Current state of the controller, see enum dwc3_apple_state and the comment above * @core_active: Core still owns initialized PHYs @@ -121,6 +134,10 @@ struct dwc3_apple { struct phy *usb3_phy; struct usb_role_switch *role_sw; bool fixed_hub; + bool t8152_usb2; + bool fixed_peripheral; + void __iomem *tunable_regs[6]; + struct apple_tunable *tunables[11]; struct mutex lock; @@ -193,6 +210,121 @@ static void dwc3_apple_setup_cio(struct dwc3_apple *appledwc) APPLE_DWC3_CIO_PM_ENTRY_TIMER_VALUE)); } +static const struct { + const char *name; + u8 resource; + u32 offset; + bool optional; + bool host_only; +} t8152_host_tunables[] = { + { "apple,tunable-bulk-fabric-default", 4, 0 }, + { "apple,tunable-pipe-handler-default", 2, 0 }, + { "apple,tunable-pipe-handler-host", 2, 0, true, true }, + { "apple,tunable-pwr-ctrl-default", 3, 0, true }, + { "apple,tunable-drd-usb31-gbl-default", 0, 0xc100 }, + { "apple,tunable-drd-usb31-gbl-host", 0, 0xc100, false, true }, + { "apple,tunable-drd-cio-regs-default", 1, 0x20, true }, + { "apple,tunable-drd-usb31-dbg-default", 1, 0xb00 }, + { "apple,tunable-drd-usb31-cfg-host", 1, 0x3300, false, true }, + { "apple,tunable-link-regs-default", 1, 0x300 }, + { "apple,tunable-ausbc-debug-default", 5, 0 }, +}; + +static int dwc3_apple_t8152_parse_tunables(struct platform_device *pdev, + struct dwc3_apple *appledwc) +{ + static const char * const names[] = { + "dwc3-core", "dwc3-apple", "pipe-handler", + "pwr-ctrl", "bulk-fabric", "ausbc-debug", + }; + struct device *dev = &pdev->dev; + struct resource *resources[ARRAY_SIZE(names)]; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(names); i++) { + resources[i] = platform_get_resource_byname(pdev, IORESOURCE_MEM, names[i]); + if (!resources[i]) + return dev_err_probe(dev, -EINVAL, "Missing %s MMIO\n", names[i]); + if (i < 2) + continue; + appledwc->tunable_regs[i] = devm_ioremap_resource(dev, resources[i]); + if (IS_ERR(appledwc->tunable_regs[i])) + return PTR_ERR(appledwc->tunable_regs[i]); + } + if (resource_size(resources[0]) != 0xcd00 || + resource_size(resources[1]) < 0x4b00 || + resources[1]->start != resources[0]->start + 0xcd00 || + resource_size(resources[2]) < 0x24) + return dev_err_probe(dev, -EINVAL, "Invalid T8152 register windows\n"); + appledwc->tunable_regs[1] = appledwc->apple_regs; + + for (i = 0; i < ARRAY_SIZE(t8152_host_tunables); i++) { + struct resource window = *resources[t8152_host_tunables[i].resource]; + struct apple_tunable *tunable; + + /* A fixed peripheral never applies the host-only lists, so they may be absent. */ + if (appledwc->fixed_peripheral && t8152_host_tunables[i].host_only) + continue; + if (resource_size(&window) <= t8152_host_tunables[i].offset) + return -EINVAL; + window.start += t8152_host_tunables[i].offset; + tunable = devm_apple_tunable_parse(dev, dev->of_node, + t8152_host_tunables[i].name, &window); + if (IS_ERR(tunable)) { + if (PTR_ERR(tunable) == -ENOENT && t8152_host_tunables[i].optional) + continue; + return dev_err_probe(dev, PTR_ERR(tunable), "Invalid %s\n", + t8152_host_tunables[i].name); + } + appledwc->tunables[i] = tunable; + } + return 0; +} + +/* + * T8152 pipe handler registers programmed by m1n1 src/usb.c usb_phy_bringup() + * for the USB2 device console: route the mux to the dummy backend, release the + * DWC3 reset, and write the whole non-selected override word. + */ +#define T8152_PIPE_MUX_CTRL 0x0c +#define T8152_PIPE_AON_GEN 0x1c +#define T8152_PIPE_NONSELECTED_OVERRIDE 0x20 +#define T8152_PIPE_MUX_CTRL_DUMMY 0x22 +#define T8152_PIPE_AON_GEN_DWC3_RESET_N BIT(0) +#define T8152_PIPE_NONSELECTED_DEVICE 0x9332 + +static void dwc3_apple_t8152_apply_tunables(struct dwc3_apple *appledwc, + enum dwc3_apple_state state) +{ + bool device = state == DWC3_APPLE_DEVICE; + void __iomem *pipe = appledwc->tunable_regs[2]; + unsigned int i; + u32 value; + + for (i = 0; i < ARRAY_SIZE(t8152_host_tunables); i++) { + u8 resource = t8152_host_tunables[i].resource; + u32 offset = t8152_host_tunables[i].offset; + void __iomem *base = resource ? appledwc->tunable_regs[resource] : + appledwc->dwc.regs; + + if (device && t8152_host_tunables[i].host_only) + continue; + if (!resource) + offset -= DWC3_GLOBALS_REGS_START; + apple_tunable_apply(base + offset, appledwc->tunables[i]); + } + + if (device) + return; + + writel(readl(pipe + 0x20) | BIT(15), pipe + 0x20); + /* Raw core access; io.h helpers drag unexported dwc3 tracepoints into this module. */ + value = readl(appledwc->dwc.regs - DWC3_GLOBALS_REGS_START + 0xc680); + writel(value & ~BIT(13), appledwc->dwc.regs - DWC3_GLOBALS_REGS_START + 0xc680); + value = readl(appledwc->dwc.regs - DWC3_GLOBALS_REGS_START + 0xc680); + writel(value & ~BIT(13), appledwc->dwc.regs - DWC3_GLOBALS_REGS_START + 0xc680); +} + static void dwc3_apple_set_ptrcap(struct dwc3_apple *appledwc, u32 mode) { guard(spinlock_irqsave)(&appledwc->dwc.lock); @@ -374,7 +506,40 @@ static int dwc3_apple_core_start(struct dwc3_apple *appledwc, enum dwc3_apple_st appledwc->reset_pending = true; /* Keep register access powered until both PHY shutdown and reset finish. */ dev_pm_syscore_device(appledwc->dev, true); + + if (appledwc->t8152_usb2) { + void __iomem *pipe = appledwc->tunable_regs[2]; + + if (state == DWC3_APPLE_DEVICE) { + writel(T8152_PIPE_MUX_CTRL_DUMMY, pipe + T8152_PIPE_MUX_CTRL); + writel(T8152_PIPE_NONSELECTED_DEVICE, pipe + T8152_PIPE_NONSELECTED_OVERRIDE); + } + /* + * The AON block gates every DWC3 core register with its reset. + * dwc3_core_probe() reads GSNPSID/GHWPARAMS first, so the reset + * must be released here for the host role too, not only for the + * peripheral (verified on port 1; port 2 returned garbage on the + * first hardware run without this). + */ + writel(T8152_PIPE_AON_GEN_DWC3_RESET_N, pipe + T8152_PIPE_AON_GEN); + } + + if (appledwc->t8152_usb2) { + ret = phy_init(appledwc->usb2_phy); + if (ret) + goto err_stop; + ret = phy_power_on(appledwc->usb2_phy); + if (ret) { + phy_exit(appledwc->usb2_phy); + goto err_stop; + } + } + ret = dwc3_apple_core_init(appledwc); + if (appledwc->t8152_usb2) { + phy_power_off(appledwc->usb2_phy); + phy_exit(appledwc->usb2_phy); + } if (ret) { ret_reset = dwc3_apple_core_stop(appledwc); if (ret_reset) @@ -392,6 +557,12 @@ static int dwc3_apple_core_start(struct dwc3_apple *appledwc, enum dwc3_apple_st appledwc->usb3_power_off_pending = BIT(appledwc->dwc.num_usb3_ports) - 1; return 0; + +err_stop: + ret_reset = dwc3_apple_core_stop(appledwc); + if (ret_reset) + dev_warn(appledwc->dev, "Failed to finish core shutdown, err=%d\n", ret_reset); + return ret; } /* Select the role in the core and route the USB3 PHY, before xhci or the gadget is started */ @@ -414,6 +585,27 @@ static void dwc3_apple_set_role(struct dwc3_apple *appledwc, enum dwc3_apple_sta ret = phy_set_mode(appledwc->usb3_phy, host ? PHY_MODE_USB_HOST : PHY_MODE_USB_DEVICE); if (ret) dev_warn(appledwc->dev, "USB3 PHY setup failed, USB2 only, err=%d\n", ret); + if (appledwc->t8152_usb2) + dwc3_apple_t8152_apply_tunables(appledwc, state); +} + +/* + * A T8152 controller never switches roles: it starts as a fixed host (with or + * without a hub child) or — with dr_mode = "peripheral" — as a fixed + * peripheral. Returns false for the dynamic-role profiles. + */ +static bool dwc3_apple_fixed_state(const struct dwc3_apple *appledwc, + enum dwc3_apple_state *state) +{ + if (appledwc->fixed_peripheral) { + *state = DWC3_APPLE_DEVICE; + return true; + } + if (appledwc->fixed_hub || appledwc->t8152_usb2) { + *state = DWC3_APPLE_HOST; + return true; + } + return false; } static int dwc3_apple_init(struct dwc3_apple *appledwc, enum dwc3_apple_state state) @@ -505,6 +697,16 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro if (appledwc->fixed_hub) return 0; + if (appledwc->t8152_usb2) { + enum usb_role fixed_role = appledwc->fixed_peripheral ? + USB_ROLE_DEVICE : USB_ROLE_HOST; + if (role != fixed_role) { + dev_err(appledwc->dev, "Invalid target role: %d\n", role); + return -EINVAL; + } + return 0; + } + /* * Skip role switches if appledwc is already in the desired state. The * USB-C port controller on M2 and M1/M2 Pro/Max/Ultra devices issues @@ -590,6 +792,7 @@ static bool dwc3_apple_has_fixed_hub(struct device *dev) static int dwc3_apple_probe(struct platform_device *pdev) { + enum dwc3_apple_state fixed_state; struct device *dev = &pdev->dev; struct dwc3_apple *appledwc; int ret; @@ -599,10 +802,24 @@ static int dwc3_apple_probe(struct platform_device *pdev) return -ENOMEM; appledwc->dev = &pdev->dev; + appledwc->t8152_usb2 = of_device_is_compatible(dev->of_node, "apple,t8152-dwc3"); appledwc->fixed_hub = dwc3_apple_has_fixed_hub(dev); + if (appledwc->t8152_usb2) { + enum usb_dr_mode dr_mode = usb_get_dr_mode(dev); + + if ((dr_mode != USB_DR_MODE_HOST && dr_mode != USB_DR_MODE_PERIPHERAL) || + usb_get_maximum_speed(dev) != USB_SPEED_HIGH) + return dev_err_probe(dev, -EINVAL, + "T8152 requires dr_mode = \"host\" or \"peripheral\" and maximum-speed = \"high-speed\"\n"); + appledwc->fixed_peripheral = dr_mode == USB_DR_MODE_PERIPHERAL; + if (appledwc->fixed_peripheral && appledwc->fixed_hub) + return dev_err_probe(dev, -EINVAL, + "A hub child node requires dr_mode = \"host\"\n"); + } mutex_init(&appledwc->lock); - appledwc->reset = devm_reset_control_get_exclusive(dev, NULL); + if (!appledwc->t8152_usb2) + appledwc->reset = devm_reset_control_get_exclusive(dev, NULL); if (IS_ERR(appledwc->reset)) return dev_err_probe(&pdev->dev, PTR_ERR(appledwc->reset), "Failed to get reset control\n"); @@ -612,7 +829,8 @@ static int dwc3_apple_probe(struct platform_device *pdev) return dev_err_probe(dev, PTR_ERR(appledwc->usb2_phy), "Failed to get the USB2 PHY\n"); - appledwc->usb3_phy = devm_phy_get(dev, "usb3-phy"); + if (!appledwc->t8152_usb2) + appledwc->usb3_phy = devm_phy_get(dev, "usb3-phy"); if (IS_ERR(appledwc->usb3_phy)) return dev_err_probe(dev, PTR_ERR(appledwc->usb3_phy), "Failed to get the USB3 PHY\n"); @@ -633,6 +851,11 @@ static int dwc3_apple_probe(struct platform_device *pdev) if (IS_ERR(appledwc->apple_regs)) return dev_err_probe(dev, PTR_ERR(appledwc->apple_regs), "Failed to map Apple-specific MMIO\n"); + if (appledwc->t8152_usb2) { + ret = dwc3_apple_t8152_parse_tunables(pdev, appledwc); + if (ret) + return ret; + } /* * Like a PCI xHCI controller, allow USB devices to wake the system by default. This has @@ -654,16 +877,11 @@ static int dwc3_apple_probe(struct platform_device *pdev) if (ret) return dev_err_probe(&pdev->dev, ret, "Failed to setup role switch\n"); - if (!appledwc->fixed_hub) + if (!dwc3_apple_fixed_state(appledwc, &fixed_state)) return 0; - /* - * The hub is the controller's permanent USB2 peer, and the PHY has kept - * its USB2 path up for it since its own probe, so there is nothing to - * wait for: bring the host up now. - */ scoped_guard(mutex, &appledwc->lock) - ret = dwc3_apple_init(appledwc, DWC3_APPLE_HOST); + ret = dwc3_apple_init(appledwc, fixed_state); if (ret) { /* A failed cleanup remains visible even when probe cannot be retried. */ if (appledwc->core_active || appledwc->reset_pending) @@ -671,7 +889,8 @@ static int dwc3_apple_probe(struct platform_device *pdev) if (appledwc->state != DWC3_APPLE_PROBE_PENDING) dwc3_core_remove_no_shutdown(&appledwc->dwc); usb_role_switch_unregister(appledwc->role_sw); - return dev_err_probe(dev, ret, "Failed to start host mode\n"); + return dev_err_probe(dev, ret, "Failed to start fixed %s mode\n", + fixed_state == DWC3_APPLE_DEVICE ? "peripheral" : "host"); } return 0; @@ -699,6 +918,7 @@ static void dwc3_apple_remove(struct platform_device *pdev) static const struct of_device_id dwc3_apple_of_match[] = { { .compatible = "apple,t8103-dwc3" }, + { .compatible = "apple,t8152-dwc3" }, {} }; MODULE_DEVICE_TABLE(of, dwc3_apple_of_match); diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index 095a0391ea411c..5ff40333e03f30 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -108,6 +108,8 @@ struct macaudio_platform_cfg { const char *fe_dai_names[MACAUDIO_NUM_FE_LINKS]; unsigned int dai_fmt; unsigned int primary_bclk_ratio; + /* DAI format of the speaker amplifier's link only (0: as the others). */ + unsigned int spk_dai_fmt; }; static const char *volume_control_names[] = { @@ -830,6 +832,8 @@ static int macaudio_parse_of(struct macaudio_snd_data *ma) goto err_free; link_props->is_speakers = speakers; + if (speakers && ma->cfg->spk_dai_fmt) + link->dai_fmt = ma->cfg->spk_dai_fmt; link_props->is_headphones = !speakers; link_props->codecs = devm_kcalloc(dev, ncodecs_per_cpu, @@ -1498,6 +1502,33 @@ static int macaudio_set_speaker(struct snd_soc_card *card, const char *prefix, b return 0; } +/* + * J873 (Mac mini, T8152): AOP-fed like the J700, but the speaker amplifier is + * an SN012776 on the AP's I2C bus and needs no software gain. The speaker + * is protected by the limiter in the AOP audio component (a ceiling, a + * fixed high-pass and a thermal model of the speaker in the copy path), + * not by a user-space daemon: the machine has no sense stream for one, so + * there is no volume lock to hand over (safe_vol = 0). The front-end DAIs + * are those of the AOP audio component, which keeps the T8140 names. The + * serial link format of the speaker is set below; the jack keeps the + * default. + */ +struct macaudio_platform_cfg macaudio_j873_cfg = { + .enable_speakers = true, + .amp = AMP_SN012776, + .speakers = SPKR_1W, + .stereo = false, + .amp_gain = 15, + .safe_vol = 0, + .fe_dai_names = { "j700-pcm-0", "j700-pcm-1", "j700-pcm-2" }, + /* + * The amplifier must sample on the rising bit-clock edge here: with + * the default format (falling edge) it stays in software shutdown. + */ + .spk_dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBC_CFC | + SND_SOC_DAIFMT_GATED | SND_SOC_DAIFMT_NB_IF, +}; + static int macaudio_fixup_controls(struct snd_soc_card *card) { struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); @@ -1525,6 +1556,15 @@ static int macaudio_fixup_controls(struct snd_soc_card *card) /* only 1W stereo system (J313) is uses cs42l83 */ if (ma->cfg->stereo) { CHECK(macaudio_set_speaker, "* ", false, 0); + } else if (ma->cfg == &macaudio_j873_cfg) { + /* + * The J873's SN012776 amp node carries the + * sound-name-prefix "Speaker": with the empty prefix + * the lookup of "Amp Gain Volume" fails with -EINVAL, + * and "* " would match the cs42l84 jack codec's + * "Jack HPF Corner Frequency" instead. + */ + CHECK(macaudio_set_speaker, "Speaker ", false, 0); } else { CHECK(macaudio_set_speaker, "", false, 0); } @@ -1898,6 +1938,8 @@ static const struct of_device_id macaudio_snd_device_id[] = { /* j615 AID21 sn012776 15 2x 2W+1T Compat: apple,j415-macaudio */ /* j700 AID44 max98360a - 2x 1W (AOP-fed, software gain) */ { .compatible = "apple,j700-macaudio", .data = &macaudio_j700_cfg }, + /* j873 AID? sn012776 15 1x 1W (AOP-fed, speakers disabled) */ + { .compatible = "apple,j873-macaudio", .data = &macaudio_j873_cfg }, /* Fallback, jack only */ { .compatible = "apple,macaudio"}, { } diff --git a/sound/soc/apple/t8140_aop_audio.rs b/sound/soc/apple/t8140_aop_audio.rs index f077ff4df6e6c0..6a68f04e8094ec 100644 --- a/sound/soc/apple/t8140_aop_audio.rs +++ b/sound/soc/apple/t8140_aop_audio.rs @@ -556,7 +556,14 @@ struct SndSocT8140AopData { dev: ARef, adata: Arc, service: EPICService, - ring: Arc, + /// The low-power microphone's source ring; `None` on machines without lpai. + ring: Option>, + /// The machine has the lpai microphone, whose run state also raises the + /// gated audio fabric, and PMGR serializer leaves the AP must power + /// (T8140). On T8152 neither exists: the firmware powers its own fabric + /// and the serializers, and lpai, hpai and tap attach with + /// kIOReturnNoDevice. + has_lpai: bool, ring_bytes: Atomic, sequence: Atomic, /// The low-power microphone PCM holds a reference on `lpai_run`. @@ -613,6 +620,9 @@ struct SndSocT8140AopData { /// [`SndSocT8140AopData::lpai_run_get`]. #[pin] lpai_run: Mutex, + /// The T8152 speaker's thermal limiter, see [`spk_thermal`]. + #[pin] + spkr_thermal: SpinLock, } const SPKR_START_WORK: u64 = 0; @@ -903,6 +913,9 @@ impl SndSocT8140AopData { /// power up fails the stream; recovering a domain from that state is the /// power-domain driver's business, not a consumer's. fn power_up(&self, name: &str, pds: &[&Option], attempts: u32) -> Result<()> { + if !self.has_lpai { + return Ok(()); + } for (i, slot) in pds.iter().enumerate() { let Some(pd) = slot else { dev_err!(self.dev, "{}: power domain not attached\n", name); @@ -944,11 +957,57 @@ impl SndSocT8140AopData { fn service_startup(&self, svc: &Service, pds: &[&Option]) -> Result<()> { self.attach_device(svc.dev_id, svc.name)?; self.service_set_power(svc, SPKR_POWER_STATE_PW0)?; + if !self.has_lpai { + self.log_service_props(svc, "pw0"); + } self.power_up_raising(svc.name, pds)?; svc.running.store(false, Relaxed); Ok(()) } + /// T8152 bring-up diagnostics: log what the firmware reports for a + /// service (properties 200, 300, 301 and 302) so that the stream format + /// and the serializer state can be read from the log. Read-only. + fn log_service_props(&self, svc: &Service, when: &str) { + for modifier in [ + PROP_POWER_STATE, + PROP_CHANNEL_CONTROL, + PROP_STREAM_FORMAT, + PROP_RING_GEOMETRY, + ] { + let msg = AudioGetDeviceProp::new(svc.dev_id, modifier); + // SAFETY: the message is a plain packed firmware record. + let bytes = unsafe { + slice::from_raw_parts( + ptr::from_ref(&msg).cast::(), + mem::size_of::(), + ) + }; + match self + .adata + .epic_call_ret(&self.service, EPIC_SUBTYPE_WRAPPED_CALL, bytes, 64) + { + Ok((ret, data)) => dev_dbg!( + self.dev, + "{} {}: property {} retcode {:#x} data {:02x?}\n", + svc.name, + when, + modifier, + ret, + &data[..] + ), + Err(e) => dev_dbg!( + self.dev, + "{} {}: property {} call failed: {:?}\n", + svc.name, + when, + modifier, + e + ), + } + } + } + /// [`Self::power_up`] with the fabric raised first. /// /// The fabric may be gated at any open (a low-power capture is enough, @@ -967,6 +1026,9 @@ impl SndSocT8140AopData { /// microphone is attached for the firmware's lifetime, so hold it in /// `runn` around `f`, once its first report has arrived. fn with_fabric_raised(&self, f: impl FnOnce() -> Result) -> Result { + if !self.has_lpai { + return f(); + } self.lpai_run_get()?; let mut polls = 0; while !self.reported.load(Relaxed) && polls < FABRIC_RAISE_POLLS { @@ -994,6 +1056,9 @@ impl SndSocT8140AopData { } self.service_set_power(svc, SPKR_POWER_STATE_PWRD)?; svc.running.store(true, Relaxed); + if !self.has_lpai { + self.log_service_props(svc, "pwrd"); + } Ok(()) } @@ -1039,14 +1104,23 @@ impl SndSocT8140AopData { // data after the copy-time gain, it senses no current or voltage. ---- fn sense_startup(&self) -> Result<()> { + if !self.has_lpai { + return Ok(()); + } self.service_startup(&self.tap, &[&self.pd_mca0]) } fn sense_prepare(&self) -> Result<()> { + if !self.has_lpai { + return Ok(()); + } self.service_run(&self.tap) } fn sense_shutdown(&self) { + if !self.has_lpai { + return; + } self.service_shutdown(&self.tap, &[&self.pd_mca0]) } @@ -1138,6 +1212,17 @@ impl SndSocT8140AopData { /// Output mute (property 700) is independent of the amplifier's SD_MODE; /// the amplifier stays off across a failure here. fn spkr_set_mute(&self, muted: bool) -> Result<()> { + if !self.has_lpai { + // T8152: the firmware answers every property above 302 with + // 0xe00002f0 (unsupported), the mute request included (measured on + // an M6 Mac mini); the amplifier is muted through its own codec. + dev_dbg!( + self.dev, + "speaker mute {} not requested on this firmware\n", + muted + ); + return Ok(()); + } let ret = self.epic_wrapped_call(&AudioSetDeviceProp::new( AUDIO_DEV_SPKR, PROP_OUTPUT_MUTE, @@ -1340,7 +1425,7 @@ impl ReportListener for SndSocT8140AopData { let dma_area = unsafe { (*(*st.substream).runtime).dma_area }; let buffer_bytes = st.buffer_bytes; let new_ptr = copy_span( - &self.ring, + self.ring.as_ref().ok_or(ENODEV)?, dma_area, buffer_bytes, ring_bytes, @@ -2238,7 +2323,7 @@ unsafe extern "C" fn asoc_pcm_hw_params( /// Called by ASoC on this driver's registered component, with live /// arguments belonging to one of its links. unsafe extern "C" fn asoc_pcm_prepare( - _component: *mut bindings::snd_soc_component, + component: *mut bindings::snd_soc_component, substream: *mut bindings::snd_pcm_substream, ) -> i32 { // SAFETY: ASoC hands its ops a live substream whose private data is the @@ -2259,6 +2344,14 @@ unsafe extern "C" fn asoc_pcm_prepare( // trigger START. unsafe { ptr::write_bytes(area, 0, bytes) }; } + // SAFETY: ASoC calls this op on a component whose descriptor/context stays live. + let data = unsafe { component_data(component) }; + if !data.has_lpai { + // The speaker cooled while no stream ran. + let mut thermal = data.spkr_thermal.lock(); + thermal.cool_idle(); + thermal.reset_highpass(); + } 0 } @@ -2288,6 +2381,242 @@ fn spkr_f32_bits(sample: i32, gain_q31: u32) -> u32 { f32_bits_of_q31(scaled as i32) } +/// Feed-forward thermal limiter of the T8152 speaker. +/// +/// The J873 has no speaker tap: the AOP offers no sense stream, so there is +/// nothing a daemon could measure. The vendor protects this speaker with a +/// thermal model driven by the signal itself, and so does this limiter, in the +/// copy path where no stream can bypass it. The vendor's numbers for the +/// speaker (one type-A speaker for all channels): amplifier sensitivity +/// 11.9 V per full scale, voice-coil resistance 4.6 ohm, coil 40 C/W with a +/// time constant of 3.4 s, magnet 60 C/W with 210 s, ambient 50 C, limit +/// 140 C. The thermal part is the two-filter model of the Linux speaker +/// daemon: the power of every word (x * 11.9 V)^2 / 4.6 ohm, with x the word +/// after the limiter gain in units of the I2S full scale, heats the magnet +/// towards ambient + 60 C/W * P and the coil towards the magnet + 40 C/W * P; +/// the gain falls from 0 dB to the sustained level (a full-scale square wave +/// at 98 % of (140 - 50) / (40 + 60) = 0.9 W, -15.43 dB) over the last 20 C +/// below the limit. Beyond the vendor, the attack is immediate (one block of +/// 128 words) and the release follows the vendor's 20 s, and the amplifier is +/// assumed at the voltage of the full 11.9 V per full scale. All of it is +/// fixed point: no floating point exists in the kernel. `CEILING` is a hard +/// amplitude ceiling in the first audible tests (-20 dB below the sustained +/// level), taken out only by a rebuild. +mod spk_thermal { + // Temperatures are Q40 degrees C, gains Q30, the power in units of + // (I2S full scale)^2 * 2^32. + const KCQ: i64 = 315236; + const KMQ: i64 = 472854; + const AC: i64 = 26317; + const AM: i64 = 426; + const T_AMB_Q: i64 = 54975581388800; + const LO_Q: i64 = 131941395333120; + const WIN_Q: i64 = 21990232555520; + const HYST_Q: i64 = 5497558138880; + const REL: i64 = 572586; + /// 10^(-15.43 dB * k / 64 / 20) in Q30, rounded down. + const GTAB: [i64; 65] = [ + 1073741824, 1044350352, 1015763411, 987958979, 960915636, 934612548, 909029454, 884146644, + 859944950, 836405728, 813510843, 791242658, 769584020, 748518241, 728029096, 708100798, + 688717996, 669865759, 651529563, 633695283, 616349179, 599477889, 583068416, 567108119, + 551584701, 536486206, 521801000, 507517772, 493625518, 480113536, 466971416, 454189035, + 441756545, 429664369, 417903192, 406463952, 395337838, 384516279, 373990937, 363753705, + 353796697, 344112240, 334692876, 325531348, 316620597, 307953760, 299524160, 291325303, + 283350873, 275594726, 268050888, 260713546, 253577050, 246635900, 239884750, 233318399, + 226931788, 220719998, 214678242, 208801867, 203086346, 197527275, 192120373, 186861473, + 181746525, + ]; + /// Start state: the coil just below the start of the limiting window (the + /// driver cannot know how warm the speaker is). + const T_START_COIL_Q: i64 = 130841883705344; + const T_START_MAGNET_Q: i64 = 100495362778726; + /// The hard amplitude ceiling of the first audible tests: -20 dB below the + /// sustained level (a worst-case power of about 9 mW); kept for the unit + /// tests of the model. + #[allow(dead_code)] + pub(super) const CEILING_M20_Q30: i64 = 18174652; + /// The ceiling of the speaker path: 0.078 of full scale (0.93 V peak and + /// about 94 mW for a sine at an amplifier gain of 15, by this model), 6 dB + /// below the level at which the vendor's own driver plays a 440 Hz tone at + /// its maximum volume, measured with one microphone at one place. + pub(super) const CEILING_Q30: i64 = 83_800_000; + /// No ceiling (unit tests, later steps). + #[allow(dead_code)] + pub(super) const CEILING_0DB_Q30: i64 = 1073741824; + /// 10 ms steps of the idle cooling: 1 - exp(-0.01 s / tau) in Q32. + const AC10: i64 = 12613698; + const AM10: i64 = 204517; + const ONE: i64 = 1 << 30; + + fn gain_for_reduction(r16: i64) -> i64 { + let k = (r16 >> 10) as usize; + let f = r16 & 1023; + if k >= 64 { + return GTAB[64]; + } + GTAB[k] + (((GTAB[k + 1] - GTAB[k]) * f) >> 10) + } + + // Fixed high-pass of the speaker path: 2nd-order Butterworth, 100 Hz at + // 48 kHz, direct form I, Q28 coefficients, 64-bit state. The speaker + // cone moves far more at low frequencies for the same voltage and the + // power model of the limiter has no excursion term; below 100 Hz the + // speaker radiates almost nothing anyway. The output is held to the + // input's full scale (a step through a high-pass overshoots by up to + // 2x), so the ceiling stays a true peak ceiling. |x| <= 2^31 and the + // coefficients are below 2^29: the sum of five products stays below + // 2^63 (the state, below 2.5 full scales, keeps it under 2^61). + const HP_B0: i64 = 265_962_279; + const HP_B1: i64 = -531_924_558; + const HP_B2: i64 = 265_962_279; + const HP_A1: i64 = -531_901_772; + const HP_A2: i64 = 263_511_889; + const HP_SHIFT: u32 = 28; + const HP_LIMIT: i64 = 1 << 30; + + #[derive(Default)] + pub(super) struct Highpass { + x1: i64, + x2: i64, + y1: i64, + y2: i64, + } + + impl Highpass { + pub(super) fn step(&mut self, x: i32) -> i32 { + let x = i64::from(x); + let acc = HP_B0 * x + HP_B1 * self.x1 + HP_B2 * self.x2 + - HP_A1 * self.y1 + - HP_A2 * self.y2; + // The feedback keeps the unclamped value: feeding the clamped one + // back would latch a nearly full-scale sine into sustained + // clipping. The state stays below 2.5 full scales. + let y = (acc + (1 << (HP_SHIFT - 1))) >> HP_SHIFT; + self.x2 = self.x1; + self.x1 = x; + self.y2 = self.y1; + self.y1 = y; + y.clamp(-HP_LIMIT, HP_LIMIT) as i32 + } + } + + pub(super) struct SpkThermal { + t_coil: i64, + t_magnet: i64, + hyst_coil: i64, + hyst_magnet: i64, + gain: i64, + ceiling: i64, + hp: Highpass, + last: Option>, + } + + impl SpkThermal { + pub(super) fn new(ceiling: i64) -> Self { + Self { + t_coil: T_START_COIL_Q, + t_magnet: T_START_MAGNET_Q, + hyst_coil: T_START_COIL_Q, + hyst_magnet: T_START_MAGNET_Q, + gain: ONE, + ceiling, + hp: Highpass::default(), + last: None, + } + } + + /// Limit one block of words (the I2S full scale is 2^30), in place, and + /// advance the model by the block. + pub(super) fn block(&mut self, words: &mut [i32]) { + let g = (self.gain * self.ceiling) >> 30; + let (mut tc, mut tm) = (self.t_coil, self.t_magnet); + for w in words.iter_mut() { + let y = (i64::from(*w) * g) >> 30; + let yq = y >> 14; + let pfs = yq * yq; + let tgt_c = tm + pfs * KCQ; + let tgt_m = T_AMB_Q + pfs * KMQ; + tc += (((tgt_c - tc) >> 12) * AC) >> 20; + tm += (((tgt_m - tm) >> 12) * AM) >> 20; + *w = y as i32; + } + self.t_coil = tc; + self.t_magnet = tm; + self.hyst_coil = self.hyst_coil.max(tc).min(tc + HYST_Q); + self.hyst_magnet = self.hyst_magnet.max(tm).min(tm + HYST_Q); + let temp = self.hyst_coil.max(self.hyst_magnet); + let r16 = if temp <= LO_Q { + 0 + } else { + core::cmp::min(65536, ((temp - LO_Q) >> 20) * 65536 / (WIN_Q >> 20)) + }; + let target = gain_for_reduction(r16); + if target < self.gain { + self.gain = target; + } else { + self.gain += ((target - self.gain) * REL) >> 32; + } + self.last = Some(kernel::time::Instant::now()); + } + + /// Filter one block of words in place with the speaker path's high-pass + /// (before `block`; the filter state carries over from block to block). + pub(super) fn highpass(&mut self, words: &mut [i32]) { + for w in words.iter_mut() { + *w = self.hp.step(*w); + } + } + + /// Start the high-pass from rest (a new stream). + pub(super) fn reset_highpass(&mut self) { + self.hp = Highpass::default(); + } + + /// Let the speaker cool for the time since the last block: the same + /// filters with no power, in steps of 10 ms (at most 20 minutes). + pub(super) fn cool_idle(&mut self) { + let Some(last) = self.last.take() else { + return; + }; + let ms = core::cmp::min(last.elapsed().as_millis(), 20 * 60 * 1000); + self.cool(ms as u64); + } + + pub(super) fn cool(&mut self, ms: u64) { + let (mut tc, mut tm) = (self.t_coil, self.t_magnet); + for _ in 0..ms / 10 { + tc += (((tm - tc) >> 12) * AC10) >> 20; + tm += (((T_AMB_Q - tm) >> 12) * AM10) >> 20; + } + self.t_coil = tc; + self.t_magnet = tm; + self.hyst_coil = tc; + self.hyst_magnet = tm; + } + + /// Unit tests: start from a given state. + #[allow(dead_code)] + pub(super) fn cool_from(&mut self, coil: i64, magnet: i64, ms: u64) { + self.t_coil = coil; + self.t_magnet = magnet; + self.cool(ms); + } + + #[allow(dead_code)] + pub(super) fn state(&self) -> (i64, i64, i64, i64, i64) { + ( + self.t_coil, + self.t_magnet, + self.hyst_coil, + self.hyst_magnet, + self.gain, + ) + } + } +} + +use spk_thermal::SpkThermal; + /// Copy between userspace and the front-end buffers. The speaker front-end /// converts and limits; the jack front-ends copy verbatim (their buffers are /// also mmap-able, this path serves read()/write()). @@ -2360,10 +2689,34 @@ unsafe extern "C" fn asoc_pcm_copy( if iov.copy_from_iter(&mut chunk[..n]) != n { return EFAULT.to_errno(); } - for (i, word) in chunk[..n].chunks_exact(4).enumerate() { - let sample = i32::from_le_bytes([word[0], word[1], word[2], word[3]]); - let bits = spkr_f32_bits(sample, gain); - dst[off + i * 4..off + i * 4 + 4].copy_from_slice(&bits.to_le_bytes()); + if data.has_lpai { + for (i, word) in chunk[..n].chunks_exact(4).enumerate() { + let sample = i32::from_le_bytes([word[0], word[1], word[2], word[3]]); + let bits = spkr_f32_bits(sample, gain); + dst[off + i * 4..off + i * 4 + 4].copy_from_slice(&bits.to_le_bytes()); + } + } else { + // T8152: every word goes through the thermal limiter, the one + // protection this speaker has. + let mut words = [0i32; 128]; + let count = n / 4; + for (w, word) in words.iter_mut().zip(chunk[..n].chunks_exact(4)) { + let sample = i32::from_le_bytes([word[0], word[1], word[2], word[3]]); + *w = ((i64::from(sample) * i64::from(gain)) >> 31) as i32; + } + { + let mut thermal = data.spkr_thermal.lock(); + thermal.highpass(&mut words[..count]); + thermal.block(&mut words[..count]); + } + // The amplifier takes plain PCM from the AOP link (there is no + // LEAP on this machine to consume float32 words), but only the + // low 24 bits of each word: measured, a word beyond 2^23 wraps. + // The limiter's full scale, 2^30, is therefore 2^23 on the wire. + for (i, w) in words[..count].iter().enumerate() { + let w24 = *w >> 7; + dst[off + i * 4..off + i * 4 + 4].copy_from_slice(&w24.to_le_bytes()); + } } off += n; } @@ -2725,6 +3078,24 @@ fn pcm_stream( } } +fn pcm_stream_range( + name: &'static core::ffi::CStr, + formats: u64, + channels_min: u32, + channels_max: u32, +) -> bindings::snd_soc_pcm_stream { + bindings::snd_soc_pcm_stream { + stream_name: name.as_char_ptr(), + formats, + rates: bindings::SNDRV_PCM_RATE_48000, + rate_min: 48000, + rate_max: 48000, + channels_min, + channels_max, + ..Default::default() + } +} + /// The AOP-configured serializers take 24-bit samples from the low bits /// of each 32-bit DMA word: S24_LE. const OUT_FORMATS: u64 = bindings::BINDINGS_SNDRV_PCM_FMTBIT_S24_LE; @@ -2737,7 +3108,10 @@ const S32_FORMATS: u64 = bindings::BINDINGS_SNDRV_PCM_FMTBIT_S32_LE; /// renumbers every DAI after it and silently repoints the existing links. const DAI_COUNT: usize = 2 + FE_COUNT; -fn dai_drivers(ops: &DaiOps) -> [bindings::snd_soc_dai_driver; DAI_COUNT] { +// `mono_speaker`: T8152, whose speaker serializer takes one 32-bit word per +// 48 kHz frame (measured: the DMA drains 192000 bytes per second whatever the +// stream), so the speaker front-end must also accept one channel. +fn dai_drivers(ops: &DaiOps, mono_speaker: bool) -> [bindings::snd_soc_dai_driver; DAI_COUNT] { [ bindings::snd_soc_dai_driver { name: c"cout".as_char_ptr(), @@ -2762,7 +3136,12 @@ fn dai_drivers(ops: &DaiOps) -> [bindings::snd_soc_dai_driver; DAI_COUNT] { id: FE_DAI_ID_BASE + 1, ops: &ops.fe, // the speaker front-end: S32 in, converted through the ceiling - playback: pcm_stream(c"PCM1 TX", S32_FORMATS, 2), + playback: pcm_stream_range( + c"PCM1 TX", + S32_FORMATS, + if mono_speaker { 1 } else { 2 }, + 2, + ), ..Default::default() }, bindings::snd_soc_dai_driver { @@ -2780,7 +3159,12 @@ fn dai_drivers(ops: &DaiOps) -> [bindings::snd_soc_dai_driver; DAI_COUNT] { name: c"spkr".as_char_ptr(), id: BE_DAI_ID_SPKR, ops: &ops.spkr, - playback: pcm_stream(c"SPKR TX", S32_FORMATS, 2), + playback: pcm_stream_range( + c"SPKR TX", + S32_FORMATS, + if mono_speaker { 1 } else { 2 }, + 2, + ), capture: pcm_stream(c"SPKR RX", S32_FORMATS, 2), ..Default::default() }, @@ -2806,7 +3190,7 @@ const _: () = assert!(mem::offset_of!(AsocContext, component) == 0); impl AsocContext { fn new(data: Arc) -> Result> { let ops = KBox::new(DaiOps::new(), GFP_KERNEL)?; - let dais = KBox::new(Opaque::new(dai_drivers(&ops)), GFP_KERNEL)?; + let dais = KBox::new(Opaque::new(dai_drivers(&ops, !data.has_lpai)), GFP_KERNEL)?; let volume = KBox::new( Opaque::new(bindings::soc_mixer_control { min: 0, @@ -2931,89 +3315,91 @@ impl SndSocT8140AopDriver { closed: AtomicFlag::new(false), }; - // The low-power microphone: its ring is reported to us by the - // firmware for the AOP's lifetime. - data.attach_device(AUDIO_DEV_LPAI, "lpai")?; - data.set_channel_control()?; - data.read_geometry()?; - let listener: Arc = data.clone(); - data.adata - .add_report_listener(data.service, EPIC_SUBTYPE_PRODUCER_REPORT, listener)?; - - // SAFETY: FFI call with the live device and the module; `this.card` receives the new card. - let ret = unsafe { - bindings::snd_card_new( - data.dev.as_raw(), - -1, - ptr::null(), - THIS_MODULE.as_ptr(), - 0, - &mut this.card, - ) - }; - if ret < 0 { - dev_err!(data.dev, "unable to allocate the sound card\n"); - return Err(Error::from_errno(ret)); - } - // Named like the other AOP audio cards: Apple. - let chassis = fwnode - .property_read::(c_str!("apple,chassis-name")) - .required_by(&data.dev)?; - let machine_kind = fwnode - .property_read::(c_str!("apple,machine-kind")) - .required_by(&data.dev)?; - let id_str = CString::try_from_fmt(fmt!("Apple{}AOP", *chassis))?; - let shortname = CString::try_from_fmt(fmt!("{} {} AOP", *machine_kind, *chassis))?; - let longname = CString::try_from_fmt(fmt!("{} {} AOP Audio", *machine_kind, *chassis))?; - // SAFETY: `card` was created by `snd_card_new` above and is not - // registered yet, so nothing else accesses it. - let card = unsafe { &mut *this.card }; - let named = copy_str(&mut card.driver, c"t8140-aop-audio".to_bytes_with_nul()) - .and_then(|()| copy_str(&mut card.id, id_str.to_bytes_with_nul())) - .and_then(|()| copy_str(&mut card.shortname, shortname.to_bytes_with_nul())) - .and_then(|()| copy_str(&mut card.longname, longname.to_bytes_with_nul())); - if let Err(e) = named { - dev_err!( - data.dev, - "apple,chassis-name or apple,machine-kind too long for the card names\n" - ); - return Err(e); - } - - this.new_pcm(0, c"lpai", c"Low-Power Microphone", &Self::LPAI_OPS)?; - this.new_pcm(1, c"hpai", c"High-Quality Microphone", &Self::HPAI_OPS)?; - - // Both microphones' jack: unplugged while a lid is closed. - let mut jack = ptr::null_mut(); - // SAFETY: `card` was created by `snd_card_new` above and is not registered yet; the jack - // becomes one of its devices and is freed with it. - let ret = unsafe { - bindings::snd_jack_new( - this.card, - c"Internal Mic".as_char_ptr(), - bindings::snd_jack_types_SND_JACK_MICROPHONE as i32, - &mut jack, - true, - // No input device of its own: the lid it follows already - // is one, and the control is what userspace reads. - true, - ) - }; - if ret < 0 { - dev_err!(data.dev, "unable to create the microphone jack\n"); - return Err(Error::from_errno(ret)); - } - data.mic_jack.store(jack, Relaxed); - this.register_lid_handler()?; - // The state as the card is registered, from the only reporter. - data.as_arc_borrow().queue_jack_report(); - data.flush_jack_report(); + if data.has_lpai { + // The low-power microphone: its ring is reported to us by the + // firmware for the AOP's lifetime. + data.attach_device(AUDIO_DEV_LPAI, "lpai")?; + data.set_channel_control()?; + data.read_geometry()?; + let listener: Arc = data.clone(); + data.adata + .add_report_listener(data.service, EPIC_SUBTYPE_PRODUCER_REPORT, listener)?; + + // SAFETY: FFI call with the live device and the module; `this.card` receives the new card. + let ret = unsafe { + bindings::snd_card_new( + data.dev.as_raw(), + -1, + ptr::null(), + THIS_MODULE.as_ptr(), + 0, + &mut this.card, + ) + }; + if ret < 0 { + dev_err!(data.dev, "unable to allocate the sound card\n"); + return Err(Error::from_errno(ret)); + } + // Named like the other AOP audio cards: Apple. + let chassis = fwnode + .property_read::(c_str!("apple,chassis-name")) + .required_by(&data.dev)?; + let machine_kind = fwnode + .property_read::(c_str!("apple,machine-kind")) + .required_by(&data.dev)?; + let id_str = CString::try_from_fmt(fmt!("Apple{}AOP", *chassis))?; + let shortname = CString::try_from_fmt(fmt!("{} {} AOP", *machine_kind, *chassis))?; + let longname = CString::try_from_fmt(fmt!("{} {} AOP Audio", *machine_kind, *chassis))?; + // SAFETY: `card` was created by `snd_card_new` above and is not + // registered yet, so nothing else accesses it. + let card = unsafe { &mut *this.card }; + let named = copy_str(&mut card.driver, c"t8140-aop-audio".to_bytes_with_nul()) + .and_then(|()| copy_str(&mut card.id, id_str.to_bytes_with_nul())) + .and_then(|()| copy_str(&mut card.shortname, shortname.to_bytes_with_nul())) + .and_then(|()| copy_str(&mut card.longname, longname.to_bytes_with_nul())); + if let Err(e) = named { + dev_err!( + data.dev, + "apple,chassis-name or apple,machine-kind too long for the card names\n" + ); + return Err(e); + } - // SAFETY: `card` was created by `snd_card_new` above and is not registered yet. - let ret = unsafe { bindings::snd_card_register(this.card) }; - if ret < 0 { - dev_err!(data.dev, "unable to register the sound card\n"); - return Err(Error::from_errno(ret)); + this.new_pcm(0, c"lpai", c"Low-Power Microphone", &Self::LPAI_OPS)?; + this.new_pcm(1, c"hpai", c"High-Quality Microphone", &Self::HPAI_OPS)?; + + // Both microphones' jack: unplugged while a lid is closed. + let mut jack = ptr::null_mut(); + // SAFETY: `card` was created by `snd_card_new` above and is not registered yet; the jack + // becomes one of its devices and is freed with it. + let ret = unsafe { + bindings::snd_jack_new( + this.card, + c"Internal Mic".as_char_ptr(), + bindings::snd_jack_types_SND_JACK_MICROPHONE as i32, + &mut jack, + true, + // No input device of its own: the lid it follows already + // is one, and the control is what userspace reads. + true, + ) + }; + if ret < 0 { + dev_err!(data.dev, "unable to create the microphone jack\n"); + return Err(Error::from_errno(ret)); + } + data.mic_jack.store(jack, Relaxed); + this.register_lid_handler()?; + // The state as the card is registered, from the only reporter. + data.as_arc_borrow().queue_jack_report(); + data.flush_jack_report(); + + // SAFETY: `card` was created by `snd_card_new` above and is not registered yet. + let ret = unsafe { bindings::snd_card_register(this.card) }; + if ret < 0 { + dev_err!(data.dev, "unable to register the sound card\n"); + return Err(Error::from_errno(ret)); + } } // The ASoC side: the jack (cout) and speaker back-ends, the @@ -3195,23 +3581,29 @@ unsafe extern "C" fn hpai_pcm_hw_params( } } +// The match data says whether the machine has the lpai microphone, its source +// ring and the AP-powered serializer leaves (T8140), or not (T8152). kernel::of_device_table!( OF_TABLE, MODULE_OF_TABLE, ::IdInfo, - [(of::DeviceId::new(c_str!("apple,t8140-aop-audio")), ())] + [ + (of::DeviceId::new(c_str!("apple,t8140-aop-audio")), true), + (of::DeviceId::new(c_str!("apple,t8152-aop-audio")), false), + ] ); impl platform::Driver for SndSocT8140AopDriver { - type IdInfo = (); + type IdInfo = bool; type Data<'bound> = SndSocT8140AopDriver; const OF_ID_TABLE: Option> = Some(&OF_TABLE); fn probe<'bound>( pdev: &'bound platform::Device>, - _info: Option<&'bound Self::IdInfo>, + info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, Error> + 'bound { + let has_lpai = *info.ok_or(ENODEV)?; let dev = ARef::::from(pdev.as_ref()); // SAFETY: this is a service device the AOP core registered, being // probed here; the lookup defers the probe until the core has @@ -3230,18 +3622,28 @@ impl platform::Driver for SndSocT8140AopDriver { // owned by the AOP driver because the firmware binds it once per boot. // SAFETY: no DMA is in flight on this device yet. unsafe { pdev.dma_set_mask_and_coherent(DmaMask::new::<42>())? }; - let ring = adata.source_ring(pdev.as_ref(), RING_BYTES)?; + let ring = if has_lpai { + Some(adata.source_ring(pdev.as_ref(), RING_BYTES)?) + } else { + None + }; // The serializer leaves. A missing one only disables the paths that - // need it; those fail to open with ENODEV. + // need it; those fail to open with ENODEV. T8152 has none: the + // firmware powers its own serializers. let mut pds: [Option; 5] = [None, None, None, None, None]; - for (slot, (name, dt_name)) in pds.iter_mut().zip([ - ("mca1", c"mca1"), - ("mca0", c"mca0"), - ("tx0", c"tx0"), - ("leap mca", c"mca"), - ("rx0", c"rx0"), - ]) { + let leaves: &[(&'static str, &'static core::ffi::CStr)] = if has_lpai { + &[ + ("mca1", c"mca1"), + ("mca0", c"mca0"), + ("tx0", c"tx0"), + ("leap mca", c"mca"), + ("rx0", c"rx0"), + ] + } else { + &[] + }; + for (slot, (name, dt_name)) in pds.iter_mut().zip(leaves.iter().copied()) { match PowerDomain::attach(&dev, name, dt_name) { Ok(Some(pd)) => *slot = Some(pd), Ok(None) => dev_warn!(dev, "no {} power domain\n", name), @@ -3257,6 +3659,7 @@ impl platform::Driver for SndSocT8140AopDriver { adata, service: svc, ring, + has_lpai, ring_bytes: Atomic::new(0), sequence: Atomic::new(0), powered: AtomicFlag::new(false), @@ -3294,6 +3697,7 @@ impl platform::Driver for SndSocT8140AopDriver { users: 0, running: false, }), + spkr_thermal <- new_spinlock!(SpkThermal::new(spk_thermal::CEILING_Q30)), }), GFP_KERNEL, )?; @@ -3637,3 +4041,179 @@ mod lid_tests { assert!(lid.mic_cut()); } } + +#[kunit_tests(snd_soc_t8140_aop_audio_thermal)] +mod thermal_tests { + use super::spk_thermal::{Highpass, SpkThermal, CEILING_0DB_Q30, CEILING_M20_Q30, CEILING_Q30}; + + /// The 100 Hz square wave at the I2S full scale, 240 words per half period. + fn square(i: usize) -> i32 { + if (i / 240) % 2 == 0 { + 0x4000_0000 + } else { + -0x4000_0000 + } + } + + fn run_square(l: &mut SpkThermal, blocks: usize) -> i32 { + let mut words = [0i32; 128]; + let mut peak = 0i32; + for b in 0..blocks { + for (k, w) in words.iter_mut().enumerate() { + *w = square(b * 128 + k); + } + l.block(&mut words); + for w in words.iter() { + peak = peak.max(w.abs()); + } + } + peak + } + + /// The same numbers as the host model of the J873 speaker limiter (ten + /// seconds of a full-scale square wave, then two seconds of silence). + #[test] + fn ten_seconds_of_full_scale_square_match_the_host_model() { + let mut l = SpkThermal::new(CEILING_0DB_Q30); + let peak = run_square(&mut l, 3750); + assert_eq!(peak, 1_073_741_824); + assert_eq!( + l.state(), + ( + 152358436004836, + 102381555896261, + 152358436004836, + 102381555896261, + 206396213 + ) + ); + let silence = [0i32; 128]; + for _ in 0..750 { + let mut w = silence; + l.block(&mut w); + } + assert_eq!( + l.state(), + ( + 130024328395229, + 101932305864922, + 135521886534109, + 102381555896261, + 226457860 + ) + ); + } + + /// Peak of the high-pass output for a sine of peak 2^28 made by an integer + /// oscillator (`next = 2 cos(w) cur - prev`, Q30): `c2` is 2 cos(w) and + /// `s1` the first sample; the peak is taken over the last two periods + /// after the start-up has died out. + fn highpass_peak(c2: i64, s1: i64, period: usize) -> i64 { + let mut hp = Highpass::default(); + let (mut prev, mut cur) = (0i64, s1); + let mut peak = 0i64; + for n in 0..8000usize { + let y = i64::from(hp.step(prev as i32)); + if n >= 8000 - 2 * period { + peak = peak.max(y.abs()); + } + let next = ((c2 * cur) >> 30) - prev; + prev = cur; + cur = next; + } + peak + } + + /// Amplitude relative to the input peak 2^28, in 1/10000: the ideal + /// Butterworth magnitudes are 0.2425, 0.7071, 0.9701 and 0.9987. + fn highpass_gain(c2: i64, s1: i64, period: usize) -> i64 { + highpass_peak(c2, s1, period) * 10_000 / (1 << 28) + } + + #[test] + fn the_highpass_follows_a_100_hz_butterworth() { + // 50, 100, 200 and 440 Hz at 48 kHz + assert!((highpass_gain(2_147_437_652, 1_756_893, 960) - 2_425).abs() <= 100); + assert!((highpass_gain(2_147_299_668, 3_513_712, 480) - 7_071).abs() <= 100); + assert!((highpass_gain(2_146_747_759, 7_026_821, 240) - 9_701).abs() <= 100); + assert!((highpass_gain(2_143_922_726, 15_452_226, 109) - 9_987).abs() <= 100); + } + + #[test] + fn the_highpass_blocks_a_constant_and_holds_full_scale() { + let mut hp = Highpass::default(); + let mut last = 0i32; + for _ in 0..300_000 { + last = hp.step(1 << 20); + } + assert!(i64::from(last).abs() < 1 << 15); + // a full-scale square wave at 120 Hz: the overshoot is held to full scale + let mut hp = Highpass::default(); + let mut peak = 0i32; + for i in 0..40_000usize { + let x = if (i / 200) % 2 == 0 { 1 << 30 } else { -(1 << 30) }; + peak = peak.max(hp.step(x).abs()); + } + assert_eq!(peak, 1 << 30); + } + + /// A full-scale 440 Hz sine (peak 2^30, integer oscillator) through the + /// high-pass: its linear peak is 0.9987 of full scale, so no sample may + /// sit on the clamp (a clamped feedback latched it into clipping). + #[test] + fn a_full_scale_sine_does_not_latch_the_highpass_into_clipping() { + let mut hp = Highpass::default(); + let (mut prev, mut cur) = (0i64, 61_808_904i64); + let mut clamped = 0; + for n in 0..48_000usize { + let y = hp.step(prev as i32); + if n >= 48_000 - 3_000 && y.abs() == 1 << 30 { + clamped += 1; + } + let next = ((2_143_922_726i64 * cur) >> 30) - prev; + prev = cur; + cur = next; + } + assert_eq!(clamped, 0); + } + + #[test] + fn the_coil_stays_below_the_limit_with_a_full_scale_square() { + let mut l = SpkThermal::new(CEILING_0DB_Q30); + run_square(&mut l, 3750 * 6); + // 140 C in Q40 + assert!(l.state().0 < 140i64 << 40); + } + + /// At the speaker ceiling a full-scale square wave stays far below the + /// sustained power (0.19 W against 0.9 W at 11.9 V per full scale): the + /// thermal gain never moves, the ceiling is a pure peak limit. + #[test] + fn the_speaker_ceiling_is_a_peak_limit_below_the_sustained_level() { + let mut l = SpkThermal::new(CEILING_Q30); + let peak = run_square(&mut l, 3750 * 3); + assert_eq!(i64::from(peak), CEILING_Q30); + assert_eq!(l.state().4, 1 << 30); + } + + #[test] + fn idle_cooling_matches_the_host_model() { + let mut l = SpkThermal::new(CEILING_0DB_Q30); + l.cool_from( + 139i64 << 40, + (50i64 << 40) + (((139i64 - 50) * 60 << 40) / 100), + 60_000, + ); + assert_eq!(l.state().0, 99824966751432); + assert_eq!(l.state().1, 99097783336276); + } + + #[test] + fn the_first_audible_ceiling_is_20_db_below_the_sustained_level() { + let mut l = SpkThermal::new(CEILING_M20_Q30); + let mut w = [0x4000_0000i32; 128]; + l.block(&mut w); + assert_eq!(w[0], 18174652); + assert_eq!(w[127], 18174652); + } +} diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c index 10d238b9220a49..02f4294408a761 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c @@ -63,6 +63,13 @@ static void regulator_disable(int p) { assert(!p); } +/* Mirrors the pipe-handler window defines in dwc3-apple.c for the fixed T8152 role. */ +#define T8152_PIPE_MUX_CTRL 0x0c +#define T8152_PIPE_AON_GEN 0x1c +#define T8152_PIPE_NONSELECTED_OVERRIDE 0x20 +#define T8152_PIPE_MUX_CTRL_DUMMY 0x22 +#define T8152_PIPE_AON_GEN_DWC3_RESET_N BIT(0) +#define T8152_PIPE_NONSELECTED_DEVICE 0x9332 struct dwc3 { struct phy *usb2_generic_phy[3], *usb3_generic_phy[3]; int num_usb2_ports, num_usb3_ports, usb2_phy, usb3_phy; @@ -76,8 +83,10 @@ struct dwc3_apple { int lock; void *dev; struct phy *usb2_phy, *usb3_phy; - bool fixed_hub, core_active, core_exit_pending, reset_pending; + bool fixed_hub, t8152_usb2, fixed_peripheral; + bool core_active, core_exit_pending, reset_pending; unsigned long usb2_power_off_pending, usb3_power_off_pending; + void __iomem *tunable_regs[6]; }; static unsigned int polls, fail_poll, reset_calls, reset_writes, core_writes; static unsigned int host_exits, clocks_disabled, callback_calls;