Root/target/linux/adm5120/files/drivers/mtd/maps/adm5120-flash.c

1/*
2 * Platform driver for NOR flash devices on ADM5120 based boards
3 *
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This file was derived from: drivers/mtd/map/physmap.c
7 * Copyright (C) 2003 MontaVista Software Inc.
8 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/io.h>
22#include <linux/device.h>
23#include <linux/platform_device.h>
24
25#include <linux/mtd/mtd.h>
26#include <linux/mtd/map.h>
27#include <linux/mtd/partitions.h>
28
29#include <asm/mach-adm5120/adm5120_defs.h>
30#include <asm/mach-adm5120/adm5120_switch.h>
31#include <asm/mach-adm5120/adm5120_mpmc.h>
32#include <asm/mach-adm5120/adm5120_platform.h>
33
34#define DRV_NAME "adm5120-flash"
35#define DRV_DESC "ADM5120 flash MAP driver"
36#define MAX_PARSED_PARTS 8
37
38#ifdef ADM5120_FLASH_DEBUG
39#define MAP_DBG(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
40#else
41#define MAP_DBG(m, f, a...) do {} while (0)
42#endif
43#define MAP_ERR(m, f, a...) printk(KERN_ERR "%s: " f, (m->name) , ## a)
44#define MAP_INFO(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
45
46struct adm5120_map_info {
47    struct map_info map;
48    void (*switch_bank)(unsigned);
49    unsigned long window_size;
50};
51
52struct adm5120_flash_info {
53    struct mtd_info *mtd;
54    struct resource *res;
55    struct platform_device *dev;
56    struct adm5120_map_info amap;
57};
58
59struct flash_desc {
60    u32 phys;
61    u32 srs_shift;
62};
63
64/*
65 * Globals
66 */
67static DEFINE_SPINLOCK(adm5120_flash_spin);
68#define FLASH_LOCK() spin_lock(&adm5120_flash_spin)
69#define FLASH_UNLOCK() spin_unlock(&adm5120_flash_spin)
70
71static u32 flash_bankwidths[4] = { 1, 2, 4, 0 };
72
73static u32 flash_sizes[8] = {
74    0, 512*1024, 1024*1024, 2*1024*1024,
75    4*1024*1024, 0, 0, 0
76};
77
78static struct flash_desc flash_descs[2] = {
79    {
80        .phys = ADM5120_SRAM0_BASE,
81        .srs_shift = MEMCTRL_SRS0_SHIFT,
82    }, {
83        .phys = ADM5120_SRAM1_BASE,
84        .srs_shift = MEMCTRL_SRS1_SHIFT,
85    }
86};
87
88static const char const *probe_types[] = {
89    "cfi_probe",
90    "jedec_probe",
91    "map_rom",
92    NULL
93};
94
95static const char const *parse_types[] = {
96    "cmdlinepart",
97#ifdef CONFIG_MTD_REDBOOT_PARTS
98    "RedBoot",
99#endif
100#ifdef CONFIG_MTD_MYLOADER_PARTS
101    "MyLoader",
102#endif
103    NULL,
104};
105
106#define BANK_SIZE (2<<20)
107#define BANK_SIZE_MAX (4<<20)
108#define BANK_OFFS_MASK (BANK_SIZE-1)
109#define BANK_START_MASK (~BANK_OFFS_MASK)
110
111static inline struct adm5120_map_info *map_to_amap(struct map_info *map)
112{
113    return (struct adm5120_map_info *)map;
114}
115
116static void adm5120_flash_switchbank(struct map_info *map,
117        unsigned long ofs)
118{
119    struct adm5120_map_info *amap = map_to_amap(map);
120    unsigned bank;
121
122    if (amap->switch_bank == NULL)
123        return;
124
125    bank = (ofs & BANK_START_MASK) >> 21;
126    if (bank > 1)
127        BUG();
128
129    MAP_DBG(map, "switching to bank %u, ofs=%lX\n", bank, ofs);
130    amap->switch_bank(bank);
131}
132
133static map_word adm5120_flash_read(struct map_info *map, unsigned long ofs)
134{
135    struct adm5120_map_info *amap = map_to_amap(map);
136    map_word ret;
137
138    MAP_DBG(map, "reading from ofs %lX\n", ofs);
139
140    if (ofs >= amap->window_size)
141        return map_word_ff(map);
142
143    FLASH_LOCK();
144    adm5120_flash_switchbank(map, ofs);
145    ret = inline_map_read(map, (ofs & (amap->window_size-1)));
146    FLASH_UNLOCK();
147
148    return ret;
149}
150
151static void adm5120_flash_write(struct map_info *map, const map_word datum,
152        unsigned long ofs)
153{
154    struct adm5120_map_info *amap = map_to_amap(map);
155
156    MAP_DBG(map, "writing to ofs %lX\n", ofs);
157
158    if (ofs > amap->window_size)
159        return;
160
161    FLASH_LOCK();
162    adm5120_flash_switchbank(map, ofs);
163    inline_map_write(map, datum, (ofs & (amap->window_size-1)));
164    FLASH_UNLOCK();
165}
166
167static void adm5120_flash_copy_from(struct map_info *map, void *to,
168        unsigned long from, ssize_t len)
169{
170    struct adm5120_map_info *amap = map_to_amap(map);
171    char *p;
172    ssize_t t;
173
174    MAP_DBG(map, "copy_from, to=%lX, from=%lX, len=%lX\n",
175        (unsigned long)to, from, (unsigned long)len);
176
177    if (from > amap->window_size)
178        return;
179
180    p = (char *)to;
181    while (len > 0) {
182        t = len;
183        if ((from < BANK_SIZE) && ((from+len) > BANK_SIZE))
184            t = BANK_SIZE-from;
185
186        FLASH_LOCK();
187        MAP_DBG(map, "copying %lu byte(s) from %lX to %lX\n",
188            (unsigned long)t, (from & (amap->window_size-1)),
189            (unsigned long)p);
190        adm5120_flash_switchbank(map, from);
191        inline_map_copy_from(map, p, (from & (amap->window_size-1)), t);
192        FLASH_UNLOCK();
193        p += t;
194        from += t;
195        len -= t;
196    }
197}
198
199static int adm5120_flash_initres(struct adm5120_flash_info *info)
200{
201    struct map_info *map = &info->amap.map;
202    int err = 0;
203
204    info->res = request_mem_region(map->phys, info->amap.window_size,
205            map->name);
206    if (info->res == NULL) {
207        MAP_ERR(map, "could not reserve memory region\n");
208        err = -ENOMEM;
209        goto out;
210    }
211
212    map->virt = ioremap_nocache(map->phys, info->amap.window_size);
213    if (map->virt == NULL) {
214        MAP_ERR(map, "failed to ioremap flash region\n");
215        err = -ENOMEM;
216        goto out;
217    }
218
219out:
220    return err;
221}
222
223static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
224        struct platform_device *dev)
225{
226    struct map_info *map = &info->amap.map;
227    struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
228    struct flash_desc *fdesc;
229    u32 t = 0;
230
231    map->name = dev_name(&dev->dev);
232
233    if (dev->id > 1) {
234        MAP_ERR(map, "invalid flash id\n");
235        goto err_out;
236    }
237
238    fdesc = &flash_descs[dev->id];
239
240    if (pdata)
241        info->amap.window_size = pdata->window_size;
242
243    if (info->amap.window_size == 0) {
244        /* get memory window size */
245        t = SW_READ_REG(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
246        t &= MEMCTRL_SRS_MASK;
247        info->amap.window_size = flash_sizes[t];
248    }
249
250    if (info->amap.window_size == 0) {
251        MAP_ERR(map, "unable to determine window size\n");
252        goto err_out;
253    }
254
255    /* get flash bus width */
256    switch (dev->id) {
257    case 0:
258        t = MPMC_READ_REG(SC1) & SC_MW_MASK;
259        break;
260    case 1:
261        t = MPMC_READ_REG(SC0) & SC_MW_MASK;
262        break;
263    }
264    map->bankwidth = flash_bankwidths[t];
265    if (map->bankwidth == 0) {
266        MAP_ERR(map, "invalid bus width detected\n");
267        goto err_out;
268    }
269
270    map->phys = fdesc->phys;
271    map->size = BANK_SIZE_MAX;
272
273    simple_map_init(map);
274    map->read = adm5120_flash_read;
275    map->write = adm5120_flash_write;
276    map->copy_from = adm5120_flash_copy_from;
277
278    if (pdata) {
279        map->set_vpp = pdata->set_vpp;
280        info->amap.switch_bank = pdata->switch_bank;
281    }
282
283    info->dev = dev;
284
285    MAP_INFO(map, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
286        (unsigned long)map->phys,
287        (unsigned long)info->amap.window_size >> 10,
288        map->bankwidth*8);
289
290    return 0;
291
292err_out:
293    return -ENODEV;
294}
295
296static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
297{
298    struct map_info *map = &info->amap.map;
299
300    if (info->mtd->size <= BANK_SIZE)
301        /* no bank switching needed */
302        return;
303
304    if (info->amap.switch_bank) {
305        info->amap.window_size = info->mtd->size;
306        return;
307    }
308
309    MAP_ERR(map, "reduce visibility from %ldKiB to %ldKiB\n",
310        (unsigned long)map->size >> 10,
311        (unsigned long)info->mtd->size >> 10);
312
313    info->mtd->size = info->amap.window_size;
314}
315
316static int adm5120_flash_remove(struct platform_device *dev)
317{
318    struct adm5120_flash_info *info;
319
320    info = platform_get_drvdata(dev);
321    if (info == NULL)
322        return 0;
323
324    platform_set_drvdata(dev, NULL);
325
326    if (info->mtd != NULL) {
327        mtd_device_unregister(info->mtd);
328        map_destroy(info->mtd);
329    }
330
331    if (info->amap.map.virt != NULL)
332        iounmap(info->amap.map.virt);
333
334    if (info->res != NULL) {
335        release_resource(info->res);
336        kfree(info->res);
337    }
338
339    return 0;
340}
341
342static int adm5120_flash_probe(struct platform_device *dev)
343{
344    struct adm5120_flash_platform_data *pdata;
345    struct adm5120_flash_info *info;
346    struct map_info *map;
347    const char **probe_type;
348    int err;
349
350    pdata = dev->dev.platform_data;
351    if (!pdata) {
352        dev_err(&dev->dev, "no platform data\n");
353        return -EINVAL;
354    }
355
356    info = kzalloc(sizeof(*info), GFP_KERNEL);
357    if (info == NULL) {
358        err = -ENOMEM;
359        goto err_out;
360    }
361
362    platform_set_drvdata(dev, info);
363
364    err = adm5120_flash_initinfo(info, dev);
365    if (err)
366        goto err_out;
367
368    err = adm5120_flash_initres(info);
369    if (err)
370        goto err_out;
371
372    map = &info->amap.map;
373    for (probe_type = probe_types; info->mtd == NULL && *probe_type != NULL;
374        probe_type++)
375        info->mtd = do_map_probe(*probe_type, map);
376
377    if (info->mtd == NULL) {
378        MAP_ERR(map, "map_probe failed\n");
379        err = -ENXIO;
380        goto err_out;
381    }
382
383    adm5120_flash_initbanks(info);
384
385    if (info->mtd->size < info->amap.window_size) {
386        /* readjust resources */
387        iounmap(map->virt);
388        release_resource(info->res);
389        kfree(info->res);
390
391        info->amap.window_size = info->mtd->size;
392        map->size = info->mtd->size;
393        MAP_INFO(map, "reducing map size to %ldKiB\n",
394            (unsigned long)map->size >> 10);
395        err = adm5120_flash_initres(info);
396        if (err)
397            goto err_out;
398    }
399
400    MAP_INFO(map, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
401        (unsigned long)map->phys, (unsigned long)info->mtd->size >> 10,
402        map->bankwidth*8);
403
404    info->mtd->owner = THIS_MODULE;
405
406    err = mtd_device_parse_register(info->mtd, parse_types, 0,
407                      pdata->parts, pdata->nr_parts);
408    if (err)
409        goto err_out;
410
411    return 0;
412
413err_out:
414    adm5120_flash_remove(dev);
415    return err;
416}
417
418#ifdef CONFIG_PM
419static int adm5120_flash_suspend(struct platform_device *dev,
420        pm_message_t state)
421{
422    struct adm5120_flash_info *info = platform_get_drvdata(dev);
423    int ret = 0;
424
425    if (info)
426        ret = info->mtd->suspend(info->mtd);
427
428    return ret;
429}
430
431static int adm5120_flash_resume(struct platform_device *dev)
432{
433    struct adm5120_flash_info *info = platform_get_drvdata(dev);
434
435    if (info)
436        info->mtd->resume(info->mtd);
437
438    return 0;
439}
440
441static void adm5120_flash_shutdown(struct platform_device *dev)
442{
443    struct adm5120_flash_info *info = platform_get_drvdata(dev);
444
445    if (info && info->mtd->suspend(info->mtd) == 0)
446        info->mtd->resume(info->mtd);
447}
448#endif
449
450static struct platform_driver adm5120_flash_driver = {
451    .probe = adm5120_flash_probe,
452    .remove = adm5120_flash_remove,
453#ifdef CONFIG_PM
454    .suspend = adm5120_flash_suspend,
455    .resume = adm5120_flash_resume,
456    .shutdown = adm5120_flash_shutdown,
457#endif
458    .driver = {
459        .name = DRV_NAME,
460    },
461};
462
463static int __init adm5120_flash_init(void)
464{
465    int err;
466
467    err = platform_driver_register(&adm5120_flash_driver);
468
469    return err;
470}
471
472static void __exit adm5120_flash_exit(void)
473{
474    platform_driver_unregister(&adm5120_flash_driver);
475}
476
477module_init(adm5120_flash_init);
478module_exit(adm5120_flash_exit);
479
480MODULE_LICENSE("GPL v2");
481MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
482MODULE_DESCRIPTION(DRV_DESC);
483

Archive Download this file



interactive