i2c.c 48 KB

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