ms02-nv.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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@ds2.pg.gda.pl>");
  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 8MB boundary within a 0MB up to 112MB range or at any 32MB
  32.  * boundary within a 0MB up to 448MB range.  We don't support a module
  33.  * at 0MB, 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 = (struct ms02nv_private *)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 = (struct ms02nv_private *)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 *)(KSEG1ADDR(addr + MS02NV_DIAG));
  78. ms02nv_magicp = (ms02nv_uint *)(KSEG1ADDR(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 8MB of address space. */
  103. mod_res = kmalloc(sizeof(*mod_res), GFP_KERNEL);
  104. if (!mod_res)
  105. return -ENOMEM;
  106. memset(mod_res, 0, sizeof(*mod_res));
  107. mod_res->name = ms02nv_name;
  108. mod_res->start = addr;
  109. mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
  110. mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  111. if (request_resource(&iomem_resource, mod_res) < 0)
  112. goto err_out_mod_res;
  113. size = ms02nv_probe_one(addr);
  114. if (!size)
  115. goto err_out_mod_res_rel;
  116. if (!version_printed) {
  117. printk(KERN_INFO "%s", version);
  118. version_printed = 1;
  119. }
  120. ret = -ENOMEM;
  121. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  122. if (!mtd)
  123. goto err_out_mod_res_rel;
  124. memset(mtd, 0, sizeof(*mtd));
  125. mp = kmalloc(sizeof(*mp), GFP_KERNEL);
  126. if (!mp)
  127. goto err_out_mtd;
  128. memset(mp, 0, sizeof(*mp));
  129. mtd->priv = mp;
  130. mp->resource.module = mod_res;
  131. /* Firmware's diagnostic NVRAM area. */
  132. diag_res = kmalloc(sizeof(*diag_res), GFP_KERNEL);
  133. if (!diag_res)
  134. goto err_out_mp;
  135. memset(diag_res, 0, sizeof(*diag_res));
  136. diag_res->name = ms02nv_res_diag_ram;
  137. diag_res->start = addr;
  138. diag_res->end = addr + MS02NV_RAM - 1;
  139. diag_res->flags = IORESOURCE_BUSY;
  140. request_resource(mod_res, diag_res);
  141. mp->resource.diag_ram = diag_res;
  142. /* User-available general-purpose NVRAM area. */
  143. user_res = kmalloc(sizeof(*user_res), GFP_KERNEL);
  144. if (!user_res)
  145. goto err_out_diag_res;
  146. memset(user_res, 0, sizeof(*user_res));
  147. user_res->name = ms02nv_res_user_ram;
  148. user_res->start = addr + MS02NV_RAM;
  149. user_res->end = addr + size - 1;
  150. user_res->flags = IORESOURCE_BUSY;
  151. request_resource(mod_res, user_res);
  152. mp->resource.user_ram = user_res;
  153. /* Control and status register. */
  154. csr_res = kmalloc(sizeof(*csr_res), GFP_KERNEL);
  155. if (!csr_res)
  156. goto err_out_user_res;
  157. memset(csr_res, 0, sizeof(*csr_res));
  158. csr_res->name = ms02nv_res_csr;
  159. csr_res->start = addr + MS02NV_CSR;
  160. csr_res->end = addr + MS02NV_CSR + 3;
  161. csr_res->flags = IORESOURCE_BUSY;
  162. request_resource(mod_res, csr_res);
  163. mp->resource.csr = csr_res;
  164. mp->addr = phys_to_virt(addr);
  165. mp->size = size;
  166. /*
  167.  * Hide the firmware's diagnostic area.  It may get destroyed
  168.  * upon a reboot.  Take paging into account for mapping support.
  169.  */
  170. fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
  171. fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
  172. mp->uaddr = phys_to_virt(fixaddr);
  173. mtd->type = MTD_RAM;
  174. mtd->flags = MTD_CAP_RAM | MTD_XIP;
  175. mtd->size = fixsize;
  176. mtd->name = (char *)ms02nv_name;
  177. mtd->module = THIS_MODULE;
  178. mtd->read = ms02nv_read;
  179. mtd->write = ms02nv_write;
  180. ret = -EIO;
  181. if (add_mtd_device(mtd)) {
  182. printk(KERN_ERR
  183. "ms02-nv: Unable to register MTD device, aborting!n");
  184. goto err_out_csr_res;
  185. }
  186. printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %uMB.n",
  187. mtd->index, ms02nv_name, addr, size >> 20);
  188. mp->next = root_ms02nv_mtd;
  189. root_ms02nv_mtd = mtd;
  190. return 0;
  191. err_out_csr_res:
  192. release_resource(csr_res);
  193. kfree(csr_res);
  194. err_out_user_res:
  195. release_resource(user_res);
  196. kfree(user_res);
  197. err_out_diag_res:
  198. release_resource(diag_res);
  199. kfree(diag_res);
  200. err_out_mp:
  201. kfree(mp);
  202. err_out_mtd:
  203. kfree(mtd);
  204. err_out_mod_res_rel:
  205. release_resource(mod_res);
  206. err_out_mod_res:
  207. kfree(mod_res);
  208. return ret;
  209. }
  210. static void __exit ms02nv_remove_one(void)
  211. {
  212. struct mtd_info *mtd = root_ms02nv_mtd;
  213. struct ms02nv_private *mp = (struct ms02nv_private *)mtd->priv;
  214. root_ms02nv_mtd = mp->next;
  215. del_mtd_device(mtd);
  216. release_resource(mp->resource.csr);
  217. kfree(mp->resource.csr);
  218. release_resource(mp->resource.user_ram);
  219. kfree(mp->resource.user_ram);
  220. release_resource(mp->resource.diag_ram);
  221. kfree(mp->resource.diag_ram);
  222. release_resource(mp->resource.module);
  223. kfree(mp->resource.module);
  224. kfree(mp);
  225. kfree(mtd);
  226. }
  227. static int __init ms02nv_init(void)
  228. {
  229. volatile u32 *csr;
  230. uint stride = 0;
  231. int count = 0;
  232. int i;
  233. switch (mips_machtype) {
  234. case MACH_DS5000_200:
  235. csr = (volatile u32 *)KN02_CSR_ADDR;
  236. if (*csr & KN02_CSR_BNK32M)
  237. stride = 2;
  238. break;
  239. case MACH_DS5000_2X0:
  240. csr = (volatile u32 *)KN03_MCR_BASE;
  241. if (*csr & KN03_MCR_BNK32M)
  242. stride = 2;
  243. break;
  244. default:
  245. return -ENODEV;
  246. break;
  247. }
  248. for (i = 0; i < (sizeof(ms02nv_addrs) / sizeof(*ms02nv_addrs)); i++)
  249. if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
  250. count++;
  251. return (count > 0) ? 0 : -ENODEV;
  252. }
  253. static void __exit ms02nv_cleanup(void)
  254. {
  255. while (root_ms02nv_mtd)
  256. ms02nv_remove_one();
  257. }
  258. module_init(ms02nv_init);
  259. module_exit(ms02nv_cleanup);