cmd_i2c.c 39 KB

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