cmd_i2c.c 40 KB

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