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