Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:00

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *
0005  * Copyright (C) 2024 Kevin Kirspel
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
0017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
0020  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0026  * SUCH DAMAGE.
0027  */
0028 
0029 #include <bsp.h>
0030 #include <bsp/bootcard.h>
0031 #include <stdio.h>
0032 #include <rtems.h>
0033 #include <rtems/libio.h>
0034 #include <rtems/error.h>
0035 #include <stdlib.h>
0036 #include <string.h>
0037 #include <assert.h>
0038 #include <bsp/fatal.h>
0039 #include <bsp/irq.h>
0040 #include <bsp/niosv.h>
0041 
0042 #include <rtems/score/riscv-utility.h>
0043 
0044 /* Local Definitaions */
0045 #define FILE_HEADER_EPCQ_OFFSET                     0x100000
0046 #define FILE_HEADER_VER_SIZE_VERSION                11
0047 /* Local Structures */
0048 typedef struct
0049 {
0050   uint32_t offset;
0051   uint32_t size;
0052   uint32_t crc;
0053   char version[FILE_HEADER_VER_SIZE_VERSION];
0054 }file_header_t;
0055 /* Local Functions */
0056 /* Local Variables */
0057 static char software_version[VERSION_SIZE];
0058 
0059 /* system_initialize --
0060  *     This routine registers the system device
0061  *
0062  * PARAMETERS:
0063  *     major - major led device number
0064  *     minor - miled led device number (not used)
0065  *     arg - device initialize argument
0066  *
0067  * RETURNS:
0068  *     RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly)
0069  */
0070 rtems_device_driver system_initialize (
0071   rtems_device_major_number major,
0072   rtems_device_minor_number minor,
0073   void *arg
0074 )
0075 {
0076   /* Local Variables */
0077   rtems_status_code status;
0078   file_header_t fhead;
0079 
0080   /*
0081    * Register the devices
0082    */
0083   status = rtems_io_register_name ("/dev/system", major, minor );
0084   assert( status == RTEMS_SUCCESSFUL );
0085 
0086   /* read the file header information */
0087   epcq_read_buffer(
0088     FILE_HEADER_EPCQ_OFFSET,
0089     ( uint8_t * )&fhead,
0090     sizeof( fhead )
0091   );
0092 
0093   memset( &software_version[0], 0, sizeof( software_version ));
0094   strncpy( software_version, fhead.version, FILE_HEADER_VER_SIZE_VERSION );
0095 
0096   return RTEMS_SUCCESSFUL;
0097 }
0098 
0099 /* system_open --
0100  *     Open system device.
0101  *
0102  * PARAMETERS:
0103  *     major - major device number for led devices
0104  *     minor - miled device number for led
0105  *     arg - device opening argument
0106  *
0107  * RETURNS:
0108  *     RTEMS error code
0109  */
0110 rtems_device_driver system_open (
0111   rtems_device_major_number major,
0112   rtems_device_minor_number minor,
0113   void *arg
0114 )
0115 {
0116   return RTEMS_SUCCESSFUL;
0117 }
0118 
0119 /* system_close --
0120  *     Close system device.
0121  *
0122  * PARAMETERS:
0123  *     major - major device number for led devices
0124  *     minor - miled device number for led
0125  *     arg - device close argument
0126  *
0127  * RETURNS:
0128  *     RTEMS error code
0129  */
0130 rtems_device_driver system_close (
0131   rtems_device_major_number major,
0132   rtems_device_minor_number minor,
0133   void *arg
0134 )
0135 {
0136   return RTEMS_SUCCESSFUL;
0137 }
0138 
0139 /* system_read --
0140  *     Read from the system device
0141  *
0142  * PARAMETERS:
0143  *     major - major device number for led devices
0144  *     minor - miled device number for led
0145  *     arg - device read argument
0146  *
0147  * RETURNS:
0148  *     RTEMS error code
0149  */
0150 rtems_device_driver system_read (
0151   rtems_device_major_number major,
0152   rtems_device_minor_number minor,
0153   void *arg
0154 )
0155 {
0156   return RTEMS_SUCCESSFUL;
0157 }
0158 
0159 /* system_write --
0160  *     Write to the system device
0161  *
0162  * PARAMETERS:
0163  *     major - major device number for led devices
0164  *     minor - miled device number for led
0165  *     arg - device write argument
0166  *
0167  * RETURNS:
0168  *     RTEMS error code
0169  */
0170 rtems_device_driver system_write (
0171   rtems_device_major_number major,
0172   rtems_device_minor_number minor,
0173   void *arg
0174 )
0175 {
0176   return RTEMS_SUCCESSFUL;
0177 }
0178 
0179 /* system_control --
0180  *     Handle system I/O control (IOCTL)
0181  *
0182  * PARAMETERS:
0183  *     major - major device number for system devices
0184  *     minor - miled device number for system
0185  *     arg - device ioctl argument
0186  *
0187  * RETURNS:
0188  *     RTEMS error code
0189  */
0190 rtems_device_driver system_control (
0191   rtems_device_major_number major,
0192   rtems_device_minor_number minor,
0193   void *arg
0194 )
0195 {
0196   /* Local Varaibles */
0197   rtems_libio_ioctl_args_t *args = arg;
0198   system_control_t *ctrl;
0199 
0200   /* Get Data */
0201   ctrl = ( system_control_t * )args->buffer;
0202   args->ioctl_return = 0;
0203 
0204   /* Parse Command */
0205   switch( args->command ) {
0206     case IOCTL_SYSTEM_SOFTWARE_VERSION :
0207       strcpy( ctrl->version, software_version );
0208       break;
0209     case IOCTL_SYSTEM_RESET :
0210       bsp_reset( ( rtems_fatal_source )0, ( rtems_fatal_code )0 );
0211       break;
0212     default :
0213       args->ioctl_return = -1;
0214       break;
0215   }
0216   /* OK */
0217   return RTEMS_SUCCESSFUL;
0218 }