pismo.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * PISMO memory driver - http://www.pismoworld.org/
  3. *
  4. * For ARM Realview and Versatile platforms
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/slab.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/mutex.h>
  17. #include <linux/mtd/physmap.h>
  18. #include <linux/mtd/plat-ram.h>
  19. #include <linux/mtd/pismo.h>
  20. #define PISMO_NUM_CS 5
  21. struct pismo_cs_block {
  22. u8 type;
  23. u8 width;
  24. __le16 access;
  25. __le32 size;
  26. u32 reserved[2];
  27. char device[32];
  28. } __packed;
  29. struct pismo_eeprom {
  30. struct pismo_cs_block cs[PISMO_NUM_CS];
  31. char board[15];
  32. u8 sum;
  33. } __packed;
  34. struct pismo_mem {
  35. phys_addr_t base;
  36. u32 size;
  37. u16 access;
  38. u8 width;
  39. u8 type;
  40. };
  41. struct pismo_data {
  42. struct i2c_client *client;
  43. void (*vpp)(void *, int);
  44. void *vpp_data;
  45. struct platform_device *dev[PISMO_NUM_CS];
  46. };
  47. /* FIXME: set_vpp could do with a better calling convention */
  48. static struct pismo_data *vpp_pismo;
  49. static DEFINE_MUTEX(pismo_mutex);
  50. static int pismo_setvpp_probe_fix(struct pismo_data *pismo)
  51. {
  52. mutex_lock(&pismo_mutex);
  53. if (vpp_pismo) {
  54. mutex_unlock(&pismo_mutex);
  55. kfree(pismo);
  56. return -EBUSY;
  57. }
  58. vpp_pismo = pismo;
  59. mutex_unlock(&pismo_mutex);
  60. return 0;
  61. }
  62. static void pismo_setvpp_remove_fix(struct pismo_data *pismo)
  63. {
  64. mutex_lock(&pismo_mutex);
  65. if (vpp_pismo == pismo)
  66. vpp_pismo = NULL;
  67. mutex_unlock(&pismo_mutex);
  68. }
  69. static void pismo_set_vpp(struct map_info *map, int on)
  70. {
  71. struct pismo_data *pismo = vpp_pismo;
  72. pismo->vpp(pismo->vpp_data, on);
  73. }
  74. /* end of hack */
  75. static unsigned int __devinit pismo_width_to_bytes(unsigned int width)
  76. {
  77. width &= 15;
  78. if (width > 2)
  79. return 0;
  80. return 1 << width;
  81. }
  82. static int __devinit pismo_eeprom_read(struct i2c_client *client, void *buf,
  83. u8 addr, size_t size)
  84. {
  85. int ret;
  86. struct i2c_msg msg[] = {
  87. {
  88. .addr = client->addr,
  89. .len = sizeof(addr),
  90. .buf = &addr,
  91. }, {
  92. .addr = client->addr,
  93. .flags = I2C_M_RD,
  94. .len = size,
  95. .buf = buf,
  96. },
  97. };
  98. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  99. return ret == ARRAY_SIZE(msg) ? size : -EIO;
  100. }
  101. static int __devinit pismo_add_device(struct pismo_data *pismo, int i,
  102. struct pismo_mem *region, const char *name, void *pdata, size_t psize)
  103. {
  104. struct platform_device *dev;
  105. struct resource res = { };
  106. phys_addr_t base = region->base;
  107. int ret;
  108. if (base == ~0)
  109. return -ENXIO;
  110. res.start = base;
  111. res.end = base + region->size - 1;
  112. res.flags = IORESOURCE_MEM;
  113. dev = platform_device_alloc(name, i);
  114. if (!dev)
  115. return -ENOMEM;
  116. dev->dev.parent = &pismo->client->dev;
  117. do {
  118. ret = platform_device_add_resources(dev, &res, 1);
  119. if (ret)
  120. break;
  121. ret = platform_device_add_data(dev, pdata, psize);
  122. if (ret)
  123. break;
  124. ret = platform_device_add(dev);
  125. if (ret)
  126. break;
  127. pismo->dev[i] = dev;
  128. return 0;
  129. } while (0);
  130. platform_device_put(dev);
  131. return ret;
  132. }
  133. static int __devinit pismo_add_nor(struct pismo_data *pismo, int i,
  134. struct pismo_mem *region)
  135. {
  136. struct physmap_flash_data data = {
  137. .width = region->width,
  138. };
  139. if (pismo->vpp)
  140. data.set_vpp = pismo_set_vpp;
  141. return pismo_add_device(pismo, i, region, "physmap-flash",
  142. &data, sizeof(data));
  143. }
  144. static int __devinit pismo_add_sram(struct pismo_data *pismo, int i,
  145. struct pismo_mem *region)
  146. {
  147. struct platdata_mtd_ram data = {
  148. .bankwidth = region->width,
  149. };
  150. return pismo_add_device(pismo, i, region, "mtd-ram",
  151. &data, sizeof(data));
  152. }
  153. static void __devinit pismo_add_one(struct pismo_data *pismo, int i,
  154. const struct pismo_cs_block *cs, phys_addr_t base)
  155. {
  156. struct device *dev = &pismo->client->dev;
  157. struct pismo_mem region;
  158. region.base = base;
  159. region.type = cs->type;
  160. region.width = pismo_width_to_bytes(cs->width);
  161. region.access = le16_to_cpu(cs->access);
  162. region.size = le32_to_cpu(cs->size);
  163. if (region.width == 0) {
  164. dev_err(dev, "cs%u: bad width: %02x, ignoring\n", i, cs->width);
  165. return;
  166. }
  167. /*
  168. * FIXME: may need to the platforms memory controller here, but at
  169. * the moment we assume that it has already been correctly setup.
  170. * The memory controller can also tell us the base address as well.
  171. */
  172. dev_info(dev, "cs%u: %.32s: type %02x access %u00ps size %uK\n",
  173. i, cs->device, region.type, region.access, region.size / 1024);
  174. switch (region.type) {
  175. case 0:
  176. break;
  177. case 1:
  178. /* static DOC */
  179. break;
  180. case 2:
  181. /* static NOR */
  182. pismo_add_nor(pismo, i, &region);
  183. break;
  184. case 3:
  185. /* static RAM */
  186. pismo_add_sram(pismo, i, &region);
  187. break;
  188. }
  189. }
  190. static int __devexit pismo_remove(struct i2c_client *client)
  191. {
  192. struct pismo_data *pismo = i2c_get_clientdata(client);
  193. int i;
  194. for (i = 0; i < ARRAY_SIZE(pismo->dev); i++)
  195. platform_device_unregister(pismo->dev[i]);
  196. /* FIXME: set_vpp needs saner arguments */
  197. pismo_setvpp_remove_fix(pismo);
  198. kfree(pismo);
  199. return 0;
  200. }
  201. static int __devinit pismo_probe(struct i2c_client *client,
  202. const struct i2c_device_id *id)
  203. {
  204. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  205. struct pismo_pdata *pdata = client->dev.platform_data;
  206. struct pismo_eeprom eeprom;
  207. struct pismo_data *pismo;
  208. int ret, i;
  209. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  210. dev_err(&client->dev, "functionality mismatch\n");
  211. return -EIO;
  212. }
  213. pismo = kzalloc(sizeof(*pismo), GFP_KERNEL);
  214. if (!pismo)
  215. return -ENOMEM;
  216. /* FIXME: set_vpp needs saner arguments */
  217. ret = pismo_setvpp_probe_fix(pismo);
  218. if (ret)
  219. return ret;
  220. pismo->client = client;
  221. if (pdata) {
  222. pismo->vpp = pdata->set_vpp;
  223. pismo->vpp_data = pdata->vpp_data;
  224. }
  225. i2c_set_clientdata(client, pismo);
  226. ret = pismo_eeprom_read(client, &eeprom, 0, sizeof(eeprom));
  227. if (ret < 0) {
  228. dev_err(&client->dev, "error reading EEPROM: %d\n", ret);
  229. goto exit_free;
  230. }
  231. dev_info(&client->dev, "%.15s board found\n", eeprom.board);
  232. for (i = 0; i < ARRAY_SIZE(eeprom.cs); i++)
  233. if (eeprom.cs[i].type != 0xff)
  234. pismo_add_one(pismo, i, &eeprom.cs[i],
  235. pdata->cs_addrs[i]);
  236. return 0;
  237. exit_free:
  238. kfree(pismo);
  239. return ret;
  240. }
  241. static const struct i2c_device_id pismo_id[] = {
  242. { "pismo" },
  243. { },
  244. };
  245. MODULE_DEVICE_TABLE(i2c, pismo_id);
  246. static struct i2c_driver pismo_driver = {
  247. .driver = {
  248. .name = "pismo",
  249. .owner = THIS_MODULE,
  250. },
  251. .probe = pismo_probe,
  252. .remove = __devexit_p(pismo_remove),
  253. .id_table = pismo_id,
  254. };
  255. static int __init pismo_init(void)
  256. {
  257. BUILD_BUG_ON(sizeof(struct pismo_cs_block) != 48);
  258. BUILD_BUG_ON(sizeof(struct pismo_eeprom) != 256);
  259. return i2c_add_driver(&pismo_driver);
  260. }
  261. module_init(pismo_init);
  262. static void __exit pismo_exit(void)
  263. {
  264. i2c_del_driver(&pismo_driver);
  265. }
  266. module_exit(pismo_exit);
  267. MODULE_AUTHOR("Russell King <linux@arm.linux.org.uk>");
  268. MODULE_DESCRIPTION("PISMO memory driver");
  269. MODULE_LICENSE("GPL");