trab_fkt.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /*
  2. * (C) Copyright 2003
  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. #define DEBUG
  24. #include <common.h>
  25. #include <exports.h>
  26. #include <timestamp.h>
  27. #include <asm/arch/s3c24x0_cpu.h>
  28. #include "tsc2000.h"
  29. #include "rs485.h"
  30. /*
  31. * define, to wait for the touch to be pressed, before reading coordinates in
  32. * command do_touch. If not defined, an error message is printed, when the
  33. * command do_touch is invoked and the touch is not pressed within an specific
  34. * interval.
  35. */
  36. #undef CONFIG_TOUCH_WAIT_PRESSED
  37. /* max time to wait for touch is pressed */
  38. #ifndef CONFIG_TOUCH_WAIT_PRESSED
  39. #define TOUCH_TIMEOUT 5
  40. #endif /* !CONFIG_TOUCH_WAIT_PRESSED */
  41. /* assignment of CPU internal ADC channels with TRAB hardware */
  42. #define VCC5V 2
  43. #define VCC12V 3
  44. /* CPLD-Register for controlling TRAB hardware functions */
  45. #define CPLD_BUTTONS ((volatile unsigned long *)0x04020000)
  46. #define CPLD_FILL_LEVEL ((volatile unsigned long *)0x04008000)
  47. #define CPLD_ROTARY_SWITCH ((volatile unsigned long *)0x04018000)
  48. #define CPLD_RS485_RE ((volatile unsigned long *)0x04028000)
  49. /* timer configuration bits for buzzer and PWM */
  50. #define START2 (1 << 12)
  51. #define UPDATE2 (1 << 13)
  52. #define INVERT2 (1 << 14)
  53. #define RELOAD2 (1 << 15)
  54. #define START3 (1 << 16)
  55. #define UPDATE3 (1 << 17)
  56. #define INVERT3 (1 << 18)
  57. #define RELOAD3 (1 << 19)
  58. #define PCLK 66000000
  59. #define BUZZER_FREQ 1000 /* frequency in Hz */
  60. #define PWM_FREQ 500
  61. /* definitions of I2C EEPROM device address */
  62. #define I2C_EEPROM_DEV_ADDR 0x54
  63. /* definition for touch panel calibration points */
  64. #define CALIB_TL 0 /* calibration point in (T)op (L)eft corner */
  65. #define CALIB_DR 1 /* calibration point in (D)own (R)ight corner */
  66. /* EEPROM address map */
  67. #define SERIAL_NUMBER 8
  68. #define TOUCH_X0 52
  69. #define TOUCH_Y0 54
  70. #define TOUCH_X1 56
  71. #define TOUCH_Y1 58
  72. #define CRC16 60
  73. /* EEPROM stuff */
  74. #define EEPROM_MAX_CRC_BUF 64
  75. /* RS485 stuff */
  76. #define RS485_MAX_RECEIVE_BUF_LEN 100
  77. /* Bit definitions for ADCCON */
  78. #define ADC_ENABLE_START 0x1
  79. #define ADC_READ_START 0x2
  80. #define ADC_STDBM 0x4
  81. #define ADC_INP_AIN0 (0x0 << 3)
  82. #define ADC_INP_AIN1 (0x1 << 3)
  83. #define ADC_INP_AIN2 (0x2 << 3)
  84. #define ADC_INP_AIN3 (0x3 << 3)
  85. #define ADC_INP_AIN4 (0x4 << 3)
  86. #define ADC_INP_AIN5 (0x5 << 3)
  87. #define ADC_INP_AIN6 (0x6 << 3)
  88. #define ADC_INP_AIN7 (0x7 << 3)
  89. #define ADC_PRSCEN 0x4000
  90. #define ADC_ECFLG 0x8000
  91. /* function test functions */
  92. int do_dip (void);
  93. int do_info (void);
  94. int do_vcc5v (void);
  95. int do_vcc12v (void);
  96. int do_buttons (void);
  97. int do_fill_level (void);
  98. int do_rotary_switch (void);
  99. int do_pressure (void);
  100. int do_v_bat (void);
  101. int do_vfd_id (void);
  102. int do_buzzer (char **);
  103. int do_led (char **);
  104. int do_full_bridge (char **);
  105. int do_dac (char **);
  106. int do_motor_contact (void);
  107. int do_motor (char **);
  108. int do_pwm (char **);
  109. int do_thermo (char **);
  110. int do_touch (char **);
  111. int do_rs485 (char **);
  112. int do_serial_number (char **);
  113. int do_crc16 (void);
  114. int do_power_switch (void);
  115. int do_gain (char **);
  116. int do_eeprom (char **);
  117. /* helper functions */
  118. static void adc_init (void);
  119. static int adc_read (unsigned int channel);
  120. static void print_identifier (void);
  121. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  122. static void touch_wait_pressed (void);
  123. #else
  124. static int touch_check_pressed (void);
  125. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  126. static void touch_read_x_y (int *x, int *y);
  127. static int touch_write_clibration_values (int calib_point, int x, int y);
  128. static int rs485_send_line (const char *data);
  129. static int rs485_receive_chars (char *data, int timeout);
  130. static unsigned short updcrc(unsigned short icrc, unsigned char *icp,
  131. unsigned int icnt);
  132. #if defined(CONFIG_CMD_I2C)
  133. static int trab_eeprom_read (char **argv);
  134. static int trab_eeprom_write (char **argv);
  135. int i2c_write_multiple (uchar chip, uint addr, int alen, uchar *buffer,
  136. int len);
  137. int i2c_read_multiple ( uchar chip, uint addr, int alen, uchar *buffer,
  138. int len);
  139. #endif
  140. /*
  141. * TRAB board specific commands. Especially commands for burn-in and function
  142. * test.
  143. */
  144. int trab_fkt (int argc, char *argv[])
  145. {
  146. int i;
  147. app_startup(argv);
  148. if (get_version () != XF_VERSION) {
  149. printf ("Wrong XF_VERSION. Please re-compile with actual "
  150. "u-boot sources\n");
  151. printf ("Example expects ABI version %d\n", XF_VERSION);
  152. printf ("Actual U-Boot ABI version %d\n", (int)get_version());
  153. return 1;
  154. }
  155. debug ("argc = %d\n", argc);
  156. for (i=0; i<=argc; ++i) {
  157. debug ("argv[%d] = \"%s\"\n", i, argv[i] ? argv[i] : "<NULL>");
  158. }
  159. adc_init ();
  160. switch (argc) {
  161. case 0:
  162. case 1:
  163. break;
  164. case 2:
  165. if (strcmp (argv[1], "info") == 0) {
  166. return (do_info ());
  167. }
  168. if (strcmp (argv[1], "dip") == 0) {
  169. return (do_dip ());
  170. }
  171. if (strcmp (argv[1], "vcc5v") == 0) {
  172. return (do_vcc5v ());
  173. }
  174. if (strcmp (argv[1], "vcc12v") == 0) {
  175. return (do_vcc12v ());
  176. }
  177. if (strcmp (argv[1], "buttons") == 0) {
  178. return (do_buttons ());
  179. }
  180. if (strcmp (argv[1], "fill_level") == 0) {
  181. return (do_fill_level ());
  182. }
  183. if (strcmp (argv[1], "rotary_switch") == 0) {
  184. return (do_rotary_switch ());
  185. }
  186. if (strcmp (argv[1], "pressure") == 0) {
  187. return (do_pressure ());
  188. }
  189. if (strcmp (argv[1], "v_bat") == 0) {
  190. return (do_v_bat ());
  191. }
  192. if (strcmp (argv[1], "vfd_id") == 0) {
  193. return (do_vfd_id ());
  194. }
  195. if (strcmp (argv[1], "motor_contact") == 0) {
  196. return (do_motor_contact ());
  197. }
  198. if (strcmp (argv[1], "crc16") == 0) {
  199. return (do_crc16 ());
  200. }
  201. if (strcmp (argv[1], "power_switch") == 0) {
  202. return (do_power_switch ());
  203. }
  204. break;
  205. case 3:
  206. if (strcmp (argv[1], "full_bridge") == 0) {
  207. return (do_full_bridge (argv));
  208. }
  209. if (strcmp (argv[1], "dac") == 0) {
  210. return (do_dac (argv));
  211. }
  212. if (strcmp (argv[1], "motor") == 0) {
  213. return (do_motor (argv));
  214. }
  215. if (strcmp (argv[1], "pwm") == 0) {
  216. return (do_pwm (argv));
  217. }
  218. if (strcmp (argv[1], "thermo") == 0) {
  219. return (do_thermo (argv));
  220. }
  221. if (strcmp (argv[1], "touch") == 0) {
  222. return (do_touch (argv));
  223. }
  224. if (strcmp (argv[1], "serial_number") == 0) {
  225. return (do_serial_number (argv));
  226. }
  227. if (strcmp (argv[1], "buzzer") == 0) {
  228. return (do_buzzer (argv));
  229. }
  230. if (strcmp (argv[1], "gain") == 0) {
  231. return (do_gain (argv));
  232. }
  233. break;
  234. case 4:
  235. if (strcmp (argv[1], "led") == 0) {
  236. return (do_led (argv));
  237. }
  238. if (strcmp (argv[1], "rs485") == 0) {
  239. return (do_rs485 (argv));
  240. }
  241. if (strcmp (argv[1], "serial_number") == 0) {
  242. return (do_serial_number (argv));
  243. }
  244. break;
  245. case 5:
  246. if (strcmp (argv[1], "eeprom") == 0) {
  247. return (do_eeprom (argv));
  248. }
  249. break;
  250. case 6:
  251. if (strcmp (argv[1], "eeprom") == 0) {
  252. return (do_eeprom (argv));
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. printf ("Usage:\n<command> <parameter1> <parameter2> ...\n");
  259. return 1;
  260. }
  261. void hang (void)
  262. {
  263. puts ("### ERROR ### Please RESET the board ###\n");
  264. for (;;);
  265. }
  266. int do_info (void)
  267. {
  268. printf ("Stand-alone application for TRAB board function test\n");
  269. printf ("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME);
  270. return 0;
  271. }
  272. int do_dip (void)
  273. {
  274. unsigned int result = 0;
  275. int adc_val;
  276. int i;
  277. /***********************************************************
  278. DIP switch connection (according to wa4-cpu.sp.301.pdf, page 3):
  279. SW1 - AIN4
  280. SW2 - AIN5
  281. SW3 - AIN6
  282. SW4 - AIN7
  283. "On" DIP switch position short-circuits the voltage from
  284. the input channel (i.e. '0' conversion result means "on").
  285. *************************************************************/
  286. for (i = 7; i > 3; i--) {
  287. if ((adc_val = adc_read (i)) == -1) {
  288. printf ("Channel %d could not be read\n", i);
  289. return 1;
  290. }
  291. /*
  292. * Input voltage (switch open) is 1.8 V.
  293. * (Vin_High/VRef)*adc_res = (1,8V/2,5V)*1023) = 736
  294. * Set trigger at halve that value.
  295. */
  296. if (adc_val < 368)
  297. result |= (1 << (i-4));
  298. }
  299. /* print result to console */
  300. print_identifier ();
  301. for (i = 0; i < 4; i++) {
  302. if ((result & (1 << i)) == 0)
  303. printf("0");
  304. else
  305. printf("1");
  306. }
  307. printf("\n");
  308. return 0;
  309. }
  310. int do_vcc5v (void)
  311. {
  312. int result;
  313. /* VCC5V is connected to channel 2 */
  314. if ((result = adc_read (VCC5V)) == -1) {
  315. printf ("VCC5V could not be read\n");
  316. return 1;
  317. }
  318. /*
  319. * Calculate voltage value. Split in two parts because there is no
  320. * floating point support. VCC5V is connected over an resistor divider:
  321. * VCC5V=ADCval*2,5V/1023*(10K+30K)/10K.
  322. */
  323. print_identifier ();
  324. printf ("%d", (result & 0x3FF)* 10 / 1023);
  325. printf (".%d", ((result & 0x3FF)* 10 % 1023)* 10 / 1023);
  326. printf ("%d V\n", (((result & 0x3FF) * 10 % 1023 ) * 10 % 1023)
  327. * 10 / 1024);
  328. return 0;
  329. }
  330. int do_vcc12v (void)
  331. {
  332. int result;
  333. if ((result = adc_read (VCC12V)) == -1) {
  334. printf ("VCC12V could not be read\n");
  335. return 1;
  336. }
  337. /*
  338. * Calculate voltage value. Split in two parts because there is no
  339. * floating point support. VCC5V is connected over an resistor divider:
  340. * VCC12V=ADCval*2,5V/1023*(30K+270K)/30K.
  341. */
  342. print_identifier ();
  343. printf ("%d", (result & 0x3FF)* 25 / 1023);
  344. printf (".%d V\n", ((result & 0x3FF)* 25 % 1023) * 10 / 1023);
  345. return 0;
  346. }
  347. static int adc_read (unsigned int channel)
  348. {
  349. int j = 1000; /* timeout value for wait loop in us */
  350. int result;
  351. struct s3c2400_adc *padc;
  352. padc = s3c2400_get_base_adc();
  353. channel &= 0x7;
  354. padc->ADCCON &= ~ADC_STDBM; /* select normal mode */
  355. padc->ADCCON &= ~(0x7 << 3); /* clear the channel bits */
  356. padc->ADCCON |= ((channel << 3) | ADC_ENABLE_START);
  357. while (j--) {
  358. if ((padc->ADCCON & ADC_ENABLE_START) == 0)
  359. break;
  360. udelay (1);
  361. }
  362. if (j == 0) {
  363. printf("%s: ADC timeout\n", __FUNCTION__);
  364. padc->ADCCON |= ADC_STDBM; /* select standby mode */
  365. return -1;
  366. }
  367. result = padc->ADCDAT & 0x3FF;
  368. padc->ADCCON |= ADC_STDBM; /* select standby mode */
  369. debug ("%s: channel %d, result[DIGIT]=%d\n", __FUNCTION__,
  370. (padc->ADCCON >> 3) & 0x7, result);
  371. /*
  372. * Wait for ADC to be ready for next conversion. This delay value was
  373. * estimated, because the datasheet does not specify a value.
  374. */
  375. udelay (1000);
  376. return (result);
  377. }
  378. static void adc_init (void)
  379. {
  380. struct s3c2400_adc *padc;
  381. padc = s3c2400_get_base_adc();
  382. padc->ADCCON &= ~(0xff << 6); /* clear prescaler bits */
  383. padc->ADCCON |= ((65 << 6) | ADC_PRSCEN); /* set prescaler */
  384. /*
  385. * Wait some time to avoid problem with very first call of
  386. * adc_read(). Without * this delay, sometimes the first read adc
  387. * value is 0. Perhaps because the * adjustment of prescaler takes
  388. * some clock cycles?
  389. */
  390. udelay (1000);
  391. return;
  392. }
  393. int do_buttons (void)
  394. {
  395. int result;
  396. int i;
  397. result = *CPLD_BUTTONS; /* read CPLD */
  398. debug ("%s: cpld_taster (32 bit) %#x\n", __FUNCTION__, result);
  399. /* print result to console */
  400. print_identifier ();
  401. for (i = 16; i <= 19; i++) {
  402. if ((result & (1 << i)) == 0)
  403. printf("0");
  404. else
  405. printf("1");
  406. }
  407. printf("\n");
  408. return 0;
  409. }
  410. int do_power_switch (void)
  411. {
  412. int result;
  413. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  414. /* configure GPE7 as input */
  415. gpio->PECON &= ~(0x3 << (2 * 7));
  416. /* signal GPE7 from power switch is low active: 0=on , 1=off */
  417. result = ((gpio->PEDAT & (1 << 7)) == (1 << 7)) ? 0 : 1;
  418. print_identifier ();
  419. printf("%d\n", result);
  420. return 0;
  421. }
  422. int do_fill_level (void)
  423. {
  424. int result;
  425. result = *CPLD_FILL_LEVEL; /* read CPLD */
  426. debug ("%s: cpld_fuellstand (32 bit) %#x\n", __FUNCTION__, result);
  427. /* print result to console */
  428. print_identifier ();
  429. if ((result & (1 << 16)) == 0)
  430. printf("0\n");
  431. else
  432. printf("1\n");
  433. return 0;
  434. }
  435. int do_rotary_switch (void)
  436. {
  437. int result;
  438. /*
  439. * Please note, that the default values of the direction bits are
  440. * undefined after reset. So it is a good idea, to make first a dummy
  441. * call to this function, to clear the direction bits and set so to
  442. * proper values.
  443. */
  444. result = *CPLD_ROTARY_SWITCH; /* read CPLD */
  445. debug ("%s: cpld_inc (32 bit) %#x\n", __FUNCTION__, result);
  446. *CPLD_ROTARY_SWITCH |= (3 << 16); /* clear direction bits in CPLD */
  447. /* print result to console */
  448. print_identifier ();
  449. if ((result & (1 << 16)) == (1 << 16))
  450. printf("R");
  451. if ((result & (1 << 17)) == (1 << 17))
  452. printf("L");
  453. if (((result & (1 << 16)) == 0) && ((result & (1 << 17)) == 0))
  454. printf("0");
  455. if ((result & (1 << 18)) == 0)
  456. printf("0\n");
  457. else
  458. printf("1\n");
  459. return 0;
  460. }
  461. int do_vfd_id (void)
  462. {
  463. int i;
  464. long int pcup_old, pccon_old;
  465. int vfd_board_id;
  466. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  467. /* try to red vfd board id from the value defined by pull-ups */
  468. pcup_old = gpio->PCUP;
  469. pccon_old = gpio->PCCON;
  470. gpio->PCUP = (gpio->PCUP & 0xFFF0); /* activate GPC0...GPC3 pull-ups */
  471. gpio->PCCON = (gpio->PCCON & 0xFFFFFF00); /* configure GPC0...GPC3 as
  472. * inputs */
  473. udelay (10); /* allow signals to settle */
  474. vfd_board_id = (~gpio->PCDAT) & 0x000F; /* read GPC0...GPC3 port pins */
  475. gpio->PCCON = pccon_old;
  476. gpio->PCUP = pcup_old;
  477. /* print vfd_board_id to console */
  478. print_identifier ();
  479. for (i = 0; i < 4; i++) {
  480. if ((vfd_board_id & (1 << i)) == 0)
  481. printf("0");
  482. else
  483. printf("1");
  484. }
  485. printf("\n");
  486. return 0;
  487. }
  488. int do_buzzer (char **argv)
  489. {
  490. int counter;
  491. struct s3c24x0_timers * const timers = s3c24x0_get_base_timers();
  492. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  493. /* set prescaler for timer 2, 3 and 4 */
  494. timers->TCFG0 &= ~0xFF00;
  495. timers->TCFG0 |= 0x0F00;
  496. /* set divider for timer 2 */
  497. timers->TCFG1 &= ~0xF00;
  498. timers->TCFG1 |= 0x300;
  499. /* set frequency */
  500. counter = (PCLK / BUZZER_FREQ) >> 9;
  501. timers->ch[2].TCNTB = counter;
  502. timers->ch[2].TCMPB = counter / 2;
  503. if (strcmp (argv[2], "on") == 0) {
  504. debug ("%s: frequency: %d\n", __FUNCTION__,
  505. BUZZER_FREQ);
  506. /* configure pin GPD7 as TOUT2 */
  507. gpio->PDCON &= ~0xC000;
  508. gpio->PDCON |= 0x8000;
  509. /* start */
  510. timers->TCON = (timers->TCON | UPDATE2 | RELOAD2) &
  511. ~INVERT2;
  512. timers->TCON = (timers->TCON | START2) & ~UPDATE2;
  513. return (0);
  514. }
  515. else if (strcmp (argv[2], "off") == 0) {
  516. /* stop */
  517. timers->TCON &= ~(START2 | RELOAD2);
  518. /* configure GPD7 as output and set to low */
  519. gpio->PDCON &= ~0xC000;
  520. gpio->PDCON |= 0x4000;
  521. gpio->PDDAT &= ~0x80;
  522. return (0);
  523. }
  524. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  525. return 1;
  526. }
  527. int do_led (char **argv)
  528. {
  529. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  530. /* configure PC14 and PC15 as output */
  531. gpio->PCCON &= ~(0xF << 28);
  532. gpio->PCCON |= (0x5 << 28);
  533. /* configure PD0 and PD4 as output */
  534. gpio->PDCON &= ~((0x3 << 8) | 0x3);
  535. gpio->PDCON |= ((0x1 << 8) | 0x1);
  536. switch (simple_strtoul(argv[2], NULL, 10)) {
  537. case 0:
  538. case 1:
  539. break;
  540. case 2:
  541. if (strcmp (argv[3], "on") == 0)
  542. gpio->PCDAT |= (1 << 14);
  543. else
  544. gpio->PCDAT &= ~(1 << 14);
  545. return 0;
  546. case 3:
  547. if (strcmp (argv[3], "on") == 0)
  548. gpio->PCDAT |= (1 << 15);
  549. else
  550. gpio->PCDAT &= ~(1 << 15);
  551. return 0;
  552. case 4:
  553. if (strcmp (argv[3], "on") == 0)
  554. gpio->PDDAT |= (1 << 0);
  555. else
  556. gpio->PDDAT &= ~(1 << 0);
  557. return 0;
  558. case 5:
  559. if (strcmp (argv[3], "on") == 0)
  560. gpio->PDDAT |= (1 << 4);
  561. else
  562. gpio->PDDAT &= ~(1 << 4);
  563. return 0;
  564. default:
  565. break;
  566. }
  567. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  568. return 1;
  569. }
  570. int do_full_bridge (char **argv)
  571. {
  572. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  573. /* configure PD5 and PD6 as output */
  574. gpio->PDCON &= ~((0x3 << 5*2) | (0x3 << 6*2));
  575. gpio->PDCON |= ((0x1 << 5*2) | (0x1 << 6*2));
  576. if (strcmp (argv[2], "+") == 0) {
  577. gpio->PDDAT |= (1 << 5);
  578. gpio->PDDAT |= (1 << 6);
  579. return 0;
  580. }
  581. else if (strcmp (argv[2], "-") == 0) {
  582. gpio->PDDAT &= ~(1 << 5);
  583. gpio->PDDAT |= (1 << 6);
  584. return 0;
  585. }
  586. else if (strcmp (argv[2], "off") == 0) {
  587. gpio->PDDAT &= ~(1 << 5);
  588. gpio->PDDAT &= ~(1 << 6);
  589. return 0;
  590. }
  591. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  592. return 1;
  593. }
  594. /* val must be in [0, 4095] */
  595. static inline unsigned long tsc2000_to_uv (u16 val)
  596. {
  597. return ((250000 * val) / 4096) * 10;
  598. }
  599. int do_dac (char **argv)
  600. {
  601. int brightness;
  602. /* initialize SPI */
  603. tsc2000_spi_init ();
  604. if (((brightness = simple_strtoul (argv[2], NULL, 10)) < 0) ||
  605. (brightness > 255)) {
  606. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  607. return 1;
  608. }
  609. tsc2000_write(TSC2000_REG_DACCTL, 0x0); /* Power up DAC */
  610. tsc2000_write(TSC2000_REG_DAC, brightness & 0xff);
  611. return 0;
  612. }
  613. int do_v_bat (void)
  614. {
  615. unsigned long ret, res;
  616. /* initialize SPI */
  617. spi_init ();
  618. tsc2000_write(TSC2000_REG_ADC, 0x1836);
  619. /* now wait for data available */
  620. adc_wait_conversion_done();
  621. ret = tsc2000_read(TSC2000_REG_BAT1);
  622. res = (tsc2000_to_uv(ret) + 1250) / 2500;
  623. res += (ERROR_BATTERY * res) / 1000;
  624. print_identifier ();
  625. printf ("%ld", (res / 100));
  626. printf (".%ld", ((res % 100) / 10));
  627. printf ("%ld V\n", (res % 10));
  628. return 0;
  629. }
  630. int do_pressure (void)
  631. {
  632. /* initialize SPI */
  633. spi_init ();
  634. tsc2000_write(TSC2000_REG_ADC, 0x2436);
  635. /* now wait for data available */
  636. adc_wait_conversion_done();
  637. print_identifier ();
  638. printf ("%d\n", tsc2000_read(TSC2000_REG_AUX2));
  639. return 0;
  640. }
  641. int do_motor_contact (void)
  642. {
  643. int result;
  644. result = *CPLD_FILL_LEVEL; /* read CPLD */
  645. debug ("%s: cpld_fuellstand (32 bit) %#x\n", __FUNCTION__, result);
  646. /* print result to console */
  647. print_identifier ();
  648. if ((result & (1 << 17)) == 0)
  649. printf("0\n");
  650. else
  651. printf("1\n");
  652. return 0;
  653. }
  654. int do_motor (char **argv)
  655. {
  656. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  657. /* Configure I/O port */
  658. gpio->PGCON &= ~(0x3 << 0);
  659. gpio->PGCON |= (0x1 << 0);
  660. if (strcmp (argv[2], "on") == 0) {
  661. gpio->PGDAT &= ~(1 << 0);
  662. return 0;
  663. }
  664. if (strcmp (argv[2], "off") == 0) {
  665. gpio->PGDAT |= (1 << 0);
  666. return 0;
  667. }
  668. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  669. return 1;
  670. }
  671. static void print_identifier (void)
  672. {
  673. printf ("## FKT: ");
  674. }
  675. int do_pwm (char **argv)
  676. {
  677. int counter;
  678. struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
  679. struct s3c24x0_timers * const timers = s3c24x0_get_base_timers();
  680. if (strcmp (argv[2], "on") == 0) {
  681. /* configure pin GPD8 as TOUT3 */
  682. gpio->PDCON &= ~(0x3 << 8*2);
  683. gpio->PDCON |= (0x2 << 8*2);
  684. /* set prescaler for timer 2, 3 and 4 */
  685. timers->TCFG0 &= ~0xFF00;
  686. timers->TCFG0 |= 0x0F00;
  687. /* set divider for timer 3 */
  688. timers->TCFG1 &= ~(0xf << 12);
  689. timers->TCFG1 |= (0x3 << 12);
  690. /* set frequency */
  691. counter = (PCLK / PWM_FREQ) >> 9;
  692. timers->ch[3].TCNTB = counter;
  693. timers->ch[3].TCMPB = counter / 2;
  694. /* start timer */
  695. timers->TCON = (timers->TCON | UPDATE3 | RELOAD3) & ~INVERT3;
  696. timers->TCON = (timers->TCON | START3) & ~UPDATE3;
  697. return 0;
  698. }
  699. if (strcmp (argv[2], "off") == 0) {
  700. /* stop timer */
  701. timers->TCON &= ~(START2 | RELOAD2);
  702. /* configure pin GPD8 as output and set to 0 */
  703. gpio->PDCON &= ~(0x3 << 8*2);
  704. gpio->PDCON |= (0x1 << 8*2);
  705. gpio->PDDAT &= ~(1 << 8);
  706. return 0;
  707. }
  708. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  709. return 1;
  710. }
  711. int do_thermo (char **argv)
  712. {
  713. int channel, res;
  714. tsc2000_reg_init ();
  715. if (strcmp (argv[2], "all") == 0) {
  716. int i;
  717. for (i=0; i <= 15; i++) {
  718. res = tsc2000_read_channel(i);
  719. print_identifier ();
  720. printf ("c%d: %d\n", i, res);
  721. }
  722. return 0;
  723. }
  724. channel = simple_strtoul (argv[2], NULL, 10);
  725. res = tsc2000_read_channel(channel);
  726. print_identifier ();
  727. printf ("%d\n", res);
  728. return 0; /* return OK */
  729. }
  730. int do_touch (char **argv)
  731. {
  732. int x, y;
  733. if (strcmp (argv[2], "tl") == 0) {
  734. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  735. touch_wait_pressed();
  736. #else
  737. {
  738. int i;
  739. for (i = 0; i < (TOUCH_TIMEOUT * 1000); i++) {
  740. if (touch_check_pressed ()) {
  741. break;
  742. }
  743. udelay (1000); /* pause 1 ms */
  744. }
  745. }
  746. if (!touch_check_pressed()) {
  747. print_identifier ();
  748. printf ("error: touch not pressed\n");
  749. return 1;
  750. }
  751. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  752. touch_read_x_y (&x, &y);
  753. print_identifier ();
  754. printf ("x=%d y=%d\n", x, y);
  755. return touch_write_clibration_values (CALIB_TL, x, y);
  756. }
  757. else if (strcmp (argv[2], "dr") == 0) {
  758. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  759. touch_wait_pressed();
  760. #else
  761. {
  762. int i;
  763. for (i = 0; i < (TOUCH_TIMEOUT * 1000); i++) {
  764. if (touch_check_pressed ()) {
  765. break;
  766. }
  767. udelay (1000); /* pause 1 ms */
  768. }
  769. }
  770. if (!touch_check_pressed()) {
  771. print_identifier ();
  772. printf ("error: touch not pressed\n");
  773. return 1;
  774. }
  775. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  776. touch_read_x_y (&x, &y);
  777. print_identifier ();
  778. printf ("x=%d y=%d\n", x, y);
  779. return touch_write_clibration_values (CALIB_DR, x, y);
  780. }
  781. return 1; /* not "tl", nor "dr", so return error */
  782. }
  783. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  784. static void touch_wait_pressed (void)
  785. {
  786. while (!(tsc2000_read(TSC2000_REG_ADC) & TC_PSM));
  787. }
  788. #else
  789. static int touch_check_pressed (void)
  790. {
  791. return (tsc2000_read(TSC2000_REG_ADC) & TC_PSM);
  792. }
  793. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  794. static int touch_write_clibration_values (int calib_point, int x, int y)
  795. {
  796. #if defined(CONFIG_CMD_I2C)
  797. int x_verify = 0;
  798. int y_verify = 0;
  799. tsc2000_reg_init ();
  800. if (calib_point == CALIB_TL) {
  801. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X0, 1,
  802. (unsigned char *)&x, 2)) {
  803. return 1;
  804. }
  805. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y0, 1,
  806. (unsigned char *)&y, 2)) {
  807. return 1;
  808. }
  809. /* verify written values */
  810. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X0, 1,
  811. (unsigned char *)&x_verify, 2)) {
  812. return 1;
  813. }
  814. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y0, 1,
  815. (unsigned char *)&y_verify, 2)) {
  816. return 1;
  817. }
  818. if ((y != y_verify) || (x != x_verify)) {
  819. print_identifier ();
  820. printf ("error: verify error\n");
  821. return 1;
  822. }
  823. return 0; /* no error */
  824. }
  825. else if (calib_point == CALIB_DR) {
  826. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X1, 1,
  827. (unsigned char *)&x, 2)) {
  828. return 1;
  829. }
  830. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y1, 1,
  831. (unsigned char *)&y, 2)) {
  832. return 1;
  833. }
  834. /* verify written values */
  835. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X1, 1,
  836. (unsigned char *)&x_verify, 2)) {
  837. return 1;
  838. }
  839. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y1, 1,
  840. (unsigned char *)&y_verify, 2)) {
  841. return 1;
  842. }
  843. if ((y != y_verify) || (x != x_verify)) {
  844. print_identifier ();
  845. printf ("error: verify error\n");
  846. return 1;
  847. }
  848. return 0;
  849. }
  850. return 1;
  851. #else
  852. printf ("No I2C support enabled (CONFIG_CMD_I2C), could not write "
  853. "to EEPROM\n");
  854. return (1);
  855. #endif
  856. }
  857. static void touch_read_x_y (int *px, int *py)
  858. {
  859. tsc2000_write(TSC2000_REG_ADC, DEFAULT_ADC | TC_AD0 | TC_AD1);
  860. adc_wait_conversion_done();
  861. *px = tsc2000_read(TSC2000_REG_X);
  862. tsc2000_write(TSC2000_REG_ADC, DEFAULT_ADC | TC_AD2);
  863. adc_wait_conversion_done();
  864. *py = tsc2000_read(TSC2000_REG_Y);
  865. }
  866. int do_rs485 (char **argv)
  867. {
  868. int timeout;
  869. char data[RS485_MAX_RECEIVE_BUF_LEN];
  870. if (strcmp (argv[2], "send") == 0) {
  871. return (rs485_send_line (argv[3]));
  872. }
  873. else if (strcmp (argv[2], "receive") == 0) {
  874. timeout = simple_strtoul(argv[3], NULL, 10);
  875. if (rs485_receive_chars (data, timeout) != 0) {
  876. print_identifier ();
  877. printf ("## nothing received\n");
  878. return (1);
  879. }
  880. else {
  881. print_identifier ();
  882. printf ("%s\n", data);
  883. return (0);
  884. }
  885. }
  886. printf ("%s: unknown command %s\n", __FUNCTION__, argv[2]);
  887. return (1); /* unknown command, return error */
  888. }
  889. static int rs485_send_line (const char *data)
  890. {
  891. rs485_init ();
  892. trab_rs485_enable_tx ();
  893. rs485_puts (data);
  894. rs485_putc ('\n');
  895. return (0);
  896. }
  897. static int rs485_receive_chars (char *data, int timeout)
  898. {
  899. int i;
  900. int receive_count = 0;
  901. rs485_init ();
  902. trab_rs485_enable_rx ();
  903. /* test every 1 ms for received characters to avoid a receive FIFO
  904. * overrun (@ 38.400 Baud) */
  905. for (i = 0; i < (timeout * 1000); i++) {
  906. while (rs485_tstc ()) {
  907. if (receive_count >= RS485_MAX_RECEIVE_BUF_LEN-1)
  908. break;
  909. *data++ = rs485_getc ();
  910. receive_count++;
  911. }
  912. udelay (1000); /* pause 1 ms */
  913. }
  914. *data = '\0'; /* terminate string */
  915. if (receive_count == 0)
  916. return (1);
  917. else
  918. return (0);
  919. }
  920. int do_serial_number (char **argv)
  921. {
  922. #if defined(CONFIG_CMD_I2C)
  923. unsigned int serial_number;
  924. if (strcmp (argv[2], "read") == 0) {
  925. if (i2c_read (I2C_EEPROM_DEV_ADDR, SERIAL_NUMBER, 1,
  926. (unsigned char *)&serial_number, 4)) {
  927. printf ("could not read from eeprom\n");
  928. return (1);
  929. }
  930. print_identifier ();
  931. printf ("%08d\n", serial_number);
  932. return (0);
  933. }
  934. else if (strcmp (argv[2], "write") == 0) {
  935. serial_number = simple_strtoul(argv[3], NULL, 10);
  936. if (i2c_write (I2C_EEPROM_DEV_ADDR, SERIAL_NUMBER, 1,
  937. (unsigned char *)&serial_number, 4)) {
  938. printf ("could not write to eeprom\n");
  939. return (1);
  940. }
  941. return (0);
  942. }
  943. printf ("%s: unknown command %s\n", __FUNCTION__, argv[2]);
  944. return (1); /* unknown command, return error */
  945. #else
  946. printf ("No I2C support enabled (CONFIG_CMD_I2C), could not write "
  947. "to EEPROM\n");
  948. return (1);
  949. #endif
  950. }
  951. int do_crc16 (void)
  952. {
  953. #if defined(CONFIG_CMD_I2C)
  954. int crc;
  955. unsigned char buf[EEPROM_MAX_CRC_BUF];
  956. if (i2c_read (I2C_EEPROM_DEV_ADDR, 0, 1, buf, 60)) {
  957. printf ("could not read from eeprom\n");
  958. return (1);
  959. }
  960. crc = 0; /* start value of crc calculation */
  961. crc = updcrc (crc, buf, 60);
  962. print_identifier ();
  963. printf ("crc16=%#04x\n", crc);
  964. if (i2c_write (I2C_EEPROM_DEV_ADDR, CRC16, 1, (unsigned char *)&crc,
  965. sizeof (crc))) {
  966. printf ("could not read from eeprom\n");
  967. return (1);
  968. }
  969. return (0);
  970. #else
  971. printf ("No I2C support enabled (CONFIG_CMD_I2C), could not write "
  972. "to EEPROM\n");
  973. return (1);
  974. #endif
  975. }
  976. /*
  977. * Calculate, intelligently, the CRC of a dataset incrementally given a
  978. * buffer full at a time.
  979. * Initialize crc to 0 for XMODEM, -1 for CCITT.
  980. *
  981. * Usage:
  982. * newcrc = updcrc( oldcrc, bufadr, buflen )
  983. * unsigned int oldcrc, buflen;
  984. * char *bufadr;
  985. *
  986. * Compile with -DTEST to generate program that prints CRC of stdin to stdout.
  987. * Compile with -DMAKETAB to print values for crctab to stdout
  988. */
  989. /* the CRC polynomial. This is used by XMODEM (almost CCITT).
  990. * If you change P, you must change crctab[]'s initial value to what is
  991. * printed by initcrctab()
  992. */
  993. #define P 0x1021
  994. /* number of bits in CRC: don't change it. */
  995. #define W 16
  996. /* this the number of bits per char: don't change it. */
  997. #define B 8
  998. static unsigned short crctab[1<<B] = { /* as calculated by initcrctab() */
  999. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  1000. 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  1001. 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  1002. 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  1003. 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  1004. 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  1005. 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  1006. 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  1007. 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  1008. 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  1009. 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  1010. 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  1011. 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  1012. 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  1013. 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  1014. 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  1015. 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  1016. 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  1017. 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  1018. 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  1019. 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  1020. 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  1021. 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  1022. 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  1023. 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  1024. 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  1025. 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  1026. 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  1027. 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  1028. 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  1029. 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  1030. 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  1031. };
  1032. static unsigned short updcrc(unsigned short icrc, unsigned char *icp,
  1033. unsigned int icnt )
  1034. {
  1035. register unsigned short crc = icrc;
  1036. register unsigned char *cp = icp;
  1037. register unsigned int cnt = icnt;
  1038. while (cnt--)
  1039. crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
  1040. return (crc);
  1041. }
  1042. int do_gain (char **argv)
  1043. {
  1044. int range;
  1045. range = simple_strtoul (argv[2], NULL, 10);
  1046. if ((range < 1) || (range > 3))
  1047. {
  1048. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  1049. return 1;
  1050. }
  1051. tsc2000_set_range (range);
  1052. return (0);
  1053. }
  1054. int do_eeprom (char **argv)
  1055. {
  1056. #if defined(CONFIG_CMD_I2C)
  1057. if (strcmp (argv[2], "read") == 0) {
  1058. return (trab_eeprom_read (argv));
  1059. }
  1060. else if (strcmp (argv[2], "write") == 0) {
  1061. return (trab_eeprom_write (argv));
  1062. }
  1063. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  1064. return (1);
  1065. #else
  1066. printf ("No I2C support enabled (CONFIG_CMD_I2C), could not write "
  1067. "to EEPROM\n");
  1068. return (1);
  1069. #endif
  1070. }
  1071. #if defined(CONFIG_CMD_I2C)
  1072. static int trab_eeprom_read (char **argv)
  1073. {
  1074. int i;
  1075. int len;
  1076. unsigned int addr;
  1077. long int value = 0;
  1078. uchar *buffer;
  1079. buffer = (uchar *) &value;
  1080. addr = simple_strtoul (argv[3], NULL, 10);
  1081. addr &= 0xfff;
  1082. len = simple_strtoul (argv[4], NULL, 10);
  1083. if ((len < 1) || (len > 4)) {
  1084. printf ("%s: invalid parameter %s\n", __FUNCTION__,
  1085. argv[4]);
  1086. return (1);
  1087. }
  1088. for (i = 0; i < len; i++) {
  1089. if (i2c_read (I2C_EEPROM_DEV_ADDR, addr+i, 1, buffer+i, 1)) {
  1090. printf ("%s: could not read from i2c device %#x"
  1091. ", addr %d\n", __FUNCTION__,
  1092. I2C_EEPROM_DEV_ADDR, addr);
  1093. return (1);
  1094. }
  1095. }
  1096. print_identifier ();
  1097. if (strcmp (argv[5], "-") == 0) {
  1098. if (len == 1)
  1099. printf ("%d\n", (signed char) value);
  1100. else if (len == 2)
  1101. printf ("%d\n", (signed short int) value);
  1102. else
  1103. printf ("%ld\n", value);
  1104. }
  1105. else {
  1106. if (len == 1)
  1107. printf ("%d\n", (unsigned char) value);
  1108. else if (len == 2)
  1109. printf ("%d\n", (unsigned short int) value);
  1110. else
  1111. printf ("%ld\n", (unsigned long int) value);
  1112. }
  1113. return (0);
  1114. }
  1115. static int trab_eeprom_write (char **argv)
  1116. {
  1117. int i;
  1118. int len;
  1119. unsigned int addr;
  1120. long int value = 0;
  1121. uchar *buffer;
  1122. buffer = (uchar *) &value;
  1123. addr = simple_strtoul (argv[3], NULL, 10);
  1124. addr &= 0xfff;
  1125. len = simple_strtoul (argv[4], NULL, 10);
  1126. if ((len < 1) || (len > 4)) {
  1127. printf ("%s: invalid parameter %s\n", __FUNCTION__,
  1128. argv[4]);
  1129. return (1);
  1130. }
  1131. value = simple_strtol (argv[5], NULL, 10);
  1132. debug ("value=%ld\n", value);
  1133. for (i = 0; i < len; i++) {
  1134. if (i2c_write (I2C_EEPROM_DEV_ADDR, addr+i, 1, buffer+i, 1)) {
  1135. printf ("%s: could not write to i2c device %d"
  1136. ", addr %d\n", __FUNCTION__,
  1137. I2C_EEPROM_DEV_ADDR, addr);
  1138. return (1);
  1139. }
  1140. #if 0
  1141. printf ("chip=%#x, addr+i=%#x+%d=%p, alen=%d, *buffer+i="
  1142. "%#x+%d=%p=%#x \n",I2C_EEPROM_DEV_ADDR_DEV_ADDR , addr,
  1143. i, addr+i, 1, buffer, i, buffer+i, *(buffer+i));
  1144. #endif
  1145. udelay (30000); /* wait for EEPROM ready */
  1146. }
  1147. return (0);
  1148. }
  1149. int i2c_write_multiple (uchar chip, uint addr, int alen,
  1150. uchar *buffer, int len)
  1151. {
  1152. int i;
  1153. if (alen != 1) {
  1154. printf ("%s: addr len other than 1 not supported\n",
  1155. __FUNCTION__);
  1156. return (1);
  1157. }
  1158. for (i = 0; i < len; i++) {
  1159. if (i2c_write (chip, addr+i, alen, buffer+i, 1)) {
  1160. printf ("%s: could not write to i2c device %d"
  1161. ", addr %d\n", __FUNCTION__, chip, addr);
  1162. return (1);
  1163. }
  1164. #if 0
  1165. printf ("chip=%#x, addr+i=%#x+%d=%p, alen=%d, *buffer+i="
  1166. "%#x+%d=%p=\"%.1s\"\n", chip, addr, i, addr+i,
  1167. alen, buffer, i, buffer+i, buffer+i);
  1168. #endif
  1169. udelay (30000);
  1170. }
  1171. return (0);
  1172. }
  1173. int i2c_read_multiple ( uchar chip, uint addr, int alen,
  1174. uchar *buffer, int len)
  1175. {
  1176. int i;
  1177. if (alen != 1) {
  1178. printf ("%s: addr len other than 1 not supported\n",
  1179. __FUNCTION__);
  1180. return (1);
  1181. }
  1182. for (i = 0; i < len; i++) {
  1183. if (i2c_read (chip, addr+i, alen, buffer+i, 1)) {
  1184. printf ("%s: could not read from i2c device %#x"
  1185. ", addr %d\n", __FUNCTION__, chip, addr);
  1186. return (1);
  1187. }
  1188. }
  1189. return (0);
  1190. }
  1191. #endif