 +		
 +		time->seconds = BOARD_TIME_TO_SEC(board_time);
 +		time->nanoseconds = BOARD_TIME_TO_NSEC(board_time, time->seconds);
-+		DBG( "mpc831x_get_tx_time seconds (%d), nanoseconds (%d)\n", \
++		DBG( "mpc831x_get_tx_time   %9.9d.%9.9d\n", \
 +			time->seconds, time->nanoseconds );
 +	} 
 +	else

+void mpc831x_adj_addend(Integer32 adj);
+
+#endif //MPC831X_H
diff --git a/src/probe.c b/src/probe.c
index 5454e30..8e7a1b9 100644
--- a/src/probe.c
+++ b/src/probe.c
@@ -64,7 +64,7 @@ void probe(RunTimeOpts *rtOpts, PtpClock *ptpClock)
     if(rtOpts->probe_management_key > 0)
       break;
   }
-  
+ 
   getTime(&finish);
   finish.seconds += PTP_SYNC_INTERVAL_TIMEOUT(ptpClock->sync_interval);
   for(;;)
@@ -73,7 +73,7 @@ void probe(RunTimeOpts *rtOpts, PtpClock *ptpClock)
     interval.nanoseconds = 0;
     netSelect(&interval, &ptpClock->netPath);
     
-    netRecvEvent(0, ptpClock->msgIbuf, 0, &ptpClock->netPath);
+    netRecvEvent(0, ptpClock->msgIbuf, 0, &ptpClock->netPath, ptpClock->port_state);
     
     if(netRecvGeneral(0, ptpClock->msgIbuf, &ptpClock->netPath))
     {
@@ -90,6 +90,7 @@ void probe(RunTimeOpts *rtOpts, PtpClock *ptpClock)
       fflush(stdout);
     }
     
+    printf( "getTime probe (now)\n" ); 
     getTime(&now);
     if( now.seconds > finish.seconds || (now.seconds == finish.seconds
       && now.nanoseconds > finish.nanoseconds) )
diff --git a/src/protocol.c b/src/protocol.c
index 4ae14d4..20d9562 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -1,6 +1,9 @@
 /* protocol.c */
 
 #include "ptpd.h"
+#ifdef CONFIG_MPC831X
+#include "mpc831x.h"
+#endif
 
 Boolean doInit(RunTimeOpts*,PtpClock*);
 void doState(RunTimeOpts*,PtpClock*);
@@ -281,7 +284,7 @@ void handle(RunTimeOpts *rtOpts, PtpClock *ptpClock)
   if(!ptpClock->message_activity)
     netSelect(&wait, &ptpClock->netPath);
   
-  if(netRecvEvent(0, ptpClock->msgIbuf, &time, &ptpClock->netPath))
+  if(netRecvEvent(0, ptpClock->msgIbuf, &time, &ptpClock->netPath, ptpClock->port_state))
   {
     subTime(&time, &time, &rtOpts->inboundLatency);
   }
@@ -496,6 +499,10 @@ void handleDelayReq(MsgHeader *header, TimeInternal *time, Boolean isFromSelf, R
       || header->sourceCommunicationTechnology == PTP_DEFAULT
       || ptpClock->clock_communication_technology == PTP_DEFAULT )
     {
+#ifdef CONFIG_MPC831X
+      /* Get the RX time from our HW counter */
+      mpc831x_get_rx_time(time);
+#endif
       issueDelayResp(time, &ptpClock->msgTmpHeader, rtOpts, ptpClock);
     }
     
@@ -505,6 +512,11 @@ void handleDelayReq(MsgHeader *header, TimeInternal *time, Boolean isFromSelf, R
     if(isFromSelf)
     {
       DBG("handleDelayReq: self\n");
+
+#ifdef CONFIG_MPC831X
+      /* Get the TX time from our HW counter */
+      mpc831x_get_tx_time(time);
+#endif
       
       ptpClock->delay_req_send_time.seconds = time->seconds;
       ptpClock->delay_req_send_time.nanoseconds = time->nanoseconds;
@@ -540,7 +552,7 @@ void handleDelayResp(MsgHeader *header, Octet *msgIbuf, Boolean isFromSelf, RunT
       DBG("handleDelayResp: ignore from self\n");
       return;
     }
-    
+   
     resp = &ptpClock->msgTmp.resp;
     msgUnpackDelayResp(ptpClock->msgIbuf, resp);
     
@@ -554,11 +566,12 @@ void handleDelayResp(MsgHeader *header, Octet *msgIbuf, Boolean isFromSelf, RunT
       && !memcmp(header->sourceUuid, ptpClock->parent_uuid, PTP_UUID_LENGTH) )
     {
       ptpClock->sentDelayReq = FALSE;
-      
+    
       toInternalTime(&ptpClock->delay_req_receive_time, &resp->delayReceiptTimestamp, &ptpClock->halfEpoch);
       
       if(ptpClock->delay_req_send_time.seconds)
       {
+        DBG( "handleDelayResp: from master\n" );
         updateDelay(&ptpClock->delay_req_send_time, &ptpClock->delay_req_receive_time,
           &ptpClock->owd_filt, rtOpts, ptpClock);
         
@@ -626,7 +639,8 @@ void issueSync(RunTimeOpts *rtOpts, PtpClock *ptpClock)
   
   ++ptpClock->last_sync_event_sequence_number;
   ptpClock->grandmaster_sequence_number = ptpClock->last_sync_event_sequence_number;
-  
+ 
+  printf( "getTime issueSync\n" ); 
   getTime(&internalTime);
   fromInternalTime(&internalTime, &originTimestamp, ptpClock->halfEpoch);
   msgPackSync(ptpClock->msgObuf, FALSE, &originTimestamp, ptpClock);
@@ -660,14 +674,16 @@ void issueDelayReq(RunTimeOpts *rtOpts, PtpClock *ptpClock)
   ptpClock->sentDelayReq = TRUE;
   ptpClock->sentDelayReqSequenceId = ++ptpClock->last_sync_event_sequence_number;
   
+  printf( "getTime issueDelayReq\n" ); 
   getTime(&internalTime);
   fromInternalTime(&internalTime, &originTimestamp, ptpClock->halfEpoch);
   msgPackDelayReq(ptpClock->msgObuf, FALSE, &originTimestamp, ptpClock);
   
   if(!netSendEvent(0, ptpClock->msgObuf, DELAY_REQ_PACKET_LENGTH, &ptpClock->netPath))
     toState(PTP_FAULTY, rtOpts, ptpClock);
-  else
+  else{
     DBGV("sent delay request message\n");
+  }
 }
 
 void issueDelayResp(TimeInternal *time, MsgHeader *header, RunTimeOpts *rtOpts, PtpClock *ptpClock)

 	  	 

Index: extras/ptpd.spec
===================================================================
--- extras/ptpd.spec	(revision 0)
+++ extras/ptpd.spec	(revision 0)
@@ -0,0 +1,61 @@
+#**************************************
+#
+#Copyright notice:
+#
+#Author: Alan K. Bartky, Bartky Networks, 
+#        alan@bartky.net, www.bartky.net
+#
+#Copyright (c) 2007 Alan K. Bartky
+#
+#Permission is hereby granted to use, copy, modify, and distribute this software
+#for any purpose and without fee, provided that this notice appears in all
+#copies of this file and any derivative works. 
+#
+#The author makes no representations about the suitability of this
+#software for any purpose. This software is provided "as is" without express or
+#implied warranty.
+#
+#***************************************
+%define pfx /opt/freescale/rootfs/%{_target_cpu}
+
+Summary         : The PTP daemon which implements the IEEE 1588v1 Precision Time protocol (PTP)
+Name            : ptpd
+Version         : 1rc1
+Release         : RC1
+License         : BSD
+Vendor          : Bartky Networks (www.bartky.net)
+Packager        : Alan K. Bartky (alan@bartky.net)
+Group           : System Environment/Daemons
+URL             : http://sourceforge.net/projects/ptpd/
+Source          : %{name}-%{version}.tar.gz
+Patch0          : %{name}-%{version}-mpc831x.patch
+BuildRoot       : %{_tmppath}/%{name}
+Prefix          : %{pfx}
+
+%Description
+%{summary}
+
+%Prep
+%setup 
+%patch0 -p1
+
+%Build
+cd src
+make all
+#./configure --prefix=%{_prefix} --host=$CFGHOST --build=%{_build}
+
+%Install
+rm -rf $RPM_BUILD_ROOT
+#make install DESTDIR=$RPM_BUILD_ROOT/%{pfx}
+for i in usr/sbin 
+do
+    mkdir -p $RPM_BUILD_ROOT/%{pfx}/$i
+done
+cp -a src/ptpd $RPM_BUILD_ROOT/%{pfx}/usr/sbin
+
+%Clean
+rm -rf $RPM_BUILD_ROOT
+
+%Files
+%defattr(-,root,root)
+%{pfx}/*
Index: extras/PTPD_MPC8313_SETUP_README.txt
===================================================================
--- extras/PTPD_MPC8313_SETUP_README.txt	(revision 0)
+++ extras/PTPD_MPC8313_SETUP_README.txt	(revision 0)
@@ -0,0 +1,202 @@
+Instructions on prepping ptpd version 1rc1 for MPC8313E-RDB board 
+under a Freescale LTIB environment
+
+**************************************
+
+Copyright notice:
+
+Author: Alan K. Bartky, Bartky Networks, 
+        alan@bartky.net, www.bartky.net
+
+Copyright (c) 2007 Alan K. Bartky
+
+Permission is hereby granted to use, copy, modify, and distribute this file
+for any purpose and without fee, provided that this notice appears in all
+copies of this file and any derivative works. 
+
+The author makes no representations about the suitability of this
+software for any purpose. This software is provided "as is" without express or
+implied warranty.
+
+***************************************
+Comments/corrections are welcome.
+
+Revision: 0.1 2007-08-29  - First version of this file.
+
+Steps to get ptpd code to work.  
+
+NOTE: These steps assume that the ptpd code tar file and
+the correct MPC8313 patch file have been downloaded and copied to a working
+directory and also that the Freescale Linux Target Image Builder (LTIB)
+has been properely installed, built and the based code has been 
+verified on a known good MPC8313E-RDB board.
+
+*****
+
+Step A: Manually apply the MPC831x patch to test it and to get the
+ptpd.spec file needed for setting up LTIB:
+
+******
+
+Change to a working directory (in this example, I am using
+/home/alan)
+
+Un-tar the files (version 1rc1 extracts to a directory named ptpd)
+
+ alan@raptor:~$ tar -xvvf ptpd_1rc1.tar.tar
+
+We are only using this as a temp process to get some files we need
+to proceed, so rename the ptpd directory:
+
+ alan@raptor:~$ mv ptpd ptpd-temp
+
+To apply the patch use a manual patch command as follows
+(assumes you have copied the patch file to your working
+directory):
+
+ alan@raptor:~$ cd ptpd-temp
+ alan@raptor:~/ptpd-temp$ patch -p1 <../ptpd-1rc1-mpc831x.patch
+
+Copy the spec file to your working directory
+
+ alan@raptor:~/ptpd-temp$ cp extras/ptpd.spec ..
+ alan@raptor:~/ptpd-temp$ cd ..
+ alan@raptor:~$
+
+In version 1rc1, this will extract the files to a directory called ptpd.
+The first step in prepping for LTIB, is to rename the directory
+to include the version number (used several places in LTIB scripts).
+
+ alan@raptor:~$ mv ptpd ptpd-1rc1
+
+******
+
+Step B: Create and setupnew gzip tar files and md5 checksum files 
+with version numbers to make LTIB happy (otherwise, I got failures 
+at multiple points when running setups and builds).
+
+******
+
+If not already there, go to your working directory with the 
+tar file and the patch file in it.
+
+Unzip from the downloaded file again (this time we are not going
+to apply the patch and let LTIB do it using the .spec
+file we just extracted)
+
+ alan@raptor:~$ tar -xvvf ptpd_1rc1.tar.tar
+
+This time we are going to raname the directory to include the
+revision 
+
+ alan@raptor:~$ mv ptpd ptpd-1rc1
+
+Now we create a new gzip tar file and an associated md5 checksum file:
+
+ alan@raptor:~$ tar -zcvf ptpd-1rc1.tar.gz ptpd-1rc1
+
+ alan@raptor:~$ md5sum ptpd-1rc1.tar.gz > ptpd-1rc1.tar.gz.md5
+
+We also need an md5 checksum file for the patch as well:
+
+ alan@raptor:~$ md5sum ptpd-1rc1-mpc831x.patch > ptpd-1rc1-mpc831x.patch.md5
+
+Now we have all the files we need, now they have to be copied
+to the right directories, first we copy the package files to
+the Freescale package directories:
+
+ alan@raptor:~$ cp ptpd-1rc1.tar.gz* /opt/freescale/pkgs/
+ alan@raptor:~$ cp ptpd-1rc1-mpc831x.patch* /opt/freescale/pkgs
+
+Now we setup the ptpd.spec file to be in the correct location.
+Go to <ltib directory name>/dist/<lfs directory name> example:
+
+ alan@raptor:~$ cd ltib-mpc8313erdb-20070718/
+ alan@raptor:~/ltib-mpc8313erdb-20070718$ cd dist/lfs-5.1/
+ alan@raptor:~/ltib-mpc8313erdb-20070718/dist/lfs-5.1$
+
+Make a new directory for ptpd
+
+ alan@raptor:~/ltib-mpc8313erdb-20070718/dist/lfs-5.1$ mkdir ptpd
+ alan@raptor:~/ltib-mpc8313erdb-20070718/dist/lfs-5.1$ cd ptpd
+
+Copy the ptpd.spec file from your working directory (this
+example is using /home/alan):
+
+ alan@raptor:~/ltib-mpc8313erdb-20070718/dist/lfs-5.1/ptpd$ cp /home/alan/ptpd.spec .
+
+This completes all the package and spec file setup, you can now setup
+the LTIB to install, build and deploy :-)
+
+******
+
+Step C: Installing, building and deploying ptpd
+
+******
+
+If you did all the above steps correctly, you can now perform the LTIB
+prep step by going to your ltib directory and running the -m prep option:
+
+ alan@raptor:~/ltib-mpc8313erdb-20070718$ ./ltib -m prep -p ptpd.spec
+
+Assuming you did all the previous steps correct, LTIB should automatically
+run rpm, setup the main file structure and automatically apply
+the MPC831x patch.  You should now have no copy or patch error messages
+and a new ptpd-1rc1 directory under the ltib rpm/BUILD directory.  You can 
+now try a build.
+
+ alan@raptor:~/ltib-mpc8313erdb-20070718$ ./ltib -m scbuild -p ptpd.spec
+
+Assuming the build goes OK, you can try a deploy operation:
+
+ alan@raptor:~/ltib-mpc8313erdb-20070718$ ./ltib -m scdeploy -p ptpd.spec
+
+If all this works, the root filesystem should now be all set and have
+the ptpd executable loaded under /usr/sbin of the target root filesystem.
+
+I have tested this code running on two MPC8313E-RDB boards hooked back to
+back via a gigabit Ethernet running on ETSEC2 (lower right connector of
+the 6 port ethernet connector bloc).  If you use my makefile, it turns on
+all debugging, so if you want to turn it off, just modify the makefile.
+
+The simplist way I have found to run this test is run ptpd on one device
+as preffered clock:
+
+ /usr/sbin ptpd -c -b eth1 -p
+
+and then run the other as non-preffered clock (without -p):
+
+ /usr/sbin ptpd -c -b eth1
+
+****************
+
+Final note/caution on using MPC8313E-RDB distribution from 
+Freescale (Beta version as of 2007-07-18 and also the 
+previous version)
+
+****************
+
+During the time that it took for me to get ptpd running using the 
+patch as previously posted, I found I was able to get ptpd
+to compile without too much trouble, but the same was not the
+case for the MPC8313E-RDB Linux BSP from Freescale.  I spent
+a huge amount of time hunting down and then fixing to the best of
+my ability several bugs in the kernel Ethernet driver HW timestamping
+code and also other problems in the Marvell PHY chip initialization
+(I am an experienced programmer, but a linux newbie, so it
+took me much longer than expected to get the Freescale code
+part to work).
+
+Note that if you do not also modify the Beta kernel driver code
+with fixes in this area, the ptpd HW timestamping will not work!
+
+As this Beta code is all maintained by Freescale, I did not post any
+of these fixes on the ptpd sourceforge.net site.  If you are interested
+in help in this area you can contact me by email at alan@bartky.net
+
+
+- Alan K. Bartky
+  Owner, Bartky Networks (Network protocol and system design)
+  www.bartky.net
+  alan@bartky.net
+
Index: src/protocol.c
===================================================================
--- src/protocol.c	(revision 35)
+++ src/protocol.c	(working copy)
@@ -1,6 +1,9 @@
 /* protocol.c */
 
 #include "ptpd.h"
+#ifdef CONFIG_MPC831X
+#include "mpc831x.h"
+#endif
 
 Boolean doInit(RunTimeOpts*,PtpClock*);
 void doState(RunTimeOpts*,PtpClock*);
@@ -299,7 +302,11 @@
   
   DBGV("handle: something\n");
   
+#ifdef CONFIG_MPC831X
+  length = netRecvEvent(ptpClock->msgIbuf, &time, &ptpClock->netPath, ptpClock->port_state);
+#else
   length = netRecvEvent(ptpClock->msgIbuf, &time, &ptpClock->netPath);
+#endif
   if(length < 0)
   {
     PERROR("failed to receive on the event socket");
@@ -571,6 +578,10 @@
       || header->sourceCommunicationTechnology == PTP_DEFAULT
       || ptpClock->clock_communication_technology == PTP_DEFAULT )
     {
+#ifdef CONFIG_MPC831X
+      /* Get the RX time from HW counter instead of value passed in function call */
+      mpc831x_get_rx_time(time);
+#endif
       issueDelayResp(time, &ptpClock->msgTmpHeader, rtOpts, ptpClock);
     }
     
@@ -581,6 +592,10 @@
     {
       DBG("handleDelayReq: self\n");
       
+#ifdef CONFIG_MPC831X
+      /* Get the TX time from HW counter instead of value passed in function call */
+      mpc831x_get_tx_time(time);
+#endif
       ptpClock->delay_req_send_time.seconds = time->seconds;
       ptpClock->delay_req_send_time.nanoseconds = time->nanoseconds;
       
Index: src/mpc831x.c
===================================================================
--- src/mpc831x.c	(revision 0)
+++ src/mpc831x.c	(revision 0)
@@ -0,0 +1,149 @@
+#include "ptpd.h"
+#include "mpc831x.h"
+#include <unistd.h>
