18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
48c2ecf20Sopenharmony_ci *                    <benh@kernel.crashing.org>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_DCR_NATIVE_H
88c2ecf20Sopenharmony_ci#define _ASM_POWERPC_DCR_NATIVE_H
98c2ecf20Sopenharmony_ci#ifdef __KERNEL__
108c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
138c2ecf20Sopenharmony_ci#include <asm/cputable.h>
148c2ecf20Sopenharmony_ci#include <asm/cpu_has_feature.h>
158c2ecf20Sopenharmony_ci#include <linux/stringify.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_citypedef struct {
188c2ecf20Sopenharmony_ci	unsigned int base;
198c2ecf20Sopenharmony_ci} dcr_host_native_t;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic inline bool dcr_map_ok_native(dcr_host_native_t host)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	return true;
248c2ecf20Sopenharmony_ci}
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define dcr_map_native(dev, dcr_n, dcr_c) \
278c2ecf20Sopenharmony_ci	((dcr_host_native_t){ .base = (dcr_n) })
288c2ecf20Sopenharmony_ci#define dcr_unmap_native(host, dcr_c)		do {} while (0)
298c2ecf20Sopenharmony_ci#define dcr_read_native(host, dcr_n)		mfdcr(dcr_n + host.base)
308c2ecf20Sopenharmony_ci#define dcr_write_native(host, dcr_n, value)	mtdcr(dcr_n + host.base, value)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Table based DCR accessors */
338c2ecf20Sopenharmony_ciextern void __mtdcr(unsigned int reg, unsigned int val);
348c2ecf20Sopenharmony_ciextern unsigned int __mfdcr(unsigned int reg);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* mfdcrx/mtdcrx instruction based accessors. We hand code
378c2ecf20Sopenharmony_ci * the opcodes in order not to depend on newer binutils
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_cistatic inline unsigned int mfdcrx(unsigned int reg)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	unsigned int ret;
428c2ecf20Sopenharmony_ci	asm volatile(".long 0x7c000206 | (%0 << 21) | (%1 << 16)"
438c2ecf20Sopenharmony_ci		     : "=r" (ret) : "r" (reg));
448c2ecf20Sopenharmony_ci	return ret;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic inline void mtdcrx(unsigned int reg, unsigned int val)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	asm volatile(".long 0x7c000306 | (%0 << 21) | (%1 << 16)"
508c2ecf20Sopenharmony_ci		     : : "r" (val), "r" (reg));
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define mfdcr(rn)						\
548c2ecf20Sopenharmony_ci	({unsigned int rval;					\
558c2ecf20Sopenharmony_ci	if (__builtin_constant_p(rn) && rn < 1024)		\
568c2ecf20Sopenharmony_ci		asm volatile("mfdcr %0, %1" : "=r" (rval)	\
578c2ecf20Sopenharmony_ci			      : "n" (rn));			\
588c2ecf20Sopenharmony_ci	else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR)))	\
598c2ecf20Sopenharmony_ci		rval = mfdcrx(rn);				\
608c2ecf20Sopenharmony_ci	else							\
618c2ecf20Sopenharmony_ci		rval = __mfdcr(rn);				\
628c2ecf20Sopenharmony_ci	rval;})
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define mtdcr(rn, v)						\
658c2ecf20Sopenharmony_cido {								\
668c2ecf20Sopenharmony_ci	if (__builtin_constant_p(rn) && rn < 1024)		\
678c2ecf20Sopenharmony_ci		asm volatile("mtdcr %0, %1"			\
688c2ecf20Sopenharmony_ci			      : : "n" (rn), "r" (v));		\
698c2ecf20Sopenharmony_ci	else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR)))	\
708c2ecf20Sopenharmony_ci		mtdcrx(rn, v);					\
718c2ecf20Sopenharmony_ci	else							\
728c2ecf20Sopenharmony_ci		__mtdcr(rn, v);					\
738c2ecf20Sopenharmony_ci} while (0)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* R/W of indirect DCRs make use of standard naming conventions for DCRs */
768c2ecf20Sopenharmony_ciextern spinlock_t dcr_ind_lock;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline unsigned __mfdcri(int base_addr, int base_data, int reg)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	unsigned long flags;
818c2ecf20Sopenharmony_ci	unsigned int val;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dcr_ind_lock, flags);
848c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
858c2ecf20Sopenharmony_ci		mtdcrx(base_addr, reg);
868c2ecf20Sopenharmony_ci		val = mfdcrx(base_data);
878c2ecf20Sopenharmony_ci	} else {
888c2ecf20Sopenharmony_ci		__mtdcr(base_addr, reg);
898c2ecf20Sopenharmony_ci		val = __mfdcr(base_data);
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dcr_ind_lock, flags);
928c2ecf20Sopenharmony_ci	return val;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic inline void __mtdcri(int base_addr, int base_data, int reg,
968c2ecf20Sopenharmony_ci			    unsigned val)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	unsigned long flags;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dcr_ind_lock, flags);
1018c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
1028c2ecf20Sopenharmony_ci		mtdcrx(base_addr, reg);
1038c2ecf20Sopenharmony_ci		mtdcrx(base_data, val);
1048c2ecf20Sopenharmony_ci	} else {
1058c2ecf20Sopenharmony_ci		__mtdcr(base_addr, reg);
1068c2ecf20Sopenharmony_ci		__mtdcr(base_data, val);
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dcr_ind_lock, flags);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic inline void __dcri_clrset(int base_addr, int base_data, int reg,
1128c2ecf20Sopenharmony_ci				 unsigned clr, unsigned set)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	unsigned long flags;
1158c2ecf20Sopenharmony_ci	unsigned int val;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dcr_ind_lock, flags);
1188c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
1198c2ecf20Sopenharmony_ci		mtdcrx(base_addr, reg);
1208c2ecf20Sopenharmony_ci		val = (mfdcrx(base_data) & ~clr) | set;
1218c2ecf20Sopenharmony_ci		mtdcrx(base_data, val);
1228c2ecf20Sopenharmony_ci	} else {
1238c2ecf20Sopenharmony_ci		__mtdcr(base_addr, reg);
1248c2ecf20Sopenharmony_ci		val = (__mfdcr(base_data) & ~clr) | set;
1258c2ecf20Sopenharmony_ci		__mtdcr(base_data, val);
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dcr_ind_lock, flags);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define mfdcri(base, reg)	__mfdcri(DCRN_ ## base ## _CONFIG_ADDR,	\
1318c2ecf20Sopenharmony_ci					 DCRN_ ## base ## _CONFIG_DATA,	\
1328c2ecf20Sopenharmony_ci					 reg)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define mtdcri(base, reg, data)	__mtdcri(DCRN_ ## base ## _CONFIG_ADDR,	\
1358c2ecf20Sopenharmony_ci					 DCRN_ ## base ## _CONFIG_DATA,	\
1368c2ecf20Sopenharmony_ci					 reg, data)
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define dcri_clrset(base, reg, clr, set)	__dcri_clrset(DCRN_ ## base ## _CONFIG_ADDR,	\
1398c2ecf20Sopenharmony_ci							      DCRN_ ## base ## _CONFIG_DATA,	\
1408c2ecf20Sopenharmony_ci							      reg, clr, set)
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
1438c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */
1448c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_DCR_NATIVE_H */
145