Merge commit 'v2.6.29-rc1' into tracing/urgent
[linux-2.6] / 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 = kmalloc(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         memset(lpddr->qinfo, 0, sizeof(struct qinfo_chip));
144
145         /* Get the ManuID */
146         lpddr->ManufactId = CMDVAL(map_read(map, map->pfow_base + PFOW_MANUFACTURER_ID));
147         /* Get the DeviceID */
148         lpddr->DevId = CMDVAL(map_read(map, map->pfow_base + PFOW_DEVICE_ID));
149         /* read parameters from chip qinfo table */
150         lpddr->qinfo->DevSizeShift = lpddr_info_query(map, "DevSizeShift");
151         lpddr->qinfo->TotalBlocksNum = lpddr_info_query(map, "TotalBlocksNum");
152         lpddr->qinfo->BufSizeShift = lpddr_info_query(map, "BufSizeShift");
153         lpddr->qinfo->HWPartsNum = lpddr_info_query(map, "HWPartsNum");
154         lpddr->qinfo->UniformBlockSizeShift =
155                                 lpddr_info_query(map, "UniformBlockSizeShift");
156         lpddr->qinfo->SuspEraseSupp = lpddr_info_query(map, "SuspEraseSupp");
157         lpddr->qinfo->SingleWordProgTime =
158                                 lpddr_info_query(map, "SingleWordProgTime");
159         lpddr->qinfo->ProgBufferTime = lpddr_info_query(map, "ProgBufferTime");
160         lpddr->qinfo->BlockEraseTime = lpddr_info_query(map, "BlockEraseTime");
161         return 1;
162 }
163 static struct lpddr_private *lpddr_probe_chip(struct map_info *map)
164 {
165         struct lpddr_private lpddr;
166         struct lpddr_private *retlpddr;
167         int numvirtchips;
168
169
170         if ((map->pfow_base + 0x1000) >= map->size) {
171                 printk(KERN_NOTICE"%s Probe at base (0x%08lx) past the end of"
172                                 "the map(0x%08lx)\n", map->name,
173                                 (unsigned long)map->pfow_base, map->size - 1);
174                 return NULL;
175         }
176         memset(&lpddr, 0, sizeof(struct lpddr_private));
177         if (!lpddr_pfow_present(map, &lpddr))
178                 return NULL;
179
180         if (!lpddr_chip_setup(map, &lpddr))
181                 return NULL;
182
183         /* Ok so we found a chip */
184         lpddr.chipshift = lpddr.qinfo->DevSizeShift;
185         lpddr.numchips = 1;
186
187         numvirtchips = lpddr.numchips * lpddr.qinfo->HWPartsNum;
188         retlpddr = kmalloc(sizeof(struct lpddr_private) +
189                         numvirtchips * sizeof(struct flchip), GFP_KERNEL);
190         if (!retlpddr)
191                 return NULL;
192
193         memset(retlpddr, 0, sizeof(struct lpddr_private) +
194                                 numvirtchips * sizeof(struct flchip));
195         memcpy(retlpddr, &lpddr, sizeof(struct lpddr_private));
196
197         retlpddr->numchips = numvirtchips;
198         retlpddr->chipshift = retlpddr->qinfo->DevSizeShift -
199                                 __ffs(retlpddr->qinfo->HWPartsNum);
200
201         return retlpddr;
202 }
203
204 struct mtd_info *lpddr_probe(struct map_info *map)
205 {
206         struct mtd_info *mtd = NULL;
207         struct lpddr_private *lpddr;
208
209         /* First probe the map to see if we havecan open PFOW here */
210         lpddr = lpddr_probe_chip(map);
211         if (!lpddr)
212                 return NULL;
213
214         map->fldrv_priv = lpddr;
215         mtd = lpddr_cmdset(map);
216         if (mtd) {
217                 if (mtd->size > map->size) {
218                         printk(KERN_WARNING "Reducing visibility of %ldKiB chip"
219                                 "to %ldKiB\n", (unsigned long)mtd->size >> 10,
220                                 (unsigned long)map->size >> 10);
221                         mtd->size = map->size;
222                 }
223                 return mtd;
224         }
225
226         kfree(lpddr->qinfo);
227         kfree(lpddr);
228         map->fldrv_priv = NULL;
229         return NULL;
230 }
231
232 static struct mtd_chip_driver lpddr_chipdrv = {
233         .probe          = lpddr_probe,
234         .name           = "qinfo_probe",
235         .module         = THIS_MODULE
236 };
237
238 static int __init lpddr_probe_init(void)
239 {
240         register_mtd_chip_driver(&lpddr_chipdrv);
241         return 0;
242 }
243
244 static void __exit lpddr_probe_exit(void)
245 {
246         unregister_mtd_chip_driver(&lpddr_chipdrv);
247 }
248
249 module_init(lpddr_probe_init);
250 module_exit(lpddr_probe_exit);
251
252 MODULE_LICENSE("GPL");
253 MODULE_AUTHOR("Vasiliy Leonenko <vasiliy.leonenko@gmail.com>");
254 MODULE_DESCRIPTION("Driver to probe qinfo flash chips");
255