Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lantiq-flash.c
Go to the documentation of this file.
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 <[email protected]>
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 #include <linux/of.h>
23 
24 #include <lantiq_soc.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 {
39 };
40 
41 struct ltq_mtd {
42  struct resource *res;
43  struct mtd_info *mtd;
44  struct map_info *map;
45 };
46 
47 static const char ltq_map_name[] = "ltq_nor";
48 static const char *ltq_probe_types[] __devinitconst = {
49  "cmdlinepart", "ofpart", NULL };
50 
51 static map_word
52 ltq_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 
65 static void
66 ltq_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  */
84 static void
85 ltq_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 
98 static void
99 ltq_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 
112 static int __devinit
113 ltq_mtd_probe(struct platform_device *pdev)
114 {
116  struct ltq_mtd *ltq_mtd;
117  struct cfi_private *cfi;
118  int err;
119 
120  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 
126  ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
127  platform_set_drvdata(pdev, ltq_mtd);
128 
129  ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130  if (!ltq_mtd->res) {
131  dev_err(&pdev->dev, "failed to get memory resource\n");
132  err = -ENOENT;
133  goto err_out;
134  }
135 
136  ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
137  ltq_mtd->map->phys = ltq_mtd->res->start;
138  ltq_mtd->map->size = resource_size(ltq_mtd->res);
139  ltq_mtd->map->virt = devm_request_and_ioremap(&pdev->dev, ltq_mtd->res);
140  if (!ltq_mtd->map->virt) {
141  dev_err(&pdev->dev, "failed to remap mem resource\n");
142  err = -EBUSY;
143  goto err_out;
144  }
145 
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;
160  goto err_free;
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 
169  ppdata.of_node = pdev->dev.of_node;
170  err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
171  &ppdata, NULL, 0);
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);
181 err_free:
182  kfree(ltq_mtd->map);
183 err_out:
184  kfree(ltq_mtd);
185  return err;
186 }
187 
188 static int __devexit
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) {
195  mtd_device_unregister(ltq_mtd->mtd);
196  map_destroy(ltq_mtd->mtd);
197  }
198  kfree(ltq_mtd->map);
199  kfree(ltq_mtd);
200  }
201  return 0;
202 }
203 
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 
210 static struct platform_driver ltq_mtd_driver = {
211  .probe = ltq_mtd_probe,
212  .remove = __devexit_p(ltq_mtd_remove),
213  .driver = {
214  .name = "ltq-nor",
215  .owner = THIS_MODULE,
216  .of_match_table = ltq_mtd_match,
217  },
218 };
219 
220 module_platform_driver(ltq_mtd_driver);
221 
222 MODULE_LICENSE("GPL");
223 MODULE_AUTHOR("John Crispin <[email protected]>");
224 MODULE_DESCRIPTION("Lantiq SoC NOR");