printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / pc80 / keyboard.c
index c60a2fad231194801c7db5341355d42cb7ca186f..32e2576facb7e3828332b575696d2d015d9143dd 100644 (file)
@@ -1,8 +1,9 @@
 /*
  * This file is part of the coreboot project.
  *
+ * Copyright (C) 2009 coresystems GmbH
  * Copyright (C) 2008 Advanced Micro Devices, Inc.
- * Copyright (C) ???? Ollie Lo <ollielo@hotmail.com>
+ * Copyright (C) 2003 Ollie Lo <ollielo@hotmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <pc80/keyboard.h>
 #include <device/device.h>
 #include <arch/io.h>
+#include <delay.h>
+
+#define KBD_DATA       0x60
+#define KBD_COMMAND    0x64
+#define KBD_STATUS     0x64
+#define   KBD_IBF      (1 << 1) // 1: input buffer full (data ready for ec)
+#define   KBD_OBF      (1 << 0) // 1: output buffer full (data ready for host)
+
+// Keyboard Controller Commands
+#define KBC_CMD_READ_COMMAND   0x20 // Read command byte
+#define KBC_CMD_WRITE_COMMAND  0x60 // Write command byte
+#define KBC_CMD_SELF_TEST      0xAA // Controller self-test
+
+/* The Keyboard controller command byte
+ *  BIT        | Description
+ *  ----+-------------------------------------------------------
+ *   7  | reserved, must be zero
+ *   6  | XT Translation, (1 = on, 0 = off)
+ *   5  | Disable Mouse Port (1 = disable, 0 = enable)
+ *   4  | Disable Keyboard Port (1 = disable, 0 = enable)
+ *   3  | reserved, must be zero
+ *   2  | System Flag (1 = self-test passed. DO NOT SET TO ZERO)
+ *   1  | Mouse Port Interrupts (1 = enable, 0 = disable)
+ *   0  | Keyboard Port Interrupts (1 = enable, 0 = disable)
+ */
+
+// Keyboard Controller Replies
+#define KBC_REPLY_SELFTEST_OK  0x55 // controller self-test succeeded
+
+//
+// Keyboard Replies
+//
+#define KBD_REPLY_POR          0xAA    // Power on reset
+#define KBD_REPLY_ACK          0xFA    // Command ACK
+#define KBD_REPLY_RESEND       0xFE    // Command NACK, send command again
+
+/* Wait 200ms for keyboard controller answers */
+#define KBC_TIMEOUT_IN_MS 200
 
 static int kbc_input_buffer_empty(void)
 {
        u32 timeout;
-       for(timeout = 1000000; timeout && (inb(0x64) & 0x02); timeout--) {
-               inb(0x80);
+       for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--) {
+               mdelay(1);
        }
 
        if (!timeout) {
-               printk_err("Unexpected Keyboard controller input buffer full\n");
+               printk(BIOS_WARNING, "Unexpected Keyboard controller input buffer full\n");
        }
        return !!timeout;
 }
@@ -41,12 +80,12 @@ static int kbc_input_buffer_empty(void)
 static int kbc_output_buffer_full(void)
 {
        u32 timeout;
-       for(timeout = 1000000; timeout && ((inb(0x64) & 0x01) == 0); timeout--) {
-               inb(0x80);
+       for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--) {
+               mdelay(1);
        }
 
        if (!timeout) {
-               printk_err("Keyboard controller output buffer result timeout\n");
+               printk(BIOS_WARNING, "Keyboard controller output buffer result timeout\n");
        }
        return !!timeout;
 }
@@ -55,17 +94,50 @@ static int kbc_output_buffer_full(void)
 static int kbc_cleanup_buffers(void)
 {
        u32 timeout;
-       for(timeout = 1000000; timeout && (inb(0x64) & 0x03); timeout--) {
-               inb(0x60);
+       for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
+               mdelay(1);
+               inb(KBD_DATA);
        }
 
        if (!timeout) {
-               printk_err("Couldn't cleanup the keyboard controller buffers\n");
-               printk_err("0x64: 0x%x, 0x60: 0x%x\n", inb(0x64), inb(0x60));
+               printk(BIOS_ERR, "Couldn't cleanup the keyboard controller buffers\n");
+               printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
+                               KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
        }
+
        return !!timeout;
 }
 
+static int kbc_self_test(void)
+{
+       u8 self_test;
+
+       /* Clean up any junk that might have been in the KBC.
+        * Both input and output buffers must be empty.
+        */
+       if (!kbc_cleanup_buffers())
+               return 0;
+
+       /* reset/self test 8042 - send cmd 0xAA */
+       outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
+
+       if (!kbc_output_buffer_full()) {
+               /* There probably is no keyboard controller. */
+               printk(BIOS_ERR, "Could not reset keyboard controller.\n");
+               return 0;
+       }
+
+       /* read self-test result, 0x55 is returned in the output buffer */
+       self_test = inb(KBD_DATA);
+
+       if (self_test != 0x55) {
+               printk(BIOS_ERR, "Keyboard Controller self-test failed: 0x%x\n",
+                               self_test);
+               return 0;
+       }
+
+       return 1;
+}
 
 static u8 send_keyboard(u8 command)
 {
@@ -74,63 +146,62 @@ static u8 send_keyboard(u8 command)
 
        do {
                if (!kbc_input_buffer_empty()) return 0;
-               outb(command, 0x60);
-               if (!kbc_output_buffer_full()) return 0;
-               regval = inb(0x60);
+               outb(command, KBD_DATA);
+               if (!kbc_output_buffer_full()) {
+                       printk(BIOS_ERR, "Could not send keyboard command %02x\n",
+                                       command);
+                       return 0;
+               }
+               regval = inb(KBD_DATA);
                --resend;
-       } while (regval == 0xFE && resend > 0);
+       } while (regval == KBD_REPLY_RESEND && resend > 0);
 
        return regval;
 }
 
-
-static void pc_keyboard_init(struct pc_keyboard *keyboard)
+void pc_keyboard_init(struct pc_keyboard *keyboard)
 {
        u8 regval;
-       u8 resend;
-       printk_debug("Keyboard init...\n");
+       printk(BIOS_DEBUG, "Keyboard init...\n");
 
-       /* clean up any junk that might have been in the kbc */
-       if (!kbc_cleanup_buffers()) return;
+       /* Run a keyboard controller self-test */
+       if (!kbc_self_test())
+               return;
 
-       /* reset/self test 8042 - send cmd 0xAA,  */
+       /* Enable keyboard interface - No IRQ */
        if (!kbc_input_buffer_empty()) return;
-       outb(0xAA, 0x64);
-       if (!kbc_output_buffer_full()) return;
-
-       /* read self-test result, 0x55 is returned in the output buffer (0x60) */
-       if ((regval = inb(0x60) != 0x55)) {
-               printk_err("Keyboard Controller selftest failed: 0x%x\n", regval);
+       outb(0x60, KBD_COMMAND);
+       if (!kbc_input_buffer_empty()) return;
+       outb(0x20, KBD_DATA);   /* send cmd: enable keyboard */
+       if (!kbc_input_buffer_empty()) {
+               printk(BIOS_INFO, "Timeout while enabling keyboard\n");
                return;
        }
 
-       /* Enable keyboard interface - No IRQ*/
-       resend = 10;
-       regval = 0;
-       do {
-               if (!kbc_input_buffer_empty()) return;
-               outb(0x60, 0x64);
-               if (!kbc_input_buffer_empty()) return;
-               outb(0x20, 0x60);       /* send cmd: enable keyboard */
-               if ((inb(0x64) & 0x01)) {
-                       regval = inb(0x60);
-               }
-               --resend;
-       } while (regval == 0xFE && resend > 0);
-
        /* clean up any junk that might have been in the keyboard */
        if (!kbc_cleanup_buffers()) return;
 
        /* reset keyboard and self test (keyboard side) */
        regval = send_keyboard(0xFF);
-       if (regval != 0xFA) {
-               printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
+       if (regval == KBD_REPLY_RESEND) {
+               /* keeps sending RESENDs, probably no keyboard. */
+               printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
+               return;
+       }
+
+       if (regval != KBD_REPLY_ACK) {
+               printk(BIOS_ERR, "Keyboard selftest failed ACK: 0x%x\n", regval);
+               return;
+       }
+
+       if (!kbc_output_buffer_full()) {
+               printk(BIOS_ERR, "Timeout waiting for keyboard after reset.\n");
                return;
        }
-       if (!kbc_output_buffer_full()) return;
-       regval = inb(0x60);
+
+       regval = inb(KBD_DATA);
        if (regval != 0xAA) {
-               printk_err("Keyboard selftest failed: 0x%x\n", regval);
+               printk(BIOS_ERR, "Keyboard selftest failed: 0x%x\n", regval);
                return;
        }
 
@@ -141,51 +212,39 @@ static void pc_keyboard_init(struct pc_keyboard *keyboard)
 
        /* disable the keyboard */
        regval = send_keyboard(0xF5);
-       if (regval != 0xFA) {
-               printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
+       if (regval != KBD_REPLY_ACK) {
+               printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
                return;
        }
 
        /* Set scancode command */
        regval = send_keyboard(0xF0);
-       if (regval != 0xFA) {
-               printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
+       if (regval != KBD_REPLY_ACK) {
+               printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
                return;
        }
        /* Set scancode mode 2 */
        regval = send_keyboard(0x02);
-       if (regval != 0xFA) {
-               printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
+       if (regval != KBD_REPLY_ACK) {
+               printk(BIOS_ERR, "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
                return;
        }
 
        /* enable the keyboard */
        regval = send_keyboard(0xF4);
-       if (regval != 0xFA) {
-               printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
+       if (regval != KBD_REPLY_ACK) {
+               printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
                return;
        }
 
        /* All is well - enable keyboard interface */
-       resend = 10;
-       regval = 0;
-       do {
-               if (!kbc_input_buffer_empty()) return;
-               outb(0x60, 0x64);
-               if (!kbc_input_buffer_empty()) return;
-               outb(0x61, 0x60);       /* send cmd: enable keyboard and IRQ 1 */
-               if ((inb(0x64) & 0x01)) {
-                       regval = inb(0x60);
-               }
-               --resend;
-       } while (regval == 0xFE && resend > 0);
-}
-
-
-void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
-{
-       if ((port0 == 0x60) && (port1 == 0x64)) {
-               pc_keyboard_init(kbd);
+       if (!kbc_input_buffer_empty()) return;
+       outb(0x60, KBD_COMMAND);
+       if (!kbc_input_buffer_empty()) return;
+       outb(0x61, KBD_DATA);   /* send cmd: enable keyboard and IRQ 1 */
+       if (kbc_output_buffer_full()) {
+               printk(BIOS_ERR, "Timeout during final keyboard enable\n");
+               return;
        }
 }
 
@@ -196,24 +255,16 @@ void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
  */
 void set_kbc_ps2_mode(void)
 {
-       /* clean up any junk that might have been in the kbc */
-       if (!kbc_cleanup_buffers()) return;
-
-       /* reset/self test 8042 before we can do anything */
-       if (!kbc_input_buffer_empty()) return;
-       outb(0xAA, 0x64);
-       if (!kbc_output_buffer_full()) return;
-
-       /* read self-test result, 0x55 is returned in the output buffer (0x60) */
-       if ((inb(0x60) != 0x55)) {
-               printk_err("Keyboard Controller selftest failed\n");
+       /* Run a keyboard controller self-test */
+       if (!kbc_self_test())
                return;
-       }
 
        /* Support PS/2 mode */
        if (!kbc_input_buffer_empty()) return;
-       outb(0xcb, 0x64);
+       outb(0xcb, KBD_COMMAND);
+
        if (!kbc_input_buffer_empty()) return;
-       outb(0x01, 0x60);
+       outb(0x01, KBD_DATA);
+
        kbc_cleanup_buffers();
 }