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 | |
| 9 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 14 | #include <asm/io.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | #include <dm/test.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 16 | #include <linux/list.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 17 | #include <test/test.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | #include <test/ut.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 19 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | static struct unit_test_state *uts = &global_dm_test_state; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 21 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 22 | int test_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | 2e7d35d | 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 | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 32 | static int test_post_bind(struct udevice *dev) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 33 | { |
Przemyslaw Marczak | 754e71e | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 34 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 35 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 36 | dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++; |
Simon Glass | 206d4d2 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 37 | ut_assert(!device_active(dev)); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 38 | |
Przemyslaw Marczak | 754e71e | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 39 | uc_pdata = dev_get_uclass_platdata(dev); |
| 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 | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 49 | static int test_pre_unbind(struct udevice *dev) |
Simon Glass | 2e7d35d | 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 | 02c07b3 | 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 | 206d4d2 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 62 | ut_assert(device_active(dev)); |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 67 | static int test_post_probe(struct udevice *dev) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 68 | { |
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; |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 74 | struct dm_test_state *dms = uts->priv; |
Simon Glass | 2e7d35d | 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 | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 84 | = dev_get_uclass_priv(prev); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 85 | struct dm_test_pdata *pdata = prev->platdata; |
| 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 | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 95 | static int test_pre_remove(struct udevice *dev) |
Simon Glass | 2e7d35d | 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 | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 122 | .pre_probe = test_pre_probe, |
Simon Glass | 2e7d35d | 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, |
| 127 | .priv_auto_alloc_size = sizeof(struct dm_test_uclass_priv), |
| 128 | .per_device_auto_alloc_size = sizeof(struct dm_test_uclass_perdev_priv), |
Przemyslaw Marczak | 754e71e | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 129 | .per_device_platdata_auto_alloc_size = |
| 130 | sizeof(struct dm_test_perdev_uc_pdata), |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 131 | }; |