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 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 20 | int test_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 21 | { |
| 22 | const struct test_ops *ops = device_get_ops(dev); |
| 23 | |
| 24 | if (!ops->ping) |
| 25 | return -ENOSYS; |
| 26 | |
| 27 | return ops->ping(dev, pingval, pingret); |
| 28 | } |
| 29 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 30 | static int test_post_bind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 31 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 32 | struct unit_test_state *uts = test_get_state(); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 33 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 34 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 35 | dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++; |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 36 | ut_assert(!device_active(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 37 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 38 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 39 | ut_assert(uc_pdata); |
| 40 | |
| 41 | uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1; |
| 42 | uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2; |
| 43 | uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3; |
| 44 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 48 | static int test_pre_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 49 | { |
| 50 | dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 55 | static int test_pre_probe(struct udevice *dev) |
| 56 | { |
| 57 | struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 58 | struct unit_test_state *uts = test_get_state(); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 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 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 69 | struct unit_test_state *uts = test_get_state(); |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 70 | struct udevice *prev = list_entry(dev->uclass_node.prev, |
| 71 | struct udevice, uclass_node); |
| 72 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 73 | struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 74 | struct uclass *uc = dev->uclass; |
| 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; |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 80 | if (uts->skip_post_probe) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 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 | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 85 | struct dm_test_pdata *pdata = dev_get_plat(prev); |
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 | { |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 104 | struct unit_test_state *uts = test_get_state(); |
| 105 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 106 | dm_testdrv_op_count[DM_TEST_OP_INIT]++; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 107 | ut_assert(uclass_get_priv(uc)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int test_destroy(struct uclass *uc) |
| 113 | { |
| 114 | dm_testdrv_op_count[DM_TEST_OP_DESTROY]++; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | UCLASS_DRIVER(test) = { |
| 120 | .name = "test", |
| 121 | .id = UCLASS_TEST, |
| 122 | .post_bind = test_post_bind, |
| 123 | .pre_unbind = test_pre_unbind, |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 124 | .pre_probe = test_pre_probe, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 125 | .post_probe = test_post_probe, |
| 126 | .pre_remove = test_pre_remove, |
| 127 | .init = test_init, |
| 128 | .destroy = test_destroy, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 129 | .priv_auto = sizeof(struct dm_test_uclass_priv), |
| 130 | .per_device_auto = sizeof(struct dm_test_uclass_perdev_priv), |
Simon Glass | 33b2efb | 2020-12-03 16:55:22 -0700 | [diff] [blame] | 131 | .per_device_plat_auto = sizeof(struct dm_test_perdev_uc_pdata), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 132 | }; |