blob: 040728b45d3c9f415b31bdb330f506319457e45a [file] [log] [blame]
Simon Glassc40fdca2016-05-01 13:52:35 -06001/*
2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Simon Glass5aed4cb2016-06-12 23:30:17 -06009#include <malloc.h>
Simon Glassc40fdca2016-05-01 13:52:35 -060010#include <mmc.h>
Simon Glass5aed4cb2016-06-12 23:30:17 -060011#include "mmc_private.h"
Simon Glassc40fdca2016-05-01 13:52:35 -060012
13static struct list_head mmc_devices;
14static int cur_dev_num = -1;
15
16struct mmc *find_mmc_device(int dev_num)
17{
18 struct mmc *m;
19 struct list_head *entry;
20
21 list_for_each(entry, &mmc_devices) {
22 m = list_entry(entry, struct mmc, link);
23
24 if (m->block_dev.devnum == dev_num)
25 return m;
26 }
27
28#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
29 printf("MMC Device %d not found\n", dev_num);
30#endif
31
32 return NULL;
33}
34
35int mmc_get_next_devnum(void)
36{
37 return cur_dev_num++;
38}
39
40struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
41{
42 return &mmc->block_dev;
43}
44
45int get_mmc_num(void)
46{
47 return cur_dev_num;
48}
49
50void mmc_do_preinit(void)
51{
52 struct mmc *m;
53 struct list_head *entry;
54
55 list_for_each(entry, &mmc_devices) {
56 m = list_entry(entry, struct mmc, link);
57
58#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
59 mmc_set_preinit(m, 1);
60#endif
61 if (m->preinit)
62 mmc_start_init(m);
63 }
64}
65
66void mmc_list_init(void)
67{
68 INIT_LIST_HEAD(&mmc_devices);
69 cur_dev_num = 0;
70}
71
72void mmc_list_add(struct mmc *mmc)
73{
74 INIT_LIST_HEAD(&mmc->link);
75
76 list_add_tail(&mmc->link, &mmc_devices);
77}
78
79#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
80void print_mmc_devices(char separator)
81{
82 struct mmc *m;
83 struct list_head *entry;
84 char *mmc_type;
85
86 list_for_each(entry, &mmc_devices) {
87 m = list_entry(entry, struct mmc, link);
88
89 if (m->has_init)
90 mmc_type = IS_SD(m) ? "SD" : "eMMC";
91 else
92 mmc_type = NULL;
93
94 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
95 if (mmc_type)
96 printf(" (%s)", mmc_type);
97
98 if (entry->next != &mmc_devices) {
99 printf("%c", separator);
100 if (separator != '\n')
101 puts(" ");
102 }
103 }
104
105 printf("\n");
106}
107
108#else
109void print_mmc_devices(char separator) { }
110#endif
Simon Glass5aed4cb2016-06-12 23:30:17 -0600111
112struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
113{
114 struct blk_desc *bdesc;
115 struct mmc *mmc;
116
117 /* quick validation */
118 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
119 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
120 return NULL;
121
122 mmc = calloc(1, sizeof(*mmc));
123 if (mmc == NULL)
124 return NULL;
125
126 mmc->cfg = cfg;
127 mmc->priv = priv;
128
129 /* the following chunk was mmc_register() */
130
131 /* Setup dsr related values */
132 mmc->dsr_imp = 0;
133 mmc->dsr = 0xffffffff;
134 /* Setup the universal parts of the block interface just once */
135 bdesc = mmc_get_blk_desc(mmc);
136 bdesc->if_type = IF_TYPE_MMC;
137 bdesc->removable = 1;
138 bdesc->devnum = mmc_get_next_devnum();
139 bdesc->block_read = mmc_bread;
140 bdesc->block_write = mmc_bwrite;
141 bdesc->block_erase = mmc_berase;
142
143 /* setup initial part type */
144 bdesc->part_type = mmc->cfg->part_type;
145 mmc_list_add(mmc);
146
147 return mmc;
148}
149
150void mmc_destroy(struct mmc *mmc)
151{
152 /* only freeing memory for now */
153 free(mmc);
154}
155
156static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
157{
158 struct mmc *mmc = find_mmc_device(desc->devnum);
159 int ret;
160
161 if (!mmc)
162 return -ENODEV;
163
164 if (mmc->block_dev.hwpart == hwpart)
165 return 0;
166
167 if (mmc->part_config == MMCPART_NOAVAILABLE)
168 return -EMEDIUMTYPE;
169
170 ret = mmc_switch_part(mmc, hwpart);
171 if (ret)
172 return ret;
173
174 return 0;
175}
176
177static int mmc_get_dev(int dev, struct blk_desc **descp)
178{
179 struct mmc *mmc = find_mmc_device(dev);
180 int ret;
181
182 if (!mmc)
183 return -ENODEV;
184 ret = mmc_init(mmc);
185 if (ret)
186 return ret;
187
188 *descp = &mmc->block_dev;
189
190 return 0;
191}
192
193U_BOOT_LEGACY_BLK(mmc) = {
194 .if_typename = "mmc",
195 .if_type = IF_TYPE_MMC,
196 .max_devs = -1,
197 .get_dev = mmc_get_dev,
198 .select_hwpart = mmc_select_hwpartp,
199};