octeon-platform.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. of_node_put(ehci_node);
  274. if (!pd)
  275. return 0;
  276. pd->dev.platform_data = &octeon_ehci_pdata;
  277. octeon_ehci_hw_start(&pd->dev);
  278. return ret;
  279. }
  280. device_initcall(octeon_ehci_device_init);
  281. static int octeon_ohci_power_on(struct platform_device *pdev)
  282. {
  283. octeon2_usb_clocks_start(&pdev->dev);
  284. return 0;
  285. }
  286. static void octeon_ohci_power_off(struct platform_device *pdev)
  287. {
  288. octeon2_usb_clocks_stop();
  289. }
  290. static struct usb_ohci_pdata octeon_ohci_pdata = {
  291. /* Octeon OHCI matches CPU endianness. */
  292. #ifdef __BIG_ENDIAN
  293. .big_endian_mmio = 1,
  294. #endif
  295. .power_on = octeon_ohci_power_on,
  296. .power_off = octeon_ohci_power_off,
  297. };
  298. static void __init octeon_ohci_hw_start(struct device *dev)
  299. {
  300. union cvmx_uctlx_ohci_ctl ohci_ctl;
  301. octeon2_usb_clocks_start(dev);
  302. ohci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_OHCI_CTL(0));
  303. ohci_ctl.s.l2c_addr_msb = 0;
  304. #ifdef __BIG_ENDIAN
  305. ohci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */
  306. ohci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */
  307. #else
  308. ohci_ctl.s.l2c_buff_emod = 0; /* not swapped. */
  309. ohci_ctl.s.l2c_desc_emod = 0; /* not swapped. */
  310. ohci_ctl.s.inv_reg_a2 = 1;
  311. #endif
  312. cvmx_write_csr(CVMX_UCTLX_OHCI_CTL(0), ohci_ctl.u64);
  313. octeon2_usb_clocks_stop();
  314. }
  315. static int __init octeon_ohci_device_init(void)
  316. {
  317. struct platform_device *pd;
  318. struct device_node *ohci_node;
  319. int ret = 0;
  320. ohci_node = of_find_node_by_name(NULL, "ohci");
  321. if (!ohci_node)
  322. return 0;
  323. pd = of_find_device_by_node(ohci_node);
  324. of_node_put(ohci_node);
  325. if (!pd)
  326. return 0;
  327. pd->dev.platform_data = &octeon_ohci_pdata;
  328. octeon_ohci_hw_start(&pd->dev);
  329. return ret;
  330. }
  331. device_initcall(octeon_ohci_device_init);
  332. #endif /* CONFIG_USB */
  333. /* Octeon Random Number Generator. */
  334. static int __init octeon_rng_device_init(void)
  335. {
  336. struct platform_device *pd;
  337. int ret = 0;
  338. struct resource rng_resources[] = {
  339. {
  340. .flags = IORESOURCE_MEM,
  341. .start = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS),
  342. .end = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS) + 0xf
  343. }, {
  344. .flags = IORESOURCE_MEM,
  345. .start = cvmx_build_io_address(8, 0),
  346. .end = cvmx_build_io_address(8, 0) + 0x7
  347. }
  348. };
  349. pd = platform_device_alloc("octeon_rng", -1);
  350. if (!pd) {
  351. ret = -ENOMEM;
  352. goto out;
  353. }
  354. ret = platform_device_add_resources(pd, rng_resources,
  355. ARRAY_SIZE(rng_resources));
  356. if (ret)
  357. goto fail;
  358. ret = platform_device_add(pd);
  359. if (ret)
  360. goto fail;
  361. return ret;
  362. fail:
  363. platform_device_put(pd);
  364. out:
  365. return ret;
  366. }
  367. device_initcall(octeon_rng_device_init);
  368. const struct of_device_id octeon_ids[] __initconst = {
  369. { .compatible = "simple-bus", },
  370. { .compatible = "cavium,octeon-6335-uctl", },
  371. { .compatible = "cavium,octeon-5750-usbn", },
  372. { .compatible = "cavium,octeon-3860-bootbus", },
  373. { .compatible = "cavium,mdio-mux", },
  374. { .compatible = "gpio-leds", },
  375. { .compatible = "cavium,octeon-7130-usb-uctl", },
  376. {},
  377. };
  378. static bool __init octeon_has_88e1145(void)
  379. {
  380. return !OCTEON_IS_MODEL(OCTEON_CN52XX) &&
  381. !OCTEON_IS_MODEL(OCTEON_CN6XXX) &&
  382. !OCTEON_IS_MODEL(OCTEON_CN56XX);
  383. }
  384. static void __init octeon_fdt_set_phy(int eth, int phy_addr)
  385. {
  386. const __be32 *phy_handle;
  387. const __be32 *alt_phy_handle;
  388. const __be32 *reg;
  389. u32 phandle;
  390. int phy;
  391. int alt_phy;
  392. const char *p;
  393. int current_len;
  394. char new_name[20];
  395. phy_handle = fdt_getprop(initial_boot_params, eth, "phy-handle", NULL);
  396. if (!phy_handle)
  397. return;
  398. phandle = be32_to_cpup(phy_handle);
  399. phy = fdt_node_offset_by_phandle(initial_boot_params, phandle);
  400. alt_phy_handle = fdt_getprop(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);
  401. if (alt_phy_handle) {
  402. u32 alt_phandle = be32_to_cpup(alt_phy_handle);
  403. alt_phy = fdt_node_offset_by_phandle(initial_boot_params, alt_phandle);
  404. } else {
  405. alt_phy = -1;
  406. }
  407. if (phy_addr < 0 || phy < 0) {
  408. /* Delete the PHY things */
  409. fdt_nop_property(initial_boot_params, eth, "phy-handle");
  410. /* This one may fail */
  411. fdt_nop_property(initial_boot_params, eth, "cavium,alt-phy-handle");
  412. if (phy >= 0)
  413. fdt_nop_node(initial_boot_params, phy);
  414. if (alt_phy >= 0)
  415. fdt_nop_node(initial_boot_params, alt_phy);
  416. return;
  417. }
  418. if (phy_addr >= 256 && alt_phy > 0) {
  419. const struct fdt_property *phy_prop;
  420. struct fdt_property *alt_prop;
  421. fdt32_t phy_handle_name;
  422. /* Use the alt phy node instead.*/
  423. phy_prop = fdt_get_property(initial_boot_params, eth, "phy-handle", NULL);
  424. phy_handle_name = phy_prop->nameoff;
  425. fdt_nop_node(initial_boot_params, phy);
  426. fdt_nop_property(initial_boot_params, eth, "phy-handle");
  427. alt_prop = fdt_get_property_w(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);
  428. alt_prop->nameoff = phy_handle_name;
  429. phy = alt_phy;
  430. }
  431. phy_addr &= 0xff;
  432. if (octeon_has_88e1145()) {
  433. fdt_nop_property(initial_boot_params, phy, "marvell,reg-init");
  434. memset(new_name, 0, sizeof(new_name));
  435. strcpy(new_name, "marvell,88e1145");
  436. p = fdt_getprop(initial_boot_params, phy, "compatible",
  437. &current_len);
  438. if (p && current_len >= strlen(new_name))
  439. fdt_setprop_inplace(initial_boot_params, phy,
  440. "compatible", new_name, current_len);
  441. }
  442. reg = fdt_getprop(initial_boot_params, phy, "reg", NULL);
  443. if (phy_addr == be32_to_cpup(reg))
  444. return;
  445. fdt_setprop_inplace_cell(initial_boot_params, phy, "reg", phy_addr);
  446. snprintf(new_name, sizeof(new_name), "ethernet-phy@%x", phy_addr);
  447. p = fdt_get_name(initial_boot_params, phy, &current_len);
  448. if (p && current_len == strlen(new_name))
  449. fdt_set_name(initial_boot_params, phy, new_name);
  450. else
  451. pr_err("Error: could not rename ethernet phy: <%s>", p);
  452. }
  453. static void __init octeon_fdt_set_mac_addr(int n, u64 *pmac)
  454. {
  455. const u8 *old_mac;
  456. int old_len;
  457. u8 new_mac[6];
  458. u64 mac = *pmac;
  459. int r;
  460. old_mac = fdt_getprop(initial_boot_params, n, "local-mac-address",
  461. &old_len);
  462. if (!old_mac || old_len != 6 || is_valid_ether_addr(old_mac))
  463. return;
  464. new_mac[0] = (mac >> 40) & 0xff;
  465. new_mac[1] = (mac >> 32) & 0xff;
  466. new_mac[2] = (mac >> 24) & 0xff;
  467. new_mac[3] = (mac >> 16) & 0xff;
  468. new_mac[4] = (mac >> 8) & 0xff;
  469. new_mac[5] = mac & 0xff;
  470. r = fdt_setprop_inplace(initial_boot_params, n, "local-mac-address",
  471. new_mac, sizeof(new_mac));
  472. if (r) {
  473. pr_err("Setting \"local-mac-address\" failed %d", r);
  474. return;
  475. }
  476. *pmac = mac + 1;
  477. }
  478. static void __init octeon_fdt_rm_ethernet(int node)
  479. {
  480. const __be32 *phy_handle;
  481. phy_handle = fdt_getprop(initial_boot_params, node, "phy-handle", NULL);
  482. if (phy_handle) {
  483. u32 ph = be32_to_cpup(phy_handle);
  484. int p = fdt_node_offset_by_phandle(initial_boot_params, ph);
  485. if (p >= 0)
  486. fdt_nop_node(initial_boot_params, p);
  487. }
  488. fdt_nop_node(initial_boot_params, node);
  489. }
  490. static void __init octeon_fdt_pip_port(int iface, int i, int p, int max)
  491. {
  492. char name_buffer[20];
  493. int eth;
  494. int phy_addr;
  495. int ipd_port;
  496. snprintf(name_buffer, sizeof(name_buffer), "ethernet@%x", p);
  497. eth = fdt_subnode_offset(initial_boot_params, iface, name_buffer);
  498. if (eth < 0)
  499. return;
  500. if (p > max) {
  501. pr_debug("Deleting port %x:%x\n", i, p);
  502. octeon_fdt_rm_ethernet(eth);
  503. return;
  504. }
  505. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  506. ipd_port = (0x100 * i) + (0x10 * p) + 0x800;
  507. else
  508. ipd_port = 16 * i + p;
  509. phy_addr = cvmx_helper_board_get_mii_address(ipd_port);
  510. octeon_fdt_set_phy(eth, phy_addr);
  511. }
  512. static void __init octeon_fdt_pip_iface(int pip, int idx)
  513. {
  514. char name_buffer[20];
  515. int iface;
  516. int p;
  517. int count = 0;
  518. snprintf(name_buffer, sizeof(name_buffer), "interface@%d", idx);
  519. iface = fdt_subnode_offset(initial_boot_params, pip, name_buffer);
  520. if (iface < 0)
  521. return;
  522. if (cvmx_helper_interface_enumerate(idx) == 0)
  523. count = cvmx_helper_ports_on_interface(idx);
  524. for (p = 0; p < 16; p++)
  525. octeon_fdt_pip_port(iface, idx, p, count - 1);
  526. }
  527. void __init octeon_fill_mac_addresses(void)
  528. {
  529. const char *alias_prop;
  530. char name_buffer[20];
  531. u64 mac_addr_base;
  532. int aliases;
  533. int pip;
  534. int i;
  535. aliases = fdt_path_offset(initial_boot_params, "/aliases");
  536. if (aliases < 0)
  537. return;
  538. mac_addr_base =
  539. ((octeon_bootinfo->mac_addr_base[0] & 0xffull)) << 40 |
  540. ((octeon_bootinfo->mac_addr_base[1] & 0xffull)) << 32 |
  541. ((octeon_bootinfo->mac_addr_base[2] & 0xffull)) << 24 |
  542. ((octeon_bootinfo->mac_addr_base[3] & 0xffull)) << 16 |
  543. ((octeon_bootinfo->mac_addr_base[4] & 0xffull)) << 8 |
  544. (octeon_bootinfo->mac_addr_base[5] & 0xffull);
  545. for (i = 0; i < 2; i++) {
  546. int mgmt;
  547. snprintf(name_buffer, sizeof(name_buffer), "mix%d", i);
  548. alias_prop = fdt_getprop(initial_boot_params, aliases,
  549. name_buffer, NULL);
  550. if (!alias_prop)
  551. continue;
  552. mgmt = fdt_path_offset(initial_boot_params, alias_prop);
  553. if (mgmt < 0)
  554. continue;
  555. octeon_fdt_set_mac_addr(mgmt, &mac_addr_base);
  556. }
  557. alias_prop = fdt_getprop(initial_boot_params, aliases, "pip", NULL);
  558. if (!alias_prop)
  559. return;
  560. pip = fdt_path_offset(initial_boot_params, alias_prop);
  561. if (pip < 0)
  562. return;
  563. for (i = 0; i <= 4; i++) {
  564. int iface;
  565. int p;
  566. snprintf(name_buffer, sizeof(name_buffer), "interface@%d", i);
  567. iface = fdt_subnode_offset(initial_boot_params, pip,
  568. name_buffer);
  569. if (iface < 0)
  570. continue;
  571. for (p = 0; p < 16; p++) {
  572. int eth;
  573. snprintf(name_buffer, sizeof(name_buffer),
  574. "ethernet@%x", p);
  575. eth = fdt_subnode_offset(initial_boot_params, iface,
  576. name_buffer);
  577. if (eth < 0)
  578. continue;
  579. octeon_fdt_set_mac_addr(eth, &mac_addr_base);
  580. }
  581. }
  582. }
  583. int __init octeon_prune_device_tree(void)
  584. {
  585. int i, max_port, uart_mask;
  586. const char *pip_path;
  587. const char *alias_prop;
  588. char name_buffer[20];
  589. int aliases;
  590. if (fdt_check_header(initial_boot_params))
  591. panic("Corrupt Device Tree.");
  592. WARN(octeon_bootinfo->board_type == CVMX_BOARD_TYPE_CUST_DSR1000N,
  593. "Built-in DTB booting is deprecated on %s. Please switch to use appended DTB.",
  594. cvmx_board_type_to_string(octeon_bootinfo->board_type));
  595. aliases = fdt_path_offset(initial_boot_params, "/aliases");
  596. if (aliases < 0) {
  597. pr_err("Error: No /aliases node in device tree.");
  598. return -EINVAL;
  599. }
  600. if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))
  601. max_port = 2;
  602. else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN68XX))
  603. max_port = 1;
  604. else
  605. max_port = 0;
  606. if (octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC10E)
  607. max_port = 0;
  608. for (i = 0; i < 2; i++) {
  609. int mgmt;
  610. snprintf(name_buffer, sizeof(name_buffer),
  611. "mix%d", i);
  612. alias_prop = fdt_getprop(initial_boot_params, aliases,
  613. name_buffer, NULL);
  614. if (alias_prop) {
  615. mgmt = fdt_path_offset(initial_boot_params, alias_prop);
  616. if (mgmt < 0)
  617. continue;
  618. if (i >= max_port) {
  619. pr_debug("Deleting mix%d\n", i);
  620. octeon_fdt_rm_ethernet(mgmt);
  621. fdt_nop_property(initial_boot_params, aliases,
  622. name_buffer);
  623. } else {
  624. int phy_addr = cvmx_helper_board_get_mii_address(CVMX_HELPER_BOARD_MGMT_IPD_PORT + i);
  625. octeon_fdt_set_phy(mgmt, phy_addr);
  626. }
  627. }
  628. }
  629. pip_path = fdt_getprop(initial_boot_params, aliases, "pip", NULL);
  630. if (pip_path) {
  631. int pip = fdt_path_offset(initial_boot_params, pip_path);
  632. if (pip >= 0)
  633. for (i = 0; i <= 4; i++)
  634. octeon_fdt_pip_iface(pip, i);
  635. }
  636. /* I2C */
  637. if (OCTEON_IS_MODEL(OCTEON_CN52XX) ||
  638. OCTEON_IS_MODEL(OCTEON_CN63XX) ||
  639. OCTEON_IS_MODEL(OCTEON_CN68XX) ||
  640. OCTEON_IS_MODEL(OCTEON_CN56XX))
  641. max_port = 2;
  642. else
  643. max_port = 1;
  644. for (i = 0; i < 2; i++) {
  645. int i2c;
  646. snprintf(name_buffer, sizeof(name_buffer),
  647. "twsi%d", i);
  648. alias_prop = fdt_getprop(initial_boot_params, aliases,
  649. name_buffer, NULL);
  650. if (alias_prop) {
  651. i2c = fdt_path_offset(initial_boot_params, alias_prop);
  652. if (i2c < 0)
  653. continue;
  654. if (i >= max_port) {
  655. pr_debug("Deleting twsi%d\n", i);
  656. fdt_nop_node(initial_boot_params, i2c);
  657. fdt_nop_property(initial_boot_params, aliases,
  658. name_buffer);
  659. }
  660. }
  661. }
  662. /* SMI/MDIO */
  663. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  664. max_port = 4;
  665. else if (OCTEON_IS_MODEL(OCTEON_CN52XX) ||
  666. OCTEON_IS_MODEL(OCTEON_CN63XX) ||
  667. OCTEON_IS_MODEL(OCTEON_CN56XX))
  668. max_port = 2;
  669. else
  670. max_port = 1;
  671. for (i = 0; i < 2; i++) {
  672. int i2c;
  673. snprintf(name_buffer, sizeof(name_buffer),
  674. "smi%d", i);
  675. alias_prop = fdt_getprop(initial_boot_params, aliases,
  676. name_buffer, NULL);
  677. if (alias_prop) {
  678. i2c = fdt_path_offset(initial_boot_params, alias_prop);
  679. if (i2c < 0)
  680. continue;
  681. if (i >= max_port) {
  682. pr_debug("Deleting smi%d\n", i);
  683. fdt_nop_node(initial_boot_params, i2c);
  684. fdt_nop_property(initial_boot_params, aliases,
  685. name_buffer);
  686. }
  687. }
  688. }
  689. /* Serial */
  690. uart_mask = 3;
  691. /* Right now CN52XX is the only chip with a third uart */
  692. if (OCTEON_IS_MODEL(OCTEON_CN52XX))
  693. uart_mask |= 4; /* uart2 */
  694. for (i = 0; i < 3; i++) {
  695. int uart;
  696. snprintf(name_buffer, sizeof(name_buffer),
  697. "uart%d", i);
  698. alias_prop = fdt_getprop(initial_boot_params, aliases,
  699. name_buffer, NULL);
  700. if (alias_prop) {
  701. uart = fdt_path_offset(initial_boot_params, alias_prop);
  702. if (uart_mask & (1 << i)) {
  703. __be32 f;
  704. f = cpu_to_be32(octeon_get_io_clock_rate());
  705. fdt_setprop_inplace(initial_boot_params,
  706. uart, "clock-frequency",
  707. &f, sizeof(f));
  708. continue;
  709. }
  710. pr_debug("Deleting uart%d\n", i);
  711. fdt_nop_node(initial_boot_params, uart);
  712. fdt_nop_property(initial_boot_params, aliases,
  713. name_buffer);
  714. }
  715. }
  716. /* Compact Flash */
  717. alias_prop = fdt_getprop(initial_boot_params, aliases,
  718. "cf0", NULL);
  719. if (alias_prop) {
  720. union cvmx_mio_boot_reg_cfgx mio_boot_reg_cfg;
  721. unsigned long base_ptr, region_base, region_size;
  722. unsigned long region1_base = 0;
  723. unsigned long region1_size = 0;
  724. int cs, bootbus;
  725. bool is_16bit = false;
  726. bool is_true_ide = false;
  727. __be32 new_reg[6];
  728. __be32 *ranges;
  729. int len;
  730. int cf = fdt_path_offset(initial_boot_params, alias_prop);
  731. base_ptr = 0;
  732. if (octeon_bootinfo->major_version == 1
  733. && octeon_bootinfo->minor_version >= 1) {
  734. if (octeon_bootinfo->compact_flash_common_base_addr)
  735. base_ptr = octeon_bootinfo->compact_flash_common_base_addr;
  736. } else {
  737. base_ptr = 0x1d000800;
  738. }
  739. if (!base_ptr)
  740. goto no_cf;
  741. /* Find CS0 region. */
  742. for (cs = 0; cs < 8; cs++) {
  743. mio_boot_reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));
  744. region_base = mio_boot_reg_cfg.s.base << 16;
  745. region_size = (mio_boot_reg_cfg.s.size + 1) << 16;
  746. if (mio_boot_reg_cfg.s.en && base_ptr >= region_base
  747. && base_ptr < region_base + region_size) {
  748. is_16bit = mio_boot_reg_cfg.s.width;
  749. break;
  750. }
  751. }
  752. if (cs >= 7) {
  753. /* cs and cs + 1 are CS0 and CS1, both must be less than 8. */
  754. goto no_cf;
  755. }
  756. if (!(base_ptr & 0xfffful)) {
  757. /*
  758. * Boot loader signals availability of DMA (true_ide
  759. * mode) by setting low order bits of base_ptr to
  760. * zero.
  761. */
  762. /* Asume that CS1 immediately follows. */
  763. mio_boot_reg_cfg.u64 =
  764. cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs + 1));
  765. region1_base = mio_boot_reg_cfg.s.base << 16;
  766. region1_size = (mio_boot_reg_cfg.s.size + 1) << 16;
  767. if (!mio_boot_reg_cfg.s.en)
  768. goto no_cf;
  769. is_true_ide = true;
  770. } else {
  771. fdt_nop_property(initial_boot_params, cf, "cavium,true-ide");
  772. fdt_nop_property(initial_boot_params, cf, "cavium,dma-engine-handle");
  773. if (!is_16bit) {
  774. __be32 width = cpu_to_be32(8);
  775. fdt_setprop_inplace(initial_boot_params, cf,
  776. "cavium,bus-width", &width, sizeof(width));
  777. }
  778. }
  779. new_reg[0] = cpu_to_be32(cs);
  780. new_reg[1] = cpu_to_be32(0);
  781. new_reg[2] = cpu_to_be32(0x10000);
  782. new_reg[3] = cpu_to_be32(cs + 1);
  783. new_reg[4] = cpu_to_be32(0);
  784. new_reg[5] = cpu_to_be32(0x10000);
  785. fdt_setprop_inplace(initial_boot_params, cf,
  786. "reg", new_reg, sizeof(new_reg));
  787. bootbus = fdt_parent_offset(initial_boot_params, cf);
  788. if (bootbus < 0)
  789. goto no_cf;
  790. ranges = fdt_getprop_w(initial_boot_params, bootbus, "ranges", &len);
  791. if (!ranges || len < (5 * 8 * sizeof(__be32)))
  792. goto no_cf;
  793. ranges[(cs * 5) + 2] = cpu_to_be32(region_base >> 32);
  794. ranges[(cs * 5) + 3] = cpu_to_be32(region_base & 0xffffffff);
  795. ranges[(cs * 5) + 4] = cpu_to_be32(region_size);
  796. if (is_true_ide) {
  797. cs++;
  798. ranges[(cs * 5) + 2] = cpu_to_be32(region1_base >> 32);
  799. ranges[(cs * 5) + 3] = cpu_to_be32(region1_base & 0xffffffff);
  800. ranges[(cs * 5) + 4] = cpu_to_be32(region1_size);
  801. }
  802. goto end_cf;
  803. no_cf:
  804. fdt_nop_node(initial_boot_params, cf);
  805. end_cf:
  806. ;
  807. }
  808. /* 8 char LED */
  809. alias_prop = fdt_getprop(initial_boot_params, aliases,
  810. "led0", NULL);
  811. if (alias_prop) {
  812. union cvmx_mio_boot_reg_cfgx mio_boot_reg_cfg;
  813. unsigned long base_ptr, region_base, region_size;
  814. int cs, bootbus;
  815. __be32 new_reg[6];
  816. __be32 *ranges;
  817. int len;
  818. int led = fdt_path_offset(initial_boot_params, alias_prop);
  819. base_ptr = octeon_bootinfo->led_display_base_addr;
  820. if (base_ptr == 0)
  821. goto no_led;
  822. /* Find CS0 region. */
  823. for (cs = 0; cs < 8; cs++) {
  824. mio_boot_reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));
  825. region_base = mio_boot_reg_cfg.s.base << 16;
  826. region_size = (mio_boot_reg_cfg.s.size + 1) << 16;
  827. if (mio_boot_reg_cfg.s.en && base_ptr >= region_base
  828. && base_ptr < region_base + region_size)
  829. break;
  830. }
  831. if (cs > 7)
  832. goto no_led;
  833. new_reg[0] = cpu_to_be32(cs);
  834. new_reg[1] = cpu_to_be32(0x20);
  835. new_reg[2] = cpu_to_be32(0x20);
  836. new_reg[3] = cpu_to_be32(cs);
  837. new_reg[4] = cpu_to_be32(0);
  838. new_reg[5] = cpu_to_be32(0x20);
  839. fdt_setprop_inplace(initial_boot_params, led,
  840. "reg", new_reg, sizeof(new_reg));
  841. bootbus = fdt_parent_offset(initial_boot_params, led);
  842. if (bootbus < 0)
  843. goto no_led;
  844. ranges = fdt_getprop_w(initial_boot_params, bootbus, "ranges", &len);
  845. if (!ranges || len < (5 * 8 * sizeof(__be32)))
  846. goto no_led;
  847. ranges[(cs * 5) + 2] = cpu_to_be32(region_base >> 32);
  848. ranges[(cs * 5) + 3] = cpu_to_be32(region_base & 0xffffffff);
  849. ranges[(cs * 5) + 4] = cpu_to_be32(region_size);
  850. goto end_led;
  851. no_led:
  852. fdt_nop_node(initial_boot_params, led);
  853. end_led:
  854. ;
  855. }
  856. #ifdef CONFIG_USB
  857. /* OHCI/UHCI USB */
  858. alias_prop = fdt_getprop(initial_boot_params, aliases,
  859. "uctl", NULL);
  860. if (alias_prop) {
  861. int uctl = fdt_path_offset(initial_boot_params, alias_prop);
  862. if (uctl >= 0 && (!OCTEON_IS_MODEL(OCTEON_CN6XXX) ||
  863. octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC2E)) {
  864. pr_debug("Deleting uctl\n");
  865. fdt_nop_node(initial_boot_params, uctl);
  866. fdt_nop_property(initial_boot_params, aliases, "uctl");
  867. } else if (octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC10E ||
  868. octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC4E) {
  869. /* Missing "refclk-type" defaults to crystal. */
  870. fdt_nop_property(initial_boot_params, uctl, "refclk-type");
  871. }
  872. }
  873. /* DWC2 USB */
  874. alias_prop = fdt_getprop(initial_boot_params, aliases,
  875. "usbn", NULL);
  876. if (alias_prop) {
  877. int usbn = fdt_path_offset(initial_boot_params, alias_prop);
  878. if (usbn >= 0 && (current_cpu_type() == CPU_CAVIUM_OCTEON2 ||
  879. !octeon_has_feature(OCTEON_FEATURE_USB))) {
  880. pr_debug("Deleting usbn\n");
  881. fdt_nop_node(initial_boot_params, usbn);
  882. fdt_nop_property(initial_boot_params, aliases, "usbn");
  883. } else {
  884. __be32 new_f[1];
  885. enum cvmx_helper_board_usb_clock_types c;
  886. c = __cvmx_helper_board_usb_get_clock_type();
  887. switch (c) {
  888. case USB_CLOCK_TYPE_REF_48:
  889. new_f[0] = cpu_to_be32(48000000);
  890. fdt_setprop_inplace(initial_boot_params, usbn,
  891. "refclk-frequency", new_f, sizeof(new_f));
  892. /* Fall through ...*/
  893. case USB_CLOCK_TYPE_REF_12:
  894. /* Missing "refclk-type" defaults to external. */
  895. fdt_nop_property(initial_boot_params, usbn, "refclk-type");
  896. break;
  897. default:
  898. break;
  899. }
  900. }
  901. }
  902. #endif
  903. return 0;
  904. }
  905. static int __init octeon_publish_devices(void)
  906. {
  907. return of_platform_populate(NULL, octeon_ids, NULL, NULL);
  908. }
  909. arch_initcall(octeon_publish_devices);