]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/tqm5200/cmd_stk52xx.c
Merge with git://www.denx.de/git/u-boot.git
[karo-tx-uboot.git] / board / tqm5200 / cmd_stk52xx.c
1 /*
2  * (C) Copyright 2005
3  * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de.
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 /*
25  * STK52XX specific functions
26  */
27 /*#define DEBUG*/
28
29 #include <common.h>
30 #include <command.h>
31
32 #if defined(CONFIG_CMD_BSP)
33
34 #if defined(CONFIG_STK52XX) || defined(CONFIG_FO300)
35 #define DEFAULT_VOL     45
36 #define DEFAULT_FREQ    500
37 #define DEFAULT_DURATION        200
38 #define LEFT            1
39 #define RIGHT           2
40 #define LEFT_RIGHT      3
41 #define BL_OFF          0
42 #define BL_ON           1
43
44 #define SM501_GPIO_CTRL_LOW             0x00000008UL
45 #define SM501_GPIO_CTRL_HIGH            0x0000000CUL
46 #define SM501_POWER_MODE0_GATE          0x00000040UL
47 #define SM501_POWER_MODE1_GATE          0x00000048UL
48 #define POWER_MODE_GATE_GPIO_PWM_I2C    0x00000040UL
49 #define SM501_GPIO_DATA_LOW             0x00010000UL
50 #define SM501_GPIO_DATA_HIGH            0x00010004UL
51 #define SM501_GPIO_DATA_DIR_LOW         0x00010008UL
52 #define SM501_GPIO_DATA_DIR_HIGH        0x0001000CUL
53 #define SM501_PANEL_DISPLAY_CONTROL     0x00080000UL
54
55 static int i2s_squarewave(unsigned long duration, unsigned int freq,
56                           unsigned int channel);
57 static int i2s_sawtooth(unsigned long duration, unsigned int freq,
58                         unsigned int channel);
59 static void spi_init(void);
60 static int spi_transmit(unsigned char data);
61 static void pcm1772_write_reg(unsigned char addr, unsigned char data);
62 static void set_attenuation(unsigned char attenuation);
63
64 static void spi_init(void)
65 {
66         struct mpc5xxx_spi *spi = (struct mpc5xxx_spi*)MPC5XXX_SPI;
67         struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO;
68
69         /* PSC3 as SPI and GPIOs */
70         gpio->port_config &= 0xFFFFF0FF;
71         gpio->port_config |= 0x00000800;
72         /*
73          * Its important to use the correct order when initializing the
74          * registers
75          */
76         spi->ddr = 0x0F; /* set all SPI pins as output */
77         spi->pdr = 0x08; /* set SS high */
78         spi->cr1 = 0x50; /* SPI is master, SS is general purpose output */
79         spi->cr2 = 0x00; /* normal operation */
80         spi->brr = 0xFF; /* baud rate: IPB clock / 2048 */
81 }
82
83 static int spi_transmit(unsigned char data)
84 {
85         int dummy;
86         struct mpc5xxx_spi *spi = (struct mpc5xxx_spi*)MPC5XXX_SPI;
87
88         spi->dr = data;
89         /* wait for SPI transmission completed */
90         while(!(spi->sr & 0x80))
91         {
92                 if (spi->sr & 0x40)     /* if write collision occured */
93                 {
94                         /* do dummy read to clear status register */
95                         dummy = spi->dr;
96                         printf ("SPI write collision\n");
97                         return -1;
98                 }
99         }
100         return (spi->dr);
101 }
102
103 static void pcm1772_write_reg(unsigned char addr, unsigned char data)
104 {
105         struct mpc5xxx_spi *spi = (struct mpc5xxx_spi*)MPC5XXX_SPI;
106
107         spi->pdr = 0x00; /* Set SS low */
108         spi_transmit(addr);
109         spi_transmit(data);
110         /* wait some time to meet MS# hold time of PCM1772 */
111         udelay (1);
112         spi->pdr = 0x08; /* set SS high */
113 }
114
115 static void set_attenuation(unsigned char attenuation)
116 {
117         pcm1772_write_reg(0x01, attenuation); /* left channel */
118         debug ("PCM1772 attenuation left set to %d.\n", attenuation);
119         pcm1772_write_reg(0x02, attenuation); /* right channel */
120         debug ("PCM1772 attenuation right set to %d.\n", attenuation);
121 }
122
123 void amplifier_init(void)
124 {
125         static int init_done = 0;
126         int i;
127         struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO;
128
129         /* Do this only once, because of the long time delay */
130         if (!init_done) {
131                 /* configure PCM1772 audio format as I2S */
132                 pcm1772_write_reg(0x03, 0x01);
133                 /* enable audio amplifier */
134                 gpio->sint_gpioe |=  0x02;      /* PSC3_5 as GPIO */
135                 gpio->sint_ode &= ~0x02;        /* PSC3_5 is not open Drain */
136                 gpio->sint_dvo &= ~0x02;        /* PSC3_5 is LOW */
137                 gpio->sint_ddr |=  0x02;        /* PSC3_5 as output */
138                 /*
139                  * wait some time to allow amplifier to recover from shutdown
140                  * mode.
141                  */
142                 for(i = 0; i < 350; i++)
143                         udelay(1000);
144                 /*
145                  * The used amplifier (LM4867) has a so called "pop and click"
146                  * elmination filter. The input signal of the amplifier must
147                  * exceed a certain level once after power up to activate the
148                  * generation of the output signal. This is achieved by
149                  * sending a low frequent (nearly inaudible) sawtooth with a
150                  * sufficient signal level.
151                  */
152                 set_attenuation(50);
153                 i2s_sawtooth (200, 5, LEFT_RIGHT);
154                 init_done = 1;
155         }
156 }
157
158 static void i2s_init(void)
159 {
160         unsigned long i;
161         struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;;
162         struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO;
163
164         gpio->port_config |= 0x00000070; /* PSC2 ports as Codec with MCLK */
165         psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);
166         psc->sicr = 0x22E00000;         /* 16 bit data; I2S */
167
168         *(vu_long *)(CFG_MBAR + 0x22C) = 0x805d; /* PSC2 CDM MCLK config; MCLK
169                                                   * 5.617 MHz */
170         *(vu_long *)(CFG_MBAR + 0x214) |= 0x00000040; /* CDM clock enable
171                                                        * register */
172         psc->ccr = 0x1F03;      /* 16 bit data width; 5.617MHz MCLK */
173         psc->ctur = 0x0F;       /* 16 bit frame width */
174
175         for(i=0;i<128;i++)
176         {
177                 psc->psc_buffer_32 = 0; /* clear tx fifo */
178         }
179 }
180
181 static int i2s_play_wave(unsigned long addr, unsigned long len)
182 {
183         unsigned long i;
184         unsigned char *wave_file = (uchar *)addr + 44;  /* quick'n dirty: skip
185                                                          * wav header*/
186         unsigned char swapped[4];
187         struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;
188
189         /*
190          * play wave file in memory; bytes/words are be swapped
191          */
192         psc->command = (PSC_RX_ENABLE | PSC_TX_ENABLE);
193
194         for(i = 0;i < (len / 4); i++) {
195                 swapped[3]=*wave_file++;
196                 swapped[2]=*wave_file++;
197                 swapped[1]=*wave_file++;
198                 swapped[0]=*wave_file++;
199                 psc->psc_buffer_32 =  *((unsigned long*)swapped);
200                 while (psc->tfnum > 400) {
201                         if(ctrlc())
202                                 return 0;
203                 }
204         }
205         while (psc->tfnum > 0);         /* wait for fifo empty */
206         udelay (100);
207         psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);
208         return 0;
209 }
210
211 static int i2s_sawtooth(unsigned long duration, unsigned int freq,
212                         unsigned int channel)
213 {
214         long i,j;
215         unsigned long data;
216         struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;
217
218         psc->command = (PSC_RX_ENABLE | PSC_TX_ENABLE);
219
220         /*
221          * Generate sawtooth. Start with middle level up to highest level. Then
222          * go to lowest level and back to middle level.
223          */
224         for(j = 0; j < ((duration * freq) / 1000); j++) {
225                 for(i = 0; i <= 0x7FFF; i += (0x7FFF/(44100/(freq*4)))) {
226                         data = (i & 0xFFFF);
227                         /* data format: right data left data) */
228                         if (channel == LEFT_RIGHT)
229                                 data |= (data<<16);
230                         if (channel == RIGHT)
231                                 data = (data<<16);
232                         psc->psc_buffer_32 = data;
233                         while (psc->tfnum > 400);
234                 }
235                 for(i = 0x7FFF; i >= -0x7FFF; i -= (0xFFFF/(44100/(freq*2)))) {
236                         data = (i & 0xFFFF);
237                         /* data format: right data left data) */
238                         if (channel == LEFT_RIGHT)
239                                 data |= (data<<16);
240                         if (channel == RIGHT)
241                                 data = (data<<16);
242                         psc->psc_buffer_32 = data;
243                         while (psc->tfnum > 400);
244                 }
245                 for(i = -0x7FFF; i <= 0; i += (0x7FFF/(44100/(freq*4)))) {
246                         data = (i & 0xFFFF);
247                         /* data format: right data left data) */
248                         if (channel == LEFT_RIGHT)
249                                 data |= (data<<16);
250                         if (channel == RIGHT)
251                                 data = (data<<16);
252                         psc->psc_buffer_32 = data;
253                         while (psc->tfnum > 400);
254                 }
255         }
256         while (psc->tfnum > 0);         /* wait for fifo empty */
257         udelay (100);
258         psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);
259
260         return 0;
261 }
262
263 static int i2s_squarewave(unsigned long duration, unsigned int freq,
264                          unsigned int channel)
265 {
266         long i,j;
267         unsigned long data;
268         struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;
269
270         psc->command = (PSC_RX_ENABLE | PSC_TX_ENABLE);
271
272         /*
273          * Generate sqarewave. Start with high level, duty cycle 1:1.
274          */
275         for(j = 0; j < ((duration * freq) / 1000); j++) {
276                 for(i = 0; i < (44100/(freq*2)); i ++) {
277                         data = 0x7FFF;
278                         /* data format: right data left data) */
279                         if (channel == LEFT_RIGHT)
280                                 data |= (data<<16);
281                         if (channel == RIGHT)
282                                 data = (data<<16);
283                         psc->psc_buffer_32 = data;
284                         while (psc->tfnum > 400);
285                 }
286                 for(i = 0; i < (44100/(freq*2)); i ++) {
287                         data = 0x8000;
288                         /* data format: right data left data) */
289                         if (channel == LEFT_RIGHT)
290                                 data |= (data<<16);
291                         if (channel == RIGHT)
292                                 data = (data<<16);
293                         psc->psc_buffer_32 = data;
294                         while (psc->tfnum > 400);
295                 }
296         }
297         while (psc->tfnum > 0);         /* wait for fifo empty */
298         udelay (100);
299         psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);
300
301         return 0;
302 }
303
304 static int cmd_sound(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
305 {
306         unsigned long reg, val, duration;
307         char *tmp;
308         unsigned int freq, channel;
309         unsigned char volume;
310         int rcode = 1;
311
312 #ifdef CONFIG_STK52XX_REV100
313         printf ("Revision 100 of STK52XX not supported!\n");
314         return 1;
315 #endif
316         spi_init();
317         i2s_init();
318         amplifier_init();
319
320         if ((tmp = getenv ("volume")) != NULL) {
321                 volume = simple_strtoul (tmp, NULL, 10);
322         } else {
323                 volume = DEFAULT_VOL;
324         }
325         set_attenuation(volume);
326
327         switch (argc) {
328         case 0:
329         case 1:
330                 printf ("Usage:\n%s\n", cmdtp->usage);
331                 return 1;
332         case 2:
333                 if (strncmp(argv[1],"saw",3) == 0) {
334                         printf ("Play sawtooth\n");
335                         rcode = i2s_sawtooth (DEFAULT_DURATION, DEFAULT_FREQ,
336                                               LEFT_RIGHT);
337                         return rcode;
338                 } else if (strncmp(argv[1],"squ",3) == 0) {
339                         printf ("Play squarewave\n");
340                         rcode = i2s_squarewave (DEFAULT_DURATION, DEFAULT_FREQ,
341                                                 LEFT_RIGHT);
342                         return rcode;
343                 }
344
345                 printf ("Usage:\n%s\n", cmdtp->usage);
346                 return 1;
347         case 3:
348                 if (strncmp(argv[1],"saw",3) == 0) {
349                         duration = simple_strtoul(argv[2], NULL, 10);
350                         printf ("Play sawtooth\n");
351                         rcode = i2s_sawtooth (duration, DEFAULT_FREQ,
352                                               LEFT_RIGHT);
353                         return rcode;
354                 } else if (strncmp(argv[1],"squ",3) == 0) {
355                         duration = simple_strtoul(argv[2], NULL, 10);
356                         printf ("Play squarewave\n");
357                         rcode = i2s_squarewave (duration, DEFAULT_FREQ,
358                                                 LEFT_RIGHT);
359                         return rcode;
360                 }
361                 printf ("Usage:\n%s\n", cmdtp->usage);
362                 return 1;
363         case 4:
364                 if (strncmp(argv[1],"saw",3) == 0) {
365                         duration = simple_strtoul(argv[2], NULL, 10);
366                         freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
367                         printf ("Play sawtooth\n");
368                         rcode = i2s_sawtooth (duration, freq,
369                                               LEFT_RIGHT);
370                         return rcode;
371                 } else if (strncmp(argv[1],"squ",3) == 0) {
372                         duration = simple_strtoul(argv[2], NULL, 10);
373                         freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
374                         printf ("Play squarewave\n");
375                         rcode = i2s_squarewave (duration, freq,
376                                                 LEFT_RIGHT);
377                         return rcode;
378                 } else if (strcmp(argv[1],"pcm1772") == 0) {
379                         reg = simple_strtoul(argv[2], NULL, 10);
380                         val = simple_strtoul(argv[3], NULL, 10);
381                         printf("Set PCM1772 %lu. %lu\n", reg, val);
382                         pcm1772_write_reg((uchar)reg, (uchar)val);
383                         return 0;
384                 }
385                 printf ("Usage:\n%s\n", cmdtp->usage);
386                 return 1;
387         case 5:
388                 if (strncmp(argv[1],"saw",3) == 0) {
389                         duration = simple_strtoul(argv[2], NULL, 10);
390                         freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
391                         if (strncmp(argv[4],"l",1) == 0)
392                                 channel = LEFT;
393                         else if (strncmp(argv[4],"r",1) == 0)
394                                 channel = RIGHT;
395                         else
396                                 channel = LEFT_RIGHT;
397                         printf ("Play squarewave\n");
398                         rcode = i2s_sawtooth (duration, freq,
399                                               channel);
400                         return rcode;
401                 } else if (strncmp(argv[1],"squ",3) == 0) {
402                         duration = simple_strtoul(argv[2], NULL, 10);
403                         freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
404                         if (strncmp(argv[4],"l",1) == 0)
405                                 channel = LEFT;
406                         else if (strncmp(argv[4],"r",1) == 0)
407                                 channel = RIGHT;
408                         else
409                                 channel = LEFT_RIGHT;
410                         printf ("Play squarewave\n");
411                         rcode = i2s_squarewave (duration, freq,
412                                                 channel);
413                         return rcode;
414                 }
415                 printf ("Usage:\n%s\n", cmdtp->usage);
416                 return 1;
417         }
418         printf ("Usage:\nsound cmd [arg1] [arg2] ...\n");
419         return 1;
420 }
421
422 static int cmd_wav(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
423 {
424         unsigned long length, addr;
425         unsigned char volume;
426         int rcode = 1;
427         char *tmp;
428
429 #ifdef CONFIG_STK52XX_REV100
430         printf ("Revision 100 of STK52XX not supported!\n");
431         return 1;
432 #endif
433         spi_init();
434         i2s_init();
435         amplifier_init();
436
437         switch (argc) {
438
439         case 3:
440                 length = simple_strtoul(argv[2], NULL, 16);
441                 addr = simple_strtoul(argv[1], NULL, 16);
442                 break;
443
444         case 2:
445                 if ((tmp = getenv ("filesize")) != NULL) {
446                         length = simple_strtoul (tmp, NULL, 16);
447                 } else {
448                         puts ("No filesize provided\n");
449                         return 1;
450                 }
451                 addr = simple_strtoul(argv[1], NULL, 16);
452
453         case 1:
454                 if ((tmp = getenv ("filesize")) != NULL) {
455                         length = simple_strtoul (tmp, NULL, 16);
456                 } else {
457                         puts ("No filesize provided\n");
458                         return 1;
459                 }
460                 if ((tmp = getenv ("loadaddr")) != NULL) {
461                         addr = simple_strtoul (tmp, NULL, 16);
462                 } else {
463                         puts ("No loadaddr provided\n");
464                         return 1;
465                 }
466                 break;
467
468         default:
469                 printf("Usage:\nwav <addr> <length[s]\n");
470                 return 1;
471                 break;
472         }
473
474         if ((tmp = getenv ("volume")) != NULL) {
475                 volume = simple_strtoul (tmp, NULL, 10);
476         } else {
477                 volume = DEFAULT_VOL;
478         }
479         set_attenuation(volume);
480
481         printf("Play wave file at %#p with length %#x\n", addr, length);
482         rcode = i2s_play_wave(addr, length);
483
484         return rcode;
485 }
486
487 static int cmd_beep(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
488 {
489         unsigned char volume;
490         unsigned int channel;
491         int rcode;
492         char *tmp;
493
494 #ifdef CONFIG_STK52XX_REV100
495         printf ("Revision 100 of STK52XX not supported!\n");
496         return 1;
497 #endif
498         spi_init();
499         i2s_init();
500         amplifier_init();
501
502         switch (argc) {
503         case 0:
504         case 1:
505                 channel = LEFT_RIGHT;
506                 break;
507         case 2:
508                 if (strncmp(argv[1],"l",1) == 0)
509                         channel = LEFT;
510                 else if (strncmp(argv[1],"r",1) == 0)
511                         channel = RIGHT;
512                 else
513                         channel = LEFT_RIGHT;
514                 break;
515         default:
516                 printf ("Usage:\n%s\n", cmdtp->usage);
517                 return 1;
518         }
519
520         if ((tmp = getenv ("volume")) != NULL) {
521                 volume = simple_strtoul (tmp, NULL, 10);
522         } else {
523                 volume = DEFAULT_VOL;
524         }
525         set_attenuation(volume);
526
527         printf("Beep on ");
528         if (channel == LEFT)
529                 printf ("left ");
530         else if (channel == RIGHT)
531                 printf ("right ");
532         else
533                 printf ("left and right ");
534         printf ("channel\n");
535
536         rcode = i2s_squarewave (DEFAULT_DURATION, DEFAULT_FREQ, channel);
537
538         return rcode;
539 }
540 #endif
541
542 #if defined(CONFIG_STK52XX)
543 void led_init(void)
544 {
545         struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
546         struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
547
548         /* configure PSC3 for SPI and GPIO */
549         gpio->port_config &= ~(0x00000F00);
550         gpio->port_config |=   0x00000800;
551
552         gpio->simple_gpioe &= ~(0x00000F00);
553         gpio->simple_gpioe |=   0x00000F00;
554
555         gpio->simple_ddr &= ~(0x00000F00);
556         gpio->simple_ddr |=   0x00000F00;
557
558         /* configure timer 4-7 for simple GPIO output */
559         gpt->gpt4.emsr |=  0x00000024;
560         gpt->gpt5.emsr |=  0x00000024;
561         gpt->gpt6.emsr |=  0x00000024;
562         gpt->gpt7.emsr |=  0x00000024;
563
564
565         /* enable SM501 GPIO control (in both power modes) */
566         *(vu_long *) (SM501_MMIO_BASE+SM501_POWER_MODE0_GATE) |=
567                 POWER_MODE_GATE_GPIO_PWM_I2C;
568         *(vu_long *) (SM501_MMIO_BASE+SM501_POWER_MODE1_GATE) |=
569                 POWER_MODE_GATE_GPIO_PWM_I2C;
570
571         /* configure SM501 gpio pins 24-27 as output */
572         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_CTRL_LOW) &= ~(0xF << 24);
573         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_DIR_LOW) |= (0xF << 24);
574
575         /* configure SM501 gpio pins 48-51 as output */
576         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_DIR_HIGH) |= (0xF << 16);
577 }
578
579 /*
580  * return 1 if led number unknown
581  * return 0 else
582  */
583 int do_led(char *argv[])
584 {
585         struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
586         struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
587
588         switch  (simple_strtoul(argv[2], NULL, 10)) {
589
590         case 0:
591                 if (strcmp (argv[3], "on") == 0) {
592                         gpio->simple_dvo |=   (1 << 8);
593                 } else {
594                         gpio->simple_dvo &= ~(1 << 8);
595                 }
596                 break;
597
598         case 1:
599                 if (strcmp (argv[3], "on") == 0) {
600                         gpio->simple_dvo |=   (1 << 9);
601                 } else {
602                         gpio->simple_dvo &= ~(1 << 9);
603                 }
604                 break;
605
606         case 2:
607                 if (strcmp (argv[3], "on") == 0) {
608                         gpio->simple_dvo |=   (1 << 10);
609                 } else {
610                         gpio->simple_dvo &= ~(1 << 10);
611                 }
612                 break;
613
614         case 3:
615                 if (strcmp (argv[3], "on") == 0) {
616                         gpio->simple_dvo |=   (1 << 11);
617                 } else {
618                         gpio->simple_dvo &= ~(1 << 11);
619                 }
620                 break;
621
622         case 4:
623                 if (strcmp (argv[3], "on") == 0) {
624                         gpt->gpt4.emsr |=  (1 << 4);
625                 } else {
626                         gpt->gpt4.emsr &=  ~(1 << 4);
627                 }
628                 break;
629
630         case 5:
631                 if (strcmp (argv[3], "on") == 0) {
632                         gpt->gpt5.emsr |=  (1 << 4);
633                 } else {
634                         gpt->gpt5.emsr &=  ~(1 << 4);
635                 }
636                 break;
637
638         case 6:
639                 if (strcmp (argv[3], "on") == 0) {
640                         gpt->gpt6.emsr |=  (1 << 4);
641                 } else {
642                         gpt->gpt6.emsr &=  ~(1 << 4);
643                 }
644                 break;
645
646         case 7:
647                 if (strcmp (argv[3], "on") == 0) {
648                         gpt->gpt7.emsr |=  (1 << 4);
649                 } else {
650                         gpt->gpt7.emsr &=  ~(1 << 4);
651                 }
652                 break;
653
654         case 24:
655                 if (strcmp (argv[3], "on") == 0) {
656                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
657                                 (0x1 << 24);
658                 } else {
659                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
660                                 ~(0x1 << 24);
661                 }
662                 break;
663
664         case 25:
665                 if (strcmp (argv[3], "on") == 0) {
666                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
667                                 (0x1 << 25);
668                 } else {
669                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
670                                 ~(0x1 << 25);
671                 }
672                 break;
673
674         case 26:
675                 if (strcmp (argv[3], "on") == 0) {
676                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
677                                 (0x1 << 26);
678                 } else {
679                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
680                                 ~(0x1 << 26);
681                 }
682                 break;
683
684         case 27:
685                 if (strcmp (argv[3], "on") == 0) {
686                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
687                                 (0x1 << 27);
688                 } else {
689                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
690                                 ~(0x1 << 27);
691                 }
692                 break;
693
694         case 48:
695                 if (strcmp (argv[3], "on") == 0) {
696                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
697                                 (0x1 << 16);
698                 } else {
699                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
700                                 ~(0x1 << 16);
701                 }
702                 break;
703
704         case 49:
705                 if (strcmp (argv[3], "on") == 0) {
706                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
707                                 (0x1 << 17);
708                 } else {
709                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
710                                 ~(0x1 << 17);
711                 }
712                 break;
713
714         case 50:
715                 if (strcmp (argv[3], "on") == 0) {
716                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
717                                 (0x1 << 18);
718                 } else {
719                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
720                                 ~(0x1 << 18);
721                 }
722                 break;
723
724         case 51:
725                 if (strcmp (argv[3], "on") == 0) {
726                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
727                                 (0x1 << 19);
728                 } else {
729                         *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
730                                 ~(0x1 << 19);
731                 }
732                 break;
733
734         default:
735                 printf ("%s: invalid led number %s\n", __FUNCTION__, argv[2]);
736                 return 1;
737         }
738
739         return 0;
740 }
741 #endif
742
743 #if defined(CONFIG_STK52XX) || defined(CONFIG_FO300)
744 /*
745  * return 1 on CAN initialization failure
746  * return 0 if no failure
747  */
748 int can_init(void)
749 {
750         static int init_done = 0;
751         int i;
752         struct mpc5xxx_mscan *can1 =
753                 (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0900);
754         struct mpc5xxx_mscan *can2 =
755                 (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0980);
756
757         /* GPIO configuration of the CAN pins is done in TQM5200.h */
758
759         if (!init_done) {
760                 /* init CAN 1 */
761                 can1->canctl1 |= 0x80;  /* CAN enable */
762                 udelay(100);
763
764                 i = 0;
765                 can1->canctl0 |= 0x02;  /* sleep mode */
766                 /* wait until sleep mode reached */
767                 while (!(can1->canctl1 & 0x02)) {
768                         udelay(10);
769                 i++;
770                 if (i == 10) {
771                         printf ("%s: CAN1 initialize error, "
772                                 "can not enter sleep mode!\n",
773                                 __FUNCTION__);
774                         return 1;
775                 }
776                 }
777                 i = 0;
778                 can1->canctl0 = 0x01;   /* enter init mode */
779                 /* wait until init mode reached */
780                 while (!(can1->canctl1 & 0x01)) {
781                         udelay(10);
782                         i++;
783                         if (i == 10) {
784                                 printf ("%s: CAN1 initialize error, "
785                                         "can not enter init mode!\n",
786                                         __FUNCTION__);
787                                 return 1;
788                         }
789                 }
790                 can1->canctl1 = 0x80;
791                 can1->canctl1 |= 0x40;
792                 can1->canbtr0 = 0x0F;
793                 can1->canbtr1 = 0x7F;
794                 can1->canidac &= ~(0x30);
795                 can1->canidar1 = 0x00;
796                 can1->canidar3 = 0x00;
797                 can1->canidar5 = 0x00;
798                 can1->canidar7 = 0x00;
799                 can1->canidmr0 = 0xFF;
800                 can1->canidmr1 = 0xFF;
801                 can1->canidmr2 = 0xFF;
802                 can1->canidmr3 = 0xFF;
803                 can1->canidmr4 = 0xFF;
804                 can1->canidmr5 = 0xFF;
805                 can1->canidmr6 = 0xFF;
806                 can1->canidmr7 = 0xFF;
807
808                 i = 0;
809                 can1->canctl0 &= ~(0x01);       /* leave init mode */
810                 can1->canctl0 &= ~(0x02);
811                 /* wait until init and sleep mode left */
812                 while ((can1->canctl1 & 0x01) || (can1->canctl1 & 0x02)) {
813                         udelay(10);
814                         i++;
815                         if (i == 10) {
816                                 printf ("%s: CAN1 initialize error, "
817                                         "can not leave init/sleep mode!\n",
818                                         __FUNCTION__);
819                                 return 1;
820                         }
821                 }
822
823                 /* init CAN 2 */
824                 can2->canctl1 |= 0x80;  /* CAN enable */
825                 udelay(100);
826
827                 i = 0;
828                 can2->canctl0 |= 0x02;  /* sleep mode */
829                 /* wait until sleep mode reached */
830                 while (!(can2->canctl1 & 0x02)) {
831                         udelay(10);
832                         i++;
833                         if (i == 10) {
834                                 printf ("%s: CAN2 initialize error, "
835                                         "can not enter sleep mode!\n",
836                                         __FUNCTION__);
837                                 return 1;
838                         }
839                 }
840                 i = 0;
841                 can2->canctl0 = 0x01;   /* enter init mode */
842                 /* wait until init mode reached */
843                 while (!(can2->canctl1 & 0x01)) {
844                         udelay(10);
845                         i++;
846                         if (i == 10) {
847                                 printf ("%s: CAN2 initialize error, "
848                                         "can not enter init mode!\n",
849                                         __FUNCTION__);
850                                 return 1;
851                         }
852                 }
853                 can2->canctl1 = 0x80;
854                 can2->canctl1 |= 0x40;
855                 can2->canbtr0 = 0x0F;
856                 can2->canbtr1 = 0x7F;
857                 can2->canidac &= ~(0x30);
858                 can2->canidar1 = 0x00;
859                 can2->canidar3 = 0x00;
860                 can2->canidar5 = 0x00;
861                 can2->canidar7 = 0x00;
862                 can2->canidmr0 = 0xFF;
863                 can2->canidmr1 = 0xFF;
864                 can2->canidmr2 = 0xFF;
865                 can2->canidmr3 = 0xFF;
866                 can2->canidmr4 = 0xFF;
867                 can2->canidmr5 = 0xFF;
868                 can2->canidmr6 = 0xFF;
869                 can2->canidmr7 = 0xFF;
870                 can2->canctl0 &= ~(0x01);       /* leave init mode */
871                 can2->canctl0 &= ~(0x02);
872
873                 i = 0;
874                 /* wait until init mode left */
875                 while ((can2->canctl1 & 0x01) || (can2->canctl1 & 0x02)) {
876                         udelay(10);
877                         i++;
878                         if (i == 10) {
879                                 printf ("%s: CAN2 initialize error, "
880                                         "can not leave init/sleep mode!\n",
881                                         __FUNCTION__);
882                                 return 1;
883                         }
884                 }
885                 init_done = 1;
886         }
887         return 0;
888 }
889
890 /*
891  * return 1 on CAN failure
892  * return 0 if no failure
893  */
894 int do_can(char *argv[])
895 {
896         int i;
897         struct mpc5xxx_mscan *can1 =
898                 (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0900);
899         struct mpc5xxx_mscan *can2 =
900                 (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0980);
901
902         /* send a message on CAN1 */
903         can1->cantbsel = 0x01;
904         can1->cantxfg.idr[0] = 0x55;
905         can1->cantxfg.idr[1] = 0x00;
906         can1->cantxfg.idr[1] &= ~0x8;
907         can1->cantxfg.idr[1] &= ~0x10;
908         can1->cantxfg.dsr[0] = 0xCC;
909         can1->cantxfg.dlr = 1;
910         can1->cantxfg.tbpr = 0;
911         can1->cantflg = 0x01;
912
913         i = 0;
914         while ((can1->cantflg & 0x01) == 0) {
915                 i++;
916                 if (i == 10) {
917                         printf ("%s: CAN1 send timeout, "
918                                 "can not send message!\n",
919                                 __FUNCTION__);
920                         return 1;
921                 }
922                 udelay(1000);
923         }
924         udelay(1000);
925
926         i = 0;
927         while (!(can2->canrflg & 0x01)) {
928                 i++;
929                 if (i == 10) {
930                         printf ("%s: CAN2 receive timeout, "
931                                 "no message received!\n",
932                                 __FUNCTION__);
933                         return 1;
934                 }
935                 udelay(1000);
936         }
937
938         if (can2->canrxfg.dsr[0] != 0xCC) {
939                 printf ("%s: CAN2 receive error, "
940                          "data mismatch!\n",
941                         __FUNCTION__);
942                 return 1;
943         }
944
945         /* send a message on CAN2 */
946         can2->cantbsel = 0x01;
947         can2->cantxfg.idr[0] = 0x55;
948         can2->cantxfg.idr[1] = 0x00;
949         can2->cantxfg.idr[1] &= ~0x8;
950         can2->cantxfg.idr[1] &= ~0x10;
951         can2->cantxfg.dsr[0] = 0xCC;
952         can2->cantxfg.dlr = 1;
953         can2->cantxfg.tbpr = 0;
954         can2->cantflg = 0x01;
955
956         i = 0;
957         while ((can2->cantflg & 0x01) == 0) {
958                 i++;
959                 if (i == 10) {
960                         printf ("%s: CAN2 send error, "
961                                 "can not send message!\n",
962                                 __FUNCTION__);
963                         return 1;
964                 }
965                 udelay(1000);
966         }
967         udelay(1000);
968
969         i = 0;
970         while (!(can1->canrflg & 0x01)) {
971                 i++;
972                 if (i == 10) {
973                         printf ("%s: CAN1 receive timeout, "
974                                 "no message received!\n",
975                                 __FUNCTION__);
976                         return 1;
977                 }
978                 udelay(1000);
979         }
980
981         if (can1->canrxfg.dsr[0] != 0xCC) {
982                 printf ("%s: CAN1 receive error 0x%02x\n",
983                         __FUNCTION__, (can1->canrxfg.dsr[0]));
984                 return 1;
985         }
986
987         return 0;
988 }
989
990 /*
991  * return 1 if rs232 port unknown
992  * return 2 on txd/rxd failure (only rs232 2)
993  * return 3 on rts/cts failure
994  * return 0 if no failure
995  */
996 int do_rs232(char *argv[])
997 {
998         int error_status = 0;
999         struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
1000         struct mpc5xxx_psc *psc1 = (struct mpc5xxx_psc *)MPC5XXX_PSC1;
1001
1002         switch  (simple_strtoul(argv[2], NULL, 10)) {
1003
1004         case 1:
1005                 /* check RTS <-> CTS loop */
1006                 /* set rts to 0 */
1007                 psc1->op1 |= 0x01;
1008
1009                 /* wait some time before requesting status */
1010                 udelay(10);
1011
1012                 /* check status at cts */
1013                 if ((psc1->ip & 0x01) != 0) {
1014                         error_status = 3;
1015                         printf ("%s: failure at rs232_1, cts status is %d "
1016                                 "(should be 0)\n",
1017                                 __FUNCTION__, (psc1->ip & 0x01));
1018                 }
1019
1020                 /* set rts to 1 */
1021                 psc1->op0 |= 0x01;
1022
1023                 /* wait some time before requesting status */
1024                 udelay(10);
1025
1026                 /* check status at cts */
1027                 if ((psc1->ip & 0x01) != 1) {
1028                         error_status = 3;
1029                         printf ("%s: failure at rs232_1, cts status is %d "
1030                                 "(should be 1)\n",
1031                                 __FUNCTION__, (psc1->ip & 0x01));
1032                 }
1033
1034                 break;
1035
1036         case 2:
1037                 /* set PSC3_0, PSC3_2 as output and PSC3_1, PSC3_3 as input */
1038                 gpio->simple_ddr &= ~(0x00000F00);
1039                 gpio->simple_ddr |=   0x00000500;
1040
1041                 /* check TXD <-> RXD loop */
1042                 /* set TXD to 1 */
1043                 gpio->simple_dvo |=   (1 << 8);
1044
1045                 /* wait some time before requesting status */
1046                 udelay(10);
1047
1048                 if ((gpio->simple_ival & 0x00000200) != 0x00000200) {
1049                         error_status = 2;
1050                         printf ("%s: failure at rs232_2, rxd status is %d "
1051                                 "(should be 1)\n",
1052                                 __FUNCTION__,
1053                                 (gpio->simple_ival & 0x00000200) >> 9);
1054                 }
1055
1056                 /* set TXD to 0 */
1057                 gpio->simple_dvo &= ~(1 << 8);
1058
1059                 /* wait some time before requesting status */
1060                 udelay(10);
1061
1062                 if ((gpio->simple_ival & 0x00000200) != 0x00000000) {
1063                         error_status = 2;
1064                         printf ("%s: failure at rs232_2, rxd status is %d "
1065                                 "(should be 0)\n",
1066                                 __FUNCTION__,
1067                                 (gpio->simple_ival & 0x00000200) >> 9);
1068                 }
1069
1070                 /* check RTS <-> CTS loop */
1071                 /* set RTS to 1 */
1072                 gpio->simple_dvo |=   (1 << 10);
1073
1074                 /* wait some time before requesting status */
1075                 udelay(10);
1076
1077                 if ((gpio->simple_ival & 0x00000800) != 0x00000800) {
1078                         error_status = 3;
1079                         printf ("%s: failure at rs232_2, cts status is %d "
1080                                 "(should be 1)\n",
1081                                 __FUNCTION__,
1082                                 (gpio->simple_ival & 0x00000800) >> 11);
1083                 }
1084
1085                 /* set RTS to 0 */
1086                 gpio->simple_dvo &= ~(1 << 10);
1087
1088                 /* wait some time before requesting status */
1089                 udelay(10);
1090
1091                 if ((gpio->simple_ival & 0x00000800) != 0x00000000) {
1092                         error_status = 3;
1093                         printf ("%s: failure at rs232_2, cts status is %d "
1094                                 "(should be 0)\n",
1095                                 __FUNCTION__,
1096                                 (gpio->simple_ival & 0x00000800) >> 11);
1097                 }
1098
1099                 /* set PSC3_0, PSC3_1, PSC3_2 and PSC3_3 as output */
1100                 gpio->simple_ddr &= ~(0x00000F00);
1101                 gpio->simple_ddr |=   0x00000F00;
1102                 break;
1103
1104         default:
1105                 printf ("%s: invalid rs232 number %s\n", __FUNCTION__, argv[2]);
1106                 error_status = 1;
1107                 break;
1108         }
1109
1110         return error_status;
1111 }
1112
1113 #ifndef CONFIG_FO300
1114 static void sm501_backlight (unsigned int state)
1115 {
1116         if (state == BL_ON) {
1117                 *(vu_long *)(SM501_MMIO_BASE+SM501_PANEL_DISPLAY_CONTROL) |=
1118                         (1 << 26) | (1 << 27);
1119         } else if (state == BL_OFF)
1120                 *(vu_long *)(SM501_MMIO_BASE+SM501_PANEL_DISPLAY_CONTROL) &=
1121                         ~((1 << 26) | (1 << 27));
1122 }
1123 #endif
1124
1125 int cmd_fkt(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1126 {
1127         int rcode;
1128
1129 #ifdef CONFIG_STK52XX_REV100
1130         printf ("Revision 100 of STK52XX not supported!\n");
1131         return 1;
1132 #endif
1133 #if defined(CONFIG_STK52XX)
1134         led_init();
1135 #endif
1136         can_init();
1137
1138         switch (argc) {
1139
1140         case 0:
1141         case 1:
1142                 break;
1143
1144         case 2:
1145                 if (strncmp (argv[1], "can", 3) == 0) {
1146                         rcode = do_can (argv);
1147                         if (rcode == 0)
1148                                 printf ("OK\n");
1149                         else
1150                                 printf ("Error\n");
1151                         return rcode;
1152                 }
1153                 break;
1154
1155         case 3:
1156                 if (strncmp (argv[1], "rs232", 3) == 0) {
1157                         rcode = do_rs232 (argv);
1158                         if (rcode == 0)
1159                                 printf ("OK\n");
1160                         else
1161                                 printf ("Error\n");
1162                         return rcode;
1163 #ifndef CONFIG_FO300
1164                 } else if (strncmp (argv[1], "backlight", 4) == 0) {
1165                         if (strncmp (argv[2], "on", 2) == 0) {
1166                                 sm501_backlight (BL_ON);
1167                                 return 0;
1168                         }
1169                         else if (strncmp (argv[2], "off", 3) == 0) {
1170                                 sm501_backlight (BL_OFF);
1171                                 return 0;
1172                         }
1173 #endif
1174                 }
1175                 break;
1176
1177 #if defined(CONFIG_STK52XX)
1178         case 4:
1179                 if (strcmp (argv[1], "led") == 0) {
1180                         return (do_led (argv));
1181                 }
1182                 break;
1183 #endif
1184
1185         default:
1186                 break;
1187         }
1188
1189         printf ("Usage:\nfkt cmd [arg1] [arg2] ...\n");
1190         return 1;
1191 }
1192
1193
1194 U_BOOT_CMD(
1195         sound ,    5,    1,     cmd_sound,
1196         "sound   - Sound sub-system\n",
1197         "saw [duration] [freq] [channel]\n"
1198         "    - generate sawtooth for 'duration' ms with frequency 'freq'\n"
1199         "      on left \"l\" or right \"r\" channel\n"
1200         "sound square [duration] [freq] [channel]\n"
1201         "    - generate squarewave for 'duration' ms with frequency 'freq'\n"
1202         "      on left \"l\" or right \"r\" channel\n"
1203         "pcm1772 reg val\n"
1204 );
1205
1206 U_BOOT_CMD(
1207         wav ,    3,    1,     cmd_wav,
1208         "wav     - play wav file\n",
1209         "[addr] [bytes]\n"
1210         "    - play wav file at address 'addr' with length 'bytes'\n"
1211 );
1212
1213 U_BOOT_CMD(
1214         beep ,    2,    1,     cmd_beep,
1215         "beep    - play short beep\n",
1216         "[channel]\n"
1217         "    - play short beep on \"l\"eft or \"r\"ight channel\n"
1218 );
1219 #endif /* CONFIG_STK52XX  || CONFIG_FO300 */
1220
1221 #if defined(CONFIG_STK52XX)
1222 U_BOOT_CMD(
1223         fkt ,   4,      1,      cmd_fkt,
1224         "fkt     - Function test routines\n",
1225         "led number on/off\n"
1226         "     - 'number's like printed on STK52XX board\n"
1227         "fkt can\n"
1228         "     - loopback plug for X83 required\n"
1229         "fkt rs232 number\n"
1230         "     - loopback plug(s) for X2 required\n"
1231         "fkt backlight on/off\n"
1232         "     - switch backlight on or off\n"
1233 );
1234 #elif defined(CONFIG_FO300)
1235 U_BOOT_CMD(
1236         fkt ,   3,      1,      cmd_fkt,
1237         "fkt     - Function test routines\n",
1238         "fkt can\n"
1239         "     - loopback plug for X16/X29 required\n"
1240         "fkt rs232 number\n"
1241         "     - loopback plug(s) for X21/X22 required\n"
1242 );
1243 #endif
1244 #endif