media: soc_camera: tegra: fix one building warning
[linux-2.6.git] / drivers / mtd / lpddr / qinfo_probe.c
1 /*
2  * Probing flash chips with QINFO records.
3  * (C) 2008 Korolev Alexey <akorolev@infradead.org>
4  * (C) 2008 Vasiliy Leonenko <vasiliy.leonenko@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28
29 #include <linux/mtd/xip.h>
30 #include <linux/mtd/map.h>
31 #include <linux/mtd/pfow.h>
32 #include <linux/mtd/qinfo.h>
33
34 static int lpddr_chip_setup(struct map_info *map, struct lpddr_private *lpddr);
35 struct mtd_info *lpddr_probe(struct map_info *map);
36 static struct lpddr_private *lpddr_probe_chip(struct map_info *map);
37 static int lpddr_pfow_present(struct map_info *map,
38                         struct lpddr_private *lpddr);
39
40 static struct qinfo_query_info qinfo_array[] = {
41         /* General device info */
42         {0, 0, "DevSizeShift", "Device size 2^n bytes"},
43         {0, 3, "BufSizeShift", "Program buffer size 2^n bytes"},
44         /* Erase block information */
45         {1, 1, "TotalBlocksNum", "Total number of blocks"},
46         {1, 2, "UniformBlockSizeShift", "Uniform block size 2^n bytes"},
47         /* Partition information */
48         {2, 1, "HWPartsNum", "Number of hardware partitions"},
49         /* Optional features */
50         {5, 1, "SuspEraseSupp", "Suspend erase supported"},
51         /* Operation typical time */
52         {10, 0, "SingleWordProgTime", "Single word program 2^n u-sec"},
53         {10, 1, "ProgBufferTime", "Program buffer write 2^n u-sec"},
54         {10, 2, "BlockEraseTime", "Block erase 2^n m-sec"},
55         {10, 3, "FullChipEraseTime", "Full chip erase 2^n m-sec"},
56 };
57
58 static long lpddr_get_qinforec_pos(struct map_info *map, char *id_str)
59 {
60         int qinfo_lines = sizeof(qinfo_array)/sizeof(struct qinfo_query_info);
61         int i;
62         int bankwidth = map_bankwidth(map) * 8;
63         int major, minor;
64
65         for (i = 0; i < qinfo_lines; i++) {
66                 if (strcmp(id_str, qinfo_array[i].id_str) == 0) {
67                         major = qinfo_array[i].major & ((1 << bankwidth) - 1);
68                         minor = qinfo_array[i].minor & ((1 << bankwidth) - 1);
69                         return minor | (major << bankwidth);
70                 }
71         }
72         printk(KERN_ERR"%s qinfo id string is wrong! \n", map->name);
73         BUG();
74         return -1;
75 }
76
77 static uint16_t lpddr_info_query(struct map_info *map, char *id_str)
78 {
79         unsigned int dsr, val;
80         int bits_per_chip = map_bankwidth(map) * 8;
81         unsigned long adr = lpddr_get_qinforec_pos(map, id_str);
82         int attempts = 20;
83
84         /* Write a request for the PFOW record */
85         map_write(map, CMD(LPDDR_INFO_QUERY),
86                         map->pfow_base + PFOW_COMMAND_CODE);
87         map_write(map, CMD(adr & ((1 << bits_per_chip) - 1)),
88                         map->pfow_base + PFOW_COMMAND_ADDRESS_L);
89         map_write(map, CMD(adr >> bits_per_chip),
90                         map->pfow_base + PFOW_COMMAND_ADDRESS_H);
91         map_write(map, CMD(LPDDR_START_EXECUTION),
92                         map->pfow_base + PFOW_COMMAND_EXECUTE);
93
94         while ((attempts--) > 0) {
95                 dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
96                 if (dsr & DSR_READY_STATUS)
97                         break;
98                 udelay(10);
99         }
100
101         val = CMDVAL(map_read(map, map->pfow_base + PFOW_COMMAND_DATA));
102         return val;
103 }
104
105 static int lpddr_pfow_present(struct map_info *map, struct lpddr_private *lpddr)
106 {
107         map_word pfow_val[4];
108
109         /* Check identification string */
110         pfow_val[0] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_P);
111         pfow_val[1] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_F);
112         pfow_val[2] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_O);
113         pfow_val[3] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_W);
114
115         if (!map_word_equal(map, CMD('P'), pfow_val[0]))
116                 goto out;
117
118         if (!map_word_equal(map, CMD('F'), pfow_val[1]))
119                 goto out;
120
121         if (!map_word_equal(map, CMD('O'), pfow_val[2]))
122                 goto out;
123
124         if (!map_word_equal(map, CMD('W'), pfow_val[3]))
125                 goto out;
126
127         return 1;       /* "PFOW" is found */
128 out:
129         printk(KERN_WARNING"%s: PFOW string at 0x%lx is not found \n",
130                                         map->name, map->pfow_base);
131         return 0;
132 }
133
134 static int lpddr_chip_setup(struct map_info *map, struct lpddr_private *lpddr)
135 {
136
137         lpddr->qinfo = kzalloc(sizeof(struct qinfo_chip), GFP_KERNEL);
138         if (!lpddr->qinfo) {
139                 printk(KERN_WARNING "%s: no memory for LPDDR qinfo structure\n",
140                                 map->name);
141                 return 0;
142         }
143
144         /* Get the ManuID */
145         lpddr->ManufactId = CMDVAL(map_read(map, map->pfow_base + PFOW_MANUFACTURER_ID));
146         /* Get the DeviceID */
147         lpddr->DevId = CMDVAL(map_read(map, map->pfow_base + PFOW_DEVICE_ID));
148         /* read parameters from chip qinfo table */
149         lpddr->qinfo->DevSizeShift = lpddr_info_query(map, "DevSizeShift");
150         lpddr->qinfo->TotalBlocksNum = lpddr_info_query(map, "TotalBlocksNum");
151         lpddr->qinfo->BufSizeShift = lpddr_info_query(map, "BufSizeShift");
152         lpddr->qinfo->HWPartsNum = lpddr_info_query(map, "HWPartsNum");
153         lpddr->qinfo->UniformBlockSizeShift =
154                                 lpddr_info_query(map, "UniformBlockSizeShift");
155         lpddr->qinfo->SuspEraseSupp = lpddr_info_query(map, "SuspEraseSupp");
156         lpddr->qinfo->SingleWordProgTime =
157                                 lpddr_info_query(map, "SingleWordProgTime");
158         lpddr->qinfo->ProgBufferTime = lpddr_info_query(map, "ProgBufferTime");
159         lpddr->qinfo->BlockEraseTime = lpddr_info_query(map, "BlockEraseTime");
160         return 1;
161 }
162 static struct lpddr_private *lpddr_probe_chip(struct map_info *map)
163 {
164         struct lpddr_private lpddr;
165         struct lpddr_private *retlpddr;
166         int numvirtchips;
167
168
169         if ((map->pfow_base + 0x1000) >= map->size) {
170                 printk(KERN_NOTICE"%s Probe at base (0x%08lx) past the end of"
171                                 "the map(0x%08lx)\n", map->name,
172                                 (unsigned long)map->pfow_base, map->size - 1);
173                 return NULL;
174         }
175         memset(&lpddr, 0, sizeof(struct lpddr_private));
176         if (!lpddr_pfow_present(map, &lpddr))
177                 return NULL;
178
179         if (!lpddr_chip_setup(map, &lpddr))
180                 return NULL;
181
182         /* Ok so we found a chip */
183         lpddr.chipshift = lpddr.qinfo->DevSizeShift;
184         lpddr.numchips = 1;
185
186         numvirtchips = lpddr.numchips * lpddr.qinfo->HWPartsNum;
187         retlpddr = kzalloc(sizeof(struct lpddr_private) +
188                         numvirtchips * sizeof(struct flchip), GFP_KERNEL);
189         if (!retlpddr)
190                 return NULL;
191
192         memcpy(retlpddr, &lpddr, sizeof(struct lpddr_private));
193
194         retlpddr->numchips = numvirtchips;
195         retlpddr->chipshift = retlpddr->qinfo->DevSizeShift -
196                                 __ffs(retlpddr->qinfo->HWPartsNum);
197
198         return retlpddr;
199 }
200
201 struct mtd_info *lpddr_probe(struct map_info *map)
202 {
203         struct mtd_info *mtd = NULL;
204         struct lpddr_private *lpddr;
205
206         /* First probe the map to see if we havecan open PFOW here */
207         lpddr = lpddr_probe_chip(map);
208         if (!lpddr)
209                 return NULL;
210
211         map->fldrv_priv = lpddr;
212         mtd = lpddr_cmdset(map);
213         if (mtd) {
214                 if (mtd->size > map->size) {
215                         printk(KERN_WARNING "Reducing visibility of %ldKiB chip"
216                                 "to %ldKiB\n", (unsigned long)mtd->size >> 10,
217                                 (unsigned long)map->size >> 10);
218                         mtd->size = map->size;
219                 }
220                 return mtd;
221         }
222
223         kfree(lpddr->qinfo);
224         kfree(lpddr);
225         map->fldrv_priv = NULL;
226         return NULL;
227 }
228
229 static struct mtd_chip_driver lpddr_chipdrv = {
230         .probe          = lpddr_probe,
231         .name           = "qinfo_probe",
232         .module         = THIS_MODULE
233 };
234
235 static int __init lpddr_probe_init(void)
236 {
237         register_mtd_chip_driver(&lpddr_chipdrv);
238         return 0;
239 }
240
241 static void __exit lpddr_probe_exit(void)
242 {
243         unregister_mtd_chip_driver(&lpddr_chipdrv);
244 }
245
246 module_init(lpddr_probe_init);
247 module_exit(lpddr_probe_exit);
248
249 MODULE_LICENSE("GPL");
250 MODULE_AUTHOR("Vasiliy Leonenko <vasiliy.leonenko@gmail.com>");
251 MODULE_DESCRIPTION("Driver to probe qinfo flash chips");
252