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-flash.h" 27 28#define ADRS(offset) (COBALT_BUS_FLASH_BASE + offset) 29 30static 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 37static map_word flash_read16(struct map_info *map, unsigned long offset) 38{ 39 map_word r; 40 41 r.x[0] = cobalt_bus_read32(map->virt, ADRS(offset)); 42 if (offset & 0x2) 43 r.x[0] >>= 16; 44 else 45 r.x[0] &= 0x0000ffff; 46 47 return r; 48} 49 50static void flash_write16(struct map_info *map, const map_word datum, 51 unsigned long offset) 52{ 53 u16 data = (u16)datum.x[0]; 54 55 cobalt_bus_write16(map->virt, ADRS(offset), data); 56} 57 58static void flash_copy_from(struct map_info *map, void *to, 59 unsigned long from, ssize_t len) 60{ 61 u32 src = from; 62 u8 *dest = to; 63 u32 data; 64 65 while (len) { 66 data = cobalt_bus_read32(map->virt, ADRS(src)); 67 do { 68 *dest = data >> (8 * (src & 3)); 69 src++; 70 dest++; 71 len--; 72 } while (len && (src % 4)); 73 } 74} 75 76static void flash_copy_to(struct map_info *map, unsigned long to, 77 const void *from, ssize_t len) 78{ 79 const u8 *src = from; 80 u32 dest = to; 81 82 pr_info("%s: offset 0x%x: length %zu\n", __func__, dest, len); 83 while (len) { 84 u16 data = 0xffff; 85 86 do { 87 data = *src << (8 * (dest & 1)); 88 src++; 89 dest++; 90 len--; 91 } while (len && (dest % 2)); 92 93 cobalt_bus_write16(map->virt, ADRS(dest - 2), data); 94 } 95} 96 97int cobalt_flash_probe(struct cobalt *cobalt) 98{ 99 struct map_info *map = &cobalt_flash_map; 100 struct mtd_info *mtd; 101 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; 108 109 mtd = do_map_probe("cfi_probe", map); 110 cobalt->mtd = mtd; 111 if (!mtd) { 112 cobalt_err("Probe CFI flash failed!\n"); 113 return -1; 114 } 115 116 mtd->owner = THIS_MODULE; 117 mtd->dev.parent = &cobalt->pci_dev->dev; 118 mtd_device_register(mtd, NULL, 0); 119 return 0; 120} 121 122void cobalt_flash_remove(struct cobalt *cobalt) 123{ 124 if (cobalt->mtd) { 125 mtd_device_unregister(cobalt->mtd); 126 map_destroy(cobalt->mtd); 127 } 128} 129