From 0e17166d377e74164e4067ae162ed19226352a97 Mon Sep 17 00:00:00 2001 From: Greg Kurz Date: Thu, 19 Jan 2017 11:50:10 +0100 Subject: cxl: Drop unused header asm/pnv-pci.h The kernel API does not use anything from this header file. Signed-off-by: Greg Kurz Reviewed-by: Andrew Donnellan Acked-by: Frederic Barrat Signed-off-by: Michael Ellerman --- drivers/misc/cxl/api.c | 1 - 1 file changed, 1 deletion(-) (limited to 'drivers') diff --git a/drivers/misc/cxl/api.c b/drivers/misc/cxl/api.c index 1b35e33d2434..bcc030eacab7 100644 --- a/drivers/misc/cxl/api.c +++ b/drivers/misc/cxl/api.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include -- cgit v1.2.3 From d7b1946c7925a270062b2e0718aa57b42ba619c0 Mon Sep 17 00:00:00 2001 From: Vaibhav Jain Date: Wed, 4 Jan 2017 11:48:52 +0530 Subject: cxl: Force psl data-cache flush during device shutdown This change adds a force psl data cache flush during device shutdown callback. This should reduce a possibility of psl holding a dirty cache line while the CAPP is being reinitialized, which may result in a UE [load/store] machine check error. Signed-off-by: Vaibhav Jain Reviewed-by: Andrew Donnellan Acked-by: Frederic Barrat Signed-off-by: Michael Ellerman --- drivers/misc/cxl/pci.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'drivers') diff --git a/drivers/misc/cxl/pci.c b/drivers/misc/cxl/pci.c index 80a87ab25b83..73432e7d925d 100644 --- a/drivers/misc/cxl/pci.c +++ b/drivers/misc/cxl/pci.c @@ -1610,6 +1610,9 @@ static void cxl_pci_remove_adapter(struct cxl *adapter) cxl_sysfs_adapter_remove(adapter); cxl_debugfs_adapter_remove(adapter); + /* Flush adapter datacache as its about to be removed */ + cxl_data_cache_flush(adapter); + cxl_deconfigure_adapter(adapter); device_unregister(&adapter->dev); -- cgit v1.2.3 From 14a3ae34bfd0bcb1cc12d55b06a8584c11fac6fc Mon Sep 17 00:00:00 2001 From: Andrew Donnellan Date: Fri, 9 Dec 2016 17:18:50 +1100 Subject: cxl: Prevent read/write to AFU config space while AFU not configured During EEH recovery, we deconfigure all AFUs whilst leaving the corresponding vPHB and virtual PCI device in place. If something attempts to interact with the AFU's PCI config space (e.g. running lspci) after the AFU has been deconfigured and before it's reconfigured, cxl_pcie_{read,write}_config() will read invalid values from the deconfigured struct cxl_afu and proceed to Oops when they try to dereference pointers that have been set to NULL during deconfiguration. Add a rwsem to struct cxl_afu so we can prevent interaction with config space while the AFU is deconfigured. Reported-by: Pradipta Ghosh Suggested-by: Frederic Barrat Cc: stable@vger.kernel.org # v4.9+ Signed-off-by: Andrew Donnellan Signed-off-by: Vaibhav Jain Signed-off-by: Michael Ellerman --- drivers/misc/cxl/cxl.h | 2 ++ drivers/misc/cxl/main.c | 3 ++- drivers/misc/cxl/pci.c | 2 ++ drivers/misc/cxl/vphb.c | 51 ++++++++++++++++++++++++++++--------------------- 4 files changed, 35 insertions(+), 23 deletions(-) (limited to 'drivers') diff --git a/drivers/misc/cxl/cxl.h b/drivers/misc/cxl/cxl.h index b24d76723fb0..b4a43fd14b99 100644 --- a/drivers/misc/cxl/cxl.h +++ b/drivers/misc/cxl/cxl.h @@ -418,6 +418,8 @@ struct cxl_afu { struct dentry *debugfs; struct mutex contexts_lock; spinlock_t afu_cntl_lock; + /* Used to block access to AFU config space while deconfigured */ + struct rw_semaphore configured_rwsem; /* AFU error buffer fields and bin attribute for sysfs */ u64 eb_len, eb_offset; diff --git a/drivers/misc/cxl/main.c b/drivers/misc/cxl/main.c index 62e0dfb5f15b..2a6bf1d0a3a4 100644 --- a/drivers/misc/cxl/main.c +++ b/drivers/misc/cxl/main.c @@ -268,7 +268,8 @@ struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice) idr_init(&afu->contexts_idr); mutex_init(&afu->contexts_lock); spin_lock_init(&afu->afu_cntl_lock); - + init_rwsem(&afu->configured_rwsem); + down_write(&afu->configured_rwsem); afu->prefault_mode = CXL_PREFAULT_NONE; afu->irqs_max = afu->adapter->user_irqs; diff --git a/drivers/misc/cxl/pci.c b/drivers/misc/cxl/pci.c index 73432e7d925d..cca938845ffd 100644 --- a/drivers/misc/cxl/pci.c +++ b/drivers/misc/cxl/pci.c @@ -1129,6 +1129,7 @@ static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pc if ((rc = cxl_native_register_psl_irq(afu))) goto err2; + up_write(&afu->configured_rwsem); return 0; err2: @@ -1141,6 +1142,7 @@ err1: static void pci_deconfigure_afu(struct cxl_afu *afu) { + down_write(&afu->configured_rwsem); cxl_native_release_psl_irq(afu); if (afu->adapter->native->sl_ops->release_serr_irq) afu->adapter->native->sl_ops->release_serr_irq(afu); diff --git a/drivers/misc/cxl/vphb.c b/drivers/misc/cxl/vphb.c index 3519acebfdab..639a343b7836 100644 --- a/drivers/misc/cxl/vphb.c +++ b/drivers/misc/cxl/vphb.c @@ -76,23 +76,22 @@ static int cxl_pcie_cfg_record(u8 bus, u8 devfn) return (bus << 8) + devfn; } -static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn, - struct cxl_afu **_afu, int *_record) +static inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus) { - struct pci_controller *phb; - struct cxl_afu *afu; - int record; + struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL; - phb = pci_bus_to_host(bus); - if (phb == NULL) - return PCIBIOS_DEVICE_NOT_FOUND; + return phb ? phb->private_data : NULL; +} + +static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn, + struct cxl_afu *afu, int *_record) +{ + int record; - afu = (struct cxl_afu *)phb->private_data; record = cxl_pcie_cfg_record(bus->number, devfn); if (record > afu->crs_num) return PCIBIOS_DEVICE_NOT_FOUND; - *_afu = afu; *_record = record; return 0; } @@ -106,9 +105,14 @@ static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn, u16 val16; u32 val32; - rc = cxl_pcie_config_info(bus, devfn, &afu, &record); + afu = pci_bus_to_afu(bus); + /* Grab a reader lock on afu. */ + if (afu == NULL || !down_read_trylock(&afu->configured_rwsem)) + return PCIBIOS_DEVICE_NOT_FOUND; + + rc = cxl_pcie_config_info(bus, devfn, afu, &record); if (rc) - return rc; + goto out; switch (len) { case 1: @@ -127,10 +131,9 @@ static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn, WARN_ON(1); } - if (rc) - return PCIBIOS_DEVICE_NOT_FOUND; - - return PCIBIOS_SUCCESSFUL; +out: + up_read(&afu->configured_rwsem); + return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL; } static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn, @@ -139,9 +142,14 @@ static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn, int rc, record; struct cxl_afu *afu; - rc = cxl_pcie_config_info(bus, devfn, &afu, &record); + afu = pci_bus_to_afu(bus); + /* Grab a reader lock on afu. */ + if (afu == NULL || !down_read_trylock(&afu->configured_rwsem)) + return PCIBIOS_DEVICE_NOT_FOUND; + + rc = cxl_pcie_config_info(bus, devfn, afu, &record); if (rc) - return rc; + goto out; switch (len) { case 1: @@ -157,10 +165,9 @@ static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn, WARN_ON(1); } - if (rc) - return PCIBIOS_SET_FAILED; - - return PCIBIOS_SUCCESSFUL; +out: + up_read(&afu->configured_rwsem); + return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL; } static struct pci_ops cxl_pcie_pci_ops = -- cgit v1.2.3 From 9e9fc6f00a54f7064dc681ac187be6498d566a4f Mon Sep 17 00:00:00 2001 From: "Gautham R. Shenoy" Date: Wed, 25 Jan 2017 14:06:27 +0530 Subject: cpuidle:powernv: Add helper function to populate powernv idle states. In the current code for powernv_add_idle_states, there is a lot of code duplication while initializing an idle state in powernv_states table. Add an inline helper function to populate the powernv_states[] table for a given idle state. Invoke this for populating the "Nap", "Fastsleep" and the stop states in powernv_add_idle_states. Signed-off-by: Gautham R. Shenoy Acked-by: Balbir Singh Acked-by: Rafael J. Wysocki Signed-off-by: Michael Ellerman --- drivers/cpuidle/cpuidle-powernv.c | 89 +++++++++++++++++++++++---------------- include/linux/cpuidle.h | 1 + 2 files changed, 54 insertions(+), 36 deletions(-) (limited to 'drivers') diff --git a/drivers/cpuidle/cpuidle-powernv.c b/drivers/cpuidle/cpuidle-powernv.c index 0835a37a5f3a..6871b7f34dc8 100644 --- a/drivers/cpuidle/cpuidle-powernv.c +++ b/drivers/cpuidle/cpuidle-powernv.c @@ -20,6 +20,10 @@ #include #include +/* + * Expose only those Hardware idle states via the cpuidle framework + * that have latency value below POWERNV_THRESHOLD_LATENCY_NS. + */ #define POWERNV_THRESHOLD_LATENCY_NS 200000 static struct cpuidle_driver powernv_idle_driver = { @@ -167,6 +171,24 @@ static int powernv_cpuidle_driver_init(void) return 0; } +static inline void add_powernv_state(int index, const char *name, + unsigned int flags, + int (*idle_fn)(struct cpuidle_device *, + struct cpuidle_driver *, + int), + unsigned int target_residency, + unsigned int exit_latency, + u64 psscr_val) +{ + strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN); + strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN); + powernv_states[index].flags = flags; + powernv_states[index].target_residency = target_residency; + powernv_states[index].exit_latency = exit_latency; + powernv_states[index].enter = idle_fn; + stop_psscr_table[index] = psscr_val; +} + static int powernv_add_idle_states(void) { struct device_node *power_mgt; @@ -236,6 +258,7 @@ static int powernv_add_idle_states(void) "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states); for (i = 0; i < dt_idle_states; i++) { + unsigned int exit_latency, target_residency; /* * If an idle state has exit latency beyond * POWERNV_THRESHOLD_LATENCY_NS then don't use it @@ -243,28 +266,33 @@ static int powernv_add_idle_states(void) */ if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS) continue; + /* + * Firmware passes residency and latency values in ns. + * cpuidle expects it in us. + */ + exit_latency = latency_ns[i] / 1000; + if (!rc) + target_residency = residency_ns[i] / 1000; + else + target_residency = 0; /* - * Cpuidle accepts exit_latency and target_residency in us. - * Use default target_residency values if f/w does not expose it. + * For nap and fastsleep, use default target_residency + * values if f/w does not expose it. */ if (flags[i] & OPAL_PM_NAP_ENABLED) { + if (!rc) + target_residency = 100; /* Add NAP state */ - strcpy(powernv_states[nr_idle_states].name, "Nap"); - strcpy(powernv_states[nr_idle_states].desc, "Nap"); - powernv_states[nr_idle_states].flags = 0; - powernv_states[nr_idle_states].target_residency = 100; - powernv_states[nr_idle_states].enter = nap_loop; + add_powernv_state(nr_idle_states, "Nap", + CPUIDLE_FLAG_NONE, nap_loop, + target_residency, exit_latency, 0); } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) && !(flags[i] & OPAL_PM_TIMEBASE_STOP)) { - strncpy(powernv_states[nr_idle_states].name, - names[i], CPUIDLE_NAME_LEN); - strncpy(powernv_states[nr_idle_states].desc, - names[i], CPUIDLE_NAME_LEN); - powernv_states[nr_idle_states].flags = 0; - - powernv_states[nr_idle_states].enter = stop_loop; - stop_psscr_table[nr_idle_states] = psscr_val[i]; + add_powernv_state(nr_idle_states, names[i], + CPUIDLE_FLAG_NONE, stop_loop, + target_residency, exit_latency, + psscr_val[i]); } /* @@ -274,32 +302,21 @@ static int powernv_add_idle_states(void) #ifdef CONFIG_TICK_ONESHOT if (flags[i] & OPAL_PM_SLEEP_ENABLED || flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) { + if (!rc) + target_residency = 300000; /* Add FASTSLEEP state */ - strcpy(powernv_states[nr_idle_states].name, "FastSleep"); - strcpy(powernv_states[nr_idle_states].desc, "FastSleep"); - powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP; - powernv_states[nr_idle_states].target_residency = 300000; - powernv_states[nr_idle_states].enter = fastsleep_loop; + add_powernv_state(nr_idle_states, "FastSleep", + CPUIDLE_FLAG_TIMER_STOP, + fastsleep_loop, + target_residency, exit_latency, 0); } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) && (flags[i] & OPAL_PM_TIMEBASE_STOP)) { - strncpy(powernv_states[nr_idle_states].name, - names[i], CPUIDLE_NAME_LEN); - strncpy(powernv_states[nr_idle_states].desc, - names[i], CPUIDLE_NAME_LEN); - - powernv_states[nr_idle_states].flags = CPUIDLE_FLAG_TIMER_STOP; - powernv_states[nr_idle_states].enter = stop_loop; - stop_psscr_table[nr_idle_states] = psscr_val[i]; + add_powernv_state(nr_idle_states, names[i], + CPUIDLE_FLAG_TIMER_STOP, stop_loop, + target_residency, exit_latency, + psscr_val[i]); } #endif - powernv_states[nr_idle_states].exit_latency = - ((unsigned int)latency_ns[i]) / 1000; - - if (!rc) { - powernv_states[nr_idle_states].target_residency = - ((unsigned int)residency_ns[i]) / 1000; - } - nr_idle_states++; } out: diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h index da346f2817a8..fc1e5d7fc1c7 100644 --- a/include/linux/cpuidle.h +++ b/include/linux/cpuidle.h @@ -62,6 +62,7 @@ struct cpuidle_state { }; /* Idle State Flags */ +#define CPUIDLE_FLAG_NONE (0x00) #define CPUIDLE_FLAG_COUPLED (0x02) /* state applies to multiple cpus */ #define CPUIDLE_FLAG_TIMER_STOP (0x04) /* timer is stopped on this state */ -- cgit v1.2.3 From 09206b600c76f20984e80d99f3b5343c79332a97 Mon Sep 17 00:00:00 2001 From: "Gautham R. Shenoy" Date: Wed, 25 Jan 2017 14:06:28 +0530 Subject: powernv: Pass PSSCR value and mask to power9_idle_stop The power9_idle_stop method currently takes only the requested stop level as a parameter and picks up the rest of the PSSCR bits from a hand-coded macro. This is not a very flexible design, especially when the firmware has the capability to communicate the psscr value and the mask associated with a particular stop state via device tree. This patch modifies the power9_idle_stop API to take as parameters the PSSCR value and the PSSCR mask corresponding to the stop state that needs to be set. These PSSCR value and mask are respectively obtained by parsing the "ibm,cpu-idle-state-psscr" and "ibm,cpu-idle-state-psscr-mask" fields from the device tree. In addition to this, the patch adds support for handling stop states for which ESL and EC bits in the PSSCR are zero. As per the architecture, a wakeup from these stop states resumes execution from the subsequent instruction as opposed to waking up at the System Vector. The older firmware sets only the Requested Level (RL) field in the psscr and psscr-mask exposed in the device tree. For older firmware where psscr-mask=0xf, this patch will set the default sane values that the set for for remaining PSSCR fields (i.e PSLL, MTL, ESL, EC, and TR). For the new firmware, the patch will validate that the invariants required by the ISA for the psscr values are maintained by the firmware. This skiboot patch that exports fully populated PSSCR values and the mask for all the stop states can be found here: https://lists.ozlabs.org/pipermail/skiboot/2016-September/004869.html [Optimize the number of instructions before entering STOP with ESL=EC=0, validate the PSSCR values provided by the firimware maintains the invariants required as per the ISA suggested by Balbir Singh] Acked-by: Balbir Singh Signed-off-by: Gautham R. Shenoy Signed-off-by: Michael Ellerman --- arch/powerpc/include/asm/cpuidle.h | 44 ++++++++++ arch/powerpc/include/asm/processor.h | 3 +- arch/powerpc/kernel/idle_book3s.S | 30 ++++--- arch/powerpc/platforms/powernv/idle.c | 138 ++++++++++++++++++++++++++++--- arch/powerpc/platforms/powernv/powernv.h | 3 +- arch/powerpc/platforms/powernv/smp.c | 14 ++-- drivers/cpuidle/cpuidle-powernv.c | 52 +++++++++--- 7 files changed, 241 insertions(+), 43 deletions(-) (limited to 'drivers') diff --git a/arch/powerpc/include/asm/cpuidle.h b/arch/powerpc/include/asm/cpuidle.h index 0a3255b12587..fd321eb423cb 100644 --- a/arch/powerpc/include/asm/cpuidle.h +++ b/arch/powerpc/include/asm/cpuidle.h @@ -10,11 +10,55 @@ #define PNV_CORE_IDLE_LOCK_BIT 0x100 #define PNV_CORE_IDLE_THREAD_BITS 0x0FF +/* + * ============================ NOTE ================================= + * The older firmware populates only the RL field in the psscr_val and + * sets the psscr_mask to 0xf. On such a firmware, the kernel sets the + * remaining PSSCR fields to default values as follows: + * + * - ESL and EC bits are to 1. So wakeup from any stop state will be + * at vector 0x100. + * + * - MTL and PSLL are set to the maximum allowed value as per the ISA, + * i.e. 15. + * + * - The Transition Rate, TR is set to the Maximum value 3. + */ +#define PSSCR_HV_DEFAULT_VAL (PSSCR_ESL | PSSCR_EC | \ + PSSCR_PSLL_MASK | PSSCR_TR_MASK | \ + PSSCR_MTL_MASK) + +#define PSSCR_HV_DEFAULT_MASK (PSSCR_ESL | PSSCR_EC | \ + PSSCR_PSLL_MASK | PSSCR_TR_MASK | \ + PSSCR_MTL_MASK | PSSCR_RL_MASK) +#define PSSCR_EC_SHIFT 20 +#define PSSCR_ESL_SHIFT 21 +#define GET_PSSCR_EC(x) (((x) & PSSCR_EC) >> PSSCR_EC_SHIFT) +#define GET_PSSCR_ESL(x) (((x) & PSSCR_ESL) >> PSSCR_ESL_SHIFT) +#define GET_PSSCR_RL(x) ((x) & PSSCR_RL_MASK) + +#define ERR_EC_ESL_MISMATCH -1 +#define ERR_DEEP_STATE_ESL_MISMATCH -2 + #ifndef __ASSEMBLY__ extern u32 pnv_fastsleep_workaround_at_entry[]; extern u32 pnv_fastsleep_workaround_at_exit[]; extern u64 pnv_first_deep_stop_state; + +int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags); +static inline void report_invalid_psscr_val(u64 psscr_val, int err) +{ + switch (err) { + case ERR_EC_ESL_MISMATCH: + pr_warn("Invalid psscr 0x%016llx : ESL,EC bits unequal", + psscr_val); + break; + case ERR_DEEP_STATE_ESL_MISMATCH: + pr_warn("Invalid psscr 0x%016llx : ESL cleared for deep stop-state", + psscr_val); + } +} #endif #endif diff --git a/arch/powerpc/include/asm/processor.h b/arch/powerpc/include/asm/processor.h index 1ba814436c73..21e0b52685b5 100644 --- a/arch/powerpc/include/asm/processor.h +++ b/arch/powerpc/include/asm/processor.h @@ -454,7 +454,8 @@ extern int powersave_nap; /* set if nap mode can be used in idle loop */ extern unsigned long power7_nap(int check_irq); extern unsigned long power7_sleep(void); extern unsigned long power7_winkle(void); -extern unsigned long power9_idle_stop(unsigned long stop_level); +extern unsigned long power9_idle_stop(unsigned long stop_psscr_val, + unsigned long stop_psscr_mask); extern void flush_instruction_cache(void); extern void hard_reset_now(void); diff --git a/arch/powerpc/kernel/idle_book3s.S b/arch/powerpc/kernel/idle_book3s.S index be90e2f62bba..4f6cf5596235 100644 --- a/arch/powerpc/kernel/idle_book3s.S +++ b/arch/powerpc/kernel/idle_book3s.S @@ -40,9 +40,7 @@ #define _WORC GPR11 #define _PTCR GPR12 -#define PSSCR_HV_TEMPLATE PSSCR_ESL | PSSCR_EC | \ - PSSCR_PSLL_MASK | PSSCR_TR_MASK | \ - PSSCR_MTL_MASK +#define PSSCR_EC_ESL_MASK_SHIFTED (PSSCR_EC | PSSCR_ESL) >> 16 .text @@ -264,7 +262,7 @@ enter_winkle: IDLE_STATE_ENTER_SEQ_NORET(PPC_WINKLE) /* - * r3 - requested stop state + * r3 - PSSCR value corresponding to the requested stop state. */ power_enter_stop: #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE @@ -273,10 +271,19 @@ power_enter_stop: /* DO THIS IN REAL MODE! See comment above. */ stb r4,HSTATE_HWTHREAD_STATE(r13) #endif +/* + * Check if we are executing the lite variant with ESL=EC=0 + */ + andis. r4,r3,PSSCR_EC_ESL_MASK_SHIFTED + clrldi r3,r3,60 /* r3 = Bits[60:63] = Requested Level (RL) */ + bne 1f + IDLE_STATE_ENTER_SEQ(PPC_STOP) + li r3,0 /* Since we didn't lose state, return 0 */ + b pnv_wakeup_noloss /* * Check if the requested state is a deep idle state. */ - LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state) +1: LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state) ld r4,ADDROFF(pnv_first_deep_stop_state)(r5) cmpd r3,r4 bge 2f @@ -353,16 +360,17 @@ ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66); \ ld r3,ORIG_GPR3(r1); /* Restore original r3 */ \ 20: nop; - /* - * r3 - requested stop state + * r3 - The PSSCR value corresponding to the stop state. + * r4 - The PSSCR mask corrresonding to the stop state. */ _GLOBAL(power9_idle_stop) - LOAD_REG_IMMEDIATE(r4, PSSCR_HV_TEMPLATE) - or r4,r4,r3 - mtspr SPRN_PSSCR, r4 - li r4, 1 + mfspr r5,SPRN_PSSCR + andc r5,r5,r4 + or r3,r3,r5 + mtspr SPRN_PSSCR,r3 LOAD_REG_ADDR(r5,power_enter_stop) + li r4,1 b pnv_powersave_common /* No return */ /* diff --git a/arch/powerpc/platforms/powernv/idle.c b/arch/powerpc/platforms/powernv/idle.c index 57bec031291b..4ee837e6391a 100644 --- a/arch/powerpc/platforms/powernv/idle.c +++ b/arch/powerpc/platforms/powernv/idle.c @@ -237,15 +237,21 @@ static DEVICE_ATTR(fastsleep_workaround_applyonce, 0600, show_fastsleep_workaround_applyonce, store_fastsleep_workaround_applyonce); +/* + * The default stop state that will be used by ppc_md.power_save + * function on platforms that support stop instruction. + */ +u64 pnv_default_stop_val; +u64 pnv_default_stop_mask; /* * Used for ppc_md.power_save which needs a function with no parameters */ static void power9_idle(void) { - /* Requesting stop state 0 */ - power9_idle_stop(0); + power9_idle_stop(pnv_default_stop_val, pnv_default_stop_mask); } + /* * First deep stop state. Used to figure out when to save/restore * hypervisor context. @@ -253,9 +259,11 @@ static void power9_idle(void) u64 pnv_first_deep_stop_state = MAX_STOP_STATE; /* - * Deepest stop idle state. Used when a cpu is offlined + * psscr value and mask of the deepest stop idle state. + * Used when a cpu is offlined. */ -u64 pnv_deepest_stop_state; +u64 pnv_deepest_stop_psscr_val; +u64 pnv_deepest_stop_psscr_mask; /* * Power ISA 3.0 idle initialization. @@ -292,6 +300,44 @@ u64 pnv_deepest_stop_state; * Bits 60:63 - Requested Level * Used to specify which power-saving level must be entered on executing * stop instruction + */ + +int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags) +{ + int err = 0; + + /* + * psscr_mask == 0xf indicates an older firmware. + * Set remaining fields of psscr to the default values. + * See NOTE above definition of PSSCR_HV_DEFAULT_VAL + */ + if (*psscr_mask == 0xf) { + *psscr_val = *psscr_val | PSSCR_HV_DEFAULT_VAL; + *psscr_mask = PSSCR_HV_DEFAULT_MASK; + return err; + } + + /* + * New firmware is expected to set the psscr_val bits correctly. + * Validate that the following invariants are correctly maintained by + * the new firmware. + * - ESL bit value matches the EC bit value. + * - ESL bit is set for all the deep stop states. + */ + if (GET_PSSCR_ESL(*psscr_val) != GET_PSSCR_EC(*psscr_val)) { + err = ERR_EC_ESL_MISMATCH; + } else if ((flags & OPAL_PM_LOSE_FULL_CONTEXT) && + GET_PSSCR_ESL(*psscr_val) == 0) { + err = ERR_DEEP_STATE_ESL_MISMATCH; + } + + return err; +} + +/* + * pnv_arch300_idle_init: Initializes the default idle state, first + * deep idle state and deepest idle state on + * ISA 3.0 CPUs. * * @np: /ibm,opal/power-mgt device node * @flags: cpu-idle-state-flags array @@ -302,43 +348,109 @@ static int __init pnv_power9_idle_init(struct device_node *np, u32 *flags, int dt_idle_states) { u64 *psscr_val = NULL; + u64 *psscr_mask = NULL; + u32 *residency_ns = NULL; + u64 max_residency_ns = 0; int rc = 0, i; + bool default_stop_found = false, deepest_stop_found = false; - psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val), - GFP_KERNEL); - if (!psscr_val) { + psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val), GFP_KERNEL); + psscr_mask = kcalloc(dt_idle_states, sizeof(*psscr_mask), GFP_KERNEL); + residency_ns = kcalloc(dt_idle_states, sizeof(*residency_ns), + GFP_KERNEL); + + if (!psscr_val || !psscr_mask || !residency_ns) { rc = -1; goto out; } + if (of_property_read_u64_array(np, "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) { - pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n"); + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n"); + rc = -1; + goto out; + } + + if (of_property_read_u64_array(np, + "ibm,cpu-idle-state-psscr-mask", + psscr_mask, dt_idle_states)) { + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr-mask in DT\n"); + rc = -1; + goto out; + } + + if (of_property_read_u32_array(np, + "ibm,cpu-idle-state-residency-ns", + residency_ns, dt_idle_states)) { + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-residency-ns in DT\n"); rc = -1; goto out; } /* - * Set pnv_first_deep_stop_state and pnv_deepest_stop_state. + * Set pnv_first_deep_stop_state, pnv_deepest_stop_psscr_{val,mask}, + * and the pnv_default_stop_{val,mask}. + * * pnv_first_deep_stop_state should be set to the first stop * level to cause hypervisor state loss. - * pnv_deepest_stop_state should be set to the deepest stop - * stop state. + * + * pnv_deepest_stop_{val,mask} should be set to values corresponding to + * the deepest stop state. + * + * pnv_default_stop_{val,mask} should be set to values corresponding to + * the shallowest (OPAL_PM_STOP_INST_FAST) loss-less stop state. */ pnv_first_deep_stop_state = MAX_STOP_STATE; for (i = 0; i < dt_idle_states; i++) { + int err; u64 psscr_rl = psscr_val[i] & PSSCR_RL_MASK; if ((flags[i] & OPAL_PM_LOSE_FULL_CONTEXT) && (pnv_first_deep_stop_state > psscr_rl)) pnv_first_deep_stop_state = psscr_rl; - if (pnv_deepest_stop_state < psscr_rl) - pnv_deepest_stop_state = psscr_rl; + err = validate_psscr_val_mask(&psscr_val[i], &psscr_mask[i], + flags[i]); + if (err) { + report_invalid_psscr_val(psscr_val[i], err); + continue; + } + + if (max_residency_ns < residency_ns[i]) { + max_residency_ns = residency_ns[i]; + pnv_deepest_stop_psscr_val = psscr_val[i]; + pnv_deepest_stop_psscr_mask = psscr_mask[i]; + deepest_stop_found = true; + } + + if (!default_stop_found && + (flags[i] & OPAL_PM_STOP_INST_FAST)) { + pnv_default_stop_val = psscr_val[i]; + pnv_default_stop_mask = psscr_mask[i]; + default_stop_found = true; + } + } + + if (!default_stop_found) { + pnv_default_stop_val = PSSCR_HV_DEFAULT_VAL; + pnv_default_stop_mask = PSSCR_HV_DEFAULT_MASK; + pr_warn("Setting default stop psscr val=0x%016llx,mask=0x%016llx\n", + pnv_default_stop_val, pnv_default_stop_mask); + } + + if (!deepest_stop_found) { + pnv_deepest_stop_psscr_val = PSSCR_HV_DEFAULT_VAL; + pnv_deepest_stop_psscr_mask = PSSCR_HV_DEFAULT_MASK; + pr_warn("Setting default stop psscr val=0x%016llx,mask=0x%016llx\n", + pnv_deepest_stop_psscr_val, + pnv_deepest_stop_psscr_mask); } out: kfree(psscr_val); + kfree(psscr_mask); + kfree(residency_ns); return rc; } diff --git a/arch/powerpc/platforms/powernv/powernv.h b/arch/powerpc/platforms/powernv/powernv.h index da7c843ac7f1..613052232475 100644 --- a/arch/powerpc/platforms/powernv/powernv.h +++ b/arch/powerpc/platforms/powernv/powernv.h @@ -18,7 +18,8 @@ static inline void pnv_pci_shutdown(void) { } #endif extern u32 pnv_get_supported_cpuidle_states(void); -extern u64 pnv_deepest_stop_state; +extern u64 pnv_deepest_stop_psscr_val; +extern u64 pnv_deepest_stop_psscr_mask; extern void pnv_lpc_init(void); diff --git a/arch/powerpc/platforms/powernv/smp.c b/arch/powerpc/platforms/powernv/smp.c index c789258ae1e1..1c6405fb769a 100644 --- a/arch/powerpc/platforms/powernv/smp.c +++ b/arch/powerpc/platforms/powernv/smp.c @@ -182,15 +182,17 @@ static void pnv_smp_cpu_kill_self(void) ppc64_runlatch_off(); - if (cpu_has_feature(CPU_FTR_ARCH_300)) - srr1 = power9_idle_stop(pnv_deepest_stop_state); - else if (idle_states & OPAL_PM_WINKLE_ENABLED) + if (cpu_has_feature(CPU_FTR_ARCH_300)) { + srr1 = power9_idle_stop(pnv_deepest_stop_psscr_val, + pnv_deepest_stop_psscr_mask); + } else if (idle_states & OPAL_PM_WINKLE_ENABLED) { srr1 = power7_winkle(); - else if ((idle_states & OPAL_PM_SLEEP_ENABLED) || - (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) + } else if ((idle_states & OPAL_PM_SLEEP_ENABLED) || + (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) { srr1 = power7_sleep(); - else + } else { srr1 = power7_nap(1); + } ppc64_runlatch_on(); diff --git a/drivers/cpuidle/cpuidle-powernv.c b/drivers/cpuidle/cpuidle-powernv.c index 6871b7f34dc8..370593006f5f 100644 --- a/drivers/cpuidle/cpuidle-powernv.c +++ b/drivers/cpuidle/cpuidle-powernv.c @@ -19,6 +19,7 @@ #include #include #include +#include /* * Expose only those Hardware idle states via the cpuidle framework @@ -34,7 +35,12 @@ static struct cpuidle_driver powernv_idle_driver = { static int max_idle_state; static struct cpuidle_state *cpuidle_state_table; -static u64 stop_psscr_table[CPUIDLE_STATE_MAX]; +struct stop_psscr_table { + u64 val; + u64 mask; +}; + +static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX]; static u64 snooze_timeout; static bool snooze_timeout_en; @@ -106,7 +112,8 @@ static int stop_loop(struct cpuidle_device *dev, int index) { ppc64_runlatch_off(); - power9_idle_stop(stop_psscr_table[index]); + power9_idle_stop(stop_psscr_table[index].val, + stop_psscr_table[index].mask); ppc64_runlatch_on(); return index; } @@ -178,7 +185,7 @@ static inline void add_powernv_state(int index, const char *name, int), unsigned int target_residency, unsigned int exit_latency, - u64 psscr_val) + u64 psscr_val, u64 psscr_mask) { strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN); strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN); @@ -186,7 +193,8 @@ static inline void add_powernv_state(int index, const char *name, powernv_states[index].target_residency = target_residency; powernv_states[index].exit_latency = exit_latency; powernv_states[index].enter = idle_fn; - stop_psscr_table[index] = psscr_val; + stop_psscr_table[index].val = psscr_val; + stop_psscr_table[index].mask = psscr_mask; } static int powernv_add_idle_states(void) @@ -198,7 +206,9 @@ static int powernv_add_idle_states(void) u32 residency_ns[CPUIDLE_STATE_MAX]; u32 flags[CPUIDLE_STATE_MAX]; u64 psscr_val[CPUIDLE_STATE_MAX]; + u64 psscr_mask[CPUIDLE_STATE_MAX]; const char *names[CPUIDLE_STATE_MAX]; + u32 has_stop_states = 0; int i, rc; /* Currently we have snooze statically defined */ @@ -245,15 +255,25 @@ static int powernv_add_idle_states(void) /* * If the idle states use stop instruction, probe for psscr values - * which are necessary to specify required stop level. + * and psscr mask which are necessary to specify required stop level. */ - if (flags[0] & (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP)) + has_stop_states = (flags[0] & + (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP)); + if (has_stop_states) { if (of_property_read_u64_array(power_mgt, "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) { - pr_warn("cpuidle-powernv: missing ibm,cpu-idle-states-psscr in DT\n"); + pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n"); goto out; } + if (of_property_read_u64_array(power_mgt, + "ibm,cpu-idle-state-psscr-mask", + psscr_mask, dt_idle_states)) { + pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n"); + goto out; + } + } + rc = of_property_read_u32_array(power_mgt, "ibm,cpu-idle-state-residency-ns", residency_ns, dt_idle_states); @@ -276,6 +296,16 @@ static int powernv_add_idle_states(void) else target_residency = 0; + if (has_stop_states) { + int err = validate_psscr_val_mask(&psscr_val[i], + &psscr_mask[i], + flags[i]); + if (err) { + report_invalid_psscr_val(psscr_val[i], err); + continue; + } + } + /* * For nap and fastsleep, use default target_residency * values if f/w does not expose it. @@ -286,13 +316,13 @@ static int powernv_add_idle_states(void) /* Add NAP state */ add_powernv_state(nr_idle_states, "Nap", CPUIDLE_FLAG_NONE, nap_loop, - target_residency, exit_latency, 0); + target_residency, exit_latency, 0, 0); } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) && !(flags[i] & OPAL_PM_TIMEBASE_STOP)) { add_powernv_state(nr_idle_states, names[i], CPUIDLE_FLAG_NONE, stop_loop, target_residency, exit_latency, - psscr_val[i]); + psscr_val[i], psscr_mask[i]); } /* @@ -308,13 +338,13 @@ static int powernv_add_idle_states(void) add_powernv_state(nr_idle_states, "FastSleep", CPUIDLE_FLAG_TIMER_STOP, fastsleep_loop, - target_residency, exit_latency, 0); + target_residency, exit_latency, 0, 0); } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) && (flags[i] & OPAL_PM_TIMEBASE_STOP)) { add_powernv_state(nr_idle_states, names[i], CPUIDLE_FLAG_TIMER_STOP, stop_loop, target_residency, exit_latency, - psscr_val[i]); + psscr_val[i], psscr_mask[i]); } #endif nr_idle_states++; -- cgit v1.2.3 From 39d40871526627fd0e2cfc1e2fb88500a5049c4c Mon Sep 17 00:00:00 2001 From: Andrew Donnellan Date: Wed, 1 Feb 2017 14:22:07 +1100 Subject: cxl: Fix build when CONFIG_DEBUG_FS=n Stub out the debugfs functions so that the build doesn't break when CONFIG_DEBUG_FS=n. Reported-by: Michael Ellerman Signed-off-by: Andrew Donnellan Acked-by: Ian Munsie Signed-off-by: Michael Ellerman --- drivers/misc/cxl/Makefile | 3 ++- drivers/misc/cxl/cxl.h | 59 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 5 deletions(-) (limited to 'drivers') diff --git a/drivers/misc/cxl/Makefile b/drivers/misc/cxl/Makefile index 56e9a4732ef0..c14fd6b65b5a 100644 --- a/drivers/misc/cxl/Makefile +++ b/drivers/misc/cxl/Makefile @@ -2,9 +2,10 @@ ccflags-y := $(call cc-disable-warning, unused-const-variable) ccflags-$(CONFIG_PPC_WERROR) += -Werror cxl-y += main.o file.o irq.o fault.o native.o -cxl-y += context.o sysfs.o debugfs.o pci.o trace.o +cxl-y += context.o sysfs.o pci.o trace.o cxl-y += vphb.o phb.o api.o cxl-$(CONFIG_PPC_PSERIES) += flash.o guest.o of.o hcalls.o +cxl-$(CONFIG_DEBUG_FS) += debugfs.o obj-$(CONFIG_CXL) += cxl.o obj-$(CONFIG_CXL_BASE) += base.o diff --git a/drivers/misc/cxl/cxl.h b/drivers/misc/cxl/cxl.h index b4a43fd14b99..6c722d96b775 100644 --- a/drivers/misc/cxl/cxl.h +++ b/drivers/misc/cxl/cxl.h @@ -802,12 +802,67 @@ int afu_register_irqs(struct cxl_context *ctx, u32 count); void afu_release_irqs(struct cxl_context *ctx, void *cookie); void afu_irq_name_free(struct cxl_context *ctx); +#ifdef CONFIG_DEBUG_FS + int cxl_debugfs_init(void); void cxl_debugfs_exit(void); int cxl_debugfs_adapter_add(struct cxl *adapter); void cxl_debugfs_adapter_remove(struct cxl *adapter); int cxl_debugfs_afu_add(struct cxl_afu *afu); void cxl_debugfs_afu_remove(struct cxl_afu *afu); +void cxl_stop_trace(struct cxl *cxl); +void cxl_debugfs_add_adapter_psl_regs(struct cxl *adapter, struct dentry *dir); +void cxl_debugfs_add_adapter_xsl_regs(struct cxl *adapter, struct dentry *dir); +void cxl_debugfs_add_afu_psl_regs(struct cxl_afu *afu, struct dentry *dir); + +#else /* CONFIG_DEBUG_FS */ + +static inline int __init cxl_debugfs_init(void) +{ + return 0; +} + +static inline void cxl_debugfs_exit(void) +{ +} + +static inline int cxl_debugfs_adapter_add(struct cxl *adapter) +{ + return 0; +} + +static inline void cxl_debugfs_adapter_remove(struct cxl *adapter) +{ +} + +static inline int cxl_debugfs_afu_add(struct cxl_afu *afu) +{ + return 0; +} + +static inline void cxl_debugfs_afu_remove(struct cxl_afu *afu) +{ +} + +static inline void cxl_stop_trace(struct cxl *cxl) +{ +} + +static inline void cxl_debugfs_add_adapter_psl_regs(struct cxl *adapter, + struct dentry *dir) +{ +} + +static inline void cxl_debugfs_add_adapter_xsl_regs(struct cxl *adapter, + struct dentry *dir) +{ +} + +static inline void cxl_debugfs_add_afu_psl_regs(struct cxl_afu *afu, struct dentry *dir) +{ +} + +#endif /* CONFIG_DEBUG_FS */ void cxl_handle_fault(struct work_struct *work); void cxl_prefault(struct cxl_context *ctx, u64 wed); @@ -872,12 +927,8 @@ int cxl_data_cache_flush(struct cxl *adapter); int cxl_afu_disable(struct cxl_afu *afu); int cxl_psl_purge(struct cxl_afu *afu); -void cxl_debugfs_add_adapter_psl_regs(struct cxl *adapter, struct dentry *dir); -void cxl_debugfs_add_adapter_xsl_regs(struct cxl *adapter, struct dentry *dir); -void cxl_debugfs_add_afu_psl_regs(struct cxl_afu *afu, struct dentry *dir); void cxl_native_psl_irq_dump_regs(struct cxl_context *ctx); void cxl_native_err_irq_dump_regs(struct cxl *adapter); -void cxl_stop_trace(struct cxl *cxl); int cxl_pci_vphb_add(struct cxl_afu *afu); void cxl_pci_vphb_remove(struct cxl_afu *afu); void cxl_release_mapping(struct cxl_context *ctx); -- cgit v1.2.3 From 523717d1496c794e8380d0e3de5ca6a8c1887dab Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Cleanup printk calls Add missing log message severity, remove old debug messages and replace printk() loop with print_hex_dump() call. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 2088e23a8002..06175126c986 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -221,7 +221,7 @@ static int __init via_cuda_start(void) return -EAGAIN; } - printk("Macintosh CUDA driver v0.5 for Unified ADB.\n"); + pr_info("Macintosh CUDA driver v0.5 for Unified ADB.\n"); cuda_fully_inited = 1; return 0; @@ -251,7 +251,7 @@ cuda_probe(void) int x; \ for (x = 1000; !(cond); --x) { \ if (x == 0) { \ - printk("Timeout waiting for " what "\n"); \ + pr_err("Timeout waiting for " what "\n"); \ return -ENXIO; \ } \ udelay(100); \ @@ -357,6 +357,7 @@ cuda_reset_adb_bus(void) return 0; } #endif /* CONFIG_ADB */ + /* Construct and send a cuda request */ int cuda_request(struct adb_request *req, void (*done)(struct adb_request *), @@ -474,12 +475,9 @@ cuda_interrupt(int irq, void *arg) } status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT); - /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */ switch (cuda_state) { case idle: /* CUDA has sent us the first byte of data - unsolicited */ - if (status != TREQ) - printk("cuda: state=idle, status=%x\n", status); (void)in_8(&via[SR]); out_8(&via[B], in_8(&via[B]) & ~TIP); cuda_state = reading; @@ -489,8 +487,6 @@ cuda_interrupt(int irq, void *arg) case awaiting_reply: /* CUDA has sent us the first byte of data of a reply */ - if (status != TREQ) - printk("cuda: state=awaiting_reply, status=%x\n", status); (void)in_8(&via[SR]); out_8(&via[B], in_8(&via[B]) & ~TIP); cuda_state = reading; @@ -506,9 +502,6 @@ cuda_interrupt(int irq, void *arg) out_8(&via[B], in_8(&via[B]) | TIP | TACK); cuda_state = idle; } else { - /* assert status == TIP + SR_OUT */ - if (status != TIP + SR_OUT) - printk("cuda: state=sent_first_byte status=%x\n", status); out_8(&via[SR], current_req->data[1]); out_8(&via[B], in_8(&via[B]) ^ TACK); data_index = 2; @@ -545,9 +538,6 @@ cuda_interrupt(int irq, void *arg) out_8(&via[B], in_8(&via[B]) | TACK | TIP); cuda_state = read_done; } else { - /* assert status == TIP | TREQ */ - if (status != TIP + TREQ) - printk("cuda: state=reading status=%x\n", status); out_8(&via[B], in_8(&via[B]) ^ TACK); } break; @@ -593,7 +583,7 @@ cuda_interrupt(int irq, void *arg) break; default: - printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); + pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); } spin_unlock(&cuda_lock); if (complete && req) { @@ -614,8 +604,6 @@ cuda_interrupt(int irq, void *arg) static void cuda_input(unsigned char *buf, int nb) { - int i; - switch (buf[0]) { case ADB_PACKET: #ifdef CONFIG_XMON @@ -633,9 +621,7 @@ cuda_input(unsigned char *buf, int nb) break; default: - printk("data from cuda (%d bytes):", nb); - for (i = 0; i < nb; ++i) - printk(" %.2x", buf[i]); - printk("\n"); + print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1, + buf, nb, false); } } -- cgit v1.2.3 From 06d7e99408acd2faa099d8af1a57cb1f6624062a Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Remove redundant temporary variable There is no possibility that current_req can change during execution of cuda_start(). This can be confirmed by inspection: cuda_lock is always held whenever cuda_start() is called or current_req is modified. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 06175126c986..dd51df5a6ec0 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -414,19 +414,15 @@ cuda_write(struct adb_request *req) static void cuda_start(void) { - struct adb_request *req; - /* assert cuda_state == idle */ - /* get the packet to send */ - req = current_req; - if (req == 0) + if (current_req == NULL) return; if ((in_8(&via[B]) & TREQ) == 0) return; /* a byte is coming in from the CUDA */ /* set the shift register to shift out and send a byte */ out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); - out_8(&via[SR], req->data[0]); + out_8(&via[SR], current_req->data[0]); out_8(&via[B], in_8(&via[B]) & ~TIP); cuda_state = sent_first_byte; } -- cgit v1.2.3 From fd7a65a27c6cb9b0920130d9402b95695168092d Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Add TREQ, TIP and TACK signal helpers Introduce some helpers for handling the signalling between VIA and Cuda. This abstraction will be used to add support for Egret devices, which utilize slightly different signalling. Don't invert the sense of the Cuda's active-low signals when storing them in the 'status' variable. Just assert, negate and test those signals using the helpers. The state machine does not need to test its own output signals to figure out what to do next: the next state depends on the Cuda's TREQ output. Just call the TREQ_asserted() helper function to test for that. Similarly, there is no need to store pin directions in the 'status' variable. That was only useful for debugging messages. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 76 ++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 23 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index dd51df5a6ec0..64a04af248a1 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -65,6 +65,36 @@ static DEFINE_SPINLOCK(cuda_lock); #define IER_CLR 0 /* clear bits in IER */ #define SR_INT 0x04 /* Shift register full/empty */ +static inline bool TREQ_asserted(u8 portb) +{ + return !(portb & TREQ); +} + +static inline void assert_TIP(void) +{ + out_8(&via[B], in_8(&via[B]) & ~TIP); +} + +static inline void assert_TACK(void) +{ + out_8(&via[B], in_8(&via[B]) & ~TACK); +} + +static inline void toggle_TACK(void) +{ + out_8(&via[B], in_8(&via[B]) ^ TACK); +} + +static inline void negate_TACK(void) +{ + out_8(&via[B], in_8(&via[B]) | TACK); +} + +static inline void negate_TIP_and_TACK(void) +{ + out_8(&via[B], in_8(&via[B]) | TIP | TACK); +} + static enum cuda_state { idle, sent_first_byte, @@ -262,7 +292,7 @@ static int __init cuda_init_via(void) { out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ - out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */ + negate_TIP_and_TACK(); out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ (void)in_8(&via[SR]); /* clear any left-over data */ #ifdef CONFIG_PPC @@ -278,10 +308,10 @@ __init cuda_init_via(void) out_8(&via[IFR], SR_INT); /* sync with the CUDA - assert TACK without TIP */ - out_8(&via[B], in_8(&via[B]) & ~TACK); + assert_TACK(); /* wait for the CUDA to assert TREQ in response */ - WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync"); + WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync"); /* wait for the interrupt and then clear it */ WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)"); @@ -289,14 +319,13 @@ __init cuda_init_via(void) out_8(&via[IFR], SR_INT); /* finish the sync by negating TACK */ - out_8(&via[B], in_8(&via[B]) | TACK); + negate_TACK(); /* wait for the CUDA to negate TREQ and the corresponding interrupt */ - WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)"); + WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)"); WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)"); (void)in_8(&via[SR]); out_8(&via[IFR], SR_INT); - out_8(&via[B], in_8(&via[B]) | TIP); /* should be unnecessary */ return 0; } @@ -417,13 +446,13 @@ cuda_start(void) /* assert cuda_state == idle */ if (current_req == NULL) return; - if ((in_8(&via[B]) & TREQ) == 0) + if (TREQ_asserted(in_8(&via[B]))) return; /* a byte is coming in from the CUDA */ /* set the shift register to shift out and send a byte */ out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); out_8(&via[SR], current_req->data[0]); - out_8(&via[B], in_8(&via[B]) & ~TIP); + assert_TIP(); cuda_state = sent_first_byte; } @@ -444,7 +473,7 @@ EXPORT_SYMBOL(cuda_poll); static irqreturn_t cuda_interrupt(int irq, void *arg) { - int status; + u8 status; struct adb_request *req = NULL; unsigned char ibuf[16]; int ibuf_len = 0; @@ -469,13 +498,14 @@ cuda_interrupt(int irq, void *arg) out_8(&via[IFR], SR_INT); } } - - status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT); + + status = in_8(&via[B]) & (TIP | TACK | TREQ); + switch (cuda_state) { case idle: /* CUDA has sent us the first byte of data - unsolicited */ (void)in_8(&via[SR]); - out_8(&via[B], in_8(&via[B]) & ~TIP); + assert_TIP(); cuda_state = reading; reply_ptr = cuda_rbuf; reading_reply = 0; @@ -484,22 +514,22 @@ cuda_interrupt(int irq, void *arg) case awaiting_reply: /* CUDA has sent us the first byte of data of a reply */ (void)in_8(&via[SR]); - out_8(&via[B], in_8(&via[B]) & ~TIP); + assert_TIP(); cuda_state = reading; reply_ptr = current_req->reply; reading_reply = 1; break; case sent_first_byte: - if (status == TREQ + TIP + SR_OUT) { + if (TREQ_asserted(status)) { /* collision */ out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); (void)in_8(&via[SR]); - out_8(&via[B], in_8(&via[B]) | TIP | TACK); + negate_TIP_and_TACK(); cuda_state = idle; } else { out_8(&via[SR], current_req->data[1]); - out_8(&via[B], in_8(&via[B]) ^ TACK); + toggle_TACK(); data_index = 2; cuda_state = sending; } @@ -510,7 +540,7 @@ cuda_interrupt(int irq, void *arg) if (data_index >= req->nbytes) { out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); (void)in_8(&via[SR]); - out_8(&via[B], in_8(&via[B]) | TACK | TIP); + negate_TIP_and_TACK(); req->sent = 1; if (req->reply_expected) { cuda_state = awaiting_reply; @@ -523,18 +553,18 @@ cuda_interrupt(int irq, void *arg) } } else { out_8(&via[SR], req->data[data_index++]); - out_8(&via[B], in_8(&via[B]) ^ TACK); + toggle_TACK(); } break; case reading: *reply_ptr++ = in_8(&via[SR]); - if (status == TIP) { + if (!TREQ_asserted(status)) { /* that's all folks */ - out_8(&via[B], in_8(&via[B]) | TACK | TIP); + negate_TIP_and_TACK(); cuda_state = read_done; } else { - out_8(&via[B], in_8(&via[B]) ^ TACK); + toggle_TACK(); } break; @@ -567,8 +597,8 @@ cuda_interrupt(int irq, void *arg) ibuf_len = reply_ptr - cuda_rbuf; memcpy(ibuf, cuda_rbuf, ibuf_len); } - if (status == TREQ) { - out_8(&via[B], in_8(&via[B]) & ~TIP); + if (TREQ_asserted(status)) { + assert_TIP(); cuda_state = reading; reply_ptr = cuda_rbuf; reading_reply = 0; -- cgit v1.2.3 From fe73b582f179354e233e5deddbd274efe8d3bbb9 Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Prevent read buffer overflow If the Cuda driver does not enter the 'read_done' state for some reason, it may continue in the 'reading' state until the buffer overflows. Add a bounds check to prevent this. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 64a04af248a1..1cf1467cf6e5 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -470,6 +470,8 @@ cuda_poll(void) } EXPORT_SYMBOL(cuda_poll); +#define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a)) + static irqreturn_t cuda_interrupt(int irq, void *arg) { @@ -558,7 +560,11 @@ cuda_interrupt(int irq, void *arg) break; case reading: - *reply_ptr++ = in_8(&via[SR]); + if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr) + : ARRAY_FULL(cuda_rbuf, reply_ptr)) + (void)in_8(&via[SR]); + else + *reply_ptr++ = in_8(&via[SR]); if (!TREQ_asserted(status)) { /* that's all folks */ negate_TIP_and_TACK(); -- cgit v1.2.3 From cfbf99801bcaf8398492ebc16af72259ad7aa146 Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Fix re-initialization of reply_ptr and reading_reply When reading_reply is set, reply_ptr points into an adb_request struct. Conversely, when reply_ptr instead points into the global cuda_rbuf, reading_reply must be false. Unfortunately, this rule can be violated because re-initialization of reply_ptr and reading_reply presently depends on the TREQ input. Fix this by re-initializing reply_ptr and reading_reply as soon as they are known to be invalid. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 1cf1467cf6e5..ae3da6b95229 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -592,6 +592,7 @@ cuda_interrupt(int irq, void *arg) } current_req = req->next; complete = 1; + reading_reply = 0; } else { /* This is tricky. We must break the spinlock to call * cuda_input. However, doing so means we might get @@ -603,11 +604,10 @@ cuda_interrupt(int irq, void *arg) ibuf_len = reply_ptr - cuda_rbuf; memcpy(ibuf, cuda_rbuf, ibuf_len); } + reply_ptr = cuda_rbuf; if (TREQ_asserted(status)) { assert_TIP(); cuda_state = reading; - reply_ptr = cuda_rbuf; - reading_reply = 0; } else { cuda_state = idle; cuda_start(); -- cgit v1.2.3 From a64662432200f8af6f67cd3664885a323f6a2f2d Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Avoid TREQ race condition When a read transaction completes, one of several things will happen: a new transfer is started by the driver, a new transfer request is raised by the Cuda (i.e. TREQ asserted), or both happen at once. When both happen at once, there is a race condition between the TREQ test in the read_done state and the same test in cuda_start(). Moreover, the former test uses a stale TREQ value. Theoretically, this can result in the undesirable outcome that the interrupt handler completes with the state machine 'idle' when it should instead start the next transaction. Avoid this race by calling cuda_start() first and then confirming that it succeeded. If not, test the current TREQ value before entering the 'reading' state. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index ae3da6b95229..32126958ac66 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -605,12 +605,11 @@ cuda_interrupt(int irq, void *arg) memcpy(ibuf, cuda_rbuf, ibuf_len); } reply_ptr = cuda_rbuf; - if (TREQ_asserted(status)) { + cuda_state = idle; + cuda_start(); + if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) { assert_TIP(); cuda_state = reading; - } else { - cuda_state = idle; - cuda_start(); } break; -- cgit v1.2.3 From ac39452e942af6a212e8f89e8a36b71354323845 Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Use spinlock_irq_save/restore instead of enable/disable_irq The cuda_start() function uses spinlock_irq_save/restore for mutual exclusion. Let's have cuda_poll() do the same when polling the VIA interrupt. The benefit to disabling local irqs when the interrupt is being polled is that the interrupt handler now has the same timing properties regardless of whether it is invoked normally or from cuda_poll(). This driver was written back when local irqs remained enabled during execution of interrupt handlers and cuda_poll() was probably trying to achieve the same effect by use of enable/disable_irq. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 32126958ac66..e3763cb4184b 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -459,14 +459,7 @@ cuda_start(void) void cuda_poll(void) { - /* cuda_interrupt only takes a normal lock, we disable - * interrupts here to avoid re-entering and thus deadlocking. - */ - if (cuda_irq) - disable_irq(cuda_irq); - cuda_interrupt(0, NULL); - if (cuda_irq) - enable_irq(cuda_irq); + cuda_interrupt(0, NULL); } EXPORT_SYMBOL(cuda_poll); @@ -475,13 +468,14 @@ EXPORT_SYMBOL(cuda_poll); static irqreturn_t cuda_interrupt(int irq, void *arg) { + unsigned long flags; u8 status; struct adb_request *req = NULL; unsigned char ibuf[16]; int ibuf_len = 0; int complete = 0; - spin_lock(&cuda_lock); + spin_lock_irqsave(&cuda_lock, flags); /* On powermacs, this handler is registered for the VIA IRQ. But they use * just the shift register IRQ -- other VIA interrupt sources are disabled. @@ -494,7 +488,7 @@ cuda_interrupt(int irq, void *arg) #endif { if ((in_8(&via[IFR]) & SR_INT) == 0) { - spin_unlock(&cuda_lock); + spin_unlock_irqrestore(&cuda_lock, flags); return IRQ_NONE; } else { out_8(&via[IFR], SR_INT); @@ -616,7 +610,7 @@ cuda_interrupt(int irq, void *arg) default: pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); } - spin_unlock(&cuda_lock); + spin_unlock_irqrestore(&cuda_lock, flags); if (complete && req) { void (*done)(struct adb_request *) = req->done; mb(); -- cgit v1.2.3 From 97ced1aac07e7b5348a560512b287af69f863917 Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Initialize data_index early and increment consistently Initialize data_index where appropriate to improve readability and assist debugging. This change doesn't affect driver behaviour. I prefer to see current_req->data[data_index++] in place of current_req->data[0] or current_req->data[1] inasmuchas it becomes obvious what the data_index variable does. Moreover, the actual value of data_index when examined at any given moment tells me something about prior events, which did prove helpful. Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index e3763cb4184b..57fb20dcb9dd 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -446,12 +446,13 @@ cuda_start(void) /* assert cuda_state == idle */ if (current_req == NULL) return; + data_index = 0; if (TREQ_asserted(in_8(&via[B]))) return; /* a byte is coming in from the CUDA */ /* set the shift register to shift out and send a byte */ out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); - out_8(&via[SR], current_req->data[0]); + out_8(&via[SR], current_req->data[data_index++]); assert_TIP(); cuda_state = sent_first_byte; } @@ -524,9 +525,8 @@ cuda_interrupt(int irq, void *arg) negate_TIP_and_TACK(); cuda_state = idle; } else { - out_8(&via[SR], current_req->data[1]); + out_8(&via[SR], current_req->data[data_index++]); toggle_TACK(); - data_index = 2; cuda_state = sending; } break; -- cgit v1.2.3 From d23eee88b56921a0bccd3b2355fc6feb4b5d343b Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: via-cuda: Add support for Egret system controller The Egret system controller was the predecessor to the Cuda and the differences are minor. On Cuda, byte acknowledgement requires one transition of the TACK signal; on Egret two are needed. On Cuda, TIP is active low; on Egret it is active high. And Cuda raises certain interrupts that Egret omits. Accomodating these differences complicates the Cuda driver slightly but avoids a lot of duplication (see next patch). Tested-by: Stan Johnson Signed-off-by: Finn Thain Signed-off-by: Michael Ellerman --- drivers/macintosh/via-cuda.c | 155 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 134 insertions(+), 21 deletions(-) (limited to 'drivers') diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 57fb20dcb9dd..1a742bd9f612 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -1,10 +1,10 @@ /* - * Device driver for the via-cuda on Apple Powermacs. + * Device driver for the Cuda and Egret system controllers found on PowerMacs + * and 68k Macs. * - * The VIA (versatile interface adapter) interfaces to the CUDA, - * a 6805 microprocessor core which controls the ADB (Apple Desktop - * Bus) which connects to the keyboard and mouse. The CUDA also - * controls system power and the RTC (real time clock) chip. + * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA. + * This MCU controls system power, Parameter RAM, Real Time Clock and the + * Apple Desktop Bus (ADB) that connects to the keyboard and mouse. * * Copyright (C) 1996 Paul Mackerras. */ @@ -50,10 +50,27 @@ static DEFINE_SPINLOCK(cuda_lock); #define IER (14*RS) /* Interrupt enable register */ #define ANH (15*RS) /* A-side data, no handshake */ -/* Bits in B data register: all active low */ -#define TREQ 0x08 /* Transfer request (input) */ -#define TACK 0x10 /* Transfer acknowledge (output) */ -#define TIP 0x20 /* Transfer in progress (output) */ +/* + * When the Cuda design replaced the Egret, some signal names and + * logic sense changed. They all serve the same purposes, however. + * + * VIA pin | Egret pin + * ----------------+------------------------------------------ + * PB3 (input) | Transceiver session (active low) + * PB4 (output) | VIA full (active high) + * PB5 (output) | System session (active high) + * + * VIA pin | Cuda pin + * ----------------+------------------------------------------ + * PB3 (input) | Transfer request (active low) + * PB4 (output) | Byte acknowledge (active low) + * PB5 (output) | Transfer in progress (active low) + */ + +/* Bits in Port B data register */ +#define TREQ 0x08 /* Transfer request */ +#define TACK 0x10 /* Transfer acknowledge */ +#define TIP 0x20 /* Transfer in progress */ /* Bits in ACR */ #define SR_CTRL 0x1c /* Shift register control bits */ @@ -65,6 +82,19 @@ static DEFINE_SPINLOCK(cuda_lock); #define IER_CLR 0 /* clear bits in IER */ #define SR_INT 0x04 /* Shift register full/empty */ +/* Duration of byte acknowledgement pulse (us) */ +#define EGRET_TACK_ASSERTED_DELAY 300 +#define EGRET_TACK_NEGATED_DELAY 400 + +/* Interval from interrupt to start of session (us) */ +#define EGRET_SESSION_DELAY 450 + +#ifdef CONFIG_PPC +#define mcu_is_egret false +#else +static bool mcu_is_egret; +#endif + static inline bool TREQ_asserted(u8 portb) { return !(portb & TREQ); @@ -72,12 +102,29 @@ static inline bool TREQ_asserted(u8 portb) static inline void assert_TIP(void) { - out_8(&via[B], in_8(&via[B]) & ~TIP); + if (mcu_is_egret) { + udelay(EGRET_SESSION_DELAY); + out_8(&via[B], in_8(&via[B]) | TIP); + } else + out_8(&via[B], in_8(&via[B]) & ~TIP); +} + +static inline void assert_TIP_and_TACK(void) +{ + if (mcu_is_egret) { + udelay(EGRET_SESSION_DELAY); + out_8(&via[B], in_8(&via[B]) | TIP | TACK); + } else + out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK)); } static inline void assert_TACK(void) { - out_8(&via[B], in_8(&via[B]) & ~TACK); + if (mcu_is_egret) { + udelay(EGRET_TACK_NEGATED_DELAY); + out_8(&via[B], in_8(&via[B]) | TACK); + } else + out_8(&via[B], in_8(&via[B]) & ~TACK); } static inline void toggle_TACK(void) @@ -87,12 +134,20 @@ static inline void toggle_TACK(void) static inline void negate_TACK(void) { - out_8(&via[B], in_8(&via[B]) | TACK); + if (mcu_is_egret) { + udelay(EGRET_TACK_ASSERTED_DELAY); + out_8(&via[B], in_8(&via[B]) & ~TACK); + } else + out_8(&via[B], in_8(&via[B]) | TACK); } static inline void negate_TIP_and_TACK(void) { - out_8(&via[B], in_8(&via[B]) | TIP | TACK); + if (mcu_is_egret) { + udelay(EGRET_TACK_ASSERTED_DELAY); + out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK)); + } else + out_8(&via[B], in_8(&via[B]) | TIP | TACK); } static enum cuda_state { @@ -155,6 +210,7 @@ int __init find_via_cuda(void) via = via1; cuda_state = idle; + mcu_is_egret = false; err = cuda_init_via(); if (err) { @@ -251,7 +307,7 @@ static int __init via_cuda_start(void) return -EAGAIN; } - pr_info("Macintosh CUDA driver v0.5 for Unified ADB.\n"); + pr_info("Macintosh Cuda and Egret driver.\n"); cuda_fully_inited = 1; return 0; @@ -276,6 +332,33 @@ cuda_probe(void) } #endif /* CONFIG_ADB */ +static int __init sync_egret(void) +{ + if (TREQ_asserted(in_8(&via[B]))) { + /* Complete the inbound transfer */ + assert_TIP_and_TACK(); + while (1) { + negate_TACK(); + mdelay(1); + (void)in_8(&via[SR]); + assert_TACK(); + if (!TREQ_asserted(in_8(&via[B]))) + break; + } + negate_TIP_and_TACK(); + } else if (in_8(&via[B]) & TIP) { + /* Terminate the outbound transfer */ + negate_TACK(); + assert_TACK(); + mdelay(1); + negate_TIP_and_TACK(); + } + /* Clear shift register interrupt */ + if (in_8(&via[IFR]) & SR_INT) + (void)in_8(&via[SR]); + return 0; +} + #define WAIT_FOR(cond, what) \ do { \ int x; \ @@ -291,10 +374,6 @@ cuda_probe(void) static int __init cuda_init_via(void) { - out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ - negate_TIP_and_TACK(); - out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ - (void)in_8(&via[SR]); /* clear any left-over data */ #ifdef CONFIG_PPC out_8(&via[IER], 0x7f); /* disable interrupts from VIA */ (void)in_8(&via[IER]); @@ -302,6 +381,15 @@ __init cuda_init_via(void) out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */ #endif + out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ + out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ + (void)in_8(&via[SR]); /* clear any left-over data */ + + if (mcu_is_egret) + return sync_egret(); + + negate_TIP_and_TACK(); + /* delay 4ms and then clear any pending interrupt */ mdelay(4); (void)in_8(&via[SR]); @@ -453,7 +541,10 @@ cuda_start(void) /* set the shift register to shift out and send a byte */ out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); out_8(&via[SR], current_req->data[data_index++]); - assert_TIP(); + if (mcu_is_egret) + assert_TIP_and_TACK(); + else + assert_TIP(); cuda_state = sent_first_byte; } @@ -500,8 +591,9 @@ cuda_interrupt(int irq, void *arg) switch (cuda_state) { case idle: - /* CUDA has sent us the first byte of data - unsolicited */ + /* System controller has unsolicited data for us */ (void)in_8(&via[SR]); +idle_state: assert_TIP(); cuda_state = reading; reply_ptr = cuda_rbuf; @@ -509,7 +601,7 @@ cuda_interrupt(int irq, void *arg) break; case awaiting_reply: - /* CUDA has sent us the first byte of data of a reply */ + /* System controller has reply data for us */ (void)in_8(&via[SR]); assert_TIP(); cuda_state = reading; @@ -524,9 +616,14 @@ cuda_interrupt(int irq, void *arg) (void)in_8(&via[SR]); negate_TIP_and_TACK(); cuda_state = idle; + /* Egret does not raise an "aborted" interrupt */ + if (mcu_is_egret) + goto idle_state; } else { out_8(&via[SR], current_req->data[data_index++]); toggle_TACK(); + if (mcu_is_egret) + assert_TACK(); cuda_state = sending; } break; @@ -550,6 +647,8 @@ cuda_interrupt(int irq, void *arg) } else { out_8(&via[SR], req->data[data_index++]); toggle_TACK(); + if (mcu_is_egret) + assert_TACK(); } break; @@ -560,16 +659,24 @@ cuda_interrupt(int irq, void *arg) else *reply_ptr++ = in_8(&via[SR]); if (!TREQ_asserted(status)) { + if (mcu_is_egret) + assert_TACK(); /* that's all folks */ negate_TIP_and_TACK(); cuda_state = read_done; + /* Egret does not raise a "read done" interrupt */ + if (mcu_is_egret) + goto read_done_state; } else { toggle_TACK(); + if (mcu_is_egret) + negate_TACK(); } break; case read_done: (void)in_8(&via[SR]); +read_done_state: if (reading_reply) { req = current_req; req->reply_len = reply_ptr - req->reply; @@ -645,6 +752,12 @@ cuda_input(unsigned char *buf, int nb) #endif /* CONFIG_ADB */ break; + case TIMER_PACKET: + /* Egret sends these periodically. Might be useful as a 'heartbeat' + * to trigger a recovery for the VIA shift register errata. + */ + break; + default: print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1, buf, nb, false); -- cgit v1.2.3 From f74faec6b3af9d88943a33ccd08de63b0dab8bc7 Mon Sep 17 00:00:00 2001 From: Finn Thain Date: Sat, 31 Dec 2016 19:56:26 -0500 Subject: m68k/mac: Replace via-maciisi driver with via-cuda driver Change the device probe test in the via-cuda.c driver so it will load on Egret-based machines too. Remove the now redundant via-maciisi.c driver. Tested-by: Stan Johnson Signed-off-by: Finn Thain Acked-by: Geert Uytterhoeven Signed-off-by: Michael Ellerman --- arch/m68k/include/asm/macintosh.h | 2 +- arch/m68k/mac/config.c | 18 +- arch/m68k/mac/misc.c | 72 +--- drivers/macintosh/Kconfig | 24 +- drivers/macintosh/Makefile | 1 - drivers/macintosh/adb.c | 4 - drivers/macintosh/via-cuda.c | 8 +- drivers/macintosh/via-maciisi.c | 677 -------------------------------------- 8 files changed, 30 insertions(+), 776 deletions(-) delete mode 100644 drivers/macintosh/via-maciisi.c (limited to 'drivers') diff --git a/arch/m68k/include/asm/macintosh.h b/arch/m68k/include/asm/macintosh.h index 42235e7fbeed..5b81ab188aa5 100644 --- a/arch/m68k/include/asm/macintosh.h +++ b/arch/m68k/include/asm/macintosh.h @@ -38,7 +38,7 @@ struct mac_model #define MAC_ADB_NONE 0 #define MAC_ADB_II 1 -#define MAC_ADB_IISI 2 +#define MAC_ADB_EGRET 2 #define MAC_ADB_CUDA 3 #define MAC_ADB_PB1 4 #define MAC_ADB_PB2 5 diff --git a/arch/m68k/mac/config.c b/arch/m68k/mac/config.c index e46895316eb0..9dc65a4c28d2 100644 --- a/arch/m68k/mac/config.c +++ b/arch/m68k/mac/config.c @@ -286,7 +286,7 @@ static struct mac_model mac_data_table[] = { }, { .ident = MAC_MODEL_IISI, .name = "IIsi", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_OLD, .scc_type = MAC_SCC_II, @@ -295,7 +295,7 @@ static struct mac_model mac_data_table[] = { }, { .ident = MAC_MODEL_IIVI, .name = "IIvi", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -304,7 +304,7 @@ static struct mac_model mac_data_table[] = { }, { .ident = MAC_MODEL_IIVX, .name = "IIvx", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -319,7 +319,7 @@ static struct mac_model mac_data_table[] = { { .ident = MAC_MODEL_CLII, .name = "Classic II", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -352,7 +352,7 @@ static struct mac_model mac_data_table[] = { { .ident = MAC_MODEL_LC, .name = "LC", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -361,7 +361,7 @@ static struct mac_model mac_data_table[] = { }, { .ident = MAC_MODEL_LCII, .name = "LC II", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -370,7 +370,7 @@ static struct mac_model mac_data_table[] = { }, { .ident = MAC_MODEL_LCIII, .name = "LC III", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -498,7 +498,7 @@ static struct mac_model mac_data_table[] = { { .ident = MAC_MODEL_P460, .name = "Performa 460", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, @@ -575,7 +575,7 @@ static struct mac_model mac_data_table[] = { }, { .ident = MAC_MODEL_P600, .name = "Performa 600", - .adb_type = MAC_ADB_IISI, + .adb_type = MAC_ADB_EGRET, .via_type = MAC_VIA_IICI, .scsi_type = MAC_SCSI_LC, .scc_type = MAC_SCC_II, diff --git a/arch/m68k/mac/misc.c b/arch/m68k/mac/misc.c index c6d351f5bd79..f4bb73fcb67a 100644 --- a/arch/m68k/mac/misc.c +++ b/arch/m68k/mac/misc.c @@ -142,54 +142,6 @@ static void pmu_write_pram(int offset, __u8 data) #define pmu_write_pram NULL #endif -#if 0 /* def CONFIG_ADB_MACIISI */ -extern int maciisi_request(struct adb_request *req, - void (*done)(struct adb_request *), int nbytes, ...); - -static long maciisi_read_time(void) -{ - struct adb_request req; - long time; - - if (maciisi_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME)) - return 0; - - time = (req.reply[3] << 24) | (req.reply[4] << 16) - | (req.reply[5] << 8) | req.reply[6]; - return time - RTC_OFFSET; -} - -static void maciisi_write_time(long data) -{ - struct adb_request req; - data += RTC_OFFSET; - maciisi_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME, - (data >> 24) & 0xFF, (data >> 16) & 0xFF, - (data >> 8) & 0xFF, data & 0xFF); -} - -static __u8 maciisi_read_pram(int offset) -{ - struct adb_request req; - if (maciisi_request(&req, NULL, 4, CUDA_PACKET, CUDA_GET_PRAM, - (offset >> 8) & 0xFF, offset & 0xFF)) - return 0; - return req.reply[3]; -} - -static void maciisi_write_pram(int offset, __u8 data) -{ - struct adb_request req; - maciisi_request(&req, NULL, 5, CUDA_PACKET, CUDA_SET_PRAM, - (offset >> 8) & 0xFF, offset & 0xFF, data); -} -#else -#define maciisi_read_time() 0 -#define maciisi_write_time(n) -#define maciisi_read_pram NULL -#define maciisi_write_pram NULL -#endif - /* * VIA PRAM/RTC access routines * @@ -458,11 +410,10 @@ void mac_pram_read(int offset, __u8 *buffer, int len) int i; switch(macintosh_config->adb_type) { - case MAC_ADB_IISI: - func = maciisi_read_pram; break; case MAC_ADB_PB1: case MAC_ADB_PB2: func = pmu_read_pram; break; + case MAC_ADB_EGRET: case MAC_ADB_CUDA: func = cuda_read_pram; break; default: @@ -481,11 +432,10 @@ void mac_pram_write(int offset, __u8 *buffer, int len) int i; switch(macintosh_config->adb_type) { - case MAC_ADB_IISI: - func = maciisi_write_pram; break; case MAC_ADB_PB1: case MAC_ADB_PB2: func = pmu_write_pram; break; + case MAC_ADB_EGRET: case MAC_ADB_CUDA: func = cuda_write_pram; break; default: @@ -500,17 +450,13 @@ void mac_pram_write(int offset, __u8 *buffer, int len) void mac_poweroff(void) { - /* - * MAC_ADB_IISI may need to be moved up here if it doesn't actually - * work using the ADB packet method. --David Kilzer - */ - if (oss_present) { oss_shutdown(); } else if (macintosh_config->adb_type == MAC_ADB_II) { via_shutdown(); #ifdef CONFIG_ADB_CUDA - } else if (macintosh_config->adb_type == MAC_ADB_CUDA) { + } else if (macintosh_config->adb_type == MAC_ADB_EGRET || + macintosh_config->adb_type == MAC_ADB_CUDA) { cuda_shutdown(); #endif #ifdef CONFIG_ADB_PMU68K @@ -550,7 +496,8 @@ void mac_reset(void) local_irq_restore(flags); } #ifdef CONFIG_ADB_CUDA - } else if (macintosh_config->adb_type == MAC_ADB_CUDA) { + } else if (macintosh_config->adb_type == MAC_ADB_EGRET || + macintosh_config->adb_type == MAC_ADB_CUDA) { cuda_restart(); #endif #ifdef CONFIG_ADB_PMU68K @@ -699,13 +646,11 @@ int mac_hwclk(int op, struct rtc_time *t) case MAC_ADB_IOP: now = via_read_time(); break; - case MAC_ADB_IISI: - now = maciisi_read_time(); - break; case MAC_ADB_PB1: case MAC_ADB_PB2: now = pmu_read_time(); break; + case MAC_ADB_EGRET: case MAC_ADB_CUDA: now = cuda_read_time(); break; @@ -737,6 +682,7 @@ int mac_hwclk(int op, struct rtc_time *t) case MAC_ADB_IOP: via_write_time(now); break; + case MAC_ADB_EGRET: case MAC_ADB_CUDA: cuda_write_time(now); break; @@ -744,8 +690,6 @@ int mac_hwclk(int op, struct rtc_time *t) case MAC_ADB_PB2: pmu_write_time(now); break; - case MAC_ADB_IISI: - maciisi_write_time(now); } } return 0; diff --git a/drivers/macintosh/Kconfig b/drivers/macintosh/Kconfig index 5d80810934df..97a420c11eed 100644 --- a/drivers/macintosh/Kconfig +++ b/drivers/macintosh/Kconfig @@ -30,14 +30,6 @@ config ADB_MACII Quadra 610, Quadra 650, Quadra 700, Quadra 800, Centris 610 and Centris 650. -config ADB_MACIISI - bool "Include Mac IIsi ADB driver" - depends on ADB && MAC && BROKEN - help - Say Y here if want your kernel to support Macintosh systems that use - the Mac IIsi style ADB. This includes the IIsi, IIvi, IIvx, Classic - II, LC, LC II, LC III, Performa 460, and the Performa 600. - config ADB_IOP bool "Include IOP (IIfx/Quadra 9x0) ADB driver" depends on ADB && MAC @@ -60,17 +52,15 @@ config ADB_PMU68K # we want to change this to something like CONFIG_SYSCTRL_CUDA/PMU config ADB_CUDA - bool "Support for CUDA based Macs and PowerMacs" + bool "Support for Cuda/Egret based Macs and PowerMacs" depends on (ADB || PPC_PMAC) && !PPC_PMAC64 help - This provides support for CUDA based Macintosh and Power Macintosh - systems. This includes many m68k based Macs (Color Classic, Mac TV, - Performa 475, Performa 520, Performa 550, Performa 575, - Performa 588, Quadra 605, Quadra 630, Quadra/Centris 660AV, and - Quadra 840AV), most OldWorld PowerMacs, the first generation iMacs, - the Blue&White G3 and the "Yikes" G4 (PCI Graphics). All later - models should use CONFIG_ADB_PMU instead. It is safe to say Y here - even if your machine doesn't have a CUDA. + This provides support for Cuda/Egret based Macintosh and + Power Macintosh systems. This includes most m68k based Macs, + most Old World PowerMacs, the first generation iMacs, the + Blue & White G3 and the "Yikes" G4 (PCI Graphics). All later + models should use CONFIG_ADB_PMU instead. It is safe to say Y + here even if your machine doesn't have a Cuda or Egret device. If unsure say Y. diff --git a/drivers/macintosh/Makefile b/drivers/macintosh/Makefile index 383ba920085b..516eb65bcacc 100644 --- a/drivers/macintosh/Makefile +++ b/drivers/macintosh/Makefile @@ -20,7 +20,6 @@ obj-$(CONFIG_PMAC_SMU) += smu.o obj-$(CONFIG_ADB) += adb.o obj-$(CONFIG_ADB_MACII) += via-macii.o -obj-$(CONFIG_ADB_MACIISI) += via-maciisi.o obj-$(CONFIG_ADB_IOP) += adb-iop.o obj-$(CONFIG_ADB_PMU68K) += via-pmu68k.o obj-$(CONFIG_ADB_MACIO) += macio-adb.o diff --git a/drivers/macintosh/adb.c b/drivers/macintosh/adb.c index 226179b975a0..152414e6378a 100644 --- a/drivers/macintosh/adb.c +++ b/drivers/macintosh/adb.c @@ -48,7 +48,6 @@ EXPORT_SYMBOL(adb_client_list); extern struct adb_driver via_macii_driver; -extern struct adb_driver via_maciisi_driver; extern struct adb_driver via_cuda_driver; extern struct adb_driver adb_iop_driver; extern struct adb_driver via_pmu_driver; @@ -59,9 +58,6 @@ static struct adb_driver *adb_driver_list[] = { #ifdef CONFIG_ADB_MACII &via_macii_driver, #endif -#ifdef CONFIG_ADB_MACIISI - &via_maciisi_driver, -#endif #ifdef CONFIG_ADB_CUDA &via_cuda_driver, #endif diff --git a/drivers/macintosh/via-cuda.c b/drivers/macintosh/via-cuda.c index 1a742bd9f612..c60415958dfe 100644 --- a/drivers/macintosh/via-cuda.c +++ b/drivers/macintosh/via-cuda.c @@ -205,12 +205,13 @@ int __init find_via_cuda(void) struct adb_request req; int err; - if (macintosh_config->adb_type != MAC_ADB_CUDA) + if (macintosh_config->adb_type != MAC_ADB_CUDA && + macintosh_config->adb_type != MAC_ADB_EGRET) return 0; via = via1; cuda_state = idle; - mcu_is_egret = false; + mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET; err = cuda_init_via(); if (err) { @@ -323,7 +324,8 @@ cuda_probe(void) if (sys_ctrler != SYS_CTRLER_CUDA) return -ENODEV; #else - if (macintosh_config->adb_type != MAC_ADB_CUDA) + if (macintosh_config->adb_type != MAC_ADB_CUDA && + macintosh_config->adb_type != MAC_ADB_EGRET) return -ENODEV; #endif if (via == NULL) diff --git a/drivers/macintosh/via-maciisi.c b/drivers/macintosh/via-maciisi.c deleted file mode 100644 index 34d02a91b29f..000000000000 --- a/drivers/macintosh/via-maciisi.c +++ /dev/null @@ -1,677 +0,0 @@ -/* - * Device driver for the IIsi-style ADB on some Mac LC and II-class machines - * - * Based on via-cuda.c and via-macii.c, as well as the original - * adb-bus.c, which in turn is somewhat influenced by (but uses no - * code from) the NetBSD HWDIRECT ADB code. Original IIsi driver work - * was done by Robert Thompson and integrated into the old style - * driver by Michael Schmitz. - * - * Original sources (c) Alan Cox, Paul Mackerras, and others. - * - * Rewritten for Unified ADB by David Huggins-Daines - * - * 7/13/2000- extensive changes by Andrew McPherson - * Works about 30% of the time now. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static volatile unsigned char *via; - -/* VIA registers - spaced 0x200 bytes apart - only the ones we actually use */ -#define RS 0x200 /* skip between registers */ -#define B 0 /* B-side data */ -#define A RS /* A-side data */ -#define DIRB (2*RS) /* B-side direction (1=output) */ -#define DIRA (3*RS) /* A-side direction (1=output) */ -#define SR (10*RS) /* Shift register */ -#define ACR (11*RS) /* Auxiliary control register */ -#define IFR (13*RS) /* Interrupt flag register */ -#define IER (14*RS) /* Interrupt enable register */ - -/* Bits in B data register: all active low */ -#define TREQ 0x08 /* Transfer request (input) */ -#define TACK 0x10 /* Transfer acknowledge (output) */ -#define TIP 0x20 /* Transfer in progress (output) */ -#define ST_MASK 0x30 /* mask for selecting ADB state bits */ - -/* Bits in ACR */ -#define SR_CTRL 0x1c /* Shift register control bits */ -#define SR_EXT 0x0c /* Shift on external clock */ -#define SR_OUT 0x10 /* Shift out if 1 */ - -/* Bits in IFR and IER */ -#define IER_SET 0x80 /* set bits in IER */ -#define IER_CLR 0 /* clear bits in IER */ -#define SR_INT 0x04 /* Shift register full/empty */ -#define SR_DATA 0x08 /* Shift register data */ -#define SR_CLOCK 0x10 /* Shift register clock */ - -#define ADB_DELAY 150 - -#undef DEBUG_MACIISI_ADB - -static struct adb_request* current_req; -static struct adb_request* last_req; -static unsigned char maciisi_rbuf[16]; -static unsigned char *reply_ptr; -static int data_index; -static int reading_reply; -static int reply_len; -static int tmp; -static int need_sync; - -static enum maciisi_state { - idle, - sending, - reading, -} maciisi_state; - -static int maciisi_probe(void); -static int maciisi_init(void); -static int maciisi_send_request(struct adb_request* req, int sync); -static void maciisi_sync(struct adb_request *req); -static int maciisi_write(struct adb_request* req); -static irqreturn_t maciisi_interrupt(int irq, void* arg); -static void maciisi_input(unsigned char *buf, int nb); -static int maciisi_init_via(void); -static void maciisi_poll(void); -static int maciisi_start(void); - -struct adb_driver via_maciisi_driver = { - "Mac IIsi", - maciisi_probe, - maciisi_init, - maciisi_send_request, - NULL, /* maciisi_adb_autopoll, */ - maciisi_poll, - NULL /* maciisi_reset_adb_bus */ -}; - -static int -maciisi_probe(void) -{ - if (macintosh_config->adb_type != MAC_ADB_IISI) - return -ENODEV; - - via = via1; - return 0; -} - -static int -maciisi_init(void) -{ - int err; - - if (via == NULL) - return -ENODEV; - - if ((err = maciisi_init_via())) { - printk(KERN_ERR "maciisi_init: maciisi_init_via() failed, code %d\n", err); - via = NULL; - return err; - } - - if (request_irq(IRQ_MAC_ADB, maciisi_interrupt, 0, "ADB", - maciisi_interrupt)) { - printk(KERN_ERR "maciisi_init: can't get irq %d\n", IRQ_MAC_ADB); - return -EAGAIN; - } - - printk("adb: Mac IIsi driver v0.2 for Unified ADB.\n"); - return 0; -} - -/* Flush data from the ADB controller */ -static void -maciisi_stfu(void) -{ - int status = via[B] & (TIP|TREQ); - - if (status & TREQ) { -#ifdef DEBUG_MACIISI_ADB - printk (KERN_DEBUG "maciisi_stfu called with TREQ high!\n"); -#endif - return; - } - - udelay(ADB_DELAY); - via[ACR] &= ~SR_OUT; - via[IER] = IER_CLR | SR_INT; - - udelay(ADB_DELAY); - - status = via[B] & (TIP|TREQ); - - if (!(status & TREQ)) - { - via[B] |= TIP; - - while(1) - { - int poll_timeout = ADB_DELAY * 5; - /* Poll for SR interrupt */ - while (!(via[IFR] & SR_INT) && poll_timeout-- > 0) - status = via[B] & (TIP|TREQ); - - tmp = via[SR]; /* Clear shift register */ -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "maciisi_stfu: status %x timeout %d data %x\n", - status, poll_timeout, tmp); -#endif - if(via[B] & TREQ) - break; - - /* ACK on-off */ - via[B] |= TACK; - udelay(ADB_DELAY); - via[B] &= ~TACK; - } - - /* end frame */ - via[B] &= ~TIP; - udelay(ADB_DELAY); - } - - via[IER] = IER_SET | SR_INT; -} - -/* All specifically VIA-related initialization goes here */ -static int -maciisi_init_via(void) -{ - int i; - - /* Set the lines up. We want TREQ as input TACK|TIP as output */ - via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ; - /* Shift register on input */ - via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT; -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "maciisi_init_via: initial status %x\n", via[B] & (TIP|TREQ)); -#endif - /* Wipe any pending data and int */ - tmp = via[SR]; - /* Enable keyboard interrupts */ - via[IER] = IER_SET | SR_INT; - /* Set initial state: idle */ - via[B] &= ~(TACK|TIP); - /* Clear interrupt bit */ - via[IFR] = SR_INT; - - for(i = 0; i < 60; i++) { - udelay(ADB_DELAY); - maciisi_stfu(); - udelay(ADB_DELAY); - if(via[B] & TREQ) - break; - } - if (i == 60) - printk(KERN_ERR "maciisi_init_via: bus jam?\n"); - - maciisi_state = idle; - need_sync = 0; - - return 0; -} - -/* Send a request, possibly waiting for a reply */ -static int -maciisi_send_request(struct adb_request* req, int sync) -{ - int i; - -#ifdef DEBUG_MACIISI_ADB - static int dump_packet = 0; -#endif - - if (via == NULL) { - req->complete = 1; - return -ENXIO; - } - -#ifdef DEBUG_MACIISI_ADB - if (dump_packet) { - printk(KERN_DEBUG "maciisi_send_request:"); - for (i = 0; i < req->nbytes; i++) { - printk(" %.2x", req->data[i]); - } - printk(" sync %d\n", sync); - } -#endif - - req->reply_expected = 1; - - i = maciisi_write(req); - if (i) - { - /* Normally, if a packet requires syncing, that happens at the end of - * maciisi_send_request. But if the transfer fails, it will be restarted - * by maciisi_interrupt(). We use need_sync to tell maciisi_interrupt - * when to sync a packet that it sends out. - * - * Suggestions on a better way to do this are welcome. - */ - if(i == -EBUSY && sync) - need_sync = 1; - else - need_sync = 0; - return i; - } - if(sync) - maciisi_sync(req); - - return 0; -} - -/* Poll the ADB chip until the request completes */ -static void maciisi_sync(struct adb_request *req) -{ - int count = 0; - -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "maciisi_sync called\n"); -#endif - - /* If for some reason the ADB chip shuts up on us, we want to avoid an endless loop. */ - while (!req->complete && count++ < 50) { - maciisi_poll(); - } - /* This could be BAD... when the ADB controller doesn't respond - * for this long, it's probably not coming back :-( */ - if (count > 50) /* Hopefully shouldn't happen */ - printk(KERN_ERR "maciisi_send_request: poll timed out!\n"); -} - -int -maciisi_request(struct adb_request *req, void (*done)(struct adb_request *), - int nbytes, ...) -{ - va_list list; - int i; - - req->nbytes = nbytes; - req->done = done; - req->reply_expected = 0; - va_start(list, nbytes); - for (i = 0; i < nbytes; i++) - req->data[i++] = va_arg(list, int); - va_end(list); - - return maciisi_send_request(req, 1); -} - -/* Enqueue a request, and run the queue if possible */ -static int -maciisi_write(struct adb_request* req) -{ - unsigned long flags; - int i; - - /* We will accept CUDA packets - the VIA sends them to us, so - it figures that we should be able to send them to it */ - if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) { - printk(KERN_ERR "maciisi_write: packet too small or not an ADB or CUDA packet\n"); - req->complete = 1; - return -EINVAL; - } - req->next = NULL; - req->sent = 0; - req->complete = 0; - req->reply_len = 0; - - local_irq_save(flags); - - if (current_req) { - last_req->next = req; - last_req = req; - } else { - current_req = req; - last_req = req; - } - if (maciisi_state == idle) - { - i = maciisi_start(); - if(i != 0) - { - local_irq_restore(flags); - return i; - } - } - else - { -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "maciisi_write: would start, but state is %d\n", maciisi_state); -#endif - local_irq_restore(flags); - return -EBUSY; - } - - local_irq_restore(flags); - - return 0; -} - -static int -maciisi_start(void) -{ - struct adb_request* req; - int status; - -#ifdef DEBUG_MACIISI_ADB - status = via[B] & (TIP | TREQ); - - printk(KERN_DEBUG "maciisi_start called, state=%d, status=%x, ifr=%x\n", maciisi_state, status, via[IFR]); -#endif - - if (maciisi_state != idle) { - /* shouldn't happen */ - printk(KERN_ERR "maciisi_start: maciisi_start called when driver busy!\n"); - return -EBUSY; - } - - req = current_req; - if (req == NULL) - return -EINVAL; - - status = via[B] & (TIP|TREQ); - if (!(status & TREQ)) { -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "maciisi_start: bus busy - aborting\n"); -#endif - return -EBUSY; - } - - /* Okay, send */ -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "maciisi_start: sending\n"); -#endif - /* Set state to active */ - via[B] |= TIP; - /* ACK off */ - via[B] &= ~TACK; - /* Delay */ - udelay(ADB_DELAY); - /* Shift out and send */ - via[ACR] |= SR_OUT; - via[SR] = req->data[0]; - data_index = 1; - /* ACK on */ - via[B] |= TACK; - maciisi_state = sending; - - return 0; -} - -void -maciisi_poll(void) -{ - unsigned long flags; - - local_irq_save(flags); - if (via[IFR] & SR_INT) { - maciisi_interrupt(0, NULL); - } - else /* avoid calling this function too quickly in a loop */ - udelay(ADB_DELAY); - - local_irq_restore(flags); -} - -/* Shift register interrupt - this is *supposed* to mean that the - register is either full or empty. In practice, I have no idea what - it means :( */ -static irqreturn_t -maciisi_interrupt(int irq, void* arg) -{ - int status; - struct adb_request *req; -#ifdef DEBUG_MACIISI_ADB - static int dump_reply = 0; -#endif - int i; - unsigned long flags; - - local_irq_save(flags); - - status = via[B] & (TIP|TREQ); -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "state %d status %x ifr %x\n", maciisi_state, status, via[IFR]); -#endif - - if (!(via[IFR] & SR_INT)) { - /* Shouldn't happen, we hope */ - printk(KERN_ERR "maciisi_interrupt: called without interrupt flag set\n"); - local_irq_restore(flags); - return IRQ_NONE; - } - - /* Clear the interrupt */ - /* via[IFR] = SR_INT; */ - - switch_start: - switch (maciisi_state) { - case idle: - if (status & TIP) - printk(KERN_ERR "maciisi_interrupt: state is idle but TIP asserted!\n"); - - if(!reading_reply) - udelay(ADB_DELAY); - /* Shift in */ - via[ACR] &= ~SR_OUT; - /* Signal start of frame */ - via[B] |= TIP; - /* Clear the interrupt (throw this value on the floor, it's useless) */ - tmp = via[SR]; - /* ACK adb chip, high-low */ - via[B] |= TACK; - udelay(ADB_DELAY); - via[B] &= ~TACK; - reply_len = 0; - maciisi_state = reading; - if (reading_reply) { - reply_ptr = current_req->reply; - } else { - reply_ptr = maciisi_rbuf; - } - break; - - case sending: - /* via[SR]; */ - /* Set ACK off */ - via[B] &= ~TACK; - req = current_req; - - if (!(status & TREQ)) { - /* collision */ - printk(KERN_ERR "maciisi_interrupt: send collision\n"); - /* Set idle and input */ - via[ACR] &= ~SR_OUT; - tmp = via[SR]; - via[B] &= ~TIP; - /* Must re-send */ - reading_reply = 0; - reply_len = 0; - maciisi_state = idle; - udelay(ADB_DELAY); - /* process this now, because the IFR has been cleared */ - goto switch_start; - } - - udelay(ADB_DELAY); - - if (data_index >= req->nbytes) { - /* Sent the whole packet, put the bus back in idle state */ - /* Shift in, we are about to read a reply (hopefully) */ - via[ACR] &= ~SR_OUT; - tmp = via[SR]; - /* End of frame */ - via[B] &= ~TIP; - req->sent = 1; - maciisi_state = idle; - if (req->reply_expected) { - /* Note: only set this once we've - successfully sent the packet */ - reading_reply = 1; - } else { - current_req = req->next; - if (req->done) - (*req->done)(req); - /* Do any queued requests now */ - i = maciisi_start(); - if(i == 0 && need_sync) { - /* Packet needs to be synced */ - maciisi_sync(current_req); - } - if(i != -EBUSY) - need_sync = 0; - } - } else { - /* Sending more stuff */ - /* Shift out */ - via[ACR] |= SR_OUT; - /* Write */ - via[SR] = req->data[data_index++]; - /* Signal 'byte ready' */ - via[B] |= TACK; - } - break; - - case reading: - /* Shift in */ - /* via[ACR] &= ~SR_OUT; */ /* Not in 2.2 */ - if (reply_len++ > 16) { - printk(KERN_ERR "maciisi_interrupt: reply too long, aborting read\n"); - via[B] |= TACK; - udelay(ADB_DELAY); - via[B] &= ~(TACK|TIP); - maciisi_state = idle; - i = maciisi_start(); - if(i == 0 && need_sync) { - /* Packet needs to be synced */ - maciisi_sync(current_req); - } - if(i != -EBUSY) - need_sync = 0; - break; - } - /* Read data */ - *reply_ptr++ = via[SR]; - status = via[B] & (TIP|TREQ); - /* ACK on/off */ - via[B] |= TACK; - udelay(ADB_DELAY); - via[B] &= ~TACK; - if (!(status & TREQ)) - break; /* more stuff to deal with */ - - /* end of frame */ - via[B] &= ~TIP; - tmp = via[SR]; /* That's what happens in 2.2 */ - udelay(ADB_DELAY); /* Give controller time to recover */ - - /* end of packet, deal with it */ - if (reading_reply) { - req = current_req; - req->reply_len = reply_ptr - req->reply; - if (req->data[0] == ADB_PACKET) { - /* Have to adjust the reply from ADB commands */ - if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) { - /* the 0x2 bit indicates no response */ - req->reply_len = 0; - } else { - /* leave just the command and result bytes in the reply */ - req->reply_len -= 2; - memmove(req->reply, req->reply + 2, req->reply_len); - } - } -#ifdef DEBUG_MACIISI_ADB - if (dump_reply) { - int i; - printk(KERN_DEBUG "maciisi_interrupt: reply is "); - for (i = 0; i < req->reply_len; ++i) - printk(" %.2x", req->reply[i]); - printk("\n"); - } -#endif - req->complete = 1; - current_req = req->next; - if (req->done) - (*req->done)(req); - /* Obviously, we got it */ - reading_reply = 0; - } else { - maciisi_input(maciisi_rbuf, reply_ptr - maciisi_rbuf); - } - maciisi_state = idle; - status = via[B] & (TIP|TREQ); - if (!(status & TREQ)) { - /* Timeout?! More likely, another packet coming in already */ -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "extra data after packet: status %x ifr %x\n", - status, via[IFR]); -#endif -#if 0 - udelay(ADB_DELAY); - via[B] |= TIP; - - maciisi_state = reading; - reading_reply = 0; - reply_ptr = maciisi_rbuf; -#else - /* Process the packet now */ - reading_reply = 0; - goto switch_start; -#endif - /* We used to do this... but the controller might actually have data for us */ - /* maciisi_stfu(); */ - } - else { - /* Do any queued requests now if possible */ - i = maciisi_start(); - if(i == 0 && need_sync) { - /* Packet needs to be synced */ - maciisi_sync(current_req); - } - if(i != -EBUSY) - need_sync = 0; - } - break; - - default: - printk("maciisi_interrupt: unknown maciisi_state %d?\n", maciisi_state); - } - local_irq_restore(flags); - return IRQ_HANDLED; -} - -static void -maciisi_input(unsigned char *buf, int nb) -{ -#ifdef DEBUG_MACIISI_ADB - int i; -#endif - - switch (buf[0]) { - case ADB_PACKET: - adb_input(buf+2, nb-2, buf[1] & 0x40); - break; - default: -#ifdef DEBUG_MACIISI_ADB - printk(KERN_DEBUG "data from IIsi ADB (%d bytes):", nb); - for (i = 0; i < nb; ++i) - printk(" %.2x", buf[i]); - printk("\n"); -#endif - break; - } -} -- cgit v1.2.3