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> |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 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> |
John Crispin | fa09ede | 2012-05-05 15:41:42 +0200 | [diff] [blame] | 22 | #include <linux/of.h> |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 23 | |
| 24 | #include <lantiq_soc.h> |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 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 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 47 | static const char ltq_map_name[] = "ltq_nor"; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 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 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 110 | static int |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 111 | ltq_mtd_probe(struct platform_device *pdev) |
| 112 | { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 113 | struct mtd_part_parser_data ppdata; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 114 | struct ltq_mtd *ltq_mtd; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 115 | struct cfi_private *cfi; |
| 116 | int err; |
| 117 | |
John Crispin | fa09ede | 2012-05-05 15:41:42 +0200 | [diff] [blame] | 118 | if (of_machine_is_compatible("lantiq,falcon") && |
| 119 | (ltq_boot_select() != BS_FLASH)) { |
| 120 | dev_err(&pdev->dev, "invalid bootstrap options\n"); |
| 121 | return -ENODEV; |
| 122 | } |
| 123 | |
Jingoo Han | 436bf63 | 2013-12-26 10:42:34 +0900 | [diff] [blame] | 124 | ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL); |
| 125 | if (!ltq_mtd) |
| 126 | return -ENOMEM; |
| 127 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 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"); |
Jingoo Han | 436bf63 | 2013-12-26 10:42:34 +0900 | [diff] [blame] | 133 | return -ENOENT; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Jingoo Han | 436bf63 | 2013-12-26 10:42:34 +0900 | [diff] [blame] | 136 | ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info), |
| 137 | GFP_KERNEL); |
| 138 | if (!ltq_mtd->map) |
| 139 | return -ENOMEM; |
| 140 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 141 | ltq_mtd->map->phys = ltq_mtd->res->start; |
| 142 | ltq_mtd->map->size = resource_size(ltq_mtd->res); |
Thierry Reding | b0de774 | 2013-01-21 11:09:12 +0100 | [diff] [blame] | 143 | ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res); |
Jingoo Han | 436bf63 | 2013-12-26 10:42:34 +0900 | [diff] [blame] | 144 | if (IS_ERR(ltq_mtd->map->virt)) |
| 145 | return PTR_ERR(ltq_mtd->map->virt); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 146 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 147 | ltq_mtd->map->name = ltq_map_name; |
| 148 | ltq_mtd->map->bankwidth = 2; |
| 149 | ltq_mtd->map->read = ltq_read16; |
| 150 | ltq_mtd->map->write = ltq_write16; |
| 151 | ltq_mtd->map->copy_from = ltq_copy_from; |
| 152 | ltq_mtd->map->copy_to = ltq_copy_to; |
| 153 | |
| 154 | ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING; |
| 155 | ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map); |
| 156 | ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL; |
| 157 | |
| 158 | if (!ltq_mtd->mtd) { |
| 159 | dev_err(&pdev->dev, "probing failed\n"); |
Jingoo Han | 436bf63 | 2013-12-26 10:42:34 +0900 | [diff] [blame] | 160 | return -ENXIO; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Frans Klaver | c54c2fb | 2015-06-10 22:38:28 +0200 | [diff] [blame] | 163 | ltq_mtd->mtd->dev.parent = &pdev->dev; |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 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; |
Brian Norris | c3c263a | 2015-05-18 16:17:11 -0700 | [diff] [blame] | 170 | err = mtd_device_parse_register(ltq_mtd->mtd, NULL, &ppdata, NULL, 0); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 171 | if (err) { |
| 172 | dev_err(&pdev->dev, "failed to add partitions\n"); |
| 173 | goto err_destroy; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | |
| 178 | err_destroy: |
| 179 | map_destroy(ltq_mtd->mtd); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 180 | return err; |
| 181 | } |
| 182 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 183 | static int |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 184 | ltq_mtd_remove(struct platform_device *pdev) |
| 185 | { |
| 186 | struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev); |
| 187 | |
Jingoo Han | 436bf63 | 2013-12-26 10:42:34 +0900 | [diff] [blame] | 188 | if (ltq_mtd && ltq_mtd->mtd) { |
| 189 | mtd_device_unregister(ltq_mtd->mtd); |
| 190 | map_destroy(ltq_mtd->mtd); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 195 | static const struct of_device_id ltq_mtd_match[] = { |
| 196 | { .compatible = "lantiq,nor" }, |
| 197 | {}, |
| 198 | }; |
| 199 | MODULE_DEVICE_TABLE(of, ltq_mtd_match); |
| 200 | |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 201 | static struct platform_driver ltq_mtd_driver = { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 202 | .probe = ltq_mtd_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 203 | .remove = ltq_mtd_remove, |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 204 | .driver = { |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 205 | .name = "ltq-nor", |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 206 | .of_match_table = ltq_mtd_match, |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 207 | }, |
| 208 | }; |
| 209 | |
John Crispin | 02fa961 | 2012-04-13 09:36:46 +0200 | [diff] [blame] | 210 | module_platform_driver(ltq_mtd_driver); |
John Crispin | 3c54473 | 2011-04-12 18:10:01 +0200 | [diff] [blame] | 211 | |
| 212 | MODULE_LICENSE("GPL"); |
| 213 | MODULE_AUTHOR("John Crispin <blogic@openwrt.org>"); |
| 214 | MODULE_DESCRIPTION("Lantiq SoC NOR"); |