1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-25 GuEe-GUI the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "ahci.dwc"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
struct dwc_ahci_host
{
struct rt_ahci_host parent;
struct rt_clk_array *clk_arr;
struct rt_phye *sata_phy;
};
static const struct rt_ahci_ops dwc_ahci_ops = {};
static void dwc_ahci_free(struct dwc_ahci_host *dwc_ahci)
{
if (!rt_is_err_or_null(dwc_ahci->sata_phy))
{
rt_phye_put(dwc_ahci->sata_phy);
}
if (dwc_ahci->parent.regs)
{
rt_iounmap(dwc_ahci->parent.regs);
}
rt_free(dwc_ahci);
}
static rt_err_t dwc_ahci_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
struct rt_ahci_host *ahci;
struct rt_device *dev = &pdev->parent;
struct dwc_ahci_host *dwc_ahci = rt_calloc(1, sizeof(*dwc_ahci));
if (!dwc_ahci)
{
return -RT_ENOMEM;
}
ahci = &dwc_ahci->parent;
ahci->parent.dev = dev;
ahci->regs = rt_dm_dev_iomap(dev, 0);
if (!ahci->regs)
{
err = -RT_EIO;
goto _fail;
}
ahci->irq = rt_dm_dev_get_irq(dev, 0);
if (ahci->irq < 0)
{
err = ahci->irq;
goto _fail;
}
dwc_ahci->clk_arr = rt_clk_get_array(dev);
if (rt_is_err(dwc_ahci->clk_arr))
{
LOG_E("Missing CLK");
err = rt_ptr_err(dwc_ahci->clk_arr);
goto _fail;
}
dwc_ahci->sata_phy = rt_phye_get_by_name(dev, "sata-phy");
if (rt_is_err(dwc_ahci->sata_phy))
{
LOG_E("Missing PHY");
err = rt_ptr_err(dwc_ahci->sata_phy);
goto _fail;
}
if ((err = rt_clk_array_prepare_enable(dwc_ahci->clk_arr)))
{
goto _fail;
}
if ((err = rt_phye_init(dwc_ahci->sata_phy)))
{
goto _disable_clk;
}
if ((err = rt_phye_power_on(dwc_ahci->sata_phy)))
{
goto _phy_exit;
}
ahci->ops = &dwc_ahci_ops;
if ((err = rt_ahci_host_register(ahci)))
{
goto _phy_power_off;
}
dev->user_data = dwc_ahci;
return RT_EOK;
_phy_power_off:
rt_phye_power_off(dwc_ahci->sata_phy);
_phy_exit:
rt_phye_exit(dwc_ahci->sata_phy);
_disable_clk:
rt_clk_array_disable_unprepare(dwc_ahci->clk_arr);
_fail:
dwc_ahci_free(dwc_ahci);
return err;
}
static rt_err_t dwc_ahci_remove(struct rt_platform_device *pdev)
{
struct dwc_ahci_host *dwc_ahci = pdev->parent.user_data;
rt_phye_exit(dwc_ahci->sata_phy);
rt_phye_power_off(dwc_ahci->sata_phy);
rt_clk_array_disable_unprepare(dwc_ahci->clk_arr);
dwc_ahci_free(dwc_ahci);
return RT_EOK;
}
static rt_err_t dwc_ahci_shutdown(struct rt_platform_device *pdev)
{
return dwc_ahci_remove(pdev);
}
static const struct rt_ofw_node_id dwc_ahci_ofw_ids[] = {
{ .compatible = "snps,dwc-ahci" },
{ /* sentinel */ }
};
static struct rt_platform_driver ahci_dwc_driver = {
.name = "ahci-dwc",
.ids = dwc_ahci_ofw_ids,
.probe = dwc_ahci_probe,
.remove = dwc_ahci_remove,
.shutdown = dwc_ahci_shutdown,
};
RT_PLATFORM_DRIVER_EXPORT(ahci_dwc_driver);
|