| [f761f1eb] | 1 | /*
|
|---|
| [4c7257b] | 2 | * Copyright (c) 2009 Jakub Jermar
|
|---|
| [f761f1eb] | 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 |
|
|---|
| [3c5006a0] | 29 | /** @addtogroup genarch
|
|---|
| [b45c443] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| [3c5006a0] | 32 | /**
|
|---|
| [cc73a8a1] | 33 | * @file
|
|---|
| [411b6a6] | 34 | * @brief i8042 processor driver
|
|---|
| [3c5006a0] | 35 | *
|
|---|
| [411b6a6] | 36 | * It takes care of the i8042 serial communication.
|
|---|
| [b45c443] | 37 | */
|
|---|
| 38 |
|
|---|
| [411b6a6] | 39 | #include <genarch/drivers/i8042/i8042.h>
|
|---|
| [91825d90] | 40 | #include <genarch/drivers/legacy/ia32/io.h>
|
|---|
| [f761f1eb] | 41 | #include <arch/asm.h>
|
|---|
| [607c5f9] | 42 | #include <console/chardev.h>
|
|---|
| [411b6a6] | 43 | #include <mm/slab.h>
|
|---|
| [6ccb238] | 44 |
|
|---|
| [481c520] | 45 | #define i8042_SET_COMMAND 0x60
|
|---|
| [a832dd7] | 46 | #define i8042_COMMAND 0x69
|
|---|
| [02f441c0] | 47 |
|
|---|
| 48 | #define i8042_BUFFER_FULL_MASK 0x01
|
|---|
| [c43b1db2] | 49 | #define i8042_WAIT_MASK 0x02
|
|---|
| [607c5f9] | 50 |
|
|---|
| [c9b550b] | 51 | static irq_ownership_t i8042_claim(irq_t *irq)
|
|---|
| [41d33ac] | 52 | {
|
|---|
| [c9b550b] | 53 | i8042_instance_t *i8042_instance = irq->instance;
|
|---|
| [4544884] | 54 | i8042_t *dev = i8042_instance->i8042;
|
|---|
| 55 | if (pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK)
|
|---|
| 56 | return IRQ_ACCEPT;
|
|---|
| 57 | else
|
|---|
| 58 | return IRQ_DECLINE;
|
|---|
| [cea12e9] | 59 | }
|
|---|
| [41d33ac] | 60 |
|
|---|
| [6cd9aa6] | 61 | static void i8042_irq_handler(irq_t *irq)
|
|---|
| [cea12e9] | 62 | {
|
|---|
| [4544884] | 63 | i8042_instance_t *instance = irq->instance;
|
|---|
| 64 | i8042_t *dev = instance->i8042;
|
|---|
| 65 |
|
|---|
| 66 | uint8_t data;
|
|---|
| 67 | uint8_t status;
|
|---|
| [cea12e9] | 68 |
|
|---|
| [4544884] | 69 | if (((status = pio_read_8(&dev->status)) & i8042_BUFFER_FULL_MASK)) {
|
|---|
| 70 | data = pio_read_8(&dev->data);
|
|---|
| [cea12e9] | 71 |
|
|---|
| [411b6a6] | 72 | if (instance->devout)
|
|---|
| 73 | chardev_push_character(instance->devout, data);
|
|---|
| [cea12e9] | 74 | }
|
|---|
| 75 | }
|
|---|
| [6ccb238] | 76 |
|
|---|
| [cea12e9] | 77 | /** Initialize i8042. */
|
|---|
| [5b0ae4be] | 78 | bool
|
|---|
| [411b6a6] | 79 | i8042_init(i8042_t *dev, devno_t devno, inr_t inr, chardev_t *devout)
|
|---|
| [cea12e9] | 80 | {
|
|---|
| [5b0ae4be] | 81 | i8042_instance_t *instance;
|
|---|
| [4544884] | 82 |
|
|---|
| [5b0ae4be] | 83 | instance = malloc(sizeof(i8042_instance_t), FRAME_ATOMIC);
|
|---|
| 84 | if (!instance)
|
|---|
| 85 | return false;
|
|---|
| 86 |
|
|---|
| 87 | instance->devno = devno;
|
|---|
| 88 | instance->i8042 = dev;
|
|---|
| [411b6a6] | 89 | instance->devout = devout;
|
|---|
| [16d71f41] | 90 |
|
|---|
| [5b0ae4be] | 91 | irq_initialize(&instance->irq);
|
|---|
| 92 | instance->irq.devno = devno;
|
|---|
| 93 | instance->irq.inr = inr;
|
|---|
| 94 | instance->irq.claim = i8042_claim;
|
|---|
| 95 | instance->irq.handler = i8042_irq_handler;
|
|---|
| 96 | instance->irq.instance = instance;
|
|---|
| 97 | irq_register(&instance->irq);
|
|---|
| [af75db9] | 98 |
|
|---|
| [8b473ce] | 99 | /*
|
|---|
| [c624b96] | 100 | * Clear input buffer.
|
|---|
| 101 | */
|
|---|
| [d063365] | 102 | while (pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK)
|
|---|
| [4544884] | 103 | (void) pio_read_8(&dev->data);
|
|---|
| [cea12e9] | 104 |
|
|---|
| [5b0ae4be] | 105 | return true;
|
|---|
| [30ab05f] | 106 | }
|
|---|
| 107 |
|
|---|
| [3c5006a0] | 108 | /** @}
|
|---|
| [b45c443] | 109 | */
|
|---|