pismo.c 6.1 KB

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