PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://snake-os.googlecode.com/
C | 1221 lines | 962 code | 139 blank | 120 comment | 231 complexity | 5cb7a384f281af2f237dd52ee0abdab2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, 0BSD, BSD-3-Clause, GPL-3.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. * SKT52XX specific functions
  25. */
  26. /*#define DEBUG*/
  27. #include <common.h>
  28. #include <command.h>
  29. #if (CONFIG_COMMANDS & CFG_CMD_BSP)
  30. #define DEFAULT_VOL 45
  31. #define DEFAULT_FREQ 500
  32. #define DEFAULT_DURATION 200
  33. #define LEFT 1
  34. #define RIGHT 2
  35. #define LEFT_RIGHT 3
  36. #define BL_OFF 0
  37. #define BL_ON 1
  38. #define SM501_GPIO_CTRL_LOW 0x00000008UL
  39. #define SM501_GPIO_CTRL_HIGH 0x0000000CUL
  40. #define SM501_POWER_MODE0_GATE 0x00000040UL
  41. #define SM501_POWER_MODE1_GATE 0x00000048UL
  42. #define POWER_MODE_GATE_GPIO_PWM_I2C 0x00000040UL
  43. #define SM501_GPIO_DATA_LOW 0x00010000UL
  44. #define SM501_GPIO_DATA_HIGH 0x00010004UL
  45. #define SM501_GPIO_DATA_DIR_LOW 0x00010008UL
  46. #define SM501_GPIO_DATA_DIR_HIGH 0x0001000CUL
  47. #define SM501_PANEL_DISPLAY_CONTROL 0x00080000UL
  48. static int i2s_squarewave(unsigned long duration, unsigned int freq,
  49. unsigned int channel);
  50. static int i2s_sawtooth(unsigned long duration, unsigned int freq,
  51. unsigned int channel);
  52. static void spi_init(void);
  53. static int spi_transmit(unsigned char data);
  54. static void pcm1772_write_reg(unsigned char addr, unsigned char data);
  55. static void set_attenuation(unsigned char attenuation);
  56. #ifdef CONFIG_STK52XX
  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. void led_init(void)
  489. {
  490. struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
  491. struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
  492. /* configure PSC3 for SPI and GPIO */
  493. gpio->port_config &= ~(0x00000F00);
  494. gpio->port_config |= 0x00000800;
  495. gpio->simple_gpioe &= ~(0x00000F00);
  496. gpio->simple_gpioe |= 0x00000F00;
  497. gpio->simple_ddr &= ~(0x00000F00);
  498. gpio->simple_ddr |= 0x00000F00;
  499. /* configure timer 4-7 for simple GPIO output */
  500. gpt->gpt4.emsr |= 0x00000024;
  501. gpt->gpt5.emsr |= 0x00000024;
  502. gpt->gpt6.emsr |= 0x00000024;
  503. gpt->gpt7.emsr |= 0x00000024;
  504. /* enable SM501 GPIO control (in both power modes) */
  505. *(vu_long *) (SM501_MMIO_BASE+SM501_POWER_MODE0_GATE) |=
  506. POWER_MODE_GATE_GPIO_PWM_I2C;
  507. *(vu_long *) (SM501_MMIO_BASE+SM501_POWER_MODE1_GATE) |=
  508. POWER_MODE_GATE_GPIO_PWM_I2C;
  509. /* configure SM501 gpio pins 24-27 as output */
  510. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_CTRL_LOW) &= ~(0xF << 24);
  511. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_DIR_LOW) |= (0xF << 24);
  512. /* configure SM501 gpio pins 48-51 as output */
  513. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_DIR_HIGH) |= (0xF << 16);
  514. }
  515. /*
  516. * return 1 if led number unknown
  517. * return 0 else
  518. */
  519. int do_led(char *argv[])
  520. {
  521. struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
  522. struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
  523. switch (simple_strtoul(argv[2], NULL, 10)) {
  524. case 0:
  525. if (strcmp (argv[3], "on") == 0) {
  526. gpio->simple_dvo |= (1 << 8);
  527. } else {
  528. gpio->simple_dvo &= ~(1 << 8);
  529. }
  530. break;
  531. case 1:
  532. if (strcmp (argv[3], "on") == 0) {
  533. gpio->simple_dvo |= (1 << 9);
  534. } else {
  535. gpio->simple_dvo &= ~(1 << 9);
  536. }
  537. break;
  538. case 2:
  539. if (strcmp (argv[3], "on") == 0) {
  540. gpio->simple_dvo |= (1 << 10);
  541. } else {
  542. gpio->simple_dvo &= ~(1 << 10);
  543. }
  544. break;
  545. case 3:
  546. if (strcmp (argv[3], "on") == 0) {
  547. gpio->simple_dvo |= (1 << 11);
  548. } else {
  549. gpio->simple_dvo &= ~(1 << 11);
  550. }
  551. break;
  552. case 4:
  553. if (strcmp (argv[3], "on") == 0) {
  554. gpt->gpt4.emsr |= (1 << 4);
  555. } else {
  556. gpt->gpt4.emsr &= ~(1 << 4);
  557. }
  558. break;
  559. case 5:
  560. if (strcmp (argv[3], "on") == 0) {
  561. gpt->gpt5.emsr |= (1 << 4);
  562. } else {
  563. gpt->gpt5.emsr &= ~(1 << 4);
  564. }
  565. break;
  566. case 6:
  567. if (strcmp (argv[3], "on") == 0) {
  568. gpt->gpt6.emsr |= (1 << 4);
  569. } else {
  570. gpt->gpt6.emsr &= ~(1 << 4);
  571. }
  572. break;
  573. case 7:
  574. if (strcmp (argv[3], "on") == 0) {
  575. gpt->gpt7.emsr |= (1 << 4);
  576. } else {
  577. gpt->gpt7.emsr &= ~(1 << 4);
  578. }
  579. break;
  580. case 24:
  581. if (strcmp (argv[3], "on") == 0) {
  582. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
  583. (0x1 << 24);
  584. } else {
  585. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
  586. ~(0x1 << 24);
  587. }
  588. break;
  589. case 25:
  590. if (strcmp (argv[3], "on") == 0) {
  591. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
  592. (0x1 << 25);
  593. } else {
  594. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
  595. ~(0x1 << 25);
  596. }
  597. break;
  598. case 26:
  599. if (strcmp (argv[3], "on") == 0) {
  600. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
  601. (0x1 << 26);
  602. } else {
  603. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
  604. ~(0x1 << 26);
  605. }
  606. break;
  607. case 27:
  608. if (strcmp (argv[3], "on") == 0) {
  609. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
  610. (0x1 << 27);
  611. } else {
  612. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
  613. ~(0x1 << 27);
  614. }
  615. break;
  616. case 48:
  617. if (strcmp (argv[3], "on") == 0) {
  618. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
  619. (0x1 << 16);
  620. } else {
  621. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
  622. ~(0x1 << 16);
  623. }
  624. break;
  625. case 49:
  626. if (strcmp (argv[3], "on") == 0) {
  627. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
  628. (0x1 << 17);
  629. } else {
  630. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
  631. ~(0x1 << 17);
  632. }
  633. break;
  634. case 50:
  635. if (strcmp (argv[3], "on") == 0) {
  636. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
  637. (0x1 << 18);
  638. } else {
  639. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
  640. ~(0x1 << 18);
  641. }
  642. break;
  643. case 51:
  644. if (strcmp (argv[3], "on") == 0) {
  645. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
  646. (0x1 << 19);
  647. } else {
  648. *(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
  649. ~(0x1 << 19);
  650. }
  651. break;
  652. default:
  653. printf ("%s: invalid led number %s\n", __FUNCTION__, argv[2]);
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. /*
  659. * return 1 on CAN initialization failure
  660. * return 0 if no failure
  661. */
  662. int can_init(void)
  663. {
  664. static int init_done = 0;
  665. int i;
  666. struct mpc5xxx_mscan *can1 =
  667. (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0900);
  668. struct mpc5xxx_mscan *can2 =
  669. (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0980);
  670. /* GPIO configuration of the CAN pins is done in TQM5200.h */
  671. if (!init_done) {
  672. /* init CAN 1 */
  673. can1->canctl1 |= 0x80; /* CAN enable */
  674. udelay(100);
  675. i = 0;
  676. can1->canctl0 |= 0x02; /* sleep mode */
  677. /* wait until sleep mode reached */
  678. while (!(can1->canctl1 & 0x02)) {
  679. udelay(10);
  680. i++;
  681. if (i == 10) {
  682. printf ("%s: CAN1 initialize error, "
  683. "can not enter sleep mode!\n",
  684. __FUNCTION__);
  685. return 1;
  686. }
  687. }
  688. i = 0;
  689. can1->canctl0 = 0x01; /* enter init mode */
  690. /* wait until init mode reached */
  691. while (!(can1->canctl1 & 0x01)) {
  692. udelay(10);
  693. i++;
  694. if (i == 10) {
  695. printf ("%s: CAN1 initialize error, "
  696. "can not enter init mode!\n",
  697. __FUNCTION__);
  698. return 1;
  699. }
  700. }
  701. can1->canctl1 = 0x80;
  702. can1->canctl1 |= 0x40;
  703. can1->canbtr0 = 0x0F;
  704. can1->canbtr1 = 0x7F;
  705. can1->canidac &= ~(0x30);
  706. can1->canidar1 = 0x00;
  707. can1->canidar3 = 0x00;
  708. can1->canidar5 = 0x00;
  709. can1->canidar7 = 0x00;
  710. can1->canidmr0 = 0xFF;
  711. can1->canidmr1 = 0xFF;
  712. can1->canidmr2 = 0xFF;
  713. can1->canidmr3 = 0xFF;
  714. can1->canidmr4 = 0xFF;
  715. can1->canidmr5 = 0xFF;
  716. can1->canidmr6 = 0xFF;
  717. can1->canidmr7 = 0xFF;
  718. i = 0;
  719. can1->canctl0 &= ~(0x01); /* leave init mode */
  720. can1->canctl0 &= ~(0x02);
  721. /* wait until init and sleep mode left */
  722. while ((can1->canctl1 & 0x01) || (can1->canctl1 & 0x02)) {
  723. udelay(10);
  724. i++;
  725. if (i == 10) {
  726. printf ("%s: CAN1 initialize error, "
  727. "can not leave init/sleep mode!\n",
  728. __FUNCTION__);
  729. return 1;
  730. }
  731. }
  732. /* init CAN 2 */
  733. can2->canctl1 |= 0x80; /* CAN enable */
  734. udelay(100);
  735. i = 0;
  736. can2->canctl0 |= 0x02; /* sleep mode */
  737. /* wait until sleep mode reached */
  738. while (!(can2->canctl1 & 0x02)) {
  739. udelay(10);
  740. i++;
  741. if (i == 10) {
  742. printf ("%s: CAN2 initialize error, "
  743. "can not enter sleep mode!\n",
  744. __FUNCTION__);
  745. return 1;
  746. }
  747. }
  748. i = 0;
  749. can2->canctl0 = 0x01; /* enter init mode */
  750. /* wait until init mode reached */
  751. while (!(can2->canctl1 & 0x01)) {
  752. udelay(10);
  753. i++;
  754. if (i == 10) {
  755. printf ("%s: CAN2 initialize error, "
  756. "can not enter init mode!\n",
  757. __FUNCTION__);
  758. return 1;
  759. }
  760. }
  761. can2->canctl1 = 0x80;
  762. can2->canctl1 |= 0x40;
  763. can2->canbtr0 = 0x0F;
  764. can2->canbtr1 = 0x7F;
  765. can2->canidac &= ~(0x30);
  766. can2->canidar1 = 0x00;
  767. can2->canidar3 = 0x00;
  768. can2->canidar5 = 0x00;
  769. can2->canidar7 = 0x00;
  770. can2->canidmr0 = 0xFF;
  771. can2->canidmr1 = 0xFF;
  772. can2->canidmr2 = 0xFF;
  773. can2->canidmr3 = 0xFF;
  774. can2->canidmr4 = 0xFF;
  775. can2->canidmr5 = 0xFF;
  776. can2->canidmr6 = 0xFF;
  777. can2->canidmr7 = 0xFF;
  778. can2->canctl0 &= ~(0x01); /* leave init mode */
  779. can2->canctl0 &= ~(0x02);
  780. i = 0;
  781. /* wait until init mode left */
  782. while ((can2->canctl1 & 0x01) || (can2->canctl1 & 0x02)) {
  783. udelay(10);
  784. i++;
  785. if (i == 10) {
  786. printf ("%s: CAN2 initialize error, "
  787. "can not leave init/sleep mode!\n",
  788. __FUNCTION__);
  789. return 1;
  790. }
  791. }
  792. init_done = 1;
  793. }
  794. return 0;
  795. }
  796. /*
  797. * return 1 on CAN failure
  798. * return 0 if no failure
  799. */
  800. int do_can(char *argv[])
  801. {
  802. int i;
  803. struct mpc5xxx_mscan *can1 =
  804. (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0900);
  805. struct mpc5xxx_mscan *can2 =
  806. (struct mpc5xxx_mscan *)(CFG_MBAR + 0x0980);
  807. /* send a message on CAN1 */
  808. can1->cantbsel = 0x01;
  809. can1->cantxfg.idr[0] = 0x55;
  810. can1->cantxfg.idr[1] = 0x00;
  811. can1->cantxfg.idr[1] &= ~0x8;
  812. can1->cantxfg.idr[1] &= ~0x10;
  813. can1->cantxfg.dsr[0] = 0xCC;
  814. can1->cantxfg.dlr = 1;
  815. can1->cantxfg.tbpr = 0;
  816. can1->cantflg = 0x01;
  817. i = 0;
  818. while ((can1->cantflg & 0x01) == 0) {
  819. i++;
  820. if (i == 10) {
  821. printf ("%s: CAN1 send timeout, "
  822. "can not send message!\n",
  823. __FUNCTION__);
  824. return 1;
  825. }
  826. udelay(1000);
  827. }
  828. udelay(1000);
  829. i = 0;
  830. while (!(can2->canrflg & 0x01)) {
  831. i++;
  832. if (i == 10) {
  833. printf ("%s: CAN2 receive timeout, "
  834. "no message received!\n",
  835. __FUNCTION__);
  836. return 1;
  837. }
  838. udelay(1000);
  839. }
  840. if (can2->canrxfg.dsr[0] != 0xCC) {
  841. printf ("%s: CAN2 receive error, "
  842. "data mismatch!\n",
  843. __FUNCTION__);
  844. return 1;
  845. }
  846. /* send a message on CAN2 */
  847. can2->cantbsel = 0x01;
  848. can2->cantxfg.idr[0] = 0x55;
  849. can2->cantxfg.idr[1] = 0x00;
  850. can2->cantxfg.idr[1] &= ~0x8;
  851. can2->cantxfg.idr[1] &= ~0x10;
  852. can2->cantxfg.dsr[0] = 0xCC;
  853. can2->cantxfg.dlr = 1;
  854. can2->cantxfg.tbpr = 0;
  855. can2->cantflg = 0x01;
  856. i = 0;
  857. while ((can2->cantflg & 0x01) == 0) {
  858. i++;
  859. if (i == 10) {
  860. printf ("%s: CAN2 send error, "
  861. "can not send message!\n",
  862. __FUNCTION__);
  863. return 1;
  864. }
  865. udelay(1000);
  866. }
  867. udelay(1000);
  868. i = 0;
  869. while (!(can1->canrflg & 0x01)) {
  870. i++;
  871. if (i == 10) {
  872. printf ("%s: CAN1 receive timeout, "
  873. "no message received!\n",
  874. __FUNCTION__);
  875. return 1;
  876. }
  877. udelay(1000);
  878. }
  879. if (can1->canrxfg.dsr[0] != 0xCC) {
  880. printf ("%s: CAN1 receive error 0x%02x\n",
  881. __FUNCTION__, (can1->canrxfg.dsr[0]));
  882. return 1;
  883. }
  884. return 0;
  885. }
  886. /*
  887. * return 1 if rs232 port unknown
  888. * return 2 on txd/rxd failure (only rs232 2)
  889. * return 3 on rts/cts failure
  890. * return 0 if no failure
  891. */
  892. int do_rs232(char *argv[])
  893. {
  894. int error_status = 0;
  895. struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
  896. struct mpc5xxx_psc *psc1 = (struct mpc5xxx_psc *)MPC5XXX_PSC1;
  897. switch (simple_strtoul(argv[2], NULL, 10)) {
  898. case 1:
  899. /* check RTS <-> CTS loop */
  900. /* set rts to 0 */
  901. psc1->op1 |= 0x01;
  902. /* wait some time before requesting status */
  903. udelay(10);
  904. /* check status at cts */
  905. if ((psc1->ip & 0x01) != 0) {
  906. error_status = 3;
  907. printf ("%s: failure at rs232_1, cts status is %d "
  908. "(should be 0)\n",
  909. __FUNCTION__, (psc1->ip & 0x01));
  910. }
  911. /* set rts to 1 */
  912. psc1->op0 |= 0x01;
  913. /* wait some time before requesting status */
  914. udelay(10);
  915. /* check status at cts */
  916. if ((psc1->ip & 0x01) != 1) {
  917. error_status = 3;
  918. printf ("%s: failure at rs232_1, cts status is %d "
  919. "(should be 1)\n",
  920. __FUNCTION__, (psc1->ip & 0x01));
  921. }
  922. break;
  923. case 2:
  924. /* set PSC3_0, PSC3_2 as output and PSC3_1, PSC3_3 as input */
  925. gpio->simple_ddr &= ~(0x00000F00);
  926. gpio->simple_ddr |= 0x00000500;
  927. /* check TXD <-> RXD loop */
  928. /* set TXD to 1 */
  929. gpio->simple_dvo |= (1 << 8);
  930. /* wait some time before requesting status */
  931. udelay(10);
  932. if ((gpio->simple_ival & 0x00000200) != 0x00000200) {
  933. error_status = 2;
  934. printf ("%s: failure at rs232_2, rxd status is %d "
  935. "(should be 1)\n",
  936. __FUNCTION__,
  937. (gpio->simple_ival & 0x00000200) >> 9);
  938. }
  939. /* set TXD to 0 */
  940. gpio->simple_dvo &= ~(1 << 8);
  941. /* wait some time before requesting status */
  942. udelay(10);
  943. if ((gpio->simple_ival & 0x00000200) != 0x00000000) {
  944. error_status = 2;
  945. printf ("%s: failure at rs232_2, rxd status is %d "
  946. "(should be 0)\n",
  947. __FUNCTION__,
  948. (gpio->simple_ival & 0x00000200) >> 9);
  949. }
  950. /* check RTS <-> CTS loop */
  951. /* set RTS to 1 */
  952. gpio->simple_dvo |= (1 << 10);
  953. /* wait some time before requesting status */
  954. udelay(10);
  955. if ((gpio->simple_ival & 0x00000800) != 0x00000800) {
  956. error_status = 3;
  957. printf ("%s: failure at rs232_2, cts status is %d "
  958. "(should be 1)\n",
  959. __FUNCTION__,
  960. (gpio->simple_ival & 0x00000800) >> 11);
  961. }
  962. /* set RTS to 0 */
  963. gpio->simple_dvo &= ~(1 << 10);
  964. /* wait some time before requesting status */
  965. udelay(10);
  966. if ((gpio->simple_ival & 0x00000800) != 0x00000000) {
  967. error_status = 3;
  968. printf ("%s: failure at rs232_2, cts status is %d "
  969. "(should be 0)\n",
  970. __FUNCTION__,
  971. (gpio->simple_ival & 0x00000800) >> 11);
  972. }
  973. /* set PSC3_0, PSC3_1, PSC3_2 and PSC3_3 as output */
  974. gpio->simple_ddr &= ~(0x00000F00);
  975. gpio->simple_ddr |= 0x00000F00;
  976. break;
  977. default:
  978. printf ("%s: invalid rs232 number %s\n", __FUNCTION__, argv[2]);
  979. error_status = 1;
  980. break;
  981. }
  982. return error_status;
  983. }
  984. static void sm501_backlight (unsigned int state)
  985. {
  986. if (state == BL_ON) {
  987. *(vu_long *)(SM501_MMIO_BASE+SM501_PANEL_DISPLAY_CONTROL) |=
  988. (1 << 26) | (1 << 27);
  989. } else if (state == BL_OFF)
  990. *(vu_long *)(SM501_MMIO_BASE+SM501_PANEL_DISPLAY_CONTROL) &=
  991. ~((1 << 26) | (1 << 27));
  992. }
  993. int cmd_fkt(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  994. {
  995. int rcode;
  996. #ifdef CONFIG_STK52XX_REV100
  997. printf ("Revision 100 of STK52XX not supported!\n");
  998. return 1;
  999. #endif
  1000. led_init();
  1001. can_init();
  1002. switch (argc) {
  1003. case 0:
  1004. case 1:
  1005. break;
  1006. case 2:
  1007. if (strncmp (argv[1], "can", 3) == 0) {
  1008. rcode = do_can (argv);
  1009. if (rcode == 0)
  1010. printf ("OK\n");
  1011. else
  1012. printf ("Error\n");
  1013. return rcode;
  1014. }
  1015. break;
  1016. case 3:
  1017. if (strncmp (argv[1], "rs232", 3) == 0) {
  1018. rcode = do_rs232 (argv);
  1019. if (rcode == 0)
  1020. printf ("OK\n");
  1021. else
  1022. printf ("Error\n");
  1023. return rcode;
  1024. } else if (strncmp (argv[1], "backlight", 4) == 0) {
  1025. if (strncmp (argv[2], "on", 2) == 0) {
  1026. sm501_backlight (BL_ON);
  1027. return 0;
  1028. }
  1029. else if (strncmp (argv[2], "off", 3) == 0) {
  1030. sm501_backlight (BL_OFF);
  1031. return 0;
  1032. }
  1033. }
  1034. break;
  1035. case 4:
  1036. if (strcmp (argv[1], "led") == 0) {
  1037. return (do_led (argv));
  1038. }
  1039. break;
  1040. default:
  1041. break;
  1042. }
  1043. printf ("Usage:\nfkt cmd [arg1] [arg2] ...\n");
  1044. return 1;
  1045. }
  1046. U_BOOT_CMD(
  1047. sound , 5, 1, cmd_sound,
  1048. "sound - Sound sub-system\n",
  1049. "saw [duration] [freq] [channel]\n"
  1050. " - generate sawtooth for 'duration' ms with frequency 'freq'\n"
  1051. " on left \"l\" or right \"r\" channel\n"
  1052. "sound square [duration] [freq] [channel]\n"
  1053. " - generate squarewave for 'duration' ms with frequency 'freq'\n"
  1054. " on left \"l\" or right \"r\" channel\n"
  1055. "pcm1772 reg val\n"
  1056. );
  1057. U_BOOT_CMD(
  1058. wav , 3, 1, cmd_wav,
  1059. "wav - play wav file\n",
  1060. "[addr] [bytes]\n"
  1061. " - play wav file at address 'addr' with length 'bytes'\n"
  1062. );
  1063. U_BOOT_CMD(
  1064. beep , 2, 1, cmd_beep,
  1065. "beep - play short beep\n",
  1066. "[channel]\n"
  1067. " - play short beep on \"l\"eft or \"r\"ight channel\n"
  1068. );
  1069. U_BOOT_CMD(
  1070. fkt , 4, 1, cmd_fkt,
  1071. "fkt - Function test routines\n",
  1072. "led number on/off\n"
  1073. " - 'number's like printed on SKT52XX board\n"
  1074. "fkt can\n"
  1075. " - loopback plug for X83 required\n"
  1076. "fkt rs232 number\n"
  1077. " - loopback plug(s) for X2 required\n"
  1078. "fkt backlight on/off\n"
  1079. " - switch backlight on or off\n"
  1080. );
  1081. #endif /* CONFIG_STK52XX */
  1082. #endif /* CFG_CMD_BSP */