revert between 56095 -> 55830 in arch
[AROS.git] / arch / ppc-sam440 / battclock / writebattclock.c
blob8e385b9600bc0efaa0b2065d873f90c364873a06
1 /*
2 Copyright � 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: WriteBattClock()
6 Lang: english
7 */
8 #include "battclock_intern.h"
10 #define DEBUG 1
11 #include <aros/debug.h>
13 #include <proto/battclock.h>
14 #include <proto/utility.h>
15 #include <proto/exec.h>
16 #include <proto/oop.h>
17 #include <hidd/i2c.h>
18 #include <utility/date.h>
19 #include <asm/amcc440.h>
20 #include <stdarg.h>
22 /* See rom/battclock/writebattclock.c for documentation */
24 AROS_LH1(void, WriteBattClock,
25 AROS_LHA(ULONG, time, D0),
26 APTR *, BattClockBase, 3, Battclock)
28 AROS_LIBFUNC_INIT
30 struct ClockData date;
31 OOP_Object *i2c;
32 OOP_AttrBase __IHidd_I2CDevice;
34 /* The code here looks more complex than it really is */
36 struct pHidd_I2C_ProbeAddress p, *msg=&p;
37 struct Library *OOPBase = OpenLibrary("oop.library", 0);
38 struct Library *I2CBase = OpenLibrary("i2c-amcc440.library", 0);
40 Amiga2Date(time, &date);
42 __IHidd_I2CDevice = OOP_ObtainAttrBase(IID_Hidd_I2CDevice);
44 /* New i2c driver */
45 i2c = OOP_NewObject(NULL, CLID_I2C_AMCC440, NULL);
47 p.mID = OOP_GetMethodID((STRPTR)IID_Hidd_I2C, moHidd_I2C_ProbeAddress);
49 D(bug("[BATT] i2c=%08x\n", i2c));
51 if (i2c)
53 /* i2c class successfully created. Probe for RTC on the bus... */
54 D(bug("[BATT] Probing i2c RTC...\n"));
56 p.address = 0xd0;
58 if (OOP_DoMethod(i2c, (OOP_Msg) msg))
60 /* Got it. Now read the data (7 bytes) from address 1 in RTC */
61 char wb[8] = {1,};
63 struct TagItem attrs[] = {
64 { aHidd_I2CDevice_Driver, (IPTR)i2c },
65 { aHidd_I2CDevice_Address, 0xd0 },
66 { aHidd_I2CDevice_Name, (IPTR)"RTC" },
67 { TAG_DONE, 0UL }
70 OOP_Object *obj = OOP_NewObject(NULL, CLID_Hidd_I2CDevice, attrs);
72 D(bug("[BATT] RTC found. Object=%08x\n", obj));
74 /* i2c device object created. read data now */
75 if (obj)
77 struct pHidd_I2CDevice_WriteRead msg;
79 msg.mID = OOP_GetMethodID((STRPTR)IID_Hidd_I2CDevice, moHidd_I2CDevice_WriteRead);
80 msg.readBuffer = &wb[1];
81 msg.readLength = 7;
82 msg.writeBuffer = &wb[0];
83 msg.writeLength = 1;
85 OOP_DoMethod(obj, &msg.mID);
87 int i;
89 D(bug("[BATT] Old dump: "));
90 for (i=1; i < 8; i++)
92 D(bug("%02x ", wb[i]));
94 D(bug("\n"));
96 /* Data read. Modify the bits and pieces now */
97 wb[1] = (wb[1] & 0x80) | (date.sec / 10) << 4 | (date.sec % 10);
98 wb[2] = (date.min / 10) << 4 | (date.min % 10);
99 wb[3] = (wb[3] & 0xc0) | (date.hour / 10) << 4 | (date.hour % 10);
100 wb[4] = (wb[4] & 0x80) | (date.wday);
101 wb[5] = (date.mday / 10) << 4 | (date.mday % 10);
102 wb[6] = (date.month / 10) << 4 | (date.month % 10);
103 wb[7] = ((date.year - 2000) / 10) << 4 | ((date.year - 2000) % 10);
105 D(bug("[BATT] New dump: "));
106 for (i=1; i < 8; i++)
108 D(bug("%02x ", wb[i]));
110 D(bug("\n"));
112 /* Write data to RTC */
113 wb[0] = 1;
114 msg.readBuffer = NULL;
115 msg.readLength = 0;
116 msg.writeBuffer = &wb[0];
117 msg.writeLength = 8;
119 OOP_DoMethod(obj, &msg.mID);
121 /* Done with i2c device */
122 OOP_DisposeObject(obj);
126 /* Done with i2c bus */
127 OOP_DisposeObject(i2c);
130 /* Cleanup */
131 OOP_ReleaseAttrBase(IID_Hidd_I2CDevice);
132 if (OOPBase)
133 CloseLibrary(OOPBase);
134 if (I2CBase)
135 CloseLibrary(I2CBase);
137 AROS_LIBFUNC_EXIT
138 } /* WriteBattClock */