Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2012 |
| 6 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <asm/io.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 15 | #include <dm/test.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 16 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 18 | |
| 19 | int dm_testdrv_op_count[DM_TEST_OP_COUNT]; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 20 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 21 | static int testdrv_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 22 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 23 | const struct dm_test_pdata *pdata = dev_get_plat(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 24 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 25 | |
| 26 | *pingret = pingval + pdata->ping_add; |
| 27 | priv->ping_total += *pingret; |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static const struct test_ops test_ops = { |
| 33 | .ping = testdrv_ping, |
| 34 | }; |
| 35 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 36 | static int test_bind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 37 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 38 | struct unit_test_state *uts = test_get_state(); |
| 39 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 40 | /* Private data should not be allocated */ |
| 41 | ut_assert(!dev_get_priv(dev)); |
| 42 | |
| 43 | dm_testdrv_op_count[DM_TEST_OP_BIND]++; |
| 44 | return 0; |
| 45 | } |
| 46 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 47 | static int test_probe(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 48 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 49 | struct unit_test_state *uts = test_get_state(); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 50 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 51 | |
| 52 | /* Private data should be allocated */ |
| 53 | ut_assert(priv); |
| 54 | |
| 55 | dm_testdrv_op_count[DM_TEST_OP_PROBE]++; |
| 56 | priv->ping_total += DM_TEST_START_TOTAL; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 60 | static int test_remove(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 61 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 62 | struct unit_test_state *uts = test_get_state(); |
| 63 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 64 | /* Private data should still be allocated */ |
| 65 | ut_assert(dev_get_priv(dev)); |
| 66 | |
| 67 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; |
| 68 | return 0; |
| 69 | } |
| 70 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 71 | static int test_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 72 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 73 | struct unit_test_state *uts = test_get_state(); |
| 74 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 75 | /* Private data should not be allocated */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 76 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 77 | |
| 78 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | U_BOOT_DRIVER(test_drv) = { |
| 83 | .name = "test_drv", |
| 84 | .id = UCLASS_TEST, |
| 85 | .ops = &test_ops, |
| 86 | .bind = test_bind, |
| 87 | .probe = test_probe, |
| 88 | .remove = test_remove, |
| 89 | .unbind = test_unbind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 90 | .priv_auto = sizeof(struct dm_test_priv), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | U_BOOT_DRIVER(test2_drv) = { |
| 94 | .name = "test2_drv", |
| 95 | .id = UCLASS_TEST, |
| 96 | .ops = &test_ops, |
| 97 | .bind = test_bind, |
| 98 | .probe = test_probe, |
| 99 | .remove = test_remove, |
| 100 | .unbind = test_unbind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 101 | .priv_auto = sizeof(struct dm_test_priv), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 104 | static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 105 | { |
| 106 | *pingret = pingval + 2; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static const struct test_ops test_manual_ops = { |
| 112 | .ping = test_manual_drv_ping, |
| 113 | }; |
| 114 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 115 | static int test_manual_bind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 116 | { |
| 117 | dm_testdrv_op_count[DM_TEST_OP_BIND]++; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 122 | static int test_manual_probe(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 123 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 124 | struct unit_test_state *uts = test_get_state(); |
| 125 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 126 | dm_testdrv_op_count[DM_TEST_OP_PROBE]++; |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 127 | if (!uts->force_fail_alloc) |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 128 | dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv))); |
| 129 | if (!dev_get_priv(dev)) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 130 | return -ENOMEM; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 135 | static int test_manual_remove(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 136 | { |
| 137 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; |
| 138 | return 0; |
| 139 | } |
| 140 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 141 | static int test_manual_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 142 | { |
| 143 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | U_BOOT_DRIVER(test_manual_drv) = { |
| 148 | .name = "test_manual_drv", |
| 149 | .id = UCLASS_TEST, |
| 150 | .ops = &test_manual_ops, |
| 151 | .bind = test_manual_bind, |
| 152 | .probe = test_manual_probe, |
| 153 | .remove = test_manual_remove, |
| 154 | .unbind = test_manual_unbind, |
| 155 | }; |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 156 | |
| 157 | U_BOOT_DRIVER(test_pre_reloc_drv) = { |
| 158 | .name = "test_pre_reloc_drv", |
| 159 | .id = UCLASS_TEST, |
| 160 | .ops = &test_manual_ops, |
| 161 | .bind = test_manual_bind, |
| 162 | .probe = test_manual_probe, |
| 163 | .remove = test_manual_remove, |
| 164 | .unbind = test_manual_unbind, |
| 165 | .flags = DM_FLAG_PRE_RELOC, |
| 166 | }; |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 167 | |
| 168 | U_BOOT_DRIVER(test_act_dma_drv) = { |
| 169 | .name = "test_act_dma_drv", |
| 170 | .id = UCLASS_TEST, |
| 171 | .ops = &test_manual_ops, |
| 172 | .bind = test_manual_bind, |
| 173 | .probe = test_manual_probe, |
| 174 | .remove = test_manual_remove, |
| 175 | .unbind = test_manual_unbind, |
| 176 | .flags = DM_FLAG_ACTIVE_DMA, |
| 177 | }; |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 178 | |
| 179 | U_BOOT_DRIVER(test_vital_clk_drv) = { |
| 180 | .name = "test_vital_clk_drv", |
| 181 | .id = UCLASS_TEST, |
| 182 | .ops = &test_manual_ops, |
| 183 | .bind = test_manual_bind, |
| 184 | .probe = test_manual_probe, |
| 185 | .remove = test_manual_remove, |
| 186 | .unbind = test_manual_unbind, |
| 187 | .flags = DM_FLAG_VITAL, |
| 188 | }; |
| 189 | |
| 190 | U_BOOT_DRIVER(test_act_dma_vital_clk_drv) = { |
| 191 | .name = "test_act_dma_vital_clk_drv", |
| 192 | .id = UCLASS_TEST, |
| 193 | .ops = &test_manual_ops, |
| 194 | .bind = test_manual_bind, |
| 195 | .probe = test_manual_probe, |
| 196 | .remove = test_manual_remove, |
| 197 | .unbind = test_manual_unbind, |
| 198 | .flags = DM_FLAG_VITAL | DM_FLAG_ACTIVE_DMA, |
| 199 | }; |