nettel.c 13 KB

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