cmd_i2c.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. * Two of the commands (imm and imw) take a byte/word/long modifier
  30. * (e.g. imm.w specifies the word-length modifier). This was done to
  31. * allow manipulating word-length registers. It was not done on any other
  32. * commands because it was not deemed useful.
  33. *
  34. * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
  35. * Each I2C chip on the bus has a unique address. On the I2C data bus,
  36. * the address is the upper seven bits and the LSB is the "read/write"
  37. * bit. Note that the {i2c_chip} address specified on the command
  38. * line is not shifted up: e.g. a typical EEPROM memory chip may have
  39. * an I2C address of 0x50, but the data put on the bus will be 0xA0
  40. * for write and 0xA1 for read. This "non shifted" address notation
  41. * matches at least half of the data sheets :-/.
  42. *
  43. * {addr} is the address (or offset) within the chip. Small memory
  44. * chips have 8 bit addresses. Large memory chips have 16 bit
  45. * addresses. Other memory chips have 9, 10, or 11 bit addresses.
  46. * Many non-memory chips have multiple registers and {addr} is used
  47. * as the register index. Some non-memory chips have only one register
  48. * and therefore don't need any {addr} parameter.
  49. *
  50. * The default {addr} parameter is one byte (.1) which works well for
  51. * memories and registers with 8 bits of address space.
  52. *
  53. * You can specify the length of the {addr} field with the optional .0,
  54. * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
  55. * manipulating a single register device which doesn't use an address
  56. * field, use "0.0" for the address and the ".0" length field will
  57. * suppress the address in the I2C data stream. This also works for
  58. * successive reads using the I2C auto-incrementing memory pointer.
  59. *
  60. * If you are manipulating a large memory with 2-byte addresses, use
  61. * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
  62. *
  63. * Then there are the unfortunate memory chips that spill the most
  64. * significant 1, 2, or 3 bits of address into the chip address byte.
  65. * This effectively makes one chip (logically) look like 2, 4, or
  66. * 8 chips. This is handled (awkwardly) by #defining
  67. * CFG_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
  68. * {addr} field (since .1 is the default, it doesn't actually have to
  69. * be specified). Examples: given a memory chip at I2C chip address
  70. * 0x50, the following would happen...
  71. * imd 50 0 10 display 16 bytes starting at 0x000
  72. * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
  73. * imd 50 100 10 display 16 bytes starting at 0x100
  74. * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
  75. * imd 50 210 10 display 16 bytes starting at 0x210
  76. * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
  77. * This is awfully ugly. It would be nice if someone would think up
  78. * a better way of handling this.
  79. *
  80. * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
  81. */
  82. #include <common.h>
  83. #include <command.h>
  84. #include <i2c.h>
  85. #include <asm/byteorder.h>
  86. #if defined(CONFIG_CMD_I2C)
  87. /* Display values from last command.
  88. * Memory modify remembered values are different from display memory.
  89. */
  90. static uchar i2c_dp_last_chip;
  91. static uint i2c_dp_last_addr;
  92. static uint i2c_dp_last_alen;
  93. static uint i2c_dp_last_length = 0x10;
  94. static uchar i2c_mm_last_chip;
  95. static uint i2c_mm_last_addr;
  96. static uint i2c_mm_last_alen;
  97. /* If only one I2C bus is present, the list of devices to ignore when
  98. * the probe command is issued is represented by a 1D array of addresses.
  99. * When multiple buses are present, the list is an array of bus-address
  100. * pairs. The following macros take care of this */
  101. #if defined(CFG_I2C_NOPROBES)
  102. #if defined(CONFIG_I2C_MULTI_BUS)
  103. static struct
  104. {
  105. uchar bus;
  106. uchar addr;
  107. } i2c_no_probes[] = CFG_I2C_NOPROBES;
  108. #define GET_BUS_NUM i2c_get_bus_num()
  109. #define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
  110. #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
  111. #define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
  112. #else /* single bus */
  113. static uchar i2c_no_probes[] = CFG_I2C_NOPROBES;
  114. #define GET_BUS_NUM 0
  115. #define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
  116. #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
  117. #define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
  118. #endif /* CONFIG_MULTI_BUS */
  119. #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
  120. #endif
  121. static int
  122. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[]);
  123. extern int cmd_get_data_size(char* arg, int default_size);
  124. /*
  125. * Syntax:
  126. * imd {i2c_chip} {addr}{.0, .1, .2} {len}
  127. */
  128. #define DISP_LINE_LEN 16
  129. int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  130. {
  131. u_char chip;
  132. uint addr, alen, length;
  133. int j, nbytes, linebytes;
  134. /* We use the last specified parameters, unless new ones are
  135. * entered.
  136. */
  137. chip = i2c_dp_last_chip;
  138. addr = i2c_dp_last_addr;
  139. alen = i2c_dp_last_alen;
  140. length = i2c_dp_last_length;
  141. if (argc < 3) {
  142. printf ("Usage:\n%s\n", cmdtp->usage);
  143. return 1;
  144. }
  145. if ((flag & CMD_FLAG_REPEAT) == 0) {
  146. /*
  147. * New command specified.
  148. */
  149. alen = 1;
  150. /*
  151. * I2C chip address
  152. */
  153. chip = simple_strtoul(argv[1], NULL, 16);
  154. /*
  155. * I2C data address within the chip. This can be 1 or
  156. * 2 bytes long. Some day it might be 3 bytes long :-).
  157. */
  158. addr = simple_strtoul(argv[2], NULL, 16);
  159. alen = 1;
  160. for (j = 0; j < 8; j++) {
  161. if (argv[2][j] == '.') {
  162. alen = argv[2][j+1] - '0';
  163. if (alen > 4) {
  164. printf ("Usage:\n%s\n", cmdtp->usage);
  165. return 1;
  166. }
  167. break;
  168. } else if (argv[2][j] == '\0')
  169. break;
  170. }
  171. /*
  172. * If another parameter, it is the length to display.
  173. * Length is the number of objects, not number of bytes.
  174. */
  175. if (argc > 3)
  176. length = simple_strtoul(argv[3], NULL, 16);
  177. }
  178. /*
  179. * Print the lines.
  180. *
  181. * We buffer all read data, so we can make sure data is read only
  182. * once.
  183. */
  184. nbytes = length;
  185. do {
  186. unsigned char linebuf[DISP_LINE_LEN];
  187. unsigned char *cp;
  188. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  189. if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
  190. puts ("Error reading the chip.\n");
  191. else {
  192. printf("%04x:", addr);
  193. cp = linebuf;
  194. for (j=0; j<linebytes; j++) {
  195. printf(" %02x", *cp++);
  196. addr++;
  197. }
  198. puts (" ");
  199. cp = linebuf;
  200. for (j=0; j<linebytes; j++) {
  201. if ((*cp < 0x20) || (*cp > 0x7e))
  202. puts (".");
  203. else
  204. printf("%c", *cp);
  205. cp++;
  206. }
  207. putc ('\n');
  208. }
  209. nbytes -= linebytes;
  210. } while (nbytes > 0);
  211. i2c_dp_last_chip = chip;
  212. i2c_dp_last_addr = addr;
  213. i2c_dp_last_alen = alen;
  214. i2c_dp_last_length = length;
  215. return 0;
  216. }
  217. int do_i2c_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  218. {
  219. return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
  220. }
  221. int do_i2c_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  222. {
  223. return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
  224. }
  225. /* Write (fill) memory
  226. *
  227. * Syntax:
  228. * imw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
  229. */
  230. int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  231. {
  232. uchar chip;
  233. ulong addr;
  234. uint alen;
  235. uchar byte;
  236. int count;
  237. int j;
  238. if ((argc < 4) || (argc > 5)) {
  239. printf ("Usage:\n%s\n", cmdtp->usage);
  240. return 1;
  241. }
  242. /*
  243. * Chip is always specified.
  244. */
  245. chip = simple_strtoul(argv[1], NULL, 16);
  246. /*
  247. * Address is always specified.
  248. */
  249. addr = simple_strtoul(argv[2], NULL, 16);
  250. alen = 1;
  251. for (j = 0; j < 8; j++) {
  252. if (argv[2][j] == '.') {
  253. alen = argv[2][j+1] - '0';
  254. if (alen > 4) {
  255. printf ("Usage:\n%s\n", cmdtp->usage);
  256. return 1;
  257. }
  258. break;
  259. } else if (argv[2][j] == '\0')
  260. break;
  261. }
  262. /*
  263. * Value to write is always specified.
  264. */
  265. byte = simple_strtoul(argv[3], NULL, 16);
  266. /*
  267. * Optional count
  268. */
  269. if (argc == 5)
  270. count = simple_strtoul(argv[4], NULL, 16);
  271. else
  272. count = 1;
  273. while (count-- > 0) {
  274. if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
  275. puts ("Error writing the chip.\n");
  276. /*
  277. * Wait for the write to complete. The write can take
  278. * up to 10mSec (we allow a little more time).
  279. *
  280. * On some chips, while the write is in progress, the
  281. * chip doesn't respond. This apparently isn't a
  282. * universal feature so we don't take advantage of it.
  283. */
  284. /*
  285. * No write delay with FRAM devices.
  286. */
  287. #if !defined(CFG_I2C_FRAM)
  288. udelay(11000);
  289. #endif
  290. #if 0
  291. for (timeout = 0; timeout < 10; timeout++) {
  292. udelay(2000);
  293. if (i2c_probe(chip) == 0)
  294. break;
  295. }
  296. #endif
  297. }
  298. return (0);
  299. }
  300. /* Calculate a CRC on memory
  301. *
  302. * Syntax:
  303. * icrc32 {i2c_chip} {addr}{.0, .1, .2} {count}
  304. */
  305. int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  306. {
  307. uchar chip;
  308. ulong addr;
  309. uint alen;
  310. int count;
  311. uchar byte;
  312. ulong crc;
  313. ulong err;
  314. int j;
  315. if (argc < 4) {
  316. printf ("Usage:\n%s\n", cmdtp->usage);
  317. return 1;
  318. }
  319. /*
  320. * Chip is always specified.
  321. */
  322. chip = simple_strtoul(argv[1], NULL, 16);
  323. /*
  324. * Address is always specified.
  325. */
  326. addr = simple_strtoul(argv[2], NULL, 16);
  327. alen = 1;
  328. for (j = 0; j < 8; j++) {
  329. if (argv[2][j] == '.') {
  330. alen = argv[2][j+1] - '0';
  331. if (alen > 4) {
  332. printf ("Usage:\n%s\n", cmdtp->usage);
  333. return 1;
  334. }
  335. break;
  336. } else if (argv[2][j] == '\0')
  337. break;
  338. }
  339. /*
  340. * Count is always specified
  341. */
  342. count = simple_strtoul(argv[3], NULL, 16);
  343. printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
  344. /*
  345. * CRC a byte at a time. This is going to be slooow, but hey, the
  346. * memories are small and slow too so hopefully nobody notices.
  347. */
  348. crc = 0;
  349. err = 0;
  350. while (count-- > 0) {
  351. if (i2c_read(chip, addr, alen, &byte, 1) != 0)
  352. err++;
  353. crc = crc32 (crc, &byte, 1);
  354. addr++;
  355. }
  356. if (err > 0)
  357. puts ("Error reading the chip,\n");
  358. else
  359. printf ("%08lx\n", crc);
  360. return 0;
  361. }
  362. /* Modify memory.
  363. *
  364. * Syntax:
  365. * imm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  366. * inm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  367. */
  368. static int
  369. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  370. {
  371. uchar chip;
  372. ulong addr;
  373. uint alen;
  374. ulong data;
  375. int size = 1;
  376. int nbytes;
  377. int j;
  378. extern char console_buffer[];
  379. if (argc != 3) {
  380. printf ("Usage:\n%s\n", cmdtp->usage);
  381. return 1;
  382. }
  383. #ifdef CONFIG_BOOT_RETRY_TIME
  384. reset_cmd_timeout(); /* got a good command to get here */
  385. #endif
  386. /*
  387. * We use the last specified parameters, unless new ones are
  388. * entered.
  389. */
  390. chip = i2c_mm_last_chip;
  391. addr = i2c_mm_last_addr;
  392. alen = i2c_mm_last_alen;
  393. if ((flag & CMD_FLAG_REPEAT) == 0) {
  394. /*
  395. * New command specified. Check for a size specification.
  396. * Defaults to byte if no or incorrect specification.
  397. */
  398. size = cmd_get_data_size(argv[0], 1);
  399. /*
  400. * Chip is always specified.
  401. */
  402. chip = simple_strtoul(argv[1], NULL, 16);
  403. /*
  404. * Address is always specified.
  405. */
  406. addr = simple_strtoul(argv[2], NULL, 16);
  407. alen = 1;
  408. for (j = 0; j < 8; j++) {
  409. if (argv[2][j] == '.') {
  410. alen = argv[2][j+1] - '0';
  411. if (alen > 4) {
  412. printf ("Usage:\n%s\n", cmdtp->usage);
  413. return 1;
  414. }
  415. break;
  416. } else if (argv[2][j] == '\0')
  417. break;
  418. }
  419. }
  420. /*
  421. * Print the address, followed by value. Then accept input for
  422. * the next value. A non-converted value exits.
  423. */
  424. do {
  425. printf("%08lx:", addr);
  426. if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
  427. puts ("\nError reading the chip,\n");
  428. else {
  429. data = cpu_to_be32(data);
  430. if (size == 1)
  431. printf(" %02lx", (data >> 24) & 0x000000FF);
  432. else if (size == 2)
  433. printf(" %04lx", (data >> 16) & 0x0000FFFF);
  434. else
  435. printf(" %08lx", data);
  436. }
  437. nbytes = readline (" ? ");
  438. if (nbytes == 0) {
  439. /*
  440. * <CR> pressed as only input, don't modify current
  441. * location and move to next.
  442. */
  443. if (incrflag)
  444. addr += size;
  445. nbytes = size;
  446. #ifdef CONFIG_BOOT_RETRY_TIME
  447. reset_cmd_timeout(); /* good enough to not time out */
  448. #endif
  449. }
  450. #ifdef CONFIG_BOOT_RETRY_TIME
  451. else if (nbytes == -2)
  452. break; /* timed out, exit the command */
  453. #endif
  454. else {
  455. char *endp;
  456. data = simple_strtoul(console_buffer, &endp, 16);
  457. if (size == 1)
  458. data = data << 24;
  459. else if (size == 2)
  460. data = data << 16;
  461. data = be32_to_cpu(data);
  462. nbytes = endp - console_buffer;
  463. if (nbytes) {
  464. #ifdef CONFIG_BOOT_RETRY_TIME
  465. /*
  466. * good enough to not time out
  467. */
  468. reset_cmd_timeout();
  469. #endif
  470. if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
  471. puts ("Error writing the chip.\n");
  472. #ifdef CFG_EEPROM_PAGE_WRITE_DELAY_MS
  473. udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  474. #endif
  475. if (incrflag)
  476. addr += size;
  477. }
  478. }
  479. } while (nbytes);
  480. chip = i2c_mm_last_chip;
  481. addr = i2c_mm_last_addr;
  482. alen = i2c_mm_last_alen;
  483. return 0;
  484. }
  485. /*
  486. * Syntax:
  487. * iprobe {addr}{.0, .1, .2}
  488. */
  489. int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  490. {
  491. int j;
  492. #if defined(CFG_I2C_NOPROBES)
  493. int k, skip;
  494. uchar bus = GET_BUS_NUM;
  495. #endif /* NOPROBES */
  496. puts ("Valid chip addresses:");
  497. for (j = 0; j < 128; j++) {
  498. #if defined(CFG_I2C_NOPROBES)
  499. skip = 0;
  500. for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
  501. if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
  502. skip = 1;
  503. break;
  504. }
  505. }
  506. if (skip)
  507. continue;
  508. #endif
  509. if (i2c_probe(j) == 0)
  510. printf(" %02X", j);
  511. }
  512. putc ('\n');
  513. #if defined(CFG_I2C_NOPROBES)
  514. puts ("Excluded chip addresses:");
  515. for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
  516. if (COMPARE_BUS(bus,k))
  517. printf(" %02X", NO_PROBE_ADDR(k));
  518. }
  519. putc ('\n');
  520. #endif
  521. return 0;
  522. }
  523. /*
  524. * Syntax:
  525. * iloop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
  526. * {length} - Number of bytes to read
  527. * {delay} - A DECIMAL number and defaults to 1000 uSec
  528. */
  529. int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  530. {
  531. u_char chip;
  532. ulong alen;
  533. uint addr;
  534. uint length;
  535. u_char bytes[16];
  536. int delay;
  537. int j;
  538. if (argc < 3) {
  539. printf ("Usage:\n%s\n", cmdtp->usage);
  540. return 1;
  541. }
  542. /*
  543. * Chip is always specified.
  544. */
  545. chip = simple_strtoul(argv[1], NULL, 16);
  546. /*
  547. * Address is always specified.
  548. */
  549. addr = simple_strtoul(argv[2], NULL, 16);
  550. alen = 1;
  551. for (j = 0; j < 8; j++) {
  552. if (argv[2][j] == '.') {
  553. alen = argv[2][j+1] - '0';
  554. if (alen > 4) {
  555. printf ("Usage:\n%s\n", cmdtp->usage);
  556. return 1;
  557. }
  558. break;
  559. } else if (argv[2][j] == '\0')
  560. break;
  561. }
  562. /*
  563. * Length is the number of objects, not number of bytes.
  564. */
  565. length = 1;
  566. length = simple_strtoul(argv[3], NULL, 16);
  567. if (length > sizeof(bytes))
  568. length = sizeof(bytes);
  569. /*
  570. * The delay time (uSec) is optional.
  571. */
  572. delay = 1000;
  573. if (argc > 3)
  574. delay = simple_strtoul(argv[4], NULL, 10);
  575. /*
  576. * Run the loop...
  577. */
  578. while (1) {
  579. if (i2c_read(chip, addr, alen, bytes, length) != 0)
  580. puts ("Error reading the chip.\n");
  581. udelay(delay);
  582. }
  583. /* NOTREACHED */
  584. return 0;
  585. }
  586. /*
  587. * The SDRAM command is separately configured because many
  588. * (most?) embedded boards don't use SDRAM DIMMs.
  589. */
  590. #if defined(CONFIG_CMD_SDRAM)
  591. /*
  592. * Syntax:
  593. * sdram {i2c_chip}
  594. */
  595. int do_sdram ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  596. {
  597. u_char chip;
  598. u_char data[128];
  599. u_char cksum;
  600. int j;
  601. if (argc < 2) {
  602. printf ("Usage:\n%s\n", cmdtp->usage);
  603. return 1;
  604. }
  605. /*
  606. * Chip is always specified.
  607. */
  608. chip = simple_strtoul(argv[1], NULL, 16);
  609. if (i2c_read(chip, 0, 1, data, sizeof(data)) != 0) {
  610. puts ("No SDRAM Serial Presence Detect found.\n");
  611. return 1;
  612. }
  613. cksum = 0;
  614. for (j = 0; j < 63; j++) {
  615. cksum += data[j];
  616. }
  617. if (cksum != data[63]) {
  618. printf ("WARNING: Configuration data checksum failure:\n"
  619. " is 0x%02x, calculated 0x%02x\n",
  620. data[63], cksum);
  621. }
  622. printf("SPD data revision %d.%d\n",
  623. (data[62] >> 4) & 0x0F, data[62] & 0x0F);
  624. printf("Bytes used 0x%02X\n", data[0]);
  625. printf("Serial memory size 0x%02X\n", 1 << data[1]);
  626. puts ("Memory type ");
  627. switch(data[2]) {
  628. case 2: puts ("EDO\n"); break;
  629. case 4: puts ("SDRAM\n"); break;
  630. case 8: puts ("DDR2\n"); break;
  631. default: puts ("unknown\n"); break;
  632. }
  633. puts ("Row address bits ");
  634. if ((data[3] & 0x00F0) == 0)
  635. printf("%d\n", data[3] & 0x0F);
  636. else
  637. printf("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
  638. puts ("Column address bits ");
  639. if ((data[4] & 0x00F0) == 0)
  640. printf("%d\n", data[4] & 0x0F);
  641. else
  642. printf("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
  643. printf("Module rows %d\n", data[5]);
  644. printf("Module data width %d bits\n", (data[7] << 8) | data[6]);
  645. puts ("Interface signal levels ");
  646. switch(data[8]) {
  647. case 0: puts ("5.0v/TTL\n"); break;
  648. case 1: puts ("LVTTL\n"); break;
  649. case 2: puts ("HSTL 1.5\n"); break;
  650. case 3: puts ("SSTL 3.3\n"); break;
  651. case 4: puts ("SSTL 2.5\n"); break;
  652. case 5: puts ("SSTL 1.8\n"); break;
  653. default: puts ("unknown\n"); break;
  654. }
  655. printf("SDRAM cycle time %d.%d nS\n",
  656. (data[9] >> 4) & 0x0F, data[9] & 0x0F);
  657. printf("SDRAM access time %d.%d nS\n",
  658. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  659. puts ("EDC configuration ");
  660. switch(data[11]) {
  661. case 0: puts ("None\n"); break;
  662. case 1: puts ("Parity\n"); break;
  663. case 2: puts ("ECC\n"); break;
  664. default: puts ("unknown\n"); break;
  665. }
  666. if ((data[12] & 0x80) == 0)
  667. puts ("No self refresh, rate ");
  668. else
  669. puts ("Self refresh, rate ");
  670. switch(data[12] & 0x7F) {
  671. case 0: puts ("15.625uS\n"); break;
  672. case 1: puts ("3.9uS\n"); break;
  673. case 2: puts ("7.8uS\n"); break;
  674. case 3: puts ("31.3uS\n"); break;
  675. case 4: puts ("62.5uS\n"); break;
  676. case 5: puts ("125uS\n"); break;
  677. default: puts ("unknown\n"); break;
  678. }
  679. printf("SDRAM width (primary) %d\n", data[13] & 0x7F);
  680. if ((data[13] & 0x80) != 0) {
  681. printf(" (second bank) %d\n",
  682. 2 * (data[13] & 0x7F));
  683. }
  684. if (data[14] != 0) {
  685. printf("EDC width %d\n",
  686. data[14] & 0x7F);
  687. if ((data[14] & 0x80) != 0)
  688. printf(" (second bank) %d\n",
  689. 2 * (data[14] & 0x7F));
  690. }
  691. printf("Min clock delay, back-to-back random column addresses %d\n",
  692. data[15]);
  693. puts ("Burst length(s) ");
  694. if (data[16] & 0x80) puts (" Page");
  695. if (data[16] & 0x08) puts (" 8");
  696. if (data[16] & 0x04) puts (" 4");
  697. if (data[16] & 0x02) puts (" 2");
  698. if (data[16] & 0x01) puts (" 1");
  699. putc ('\n');
  700. printf("Number of banks %d\n", data[17]);
  701. puts ("CAS latency(s) ");
  702. if (data[18] & 0x80) puts (" TBD");
  703. if (data[18] & 0x40) puts (" 7");
  704. if (data[18] & 0x20) puts (" 6");
  705. if (data[18] & 0x10) puts (" 5");
  706. if (data[18] & 0x08) puts (" 4");
  707. if (data[18] & 0x04) puts (" 3");
  708. if (data[18] & 0x02) puts (" 2");
  709. if (data[18] & 0x01) puts (" 1");
  710. putc ('\n');
  711. puts ("CS latency(s) ");
  712. if (data[19] & 0x80) puts (" TBD");
  713. if (data[19] & 0x40) puts (" 6");
  714. if (data[19] & 0x20) puts (" 5");
  715. if (data[19] & 0x10) puts (" 4");
  716. if (data[19] & 0x08) puts (" 3");
  717. if (data[19] & 0x04) puts (" 2");
  718. if (data[19] & 0x02) puts (" 1");
  719. if (data[19] & 0x01) puts (" 0");
  720. putc ('\n');
  721. puts ("WE latency(s) ");
  722. if (data[20] & 0x80) puts (" TBD");
  723. if (data[20] & 0x40) puts (" 6");
  724. if (data[20] & 0x20) puts (" 5");
  725. if (data[20] & 0x10) puts (" 4");
  726. if (data[20] & 0x08) puts (" 3");
  727. if (data[20] & 0x04) puts (" 2");
  728. if (data[20] & 0x02) puts (" 1");
  729. if (data[20] & 0x01) puts (" 0");
  730. putc ('\n');
  731. puts ("Module attributes:\n");
  732. if (!data[21]) puts (" (none)\n");
  733. if (data[21] & 0x80) puts (" TBD (bit 7)\n");
  734. if (data[21] & 0x40) puts (" Redundant row address\n");
  735. if (data[21] & 0x20) puts (" Differential clock input\n");
  736. if (data[21] & 0x10) puts (" Registerd DQMB inputs\n");
  737. if (data[21] & 0x08) puts (" Buffered DQMB inputs\n");
  738. if (data[21] & 0x04) puts (" On-card PLL\n");
  739. if (data[21] & 0x02) puts (" Registered address/control lines\n");
  740. if (data[21] & 0x01) puts (" Buffered address/control lines\n");
  741. puts ("Device attributes:\n");
  742. if (data[22] & 0x80) puts (" TBD (bit 7)\n");
  743. if (data[22] & 0x40) puts (" TBD (bit 6)\n");
  744. if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
  745. else puts (" Upper Vcc tolerance 10%\n");
  746. if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
  747. else puts (" Lower Vcc tolerance 10%\n");
  748. if (data[22] & 0x08) puts (" Supports write1/read burst\n");
  749. if (data[22] & 0x04) puts (" Supports precharge all\n");
  750. if (data[22] & 0x02) puts (" Supports auto precharge\n");
  751. if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
  752. printf("SDRAM cycle time (2nd highest CAS latency) %d.%d nS\n",
  753. (data[23] >> 4) & 0x0F, data[23] & 0x0F);
  754. printf("SDRAM access from clock (2nd highest CAS latency) %d.%d nS\n",
  755. (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  756. printf("SDRAM cycle time (3rd highest CAS latency) %d.%d nS\n",
  757. (data[25] >> 4) & 0x0F, data[25] & 0x0F);
  758. printf("SDRAM access from clock (3rd highest CAS latency) %d.%d nS\n",
  759. (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  760. printf("Minimum row precharge %d nS\n", data[27]);
  761. printf("Row active to row active min %d nS\n", data[28]);
  762. printf("RAS to CAS delay min %d nS\n", data[29]);
  763. printf("Minimum RAS pulse width %d nS\n", data[30]);
  764. puts ("Density of each row ");
  765. if (data[31] & 0x80) puts (" 512");
  766. if (data[31] & 0x40) puts (" 256");
  767. if (data[31] & 0x20) puts (" 128");
  768. if (data[31] & 0x10) puts (" 64");
  769. if (data[31] & 0x08) puts (" 32");
  770. if (data[31] & 0x04) puts (" 16");
  771. if (data[31] & 0x02) puts (" 8");
  772. if (data[31] & 0x01) puts (" 4");
  773. puts ("MByte\n");
  774. printf("Command and Address setup %c%d.%d nS\n",
  775. (data[32] & 0x80) ? '-' : '+',
  776. (data[32] >> 4) & 0x07, data[32] & 0x0F);
  777. printf("Command and Address hold %c%d.%d nS\n",
  778. (data[33] & 0x80) ? '-' : '+',
  779. (data[33] >> 4) & 0x07, data[33] & 0x0F);
  780. printf("Data signal input setup %c%d.%d nS\n",
  781. (data[34] & 0x80) ? '-' : '+',
  782. (data[34] >> 4) & 0x07, data[34] & 0x0F);
  783. printf("Data signal input hold %c%d.%d nS\n",
  784. (data[35] & 0x80) ? '-' : '+',
  785. (data[35] >> 4) & 0x07, data[35] & 0x0F);
  786. puts ("Manufacturer's JEDEC ID ");
  787. for (j = 64; j <= 71; j++)
  788. printf("%02X ", data[j]);
  789. putc ('\n');
  790. printf("Manufacturing Location %02X\n", data[72]);
  791. puts ("Manufacturer's Part Number ");
  792. for (j = 73; j <= 90; j++)
  793. printf("%02X ", data[j]);
  794. putc ('\n');
  795. printf("Revision Code %02X %02X\n", data[91], data[92]);
  796. printf("Manufacturing Date %02X %02X\n", data[93], data[94]);
  797. puts ("Assembly Serial Number ");
  798. for (j = 95; j <= 98; j++)
  799. printf("%02X ", data[j]);
  800. putc ('\n');
  801. printf("Speed rating PC%d\n",
  802. data[126] == 0x66 ? 66 : data[126]);
  803. return 0;
  804. }
  805. #endif
  806. #if defined(CONFIG_I2C_CMD_TREE)
  807. #if defined(CONFIG_I2C_MULTI_BUS)
  808. int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  809. {
  810. int bus_idx, ret=0;
  811. if (argc == 1)
  812. /* querying current setting */
  813. printf("Current bus is %d\n", i2c_get_bus_num());
  814. else {
  815. bus_idx = simple_strtoul(argv[1], NULL, 10);
  816. printf("Setting bus to %d\n", bus_idx);
  817. ret = i2c_set_bus_num(bus_idx);
  818. if (ret)
  819. printf("Failure changing bus number (%d)\n", ret);
  820. }
  821. return ret;
  822. }
  823. #endif /* CONFIG_I2C_MULTI_BUS */
  824. int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  825. {
  826. int speed, ret=0;
  827. if (argc == 1)
  828. /* querying current speed */
  829. printf("Current bus speed=%d\n", i2c_get_bus_speed());
  830. else {
  831. speed = simple_strtoul(argv[1], NULL, 10);
  832. printf("Setting bus speed to %d Hz\n", speed);
  833. ret = i2c_set_bus_speed(speed);
  834. if (ret)
  835. printf("Failure changing bus speed (%d)\n", ret);
  836. }
  837. return ret;
  838. }
  839. int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  840. {
  841. #if defined(CONFIG_I2C_MULTI_BUS)
  842. if (!strncmp(argv[1], "de", 2))
  843. return do_i2c_bus_num(cmdtp, flag, --argc, ++argv);
  844. #endif /* CONFIG_I2C_MULTI_BUS */
  845. if (!strncmp(argv[1], "sp", 2))
  846. return do_i2c_bus_speed(cmdtp, flag, --argc, ++argv);
  847. if (!strncmp(argv[1], "md", 2))
  848. return do_i2c_md(cmdtp, flag, --argc, ++argv);
  849. if (!strncmp(argv[1], "mm", 2))
  850. return do_i2c_mm(cmdtp, flag, --argc, ++argv);
  851. if (!strncmp(argv[1], "mw", 2))
  852. return do_i2c_mw(cmdtp, flag, --argc, ++argv);
  853. if (!strncmp(argv[1], "nm", 2))
  854. return do_i2c_nm(cmdtp, flag, --argc, ++argv);
  855. if (!strncmp(argv[1], "cr", 2))
  856. return do_i2c_crc(cmdtp, flag, --argc, ++argv);
  857. if (!strncmp(argv[1], "pr", 2))
  858. return do_i2c_probe(cmdtp, flag, --argc, ++argv);
  859. if (!strncmp(argv[1], "lo", 2))
  860. return do_i2c_loop(cmdtp, flag, --argc, ++argv);
  861. #if defined(CONFIG_CMD_SDRAM)
  862. if (!strncmp(argv[1], "sd", 2))
  863. return do_sdram(cmdtp, flag, --argc, ++argv);
  864. #endif
  865. else
  866. printf ("Usage:\n%s\n", cmdtp->usage);
  867. return 0;
  868. }
  869. #endif /* CONFIG_I2C_CMD_TREE */
  870. /***************************************************/
  871. #if defined(CONFIG_I2C_CMD_TREE)
  872. U_BOOT_CMD(
  873. i2c, 6, 1, do_i2c,
  874. "i2c - I2C sub-system\n",
  875. #if defined(CONFIG_I2C_MULTI_BUS)
  876. "dev [dev] - show or set current I2C bus\n"
  877. #endif /* CONFIG_I2C_MULTI_BUS */
  878. "i2c speed [speed] - show or set I2C bus speed\n"
  879. "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
  880. "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
  881. "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
  882. "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
  883. "i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
  884. "i2c probe - show devices on the I2C bus\n"
  885. "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
  886. #if defined(CONFIG_CMD_SDRAM)
  887. "i2c sdram chip - print SDRAM configuration information\n"
  888. #endif
  889. );
  890. #endif /* CONFIG_I2C_CMD_TREE */
  891. U_BOOT_CMD(
  892. imd, 4, 1, do_i2c_md, \
  893. "imd - i2c memory display\n", \
  894. "chip address[.0, .1, .2] [# of objects]\n - i2c memory display\n" \
  895. );
  896. U_BOOT_CMD(
  897. imm, 3, 1, do_i2c_mm,
  898. "imm - i2c memory modify (auto-incrementing)\n",
  899. "chip address[.0, .1, .2]\n"
  900. " - memory modify, auto increment address\n"
  901. );
  902. U_BOOT_CMD(
  903. inm, 3, 1, do_i2c_nm,
  904. "inm - memory modify (constant address)\n",
  905. "chip address[.0, .1, .2]\n - memory modify, read and keep address\n"
  906. );
  907. U_BOOT_CMD(
  908. imw, 5, 1, do_i2c_mw,
  909. "imw - memory write (fill)\n",
  910. "chip address[.0, .1, .2] value [count]\n - memory write (fill)\n"
  911. );
  912. U_BOOT_CMD(
  913. icrc32, 5, 1, do_i2c_crc,
  914. "icrc32 - checksum calculation\n",
  915. "chip address[.0, .1, .2] count\n - compute CRC32 checksum\n"
  916. );
  917. U_BOOT_CMD(
  918. iprobe, 1, 1, do_i2c_probe,
  919. "iprobe - probe to discover valid I2C chip addresses\n",
  920. "\n -discover valid I2C chip addresses\n"
  921. );
  922. /*
  923. * Require full name for "iloop" because it is an infinite loop!
  924. */
  925. U_BOOT_CMD(
  926. iloop, 5, 1, do_i2c_loop,
  927. "iloop - infinite loop on address range\n",
  928. "chip address[.0, .1, .2] [# of objects]\n"
  929. " - loop, reading a set of addresses\n"
  930. );
  931. #if defined(CONFIG_CMD_SDRAM)
  932. U_BOOT_CMD(
  933. isdram, 2, 1, do_sdram,
  934. "isdram - print SDRAM configuration information\n",
  935. "chip\n - print SDRAM configuration information\n"
  936. " (valid chip values 50..57)\n"
  937. );
  938. #endif
  939. #endif