Blame view

drivers/mtd/maps/lantiq-flash.c 5.35 KB
3c5447390   John Crispin   MIPS: Lantiq: Add...
1
2
3
4
5
6
  /*
   *  This program is free software; you can redistribute it and/or modify it
   *  under the terms of the GNU General Public License version 2 as published
   *  by the Free Software Foundation.
   *
   *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
40bc941d6   John Crispin   mtd: update my em...
7
   *  Copyright (C) 2010 John Crispin <john@phrozen.org>
3c5447390   John Crispin   MIPS: Lantiq: Add...
8
   */
b0de774c6   Thierry Reding   mtd: Convert to d...
9
  #include <linux/err.h>
3c5447390   John Crispin   MIPS: Lantiq: Add...
10
11
12
13
14
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <linux/io.h>
  #include <linux/slab.h>
3c5447390   John Crispin   MIPS: Lantiq: Add...
15
16
17
18
19
20
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/map.h>
  #include <linux/mtd/partitions.h>
  #include <linux/mtd/cfi.h>
  #include <linux/platform_device.h>
  #include <linux/mtd/physmap.h>
fa09eded0   John Crispin   MTD: MIPS: lantiq...
21
  #include <linux/of.h>
3c5447390   John Crispin   MIPS: Lantiq: Add...
22
23
  
  #include <lantiq_soc.h>
3c5447390   John Crispin   MIPS: Lantiq: Add...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  
  /*
   * The NOR flash is connected to the same external bus unit (EBU) as PCI.
   * To make PCI work we need to enable the endianness swapping for the address
   * written to the EBU. This endianness swapping works for PCI correctly but
   * fails for attached NOR devices. To workaround this we need to use a complex
   * map. The workaround involves swapping all addresses whilst probing the chip.
   * Once probing is complete we stop swapping the addresses but swizzle the
   * unlock addresses to ensure that access to the NOR device works correctly.
   */
  
  enum {
  	LTQ_NOR_PROBING,
  	LTQ_NOR_NORMAL
  };
  
  struct ltq_mtd {
  	struct resource *res;
  	struct mtd_info *mtd;
  	struct map_info *map;
  };
02fa961fb   John Crispin   MTD: MIPS: lantiq...
45
  static const char ltq_map_name[] = "ltq_nor";
3c5447390   John Crispin   MIPS: Lantiq: Add...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  
  static map_word
  ltq_read16(struct map_info *map, unsigned long adr)
  {
  	unsigned long flags;
  	map_word temp;
  
  	if (map->map_priv_1 == LTQ_NOR_PROBING)
  		adr ^= 2;
  	spin_lock_irqsave(&ebu_lock, flags);
  	temp.x[0] = *(u16 *)(map->virt + adr);
  	spin_unlock_irqrestore(&ebu_lock, flags);
  	return temp;
  }
  
  static void
  ltq_write16(struct map_info *map, map_word d, unsigned long adr)
  {
  	unsigned long flags;
  
  	if (map->map_priv_1 == LTQ_NOR_PROBING)
  		adr ^= 2;
  	spin_lock_irqsave(&ebu_lock, flags);
  	*(u16 *)(map->virt + adr) = d.x[0];
  	spin_unlock_irqrestore(&ebu_lock, flags);
  }
  
  /*
   * The following 2 functions copy data between iomem and a cached memory
   * section. As memcpy() makes use of pre-fetching we cannot use it here.
   * The normal alternative of using memcpy_{to,from}io also makes use of
   * memcpy() on MIPS so it is not applicable either. We are therefore stuck
   * with having to use our own loop.
   */
  static void
  ltq_copy_from(struct map_info *map, void *to,
  	unsigned long from, ssize_t len)
  {
  	unsigned char *f = (unsigned char *)map->virt + from;
  	unsigned char *t = (unsigned char *)to;
  	unsigned long flags;
  
  	spin_lock_irqsave(&ebu_lock, flags);
  	while (len--)
  		*t++ = *f++;
  	spin_unlock_irqrestore(&ebu_lock, flags);
  }
  
  static void
  ltq_copy_to(struct map_info *map, unsigned long to,
  	const void *from, ssize_t len)
  {
  	unsigned char *f = (unsigned char *)from;
  	unsigned char *t = (unsigned char *)map->virt + to;
  	unsigned long flags;
  
  	spin_lock_irqsave(&ebu_lock, flags);
  	while (len--)
  		*t++ = *f++;
  	spin_unlock_irqrestore(&ebu_lock, flags);
  }
06f255106   Bill Pemberton   mtd: remove use o...
107
  static int
3c5447390   John Crispin   MIPS: Lantiq: Add...
108
109
  ltq_mtd_probe(struct platform_device *pdev)
  {
3c5447390   John Crispin   MIPS: Lantiq: Add...
110
  	struct ltq_mtd *ltq_mtd;
3c5447390   John Crispin   MIPS: Lantiq: Add...
111
112
  	struct cfi_private *cfi;
  	int err;
fa09eded0   John Crispin   MTD: MIPS: lantiq...
113
114
115
116
117
118
  	if (of_machine_is_compatible("lantiq,falcon") &&
  			(ltq_boot_select() != BS_FLASH)) {
  		dev_err(&pdev->dev, "invalid bootstrap options
  ");
  		return -ENODEV;
  	}
436bf63ba   Jingoo Han   mtd: lantiq-flash...
119
120
121
  	ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
  	if (!ltq_mtd)
  		return -ENOMEM;
3c5447390   John Crispin   MIPS: Lantiq: Add...
122
123
124
125
  	platform_set_drvdata(pdev, ltq_mtd);
  
  	ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!ltq_mtd->res) {
02fa961fb   John Crispin   MTD: MIPS: lantiq...
126
127
  		dev_err(&pdev->dev, "failed to get memory resource
  ");
436bf63ba   Jingoo Han   mtd: lantiq-flash...
128
  		return -ENOENT;
3c5447390   John Crispin   MIPS: Lantiq: Add...
129
  	}
436bf63ba   Jingoo Han   mtd: lantiq-flash...
130
131
132
133
  	ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
  				    GFP_KERNEL);
  	if (!ltq_mtd->map)
  		return -ENOMEM;
02fa961fb   John Crispin   MTD: MIPS: lantiq...
134
135
  	ltq_mtd->map->phys = ltq_mtd->res->start;
  	ltq_mtd->map->size = resource_size(ltq_mtd->res);
b0de774c6   Thierry Reding   mtd: Convert to d...
136
  	ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
436bf63ba   Jingoo Han   mtd: lantiq-flash...
137
138
  	if (IS_ERR(ltq_mtd->map->virt))
  		return PTR_ERR(ltq_mtd->map->virt);
3c5447390   John Crispin   MIPS: Lantiq: Add...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  
  	ltq_mtd->map->name = ltq_map_name;
  	ltq_mtd->map->bankwidth = 2;
  	ltq_mtd->map->read = ltq_read16;
  	ltq_mtd->map->write = ltq_write16;
  	ltq_mtd->map->copy_from = ltq_copy_from;
  	ltq_mtd->map->copy_to = ltq_copy_to;
  
  	ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
  	ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
  	ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
  
  	if (!ltq_mtd->mtd) {
  		dev_err(&pdev->dev, "probing failed
  ");
436bf63ba   Jingoo Han   mtd: lantiq-flash...
154
  		return -ENXIO;
3c5447390   John Crispin   MIPS: Lantiq: Add...
155
  	}
c54c2fb78   Frans Klaver   mtd: maps: lantiq...
156
  	ltq_mtd->mtd->dev.parent = &pdev->dev;
004b5e603   Brian Norris   mtd: drop unneces...
157
  	mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
3c5447390   John Crispin   MIPS: Lantiq: Add...
158
159
160
161
  
  	cfi = ltq_mtd->map->fldrv_priv;
  	cfi->addr_unlock1 ^= 1;
  	cfi->addr_unlock2 ^= 1;
004b5e603   Brian Norris   mtd: drop unneces...
162
  	err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
3c5447390   John Crispin   MIPS: Lantiq: Add...
163
164
165
166
167
168
169
170
171
172
  	if (err) {
  		dev_err(&pdev->dev, "failed to add partitions
  ");
  		goto err_destroy;
  	}
  
  	return 0;
  
  err_destroy:
  	map_destroy(ltq_mtd->mtd);
3c5447390   John Crispin   MIPS: Lantiq: Add...
173
174
  	return err;
  }
810b7e060   Bill Pemberton   mtd: remove use o...
175
  static int
3c5447390   John Crispin   MIPS: Lantiq: Add...
176
177
178
  ltq_mtd_remove(struct platform_device *pdev)
  {
  	struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
436bf63ba   Jingoo Han   mtd: lantiq-flash...
179
180
181
  	if (ltq_mtd && ltq_mtd->mtd) {
  		mtd_device_unregister(ltq_mtd->mtd);
  		map_destroy(ltq_mtd->mtd);
3c5447390   John Crispin   MIPS: Lantiq: Add...
182
183
184
  	}
  	return 0;
  }
02fa961fb   John Crispin   MTD: MIPS: lantiq...
185
186
187
188
189
  static const struct of_device_id ltq_mtd_match[] = {
  	{ .compatible = "lantiq,nor" },
  	{},
  };
  MODULE_DEVICE_TABLE(of, ltq_mtd_match);
3c5447390   John Crispin   MIPS: Lantiq: Add...
190
  static struct platform_driver ltq_mtd_driver = {
02fa961fb   John Crispin   MTD: MIPS: lantiq...
191
  	.probe = ltq_mtd_probe,
5153b88ca   Bill Pemberton   mtd: remove use o...
192
  	.remove = ltq_mtd_remove,
3c5447390   John Crispin   MIPS: Lantiq: Add...
193
  	.driver = {
02fa961fb   John Crispin   MTD: MIPS: lantiq...
194
  		.name = "ltq-nor",
02fa961fb   John Crispin   MTD: MIPS: lantiq...
195
  		.of_match_table = ltq_mtd_match,
3c5447390   John Crispin   MIPS: Lantiq: Add...
196
197
  	},
  };
02fa961fb   John Crispin   MTD: MIPS: lantiq...
198
  module_platform_driver(ltq_mtd_driver);
3c5447390   John Crispin   MIPS: Lantiq: Add...
199
200
  
  MODULE_LICENSE("GPL");
40bc941d6   John Crispin   mtd: update my em...
201
  MODULE_AUTHOR("John Crispin <john@phrozen.org>");
3c5447390   John Crispin   MIPS: Lantiq: Add...
202
  MODULE_DESCRIPTION("Lantiq SoC NOR");