Path: blob/master/arch/mips/cavium-octeon/octeon-platform.c
26424 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>1516#include <asm/octeon/octeon.h>17#include <asm/octeon/cvmx-helper-board.h>1819#ifdef CONFIG_USB20#include <linux/usb/ehci_def.h>21#include <linux/usb/ehci_pdriver.h>22#include <linux/usb/ohci_pdriver.h>23#include <asm/octeon/cvmx-uctlx-defs.h>2425#define CVMX_UAHCX_EHCI_USBCMD (CVMX_ADD_IO_SEG(0x00016F0000000010ull))26#define CVMX_UAHCX_OHCI_USBCMD (CVMX_ADD_IO_SEG(0x00016F0000000408ull))2728static DEFINE_MUTEX(octeon2_usb_clocks_mutex);2930static int octeon2_usb_clock_start_cnt;3132static int __init octeon2_usb_reset(void)33{34union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;35u32 ucmd;3637if (!OCTEON_IS_OCTEON2())38return 0;3940clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));41if (clk_rst_ctl.s.hrst) {42ucmd = cvmx_read64_uint32(CVMX_UAHCX_EHCI_USBCMD);43ucmd &= ~CMD_RUN;44cvmx_write64_uint32(CVMX_UAHCX_EHCI_USBCMD, ucmd);45mdelay(2);46ucmd |= CMD_RESET;47cvmx_write64_uint32(CVMX_UAHCX_EHCI_USBCMD, ucmd);48ucmd = cvmx_read64_uint32(CVMX_UAHCX_OHCI_USBCMD);49ucmd |= CMD_RUN;50cvmx_write64_uint32(CVMX_UAHCX_OHCI_USBCMD, ucmd);51}5253return 0;54}55arch_initcall(octeon2_usb_reset);5657static void octeon2_usb_clocks_start(struct device *dev)58{59u64 div;60union cvmx_uctlx_if_ena if_ena;61union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;62union cvmx_uctlx_uphy_portx_ctl_status port_ctl_status;63int i;64unsigned long io_clk_64_to_ns;65u32 clock_rate = 12000000;66bool is_crystal_clock = false;676869mutex_lock(&octeon2_usb_clocks_mutex);7071octeon2_usb_clock_start_cnt++;72if (octeon2_usb_clock_start_cnt != 1)73goto exit;7475io_clk_64_to_ns = 64000000000ull / octeon_get_io_clock_rate();7677if (dev->of_node) {78struct device_node *uctl_node;79const char *clock_type;8081uctl_node = of_get_parent(dev->of_node);82if (!uctl_node) {83dev_err(dev, "No UCTL device node\n");84goto exit;85}86i = of_property_read_u32(uctl_node,87"refclk-frequency", &clock_rate);88if (i) {89dev_err(dev, "No UCTL \"refclk-frequency\"\n");90of_node_put(uctl_node);91goto exit;92}93i = of_property_read_string(uctl_node,94"refclk-type", &clock_type);95of_node_put(uctl_node);96if (!i && strcmp("crystal", clock_type) == 0)97is_crystal_clock = true;98}99100/*101* Step 1: Wait for voltages stable. That surely happened102* before starting the kernel.103*104* Step 2: Enable SCLK of UCTL by writing UCTL0_IF_ENA[EN] = 1105*/106if_ena.u64 = 0;107if_ena.s.en = 1;108cvmx_write_csr(CVMX_UCTLX_IF_ENA(0), if_ena.u64);109110for (i = 0; i <= 1; i++) {111port_ctl_status.u64 =112cvmx_read_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0));113/* Set txvreftune to 15 to obtain compliant 'eye' diagram. */114port_ctl_status.s.txvreftune = 15;115port_ctl_status.s.txrisetune = 1;116port_ctl_status.s.txpreemphasistune = 1;117cvmx_write_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0),118port_ctl_status.u64);119}120121/* Step 3: Configure the reference clock, PHY, and HCLK */122clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));123124/*125* If the UCTL looks like it has already been started, skip126* the initialization, otherwise bus errors are obtained.127*/128if (clk_rst_ctl.s.hrst)129goto end_clock;130/* 3a */131clk_rst_ctl.s.p_por = 1;132clk_rst_ctl.s.hrst = 0;133clk_rst_ctl.s.p_prst = 0;134clk_rst_ctl.s.h_clkdiv_rst = 0;135clk_rst_ctl.s.o_clkdiv_rst = 0;136clk_rst_ctl.s.h_clkdiv_en = 0;137clk_rst_ctl.s.o_clkdiv_en = 0;138cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);139140/* 3b */141clk_rst_ctl.s.p_refclk_sel = is_crystal_clock ? 0 : 1;142switch (clock_rate) {143default:144pr_err("Invalid UCTL clock rate of %u, using 12000000 instead\n",145clock_rate);146fallthrough;147case 12000000:148clk_rst_ctl.s.p_refclk_div = 0;149break;150case 24000000:151clk_rst_ctl.s.p_refclk_div = 1;152break;153case 48000000:154clk_rst_ctl.s.p_refclk_div = 2;155break;156}157cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);158159/* 3c */160div = octeon_get_io_clock_rate() / 130000000ull;161162switch (div) {163case 0:164div = 1;165break;166case 1:167case 2:168case 3:169case 4:170break;171case 5:172div = 4;173break;174case 6:175case 7:176div = 6;177break;178case 8:179case 9:180case 10:181case 11:182div = 8;183break;184default:185div = 12;186break;187}188clk_rst_ctl.s.h_div = div;189cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);190/* Read it back, */191clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));192clk_rst_ctl.s.h_clkdiv_en = 1;193cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);194/* 3d */195clk_rst_ctl.s.h_clkdiv_rst = 1;196cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);197198/* 3e: delay 64 io clocks */199ndelay(io_clk_64_to_ns);200201/*202* Step 4: Program the power-on reset field in the UCTL203* clock-reset-control register.204*/205clk_rst_ctl.s.p_por = 0;206cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);207208/* Step 5: Wait 3 ms for the PHY clock to start. */209mdelay(3);210211/* Steps 6..9 for ATE only, are skipped. */212213/* Step 10: Configure the OHCI_CLK48 and OHCI_CLK12 clocks. */214/* 10a */215clk_rst_ctl.s.o_clkdiv_rst = 1;216cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);217218/* 10b */219clk_rst_ctl.s.o_clkdiv_en = 1;220cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);221222/* 10c */223ndelay(io_clk_64_to_ns);224225/*226* Step 11: Program the PHY reset field:227* UCTL0_CLK_RST_CTL[P_PRST] = 1228*/229clk_rst_ctl.s.p_prst = 1;230cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);231232/* Step 11b */233udelay(1);234235/* Step 11c */236clk_rst_ctl.s.p_prst = 0;237cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);238239/* Step 11d */240mdelay(1);241242/* Step 11e */243clk_rst_ctl.s.p_prst = 1;244cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);245246/* Step 12: Wait 1 uS. */247udelay(1);248249/* Step 13: Program the HRESET_N field: UCTL0_CLK_RST_CTL[HRST] = 1 */250clk_rst_ctl.s.hrst = 1;251cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);252253end_clock:254/* Set uSOF cycle period to 60,000 bits. */255cvmx_write_csr(CVMX_UCTLX_EHCI_FLA(0), 0x20ull);256257exit:258mutex_unlock(&octeon2_usb_clocks_mutex);259}260261static void octeon2_usb_clocks_stop(void)262{263mutex_lock(&octeon2_usb_clocks_mutex);264octeon2_usb_clock_start_cnt--;265mutex_unlock(&octeon2_usb_clocks_mutex);266}267268static int octeon_ehci_power_on(struct platform_device *pdev)269{270octeon2_usb_clocks_start(&pdev->dev);271return 0;272}273274static void octeon_ehci_power_off(struct platform_device *pdev)275{276octeon2_usb_clocks_stop();277}278279static struct usb_ehci_pdata octeon_ehci_pdata = {280/* Octeon EHCI matches CPU endianness. */281#ifdef __BIG_ENDIAN282.big_endian_mmio = 1,283#endif284/*285* We can DMA from anywhere. But the descriptors must be in286* the lower 4GB.287*/288.dma_mask_64 = 0,289.power_on = octeon_ehci_power_on,290.power_off = octeon_ehci_power_off,291};292293static void __init octeon_ehci_hw_start(struct device *dev)294{295union cvmx_uctlx_ehci_ctl ehci_ctl;296297octeon2_usb_clocks_start(dev);298299ehci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_EHCI_CTL(0));300/* Use 64-bit addressing. */301ehci_ctl.s.ehci_64b_addr_en = 1;302ehci_ctl.s.l2c_addr_msb = 0;303#ifdef __BIG_ENDIAN304ehci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */305ehci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */306#else307ehci_ctl.s.l2c_buff_emod = 0; /* not swapped. */308ehci_ctl.s.l2c_desc_emod = 0; /* not swapped. */309ehci_ctl.s.inv_reg_a2 = 1;310#endif311cvmx_write_csr(CVMX_UCTLX_EHCI_CTL(0), ehci_ctl.u64);312313octeon2_usb_clocks_stop();314}315316static int __init octeon_ehci_device_init(void)317{318struct platform_device *pd;319struct device_node *ehci_node;320int ret = 0;321322ehci_node = of_find_node_by_name(NULL, "ehci");323if (!ehci_node)324return 0;325326pd = of_find_device_by_node(ehci_node);327of_node_put(ehci_node);328if (!pd)329return 0;330331pd->dev.platform_data = &octeon_ehci_pdata;332octeon_ehci_hw_start(&pd->dev);333put_device(&pd->dev);334335return ret;336}337device_initcall(octeon_ehci_device_init);338339static int octeon_ohci_power_on(struct platform_device *pdev)340{341octeon2_usb_clocks_start(&pdev->dev);342return 0;343}344345static void octeon_ohci_power_off(struct platform_device *pdev)346{347octeon2_usb_clocks_stop();348}349350static struct usb_ohci_pdata octeon_ohci_pdata = {351/* Octeon OHCI matches CPU endianness. */352#ifdef __BIG_ENDIAN353.big_endian_mmio = 1,354#endif355.power_on = octeon_ohci_power_on,356.power_off = octeon_ohci_power_off,357};358359static void __init octeon_ohci_hw_start(struct device *dev)360{361union cvmx_uctlx_ohci_ctl ohci_ctl;362363octeon2_usb_clocks_start(dev);364365ohci_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_OHCI_CTL(0));366ohci_ctl.s.l2c_addr_msb = 0;367#ifdef __BIG_ENDIAN368ohci_ctl.s.l2c_buff_emod = 1; /* Byte swapped. */369ohci_ctl.s.l2c_desc_emod = 1; /* Byte swapped. */370#else371ohci_ctl.s.l2c_buff_emod = 0; /* not swapped. */372ohci_ctl.s.l2c_desc_emod = 0; /* not swapped. */373ohci_ctl.s.inv_reg_a2 = 1;374#endif375cvmx_write_csr(CVMX_UCTLX_OHCI_CTL(0), ohci_ctl.u64);376377octeon2_usb_clocks_stop();378}379380static int __init octeon_ohci_device_init(void)381{382struct platform_device *pd;383struct device_node *ohci_node;384int ret = 0;385386ohci_node = of_find_node_by_name(NULL, "ohci");387if (!ohci_node)388return 0;389390pd = of_find_device_by_node(ohci_node);391of_node_put(ohci_node);392if (!pd)393return 0;394395pd->dev.platform_data = &octeon_ohci_pdata;396octeon_ohci_hw_start(&pd->dev);397put_device(&pd->dev);398399return ret;400}401device_initcall(octeon_ohci_device_init);402403#endif /* CONFIG_USB */404405/* Octeon Random Number Generator. */406static int __init octeon_rng_device_init(void)407{408struct platform_device *pd;409int ret = 0;410411struct resource rng_resources[] = {412{413.flags = IORESOURCE_MEM,414.start = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS),415.end = XKPHYS_TO_PHYS(CVMX_RNM_CTL_STATUS) + 0xf416}, {417.flags = IORESOURCE_MEM,418.start = cvmx_build_io_address(8, 0),419.end = cvmx_build_io_address(8, 0) + 0x7420}421};422423pd = platform_device_alloc("octeon_rng", -1);424if (!pd) {425ret = -ENOMEM;426goto out;427}428429ret = platform_device_add_resources(pd, rng_resources,430ARRAY_SIZE(rng_resources));431if (ret)432goto fail;433434ret = platform_device_add(pd);435if (ret)436goto fail;437438return ret;439fail:440platform_device_put(pd);441442out:443return ret;444}445device_initcall(octeon_rng_device_init);446447static const struct of_device_id octeon_ids[] __initconst = {448{ .compatible = "simple-bus", },449{ .compatible = "cavium,octeon-6335-uctl", },450{ .compatible = "cavium,octeon-5750-usbn", },451{ .compatible = "cavium,octeon-3860-bootbus", },452{ .compatible = "cavium,mdio-mux", },453{ .compatible = "gpio-leds", },454{},455};456457static bool __init octeon_has_88e1145(void)458{459return !OCTEON_IS_MODEL(OCTEON_CN52XX) &&460!OCTEON_IS_MODEL(OCTEON_CN6XXX) &&461!OCTEON_IS_MODEL(OCTEON_CN56XX);462}463464static bool __init octeon_has_fixed_link(int ipd_port)465{466switch (cvmx_sysinfo_get()->board_type) {467case CVMX_BOARD_TYPE_CN3005_EVB_HS5:468case CVMX_BOARD_TYPE_CN3010_EVB_HS5:469case CVMX_BOARD_TYPE_CN3020_EVB_HS5:470case CVMX_BOARD_TYPE_CUST_NB5:471case CVMX_BOARD_TYPE_EBH3100:472/* Port 1 on these boards is always gigabit. */473return ipd_port == 1;474case CVMX_BOARD_TYPE_BBGW_REF:475/* Ports 0 and 1 connect to the switch. */476return ipd_port == 0 || ipd_port == 1;477}478return false;479}480481static void __init octeon_fdt_set_phy(int eth, int phy_addr)482{483const __be32 *phy_handle;484const __be32 *alt_phy_handle;485const __be32 *reg;486u32 phandle;487int phy;488int alt_phy;489const char *p;490int current_len;491char new_name[20];492493phy_handle = fdt_getprop(initial_boot_params, eth, "phy-handle", NULL);494if (!phy_handle)495return;496497phandle = be32_to_cpup(phy_handle);498phy = fdt_node_offset_by_phandle(initial_boot_params, phandle);499500alt_phy_handle = fdt_getprop(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);501if (alt_phy_handle) {502u32 alt_phandle = be32_to_cpup(alt_phy_handle);503504alt_phy = fdt_node_offset_by_phandle(initial_boot_params, alt_phandle);505} else {506alt_phy = -1;507}508509if (phy_addr < 0 || phy < 0) {510/* Delete the PHY things */511fdt_nop_property(initial_boot_params, eth, "phy-handle");512/* This one may fail */513fdt_nop_property(initial_boot_params, eth, "cavium,alt-phy-handle");514if (phy >= 0)515fdt_nop_node(initial_boot_params, phy);516if (alt_phy >= 0)517fdt_nop_node(initial_boot_params, alt_phy);518return;519}520521if (phy_addr >= 256 && alt_phy > 0) {522const struct fdt_property *phy_prop;523struct fdt_property *alt_prop;524fdt32_t phy_handle_name;525526/* Use the alt phy node instead.*/527phy_prop = fdt_get_property(initial_boot_params, eth, "phy-handle", NULL);528phy_handle_name = phy_prop->nameoff;529fdt_nop_node(initial_boot_params, phy);530fdt_nop_property(initial_boot_params, eth, "phy-handle");531alt_prop = fdt_get_property_w(initial_boot_params, eth, "cavium,alt-phy-handle", NULL);532alt_prop->nameoff = phy_handle_name;533phy = alt_phy;534}535536phy_addr &= 0xff;537538if (octeon_has_88e1145()) {539fdt_nop_property(initial_boot_params, phy, "marvell,reg-init");540memset(new_name, 0, sizeof(new_name));541strcpy(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