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