1 // SPDX-License-Identifier: GPL-2.0-only
3 * PISMO memory driver - http://www.pismoworld.org/
5 * For ARM Realview and Versatile platforms
7 #include <linux/init.h>
8 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/platform_device.h>
12 #include <linux/spinlock.h>
13 #include <linux/mutex.h>
14 #include <linux/mtd/physmap.h>
15 #include <linux/mtd/plat-ram.h>
16 #include <linux/mtd/pismo.h>
18 #define PISMO_NUM_CS 5
20 struct pismo_cs_block
{
30 struct pismo_cs_block cs
[PISMO_NUM_CS
];
44 struct i2c_client
*client
;
45 void (*vpp
)(void *, int);
47 struct platform_device
*dev
[PISMO_NUM_CS
];
50 static void pismo_set_vpp(struct platform_device
*pdev
, int on
)
52 struct i2c_client
*client
= to_i2c_client(pdev
->dev
.parent
);
53 struct pismo_data
*pismo
= i2c_get_clientdata(client
);
55 pismo
->vpp(pismo
->vpp_data
, on
);
58 static unsigned int pismo_width_to_bytes(unsigned int width
)
66 static int pismo_eeprom_read(struct i2c_client
*client
, void *buf
, u8 addr
,
70 struct i2c_msg msg
[] = {
83 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
85 return ret
== ARRAY_SIZE(msg
) ? size
: -EIO
;
88 static int pismo_add_device(struct pismo_data
*pismo
, int i
,
89 struct pismo_mem
*region
, const char *name
,
90 void *pdata
, size_t psize
)
92 struct platform_device
*dev
;
93 struct resource res
= { };
94 phys_addr_t base
= region
->base
;
101 res
.end
= base
+ region
->size
- 1;
102 res
.flags
= IORESOURCE_MEM
;
104 dev
= platform_device_alloc(name
, i
);
107 dev
->dev
.parent
= &pismo
->client
->dev
;
110 ret
= platform_device_add_resources(dev
, &res
, 1);
114 ret
= platform_device_add_data(dev
, pdata
, psize
);
118 ret
= platform_device_add(dev
);
126 platform_device_put(dev
);
130 static int pismo_add_nor(struct pismo_data
*pismo
, int i
,
131 struct pismo_mem
*region
)
133 struct physmap_flash_data data
= {
134 .width
= region
->width
,
138 data
.set_vpp
= pismo_set_vpp
;
140 return pismo_add_device(pismo
, i
, region
, "physmap-flash",
141 &data
, sizeof(data
));
144 static int pismo_add_sram(struct pismo_data
*pismo
, int i
,
145 struct pismo_mem
*region
)
147 struct platdata_mtd_ram data
= {
148 .bankwidth
= region
->width
,
151 return pismo_add_device(pismo
, i
, region
, "mtd-ram",
152 &data
, sizeof(data
));
155 static void pismo_add_one(struct pismo_data
*pismo
, int i
,
156 const struct pismo_cs_block
*cs
, phys_addr_t base
)
158 struct device
*dev
= &pismo
->client
->dev
;
159 struct pismo_mem region
;
162 region
.type
= cs
->type
;
163 region
.width
= pismo_width_to_bytes(cs
->width
);
164 region
.access
= le16_to_cpu(cs
->access
);
165 region
.size
= le32_to_cpu(cs
->size
);
167 if (region
.width
== 0) {
168 dev_err(dev
, "cs%u: bad width: %02x, ignoring\n", i
, cs
->width
);
173 * FIXME: may need to the platforms memory controller here, but at
174 * the moment we assume that it has already been correctly setup.
175 * The memory controller can also tell us the base address as well.
178 dev_info(dev
, "cs%u: %.32s: type %02x access %u00ps size %uK\n",
179 i
, cs
->device
, region
.type
, region
.access
, region
.size
/ 1024);
181 switch (region
.type
) {
189 pismo_add_nor(pismo
, i
, ®ion
);
193 pismo_add_sram(pismo
, i
, ®ion
);
198 static void pismo_remove(struct i2c_client
*client
)
200 struct pismo_data
*pismo
= i2c_get_clientdata(client
);
203 for (i
= 0; i
< ARRAY_SIZE(pismo
->dev
); i
++)
204 platform_device_unregister(pismo
->dev
[i
]);
209 static int pismo_probe(struct i2c_client
*client
)
211 struct pismo_pdata
*pdata
= client
->dev
.platform_data
;
212 struct pismo_eeprom eeprom
;
213 struct pismo_data
*pismo
;
216 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
217 dev_err(&client
->dev
, "functionality mismatch\n");
221 pismo
= kzalloc(sizeof(*pismo
), GFP_KERNEL
);
225 pismo
->client
= client
;
227 pismo
->vpp
= pdata
->set_vpp
;
228 pismo
->vpp_data
= pdata
->vpp_data
;
230 i2c_set_clientdata(client
, pismo
);
232 ret
= pismo_eeprom_read(client
, &eeprom
, 0, sizeof(eeprom
));
234 dev_err(&client
->dev
, "error reading EEPROM: %d\n", ret
);
238 dev_info(&client
->dev
, "%.15s board found\n", eeprom
.board
);
240 for (i
= 0; i
< ARRAY_SIZE(eeprom
.cs
); i
++)
241 if (eeprom
.cs
[i
].type
!= 0xff)
242 pismo_add_one(pismo
, i
, &eeprom
.cs
[i
],
252 static const struct i2c_device_id pismo_id
[] = {
256 MODULE_DEVICE_TABLE(i2c
, pismo_id
);
258 static struct i2c_driver pismo_driver
= {
262 .probe
= pismo_probe
,
263 .remove
= pismo_remove
,
264 .id_table
= pismo_id
,
267 static int __init
pismo_init(void)
269 BUILD_BUG_ON(sizeof(struct pismo_cs_block
) != 48);
270 BUILD_BUG_ON(sizeof(struct pismo_eeprom
) != 256);
272 return i2c_add_driver(&pismo_driver
);
274 module_init(pismo_init
);
276 static void __exit
pismo_exit(void)
278 i2c_del_driver(&pismo_driver
);
280 module_exit(pismo_exit
);
282 MODULE_AUTHOR("Russell King <linux@arm.linux.org.uk>");
283 MODULE_DESCRIPTION("PISMO memory driver");
284 MODULE_LICENSE("GPL");