1 // SPDX-License-Identifier: GPL-2.0-only
3 * Cobalt NOR flash functions
5 * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/map.h>
11 #include <linux/mtd/cfi.h>
12 #include <linux/time.h>
14 #include "cobalt-flash.h"
16 #define ADRS(offset) (COBALT_BUS_FLASH_BASE + offset)
18 static struct map_info cobalt_flash_map
= {
19 .name
= "cobalt-flash",
20 .bankwidth
= 2, /* 16 bits */
21 .size
= 0x4000000, /* 64MB */
22 .phys
= 0, /* offset */
25 static map_word
flash_read16(struct map_info
*map
, unsigned long offset
)
29 r
.x
[0] = cobalt_bus_read32(map
->virt
, ADRS(offset
));
38 static void flash_write16(struct map_info
*map
, const map_word datum
,
41 u16 data
= (u16
)datum
.x
[0];
43 cobalt_bus_write16(map
->virt
, ADRS(offset
), data
);
46 static void flash_copy_from(struct map_info
*map
, void *to
,
47 unsigned long from
, ssize_t len
)
54 data
= cobalt_bus_read32(map
->virt
, ADRS(src
));
56 *dest
= data
>> (8 * (src
& 3));
60 } while (len
&& (src
% 4));
64 static void flash_copy_to(struct map_info
*map
, unsigned long to
,
65 const void *from
, ssize_t len
)
70 pr_info("%s: offset 0x%x: length %zu\n", __func__
, dest
, len
);
75 data
= *src
<< (8 * (dest
& 1));
79 } while (len
&& (dest
% 2));
81 cobalt_bus_write16(map
->virt
, ADRS(dest
- 2), data
);
85 int cobalt_flash_probe(struct cobalt
*cobalt
)
87 struct map_info
*map
= &cobalt_flash_map
;
90 BUG_ON(!map_bankwidth_supported(map
->bankwidth
));
91 map
->virt
= cobalt
->bar1
;
92 map
->read
= flash_read16
;
93 map
->write
= flash_write16
;
94 map
->copy_from
= flash_copy_from
;
95 map
->copy_to
= flash_copy_to
;
97 mtd
= do_map_probe("cfi_probe", map
);
100 cobalt_err("Probe CFI flash failed!\n");
104 mtd
->owner
= THIS_MODULE
;
105 mtd
->dev
.parent
= &cobalt
->pci_dev
->dev
;
106 mtd_device_register(mtd
, NULL
, 0);
110 void cobalt_flash_remove(struct cobalt
*cobalt
)
113 mtd_device_unregister(cobalt
->mtd
);
114 map_destroy(cobalt
->mtd
);