cvmx-helper-fdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2022 Marvell International Ltd.
  4. *
  5. * FDT Helper functions similar to those provided to U-Boot.
  6. */
  7. #include <dm.h>
  8. #include <i2c.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <linux/delay.h>
  13. #include <asm-generic/gpio.h>
  14. #include <mach/cvmx-regs.h>
  15. #include <mach/cvmx-csr.h>
  16. #include <mach/cvmx-bootmem.h>
  17. #include <mach/octeon-model.h>
  18. #include <mach/octeon_fdt.h>
  19. #include <mach/cvmx-helper.h>
  20. #include <mach/cvmx-helper-board.h>
  21. #include <mach/cvmx-helper-cfg.h>
  22. #include <mach/cvmx-helper-fdt.h>
  23. /**
  24. * Local allocator to handle both SE and U-Boot that also zeroes out memory
  25. *
  26. * @param size number of bytes to allocate
  27. *
  28. * Return: pointer to allocated memory or NULL if out of memory.
  29. * Alignment is set to 8-bytes.
  30. */
  31. static void *cvmx_fdt_alloc(size_t size)
  32. {
  33. return calloc(size, 1);
  34. }
  35. int cvmx_ofnode_lookup_phandles(ofnode node, const char *prop_name, int *lenp,
  36. ofnode *nodes)
  37. {
  38. const u32 *phandles;
  39. int count;
  40. int i;
  41. phandles = ofnode_get_property(node, prop_name, &count);
  42. if (!phandles || count < 0)
  43. return -FDT_ERR_NOTFOUND;
  44. count /= 4;
  45. if (count > *lenp)
  46. count = *lenp;
  47. for (i = 0; i < count; i++)
  48. nodes[i] = ofnode_get_by_phandle(fdt32_to_cpu(phandles[i]));
  49. *lenp = count;
  50. return 0;
  51. }
  52. /**
  53. * Given a FDT node return the CPU node number
  54. *
  55. * @param[in] fdt_addr Address of FDT
  56. * @param node FDT node number
  57. *
  58. * Return: CPU node number or error if negative
  59. */
  60. int cvmx_fdt_get_cpu_node(const void *fdt_addr, int node)
  61. {
  62. int parent = node;
  63. const u32 *ranges;
  64. int len = 0;
  65. while (fdt_node_check_compatible(fdt_addr, parent, "simple-bus") != 0) {
  66. parent = fdt_parent_offset(fdt_addr, parent);
  67. if (parent < 0)
  68. return parent;
  69. }
  70. ranges = fdt_getprop(fdt_addr, parent, "ranges", &len);
  71. if (!ranges)
  72. return len;
  73. if (len == 0)
  74. return 0;
  75. if (len < 24)
  76. return -FDT_ERR_TRUNCATED;
  77. return fdt32_to_cpu(ranges[2]) / 0x10;
  78. }
  79. /**
  80. * Parses all of the channels assigned to a VSC7224 device
  81. *
  82. * @param[in] fdt_addr Address of flat device tree
  83. * @param of_offset Offset of vsc7224 node
  84. * @param[in,out] vsc7224 Data structure to hold the data
  85. *
  86. * Return: 0 for success, -1 on error
  87. */
  88. static int cvmx_fdt_parse_vsc7224_channels(ofnode node,
  89. struct cvmx_vsc7224 *vsc7224)
  90. {
  91. struct ofnode_phandle_args phandle;
  92. int err = 0;
  93. int reg;
  94. int num_chan = 0;
  95. struct cvmx_vsc7224_chan *channel;
  96. struct cvmx_fdt_sfp_info *sfp_info;
  97. int len;
  98. int num_taps;
  99. int i;
  100. const u32 *tap_values;
  101. int of_mac;
  102. int xiface, index;
  103. bool is_tx;
  104. bool is_qsfp;
  105. const char *mac_str;
  106. ofnode node_chan;
  107. debug("%s(%x, %s)\n", __func__, ofnode_to_offset(node), vsc7224->name);
  108. ofnode_for_each_compatible_node(node_chan, "vitesse,vsc7224-channel") {
  109. if (!ofnode_valid(node_chan)) {
  110. debug("%s: Error parsing FDT node %s\n",
  111. __func__, ofnode_get_name(node));
  112. break;
  113. }
  114. if (ofnode_to_offset(ofnode_get_parent(node_chan)) !=
  115. ofnode_to_offset(node))
  116. break;
  117. reg = ofnode_get_addr(node_chan);
  118. if (reg < 0 || reg > 3) {
  119. debug("%s: channel reg is either not present or out of range\n",
  120. __func__);
  121. err = -1;
  122. break;
  123. }
  124. is_tx = ofnode_read_bool(node_chan, "direction-tx");
  125. debug("%s(%s): Adding %cx channel %d\n",
  126. __func__, vsc7224->name, is_tx ? 't' : 'r',
  127. reg);
  128. tap_values = ofnode_get_property(node_chan, "taps", &len);
  129. if (!tap_values) {
  130. debug("%s: Error: no taps defined for vsc7224 channel %d\n",
  131. __func__, reg);
  132. err = -1;
  133. break;
  134. }
  135. if (vsc7224->channel[reg]) {
  136. debug("%s: Error: channel %d already assigned at %p\n",
  137. __func__, reg,
  138. vsc7224->channel[reg]);
  139. err = -1;
  140. break;
  141. }
  142. if (len % 16) {
  143. debug("%s: Error: tap format error for channel %d\n",
  144. __func__, reg);
  145. err = -1;
  146. break;
  147. }
  148. num_taps = len / 16;
  149. debug("%s: Adding %d taps\n", __func__, num_taps);
  150. channel = cvmx_fdt_alloc(sizeof(*channel) +
  151. num_taps * sizeof(struct cvmx_vsc7224_tap));
  152. if (!channel) {
  153. debug("%s: Out of memory\n", __func__);
  154. err = -1;
  155. break;
  156. }
  157. vsc7224->channel[reg] = channel;
  158. channel->num_taps = num_taps;
  159. channel->lane = reg;
  160. channel->of_offset = ofnode_to_offset(node_chan);
  161. channel->is_tx = is_tx;
  162. channel->pretap_disable = ofnode_read_bool(node_chan,
  163. "pretap-disable");
  164. channel->posttap_disable = ofnode_read_bool(node_chan,
  165. "posttap-disable");
  166. channel->vsc7224 = vsc7224;
  167. /* Read all the tap values */
  168. for (i = 0; i < num_taps; i++) {
  169. channel->taps[i].len = fdt32_to_cpu(tap_values[i * 4 + 0]);
  170. channel->taps[i].main_tap = fdt32_to_cpu(tap_values[i * 4 + 1]);
  171. channel->taps[i].pre_tap = fdt32_to_cpu(tap_values[i * 4 + 2]);
  172. channel->taps[i].post_tap = fdt32_to_cpu(tap_values[i * 4 + 3]);
  173. debug("%s: tap %d: len: %d, main_tap: 0x%x, pre_tap: 0x%x, post_tap: 0x%x\n",
  174. __func__, i, channel->taps[i].len, channel->taps[i].main_tap,
  175. channel->taps[i].pre_tap, channel->taps[i].post_tap);
  176. }
  177. /* Now find out which interface it's mapped to */
  178. channel->ipd_port = -1;
  179. mac_str = "sfp-mac";
  180. if (ofnode_get_property(node_chan, mac_str, NULL)) {
  181. is_qsfp = false;
  182. } else if (ofnode_get_property(node_chan, "qsfp-mac", NULL)) {
  183. is_qsfp = true;
  184. mac_str = "qsfp-mac";
  185. } else {
  186. debug("%s: Error: MAC not found for %s channel %d\n", __func__,
  187. vsc7224->name, reg);
  188. return -1;
  189. }
  190. err = ofnode_parse_phandle_with_args(node_chan, mac_str, NULL,
  191. 0, 0, &phandle);
  192. if (err) {
  193. debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
  194. mac_str, vsc7224->name);
  195. return -1;
  196. }
  197. debug("%s: Found mac at %s\n", __func__,
  198. ofnode_get_name(phandle.node));
  199. xiface = (ofnode_get_addr(ofnode_get_parent(phandle.node))
  200. >> 24) & 0x0f;
  201. index = ofnode_get_addr(phandle.node);
  202. channel->xiface = xiface;
  203. channel->index = index;
  204. channel->ipd_port = cvmx_helper_get_ipd_port(xiface, index);
  205. debug("%s: Found MAC, xiface: 0x%x, index: %d, ipd port: %d\n", __func__,
  206. xiface, index, channel->ipd_port);
  207. if (channel->ipd_port >= 0) {
  208. cvmx_helper_cfg_set_vsc7224_chan_info(xiface, index, channel);
  209. debug("%s: Storing config channel for xiface 0x%x, index %d\n",
  210. __func__, xiface, index);
  211. }
  212. sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
  213. if (!sfp_info) {
  214. debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d for channel %d\n",
  215. __func__, xiface, index, channel->lane);
  216. continue;
  217. }
  218. /* Link it */
  219. channel->next = sfp_info->vsc7224_chan;
  220. if (sfp_info->vsc7224_chan)
  221. sfp_info->vsc7224_chan->prev = channel;
  222. sfp_info->vsc7224_chan = channel;
  223. sfp_info->is_vsc7224 = true;
  224. debug("%s: Registering VSC7224 %s channel %d with SFP %s\n", __func__,
  225. vsc7224->name, channel->lane, sfp_info->name);
  226. if (!sfp_info->mod_abs_changed) {
  227. debug("%s: Registering cvmx_sfp_vsc7224_mod_abs_changed at %p for xinterface 0x%x, index %d\n",
  228. __func__, &cvmx_sfp_vsc7224_mod_abs_changed, xiface, index);
  229. cvmx_sfp_register_mod_abs_changed(
  230. sfp_info,
  231. &cvmx_sfp_vsc7224_mod_abs_changed,
  232. NULL);
  233. }
  234. if (num_chan >= 4)
  235. break;
  236. }
  237. return err;
  238. }
  239. /**
  240. * @INTERNAL
  241. * Parses all instances of the Vitesse VSC7224 reclocking chip
  242. *
  243. * @param[in] fdt_addr Address of flat device tree
  244. *
  245. * Return: 0 for success, error otherwise
  246. */
  247. int __cvmx_fdt_parse_vsc7224(const void *fdt_addr)
  248. {
  249. struct cvmx_vsc7224 *vsc7224 = NULL;
  250. ofnode node;
  251. int err = 0;
  252. static bool parsed;
  253. const int *init_array;
  254. struct udevice *dev;
  255. u16 value;
  256. int reg;
  257. int len;
  258. int ret;
  259. int i;
  260. debug("%s(%p)\n", __func__, fdt_addr);
  261. if (parsed) {
  262. debug("%s: Already parsed\n", __func__);
  263. return 0;
  264. }
  265. ofnode_for_each_compatible_node(node, "vitesse,vsc7224") {
  266. if (!ofnode_valid(node)) {
  267. debug("%s: Error parsing FDT node %s\n",
  268. __func__, ofnode_get_name(node));
  269. break;
  270. }
  271. vsc7224 = cvmx_fdt_alloc(sizeof(*vsc7224));
  272. if (!vsc7224) {
  273. debug("%s: Out of memory!\n", __func__);
  274. return -1;
  275. }
  276. vsc7224->of_offset = ofnode_to_offset(node);
  277. vsc7224->i2c_addr = ofnode_get_addr(node);
  278. vsc7224->i2c_bus = cvmx_ofnode_get_i2c_bus(ofnode_get_parent(node));
  279. if (vsc7224->i2c_addr < 0) {
  280. debug("%s: Error: reg field missing\n", __func__);
  281. err = -1;
  282. break;
  283. }
  284. if (!vsc7224->i2c_bus) {
  285. debug("%s: Error getting i2c bus\n", __func__);
  286. err = -1;
  287. break;
  288. }
  289. vsc7224->name = ofnode_get_name(node);
  290. debug("%s: Adding %s\n", __func__, vsc7224->name);
  291. err = gpio_request_by_name_nodev(node, "reset", 0,
  292. &vsc7224->reset_gpio,
  293. GPIOD_IS_OUT);
  294. if (err) {
  295. printf("%s: reset GPIO not found in DT!\n", __func__);
  296. return -ENODEV;
  297. }
  298. err = gpio_request_by_name_nodev(node, "los", 0,
  299. &vsc7224->los_gpio,
  300. GPIOD_IS_IN);
  301. if (err) {
  302. printf("%s: los GPIO not found in DT!\n", __func__);
  303. return -ENODEV;
  304. }
  305. /*
  306. * This code was taken from the NIC23 board specific code
  307. * but should be better placed here in the common code
  308. */
  309. debug("%s: Putting device in reset\n", __func__);
  310. dm_gpio_set_value(&vsc7224->reset_gpio, 1);
  311. mdelay(10);
  312. debug("%s: Taking device out of reset\n", __func__);
  313. dm_gpio_set_value(&vsc7224->reset_gpio, 0);
  314. mdelay(50);
  315. init_array = ofnode_get_property(node, "vitesse,reg-init",
  316. &len);
  317. if (!init_array) {
  318. debug("%s: No initialization array\n", __func__);
  319. continue;
  320. }
  321. if ((len % 8) != 0) {
  322. printf("%s: Error: register init string should be an array of reg number followed by value\n",
  323. __func__);
  324. return -1;
  325. }
  326. ret = i2c_get_chip(vsc7224->i2c_bus->i2c_bus,
  327. vsc7224->i2c_addr, 1, &dev);
  328. if (ret) {
  329. debug("Cannot find I2C device: %d\n", ret);
  330. return -1;
  331. }
  332. for (i = 0; i < len / sizeof(int); i += 2) {
  333. u8 buffer[2];
  334. reg = fdt32_to_cpu(init_array[i]);
  335. value = fdt32_to_cpu(init_array[i + 1]);
  336. buffer[0] = value >> 8;
  337. buffer[1] = value & 0xff;
  338. ret = dm_i2c_write(dev, reg, buffer, 2);
  339. if (ret) {
  340. debug("Cannot write I2C device: %d\n", ret);
  341. return -1;
  342. }
  343. debug(" Wrote 0x%02x <= 0x%02x%02x\n", reg,
  344. buffer[0], buffer[1]);
  345. }
  346. debug("%s: Parsing channels\n", __func__);
  347. err = cvmx_fdt_parse_vsc7224_channels(node, vsc7224);
  348. if (err) {
  349. debug("%s: Error parsing VSC7224 channels\n", __func__);
  350. break;
  351. }
  352. }
  353. if (err) {
  354. debug("%s(): Error\n", __func__);
  355. if (vsc7224) {
  356. dm_gpio_free(vsc7224->reset_gpio.dev,
  357. &vsc7224->reset_gpio);
  358. dm_gpio_free(vsc7224->los_gpio.dev,
  359. &vsc7224->los_gpio);
  360. if (vsc7224->i2c_bus)
  361. cvmx_fdt_free_i2c_bus(vsc7224->i2c_bus);
  362. free(vsc7224);
  363. }
  364. }
  365. if (!err)
  366. parsed = true;
  367. return err;
  368. }
  369. /**
  370. * Given the parent offset of an i2c device build up a list describing the bus
  371. * which can contain i2c muxes and switches.
  372. *
  373. * @param[in] node ofnode of the parent node of a GPIO device in
  374. * the device tree.
  375. *
  376. * @return pointer to list of i2c devices starting from the root which
  377. * can include i2c muxes and switches or NULL if error. Note that
  378. * all entries are allocated on the heap.
  379. *
  380. * @see cvmx_fdt_free_i2c_bus()
  381. */
  382. struct cvmx_fdt_i2c_bus_info *cvmx_ofnode_get_i2c_bus(ofnode node)
  383. {
  384. struct cvmx_fdt_i2c_bus_info *businfo = NULL;
  385. struct udevice *bus;
  386. int ret;
  387. businfo = cvmx_fdt_alloc(sizeof(*businfo));
  388. if (!businfo) {
  389. debug("Out of memory\n");
  390. return NULL;
  391. }
  392. debug("%s: Found node %s\n", __func__, ofnode_get_name(node));
  393. businfo->of_offset = ofnode_to_offset(node);
  394. /*
  395. * Get I2C bus and probe it automatically - needed for later use
  396. */
  397. ret = device_get_global_by_ofnode(node, &bus);
  398. if (!bus || ret) {
  399. printf("Cannot find a I2C bus\n");
  400. return NULL;
  401. }
  402. businfo->i2c_bus = bus;
  403. return businfo;
  404. }
  405. /**
  406. * Return the Octeon bus number for a bus descriptor
  407. *
  408. * @param[in] bus bus descriptor
  409. *
  410. * @return Octeon twsi bus number or -1 on error
  411. */
  412. int cvmx_fdt_i2c_get_root_bus(const struct cvmx_fdt_i2c_bus_info *bus)
  413. {
  414. if (bus->type != CVMX_I2C_BUS_OCTEON)
  415. return -1;
  416. return bus->channel;
  417. }
  418. /**
  419. * Frees all entries for an i2c bus descriptor
  420. *
  421. * @param bus bus to free
  422. *
  423. * @return 0
  424. */
  425. int cvmx_fdt_free_i2c_bus(struct cvmx_fdt_i2c_bus_info *bus)
  426. {
  427. struct cvmx_fdt_i2c_bus_info *last;
  428. while (bus) {
  429. last = bus;
  430. bus = bus->child;
  431. free(last);
  432. }
  433. return 0;
  434. }