cmd_i2c.c 38 KB

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