cmd_i2c.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /*
  2. * (C) Copyright 2009
  3. * Sergey Kubushyn, himself, ksi@koi8.net
  4. *
  5. * Changes for unified multibus/multiadapter I2C support.
  6. *
  7. * (C) Copyright 2001
  8. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. /*
  13. * I2C Functions similar to the standard memory functions.
  14. *
  15. * There are several parameters in many of the commands that bear further
  16. * explanations:
  17. *
  18. * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
  19. * Each I2C chip on the bus has a unique address. On the I2C data bus,
  20. * the address is the upper seven bits and the LSB is the "read/write"
  21. * bit. Note that the {i2c_chip} address specified on the command
  22. * line is not shifted up: e.g. a typical EEPROM memory chip may have
  23. * an I2C address of 0x50, but the data put on the bus will be 0xA0
  24. * for write and 0xA1 for read. This "non shifted" address notation
  25. * matches at least half of the data sheets :-/.
  26. *
  27. * {addr} is the address (or offset) within the chip. Small memory
  28. * chips have 8 bit addresses. Large memory chips have 16 bit
  29. * addresses. Other memory chips have 9, 10, or 11 bit addresses.
  30. * Many non-memory chips have multiple registers and {addr} is used
  31. * as the register index. Some non-memory chips have only one register
  32. * and therefore don't need any {addr} parameter.
  33. *
  34. * The default {addr} parameter is one byte (.1) which works well for
  35. * memories and registers with 8 bits of address space.
  36. *
  37. * You can specify the length of the {addr} field with the optional .0,
  38. * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
  39. * manipulating a single register device which doesn't use an address
  40. * field, use "0.0" for the address and the ".0" length field will
  41. * suppress the address in the I2C data stream. This also works for
  42. * successive reads using the I2C auto-incrementing memory pointer.
  43. *
  44. * If you are manipulating a large memory with 2-byte addresses, use
  45. * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
  46. *
  47. * Then there are the unfortunate memory chips that spill the most
  48. * significant 1, 2, or 3 bits of address into the chip address byte.
  49. * This effectively makes one chip (logically) look like 2, 4, or
  50. * 8 chips. This is handled (awkwardly) by #defining
  51. * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
  52. * {addr} field (since .1 is the default, it doesn't actually have to
  53. * be specified). Examples: given a memory chip at I2C chip address
  54. * 0x50, the following would happen...
  55. * i2c md 50 0 10 display 16 bytes starting at 0x000
  56. * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
  57. * i2c md 50 100 10 display 16 bytes starting at 0x100
  58. * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
  59. * i2c md 50 210 10 display 16 bytes starting at 0x210
  60. * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
  61. * This is awfully ugly. It would be nice if someone would think up
  62. * a better way of handling this.
  63. *
  64. * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
  65. */
  66. #include <common.h>
  67. #include <bootretry.h>
  68. #include <cli.h>
  69. #include <command.h>
  70. #include <edid.h>
  71. #include <environment.h>
  72. #include <i2c.h>
  73. #include <malloc.h>
  74. #include <asm/byteorder.h>
  75. #include <linux/compiler.h>
  76. DECLARE_GLOBAL_DATA_PTR;
  77. /* Display values from last command.
  78. * Memory modify remembered values are different from display memory.
  79. */
  80. static uchar i2c_dp_last_chip;
  81. static uint i2c_dp_last_addr;
  82. static uint i2c_dp_last_alen;
  83. static uint i2c_dp_last_length = 0x10;
  84. static uchar i2c_mm_last_chip;
  85. static uint i2c_mm_last_addr;
  86. static uint i2c_mm_last_alen;
  87. /* If only one I2C bus is present, the list of devices to ignore when
  88. * the probe command is issued is represented by a 1D array of addresses.
  89. * When multiple buses are present, the list is an array of bus-address
  90. * pairs. The following macros take care of this */
  91. #if defined(CONFIG_SYS_I2C_NOPROBES)
  92. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  93. static struct
  94. {
  95. uchar bus;
  96. uchar addr;
  97. } i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
  98. #define GET_BUS_NUM i2c_get_bus_num()
  99. #define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
  100. #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
  101. #define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
  102. #else /* single bus */
  103. static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
  104. #define GET_BUS_NUM 0
  105. #define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
  106. #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
  107. #define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
  108. #endif /* defined(CONFIG_SYS_I2C) */
  109. #endif
  110. #define DISP_LINE_LEN 16
  111. /**
  112. * i2c_init_board() - Board-specific I2C bus init
  113. *
  114. * This function is the default no-op implementation of I2C bus
  115. * initialization. This function can be overriden by board-specific
  116. * implementation if needed.
  117. */
  118. __weak
  119. void i2c_init_board(void)
  120. {
  121. }
  122. /* TODO: Implement architecture-specific get/set functions */
  123. /**
  124. * i2c_get_bus_speed() - Return I2C bus speed
  125. *
  126. * This function is the default implementation of function for retrieveing
  127. * the current I2C bus speed in Hz.
  128. *
  129. * A driver implementing runtime switching of I2C bus speed must override
  130. * this function to report the speed correctly. Simple or legacy drivers
  131. * can use this fallback.
  132. *
  133. * Returns I2C bus speed in Hz.
  134. */
  135. #if !defined(CONFIG_SYS_I2C)
  136. /*
  137. * TODO: Implement architecture-specific get/set functions
  138. * Should go away, if we switched completely to new multibus support
  139. */
  140. __weak
  141. unsigned int i2c_get_bus_speed(void)
  142. {
  143. return CONFIG_SYS_I2C_SPEED;
  144. }
  145. /**
  146. * i2c_set_bus_speed() - Configure I2C bus speed
  147. * @speed: Newly set speed of the I2C bus in Hz
  148. *
  149. * This function is the default implementation of function for setting
  150. * the I2C bus speed in Hz.
  151. *
  152. * A driver implementing runtime switching of I2C bus speed must override
  153. * this function to report the speed correctly. Simple or legacy drivers
  154. * can use this fallback.
  155. *
  156. * Returns zero on success, negative value on error.
  157. */
  158. __weak
  159. int i2c_set_bus_speed(unsigned int speed)
  160. {
  161. if (speed != CONFIG_SYS_I2C_SPEED)
  162. return -1;
  163. return 0;
  164. }
  165. #endif
  166. /**
  167. * get_alen() - Small parser helper function to get address length
  168. *
  169. * Returns the address length.
  170. */
  171. static uint get_alen(char *arg)
  172. {
  173. int j;
  174. int alen;
  175. alen = 1;
  176. for (j = 0; j < 8; j++) {
  177. if (arg[j] == '.') {
  178. alen = arg[j+1] - '0';
  179. break;
  180. } else if (arg[j] == '\0')
  181. break;
  182. }
  183. return alen;
  184. }
  185. /**
  186. * do_i2c_read() - Handle the "i2c read" command-line command
  187. * @cmdtp: Command data struct pointer
  188. * @flag: Command flag
  189. * @argc: Command-line argument count
  190. * @argv: Array of command-line arguments
  191. *
  192. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  193. * on error.
  194. *
  195. * Syntax:
  196. * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
  197. */
  198. static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  199. {
  200. u_char chip;
  201. uint devaddr, alen, length;
  202. u_char *memaddr;
  203. if (argc != 5)
  204. return CMD_RET_USAGE;
  205. /*
  206. * I2C chip address
  207. */
  208. chip = simple_strtoul(argv[1], NULL, 16);
  209. /*
  210. * I2C data address within the chip. This can be 1 or
  211. * 2 bytes long. Some day it might be 3 bytes long :-).
  212. */
  213. devaddr = simple_strtoul(argv[2], NULL, 16);
  214. alen = get_alen(argv[2]);
  215. if (alen > 3)
  216. return CMD_RET_USAGE;
  217. /*
  218. * Length is the number of objects, not number of bytes.
  219. */
  220. length = simple_strtoul(argv[3], NULL, 16);
  221. /*
  222. * memaddr is the address where to store things in memory
  223. */
  224. memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
  225. if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
  226. puts ("Error reading the chip.\n");
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  232. {
  233. u_char chip;
  234. uint devaddr, alen, length;
  235. u_char *memaddr;
  236. if (argc != 5)
  237. return cmd_usage(cmdtp);
  238. /*
  239. * memaddr is the address where to store things in memory
  240. */
  241. memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
  242. /*
  243. * I2C chip address
  244. */
  245. chip = simple_strtoul(argv[2], NULL, 16);
  246. /*
  247. * I2C data address within the chip. This can be 1 or
  248. * 2 bytes long. Some day it might be 3 bytes long :-).
  249. */
  250. devaddr = simple_strtoul(argv[3], NULL, 16);
  251. alen = get_alen(argv[3]);
  252. if (alen > 3)
  253. return cmd_usage(cmdtp);
  254. /*
  255. * Length is the number of objects, not number of bytes.
  256. */
  257. length = simple_strtoul(argv[4], NULL, 16);
  258. while (length-- > 0) {
  259. if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
  260. puts("Error writing to the chip.\n");
  261. return 1;
  262. }
  263. /*
  264. * No write delay with FRAM devices.
  265. */
  266. #if !defined(CONFIG_SYS_I2C_FRAM)
  267. udelay(11000);
  268. #endif
  269. }
  270. return 0;
  271. }
  272. /**
  273. * do_i2c_md() - Handle the "i2c md" command-line command
  274. * @cmdtp: Command data struct pointer
  275. * @flag: Command flag
  276. * @argc: Command-line argument count
  277. * @argv: Array of command-line arguments
  278. *
  279. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  280. * on error.
  281. *
  282. * Syntax:
  283. * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
  284. */
  285. static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  286. {
  287. u_char chip;
  288. uint addr, alen, length;
  289. int j, nbytes, linebytes;
  290. /* We use the last specified parameters, unless new ones are
  291. * entered.
  292. */
  293. chip = i2c_dp_last_chip;
  294. addr = i2c_dp_last_addr;
  295. alen = i2c_dp_last_alen;
  296. length = i2c_dp_last_length;
  297. if (argc < 3)
  298. return CMD_RET_USAGE;
  299. if ((flag & CMD_FLAG_REPEAT) == 0) {
  300. /*
  301. * New command specified.
  302. */
  303. /*
  304. * I2C chip address
  305. */
  306. chip = simple_strtoul(argv[1], NULL, 16);
  307. /*
  308. * I2C data address within the chip. This can be 1 or
  309. * 2 bytes long. Some day it might be 3 bytes long :-).
  310. */
  311. addr = simple_strtoul(argv[2], NULL, 16);
  312. alen = get_alen(argv[2]);
  313. if (alen > 3)
  314. return CMD_RET_USAGE;
  315. /*
  316. * If another parameter, it is the length to display.
  317. * Length is the number of objects, not number of bytes.
  318. */
  319. if (argc > 3)
  320. length = simple_strtoul(argv[3], NULL, 16);
  321. }
  322. /*
  323. * Print the lines.
  324. *
  325. * We buffer all read data, so we can make sure data is read only
  326. * once.
  327. */
  328. nbytes = length;
  329. do {
  330. unsigned char linebuf[DISP_LINE_LEN];
  331. unsigned char *cp;
  332. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  333. if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
  334. puts ("Error reading the chip.\n");
  335. else {
  336. printf("%04x:", addr);
  337. cp = linebuf;
  338. for (j=0; j<linebytes; j++) {
  339. printf(" %02x", *cp++);
  340. addr++;
  341. }
  342. puts (" ");
  343. cp = linebuf;
  344. for (j=0; j<linebytes; j++) {
  345. if ((*cp < 0x20) || (*cp > 0x7e))
  346. puts (".");
  347. else
  348. printf("%c", *cp);
  349. cp++;
  350. }
  351. putc ('\n');
  352. }
  353. nbytes -= linebytes;
  354. } while (nbytes > 0);
  355. i2c_dp_last_chip = chip;
  356. i2c_dp_last_addr = addr;
  357. i2c_dp_last_alen = alen;
  358. i2c_dp_last_length = length;
  359. return 0;
  360. }
  361. /**
  362. * do_i2c_mw() - Handle the "i2c mw" command-line command
  363. * @cmdtp: Command data struct pointer
  364. * @flag: Command flag
  365. * @argc: Command-line argument count
  366. * @argv: Array of command-line arguments
  367. *
  368. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  369. * on error.
  370. *
  371. * Syntax:
  372. * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
  373. */
  374. static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  375. {
  376. uchar chip;
  377. ulong addr;
  378. uint alen;
  379. uchar byte;
  380. int count;
  381. if ((argc < 4) || (argc > 5))
  382. return CMD_RET_USAGE;
  383. /*
  384. * Chip is always specified.
  385. */
  386. chip = simple_strtoul(argv[1], NULL, 16);
  387. /*
  388. * Address is always specified.
  389. */
  390. addr = simple_strtoul(argv[2], NULL, 16);
  391. alen = get_alen(argv[2]);
  392. if (alen > 3)
  393. return CMD_RET_USAGE;
  394. /*
  395. * Value to write is always specified.
  396. */
  397. byte = simple_strtoul(argv[3], NULL, 16);
  398. /*
  399. * Optional count
  400. */
  401. if (argc == 5)
  402. count = simple_strtoul(argv[4], NULL, 16);
  403. else
  404. count = 1;
  405. while (count-- > 0) {
  406. if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
  407. puts ("Error writing the chip.\n");
  408. /*
  409. * Wait for the write to complete. The write can take
  410. * up to 10mSec (we allow a little more time).
  411. */
  412. /*
  413. * No write delay with FRAM devices.
  414. */
  415. #if !defined(CONFIG_SYS_I2C_FRAM)
  416. udelay(11000);
  417. #endif
  418. }
  419. return 0;
  420. }
  421. /**
  422. * do_i2c_crc() - Handle the "i2c crc32" command-line command
  423. * @cmdtp: Command data struct pointer
  424. * @flag: Command flag
  425. * @argc: Command-line argument count
  426. * @argv: Array of command-line arguments
  427. *
  428. * Calculate a CRC on memory
  429. *
  430. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  431. * on error.
  432. *
  433. * Syntax:
  434. * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
  435. */
  436. static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  437. {
  438. uchar chip;
  439. ulong addr;
  440. uint alen;
  441. int count;
  442. uchar byte;
  443. ulong crc;
  444. ulong err;
  445. if (argc < 4)
  446. return CMD_RET_USAGE;
  447. /*
  448. * Chip is always specified.
  449. */
  450. chip = simple_strtoul(argv[1], NULL, 16);
  451. /*
  452. * Address is always specified.
  453. */
  454. addr = simple_strtoul(argv[2], NULL, 16);
  455. alen = get_alen(argv[2]);
  456. if (alen > 3)
  457. return CMD_RET_USAGE;
  458. /*
  459. * Count is always specified
  460. */
  461. count = simple_strtoul(argv[3], NULL, 16);
  462. printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
  463. /*
  464. * CRC a byte at a time. This is going to be slooow, but hey, the
  465. * memories are small and slow too so hopefully nobody notices.
  466. */
  467. crc = 0;
  468. err = 0;
  469. while (count-- > 0) {
  470. if (i2c_read(chip, addr, alen, &byte, 1) != 0)
  471. err++;
  472. crc = crc32 (crc, &byte, 1);
  473. addr++;
  474. }
  475. if (err > 0)
  476. puts ("Error reading the chip,\n");
  477. else
  478. printf ("%08lx\n", crc);
  479. return 0;
  480. }
  481. /**
  482. * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
  483. * @cmdtp: Command data struct pointer
  484. * @flag: Command flag
  485. * @argc: Command-line argument count
  486. * @argv: Array of command-line arguments
  487. *
  488. * Modify memory.
  489. *
  490. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  491. * on error.
  492. *
  493. * Syntax:
  494. * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  495. * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  496. */
  497. static int
  498. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
  499. {
  500. uchar chip;
  501. ulong addr;
  502. uint alen;
  503. ulong data;
  504. int size = 1;
  505. int nbytes;
  506. if (argc != 3)
  507. return CMD_RET_USAGE;
  508. #ifdef CONFIG_BOOT_RETRY_TIME
  509. reset_cmd_timeout(); /* got a good command to get here */
  510. #endif
  511. /*
  512. * We use the last specified parameters, unless new ones are
  513. * entered.
  514. */
  515. chip = i2c_mm_last_chip;
  516. addr = i2c_mm_last_addr;
  517. alen = i2c_mm_last_alen;
  518. if ((flag & CMD_FLAG_REPEAT) == 0) {
  519. /*
  520. * New command specified. Check for a size specification.
  521. * Defaults to byte if no or incorrect specification.
  522. */
  523. size = cmd_get_data_size(argv[0], 1);
  524. /*
  525. * Chip is always specified.
  526. */
  527. chip = simple_strtoul(argv[1], NULL, 16);
  528. /*
  529. * Address is always specified.
  530. */
  531. addr = simple_strtoul(argv[2], NULL, 16);
  532. alen = get_alen(argv[2]);
  533. if (alen > 3)
  534. return CMD_RET_USAGE;
  535. }
  536. /*
  537. * Print the address, followed by value. Then accept input for
  538. * the next value. A non-converted value exits.
  539. */
  540. do {
  541. printf("%08lx:", addr);
  542. if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
  543. puts ("\nError reading the chip,\n");
  544. else {
  545. data = cpu_to_be32(data);
  546. if (size == 1)
  547. printf(" %02lx", (data >> 24) & 0x000000FF);
  548. else if (size == 2)
  549. printf(" %04lx", (data >> 16) & 0x0000FFFF);
  550. else
  551. printf(" %08lx", data);
  552. }
  553. nbytes = cli_readline(" ? ");
  554. if (nbytes == 0) {
  555. /*
  556. * <CR> pressed as only input, don't modify current
  557. * location and move to next.
  558. */
  559. if (incrflag)
  560. addr += size;
  561. nbytes = size;
  562. #ifdef CONFIG_BOOT_RETRY_TIME
  563. reset_cmd_timeout(); /* good enough to not time out */
  564. #endif
  565. }
  566. #ifdef CONFIG_BOOT_RETRY_TIME
  567. else if (nbytes == -2)
  568. break; /* timed out, exit the command */
  569. #endif
  570. else {
  571. char *endp;
  572. data = simple_strtoul(console_buffer, &endp, 16);
  573. if (size == 1)
  574. data = data << 24;
  575. else if (size == 2)
  576. data = data << 16;
  577. data = be32_to_cpu(data);
  578. nbytes = endp - console_buffer;
  579. if (nbytes) {
  580. #ifdef CONFIG_BOOT_RETRY_TIME
  581. /*
  582. * good enough to not time out
  583. */
  584. reset_cmd_timeout();
  585. #endif
  586. if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
  587. puts ("Error writing the chip.\n");
  588. #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  589. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  590. #endif
  591. if (incrflag)
  592. addr += size;
  593. }
  594. }
  595. } while (nbytes);
  596. i2c_mm_last_chip = chip;
  597. i2c_mm_last_addr = addr;
  598. i2c_mm_last_alen = alen;
  599. return 0;
  600. }
  601. /**
  602. * do_i2c_probe() - Handle the "i2c probe" command-line command
  603. * @cmdtp: Command data struct pointer
  604. * @flag: Command flag
  605. * @argc: Command-line argument count
  606. * @argv: Array of command-line arguments
  607. *
  608. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  609. * on error.
  610. *
  611. * Syntax:
  612. * i2c probe {addr}
  613. *
  614. * Returns zero (success) if one or more I2C devices was found
  615. */
  616. static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  617. {
  618. int j;
  619. int addr = -1;
  620. int found = 0;
  621. #if defined(CONFIG_SYS_I2C_NOPROBES)
  622. int k, skip;
  623. unsigned int bus = GET_BUS_NUM;
  624. #endif /* NOPROBES */
  625. if (argc == 2)
  626. addr = simple_strtol(argv[1], 0, 16);
  627. puts ("Valid chip addresses:");
  628. for (j = 0; j < 128; j++) {
  629. if ((0 <= addr) && (j != addr))
  630. continue;
  631. #if defined(CONFIG_SYS_I2C_NOPROBES)
  632. skip = 0;
  633. for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
  634. if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
  635. skip = 1;
  636. break;
  637. }
  638. }
  639. if (skip)
  640. continue;
  641. #endif
  642. if (i2c_probe(j) == 0) {
  643. printf(" %02X", j);
  644. found++;
  645. }
  646. }
  647. putc ('\n');
  648. #if defined(CONFIG_SYS_I2C_NOPROBES)
  649. puts ("Excluded chip addresses:");
  650. for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
  651. if (COMPARE_BUS(bus,k))
  652. printf(" %02X", NO_PROBE_ADDR(k));
  653. }
  654. putc ('\n');
  655. #endif
  656. return (0 == found);
  657. }
  658. /**
  659. * do_i2c_loop() - Handle the "i2c loop" command-line command
  660. * @cmdtp: Command data struct pointer
  661. * @flag: Command flag
  662. * @argc: Command-line argument count
  663. * @argv: Array of command-line arguments
  664. *
  665. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  666. * on error.
  667. *
  668. * Syntax:
  669. * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
  670. * {length} - Number of bytes to read
  671. * {delay} - A DECIMAL number and defaults to 1000 uSec
  672. */
  673. static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  674. {
  675. u_char chip;
  676. ulong alen;
  677. uint addr;
  678. uint length;
  679. u_char bytes[16];
  680. int delay;
  681. if (argc < 3)
  682. return CMD_RET_USAGE;
  683. /*
  684. * Chip is always specified.
  685. */
  686. chip = simple_strtoul(argv[1], NULL, 16);
  687. /*
  688. * Address is always specified.
  689. */
  690. addr = simple_strtoul(argv[2], NULL, 16);
  691. alen = get_alen(argv[2]);
  692. if (alen > 3)
  693. return CMD_RET_USAGE;
  694. /*
  695. * Length is the number of objects, not number of bytes.
  696. */
  697. length = 1;
  698. length = simple_strtoul(argv[3], NULL, 16);
  699. if (length > sizeof(bytes))
  700. length = sizeof(bytes);
  701. /*
  702. * The delay time (uSec) is optional.
  703. */
  704. delay = 1000;
  705. if (argc > 3)
  706. delay = simple_strtoul(argv[4], NULL, 10);
  707. /*
  708. * Run the loop...
  709. */
  710. while (1) {
  711. if (i2c_read(chip, addr, alen, bytes, length) != 0)
  712. puts ("Error reading the chip.\n");
  713. udelay(delay);
  714. }
  715. /* NOTREACHED */
  716. return 0;
  717. }
  718. /*
  719. * The SDRAM command is separately configured because many
  720. * (most?) embedded boards don't use SDRAM DIMMs.
  721. *
  722. * FIXME: Document and probably move elsewhere!
  723. */
  724. #if defined(CONFIG_CMD_SDRAM)
  725. static void print_ddr2_tcyc (u_char const b)
  726. {
  727. printf ("%d.", (b >> 4) & 0x0F);
  728. switch (b & 0x0F) {
  729. case 0x0:
  730. case 0x1:
  731. case 0x2:
  732. case 0x3:
  733. case 0x4:
  734. case 0x5:
  735. case 0x6:
  736. case 0x7:
  737. case 0x8:
  738. case 0x9:
  739. printf ("%d ns\n", b & 0x0F);
  740. break;
  741. case 0xA:
  742. puts ("25 ns\n");
  743. break;
  744. case 0xB:
  745. puts ("33 ns\n");
  746. break;
  747. case 0xC:
  748. puts ("66 ns\n");
  749. break;
  750. case 0xD:
  751. puts ("75 ns\n");
  752. break;
  753. default:
  754. puts ("?? ns\n");
  755. break;
  756. }
  757. }
  758. static void decode_bits (u_char const b, char const *str[], int const do_once)
  759. {
  760. u_char mask;
  761. for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
  762. if (b & mask) {
  763. puts (*str);
  764. if (do_once)
  765. return;
  766. }
  767. }
  768. }
  769. /*
  770. * Syntax:
  771. * i2c sdram {i2c_chip}
  772. */
  773. static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  774. {
  775. enum { unknown, EDO, SDRAM, DDR2 } type;
  776. u_char chip;
  777. u_char data[128];
  778. u_char cksum;
  779. int j;
  780. static const char *decode_CAS_DDR2[] = {
  781. " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
  782. };
  783. static const char *decode_CAS_default[] = {
  784. " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
  785. };
  786. static const char *decode_CS_WE_default[] = {
  787. " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
  788. };
  789. static const char *decode_byte21_default[] = {
  790. " TBD (bit 7)\n",
  791. " Redundant row address\n",
  792. " Differential clock input\n",
  793. " Registerd DQMB inputs\n",
  794. " Buffered DQMB inputs\n",
  795. " On-card PLL\n",
  796. " Registered address/control lines\n",
  797. " Buffered address/control lines\n"
  798. };
  799. static const char *decode_byte22_DDR2[] = {
  800. " TBD (bit 7)\n",
  801. " TBD (bit 6)\n",
  802. " TBD (bit 5)\n",
  803. " TBD (bit 4)\n",
  804. " TBD (bit 3)\n",
  805. " Supports partial array self refresh\n",
  806. " Supports 50 ohm ODT\n",
  807. " Supports weak driver\n"
  808. };
  809. static const char *decode_row_density_DDR2[] = {
  810. "512 MiB", "256 MiB", "128 MiB", "16 GiB",
  811. "8 GiB", "4 GiB", "2 GiB", "1 GiB"
  812. };
  813. static const char *decode_row_density_default[] = {
  814. "512 MiB", "256 MiB", "128 MiB", "64 MiB",
  815. "32 MiB", "16 MiB", "8 MiB", "4 MiB"
  816. };
  817. if (argc < 2)
  818. return CMD_RET_USAGE;
  819. /*
  820. * Chip is always specified.
  821. */
  822. chip = simple_strtoul (argv[1], NULL, 16);
  823. if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
  824. puts ("No SDRAM Serial Presence Detect found.\n");
  825. return 1;
  826. }
  827. cksum = 0;
  828. for (j = 0; j < 63; j++) {
  829. cksum += data[j];
  830. }
  831. if (cksum != data[63]) {
  832. printf ("WARNING: Configuration data checksum failure:\n"
  833. " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
  834. }
  835. printf ("SPD data revision %d.%d\n",
  836. (data[62] >> 4) & 0x0F, data[62] & 0x0F);
  837. printf ("Bytes used 0x%02X\n", data[0]);
  838. printf ("Serial memory size 0x%02X\n", 1 << data[1]);
  839. puts ("Memory type ");
  840. switch (data[2]) {
  841. case 2:
  842. type = EDO;
  843. puts ("EDO\n");
  844. break;
  845. case 4:
  846. type = SDRAM;
  847. puts ("SDRAM\n");
  848. break;
  849. case 8:
  850. type = DDR2;
  851. puts ("DDR2\n");
  852. break;
  853. default:
  854. type = unknown;
  855. puts ("unknown\n");
  856. break;
  857. }
  858. puts ("Row address bits ");
  859. if ((data[3] & 0x00F0) == 0)
  860. printf ("%d\n", data[3] & 0x0F);
  861. else
  862. printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
  863. puts ("Column address bits ");
  864. if ((data[4] & 0x00F0) == 0)
  865. printf ("%d\n", data[4] & 0x0F);
  866. else
  867. printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
  868. switch (type) {
  869. case DDR2:
  870. printf ("Number of ranks %d\n",
  871. (data[5] & 0x07) + 1);
  872. break;
  873. default:
  874. printf ("Module rows %d\n", data[5]);
  875. break;
  876. }
  877. switch (type) {
  878. case DDR2:
  879. printf ("Module data width %d bits\n", data[6]);
  880. break;
  881. default:
  882. printf ("Module data width %d bits\n",
  883. (data[7] << 8) | data[6]);
  884. break;
  885. }
  886. puts ("Interface signal levels ");
  887. switch(data[8]) {
  888. case 0: puts ("TTL 5.0 V\n"); break;
  889. case 1: puts ("LVTTL\n"); break;
  890. case 2: puts ("HSTL 1.5 V\n"); break;
  891. case 3: puts ("SSTL 3.3 V\n"); break;
  892. case 4: puts ("SSTL 2.5 V\n"); break;
  893. case 5: puts ("SSTL 1.8 V\n"); break;
  894. default: puts ("unknown\n"); break;
  895. }
  896. switch (type) {
  897. case DDR2:
  898. printf ("SDRAM cycle time ");
  899. print_ddr2_tcyc (data[9]);
  900. break;
  901. default:
  902. printf ("SDRAM cycle time %d.%d ns\n",
  903. (data[9] >> 4) & 0x0F, data[9] & 0x0F);
  904. break;
  905. }
  906. switch (type) {
  907. case DDR2:
  908. printf ("SDRAM access time 0.%d%d ns\n",
  909. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  910. break;
  911. default:
  912. printf ("SDRAM access time %d.%d ns\n",
  913. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  914. break;
  915. }
  916. puts ("EDC configuration ");
  917. switch (data[11]) {
  918. case 0: puts ("None\n"); break;
  919. case 1: puts ("Parity\n"); break;
  920. case 2: puts ("ECC\n"); break;
  921. default: puts ("unknown\n"); break;
  922. }
  923. if ((data[12] & 0x80) == 0)
  924. puts ("No self refresh, rate ");
  925. else
  926. puts ("Self refresh, rate ");
  927. switch(data[12] & 0x7F) {
  928. case 0: puts ("15.625 us\n"); break;
  929. case 1: puts ("3.9 us\n"); break;
  930. case 2: puts ("7.8 us\n"); break;
  931. case 3: puts ("31.3 us\n"); break;
  932. case 4: puts ("62.5 us\n"); break;
  933. case 5: puts ("125 us\n"); break;
  934. default: puts ("unknown\n"); break;
  935. }
  936. switch (type) {
  937. case DDR2:
  938. printf ("SDRAM width (primary) %d\n", data[13]);
  939. break;
  940. default:
  941. printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
  942. if ((data[13] & 0x80) != 0) {
  943. printf (" (second bank) %d\n",
  944. 2 * (data[13] & 0x7F));
  945. }
  946. break;
  947. }
  948. switch (type) {
  949. case DDR2:
  950. if (data[14] != 0)
  951. printf ("EDC width %d\n", data[14]);
  952. break;
  953. default:
  954. if (data[14] != 0) {
  955. printf ("EDC width %d\n",
  956. data[14] & 0x7F);
  957. if ((data[14] & 0x80) != 0) {
  958. printf (" (second bank) %d\n",
  959. 2 * (data[14] & 0x7F));
  960. }
  961. }
  962. break;
  963. }
  964. if (DDR2 != type) {
  965. printf ("Min clock delay, back-to-back random column addresses "
  966. "%d\n", data[15]);
  967. }
  968. puts ("Burst length(s) ");
  969. if (data[16] & 0x80) puts (" Page");
  970. if (data[16] & 0x08) puts (" 8");
  971. if (data[16] & 0x04) puts (" 4");
  972. if (data[16] & 0x02) puts (" 2");
  973. if (data[16] & 0x01) puts (" 1");
  974. putc ('\n');
  975. printf ("Number of banks %d\n", data[17]);
  976. switch (type) {
  977. case DDR2:
  978. puts ("CAS latency(s) ");
  979. decode_bits (data[18], decode_CAS_DDR2, 0);
  980. putc ('\n');
  981. break;
  982. default:
  983. puts ("CAS latency(s) ");
  984. decode_bits (data[18], decode_CAS_default, 0);
  985. putc ('\n');
  986. break;
  987. }
  988. if (DDR2 != type) {
  989. puts ("CS latency(s) ");
  990. decode_bits (data[19], decode_CS_WE_default, 0);
  991. putc ('\n');
  992. }
  993. if (DDR2 != type) {
  994. puts ("WE latency(s) ");
  995. decode_bits (data[20], decode_CS_WE_default, 0);
  996. putc ('\n');
  997. }
  998. switch (type) {
  999. case DDR2:
  1000. puts ("Module attributes:\n");
  1001. if (data[21] & 0x80)
  1002. puts (" TBD (bit 7)\n");
  1003. if (data[21] & 0x40)
  1004. puts (" Analysis probe installed\n");
  1005. if (data[21] & 0x20)
  1006. puts (" TBD (bit 5)\n");
  1007. if (data[21] & 0x10)
  1008. puts (" FET switch external enable\n");
  1009. printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
  1010. if (data[20] & 0x11) {
  1011. printf (" %d active registers on DIMM\n",
  1012. (data[21] & 0x03) + 1);
  1013. }
  1014. break;
  1015. default:
  1016. puts ("Module attributes:\n");
  1017. if (!data[21])
  1018. puts (" (none)\n");
  1019. else
  1020. decode_bits (data[21], decode_byte21_default, 0);
  1021. break;
  1022. }
  1023. switch (type) {
  1024. case DDR2:
  1025. decode_bits (data[22], decode_byte22_DDR2, 0);
  1026. break;
  1027. default:
  1028. puts ("Device attributes:\n");
  1029. if (data[22] & 0x80) puts (" TBD (bit 7)\n");
  1030. if (data[22] & 0x40) puts (" TBD (bit 6)\n");
  1031. if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
  1032. else puts (" Upper Vcc tolerance 10%\n");
  1033. if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
  1034. else puts (" Lower Vcc tolerance 10%\n");
  1035. if (data[22] & 0x08) puts (" Supports write1/read burst\n");
  1036. if (data[22] & 0x04) puts (" Supports precharge all\n");
  1037. if (data[22] & 0x02) puts (" Supports auto precharge\n");
  1038. if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
  1039. break;
  1040. }
  1041. switch (type) {
  1042. case DDR2:
  1043. printf ("SDRAM cycle time (2nd highest CAS latency) ");
  1044. print_ddr2_tcyc (data[23]);
  1045. break;
  1046. default:
  1047. printf ("SDRAM cycle time (2nd highest CAS latency) %d."
  1048. "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
  1049. break;
  1050. }
  1051. switch (type) {
  1052. case DDR2:
  1053. printf ("SDRAM access from clock (2nd highest CAS latency) 0."
  1054. "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  1055. break;
  1056. default:
  1057. printf ("SDRAM access from clock (2nd highest CAS latency) %d."
  1058. "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  1059. break;
  1060. }
  1061. switch (type) {
  1062. case DDR2:
  1063. printf ("SDRAM cycle time (3rd highest CAS latency) ");
  1064. print_ddr2_tcyc (data[25]);
  1065. break;
  1066. default:
  1067. printf ("SDRAM cycle time (3rd highest CAS latency) %d."
  1068. "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
  1069. break;
  1070. }
  1071. switch (type) {
  1072. case DDR2:
  1073. printf ("SDRAM access from clock (3rd highest CAS latency) 0."
  1074. "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  1075. break;
  1076. default:
  1077. printf ("SDRAM access from clock (3rd highest CAS latency) %d."
  1078. "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  1079. break;
  1080. }
  1081. switch (type) {
  1082. case DDR2:
  1083. printf ("Minimum row precharge %d.%02d ns\n",
  1084. (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
  1085. break;
  1086. default:
  1087. printf ("Minimum row precharge %d ns\n", data[27]);
  1088. break;
  1089. }
  1090. switch (type) {
  1091. case DDR2:
  1092. printf ("Row active to row active min %d.%02d ns\n",
  1093. (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
  1094. break;
  1095. default:
  1096. printf ("Row active to row active min %d ns\n", data[28]);
  1097. break;
  1098. }
  1099. switch (type) {
  1100. case DDR2:
  1101. printf ("RAS to CAS delay min %d.%02d ns\n",
  1102. (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
  1103. break;
  1104. default:
  1105. printf ("RAS to CAS delay min %d ns\n", data[29]);
  1106. break;
  1107. }
  1108. printf ("Minimum RAS pulse width %d ns\n", data[30]);
  1109. switch (type) {
  1110. case DDR2:
  1111. puts ("Density of each row ");
  1112. decode_bits (data[31], decode_row_density_DDR2, 1);
  1113. putc ('\n');
  1114. break;
  1115. default:
  1116. puts ("Density of each row ");
  1117. decode_bits (data[31], decode_row_density_default, 1);
  1118. putc ('\n');
  1119. break;
  1120. }
  1121. switch (type) {
  1122. case DDR2:
  1123. puts ("Command and Address setup ");
  1124. if (data[32] >= 0xA0) {
  1125. printf ("1.%d%d ns\n",
  1126. ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
  1127. } else {
  1128. printf ("0.%d%d ns\n",
  1129. ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
  1130. }
  1131. break;
  1132. default:
  1133. printf ("Command and Address setup %c%d.%d ns\n",
  1134. (data[32] & 0x80) ? '-' : '+',
  1135. (data[32] >> 4) & 0x07, data[32] & 0x0F);
  1136. break;
  1137. }
  1138. switch (type) {
  1139. case DDR2:
  1140. puts ("Command and Address hold ");
  1141. if (data[33] >= 0xA0) {
  1142. printf ("1.%d%d ns\n",
  1143. ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
  1144. } else {
  1145. printf ("0.%d%d ns\n",
  1146. ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
  1147. }
  1148. break;
  1149. default:
  1150. printf ("Command and Address hold %c%d.%d ns\n",
  1151. (data[33] & 0x80) ? '-' : '+',
  1152. (data[33] >> 4) & 0x07, data[33] & 0x0F);
  1153. break;
  1154. }
  1155. switch (type) {
  1156. case DDR2:
  1157. printf ("Data signal input setup 0.%d%d ns\n",
  1158. (data[34] >> 4) & 0x0F, data[34] & 0x0F);
  1159. break;
  1160. default:
  1161. printf ("Data signal input setup %c%d.%d ns\n",
  1162. (data[34] & 0x80) ? '-' : '+',
  1163. (data[34] >> 4) & 0x07, data[34] & 0x0F);
  1164. break;
  1165. }
  1166. switch (type) {
  1167. case DDR2:
  1168. printf ("Data signal input hold 0.%d%d ns\n",
  1169. (data[35] >> 4) & 0x0F, data[35] & 0x0F);
  1170. break;
  1171. default:
  1172. printf ("Data signal input hold %c%d.%d ns\n",
  1173. (data[35] & 0x80) ? '-' : '+',
  1174. (data[35] >> 4) & 0x07, data[35] & 0x0F);
  1175. break;
  1176. }
  1177. puts ("Manufacturer's JEDEC ID ");
  1178. for (j = 64; j <= 71; j++)
  1179. printf ("%02X ", data[j]);
  1180. putc ('\n');
  1181. printf ("Manufacturing Location %02X\n", data[72]);
  1182. puts ("Manufacturer's Part Number ");
  1183. for (j = 73; j <= 90; j++)
  1184. printf ("%02X ", data[j]);
  1185. putc ('\n');
  1186. printf ("Revision Code %02X %02X\n", data[91], data[92]);
  1187. printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
  1188. puts ("Assembly Serial Number ");
  1189. for (j = 95; j <= 98; j++)
  1190. printf ("%02X ", data[j]);
  1191. putc ('\n');
  1192. if (DDR2 != type) {
  1193. printf ("Speed rating PC%d\n",
  1194. data[126] == 0x66 ? 66 : data[126]);
  1195. }
  1196. return 0;
  1197. }
  1198. #endif
  1199. /*
  1200. * Syntax:
  1201. * i2c edid {i2c_chip}
  1202. */
  1203. #if defined(CONFIG_I2C_EDID)
  1204. int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  1205. {
  1206. u_char chip;
  1207. struct edid1_info edid;
  1208. if (argc < 2) {
  1209. cmd_usage(cmdtp);
  1210. return 1;
  1211. }
  1212. chip = simple_strtoul(argv[1], NULL, 16);
  1213. if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
  1214. puts("Error reading EDID content.\n");
  1215. return 1;
  1216. }
  1217. if (edid_check_info(&edid)) {
  1218. puts("Content isn't valid EDID.\n");
  1219. return 1;
  1220. }
  1221. edid_print_info(&edid);
  1222. return 0;
  1223. }
  1224. #endif /* CONFIG_I2C_EDID */
  1225. /**
  1226. * do_i2c_show_bus() - Handle the "i2c bus" command-line command
  1227. * @cmdtp: Command data struct pointer
  1228. * @flag: Command flag
  1229. * @argc: Command-line argument count
  1230. * @argv: Array of command-line arguments
  1231. *
  1232. * Returns zero always.
  1233. */
  1234. #if defined(CONFIG_SYS_I2C)
  1235. int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1236. {
  1237. int i;
  1238. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  1239. int j;
  1240. #endif
  1241. if (argc == 1) {
  1242. /* show all busses */
  1243. for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
  1244. printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
  1245. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  1246. for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
  1247. if (i2c_bus[i].next_hop[j].chip == 0)
  1248. break;
  1249. printf("->%s@0x%2x:%d",
  1250. i2c_bus[i].next_hop[j].mux.name,
  1251. i2c_bus[i].next_hop[j].chip,
  1252. i2c_bus[i].next_hop[j].channel);
  1253. }
  1254. #endif
  1255. printf("\n");
  1256. }
  1257. } else {
  1258. /* show specific bus */
  1259. i = simple_strtoul(argv[1], NULL, 10);
  1260. if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
  1261. printf("Invalid bus %d\n", i);
  1262. return -1;
  1263. }
  1264. printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
  1265. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  1266. for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
  1267. if (i2c_bus[i].next_hop[j].chip == 0)
  1268. break;
  1269. printf("->%s@0x%2x:%d",
  1270. i2c_bus[i].next_hop[j].mux.name,
  1271. i2c_bus[i].next_hop[j].chip,
  1272. i2c_bus[i].next_hop[j].channel);
  1273. }
  1274. #endif
  1275. printf("\n");
  1276. }
  1277. return 0;
  1278. }
  1279. #endif
  1280. /**
  1281. * do_i2c_bus_num() - Handle the "i2c dev" command-line command
  1282. * @cmdtp: Command data struct pointer
  1283. * @flag: Command flag
  1284. * @argc: Command-line argument count
  1285. * @argv: Array of command-line arguments
  1286. *
  1287. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1288. * on error.
  1289. */
  1290. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  1291. int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1292. {
  1293. int ret = 0;
  1294. unsigned int bus_no;
  1295. if (argc == 1)
  1296. /* querying current setting */
  1297. printf("Current bus is %d\n", i2c_get_bus_num());
  1298. else {
  1299. bus_no = simple_strtoul(argv[1], NULL, 10);
  1300. #if defined(CONFIG_SYS_I2C)
  1301. if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
  1302. printf("Invalid bus %d\n", bus_no);
  1303. return -1;
  1304. }
  1305. #endif
  1306. printf("Setting bus to %d\n", bus_no);
  1307. ret = i2c_set_bus_num(bus_no);
  1308. if (ret)
  1309. printf("Failure changing bus number (%d)\n", ret);
  1310. }
  1311. return ret;
  1312. }
  1313. #endif /* defined(CONFIG_SYS_I2C) */
  1314. /**
  1315. * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
  1316. * @cmdtp: Command data struct pointer
  1317. * @flag: Command flag
  1318. * @argc: Command-line argument count
  1319. * @argv: Array of command-line arguments
  1320. *
  1321. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1322. * on error.
  1323. */
  1324. static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1325. {
  1326. int speed, ret=0;
  1327. if (argc == 1)
  1328. /* querying current speed */
  1329. printf("Current bus speed=%d\n", i2c_get_bus_speed());
  1330. else {
  1331. speed = simple_strtoul(argv[1], NULL, 10);
  1332. printf("Setting bus speed to %d Hz\n", speed);
  1333. ret = i2c_set_bus_speed(speed);
  1334. if (ret)
  1335. printf("Failure changing bus speed (%d)\n", ret);
  1336. }
  1337. return ret;
  1338. }
  1339. /**
  1340. * do_i2c_mm() - Handle the "i2c mm" command-line command
  1341. * @cmdtp: Command data struct pointer
  1342. * @flag: Command flag
  1343. * @argc: Command-line argument count
  1344. * @argv: Array of command-line arguments
  1345. *
  1346. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1347. * on error.
  1348. */
  1349. static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1350. {
  1351. return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
  1352. }
  1353. /**
  1354. * do_i2c_nm() - Handle the "i2c nm" command-line command
  1355. * @cmdtp: Command data struct pointer
  1356. * @flag: Command flag
  1357. * @argc: Command-line argument count
  1358. * @argv: Array of command-line arguments
  1359. *
  1360. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1361. * on error.
  1362. */
  1363. static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1364. {
  1365. return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
  1366. }
  1367. /**
  1368. * do_i2c_reset() - Handle the "i2c reset" command-line command
  1369. * @cmdtp: Command data struct pointer
  1370. * @flag: Command flag
  1371. * @argc: Command-line argument count
  1372. * @argv: Array of command-line arguments
  1373. *
  1374. * Returns zero always.
  1375. */
  1376. static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1377. {
  1378. #if defined(CONFIG_SYS_I2C)
  1379. i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
  1380. #else
  1381. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  1382. #endif
  1383. return 0;
  1384. }
  1385. static cmd_tbl_t cmd_i2c_sub[] = {
  1386. #if defined(CONFIG_SYS_I2C)
  1387. U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
  1388. #endif
  1389. U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
  1390. #if defined(CONFIG_SYS_I2C) || \
  1391. defined(CONFIG_I2C_MULTI_BUS)
  1392. U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
  1393. #endif /* CONFIG_I2C_MULTI_BUS */
  1394. #if defined(CONFIG_I2C_EDID)
  1395. U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
  1396. #endif /* CONFIG_I2C_EDID */
  1397. U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
  1398. U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
  1399. U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
  1400. U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
  1401. U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
  1402. U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
  1403. U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
  1404. U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
  1405. U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
  1406. #if defined(CONFIG_CMD_SDRAM)
  1407. U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
  1408. #endif
  1409. U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
  1410. };
  1411. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  1412. void i2c_reloc(void) {
  1413. fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
  1414. }
  1415. #endif
  1416. /**
  1417. * do_i2c() - Handle the "i2c" command-line command
  1418. * @cmdtp: Command data struct pointer
  1419. * @flag: Command flag
  1420. * @argc: Command-line argument count
  1421. * @argv: Array of command-line arguments
  1422. *
  1423. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1424. * on error.
  1425. */
  1426. static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1427. {
  1428. cmd_tbl_t *c;
  1429. if (argc < 2)
  1430. return CMD_RET_USAGE;
  1431. /* Strip off leading 'i2c' command argument */
  1432. argc--;
  1433. argv++;
  1434. c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
  1435. if (c)
  1436. return c->cmd(cmdtp, flag, argc, argv);
  1437. else
  1438. return CMD_RET_USAGE;
  1439. }
  1440. /***************************************************/
  1441. #ifdef CONFIG_SYS_LONGHELP
  1442. static char i2c_help_text[] =
  1443. #if defined(CONFIG_SYS_I2C)
  1444. "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
  1445. #endif
  1446. "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
  1447. #if defined(CONFIG_SYS_I2C) || \
  1448. defined(CONFIG_I2C_MULTI_BUS)
  1449. "i2c dev [dev] - show or set current I2C bus\n"
  1450. #endif /* CONFIG_I2C_MULTI_BUS */
  1451. #if defined(CONFIG_I2C_EDID)
  1452. "i2c edid chip - print EDID configuration information\n"
  1453. #endif /* CONFIG_I2C_EDID */
  1454. "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
  1455. "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
  1456. "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
  1457. "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
  1458. "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
  1459. "i2c probe [address] - test for and show device(s) on the I2C bus\n"
  1460. "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
  1461. "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
  1462. "i2c reset - re-init the I2C Controller\n"
  1463. #if defined(CONFIG_CMD_SDRAM)
  1464. "i2c sdram chip - print SDRAM configuration information\n"
  1465. #endif
  1466. "i2c speed [speed] - show or set I2C bus speed";
  1467. #endif
  1468. U_BOOT_CMD(
  1469. i2c, 6, 1, do_i2c,
  1470. "I2C sub-system",
  1471. i2c_help_text
  1472. );