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

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