cmd_i2c.c 44 KB

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