Modules/Tegra2Vid - Misc
[tpg/acess2.git] / KernelLand / Modules / Input / PS2KbMouse / pl050.c
1 /*
2  * Acess2
3  * - By thePowersGang (John Hodge)
4  *
5  * PL050 (or comaptible) Driver
6  */
7 #define DEBUG   1
8
9 #include <acess.h>
10 #include "common.h"
11
12 // === CONSTANTS ===
13 #define PL050_TXBUSY    0x20
14
15 // === PROTOTYPES ===
16 void    PL050_Init(Uint32 KeyboardBase, Uint8 KeyboardIRQ, Uint32 MouseBase, Uint8 MouseIRQ);
17 void    PL050_KeyboardHandler(int IRQ, void *Ptr);
18 void    PL050_MouseHandler(int IRQ, void *Ptr);
19 void    PL050_EnableMouse(void);
20 static inline void      PL050_WriteMouseData(Uint8 data);
21 static inline void      PL050_WriteKeyboardData(Uint8 data);
22 static inline Uint8     PL050_ReadMouseData(void);
23 static inline Uint8     PL050_ReadKeyboardData(void);
24
25 // === GLOBALS ===
26 Uint32  *gpPL050_KeyboardBase;
27 Uint32  *gpPL050_MouseBase;
28
29 // === CODE ===
30 void PL050_Init(Uint32 KeyboardBase, Uint8 KeyboardIRQ, Uint32 MouseBase, Uint8 MouseIRQ)
31 {
32         if( KeyboardBase ) {
33                 LOG("KeyboardBase = 0x%x", KeyboardBase);
34                 gpPL050_KeyboardBase = (void*)MM_MapHWPages(KeyboardBase, 1);
35                 LOG("gpPL050_KeyboardBase = %p", gpPL050_KeyboardBase);
36                 IRQ_AddHandler(KeyboardIRQ, PL050_KeyboardHandler, NULL);
37
38                 gpPL050_KeyboardBase[0] = 0x10;
39         }
40         if( MouseBase ) {
41                 gpPL050_MouseBase = (void*)MM_MapHWPages(MouseBase, 1);
42                 IRQ_AddHandler(MouseIRQ, PL050_MouseHandler, NULL);
43
44                 gpPL050_MouseBase[0] = 0x10;
45         }
46 }
47
48 void PL050_KeyboardHandler(int IRQ, void *Ptr)
49 {
50         Uint8   scancode;
51
52         scancode = PL050_ReadKeyboardData();
53         KB_HandleScancode( scancode );
54 }
55
56 void PL050_MouseHandler(int IRQ, void *Ptr)
57 {
58         PS2Mouse_HandleInterrupt( PL050_ReadMouseData() );
59 }
60
61 void PL050_SetLEDs(Uint8 leds)
62 {
63         PL050_WriteKeyboardData(0xED);
64         PL050_WriteKeyboardData(leds);
65 }
66
67 void PL050_EnableMouse(void)
68 {
69         Log_Log("PL050", "Enabling Mouse...");
70         
71         //PL050_WriteMouseData(0xD4);
72         //PL050_WriteMouseData(0xF6);   // Set Default Settings
73 //      PL050_WriteMouseData(0xD4);
74 //      PL050_WriteMouseData(0xF4);     // Enable Packets
75         LOG("Done");
76 }
77
78 static inline void PL050_WriteMouseData(Uint8 Data)
79 {
80          int    timeout = 10000;
81
82         if( !gpPL050_MouseBase ) {
83                 Log_Error("PL050", "Mouse disabled (gpPL050_MouseBase = NULL)");
84                 return ;
85         }
86
87         ENTER("xData", Data);
88
89         while( --timeout && (gpPL050_MouseBase[1] & PL050_TXBUSY) );
90         if(timeout)
91                 gpPL050_MouseBase[2] = Data;
92         else
93                 Log_Error("PL050", "Write to mouse timed out");
94         LEAVE('-');
95 }
96
97 static inline Uint8 PL050_ReadMouseData(void)
98 {
99         if( !gpPL050_MouseBase ) {
100                 Log_Error("PL050", "Mouse disabled (gpPL050_MouseBase = NULL)");
101                 return 0;
102         }
103         return gpPL050_MouseBase[2];
104 }
105 static inline void PL050_WriteKeyboardData(Uint8 Data)
106 {
107          int    timeout = 10000;
108
109         if( !gpPL050_KeyboardBase ) {
110                 Log_Error("PL050", "Keyboard disabled (gpPL050_KeyboardBase = NULL)");
111                 return ;
112         }
113
114         while( --timeout && (gpPL050_KeyboardBase[1] & PL050_TXBUSY) );
115         if(timeout)
116                 gpPL050_KeyboardBase[2] = Data;
117         else
118                 Log_Error("PL050", "Write to keyboard timed out");
119 }
120 static inline Uint8 PL050_ReadKeyboardData(void)
121 {
122         if( !gpPL050_KeyboardBase ) {
123                 Log_Error("PL050", "Keyboard disabled (gpPL050_KeyboardBase = NULL)");
124                 return 0;
125         }
126
127         return gpPL050_KeyboardBase[2];
128 }
129

UCC git Repository :: git.ucc.asn.au