i2c.c 48 KB

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