pismo.c 6.7 KB

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