blob: df66f6fa62c928ab8fc143013124bcbe9768186b [file] [log] [blame]
Vladimir Serbinenko45988da2013-03-30 02:02:13 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Vladimir Serbinenko
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010015 */
16
17#include <console/console.h>
18#include <arch/io.h>
19#include <console/spkmodem.h>
20#include <cpu/x86/tsc.h>
21
22#define SPEAKER_PIT_FREQUENCY 0x1234dd
23
24
25enum {
26 PIT_COUNTER_0 = 0x40,
27 PIT_COUNTER_1 = 0x41,
28 PIT_COUNTER_2 = 0x42,
29 PIT_CTRL = 0x43,
30 PIT_SPEAKER_PORT = 0x61,
31};
32
33
34enum {
35 PIT_SPK_TMR2 = 0x01,
36 PIT_SPK_DATA = 0x02,
37 PIT_SPK_TMR2_LATCH = 0x20
38};
39
40enum {
41 PIT_CTRL_SELECT_MASK = 0xc0,
42 PIT_CTRL_SELECT_0 = 0x00,
43 PIT_CTRL_SELECT_1 = 0x40,
44 PIT_CTRL_SELECT_2 = 0x80,
45
Stefan Reinauer86ddd732016-03-11 20:22:28 -080046 PIT_CTRL_READLOAD_MASK = 0x30,
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010047 PIT_CTRL_COUNTER_LATCH = 0x00,
48 PIT_CTRL_READLOAD_LSB = 0x10,
49 PIT_CTRL_READLOAD_MSB = 0x20,
50 PIT_CTRL_READLOAD_WORD = 0x30,
51
52 PIT_CTRL_MODE_MASK = 0x0e,
53 PIT_CTRL_INTR_ON_TERM = 0x00,
54 PIT_CTRL_PROGR_ONE_SHOT = 0x02,
55
56 PIT_CTRL_RATE_GEN = 0x04,
57
58 PIT_CTRL_SQUAREWAVE_GEN = 0x06,
59 PIT_CTRL_SOFTSTROBE = 0x08,
60
61 PIT_CTRL_HARDSTROBE = 0x0a,
62
63
64 PIT_CTRL_COUNT_MASK = 0x01,
65 PIT_CTRL_COUNT_BINARY = 0x00,
66 PIT_CTRL_COUNT_BCD = 0x01
67};
68
69
70static void
Stefan Reinauer86ddd732016-03-11 20:22:28 -080071make_tone(uint16_t freq_count, unsigned int duration)
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010072{
Stefan Reinauer86ddd732016-03-11 20:22:28 -080073 outb(PIT_CTRL_SELECT_2
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010074 | PIT_CTRL_READLOAD_WORD
75 | PIT_CTRL_SQUAREWAVE_GEN
76 | PIT_CTRL_COUNT_BINARY, PIT_CTRL);
77
Stefan Reinauer86ddd732016-03-11 20:22:28 -080078 outb(freq_count & 0xff, PIT_COUNTER_2);
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010079
Stefan Reinauer86ddd732016-03-11 20:22:28 -080080 outb((freq_count >> 8) & 0xff, PIT_COUNTER_2);
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010081
Stefan Reinauer86ddd732016-03-11 20:22:28 -080082 outb(inb(PIT_SPEAKER_PORT)
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010083 | PIT_SPK_TMR2 | PIT_SPK_DATA,
84 PIT_SPEAKER_PORT);
85
86 for (; duration; duration--) {
87 unsigned short counter, previous_counter = 0xffff;
Stefan Reinauer86ddd732016-03-11 20:22:28 -080088
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010089 while (1) {
Stefan Reinauer86ddd732016-03-11 20:22:28 -080090 counter = inb(PIT_COUNTER_2);
91 counter |= ((uint16_t)inb(PIT_COUNTER_2)) << 8;
92 if (counter > previous_counter) {
Vladimir Serbinenko45988da2013-03-30 02:02:13 +010093 previous_counter = counter;
94 break;
95 }
96 previous_counter = counter;
97 }
98 }
99}
100
101void spkmodem_tx_byte(unsigned char c)
102{
103 int i;
104
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800105 make_tone(SPEAKER_PIT_FREQUENCY / 200, 4);
Vladimir Serbinenko45988da2013-03-30 02:02:13 +0100106 for (i = 7; i >= 0; i--) {
107 if ((c >> i) & 1)
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800108 make_tone(SPEAKER_PIT_FREQUENCY / 2000, 20);
Vladimir Serbinenko45988da2013-03-30 02:02:13 +0100109 else
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800110 make_tone(SPEAKER_PIT_FREQUENCY / 4000, 40);
111 make_tone(SPEAKER_PIT_FREQUENCY / 1000, 10);
Vladimir Serbinenko45988da2013-03-30 02:02:13 +0100112 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800113 make_tone(SPEAKER_PIT_FREQUENCY / 200, 0);
Vladimir Serbinenko45988da2013-03-30 02:02:13 +0100114}
115
116void spkmodem_init(void)
117{
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800118 /* Some cards need time to come online.
119 * Output some message to get it started.
120 */
Vladimir Serbinenko45988da2013-03-30 02:02:13 +0100121 spkmodem_tx_byte('S');
122 spkmodem_tx_byte('P');
123 spkmodem_tx_byte('K');
124 spkmodem_tx_byte('\r');
125 spkmodem_tx_byte('\n');
126}