cmd_i2c.c 37 KB

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