Modules/PS2KbMouse - Adding pl050 support
[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
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);
16 void    PL050_MouseHandler(int IRQ);
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 = MM_MapHW(KeyboardBase, 0x1000);
32                 IRQ_AddHandler(KeyboardIRQ, PL050_KeyboardHandler);
33         }
34         if( MouseBase ) {
35                 gpPL050_MouseBase = MM_MapHW(MouseBase, 0x1000);
36                 IRQ_AddHandler(MouseIRQ, PL050_MouseHandler);
37         }
38 }
39
40 void PL050_KeyboardHandler(int IRQ)
41 {
42         Uint8   scancode;
43
44         scancode = PL050_ReadKeyboardData(0x60);
45         KB_HandleScancode( scancode );
46 }
47
48 void PL050_MouseHandler(int IRQ)
49 {
50         PS2Mouse_HandleInterrupt( PL050_ReadMouseData(0x60) );
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         Uint8   status;
62         Log_Log("8042", "Enabling Mouse...");
63         
64         
65         //PL050_WriteMouseData(0xD4);
66         //PL050_WriteMouseData(0xF6);   // Set Default Settings
67         PL050_WriteMouseData(0xD4);
68         PL050_WriteMouseData(0xF4);     // Enable Packets
69 }
70
71 static inline void PL050_WriteMouseData(Uint8 Data)
72 {
73          int    timeout = 10000;
74         while( --timeout && *(Uint32*)(MOUSE_BASE+1) & PL050_TXBUSY );
75         if(timeout)
76                 *(Uint32*)(MOUSE_BASE+2) = Data;
77         else
78                 Log_Error("PL050", "Write to mouse timed out");
79 }
80
81 static inline Uint8 PL050_ReadMouseData(void)
82 {
83         return *(Uint32*)(MOUSE_BASE+2);
84 }
85 static inline void PL050_WriteKeyboardData(Uint8 data)
86 {
87          int    timeout = 10000;
88         while( --timeout && *(Uint32*)(KEYBOARD_BASE+1) & PL050_TXBUSY );
89         if(timeout)
90                 *(Uint32*)(KEYBOARD_BASE+2) = Data;
91         else
92                 Log_Error("PL050", "Write to keyboard timed out");
93 }
94 static inline Uint8 PL050_ReadKeyboardData(void)
95 {
96         return *(Uint32*)(MOUSE_BASE+2);
97 }
98

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