Home | History | Annotate | Download | only in reset
      1 // SPDX-License-Identifier: GPL-2.0
      2 /*
      3  * Copyright (c) 2016, NVIDIA CORPORATION.
      4  */
      5 
      6 #include <common.h>
      7 #include <dm.h>
      8 #include <fdtdec.h>
      9 #include <reset.h>
     10 #include <reset-uclass.h>
     11 
     12 static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
     13 {
     14 	return (struct reset_ops *)dev->driver->ops;
     15 }
     16 
     17 static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
     18 				  struct ofnode_phandle_args *args)
     19 {
     20 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
     21 
     22 	if (args->args_count != 1) {
     23 		debug("Invaild args_count: %d\n", args->args_count);
     24 		return -EINVAL;
     25 	}
     26 
     27 	reset_ctl->id = args->args[0];
     28 
     29 	return 0;
     30 }
     31 
     32 int reset_get_by_index(struct udevice *dev, int index,
     33 		       struct reset_ctl *reset_ctl)
     34 {
     35 	struct ofnode_phandle_args args;
     36 	int ret;
     37 	struct udevice *dev_reset;
     38 	struct reset_ops *ops;
     39 
     40 	debug("%s(dev=%p, index=%d, reset_ctl=%p)\n", __func__, dev, index,
     41 	      reset_ctl);
     42 	reset_ctl->dev = NULL;
     43 
     44 	ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
     45 					  index, &args);
     46 	if (ret) {
     47 		debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n",
     48 		      __func__, ret);
     49 		return ret;
     50 	}
     51 
     52 	ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node,
     53 					  &dev_reset);
     54 	if (ret) {
     55 		debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
     56 		      __func__, ret);
     57 		debug("%s %d\n", ofnode_get_name(args.node), args.args[0]);
     58 		return ret;
     59 	}
     60 	ops = reset_dev_ops(dev_reset);
     61 
     62 	reset_ctl->dev = dev_reset;
     63 	if (ops->of_xlate)
     64 		ret = ops->of_xlate(reset_ctl, &args);
     65 	else
     66 		ret = reset_of_xlate_default(reset_ctl, &args);
     67 	if (ret) {
     68 		debug("of_xlate() failed: %d\n", ret);
     69 		return ret;
     70 	}
     71 
     72 	ret = ops->request(reset_ctl);
     73 	if (ret) {
     74 		debug("ops->request() failed: %d\n", ret);
     75 		return ret;
     76 	}
     77 
     78 	return 0;
     79 }
     80 
     81 int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
     82 {
     83 	int i, ret, err, count;
     84 
     85 	bulk->count = 0;
     86 
     87 	count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
     88 	if (count < 1)
     89 		return count;
     90 
     91 	bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
     92 				    GFP_KERNEL);
     93 	if (!bulk->resets)
     94 		return -ENOMEM;
     95 
     96 	for (i = 0; i < count; i++) {
     97 		ret = reset_get_by_index(dev, i, &bulk->resets[i]);
     98 		if (ret < 0)
     99 			goto bulk_get_err;
    100 
    101 		++bulk->count;
    102 	}
    103 
    104 	return 0;
    105 
    106 bulk_get_err:
    107 	err = reset_release_all(bulk->resets, bulk->count);
    108 	if (err)
    109 		debug("%s: could release all resets for %p\n",
    110 		      __func__, dev);
    111 
    112 	return ret;
    113 }
    114 
    115 int reset_get_by_name(struct udevice *dev, const char *name,
    116 		     struct reset_ctl *reset_ctl)
    117 {
    118 	int index;
    119 
    120 	debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
    121 	      reset_ctl);
    122 	reset_ctl->dev = NULL;
    123 
    124 	index = dev_read_stringlist_search(dev, "reset-names", name);
    125 	if (index < 0) {
    126 		debug("fdt_stringlist_search() failed: %d\n", index);
    127 		return index;
    128 	}
    129 
    130 	return reset_get_by_index(dev, index, reset_ctl);
    131 }
    132 
    133 int reset_request(struct reset_ctl *reset_ctl)
    134 {
    135 	struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
    136 
    137 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
    138 
    139 	return ops->request(reset_ctl);
    140 }
    141 
    142 int reset_free(struct reset_ctl *reset_ctl)
    143 {
    144 	struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
    145 
    146 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
    147 
    148 	return ops->free(reset_ctl);
    149 }
    150 
    151 int reset_assert(struct reset_ctl *reset_ctl)
    152 {
    153 	struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
    154 
    155 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
    156 
    157 	return ops->rst_assert(reset_ctl);
    158 }
    159 
    160 int reset_assert_bulk(struct reset_ctl_bulk *bulk)
    161 {
    162 	int i, ret;
    163 
    164 	for (i = 0; i < bulk->count; i++) {
    165 		ret = reset_assert(&bulk->resets[i]);
    166 		if (ret < 0)
    167 			return ret;
    168 	}
    169 
    170 	return 0;
    171 }
    172 
    173 int reset_deassert(struct reset_ctl *reset_ctl)
    174 {
    175 	struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
    176 
    177 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
    178 
    179 	return ops->rst_deassert(reset_ctl);
    180 }
    181 
    182 int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
    183 {
    184 	int i, ret;
    185 
    186 	for (i = 0; i < bulk->count; i++) {
    187 		ret = reset_deassert(&bulk->resets[i]);
    188 		if (ret < 0)
    189 			return ret;
    190 	}
    191 
    192 	return 0;
    193 }
    194 
    195 int reset_release_all(struct reset_ctl *reset_ctl, int count)
    196 {
    197 	int i, ret;
    198 
    199 	for (i = 0; i < count; i++) {
    200 		debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
    201 
    202 		/* check if reset has been previously requested */
    203 		if (!reset_ctl[i].dev)
    204 			continue;
    205 
    206 		ret = reset_assert(&reset_ctl[i]);
    207 		if (ret)
    208 			return ret;
    209 
    210 		ret = reset_free(&reset_ctl[i]);
    211 		if (ret)
    212 			return ret;
    213 	}
    214 
    215 	return 0;
    216 }
    217 
    218 UCLASS_DRIVER(reset) = {
    219 	.id		= UCLASS_RESET,
    220 	.name		= "reset",
    221 };
    222