cmd_i2c.c 43 KB

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