i2c.c 48 KB

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