2 * This file is part of the libpayload project.
4 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * - Name: 82C54 CHMOS Programmable Interval Timer
33 * - PDF: http://www.intel.com/design/archives/periphrl/docs/23124406.pdf
34 * - Order number: 231244-006
37 #include <libpayload.h>
39 #define I82C54_CONTROL_WORD_REGISTER 0x43 /* Write-only. */
41 #define I82C54_COUNTER0 0x40
42 #define I82C54_COUNTER1 0x41
43 #define I82C54_COUNTER2 0x42
45 #define PC_SPEAKER_PORT 0x61
48 * Use the PC speaker to create a tone/sound of the specified frequency.
50 * The Intel 82C54 CHMOS Programmable Interval Timer (PIT) provides a
51 * superset of the functions of the original Intel 8253/8254 PIT. It has
52 * three programmable counters/timers (counter 0, 1, and 2). Counter 2 can
53 * be used to generate tones/sounds of various frequencies and duration.
56 * - http://en.wikipedia.org/wiki/Pc_speaker
57 * - http://en.wikipedia.org/wiki/Intel_8253
59 * @param freq The frequency of the tone.
61 void speaker_enable(u16 freq)
63 u16 reg16 = 1193180 / freq;
65 /* Select counter 2. Read/write LSB first, then MSB. Use mode 3
66 (square wave generator). Use a 16bit binary counter. */
67 outb(0xb6, I82C54_CONTROL_WORD_REGISTER);
69 /* Set the desired tone frequency. */
70 outb((u8)(reg16 & 0x00ff), I82C54_COUNTER2); /* LSB. */
71 outb((u8)(reg16 >> 8), I82C54_COUNTER2); /* MSB. */
73 /* Enable the PC speaker (set bits 0 and 1). */
74 outb(inb(PC_SPEAKER_PORT) | 0x03, PC_SPEAKER_PORT);
78 * Disable the PC speaker.
80 void speaker_disable(void)
82 /* Disable the PC speaker (clear bits 0 and 1). */
83 outb(inb(PC_SPEAKER_PORT) & 0xfc, PC_SPEAKER_PORT);
87 * Use the PC speaker to create a tone/beep of the specified frequency
90 * Wait for a short amount of time after the beep to make it distinguishable
91 * from the next beep (if any).
93 * @param freq The frequency of the tone/beep.
94 * @param duration The duration of the tone/beep in milliseconds.
96 void speaker_tone(u16 freq, unsigned int duration)