octeon-platform.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2004-2017 Cavium, Inc.
  7. * Copyright (C) 2008 Wind River Systems
  8. */
  9. #include <linux/etherdevice.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/of_fdt.h>
  12. #include <linux/libfdt.h>
  13. #include <asm/octeon/octeon.h>
  14. #include <asm/octeon/cvmx-helper-board.h>
  15. #ifdef CONFIG_USB
  16. #include <linux/usb/ehci_def.h>
  17. #include <linux/usb/ehci_pdriver.h>
  18. #include <linux/usb/ohci_pdriver.h>
  19. #include <asm/octeon/cvmx-uctlx-defs.h>
  20. #define CVMX_UAHCX_EHCI_USBCMD (CVMX_ADD_IO_SEG(0x00016F0000000010ull))
  21. #define CVMX_UAHCX_OHCI_USBCMD (CVMX_ADD_IO_SEG(0x00016F0000000408ull))
  22. static DEFINE_MUTEX(octeon2_usb_clocks_mutex);
  23. static int octeon2_usb_clock_start_cnt;
  24. static int __init octeon2_usb_reset(void)
  25. {
  26. union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;
  27. u32 ucmd;
  28. if (!OCTEON_IS_OCTEON2())
  29. return 0;
  30. clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));
  31. if (clk_rst_ctl.s.hrst) {
  32. ucmd = cvmx_read64_uint32(CVMX_UAHCX_EHCI_USBCMD);
  33. ucmd &= ~CMD_RUN;
  34. cvmx_write64_uint32(CVMX_UAHCX_EHCI_USBCMD, ucmd);
  35. mdelay(2);
  36. ucmd |= CMD_RESET;
  37. cvmx_write64_uint32(CVMX_UAHCX_EHCI_USBCMD, ucmd);
  38. ucmd = cvmx_read64_uint32(CVMX_UAHCX_OHCI_USBCMD);
  39. ucmd |= CMD_RUN;
  40. cvmx_write64_uint32(CVMX_UAHCX_OHCI_USBCMD, ucmd);
  41. }
  42. return 0;
  43. }
  44. arch_initcall(octeon2_usb_reset);
  45. static void octeon2_usb_clocks_start(struct device *dev)
  46. {
  47. u64 div;
  48. union cvmx_uctlx_if_ena if_ena;
  49. union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;
  50. union cvmx_uctlx_uphy_portx_ctl_status port_ctl_status;
  51. int i;
  52. unsigned long io_clk_64_to_ns;
  53. u32 clock_rate = 12000000;
  54. bool is_crystal_clock = false;
  55. mutex_lock(&octeon2_usb_clocks_mutex);
  56. octeon2_usb_clock_start_cnt++;
  57. if (octeon2_usb_clock_start_cnt != 1)
  58. goto exit;
  59. io_clk_64_to_ns = 64000000000ull / octeon_get_io_clock_rate();
  60. if (dev->of_node) {
  61. struct device_node *uctl_node;
  62. const char *clock_type;
  63. uctl_node = of_get_parent(dev->of_node);
  64. if (!uctl_node) {
  65. dev_err(dev, "No UCTL device node\n");
  66. goto exit;
  67. }
  68. i = of_property_read_u32(uctl_node,
  69. "refclk-frequency", &clock_rate);
  70. if (i) {
  71. dev_err(dev, "No UCTL \"refclk-frequency\"\n");
  72. of_node_put(uctl_node);
  73. goto exit;
  74. }
  75. i = of_property_read_string(uctl_node,
  76. "refclk-type", &clock_type);
  77. of_node_put(uctl_node);
  78. if (!i && strcmp("crystal", clock_type) == 0)
  79. is_crystal_clock = true;
  80. }
  81. /*
  82. * Step 1: Wait for voltages stable. That surely happened
  83. * before starting the kernel.
  84. *
  85. * Step 2: Enable SCLK of UCTL by writing UCTL0_IF_ENA[EN] = 1
  86. */
  87. if_ena.u64 = 0;
  88. if_ena.s.en = 1;
  89. cvmx_write_csr(CVMX_UCTLX_IF_ENA(0), if_ena.u64);
  90. for (i = 0; i <= 1; i++) {
  91. port_ctl_status.u64 =
  92. cvmx_read_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0));
  93. /* Set txvreftune to 15 to obtain compliant 'eye' diagram. */
  94. port_ctl_status.s.txvreftune = 15;
  95. port_ctl_status.s.txrisetune = 1;
  96. port_ctl_status.s.txpreemphasistune = 1;
  97. cvmx_write_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0),
  98. port_ctl_status.u64);
  99. }
  100. /* Step 3: Configure the reference clock, PHY, and HCLK */
  101. clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));
  102. /*
  103. * If the UCTL looks like it has already been started, skip
  104. * the initialization, otherwise bus errors are obtained.
  105. */
  106. if (clk_rst_ctl.s.hrst)
  107. goto end_clock;
  108. /* 3a */
  109. clk_rst_ctl.s.p_por = 1;
  110. clk_rst_ctl.s.hrst = 0;
  111. clk_rst_ctl.s.p_prst = 0;
  112. clk_rst_ctl.s.h_clkdiv_rst = 0;
  113. clk_rst_ctl.s.o_clkdiv_rst = 0;
  114. clk_rst_ctl.s.h_clkdiv_en = 0;
  115. clk_rst_ctl.s.o_clkdiv_en = 0;
  116. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  117. /* 3b */
  118. clk_rst_ctl.s.p_refclk_sel = is_crystal_clock ? 0 : 1;
  119. switch (clock_rate) {
  120. default:
  121. pr_err("Invalid UCTL clock rate of %u, using 12000000 instead\n",
  122. clock_rate);
  123. fallthrough;
  124. case 12000000:
  125. clk_rst_ctl.s.p_refclk_div = 0;
  126. break;
  127. case 24000000:
  128. clk_rst_ctl.s.p_refclk_div = 1;
  129. break;
  130. case 48000000:
  131. clk_rst_ctl.s.p_refclk_div = 2;
  132. break;
  133. }
  134. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  135. /* 3c */
  136. div = octeon_get_io_clock_rate() / 130000000ull;
  137. switch (div) {
  138. case 0:
  139. div = 1;
  140. break;
  141. case 1:
  142. case 2:
  143. case 3:
  144. case 4:
  145. break;
  146. case 5:
  147. div = 4;
  148. break;
  149. case 6:
  150. case 7:
  151. div = 6;
  152. break;
  153. case 8:
  154. case 9:
  155. case 10:
  156. case 11:
  157. div = 8;
  158. break;
  159. default:
  160. div = 12;
  161. break;
  162. }
  163. clk_rst_ctl.s.h_div = div;
  164. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  165. /* Read it back, */
  166. clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));
  167. clk_rst_ctl.s.h_clkdiv_en = 1;
  168. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  169. /* 3d */
  170. clk_rst_ctl.s.h_clkdiv_rst = 1;
  171. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  172. /* 3e: delay 64 io clocks */
  173. ndelay(io_clk_64_to_ns);
  174. /*
  175. * Step 4: Program the power-on reset field in the UCTL
  176. * clock-reset-control register.
  177. */
  178. clk_rst_ctl.s.p_por = 0;
  179. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  180. /* Step 5: Wait 3 ms for the PHY clock to start. */
  181. mdelay(3);
  182. /* Steps 6..9 for ATE only, are skipped. */
  183. /* Step 10: Configure the OHCI_CLK48 and OHCI_CLK12 clocks. */
  184. /* 10a */
  185. clk_rst_ctl.s.o_clkdiv_rst = 1;
  186. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  187. /* 10b */
  188. clk_rst_ctl.s.o_clkdiv_en = 1;
  189. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  190. /* 10c */
  191. ndelay(io_clk_64_to_ns);
  192. /*
  193. * Step 11: Program the PHY reset field:
  194. * UCTL0_CLK_RST_CTL[P_PRST] = 1
  195. */
  196. clk_rst_ctl.s.p_prst = 1;
  197. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  198. /* Step 11b */
  199. udelay(1);
  200. /* Step 11c */
  201. clk_rst_ctl.s.p_prst = 0;
  202. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  203. /* Step 11d */
  204. mdelay(1);
  205. /* Step 11e */
  206. clk_rst_ctl.s.p_prst = 1;
  207. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  208. /* Step 12: Wait 1 uS. */
  209. udelay(1);
  210. /* Step 13: Program the HRESET_N field: UCTL0_CLK_RST_CTL[HRST] = 1 */
  211. clk_rst_ctl.s.hrst = 1;
  212. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  213. end_clock:
  214. /* Set uSOF cycle period to 60,000 bits. */
  215. cvmx_write_csr(CVMX_UCTLX_EHCI_FLA(0), 0x20ull);
  216. exit:
  217. mutex_unlock(&octeon2_usb_clocks_mutex);
  218. }
  219. static void octeon2_usb_clocks_stop(void)
  220. {
  221. mutex_lock(&octeon2_usb_clocks_mutex);
  222. octeon2_usb_clock_start_cnt--;
  223. mutex_unlock(&octeon2_usb_clocks_mutex);
  224. }
  225. static int octeon_ehci_power_on(struct platform_device *pdev)
  226. {
  227. octeon2_usb_clocks_start(&pdev->dev);
  228. return 0;
  229. }
  230. static void octeon_ehci_power_off(struct platform_device *pdev)
  231. {
  232. octeon2_usb_clocks_stop();
  233. }
  234. static struct usb_ehci_pdata octeon_ehci_pdata = {
  235. /* Octeon EHCI matches CPU endianness. */
  236. #ifdef __BIG_ENDIAN
  237. .big_endian_mmio = 1,
  238. #endif
  239. /*
  240. * We can DMA from anywhere. But the descriptors must be in
  241. * the lower 4GB.
  242. */
  243. .dma_mask_64 = 0,
  244. .power_on = octeon_ehci_power_on,
  245. .power_off = octeon_ehci_power_off,
  246. };
  247. static void __init octeon_ehci_hw_start(struct device *dev)
  248. {
  249. union cvmx_uctlx_ehci_ctl ehci_ctl;
  250. octeon2_usb_clocks_start(dev);
  251. ehci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_EHCI_CTL(0));
  252. /* Use 64-bit addressing. */
  253. ehci_ctl.s.ehci_64b_addr_en = 1;
  254. ehci_ctl.s.l2c_addr_msb = 0;
  255. #ifdef __BIG_ENDIAN
  256. ehci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */
  257. ehci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */
  258. #else
  259. ehci_ctl.s.l2c_buff_emod = 0; /* not swapped. */
  260. ehci_ctl.s.l2c_desc_emod = 0; /* not swapped. */
  261. ehci_ctl.s.inv_reg_a2 = 1;
  262. #endif
  263. cvmx_write_csr(CVMX_UCTLX_EHCI_CTL(0), ehci_ctl.u64);
  264. octeon2_usb_clocks_stop();
  265. }
  266. static int __init octeon_ehci_device_init(void)
  267. {
  268. struct platform_device *pd;
  269. struct device_node *ehci_node;
  270. int ret = 0;
  271. ehci_node = of_find_node_by_name(NULL, "ehci");
  272. if (!ehci_node)
  273. return 0;
  274. pd = of_find_device_by_node(ehci_node);
  275. of_node_put(ehci_node);
  276. if (!pd)
  277. return 0;
  278. pd->dev.platform_data = &octeon_ehci_pdata;
  279. octeon_ehci_hw_start(&pd->dev);
  280. put_device(&pd->dev);
  281. return ret;
  282. }
  283. device_initcall(octeon_ehci_device_init);
  284. static int octeon_ohci_power_on(struct platform_device *pdev)
  285. {
  286. octeon2_usb_clocks_start(&pdev->dev);
  287. return 0;
  288. }
  289. static void octeon_ohci_power_off(struct platform_device *pdev)
  290. {
  291. octeon2_usb_clocks_stop();
  292. }
  293. static struct usb_ohci_pdata octeon_ohci_pdata = {
  294. /* Octeon OHCI matches CPU endianness. */
  295. #ifdef __BIG_ENDIAN
  296. .big_endian_mmio = 1,
  297. #endif
  298. .power_on = octeon_ohci_power_on,
  299. .power_off = octeon_ohci_power_off,
  300. };
  301. static void __init octeon_ohci_hw_start(struct device *dev)
  302. {
  303. union cvmx_uctlx_ohci_ctl ohci_ctl;
  304. octeon2_usb_clocks_start(dev);
  305. ohci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_OHCI_CTL(0));
  306. ohci_ctl.s.l2c_addr_msb = 0;
  307. #ifdef __BIG_ENDIAN
  308. ohci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */
  309. ohci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */
  310. #else
  311. ohci_ctl.s.l2c_buff_emod = 0; /* not swapped. */
  312. ohci_ctl.s.l2c_desc_emod = 0; /* not swapped. */
  313. ohci_ctl.s.inv_reg_a2 = 1;
  314. #endif
  315. cvmx_write_csr(CVMX_UCTLX_OHCI_CTL(0), ohci_ctl.u64);
  316. octeon2_usb_clocks_stop();
  317. }
  318. static int __init octeon_ohci_device_init(void)
  319. {
  320. struct platform_device *pd;
  321. struct device_node *ohci_node;
  322. int ret = 0;
  323. ohci_node = of_find_node_by_name(NULL, "ohci");
  324. if (!ohci_node)
  325. return 0;
  326. pd = of_find_device_by_node(ohci_node);
  327. of_node_put(ohci_node);
  328. if (!pd)
  329. return 0;
  330. pd->dev.platform_data = &octeon_ohci_pdata;
  331. octeon_ohci_hw_start(&pd->dev);
  332. put_device(&pd->dev);
  333. return ret;
  334. }
  335. device_initcall(octeon_ohci_device_init);
  336. #endif /* CONFIG_USB */
  337. /* Octeon Random Number Generator. */
  338. static int __init octeon_rng_device_init(void)
  339. {
  340. struct platform_device *pd;
  341. int ret = 0;
  342. struct resource rng_resources[] = {
  343. {
  344. .flags = IORESOURCE_MEM,
  345. .start = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS),
  346. .end = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS) + 0xf
  347. }, {
  348. .flags = IORESOURCE_MEM,
  349. .start = cvmx_build_io_address(8, 0),
  350. .end = cvmx_build_io_address(8, 0) + 0x7
  351. }
  352. };
  353. pd = platform_device_alloc("octeon_rng", -1);
  354. if (!pd) {
  355. ret = -ENOMEM;
  356. goto out;
  357. }
  358. ret = platform_device_add_resources(pd, rng_resources,
  359. ARRAY_SIZE(rng_resources));
  360. if (ret)
  361. goto fail;
  362. ret = platform_device_add(pd);
  363. if (ret)
  364. goto fail;
  365. return ret;
  366. fail:
  367. platform_device_put(pd);
  368. out:
  369. return ret;
  370. }
  371. device_initcall(octeon_rng_device_init);
  372. static const struct of_device_id octeon_ids[] __initconst = {
  373. { .compatible = "simple-bus", },
  374. { .compatible = "cavium,octeon-6335-uctl", },
  375. { .compatible = "cavium,octeon-5750-usbn", },
  376. { .compatible = "cavium,octeon-3860-bootbus", },
  377. { .compatible = "cavium,mdio-mux", },
  378. { .compatible = "gpio-leds", },
  379. { .compatible = "cavium,octeon-7130-usb-uctl", },
  380. {},
  381. };
  382. static bool __init octeon_has_88e1145(void)
  383. {
  384. return !OCTEON_IS_MODEL(OCTEON_CN52XX) &&
  385. !OCTEON_IS_MODEL(OCTEON_CN6XXX) &&
  386. !OCTEON_IS_MODEL(OCTEON_CN56XX);
  387. }
  388. static bool __init octeon_has_fixed_link(int ipd_port)
  389. {
  390. switch (cvmx_sysinfo_get()->board_type) {
  391. case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
  392. case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
  393. case CVMX_BOARD_TYPE_CN3020_EVB_HS5:
  394. case CVMX_BOARD_TYPE_CUST_NB5:
  395. case CVMX_BOARD_TYPE_EBH3100:
  396. /* Port 1 on these boards is always gigabit. */
  397. return ipd_port == 1;
  398. case CVMX_BOARD_TYPE_BBGW_REF:
  399. /* Ports 0 and 1 connect to the switch. */
  400. return ipd_port == 0 || ipd_port == 1;
  401. }
  402. return false;
  403. }
  404. static void __init octeon_fdt_set_phy(int eth, int phy_addr)
  405. {
  406. const __be32 *phy_handle;
  407. const __be32 *alt_phy_handle;
  408. const __be32 *reg;
  409. u32 phandle;
  410. int phy;
  411. int alt_phy;
  412. const char *p;
  413. int current_len;
  414. char new_name[20];
  415. phy_handle = fdt_getprop(initial_boot_params, eth, "phy-handle", NULL);
  416. if (!phy_handle)
  417. return;
  418. phandle = be32_to_cpup(phy_handle);
  419. phy = fdt_node_offset_by_phandle(initial_boot_params, phandle);
  420. alt_phy_handle = fdt_getprop(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);
  421. if (alt_phy_handle) {
  422. u32 alt_phandle = be32_to_cpup(alt_phy_handle);
  423. alt_phy = fdt_node_offset_by_phandle(initial_boot_params, alt_phandle);
  424. } else {
  425. alt_phy = -1;
  426. }
  427. if (phy_addr < 0 || phy < 0) {
  428. /* Delete the PHY things */
  429. fdt_nop_property(initial_boot_params, eth, "phy-handle");
  430. /* This one may fail */
  431. fdt_nop_property(initial_boot_params, eth, "cavium,alt-phy-handle");
  432. if (phy >= 0)
  433. fdt_nop_node(initial_boot_params, phy);
  434. if (alt_phy >= 0)
  435. fdt_nop_node(initial_boot_params, alt_phy);
  436. return;
  437. }
  438. if (phy_addr >= 256 && alt_phy > 0) {
  439. const struct fdt_property *phy_prop;
  440. struct fdt_property *alt_prop;
  441. fdt32_t phy_handle_name;
  442. /* Use the alt phy node instead.*/
  443. phy_prop = fdt_get_property(initial_boot_params, eth, "phy-handle", NULL);
  444. phy_handle_name = phy_prop->nameoff;
  445. fdt_nop_node(initial_boot_params, phy);
  446. fdt_nop_property(initial_boot_params, eth, "phy-handle");
  447. alt_prop = fdt_get_property_w(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);
  448. alt_prop->nameoff = phy_handle_name;
  449. phy = alt_phy;
  450. }
  451. phy_addr &= 0xff;
  452. if (octeon_has_88e1145()) {
  453. fdt_nop_property(initial_boot_params, phy, "marvell,reg-init");
  454. memset(new_name, 0, sizeof(new_name));
  455. strcpy(new_name, "marvell,88e1145");
  456. p = fdt_getprop(initial_boot_params, phy, "compatible",
  457. &current_len);
  458. if (p && current_len >= strlen(new_name))
  459. fdt_setprop_inplace(initial_boot_params, phy,
  460. "compatible", new_name, current_len);
  461. }
  462. reg = fdt_getprop(initial_boot_params, phy, "reg", NULL);
  463. if (phy_addr == be32_to_cpup(reg))
  464. return;
  465. fdt_setprop_inplace_cell(initial_boot_params, phy, "reg", phy_addr);
  466. snprintf(new_name, sizeof(new_name), "ethernet-phy@%x", phy_addr);
  467. p = fdt_get_name(initial_boot_params, phy, &current_len);
  468. if (p && current_len == strlen(new_name))
  469. fdt_set_name(initial_boot_params, phy, new_name);
  470. else
  471. pr_err("Error: could not rename ethernet phy: <%s>", p);
  472. }
  473. static void __init octeon_fdt_set_mac_addr(int n, u64 *pmac)
  474. {
  475. const u8 *old_mac;
  476. int old_len;
  477. u8 new_mac[6];
  478. u64 mac = *pmac;
  479. int r;
  480. old_mac = fdt_getprop(initial_boot_params, n, "local-mac-address",
  481. &old_len);
  482. if (!old_mac || old_len != 6 || is_valid_ether_addr(old_mac))
  483. return;
  484. new_mac[0] = (mac >> 40) & 0xff;
  485. new_mac[1] = (mac >> 32) & 0xff;
  486. new_mac[2] = (mac >> 24) & 0xff;
  487. new_mac[3] = (mac >> 16) & 0xff;
  488. new_mac[4] = (mac >> 8) & 0xff;
  489. new_mac[5] = mac & 0xff;
  490. r = fdt_setprop_inplace(initial_boot_params, n, "local-mac-address",
  491. new_mac, sizeof(new_mac));
  492. if (r) {
  493. pr_err("Setting \"local-mac-address\" failed %d", r);
  494. return;
  495. }
  496. *pmac = mac + 1;
  497. }
  498. static void __init octeon_fdt_rm_ethernet(int node)
  499. {
  500. const __be32 *phy_handle;
  501. phy_handle = fdt_getprop(initial_boot_params, node, "phy-handle", NULL);
  502. if (phy_handle) {
  503. u32 ph = be32_to_cpup(phy_handle);
  504. int p = fdt_node_offset_by_phandle(initial_boot_params, ph);
  505. if (p >= 0)
  506. fdt_nop_node(initial_boot_params, p);
  507. }
  508. fdt_nop_node(initial_boot_params, node);
  509. }
  510. static void __init _octeon_rx_tx_delay(int eth, int rx_delay, int tx_delay)
  511. {
  512. fdt_setprop_inplace_cell(initial_boot_params, eth, "rx-delay",
  513. rx_delay);
  514. fdt_setprop_inplace_cell(initial_boot_params, eth, "tx-delay",
  515. tx_delay);
  516. }
  517. static void __init octeon_rx_tx_delay(int eth, int iface, int port)
  518. {
  519. switch (cvmx_sysinfo_get()->board_type) {
  520. case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
  521. if (iface == 0) {
  522. if (port == 0) {
  523. /*
  524. * Boards with gigabit WAN ports need a
  525. * different setting that is compatible with
  526. * 100 Mbit settings
  527. */
  528. _octeon_rx_tx_delay(eth, 0xc, 0x0c);
  529. return;
  530. } else if (port == 1) {
  531. /* Different config for switch port. */
  532. _octeon_rx_tx_delay(eth, 0x0, 0x0);
  533. return;
  534. }
  535. }
  536. break;
  537. case CVMX_BOARD_TYPE_UBNT_E100:
  538. if (iface == 0 && port <= 2) {
  539. _octeon_rx_tx_delay(eth, 0x0, 0x10);
  540. return;
  541. }
  542. break;
  543. }
  544. fdt_nop_property(initial_boot_params, eth, "rx-delay");
  545. fdt_nop_property(initial_boot_params, eth, "tx-delay");
  546. }
  547. static void __init octeon_fdt_pip_port(int iface, int i, int p, int max)
  548. {
  549. char name_buffer[20];
  550. int eth;
  551. int phy_addr;
  552. int ipd_port;
  553. int fixed_link;
  554. snprintf(name_buffer, sizeof(name_buffer), "ethernet@%x", p);
  555. eth = fdt_subnode_offset(initial_boot_params, iface, name_buffer);
  556. if (eth < 0)
  557. return;
  558. if (p > max) {
  559. pr_debug("Deleting port %x:%x\n", i, p);
  560. octeon_fdt_rm_ethernet(eth);
  561. return;
  562. }
  563. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  564. ipd_port = (0x100 * i) + (0x10 * p) + 0x800;
  565. else
  566. ipd_port = 16 * i + p;
  567. phy_addr = cvmx_helper_board_get_mii_address(ipd_port);
  568. octeon_fdt_set_phy(eth, phy_addr);
  569. fixed_link = fdt_subnode_offset(initial_boot_params, eth, "fixed-link");
  570. if (fixed_link < 0)
  571. WARN_ON(octeon_has_fixed_link(ipd_port));
  572. else if (!octeon_has_fixed_link(ipd_port))
  573. fdt_nop_node(initial_boot_params, fixed_link);
  574. octeon_rx_tx_delay(eth, i, p);
  575. }
  576. static void __init octeon_fdt_pip_iface(int pip, int idx)
  577. {
  578. char name_buffer[20];
  579. int iface;
  580. int p;
  581. int count = 0;
  582. snprintf(name_buffer, sizeof(name_buffer), "interface@%d", idx);
  583. iface = fdt_subnode_offset(initial_boot_params, pip, name_buffer);
  584. if (iface < 0)
  585. return;
  586. if (cvmx_helper_interface_enumerate(idx) == 0)
  587. count = cvmx_helper_ports_on_interface(idx);
  588. for (p = 0; p < 16; p++)
  589. octeon_fdt_pip_port(iface, idx, p, count - 1);
  590. }
  591. void __init octeon_fill_mac_addresses(void)
  592. {
  593. const char *alias_prop;
  594. char name_buffer[20];
  595. u64 mac_addr_base;
  596. int aliases;
  597. int pip;
  598. int i;
  599. aliases = fdt_path_offset(initial_boot_params, "/aliases");
  600. if (aliases < 0)
  601. return;
  602. mac_addr_base =
  603. ((octeon_bootinfo->mac_addr_base[0] & 0xffull)) << 40 |
  604. ((octeon_bootinfo->mac_addr_base[1] & 0xffull)) << 32 |
  605. ((octeon_bootinfo->mac_addr_base[2] & 0xffull)) << 24 |
  606. ((octeon_bootinfo->mac_addr_base[3] & 0xffull)) << 16 |
  607. ((octeon_bootinfo->mac_addr_base[4] & 0xffull)) << 8 |
  608. (octeon_bootinfo->mac_addr_base[5] & 0xffull);
  609. for (i = 0; i < 2; i++) {
  610. int mgmt;
  611. snprintf(name_buffer, sizeof(name_buffer), "mix%d", i);
  612. alias_prop = fdt_getprop(initial_boot_params, aliases,
  613. name_buffer, NULL);
  614. if (!alias_prop)
  615. continue;
  616. mgmt = fdt_path_offset(initial_boot_params, alias_prop);
  617. if (mgmt < 0)
  618. continue;
  619. octeon_fdt_set_mac_addr(mgmt, &mac_addr_base);
  620. }
  621. alias_prop = fdt_getprop(initial_boot_params, aliases, "pip", NULL);
  622. if (!alias_prop)
  623. return;
  624. pip = fdt_path_offset(initial_boot_params, alias_prop);
  625. if (pip < 0)
  626. return;
  627. for (i = 0; i <= 4; i++) {
  628. int iface;
  629. int p;
  630. snprintf(name_buffer, sizeof(name_buffer), "interface@%d", i);
  631. iface = fdt_subnode_offset(initial_boot_params, pip,
  632. name_buffer);
  633. if (iface < 0)
  634. continue;
  635. for (p = 0; p < 16; p++) {
  636. int eth;
  637. snprintf(name_buffer, sizeof(name_buffer),
  638. "ethernet@%x", p);
  639. eth = fdt_subnode_offset(initial_boot_params, iface,
  640. name_buffer);
  641. if (eth < 0)
  642. continue;
  643. octeon_fdt_set_mac_addr(eth, &mac_addr_base);
  644. }
  645. }
  646. }
  647. int __init octeon_prune_device_tree(void)
  648. {
  649. int i, max_port, uart_mask;
  650. const char *pip_path;
  651. const char *alias_prop;
  652. char name_buffer[20];
  653. int aliases;
  654. if (fdt_check_header(initial_boot_params))
  655. panic("Corrupt Device Tree.");
  656. WARN(octeon_bootinfo->board_type == CVMX_BOARD_TYPE_CUST_DSR1000N,
  657. "Built-in DTB booting is deprecated on %s. Please switch to use appended DTB.",
  658. cvmx_board_type_to_string(octeon_bootinfo->board_type));
  659. aliases = fdt_path_offset(initial_boot_params, "/aliases");
  660. if (aliases < 0) {
  661. pr_err("Error: No /aliases node in device tree.");
  662. return -EINVAL;
  663. }
  664. if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))
  665. max_port = 2;
  666. else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN68XX))
  667. max_port = 1;
  668. else
  669. max_port = 0;
  670. if (octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC10E)
  671. max_port = 0;
  672. for (i = 0; i < 2; i++) {
  673. int mgmt;
  674. snprintf(name_buffer, sizeof(name_buffer),
  675. "mix%d", i);
  676. alias_prop = fdt_getprop(initial_boot_params, aliases,
  677. name_buffer, NULL);
  678. if (alias_prop) {
  679. mgmt = fdt_path_offset(initial_boot_params, alias_prop);
  680. if (mgmt < 0)
  681. continue;
  682. if (i >= max_port) {
  683. pr_debug("Deleting mix%d\n", i);
  684. octeon_fdt_rm_ethernet(mgmt);
  685. fdt_nop_property(initial_boot_params, aliases,
  686. name_buffer);
  687. } else {
  688. int phy_addr = cvmx_helper_board_get_mii_address(CVMX_HELPER_BOARD_MGMT_IPD_PORT + i);
  689. octeon_fdt_set_phy(mgmt, phy_addr);
  690. }
  691. }
  692. }
  693. pip_path = fdt_getprop(initial_boot_params, aliases, "pip", NULL);
  694. if (pip_path) {
  695. int pip = fdt_path_offset(initial_boot_params, pip_path);
  696. if (pip >= 0)
  697. for (i = 0; i <= 4; i++)
  698. octeon_fdt_pip_iface(pip, i);
  699. }
  700. /* I2C */
  701. if (OCTEON_IS_MODEL(OCTEON_CN52XX) ||
  702. OCTEON_IS_MODEL(OCTEON_CN63XX) ||
  703. OCTEON_IS_MODEL(OCTEON_CN68XX) ||
  704. OCTEON_IS_MODEL(OCTEON_CN56XX))
  705. max_port = 2;
  706. else
  707. max_port = 1;
  708. for (i = 0; i < 2; i++) {
  709. int i2c;
  710. snprintf(name_buffer, sizeof(name_buffer),
  711. "twsi%d", i);
  712. alias_prop = fdt_getprop(initial_boot_params, aliases,
  713. name_buffer, NULL);
  714. if (alias_prop) {
  715. i2c = fdt_path_offset(initial_boot_params, alias_prop);
  716. if (i2c < 0)
  717. continue;
  718. if (i >= max_port) {
  719. pr_debug("Deleting twsi%d\n", i);
  720. fdt_nop_node(initial_boot_params, i2c);
  721. fdt_nop_property(initial_boot_params, aliases,
  722. name_buffer);
  723. }
  724. }
  725. }
  726. /* SMI/MDIO */
  727. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  728. max_port = 4;
  729. else if (OCTEON_IS_MODEL(OCTEON_CN52XX) ||
  730. OCTEON_IS_MODEL(OCTEON_CN63XX) ||
  731. OCTEON_IS_MODEL(OCTEON_CN56XX))
  732. max_port = 2;
  733. else
  734. max_port = 1;
  735. for (i = 0; i < 2; i++) {
  736. int i2c;
  737. snprintf(name_buffer, sizeof(name_buffer),
  738. "smi%d", i);
  739. alias_prop = fdt_getprop(initial_boot_params, aliases,
  740. name_buffer, NULL);
  741. if (alias_prop) {
  742. i2c = fdt_path_offset(initial_boot_params, alias_prop);
  743. if (i2c < 0)
  744. continue;
  745. if (i >= max_port) {
  746. pr_debug("Deleting smi%d\n", i);
  747. fdt_nop_node(initial_boot_params, i2c);
  748. fdt_nop_property(initial_boot_params, aliases,
  749. name_buffer);
  750. }
  751. }
  752. }
  753. /* Serial */
  754. uart_mask = 3;
  755. /* Right now CN52XX is the only chip with a third uart */
  756. if (OCTEON_IS_MODEL(OCTEON_CN52XX))
  757. uart_mask |= 4; /* uart2 */
  758. for (i = 0; i < 3; i++) {
  759. int uart;
  760. snprintf(name_buffer, sizeof(name_buffer),
  761. "uart%d", i);
  762. alias_prop = fdt_getprop(initial_boot_params, aliases,
  763. name_buffer, NULL);
  764. if (alias_prop) {
  765. uart = fdt_path_offset(initial_boot_params, alias_prop);
  766. if (uart_mask & (1 << i)) {
  767. __be32 f;
  768. f = cpu_to_be32(octeon_get_io_clock_rate());
  769. fdt_setprop_inplace(initial_boot_params,
  770. uart, "clock-frequency",
  771. &f, sizeof(f));
  772. continue;
  773. }
  774. pr_debug("Deleting uart%d\n", i);
  775. fdt_nop_node(initial_boot_params, uart);
  776. fdt_nop_property(initial_boot_params, aliases,
  777. name_buffer);
  778. }
  779. }
  780. /* Compact Flash */
  781. alias_prop = fdt_getprop(initial_boot_params, aliases,
  782. "cf0", NULL);
  783. if (alias_prop) {
  784. union cvmx_mio_boot_reg_cfgx mio_boot_reg_cfg;
  785. unsigned long base_ptr, region_base, region_size;
  786. unsigned long region1_base = 0;
  787. unsigned long region1_size = 0;
  788. int cs, bootbus;
  789. bool is_16bit = false;
  790. bool is_true_ide = false;
  791. __be32 new_reg[6];
  792. __be32 *ranges;
  793. int len;
  794. int cf = fdt_path_offset(initial_boot_params, alias_prop);
  795. base_ptr = 0;
  796. if (octeon_bootinfo->major_version == 1
  797. && octeon_bootinfo->minor_version >= 1) {
  798. if (octeon_bootinfo->compact_flash_common_base_addr)
  799. base_ptr = octeon_bootinfo->compact_flash_common_base_addr;
  800. } else {
  801. base_ptr = 0x1d000800;
  802. }
  803. if (!base_ptr)
  804. goto no_cf;
  805. /* Find CS0 region. */
  806. for (cs = 0; cs < 8; cs++) {
  807. mio_boot_reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));
  808. region_base = mio_boot_reg_cfg.s.base << 16;
  809. region_size = (mio_boot_reg_cfg.s.size + 1) << 16;
  810. if (mio_boot_reg_cfg.s.en && base_ptr >= region_base
  811. && base_ptr < region_base + region_size) {
  812. is_16bit = mio_boot_reg_cfg.s.width;
  813. break;
  814. }
  815. }
  816. if (cs >= 7) {
  817. /* cs and cs + 1 are CS0 and CS1, both must be less than 8. */
  818. goto no_cf;
  819. }
  820. if (!(base_ptr & 0xfffful)) {
  821. /*
  822. * Boot loader signals availability of DMA (true_ide
  823. * mode) by setting low order bits of base_ptr to
  824. * zero.
  825. */
  826. /* Asume that CS1 immediately follows. */
  827. mio_boot_reg_cfg.u64 =
  828. cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs + 1));
  829. region1_base = mio_boot_reg_cfg.s.base << 16;
  830. region1_size = (mio_boot_reg_cfg.s.size + 1) << 16;
  831. if (!mio_boot_reg_cfg.s.en)
  832. goto no_cf;
  833. is_true_ide = true;
  834. } else {
  835. fdt_nop_property(initial_boot_params, cf, "cavium,true-ide");
  836. fdt_nop_property(initial_boot_params, cf, "cavium,dma-engine-handle");
  837. if (!is_16bit) {
  838. __be32 width = cpu_to_be32(8);
  839. fdt_setprop_inplace(initial_boot_params, cf,
  840. "cavium,bus-width", &width, sizeof(width));
  841. }
  842. }
  843. new_reg[0] = cpu_to_be32(cs);
  844. new_reg[1] = cpu_to_be32(0);
  845. new_reg[2] = cpu_to_be32(0x10000);
  846. new_reg[3] = cpu_to_be32(cs + 1);
  847. new_reg[4] = cpu_to_be32(0);
  848. new_reg[5] = cpu_to_be32(0x10000);
  849. fdt_setprop_inplace(initial_boot_params, cf,
  850. "reg", new_reg, sizeof(new_reg));
  851. bootbus = fdt_parent_offset(initial_boot_params, cf);
  852. if (bootbus < 0)
  853. goto no_cf;
  854. ranges = fdt_getprop_w(initial_boot_params, bootbus, "ranges", &len);
  855. if (!ranges || len < (5 * 8 * sizeof(__be32)))
  856. goto no_cf;
  857. ranges[(cs * 5) + 2] = cpu_to_be32(region_base >> 32);
  858. ranges[(cs * 5) + 3] = cpu_to_be32(region_base & 0xffffffff);
  859. ranges[(cs * 5) + 4] = cpu_to_be32(region_size);
  860. if (is_true_ide) {
  861. cs++;
  862. ranges[(cs * 5) + 2] = cpu_to_be32(region1_base >> 32);
  863. ranges[(cs * 5) + 3] = cpu_to_be32(region1_base & 0xffffffff);
  864. ranges[(cs * 5) + 4] = cpu_to_be32(region1_size);
  865. }
  866. goto end_cf;
  867. no_cf:
  868. fdt_nop_node(initial_boot_params, cf);
  869. end_cf:
  870. ;
  871. }
  872. /* 8 char LED */
  873. alias_prop = fdt_getprop(initial_boot_params, aliases,
  874. "led0", NULL);
  875. if (alias_prop) {
  876. union cvmx_mio_boot_reg_cfgx mio_boot_reg_cfg;
  877. unsigned long base_ptr, region_base, region_size;
  878. int cs, bootbus;
  879. __be32 new_reg[6];
  880. __be32 *ranges;
  881. int len;
  882. int led = fdt_path_offset(initial_boot_params, alias_prop);
  883. base_ptr = octeon_bootinfo->led_display_base_addr;
  884. if (base_ptr == 0)
  885. goto no_led;
  886. /* Find CS0 region. */
  887. for (cs = 0; cs < 8; cs++) {
  888. mio_boot_reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));
  889. region_base = mio_boot_reg_cfg.s.base << 16;
  890. region_size = (mio_boot_reg_cfg.s.size + 1) << 16;
  891. if (mio_boot_reg_cfg.s.en && base_ptr >= region_base
  892. && base_ptr < region_base + region_size)
  893. break;
  894. }
  895. if (cs > 7)
  896. goto no_led;
  897. new_reg[0] = cpu_to_be32(cs);
  898. new_reg[1] = cpu_to_be32(0x20);
  899. new_reg[2] = cpu_to_be32(0x20);
  900. new_reg[3] = cpu_to_be32(cs);
  901. new_reg[4] = cpu_to_be32(0);
  902. new_reg[5] = cpu_to_be32(0x20);
  903. fdt_setprop_inplace(initial_boot_params, led,
  904. "reg", new_reg, sizeof(new_reg));
  905. bootbus = fdt_parent_offset(initial_boot_params, led);
  906. if (bootbus < 0)
  907. goto no_led;
  908. ranges = fdt_getprop_w(initial_boot_params, bootbus, "ranges", &len);
  909. if (!ranges || len < (5 * 8 * sizeof(__be32)))
  910. goto no_led;
  911. ranges[(cs * 5) + 2] = cpu_to_be32(region_base >> 32);
  912. ranges[(cs * 5) + 3] = cpu_to_be32(region_base & 0xffffffff);
  913. ranges[(cs * 5) + 4] = cpu_to_be32(region_size);
  914. goto end_led;
  915. no_led:
  916. fdt_nop_node(initial_boot_params, led);
  917. end_led:
  918. ;
  919. }
  920. #ifdef CONFIG_USB
  921. /* OHCI/UHCI USB */
  922. alias_prop = fdt_getprop(initial_boot_params, aliases,
  923. "uctl", NULL);
  924. if (alias_prop) {
  925. int uctl = fdt_path_offset(initial_boot_params, alias_prop);
  926. if (uctl >= 0 && (!OCTEON_IS_MODEL(OCTEON_CN6XXX) ||
  927. octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC2E)) {
  928. pr_debug("Deleting uctl\n");
  929. fdt_nop_node(initial_boot_params, uctl);
  930. fdt_nop_property(initial_boot_params, aliases, "uctl");
  931. } else if (octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC10E ||
  932. octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC4E) {
  933. /* Missing "refclk-type" defaults to crystal. */
  934. fdt_nop_property(initial_boot_params, uctl, "refclk-type");
  935. }
  936. }
  937. /* DWC2 USB */
  938. alias_prop = fdt_getprop(initial_boot_params, aliases,
  939. "usbn", NULL);
  940. if (alias_prop) {
  941. int usbn = fdt_path_offset(initial_boot_params, alias_prop);
  942. if (usbn >= 0 && (current_cpu_type() == CPU_CAVIUM_OCTEON2 ||
  943. !octeon_has_feature(OCTEON_FEATURE_USB))) {
  944. pr_debug("Deleting usbn\n");
  945. fdt_nop_node(initial_boot_params, usbn);
  946. fdt_nop_property(initial_boot_params, aliases, "usbn");
  947. } else {
  948. __be32 new_f[1];
  949. enum cvmx_helper_board_usb_clock_types c;
  950. c = __cvmx_helper_board_usb_get_clock_type();
  951. switch (c) {
  952. case USB_CLOCK_TYPE_REF_48:
  953. new_f[0] = cpu_to_be32(48000000);
  954. fdt_setprop_inplace(initial_boot_params, usbn,
  955. "refclk-frequency", new_f, sizeof(new_f));
  956. fallthrough;
  957. case USB_CLOCK_TYPE_REF_12:
  958. /* Missing "refclk-type" defaults to external. */
  959. fdt_nop_property(initial_boot_params, usbn, "refclk-type");
  960. break;
  961. default:
  962. break;
  963. }
  964. }
  965. }
  966. #endif
  967. return 0;
  968. }
  969. static int __init octeon_publish_devices(void)
  970. {
  971. return of_platform_populate(NULL, octeon_ids, NULL, NULL);
  972. }
  973. arch_initcall(octeon_publish_devices);