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