octeon-platform.c 25 KB

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