xref: /linux/drivers/mtd/maps/pismo.c (revision ed5c2f5fd10dda07263f79f338a512c0f49f76f5)
184a14ae8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f6a8c609SRussell King - ARM Linux /*
3f6a8c609SRussell King - ARM Linux  * PISMO memory driver - http://www.pismoworld.org/
4f6a8c609SRussell King - ARM Linux  *
5f6a8c609SRussell King - ARM Linux  * For ARM Realview and Versatile platforms
6f6a8c609SRussell King - ARM Linux  */
7f6a8c609SRussell King - ARM Linux #include <linux/init.h>
8f6a8c609SRussell King - ARM Linux #include <linux/module.h>
9f6a8c609SRussell King - ARM Linux #include <linux/i2c.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
11f6a8c609SRussell King - ARM Linux #include <linux/platform_device.h>
12f6a8c609SRussell King - ARM Linux #include <linux/spinlock.h>
13f6a8c609SRussell King - ARM Linux #include <linux/mutex.h>
14f6a8c609SRussell King - ARM Linux #include <linux/mtd/physmap.h>
15f6a8c609SRussell King - ARM Linux #include <linux/mtd/plat-ram.h>
16f6a8c609SRussell King - ARM Linux #include <linux/mtd/pismo.h>
17f6a8c609SRussell King - ARM Linux 
18f6a8c609SRussell King - ARM Linux #define PISMO_NUM_CS	5
19f6a8c609SRussell King - ARM Linux 
20f6a8c609SRussell King - ARM Linux struct pismo_cs_block {
21f6a8c609SRussell King - ARM Linux 	u8	type;
22f6a8c609SRussell King - ARM Linux 	u8	width;
23f6a8c609SRussell King - ARM Linux 	__le16	access;
24f6a8c609SRussell King - ARM Linux 	__le32	size;
25f6a8c609SRussell King - ARM Linux 	u32	reserved[2];
26f6a8c609SRussell King - ARM Linux 	char	device[32];
27f6a8c609SRussell King - ARM Linux } __packed;
28f6a8c609SRussell King - ARM Linux 
29f6a8c609SRussell King - ARM Linux struct pismo_eeprom {
30f6a8c609SRussell King - ARM Linux 	struct pismo_cs_block cs[PISMO_NUM_CS];
31f6a8c609SRussell King - ARM Linux 	char	board[15];
32f6a8c609SRussell King - ARM Linux 	u8	sum;
33f6a8c609SRussell King - ARM Linux } __packed;
34f6a8c609SRussell King - ARM Linux 
35f6a8c609SRussell King - ARM Linux struct pismo_mem {
36f6a8c609SRussell King - ARM Linux 	phys_addr_t base;
37f6a8c609SRussell King - ARM Linux 	u32	size;
38f6a8c609SRussell King - ARM Linux 	u16	access;
39f6a8c609SRussell King - ARM Linux 	u8	width;
40f6a8c609SRussell King - ARM Linux 	u8	type;
41f6a8c609SRussell King - ARM Linux };
42f6a8c609SRussell King - ARM Linux 
43f6a8c609SRussell King - ARM Linux struct pismo_data {
44f6a8c609SRussell King - ARM Linux 	struct i2c_client	*client;
45f6a8c609SRussell King - ARM Linux 	void			(*vpp)(void *, int);
46f6a8c609SRussell King - ARM Linux 	void			*vpp_data;
47f6a8c609SRussell King - ARM Linux 	struct platform_device	*dev[PISMO_NUM_CS];
48f6a8c609SRussell King - ARM Linux };
49f6a8c609SRussell King - ARM Linux 
50667f390bSMarc Zyngier static void pismo_set_vpp(struct platform_device *pdev, int on)
51f6a8c609SRussell King - ARM Linux {
52667f390bSMarc Zyngier 	struct i2c_client *client = to_i2c_client(pdev->dev.parent);
53667f390bSMarc Zyngier 	struct pismo_data *pismo = i2c_get_clientdata(client);
54f6a8c609SRussell King - ARM Linux 
55f6a8c609SRussell King - ARM Linux 	pismo->vpp(pismo->vpp_data, on);
56f6a8c609SRussell King - ARM Linux }
57f6a8c609SRussell King - ARM Linux 
5806f25510SBill Pemberton static unsigned int pismo_width_to_bytes(unsigned int width)
59f6a8c609SRussell King - ARM Linux {
60f6a8c609SRussell King - ARM Linux 	width &= 15;
61f6a8c609SRussell King - ARM Linux 	if (width > 2)
62f6a8c609SRussell King - ARM Linux 		return 0;
63f6a8c609SRussell King - ARM Linux 	return 1 << width;
64f6a8c609SRussell King - ARM Linux }
65f6a8c609SRussell King - ARM Linux 
66d8929942SGreg Kroah-Hartman static int pismo_eeprom_read(struct i2c_client *client, void *buf, u8 addr,
67d8929942SGreg Kroah-Hartman 			     size_t size)
68f6a8c609SRussell King - ARM Linux {
69f6a8c609SRussell King - ARM Linux 	int ret;
70f6a8c609SRussell King - ARM Linux 	struct i2c_msg msg[] = {
71f6a8c609SRussell King - ARM Linux 		{
72f6a8c609SRussell King - ARM Linux 			.addr = client->addr,
73f6a8c609SRussell King - ARM Linux 			.len = sizeof(addr),
74f6a8c609SRussell King - ARM Linux 			.buf = &addr,
75f6a8c609SRussell King - ARM Linux 		}, {
76f6a8c609SRussell King - ARM Linux 			.addr = client->addr,
77f6a8c609SRussell King - ARM Linux 			.flags = I2C_M_RD,
78f6a8c609SRussell King - ARM Linux 			.len = size,
79f6a8c609SRussell King - ARM Linux 			.buf = buf,
80f6a8c609SRussell King - ARM Linux 		},
81f6a8c609SRussell King - ARM Linux 	};
82f6a8c609SRussell King - ARM Linux 
83f6a8c609SRussell King - ARM Linux 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
84f6a8c609SRussell King - ARM Linux 
85f6a8c609SRussell King - ARM Linux 	return ret == ARRAY_SIZE(msg) ? size : -EIO;
86f6a8c609SRussell King - ARM Linux }
87f6a8c609SRussell King - ARM Linux 
8806f25510SBill Pemberton static int pismo_add_device(struct pismo_data *pismo, int i,
89d8929942SGreg Kroah-Hartman 			    struct pismo_mem *region, const char *name,
90d8929942SGreg Kroah-Hartman 			    void *pdata, size_t psize)
91f6a8c609SRussell King - ARM Linux {
92f6a8c609SRussell King - ARM Linux 	struct platform_device *dev;
93f6a8c609SRussell King - ARM Linux 	struct resource res = { };
941d40ddd1SRussell King 	phys_addr_t base = region->base;
95f6a8c609SRussell King - ARM Linux 	int ret;
96f6a8c609SRussell King - ARM Linux 
97f6a8c609SRussell King - ARM Linux 	if (base == ~0)
98f6a8c609SRussell King - ARM Linux 		return -ENXIO;
99f6a8c609SRussell King - ARM Linux 
100f6a8c609SRussell King - ARM Linux 	res.start = base;
101f6a8c609SRussell King - ARM Linux 	res.end = base + region->size - 1;
102f6a8c609SRussell King - ARM Linux 	res.flags = IORESOURCE_MEM;
103f6a8c609SRussell King - ARM Linux 
104f6a8c609SRussell King - ARM Linux 	dev = platform_device_alloc(name, i);
105f6a8c609SRussell King - ARM Linux 	if (!dev)
106f6a8c609SRussell King - ARM Linux 		return -ENOMEM;
107f6a8c609SRussell King - ARM Linux 	dev->dev.parent = &pismo->client->dev;
108f6a8c609SRussell King - ARM Linux 
109f6a8c609SRussell King - ARM Linux 	do {
110f6a8c609SRussell King - ARM Linux 		ret = platform_device_add_resources(dev, &res, 1);
111f6a8c609SRussell King - ARM Linux 		if (ret)
112f6a8c609SRussell King - ARM Linux 			break;
113f6a8c609SRussell King - ARM Linux 
114f6a8c609SRussell King - ARM Linux 		ret = platform_device_add_data(dev, pdata, psize);
115f6a8c609SRussell King - ARM Linux 		if (ret)
116f6a8c609SRussell King - ARM Linux 			break;
117f6a8c609SRussell King - ARM Linux 
118f6a8c609SRussell King - ARM Linux 		ret = platform_device_add(dev);
119f6a8c609SRussell King - ARM Linux 		if (ret)
120f6a8c609SRussell King - ARM Linux 			break;
121f6a8c609SRussell King - ARM Linux 
122f6a8c609SRussell King - ARM Linux 		pismo->dev[i] = dev;
123f6a8c609SRussell King - ARM Linux 		return 0;
124f6a8c609SRussell King - ARM Linux 	} while (0);
125f6a8c609SRussell King - ARM Linux 
126f6a8c609SRussell King - ARM Linux 	platform_device_put(dev);
127f6a8c609SRussell King - ARM Linux 	return ret;
128f6a8c609SRussell King - ARM Linux }
129f6a8c609SRussell King - ARM Linux 
13006f25510SBill Pemberton static int pismo_add_nor(struct pismo_data *pismo, int i,
131f6a8c609SRussell King - ARM Linux 			 struct pismo_mem *region)
132f6a8c609SRussell King - ARM Linux {
133f6a8c609SRussell King - ARM Linux 	struct physmap_flash_data data = {
134f6a8c609SRussell King - ARM Linux 		.width = region->width,
135f6a8c609SRussell King - ARM Linux 	};
136f6a8c609SRussell King - ARM Linux 
137f6a8c609SRussell King - ARM Linux 	if (pismo->vpp)
138f6a8c609SRussell King - ARM Linux 		data.set_vpp = pismo_set_vpp;
139f6a8c609SRussell King - ARM Linux 
140f6a8c609SRussell King - ARM Linux 	return pismo_add_device(pismo, i, region, "physmap-flash",
141f6a8c609SRussell King - ARM Linux 		&data, sizeof(data));
142f6a8c609SRussell King - ARM Linux }
143f6a8c609SRussell King - ARM Linux 
14406f25510SBill Pemberton static int pismo_add_sram(struct pismo_data *pismo, int i,
145f6a8c609SRussell King - ARM Linux 			  struct pismo_mem *region)
146f6a8c609SRussell King - ARM Linux {
147f6a8c609SRussell King - ARM Linux 	struct platdata_mtd_ram data = {
148f6a8c609SRussell King - ARM Linux 		.bankwidth = region->width,
149f6a8c609SRussell King - ARM Linux 	};
150f6a8c609SRussell King - ARM Linux 
151f6a8c609SRussell King - ARM Linux 	return pismo_add_device(pismo, i, region, "mtd-ram",
152f6a8c609SRussell King - ARM Linux 		&data, sizeof(data));
153f6a8c609SRussell King - ARM Linux }
154f6a8c609SRussell King - ARM Linux 
15506f25510SBill Pemberton static void pismo_add_one(struct pismo_data *pismo, int i,
156f6a8c609SRussell King - ARM Linux 			  const struct pismo_cs_block *cs, phys_addr_t base)
157f6a8c609SRussell King - ARM Linux {
158f6a8c609SRussell King - ARM Linux 	struct device *dev = &pismo->client->dev;
159f6a8c609SRussell King - ARM Linux 	struct pismo_mem region;
160f6a8c609SRussell King - ARM Linux 
161f6a8c609SRussell King - ARM Linux 	region.base = base;
162f6a8c609SRussell King - ARM Linux 	region.type = cs->type;
163f6a8c609SRussell King - ARM Linux 	region.width = pismo_width_to_bytes(cs->width);
164f6a8c609SRussell King - ARM Linux 	region.access = le16_to_cpu(cs->access);
165f6a8c609SRussell King - ARM Linux 	region.size = le32_to_cpu(cs->size);
166f6a8c609SRussell King - ARM Linux 
167f6a8c609SRussell King - ARM Linux 	if (region.width == 0) {
168f6a8c609SRussell King - ARM Linux 		dev_err(dev, "cs%u: bad width: %02x, ignoring\n", i, cs->width);
169f6a8c609SRussell King - ARM Linux 		return;
170f6a8c609SRussell King - ARM Linux 	}
171f6a8c609SRussell King - ARM Linux 
172f6a8c609SRussell King - ARM Linux 	/*
173f6a8c609SRussell King - ARM Linux 	 * FIXME: may need to the platforms memory controller here, but at
174f6a8c609SRussell King - ARM Linux 	 * the moment we assume that it has already been correctly setup.
175f6a8c609SRussell King - ARM Linux 	 * The memory controller can also tell us the base address as well.
176f6a8c609SRussell King - ARM Linux 	 */
177f6a8c609SRussell King - ARM Linux 
178f6a8c609SRussell King - ARM Linux 	dev_info(dev, "cs%u: %.32s: type %02x access %u00ps size %uK\n",
179f6a8c609SRussell King - ARM Linux 		i, cs->device, region.type, region.access, region.size / 1024);
180f6a8c609SRussell King - ARM Linux 
181f6a8c609SRussell King - ARM Linux 	switch (region.type) {
182f6a8c609SRussell King - ARM Linux 	case 0:
183f6a8c609SRussell King - ARM Linux 		break;
184f6a8c609SRussell King - ARM Linux 	case 1:
185f6a8c609SRussell King - ARM Linux 		/* static DOC */
186f6a8c609SRussell King - ARM Linux 		break;
187f6a8c609SRussell King - ARM Linux 	case 2:
188f6a8c609SRussell King - ARM Linux 		/* static NOR */
189f6a8c609SRussell King - ARM Linux 		pismo_add_nor(pismo, i, &region);
190f6a8c609SRussell King - ARM Linux 		break;
191f6a8c609SRussell King - ARM Linux 	case 3:
192f6a8c609SRussell King - ARM Linux 		/* static RAM */
193f6a8c609SRussell King - ARM Linux 		pismo_add_sram(pismo, i, &region);
194f6a8c609SRussell King - ARM Linux 		break;
195f6a8c609SRussell King - ARM Linux 	}
196f6a8c609SRussell King - ARM Linux }
197f6a8c609SRussell King - ARM Linux 
198*ed5c2f5fSUwe Kleine-König static void pismo_remove(struct i2c_client *client)
199f6a8c609SRussell King - ARM Linux {
200f6a8c609SRussell King - ARM Linux 	struct pismo_data *pismo = i2c_get_clientdata(client);
201f6a8c609SRussell King - ARM Linux 	int i;
202f6a8c609SRussell King - ARM Linux 
203f6a8c609SRussell King - ARM Linux 	for (i = 0; i < ARRAY_SIZE(pismo->dev); i++)
204f6a8c609SRussell King - ARM Linux 		platform_device_unregister(pismo->dev[i]);
205f6a8c609SRussell King - ARM Linux 
206f6a8c609SRussell King - ARM Linux 	kfree(pismo);
207f6a8c609SRussell King - ARM Linux }
208f6a8c609SRussell King - ARM Linux 
20906f25510SBill Pemberton static int pismo_probe(struct i2c_client *client,
210f6a8c609SRussell King - ARM Linux 		       const struct i2c_device_id *id)
211f6a8c609SRussell King - ARM Linux {
212f6a8c609SRussell King - ARM Linux 	struct pismo_pdata *pdata = client->dev.platform_data;
213f6a8c609SRussell King - ARM Linux 	struct pismo_eeprom eeprom;
214f6a8c609SRussell King - ARM Linux 	struct pismo_data *pismo;
215f6a8c609SRussell King - ARM Linux 	int ret, i;
216f6a8c609SRussell King - ARM Linux 
21775203121SWolfram Sang 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
218f6a8c609SRussell King - ARM Linux 		dev_err(&client->dev, "functionality mismatch\n");
219f6a8c609SRussell King - ARM Linux 		return -EIO;
220f6a8c609SRussell King - ARM Linux 	}
221f6a8c609SRussell King - ARM Linux 
222f6a8c609SRussell King - ARM Linux 	pismo = kzalloc(sizeof(*pismo), GFP_KERNEL);
223f6a8c609SRussell King - ARM Linux 	if (!pismo)
224f6a8c609SRussell King - ARM Linux 		return -ENOMEM;
225f6a8c609SRussell King - ARM Linux 
226f6a8c609SRussell King - ARM Linux 	pismo->client = client;
227f6a8c609SRussell King - ARM Linux 	if (pdata) {
228f6a8c609SRussell King - ARM Linux 		pismo->vpp = pdata->set_vpp;
229f6a8c609SRussell King - ARM Linux 		pismo->vpp_data = pdata->vpp_data;
230f6a8c609SRussell King - ARM Linux 	}
231f6a8c609SRussell King - ARM Linux 	i2c_set_clientdata(client, pismo);
232f6a8c609SRussell King - ARM Linux 
233f6a8c609SRussell King - ARM Linux 	ret = pismo_eeprom_read(client, &eeprom, 0, sizeof(eeprom));
234f6a8c609SRussell King - ARM Linux 	if (ret < 0) {
235f6a8c609SRussell King - ARM Linux 		dev_err(&client->dev, "error reading EEPROM: %d\n", ret);
236395b2288SWolfram Sang 		goto exit_free;
237f6a8c609SRussell King - ARM Linux 	}
238f6a8c609SRussell King - ARM Linux 
239f6a8c609SRussell King - ARM Linux 	dev_info(&client->dev, "%.15s board found\n", eeprom.board);
240f6a8c609SRussell King - ARM Linux 
241f6a8c609SRussell King - ARM Linux 	for (i = 0; i < ARRAY_SIZE(eeprom.cs); i++)
242f6a8c609SRussell King - ARM Linux 		if (eeprom.cs[i].type != 0xff)
243f6a8c609SRussell King - ARM Linux 			pismo_add_one(pismo, i, &eeprom.cs[i],
244f6a8c609SRussell King - ARM Linux 				      pdata->cs_addrs[i]);
245f6a8c609SRussell King - ARM Linux 
246f6a8c609SRussell King - ARM Linux 	return 0;
247395b2288SWolfram Sang 
248395b2288SWolfram Sang  exit_free:
249395b2288SWolfram Sang 	kfree(pismo);
250395b2288SWolfram Sang 	return ret;
251f6a8c609SRussell King - ARM Linux }
252f6a8c609SRussell King - ARM Linux 
253f6a8c609SRussell King - ARM Linux static const struct i2c_device_id pismo_id[] = {
254f6a8c609SRussell King - ARM Linux 	{ "pismo" },
255f6a8c609SRussell King - ARM Linux 	{ },
256f6a8c609SRussell King - ARM Linux };
257f6a8c609SRussell King - ARM Linux MODULE_DEVICE_TABLE(i2c, pismo_id);
258f6a8c609SRussell King - ARM Linux 
259f6a8c609SRussell King - ARM Linux static struct i2c_driver pismo_driver = {
260f6a8c609SRussell King - ARM Linux 	.driver	= {
261f6a8c609SRussell King - ARM Linux 		.name	= "pismo",
262f6a8c609SRussell King - ARM Linux 	},
263f6a8c609SRussell King - ARM Linux 	.probe		= pismo_probe,
2645153b88cSBill Pemberton 	.remove		= pismo_remove,
265f6a8c609SRussell King - ARM Linux 	.id_table	= pismo_id,
266f6a8c609SRussell King - ARM Linux };
267f6a8c609SRussell King - ARM Linux 
268f6a8c609SRussell King - ARM Linux static int __init pismo_init(void)
269f6a8c609SRussell King - ARM Linux {
270f6a8c609SRussell King - ARM Linux 	BUILD_BUG_ON(sizeof(struct pismo_cs_block) != 48);
271f6a8c609SRussell King - ARM Linux 	BUILD_BUG_ON(sizeof(struct pismo_eeprom) != 256);
272f6a8c609SRussell King - ARM Linux 
273f6a8c609SRussell King - ARM Linux 	return i2c_add_driver(&pismo_driver);
274f6a8c609SRussell King - ARM Linux }
275f6a8c609SRussell King - ARM Linux module_init(pismo_init);
276f6a8c609SRussell King - ARM Linux 
277f6a8c609SRussell King - ARM Linux static void __exit pismo_exit(void)
278f6a8c609SRussell King - ARM Linux {
279f6a8c609SRussell King - ARM Linux 	i2c_del_driver(&pismo_driver);
280f6a8c609SRussell King - ARM Linux }
281f6a8c609SRussell King - ARM Linux module_exit(pismo_exit);
282f6a8c609SRussell King - ARM Linux 
283f6a8c609SRussell King - ARM Linux MODULE_AUTHOR("Russell King <linux@arm.linux.org.uk>");
284f6a8c609SRussell King - ARM Linux MODULE_DESCRIPTION("PISMO memory driver");
285f6a8c609SRussell King - ARM Linux MODULE_LICENSE("GPL");
286