3b3a750318c5df33557bdbaccec1e3882a966e79
[coreboot.git] / payloads / libpayload / drivers / speaker.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * Datasheet:
32  *  - Name: 82C54 CHMOS Programmable Interval Timer
33  *  - PDF: http://www.intel.com/design/archives/periphrl/docs/23124406.pdf
34  *  - Order number: 231244-006
35  */
36
37 #include <libpayload.h>
38
39 #define I82C54_CONTROL_WORD_REGISTER    0x43    /* Write-only. */
40
41 #define I82C54_COUNTER0                 0x40
42 #define I82C54_COUNTER1                 0x41
43 #define I82C54_COUNTER2                 0x42
44
45 #define PC_SPEAKER_PORT                 0x61
46
47 /**
48  * Use the PC speaker to create a tone/sound of the specified frequency.
49  *
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.
54  *
55  * See also:
56  *  - http://en.wikipedia.org/wiki/Pc_speaker
57  *  - http://en.wikipedia.org/wiki/Intel_8253
58  *
59  * @param freq The frequency of the tone.
60  */
61 void speaker_enable(u16 freq)
62 {
63         /* Select counter 2. Read/write LSB first, then MSB. Use mode 3
64            (square wave generator). Use a 16bit binary counter. */
65         outb(0xb6, I82C54_CONTROL_WORD_REGISTER);
66
67         /* Set the desired tone frequency. */
68         outb((u8)(freq & 0x00ff), I82C54_COUNTER2);     /* LSB. */
69         outb((u8)(freq >> 8), I82C54_COUNTER2);         /* MSB. */
70
71         /* Enable the PC speaker (set bits 0 and 1). */
72         outb(inb(PC_SPEAKER_PORT) | 0x03, PC_SPEAKER_PORT);
73 }
74
75 /**
76  * Disable the PC speaker.
77  */
78 void speaker_disable(void)
79 {
80         /* Disable the PC speaker (clear bits 0 and 1). */
81         outb(inb(PC_SPEAKER_PORT) & 0xfc, PC_SPEAKER_PORT);
82 }
83
84 /**
85  * Use the PC speaker to create a tone/beep of the specified frequency
86  * and duration.
87  *
88  * Wait for a short amount of time after the beep to make it distinguishable
89  * from the next beep (if any).
90  *
91  * @param freq The frequency of the tone/beep.
92  * @param duration The duration of the tone/beep in milliseconds.
93  */
94 void speaker_tone(u16 freq, unsigned int duration)
95 {
96         speaker_enable(freq);
97         mdelay(duration);
98         speaker_disable();
99         mdelay(100);
100 }