cmd_i2c.c 36 KB

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