i2c.c 49 KB

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