2 * Cobalt NOR flash functions
4 * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
7 * This program is free software; you may redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
15 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/map.h>
23 #include <linux/mtd/cfi.h>
24 #include <linux/time.h>
26 #include "cobalt-flash.h"
28 #define ADRS(offset) (COBALT_BUS_FLASH_BASE + offset)
30 static struct map_info cobalt_flash_map
= {
31 .name
= "cobalt-flash",
32 .bankwidth
= 2, /* 16 bits */
33 .size
= 0x4000000, /* 64MB */
34 .phys
= 0, /* offset */
37 static map_word
flash_read16(struct map_info
*map
, unsigned long offset
)
41 r
.x
[0] = cobalt_bus_read32(map
->virt
, ADRS(offset
));
50 static void flash_write16(struct map_info
*map
, const map_word datum
,
53 u16 data
= (u16
)datum
.x
[0];
55 cobalt_bus_write16(map
->virt
, ADRS(offset
), data
);
58 static void flash_copy_from(struct map_info
*map
, void *to
,
59 unsigned long from
, ssize_t len
)
66 data
= cobalt_bus_read32(map
->virt
, ADRS(src
));
68 *dest
= data
>> (8 * (src
& 3));
72 } while (len
&& (src
% 4));
76 static void flash_copy_to(struct map_info
*map
, unsigned long to
,
77 const void *from
, ssize_t len
)
82 pr_info("%s: offset 0x%x: length %zu\n", __func__
, dest
, len
);
87 data
= *src
<< (8 * (dest
& 1));
91 } while (len
&& (dest
% 2));
93 cobalt_bus_write16(map
->virt
, ADRS(dest
- 2), data
);
97 int cobalt_flash_probe(struct cobalt
*cobalt
)
99 struct map_info
*map
= &cobalt_flash_map
;
100 struct mtd_info
*mtd
;
102 BUG_ON(!map_bankwidth_supported(map
->bankwidth
));
103 map
->virt
= cobalt
->bar1
;
104 map
->read
= flash_read16
;
105 map
->write
= flash_write16
;
106 map
->copy_from
= flash_copy_from
;
107 map
->copy_to
= flash_copy_to
;
109 mtd
= do_map_probe("cfi_probe", map
);
112 cobalt_err("Probe CFI flash failed!\n");
116 mtd
->owner
= THIS_MODULE
;
117 mtd
->dev
.parent
= &cobalt
->pci_dev
->dev
;
118 mtd_device_register(mtd
, NULL
, 0);
122 void cobalt_flash_remove(struct cobalt
*cobalt
)
125 mtd_device_unregister(cobalt
->mtd
);
126 map_destroy(cobalt
->mtd
);