ms02-nv.c 7.5 KB

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