cmd_i2c.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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. bootretry_reset_cmd_timeout(); /* got a good command to get here */
  509. /*
  510. * We use the last specified parameters, unless new ones are
  511. * entered.
  512. */
  513. chip = i2c_mm_last_chip;
  514. addr = i2c_mm_last_addr;
  515. alen = i2c_mm_last_alen;
  516. if ((flag & CMD_FLAG_REPEAT) == 0) {
  517. /*
  518. * New command specified. Check for a size specification.
  519. * Defaults to byte if no or incorrect specification.
  520. */
  521. size = cmd_get_data_size(argv[0], 1);
  522. /*
  523. * Chip is always specified.
  524. */
  525. chip = simple_strtoul(argv[1], NULL, 16);
  526. /*
  527. * Address is always specified.
  528. */
  529. addr = simple_strtoul(argv[2], NULL, 16);
  530. alen = get_alen(argv[2]);
  531. if (alen > 3)
  532. return CMD_RET_USAGE;
  533. }
  534. /*
  535. * Print the address, followed by value. Then accept input for
  536. * the next value. A non-converted value exits.
  537. */
  538. do {
  539. printf("%08lx:", addr);
  540. if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
  541. puts ("\nError reading the chip,\n");
  542. else {
  543. data = cpu_to_be32(data);
  544. if (size == 1)
  545. printf(" %02lx", (data >> 24) & 0x000000FF);
  546. else if (size == 2)
  547. printf(" %04lx", (data >> 16) & 0x0000FFFF);
  548. else
  549. printf(" %08lx", data);
  550. }
  551. nbytes = cli_readline(" ? ");
  552. if (nbytes == 0) {
  553. /*
  554. * <CR> pressed as only input, don't modify current
  555. * location and move to next.
  556. */
  557. if (incrflag)
  558. addr += size;
  559. nbytes = size;
  560. /* good enough to not time out */
  561. bootretry_reset_cmd_timeout();
  562. }
  563. #ifdef CONFIG_BOOT_RETRY_TIME
  564. else if (nbytes == -2)
  565. break; /* timed out, exit the command */
  566. #endif
  567. else {
  568. char *endp;
  569. data = simple_strtoul(console_buffer, &endp, 16);
  570. if (size == 1)
  571. data = data << 24;
  572. else if (size == 2)
  573. data = data << 16;
  574. data = be32_to_cpu(data);
  575. nbytes = endp - console_buffer;
  576. if (nbytes) {
  577. /*
  578. * good enough to not time out
  579. */
  580. bootretry_reset_cmd_timeout();
  581. if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
  582. puts ("Error writing the chip.\n");
  583. #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  584. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  585. #endif
  586. if (incrflag)
  587. addr += size;
  588. }
  589. }
  590. } while (nbytes);
  591. i2c_mm_last_chip = chip;
  592. i2c_mm_last_addr = addr;
  593. i2c_mm_last_alen = alen;
  594. return 0;
  595. }
  596. /**
  597. * do_i2c_probe() - Handle the "i2c probe" command-line command
  598. * @cmdtp: Command data struct pointer
  599. * @flag: Command flag
  600. * @argc: Command-line argument count
  601. * @argv: Array of command-line arguments
  602. *
  603. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  604. * on error.
  605. *
  606. * Syntax:
  607. * i2c probe {addr}
  608. *
  609. * Returns zero (success) if one or more I2C devices was found
  610. */
  611. static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  612. {
  613. int j;
  614. int addr = -1;
  615. int found = 0;
  616. #if defined(CONFIG_SYS_I2C_NOPROBES)
  617. int k, skip;
  618. unsigned int bus = GET_BUS_NUM;
  619. #endif /* NOPROBES */
  620. if (argc == 2)
  621. addr = simple_strtol(argv[1], 0, 16);
  622. puts ("Valid chip addresses:");
  623. for (j = 0; j < 128; j++) {
  624. if ((0 <= addr) && (j != addr))
  625. continue;
  626. #if defined(CONFIG_SYS_I2C_NOPROBES)
  627. skip = 0;
  628. for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
  629. if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
  630. skip = 1;
  631. break;
  632. }
  633. }
  634. if (skip)
  635. continue;
  636. #endif
  637. if (i2c_probe(j) == 0) {
  638. printf(" %02X", j);
  639. found++;
  640. }
  641. }
  642. putc ('\n');
  643. #if defined(CONFIG_SYS_I2C_NOPROBES)
  644. puts ("Excluded chip addresses:");
  645. for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
  646. if (COMPARE_BUS(bus,k))
  647. printf(" %02X", NO_PROBE_ADDR(k));
  648. }
  649. putc ('\n');
  650. #endif
  651. return (0 == found);
  652. }
  653. /**
  654. * do_i2c_loop() - Handle the "i2c loop" command-line command
  655. * @cmdtp: Command data struct pointer
  656. * @flag: Command flag
  657. * @argc: Command-line argument count
  658. * @argv: Array of command-line arguments
  659. *
  660. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  661. * on error.
  662. *
  663. * Syntax:
  664. * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
  665. * {length} - Number of bytes to read
  666. * {delay} - A DECIMAL number and defaults to 1000 uSec
  667. */
  668. static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  669. {
  670. u_char chip;
  671. ulong alen;
  672. uint addr;
  673. uint length;
  674. u_char bytes[16];
  675. int delay;
  676. if (argc < 3)
  677. return CMD_RET_USAGE;
  678. /*
  679. * Chip is always specified.
  680. */
  681. chip = simple_strtoul(argv[1], NULL, 16);
  682. /*
  683. * Address is always specified.
  684. */
  685. addr = simple_strtoul(argv[2], NULL, 16);
  686. alen = get_alen(argv[2]);
  687. if (alen > 3)
  688. return CMD_RET_USAGE;
  689. /*
  690. * Length is the number of objects, not number of bytes.
  691. */
  692. length = 1;
  693. length = simple_strtoul(argv[3], NULL, 16);
  694. if (length > sizeof(bytes))
  695. length = sizeof(bytes);
  696. /*
  697. * The delay time (uSec) is optional.
  698. */
  699. delay = 1000;
  700. if (argc > 3)
  701. delay = simple_strtoul(argv[4], NULL, 10);
  702. /*
  703. * Run the loop...
  704. */
  705. while (1) {
  706. if (i2c_read(chip, addr, alen, bytes, length) != 0)
  707. puts ("Error reading the chip.\n");
  708. udelay(delay);
  709. }
  710. /* NOTREACHED */
  711. return 0;
  712. }
  713. /*
  714. * The SDRAM command is separately configured because many
  715. * (most?) embedded boards don't use SDRAM DIMMs.
  716. *
  717. * FIXME: Document and probably move elsewhere!
  718. */
  719. #if defined(CONFIG_CMD_SDRAM)
  720. static void print_ddr2_tcyc (u_char const b)
  721. {
  722. printf ("%d.", (b >> 4) & 0x0F);
  723. switch (b & 0x0F) {
  724. case 0x0:
  725. case 0x1:
  726. case 0x2:
  727. case 0x3:
  728. case 0x4:
  729. case 0x5:
  730. case 0x6:
  731. case 0x7:
  732. case 0x8:
  733. case 0x9:
  734. printf ("%d ns\n", b & 0x0F);
  735. break;
  736. case 0xA:
  737. puts ("25 ns\n");
  738. break;
  739. case 0xB:
  740. puts ("33 ns\n");
  741. break;
  742. case 0xC:
  743. puts ("66 ns\n");
  744. break;
  745. case 0xD:
  746. puts ("75 ns\n");
  747. break;
  748. default:
  749. puts ("?? ns\n");
  750. break;
  751. }
  752. }
  753. static void decode_bits (u_char const b, char const *str[], int const do_once)
  754. {
  755. u_char mask;
  756. for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
  757. if (b & mask) {
  758. puts (*str);
  759. if (do_once)
  760. return;
  761. }
  762. }
  763. }
  764. /*
  765. * Syntax:
  766. * i2c sdram {i2c_chip}
  767. */
  768. static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  769. {
  770. enum { unknown, EDO, SDRAM, DDR2 } type;
  771. u_char chip;
  772. u_char data[128];
  773. u_char cksum;
  774. int j;
  775. static const char *decode_CAS_DDR2[] = {
  776. " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
  777. };
  778. static const char *decode_CAS_default[] = {
  779. " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
  780. };
  781. static const char *decode_CS_WE_default[] = {
  782. " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
  783. };
  784. static const char *decode_byte21_default[] = {
  785. " TBD (bit 7)\n",
  786. " Redundant row address\n",
  787. " Differential clock input\n",
  788. " Registerd DQMB inputs\n",
  789. " Buffered DQMB inputs\n",
  790. " On-card PLL\n",
  791. " Registered address/control lines\n",
  792. " Buffered address/control lines\n"
  793. };
  794. static const char *decode_byte22_DDR2[] = {
  795. " TBD (bit 7)\n",
  796. " TBD (bit 6)\n",
  797. " TBD (bit 5)\n",
  798. " TBD (bit 4)\n",
  799. " TBD (bit 3)\n",
  800. " Supports partial array self refresh\n",
  801. " Supports 50 ohm ODT\n",
  802. " Supports weak driver\n"
  803. };
  804. static const char *decode_row_density_DDR2[] = {
  805. "512 MiB", "256 MiB", "128 MiB", "16 GiB",
  806. "8 GiB", "4 GiB", "2 GiB", "1 GiB"
  807. };
  808. static const char *decode_row_density_default[] = {
  809. "512 MiB", "256 MiB", "128 MiB", "64 MiB",
  810. "32 MiB", "16 MiB", "8 MiB", "4 MiB"
  811. };
  812. if (argc < 2)
  813. return CMD_RET_USAGE;
  814. /*
  815. * Chip is always specified.
  816. */
  817. chip = simple_strtoul (argv[1], NULL, 16);
  818. if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
  819. puts ("No SDRAM Serial Presence Detect found.\n");
  820. return 1;
  821. }
  822. cksum = 0;
  823. for (j = 0; j < 63; j++) {
  824. cksum += data[j];
  825. }
  826. if (cksum != data[63]) {
  827. printf ("WARNING: Configuration data checksum failure:\n"
  828. " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
  829. }
  830. printf ("SPD data revision %d.%d\n",
  831. (data[62] >> 4) & 0x0F, data[62] & 0x0F);
  832. printf ("Bytes used 0x%02X\n", data[0]);
  833. printf ("Serial memory size 0x%02X\n", 1 << data[1]);
  834. puts ("Memory type ");
  835. switch (data[2]) {
  836. case 2:
  837. type = EDO;
  838. puts ("EDO\n");
  839. break;
  840. case 4:
  841. type = SDRAM;
  842. puts ("SDRAM\n");
  843. break;
  844. case 8:
  845. type = DDR2;
  846. puts ("DDR2\n");
  847. break;
  848. default:
  849. type = unknown;
  850. puts ("unknown\n");
  851. break;
  852. }
  853. puts ("Row address bits ");
  854. if ((data[3] & 0x00F0) == 0)
  855. printf ("%d\n", data[3] & 0x0F);
  856. else
  857. printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
  858. puts ("Column address bits ");
  859. if ((data[4] & 0x00F0) == 0)
  860. printf ("%d\n", data[4] & 0x0F);
  861. else
  862. printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
  863. switch (type) {
  864. case DDR2:
  865. printf ("Number of ranks %d\n",
  866. (data[5] & 0x07) + 1);
  867. break;
  868. default:
  869. printf ("Module rows %d\n", data[5]);
  870. break;
  871. }
  872. switch (type) {
  873. case DDR2:
  874. printf ("Module data width %d bits\n", data[6]);
  875. break;
  876. default:
  877. printf ("Module data width %d bits\n",
  878. (data[7] << 8) | data[6]);
  879. break;
  880. }
  881. puts ("Interface signal levels ");
  882. switch(data[8]) {
  883. case 0: puts ("TTL 5.0 V\n"); break;
  884. case 1: puts ("LVTTL\n"); break;
  885. case 2: puts ("HSTL 1.5 V\n"); break;
  886. case 3: puts ("SSTL 3.3 V\n"); break;
  887. case 4: puts ("SSTL 2.5 V\n"); break;
  888. case 5: puts ("SSTL 1.8 V\n"); break;
  889. default: puts ("unknown\n"); break;
  890. }
  891. switch (type) {
  892. case DDR2:
  893. printf ("SDRAM cycle time ");
  894. print_ddr2_tcyc (data[9]);
  895. break;
  896. default:
  897. printf ("SDRAM cycle time %d.%d ns\n",
  898. (data[9] >> 4) & 0x0F, data[9] & 0x0F);
  899. break;
  900. }
  901. switch (type) {
  902. case DDR2:
  903. printf ("SDRAM access time 0.%d%d ns\n",
  904. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  905. break;
  906. default:
  907. printf ("SDRAM access time %d.%d ns\n",
  908. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  909. break;
  910. }
  911. puts ("EDC configuration ");
  912. switch (data[11]) {
  913. case 0: puts ("None\n"); break;
  914. case 1: puts ("Parity\n"); break;
  915. case 2: puts ("ECC\n"); break;
  916. default: puts ("unknown\n"); break;
  917. }
  918. if ((data[12] & 0x80) == 0)
  919. puts ("No self refresh, rate ");
  920. else
  921. puts ("Self refresh, rate ");
  922. switch(data[12] & 0x7F) {
  923. case 0: puts ("15.625 us\n"); break;
  924. case 1: puts ("3.9 us\n"); break;
  925. case 2: puts ("7.8 us\n"); break;
  926. case 3: puts ("31.3 us\n"); break;
  927. case 4: puts ("62.5 us\n"); break;
  928. case 5: puts ("125 us\n"); break;
  929. default: puts ("unknown\n"); break;
  930. }
  931. switch (type) {
  932. case DDR2:
  933. printf ("SDRAM width (primary) %d\n", data[13]);
  934. break;
  935. default:
  936. printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
  937. if ((data[13] & 0x80) != 0) {
  938. printf (" (second bank) %d\n",
  939. 2 * (data[13] & 0x7F));
  940. }
  941. break;
  942. }
  943. switch (type) {
  944. case DDR2:
  945. if (data[14] != 0)
  946. printf ("EDC width %d\n", data[14]);
  947. break;
  948. default:
  949. if (data[14] != 0) {
  950. printf ("EDC width %d\n",
  951. data[14] & 0x7F);
  952. if ((data[14] & 0x80) != 0) {
  953. printf (" (second bank) %d\n",
  954. 2 * (data[14] & 0x7F));
  955. }
  956. }
  957. break;
  958. }
  959. if (DDR2 != type) {
  960. printf ("Min clock delay, back-to-back random column addresses "
  961. "%d\n", data[15]);
  962. }
  963. puts ("Burst length(s) ");
  964. if (data[16] & 0x80) puts (" Page");
  965. if (data[16] & 0x08) puts (" 8");
  966. if (data[16] & 0x04) puts (" 4");
  967. if (data[16] & 0x02) puts (" 2");
  968. if (data[16] & 0x01) puts (" 1");
  969. putc ('\n');
  970. printf ("Number of banks %d\n", data[17]);
  971. switch (type) {
  972. case DDR2:
  973. puts ("CAS latency(s) ");
  974. decode_bits (data[18], decode_CAS_DDR2, 0);
  975. putc ('\n');
  976. break;
  977. default:
  978. puts ("CAS latency(s) ");
  979. decode_bits (data[18], decode_CAS_default, 0);
  980. putc ('\n');
  981. break;
  982. }
  983. if (DDR2 != type) {
  984. puts ("CS latency(s) ");
  985. decode_bits (data[19], decode_CS_WE_default, 0);
  986. putc ('\n');
  987. }
  988. if (DDR2 != type) {
  989. puts ("WE latency(s) ");
  990. decode_bits (data[20], decode_CS_WE_default, 0);
  991. putc ('\n');
  992. }
  993. switch (type) {
  994. case DDR2:
  995. puts ("Module attributes:\n");
  996. if (data[21] & 0x80)
  997. puts (" TBD (bit 7)\n");
  998. if (data[21] & 0x40)
  999. puts (" Analysis probe installed\n");
  1000. if (data[21] & 0x20)
  1001. puts (" TBD (bit 5)\n");
  1002. if (data[21] & 0x10)
  1003. puts (" FET switch external enable\n");
  1004. printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
  1005. if (data[20] & 0x11) {
  1006. printf (" %d active registers on DIMM\n",
  1007. (data[21] & 0x03) + 1);
  1008. }
  1009. break;
  1010. default:
  1011. puts ("Module attributes:\n");
  1012. if (!data[21])
  1013. puts (" (none)\n");
  1014. else
  1015. decode_bits (data[21], decode_byte21_default, 0);
  1016. break;
  1017. }
  1018. switch (type) {
  1019. case DDR2:
  1020. decode_bits (data[22], decode_byte22_DDR2, 0);
  1021. break;
  1022. default:
  1023. puts ("Device attributes:\n");
  1024. if (data[22] & 0x80) puts (" TBD (bit 7)\n");
  1025. if (data[22] & 0x40) puts (" TBD (bit 6)\n");
  1026. if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
  1027. else puts (" Upper Vcc tolerance 10%\n");
  1028. if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
  1029. else puts (" Lower Vcc tolerance 10%\n");
  1030. if (data[22] & 0x08) puts (" Supports write1/read burst\n");
  1031. if (data[22] & 0x04) puts (" Supports precharge all\n");
  1032. if (data[22] & 0x02) puts (" Supports auto precharge\n");
  1033. if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
  1034. break;
  1035. }
  1036. switch (type) {
  1037. case DDR2:
  1038. printf ("SDRAM cycle time (2nd highest CAS latency) ");
  1039. print_ddr2_tcyc (data[23]);
  1040. break;
  1041. default:
  1042. printf ("SDRAM cycle time (2nd highest CAS latency) %d."
  1043. "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
  1044. break;
  1045. }
  1046. switch (type) {
  1047. case DDR2:
  1048. printf ("SDRAM access from clock (2nd highest CAS latency) 0."
  1049. "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  1050. break;
  1051. default:
  1052. printf ("SDRAM access from clock (2nd highest CAS latency) %d."
  1053. "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  1054. break;
  1055. }
  1056. switch (type) {
  1057. case DDR2:
  1058. printf ("SDRAM cycle time (3rd highest CAS latency) ");
  1059. print_ddr2_tcyc (data[25]);
  1060. break;
  1061. default:
  1062. printf ("SDRAM cycle time (3rd highest CAS latency) %d."
  1063. "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
  1064. break;
  1065. }
  1066. switch (type) {
  1067. case DDR2:
  1068. printf ("SDRAM access from clock (3rd highest CAS latency) 0."
  1069. "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  1070. break;
  1071. default:
  1072. printf ("SDRAM access from clock (3rd highest CAS latency) %d."
  1073. "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  1074. break;
  1075. }
  1076. switch (type) {
  1077. case DDR2:
  1078. printf ("Minimum row precharge %d.%02d ns\n",
  1079. (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
  1080. break;
  1081. default:
  1082. printf ("Minimum row precharge %d ns\n", data[27]);
  1083. break;
  1084. }
  1085. switch (type) {
  1086. case DDR2:
  1087. printf ("Row active to row active min %d.%02d ns\n",
  1088. (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
  1089. break;
  1090. default:
  1091. printf ("Row active to row active min %d ns\n", data[28]);
  1092. break;
  1093. }
  1094. switch (type) {
  1095. case DDR2:
  1096. printf ("RAS to CAS delay min %d.%02d ns\n",
  1097. (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
  1098. break;
  1099. default:
  1100. printf ("RAS to CAS delay min %d ns\n", data[29]);
  1101. break;
  1102. }
  1103. printf ("Minimum RAS pulse width %d ns\n", data[30]);
  1104. switch (type) {
  1105. case DDR2:
  1106. puts ("Density of each row ");
  1107. decode_bits (data[31], decode_row_density_DDR2, 1);
  1108. putc ('\n');
  1109. break;
  1110. default:
  1111. puts ("Density of each row ");
  1112. decode_bits (data[31], decode_row_density_default, 1);
  1113. putc ('\n');
  1114. break;
  1115. }
  1116. switch (type) {
  1117. case DDR2:
  1118. puts ("Command and Address setup ");
  1119. if (data[32] >= 0xA0) {
  1120. printf ("1.%d%d ns\n",
  1121. ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
  1122. } else {
  1123. printf ("0.%d%d ns\n",
  1124. ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
  1125. }
  1126. break;
  1127. default:
  1128. printf ("Command and Address setup %c%d.%d ns\n",
  1129. (data[32] & 0x80) ? '-' : '+',
  1130. (data[32] >> 4) & 0x07, data[32] & 0x0F);
  1131. break;
  1132. }
  1133. switch (type) {
  1134. case DDR2:
  1135. puts ("Command and Address hold ");
  1136. if (data[33] >= 0xA0) {
  1137. printf ("1.%d%d ns\n",
  1138. ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
  1139. } else {
  1140. printf ("0.%d%d ns\n",
  1141. ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
  1142. }
  1143. break;
  1144. default:
  1145. printf ("Command and Address hold %c%d.%d ns\n",
  1146. (data[33] & 0x80) ? '-' : '+',
  1147. (data[33] >> 4) & 0x07, data[33] & 0x0F);
  1148. break;
  1149. }
  1150. switch (type) {
  1151. case DDR2:
  1152. printf ("Data signal input setup 0.%d%d ns\n",
  1153. (data[34] >> 4) & 0x0F, data[34] & 0x0F);
  1154. break;
  1155. default:
  1156. printf ("Data signal input setup %c%d.%d ns\n",
  1157. (data[34] & 0x80) ? '-' : '+',
  1158. (data[34] >> 4) & 0x07, data[34] & 0x0F);
  1159. break;
  1160. }
  1161. switch (type) {
  1162. case DDR2:
  1163. printf ("Data signal input hold 0.%d%d ns\n",
  1164. (data[35] >> 4) & 0x0F, data[35] & 0x0F);
  1165. break;
  1166. default:
  1167. printf ("Data signal input hold %c%d.%d ns\n",
  1168. (data[35] & 0x80) ? '-' : '+',
  1169. (data[35] >> 4) & 0x07, data[35] & 0x0F);
  1170. break;
  1171. }
  1172. puts ("Manufacturer's JEDEC ID ");
  1173. for (j = 64; j <= 71; j++)
  1174. printf ("%02X ", data[j]);
  1175. putc ('\n');
  1176. printf ("Manufacturing Location %02X\n", data[72]);
  1177. puts ("Manufacturer's Part Number ");
  1178. for (j = 73; j <= 90; j++)
  1179. printf ("%02X ", data[j]);
  1180. putc ('\n');
  1181. printf ("Revision Code %02X %02X\n", data[91], data[92]);
  1182. printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
  1183. puts ("Assembly Serial Number ");
  1184. for (j = 95; j <= 98; j++)
  1185. printf ("%02X ", data[j]);
  1186. putc ('\n');
  1187. if (DDR2 != type) {
  1188. printf ("Speed rating PC%d\n",
  1189. data[126] == 0x66 ? 66 : data[126]);
  1190. }
  1191. return 0;
  1192. }
  1193. #endif
  1194. /*
  1195. * Syntax:
  1196. * i2c edid {i2c_chip}
  1197. */
  1198. #if defined(CONFIG_I2C_EDID)
  1199. int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  1200. {
  1201. u_char chip;
  1202. struct edid1_info edid;
  1203. if (argc < 2) {
  1204. cmd_usage(cmdtp);
  1205. return 1;
  1206. }
  1207. chip = simple_strtoul(argv[1], NULL, 16);
  1208. if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
  1209. puts("Error reading EDID content.\n");
  1210. return 1;
  1211. }
  1212. if (edid_check_info(&edid)) {
  1213. puts("Content isn't valid EDID.\n");
  1214. return 1;
  1215. }
  1216. edid_print_info(&edid);
  1217. return 0;
  1218. }
  1219. #endif /* CONFIG_I2C_EDID */
  1220. /**
  1221. * do_i2c_show_bus() - Handle the "i2c bus" command-line command
  1222. * @cmdtp: Command data struct pointer
  1223. * @flag: Command flag
  1224. * @argc: Command-line argument count
  1225. * @argv: Array of command-line arguments
  1226. *
  1227. * Returns zero always.
  1228. */
  1229. #if defined(CONFIG_SYS_I2C)
  1230. static int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc,
  1231. char * const argv[])
  1232. {
  1233. int i;
  1234. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  1235. int j;
  1236. #endif
  1237. if (argc == 1) {
  1238. /* show all busses */
  1239. for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
  1240. printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
  1241. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  1242. for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
  1243. if (i2c_bus[i].next_hop[j].chip == 0)
  1244. break;
  1245. printf("->%s@0x%2x:%d",
  1246. i2c_bus[i].next_hop[j].mux.name,
  1247. i2c_bus[i].next_hop[j].chip,
  1248. i2c_bus[i].next_hop[j].channel);
  1249. }
  1250. #endif
  1251. printf("\n");
  1252. }
  1253. } else {
  1254. /* show specific bus */
  1255. i = simple_strtoul(argv[1], NULL, 10);
  1256. if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
  1257. printf("Invalid bus %d\n", i);
  1258. return -1;
  1259. }
  1260. printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
  1261. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  1262. for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
  1263. if (i2c_bus[i].next_hop[j].chip == 0)
  1264. break;
  1265. printf("->%s@0x%2x:%d",
  1266. i2c_bus[i].next_hop[j].mux.name,
  1267. i2c_bus[i].next_hop[j].chip,
  1268. i2c_bus[i].next_hop[j].channel);
  1269. }
  1270. #endif
  1271. printf("\n");
  1272. }
  1273. return 0;
  1274. }
  1275. #endif
  1276. /**
  1277. * do_i2c_bus_num() - Handle the "i2c dev" command-line command
  1278. * @cmdtp: Command data struct pointer
  1279. * @flag: Command flag
  1280. * @argc: Command-line argument count
  1281. * @argv: Array of command-line arguments
  1282. *
  1283. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1284. * on error.
  1285. */
  1286. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  1287. static int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc,
  1288. char * const argv[])
  1289. {
  1290. int ret = 0;
  1291. unsigned int bus_no;
  1292. if (argc == 1)
  1293. /* querying current setting */
  1294. printf("Current bus is %d\n", i2c_get_bus_num());
  1295. else {
  1296. bus_no = simple_strtoul(argv[1], NULL, 10);
  1297. #if defined(CONFIG_SYS_I2C)
  1298. if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
  1299. printf("Invalid bus %d\n", bus_no);
  1300. return -1;
  1301. }
  1302. #endif
  1303. printf("Setting bus to %d\n", bus_no);
  1304. ret = i2c_set_bus_num(bus_no);
  1305. if (ret)
  1306. printf("Failure changing bus number (%d)\n", ret);
  1307. }
  1308. return ret;
  1309. }
  1310. #endif /* defined(CONFIG_SYS_I2C) */
  1311. /**
  1312. * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
  1313. * @cmdtp: Command data struct pointer
  1314. * @flag: Command flag
  1315. * @argc: Command-line argument count
  1316. * @argv: Array of command-line arguments
  1317. *
  1318. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1319. * on error.
  1320. */
  1321. static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1322. {
  1323. int speed, ret=0;
  1324. if (argc == 1)
  1325. /* querying current speed */
  1326. printf("Current bus speed=%d\n", i2c_get_bus_speed());
  1327. else {
  1328. speed = simple_strtoul(argv[1], NULL, 10);
  1329. printf("Setting bus speed to %d Hz\n", speed);
  1330. ret = i2c_set_bus_speed(speed);
  1331. if (ret)
  1332. printf("Failure changing bus speed (%d)\n", ret);
  1333. }
  1334. return ret;
  1335. }
  1336. /**
  1337. * do_i2c_mm() - Handle the "i2c mm" command-line command
  1338. * @cmdtp: Command data struct pointer
  1339. * @flag: Command flag
  1340. * @argc: Command-line argument count
  1341. * @argv: Array of command-line arguments
  1342. *
  1343. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1344. * on error.
  1345. */
  1346. static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1347. {
  1348. return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
  1349. }
  1350. /**
  1351. * do_i2c_nm() - Handle the "i2c nm" command-line command
  1352. * @cmdtp: Command data struct pointer
  1353. * @flag: Command flag
  1354. * @argc: Command-line argument count
  1355. * @argv: Array of command-line arguments
  1356. *
  1357. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1358. * on error.
  1359. */
  1360. static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1361. {
  1362. return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
  1363. }
  1364. /**
  1365. * do_i2c_reset() - Handle the "i2c reset" command-line command
  1366. * @cmdtp: Command data struct pointer
  1367. * @flag: Command flag
  1368. * @argc: Command-line argument count
  1369. * @argv: Array of command-line arguments
  1370. *
  1371. * Returns zero always.
  1372. */
  1373. static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1374. {
  1375. #if defined(CONFIG_SYS_I2C)
  1376. i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
  1377. #else
  1378. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  1379. #endif
  1380. return 0;
  1381. }
  1382. static cmd_tbl_t cmd_i2c_sub[] = {
  1383. #if defined(CONFIG_SYS_I2C)
  1384. U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
  1385. #endif
  1386. U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
  1387. #if defined(CONFIG_SYS_I2C) || \
  1388. defined(CONFIG_I2C_MULTI_BUS)
  1389. U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
  1390. #endif /* CONFIG_I2C_MULTI_BUS */
  1391. #if defined(CONFIG_I2C_EDID)
  1392. U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
  1393. #endif /* CONFIG_I2C_EDID */
  1394. U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
  1395. U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
  1396. U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
  1397. U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
  1398. U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
  1399. U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
  1400. U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
  1401. U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
  1402. U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
  1403. #if defined(CONFIG_CMD_SDRAM)
  1404. U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
  1405. #endif
  1406. U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
  1407. };
  1408. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  1409. void i2c_reloc(void) {
  1410. fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
  1411. }
  1412. #endif
  1413. /**
  1414. * do_i2c() - Handle the "i2c" command-line command
  1415. * @cmdtp: Command data struct pointer
  1416. * @flag: Command flag
  1417. * @argc: Command-line argument count
  1418. * @argv: Array of command-line arguments
  1419. *
  1420. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  1421. * on error.
  1422. */
  1423. static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1424. {
  1425. cmd_tbl_t *c;
  1426. if (argc < 2)
  1427. return CMD_RET_USAGE;
  1428. /* Strip off leading 'i2c' command argument */
  1429. argc--;
  1430. argv++;
  1431. c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
  1432. if (c)
  1433. return c->cmd(cmdtp, flag, argc, argv);
  1434. else
  1435. return CMD_RET_USAGE;
  1436. }
  1437. /***************************************************/
  1438. #ifdef CONFIG_SYS_LONGHELP
  1439. static char i2c_help_text[] =
  1440. #if defined(CONFIG_SYS_I2C)
  1441. "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
  1442. #endif
  1443. "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
  1444. #if defined(CONFIG_SYS_I2C) || \
  1445. defined(CONFIG_I2C_MULTI_BUS)
  1446. "i2c dev [dev] - show or set current I2C bus\n"
  1447. #endif /* CONFIG_I2C_MULTI_BUS */
  1448. #if defined(CONFIG_I2C_EDID)
  1449. "i2c edid chip - print EDID configuration information\n"
  1450. #endif /* CONFIG_I2C_EDID */
  1451. "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
  1452. "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
  1453. "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
  1454. "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
  1455. "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
  1456. "i2c probe [address] - test for and show device(s) on the I2C bus\n"
  1457. "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
  1458. "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
  1459. "i2c reset - re-init the I2C Controller\n"
  1460. #if defined(CONFIG_CMD_SDRAM)
  1461. "i2c sdram chip - print SDRAM configuration information\n"
  1462. #endif
  1463. "i2c speed [speed] - show or set I2C bus speed";
  1464. #endif
  1465. U_BOOT_CMD(
  1466. i2c, 6, 1, do_i2c,
  1467. "I2C sub-system",
  1468. i2c_help_text
  1469. );