cmd_stk52xx.c 29 KB

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