cmd_bc3450.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * (C) Copyright 2005
  3. * Stefan Strobl, GERSYS GmbH, stefan.strobl@gersys.de
  4. *
  5. * (C) Copyright 2005
  6. * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. /*
  29. * BC3450 specific commands
  30. */
  31. #if defined(CONFIG_CMD_BSP)
  32. /*
  33. * Definitions for DS1620 chip
  34. */
  35. #define THERM_START_CONVERT 0xee
  36. #define THERM_RESET 0xaf
  37. #define THERM_READ_CONFIG 0xac
  38. #define THERM_READ_TEMP 0xaa
  39. #define THERM_READ_TL 0xa2
  40. #define THERM_READ_TH 0xa1
  41. #define THERM_WRITE_CONFIG 0x0c
  42. #define THERM_WRITE_TL 0x02
  43. #define THERM_WRITE_TH 0x01
  44. #define CONFIG_SYS_CPU 2
  45. #define CONFIG_SYS_1SHOT 1
  46. #define CONFIG_SYS_STANDALONE 0
  47. struct therm {
  48. int hi;
  49. int lo;
  50. };
  51. /*
  52. * SM501 Register
  53. */
  54. #define SM501_GPIO_CTRL_LOW 0x00000008UL /* gpio pins 0..31 */
  55. #define SM501_GPIO_CTRL_HIGH 0x0000000CUL /* gpio pins 32..63 */
  56. #define SM501_POWER_MODE0_GATE 0x00000040UL
  57. #define SM501_POWER_MODE1_GATE 0x00000048UL
  58. #define POWER_MODE_GATE_GPIO_PWM_I2C 0x00000040UL
  59. #define SM501_GPIO_DATA_LOW 0x00010000UL
  60. #define SM501_GPIO_DATA_HIGH 0x00010004UL
  61. #define SM501_GPIO_DATA_DIR_LOW 0x00010008UL
  62. #define SM501_GPIO_DATA_DIR_HIGH 0x0001000CUL
  63. #define SM501_PANEL_DISPLAY_CONTROL 0x00080000UL
  64. #define SM501_CRT_DISPLAY_CONTROL 0x00080200UL
  65. /* SM501 CRT Display Control Bits */
  66. #define SM501_CDC_SEL (1 << 9)
  67. #define SM501_CDC_TE (1 << 8)
  68. #define SM501_CDC_E (1 << 2)
  69. /* SM501 Panel Display Control Bits */
  70. #define SM501_PDC_FPEN (1 << 27)
  71. #define SM501_PDC_BIAS (1 << 26)
  72. #define SM501_PDC_DATA (1 << 25)
  73. #define SM501_PDC_VDDEN (1 << 24)
  74. /* SM501 GPIO Data LOW Bits */
  75. #define SM501_GPIO24 0x01000000
  76. #define SM501_GPIO25 0x02000000
  77. #define SM501_GPIO26 0x04000000
  78. #define SM501_GPIO27 0x08000000
  79. #define SM501_GPIO28 0x10000000
  80. #define SM501_GPIO29 0x20000000
  81. #define SM501_GPIO30 0x40000000
  82. #define SM501_GPIO31 0x80000000
  83. /* SM501 GPIO Data HIGH Bits */
  84. #define SM501_GPIO46 0x00004000
  85. #define SM501_GPIO47 0x00008000
  86. #define SM501_GPIO48 0x00010000
  87. #define SM501_GPIO49 0x00020000
  88. #define SM501_GPIO50 0x00040000
  89. #define SM501_GPIO51 0x00080000
  90. /* BC3450 GPIOs @ SM501 Data LOW */
  91. #define DIP (SM501_GPIO24 | SM501_GPIO25 | SM501_GPIO26 | SM501_GPIO27)
  92. #define DS1620_DQ SM501_GPIO29 /* I/O */
  93. #define DS1620_CLK SM501_GPIO30 /* High active O/P */
  94. #define DS1620_RES SM501_GPIO31 /* Low active O/P */
  95. /* BC3450 GPIOs @ SM501 Data HIGH */
  96. #define BUZZER SM501_GPIO47 /* Low active O/P */
  97. #define DS1620_TLOW SM501_GPIO48 /* High active I/P */
  98. #define PWR_OFF SM501_GPIO49 /* Low active O/P */
  99. #define FP_DATA_TRI SM501_GPIO50 /* High active O/P */
  100. /*
  101. * Initialise GPIO on SM501
  102. *
  103. * This function may be called from several other functions.
  104. * Yet, the initialisation sequence is executed only the first
  105. * time the function is called.
  106. */
  107. int sm501_gpio_init (void)
  108. {
  109. static int init_done = 0;
  110. if (init_done) {
  111. debug("sm501_gpio_init: nothing to be done.\n");
  112. return 1;
  113. }
  114. /* enable SM501 GPIO control (in both power modes) */
  115. *(vu_long *) (SM501_MMIO_BASE + SM501_POWER_MODE0_GATE) |=
  116. POWER_MODE_GATE_GPIO_PWM_I2C;
  117. *(vu_long *) (SM501_MMIO_BASE + SM501_POWER_MODE1_GATE) |=
  118. POWER_MODE_GATE_GPIO_PWM_I2C;
  119. /* set up default O/Ps */
  120. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &=
  121. ~(DS1620_RES | DS1620_CLK);
  122. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |= DS1620_DQ;
  123. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) &=
  124. ~(FP_DATA_TRI);
  125. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) |=
  126. (BUZZER | PWR_OFF);
  127. /* configure directions for SM501 GPIO pins */
  128. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_CTRL_LOW) &= ~(0xFF << 24);
  129. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_CTRL_HIGH) &=
  130. ~(0x3F << 14);
  131. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) &=
  132. ~(DIP | DS1620_DQ);
  133. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) |=
  134. (DS1620_RES | DS1620_CLK);
  135. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_HIGH) &=
  136. ~DS1620_TLOW;
  137. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_HIGH) |=
  138. (PWR_OFF | BUZZER | FP_DATA_TRI);
  139. init_done = 1;
  140. debug("sm501_gpio_init: done.\n");
  141. return 0;
  142. }
  143. /*
  144. * dip - read Config Inputs
  145. *
  146. * read and prints the dip switch
  147. * and/or external config inputs (4bits) 0...0x0F
  148. */
  149. int cmd_dip (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  150. {
  151. vu_long rc = 0;
  152. sm501_gpio_init ();
  153. /* read dip switch */
  154. rc = *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW);
  155. rc = ~rc;
  156. rc &= DIP;
  157. rc = (int) (rc >> 24);
  158. /* plausibility check */
  159. if (rc > 0x0F)
  160. return -1;
  161. printf ("0x%lx\n", rc);
  162. return 0;
  163. }
  164. U_BOOT_CMD (dip, 1, 1, cmd_dip,
  165. "read dip switch and config inputs",
  166. "\n"
  167. " - prints the state of the dip switch and/or\n"
  168. " external configuration inputs as hex value.\n"
  169. " - \"Config 1\" is the LSB");
  170. /*
  171. * buz - turns Buzzer on/off
  172. */
  173. #ifdef CONFIG_BC3450_BUZZER
  174. static int cmd_buz (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  175. {
  176. if (argc != 2) {
  177. printf ("Usage:\nspecify one argument: \"on\" or \"off\"\n");
  178. return 1;
  179. }
  180. sm501_gpio_init ();
  181. if (strncmp (argv[1], "on", 2) == 0) {
  182. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) &=
  183. ~(BUZZER);
  184. return 0;
  185. } else if (strncmp (argv[1], "off", 3) == 0) {
  186. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) |=
  187. BUZZER;
  188. return 0;
  189. }
  190. printf ("Usage:\nspecify one argument: \"on\" or \"off\"\n");
  191. return 1;
  192. }
  193. U_BOOT_CMD (buz, 2, 1, cmd_buz,
  194. "turns buzzer on/off",
  195. "\n" "buz <on/off>\n" " - turns the buzzer on or off");
  196. #endif /* CONFIG_BC3450_BUZZER */
  197. /*
  198. * fp - front panel commands
  199. */
  200. static int cmd_fp (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  201. {
  202. sm501_gpio_init ();
  203. if (strncmp (argv[1], "on", 2) == 0) {
  204. /* turn on VDD first */
  205. *(vu_long *) (SM501_MMIO_BASE +
  206. SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_VDDEN;
  207. udelay (1000);
  208. /* then put data on */
  209. *(vu_long *) (SM501_MMIO_BASE +
  210. SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_DATA;
  211. /* wait some time and enable backlight */
  212. udelay (1000);
  213. *(vu_long *) (SM501_MMIO_BASE +
  214. SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_BIAS;
  215. udelay (1000);
  216. *(vu_long *) (SM501_MMIO_BASE +
  217. SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_FPEN;
  218. return 0;
  219. } else if (strncmp (argv[1], "off", 3) == 0) {
  220. /* turn off the backlight first */
  221. *(vu_long *) (SM501_MMIO_BASE +
  222. SM501_PANEL_DISPLAY_CONTROL) &= ~SM501_PDC_FPEN;
  223. udelay (1000);
  224. *(vu_long *) (SM501_MMIO_BASE +
  225. SM501_PANEL_DISPLAY_CONTROL) &= ~SM501_PDC_BIAS;
  226. udelay (200000);
  227. /* wait some time, then remove data */
  228. *(vu_long *) (SM501_MMIO_BASE +
  229. SM501_PANEL_DISPLAY_CONTROL) &= ~SM501_PDC_DATA;
  230. udelay (1000);
  231. /* and remove VDD last */
  232. *(vu_long *) (SM501_MMIO_BASE +
  233. SM501_PANEL_DISPLAY_CONTROL) &=
  234. ~SM501_PDC_VDDEN;
  235. return 0;
  236. } else if (strncmp (argv[1], "bl", 2) == 0) {
  237. /* turn on/off backlight only */
  238. if (strncmp (argv[2], "on", 2) == 0) {
  239. *(vu_long *) (SM501_MMIO_BASE +
  240. SM501_PANEL_DISPLAY_CONTROL) |=
  241. SM501_PDC_BIAS;
  242. udelay (1000);
  243. *(vu_long *) (SM501_MMIO_BASE +
  244. SM501_PANEL_DISPLAY_CONTROL) |=
  245. SM501_PDC_FPEN;
  246. return 0;
  247. } else if (strncmp (argv[2], "off", 3) == 0) {
  248. *(vu_long *) (SM501_MMIO_BASE +
  249. SM501_PANEL_DISPLAY_CONTROL) &=
  250. ~SM501_PDC_FPEN;
  251. udelay (1000);
  252. *(vu_long *) (SM501_MMIO_BASE +
  253. SM501_PANEL_DISPLAY_CONTROL) &=
  254. ~SM501_PDC_BIAS;
  255. return 0;
  256. }
  257. }
  258. #ifdef CONFIG_BC3450_CRT
  259. else if (strncmp (argv[1], "crt", 3) == 0) {
  260. /* enables/disables the crt output (debug only) */
  261. if (strncmp (argv[2], "on", 2) == 0) {
  262. *(vu_long *) (SM501_MMIO_BASE +
  263. SM501_CRT_DISPLAY_CONTROL) |=
  264. (SM501_CDC_TE | SM501_CDC_E);
  265. *(vu_long *) (SM501_MMIO_BASE +
  266. SM501_CRT_DISPLAY_CONTROL) &=
  267. ~SM501_CDC_SEL;
  268. return 0;
  269. } else if (strncmp (argv[2], "off", 3) == 0) {
  270. *(vu_long *) (SM501_MMIO_BASE +
  271. SM501_CRT_DISPLAY_CONTROL) &=
  272. ~(SM501_CDC_TE | SM501_CDC_E);
  273. *(vu_long *) (SM501_MMIO_BASE +
  274. SM501_CRT_DISPLAY_CONTROL) |=
  275. SM501_CDC_SEL;
  276. return 0;
  277. }
  278. }
  279. #endif /* CONFIG_BC3450_CRT */
  280. printf ("Usage:%s\n", cmdtp->help);
  281. return 1;
  282. }
  283. U_BOOT_CMD (fp, 3, 1, cmd_fp,
  284. "front panes access functions",
  285. "\n"
  286. "fp bl <on/off>\n"
  287. " - turns the CCFL backlight of the display on/off\n"
  288. "fp <on/off>\n" " - turns the whole display on/off"
  289. #ifdef CONFIG_BC3450_CRT
  290. "\n"
  291. "fp crt <on/off>\n"
  292. " - enables/disables the crt output (debug only)"
  293. #endif /* CONFIG_BC3450_CRT */
  294. );
  295. /*
  296. * temp - DS1620 thermometer
  297. */
  298. /* GERSYS BC3450 specific functions */
  299. static inline void bc_ds1620_set_clk (int clk)
  300. {
  301. if (clk)
  302. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |=
  303. DS1620_CLK;
  304. else
  305. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &=
  306. ~DS1620_CLK;
  307. }
  308. static inline void bc_ds1620_set_data (int dat)
  309. {
  310. if (dat)
  311. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |=
  312. DS1620_DQ;
  313. else
  314. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &=
  315. ~DS1620_DQ;
  316. }
  317. static inline int bc_ds1620_get_data (void)
  318. {
  319. vu_long rc;
  320. rc = *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW);
  321. rc &= DS1620_DQ;
  322. if (rc != 0)
  323. rc = 1;
  324. return (int) rc;
  325. }
  326. static inline void bc_ds1620_set_data_dir (int dir)
  327. {
  328. if (dir) /* in */
  329. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) &= ~DS1620_DQ;
  330. else /* out */
  331. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) |= DS1620_DQ;
  332. }
  333. static inline void bc_ds1620_set_reset (int res)
  334. {
  335. if (res)
  336. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |= DS1620_RES;
  337. else
  338. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &= ~DS1620_RES;
  339. }
  340. /* hardware independent functions */
  341. static void ds1620_send_bits (int nr, int value)
  342. {
  343. int i;
  344. for (i = 0; i < nr; i++) {
  345. bc_ds1620_set_data (value & 1);
  346. bc_ds1620_set_clk (0);
  347. udelay (1);
  348. bc_ds1620_set_clk (1);
  349. udelay (1);
  350. value >>= 1;
  351. }
  352. }
  353. static unsigned int ds1620_recv_bits (int nr)
  354. {
  355. unsigned int value = 0, mask = 1;
  356. int i;
  357. bc_ds1620_set_data (0);
  358. for (i = 0; i < nr; i++) {
  359. bc_ds1620_set_clk (0);
  360. udelay (1);
  361. if (bc_ds1620_get_data ())
  362. value |= mask;
  363. mask <<= 1;
  364. bc_ds1620_set_clk (1);
  365. udelay (1);
  366. }
  367. return value;
  368. }
  369. static void ds1620_out (int cmd, int bits, int value)
  370. {
  371. bc_ds1620_set_clk (1);
  372. bc_ds1620_set_data_dir (0);
  373. bc_ds1620_set_reset (0);
  374. udelay (1);
  375. bc_ds1620_set_reset (1);
  376. udelay (1);
  377. ds1620_send_bits (8, cmd);
  378. if (bits)
  379. ds1620_send_bits (bits, value);
  380. udelay (1);
  381. /* go stand alone */
  382. bc_ds1620_set_data_dir (1);
  383. bc_ds1620_set_reset (0);
  384. bc_ds1620_set_clk (0);
  385. udelay (10000);
  386. }
  387. static unsigned int ds1620_in (int cmd, int bits)
  388. {
  389. unsigned int value;
  390. bc_ds1620_set_clk (1);
  391. bc_ds1620_set_data_dir (0);
  392. bc_ds1620_set_reset (0);
  393. udelay (1);
  394. bc_ds1620_set_reset (1);
  395. udelay (1);
  396. ds1620_send_bits (8, cmd);
  397. bc_ds1620_set_data_dir (1);
  398. value = ds1620_recv_bits (bits);
  399. /* go stand alone */
  400. bc_ds1620_set_data_dir (1);
  401. bc_ds1620_set_reset (0);
  402. bc_ds1620_set_clk (0);
  403. return value;
  404. }
  405. static int cvt_9_to_int (unsigned int val)
  406. {
  407. if (val & 0x100)
  408. val |= 0xfffffe00;
  409. return val;
  410. }
  411. /* set thermostate thresholds */
  412. static void ds1620_write_state (struct therm *therm)
  413. {
  414. ds1620_out (THERM_WRITE_TL, 9, therm->lo);
  415. ds1620_out (THERM_WRITE_TH, 9, therm->hi);
  416. ds1620_out (THERM_START_CONVERT, 0, 0);
  417. }
  418. static int cmd_temp (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  419. {
  420. int i;
  421. struct therm therm;
  422. sm501_gpio_init ();
  423. /* print temperature */
  424. if (argc == 1) {
  425. i = cvt_9_to_int (ds1620_in (THERM_READ_TEMP, 9));
  426. printf ("%d.%d C\n", i >> 1, i & 1 ? 5 : 0);
  427. return 0;
  428. }
  429. /* set to default operation */
  430. if (strncmp (argv[1], "set", 3) == 0) {
  431. if (strncmp (argv[2], "default", 3) == 0) {
  432. therm.hi = +88;
  433. therm.lo = -20;
  434. therm.hi <<= 1;
  435. therm.lo <<= 1;
  436. ds1620_write_state (&therm);
  437. ds1620_out (THERM_WRITE_CONFIG, 8, CONFIG_SYS_STANDALONE);
  438. return 0;
  439. }
  440. }
  441. printf ("Usage:%s\n", cmdtp->help);
  442. return 1;
  443. }
  444. U_BOOT_CMD (temp, 3, 1, cmd_temp,
  445. "print current temperature",
  446. "\n" "temp\n" " - print current temperature");
  447. #ifdef CONFIG_BC3450_CAN
  448. /*
  449. * Initialise CAN interface
  450. *
  451. * return 1 on CAN initialization failure
  452. * return 0 if no failure
  453. */
  454. int can_init (void)
  455. {
  456. static int init_done = 0;
  457. int i;
  458. struct mpc5xxx_mscan *can1 =
  459. (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0900);
  460. struct mpc5xxx_mscan *can2 =
  461. (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0980);
  462. /* GPIO configuration of the CAN pins is done in BC3450.h */
  463. if (!init_done) {
  464. /* init CAN 1 */
  465. can1->canctl1 |= 0x80; /* CAN enable */
  466. udelay (100);
  467. i = 0;
  468. can1->canctl0 |= 0x02; /* sleep mode */
  469. /* wait until sleep mode reached */
  470. while (!(can1->canctl1 & 0x02)) {
  471. udelay (10);
  472. i++;
  473. if (i == 10) {
  474. printf ("%s: CAN1 initialize error, "
  475. "can not enter sleep mode!\n",
  476. __FUNCTION__);
  477. return 1;
  478. }
  479. }
  480. i = 0;
  481. can1->canctl0 = 0x01; /* enter init mode */
  482. /* wait until init mode reached */
  483. while (!(can1->canctl1 & 0x01)) {
  484. udelay (10);
  485. i++;
  486. if (i == 10) {
  487. printf ("%s: CAN1 initialize error, "
  488. "can not enter init mode!\n",
  489. __FUNCTION__);
  490. return 1;
  491. }
  492. }
  493. can1->canctl1 = 0x80;
  494. can1->canctl1 |= 0x40;
  495. can1->canbtr0 = 0x0F;
  496. can1->canbtr1 = 0x7F;
  497. can1->canidac &= ~(0x30);
  498. can1->canidar1 = 0x00;
  499. can1->canidar3 = 0x00;
  500. can1->canidar5 = 0x00;
  501. can1->canidar7 = 0x00;
  502. can1->canidmr0 = 0xFF;
  503. can1->canidmr1 = 0xFF;
  504. can1->canidmr2 = 0xFF;
  505. can1->canidmr3 = 0xFF;
  506. can1->canidmr4 = 0xFF;
  507. can1->canidmr5 = 0xFF;
  508. can1->canidmr6 = 0xFF;
  509. can1->canidmr7 = 0xFF;
  510. i = 0;
  511. can1->canctl0 &= ~(0x01); /* leave init mode */
  512. can1->canctl0 &= ~(0x02);
  513. /* wait until init and sleep mode left */
  514. while ((can1->canctl1 & 0x01) || (can1->canctl1 & 0x02)) {
  515. udelay (10);
  516. i++;
  517. if (i == 10) {
  518. printf ("%s: CAN1 initialize error, "
  519. "can not leave init/sleep mode!\n",
  520. __FUNCTION__);
  521. return 1;
  522. }
  523. }
  524. /* init CAN 2 */
  525. can2->canctl1 |= 0x80; /* CAN enable */
  526. udelay (100);
  527. i = 0;
  528. can2->canctl0 |= 0x02; /* sleep mode */
  529. /* wait until sleep mode reached */
  530. while (!(can2->canctl1 & 0x02)) {
  531. udelay (10);
  532. i++;
  533. if (i == 10) {
  534. printf ("%s: CAN2 initialize error, "
  535. "can not enter sleep mode!\n",
  536. __FUNCTION__);
  537. return 1;
  538. }
  539. }
  540. i = 0;
  541. can2->canctl0 = 0x01; /* enter init mode */
  542. /* wait until init mode reached */
  543. while (!(can2->canctl1 & 0x01)) {
  544. udelay (10);
  545. i++;
  546. if (i == 10) {
  547. printf ("%s: CAN2 initialize error, "
  548. "can not enter init mode!\n",
  549. __FUNCTION__);
  550. return 1;
  551. }
  552. }
  553. can2->canctl1 = 0x80;
  554. can2->canctl1 |= 0x40;
  555. can2->canbtr0 = 0x0F;
  556. can2->canbtr1 = 0x7F;
  557. can2->canidac &= ~(0x30);
  558. can2->canidar1 = 0x00;
  559. can2->canidar3 = 0x00;
  560. can2->canidar5 = 0x00;
  561. can2->canidar7 = 0x00;
  562. can2->canidmr0 = 0xFF;
  563. can2->canidmr1 = 0xFF;
  564. can2->canidmr2 = 0xFF;
  565. can2->canidmr3 = 0xFF;
  566. can2->canidmr4 = 0xFF;
  567. can2->canidmr5 = 0xFF;
  568. can2->canidmr6 = 0xFF;
  569. can2->canidmr7 = 0xFF;
  570. can2->canctl0 &= ~(0x01); /* leave init mode */
  571. can2->canctl0 &= ~(0x02);
  572. i = 0;
  573. /* wait until init mode left */
  574. while ((can2->canctl1 & 0x01) || (can2->canctl1 & 0x02)) {
  575. udelay (10);
  576. i++;
  577. if (i == 10) {
  578. printf ("%s: CAN2 initialize error, "
  579. "can not leave init/sleep mode!\n",
  580. __FUNCTION__);
  581. return 1;
  582. }
  583. }
  584. init_done = 1;
  585. }
  586. return 0;
  587. }
  588. /*
  589. * Do CAN test
  590. * by sending message between CAN1 and CAN2
  591. *
  592. * return 1 on CAN failure
  593. * return 0 if no failure
  594. */
  595. int do_can (char * const argv[])
  596. {
  597. int i;
  598. struct mpc5xxx_mscan *can1 =
  599. (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0900);
  600. struct mpc5xxx_mscan *can2 =
  601. (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0980);
  602. /* send a message on CAN1 */
  603. can1->cantbsel = 0x01;
  604. can1->cantxfg.idr[0] = 0x55;
  605. can1->cantxfg.idr[1] = 0x00;
  606. can1->cantxfg.idr[1] &= ~0x8;
  607. can1->cantxfg.idr[1] &= ~0x10;
  608. can1->cantxfg.dsr[0] = 0xCC;
  609. can1->cantxfg.dlr = 1;
  610. can1->cantxfg.tbpr = 0;
  611. can1->cantflg = 0x01;
  612. i = 0;
  613. while ((can1->cantflg & 0x01) == 0) {
  614. i++;
  615. if (i == 10) {
  616. printf ("%s: CAN1 send timeout, "
  617. "can not send message!\n", __FUNCTION__);
  618. return 1;
  619. }
  620. udelay (1000);
  621. }
  622. udelay (1000);
  623. i = 0;
  624. while (!(can2->canrflg & 0x01)) {
  625. i++;
  626. if (i == 10) {
  627. printf ("%s: CAN2 receive timeout, "
  628. "no message received!\n", __FUNCTION__);
  629. return 1;
  630. }
  631. udelay (1000);
  632. }
  633. if (can2->canrxfg.dsr[0] != 0xCC) {
  634. printf ("%s: CAN2 receive error, "
  635. "data mismatch!\n", __FUNCTION__);
  636. return 1;
  637. }
  638. /* send a message on CAN2 */
  639. can2->cantbsel = 0x01;
  640. can2->cantxfg.idr[0] = 0x55;
  641. can2->cantxfg.idr[1] = 0x00;
  642. can2->cantxfg.idr[1] &= ~0x8;
  643. can2->cantxfg.idr[1] &= ~0x10;
  644. can2->cantxfg.dsr[0] = 0xCC;
  645. can2->cantxfg.dlr = 1;
  646. can2->cantxfg.tbpr = 0;
  647. can2->cantflg = 0x01;
  648. i = 0;
  649. while ((can2->cantflg & 0x01) == 0) {
  650. i++;
  651. if (i == 10) {
  652. printf ("%s: CAN2 send error, "
  653. "can not send message!\n", __FUNCTION__);
  654. return 1;
  655. }
  656. udelay (1000);
  657. }
  658. udelay (1000);
  659. i = 0;
  660. while (!(can1->canrflg & 0x01)) {
  661. i++;
  662. if (i == 10) {
  663. printf ("%s: CAN1 receive timeout, "
  664. "no message received!\n", __FUNCTION__);
  665. return 1;
  666. }
  667. udelay (1000);
  668. }
  669. if (can1->canrxfg.dsr[0] != 0xCC) {
  670. printf ("%s: CAN1 receive error 0x%02x\n",
  671. __FUNCTION__, (can1->canrxfg.dsr[0]));
  672. return 1;
  673. }
  674. return 0;
  675. }
  676. #endif /* CONFIG_BC3450_CAN */
  677. /*
  678. * test - BC3450 HW test routines
  679. */
  680. int cmd_test (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  681. {
  682. #ifdef CONFIG_BC3450_CAN
  683. int rcode;
  684. can_init ();
  685. #endif /* CONFIG_BC3450_CAN */
  686. sm501_gpio_init ();
  687. if (argc != 2) {
  688. printf ("Usage:%s\n", cmdtp->help);
  689. return 1;
  690. }
  691. if (strncmp (argv[1], "unit-off", 8) == 0) {
  692. printf ("waiting 2 seconds...\n");
  693. udelay (2000000);
  694. *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) &=
  695. ~PWR_OFF;
  696. return 0;
  697. }
  698. #ifdef CONFIG_BC3450_CAN
  699. else if (strncmp (argv[1], "can", 2) == 0) {
  700. rcode = do_can (argv);
  701. if (simple_strtoul (argv[2], NULL, 10) == 2) {
  702. if (rcode == 0)
  703. printf ("OK\n");
  704. else
  705. printf ("Error\n");
  706. }
  707. return rcode;
  708. }
  709. #endif /* CONFIG_BC3450_CAN */
  710. printf ("Usage:%s\n", cmdtp->help);
  711. return 1;
  712. }
  713. U_BOOT_CMD (test, 2, 1, cmd_test, "unit test routines", "\n"
  714. #ifdef CONFIG_BC3450_CAN
  715. "test can\n"
  716. " - connect CAN1 (X8) with CAN2 (X9) for this test\n"
  717. #endif /* CONFIG_BC3450_CAN */
  718. "test unit-off\n"
  719. " - turns off the BC3450 unit\n"
  720. " WARNING: Unsaved environment variables will be lost!"
  721. );
  722. #endif