cmd_i2c.c 37 KB

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