trab_fkt.c 33 KB

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