blob: 0bebca98b1e61d176a503b289f1ffc7489e1761a [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Keith Outwater, keith_outwater@mvis.com
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
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
wdenkc6097192002-11-03 00:24:07 +000021/*
22 * Initialize beeper-related hardware. Initialize timer 1 for use with
Wolfgang Denkaf0501a2008-10-19 02:35:50 +020023 * the beeper. Use 66 MHz internal clock with prescale of 33 to get
wdenkc6097192002-11-03 00:24:07 +000024 * 1 uS period per count.
25 * FIXME: we should really compute the prescale based on the reported
26 * core clock frequency.
27 */
Wolfgang Denka1be4762008-05-20 16:00:29 +020028void init_beeper (void)
wdenkc6097192002-11-03 00:24:07 +000029{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020030 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +000031
32 immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_RST1 | TGCR_STP1;
33 immap->im_cpmtimer.cpmt_tmr1 = ((33 << TMR_PS_SHIFT) & TMR_PS_MSK)
Wolfgang Denka1be4762008-05-20 16:00:29 +020034 | TMR_OM | TMR_FRR | TMR_ICLK_IN_GEN;
wdenkc6097192002-11-03 00:24:07 +000035 immap->im_cpmtimer.cpmt_tcn1 = 0;
36 immap->im_cpmtimer.cpmt_ter1 = 0xffff;
37 immap->im_cpmtimer.cpmt_tgcr |= TGCR_RST1;
38}
39
wdenkc6097192002-11-03 00:24:07 +000040/*
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 Denka1be4762008-05-20 16:00:29 +020045void set_beeper_frequency (uint frequency)
wdenkc6097192002-11-03 00:24:07 +000046{
47#define FREQ_LIMIT 2500
48
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +000050
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 Denka1be4762008-05-20 16:00:29 +020055 if (frequency > FREQ_LIMIT)
56 frequency = FREQ_LIMIT;
57 frequency = 1000000 / frequency;
58 immap->im_cpmtimer.cpmt_trr1 = (ushort) frequency;
wdenkc6097192002-11-03 00:24:07 +000059}
60
wdenkc6097192002-11-03 00:24:07 +000061/*
62 * Turn the beeper on
63 */
Wolfgang Denka1be4762008-05-20 16:00:29 +020064void beeper_on (void)
wdenkc6097192002-11-03 00:24:07 +000065{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020066 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +000067
68 immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1;
69}
70
wdenkc6097192002-11-03 00:24:07 +000071/*
72 * Turn the beeper off
73 */
Wolfgang Denka1be4762008-05-20 16:00:29 +020074void beeper_off (void)
wdenkc6097192002-11-03 00:24:07 +000075{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020076 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +000077
78 immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1;
79}
80
wdenkc6097192002-11-03 00:24:07 +000081/*
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 Denka1be4762008-05-20 16:00:29 +020089void set_beeper_volume (int steps)
wdenkc6097192002-11-03 00:24:07 +000090{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020091 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +000092 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 Denka1be4762008-05-20 16:00:29 +020097 udelay (1);
wdenkc6097192002-11-03 00:24:07 +000098 immap->im_cpm.cp_pbodr |= (0x80000000 >> 19);
Wolfgang Denka1be4762008-05-20 16:00:29 +020099 udelay (1);
wdenkc6097192002-11-03 00:24:07 +0000100 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200101 } else {
wdenkc6097192002-11-03 00:24:07 +0000102 for (i = 0; i > (steps <= -64 ? -64 : steps); i--) {
103 immap->im_cpm.cp_pbdat &= ~(0x80000000 >> 19);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200104 udelay (1);
wdenkc6097192002-11-03 00:24:07 +0000105 immap->im_cpm.cp_pbdat |= (0x80000000 >> 19);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200106 udelay (1);
wdenkc6097192002-11-03 00:24:07 +0000107 }
108 }
109}
110
wdenkc6097192002-11-03 00:24:07 +0000111/*
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 Denka1be4762008-05-20 16:00:29 +0200115 * freq = frequency in Hz (0 - 2500)
wdenkc6097192002-11-03 00:24:07 +0000116 * 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 Denka1be4762008-05-20 16:00:29 +0200122int do_beeper (char *sequence)
wdenkc6097192002-11-03 00:24:07 +0000123{
124#define DELIMITER ';'
125
Wolfgang Denka1be4762008-05-20 16:00:29 +0200126 int args[4];
127 int i;
128 int val;
129 char *p = sequence;
130 char *tp;
wdenkc6097192002-11-03 00:24:07 +0000131
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 Denka1be4762008-05-20 16:00:29 +0200137 if (*p == '\0' || !isdigit (*p)) {
138 printf ("%s:%d: null or invalid string (%s)\n",
139 __FILE__, __LINE__, p);
wdenkc6097192002-11-03 00:24:07 +0000140 return 0;
141 }
142
143 i = 0;
144 while (*p != '\0') {
145 while (*p != DELIMITER) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200146 if (i > 3)
147 i = 0;
148 val = (int) simple_strtol (p, &tp, 0);
wdenkc6097192002-11-03 00:24:07 +0000149 if (tp == p) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200150 printf ("%s:%d: no digits or bad format\n",
151 __FILE__, __LINE__);
wdenkc6097192002-11-03 00:24:07 +0000152 return 0;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200153 } else {
wdenkc6097192002-11-03 00:24:07 +0000154 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 Denka1be4762008-05-20 16:00:29 +0200170 printf ("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i,
171 args[i]);
wdenkc6097192002-11-03 00:24:07 +0000172 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200173 printf ("\n");
wdenkc6097192002-11-03 00:24:07 +0000174#endif
Wolfgang Denka1be4762008-05-20 16:00:29 +0200175 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]);
wdenkc6097192002-11-03 00:24:07 +0000181 }
182 return 1;
183}