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