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