wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Keith Outwater, keith_outwater@mvis.com |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <mpc8xx.h> |
| 26 | #include <asm/8xx_immap.h> |
| 27 | #include <linux/ctype.h> |
| 28 | |
| 29 | /* |
| 30 | * Basic beeper support for the GEN860T board. The GEN860T includes |
| 31 | * an audio sounder driven by a Phillips TDA8551 amplifier. The |
| 32 | * TDA8551 features a digital volume control which uses a "trinary" |
| 33 | * input (high/high-Z/low) to set volume. The 860's SPKROUT pin |
| 34 | * drives the amplifier input. |
| 35 | */ |
| 36 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | /* |
| 38 | * Initialize beeper-related hardware. Initialize timer 1 for use with |
Wolfgang Denk | 8ed44d9 | 2008-10-19 02:35:50 +0200 | [diff] [blame] | 39 | * the beeper. Use 66 MHz internal clock with prescale of 33 to get |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | * 1 uS period per count. |
| 41 | * FIXME: we should really compute the prescale based on the reported |
| 42 | * core clock frequency. |
| 43 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 44 | void init_beeper (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 47 | |
| 48 | immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_RST1 | TGCR_STP1; |
| 49 | immap->im_cpmtimer.cpmt_tmr1 = ((33 << TMR_PS_SHIFT) & TMR_PS_MSK) |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 50 | | TMR_OM | TMR_FRR | TMR_ICLK_IN_GEN; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | immap->im_cpmtimer.cpmt_tcn1 = 0; |
| 52 | immap->im_cpmtimer.cpmt_ter1 = 0xffff; |
| 53 | immap->im_cpmtimer.cpmt_tgcr |= TGCR_RST1; |
| 54 | } |
| 55 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | /* |
| 57 | * Set beeper frequency. Max allowed frequency is 2.5 KHz. This limit |
| 58 | * is mostly arbitrary, but the beeper isn't really much good beyond this |
| 59 | * frequency. |
| 60 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 61 | void set_beeper_frequency (uint frequency) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | { |
| 63 | #define FREQ_LIMIT 2500 |
| 64 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Compute timer ticks given desired frequency. The timer is set up |
| 69 | * to count 0.5 uS per tick and it takes two ticks per cycle (Hz). |
| 70 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 71 | if (frequency > FREQ_LIMIT) |
| 72 | frequency = FREQ_LIMIT; |
| 73 | frequency = 1000000 / frequency; |
| 74 | immap->im_cpmtimer.cpmt_trr1 = (ushort) frequency; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Turn the beeper on |
| 79 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 80 | void beeper_on (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | |
| 84 | immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1; |
| 85 | } |
| 86 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 87 | /* |
| 88 | * Turn the beeper off |
| 89 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 90 | void beeper_off (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 92 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 93 | |
| 94 | immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1; |
| 95 | } |
| 96 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 97 | /* |
| 98 | * Increase or decrease the beeper volume. Volume can be set |
| 99 | * from off to full in 64 steps. To increase volume, the output |
| 100 | * pin is actively driven high, then returned to tristate. |
| 101 | * To decrease volume, output a low on the port pin (no need to |
| 102 | * change pin mode to tristate) then output a high to go back to |
| 103 | * tristate. |
| 104 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 105 | void set_beeper_volume (int steps) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | int i; |
| 109 | |
| 110 | if (steps >= 0) { |
| 111 | for (i = 0; i < (steps >= 64 ? 64 : steps); i++) { |
| 112 | immap->im_cpm.cp_pbodr &= ~(0x80000000 >> 19); |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 113 | udelay (1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | immap->im_cpm.cp_pbodr |= (0x80000000 >> 19); |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 115 | udelay (1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | } |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 117 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | for (i = 0; i > (steps <= -64 ? -64 : steps); i--) { |
| 119 | immap->im_cpm.cp_pbdat &= ~(0x80000000 >> 19); |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 120 | udelay (1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | immap->im_cpm.cp_pbdat |= (0x80000000 >> 19); |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 122 | udelay (1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | /* |
| 128 | * Check the environment to see if the beeper needs beeping. |
| 129 | * Controlled by a sequence of the form: |
| 130 | * freq/delta volume/on time/off time;... where: |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 131 | * freq = frequency in Hz (0 - 2500) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | * delta volume = volume steps up or down (-64 <= vol <= 64) |
| 133 | * on time = time in mS |
| 134 | * off time = time in mS |
| 135 | * |
| 136 | * Return 1 on success, 0 on failure |
| 137 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 138 | int do_beeper (char *sequence) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 139 | { |
| 140 | #define DELIMITER ';' |
| 141 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 142 | int args[4]; |
| 143 | int i; |
| 144 | int val; |
| 145 | char *p = sequence; |
| 146 | char *tp; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Parse the control sequence. This is a really simple parser |
| 150 | * without any real error checking. You can probably blow it |
| 151 | * up really easily. |
| 152 | */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 153 | if (*p == '\0' || !isdigit (*p)) { |
| 154 | printf ("%s:%d: null or invalid string (%s)\n", |
| 155 | __FILE__, __LINE__, p); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | i = 0; |
| 160 | while (*p != '\0') { |
| 161 | while (*p != DELIMITER) { |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 162 | if (i > 3) |
| 163 | i = 0; |
| 164 | val = (int) simple_strtol (p, &tp, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 165 | if (tp == p) { |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 166 | printf ("%s:%d: no digits or bad format\n", |
| 167 | __FILE__, __LINE__); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 168 | return 0; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 169 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 170 | args[i] = val; |
| 171 | } |
| 172 | |
| 173 | i++; |
| 174 | if (*tp == DELIMITER) |
| 175 | p = tp; |
| 176 | else |
| 177 | p = ++tp; |
| 178 | } |
| 179 | p++; |
| 180 | |
| 181 | /* |
| 182 | * Well, we got something that has a chance of being correct |
| 183 | */ |
| 184 | #if 0 |
| 185 | for (i = 0; i < 4; i++) { |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 186 | printf ("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i, |
| 187 | args[i]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 188 | } |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 189 | printf ("\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 190 | #endif |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 191 | set_beeper_frequency (args[0]); |
| 192 | set_beeper_volume (args[1]); |
| 193 | beeper_on (); |
| 194 | udelay (1000 * args[2]); |
| 195 | beeper_off (); |
| 196 | udelay (1000 * args[3]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | } |
| 198 | return 1; |
| 199 | } |