diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..093688d --- /dev/null +++ b/.clang-format @@ -0,0 +1,807 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# clang-format configuration file. Intended for clang-format >= 11. +# +# For more information, see: +# +# Documentation/dev-tools/clang-format.rst +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# +--- +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: true # Modified for culvert +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true # Modified for culvert +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false + +# Taken from: +# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ tools/ \ +# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \ +# | LC_ALL=C sort -u +ForEachMacros: + - '__ata_qc_for_each' + - '__bio_for_each_bvec' + - '__bio_for_each_segment' + - '__evlist__for_each_entry' + - '__evlist__for_each_entry_continue' + - '__evlist__for_each_entry_from' + - '__evlist__for_each_entry_reverse' + - '__evlist__for_each_entry_safe' + - '__for_each_mem_range' + - '__for_each_mem_range_rev' + - '__for_each_thread' + - '__hlist_for_each_rcu' + - '__map__for_each_symbol_by_name' + - '__pci_bus_for_each_res0' + - '__pci_bus_for_each_res1' + - '__pci_dev_for_each_res0' + - '__pci_dev_for_each_res1' + - '__perf_evlist__for_each_entry' + - '__perf_evlist__for_each_entry_reverse' + - '__perf_evlist__for_each_entry_safe' + - '__rq_for_each_bio' + - '__shost_for_each_device' + - '__sym_for_each' + - '_for_each_counter' + - 'apei_estatus_for_each_section' + - 'ata_for_each_dev' + - 'ata_for_each_link' + - 'ata_qc_for_each' + - 'ata_qc_for_each_raw' + - 'ata_qc_for_each_with_internal' + - 'ax25_for_each' + - 'ax25_uid_for_each' + - 'bio_for_each_bvec' + - 'bio_for_each_bvec_all' + - 'bio_for_each_folio_all' + - 'bio_for_each_integrity_vec' + - 'bio_for_each_segment' + - 'bio_for_each_segment_all' + - 'bio_list_for_each' + - 'bip_for_each_vec' + - 'bond_for_each_slave' + - 'bond_for_each_slave_rcu' + - 'bpf_for_each' + - 'bpf_for_each_reg_in_vstate' + - 'bpf_for_each_reg_in_vstate_mask' + - 'bpf_for_each_spilled_reg' + - 'bpf_object__for_each_map' + - 'bpf_object__for_each_program' + - 'btree_for_each_safe128' + - 'btree_for_each_safe32' + - 'btree_for_each_safe64' + - 'btree_for_each_safel' + - 'card_for_each_dev' + - 'cgroup_taskset_for_each' + - 'cgroup_taskset_for_each_leader' + - 'cpu_aggr_map__for_each_idx' + - 'cpufreq_for_each_efficient_entry_idx' + - 'cpufreq_for_each_entry' + - 'cpufreq_for_each_entry_idx' + - 'cpufreq_for_each_valid_entry' + - 'cpufreq_for_each_valid_entry_idx' + - 'css_for_each_child' + - 'css_for_each_descendant_post' + - 'css_for_each_descendant_pre' + - 'damon_for_each_region' + - 'damon_for_each_region_from' + - 'damon_for_each_region_safe' + - 'damon_for_each_scheme' + - 'damon_for_each_scheme_safe' + - 'damon_for_each_target' + - 'damon_for_each_target_safe' + - 'damos_for_each_core_filter' + - 'damos_for_each_core_filter_safe' + - 'damos_for_each_ops_filter' + - 'damos_for_each_ops_filter_safe' + - 'damos_for_each_quota_goal' + - 'damos_for_each_quota_goal_safe' + - 'data__for_each_file' + - 'data__for_each_file_new' + - 'data__for_each_file_start' + - 'def_for_each_cpu' + - 'device_for_each_child_node' + - 'device_for_each_child_node_scoped' + - 'dma_fence_array_for_each' + - 'dma_fence_chain_for_each' + - 'dma_fence_unwrap_for_each' + - 'dma_resv_for_each_fence' + - 'dma_resv_for_each_fence_unlocked' + - 'do_for_each_ftrace_op' + - 'drm_atomic_crtc_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane_state' + - 'drm_atomic_for_each_plane_damage' + - 'drm_client_for_each_connector_iter' + - 'drm_client_for_each_modeset' + - 'drm_connector_for_each_possible_encoder' + - 'drm_exec_for_each_locked_object' + - 'drm_exec_for_each_locked_object_reverse' + - 'drm_for_each_bridge_in_chain_scoped' + - 'drm_for_each_connector_iter' + - 'drm_for_each_crtc' + - 'drm_for_each_crtc_reverse' + - 'drm_for_each_encoder' + - 'drm_for_each_encoder_mask' + - 'drm_for_each_fb' + - 'drm_for_each_legacy_plane' + - 'drm_for_each_plane' + - 'drm_for_each_plane_mask' + - 'drm_for_each_privobj' + - 'drm_gem_for_each_gpuvm_bo' + - 'drm_gem_for_each_gpuvm_bo_safe' + - 'drm_gpusvm_for_each_range' + - 'drm_gpuva_for_each_op' + - 'drm_gpuva_for_each_op_from_reverse' + - 'drm_gpuva_for_each_op_reverse' + - 'drm_gpuva_for_each_op_safe' + - 'drm_gpuvm_bo_for_each_va' + - 'drm_gpuvm_bo_for_each_va_safe' + - 'drm_gpuvm_for_each_va' + - 'drm_gpuvm_for_each_va_range' + - 'drm_gpuvm_for_each_va_range_safe' + - 'drm_gpuvm_for_each_va_safe' + - 'drm_mm_for_each_hole' + - 'drm_mm_for_each_node' + - 'drm_mm_for_each_node_in_range' + - 'drm_mm_for_each_node_safe' + - 'dsa_switch_for_each_available_port' + - 'dsa_switch_for_each_cpu_port' + - 'dsa_switch_for_each_cpu_port_continue_reverse' + - 'dsa_switch_for_each_port' + - 'dsa_switch_for_each_port_continue_reverse' + - 'dsa_switch_for_each_port_safe' + - 'dsa_switch_for_each_user_port' + - 'dsa_switch_for_each_user_port_continue_reverse' + - 'dsa_tree_for_each_cpu_port' + - 'dsa_tree_for_each_user_port' + - 'dsa_tree_for_each_user_port_continue_reverse' + - 'dso__for_each_symbol' + - 'elf_hash_for_each_possible' + - 'elf_symtab__for_each_symbol' + - 'evlist__for_each_cpu' + - 'evlist__for_each_entry' + - 'evlist__for_each_entry_continue' + - 'evlist__for_each_entry_from' + - 'evlist__for_each_entry_reverse' + - 'evlist__for_each_entry_safe' + - 'flow_action_for_each' + - 'for_each_acpi_consumer_dev' + - 'for_each_acpi_dev_match' + - 'for_each_active_dev_scope' + - 'for_each_active_drhd_unit' + - 'for_each_active_iommu' + - 'for_each_active_irq' + - 'for_each_active_route' + - 'for_each_aggr_pgid' + - 'for_each_alloc_capable_rdt_resource' + - 'for_each_and_bit' + - 'for_each_andnot_bit' + - 'for_each_available_child_of_node' + - 'for_each_available_child_of_node_scoped' + - 'for_each_bench' + - 'for_each_bio' + - 'for_each_board_func_rsrc' + - 'for_each_btf_ext_rec' + - 'for_each_btf_ext_sec' + - 'for_each_bvec' + - 'for_each_capable_rdt_resource' + - 'for_each_card_auxs' + - 'for_each_card_auxs_safe' + - 'for_each_card_components' + - 'for_each_card_dapms' + - 'for_each_card_pre_auxs' + - 'for_each_card_prelinks' + - 'for_each_card_rtds' + - 'for_each_card_rtds_safe' + - 'for_each_card_widgets' + - 'for_each_card_widgets_safe' + - 'for_each_cgroup_storage_type' + - 'for_each_child_of_node' + - 'for_each_child_of_node_scoped' + - 'for_each_child_of_node_with_prefix' + - 'for_each_clear_bit' + - 'for_each_clear_bit_from' + - 'for_each_clear_bitrange' + - 'for_each_clear_bitrange_from' + - 'for_each_cmd' + - 'for_each_cmsghdr' + - 'for_each_collection' + - 'for_each_comp_order' + - 'for_each_compatible_node' + - 'for_each_compatible_node_scoped' + - 'for_each_component_dais' + - 'for_each_component_dais_safe' + - 'for_each_conduit' + - 'for_each_console' + - 'for_each_console_srcu' + - 'for_each_cpu' + - 'for_each_cpu_and' + - 'for_each_cpu_andnot' + - 'for_each_cpu_from' + - 'for_each_cpu_or' + - 'for_each_cpu_wrap' + - 'for_each_dapm_widgets' + - 'for_each_dedup_cand' + - 'for_each_dev_addr' + - 'for_each_dev_scope' + - 'for_each_dma_cap_mask' + - 'for_each_dpcm_be' + - 'for_each_dpcm_be_rollback' + - 'for_each_dpcm_be_safe' + - 'for_each_dpcm_fe' + - 'for_each_drhd_unit' + - 'for_each_dss_dev' + - 'for_each_efi_memory_desc' + - 'for_each_efi_memory_desc_in_map' + - 'for_each_element' + - 'for_each_element_extid' + - 'for_each_element_id' + - 'for_each_enabled_cpu' + - 'for_each_endpoint_of_node' + - 'for_each_event' + - 'for_each_event_tps' + - 'for_each_evictable_lru' + - 'for_each_fib6_node_rt_rcu' + - 'for_each_fib6_walker_rt' + - 'for_each_file_lock' + - 'for_each_free_mem_range' + - 'for_each_free_mem_range_reverse' + - 'for_each_func_rsrc' + - 'for_each_gpiochip_node' + - 'for_each_group_evsel' + - 'for_each_group_evsel_head' + - 'for_each_group_member' + - 'for_each_group_member_head' + - 'for_each_hstate' + - 'for_each_hwgpio' + - 'for_each_hwgpio_in_range' + - 'for_each_if' + - 'for_each_inject_fn' + - 'for_each_insn' + - 'for_each_insn_op_loc' + - 'for_each_insn_prefix' + - 'for_each_intid' + - 'for_each_iommu' + - 'for_each_ip_tunnel_rcu' + - 'for_each_irq_desc' + - 'for_each_irq_nr' + - 'for_each_lang' + - 'for_each_link_ch_maps' + - 'for_each_link_codecs' + - 'for_each_link_cpus' + - 'for_each_link_platforms' + - 'for_each_lru' + - 'for_each_matching_node' + - 'for_each_matching_node_and_match' + - 'for_each_media_entity_data_link' + - 'for_each_mem_pfn_range' + - 'for_each_mem_range' + - 'for_each_mem_range_rev' + - 'for_each_mem_region' + - 'for_each_member' + - 'for_each_memory' + - 'for_each_migratetype_order' + - 'for_each_missing_reg' + - 'for_each_mle_subelement' + - 'for_each_mod_mem_type' + - 'for_each_mon_capable_rdt_resource' + - 'for_each_mp_bvec' + - 'for_each_net' + - 'for_each_net_continue_reverse' + - 'for_each_net_rcu' + - 'for_each_netdev' + - 'for_each_netdev_continue' + - 'for_each_netdev_continue_rcu' + - 'for_each_netdev_continue_reverse' + - 'for_each_netdev_dump' + - 'for_each_netdev_feature' + - 'for_each_netdev_in_bond_rcu' + - 'for_each_netdev_rcu' + - 'for_each_netdev_reverse' + - 'for_each_netdev_safe' + - 'for_each_new_connector_in_state' + - 'for_each_new_crtc_in_state' + - 'for_each_new_mst_mgr_in_state' + - 'for_each_new_plane_in_state' + - 'for_each_new_plane_in_state_reverse' + - 'for_each_new_private_obj_in_state' + - 'for_each_new_reg' + - 'for_each_nhlt_endpoint' + - 'for_each_nhlt_endpoint_fmtcfg' + - 'for_each_nhlt_fmtcfg' + - 'for_each_node' + - 'for_each_node_by_name' + - 'for_each_node_by_type' + - 'for_each_node_mask' + - 'for_each_node_numadist' + - 'for_each_node_state' + - 'for_each_node_with_cpus' + - 'for_each_node_with_property' + - 'for_each_nonreserved_multicast_dest_pgid' + - 'for_each_numa_hop_mask' + - 'for_each_of_allnodes' + - 'for_each_of_allnodes_from' + - 'for_each_of_cpu_node' + - 'for_each_of_graph_port' + - 'for_each_of_graph_port_endpoint' + - 'for_each_of_pci_range' + - 'for_each_old_connector_in_state' + - 'for_each_old_crtc_in_state' + - 'for_each_old_mst_mgr_in_state' + - 'for_each_old_plane_in_state' + - 'for_each_old_private_obj_in_state' + - 'for_each_oldnew_connector_in_state' + - 'for_each_oldnew_crtc_in_state' + - 'for_each_oldnew_mst_mgr_in_state' + - 'for_each_oldnew_plane_in_state' + - 'for_each_oldnew_plane_in_state_reverse' + - 'for_each_oldnew_private_obj_in_state' + - 'for_each_online_cpu' + - 'for_each_online_cpu_wrap' + - 'for_each_online_node' + - 'for_each_online_pgdat' + - 'for_each_or_bit' + - 'for_each_page_ext' + - 'for_each_path' + - 'for_each_pci_bridge' + - 'for_each_pci_dev' + - 'for_each_pcm_streams' + - 'for_each_physmem_range' + - 'for_each_populated_zone' + - 'for_each_possible_cpu' + - 'for_each_possible_cpu_wrap' + - 'for_each_present_blessed_reg' + - 'for_each_present_cpu' + - 'for_each_present_section_nr' + - 'for_each_prime_number' + - 'for_each_prime_number_from' + - 'for_each_probe_cache_entry' + - 'for_each_process' + - 'for_each_process_thread' + - 'for_each_prop_codec_conf' + - 'for_each_prop_dai_codec' + - 'for_each_prop_dai_cpu' + - 'for_each_prop_dlc_codecs' + - 'for_each_prop_dlc_cpus' + - 'for_each_prop_dlc_platforms' + - 'for_each_property_of_node' + - 'for_each_pt_level_entry' + - 'for_each_rdt_resource' + - 'for_each_reg' + - 'for_each_reg_filtered' + - 'for_each_reloc' + - 'for_each_reloc_from' + - 'for_each_requested_gpio' + - 'for_each_requested_gpio_in_range' + - 'for_each_reserved_child_of_node' + - 'for_each_reserved_mem_range' + - 'for_each_reserved_mem_region' + - 'for_each_rtd_ch_maps' + - 'for_each_rtd_codec_dais' + - 'for_each_rtd_components' + - 'for_each_rtd_cpu_dais' + - 'for_each_rtd_dais' + - 'for_each_rtd_dais_reverse' + - 'for_each_sband_iftype_data' + - 'for_each_script' + - 'for_each_sec' + - 'for_each_set_bit' + - 'for_each_set_bit_from' + - 'for_each_set_bit_wrap' + - 'for_each_set_bitrange' + - 'for_each_set_bitrange_from' + - 'for_each_set_clump8' + - 'for_each_sg' + - 'for_each_sg_dma_page' + - 'for_each_sg_page' + - 'for_each_sgtable_dma_page' + - 'for_each_sgtable_dma_sg' + - 'for_each_sgtable_page' + - 'for_each_sgtable_sg' + - 'for_each_sibling_event' + - 'for_each_sta_active_link' + - 'for_each_subelement' + - 'for_each_subelement_extid' + - 'for_each_subelement_id' + - 'for_each_sublist' + - 'for_each_subsystem' + - 'for_each_suite' + - 'for_each_supported_activate_fn' + - 'for_each_supported_inject_fn' + - 'for_each_sym' + - 'for_each_thread' + - 'for_each_token' + - 'for_each_unicast_dest_pgid' + - 'for_each_valid_link' + - 'for_each_vif_active_link' + - 'for_each_vma' + - 'for_each_vma_range' + - 'for_each_vsi' + - 'for_each_wakeup_source' + - 'for_each_zone' + - 'for_each_zone_zonelist' + - 'for_each_zone_zonelist_nodemask' + - 'func_for_each_insn' + - 'fwnode_for_each_available_child_node' + - 'fwnode_for_each_child_node' + - 'fwnode_for_each_parent_node' + - 'fwnode_graph_for_each_endpoint' + - 'gadget_for_each_ep' + - 'genradix_for_each' + - 'genradix_for_each_from' + - 'genradix_for_each_reverse' + - 'hash_for_each' + - 'hash_for_each_possible' + - 'hash_for_each_possible_rcu' + - 'hash_for_each_possible_rcu_notrace' + - 'hash_for_each_possible_safe' + - 'hash_for_each_rcu' + - 'hash_for_each_safe' + - 'hashmap__for_each_entry' + - 'hashmap__for_each_entry_safe' + - 'hashmap__for_each_key_entry' + - 'hashmap__for_each_key_entry_safe' + - 'hctx_for_each_ctx' + - 'hists__for_each_format' + - 'hists__for_each_sort_list' + - 'hlist_bl_for_each_entry' + - 'hlist_bl_for_each_entry_rcu' + - 'hlist_bl_for_each_entry_safe' + - 'hlist_for_each' + - 'hlist_for_each_entry' + - 'hlist_for_each_entry_continue' + - 'hlist_for_each_entry_continue_rcu' + - 'hlist_for_each_entry_continue_rcu_bh' + - 'hlist_for_each_entry_from' + - 'hlist_for_each_entry_from_rcu' + - 'hlist_for_each_entry_rcu' + - 'hlist_for_each_entry_rcu_bh' + - 'hlist_for_each_entry_rcu_notrace' + - 'hlist_for_each_entry_safe' + - 'hlist_for_each_entry_srcu' + - 'hlist_for_each_safe' + - 'hlist_nulls_for_each_entry' + - 'hlist_nulls_for_each_entry_from' + - 'hlist_nulls_for_each_entry_rcu' + - 'hlist_nulls_for_each_entry_safe' + - 'i3c_bus_for_each_i2cdev' + - 'i3c_bus_for_each_i3cdev' + - 'idr_for_each_entry' + - 'idr_for_each_entry_continue' + - 'idr_for_each_entry_continue_ul' + - 'idr_for_each_entry_ul' + - 'iio_for_each_active_channel' + - 'in_dev_for_each_ifa_rcu' + - 'in_dev_for_each_ifa_rtnl' + - 'in_dev_for_each_ifa_rtnl_net' + - 'inet_bind_bucket_for_each' + - 'interval_tree_for_each_span' + - 'intlist__for_each_entry' + - 'intlist__for_each_entry_safe' + - 'kcore_copy__for_each_phdr' + - 'key_for_each' + - 'key_for_each_safe' + - 'klp_for_each_func' + - 'klp_for_each_func_safe' + - 'klp_for_each_func_static' + - 'klp_for_each_object' + - 'klp_for_each_object_safe' + - 'klp_for_each_object_static' + - 'kunit_suite_for_each_test_case' + - 'kvm_for_each_memslot' + - 'kvm_for_each_memslot_in_gfn_range' + - 'kvm_for_each_vcpu' + - 'libbpf_nla_for_each_attr' + - 'list_for_each' + - 'list_for_each_codec' + - 'list_for_each_codec_safe' + - 'list_for_each_continue' + - 'list_for_each_entry' + - 'list_for_each_entry_continue' + - 'list_for_each_entry_continue_rcu' + - 'list_for_each_entry_continue_reverse' + - 'list_for_each_entry_from' + - 'list_for_each_entry_from_rcu' + - 'list_for_each_entry_from_reverse' + - 'list_for_each_entry_lockless' + - 'list_for_each_entry_rcu' + - 'list_for_each_entry_reverse' + - 'list_for_each_entry_safe' + - 'list_for_each_entry_safe_continue' + - 'list_for_each_entry_safe_from' + - 'list_for_each_entry_safe_reverse' + - 'list_for_each_entry_srcu' + - 'list_for_each_from' + - 'list_for_each_prev' + - 'list_for_each_prev_safe' + - 'list_for_each_rcu' + - 'list_for_each_safe' + - 'llist_for_each' + - 'llist_for_each_entry' + - 'llist_for_each_entry_safe' + - 'llist_for_each_safe' + - 'lwq_for_each_safe' + - 'map__for_each_symbol' + - 'map__for_each_symbol_by_name' + - 'mas_for_each' + - 'mas_for_each_rev' + - 'mci_for_each_dimm' + - 'media_device_for_each_entity' + - 'media_device_for_each_intf' + - 'media_device_for_each_link' + - 'media_device_for_each_pad' + - 'media_entity_for_each_pad' + - 'media_pipeline_for_each_entity' + - 'media_pipeline_for_each_pad' + - 'mlx5_lag_for_each_peer_mdev' + - 'mptcp_for_each_subflow' + - 'msi_domain_for_each_desc' + - 'msi_for_each_desc' + - 'mt_for_each' + - 'nanddev_io_for_each_block' + - 'nanddev_io_for_each_page' + - 'neigh_for_each_in_bucket' + - 'neigh_for_each_in_bucket_rcu' + - 'neigh_for_each_in_bucket_safe' + - 'netdev_for_each_lower_dev' + - 'netdev_for_each_lower_private' + - 'netdev_for_each_lower_private_rcu' + - 'netdev_for_each_mc_addr' + - 'netdev_for_each_synced_mc_addr' + - 'netdev_for_each_synced_uc_addr' + - 'netdev_for_each_uc_addr' + - 'netdev_for_each_upper_dev_rcu' + - 'netdev_hw_addr_list_for_each' + - 'nft_rule_for_each_expr' + - 'nla_for_each_attr' + - 'nla_for_each_attr_type' + - 'nla_for_each_nested' + - 'nla_for_each_nested_type' + - 'nlmsg_for_each_attr' + - 'nlmsg_for_each_msg' + - 'nr_neigh_for_each' + - 'nr_neigh_for_each_safe' + - 'nr_node_for_each' + - 'nr_node_for_each_safe' + - 'of_for_each_phandle' + - 'of_property_for_each_string' + - 'of_property_for_each_u32' + - 'pci_bus_for_each_resource' + - 'pci_dev_for_each_resource' + - 'pcl_for_each_chunk' + - 'pcl_for_each_segment' + - 'pcm_for_each_format' + - 'perf_config_items__for_each_entry' + - 'perf_config_sections__for_each_entry' + - 'perf_config_set__for_each_entry' + - 'perf_cpu_map__for_each_cpu' + - 'perf_cpu_map__for_each_cpu_skip_any' + - 'perf_cpu_map__for_each_idx' + - 'perf_evlist__for_each_entry' + - 'perf_evlist__for_each_entry_reverse' + - 'perf_evlist__for_each_entry_safe' + - 'perf_evlist__for_each_evsel' + - 'perf_evlist__for_each_mmap' + - 'perf_evsel_for_each_per_thread_period_safe' + - 'perf_hpp_list__for_each_format' + - 'perf_hpp_list__for_each_format_safe' + - 'perf_hpp_list__for_each_sort_list' + - 'perf_hpp_list__for_each_sort_list_safe' + - 'plist_for_each' + - 'plist_for_each_continue' + - 'plist_for_each_entry' + - 'plist_for_each_entry_continue' + - 'plist_for_each_entry_safe' + - 'plist_for_each_safe' + - 'pnp_for_each_card' + - 'pnp_for_each_dev' + - 'protocol_for_each_card' + - 'protocol_for_each_dev' + - 'queue_for_each_hw_ctx' + - 'radix_tree_for_each_slot' + - 'radix_tree_for_each_tagged' + - 'rb_for_each' + - 'rbtree_postorder_for_each_entry_safe' + - 'rdma_for_each_block' + - 'rdma_for_each_port' + - 'rdma_umem_for_each_dma_block' + - 'resource_list_for_each_entry' + - 'resource_list_for_each_entry_safe' + - 'rhl_for_each_entry_rcu' + - 'rhl_for_each_rcu' + - 'rht_for_each' + - 'rht_for_each_entry' + - 'rht_for_each_entry_from' + - 'rht_for_each_entry_rcu' + - 'rht_for_each_entry_rcu_from' + - 'rht_for_each_entry_safe' + - 'rht_for_each_from' + - 'rht_for_each_rcu' + - 'rht_for_each_rcu_from' + - 'rq_for_each_bvec' + - 'rq_for_each_segment' + - 'rq_list_for_each' + - 'rq_list_for_each_safe' + - 'sample_read_group__for_each' + - 'scsi_for_each_prot_sg' + - 'scsi_for_each_sg' + - 'sctp_for_each_hentry' + - 'sctp_skb_for_each' + - 'sec_for_each_insn' + - 'sec_for_each_insn_continue' + - 'sec_for_each_insn_from' + - 'sec_for_each_sym' + - 'shdma_for_each_chan' + - 'shost_for_each_device' + - 'sk_for_each' + - 'sk_for_each_bound' + - 'sk_for_each_bound_safe' + - 'sk_for_each_entry_offset_rcu' + - 'sk_for_each_from' + - 'sk_for_each_rcu' + - 'sk_for_each_safe' + - 'sk_nulls_for_each' + - 'sk_nulls_for_each_from' + - 'sk_nulls_for_each_rcu' + - 'snd_array_for_each' + - 'snd_pcm_group_for_each_entry' + - 'snd_soc_dapm_widget_for_each_path' + - 'snd_soc_dapm_widget_for_each_path_safe' + - 'snd_soc_dapm_widget_for_each_sink_path' + - 'snd_soc_dapm_widget_for_each_source_path' + - 'sparsebit_for_each_set_range' + - 'strlist__for_each_entry' + - 'strlist__for_each_entry_safe' + - 'sym_for_each_insn' + - 'sym_for_each_insn_continue_reverse' + - 'symbols__for_each_entry' + - 'tb_property_for_each' + - 'tcf_act_for_each_action' + - 'tcf_exts_for_each_action' + - 'test_suite__for_each_test_case' + - 'tool_pmu__for_each_event' + - 'ttm_bo_lru_for_each_reserved_guarded' + - 'ttm_resource_manager_for_each_res' + - 'udp_lrpa_for_each_entry_rcu' + - 'udp_portaddr_for_each_entry' + - 'udp_portaddr_for_each_entry_rcu' + - 'usb_hub_for_each_child' + - 'v4l2_device_for_each_subdev' + - 'v4l2_m2m_for_each_dst_buf' + - 'v4l2_m2m_for_each_dst_buf_safe' + - 'v4l2_m2m_for_each_src_buf' + - 'v4l2_m2m_for_each_src_buf_safe' + - 'virtio_device_for_each_vq' + - 'vkms_config_for_each_connector' + - 'vkms_config_for_each_crtc' + - 'vkms_config_for_each_encoder' + - 'vkms_config_for_each_plane' + - 'vkms_config_connector_for_each_possible_encoder' + - 'vkms_config_encoder_for_each_possible_crtc' + - 'vkms_config_plane_for_each_possible_crtc' + - 'while_for_each_ftrace_op' + - 'workloads__for_each' + - 'xa_for_each' + - 'xa_for_each_marked' + - 'xa_for_each_range' + - 'xa_for_each_start' + - 'xas_for_each' + - 'xas_for_each_conflict' + - 'xas_for_each_marked' + - 'xbc_array_for_each_value' + - 'xbc_for_each_key_value' + - 'xbc_node_for_each_array_value' + - 'xbc_node_for_each_child' + - 'xbc_node_for_each_key_value' + - 'xbc_node_for_each_subkey' + - 'ynl_attr_for_each' + - 'ynl_attr_for_each_nested' + - 'ynl_attr_for_each_payload' + - 'zorro_for_each_dev' + - 'zpci_bus_for_each' + +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentGotoLabels: false +IndentPPDirectives: None +IndentWidth: 8 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 8 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true + +# Taken from git's rules +PenaltyBreakAssignment: 10 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatementsExceptForEachMacros +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp03 +TabWidth: 8 +UseTab: Always +... diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 0000000..5301ed1 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1 @@ +src/ccan/* diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 37c63b5..0db95f6 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -2,6 +2,27 @@ name: CI on: [push, pull_request] jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + - name: Install dependencies + run: sudo apt-get update -qq && + sudo apt-get install -qq + ninja-build python3-pip + device-tree-compiler + - name: Install meson + run: pip3 install --user meson + - name: Install clang + uses: KyleMayes/install-llvm-action@v2.0.9 + with: + version: "21" + - name: Check clang-format + run: meson setup build-lint && ninja -C build-lint clang-format-check + - name: Check meson format + run: meson format --check-only --recursive + build: name: Build runs-on: ubuntu-latest diff --git a/meson.build b/meson.build index ae02fdc..c9f2863 100644 --- a/meson.build +++ b/meson.build @@ -1,21 +1,25 @@ -project('culvert', 'c', - default_options: [ - 'buildtype=debugoptimized', - 'default_library=static', - 'warning_level=2', - 'c_std=gnu99', - 'b_lto=true', - 'b_ndebug=true', - ], - meson_version: '>=0.63.0', - version: 'v0.4.0') +project( + 'culvert', + 'c', + default_options: [ + 'buildtype=debugoptimized', + 'default_library=static', + 'warning_level=2', + 'c_std=gnu99', + 'b_lto=true', + 'b_ndebug=true', + ], + meson_version: '>=0.63.0', + version: 'v0.4.0', +) add_global_link_arguments('-z', 'noexecstack', language: 'c') -libfdt_dep = dependency('libfdt', - default_options: [ 'warning_level=1', - 'python=disabled' ], - fallback: [ 'dtc', 'libfdt_dep' ], - required: true) +libfdt_dep = dependency( + 'libfdt', + default_options: ['warning_level=1', 'python=disabled'], + fallback: ['dtc', 'libfdt_dep'], + required: true, +) subdir('src') diff --git a/src/ahb.c b/src/ahb.c index e0c2530..35d5f68 100644 --- a/src/ahb.c +++ b/src/ahb.c @@ -17,84 +17,98 @@ ssize_t ahb_siphon_out(struct ahb *ctx, uint32_t phys, ssize_t len, int outfd) { - ssize_t ingress, egress; - void *chunk, *cursor; - int rc = 0; - - if (!len) - return 0; - - chunk = malloc(AHB_CHUNK); - if (!chunk) - return -errno; - - do { - ingress = (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : len; - - ingress = ahb_read(ctx, phys, chunk, ingress); - if (ingress < 0) { rc = -EIO; goto done; } - - phys += ingress; - if (len > 0) { - len -= ingress; - } - - cursor = chunk; - while (ingress) { - egress = write(outfd, cursor, ingress); - if (egress == -1) { rc = -errno; goto done; } - - cursor += egress; - ingress -= egress; - } - - fprintf(stderr, "."); - } while (!!len); + ssize_t ingress, egress; + void *chunk, *cursor; + int rc = 0; + + if (!len) + return 0; + + chunk = malloc(AHB_CHUNK); + if (!chunk) + return -errno; + + do { + ingress = (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : len; + + ingress = ahb_read(ctx, phys, chunk, ingress); + if (ingress < 0) { + rc = -EIO; + goto done; + } + + phys += ingress; + if (len > 0) { + len -= ingress; + } + + cursor = chunk; + while (ingress) { + egress = write(outfd, cursor, ingress); + if (egress == -1) { + rc = -errno; + goto done; + } + + cursor += egress; + ingress -= egress; + } + + fprintf(stderr, "."); + } while (!!len); done: - fprintf(stderr, "\n"); - free(chunk); + fprintf(stderr, "\n"); + free(chunk); - return rc; + return rc; } ssize_t ahb_siphon_in(struct ahb *ctx, uint32_t phys, ssize_t len, int infd) { - ssize_t ingress, egress; - void *chunk; - int rc = 0; - - chunk = malloc(AHB_CHUNK); - if (!chunk) - return -1; - - while ((ingress = read(infd, chunk, (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : len))) { - if (ingress < 0) { rc = -errno; goto done; } - - egress = ahb_write(ctx, phys, chunk, ingress); - if (egress < 0) { rc = -EIO; goto done; } - - phys += ingress; - if (len > 0) { - len -= ingress; - } - - fprintf(stderr, "."); - } + ssize_t ingress, egress; + void *chunk; + int rc = 0; + + chunk = malloc(AHB_CHUNK); + if (!chunk) + return -1; + + while ((ingress = read(infd, chunk, + (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : + len))) { + if (ingress < 0) { + rc = -errno; + goto done; + } + + egress = ahb_write(ctx, phys, chunk, ingress); + if (egress < 0) { + rc = -EIO; + goto done; + } + + phys += ingress; + if (len > 0) { + len -= ingress; + } + + fprintf(stderr, "."); + } done: - fprintf(stderr, "\n"); - free(chunk); + fprintf(stderr, "\n"); + free(chunk); - return rc; + return rc; } int ahb_release_bridge(struct ahb *ctx) { - return ctx->drv->release ? ctx->drv->release(ctx) : 0; + return ctx->drv->release ? ctx->drv->release(ctx) : 0; } int ahb_reinit_bridge(struct ahb *ctx) { - return ctx->drv->reinit ? ctx->drv->reinit(ctx) : 0; + return ctx->drv->reinit ? ctx->drv->reinit(ctx) : 0; } diff --git a/src/ahb.h b/src/ahb.h index 4eb9901..aeb1f3c 100644 --- a/src/ahb.h +++ b/src/ahb.h @@ -14,64 +14,69 @@ #include struct ahb_range { - const char *name; - uint32_t start; - uint64_t len; - bool rw; + const char *name; + uint32_t start; + uint64_t len; + bool rw; }; struct ahb; struct ahb_ops { - ssize_t (*read)(struct ahb *ctx, uint32_t phys, void *buf, size_t len); - ssize_t (*write)(struct ahb *ctx, uint32_t phys, const void *buf, size_t len); - int (*readl)(struct ahb *ctx, uint32_t phys, uint32_t *val); - int (*writel)(struct ahb *ctx, uint32_t phys, uint32_t val); + ssize_t (*read)(struct ahb *ctx, uint32_t phys, void *buf, size_t len); + ssize_t (*write)(struct ahb *ctx, uint32_t phys, const void *buf, + size_t len); + int (*readl)(struct ahb *ctx, uint32_t phys, uint32_t *val); + int (*writel)(struct ahb *ctx, uint32_t phys, uint32_t val); }; struct ahb { - const struct bridge_driver *drv; - const struct ahb_ops *ops; + const struct bridge_driver *drv; + const struct ahb_ops *ops; }; -static inline void ahb_init_ops(struct ahb *ctx, const struct bridge_driver *drv, - const struct ahb_ops *ops) +static inline void ahb_init_ops(struct ahb *ctx, + const struct bridge_driver *drv, + const struct ahb_ops *ops) { - ctx->drv = drv; - ctx->ops = ops; + ctx->drv = drv; + ctx->ops = ops; } -static inline ssize_t ahb_read(struct ahb *ctx, uint32_t phys, void *buf, size_t len) +static inline ssize_t ahb_read(struct ahb *ctx, uint32_t phys, void *buf, + size_t len) { - return ctx->ops->read(ctx, phys, buf, len); + return ctx->ops->read(ctx, phys, buf, len); } -static inline ssize_t ahb_write(struct ahb *ctx, uint32_t phys, const void *buf, size_t len) +static inline ssize_t ahb_write(struct ahb *ctx, uint32_t phys, const void *buf, + size_t len) { - return ctx->ops->write(ctx, phys, buf, len); + return ctx->ops->write(ctx, phys, buf, len); } static inline int ahb_readl(struct ahb *ctx, uint32_t phys, uint32_t *val) { - int rc = ctx->ops->readl(ctx, phys, val); - if (!rc) { - logt("%s: 0x%08"PRIx32": 0x%08"PRIx32"\n", __func__, phys, *val); - } + int rc = ctx->ops->readl(ctx, phys, val); + if (!rc) { + logt("%s: 0x%08" PRIx32 ": 0x%08" PRIx32 "\n", __func__, phys, + *val); + } - return rc; + return rc; } static inline int ahb_writel(struct ahb *ctx, uint32_t phys, uint32_t val) { - int rc = ctx->ops->writel(ctx, phys, val); - if (!rc) { - logt("%s: 0x%08"PRIx32": 0x%08"PRIx32"\n", __func__, phys, val); - } + int rc = ctx->ops->writel(ctx, phys, val); + if (!rc) { + logt("%s: 0x%08" PRIx32 ": 0x%08" PRIx32 "\n", __func__, phys, + val); + } - return rc; + return rc; } - ssize_t ahb_siphon_out(struct ahb *ctx, uint32_t phys, ssize_t len, int outfd); ssize_t ahb_siphon_in(struct ahb *ctx, uint32_t phys, ssize_t len, int infd); diff --git a/src/arch/ppc64/lpc.c b/src/arch/ppc64/lpc.c index ba4e8e0..1936a8f 100644 --- a/src/arch/ppc64/lpc.c +++ b/src/arch/ppc64/lpc.c @@ -16,118 +16,118 @@ int lpc_init(struct lpc *ctx, const char *space) { - char pathbuf[PATH_MAX]; - int rc; + char pathbuf[PATH_MAX]; + int rc; - assert(ctx); - assert(space); + assert(ctx); + assert(space); - rc = snprintf(pathbuf, sizeof(pathbuf), "%s/%s", SYSFS_PREFIX, space); - if (rc < 0) - return rc; + rc = snprintf(pathbuf, sizeof(pathbuf), "%s/%s", SYSFS_PREFIX, space); + if (rc < 0) + return rc; - ctx->fd = open(pathbuf, O_RDWR); - if (ctx->fd == -1) - return -errno; + ctx->fd = open(pathbuf, O_RDWR); + if (ctx->fd == -1) + return -errno; - return 0; + return 0; } int lpc_destroy(struct lpc *ctx) { - int rc; + int rc; - assert(ctx); + assert(ctx); - rc = close(ctx->fd); - if (rc == -1) - return -errno; + rc = close(ctx->fd); + if (rc == -1) + return -errno; - return 0; + return 0; } int lpc_read(struct lpc *ctx, size_t addr, void *val, size_t size) { - off_t seek_rc; - ssize_t rc; + off_t seek_rc; + ssize_t rc; - seek_rc = lseek(ctx->fd, addr, SEEK_SET); - if (seek_rc == (off_t) -1) - return -errno; + seek_rc = lseek(ctx->fd, addr, SEEK_SET); + if (seek_rc == (off_t)-1) + return -errno; - rc = read(ctx->fd, val, size); - if (rc == -1) - return -errno; + rc = read(ctx->fd, val, size); + if (rc == -1) + return -errno; - return rc; + return rc; } int lpc_write(struct lpc *ctx, size_t addr, const void *val, size_t size) { - off_t seek_rc; - ssize_t rc; + off_t seek_rc; + ssize_t rc; - seek_rc = lseek(ctx->fd, addr, SEEK_SET); - if (seek_rc == (off_t) -1) - return -errno; + seek_rc = lseek(ctx->fd, addr, SEEK_SET); + if (seek_rc == (off_t)-1) + return -errno; - rc = write(ctx->fd, val, size); - if (rc == -1) - return -errno; + rc = write(ctx->fd, val, size); + if (rc == -1) + return -errno; - return rc; + return rc; } int lpc_readb(struct lpc *ctx, size_t addr, uint8_t *val) { - int rc; + int rc; - rc = lpc_read(ctx, addr, val, sizeof(*val)); + rc = lpc_read(ctx, addr, val, sizeof(*val)); - return rc < 0 ? rc : 0; + return rc < 0 ? rc : 0; } int lpc_writeb(struct lpc *ctx, size_t addr, uint8_t val) { - int rc; + int rc; - rc = lpc_write(ctx, addr, &val, sizeof(val)); + rc = lpc_write(ctx, addr, &val, sizeof(val)); - return rc < 0 ? rc : 0; + return rc < 0 ? rc : 0; } int lpc_readw(struct lpc *ctx, size_t addr, uint16_t *val) { - int rc; + int rc; - rc = lpc_read(ctx, addr, (unsigned char *) val, sizeof(*val)); + rc = lpc_read(ctx, addr, (unsigned char *)val, sizeof(*val)); - return rc < 0 ? rc : 0; + return rc < 0 ? rc : 0; } int lpc_writew(struct lpc *ctx, size_t addr, uint16_t val) { - int rc; + int rc; - rc = lpc_write(ctx, addr, (const unsigned char *)&val, sizeof(val)); + rc = lpc_write(ctx, addr, (const unsigned char *)&val, sizeof(val)); - return rc < 0 ? rc : 0; + return rc < 0 ? rc : 0; } int lpc_readl(struct lpc *ctx, size_t addr, uint32_t *val) { - int rc; + int rc; - rc = lpc_read(ctx, addr, (unsigned char *)val, sizeof(*val)); + rc = lpc_read(ctx, addr, (unsigned char *)val, sizeof(*val)); - return rc < 0 ? rc : 0; + return rc < 0 ? rc : 0; } int lpc_writel(struct lpc *ctx, size_t addr, uint32_t val) { - int rc; + int rc; - rc = lpc_write(ctx, addr, (const unsigned char *)&val, sizeof(val)); + rc = lpc_write(ctx, addr, (const unsigned char *)&val, sizeof(val)); - return rc < 0 ? rc : 0; + return rc < 0 ? rc : 0; } diff --git a/src/arch/x86_64/lpc.c b/src/arch/x86_64/lpc.c index 5cfa05e..259377d 100644 --- a/src/arch/x86_64/lpc.c +++ b/src/arch/x86_64/lpc.c @@ -10,126 +10,130 @@ #include #if !defined(__GLIBC__) -static __inline unsigned char -inb_p (unsigned short int __port) +static __inline unsigned char inb_p(unsigned short int __port) { - unsigned char _v; + unsigned char _v; - __asm__ __volatile__ ("inb %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port)); - return _v; + __asm__ __volatile__("inb %w1,%0\noutb %%al,$0x80" + : "=a"(_v) + : "Nd"(__port)); + return _v; } -static __inline unsigned short int -inw_p (unsigned short int __port) +static __inline unsigned short int inw_p(unsigned short int __port) { - unsigned short int _v; + unsigned short int _v; - __asm__ __volatile__ ("inw %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port)); - return _v; + __asm__ __volatile__("inw %w1,%0\noutb %%al,$0x80" + : "=a"(_v) + : "Nd"(__port)); + return _v; } -static __inline unsigned int -inl_p (unsigned short int __port) +static __inline unsigned int inl_p(unsigned short int __port) { - unsigned int _v; - __asm__ __volatile__ ("inl %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port)); - return _v; + unsigned int _v; + __asm__ __volatile__("inl %w1,%0\noutb %%al,$0x80" + : "=a"(_v) + : "Nd"(__port)); + return _v; } -static __inline void -outb_p (unsigned char __value, unsigned short int __port) +static __inline void outb_p(unsigned char __value, unsigned short int __port) { - __asm__ __volatile__ ("outb %b0,%w1\noutb %%al,$0x80": :"a" (__value), - "Nd" (__port)); + __asm__ __volatile__("outb %b0,%w1\noutb %%al,$0x80" + : + : "a"(__value), "Nd"(__port)); } -static __inline void -outw_p (unsigned short int __value, unsigned short int __port) +static __inline void outw_p(unsigned short int __value, + unsigned short int __port) { - __asm__ __volatile__ ("outw %w0,%w1\noutb %%al,$0x80": :"a" (__value), - "Nd" (__port)); + __asm__ __volatile__("outw %w0,%w1\noutb %%al,$0x80" + : + : "a"(__value), "Nd"(__port)); } -static __inline void -outl_p (unsigned int __value, unsigned short int __port) +static __inline void outl_p(unsigned int __value, unsigned short int __port) { - __asm__ __volatile__ ("outl %0,%w1\noutb %%al,$0x80": :"a" (__value), - "Nd" (__port)); + __asm__ __volatile__("outl %0,%w1\noutb %%al,$0x80" + : + : "a"(__value), "Nd"(__port)); } #endif int lpc_init(struct lpc *ctx __unused, const char *space) { - int rc; + int rc; - if (strcmp(space, "io")) - return -ENOTSUP; + if (strcmp(space, "io")) + return -ENOTSUP; - /* YOLO */ - rc = iopl(3); - if (rc < 0) { - perror("iopl"); - return rc; - } + /* YOLO */ + rc = iopl(3); + if (rc < 0) { + perror("iopl"); + return rc; + } - return 0; + return 0; } int lpc_destroy(struct lpc *ctx __unused) { - return 0; + return 0; } int lpc_readb(struct lpc *ctx __unused, size_t addr, uint8_t *val) { - *val = inb_p(addr); + *val = inb_p(addr); - return 0; + return 0; } int lpc_writeb(struct lpc *ctx __unused, size_t addr, uint8_t val) { - outb_p(val, addr); + outb_p(val, addr); - return 0; + return 0; } int lpc_readw(struct lpc *ctx __unused, size_t addr, uint16_t *val) { - *val = inw_p(addr); + *val = inw_p(addr); - return 0; + return 0; } int lpc_writew(struct lpc *ctx __unused, size_t addr, uint16_t val) { - outw_p(val, addr); + outw_p(val, addr); - return 0; + return 0; } int lpc_readl(struct lpc *ctx __unused, size_t addr, uint32_t *val) { - *val = inl_p(addr); + *val = inl_p(addr); - return 0; + return 0; } int lpc_writel(struct lpc *ctx __unused, size_t addr, uint32_t val) { - outl_p(val, addr); + outl_p(val, addr); - return 0; + return 0; } int lpc_read(struct lpc *ctx __unused, size_t addr __unused, void *val __unused, - size_t size __unused) + size_t size __unused) { - return -ENOTSUP; + return -ENOTSUP; } int lpc_write(struct lpc *ctx __unused, size_t addr __unused, - const void *val __unused, size_t size __unused) + const void *val __unused, size_t size __unused) { - return -ENOTSUP; + return -ENOTSUP; } diff --git a/src/ast.c b/src/ast.c index 286e7d0..428f7dc 100644 --- a/src/ast.c +++ b/src/ast.c @@ -33,44 +33,48 @@ */ int ast_ahb_access(struct ast_ahb_args *args, struct ahb *ahb) { - uint32_t data; - int rc; + uint32_t data; + int rc; - if (args->read) { - unsigned long len = 4; + if (args->read) { + unsigned long len = 4; - if (args->read_length != NULL) - len = *args->read_length; + if (args->read_length != NULL) + len = *args->read_length; - if (len > 4) { - if ((rc = ahb_siphon_out(ahb, args->address, len, STDOUT_FILENO))) { - errno = -rc; - perror("ahb_siphon_in"); - exit(EXIT_FAILURE); - } - } else { - if ((rc = ahb_readl(ahb, args->address, &data))) { - errno = -rc; - perror("ahb_readl"); - exit(EXIT_FAILURE); - } - printf("0x%08x: 0x%08x\n", args->address, le32toh(data)); - } - } else { - if (args->write_value != NULL) { - if ((rc = ahb_writel(ahb, args->address, htole32(*args->write_value)))) { - errno = -rc; - perror("ahb_writel"); - exit(EXIT_FAILURE); - } - } else { - if ((rc = ahb_siphon_in(ahb, args->address, -1, STDIN_FILENO))) { - errno = -rc; - perror("ahb_writel"); - exit(EXIT_FAILURE); - } - } - } + if (len > 4) { + if ((rc = ahb_siphon_out(ahb, args->address, len, + STDOUT_FILENO))) { + errno = -rc; + perror("ahb_siphon_in"); + exit(EXIT_FAILURE); + } + } else { + if ((rc = ahb_readl(ahb, args->address, &data))) { + errno = -rc; + perror("ahb_readl"); + exit(EXIT_FAILURE); + } + printf("0x%08x: 0x%08x\n", args->address, + le32toh(data)); + } + } else { + if (args->write_value != NULL) { + if ((rc = ahb_writel(ahb, args->address, + htole32(*args->write_value)))) { + errno = -rc; + perror("ahb_writel"); + exit(EXIT_FAILURE); + } + } else { + if ((rc = ahb_siphon_in(ahb, args->address, -1, + STDIN_FILENO))) { + errno = -rc; + perror("ahb_writel"); + exit(EXIT_FAILURE); + } + } + } - return 0; + return 0; } diff --git a/src/ast.h b/src/ast.h index 7d78625..c99b3ea 100644 --- a/src/ast.h +++ b/src/ast.h @@ -10,19 +10,19 @@ #include struct ast_ahb_args { - /** + /** * read_length defines the width of an address to be read and is * not always required. */ - uint32_t *read_length; + uint32_t *read_length; - /** + /** * write_value is only required for bit-wise operations like register * modifications. For binary data, please consider to pipe the data to stdin. */ - uint32_t *write_value; - uint32_t address; - bool read; + uint32_t *write_value; + uint32_t address; + bool read; }; int ast_ahb_access(struct ast_ahb_args *args, struct ahb *ahb); diff --git a/src/bits.h b/src/bits.h index c134a33..06ba03a 100644 --- a/src/bits.h +++ b/src/bits.h @@ -4,9 +4,9 @@ #ifndef _BITS_H #define _BITS_H -#define BIT(x) (1UL << (x)) -#define GENMASK(h, l) ((BIT(h) | (BIT(h) - 1)) & ~(BIT(l) - 1)) -#define FIELD_PREP(mask, val) (((val) << __builtin_ctz(mask)) & (mask)) -#define FIELD_GET(mask, val) (((val) & (mask)) >> __builtin_ctz(mask)) +#define BIT(x) (1UL << (x)) +#define GENMASK(h, l) ((BIT(h) | (BIT(h) - 1)) & ~(BIT(l) - 1)) +#define FIELD_PREP(mask, val) (((val) << __builtin_ctz(mask)) & (mask)) +#define FIELD_GET(mask, val) (((val) & (mask)) >> __builtin_ctz(mask)) #endif diff --git a/src/bridge/debug.c b/src/bridge/debug.c index 534f3bb..f37d0c1 100644 --- a/src/bridge/debug.c +++ b/src/bridge/debug.c @@ -34,444 +34,454 @@ * * https://github.com/AspeedTech-BMC/openbmc/releases/download/v09.01/SDK_User_Guide_v09.01.pdf */ -static const char *password = "5z&0VK{@`HW}H~V310=l=JB+M]IV-f;Sz98XfCA&Rp)i|Jo=2?IBN$QaQ2\"Kb|Ov"; +static const char *password = + "5z&0VK{@`HW}H~V310=l=JB+M]IV-f;Sz98XfCA&Rp)i|Jo=2?IBN$QaQ2\"Kb|Ov"; static inline int streq(const char *a, const char *b) { - return !strcmp(a, b); + return !strcmp(a, b); } int debug_enter(struct debug *ctx) { - int rc; + int rc; - if (ctx->force_quit) { - logi("Foce quit requested\n"); - rc = console_set_baud(ctx->console, 115200); - if (rc < 0) - return rc; + if (ctx->force_quit) { + logi("Foce quit requested\n"); + rc = console_set_baud(ctx->console, 115200); + if (rc < 0) + return rc; - // Escape character should cancel previous commands - // q will exit debug mode - rc = prompt_write(&ctx->prompt, - "\x1Bq\r\n\x1Bq\r\n", strlen("\x1Bq\r\n\x1Bq\r\n")); - if (rc < 0) - return rc; - } + // Escape character should cancel previous commands + // q will exit debug mode + rc = prompt_write(&ctx->prompt, "\x1Bq\r\n\x1Bq\r\n", + strlen("\x1Bq\r\n\x1Bq\r\n")); + if (rc < 0) + return rc; + } - logi("Entering debug mode\n"); + logi("Entering debug mode\n"); - // Enter debug mode - rc = console_set_baud(ctx->console, 1200); - if (rc < 0) - return rc; + // Enter debug mode + rc = console_set_baud(ctx->console, 1200); + if (rc < 0) + return rc; - rc = prompt_write(&ctx->prompt, password, strlen(password)); - if (rc < 0) - goto cleanup_port_password; + rc = prompt_write(&ctx->prompt, password, strlen(password)); + if (rc < 0) + goto cleanup_port_password; - rc = prompt_expect(&ctx->prompt, "$ "); - if (rc < 0) - goto cleanup_port_password; + rc = prompt_expect(&ctx->prompt, "$ "); + if (rc < 0) + goto cleanup_port_password; - rc = console_set_baud(ctx->console, 115200); - if (!rc) - sleep(1); + rc = console_set_baud(ctx->console, 115200); + if (!rc) + sleep(1); - return rc; + return rc; cleanup_port_password: - console_set_baud(ctx->console, 115200); + console_set_baud(ctx->console, 115200); - prompt_run(&ctx->prompt, ""); + prompt_run(&ctx->prompt, ""); - return rc; + return rc; } int debug_exit(struct debug *ctx) { - int rc; + int rc; - logi("Exiting debug mode\n"); - rc = prompt_run(&ctx->prompt, "q"); - if (rc < 0) - return rc; + logi("Exiting debug mode\n"); + rc = prompt_run(&ctx->prompt, "q"); + if (rc < 0) + return rc; - sleep(1); + sleep(1); - prompt_run(&ctx->prompt, ""); + prompt_run(&ctx->prompt, ""); - return console_set_baud(ctx->console, 115200); + return console_set_baud(ctx->console, 115200); } int debug_probe(struct debug *ctx) { - int rc; + int rc; - logd("Probing %s\n", ctx->ahb.drv->name); + logd("Probing %s\n", ctx->ahb.drv->name); - rc = debug_enter(ctx); - if (rc < 0) - return rc; + rc = debug_enter(ctx); + if (rc < 0) + return rc; - return debug_exit(ctx); + return debug_exit(ctx); } static ssize_t debug_parse_d(char *line, char *buf) { - char *eoa, *words, *token, *stream; - char *saveptr; - char *cursor; - ssize_t rc; - - /* Strip leading address */ - eoa = strchr(line, ':'); - if (!eoa) - return -EBADE; - - /* Extract 4-byte values */ - words = eoa + 1; - cursor = buf; - stream = strdup(words); - while ((token = strtok_r(words, " ", &saveptr))) { - rc = sscanf(token, "%02hhx%02hhx%02hhx%02hhx", - &cursor[3], &cursor[2], &cursor[1], &cursor[0]); - if (rc < 0) { - rc = -errno; - goto done; - } - - if (rc < 4) { - loge("Wanted 4 but extracted %zd bytes from token '%s' in words '%s' from line '%s'\n", - rc, token, words, line); - rc = -EBADE; - goto done; - } - - cursor += rc; - words = NULL; - } - - rc = cursor - buf; + char *eoa, *words, *token, *stream; + char *saveptr; + char *cursor; + ssize_t rc; + + /* Strip leading address */ + eoa = strchr(line, ':'); + if (!eoa) + return -EBADE; + + /* Extract 4-byte values */ + words = eoa + 1; + cursor = buf; + stream = strdup(words); + while ((token = strtok_r(words, " ", &saveptr))) { + rc = sscanf(token, "%02hhx%02hhx%02hhx%02hhx", &cursor[3], + &cursor[2], &cursor[1], &cursor[0]); + if (rc < 0) { + rc = -errno; + goto done; + } + + if (rc < 4) { + loge("Wanted 4 but extracted %zd bytes from token '%s' in words '%s' from line '%s'\n", + rc, token, words, line); + rc = -EBADE; + goto done; + } + + cursor += rc; + words = NULL; + } + + rc = cursor - buf; done: - free(stream); - return rc; + free(stream); + return rc; } static int debug_read_fixed(struct debug *ctx, char mode, uint32_t phys, - uint32_t *val) + uint32_t *val) { - char buf[100], *response, *prompt; - char *command; - unsigned long parsed; - int rc; - - if (!(mode == 'i' || mode == 'r')) - return -EINVAL; - - rc = asprintf(&command, "%c %x", mode, phys); - if (rc < 0) - return -errno; - - prompt = &buf[0]; - rc = prompt_run_expect(&ctx->prompt, command, "$ ", &prompt, sizeof(buf)); - free(command); - if (rc < 0) - return rc; - - /* Terminate the string by overwriting the prompt */ - *prompt = '\0'; - - /* Discard echoed response */ - response = strchr(buf, ctx->prompt.eol[0]); - if (!response) - return -EIO; - - /* Extract the data */ - errno = 0; - parsed = strtoul(response, NULL, 16); - if (errno == ERANGE && (parsed == ULONG_MAX)) - return -errno; - - *val = parsed; - - return 0; + char buf[100], *response, *prompt; + char *command; + unsigned long parsed; + int rc; + + if (!(mode == 'i' || mode == 'r')) + return -EINVAL; + + rc = asprintf(&command, "%c %x", mode, phys); + if (rc < 0) + return -errno; + + prompt = &buf[0]; + rc = prompt_run_expect(&ctx->prompt, command, "$ ", &prompt, + sizeof(buf)); + free(command); + if (rc < 0) + return rc; + + /* Terminate the string by overwriting the prompt */ + *prompt = '\0'; + + /* Discard echoed response */ + response = strchr(buf, ctx->prompt.eol[0]); + if (!response) + return -EIO; + + /* Extract the data */ + errno = 0; + parsed = strtoul(response, NULL, 16); + if (errno == ERANGE && (parsed == ULONG_MAX)) + return -errno; + + *val = parsed; + + return 0; } #define DEBUG_D_MAX_LEN (128 * 1024) ssize_t debug_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len) { - char line[2 * sizeof("20002ba0:31e01002 20433002 30813003 e1a06002\r\n")]; - struct debug *ctx = to_debug(ahb); - size_t remaining = len; - size_t ingress; - char *command; - char *cursor; - ssize_t rc; - - if (len > SSIZE_MAX) { - return -1; - } - - if (len < 4) { - uint32_t val = 0; - - cursor = buf; - while (remaining) { - rc = debug_read_fixed(ctx, 'i', phys, &val); - if (rc < 0) - return -1; - - *cursor++ = val & 0xff; - remaining--; - phys++; - } - - return len; - } - - cursor = buf; - do { - size_t consumed; - int found; + char line[2 * + sizeof("20002ba0:31e01002 20433002 30813003 e1a06002\r\n")]; + struct debug *ctx = to_debug(ahb); + size_t remaining = len; + size_t ingress; + char *command; + char *cursor; + ssize_t rc; + + if (len > SSIZE_MAX) { + return -1; + } + + if (len < 4) { + uint32_t val = 0; + + cursor = buf; + while (remaining) { + rc = debug_read_fixed(ctx, 'i', phys, &val); + if (rc < 0) + return -1; + + *cursor++ = val & 0xff; + remaining--; + phys++; + } + + return len; + } + + cursor = buf; + do { + size_t consumed; + int found; retry: - ingress = remaining > DEBUG_D_MAX_LEN ? DEBUG_D_MAX_LEN : remaining; - - rc = asprintf(&command, "d %x %zx", phys, ingress); - if (rc < 0) - return -1; - - rc = prompt_run(&ctx->prompt, command); - free(command); - if (rc < 0) - return -1; - - /* Eat the echoed command */ - do { - found = prompt_gets(&ctx->prompt, line, sizeof(line)); - if (found < 0) - return -1; - } while (!strcmp("$ \n", line)); /* Deal any prompt from a prior run */ - - consumed = 0; - do { - found = prompt_gets(&ctx->prompt, line, sizeof(line)); - if (found < 0) - return -1; - - rc = debug_parse_d(line, cursor); - if (rc < 0) { - rc = prompt_run(&ctx->prompt, ""); - if (rc < 0) - return -1; - rc = prompt_expect(&ctx->prompt, "$ "); - if (rc < 0) - return -1; - loge("Failed to parse line '%s'\n", line); - loge("Retrying from address 0x%"PRIx32"\n", phys); - goto retry; - } - - cursor += rc; - consumed += rc; - } while (consumed < ingress); - - /* - * Normally we would prompt_expect() here, but prompt_gets() has likely - * swallowed the prompt, so we'll YOLO and just assume it's done. - */ - - phys += ingress; - remaining -= ingress; - } while(remaining); - - return len; + ingress = remaining > DEBUG_D_MAX_LEN ? DEBUG_D_MAX_LEN : + remaining; + + rc = asprintf(&command, "d %x %zx", phys, ingress); + if (rc < 0) + return -1; + + rc = prompt_run(&ctx->prompt, command); + free(command); + if (rc < 0) + return -1; + + /* Eat the echoed command */ + do { + found = prompt_gets(&ctx->prompt, line, sizeof(line)); + if (found < 0) + return -1; + } while (!strcmp("$ \n", + line)); /* Deal any prompt from a prior run */ + + consumed = 0; + do { + found = prompt_gets(&ctx->prompt, line, sizeof(line)); + if (found < 0) + return -1; + + rc = debug_parse_d(line, cursor); + if (rc < 0) { + rc = prompt_run(&ctx->prompt, ""); + if (rc < 0) + return -1; + rc = prompt_expect(&ctx->prompt, "$ "); + if (rc < 0) + return -1; + loge("Failed to parse line '%s'\n", line); + loge("Retrying from address 0x%" PRIx32 "\n", + phys); + goto retry; + } + + cursor += rc; + consumed += rc; + } while (consumed < ingress); + + /* + * Normally we would prompt_expect() here, but prompt_gets() has likely + * swallowed the prompt, so we'll YOLO and just assume it's done. + */ + + phys += ingress; + remaining -= ingress; + } while (remaining); + + return len; } #define DEBUG_CMD_U_MAX 128 ssize_t debug_write(struct ahb *ahb, uint32_t phys, const void *buf, size_t len) { - struct debug *ctx = to_debug(ahb); - const void *cursor; - size_t remaining; - size_t egress; - char *command; - char mode; - int rc; - - if (len > SSIZE_MAX) { - return -1; - } - - if (len <= 4) { - size_t remaining; - - remaining = len; - cursor = buf; - while (remaining) { - rc = asprintf(&command, "o %x %hhx", phys, *(const uint8_t *)cursor); - if (rc < 0) - return -1; - - rc = prompt_run(&ctx->prompt, command); - if (rc < 0) - return -1; - - rc = prompt_expect(&ctx->prompt, "$ "); - if (rc < 0) - return rc; - if (rc == 0) - return -1; - - cursor++; - phys++; - remaining--; - } - - return len; - } - - mode = 'u'; - - remaining = len; - cursor = buf; - do { - egress = remaining > DEBUG_CMD_U_MAX ? DEBUG_CMD_U_MAX : remaining; - - rc = asprintf(&command, "%c %x %zx", mode, phys, egress); - if (rc < 0) - return -1; - - rc = prompt_run(&ctx->prompt, command); - free(command); - if (rc < 0) - return -1; - - rc = prompt_write(&ctx->prompt, (const char *)cursor, egress); - if (rc < 0) - return -1; - - rc = prompt_expect(&ctx->prompt, "$ "); - if (rc < 0) - return -1; - - phys += egress; - cursor += egress; - remaining -= egress; - } while (remaining); - - return len; + struct debug *ctx = to_debug(ahb); + const void *cursor; + size_t remaining; + size_t egress; + char *command; + char mode; + int rc; + + if (len > SSIZE_MAX) { + return -1; + } + + if (len <= 4) { + size_t remaining; + + remaining = len; + cursor = buf; + while (remaining) { + rc = asprintf(&command, "o %x %hhx", phys, + *(const uint8_t *)cursor); + if (rc < 0) + return -1; + + rc = prompt_run(&ctx->prompt, command); + if (rc < 0) + return -1; + + rc = prompt_expect(&ctx->prompt, "$ "); + if (rc < 0) + return rc; + if (rc == 0) + return -1; + + cursor++; + phys++; + remaining--; + } + + return len; + } + + mode = 'u'; + + remaining = len; + cursor = buf; + do { + egress = remaining > DEBUG_CMD_U_MAX ? DEBUG_CMD_U_MAX : + remaining; + + rc = asprintf(&command, "%c %x %zx", mode, phys, egress); + if (rc < 0) + return -1; + + rc = prompt_run(&ctx->prompt, command); + free(command); + if (rc < 0) + return -1; + + rc = prompt_write(&ctx->prompt, (const char *)cursor, egress); + if (rc < 0) + return -1; + + rc = prompt_expect(&ctx->prompt, "$ "); + if (rc < 0) + return -1; + + phys += egress; + cursor += egress; + remaining -= egress; + } while (remaining); + + return len; } int debug_readl(struct ahb *ahb, uint32_t phys, uint32_t *val) { - struct debug *ctx = to_debug(ahb); - return debug_read_fixed(ctx, 'r', phys, val); + struct debug *ctx = to_debug(ahb); + return debug_read_fixed(ctx, 'r', phys, val); } int debug_writel(struct ahb *ahb, uint32_t phys, uint32_t val) { - struct debug *ctx = to_debug(ahb); - char *command; - int rc; - - rc = asprintf(&command, "w %x %x", phys, val); - if (rc < 0) - return -errno; - - rc = prompt_run(&ctx->prompt, command); - if (rc < 0) - return rc; - - /* XXX: This kludge is super annoying */ -#define AST_G5_WDT 0x1e785000 -#define WDT_RELOAD 0x04 - if (!((phys & ~0x20) == (AST_G5_WDT | WDT_RELOAD) && val == 0)) { + struct debug *ctx = to_debug(ahb); + char *command; + int rc; + + rc = asprintf(&command, "w %x %x", phys, val); + if (rc < 0) + return -errno; + + rc = prompt_run(&ctx->prompt, command); + if (rc < 0) + return rc; + + /* XXX: This kludge is super annoying */ +#define AST_G5_WDT 0x1e785000 +#define WDT_RELOAD 0x04 + if (!((phys & ~0x20) == (AST_G5_WDT | WDT_RELOAD) && val == 0)) { #undef AST_G5_WDT -#undef WDT_RELOAD - rc = prompt_expect(&ctx->prompt, "$ "); - if (rc < 0) - return rc; - if (rc == 0) - return -EINVAL; - } - - return 0; +#undef WDT_RELOAD + rc = prompt_expect(&ctx->prompt, "$ "); + if (rc < 0) + return rc; + if (rc == 0) + return -EINVAL; + } + + return 0; } static int ts16_console_init(struct debug *ctx, va_list args) { - const char *ip, *username, *password; - struct ts16 *ts16; - int port; - int fd; + const char *ip, *username, *password; + struct ts16 *ts16; + int port; + int fd; - ts16 = malloc(sizeof(*ts16)); - if (!ts16) - return -ENOMEM; + ts16 = malloc(sizeof(*ts16)); + if (!ts16) + return -ENOMEM; - ip = va_arg(args, const char *); - port = va_arg(args, int); - username = va_arg(args, const char *); - password = va_arg(args, const char *); + ip = va_arg(args, const char *); + port = va_arg(args, int); + username = va_arg(args, const char *); + password = va_arg(args, const char *); - fd = ts16_init(ts16, ip, port, username, password); - if (fd < 0) { goto cleanup_free; } + fd = ts16_init(ts16, ip, port, username, password); + if (fd < 0) { + goto cleanup_free; + } - ctx->console = &ts16->console; + ctx->console = &ts16->console; - return fd; + return fd; cleanup_free: - free(ts16); + free(ts16); - return fd; + return fd; } static int tty_console_init(struct debug *ctx, const char *path) { - struct tty *tty; - int fd; + struct tty *tty; + int fd; - tty = malloc(sizeof(*tty)); - if (!tty) - return -ENOMEM; + tty = malloc(sizeof(*tty)); + if (!tty) + return -ENOMEM; - fd = tty_init(tty, path); - if (fd < 0) { goto cleanup_free; } + fd = tty_init(tty, path); + if (fd < 0) { + goto cleanup_free; + } - ctx->console = &tty->console; + ctx->console = &tty->console; - return fd; + return fd; cleanup_free: - free(tty); + free(tty); - return fd; + return fd; } int debug_init(struct debug *ctx, ...) { - va_list args; - int rc; + va_list args; + int rc; - va_start(args, ctx); - rc = debug_init_v(ctx, args); - va_end(args); + va_start(args, ctx); + rc = debug_init_v(ctx, args); + va_end(args); - return rc; + return rc; } -static const struct ahb_ops debug_ahb_ops = { - .read = debug_read, - .write = debug_write, - .readl = debug_readl, - .writel = debug_writel -}; +static const struct ahb_ops debug_ahb_ops = { .read = debug_read, + .write = debug_write, + .readl = debug_readl, + .writel = debug_writel }; static struct ahb *debug_driver_probe(struct connection_args *connection); static void debug_driver_destroy(struct ahb *ahb); @@ -479,134 +489,139 @@ static int debug_driver_release(struct ahb *ahb); static int debug_driver_reinit(struct ahb *ahb); static struct bridge_driver debug_driver = { - .name = "debug-uart", - .probe = debug_driver_probe, - .destroy = debug_driver_destroy, - .release = debug_driver_release, - .reinit = debug_driver_reinit, - .path_required = true, + .name = "debug-uart", + .probe = debug_driver_probe, + .destroy = debug_driver_destroy, + .release = debug_driver_release, + .reinit = debug_driver_reinit, + .path_required = true, }; REGISTER_BRIDGE_DRIVER(debug_driver); int debug_init_v(struct debug *ctx, va_list args) { - const char *interface; - int rc, fd; + const char *interface; + int rc, fd; - interface = va_arg(args, const char *); + interface = va_arg(args, const char *); - if (!interface) - return -EINVAL; + if (!interface) + return -EINVAL; - if (streq("digi,portserver-ts-16", interface)) { - fd = ts16_console_init(ctx, args); - } else { - fd = tty_console_init(ctx, interface); - } + if (streq("digi,portserver-ts-16", interface)) { + fd = ts16_console_init(ctx, args); + } else { + fd = tty_console_init(ctx, interface); + } - if (fd < 0) { - loge("Failed to initialise the console (%s): %d\n", interface, fd); - return fd; - } + if (fd < 0) { + loge("Failed to initialise the console (%s): %d\n", interface, + fd); + return fd; + } - rc = prompt_init(&ctx->prompt, fd, "\r", false); - if (rc < 0) { goto cleanup_ts16; } + rc = prompt_init(&ctx->prompt, fd, "\r", false); + if (rc < 0) { + goto cleanup_ts16; + } - ahb_init_ops(&ctx->ahb, &debug_driver, &debug_ahb_ops); + ahb_init_ops(&ctx->ahb, &debug_driver, &debug_ahb_ops); - return 0; + return 0; cleanup_ts16: - console_destroy(ctx->console); + console_destroy(ctx->console); - return rc; + return rc; } int debug_destroy(struct debug *ctx) { - int rc = 0; + int rc = 0; - rc |= prompt_destroy(&ctx->prompt); - rc |= console_destroy(ctx->console); + rc |= prompt_destroy(&ctx->prompt); + rc |= console_destroy(ctx->console); - return rc ? -EBADF : 0; + return rc ? -EBADF : 0; } static struct ahb *debug_driver_probe(struct connection_args *connection) { - struct debug *ctx; - int rc; - - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return NULL; - } - - /* Early abort if no interface at all is defined */ - if (connection->interface == NULL) { - logd("No interface for debug provided, skipping...\n"); - return NULL; - } - - if (!connection->internet_args) { - /* Local debug interface */ - if ((rc = debug_init(ctx, connection->interface)) < 0) { - loge("Failed to initialise local debug interace on %s: %d\n", - connection->interface, rc); - goto cleanup_ctx; - } - } else { - /* Remote debug interface */ - rc = debug_init(ctx, connection->interface, connection->ip, connection->port, - connection->username, connection->password); - if (rc < 0) { - loge("Failed to initialise remote debug interface: %d\n", rc); - goto cleanup_ctx; - } - } - - if ((rc = debug_enter(ctx)) < 0) { - loge("Failed to enter debug UART context: %d\n", rc); - goto destroy_ctx; - } - - return debug_as_ahb(ctx); + struct debug *ctx; + int rc; + + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return NULL; + } + + /* Early abort if no interface at all is defined */ + if (connection->interface == NULL) { + logd("No interface for debug provided, skipping...\n"); + return NULL; + } + + if (!connection->internet_args) { + /* Local debug interface */ + if ((rc = debug_init(ctx, connection->interface)) < 0) { + loge("Failed to initialise local debug interace on %s: %d\n", + connection->interface, rc); + goto cleanup_ctx; + } + } else { + /* Remote debug interface */ + rc = debug_init(ctx, connection->interface, connection->ip, + connection->port, connection->username, + connection->password); + if (rc < 0) { + loge("Failed to initialise remote debug interface: %d\n", + rc); + goto cleanup_ctx; + } + } + + if ((rc = debug_enter(ctx)) < 0) { + loge("Failed to enter debug UART context: %d\n", rc); + goto destroy_ctx; + } + + return debug_as_ahb(ctx); destroy_ctx: - debug_destroy(ctx); + debug_destroy(ctx); cleanup_ctx: - free(ctx); + free(ctx); - return NULL; + return NULL; } static void debug_driver_destroy(struct ahb *ahb) { - struct debug *ctx = to_debug(ahb); - int rc; + struct debug *ctx = to_debug(ahb); + int rc; - if ((rc = debug_exit(ctx)) < 0) { - loge("Failed to exit debug UART context: %d\n", rc); - } + if ((rc = debug_exit(ctx)) < 0) { + loge("Failed to exit debug UART context: %d\n", rc); + } - if ((rc = debug_destroy(ctx)) < 0) { - loge("Failed to destroy debug bridge: %d\n", rc); - } + if ((rc = debug_destroy(ctx)) < 0) { + loge("Failed to destroy debug bridge: %d\n", rc); + } - free(ctx); + free(ctx); } static int debug_driver_release(struct ahb *ahb) { - struct debug *ctx = to_debug(ahb); + struct debug *ctx = to_debug(ahb); - return debug_exit(ctx); + return debug_exit(ctx); } static int debug_driver_reinit(struct ahb *ahb) { - struct debug *ctx = to_debug(ahb); + struct debug *ctx = to_debug(ahb); - return debug_enter(ctx); + return debug_enter(ctx); } diff --git a/src/bridge/debug.h b/src/bridge/debug.h index 8a57afa..3673a76 100644 --- a/src/bridge/debug.h +++ b/src/bridge/debug.h @@ -14,11 +14,11 @@ #include struct debug { - struct ahb ahb; - struct console *console; - struct prompt prompt; - int port; - int force_quit; + struct ahb ahb; + struct console *console; + struct prompt prompt; + int port; + int force_quit; }; int debug_init(struct debug *ctx, ...); @@ -31,11 +31,12 @@ int debug_probe(struct debug *ctx); static inline struct ahb *debug_as_ahb(struct debug *ctx) { - return &ctx->ahb; + return &ctx->ahb; } ssize_t debug_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len); -ssize_t debug_write(struct ahb *ahb, uint32_t phys, const void *buf, size_t len); +ssize_t debug_write(struct ahb *ahb, uint32_t phys, const void *buf, + size_t len); int debug_readl(struct ahb *ahb, uint32_t phys, uint32_t *val); int debug_writel(struct ahb *ahb, uint32_t phys, uint32_t val); diff --git a/src/bridge/devmem.c b/src/bridge/devmem.c index 5e9e204..a9dce5e 100644 --- a/src/bridge/devmem.c +++ b/src/bridge/devmem.c @@ -26,250 +26,259 @@ #include "ccan/container_of/container_of.h" -#define AST_SOC_IO 0x1e600000 -#define AST_SOC_IO_LEN 0x00200000 +#define AST_SOC_IO 0x1e600000 +#define AST_SOC_IO_LEN 0x00200000 #define to_devmem(ahb) container_of(ahb, struct devmem, ahb) int devmem_probe(struct devmem *ctx) { - int rc; + int rc; - logd("Probing %s\n", ctx->ahb.drv->name); + logd("Probing %s\n", ctx->ahb.drv->name); #ifndef __arm__ - return -ENOTSUP; + return -ENOTSUP; #endif - rc = rev_probe(devmem_as_ahb(ctx)); + rc = rev_probe(devmem_as_ahb(ctx)); - return rc < 0 ? rc : 1; + return rc < 0 ? rc : 1; } static int64_t devmem_setup_win(struct devmem *ctx, uint32_t phys, size_t len) { - uint32_t aligned = phys & ~(ctx->pgsize - 1); - uint32_t offset = phys & (ctx->pgsize - 1); - int rc; + uint32_t aligned = phys & ~(ctx->pgsize - 1); + uint32_t offset = phys & (ctx->pgsize - 1); + int rc; - if (ctx->win && !(ctx->phys == aligned && ctx->len >= offset + len)) { - rc = munmap(ctx->win, ctx->len); - if (rc < 0) - goto cleanup; + if (ctx->win && !(ctx->phys == aligned && ctx->len >= offset + len)) { + rc = munmap(ctx->win, ctx->len); + if (rc < 0) + goto cleanup; - ctx->win = NULL; - } + ctx->win = NULL; + } - if (!ctx->win) { - ctx->win = mmap(NULL, offset + len, PROT_READ | PROT_WRITE, MAP_SHARED, - ctx->fd, aligned); - if (ctx->win == MAP_FAILED) - goto cleanup; + if (!ctx->win) { + ctx->win = mmap(NULL, offset + len, PROT_READ | PROT_WRITE, + MAP_SHARED, ctx->fd, aligned); + if (ctx->win == MAP_FAILED) + goto cleanup; - ctx->phys = aligned; - ctx->len = offset + len; - } + ctx->phys = aligned; + ctx->len = offset + len; + } - return offset; + return offset; cleanup: - ctx->win = NULL; - ctx->phys = 0; - ctx->len = 0; + ctx->win = NULL; + ctx->phys = 0; + ctx->len = 0; - return -errno; + return -errno; } ssize_t devmem_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len) { - struct devmem *ctx = to_devmem(ahb); - int64_t woff; + struct devmem *ctx = to_devmem(ahb); + int64_t woff; - if (len > SSIZE_MAX) { - return -1; - } + if (len > SSIZE_MAX) { + return -1; + } - woff = devmem_setup_win(ctx, phys, len); - if (woff < 0) - return -1; + woff = devmem_setup_win(ctx, phys, len); + if (woff < 0) + return -1; - mmio_memcpy(buf, ctx->win + woff, len); + mmio_memcpy(buf, ctx->win + woff, len); - return len; + return len; } -ssize_t devmem_write(struct ahb *ahb, uint32_t phys, const void *buf, size_t len) +ssize_t devmem_write(struct ahb *ahb, uint32_t phys, const void *buf, + size_t len) { - struct devmem *ctx = to_devmem(ahb); - int64_t woff; + struct devmem *ctx = to_devmem(ahb); + int64_t woff; - if (len > SSIZE_MAX) { - return -1; - } + if (len > SSIZE_MAX) { + return -1; + } - woff = devmem_setup_win(ctx, phys, len); - if (woff < 0) - return -1; + woff = devmem_setup_win(ctx, phys, len); + if (woff < 0) + return -1; - mmio_memcpy(ctx->win + woff, buf, len); + mmio_memcpy(ctx->win + woff, buf, len); - return len; + return len; } int devmem_readl(struct ahb *ahb, uint32_t phys, uint32_t *val) { - struct devmem *ctx = to_devmem(ahb); - uint32_t container; + struct devmem *ctx = to_devmem(ahb); + uint32_t container; - if (phys & 0x3) - return -EINVAL; + if (phys & 0x3) + return -EINVAL; - if (phys >= AST_SOC_IO && phys < (AST_SOC_IO + AST_SOC_IO_LEN)) { - off_t offset = phys - AST_SOC_IO; - container = *((volatile uint32_t *)(((char *)ctx->io) + offset)); - } else { - int64_t woff; + if (phys >= AST_SOC_IO && phys < (AST_SOC_IO + AST_SOC_IO_LEN)) { + off_t offset = phys - AST_SOC_IO; + container = + *((volatile uint32_t *)(((char *)ctx->io) + offset)); + } else { + int64_t woff; - woff = devmem_setup_win(ctx, phys, sizeof(*val)); - if (woff < 0) - return woff; + woff = devmem_setup_win(ctx, phys, sizeof(*val)); + if (woff < 0) + return woff; - container = *((volatile uint32_t *)(((char *)ctx->win) + woff)); - } + container = *((volatile uint32_t *)(((char *)ctx->win) + woff)); + } - *val = le32toh(container); + *val = le32toh(container); - return 0; + return 0; } int devmem_writel(struct ahb *ahb, uint32_t phys, uint32_t val) { - struct devmem *ctx = to_devmem(ahb); + struct devmem *ctx = to_devmem(ahb); - if (phys & 0x3) - return -EINVAL; + if (phys & 0x3) + return -EINVAL; - val = htole32(val); + val = htole32(val); - if (phys >= AST_SOC_IO && phys < (AST_SOC_IO + AST_SOC_IO_LEN)) { - off_t offset = phys - AST_SOC_IO; - *(volatile uint32_t *)(((char *)ctx->io) + offset) = val; - } else { - int64_t woff; + if (phys >= AST_SOC_IO && phys < (AST_SOC_IO + AST_SOC_IO_LEN)) { + off_t offset = phys - AST_SOC_IO; + *(volatile uint32_t *)(((char *)ctx->io) + offset) = val; + } else { + int64_t woff; - woff = devmem_setup_win(ctx, phys, sizeof(val)); - if (woff < 0) - return woff; + woff = devmem_setup_win(ctx, phys, sizeof(val)); + if (woff < 0) + return woff; - *(volatile uint32_t *)(((char *)ctx->win) + woff) = val; - } + *(volatile uint32_t *)(((char *)ctx->win) + woff) = val; + } - iob(); + iob(); - return 0; + return 0; } -static const struct ahb_ops devmem_ahb_ops = { - .read = devmem_read, - .write = devmem_write, - .readl = devmem_readl, - .writel = devmem_writel -}; +static const struct ahb_ops devmem_ahb_ops = { .read = devmem_read, + .write = devmem_write, + .readl = devmem_readl, + .writel = devmem_writel }; static struct ahb *devmem_driver_probe(struct connection_args *connection); static void devmem_driver_destroy(struct ahb *ahb); static struct bridge_driver devmem_driver = { - .name = "devmem", - .probe = devmem_driver_probe, - .destroy = devmem_driver_destroy, - .local = true, + .name = "devmem", + .probe = devmem_driver_probe, + .destroy = devmem_driver_destroy, + .local = true, }; REGISTER_BRIDGE_DRIVER(devmem_driver); int devmem_init(struct devmem *ctx) { - int cleanup; - int rc; + int cleanup; + int rc; - ctx->pgsize = sysconf(_SC_PAGE_SIZE); + ctx->pgsize = sysconf(_SC_PAGE_SIZE); - ctx->fd = open("/dev/mem", O_RDWR | O_SYNC); - if (ctx->fd < 0) - return -errno; + ctx->fd = open("/dev/mem", O_RDWR | O_SYNC); + if (ctx->fd < 0) + return -errno; - ctx->io = mmap(NULL, AST_SOC_IO_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, - ctx->fd, AST_SOC_IO); - if (ctx->io == MAP_FAILED) { rc = -errno; goto cleanup_fd; } + ctx->io = mmap(NULL, AST_SOC_IO_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, + ctx->fd, AST_SOC_IO); + if (ctx->io == MAP_FAILED) { + rc = -errno; + goto cleanup_fd; + } - ctx->win = NULL; + ctx->win = NULL; - ahb_init_ops(&ctx->ahb, &devmem_driver, &devmem_ahb_ops); + ahb_init_ops(&ctx->ahb, &devmem_driver, &devmem_ahb_ops); - return 0; + return 0; cleanup_fd: - cleanup = close(ctx->fd); - if (cleanup < 0) { perror("close"); } + cleanup = close(ctx->fd); + if (cleanup < 0) { + perror("close"); + } - return rc; + return rc; } int devmem_destroy(struct devmem *ctx) { - int rc; + int rc; - assert(!ctx->win); + assert(!ctx->win); - rc = munmap(ctx->io, AST_SOC_IO_LEN); - if (rc < 0) { perror("munmap"); } + rc = munmap(ctx->io, AST_SOC_IO_LEN); + if (rc < 0) { + perror("munmap"); + } - rc = close(ctx->fd); - if (rc < 0) { perror("close"); } + rc = close(ctx->fd); + if (rc < 0) { + perror("close"); + } - return 0; + return 0; } static struct ahb * devmem_driver_probe(struct connection_args *connection __unused) { - struct devmem *ctx; - int rc; + struct devmem *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return NULL; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return NULL; + } - if ((rc = devmem_init(ctx)) < 0) { - logd("failed to initialise devmem bridge: %d\n", rc); - goto cleanup_ctx; - } + if ((rc = devmem_init(ctx)) < 0) { + logd("failed to initialise devmem bridge: %d\n", rc); + goto cleanup_ctx; + } - if ((rc = devmem_probe(ctx)) < 0) { - logd("Failed devmem probe: %d\n", rc); - goto destroy_ctx; - } + if ((rc = devmem_probe(ctx)) < 0) { + logd("Failed devmem probe: %d\n", rc); + goto destroy_ctx; + } - return devmem_as_ahb(ctx); + return devmem_as_ahb(ctx); destroy_ctx: - devmem_destroy(ctx); + devmem_destroy(ctx); cleanup_ctx: - free(ctx); + free(ctx); - return NULL; + return NULL; } static void devmem_driver_destroy(struct ahb *ahb) { - struct devmem *ctx = to_devmem(ahb); - int rc; + struct devmem *ctx = to_devmem(ahb); + int rc; - if ((rc = devmem_destroy(ctx)) < 0) { - loge("Failed to destroy devmem bridge: %d\n", rc); - } + if ((rc = devmem_destroy(ctx)) < 0) { + loge("Failed to destroy devmem bridge: %d\n", rc); + } - free(ctx); + free(ctx); } diff --git a/src/bridge/devmem.h b/src/bridge/devmem.h index 1ea006b..382f6c9 100644 --- a/src/bridge/devmem.h +++ b/src/bridge/devmem.h @@ -10,13 +10,13 @@ #include struct devmem { - struct ahb ahb; - int fd; - void *io; - void *win; - off_t phys; - size_t len; - off_t pgsize; + struct ahb ahb; + int fd; + void *io; + void *win; + off_t phys; + size_t len; + off_t pgsize; }; int devmem_init(struct devmem *ctx); @@ -26,11 +26,12 @@ int devmem_probe(struct devmem *ctx); static inline struct ahb *devmem_as_ahb(struct devmem *ctx) { - return &ctx->ahb; + return &ctx->ahb; } ssize_t devmem_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len); -ssize_t devmem_write(struct ahb *ahb, uint32_t phys, const void *buf, size_t len); +ssize_t devmem_write(struct ahb *ahb, uint32_t phys, const void *buf, + size_t len); int devmem_readl(struct ahb *ahb, uint32_t phys, uint32_t *val); int devmem_writel(struct ahb *ahb, uint32_t phys, uint32_t val); diff --git a/src/bridge/ilpc.c b/src/bridge/ilpc.c index 6195585..6276d58 100644 --- a/src/bridge/ilpc.c +++ b/src/bridge/ilpc.c @@ -23,354 +23,352 @@ int ilpcb_probe(struct ilpcb *ctx) { - int rc; + int rc; - logd("Probing %s\n", ctx->ahb.drv->name); + logd("Probing %s\n", ctx->ahb.drv->name); - if (!sio_probe(&ctx->sio)) - return 0; + if (!sio_probe(&ctx->sio)) + return 0; - rc = rev_probe(ilpcb_as_ahb(ctx)); + rc = rev_probe(ilpcb_as_ahb(ctx)); - return rc < 0 ? rc : 1; + return rc < 0 ? rc : 1; } int ilpcb_mode(struct ilpcb *ctx) { - uint32_t hicrb = 0; - int rc; + uint32_t hicrb = 0; + int rc; - rc = ilpcb_readl(ilpcb_as_ahb(ctx), 0x1e789100, &hicrb); - if (rc) - return rc; + rc = ilpcb_readl(ilpcb_as_ahb(ctx), 0x1e789100, &hicrb); + if (rc) + return rc; - return !!(hicrb & LPC_HICRB_ILPCB_RO); /* Maps to enum ilpcb_mode */ + return !!(hicrb & LPC_HICRB_ILPCB_RO); /* Maps to enum ilpcb_mode */ } ssize_t ilpcb_read(struct ahb *ahb, uint32_t addr, void *buf, size_t len) { - struct ilpcb *ctx = to_ilpcb(ahb); - struct sio *sio = &ctx->sio; - size_t remaining; - uint8_t data; - int locked; - int rc; - - if (len > SSIZE_MAX) - return -1; - - rc = sio_unlock(sio); - if (rc) - goto done; - - /* Select iLPC2AHB */ - rc = sio_select(sio, sio_ilpc); - if (rc) - goto done; - - /* Enable iLPC2AHB */ - rc = sio_writeb(sio, 0x30, 0x01); - if (rc) - goto done; - - /* 1-byte access */ - rc = sio_writeb(sio, 0xf8, 0); - if (rc) - goto done; - - /* XXX: Think about optimising this */ - remaining = len; - while (remaining) { - /* Address */ - rc |= sio_writeb(sio, 0xf0, addr >> 24); - rc |= sio_writeb(sio, 0xf1, addr >> 16); - rc |= sio_writeb(sio, 0xf2, addr >> 8); - rc |= sio_writeb(sio, 0xf3, addr ); - if (rc) - goto done; - - /* Trigger */ - rc = sio_readb(sio, 0xfe, &data); - if (rc) - goto done; - - rc = sio_readb(sio, 0xf7, (uint8_t *)buf); - if (rc) - goto done; - - buf++; - addr++; - remaining--; - } + struct ilpcb *ctx = to_ilpcb(ahb); + struct sio *sio = &ctx->sio; + size_t remaining; + uint8_t data; + int locked; + int rc; + + if (len > SSIZE_MAX) + return -1; + + rc = sio_unlock(sio); + if (rc) + goto done; + + /* Select iLPC2AHB */ + rc = sio_select(sio, sio_ilpc); + if (rc) + goto done; + + /* Enable iLPC2AHB */ + rc = sio_writeb(sio, 0x30, 0x01); + if (rc) + goto done; + + /* 1-byte access */ + rc = sio_writeb(sio, 0xf8, 0); + if (rc) + goto done; + + /* XXX: Think about optimising this */ + remaining = len; + while (remaining) { + /* Address */ + rc |= sio_writeb(sio, 0xf0, addr >> 24); + rc |= sio_writeb(sio, 0xf1, addr >> 16); + rc |= sio_writeb(sio, 0xf2, addr >> 8); + rc |= sio_writeb(sio, 0xf3, addr); + if (rc) + goto done; + + /* Trigger */ + rc = sio_readb(sio, 0xfe, &data); + if (rc) + goto done; + + rc = sio_readb(sio, 0xf7, (uint8_t *)buf); + if (rc) + goto done; + + buf++; + addr++; + remaining--; + } done: - locked = sio_lock(sio); - if (locked) { - errno = -locked; - perror("sio_lock"); - } + locked = sio_lock(sio); + if (locked) { + errno = -locked; + perror("sio_lock"); + } - return rc ? -1 : (ssize_t)len; + return rc ? -1 : (ssize_t)len; } ssize_t ilpcb_write(struct ahb *ahb, uint32_t addr, const void *buf, size_t len) { - struct ilpcb *ctx = to_ilpcb(ahb); - struct sio *sio = &ctx->sio; - size_t remaining; - int locked; - int rc; - - if (len > SSIZE_MAX) - return -1; - - rc = sio_unlock(sio); - if (rc) - goto done; - - /* Select iLPC2AHB */ - rc = sio_select(sio, sio_ilpc); - if (rc) - goto done; - - /* Enable iLPC2AHB */ - rc = sio_writeb(sio, 0x30, 0x01); - if (rc) - goto done; - - /* 1-byte access */ - rc = sio_writeb(sio, 0xf8, 0); - if (rc) - goto done; - - /* XXX: Think about optimising this */ - remaining = len; - while (remaining) { - /* Address */ - rc |= sio_writeb(sio, 0xf0, addr >> 24); - rc |= sio_writeb(sio, 0xf1, addr >> 16); - rc |= sio_writeb(sio, 0xf2, addr >> 8); - rc |= sio_writeb(sio, 0xf3, addr ); - if (rc) - goto done; - - rc = sio_writeb(sio, 0xf7, *(uint8_t *)buf); - if (rc) - goto done; - - /* Trigger */ - rc = sio_writeb(sio, 0xfe, 0xcf); - if (rc) - goto done; - - buf++; - remaining--; - } + struct ilpcb *ctx = to_ilpcb(ahb); + struct sio *sio = &ctx->sio; + size_t remaining; + int locked; + int rc; + + if (len > SSIZE_MAX) + return -1; + + rc = sio_unlock(sio); + if (rc) + goto done; + + /* Select iLPC2AHB */ + rc = sio_select(sio, sio_ilpc); + if (rc) + goto done; + + /* Enable iLPC2AHB */ + rc = sio_writeb(sio, 0x30, 0x01); + if (rc) + goto done; + + /* 1-byte access */ + rc = sio_writeb(sio, 0xf8, 0); + if (rc) + goto done; + + /* XXX: Think about optimising this */ + remaining = len; + while (remaining) { + /* Address */ + rc |= sio_writeb(sio, 0xf0, addr >> 24); + rc |= sio_writeb(sio, 0xf1, addr >> 16); + rc |= sio_writeb(sio, 0xf2, addr >> 8); + rc |= sio_writeb(sio, 0xf3, addr); + if (rc) + goto done; + + rc = sio_writeb(sio, 0xf7, *(uint8_t *)buf); + if (rc) + goto done; + + /* Trigger */ + rc = sio_writeb(sio, 0xfe, 0xcf); + if (rc) + goto done; + + buf++; + remaining--; + } done: - locked = sio_lock(sio); - if (locked) { - errno = -locked; - perror("sio_lock"); - } + locked = sio_lock(sio); + if (locked) { + errno = -locked; + perror("sio_lock"); + } - return rc ? -1 : (ssize_t)len; + return rc ? -1 : (ssize_t)len; } /* Little-endian */ int ilpcb_readl(struct ahb *ahb, uint32_t addr, uint32_t *val) { - struct ilpcb *ctx = to_ilpcb(ahb); - struct sio *sio = &ctx->sio; - uint32_t extracted; - uint8_t data; - int locked; - int rc; - - rc = sio_unlock(sio); - if (rc) - goto done; - - /* Select iLPC2AHB */ - rc = sio_select(sio, sio_ilpc); - if (rc) - goto done; - - /* Enable iLPC2AHB */ - rc = sio_writeb(sio, 0x30, 0x01); - if (rc) - goto done; - - /* 4-byte access */ - rc = sio_writeb(sio, 0xf8, 2); - if (rc) - goto done; - - /* Address */ - rc |= sio_writeb(sio, 0xf0, addr >> 24); - rc |= sio_writeb(sio, 0xf1, addr >> 16); - rc |= sio_writeb(sio, 0xf2, addr >> 8); - rc |= sio_writeb(sio, 0xf3, addr ); - if (rc) - goto done; - - /* Trigger */ - rc = sio_readb(sio, 0xfe, &data); - if (rc) - goto done; - - /* Value */ - extracted = 0; - rc |= sio_readb(sio, 0xf4, &data); - extracted = (extracted << 8) | data; - rc |= sio_readb(sio, 0xf5, &data); - extracted = (extracted << 8) | data; - rc |= sio_readb(sio, 0xf6, &data); - extracted = (extracted << 8) | data; - rc |= sio_readb(sio, 0xf7, &data); - extracted = (extracted << 8) | data; - if (rc) - goto done; - - *val = extracted; + struct ilpcb *ctx = to_ilpcb(ahb); + struct sio *sio = &ctx->sio; + uint32_t extracted; + uint8_t data; + int locked; + int rc; + + rc = sio_unlock(sio); + if (rc) + goto done; + + /* Select iLPC2AHB */ + rc = sio_select(sio, sio_ilpc); + if (rc) + goto done; + + /* Enable iLPC2AHB */ + rc = sio_writeb(sio, 0x30, 0x01); + if (rc) + goto done; + + /* 4-byte access */ + rc = sio_writeb(sio, 0xf8, 2); + if (rc) + goto done; + + /* Address */ + rc |= sio_writeb(sio, 0xf0, addr >> 24); + rc |= sio_writeb(sio, 0xf1, addr >> 16); + rc |= sio_writeb(sio, 0xf2, addr >> 8); + rc |= sio_writeb(sio, 0xf3, addr); + if (rc) + goto done; + + /* Trigger */ + rc = sio_readb(sio, 0xfe, &data); + if (rc) + goto done; + + /* Value */ + extracted = 0; + rc |= sio_readb(sio, 0xf4, &data); + extracted = (extracted << 8) | data; + rc |= sio_readb(sio, 0xf5, &data); + extracted = (extracted << 8) | data; + rc |= sio_readb(sio, 0xf6, &data); + extracted = (extracted << 8) | data; + rc |= sio_readb(sio, 0xf7, &data); + extracted = (extracted << 8) | data; + if (rc) + goto done; + + *val = extracted; done: - locked = sio_lock(sio); - if (locked) { - errno = -locked; - perror("sio_lock"); - } + locked = sio_lock(sio); + if (locked) { + errno = -locked; + perror("sio_lock"); + } - return rc; + return rc; } /* Little-endian */ int ilpcb_writel(struct ahb *ahb, uint32_t addr, uint32_t val) { - struct ilpcb *ctx = to_ilpcb(ahb); - struct sio *sio = &ctx->sio; - int locked; - int rc; - - rc = sio_unlock(sio); - if (rc) - goto done; - - /* Select iLPC2AHB */ - rc = sio_select(sio, sio_ilpc); - if (rc) - goto done; - - /* Enable iLPC2AHB */ - rc = sio_writeb(sio, 0x30, 0x01); - if (rc) - goto done; - - /* 4-byte access */ - rc = sio_writeb(sio, 0xf8, 2); - if (rc) - goto done; - - /* Address */ - rc |= sio_writeb(sio, 0xf0, addr >> 24); - rc |= sio_writeb(sio, 0xf1, addr >> 16); - rc |= sio_writeb(sio, 0xf2, addr >> 8); - rc |= sio_writeb(sio, 0xf3, addr >> 0); - if (rc) - goto done; - - /* Value */ - rc |= sio_writeb(sio, 0xf4, val >> 24); - rc |= sio_writeb(sio, 0xf5, val >> 16); - rc |= sio_writeb(sio, 0xf6, val >> 8); - rc |= sio_writeb(sio, 0xf7, val >> 0); - if (rc) - goto done; - - /* Trigger */ - rc = sio_writeb(sio, 0xfe, 0xcf); - if (rc) - goto done; + struct ilpcb *ctx = to_ilpcb(ahb); + struct sio *sio = &ctx->sio; + int locked; + int rc; + + rc = sio_unlock(sio); + if (rc) + goto done; + + /* Select iLPC2AHB */ + rc = sio_select(sio, sio_ilpc); + if (rc) + goto done; + + /* Enable iLPC2AHB */ + rc = sio_writeb(sio, 0x30, 0x01); + if (rc) + goto done; + + /* 4-byte access */ + rc = sio_writeb(sio, 0xf8, 2); + if (rc) + goto done; + + /* Address */ + rc |= sio_writeb(sio, 0xf0, addr >> 24); + rc |= sio_writeb(sio, 0xf1, addr >> 16); + rc |= sio_writeb(sio, 0xf2, addr >> 8); + rc |= sio_writeb(sio, 0xf3, addr >> 0); + if (rc) + goto done; + + /* Value */ + rc |= sio_writeb(sio, 0xf4, val >> 24); + rc |= sio_writeb(sio, 0xf5, val >> 16); + rc |= sio_writeb(sio, 0xf6, val >> 8); + rc |= sio_writeb(sio, 0xf7, val >> 0); + if (rc) + goto done; + + /* Trigger */ + rc = sio_writeb(sio, 0xfe, 0xcf); + if (rc) + goto done; done: - locked = sio_lock(sio); - if (locked) { - errno = -locked; - perror("Failed to lock SuperIO device"); - } + locked = sio_lock(sio); + if (locked) { + errno = -locked; + perror("Failed to lock SuperIO device"); + } - return rc; + return rc; } -static const struct ahb_ops ilpcb_ops = { - .read = ilpcb_read, - .write = ilpcb_write, - .readl = ilpcb_readl, - .writel = ilpcb_writel -}; +static const struct ahb_ops ilpcb_ops = { .read = ilpcb_read, + .write = ilpcb_write, + .readl = ilpcb_readl, + .writel = ilpcb_writel }; static struct ahb *ilpcb_driver_probe(struct connection_args *connection); static void ilpcb_driver_destroy(struct ahb *ahb); static struct bridge_driver ilpcb_driver = { - .name = "ilpc", - .probe = ilpcb_driver_probe, - .destroy = ilpcb_driver_destroy, + .name = "ilpc", + .probe = ilpcb_driver_probe, + .destroy = ilpcb_driver_destroy, }; REGISTER_BRIDGE_DRIVER(ilpcb_driver); int ilpcb_init(struct ilpcb *ctx) { - ahb_init_ops(&ctx->ahb, &ilpcb_driver, &ilpcb_ops); + ahb_init_ops(&ctx->ahb, &ilpcb_driver, &ilpcb_ops); - return sio_init(&ctx->sio); + return sio_init(&ctx->sio); } int ilpcb_destroy(struct ilpcb *ctx) { - return sio_destroy(&ctx->sio); + return sio_destroy(&ctx->sio); } static struct ahb * ilpcb_driver_probe(struct connection_args *connection __unused) { - struct ilpcb *ctx; - int rc; + struct ilpcb *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return NULL; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return NULL; + } - if ((rc = ilpcb_init(ctx)) < 0) { - logd("Failed to initialise iLPC bridge: %d\n", rc); - goto cleanup_ctx; - } + if ((rc = ilpcb_init(ctx)) < 0) { + logd("Failed to initialise iLPC bridge: %d\n", rc); + goto cleanup_ctx; + } - if ((rc = ilpcb_probe(ctx)) < 0) { - logd("Failed iLPC probe: %d\n", rc); - goto destroy_ctx; - } + if ((rc = ilpcb_probe(ctx)) < 0) { + logd("Failed iLPC probe: %d\n", rc); + goto destroy_ctx; + } - return ilpcb_as_ahb(ctx); + return ilpcb_as_ahb(ctx); destroy_ctx: - ilpcb_destroy(ctx); + ilpcb_destroy(ctx); cleanup_ctx: - free(ctx); + free(ctx); - return NULL; + return NULL; } static void ilpcb_driver_destroy(struct ahb *ahb) { - struct ilpcb *ctx = to_ilpcb(ahb); - int rc; + struct ilpcb *ctx = to_ilpcb(ahb); + int rc; - if ((rc = ilpcb_destroy(ctx)) < 0) { - loge("Failed to destroy iLPC bridge: %d\n", rc); - } + if ((rc = ilpcb_destroy(ctx)) < 0) { + loge("Failed to destroy iLPC bridge: %d\n", rc); + } - free(ctx); + free(ctx); } diff --git a/src/bridge/ilpc.h b/src/bridge/ilpc.h index ad453fa..415f663 100644 --- a/src/bridge/ilpc.h +++ b/src/bridge/ilpc.h @@ -11,10 +11,9 @@ #include #include -struct ilpcb -{ - struct ahb ahb; - struct sio sio; +struct ilpcb { + struct ahb ahb; + struct sio sio; }; int ilpcb_init(struct ilpcb *ctx); @@ -23,12 +22,13 @@ int ilpcb_probe(struct ilpcb *ctx); static inline struct ahb *ilpcb_as_ahb(struct ilpcb *ctx) { - return &ctx->ahb; + return &ctx->ahb; } /* These are going to be *amazingly* slow. Use the l2ab */ ssize_t ilpcb_read(struct ahb *ahb, uint32_t addr, void *buf, size_t len); -ssize_t ilpcb_write(struct ahb *ahb, uint32_t addr, const void *buf, size_t len); +ssize_t ilpcb_write(struct ahb *ahb, uint32_t addr, const void *buf, + size_t len); int ilpcb_readl(struct ahb *ahb, uint32_t addr, uint32_t *val); int ilpcb_writel(struct ahb *ahb, uint32_t addr, uint32_t val); diff --git a/src/bridge/l2a.c b/src/bridge/l2a.c index 299cc6d..bc62987 100644 --- a/src/bridge/l2a.c +++ b/src/bridge/l2a.c @@ -15,160 +15,162 @@ #include #include -#define LPC_HICR7 0x1e789088 -#define LPC_HICR8 0x1e78908c -#define L2AB_WINDOW_SIZE (1 << 27) +#define LPC_HICR7 0x1e789088 +#define LPC_HICR8 0x1e78908c +#define L2AB_WINDOW_SIZE (1 << 27) #define to_l2ab(ahb) container_of(ahb, struct l2ab, ahb) /* @return The LPC FW offset mapped to phys */ int64_t l2ab_map(struct l2ab *ctx, uint32_t phys, size_t len) { - struct ilpcb *ilpcb = &ctx->ilpcb; - uint32_t aligned = phys & ~0xffff; - uint32_t hicr7, hicr8; - int rc; + struct ilpcb *ilpcb = &ctx->ilpcb; + uint32_t aligned = phys & ~0xffff; + uint32_t hicr7, hicr8; + int rc; - /* Check if the requested phys/len fit inside the current mapping */ - if (phys >= ctx->phys && (phys + len) <= (ctx->phys + ctx->len)) - return phys - ctx->phys; + /* Check if the requested phys/len fit inside the current mapping */ + if (phys >= ctx->phys && (phys + len) <= (ctx->phys + ctx->len)) + return phys - ctx->phys; - /* Check if we'd intersect hiomapd/skiboot territory */ - if (len > L2AB_WINDOW_SIZE) - return -EINVAL; + /* Check if we'd intersect hiomapd/skiboot territory */ + if (len > L2AB_WINDOW_SIZE) + return -EINVAL; - /* Expand length to account for alignment */ - if (len < (phys + len - aligned)) - len = phys + len - aligned; + /* Expand length to account for alignment */ + if (len < (phys + len - aligned)) + len = phys + len - aligned; - /* - * Open a window sized to the nearest power of 2 greater than len that is - * at least 2^16 bytes, due to the structure of HICR8. - */ - if (len < (1 << 16)) - len = (1 << 16); + /* + * Open a window sized to the nearest power of 2 greater than len that is + * at least 2^16 bytes, due to the structure of HICR8. + */ + if (len < (1 << 16)) + len = (1 << 16); - if (__builtin_popcount(len) != 1) - len = 1 << (31 - __builtin_clz(len)); + if (__builtin_popcount(len) != 1) + len = 1 << (31 - __builtin_clz(len)); - hicr7 = (phys & ~(0xffff)); - hicr8 = (~(len - 1)) | ((len - 1) >> 16); + hicr7 = (phys & ~(0xffff)); + hicr8 = (~(len - 1)) | ((len - 1) >> 16); - rc = ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR7, hicr7); - if (rc) - return rc; + rc = ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR7, hicr7); + if (rc) + return rc; - rc = ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR8, hicr8); - if (rc) - return rc; + rc = ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR8, hicr8); + if (rc) + return rc; - ctx->phys = hicr7; /* This is correct as we're mapping to 0 in LPC FW */ - ctx->len = len; + ctx->phys = hicr7; /* This is correct as we're mapping to 0 in LPC FW */ + ctx->len = len; - return phys & 0xffff; + return phys & 0xffff; } ssize_t l2ab_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len) { - struct l2ab *ctx = to_l2ab(ahb); - size_t remaining = len; - ssize_t ingress; - int64_t offset; + struct l2ab *ctx = to_l2ab(ahb); + size_t remaining = len; + ssize_t ingress; + int64_t offset; - if (len > SSIZE_MAX) { - return -1; - } + if (len > SSIZE_MAX) { + return -1; + } - do { - ingress = remaining > L2AB_WINDOW_SIZE ? L2AB_WINDOW_SIZE : remaining; + do { + ingress = remaining > L2AB_WINDOW_SIZE ? L2AB_WINDOW_SIZE : + remaining; - offset = l2ab_map(ctx, phys, ingress); - if (offset < 0) - return -1; + offset = l2ab_map(ctx, phys, ingress); + if (offset < 0) + return -1; - ingress = lpc_read(&ctx->fw, offset, buf, ingress); - if (ingress < 0) - return -1; + ingress = lpc_read(&ctx->fw, offset, buf, ingress); + if (ingress < 0) + return -1; - phys += ingress; - buf += ingress; - remaining -= ingress; - } while (remaining); + phys += ingress; + buf += ingress; + remaining -= ingress; + } while (remaining); - return len; + return len; } ssize_t l2ab_write(struct ahb *ahb, uint32_t phys, const void *buf, size_t len) { - struct l2ab *ctx = to_l2ab(ahb); - size_t remaining = len; - ssize_t egress; - int64_t offset; + struct l2ab *ctx = to_l2ab(ahb); + size_t remaining = len; + ssize_t egress; + int64_t offset; - if (len > SSIZE_MAX) { - return -1; - } + if (len > SSIZE_MAX) { + return -1; + } - do { - egress = remaining > L2AB_WINDOW_SIZE ? L2AB_WINDOW_SIZE : remaining; + do { + egress = remaining > L2AB_WINDOW_SIZE ? L2AB_WINDOW_SIZE : + remaining; - offset = l2ab_map(ctx, phys, egress); - if (offset < 0) - return -1; + offset = l2ab_map(ctx, phys, egress); + if (offset < 0) + return -1; - egress = lpc_write(&ctx->fw, offset, buf, egress); - if (egress < 0) - return -1; + egress = lpc_write(&ctx->fw, offset, buf, egress); + if (egress < 0) + return -1; - phys += egress; - buf += egress; - remaining -= egress; - } while (remaining); + phys += egress; + buf += egress; + remaining -= egress; + } while (remaining); - return len; + return len; } int l2ab_readl(struct ahb *ahb, uint32_t phys, uint32_t *val) { - struct l2ab *ctx = to_l2ab(ahb); - return ilpcb_readl(ilpcb_as_ahb(&ctx->ilpcb), phys, val); + struct l2ab *ctx = to_l2ab(ahb); + return ilpcb_readl(ilpcb_as_ahb(&ctx->ilpcb), phys, val); } int l2ab_writel(struct ahb *ahb, uint32_t phys, uint32_t val) { - struct l2ab *ctx = to_l2ab(ahb); - return ilpcb_writel(ilpcb_as_ahb(&ctx->ilpcb), phys, val); + struct l2ab *ctx = to_l2ab(ahb); + return ilpcb_writel(ilpcb_as_ahb(&ctx->ilpcb), phys, val); } static const struct ahb_ops l2ab_ahb_ops = { - .read = l2ab_read, - .write = l2ab_write, - .readl = l2ab_readl, - .writel = l2ab_writel, + .read = l2ab_read, + .write = l2ab_write, + .readl = l2ab_readl, + .writel = l2ab_writel, }; static int l2ab_save_hicr78(struct l2ab *ctx) { - struct ilpcb *ilpcb = &ctx->ilpcb; - int rc; + struct ilpcb *ilpcb = &ctx->ilpcb; + int rc; - rc = ilpcb_readl(ilpcb_as_ahb(ilpcb), LPC_HICR7, &ctx->restore7); - if (rc) - return rc; + rc = ilpcb_readl(ilpcb_as_ahb(ilpcb), LPC_HICR7, &ctx->restore7); + if (rc) + return rc; - return ilpcb_readl(ilpcb_as_ahb(ilpcb), LPC_HICR8, &ctx->restore8); + return ilpcb_readl(ilpcb_as_ahb(ilpcb), LPC_HICR8, &ctx->restore8); } static int l2ab_restore_hicr78(struct l2ab *ctx) { - struct ilpcb *ilpcb = &ctx->ilpcb; - int rc; + struct ilpcb *ilpcb = &ctx->ilpcb; + int rc; - rc = ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR8, ctx->restore8); - if (rc) - return rc; + rc = ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR8, ctx->restore8); + if (rc) + return rc; - return ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR7, ctx->restore7); + return ilpcb_writel(ilpcb_as_ahb(ilpcb), LPC_HICR7, ctx->restore7); } static struct ahb *l2ab_driver_probe(struct connection_args *connection); @@ -177,103 +179,103 @@ static int l2ab_driver_release(struct ahb *ahb); static int l2ab_driver_reinit(struct ahb *ahb); static struct bridge_driver l2ab_driver = { - .name = "l2a", - .probe = l2ab_driver_probe, - .destroy = l2ab_driver_destroy, - .reinit = l2ab_driver_reinit, - .release = l2ab_driver_release, + .name = "l2a", + .probe = l2ab_driver_probe, + .destroy = l2ab_driver_destroy, + .reinit = l2ab_driver_reinit, + .release = l2ab_driver_release, }; REGISTER_BRIDGE_DRIVER(l2ab_driver); int l2ab_init(struct l2ab *ctx) { - struct ilpcb *ilpcb = &ctx->ilpcb; - int rc; + struct ilpcb *ilpcb = &ctx->ilpcb; + int rc; - rc = lpc_init(&ctx->fw, "fw"); - if (rc) - return rc; + rc = lpc_init(&ctx->fw, "fw"); + if (rc) + return rc; - rc = ilpcb_init(ilpcb); - if (rc) - return rc; + rc = ilpcb_init(ilpcb); + if (rc) + return rc; - rc = ilpcb_probe(ilpcb); - if (rc) - goto cleanup; + rc = ilpcb_probe(ilpcb); + if (rc) + goto cleanup; - rc = l2ab_save_hicr78(ctx); - if (rc) - goto cleanup; + rc = l2ab_save_hicr78(ctx); + if (rc) + goto cleanup; - ahb_init_ops(&ctx->ahb, &l2ab_driver, &l2ab_ahb_ops); + ahb_init_ops(&ctx->ahb, &l2ab_driver, &l2ab_ahb_ops); - return 0; + return 0; cleanup: - ilpcb_destroy(ilpcb); - lpc_destroy(&ctx->fw); + ilpcb_destroy(ilpcb); + lpc_destroy(&ctx->fw); - return rc; + return rc; } int l2ab_destroy(struct l2ab *ctx) { - int rc; + int rc; - rc = l2ab_restore_hicr78(ctx); - if (rc) - return rc; + rc = l2ab_restore_hicr78(ctx); + if (rc) + return rc; - rc = ilpcb_destroy(&ctx->ilpcb); - if (rc) - return rc; + rc = ilpcb_destroy(&ctx->ilpcb); + if (rc) + return rc; - return lpc_destroy(&ctx->fw); + return lpc_destroy(&ctx->fw); } static struct ahb * l2ab_driver_probe(struct connection_args *connection __unused) { - struct l2ab *ctx; - int rc; + struct l2ab *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return NULL; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return NULL; + } - if ((rc = l2ab_init(ctx)) < 0) { - logd("Failed to initialise L2A bridge: %d\n", rc); - goto cleanup_ctx; - } + if ((rc = l2ab_init(ctx)) < 0) { + logd("Failed to initialise L2A bridge: %d\n", rc); + goto cleanup_ctx; + } - return l2ab_as_ahb(ctx); + return l2ab_as_ahb(ctx); cleanup_ctx: - free(ctx); + free(ctx); - return NULL; + return NULL; } static void l2ab_driver_destroy(struct ahb *ahb) { - struct l2ab *ctx = to_l2ab(ahb); - int rc; + struct l2ab *ctx = to_l2ab(ahb); + int rc; - if ((rc = l2ab_destroy(ctx)) < 0) { - loge("Failed to destroy L2A bridge: %d\n", rc); - } + if ((rc = l2ab_destroy(ctx)) < 0) { + loge("Failed to destroy L2A bridge: %d\n", rc); + } - free(ctx); + free(ctx); } static int l2ab_driver_release(struct ahb *ahb) { - return l2ab_restore_hicr78(to_l2ab(ahb)); + return l2ab_restore_hicr78(to_l2ab(ahb)); } static int l2ab_driver_reinit(struct ahb *ahb) { - return l2ab_save_hicr78(to_l2ab(ahb)); + return l2ab_save_hicr78(to_l2ab(ahb)); } diff --git a/src/bridge/l2a.h b/src/bridge/l2a.h index 9ed759e..601169e 100644 --- a/src/bridge/l2a.h +++ b/src/bridge/l2a.h @@ -12,13 +12,13 @@ #include struct l2ab { - struct ahb ahb; - struct lpc fw; - struct ilpcb ilpcb; - uint32_t phys; - size_t len; - uint32_t restore7; - uint32_t restore8; + struct ahb ahb; + struct lpc fw; + struct ilpcb ilpcb; + uint32_t phys; + size_t len; + uint32_t restore7; + uint32_t restore8; }; int l2ab_init(struct l2ab *ctx); @@ -28,7 +28,7 @@ int64_t l2ab_map(struct l2ab *ctx, uint32_t phys, size_t len); static inline struct ahb *l2ab_as_ahb(struct l2ab *ctx) { - return &ctx->ahb; + return &ctx->ahb; } ssize_t l2ab_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len); diff --git a/src/bridge/meson.build b/src/bridge/meson.build index f68654a..5c4b7d3 100644 --- a/src/bridge/meson.build +++ b/src/bridge/meson.build @@ -1,5 +1 @@ -src += files('debug.c', - 'devmem.c', - 'ilpc.c', - 'l2a.c', - 'p2a.c') +src += files('debug.c', 'devmem.c', 'ilpc.c', 'l2a.c', 'p2a.c') diff --git a/src/bridge/p2a.c b/src/bridge/p2a.c index 3903565..1e07f4f 100644 --- a/src/bridge/p2a.c +++ b/src/bridge/p2a.c @@ -25,188 +25,190 @@ #include #include -#define AST_MMIO_BAR 1 -#define AST_MMIO_LEN (128 * 1024) -#define P2AB_PKR 0xf000 -#define P2AB_RBAR 0xf004 -#define P2AB_RBAR_REMAP_MASK 0xffff0000 -#define P2AB_WINDOW_BASE 0x10000 -#define P2AB_WINDOW_LEN 0x10000 +#define AST_MMIO_BAR 1 +#define AST_MMIO_LEN (128 * 1024) +#define P2AB_PKR 0xf000 +#define P2AB_RBAR 0xf004 +#define P2AB_RBAR_REMAP_MASK 0xffff0000 +#define P2AB_WINDOW_BASE 0x10000 +#define P2AB_WINDOW_LEN 0x10000 #define to_p2ab(ahb) container_of(ahb, struct p2ab, ahb) static int __p2ab_readl(struct p2ab *ctx, size_t addr, uint32_t *val) { - assert(addr < (AST_MMIO_LEN - sizeof(val) + 1)); - assert(!(addr & (sizeof(val) - 1))); + assert(addr < (AST_MMIO_LEN - sizeof(val) + 1)); + assert(!(addr & (sizeof(val) - 1))); - *val = le32toh(*((volatile uint32_t *)(ctx->mmio + addr))); + *val = le32toh(*((volatile uint32_t *)(ctx->mmio + addr))); - iob(); + iob(); - return 0; + return 0; } static int __p2ab_writel(struct p2ab *ctx, size_t addr, uint32_t val) { - assert(addr < (AST_MMIO_LEN - sizeof(val) + 1)); - assert(!(addr & (sizeof(val) - 1))); + assert(addr < (AST_MMIO_LEN - sizeof(val) + 1)); + assert(!(addr & (sizeof(val) - 1))); - *((uint32_t *)(ctx->mmio + addr)) = htole32(val); + *((uint32_t *)(ctx->mmio + addr)) = htole32(val); - iob(); + iob(); - return 0; + return 0; } static inline int p2ab_unlock(struct p2ab *ctx) { - int rc; + int rc; - rc = __p2ab_writel(ctx, P2AB_PKR, 1); + rc = __p2ab_writel(ctx, P2AB_PKR, 1); - return rc; + return rc; } static inline int p2ab_lock(struct p2ab *ctx) { - int rc; + int rc; - rc = __p2ab_writel(ctx, P2AB_PKR, 0); + rc = __p2ab_writel(ctx, P2AB_PKR, 0); - return rc; + return rc; } int p2ab_probe(struct p2ab *ctx) { - int64_t rc; + int64_t rc; - logd("Probing %s\n", ctx->ahb.drv->name); + logd("Probing %s\n", ctx->ahb.drv->name); - rc = rev_probe(p2ab_as_ahb(ctx)); - if (rc < 0) - return rc; + rc = rev_probe(p2ab_as_ahb(ctx)); + if (rc < 0) + return rc; - return rc < 0 ? rc : 1; + return rc < 0 ? rc : 1; } int64_t p2ab_map(struct p2ab *ctx, uint32_t phys, size_t len __unused) { - uint32_t rbar; - uint32_t offset; - int64_t rc; + uint32_t rbar; + uint32_t offset; + int64_t rc; - rbar = phys & P2AB_RBAR_REMAP_MASK; - offset = phys & ~P2AB_RBAR_REMAP_MASK; + rbar = phys & P2AB_RBAR_REMAP_MASK; + offset = phys & ~P2AB_RBAR_REMAP_MASK; - if (ctx->rbar == rbar) - return offset; + if (ctx->rbar == rbar) + return offset; - rc = __p2ab_writel(ctx, P2AB_RBAR, rbar); + rc = __p2ab_writel(ctx, P2AB_RBAR, rbar); - if (rc < 0) - return rc; + if (rc < 0) + return rc; - ctx->rbar = rbar; + ctx->rbar = rbar; - return offset; + return offset; } ssize_t p2ab_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len) { - struct p2ab *ctx = to_p2ab(ahb); - size_t remaining = len; - size_t ingress; - int64_t rc; - - if (len > SSIZE_MAX) { - return -1; - } - - do { - ingress = remaining > P2AB_WINDOW_LEN ? P2AB_WINDOW_LEN : remaining; - - rc = p2ab_map(ctx, phys, ingress); - if (rc < 0) - return -1; - - mmio_memcpy(buf, (ctx->mmio + P2AB_WINDOW_BASE + rc), ingress); - phys += ingress; - buf += ingress; - remaining -= ingress; - } while (remaining); - - return len; + struct p2ab *ctx = to_p2ab(ahb); + size_t remaining = len; + size_t ingress; + int64_t rc; + + if (len > SSIZE_MAX) { + return -1; + } + + do { + ingress = remaining > P2AB_WINDOW_LEN ? P2AB_WINDOW_LEN : + remaining; + + rc = p2ab_map(ctx, phys, ingress); + if (rc < 0) + return -1; + + mmio_memcpy(buf, (ctx->mmio + P2AB_WINDOW_BASE + rc), ingress); + phys += ingress; + buf += ingress; + remaining -= ingress; + } while (remaining); + + return len; } ssize_t p2ab_write(struct ahb *ahb, uint32_t phys, const void *buf, size_t len) { - struct p2ab *ctx = to_p2ab(ahb); - size_t remaining = len; - size_t egress; - int64_t rc; - - if (len > SSIZE_MAX) { - return -1; - } - - do { - egress = remaining > P2AB_WINDOW_LEN ? P2AB_WINDOW_LEN : remaining; - - rc = p2ab_map(ctx, phys, egress); - if (rc < 0) - return -1; - - mmio_memcpy((ctx->mmio + P2AB_WINDOW_BASE + rc), buf, egress); - phys += egress; - buf += egress; - remaining -= egress; - } while (remaining); - - return len; + struct p2ab *ctx = to_p2ab(ahb); + size_t remaining = len; + size_t egress; + int64_t rc; + + if (len > SSIZE_MAX) { + return -1; + } + + do { + egress = remaining > P2AB_WINDOW_LEN ? P2AB_WINDOW_LEN : + remaining; + + rc = p2ab_map(ctx, phys, egress); + if (rc < 0) + return -1; + + mmio_memcpy((ctx->mmio + P2AB_WINDOW_BASE + rc), buf, egress); + phys += egress; + buf += egress; + remaining -= egress; + } while (remaining); + + return len; } int p2ab_readl(struct ahb *ahb, uint32_t phys, uint32_t *val) { - struct p2ab *ctx = to_p2ab(ahb); - uint32_t le; - ssize_t rc; + struct p2ab *ctx = to_p2ab(ahb); + uint32_t le; + ssize_t rc; - if (phys & 0x3) - return -EINVAL; + if (phys & 0x3) + return -EINVAL; - rc = p2ab_map(ctx, phys, sizeof(*val)); - if (rc < 0) - return rc; + rc = p2ab_map(ctx, phys, sizeof(*val)); + if (rc < 0) + return rc; - le = *(uint32_t *)(ctx->mmio + P2AB_WINDOW_BASE + rc); + le = *(uint32_t *)(ctx->mmio + P2AB_WINDOW_BASE + rc); - *val = le32toh(le); + *val = le32toh(le); - return 0; + return 0; } int p2ab_writel(struct ahb *ahb, uint32_t phys, uint32_t val) { - struct p2ab *ctx = to_p2ab(ahb); - int rc; + struct p2ab *ctx = to_p2ab(ahb); + int rc; - val = htole32(val); + val = htole32(val); - rc = p2ab_map(ctx, phys, sizeof(val)); - if (rc < 0) - return rc; + rc = p2ab_map(ctx, phys, sizeof(val)); + if (rc < 0) + return rc; - *(uint32_t *)(ctx->mmio + P2AB_WINDOW_BASE + rc) = val; + *(uint32_t *)(ctx->mmio + P2AB_WINDOW_BASE + rc) = val; - return 0; + return 0; } static const struct ahb_ops p2ab_ahb_ops = { - .read = p2ab_read, - .write = p2ab_write, - .readl = p2ab_readl, - .writel = p2ab_writel, + .read = p2ab_read, + .write = p2ab_write, + .readl = p2ab_readl, + .writel = p2ab_writel, }; static struct ahb *p2ab_driver_probe(struct connection_args *connection); @@ -214,112 +216,112 @@ static int p2ab_driver_reinit(struct ahb *ahb); static void p2ab_driver_destroy(struct ahb *ahb); static struct bridge_driver p2ab_driver = { - .name = "p2a", - .probe = p2ab_driver_probe, - .reinit = p2ab_driver_reinit, - .destroy = p2ab_driver_destroy, + .name = "p2a", + .probe = p2ab_driver_probe, + .reinit = p2ab_driver_reinit, + .destroy = p2ab_driver_destroy, }; REGISTER_BRIDGE_DRIVER(p2ab_driver); int p2ab_init(struct p2ab *ctx, uint16_t vid, uint16_t did) { - int rc; + int rc; - rc = pci_open(vid, did, AST_MMIO_BAR); - if (rc < 0) - return rc; + rc = pci_open(vid, did, AST_MMIO_BAR); + if (rc < 0) + return rc; - ctx->res = rc; - ctx->mmio = mmap(0, AST_MMIO_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, - ctx->res, 0); - if (ctx->mmio == MAP_FAILED) { - rc = -errno; - goto cleanup_pci; - } + ctx->res = rc; + ctx->mmio = mmap(0, AST_MMIO_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, + ctx->res, 0); + if (ctx->mmio == MAP_FAILED) { + rc = -errno; + goto cleanup_pci; + } - /* ensure the HW and SW rbar values are in sync */ - ctx->rbar = 0; - __p2ab_writel(ctx, P2AB_RBAR, ctx->rbar); + /* ensure the HW and SW rbar values are in sync */ + ctx->rbar = 0; + __p2ab_writel(ctx, P2AB_RBAR, ctx->rbar); - if ((rc = p2ab_unlock(ctx)) < 0) - goto cleanup_mmap; + if ((rc = p2ab_unlock(ctx)) < 0) + goto cleanup_mmap; - ahb_init_ops(&ctx->ahb, &p2ab_driver, &p2ab_ahb_ops); + ahb_init_ops(&ctx->ahb, &p2ab_driver, &p2ab_ahb_ops); - return 0; + return 0; cleanup_mmap: - munmap(ctx->mmio, AST_MMIO_LEN); + munmap(ctx->mmio, AST_MMIO_LEN); cleanup_pci: - pci_close(ctx->res); + pci_close(ctx->res); - return rc; + return rc; } int p2ab_destroy(struct p2ab *ctx) { - int rc; + int rc; - rc = p2ab_lock(ctx); - if (rc < 0) - return rc; + rc = p2ab_lock(ctx); + if (rc < 0) + return rc; - rc = munmap(ctx->mmio, AST_MMIO_LEN); - if (rc == -1) - return -errno; + rc = munmap(ctx->mmio, AST_MMIO_LEN); + if (rc == -1) + return -errno; - return pci_close(ctx->res); + return pci_close(ctx->res); } static struct ahb * p2ab_driver_probe(struct connection_args *connection __unused) { - struct p2ab *ctx; - int rc; + struct p2ab *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return NULL; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return NULL; + } - if ((rc = p2ab_init(ctx, AST_PCI_VID, AST_PCI_DID_VGA)) < 0) { - logd("Failed to initialise P2A bridge: %d\n", rc); - goto cleanup_ctx; - } + if ((rc = p2ab_init(ctx, AST_PCI_VID, AST_PCI_DID_VGA)) < 0) { + logd("Failed to initialise P2A bridge: %d\n", rc); + goto cleanup_ctx; + } - if ((rc = p2ab_probe(ctx)) < 0) { - logd("Failed P2A probe: %d\n", rc); - goto destroy_ctx; - } + if ((rc = p2ab_probe(ctx)) < 0) { + logd("Failed P2A probe: %d\n", rc); + goto destroy_ctx; + } - return p2ab_as_ahb(ctx); + return p2ab_as_ahb(ctx); destroy_ctx: - p2ab_destroy(ctx); + p2ab_destroy(ctx); cleanup_ctx: - free(ctx); + free(ctx); - return NULL; + return NULL; } static int p2ab_driver_reinit(struct ahb *ahb) { - struct p2ab *ctx = to_p2ab(ahb); + struct p2ab *ctx = to_p2ab(ahb); - /* Update the software cache with the hardware state */ - return __p2ab_readl(ctx, P2AB_RBAR, &ctx->rbar); + /* Update the software cache with the hardware state */ + return __p2ab_readl(ctx, P2AB_RBAR, &ctx->rbar); } static void p2ab_driver_destroy(struct ahb *ahb) { - struct p2ab *ctx = to_p2ab(ahb); - int rc; + struct p2ab *ctx = to_p2ab(ahb); + int rc; - if ((rc = p2ab_destroy(ctx)) < 0) { - loge("Failed to destroy P2A bridge: %d\n", rc); - } + if ((rc = p2ab_destroy(ctx)) < 0) { + loge("Failed to destroy P2A bridge: %d\n", rc); + } - free(ctx); + free(ctx); } diff --git a/src/bridge/p2a.h b/src/bridge/p2a.h index 3f0eb56..c698037 100644 --- a/src/bridge/p2a.h +++ b/src/bridge/p2a.h @@ -9,15 +9,15 @@ #include #include -#define AST_PCI_VID 0x1a03 +#define AST_PCI_VID 0x1a03 #define AST_PCI_DID_VGA 0x2000 #define AST_PCI_DID_BMC 0x2402 struct p2ab { - struct ahb ahb; - int res; - void *mmio; - uint32_t rbar; + struct ahb ahb; + int res; + void *mmio; + uint32_t rbar; }; int p2ab_init(struct p2ab *p2ab, uint16_t vid, uint16_t did); @@ -28,7 +28,7 @@ int64_t p2ab_map(struct p2ab *p2ab, uint32_t phys, size_t len); static inline struct ahb *p2ab_as_ahb(struct p2ab *ctx) { - return &ctx->ahb; + return &ctx->ahb; } ssize_t p2ab_read(struct ahb *ahb, uint32_t phys, void *buf, size_t len); diff --git a/src/ccan/meson.build b/src/ccan/meson.build index 8b2ae52..6ad7281 100644 --- a/src/ccan/meson.build +++ b/src/ccan/meson.build @@ -1,12 +1,14 @@ -libccan = static_library('ccan', - implicit_include_directories: false, - include_directories: incdirs, - install: false, - sources: [ - 'autodata/autodata.c', - 'list/list.c', - 'noerr/noerr.c', - 'ptr_valid/ptr_valid.c', - 'str/debug.c', - 'str/str.c' - ]) +libccan = static_library( + 'ccan', + implicit_include_directories: false, + include_directories: incdirs, + install: false, + sources: [ + 'autodata/autodata.c', + 'list/list.c', + 'noerr/noerr.c', + 'ptr_valid/ptr_valid.c', + 'str/debug.c', + 'str/str.c', + ], +) diff --git a/src/cmd.h b/src/cmd.h index 535b4f7..e26fb50 100644 --- a/src/cmd.h +++ b/src/cmd.h @@ -15,9 +15,9 @@ #include struct cmd { - const char *name; - const char *description; - int (*fn)(int argc, char **argv); + const char *name; + const char *description; + int (*fn)(int argc, char **argv); }; AUTODATA_TYPE(cmds, struct cmd); @@ -25,10 +25,10 @@ AUTODATA_TYPE(cmds, struct cmd); static inline int cmd_cmp(const void *a, const void *b) { - const struct cmd * const *acmd = a; - const struct cmd * const *bcmd = b; + const struct cmd *const *acmd = a; + const struct cmd *const *bcmd = b; - return strcmp((*acmd)->name, (*bcmd)->name); + return strcmp((*acmd)->name, (*bcmd)->name); } /** @@ -40,21 +40,24 @@ static inline int cmd_cmp(const void *a, const void *b) * @param arg The input value */ static inline void parse_mem_arg(struct argp_state *state, const char *label, - unsigned long *out, const char *arg) + unsigned long *out, const char *arg) { - char *endp; - errno = 0; - *out = strtoul(arg, &endp, 0); - if (*out == ULONG_MAX && errno) { - argp_error(state, "Failed to parse %s '%s': %s", label, arg, strerror(errno)); + char *endp; + errno = 0; + *out = strtoul(arg, &endp, 0); + if (*out == ULONG_MAX && errno) { + argp_error(state, "Failed to parse %s '%s': %s", label, arg, + strerror(errno)); #if ULONG_MAX > UINT32_MAX - } else if (*out > UINT32_MAX) { - argp_error(state, "Failed to parse %s: '%s' exceeds address space", label, arg); + } else if (*out > UINT32_MAX) { + argp_error(state, + "Failed to parse %s: '%s' exceeds address space", + label, arg); #endif - } + } - if (arg == endp || *endp) - argp_error(state, "Invalid %s format: '%s'", label, arg); + if (arg == endp || *endp) + argp_error(state, "Invalid %s format: '%s'", label, arg); } /** @@ -73,54 +76,56 @@ static inline void parse_mem_arg(struct argp_state *state, const char *label, * @param args The connection arguments struct that should be modified. */ static inline int cmd_parse_via(int argi, struct argp_state *state, - struct connection_args *args) + struct connection_args *args) { - int rc; - logt("via parse: argi = %d, state->argc = %d\n", argi, state->argc); - if (argi >= state->argc) { - loge("Provided argi >= argp state count. This is liekly a bug!\n"); - return -EINVAL; - } - - /* global argc - already processed arguments - 1 (for the `via` word) */ - int argc = state->argc - argi - 1; - - /* Preflight validation if argc is either 1, 2 or 5 */ - if (argc != 1 && argc != 2 && argc != 6) { - loge("via arguments must be either 1, 2 or 5\n"); - return -EINVAL; - } - - /* Resolve bridge driver to check if it even exists and bind it */ - rc = get_bridge_driver(state->argv[argi + 1], &args->bridge_driver); - if (rc != 0) { - loge("Couldn't find provided bridge driver '%s'\n", state->argv[argi + 1]); - return rc; - } - - /* Early abort if driver is only set, but fail when path_required is true */ - if (argc == 1) { - if (args->bridge_driver->path_required) { - loge("Bridge driver '%s' requires an interface path\n", args->bridge_driver->name); - return -EINVAL; - } - return 0; - } - - args->interface = state->argv[argi + 2]; - - /* Early abort if interface is set */ - if (argc == 2) - return 0; - - logt("via parse: Detected more than one argument!\n"); - args->ip = state->argv[argi + 3]; - args->port = strtoul(state->argv[argi + 4], NULL, 0); - args->username = state->argv[argi + 5]; - args->password = state->argv[argi + 6]; - args->internet_args = true; - - return 0; + int rc; + logt("via parse: argi = %d, state->argc = %d\n", argi, state->argc); + if (argi >= state->argc) { + loge("Provided argi >= argp state count. This is liekly a bug!\n"); + return -EINVAL; + } + + /* global argc - already processed arguments - 1 (for the `via` word) */ + int argc = state->argc - argi - 1; + + /* Preflight validation if argc is either 1, 2 or 5 */ + if (argc != 1 && argc != 2 && argc != 6) { + loge("via arguments must be either 1, 2 or 5\n"); + return -EINVAL; + } + + /* Resolve bridge driver to check if it even exists and bind it */ + rc = get_bridge_driver(state->argv[argi + 1], &args->bridge_driver); + if (rc != 0) { + loge("Couldn't find provided bridge driver '%s'\n", + state->argv[argi + 1]); + return rc; + } + + /* Early abort if driver is only set, but fail when path_required is true */ + if (argc == 1) { + if (args->bridge_driver->path_required) { + loge("Bridge driver '%s' requires an interface path\n", + args->bridge_driver->name); + return -EINVAL; + } + return 0; + } + + args->interface = state->argv[argi + 2]; + + /* Early abort if interface is set */ + if (argc == 2) + return 0; + + logt("via parse: Detected more than one argument!\n"); + args->ip = state->argv[argi + 3]; + args->port = strtoul(state->argv[argi + 4], NULL, 0); + args->username = state->argv[argi + 5]; + args->password = state->argv[argi + 6]; + args->internet_args = true; + + return 0; } #endif diff --git a/src/cmd/console.c b/src/cmd/console.c index aa891d8..cd3166b 100644 --- a/src/cmd/console.c +++ b/src/cmd/console.c @@ -20,226 +20,299 @@ #include static char cmd_console_args_doc[] = "HOST_UART BMC_UART BAUD USER PASSWORD"; -static char cmd_console_doc[] = - "\n" - "Console command" - "\v" - "Supported host consoles:\n" - " uart3\n" - "\n" - "Supported BMC consoles:\n" - " uart2\n"; +static char cmd_console_doc[] = "\n" + "Console command" + "\v" + "Supported host consoles:\n" + " uart3\n" + "\n" + "Supported BMC consoles:\n" + " uart2\n"; struct cmd_console_args { - const char *host_uart; - const char *user; - const char *pass; - struct connection_args connection; - int baud; - /* 4 bytes of padding */ + const char *host_uart; + const char *user; + const char *pass; + struct connection_args connection; + int baud; + /* 4 bytes of padding */ }; -static struct argp_option cmd_console_options[] = { - {0} -}; +static struct argp_option cmd_console_options[] = { { 0 } }; static error_t cmd_console_parse_opt(int key, char *arg, - struct argp_state *state) + struct argp_state *state) { - struct cmd_console_args *arguments = state->input; - - switch (key) { - case ARGP_KEY_ARG: - switch (state->arg_num) { - case 0: - if (strcmp("uart3", arg)) - argp_error(state, "Console only supports host on 'uart3'"); - arguments->host_uart = arg; - break; - case 1: - if (strcmp("uart2", arg)) - argp_error(state, "Console only supports BMC on 'uart2'"); - arguments->connection.interface = arg; - break; - case 2: - arguments->baud = atoi(arg); - break; - case 3: - arguments->user = arg; - break; - case 4: - arguments->pass = arg; - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 5) - argp_error(state, "Not enough arguments provided..."); - break; - default: - return ARGP_ERR_UNKNOWN; - } - return 0; + struct cmd_console_args *arguments = state->input; + + switch (key) { + case ARGP_KEY_ARG: + switch (state->arg_num) { + case 0: + if (strcmp("uart3", arg)) + argp_error( + state, + "Console only supports host on 'uart3'"); + arguments->host_uart = arg; + break; + case 1: + if (strcmp("uart2", arg)) + argp_error( + state, + "Console only supports BMC on 'uart2'"); + arguments->connection.interface = arg; + break; + case 2: + arguments->baud = atoi(arg); + break; + case 3: + arguments->user = arg; + break; + case 4: + arguments->pass = arg; + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 5) + argp_error(state, "Not enough arguments provided..."); + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; } static struct argp cmd_console_argp = { - .options = cmd_console_options, - .parser = cmd_console_parse_opt, - .args_doc = cmd_console_args_doc, - .doc = cmd_console_doc, + .options = cmd_console_options, + .parser = cmd_console_parse_opt, + .args_doc = cmd_console_args_doc, + .doc = cmd_console_doc, }; static int do_console(int argc, char **argv) { - struct suart _suart, *suart = &_suart; - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct uart_mux *mux; - struct ahb *ahb; - struct clk *clk; - int cleanup; - int rc; - - struct cmd_console_args arguments = {0}; - rc = argp_parse(&cmd_console_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) { - rc = EXIT_FAILURE; - goto done; - } - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - goto done; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto host_cleanup; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - errno = -rc; - perror("soc_probe"); - goto host_cleanup; - } - - if (!(clk = clk_get(soc))) { - loge("Failed to acquire clock controller, exiting\n"); - goto soc_cleanup; - } - - if (!(mux = uart_mux_get(soc))) { - loge("Failed to acquire UART mux controller, exiting\n"); - goto soc_cleanup; - } - - logi("Enabling UART clocks\n"); - /* Only 3 needs to be enabled as 1 and 2 are "reserved" for the host */ - if ((rc = clk_enable(clk, clk_uart3)) < 0) { - errno = -rc; - perror("clk_enable"); - goto soc_cleanup; - } - - logi("Routing UART3 to UART5\n"); - if ((rc = uart_mux_route(mux, mux_obj_uart3, mux_obj_uart5)) < 0) { - errno = -rc; - perror("uart_mux_route"); - goto soc_cleanup; - } - - logi("Initialising SUART3\n"); - rc = suart_init_defaults(suart, sio_suart3); - if (rc) { errno = -rc; perror("suart_init"); goto mux_restore; } - - logi("Configuring baud rate of 115200 for BMC console\n"); - rc = suart_set_baud(suart, 115200); - if (rc) { errno = -rc; perror("suart_set_baud"); goto suart_cleanup; } - - logi("Starting getty from BMC console\n"); - rc = suart_flush(suart, arguments.user, strlen(arguments.user)); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - rc = suart_flush(suart, "\n", 5); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - sleep(3); - - rc = suart_flush(suart, arguments.pass, strlen(arguments.pass)); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - rc = suart_flush(suart, "\n", 8); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - sleep(5); - - const char *run_getty = "/sbin/agetty -8 -L ttyS1 1200 xterm &\n"; - rc = suart_flush(suart, run_getty, strlen(run_getty)); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - /* We need to wait for the XMIT FIFO to clear before changing the UART - * routing. - * - * TODO: Make this suck less by spinning on THRE - */ - sleep(3); - - logi("Launched getty with: %s", run_getty); - - logi("Routing UARTs to connect UART3 with UART2\n"); - rc = uart_mux_restore(mux); - if (rc) { errno = -rc; perror("uart_mux_restore"); goto suart_cleanup; } - - rc = uart_mux_connect(mux, mux_obj_uart3, mux_obj_uart2); - if (rc) { errno = -rc; perror("uart_mux_connect"); goto suart_cleanup; } - - logi("Setting target baud rate of %d\n", arguments.baud); - rc = suart_set_baud(suart, arguments.baud); - if (rc) { errno = -rc; perror("suart_set_baud"); goto suart_cleanup; } - - rc = suart_flush(suart, "\n", 1); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - sleep(5); - - rc = suart_flush(suart, arguments.user, strlen(arguments.user)); /* username */ - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - rc = suart_flush(suart, "\n", 5); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - sleep(3); - - rc = suart_flush(suart, arguments.pass, strlen(arguments.pass)); /* password */ - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - rc = suart_flush(suart, "\n", 1); - if (rc) { errno = -rc; perror("suart_flush"); goto suart_cleanup; } - - rc = suart_run(suart, 0, 1); - if (rc) { errno = -rc; perror("suart_run"); } + struct suart _suart, *suart = &_suart; + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct uart_mux *mux; + struct ahb *ahb; + struct clk *clk; + int cleanup; + int rc; + + struct cmd_console_args arguments = { 0 }; + rc = argp_parse(&cmd_console_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) { + rc = EXIT_FAILURE; + goto done; + } + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + goto done; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto host_cleanup; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + errno = -rc; + perror("soc_probe"); + goto host_cleanup; + } + + if (!(clk = clk_get(soc))) { + loge("Failed to acquire clock controller, exiting\n"); + goto soc_cleanup; + } + + if (!(mux = uart_mux_get(soc))) { + loge("Failed to acquire UART mux controller, exiting\n"); + goto soc_cleanup; + } + + logi("Enabling UART clocks\n"); + /* Only 3 needs to be enabled as 1 and 2 are "reserved" for the host */ + if ((rc = clk_enable(clk, clk_uart3)) < 0) { + errno = -rc; + perror("clk_enable"); + goto soc_cleanup; + } + + logi("Routing UART3 to UART5\n"); + if ((rc = uart_mux_route(mux, mux_obj_uart3, mux_obj_uart5)) < 0) { + errno = -rc; + perror("uart_mux_route"); + goto soc_cleanup; + } + + logi("Initialising SUART3\n"); + rc = suart_init_defaults(suart, sio_suart3); + if (rc) { + errno = -rc; + perror("suart_init"); + goto mux_restore; + } + + logi("Configuring baud rate of 115200 for BMC console\n"); + rc = suart_set_baud(suart, 115200); + if (rc) { + errno = -rc; + perror("suart_set_baud"); + goto suart_cleanup; + } + + logi("Starting getty from BMC console\n"); + rc = suart_flush(suart, arguments.user, strlen(arguments.user)); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + rc = suart_flush(suart, "\n", 5); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + sleep(3); + + rc = suart_flush(suart, arguments.pass, strlen(arguments.pass)); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + rc = suart_flush(suart, "\n", 8); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + sleep(5); + + const char *run_getty = "/sbin/agetty -8 -L ttyS1 1200 xterm &\n"; + rc = suart_flush(suart, run_getty, strlen(run_getty)); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + /* We need to wait for the XMIT FIFO to clear before changing the UART + * routing. + * + * TODO: Make this suck less by spinning on THRE + */ + sleep(3); + + logi("Launched getty with: %s", run_getty); + + logi("Routing UARTs to connect UART3 with UART2\n"); + rc = uart_mux_restore(mux); + if (rc) { + errno = -rc; + perror("uart_mux_restore"); + goto suart_cleanup; + } + + rc = uart_mux_connect(mux, mux_obj_uart3, mux_obj_uart2); + if (rc) { + errno = -rc; + perror("uart_mux_connect"); + goto suart_cleanup; + } + + logi("Setting target baud rate of %d\n", arguments.baud); + rc = suart_set_baud(suart, arguments.baud); + if (rc) { + errno = -rc; + perror("suart_set_baud"); + goto suart_cleanup; + } + + rc = suart_flush(suart, "\n", 1); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + sleep(5); + + rc = suart_flush(suart, arguments.user, + strlen(arguments.user)); /* username */ + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + rc = suart_flush(suart, "\n", 5); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + sleep(3); + + rc = suart_flush(suart, arguments.pass, + strlen(arguments.pass)); /* password */ + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + rc = suart_flush(suart, "\n", 1); + if (rc) { + errno = -rc; + perror("suart_flush"); + goto suart_cleanup; + } + + rc = suart_run(suart, 0, 1); + if (rc) { + errno = -rc; + perror("suart_run"); + } suart_cleanup: - cleanup = suart_destroy(suart); - if (cleanup) { errno = -cleanup; perror("suart_destroy"); } + cleanup = suart_destroy(suart); + if (cleanup) { + errno = -cleanup; + perror("suart_destroy"); + } mux_restore: - cleanup = uart_mux_restore(mux); - if (cleanup) { errno = -cleanup; perror("suart_destroy"); } + cleanup = uart_mux_restore(mux); + if (cleanup) { + errno = -cleanup; + perror("suart_destroy"); + } soc_cleanup: - soc_destroy(soc); + soc_destroy(soc); host_cleanup: - host_destroy(host); + host_destroy(host); done: - return rc; + return rc; } static const struct cmd console_cmd = { - .name = "console", - .description = "Start a getty on the BMC console", - .fn = do_console, + .name = "console", + .description = "Start a getty on the BMC console", + .fn = do_console, }; REGISTER_CMD(console_cmd); diff --git a/src/cmd/coprocessor.c b/src/cmd/coprocessor.c index 4db5ce5..4b2bb01 100644 --- a/src/cmd/coprocessor.c +++ b/src/cmd/coprocessor.c @@ -21,338 +21,359 @@ #include #define COPROC_CACHED_MEM_SIZE (16 * 1024 * 1024) -#define COPROC_TOTAL_MEM_SIZE (32 * 1024 * 1024) - -#define SCU_COPROC_CTRL 0xa00 -#define SCU_COPROC_CTRL_RESET_ASSERT BIT(1) -#define SCU_COPROC_CTRL_EN BIT(0) - -#define SCU_COPROC_MEM_BASE 0xa04 -#define SCU_COPROC_IMEM_LIMIT 0xa08 -#define SCU_COPROC_DMEM_LIMIT 0xa0c -#define SCU_COPROC_CACHE_RANGE 0xa40 -#define SCU_COPROC_CACHE_1ST_16MB_EN BIT(0) -#define SCU_COPROC_CACHE_FUNC 0xa48 -#define SCU_COPROC_CACHE_EN BIT(0) - -static char cmd_coprocessor_args_doc[] = " [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; -static char cmd_coprocessor_doc[] = - "\n" - "Coprocessor command" - "\v" - "Supported commands:\n" - " run Run the coprocessor\n" - " stop Stop the coprocessor\n"; +#define COPROC_TOTAL_MEM_SIZE (32 * 1024 * 1024) + +#define SCU_COPROC_CTRL 0xa00 +#define SCU_COPROC_CTRL_RESET_ASSERT BIT(1) +#define SCU_COPROC_CTRL_EN BIT(0) + +#define SCU_COPROC_MEM_BASE 0xa04 +#define SCU_COPROC_IMEM_LIMIT 0xa08 +#define SCU_COPROC_DMEM_LIMIT 0xa0c +#define SCU_COPROC_CACHE_RANGE 0xa40 +#define SCU_COPROC_CACHE_1ST_16MB_EN BIT(0) +#define SCU_COPROC_CACHE_FUNC 0xa48 +#define SCU_COPROC_CACHE_EN BIT(0) + +static char cmd_coprocessor_args_doc[] = + " [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; +static char cmd_coprocessor_doc[] = "\n" + "Coprocessor command" + "\v" + "Supported commands:\n" + " run Run the coprocessor\n" + " stop Stop the coprocessor\n"; enum cmd_coprocessor_mode { - run, - stop, + run, + stop, }; static struct argp_option cmd_coprocessor_options[] = { - {0}, + { 0 }, }; struct cmd_coprocessor_args { - unsigned long mem_base; - unsigned long mem_size; - struct connection_args connection; - enum cmd_coprocessor_mode mode; - /* padding 4 bytes */ + unsigned long mem_base; + unsigned long mem_size; + struct connection_args connection; + enum cmd_coprocessor_mode mode; + /* padding 4 bytes */ }; static error_t cmd_coprocessor_parse_opt(int key, char *arg, - struct argp_state *state) + struct argp_state *state) { - struct cmd_coprocessor_args *arguments = state->input; - int rc; - - switch (key) { - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - if (state->arg_num == 0) { - if (strcmp("run", arg) && strcmp("stop", arg)) - argp_error(state, "Invalid command '%s'", arg); - - if (!strcmp(arg, "run")) - arguments->mode = run; - else - arguments->mode = stop; - break; - } - - /* If mode is not run, skip any further args */ - if (arguments->mode != run) - break; - - if (state->arg_num == 1) { - parse_mem_arg(state, "coprocessor RAM base", &arguments->mem_base, arg); - } else if (state->arg_num == 2) { - parse_mem_arg(state, "coprocessor RAM size", &arguments->mem_size, arg); - if (arguments->mem_size != COPROC_TOTAL_MEM_SIZE) - argp_error(state, "Only 32M memory is currently supported for the coprocessor"); - } - - break; - case ARGP_KEY_END: - if (arguments->mode == run && state->arg_num < 3) - argp_error(state, "Not enough arguments for run mode..."); - if (arguments->mode == stop && state->arg_num < 1) - argp_error(state, "Not enough arguments for stop mode..."); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_coprocessor_args *arguments = state->input; + int rc; + + switch (key) { + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + if (state->arg_num == 0) { + if (strcmp("run", arg) && strcmp("stop", arg)) + argp_error(state, "Invalid command '%s'", arg); + + if (!strcmp(arg, "run")) + arguments->mode = run; + else + arguments->mode = stop; + break; + } + + /* If mode is not run, skip any further args */ + if (arguments->mode != run) + break; + + if (state->arg_num == 1) { + parse_mem_arg(state, "coprocessor RAM base", + &arguments->mem_base, arg); + } else if (state->arg_num == 2) { + parse_mem_arg(state, "coprocessor RAM size", + &arguments->mem_size, arg); + if (arguments->mem_size != COPROC_TOTAL_MEM_SIZE) + argp_error( + state, + "Only 32M memory is currently supported for the coprocessor"); + } + + break; + case ARGP_KEY_END: + if (arguments->mode == run && state->arg_num < 3) + argp_error(state, + "Not enough arguments for run mode..."); + if (arguments->mode == stop && state->arg_num < 1) + argp_error(state, + "Not enough arguments for stop mode..."); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_coprocessor_argp = { - .options = cmd_coprocessor_options, - .parser = cmd_coprocessor_parse_opt, - .args_doc = cmd_coprocessor_args_doc, - .doc = cmd_coprocessor_doc, + .options = cmd_coprocessor_options, + .parser = cmd_coprocessor_parse_opt, + .args_doc = cmd_coprocessor_args_doc, + .doc = cmd_coprocessor_doc, }; static int cmd_coprocessor_run(struct cmd_coprocessor_args *arguments) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct soc_region dram; - struct sdmc *sdmc; - struct ahb *ahb; - struct scu *scu; - ssize_t src; - int rc; - - if ((rc = host_init(host, &arguments->connection)) < 0) { - loge("Failed to initialise host interface: %d\n", rc); - return EXIT_FAILURE; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - loge("Failed to probe SoC: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if (soc_generation(soc) != ast_g6) { - loge("We currently only support the AST2600-series coprocessor\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if (!(sdmc = sdmc_get(soc))) { - loge("Failed to acquire SDRAM memory controller\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if ((rc = sdmc_get_dram(sdmc, &dram))) { - loge("Failed to locate DRAM: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_soc; - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct soc_region dram; + struct sdmc *sdmc; + struct ahb *ahb; + struct scu *scu; + ssize_t src; + int rc; + + if ((rc = host_init(host, &arguments->connection)) < 0) { + loge("Failed to initialise host interface: %d\n", rc); + return EXIT_FAILURE; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + loge("Failed to probe SoC: %d\n", rc); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if (soc_generation(soc) != ast_g6) { + loge("We currently only support the AST2600-series coprocessor\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if (!(sdmc = sdmc_get(soc))) { + loge("Failed to acquire SDRAM memory controller\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if ((rc = sdmc_get_dram(sdmc, &dram))) { + loge("Failed to locate DRAM: %d\n", rc); + rc = EXIT_FAILURE; + goto cleanup_soc; + } #if ULONG_MAX > UINT32_MAX - if (arguments->mem_base > UINT32_MAX) { - loge("Provided RAM base 0x%ux exceeds SoC physical address space\n", arguments->mem_base); - rc = EXIT_FAILURE; - goto cleanup_soc; - } + if (arguments->mem_base > UINT32_MAX) { + loge("Provided RAM base 0x%ux exceeds SoC physical address space\n", + arguments->mem_base); + rc = EXIT_FAILURE; + goto cleanup_soc; + } #endif - if (((arguments->mem_base + arguments->mem_size) & UINT32_MAX) < arguments->mem_base) { - loge("Invalid RAM region provided for coprocessor\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if (arguments->mem_base < dram.start || (arguments->mem_base + arguments->mem_size) > (dram.start + dram.length)) { - loge("Ill-formed RAM region provided for coprocessor\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if (!(scu = scu_get(soc))) { - loge("Failed to acquire SCU driver\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - /* 4.1.2 SSP Cache Programming Procedure - * - * 'AST2600 SECONDARY SERVICE PROCESSOR v0.1f.pdf' - */ - /* 1. */ - if ((rc = scu_writel(scu, SCU_COPROC_CTRL, 0)) < 0) { - loge("Failed to disable coprocoessor: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - /* 2. */ - if ((rc = scu_writel(scu, SCU_COPROC_CTRL, SCU_COPROC_CTRL_RESET_ASSERT)) < 0) { - loge("Failed to assert the coprocessor reset: %d", rc); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - /* 3. */ - if ((src = soc_siphon_in(soc, arguments->mem_base, arguments->mem_size, STDIN_FILENO)) < 0) { - loge("Failed to load coprocessor firmware to provided region: %d\n", src); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - /* 4. */ - if (scu_writel(scu, SCU_COPROC_MEM_BASE, arguments->mem_base) || - /* 5. */ - scu_writel(scu, SCU_COPROC_IMEM_LIMIT, - arguments->mem_base + COPROC_CACHED_MEM_SIZE) || - /* 6. */ - scu_writel(scu, SCU_COPROC_DMEM_LIMIT, arguments->mem_base + arguments->mem_size) || - /* 7. */ - scu_writel(scu, SCU_COPROC_CACHE_RANGE, SCU_COPROC_CACHE_1ST_16MB_EN) || - /* 8. */ - scu_writel(scu, SCU_COPROC_CACHE_FUNC, SCU_COPROC_CACHE_EN)) { - loge("Failed to configure coprocessor control registers\n"); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - if (usleep(1000) == -1) { - loge("Coprocessor reset pre-delay failed: %s", strerror(errno)); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - /* 9. */ - if ((rc = scu_writel(scu, SCU_COPROC_CTRL, 0)) < 0) { - loge("Failed to disable coprocessor: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - if (usleep(1000) == -1) { - loge("Coprocessor reset post-delay failed: %s", strerror(errno)); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - /* 10. */ - if ((rc = scu_writel(scu, SCU_COPROC_CTRL, SCU_COPROC_CTRL_EN)) < 0) { - loge("Failed to start coprocessor: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_scu; - } - - rc = EXIT_SUCCESS; + if (((arguments->mem_base + arguments->mem_size) & UINT32_MAX) < + arguments->mem_base) { + loge("Invalid RAM region provided for coprocessor\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if (arguments->mem_base < dram.start || + (arguments->mem_base + arguments->mem_size) > + (dram.start + dram.length)) { + loge("Ill-formed RAM region provided for coprocessor\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if (!(scu = scu_get(soc))) { + loge("Failed to acquire SCU driver\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + /* 4.1.2 SSP Cache Programming Procedure + * + * 'AST2600 SECONDARY SERVICE PROCESSOR v0.1f.pdf' + */ + /* 1. */ + if ((rc = scu_writel(scu, SCU_COPROC_CTRL, 0)) < 0) { + loge("Failed to disable coprocoessor: %d\n", rc); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + /* 2. */ + if ((rc = scu_writel(scu, SCU_COPROC_CTRL, + SCU_COPROC_CTRL_RESET_ASSERT)) < 0) { + loge("Failed to assert the coprocessor reset: %d", rc); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + /* 3. */ + if ((src = soc_siphon_in(soc, arguments->mem_base, arguments->mem_size, + STDIN_FILENO)) < 0) { + loge("Failed to load coprocessor firmware to provided region: %d\n", + src); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + /* 4. */ + if (scu_writel(scu, SCU_COPROC_MEM_BASE, arguments->mem_base) || + /* 5. */ + scu_writel(scu, SCU_COPROC_IMEM_LIMIT, + arguments->mem_base + COPROC_CACHED_MEM_SIZE) || + /* 6. */ + scu_writel(scu, SCU_COPROC_DMEM_LIMIT, + arguments->mem_base + arguments->mem_size) || + /* 7. */ + scu_writel(scu, SCU_COPROC_CACHE_RANGE, + SCU_COPROC_CACHE_1ST_16MB_EN) || + /* 8. */ + scu_writel(scu, SCU_COPROC_CACHE_FUNC, SCU_COPROC_CACHE_EN)) { + loge("Failed to configure coprocessor control registers\n"); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + if (usleep(1000) == -1) { + loge("Coprocessor reset pre-delay failed: %s", strerror(errno)); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + /* 9. */ + if ((rc = scu_writel(scu, SCU_COPROC_CTRL, 0)) < 0) { + loge("Failed to disable coprocessor: %d\n", rc); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + if (usleep(1000) == -1) { + loge("Coprocessor reset post-delay failed: %s", + strerror(errno)); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + /* 10. */ + if ((rc = scu_writel(scu, SCU_COPROC_CTRL, SCU_COPROC_CTRL_EN)) < 0) { + loge("Failed to start coprocessor: %d\n", rc); + rc = EXIT_FAILURE; + goto cleanup_scu; + } + + rc = EXIT_SUCCESS; cleanup_scu: - scu_put(scu); + scu_put(scu); cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static int cmd_coprocessor_stop(struct cmd_coprocessor_args *arguments) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct ahb *ahb; - struct scu *scu; - int rc; - - if ((rc = host_init(host, &arguments->connection)) < 0) { - loge("Failed to initialise host interface: %d\n", rc); - return EXIT_FAILURE; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - loge("Failed to probe SoC: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if (soc_generation(soc) != ast_g6) { - loge("We currently only support the AST2600-series coprocessor\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if (!(scu = scu_get(soc))) { - loge("Failed to acquire SCU driver\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if ((rc = scu_writel(scu, SCU_COPROC_CTRL, 0)) < 0) { - loge("Failed to disable coprocoessor: %d\n", rc); - rc = EXIT_FAILURE; - } - - scu_put(scu); + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct ahb *ahb; + struct scu *scu; + int rc; + + if ((rc = host_init(host, &arguments->connection)) < 0) { + loge("Failed to initialise host interface: %d\n", rc); + return EXIT_FAILURE; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + loge("Failed to probe SoC: %d\n", rc); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if (soc_generation(soc) != ast_g6) { + loge("We currently only support the AST2600-series coprocessor\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if (!(scu = scu_get(soc))) { + loge("Failed to acquire SCU driver\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if ((rc = scu_writel(scu, SCU_COPROC_CTRL, 0)) < 0) { + loge("Failed to disable coprocoessor: %d\n", rc); + rc = EXIT_FAILURE; + } + + scu_put(scu); cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static int do_coprocessor(int argc, char **argv) { - int rc; - - struct cmd_coprocessor_args arguments = {0}; - rc = argp_parse(&cmd_coprocessor_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) { - rc = EXIT_FAILURE; - goto done; - } - - switch (arguments.mode) { - case run: - rc = cmd_coprocessor_run(&arguments); - break; - case stop: - rc = cmd_coprocessor_stop(&arguments); - break; - } + int rc; + + struct cmd_coprocessor_args arguments = { 0 }; + rc = argp_parse(&cmd_coprocessor_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) { + rc = EXIT_FAILURE; + goto done; + } + + switch (arguments.mode) { + case run: + rc = cmd_coprocessor_run(&arguments); + break; + case stop: + rc = cmd_coprocessor_stop(&arguments); + break; + } done: - return rc; + return rc; } static const struct cmd coprocessor_cmd = { - .name = "coprocessor", - .description = "Do things on the coprocessors of the AST2600", - .fn = do_coprocessor, + .name = "coprocessor", + .description = "Do things on the coprocessors of the AST2600", + .fn = do_coprocessor, }; REGISTER_CMD(coprocessor_cmd); diff --git a/src/cmd/debug.c b/src/cmd/debug.c index 6ee05be..fb6d1b8 100644 --- a/src/cmd/debug.c +++ b/src/cmd/debug.c @@ -14,155 +14,179 @@ #include #include -static char cmd_debug_args_doc[] = " via DRIVER INTERFACE [IP PORT USERNAME PASSWORD]"; -static char cmd_debug_doc[] = - "\n" - "Debug command" - "\v" - "Supported commands:\n" - " read Read data via debug UART\n" - " write Write data via debug UART\n"; +static char cmd_debug_args_doc[] = + " via DRIVER INTERFACE [IP PORT USERNAME PASSWORD]"; +static char cmd_debug_doc[] = "\n" + "Debug command" + "\v" + "Supported commands:\n" + " read Read data via debug UART\n" + " write Write data via debug UART\n"; static struct argp_option cmd_debug_options[] = { - { "force-quit", 'F', 0, 0, "Blindly exit debug mode before entering", 0 }, - {0}, + { "force-quit", 'F', 0, 0, "Blindly exit debug mode before entering", + 0 }, + { 0 }, }; struct cmd_debug_args { - struct connection_args connection; - struct ast_ahb_args ahb_args; - bool force_quit; + struct connection_args connection; + struct ast_ahb_args ahb_args; + bool force_quit; }; static error_t cmd_debug_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_debug_args *arguments = state->input; - int rc; - - switch (key) { - case 'F': - arguments->force_quit = 1; - break; - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - switch (state->arg_num) { - case 0: - if (strcmp("read", arg) && strcmp("write", arg)) - argp_error(state, "Invalid command '%s'", arg); - - if (!strcmp("read", arg)) - arguments->ahb_args.read = true; - break; - case 1: - arguments->ahb_args.address = strtoul(arg, NULL, 0); - break; - case 2: - if (!arguments->ahb_args.read) { - static uint32_t write_val; - write_val = strtoul(arg, NULL, 0); - arguments->ahb_args.write_value = &write_val; - } else { - logd("Found third argument in read mode, ignoring...\n"); - } - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 2) - argp_error(state, "Not enough arguments provided. Need at least an address."); - - if (!arguments->ahb_args.read && state->arg_num < 3) - argp_error(state, "Write mode detected but either no address or value detected"); - - if (arguments->connection.interface == NULL) - argp_error(state, "Debug interface path must be defined"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_debug_args *arguments = state->input; + int rc; + + switch (key) { + case 'F': + arguments->force_quit = 1; + break; + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + switch (state->arg_num) { + case 0: + if (strcmp("read", arg) && strcmp("write", arg)) + argp_error(state, "Invalid command '%s'", arg); + + if (!strcmp("read", arg)) + arguments->ahb_args.read = true; + break; + case 1: + arguments->ahb_args.address = strtoul(arg, NULL, 0); + break; + case 2: + if (!arguments->ahb_args.read) { + static uint32_t write_val; + write_val = strtoul(arg, NULL, 0); + arguments->ahb_args.write_value = &write_val; + } else { + logd("Found third argument in read mode, ignoring...\n"); + } + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 2) + argp_error( + state, + "Not enough arguments provided. Need at least an address."); + + if (!arguments->ahb_args.read && state->arg_num < 3) + argp_error( + state, + "Write mode detected but either no address or value detected"); + + if (arguments->connection.interface == NULL) + argp_error(state, + "Debug interface path must be defined"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_debug_argp = { - .options = cmd_debug_options, - .parser = cmd_debug_parse_opt, - .args_doc = cmd_debug_args_doc, - .doc = cmd_debug_doc, + .options = cmd_debug_options, + .parser = cmd_debug_parse_opt, + .args_doc = cmd_debug_args_doc, + .doc = cmd_debug_doc, }; static int do_debug(int argc, char **argv) { - struct debug _debug, *debug = &_debug; - struct ahb *ahb; - int rc, cleanup; - - /* ./culvert debug read 0x1e6e207c digi,portserver-ts-16 */ - struct cmd_debug_args arguments = {0}; - rc = argp_parse(&cmd_debug_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - /* Ensure that force_quit is passed to debug ctx */ - debug->force_quit = arguments.force_quit; - - logi("Initialising debug interface\n"); - if (!arguments.connection.internet_args) { - /* Local debug interface */ - if ((rc = debug_init(debug, arguments.connection.interface)) < 0) { - loge("Failed to initialise local debug interace on %s: %d\n", - arguments.connection.interface, rc); - return rc; - } - } else { - /* Remote debug interface */ - rc = debug_init(debug, arguments.connection.interface, - arguments.connection.ip, arguments.connection.port, - arguments.connection.username, - arguments.connection.password); - if (rc < 0) { - loge("Failed to initialise remote debug interface: %d\n", rc); - return rc; - } - } - - if (rc < 0) { - errno = -rc; - perror("debug_init"); - return rc; - } - - rc = debug_enter(debug); - if (rc < 0) { errno = -rc; perror("debug_enter"); goto destroy_ctx; } - - ahb = debug_as_ahb(debug); - rc = ast_ahb_access(&arguments.ahb_args, ahb); - if (rc) { - errno = -rc; - perror("ast_ahb_access"); - return rc; - } - - cleanup = debug_exit(debug); - if (cleanup < 0) { errno = -cleanup; perror("debug_exit"); } + struct debug _debug, *debug = &_debug; + struct ahb *ahb; + int rc, cleanup; + + /* ./culvert debug read 0x1e6e207c digi,portserver-ts-16 */ + struct cmd_debug_args arguments = { 0 }; + rc = argp_parse(&cmd_debug_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + /* Ensure that force_quit is passed to debug ctx */ + debug->force_quit = arguments.force_quit; + + logi("Initialising debug interface\n"); + if (!arguments.connection.internet_args) { + /* Local debug interface */ + if ((rc = debug_init(debug, arguments.connection.interface)) < + 0) { + loge("Failed to initialise local debug interace on %s: %d\n", + arguments.connection.interface, rc); + return rc; + } + } else { + /* Remote debug interface */ + rc = debug_init(debug, arguments.connection.interface, + arguments.connection.ip, + arguments.connection.port, + arguments.connection.username, + arguments.connection.password); + if (rc < 0) { + loge("Failed to initialise remote debug interface: %d\n", + rc); + return rc; + } + } + + if (rc < 0) { + errno = -rc; + perror("debug_init"); + return rc; + } + + rc = debug_enter(debug); + if (rc < 0) { + errno = -rc; + perror("debug_enter"); + goto destroy_ctx; + } + + ahb = debug_as_ahb(debug); + rc = ast_ahb_access(&arguments.ahb_args, ahb); + if (rc) { + errno = -rc; + perror("ast_ahb_access"); + return rc; + } + + cleanup = debug_exit(debug); + if (cleanup < 0) { + errno = -cleanup; + perror("debug_exit"); + } destroy_ctx: - logi("Destroying debug interface\n"); - cleanup = debug_destroy(debug); - if (cleanup < 0) { errno = -cleanup; perror("debug_destroy"); } - - return rc; + logi("Destroying debug interface\n"); + cleanup = debug_destroy(debug); + if (cleanup < 0) { + errno = -cleanup; + perror("debug_destroy"); + } + + return rc; } static const struct cmd debug_cmd = { - .name = "debug", - .description = "Read or write 4 bytes of data via the AHB bridge", - .fn = do_debug, + .name = "debug", + .description = "Read or write 4 bytes of data via the AHB bridge", + .fn = do_debug, }; REGISTER_CMD(debug_cmd); diff --git a/src/cmd/devmem.c b/src/cmd/devmem.c index 6aa40cb..bc5df9b 100644 --- a/src/cmd/devmem.c +++ b/src/cmd/devmem.c @@ -13,118 +13,123 @@ #include static char cmd_devmem_args_doc[] = ""; -static char cmd_devmem_doc[] = - "\n" - "Devmem command" - "\v" - "Supported commands:\n" - " read Read data from /dev/mem\n" - " write Write data to /dev/mem\n"; +static char cmd_devmem_doc[] = "\n" + "Devmem command" + "\v" + "Supported commands:\n" + " read Read data from /dev/mem\n" + " write Write data to /dev/mem\n"; static struct argp_option cmd_devmem_options[] = { - {0}, + { 0 }, }; struct cmd_devmem_args { - struct ast_ahb_args ahb_args; + struct ast_ahb_args ahb_args; }; -static error_t cmd_devmem_parse_opt(int key, char *arg, struct argp_state *state) +static error_t cmd_devmem_parse_opt(int key, char *arg, + struct argp_state *state) { - struct cmd_devmem_args *arguments = state->input; - - switch (key) { - case ARGP_KEY_ARG: - switch (state->arg_num) { - case 0: - if (strcmp("read", arg) && strcmp("write", arg)) - argp_error(state, "Invalid command '%s'", arg); - - if (!strcmp("read", arg)) - arguments->ahb_args.read = true; - break; - case 1: - arguments->ahb_args.address = strtoul(arg, NULL, 0); - break; - case 2: - if (!arguments->ahb_args.read) { - static uint32_t write_val; - write_val = strtoul(arg, NULL, 0); - arguments->ahb_args.write_value = &write_val; - } else - logd("Found third argument in read mode, ignoring...\n"); - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 2) - argp_error(state, "Not enough arguments provided. Need at least an address."); - - if (!arguments->ahb_args.read && state->arg_num < 3) - argp_error(state, "Write mode detected but either no address or value detected"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_devmem_args *arguments = state->input; + + switch (key) { + case ARGP_KEY_ARG: + switch (state->arg_num) { + case 0: + if (strcmp("read", arg) && strcmp("write", arg)) + argp_error(state, "Invalid command '%s'", arg); + + if (!strcmp("read", arg)) + arguments->ahb_args.read = true; + break; + case 1: + arguments->ahb_args.address = strtoul(arg, NULL, 0); + break; + case 2: + if (!arguments->ahb_args.read) { + static uint32_t write_val; + write_val = strtoul(arg, NULL, 0); + arguments->ahb_args.write_value = &write_val; + } else + logd("Found third argument in read mode, ignoring...\n"); + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 2) + argp_error( + state, + "Not enough arguments provided. Need at least an address."); + + if (!arguments->ahb_args.read && state->arg_num < 3) + argp_error( + state, + "Write mode detected but either no address or value detected"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_devmem_argp = { - .options = cmd_devmem_options, - .parser = cmd_devmem_parse_opt, - .args_doc = cmd_devmem_args_doc, - .doc = cmd_devmem_doc, + .options = cmd_devmem_options, + .parser = cmd_devmem_parse_opt, + .args_doc = cmd_devmem_args_doc, + .doc = cmd_devmem_doc, }; static int do_devmem(int argc, char **argv) { - struct devmem _devmem, *devmem = &_devmem; - struct ahb *ahb; - int cleanup, rc; - - struct cmd_devmem_args arguments = {0}; - rc = argp_parse(&cmd_devmem_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - /* - * We do not use debug_drvier_probe here as we need data - * that's not being returned there. - */ - rc = devmem_init(devmem); - if (rc < 0) { - bool denied = (rc == -EACCES || rc == -EPERM); - if (denied && !priv_am_root()) { - priv_print_unprivileged(program_invocation_short_name); - } else { - errno = -rc; - perror("devmem_init"); - } - return rc; - } - - ahb = devmem_as_ahb(devmem); - rc = ast_ahb_access(&arguments.ahb_args, ahb); - if (rc) { - errno = -rc; - perror("ast_ahb_access"); - return rc; - } - - cleanup = devmem_destroy(devmem); - if (cleanup) { - errno = -cleanup; - perror("devmem_destroy"); - return cleanup; - } - - return 0; + struct devmem _devmem, *devmem = &_devmem; + struct ahb *ahb; + int cleanup, rc; + + struct cmd_devmem_args arguments = { 0 }; + rc = argp_parse(&cmd_devmem_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + /* + * We do not use debug_drvier_probe here as we need data + * that's not being returned there. + */ + rc = devmem_init(devmem); + if (rc < 0) { + bool denied = (rc == -EACCES || rc == -EPERM); + if (denied && !priv_am_root()) { + priv_print_unprivileged(program_invocation_short_name); + } else { + errno = -rc; + perror("devmem_init"); + } + return rc; + } + + ahb = devmem_as_ahb(devmem); + rc = ast_ahb_access(&arguments.ahb_args, ahb); + if (rc) { + errno = -rc; + perror("ast_ahb_access"); + return rc; + } + + cleanup = devmem_destroy(devmem); + if (cleanup) { + errno = -cleanup; + perror("devmem_destroy"); + return cleanup; + } + + return 0; } static const struct cmd devmem_cmd = { - .name = "devmem", - .description = "Read or write data to /dev/mem", - .fn = do_devmem, + .name = "devmem", + .description = "Read or write data to /dev/mem", + .fn = do_devmem, }; REGISTER_CMD(devmem_cmd); diff --git a/src/cmd/ilpc.c b/src/cmd/ilpc.c index 6426cd9..a5e46eb 100644 --- a/src/cmd/ilpc.c +++ b/src/cmd/ilpc.c @@ -13,119 +13,123 @@ #include static char cmd_ilpc_args_doc[] = ""; -static char cmd_ilpc_doc[] = - "\n" - "ilpc command" - "\v" - "Supported commands:\n" - " read Read data via iLPC\n" - " write Write data via iLPC\n"; +static char cmd_ilpc_doc[] = "\n" + "ilpc command" + "\v" + "Supported commands:\n" + " read Read data via iLPC\n" + " write Write data via iLPC\n"; static struct argp_option cmd_ilpc_options[] = { - {0}, + { 0 }, }; struct cmd_ilpc_args { - struct ast_ahb_args ahb_args; + struct ast_ahb_args ahb_args; }; static error_t cmd_ilpc_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_ilpc_args *arguments = state->input; - - switch (key) { - case ARGP_KEY_ARG: - switch (state->arg_num) { - case 0: - if (strcmp("read", arg) && strcmp("write", arg)) - argp_error(state, "Invalid command '%s'", arg); - - if (!strcmp("read", arg)) - arguments->ahb_args.read = true; - break; - case 1: - arguments->ahb_args.address = strtoul(arg, NULL, 0); - break; - case 2: - if (!arguments->ahb_args.read) { - static uint32_t write_val; - write_val = strtoul(arg, NULL, 0); - arguments->ahb_args.write_value = &write_val; - } else { - logd("Found third argument in read mode, ignoring...\n"); - } - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 2) - argp_error(state, "Not enough arguments provided. Need at least an address."); - - if (!arguments->ahb_args.read && state->arg_num < 3) - argp_error(state, "Write mode detected but either no address or value detected"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_ilpc_args *arguments = state->input; + + switch (key) { + case ARGP_KEY_ARG: + switch (state->arg_num) { + case 0: + if (strcmp("read", arg) && strcmp("write", arg)) + argp_error(state, "Invalid command '%s'", arg); + + if (!strcmp("read", arg)) + arguments->ahb_args.read = true; + break; + case 1: + arguments->ahb_args.address = strtoul(arg, NULL, 0); + break; + case 2: + if (!arguments->ahb_args.read) { + static uint32_t write_val; + write_val = strtoul(arg, NULL, 0); + arguments->ahb_args.write_value = &write_val; + } else { + logd("Found third argument in read mode, ignoring...\n"); + } + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 2) + argp_error( + state, + "Not enough arguments provided. Need at least an address."); + + if (!arguments->ahb_args.read && state->arg_num < 3) + argp_error( + state, + "Write mode detected but either no address or value detected"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_ilpc_argp = { - .options = cmd_ilpc_options, - .parser = cmd_ilpc_parse_opt, - .args_doc = cmd_ilpc_args_doc, - .doc = cmd_ilpc_doc, + .options = cmd_ilpc_options, + .parser = cmd_ilpc_parse_opt, + .args_doc = cmd_ilpc_args_doc, + .doc = cmd_ilpc_doc, }; static int do_ilpc(int argc, char **argv) { - struct ilpcb _ilpcb , *ilpcb = &_ilpcb; - struct ahb *ahb; - int cleanup, rc; - - struct cmd_ilpc_args arguments = {0}; - rc = argp_parse(&cmd_ilpc_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - /* - * We do not use debug_drvier_probe here as we need data - * that's not being returned there. - */ - rc = ilpcb_init(ilpcb); - if (rc < 0) { - bool denied = (rc == -EACCES || rc == -EPERM); - if (denied && !priv_am_root()) { - priv_print_unprivileged(program_invocation_short_name); - } else { - errno = -rc; - perror("ilpcb_init"); - } - return rc; - } - - ahb = ilpcb_as_ahb(ilpcb); - rc = ast_ahb_access(&arguments.ahb_args, ahb); - if (rc) { - errno = -rc; - perror("ast_ahb_access"); - return rc; - } - - cleanup = ilpcb_destroy(ilpcb); - if (cleanup) { - errno = -cleanup; - perror("ilpcb_destroy"); - return rc; - } - - return 0; + struct ilpcb _ilpcb, *ilpcb = &_ilpcb; + struct ahb *ahb; + int cleanup, rc; + + struct cmd_ilpc_args arguments = { 0 }; + rc = argp_parse(&cmd_ilpc_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + /* + * We do not use debug_drvier_probe here as we need data + * that's not being returned there. + */ + rc = ilpcb_init(ilpcb); + if (rc < 0) { + bool denied = (rc == -EACCES || rc == -EPERM); + if (denied && !priv_am_root()) { + priv_print_unprivileged(program_invocation_short_name); + } else { + errno = -rc; + perror("ilpcb_init"); + } + return rc; + } + + ahb = ilpcb_as_ahb(ilpcb); + rc = ast_ahb_access(&arguments.ahb_args, ahb); + if (rc) { + errno = -rc; + perror("ast_ahb_access"); + return rc; + } + + cleanup = ilpcb_destroy(ilpcb); + if (cleanup) { + errno = -cleanup; + perror("ilpcb_destroy"); + return rc; + } + + return 0; } static const struct cmd ilpc_cmd = { - .name = "ilpc", - .description = "Read or write via iLPC", - .fn = do_ilpc, + .name = "ilpc", + .description = "Read or write via iLPC", + .fn = do_ilpc, }; REGISTER_CMD(ilpc_cmd); diff --git a/src/cmd/jtag.c b/src/cmd/jtag.c index 0087655..0c28c36 100644 --- a/src/cmd/jtag.c +++ b/src/cmd/jtag.c @@ -25,255 +25,271 @@ #include #include -static char cmd_jtag_args_doc[] = "via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]"; +static char cmd_jtag_args_doc[] = + "via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]"; static char cmd_jtag_doc[] = - "\n" - "JTAG OpenOCD Bitbang server command" - "\v" - "Supported target types:\n" - " arm Route JTAG to the internal ARM core\n" - " pcie Route JTAG to the internal PCIe PHY\n" - " external Route JTAG to the external GPIO pins\n"; + "\n" + "JTAG OpenOCD Bitbang server command" + "\v" + "Supported target types:\n" + " arm Route JTAG to the internal ARM core\n" + " pcie Route JTAG to the internal PCIe PHY\n" + " external Route JTAG to the external GPIO pins\n"; static struct argp_option cmd_jtag_options[] = { - { "controller", 'c', "CTRL", 0, "JTAG controller to use (default: jtag)", 0 }, - { "port", 'p', "PORT", 0, "Port to listen on (default: 33333)", 0 }, - { "target-type", 't', "TYPE", 0, "JTAG target to route to (default: arm)", 0 }, - {0}, + { "controller", 'c', "CTRL", 0, + "JTAG controller to use (default: jtag)", 0 }, + { "port", 'p', "PORT", 0, "Port to listen on (default: 33333)", 0 }, + { "target-type", 't', "TYPE", 0, + "JTAG target to route to (default: arm)", 0 }, + { 0 }, }; -struct cmd_jtag_args -{ - struct connection_args connection; - const char *controller; - uint32_t target_bits; - int listen_port; +struct cmd_jtag_args { + struct connection_args connection; + const char *controller; + uint32_t target_bits; + int listen_port; }; static error_t cmd_jtag_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_jtag_args *arguments = state->input; - int rc; - - switch (key) { - case 'c': - arguments->controller = arg; - break; - case 'p': - arguments->listen_port = atoi(arg); - if (!arguments->listen_port && arguments->listen_port > UINT16_MAX) - argp_error(state, "Invalid port '%s'", arg); - break; - case 't': - if (!strcmp(arg, "arm")) - arguments->target_bits = SCU_JTAG_MASTER_TO_ARM; - else if (!strcmp(arg, "pcie")) - arguments->target_bits = SCU_JTAG_MASTER_TO_PCIE; - else if (!strcmp(arg, "external")) - arguments->target_bits = SCU_JTAG_NORMAL; - else - argp_error(state, "Invalid target '%s'", arg); - break; - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - } - break; - case ARGP_KEY_END: - if (!arguments->controller) - arguments->controller = "jtag"; - if (!arguments->listen_port) - arguments->listen_port = 33333; - if (!arguments->target_bits) - arguments->target_bits = SCU_JTAG_MASTER_TO_ARM; - - if (arguments->connection.bridge_driver == NULL) - argp_error(state, "Explicit driver must be defined"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_jtag_args *arguments = state->input; + int rc; + + switch (key) { + case 'c': + arguments->controller = arg; + break; + case 'p': + arguments->listen_port = atoi(arg); + if (!arguments->listen_port && + arguments->listen_port > UINT16_MAX) + argp_error(state, "Invalid port '%s'", arg); + break; + case 't': + if (!strcmp(arg, "arm")) + arguments->target_bits = SCU_JTAG_MASTER_TO_ARM; + else if (!strcmp(arg, "pcie")) + arguments->target_bits = SCU_JTAG_MASTER_TO_PCIE; + else if (!strcmp(arg, "external")) + arguments->target_bits = SCU_JTAG_NORMAL; + else + argp_error(state, "Invalid target '%s'", arg); + break; + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + } + break; + case ARGP_KEY_END: + if (!arguments->controller) + arguments->controller = "jtag"; + if (!arguments->listen_port) + arguments->listen_port = 33333; + if (!arguments->target_bits) + arguments->target_bits = SCU_JTAG_MASTER_TO_ARM; + + if (arguments->connection.bridge_driver == NULL) + argp_error(state, "Explicit driver must be defined"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_jtag_argp = { - .options = cmd_jtag_options, - .parser = cmd_jtag_parse_opt, - .args_doc = cmd_jtag_args_doc, - .doc = cmd_jtag_doc, + .options = cmd_jtag_options, + .parser = cmd_jtag_parse_opt, + .args_doc = cmd_jtag_args_doc, + .doc = cmd_jtag_doc, }; static int run_openocd_bitbang_server(struct jtag *jtag, uint16_t port) { - int server_fd; - int client_fd; - struct sockaddr_in listen_addr; - struct sockaddr_in client_addr; - int opt = 1; - - if ((server_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - loge("socket() failed: %s\n", strerror(errno)); - return 1; - } - - setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); - - memset(&listen_addr, 0, sizeof(listen_addr)); - listen_addr.sin_family = AF_INET; - listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - listen_addr.sin_port = htons(port); - - if (bind(server_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { - loge("bind() failed: %s\n", strerror(errno)); - return 1; - } - if (listen(server_fd, 5) < 0) { - loge("listen() failed: %s\n", strerror(errno)); - return 1; - } - - logi("Ready to accept OpenOCD remote_bitbang connection on 127.0.0.1:%u\n", port); - - while (true) { - socklen_t addr_len = sizeof(client_addr); - - if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) < 0) { - loge("accept() failed: %s\n", strerror(errno)); - return 1; - } - - logi("New connection from %s\n", inet_ntoa(client_addr.sin_addr)); - - while (true) { - int rc; - uint8_t state; - uint8_t tdo; - uint8_t tdi; - uint8_t tms; - uint8_t tck; - - char command = 0; - ssize_t n = read(client_fd, &command, 1); - if (n <= 0) { - loge("Client closed connection\n"); - close(server_fd); - return 1; - } - - switch (command) { - /* LED blink commands */ - case 'B': - case 'b': - break; - /* Read state */ - case 'R': - rc = jtag_bitbang_get(jtag, &tdo); - if (rc < 0) { - loge("jtag_bitbang_get() failed\n"); - return 1; - } - - // send ASCII 0 or 1 for TDO state - tdo += '0'; - rc = write(client_fd, &tdo, 1); - if (rc < 0) { - loge("write(client_fd) failed: %d\n", rc); - return 1; - } - break; - case 'Q': - logi("Received quit request from OpenOCD\n"); - close(client_fd); - close(server_fd); - return 1; - /* Data requests */ - case '0'...'7': - state = command - '0'; - - tdi = !!(state & 1); - tms = !!(state & 2); - tck = !!(state & 4); - - rc = jtag_bitbang_set(jtag, tck, tms, tdi); - if (rc < 0) { - loge("jtag_bitbang_set() failed: %d\n", rc); - return 1; - } - break; - /* Reset requests */ - case 'r': - case 's': - case 't': - case 'u': - logt("Received reset request from OpenOCD, currently unsupported\n"); - break; - default: - loge("Received unknown command from OpenOCD: %c\n", command); - } - } - } + int server_fd; + int client_fd; + struct sockaddr_in listen_addr; + struct sockaddr_in client_addr; + int opt = 1; + + if ((server_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + loge("socket() failed: %s\n", strerror(errno)); + return 1; + } + + setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); + + memset(&listen_addr, 0, sizeof(listen_addr)); + listen_addr.sin_family = AF_INET; + listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + listen_addr.sin_port = htons(port); + + if (bind(server_fd, (struct sockaddr *)&listen_addr, + sizeof(listen_addr)) < 0) { + loge("bind() failed: %s\n", strerror(errno)); + return 1; + } + if (listen(server_fd, 5) < 0) { + loge("listen() failed: %s\n", strerror(errno)); + return 1; + } + + logi("Ready to accept OpenOCD remote_bitbang connection on 127.0.0.1:%u\n", + port); + + while (true) { + socklen_t addr_len = sizeof(client_addr); + + if ((client_fd = accept(server_fd, + (struct sockaddr *)&client_addr, + &addr_len)) < 0) { + loge("accept() failed: %s\n", strerror(errno)); + return 1; + } + + logi("New connection from %s\n", + inet_ntoa(client_addr.sin_addr)); + + while (true) { + int rc; + uint8_t state; + uint8_t tdo; + uint8_t tdi; + uint8_t tms; + uint8_t tck; + + char command = 0; + ssize_t n = read(client_fd, &command, 1); + if (n <= 0) { + loge("Client closed connection\n"); + close(server_fd); + return 1; + } + + switch (command) { + /* LED blink commands */ + case 'B': + case 'b': + break; + /* Read state */ + case 'R': + rc = jtag_bitbang_get(jtag, &tdo); + if (rc < 0) { + loge("jtag_bitbang_get() failed\n"); + return 1; + } + + // send ASCII 0 or 1 for TDO state + tdo += '0'; + rc = write(client_fd, &tdo, 1); + if (rc < 0) { + loge("write(client_fd) failed: %d\n", + rc); + return 1; + } + break; + case 'Q': + logi("Received quit request from OpenOCD\n"); + close(client_fd); + close(server_fd); + return 1; + /* Data requests */ + case '0' ... '7': + state = command - '0'; + + tdi = !!(state & 1); + tms = !!(state & 2); + tck = !!(state & 4); + + rc = jtag_bitbang_set(jtag, tck, tms, tdi); + if (rc < 0) { + loge("jtag_bitbang_set() failed: %d\n", + rc); + return 1; + } + break; + /* Reset requests */ + case 'r': + case 's': + case 't': + case 'u': + logt("Received reset request from OpenOCD, currently unsupported\n"); + break; + default: + loge("Received unknown command from OpenOCD: %c\n", + command); + } + } + } } static int do_jtag(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct ahb *ahb; - struct jtag *jtag; - int rc; - - struct cmd_jtag_args arguments = {0}; - rc = argp_parse(&cmd_jtag_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - rc = EXIT_FAILURE; - goto done; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - exit(EXIT_FAILURE); - } - - /* Probe the SoC */ - if ((rc = soc_probe(soc, ahb)) < 0) { - errno = -rc; - perror("soc_probe"); - goto cleanup_host; - } - - /* Initialise the required SoC drivers */ - if (!(jtag = jtag_get(soc, arguments.controller))) { - loge("Failed to acquire JTAG controller, exiting\n"); - goto cleanup_soc; - } - - jtag_route(jtag, arguments.target_bits); - - while (true) { - run_openocd_bitbang_server(jtag, arguments.listen_port); - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct ahb *ahb; + struct jtag *jtag; + int rc; + + struct cmd_jtag_args arguments = { 0 }; + rc = argp_parse(&cmd_jtag_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + rc = EXIT_FAILURE; + goto done; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + exit(EXIT_FAILURE); + } + + /* Probe the SoC */ + if ((rc = soc_probe(soc, ahb)) < 0) { + errno = -rc; + perror("soc_probe"); + goto cleanup_host; + } + + /* Initialise the required SoC drivers */ + if (!(jtag = jtag_get(soc, arguments.controller))) { + loge("Failed to acquire JTAG controller, exiting\n"); + goto cleanup_soc; + } + + jtag_route(jtag, arguments.target_bits); + + while (true) { + run_openocd_bitbang_server(jtag, arguments.listen_port); + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); done: - exit(rc); + exit(rc); } static const struct cmd jtag_cmd = { - .name = "jtag", - .description = "Start a JTAG OpenOCD Bitbang server", - .fn = do_jtag, + .name = "jtag", + .description = "Start a JTAG OpenOCD Bitbang server", + .fn = do_jtag, }; REGISTER_CMD(jtag_cmd); diff --git a/src/cmd/meson.build b/src/cmd/meson.build index 502779c..a956d67 100644 --- a/src/cmd/meson.build +++ b/src/cmd/meson.build @@ -1,15 +1,17 @@ -src += files('console.c', - 'coprocessor.c', - 'debug.c', - 'devmem.c', - 'ilpc.c', - 'jtag.c', - 'otp.c', - 'p2a.c', - 'probe.c', - 'read.c', - 'replace.c', - 'reset.c', - 'sfc.c', - 'trace.c', - 'write.c') +src += files( + 'console.c', + 'coprocessor.c', + 'debug.c', + 'devmem.c', + 'ilpc.c', + 'jtag.c', + 'otp.c', + 'p2a.c', + 'probe.c', + 'read.c', + 'replace.c', + 'reset.c', + 'sfc.c', + 'trace.c', + 'write.c', +) diff --git a/src/cmd/otp.c b/src/cmd/otp.c index 021e7df..a57f755 100644 --- a/src/cmd/otp.c +++ b/src/cmd/otp.c @@ -19,151 +19,160 @@ #include static char cmd_otp_args_doc[] = - " >" - " [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; + " >" + " [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; static char cmd_otp_doc[] = - "\n" - "Otp command" - "\v" - "Supported regions:\n" - " conf OTP configuration (word and bit for write)\n" - " strap OTP strap (bit and value for write)" - "\v" - "Supported commands:\n" - " read Read data to OTP storage\n" - " write Write data to OTP storage\n"; + "\n" + "Otp command" + "\v" + "Supported regions:\n" + " conf OTP configuration (word and bit for write)\n" + " strap OTP strap (bit and value for write)" + "\v" + "Supported commands:\n" + " read Read data to OTP storage\n" + " write Write data to OTP storage\n"; static struct argp_option cmd_otp_options[] = { - {0}, + { 0 }, }; struct cmd_otp_args { - struct connection_args connection; - enum otp_region region; - bool read; - unsigned int key; - unsigned int value; + struct connection_args connection; + enum otp_region region; + bool read; + unsigned int key; + unsigned int value; }; static error_t cmd_otp_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_otp_args *arguments = state->input; - int rc; - - switch (key) { - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - switch (state->arg_num) { - case 0: - if (strcmp("conf", arg) && strcmp("strap", arg)) - argp_error(state, "Invalid region '%s'", arg); - - arguments->region = !strcmp("conf", arg) ? otp_region_conf : otp_region_strap; - break; - case 1: - if (strcmp("read", arg) && strcmp("write", arg)) - argp_error(state, "Invalid command '%s'", arg); - - arguments->read = !(strcmp(arg, "read")); - break; - case 2: - arguments->key = strtoul(arg, NULL, 0); - break; - case 3: - arguments->key = strtoul(arg, NULL, 0); - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 2) - argp_error(state, "Not enough arguments"); - if (!arguments->read && state->arg_num < 4) - argp_error(state, "Not enough arguments for write"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_otp_args *arguments = state->input; + int rc; + + switch (key) { + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + switch (state->arg_num) { + case 0: + if (strcmp("conf", arg) && strcmp("strap", arg)) + argp_error(state, "Invalid region '%s'", arg); + + arguments->region = !strcmp("conf", arg) ? + otp_region_conf : + otp_region_strap; + break; + case 1: + if (strcmp("read", arg) && strcmp("write", arg)) + argp_error(state, "Invalid command '%s'", arg); + + arguments->read = !(strcmp(arg, "read")); + break; + case 2: + arguments->key = strtoul(arg, NULL, 0); + break; + case 3: + arguments->key = strtoul(arg, NULL, 0); + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 2) + argp_error(state, "Not enough arguments"); + if (!arguments->read && state->arg_num < 4) + argp_error(state, "Not enough arguments for write"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_otp_argp = { - .options = cmd_otp_options, - .parser = cmd_otp_parse_opt, - .args_doc = cmd_otp_args_doc, - .doc = cmd_otp_doc, + .options = cmd_otp_options, + .parser = cmd_otp_parse_opt, + .args_doc = cmd_otp_args_doc, + .doc = cmd_otp_doc, }; static int do_otp(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct otp *otp; - struct ahb *ahb; - int rc; - - struct cmd_otp_args arguments = {0}; - rc = argp_parse(&cmd_otp_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - errno = -rc; - perror("soc_probe"); - goto cleanup_host; - } - - if (soc_generation(soc) != ast_g6) { - loge("We currently only support the AST2600-series coprocessor\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if (!(otp = otp_get(soc))) { - loge("Failed to acquire OTP controller, exiting\n"); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - if (arguments.read) - rc = otp_read(otp, arguments.region); - else { - if (arguments.region == otp_region_strap) - rc = otp_write_strap(otp, arguments.key, arguments.value); - else - rc = otp_write_conf(otp, arguments.key, arguments.value); - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct otp *otp; + struct ahb *ahb; + int rc; + + struct cmd_otp_args arguments = { 0 }; + rc = argp_parse(&cmd_otp_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + errno = -rc; + perror("soc_probe"); + goto cleanup_host; + } + + if (soc_generation(soc) != ast_g6) { + loge("We currently only support the AST2600-series coprocessor\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if (!(otp = otp_get(soc))) { + loge("Failed to acquire OTP controller, exiting\n"); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + if (arguments.read) + rc = otp_read(otp, arguments.region); + else { + if (arguments.region == otp_region_strap) + rc = otp_write_strap(otp, arguments.key, + arguments.value); + else + rc = otp_write_conf(otp, arguments.key, + arguments.value); + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static const struct cmd otp_cmd = { - .name = "otp", - .description = "Read and write the OTP configuration (AST2600-only)", - .fn = do_otp, + .name = "otp", + .description = "Read and write the OTP configuration (AST2600-only)", + .fn = do_otp, }; REGISTER_CMD(otp_cmd); diff --git a/src/cmd/p2a.c b/src/cmd/p2a.c index abbd6c3..a364ec1 100644 --- a/src/cmd/p2a.c +++ b/src/cmd/p2a.c @@ -14,129 +14,136 @@ #include #include -static char cmd_p2a_args_doc[] = " "; -static char cmd_p2a_doc[] = - "\n" - "p2a command" - "\v" - "Supported PCIe devices:\n" - " vga VGA PCIe device\n" - " bmc BMC PCIe device\n" - "\v" - "Supported commands:\n" - " read Read data via p2a\n" - " write Write data via p2a\n"; +static char cmd_p2a_args_doc[] = + " "; +static char cmd_p2a_doc[] = "\n" + "p2a command" + "\v" + "Supported PCIe devices:\n" + " vga VGA PCIe device\n" + " bmc BMC PCIe device\n" + "\v" + "Supported commands:\n" + " read Read data via p2a\n" + " write Write data via p2a\n"; static struct argp_option cmd_p2a_options[] = { - {0}, + { 0 }, }; struct cmd_p2a_args { - struct ast_ahb_args ahb_args; - uint16_t device_id; + struct ast_ahb_args ahb_args; + uint16_t device_id; }; static error_t cmd_p2a_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_p2a_args *arguments = state->input; - - switch (key) { - case ARGP_KEY_ARG: - switch (state->arg_num) { - case 0: - if (!strcmp("vga", arg)) - arguments->device_id = AST_PCI_DID_VGA; - else if (!strcmp("bmc", arg)) - arguments->device_id = AST_PCI_DID_BMC; - else - argp_error(state, "Invalid PCIe device '%s'", arg); - break; - case 1: - if (strcmp("read", arg) && strcmp("write", arg)) - argp_error(state, "Invalid operation '%s'", arg); - - if (!strcmp("read", arg)) - arguments->ahb_args.read = true; - break; - case 2: - arguments->ahb_args.address = strtoul(arg, NULL, 0); - break; - case 3: - if (!arguments->ahb_args.read) { - static uint32_t write_val; - write_val = strtoul(arg, NULL, 0); - arguments->ahb_args.write_value = &write_val; - } else { - logd("Found third argument in read mode, ignoring...\n"); - } - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 3) - argp_error(state, "Not enough arguments provided. Need the device type, the operation and an address."); - - if (!arguments->ahb_args.read && state->arg_num < 4) - argp_error(state, "Write mode detected but either no address or value detected"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_p2a_args *arguments = state->input; + + switch (key) { + case ARGP_KEY_ARG: + switch (state->arg_num) { + case 0: + if (!strcmp("vga", arg)) + arguments->device_id = AST_PCI_DID_VGA; + else if (!strcmp("bmc", arg)) + arguments->device_id = AST_PCI_DID_BMC; + else + argp_error(state, "Invalid PCIe device '%s'", + arg); + break; + case 1: + if (strcmp("read", arg) && strcmp("write", arg)) + argp_error(state, "Invalid operation '%s'", + arg); + + if (!strcmp("read", arg)) + arguments->ahb_args.read = true; + break; + case 2: + arguments->ahb_args.address = strtoul(arg, NULL, 0); + break; + case 3: + if (!arguments->ahb_args.read) { + static uint32_t write_val; + write_val = strtoul(arg, NULL, 0); + arguments->ahb_args.write_value = &write_val; + } else { + logd("Found third argument in read mode, ignoring...\n"); + } + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 3) + argp_error( + state, + "Not enough arguments provided. Need the device type, the operation and an address."); + + if (!arguments->ahb_args.read && state->arg_num < 4) + argp_error( + state, + "Write mode detected but either no address or value detected"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_p2a_argp = { - .options = cmd_p2a_options, - .parser = cmd_p2a_parse_opt, - .args_doc = cmd_p2a_args_doc, - .doc = cmd_p2a_doc, + .options = cmd_p2a_options, + .parser = cmd_p2a_parse_opt, + .args_doc = cmd_p2a_args_doc, + .doc = cmd_p2a_doc, }; static int do_p2a(int argc, char **argv) { - struct p2ab _p2ab, *p2ab = &_p2ab; - struct ahb *ahb; - int cleanup, rc; - - struct cmd_p2a_args arguments = {0}; - rc = argp_parse(&cmd_p2a_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - rc = p2ab_init(p2ab, AST_PCI_VID, arguments.device_id); - if (rc < 0) { - bool denied = (rc == -EACCES || rc == -EPERM); - if (denied && !priv_am_root()) { - priv_print_unprivileged(program_invocation_short_name); - } else { - errno = -rc; - perror("p2ab_init"); - } - return rc; - } - - ahb = p2ab_as_ahb(p2ab); - rc = ast_ahb_access(&arguments.ahb_args, ahb); - if (rc) { - errno = -rc; - perror("ast_ahb_access"); - return rc; - } - - cleanup = p2ab_destroy(p2ab); - if (cleanup) { - errno = -cleanup; - perror("p2ab_destroy"); - return cleanup; - } - - return 0; + struct p2ab _p2ab, *p2ab = &_p2ab; + struct ahb *ahb; + int cleanup, rc; + + struct cmd_p2a_args arguments = { 0 }; + rc = argp_parse(&cmd_p2a_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + rc = p2ab_init(p2ab, AST_PCI_VID, arguments.device_id); + if (rc < 0) { + bool denied = (rc == -EACCES || rc == -EPERM); + if (denied && !priv_am_root()) { + priv_print_unprivileged(program_invocation_short_name); + } else { + errno = -rc; + perror("p2ab_init"); + } + return rc; + } + + ahb = p2ab_as_ahb(p2ab); + rc = ast_ahb_access(&arguments.ahb_args, ahb); + if (rc) { + errno = -rc; + perror("ast_ahb_access"); + return rc; + } + + cleanup = p2ab_destroy(p2ab); + if (cleanup) { + errno = -cleanup; + perror("p2ab_destroy"); + return cleanup; + } + + return 0; } static const struct cmd p2a_cmd = { - .name = "p2a", - .description = "Read or write data via p2a devices", - .fn = do_p2a, + .name = "p2a", + .description = "Read or write data via p2a devices", + .fn = do_p2a, }; REGISTER_CMD(p2a_cmd); diff --git a/src/cmd/probe.c b/src/cmd/probe.c index 1974cec..f03df09 100644 --- a/src/cmd/probe.c +++ b/src/cmd/probe.c @@ -11,133 +11,142 @@ #include #include -static char cmd_probe_args_doc[] = "[-l] [-r REQUIREMENT] [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; -static char cmd_probe_doc[] = - "\n" - "Probe command" - "\v" - "Supported requirements:\n" - " integrity Require integrity\n" - " confidentiality Require confidentiality\n"; +static char cmd_probe_args_doc[] = + "[-l] [-r REQUIREMENT] [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; +static char cmd_probe_doc[] = "\n" + "Probe command" + "\v" + "Supported requirements:\n" + " integrity Require integrity\n" + " confidentiality Require confidentiality\n"; static struct argp_option cmd_probe_options[] = { - {"list-interfaces", 'l', 0, 0, "List available interfaces", 0}, - {"require", 'r', "REQUIREMENT", 0, "Requirement to probe for", 0}, - {0}, + { "list-interfaces", 'l', 0, 0, "List available interfaces", 0 }, + { "require", 'r', "REQUIREMENT", 0, "Requirement to probe for", 0 }, + { 0 }, }; struct cmd_probe_args { - int list_ifaces; - enum bridge_mode requirement; - struct connection_args connection; + int list_ifaces; + enum bridge_mode requirement; + struct connection_args connection; }; static error_t cmd_probe_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_probe_args *arguments = state->input; - int rc = 0; - - switch (key) { - case 'l': - arguments->list_ifaces = 1; - break; - case 'r': - if (strcmp("integrity", arg) && strcmp("confidentiality", arg)) - argp_error(state, "Invalid requirement '%s'", arg); - - if (!strcmp("integrity", arg)) - arguments->requirement = bm_restricted; - else - arguments->requirement = bm_disabled; - break; - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - return 0; - } - - break; - case ARGP_KEY_END: - if (!arguments->requirement) - arguments->requirement = bm_permissive; - - if (!arguments->connection.interface) - arguments->connection.interface = NULL; - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_probe_args *arguments = state->input; + int rc = 0; + + switch (key) { + case 'l': + arguments->list_ifaces = 1; + break; + case 'r': + if (strcmp("integrity", arg) && strcmp("confidentiality", arg)) + argp_error(state, "Invalid requirement '%s'", arg); + + if (!strcmp("integrity", arg)) + arguments->requirement = bm_restricted; + else + arguments->requirement = bm_disabled; + break; + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + return 0; + } + + break; + case ARGP_KEY_END: + if (!arguments->requirement) + arguments->requirement = bm_permissive; + + if (!arguments->connection.interface) + arguments->connection.interface = NULL; + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_probe_argp = { - .options = cmd_probe_options, - .parser = cmd_probe_parse_opt, - .args_doc = cmd_probe_args_doc, - .doc = cmd_probe_doc, + .options = cmd_probe_options, + .parser = cmd_probe_parse_opt, + .args_doc = cmd_probe_args_doc, + .doc = cmd_probe_doc, }; static int do_probe(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - enum bridge_mode discovered; - struct ahb *ahb; - int rc; - - struct cmd_probe_args arguments = {0}; - rc = argp_parse(&cmd_probe_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) { - rc = EXIT_FAILURE; - goto done; - } - - if ((rc = host_init(host, &arguments.connection) < 0)) { - loge("Failed to initialise host interfaces: %d\n", rc); - rc = EXIT_FAILURE; - goto done; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - loge("Failed to probe SoC, exiting: %d\n", rc); - goto cleanup_host; - } - - if (arguments.list_ifaces) { - soc_list_bridge_controllers(soc); - rc = EXIT_SUCCESS; - } else { - if ((rc = soc_probe_bridge_controllers(soc, &discovered, arguments.connection.interface)) < 0) { - loge("Failed to probe SoC bridge controllers: %d\n", rc); - rc = EXIT_FAILURE; - goto cleanup_soc; - } - - rc = (arguments.requirement <= discovered) ? EXIT_SUCCESS : EXIT_FAILURE; - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + enum bridge_mode discovered; + struct ahb *ahb; + int rc; + + struct cmd_probe_args arguments = { 0 }; + rc = argp_parse(&cmd_probe_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) { + rc = EXIT_FAILURE; + goto done; + } + + if ((rc = host_init(host, &arguments.connection) < 0)) { + loge("Failed to initialise host interfaces: %d\n", rc); + rc = EXIT_FAILURE; + goto done; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + loge("Failed to probe SoC, exiting: %d\n", rc); + goto cleanup_host; + } + + if (arguments.list_ifaces) { + soc_list_bridge_controllers(soc); + rc = EXIT_SUCCESS; + } else { + if ((rc = soc_probe_bridge_controllers( + soc, &discovered, + arguments.connection.interface)) < 0) { + loge("Failed to probe SoC bridge controllers: %d\n", + rc); + rc = EXIT_FAILURE; + goto cleanup_soc; + } + + rc = (arguments.requirement <= discovered) ? EXIT_SUCCESS : + EXIT_FAILURE; + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); done: - exit(rc); + exit(rc); } static const struct cmd probe_cmd = { - .name = "probe", - .description = "Probe for any BMC", - .fn = do_probe, + .name = "probe", + .description = "Probe for any BMC", + .fn = do_probe, }; REGISTER_CMD(probe_cmd); diff --git a/src/cmd/read.c b/src/cmd/read.c index cbcfafa..573ff89 100644 --- a/src/cmd/read.c +++ b/src/cmd/read.c @@ -23,262 +23,276 @@ #include static char cmd_read_args_doc[] = - "--type= [
] " - "[via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; + "--type= [
] " + "[via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; static char cmd_read_doc[] = - "\n" - "Read command" - "\v" - "NOTE: Only the 'ram' type can parse address and length!\n\n" - "All data will be written to stdout.\n\n" - "Supported types:\n" - " firmware Read the content from the FMC\n" - " ram Read the content from the memory\n"; + "\n" + "Read command" + "\v" + "NOTE: Only the 'ram' type can parse address and length!\n\n" + "All data will be written to stdout.\n\n" + "Supported types:\n" + " firmware Read the content from the FMC\n" + " ram Read the content from the memory\n"; enum cmd_read_mode { - none, - firmware, - ram, + none, + firmware, + ram, }; static struct argp_option cmd_read_options[] = { - { "type", 't', "TYPE", 0, "Type to be read from", 0 }, - {0}, + { "type", 't', "TYPE", 0, "Type to be read from", 0 }, + { 0 }, }; struct cmd_read_args { - unsigned long mem_base; - unsigned long mem_size; - struct connection_args connection; - enum cmd_read_mode mode; + unsigned long mem_base; + unsigned long mem_size; + struct connection_args connection; + enum cmd_read_mode mode; }; static error_t cmd_read_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_read_args *arguments = state->input; - int rc; - - switch (key) { - case 't': - if (strcmp(arg, "firmware") && strcmp(arg, "ram")) - argp_error(state, "Invalid type '%s'", arg); - - if (!strcmp(arg, "firmware")) - arguments->mode = firmware; - else - arguments->mode = ram; - break; - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - /* If mode is not ram, skip any further args */ - if (arguments->mode != ram) - break; - - if (state->arg_num == 0) - parse_mem_arg(state, "read RAM base", &arguments->mem_base, arg); - else if (state->arg_num == 1) - parse_mem_arg(state, "read RAM size", &arguments->mem_size, arg); - - break; - case ARGP_KEY_END: - if (arguments->mode == none) - argp_error(state, "No type to be read from defined..."); - /* It is possible to not pass the ram base and size */ - if (arguments->mode == ram && (state->arg_num > 0 && state->arg_num < 2)) - argp_error(state, "Not enough arguments for ram mode..."); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_read_args *arguments = state->input; + int rc; + + switch (key) { + case 't': + if (strcmp(arg, "firmware") && strcmp(arg, "ram")) + argp_error(state, "Invalid type '%s'", arg); + + if (!strcmp(arg, "firmware")) + arguments->mode = firmware; + else + arguments->mode = ram; + break; + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + /* If mode is not ram, skip any further args */ + if (arguments->mode != ram) + break; + + if (state->arg_num == 0) + parse_mem_arg(state, "read RAM base", + &arguments->mem_base, arg); + else if (state->arg_num == 1) + parse_mem_arg(state, "read RAM size", + &arguments->mem_size, arg); + + break; + case ARGP_KEY_END: + if (arguments->mode == none) + argp_error(state, "No type to be read from defined..."); + /* It is possible to not pass the ram base and size */ + if (arguments->mode == ram && + (state->arg_num > 0 && state->arg_num < 2)) + argp_error(state, + "Not enough arguments for ram mode..."); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_read_argp = { - .options = cmd_read_options, - .parser = cmd_read_parse_opt, - .args_doc = cmd_read_args_doc, - .doc = cmd_read_doc, + .options = cmd_read_options, + .parser = cmd_read_parse_opt, + .args_doc = cmd_read_args_doc, + .doc = cmd_read_doc, }; static int cmd_read_firmware(struct cmd_read_args *arguments) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct ahb *ahb; - struct soc_region flash; - struct flash_chip *chip; - struct sfc *sfc; - uint32_t wp; - int cleanup; - int rc; - - if ((rc = host_init(host, &arguments->connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = -ENODEV; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) - goto cleanup_host; - - logi("Initialising flash controller\n"); - if (!(sfc = sfc_get_by_name(soc, "fmc"))) { - loge("Failed to acquire SPI controller\n"); - rc = -ENODEV; - goto cleanup_soc; - } - - logi("Initialising flash chip\n"); - if ((rc = flash_init(sfc, &chip))) { - goto cleanup_soc; - } - - logi("Write-protecting all chip-selects\n"); - if ((rc = sfc_write_protect_save(sfc, true, &wp))) - goto cleanup_chip; - - if ((rc = sfc_get_flash(sfc, &flash)) < 0) - goto cleanup_chip; - - logi("Exfiltrating BMC flash to stdout\n\n"); - rc = soc_siphon_out(soc, flash.start, chip->info.size, 1); - if (rc) { errno = -rc; perror("soc_siphon_in"); } - - if ((cleanup = sfc_write_protect_restore(sfc, wp)) < 0) { - errno = -rc; - perror("sfc_write_protect_restore"); - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct ahb *ahb; + struct soc_region flash; + struct flash_chip *chip; + struct sfc *sfc; + uint32_t wp; + int cleanup; + int rc; + + if ((rc = host_init(host, &arguments->connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = -ENODEV; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) + goto cleanup_host; + + logi("Initialising flash controller\n"); + if (!(sfc = sfc_get_by_name(soc, "fmc"))) { + loge("Failed to acquire SPI controller\n"); + rc = -ENODEV; + goto cleanup_soc; + } + + logi("Initialising flash chip\n"); + if ((rc = flash_init(sfc, &chip))) { + goto cleanup_soc; + } + + logi("Write-protecting all chip-selects\n"); + if ((rc = sfc_write_protect_save(sfc, true, &wp))) + goto cleanup_chip; + + if ((rc = sfc_get_flash(sfc, &flash)) < 0) + goto cleanup_chip; + + logi("Exfiltrating BMC flash to stdout\n\n"); + rc = soc_siphon_out(soc, flash.start, chip->info.size, 1); + if (rc) { + errno = -rc; + perror("soc_siphon_in"); + } + + if ((cleanup = sfc_write_protect_restore(sfc, wp)) < 0) { + errno = -rc; + perror("sfc_write_protect_restore"); + } cleanup_chip: - flash_destroy(chip); + flash_destroy(chip); cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static int cmd_read_ram(struct cmd_read_args *arguments) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct soc_region dram, vram; - struct sdmc *sdmc; - struct ahb *ahb; - int rc; - - if (UINT32_MAX - arguments->mem_size < arguments->mem_base) - return -EINVAL; - - if ((rc = host_init(host, &arguments->connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = -ENODEV; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) - goto cleanup_host; - - - if (!(sdmc = sdmc_get(soc))) { - rc = -ENODEV; - goto cleanup_soc; - } - - if ((rc = sdmc_get_dram(sdmc, &dram))) { - goto cleanup_soc; - } - - if (arguments->mem_base && arguments->mem_size) { - if (arguments->mem_base < dram.start || - (arguments->mem_base + arguments->mem_size) > (dram.start + dram.length)) { - loge("Ill-formed RAM region provided for write\n"); - rc = -EINVAL; - goto cleanup_soc; - } - - logi("Dumping %" PRId32 "MiB (%#.8" PRIx32 "-%#.8" PRIx32 ")", - arguments->mem_size >> 20, arguments->mem_base, - arguments->mem_base + arguments->mem_size - 1); - } else { - if ((rc = sdmc_get_vram(sdmc, &vram))) { - goto cleanup_soc; - } - - arguments->mem_base = dram.start; - arguments->mem_size = dram.length - vram.length; - - logi("%dMiB DRAM with %dMiB VRAM; dumping %dMiB (0x%x-0x%08x)\n", - dram.length >> 20, vram.length >> 20, - (dram.length - vram.length) >> 20, dram.start, vram.start - 1); - } - - rc = soc_siphon_out(soc, arguments->mem_base, arguments->mem_size, STDOUT_FILENO); - if (rc) { - errno = -rc; - perror("soc_siphon_in"); - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct soc_region dram, vram; + struct sdmc *sdmc; + struct ahb *ahb; + int rc; + + if (UINT32_MAX - arguments->mem_size < arguments->mem_base) + return -EINVAL; + + if ((rc = host_init(host, &arguments->connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = -ENODEV; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) + goto cleanup_host; + + if (!(sdmc = sdmc_get(soc))) { + rc = -ENODEV; + goto cleanup_soc; + } + + if ((rc = sdmc_get_dram(sdmc, &dram))) { + goto cleanup_soc; + } + + if (arguments->mem_base && arguments->mem_size) { + if (arguments->mem_base < dram.start || + (arguments->mem_base + arguments->mem_size) > + (dram.start + dram.length)) { + loge("Ill-formed RAM region provided for write\n"); + rc = -EINVAL; + goto cleanup_soc; + } + + logi("Dumping %" PRId32 "MiB (%#.8" PRIx32 "-%#.8" PRIx32 ")", + arguments->mem_size >> 20, arguments->mem_base, + arguments->mem_base + arguments->mem_size - 1); + } else { + if ((rc = sdmc_get_vram(sdmc, &vram))) { + goto cleanup_soc; + } + + arguments->mem_base = dram.start; + arguments->mem_size = dram.length - vram.length; + + logi("%dMiB DRAM with %dMiB VRAM; dumping %dMiB (0x%x-0x%08x)\n", + dram.length >> 20, vram.length >> 20, + (dram.length - vram.length) >> 20, dram.start, + vram.start - 1); + } + + rc = soc_siphon_out(soc, arguments->mem_base, arguments->mem_size, + STDOUT_FILENO); + if (rc) { + errno = -rc; + perror("soc_siphon_in"); + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static int do_read(int argc, char **argv) { - int rc; - - struct cmd_read_args arguments = {0}; - arguments.mode = none; - rc = argp_parse(&cmd_read_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - switch (arguments.mode) { - case firmware: - rc = cmd_read_firmware(&arguments); - break; - case ram: - rc = cmd_read_ram(&arguments); - break; - /* If it reaches none here, then the argument parse logic is broken. */ - case none: - loge("read: Reached 'none' mode after argument parsing. This is a bug!\n"); - rc = -EINVAL; - break; - } - - return rc; + int rc; + + struct cmd_read_args arguments = { 0 }; + arguments.mode = none; + rc = argp_parse(&cmd_read_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + switch (arguments.mode) { + case firmware: + rc = cmd_read_firmware(&arguments); + break; + case ram: + rc = cmd_read_ram(&arguments); + break; + /* If it reaches none here, then the argument parse logic is broken. */ + case none: + loge("read: Reached 'none' mode after argument parsing. This is a bug!\n"); + rc = -EINVAL; + break; + } + + return rc; } static const struct cmd read_cmd = { - .name = "read", - .description = "Read data from the FMC or RAM", - .fn = do_read, + .name = "read", + .description = "Read data from the FMC or RAM", + .fn = do_read, }; REGISTER_CMD(read_cmd); diff --git a/src/cmd/replace.c b/src/cmd/replace.c index 0301ed6..82191de 100644 --- a/src/cmd/replace.c +++ b/src/cmd/replace.c @@ -19,181 +19,198 @@ #include #include -#define DUMP_RAM_WIN (8 << 20) +#define DUMP_RAM_WIN (8 << 20) static char cmd_replace_args_doc[] = - "ram " - "[via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; + "ram " + "[via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; -static char cmd_replace_doc[] = - "\n" - "Replace commnd" - "\v" - "Replace a portion in the memory.\n"; +static char cmd_replace_doc[] = "\n" + "Replace commnd" + "\v" + "Replace a portion in the memory.\n"; static struct argp_option cmd_replace_options[] = { - {0}, + { 0 }, }; struct cmd_replace_args { - const char *mem_match; - const char *mem_replace; - struct connection_args connection; + const char *mem_match; + const char *mem_replace; + struct connection_args connection; }; static error_t cmd_replace_parse_opt(int key, char *arg, - struct argp_state *state) + struct argp_state *state) { - struct cmd_replace_args *arguments = state->input; - int rc; - - switch (key) { - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - switch (state->arg_num) { - case 0: - if (strcmp(arg, "ram")) - argp_error(state, "Invalid type '%s' found. Only 'ram' is allowed", arg); - break; - case 1: - arguments->mem_match = arg; - break; - case 2: - arguments->mem_replace = arg; - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 3) - argp_error(state, "Not enough arguments..."); - - if (strlen(arguments->mem_match) < strlen(arguments->mem_replace)) - argp_error(state, "REPLACE length %zd overruns MATCH length %zd, bailing", - strlen(arguments->mem_match), strlen(arguments->mem_replace)); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_replace_args *arguments = state->input; + int rc; + + switch (key) { + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + switch (state->arg_num) { + case 0: + if (strcmp(arg, "ram")) + argp_error( + state, + "Invalid type '%s' found. Only 'ram' is allowed", + arg); + break; + case 1: + arguments->mem_match = arg; + break; + case 2: + arguments->mem_replace = arg; + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 3) + argp_error(state, "Not enough arguments..."); + + if (strlen(arguments->mem_match) < + strlen(arguments->mem_replace)) + argp_error( + state, + "REPLACE length %zd overruns MATCH length %zd, bailing", + strlen(arguments->mem_match), + strlen(arguments->mem_replace)); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_replace_argp = { - .options = cmd_replace_options, - .parser = cmd_replace_parse_opt, - .args_doc = cmd_replace_args_doc, - .doc = cmd_replace_doc, + .options = cmd_replace_options, + .parser = cmd_replace_parse_opt, + .args_doc = cmd_replace_args_doc, + .doc = cmd_replace_doc, }; static int do_replace(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct soc_region dram, vram; - size_t replace_len; - struct sdmc *sdmc; - size_t ram_cursor; - struct ahb *ahb; - void *win_chunk; - void *needle; - int rc; - - struct cmd_replace_args arguments = {0}; - rc = argp_parse(&cmd_replace_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - win_chunk = malloc(DUMP_RAM_WIN); - if (!win_chunk) { perror("malloc"); exit(EXIT_FAILURE); } - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - rc = EXIT_FAILURE; - goto win_chunk_cleanup; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto host_cleanup; - } - - if ((rc = soc_probe(soc, ahb))) - goto host_cleanup; - - if (!(sdmc = sdmc_get(soc))) { - loge("Failed to acquire memory controller, exiting\n"); - rc = EXIT_FAILURE; - goto soc_cleanup; - } - - if ((rc = sdmc_get_dram(sdmc, &dram))) - goto soc_cleanup; - - if ((rc = sdmc_get_vram(sdmc, &vram))) - goto soc_cleanup; - - replace_len = strlen(arguments.mem_match); - for (ram_cursor = dram.start; - ram_cursor < vram.start; - ram_cursor += DUMP_RAM_WIN) { - logi("Scanning BMC RAM in range 0x%08zx-0x%08zx\n", - ram_cursor, ram_cursor + DUMP_RAM_WIN - 1); - rc = ahb_read(ahb, ram_cursor, win_chunk, DUMP_RAM_WIN); - if (rc < 0) { - errno = -rc; - perror("l2ab_read"); - break; - } else if (rc != DUMP_RAM_WIN) { - loge("Short read: %d\n", rc); - break; - } - - /* FIXME: Handle sub-strings at the right hand edge */ - needle = win_chunk; - while ((needle = memmem(needle, win_chunk + DUMP_RAM_WIN - needle, - arguments.mem_match, replace_len))) { - logi("0x%08zx: Replacing '%s' with '%s'\n", - ram_cursor + (needle - win_chunk), arguments.mem_match, arguments.mem_replace); - rc = ahb_write(ahb, ram_cursor + (needle - win_chunk), arguments.mem_replace, - strlen(arguments.mem_replace)); - if (rc < 0) { - errno = -rc; - perror("l2ab_write"); - break; - } else if ((size_t)rc != strlen(arguments.mem_replace)) { - loge("Short write: %d\n", rc); - break; - } - - if ((needle + replace_len) > win_chunk + DUMP_RAM_WIN) - break; - - needle += replace_len; - } - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct soc_region dram, vram; + size_t replace_len; + struct sdmc *sdmc; + size_t ram_cursor; + struct ahb *ahb; + void *win_chunk; + void *needle; + int rc; + + struct cmd_replace_args arguments = { 0 }; + rc = argp_parse(&cmd_replace_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + win_chunk = malloc(DUMP_RAM_WIN); + if (!win_chunk) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + rc = EXIT_FAILURE; + goto win_chunk_cleanup; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto host_cleanup; + } + + if ((rc = soc_probe(soc, ahb))) + goto host_cleanup; + + if (!(sdmc = sdmc_get(soc))) { + loge("Failed to acquire memory controller, exiting\n"); + rc = EXIT_FAILURE; + goto soc_cleanup; + } + + if ((rc = sdmc_get_dram(sdmc, &dram))) + goto soc_cleanup; + + if ((rc = sdmc_get_vram(sdmc, &vram))) + goto soc_cleanup; + + replace_len = strlen(arguments.mem_match); + for (ram_cursor = dram.start; ram_cursor < vram.start; + ram_cursor += DUMP_RAM_WIN) { + logi("Scanning BMC RAM in range 0x%08zx-0x%08zx\n", ram_cursor, + ram_cursor + DUMP_RAM_WIN - 1); + rc = ahb_read(ahb, ram_cursor, win_chunk, DUMP_RAM_WIN); + if (rc < 0) { + errno = -rc; + perror("l2ab_read"); + break; + } else if (rc != DUMP_RAM_WIN) { + loge("Short read: %d\n", rc); + break; + } + + /* FIXME: Handle sub-strings at the right hand edge */ + needle = win_chunk; + while ((needle = memmem(needle, + win_chunk + DUMP_RAM_WIN - needle, + arguments.mem_match, replace_len))) { + logi("0x%08zx: Replacing '%s' with '%s'\n", + ram_cursor + (needle - win_chunk), + arguments.mem_match, arguments.mem_replace); + rc = ahb_write(ahb, ram_cursor + (needle - win_chunk), + arguments.mem_replace, + strlen(arguments.mem_replace)); + if (rc < 0) { + errno = -rc; + perror("l2ab_write"); + break; + } else if ((size_t)rc != + strlen(arguments.mem_replace)) { + loge("Short write: %d\n", rc); + break; + } + + if ((needle + replace_len) > win_chunk + DUMP_RAM_WIN) + break; + + needle += replace_len; + } + } soc_cleanup: - soc_destroy(soc); + soc_destroy(soc); host_cleanup: - host_destroy(host); + host_destroy(host); win_chunk_cleanup: - free(win_chunk); + free(win_chunk); - return rc; + return rc; } static const struct cmd replace_cmd = { - .name = "replace", - .description = "Replace a portion in the memory", - .fn = do_replace, + .name = "replace", + .description = "Replace a portion in the memory", + .fn = do_replace, }; REGISTER_CMD(replace_cmd); diff --git a/src/cmd/reset.c b/src/cmd/reset.c index ab0a42c..7ecb63c 100644 --- a/src/cmd/reset.c +++ b/src/cmd/reset.c @@ -20,153 +20,163 @@ #include #include -static char cmd_reset_args_doc[] = "TARGET WDT [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; +static char cmd_reset_args_doc[] = + "TARGET WDT [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; static char cmd_reset_doc[] = - "\n" - "Reset command" - "\v" - "Supported targets:\n" - " soc Reset the whole SOC\n" - "\v" - "Examples:\n" - " Reset SoC hard via wdt3:\n" - " culvert reset soc wdt3\n\n" - " Reset SoC hard via wdt2 (may causes FMC CS switch):\n" - " culvert reset soc wdt2\n"; + "\n" + "Reset command" + "\v" + "Supported targets:\n" + " soc Reset the whole SOC\n" + "\v" + "Examples:\n" + " Reset SoC hard via wdt3:\n" + " culvert reset soc wdt3\n\n" + " Reset SoC hard via wdt2 (may causes FMC CS switch):\n" + " culvert reset soc wdt2\n"; static struct argp_option cmd_reset_options[] = { - {0}, + { 0 }, }; struct cmd_reset_args { - struct connection_args connection; - const char *wdt; + struct connection_args connection; + const char *wdt; }; static error_t cmd_reset_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_reset_args *arguments = state->input; - int rc = 0; - - switch (key) { - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - return 0; - } - - switch (state->arg_num) { - case 0: - /* Right now, we only support soc as reset target */ - if (strcmp("soc", arg)) - argp_error(state, "Invalid reset target '%s'", arg); - break; - case 1: - arguments->wdt = arg; - break; - } - break; - case ARGP_KEY_END: - if (state->arg_num < 2) - argp_error(state, "Not enough arguments provided. Need type and watchdog."); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_reset_args *arguments = state->input; + int rc = 0; + + switch (key) { + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + return 0; + } + + switch (state->arg_num) { + case 0: + /* Right now, we only support soc as reset target */ + if (strcmp("soc", arg)) + argp_error(state, "Invalid reset target '%s'", + arg); + break; + case 1: + arguments->wdt = arg; + break; + } + break; + case ARGP_KEY_END: + if (state->arg_num < 2) + argp_error( + state, + "Not enough arguments provided. Need type and watchdog."); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_reset_argp = { - .options = cmd_reset_options, - .parser = cmd_reset_parse_opt, - .args_doc = cmd_reset_args_doc, - .doc = cmd_reset_doc, + .options = cmd_reset_options, + .parser = cmd_reset_parse_opt, + .args_doc = cmd_reset_args_doc, + .doc = cmd_reset_doc, }; static int do_reset(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct ahb *ahb; - struct clk *clk; - struct wdt *wdt; - int cleanup, rc; - - struct cmd_reset_args arguments = {0}; - rc = argp_parse(&cmd_reset_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to acquire AHB interface, exiting: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - return rc; - } - - /* Probe the SoC */ - if ((rc = soc_probe(soc, ahb)) < 0) { - errno = -rc; - perror("soc_probe"); - goto cleanup_host; - } - - /* Initialise the required SoC drivers */ - if (!(clk = clk_get(soc))) { - loge("Failed to acquire clock controller, exiting\n"); - goto cleanup_soc; - } - - if (!(wdt = wdt_get_by_name(soc, arguments.wdt))) { - loge("Failed to acquire %s controller, exiting\n", arguments.wdt); - goto cleanup_soc; - } - - /* Do the reset */ - if (!ahb->drv->local) { - logi("Gating ARM clock\n"); - rc = clk_disable(clk, clk_arm); - if (rc < 0) - goto cleanup_soc; - } - - logi("Preventing system reset\n"); - if ((rc = wdt_prevent_reset(soc)) < 0) { - errno = -rc; - perror("wdt_prevent_reset"); - goto clk_enable_arm; - } - - /* wdt_perform_reset ungates the ARM if required */ - logi("Performing SoC reset\n"); - rc = wdt_perform_reset(wdt); - if (rc < 0) { + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct ahb *ahb; + struct clk *clk; + struct wdt *wdt; + int cleanup, rc; + + struct cmd_reset_args arguments = { 0 }; + rc = argp_parse(&cmd_reset_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to acquire AHB interface, exiting: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + return rc; + } + + /* Probe the SoC */ + if ((rc = soc_probe(soc, ahb)) < 0) { + errno = -rc; + perror("soc_probe"); + goto cleanup_host; + } + + /* Initialise the required SoC drivers */ + if (!(clk = clk_get(soc))) { + loge("Failed to acquire clock controller, exiting\n"); + goto cleanup_soc; + } + + if (!(wdt = wdt_get_by_name(soc, arguments.wdt))) { + loge("Failed to acquire %s controller, exiting\n", + arguments.wdt); + goto cleanup_soc; + } + + /* Do the reset */ + if (!ahb->drv->local) { + logi("Gating ARM clock\n"); + rc = clk_disable(clk, clk_arm); + if (rc < 0) + goto cleanup_soc; + } + + logi("Preventing system reset\n"); + if ((rc = wdt_prevent_reset(soc)) < 0) { + errno = -rc; + perror("wdt_prevent_reset"); + goto clk_enable_arm; + } + + /* wdt_perform_reset ungates the ARM if required */ + logi("Performing SoC reset\n"); + rc = wdt_perform_reset(wdt); + if (rc < 0) { clk_enable_arm: - if ((cleanup = clk_enable(clk, clk_arm)) < 0) { - errno = -cleanup; - perror("clk_enable"); - } - } + if ((cleanup = clk_enable(clk, clk_arm)) < 0) { + errno = -cleanup; + perror("clk_enable"); + } + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static const struct cmd reset_cmd = { - .name = "reset", - .description = "Reset a component of the BMC chip", - .fn = do_reset, + .name = "reset", + .description = "Reset a component of the BMC chip", + .fn = do_reset, }; REGISTER_CMD(reset_cmd); diff --git a/src/cmd/sfc.c b/src/cmd/sfc.c index 4b40c9a..37cc5f2 100644 --- a/src/cmd/sfc.c +++ b/src/cmd/sfc.c @@ -22,189 +22,195 @@ #define SFC_FLASH_WIN (64 << 10) static char cmd_sfc_args_doc[] = - " ADDRESS LENGTH [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; + " ADDRESS LENGTH [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; static char cmd_sfc_doc[] = - "\n" - "SFC (SPI Flash Controller) command" - "\v" - "Supported sfcs':\n" - " fmc Firmware memory controller\n" - "\v" - "Supported commands:\n" - " erase Erase data on the specified address and length\n" - " read Read data from the specified address and length to stdout\n" - " write Write data on the specified address and length from stdin\n"; + "\n" + "SFC (SPI Flash Controller) command" + "\v" + "Supported sfcs':\n" + " fmc Firmware memory controller\n" + "\v" + "Supported commands:\n" + " erase Erase data on the specified address and length\n" + " read Read data from the specified address and length to stdout\n" + " write Write data on the specified address and length from stdin\n"; static struct argp_option cmd_sfc_options[] = { - { "sfc", 's', "SFC", 0, "SFC to be used", 0 }, - { 0 }, + { "sfc", 's', "SFC", 0, "SFC to be used", 0 }, + { 0 }, }; enum flash_op { flash_op_read, flash_op_write, flash_op_erase }; struct cmd_sfc_args { - struct connection_args connection; - char *sfc; - uint32_t address; - uint32_t len; - enum flash_op operation; + struct connection_args connection; + char *sfc; + uint32_t address; + uint32_t len; + enum flash_op operation; }; static error_t cmd_sfc_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_sfc_args *arguments = state->input; - int rc; - - switch (key) { - case 's': - if (strcmp("fmc", arg)) - argp_error(state, "Invalid SFC '%s'", arg); - - arguments->sfc = arg; - break; - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - switch (state->arg_num) { - case 0: - if (!strcmp("erase", arg)) - arguments->operation = flash_op_erase; - else if (!strcmp("read", arg)) - arguments->operation = flash_op_read; - else if (!strcmp("write", arg)) - arguments->operation = flash_op_write; - else - argp_error(state, "Invalid command '%s'", arg); - break; - case 1: - arguments->address = strtoul(arg, NULL, 0); - break; - case 2: - arguments->len = strtoul(arg, NULL, 0); - break; - } - break; - case ARGP_KEY_END: - if (arguments->sfc == NULL) - argp_error(state, "No SFC defined."); - if (state->arg_num < 3) - argp_error(state, "Not enough arguments provided."); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_sfc_args *arguments = state->input; + int rc; + + switch (key) { + case 's': + if (strcmp("fmc", arg)) + argp_error(state, "Invalid SFC '%s'", arg); + + arguments->sfc = arg; + break; + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + switch (state->arg_num) { + case 0: + if (!strcmp("erase", arg)) + arguments->operation = flash_op_erase; + else if (!strcmp("read", arg)) + arguments->operation = flash_op_read; + else if (!strcmp("write", arg)) + arguments->operation = flash_op_write; + else + argp_error(state, "Invalid command '%s'", arg); + break; + case 1: + arguments->address = strtoul(arg, NULL, 0); + break; + case 2: + arguments->len = strtoul(arg, NULL, 0); + break; + } + break; + case ARGP_KEY_END: + if (arguments->sfc == NULL) + argp_error(state, "No SFC defined."); + if (state->arg_num < 3) + argp_error(state, "Not enough arguments provided."); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_sfc_argp = { - .options = cmd_sfc_options, - .parser = cmd_sfc_parse_opt, - .args_doc = cmd_sfc_args_doc, - .doc = cmd_sfc_doc, + .options = cmd_sfc_options, + .parser = cmd_sfc_parse_opt, + .args_doc = cmd_sfc_args_doc, + .doc = cmd_sfc_doc, }; static int do_sfc(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct flash_chip *chip; - struct sfc *sfc; - struct ahb *ahb; - char *buf; - int rc; - - struct cmd_sfc_args arguments = {0}; - rc = argp_parse(&cmd_sfc_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - rc = soc_probe(soc, ahb); - if (rc < 0) - goto cleanup_host; - - if (!(sfc = sfc_get_by_name(soc, arguments.sfc))) { - loge("Failed to acquire SPI controller, exiting\n"); - goto cleanup_soc; - } - - rc = flash_init(sfc, &chip); - if (rc < 0) - goto cleanup_soc; - - if (arguments.operation == flash_op_read) { - ssize_t egress; - - buf = malloc(arguments.len); - if (!buf) - goto cleanup_flash; - - rc = flash_read(chip, arguments.address, buf, arguments.len); - egress = write(1, buf, arguments.len); - if (egress == -1) { - rc = -errno; - perror("write"); - } - - free(buf); - } else if (arguments.operation == flash_op_write) { - ssize_t ingress; - - arguments.len = SFC_FLASH_WIN; - buf = malloc(arguments.len); - if (!buf) - goto cleanup_flash; - - while ((ingress = read(0, buf, arguments.len))) { - if (ingress < 0) { - rc = -errno; - break; - } - - rc = flash_write(chip, arguments.address, buf, ingress, true); - if (rc < 0) - break; - - arguments.address += ingress; - } - - free(buf); - } else if (arguments.operation == flash_op_erase) { - rc = flash_erase(chip, arguments.address, arguments.len); - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct flash_chip *chip; + struct sfc *sfc; + struct ahb *ahb; + char *buf; + int rc; + + struct cmd_sfc_args arguments = { 0 }; + rc = argp_parse(&cmd_sfc_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + rc = soc_probe(soc, ahb); + if (rc < 0) + goto cleanup_host; + + if (!(sfc = sfc_get_by_name(soc, arguments.sfc))) { + loge("Failed to acquire SPI controller, exiting\n"); + goto cleanup_soc; + } + + rc = flash_init(sfc, &chip); + if (rc < 0) + goto cleanup_soc; + + if (arguments.operation == flash_op_read) { + ssize_t egress; + + buf = malloc(arguments.len); + if (!buf) + goto cleanup_flash; + + rc = flash_read(chip, arguments.address, buf, arguments.len); + egress = write(1, buf, arguments.len); + if (egress == -1) { + rc = -errno; + perror("write"); + } + + free(buf); + } else if (arguments.operation == flash_op_write) { + ssize_t ingress; + + arguments.len = SFC_FLASH_WIN; + buf = malloc(arguments.len); + if (!buf) + goto cleanup_flash; + + while ((ingress = read(0, buf, arguments.len))) { + if (ingress < 0) { + rc = -errno; + break; + } + + rc = flash_write(chip, arguments.address, buf, ingress, + true); + if (rc < 0) + break; + + arguments.address += ingress; + } + + free(buf); + } else if (arguments.operation == flash_op_erase) { + rc = flash_erase(chip, arguments.address, arguments.len); + } cleanup_flash: - flash_destroy(chip); + flash_destroy(chip); cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static const struct cmd sfc_cmd = { - .name = "sfc", - .description = "Read, write or erase areas of a supported SFC", - .fn = do_sfc, + .name = "sfc", + .description = "Read, write or erase areas of a supported SFC", + .fn = do_sfc, }; REGISTER_CMD(sfc_cmd); diff --git a/src/cmd/trace.c b/src/cmd/trace.c index b94a769..5f223b5 100644 --- a/src/cmd/trace.c +++ b/src/cmd/trace.c @@ -23,78 +23,84 @@ #include static char cmd_trace_args_doc[] = - "ADDRESS WIDTH MODE [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; + "ADDRESS WIDTH MODE [via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; -static char cmd_trace_doc[] = - "\n" - "Trace command" - "\v" - "Supported widths: 1,2,4\n\n" - "Supported modes:\n" - " read Trace read operations\n" - " write Trace write operations\n"; +static char cmd_trace_doc[] = "\n" + "Trace command" + "\v" + "Supported widths: 1,2,4\n\n" + "Supported modes:\n" + " read Trace read operations\n" + " write Trace write operations\n"; static struct argp_option cmd_trace_options[] = { - {0}, + { 0 }, }; struct cmd_trace_args { - unsigned long address; - unsigned long width; - enum trace_mode mode; - struct connection_args connection; + unsigned long address; + unsigned long width; + enum trace_mode mode; + struct connection_args connection; }; static error_t cmd_trace_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_trace_args *arguments = state->input; - int rc; - - switch (key) { - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - switch (state->arg_num) { - case 0: - arguments->address = strtoul(arg, NULL, 0); - break; - case 1: - arguments->width = strtoul(arg, NULL, 0); - if (arguments->width != 1 && arguments->width != 2 - && arguments->width != 4) - argp_error(state, "Invalid access size '%lu'", arguments->width); - break; - case 2: - if (!strcmp(arg, "read")) - arguments->mode = trace_read; - else if (!strcmp(arg, "write")) - arguments->mode = trace_write; - else - argp_error(state, "Invalid mode '%s'", arg); - break; - } - break; - case ARGP_KEY_END: - if (arguments->address & (arguments->width - 1)) - argp_error(state, "Listening address must be aligned to the access size"); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_trace_args *arguments = state->input; + int rc; + + switch (key) { + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + switch (state->arg_num) { + case 0: + arguments->address = strtoul(arg, NULL, 0); + break; + case 1: + arguments->width = strtoul(arg, NULL, 0); + if (arguments->width != 1 && arguments->width != 2 && + arguments->width != 4) + argp_error(state, "Invalid access size '%lu'", + arguments->width); + break; + case 2: + if (!strcmp(arg, "read")) + arguments->mode = trace_read; + else if (!strcmp(arg, "write")) + arguments->mode = trace_write; + else + argp_error(state, "Invalid mode '%s'", arg); + break; + } + break; + case ARGP_KEY_END: + if (arguments->address & (arguments->width - 1)) + argp_error( + state, + "Listening address must be aligned to the access size"); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_trace_argp = { - .options = cmd_trace_options, - .parser = cmd_trace_parse_opt, - .args_doc = cmd_trace_args_doc, - .doc = cmd_trace_doc, + .options = cmd_trace_options, + .parser = cmd_trace_parse_opt, + .args_doc = cmd_trace_args_doc, + .doc = cmd_trace_doc, }; //culvert trace ADDRESS WIDTH:OFFSET MODE @@ -103,102 +109,103 @@ static struct argp cmd_trace_argp = { //culvert trace 0x1e788000 4:0 write static int do_trace(int argc, char **argv) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct trace *trace; - struct ahb *ahb; - sigset_t set; - int sig; - int rc; - - struct cmd_trace_args arguments = {0}; - rc = argp_parse(&cmd_trace_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - if (sigemptyset(&set)) { - rc = -errno; - loge("Unable to initialise signal set: %d\n", rc); - return rc; - } - - if (sigaddset(&set, SIGINT)) { - rc = -errno; - loge("Unable to add SIGINT to signal set: %d\n", rc); - return rc; - } - - if ((rc = host_init(host, &arguments.connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - exit(EXIT_FAILURE); - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) - goto cleanup_host; - - if (!(trace = trace_get(soc))) { - loge("Unable to acquire trace controller\n"); - goto cleanup_soc; - } - - if ((rc = trace_start(trace, arguments.address, - arguments.width, arguments.mode))) { - loge("Unable to start trace for 0x%08x %db %s: %d\n", - arguments.address, arguments.width, arguments.mode, rc); - goto cleanup_soc; - } - - /* - * The trace command waits for an unbounded amount of time while the trace - * is collected; it's possible the bridge state may change while we're - * waiting (for example if other culvert functions are being used). We - * attempt to handle this gracefully by releasing the bridge and then - * reinitializing it later when we need to dump the trace. - */ - if ((rc = host_bridge_release_from_ahb(ahb)) < 0) { - loge("Failed to release AHB bridge: %d\n", rc); - goto cleanup_soc; - } - - sigprocmask(SIG_BLOCK, &set, NULL); - if ((rc = sigwait(&set, &sig))) { - rc = -rc; - loge("Unable to wait for SIGINT: %d\n", rc); - goto cleanup_soc; - } - sigprocmask(SIG_UNBLOCK, &set, NULL); - - if ((rc = host_bridge_reinit_from_ahb(ahb)) < 0) { - loge("Failed to reinitialise AHB bridge: %d\n", rc); - goto cleanup_soc; - } - - if ((rc = trace_stop(trace))) { - loge("Unable to stop trace: %d\n"); - goto cleanup_soc; - } - - if ((rc = trace_dump(trace, 1))) - loge("Unable to dump trace to stdout: %d\n", rc); + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct trace *trace; + struct ahb *ahb; + sigset_t set; + int sig; + int rc; + + struct cmd_trace_args arguments = { 0 }; + rc = argp_parse(&cmd_trace_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + if (sigemptyset(&set)) { + rc = -errno; + loge("Unable to initialise signal set: %d\n", rc); + return rc; + } + + if (sigaddset(&set, SIGINT)) { + rc = -errno; + loge("Unable to add SIGINT to signal set: %d\n", rc); + return rc; + } + + if ((rc = host_init(host, &arguments.connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + exit(EXIT_FAILURE); + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) + goto cleanup_host; + + if (!(trace = trace_get(soc))) { + loge("Unable to acquire trace controller\n"); + goto cleanup_soc; + } + + if ((rc = trace_start(trace, arguments.address, arguments.width, + arguments.mode))) { + loge("Unable to start trace for 0x%08x %db %s: %d\n", + arguments.address, arguments.width, arguments.mode, rc); + goto cleanup_soc; + } + + /* + * The trace command waits for an unbounded amount of time while the trace + * is collected; it's possible the bridge state may change while we're + * waiting (for example if other culvert functions are being used). We + * attempt to handle this gracefully by releasing the bridge and then + * reinitializing it later when we need to dump the trace. + */ + if ((rc = host_bridge_release_from_ahb(ahb)) < 0) { + loge("Failed to release AHB bridge: %d\n", rc); + goto cleanup_soc; + } + + sigprocmask(SIG_BLOCK, &set, NULL); + if ((rc = sigwait(&set, &sig))) { + rc = -rc; + loge("Unable to wait for SIGINT: %d\n", rc); + goto cleanup_soc; + } + sigprocmask(SIG_UNBLOCK, &set, NULL); + + if ((rc = host_bridge_reinit_from_ahb(ahb)) < 0) { + loge("Failed to reinitialise AHB bridge: %d\n", rc); + goto cleanup_soc; + } + + if ((rc = trace_stop(trace))) { + loge("Unable to stop trace: %d\n"); + goto cleanup_soc; + } + + if ((rc = trace_dump(trace, 1))) + loge("Unable to dump trace to stdout: %d\n", rc); cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static const struct cmd trace_cmd = { - .name = "trace", - .description = "Trace what happens on a register", - .fn = do_trace, + .name = "trace", + .description = "Trace what happens on a register", + .fn = do_trace, }; REGISTER_CMD(trace_cmd); diff --git a/src/cmd/write.c b/src/cmd/write.c index e0b903f..662a19a 100644 --- a/src/cmd/write.c +++ b/src/cmd/write.c @@ -27,333 +27,345 @@ #define SFC_FLASH_WIN (64 << 10) static char cmd_write_args_doc[] = - "--type= [
] " - "[via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; + "--type= [
] " + "[via DRIVER [INTERFACE [IP PORT USERNAME PASSWORD]]]"; static char cmd_write_doc[] = - "\n" - "Write command" - "\v" - "NOTE: Only the 'ram' type can parse address and length and requires them!\n\n" - "All data will be read from stdin.\n\n" - "Supported types:\n" - " firmware Write stdin to the FMC\n" - " ram Write stdin to the memory\n"; + "\n" + "Write command" + "\v" + "NOTE: Only the 'ram' type can parse address and length and requires them!\n\n" + "All data will be read from stdin.\n\n" + "Supported types:\n" + " firmware Write stdin to the FMC\n" + " ram Write stdin to the memory\n"; enum cmd_write_mode { - none, - firmware, - ram, + none, + firmware, + ram, }; static struct argp_option cmd_write_options[] = { - { "type", 't', "TYPE", 0, "Type to be write to", 0 }, - {0}, + { "type", 't', "TYPE", 0, "Type to be write to", 0 }, + { 0 }, }; struct cmd_write_args { - unsigned long mem_base; - unsigned long mem_size; - struct connection_args connection; - enum cmd_write_mode mode; + unsigned long mem_base; + unsigned long mem_size; + struct connection_args connection; + enum cmd_write_mode mode; }; static error_t cmd_write_parse_opt(int key, char *arg, struct argp_state *state) { - struct cmd_write_args *arguments = state->input; - int rc; - - switch (key) { - case 't': - if (strcmp(arg, "firmware") && strcmp(arg, "ram")) - argp_error(state, "Invalid type '%s'", arg); - - if (!strcmp(arg, "firmware")) - arguments->mode = firmware; - else - arguments->mode = ram; - break; - case ARGP_KEY_ARG: - if (!strcmp(arg, "via")) { - rc = cmd_parse_via(state->next - 1, state, &arguments->connection); - if (rc != 0) - argp_error(state, "Failed to parse connection arguments. Returned code %d", rc); - break; - } - - /* If mode is not ram, skip any further args */ - if (arguments->mode != ram) - break; - - switch (state->arg_num) { - case 0: - parse_mem_arg(state, "write RAM base", &arguments->mem_base, arg); - break; - case 1: - parse_mem_arg(state, "write RAM size", &arguments->mem_size, arg); - break; - } - - break; - case ARGP_KEY_END: - if (arguments->mode == none) - argp_error(state, "No type to write to defined..."); - /* Other than the read command,this requires the arguments for ram */ - if (arguments->mode == ram && state->arg_num < 2) - argp_error(state, "Not enough arguments for ram mode..."); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct cmd_write_args *arguments = state->input; + int rc; + + switch (key) { + case 't': + if (strcmp(arg, "firmware") && strcmp(arg, "ram")) + argp_error(state, "Invalid type '%s'", arg); + + if (!strcmp(arg, "firmware")) + arguments->mode = firmware; + else + arguments->mode = ram; + break; + case ARGP_KEY_ARG: + if (!strcmp(arg, "via")) { + rc = cmd_parse_via(state->next - 1, state, + &arguments->connection); + if (rc != 0) + argp_error( + state, + "Failed to parse connection arguments. Returned code %d", + rc); + break; + } + + /* If mode is not ram, skip any further args */ + if (arguments->mode != ram) + break; + + switch (state->arg_num) { + case 0: + parse_mem_arg(state, "write RAM base", + &arguments->mem_base, arg); + break; + case 1: + parse_mem_arg(state, "write RAM size", + &arguments->mem_size, arg); + break; + } + + break; + case ARGP_KEY_END: + if (arguments->mode == none) + argp_error(state, "No type to write to defined..."); + /* Other than the read command,this requires the arguments for ram */ + if (arguments->mode == ram && state->arg_num < 2) + argp_error(state, + "Not enough arguments for ram mode..."); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } static struct argp cmd_write_argp = { - .options = cmd_write_options, - .parser = cmd_write_parse_opt, - .args_doc = cmd_write_args_doc, - .doc = cmd_write_doc, + .options = cmd_write_options, + .parser = cmd_write_parse_opt, + .args_doc = cmd_write_args_doc, + .doc = cmd_write_doc, }; static int cmd_write_firmware(struct cmd_write_args *arguments) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct ahb *ahb; - struct flash_chip *chip; - struct vuart *vuart; - struct clk *clk; - ssize_t ingress; - struct sfc *sfc; - int rc, cleanup; - uint32_t phys; - char *buf; - - if ((rc = host_init(host, &arguments->connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = -ENODEV; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - loge("Failed to probe SoC: %d\n", rc); - goto cleanup_host; - } - - if (!(clk = clk_get(soc))) { - loge("Failed to acquire clock controller, exiting\n"); - rc = -ENODEV; - goto cleanup_soc; - } - - if (!(vuart = vuart_get_by_name(soc, "vuart"))) { - loge("Failed to acquire VUART controller, exiting\n"); - rc = -ENODEV; - goto cleanup_soc; - } - - if (ahb->drv->local) - loge("I hope you know what you are doing\n"); - else { - logi("Preventing system reset\n"); - if ((rc = wdt_prevent_reset(soc)) < 0) - goto cleanup_state; - - logi("Gating ARM clock\n"); - if ((rc = clk_disable(clk, clk_arm)) < 0) - goto cleanup_state; - - logi("Configuring VUART for host Tx discard\n"); - if ((rc = vuart_set_host_tx_discard(vuart, discard_enable)) < 0) - goto cleanup_state; - } - - logi("Initialising flash subsystem\n"); - if (!(sfc = sfc_get_by_name(soc, "fmc"))) { - loge("Failed to acquire SPI flash controller, exiting\n"); - rc = -ENODEV; - goto cleanup_state; - } - - rc = flash_init(sfc, &chip); - if (rc < 0) - goto cleanup_state; - - /* FIXME: Make this common with the sfc write implementation */ - buf = malloc(SFC_FLASH_WIN); - if (!buf) { - rc = -ENOMEM; - goto cleanup_flash; - } - - logi("Writing firmware image\n"); - phys = 0; - while ((ingress = read(0, buf, SFC_FLASH_WIN))) { - if (ingress < 0) { - rc = -errno; - break; - } - - do { - if (ingress < SFC_FLASH_WIN) { - loge("Unexpected ingress value: 0x%zx\n", ingress); - goto cleanup_buf; - } - - rc = flash_erase(chip, phys, ingress); - if (rc < 0) - goto cleanup_buf; - - rc = flash_write(chip, phys, buf, ingress, true); - } while (rc == -EREMOTEIO); /* Miscompare */ - - if (rc) - break; - - phys += ingress; - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct ahb *ahb; + struct flash_chip *chip; + struct vuart *vuart; + struct clk *clk; + ssize_t ingress; + struct sfc *sfc; + int rc, cleanup; + uint32_t phys; + char *buf; + + if ((rc = host_init(host, &arguments->connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = -ENODEV; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + loge("Failed to probe SoC: %d\n", rc); + goto cleanup_host; + } + + if (!(clk = clk_get(soc))) { + loge("Failed to acquire clock controller, exiting\n"); + rc = -ENODEV; + goto cleanup_soc; + } + + if (!(vuart = vuart_get_by_name(soc, "vuart"))) { + loge("Failed to acquire VUART controller, exiting\n"); + rc = -ENODEV; + goto cleanup_soc; + } + + if (ahb->drv->local) + loge("I hope you know what you are doing\n"); + else { + logi("Preventing system reset\n"); + if ((rc = wdt_prevent_reset(soc)) < 0) + goto cleanup_state; + + logi("Gating ARM clock\n"); + if ((rc = clk_disable(clk, clk_arm)) < 0) + goto cleanup_state; + + logi("Configuring VUART for host Tx discard\n"); + if ((rc = vuart_set_host_tx_discard(vuart, discard_enable)) < 0) + goto cleanup_state; + } + + logi("Initialising flash subsystem\n"); + if (!(sfc = sfc_get_by_name(soc, "fmc"))) { + loge("Failed to acquire SPI flash controller, exiting\n"); + rc = -ENODEV; + goto cleanup_state; + } + + rc = flash_init(sfc, &chip); + if (rc < 0) + goto cleanup_state; + + /* FIXME: Make this common with the sfc write implementation */ + buf = malloc(SFC_FLASH_WIN); + if (!buf) { + rc = -ENOMEM; + goto cleanup_flash; + } + + logi("Writing firmware image\n"); + phys = 0; + while ((ingress = read(0, buf, SFC_FLASH_WIN))) { + if (ingress < 0) { + rc = -errno; + break; + } + + do { + if (ingress < SFC_FLASH_WIN) { + loge("Unexpected ingress value: 0x%zx\n", + ingress); + goto cleanup_buf; + } + + rc = flash_erase(chip, phys, ingress); + if (rc < 0) + goto cleanup_buf; + + rc = flash_write(chip, phys, buf, ingress, true); + } while (rc == -EREMOTEIO); /* Miscompare */ + + if (rc) + break; + + phys += ingress; + } cleanup_buf: - free(buf); + free(buf); cleanup_flash: - flash_destroy(chip); + flash_destroy(chip); cleanup_state: - if (rc == 0) { - if (!ahb->drv->local) { - struct wdt *wdt; - - logi("Performing SoC reset\n"); - if (!(wdt = wdt_get_by_name(soc, "wdt2"))) { - loge("Failed to acquire wdt2 controller, exiting\n"); - return -ENODEV; - } - - rc = wdt_perform_reset(wdt); - if (rc < 0) { - return rc; - } - } - } else { - logi("Deconfiguring VUART host Tx discard\n"); - if ((cleanup = vuart_set_host_tx_discard(vuart, discard_disable))) { - errno = -cleanup; - perror("vuart_set_host_tx_discard"); - } - - logi("Ungating ARM clock\n"); - if ((cleanup = clk_enable(clk, clk_arm))) { - errno = -cleanup; - perror("clk_enable"); - } - } + if (rc == 0) { + if (!ahb->drv->local) { + struct wdt *wdt; + + logi("Performing SoC reset\n"); + if (!(wdt = wdt_get_by_name(soc, "wdt2"))) { + loge("Failed to acquire wdt2 controller, exiting\n"); + return -ENODEV; + } + + rc = wdt_perform_reset(wdt); + if (rc < 0) { + return rc; + } + } + } else { + logi("Deconfiguring VUART host Tx discard\n"); + if ((cleanup = vuart_set_host_tx_discard(vuart, + discard_disable))) { + errno = -cleanup; + perror("vuart_set_host_tx_discard"); + } + + logi("Ungating ARM clock\n"); + if ((cleanup = clk_enable(clk, clk_arm))) { + errno = -cleanup; + perror("clk_enable"); + } + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static int cmd_write_ram(struct cmd_write_args *arguments) { - struct host _host, *host = &_host; - struct soc _soc, *soc = &_soc; - struct soc_region dram; - struct sdmc *sdmc; - struct ahb *ahb; - int rc; - - if ((rc = host_init(host, &arguments->connection)) < 0) { - loge("Failed to initialise host interfaces: %d\n", rc); - return rc; - } - - if (!(ahb = host_get_ahb(host))) { - loge("Failed to acquire AHB interface, exiting\n"); - rc = EXIT_FAILURE; - goto cleanup_host; - } - - if ((rc = soc_probe(soc, ahb)) < 0) { - loge("Failed to probe SoC: %d\n", rc); - goto cleanup_host; - } - - if (!(sdmc = sdmc_get(soc))) { - loge("Failed to acquire SDRAM memory controller\n"); - rc = -ENODEV; - goto cleanup_soc; - } - - if ((rc = sdmc_get_dram(sdmc, &dram))) { - loge("Failed to locate DRAM: %d\n", rc); - goto cleanup_soc; - } - - if (arguments->mem_base < dram.start || - (arguments->mem_base + arguments->mem_size) > (dram.start + dram.length)) { - loge("Ill-formed RAM region provided for write\n"); - rc = -EINVAL; - goto cleanup_soc; - } + struct host _host, *host = &_host; + struct soc _soc, *soc = &_soc; + struct soc_region dram; + struct sdmc *sdmc; + struct ahb *ahb; + int rc; + + if ((rc = host_init(host, &arguments->connection)) < 0) { + loge("Failed to initialise host interfaces: %d\n", rc); + return rc; + } + + if (!(ahb = host_get_ahb(host))) { + loge("Failed to acquire AHB interface, exiting\n"); + rc = EXIT_FAILURE; + goto cleanup_host; + } + + if ((rc = soc_probe(soc, ahb)) < 0) { + loge("Failed to probe SoC: %d\n", rc); + goto cleanup_host; + } + + if (!(sdmc = sdmc_get(soc))) { + loge("Failed to acquire SDRAM memory controller\n"); + rc = -ENODEV; + goto cleanup_soc; + } + + if ((rc = sdmc_get_dram(sdmc, &dram))) { + loge("Failed to locate DRAM: %d\n", rc); + goto cleanup_soc; + } + + if (arguments->mem_base < dram.start || + (arguments->mem_base + arguments->mem_size) > + (dram.start + dram.length)) { + loge("Ill-formed RAM region provided for write\n"); + rc = -EINVAL; + goto cleanup_soc; + } #if UINT32_MAX > SSIZE_MAX - if (SSIZE_MAX < arguments->mem_size) - return -EINVAL; + if (SSIZE_MAX < arguments->mem_size) + return -EINVAL; #endif - if ((rc = soc_siphon_in(soc, arguments->mem_base, arguments->mem_size, STDIN_FILENO))) { - loge("Failed to write to provided memory region: %d\n", rc); - } + if ((rc = soc_siphon_in(soc, arguments->mem_base, arguments->mem_size, + STDIN_FILENO))) { + loge("Failed to write to provided memory region: %d\n", rc); + } cleanup_soc: - soc_destroy(soc); + soc_destroy(soc); cleanup_host: - host_destroy(host); + host_destroy(host); - return rc; + return rc; } static int do_write(int argc, char **argv) { - int rc; - - struct cmd_write_args arguments = {0}; - rc = argp_parse(&cmd_write_argp, argc, argv, ARGP_IN_ORDER, 0, &arguments); - if (rc != 0) - return rc; - - switch (arguments.mode) { - case firmware: - rc = cmd_write_firmware(&arguments); - break; - case ram: - rc = cmd_write_ram(&arguments); - break; - /* If it reaches none here, then the argument parse logic is broken. */ - case none: - loge("read: Reached 'none' mode after argument parsing. This is a bug!\n"); - rc = -EINVAL; - break; - } - - return rc; + int rc; + + struct cmd_write_args arguments = { 0 }; + rc = argp_parse(&cmd_write_argp, argc, argv, ARGP_IN_ORDER, 0, + &arguments); + if (rc != 0) + return rc; + + switch (arguments.mode) { + case firmware: + rc = cmd_write_firmware(&arguments); + break; + case ram: + rc = cmd_write_ram(&arguments); + break; + /* If it reaches none here, then the argument parse logic is broken. */ + case none: + loge("read: Reached 'none' mode after argument parsing. This is a bug!\n"); + rc = -EINVAL; + break; + } + + return rc; } static const struct cmd write_cmd = { - .name = "write", - .description = "Write data to the FMC or RAM", - .fn = do_write, + .name = "write", + .description = "Write data to the FMC or RAM", + .fn = do_write, }; REGISTER_CMD(write_cmd); diff --git a/src/connection.h b/src/connection.h index f77b918..6253a66 100644 --- a/src/connection.h +++ b/src/connection.h @@ -19,28 +19,28 @@ * then you may have to set it on your own. */ struct connection_args { - /** Bridge implementation to be used */ - struct bridge_driver *bridge_driver; + /** Bridge implementation to be used */ + struct bridge_driver *bridge_driver; - /** BMC interface path (e.g. /dev/ttyUSB0) */ - const char *interface; + /** BMC interface path (e.g. /dev/ttyUSB0) */ + const char *interface; - /** IP address of the console server */ - const char *ip; + /** IP address of the console server */ + const char *ip; - /** Username for connecting to the console server */ - const char *username; + /** Username for connecting to the console server */ + const char *username; - /** Password for connecting to the console server */ - const char *password; + /** Password for connecting to the console server */ + const char *password; - /** Port for connecting to the console server */ - int port; + /** Port for connecting to the console server */ + int port; - /** - * Internal flag that must be true if console server fields - * (IP, username, etc.) are set. - */ - bool internet_args; + /** + * Internal flag that must be true if console server fields + * (IP, username, etc.) are set. + */ + bool internet_args; }; #endif diff --git a/src/console.h b/src/console.h index 2d95c66..63ec980 100644 --- a/src/console.h +++ b/src/console.h @@ -7,22 +7,22 @@ struct console; struct console_ops { - int (*destroy)(struct console *ctx); - int (*set_baud)(struct console *ctx, int baud); + int (*destroy)(struct console *ctx); + int (*set_baud)(struct console *ctx, int baud); }; struct console { - const struct console_ops *ops; + const struct console_ops *ops; }; static inline int console_destroy(struct console *ctx) { - return ctx->ops->destroy(ctx); + return ctx->ops->destroy(ctx); } static inline int console_set_baud(struct console *ctx, int baud) { - return ctx->ops->set_baud(ctx, baud); + return ctx->ops->set_baud(ctx, baud); } #endif diff --git a/src/culvert.c b/src/culvert.c index a8df37d..c479843 100644 --- a/src/culvert.c +++ b/src/culvert.c @@ -27,164 +27,167 @@ const char *argp_program_version = "culvert " CULVERT_VERSION; const char *argp_program_bug_address = "GitHub amboar/culvert"; struct root_arguments { - char *name; - char **args; - int argc; - struct cmd *cmd; + char *name; + char **args; + int argc; + struct cmd *cmd; }; static struct argp_option root_options[] = { - { "verbose", 'v', 0, 0, "Get verbose output", 0 }, - { "quiet", 'q', 0, 0, "Don't produce any output", 0 }, - { "skip-bridge", 's', "BRIDGE", 0, "Skip BRIDGE driver", 0 }, - { "list-bridges", 'l', 0, 0, "List available bridge drivers", 0 }, - {0} + { "verbose", 'v', 0, 0, "Get verbose output", 0 }, + { "quiet", 'q', 0, 0, "Don't produce any output", 0 }, + { "skip-bridge", 's', "BRIDGE", 0, "Skip BRIDGE driver", 0 }, + { "list-bridges", 'l', 0, 0, "List available bridge drivers", 0 }, + { 0 } }; static char *make_root_doc(struct cmd **cmds, size_t n_cmds) { - char *doc = NULL; - if (asprintf(&doc, "\nCulvert — A Test and Debug Tool for BMC AHB Interfaces\n" - "\vAvailable commands:\n") < 0) - return NULL; - - for (size_t i = 0; i < n_cmds; i++) { - char *new_doc; - if (asprintf(&new_doc, "%s %-20s %s\n", doc, cmds[i]->name, - cmds[i]->description ? cmds[i]->description : "") < 0) { - free(doc); - return NULL; - } - free(doc); - doc = new_doc; - } - - return doc; + char *doc = NULL; + if (asprintf(&doc, + "\nCulvert — A Test and Debug Tool for BMC AHB Interfaces\n" + "\vAvailable commands:\n") < 0) + return NULL; + + for (size_t i = 0; i < n_cmds; i++) { + char *new_doc; + if (asprintf(&new_doc, "%s %-20s %s\n", doc, cmds[i]->name, + cmds[i]->description ? cmds[i]->description : "") < + 0) { + free(doc); + return NULL; + } + free(doc); + doc = new_doc; + } + + return doc; } -static error_t -parse_root_opt(int key, char *arg, struct argp_state *state) +static error_t parse_root_opt(int key, char *arg, struct argp_state *state) { - struct root_arguments *arguments = state->input; - - switch (key) { - case 'q': - log_set_level(level_none); - break; - case 'v': - log_set_level(level_trace); - break; - case 's': - if (disable_bridge_driver(arg)) { - fprintf(stderr, "Error: '%s' not a recognized bridge name (use '-l' to list)\n", arg); - return -EINVAL; - } - break; - case 'l': - print_bridge_drivers(); - /* Early exit as we only print the bridge drivers */ - exit(EXIT_SUCCESS); - break; - case ARGP_KEY_ARG: - /* Ensure that only the first argument is being used as name */ - if (!arguments->name) - arguments->name = arg; - else - argp_usage(state); - - /* Skip other arguments but save them to args array */ - arguments->args = &state->argv[state->next]; - arguments->argc = state->argc - state->next; - state->next = state->argc; - break; - case ARGP_KEY_END: - if (!arguments->name) - argp_usage(state); - break; - default: - return ARGP_ERR_UNKNOWN; - } - - return 0; + struct root_arguments *arguments = state->input; + + switch (key) { + case 'q': + log_set_level(level_none); + break; + case 'v': + log_set_level(level_trace); + break; + case 's': + if (disable_bridge_driver(arg)) { + fprintf(stderr, + "Error: '%s' not a recognized bridge name (use '-l' to list)\n", + arg); + return -EINVAL; + } + break; + case 'l': + print_bridge_drivers(); + /* Early exit as we only print the bridge drivers */ + exit(EXIT_SUCCESS); + break; + case ARGP_KEY_ARG: + /* Ensure that only the first argument is being used as name */ + if (!arguments->name) + arguments->name = arg; + else + argp_usage(state); + + /* Skip other arguments but save them to args array */ + arguments->args = &state->argv[state->next]; + arguments->argc = state->argc - state->next; + state->next = state->argc; + break; + case ARGP_KEY_END: + if (!arguments->name) + argp_usage(state); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; } int main(int argc, char **argv) { - struct cmd **cmds; - struct root_arguments args = {0}; - size_t n_cmds = 0; - int rc = 0; - - /* - * Always initialise the log level and set a different level if an - * argument is passed. - */ - log_set_level(level_info); - - /* Get all commands via autodata */ - cmds = autodata_get(cmds, &n_cmds); - qsort(cmds, n_cmds, sizeof(void *), cmd_cmp); - - char *doc = make_root_doc(cmds, n_cmds); - - /* argp struct must be in main because of doc generation. */ - struct argp root_argp = { - .options = root_options, - .parser = parse_root_opt, - .args_doc = " [CMD_OPTIONS]...", - .doc = doc, - }; - - /* - * Parse command-line arguments - * NOTE: `ARGP_IN_ORDER` is required to make sub-commands work - * properly and to enforce options to be in order. - */ - rc = argp_parse(&root_argp, argc, argv, ARGP_IN_ORDER, 0, &args); - free(doc); - if (rc != 0) - goto cleanup_cmds; - - /* If for some reason args.name is still empty but rc is 0, break here */ - if (args.name == 0) - goto cleanup_cmds; - - struct cmd *selected = NULL; - for (size_t i = 0; i < n_cmds; i++) { - if (!strcmp(args.name, cmds[i]->name)) { - selected = cmds[i]; - break; - } - } - - if (!selected) { - fprintf(stderr, "Unknown command: %s\n", args.name); - rc = -EINVAL; - goto cleanup_cmds; - } - - /* Allocate new array for the command */ - char **subcmd_argv = calloc(args.argc + 1, sizeof(char *)); - if (!subcmd_argv) { - rc = -ENOMEM; - goto cleanup_cmds; - } - - /* Set argv[0] to a combined name like "culvert coprocessor" */ - if (asprintf(&subcmd_argv[0], "%s %s", argv[0], args.name) < 0) { - rc = -ENOMEM; - goto cleanup_cmds; - } - - /* Copy the remaining arguments from args.args */ - for (int i = 0; i < args.argc; i++) - subcmd_argv[i + 1] = args.args[i]; - - rc = selected->fn(args.argc + 1, subcmd_argv); - free(subcmd_argv[0]); - free(subcmd_argv); + struct cmd **cmds; + struct root_arguments args = { 0 }; + size_t n_cmds = 0; + int rc = 0; + + /* + * Always initialise the log level and set a different level if an + * argument is passed. + */ + log_set_level(level_info); + + /* Get all commands via autodata */ + cmds = autodata_get(cmds, &n_cmds); + qsort(cmds, n_cmds, sizeof(void *), cmd_cmp); + + char *doc = make_root_doc(cmds, n_cmds); + + /* argp struct must be in main because of doc generation. */ + struct argp root_argp = { + .options = root_options, + .parser = parse_root_opt, + .args_doc = " [CMD_OPTIONS]...", + .doc = doc, + }; + + /* + * Parse command-line arguments + * NOTE: `ARGP_IN_ORDER` is required to make sub-commands work + * properly and to enforce options to be in order. + */ + rc = argp_parse(&root_argp, argc, argv, ARGP_IN_ORDER, 0, &args); + free(doc); + if (rc != 0) + goto cleanup_cmds; + + /* If for some reason args.name is still empty but rc is 0, break here */ + if (args.name == 0) + goto cleanup_cmds; + + struct cmd *selected = NULL; + for (size_t i = 0; i < n_cmds; i++) { + if (!strcmp(args.name, cmds[i]->name)) { + selected = cmds[i]; + break; + } + } + + if (!selected) { + fprintf(stderr, "Unknown command: %s\n", args.name); + rc = -EINVAL; + goto cleanup_cmds; + } + + /* Allocate new array for the command */ + char **subcmd_argv = calloc(args.argc + 1, sizeof(char *)); + if (!subcmd_argv) { + rc = -ENOMEM; + goto cleanup_cmds; + } + + /* Set argv[0] to a combined name like "culvert coprocessor" */ + if (asprintf(&subcmd_argv[0], "%s %s", argv[0], args.name) < 0) { + rc = -ENOMEM; + goto cleanup_cmds; + } + + /* Copy the remaining arguments from args.args */ + for (int i = 0; i < args.argc; i++) + subcmd_argv[i + 1] = args.args[i]; + + rc = selected->fn(args.argc + 1, subcmd_argv); + free(subcmd_argv[0]); + free(subcmd_argv); cleanup_cmds: - autodata_free(cmds); - exit(rc ? EXIT_FAILURE : EXIT_SUCCESS); + autodata_free(cmds); + exit(rc ? EXIT_FAILURE : EXIT_SUCCESS); } diff --git a/src/devicetree/meson.build b/src/devicetree/meson.build index 64c7ea6..8430323 100644 --- a/src/devicetree/meson.build +++ b/src/devicetree/meson.build @@ -5,58 +5,82 @@ cc_prog = meson.get_compiler('c').cmd_array() # TODO: Figure out how to use a generator -cpp_cmd = [ cc_prog, '-E', - '-x', 'assembler-with-cpp', - '-nostdinc', - '-I', '@SOURCE_ROOT@' / 'src', - '-o', '@OUTPUT@', - '@INPUT@' ] -dtc_cmd = [ dtc_prog, '@INPUT@', '-O', 'dtb', '-o', '@OUTPUT@' ] -ld_cmd = [ ld_prog, '-r', '-b', 'binary', '@INPUT@', '-o', '@OUTPUT@'] - -g4_dts_i = custom_target('g4_dts', - input: 'g4.dts', - output: 'g4.dts.i', - command: cpp_cmd) - -g4_dtb = custom_target('g4_dtb', - input: g4_dts_i, - output: 'g4.dtb', - command: dtc_cmd) - -g4_dtbo = custom_target('g4_dtbo', - input: g4_dtb, - output: 'g4.dtb.o', - command: ld_cmd) - -g5_dts_i = custom_target('g5_dts', - input: 'g5.dts', - output: 'g5.dts.i', - command: cpp_cmd) - -g5_dtb = custom_target('g5_dtb', - input: g5_dts_i, - output: 'g5.dtb', - command: dtc_cmd) - -g5_dtbo = custom_target('g5_dtbo', - input: g5_dtb, - output: 'g5.dtb.o', - command: ld_cmd) - -g6_dts_i = custom_target('g6_dts', - input: 'g6.dts', - output: 'g6.dts.i', - command: cpp_cmd) - -g6_dtb = custom_target('g6_dtb', - input: g6_dts_i, - output: 'g6.dtb', - command: dtc_cmd) - -g6_dtbo = custom_target('g6_dtbo', - input: g6_dtb, - output: 'g6.dtb.o', - command: ld_cmd) - -dtbos = [ g4_dtbo, g5_dtbo, g6_dtbo ] +cpp_cmd = [ + cc_prog, + '-E', + '-x', + 'assembler-with-cpp', + '-nostdinc', + '-I', + '@SOURCE_ROOT@' / 'src', + '-o', + '@OUTPUT@', + '@INPUT@', +] +dtc_cmd = [dtc_prog, '@INPUT@', '-O', 'dtb', '-o', '@OUTPUT@'] +ld_cmd = [ld_prog, '-r', '-b', 'binary', '@INPUT@', '-o', '@OUTPUT@'] + +g4_dts_i = custom_target( + 'g4_dts', + input: 'g4.dts', + output: 'g4.dts.i', + command: cpp_cmd, +) + +g4_dtb = custom_target( + 'g4_dtb', + input: g4_dts_i, + output: 'g4.dtb', + command: dtc_cmd, +) + +g4_dtbo = custom_target( + 'g4_dtbo', + input: g4_dtb, + output: 'g4.dtb.o', + command: ld_cmd, +) + +g5_dts_i = custom_target( + 'g5_dts', + input: 'g5.dts', + output: 'g5.dts.i', + command: cpp_cmd, +) + +g5_dtb = custom_target( + 'g5_dtb', + input: g5_dts_i, + output: 'g5.dtb', + command: dtc_cmd, +) + +g5_dtbo = custom_target( + 'g5_dtbo', + input: g5_dtb, + output: 'g5.dtb.o', + command: ld_cmd, +) + +g6_dts_i = custom_target( + 'g6_dts', + input: 'g6.dts', + output: 'g6.dts.i', + command: cpp_cmd, +) + +g6_dtb = custom_target( + 'g6_dtb', + input: g6_dts_i, + output: 'g6.dtb', + command: dtc_cmd, +) + +g6_dtbo = custom_target( + 'g6_dtbo', + input: g6_dtb, + output: 'g6.dtb.o', + command: ld_cmd, +) + +dtbos = [g4_dtbo, g5_dtbo, g6_dtbo] diff --git a/src/flash.c b/src/flash.c index fa183fd..d2fe981 100644 --- a/src/flash.c +++ b/src/flash.c @@ -13,39 +13,46 @@ #include "log.h" #ifndef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif #define FL_ERR(fmt, ...) loge(fmt, ##__VA_ARGS__) #define FL_DBG(fmt, ...) logd(fmt, ##__VA_ARGS__) static const struct flash_info flash_info[] = { - { 0xc22018, 0x01000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MXxxL12835F"}, - { 0xc22019, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MXxxL25635F"}, - { 0xc2201a, 0x04000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MXxxL51235F"}, - { 0xc2201b, 0x08000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MX66L1G45G"}, - { 0xef4018, 0x01000000, FL_ERASE_ALL, "Winbond W25Q128BV" }, - { 0xef4019, 0x02000000, FL_ERASE_ALL | FL_ERASE_64K | FL_CAN_4B | - FL_ERASE_BULK, - "Winbond W25Q256BV"}, - { 0xef4020, 0x04000000, FL_ERASE_ALL | FL_ERASE_64K | FL_CAN_4B | - FL_ERASE_BULK, - "Winbond W25Q512JV"}, - { 0xef4021, 0x08000000, FL_ERASE_ALL | FL_ERASE_64K | FL_CAN_4B | - FL_ERASE_BULK, - "Winbond W25Q01JV"}, - { 0x20ba20, 0x04000000, FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B | - FL_ERASE_BULK | FL_MICRON_BUGS, - "Micron N25Qx512Ax" }, - { 0x20ba19, 0x02000000, FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B | - FL_ERASE_BULK | FL_MICRON_BUGS, - "Micron N25Q256Ax" }, - { 0x1940ef, 0x02000000, FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B | - FL_ERASE_BULK | FL_MICRON_BUGS, - "Micron N25Qx256Ax" }, - { 0x4d5444, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "File Abstraction"}, + { 0xc22018, 0x01000000, FL_ERASE_ALL | FL_CAN_4B, + "Macronix MXxxL12835F" }, + { 0xc22019, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, + "Macronix MXxxL25635F" }, + { 0xc2201a, 0x04000000, FL_ERASE_ALL | FL_CAN_4B, + "Macronix MXxxL51235F" }, + { 0xc2201b, 0x08000000, FL_ERASE_ALL | FL_CAN_4B, + "Macronix MX66L1G45G" }, + { 0xef4018, 0x01000000, FL_ERASE_ALL, "Winbond W25Q128BV" }, + { 0xef4019, 0x02000000, + FL_ERASE_ALL | FL_ERASE_64K | FL_CAN_4B | FL_ERASE_BULK, + "Winbond W25Q256BV" }, + { 0xef4020, 0x04000000, + FL_ERASE_ALL | FL_ERASE_64K | FL_CAN_4B | FL_ERASE_BULK, + "Winbond W25Q512JV" }, + { 0xef4021, 0x08000000, + FL_ERASE_ALL | FL_ERASE_64K | FL_CAN_4B | FL_ERASE_BULK, + "Winbond W25Q01JV" }, + { 0x20ba20, 0x04000000, + FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B | FL_ERASE_BULK | + FL_MICRON_BUGS, + "Micron N25Qx512Ax" }, + { 0x20ba19, 0x02000000, + FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B | FL_ERASE_BULK | + FL_MICRON_BUGS, + "Micron N25Q256Ax" }, + { 0x1940ef, 0x02000000, + FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B | FL_ERASE_BULK | + FL_MICRON_BUGS, + "Micron N25Qx256Ax" }, + { 0x4d5444, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "File Abstraction" }, { 0x55aa55, 0x00100000, FL_ERASE_ALL | FL_CAN_4B, "TEST_FLASH" }, - { 0xaa55aa, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "EMULATED_FLASH"}, + { 0xaa55aa, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "EMULATED_FLASH" }, }; static int fl_read_stat(struct sfc *ct, uint8_t *stat) @@ -74,7 +81,8 @@ static int fl_sync_wait_idle(struct sfc *ct) /* XXX Add timeout */ for (;;) { rc = fl_read_stat(ct, &stat); - if (rc) return rc; + if (rc) + return rc; if (!(stat & STAT_WIP)) { if (ct->finfo->flags & FL_MICRON_BUGS) fl_micron_status(ct); @@ -93,9 +101,11 @@ static int fl_wren(struct sfc *ct) /* Some flashes need it to be hammered */ for (i = 0; i < 1000; i++) { rc = ct->cmd_wr(ct, CMD_WREN, false, 0, NULL, 0); - if (rc) return rc; + if (rc) + return rc; rc = fl_read_stat(ct, &stat); - if (rc) return rc; + if (rc) + return rc; if (stat & STAT_WIP) { FL_ERR("LIBFLASH: WREN has WIP status set !\n"); rc = fl_sync_wait_idle(ct); @@ -178,15 +188,15 @@ int flash_erase(struct flash_chip *c, uint64_t dst, uint64_t size) if ((dst | size) & c->min_erase_mask) return -EINVAL; - FL_DBG("LIBFLASH: Erasing 0x%" PRIx64"..0%" PRIx64 "...\n", - dst, dst + size); + FL_DBG("LIBFLASH: Erasing 0x%" PRIx64 "..0%" PRIx64 "...\n", dst, + dst + size); /* Use controller erase if supported */ if (ct->erase) return ct->erase(ct, dst, size); /* Allright, loop as long as there's something to erase */ - while(size) { + while (size) { /* How big can we make it based on alignent & size */ fl_get_best_erase(c, dst, size, &chunk, &cmd); @@ -222,17 +232,18 @@ int flash_erase_chip(struct flash_chip *c) int rc; /* XXX TODO: Fallback to using normal erases */ - if (!(c->info.flags & (FL_ERASE_CHIP|FL_ERASE_BULK))) + if (!(c->info.flags & (FL_ERASE_CHIP | FL_ERASE_BULK))) return -EOPNOTSUPP; FL_DBG("LIBFLASH: Erasing chip...\n"); - + /* Use controller erase if supported */ if (ct->erase) return ct->erase(ct, 0, 0xffffffff); rc = fl_wren(ct); - if (rc) return rc; + if (rc) + return rc; if (c->info.flags & FL_ERASE_CHIP) rc = ct->cmd_wr(ct, CMD_CE, false, 0, NULL, 0); @@ -255,7 +266,8 @@ static int fl_wpage(struct flash_chip *c, uint32_t dst, const void *src, return -EINVAL; rc = fl_wren(ct); - if (rc) return rc; + if (rc) + return rc; rc = ct->cmd_wr(ct, CMD_PP, true, dst, src, size); if (rc) @@ -273,7 +285,7 @@ int flash_write(struct flash_chip *c, uint32_t dst, const void *src, uint32_t d = dst; const void *s = src; uint8_t vbuf[0x100]; - int rc; + int rc; /* Some sanity checking */ if (((dst + size) <= dst) || !size || (dst + size) > c->tsize) @@ -298,7 +310,7 @@ int flash_write(struct flash_chip *c, uint32_t dst, const void *src, return -EOPNOTSUPP; /* Iterate for each page to write */ - while(todo) { + while (todo) { uint32_t chunk; /* Handle misaligned start */ @@ -307,7 +319,8 @@ int flash_write(struct flash_chip *c, uint32_t dst, const void *src, chunk = todo; rc = fl_wpage(c, d, s, chunk); - if (rc) return rc; + if (rc) + return rc; d += chunk; s += chunk; todo -= chunk; @@ -316,21 +329,22 @@ int flash_write(struct flash_chip *c, uint32_t dst, const void *src, } fprintf(stderr, "\n"); - writing_done: +writing_done: if (!verify) return 0; /* Verify */ FL_DBG("LIBFLASH: Verifying...\n"); - while(size) { + while (size) { uint32_t chunk; chunk = sizeof(vbuf); if (chunk > size) chunk = size; rc = flash_read(c, dst, vbuf, chunk); - if (rc) return rc; + if (rc) + return rc; if (memcmp(vbuf, src, chunk)) { FL_ERR("LIBFLASH: Miscompare at 0x%08x\n", dst); return -EREMOTEIO; @@ -351,8 +365,7 @@ enum sm_comp_res { sm_need_erase, }; -static enum sm_comp_res flash_smart_comp(struct flash_chip *c, - const void *src, +static enum sm_comp_res flash_smart_comp(struct flash_chip *c, const void *src, uint32_t offset, uint32_t size) { uint8_t *b = c->smart_buf + offset; @@ -363,9 +376,9 @@ static enum sm_comp_res flash_smart_comp(struct flash_chip *c, /* SRC DEST NEED_ERASE * 0 1 0 * 1 1 0 - * 0 0 0 - * 1 0 1 - */ + * 0 0 0 + * 1 0 1 + */ for (i = 0; i < size; i++) { /* Any bit need to be set, need erase */ if (s[i] & ~b[i]) @@ -393,15 +406,15 @@ int flash_smart_write(struct flash_chip *c, uint64_t dst, const void *src, dst, dst + size); /* As long as we have something to write ... */ - while(dst < end) { + while (dst < end) { uint32_t page, off, chunk; enum sm_comp_res sr; /* Figure out which erase page we are in and read it */ page = dst & ~c->min_erase_mask; off = dst & c->min_erase_mask; - FL_DBG("LIBFLASH: reading page 0x%08x..0x%08x...", - page, page + er_size); + FL_DBG("LIBFLASH: reading page 0x%08x..0x%08x...", page, + page + er_size); rc = flash_read(c, page, c->smart_buf, er_size); if (rc) { FL_DBG(" error %d!\n", rc); @@ -415,7 +428,7 @@ int flash_smart_write(struct flash_chip *c, uint64_t dst, const void *src, /* Compare against what we are writing and ff */ sr = flash_smart_comp(c, src, off, chunk); - switch(sr) { + switch (sr) { case sm_no_change: /* Identical, skip it */ FL_DBG(" same !\n"); @@ -452,8 +465,7 @@ int flash_smart_write(struct flash_chip *c, uint64_t dst, const void *src, return 0; } -static int fl_chip_id(struct sfc *ct, uint8_t *id_buf, - uint32_t *id_size) +static int fl_chip_id(struct sfc *ct, uint8_t *id_buf, uint32_t *id_size) { int rc; uint8_t stat; @@ -486,7 +498,7 @@ static int flash_identify(struct flash_chip *c) struct sfc *ct = c->ctrl; const struct flash_info *info = NULL; uint32_t iid, id_size; -#define MAX_ID_SIZE 16 +#define MAX_ID_SIZE 16 uint8_t id[MAX_ID_SIZE]; int rc, i; @@ -506,14 +518,14 @@ static int flash_identify(struct flash_chip *c) iid = (iid << 8) | id[1]; iid = (iid << 8) | id[2]; - FL_DBG("LIBFLASH: Flash ID: %02x.%02x.%02x (%06x)\n", - id[0], id[1], id[2], iid); + FL_DBG("LIBFLASH: Flash ID: %02x.%02x.%02x (%06x)\n", id[0], id[1], + id[2], iid); /* Lookup in flash_info */ for (i = 0; (size_t)i < ARRAY_SIZE(flash_info); i++) { info = &flash_info[i]; if (info->id == iid) - break; + break; } if (!info || info->id != iid) return -ENXIO; @@ -595,7 +607,6 @@ static int flash_configure(struct flash_chip *c) } } - /* Set controller to 4b mode if supported */ if (ct->set_4b) { FL_DBG("LIBFLASH: Enabling controller 4B mode...\n"); diff --git a/src/flash.h b/src/flash.h index ba442ae..a1a7c1c 100644 --- a/src/flash.h +++ b/src/flash.h @@ -23,17 +23,17 @@ #include /* Flash status bits */ -#define STAT_WIP 0x01 -#define STAT_WEN 0x02 +#define STAT_WIP 0x01 +#define STAT_WEN 0x02 struct flash_chip { - struct sfc *ctrl; - struct flash_info info; - uint32_t tsize; - uint32_t min_erase_mask; - bool mode_4b; - struct flash_req *cur_req; - void *smart_buf; + struct sfc *ctrl; + struct flash_info info; + uint32_t tsize; + uint32_t min_erase_mask; + bool mode_4b; + struct flash_req *cur_req; + void *smart_buf; }; int flash_init(struct sfc *ctrl, struct flash_chip **chip); diff --git a/src/host.c b/src/host.c index 795f3ff..8a5ceeb 100644 --- a/src/host.c +++ b/src/host.c @@ -25,150 +25,150 @@ struct bridge { void print_bridge_drivers(void) { - struct bridge_driver **bridges; - size_t n_bridges = 0; + struct bridge_driver **bridges; + size_t n_bridges = 0; - printf("Available bridges:\n"); + printf("Available bridges:\n"); - bridges = autodata_get(bridge_drivers, &n_bridges); + bridges = autodata_get(bridge_drivers, &n_bridges); - for (size_t i = 0; i < n_bridges; i++) { - printf(" %s\n", bridges[i]->name); - } + for (size_t i = 0; i < n_bridges; i++) { + printf(" %s\n", bridges[i]->name); + } - autodata_free(bridges); + autodata_free(bridges); } int disable_bridge_driver(const char *drv) { - struct bridge_driver **bridges; - size_t n_bridges = 0; - int ret = -ENOENT; + struct bridge_driver **bridges; + size_t n_bridges = 0; + int ret = -ENOENT; - bridges = autodata_get(bridge_drivers, &n_bridges); + bridges = autodata_get(bridge_drivers, &n_bridges); - for (size_t i = 0; i < n_bridges; i++) { - if (!strcmp(bridges[i]->name, drv)) { - bridges[i]->disabled = true; - ret = 0; - goto out; - } - } + for (size_t i = 0; i < n_bridges; i++) { + if (!strcmp(bridges[i]->name, drv)) { + bridges[i]->disabled = true; + ret = 0; + goto out; + } + } out: - autodata_free(bridges); + autodata_free(bridges); - return ret; + return ret; } int get_bridge_driver(const char *drv, struct bridge_driver **bridge) { - struct bridge_driver **bridges; - size_t n_bridges = 0; - int ret = -ENOENT; + struct bridge_driver **bridges; + size_t n_bridges = 0; + int ret = -ENOENT; - bridges = autodata_get(bridge_drivers, &n_bridges); + bridges = autodata_get(bridge_drivers, &n_bridges); - for (size_t i = 0; i < n_bridges; i++) { - if (!strcmp(bridges[i]->name, drv) && !bridges[i]->disabled) { - *bridge = bridges[i]; - ret = 0; - goto out; - } - } + for (size_t i = 0; i < n_bridges; i++) { + if (!strcmp(bridges[i]->name, drv) && !bridges[i]->disabled) { + *bridge = bridges[i]; + ret = 0; + goto out; + } + } out: - autodata_free(bridges); + autodata_free(bridges); - return ret; + return ret; } static inline int host_probe_bridge(struct host *ctx, - struct bridge_driver *driver, - struct connection_args *connection) + struct bridge_driver *driver, + struct connection_args *connection) { - struct ahb *ahb; - struct bridge *bridge; + struct ahb *ahb; + struct bridge *bridge; - if (driver->disabled) { - logd("Skipping bridge driver %s\n", driver->name); - return 0; - } + if (driver->disabled) { + logd("Skipping bridge driver %s\n", driver->name); + return 0; + } - logd("Trying bridge driver %s\n", driver->name); + logd("Trying bridge driver %s\n", driver->name); - ahb = driver->probe(connection); - if (!ahb) - return 0; + ahb = driver->probe(connection); + if (!ahb) + return 0; - bridge = malloc(sizeof(*bridge)); - if (!bridge) - return -ENOMEM; + bridge = malloc(sizeof(*bridge)); + if (!bridge) + return -ENOMEM; - bridge->driver = driver; - bridge->ahb = ahb; + bridge->driver = driver; + bridge->ahb = ahb; - list_add(&ctx->bridges, &bridge->entry); - return 0; + list_add(&ctx->bridges, &bridge->entry); + return 0; } int host_init(struct host *ctx, struct connection_args *connection) { - struct bridge_driver **bridges; - size_t n_bridges; - int rc; - - /* Always init head for legacy reasons */ - list_head_init(&ctx->bridges); - - /* If a bridge driver is defined, use it instead of probing all */ - if (connection->bridge_driver != NULL) { - logd("Host probing found bridge driver '%s', using it\n", - connection->bridge_driver->name); - rc = host_probe_bridge(ctx, connection->bridge_driver, connection); - goto done; - } - - bridges = autodata_get(bridge_drivers, &n_bridges); - logd("Found %zu registered bridge drivers\n", n_bridges); - - for (size_t i = 0; i < n_bridges; i++) { - rc = host_probe_bridge(ctx, bridges[i], connection); - if (rc < 0) - goto cleanup_bridges; - } + struct bridge_driver **bridges; + size_t n_bridges; + int rc; + + /* Always init head for legacy reasons */ + list_head_init(&ctx->bridges); + + /* If a bridge driver is defined, use it instead of probing all */ + if (connection->bridge_driver != NULL) { + logd("Host probing found bridge driver '%s', using it\n", + connection->bridge_driver->name); + rc = host_probe_bridge(ctx, connection->bridge_driver, + connection); + goto done; + } + + bridges = autodata_get(bridge_drivers, &n_bridges); + logd("Found %zu registered bridge drivers\n", n_bridges); + + for (size_t i = 0; i < n_bridges; i++) { + rc = host_probe_bridge(ctx, bridges[i], connection); + if (rc < 0) + goto cleanup_bridges; + } cleanup_bridges: - autodata_free(bridges); + autodata_free(bridges); done: - return rc; + return rc; } - void host_destroy(struct host *ctx) { - struct bridge *bridge, *next; + struct bridge *bridge, *next; - list_for_each_safe(&ctx->bridges, bridge, next, entry) { - bridge->driver->destroy(bridge->ahb); - list_del(&bridge->entry); - free(bridge); - } + list_for_each_safe(&ctx->bridges, bridge, next, entry) { + bridge->driver->destroy(bridge->ahb); + list_del(&bridge->entry); + free(bridge); + } } struct ahb *host_get_ahb(struct host *ctx) { - struct bridge *bridge; + struct bridge *bridge; - bridge = list_top(&ctx->bridges, struct bridge, entry); + bridge = list_top(&ctx->bridges, struct bridge, entry); - if (bridge) { - logd("Accessing the BMC's AHB via the %s bridge\n", - bridge->driver->name); - return bridge->ahb; - } + if (bridge) { + logd("Accessing the BMC's AHB via the %s bridge\n", + bridge->driver->name); + return bridge->ahb; + } - loge("Bridge discovery failed, cannot access BMC AHB\n"); - return NULL; + loge("Bridge discovery failed, cannot access BMC AHB\n"); + return NULL; } diff --git a/src/log.c b/src/log.c index 46bf0e4..7628bfd 100644 --- a/src/log.c +++ b/src/log.c @@ -6,91 +6,95 @@ #include const char *log_colour_codes[] = { - [colour_white] = "\e[97m", - [colour_yellow] = "\e[93m", - [colour_green] = "\e[92m", - [colour_red] = "\e[91m", + [colour_white] = "\e[97m", + [colour_yellow] = "\e[93m", + [colour_green] = "\e[92m", + [colour_red] = "\e[91m", }; const int log_colour_lookup[] = { - [level_trace] = colour_white, - [level_debug] = colour_yellow, - [level_info] = colour_green, - [level_error] = colour_red, + [level_trace] = colour_white, + [level_debug] = colour_yellow, + [level_info] = colour_green, + [level_error] = colour_red, }; static enum log_level log_current_level; static void log_write_all(int fd, const char *buf, size_t len) { - ssize_t egress; - - while (len) { - egress = write(fd, buf, len); - if (egress < 0) { - perror("write"); - return; - } - len -= egress; - buf += egress; - } + ssize_t egress; + + while (len) { + egress = write(fd, buf, len); + if (egress < 0) { + perror("write"); + return; + } + len -= egress; + buf += egress; + } } static void log_set_colour(int fd, enum log_colour colour) { - const char *code = log_colour_codes[colour]; + const char *code = log_colour_codes[colour]; - log_write_all(fd, code, strlen(code)); + log_write_all(fd, code, strlen(code)); } static void log_reset_colour(int fd) { - const char *reset_code = "\e[0m"; + const char *reset_code = "\e[0m"; - log_write_all(fd, reset_code, 4); + log_write_all(fd, reset_code, 4); } void log_msg(enum log_level lvl, const char *fmt, ...) { - const int fd = fileno(stderr); - va_list args; - int rc; + const int fd = fileno(stderr); + va_list args; + int rc; - if (lvl > log_current_level) - return; + if (lvl > log_current_level) + return; - if (isatty(fd)) - log_set_colour(fd, log_colour_lookup[lvl]); + if (isatty(fd)) + log_set_colour(fd, log_colour_lookup[lvl]); - log_write_all(fd, "[*] ", 4); + log_write_all(fd, "[*] ", 4); - if (isatty(fd)) - log_reset_colour(fd); + if (isatty(fd)) + log_reset_colour(fd); - va_start(args, fmt); - rc = vdprintf(fd, fmt, args); - va_end(args); - if (rc < 0) { perror("vdprintf"); } + va_start(args, fmt); + rc = vdprintf(fd, fmt, args); + va_end(args); + if (rc < 0) { + perror("vdprintf"); + } } void log_highlight(int fd, enum log_colour colour, const char *fmt, ...) { - va_list args; - int rc; + va_list args; + int rc; - if (isatty(fd)) - log_set_colour(fd, colour); + if (isatty(fd)) + log_set_colour(fd, colour); - va_start(args, fmt); - rc = vdprintf(fd, fmt, args); - va_end(args); - if (rc < 0) { perror("vdprintf"); } + va_start(args, fmt); + rc = vdprintf(fd, fmt, args); + va_end(args); + if (rc < 0) { + perror("vdprintf"); + } - if (isatty(fd)) - log_reset_colour(fd); + if (isatty(fd)) + log_reset_colour(fd); } void log_set_level(enum log_level level) { - log_current_level = level; + log_current_level = level; } diff --git a/src/log.h b/src/log.h index facaaca..54a5c0c 100644 --- a/src/log.h +++ b/src/log.h @@ -1,7 +1,13 @@ #ifndef _LOG_H #define _LOG_H -enum log_level { level_none, level_error, level_info, level_debug, level_trace }; +enum log_level { + level_none, + level_error, + level_info, + level_debug, + level_trace +}; enum log_colour { colour_white, colour_yellow, colour_green, colour_red }; diff --git a/src/lpc.h b/src/lpc.h index 2e95fd6..ff345dd 100644 --- a/src/lpc.h +++ b/src/lpc.h @@ -11,10 +11,9 @@ #include #include -struct lpc -{ - int fd; - const char *space; +struct lpc { + int fd; + const char *space; }; #if HAVE_LPC @@ -31,70 +30,62 @@ int lpc_writel(struct lpc *ctx, size_t addr, uint32_t val); int lpc_read(struct lpc *ctx, size_t addr, void *val, size_t size); int lpc_write(struct lpc *ctx, size_t addr, const void *val, size_t size); #else -static inline int -lpc_init(struct lpc *ctx __unused, const char *space __unused) +static inline int lpc_init(struct lpc *ctx __unused, const char *space __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_destroy(struct lpc *ctx __unused) +static inline int lpc_destroy(struct lpc *ctx __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_readb(struct lpc *ctx __unused, size_t addr __unused, uint8_t *val __unused) +static inline int lpc_readb(struct lpc *ctx __unused, size_t addr __unused, + uint8_t *val __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_writeb(struct lpc *ctx __unused, size_t addr __unused, uint8_t val __unused) +static inline int lpc_writeb(struct lpc *ctx __unused, size_t addr __unused, + uint8_t val __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_readw(struct lpc *ctx __unused, size_t addr __unused, - uint16_t *val __unused) +static inline int lpc_readw(struct lpc *ctx __unused, size_t addr __unused, + uint16_t *val __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_writew(struct lpc *ctx __unused, size_t addr __unused, - uint16_t val __unused) +static inline int lpc_writew(struct lpc *ctx __unused, size_t addr __unused, + uint16_t val __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_readl(struct lpc *ctx __unused, size_t addr __unused, - uint32_t *val __unused) +static inline int lpc_readl(struct lpc *ctx __unused, size_t addr __unused, + uint32_t *val __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_writel(struct lpc *ctx __unused, size_t addr __unused, - uint32_t val __unused) +static inline int lpc_writel(struct lpc *ctx __unused, size_t addr __unused, + uint32_t val __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_read(struct lpc *ctx __unused, size_t addr __unused, void *val __unused, - size_t size __unused) +static inline int lpc_read(struct lpc *ctx __unused, size_t addr __unused, + void *val __unused, size_t size __unused) { - return -ENOTSUP; + return -ENOTSUP; } -static inline int -lpc_write(struct lpc *ctx __unused, size_t addr __unused, const void *val - __unused, size_t size __unused) +static inline int lpc_write(struct lpc *ctx __unused, size_t addr __unused, + const void *val __unused, size_t size __unused) { - return -ENOTSUP; + return -ENOTSUP; } #endif diff --git a/src/meson.build b/src/meson.build index cf7fe9d..f552b47 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,29 +1,29 @@ fs = import('fs') if get_option('prefer_static') - add_project_link_arguments('-static', language: 'c') + add_project_link_arguments('-static', language: 'c') endif -incdirs = [ '.' ] +incdirs = ['.'] src = files( - 'ahb.c', - 'ast.c', - 'culvert.c', - 'flash.c', - 'host.c', - 'log.c', - 'mmio.c', - 'pci.c', - 'priv.c', - 'prompt.c', - 'rev.c', - 'shell.c', - 'sio.c', - 'soc.c', - 'ts16.c', - 'tty.c', - 'uart/suart.c' + 'ahb.c', + 'ast.c', + 'culvert.c', + 'flash.c', + 'host.c', + 'log.c', + 'mmio.c', + 'pci.c', + 'priv.c', + 'prompt.c', + 'rev.c', + 'shell.c', + 'sio.c', + 'soc.c', + 'ts16.c', + 'tty.c', + 'uart/suart.c', ) conf_data = configuration_data() @@ -32,21 +32,23 @@ conf_data = configuration_data() host = host_machine.cpu_family() host_inc = 'arch/@0@'.format(host) if fs.is_dir(host_inc) - incdirs += [ host_inc ] + incdirs += [host_inc] endif incdirs = include_directories(incdirs) host_lpc = 'arch/@0@/lpc.c'.format(host) if fs.is_file(host_lpc) - src += [ host_lpc ] - conf_data.set10('have_lpc', true) + src += [host_lpc] + conf_data.set10('have_lpc', true) else - conf_data.set10('have_lpc', false) + conf_data.set10('have_lpc', false) endif -configure_file(input: 'config.h.in', - output: 'config.h', - configuration: conf_data) +configure_file( + input: 'config.h.in', + output: 'config.h', + configuration: conf_data, +) subdir('bridge') subdir('ccan') @@ -55,14 +57,21 @@ subdir('devicetree') subdir('soc') # Version -version = vcs_tag(command: ['git', 'describe', '--tags', '--dirty', '--always'], - fallback: meson.project_version(), - input: 'version.h.in', - output: 'version.h', - replace_string: '@culvert_version@') +version = vcs_tag( + command: ['git', 'describe', '--tags', '--dirty', '--always'], + fallback: meson.project_version(), + input: 'version.h.in', + output: 'version.h', + replace_string: '@culvert_version@', +) -executable('culvert', src, dtbos, version, - include_directories: incdirs, - dependencies: [ libfdt_dep ], - link_with: [ libccan ], - install: true) +executable( + 'culvert', + src, + dtbos, + version, + include_directories: incdirs, + dependencies: [libfdt_dep], + link_with: [libccan], + install: true, +) diff --git a/src/mmio.c b/src/mmio.c index 7ff9680..ffb6ec1 100644 --- a/src/mmio.c +++ b/src/mmio.c @@ -6,39 +6,44 @@ #include -volatile void *mmio_memcpy(volatile void * restrict dst, const volatile void * restrict src, size_t len) +volatile void *mmio_memcpy(volatile void *restrict dst, + const volatile void *restrict src, size_t len) { - if (((unsigned long)src & 0x3) != ((unsigned long)dst & 0x3)) { - while (len) { - *(volatile uint8_t * restrict)dst++ = *(const volatile uint8_t * restrict)src++; - len--; - } - - iob(); - - return dst; - } - - while (len && ((unsigned long)src & 0x3) && ((unsigned long)dst & 0x3)) { - *(volatile uint8_t * restrict)dst++ = *(const volatile uint8_t * restrict)src++; - len--; - } - - while (len > 3) { - const volatile uint32_t * restrict tsrc = src; - volatile uint32_t * restrict tdst = dst; - *tdst = *tsrc; - len -= sizeof(*tdst); - src += sizeof(*tsrc); - dst += sizeof(*tdst); - } - - while (len) { - *(volatile uint8_t * restrict)dst++ = *(const volatile uint8_t * restrict)src++; - len--; - } - - iob(); - - return dst; + if (((unsigned long)src & 0x3) != ((unsigned long)dst & 0x3)) { + while (len) { + *(volatile uint8_t *restrict)dst++ = + *(const volatile uint8_t *restrict)src++; + len--; + } + + iob(); + + return dst; + } + + while (len && ((unsigned long)src & 0x3) && + ((unsigned long)dst & 0x3)) { + *(volatile uint8_t *restrict)dst++ = + *(const volatile uint8_t *restrict)src++; + len--; + } + + while (len > 3) { + const volatile uint32_t *restrict tsrc = src; + volatile uint32_t *restrict tdst = dst; + *tdst = *tsrc; + len -= sizeof(*tdst); + src += sizeof(*tsrc); + dst += sizeof(*tdst); + } + + while (len) { + *(volatile uint8_t *restrict)dst++ = + *(const volatile uint8_t *restrict)src++; + len--; + } + + iob(); + + return dst; } diff --git a/src/mmio.h b/src/mmio.h index bbcf33b..d991f55 100644 --- a/src/mmio.h +++ b/src/mmio.h @@ -6,6 +6,7 @@ #include -volatile void *mmio_memcpy(volatile void *dst, const volatile void *src, size_t len); +volatile void *mmio_memcpy(volatile void *dst, const volatile void *src, + size_t len); #endif diff --git a/src/pci.c b/src/pci.c index b3e5bb8..d195eeb 100644 --- a/src/pci.c +++ b/src/pci.c @@ -44,9 +44,9 @@ int read_sysfs_id(int dirfd, const char *file) int pci_open(uint16_t vid, uint16_t did, int bar) { - char *res; - int rc; - int fd; + char *res; + int rc; + int fd; int found = 0; struct dirent *de; @@ -94,12 +94,12 @@ int pci_open(uint16_t vid, uint16_t did, int bar) free(res); closedir(d); - return fd; + return fd; } int pci_close(int fd) { - assert(fd >= 0); + assert(fd >= 0); - return close(fd); + return close(fd); } diff --git a/src/priv.c b/src/priv.c index 528b511..613e43f 100644 --- a/src/priv.c +++ b/src/priv.c @@ -11,10 +11,10 @@ bool priv_am_root(void) { - return !geteuid(); + return !geteuid(); } void priv_print_unprivileged(const char *name) { - loge("%s needs to be run as root\n", name); + loge("%s needs to be run as root\n", name); } diff --git a/src/prompt.c b/src/prompt.c index ea8b293..42bcc1b 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -12,166 +12,168 @@ int prompt_init(struct prompt *ctx, int fd, const char *eol, bool have_echo) { - ctx->stream = fdopen(fd, "r+"); - if (!ctx->stream) - return -errno; + ctx->stream = fdopen(fd, "r+"); + if (!ctx->stream) + return -errno; - ctx->eol = eol; + ctx->eol = eol; - ctx->have_echo = have_echo; + ctx->have_echo = have_echo; - return 0; + return 0; } int prompt_destroy(struct prompt *ctx) { - int rc; + int rc; - rc = fclose(ctx->stream); - if (rc < 0) - return -errno; + rc = fclose(ctx->stream); + if (rc < 0) + return -errno; - return 0; + return 0; } int prompt_gets(struct prompt *ctx, char *output, size_t len) { - char *cursor; - char *res; - - cursor = output; - do { - res = fgets(cursor, len, ctx->stream); - if (!res) - return -errno; - cursor += strlen(res); - } while (!strchr(output, '\n')); - - return 0; + char *cursor; + char *res; + + cursor = output; + do { + res = fgets(cursor, len, ctx->stream); + if (!res) + return -errno; + cursor += strlen(res); + } while (!strchr(output, '\n')); + + return 0; } int prompt_expect_into(struct prompt *ctx, const char *str, char *prior, - size_t len, char **prompt) + size_t len, char **prompt) { - ssize_t ingress; - char *cursor; - char *res; + ssize_t ingress; + char *cursor; + char *res; - cursor = prior; - do { - ingress = read(fileno(ctx->stream), cursor, prior + len - cursor); - if (ingress < 0) - return -errno; + cursor = prior; + do { + ingress = + read(fileno(ctx->stream), cursor, prior + len - cursor); + if (ingress < 0) + return -errno; - cursor += ingress; - } while(!(res = memmem(prior, cursor - prior, str, strlen(str))) && - (ptrdiff_t)len > (cursor - prior)); + cursor += ingress; + } while (!(res = memmem(prior, cursor - prior, str, strlen(str))) && + (ptrdiff_t)len > (cursor - prior)); - if (prompt) - *prompt = res; + if (prompt) + *prompt = res; - return res != NULL; + return res != NULL; } int prompt_expect(struct prompt *ctx, const char *str) { - char buf[100]; - int rc; + char buf[100]; + int rc; - do { - rc = prompt_expect_into(ctx, str, &buf[0], sizeof(buf), NULL); - if (rc < 0) - return rc; - } while(!rc); + do { + rc = prompt_expect_into(ctx, str, &buf[0], sizeof(buf), NULL); + if (rc < 0) + return rc; + } while (!rc); - return rc; + return rc; } ssize_t prompt_write(struct prompt *ctx, const char *buf, size_t len) { - const char *cursor; - ssize_t egress; + const char *cursor; + ssize_t egress; - cursor = buf; - do { - egress = write(fileno(ctx->stream), cursor, buf + len - cursor); - if (egress < 0) - return -errno; + cursor = buf; + do { + egress = write(fileno(ctx->stream), cursor, buf + len - cursor); + if (egress < 0) + return -errno; - cursor += egress; - } while(cursor < (buf + len)); + cursor += egress; + } while (cursor < (buf + len)); - return len; + return len; } ssize_t prompt_read(struct prompt *ctx, char *output, size_t len) { - char *cursor; - ssize_t ingress; + char *cursor; + ssize_t ingress; - cursor = output; - do { - ingress = read(fileno(ctx->stream), cursor, output + len - cursor); - if (ingress < 0) - return -errno; + cursor = output; + do { + ingress = read(fileno(ctx->stream), cursor, + output + len - cursor); + if (ingress < 0) + return -errno; - cursor += ingress; - } while(cursor < (output + len)); + cursor += ingress; + } while (cursor < (output + len)); - return len; + return len; } int prompt_run(struct prompt *ctx, const char *cmd) { - size_t len = strlen(cmd); - int rc; + size_t len = strlen(cmd); + int rc; - rc = prompt_write(ctx, cmd, len); - if (rc < 0) - return rc; + rc = prompt_write(ctx, cmd, len); + if (rc < 0) + return rc; - rc = prompt_write(ctx, ctx->eol, strlen(ctx->eol)); - if (rc < 0) - return rc; + rc = prompt_write(ctx, ctx->eol, strlen(ctx->eol)); + if (rc < 0) + return rc; - if (ctx->have_echo) { - char *echo, *res; - size_t eol_len = len + strlen(ctx->eol) + 1; + if (ctx->have_echo) { + char *echo, *res; + size_t eol_len = len + strlen(ctx->eol) + 1; - echo = malloc(eol_len); - if (!echo) - return -errno; + echo = malloc(eol_len); + if (!echo) + return -errno; - res = fgets(echo, eol_len, ctx->stream); - free(echo); - if (!res) - return -errno; - } + res = fgets(echo, eol_len, ctx->stream); + free(echo); + if (!res) + return -errno; + } - return rc; + return rc; } int prompt_expect_run(struct prompt *ctx, const char *prompt, const char *cmd) { - int rc; + int rc; - rc = prompt_expect(ctx, prompt); - if (rc < 0) - return rc; - else if (!rc) - return -EBADE; + rc = prompt_expect(ctx, prompt); + if (rc < 0) + return rc; + else if (!rc) + return -EBADE; - return prompt_run(ctx, cmd); + return prompt_run(ctx, cmd); } int prompt_run_expect(struct prompt *ctx, const char *cmd, const char *prompt, - char **output, size_t len) + char **output, size_t len) { - int rc; + int rc; - rc = prompt_run(ctx, cmd); - if (rc < 0) - return rc; + rc = prompt_run(ctx, cmd); + if (rc < 0) + return rc; - return prompt_expect_into(ctx, prompt, *output, len, output); + return prompt_expect_into(ctx, prompt, *output, len, output); } diff --git a/src/prompt.h b/src/prompt.h index 214a069..d9a730a 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -9,9 +9,9 @@ #include struct prompt { - FILE *stream; - const char *eol; - bool have_echo; + FILE *stream; + const char *eol; + bool have_echo; }; /** @@ -22,7 +22,7 @@ int prompt_destroy(struct prompt *ctx); int prompt_expect(struct prompt *ctx, const char *str); int prompt_expect_into(struct prompt *ctx, const char *str, char *prior, - size_t len, char **prompt); + size_t len, char **prompt); ssize_t prompt_write(struct prompt *ctx, const char *cmd, size_t len); ssize_t prompt_read(struct prompt *ctx, char *output, size_t len); int prompt_gets(struct prompt *ctx, char *output, size_t len); diff --git a/src/rev.c b/src/rev.c index 8279879..0a87b22 100644 --- a/src/rev.c +++ b/src/rev.c @@ -11,180 +11,183 @@ #include struct bmc_silicon_rev { - uint32_t rev; - const char *name; + uint32_t rev; + const char *name; }; static const struct bmc_silicon_rev ast_silicon_revs[] = { - { 0x02000303, "AST2400 A0" }, - { 0x02010303, "AST2400 A1" }, - { 0x04000303, "AST2500 A0" }, - { 0x04010303, "AST2500 A1" }, - { 0x04030303, "AST2500 A2" }, - { 0x05000303, "AST2600 A0" }, - { 0x05010303, "AST2600 A1" }, - { 0x05020303, "AST2600 A2" }, - { 0x05030303, "AST2600 A3" }, + { 0x02000303, "AST2400 A0" }, { 0x02010303, "AST2400 A1" }, + { 0x04000303, "AST2500 A0" }, { 0x04010303, "AST2500 A1" }, + { 0x04030303, "AST2500 A2" }, { 0x05000303, "AST2600 A0" }, + { 0x05010303, "AST2600 A1" }, { 0x05020303, "AST2600 A2" }, + { 0x05030303, "AST2600 A3" }, }; int64_t rev_probe(struct ahb *ahb) { - uint32_t probe[2], rev; - bool is_g6; - int rc; - size_t i; - - logd("Probing for SoC revision registers\n"); - - /* - * The layout of the AST2600 SCU is drastically different from the 2400 and - * 2500 to the point that the silicon revision registers have moved. We - * need to do some fingerprinting to work around it. - * - * That said, 0x1e6e2000 is the SCU on all of the 2400, 2500 and 2600. - */ + uint32_t probe[2], rev; + bool is_g6; + int rc; + size_t i; + + logd("Probing for SoC revision registers\n"); + + /* + * The layout of the AST2600 SCU is drastically different from the 2400 and + * 2500 to the point that the silicon revision registers have moved. We + * need to do some fingerprinting to work around it. + * + * That said, 0x1e6e2000 is the SCU on all of the 2400, 2500 and 2600. + */ #define AST_SCU 0x1e6e2000 - /* - * SCU004 is: - * - * - AST2400: System Reset Control Register - * - AST2500: System Reset Control Register - * - AST2600: Silicon Revision ID Register - * - * With the following properties: - * - * - AST2400: - * [31:28]: Reserved, 0b1111 - * [27:26]: Reserved, 0b11 - * - * - AST2500: - * [31:26]: Reserved, 0b111111 according to one line of the - * datasheet, 0b111101 according to another line, but - * 0b000000 has also been seen in practice. - * - * - AST2600: - * [31:24]: Reserved, 0x05 - * - * The somewhat unpredictable behavior of the upper bits on AST2500 make - * it a bit tricky to use this confidently, but we can at least treat all - * of the bits of the top nibble being clear as a fairly strong (but not - * entirely conclusive) indicator that it's an AST2600. - */ - rc = ahb_readl(ahb, AST_SCU | 0x004, &probe[0]); - if (rc < 0) { return rc; } - - /* - * SCU07C is: - * - * - AST2400: Silicon Revision ID Register - * - AST2500: Silicon Revision ID Register - * - AST2600: System Reset Event Log Register Set 2-3 - * - * With the following properties: - * - * - AST2400: - * [31:24]: Reserved, 0x02 - * - * - AST2500: - * [31:24]: Reserved, 0x04 - * - * - AST2600: - * [31:16]: Reserved, 0x0000 - * - * The AST2600 reserved value of 0x0000 allows us to infer that any set - * bits in the top byte indicate that we are _not_ looking at a 2600 SoC. - */ - rc = ahb_readl(ahb, AST_SCU | 0x07c, &probe[1]); - if (rc < 0) { return rc; } - - /* - * Given the above, assume we're on a g6 if the top 4 bits of SCU004 and - * top 8 of SCU07C are all zero. - */ - is_g6 = !((probe[0] >> 28) & 0xf) && !((probe[1] >> 24) & 0xff); - - /* Based on the above observations, extract the true silicon revision ID */ - if (is_g6) { - /* - * AST2600 A2+ doesn't reflect the stepping in SCU004... that's only - * indicated in SCU014. But we can't just use 014 across the board - * because it can't be used to distinguish older chips - * (AST2400/AST2500) as easily as SCU004. So use it once we know it's - * the AST2600. - */ - rc = ahb_readl(ahb, AST_SCU | 0x014, &rev); - if (rc < 0) { return rc; } - } else { - rev = probe[1]; - } + /* + * SCU004 is: + * + * - AST2400: System Reset Control Register + * - AST2500: System Reset Control Register + * - AST2600: Silicon Revision ID Register + * + * With the following properties: + * + * - AST2400: + * [31:28]: Reserved, 0b1111 + * [27:26]: Reserved, 0b11 + * + * - AST2500: + * [31:26]: Reserved, 0b111111 according to one line of the + * datasheet, 0b111101 according to another line, but + * 0b000000 has also been seen in practice. + * + * - AST2600: + * [31:24]: Reserved, 0x05 + * + * The somewhat unpredictable behavior of the upper bits on AST2500 make + * it a bit tricky to use this confidently, but we can at least treat all + * of the bits of the top nibble being clear as a fairly strong (but not + * entirely conclusive) indicator that it's an AST2600. + */ + rc = ahb_readl(ahb, AST_SCU | 0x004, &probe[0]); + if (rc < 0) { + return rc; + } + + /* + * SCU07C is: + * + * - AST2400: Silicon Revision ID Register + * - AST2500: Silicon Revision ID Register + * - AST2600: System Reset Event Log Register Set 2-3 + * + * With the following properties: + * + * - AST2400: + * [31:24]: Reserved, 0x02 + * + * - AST2500: + * [31:24]: Reserved, 0x04 + * + * - AST2600: + * [31:16]: Reserved, 0x0000 + * + * The AST2600 reserved value of 0x0000 allows us to infer that any set + * bits in the top byte indicate that we are _not_ looking at a 2600 SoC. + */ + rc = ahb_readl(ahb, AST_SCU | 0x07c, &probe[1]); + if (rc < 0) { + return rc; + } + + /* + * Given the above, assume we're on a g6 if the top 4 bits of SCU004 and + * top 8 of SCU07C are all zero. + */ + is_g6 = !((probe[0] >> 28) & 0xf) && !((probe[1] >> 24) & 0xff); + + /* Based on the above observations, extract the true silicon revision ID */ + if (is_g6) { + /* + * AST2600 A2+ doesn't reflect the stepping in SCU004... that's only + * indicated in SCU014. But we can't just use 014 across the board + * because it can't be used to distinguish older chips + * (AST2400/AST2500) as easily as SCU004. So use it once we know it's + * the AST2600. + */ + rc = ahb_readl(ahb, AST_SCU | 0x014, &rev); + if (rc < 0) { + return rc; + } + } else { + rev = probe[1]; + } #undef AST_SCU - logd("Found revision 0x%x\n", rev); + logd("Found revision 0x%x\n", rev); - /* Is it a supported revision? */ - for (i = 0; i < ARRAY_SIZE(ast_silicon_revs); i++) { - if (rev == ast_silicon_revs[i].rev) - return rev; - } + /* Is it a supported revision? */ + for (i = 0; i < ARRAY_SIZE(ast_silicon_revs); i++) { + if (rev == ast_silicon_revs[i].rev) + return rev; + } - logd("Revision 0x%x is unsupported\n", rev); + logd("Revision 0x%x is unsupported\n", rev); - return -ENODEV; + return -ENODEV; } bool rev_is_supported(uint32_t rev) { - size_t i; + size_t i; - for (i = 0; i < ARRAY_SIZE(ast_silicon_revs); i++) { - if (rev == ast_silicon_revs[i].rev) - return true; - } + for (i = 0; i < ARRAY_SIZE(ast_silicon_revs); i++) { + if (rev == ast_silicon_revs[i].rev) + return true; + } - return false; + return false; } -const char *rev_name(uint32_t rev) { - size_t i; +const char *rev_name(uint32_t rev) +{ + size_t i; - for (i = 0; i < ARRAY_SIZE(ast_silicon_revs); i++) { - if (rev == ast_silicon_revs[i].rev) - return ast_silicon_revs[i].name; - } + for (i = 0; i < ARRAY_SIZE(ast_silicon_revs); i++) { + if (rev == ast_silicon_revs[i].rev) + return ast_silicon_revs[i].name; + } - return NULL; + return NULL; } static const uint8_t bmc_silicon_gens[] = { - [ast_g4] = 0x02, - [ast_g5] = 0x04, - [ast_g6] = 0x05, + [ast_g4] = 0x02, + [ast_g5] = 0x04, + [ast_g6] = 0x05, }; enum ast_generation rev_generation(uint32_t rev) { - switch ((rev >> 24) & 0xff) { - case 0x02: - return ast_g4; - case 0x04: - return ast_g5; - case 0x05: - return ast_g6; - default: - return -EINVAL; - } + switch ((rev >> 24) & 0xff) { + case 0x02: + return ast_g4; + case 0x04: + return ast_g5; + case 0x05: + return ast_g6; + default: + return -EINVAL; + } } bool rev_is_generation(uint32_t rev, enum ast_generation gen) { - assert(gen < ARRAY_SIZE(bmc_silicon_gens)); + assert(gen < ARRAY_SIZE(bmc_silicon_gens)); - return rev >> 24 == bmc_silicon_gens[gen]; + return rev >> 24 == bmc_silicon_gens[gen]; } int rev_stepping(uint32_t rev) { - return (rev >> 16) & 0xf; + return (rev >> 16) & 0xf; } diff --git a/src/shell.c b/src/shell.c index e638f01..1c56f13 100644 --- a/src/shell.c +++ b/src/shell.c @@ -14,101 +14,101 @@ ssize_t shell_get_output(const char *cmd, char *buf, size_t len) { - int _stdout, _stderr; - size_t remaining; - ssize_t ingress; - int cleanup; - ssize_t rc; - int p[2]; - - assert(cmd); - assert(buf); - assert(len); - - _stdout = dup(STDOUT); - if (_stdout == -1) - return -errno; - - _stderr = dup(STDERR); - if (_stderr == -1) { - rc = -errno; - goto cleanup_stdout_dup; - } - - rc = close(STDERR); - if (rc == -1) { - rc = -errno; - goto cleanup_stderr_dup; - } - - rc = open("/dev/null", O_WRONLY); - if (rc == -1) { - rc = -errno; - goto cleanup_stderr_close; - } - - rc = pipe(p); - if (rc == -1) { - rc = -errno; - goto cleanup_stderr_close; - } - - rc = dup2(p[1], STDOUT); - if (rc == -1) { - rc = -errno; - goto cleanup_pipe; - } - - assert(rc == STDOUT); - - errno = 0; - rc = system(cmd); - if (rc == -1) { - assert(errno); - rc = -errno; - goto cleanup_stdout; - } - - close(STDOUT); - close(p[1]); - p[1] = -1; - - remaining = len; - do { - ingress = read(p[0], buf, remaining); - if (ingress == -1) { - rc = -errno; - goto cleanup_stdout; - } - - buf += ingress; - remaining -= ingress; - } while (ingress && remaining); - - if (remaining) - *buf = '\0'; - else - *(buf - 1) = '\0'; - - rc = len - remaining; + int _stdout, _stderr; + size_t remaining; + ssize_t ingress; + int cleanup; + ssize_t rc; + int p[2]; + + assert(cmd); + assert(buf); + assert(len); + + _stdout = dup(STDOUT); + if (_stdout == -1) + return -errno; + + _stderr = dup(STDERR); + if (_stderr == -1) { + rc = -errno; + goto cleanup_stdout_dup; + } + + rc = close(STDERR); + if (rc == -1) { + rc = -errno; + goto cleanup_stderr_dup; + } + + rc = open("/dev/null", O_WRONLY); + if (rc == -1) { + rc = -errno; + goto cleanup_stderr_close; + } + + rc = pipe(p); + if (rc == -1) { + rc = -errno; + goto cleanup_stderr_close; + } + + rc = dup2(p[1], STDOUT); + if (rc == -1) { + rc = -errno; + goto cleanup_pipe; + } + + assert(rc == STDOUT); + + errno = 0; + rc = system(cmd); + if (rc == -1) { + assert(errno); + rc = -errno; + goto cleanup_stdout; + } + + close(STDOUT); + close(p[1]); + p[1] = -1; + + remaining = len; + do { + ingress = read(p[0], buf, remaining); + if (ingress == -1) { + rc = -errno; + goto cleanup_stdout; + } + + buf += ingress; + remaining -= ingress; + } while (ingress && remaining); + + if (remaining) + *buf = '\0'; + else + *(buf - 1) = '\0'; + + rc = len - remaining; cleanup_stdout: - cleanup = dup2(_stdout, STDOUT); - if (cleanup != STDOUT) - perror("dup2"); + cleanup = dup2(_stdout, STDOUT); + if (cleanup != STDOUT) + perror("dup2"); cleanup_pipe: - if (p[1] != -1) - close(p[1]); - close(p[0]); + if (p[1] != -1) + close(p[1]); + close(p[0]); cleanup_stderr_close: - dup2(_stderr, STDERR); + dup2(_stderr, STDERR); cleanup_stderr_dup: - close(_stderr); + close(_stderr); cleanup_stdout_dup: - close(_stdout); + close(_stdout); - return rc; + return rc; } diff --git a/src/sio.c b/src/sio.c index 3c32d75..571f3fa 100644 --- a/src/sio.c +++ b/src/sio.c @@ -13,119 +13,119 @@ int sio_init(struct sio *ctx) { - ctx->base = 0x2e; - return lpc_init(&ctx->io, "io"); + ctx->base = 0x2e; + return lpc_init(&ctx->io, "io"); } int sio_destroy(struct sio *ctx) { - return lpc_destroy(&ctx->io); + return lpc_destroy(&ctx->io); } int sio_lock(struct sio *ctx) { - return lpc_writeb(&ctx->io, SIO_ADDR(ctx), 0xaa); + return lpc_writeb(&ctx->io, SIO_ADDR(ctx), 0xaa); } int sio_unlock(struct sio *ctx) { - int rc; + int rc; - rc = lpc_writeb(&ctx->io, SIO_ADDR(ctx), 0xa5); - rc |= lpc_writeb(&ctx->io, SIO_ADDR(ctx), 0xa5); + rc = lpc_writeb(&ctx->io, SIO_ADDR(ctx), 0xa5); + rc |= lpc_writeb(&ctx->io, SIO_ADDR(ctx), 0xa5); - return rc; + return rc; } int sio_select(struct sio *ctx, enum sio_dev dev) { - return sio_writeb(ctx, 0x07, dev); + return sio_writeb(ctx, 0x07, dev); } static int sio_present(struct sio *ctx) { - uint8_t dev; - int rc; + uint8_t dev; + int rc; - logd("Probing 0x%x for SuperIO\n", ctx->base); + logd("Probing 0x%x for SuperIO\n", ctx->base); - /* Dumb heuristics as we don't have access to the LPCHC */ - rc = sio_unlock(ctx); - logt("Unlocking SuperIO: %d\n", rc); - if (rc) - goto out; + /* Dumb heuristics as we don't have access to the LPCHC */ + rc = sio_unlock(ctx); + logt("Unlocking SuperIO: %d\n", rc); + if (rc) + goto out; - rc = sio_select(ctx, sio_suart1); - logt("Selecting SuperIO device %d (SUART1): %d\n", sio_suart1, rc); - if (rc) - goto out; + rc = sio_select(ctx, sio_suart1); + logt("Selecting SuperIO device %d (SUART1): %d\n", sio_suart1, rc); + if (rc) + goto out; - rc = sio_readb(ctx, 0x07, &dev); - logt("Found device %d selected: %d\n", dev, rc); - if (rc) - goto out; + rc = sio_readb(ctx, 0x07, &dev); + logt("Found device %d selected: %d\n", dev, rc); + if (rc) + goto out; - rc = (dev == sio_suart1); - if (!rc) - goto out; + rc = (dev == sio_suart1); + if (!rc) + goto out; - rc = sio_select(ctx, sio_suart4); - logt("Selecting SuperIO device %d (SUART4): %d\n", sio_suart4, rc); - if (rc) - goto out; + rc = sio_select(ctx, sio_suart4); + logt("Selecting SuperIO device %d (SUART4): %d\n", sio_suart4, rc); + if (rc) + goto out; - rc = sio_readb(ctx, 0x07, &dev); - logt("Found device %d selected: %d\n", dev, rc); - if (rc) - goto out; + rc = sio_readb(ctx, 0x07, &dev); + logt("Found device %d selected: %d\n", dev, rc); + if (rc) + goto out; - rc = (dev == sio_suart4); + rc = (dev == sio_suart4); out: - sio_lock(ctx); - logt("Locking SuperIO\n"); + sio_lock(ctx); + logt("Locking SuperIO\n"); - return rc; + return rc; } int sio_probe(struct sio *ctx) { - bool found; - - ctx->base = 0x2e; - found = sio_present(ctx); - if (!found) { - ctx->base = 0x4e; - found = sio_present(ctx); - } - - if (found) { - logd("Found SuperIO device at 0x%" PRIx16 "\n", ctx->base); - } else { - logd("SuperIO disabled\n"); - } - - return found; + bool found; + + ctx->base = 0x2e; + found = sio_present(ctx); + if (!found) { + ctx->base = 0x4e; + found = sio_present(ctx); + } + + if (found) { + logd("Found SuperIO device at 0x%" PRIx16 "\n", ctx->base); + } else { + logd("SuperIO disabled\n"); + } + + return found; } int sio_readb(struct sio *ctx, uint32_t addr, uint8_t *val) { - int rc; + int rc; - rc = lpc_writeb(&ctx->io, SIO_ADDR(ctx), addr); - if (rc) - return rc; + rc = lpc_writeb(&ctx->io, SIO_ADDR(ctx), addr); + if (rc) + return rc; - return lpc_readb(&ctx->io, SIO_DATA(ctx), val); + return lpc_readb(&ctx->io, SIO_DATA(ctx), val); } int sio_writeb(struct sio *ctx, uint32_t addr, uint8_t val) { - int rc; + int rc; - rc = lpc_writeb(&ctx->io, SIO_ADDR(ctx), addr); - if (rc) - return rc; + rc = lpc_writeb(&ctx->io, SIO_ADDR(ctx), addr); + if (rc) + return rc; - return lpc_writeb(&ctx->io, SIO_DATA(ctx), val); + return lpc_writeb(&ctx->io, SIO_DATA(ctx), val); } diff --git a/src/sio.h b/src/sio.h index 56f4f34..b6157f7 100644 --- a/src/sio.h +++ b/src/sio.h @@ -9,22 +9,20 @@ #include "lpc.h" -enum sio_dev -{ - sio_suart1 = 0x02, - sio_suart2 = 0x03, - sio_wakeup = 0x04, - sio_gpio = 0x07, - sio_suart3 = 0x0b, - sio_suart4 = 0x0c, - sio_ilpc = 0x0d, - sio_mbox = 0x0e, +enum sio_dev { + sio_suart1 = 0x02, + sio_suart2 = 0x03, + sio_wakeup = 0x04, + sio_gpio = 0x07, + sio_suart3 = 0x0b, + sio_suart4 = 0x0c, + sio_ilpc = 0x0d, + sio_mbox = 0x0e, }; -struct sio -{ - struct lpc io; - uint16_t base; +struct sio { + struct lpc io; + uint16_t base; }; int sio_init(struct sio *ctx); diff --git a/src/soc.c b/src/soc.c index 50adcbe..be25a64 100644 --- a/src/soc.c +++ b/src/soc.c @@ -61,7 +61,8 @@ int soc_from_rev(struct soc *ctx, struct ahb *ahb, uint32_t rev) if (!(rev_is_generation(rev, ast_g4) || rev_is_generation(rev, ast_g5) || rev_is_generation(rev, ast_g6))) { - loge("Found unsupported SoC generation: 0x%08" PRIx32 "\n", rev); + loge("Found unsupported SoC generation: 0x%08" PRIx32 "\n", + rev); return -ENOTSUP; } @@ -73,11 +74,13 @@ int soc_from_rev(struct soc *ctx, struct ahb *ahb, uint32_t rev) } /* soc_bus_enumerate_devices() and soc_device_bind_driver() mutually recurse */ -static int -soc_bus_enumerate_devices(struct soc *ctx, struct soc_device *dev, int bus, struct soc_driver **drivers, size_t n_drivers); +static int soc_bus_enumerate_devices(struct soc *ctx, struct soc_device *dev, + int bus, struct soc_driver **drivers, + size_t n_drivers); -static int -soc_device_bind_driver(struct soc *ctx, struct soc_device *parent, int node, struct soc_driver **drivers, size_t n_drivers) +static int soc_device_bind_driver(struct soc *ctx, struct soc_device *parent, + int node, struct soc_driver **drivers, + size_t n_drivers) { struct soc_device *dev; char path[PATH_MAX]; @@ -107,7 +110,8 @@ soc_device_bind_driver(struct soc *ctx, struct soc_device *parent, int node, str is_mfd = !fdt_node_check_compatible(ctx->fdt.start, node, "simple-mfd"); if (is_bus || is_mfd) { - rc = soc_bus_enumerate_devices(ctx, dev, node, drivers, n_drivers); + rc = soc_bus_enumerate_devices(ctx, dev, node, drivers, + n_drivers); if (rc < 0) { return rc; } @@ -116,8 +120,10 @@ soc_device_bind_driver(struct soc *ctx, struct soc_device *parent, int node, str for (i = 0; i < n_drivers; i++) { const struct soc_device_id *entry; - for (entry = drivers[i]->matches; entry && entry->compatible; entry++) { - if (fdt_node_check_compatible(ctx->fdt.start, node, entry->compatible)) { + for (entry = drivers[i]->matches; entry && entry->compatible; + entry++) { + if (fdt_node_check_compatible(ctx->fdt.start, node, + entry->compatible)) { continue; } @@ -138,12 +144,15 @@ soc_device_bind_driver(struct soc *ctx, struct soc_device *parent, int node, str return 0; } -static int soc_bus_enumerate_devices(struct soc *ctx, struct soc_device *dev, int bus, struct soc_driver **drivers, size_t n_drivers) +static int soc_bus_enumerate_devices(struct soc *ctx, struct soc_device *dev, + int bus, struct soc_driver **drivers, + size_t n_drivers) { int node; int rc; - fdt_for_each_subnode(node, ctx->fdt.start, bus) { + fdt_for_each_subnode(node, ctx->fdt.start, bus) + { rc = soc_device_bind_driver(ctx, dev, node, drivers, n_drivers); if (rc < 0) { return rc; @@ -220,8 +229,7 @@ void soc_destroy(struct soc *ctx) free(ctx->fdt.start); } -int soc_device_match_node(struct soc *ctx, - const struct soc_device_id table[], +int soc_device_match_node(struct soc *ctx, const struct soc_device_id table[], struct soc_device_node *dn) { const char *pval; @@ -356,7 +364,8 @@ int soc_device_from_type(struct soc *ctx, const char *type, logd("fdt: Searching devicetree for type '%s'\n", type); - fdt_for_each_subnode(node, ctx->fdt.start, 0) { + fdt_for_each_subnode(node, ctx->fdt.start, 0) + { const char *found; found = fdt_getprop(ctx->fdt.start, node, "device_type", NULL); @@ -372,9 +381,9 @@ int soc_device_from_type(struct soc *ctx, const char *type, return -ENOENT; } -static int -soc_device_resolve_node(struct soc *ctx, const struct soc_device_node *src, - struct soc_device_node *dst) +static int soc_device_resolve_node(struct soc *ctx, + const struct soc_device_node *src, + struct soc_device_node *dst) { bool is_mfd; int parent; @@ -386,7 +395,8 @@ soc_device_resolve_node(struct soc *ctx, const struct soc_device_node *src, return -EINVAL; } - is_mfd = !fdt_node_check_compatible(ctx->fdt.start, parent, "simple-mfd"); + is_mfd = !fdt_node_check_compatible(ctx->fdt.start, parent, + "simple-mfd"); dst->offset = is_mfd ? parent : src->offset; return 0; @@ -412,7 +422,8 @@ int soc_device_get_memory_index(struct soc *ctx, char path[PATH_MAX]; int rc; - rc = fdt_get_path(ctx->fdt.start, dn->offset, path, sizeof(path)); + rc = fdt_get_path(ctx->fdt.start, dn->offset, path, + sizeof(path)); if (rc < 0) { loge("fdt: Failed to determine node path while reporting failure to find reg property for offset %d\n", dn->offset); @@ -435,9 +446,10 @@ int soc_device_get_memory_index(struct soc *ctx, return 0; } -int -soc_device_get_memory_region_named(struct soc *ctx, const struct soc_device_node *dn, - const char *name, struct soc_region *region) +int soc_device_get_memory_region_named(struct soc *ctx, + const struct soc_device_node *dn, + const char *name, + struct soc_region *region) { struct soc_device_node rdn; const uint32_t *regions; @@ -446,13 +458,16 @@ soc_device_get_memory_region_named(struct soc *ctx, const struct soc_device_node int idx; int len; - idx = fdt_stringlist_search(ctx->fdt.start, dn->offset, "memory-region-names", name); + idx = fdt_stringlist_search(ctx->fdt.start, dn->offset, + "memory-region-names", name); if (idx < 0) { - loge("fdt: No memory region named '%s' for node %d: %d\n", name, dn->offset, idx); + loge("fdt: No memory region named '%s' for node %d: %d\n", name, + dn->offset, idx); return -ENOENT; } - regions = fdt_getprop(ctx->fdt.start, dn->offset, "memory-region", &len); + regions = + fdt_getprop(ctx->fdt.start, dn->offset, "memory-region", &len); if (len < 0) { loge("fdt: Failed to find 'memory-region' property in node %d: %d\n", dn->offset, len); @@ -469,7 +484,9 @@ soc_device_get_memory_region_named(struct soc *ctx, const struct soc_device_node offset = fdt_node_offset_by_phandle(ctx->fdt.start, phandle); if (offset < 0) { - loge("fdt: Failed to find node for phandle %"PRIu32" at index %d: %d\n", phandle, idx, offset); + loge("fdt: Failed to find node for phandle %" PRIu32 + " at index %d: %d\n", + phandle, idx, offset); return -EUCLEAN; } @@ -493,9 +510,11 @@ static void *soc_device_init_driver(struct soc *ctx, struct soc_device *dev) char path[PATH_MAX]; int rc; - rc = fdt_get_path(ctx->fdt.start, dev->node.offset, path, sizeof(path)); + rc = fdt_get_path(ctx->fdt.start, dev->node.offset, path, + sizeof(path)); if (rc < 0) { - loge("Failed to get path for offset %d\n", dev->node.offset); + loge("Failed to get path for offset %d\n", + dev->node.offset); return NULL; } @@ -526,7 +545,8 @@ void *soc_driver_get_drvdata(struct soc *soc, const struct soc_driver *match) return NULL; } -void *soc_driver_get_drvdata_by_name(struct soc *soc, const struct soc_driver *match, +void *soc_driver_get_drvdata_by_name(struct soc *soc, + const struct soc_driver *match, const char *name) { struct soc_device_node dn; @@ -545,7 +565,8 @@ void *soc_driver_get_drvdata_by_name(struct soc *soc, const struct soc_driver *m } if (dev->driver != match) { - logi("Failed to match driver %s on device %s", match->name, name); + logi("Failed to match driver %s on device %s", + match->name, name); return NULL; } @@ -555,7 +576,8 @@ void *soc_driver_get_drvdata_by_name(struct soc *soc, const struct soc_driver *m return NULL; } -void *soc_driver_get_drvdata_by_node(struct soc *soc, const struct soc_device_node *dn) +void *soc_driver_get_drvdata_by_node(struct soc *soc, + const struct soc_device_node *dn) { struct soc_device *dev; @@ -570,8 +592,8 @@ void *soc_driver_get_drvdata_by_node(struct soc *soc, const struct soc_device_no return NULL; } -int -soc_bridge_controller_register(struct soc *ctx, struct bridgectl *bridge, const struct bridgectl_ops *ops) +int soc_bridge_controller_register(struct soc *ctx, struct bridgectl *bridge, + const struct bridgectl_ops *ops) { bridge->ops = ops; list_add(&ctx->bridges, &bridge->entry); @@ -579,19 +601,20 @@ soc_bridge_controller_register(struct soc *ctx, struct bridgectl *bridge, const return 0; } -void -soc_bridge_controller_unregister(struct soc *ctx __unused, struct bridgectl *bridge) +void soc_bridge_controller_unregister(struct soc *ctx __unused, + struct bridgectl *bridge) { list_del(&bridge->entry); } static void soc_init_bridge_controllers(struct soc *ctx) { - static const char *compatible = "bridge-controller"; + static const char *compatible = "bridge-controller"; struct soc_device *dev; list_for_each(&ctx->devices, dev, entry) { - if (fdt_node_check_compatible(ctx->fdt.start, dev->node.offset, compatible)) { + if (fdt_node_check_compatible(ctx->fdt.start, dev->node.offset, + compatible)) { continue; } @@ -599,7 +622,8 @@ static void soc_init_bridge_controllers(struct soc *ctx) continue; } - logd("Initialised %s AHB bridge controller\n", dev->driver->name); + logd("Initialised %s AHB bridge controller\n", + dev->driver->name); } } @@ -614,7 +638,8 @@ void soc_list_bridge_controllers(struct soc *ctx) } } -int soc_probe_bridge_controllers(struct soc *ctx, enum bridge_mode *discovered, const char *name) +int soc_probe_bridge_controllers(struct soc *ctx, enum bridge_mode *discovered, + const char *name) { enum bridge_mode current, aggregate; struct bridgectl *bridge; @@ -633,7 +658,8 @@ int soc_probe_bridge_controllers(struct soc *ctx, enum bridge_mode *discovered, /* Write the report to stdout */ if ((rc = bridgectl_report(bridge, 1, ¤t)) < 0) { - loge("Failed to generate %s report: %d\n", bridgectl_name(bridge), rc); + loge("Failed to generate %s report: %d\n", + bridgectl_name(bridge), rc); error = rc; } diff --git a/src/soc.h b/src/soc.h index 7439b4f..e7fa172 100644 --- a/src/soc.h +++ b/src/soc.h @@ -41,14 +41,14 @@ static inline int soc_stepping(struct soc *ctx) return rev_stepping(ctx->rev); } -static inline ssize_t -soc_read(struct soc *ctx, uint32_t phys, void *buf, size_t len) +static inline ssize_t soc_read(struct soc *ctx, uint32_t phys, void *buf, + size_t len) { return ahb_read(ctx->ahb, phys, buf, len); } -static inline ssize_t -soc_write(struct soc *ctx, uint32_t phys, const void *buf, size_t len) +static inline ssize_t soc_write(struct soc *ctx, uint32_t phys, const void *buf, + size_t len) { return ahb_write(ctx->ahb, phys, buf, len); } @@ -63,14 +63,14 @@ static inline int soc_writel(struct soc *ctx, uint32_t phys, uint32_t val) return ahb_writel(ctx->ahb, phys, val); } -static inline ssize_t -soc_siphon_out(struct soc *ctx, uint32_t phys, size_t len, int outfd) +static inline ssize_t soc_siphon_out(struct soc *ctx, uint32_t phys, size_t len, + int outfd) { return ahb_siphon_out(ctx->ahb, phys, len, outfd); } -static inline ssize_t -soc_siphon_in(struct soc *ctx, uint32_t phys, ssize_t length, int infd) +static inline ssize_t soc_siphon_in(struct soc *ctx, uint32_t phys, + ssize_t length, int infd) { return ahb_siphon_in(ctx->ahb, phys, length, infd); } @@ -105,8 +105,7 @@ struct soc_device_id { const void *data; }; -int soc_device_match_node(struct soc *ctx, - const struct soc_device_id table[], +int soc_device_match_node(struct soc *ctx, const struct soc_device_id table[], struct soc_device_node *dn); int soc_device_from_name(struct soc *ctx, const char *name, @@ -128,23 +127,24 @@ struct soc_region { uint32_t length; }; -int -soc_device_get_memory_index(struct soc *ctx, const struct soc_device_node *dn, - int index, struct soc_region *region); +int soc_device_get_memory_index(struct soc *ctx, + const struct soc_device_node *dn, int index, + struct soc_region *region); -static inline int -soc_device_get_memory(struct soc *ctx, const struct soc_device_node *dn, - struct soc_region *region) +static inline int soc_device_get_memory(struct soc *ctx, + const struct soc_device_node *dn, + struct soc_region *region) { return soc_device_get_memory_index(ctx, dn, 0, region); } -int -soc_device_get_memory_region_named(struct soc *ctx, const struct soc_device_node *dn, - const char *name, struct soc_region *region); +int soc_device_get_memory_region_named(struct soc *ctx, + const struct soc_device_node *dn, + const char *name, + struct soc_region *region); struct soc_driver { - const char* name; + const char *name; const struct soc_device_id *matches; int (*init)(struct soc *soc, struct soc_device *dev); void (*destroy)(struct soc_device *dev); @@ -154,15 +154,19 @@ AUTODATA_TYPE(soc_drivers, struct soc_driver); #define REGISTER_SOC_DRIVER(sd) AUTODATA_SYM(soc_drivers, sd) void *soc_driver_get_drvdata(struct soc *soc, const struct soc_driver *match); -void *soc_driver_get_drvdata_by_name(struct soc *soc, const struct soc_driver *match, const char - *name); -void *soc_driver_get_drvdata_by_node(struct soc *soc, const struct soc_device_node *dn); +void *soc_driver_get_drvdata_by_name(struct soc *soc, + const struct soc_driver *match, + const char *name); +void *soc_driver_get_drvdata_by_node(struct soc *soc, + const struct soc_device_node *dn); int soc_bridge_controller_register(struct soc *soc, struct bridgectl *bridge, const struct bridgectl_ops *ops); -void soc_bridge_controller_unregister(struct soc *soc, struct bridgectl *bridge); +void soc_bridge_controller_unregister(struct soc *soc, + struct bridgectl *bridge); void soc_list_bridge_controllers(struct soc *soc); -int soc_probe_bridge_controllers(struct soc *soc, enum bridge_mode *discovered, const char *name); +int soc_probe_bridge_controllers(struct soc *soc, enum bridge_mode *discovered, + const char *name); #endif diff --git a/src/soc/bridge-ids.h b/src/soc/bridge-ids.h index 20f3f47..f26e038 100644 --- a/src/soc/bridge-ids.h +++ b/src/soc/bridge-ids.h @@ -4,13 +4,13 @@ #ifndef _SOC_BRIDGE_IDS_H #define _SOC_BRIDGE_IDS_H -#define AST2500_DEBUG_UART_GATE 0 +#define AST2500_DEBUG_UART_GATE 0 -#define AST2600_DEBUG_UART1_GATE 0 -#define AST2600_DEBUG_UART5_GATE 1 -#define AST2600_P2A_GATE 2 -#define AST2600_XDMA_GATE 3 -#define AST2600_XDMA_VGA_GATE 4 -#define AST2600_ILPC_GATE 5 +#define AST2600_DEBUG_UART1_GATE 0 +#define AST2600_DEBUG_UART5_GATE 1 +#define AST2600_P2A_GATE 2 +#define AST2600_XDMA_GATE 3 +#define AST2600_XDMA_VGA_GATE 4 +#define AST2600_ILPC_GATE 5 #endif diff --git a/src/soc/bridgectl.c b/src/soc/bridgectl.c index ac9f026..4c13aaa 100644 --- a/src/soc/bridgectl.c +++ b/src/soc/bridgectl.c @@ -8,26 +8,28 @@ #include static const char *bridge_mode_description[] = { - [bm_permissive] = "Permissive", - [bm_restricted] = "Restricted", - [bm_disabled] = "Disabled", + [bm_permissive] = "Permissive", + [bm_restricted] = "Restricted", + [bm_disabled] = "Disabled", }; static const enum log_colour bridge_mode_colour[] = { - [bm_permissive] = colour_red, - [bm_restricted] = colour_yellow, - [bm_disabled] = colour_green, + [bm_permissive] = colour_red, + [bm_restricted] = colour_yellow, + [bm_disabled] = colour_green, }; -void bridgectl_log_status(struct bridgectl *ctx, int fd, enum bridge_mode status) +void bridgectl_log_status(struct bridgectl *ctx, int fd, + enum bridge_mode status) { - int rc; + int rc; - log_highlight(fd, bridge_mode_colour[status], "%s:\t%s", bridgectl_name(ctx), bridge_mode_description[status]); + log_highlight(fd, bridge_mode_colour[status], "%s:\t%s", + bridgectl_name(ctx), bridge_mode_description[status]); - rc = write(fd, "\n", 1); - if (rc < 0) { - perror("write"); - return; - } + rc = write(fd, "\n", 1); + if (rc < 0) { + perror("write"); + return; + } } diff --git a/src/soc/bridgectl.h b/src/soc/bridgectl.h index 3887161..97ae87b 100644 --- a/src/soc/bridgectl.h +++ b/src/soc/bridgectl.h @@ -11,35 +11,38 @@ enum bridge_mode { bm_permissive, bm_restricted, bm_disabled }; struct bridgectl; struct bridgectl_ops { - const char *(*name)(struct bridgectl *ctx); - int (*enforce)(struct bridgectl *ctx, enum bridge_mode mode); - int (*status)(struct bridgectl *ctx, enum bridge_mode *mode); - int (*report)(struct bridgectl *ctx, int fd, enum bridge_mode *mode); + const char *(*name)(struct bridgectl *ctx); + int (*enforce)(struct bridgectl *ctx, enum bridge_mode mode); + int (*status)(struct bridgectl *ctx, enum bridge_mode *mode); + int (*report)(struct bridgectl *ctx, int fd, enum bridge_mode *mode); }; struct bridgectl { - struct list_node entry; - const struct bridgectl_ops *ops; + struct list_node entry; + const struct bridgectl_ops *ops; }; static inline const char *bridgectl_name(struct bridgectl *ctx) { - return ctx->ops->name(ctx); + return ctx->ops->name(ctx); } -static inline int bridgectl_enforce(struct bridgectl *ctx, enum bridge_mode mode) +static inline int bridgectl_enforce(struct bridgectl *ctx, + enum bridge_mode mode) { - return ctx->ops->enforce(ctx, mode); + return ctx->ops->enforce(ctx, mode); } -static inline int bridgectl_status(struct bridgectl *ctx, enum bridge_mode *mode) +static inline int bridgectl_status(struct bridgectl *ctx, + enum bridge_mode *mode) { - return ctx->ops->status(ctx, mode); + return ctx->ops->status(ctx, mode); } -static inline int bridgectl_report(struct bridgectl *ctx, int fd, enum bridge_mode *mode) +static inline int bridgectl_report(struct bridgectl *ctx, int fd, + enum bridge_mode *mode) { - return ctx->ops->report(ctx, fd, mode); + return ctx->ops->report(ctx, fd, mode); } void bridgectl_log_status(struct bridgectl *ctx, int fd, enum bridge_mode mode); diff --git a/src/soc/bridges.c b/src/soc/bridges.c index 82a07e7..3dce50d 100644 --- a/src/soc/bridges.c +++ b/src/soc/bridges.c @@ -14,289 +14,311 @@ #include struct bridge_gate_desc { - uint32_t reg; - uint32_t mask; + uint32_t reg; + uint32_t mask; }; struct bridge_gate_pdata { - const struct bridge_gate_desc *descs; - size_t ndescs; + const struct bridge_gate_desc *descs; + size_t ndescs; }; -#define AST2500_SCU_MISC 0x02c -#define AST2500_SCU_MISC_UART_DBG (1 << 10) +#define AST2500_SCU_MISC 0x02c +#define AST2500_SCU_MISC_UART_DBG (1 << 10) static const struct bridge_gate_desc ast2500_bridge_gates[] = { - [AST2500_DEBUG_UART_GATE] = { .reg = AST2500_SCU_MISC, .mask = AST2500_SCU_MISC_UART_DBG }, + [AST2500_DEBUG_UART_GATE] = { .reg = AST2500_SCU_MISC, + .mask = AST2500_SCU_MISC_UART_DBG }, }; static const struct bridge_gate_pdata ast2500_bridge_pdata = { - .descs = ast2500_bridge_gates, - .ndescs = ARRAY_SIZE(ast2500_bridge_gates), + .descs = ast2500_bridge_gates, + .ndescs = ARRAY_SIZE(ast2500_bridge_gates), }; -#define AST2600_SCU_DBGCTL1 0x0c8 -#define AST2600_SCU_DBGCTL1_XDMA_VGA (1 << 8) -#define AST2600_SCU_DBGCTL1_XDMA (1 << 2) -#define AST2600_SCU_DBGCTL1_UART5_DBG (1 << 1) -#define AST2600_SCU_DBGCTL1_P2A (1 << 0) -#define AST2600_SCU_DBGCTL2 0x0d8 -#define AST2600_SCU_DBGCTL2_UART1_DBG (1 << 3) -#define AST2600_SCU_DBGCTL2_ILPC (1 << 1) +#define AST2600_SCU_DBGCTL1 0x0c8 +#define AST2600_SCU_DBGCTL1_XDMA_VGA (1 << 8) +#define AST2600_SCU_DBGCTL1_XDMA (1 << 2) +#define AST2600_SCU_DBGCTL1_UART5_DBG (1 << 1) +#define AST2600_SCU_DBGCTL1_P2A (1 << 0) +#define AST2600_SCU_DBGCTL2 0x0d8 +#define AST2600_SCU_DBGCTL2_UART1_DBG (1 << 3) +#define AST2600_SCU_DBGCTL2_ILPC (1 << 1) static const struct bridge_gate_desc ast2600_bridge_gates[] = { - [AST2600_DEBUG_UART1_GATE] = - { .reg = AST2600_SCU_DBGCTL2, .mask = AST2600_SCU_DBGCTL2_UART1_DBG }, - [AST2600_DEBUG_UART5_GATE] = - { .reg = AST2600_SCU_DBGCTL1, .mask = AST2600_SCU_DBGCTL1_UART5_DBG }, - [AST2600_P2A_GATE] = - { .reg = AST2600_SCU_DBGCTL1, .mask = AST2600_SCU_DBGCTL1_P2A }, - [AST2600_XDMA_GATE] = - { .reg = AST2600_SCU_DBGCTL1, .mask = AST2600_SCU_DBGCTL1_XDMA }, - [AST2600_XDMA_VGA_GATE] = - { .reg = AST2600_SCU_DBGCTL1, .mask = AST2600_SCU_DBGCTL1_XDMA_VGA }, - [AST2600_ILPC_GATE] = - { .reg = AST2600_SCU_DBGCTL2, .mask = AST2600_SCU_DBGCTL2_ILPC }, + [AST2600_DEBUG_UART1_GATE] = { .reg = AST2600_SCU_DBGCTL2, + .mask = AST2600_SCU_DBGCTL2_UART1_DBG }, + [AST2600_DEBUG_UART5_GATE] = { .reg = AST2600_SCU_DBGCTL1, + .mask = AST2600_SCU_DBGCTL1_UART5_DBG }, + [AST2600_P2A_GATE] = { .reg = AST2600_SCU_DBGCTL1, + .mask = AST2600_SCU_DBGCTL1_P2A }, + [AST2600_XDMA_GATE] = { .reg = AST2600_SCU_DBGCTL1, + .mask = AST2600_SCU_DBGCTL1_XDMA }, + [AST2600_XDMA_VGA_GATE] = { .reg = AST2600_SCU_DBGCTL1, + .mask = AST2600_SCU_DBGCTL1_XDMA_VGA }, + [AST2600_ILPC_GATE] = { .reg = AST2600_SCU_DBGCTL2, + .mask = AST2600_SCU_DBGCTL2_ILPC }, }; static const struct bridge_gate_pdata ast2600_bridge_pdata = { - .descs = ast2600_bridge_gates, - .ndescs = ARRAY_SIZE(ast2600_bridge_gates), + .descs = ast2600_bridge_gates, + .ndescs = ARRAY_SIZE(ast2600_bridge_gates), }; static const struct soc_device_id bridges_matches[] = { - { .compatible = "aspeed,ast2500-bridge-controller", .data = &ast2500_bridge_pdata }, - { .compatible = "aspeed,ast2600-bridge-controller", .data = &ast2600_bridge_pdata }, - { }, + { .compatible = "aspeed,ast2500-bridge-controller", + .data = &ast2500_bridge_pdata }, + { .compatible = "aspeed,ast2600-bridge-controller", + .data = &ast2600_bridge_pdata }, + {}, }; struct bridges { - struct soc *soc; - struct soc_region scu; - const struct bridge_gate_pdata *pdata; + struct soc *soc; + struct soc_region scu; + const struct bridge_gate_pdata *pdata; }; int bridges_driver_init(struct soc *soc, struct soc_device *dev) { - struct bridges *ctx; - int rc; + struct bridges *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->scu)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->scu)) < 0) { + goto cleanup_ctx; + } - ctx->soc = soc; - ctx->pdata = soc_device_get_match_data(soc, bridges_matches, &dev->node); + ctx->soc = soc; + ctx->pdata = + soc_device_get_match_data(soc, bridges_matches, &dev->node); - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void bridges_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } static const struct soc_driver bridges_driver = { - .name = "bridge-controller", - .matches = bridges_matches, - .init = bridges_driver_init, - .destroy = bridges_driver_destroy, + .name = "bridge-controller", + .matches = bridges_matches, + .init = bridges_driver_init, + .destroy = bridges_driver_destroy, }; REGISTER_SOC_DRIVER(bridges_driver); -static struct bridges *bridges_get_by_prop(struct soc *soc, const uint32_t *cells) +static struct bridges *bridges_get_by_prop(struct soc *soc, + const uint32_t *cells) { - struct soc_device_node bdn; - int phandle; - int offset; + struct soc_device_node bdn; + int phandle; + int offset; - assert(cells); + assert(cells); - phandle = be32toh(cells[0]); - offset = fdt_node_offset_by_phandle(soc->fdt.start, phandle); - if (offset < 0) { - loge("fdt: Failed to find node for phandle %"PRIu32" at index %d: %d\n", - phandle, 0, offset); - return NULL; - } + phandle = be32toh(cells[0]); + offset = fdt_node_offset_by_phandle(soc->fdt.start, phandle); + if (offset < 0) { + loge("fdt: Failed to find node for phandle %" PRIu32 + " at index %d: %d\n", + phandle, 0, offset); + return NULL; + } - bdn.fdt = &soc->fdt; - bdn.offset = offset; + bdn.fdt = &soc->fdt; + bdn.offset = offset; - return soc_driver_get_drvdata_by_node(soc, &bdn); + return soc_driver_get_drvdata_by_node(soc, &bdn); } struct bridges *bridges_get_by_device(struct soc *soc, struct soc_device *dev) { - const uint32_t *cells; - int len; - - cells = fdt_getprop(soc->fdt.start, dev->node.offset, "bridge-gates", &len); - if (len < 0) { - loge("Failed to find 'bridge-gates' property in node %d: %d\n", dev->node.offset, len); - return NULL; - } - - if ((long unsigned int)len < 2 * sizeof(*cells)) { - loge("Invalid value for 'bridge-gates' property, must be \n"); - return NULL; - } - - return bridges_get_by_prop(soc, cells); + const uint32_t *cells; + int len; + + cells = fdt_getprop(soc->fdt.start, dev->node.offset, "bridge-gates", + &len); + if (len < 0) { + loge("Failed to find 'bridge-gates' property in node %d: %d\n", + dev->node.offset, len); + return NULL; + } + + if ((long unsigned int)len < 2 * sizeof(*cells)) { + loge("Invalid value for 'bridge-gates' property, must be \n"); + return NULL; + } + + return bridges_get_by_prop(soc, cells); } -int bridges_device_get_gate_by_index(struct soc *soc, struct soc_device *dev, int index, - struct bridges **bridges, int *gate) +int bridges_device_get_gate_by_index(struct soc *soc, struct soc_device *dev, + int index, struct bridges **bridges, + int *gate) { - const uint32_t *cells; - int len; - - if (!gate) { - loge("'gate' parameter must point to a valid int object\n"); - return -EINVAL; - } - - cells = fdt_getprop(soc->fdt.start, dev->node.offset, "bridge-gates", &len); - if (len < 0) { - loge("Failed to find 'bridge-gates' property in node %d: %d\n", dev->node.offset, len); - return -ERANGE; - } - - if ((long unsigned int)len < 2 * sizeof(*cells)) { - loge("Invalid value for 'bridge-gates' property, must be \n"); - return -EINVAL; - } - - if ((long unsigned int)len <= (1 + index) * sizeof(*cells)) { - loge("Invalid index for 'bridge-gates' property: %d (%d)\n", index, (len / sizeof(*cells))); - return -EINVAL; - } - - if (bridges) { - struct bridges *_bridges; - - if (!(_bridges = bridges_get_by_prop(soc, cells))) { - return -EINVAL; - } - - *bridges = _bridges; - } - - *gate = be32toh(cells[1 + index]); - - return 0; + const uint32_t *cells; + int len; + + if (!gate) { + loge("'gate' parameter must point to a valid int object\n"); + return -EINVAL; + } + + cells = fdt_getprop(soc->fdt.start, dev->node.offset, "bridge-gates", + &len); + if (len < 0) { + loge("Failed to find 'bridge-gates' property in node %d: %d\n", + dev->node.offset, len); + return -ERANGE; + } + + if ((long unsigned int)len < 2 * sizeof(*cells)) { + loge("Invalid value for 'bridge-gates' property, must be \n"); + return -EINVAL; + } + + if ((long unsigned int)len <= (1 + index) * sizeof(*cells)) { + loge("Invalid index for 'bridge-gates' property: %d (%d)\n", + index, (len / sizeof(*cells))); + return -EINVAL; + } + + if (bridges) { + struct bridges *_bridges; + + if (!(_bridges = bridges_get_by_prop(soc, cells))) { + return -EINVAL; + } + + *bridges = _bridges; + } + + *gate = be32toh(cells[1 + index]); + + return 0; } -int bridges_device_get_gate(struct soc *soc, struct soc_device *dev, struct bridges **bridges, - int *gate) +int bridges_device_get_gate(struct soc *soc, struct soc_device *dev, + struct bridges **bridges, int *gate) { - return bridges_device_get_gate_by_index(soc, dev, 0, bridges, gate); + return bridges_device_get_gate_by_index(soc, dev, 0, bridges, gate); } -int bridges_device_get_gate_by_name(struct soc *soc, struct soc_device *dev, const char *name, - struct bridges **bridges, int *gate) +int bridges_device_get_gate_by_name(struct soc *soc, struct soc_device *dev, + const char *name, struct bridges **bridges, + int *gate) { - int idx; - int rc; - - idx = fdt_stringlist_search(soc->fdt.start, dev->node.offset, "bridge-gate-names", name); - if (idx < 0) { - loge("Failed to find 'bridge-gate-names' node: %d\n", idx); - return -EINVAL; - } - - rc = bridges_device_get_gate_by_index(soc, dev, idx, bridges, gate); - if (!rc) { - logt("Resolved bridge gate name '%s' to ID %d via device node %d\n", - name, *gate, dev->node.offset); - } - - return rc; + int idx; + int rc; + + idx = fdt_stringlist_search(soc->fdt.start, dev->node.offset, + "bridge-gate-names", name); + if (idx < 0) { + loge("Failed to find 'bridge-gate-names' node: %d\n", idx); + return -EINVAL; + } + + rc = bridges_device_get_gate_by_index(soc, dev, idx, bridges, gate); + if (!rc) { + logt("Resolved bridge gate name '%s' to ID %d via device node %d\n", + name, *gate, dev->node.offset); + } + + return rc; } static int bridges_configure(struct bridges *ctx, int bridge, bool enable) { - const struct bridge_gate_desc *desc; - uint32_t phys, val; - int rc; - - if (bridge < 0) { - logd("Invalid bridge identifier: %d\n", bridge); - return -EINVAL; - } - - if ((size_t)bridge >= ctx->pdata->ndescs) { - logd("Invalid bridge identifier: %d (%d)\n", bridge, ctx->pdata->ndescs); - return -EINVAL; - } - - desc = &ctx->pdata->descs[bridge]; - phys = ctx->scu.start + desc->reg; - if ((rc = soc_readl(ctx->soc, phys, &val)) < 0) { - loge("Failed to read bridge control register 0x%"PRIx32": %d\n", phys, rc); - return rc; - } - - /* Bridge control registers tend to set bits to disable the bridge */ - if (enable) { - val &= ~desc->mask; - } else { - val |= desc->mask; - } - - if ((rc = soc_writel(ctx->soc, phys, val)) < 0) { - loge("Failed to write 0x%"PRIx32" to bridge control register 0x%"PRIx32": %d\n", phys, rc); - return rc; - } - - return 0; + const struct bridge_gate_desc *desc; + uint32_t phys, val; + int rc; + + if (bridge < 0) { + logd("Invalid bridge identifier: %d\n", bridge); + return -EINVAL; + } + + if ((size_t)bridge >= ctx->pdata->ndescs) { + logd("Invalid bridge identifier: %d (%d)\n", bridge, + ctx->pdata->ndescs); + return -EINVAL; + } + + desc = &ctx->pdata->descs[bridge]; + phys = ctx->scu.start + desc->reg; + if ((rc = soc_readl(ctx->soc, phys, &val)) < 0) { + loge("Failed to read bridge control register 0x%" PRIx32 + ": %d\n", + phys, rc); + return rc; + } + + /* Bridge control registers tend to set bits to disable the bridge */ + if (enable) { + val &= ~desc->mask; + } else { + val |= desc->mask; + } + + if ((rc = soc_writel(ctx->soc, phys, val)) < 0) { + loge("Failed to write 0x%" PRIx32 + " to bridge control register 0x%" PRIx32 ": %d\n", + phys, rc); + return rc; + } + + return 0; } int bridges_enable(struct bridges *ctx, int bridge) { - return bridges_configure(ctx, bridge, true); + return bridges_configure(ctx, bridge, true); } int bridges_disable(struct bridges *ctx, int bridge) { - return bridges_configure(ctx, bridge, false); + return bridges_configure(ctx, bridge, false); } int bridges_status(struct bridges *ctx, int bridge) { - const struct bridge_gate_desc *desc; - uint32_t phys, val; - int rc; - - if (bridge < 0) { - logd("Invalid bridge identifier: %d\n", bridge); - return -EINVAL; - } - - if ((size_t)bridge >= ctx->pdata->ndescs) { - logd("Invalid bridge identifier: %d (%d)\n", bridge, ctx->pdata->ndescs); - return -EINVAL; - } - - desc = &ctx->pdata->descs[bridge]; - phys = ctx->scu.start + desc->reg; - if ((rc = soc_readl(ctx->soc, phys, &val)) < 0) { - loge("Failed to read bridge control register 0x%"PRIx32": %d\n", phys, rc); - return rc; - } - - /* Bridge control registers tend to set bits to disable the bridge */ - return !(val & desc->mask); + const struct bridge_gate_desc *desc; + uint32_t phys, val; + int rc; + + if (bridge < 0) { + logd("Invalid bridge identifier: %d\n", bridge); + return -EINVAL; + } + + if ((size_t)bridge >= ctx->pdata->ndescs) { + logd("Invalid bridge identifier: %d (%d)\n", bridge, + ctx->pdata->ndescs); + return -EINVAL; + } + + desc = &ctx->pdata->descs[bridge]; + phys = ctx->scu.start + desc->reg; + if ((rc = soc_readl(ctx->soc, phys, &val)) < 0) { + loge("Failed to read bridge control register 0x%" PRIx32 + ": %d\n", + phys, rc); + return rc; + } + + /* Bridge control registers tend to set bits to disable the bridge */ + return !(val & desc->mask); } diff --git a/src/soc/bridges.h b/src/soc/bridges.h index 3d05a9f..abcfe2f 100644 --- a/src/soc/bridges.h +++ b/src/soc/bridges.h @@ -10,12 +10,14 @@ struct bridges; struct bridges *bridges_get_by_device(struct soc *soc, struct soc_device *dev); -int bridges_device_get_gate(struct soc *soc, struct soc_device *dev, struct bridges **bridges, - int *gate); -int bridges_device_get_gate_by_index(struct soc *soc, struct soc_device *dev, int index, - struct bridges **bridges, int *gate); -int bridges_device_get_gate_by_name(struct soc *soc, struct soc_device *dev, const char *name, - struct bridges **bridges, int *gate); +int bridges_device_get_gate(struct soc *soc, struct soc_device *dev, + struct bridges **bridges, int *gate); +int bridges_device_get_gate_by_index(struct soc *soc, struct soc_device *dev, + int index, struct bridges **bridges, + int *gate); +int bridges_device_get_gate_by_name(struct soc *soc, struct soc_device *dev, + const char *name, struct bridges **bridges, + int *gate); int bridges_enable(struct bridges *ctx, int bridge); int bridges_disable(struct bridges *ctx, int bridge); diff --git a/src/soc/clk.c b/src/soc/clk.c index d69af38..4a11989 100644 --- a/src/soc/clk.c +++ b/src/soc/clk.c @@ -14,20 +14,20 @@ #include /* Shared registers between the AST2400 and 2500 will not (!) have a prefix */ -#define SCU_CLK_STOP 0x0c -#define SCU_CLK_STOP_UART3 BIT(25) +#define SCU_CLK_STOP 0x0c +#define SCU_CLK_STOP_UART3 BIT(25) -#define SCU_HW_STRAP 0x70 -#define SCU_HW_STRAP_UART_DBG_SEL BIT(29) -#define SCU_HW_STRAP_CLKIN_IN_MOD BIT(23) -#define SCU_HW_STRAP_SIO_DEC BIT(20) -#define SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_MASK GENMASK(11, 9) -#define SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_SHIFT 9 -#define SCU_HW_STRAP_ARM_CLK BIT(0) +#define SCU_HW_STRAP 0x70 +#define SCU_HW_STRAP_UART_DBG_SEL BIT(29) +#define SCU_HW_STRAP_CLKIN_IN_MOD BIT(23) +#define SCU_HW_STRAP_SIO_DEC BIT(20) +#define SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_MASK GENMASK(11, 9) +#define SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_SHIFT 9 +#define SCU_HW_STRAP_ARM_CLK BIT(0) -#define SCU_SILICON_REVISION 0x7c +#define SCU_SILICON_REVISION 0x7c -#define AST2500_SCU_H_PLL 0x24 +#define AST2500_SCU_H_PLL 0x24 union ast2500_h_pll_reg { uint32_t w; @@ -38,35 +38,35 @@ union ast2500_h_pll_reg { uint32_t off : 1; /* bit[19] */ uint32_t bypass : 1; /* bit[20] */ uint32_t reset : 1; /* bit[21] */ - uint32_t reserved : 10; /* bit[31:22] */ + uint32_t reserved : 10; /* bit[31:22] */ } b; }; /* We only care about SCU004 for now, revision is not required here */ -#define AST2600_SCU_SILICON_REVISION 0x04 +#define AST2600_SCU_SILICON_REVISION 0x04 -#define AST2600_SCU_H_PLL 0x200 +#define AST2600_SCU_H_PLL 0x200 union ast2600_h_pll_reg { uint32_t w; struct { - uint32_t m : 13; /* bit[12:0] */ - uint32_t n : 6; /* bit[18:13] */ - uint32_t p : 4; /* bit[22:19] */ - uint32_t off : 1; /* bit[23] */ - uint32_t bypass : 1; /* bit[24] */ - uint32_t reset : 1; /* bit[25] */ - uint32_t reserved : 6; /* bit[31:26] */ + uint32_t m : 13; /* bit[12:0] */ + uint32_t n : 6; /* bit[18:13] */ + uint32_t p : 4; /* bit[22:19] */ + uint32_t off : 1; /* bit[23] */ + uint32_t bypass : 1; /* bit[24] */ + uint32_t reset : 1; /* bit[25] */ + uint32_t reserved : 6; /* bit[31:26] */ } b; }; -#define AST2600_SCU_HW_STRAP1 0x500 -#define AST2600_SCU_HW_STRAP1_ARM_CLK BIT(0) -#define AST2600_SCU_HW_STRAP1_CPU_AXI_RATIO BIT(16) -#define AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_MASK GENMASK(12, 11) -#define AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_SHIFT 11 +#define AST2600_SCU_HW_STRAP1 0x500 +#define AST2600_SCU_HW_STRAP1_ARM_CLK BIT(0) +#define AST2600_SCU_HW_STRAP1_CPU_AXI_RATIO BIT(16) +#define AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_MASK GENMASK(12, 11) +#define AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_SHIFT 11 -#define AST2600_SCU_HW_STRAP1_CLEAR 0x504 +#define AST2600_SCU_HW_STRAP1_CLEAR 0x504 struct clk_ops { int64_t (*rate_ahb)(struct clk *ctx); @@ -99,19 +99,11 @@ int clk_enable(struct clk *ctx, enum clksrc src) int64_t ast2400_clk_rate_ahb(struct clk *ctx) { - static const uint32_t cpu_freqs_24_48[] = { - 384000000, - 360000000, - 336000000, - 408000000 - }; - - static const uint32_t cpu_freqs_25[] = { - 400000000, - 375000000, - 350000000, - 425000000 - }; + static const uint32_t cpu_freqs_24_48[] = { 384000000, 360000000, + 336000000, 408000000 }; + + static const uint32_t cpu_freqs_25[] = { 400000000, 375000000, + 350000000, 425000000 }; static const uint32_t ahb_div[] = { 1, 2, 4, 3 }; uint32_t strap, cpu_clk, div; @@ -164,7 +156,7 @@ int ast2400_clk_enable(struct clk *ctx, enum clksrc src) switch (src) { case clk_arm: return scu_writel(ctx->scu, SCU_SILICON_REVISION, - SCU_HW_STRAP_ARM_CLK); + SCU_HW_STRAP_ARM_CLK); case clk_uart3: if ((rc = scu_readl(ctx->scu, SCU_CLK_STOP, ®)) < 0) return rc; @@ -210,15 +202,13 @@ int64_t ast2500_clk_rate_ahb(struct clk *ctx) * H-PLL = CLKIN * [(M+1) / (N+1)] / (P+1) * The usual frequency for H-PLL with CLKIN 24 MHz is 792 MHz. */ - h_pll = (clk_25mhz ? 25 : 24) - * (h_pll_reg.b.m + 1) - / (h_pll_reg.b.n + 1) - / (h_pll_reg.b.p + 1); + h_pll = (clk_25mhz ? 25 : 24) * (h_pll_reg.b.m + 1) / + (h_pll_reg.b.n + 1) / (h_pll_reg.b.p + 1); logt("clk: ast2500: calculated h-pll is %d MHz\n", h_pll); /* AXI/AHB clock frequency ratio selection */ - ahb_ratio = (strap & SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_MASK) - >> SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_SHIFT; + ahb_ratio = (strap & SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_MASK) >> + SCU_HW_STRAP_AXI_CLK_FREQ_RATIO_SHIFT; if (!ahb_ratio) return -EINVAL; /* @@ -268,7 +258,7 @@ int ast2500_clk_enable(struct clk *ctx, enum clksrc src) switch (src) { case clk_arm: return scu_writel(ctx->scu, SCU_SILICON_REVISION, - SCU_HW_STRAP_ARM_CLK); + SCU_HW_STRAP_ARM_CLK); case clk_uart3: if ((rc = scu_readl(ctx->scu, SCU_CLK_STOP, ®)) < 0) return rc; @@ -306,17 +296,15 @@ static int64_t ast2600_clk_rate_ahb(struct clk *ctx) return rc; /* H-PLL = CLKIN (always 25 MHz) * (M+1/N+1) / P+1 */ - h_pll = 25 - * (h_pll_reg.b.m + 1) - / (h_pll_reg.b.n + 1) - / (h_pll_reg.b.p + 1); + h_pll = 25 * (h_pll_reg.b.m + 1) / (h_pll_reg.b.n + 1) / + (h_pll_reg.b.p + 1); logt("clk: ast2600: calculated h-pll is %d MHz\n", h_pll); cpu_axi_ratio = (strap & AST2600_SCU_HW_STRAP1_CPU_AXI_RATIO); /* AXI/AHB clock frequency ratio selection */ - ahb_ratio = (strap & AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_MASK) - >> AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_SHIFT; + ahb_ratio = (strap & AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_MASK) >> + AST2600_SCU_HW_STRAP1_AHB_FREQ_RATIO_SHIFT; if (!ahb_ratio) return -EINVAL; @@ -350,7 +338,7 @@ int ast2600_clk_disable(struct clk *ctx, enum clksrc src) return -ENOTSUP; return scu_writel(ctx->scu, AST2600_SCU_HW_STRAP1, - AST2600_SCU_HW_STRAP1_ARM_CLK); + AST2600_SCU_HW_STRAP1_ARM_CLK); } int ast2600_clk_enable(struct clk *ctx, enum clksrc src) @@ -359,7 +347,7 @@ int ast2600_clk_enable(struct clk *ctx, enum clksrc src) return -ENOTSUP; return scu_writel(ctx->scu, AST2600_SCU_HW_STRAP1_CLEAR, - AST2600_SCU_HW_STRAP1_ARM_CLK); + AST2600_SCU_HW_STRAP1_ARM_CLK); } static const struct clk_ops ast2600_clk_ops = { @@ -372,7 +360,7 @@ static const struct soc_device_id clk_match[] = { { .compatible = "aspeed,ast2400-clock", .data = &ast2400_clk_ops }, { .compatible = "aspeed,ast2500-clock", .data = &ast2500_clk_ops }, { .compatible = "aspeed,ast2600-clock", .data = &ast2600_clk_ops }, - { }, + {}, }; static int clk_driver_init(struct soc *soc, struct soc_device *dev) diff --git a/src/soc/debugctl.c b/src/soc/debugctl.c index 11986ae..1942a20 100644 --- a/src/soc/debugctl.c +++ b/src/soc/debugctl.c @@ -13,190 +13,196 @@ #include #include -#define SCU_MISC 0x02c -#define SCU_MISC_UART_DBG (1 << 10) -#define SCU_STRAP 0x070 -#define SCU_STRAP_DBG_SEL (1 << 29) +#define SCU_MISC 0x02c +#define SCU_MISC_UART_DBG (1 << 10) +#define SCU_STRAP 0x070 +#define SCU_STRAP_DBG_SEL (1 << 29) struct debugctl { - struct bridgectl ctl; - struct soc *soc; - struct soc_region region; - struct bridges *bridges; - int id; + struct bridgectl ctl; + struct soc *soc; + struct soc_region region; + struct bridges *bridges; + int id; }; #define to_debugctl(b) container_of((b), struct debugctl, ctl) struct bridgectl *debugctl_as_bridgectl(struct debugctl *ctx) { - return &ctx->ctl; + return &ctx->ctl; } static const char *debugctl_name(struct bridgectl *bridge __unused) { - return "debug"; + return "debug"; } static int debugctl_enforce(struct bridgectl *bridge, enum bridge_mode mode) { - struct debugctl *ctx = to_debugctl(bridge); + struct debugctl *ctx = to_debugctl(bridge); - if (mode == bm_disabled) { - return bridges_disable(ctx->bridges, ctx->id); - } + if (mode == bm_disabled) { + return bridges_disable(ctx->bridges, ctx->id); + } - /* The debug UART doesn't support restricted mode */ - return bridges_enable(ctx->bridges, ctx->id); + /* The debug UART doesn't support restricted mode */ + return bridges_enable(ctx->bridges, ctx->id); } static int debugctl_status(struct bridgectl *bridge, enum bridge_mode *mode) { - struct debugctl *ctx = to_debugctl(bridge); - int rc; + struct debugctl *ctx = to_debugctl(bridge); + int rc; - if ((rc = bridges_status(ctx->bridges, ctx->id)) < 0) { - loge("Failed to read debug controller status: %d\n", rc); - return rc; - } + if ((rc = bridges_status(ctx->bridges, ctx->id)) < 0) { + loge("Failed to read debug controller status: %d\n", rc); + return rc; + } - *mode = rc ? bm_permissive : bm_disabled; + *mode = rc ? bm_permissive : bm_disabled; - return 0; + return 0; } -static int ast2500_debugctl_report(struct bridgectl *bridge, int fd, enum bridge_mode *mode) +static int ast2500_debugctl_report(struct bridgectl *bridge, int fd, + enum bridge_mode *mode) { - struct debugctl *ctx = to_debugctl(bridge); - const char *description; - uint32_t strap; - int rc; + struct debugctl *ctx = to_debugctl(bridge); + const char *description; + uint32_t strap; + int rc; - if ((rc = debugctl_status(bridge, mode)) < 0) { - loge("Failed to read debug UART bridge status: %d\n", rc); - return rc; - } + if ((rc = debugctl_status(bridge, mode)) < 0) { + loge("Failed to read debug UART bridge status: %d\n", rc); + return rc; + } - bridgectl_log_status(bridge, fd, *mode); + bridgectl_log_status(bridge, fd, *mode); - if (*mode == bm_disabled) { - return 0; - } + if (*mode == bm_disabled) { + return 0; + } - if ((rc = soc_readl(ctx->soc, ctx->region.start + SCU_STRAP, &strap)) < 0) { - loge("Failed to read debug UART port state: %d\n", rc); - return rc; - } + if ((rc = soc_readl(ctx->soc, ctx->region.start + SCU_STRAP, &strap)) < + 0) { + loge("Failed to read debug UART port state: %d\n", rc); + return rc; + } - description = (strap & SCU_STRAP_DBG_SEL) ? "UART5" : "UART1"; - dprintf(fd, "\tDebug UART port: %s\n", description); + description = (strap & SCU_STRAP_DBG_SEL) ? "UART5" : "UART1"; + dprintf(fd, "\tDebug UART port: %s\n", description); - return 0; + return 0; } static const struct bridgectl_ops ast2500_debugctl_ops = { - .name = debugctl_name, - .enforce = debugctl_enforce, - .status = debugctl_status, - .report = ast2500_debugctl_report, + .name = debugctl_name, + .enforce = debugctl_enforce, + .status = debugctl_status, + .report = ast2500_debugctl_report, }; -static int ast2600_debugctl_report(struct bridgectl *bridge, int fd, enum bridge_mode *mode) +static int ast2600_debugctl_report(struct bridgectl *bridge, int fd, + enum bridge_mode *mode) { - struct debugctl *ctx = to_debugctl(bridge); - int rc; - - if ((rc = debugctl_status(bridge, mode)) < 0) { - loge("Failed to read debug UART bridge status: %d\n", rc); - return rc; - } - - bridgectl_log_status(bridge, fd, *mode); - - switch (ctx->region.start) { - case 0x1e783000: - dprintf(fd, "\tDebug UART port: UART1\n"); - break; - case 0x1e784000: - dprintf(fd, "\tDebug UART port: UART5\n"); - break; - default: - return -EINVAL; - } - - return 0; + struct debugctl *ctx = to_debugctl(bridge); + int rc; + + if ((rc = debugctl_status(bridge, mode)) < 0) { + loge("Failed to read debug UART bridge status: %d\n", rc); + return rc; + } + + bridgectl_log_status(bridge, fd, *mode); + + switch (ctx->region.start) { + case 0x1e783000: + dprintf(fd, "\tDebug UART port: UART1\n"); + break; + case 0x1e784000: + dprintf(fd, "\tDebug UART port: UART5\n"); + break; + default: + return -EINVAL; + } + + return 0; } static const struct bridgectl_ops ast2600_debugctl_ops = { - .name = debugctl_name, - .enforce = debugctl_enforce, - .status = debugctl_status, - .report = ast2600_debugctl_report, + .name = debugctl_name, + .enforce = debugctl_enforce, + .status = debugctl_status, + .report = ast2600_debugctl_report, }; static const struct soc_device_id debugctl_matches[] = { - { .compatible = "aspeed,ast2500-debug-ahb-bridge", .data = &ast2500_debugctl_ops }, - { .compatible = "aspeed,ast2600-uart", .data = &ast2600_debugctl_ops }, - { }, + { .compatible = "aspeed,ast2500-debug-ahb-bridge", + .data = &ast2500_debugctl_ops }, + { .compatible = "aspeed,ast2600-uart", .data = &ast2600_debugctl_ops }, + {}, }; static int debugctl_driver_init(struct soc *soc, struct soc_device *dev) { - const struct bridgectl_ops *ops; - struct debugctl *ctx; - int rc; + const struct bridgectl_ops *ops; + struct debugctl *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->region)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->region)) < 0) { + goto cleanup_ctx; + } - ctx->soc = soc; + ctx->soc = soc; - if ((rc = bridges_device_get_gate(soc, dev, &ctx->bridges, &ctx->id)) < 0) { - goto cleanup_ctx; - } + if ((rc = bridges_device_get_gate(soc, dev, &ctx->bridges, &ctx->id)) < + 0) { + goto cleanup_ctx; + } - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - ops = soc_device_get_match_data(soc, debugctl_matches, &dev->node); - if (!ops) { - logd("Failed to find device match data\n"); - rc = -EINVAL; - goto cleanup_drvdata; - } + ops = soc_device_get_match_data(soc, debugctl_matches, &dev->node); + if (!ops) { + logd("Failed to find device match data\n"); + rc = -EINVAL; + goto cleanup_drvdata; + } - if ((rc = soc_bridge_controller_register(soc, debugctl_as_bridgectl(ctx), ops)) < 0) { - goto cleanup_drvdata; - } + if ((rc = soc_bridge_controller_register( + soc, debugctl_as_bridgectl(ctx), ops)) < 0) { + goto cleanup_drvdata; + } - return 0; + return 0; cleanup_drvdata: - soc_device_set_drvdata(dev, NULL); + soc_device_set_drvdata(dev, NULL); cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void debugctl_driver_destroy(struct soc_device *dev) { - struct debugctl *ctx = soc_device_get_drvdata(dev); + struct debugctl *ctx = soc_device_get_drvdata(dev); - soc_bridge_controller_unregister(ctx->soc, debugctl_as_bridgectl(ctx)); + soc_bridge_controller_unregister(ctx->soc, debugctl_as_bridgectl(ctx)); - free(ctx); + free(ctx); } static const struct soc_driver debugctl_driver = { - .name = "debugctl", - .matches = debugctl_matches, - .init = debugctl_driver_init, - .destroy = debugctl_driver_destroy, + .name = "debugctl", + .matches = debugctl_matches, + .init = debugctl_driver_init, + .destroy = debugctl_driver_destroy, }; REGISTER_SOC_DRIVER(debugctl_driver); diff --git a/src/soc/ilpcctl.c b/src/soc/ilpcctl.c index 1b049e2..ea0daff 100644 --- a/src/soc/ilpcctl.c +++ b/src/soc/ilpcctl.c @@ -14,304 +14,325 @@ #include #include -#define LPC_HICRB 0x100 -#define LPC_HICRB_ILPC_DIS (1 << 29) -#define LPC_HICRB_ILPC_RO (1 << 6) +#define LPC_HICRB 0x100 +#define LPC_HICRB_ILPC_DIS (1 << 29) +#define LPC_HICRB_ILPC_RO (1 << 6) struct ilpcctl { - struct bridgectl ctl; - struct soc *soc; - struct soc_region lpc; - struct sioctl *sioctl; - struct bridges *bridges; - int gate; + struct bridgectl ctl; + struct soc *soc; + struct soc_region lpc; + struct sioctl *sioctl; + struct bridges *bridges; + int gate; }; #define to_ilpcctl(b) container_of((b), struct ilpcctl, ctl) struct bridgectl *ilpcctl_as_bridgectl(struct ilpcctl *ctx) { - return &ctx->ctl; + return &ctx->ctl; } static const char *ilpcctl_name(struct bridgectl *bridge __unused) { - return "ilpc"; + return "ilpc"; } -static int ast2400_ilpcctl_enforce(struct bridgectl *bridge, enum bridge_mode mode) +static int ast2400_ilpcctl_enforce(struct bridgectl *bridge, + enum bridge_mode mode) { - struct ilpcctl *ctx = to_ilpcctl(bridge); - uint32_t hicrb; - int rc; - - if (mode == bm_disabled) { - if ((rc = sioctl_decode_configure(ctx->sioctl, sioctl_decode_disable)) < 0) { - loge("Failed to disable SuperIO decoding: %d\n"); - } - - return rc; - } - - if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < 0) { - loge("Failed to read LPC HICRB: %d\n", rc); - return rc; - } - - if (mode == bm_restricted) { - hicrb |= LPC_HICRB_ILPC_RO; - } else { - assert(mode == bm_permissive); - hicrb &= ~LPC_HICRB_ILPC_RO; - } - - if ((rc = soc_writel(ctx->soc, ctx->lpc.start + LPC_HICRB, hicrb)) < 0) { - loge("Failed to write LPC HICRB: %d\n", rc); - return rc; - } - - /* FIXME: Sort out a way to configure which IO address we use */ - if ((rc = sioctl_decode_configure(ctx->sioctl, sioctl_decode_2e)) < 0) { - loge("Failed to enable SuperIO decoding on 0x2e: %d\n", rc); - } - - return rc; + struct ilpcctl *ctx = to_ilpcctl(bridge); + uint32_t hicrb; + int rc; + + if (mode == bm_disabled) { + if ((rc = sioctl_decode_configure(ctx->sioctl, + sioctl_decode_disable)) < 0) { + loge("Failed to disable SuperIO decoding: %d\n"); + } + + return rc; + } + + if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < + 0) { + loge("Failed to read LPC HICRB: %d\n", rc); + return rc; + } + + if (mode == bm_restricted) { + hicrb |= LPC_HICRB_ILPC_RO; + } else { + assert(mode == bm_permissive); + hicrb &= ~LPC_HICRB_ILPC_RO; + } + + if ((rc = soc_writel(ctx->soc, ctx->lpc.start + LPC_HICRB, hicrb)) < + 0) { + loge("Failed to write LPC HICRB: %d\n", rc); + return rc; + } + + /* FIXME: Sort out a way to configure which IO address we use */ + if ((rc = sioctl_decode_configure(ctx->sioctl, sioctl_decode_2e)) < 0) { + loge("Failed to enable SuperIO decoding on 0x2e: %d\n", rc); + } + + return rc; } -static int ast2400_ilpcctl_status(struct bridgectl *bridge, enum bridge_mode *mode) +static int ast2400_ilpcctl_status(struct bridgectl *bridge, + enum bridge_mode *mode) { - struct ilpcctl *ctx = to_ilpcctl(bridge); - enum sioctl_decode decode; - uint32_t hicrb; - int rc; - - if ((rc = sioctl_decode_status(ctx->sioctl, &decode)) < 0) { - loge("Failed to read SuperIO decode status: %d\n", rc); - return rc; - } - - if (decode == sioctl_decode_disable) { - *mode = bm_disabled; - return 0; - } - - if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < 0) { - loge("Failed to read LPC HICRB: %d\n", rc); - return rc; - } - - *mode = (hicrb & LPC_HICRB_ILPC_RO) ? bm_restricted : bm_permissive; - - return 0; + struct ilpcctl *ctx = to_ilpcctl(bridge); + enum sioctl_decode decode; + uint32_t hicrb; + int rc; + + if ((rc = sioctl_decode_status(ctx->sioctl, &decode)) < 0) { + loge("Failed to read SuperIO decode status: %d\n", rc); + return rc; + } + + if (decode == sioctl_decode_disable) { + *mode = bm_disabled; + return 0; + } + + if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < + 0) { + loge("Failed to read LPC HICRB: %d\n", rc); + return rc; + } + + *mode = (hicrb & LPC_HICRB_ILPC_RO) ? bm_restricted : bm_permissive; + + return 0; } -static int ilpcctl_report(struct bridgectl *bridge, int fd, enum bridge_mode *mode) +static int ilpcctl_report(struct bridgectl *bridge, int fd, + enum bridge_mode *mode) { - struct ilpcctl *ctx = to_ilpcctl(bridge); - enum sioctl_decode decode; - int address; - int rc; + struct ilpcctl *ctx = to_ilpcctl(bridge); + enum sioctl_decode decode; + int address; + int rc; - if ((rc = bridgectl_status(bridge, mode)) < 0) { - loge("Failed to read iLPC bridge status: %d\n", rc); - return rc; - } + if ((rc = bridgectl_status(bridge, mode)) < 0) { + loge("Failed to read iLPC bridge status: %d\n", rc); + return rc; + } - bridgectl_log_status(bridge, fd, *mode); + bridgectl_log_status(bridge, fd, *mode); - if (*mode == bm_disabled) { - return 0; - } + if (*mode == bm_disabled) { + return 0; + } - if ((rc = sioctl_decode_status(ctx->sioctl, &decode)) < 0) { - loge("Failed to get SuperIO decode status: %d\n", rc); - return rc; - } + if ((rc = sioctl_decode_status(ctx->sioctl, &decode)) < 0) { + loge("Failed to get SuperIO decode status: %d\n", rc); + return rc; + } - address = (decode == sioctl_decode_2e) ? 0x2e : 0x4e; - dprintf(fd, "\tSuperIO address: 0x%02x\n", address); + address = (decode == sioctl_decode_2e) ? 0x2e : 0x4e; + dprintf(fd, "\tSuperIO address: 0x%02x\n", address); - return 0; + return 0; } static const struct bridgectl_ops ast2400_ilpcctl_ops = { - .name = ilpcctl_name, - .enforce = ast2400_ilpcctl_enforce, - .status = ast2400_ilpcctl_status, - .report = ilpcctl_report, + .name = ilpcctl_name, + .enforce = ast2400_ilpcctl_enforce, + .status = ast2400_ilpcctl_status, + .report = ilpcctl_report, }; -static int ast2600_ilpcctl_enforce(struct bridgectl *bridge, enum bridge_mode mode) +static int ast2600_ilpcctl_enforce(struct bridgectl *bridge, + enum bridge_mode mode) { - struct ilpcctl *ctx = to_ilpcctl(bridge); - uint32_t hicrb; - int rc; - - if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < 0) { - loge("Failed to read HICRB: %d\n", rc); - return rc; - } - - switch (mode) { - case bm_permissive: - hicrb &= ~(LPC_HICRB_ILPC_DIS | LPC_HICRB_ILPC_RO); - break; - case bm_restricted: - hicrb &= ~LPC_HICRB_ILPC_DIS; - hicrb |= LPC_HICRB_ILPC_RO; - break; - case bm_disabled: - hicrb |= LPC_HICRB_ILPC_DIS; - break; - default: - loge("Unrecognised value for mode: %d\n", mode); - return -EINVAL; - } - - if ((rc = soc_writel(ctx->soc, ctx->lpc.start + LPC_HICRB, hicrb)) < 0) { - loge("Failed to write HICRB: %d\n", rc); - return rc; - } - - if (mode == bm_permissive || mode == bm_restricted) { - if ((rc = bridges_enable(ctx->bridges, ctx->gate)) < 0) { - loge("Failed to ungate iLPC bridge: %d\n", rc); - return rc; - } - - /* FIXME: Sort out a way to configure which IO address we use */ - if ((rc = sioctl_decode_configure(ctx->sioctl, sioctl_decode_2e)) < 0) { - loge("Failed to enable SuperIO decoding on 0x2e: %d\n", rc); - return rc; - } - } - - return 0; + struct ilpcctl *ctx = to_ilpcctl(bridge); + uint32_t hicrb; + int rc; + + if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < + 0) { + loge("Failed to read HICRB: %d\n", rc); + return rc; + } + + switch (mode) { + case bm_permissive: + hicrb &= ~(LPC_HICRB_ILPC_DIS | LPC_HICRB_ILPC_RO); + break; + case bm_restricted: + hicrb &= ~LPC_HICRB_ILPC_DIS; + hicrb |= LPC_HICRB_ILPC_RO; + break; + case bm_disabled: + hicrb |= LPC_HICRB_ILPC_DIS; + break; + default: + loge("Unrecognised value for mode: %d\n", mode); + return -EINVAL; + } + + if ((rc = soc_writel(ctx->soc, ctx->lpc.start + LPC_HICRB, hicrb)) < + 0) { + loge("Failed to write HICRB: %d\n", rc); + return rc; + } + + if (mode == bm_permissive || mode == bm_restricted) { + if ((rc = bridges_enable(ctx->bridges, ctx->gate)) < 0) { + loge("Failed to ungate iLPC bridge: %d\n", rc); + return rc; + } + + /* FIXME: Sort out a way to configure which IO address we use */ + if ((rc = sioctl_decode_configure(ctx->sioctl, + sioctl_decode_2e)) < 0) { + loge("Failed to enable SuperIO decoding on 0x2e: %d\n", + rc); + return rc; + } + } + + return 0; } -static int ast2600_ilpcctl_status(struct bridgectl *bridge, enum bridge_mode *mode) +static int ast2600_ilpcctl_status(struct bridgectl *bridge, + enum bridge_mode *mode) { - struct ilpcctl *ctx = to_ilpcctl(bridge); - enum sioctl_decode decode; - uint32_t hicrb; - int rc; - - if ((rc = sioctl_decode_status(ctx->sioctl, &decode)) < 0) { - loge("Failed to read SuperIO decode status: %d\n", rc); - return rc; - } else if (decode == sioctl_decode_disable) { - *mode = bm_disabled; - return 0; - } - - if ((rc = bridges_status(ctx->bridges, ctx->gate)) < 0) { - loge("Failed to query bridge status for gate %d: %d\n", ctx->gate, rc); - return rc; - } else if (!rc) { - *mode = bm_disabled; - return 0; - } - - if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < 0) { - loge("Failed to read HICRB: %d\n", rc); - return rc; - } - - if (hicrb & LPC_HICRB_ILPC_DIS) { - *mode = bm_disabled; - return 0; - } - - if (hicrb & LPC_HICRB_ILPC_RO) { - *mode = bm_restricted; - return 0; - } - - *mode = bm_permissive; - return 0; + struct ilpcctl *ctx = to_ilpcctl(bridge); + enum sioctl_decode decode; + uint32_t hicrb; + int rc; + + if ((rc = sioctl_decode_status(ctx->sioctl, &decode)) < 0) { + loge("Failed to read SuperIO decode status: %d\n", rc); + return rc; + } else if (decode == sioctl_decode_disable) { + *mode = bm_disabled; + return 0; + } + + if ((rc = bridges_status(ctx->bridges, ctx->gate)) < 0) { + loge("Failed to query bridge status for gate %d: %d\n", + ctx->gate, rc); + return rc; + } else if (!rc) { + *mode = bm_disabled; + return 0; + } + + if ((rc = soc_readl(ctx->soc, ctx->lpc.start + LPC_HICRB, &hicrb)) < + 0) { + loge("Failed to read HICRB: %d\n", rc); + return rc; + } + + if (hicrb & LPC_HICRB_ILPC_DIS) { + *mode = bm_disabled; + return 0; + } + + if (hicrb & LPC_HICRB_ILPC_RO) { + *mode = bm_restricted; + return 0; + } + + *mode = bm_permissive; + return 0; } static const struct bridgectl_ops ast2600_ilpcctl_ops = { - .name = ilpcctl_name, - .enforce = ast2600_ilpcctl_enforce, - .status = ast2600_ilpcctl_status, - .report = ilpcctl_report, + .name = ilpcctl_name, + .enforce = ast2600_ilpcctl_enforce, + .status = ast2600_ilpcctl_status, + .report = ilpcctl_report, }; static const struct soc_device_id ilpcctl_matches[] = { - { .compatible = "aspeed,ast2400-ilpc-ahb-bridge", .data = &ast2400_ilpcctl_ops }, - { .compatible = "aspeed,ast2500-ilpc-ahb-bridge", .data = &ast2400_ilpcctl_ops }, - { .compatible = "aspeed,ast2600-ilpc-ahb-bridge", .data = &ast2600_ilpcctl_ops }, - { }, + { .compatible = "aspeed,ast2400-ilpc-ahb-bridge", + .data = &ast2400_ilpcctl_ops }, + { .compatible = "aspeed,ast2500-ilpc-ahb-bridge", + .data = &ast2400_ilpcctl_ops }, + { .compatible = "aspeed,ast2600-ilpc-ahb-bridge", + .data = &ast2600_ilpcctl_ops }, + {}, }; static int ilpcctl_driver_init(struct soc *soc, struct soc_device *dev) { - const struct bridgectl_ops *ops; - struct ilpcctl *ctx; - int rc; + const struct bridgectl_ops *ops; + struct ilpcctl *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->lpc)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->lpc)) < 0) { + goto cleanup_ctx; + } - ops = soc_device_get_match_data(soc, ilpcctl_matches, &dev->node); - if (!ops) { - loge("Failed to find ilpcctl ops\n"); - rc = -EINVAL; - goto cleanup_ctx; - } + ops = soc_device_get_match_data(soc, ilpcctl_matches, &dev->node); + if (!ops) { + loge("Failed to find ilpcctl ops\n"); + rc = -EINVAL; + goto cleanup_ctx; + } - if (!(ctx->sioctl = sioctl_get(soc))) { - loge("Failed to acquire SuperIO controller\n"); - goto cleanup_ctx; - } + if (!(ctx->sioctl = sioctl_get(soc))) { + loge("Failed to acquire SuperIO controller\n"); + goto cleanup_ctx; + } - if (ops == &ast2600_ilpcctl_ops) { - if ((rc = bridges_device_get_gate(soc, dev, &ctx->bridges, &ctx->gate)) < 0) { - loge("Failed to fetch bridge gate for iLPC bridge: %d\n", rc); - goto cleanup_ctx; - } + if (ops == &ast2600_ilpcctl_ops) { + if ((rc = bridges_device_get_gate(soc, dev, &ctx->bridges, + &ctx->gate)) < 0) { + loge("Failed to fetch bridge gate for iLPC bridge: %d\n", + rc); + goto cleanup_ctx; + } - logd("iLPC bridge gate ID: %d\n", ctx->gate); - } + logd("iLPC bridge gate ID: %d\n", ctx->gate); + } - ctx->soc = soc; + ctx->soc = soc; - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - if ((rc = soc_bridge_controller_register(soc, ilpcctl_as_bridgectl(ctx), ops)) < 0) { - goto cleanup_drvdata; - } + if ((rc = soc_bridge_controller_register(soc, ilpcctl_as_bridgectl(ctx), + ops)) < 0) { + goto cleanup_drvdata; + } - return 0; + return 0; cleanup_drvdata: - soc_device_set_drvdata(dev, NULL); + soc_device_set_drvdata(dev, NULL); cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void ilpcctl_driver_destroy(struct soc_device *dev) { - struct ilpcctl *ctx = soc_device_get_drvdata(dev); + struct ilpcctl *ctx = soc_device_get_drvdata(dev); - soc_bridge_controller_unregister(ctx->soc, ilpcctl_as_bridgectl(ctx)); + soc_bridge_controller_unregister(ctx->soc, ilpcctl_as_bridgectl(ctx)); - free(ctx); + free(ctx); } static const struct soc_driver ilpcctl_driver = { - .name = "ilpcctl", - .matches = ilpcctl_matches, - .init = ilpcctl_driver_init, - .destroy = ilpcctl_driver_destroy, + .name = "ilpcctl", + .matches = ilpcctl_matches, + .init = ilpcctl_driver_init, + .destroy = ilpcctl_driver_destroy, }; REGISTER_SOC_DRIVER(ilpcctl_driver); diff --git a/src/soc/ilpcctl.h b/src/soc/ilpcctl.h index 1b544c1..2a8e479 100644 --- a/src/soc/ilpcctl.h +++ b/src/soc/ilpcctl.h @@ -5,5 +5,5 @@ struct ilpcctl; -struct ilpcctl *ilpcctl_get(struct soc* soc); +struct ilpcctl *ilpcctl_get(struct soc *soc); struct bridgectl *ilpcctl_as_bridgectl(struct ilpcctl *ctx); diff --git a/src/soc/jtag.c b/src/soc/jtag.c index a23f7a3..d6179f9 100644 --- a/src/soc/jtag.c +++ b/src/soc/jtag.c @@ -8,245 +8,245 @@ #include "soc.h" /* ASPEED Register definitions */ -#define AST_JTAG_DATA 0x00 -#define AST_JTAG_INST 0x04 -#define AST_JTAG_EC 0x08 -#define AST_JTAG_ISR 0x0C -#define AST_JTAG_SW_MODE 0x10 -#define AST_JTAG_TCK 0x14 -#define AST_JTAG_EC1 0x18 +#define AST_JTAG_DATA 0x00 +#define AST_JTAG_INST 0x04 +#define AST_JTAG_EC 0x08 +#define AST_JTAG_ISR 0x0C +#define AST_JTAG_SW_MODE 0x10 +#define AST_JTAG_TCK 0x14 +#define AST_JTAG_EC1 0x18 /* AST_JTAG_EC/JTAG08: Engine control */ -#define AST_JTAG_EC_ENG_EN BIT(31) -#define AST_JTAG_EC_ENG_OUT_EN BIT(30) -#define AST_JTAG_EC_FORCE_TMS BIT(29) +#define AST_JTAG_EC_ENG_EN BIT(31) +#define AST_JTAG_EC_ENG_OUT_EN BIT(30) +#define AST_JTAG_EC_FORCE_TMS BIT(29) /* AST_JTAG_SW_MODE/JTAG10: Software mode and status */ -#define AST_JTAG_SW_MODE_EN BIT(19) -#define AST_JTAG_SW_MODE_TCK BIT(18) -#define AST_JTAG_SW_MODE_TMS BIT(17) -#define AST_JTAG_SW_MODE_TDIO BIT(16) +#define AST_JTAG_SW_MODE_EN BIT(19) +#define AST_JTAG_SW_MODE_TCK BIT(18) +#define AST_JTAG_SW_MODE_TMS BIT(17) +#define AST_JTAG_SW_MODE_TDIO BIT(16) -#define AST2400_SCU_RESET_CTRL 0x04 -#define AST2600_SCU_RESET_CTRL 0x40 -#define SCU_RESET_CTRL_JTAG_MASTER BIT(22) +#define AST2400_SCU_RESET_CTRL 0x04 +#define AST2600_SCU_RESET_CTRL 0x40 +#define SCU_RESET_CTRL_JTAG_MASTER BIT(22) -#define AST2400_SCU_MISC_CTRL 0x2c -#define AST2600_SCU_MISC_CTRL 0xc0 -#define SCU_MISC_CTRL_JTAG_MASK (BIT(15) | BIT(14)) +#define AST2400_SCU_MISC_CTRL 0x2c +#define AST2600_SCU_MISC_CTRL 0xc0 +#define SCU_MISC_CTRL_JTAG_MASK (BIT(15) | BIT(14)) struct jtag_ops { - int (*release)(struct jtag *ctx); - int (*route)(struct jtag *ctx, uint32_t route); + int (*release)(struct jtag *ctx); + int (*route)(struct jtag *ctx, uint32_t route); }; struct jtag { - const struct jtag_ops *ops; - struct soc_region regs; - struct scu *scu; - struct soc *soc; - int refcnt; + const struct jtag_ops *ops; + struct soc_region regs; + struct scu *scu; + struct soc *soc; + int refcnt; }; static int jtag_readl(struct jtag *ctx, uint32_t reg, uint32_t *value) { - return soc_readl(ctx->soc, ctx->regs.start + reg, value); + return soc_readl(ctx->soc, ctx->regs.start + reg, value); } static int jtag_writel(struct jtag *ctx, uint32_t reg, uint32_t value) { - return soc_writel(ctx->soc, ctx->regs.start + reg, value); + return soc_writel(ctx->soc, ctx->regs.start + reg, value); } int jtag_route(struct jtag *ctx, uint32_t route) { - if (route & ~SCU_MISC_CTRL_JTAG_MASK) { - return -EINVAL; - } + if (route & ~SCU_MISC_CTRL_JTAG_MASK) { + return -EINVAL; + } - return ctx->ops->route(ctx, route); + return ctx->ops->route(ctx, route); } int jtag_bitbang_set(struct jtag *ctx, uint8_t tck, uint8_t tms, uint8_t tdi) { - return jtag_writel(ctx, AST_JTAG_SW_MODE, - AST_JTAG_SW_MODE_EN | - ((!!tck) * AST_JTAG_SW_MODE_TCK) | - ((!!tms) * AST_JTAG_SW_MODE_TMS) | - ((!!tdi) * AST_JTAG_SW_MODE_TDIO)); + return jtag_writel(ctx, AST_JTAG_SW_MODE, + AST_JTAG_SW_MODE_EN | + ((!!tck) * AST_JTAG_SW_MODE_TCK) | + ((!!tms) * AST_JTAG_SW_MODE_TMS) | + ((!!tdi) * AST_JTAG_SW_MODE_TDIO)); } -int jtag_bitbang_get(struct jtag *ctx, uint8_t* tdo) +int jtag_bitbang_get(struct jtag *ctx, uint8_t *tdo) { - uint32_t reg = 0; - int rc = jtag_readl(ctx, AST_JTAG_SW_MODE, ®); + uint32_t reg = 0; + int rc = jtag_readl(ctx, AST_JTAG_SW_MODE, ®); - *tdo = !!(reg & AST_JTAG_SW_MODE_TDIO); + *tdo = !!(reg & AST_JTAG_SW_MODE_TDIO); - return rc; + return rc; } static int ast2400_jtag_release(struct jtag *ctx) { - uint32_t reg; - int rc; + uint32_t reg; + int rc; - if ((rc = scu_readl(ctx->scu, AST2400_SCU_RESET_CTRL, ®)) < 0) - return rc; + if ((rc = scu_readl(ctx->scu, AST2400_SCU_RESET_CTRL, ®)) < 0) + return rc; - reg &= ~SCU_RESET_CTRL_JTAG_MASTER; + reg &= ~SCU_RESET_CTRL_JTAG_MASTER; - if ((rc = scu_writel(ctx->scu, AST2400_SCU_RESET_CTRL, reg)) < 0) - return rc; + if ((rc = scu_writel(ctx->scu, AST2400_SCU_RESET_CTRL, reg)) < 0) + return rc; - return 0; + return 0; } static int ast2400_jtag_route(struct jtag *ctx, uint32_t route) { - uint32_t reg; - int rc; + uint32_t reg; + int rc; - if ((rc = scu_readl(ctx->scu, AST2400_SCU_MISC_CTRL, ®)) < 0) - return rc; + if ((rc = scu_readl(ctx->scu, AST2400_SCU_MISC_CTRL, ®)) < 0) + return rc; - reg &= ~(uint32_t)SCU_MISC_CTRL_JTAG_MASK; - reg |= route; + reg &= ~(uint32_t)SCU_MISC_CTRL_JTAG_MASK; + reg |= route; - if ((rc = scu_writel(ctx->scu, AST2400_SCU_MISC_CTRL, reg)) < 0) - return rc; + if ((rc = scu_writel(ctx->scu, AST2400_SCU_MISC_CTRL, reg)) < 0) + return rc; - return 0; + return 0; } -static const struct jtag_ops ast2400_jtag_ops = { - .release = ast2400_jtag_release, - .route = ast2400_jtag_route -}; +static const struct jtag_ops ast2400_jtag_ops = { .release = + ast2400_jtag_release, + .route = ast2400_jtag_route }; static int ast2600_jtag_release(struct jtag *ctx) { - return scu_writel(ctx->scu, AST2600_SCU_RESET_CTRL + 4, - SCU_RESET_CTRL_JTAG_MASTER); + return scu_writel(ctx->scu, AST2600_SCU_RESET_CTRL + 4, + SCU_RESET_CTRL_JTAG_MASTER); } static int ast2600_jtag_route(struct jtag *ctx, uint32_t route) { - uint32_t reg; - int rc; + uint32_t reg; + int rc; - if ((rc = scu_readl(ctx->scu, AST2600_SCU_MISC_CTRL, ®)) < 0) - return rc; + if ((rc = scu_readl(ctx->scu, AST2600_SCU_MISC_CTRL, ®)) < 0) + return rc; - reg &= ~(uint32_t)SCU_MISC_CTRL_JTAG_MASK; - reg |= route; + reg &= ~(uint32_t)SCU_MISC_CTRL_JTAG_MASK; + reg |= route; - if ((rc = scu_writel(ctx->scu, AST2600_SCU_MISC_CTRL, reg)) < 0) - return rc; + if ((rc = scu_writel(ctx->scu, AST2600_SCU_MISC_CTRL, reg)) < 0) + return rc; - return 0; + return 0; } static const struct jtag_ops ast2600_jtag_ops = { - .release = ast2600_jtag_release, - .route = ast2600_jtag_route, + .release = ast2600_jtag_release, + .route = ast2600_jtag_route, }; static const struct soc_device_id jtag_match[] = { - { .compatible = "aspeed,ast2400-jtag", .data = &ast2400_jtag_ops }, - { .compatible = "aspeed,ast2500-jtag", .data = &ast2400_jtag_ops }, - { .compatible = "aspeed,ast2600-jtag", .data = &ast2600_jtag_ops }, - { }, + { .compatible = "aspeed,ast2400-jtag", .data = &ast2400_jtag_ops }, + { .compatible = "aspeed,ast2500-jtag", .data = &ast2400_jtag_ops }, + { .compatible = "aspeed,ast2600-jtag", .data = &ast2600_jtag_ops }, + {}, }; static int jtag_driver_init(struct soc *soc, struct soc_device *dev) { - struct jtag *ctx; - int rc; - - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } - - ctx->ops = soc_device_get_match_data(soc, jtag_match, &dev->node); - if (!ctx->ops || !ctx->ops->release || !ctx->ops->route) { - rc = -EINVAL; - goto cleanup_ctx; - } - - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->regs)) < 0) { - goto cleanup_ctx; - } - ctx->refcnt = 1; - ctx->soc = soc; - - ctx->scu = scu_get(soc); - if (!ctx->scu) { - rc = -ENODEV; - goto cleanup_ctx; - } - - // take JTAG master out of reset - if ((rc = ctx->ops->release(ctx)) < 0) { - goto cleanup_ctx; - } - - // enable JTAG master controller - if ((rc = jtag_writel(ctx, AST_JTAG_EC, - AST_JTAG_EC_ENG_EN | - AST_JTAG_EC_ENG_OUT_EN)) < 0) { - goto cleanup_ctx; - } - - // reset JTAG master controller (peripheral clears bit itself) - if ((rc = jtag_writel(ctx, AST_JTAG_EC, - AST_JTAG_EC_ENG_EN | - AST_JTAG_EC_ENG_OUT_EN | - AST_JTAG_EC_FORCE_TMS)) < 0) { - goto cleanup_ctx; - } - - // enable software JTAG mode/bitbang - if ((rc = jtag_writel(ctx, AST_JTAG_SW_MODE, AST_JTAG_SW_MODE_EN)) < 0) { - goto cleanup_ctx; - } - - soc_device_set_drvdata(dev, ctx); - - return 0; + struct jtag *ctx; + int rc; + + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } + + ctx->ops = soc_device_get_match_data(soc, jtag_match, &dev->node); + if (!ctx->ops || !ctx->ops->release || !ctx->ops->route) { + rc = -EINVAL; + goto cleanup_ctx; + } + + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->regs)) < 0) { + goto cleanup_ctx; + } + ctx->refcnt = 1; + ctx->soc = soc; + + ctx->scu = scu_get(soc); + if (!ctx->scu) { + rc = -ENODEV; + goto cleanup_ctx; + } + + // take JTAG master out of reset + if ((rc = ctx->ops->release(ctx)) < 0) { + goto cleanup_ctx; + } + + // enable JTAG master controller + if ((rc = jtag_writel(ctx, AST_JTAG_EC, + AST_JTAG_EC_ENG_EN | AST_JTAG_EC_ENG_OUT_EN)) < + 0) { + goto cleanup_ctx; + } + + // reset JTAG master controller (peripheral clears bit itself) + if ((rc = jtag_writel(ctx, AST_JTAG_EC, + AST_JTAG_EC_ENG_EN | AST_JTAG_EC_ENG_OUT_EN | + AST_JTAG_EC_FORCE_TMS)) < 0) { + goto cleanup_ctx; + } + + // enable software JTAG mode/bitbang + if ((rc = jtag_writel(ctx, AST_JTAG_SW_MODE, AST_JTAG_SW_MODE_EN)) < + 0) { + goto cleanup_ctx; + } + + soc_device_set_drvdata(dev, ctx); + + return 0; cleanup_ctx: - free(ctx); - return rc; + free(ctx); + return rc; } static void jtag_driver_destroy(struct soc_device *dev) { - jtag_put(soc_device_get_drvdata(dev)); + jtag_put(soc_device_get_drvdata(dev)); } static const struct soc_driver jtag_driver = { - .name = "jtag", - .matches = jtag_match, - .init = jtag_driver_init, - .destroy = jtag_driver_destroy, + .name = "jtag", + .matches = jtag_match, + .init = jtag_driver_init, + .destroy = jtag_driver_destroy, }; REGISTER_SOC_DRIVER(jtag_driver); struct jtag *jtag_get(struct soc *soc, const char *name) { - struct jtag *ctx = soc_driver_get_drvdata_by_name(soc, &jtag_driver, name); - if (ctx) { - ctx->refcnt += 1; - } - return ctx; + struct jtag *ctx = + soc_driver_get_drvdata_by_name(soc, &jtag_driver, name); + if (ctx) { + ctx->refcnt += 1; + } + return ctx; } void jtag_put(struct jtag *ctx) { - ctx->refcnt -= 1; - if (!ctx->refcnt) { - free(ctx); - } + ctx->refcnt -= 1; + if (!ctx->refcnt) { + free(ctx); + } } diff --git a/src/soc/jtag.h b/src/soc/jtag.h index 5e4c976..876e595 100644 --- a/src/soc/jtag.h +++ b/src/soc/jtag.h @@ -6,17 +6,17 @@ #include "soc.h" #include "bits.h" -#define SCU_JTAG_NORMAL 0 -#define SCU_JTAG_IO_TO_PCIE BIT(14) -#define SCU_JTAG_MASTER_TO_PCIE BIT(15) -#define SCU_JTAG_MASTER_TO_ARM BIT(15) | BIT(14) +#define SCU_JTAG_NORMAL 0 +#define SCU_JTAG_IO_TO_PCIE BIT(14) +#define SCU_JTAG_MASTER_TO_PCIE BIT(15) +#define SCU_JTAG_MASTER_TO_ARM BIT(15) | BIT(14) struct jtag; struct jtag *jtag_get(struct soc *soc, const char *name); void jtag_put(struct jtag *ctx); int jtag_bitbang_set(struct jtag *ctx, uint8_t tck, uint8_t tms, uint8_t tdi); -int jtag_bitbang_get(struct jtag *ctx, uint8_t* tdo); +int jtag_bitbang_get(struct jtag *ctx, uint8_t *tdo); int jtag_route(struct jtag *ctx, uint32_t route); #endif diff --git a/src/soc/meson.build b/src/soc/meson.build index 1599cab..2f8c2e7 100644 --- a/src/soc/meson.build +++ b/src/soc/meson.build @@ -1,19 +1,19 @@ src += files( - 'bridgectl.c', - 'bridges.c', - 'clk.c', - 'debugctl.c', - 'ilpcctl.c', - 'jtag.c', - 'otp.c', - 'pciectl.c', - 'scu.c', - 'sdmc.c', - 'sfc.c', - 'sioctl.c', - 'strap.c', - 'trace.c', - 'uart/mux.c', - 'uart/vuart.c', - 'wdt.c' + 'bridgectl.c', + 'bridges.c', + 'clk.c', + 'debugctl.c', + 'ilpcctl.c', + 'jtag.c', + 'otp.c', + 'pciectl.c', + 'scu.c', + 'sdmc.c', + 'sfc.c', + 'sioctl.c', + 'strap.c', + 'trace.c', + 'uart/mux.c', + 'uart/vuart.c', + 'wdt.c', ) diff --git a/src/soc/otp.c b/src/soc/otp.c index 09528c4..dc6c2e4 100644 --- a/src/soc/otp.c +++ b/src/soc/otp.c @@ -14,513 +14,519 @@ #include #include -#define OTP_PASSWD 0x349fe38a -#define OTP_TRIGGER_PROGRAM 0x23b1e364 -#define OTP_TRIGGER_READ 0x23b1e361 -#define OTP_TRIGGER_WRITE_REG 0x23b1e362 - -#define OTP_PROTECT_KEY 0x00 -#define OTP_COMMAND 0x04 -#define OTP_TIMING 0x08 -#define OTP_ADDR 0x10 -#define OTP_STATUS 0x14 -#define OTP_STATUS_IDLE 0x6 -#define OTP_COMPARE_1 0x20 -#define OTP_COMPARE_2 0x24 -#define OTP_COMPARE_3 0x28 -#define OTP_COMPARE_4 0x2c - -#define NUM_OTP_CONF 16 -#define NUM_PROG_TRIES 16 +#define OTP_PASSWD 0x349fe38a +#define OTP_TRIGGER_PROGRAM 0x23b1e364 +#define OTP_TRIGGER_READ 0x23b1e361 +#define OTP_TRIGGER_WRITE_REG 0x23b1e362 + +#define OTP_PROTECT_KEY 0x00 +#define OTP_COMMAND 0x04 +#define OTP_TIMING 0x08 +#define OTP_ADDR 0x10 +#define OTP_STATUS 0x14 +#define OTP_STATUS_IDLE 0x6 +#define OTP_COMPARE_1 0x20 +#define OTP_COMPARE_2 0x24 +#define OTP_COMPARE_3 0x28 +#define OTP_COMPARE_4 0x2c + +#define NUM_OTP_CONF 16 +#define NUM_PROG_TRIES 16 struct otp { - struct soc *soc; - struct soc_region iomem; - uint32_t timings[3]; - uint32_t soak_parameters[3][3]; + struct soc *soc; + struct soc_region iomem; + uint32_t timings[3]; + uint32_t soak_parameters[3][3]; }; struct otpstrap_status { - uint8_t value; - uint8_t option_array[7]; - uint8_t remain_times; - uint8_t writeable_option; - uint8_t protected; + uint8_t value; + uint8_t option_array[7]; + uint8_t remain_times; + uint8_t writeable_option; + uint8_t protected; }; static int otp_readl(struct otp *otp, uint32_t offset, uint32_t *val) { - int rc = soc_readl(otp->soc, otp->iomem.start + offset, val); + int rc = soc_readl(otp->soc, otp->iomem.start + offset, val); #ifdef _OTP_DEBUG - if (rc) - printf("otp rd %02x failed: %d\n", offset, rc); - else - printf("otp rd %02x:%08x\n", offset, *val); + if (rc) + printf("otp rd %02x failed: %d\n", offset, rc); + else + printf("otp rd %02x:%08x\n", offset, *val); #endif - return rc; + return rc; } static int otp_writel(struct otp *otp, uint32_t offset, uint32_t val) { - int rc = soc_writel(otp->soc, otp->iomem.start + offset, val); + int rc = soc_writel(otp->soc, otp->iomem.start + offset, val); #ifdef _OTP_DEBUG - if (rc) - printf("otp wr %02x failed: %d\n", offset, rc); - else - printf("otp wr %02x:%08x\n", offset, val); + if (rc) + printf("otp wr %02x failed: %d\n", offset, rc); + else + printf("otp wr %02x:%08x\n", offset, val); #endif - return rc; + return rc; } static void diff_timespec(const struct timespec *start, - const struct timespec *end, struct timespec *diff) + const struct timespec *end, struct timespec *diff) { - diff->tv_sec = end->tv_sec - start->tv_sec; - diff->tv_nsec = end->tv_nsec - start->tv_nsec; - if (diff->tv_nsec < 0) { - --diff->tv_sec; - diff->tv_nsec += 1000000000L; - } + diff->tv_sec = end->tv_sec - start->tv_sec; + diff->tv_nsec = end->tv_nsec - start->tv_nsec; + if (diff->tv_nsec < 0) { + --diff->tv_sec; + diff->tv_nsec += 1000000000L; + } } static int otp_wait_complete(struct otp *otp) { - int rc; - uint32_t reg = 0; - struct timespec diff, end, intv, start; + int rc; + uint32_t reg = 0; + struct timespec diff, end, intv, start; - intv.tv_sec = 0; - intv.tv_nsec = 1000000L; - clock_gettime(CLOCK_MONOTONIC, &start); + intv.tv_sec = 0; + intv.tv_nsec = 1000000L; + clock_gettime(CLOCK_MONOTONIC, &start); - do { - rc = otp_readl(otp, OTP_STATUS, ®); - if (rc) - return rc; + do { + rc = otp_readl(otp, OTP_STATUS, ®); + if (rc) + return rc; - if ((reg & OTP_STATUS_IDLE) == OTP_STATUS_IDLE) - return 0; + if ((reg & OTP_STATUS_IDLE) == OTP_STATUS_IDLE) + return 0; - rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &intv, NULL); - if (rc) - return rc; + rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &intv, NULL); + if (rc) + return rc; - clock_gettime(CLOCK_MONOTONIC, &end); - diff_timespec(&start, &end, &diff); - } while (!diff.tv_sec && diff.tv_nsec < 500000000L); + clock_gettime(CLOCK_MONOTONIC, &end); + diff_timespec(&start, &end, &diff); + } while (!diff.tv_sec && diff.tv_nsec < 500000000L); - return -ETIMEDOUT; + return -ETIMEDOUT; } static int otp_program(struct otp *otp, uint32_t addr, uint32_t val) { - int rc; + int rc; - if ((rc = otp_writel(otp, OTP_ADDR, addr)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_ADDR, addr)) < 0) + return rc; - if ((rc = otp_writel(otp, OTP_COMPARE_1, val)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_COMPARE_1, val)) < 0) + return rc; - if ((rc = otp_writel(otp, OTP_COMMAND, OTP_TRIGGER_PROGRAM)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_COMMAND, OTP_TRIGGER_PROGRAM)) < 0) + return rc; - return otp_wait_complete(otp); + return otp_wait_complete(otp); } static int otp_read_reg(struct otp *otp, uint32_t addr, uint32_t *val) { - int rc; + int rc; - if ((rc = otp_writel(otp, OTP_ADDR, addr)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_ADDR, addr)) < 0) + return rc; - if ((rc = otp_writel(otp, OTP_COMMAND, OTP_TRIGGER_READ)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_COMMAND, OTP_TRIGGER_READ)) < 0) + return rc; - if ((rc = otp_wait_complete(otp)) < 0) - return rc; + if ((rc = otp_wait_complete(otp)) < 0) + return rc; - if ((rc = otp_readl(otp, OTP_COMPARE_1, val)) < 0) - return rc; + if ((rc = otp_readl(otp, OTP_COMPARE_1, val)) < 0) + return rc; - return 0; + return 0; } static int otp_read_config(struct otp *otp, int offset, uint32_t *val) { - uint32_t config_offset = 0x800; + uint32_t config_offset = 0x800; - config_offset |= (offset / 8) * 0x200; - config_offset |= (offset % 8) * 2; + config_offset |= (offset / 8) * 0x200; + config_offset |= (offset % 8) * 2; - return otp_read_reg(otp, config_offset, val); + return otp_read_reg(otp, config_offset, val); } static int otp_write_reg(struct otp *otp, uint32_t addr, uint32_t val) { - int rc; + int rc; - if ((rc = otp_writel(otp, OTP_ADDR, addr)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_ADDR, addr)) < 0) + return rc; - if ((rc = otp_writel(otp, OTP_COMPARE_1, val)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_COMPARE_1, val)) < 0) + return rc; - if ((rc = otp_writel(otp, OTP_COMMAND, OTP_TRIGGER_WRITE_REG)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_COMMAND, OTP_TRIGGER_WRITE_REG)) < 0) + return rc; - return otp_wait_complete(otp); + return otp_wait_complete(otp); } static int otp_set_soak(struct otp *otp, unsigned int soak) { - int rc; + int rc; - if (soak > 2) - return -EINVAL; + if (soak > 2) + return -EINVAL; - if ((rc = otp_write_reg(otp, 0x3000, otp->soak_parameters[soak][0])) < 0) - return rc; + if ((rc = otp_write_reg(otp, 0x3000, otp->soak_parameters[soak][0])) < + 0) + return rc; - if ((rc = otp_write_reg(otp, 0x5000, otp->soak_parameters[soak][1])) < 0) - return rc; + if ((rc = otp_write_reg(otp, 0x5000, otp->soak_parameters[soak][1])) < + 0) + return rc; - if ((rc = otp_write_reg(otp, 0x1000, otp->soak_parameters[soak][2])) < 0) - return rc; + if ((rc = otp_write_reg(otp, 0x1000, otp->soak_parameters[soak][2])) < + 0) + return rc; - if ((rc = otp_writel(otp, OTP_TIMING, otp->timings[soak])) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_TIMING, otp->timings[soak])) < 0) + return rc; - return 0; + return 0; } static int otp_confirm() { - int rc; - char inp[8]; - - printf("Is this acceptable? If so, type YES: "); - rc = scanf("%7s", inp); - if (rc == EOF) { - loge("Invalid input\n"); - return -errno; - } - - if (strcmp(inp, "YES")) { - loge("Strap write unconfirmed\n"); - return -EINVAL; - } - - return 0; + int rc; + char inp[8]; + + printf("Is this acceptable? If so, type YES: "); + rc = scanf("%7s", inp); + if (rc == EOF) { + loge("Invalid input\n"); + return -errno; + } + + if (strcmp(inp, "YES")) { + loge("Strap write unconfirmed\n"); + return -EINVAL; + } + + return 0; } static int otp_write(struct otp *otp, uint32_t address, uint32_t bitmask) { - int rc; - int tries = 0; - uint32_t prog; - uint32_t readback; + int rc; + int tries = 0; + uint32_t prog; + uint32_t readback; - if ((rc = otp_set_soak(otp, 1)) < 0) - return rc; + if ((rc = otp_set_soak(otp, 1)) < 0) + return rc; - prog = ~bitmask; - if ((rc = otp_program(otp, address, prog)) < 0) - goto undo_soak; + prog = ~bitmask; + if ((rc = otp_program(otp, address, prog)) < 0) + goto undo_soak; - do { - if ((rc = otp_read_reg(otp, address, &readback)) < 0) - goto undo_soak; + do { + if ((rc = otp_read_reg(otp, address, &readback)) < 0) + goto undo_soak; - if (readback & bitmask) - break; + if (readback & bitmask) + break; - if ((rc = otp_set_soak(otp, (tries % 2) ? 1 : 2)) < 0) - goto undo_soak; + if ((rc = otp_set_soak(otp, (tries % 2) ? 1 : 2)) < 0) + goto undo_soak; - if ((rc = otp_program(otp, address, prog)) < 0) - goto undo_soak; - } while (tries++ < NUM_PROG_TRIES); + if ((rc = otp_program(otp, address, prog)) < 0) + goto undo_soak; + } while (tries++ < NUM_PROG_TRIES); - if (tries == NUM_PROG_TRIES) { - loge("Failed to program OTP\n"); - rc = -EREMOTEIO; - } else - logi("Success!\n"); + if (tries == NUM_PROG_TRIES) { + loge("Failed to program OTP\n"); + rc = -EREMOTEIO; + } else + logi("Success!\n"); undo_soak: - otp_set_soak(otp, 0); + otp_set_soak(otp, 0); - return rc; + return rc; } int otp_read(struct otp *otp, enum otp_region reg) { - int rc; + int rc; - if ((rc = otp_writel(otp, OTP_PROTECT_KEY, OTP_PASSWD)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_PROTECT_KEY, OTP_PASSWD)) < 0) + return rc; - if (reg == otp_region_strap) { - int i; - uint32_t res[2] = { 0, 0}; - uint32_t strap[6][2]; - uint32_t protect[2]; - uint32_t scu_protect[2]; + if (reg == otp_region_strap) { + int i; + uint32_t res[2] = { 0, 0 }; + uint32_t strap[6][2]; + uint32_t protect[2]; + uint32_t scu_protect[2]; - if ((rc = otp_read_config(otp, 28, &scu_protect[0])) < 0) - goto done; + if ((rc = otp_read_config(otp, 28, &scu_protect[0])) < 0) + goto done; - if ((rc = otp_read_config(otp, 29, &scu_protect[1])) < 0) - goto done; + if ((rc = otp_read_config(otp, 29, &scu_protect[1])) < 0) + goto done; - if ((rc = otp_read_config(otp, 30, &protect[0])) < 0) - goto done; + if ((rc = otp_read_config(otp, 30, &protect[0])) < 0) + goto done; - if ((rc = otp_read_config(otp, 31, &protect[1])) < 0) - goto done; + if ((rc = otp_read_config(otp, 31, &protect[1])) < 0) + goto done; - for (i = 0; i < 6; ++i) { - int o = 16 + (i * 2); + for (i = 0; i < 6; ++i) { + int o = 16 + (i * 2); - if ((rc = otp_read_config(otp, o, &strap[i][0])) < 0) - goto done; + if ((rc = otp_read_config(otp, o, &strap[i][0])) < 0) + goto done; - if ((rc = otp_read_config(otp, o + 1, &strap[i][1])) < 0) - goto done; + if ((rc = otp_read_config(otp, o + 1, &strap[i][1])) < + 0) + goto done; - res[0] ^= strap[i][0]; - res[1] ^= strap[i][1]; - } + res[0] ^= strap[i][0]; + res[1] ^= strap[i][1]; + } - logi("OTP straps:\t\t63 32 31 0\n"); - logi("Protect SCU:\t%08x %08x\n", scu_protect[1], scu_protect[0]); - logi("Protect:\t\t%08x %08x\n", protect[1], protect[0]); + logi("OTP straps:\t\t63 32 31 0\n"); + logi("Protect SCU:\t%08x %08x\n", scu_protect[1], + scu_protect[0]); + logi("Protect:\t\t%08x %08x\n", protect[1], protect[0]); - for (i = 0; i < 6; ++i) - logi("Option %u:\t\t%08x %08x\n", i, strap[i][1], strap[i][0]); + for (i = 0; i < 6; ++i) + logi("Option %u:\t\t%08x %08x\n", i, strap[i][1], + strap[i][0]); - logi("Result:\t\t%08x %08x\n", res[1], res[0]); - } else { - unsigned char i; - uint32_t conf[NUM_OTP_CONF]; + logi("Result:\t\t%08x %08x\n", res[1], res[0]); + } else { + unsigned char i; + uint32_t conf[NUM_OTP_CONF]; - for (i = 0; i < NUM_OTP_CONF; ++i) { - if ((rc = otp_read_config(otp, i, &conf[i])) < 0) - goto done; - } + for (i = 0; i < NUM_OTP_CONF; ++i) { + if ((rc = otp_read_config(otp, i, &conf[i])) < 0) + goto done; + } - logi("OTP configuration:\n"); - for (i = 0; i < NUM_OTP_CONF; ++i) - logi("%02u: %08x\n", i, conf[i]); - } + logi("OTP configuration:\n"); + for (i = 0; i < NUM_OTP_CONF; ++i) + logi("%02u: %08x\n", i, conf[i]); + } done: - otp_writel(otp, OTP_PROTECT_KEY, 0); + otp_writel(otp, OTP_PROTECT_KEY, 0); - return rc; + return rc; } int otp_write_conf(struct otp *otp, unsigned int word, unsigned int bit) { - int rc; - uint32_t conf; - uint32_t address; - uint32_t bitmask; + int rc; + uint32_t conf; + uint32_t address; + uint32_t bitmask; - if (word >= NUM_OTP_CONF || bit >= 32) - return -EINVAL; + if (word >= NUM_OTP_CONF || bit >= 32) + return -EINVAL; - bitmask = 1 << bit; + bitmask = 1 << bit; - if ((rc = otp_writel(otp, OTP_PROTECT_KEY, OTP_PASSWD)) < 0) - return rc; + if ((rc = otp_writel(otp, OTP_PROTECT_KEY, OTP_PASSWD)) < 0) + return rc; - if ((rc = otp_read_config(otp, word, &conf)) < 0) - goto done; + if ((rc = otp_read_config(otp, word, &conf)) < 0) + goto done; - if (conf & bitmask) { - loge("Configuration bit already set\n"); - rc = -EALREADY; - goto done; - } + if (conf & bitmask) { + loge("Configuration bit already set\n"); + rc = -EALREADY; + goto done; + } - address = 0x800; - address |= (word / 8) * 0x200; - address |= (word % 8) * 2; + address = 0x800; + address |= (word / 8) * 0x200; + address |= (word % 8) * 2; - logi("Writing configuration at OTP %04x with %08x\n", address, bitmask); - if ((rc = otp_confirm()) < 0) - goto done; + logi("Writing configuration at OTP %04x with %08x\n", address, bitmask); + if ((rc = otp_confirm()) < 0) + goto done; - rc = otp_write(otp, address, bitmask); + rc = otp_write(otp, address, bitmask); done: - otp_writel(otp, OTP_PROTECT_KEY, 0); - return rc; + otp_writel(otp, OTP_PROTECT_KEY, 0); + return rc; } int otp_write_strap(struct otp *otp, unsigned int bit, unsigned int val) { - int i; - int rc; - int f = -1; - uint32_t address; - uint32_t bitmask; - uint32_t protect; - uint32_t res = 0; - uint32_t word = 0; - uint32_t strap[6]; - - if (bit >= 64 || val > 1) - return -EINVAL; - - if ((rc = otp_writel(otp, OTP_PROTECT_KEY, OTP_PASSWD)) < 0) - return rc; - - if (bit > 31) { - word = 1; - bit -= 32; - } - - bitmask = 1 << bit; - - if ((rc = otp_read_config(otp, 30 + word, &protect)) < 0) - goto done; - - if (protect & bitmask) { - loge("Cannot write strap; bit is protected\n"); - rc = -EACCES; - goto done; - } - - for (i = 0; i < 6; ++i) { - uint32_t o = 16 + (i * 2); - - if ((rc = otp_read_config(otp, o + word, &strap[i])) < 0) - goto done; - - res ^= strap[i]; - if (f < 0 && !(strap[i] & bitmask)) - f = i; - } - - if (f < 0) { - loge("Strap cannot be configured further\n"); - rc = -EPERM; - goto done; - } - - if (((res & bitmask) && val) || (!val && !(res & bitmask))) { - loge("Strap already in desired configuration\n"); - rc = -EALREADY; - goto done; - } - - i = (16 + (f * 2)) + word; - address = 0x800; - address |= (i / 8) * 0x200; - address |= (i % 8) * 2; - - logi("Writing strap at OTP %04x with %08x\n", address, bitmask); - if ((rc = otp_confirm()) < 0) - goto done; - - rc = otp_write(otp, address, bitmask); + int i; + int rc; + int f = -1; + uint32_t address; + uint32_t bitmask; + uint32_t protect; + uint32_t res = 0; + uint32_t word = 0; + uint32_t strap[6]; + + if (bit >= 64 || val > 1) + return -EINVAL; + + if ((rc = otp_writel(otp, OTP_PROTECT_KEY, OTP_PASSWD)) < 0) + return rc; + + if (bit > 31) { + word = 1; + bit -= 32; + } + + bitmask = 1 << bit; + + if ((rc = otp_read_config(otp, 30 + word, &protect)) < 0) + goto done; + + if (protect & bitmask) { + loge("Cannot write strap; bit is protected\n"); + rc = -EACCES; + goto done; + } + + for (i = 0; i < 6; ++i) { + uint32_t o = 16 + (i * 2); + + if ((rc = otp_read_config(otp, o + word, &strap[i])) < 0) + goto done; + + res ^= strap[i]; + if (f < 0 && !(strap[i] & bitmask)) + f = i; + } + + if (f < 0) { + loge("Strap cannot be configured further\n"); + rc = -EPERM; + goto done; + } + + if (((res & bitmask) && val) || (!val && !(res & bitmask))) { + loge("Strap already in desired configuration\n"); + rc = -EALREADY; + goto done; + } + + i = (16 + (f * 2)) + word; + address = 0x800; + address |= (i / 8) * 0x200; + address |= (i % 8) * 2; + + logi("Writing strap at OTP %04x with %08x\n", address, bitmask); + if ((rc = otp_confirm()) < 0) + goto done; + + rc = otp_write(otp, address, bitmask); done: - otp_writel(otp, OTP_PROTECT_KEY, 0); + otp_writel(otp, OTP_PROTECT_KEY, 0); - return rc; + return rc; } static const struct soc_device_id otp_match[] = { - { .compatible = "aspeed,ast2600-secure-boot-controller" }, - { }, + { .compatible = "aspeed,ast2600-secure-boot-controller" }, + {}, }; static int otp_driver_init(struct soc *soc, struct soc_device *dev) { - struct otp *ctx; - int rc; + struct otp *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { + goto cleanup_ctx; + } - ctx->soc = soc; + ctx->soc = soc; - if (soc_stepping(soc) >= 2) { - logi("Detected AST2600 A2\n"); + if (soc_stepping(soc) >= 2) { + logi("Detected AST2600 A2\n"); - ctx->timings[0] = 0x04190760; - ctx->timings[1] = 0x04191388; - ctx->timings[2] = 0x04193a98; + ctx->timings[0] = 0x04190760; + ctx->timings[1] = 0x04191388; + ctx->timings[2] = 0x04193a98; - ctx->soak_parameters[0][0] = 0x0210; - ctx->soak_parameters[0][1] = 0x2000; - ctx->soak_parameters[0][2] = 0x0; + ctx->soak_parameters[0][0] = 0x0210; + ctx->soak_parameters[0][1] = 0x2000; + ctx->soak_parameters[0][2] = 0x0; - ctx->soak_parameters[1][0] = 0x1200; - ctx->soak_parameters[1][1] = 0x107f; - ctx->soak_parameters[1][2] = 0x1024; + ctx->soak_parameters[1][0] = 0x1200; + ctx->soak_parameters[1][1] = 0x107f; + ctx->soak_parameters[1][2] = 0x1024; - ctx->soak_parameters[2][0] = 0x1220; - ctx->soak_parameters[2][1] = 0x2074; - ctx->soak_parameters[2][2] = 0x08a4; - } else { - logi("Detected AST2600 A0/A1\n"); + ctx->soak_parameters[2][0] = 0x1220; + ctx->soak_parameters[2][1] = 0x2074; + ctx->soak_parameters[2][2] = 0x08a4; + } else { + logi("Detected AST2600 A0/A1\n"); - ctx->timings[0] = 0x04190760; - ctx->timings[1] = 0x04190760; - ctx->timings[2] = 0x041930d4; + ctx->timings[0] = 0x04190760; + ctx->timings[1] = 0x04190760; + ctx->timings[2] = 0x041930d4; - ctx->soak_parameters[0][0] = 0x0; - ctx->soak_parameters[0][1] = 0x0; - ctx->soak_parameters[0][2] = 0x0; + ctx->soak_parameters[0][0] = 0x0; + ctx->soak_parameters[0][1] = 0x0; + ctx->soak_parameters[0][2] = 0x0; - ctx->soak_parameters[1][0] = 0x4021; - ctx->soak_parameters[1][1] = 0x302f; - ctx->soak_parameters[1][2] = 0x4020; + ctx->soak_parameters[1][0] = 0x4021; + ctx->soak_parameters[1][1] = 0x302f; + ctx->soak_parameters[1][2] = 0x4020; - ctx->soak_parameters[2][0] = 0x4021; - ctx->soak_parameters[2][1] = 0x1027; - ctx->soak_parameters[2][2] = 0x4820; - } + ctx->soak_parameters[2][0] = 0x4021; + ctx->soak_parameters[2][1] = 0x1027; + ctx->soak_parameters[2][2] = 0x4820; + } - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void otp_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } static const struct soc_driver otp_driver = { - .name = "otp", - .matches = otp_match, - .init = otp_driver_init, - .destroy = otp_driver_destroy, + .name = "otp", + .matches = otp_match, + .init = otp_driver_init, + .destroy = otp_driver_destroy, }; REGISTER_SOC_DRIVER(otp_driver); struct otp *otp_get(struct soc *soc) { - return soc_driver_get_drvdata(soc, &otp_driver); + return soc_driver_get_drvdata(soc, &otp_driver); } diff --git a/src/soc/otp.h b/src/soc/otp.h index 83e0cbf..7b6c6ff 100644 --- a/src/soc/otp.h +++ b/src/soc/otp.h @@ -7,8 +7,8 @@ #include "soc.h" enum otp_region { - otp_region_strap, - otp_region_conf, + otp_region_strap, + otp_region_conf, }; struct otp; diff --git a/src/soc/pciectl.c b/src/soc/pciectl.c index 16da49f..07aa5f4 100644 --- a/src/soc/pciectl.c +++ b/src/soc/pciectl.c @@ -15,538 +15,574 @@ #include #include -#define G4_SCU_MISC 0x02c -#define G6_SCU_MISC 0x0c0 -#define G4_SCU_MISC_P2A_DRAM_RO (1 << 25) -#define G4_SCU_MISC_P2A_SPI_RO (1 << 24) -#define G4_SCU_MISC_P2A_SOC_RO (1 << 23) -#define G4_SCU_MISC_P2A_FMC_RO (1 << 22) -#define G5_SCU_MISC_P2A_DRAM_RO (1 << 25) -#define G5_SCU_MISC_P2A_LPCH_RO (1 << 24) -#define G5_SCU_MISC_P2A_SOC_RO (1 << 23) -#define G5_SCU_MISC_P2A_FLASH_RO (1 << 22) -#define G4_SCU_PCIE_CONFIG 0x180 -#define G6_SCU_PCIE_CONFIG 0xc20 -#define SCU_PCIE_CONFIG_BMC_XDMA (1 << 14) -#define SCU_PCIE_CONFIG_BMC_MMIO (1 << 9) -#define SCU_PCIE_CONFIG_BMC (1 << 8) -#define SCU_PCIE_CONFIG_VGA_XDMA (1 << 6) -#define SCU_PCIE_CONFIG_VGA_MMIO (1 << 1) -#define SCU_PCIE_CONFIG_VGA (1 << 0) +#define G4_SCU_MISC 0x02c +#define G6_SCU_MISC 0x0c0 +#define G4_SCU_MISC_P2A_DRAM_RO (1 << 25) +#define G4_SCU_MISC_P2A_SPI_RO (1 << 24) +#define G4_SCU_MISC_P2A_SOC_RO (1 << 23) +#define G4_SCU_MISC_P2A_FMC_RO (1 << 22) +#define G5_SCU_MISC_P2A_DRAM_RO (1 << 25) +#define G5_SCU_MISC_P2A_LPCH_RO (1 << 24) +#define G5_SCU_MISC_P2A_SOC_RO (1 << 23) +#define G5_SCU_MISC_P2A_FLASH_RO (1 << 22) +#define G4_SCU_PCIE_CONFIG 0x180 +#define G6_SCU_PCIE_CONFIG 0xc20 +#define SCU_PCIE_CONFIG_BMC_XDMA (1 << 14) +#define SCU_PCIE_CONFIG_BMC_MMIO (1 << 9) +#define SCU_PCIE_CONFIG_BMC (1 << 8) +#define SCU_PCIE_CONFIG_VGA_XDMA (1 << 6) +#define SCU_PCIE_CONFIG_VGA_MMIO (1 << 1) +#define SCU_PCIE_CONFIG_VGA (1 << 0) static const char *pcie_device_table[] = { - [pcie_device_vga] = "VGA", - [pcie_device_bmc] = "BMC", + [pcie_device_vga] = "VGA", + [pcie_device_bmc] = "BMC", }; -enum device_function { device_function_none = 0, device_function_mmio, device_function_xdma }; +enum device_function { + device_function_none = 0, + device_function_mmio, + device_function_xdma +}; -static const char* device_function_table[] = { - [device_function_none] = "none", - [device_function_mmio] = "MMIO", - [device_function_xdma] = "XDMA", +static const char *device_function_table[] = { + [device_function_none] = "none", + [device_function_mmio] = "MMIO", + [device_function_xdma] = "XDMA", }; struct pciectl_p2a_region { - const char *name; - uint32_t mask; - uint32_t start; - uint32_t length; + const char *name; + uint32_t mask; + uint32_t start; + uint32_t length; }; struct pciectl_endpoint { - struct { - enum pcie_device type; - uint32_t mask; - } device; + struct { + enum pcie_device type; + uint32_t mask; + } device; - struct { - enum device_function type; - uint32_t mask; - } function; + struct { + enum device_function type; + uint32_t mask; + } function; - const char *gate; + const char *gate; }; struct pciectl_pdata { - uint32_t misc; - uint32_t config; - const struct pciectl_endpoint *endpoints; - const struct pciectl_p2a_region *regions; + uint32_t misc; + uint32_t config; + const struct pciectl_endpoint *endpoints; + const struct pciectl_p2a_region *regions; }; struct pciectl { - struct bridgectl p2actl; - struct bridgectl xdmactl; - struct bridges *bridges; - struct soc *soc; - struct soc_device *dev; - struct soc_region scu; - const struct pciectl_pdata *pdata; - struct sdmc *sdmc; + struct bridgectl p2actl; + struct bridgectl xdmactl; + struct bridges *bridges; + struct soc *soc; + struct soc_device *dev; + struct soc_region scu; + const struct pciectl_pdata *pdata; + struct sdmc *sdmc; }; -#define p2actl_to_pciectl(b) container_of((b), struct pciectl, p2actl) +#define p2actl_to_pciectl(b) container_of((b), struct pciectl, p2actl) #define xdmactl_to_pciectl(b) container_of((b), struct pciectl, xdmactl) struct bridgectl *p2actl_as_bridgectl(struct pciectl *ctx) { - return &ctx->p2actl; + return &ctx->p2actl; } struct bridgectl *xdmactl_as_bridgectl(struct pciectl *ctx) { - return &ctx->xdmactl; + return &ctx->xdmactl; } -static uint32_t pciectl_pdata_collect_region_mask(const struct pciectl_pdata *pdata, - const struct pciectl_endpoint *ep) +static uint32_t +pciectl_pdata_collect_region_mask(const struct pciectl_pdata *pdata, + const struct pciectl_endpoint *ep) { - const struct pciectl_p2a_region *curr; - uint32_t mask = 0; + const struct pciectl_p2a_region *curr; + uint32_t mask = 0; - if (ep->function.type != device_function_mmio) { - return 0; - } + if (ep->function.type != device_function_mmio) { + return 0; + } - curr = pdata->regions; + curr = pdata->regions; - while(curr && curr->name) { - mask |= curr->mask; - curr++; - } + while (curr && curr->name) { + mask |= curr->mask; + curr++; + } - return mask; + return mask; } -static int -pciectl_device_enforce(struct pciectl *ctx, const struct pciectl_endpoint *ep, enum bridge_mode mode) +static int pciectl_device_enforce(struct pciectl *ctx, + const struct pciectl_endpoint *ep, + enum bridge_mode mode) { - uint32_t pcie, misc; - uint32_t mask; - int rc; - - if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->config, &pcie)) < 0) { - loge("Failed to read PCIe device configuration: %d\n", rc); - return rc; - } - - if (mode == bm_disabled) { - pcie &= ~ep->function.mask; - - if ((rc = soc_writel(ctx->soc, ctx->scu.start + ctx->pdata->config, pcie)) < 0) { - loge("Failed to disable PCIe MMIO regions: %d\n", rc); - } - - return rc; - } - - if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->misc, &misc)) < 0) { - loge("Failed to read PCIe MMIO region configuration: %d\n", rc); - return rc; - } - - mask = pciectl_pdata_collect_region_mask(ctx->pdata, ep); - if (!mask) { - return 0; - } - - if (mode == bm_restricted) { - misc |= mask; - } else { - assert(mode == bm_permissive); - misc &= ~mask; - } - - if ((rc = soc_writel(ctx->soc, ctx->scu.start + ctx->pdata->misc, misc)) < 0) { - loge("Failed to write PCIe MMIO region configuration: %d\n", rc); - return rc; - } - - pcie |= ep->device.mask | ep->function.mask; - if ((rc = soc_writel(ctx->soc, ctx->scu.start + ctx->pdata->config, pcie)) < 0) { - loge("Failed to enable PCIe VGA device: %d\n", rc); - } - - if (ctx->bridges) { - int id; - - if ((rc = bridges_device_get_gate_by_name(ctx->soc, ctx->dev, ep->gate, NULL, &id)) < 0) { - loge("Failed to resolve bridge gate for gate name '%s': %d\n", ep->gate, rc); - return rc; - } - - bridges_enable(ctx->bridges, id); - } - - return rc; + uint32_t pcie, misc; + uint32_t mask; + int rc; + + if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->config, + &pcie)) < 0) { + loge("Failed to read PCIe device configuration: %d\n", rc); + return rc; + } + + if (mode == bm_disabled) { + pcie &= ~ep->function.mask; + + if ((rc = soc_writel(ctx->soc, + ctx->scu.start + ctx->pdata->config, + pcie)) < 0) { + loge("Failed to disable PCIe MMIO regions: %d\n", rc); + } + + return rc; + } + + if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->misc, + &misc)) < 0) { + loge("Failed to read PCIe MMIO region configuration: %d\n", rc); + return rc; + } + + mask = pciectl_pdata_collect_region_mask(ctx->pdata, ep); + if (!mask) { + return 0; + } + + if (mode == bm_restricted) { + misc |= mask; + } else { + assert(mode == bm_permissive); + misc &= ~mask; + } + + if ((rc = soc_writel(ctx->soc, ctx->scu.start + ctx->pdata->misc, + misc)) < 0) { + loge("Failed to write PCIe MMIO region configuration: %d\n", + rc); + return rc; + } + + pcie |= ep->device.mask | ep->function.mask; + if ((rc = soc_writel(ctx->soc, ctx->scu.start + ctx->pdata->config, + pcie)) < 0) { + loge("Failed to enable PCIe VGA device: %d\n", rc); + } + + if (ctx->bridges) { + int id; + + if ((rc = bridges_device_get_gate_by_name( + ctx->soc, ctx->dev, ep->gate, NULL, &id)) < 0) { + loge("Failed to resolve bridge gate for gate name '%s': %d\n", + ep->gate, rc); + return rc; + } + + bridges_enable(ctx->bridges, id); + } + + return rc; } -static int -pciectl_device_status(struct pciectl *ctx, const struct pciectl_endpoint *ep, enum bridge_mode *mode) +static int pciectl_device_status(struct pciectl *ctx, + const struct pciectl_endpoint *ep, + enum bridge_mode *mode) { - uint32_t pcie, misc; - uint32_t mask; - int rc; - - if (ctx->bridges) { - int id; - - if ((rc = bridges_device_get_gate_by_name(ctx->soc, ctx->dev, ep->gate, NULL, &id)) < 0) { - loge("Failed to resolve bridge gate for gate name '%s': %d\n", ep->gate, rc); - return rc; - } - - if ((rc = bridges_status(ctx->bridges, id)) < 0) { - loge("Failed to query state of bridge gate %d (%s): %d\n", id, ep->gate, rc); - return rc; - } - - if (!rc) { - *mode = bm_disabled; - return 0; - } - } - - if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->config, &pcie)) < 0) { - loge("Failed to read PCIe device configuration: %d\n", rc); - return rc; - } - - mask = ep->device.mask | ep->function.mask; - if (!((pcie & mask) == mask)) { - *mode = bm_disabled; - return 0; - } - - mask = pciectl_pdata_collect_region_mask(ctx->pdata, ep); - if (!mask) { - *mode = bm_permissive; - return 0; - } - - if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->misc, &misc)) < 0) { - loge("Failed to read PCIe MMIO region configuration: %d\n", rc); - return rc; - } - - *mode = ((misc & mask) == mask) ? bm_restricted : bm_permissive; - - return 0; + uint32_t pcie, misc; + uint32_t mask; + int rc; + + if (ctx->bridges) { + int id; + + if ((rc = bridges_device_get_gate_by_name( + ctx->soc, ctx->dev, ep->gate, NULL, &id)) < 0) { + loge("Failed to resolve bridge gate for gate name '%s': %d\n", + ep->gate, rc); + return rc; + } + + if ((rc = bridges_status(ctx->bridges, id)) < 0) { + loge("Failed to query state of bridge gate %d (%s): %d\n", + id, ep->gate, rc); + return rc; + } + + if (!rc) { + *mode = bm_disabled; + return 0; + } + } + + if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->config, + &pcie)) < 0) { + loge("Failed to read PCIe device configuration: %d\n", rc); + return rc; + } + + mask = ep->device.mask | ep->function.mask; + if (!((pcie & mask) == mask)) { + *mode = bm_disabled; + return 0; + } + + mask = pciectl_pdata_collect_region_mask(ctx->pdata, ep); + if (!mask) { + *mode = bm_permissive; + return 0; + } + + if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->misc, + &misc)) < 0) { + loge("Failed to read PCIe MMIO region configuration: %d\n", rc); + return rc; + } + + *mode = ((misc & mask) == mask) ? bm_restricted : bm_permissive; + + return 0; } -static int -pciectl_device_report(struct pciectl *ctx, int fd, const struct pciectl_endpoint *ep) +static int pciectl_device_report(struct pciectl *ctx, int fd, + const struct pciectl_endpoint *ep) { - const char *description; - uint32_t pcie; - bool enabled; - int rc; - - enabled = true; - - if (ctx->bridges) { - int id; - - if ((rc = bridges_device_get_gate_by_name(ctx->soc, ctx->dev, ep->gate, NULL, &id)) < 0) { - loge("Failed to resolve bridge gate for gate name '%s': %d\n", ep->gate, rc); - return rc; - } - - if ((rc = bridges_status(ctx->bridges, id)) < 0) { - loge("Failed to query state of bridge gate %d (%s): %d\n", id, ep->gate, rc); - return rc; - } - - enabled = enabled && rc; - } - - if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->config, &pcie)) < 0) { - loge("Failed to read PCIe device configuration: %d\n", rc); - return rc; - } - - /* Device */ - enabled = enabled && (pcie & ep->device.mask); - description = enabled ? "Enabled" : "Disabled"; - dprintf(fd, "\t%s: %s\n", pcie_device_table[ep->device.type], description); - if (!enabled) { - return 0; - } - - /* Function on device */ - enabled = pcie & ep->function.mask; - description = enabled ? "Enabled" : "Disabled"; - dprintf(fd, "\t%s on %s: %s\n", device_function_table[ep->function.type], - pcie_device_table[ep->device.type], description); - - return 0; + const char *description; + uint32_t pcie; + bool enabled; + int rc; + + enabled = true; + + if (ctx->bridges) { + int id; + + if ((rc = bridges_device_get_gate_by_name( + ctx->soc, ctx->dev, ep->gate, NULL, &id)) < 0) { + loge("Failed to resolve bridge gate for gate name '%s': %d\n", + ep->gate, rc); + return rc; + } + + if ((rc = bridges_status(ctx->bridges, id)) < 0) { + loge("Failed to query state of bridge gate %d (%s): %d\n", + id, ep->gate, rc); + return rc; + } + + enabled = enabled && rc; + } + + if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->config, + &pcie)) < 0) { + loge("Failed to read PCIe device configuration: %d\n", rc); + return rc; + } + + /* Device */ + enabled = enabled && (pcie & ep->device.mask); + description = enabled ? "Enabled" : "Disabled"; + dprintf(fd, "\t%s: %s\n", pcie_device_table[ep->device.type], + description); + if (!enabled) { + return 0; + } + + /* Function on device */ + enabled = pcie & ep->function.mask; + description = enabled ? "Enabled" : "Disabled"; + dprintf(fd, "\t%s on %s: %s\n", + device_function_table[ep->function.type], + pcie_device_table[ep->device.type], description); + + return 0; } -static int -pciectl_enforce(struct pciectl *ctx, enum bridge_mode mode, enum device_function function) +static int pciectl_enforce(struct pciectl *ctx, enum bridge_mode mode, + enum device_function function) { - const struct pciectl_endpoint *curr; - int rc; - - for (curr = ctx->pdata->endpoints; - curr->function.type != device_function_none; - curr++) { - if (curr->function.type != function) { - continue; - } - - if ((rc = pciectl_device_enforce(ctx, curr, mode)) < 0) { - loge("Failed to enforce bridge state for %s %s: %d\n", - pcie_device_table[curr->device.type], device_function_table[curr->function.type], rc); - return rc; - } - } - - return 0; + const struct pciectl_endpoint *curr; + int rc; + + for (curr = ctx->pdata->endpoints; + curr->function.type != device_function_none; curr++) { + if (curr->function.type != function) { + continue; + } + + if ((rc = pciectl_device_enforce(ctx, curr, mode)) < 0) { + loge("Failed to enforce bridge state for %s %s: %d\n", + pcie_device_table[curr->device.type], + device_function_table[curr->function.type], rc); + return rc; + } + } + + return 0; } -static int -pciectl_status(struct pciectl *ctx, enum bridge_mode *mode, enum device_function function) +static int pciectl_status(struct pciectl *ctx, enum bridge_mode *mode, + enum device_function function) { - enum bridge_mode curr_mode, aggregate_mode; - const struct pciectl_endpoint *curr; - int rc; - - aggregate_mode = bm_disabled; - for (curr = ctx->pdata->endpoints; - curr->function.type != device_function_none; - curr++) { - if (curr->function.type != function) { - continue; - } - - if ((rc = pciectl_device_status(ctx, curr, &curr_mode)) < 0) { - loge("Failed to query bridge state for %s %s: %d\n", - pcie_device_table[curr->device.type], device_function_table[curr->function.type], rc); - return rc; - } - - if (curr_mode < aggregate_mode) { - aggregate_mode = curr_mode; - } - } - - *mode = aggregate_mode; - - return 0; + enum bridge_mode curr_mode, aggregate_mode; + const struct pciectl_endpoint *curr; + int rc; + + aggregate_mode = bm_disabled; + for (curr = ctx->pdata->endpoints; + curr->function.type != device_function_none; curr++) { + if (curr->function.type != function) { + continue; + } + + if ((rc = pciectl_device_status(ctx, curr, &curr_mode)) < 0) { + loge("Failed to query bridge state for %s %s: %d\n", + pcie_device_table[curr->device.type], + device_function_table[curr->function.type], rc); + return rc; + } + + if (curr_mode < aggregate_mode) { + aggregate_mode = curr_mode; + } + } + + *mode = aggregate_mode; + + return 0; } -static int pciectl_report(struct pciectl *ctx, int fd, enum device_function function) +static int pciectl_report(struct pciectl *ctx, int fd, + enum device_function function) { - const struct pciectl_endpoint *curr; - int rc; - - for (curr = ctx->pdata->endpoints; - curr->function.type != device_function_none; - curr++) { - if (curr->function.type != function) { - continue; - } - - if ((rc = pciectl_device_report(ctx, fd, curr)) < 0) { - loge("Failed to enforce bridge state for %s %s: %d\n", - pcie_device_table[curr->device.type], device_function_table[curr->function.type], rc); - return rc; - } - } - - return 0; + const struct pciectl_endpoint *curr; + int rc; + + for (curr = ctx->pdata->endpoints; + curr->function.type != device_function_none; curr++) { + if (curr->function.type != function) { + continue; + } + + if ((rc = pciectl_device_report(ctx, fd, curr)) < 0) { + loge("Failed to enforce bridge state for %s %s: %d\n", + pcie_device_table[curr->device.type], + device_function_table[curr->function.type], rc); + return rc; + } + } + + return 0; } static const char *p2actl_name(struct bridgectl *bridge __unused) { - return "p2a"; + return "p2a"; } static int p2actl_enforce(struct bridgectl *bridge, enum bridge_mode mode) { - return pciectl_enforce(p2actl_to_pciectl(bridge), mode, device_function_mmio); + return pciectl_enforce(p2actl_to_pciectl(bridge), mode, + device_function_mmio); } static int p2actl_status(struct bridgectl *bridge, enum bridge_mode *mode) { - return pciectl_status(p2actl_to_pciectl(bridge), mode, device_function_mmio); + return pciectl_status(p2actl_to_pciectl(bridge), mode, + device_function_mmio); } -static int p2actl_report(struct bridgectl *bridge, int fd, enum bridge_mode *mode) +static int p2actl_report(struct bridgectl *bridge, int fd, + enum bridge_mode *mode) { - struct pciectl *ctx = p2actl_to_pciectl(bridge); - const struct pciectl_p2a_region *curr; - uint32_t misc; - int rc; + struct pciectl *ctx = p2actl_to_pciectl(bridge); + const struct pciectl_p2a_region *curr; + uint32_t misc; + int rc; - if ((rc = p2actl_status(bridge, mode)) < 0) { - loge("Failed to read P2A bridge status: %d\n", rc); - return rc; - } + if ((rc = p2actl_status(bridge, mode)) < 0) { + loge("Failed to read P2A bridge status: %d\n", rc); + return rc; + } - bridgectl_log_status(bridge, fd, *mode); + bridgectl_log_status(bridge, fd, *mode); - if (*mode == bm_disabled) { - return 0; - } + if (*mode == bm_disabled) { + return 0; + } - if ((rc = pciectl_report(ctx, fd, device_function_mmio)) < 0) { - loge("Failed to report P2A bridge state: %d\n", rc); - return rc; - } + if ((rc = pciectl_report(ctx, fd, device_function_mmio)) < 0) { + loge("Failed to report P2A bridge state: %d\n", rc); + return rc; + } - if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->misc, &misc)) < 0) { - loge("Failed to read P2A region filter configuration: %d\n", rc); - return rc; - } + if ((rc = soc_readl(ctx->soc, ctx->scu.start + ctx->pdata->misc, + &misc)) < 0) { + loge("Failed to read P2A region filter configuration: %d\n", + rc); + return rc; + } - curr = ctx->pdata->regions; - while (curr && curr->name) { - const char *permission; - uint32_t end; + curr = ctx->pdata->regions; + while (curr && curr->name) { + const char *permission; + uint32_t end; - permission = (misc & curr->mask) ? "Readable" : "Writable"; - end = curr->start + (curr->length - 1); + permission = (misc & curr->mask) ? "Readable" : "Writable"; + end = curr->start + (curr->length - 1); - dprintf(fd, "\t[0x%08x - 0x%08x] %10s: %s\n", curr->start, end, curr->name, permission); + dprintf(fd, "\t[0x%08x - 0x%08x] %10s: %s\n", curr->start, end, + curr->name, permission); - curr++; - } + curr++; + } - return 0; + return 0; } static const struct bridgectl_ops p2actl_ops = { - .name = p2actl_name, - .enforce = p2actl_enforce, - .status = p2actl_status, - .report = p2actl_report, + .name = p2actl_name, + .enforce = p2actl_enforce, + .status = p2actl_status, + .report = p2actl_report, }; static const char *xdmactl_name(struct bridgectl *bridge __unused) { - return "xdma"; + return "xdma"; } static int xdmactl_enforce(struct bridgectl *bridge, enum bridge_mode mode) { - struct pciectl *ctx = xdmactl_to_pciectl(bridge); - int rc; + struct pciectl *ctx = xdmactl_to_pciectl(bridge); + int rc; - if ((rc = sdmc_configure_xdma(ctx->sdmc, (mode == bm_permissive))) < 0) { - loge("Failed to configure XDMA constraint in SDMC: %d\n", rc); - return rc; - } + if ((rc = sdmc_configure_xdma(ctx->sdmc, (mode == bm_permissive))) < + 0) { + loge("Failed to configure XDMA constraint in SDMC: %d\n", rc); + return rc; + } - return pciectl_enforce(ctx, mode, device_function_xdma); + return pciectl_enforce(ctx, mode, device_function_xdma); } static int xdmactl_status(struct bridgectl *bridge, enum bridge_mode *mode) { - struct pciectl *ctx = xdmactl_to_pciectl(bridge); - int rc; + struct pciectl *ctx = xdmactl_to_pciectl(bridge); + int rc; - if ((rc = pciectl_status(xdmactl_to_pciectl(bridge), mode, device_function_xdma)) < 0) { - loge("Failed to assess XDMA bridge status: %d\n", rc); - return rc; - } + if ((rc = pciectl_status(xdmactl_to_pciectl(bridge), mode, + device_function_xdma)) < 0) { + loge("Failed to assess XDMA bridge status: %d\n", rc); + return rc; + } - if ((rc = sdmc_constrains_xdma(ctx->sdmc)) < 0) { - loge("Failed to assess XDMA access contraint: %d\n", rc); - return rc; - } + if ((rc = sdmc_constrains_xdma(ctx->sdmc)) < 0) { + loge("Failed to assess XDMA access contraint: %d\n", rc); + return rc; + } - if (*mode == bm_permissive && rc) { - *mode = bm_restricted; - } + if (*mode == bm_permissive && rc) { + *mode = bm_restricted; + } - return 0; + return 0; } -static int xdmactl_report(struct bridgectl *bridge, int fd, enum bridge_mode *mode) +static int xdmactl_report(struct bridgectl *bridge, int fd, + enum bridge_mode *mode) { - struct pciectl *ctx = xdmactl_to_pciectl(bridge); - int rc; + struct pciectl *ctx = xdmactl_to_pciectl(bridge); + int rc; - if ((rc = xdmactl_status(bridge, mode)) < 0) { - loge("Failed to read P2A bridge status: %d\n", rc); - return rc; - } + if ((rc = xdmactl_status(bridge, mode)) < 0) { + loge("Failed to read P2A bridge status: %d\n", rc); + return rc; + } - bridgectl_log_status(bridge, fd, *mode); + bridgectl_log_status(bridge, fd, *mode); - if (*mode == bm_disabled) { - return 0; - } + if (*mode == bm_disabled) { + return 0; + } - if ((rc = pciectl_report(ctx, fd, device_function_xdma)) < 0) { - loge("Failed to report P2A bridge state: %d\n", rc); - return rc; - } + if ((rc = pciectl_report(ctx, fd, device_function_xdma)) < 0) { + loge("Failed to report P2A bridge state: %d\n", rc); + return rc; + } - if ((rc = sdmc_constrains_xdma(ctx->sdmc)) < 0) { - loge("Failed to access XDMA access contraint: %d\n", rc); - return rc; - } + if ((rc = sdmc_constrains_xdma(ctx->sdmc)) < 0) { + loge("Failed to access XDMA access contraint: %d\n", rc); + return rc; + } - dprintf(fd, "\tXDMA is constrained: %s\n", rc ? "Yes" : "No"); + dprintf(fd, "\tXDMA is constrained: %s\n", rc ? "Yes" : "No"); - return 0; + return 0; } - static const struct bridgectl_ops xdmactl_ops = { - .name = xdmactl_name, - .enforce = xdmactl_enforce, - .status = xdmactl_status, - .report = xdmactl_report, + .name = xdmactl_name, + .enforce = xdmactl_enforce, + .status = xdmactl_status, + .report = xdmactl_report, }; static const struct pciectl_p2a_region ast2400_p2a_regions[] = { - { - .name = "Firmware", - .mask = G4_SCU_MISC_P2A_FMC_RO, - .start = 0, - .length = 0x18000000, - }, - { - .name = "SoC IO", - .mask = G4_SCU_MISC_P2A_SOC_RO, - .start = 0x18000000, - .length = 0x08000000, - }, - { - .name = "BMC Flash", - .mask = G4_SCU_MISC_P2A_FMC_RO, - .start = 0x20000000, - .length = 0x10000000, - }, - { - .name = "Host Flash", - .mask = G4_SCU_MISC_P2A_SPI_RO, - .start = 0x30000000, - .length = 0x10000000, - }, - { - .name = "DRAM", - .mask = G4_SCU_MISC_P2A_DRAM_RO, - .start = 0x40000000, - .length = 0x20000000, - }, - { - .name = "LPC Host", - .mask = G4_SCU_MISC_P2A_SOC_RO, - .start = 0x60000000, - .length = 0x20000000, - }, - { - .name = "Reserved", - .mask = G4_SCU_MISC_P2A_SOC_RO, - .start = 0x80000000, - .length = 0x80000000, - }, - { }, + { + .name = "Firmware", + .mask = G4_SCU_MISC_P2A_FMC_RO, + .start = 0, + .length = 0x18000000, + }, + { + .name = "SoC IO", + .mask = G4_SCU_MISC_P2A_SOC_RO, + .start = 0x18000000, + .length = 0x08000000, + }, + { + .name = "BMC Flash", + .mask = G4_SCU_MISC_P2A_FMC_RO, + .start = 0x20000000, + .length = 0x10000000, + }, + { + .name = "Host Flash", + .mask = G4_SCU_MISC_P2A_SPI_RO, + .start = 0x30000000, + .length = 0x10000000, + }, + { + .name = "DRAM", + .mask = G4_SCU_MISC_P2A_DRAM_RO, + .start = 0x40000000, + .length = 0x20000000, + }, + { + .name = "LPC Host", + .mask = G4_SCU_MISC_P2A_SOC_RO, + .start = 0x60000000, + .length = 0x20000000, + }, + { + .name = "Reserved", + .mask = G4_SCU_MISC_P2A_SOC_RO, + .start = 0x80000000, + .length = 0x80000000, + }, + {}, }; static const struct pciectl_endpoint ast2400_pcie_bridges[] = { @@ -594,56 +630,56 @@ static const struct pciectl_endpoint ast2400_pcie_bridges[] = { }; static const struct pciectl_pdata ast2400_pdata = { - .misc = G4_SCU_MISC, - .config = G4_SCU_PCIE_CONFIG, - .endpoints = ast2400_pcie_bridges, - .regions = ast2400_p2a_regions, + .misc = G4_SCU_MISC, + .config = G4_SCU_PCIE_CONFIG, + .endpoints = ast2400_pcie_bridges, + .regions = ast2400_p2a_regions, }; static const struct pciectl_p2a_region ast2500_p2a_regions[] = { - { - .name = "Firmware", - .mask = G5_SCU_MISC_P2A_FLASH_RO, - .start = 0, - .length = 0x10000000, - }, - { - .name = "SoC IO", - .mask = G5_SCU_MISC_P2A_SOC_RO, - .start = 0x10000000, - .length = 0x10000000, - }, - { - .name = "BMC Flash", - .mask = G5_SCU_MISC_P2A_FLASH_RO, - .start = 0x20000000, - .length = 0x10000000, - }, - { - .name = "Host Flash", - .mask = G5_SCU_MISC_P2A_FLASH_RO, - .start = 0x30000000, - .length = 0x10000000, - }, - { - .name = "Reserved", - .mask = G5_SCU_MISC_P2A_SOC_RO, - .start = 0x40000000, - .length = 0x20000000, - }, - { - .name = "LPC Host", - .mask = G5_SCU_MISC_P2A_LPCH_RO, - .start = 0x60000000, - .length = 0x20000000, - }, - { - .name = "DRAM", - .mask = G5_SCU_MISC_P2A_DRAM_RO, - .start = 0x80000000, - .length = 0x80000000, - }, - { }, + { + .name = "Firmware", + .mask = G5_SCU_MISC_P2A_FLASH_RO, + .start = 0, + .length = 0x10000000, + }, + { + .name = "SoC IO", + .mask = G5_SCU_MISC_P2A_SOC_RO, + .start = 0x10000000, + .length = 0x10000000, + }, + { + .name = "BMC Flash", + .mask = G5_SCU_MISC_P2A_FLASH_RO, + .start = 0x20000000, + .length = 0x10000000, + }, + { + .name = "Host Flash", + .mask = G5_SCU_MISC_P2A_FLASH_RO, + .start = 0x30000000, + .length = 0x10000000, + }, + { + .name = "Reserved", + .mask = G5_SCU_MISC_P2A_SOC_RO, + .start = 0x40000000, + .length = 0x20000000, + }, + { + .name = "LPC Host", + .mask = G5_SCU_MISC_P2A_LPCH_RO, + .start = 0x60000000, + .length = 0x20000000, + }, + { + .name = "DRAM", + .mask = G5_SCU_MISC_P2A_DRAM_RO, + .start = 0x80000000, + .length = 0x80000000, + }, + {}, }; static const struct pciectl_endpoint ast2500_pcie_bridges[] = { @@ -691,10 +727,10 @@ static const struct pciectl_endpoint ast2500_pcie_bridges[] = { }; static const struct pciectl_pdata ast2500_pdata = { - .misc = G4_SCU_MISC, - .config = G4_SCU_PCIE_CONFIG, - .endpoints = ast2500_pcie_bridges, - .regions = ast2500_p2a_regions, + .misc = G4_SCU_MISC, + .config = G4_SCU_PCIE_CONFIG, + .endpoints = ast2500_pcie_bridges, + .regions = ast2500_p2a_regions, }; static const struct pciectl_endpoint ast2600_pcie_bridges[] = { @@ -746,83 +782,89 @@ static const struct pciectl_endpoint ast2600_pcie_bridges[] = { }; static const struct pciectl_pdata ast2600_pdata = { - .misc = G6_SCU_MISC, - .config = G6_SCU_PCIE_CONFIG, - .endpoints = ast2600_pcie_bridges, - .regions = ast2500_p2a_regions, + .misc = G6_SCU_MISC, + .config = G6_SCU_PCIE_CONFIG, + .endpoints = ast2600_pcie_bridges, + .regions = ast2500_p2a_regions, }; static const struct soc_device_id pciectl_matches[] = { - { .compatible = "aspeed,ast2400-pcie-device-controller", .data = &ast2400_pdata }, - { .compatible = "aspeed,ast2500-pcie-device-controller", .data = &ast2500_pdata }, - { .compatible = "aspeed,ast2600-pcie-device-controller", .data = &ast2600_pdata }, - { }, + { .compatible = "aspeed,ast2400-pcie-device-controller", + .data = &ast2400_pdata }, + { .compatible = "aspeed,ast2500-pcie-device-controller", + .data = &ast2500_pdata }, + { .compatible = "aspeed,ast2600-pcie-device-controller", + .data = &ast2600_pdata }, + {}, }; static int pciectl_driver_init(struct soc *soc, struct soc_device *dev) { - struct pciectl *ctx; - int rc; - - ctx = calloc(1, sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } - - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->scu)) < 0) { - goto cleanup_ctx; - } - - if (!(ctx->sdmc = sdmc_get(soc))) { - loge("Failed to acquire SDMC controller\n"); - goto cleanup_ctx; - } - - ctx->soc = soc; - ctx->dev = dev; - ctx->pdata = soc_device_get_match_data(soc, pciectl_matches, &dev->node); - if (ctx->pdata == &ast2600_pdata) { - ctx->bridges = bridges_get_by_device(soc, dev); - if (!ctx->bridges) { - loge("Failed to aquire bridge controller\n"); - goto cleanup_ctx; - } - } - - soc_device_set_drvdata(dev, ctx); - - if ((rc = soc_bridge_controller_register(soc, p2actl_as_bridgectl(ctx), &p2actl_ops)) < 0) { - goto cleanup_drvdata; - } - - if ((rc = soc_bridge_controller_register(soc, xdmactl_as_bridgectl(ctx), &xdmactl_ops)) < 0) { - goto cleanup_drvdata; - } - - return 0; + struct pciectl *ctx; + int rc; + + ctx = calloc(1, sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } + + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->scu)) < 0) { + goto cleanup_ctx; + } + + if (!(ctx->sdmc = sdmc_get(soc))) { + loge("Failed to acquire SDMC controller\n"); + goto cleanup_ctx; + } + + ctx->soc = soc; + ctx->dev = dev; + ctx->pdata = + soc_device_get_match_data(soc, pciectl_matches, &dev->node); + if (ctx->pdata == &ast2600_pdata) { + ctx->bridges = bridges_get_by_device(soc, dev); + if (!ctx->bridges) { + loge("Failed to aquire bridge controller\n"); + goto cleanup_ctx; + } + } + + soc_device_set_drvdata(dev, ctx); + + if ((rc = soc_bridge_controller_register(soc, p2actl_as_bridgectl(ctx), + &p2actl_ops)) < 0) { + goto cleanup_drvdata; + } + + if ((rc = soc_bridge_controller_register(soc, xdmactl_as_bridgectl(ctx), + &xdmactl_ops)) < 0) { + goto cleanup_drvdata; + } + + return 0; cleanup_drvdata: - soc_device_set_drvdata(dev, NULL); + soc_device_set_drvdata(dev, NULL); cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void pciectl_driver_destroy(struct soc_device *dev) { - struct pciectl *ctx = soc_device_get_drvdata(dev); + struct pciectl *ctx = soc_device_get_drvdata(dev); - soc_bridge_controller_unregister(ctx->soc, p2actl_as_bridgectl(ctx)); + soc_bridge_controller_unregister(ctx->soc, p2actl_as_bridgectl(ctx)); - free(ctx); + free(ctx); } static const struct soc_driver pciectl_driver = { - .name = "pciectl", - .matches = pciectl_matches, - .init = pciectl_driver_init, - .destroy = pciectl_driver_destroy, + .name = "pciectl", + .matches = pciectl_matches, + .init = pciectl_driver_init, + .destroy = pciectl_driver_destroy, }; REGISTER_SOC_DRIVER(pciectl_driver); diff --git a/src/soc/scu.c b/src/soc/scu.c index 0659cc7..52c27f7 100644 --- a/src/soc/scu.c +++ b/src/soc/scu.c @@ -4,8 +4,8 @@ #include "scu.h" -#define AST_SCU_PROT_KEY 0x000 -#define AST_SCU_PASSWORD 0x1688a8a8 +#define AST_SCU_PROT_KEY 0x000 +#define AST_SCU_PASSWORD 0x1688a8a8 struct scu { int refcnt; @@ -90,7 +90,7 @@ static const struct soc_device_id scu_match[] = { { .compatible = "aspeed,ast2400-scu" }, { .compatible = "aspeed,ast2500-scu" }, { .compatible = "aspeed,ast2600-scu" }, - { }, + {}, }; static const struct soc_driver scu_driver = { diff --git a/src/soc/sdmc.c b/src/soc/sdmc.c index ee90f48..dcaa7fe 100644 --- a/src/soc/sdmc.c +++ b/src/soc/sdmc.c @@ -6,13 +6,13 @@ #include -#define MCR_CONFIG 0x04 -#define MCR_GMP 0x08 +#define MCR_CONFIG 0x04 +#define MCR_GMP 0x08 struct sdmc_pdata { - const uint32_t (*dram_sizes)[4]; - const uint32_t (*vram_sizes)[4]; - const uint32_t gmp_xdma_mask; + const uint32_t (*dram_sizes)[4]; + const uint32_t (*vram_sizes)[4]; + const uint32_t gmp_xdma_mask; }; struct sdmc { @@ -23,198 +23,193 @@ struct sdmc { }; static const uint32_t ast_vram_sizes[4] = { - [0b00] = 8 << 20, - [0b01] = 16 << 20, - [0b10] = 32 << 20, - [0b11] = 64 << 20, + [0b00] = 8 << 20, + [0b01] = 16 << 20, + [0b10] = 32 << 20, + [0b11] = 64 << 20, }; static const uint32_t ast2400_dram_sizes[4] = { - [0b00] = 64 << 20, - [0b01] = 128 << 20, - [0b10] = 256 << 20, - [0b11] = 512 << 20, + [0b00] = 64 << 20, + [0b01] = 128 << 20, + [0b10] = 256 << 20, + [0b11] = 512 << 20, }; static const struct sdmc_pdata ast2400_sdmc_pdata = { - .dram_sizes = &ast2400_dram_sizes, - .vram_sizes = &ast_vram_sizes, - .gmp_xdma_mask = BIT(16), + .dram_sizes = &ast2400_dram_sizes, + .vram_sizes = &ast_vram_sizes, + .gmp_xdma_mask = BIT(16), }; static const uint32_t ast2500_dram_sizes[4] = { - [0b00] = 128 << 20, - [0b01] = 256 << 20, - [0b10] = 512 << 20, - [0b11] = 1024 << 20, + [0b00] = 128 << 20, + [0b01] = 256 << 20, + [0b10] = 512 << 20, + [0b11] = 1024 << 20, }; static const struct sdmc_pdata ast2500_sdmc_pdata = { - .dram_sizes = &ast2500_dram_sizes, - .vram_sizes = &ast_vram_sizes, - .gmp_xdma_mask = BIT(17), + .dram_sizes = &ast2500_dram_sizes, + .vram_sizes = &ast_vram_sizes, + .gmp_xdma_mask = BIT(17), }; static const uint32_t ast2600_dram_sizes[4] = { - [0b00] = 256 << 20, - [0b01] = 512 << 20, - [0b10] = 1024 << 20, - [0b11] = 2048 << 20, + [0b00] = 256 << 20, + [0b01] = 512 << 20, + [0b10] = 1024 << 20, + [0b11] = 2048 << 20, }; static const struct sdmc_pdata ast2600_sdmc_pdata = { - .dram_sizes = &ast2600_dram_sizes, - .vram_sizes = &ast_vram_sizes, - .gmp_xdma_mask = BIT(18) | BIT(25), + .dram_sizes = &ast2600_dram_sizes, + .vram_sizes = &ast_vram_sizes, + .gmp_xdma_mask = BIT(18) | BIT(25), }; static int sdmc_readl(struct sdmc *ctx, uint32_t off, uint32_t *val) { - return soc_readl(ctx->soc, ctx->iomem.start + off, val); + return soc_readl(ctx->soc, ctx->iomem.start + off, val); } static int sdmc_writel(struct sdmc *ctx, uint32_t off, uint32_t val) { - return soc_writel(ctx->soc, ctx->iomem.start + off, val); + return soc_writel(ctx->soc, ctx->iomem.start + off, val); } static void sdmc_dram_region(struct sdmc *ctx, uint32_t mcr_conf, - struct soc_region *dram) + struct soc_region *dram) { - dram->start = ctx->dram.start; - dram->length = (*ctx->pdata->dram_sizes)[mcr_conf & 3]; + dram->start = ctx->dram.start; + dram->length = (*ctx->pdata->dram_sizes)[mcr_conf & 3]; } int sdmc_get_dram(struct sdmc *ctx, struct soc_region *dram) { - uint32_t mcr_conf; - int rc; + uint32_t mcr_conf; + int rc; - if ((rc = sdmc_readl(ctx, MCR_CONFIG, &mcr_conf)) < 0) - return rc; + if ((rc = sdmc_readl(ctx, MCR_CONFIG, &mcr_conf)) < 0) + return rc; - sdmc_dram_region(ctx, mcr_conf, dram); + sdmc_dram_region(ctx, mcr_conf, dram); - return 0; + return 0; } int sdmc_get_vram(struct sdmc *ctx, struct soc_region *vram) { - struct soc_region dram; - uint32_t mcr_conf; - int rc; + struct soc_region dram; + uint32_t mcr_conf; + int rc; - if ((rc = sdmc_readl(ctx, MCR_CONFIG, &mcr_conf)) < 0) - return rc; + if ((rc = sdmc_readl(ctx, MCR_CONFIG, &mcr_conf)) < 0) + return rc; - sdmc_dram_region(ctx, mcr_conf, &dram); + sdmc_dram_region(ctx, mcr_conf, &dram); - vram->length = (*ctx->pdata->vram_sizes)[(mcr_conf >> 2) & 3]; - vram->start = dram.start + dram.length - vram->length; + vram->length = (*ctx->pdata->vram_sizes)[(mcr_conf >> 2) & 3]; + vram->start = dram.start + dram.length - vram->length; - return 0; + return 0; } int sdmc_constrains_xdma(struct sdmc *ctx) { - uint32_t mcr_gmp; - int rc; + uint32_t mcr_gmp; + int rc; - if ((rc = sdmc_readl(ctx, MCR_GMP, &mcr_gmp)) < 0) - return rc; + if ((rc = sdmc_readl(ctx, MCR_GMP, &mcr_gmp)) < 0) + return rc; - return !!(mcr_gmp & ctx->pdata->gmp_xdma_mask); + return !!(mcr_gmp & ctx->pdata->gmp_xdma_mask); } int sdmc_configure_xdma(struct sdmc *ctx, bool constrain) { - uint32_t mcr_gmp; - int rc; + uint32_t mcr_gmp; + int rc; - if ((rc = sdmc_readl(ctx, MCR_GMP, &mcr_gmp)) < 0) { - return rc; - } + if ((rc = sdmc_readl(ctx, MCR_GMP, &mcr_gmp)) < 0) { + return rc; + } - mcr_gmp &= ~ctx->pdata->gmp_xdma_mask; - mcr_gmp |= (ctx->pdata->gmp_xdma_mask * constrain); + mcr_gmp &= ~ctx->pdata->gmp_xdma_mask; + mcr_gmp |= (ctx->pdata->gmp_xdma_mask * constrain); - if ((rc = sdmc_writel(ctx, MCR_GMP, mcr_gmp)) < 0) { - return rc; - } + if ((rc = sdmc_writel(ctx, MCR_GMP, mcr_gmp)) < 0) { + return rc; + } - return 0; + return 0; } static const struct soc_device_id sdmc_match[] = { - { - .compatible = "aspeed,ast2400-sdram-controller", - .data = &ast2400_sdmc_pdata - }, - { - .compatible = "aspeed,ast2500-sdram-controller", - .data = &ast2500_sdmc_pdata - }, - { - .compatible = "aspeed,ast2600-sdram-controller", - .data = &ast2600_sdmc_pdata - }, - { }, + { .compatible = "aspeed,ast2400-sdram-controller", + .data = &ast2400_sdmc_pdata }, + { .compatible = "aspeed,ast2500-sdram-controller", + .data = &ast2500_sdmc_pdata }, + { .compatible = "aspeed,ast2600-sdram-controller", + .data = &ast2600_sdmc_pdata }, + {}, }; static int sdmc_driver_init(struct soc *soc, struct soc_device *dev) { - struct soc_device_node dn; - struct sdmc *ctx; - int rc; + struct soc_device_node dn; + struct sdmc *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { + goto cleanup_ctx; + } - if (!(ctx->pdata = soc_device_get_match_data(soc, sdmc_match, &dev->node))) { - rc = -EINVAL; - goto cleanup_ctx; - } + if (!(ctx->pdata = + soc_device_get_match_data(soc, sdmc_match, &dev->node))) { + rc = -EINVAL; + goto cleanup_ctx; + } - if ((rc = soc_device_from_type(soc, "memory", &dn))) { - goto cleanup_ctx; - } + if ((rc = soc_device_from_type(soc, "memory", &dn))) { + goto cleanup_ctx; + } - if ((rc = soc_device_get_memory(soc, &dn, &ctx->dram)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dn, &ctx->dram)) < 0) { + goto cleanup_ctx; + } - ctx->soc = soc; + ctx->soc = soc; - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void sdmc_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } static const struct soc_driver sdmc_driver = { - .name = "sdmc", - .matches = sdmc_match, - .init = sdmc_driver_init, - .destroy = sdmc_driver_destroy, + .name = "sdmc", + .matches = sdmc_match, + .init = sdmc_driver_init, + .destroy = sdmc_driver_destroy, }; REGISTER_SOC_DRIVER(sdmc_driver); struct sdmc *sdmc_get(struct soc *soc) { - return soc_driver_get_drvdata(soc, &sdmc_driver); + return soc_driver_get_drvdata(soc, &sdmc_driver); } diff --git a/src/soc/sfc.c b/src/soc/sfc.c index 2c15994..707d19e 100644 --- a/src/soc/sfc.c +++ b/src/soc/sfc.c @@ -25,1053 +25,1101 @@ #define __unused __attribute__((unused)) #endif -#define CALIBRATE_BUF_SIZE 16384 +#define CALIBRATE_BUF_SIZE 16384 #define SFC_ERR(fmt, ...) loge(fmt, ##__VA_ARGS__) #define SFC_INF(fmt, ...) logi(fmt, ##__VA_ARGS__) #define SFC_DBG(fmt, ...) logd(fmt, ##__VA_ARGS__) /* Firmware Flash Memory Controller */ -#define FMC_CE_TYPE 0x00 -#define FMC_CE_TYPE_CE2_WP BIT(18) -#define FMC_CE_TYPE_CE1_WP BIT(17) -#define FMC_CE_TYPE_CE0_WP BIT(16) -#define FMC_CE_CTRL 0x04 -#define FMC_CE0_CTRL 0x10 -#define FMC_TIMING 0x94 +#define FMC_CE_TYPE 0x00 +#define FMC_CE_TYPE_CE2_WP BIT(18) +#define FMC_CE_TYPE_CE1_WP BIT(17) +#define FMC_CE_TYPE_CE0_WP BIT(16) +#define FMC_CE_CTRL 0x04 +#define FMC_CE0_CTRL 0x10 +#define FMC_TIMING 0x94 /* SPI (Host) Flash Memory Controller */ -#define SMC_CONF 0x00 -#define SMC_CE0_CTRL 0x10 -#define SMC_TIMING 0x94 +#define SMC_CONF 0x00 +#define SMC_CE0_CTRL 0x10 +#define SMC_TIMING 0x94 struct sfc_data { - struct soc *soc; - struct soc_region iomem; - struct soc_region flash; - struct clk *clk; + struct soc *soc; + struct soc_region iomem; + struct soc_region flash; + struct clk *clk; - /* We have 2 controllers, one for the BMC flash, one for the PNOR */ - uint8_t type; + /* We have 2 controllers, one for the BMC flash, one for the PNOR */ + uint8_t type; - uint32_t type_reg; - uint32_t type_wp_mask; + uint32_t type_reg; + uint32_t type_wp_mask; - /* Address and previous value of the ctrl register */ - uint32_t ctl_reg; + /* Address and previous value of the ctrl register */ + uint32_t ctl_reg; - /* Control register value for normal commands */ - uint32_t ctl_val; + /* Control register value for normal commands */ + uint32_t ctl_val; - /* Control register value for (fast) reads */ - uint32_t ctl_read_val; + /* Control register value for (fast) reads */ + uint32_t ctl_read_val; - /* Flash read timing register */ - uint32_t fread_timing_reg; - uint32_t fread_timing_val; + /* Flash read timing register */ + uint32_t fread_timing_reg; + uint32_t fread_timing_val; - /* Current 4b mode */ - bool mode_4b; + /* Current 4b mode */ + bool mode_4b; - /* Callbacks */ - struct sfc ops; + /* Callbacks */ + struct sfc ops; }; -static inline int -sfc_readl(struct sfc_data *ctx, uint32_t offset, uint32_t *val) +static inline int sfc_readl(struct sfc_data *ctx, uint32_t offset, + uint32_t *val) { - return soc_readl(ctx->soc, ctx->iomem.start + offset, val); + return soc_readl(ctx->soc, ctx->iomem.start + offset, val); } -static inline int -sfc_writel(struct sfc_data *ctx, uint32_t offset, uint32_t val) +static inline int sfc_writel(struct sfc_data *ctx, uint32_t offset, + uint32_t val) { - return soc_writel(ctx->soc, ctx->iomem.start + offset, val); + return soc_writel(ctx->soc, ctx->iomem.start + offset, val); } -static inline ssize_t -flash_read(struct sfc_data *ctx, uint32_t offset, void *buf, size_t len) +static inline ssize_t flash_read(struct sfc_data *ctx, uint32_t offset, + void *buf, size_t len) { - return soc_read(ctx->soc, ctx->flash.start + offset, buf, len); + return soc_read(ctx->soc, ctx->flash.start + offset, buf, len); } -static inline ssize_t -flash_write(struct sfc_data *ctx, uint32_t offset, const void *buf, size_t len) +static inline ssize_t flash_write(struct sfc_data *ctx, uint32_t offset, + const void *buf, size_t len) { - return soc_write(ctx->soc, ctx->flash.start + offset, buf, len); + return soc_write(ctx->soc, ctx->flash.start + offset, buf, len); } static uint32_t ast_ahb_freq; static const uint32_t ast_ct_hclk_divs[] = { - 0xf, /* HCLK */ - 0x7, /* HCLK/2 */ - 0xe, /* HCLK/3 */ - 0x6, /* HCLK/4 */ - 0xd, /* HCLK/5 */ + 0xf, /* HCLK */ + 0x7, /* HCLK/2 */ + 0xe, /* HCLK/3 */ + 0x6, /* HCLK/4 */ + 0xd, /* HCLK/5 */ }; static void fl_micron_status(struct sfc *ct) { - uint8_t flst; - - /* - * After a success status on a write or erase, we - * need to do that command or some chip variants will - * lock - */ - ct->cmd_rd(ct, CMD_MIC_RDFLST, false, 0, &flst, 1); + uint8_t flst; + + /* + * After a success status on a write or erase, we + * need to do that command or some chip variants will + * lock + */ + ct->cmd_rd(ct, CMD_MIC_RDFLST, false, 0, &flst, 1); } static int fl_read_stat(struct sfc *ct, uint8_t *stat) { - return ct->cmd_rd(ct, CMD_RDSR, false, 0, stat, 1); + return ct->cmd_rd(ct, CMD_RDSR, false, 0, stat, 1); } /* Synchronous write completion, probably need a yield hook */ static int fl_sync_wait_idle(struct sfc *ct) { - uint8_t stat; - int rc; - - /* XXX Add timeout */ - for (;;) { - rc = fl_read_stat(ct, &stat); - if (rc) return rc; - if (!(stat & STAT_WIP)) { - if (ct->finfo->flags & FL_MICRON_BUGS) - fl_micron_status(ct); - return 0; + uint8_t stat; + int rc; + + /* XXX Add timeout */ + for (;;) { + rc = fl_read_stat(ct, &stat); + if (rc) + return rc; + if (!(stat & STAT_WIP)) { + if (ct->finfo->flags & FL_MICRON_BUGS) + fl_micron_status(ct); + return 0; + } + usleep(100); } - usleep(100); - } - /* return FLASH_ERR_WIP_TIMEOUT; */ + /* return FLASH_ERR_WIP_TIMEOUT; */ } /* Exported for internal use */ static int fl_wren(struct sfc *ct) { - int i, rc; - uint8_t stat; - - /* Some flashes need it to be hammered */ - for (i = 0; i < 1000; i++) { - rc = ct->cmd_wr(ct, CMD_WREN, false, 0, NULL, 0); - if (rc) return rc; - rc = fl_read_stat(ct, &stat); - if (rc) return rc; - if (stat & STAT_WIP) { - SFC_ERR("LIBFLASH: WREN has WIP status set !\n"); - rc = fl_sync_wait_idle(ct); - if (rc) - return rc; - continue; + int i, rc; + uint8_t stat; + + /* Some flashes need it to be hammered */ + for (i = 0; i < 1000; i++) { + rc = ct->cmd_wr(ct, CMD_WREN, false, 0, NULL, 0); + if (rc) + return rc; + rc = fl_read_stat(ct, &stat); + if (rc) + return rc; + if (stat & STAT_WIP) { + SFC_ERR("LIBFLASH: WREN has WIP status set !\n"); + rc = fl_sync_wait_idle(ct); + if (rc) + return rc; + continue; + } + if (stat & STAT_WEN) + return 0; } - if (stat & STAT_WEN) - return 0; - } - return -ETIMEDOUT; + return -ETIMEDOUT; } static int sfc_start_cmd(struct sfc_data *ct, uint8_t cmd) { - int rc; + int rc; - /* Switch to user mode, CE# dropped */ - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_val | 7); - if (rc < 0) - return rc; + /* Switch to user mode, CE# dropped */ + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_val | 7); + if (rc < 0) + return rc; - /* user mode, CE# active */ - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_val | 3); - if (rc < 0) - return rc; + /* user mode, CE# active */ + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_val | 3); + if (rc < 0) + return rc; - /* write cmd */ - rc = flash_write(ct, 0, &cmd, 1); + /* write cmd */ + rc = flash_write(ct, 0, &cmd, 1); - return rc == 1 ? 0 : rc; + return rc == 1 ? 0 : rc; } static void sfc_end_cmd(struct sfc_data *ct) { - int rc; + int rc; - /* clear CE# */ - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_val | 7); - if (rc < 0) { errno = -rc; perror("sfc_writel"); return; } + /* clear CE# */ + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_val | 7); + if (rc < 0) { + errno = -rc; + perror("sfc_writel"); + return; + } - /* Switch back to read mode */ - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); - if (rc < 0) { errno = -rc; perror("sfc_writel"); return; } + /* Switch back to read mode */ + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + if (rc < 0) { + errno = -rc; + perror("sfc_writel"); + return; + } } static int sfc_send_addr(struct sfc_data *ct, uint32_t addr) { - const void *ap; - int rc, len; + const void *ap; + int rc, len; - /* Layout address MSB first in memory */ - addr = htobe32(addr); + /* Layout address MSB first in memory */ + addr = htobe32(addr); - /* Send the right amount of bytes */ - ap = (char *)&addr; + /* Send the right amount of bytes */ + ap = (char *)&addr; - if (ct->mode_4b) { - len = 4; - rc = flash_write(ct, 0, ap, len); - } else { - len = 3; - rc = flash_write(ct, 0, ap + 1, len); - } + if (ct->mode_4b) { + len = 4; + rc = flash_write(ct, 0, ap, len); + } else { + len = 3; + rc = flash_write(ct, 0, ap + 1, len); + } - return rc == len ? 0 : rc; + return rc == len ? 0 : rc; } -static int sfc_cmd_rd(struct sfc *ctrl, uint8_t cmd, - bool has_addr, uint32_t addr, void *buffer, - uint32_t size) +static int sfc_cmd_rd(struct sfc *ctrl, uint8_t cmd, bool has_addr, + uint32_t addr, void *buffer, uint32_t size) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - int rc; - - rc = sfc_start_cmd(ct, cmd); - if (rc) - goto bail; - if (has_addr) { - rc = sfc_send_addr(ct, addr); + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + int rc; + + rc = sfc_start_cmd(ct, cmd); if (rc) - goto bail; - } - if (buffer && size) { - uint32_t i = 0; - - /* - * Some bridges (P2A and debug UART, probably others too) have a quirk - * where they'll generate 4 byte reads even when a 1/2 byte read is - * requested. When the SFC is in user mode it'll clock out one byte for each - * byte of the MMIO read/write size as a result if we use anything smaller than - * a 4 byte read we'll lose data. The easiest solution is to just use 4 byte reads - * for everything and extract the bytes manually when needed. - * - * Writes don't have this problem, thankfully. - */ - while (size) { - uint8_t *buf = buffer; - uint32_t val = 0; - - rc = soc_readl(ct->soc, ct->flash.start, &val); - if (rc) - goto bail; - - if (size) { buf[i++] = (val >> 0) & 0xff; size--; } - if (size) { buf[i++] = (val >> 8) & 0xff; size--; } - if (size) { buf[i++] = (val >> 16) & 0xff; size--; } - if (size) { buf[i++] = (val >> 24) & 0xff; size--; } - } - rc = 0; - } + goto bail; + if (has_addr) { + rc = sfc_send_addr(ct, addr); + if (rc) + goto bail; + } + if (buffer && size) { + uint32_t i = 0; + + /* + * Some bridges (P2A and debug UART, probably others too) have a + * quirk where they'll generate 4 byte reads even when a + * 1/2 byte read is requested. When the SFC is in user mode + * it'll clock out one byte for each byte of the MMIO read/write + * size as a result if we use anything smaller than a 4 byte + * read we'll lose data. The easiest solution is to just use 4 + * byte reads for everything and extract the bytes manually + * when needed. + * + * Writes don't have this problem, thankfully. + */ + while (size) { + uint8_t *buf = buffer; + uint32_t val = 0; + + rc = soc_readl(ct->soc, ct->flash.start, &val); + if (rc) + goto bail; + + if (size) { + buf[i++] = (val >> 0) & 0xff; + size--; + } + if (size) { + buf[i++] = (val >> 8) & 0xff; + size--; + } + if (size) { + buf[i++] = (val >> 16) & 0xff; + size--; + } + if (size) { + buf[i++] = (val >> 24) & 0xff; + size--; + } + } + rc = 0; + } bail: - sfc_end_cmd(ct); + sfc_end_cmd(ct); - return rc; + return rc; } -static int sfc_cmd_wr(struct sfc *ctrl, uint8_t cmd, - bool has_addr, uint32_t addr, const void *buffer, - uint32_t size) +static int sfc_cmd_wr(struct sfc *ctrl, uint8_t cmd, bool has_addr, + uint32_t addr, const void *buffer, uint32_t size) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - ssize_t rc; + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + ssize_t rc; - rc = sfc_start_cmd(ct, cmd); - if (rc) - goto bail; - - if (has_addr) { - rc = sfc_send_addr(ct, addr); + rc = sfc_start_cmd(ct, cmd); if (rc) - goto bail; - } - if (buffer && size) - rc = flash_write(ct, 0, buffer, size); + goto bail; + + if (has_addr) { + rc = sfc_send_addr(ct, addr); + if (rc) + goto bail; + } + if (buffer && size) + rc = flash_write(ct, 0, buffer, size); bail: - sfc_end_cmd(ct); + sfc_end_cmd(ct); - return rc == (ssize_t)size ? 0 : rc; + return rc == (ssize_t)size ? 0 : rc; } static int sfc_set_4b(struct sfc *ctrl, bool enable) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - uint32_t ce_ctrl = 0; - int rc; + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + uint32_t ce_ctrl = 0; + int rc; + + if (ct->type == SFC_TYPE_FMC && ct->ops.finfo->size > 0x1000000) { + rc = sfc_readl(ct, FMC_CE_CTRL, &ce_ctrl); + if (rc < 0) + return rc; + } else if (ct->type != SFC_TYPE_SMC) + return enable ? -EIO : 0; + + /* + * We update the "old" value as well since when quitting + * we don't restore the mode of the flash itself so we need + * to leave the controller in a compatible setup + */ + if (enable) { + ct->ctl_val |= 0x2000; + ct->ctl_read_val |= 0x2000; + ce_ctrl |= 0x1; + } else { + ct->ctl_val &= ~0x2000; + ct->ctl_read_val &= ~0x2000; + ce_ctrl &= ~0x1; + } + ct->mode_4b = enable; - if (ct->type == SFC_TYPE_FMC && ct->ops.finfo->size > 0x1000000) { - rc = sfc_readl(ct, FMC_CE_CTRL, &ce_ctrl); + /* Update read mode */ + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); if (rc < 0) - return rc; - } else if (ct->type != SFC_TYPE_SMC) - return enable ? -EIO : 0; - - /* - * We update the "old" value as well since when quitting - * we don't restore the mode of the flash itself so we need - * to leave the controller in a compatible setup - */ - if (enable) { - ct->ctl_val |= 0x2000; - ct->ctl_read_val |= 0x2000; - ce_ctrl |= 0x1; - } else { - ct->ctl_val &= ~0x2000; - ct->ctl_read_val &= ~0x2000; - ce_ctrl &= ~0x1; - } - ct->mode_4b = enable; - - /* Update read mode */ - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); - if (rc < 0) - return rc; + return rc; - if (ce_ctrl && ct->type == SFC_TYPE_FMC) - return sfc_writel(ct, FMC_CE_CTRL, ce_ctrl); + if (ce_ctrl && ct->type == SFC_TYPE_FMC) + return sfc_writel(ct, FMC_CE_CTRL, ce_ctrl); - return 0; + return 0; } -static int sfc_direct_read(struct sfc *ctrl, uint32_t pos, void *buf, uint32_t len) +static int sfc_direct_read(struct sfc *ctrl, uint32_t pos, void *buf, + uint32_t len) { - ssize_t rc; + ssize_t rc; - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - /* - * We are in read mode by default. We don't yet support fancy - * things like fast read or X2 mode - */ - if ((rc = flash_read(ct, pos, buf, len)) < 0) - return rc; + /* + * We are in read mode by default. We don't yet support fancy + * things like fast read or X2 mode + */ + if ((rc = flash_read(ct, pos, buf, len)) < 0) + return rc; - return (uint32_t)rc == len ? 0 : rc; + return (uint32_t)rc == len ? 0 : rc; } static void ast2500_get_ahb_freq(struct sfc_data *ct) { - if (ast_ahb_freq) - return; + if (ast_ahb_freq) + return; - ast_ahb_freq = clk_get_rate(ct->clk, clk_ahb); + ast_ahb_freq = clk_get_rate(ct->clk, clk_ahb); } -static int sfc_check_reads(struct sfc_data *ct, - const uint8_t *golden_buf, uint8_t *test_buf) +static int sfc_check_reads(struct sfc_data *ct, const uint8_t *golden_buf, + uint8_t *test_buf) { - int i, rc; - - for (i = 0; i < 10; i++) { - rc = flash_read(ct, 0, test_buf, CALIBRATE_BUF_SIZE); - if (rc) - return rc; - if (memcmp(test_buf, golden_buf, CALIBRATE_BUF_SIZE) != 0) - return -EREMOTEIO; - } - return 0; + int i, rc; + + for (i = 0; i < 10; i++) { + rc = flash_read(ct, 0, test_buf, CALIBRATE_BUF_SIZE); + if (rc) + return rc; + if (memcmp(test_buf, golden_buf, CALIBRATE_BUF_SIZE) != 0) + return -EREMOTEIO; + } + return 0; } static int sfc_calibrate_reads(struct sfc_data *ct, uint32_t hdiv, - const uint8_t *golden_buf, uint8_t *test_buf) + const uint8_t *golden_buf, uint8_t *test_buf) { - int i, rc; - int good_pass = -1, pass_count = 0; - uint32_t shift = (hdiv - 1) << 2; - uint32_t mask = ~(0xfu << shift); - -#define FREAD_TPASS(i) (((i) / 2) | (((i) & 1) ? 0 : 8)) + int i, rc; + int good_pass = -1, pass_count = 0; + uint32_t shift = (hdiv - 1) << 2; + uint32_t mask = ~(0xfu << shift); + +#define FREAD_TPASS(i) (((i) / 2) | (((i) & 1) ? 0 : 8)) + + /* Try HCLK delay 0..5, each one with/without delay and look for a + * good pair. + */ + for (i = 0; i < 12; i++) { + bool pass; + + ct->fread_timing_val &= mask; + ct->fread_timing_val |= FREAD_TPASS(i) << shift; + rc = sfc_writel(ct, ct->fread_timing_reg, ct->fread_timing_val); + if (rc < 0) + return rc; + + rc = sfc_check_reads(ct, golden_buf, test_buf); + if (rc && rc != -EREMOTEIO) + return rc; + pass = (rc == 0); + SFC_DBG(" * [%08x] %d HCLK delay, %dns DI delay : %s\n", + ct->fread_timing_val, i / 2, (i & 1) ? 0 : 4, + pass ? "PASS" : "FAIL"); + if (pass) { + pass_count++; + if (pass_count == 3) { + good_pass = i - 1; + break; + } + } else + pass_count = 0; + } - /* Try HCLK delay 0..5, each one with/without delay and look for a - * good pair. - */ - for (i = 0; i < 12; i++) { - bool pass; + /* No good setting for this frequency */ + if (good_pass < 0) + return -EREMOTEIO; + /* We have at least one pass of margin, let's use first pass */ ct->fread_timing_val &= mask; - ct->fread_timing_val |= FREAD_TPASS(i) << shift; + ct->fread_timing_val |= FREAD_TPASS(good_pass) << shift; rc = sfc_writel(ct, ct->fread_timing_reg, ct->fread_timing_val); if (rc < 0) - return rc; - - rc = sfc_check_reads(ct, golden_buf, test_buf); - if (rc && rc != -EREMOTEIO) - return rc; - pass = (rc == 0); - SFC_DBG(" * [%08x] %d HCLK delay, %dns DI delay : %s\n", - ct->fread_timing_val, i/2, (i & 1) ? 0 : 4, pass ? "PASS" : "FAIL"); - if (pass) { - pass_count++; - if (pass_count == 3) { - good_pass = i - 1; - break; - } - } else - pass_count = 0; - } - - /* No good setting for this frequency */ - if (good_pass < 0) - return -EREMOTEIO; - - /* We have at least one pass of margin, let's use first pass */ - ct->fread_timing_val &= mask; - ct->fread_timing_val |= FREAD_TPASS(good_pass) << shift; - rc = sfc_writel(ct, ct->fread_timing_reg, ct->fread_timing_val); - if (rc < 0) - return rc; + return rc; - SFC_DBG("AST: * -> good is pass %d [0x%08x]\n", - good_pass, ct->fread_timing_val); - return 0; + SFC_DBG("AST: * -> good is pass %d [0x%08x]\n", good_pass, + ct->fread_timing_val); + return 0; } static bool ast_calib_data_usable(const uint8_t *test_buf, uint32_t size) { - const uint32_t *tb32 = (const uint32_t *)test_buf; - uint32_t i, cnt = 0; - - /* We check if we have enough words that are neither all 0 - * nor all 1's so the calibration can be considered valid. - * - * I use an arbitrary threshold for now of 64 - */ - size >>= 2; - for (i = 0; i < size; i++) { - if (tb32[i] != 0 && tb32[i] != 0xffffffff) - cnt++; - } - return cnt >= 64; + const uint32_t *tb32 = (const uint32_t *)test_buf; + uint32_t i, cnt = 0; + + /* We check if we have enough words that are neither all 0 + * nor all 1's so the calibration can be considered valid. + * + * I use an arbitrary threshold for now of 64 + */ + size >>= 2; + for (i = 0; i < size; i++) { + if (tb32[i] != 0 && tb32[i] != 0xffffffff) + cnt++; + } + return cnt >= 64; } static int sfc_optimize_reads(struct sfc_data *ct, - struct flash_info *info __unused, - uint32_t max_freq) + struct flash_info *info __unused, + uint32_t max_freq) { - uint8_t *golden_buf, *test_buf; - int i, rc, best_div = -1; - uint32_t save_read_val = ct->ctl_read_val; - - test_buf = malloc(CALIBRATE_BUF_SIZE * 2); - golden_buf = test_buf + CALIBRATE_BUF_SIZE; - - /* We start with the dumbest setting and read some data */ - ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | - (0x00 << 28) | /* Single bit */ - (0x00 << 24) | /* CE# max */ - (0x03 << 16) | /* use normal reads */ - (0x00 << 8) | /* HCLK/16 */ - (0x00 << 6) | /* no dummy cycle */ - (0x00); /* normal read */ - sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); - - rc = flash_read(ct, 0, golden_buf, CALIBRATE_BUF_SIZE); - if (rc) { - free(test_buf); - return rc; - } - - /* Establish our read mode with freq field set to 0 */ - ct->ctl_read_val = save_read_val & 0xfffff0ff; - - /* Check if calibration data is suitable */ - if (!ast_calib_data_usable(golden_buf, CALIBRATE_BUF_SIZE)) { - SFC_DBG("AST: Calibration area too uniform, " - "using low speed\n"); - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); - free(test_buf); - return rc; - } + uint8_t *golden_buf, *test_buf; + int i, rc, best_div = -1; + uint32_t save_read_val = ct->ctl_read_val; + + test_buf = malloc(CALIBRATE_BUF_SIZE * 2); + golden_buf = test_buf + CALIBRATE_BUF_SIZE; + + /* We start with the dumbest setting and read some data */ + ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | + (0x00 << 28) | /* Single bit */ + (0x00 << 24) | /* CE# max */ + (0x03 << 16) | /* use normal reads */ + (0x00 << 8) | /* HCLK/16 */ + (0x00 << 6) | /* no dummy cycle */ + (0x00); /* normal read */ + sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + + rc = flash_read(ct, 0, golden_buf, CALIBRATE_BUF_SIZE); + if (rc) { + free(test_buf); + return rc; + } - /* Now we iterate the HCLK dividers until we find our breaking point */ - for (i = 5; i > 0; i--) { - uint32_t tv, freq; + /* Establish our read mode with freq field set to 0 */ + ct->ctl_read_val = save_read_val & 0xfffff0ff; - /* Compare timing to max */ - freq = ast_ahb_freq / i; - if (freq >= max_freq) - continue; + /* Check if calibration data is suitable */ + if (!ast_calib_data_usable(golden_buf, CALIBRATE_BUF_SIZE)) { + SFC_DBG("AST: Calibration area too uniform, " + "using low speed\n"); + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + free(test_buf); + return rc; + } - /* Set the timing */ - tv = ct->ctl_read_val | (ast_ct_hclk_divs[i - 1] << 8); - rc = sfc_writel(ct, ct->ctl_reg, tv); - if (rc < 0) { - free(test_buf); - return rc; + /* Now we iterate the HCLK dividers until we find our breaking point */ + for (i = 5; i > 0; i--) { + uint32_t tv, freq; + + /* Compare timing to max */ + freq = ast_ahb_freq / i; + if (freq >= max_freq) + continue; + + /* Set the timing */ + tv = ct->ctl_read_val | (ast_ct_hclk_divs[i - 1] << 8); + rc = sfc_writel(ct, ct->ctl_reg, tv); + if (rc < 0) { + free(test_buf); + return rc; + } + SFC_DBG("AST: Trying HCLK/%d...\n", i); + rc = sfc_calibrate_reads(ct, i, golden_buf, test_buf); + + /* Some other error occurred, bail out */ + if (rc && rc != -EREMOTEIO) { + free(test_buf); + return rc; + } + if (rc == 0) + best_div = i; } - SFC_DBG("AST: Trying HCLK/%d...\n", i); - rc = sfc_calibrate_reads(ct, i, golden_buf, test_buf); + free(test_buf); - /* Some other error occurred, bail out */ - if (rc && rc != -EREMOTEIO) { - free(test_buf); - return rc; + /* Nothing found ? */ + if (best_div < 0) + SFC_INF("AST: No good frequency, using dumb slow\n"); + else { + SFC_INF("AST: Found good read timings at HCLK/%d\n", best_div); + ct->ctl_read_val |= (ast_ct_hclk_divs[best_div - 1] << 8); } - if (rc == 0) - best_div = i; - } - free(test_buf); - - /* Nothing found ? */ - if (best_div < 0) - SFC_INF("AST: No good frequency, using dumb slow\n"); - else { - SFC_INF("AST: Found good read timings at HCLK/%d\n", best_div); - ct->ctl_read_val |= (ast_ct_hclk_divs[best_div - 1] << 8); - } - return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); } static int sfc_get_hclk(uint32_t *ctl_val, uint32_t max_freq) { - int i; - - /* It appears that running commands at HCLK/2 on some micron - * chips results in occasionally reads of bogus status (that - * or unrelated chip hangs). - * - * Since we cannot calibrate properly the reads for commands, - * instead, let's limit our SPI frequency to HCLK/4 to stay - * on the safe side of things - */ -#define MIN_CMD_FREQ 4 - for (i = MIN_CMD_FREQ; i <= 5; i++) { - uint32_t freq = ast_ahb_freq / i; - if (freq >= max_freq) - continue; - *ctl_val |= (ast_ct_hclk_divs[i - 1] << 8); - return i; - } - return 0; + int i; + + /* It appears that running commands at HCLK/2 on some micron + * chips results in occasionally reads of bogus status (that + * or unrelated chip hangs). + * + * Since we cannot calibrate properly the reads for commands, + * instead, let's limit our SPI frequency to HCLK/4 to stay + * on the safe side of things + */ +#define MIN_CMD_FREQ 4 + for (i = MIN_CMD_FREQ; i <= 5; i++) { + uint32_t freq = ast_ahb_freq / i; + if (freq >= max_freq) + continue; + *ctl_val |= (ast_ct_hclk_divs[i - 1] << 8); + return i; + } + return 0; } static int sfc_setup_macronix(struct sfc_data *ct, struct flash_info *info) { - int rc, div __unused; - uint8_t srcr[2]; - - /* - * Those Macronix chips support dual reads at 104Mhz - * and dual IO at 84Mhz with 4 dummies. - * - * Our calibration algo should give us something along - * the lines of HCLK/3 (HCLK/2 seems to work sometimes - * but appears to be fairly unreliable) which is 64Mhz - * - * So we chose dual IO mode. - * - * The CE# inactive width for reads must be 7ns, we set it - * to 3T which is about 15ns at the fastest speed we support - * HCLK/2) as I've had issue with smaller values. - * - * For write and program it's 30ns so let's set the value - * for normal ops to 6T. - * - * Preserve the current 4b mode. - */ - SFC_DBG("AST: Setting up Macronix...\n"); - - /* + int rc, div __unused; + uint8_t srcr[2]; + + /* + * Those Macronix chips support dual reads at 104Mhz + * and dual IO at 84Mhz with 4 dummies. + * + * Our calibration algo should give us something along + * the lines of HCLK/3 (HCLK/2 seems to work sometimes + * but appears to be fairly unreliable) which is 64Mhz + * + * So we chose dual IO mode. + * + * The CE# inactive width for reads must be 7ns, we set it + * to 3T which is about 15ns at the fastest speed we support + * HCLK/2) as I've had issue with smaller values. + * + * For write and program it's 30ns so let's set the value + * for normal ops to 6T. + * + * Preserve the current 4b mode. + */ + SFC_DBG("AST: Setting up Macronix...\n"); + + /* * Read the status and config registers */ - rc = sfc_cmd_rd(&ct->ops, CMD_RDSR, false, 0, &srcr[0], 1); - if (rc != 0) { - SFC_ERR("AST: Failed to read status\n"); - return rc; - } - rc = sfc_cmd_rd(&ct->ops, CMD_RDCR, false, 0, &srcr[1], 1); - if (rc != 0) { - SFC_ERR("AST: Failed to read configuration\n"); - return rc; - } + rc = sfc_cmd_rd(&ct->ops, CMD_RDSR, false, 0, &srcr[0], 1); + if (rc != 0) { + SFC_ERR("AST: Failed to read status\n"); + return rc; + } + rc = sfc_cmd_rd(&ct->ops, CMD_RDCR, false, 0, &srcr[1], 1); + if (rc != 0) { + SFC_ERR("AST: Failed to read configuration\n"); + return rc; + } - SFC_DBG("AST: Macronix SR:CR: 0x%02x:%02x\n", srcr[0], srcr[1]); + SFC_DBG("AST: Macronix SR:CR: 0x%02x:%02x\n", srcr[0], srcr[1]); - /* Switch to 8 dummy cycles to enable 104Mhz operations */ - srcr[1] = (srcr[1] & 0x3f) | 0x80; + /* Switch to 8 dummy cycles to enable 104Mhz operations */ + srcr[1] = (srcr[1] & 0x3f) | 0x80; - rc = fl_wren(&ct->ops); - if (rc) { - SFC_ERR("AST: Failed to WREN for Macronix config\n"); - return rc; - } + rc = fl_wren(&ct->ops); + if (rc) { + SFC_ERR("AST: Failed to WREN for Macronix config\n"); + return rc; + } - rc = sfc_cmd_wr(&ct->ops, CMD_WRSR, false, 0, srcr, 2); - if (rc != 0) { - SFC_ERR("AST: Failed to write Macronix config\n"); - return rc; - } - rc = fl_sync_wait_idle(&ct->ops);; - if (rc != 0) { - SFC_ERR("AST: Failed waiting for config write\n"); - return rc; - } - - SFC_DBG("AST: Macronix SR:CR: 0x%02x:%02x\n", srcr[0], srcr[1]); - - /* Use 2READ */ - ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | - (0x03 << 28) | /* Dual IO */ - (0x0d << 24) | /* CE# width 3T */ - (0xbb << 16) | /* 2READ command */ - (0x00 << 8) | /* HCLK/16 (optimize later) */ - (0x02 << 6) | /* 2 bytes dummy cycle (8 clocks) */ - (0x01); /* fast read */ - - /* Configure SPI flash read timing */ - rc = sfc_optimize_reads(ct, info, 104000000); - if (rc) { - SFC_ERR("AST: Failed to setup proper read timings, rc=%d\n", rc); - return rc; - } + rc = sfc_cmd_wr(&ct->ops, CMD_WRSR, false, 0, srcr, 2); + if (rc != 0) { + SFC_ERR("AST: Failed to write Macronix config\n"); + return rc; + } + rc = fl_sync_wait_idle(&ct->ops); + ; + if (rc != 0) { + SFC_ERR("AST: Failed waiting for config write\n"); + return rc; + } - /* - * For other commands and writes also increase the SPI clock - * to HCLK/2 since the chip supports up to 133Mhz and set - * CE# inactive to 6T. We request a timing that is 20% below - * the limit of the chip, so about 106Mhz which should fit. - */ - ct->ctl_val = (ct->ctl_val & 0x2000) | - (0x00 << 28) | /* Single bit */ - (0x0a << 24) | /* CE# width 6T (b1010) */ - (0x00 << 16) | /* no command */ - (0x00 << 8) | /* HCLK/16 (done later) */ - (0x00 << 6) | /* no dummy cycle */ - (0x00); /* normal read */ - - div = sfc_get_hclk(&ct->ctl_val, 106000000); - SFC_INF("AST: Command timing set to HCLK/%d\n", div); - - /* Update chip with current read config */ - return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + SFC_DBG("AST: Macronix SR:CR: 0x%02x:%02x\n", srcr[0], srcr[1]); + + /* Use 2READ */ + ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | + (0x03 << 28) | /* Dual IO */ + (0x0d << 24) | /* CE# width 3T */ + (0xbb << 16) | /* 2READ command */ + (0x00 << 8) | /* HCLK/16 (optimize later) */ + (0x02 << 6) | /* 2 bytes dummy cycle (8 clocks) */ + (0x01); /* fast read */ + + /* Configure SPI flash read timing */ + rc = sfc_optimize_reads(ct, info, 104000000); + if (rc) { + SFC_ERR("AST: Failed to setup proper read timings, rc=%d\n", + rc); + return rc; + } + + /* + * For other commands and writes also increase the SPI clock + * to HCLK/2 since the chip supports up to 133Mhz and set + * CE# inactive to 6T. We request a timing that is 20% below + * the limit of the chip, so about 106Mhz which should fit. + */ + ct->ctl_val = (ct->ctl_val & 0x2000) | (0x00 << 28) | /* Single bit */ + (0x0a << 24) | /* CE# width 6T (b1010) */ + (0x00 << 16) | /* no command */ + (0x00 << 8) | /* HCLK/16 (done later) */ + (0x00 << 6) | /* no dummy cycle */ + (0x00); /* normal read */ + + div = sfc_get_hclk(&ct->ctl_val, 106000000); + SFC_INF("AST: Command timing set to HCLK/%d\n", div); + + /* Update chip with current read config */ + return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); } static int sfc_setup_winbond(struct sfc_data *ct, struct flash_info *info) { - int rc, div __unused; - - SFC_DBG("AST: Setting up Windbond...\n"); - - /* - * This Windbond chip support dual reads at 104Mhz - * with 8 dummy cycles. - * - * The CE# inactive width for reads must be 10ns, we set it - * to 3T which is about 15.6ns. - */ - ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | - (0x02 << 28) | /* Dual bit data only */ - (0x0e << 24) | /* CE# width 2T (b1110) */ - (0x3b << 16) | /* DREAD command */ - (0x00 << 8) | /* HCLK/16 */ - (0x01 << 6) | /* 1-byte dummy cycle */ - (0x01); /* fast read */ - - /* Configure SPI flash read timing */ - rc = sfc_optimize_reads(ct, info, 104000000); - if (rc) { - SFC_ERR("AST: Failed to setup proper read timings, rc=%d\n", rc); - return rc; - } + int rc, div __unused; + + SFC_DBG("AST: Setting up Windbond...\n"); + + /* + * This Windbond chip support dual reads at 104Mhz + * with 8 dummy cycles. + * + * The CE# inactive width for reads must be 10ns, we set it + * to 3T which is about 15.6ns. + */ + ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | + (0x02 << 28) | /* Dual bit data only */ + (0x0e << 24) | /* CE# width 2T (b1110) */ + (0x3b << 16) | /* DREAD command */ + (0x00 << 8) | /* HCLK/16 */ + (0x01 << 6) | /* 1-byte dummy cycle */ + (0x01); /* fast read */ + + /* Configure SPI flash read timing */ + rc = sfc_optimize_reads(ct, info, 104000000); + if (rc) { + SFC_ERR("AST: Failed to setup proper read timings, rc=%d\n", + rc); + return rc; + } - /* - * For other commands and writes also increase the SPI clock - * to HCLK/2 since the chip supports up to 133Mhz. CE# inactive - * for write and erase is 50ns so let's set it to 10T. - */ - ct->ctl_val = (ct->ctl_read_val & 0x2000) | - (0x00 << 28) | /* Single bit */ - (0x06 << 24) | /* CE# width 10T (b0110) */ - (0x00 << 16) | /* no command */ - (0x00 << 8) | /* HCLK/16 */ - (0x00 << 6) | /* no dummy cycle */ - (0x01); /* fast read */ - - div = sfc_get_hclk(&ct->ctl_val, 106000000); - SFC_ERR("AST: Command timing set to HCLK/%d\n", div); - - /* Update chip with current read config */ - return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + /* + * For other commands and writes also increase the SPI clock + * to HCLK/2 since the chip supports up to 133Mhz. CE# inactive + * for write and erase is 50ns so let's set it to 10T. + */ + ct->ctl_val = (ct->ctl_read_val & 0x2000) | + (0x00 << 28) | /* Single bit */ + (0x06 << 24) | /* CE# width 10T (b0110) */ + (0x00 << 16) | /* no command */ + (0x00 << 8) | /* HCLK/16 */ + (0x00 << 6) | /* no dummy cycle */ + (0x01); /* fast read */ + + div = sfc_get_hclk(&ct->ctl_val, 106000000); + SFC_ERR("AST: Command timing set to HCLK/%d\n", div); + + /* Update chip with current read config */ + return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); } static int sfc_setup_micron(struct sfc_data *ct, struct flash_info *info) { - uint8_t vconf, ext_id[6]; - int rc, div __unused; - - SFC_DBG("AST: Setting up Micron...\n"); - - /* - * Read the extended chip ID to try to detect old vs. new - * flashes since old Micron flashes have a lot of issues - */ - rc = sfc_cmd_rd(&ct->ops, CMD_RDID, false, 0, ext_id, 6); - if (rc != 0) { - SFC_ERR("AST: Failed to read Micron ext ID, sticking to dumb speed\n"); - return 0; - } - /* Check ID matches expectations */ - if (ext_id[0] != ((info->id >> 16) & 0xff) || - ext_id[1] != ((info->id >> 8) & 0xff) || - ext_id[2] != ((info->id ) & 0xff)) { - SFC_ERR("AST: Micron ext ID mismatch, sticking to dumb speed\n"); - return 0; - } - SFC_DBG("AST: Micron ext ID byte: 0x%02x\n", ext_id[4]); - - /* Check for old (<45nm) chips, don't try to be fancy on those */ - if (!(ext_id[4] & 0x40)) { - SFC_DBG("AST: Old chip, using dumb timings\n"); - goto dumb; - } - - /* - * Read the micron specific volatile configuration reg - */ - rc = sfc_cmd_rd(&ct->ops, CMD_MIC_RDVCONF, false, 0, &vconf, 1); - if (rc != 0) { - SFC_ERR("AST: Failed to read Micron vconf, sticking to dumb speed\n"); - goto dumb; - } - SFC_DBG("AST: Micron VCONF: 0x%02x\n", vconf); - - /* Switch to 8 dummy cycles (we might be able to operate with 4 - * but let's keep some margin - */ - vconf = (vconf & 0x0f) | 0x80; - - rc = sfc_cmd_wr(&ct->ops, CMD_MIC_WRVCONF, false, 0, &vconf, 1); - if (rc != 0) { - SFC_ERR("AST: Failed to write Micron vconf, " - " sticking to dumb speed\n"); - goto dumb; - } - rc = fl_sync_wait_idle(&ct->ops);; - if (rc != 0) { - SFC_ERR("AST: Failed waiting for config write\n"); - return rc; - } - SFC_DBG("AST: Updated to : 0x%02x\n", vconf); - - /* - * Try to do full dual IO, with 8 dummy cycles it supports 133Mhz - * - * The CE# inactive width for reads must be 20ns, we set it - * to 4T which is about 20.8ns. - */ - ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | - (0x03 << 28) | /* Single bit */ - (0x0c << 24) | /* CE# 4T */ - (0xbb << 16) | /* 2READ command */ - (0x00 << 8) | /* HCLK/16 (optimize later) */ - (0x02 << 6) | /* 8 dummy cycles (2 bytes) */ - (0x01); /* fast read */ - - /* Configure SPI flash read timing */ - rc = sfc_optimize_reads(ct, info, 133000000); - if (rc) { - SFC_ERR("AST: Failed to setup proper read timings, rc=%d\n", rc); - return rc; - } + uint8_t vconf, ext_id[6]; + int rc, div __unused; + + SFC_DBG("AST: Setting up Micron...\n"); + + /* + * Read the extended chip ID to try to detect old vs. new + * flashes since old Micron flashes have a lot of issues + */ + rc = sfc_cmd_rd(&ct->ops, CMD_RDID, false, 0, ext_id, 6); + if (rc != 0) { + SFC_ERR("AST: Failed to read Micron ext ID, sticking to dumb speed\n"); + return 0; + } + /* Check ID matches expectations */ + if (ext_id[0] != ((info->id >> 16) & 0xff) || + ext_id[1] != ((info->id >> 8) & 0xff) || + ext_id[2] != ((info->id) & 0xff)) { + SFC_ERR("AST: Micron ext ID mismatch, sticking to dumb speed\n"); + return 0; + } + SFC_DBG("AST: Micron ext ID byte: 0x%02x\n", ext_id[4]); - /* - * For other commands and writes also increase the SPI clock - * to HCLK/2 since the chip supports up to 133Mhz. CE# inactive - * for write and erase is 50ns so let's set it to 10T. - */ - ct->ctl_val = (ct->ctl_read_val & 0x2000) | - (0x00 << 28) | /* Single bit */ - (0x06 << 24) | /* CE# width 10T (b0110) */ - (0x00 << 16) | /* no command */ - (0x00 << 8) | /* HCLK/16 */ - (0x00 << 6) | /* no dummy cycle */ - (0x00); /* norm read */ + /* Check for old (<45nm) chips, don't try to be fancy on those */ + if (!(ext_id[4] & 0x40)) { + SFC_DBG("AST: Old chip, using dumb timings\n"); + goto dumb; + } - div = sfc_get_hclk(&ct->ctl_val, 133000000); - SFC_INF("AST: Command timing set to HCLK/%d\n", div); + /* + * Read the micron specific volatile configuration reg + */ + rc = sfc_cmd_rd(&ct->ops, CMD_MIC_RDVCONF, false, 0, &vconf, 1); + if (rc != 0) { + SFC_ERR("AST: Failed to read Micron vconf, sticking to dumb speed\n"); + goto dumb; + } + SFC_DBG("AST: Micron VCONF: 0x%02x\n", vconf); + + /* Switch to 8 dummy cycles (we might be able to operate with 4 + * but let's keep some margin + */ + vconf = (vconf & 0x0f) | 0x80; + + rc = sfc_cmd_wr(&ct->ops, CMD_MIC_WRVCONF, false, 0, &vconf, 1); + if (rc != 0) { + SFC_ERR("AST: Failed to write Micron vconf, " + " sticking to dumb speed\n"); + goto dumb; + } + rc = fl_sync_wait_idle(&ct->ops); + ; + if (rc != 0) { + SFC_ERR("AST: Failed waiting for config write\n"); + return rc; + } + SFC_DBG("AST: Updated to : 0x%02x\n", vconf); + + /* + * Try to do full dual IO, with 8 dummy cycles it supports 133Mhz + * + * The CE# inactive width for reads must be 20ns, we set it + * to 4T which is about 20.8ns. + */ + ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | + (0x03 << 28) | /* Single bit */ + (0x0c << 24) | /* CE# 4T */ + (0xbb << 16) | /* 2READ command */ + (0x00 << 8) | /* HCLK/16 (optimize later) */ + (0x02 << 6) | /* 8 dummy cycles (2 bytes) */ + (0x01); /* fast read */ + + /* Configure SPI flash read timing */ + rc = sfc_optimize_reads(ct, info, 133000000); + if (rc) { + SFC_ERR("AST: Failed to setup proper read timings, rc=%d\n", + rc); + return rc; + } - /* Update chip with current read config */ - return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + /* + * For other commands and writes also increase the SPI clock + * to HCLK/2 since the chip supports up to 133Mhz. CE# inactive + * for write and erase is 50ns so let's set it to 10T. + */ + ct->ctl_val = (ct->ctl_read_val & 0x2000) | + (0x00 << 28) | /* Single bit */ + (0x06 << 24) | /* CE# width 10T (b0110) */ + (0x00 << 16) | /* no command */ + (0x00 << 8) | /* HCLK/16 */ + (0x00 << 6) | /* no dummy cycle */ + (0x00); /* norm read */ + + div = sfc_get_hclk(&ct->ctl_val, 133000000); + SFC_INF("AST: Command timing set to HCLK/%d\n", div); + + /* Update chip with current read config */ + return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); dumb: - ct->ctl_val = ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | - (0x00 << 28) | /* Single bit */ - (0x00 << 24) | /* CE# max */ - (0x03 << 16) | /* use normal reads */ - (0x06 << 8) | /* HCLK/4 */ - (0x00 << 6) | /* no dummy cycle */ - (0x00); /* normal read */ - - /* Update chip with current read config */ - return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + ct->ctl_val = ct->ctl_read_val = (ct->ctl_read_val & 0x2000) | + (0x00 << 28) | /* Single bit */ + (0x00 << 24) | /* CE# max */ + (0x03 << 16) | /* use normal reads */ + (0x06 << 8) | /* HCLK/4 */ + (0x00 << 6) | /* no dummy cycle */ + (0x00); /* normal read */ + + /* Update chip with current read config */ + return sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); } static int sfc_setup(struct sfc *ctrl, uint32_t *tsize) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - struct flash_info *info = ctrl->finfo; + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + struct flash_info *info = ctrl->finfo; - (void)tsize; + (void)tsize; - return 0; + return 0; - /* - * Configure better timings and read mode for known - * flash chips - */ - switch(info->id) { - case 0xc22018: /* MX25L12835F */ - case 0xc22019: /* MX25L25635F */ - case 0xc2201a: /* MX66L51235F */ - case 0xc2201b: /* MX66L1G45G */ - return sfc_setup_macronix(ct, info); - case 0xef4018: /* W25Q128BV */ - case 0xef4019: /* W25Q256BV */ - return sfc_setup_winbond(ct, info); - case 0x20ba20: /* MT25Qx512xx */ - return sfc_setup_micron(ct, info); - } - /* No special tuning */ - return 0; + /* + * Configure better timings and read mode for known + * flash chips + */ + switch (info->id) { + case 0xc22018: /* MX25L12835F */ + case 0xc22019: /* MX25L25635F */ + case 0xc2201a: /* MX66L51235F */ + case 0xc2201b: /* MX66L1G45G */ + return sfc_setup_macronix(ct, info); + case 0xef4018: /* W25Q128BV */ + case 0xef4019: /* W25Q256BV */ + return sfc_setup_winbond(ct, info); + case 0x20ba20: /* MT25Qx512xx */ + return sfc_setup_micron(ct, info); + } + /* No special tuning */ + return 0; } static bool sfc_init_device(struct sfc_data *ct) { - uint32_t ce_type; - int rc; - - /* - * Snapshot control reg and sanitize it for our - * use, switching to 1-bit mode, clearing user - * mode if set, etc... - * - * Also configure SPI clock to something safe - * like HCLK/8 (24Mhz) - */ - rc = sfc_readl(ct, ct->ctl_reg, &ct->ctl_val); - if (rc < 0 || ct->ctl_val == 0xffffffff) { - SFC_ERR("AST_SF: Failed read from controller control\n"); - return false; - } - - /* Enable writes for user mode */ - rc = sfc_readl(ct, ct->type_reg, &ce_type); - if (rc < 0) { errno = -rc; perror("sfc_readl"); return false; } - - rc = sfc_writel(ct, ct->type_reg, ce_type | (7 << 16)); - if (rc < 0) { errno = -rc; perror("sfc_writel"); return false; } - - ct->ctl_val = - (0x00 << 28) | /* Single bit */ - (0x00 << 24) | /* CE# width 16T */ - (0x00 << 16) | /* no command */ - (0x04 << 8) | /* HCLK/8 */ - (0x00 << 6) | /* no dummy cycle */ - (0x00); /* normal read */ - - /* Initial read mode is default */ - ct->ctl_read_val = ct->ctl_val; - - /* Initial read timings all 0 */ - ct->fread_timing_val = 0; - - /* Configure for read */ - rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); - if (rc < 0) { errno = -rc; perror("sfc_writel"); return false; } - rc = sfc_writel(ct, ct->fread_timing_reg, ct->fread_timing_val); - if (rc < 0) { errno = -rc; perror("sfc_writel"); return false; } - - ct->mode_4b = false; - - return true; + uint32_t ce_type; + int rc; + + /* + * Snapshot control reg and sanitize it for our + * use, switching to 1-bit mode, clearing user + * mode if set, etc... + * + * Also configure SPI clock to something safe + * like HCLK/8 (24Mhz) + */ + rc = sfc_readl(ct, ct->ctl_reg, &ct->ctl_val); + if (rc < 0 || ct->ctl_val == 0xffffffff) { + SFC_ERR("AST_SF: Failed read from controller control\n"); + return false; + } + + /* Enable writes for user mode */ + rc = sfc_readl(ct, ct->type_reg, &ce_type); + if (rc < 0) { + errno = -rc; + perror("sfc_readl"); + return false; + } + + rc = sfc_writel(ct, ct->type_reg, ce_type | (7 << 16)); + if (rc < 0) { + errno = -rc; + perror("sfc_writel"); + return false; + } + + ct->ctl_val = (0x00 << 28) | /* Single bit */ + (0x00 << 24) | /* CE# width 16T */ + (0x00 << 16) | /* no command */ + (0x04 << 8) | /* HCLK/8 */ + (0x00 << 6) | /* no dummy cycle */ + (0x00); /* normal read */ + + /* Initial read mode is default */ + ct->ctl_read_val = ct->ctl_val; + + /* Initial read timings all 0 */ + ct->fread_timing_val = 0; + + /* Configure for read */ + rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val); + if (rc < 0) { + errno = -rc; + perror("sfc_writel"); + return false; + } + rc = sfc_writel(ct, ct->fread_timing_reg, ct->fread_timing_val); + if (rc < 0) { + errno = -rc; + perror("sfc_writel"); + return false; + } + + ct->mode_4b = false; + + return true; } int sfc_write_protect_save(struct sfc *ctrl, bool enable, uint32_t *save) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - uint32_t old_tsr, new_tsr; - int rc; + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + uint32_t old_tsr, new_tsr; + int rc; - if ((rc = sfc_readl(ct, ct->type_reg, &old_tsr)) < 0) - return rc; + if ((rc = sfc_readl(ct, ct->type_reg, &old_tsr)) < 0) + return rc; - /* TODO: Specify which CE? */ + /* TODO: Specify which CE? */ - if (enable) { - new_tsr = old_tsr | ct->type_wp_mask; - } else { - new_tsr = old_tsr & ~ct->type_wp_mask; - } + if (enable) { + new_tsr = old_tsr | ct->type_wp_mask; + } else { + new_tsr = old_tsr & ~ct->type_wp_mask; + } - if ((rc = sfc_writel(ct, ct->type_reg, new_tsr)) < 0) - return rc; + if ((rc = sfc_writel(ct, ct->type_reg, new_tsr)) < 0) + return rc; - *save = old_tsr & ct->type_wp_mask; + *save = old_tsr & ct->type_wp_mask; - return 0; + return 0; } int sfc_write_protect_restore(struct sfc *ctrl, uint32_t save) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - uint32_t tsr; - int rc; + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + uint32_t tsr; + int rc; - if ((rc = sfc_readl(ct, ct->type_reg, &tsr)) < 0) - return rc; + if ((rc = sfc_readl(ct, ct->type_reg, &tsr)) < 0) + return rc; - /* TODO: Specify which CE? */ - tsr &= ~ct->type_wp_mask; - tsr |= (save & ct->type_wp_mask); + /* TODO: Specify which CE? */ + tsr &= ~ct->type_wp_mask; + tsr |= (save & ct->type_wp_mask); - return sfc_writel(ct, ct->type_reg, tsr); + return sfc_writel(ct, ct->type_reg, tsr); } int sfc_get_flash(struct sfc *ctrl, struct soc_region *flash) { - struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); + struct sfc_data *ct = container_of(ctrl, struct sfc_data, ops); - *flash = ct->flash; + *flash = ct->flash; - return 0; + return 0; } static const struct soc_device_id sfc_match[] = { - { .compatible = "aspeed,ast2500-fmc", .data = (void *)SFC_TYPE_FMC }, - { .compatible = "aspeed,ast2500-spi", .data = (void *)SFC_TYPE_SMC }, - { }, + { .compatible = "aspeed,ast2500-fmc", .data = (void *)SFC_TYPE_FMC }, + { .compatible = "aspeed,ast2500-spi", .data = (void *)SFC_TYPE_SMC }, + {}, }; static int sfc_driver_init(struct soc *soc, struct soc_device *dev) { - struct sfc_data *ct; - int rc; - - ct = malloc(sizeof(*ct)); - if (!ct) { - SFC_ERR("AST_SF: Failed to allocate\n"); - return -ENOMEM; - } - memset(ct, 0, sizeof(*ct)); - - if ((rc = soc_device_get_memory_index(soc, &dev->node, 0, &ct->iomem)) < 0) - goto fail; - - if ((rc = soc_device_get_memory_index(soc, &dev->node, 1, &ct->flash)) < 0) - goto fail; - - ct->type = (unsigned long)(soc_device_get_match_data(soc, sfc_match, &dev->node)); - if (!ct->type) { - loge("sfc: Failed to acquire match data\n"); - rc = -EINVAL; - goto fail; - } - - if (!(ct->clk = clk_get(soc))) { - loge("Failed to acquire clock controller\n"); - rc = -ENODEV; - goto fail; - } - - ct->soc = soc; - - /* XXX: Hack exposing the AHB instance used to the flash layer */ - ct->ops.priv = soc->ahb; - - ct->ops.cmd_wr = sfc_cmd_wr; - ct->ops.cmd_rd = sfc_cmd_rd; - ct->ops.set_4b = sfc_set_4b; - ct->ops.direct_read = sfc_direct_read; - ct->ops.setup = sfc_setup; - - ast2500_get_ahb_freq(ct); - - /* TODO: Set these in the platform data, add platform data pointer to ct */ - if (ct->type == SFC_TYPE_SMC) { - ct->type_reg = FMC_CE_TYPE; - ct->type_wp_mask = (FMC_CE_TYPE_CE0_WP | FMC_CE_TYPE_CE1_WP | - FMC_CE_TYPE_CE2_WP); - ct->ctl_reg = SMC_CE0_CTRL; - ct->fread_timing_reg = SMC_TIMING; - } else if (ct->type == SFC_TYPE_FMC) { - ct->type_reg = FMC_CE_TYPE; - ct->type_wp_mask = (FMC_CE_TYPE_CE0_WP | FMC_CE_TYPE_CE1_WP); - ct->ctl_reg = FMC_CE0_CTRL; - ct->fread_timing_reg = FMC_TIMING; - } else { - rc = -EINVAL; - goto fail; - } - - if (!sfc_init_device(ct)) { - rc = -EIO; - goto fail; - } - - soc_device_set_drvdata(dev, &ct->ops); - - return 0; + struct sfc_data *ct; + int rc; + + ct = malloc(sizeof(*ct)); + if (!ct) { + SFC_ERR("AST_SF: Failed to allocate\n"); + return -ENOMEM; + } + memset(ct, 0, sizeof(*ct)); + + if ((rc = soc_device_get_memory_index(soc, &dev->node, 0, &ct->iomem)) < + 0) + goto fail; + + if ((rc = soc_device_get_memory_index(soc, &dev->node, 1, &ct->flash)) < + 0) + goto fail; + + ct->type = (unsigned long)(soc_device_get_match_data(soc, sfc_match, + &dev->node)); + if (!ct->type) { + loge("sfc: Failed to acquire match data\n"); + rc = -EINVAL; + goto fail; + } + + if (!(ct->clk = clk_get(soc))) { + loge("Failed to acquire clock controller\n"); + rc = -ENODEV; + goto fail; + } + + ct->soc = soc; + + /* XXX: Hack exposing the AHB instance used to the flash layer */ + ct->ops.priv = soc->ahb; + + ct->ops.cmd_wr = sfc_cmd_wr; + ct->ops.cmd_rd = sfc_cmd_rd; + ct->ops.set_4b = sfc_set_4b; + ct->ops.direct_read = sfc_direct_read; + ct->ops.setup = sfc_setup; + + ast2500_get_ahb_freq(ct); + + /* TODO: Set these in the platform data, add platform data pointer to ct */ + if (ct->type == SFC_TYPE_SMC) { + ct->type_reg = FMC_CE_TYPE; + ct->type_wp_mask = (FMC_CE_TYPE_CE0_WP | FMC_CE_TYPE_CE1_WP | + FMC_CE_TYPE_CE2_WP); + ct->ctl_reg = SMC_CE0_CTRL; + ct->fread_timing_reg = SMC_TIMING; + } else if (ct->type == SFC_TYPE_FMC) { + ct->type_reg = FMC_CE_TYPE; + ct->type_wp_mask = (FMC_CE_TYPE_CE0_WP | FMC_CE_TYPE_CE1_WP); + ct->ctl_reg = FMC_CE0_CTRL; + ct->fread_timing_reg = FMC_TIMING; + } else { + rc = -EINVAL; + goto fail; + } + + if (!sfc_init_device(ct)) { + rc = -EIO; + goto fail; + } + + soc_device_set_drvdata(dev, &ct->ops); + + return 0; fail: - free(ct); - return rc; + free(ct); + return rc; } static void sfc_driver_destroy(struct soc_device *dev) { - struct sfc_data *ct; - struct sfc *ctrl; - int rc; - - ctrl = soc_device_get_drvdata(dev); - ct = container_of(ctrl, struct sfc_data, ops); - - /* Restore control reg to read */ - if ((rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val)) < 0) { - loge("Failed to restore control reg state: %d\n", rc); - goto cleanup_ct; - } - - /* Additional cleanup */ - if (ct->type == SFC_TYPE_SMC) { - uint32_t reg; - - if ((rc = sfc_readl(ct, SMC_CONF, ®)) < 0) { - loge("Failed to read configuration register: %d\n", rc); - goto cleanup_ct; - } + struct sfc_data *ct; + struct sfc *ctrl; + int rc; + + ctrl = soc_device_get_drvdata(dev); + ct = container_of(ctrl, struct sfc_data, ops); - if (reg != 0xffffffff) { - if ((rc = sfc_writel(ct, SMC_CONF, reg & ~1)) < 0) { - loge("Failed to write configuration register: %d\n", rc); + /* Restore control reg to read */ + if ((rc = sfc_writel(ct, ct->ctl_reg, ct->ctl_read_val)) < 0) { + loge("Failed to restore control reg state: %d\n", rc); goto cleanup_ct; - } } - } + + /* Additional cleanup */ + if (ct->type == SFC_TYPE_SMC) { + uint32_t reg; + + if ((rc = sfc_readl(ct, SMC_CONF, ®)) < 0) { + loge("Failed to read configuration register: %d\n", rc); + goto cleanup_ct; + } + + if (reg != 0xffffffff) { + if ((rc = sfc_writel(ct, SMC_CONF, reg & ~1)) < 0) { + loge("Failed to write configuration register: %d\n", + rc); + goto cleanup_ct; + } + } + } cleanup_ct: - /* Free the whole lot */ - free(ct); + /* Free the whole lot */ + free(ct); } static const struct soc_driver sfc_driver = { - .name = "sfc", - .matches = sfc_match, - .init = sfc_driver_init, - .destroy = sfc_driver_destroy, + .name = "sfc", + .matches = sfc_match, + .init = sfc_driver_init, + .destroy = sfc_driver_destroy, }; REGISTER_SOC_DRIVER(sfc_driver); struct sfc *sfc_get_by_name(struct soc *soc, const char *name) { - return soc_driver_get_drvdata_by_name(soc, &sfc_driver, name); + return soc_driver_get_drvdata_by_name(soc, &sfc_driver, name); } diff --git a/src/soc/sfc.h b/src/soc/sfc.h index 6a70622..595aaa1 100644 --- a/src/soc/sfc.h +++ b/src/soc/sfc.h @@ -9,67 +9,62 @@ #include -#define SFC_TYPE_FMC 1U -#define SFC_TYPE_SMC 2U +#define SFC_TYPE_FMC 1U +#define SFC_TYPE_SMC 2U /* Flash commands */ -#define CMD_BE 0xd8 /* Block (64K) Erase */ -#define CMD_BE32K 0x52 /* Block (32K) Erase */ -#define CMD_CE 0x60 /* Chip Erase (Macronix/Winbond) */ -#define CMD_EN4B 0xb7 /* Enable 4B addresses */ -#define CMD_EX4B 0xe9 /* Exit 4B addresses */ -#define CMD_MIC_BULK_ERASE 0xc7 /* Micron Bulk Erase */ -#define CMD_MIC_RDFLST 0x70 /* Micron Read Flag Status */ -#define CMD_MIC_RDVCONF 0x85 /* Micron Read Volatile Config */ -#define CMD_MIC_WRVCONF 0x81 /* Micron Write Volatile Config */ -#define CMD_PP 0x02 /* Page Program */ -#define CMD_RDCR 0x15 /* Read configuration register (Macronix) */ -#define CMD_RDID 0x9f /* Read JEDEC ID */ -#define CMD_RDSR 0x05 /* Read Status Register */ -#define CMD_READ 0x03 /* READ */ -#define CMD_SE 0x20 /* Sector (4K) Erase */ -#define CMD_WREN 0x06 /* Write Enable */ -#define CMD_WRSR 0x01 /* Write Status Register (also config. on Macronix) */ +#define CMD_BE 0xd8 /* Block (64K) Erase */ +#define CMD_BE32K 0x52 /* Block (32K) Erase */ +#define CMD_CE 0x60 /* Chip Erase (Macronix/Winbond) */ +#define CMD_EN4B 0xb7 /* Enable 4B addresses */ +#define CMD_EX4B 0xe9 /* Exit 4B addresses */ +#define CMD_MIC_BULK_ERASE 0xc7 /* Micron Bulk Erase */ +#define CMD_MIC_RDFLST 0x70 /* Micron Read Flag Status */ +#define CMD_MIC_RDVCONF 0x85 /* Micron Read Volatile Config */ +#define CMD_MIC_WRVCONF 0x81 /* Micron Write Volatile Config */ +#define CMD_PP 0x02 /* Page Program */ +#define CMD_RDCR 0x15 /* Read configuration register (Macronix) */ +#define CMD_RDID 0x9f /* Read JEDEC ID */ +#define CMD_RDSR 0x05 /* Read Status Register */ +#define CMD_READ 0x03 /* READ */ +#define CMD_SE 0x20 /* Sector (4K) Erase */ +#define CMD_WREN 0x06 /* Write Enable */ +#define CMD_WRSR 0x01 /* Write Status Register (also config. on Macronix) */ /* Flash status bits */ -#define STAT_WIP 0x01 -#define STAT_WEN 0x02 +#define STAT_WIP 0x01 +#define STAT_WEN 0x02 /* This isn't exposed to clients but is to controllers */ struct flash_info { - uint32_t id; - uint32_t size; - uint32_t flags; -#define FL_ERASE_4K 0x00000001 /* Supports 4k erase */ -#define FL_ERASE_32K 0x00000002 /* Supports 32k erase */ -#define FL_ERASE_64K 0x00000004 /* Supports 64k erase */ -#define FL_ERASE_CHIP 0x00000008 /* Supports 0x60 cmd chip erase */ -#define FL_ERASE_BULK 0x00000010 /* Supports 0xc7 cmd bulk erase */ -#define FL_MICRON_BUGS 0x00000020 /* Various micron bug workarounds */ -#define FL_ERASE_ALL (FL_ERASE_4K | FL_ERASE_32K | FL_ERASE_64K | \ - FL_ERASE_CHIP) -#define FL_CAN_4B 0x00000010 /* Supports 4b mode */ - const char *name; + uint32_t id; + uint32_t size; + uint32_t flags; +#define FL_ERASE_4K 0x00000001 /* Supports 4k erase */ +#define FL_ERASE_32K 0x00000002 /* Supports 32k erase */ +#define FL_ERASE_64K 0x00000004 /* Supports 64k erase */ +#define FL_ERASE_CHIP 0x00000008 /* Supports 0x60 cmd chip erase */ +#define FL_ERASE_BULK 0x00000010 /* Supports 0xc7 cmd bulk erase */ +#define FL_MICRON_BUGS 0x00000020 /* Various micron bug workarounds */ +#define FL_ERASE_ALL (FL_ERASE_4K | FL_ERASE_32K | FL_ERASE_64K | FL_ERASE_CHIP) +#define FL_CAN_4B 0x00000010 /* Supports 4b mode */ + const char *name; }; /* Flash controller, return negative values for errors */ struct sfc { int (*setup)(struct sfc *ctrl, uint32_t *tsize); int (*set_4b)(struct sfc *ctrl, bool enable); - int (*chip_id)(struct sfc *ctrl, uint8_t *id_buf, - uint32_t *id_size); + int (*chip_id)(struct sfc *ctrl, uint8_t *id_buf, uint32_t *id_size); int (*direct_read)(struct sfc *ctrl, uint32_t addr, void *buf, - uint32_t size); - int (*write)(struct sfc *ctrl, uint32_t addr, - const void *buf, uint32_t size); - int (*erase)(struct sfc *ctrl, uint32_t addr, + uint32_t size); + int (*write)(struct sfc *ctrl, uint32_t addr, const void *buf, uint32_t size); - int (*cmd_rd)(struct sfc *ctrl, uint8_t cmd, - bool has_addr, uint32_t addr, void *buffer, - uint32_t size); - int (*cmd_wr)(struct sfc *ctrl, uint8_t cmd, - bool has_addr, uint32_t addr, const void *buffer, - uint32_t size); + int (*erase)(struct sfc *ctrl, uint32_t addr, uint32_t size); + int (*cmd_rd)(struct sfc *ctrl, uint8_t cmd, bool has_addr, + uint32_t addr, void *buffer, uint32_t size); + int (*cmd_wr)(struct sfc *ctrl, uint8_t cmd, bool has_addr, + uint32_t addr, const void *buffer, uint32_t size); struct flash_info *finfo; void *priv; diff --git a/src/soc/sioctl.c b/src/soc/sioctl.c index 8ddd5d2..c0f6cdf 100644 --- a/src/soc/sioctl.c +++ b/src/soc/sioctl.c @@ -9,143 +9,152 @@ #include #include -#define G4_SCU_HW_STRAP 0x070 -#define G4_SCU_HW_STRAP_SIO_DEC (1 << 20) -#define G4_SCU_HW_STRAP_SIO_4E (1 << 16) -#define G6_SCU_HW_STRAP2 0x510 -#define G6_SCU_HW_STRAP2_SIO_DEC (1 << 3) -#define G6_SCU_HW_STRAP2_SIO_4E (1 << 2) +#define G4_SCU_HW_STRAP 0x070 +#define G4_SCU_HW_STRAP_SIO_DEC (1 << 20) +#define G4_SCU_HW_STRAP_SIO_4E (1 << 16) +#define G6_SCU_HW_STRAP2 0x510 +#define G6_SCU_HW_STRAP2_SIO_DEC (1 << 3) +#define G6_SCU_HW_STRAP2_SIO_4E (1 << 2) #include "soc/strap.h" struct sioctl_pdata { - uint32_t reg; - uint32_t disable; - uint32_t select; + uint32_t reg; + uint32_t disable; + uint32_t select; }; struct sioctl { - struct soc *soc; - struct soc_region scu; - struct strap *strap; - const struct sioctl_pdata *pdata; + struct soc *soc; + struct soc_region scu; + struct strap *strap; + const struct sioctl_pdata *pdata; }; int sioctl_decode_configure(struct sioctl *ctx, const enum sioctl_decode mode) { - int rc; - - if (mode == sioctl_decode_disable) { - return strap_set(ctx->strap, ctx->pdata->reg, ctx->pdata->disable, ctx->pdata->disable); - } - - if (mode == sioctl_decode_4e) { - rc = strap_set(ctx->strap, ctx->pdata->reg, ctx->pdata->select, ctx->pdata->select); - if (rc < 0) { - return rc; - } - } else { - assert(mode == sioctl_decode_2e); - rc = strap_clear(ctx->strap, ctx->pdata->reg, ctx->pdata->select, ctx->pdata->select); - if (rc < 0) { - return rc; - } - } - - return strap_clear(ctx->strap, ctx->pdata->reg, ctx->pdata->disable, ctx->pdata->disable); + int rc; + + if (mode == sioctl_decode_disable) { + return strap_set(ctx->strap, ctx->pdata->reg, + ctx->pdata->disable, ctx->pdata->disable); + } + + if (mode == sioctl_decode_4e) { + rc = strap_set(ctx->strap, ctx->pdata->reg, ctx->pdata->select, + ctx->pdata->select); + if (rc < 0) { + return rc; + } + } else { + assert(mode == sioctl_decode_2e); + rc = strap_clear(ctx->strap, ctx->pdata->reg, + ctx->pdata->select, ctx->pdata->select); + if (rc < 0) { + return rc; + } + } + + return strap_clear(ctx->strap, ctx->pdata->reg, ctx->pdata->disable, + ctx->pdata->disable); } int sioctl_decode_status(struct sioctl *ctx, enum sioctl_decode *status) { - uint32_t strap; - int rc; + uint32_t strap; + int rc; - if ((rc = strap_read(ctx->strap, ctx->pdata->reg, &strap)) < 0) { - return rc; - } + if ((rc = strap_read(ctx->strap, ctx->pdata->reg, &strap)) < 0) { + return rc; + } - if (strap & ctx->pdata->disable) { - *status = sioctl_decode_disable; + if (strap & ctx->pdata->disable) { + *status = sioctl_decode_disable; - return 0; - } + return 0; + } - *status = (strap & ctx->pdata->select) ? sioctl_decode_4e : sioctl_decode_2e; + *status = (strap & ctx->pdata->select) ? sioctl_decode_4e : + sioctl_decode_2e; - return 0; + return 0; } static const struct sioctl_pdata ast2400_sioctl_pdata = { - .reg = G4_SCU_HW_STRAP, - .disable = G4_SCU_HW_STRAP_SIO_DEC, - .select = G4_SCU_HW_STRAP_SIO_4E, + .reg = G4_SCU_HW_STRAP, + .disable = G4_SCU_HW_STRAP_SIO_DEC, + .select = G4_SCU_HW_STRAP_SIO_4E, }; static const struct sioctl_pdata ast2600_sioctl_pdata = { - .reg = G6_SCU_HW_STRAP2, - .disable = G6_SCU_HW_STRAP2_SIO_DEC, - .select = G6_SCU_HW_STRAP2_SIO_4E, + .reg = G6_SCU_HW_STRAP2, + .disable = G6_SCU_HW_STRAP2_SIO_DEC, + .select = G6_SCU_HW_STRAP2_SIO_4E, }; static const struct soc_device_id sioctl_matches[] = { - { .compatible = "aspeed,ast2400-superio", .data = &ast2400_sioctl_pdata }, - { .compatible = "aspeed,ast2500-superio", .data = &ast2400_sioctl_pdata }, - { .compatible = "aspeed,ast2600-superio", .data = &ast2600_sioctl_pdata }, - { }, + { .compatible = "aspeed,ast2400-superio", + .data = &ast2400_sioctl_pdata }, + { .compatible = "aspeed,ast2500-superio", + .data = &ast2400_sioctl_pdata }, + { .compatible = "aspeed,ast2600-superio", + .data = &ast2600_sioctl_pdata }, + {}, }; static int sioctl_driver_init(struct soc *soc, struct soc_device *dev) { - struct sioctl *ctx; - int rc; + struct sioctl *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - ctx->soc = soc; + ctx->soc = soc; - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->scu)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->scu)) < 0) { + goto cleanup_ctx; + } - if (!(ctx->pdata = soc_device_get_match_data(soc, sioctl_matches, &dev->node))) { - loge("Failed to find sioctl platform data\n"); - rc = -EINVAL; - goto cleanup_ctx; - } + if (!(ctx->pdata = soc_device_get_match_data(soc, sioctl_matches, + &dev->node))) { + loge("Failed to find sioctl platform data\n"); + rc = -EINVAL; + goto cleanup_ctx; + } - if (!(ctx->strap = strap_get(soc))) { - loge("Failed to acquire strap controller\n"); - rc = -ENODEV; - goto cleanup_ctx; - } + if (!(ctx->strap = strap_get(soc))) { + loge("Failed to acquire strap controller\n"); + rc = -ENODEV; + goto cleanup_ctx; + } - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void sioctl_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } struct soc_driver sioctl_driver = { - .name = "sioctl", - .matches = sioctl_matches, - .init = sioctl_driver_init, - .destroy = sioctl_driver_destroy, + .name = "sioctl", + .matches = sioctl_matches, + .init = sioctl_driver_init, + .destroy = sioctl_driver_destroy, }; REGISTER_SOC_DRIVER(sioctl_driver); struct sioctl *sioctl_get(struct soc *soc) { - return soc_driver_get_drvdata(soc, &sioctl_driver); + return soc_driver_get_drvdata(soc, &sioctl_driver); } diff --git a/src/soc/sioctl.h b/src/soc/sioctl.h index e95c5db..76fc138 100644 --- a/src/soc/sioctl.h +++ b/src/soc/sioctl.h @@ -8,7 +8,11 @@ struct sioctl; -enum sioctl_decode { sioctl_decode_disable, sioctl_decode_2e, sioctl_decode_4e }; +enum sioctl_decode { + sioctl_decode_disable, + sioctl_decode_2e, + sioctl_decode_4e +}; int sioctl_decode_configure(struct sioctl *ctx, const enum sioctl_decode mode); int sioctl_decode_status(struct sioctl *ctx, enum sioctl_decode *status); diff --git a/src/soc/strap.c b/src/soc/strap.c index 9b6cc83..b750c87 100644 --- a/src/soc/strap.c +++ b/src/soc/strap.c @@ -8,272 +8,281 @@ #include struct strap_ops { - int (*read)(struct strap *ctx, int reg, uint32_t *val); - int (*set)(struct strap *ctx, int reg, uint32_t update, uint32_t mask); - int (*clear)(struct strap *ctx, int reg, uint32_t update, uint32_t mask); + int (*read)(struct strap *ctx, int reg, uint32_t *val); + int (*set)(struct strap *ctx, int reg, uint32_t update, uint32_t mask); + int (*clear)(struct strap *ctx, int reg, uint32_t update, + uint32_t mask); }; struct strap { - struct scu *scu; - const struct strap_ops *ops; + struct scu *scu; + const struct strap_ops *ops; }; int strap_read(struct strap *ctx, int reg, uint32_t *val) { - return ctx->ops->read(ctx, reg, val); + return ctx->ops->read(ctx, reg, val); } int strap_set(struct strap *ctx, int reg, uint32_t update, uint32_t mask) { - return ctx->ops->set(ctx, reg, update, mask); + return ctx->ops->set(ctx, reg, update, mask); } int strap_clear(struct strap *ctx, int reg, uint32_t update, uint32_t mask) { - return ctx->ops->clear(ctx, reg, update, mask); + return ctx->ops->clear(ctx, reg, update, mask); } static int ast2400_strap_read(struct strap *ctx, int reg, uint32_t *val) { - if (!(reg == AST2400_SCU_HW_STRAP1 || reg == AST2400_SCU_HW_STRAP2)) { - return -EINVAL; - } + if (!(reg == AST2400_SCU_HW_STRAP1 || reg == AST2400_SCU_HW_STRAP2)) { + return -EINVAL; + } - return scu_readl(ctx->scu, reg, val); + return scu_readl(ctx->scu, reg, val); } -static int ast2400_strap_set(struct strap *ctx, int reg, uint32_t update, uint32_t mask) +static int ast2400_strap_set(struct strap *ctx, int reg, uint32_t update, + uint32_t mask) { - uint32_t val; - int rc; + uint32_t val; + int rc; - if (!(reg == AST2400_SCU_HW_STRAP1 || reg == AST2400_SCU_HW_STRAP2)) { - return -EINVAL; - } + if (!(reg == AST2400_SCU_HW_STRAP1 || reg == AST2400_SCU_HW_STRAP2)) { + return -EINVAL; + } - if (update & ~mask) { - return -EINVAL; - } + if (update & ~mask) { + return -EINVAL; + } - if ((rc = scu_readl(ctx->scu, reg, &val)) < 0) { - return rc; - } + if ((rc = scu_readl(ctx->scu, reg, &val)) < 0) { + return rc; + } - val |= update; + val |= update; - return scu_writel(ctx->scu, reg, val); + return scu_writel(ctx->scu, reg, val); } -static int ast2400_strap_clear(struct strap *ctx, int reg, uint32_t update, uint32_t mask) +static int ast2400_strap_clear(struct strap *ctx, int reg, uint32_t update, + uint32_t mask) { - uint32_t val; - int rc; + uint32_t val; + int rc; - if (!(reg == AST2400_SCU_HW_STRAP1 || reg == AST2400_SCU_HW_STRAP2)) { - return -EINVAL; - } + if (!(reg == AST2400_SCU_HW_STRAP1 || reg == AST2400_SCU_HW_STRAP2)) { + return -EINVAL; + } - if (update & ~mask) { - return -EINVAL; - } + if (update & ~mask) { + return -EINVAL; + } - if ((rc = scu_readl(ctx->scu, reg, &val)) < 0) { - return rc; - } + if ((rc = scu_readl(ctx->scu, reg, &val)) < 0) { + return rc; + } - val &= ~update; + val &= ~update; - return scu_writel(ctx->scu, reg, val); + return scu_writel(ctx->scu, reg, val); } static const struct strap_ops ast2400_strap_ops = { - .read = ast2400_strap_read, - .set = ast2400_strap_set, - .clear = ast2400_strap_clear, + .read = ast2400_strap_read, + .set = ast2400_strap_set, + .clear = ast2400_strap_clear, }; static int ast2500_strap_read(struct strap *ctx, int reg, uint32_t *val) { - if (reg != AST2500_SCU_HW_STRAP) { - return -EINVAL; - } + if (reg != AST2500_SCU_HW_STRAP) { + return -EINVAL; + } - return scu_readl(ctx->scu, reg, val); + return scu_readl(ctx->scu, reg, val); } -static int ast2500_strap_set(struct strap *ctx, int reg, uint32_t update, uint32_t mask) +static int ast2500_strap_set(struct strap *ctx, int reg, uint32_t update, + uint32_t mask) { - if ((reg != AST2500_SCU_HW_STRAP) || (update & ~mask)) { - return -EINVAL; - } + if ((reg != AST2500_SCU_HW_STRAP) || (update & ~mask)) { + return -EINVAL; + } - return scu_writel(ctx->scu, reg, update); + return scu_writel(ctx->scu, reg, update); } -static int ast2500_strap_clear(struct strap *ctx, int reg, uint32_t update, uint32_t mask) +static int ast2500_strap_clear(struct strap *ctx, int reg, uint32_t update, + uint32_t mask) { - if ((reg != AST2500_SCU_HW_STRAP) || (update & ~mask)) { - return -EINVAL; - } + if ((reg != AST2500_SCU_HW_STRAP) || (update & ~mask)) { + return -EINVAL; + } - /* The silicon ID register is W1C for the strap register */ - return scu_writel(ctx->scu, AST2500_SCU_SILICON_ID, update); + /* The silicon ID register is W1C for the strap register */ + return scu_writel(ctx->scu, AST2500_SCU_SILICON_ID, update); } static const struct strap_ops ast2500_strap_ops = { - .read = ast2500_strap_read, - .set = ast2500_strap_set, - .clear = ast2500_strap_clear, + .read = ast2500_strap_read, + .set = ast2500_strap_set, + .clear = ast2500_strap_clear, }; static int ast2600_strap_read(struct strap *ctx, int reg, uint32_t *val) { - if (!(reg == AST2600_SCU_HW_STRAP1 || - reg == AST2600_SCU_HW_STRAP2 || - reg == AST2600_SCU_HW_STRAP3)) { - return -EINVAL; - } + if (!(reg == AST2600_SCU_HW_STRAP1 || reg == AST2600_SCU_HW_STRAP2 || + reg == AST2600_SCU_HW_STRAP3)) { + return -EINVAL; + } - return scu_readl(ctx->scu, reg, val); + return scu_readl(ctx->scu, reg, val); } static int ast2600_strap_is_protected(struct strap *ctx, int reg, uint32_t mask) { - /* STRAP3 is RMW with no protection */ - if (reg == AST2600_SCU_HW_STRAP3) { - return 0; - } - - /* STRAP1 and STRAP2 are W1S/W1C with write protection */ - if (reg == AST2600_SCU_HW_STRAP1 || reg == AST2600_SCU_HW_STRAP2) { - uint32_t protect; - int rc; - - if ((rc = scu_readl(ctx->scu, reg + 8, &protect)) < 0) { - loge("Failed to read protection register for strapping register %d: %d\n", reg, rc); - return rc; - } - - return !!(protect & mask); - } - - return -EINVAL; + /* STRAP3 is RMW with no protection */ + if (reg == AST2600_SCU_HW_STRAP3) { + return 0; + } + + /* STRAP1 and STRAP2 are W1S/W1C with write protection */ + if (reg == AST2600_SCU_HW_STRAP1 || reg == AST2600_SCU_HW_STRAP2) { + uint32_t protect; + int rc; + + if ((rc = scu_readl(ctx->scu, reg + 8, &protect)) < 0) { + loge("Failed to read protection register for strapping register %d: %d\n", + reg, rc); + return rc; + } + + return !!(protect & mask); + } + + return -EINVAL; } -static int ast2600_strap_set(struct strap *ctx, int reg, uint32_t update, uint32_t mask) +static int ast2600_strap_set(struct strap *ctx, int reg, uint32_t update, + uint32_t mask) { - int rc; + int rc; - if (!(reg == AST2600_SCU_HW_STRAP1 || - reg == AST2600_SCU_HW_STRAP2 || - reg == AST2600_SCU_HW_STRAP3)) { - return -EINVAL; - } + if (!(reg == AST2600_SCU_HW_STRAP1 || reg == AST2600_SCU_HW_STRAP2 || + reg == AST2600_SCU_HW_STRAP3)) { + return -EINVAL; + } - if (update & ~mask) { - return -EINVAL; - } + if (update & ~mask) { + return -EINVAL; + } - if ((rc = ast2600_strap_is_protected(ctx, reg, mask)) < 0) { - return rc; - } else if (rc) { - return -EPERM; - } + if ((rc = ast2600_strap_is_protected(ctx, reg, mask)) < 0) { + return rc; + } else if (rc) { + return -EPERM; + } - return scu_writel(ctx->scu, reg, update); + return scu_writel(ctx->scu, reg, update); } -static int ast2600_strap_clear(struct strap *ctx, int reg, uint32_t update, uint32_t mask) +static int ast2600_strap_clear(struct strap *ctx, int reg, uint32_t update, + uint32_t mask) { - int rc; + int rc; - if (update & ~mask) { - return -EINVAL; - } + if (update & ~mask) { + return -EINVAL; + } - if ((rc = ast2600_strap_is_protected(ctx, reg, mask)) < 0) { - return rc; - } else if (rc) { - return -EPERM; - } + if ((rc = ast2600_strap_is_protected(ctx, reg, mask)) < 0) { + return rc; + } else if (rc) { + return -EPERM; + } - /* W1S/W1C pairs */ - if (reg == AST2600_SCU_HW_STRAP1 || reg == AST2600_SCU_HW_STRAP2) { - return scu_writel(ctx->scu, reg + 4, update); - } + /* W1S/W1C pairs */ + if (reg == AST2600_SCU_HW_STRAP1 || reg == AST2600_SCU_HW_STRAP2) { + return scu_writel(ctx->scu, reg + 4, update); + } - /* RMW, because register layout is hard */ - if (reg == AST2600_SCU_HW_STRAP3) { - uint32_t val; - int rc; + /* RMW, because register layout is hard */ + if (reg == AST2600_SCU_HW_STRAP3) { + uint32_t val; + int rc; - if ((rc = scu_readl(ctx->scu, reg, &val)) < 0) { - return rc; - } + if ((rc = scu_readl(ctx->scu, reg, &val)) < 0) { + return rc; + } - return scu_writel(ctx->scu, reg, val & ~(update & mask)); - } + return scu_writel(ctx->scu, reg, val & ~(update & mask)); + } - return -EINVAL; + return -EINVAL; } static const struct strap_ops ast2600_strap_ops = { - .read = ast2600_strap_read, - .set = ast2600_strap_set, - .clear = ast2600_strap_clear, + .read = ast2600_strap_read, + .set = ast2600_strap_set, + .clear = ast2600_strap_clear, }; static const struct soc_device_id strap_matches[] = { - { .compatible = "aspeed,ast2400-strapping", .data = &ast2400_strap_ops }, - { .compatible = "aspeed,ast2500-strapping", .data = &ast2500_strap_ops }, - { .compatible = "aspeed,ast2600-strapping", .data = &ast2600_strap_ops }, - { }, + { .compatible = "aspeed,ast2400-strapping", + .data = &ast2400_strap_ops }, + { .compatible = "aspeed,ast2500-strapping", + .data = &ast2500_strap_ops }, + { .compatible = "aspeed,ast2600-strapping", + .data = &ast2600_strap_ops }, + {}, }; static int strap_driver_init(struct soc *soc, struct soc_device *dev) { - struct strap *ctx; - int rc; + struct strap *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - ctx->scu = scu_get(soc); - if (!ctx->scu) { - rc = -ENODEV; - goto cleanup_ctx; - } + ctx->scu = scu_get(soc); + if (!ctx->scu) { + rc = -ENODEV; + goto cleanup_ctx; + } - ctx->ops = soc_device_get_match_data(soc, strap_matches, &dev->node); + ctx->ops = soc_device_get_match_data(soc, strap_matches, &dev->node); - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void strap_driver_destroy(struct soc_device *dev) { - struct strap *ctx = soc_device_get_drvdata(dev); - scu_put(ctx->scu); - free(ctx); + struct strap *ctx = soc_device_get_drvdata(dev); + scu_put(ctx->scu); + free(ctx); } static const struct soc_driver strap_driver = { - .name = "strap", - .matches = strap_matches, - .init = strap_driver_init, - .destroy = strap_driver_destroy, + .name = "strap", + .matches = strap_matches, + .init = strap_driver_init, + .destroy = strap_driver_destroy, }; REGISTER_SOC_DRIVER(strap_driver); struct strap *strap_get(struct soc *soc) { - return soc_driver_get_drvdata(soc, &strap_driver); + return soc_driver_get_drvdata(soc, &strap_driver); } diff --git a/src/soc/strap.h b/src/soc/strap.h index 41075f8..f1031af 100644 --- a/src/soc/strap.h +++ b/src/soc/strap.h @@ -8,13 +8,13 @@ #include -#define AST2400_SCU_HW_STRAP1 0x070 -#define AST2400_SCU_HW_STRAP2 0x0d0 -#define AST2500_SCU_HW_STRAP 0x070 -#define AST2500_SCU_SILICON_ID 0x07c -#define AST2600_SCU_HW_STRAP1 0x500 -#define AST2600_SCU_HW_STRAP2 0x510 -#define AST2600_SCU_HW_STRAP3 0x51c +#define AST2400_SCU_HW_STRAP1 0x070 +#define AST2400_SCU_HW_STRAP2 0x0d0 +#define AST2500_SCU_HW_STRAP 0x070 +#define AST2500_SCU_SILICON_ID 0x07c +#define AST2600_SCU_HW_STRAP1 0x500 +#define AST2600_SCU_HW_STRAP2 0x510 +#define AST2600_SCU_HW_STRAP3 0x51c struct strap; @@ -25,4 +25,3 @@ int strap_clear(struct strap *ctx, int reg, uint32_t update, uint32_t mask); struct strap *strap_get(struct soc *soc); #endif - diff --git a/src/soc/trace.c b/src/soc/trace.c index 1e500d5..a4cdb38 100644 --- a/src/soc/trace.c +++ b/src/soc/trace.c @@ -14,39 +14,39 @@ #include "soc.h" #include "trace.h" -#define AHBC_BUF_LEN (64 * 1024) - -#define R_AHBC_BCR_CSR 0x40 -#define AHBC_BCR_CSR_BUF_LEN_SHIFT 8 -#define AHBC_BCR_CSR_BUF_LEN_MASK GENMASK(10, 8) -#define AHBC_BCR_CSR_BUF_LEN_4K 0b000 -#define AHBC_BCR_CSR_BUF_LEN_8K 0b001 -#define AHBC_BCR_CSR_BUF_LEN_16K 0b010 -#define AHBC_BCR_CSR_BUF_LEN_32K 0b011 -#define AHBC_BCR_CSR_BUF_LEN_128K 0b100 -#define AHBC_BCR_CSR_BUF_LEN_256K 0b101 -#define AHBC_BCR_CSR_BUF_LEN_512K 0b110 -#define AHBC_BCR_CSR_BUF_LEN_1024K 0b111 - -#define AHBC_BCR_CSR_POLL_DATA_SHIFT 4 -#define AHBC_BCR_CSR_POLL_DATA_MASK GENMASK(6, 4) -#define AHBC_BCR_CSR_POLL_DATA_1_0 0b000 -#define AHBC_BCR_CSR_POLL_DATA_1_1 0b001 -#define AHBC_BCR_CSR_POLL_DATA_1_2 0b010 -#define AHBC_BCR_CSR_POLL_DATA_1_3 0b011 -#define AHBC_BCR_CSR_POLL_DATA_2_0 0b100 -#define AHBC_BCR_CSR_POLL_DATA_2_2 0b101 -#define AHBC_BCR_CSR_POLL_DATA_4_0 0b110 - -#define AHBC_BCR_CSR_FLUSH BIT(2) -#define AHBC_BCR_CSR_POLL_MODE BIT(1) - -#define AHBC_BCR_CSR_POLL_EN BIT(0) - -#define R_AHBC_BCR_BUF 0x44 -#define AHBC_BCR_BUF_WRAP BIT(0) -#define R_AHBC_BCR_ADDR 0x48 -#define R_AHBC_BCR_FIFO_MERGE 0x5C +#define AHBC_BUF_LEN (64 * 1024) + +#define R_AHBC_BCR_CSR 0x40 +#define AHBC_BCR_CSR_BUF_LEN_SHIFT 8 +#define AHBC_BCR_CSR_BUF_LEN_MASK GENMASK(10, 8) +#define AHBC_BCR_CSR_BUF_LEN_4K 0b000 +#define AHBC_BCR_CSR_BUF_LEN_8K 0b001 +#define AHBC_BCR_CSR_BUF_LEN_16K 0b010 +#define AHBC_BCR_CSR_BUF_LEN_32K 0b011 +#define AHBC_BCR_CSR_BUF_LEN_128K 0b100 +#define AHBC_BCR_CSR_BUF_LEN_256K 0b101 +#define AHBC_BCR_CSR_BUF_LEN_512K 0b110 +#define AHBC_BCR_CSR_BUF_LEN_1024K 0b111 + +#define AHBC_BCR_CSR_POLL_DATA_SHIFT 4 +#define AHBC_BCR_CSR_POLL_DATA_MASK GENMASK(6, 4) +#define AHBC_BCR_CSR_POLL_DATA_1_0 0b000 +#define AHBC_BCR_CSR_POLL_DATA_1_1 0b001 +#define AHBC_BCR_CSR_POLL_DATA_1_2 0b010 +#define AHBC_BCR_CSR_POLL_DATA_1_3 0b011 +#define AHBC_BCR_CSR_POLL_DATA_2_0 0b100 +#define AHBC_BCR_CSR_POLL_DATA_2_2 0b101 +#define AHBC_BCR_CSR_POLL_DATA_4_0 0b110 + +#define AHBC_BCR_CSR_FLUSH BIT(2) +#define AHBC_BCR_CSR_POLL_MODE BIT(1) + +#define AHBC_BCR_CSR_POLL_EN BIT(0) + +#define R_AHBC_BCR_BUF 0x44 +#define AHBC_BCR_BUF_WRAP BIT(0) +#define R_AHBC_BCR_ADDR 0x48 +#define R_AHBC_BCR_FIFO_MERGE 0x5C struct trace { struct soc *soc; @@ -55,261 +55,265 @@ struct trace { }; static const size_t ahbc_bcr_buf_len[] = { - [AHBC_BCR_CSR_BUF_LEN_4K] = (4 * 1024), - [AHBC_BCR_CSR_BUF_LEN_8K] = (8 * 1024), - [AHBC_BCR_CSR_BUF_LEN_16K] = (16 * 1024), - [AHBC_BCR_CSR_BUF_LEN_32K] = (32 * 1024), - [AHBC_BCR_CSR_BUF_LEN_128K] = (128 * 1024), - [AHBC_BCR_CSR_BUF_LEN_256K] = (256 * 1024), - [AHBC_BCR_CSR_BUF_LEN_512K] = (512 * 1024), - [AHBC_BCR_CSR_BUF_LEN_1024K] = (1024 * 1024), + [AHBC_BCR_CSR_BUF_LEN_4K] = (4 * 1024), + [AHBC_BCR_CSR_BUF_LEN_8K] = (8 * 1024), + [AHBC_BCR_CSR_BUF_LEN_16K] = (16 * 1024), + [AHBC_BCR_CSR_BUF_LEN_32K] = (32 * 1024), + [AHBC_BCR_CSR_BUF_LEN_128K] = (128 * 1024), + [AHBC_BCR_CSR_BUF_LEN_256K] = (256 * 1024), + [AHBC_BCR_CSR_BUF_LEN_512K] = (512 * 1024), + [AHBC_BCR_CSR_BUF_LEN_1024K] = (1024 * 1024), }; static int trace_style(int width, int offset) { - if (!(width == 1 || width == 2 || width == 4)) - return -EINVAL; + if (!(width == 1 || width == 2 || width == 4)) + return -EINVAL; - if (offset >= 4 || (offset & (width - 1))) - return -EINVAL; + if (offset >= 4 || (offset & (width - 1))) + return -EINVAL; - switch (width) { - case 1: - switch (offset) { - case 0: - return AHBC_BCR_CSR_POLL_DATA_1_0; + switch (width) { + case 1: + switch (offset) { + case 0: + return AHBC_BCR_CSR_POLL_DATA_1_0; - case 1: - return AHBC_BCR_CSR_POLL_DATA_1_1; + case 1: + return AHBC_BCR_CSR_POLL_DATA_1_1; - case 2: - return AHBC_BCR_CSR_POLL_DATA_1_2; + case 2: + return AHBC_BCR_CSR_POLL_DATA_1_2; - case 3: - return AHBC_BCR_CSR_POLL_DATA_1_3; + case 3: + return AHBC_BCR_CSR_POLL_DATA_1_3; - default: - assert(false); - } - break; + default: + assert(false); + } + break; - case 2: - assert(offset == 0 || offset == 2); + case 2: + assert(offset == 0 || offset == 2); - return offset == 0 - ? AHBC_BCR_CSR_POLL_DATA_2_0 - : AHBC_BCR_CSR_POLL_DATA_2_2; + return offset == 0 ? AHBC_BCR_CSR_POLL_DATA_2_0 : + AHBC_BCR_CSR_POLL_DATA_2_2; - case 4: - assert(offset == 0); + case 4: + assert(offset == 0); - return AHBC_BCR_CSR_POLL_DATA_4_0; - } + return AHBC_BCR_CSR_POLL_DATA_4_0; + } - assert(false); + assert(false); - return -EINVAL; + return -EINVAL; } static int ahbc_readl(struct trace *ctx, uint32_t off, uint32_t *val) { - return soc_readl(ctx->soc, ctx->ahbc.start + off, val); + return soc_readl(ctx->soc, ctx->ahbc.start + off, val); } static int ahbc_writel(struct trace *ctx, uint32_t off, uint32_t val) { - return soc_writel(ctx->soc, ctx->ahbc.start + off, val); + return soc_writel(ctx->soc, ctx->ahbc.start + off, val); } -int trace_start(struct trace *ctx, uint32_t addr, int width, enum trace_mode mode) +int trace_start(struct trace *ctx, uint32_t addr, int width, + enum trace_mode mode) { - uint32_t csr, buf; - size_t i; - int rc; + uint32_t csr, buf; + size_t i; + int rc; - logd("%s: 0x%08" PRIx32 " %d %d\n", __func__, addr, width, mode); + logd("%s: 0x%08" PRIx32 " %d %d\n", __func__, addr, width, mode); - assert(ctx->sram.length >= (32 * 1024)); - csr = AHBC_BCR_CSR_BUF_LEN_32K << AHBC_BCR_CSR_BUF_LEN_SHIFT; - csr |= AHBC_BCR_CSR_POLL_MODE * mode; + assert(ctx->sram.length >= (32 * 1024)); + csr = AHBC_BCR_CSR_BUF_LEN_32K << AHBC_BCR_CSR_BUF_LEN_SHIFT; + csr |= AHBC_BCR_CSR_POLL_MODE * mode; - if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) - return rc; + if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) + return rc; - if ((rc = ahbc_writel(ctx, R_AHBC_BCR_ADDR, addr & ~3))) - return rc; + if ((rc = ahbc_writel(ctx, R_AHBC_BCR_ADDR, addr & ~3))) + return rc; - logi("Zeroing trace buffer [%p - %p]\n", ctx->sram.start, ctx->sram.start + ctx->sram.length); + logi("Zeroing trace buffer [%p - %p]\n", ctx->sram.start, + ctx->sram.start + ctx->sram.length); - for (i = 0; i < (ctx->sram.length / 4); i++) { - soc_writel(ctx->soc, 4 * i + ctx->sram.start, 0); - } + for (i = 0; i < (ctx->sram.length / 4); i++) { + soc_writel(ctx->soc, 4 * i + ctx->sram.start, 0); + } - buf = ctx->sram.start | AHBC_BCR_BUF_WRAP; - if ((rc = ahbc_writel(ctx, R_AHBC_BCR_BUF, buf))) - return rc; + buf = ctx->sram.start | AHBC_BCR_BUF_WRAP; + if ((rc = ahbc_writel(ctx, R_AHBC_BCR_BUF, buf))) + return rc; - if ((rc = trace_style(width, addr & 3)) < 0) - return rc; + if ((rc = trace_style(width, addr & 3)) < 0) + return rc; - csr |= rc << AHBC_BCR_CSR_POLL_DATA_SHIFT; - csr |= AHBC_BCR_CSR_FLUSH; - csr |= AHBC_BCR_CSR_POLL_EN; + csr |= rc << AHBC_BCR_CSR_POLL_DATA_SHIFT; + csr |= AHBC_BCR_CSR_FLUSH; + csr |= AHBC_BCR_CSR_POLL_EN; - if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) - return rc; + if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) + return rc; - logi("Started AHB trace for 0x%08" PRIx32 "\n", addr); + logi("Started AHB trace for 0x%08" PRIx32 "\n", addr); - return 0; + return 0; } int trace_stop(struct trace *ctx) { - uint32_t csr; - int rc; + uint32_t csr; + int rc; - if ((rc = ahbc_readl(ctx, R_AHBC_BCR_CSR, &csr))) - return rc; + if ((rc = ahbc_readl(ctx, R_AHBC_BCR_CSR, &csr))) + return rc; - if (!(csr & AHBC_BCR_CSR_POLL_EN)) { - /* Tracing isn't running, we're done */ - return 0; - } + if (!(csr & AHBC_BCR_CSR_POLL_EN)) { + /* Tracing isn't running, we're done */ + return 0; + } - logt("%s: csr: 0x%08" PRIx32 "\n", __func__, csr); + logt("%s: csr: 0x%08" PRIx32 "\n", __func__, csr); - /* Note: This won't flush the tail values if they don't form a full word */ - csr |= AHBC_BCR_CSR_FLUSH; - if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) - return rc; + /* Note: This won't flush the tail values if they don't form a full word */ + csr |= AHBC_BCR_CSR_FLUSH; + if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) + return rc; - csr &= ~(AHBC_BCR_CSR_POLL_EN | AHBC_BCR_CSR_FLUSH); + csr &= ~(AHBC_BCR_CSR_POLL_EN | AHBC_BCR_CSR_FLUSH); - if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) - return rc; + if ((rc = ahbc_writel(ctx, R_AHBC_BCR_CSR, csr))) + return rc; - logi("Stopped AHB trace\n"); + logi("Stopped AHB trace\n"); - return 0; + return 0; } int trace_dump(struct trace *ctx, int outfd) { - uint32_t buf_len, write_ptr; - uint32_t csr, buf; - uint32_t merge; - uint32_t base; - bool wrapped; - ssize_t rc; - size_t len; - - if ((rc = ahbc_readl(ctx, R_AHBC_BCR_CSR, &csr))) - return rc; - - logt("%s: csr: 0x%08" PRIx32 "\n", __func__, csr); - - if ((rc = ahbc_readl(ctx, R_AHBC_BCR_BUF, &buf))) - return rc; - - logt("%s: buf: 0x%08" PRIx32 "\n", __func__, buf); - - /* - * 1 and 2 byte trace entries are accumulated in the merge FIFO. Once the - * merge FIFO has 4 bytes of data it's moved into the "real" FIFO regs and - * eventually flushed to the trace buffer. If you're tracing byte accesses - * you might not see anything flushed to the trace buffer, but it'll be in - * the merge FIFO. - */ - rc = ahbc_readl(ctx, R_AHBC_BCR_FIFO_MERGE, &merge); - if (!rc) - logi("%s: partial trace reg: 0x%08" PRIx32 "\n", __func__, merge); - - wrapped = buf & AHBC_BCR_BUF_WRAP; - buf &= ~AHBC_BCR_BUF_WRAP; - - buf_len = (csr & AHBC_BCR_CSR_BUF_LEN_MASK) >> AHBC_BCR_CSR_BUF_LEN_SHIFT; - write_ptr = buf & GENMASK((11 + buf_len), 2); - base = (buf & ~(write_ptr | 3)); - - if (wrapped) { - len = (base + ahbc_bcr_buf_len[buf_len]) - write_ptr; - - logd("Ring buffer has wrapped, dumping trace buffer from write pointer at 0x%" PRIx32 " for %zu\n", - buf, len); - if ((rc = soc_siphon_out(ctx->soc, buf, len, outfd))) - return rc; - } - - len = buf - base; - - logd("Dumping from trace buffer at 0x%" PRIx32 " for %zu\n", base, len); - - if ((rc = soc_siphon_out(ctx->soc, base, len, outfd))) - return rc; - - return rc; + uint32_t buf_len, write_ptr; + uint32_t csr, buf; + uint32_t merge; + uint32_t base; + bool wrapped; + ssize_t rc; + size_t len; + + if ((rc = ahbc_readl(ctx, R_AHBC_BCR_CSR, &csr))) + return rc; + + logt("%s: csr: 0x%08" PRIx32 "\n", __func__, csr); + + if ((rc = ahbc_readl(ctx, R_AHBC_BCR_BUF, &buf))) + return rc; + + logt("%s: buf: 0x%08" PRIx32 "\n", __func__, buf); + + /* + * 1 and 2 byte trace entries are accumulated in the merge FIFO. Once the + * merge FIFO has 4 bytes of data it's moved into the "real" FIFO regs and + * eventually flushed to the trace buffer. If you're tracing byte accesses + * you might not see anything flushed to the trace buffer, but it'll be in + * the merge FIFO. + */ + rc = ahbc_readl(ctx, R_AHBC_BCR_FIFO_MERGE, &merge); + if (!rc) + logi("%s: partial trace reg: 0x%08" PRIx32 "\n", __func__, + merge); + + wrapped = buf & AHBC_BCR_BUF_WRAP; + buf &= ~AHBC_BCR_BUF_WRAP; + + buf_len = (csr & AHBC_BCR_CSR_BUF_LEN_MASK) >> + AHBC_BCR_CSR_BUF_LEN_SHIFT; + write_ptr = buf & GENMASK((11 + buf_len), 2); + base = (buf & ~(write_ptr | 3)); + + if (wrapped) { + len = (base + ahbc_bcr_buf_len[buf_len]) - write_ptr; + + logd("Ring buffer has wrapped, dumping trace buffer from write pointer at 0x%" PRIx32 + " for %zu\n", + buf, len); + if ((rc = soc_siphon_out(ctx->soc, buf, len, outfd))) + return rc; + } + + len = buf - base; + + logd("Dumping from trace buffer at 0x%" PRIx32 " for %zu\n", base, len); + + if ((rc = soc_siphon_out(ctx->soc, base, len, outfd))) + return rc; + + return rc; } static const struct soc_device_id ahbc_match[] = { - { .compatible = "aspeed,ast2500-ahb-controller" }, - { .compatible = "aspeed,ast2600-ahb-controller" }, - { }, + { .compatible = "aspeed,ast2500-ahb-controller" }, + { .compatible = "aspeed,ast2600-ahb-controller" }, + {}, }; static int trace_driver_init(struct soc *soc, struct soc_device *dev) { - struct trace *ctx; - int rc; + struct trace *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - rc = soc_device_get_memory(soc, &dev->node, &ctx->ahbc); - if (rc < 0) { - goto cleanup_ctx; - } + rc = soc_device_get_memory(soc, &dev->node, &ctx->ahbc); + if (rc < 0) { + goto cleanup_ctx; + } - rc = soc_device_get_memory_region_named(soc, &dev->node, "trace-buffer", &ctx->sram); - if (rc < 0) { - goto cleanup_ctx; - } + rc = soc_device_get_memory_region_named(soc, &dev->node, "trace-buffer", + &ctx->sram); + if (rc < 0) { + goto cleanup_ctx; + } - logi("Found AHBC at 0x%" PRIx32 " and SRAM at 0x%" PRIx32 "\n", ctx->ahbc.start, - ctx->sram.start); + logi("Found AHBC at 0x%" PRIx32 " and SRAM at 0x%" PRIx32 "\n", + ctx->ahbc.start, ctx->sram.start); - ctx->soc = soc; + ctx->soc = soc; - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - if ((rc = trace_stop(ctx)) < 0) { - loge("Failed to stop trace: %d\n", rc); - } + if ((rc = trace_stop(ctx)) < 0) { + loge("Failed to stop trace: %d\n", rc); + } - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void trace_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } -static const struct soc_driver trace_driver = { - .name = "trace", - .matches = ahbc_match, - .init = trace_driver_init, - .destroy = trace_driver_destroy -}; +static const struct soc_driver trace_driver = { .name = "trace", + .matches = ahbc_match, + .init = trace_driver_init, + .destroy = + trace_driver_destroy }; REGISTER_SOC_DRIVER(trace_driver); struct trace *trace_get(struct soc *soc) { - return soc_driver_get_drvdata(soc, &trace_driver); + return soc_driver_get_drvdata(soc, &trace_driver); } diff --git a/src/soc/uart/mux.c b/src/soc/uart/mux.c index 49a6814..558b9bb 100644 --- a/src/soc/uart/mux.c +++ b/src/soc/uart/mux.c @@ -11,50 +11,50 @@ #include #include -#define LPC_HICR9 0x98 -#define LPC_HICR9_SEL6IO (0b1111 << 8) -#define LPC_HICRA 0x9c -#define LPC_HICRA_SEL5DW (0b1111 << 28) -#define LPC_HICRA_SEL4DW (0b111 << 25) -#define LPC_HICRA_SEL3DW (0b111 << 22) -#define LPC_HICRA_SEL2DW (0b111 << 19) -#define LPC_HICRA_SEL1DW (0b111 << 16) -#define LPC_HICRA_SEL5IO (0b111 << 12) -#define LPC_HICRA_SEL4IO (0b111 << 9) -#define LPC_HICRA_SEL3IO (0b111 << 6) -#define LPC_HICRA_SEL2IO (0b111 << 3) -#define LPC_HICRA_SEL1IO (0b111 << 0) +#define LPC_HICR9 0x98 +#define LPC_HICR9_SEL6IO (0b1111 << 8) +#define LPC_HICRA 0x9c +#define LPC_HICRA_SEL5DW (0b1111 << 28) +#define LPC_HICRA_SEL4DW (0b111 << 25) +#define LPC_HICRA_SEL3DW (0b111 << 22) +#define LPC_HICRA_SEL2DW (0b111 << 19) +#define LPC_HICRA_SEL1DW (0b111 << 16) +#define LPC_HICRA_SEL5IO (0b111 << 12) +#define LPC_HICRA_SEL4IO (0b111 << 9) +#define LPC_HICRA_SEL3IO (0b111 << 6) +#define LPC_HICRA_SEL2IO (0b111 << 3) +#define LPC_HICRA_SEL1IO (0b111 << 0) const struct mux_obj _mux_obj_io1 = { .type = mux_io, .io = io1 }, - *mux_obj_io1 = &_mux_obj_io1; + *mux_obj_io1 = &_mux_obj_io1; const struct mux_obj _mux_obj_io3 = { .type = mux_io, .io = io3 }, - *mux_obj_io3 = &_mux_obj_io3; + *mux_obj_io3 = &_mux_obj_io3; const struct mux_obj _mux_obj_uart1 = { .type = mux_uart, .uart = uart1 }, - *mux_obj_uart1 = &_mux_obj_uart1; + *mux_obj_uart1 = &_mux_obj_uart1; const struct mux_obj _mux_obj_uart2 = { .type = mux_uart, .uart = uart2 }, - *mux_obj_uart2 = &_mux_obj_uart2; + *mux_obj_uart2 = &_mux_obj_uart2; const struct mux_obj _mux_obj_uart3 = { .type = mux_uart, .uart = uart3 }, - *mux_obj_uart3 = &_mux_obj_uart3; + *mux_obj_uart3 = &_mux_obj_uart3; const struct mux_obj _mux_obj_uart5 = { .type = mux_uart, .uart = uart5 }, - *mux_obj_uart5 = &_mux_obj_uart5; + *mux_obj_uart5 = &_mux_obj_uart5; struct uart_mux { - struct soc *soc; - struct soc_region lpc; + struct soc *soc; + struct soc_region lpc; - uint32_t hicr9; - uint32_t hicra; + uint32_t hicr9; + uint32_t hicra; }; struct mux_desc { - uint32_t reg; - uint32_t mask; - uint8_t val; + uint32_t reg; + uint32_t mask; + uint8_t val; }; /* mux_io_count > mux_uart_count */ struct mux_lookup { - struct mux_desc lookup[mux_io_count][mux_io_count]; + struct mux_desc lookup[mux_io_count][mux_io_count]; }; static const struct mux_lookup mux_lookup[2][2] = { @@ -217,125 +217,125 @@ static const struct mux_lookup mux_lookup[2][2] = { static int lpc_readl(struct uart_mux *ctx, uint32_t offset, uint32_t *val) { - return soc_readl(ctx->soc, ctx->lpc.start + offset, val); + return soc_readl(ctx->soc, ctx->lpc.start + offset, val); } static int lpc_writel(struct uart_mux *ctx, uint32_t offset, uint32_t val) { - return soc_writel(ctx->soc, ctx->lpc.start + offset, val); + return soc_writel(ctx->soc, ctx->lpc.start + offset, val); } int uart_mux_restore(struct uart_mux *ctx) { - int rc; + int rc; - if ((rc = lpc_writel(ctx, LPC_HICR9, ctx->hicr9)) < 0) - return rc; + if ((rc = lpc_writel(ctx, LPC_HICR9, ctx->hicr9)) < 0) + return rc; - if ((rc = lpc_writel(ctx, LPC_HICRA, ctx->hicra)) < 0) - return rc; + if ((rc = lpc_writel(ctx, LPC_HICRA, ctx->hicra)) < 0) + return rc; - return 0; + return 0; } int uart_mux_route(struct uart_mux *ctx, const struct mux_obj *s, - const struct mux_obj *d) + const struct mux_obj *d) { - const struct mux_desc *md; - uint32_t val; - off_t si, di; - int rc; + const struct mux_desc *md; + uint32_t val; + off_t si, di; + int rc; - si = s->type == mux_uart ? s->uart : s->io; - di = d->type == mux_uart ? d->uart : d->io; - md = &mux_lookup[s->type][d->type].lookup[si][di]; - if (!md->mask) - return -EINVAL; + si = s->type == mux_uart ? s->uart : s->io; + di = d->type == mux_uart ? d->uart : d->io; + md = &mux_lookup[s->type][d->type].lookup[si][di]; + if (!md->mask) + return -EINVAL; - if ((rc = lpc_readl(ctx, md->reg, &val)) < 0) - return rc; + if ((rc = lpc_readl(ctx, md->reg, &val)) < 0) + return rc; - val &= ~(md->mask); - val |= md->val << (ffs(md->mask) - 1); + val &= ~(md->mask); + val |= md->val << (ffs(md->mask) - 1); - if ((rc = lpc_writel(ctx, md->reg, val)) < 0) - return rc; + if ((rc = lpc_writel(ctx, md->reg, val)) < 0) + return rc; - return 0; + return 0; } int uart_mux_connect(struct uart_mux *ctx, const struct mux_obj *a, - const struct mux_obj *b) + const struct mux_obj *b) { - int rc; + int rc; - if ((rc = uart_mux_route(ctx, a, b)) < 0) - return rc; + if ((rc = uart_mux_route(ctx, a, b)) < 0) + return rc; - if ((rc = uart_mux_route(ctx, b, a)) < 0) - return rc; + if ((rc = uart_mux_route(ctx, b, a)) < 0) + return rc; - return 0; + return 0; } static const struct soc_device_id lpc_match[] = { - { .compatible = "aspeed,ast2500-lpc-v2" }, - { }, + { .compatible = "aspeed,ast2500-lpc-v2" }, + {}, }; static int uart_mux_driver_init(struct soc *soc, struct soc_device *dev) { - struct uart_mux *ctx; - uint32_t val; - int rc; + struct uart_mux *ctx; + uint32_t val; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->lpc)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->lpc)) < 0) { + goto cleanup_ctx; + } - ctx->soc = soc; + ctx->soc = soc; - if ((rc = lpc_readl(ctx, LPC_HICR9, &val)) < 0) { - goto cleanup_ctx; - } + if ((rc = lpc_readl(ctx, LPC_HICR9, &val)) < 0) { + goto cleanup_ctx; + } - ctx->hicr9 = val; + ctx->hicr9 = val; - if ((rc = lpc_readl(ctx, LPC_HICRA, &val)) < 0) { - return rc; - } + if ((rc = lpc_readl(ctx, LPC_HICRA, &val)) < 0) { + return rc; + } - ctx->hicra = val; + ctx->hicra = val; - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void uart_mux_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } static const struct soc_driver uart_mux_driver = { - .name = "uart-mux", - .matches = lpc_match, - .init = uart_mux_driver_init, - .destroy = uart_mux_driver_destroy, + .name = "uart-mux", + .matches = lpc_match, + .init = uart_mux_driver_init, + .destroy = uart_mux_driver_destroy, }; REGISTER_SOC_DRIVER(uart_mux_driver); struct uart_mux *uart_mux_get(struct soc *soc) { - return soc_driver_get_drvdata(soc, &uart_mux_driver); + return soc_driver_get_drvdata(soc, &uart_mux_driver); } diff --git a/src/soc/uart/mux.h b/src/soc/uart/mux.h index 0ed7530..be288b9 100644 --- a/src/soc/uart/mux.h +++ b/src/soc/uart/mux.h @@ -13,11 +13,11 @@ enum mux_io { io1, io2, io3, io4, io5, io6, mux_io_count }; enum mux_uart { uart1, uart2, uart3, uart4, uart5, mux_uart_count }; struct mux_obj { - enum mux_type type; - union { - enum mux_io io; - enum mux_uart uart; - }; + enum mux_type type; + union { + enum mux_io io; + enum mux_uart uart; + }; }; extern const struct mux_obj *mux_obj_io1; @@ -33,11 +33,11 @@ int uart_mux_restore(struct uart_mux *ctx); /* Uni-directional connection */ int uart_mux_route(struct uart_mux *ctx, const struct mux_obj *s, - const struct mux_obj *d); + const struct mux_obj *d); /* Bi-directional connection */ int uart_mux_connect(struct uart_mux *ctx, const struct mux_obj *a, - const struct mux_obj *b); + const struct mux_obj *b); struct uart_mux *uart_mux_get(struct soc *soc); diff --git a/src/soc/uart/vuart.c b/src/soc/uart/vuart.c index 28b94aa..c2ab95a 100644 --- a/src/soc/uart/vuart.c +++ b/src/soc/uart/vuart.c @@ -6,8 +6,8 @@ #include -#define VUART_GCRA 0x20 -#define VUART_GCRA_TX_DISCARD (1 << 5) +#define VUART_GCRA 0x20 +#define VUART_GCRA_TX_DISCARD (1 << 5) struct vuart { struct soc *soc; @@ -15,72 +15,72 @@ struct vuart { }; static const struct soc_device_id vuart_match[] = { - { .compatible = "aspeed,ast2400-vuart" }, - { .compatible = "aspeed,ast2500-vuart" }, - { .compatible = "aspeed,ast2600-vuart" }, - { }, + { .compatible = "aspeed,ast2400-vuart" }, + { .compatible = "aspeed,ast2500-vuart" }, + { .compatible = "aspeed,ast2600-vuart" }, + {}, }; int vuart_set_host_tx_discard(struct vuart *ctx, enum vuart_discard state) { - uint32_t val; - int rc; + uint32_t val; + int rc; - if (!(state == discard_enable || state == discard_disable)) - return -EINVAL; + if (!(state == discard_enable || state == discard_disable)) + return -EINVAL; - rc = soc_readl(ctx->soc, ctx->iomem.start + VUART_GCRA, &val); - if (rc < 0) - return rc; + rc = soc_readl(ctx->soc, ctx->iomem.start + VUART_GCRA, &val); + if (rc < 0) + return rc; - if (state == discard_enable) - val &= ~VUART_GCRA_TX_DISCARD; - else - val |= VUART_GCRA_TX_DISCARD; + if (state == discard_enable) + val &= ~VUART_GCRA_TX_DISCARD; + else + val |= VUART_GCRA_TX_DISCARD; - return soc_writel(ctx->soc, ctx->iomem.start + VUART_GCRA, val); + return soc_writel(ctx->soc, ctx->iomem.start + VUART_GCRA, val); } static int vuart_driver_init(struct soc *soc, struct soc_device *dev) { - struct vuart *ctx; - int rc; + struct vuart *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { + goto cleanup_ctx; + } - ctx->soc = soc; + ctx->soc = soc; - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void vuart_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } static const struct soc_driver vuart_driver = { - .name = "vuart", - .matches = vuart_match, - .init = vuart_driver_init, - .destroy = vuart_driver_destroy, + .name = "vuart", + .matches = vuart_match, + .init = vuart_driver_init, + .destroy = vuart_driver_destroy, }; REGISTER_SOC_DRIVER(vuart_driver); struct vuart *vuart_get_by_name(struct soc *soc, const char *name) { - return soc_driver_get_drvdata_by_name(soc, &vuart_driver, name); + return soc_driver_get_drvdata_by_name(soc, &vuart_driver, name); } diff --git a/src/soc/wdt.c b/src/soc/wdt.c index 367199e..4b0071a 100644 --- a/src/soc/wdt.c +++ b/src/soc/wdt.c @@ -12,18 +12,18 @@ #include /* Registers */ -#define WDT_RELOAD 0x04 -#define WDT_RESTART 0x08 -#define WDT_RESTART_MAGIC 0x4755 -#define WDT_CTRL 0x0c -#define WDT_CTRL_ALT_BOOT (1 << 7) -#define WDT_CTRL_RESET_SOC (0b00 << 5) -#define WDT_CTRL_RESET_SYS (0b01 << 5) -#define WDT_CTRL_RESET_CPU (0b10 << 5) -#define WDT_CTRL_CLK_1MHZ (1 << 4) -#define WDT_CTRL_SYS_RESET (1 << 1) -#define WDT_CTRL_ENABLE (1 << 0) -#define WDT_RESET_MASK 0x1c +#define WDT_RELOAD 0x04 +#define WDT_RESTART 0x08 +#define WDT_RESTART_MAGIC 0x4755 +#define WDT_CTRL 0x0c +#define WDT_CTRL_ALT_BOOT (1 << 7) +#define WDT_CTRL_RESET_SOC (0b00 << 5) +#define WDT_CTRL_RESET_SYS (0b01 << 5) +#define WDT_CTRL_RESET_CPU (0b10 << 5) +#define WDT_CTRL_CLK_1MHZ (1 << 4) +#define WDT_CTRL_SYS_RESET (1 << 1) +#define WDT_CTRL_ENABLE (1 << 0) +#define WDT_RESET_MASK 0x1c struct wdt { struct soc *soc; @@ -33,211 +33,214 @@ struct wdt { static inline int wdt_readl(struct wdt *ctx, uint32_t reg, uint32_t *val) { - int rc; - rc = soc_readl(ctx->soc, ctx->iomem.start + reg, val); - logt("wdt_readl:\tbase: 0x%08" PRIx32 ", reg: 0x%02" PRIx32 ", val: 0x%08" - PRIx32 "\n", ctx->iomem.start, reg, *val); - return rc; + int rc; + rc = soc_readl(ctx->soc, ctx->iomem.start + reg, val); + logt("wdt_readl:\tbase: 0x%08" PRIx32 ", reg: 0x%02" PRIx32 + ", val: 0x%08" PRIx32 "\n", + ctx->iomem.start, reg, *val); + return rc; } static inline int wdt_writel(struct wdt *ctx, uint32_t reg, uint32_t val) { - logt("wdt_writel:\tbase: 0x%08" PRIx32 ", reg: 0x%02" PRIx32 ", val: 0x%08" - PRIx32 "\n", ctx->iomem.start, reg, val); - return soc_writel(ctx->soc, ctx->iomem.start + reg, val); + logt("wdt_writel:\tbase: 0x%08" PRIx32 ", reg: 0x%02" PRIx32 + ", val: 0x%08" PRIx32 "\n", + ctx->iomem.start, reg, val); + return soc_writel(ctx->soc, ctx->iomem.start + reg, val); } static int wdt_stop(struct wdt *ctx) { - uint32_t val; - int rc; + uint32_t val; + int rc; - rc = wdt_readl(ctx, WDT_CTRL, &val); - if (rc < 0) - return rc; + rc = wdt_readl(ctx, WDT_CTRL, &val); + if (rc < 0) + return rc; - val &= ~WDT_CTRL_ENABLE; + val &= ~WDT_CTRL_ENABLE; - return wdt_writel(ctx, WDT_CTRL, val); + return wdt_writel(ctx, WDT_CTRL, val); } static int wdt_config_clksrc(struct wdt *ctx) { - uint32_t val; - int rc; + uint32_t val; + int rc; - rc = wdt_readl(ctx, WDT_CTRL, &val); - if (rc < 0) - return rc; + rc = wdt_readl(ctx, WDT_CTRL, &val); + if (rc < 0) + return rc; - val |= WDT_CTRL_CLK_1MHZ; + val |= WDT_CTRL_CLK_1MHZ; - return wdt_writel(ctx, WDT_CTRL, val); + return wdt_writel(ctx, WDT_CTRL, val); } #define AST_WDT_MAX 3 int wdt_prevent_reset(struct soc *soc) { - /* FIXME: use for_each over the wdt dt nodes or something... */ - for (int i = 0; i < AST_WDT_MAX; i++) { - struct wdt _wdt, *wdt = &_wdt; - char name[] = "wdt1"; - int rc; - - /* ... but for now we YOLO */ - assert(i < 10); - name[3] = '1' + i; - - if (!(wdt = wdt_get_by_name(soc, name))) { - logd("Failed to acquire %s controller\n", name); - return -ENODEV; - } - - rc = wdt_stop(wdt); - if (rc < 0) - return rc; - } - - return 0; + /* FIXME: use for_each over the wdt dt nodes or something... */ + for (int i = 0; i < AST_WDT_MAX; i++) { + struct wdt _wdt, *wdt = &_wdt; + char name[] = "wdt1"; + int rc; + + /* ... but for now we YOLO */ + assert(i < 10); + name[3] = '1' + i; + + if (!(wdt = wdt_get_by_name(soc, name))) { + logd("Failed to acquire %s controller\n", name); + return -ENODEV; + } + + rc = wdt_stop(wdt); + if (rc < 0) + return rc; + } + + return 0; } static int64_t wdt_usecs_to_ticks(struct wdt *ctx, uint32_t usecs) { - uint32_t val; - int rc; + uint32_t val; + int rc; - rc = wdt_readl(ctx, WDT_CTRL, &val); - if (rc < 0) - return rc; + rc = wdt_readl(ctx, WDT_CTRL, &val); + if (rc < 0) + return rc; - /* Don't support PCLK as a source yet, involves scraping around in SCU */ - if (!(val & WDT_CTRL_CLK_1MHZ)) { - loge("wdt: PCLK source unsupported, bailing\n"); - return (int64_t)-EIO; - } + /* Don't support PCLK as a source yet, involves scraping around in SCU */ + if (!(val & WDT_CTRL_CLK_1MHZ)) { + loge("wdt: PCLK source unsupported, bailing\n"); + return (int64_t)-EIO; + } - return usecs; + return usecs; } int wdt_perform_reset(struct wdt *ctx) { - uint32_t mode; - int64_t wait; - int rc; - - if ((rc = wdt_stop(ctx)) < 0) - return rc; - - if ((rc = wdt_config_clksrc(ctx)) < 0) - return rc; - - /* Reset everything except SPI, X-DMA, MCTP and SDRAM */ - /* Explicitly, reset the AHB bridges */ - rc = wdt_writel(ctx, WDT_RESET_MASK, 0x23ffffb); - if (rc < 0) - return rc; - - /* Wait enough time to cover using the debug UART for a reset */ - wait = wdt_usecs_to_ticks(ctx, 5000000); - if (wait < 0) - return wait; - - rc = wdt_writel(ctx, WDT_RELOAD, wait); - if (rc < 0) - return rc; - - rc = wdt_writel(ctx, WDT_RESTART, WDT_RESTART_MAGIC); - if (rc < 0) - return rc; - - if ((rc = wdt_readl(ctx, WDT_CTRL, &mode)) < 0) - return rc; - - mode |= WDT_CTRL_RESET_SOC | WDT_CTRL_SYS_RESET | WDT_CTRL_ENABLE; - mode &= ~WDT_CTRL_ALT_BOOT; - - if ((rc = wdt_writel(ctx, WDT_CTRL, mode)) < 0) - return rc; - - if ((rc = ahb_release_bridge(ctx->soc->ahb)) < 0) - return rc; - - /* - * Allow a little extra time for reset to occur (we're timing this - * asynchronously after all) before we try to reinitialize the bridge - */ - wait += 1000000; - logd("Waiting %"PRId64" microseconds for watchdog timer to expire\n", wait); - usleep(wait); - - if ((rc = ahb_reinit_bridge(ctx->soc->ahb)) < 0) { - loge("Failed to reinitialize bridge after reset: %d\n", rc); - return rc; - } - - /* The ARM clock gate is sticky on reset?! Ensure it's clear */ - if ((rc = clk_enable(ctx->clk, clk_arm)) < 0) - return rc; - - rc = wdt_writel(ctx, WDT_RELOAD, 0); - if (rc < 0) - return rc; - - return 0; + uint32_t mode; + int64_t wait; + int rc; + + if ((rc = wdt_stop(ctx)) < 0) + return rc; + + if ((rc = wdt_config_clksrc(ctx)) < 0) + return rc; + + /* Reset everything except SPI, X-DMA, MCTP and SDRAM */ + /* Explicitly, reset the AHB bridges */ + rc = wdt_writel(ctx, WDT_RESET_MASK, 0x23ffffb); + if (rc < 0) + return rc; + + /* Wait enough time to cover using the debug UART for a reset */ + wait = wdt_usecs_to_ticks(ctx, 5000000); + if (wait < 0) + return wait; + + rc = wdt_writel(ctx, WDT_RELOAD, wait); + if (rc < 0) + return rc; + + rc = wdt_writel(ctx, WDT_RESTART, WDT_RESTART_MAGIC); + if (rc < 0) + return rc; + + if ((rc = wdt_readl(ctx, WDT_CTRL, &mode)) < 0) + return rc; + + mode |= WDT_CTRL_RESET_SOC | WDT_CTRL_SYS_RESET | WDT_CTRL_ENABLE; + mode &= ~WDT_CTRL_ALT_BOOT; + + if ((rc = wdt_writel(ctx, WDT_CTRL, mode)) < 0) + return rc; + + if ((rc = ahb_release_bridge(ctx->soc->ahb)) < 0) + return rc; + + /* + * Allow a little extra time for reset to occur (we're timing this + * asynchronously after all) before we try to reinitialize the bridge + */ + wait += 1000000; + logd("Waiting %" PRId64 " microseconds for watchdog timer to expire\n", + wait); + usleep(wait); + + if ((rc = ahb_reinit_bridge(ctx->soc->ahb)) < 0) { + loge("Failed to reinitialize bridge after reset: %d\n", rc); + return rc; + } + + /* The ARM clock gate is sticky on reset?! Ensure it's clear */ + if ((rc = clk_enable(ctx->clk, clk_arm)) < 0) + return rc; + + rc = wdt_writel(ctx, WDT_RELOAD, 0); + if (rc < 0) + return rc; + + return 0; } static const struct soc_device_id wdt_match[] = { - { .compatible = "aspeed,ast2500-wdt" }, - { }, + { .compatible = "aspeed,ast2500-wdt" }, + {}, }; static int wdt_driver_init(struct soc *soc, struct soc_device *dev) { - struct wdt *ctx; - int rc; + struct wdt *ctx; + int rc; - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - return -ENOMEM; - } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + return -ENOMEM; + } - if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { - goto cleanup_ctx; - } + if ((rc = soc_device_get_memory(soc, &dev->node, &ctx->iomem)) < 0) { + goto cleanup_ctx; + } - if (!(ctx->clk = clk_get(soc))) { - loge("Failed to acquire clock controller\n"); - rc = -ENODEV; - goto cleanup_ctx; - } + if (!(ctx->clk = clk_get(soc))) { + loge("Failed to acquire clock controller\n"); + rc = -ENODEV; + goto cleanup_ctx; + } - ctx->soc = soc; + ctx->soc = soc; - soc_device_set_drvdata(dev, ctx); + soc_device_set_drvdata(dev, ctx); - return 0; + return 0; cleanup_ctx: - free(ctx); + free(ctx); - return rc; + return rc; } static void wdt_driver_destroy(struct soc_device *dev) { - free(soc_device_get_drvdata(dev)); + free(soc_device_get_drvdata(dev)); } static const struct soc_driver wdt_driver = { - .name = "wdt", - .matches = wdt_match, - .init = wdt_driver_init, - .destroy = wdt_driver_destroy, + .name = "wdt", + .matches = wdt_match, + .init = wdt_driver_init, + .destroy = wdt_driver_destroy, }; REGISTER_SOC_DRIVER(wdt_driver); struct wdt *wdt_get_by_name(struct soc *soc, const char *name) { - return soc_driver_get_drvdata_by_name(soc, &wdt_driver, name); + return soc_driver_get_drvdata_by_name(soc, &wdt_driver, name); } diff --git a/src/ts16.c b/src/ts16.c index c026152..6a18f47 100644 --- a/src/ts16.c +++ b/src/ts16.c @@ -22,196 +22,232 @@ #define to_ts16(console) container_of(console, struct ts16, console) static int ts16_control_init(struct ts16 *ctx, const char *ip, - int port __unused, const char *username, - const char *password) + int port __unused, const char *username, + const char *password) { - struct sockaddr_in concentrator_addr; - int concentrator; - int rc, cleanup; - char *cmd; - - logi("Connecting to Digi Portserver TS 16 at %s:%d\n", ip, 23); - concentrator_addr.sin_family = AF_INET; - concentrator_addr.sin_port = htons(23); - if (inet_aton(ip, &concentrator_addr.sin_addr) == 0) - return -errno; - - concentrator = socket(AF_INET, SOCK_STREAM, 0); - if (concentrator < 0) { return -errno; } - - rc = connect(concentrator, (struct sockaddr *)&concentrator_addr, - sizeof(concentrator_addr)); - if (rc < 0) { rc = -errno; goto cleanup_concentrator; } - - rc = prompt_init(&ctx->concentrator, concentrator, "\r\n", false); - if (rc < 0) { goto cleanup_concentrator; }; - - logi("Logging into Digi Portserver TS\n"); - rc = prompt_expect_run(&ctx->concentrator, "login: ", username); - if (rc < 0) { rc = -errno; goto cleanup_concentrator_prompt; } - - rc = prompt_expect_run(&ctx->concentrator, "password: ", password); - if (rc < 0) { rc = -errno; goto cleanup_concentrator_prompt; } - - logi("Configuring binary mode on port %d\n", ctx->port); - rc = asprintf(&cmd, "set port range=%d bin=on", ctx->port); - if (rc < 0) { goto cleanup_concentrator_prompt; } - - rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (rc < 0) { goto cleanup_concentrator_prompt; } - - logi("Resetting port %d\n", ctx->port); - rc = asprintf(&cmd, "kill tty=%d", ctx->port); - if (rc < 0) { goto cleanup_port; } - - rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (rc < 0) { goto cleanup_port; } - - sleep(1); - - return 0; + struct sockaddr_in concentrator_addr; + int concentrator; + int rc, cleanup; + char *cmd; + + logi("Connecting to Digi Portserver TS 16 at %s:%d\n", ip, 23); + concentrator_addr.sin_family = AF_INET; + concentrator_addr.sin_port = htons(23); + if (inet_aton(ip, &concentrator_addr.sin_addr) == 0) + return -errno; + + concentrator = socket(AF_INET, SOCK_STREAM, 0); + if (concentrator < 0) { + return -errno; + } + + rc = connect(concentrator, (struct sockaddr *)&concentrator_addr, + sizeof(concentrator_addr)); + if (rc < 0) { + rc = -errno; + goto cleanup_concentrator; + } + + rc = prompt_init(&ctx->concentrator, concentrator, "\r\n", false); + if (rc < 0) { + goto cleanup_concentrator; + }; + + logi("Logging into Digi Portserver TS\n"); + rc = prompt_expect_run(&ctx->concentrator, "login: ", username); + if (rc < 0) { + rc = -errno; + goto cleanup_concentrator_prompt; + } + + rc = prompt_expect_run(&ctx->concentrator, "password: ", password); + if (rc < 0) { + rc = -errno; + goto cleanup_concentrator_prompt; + } + + logi("Configuring binary mode on port %d\n", ctx->port); + rc = asprintf(&cmd, "set port range=%d bin=on", ctx->port); + if (rc < 0) { + goto cleanup_concentrator_prompt; + } + + rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (rc < 0) { + goto cleanup_concentrator_prompt; + } + + logi("Resetting port %d\n", ctx->port); + rc = asprintf(&cmd, "kill tty=%d", ctx->port); + if (rc < 0) { + goto cleanup_port; + } + + rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (rc < 0) { + goto cleanup_port; + } + + sleep(1); + + return 0; cleanup_port: - logi("Disabling binary mode on port %d\n", ctx->port); - cleanup = asprintf(&cmd, "set port range=%d bin=off", ctx->port); - if (cleanup < 0 && !rc) { rc = cleanup; }; - - cleanup = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (cleanup < 0 && !rc) { rc = cleanup; }; - - logi("Resetting port %d\n", ctx->port); - cleanup = asprintf(&cmd, "kill tty=%d", ctx->port); - if (cleanup < 0 && !rc) { rc = cleanup; }; - - cleanup = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (cleanup < 0 && !rc) { rc = cleanup; }; - - sleep(1); + logi("Disabling binary mode on port %d\n", ctx->port); + cleanup = asprintf(&cmd, "set port range=%d bin=off", ctx->port); + if (cleanup < 0 && !rc) { + rc = cleanup; + }; + + cleanup = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (cleanup < 0 && !rc) { + rc = cleanup; + }; + + logi("Resetting port %d\n", ctx->port); + cleanup = asprintf(&cmd, "kill tty=%d", ctx->port); + if (cleanup < 0 && !rc) { + rc = cleanup; + }; + + cleanup = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (cleanup < 0 && !rc) { + rc = cleanup; + }; + + sleep(1); cleanup_concentrator_prompt: - cleanup = prompt_destroy(&ctx->concentrator); - if (cleanup < 0 && !rc) { rc = cleanup; } + cleanup = prompt_destroy(&ctx->concentrator); + if (cleanup < 0 && !rc) { + rc = cleanup; + } cleanup_concentrator: - close(concentrator); + close(concentrator); - return rc; + return rc; } static int ts16_control_destroy(struct ts16 *ctx) { - char *cmd; - int rc; + char *cmd; + int rc; - logi("Disabling binary mode on port %d\n", ctx->port); - rc = asprintf(&cmd, "set port range=%d bin=off", ctx->port); - if (rc < 0) - return -errno; + logi("Disabling binary mode on port %d\n", ctx->port); + rc = asprintf(&cmd, "set port range=%d bin=off", ctx->port); + if (rc < 0) + return -errno; - rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (rc < 0) - return rc; + rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (rc < 0) + return rc; - logi("Resetting port %d\n", ctx->port); - rc = asprintf(&cmd, "kill tty=%d", ctx->port); - if (rc < 0) - return rc; + logi("Resetting port %d\n", ctx->port); + rc = asprintf(&cmd, "kill tty=%d", ctx->port); + if (rc < 0) + return rc; - rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (rc < 0) - return rc; + rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (rc < 0) + return rc; - sleep(1); + sleep(1); - return prompt_destroy(&ctx->concentrator); + return prompt_destroy(&ctx->concentrator); } static int ts16_console_init(struct ts16 *ctx __unused, const char *ip, - int port) + int port) { - struct sockaddr_in console_addr; - int console; - int rc; - - /* Use the "raw" port on the Digi portserver, which is 2000 + 100 */ - logi("Connecting to BMC console at %s:%d\n", ip, 2000 + 100 + port); - console_addr.sin_family = AF_INET; - console_addr.sin_port = htons(2000 + 100 + port); - if (inet_aton(ip, &console_addr.sin_addr) == 0) - return -errno; - - console = socket(AF_INET, SOCK_STREAM, 0); - if (console < 0) { return console; } - - rc = connect(console, (struct sockaddr *)&console_addr, - sizeof(console_addr)); - if (rc < 0) { rc = -errno; goto cleanup_console; } - - return console; + struct sockaddr_in console_addr; + int console; + int rc; + + /* Use the "raw" port on the Digi portserver, which is 2000 + 100 */ + logi("Connecting to BMC console at %s:%d\n", ip, 2000 + 100 + port); + console_addr.sin_family = AF_INET; + console_addr.sin_port = htons(2000 + 100 + port); + if (inet_aton(ip, &console_addr.sin_addr) == 0) + return -errno; + + console = socket(AF_INET, SOCK_STREAM, 0); + if (console < 0) { + return console; + } + + rc = connect(console, (struct sockaddr *)&console_addr, + sizeof(console_addr)); + if (rc < 0) { + rc = -errno; + goto cleanup_console; + } + + return console; cleanup_console: - close(console); + close(console); - return rc; + return rc; } static int ts16_set_baud(struct console *console, int baud) { - struct ts16 *ctx = to_ts16(console); - char *cmd; - int rc; + struct ts16 *ctx = to_ts16(console); + char *cmd; + int rc; - rc = asprintf(&cmd, "set line range=%d baud=%d", ctx->port, baud); - if (rc < 0) - return -errno; + rc = asprintf(&cmd, "set line range=%d baud=%d", ctx->port, baud); + if (rc < 0) + return -errno; - rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); - free(cmd); - if (rc < 0) - return rc; + rc = prompt_expect_run(&ctx->concentrator, "#> ", cmd); + free(cmd); + if (rc < 0) + return rc; - sleep(1); + sleep(1); - return 0; + return 0; } static int ts16_destroy(struct console *console) { - struct ts16 *ctx = to_ts16(console); + struct ts16 *ctx = to_ts16(console); - ts16_control_destroy(ctx); + ts16_control_destroy(ctx); - free(ctx); + free(ctx); - return 0; + return 0; } static const struct console_ops ts16_ops = { - .set_baud = ts16_set_baud, - .destroy = ts16_destroy, + .set_baud = ts16_set_baud, + .destroy = ts16_destroy, }; int ts16_init(struct ts16 *ctx, const char *ip, int port, const char *username, - const char *password) + const char *password) { - int rc, fd; + int rc, fd; - ctx->port = port; - ctx->console.ops = &ts16_ops; + ctx->port = port; + ctx->console.ops = &ts16_ops; - rc = ts16_control_init(ctx, ip, port, username, password); - if (rc < 0) - return rc; + rc = ts16_control_init(ctx, ip, port, username, password); + if (rc < 0) + return rc; - fd = ts16_console_init(ctx, ip, port); - if (fd < 0) - ts16_control_destroy(ctx); + fd = ts16_console_init(ctx, ip, port); + if (fd < 0) + ts16_control_destroy(ctx); - return fd; + return fd; } diff --git a/src/ts16.h b/src/ts16.h index 512024c..8925b25 100644 --- a/src/ts16.h +++ b/src/ts16.h @@ -8,12 +8,12 @@ #include "prompt.h" struct ts16 { - struct console console; - struct prompt concentrator; - int port; + struct console console; + struct prompt concentrator; + int port; }; int ts16_init(struct ts16 *ctx, const char *ip, int port, const char *username, - const char *password); + const char *password); #endif diff --git a/src/tty.c b/src/tty.c index 94fa274..b91442e 100644 --- a/src/tty.c +++ b/src/tty.c @@ -19,89 +19,99 @@ #define to_tty(console) container_of(console, struct tty, console) struct baud_map { - int baud; - speed_t speed; + int baud; + speed_t speed; }; static const struct baud_map tty_baud_map[] = { - { 1200, B1200 }, - { 115200, B115200 }, - { 0, B0 }, /* Sentinel */ + { 1200, B1200 }, + { 115200, B115200 }, + { 0, B0 }, /* Sentinel */ }; -static speed_t tty_find_speed(int baud) { - int i; +static speed_t tty_find_speed(int baud) +{ + int i; - for (i = 0; tty_baud_map[i].baud && tty_baud_map[i].baud != baud; i++); + for (i = 0; tty_baud_map[i].baud && tty_baud_map[i].baud != baud; i++) + ; - return tty_baud_map[i].speed; + return tty_baud_map[i].speed; } static int tty_set_baud(struct console *console, int baud) { - struct tty *ctx = to_tty(console); - struct termios termios; - speed_t speed; - int rc; - - speed = tty_find_speed(baud); - - if (speed == B0) { - loge("Unable to find matching speed for %d\n", baud); - return -EINVAL; - } - - /* - * We do a sketchy borrow of the fd from the prompt to set the baud rate. - * We cache the fd internally so no magic is required - */ - rc = tcgetattr(ctx->fd, &termios); - if (rc < 0) { return -errno; } - - rc = cfsetospeed(&termios, speed); - if (rc < 0) { return -errno; } - - rc = cfsetispeed(&termios, speed); - if (rc < 0) { return -errno; } - - rc = tcsetattr(ctx->fd, TCSADRAIN, &termios); - if (rc < 0) { return -errno; } - - return 0; + struct tty *ctx = to_tty(console); + struct termios termios; + speed_t speed; + int rc; + + speed = tty_find_speed(baud); + + if (speed == B0) { + loge("Unable to find matching speed for %d\n", baud); + return -EINVAL; + } + + /* + * We do a sketchy borrow of the fd from the prompt to set the baud rate. + * We cache the fd internally so no magic is required + */ + rc = tcgetattr(ctx->fd, &termios); + if (rc < 0) { + return -errno; + } + + rc = cfsetospeed(&termios, speed); + if (rc < 0) { + return -errno; + } + + rc = cfsetispeed(&termios, speed); + if (rc < 0) { + return -errno; + } + + rc = tcsetattr(ctx->fd, TCSADRAIN, &termios); + if (rc < 0) { + return -errno; + } + + return 0; } static int tty_destroy(struct console *console) { - struct tty *ctx = to_tty(console); + struct tty *ctx = to_tty(console); - free(ctx); + free(ctx); - return 0; + return 0; } static const struct console_ops tty_ops = { - .destroy = tty_destroy, - .set_baud = tty_set_baud, + .destroy = tty_destroy, + .set_baud = tty_set_baud, }; /* Ownership of fd passes to the prompt associated with the debug instance */ int tty_init(struct tty *ctx, const char *path) { - struct termios termios; + struct termios termios; - ctx->console.ops = &tty_ops; + ctx->console.ops = &tty_ops; - logi("Opening %s\n", path); + logi("Opening %s\n", path); - ctx->fd = open(path, O_RDWR); - if (!ctx->fd) { - loge("Error opening %s: %d\n", path, strerror(errno)); - return -errno; - } + ctx->fd = open(path, O_RDWR); + if (!ctx->fd) { + loge("Error opening %s: %d\n", path, strerror(errno)); + return -errno; + } - tcgetattr(ctx->fd, &termios); - cfmakeraw(&termios); - tcsetattr(ctx->fd, TCSAFLUSH, &termios); + tcgetattr(ctx->fd, &termios); + cfmakeraw(&termios); + tcsetattr(ctx->fd, TCSAFLUSH, &termios); - return ctx->fd; + return ctx->fd; } diff --git a/src/uart/suart.c b/src/uart/suart.c index 516491b..9831146 100644 --- a/src/uart/suart.c +++ b/src/uart/suart.c @@ -12,389 +12,391 @@ #include "suart.h" #include "log.h" -#define UART_RBR 0x00 -#define UART_THR 0x00 -#define UART_DLL 0x00 -#define UART_IER 0x01 -#define UART_DLH 0x01 -#define UART_IIR 0x02 -#define UART_FCR 0x02 -#define UART_FCR_XMIT_RST (1 << 2) -#define UART_FCR_RCVR_RST (1 << 1) -#define UART_FCR_FIFO_EN (1 << 0) -#define UART_LCR 0x03 -#define UART_LCR_DLAB (1 << 7) -#define UART_LCR_BREAK (1 << 6) -#define UART_LCR_EPS (1 << 4) -#define UART_LCR_PEN (1 << 3) -#define UART_LCR_CLS_MASK 0x03 -#define UART_LCR_CLS_8 0x03 -#define UART_MCR 0x04 -#define UART_MCR_LOOP (1 << 4) -#define UART_MCR_OUT2 (1 << 3) -#define UART_MCR_OUT1 (1 << 2) -#define UART_MCR_NRTS (1 << 1) -#define UART_MCR_NDTR (1 << 0) -#define UART_LSR 0x05 -#define UART_LSR_ERROR (1 << 7) -#define UART_LSR_TEMT (1 << 6) -#define UART_LSR_THRE (1 << 5) -#define UART_LSR_BI (1 << 4) -#define UART_LSR_FE (1 << 3) -#define UART_LSR_PE (1 << 2) -#define UART_LSR_OE (1 << 1) -#define UART_LSR_DR (1 << 0) -#define UART_MSR 0x06 -#define UART_MSR_CTS_N (1 << 4) -#define UART_SCR 0x07 +#define UART_RBR 0x00 +#define UART_THR 0x00 +#define UART_DLL 0x00 +#define UART_IER 0x01 +#define UART_DLH 0x01 +#define UART_IIR 0x02 +#define UART_FCR 0x02 +#define UART_FCR_XMIT_RST (1 << 2) +#define UART_FCR_RCVR_RST (1 << 1) +#define UART_FCR_FIFO_EN (1 << 0) +#define UART_LCR 0x03 +#define UART_LCR_DLAB (1 << 7) +#define UART_LCR_BREAK (1 << 6) +#define UART_LCR_EPS (1 << 4) +#define UART_LCR_PEN (1 << 3) +#define UART_LCR_CLS_MASK 0x03 +#define UART_LCR_CLS_8 0x03 +#define UART_MCR 0x04 +#define UART_MCR_LOOP (1 << 4) +#define UART_MCR_OUT2 (1 << 3) +#define UART_MCR_OUT1 (1 << 2) +#define UART_MCR_NRTS (1 << 1) +#define UART_MCR_NDTR (1 << 0) +#define UART_LSR 0x05 +#define UART_LSR_ERROR (1 << 7) +#define UART_LSR_TEMT (1 << 6) +#define UART_LSR_THRE (1 << 5) +#define UART_LSR_BI (1 << 4) +#define UART_LSR_FE (1 << 3) +#define UART_LSR_PE (1 << 2) +#define UART_LSR_OE (1 << 1) +#define UART_LSR_DR (1 << 0) +#define UART_MSR 0x06 +#define UART_MSR_CTS_N (1 << 4) +#define UART_SCR 0x07 #define UART_DEFAULT_BAUD 115200 static inline uint16_t baud_to_divisor(int baud) { - /* Divide by 13 can get in the bin. So much debugging */ - return ((24000000 / 13) / (16 * baud)); + /* Divide by 13 can get in the bin. So much debugging */ + return ((24000000 / 13) / (16 * baud)); } static int __suart_init(struct suart *ctx, enum sio_dev dev, bool defaults, - uint16_t base, int sirq) + uint16_t base, int sirq) { - struct sio _sio, *sio = &_sio; - struct lpc *io = &ctx->io; - uint16_t divisor; - uint8_t data; - int cleanup; - int rc; - - switch (dev) { - case sio_suart1: - case sio_suart2: - case sio_suart3: - case sio_suart4: - break; - default: - return -EINVAL; - } - - - rc = sio_init(sio); - if (rc) - return rc; - - rc = sio_unlock(sio); - if (rc) - return rc; - - rc = sio_select(sio, dev); - if (rc) - goto sio_err; - - if (defaults) { - /* Grab the SUART's LPC base address, 16 bits in two byte-size registers */ - rc = sio_readb(sio, 0x60, &data); - if (rc) - goto sio_err; - - ctx->base = data << 8; - - rc = sio_readb(sio, 0x61, &data); - if (rc) - goto sio_err; - - ctx->base |= data; - - rc = sio_readb(sio, 0x70, &data); - if (rc) - goto sio_err; - - ctx->sirq = data; - } else { - rc = sio_writeb(sio, 0x60, base >> 8); - if (rc) - goto sio_err; - - rc = sio_writeb(sio, 0x61, base & 0xff); - if (rc) - goto sio_err; - - ctx->base = base; - - rc = sio_writeb(sio, 0x70, sirq); - if (rc) - goto sio_err; - - ctx->sirq = sirq; - } - - logd("SUART base address: 0x%x\n", ctx->base); - logd("SUART SIRQ: %d\n", ctx->sirq); - - /* Enable the SUART */ - rc = sio_writeb(sio, 0x30, 1); - if (rc) - goto sio_err; - - rc = sio_lock(sio); - if (rc) { - errno = -rc; - perror("sio_lock"); - } - - rc = sio_destroy(sio); - if (rc) - return rc; - - /* Init LPC */ - rc = lpc_init(io, "io"); - if (rc) - return rc; - - /* Disable interrupts, will be polling */ - rc = lpc_writeb(io, ctx->base + UART_IER, 0); - if (rc) - goto cleanup_lpc; - - /* Setup Loop/DTR/RTS signal control */ - rc = lpc_writeb(io, ctx->base + UART_MCR, - (UART_MCR_OUT2 | UART_MCR_NRTS | UART_MCR_NDTR)); - if (rc) - goto cleanup_lpc; - - /* Configure 115200 8N1 */ - divisor = baud_to_divisor(UART_DEFAULT_BAUD); - rc = lpc_writeb(io, ctx->base + UART_LCR, - (UART_LCR_DLAB | UART_LCR_EPS | UART_LCR_CLS_8)); - if (rc) - goto cleanup_lpc; - - rc = lpc_writeb(io, ctx->base + UART_DLH, (uint8_t)(divisor >> 8)); - if (rc) - goto cleanup_lpc; - - rc = lpc_writeb(io, ctx->base + UART_DLL, divisor & 0xff); - if (rc) - goto cleanup_lpc; - - rc = lpc_writeb(io, ctx->base + UART_LCR, (UART_LCR_EPS | UART_LCR_CLS_8)); - if (rc) - goto cleanup_lpc; - - /* Polled FIFO Mode */ - return lpc_writeb(io, ctx->base + UART_FCR, - (UART_FCR_XMIT_RST | UART_FCR_RCVR_RST | UART_FCR_FIFO_EN)); + struct sio _sio, *sio = &_sio; + struct lpc *io = &ctx->io; + uint16_t divisor; + uint8_t data; + int cleanup; + int rc; + + switch (dev) { + case sio_suart1: + case sio_suart2: + case sio_suart3: + case sio_suart4: + break; + default: + return -EINVAL; + } + + rc = sio_init(sio); + if (rc) + return rc; + + rc = sio_unlock(sio); + if (rc) + return rc; + + rc = sio_select(sio, dev); + if (rc) + goto sio_err; + + if (defaults) { + /* Grab the SUART's LPC base address, 16 bits in two byte-size registers */ + rc = sio_readb(sio, 0x60, &data); + if (rc) + goto sio_err; + + ctx->base = data << 8; + + rc = sio_readb(sio, 0x61, &data); + if (rc) + goto sio_err; + + ctx->base |= data; + + rc = sio_readb(sio, 0x70, &data); + if (rc) + goto sio_err; + + ctx->sirq = data; + } else { + rc = sio_writeb(sio, 0x60, base >> 8); + if (rc) + goto sio_err; + + rc = sio_writeb(sio, 0x61, base & 0xff); + if (rc) + goto sio_err; + + ctx->base = base; + + rc = sio_writeb(sio, 0x70, sirq); + if (rc) + goto sio_err; + + ctx->sirq = sirq; + } + + logd("SUART base address: 0x%x\n", ctx->base); + logd("SUART SIRQ: %d\n", ctx->sirq); + + /* Enable the SUART */ + rc = sio_writeb(sio, 0x30, 1); + if (rc) + goto sio_err; + + rc = sio_lock(sio); + if (rc) { + errno = -rc; + perror("sio_lock"); + } + + rc = sio_destroy(sio); + if (rc) + return rc; + + /* Init LPC */ + rc = lpc_init(io, "io"); + if (rc) + return rc; + + /* Disable interrupts, will be polling */ + rc = lpc_writeb(io, ctx->base + UART_IER, 0); + if (rc) + goto cleanup_lpc; + + /* Setup Loop/DTR/RTS signal control */ + rc = lpc_writeb(io, ctx->base + UART_MCR, + (UART_MCR_OUT2 | UART_MCR_NRTS | UART_MCR_NDTR)); + if (rc) + goto cleanup_lpc; + + /* Configure 115200 8N1 */ + divisor = baud_to_divisor(UART_DEFAULT_BAUD); + rc = lpc_writeb(io, ctx->base + UART_LCR, + (UART_LCR_DLAB | UART_LCR_EPS | UART_LCR_CLS_8)); + if (rc) + goto cleanup_lpc; + + rc = lpc_writeb(io, ctx->base + UART_DLH, (uint8_t)(divisor >> 8)); + if (rc) + goto cleanup_lpc; + + rc = lpc_writeb(io, ctx->base + UART_DLL, divisor & 0xff); + if (rc) + goto cleanup_lpc; + + rc = lpc_writeb(io, ctx->base + UART_LCR, + (UART_LCR_EPS | UART_LCR_CLS_8)); + if (rc) + goto cleanup_lpc; + + /* Polled FIFO Mode */ + return lpc_writeb(io, ctx->base + UART_FCR, + (UART_FCR_XMIT_RST | UART_FCR_RCVR_RST | + UART_FCR_FIFO_EN)); cleanup_lpc: - cleanup = lpc_destroy(io); - if (cleanup) { - errno = -cleanup; - perror("lpc_destroy"); - } + cleanup = lpc_destroy(io); + if (cleanup) { + errno = -cleanup; + perror("lpc_destroy"); + } - return rc; + return rc; sio_err: - cleanup = sio_lock(sio); - if (cleanup) { - errno = -cleanup; - perror("sio_lock"); - } - - cleanup = sio_destroy(sio); - if (cleanup) { - errno = -cleanup; - perror("sio_destroy"); - } - - return rc; + cleanup = sio_lock(sio); + if (cleanup) { + errno = -cleanup; + perror("sio_lock"); + } + + cleanup = sio_destroy(sio); + if (cleanup) { + errno = -cleanup; + perror("sio_destroy"); + } + + return rc; } int suart_init_defaults(struct suart *ctx, enum sio_dev dev) { - return __suart_init(ctx, dev, true, 0, 0); + return __suart_init(ctx, dev, true, 0, 0); } int suart_init(struct suart *ctx, enum sio_dev dev, uint16_t base, int sirq) { - return __suart_init(ctx, dev, false, base, sirq); + return __suart_init(ctx, dev, false, base, sirq); } int suart_destroy(struct suart *ctx) { - struct sio _sio, *sio = &_sio; - int cleanup; - int rc; + struct sio _sio, *sio = &_sio; + int cleanup; + int rc; - rc = sio_init(sio); - if (rc) - return rc; + rc = sio_init(sio); + if (rc) + return rc; - rc = sio_unlock(sio); - if (rc) - return rc; + rc = sio_unlock(sio); + if (rc) + return rc; - rc = sio_select(sio, ctx->dev); - if (rc) - goto sio_err; + rc = sio_select(sio, ctx->dev); + if (rc) + goto sio_err; - /* Disable the SUART */ - rc = sio_writeb(sio, 0x30, 0); - if (rc) - goto sio_err; + /* Disable the SUART */ + rc = sio_writeb(sio, 0x30, 0); + if (rc) + goto sio_err; sio_err: - cleanup = sio_lock(sio); - if (cleanup) { - errno = -cleanup; - perror("sio_lock"); - } - - cleanup = sio_destroy(sio); - if (cleanup) { - errno = -cleanup; - perror("sio_destroy"); - } - - return rc; + cleanup = sio_lock(sio); + if (cleanup) { + errno = -cleanup; + perror("sio_lock"); + } + + cleanup = sio_destroy(sio); + if (cleanup) { + errno = -cleanup; + perror("sio_destroy"); + } + + return rc; } int suart_set_baud(struct suart *ctx, int rate) { - struct lpc *io = &ctx->io; - uint16_t divisor; - uint8_t lcr, lsr; - int rc; + struct lpc *io = &ctx->io; + uint16_t divisor; + uint8_t lcr, lsr; + int rc; - rc = lpc_readb(io, ctx->base + UART_LCR, &lcr); - if (rc) - return rc; + rc = lpc_readb(io, ctx->base + UART_LCR, &lcr); + if (rc) + return rc; - rc = lpc_writeb(io, ctx->base + UART_LCR, (lcr | UART_LCR_DLAB)); - if (rc) - return rc; + rc = lpc_writeb(io, ctx->base + UART_LCR, (lcr | UART_LCR_DLAB)); + if (rc) + return rc; - divisor = baud_to_divisor(rate); - rc = lpc_writeb(io, ctx->base + UART_DLH, (uint8_t)(divisor >> 8)); - if (rc) - return rc; + divisor = baud_to_divisor(rate); + rc = lpc_writeb(io, ctx->base + UART_DLH, (uint8_t)(divisor >> 8)); + if (rc) + return rc; - rc = lpc_writeb(io, ctx->base + UART_DLL, divisor & 0xff); - if (rc) - return rc; + rc = lpc_writeb(io, ctx->base + UART_DLL, divisor & 0xff); + if (rc) + return rc; - rc = lpc_writeb(io, ctx->base + UART_LCR, (lcr & ~UART_LCR_DLAB)); - if (rc) - return rc; + rc = lpc_writeb(io, ctx->base + UART_LCR, (lcr & ~UART_LCR_DLAB)); + if (rc) + return rc; - /* Reset the FIFOs to ensure any baud rate weirdness is gone */ - rc = lpc_writeb(&ctx->io, ctx->base + UART_FCR, - (UART_FCR_RCVR_RST | UART_FCR_XMIT_RST | UART_FCR_FIFO_EN)); - if (rc) - return rc; + /* Reset the FIFOs to ensure any baud rate weirdness is gone */ + rc = lpc_writeb(&ctx->io, ctx->base + UART_FCR, + (UART_FCR_RCVR_RST | UART_FCR_XMIT_RST | + UART_FCR_FIFO_EN)); + if (rc) + return rc; - rc = lpc_readb(io, ctx->base + UART_LSR, &lsr); - if (rc) - return rc; + rc = lpc_readb(io, ctx->base + UART_LSR, &lsr); + if (rc) + return rc; - if (lsr & UART_LSR_ERROR) - loge("Found error state after FIFO reset: 0x%x\n", lsr); + if (lsr & UART_LSR_ERROR) + loge("Found error state after FIFO reset: 0x%x\n", lsr); - ctx->baud = rate; + ctx->baud = rate; - return 0; + return 0; } ssize_t suart_write(struct suart *ctx, const char *buf, size_t len) { - struct lpc *io = &ctx->io; - size_t slots = 16; - uint8_t lsr; - int rc; - - if (!len) - return len; - - rc = lpc_readb(io, ctx->base + UART_LSR, &lsr); - if (rc) - return rc; - - if (lsr & UART_LSR_ERROR) { - loge("Error condition asserted: 0x%x\n", lsr); - if (lsr & UART_LSR_BI) - loge("Break condition asserted\n"); - if (lsr & UART_LSR_FE) - loge("Framing error condition asserted\n"); - if (lsr & UART_LSR_PE) - loge("Parity error condition asserted\n"); - return -EIO; - } - - if (lsr & UART_LSR_OE) - loge("Overrun condition asserted\n"); - - if (lsr & UART_LSR_DR) { - /* We want to go read RBR ASAP */ - return len; - } - - if (!(lsr & UART_LSR_THRE)) - return len; - - while (len && slots) { - rc = lpc_writeb(io, ctx->base + UART_THR, *buf++); - if (rc) - return rc; - - len--; - slots--; - } - - return len; + struct lpc *io = &ctx->io; + size_t slots = 16; + uint8_t lsr; + int rc; + + if (!len) + return len; + + rc = lpc_readb(io, ctx->base + UART_LSR, &lsr); + if (rc) + return rc; + + if (lsr & UART_LSR_ERROR) { + loge("Error condition asserted: 0x%x\n", lsr); + if (lsr & UART_LSR_BI) + loge("Break condition asserted\n"); + if (lsr & UART_LSR_FE) + loge("Framing error condition asserted\n"); + if (lsr & UART_LSR_PE) + loge("Parity error condition asserted\n"); + return -EIO; + } + + if (lsr & UART_LSR_OE) + loge("Overrun condition asserted\n"); + + if (lsr & UART_LSR_DR) { + /* We want to go read RBR ASAP */ + return len; + } + + if (!(lsr & UART_LSR_THRE)) + return len; + + while (len && slots) { + rc = lpc_writeb(io, ctx->base + UART_THR, *buf++); + if (rc) + return rc; + + len--; + slots--; + } + + return len; } ssize_t suart_read(struct suart *ctx, char *buf, size_t len) { - struct lpc *io = &ctx->io; - char *end = buf + len; - uint8_t lsr; - int retry; - int rc; - - rc = lpc_readb(io, ctx->base + UART_LSR, &lsr); - if (rc) - return rc; - - if (lsr & UART_LSR_ERROR) { - loge("Error condition asserted: 0x%x\n", lsr); - if (lsr & UART_LSR_BI) - loge("Break condition asserted\n"); - if (lsr & UART_LSR_FE) - loge("Framing error condition asserted\n"); - if (lsr & UART_LSR_PE) - loge("Parity error condition asserted\n"); - return -EIO; - } - - if (lsr & UART_LSR_OE) - loge("Overrun condition asserted\n"); - - if (!(lsr & UART_LSR_DR)) - return 0; - - retry = 100; - do { - while (!(rc = lpc_readb(io, ctx->base + UART_LSR, &lsr)) && - (lsr & UART_LSR_DR) && buf < end) { - rc = lpc_readb(io, ctx->base + UART_RBR, (uint8_t *)buf++); - if (rc) - return rc; - } - } while (--retry); - - - if (rc) - return rc; - - return buf - (end - len); + struct lpc *io = &ctx->io; + char *end = buf + len; + uint8_t lsr; + int retry; + int rc; + + rc = lpc_readb(io, ctx->base + UART_LSR, &lsr); + if (rc) + return rc; + + if (lsr & UART_LSR_ERROR) { + loge("Error condition asserted: 0x%x\n", lsr); + if (lsr & UART_LSR_BI) + loge("Break condition asserted\n"); + if (lsr & UART_LSR_FE) + loge("Framing error condition asserted\n"); + if (lsr & UART_LSR_PE) + loge("Parity error condition asserted\n"); + return -EIO; + } + + if (lsr & UART_LSR_OE) + loge("Overrun condition asserted\n"); + + if (!(lsr & UART_LSR_DR)) + return 0; + + retry = 100; + do { + while (!(rc = lpc_readb(io, ctx->base + UART_LSR, &lsr)) && + (lsr & UART_LSR_DR) && buf < end) { + rc = lpc_readb(io, ctx->base + UART_RBR, + (uint8_t *)buf++); + if (rc) + return rc; + } + } while (--retry); + + if (rc) + return rc; + + return buf - (end - len); } /* @@ -403,142 +405,146 @@ ssize_t suart_read(struct suart *ctx, char *buf, size_t len) */ int suart_run(struct suart *ctx, int uin, int uout) { - char _uout_buf[1024], *uout_buf; - char _uin_buf[16], *uin_buf; - struct epoll_event epevent; - int epfd; - int rc; - - epevent.events = EPOLLIN | EPOLLERR; - epevent.data.fd = uin; - - epfd = epoll_create1(0); - rc = epoll_ctl(epfd, EPOLL_CTL_ADD, uin, &epevent); - if (rc == -1) - return -errno; - - while ((rc = epoll_wait(epfd, &epevent, 1, 50)) != -1) { - if (rc) { - const char *uin_end; - ssize_t uin_write; - ssize_t uin_read; - - uin_buf = &_uin_buf[0]; - assert(rc == 1); - - uin_read = read(uin, uin_buf, sizeof(_uin_buf)); - if (uin_read == -1) - return -errno; - - uin_end = uin_buf + uin_read; - uin_write = uin_read; - while (uin_read > 0) { - ssize_t uout_wrote; - ssize_t uout_read; - - uin_write = suart_write(ctx, uin_end - uin_write, uin_write); - if (uin_write < 0) - return uin_write; - - uout_buf = &_uout_buf[0]; - uout_read = suart_read(ctx, uout_buf, sizeof(_uout_buf)); - if (uout_read < 0) - return uout_read; - - while (uout_read > 0) { - uout_wrote = write(uout, uout_buf, uout_read); - if (uout_wrote == -1) - return -errno; - - uout_read -= uout_wrote; - uout_buf += uout_wrote; - } - - uin_read = uin_write; - } - } else { - ssize_t uout_wrote; - ssize_t uout_read; - - uout_buf = &_uout_buf[0]; - uout_read = suart_read(ctx, uout_buf, sizeof(_uout_buf)); - if (uout_read < 0) - return uout_read; - - while (uout_read > 0) { - uout_wrote = write(uout, uout_buf, uout_read); - if (uout_wrote == -1) - return -errno; - - uout_read -= uout_wrote; - uout_buf += uout_wrote; - } - } - } - - if (rc == -1) - return -errno; - - return 0; + char _uout_buf[1024], *uout_buf; + char _uin_buf[16], *uin_buf; + struct epoll_event epevent; + int epfd; + int rc; + + epevent.events = EPOLLIN | EPOLLERR; + epevent.data.fd = uin; + + epfd = epoll_create1(0); + rc = epoll_ctl(epfd, EPOLL_CTL_ADD, uin, &epevent); + if (rc == -1) + return -errno; + + while ((rc = epoll_wait(epfd, &epevent, 1, 50)) != -1) { + if (rc) { + const char *uin_end; + ssize_t uin_write; + ssize_t uin_read; + + uin_buf = &_uin_buf[0]; + assert(rc == 1); + + uin_read = read(uin, uin_buf, sizeof(_uin_buf)); + if (uin_read == -1) + return -errno; + + uin_end = uin_buf + uin_read; + uin_write = uin_read; + while (uin_read > 0) { + ssize_t uout_wrote; + ssize_t uout_read; + + uin_write = suart_write( + ctx, uin_end - uin_write, uin_write); + if (uin_write < 0) + return uin_write; + + uout_buf = &_uout_buf[0]; + uout_read = suart_read(ctx, uout_buf, + sizeof(_uout_buf)); + if (uout_read < 0) + return uout_read; + + while (uout_read > 0) { + uout_wrote = write(uout, uout_buf, + uout_read); + if (uout_wrote == -1) + return -errno; + + uout_read -= uout_wrote; + uout_buf += uout_wrote; + } + + uin_read = uin_write; + } + } else { + ssize_t uout_wrote; + ssize_t uout_read; + + uout_buf = &_uout_buf[0]; + uout_read = + suart_read(ctx, uout_buf, sizeof(_uout_buf)); + if (uout_read < 0) + return uout_read; + + while (uout_read > 0) { + uout_wrote = write(uout, uout_buf, uout_read); + if (uout_wrote == -1) + return -errno; + + uout_read -= uout_wrote; + uout_buf += uout_wrote; + } + } + } + + if (rc == -1) + return -errno; + + return 0; } ssize_t suart_flush(struct suart *ctx, const char *buf, size_t len) { - const char *end = buf + len; - ssize_t remaining = len; - int rc; + const char *end = buf + len; + ssize_t remaining = len; + int rc; - while (remaining > 0) { - /* Force a reset of the RCVR FIFO, we're flushing XMIT */ - rc = lpc_writeb(&ctx->io, ctx->base + UART_FCR, - (UART_FCR_RCVR_RST | UART_FCR_FIFO_EN)); - if (rc) - return rc; + while (remaining > 0) { + /* Force a reset of the RCVR FIFO, we're flushing XMIT */ + rc = lpc_writeb(&ctx->io, ctx->base + UART_FCR, + (UART_FCR_RCVR_RST | UART_FCR_FIFO_EN)); + if (rc) + return rc; - remaining = suart_write(ctx, end - remaining, remaining); - } + remaining = suart_write(ctx, end - remaining, remaining); + } - return remaining; + return remaining; } ssize_t suart_fill(struct suart *ctx, char *buf, size_t len) { - ssize_t remaining = len; - ssize_t consumed = 0; + ssize_t remaining = len; + ssize_t consumed = 0; - while (remaining) { - consumed = suart_read(ctx, buf, remaining); - if (consumed < 0) - return consumed; + while (remaining) { + consumed = suart_read(ctx, buf, remaining); + if (consumed < 0) + return consumed; - remaining -= consumed; - buf += consumed; + remaining -= consumed; + buf += consumed; - sleep(1); - } + sleep(1); + } - return len - remaining; + return len - remaining; } ssize_t suart_fill_until(struct suart *ctx, char *buf, size_t len, char term) { - ssize_t remaining = len; - ssize_t consumed = 0; + ssize_t remaining = len; + ssize_t consumed = 0; - if (len > SSIZE_MAX) - return -EINVAL; + if (len > SSIZE_MAX) + return -EINVAL; - /* Condition is broken when reading bursts of multiple characters */ - while ((remaining < (ssize_t)len && *(buf - 1) != term) || remaining) { - consumed = suart_read(ctx, buf, remaining); - if (consumed < 0) - return consumed; + /* Condition is broken when reading bursts of multiple characters */ + while ((remaining < (ssize_t)len && *(buf - 1) != term) || remaining) { + consumed = suart_read(ctx, buf, remaining); + if (consumed < 0) + return consumed; - remaining -= consumed; - buf += consumed; + remaining -= consumed; + buf += consumed; - sleep(1); - } + sleep(1); + } - return len - remaining; + return len - remaining; } diff --git a/src/uart/suart.h b/src/uart/suart.h index 7a86e9f..0d92aaa 100644 --- a/src/uart/suart.h +++ b/src/uart/suart.h @@ -10,13 +10,12 @@ #include "../lpc.h" #include "../sio.h" -struct suart -{ - enum sio_dev dev; - struct lpc io; - uint8_t sirq; - uint16_t base; - uint16_t baud; +struct suart { + enum sio_dev dev; + struct lpc io; + uint8_t sirq; + uint16_t base; + uint16_t baud; }; /* If base is 0 the hardware default is selected */