cmd_i2c.c 37 KB

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