ms02-nv.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (c) 2001 Maciej W. Rozycki
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/ioport.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <asm/addrspace.h>
  17. #include <asm/bootinfo.h>
  18. #include <asm/dec/ioasic_addrs.h>
  19. #include <asm/dec/kn02.h>
  20. #include <asm/dec/kn03.h>
  21. #include <asm/io.h>
  22. #include <asm/paccess.h>
  23. #include "ms02-nv.h"
  24. static char version[] __initdata =
  25. "ms02-nv.c: v.1.0.0 13 Aug 2001 Maciej W. Rozycki.\n";
  26. MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
  27. MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver");
  28. MODULE_LICENSE("GPL");
  29. /*
  30. * Addresses we probe for an MS02-NV at. Modules may be located
  31. * at any 8MiB boundary within a 0MiB up to 112MiB range or at any 32MiB
  32. * boundary within a 0MiB up to 448MiB range. We don't support a module
  33. * at 0MiB, though.
  34. */
  35. static ulong ms02nv_addrs[] __initdata = {
  36. 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000,
  37. 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000,
  38. 0x02000000, 0x01800000, 0x01000000, 0x00800000
  39. };
  40. static const char ms02nv_name[] = "DEC MS02-NV NVRAM";
  41. static const char ms02nv_res_diag_ram[] = "Diagnostic RAM";
  42. static const char ms02nv_res_user_ram[] = "General-purpose RAM";
  43. static const char ms02nv_res_csr[] = "Control and status register";
  44. static struct mtd_info *root_ms02nv_mtd;
  45. static int ms02nv_read(struct mtd_info *mtd, loff_t from,
  46. size_t len, size_t *retlen, u_char *buf)
  47. {
  48. struct ms02nv_private *mp = mtd->priv;
  49. memcpy(buf, mp->uaddr + from, len);
  50. *retlen = len;
  51. return 0;
  52. }
  53. static int ms02nv_write(struct mtd_info *mtd, loff_t to,
  54. size_t len, size_t *retlen, const u_char *buf)
  55. {
  56. struct ms02nv_private *mp = mtd->priv;
  57. memcpy(mp->uaddr + to, buf, len);
  58. *retlen = len;
  59. return 0;
  60. }
  61. static inline uint ms02nv_probe_one(ulong addr)
  62. {
  63. ms02nv_uint *ms02nv_diagp;
  64. ms02nv_uint *ms02nv_magicp;
  65. uint ms02nv_diag;
  66. uint ms02nv_magic;
  67. size_t size;
  68. int err;
  69. /*
  70. * The firmware writes MS02NV_ID at MS02NV_MAGIC and also
  71. * a diagnostic status at MS02NV_DIAG.
  72. */
  73. ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG));
  74. ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC));
  75. err = get_dbe(ms02nv_magic, ms02nv_magicp);
  76. if (err)
  77. return 0;
  78. if (ms02nv_magic != MS02NV_ID)
  79. return 0;
  80. ms02nv_diag = *ms02nv_diagp;
  81. size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT;
  82. if (size > MS02NV_CSR)
  83. size = MS02NV_CSR;
  84. return size;
  85. }
  86. static int __init ms02nv_init_one(ulong addr)
  87. {
  88. struct mtd_info *mtd;
  89. struct ms02nv_private *mp;
  90. struct resource *mod_res;
  91. struct resource *diag_res;
  92. struct resource *user_res;
  93. struct resource *csr_res;
  94. ulong fixaddr;
  95. size_t size, fixsize;
  96. static int version_printed;
  97. int ret = -ENODEV;
  98. /* The module decodes 8MiB of address space. */
  99. mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
  100. if (!mod_res)
  101. return -ENOMEM;
  102. mod_res->name = ms02nv_name;
  103. mod_res->start = addr;
  104. mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
  105. mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  106. if (request_resource(&iomem_resource, mod_res) < 0)
  107. goto err_out_mod_res;
  108. size = ms02nv_probe_one(addr);
  109. if (!size)
  110. goto err_out_mod_res_rel;
  111. if (!version_printed) {
  112. printk(KERN_INFO "%s", version);
  113. version_printed = 1;
  114. }
  115. ret = -ENOMEM;
  116. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  117. if (!mtd)
  118. goto err_out_mod_res_rel;
  119. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  120. if (!mp)
  121. goto err_out_mtd;
  122. mtd->priv = mp;
  123. mp->resource.module = mod_res;
  124. /* Firmware's diagnostic NVRAM area. */
  125. diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
  126. if (!diag_res)
  127. goto err_out_mp;
  128. diag_res->name = ms02nv_res_diag_ram;
  129. diag_res->start = addr;
  130. diag_res->end = addr + MS02NV_RAM - 1;
  131. diag_res->flags = IORESOURCE_BUSY;
  132. request_resource(mod_res, diag_res);
  133. mp->resource.diag_ram = diag_res;
  134. /* User-available general-purpose NVRAM area. */
  135. user_res = kzalloc(sizeof(*user_res), GFP_KERNEL);
  136. if (!user_res)
  137. goto err_out_diag_res;
  138. user_res->name = ms02nv_res_user_ram;
  139. user_res->start = addr + MS02NV_RAM;
  140. user_res->end = addr + size - 1;
  141. user_res->flags = IORESOURCE_BUSY;
  142. request_resource(mod_res, user_res);
  143. mp->resource.user_ram = user_res;
  144. /* Control and status register. */
  145. csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL);
  146. if (!csr_res)
  147. goto err_out_user_res;
  148. csr_res->name = ms02nv_res_csr;
  149. csr_res->start = addr + MS02NV_CSR;
  150. csr_res->end = addr + MS02NV_CSR + 3;
  151. csr_res->flags = IORESOURCE_BUSY;
  152. request_resource(mod_res, csr_res);
  153. mp->resource.csr = csr_res;
  154. mp->addr = phys_to_virt(addr);
  155. mp->size = size;
  156. /*
  157. * Hide the firmware's diagnostic area. It may get destroyed
  158. * upon a reboot. Take paging into account for mapping support.
  159. */
  160. fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
  161. fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
  162. mp->uaddr = phys_to_virt(fixaddr);
  163. mtd->type = MTD_RAM;
  164. mtd->flags = MTD_CAP_RAM;
  165. mtd->size = fixsize;
  166. mtd->name = ms02nv_name;
  167. mtd->owner = THIS_MODULE;
  168. mtd->_read = ms02nv_read;
  169. mtd->_write = ms02nv_write;
  170. mtd->writesize = 1;
  171. ret = -EIO;
  172. if (mtd_device_register(mtd, NULL, 0)) {
  173. printk(KERN_ERR
  174. "ms02-nv: Unable to register MTD device, aborting!\n");
  175. goto err_out_csr_res;
  176. }
  177. printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n",
  178. mtd->index, ms02nv_name, addr, size >> 20);
  179. mp->next = root_ms02nv_mtd;
  180. root_ms02nv_mtd = mtd;
  181. return 0;
  182. err_out_csr_res:
  183. release_resource(csr_res);
  184. kfree(csr_res);
  185. err_out_user_res:
  186. release_resource(user_res);
  187. kfree(user_res);
  188. err_out_diag_res:
  189. release_resource(diag_res);
  190. kfree(diag_res);
  191. err_out_mp:
  192. kfree(mp);
  193. err_out_mtd:
  194. kfree(mtd);
  195. err_out_mod_res_rel:
  196. release_resource(mod_res);
  197. err_out_mod_res:
  198. kfree(mod_res);
  199. return ret;
  200. }
  201. static void __exit ms02nv_remove_one(void)
  202. {
  203. struct mtd_info *mtd = root_ms02nv_mtd;
  204. struct ms02nv_private *mp = mtd->priv;
  205. root_ms02nv_mtd = mp->next;
  206. mtd_device_unregister(mtd);
  207. release_resource(mp->resource.csr);
  208. kfree(mp->resource.csr);
  209. release_resource(mp->resource.user_ram);
  210. kfree(mp->resource.user_ram);
  211. release_resource(mp->resource.diag_ram);
  212. kfree(mp->resource.diag_ram);
  213. release_resource(mp->resource.module);
  214. kfree(mp->resource.module);
  215. kfree(mp);
  216. kfree(mtd);
  217. }
  218. static int __init ms02nv_init(void)
  219. {
  220. volatile u32 *csr;
  221. uint stride = 0;
  222. int count = 0;
  223. int i;
  224. switch (mips_machtype) {
  225. case MACH_DS5000_200:
  226. csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
  227. if (*csr & KN02_CSR_BNK32M)
  228. stride = 2;
  229. break;
  230. case MACH_DS5000_2X0:
  231. case MACH_DS5900:
  232. csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
  233. if (*csr & KN03_MCR_BNK32M)
  234. stride = 2;
  235. break;
  236. default:
  237. return -ENODEV;
  238. break;
  239. }
  240. for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
  241. if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
  242. count++;
  243. return (count > 0) ? 0 : -ENODEV;
  244. }
  245. static void __exit ms02nv_cleanup(void)
  246. {
  247. while (root_ms02nv_mtd)
  248. ms02nv_remove_one();
  249. }
  250. module_init(ms02nv_init);
  251. module_exit(ms02nv_cleanup);