cmd_bc3450.c 19 KB

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