-------- Original Message --------
Subject: spkmodem
Date: Fri, 18 Jan 2013 12:01:15 +0100
From: Vladimir 'φ-coder/phcoder' Serbinenko <phco...@gmail.com>
To: coreboot@coreboot.org

Hello, all. I send a patch to make coreboot output console using system
beeper and a corresponding code to interpret those beeps. I publish it
under GPLv2+ but other licensing arrangements are possible. This was
very useful to get console on X201 where I have neither serial nor EHCI
debug.
-- 
Regards
Vladimir 'φ-coder/phcoder' Serbinenko

diff --git a/src/console/Kconfig b/src/console/Kconfig
index b1f41de..76864ae 100644
--- a/src/console/Kconfig
+++ b/src/console/Kconfig
@@ -114,6 +114,12 @@ config TTYS0_LCS
 	default 3
 	depends on CONSOLE_SERIAL8250 || CONSOLE_SERIAL8250MEM
 
+config SPKMODEM
+	bool "Spkmodem (console on speaker) console output"
+	default n
+	help
+	  Send coreboot debug output through speaker
+
 # Use "select HAVE_USBDEBUG" on southbridges which have Debug Port code.
 config HAVE_USBDEBUG
 	def_bool n
diff --git a/src/console/Makefile.inc b/src/console/Makefile.inc
index 3c4777f..5e120c9 100644
--- a/src/console/Makefile.inc
+++ b/src/console/Makefile.inc
@@ -16,6 +16,7 @@ romstage-y += die.c
 
 ramstage-$(CONFIG_CONSOLE_SERIAL8250) += uart8250_console.c
 ramstage-$(CONFIG_CONSOLE_SERIAL8250MEM) += uart8250mem_console.c
+ramstage-$(CONFIG_SPKMODEM) += spkmodem_console.c
 ramstage-$(CONFIG_USBDEBUG) += usbdebug_console.c
 ramstage-$(CONFIG_CONSOLE_LOGBUF) += logbuf_console.c
 ramstage-$(CONFIG_CONSOLE_NE2K) += ne2k_console.c
diff --git a/src/console/spkmodem_console.c b/src/console/spkmodem_console.c
new file mode 100644
index 0000000..ccebc0a
--- /dev/null
+++ b/src/console/spkmodem_console.c
@@ -0,0 +1,141 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2003 Eric Biederman
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <console/console.h>
+#include <arch/io.h>
+
+#define SPEAKER_PIT_FREQUENCY		0x1234dd
+
+
+enum {
+	PIT_COUNTER_0 = 0x40,
+	PIT_COUNTER_1 = 0x41,
+	PIT_COUNTER_2 = 0x42,
+	PIT_CTRL = 0x43,
+	PIT_SPEAKER_PORT = 0x61,
+};
+
+
+enum {
+	PIT_SPK_TMR2 = 0x01,
+	PIT_SPK_DATA = 0x02,
+	PIT_SPK_TMR2_LATCH = 0x20
+};
+
+enum {
+	PIT_CTRL_SELECT_MASK = 0xc0,
+	PIT_CTRL_SELECT_0 = 0x00,
+	PIT_CTRL_SELECT_1 = 0x40,
+	PIT_CTRL_SELECT_2 = 0x80,
+
+	PIT_CTRL_READLOAD_MASK= 0x30,
+	PIT_CTRL_COUNTER_LATCH = 0x00,
+	PIT_CTRL_READLOAD_LSB = 0x10,
+	PIT_CTRL_READLOAD_MSB = 0x20,
+	PIT_CTRL_READLOAD_WORD = 0x30,
+
+	PIT_CTRL_MODE_MASK = 0x0e,
+	PIT_CTRL_INTR_ON_TERM = 0x00,
+	PIT_CTRL_PROGR_ONE_SHOT = 0x02,
+
+	PIT_CTRL_RATE_GEN = 0x04,
+
+	PIT_CTRL_SQUAREWAVE_GEN = 0x06,
+	PIT_CTRL_SOFTSTROBE = 0x08,
+
+	PIT_CTRL_HARDSTROBE = 0x0a,
+
+
+	PIT_CTRL_COUNT_MASK = 0x01,
+	PIT_CTRL_COUNT_BINARY = 0x00,
+	PIT_CTRL_COUNT_BCD = 0x01
+};
+
+static void
+make_tone (uint16_t freq_count, unsigned int duration)
+{
+	outb (PIT_CTRL_SELECT_2
+		   | PIT_CTRL_READLOAD_WORD
+		   | PIT_CTRL_SQUAREWAVE_GEN
+		   | PIT_CTRL_COUNT_BINARY, PIT_CTRL);
+	outb (freq_count & 0xff, PIT_COUNTER_2);
+	outb ((freq_count >> 8) & 0xff, PIT_COUNTER_2);
+
+	outb (inb (PIT_SPEAKER_PORT)
+		   | PIT_SPK_TMR2 | PIT_SPK_DATA,
+		   PIT_SPEAKER_PORT);
+
+	for (; duration; duration--) {
+		unsigned short counter, previous_counter = 0xffff;
+		while (1) {
+			counter = inb (PIT_COUNTER_2);
+			counter |= ((uint16_t) inb (PIT_COUNTER_2)) << 8;
+			if (counter > previous_counter)
+			{
+				previous_counter = counter;
+				break;
+			}
+			previous_counter = counter;
+		}
+	}
+}
+
+static void spkmodem_init(void)
+{
+	/* Some models shutdown sound when not in use and it takes for it
+	   around 30 ms to come back on which loses 3 bits. So generate a base
+	   50 Hz continously. */
+	make_tone (SPEAKER_PIT_FREQUENCY / 50, 20);
+}
+
+static void spkmodem_tx_byte(unsigned char c)
+{
+	int i;
+
+	for (i = 7; i >= 0; i--) {
+		if ((c >> i) & 1)
+			make_tone (SPEAKER_PIT_FREQUENCY / 2000, 20);
+		else
+			make_tone (SPEAKER_PIT_FREQUENCY / 4000, 40);
+		make_tone (SPEAKER_PIT_FREQUENCY / 1000, 10);
+	}
+	make_tone (SPEAKER_PIT_FREQUENCY / 50, 0);
+}
+
+static void spkmodem_tx_flush(void)
+{
+}
+
+static unsigned char spkmodem_rx_byte(void)
+{
+	return 0;
+}
+
+static int spkmodem_tst_byte(void)
+{
+	return 0;
+}
+
+static const struct console_driver spkmodem_console __console = {
+	.init     = spkmodem_init,
+	.tx_byte  = spkmodem_tx_byte,
+	.tx_flush = spkmodem_tx_flush,
+	.rx_byte  = spkmodem_rx_byte,
+	.tst_byte = spkmodem_tst_byte,
+};

/* spkmodem-recv.c - decode spkmodem signals */
/*
 *  Copyright (C) 2013  Free Software Foundation, Inc.
 *
 *  spkmodem-recv is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  spkmodem-recv is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with spkmodem-recv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Compilation:  gcc -o spkmodem-recv spkmodem-recv  */
/* Usage: parecord --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */

#define SAMPLES_PER_TRAME 240
#define FREQ_SEP_MIN 6
#define FREQ_SEP_MAX 15
#define FREQ_DATA_MIN 15
#define FREQ_DATA_THRESHOLD 25
#define FREQ_DATA_MAX 60
#define THRESHOLD 500

#define DEBUG 0

static signed short trame[2 * SAMPLES_PER_TRAME];
static signed short pulse[2 * SAMPLES_PER_TRAME];
static int ringpos = 0;
static int pos, f1, f2;
static int amplitude = 0;
static int lp = 0;

static void
read_sample (void)
{
  amplitude -= abs (trame[ringpos]);
  f1 -= pulse[ringpos];
  f1 += pulse[(ringpos + SAMPLES_PER_TRAME) % (2 * SAMPLES_PER_TRAME)];
  f2 -= pulse[(ringpos + SAMPLES_PER_TRAME) % (2 * SAMPLES_PER_TRAME)];
  fread (trame + ringpos, 1, sizeof (trame[0]), stdin);
  amplitude += abs (trame[ringpos]);

  if (pos ? (trame[ringpos] < -THRESHOLD)
      : (trame[ringpos] > +THRESHOLD))
    {
      pulse[ringpos] = 1;
      pos = !pos;
      f2++;
    }
  else
    pulse[ringpos] = 0;
  ringpos++;
  ringpos %= 2 * SAMPLES_PER_TRAME;
  lp++;
}

int
main ()
{
  int bitn = 7;
  char c = 0;
  int i;
  while (!feof (stdin))
    {
      if (lp > 20000)
	{
	  fflush (stdout);
	  bitn = 7;
	  c = 0;
	  lp = 0;
	}
      if (f2 > FREQ_SEP_MIN && f2 < FREQ_SEP_MAX
	  && f1 > FREQ_DATA_MIN && f1 < FREQ_DATA_MAX)
	{
#if DEBUG
	  printf ("%d %d %d @%d\n", f1, f2, FREQ_DATA_THRESHOLD,
		  ftell (stdin) - sizeof (trame));
#endif
	  if (f1 < FREQ_DATA_THRESHOLD)
	    c |= (1 << bitn);
	  bitn--;
	  if (bitn < 0)
	    {
#if DEBUG
	      printf ("<%c, %x>", c, c);
#else
	      printf ("%c", c);
#endif
	      bitn = 7;
	      c = 0;
	    }
	  lp = 0;
	  for (i = 0; i < SAMPLES_PER_TRAME; i++)
	    read_sample ();
	  continue;
	}
      read_sample ();
    }
  return 0;
}

Attachment: signature.asc
Description: OpenPGP digital signature

-- 
coreboot mailing list: coreboot@coreboot.org
http://www.coreboot.org/mailman/listinfo/coreboot

Reply via email to