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