cmd_i2c.c 45 KB

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