Fix libpayload speaker driver
[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         u16 reg16 = 1193180 / freq;
64
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);
68
69         /* Set the desired tone frequency. */
70         outb((u8)(reg16 & 0x00ff), I82C54_COUNTER2);    /* LSB. */
71         outb((u8)(reg16 >> 8), I82C54_COUNTER2);        /* MSB. */
72
73         /* Enable the PC speaker (set bits 0 and 1). */
74         outb(inb(PC_SPEAKER_PORT) | 0x03, PC_SPEAKER_PORT);
75 }
76
77 /**
78  * Disable the PC speaker.
79  */
80 void speaker_disable(void)
81 {
82         /* Disable the PC speaker (clear bits 0 and 1). */
83         outb(inb(PC_SPEAKER_PORT) & 0xfc, PC_SPEAKER_PORT);
84 }
85
86 /**
87  * Use the PC speaker to create a tone/beep of the specified frequency
88  * and duration.
89  *
90  * Wait for a short amount of time after the beep to make it distinguishable
91  * from the next beep (if any).
92  *
93  * @param freq The frequency of the tone/beep.
94  * @param duration The duration of the tone/beep in milliseconds.
95  */
96 void speaker_tone(u16 freq, unsigned int duration)
97 {
98         speaker_enable(freq);
99         mdelay(duration);
100         speaker_disable();
101         mdelay(100);
102 }