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 | |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame^] | 10 | #include <linux/err.h> |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/mtd/mtd.h> |
| 18 | #include <linux/mtd/map.h> |
| 19 | #include <linux/mtd/partitions.h> |
| 20 | #include <linux/mtd/cfi.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/mtd/physmap.h> |
John Crispin | fa09ede | 2012-05-05 15:41:42 +0200 | [diff] [blame] | 23 | #include <linux/of.h> |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 24 | |
| 25 | #include <lantiq_soc.h> |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * The NOR flash is connected to the same external bus unit (EBU) as PCI. |
| 29 | * To make PCI work we need to enable the endianness swapping for the address |
| 30 | * written to the EBU. This endianness swapping works for PCI correctly but |
| 31 | * fails for attached NOR devices. To workaround this we need to use a complex |
| 32 | * map. The workaround involves swapping all addresses whilst probing the chip. |
| 33 | * Once probing is complete we stop swapping the addresses but swizzle the |
| 34 | * unlock addresses to ensure that access to the NOR device works correctly. |
| 35 | */ |
| 36 | |
| 37 | enum { |
| 38 | LTQ_NOR_PROBING, |
| 39 | LTQ_NOR_NORMAL |
| 40 | }; |
| 41 | |
| 42 | struct ltq_mtd { |
| 43 | struct resource *res; |
| 44 | struct mtd_info *mtd; |
| 45 | struct map_info *map; |
| 46 | }; |
| 47 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 48 | static const char ltq_map_name[] = "ltq_nor"; |
Bill Pemberton | 5e75e86 | 2012-11-19 13:25:12 -0500 | [diff] [blame] | 49 | static const char *ltq_probe_types[] = { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 50 | "cmdlinepart", "ofpart", NULL }; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 51 | |
| 52 | static map_word |
| 53 | ltq_read16(struct map_info *map, unsigned long adr) |
| 54 | { |
| 55 | unsigned long flags; |
| 56 | map_word temp; |
| 57 | |
| 58 | if (map->map_priv_1 == LTQ_NOR_PROBING) |
| 59 | adr ^= 2; |
| 60 | spin_lock_irqsave(&ebu_lock, flags); |
| 61 | temp.x[0] = *(u16 *)(map->virt + adr); |
| 62 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 63 | return temp; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | ltq_write16(struct map_info *map, map_word d, unsigned long adr) |
| 68 | { |
| 69 | unsigned long flags; |
| 70 | |
| 71 | if (map->map_priv_1 == LTQ_NOR_PROBING) |
| 72 | adr ^= 2; |
| 73 | spin_lock_irqsave(&ebu_lock, flags); |
| 74 | *(u16 *)(map->virt + adr) = d.x[0]; |
| 75 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * The following 2 functions copy data between iomem and a cached memory |
| 80 | * section. As memcpy() makes use of pre-fetching we cannot use it here. |
| 81 | * The normal alternative of using memcpy_{to,from}io also makes use of |
| 82 | * memcpy() on MIPS so it is not applicable either. We are therefore stuck |
| 83 | * with having to use our own loop. |
| 84 | */ |
| 85 | static void |
| 86 | ltq_copy_from(struct map_info *map, void *to, |
| 87 | unsigned long from, ssize_t len) |
| 88 | { |
| 89 | unsigned char *f = (unsigned char *)map->virt + from; |
| 90 | unsigned char *t = (unsigned char *)to; |
| 91 | unsigned long flags; |
| 92 | |
| 93 | spin_lock_irqsave(&ebu_lock, flags); |
| 94 | while (len--) |
| 95 | *t++ = *f++; |
| 96 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | ltq_copy_to(struct map_info *map, unsigned long to, |
| 101 | const void *from, ssize_t len) |
| 102 | { |
| 103 | unsigned char *f = (unsigned char *)from; |
| 104 | unsigned char *t = (unsigned char *)map->virt + to; |
| 105 | unsigned long flags; |
| 106 | |
| 107 | spin_lock_irqsave(&ebu_lock, flags); |
| 108 | while (len--) |
| 109 | *t++ = *f++; |
| 110 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 111 | } |
| 112 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 113 | static int |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 114 | ltq_mtd_probe(struct platform_device *pdev) |
| 115 | { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 116 | struct mtd_part_parser_data ppdata; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 117 | struct ltq_mtd *ltq_mtd; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 118 | struct cfi_private *cfi; |
| 119 | int err; |
| 120 | |
John Crispin | fa09ede | 2012-05-05 15:41:42 +0200 | [diff] [blame] | 121 | if (of_machine_is_compatible("lantiq,falcon") && |
| 122 | (ltq_boot_select() != BS_FLASH)) { |
| 123 | dev_err(&pdev->dev, "invalid bootstrap options\n"); |
| 124 | return -ENODEV; |
| 125 | } |
| 126 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 127 | ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL); |
| 128 | platform_set_drvdata(pdev, ltq_mtd); |
| 129 | |
| 130 | ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 131 | if (!ltq_mtd->res) { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 132 | dev_err(&pdev->dev, "failed to get memory resource\n"); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 133 | err = -ENOENT; |
| 134 | goto err_out; |
| 135 | } |
| 136 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 137 | ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL); |
| 138 | ltq_mtd->map->phys = ltq_mtd->res->start; |
| 139 | ltq_mtd->map->size = resource_size(ltq_mtd->res); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame^] | 140 | ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res); |
| 141 | if (IS_ERR(ltq_mtd->map->virt)) { |
| 142 | err = PTR_ERR(ltq_mtd->map->virt); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 143 | goto err_out; |
| 144 | } |
| 145 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 146 | ltq_mtd->map->name = ltq_map_name; |
| 147 | ltq_mtd->map->bankwidth = 2; |
| 148 | ltq_mtd->map->read = ltq_read16; |
| 149 | ltq_mtd->map->write = ltq_write16; |
| 150 | ltq_mtd->map->copy_from = ltq_copy_from; |
| 151 | ltq_mtd->map->copy_to = ltq_copy_to; |
| 152 | |
| 153 | ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING; |
| 154 | ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map); |
| 155 | ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL; |
| 156 | |
| 157 | if (!ltq_mtd->mtd) { |
| 158 | dev_err(&pdev->dev, "probing failed\n"); |
| 159 | err = -ENXIO; |
Julia Lawall | af32b36 | 2011-12-26 18:38:01 +0100 | [diff] [blame] | 160 | goto err_free; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ltq_mtd->mtd->owner = THIS_MODULE; |
| 164 | |
| 165 | cfi = ltq_mtd->map->fldrv_priv; |
| 166 | cfi->addr_unlock1 ^= 1; |
| 167 | cfi->addr_unlock2 ^= 1; |
| 168 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 169 | ppdata.of_node = pdev->dev.of_node; |
| 170 | err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types, |
| 171 | &ppdata, NULL, 0); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 172 | if (err) { |
| 173 | dev_err(&pdev->dev, "failed to add partitions\n"); |
| 174 | goto err_destroy; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | |
| 179 | err_destroy: |
| 180 | map_destroy(ltq_mtd->mtd); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 181 | err_free: |
| 182 | kfree(ltq_mtd->map); |
| 183 | err_out: |
| 184 | kfree(ltq_mtd); |
| 185 | return err; |
| 186 | } |
| 187 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 188 | static int |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 189 | ltq_mtd_remove(struct platform_device *pdev) |
| 190 | { |
| 191 | struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev); |
| 192 | |
| 193 | if (ltq_mtd) { |
| 194 | if (ltq_mtd->mtd) { |
John Crispin | 2e5db86 | 2011-06-09 19:57:33 +0200 | [diff] [blame] | 195 | mtd_device_unregister(ltq_mtd->mtd); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 196 | map_destroy(ltq_mtd->mtd); |
| 197 | } |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 198 | kfree(ltq_mtd->map); |
| 199 | kfree(ltq_mtd); |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 204 | static const struct of_device_id ltq_mtd_match[] = { |
| 205 | { .compatible = "lantiq,nor" }, |
| 206 | {}, |
| 207 | }; |
| 208 | MODULE_DEVICE_TABLE(of, ltq_mtd_match); |
| 209 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 210 | static struct platform_driver ltq_mtd_driver = { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 211 | .probe = ltq_mtd_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 212 | .remove = ltq_mtd_remove, |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 213 | .driver = { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 214 | .name = "ltq-nor", |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 215 | .owner = THIS_MODULE, |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 216 | .of_match_table = ltq_mtd_match, |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 217 | }, |
| 218 | }; |
| 219 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 220 | module_platform_driver(ltq_mtd_driver); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 221 | |
| 222 | MODULE_LICENSE("GPL"); |
| 223 | MODULE_AUTHOR("John Crispin <blogic@openwrt.org>"); |
| 224 | MODULE_DESCRIPTION("Lantiq SoC NOR"); |