1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-10-24     GuEe-GUI     first version
9  */
10 
11 #include <rtthread.h>
12 
13 #include "../ecam.h"
14 
pci_host_common_probe(struct rt_platform_device * pdev)15 rt_err_t pci_host_common_probe(struct rt_platform_device *pdev)
16 {
17     void *base;
18     rt_err_t err;
19     struct rt_device *dev = &pdev->parent;
20     struct pci_ecam_config_window *conf_win;
21     struct rt_pci_host_bridge *host_bridge = rt_pci_host_bridge_alloc(0);
22 
23     if (!host_bridge)
24     {
25         return -RT_ENOMEM;
26     }
27 
28     if (!(base = rt_dm_dev_iomap(dev, 0)))
29     {
30         err = -RT_EIO;
31         goto _fail;
32     }
33 
34     host_bridge->parent.ofw_node = dev->ofw_node;
35 
36     if ((err = rt_pci_host_bridge_init(host_bridge)))
37     {
38         goto _fail;
39     }
40 
41     host_bridge->sysdata = conf_win = pci_ecam_create(host_bridge,
42             (const struct pci_ecam_ops *)pdev->id->data);
43 
44     if (!conf_win)
45     {
46         err = -RT_ENOMEM;
47         goto _fail;
48     }
49 
50     conf_win->win = base;
51     conf_win->priv = host_bridge;
52 
53     if ((err = rt_pci_host_bridge_probe(host_bridge)))
54     {
55         goto _fail;
56     }
57 
58     dev->user_data = host_bridge;
59 
60     return RT_EOK;
61 
62 _fail:
63     if (base)
64     {
65         rt_iounmap(base);
66     }
67     rt_pci_host_bridge_free(host_bridge);
68 
69     return err;
70 }
71 
pci_host_common_remove(struct rt_platform_device * pdev)72 rt_err_t pci_host_common_remove(struct rt_platform_device *pdev)
73 {
74     struct pci_ecam_config_window *conf_win;
75     struct rt_pci_host_bridge *host_bridge = pdev->parent.user_data;
76 
77     rt_pci_host_bridge_remove(host_bridge);
78 
79     conf_win = host_bridge->sysdata;
80 
81     rt_iounmap(conf_win->win);
82     rt_pci_host_bridge_free(host_bridge);
83 
84     return RT_EOK;
85 }
86