nettel.c 12 KB

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