PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/u-boot-1.1.6/board/tqm5200/cmd_stk52xx.c

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