cmd_i2c.c 36 KB

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