Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2007 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 arm32gxemul
  30.  * @{
  31.  */
  32. /** @file
  33.  *  @brief GXemul drivers.
  34.  */
  35.  
  36. #include <interrupt.h>
  37. #include <ipc/irq.h>
  38. #include <console/chardev.h>
  39. #include <arch/drivers/gxemul.h>
  40. #include <console/console.h>
  41. #include <sysinfo/sysinfo.h>
  42. #include <print.h>
  43. #include <ddi/device.h>
  44. #include <mm/page.h>
  45. #include <arch/machine.h>
  46. #include <arch/debug/print.h>
  47. #include <genarch/fb/fb.h>
  48. #include <genarch/fb/visuals.h>
  49.  
  50. /* Addresses of devices. */
  51. #define GXEMUL_VIDEORAM            0x10000000
  52. #define GXEMUL_KBD                 0x10000000
  53. #define GXEMUL_HALT_OFFSET         0x10
  54. #define GXEMUL_RTC                 0x15000000
  55. #define GXEMUL_RTC_FREQ_OFFSET     0x100
  56. #define GXEMUL_RTC_ACK_OFFSET      0x110
  57. #define GXEMUL_IRQC                0x16000000
  58. #define GXEMUL_IRQC_MASK_OFFSET    0x4
  59. #define GXEMUL_IRQC_UNMASK_OFFSET  0x8
  60. #define GXEMUL_MP                  0x11000000
  61. #define GXEMUL_MP_MEMSIZE_OFFSET   0x0090
  62. #define GXEMUL_FB                  0x12000000
  63.  
  64. /* IRQs */
  65. #define GXEMUL_KBD_IRQ      2
  66. #define GXEMUL_TIMER_IRQ    4
  67.  
  68. static gxemul_hw_map_t gxemul_hw_map;
  69. static chardev_t console;
  70. static irq_t gxemul_console_irq;
  71. static irq_t gxemul_timer_irq;
  72.  
  73. static bool hw_map_init_called = false;
  74.  
  75. static void gxemul_kbd_enable(chardev_t *dev);
  76. static void gxemul_kbd_disable(chardev_t *dev);
  77. static void gxemul_write(chardev_t *dev, const char ch);
  78. static char gxemul_do_read(chardev_t *dev);
  79.  
  80. static chardev_operations_t gxemul_ops = {
  81.     .resume = gxemul_kbd_enable,
  82.     .suspend = gxemul_kbd_disable,
  83.     .write = gxemul_write,
  84.     .read = gxemul_do_read,
  85. };
  86.  
  87.  
  88. /** Returns the mask of active interrupts. */
  89. static inline uint32_t gxemul_irqc_get_sources(void)
  90. {
  91.     return *((uint32_t *) gxemul_hw_map.irqc);
  92. }
  93.  
  94.  
  95. /** Masks interrupt.
  96.  *
  97.  * @param irq interrupt number
  98.  */
  99. static inline void gxemul_irqc_mask(uint32_t irq)
  100. {
  101.     *((uint32_t *) gxemul_hw_map.irqc_mask) = irq;
  102. }
  103.  
  104.  
  105. /** Unmasks interrupt.
  106.  *
  107.  * @param irq interrupt number
  108.  */
  109. static inline void gxemul_irqc_unmask(uint32_t irq)
  110. {
  111.     *((uint32_t *) gxemul_hw_map.irqc_unmask) = irq;
  112. }
  113.  
  114. /** Initializes the gxemul testarm frame buffer */
  115. void gxemul_fb_init(void)
  116. {
  117.     fb_init(gxemul_get_fb_address(), 640, 480, 1920, VISUAL_RGB_8_8_8);
  118. }
  119.  
  120. /** Initializes #gxemul_hw_map. */
  121. void gxemul_hw_map_init(void)
  122. {
  123.     gxemul_hw_map.videoram = hw_map(GXEMUL_VIDEORAM, PAGE_SIZE);
  124.     gxemul_hw_map.kbd = hw_map(GXEMUL_KBD, PAGE_SIZE);
  125.     gxemul_hw_map.rtc = hw_map(GXEMUL_RTC, PAGE_SIZE);
  126.     gxemul_hw_map.irqc = hw_map(GXEMUL_IRQC, PAGE_SIZE);
  127.  
  128.     gxemul_hw_map.rtc_freq = gxemul_hw_map.rtc + GXEMUL_RTC_FREQ_OFFSET;
  129.     gxemul_hw_map.rtc_ack = gxemul_hw_map.rtc + GXEMUL_RTC_ACK_OFFSET;
  130.     gxemul_hw_map.irqc_mask = gxemul_hw_map.irqc + GXEMUL_IRQC_MASK_OFFSET;
  131.     gxemul_hw_map.irqc_unmask = gxemul_hw_map.irqc +
  132.         GXEMUL_IRQC_UNMASK_OFFSET;
  133.  
  134.     hw_map_init_called = true;
  135. }
  136.  
  137.  
  138. /** Putchar that works with gxemul.
  139.  *
  140.  * @param dev Not used.
  141.  * @param ch Characted to be printed.
  142.  */
  143. static void gxemul_write(chardev_t *dev, const char ch)
  144. {
  145.     *((char *) gxemul_hw_map.videoram) = ch;
  146. }
  147.  
  148. /** Enables gxemul keyboard (interrupt unmasked).
  149.  *
  150.  * @param dev Not used.
  151.  *
  152.  * Called from getc().
  153.  */
  154. static void gxemul_kbd_enable(chardev_t *dev)
  155. {
  156.     gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
  157. }
  158.  
  159. /** Disables gxemul keyboard (interrupt masked).
  160.  *
  161.  * @param dev not used
  162.  *
  163.  * Called from getc().
  164.  */
  165. static void gxemul_kbd_disable(chardev_t *dev)
  166. {
  167.     gxemul_irqc_mask(GXEMUL_KBD_IRQ);
  168. }
  169.  
  170. /** Read character using polling, assume interrupts disabled.
  171.  *
  172.  *  @param dev Not used.
  173.  */
  174. static char gxemul_do_read(chardev_t *dev)
  175. {
  176.     char ch;
  177.  
  178.     while (1) {
  179.         ch = *((volatile char *) gxemul_hw_map.kbd);
  180.         if (ch) {
  181.             if (ch == '\r')
  182.                 return '\n';
  183.             if (ch == 0x7f)
  184.                 return '\b';
  185.             return ch;
  186.         }
  187.     }
  188. }
  189.  
  190. /** Process keyboard interrupt.
  191.  *  
  192.  *  @param irq IRQ information.
  193.  *  @param arg Not used.
  194.  */
  195. static void gxemul_irq_handler(irq_t *irq, void *arg, ...)
  196. {
  197.     if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox)) {
  198.         ipc_irq_send_notif(irq);
  199.     } else {
  200.         char ch = 0;
  201.        
  202.         ch = *((char *) gxemul_hw_map.kbd);
  203.         if (ch == '\r') {
  204.             ch = '\n';
  205.         }
  206.         if (ch == 0x7f) {
  207.             ch = '\b';
  208.         }
  209.         chardev_push_character(&console, ch);
  210.     }
  211. }
  212.  
  213. static irq_ownership_t gxemul_claim(void)
  214. {
  215.     return IRQ_ACCEPT;
  216. }
  217.  
  218.  
  219. /** Acquire console back for kernel. */
  220. void gxemul_grab_console(void)
  221. {
  222.     ipl_t ipl = interrupts_disable();
  223.     spinlock_lock(&gxemul_console_irq.lock);
  224.     gxemul_console_irq.notif_cfg.notify = false;
  225.     spinlock_unlock(&gxemul_console_irq.lock);
  226.     interrupts_restore(ipl);
  227. }
  228.  
  229. /** Return console to userspace. */
  230. void gxemul_release_console(void)
  231. {
  232.     ipl_t ipl = interrupts_disable();
  233.     spinlock_lock(&gxemul_console_irq.lock);
  234.     if (gxemul_console_irq.notif_cfg.answerbox) {
  235.         gxemul_console_irq.notif_cfg.notify = true;
  236.     }
  237.     spinlock_unlock(&gxemul_console_irq.lock);
  238.     interrupts_restore(ipl);
  239. }
  240.  
  241. /** Initializes console object representing gxemul console.
  242.  *
  243.  *  @param devno device number.
  244.  */
  245. void gxemul_console_init(devno_t devno)
  246. {
  247.     chardev_initialize("gxemul_console", &console, &gxemul_ops);
  248.     stdin = &console;
  249.     stdout = &console;
  250.    
  251.     irq_initialize(&gxemul_console_irq);
  252.     gxemul_console_irq.devno = devno;
  253.     gxemul_console_irq.inr = GXEMUL_KBD_IRQ;
  254.     gxemul_console_irq.claim = gxemul_claim;
  255.     gxemul_console_irq.handler = gxemul_irq_handler;
  256.     irq_register(&gxemul_console_irq);
  257.    
  258.     gxemul_irqc_unmask(GXEMUL_KBD_IRQ);
  259.    
  260.     sysinfo_set_item_val("kbd", NULL, true);
  261.     sysinfo_set_item_val("kbd.devno", NULL, devno);
  262.     sysinfo_set_item_val("kbd.inr", NULL, GXEMUL_KBD_IRQ);
  263.     sysinfo_set_item_val("kbd.address.virtual", NULL, gxemul_hw_map.kbd);
  264. }
  265.  
  266. /** Starts gxemul Real Time Clock device, which asserts regular interrupts.
  267.  *
  268.  * @param frequency Interrupts frequency (0 disables RTC).
  269.  */
  270. static void gxemul_timer_start(uint32_t frequency)
  271. {
  272.     *((uint32_t*) gxemul_hw_map.rtc_freq) = frequency;
  273. }
  274.  
  275. static irq_ownership_t gxemul_timer_claim(void)
  276. {
  277.     return IRQ_ACCEPT;
  278. }
  279.  
  280. /** Timer interrupt handler.
  281.  *
  282.  * @param irq Interrupt information.
  283.  * @param arg Not used.
  284.  */
  285. static void gxemul_timer_irq_handler(irq_t *irq, void *arg, ...)
  286. {
  287.     /*
  288.     * We are holding a lock which prevents preemption.
  289.     * Release the lock, call clock() and reacquire the lock again.
  290.     */
  291.     spinlock_unlock(&irq->lock);
  292.     clock();
  293.     spinlock_lock(&irq->lock);
  294.  
  295.     /* acknowledge tick */
  296.     *((uint32_t*) gxemul_hw_map.rtc_ack) = 0;
  297. }
  298.  
  299. /** Initializes and registers timer interrupt handler. */
  300. static void gxemul_timer_irq_init(void)
  301. {
  302.     irq_initialize(&gxemul_timer_irq);
  303.     gxemul_timer_irq.devno = device_assign_devno();
  304.     gxemul_timer_irq.inr = GXEMUL_TIMER_IRQ;
  305.     gxemul_timer_irq.claim = gxemul_timer_claim;
  306.     gxemul_timer_irq.handler = gxemul_timer_irq_handler;
  307.  
  308.     irq_register(&gxemul_timer_irq);
  309. }
  310.  
  311.  
  312. /** Starts timer.
  313.  *
  314.  * Initiates regular timer interrupts after initializing
  315.  * corresponding interrupt handler.
  316.  */
  317. void gxemul_timer_irq_start(void)
  318. {
  319.     gxemul_timer_irq_init();
  320.     gxemul_timer_start(GXEMUL_TIMER_FREQ);
  321. }
  322.  
  323. /** Returns the size of emulated memory.
  324.  *
  325.  * @return Size in bytes.
  326.  */
  327. size_t gxemul_get_memory_size(void)
  328. {
  329.     return  *((int *) (GXEMUL_MP + GXEMUL_MP_MEMSIZE_OFFSET));
  330. }
  331.  
  332. /** Prints a character.
  333.  *
  334.  *  @param ch Character to be printed.
  335.  */
  336. void gxemul_debug_putc(char ch)
  337. {
  338.     char *addr = 0;
  339.     if (!hw_map_init_called) {
  340.         addr = (char *) GXEMUL_KBD;
  341.     } else {
  342.         addr = (char *) gxemul_hw_map.videoram;
  343.     }
  344.  
  345.     *(addr) = ch;
  346. }
  347.  
  348. /** Stops gxemul. */
  349. void gxemul_cpu_halt(void)
  350. {
  351.     char * addr = 0;
  352.     if (!hw_map_init_called) {
  353.         addr = (char *) GXEMUL_KBD;
  354.     } else {
  355.         addr = (char *) gxemul_hw_map.videoram;
  356.     }
  357.    
  358.     *(addr + GXEMUL_HALT_OFFSET) = '\0';
  359. }
  360.  
  361. /** Gxemul specific interrupt exception handler.
  362.  *
  363.  * Determines sources of the interrupt from interrupt controller and
  364.  * calls high-level handlers for them.
  365.  *
  366.  * @param exc_no Interrupt exception number.
  367.  * @param istate Saved processor state.
  368.  */
  369. void gxemul_irq_exception(int exc_no, istate_t *istate)
  370. {
  371.     uint32_t sources = gxemul_irqc_get_sources();
  372.     int i;
  373.    
  374.     for (i = 0; i < GXEMUL_IRQC_MAX_IRQ; i++) {
  375.         if (sources & (1 << i)) {
  376.             irq_t *irq = irq_dispatch_and_lock(i);
  377.             if (irq) {
  378.                 /* The IRQ handler was found. */
  379.                 irq->handler(irq, irq->arg);
  380.                 spinlock_unlock(&irq->lock);
  381.             } else {
  382.                 /* Spurious interrupt.*/
  383.                 dprintf("cpu%d: spurious interrupt (inum=%d)\n",
  384.                     CPU->id, i);
  385.             }
  386.         }
  387.     }
  388. }
  389.  
  390. /** Returns address of framebuffer device.
  391.  *
  392.  *  @return Address of framebuffer device.
  393.  */
  394. uintptr_t gxemul_get_fb_address(void)
  395. {
  396.     return (uintptr_t) GXEMUL_FB;
  397. }
  398.  
  399. /** @}
  400.  */
  401.