xref: /openbmc/linux/drivers/media/pci/cobalt/cobalt-flash.c (revision 85756a069c55e0315ac5990806899cfb607b987f)
1 /*
2  *  Cobalt NOR flash functions
3  *
4  *  Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
5  *  All rights reserved.
6  *
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.
10  *
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
18  *  SOFTWARE.
19  */
20 
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/map.h>
23 #include <linux/mtd/cfi.h>
24 #include <linux/time.h>
25 
26 #include "cobalt-driver.h"
27 
28 #define ADRS(offset) (COBALT_BUS_FLASH_BASE + offset)
29 
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  */
35 };
36 
37 static map_word flash_read16(struct map_info *map, unsigned long offset)
38 {
39 	struct cobalt *cobalt = map->virt;
40 	map_word r;
41 
42 	r.x[0] = cobalt_bus_read32(cobalt, ADRS(offset));
43 	if (offset & 0x2)
44 		r.x[0] >>= 16;
45 	else
46 		r.x[0] &= 0x0000ffff;
47 
48 	return r;
49 }
50 
51 static void flash_write16(struct map_info *map, const map_word datum,
52 			  unsigned long offset)
53 {
54 	struct cobalt *cobalt = map->virt;
55 	u16 data = (u16)datum.x[0];
56 
57 	cobalt_bus_write16(cobalt, ADRS(offset), data);
58 }
59 
60 static void flash_copy_from(struct map_info *map, void *to,
61 			    unsigned long from, ssize_t len)
62 {
63 	struct cobalt *cobalt = map->virt;
64 	u32 src = from;
65 	u8 *dest = to;
66 	u32 data;
67 
68 	while (len) {
69 		data = cobalt_bus_read32(cobalt, ADRS(src));
70 		do {
71 			*dest = data >> (8 * (src & 3));
72 			src++;
73 			dest++;
74 			len--;
75 		} while (len && (src % 4));
76 	}
77 }
78 
79 static void flash_copy_to(struct map_info *map, unsigned long to,
80 			  const void *from, ssize_t len)
81 {
82 	struct cobalt *cobalt = map->virt;
83 	const u8 *src = from;
84 	u32 dest = to;
85 
86 	cobalt_info("%s: offset 0x%x: length %zu\n", __func__, dest, len);
87 	while (len) {
88 		u16 data = 0xffff;
89 
90 		do {
91 			data = *src << (8 * (dest & 1));
92 			src++;
93 			dest++;
94 			len--;
95 		} while (len && (dest % 2));
96 
97 		cobalt_bus_write16(cobalt, ADRS(dest - 2), data);
98 	}
99 }
100 
101 int cobalt_flash_probe(struct cobalt *cobalt)
102 {
103 	struct map_info *map = &cobalt_flash_map;
104 	struct mtd_info *mtd;
105 
106 	BUG_ON(!map_bankwidth_supported(map->bankwidth));
107 	map->virt = cobalt;
108 	map->read = flash_read16;
109 	map->write = flash_write16;
110 	map->copy_from = flash_copy_from;
111 	map->copy_to = flash_copy_to;
112 
113 	mtd = do_map_probe("cfi_probe", map);
114 	cobalt->mtd = mtd;
115 	if (!mtd) {
116 		cobalt_err("Probe CFI flash failed!\n");
117 		return -1;
118 	}
119 
120 	mtd->owner = THIS_MODULE;
121 	mtd->dev.parent = &cobalt->pci_dev->dev;
122 	mtd_device_register(mtd, NULL, 0);
123 	return 0;
124 }
125 
126 void cobalt_flash_remove(struct cobalt *cobalt)
127 {
128 	if (cobalt->mtd) {
129 		mtd_device_unregister(cobalt->mtd);
130 		map_destroy(cobalt->mtd);
131 	}
132 }
133