967 lines · 23.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * AHCI SATA platform library * * Copyright 2004-2005  Red Hat, Inc. *   Jeff Garzik <jgarzik@pobox.com> * Copyright 2010  MontaVista Software, LLC. *   Anton Vorontsov <avorontsov@ru.mvista.com> */ #include <linux/clk.h>#include <linux/kernel.h>#include <linux/gfp.h>#include <linux/module.h>#include <linux/pm.h>#include <linux/interrupt.h>#include <linux/device.h>#include <linux/platform_device.h>#include <linux/libata.h>#include <linux/ahci_platform.h>#include <linux/phy/phy.h>#include <linux/pm_runtime.h>#include <linux/of.h>#include <linux/of_platform.h>#include <linux/reset.h>#include "ahci.h" static void ahci_host_stop(struct ata_host *host); struct ata_port_operations ahci_platform_ops = {	.inherits	= &ahci_ops,	.host_stop	= ahci_host_stop,};EXPORT_SYMBOL_GPL(ahci_platform_ops); /** * ahci_platform_enable_phys - Enable PHYs * @hpriv: host private area to store config values * * This function enables all the PHYs found in hpriv->phys, if any. * If a PHY fails to be enabled, it disables all the PHYs already * enabled in reverse order and returns an error. * * RETURNS: * 0 on success otherwise a negative error code */int ahci_platform_enable_phys(struct ahci_host_priv *hpriv){	int rc, i; 	for (i = 0; i < hpriv->nports; i++) {		if (ahci_ignore_port(hpriv, i))			continue; 		rc = phy_init(hpriv->phys[i]);		if (rc)			goto disable_phys; 		rc = phy_set_mode(hpriv->phys[i], PHY_MODE_SATA);		if (rc) {			phy_exit(hpriv->phys[i]);			goto disable_phys;		} 		rc = phy_power_on(hpriv->phys[i]);		if (rc) {			phy_exit(hpriv->phys[i]);			goto disable_phys;		}	} 	return 0; disable_phys:	while (--i >= 0) {		if (ahci_ignore_port(hpriv, i))			continue; 		phy_power_off(hpriv->phys[i]);		phy_exit(hpriv->phys[i]);	}	return rc;}EXPORT_SYMBOL_GPL(ahci_platform_enable_phys); /** * ahci_platform_disable_phys - Disable PHYs * @hpriv: host private area to store config values * * This function disables all PHYs found in hpriv->phys. */void ahci_platform_disable_phys(struct ahci_host_priv *hpriv){	int i; 	for (i = 0; i < hpriv->nports; i++) {		if (ahci_ignore_port(hpriv, i))			continue; 		phy_power_off(hpriv->phys[i]);		phy_exit(hpriv->phys[i]);	}}EXPORT_SYMBOL_GPL(ahci_platform_disable_phys); /** * ahci_platform_find_clk - Find platform clock * @hpriv: host private area to store config values * @con_id: clock connection ID * * This function returns a pointer to the clock descriptor of the clock with * the passed ID. * * RETURNS: * Pointer to the clock descriptor on success otherwise NULL */struct clk *ahci_platform_find_clk(struct ahci_host_priv *hpriv, const char *con_id){	int i; 	for (i = 0; i < hpriv->n_clks; i++) {		if (hpriv->clks[i].id && !strcmp(hpriv->clks[i].id, con_id))			return hpriv->clks[i].clk;	} 	return NULL;}EXPORT_SYMBOL_GPL(ahci_platform_find_clk); /** * ahci_platform_enable_clks - Enable platform clocks * @hpriv: host private area to store config values * * This function enables all the clks found for the AHCI device. * * RETURNS: * 0 on success otherwise a negative error code */int ahci_platform_enable_clks(struct ahci_host_priv *hpriv){	return clk_bulk_prepare_enable(hpriv->n_clks, hpriv->clks);}EXPORT_SYMBOL_GPL(ahci_platform_enable_clks); /** * ahci_platform_disable_clks - Disable platform clocks * @hpriv: host private area to store config values * * This function disables all the clocks enabled before * (bulk-clocks-disable function is supposed to do that in reverse * from the enabling procedure order). */void ahci_platform_disable_clks(struct ahci_host_priv *hpriv){	clk_bulk_disable_unprepare(hpriv->n_clks, hpriv->clks);}EXPORT_SYMBOL_GPL(ahci_platform_disable_clks); /** * ahci_platform_deassert_rsts - Deassert/trigger platform resets * @hpriv: host private area to store config values * * This function deasserts or triggers all the reset lines found for * the AHCI device. * * RETURNS: * 0 on success otherwise a negative error code */int ahci_platform_deassert_rsts(struct ahci_host_priv *hpriv){	if (hpriv->f_rsts & AHCI_PLATFORM_RST_TRIGGER)		return reset_control_reset(hpriv->rsts); 	return reset_control_deassert(hpriv->rsts);}EXPORT_SYMBOL_GPL(ahci_platform_deassert_rsts); /** * ahci_platform_assert_rsts - Assert/rearm platform resets * @hpriv: host private area to store config values * * This function asserts or rearms (for self-deasserting resets) all * the reset controls found for the AHCI device. * * RETURNS: * 0 on success otherwise a negative error code */int ahci_platform_assert_rsts(struct ahci_host_priv *hpriv){	if (hpriv->f_rsts & AHCI_PLATFORM_RST_TRIGGER)		return reset_control_rearm(hpriv->rsts); 	return reset_control_assert(hpriv->rsts);}EXPORT_SYMBOL_GPL(ahci_platform_assert_rsts); /** * ahci_platform_enable_regulators - Enable regulators * @hpriv: host private area to store config values * * This function enables all the regulators found in controller and * hpriv->target_pwrs, if any.  If a regulator fails to be enabled, it * disables all the regulators already enabled in reverse order and * returns an error. * * RETURNS: * 0 on success otherwise a negative error code */int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv){	int rc, i; 	rc = regulator_enable(hpriv->ahci_regulator);	if (rc)		return rc; 	rc = regulator_enable(hpriv->phy_regulator);	if (rc)		goto disable_ahci_pwrs; 	for (i = 0; i < hpriv->nports; i++) {		if (!hpriv->target_pwrs[i])			continue; 		rc = regulator_enable(hpriv->target_pwrs[i]);		if (rc)			goto disable_target_pwrs;	} 	return 0; disable_target_pwrs:	while (--i >= 0)		if (hpriv->target_pwrs[i])			regulator_disable(hpriv->target_pwrs[i]); 	regulator_disable(hpriv->phy_regulator);disable_ahci_pwrs:	regulator_disable(hpriv->ahci_regulator);	return rc;}EXPORT_SYMBOL_GPL(ahci_platform_enable_regulators); /** * ahci_platform_disable_regulators - Disable regulators * @hpriv: host private area to store config values * * This function disables all regulators found in hpriv->target_pwrs and * AHCI controller. */void ahci_platform_disable_regulators(struct ahci_host_priv *hpriv){	int i; 	for (i = 0; i < hpriv->nports; i++) {		if (!hpriv->target_pwrs[i])			continue;		regulator_disable(hpriv->target_pwrs[i]);	} 	regulator_disable(hpriv->ahci_regulator);	regulator_disable(hpriv->phy_regulator);}EXPORT_SYMBOL_GPL(ahci_platform_disable_regulators);/** * ahci_platform_enable_resources - Enable platform resources * @hpriv: host private area to store config values * * This function enables all ahci_platform managed resources in the * following order: * 1) Regulator * 2) Clocks (through ahci_platform_enable_clks) * 3) Resets * 4) Phys * * If resource enabling fails at any point the previous enabled resources * are disabled in reverse order. * * RETURNS: * 0 on success otherwise a negative error code */int ahci_platform_enable_resources(struct ahci_host_priv *hpriv){	int rc; 	rc = ahci_platform_enable_regulators(hpriv);	if (rc)		return rc; 	rc = ahci_platform_enable_clks(hpriv);	if (rc)		goto disable_regulator; 	rc = ahci_platform_deassert_rsts(hpriv);	if (rc)		goto disable_clks; 	rc = ahci_platform_enable_phys(hpriv);	if (rc)		goto disable_rsts; 	return 0; disable_rsts:	ahci_platform_assert_rsts(hpriv); disable_clks:	ahci_platform_disable_clks(hpriv); disable_regulator:	ahci_platform_disable_regulators(hpriv); 	return rc;}EXPORT_SYMBOL_GPL(ahci_platform_enable_resources); /** * ahci_platform_disable_resources - Disable platform resources * @hpriv: host private area to store config values * * This function disables all ahci_platform managed resources in the * following order: * 1) Phys * 2) Resets * 3) Clocks (through ahci_platform_disable_clks) * 4) Regulator */void ahci_platform_disable_resources(struct ahci_host_priv *hpriv){	ahci_platform_disable_phys(hpriv); 	ahci_platform_assert_rsts(hpriv); 	ahci_platform_disable_clks(hpriv); 	ahci_platform_disable_regulators(hpriv);}EXPORT_SYMBOL_GPL(ahci_platform_disable_resources); static void ahci_platform_put_resources(struct device *dev, void *res){	struct ahci_host_priv *hpriv = res;	int c; 	if (hpriv->got_runtime_pm) {		pm_runtime_put_sync(dev);		pm_runtime_disable(dev);	} 	/*	 * The regulators are tied to child node device and not to the	 * SATA device itself. So we can't use devm for automatically	 * releasing them. We have to do it manually here.	 */	for (c = 0; c < hpriv->nports; c++)		if (hpriv->target_pwrs && hpriv->target_pwrs[c])			regulator_put(hpriv->target_pwrs[c]); 	kfree(hpriv->target_pwrs);} static int ahci_platform_get_phy(struct ahci_host_priv *hpriv, u32 port,				struct device *dev, struct device_node *node){	int rc; 	hpriv->phys[port] = devm_of_phy_get(dev, node, NULL); 	if (!IS_ERR(hpriv->phys[port]))		return 0; 	rc = PTR_ERR(hpriv->phys[port]);	switch (rc) {	case -ENOSYS:		/* No PHY support. Check if PHY is required. */		if (of_property_present(node, "phys")) {			dev_err(dev,				"couldn't get PHY in node %pOFn: ENOSYS\n",				node);			break;		}		fallthrough;	case -ENODEV:		/* continue normally */		hpriv->phys[port] = NULL;		rc = 0;		break;	case -EPROBE_DEFER:		/* Do not complain yet */		break; 	default:		dev_err(dev,			"couldn't get PHY in node %pOFn: %d\n",			node, rc); 		break;	} 	return rc;} static int ahci_platform_get_regulator(struct ahci_host_priv *hpriv, u32 port,				struct device *dev){	struct regulator *target_pwr;	int rc = 0; 	target_pwr = regulator_get(dev, "target"); 	if (!IS_ERR(target_pwr))		hpriv->target_pwrs[port] = target_pwr;	else		rc = PTR_ERR(target_pwr); 	return rc;} static int ahci_platform_get_firmware(struct ahci_host_priv *hpriv,				      struct device *dev){	u32 port; 	if (!of_property_read_u32(dev->of_node, "hba-cap", &hpriv->saved_cap))		hpriv->saved_cap &= (HOST_CAP_SSS | HOST_CAP_MPS); 	of_property_read_u32(dev->of_node,			     "ports-implemented", &hpriv->saved_port_map); 	for_each_child_of_node_scoped(dev->of_node, child) {		if (!of_device_is_available(child))			continue; 		if (of_property_read_u32(child, "reg", &port))			return -EINVAL; 		if (!of_property_read_u32(child, "hba-port-cap", &hpriv->saved_port_cap[port]))			hpriv->saved_port_cap[port] &= PORT_CMD_CAP;	} 	return 0;} static u32 ahci_platform_find_max_port_id(struct device *dev){	u32 max_port = 0; 	for_each_child_of_node_scoped(dev->of_node, child) {		u32 port; 		if (!of_property_read_u32(child, "reg", &port))			max_port = max(max_port, port);	} 	return max_port;} /** * ahci_platform_get_resources - Get platform resources * @pdev: platform device to get resources for * @flags: bitmap representing the resource to get * * This function allocates an ahci_host_priv struct, and gets the following * resources, storing a reference to them inside the returned struct: * * 1) mmio registers (IORESOURCE_MEM 0, mandatory) * 2) regulator for controlling the targets power (optional) *    regulator for controlling the AHCI controller (optional) * 3) all clocks specified in the devicetree node, or a single *    clock for non-OF platforms (optional) * 4) resets, if flags has AHCI_PLATFORM_GET_RESETS (optional) * 5) phys (optional) * * RETURNS: * The allocated ahci_host_priv on success, otherwise an ERR_PTR value */struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,						   unsigned int flags){	int child_nodes, rc = -ENOMEM, enabled_ports = 0;	struct device *dev = &pdev->dev;	struct ahci_host_priv *hpriv;	u32 mask_port_map = 0;	u32 max_port;	int nports; 	if (!devres_open_group(dev, NULL, GFP_KERNEL))		return ERR_PTR(-ENOMEM); 	/* find maximum port id for allocating structures */	max_port = ahci_platform_find_max_port_id(dev);	/*	 * Set nports according to maximum port id. Clamp at	 * AHCI_MAX_PORTS, warning message for invalid port id	 * is generated later.	 * When DT has no sub-nodes max_port is 0, nports is 1,	 * in order to be able to use the	 * ahci_platform_[en|dis]able_[phys|regulators] functions.	 */	nports = min(AHCI_MAX_PORTS, max_port + 1);

Lines 1–500 of 967.