blob: 7aa682cd4d7e74d28677a53c2fc5291452ee3ba4 [file] [log] [blame]
John Crispin3c544732011-04-12 18:10:01 +02001/*
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 Redingb0de7742013-01-21 11:09:12 +010010#include <linux/err.h>
John Crispin3c544732011-04-12 18:10:01 +020011#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 Crispin3c544732011-04-12 18:10:01 +020016#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 Crispinfa09ede2012-05-05 15:41:42 +020022#include <linux/of.h>
John Crispin3c544732011-04-12 18:10:01 +020023
24#include <lantiq_soc.h>
John Crispin3c544732011-04-12 18:10:01 +020025
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
36enum {
37 LTQ_NOR_PROBING,
38 LTQ_NOR_NORMAL
39};
40
41struct ltq_mtd {
42 struct resource *res;
43 struct mtd_info *mtd;
44 struct map_info *map;
45};
46
John Crispin02fa9612012-04-13 09:36:46 +020047static const char ltq_map_name[] = "ltq_nor";
Artem Bityutskiy0984c892013-03-12 10:46:37 +020048static const char * const ltq_probe_types[] = { "cmdlinepart", "ofpart", NULL };
John Crispin3c544732011-04-12 18:10:01 +020049
50static map_word
51ltq_read16(struct map_info *map, unsigned long adr)
52{
53 unsigned long flags;
54 map_word temp;
55
56 if (map->map_priv_1 == LTQ_NOR_PROBING)
57 adr ^= 2;
58 spin_lock_irqsave(&ebu_lock, flags);
59 temp.x[0] = *(u16 *)(map->virt + adr);
60 spin_unlock_irqrestore(&ebu_lock, flags);
61 return temp;
62}
63
64static void
65ltq_write16(struct map_info *map, map_word d, unsigned long adr)
66{
67 unsigned long flags;
68
69 if (map->map_priv_1 == LTQ_NOR_PROBING)
70 adr ^= 2;
71 spin_lock_irqsave(&ebu_lock, flags);
72 *(u16 *)(map->virt + adr) = d.x[0];
73 spin_unlock_irqrestore(&ebu_lock, flags);
74}
75
76/*
77 * The following 2 functions copy data between iomem and a cached memory
78 * section. As memcpy() makes use of pre-fetching we cannot use it here.
79 * The normal alternative of using memcpy_{to,from}io also makes use of
80 * memcpy() on MIPS so it is not applicable either. We are therefore stuck
81 * with having to use our own loop.
82 */
83static void
84ltq_copy_from(struct map_info *map, void *to,
85 unsigned long from, ssize_t len)
86{
87 unsigned char *f = (unsigned char *)map->virt + from;
88 unsigned char *t = (unsigned char *)to;
89 unsigned long flags;
90
91 spin_lock_irqsave(&ebu_lock, flags);
92 while (len--)
93 *t++ = *f++;
94 spin_unlock_irqrestore(&ebu_lock, flags);
95}
96
97static void
98ltq_copy_to(struct map_info *map, unsigned long to,
99 const void *from, ssize_t len)
100{
101 unsigned char *f = (unsigned char *)from;
102 unsigned char *t = (unsigned char *)map->virt + to;
103 unsigned long flags;
104
105 spin_lock_irqsave(&ebu_lock, flags);
106 while (len--)
107 *t++ = *f++;
108 spin_unlock_irqrestore(&ebu_lock, flags);
109}
110
Bill Pemberton06f25512012-11-19 13:23:07 -0500111static int
John Crispin3c544732011-04-12 18:10:01 +0200112ltq_mtd_probe(struct platform_device *pdev)
113{
John Crispin02fa9612012-04-13 09:36:46 +0200114 struct mtd_part_parser_data ppdata;
John Crispin3c544732011-04-12 18:10:01 +0200115 struct ltq_mtd *ltq_mtd;
John Crispin3c544732011-04-12 18:10:01 +0200116 struct cfi_private *cfi;
117 int err;
118
John Crispinfa09ede2012-05-05 15:41:42 +0200119 if (of_machine_is_compatible("lantiq,falcon") &&
120 (ltq_boot_select() != BS_FLASH)) {
121 dev_err(&pdev->dev, "invalid bootstrap options\n");
122 return -ENODEV;
123 }
124
Jingoo Han436bf632013-12-26 10:42:34 +0900125 ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
126 if (!ltq_mtd)
127 return -ENOMEM;
128
John Crispin3c544732011-04-12 18:10:01 +0200129 platform_set_drvdata(pdev, ltq_mtd);
130
131 ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 if (!ltq_mtd->res) {
John Crispin02fa9612012-04-13 09:36:46 +0200133 dev_err(&pdev->dev, "failed to get memory resource\n");
Jingoo Han436bf632013-12-26 10:42:34 +0900134 return -ENOENT;
John Crispin3c544732011-04-12 18:10:01 +0200135 }
136
Jingoo Han436bf632013-12-26 10:42:34 +0900137 ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
138 GFP_KERNEL);
139 if (!ltq_mtd->map)
140 return -ENOMEM;
141
John Crispin02fa9612012-04-13 09:36:46 +0200142 ltq_mtd->map->phys = ltq_mtd->res->start;
143 ltq_mtd->map->size = resource_size(ltq_mtd->res);
Thierry Redingb0de7742013-01-21 11:09:12 +0100144 ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
Jingoo Han436bf632013-12-26 10:42:34 +0900145 if (IS_ERR(ltq_mtd->map->virt))
146 return PTR_ERR(ltq_mtd->map->virt);
John Crispin3c544732011-04-12 18:10:01 +0200147
John Crispin3c544732011-04-12 18:10:01 +0200148 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");
Jingoo Han436bf632013-12-26 10:42:34 +0900161 return -ENXIO;
John Crispin3c544732011-04-12 18:10:01 +0200162 }
163
164 ltq_mtd->mtd->owner = THIS_MODULE;
165
166 cfi = ltq_mtd->map->fldrv_priv;
167 cfi->addr_unlock1 ^= 1;
168 cfi->addr_unlock2 ^= 1;
169
John Crispin02fa9612012-04-13 09:36:46 +0200170 ppdata.of_node = pdev->dev.of_node;
171 err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
172 &ppdata, NULL, 0);
John Crispin3c544732011-04-12 18:10:01 +0200173 if (err) {
174 dev_err(&pdev->dev, "failed to add partitions\n");
175 goto err_destroy;
176 }
177
178 return 0;
179
180err_destroy:
181 map_destroy(ltq_mtd->mtd);
John Crispin3c544732011-04-12 18:10:01 +0200182 return err;
183}
184
Bill Pemberton810b7e02012-11-19 13:26:04 -0500185static int
John Crispin3c544732011-04-12 18:10:01 +0200186ltq_mtd_remove(struct platform_device *pdev)
187{
188 struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
189
Jingoo Han436bf632013-12-26 10:42:34 +0900190 if (ltq_mtd && ltq_mtd->mtd) {
191 mtd_device_unregister(ltq_mtd->mtd);
192 map_destroy(ltq_mtd->mtd);
John Crispin3c544732011-04-12 18:10:01 +0200193 }
194 return 0;
195}
196
John Crispin02fa9612012-04-13 09:36:46 +0200197static const struct of_device_id ltq_mtd_match[] = {
198 { .compatible = "lantiq,nor" },
199 {},
200};
201MODULE_DEVICE_TABLE(of, ltq_mtd_match);
202
John Crispin3c544732011-04-12 18:10:01 +0200203static struct platform_driver ltq_mtd_driver = {
John Crispin02fa9612012-04-13 09:36:46 +0200204 .probe = ltq_mtd_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500205 .remove = ltq_mtd_remove,
John Crispin3c544732011-04-12 18:10:01 +0200206 .driver = {
John Crispin02fa9612012-04-13 09:36:46 +0200207 .name = "ltq-nor",
John Crispin3c544732011-04-12 18:10:01 +0200208 .owner = THIS_MODULE,
John Crispin02fa9612012-04-13 09:36:46 +0200209 .of_match_table = ltq_mtd_match,
John Crispin3c544732011-04-12 18:10:01 +0200210 },
211};
212
John Crispin02fa9612012-04-13 09:36:46 +0200213module_platform_driver(ltq_mtd_driver);
John Crispin3c544732011-04-12 18:10:01 +0200214
215MODULE_LICENSE("GPL");
216MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
217MODULE_DESCRIPTION("Lantiq SoC NOR");