blob: 93c507a6f86245024d6b1e79a54ca2f8ad8d78b1 [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>
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 Crispinfa09ede2012-05-05 15:41:42 +020023#include <linux/of.h>
John Crispin3c544732011-04-12 18:10:01 +020024
25#include <lantiq_soc.h>
John Crispin3c544732011-04-12 18:10:01 +020026
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
37enum {
38 LTQ_NOR_PROBING,
39 LTQ_NOR_NORMAL
40};
41
42struct ltq_mtd {
43 struct resource *res;
44 struct mtd_info *mtd;
45 struct map_info *map;
46};
47
John Crispin02fa9612012-04-13 09:36:46 +020048static const char ltq_map_name[] = "ltq_nor";
Artem Bityutskiy0984c892013-03-12 10:46:37 +020049static const char * const ltq_probe_types[] = { "cmdlinepart", "ofpart", NULL };
John Crispin3c544732011-04-12 18:10:01 +020050
51static map_word
52ltq_read16(struct map_info *map, unsigned long adr)
53{
54 unsigned long flags;
55 map_word temp;
56
57 if (map->map_priv_1 == LTQ_NOR_PROBING)
58 adr ^= 2;
59 spin_lock_irqsave(&ebu_lock, flags);
60 temp.x[0] = *(u16 *)(map->virt + adr);
61 spin_unlock_irqrestore(&ebu_lock, flags);
62 return temp;
63}
64
65static void
66ltq_write16(struct map_info *map, map_word d, unsigned long adr)
67{
68 unsigned long flags;
69
70 if (map->map_priv_1 == LTQ_NOR_PROBING)
71 adr ^= 2;
72 spin_lock_irqsave(&ebu_lock, flags);
73 *(u16 *)(map->virt + adr) = d.x[0];
74 spin_unlock_irqrestore(&ebu_lock, flags);
75}
76
77/*
78 * The following 2 functions copy data between iomem and a cached memory
79 * section. As memcpy() makes use of pre-fetching we cannot use it here.
80 * The normal alternative of using memcpy_{to,from}io also makes use of
81 * memcpy() on MIPS so it is not applicable either. We are therefore stuck
82 * with having to use our own loop.
83 */
84static void
85ltq_copy_from(struct map_info *map, void *to,
86 unsigned long from, ssize_t len)
87{
88 unsigned char *f = (unsigned char *)map->virt + from;
89 unsigned char *t = (unsigned char *)to;
90 unsigned long flags;
91
92 spin_lock_irqsave(&ebu_lock, flags);
93 while (len--)
94 *t++ = *f++;
95 spin_unlock_irqrestore(&ebu_lock, flags);
96}
97
98static void
99ltq_copy_to(struct map_info *map, unsigned long to,
100 const void *from, ssize_t len)
101{
102 unsigned char *f = (unsigned char *)from;
103 unsigned char *t = (unsigned char *)map->virt + to;
104 unsigned long flags;
105
106 spin_lock_irqsave(&ebu_lock, flags);
107 while (len--)
108 *t++ = *f++;
109 spin_unlock_irqrestore(&ebu_lock, flags);
110}
111
Bill Pemberton06f25512012-11-19 13:23:07 -0500112static int
John Crispin3c544732011-04-12 18:10:01 +0200113ltq_mtd_probe(struct platform_device *pdev)
114{
John Crispin02fa9612012-04-13 09:36:46 +0200115 struct mtd_part_parser_data ppdata;
John Crispin3c544732011-04-12 18:10:01 +0200116 struct ltq_mtd *ltq_mtd;
John Crispin3c544732011-04-12 18:10:01 +0200117 struct cfi_private *cfi;
118 int err;
119
John Crispinfa09ede2012-05-05 15:41:42 +0200120 if (of_machine_is_compatible("lantiq,falcon") &&
121 (ltq_boot_select() != BS_FLASH)) {
122 dev_err(&pdev->dev, "invalid bootstrap options\n");
123 return -ENODEV;
124 }
125
Jingoo Han436bf632013-12-26 10:42:34 +0900126 ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
127 if (!ltq_mtd)
128 return -ENOMEM;
129
John Crispin3c544732011-04-12 18:10:01 +0200130 platform_set_drvdata(pdev, ltq_mtd);
131
132 ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 if (!ltq_mtd->res) {
John Crispin02fa9612012-04-13 09:36:46 +0200134 dev_err(&pdev->dev, "failed to get memory resource\n");
Jingoo Han436bf632013-12-26 10:42:34 +0900135 return -ENOENT;
John Crispin3c544732011-04-12 18:10:01 +0200136 }
137
Jingoo Han436bf632013-12-26 10:42:34 +0900138 ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
139 GFP_KERNEL);
140 if (!ltq_mtd->map)
141 return -ENOMEM;
142
John Crispin02fa9612012-04-13 09:36:46 +0200143 ltq_mtd->map->phys = ltq_mtd->res->start;
144 ltq_mtd->map->size = resource_size(ltq_mtd->res);
Thierry Redingb0de7742013-01-21 11:09:12 +0100145 ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
Jingoo Han436bf632013-12-26 10:42:34 +0900146 if (IS_ERR(ltq_mtd->map->virt))
147 return PTR_ERR(ltq_mtd->map->virt);
John Crispin3c544732011-04-12 18:10:01 +0200148
John Crispin3c544732011-04-12 18:10:01 +0200149 ltq_mtd->map->name = ltq_map_name;
150 ltq_mtd->map->bankwidth = 2;
151 ltq_mtd->map->read = ltq_read16;
152 ltq_mtd->map->write = ltq_write16;
153 ltq_mtd->map->copy_from = ltq_copy_from;
154 ltq_mtd->map->copy_to = ltq_copy_to;
155
156 ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
157 ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
158 ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
159
160 if (!ltq_mtd->mtd) {
161 dev_err(&pdev->dev, "probing failed\n");
Jingoo Han436bf632013-12-26 10:42:34 +0900162 return -ENXIO;
John Crispin3c544732011-04-12 18:10:01 +0200163 }
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
John Crispin02fa9612012-04-13 09:36:46 +0200171 ppdata.of_node = pdev->dev.of_node;
172 err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
173 &ppdata, NULL, 0);
John Crispin3c544732011-04-12 18:10:01 +0200174 if (err) {
175 dev_err(&pdev->dev, "failed to add partitions\n");
176 goto err_destroy;
177 }
178
179 return 0;
180
181err_destroy:
182 map_destroy(ltq_mtd->mtd);
John Crispin3c544732011-04-12 18:10:01 +0200183 return err;
184}
185
Bill Pemberton810b7e02012-11-19 13:26:04 -0500186static int
John Crispin3c544732011-04-12 18:10:01 +0200187ltq_mtd_remove(struct platform_device *pdev)
188{
189 struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
190
Jingoo Han436bf632013-12-26 10:42:34 +0900191 if (ltq_mtd && ltq_mtd->mtd) {
192 mtd_device_unregister(ltq_mtd->mtd);
193 map_destroy(ltq_mtd->mtd);
John Crispin3c544732011-04-12 18:10:01 +0200194 }
195 return 0;
196}
197
John Crispin02fa9612012-04-13 09:36:46 +0200198static const struct of_device_id ltq_mtd_match[] = {
199 { .compatible = "lantiq,nor" },
200 {},
201};
202MODULE_DEVICE_TABLE(of, ltq_mtd_match);
203
John Crispin3c544732011-04-12 18:10:01 +0200204static struct platform_driver ltq_mtd_driver = {
John Crispin02fa9612012-04-13 09:36:46 +0200205 .probe = ltq_mtd_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500206 .remove = ltq_mtd_remove,
John Crispin3c544732011-04-12 18:10:01 +0200207 .driver = {
John Crispin02fa9612012-04-13 09:36:46 +0200208 .name = "ltq-nor",
John Crispin3c544732011-04-12 18:10:01 +0200209 .owner = THIS_MODULE,
John Crispin02fa9612012-04-13 09:36:46 +0200210 .of_match_table = ltq_mtd_match,
John Crispin3c544732011-04-12 18:10:01 +0200211 },
212};
213
John Crispin02fa9612012-04-13 09:36:46 +0200214module_platform_driver(ltq_mtd_driver);
John Crispin3c544732011-04-12 18:10:01 +0200215
216MODULE_LICENSE("GPL");
217MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
218MODULE_DESCRIPTION("Lantiq SoC NOR");