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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 14 | #include <asm/io.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | #include <dm/test.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 16 | #include <linux/list.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 | |
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 | int test_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 23 | { |
| 24 | const struct test_ops *ops = device_get_ops(dev); |
| 25 | |
| 26 | if (!ops->ping) |
| 27 | return -ENOSYS; |
| 28 | |
| 29 | return ops->ping(dev, pingval, pingret); |
| 30 | } |
| 31 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 32 | static int test_post_bind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 33 | { |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 34 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 35 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 36 | dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++; |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 37 | ut_assert(!device_active(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 38 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 39 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 40 | ut_assert(uc_pdata); |
| 41 | |
| 42 | uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1; |
| 43 | uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2; |
| 44 | uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3; |
| 45 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 49 | static int test_pre_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 50 | { |
| 51 | dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 56 | static int test_pre_probe(struct udevice *dev) |
| 57 | { |
| 58 | struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev); |
| 59 | |
| 60 | dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++; |
| 61 | ut_assert(priv); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 62 | ut_assert(device_active(dev)); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 67 | static int test_post_probe(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 68 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 69 | struct udevice *prev = list_entry(dev->uclass_node.prev, |
| 70 | struct udevice, uclass_node); |
| 71 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 72 | struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 73 | struct uclass *uc = dev->uclass; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 74 | struct dm_test_state *dms = uts->priv; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 75 | |
| 76 | dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++; |
| 77 | ut_assert(priv); |
| 78 | ut_assert(device_active(dev)); |
| 79 | priv->base_add = 0; |
| 80 | if (dms->skip_post_probe) |
| 81 | return 0; |
| 82 | if (&prev->uclass_node != &uc->dev_head) { |
| 83 | struct dm_test_uclass_perdev_priv *prev_uc_priv |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 84 | = dev_get_uclass_priv(prev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 85 | struct dm_test_pdata *pdata = prev->plat; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 86 | |
| 87 | ut_assert(pdata); |
| 88 | ut_assert(prev_uc_priv); |
| 89 | priv->base_add = prev_uc_priv->base_add + pdata->ping_add; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 95 | static int test_pre_remove(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 96 | { |
| 97 | dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int test_init(struct uclass *uc) |
| 103 | { |
| 104 | dm_testdrv_op_count[DM_TEST_OP_INIT]++; |
| 105 | ut_assert(uc->priv); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int test_destroy(struct uclass *uc) |
| 111 | { |
| 112 | dm_testdrv_op_count[DM_TEST_OP_DESTROY]++; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | UCLASS_DRIVER(test) = { |
| 118 | .name = "test", |
| 119 | .id = UCLASS_TEST, |
| 120 | .post_bind = test_post_bind, |
| 121 | .pre_unbind = test_pre_unbind, |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 122 | .pre_probe = test_pre_probe, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 123 | .post_probe = test_post_probe, |
| 124 | .pre_remove = test_pre_remove, |
| 125 | .init = test_init, |
| 126 | .destroy = test_destroy, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 127 | .priv_auto = sizeof(struct dm_test_uclass_priv), |
| 128 | .per_device_auto = sizeof(struct dm_test_uclass_perdev_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 129 | .per_device_plat_auto = |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 130 | sizeof(struct dm_test_perdev_uc_pdata), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 131 | }; |