cmd_i2c.c 36 KB

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