John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify it |
| 3 | * under the terms of the GNU General Public License version 2 as published |
| 4 | * by the Free Software Foundation. |
| 5 | * |
| 6 | * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE |
| 7 | * Copyright (C) 2010 John Crispin <blogic@openwrt.org> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/mtd/mtd.h> |
| 17 | #include <linux/mtd/map.h> |
| 18 | #include <linux/mtd/partitions.h> |
| 19 | #include <linux/mtd/cfi.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/mtd/physmap.h> |
| 22 | |
| 23 | #include <lantiq_soc.h> |
| 24 | #include <lantiq_platform.h> |
| 25 | |
| 26 | /* |
| 27 | * The NOR flash is connected to the same external bus unit (EBU) as PCI. |
| 28 | * To make PCI work we need to enable the endianness swapping for the address |
| 29 | * written to the EBU. This endianness swapping works for PCI correctly but |
| 30 | * fails for attached NOR devices. To workaround this we need to use a complex |
| 31 | * map. The workaround involves swapping all addresses whilst probing the chip. |
| 32 | * Once probing is complete we stop swapping the addresses but swizzle the |
| 33 | * unlock addresses to ensure that access to the NOR device works correctly. |
| 34 | */ |
| 35 | |
| 36 | enum { |
| 37 | LTQ_NOR_PROBING, |
| 38 | LTQ_NOR_NORMAL |
| 39 | }; |
| 40 | |
| 41 | struct ltq_mtd { |
| 42 | struct resource *res; |
| 43 | struct mtd_info *mtd; |
| 44 | struct map_info *map; |
| 45 | }; |
| 46 | |
| 47 | static char ltq_map_name[] = "ltq_nor"; |
| 48 | |
| 49 | static map_word |
| 50 | ltq_read16(struct map_info *map, unsigned long adr) |
| 51 | { |
| 52 | unsigned long flags; |
| 53 | map_word temp; |
| 54 | |
| 55 | if (map->map_priv_1 == LTQ_NOR_PROBING) |
| 56 | adr ^= 2; |
| 57 | spin_lock_irqsave(&ebu_lock, flags); |
| 58 | temp.x[0] = *(u16 *)(map->virt + adr); |
| 59 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 60 | return temp; |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | ltq_write16(struct map_info *map, map_word d, unsigned long adr) |
| 65 | { |
| 66 | unsigned long flags; |
| 67 | |
| 68 | if (map->map_priv_1 == LTQ_NOR_PROBING) |
| 69 | adr ^= 2; |
| 70 | spin_lock_irqsave(&ebu_lock, flags); |
| 71 | *(u16 *)(map->virt + adr) = d.x[0]; |
| 72 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * The following 2 functions copy data between iomem and a cached memory |
| 77 | * section. As memcpy() makes use of pre-fetching we cannot use it here. |
| 78 | * The normal alternative of using memcpy_{to,from}io also makes use of |
| 79 | * memcpy() on MIPS so it is not applicable either. We are therefore stuck |
| 80 | * with having to use our own loop. |
| 81 | */ |
| 82 | static void |
| 83 | ltq_copy_from(struct map_info *map, void *to, |
| 84 | unsigned long from, ssize_t len) |
| 85 | { |
| 86 | unsigned char *f = (unsigned char *)map->virt + from; |
| 87 | unsigned char *t = (unsigned char *)to; |
| 88 | unsigned long flags; |
| 89 | |
| 90 | spin_lock_irqsave(&ebu_lock, flags); |
| 91 | while (len--) |
| 92 | *t++ = *f++; |
| 93 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | ltq_copy_to(struct map_info *map, unsigned long to, |
| 98 | const void *from, ssize_t len) |
| 99 | { |
| 100 | unsigned char *f = (unsigned char *)from; |
| 101 | unsigned char *t = (unsigned char *)map->virt + to; |
| 102 | unsigned long flags; |
| 103 | |
| 104 | spin_lock_irqsave(&ebu_lock, flags); |
| 105 | while (len--) |
| 106 | *t++ = *f++; |
| 107 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 108 | } |
| 109 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 110 | static int __init |
| 111 | ltq_mtd_probe(struct platform_device *pdev) |
| 112 | { |
| 113 | struct physmap_flash_data *ltq_mtd_data = dev_get_platdata(&pdev->dev); |
| 114 | struct ltq_mtd *ltq_mtd; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 115 | struct resource *res; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 116 | struct cfi_private *cfi; |
| 117 | int err; |
| 118 | |
| 119 | ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL); |
| 120 | platform_set_drvdata(pdev, ltq_mtd); |
| 121 | |
| 122 | ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 123 | if (!ltq_mtd->res) { |
| 124 | dev_err(&pdev->dev, "failed to get memory resource"); |
| 125 | err = -ENOENT; |
| 126 | goto err_out; |
| 127 | } |
| 128 | |
| 129 | res = devm_request_mem_region(&pdev->dev, ltq_mtd->res->start, |
| 130 | resource_size(ltq_mtd->res), dev_name(&pdev->dev)); |
| 131 | if (!ltq_mtd->res) { |
| 132 | dev_err(&pdev->dev, "failed to request mem resource"); |
| 133 | err = -EBUSY; |
| 134 | goto err_out; |
| 135 | } |
| 136 | |
| 137 | ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL); |
| 138 | ltq_mtd->map->phys = res->start; |
| 139 | ltq_mtd->map->size = resource_size(res); |
| 140 | ltq_mtd->map->virt = devm_ioremap_nocache(&pdev->dev, |
| 141 | ltq_mtd->map->phys, ltq_mtd->map->size); |
| 142 | if (!ltq_mtd->map->virt) { |
| 143 | dev_err(&pdev->dev, "failed to ioremap!\n"); |
| 144 | err = -ENOMEM; |
| 145 | goto err_free; |
| 146 | } |
| 147 | |
| 148 | ltq_mtd->map->name = ltq_map_name; |
| 149 | ltq_mtd->map->bankwidth = 2; |
| 150 | ltq_mtd->map->read = ltq_read16; |
| 151 | ltq_mtd->map->write = ltq_write16; |
| 152 | ltq_mtd->map->copy_from = ltq_copy_from; |
| 153 | ltq_mtd->map->copy_to = ltq_copy_to; |
| 154 | |
| 155 | ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING; |
| 156 | ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map); |
| 157 | ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL; |
| 158 | |
| 159 | if (!ltq_mtd->mtd) { |
| 160 | dev_err(&pdev->dev, "probing failed\n"); |
| 161 | err = -ENXIO; |
Julia Lawall | af32b36 | 2011-12-26 18:38:01 +0100 | [diff] [blame] | 162 | goto err_free; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ltq_mtd->mtd->owner = THIS_MODULE; |
| 166 | |
| 167 | cfi = ltq_mtd->map->fldrv_priv; |
| 168 | cfi->addr_unlock1 ^= 1; |
| 169 | cfi->addr_unlock2 ^= 1; |
| 170 | |
Dmitry Eremin-Solenikov | ca97dec | 2011-06-02 17:59:49 +0400 | [diff] [blame] | 171 | err = mtd_device_parse_register(ltq_mtd->mtd, NULL, 0, |
| 172 | ltq_mtd_data->parts, ltq_mtd_data->nr_parts); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 173 | if (err) { |
| 174 | dev_err(&pdev->dev, "failed to add partitions\n"); |
| 175 | goto err_destroy; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | err_destroy: |
| 181 | map_destroy(ltq_mtd->mtd); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 182 | err_free: |
| 183 | kfree(ltq_mtd->map); |
| 184 | err_out: |
| 185 | kfree(ltq_mtd); |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | static int __devexit |
| 190 | ltq_mtd_remove(struct platform_device *pdev) |
| 191 | { |
| 192 | struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev); |
| 193 | |
| 194 | if (ltq_mtd) { |
| 195 | if (ltq_mtd->mtd) { |
John Crispin | 2e5db86 | 2011-06-09 19:57:33 +0200 | [diff] [blame] | 196 | mtd_device_unregister(ltq_mtd->mtd); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 197 | map_destroy(ltq_mtd->mtd); |
| 198 | } |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 199 | kfree(ltq_mtd->map); |
| 200 | kfree(ltq_mtd); |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static struct platform_driver ltq_mtd_driver = { |
| 206 | .remove = __devexit_p(ltq_mtd_remove), |
| 207 | .driver = { |
| 208 | .name = "ltq_nor", |
| 209 | .owner = THIS_MODULE, |
| 210 | }, |
| 211 | }; |
| 212 | |
| 213 | static int __init |
| 214 | init_ltq_mtd(void) |
| 215 | { |
| 216 | int ret = platform_driver_probe(<q_mtd_driver, ltq_mtd_probe); |
| 217 | |
| 218 | if (ret) |
| 219 | pr_err("ltq_nor: error registering platform driver"); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | static void __exit |
| 224 | exit_ltq_mtd(void) |
| 225 | { |
| 226 | platform_driver_unregister(<q_mtd_driver); |
| 227 | } |
| 228 | |
| 229 | module_init(init_ltq_mtd); |
| 230 | module_exit(exit_ltq_mtd); |
| 231 | |
| 232 | MODULE_LICENSE("GPL"); |
| 233 | MODULE_AUTHOR("John Crispin <blogic@openwrt.org>"); |
| 234 | MODULE_DESCRIPTION("Lantiq SoC NOR"); |