cmd_i2c.c 38 KB

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