| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup genarch
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief i8042 processor driver
|
|---|
| 35 | *
|
|---|
| 36 | * It takes care of the i8042 serial communication.
|
|---|
| 37 | *
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | #include <genarch/drivers/i8042/i8042.h>
|
|---|
| 41 | #include <genarch/drivers/legacy/ia32/io.h>
|
|---|
| 42 | #include <arch/asm.h>
|
|---|
| 43 | #include <console/chardev.h>
|
|---|
| 44 | #include <mm/slab.h>
|
|---|
| 45 | #include <ddi/device.h>
|
|---|
| 46 | #include <time/delay.h>
|
|---|
| 47 |
|
|---|
| 48 | #define i8042_SET_COMMAND 0x60
|
|---|
| 49 | #define i8042_COMMAND 0x69
|
|---|
| 50 | #define i8042_CPU_RESET 0xfe
|
|---|
| 51 |
|
|---|
| 52 | #define i8042_BUFFER_FULL_MASK 0x01
|
|---|
| 53 | #define i8042_WAIT_MASK 0x02
|
|---|
| 54 |
|
|---|
| 55 | #define i8042_TIMEOUT 65536
|
|---|
| 56 |
|
|---|
| 57 | static irq_ownership_t i8042_claim(irq_t *irq)
|
|---|
| 58 | {
|
|---|
| 59 | i8042_instance_t *i8042_instance = irq->instance;
|
|---|
| 60 | i8042_t *dev = i8042_instance->i8042;
|
|---|
| 61 |
|
|---|
| 62 | if (pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK)
|
|---|
| 63 | return IRQ_ACCEPT;
|
|---|
| 64 | else
|
|---|
| 65 | return IRQ_DECLINE;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | static void i8042_irq_handler(irq_t *irq)
|
|---|
| 69 | {
|
|---|
| 70 | i8042_instance_t *instance = irq->instance;
|
|---|
| 71 | i8042_t *dev = instance->i8042;
|
|---|
| 72 |
|
|---|
| 73 | if (pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK) {
|
|---|
| 74 | uint8_t data = pio_read_8(&dev->data);
|
|---|
| 75 | indev_push_character(instance->kbrdin, data);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**< Clear input buffer. */
|
|---|
| 80 | static void i8042_clear_buffer(i8042_t *dev)
|
|---|
| 81 | {
|
|---|
| 82 | for (uint32_t i = 0; i < i8042_TIMEOUT; i++) {
|
|---|
| 83 | if ((pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK) == 0)
|
|---|
| 84 | break;
|
|---|
| 85 |
|
|---|
| 86 | (void) pio_read_8(&dev->data);
|
|---|
| 87 | delay(50); /* 50 us think time */
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static void i8042_send_command(i8042_t *dev, uint8_t cmd)
|
|---|
| 92 | {
|
|---|
| 93 | for (uint32_t i = 0; i < i8042_TIMEOUT; i++) {
|
|---|
| 94 | if ((pio_read_8(&dev->status) & i8042_WAIT_MASK) == 0)
|
|---|
| 95 | break;
|
|---|
| 96 |
|
|---|
| 97 | delay(50); /* 50 us think time */
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | pio_write_8(&dev->status, cmd);
|
|---|
| 101 | delay(10000); /* 10 ms think time */
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /** Initialize i8042. */
|
|---|
| 105 | i8042_instance_t *i8042_init(i8042_t *dev, inr_t inr)
|
|---|
| 106 | {
|
|---|
| 107 | i8042_instance_t *instance =
|
|---|
| 108 | malloc(sizeof(i8042_instance_t), FRAME_ATOMIC);
|
|---|
| 109 | if (instance) {
|
|---|
| 110 | instance->i8042 = dev;
|
|---|
| 111 | instance->kbrdin = NULL;
|
|---|
| 112 |
|
|---|
| 113 | irq_initialize(&instance->irq);
|
|---|
| 114 | instance->irq.devno = device_assign_devno();
|
|---|
| 115 | instance->irq.inr = inr;
|
|---|
| 116 | instance->irq.claim = i8042_claim;
|
|---|
| 117 | instance->irq.handler = i8042_irq_handler;
|
|---|
| 118 | instance->irq.instance = instance;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | return instance;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void i8042_wire(i8042_instance_t *instance, indev_t *kbrdin)
|
|---|
| 125 | {
|
|---|
| 126 | ASSERT(instance);
|
|---|
| 127 | ASSERT(kbrdin);
|
|---|
| 128 |
|
|---|
| 129 | i8042_clear_buffer(instance->i8042);
|
|---|
| 130 |
|
|---|
| 131 | instance->kbrdin = kbrdin;
|
|---|
| 132 | irq_register(&instance->irq);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* Reset CPU by pulsing pin 0 */
|
|---|
| 136 | void i8042_cpu_reset(i8042_t *dev)
|
|---|
| 137 | {
|
|---|
| 138 | interrupts_disable();
|
|---|
| 139 | i8042_clear_buffer(dev);
|
|---|
| 140 | i8042_send_command(dev, i8042_CPU_RESET);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** @}
|
|---|
| 144 | */
|
|---|