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 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 16 | #include <dm/test.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 17 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 19 | |
| 20 | int dm_testdrv_op_count[DM_TEST_OP_COUNT]; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 21 | static struct unit_test_state *uts = &global_dm_test_state; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 22 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 23 | static int testdrv_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 24 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 25 | const struct dm_test_pdata *pdata = dev_get_plat(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 26 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 27 | |
| 28 | *pingret = pingval + pdata->ping_add; |
| 29 | priv->ping_total += *pingret; |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static const struct test_ops test_ops = { |
| 35 | .ping = testdrv_ping, |
| 36 | }; |
| 37 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 38 | static int test_bind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 39 | { |
| 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 | { |
| 49 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 50 | |
| 51 | /* Private data should be allocated */ |
| 52 | ut_assert(priv); |
| 53 | |
| 54 | dm_testdrv_op_count[DM_TEST_OP_PROBE]++; |
| 55 | priv->ping_total += DM_TEST_START_TOTAL; |
| 56 | return 0; |
| 57 | } |
| 58 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 59 | static int test_remove(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 60 | { |
| 61 | /* Private data should still be allocated */ |
| 62 | ut_assert(dev_get_priv(dev)); |
| 63 | |
| 64 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; |
| 65 | return 0; |
| 66 | } |
| 67 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 68 | static int test_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 69 | { |
| 70 | /* Private data should not be allocated */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 71 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 72 | |
| 73 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | U_BOOT_DRIVER(test_drv) = { |
| 78 | .name = "test_drv", |
| 79 | .id = UCLASS_TEST, |
| 80 | .ops = &test_ops, |
| 81 | .bind = test_bind, |
| 82 | .probe = test_probe, |
| 83 | .remove = test_remove, |
| 84 | .unbind = test_unbind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 85 | .priv_auto = sizeof(struct dm_test_priv), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | U_BOOT_DRIVER(test2_drv) = { |
| 89 | .name = "test2_drv", |
| 90 | .id = UCLASS_TEST, |
| 91 | .ops = &test_ops, |
| 92 | .bind = test_bind, |
| 93 | .probe = test_probe, |
| 94 | .remove = test_remove, |
| 95 | .unbind = test_unbind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 96 | .priv_auto = sizeof(struct dm_test_priv), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 99 | 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] | 100 | { |
| 101 | *pingret = pingval + 2; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static const struct test_ops test_manual_ops = { |
| 107 | .ping = test_manual_drv_ping, |
| 108 | }; |
| 109 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 110 | static int test_manual_bind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 111 | { |
| 112 | dm_testdrv_op_count[DM_TEST_OP_BIND]++; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 117 | static int test_manual_probe(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 118 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 119 | struct dm_test_state *dms = uts->priv; |
| 120 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 121 | dm_testdrv_op_count[DM_TEST_OP_PROBE]++; |
| 122 | if (!dms->force_fail_alloc) |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 123 | dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv))); |
| 124 | if (!dev_get_priv(dev)) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 125 | return -ENOMEM; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 130 | static int test_manual_remove(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 131 | { |
| 132 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; |
| 133 | return 0; |
| 134 | } |
| 135 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 136 | static int test_manual_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 137 | { |
| 138 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | U_BOOT_DRIVER(test_manual_drv) = { |
| 143 | .name = "test_manual_drv", |
| 144 | .id = UCLASS_TEST, |
| 145 | .ops = &test_manual_ops, |
| 146 | .bind = test_manual_bind, |
| 147 | .probe = test_manual_probe, |
| 148 | .remove = test_manual_remove, |
| 149 | .unbind = test_manual_unbind, |
| 150 | }; |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 151 | |
| 152 | U_BOOT_DRIVER(test_pre_reloc_drv) = { |
| 153 | .name = "test_pre_reloc_drv", |
| 154 | .id = UCLASS_TEST, |
| 155 | .ops = &test_manual_ops, |
| 156 | .bind = test_manual_bind, |
| 157 | .probe = test_manual_probe, |
| 158 | .remove = test_manual_remove, |
| 159 | .unbind = test_manual_unbind, |
| 160 | .flags = DM_FLAG_PRE_RELOC, |
| 161 | }; |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 162 | |
| 163 | U_BOOT_DRIVER(test_act_dma_drv) = { |
| 164 | .name = "test_act_dma_drv", |
| 165 | .id = UCLASS_TEST, |
| 166 | .ops = &test_manual_ops, |
| 167 | .bind = test_manual_bind, |
| 168 | .probe = test_manual_probe, |
| 169 | .remove = test_manual_remove, |
| 170 | .unbind = test_manual_unbind, |
| 171 | .flags = DM_FLAG_ACTIVE_DMA, |
| 172 | }; |