cmd_i2c.c 48 KB

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