Subject: Re: mfdcr/mtdcr
To: Simon Burge <simonb@wasabisystems.com>
From: Jachym Holecek <freza@dspfpga.com>
List: port-powerpc
Date: 05/10/2006 05:36:27
# Simon Burge 2006-05-10:
> Jachym Holecek wrote:
> > I'd like to move mfdcr()/mtdcr() macro definitions into a separate file.
> > Motivation:
> > 
> >   * Currently, they're defined multiple times in include/ibm4xx/dcr40*.h
> > 
> >   * For evbppc/virtex, I'd like to be able to access DCR without
> >     also pulling in irrelevant address definitions (DCR address
> >     layout is completely up to the HDL designer)
> > 
> > The diff below (relative to arch/powerpc/include/ibm4xx) creates dcr.h
> > and makes dcr40*.h use it. Other option would be to move the macros to
> > ibm4xx/cpu.h (the m{f,t}dcr instructions are 4xx specific).
> 
> DCRs are mentioned in BookE, insofar as "they can exist" which doesn't
> say too much.  Are they really only available on 4xx?  If so, then
> I I think I prefer putting them in ibm4xx/cpu.h.  Otherwise just
> powerpc/cpu.h rather than making a new header?

As far a quick look at Google goes, we have 403 and 405 implementing
m{ft}dcr as extension and 440 as part of BookE. The only non-IBM
BookE core seems to be Freescale's e500, which doesn't support
m{ft}dcr. In addition to BookE, m{ft}dcr seem to also be defined
by "PowerPC embedded-environment architecture" (no clue here).

Because "they can exist" in general, the updated patch below goes
with powerpc/cpu.h. Including cpu.h in dcr4*.h looked ugly, so this
is left to the user -- I've verified all affected sources will
get machine/cpu.h via sys/param.h anyway.

	-- Jachym

Index: sys/arch/powerpc/include/cpu.h
===================================================================
RCS file: /cvsroot/src/sys/arch/powerpc/include/cpu.h,v
retrieving revision 1.47
diff -u -r1.47 cpu.h
--- sys/arch/powerpc/include/cpu.h	16 Feb 2006 20:17:14 -0000	1.47
+++ sys/arch/powerpc/include/cpu.h	10 May 2006 03:13:33 -0000
@@ -252,6 +252,24 @@
 	return (pvr);
 }
 
+#if defined(PPC_IBM4XX) || defined(PPC_IBM403)
+/*
+ * DCR (Device Control Register) access. These have to be
+ * macros because register address is encoded as immediate
+ * operand (and thus needs to be a compile-time constant).
+ */
+#define mtdcr(reg, val)						\
+	__asm volatile("mtdcr %0,%1" : : "K"(reg), "r"(val))
+
+#define mfdcr(reg)						\
+({ 								\
+	uint32_t __val;					\
+								\
+	__asm volatile("mfdcr %0,%1" : "=r"(__val) : "K"(reg)); \
+	__val; 							\
+})
+#endif /* PPC_IBM4XX || PPC_IBM403 */
+
 /*
  * CLKF_BASEPRI is dependent on the underlying interrupt code
  * and can not be defined here.  It should be defined in
Index: sys/arch/powerpc/include/ibm4xx/dcr403cgx.h
===================================================================
RCS file: /cvsroot/src/sys/arch/powerpc/include/ibm4xx/dcr403cgx.h,v
retrieving revision 1.2
diff -u -r1.2 dcr403cgx.h
--- sys/arch/powerpc/include/ibm4xx/dcr403cgx.h	24 Dec 2005 22:45:36 -0000	1.2
+++ sys/arch/powerpc/include/ibm4xx/dcr403cgx.h	10 May 2006 03:13:33 -0000
@@ -39,15 +39,6 @@
 #ifndef _DCR403GCXP_H_
 #define	_DCR403GCXP_H_
 
-#ifndef _LOCORE
-#define	mtdcr(reg, val)						\
-	__asm volatile("mtdcr %0,%1" : : "K"(reg), "r"(val))
-#define	mfdcr(reg)						\
-	( { u_int32_t val;					\
-	  __asm volatile("mfdcr %0,%1" : "=r"(val) : "K"(reg));	\
-	  val; } )
-#endif /* _LOCORE */
-
 /* Device Control Register declarations */
 
 #define DCR_EXISR		0x040	/* External Interrupt Status Register */
Index: sys/arch/powerpc/include/ibm4xx/dcr405gp.h
===================================================================
RCS file: /cvsroot/src/sys/arch/powerpc/include/ibm4xx/dcr405gp.h,v
retrieving revision 1.4
diff -u -r1.4 dcr405gp.h
--- sys/arch/powerpc/include/ibm4xx/dcr405gp.h	24 Dec 2005 22:45:36 -0000	1.4
+++ sys/arch/powerpc/include/ibm4xx/dcr405gp.h	10 May 2006 03:13:33 -0000
@@ -38,15 +38,6 @@
 #ifndef _DCR405GP_H_
 #define	_DCR405GP_H_
 
-#ifndef _LOCORE
-#define	mtdcr(reg, val)						\
-	__asm volatile("mtdcr %0,%1" : : "K"(reg), "r"(val))
-#define	mfdcr(reg)						\
-	( { u_int32_t val;					\
-	  __asm volatile("mfdcr %0,%1" : "=r"(val) : "K"(reg));	\
-	  val; } )
-#endif /* _LOCORE */
-
 /* Device Control Register declarations */
 
 /* DCRs used for indirect access */
Index: sys/arch/powerpc/include/ibm4xx/dcr405xx.h
===================================================================
RCS file: /cvsroot/src/sys/arch/powerpc/include/ibm4xx/dcr405xx.h,v
retrieving revision 1.4
diff -u -r1.4 dcr405xx.h
--- sys/arch/powerpc/include/ibm4xx/dcr405xx.h	24 Dec 2005 22:45:36 -0000	1.4
+++ sys/arch/powerpc/include/ibm4xx/dcr405xx.h	10 May 2006 03:13:33 -0000
@@ -39,16 +39,6 @@
  * 	405CR/NPe405L/NPe405H/405EP/405GP/405GPr
  */
 
-#ifndef _LOCORE
-#define	mtdcr(reg, val)						\
-	__asm volatile("mtdcr %0,%1" : : "K"(reg), "r"(val))
-#define	mfdcr(reg)						\
-	( { u_int32_t val;					\
-	  __asm volatile("mfdcr %0,%1" : "=r"(val) : "K"(reg));	\
-	  val; } )
-#endif /* _LOCORE */
-
-
 /*****************************************************************************/
 /*
  * Memory Controller Registers (0x010-0x011)