Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:05

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2013, 2014 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include <assert.h>
0029 
0030 #include <bsp/irq.h>
0031 #include <bsp/arm-pl050.h>
0032 
0033 static volatile pl050 *pl050_get_regs(rtems_termios_device_context *base)
0034 {
0035   arm_pl050_context *ctx = (arm_pl050_context *) base;
0036 
0037   return ctx->regs;
0038 }
0039 
0040 static void pl050_interrupt(void *arg)
0041 {
0042   rtems_termios_tty *tty = arg;
0043   rtems_termios_device_context *base = rtems_termios_get_device_context(tty);
0044   volatile pl050 *regs = pl050_get_regs(base);
0045   uint32_t kmiir_rx = PL050_KMIIR_KMIRXINTR;
0046   uint32_t kmiir_tx = (regs->kmicr & PL050_KMICR_KMITXINTREN) != 0 ?
0047     PL050_KMIIR_KMITXINTR : 0;
0048   uint32_t kmiir = regs->kmiir;
0049 
0050   if ((kmiir & kmiir_rx) != 0) {
0051     char c = (char) PL050_KMIDATA_KMIDATA_GET(regs->kmidata);
0052 
0053     rtems_termios_enqueue_raw_characters(tty, &c, 1);
0054   }
0055 
0056   if ((kmiir & kmiir_tx) != 0) {
0057     rtems_termios_dequeue_characters(tty, 1);
0058   }
0059 }
0060 
0061 static bool pl050_first_open(
0062   struct rtems_termios_tty *tty,
0063   rtems_termios_device_context *base,
0064   struct termios *term,
0065   rtems_libio_open_close_args_t *args
0066 )
0067 {
0068   arm_pl050_context *ctx = (arm_pl050_context *) base;
0069   volatile pl050 *regs = pl050_get_regs(base);
0070   rtems_status_code sc;
0071 
0072   rtems_termios_set_initial_baud(tty, ctx->initial_baud);
0073 
0074   regs->kmicr = PL050_KMICR_KMIEN | PL050_KMICR_KMIRXINTREN;
0075 
0076   sc = rtems_interrupt_handler_install(
0077     ctx->irq,
0078     "PL050",
0079     RTEMS_INTERRUPT_UNIQUE,
0080     pl050_interrupt,
0081     tty
0082   );
0083   assert(sc == RTEMS_SUCCESSFUL);
0084 
0085   return true;
0086 }
0087 
0088 static void pl050_last_close(
0089   struct rtems_termios_tty *tty,
0090   rtems_termios_device_context *base,
0091   rtems_libio_open_close_args_t *args
0092 )
0093 {
0094   arm_pl050_context *ctx = (arm_pl050_context *) base;
0095   volatile pl050 *regs = pl050_get_regs(base);
0096   rtems_status_code sc;
0097 
0098   regs->kmicr = 0;
0099 
0100   sc = rtems_interrupt_handler_remove(
0101     ctx->irq,
0102     pl050_interrupt,
0103     tty
0104   );
0105   assert(sc == RTEMS_SUCCESSFUL);
0106 }
0107 
0108 static void pl050_write_support(
0109   rtems_termios_device_context *base,
0110   const char *s,
0111   size_t n
0112 )
0113 {
0114   volatile pl050 *regs = pl050_get_regs(base);
0115 
0116   if (n > 0) {
0117     regs->kmidata = PL050_KMIDATA_KMIDATA(s[0]);
0118     regs->kmicr |= PL050_KMICR_KMITXINTREN;
0119   } else {
0120     regs->kmicr &= ~PL050_KMICR_KMITXINTREN;
0121   }
0122 }
0123 
0124 const rtems_termios_device_handler arm_pl050_fns = {
0125   .first_open = pl050_first_open,
0126   .last_close = pl050_last_close,
0127   .write = pl050_write_support,
0128   .mode = TERMIOS_IRQ_DRIVEN
0129 };