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 <malloc.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 13 | #include <asm/io.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | #include <dm/test.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 15 | #include <linux/list.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 17 | |
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 | 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 | { |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 32 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 33 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 34 | dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++; |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 35 | ut_assert(!device_active(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 36 | |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 37 | uc_pdata = dev_get_uclass_platdata(dev); |
| 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 | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 47 | static int test_pre_unbind(struct udevice *dev) |
Simon Glass | b2c1cac | 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 | 9c1f382 | 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); |
| 57 | |
| 58 | dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++; |
| 59 | ut_assert(priv); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 60 | ut_assert(device_active(dev)); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 65 | static int test_post_probe(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 66 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 67 | struct udevice *prev = list_entry(dev->uclass_node.prev, |
| 68 | struct udevice, uclass_node); |
| 69 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 70 | struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 71 | struct uclass *uc = dev->uclass; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 72 | struct dm_test_state *dms = uts->priv; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 73 | |
| 74 | dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++; |
| 75 | ut_assert(priv); |
| 76 | ut_assert(device_active(dev)); |
| 77 | priv->base_add = 0; |
| 78 | if (dms->skip_post_probe) |
| 79 | return 0; |
| 80 | if (&prev->uclass_node != &uc->dev_head) { |
| 81 | struct dm_test_uclass_perdev_priv *prev_uc_priv |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 82 | = dev_get_uclass_priv(prev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 83 | struct dm_test_pdata *pdata = prev->platdata; |
| 84 | |
| 85 | ut_assert(pdata); |
| 86 | ut_assert(prev_uc_priv); |
| 87 | priv->base_add = prev_uc_priv->base_add + pdata->ping_add; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 93 | static int test_pre_remove(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 94 | { |
| 95 | dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int test_init(struct uclass *uc) |
| 101 | { |
| 102 | dm_testdrv_op_count[DM_TEST_OP_INIT]++; |
| 103 | ut_assert(uc->priv); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int test_destroy(struct uclass *uc) |
| 109 | { |
| 110 | dm_testdrv_op_count[DM_TEST_OP_DESTROY]++; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | UCLASS_DRIVER(test) = { |
| 116 | .name = "test", |
| 117 | .id = UCLASS_TEST, |
| 118 | .post_bind = test_post_bind, |
| 119 | .pre_unbind = test_pre_unbind, |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 120 | .pre_probe = test_pre_probe, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 121 | .post_probe = test_post_probe, |
| 122 | .pre_remove = test_pre_remove, |
| 123 | .init = test_init, |
| 124 | .destroy = test_destroy, |
| 125 | .priv_auto_alloc_size = sizeof(struct dm_test_uclass_priv), |
| 126 | .per_device_auto_alloc_size = sizeof(struct dm_test_uclass_perdev_priv), |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 127 | .per_device_platdata_auto_alloc_size = |
| 128 | sizeof(struct dm_test_perdev_uc_pdata), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 129 | }; |