cmd_i2c.c 38 KB

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