ms02-nv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. if (from + len > mtd->size)
  50. return -EINVAL;
  51. memcpy(buf, mp->uaddr + from, len);
  52. *retlen = len;
  53. return 0;
  54. }
  55. static int ms02nv_write(struct mtd_info *mtd, loff_t to,
  56. size_t len, size_t *retlen, const u_char *buf)
  57. {
  58. struct ms02nv_private *mp = mtd->priv;
  59. if (to + len > mtd->size)
  60. return -EINVAL;
  61. memcpy(mp->uaddr + to, buf, len);
  62. *retlen = len;
  63. return 0;
  64. }
  65. static inline uint ms02nv_probe_one(ulong addr)
  66. {
  67. ms02nv_uint *ms02nv_diagp;
  68. ms02nv_uint *ms02nv_magicp;
  69. uint ms02nv_diag;
  70. uint ms02nv_magic;
  71. size_t size;
  72. int err;
  73. /*
  74. * The firmware writes MS02NV_ID at MS02NV_MAGIC and also
  75. * a diagnostic status at MS02NV_DIAG.
  76. */
  77. ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG));
  78. ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC));
  79. err = get_dbe(ms02nv_magic, ms02nv_magicp);
  80. if (err)
  81. return 0;
  82. if (ms02nv_magic != MS02NV_ID)
  83. return 0;
  84. ms02nv_diag = *ms02nv_diagp;
  85. size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT;
  86. if (size > MS02NV_CSR)
  87. size = MS02NV_CSR;
  88. return size;
  89. }
  90. static int __init ms02nv_init_one(ulong addr)
  91. {
  92. struct mtd_info *mtd;
  93. struct ms02nv_private *mp;
  94. struct resource *mod_res;
  95. struct resource *diag_res;
  96. struct resource *user_res;
  97. struct resource *csr_res;
  98. ulong fixaddr;
  99. size_t size, fixsize;
  100. static int version_printed;
  101. int ret = -ENODEV;
  102. /* The module decodes 8MiB of address space. */
  103. mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
  104. if (!mod_res)
  105. return -ENOMEM;
  106. mod_res->name = ms02nv_name;
  107. mod_res->start = addr;
  108. mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
  109. mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  110. if (request_resource(&iomem_resource, mod_res) < 0)
  111. goto err_out_mod_res;
  112. size = ms02nv_probe_one(addr);
  113. if (!size)
  114. goto err_out_mod_res_rel;
  115. if (!version_printed) {
  116. printk(KERN_INFO "%s", version);
  117. version_printed = 1;
  118. }
  119. ret = -ENOMEM;
  120. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  121. if (!mtd)
  122. goto err_out_mod_res_rel;
  123. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  124. if (!mp)
  125. goto err_out_mtd;
  126. mtd->priv = mp;
  127. mp->resource.module = mod_res;
  128. /* Firmware's diagnostic NVRAM area. */
  129. diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
  130. if (!diag_res)
  131. goto err_out_mp;
  132. diag_res->name = ms02nv_res_diag_ram;
  133. diag_res->start = addr;
  134. diag_res->end = addr + MS02NV_RAM - 1;
  135. diag_res->flags = IORESOURCE_BUSY;
  136. request_resource(mod_res, diag_res);
  137. mp->resource.diag_ram = diag_res;
  138. /* User-available general-purpose NVRAM area. */
  139. user_res = kzalloc(sizeof(*user_res), GFP_KERNEL);
  140. if (!user_res)
  141. goto err_out_diag_res;
  142. user_res->name = ms02nv_res_user_ram;
  143. user_res->start = addr + MS02NV_RAM;
  144. user_res->end = addr + size - 1;
  145. user_res->flags = IORESOURCE_BUSY;
  146. request_resource(mod_res, user_res);
  147. mp->resource.user_ram = user_res;
  148. /* Control and status register. */
  149. csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL);
  150. if (!csr_res)
  151. goto err_out_user_res;
  152. csr_res->name = ms02nv_res_csr;
  153. csr_res->start = addr + MS02NV_CSR;
  154. csr_res->end = addr + MS02NV_CSR + 3;
  155. csr_res->flags = IORESOURCE_BUSY;
  156. request_resource(mod_res, csr_res);
  157. mp->resource.csr = csr_res;
  158. mp->addr = phys_to_virt(addr);
  159. mp->size = size;
  160. /*
  161. * Hide the firmware's diagnostic area. It may get destroyed
  162. * upon a reboot. Take paging into account for mapping support.
  163. */
  164. fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
  165. fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
  166. mp->uaddr = phys_to_virt(fixaddr);
  167. mtd->type = MTD_RAM;
  168. mtd->flags = MTD_CAP_RAM;
  169. mtd->size = fixsize;
  170. mtd->name = (char *)ms02nv_name;
  171. mtd->owner = THIS_MODULE;
  172. mtd->read = ms02nv_read;
  173. mtd->write = ms02nv_write;
  174. mtd->writesize = 1;
  175. ret = -EIO;
  176. if (add_mtd_device(mtd)) {
  177. printk(KERN_ERR
  178. "ms02-nv: Unable to register MTD device, aborting!\n");
  179. goto err_out_csr_res;
  180. }
  181. printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n",
  182. mtd->index, ms02nv_name, addr, size >> 20);
  183. mp->next = root_ms02nv_mtd;
  184. root_ms02nv_mtd = mtd;
  185. return 0;
  186. err_out_csr_res:
  187. release_resource(csr_res);
  188. kfree(csr_res);
  189. err_out_user_res:
  190. release_resource(user_res);
  191. kfree(user_res);
  192. err_out_diag_res:
  193. release_resource(diag_res);
  194. kfree(diag_res);
  195. err_out_mp:
  196. kfree(mp);
  197. err_out_mtd:
  198. kfree(mtd);
  199. err_out_mod_res_rel:
  200. release_resource(mod_res);
  201. err_out_mod_res:
  202. kfree(mod_res);
  203. return ret;
  204. }
  205. static void __exit ms02nv_remove_one(void)
  206. {
  207. struct mtd_info *mtd = root_ms02nv_mtd;
  208. struct ms02nv_private *mp = mtd->priv;
  209. root_ms02nv_mtd = mp->next;
  210. del_mtd_device(mtd);
  211. release_resource(mp->resource.csr);
  212. kfree(mp->resource.csr);
  213. release_resource(mp->resource.user_ram);
  214. kfree(mp->resource.user_ram);
  215. release_resource(mp->resource.diag_ram);
  216. kfree(mp->resource.diag_ram);
  217. release_resource(mp->resource.module);
  218. kfree(mp->resource.module);
  219. kfree(mp);
  220. kfree(mtd);
  221. }
  222. static int __init ms02nv_init(void)
  223. {
  224. volatile u32 *csr;
  225. uint stride = 0;
  226. int count = 0;
  227. int i;
  228. switch (mips_machtype) {
  229. case MACH_DS5000_200:
  230. csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
  231. if (*csr & KN02_CSR_BNK32M)
  232. stride = 2;
  233. break;
  234. case MACH_DS5000_2X0:
  235. case MACH_DS5900:
  236. csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
  237. if (*csr & KN03_MCR_BNK32M)
  238. stride = 2;
  239. break;
  240. default:
  241. return -ENODEV;
  242. break;
  243. }
  244. for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
  245. if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
  246. count++;
  247. return (count > 0) ? 0 : -ENODEV;
  248. }
  249. static void __exit ms02nv_cleanup(void)
  250. {
  251. while (root_ms02nv_mtd)
  252. ms02nv_remove_one();
  253. }
  254. module_init(ms02nv_init);
  255. module_exit(ms02nv_cleanup);