cmd_i2c.c 38 KB

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