octeon-platform.c 27 KB

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