cmd_i2c.c 36 KB

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