Path: blob/master/arch/mips/cavium-octeon/octeon-platform.c
29519 views
/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (C) 2004-2017 Cavium, Inc.6* Copyright (C) 2008 Wind River Systems7*/89#include <linux/etherdevice.h>10#include <linux/of.h>11#include <linux/of_platform.h>12#include <linux/of_fdt.h>13#include <linux/platform_device.h>14#include <linux/libfdt.h>15#include <linux/string.h>1617#include <asm/octeon/octeon.h>18#include <asm/octeon/cvmx-helper-board.h>1920#ifdef CONFIG_USB21#include <linux/usb/ehci_def.h>22#include <linux/usb/ehci_pdriver.h>23#include <linux/usb/ohci_pdriver.h>24#include <asm/octeon/cvmx-uctlx-defs.h>2526#define CVMX_UAHCX_EHCI_USBCMD (CVMX_ADD_IO_SEG(0x00016F0000000010ull))27#define CVMX_UAHCX_OHCI_USBCMD (CVMX_ADD_IO_SEG(0x00016F0000000408ull))2829static DEFINE_MUTEX(octeon2_usb_clocks_mutex);3031static int octeon2_usb_clock_start_cnt;3233static int __init octeon2_usb_reset(void)34{35union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;36u32 ucmd;3738if (!OCTEON_IS_OCTEON2())39return 0;4041clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));42if (clk_rst_ctl.s.hrst) {43ucmd = cvmx_read64_uint32(CVMX_UAHCX_EHCI_USBCMD);44ucmd &= ~CMD_RUN;45cvmx_write64_uint32(CVMX_UAHCX_EHCI_USBCMD, ucmd);46mdelay(2);47ucmd |= CMD_RESET;48cvmx_write64_uint32(CVMX_UAHCX_EHCI_USBCMD, ucmd);49ucmd = cvmx_read64_uint32(CVMX_UAHCX_OHCI_USBCMD);50ucmd |= CMD_RUN;51cvmx_write64_uint32(CVMX_UAHCX_OHCI_USBCMD, ucmd);52}5354return 0;55}56arch_initcall(octeon2_usb_reset);5758static void octeon2_usb_clocks_start(struct device *dev)59{60u64 div;61union cvmx_uctlx_if_ena if_ena;62union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;63union cvmx_uctlx_uphy_portx_ctl_status port_ctl_status;64int i;65unsigned long io_clk_64_to_ns;66u32 clock_rate = 12000000;67bool is_crystal_clock = false;686970mutex_lock(&octeon2_usb_clocks_mutex);7172octeon2_usb_clock_start_cnt++;73if (octeon2_usb_clock_start_cnt != 1)74goto exit;7576io_clk_64_to_ns = 64000000000ull / octeon_get_io_clock_rate();7778if (dev->of_node) {79struct device_node *uctl_node;80const char *clock_type;8182uctl_node = of_get_parent(dev->of_node);83if (!uctl_node) {84dev_err(dev, "No UCTL device node\n");85goto exit;86}87i = of_property_read_u32(uctl_node,88"refclk-frequency", &clock_rate);89if (i) {90dev_err(dev, "No UCTL \"refclk-frequency\"\n");91of_node_put(uctl_node);92goto exit;93}94i = of_property_read_string(uctl_node,95"refclk-type", &clock_type);96of_node_put(uctl_node);97if (!i && strcmp("crystal", clock_type) == 0)98is_crystal_clock = true;99}100101/*102* Step 1: Wait for voltages stable. That surely happened103* before starting the kernel.104*105* Step 2: Enable SCLK of UCTL by writing UCTL0_IF_ENA[EN] = 1106*/107if_ena.u64 = 0;108if_ena.s.en = 1;109cvmx_write_csr(CVMX_UCTLX_IF_ENA(0), if_ena.u64);110111for (i = 0; i <= 1; i++) {112port_ctl_status.u64 =113cvmx_read_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0));114/* Set txvreftune to 15 to obtain compliant 'eye' diagram. */115port_ctl_status.s.txvreftune = 15;116port_ctl_status.s.txrisetune = 1;117port_ctl_status.s.txpreemphasistune = 1;118cvmx_write_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0),119port_ctl_status.u64);120}121122/* Step 3: Configure the reference clock, PHY, and HCLK */123clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));124125/*126* If the UCTL looks like it has already been started, skip127* the initialization, otherwise bus errors are obtained.128*/129if (clk_rst_ctl.s.hrst)130goto end_clock;131/* 3a */132clk_rst_ctl.s.p_por = 1;133clk_rst_ctl.s.hrst = 0;134clk_rst_ctl.s.p_prst = 0;135clk_rst_ctl.s.h_clkdiv_rst = 0;136clk_rst_ctl.s.o_clkdiv_rst = 0;137clk_rst_ctl.s.h_clkdiv_en = 0;138clk_rst_ctl.s.o_clkdiv_en = 0;139cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);140141/* 3b */142clk_rst_ctl.s.p_refclk_sel = is_crystal_clock ? 0 : 1;143switch (clock_rate) {144default:145pr_err("Invalid UCTL clock rate of %u, using 12000000 instead\n",146clock_rate);147fallthrough;148case 12000000:149clk_rst_ctl.s.p_refclk_div = 0;150break;151case 24000000:152clk_rst_ctl.s.p_refclk_div = 1;153break;154case 48000000:155clk_rst_ctl.s.p_refclk_div = 2;156break;157}158cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);159160/* 3c */161div = octeon_get_io_clock_rate() / 130000000ull;162163switch (div) {164case 0:165div = 1;166break;167case 1:168case 2:169case 3:170case 4:171break;172case 5:173div = 4;174break;175case 6:176case 7:177div = 6;178break;179case 8:180case 9:181case 10:182case 11:183div = 8;184break;185default:186div = 12;187break;188}189clk_rst_ctl.s.h_div = div;190cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);191/* Read it back, */192clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));193clk_rst_ctl.s.h_clkdiv_en = 1;194cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);195/* 3d */196clk_rst_ctl.s.h_clkdiv_rst = 1;197cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);198199/* 3e: delay 64 io clocks */200ndelay(io_clk_64_to_ns);201202/*203* Step 4: Program the power-on reset field in the UCTL204* clock-reset-control register.205*/206clk_rst_ctl.s.p_por = 0;207cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);208209/* Step 5: Wait 3 ms for the PHY clock to start. */210mdelay(3);211212/* Steps 6..9 for ATE only, are skipped. */213214/* Step 10: Configure the OHCI_CLK48 and OHCI_CLK12 clocks. */215/* 10a */216clk_rst_ctl.s.o_clkdiv_rst = 1;217cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);218219/* 10b */220clk_rst_ctl.s.o_clkdiv_en = 1;221cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);222223/* 10c */224ndelay(io_clk_64_to_ns);225226/*227* Step 11: Program the PHY reset field:228* UCTL0_CLK_RST_CTL[P_PRST] = 1229*/230clk_rst_ctl.s.p_prst = 1;231cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);232233/* Step 11b */234udelay(1);235236/* Step 11c */237clk_rst_ctl.s.p_prst = 0;238cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);239240/* Step 11d */241mdelay(1);242243/* Step 11e */244clk_rst_ctl.s.p_prst = 1;245cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);246247/* Step 12: Wait 1 uS. */248udelay(1);249250/* Step 13: Program the HRESET_N field: UCTL0_CLK_RST_CTL[HRST] = 1 */251clk_rst_ctl.s.hrst = 1;252cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);253254end_clock:255/* Set uSOF cycle period to 60,000 bits. */256cvmx_write_csr(CVMX_UCTLX_EHCI_FLA(0), 0x20ull);257258exit:259mutex_unlock(&octeon2_usb_clocks_mutex);260}261262static void octeon2_usb_clocks_stop(void)263{264mutex_lock(&octeon2_usb_clocks_mutex);265octeon2_usb_clock_start_cnt--;266mutex_unlock(&octeon2_usb_clocks_mutex);267}268269static int octeon_ehci_power_on(struct platform_device *pdev)270{271octeon2_usb_clocks_start(&pdev->dev);272return 0;273}274275static void octeon_ehci_power_off(struct platform_device *pdev)276{277octeon2_usb_clocks_stop();278}279280static struct usb_ehci_pdata octeon_ehci_pdata = {281/* Octeon EHCI matches CPU endianness. */282#ifdef __BIG_ENDIAN283.big_endian_mmio = 1,284#endif285/*286* We can DMA from anywhere. But the descriptors must be in287* the lower 4GB.288*/289.dma_mask_64 = 0,290.power_on = octeon_ehci_power_on,291.power_off = octeon_ehci_power_off,292};293294static void __init octeon_ehci_hw_start(struct device *dev)295{296union cvmx_uctlx_ehci_ctl ehci_ctl;297298octeon2_usb_clocks_start(dev);299300ehci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_EHCI_CTL(0));301/* Use 64-bit addressing. */302ehci_ctl.s.ehci_64b_addr_en = 1;303ehci_ctl.s.l2c_addr_msb = 0;304#ifdef __BIG_ENDIAN305ehci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */306ehci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */307#else308ehci_ctl.s.l2c_buff_emod = 0; /* not swapped. */309ehci_ctl.s.l2c_desc_emod = 0; /* not swapped. */310ehci_ctl.s.inv_reg_a2 = 1;311#endif312cvmx_write_csr(CVMX_UCTLX_EHCI_CTL(0), ehci_ctl.u64);313314octeon2_usb_clocks_stop();315}316317static int __init octeon_ehci_device_init(void)318{319struct platform_device *pd;320struct device_node *ehci_node;321int ret = 0;322323ehci_node = of_find_node_by_name(NULL, "ehci");324if (!ehci_node)325return 0;326327pd = of_find_device_by_node(ehci_node);328of_node_put(ehci_node);329if (!pd)330return 0;331332pd->dev.platform_data = &octeon_ehci_pdata;333octeon_ehci_hw_start(&pd->dev);334put_device(&pd->dev);335336return ret;337}338device_initcall(octeon_ehci_device_init);339340static int octeon_ohci_power_on(struct platform_device *pdev)341{342octeon2_usb_clocks_start(&pdev->dev);343return 0;344}345346static void octeon_ohci_power_off(struct platform_device *pdev)347{348octeon2_usb_clocks_stop();349}350351static struct usb_ohci_pdata octeon_ohci_pdata = {352/* Octeon OHCI matches CPU endianness. */353#ifdef __BIG_ENDIAN354.big_endian_mmio = 1,355#endif356.power_on = octeon_ohci_power_on,357.power_off = octeon_ohci_power_off,358};359360static void __init octeon_ohci_hw_start(struct device *dev)361{362union cvmx_uctlx_ohci_ctl ohci_ctl;363364octeon2_usb_clocks_start(dev);365366ohci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_OHCI_CTL(0));367ohci_ctl.s.l2c_addr_msb = 0;368#ifdef __BIG_ENDIAN369ohci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */370ohci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */371#else372ohci_ctl.s.l2c_buff_emod = 0; /* not swapped. */373ohci_ctl.s.l2c_desc_emod = 0; /* not swapped. */374ohci_ctl.s.inv_reg_a2 = 1;375#endif376cvmx_write_csr(CVMX_UCTLX_OHCI_CTL(0), ohci_ctl.u64);377378octeon2_usb_clocks_stop();379}380381static int __init octeon_ohci_device_init(void)382{383struct platform_device *pd;384struct device_node *ohci_node;385int ret = 0;386387ohci_node = of_find_node_by_name(NULL, "ohci");388if (!ohci_node)389return 0;390391pd = of_find_device_by_node(ohci_node);392of_node_put(ohci_node);393if (!pd)394return 0;395396pd->dev.platform_data = &octeon_ohci_pdata;397octeon_ohci_hw_start(&pd->dev);398put_device(&pd->dev);399400return ret;401}402device_initcall(octeon_ohci_device_init);403404#endif /* CONFIG_USB */405406/* Octeon Random Number Generator. */407static int __init octeon_rng_device_init(void)408{409struct platform_device *pd;410int ret = 0;411412struct resource rng_resources[] = {413{414.flags = IORESOURCE_MEM,415.start = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS),416.end = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS) + 0xf417}, {418.flags = IORESOURCE_MEM,419.start = cvmx_build_io_address(8, 0),420.end = cvmx_build_io_address(8, 0) + 0x7421}422};423424pd = platform_device_alloc("octeon_rng", -1);425if (!pd) {426ret = -ENOMEM;427goto out;428}429430ret = platform_device_add_resources(pd, rng_resources,431ARRAY_SIZE(rng_resources));432if (ret)433goto fail;434435ret = platform_device_add(pd);436if (ret)437goto fail;438439return ret;440fail:441platform_device_put(pd);442443out:444return ret;445}446device_initcall(octeon_rng_device_init);447448static const struct of_device_id octeon_ids[] __initconst = {449{ .compatible = "simple-bus", },450{ .compatible = "cavium,octeon-6335-uctl", },451{ .compatible = "cavium,octeon-5750-usbn", },452{ .compatible = "cavium,octeon-3860-bootbus", },453{ .compatible = "cavium,mdio-mux", },454{ .compatible = "gpio-leds", },455{},456};457458static bool __init octeon_has_88e1145(void)459{460return !OCTEON_IS_MODEL(OCTEON_CN52XX) &&461!OCTEON_IS_MODEL(OCTEON_CN6XXX) &&462!OCTEON_IS_MODEL(OCTEON_CN56XX);463}464465static bool __init octeon_has_fixed_link(int ipd_port)466{467switch (cvmx_sysinfo_get()->board_type) {468case CVMX_BOARD_TYPE_CN3005_EVB_HS5:469case CVMX_BOARD_TYPE_CN3010_EVB_HS5:470case CVMX_BOARD_TYPE_CN3020_EVB_HS5:471case CVMX_BOARD_TYPE_CUST_NB5:472case CVMX_BOARD_TYPE_EBH3100:473/* Port 1 on these boards is always gigabit. */474return ipd_port == 1;475case CVMX_BOARD_TYPE_BBGW_REF:476/* Ports 0 and 1 connect to the switch. */477return ipd_port == 0 || ipd_port == 1;478}479return false;480}481482static void __init octeon_fdt_set_phy(int eth, int phy_addr)483{484const __be32 *phy_handle;485const __be32 *alt_phy_handle;486const __be32 *reg;487u32 phandle;488int phy;489int alt_phy;490const char *p;491int current_len;492char new_name[20];493494phy_handle = fdt_getprop(initial_boot_params, eth, "phy-handle", NULL);495if (!phy_handle)496return;497498phandle = be32_to_cpup(phy_handle);499phy = fdt_node_offset_by_phandle(initial_boot_params, phandle);500501alt_phy_handle = fdt_getprop(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);502if (alt_phy_handle) {503u32 alt_phandle = be32_to_cpup(alt_phy_handle);504505alt_phy = fdt_node_offset_by_phandle(initial_boot_params, alt_phandle);506} else {507alt_phy = -1;508}509510if (phy_addr < 0 || phy < 0) {511/* Delete the PHY things */512fdt_nop_property(initial_boot_params, eth, "phy-handle");513/* This one may fail */514fdt_nop_property(initial_boot_params, eth, "cavium,alt-phy-handle");515if (phy >= 0)516fdt_nop_node(initial_boot_params, phy);517if (alt_phy >= 0)518fdt_nop_node(initial_boot_params, alt_phy);519return;520}521522if (phy_addr >= 256 && alt_phy > 0) {523const struct fdt_property *phy_prop;524struct fdt_property *alt_prop;525fdt32_t phy_handle_name;526527/* Use the alt phy node instead.*/528phy_prop = fdt_get_property(initial_boot_params, eth, "phy-handle", NULL);529phy_handle_name = phy_prop->nameoff;530fdt_nop_node(initial_boot_params, phy);531fdt_nop_property(initial_boot_params, eth, "phy-handle");532alt_prop = fdt_get_property_w(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);533alt_prop->nameoff = phy_handle_name;534phy = alt_phy;535}536537phy_addr &= 0xff;538539if (octeon_has_88e1145()) {540fdt_nop_property(initial_boot_params, phy, "marvell,reg-init");541strscpy_pad(new_name, "marvell,88e1145");542p = fdt_getprop(initial_boot_params, phy, "compatible",543¤t_len);544if (p && current_len >= strlen(new_name))545fdt_setprop_inplace(initial_boot_params, phy,546"compatible", new_name, current_len);547}548549reg = fdt_getprop(initial_boot_params, phy, "reg", NULL);550if (phy_addr == be32_to_cpup(reg))551return;552553fdt_setprop_inplace_cell(initial_boot_params, phy, "reg", phy_addr);554555snprintf(new_name, sizeof(new_name), "ethernet-phy@%x", phy_addr);556557p = fdt_get_name(initial_boot_params, phy, ¤t_len);558if (p && current_len == strlen(new_name))559fdt_set_name(initial_boot_params, phy, new_name);560else561pr_err("Error: could not rename ethernet phy: <%s>", p);562}563564static void __init octeon_fdt_set_mac_addr(int n, u64 *pmac)565{566const u8 *old_mac;567int old_len;568u8 new_mac[6];569u64 mac = *pmac;570int r;571572old_mac = fdt_getprop(initial_boot_params, n, "local-mac-address",573&old_len);574if (!old_mac || old_len != 6 || is_valid_ether_addr(old_mac))575return;576577new_mac[0] = (mac >> 40) & 0xff;578new_mac[1] = (mac >> 32) & 0xff;579new_mac[2] = (mac >> 24) & 0xff;580new_mac[3] = (mac >> 16) & 0xff;581new_mac[4] = (mac >> 8) & 0xff;582new_mac[5] = mac & 0xff;583584r = fdt_setprop_inplace(initial_boot_params, n, "local-mac-address",585new_mac, sizeof(new_mac));586587if (r) {588pr_err("Setting \"local-mac-address\" failed %d", r);589return;590}591*pmac = mac + 1;592}593594static void __init octeon_fdt_rm_ethernet(int node)595{596const __be32 *phy_handle;597598phy_handle = fdt_getprop(initial_boot_params, node, "phy-handle", NULL);599if (phy_handle) {600u32 ph = be32_to_cpup(phy_handle);601int p = fdt_node_offset_by_phandle(initial_boot_params, ph);602603if (p >= 0)604fdt_nop_node(initial_boot_params, p);605}606fdt_nop_node(initial_boot_params, node);607}608609static void __init _octeon_rx_tx_delay(int eth, int rx_delay, int tx_delay)610{611fdt_setprop_inplace_cell(initial_boot_params, eth, "rx-delay",612rx_delay);613fdt_setprop_inplace_cell(initial_boot_params, eth, "tx-delay",614tx_delay);615}616617static void __init octeon_rx_tx_delay(int eth, int iface, int port)618{619switch (cvmx_sysinfo_get()->board_type) {620case CVMX_BOARD_TYPE_CN3005_EVB_HS5:621if (iface == 0) {622if (port == 0) {623/*624* Boards with gigabit WAN ports need a625* different setting that is compatible with626* 100 Mbit settings627*/628_octeon_rx_tx_delay(eth, 0xc, 0x0c);629return;630} else if (port == 1) {631/* Different config for switch port. */632_octeon_rx_tx_delay(eth, 0x0, 0x0);633return;634}635}636break;637case CVMX_BOARD_TYPE_UBNT_E100:638if (iface == 0 && port <= 2) {639_octeon_rx_tx_delay(eth, 0x0, 0x10);640return;641}642break;643}644fdt_nop_property(initial_boot_params, eth, "rx-delay");645fdt_nop_property(initial_boot_params, eth, "tx-delay");646}647648static void __init octeon_fdt_pip_port(int iface, int i, int p, int max)649{650char name_buffer[20];651int eth;652int phy_addr;653int ipd_port;654int fixed_link;655656snprintf(name_buffer, sizeof(name_buffer), "ethernet@%x", p);657eth = fdt_subnode_offset(initial_boot_params, iface, name_buffer);658if (eth < 0)659return;660if (p > max) {661pr_debug("Deleting port %x:%x\n", i, p);662octeon_fdt_rm_ethernet(eth);663return;664}665if (OCTEON_IS_MODEL(OCTEON_CN68XX))666ipd_port = (0x100 * i) + (0x10 * p) + 0x800;667else668ipd_port = 16 * i + p;669670phy_addr = cvmx_helper_board_get_mii_address(ipd_port);671octeon_fdt_set_phy(eth, phy_addr);672673fixed_link = fdt_subnode_offset(initial_boot_params, eth, "fixed-link");674if (fixed_link < 0)675WARN_ON(octeon_has_fixed_link(ipd_port));676else if (!octeon_has_fixed_link(ipd_port))677fdt_nop_node(initial_boot_params, fixed_link);678octeon_rx_tx_delay(eth, i, p);679}680681static void __init octeon_fdt_pip_iface(int pip, int idx)682{683char name_buffer[20];684int iface;685int p;686int count = 0;687688snprintf(name_buffer, sizeof(name_buffer), "interface@%d", idx);689iface = fdt_subnode_offset(initial_boot_params, pip, name_buffer);690if (iface < 0)691return;692693if (cvmx_helper_interface_enumerate(idx) == 0)694count = cvmx_helper_ports_on_interface(idx);695696for (p = 0; p < 16; p++)697octeon_fdt_pip_port(iface, idx, p, count - 1);698}699700void __init octeon_fill_mac_addresses(void)701{702const char *alias_prop;703char name_buffer[20];704u64 mac_addr_base;705int aliases;706int pip;707int i;708709aliases = fdt_path_offset(initial_boot_params, "/aliases");710if (aliases < 0)711return;712713mac_addr_base =714((octeon_bootinfo->mac_addr_base[0] & 0xffull)) << 40 |715((octeon_bootinfo->mac_addr_base[1] & 0xffull)) << 32 |716((octeon_bootinfo->mac_addr_base[2] & 0xffull)) << 24 |717((octeon_bootinfo->mac_addr_base[3] & 0xffull)) << 16 |718((octeon_bootinfo->mac_addr_base[4] & 0xffull)) << 8 |719(octeon_bootinfo->mac_addr_base[5] & 0xffull);720721for (i = 0; i < 2; i++) {722int mgmt;723724snprintf(name_buffer, sizeof(name_buffer), "mix%d", i);725alias_prop = fdt_getprop(initial_boot_params, aliases,726name_buffer, NULL);727if (!alias_prop)728continue;729mgmt = fdt_path_offset(initial_boot_params, alias_prop);730if (mgmt < 0)731continue;732octeon_fdt_set_mac_addr(mgmt, &mac_addr_base);733}734735alias_prop = fdt_getprop(initial_boot_params, aliases, "pip", NULL);736if (!alias_prop)737return;738739pip = fdt_path_offset(initial_boot_params, alias_prop);740if (pip < 0)741return;742743for (i = 0; i <= 4; i++) {744int iface;745int p;746747snprintf(name_buffer, sizeof(name_buffer), "interface@%d", i);748iface = fdt_subnode_offset(initial_boot_params, pip,749name_buffer);750if (iface < 0)751continue;752for (p = 0; p < 16; p++) {753int eth;754755snprintf(name_buffer, sizeof(name_buffer),756"ethernet@%x", p);757eth = fdt_subnode_offset(initial_boot_params, iface,758name_buffer);759if (eth < 0)760continue;761octeon_fdt_set_mac_addr(eth, &mac_addr_base);762}763}764}765766int __init octeon_prune_device_tree(void)767{768int i, max_port, uart_mask;769const char *pip_path;770const char *alias_prop;771char name_buffer[20];772int aliases;773774if (fdt_check_header(initial_boot_params))775panic("Corrupt Device Tree.");776777WARN(octeon_bootinfo->board_type == CVMX_BOARD_TYPE_CUST_DSR1000N,778"Built-in DTB booting is deprecated on %s. Please switch to use appended DTB.",779cvmx_board_type_to_string(octeon_bootinfo->board_type));780781aliases = fdt_path_offset(initial_boot_params, "/aliases");782if (aliases < 0) {783pr_err("Error: No /aliases node in device tree.");784return -EINVAL;785}786787if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))788max_port = 2;789else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN68XX))790max_port = 1;791else792max_port = 0;793794if (octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC10E)795max_port = 0;796797for (i = 0; i < 2; i++) {798int mgmt;799800snprintf(name_buffer, sizeof(name_buffer),801"mix%d", i);802alias_prop = fdt_getprop(initial_boot_params, aliases,803name_buffer, NULL);804if (alias_prop) {805mgmt = fdt_path_offset(initial_boot_params, alias_prop);806if (mgmt < 0)807continue;808if (i >= max_port) {809pr_debug("Deleting mix%d\n", i);810octeon_fdt_rm_ethernet(mgmt);811fdt_nop_property(initial_boot_params, aliases,812name_buffer);813} else {814int phy_addr = cvmx_helper_board_get_mii_address(CVMX_HELPER_BOARD_MGMT_IPD_PORT + i);815816octeon_fdt_set_phy(mgmt, phy_addr);817}818}819}820821pip_path = fdt_getprop(initial_boot_params, aliases, "pip", NULL);822if (pip_path) {823int pip = fdt_path_offset(initial_boot_params, pip_path);824825if (pip >= 0)826for (i = 0; i <= 4; i++)827octeon_fdt_pip_iface(pip, i);828}829830/* I2C */831if (OCTEON_IS_MODEL(OCTEON_CN52XX) ||832OCTEON_IS_MODEL(OCTEON_CN63XX) ||833OCTEON_IS_MODEL(OCTEON_CN68XX) ||834OCTEON_IS_MODEL(OCTEON_CN56XX))835max_port = 2;836else837max_port = 1;838839for (i = 0; i < 2; i++) {840int i2c;841842snprintf(name_buffer, sizeof(name_buffer),843"twsi%d", i);844alias_prop = fdt_getprop(initial_boot_params, aliases,845name_buffer, NULL);846847if (alias_prop) {848i2c = fdt_path_offset(initial_boot_params, alias_prop);849if (i2c < 0)850continue;851if (i >= max_port) {852pr_debug("Deleting twsi%d\n", i);853fdt_nop_node(initial_boot_params, i2c);854fdt_nop_property(initial_boot_params, aliases,855name_buffer);856}857}858}859860/* SMI/MDIO */861if (OCTEON_IS_MODEL(OCTEON_CN68XX))862max_port = 4;863else if (OCTEON_IS_MODEL(OCTEON_CN52XX) ||864OCTEON_IS_MODEL(OCTEON_CN63XX) ||865OCTEON_IS_MODEL(OCTEON_CN56XX))866max_port = 2;867else868max_port = 1;869870for (i = 0; i < 2; i++) {871int i2c;872873snprintf(name_buffer, sizeof(name_buffer),874"smi%d", i);875alias_prop = fdt_getprop(initial_boot_params, aliases,876name_buffer, NULL);877if (alias_prop) {878i2c = fdt_path_offset(initial_boot_params, alias_prop);879if (i2c < 0)880continue;881if (i >= max_port) {882pr_debug("Deleting smi%d\n", i);883fdt_nop_node(initial_boot_params, i2c);884fdt_nop_property(initial_boot_params, aliases,885name_buffer);886}887}888}889890/* Serial */891uart_mask = 3;892893/* Right now CN52XX is the only chip with a third uart */894if (OCTEON_IS_MODEL(OCTEON_CN52XX))895uart_mask |= 4; /* uart2 */896897for (i = 0; i < 3; i++) {898int uart;899900snprintf(name_buffer, sizeof(name_buffer),901"uart%d", i);902alias_prop = fdt_getprop(initial_boot_params, aliases,903name_buffer, NULL);904905if (alias_prop) {906uart = fdt_path_offset(initial_boot_params, alias_prop);907if (uart_mask & (1 << i)) {908__be32 f;909910f = cpu_to_be32(octeon_get_io_clock_rate());911fdt_setprop_inplace(initial_boot_params,912uart, "clock-frequency",913&f, sizeof(f));914continue;915}916pr_debug("Deleting uart%d\n", i);917fdt_nop_node(initial_boot_params, uart);918fdt_nop_property(initial_boot_params, aliases,919name_buffer);920}921}922923/* Compact Flash */924alias_prop = fdt_getprop(initial_boot_params, aliases,925"cf0", NULL);926if (alias_prop) {927union cvmx_mio_boot_reg_cfgx mio_boot_reg_cfg;928unsigned long base_ptr, region_base, region_size;929unsigned long region1_base = 0;930unsigned long region1_size = 0;931int cs, bootbus;932bool is_16bit = false;933bool is_true_ide = false;934__be32 new_reg[6];935__be32 *ranges;936int len;937938int cf = fdt_path_offset(initial_boot_params, alias_prop);939940base_ptr = 0;941if (octeon_bootinfo->major_version == 1942&& octeon_bootinfo->minor_version >= 1) {943if (octeon_bootinfo->compact_flash_common_base_addr)944base_ptr = octeon_bootinfo->compact_flash_common_base_addr;945} else {946base_ptr = 0x1d000800;947}948949if (!base_ptr)950goto no_cf;951952/* Find CS0 region. */953for (cs = 0; cs < 8; cs++) {954mio_boot_reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));955region_base = mio_boot_reg_cfg.s.base << 16;956region_size = (mio_boot_reg_cfg.s.size + 1) << 16;957if (mio_boot_reg_cfg.s.en && base_ptr >= region_base958&& base_ptr < region_base + region_size) {959is_16bit = mio_boot_reg_cfg.s.width;960break;961}962}963if (cs >= 7) {964/* cs and cs + 1 are CS0 and CS1, both must be less than 8. */965goto no_cf;966}967968if (!(base_ptr & 0xfffful)) {969/*970* Boot loader signals availability of DMA (true_ide971* mode) by setting low order bits of base_ptr to972* zero.973*/974975/* Assume that CS1 immediately follows. */976mio_boot_reg_cfg.u64 =977cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs + 1));978region1_base = mio_boot_reg_cfg.s.base << 16;979region1_size = (mio_boot_reg_cfg.s.size + 1) << 16;980if (!mio_boot_reg_cfg.s.en)981goto no_cf;982is_true_ide = true;983984} else {985fdt_nop_property(initial_boot_params, cf, "cavium,true-ide");986fdt_nop_property(initial_boot_params, cf, "cavium,dma-engine-handle");987if (!is_16bit) {988__be32 width = cpu_to_be32(8);989990fdt_setprop_inplace(initial_boot_params, cf,991"cavium,bus-width", &width, sizeof(width));992}993}994new_reg[0] = cpu_to_be32(cs);995new_reg[1] = cpu_to_be32(0);996new_reg[2] = cpu_to_be32(0x10000);997new_reg[3] = cpu_to_be32(cs + 1);998new_reg[4] = cpu_to_be32(0);999new_reg[5] = cpu_to_be32(0x10000);1000fdt_setprop_inplace(initial_boot_params, cf,1001"reg", new_reg, sizeof(new_reg));10021003bootbus = fdt_parent_offset(initial_boot_params, cf);1004if (bootbus < 0)1005goto no_cf;1006ranges = fdt_getprop_w(initial_boot_params, bootbus, "ranges", &len);1007if (!ranges || len < (5 * 8 * sizeof(__be32)))1008goto no_cf;10091010ranges[(cs * 5) + 2] = cpu_to_be32(region_base >> 32);1011ranges[(cs * 5) + 3] = cpu_to_be32(region_base & 0xffffffff);1012ranges[(cs * 5) + 4] = cpu_to_be32(region_size);1013if (is_true_ide) {1014cs++;1015ranges[(cs * 5) + 2] = cpu_to_be32(region1_base >> 32);1016ranges[(cs * 5) + 3] = cpu_to_be32(region1_base & 0xffffffff);1017ranges[(cs * 5) + 4] = cpu_to_be32(region1_size);1018}1019goto end_cf;1020no_cf:1021fdt_nop_node(initial_boot_params, cf);10221023end_cf:1024;1025}10261027/* 8 char LED */1028alias_prop = fdt_getprop(initial_boot_params, aliases,1029"led0", NULL);1030if (alias_prop) {1031union cvmx_mio_boot_reg_cfgx mio_boot_reg_cfg;1032unsigned long base_ptr, region_base, region_size;1033int cs, bootbus;1034__be32 new_reg[6];1035__be32 *ranges;1036int len;1037int led = fdt_path_offset(initial_boot_params, alias_prop);10381039base_ptr = octeon_bootinfo->led_display_base_addr;1040if (base_ptr == 0)1041goto no_led;1042/* Find CS0 region. */1043for (cs = 0; cs < 8; cs++) {1044mio_boot_reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));1045region_base = mio_boot_reg_cfg.s.base << 16;1046region_size = (mio_boot_reg_cfg.s.size + 1) << 16;1047if (mio_boot_reg_cfg.s.en && base_ptr >= region_base1048&& base_ptr < region_base + region_size)1049break;1050}10511052if (cs > 7)1053goto no_led;10541055new_reg[0] = cpu_to_be32(cs);1056new_reg[1] = cpu_to_be32(0x20);1057new_reg[2] = cpu_to_be32(0x20);1058new_reg[3] = cpu_to_be32(cs);1059new_reg[4] = cpu_to_be32(0);1060new_reg[5] = cpu_to_be32(0x20);1061fdt_setprop_inplace(initial_boot_params, led,1062"reg", new_reg, sizeof(new_reg));10631064bootbus = fdt_parent_offset(initial_boot_params, led);1065if (bootbus < 0)1066goto no_led;1067ranges = fdt_getprop_w(initial_boot_params, bootbus, "ranges", &len);1068if (!ranges || len < (5 * 8 * sizeof(__be32)))1069goto no_led;10701071ranges[(cs * 5) + 2] = cpu_to_be32(region_base >> 32);1072ranges[(cs * 5) + 3] = cpu_to_be32(region_base & 0xffffffff);1073ranges[(cs * 5) + 4] = cpu_to_be32(region_size);1074goto end_led;10751076no_led:1077fdt_nop_node(initial_boot_params, led);1078end_led:1079;1080}10811082#ifdef CONFIG_USB1083/* OHCI/UHCI USB */1084alias_prop = fdt_getprop(initial_boot_params, aliases,1085"uctl", NULL);1086if (alias_prop) {1087int uctl = fdt_path_offset(initial_boot_params, alias_prop);10881089if (uctl >= 0 && (!OCTEON_IS_MODEL(OCTEON_CN6XXX) ||1090octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC2E)) {1091pr_debug("Deleting uctl\n");1092fdt_nop_node(initial_boot_params, uctl);1093fdt_nop_property(initial_boot_params, aliases, "uctl");1094} else if (octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC10E ||1095octeon_bootinfo->board_type == CVMX_BOARD_TYPE_NIC4E) {1096/* Missing "refclk-type" defaults to crystal. */1097fdt_nop_property(initial_boot_params, uctl, "refclk-type");1098}1099}11001101/* DWC2 USB */1102alias_prop = fdt_getprop(initial_boot_params, aliases,1103"usbn", NULL);1104if (alias_prop) {1105int usbn = fdt_path_offset(initial_boot_params, alias_prop);11061107if (usbn >= 0 && (current_cpu_type() == CPU_CAVIUM_OCTEON2 ||1108!octeon_has_feature(OCTEON_FEATURE_USB))) {1109pr_debug("Deleting usbn\n");1110fdt_nop_node(initial_boot_params, usbn);1111fdt_nop_property(initial_boot_params, aliases, "usbn");1112} else {1113__be32 new_f[1];1114enum cvmx_helper_board_usb_clock_types c;11151116c = __cvmx_helper_board_usb_get_clock_type();1117switch (c) {1118case USB_CLOCK_TYPE_REF_48:1119new_f[0] = cpu_to_be32(48000000);1120fdt_setprop_inplace(initial_boot_params, usbn,1121"refclk-frequency", new_f, sizeof(new_f));1122fallthrough;1123case USB_CLOCK_TYPE_REF_12:1124/* Missing "refclk-type" defaults to external. */1125fdt_nop_property(initial_boot_params, usbn, "refclk-type");1126break;1127default:1128break;1129}1130}1131}1132#endif11331134return 0;1135}11361137static int __init octeon_publish_devices(void)1138{1139return of_platform_populate(NULL, octeon_ids, NULL, NULL);1140}1141arch_initcall(octeon_publish_devices);114211431144