cmd_i2c.c 36 KB

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