Subversion Repositories HelenOS

Rev

Rev 4610 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Martin Decky
  3.  * Copyright (c) 2009 Jiri Svoboda
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. /** @addtogroup genarch
  31.  * @{
  32.  */
  33. /** @file
  34.  */
  35.  
  36. #include <genarch/drivers/via-cuda/cuda.h>
  37. #include <console/chardev.h>
  38. #include <ddi/irq.h>
  39. #include <arch/asm.h>
  40. #include <mm/slab.h>
  41. #include <ddi/device.h>
  42. #include <synch/spinlock.h>
  43.  
  44. static void cuda_packet_handle(cuda_instance_t *instance, uint8_t *buf, size_t len);
  45. static void cuda_irq_listen(irq_t *irq);
  46. static void cuda_irq_receive(irq_t *irq);
  47. static void cuda_irq_rcv_end(irq_t *irq, void *buf, size_t *len);
  48.  
  49. /** B register fields */
  50. enum {
  51.     TREQ    = 0x08,
  52.     TACK    = 0x10,
  53.     TIP = 0x20
  54. };
  55.  
  56. /** IER register fields */
  57. enum {
  58.     IER_CLR = 0x00,
  59.     IER_SET = 0x80,
  60.  
  61.     SR_INT  = 0x04,
  62.     ALL_INT = 0x7f
  63. };
  64.  
  65. /** ACR register fields */
  66. enum {
  67.     SR_OUT  = 0x10
  68. };
  69.  
  70. #include <print.h>
  71. static irq_ownership_t cuda_claim(irq_t *irq)
  72. {
  73.     cuda_instance_t *instance = irq->instance;
  74.     cuda_t *dev = instance->cuda;
  75.     uint8_t ifr;
  76.  
  77.     ifr = pio_read_8(&dev->ifr);
  78.  
  79.     if ((ifr & SR_INT) == 0)
  80.         return IRQ_DECLINE;
  81.  
  82.     return IRQ_ACCEPT;
  83. }
  84.  
  85. static void cuda_irq_handler(irq_t *irq)
  86. {
  87.     cuda_instance_t *instance = irq->instance;
  88.     uint8_t rbuf[CUDA_RCV_BUF_SIZE];
  89.     size_t len;
  90.     bool handle;
  91.  
  92.     handle = false;
  93.     len = 0;
  94.  
  95.     spinlock_lock(&instance->dev_lock);
  96.  
  97.     /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
  98.     pio_write_8(&instance->cuda->ifr, SR_INT);
  99.  
  100.     switch (instance->xstate) {
  101.     case cx_listen: cuda_irq_listen(irq); break;
  102.     case cx_receive: cuda_irq_receive(irq); break;
  103.     case cx_rcv_end: cuda_irq_rcv_end(irq, rbuf, &len);
  104.         handle = true; break;
  105.     }
  106.  
  107.     spinlock_unlock(&instance->dev_lock);
  108.  
  109.     /* Handle an incoming packet. */
  110.     if (handle)
  111.         cuda_packet_handle(instance, rbuf, len);
  112. }
  113.  
  114. /** Interrupt in listen state.
  115.  *
  116.  * Start packet reception.
  117.  */
  118. static void cuda_irq_listen(irq_t *irq)
  119. {
  120.     cuda_instance_t *instance = irq->instance;
  121.     cuda_t *dev = instance->cuda;
  122.     uint8_t b;
  123.  
  124.     b = pio_read_8(&dev->b);
  125.  
  126.     if ((b & TREQ) != 0) {
  127.         printf("cuda_irq_listen: no TREQ?!\n");
  128.         return;
  129.     }
  130.  
  131.     pio_read_8(&dev->sr);
  132.     pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
  133.     instance->xstate = cx_receive;
  134. }
  135.  
  136. /** Interrupt in receive state.
  137.  *
  138.  * Receive next byte of packet.
  139.  */
  140. static void cuda_irq_receive(irq_t *irq)
  141. {
  142.     cuda_instance_t *instance = irq->instance;
  143.     cuda_t *dev = instance->cuda;
  144.     uint8_t b, data;
  145.  
  146.     data = pio_read_8(&dev->sr);
  147.     if (instance->bidx < CUDA_RCV_BUF_SIZE)
  148.         instance->rcv_buf[instance->bidx++] = data;
  149.  
  150.     b = pio_read_8(&dev->b);
  151.  
  152.     if ((b & TREQ) == 0) {
  153.         pio_write_8(&dev->b, b ^ TACK);
  154.     } else {
  155.         pio_write_8(&dev->b, b | TACK | TIP);
  156.         instance->xstate = cx_rcv_end;
  157.     }
  158. }
  159.  
  160. /** Interrupt in rcv_end state.
  161.  *
  162.  * Terminate packet reception. Either go back to listen state or start
  163.  * receiving another packet if CUDA has one for us.
  164.  */
  165. static void cuda_irq_rcv_end(irq_t *irq, void *buf, size_t *len)
  166. {
  167.     cuda_instance_t *instance = irq->instance;
  168.     cuda_t *dev = instance->cuda;
  169.     uint8_t data, b;
  170.  
  171.     b = pio_read_8(&dev->b);
  172.     data = pio_read_8(&dev->sr);
  173.  
  174.     instance->xstate = cx_listen;
  175.  
  176.     if ((b & TREQ) == 0) {
  177.         instance->xstate = cx_receive;
  178.         pio_write_8(&dev->b, b & ~TIP);
  179.     } else {
  180.         instance->xstate = cx_listen;
  181.     }
  182.  
  183.         memcpy(buf, instance->rcv_buf, instance->bidx);
  184.         *len = instance->bidx;
  185.     instance->bidx = 0;
  186. }
  187.  
  188. static void cuda_packet_handle(cuda_instance_t *instance, uint8_t *data, size_t len)
  189. {
  190.     if (data[0] != 0x00 || data[1] != 0x40 || (data[2] != 0x2c
  191.         && data[2] != 0x8c))
  192.         return;
  193.  
  194.     /* The packet contains one or two scancodes. */
  195.     if (data[3] != 0xff)
  196.         indev_push_character(instance->kbrdin, data[3]);       
  197.     if (data[4] != 0xff)
  198.         indev_push_character(instance->kbrdin, data[4]);
  199. }
  200.  
  201. cuda_instance_t *cuda_init(cuda_t *dev, inr_t inr, cir_t cir, void *cir_arg)
  202. {
  203.     cuda_instance_t *instance
  204.         = malloc(sizeof(cuda_instance_t), FRAME_ATOMIC);
  205.     if (instance) {
  206.         instance->cuda = dev;
  207.         instance->kbrdin = NULL;
  208.         instance->xstate = cx_listen;
  209.         instance->bidx = 0;
  210.  
  211.         spinlock_initialize(&instance->dev_lock, "cuda_dev");
  212.  
  213.         /* Disable all interrupts from CUDA. */
  214.         pio_write_8(&dev->ier, IER_CLR | ALL_INT);
  215.  
  216.         irq_initialize(&instance->irq);
  217.         instance->irq.devno = device_assign_devno();
  218.         instance->irq.inr = inr;
  219.         instance->irq.claim = cuda_claim;
  220.         instance->irq.handler = cuda_irq_handler;
  221.         instance->irq.instance = instance;
  222.         instance->irq.cir = cir;
  223.         instance->irq.cir_arg = cir_arg;
  224.         instance->irq.preack = true;
  225.     }
  226.    
  227.     return instance;
  228. }
  229.  
  230. void cuda_wire(cuda_instance_t *instance, indev_t *kbrdin)
  231. {
  232.     cuda_t *dev = instance->cuda;
  233.  
  234.     ASSERT(instance);
  235.     ASSERT(kbrdin);
  236.  
  237.     instance->kbrdin = kbrdin;
  238.     irq_register(&instance->irq);
  239.  
  240.     /* Enable SR interrupt. */
  241.     pio_write_8(&dev->ier, TIP | TREQ);
  242.     pio_write_8(&dev->ier, IER_SET | SR_INT);
  243. }
  244.  
  245. /** @}
  246.  */
  247.