cmd_i2c.c 36 KB

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