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