blob: 6d28aa20a7d32c4142dba9a52cbaa06fc6c387f1 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * linux/arch/arm/mach-omap2/hsmmc.c
3 *
4 * Copyright (C) 2007-2008 Texas Instruments
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: Texas Instruments
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/string.h>
15#include <linux/delay.h>
16#include <linux/gpio.h>
17#include <linux/mmc/host.h>
18#include <linux/platform_data/gpio-omap.h>
19#include <linux/platform_data/hsmmc-omap.h>
20
21#include "soc.h"
22#include "omap_device.h"
23#include "omap-pm.h"
24
25#include "hsmmc.h"
26#include "control.h"
27
28#if IS_ENABLED(CONFIG_MMC_OMAP_HS)
29
30static u16 control_pbias_offset;
31static u16 control_devconf1_offset;
32
33#define HSMMC_NAME_LEN 9
34
35static int __init omap_hsmmc_pdata_init(struct omap2_hsmmc_info *c,
36 struct omap_hsmmc_platform_data *mmc)
37{
38 char *hc_name;
39
40 hc_name = kzalloc(sizeof(char) * (HSMMC_NAME_LEN + 1), GFP_KERNEL);
41 if (!hc_name) {
42 kfree(hc_name);
43 return -ENOMEM;
44 }
45
46 snprintf(hc_name, (HSMMC_NAME_LEN + 1), "mmc%islot%i", c->mmc, 1);
47 mmc->name = hc_name;
48 mmc->caps = c->caps;
49 mmc->reg_offset = 0;
50
51 return 0;
52}
53
54static int omap_hsmmc_done;
55
56void omap_hsmmc_late_init(struct omap2_hsmmc_info *c)
57{
58 struct platform_device *pdev;
59 int res;
60
61 if (omap_hsmmc_done)
62 return;
63
64 omap_hsmmc_done = 1;
65
66 for (; c->mmc; c++) {
67 pdev = c->pdev;
68 if (!pdev)
69 continue;
70 res = omap_device_register(pdev);
71 if (res)
72 pr_err("Could not late init MMC\n");
73 }
74}
75
76#define MAX_OMAP_MMC_HWMOD_NAME_LEN 16
77
78static void __init omap_hsmmc_init_one(struct omap2_hsmmc_info *hsmmcinfo,
79 int ctrl_nr)
80{
81 struct omap_hwmod *oh;
82 struct omap_hwmod *ohs[1];
83 struct omap_device *od;
84 struct platform_device *pdev;
85 char oh_name[MAX_OMAP_MMC_HWMOD_NAME_LEN];
86 struct omap_hsmmc_platform_data *mmc_data;
87 struct omap_hsmmc_dev_attr *mmc_dev_attr;
88 char *name;
89 int res;
90
91 mmc_data = kzalloc(sizeof(*mmc_data), GFP_KERNEL);
92 if (!mmc_data)
93 return;
94
95 res = omap_hsmmc_pdata_init(hsmmcinfo, mmc_data);
96 if (res < 0)
97 goto free_mmc;
98
99 name = "omap_hsmmc";
100 res = snprintf(oh_name, MAX_OMAP_MMC_HWMOD_NAME_LEN,
101 "mmc%d", ctrl_nr);
102 WARN(res >= MAX_OMAP_MMC_HWMOD_NAME_LEN,
103 "String buffer overflow in MMC%d device setup\n", ctrl_nr);
104
105 oh = omap_hwmod_lookup(oh_name);
106 if (!oh) {
107 pr_err("Could not look up %s\n", oh_name);
108 goto free_name;
109 }
110 ohs[0] = oh;
111 if (oh->dev_attr != NULL) {
112 mmc_dev_attr = oh->dev_attr;
113 mmc_data->controller_flags = mmc_dev_attr->flags;
114 }
115
116 pdev = platform_device_alloc(name, ctrl_nr - 1);
117 if (!pdev) {
118 pr_err("Could not allocate pdev for %s\n", name);
119 goto free_name;
120 }
121 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
122
123 od = omap_device_alloc(pdev, ohs, 1);
124 if (IS_ERR(od)) {
125 pr_err("Could not allocate od for %s\n", name);
126 goto put_pdev;
127 }
128
129 res = platform_device_add_data(pdev, mmc_data,
130 sizeof(struct omap_hsmmc_platform_data));
131 if (res) {
132 pr_err("Could not add pdata for %s\n", name);
133 goto put_pdev;
134 }
135
136 hsmmcinfo->pdev = pdev;
137
138 res = omap_device_register(pdev);
139 if (res) {
140 pr_err("Could not register od for %s\n", name);
141 goto free_od;
142 }
143
144 goto free_mmc;
145
146free_od:
147 omap_device_delete(od);
148
149put_pdev:
150 platform_device_put(pdev);
151
152free_name:
153 kfree(mmc_data->name);
154
155free_mmc:
156 kfree(mmc_data);
157}
158
159void __init omap_hsmmc_init(struct omap2_hsmmc_info *controllers)
160{
161 if (omap_hsmmc_done)
162 return;
163
164 omap_hsmmc_done = 1;
165
166 if (cpu_is_omap2430()) {
167 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
168 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
169 } else {
170 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
171 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
172 }
173
174 for (; controllers->mmc; controllers++)
175 omap_hsmmc_init_one(controllers, controllers->mmc);
176
177}
178
179#endif