Subversion Repositories HelenOS

Rev

Rev 2245 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2005-2007 Ondrej Palkovsky, Michal Kebrt, Petr Stepan
  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 arm32
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <interrupt.h>
  36. #include <ipc/irq.h>
  37. #include <console/chardev.h>
  38. #include <arch/drivers/gxemul.h>
  39. #include <console/console.h>
  40. #include <sysinfo/sysinfo.h>
  41. #include <print.h>
  42. #include <ddi/device.h>
  43.  
  44. /** Address of devices. */
  45. #define GXEMUL_VIDEORAM     0x10000000
  46. #define GXEMUL_KBD_ADDRESS  0x10000000
  47. #define GXEMUL_RTC      0x15000000
  48. #define GXEMUL_RTC_FREQ     0x15000100
  49. #define GXEMUL_RTC_ACK      0x15000110
  50. #define GXEMUL_IRQC     0x16000000
  51. #define GXEMUL_IRQC_MASK    0x16000004
  52. #define GXEMUL_IRQC_UNMASK  0x16000008
  53.  
  54. /** IRQs */
  55. #define GXEMUL_KBD_IRQ      2
  56. #define GXEMUL_TIMER_IRQ    4
  57.  
  58. static chardev_t console;
  59. static irq_t gxemul_irq;
  60. static irq_t gxemul_timer_irq;
  61.  
  62. static void gxemul_write(chardev_t *dev, const char ch);
  63. static void gxemul_enable(chardev_t *dev);
  64. static void gxemul_disable(chardev_t *dev);
  65. static char gxemul_do_read(chardev_t *dev);
  66.  
  67. static chardev_operations_t gxemul_ops = {
  68.     .resume = gxemul_enable,
  69.     .suspend = gxemul_disable,
  70.     .write = gxemul_write,
  71.     .read = gxemul_do_read,
  72. };
  73.  
  74. /** Putchar that works with gxemul */
  75. void gxemul_write(chardev_t *dev, const char ch)
  76. {
  77.     *((char *) GXEMUL_VIDEORAM) = ch;
  78. }
  79.  
  80. /* Called from getc(). */
  81. void gxemul_enable(chardev_t *dev)
  82. {
  83. //  cp0_unmask_int(GXEMUL_KBD_IRQ);
  84.     gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
  85. }
  86.  
  87. /* Called from getc(). */
  88. void gxemul_disable(chardev_t *dev)
  89. {
  90. //  cp0_mask_int(GXEMUL_KBD_IRQ);
  91.     gxemul_irqc_mask(GXEMUL_KBD_IRQ);
  92. }
  93.  
  94. /** Read character using polling, assume interrupts disabled */
  95. static char gxemul_do_read(chardev_t *dev)
  96. {
  97.     char ch;
  98.  
  99.     while (1) {
  100.         ch = *((volatile char *) GXEMUL_KBD_ADDRESS);
  101.         if (ch) {
  102.             if (ch == '\r')
  103.                 return '\n';
  104.             if (ch == 0x7f)
  105.                 return '\b';
  106.             return ch;
  107.         }
  108.     }
  109. }
  110.  
  111. /** Process keyboard interrupt. */
  112. static void gxemul_irq_handler(irq_t *irq, void *arg, ...)
  113. {
  114.     if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
  115.         ipc_irq_send_notif(irq);
  116.     else {
  117.         char ch = 0;
  118.        
  119.             ch = *((char *) GXEMUL_KBD_ADDRESS);
  120.             if (ch =='\r')
  121.                 ch = '\n';
  122.             if (ch == 0x7f)
  123.                 ch = '\b';
  124.             //chardev_push_character(&console, ch);
  125.             printf("%c", ch);
  126.     }
  127. }
  128.  
  129. static irq_ownership_t gxemul_claim(void)
  130. {
  131.     return IRQ_ACCEPT;
  132. }
  133.  
  134. void gxemul_kbd_grab(void)
  135. {
  136.     ipl_t ipl = interrupts_disable();
  137.     spinlock_lock(&msim_irq.lock);
  138.     gxemul_irq.notif_cfg.notify = false;
  139.     spinlock_unlock(&gxemul_irq.lock);
  140.     interrupts_restore(ipl);
  141. }
  142.  
  143. void gxemul_kbd_release(void)
  144. {
  145.     ipl_t ipl = interrupts_disable();
  146.     spinlock_lock(&gxemul_irq.lock);
  147.     if (gxemul_irq.notif_cfg.answerbox)
  148.         gxemul_irq.notif_cfg.notify = true;
  149.     spinlock_unlock(&gxemul_irq.lock);
  150.     interrupts_restore(ipl);
  151. }
  152.  
  153.  
  154. /** Return console object representing msim console */
  155. void gxemul_console(devno_t devno)
  156. {
  157.     chardev_initialize("msim_console", &console, &gxemul_ops);
  158.     stdin = &console;
  159.     stdout = &console;
  160.    
  161.     irq_initialize(&gxemul_irq);
  162.     gxemul_irq.devno = devno;
  163.     gxemul_irq.inr = GXEMUL_KBD_IRQ;
  164.     gxemul_irq.claim = gxemul_claim;
  165.     gxemul_irq.handler = gxemul_irq_handler;
  166.     irq_register(&gxemul_irq);
  167.    
  168. //  cp0_unmask_int(GXEMUL_KBD_IRQ);
  169.     gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
  170.    
  171.     sysinfo_set_item_val("kbd", NULL, true);
  172.     sysinfo_set_item_val("kbd.devno", NULL, devno);
  173.     sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ);
  174.     sysinfo_set_item_val("kbd.address.virtual", NULL, GXEMUL_KBD_ADDRESS);
  175. }
  176.  
  177. /** Return the mask of active interrupts. */
  178. inline uint32_t gxemul_irqc_get_sources(void)
  179. {
  180.     return *(uint32_t*) GXEMUL_IRQC;
  181. }
  182.  
  183. /** Masks interrupt.
  184.  *
  185.  * @param irq interrupt number
  186.  */
  187. inline void gxemul_irqc_mask(uint32_t irq)
  188. {
  189.     *(uint32_t*) GXEMUL_IRQC_MASK = irq;
  190. }
  191.  
  192. /** Unmasks interrupt.
  193.  *
  194.  * @param irq interrupt number
  195.  */
  196. inline void gxemul_irqc_unmask(uint32_t irq)
  197. {
  198.     *(uint32_t*) GXEMUL_IRQC_UNMASK = irq;
  199. }
  200.  
  201.  
  202. /** Starts gxemul Real Time Clock device, which asserts regular interrupts.
  203.  *
  204.  * @param frequency interrupts frequency (0 disables RTC)
  205.  */
  206. void gxemul_timer_start(uint32_t frequency)
  207. {
  208.     *(uint32_t*) GXEMUL_RTC_FREQ = frequency;
  209. }
  210.  
  211. static irq_ownership_t gxemul_timer_claim(void)
  212. {
  213.     return IRQ_ACCEPT;
  214. }
  215.  
  216. static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...)
  217. {
  218.     /* TODO time drifts ??
  219.     unsigned long drift;
  220.  
  221.     drift = cp0_count_read() - nextcount;
  222.     while (drift > cp0_compare_value) {
  223.         drift -= cp0_compare_value;
  224.         CPU->missed_clock_ticks++;
  225.     }
  226.     nextcount = cp0_count_read() + cp0_compare_value - drift;
  227.     cp0_compare_write(nextcount);
  228.     */
  229.  
  230.     /*
  231.     * We are holding a lock which prevents preemption.
  232.     * Release the lock, call clock() and reacquire the lock again.
  233.     */
  234.     spinlock_unlock(&irq->lock);
  235.     //clock();
  236.     puts(" ");
  237.     spinlock_lock(&irq->lock);
  238.  
  239.     /* acknowledge tick */
  240.     *(uint32_t*) GXEMUL_RTC_ACK = 0;
  241.    
  242.     /* TODO what's that? *
  243.     if (virtual_timer_fnc != NULL)
  244.         virtual_timer_fnc();
  245.     */
  246. }
  247.  
  248. /**
  249.  * Initializes and registers timer interrupt handler.
  250.  */
  251. void gxemul_timer_irq_init()
  252. {
  253.     irq_initialize(&gxemul_timer_irq);
  254.     gxemul_timer_irq.devno = device_assign_devno();
  255.     gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ;
  256.     gxemul_timer_irq.claim = gxemul_timer_claim;
  257.     gxemul_timer_irq.handler = gxemul_timer_irq_handler;
  258.  
  259.     irq_register(&gxemul_timer_irq);
  260. }
  261.  
  262.  
  263. /** @}
  264.  */
  265.