nettel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************/
  3. /*
  4. * nettel.c -- mappings for NETtel/SecureEdge/SnapGear (x86) boards.
  5. *
  6. * (C) Copyright 2000-2001, Greg Ungerer ([email protected])
  7. * (C) Copyright 2001-2002, SnapGear (www.snapgear.com)
  8. */
  9. /****************************************************************************/
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/map.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/mtd/cfi.h>
  18. #include <linux/reboot.h>
  19. #include <linux/err.h>
  20. #include <linux/kdev_t.h>
  21. #include <linux/root_dev.h>
  22. #include <asm/io.h>
  23. /****************************************************************************/
  24. #define INTEL_BUSWIDTH 1
  25. #define AMD_WINDOW_MAXSIZE 0x00200000
  26. #define AMD_BUSWIDTH 1
  27. /*
  28. * PAR masks and shifts, assuming 64K pages.
  29. */
  30. #define SC520_PAR_ADDR_MASK 0x00003fff
  31. #define SC520_PAR_ADDR_SHIFT 16
  32. #define SC520_PAR_TO_ADDR(par) \
  33. (((par)&SC520_PAR_ADDR_MASK) << SC520_PAR_ADDR_SHIFT)
  34. #define SC520_PAR_SIZE_MASK 0x01ffc000
  35. #define SC520_PAR_SIZE_SHIFT 2
  36. #define SC520_PAR_TO_SIZE(par) \
  37. ((((par)&SC520_PAR_SIZE_MASK) << SC520_PAR_SIZE_SHIFT) + (64*1024))
  38. #define SC520_PAR(cs, addr, size) \
  39. ((cs) | \
  40. ((((size)-(64*1024)) >> SC520_PAR_SIZE_SHIFT) & SC520_PAR_SIZE_MASK) | \
  41. (((addr) >> SC520_PAR_ADDR_SHIFT) & SC520_PAR_ADDR_MASK))
  42. #define SC520_PAR_BOOTCS 0x8a000000
  43. #define SC520_PAR_ROMCS1 0xaa000000
  44. #define SC520_PAR_ROMCS2 0xca000000 /* Cache disabled, 64K page */
  45. static void *nettel_mmcrp = NULL;
  46. #ifdef CONFIG_MTD_CFI_INTELEXT
  47. static struct mtd_info *intel_mtd;
  48. #endif
  49. static struct mtd_info *amd_mtd;
  50. /****************************************************************************/
  51. /****************************************************************************/
  52. #ifdef CONFIG_MTD_CFI_INTELEXT
  53. static struct map_info nettel_intel_map = {
  54. .name = "SnapGear Intel",
  55. .size = 0,
  56. .bankwidth = INTEL_BUSWIDTH,
  57. };
  58. static struct mtd_partition nettel_intel_partitions[] = {
  59. {
  60. .name = "SnapGear kernel",
  61. .offset = 0,
  62. .size = 0x000e0000
  63. },
  64. {
  65. .name = "SnapGear filesystem",
  66. .offset = 0x00100000,
  67. },
  68. {
  69. .name = "SnapGear config",
  70. .offset = 0x000e0000,
  71. .size = 0x00020000
  72. },
  73. {
  74. .name = "SnapGear Intel",
  75. .offset = 0
  76. },
  77. {
  78. .name = "SnapGear BIOS Config",
  79. .offset = 0x007e0000,
  80. .size = 0x00020000
  81. },
  82. {
  83. .name = "SnapGear BIOS",
  84. .offset = 0x007e0000,
  85. .size = 0x00020000
  86. },
  87. };
  88. #endif
  89. static struct map_info nettel_amd_map = {
  90. .name = "SnapGear AMD",
  91. .size = AMD_WINDOW_MAXSIZE,
  92. .bankwidth = AMD_BUSWIDTH,
  93. };
  94. static const struct mtd_partition nettel_amd_partitions[] = {
  95. {
  96. .name = "SnapGear BIOS config",
  97. .offset = 0x000e0000,
  98. .size = 0x00010000
  99. },
  100. {
  101. .name = "SnapGear BIOS",
  102. .offset = 0x000f0000,
  103. .size = 0x00010000
  104. },
  105. {
  106. .name = "SnapGear AMD",
  107. .offset = 0
  108. },
  109. {
  110. .name = "SnapGear high BIOS",
  111. .offset = 0x001f0000,
  112. .size = 0x00010000
  113. }
  114. };
  115. #define NUM_AMD_PARTITIONS ARRAY_SIZE(nettel_amd_partitions)
  116. /****************************************************************************/
  117. #ifdef CONFIG_MTD_CFI_INTELEXT
  118. /*
  119. * Set the Intel flash back to read mode since some old boot
  120. * loaders don't.
  121. */
  122. static int nettel_reboot_notifier(struct notifier_block *nb, unsigned long val, void *v)
  123. {
  124. struct cfi_private *cfi = nettel_intel_map.fldrv_priv;
  125. unsigned long b;
  126. /* Make sure all FLASH chips are put back into read mode */
  127. for (b = 0; (b < nettel_intel_partitions[3].size); b += 0x100000) {
  128. cfi_send_gen_cmd(0xff, 0x55, b, &nettel_intel_map, cfi,
  129. cfi->device_type, NULL);
  130. }
  131. return(NOTIFY_OK);
  132. }
  133. static struct notifier_block nettel_notifier_block = {
  134. nettel_reboot_notifier, NULL, 0
  135. };
  136. #endif
  137. /****************************************************************************/
  138. static int __init nettel_init(void)
  139. {
  140. volatile unsigned long *amdpar;
  141. unsigned long amdaddr, maxsize;
  142. int num_amd_partitions=0;
  143. #ifdef CONFIG_MTD_CFI_INTELEXT
  144. volatile unsigned long *intel0par, *intel1par;
  145. unsigned long orig_bootcspar, orig_romcs1par;
  146. unsigned long intel0addr, intel0size;
  147. unsigned long intel1addr, intel1size;
  148. int intelboot, intel0cs, intel1cs;
  149. int num_intel_partitions;
  150. #endif
  151. int rc = 0;
  152. nettel_mmcrp = (void *) ioremap(0xfffef000, 4096);
  153. if (nettel_mmcrp == NULL) {
  154. printk("SNAPGEAR: failed to disable MMCR cache??\n");
  155. return(-EIO);
  156. }
  157. /* Set CPU clock to be 33.000MHz */
  158. *((unsigned char *) (nettel_mmcrp + 0xc64)) = 0x01;
  159. amdpar = (volatile unsigned long *) (nettel_mmcrp + 0xc4);
  160. #ifdef CONFIG_MTD_CFI_INTELEXT
  161. intelboot = 0;
  162. intel0cs = SC520_PAR_ROMCS1;
  163. intel0par = (volatile unsigned long *) (nettel_mmcrp + 0xc0);
  164. intel1cs = SC520_PAR_ROMCS2;
  165. intel1par = (volatile unsigned long *) (nettel_mmcrp + 0xbc);
  166. /*
  167. * Save the CS settings then ensure ROMCS1 and ROMCS2 are off,
  168. * otherwise they might clash with where we try to map BOOTCS.
  169. */
  170. orig_bootcspar = *amdpar;
  171. orig_romcs1par = *intel0par;
  172. *intel0par = 0;
  173. *intel1par = 0;
  174. #endif
  175. /*
  176. * The first thing to do is determine if we have a separate
  177. * boot FLASH device. Typically this is a small (1 to 2MB)
  178. * AMD FLASH part. It seems that device size is about the
  179. * only way to tell if this is the case...
  180. */
  181. amdaddr = 0x20000000;
  182. maxsize = AMD_WINDOW_MAXSIZE;
  183. *amdpar = SC520_PAR(SC520_PAR_BOOTCS, amdaddr, maxsize);
  184. __asm__ ("wbinvd");
  185. nettel_amd_map.phys = amdaddr;
  186. nettel_amd_map.virt = ioremap(amdaddr, maxsize);
  187. if (!nettel_amd_map.virt) {
  188. printk("SNAPGEAR: failed to ioremap() BOOTCS\n");
  189. iounmap(nettel_mmcrp);
  190. return(-EIO);
  191. }
  192. simple_map_init(&nettel_amd_map);
  193. if ((amd_mtd = do_map_probe("jedec_probe", &nettel_amd_map))) {
  194. printk(KERN_NOTICE "SNAPGEAR: AMD flash device size = %dK\n",
  195. (int)(amd_mtd->size>>10));
  196. amd_mtd->owner = THIS_MODULE;
  197. /* The high BIOS partition is only present for 2MB units */
  198. num_amd_partitions = NUM_AMD_PARTITIONS;
  199. if (amd_mtd->size < AMD_WINDOW_MAXSIZE)
  200. num_amd_partitions--;
  201. /* Don't add the partition until after the primary INTEL's */
  202. #ifdef CONFIG_MTD_CFI_INTELEXT
  203. /*
  204. * Map the Intel flash into memory after the AMD
  205. * It has to start on a multiple of maxsize.
  206. */
  207. maxsize = SC520_PAR_TO_SIZE(orig_romcs1par);
  208. if (maxsize < (32 * 1024 * 1024))
  209. maxsize = (32 * 1024 * 1024);
  210. intel0addr = amdaddr + maxsize;
  211. #endif
  212. } else {
  213. #ifdef CONFIG_MTD_CFI_INTELEXT
  214. /* INTEL boot FLASH */
  215. intelboot++;
  216. if (!orig_romcs1par) {
  217. intel0cs = SC520_PAR_BOOTCS;
  218. intel0par = (volatile unsigned long *)
  219. (nettel_mmcrp + 0xc4);
  220. intel1cs = SC520_PAR_ROMCS1;
  221. intel1par = (volatile unsigned long *)
  222. (nettel_mmcrp + 0xc0);
  223. intel0addr = SC520_PAR_TO_ADDR(orig_bootcspar);
  224. maxsize = SC520_PAR_TO_SIZE(orig_bootcspar);
  225. } else {
  226. /* Kernel base is on ROMCS1, not BOOTCS */
  227. intel0cs = SC520_PAR_ROMCS1;
  228. intel0par = (volatile unsigned long *)
  229. (nettel_mmcrp + 0xc0);
  230. intel1cs = SC520_PAR_BOOTCS;
  231. intel1par = (volatile unsigned long *)
  232. (nettel_mmcrp + 0xc4);
  233. intel0addr = SC520_PAR_TO_ADDR(orig_romcs1par);
  234. maxsize = SC520_PAR_TO_SIZE(orig_romcs1par);
  235. }
  236. /* Destroy useless AMD MTD mapping */
  237. amd_mtd = NULL;
  238. iounmap(nettel_amd_map.virt);
  239. nettel_amd_map.virt = NULL;
  240. #else
  241. /* Only AMD flash supported */
  242. rc = -ENXIO;
  243. goto out_unmap2;
  244. #endif
  245. }
  246. #ifdef CONFIG_MTD_CFI_INTELEXT
  247. /*
  248. * We have determined the INTEL FLASH configuration, so lets
  249. * go ahead and probe for them now.
  250. */
  251. /* Set PAR to the maximum size */
  252. if (maxsize < (32 * 1024 * 1024))
  253. maxsize = (32 * 1024 * 1024);
  254. *intel0par = SC520_PAR(intel0cs, intel0addr, maxsize);
  255. /* Turn other PAR off so the first probe doesn't find it */
  256. *intel1par = 0;
  257. /* Probe for the size of the first Intel flash */
  258. nettel_intel_map.size = maxsize;
  259. nettel_intel_map.phys = intel0addr;
  260. nettel_intel_map.virt = ioremap(intel0addr, maxsize);
  261. if (!nettel_intel_map.virt) {
  262. printk("SNAPGEAR: failed to ioremap() ROMCS1\n");
  263. rc = -EIO;
  264. goto out_unmap2;
  265. }
  266. simple_map_init(&nettel_intel_map);
  267. intel_mtd = do_map_probe("cfi_probe", &nettel_intel_map);
  268. if (!intel_mtd) {
  269. rc = -ENXIO;
  270. goto out_unmap1;
  271. }
  272. /* Set PAR to the detected size */
  273. intel0size = intel_mtd->size;
  274. *intel0par = SC520_PAR(intel0cs, intel0addr, intel0size);
  275. /*
  276. * Map second Intel FLASH right after first. Set its size to the
  277. * same maxsize used for the first Intel FLASH.
  278. */
  279. intel1addr = intel0addr + intel0size;
  280. *intel1par = SC520_PAR(intel1cs, intel1addr, maxsize);
  281. __asm__ ("wbinvd");
  282. maxsize += intel0size;
  283. /* Delete the old map and probe again to do both chips */
  284. map_destroy(intel_mtd);
  285. intel_mtd = NULL;
  286. iounmap(nettel_intel_map.virt);
  287. nettel_intel_map.size = maxsize;
  288. nettel_intel_map.virt = ioremap(intel0addr, maxsize);
  289. if (!nettel_intel_map.virt) {
  290. printk("SNAPGEAR: failed to ioremap() ROMCS1/2\n");
  291. rc = -EIO;
  292. goto out_unmap2;
  293. }
  294. intel_mtd = do_map_probe("cfi_probe", &nettel_intel_map);
  295. if (! intel_mtd) {
  296. rc = -ENXIO;
  297. goto out_unmap1;
  298. }
  299. intel1size = intel_mtd->size - intel0size;
  300. if (intel1size > 0) {
  301. *intel1par = SC520_PAR(intel1cs, intel1addr, intel1size);
  302. __asm__ ("wbinvd");
  303. } else {
  304. *intel1par = 0;
  305. }
  306. printk(KERN_NOTICE "SNAPGEAR: Intel flash device size = %lldKiB\n",
  307. (unsigned long long)(intel_mtd->size >> 10));
  308. intel_mtd->owner = THIS_MODULE;
  309. num_intel_partitions = ARRAY_SIZE(nettel_intel_partitions);
  310. if (intelboot) {
  311. /*
  312. * Adjust offset and size of last boot partition.
  313. * Must allow for BIOS region at end of FLASH.
  314. */
  315. nettel_intel_partitions[1].size = (intel0size + intel1size) -
  316. (1024*1024 + intel_mtd->erasesize);
  317. nettel_intel_partitions[3].size = intel0size + intel1size;
  318. nettel_intel_partitions[4].offset =
  319. (intel0size + intel1size) - intel_mtd->erasesize;
  320. nettel_intel_partitions[4].size = intel_mtd->erasesize;
  321. nettel_intel_partitions[5].offset =
  322. nettel_intel_partitions[4].offset;
  323. nettel_intel_partitions[5].size =
  324. nettel_intel_partitions[4].size;
  325. } else {
  326. /* No BIOS regions when AMD boot */
  327. num_intel_partitions -= 2;
  328. }
  329. rc = mtd_device_register(intel_mtd, nettel_intel_partitions,
  330. num_intel_partitions);
  331. if (rc)
  332. goto out_map_destroy;
  333. #endif
  334. if (amd_mtd) {
  335. rc = mtd_device_register(amd_mtd, nettel_amd_partitions,
  336. num_amd_partitions);
  337. if (rc)
  338. goto out_mtd_unreg;
  339. }
  340. #ifdef CONFIG_MTD_CFI_INTELEXT
  341. register_reboot_notifier(&nettel_notifier_block);
  342. #endif
  343. return rc;
  344. out_mtd_unreg:
  345. #ifdef CONFIG_MTD_CFI_INTELEXT
  346. mtd_device_unregister(intel_mtd);
  347. out_map_destroy:
  348. map_destroy(intel_mtd);
  349. out_unmap1:
  350. iounmap(nettel_intel_map.virt);
  351. #endif
  352. out_unmap2:
  353. iounmap(nettel_mmcrp);
  354. iounmap(nettel_amd_map.virt);
  355. return rc;
  356. }
  357. /****************************************************************************/
  358. static void __exit nettel_cleanup(void)
  359. {
  360. #ifdef CONFIG_MTD_CFI_INTELEXT
  361. unregister_reboot_notifier(&nettel_notifier_block);
  362. #endif
  363. if (amd_mtd) {
  364. mtd_device_unregister(amd_mtd);
  365. map_destroy(amd_mtd);
  366. }
  367. if (nettel_mmcrp) {
  368. iounmap(nettel_mmcrp);
  369. nettel_mmcrp = NULL;
  370. }
  371. if (nettel_amd_map.virt) {
  372. iounmap(nettel_amd_map.virt);
  373. nettel_amd_map.virt = NULL;
  374. }
  375. #ifdef CONFIG_MTD_CFI_INTELEXT
  376. if (intel_mtd) {
  377. mtd_device_unregister(intel_mtd);
  378. map_destroy(intel_mtd);
  379. }
  380. if (nettel_intel_map.virt) {
  381. iounmap(nettel_intel_map.virt);
  382. nettel_intel_map.virt = NULL;
  383. }
  384. #endif
  385. }
  386. /****************************************************************************/
  387. module_init(nettel_init);
  388. module_exit(nettel_cleanup);
  389. MODULE_LICENSE("GPL");
  390. MODULE_AUTHOR("Greg Ungerer <[email protected]>");
  391. MODULE_DESCRIPTION("SnapGear/SecureEdge FLASH support");
  392. /****************************************************************************/