summaryrefslogtreecommitdiffstats
path: root/bsps/i386
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2018-04-19 06:35:52 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2018-04-20 09:57:01 +0200
commit7632906fc290b652416ab59eb5fb49356c064ed6 (patch)
treeac036b1f95637e044e10138ceea8d2b56d80ec97 /bsps/i386
parentbsps: Move bspsmpgetcurrentprocessor.c to bsps (diff)
downloadrtems-7632906fc290b652416ab59eb5fb49356c064ed6.tar.bz2
bsps: Move clock drivers to bsps
This patch is a part of the BSP source reorganization. Update #3285.
Diffstat (limited to 'bsps/i386')
-rw-r--r--bsps/i386/pc386/clock/ckinit.c262
-rw-r--r--bsps/i386/pc386/clock/rtc.c216
-rw-r--r--bsps/i386/pc386/clock/todcfg.c32
3 files changed, 510 insertions, 0 deletions
diff --git a/bsps/i386/pc386/clock/ckinit.c b/bsps/i386/pc386/clock/ckinit.c
new file mode 100644
index 0000000000..fce267bda7
--- /dev/null
+++ b/bsps/i386/pc386/clock/ckinit.c
@@ -0,0 +1,262 @@
+/**
+ * @file
+ *
+ * Clock Tick Device Driver
+ *
+ * History:
+ * + Original driver was go32 clock by Joel Sherrill
+ * + go32 clock driver hardware code was inserted into new
+ * boilerplate when the pc386 BSP by:
+ * Pedro Miguel Da Cruz Neto Romano <pmcnr@camoes.rnl.ist.utl.pt>
+ * Jose Rufino <ruf@asterix.ist.utl.pt>
+ * + Reworked by Joel Sherrill to use clock driver template.
+ * This removes all boilerplate and leave original hardware
+ * code I developed for the go32 BSP.
+ */
+
+/*
+ * COPYRIGHT (c) 1989-2012.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#include <bsp.h>
+#include <bsp/irq-generic.h>
+#include <bspopts.h>
+#include <libcpu/cpuModel.h>
+#include <assert.h>
+#include <rtems/timecounter.h>
+
+#define CLOCK_VECTOR 0
+
+volatile uint32_t pc386_microseconds_per_isr;
+volatile uint32_t pc386_isrs_per_tick;
+uint32_t pc386_clock_click_count;
+
+/* forward declaration */
+void Clock_isr(void *param);
+static void clockOff(void);
+static void Clock_isr_handler(void *param);
+
+/*
+ * Roughly the number of cycles per second. Note that these
+ * will be wildly inaccurate if the chip speed changes due to power saving
+ * or thermal modes.
+ *
+ * NOTE: These are only used when the TSC method is used.
+ */
+static uint64_t pc586_tsc_frequency;
+
+static struct timecounter pc386_tc;
+
+/* this driver may need to count ISRs per tick */
+#define CLOCK_DRIVER_ISRS_PER_TICK 1
+#define CLOCK_DRIVER_ISRS_PER_TICK_VALUE pc386_isrs_per_tick
+
+extern volatile uint32_t Clock_driver_ticks;
+
+#define READ_8254( _lsb, _msb ) \
+ do { outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_LATCH); \
+ inport_byte(TIMER_CNTR0, _lsb); \
+ inport_byte(TIMER_CNTR0, _msb); \
+ } while (0)
+
+
+#ifdef RTEMS_SMP
+#define Clock_driver_support_at_tick() \
+ _SMP_Send_message_broadcast(SMP_MESSAGE_CLOCK_TICK)
+#endif
+
+static uint32_t pc386_get_timecount_tsc(struct timecounter *tc)
+{
+ return (uint32_t)rdtsc();
+}
+
+static uint32_t pc386_get_timecount_i8254(struct timecounter *tc)
+{
+ uint32_t irqs;
+ uint8_t lsb, msb;
+ rtems_interrupt_lock_context lock_context;
+
+ /*
+ * Fetch all the data in an interrupt critical section.
+ */
+
+ rtems_interrupt_lock_acquire(&rtems_i386_i8254_access_lock, &lock_context);
+
+ READ_8254(lsb, msb);
+ irqs = Clock_driver_ticks;
+
+ rtems_interrupt_lock_release(&rtems_i386_i8254_access_lock, &lock_context);
+
+ return (irqs + 1) * pc386_microseconds_per_isr - ((msb << 8) | lsb);
+}
+
+/*
+ * Calibrate CPU cycles per tick. Interrupts should be disabled.
+ */
+static void calibrate_tsc(void)
+{
+ uint64_t begin_time;
+ uint8_t then_lsb, then_msb, now_lsb, now_msb;
+ uint32_t i;
+
+ /*
+ * We just reset the timer, so we know we're at the beginning of a tick.
+ */
+
+ /*
+ * Count cycles. Watching the timer introduces a several microsecond
+ * uncertaintity, so let it cook for a while and divide by the number of
+ * ticks actually executed.
+ */
+
+ begin_time = rdtsc();
+
+ for (i = rtems_clock_get_ticks_per_second() * pc386_isrs_per_tick;
+ i != 0; --i ) {
+ /* We know we've just completed a tick when timer goes from low to high */
+ then_lsb = then_msb = 0xff;
+ do {
+ READ_8254(now_lsb, now_msb);
+ if ((then_msb < now_msb) ||
+ ((then_msb == now_msb) && (then_lsb < now_lsb)))
+ break;
+ then_lsb = now_lsb;
+ then_msb = now_msb;
+ } while (1);
+ }
+
+ pc586_tsc_frequency = rdtsc() - begin_time;
+
+#if 0
+ printk( "CPU clock at %u MHz\n", (uint32_t)(pc586_tsc_frequency / 1000000));
+#endif
+}
+
+static void clockOn(void)
+{
+ rtems_interrupt_lock_context lock_context;
+ pc386_isrs_per_tick = 1;
+ pc386_microseconds_per_isr = rtems_configuration_get_microseconds_per_tick();
+
+ while (US_TO_TICK(pc386_microseconds_per_isr) > 65535) {
+ pc386_isrs_per_tick *= 10;
+ pc386_microseconds_per_isr /= 10;
+ }
+ pc386_clock_click_count = US_TO_TICK(pc386_microseconds_per_isr);
+
+ #if 0
+ printk( "configured usecs per tick=%d \n",
+ rtems_configuration_get_microseconds_per_tick() );
+ printk( "Microseconds per ISR =%d\n", pc386_microseconds_per_isr );
+ printk( "final ISRs per=%d\n", pc386_isrs_per_tick );
+ printk( "final timer counts=%d\n", pc386_clock_click_count );
+ #endif
+
+ rtems_interrupt_lock_acquire(&rtems_i386_i8254_access_lock, &lock_context);
+ outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN);
+ outport_byte(TIMER_CNTR0, pc386_clock_click_count >> 0 & 0xff);
+ outport_byte(TIMER_CNTR0, pc386_clock_click_count >> 8 & 0xff);
+ rtems_interrupt_lock_release(&rtems_i386_i8254_access_lock, &lock_context);
+
+ bsp_interrupt_vector_enable( BSP_PERIODIC_TIMER - BSP_IRQ_VECTOR_BASE );
+
+ /*
+ * Now calibrate cycles per tick. Do this every time we
+ * turn the clock on in case the CPU clock speed has changed.
+ */
+ if ( x86_has_tsc() )
+ calibrate_tsc();
+}
+
+static void clockOff(void)
+{
+ rtems_interrupt_lock_context lock_context;
+ rtems_interrupt_lock_acquire(&rtems_i386_i8254_access_lock, &lock_context);
+ /* reset timer mode to standard (BIOS) value */
+ outport_byte(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN);
+ outport_byte(TIMER_CNTR0, 0);
+ outport_byte(TIMER_CNTR0, 0);
+ rtems_interrupt_lock_release(&rtems_i386_i8254_access_lock, &lock_context);
+} /* Clock_exit */
+
+bool Clock_isr_enabled = false;
+static void Clock_isr_handler(void *param)
+{
+ if ( Clock_isr_enabled )
+ Clock_isr( param );
+}
+
+void Clock_driver_install_handler(void)
+{
+ rtems_status_code status;
+
+ status = rtems_interrupt_handler_install(
+ BSP_PERIODIC_TIMER,
+ "ckinit",
+ RTEMS_INTERRUPT_UNIQUE,
+ Clock_isr_handler,
+ NULL
+ );
+ assert(status == RTEMS_SUCCESSFUL);
+ clockOn();
+}
+
+#define Clock_driver_support_set_interrupt_affinity(online_processors) \
+ do { \
+ /* FIXME: Is there a way to do this on x86? */ \
+ (void) online_processors; \
+ } while (0)
+
+void Clock_driver_support_initialize_hardware(void)
+{
+ bool use_tsc = false;
+ bool use_8254 = false;
+
+ #if (CLOCK_DRIVER_USE_TSC == 1)
+ use_tsc = true;
+ #endif
+
+ #if (CLOCK_DRIVER_USE_8254 == 1)
+ use_8254 = true;
+ #endif
+
+ if ( !use_tsc && !use_8254 ) {
+ if ( x86_has_tsc() ) use_tsc = true;
+ else use_8254 = true;
+ }
+
+ if ( use_8254 ) {
+ /* printk( "Use 8254\n" ); */
+ pc386_tc.tc_get_timecount = pc386_get_timecount_i8254;
+ pc386_tc.tc_counter_mask = 0xffffffff;
+ pc386_tc.tc_frequency = TIMER_TICK;
+ } else {
+ /* printk( "Use TSC\n" ); */
+ pc386_tc.tc_get_timecount = pc386_get_timecount_tsc;
+ pc386_tc.tc_counter_mask = 0xffffffff;
+ pc386_tc.tc_frequency = pc586_tsc_frequency;
+ }
+
+ pc386_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
+ rtems_timecounter_install(&pc386_tc);
+ Clock_isr_enabled = true;
+}
+
+#define Clock_driver_support_shutdown_hardware() \
+ do { \
+ rtems_status_code status; \
+ clockOff(); \
+ status = rtems_interrupt_handler_remove( \
+ BSP_PERIODIC_TIMER, \
+ Clock_isr_handler, \
+ NULL \
+ ); \
+ assert(status == RTEMS_SUCCESSFUL); \
+ } while (0)
+
+#include "../../../shared/dev/clock/clockimpl.h"
diff --git a/bsps/i386/pc386/clock/rtc.c b/bsps/i386/pc386/clock/rtc.c
new file mode 100644
index 0000000000..185248669c
--- /dev/null
+++ b/bsps/i386/pc386/clock/rtc.c
@@ -0,0 +1,216 @@
+/*-------------------------------------------------------------------------+
+| rtc.c v1.1 - PC386 BSP - 1997/08/07
++--------------------------------------------------------------------------+
+| This file contains the real time clock manipulation package for the
+| PC386 board.
++--------------------------------------------------------------------------+
+| (C) Copyright 1997 -
+| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
+|
+| http://pandora.ist.utl.pt
+|
+| Instituto Superior Tecnico * Lisboa * PORTUGAL
++--------------------------------------------------------------------------+
+| Disclaimer:
+|
+| This file is provided "AS IS" without warranty of any kind, either
+| expressed or implied.
++--------------------------------------------------------------------------+
+| This code is based on:
+| rtc.c,v 1.4 1995/12/19 20:07:15 joel Exp - go32 BSP
+| With the following copyright notice:
+| **************************************************************************
+| * COPYRIGHT (c) 1989-1999.
+| * On-Line Applications Research Corporation (OAR).
+| *
+| * The license and distribution terms for this file may be
+| * found in the file LICENSE in this distribution or at
+| * http://www.rtems.org/license/LICENSE.
+| **************************************************************************
++--------------------------------------------------------------------------*/
+
+#include <string.h>
+
+#include <bsp.h>
+
+/*-------------------------------------------------------------------------+
+| Constants
++--------------------------------------------------------------------------*/
+#define IO_RTC 0x70 /* RTC */
+
+#define RTC_SEC 0x00 /* seconds */
+#define RTC_SECALRM 0x01 /* seconds alarm */
+#define RTC_MIN 0x02 /* minutes */
+#define RTC_MINALRM 0x03 /* minutes alarm */
+#define RTC_HRS 0x04 /* hours */
+#define RTC_HRSALRM 0x05 /* hours alarm */
+#define RTC_WDAY 0x06 /* week day */
+#define RTC_DAY 0x07 /* day of month */
+#define RTC_MONTH 0x08 /* month of year */
+#define RTC_YEAR 0x09 /* month of year */
+#define RTC_STATUSA 0x0a /* status register A */
+#define RTCSA_TUP 0x80 /* time update, don't look now */
+
+#define RTC_STATUSB 0x0b /* status register B */
+
+#define RTC_INTR 0x0c /* status register C (R) interrupt source */
+#define RTCIR_UPDATE 0x10 /* update intr */
+#define RTCIR_ALARM 0x20 /* alarm intr */
+#define RTCIR_PERIOD 0x40 /* periodic intr */
+#define RTCIR_INT 0x80 /* interrupt output signal */
+
+#define RTC_STATUSD 0x0d /* status register D (R) Lost Power */
+#define RTCSD_PWR 0x80 /* clock lost power */
+
+#define RTC_DIAG 0x0e /* status register E - bios diagnostic */
+#define RTCDG_BITS "\020\010clock_battery\007ROM_cksum\006config_unit\005memory_size\004fixed_disk\003invalid_time"
+
+#define RTC_CENTURY 0x32 /* current century - increment in Dec99 */
+
+/*-------------------------------------------------------------------------+
+| Auxiliary Functions
++--------------------------------------------------------------------------*/
+/*-------------------------------------------------------------------------+
+| Function: bcd
+| Description: Convert 2 digit number to its BCD representation.
+| Global Variables: None.
+| Arguments: i - Number to convert.
+| Returns: BCD representation of number.
++--------------------------------------------------------------------------*/
+static inline uint8_t
+bcd(uint8_t i)
+{
+ return ((i / 16) * 10 + (i % 16));
+} /* bcd */
+
+#define QUICK_READ /* Quick read of the RTC: don't return number of seconds. */
+
+#ifndef QUICK_READ
+
+#define SECS_PER_DAY (24 * 60 * 60)
+#define SECS_PER_REG_YEAR (365 * SECS_PER_DAY)
+
+/*-------------------------------------------------------------------------+
+| Function: ytos
+| Description: Convert years to seconds (since 1970).
+| Global Variables: None.
+| Arguments: y - year to convert (1970 <= y <= 2100).
+| Returns: number of seconds since 1970.
++--------------------------------------------------------------------------*/
+static inline uint32_t
+ytos(uint16_t y)
+{ /* v NUM LEAP YEARS v */
+ return ((y - 1970) * SECS_PER_REG_YEAR + (y - 1970 + 1) / 4 * SECS_PER_DAY);
+} /* ytos */
+
+/*-------------------------------------------------------------------------+
+| Function: mtos
+| Description: Convert months to seconds since January.
+| Global Variables: None.
+| Arguments: m - month to convert, leap - is this a month of a leap year.
+| Returns: number of seconds since January.
++--------------------------------------------------------------------------*/
+static inline uint32_t
+mtos(uint8_t m, bool leap)
+{
+ static uint16_t daysMonth[] = { 0, 0, 31, 59, 90, 120, 151, 181,
+ 212, 243, 273, 304, 334, 365 };
+ /* Days since beginning of year until beginning of month. */
+
+ return ((daysMonth[m] + (leap ? 1 : 0)) * SECS_PER_DAY);
+} /* mtos */
+
+#endif /* QUICK_READ */
+
+/*-------------------------------------------------------------------------+
+| Function: rtcin
+| Description: Perform action on RTC and return its result.
+| Global Variables: None.
+| Arguments: what - what to write to RTC port (what to do).
+| Returns: result received from RTC port after action performed.
++--------------------------------------------------------------------------*/
+static inline uint8_t
+rtcin(uint8_t what)
+{
+ uint8_t r;
+
+ outport_byte(IO_RTC, what);
+ inport_byte (IO_RTC+1, r);
+ return r;
+} /* rtcin */
+
+/*-------------------------------------------------------------------------+
+| Functions
++--------------------------------------------------------------------------*/
+/*-------------------------------------------------------------------------+
+| Function: init_rtc
+| Description: Initialize real-time clock (RTC).
+| Global Variables: None.
+| Arguments: None.
+| Returns: Nothing.
++--------------------------------------------------------------------------*/
+void
+init_rtc(void)
+{
+ uint8_t s;
+
+ /* initialize brain-dead battery powered clock */
+ outport_byte(IO_RTC, RTC_STATUSA);
+ outport_byte(IO_RTC+1, 0x26);
+ outport_byte(IO_RTC, RTC_STATUSB);
+ outport_byte(IO_RTC+1, 2);
+
+ outport_byte(IO_RTC, RTC_DIAG);
+ inport_byte (IO_RTC+1, s);
+ if (s)
+ printk("RTC BIOS diagnostic error %b\n", s);
+
+ /* FIXME: This was last line's original version. How was it supposed to work?
+ printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS); */
+} /* init_rtc */
+
+/*-------------------------------------------------------------------------+
+| Function: rtc_read
+| Description: Read present time from RTC and return it.
+| Global Variables: None.
+| Arguments: tod - to return present time in 'rtems_time_of_day' format.
+| Returns: number of seconds from 1970/01/01 corresponding to 'tod'.
++--------------------------------------------------------------------------*/
+long int
+rtc_read(rtems_time_of_day *tod)
+{
+ uint8_t sa;
+ uint32_t sec = 0;
+
+ memset(tod, 0, sizeof *tod); /* zero tod structure */
+
+ /* do we have a realtime clock present? (otherwise we loop below) */
+ sa = rtcin(RTC_STATUSA);
+ if (sa == 0xff || sa == 0)
+ return -1;
+
+ /* ready for a read? */
+ while ((sa&RTCSA_TUP) == RTCSA_TUP)
+ sa = rtcin(RTC_STATUSA);
+
+ tod->year = bcd(rtcin(RTC_YEAR)) + 1900; /* year */
+ if (tod->year < 1970) tod->year += 100;
+ tod->month = bcd(rtcin(RTC_MONTH)); /* month */
+ tod->day = bcd(rtcin(RTC_DAY)); /* day */
+ (void) bcd(rtcin(RTC_WDAY)); /* weekday */
+ tod->hour = bcd(rtcin(RTC_HRS)); /* hour */
+ tod->minute = bcd(rtcin(RTC_MIN)); /* minutes */
+ tod->second = bcd(rtcin(RTC_SEC)); /* seconds */
+ tod->ticks = 0;
+
+#ifndef QUICK_READ /* Quick read of the RTC: don't return number of seconds. */
+ sec = ytos(tod->year);
+ sec += mtos(tod->month, (tod->year % 4) == 0);
+ sec += tod->day * SECS_PER_DAY;
+ sec += tod->hour * 60 * 60; /* hour */
+ sec += tod->minute * 60; /* minutes */
+ sec += tod->second; /* seconds */
+#endif /* QUICK_READ */
+
+ return (long int)sec;
+} /* rtc_read */
diff --git a/bsps/i386/pc386/clock/todcfg.c b/bsps/i386/pc386/clock/todcfg.c
new file mode 100644
index 0000000000..7a1d36900a
--- /dev/null
+++ b/bsps/i386/pc386/clock/todcfg.c
@@ -0,0 +1,32 @@
+/*
+ * This file contains the RTC driver table for Motorola shared BSPs.
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#include <bsp.h>
+#include <libchip/rtc.h>
+#include <libchip/mc146818a.h>
+
+/* The following table configures the RTC drivers used in this BSP */
+rtc_tbl RTC_Table[] = {
+ {
+ "/dev/rtc", /* sDeviceName */
+ RTC_MC146818A, /* deviceType */
+ &mc146818a_fns, /* pDeviceFns */
+ mc146818a_probe, /* deviceProbe */
+ NULL, /* pDeviceParams */
+ 0x70, /* ulCtrlPort1 */
+ 0x00, /* ulDataPort */
+ mc146818a_get_register, /* getRegister */
+ mc146818a_set_register /* setRegister */
+ }
+};
+
+/* Some information used by the RTC driver */
+
+#define NUM_RTCS (sizeof(RTC_Table)/sizeof(rtc_tbl))
+
+size_t RTC_Count = NUM_RTCS;