octeon-platform.c 27 KB

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