octeon_fdt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Marvell International Ltd.
  4. */
  5. #include <env.h>
  6. #include <log.h>
  7. #include <i2c.h>
  8. #include <net.h>
  9. #include <dm/device.h>
  10. #include <linux/delay.h>
  11. #include <mach/cvmx-regs.h>
  12. #include <mach/cvmx-csr.h>
  13. #include <mach/cvmx-bootmem.h>
  14. #include <mach/octeon-model.h>
  15. #include <mach/octeon_eth.h>
  16. #include <mach/octeon_fdt.h>
  17. #include <mach/cvmx-helper-fdt.h>
  18. #include <mach/cvmx-helper-gpio.h>
  19. #include <mach/cvmx-fuse.h>
  20. #include <mach/octeon-feature.h>
  21. #include <mach/cvmx-qlm.h>
  22. #include <mach/octeon_qlm.h>
  23. #include <asm/gpio.h>
  24. #ifdef CONFIG_PCA953X
  25. #include <pca953x.h>
  26. #endif
  27. #ifdef CONFIG_PCF857X
  28. #include <pcf857x.h>
  29. #endif
  30. #ifdef CONFIG_PCA9698
  31. #include <pca9698.h>
  32. #endif
  33. #ifdef CONFIG_PCA9554
  34. #include <pca9554.h>
  35. #endif
  36. #ifdef CONFIG_PCA9555
  37. #include <pca9555.h>
  38. #endif
  39. DECLARE_GLOBAL_DATA_PTR;
  40. #ifdef CONFIG_PCA9554
  41. static const char * const pca9554_gpio_list[] = {
  42. "pca9554",
  43. "nxp,pca9554",
  44. "ti,pca9554",
  45. NULL,
  46. };
  47. #endif
  48. #ifdef CONFIG_PCA9555
  49. static const char * const pca9555_gpio_list[] = {
  50. "pca9535", "nxp,pca9535", "pca9539", "nxp,pca9539", "pca9555",
  51. "nxp,pca9555", "ti,pca9555", "max7312", "maxim,max7312", "max7313",
  52. "maxim,max7313", "tca6416", "tca9539", NULL,
  53. };
  54. #endif
  55. #ifdef CONFIG_PCA9698
  56. /** List of compatible strings supported by pca9698 driver */
  57. static const char * const pca9698_gpio_list[] = {
  58. "nxp,pca9505", "pca9505", "nxp,pca9698", "pca9698", NULL,
  59. };
  60. #endif
  61. #ifdef CONFIG_PCA953X
  62. /** List of compatible strings supported by pca953x driver */
  63. static const char * const pca953x_gpio_list[] = {
  64. "nxp,pca9534", "nxp,pca9535", "nxp,pca9536", "nxp,pca9537", "nxp,pca9538", "nxp,pca9539",
  65. "nxp,pca953x", "nxp,pca9554", "nxp,pca9555", "nxp,pca9556", "nxp,pca9557", "nxp,pca6107",
  66. "pca9534", "pca9535", "pca9536", "pca9537", "pca9538", "pca9539",
  67. "pca953x", "pca9554", "pca9555", "pca9556", "pca9557", "max7310",
  68. "max7312", "max7313", "max7315", "pca6107", "tca6408", "tca6416",
  69. "tca9555", NULL
  70. };
  71. #endif
  72. #ifdef CONFIG_PHY_VITESSE
  73. static const char * const vitesse_vsc8488_gpio_list[] = {
  74. "vitesse,vsc8486", "microsemi,vsc8486", "vitesse,vsc8488",
  75. "microsemi,vsc8488", "vitesse,vsc8489", "microsemi,vsc8489",
  76. "vitesse,vsc8490", "microsemi,vsc8490", NULL
  77. };
  78. #endif
  79. /** List of compatible strings supported by Octeon driver */
  80. static const char * const octeon_gpio_list[] = {
  81. "cavium,octeon-7890-gpio",
  82. "cavium,octeon-3860-gpio",
  83. NULL
  84. };
  85. /**
  86. * Trims nodes from the flat device tree.
  87. *
  88. * @param fdt - pointer to working FDT, usually in gd->fdt_blob
  89. * @param fdt_key - key to preserve. All non-matching keys are removed
  90. * @param trim_name - name of property to look for. If NULL use
  91. * 'cavium,qlm-trim'
  92. *
  93. * The key should look something like device #, type where device # is a
  94. * number from 0-9 and type is a string describing the type. For QLM
  95. * operations this would typically contain the QLM number followed by
  96. * the type in the device tree, like "0,xaui", "0,sgmii", etc. This function
  97. * will trim all items in the device tree which match the device number but
  98. * have a type which does not match. For example, if a QLM has a xaui module
  99. * installed on QLM 0 and "0,xaui" is passed as a key, then all FDT nodes that
  100. * have "0,xaui" will be preserved but all others, i.e. "0,sgmii" will be
  101. * removed.
  102. *
  103. * Note that the trim_name must also match. If trim_name is NULL then it
  104. * looks for the property "cavium,qlm-trim".
  105. *
  106. * Also, when the trim_name is "cavium,qlm-trim" or NULL that the interfaces
  107. * will also be renamed based on their register values.
  108. *
  109. * For example, if a PIP interface is named "interface@W" and has the property
  110. * reg = <0> then the interface will be renamed after this function to
  111. * interface@0.
  112. *
  113. * Return: 0 for success.
  114. */
  115. int __octeon_fdt_patch(void *fdt, const char *fdt_key, const char *trim_name)
  116. {
  117. bool rename = !trim_name || !strcmp(trim_name, "cavium,qlm-trim");
  118. return octeon_fdt_patch_rename(fdt, fdt_key, trim_name, rename, NULL, NULL);
  119. }
  120. int octeon_fdt_patch(void *fdt, const char *fdt_key, const char *trim_name)
  121. __attribute__((weak, alias("__octeon_fdt_patch")));
  122. /**
  123. * Trims nodes from the flat device tree.
  124. *
  125. * @param fdt - pointer to working FDT, usually in gd->fdt_blob
  126. * @param fdt_key - key to preserve. All non-matching keys are removed
  127. * @param trim_name - name of property to look for. If NULL use
  128. * 'cavium,qlm-trim'
  129. * @param rename - set to TRUE to rename interfaces.
  130. * @param callback - function to call on matched nodes.
  131. * @param cbarg - passed to callback.
  132. *
  133. * The key should look something like device #, type where device # is a
  134. * number from 0-9 and type is a string describing the type. For QLM
  135. * operations this would typically contain the QLM number followed by
  136. * the type in the device tree, like "0,xaui", "0,sgmii", etc. This function
  137. * will trim all items in the device tree which match the device number but
  138. * have a type which does not match. For example, if a QLM has a xaui module
  139. * installed on QLM 0 and "0,xaui" is passed as a key, then all FDT nodes that
  140. * have "0,xaui" will be preserved but all others, i.e. "0,sgmii" will be
  141. * removed.
  142. *
  143. * Note that the trim_name must also match. If trim_name is NULL then it
  144. * looks for the property "cavium,qlm-trim".
  145. *
  146. * Also, when the trim_name is "cavium,qlm-trim" or NULL that the interfaces
  147. * will also be renamed based on their register values.
  148. *
  149. * For example, if a PIP interface is named "interface@W" and has the property
  150. * reg = <0> then the interface will be renamed after this function to
  151. * interface@0.
  152. *
  153. * Return: 0 for success.
  154. */
  155. int octeon_fdt_patch_rename(void *fdt, const char *fdt_key,
  156. const char *trim_name, bool rename,
  157. void (*callback)(void *fdt, int offset, void *arg),
  158. void *cbarg)
  159. __attribute__((weak, alias("__octeon_fdt_patch_rename")));
  160. int __octeon_fdt_patch_rename(void *fdt, const char *fdt_key,
  161. const char *trim_name, bool rename,
  162. void (*callback)(void *fdt, int offset, void *arg),
  163. void *cbarg)
  164. {
  165. int fdt_key_len;
  166. int offset, next_offset;
  167. int aliases;
  168. const void *aprop;
  169. char qlm[32];
  170. char *mode;
  171. int qlm_key_len;
  172. int rc;
  173. int cpu_node;
  174. if (!trim_name)
  175. trim_name = "cavium,qlm-trim";
  176. strncpy(qlm, fdt_key, sizeof(qlm));
  177. mode = qlm;
  178. strsep(&mode, ",");
  179. qlm_key_len = strlen(qlm);
  180. debug("In %s: Patching FDT header at 0x%p with key \"%s\"\n", __func__, fdt, fdt_key);
  181. if (!fdt || fdt_check_header(fdt) != 0) {
  182. printf("%s: Invalid device tree\n", __func__);
  183. return -1;
  184. }
  185. fdt_key_len = strlen(fdt_key) + 1;
  186. /* Prune out the unwanted parts based on the QLM mode. */
  187. offset = 0;
  188. for (offset = fdt_next_node(fdt, offset, NULL); offset >= 0; offset = next_offset) {
  189. int len;
  190. const char *val;
  191. const char *val_comma;
  192. next_offset = fdt_next_node(fdt, offset, NULL);
  193. val = fdt_getprop(fdt, offset, trim_name, &len);
  194. if (!val)
  195. continue;
  196. debug("fdt found trim name %s, comparing key \"%s\"(%d) with \"%s\"(%d)\n",
  197. trim_name, fdt_key, fdt_key_len, val, len);
  198. val_comma = strchr(val, ',');
  199. if (!val_comma || (val_comma - val) != qlm_key_len)
  200. continue;
  201. if (strncmp(val, qlm, qlm_key_len) != 0)
  202. continue; /* Not this QLM. */
  203. debug("fdt key number \"%s\" matches\n", val);
  204. if (!fdt_stringlist_contains(val, len, fdt_key)) {
  205. debug("Key \"%s\" does not match \"%s\"\n", val, fdt_key);
  206. /* This QLM, but wrong mode. Delete it. */
  207. /* See if there's an alias that needs deleting */
  208. val = fdt_getprop(fdt, offset, "cavium,qlm-trim-alias", NULL);
  209. if (val) {
  210. debug("Trimming alias \"%s\"\n", val);
  211. aliases = fdt_path_offset(fdt, "/aliases");
  212. if (aliases) {
  213. aprop = fdt_getprop(fdt, aliases, val, NULL);
  214. if (aprop) {
  215. rc = fdt_nop_property(fdt, aliases, val);
  216. if (rc) {
  217. printf("Error: Could not NOP alias %s in fdt\n",
  218. val);
  219. }
  220. } else {
  221. printf("Error: could not find /aliases/%s in device tree\n",
  222. val);
  223. }
  224. } else {
  225. puts("Error: could not find /aliases in device tree\n");
  226. }
  227. }
  228. debug("fdt trimming matching key %s\n", fdt_key);
  229. next_offset = fdt_parent_offset(fdt, offset);
  230. rc = fdt_nop_node(fdt, offset);
  231. if (rc)
  232. printf("Error %d noping node in device tree\n", rc);
  233. }
  234. }
  235. debug("%s: Starting pass 2 for key %s\n", __func__, fdt_key);
  236. /* Second pass: Rewrite names and remove key properties. */
  237. offset = -1;
  238. for (offset = fdt_next_node(fdt, offset, NULL); offset >= 0; offset = next_offset) {
  239. int len;
  240. const char *val = fdt_getprop(fdt, offset, trim_name, &len);
  241. next_offset = fdt_next_node(fdt, offset, NULL);
  242. if (!val)
  243. continue;
  244. debug("Searching stringlist %s for %s\n", val, fdt_key);
  245. if (fdt_stringlist_contains(val, len, fdt_key)) {
  246. char new_name[64];
  247. const char *name;
  248. const char *at;
  249. int reg;
  250. debug("Found key %s at offset 0x%x\n", fdt_key, offset);
  251. fdt_nop_property(fdt, offset, trim_name);
  252. if (rename) {
  253. name = fdt_get_name(fdt, offset, NULL);
  254. debug(" name: %s\n", name);
  255. if (!name)
  256. continue;
  257. at = strchr(name, '@');
  258. if (!at)
  259. continue;
  260. reg = fdtdec_get_int(fdt, offset, "reg", -1);
  261. if (reg == -1)
  262. continue;
  263. debug(" reg: %d\n", reg);
  264. len = at - name + 1;
  265. debug(" len: %d\n", len);
  266. if (len + 9 >= sizeof(new_name))
  267. continue;
  268. memcpy(new_name, name, len);
  269. cpu_node = cvmx_fdt_get_cpu_node(fdt, offset);
  270. if (cpu_node > 0)
  271. snprintf(new_name + len, sizeof(new_name) - len, "%x_%x",
  272. cpu_node, reg);
  273. else
  274. sprintf(new_name + len, "%x", reg);
  275. debug("Renaming cpu node %d %s to %s\n", cpu_node, name, new_name);
  276. fdt_set_name(fdt, offset, new_name);
  277. }
  278. if (callback)
  279. callback(fdt, offset, cbarg);
  280. /* Structure may have changed, start at the beginning. */
  281. next_offset = 0;
  282. }
  283. }
  284. return 0;
  285. }
  286. #ifdef CONFIG_CMD_NET
  287. static void octeon_set_one_fdt_mac(int node, uint64_t *mac)
  288. {
  289. u8 mac_addr[6];
  290. int r;
  291. mac_addr[5] = *mac & 0xff;
  292. mac_addr[4] = (*mac >> 8) & 0xff;
  293. mac_addr[3] = (*mac >> 16) & 0xff;
  294. mac_addr[2] = (*mac >> 24) & 0xff;
  295. mac_addr[1] = (*mac >> 32) & 0xff;
  296. mac_addr[0] = (*mac >> 40) & 0xff;
  297. r = fdt_setprop_inplace(working_fdt, node, "local-mac-address", mac_addr, 6);
  298. if (r == 0)
  299. *mac = *mac + 1;
  300. }
  301. static uint64_t convert_mac(const u8 mac_addr[6])
  302. {
  303. int i;
  304. u64 mac = 0;
  305. for (i = 0; i < 6; i++)
  306. mac = (mac << 8) | mac_addr[i];
  307. return mac;
  308. }
  309. /**
  310. * Fix up the MAC address in the flat device tree based on the MAC address
  311. * stored in ethaddr or in the board descriptor.
  312. *
  313. * NOTE: This function is weak and an alias for __octeon_fixup_fdt_mac_addr.
  314. */
  315. void octeon_fixup_fdt_mac_addr(void) __attribute__((weak, alias("__octeon_fixup_fdt_mac_addr")));
  316. void __octeon_fixup_fdt_mac_addr(void)
  317. {
  318. int node, pip, interface, ethernet;
  319. int i, e;
  320. u64 mac = 0;
  321. uchar mac_addr[6];
  322. char name[20];
  323. bool env_mac_addr_valid;
  324. const char *p;
  325. debug("%s: env ethaddr: %s\n", __func__, (p = env_get("ethaddr")) ? p : "not set");
  326. if (eth_env_get_enetaddr("ethaddr", mac_addr)) {
  327. mac = convert_mac(mac_addr);
  328. env_mac_addr_valid = true;
  329. } else {
  330. mac = convert_mac((uint8_t *)gd->arch.mac_desc.mac_addr_base);
  331. env_mac_addr_valid = false;
  332. }
  333. debug("%s: mac_addr: %pM, board mac: %pM, env valid: %s\n", __func__, mac_addr,
  334. gd->arch.mac_desc.mac_addr_base, env_mac_addr_valid ? "true" : "false");
  335. if (env_mac_addr_valid && memcmp(mac_addr, (void *)gd->arch.mac_desc.mac_addr_base, 6))
  336. printf("Warning: the environment variable ethaddr is set to %pM\n"
  337. "which does not match the board descriptor MAC address %pM.\n"
  338. "Please clear the ethaddr environment variable with the command\n"
  339. "\"setenv -f ethaddr; saveenv\" or change the board MAC address with the command\n"
  340. "\"tlv_eeprom set mac %pM\" to change the board MAC address so that it matches\n"
  341. "the environment address.\n"
  342. "Note: the correct MAC address is usually the one stored in the tlv EEPROM.\n",
  343. mac_addr, gd->arch.mac_desc.mac_addr_base, mac_addr);
  344. for (i = 0; i < 2; i++) {
  345. sprintf(name, "mix%x", i);
  346. p = fdt_get_alias(working_fdt, name);
  347. if (p) {
  348. node = fdt_path_offset(working_fdt, p);
  349. if (node > 0)
  350. octeon_set_one_fdt_mac(node, &mac);
  351. }
  352. }
  353. for (i = 0; i < 2; i++) {
  354. sprintf(name, "rgmii%x", i);
  355. p = fdt_get_alias(working_fdt, name);
  356. if (p) {
  357. node = fdt_path_offset(working_fdt, p);
  358. if (node > 0)
  359. octeon_set_one_fdt_mac(node, &mac);
  360. }
  361. }
  362. pip = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-3860-pip");
  363. if (pip > 0)
  364. for (i = 0; i < 8; i++) {
  365. sprintf(name, "interface@%d", i);
  366. interface = fdt_subnode_offset(working_fdt, pip, name);
  367. if (interface <= 0)
  368. continue;
  369. for (e = 0; e < 16; e++) {
  370. sprintf(name, "ethernet@%d", e);
  371. ethernet = fdt_subnode_offset(working_fdt, interface, name);
  372. if (ethernet <= 0)
  373. continue;
  374. octeon_set_one_fdt_mac(ethernet, &mac);
  375. }
  376. }
  377. /* Assign 78XX addresses in the order they appear in the device tree. */
  378. node = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-7890-bgx-port");
  379. while (node != -FDT_ERR_NOTFOUND) {
  380. octeon_set_one_fdt_mac(node, &mac);
  381. node = fdt_node_offset_by_compatible(working_fdt, node,
  382. "cavium,octeon-7890-bgx-port");
  383. }
  384. }
  385. #endif
  386. /**
  387. * This function fixes the clock-frequency in the flat device tree for the UART.
  388. *
  389. * NOTE: This function is weak and an alias for __octeon_fixup_fdt_uart.
  390. */
  391. void octeon_fixup_fdt_uart(void) __attribute__((weak, alias("__octeon_fixup_fdt_uart")));
  392. void __octeon_fixup_fdt_uart(void)
  393. {
  394. u32 clk;
  395. int node;
  396. clk = gd->bus_clk;
  397. /* Device trees already have good values for fast simulator
  398. * output, real boards need the correct value.
  399. */
  400. node = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-3860-uart");
  401. while (node != -FDT_ERR_NOTFOUND) {
  402. fdt_setprop_inplace_cell(working_fdt, node, "clock-frequency", clk);
  403. node = fdt_node_offset_by_compatible(working_fdt, node, "cavium,octeon-3860-uart");
  404. }
  405. }
  406. /**
  407. * This function fills in the /memory portion of the flat device tree.
  408. *
  409. * NOTE: This function is weak and aliased to __octeon_fixup_fdt_memory.
  410. */
  411. void octeon_fixup_fdt_memory(void) __attribute__((weak, alias("__octeon_fixup_fdt_memory")));
  412. void __octeon_fixup_fdt_memory(void)
  413. {
  414. u64 sizes[3], addresses[3];
  415. u64 size_left = gd->ram_size;
  416. int num_addresses = 0;
  417. int rc;
  418. int node;
  419. size_left = gd->ram_size;
  420. sizes[num_addresses] = min_t(u64, size_left, 256 * 1024 * 1024);
  421. size_left -= sizes[num_addresses];
  422. addresses[num_addresses] = 0;
  423. num_addresses++;
  424. if (size_left > 0) {
  425. sizes[num_addresses] = size_left;
  426. addresses[num_addresses] = 0x20000000ULL;
  427. num_addresses++;
  428. }
  429. node = fdt_path_offset(working_fdt, "/memory");
  430. if (node < 0)
  431. node = fdt_add_subnode(working_fdt, fdt_path_offset(working_fdt, "/"), "memory");
  432. if (node < 0) {
  433. printf("Could not add memory section to fdt: %s\n", fdt_strerror(node));
  434. return;
  435. }
  436. rc = fdt_fixup_memory_banks(working_fdt, addresses, sizes, num_addresses);
  437. if (rc != 0)
  438. printf("%s: fdt_fixup_memory_banks returned %d when adding %d addresses\n",
  439. __func__, rc, num_addresses);
  440. }
  441. void octeon_fixup_fdt(void) __attribute__((weak, alias("__octeon_fixup_fdt")));
  442. void __octeon_fixup_fdt(void)
  443. {
  444. if (!working_fdt)
  445. return;
  446. #ifdef CONFIG_CMD_NET
  447. octeon_fixup_fdt_mac_addr();
  448. #endif /* CONFIG_CMD_NET */
  449. #if !CONFIG_OCTEON_SIM_SPEED
  450. octeon_fixup_fdt_uart();
  451. #endif
  452. octeon_fixup_fdt_memory();
  453. }
  454. int __board_fixup_fdt(void)
  455. {
  456. /*
  457. * Nothing to do in this dummy implementation
  458. */
  459. return 0;
  460. }
  461. int board_fixup_fdt(void) __attribute__((weak, alias("__board_fixup_fdt")));
  462. /**
  463. * This is a helper function to find the offset of a PHY device given
  464. * an Ethernet device.
  465. *
  466. * @param[in] eth - Ethernet device to search for PHY offset
  467. *
  468. * @returns offset of phy info in device tree or -1 if not found
  469. */
  470. //int octeon_fdt_find_phy(const struct eth_device *eth)
  471. int octeon_fdt_find_phy(const struct udevice *eth)
  472. {
  473. int aliases;
  474. const void *fdt = gd->fdt_blob;
  475. const char *pip_path;
  476. int pip;
  477. char buffer[64];
  478. #if 0
  479. struct octeon_eth_info *oct_eth_info =
  480. (struct octeon_eth_info *)eth->priv;
  481. #else
  482. struct octeon_eth_info *oct_eth_info = dev_get_priv(eth);
  483. #endif
  484. int interface, index;
  485. int phandle;
  486. int phy;
  487. u32 *phy_handle;
  488. aliases = fdt_path_offset(fdt, "/aliases");
  489. if (aliases < 0) {
  490. puts("/aliases not found in device tree!\n");
  491. return -1;
  492. }
  493. pip_path = fdt_getprop(fdt, aliases, "pip", NULL);
  494. if (!pip_path) {
  495. puts("pip not found in aliases in device tree\n");
  496. return -1;
  497. }
  498. pip = fdt_path_offset(fdt, pip_path);
  499. if (pip < 0) {
  500. puts("pip not found in device tree\n");
  501. return -1;
  502. }
  503. snprintf(buffer, sizeof(buffer), "interface@%d", oct_eth_info->interface);
  504. interface = fdt_subnode_offset(fdt, pip, buffer);
  505. if (interface < 0) {
  506. printf("%s: interface@%d not found in device tree for %s\n", __func__,
  507. oct_eth_info->interface, eth->name);
  508. return -1;
  509. }
  510. snprintf(buffer, sizeof(buffer), "ethernet@%x", oct_eth_info->index);
  511. index = fdt_subnode_offset(fdt, interface, buffer);
  512. if (index < 0) {
  513. printf("%s: ethernet@%x not found in device tree for %s\n", __func__,
  514. oct_eth_info->index, eth->name);
  515. return -1;
  516. }
  517. phy_handle = (uint32_t *)fdt_getprop(fdt, index, "phy-handle", NULL);
  518. if (phy_handle < 0) {
  519. printf("%s: phy-handle not found for %s\n", __func__, eth->name);
  520. return -1;
  521. }
  522. phandle = fdt32_to_cpu(*phy_handle);
  523. phy = fdt_node_offset_by_phandle(fdt, phandle);
  524. if (phy < 0) {
  525. printf("%s: phy not found for %s\n", __func__, eth->name);
  526. return -1;
  527. }
  528. return phy;
  529. }
  530. /**
  531. * This helper function returns if a node contains the specified vendor name.
  532. *
  533. * @param[in] fdt pointer to device tree blob
  534. * @param nodeoffset offset of the tree node
  535. * @param[in] vendor name of vendor to check
  536. *
  537. * returns:
  538. * 0, if the node has a compatible vendor string property
  539. * 1, if the node does not contain the vendor string property
  540. * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
  541. * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
  542. * -FDT_ERR_BADMAGIC,
  543. * -FDT_ERR_BADVERSION,
  544. * -FDT_BADSTATE,
  545. * -FDT_ERR_BADSTRUCTURE, standard meanings
  546. */
  547. int octeon_fdt_compat_vendor(const void *fdt, int nodeoffset, const char *vendor)
  548. {
  549. const char *strlist;
  550. const char *p;
  551. int len;
  552. int listlen;
  553. strlist = fdt_getprop(fdt, nodeoffset, "compatible", &listlen);
  554. if (!strlist)
  555. return listlen;
  556. len = strlen(vendor);
  557. debug("%s(%p, %d, %s (%p)) strlist: %s (%p), len: %d\n", __func__, fdt, nodeoffset, vendor,
  558. vendor, strlist, strlist, len);
  559. while (listlen >= len) {
  560. debug(" Comparing %d bytes of %s and %s\n", len, vendor, strlist);
  561. if ((memcmp(vendor, strlist, len) == 0) &&
  562. ((strlist[len] == ',') || (strlist[len] == '\0')))
  563. return 0;
  564. p = memchr(strlist, '\0', listlen);
  565. if (!p)
  566. return 1; /* malformed strlist.. */
  567. listlen -= (p - strlist) + 1;
  568. strlist = p + 1;
  569. }
  570. return 1;
  571. }
  572. /**
  573. * Given a node in the device tree get the OCTEON OCX node number
  574. *
  575. * @param fdt pointer to flat device tree
  576. * @param nodeoffset node offset to get OCX node for
  577. *
  578. * Return: the Octeon OCX node number
  579. */
  580. int octeon_fdt_get_soc_node(const void *fdt, int nodeoffset)
  581. {
  582. return 0;
  583. }
  584. /**
  585. * Given a FDT node, check if it is compatible with a list of devices
  586. *
  587. * @param[in] fdt Flat device tree pointer
  588. * @param node_offset Node offset in device tree
  589. * @param[in] strlist Array of FDT devices to check, end must be NULL
  590. *
  591. * Return: 0 if at least one device is compatible, 1 if not compatible.
  592. */
  593. int octeon_fdt_node_check_compatible(const void *fdt, int node_offset,
  594. const char *const *strlist)
  595. {
  596. while (*strlist && **strlist) {
  597. debug("%s: Checking %s\n", __func__, *strlist);
  598. if (!fdt_node_check_compatible(fdt, node_offset, *strlist)) {
  599. debug("%s: match found\n", __func__);
  600. return 0;
  601. }
  602. strlist++;
  603. }
  604. debug("%s: No match found\n", __func__);
  605. return 1;
  606. }
  607. /**
  608. * Given a node offset, find the i2c bus number for that node
  609. *
  610. * @param[in] fdt Pointer to flat device tree
  611. * @param node_offset Node offset in device tree
  612. *
  613. * Return: i2c bus number or -1 if error
  614. */
  615. int octeon_fdt_i2c_get_bus(const void *fdt, int node_offset)
  616. {
  617. const char *compat;
  618. const u64 addresses[] = { 0x1180000001000, 0x1180000001200 };
  619. u64 reg;
  620. int i;
  621. int bus = -1;
  622. bool found = false;
  623. if (octeon_has_feature(OCTEON_FEATURE_CIU3))
  624. compat = "cavium,octeon-7890-twsi";
  625. else
  626. compat = "cavium,octeon-3860-twsi";
  627. while (node_offset > 0 &&
  628. !(found = !fdt_node_check_compatible(fdt, node_offset, compat))) {
  629. node_offset = fdt_parent_offset(fdt, node_offset);
  630. #ifdef CONFIG_OCTEON_I2C_FDT
  631. bus = i2c_get_bus_num_fdt(node_offset);
  632. if (bus >= 0) {
  633. debug("%s: Found bus 0x%x\n", __func__, bus);
  634. return bus;
  635. }
  636. #endif
  637. }
  638. if (!found) {
  639. printf("Error: node %d in device tree is not a child of the I2C bus\n",
  640. node_offset);
  641. return -1;
  642. }
  643. reg = fdtdec_get_addr(fdt, node_offset, "reg");
  644. if (reg == FDT_ADDR_T_NONE) {
  645. printf("%s: Error: invalid reg address for TWSI bus\n", __func__);
  646. return -1;
  647. }
  648. for (i = 0; i < ARRAY_SIZE(addresses); i++)
  649. if (reg == addresses[i]) {
  650. bus = i;
  651. break;
  652. }
  653. debug("%s: bus 0x%x\n", __func__, bus);
  654. return bus;
  655. }
  656. /**
  657. * Given an offset into the fdt, output the i2c bus and address of the device
  658. *
  659. * @param[in] fdt fdt blob pointer
  660. * @param node offset in FDT of device
  661. * @param[out] bus i2c bus number of device
  662. * @param[out] addr address of device on i2c bus
  663. *
  664. * Return: 0 for success, -1 on error
  665. */
  666. int octeon_fdt_get_i2c_bus_addr(const void *fdt, int node, int *bus, int *addr)
  667. {
  668. *bus = octeon_fdt_i2c_get_bus(fdt, fdt_parent_offset(fdt, node));
  669. if (*bus < 0) {
  670. printf("%s: Could not get parent i2c bus\n", __func__);
  671. return -1;
  672. }
  673. *addr = fdtdec_get_int(fdt, node, "reg", -1);
  674. if (*addr < 0)
  675. return -1;
  676. return 0;
  677. }
  678. /**
  679. * Reads a GPIO pin given the node of the GPIO device in the device tree and
  680. * the pin number.
  681. *
  682. * @param[in] fdt fdt blob pointer
  683. * @param phandle phandle of GPIO node
  684. * @param pin pin number to read
  685. *
  686. * Return: 0 = pin is low, 1 = pin is high, -1 = error
  687. */
  688. int octeon_fdt_read_gpio(const void *fdt, int phandle, int pin)
  689. {
  690. enum cvmx_gpio_type type;
  691. __maybe_unused int node;
  692. __maybe_unused int addr;
  693. __maybe_unused int bus;
  694. __maybe_unused int old_bus;
  695. int num_pins;
  696. int value;
  697. type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
  698. if ((pin & 0xff) >= num_pins) {
  699. debug("%s: pin number %d out of range\n", __func__, pin);
  700. return -1;
  701. }
  702. switch (type) {
  703. #ifdef CONFIG_PCA953X
  704. case CVMX_GPIO_PIN_PCA953X:
  705. node = fdt_node_offset_by_phandle(fdt, phandle);
  706. if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
  707. printf("%s: Could not get gpio bus and/or address\n", __func__);
  708. return -1;
  709. }
  710. value = pca953x_get_val(bus, addr);
  711. if (value < 0) {
  712. printf("%s: Error reading PCA953X GPIO at 0x%x:0x%x\n", __func__, bus,
  713. addr);
  714. return -1;
  715. }
  716. value = (value >> pin) & 1;
  717. break;
  718. #endif
  719. #ifdef CONFIG_PCF857X
  720. case CVMX_GPIO_PIN_PCF857X:
  721. node = fdt_node_offset_by_phandle(fdt, phandle);
  722. if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
  723. printf("%s: Could not get gpio bus and/or address\n", __func__);
  724. return -1;
  725. }
  726. value = pcf857x_get_val(bus, addr);
  727. if (value < 0) {
  728. printf("%s: Error reading PCF857X GPIO at 0x%x:0x%x\n", __func__, bus,
  729. addr);
  730. return -1;
  731. }
  732. value = (value >> pin) & 1;
  733. break;
  734. #endif
  735. #ifdef CONFIG_PCA9698
  736. case CVMX_GPIO_PIN_PCA9698:
  737. node = fdt_node_offset_by_phandle(fdt, phandle);
  738. if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
  739. printf("%s: Could not get gpio bus and/or address\n", __func__);
  740. return -1;
  741. }
  742. old_bus = i2c_get_bus_num();
  743. i2c_set_bus_num(bus);
  744. value = pca9698_get_value(addr, pin);
  745. i2c_set_bus_num(old_bus);
  746. break;
  747. #endif
  748. case CVMX_GPIO_PIN_OCTEON:
  749. value = gpio_get_value(pin);
  750. break;
  751. default:
  752. printf("%s: Unknown GPIO type %d\n", __func__, type);
  753. return -1;
  754. }
  755. return value;
  756. }
  757. /**
  758. * Reads a GPIO pin given the node of the GPIO device in the device tree and
  759. * the pin number.
  760. *
  761. * @param[in] fdt fdt blob pointer
  762. * @param phandle phandle of GPIO node
  763. * @param pin pin number to read
  764. * @param val value to write (1 = high, 0 = low)
  765. *
  766. * Return: 0 = success, -1 = error
  767. */
  768. int octeon_fdt_set_gpio(const void *fdt, int phandle, int pin, int val)
  769. {
  770. enum cvmx_gpio_type type;
  771. int node;
  772. int num_pins;
  773. __maybe_unused int addr;
  774. __maybe_unused int bus;
  775. __maybe_unused int old_bus;
  776. __maybe_unused int rc;
  777. node = fdt_node_offset_by_phandle(fdt, phandle);
  778. if (node < 0) {
  779. printf("%s: Invalid phandle\n", __func__);
  780. return -1;
  781. }
  782. type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
  783. if ((pin & 0xff) >= num_pins) {
  784. debug("%s: pin number %d out of range\n", __func__, pin);
  785. return -1;
  786. }
  787. switch (type) {
  788. #ifdef CONFIG_PCA953X
  789. case CVMX_GPIO_PIN_PCA953X:
  790. if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
  791. printf("%s: Could not get gpio bus and/or address\n", __func__);
  792. return -1;
  793. }
  794. return pca953x_set_val(bus, addr, 1 << pin, val << pin);
  795. #endif
  796. #ifdef CONFIG_PCF857X
  797. case CVMX_GPIO_PIN_PCF857X:
  798. if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
  799. printf("%s: Could not get gpio bus and/or address\n", __func__);
  800. return -1;
  801. }
  802. return pcf957x_set_val(bus, addr, 1 << pin, val << pin);
  803. #endif
  804. #ifdef CONFIG_PCA9698
  805. case CVMX_GPIO_PIN_PCA9698:
  806. if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
  807. printf("%s: Could not get gpio bus and/or address\n", __func__);
  808. return -1;
  809. }
  810. old_bus = i2c_get_bus_num();
  811. i2c_set_bus_num(bus);
  812. rc = pca9698_set_value(addr, pin, val);
  813. i2c_set_bus_num(old_bus);
  814. return rc;
  815. #endif
  816. case CVMX_GPIO_PIN_OCTEON:
  817. return gpio_set_value(pin, val);
  818. default:
  819. printf("%s: Unknown GPIO type %d\n", __func__, type);
  820. return -1;
  821. }
  822. }
  823. /**
  824. * Given the node of a GPIO entry output the GPIO type, i2c bus and i2c
  825. * address.
  826. *
  827. * @param fdt_node node of GPIO in device tree, generally
  828. * derived from a phandle.
  829. * @param[out] type Type of GPIO detected
  830. * @param[out] i2c_bus For i2c GPIO expanders, the i2c bus number
  831. * @param[out] i2c_addr For i2c GPIO expanders, the i2c address
  832. *
  833. * Return: 0 for success, -1 for errors
  834. *
  835. * NOTE: It is up to the caller to determine the pin number.
  836. */
  837. int octeon_fdt_get_gpio_info(int fdt_node, enum octeon_gpio_type *type,
  838. int *i2c_bus, int *i2c_addr)
  839. {
  840. const void *fdt = gd->fdt_blob;
  841. int i2c_bus_node __attribute__((unused));
  842. *type = GPIO_TYPE_UNKNOWN;
  843. if (!octeon_fdt_node_check_compatible(fdt, fdt_node, octeon_gpio_list)) {
  844. debug("%s: Found Octeon compatible GPIO\n", __func__);
  845. *type = GPIO_TYPE_OCTEON;
  846. if (i2c_bus)
  847. *i2c_bus = -1;
  848. if (i2c_addr)
  849. *i2c_addr = -1;
  850. return 0;
  851. }
  852. #ifdef CONFIG_PCA9555
  853. if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9555_gpio_list)) {
  854. debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
  855. *type = GPIO_TYPE_PCA9555;
  856. }
  857. #endif
  858. #ifdef CONFIG_PCA9554
  859. if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9554_gpio_list)) {
  860. debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
  861. *type = GPIO_TYPE_PCA9554;
  862. }
  863. #endif
  864. #ifdef CONFIG_PCA953X
  865. if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca953x_gpio_list)) {
  866. debug("%s: Found PCA953x compatible GPIO", __func__);
  867. *type = GPIO_TYPE_PCA953X;
  868. }
  869. #endif
  870. #ifdef CONFIG_PCA9698
  871. if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9698_gpio_list)) {
  872. debug("%s: Found PCA9698 compatible GPIO", __func__);
  873. *type = GPIO_TYPE_PCA9698;
  874. }
  875. #endif
  876. #if defined(CONFIG_PCA953X) || defined(CONFIG_PCA9698) || \
  877. defined(CONFIG_PCA9555) || defined(CONFIG_PCA9554)
  878. if (!i2c_addr || !i2c_bus) {
  879. printf("%s: Error: i2c_addr or i2c_bus is NULL\n", __func__);
  880. return -1;
  881. }
  882. *i2c_addr = fdtdec_get_int(fdt, fdt_node, "reg", -1);
  883. i2c_bus_node = fdt_parent_offset(fdt, fdt_node);
  884. if (i2c_bus_node < 0) {
  885. printf("%s: Invalid parent\n", __func__);
  886. return -1;
  887. }
  888. *i2c_bus = i2c_get_bus_num_fdt(i2c_bus_node);
  889. #endif
  890. return (*type != GPIO_TYPE_UNKNOWN) ? 0 : -1;
  891. }
  892. #ifdef CONFIG_PHY_VITESSE
  893. /**
  894. * Given a node in the flat device tree, return the matching PHY device
  895. *
  896. * @param fdt_node FDT node in device tree
  897. *
  898. * Return: pointer to PHY device or NULL if none found.
  899. */
  900. static struct phy_device *octeon_fdt_get_phy_device_from_node(int fdt_node)
  901. {
  902. struct eth_device *dev;
  903. int i = 0;
  904. struct octeon_eth_info *ethinfo = NULL;
  905. do {
  906. dev = eth_get_dev_by_index(i++);
  907. if (!dev)
  908. return NULL;
  909. ethinfo = dev->priv;
  910. if (ethinfo->phy_offset == fdt_node)
  911. return ethinfo->phydev;
  912. } while (dev);
  913. return NULL;
  914. }
  915. #endif
  916. /**
  917. * Get the PHY data structure for the specified FDT node and output the type
  918. *
  919. * @param fdt_node FDT node of phy
  920. * @param[out] type Type of GPIO
  921. *
  922. * Return: pointer to phy device or NULL if no match found.
  923. */
  924. struct phy_device *octeon_fdt_get_phy_gpio_info(int fdt_node, enum octeon_gpio_type *type)
  925. {
  926. #ifdef CONFIG_PHY_VITESSE
  927. struct phy_device *phydev;
  928. if (!octeon_fdt_node_check_compatible(gd->fdt_blob, fdt_node,
  929. vitesse_vsc8488_gpio_list)) {
  930. phydev = octeon_fdt_get_phy_device_from_node(fdt_node);
  931. if (phydev) {
  932. debug("%s: Found Vitesse VSC848X compatible GPIO\n", __func__);
  933. *type = GPIO_TYPE_VSC8488;
  934. return phydev;
  935. }
  936. debug("%s: Error: phy device not found!\n", __func__);
  937. return NULL;
  938. }
  939. debug("%s: No compatible Vitesse PHY type found\n", __func__);
  940. #endif
  941. return NULL;
  942. }