pmac_nvram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * arch/ppc/platforms/pmac_nvram.c
  3. *
  4. * Copyright (C) 2002 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Todo: - add support for the OF persistent properties
  12. */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/stddef.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/completion.h>
  24. #include <linux/spinlock.h>
  25. #include <asm/sections.h>
  26. #include <asm/io.h>
  27. #include <asm/system.h>
  28. #include <asm/prom.h>
  29. #include <asm/machdep.h>
  30. #include <asm/nvram.h>
  31. #define DEBUG
  32. #ifdef DEBUG
  33. #define DBG(x...) printk(x)
  34. #else
  35. #define DBG(x...)
  36. #endif
  37. #define NVRAM_SIZE 0x2000 /* 8kB of non-volatile RAM */
  38. #define CORE99_SIGNATURE 0x5a
  39. #define CORE99_ADLER_START 0x14
  40. /* On Core99, nvram is either a sharp, a micron or an AMD flash */
  41. #define SM_FLASH_STATUS_DONE 0x80
  42. #define SM_FLASH_STATUS_ERR 0x38
  43. #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
  44. #define SM_FLASH_CMD_ERASE_SETUP 0x20
  45. #define SM_FLASH_CMD_RESET 0xff
  46. #define SM_FLASH_CMD_WRITE_SETUP 0x40
  47. #define SM_FLASH_CMD_CLEAR_STATUS 0x50
  48. #define SM_FLASH_CMD_READ_STATUS 0x70
  49. /* CHRP NVRAM header */
  50. struct chrp_header {
  51. u8 signature;
  52. u8 cksum;
  53. u16 len;
  54. char name[12];
  55. u8 data[0];
  56. };
  57. struct core99_header {
  58. struct chrp_header hdr;
  59. u32 adler;
  60. u32 generation;
  61. u32 reserved[2];
  62. };
  63. /*
  64. * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
  65. */
  66. static volatile unsigned char *nvram_data;
  67. static int core99_bank = 0;
  68. // XXX Turn that into a sem
  69. static DEFINE_SPINLOCK(nv_lock);
  70. extern int system_running;
  71. static int (*core99_write_bank)(int bank, u8* datas);
  72. static int (*core99_erase_bank)(int bank);
  73. static char *nvram_image __pmacdata;
  74. static ssize_t __pmac core99_nvram_read(char *buf, size_t count, loff_t *index)
  75. {
  76. int i;
  77. if (nvram_image == NULL)
  78. return -ENODEV;
  79. if (*index > NVRAM_SIZE)
  80. return 0;
  81. i = *index;
  82. if (i + count > NVRAM_SIZE)
  83. count = NVRAM_SIZE - i;
  84. memcpy(buf, &nvram_image[i], count);
  85. *index = i + count;
  86. return count;
  87. }
  88. static ssize_t __pmac core99_nvram_write(char *buf, size_t count, loff_t *index)
  89. {
  90. int i;
  91. if (nvram_image == NULL)
  92. return -ENODEV;
  93. if (*index > NVRAM_SIZE)
  94. return 0;
  95. i = *index;
  96. if (i + count > NVRAM_SIZE)
  97. count = NVRAM_SIZE - i;
  98. memcpy(&nvram_image[i], buf, count);
  99. *index = i + count;
  100. return count;
  101. }
  102. static ssize_t __pmac core99_nvram_size(void)
  103. {
  104. if (nvram_image == NULL)
  105. return -ENODEV;
  106. return NVRAM_SIZE;
  107. }
  108. static u8 __pmac chrp_checksum(struct chrp_header* hdr)
  109. {
  110. u8 *ptr;
  111. u16 sum = hdr->signature;
  112. for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
  113. sum += *ptr;
  114. while (sum > 0xFF)
  115. sum = (sum & 0xFF) + (sum>>8);
  116. return sum;
  117. }
  118. static u32 __pmac core99_calc_adler(u8 *buffer)
  119. {
  120. int cnt;
  121. u32 low, high;
  122. buffer += CORE99_ADLER_START;
  123. low = 1;
  124. high = 0;
  125. for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
  126. if ((cnt % 5000) == 0) {
  127. high %= 65521UL;
  128. high %= 65521UL;
  129. }
  130. low += buffer[cnt];
  131. high += low;
  132. }
  133. low %= 65521UL;
  134. high %= 65521UL;
  135. return (high << 16) | low;
  136. }
  137. static u32 __pmac core99_check(u8* datas)
  138. {
  139. struct core99_header* hdr99 = (struct core99_header*)datas;
  140. if (hdr99->hdr.signature != CORE99_SIGNATURE) {
  141. DBG("Invalid signature\n");
  142. return 0;
  143. }
  144. if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
  145. DBG("Invalid checksum\n");
  146. return 0;
  147. }
  148. if (hdr99->adler != core99_calc_adler(datas)) {
  149. DBG("Invalid adler\n");
  150. return 0;
  151. }
  152. return hdr99->generation;
  153. }
  154. static int __pmac sm_erase_bank(int bank)
  155. {
  156. int stat, i;
  157. unsigned long timeout;
  158. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  159. DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
  160. out_8(base, SM_FLASH_CMD_ERASE_SETUP);
  161. out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
  162. timeout = 0;
  163. do {
  164. if (++timeout > 1000000) {
  165. printk(KERN_ERR "nvram: Sharp/Miron flash erase timeout !\n");
  166. break;
  167. }
  168. out_8(base, SM_FLASH_CMD_READ_STATUS);
  169. stat = in_8(base);
  170. } while (!(stat & SM_FLASH_STATUS_DONE));
  171. out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
  172. out_8(base, SM_FLASH_CMD_RESET);
  173. for (i=0; i<NVRAM_SIZE; i++)
  174. if (base[i] != 0xff) {
  175. printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
  176. return -ENXIO;
  177. }
  178. return 0;
  179. }
  180. static int __pmac sm_write_bank(int bank, u8* datas)
  181. {
  182. int i, stat = 0;
  183. unsigned long timeout;
  184. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  185. DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
  186. for (i=0; i<NVRAM_SIZE; i++) {
  187. out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
  188. udelay(1);
  189. out_8(base+i, datas[i]);
  190. timeout = 0;
  191. do {
  192. if (++timeout > 1000000) {
  193. printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
  194. break;
  195. }
  196. out_8(base, SM_FLASH_CMD_READ_STATUS);
  197. stat = in_8(base);
  198. } while (!(stat & SM_FLASH_STATUS_DONE));
  199. if (!(stat & SM_FLASH_STATUS_DONE))
  200. break;
  201. }
  202. out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
  203. out_8(base, SM_FLASH_CMD_RESET);
  204. for (i=0; i<NVRAM_SIZE; i++)
  205. if (base[i] != datas[i]) {
  206. printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
  207. return -ENXIO;
  208. }
  209. return 0;
  210. }
  211. static int __pmac amd_erase_bank(int bank)
  212. {
  213. int i, stat = 0;
  214. unsigned long timeout;
  215. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  216. DBG("nvram: AMD Erasing bank %d...\n", bank);
  217. /* Unlock 1 */
  218. out_8(base+0x555, 0xaa);
  219. udelay(1);
  220. /* Unlock 2 */
  221. out_8(base+0x2aa, 0x55);
  222. udelay(1);
  223. /* Sector-Erase */
  224. out_8(base+0x555, 0x80);
  225. udelay(1);
  226. out_8(base+0x555, 0xaa);
  227. udelay(1);
  228. out_8(base+0x2aa, 0x55);
  229. udelay(1);
  230. out_8(base, 0x30);
  231. udelay(1);
  232. timeout = 0;
  233. do {
  234. if (++timeout > 1000000) {
  235. printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
  236. break;
  237. }
  238. stat = in_8(base) ^ in_8(base);
  239. } while (stat != 0);
  240. /* Reset */
  241. out_8(base, 0xf0);
  242. udelay(1);
  243. for (i=0; i<NVRAM_SIZE; i++)
  244. if (base[i] != 0xff) {
  245. printk(KERN_ERR "nvram: AMD flash erase failed !\n");
  246. return -ENXIO;
  247. }
  248. return 0;
  249. }
  250. static int __pmac amd_write_bank(int bank, u8* datas)
  251. {
  252. int i, stat = 0;
  253. unsigned long timeout;
  254. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  255. DBG("nvram: AMD Writing bank %d...\n", bank);
  256. for (i=0; i<NVRAM_SIZE; i++) {
  257. /* Unlock 1 */
  258. out_8(base+0x555, 0xaa);
  259. udelay(1);
  260. /* Unlock 2 */
  261. out_8(base+0x2aa, 0x55);
  262. udelay(1);
  263. /* Write single word */
  264. out_8(base+0x555, 0xa0);
  265. udelay(1);
  266. out_8(base+i, datas[i]);
  267. timeout = 0;
  268. do {
  269. if (++timeout > 1000000) {
  270. printk(KERN_ERR "nvram: AMD flash write timeout !\n");
  271. break;
  272. }
  273. stat = in_8(base) ^ in_8(base);
  274. } while (stat != 0);
  275. if (stat != 0)
  276. break;
  277. }
  278. /* Reset */
  279. out_8(base, 0xf0);
  280. udelay(1);
  281. for (i=0; i<NVRAM_SIZE; i++)
  282. if (base[i] != datas[i]) {
  283. printk(KERN_ERR "nvram: AMD flash write failed !\n");
  284. return -ENXIO;
  285. }
  286. return 0;
  287. }
  288. static int __pmac core99_nvram_sync(void)
  289. {
  290. struct core99_header* hdr99;
  291. unsigned long flags;
  292. spin_lock_irqsave(&nv_lock, flags);
  293. if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
  294. NVRAM_SIZE))
  295. goto bail;
  296. DBG("Updating nvram...\n");
  297. hdr99 = (struct core99_header*)nvram_image;
  298. hdr99->generation++;
  299. hdr99->hdr.signature = CORE99_SIGNATURE;
  300. hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
  301. hdr99->adler = core99_calc_adler(nvram_image);
  302. core99_bank = core99_bank ? 0 : 1;
  303. if (core99_erase_bank)
  304. if (core99_erase_bank(core99_bank)) {
  305. printk("nvram: Error erasing bank %d\n", core99_bank);
  306. goto bail;
  307. }
  308. if (core99_write_bank)
  309. if (core99_write_bank(core99_bank, nvram_image))
  310. printk("nvram: Error writing bank %d\n", core99_bank);
  311. bail:
  312. spin_unlock_irqrestore(&nv_lock, flags);
  313. return 0;
  314. }
  315. int __init pmac_nvram_init(void)
  316. {
  317. struct device_node *dp;
  318. u32 gen_bank0, gen_bank1;
  319. int i;
  320. dp = find_devices("nvram");
  321. if (dp == NULL) {
  322. printk(KERN_ERR "Can't find NVRAM device\n");
  323. return -ENODEV;
  324. }
  325. if (!device_is_compatible(dp, "nvram,flash")) {
  326. printk(KERN_ERR "Incompatible type of NVRAM\n");
  327. return -ENXIO;
  328. }
  329. nvram_image = alloc_bootmem(NVRAM_SIZE);
  330. if (nvram_image == NULL) {
  331. printk(KERN_ERR "nvram: can't allocate ram image\n");
  332. return -ENOMEM;
  333. }
  334. nvram_data = ioremap(dp->addrs[0].address, NVRAM_SIZE*2);
  335. DBG("nvram: Checking bank 0...\n");
  336. gen_bank0 = core99_check((u8 *)nvram_data);
  337. gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
  338. core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
  339. DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
  340. DBG("nvram: Active bank is: %d\n", core99_bank);
  341. for (i=0; i<NVRAM_SIZE; i++)
  342. nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
  343. ppc_md.nvram_read = core99_nvram_read;
  344. ppc_md.nvram_write = core99_nvram_write;
  345. ppc_md.nvram_size = core99_nvram_size;
  346. ppc_md.nvram_sync = core99_nvram_sync;
  347. /*
  348. * Maybe we could be smarter here though making an exclusive list
  349. * of known flash chips is a bit nasty as older OF didn't provide us
  350. * with a useful "compatible" entry. A solution would be to really
  351. * identify the chip using flash id commands and base ourselves on
  352. * a list of known chips IDs
  353. */
  354. if (device_is_compatible(dp, "amd-0137")) {
  355. core99_erase_bank = amd_erase_bank;
  356. core99_write_bank = amd_write_bank;
  357. } else {
  358. core99_erase_bank = sm_erase_bank;
  359. core99_write_bank = sm_write_bank;
  360. }
  361. return 0;
  362. }
  363. int __pmac pmac_get_partition(int partition)
  364. {
  365. struct nvram_partition *part;
  366. const char *name;
  367. int sig;
  368. switch(partition) {
  369. case pmac_nvram_OF:
  370. name = "common";
  371. sig = NVRAM_SIG_SYS;
  372. break;
  373. case pmac_nvram_XPRAM:
  374. name = "APL,MacOS75";
  375. sig = NVRAM_SIG_OS;
  376. break;
  377. case pmac_nvram_NR:
  378. default:
  379. /* Oldworld stuff */
  380. return -ENODEV;
  381. }
  382. part = nvram_find_partition(sig, name);
  383. if (part == NULL)
  384. return 0;
  385. return part->index;
  386. }
  387. u8 __pmac pmac_xpram_read(int xpaddr)
  388. {
  389. int offset = pmac_get_partition(pmac_nvram_XPRAM);
  390. loff_t index;
  391. u8 buf;
  392. ssize_t count;
  393. if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
  394. return 0xff;
  395. index = offset + xpaddr;
  396. count = ppc_md.nvram_read(&buf, 1, &index);
  397. if (count != 1)
  398. return 0xff;
  399. return buf;
  400. }
  401. void __pmac pmac_xpram_write(int xpaddr, u8 data)
  402. {
  403. int offset = pmac_get_partition(pmac_nvram_XPRAM);
  404. loff_t index;
  405. u8 buf;
  406. if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
  407. return;
  408. index = offset + xpaddr;
  409. buf = data;
  410. ppc_md.nvram_write(&buf, 1, &index);
  411. }
  412. EXPORT_SYMBOL(pmac_get_partition);
  413. EXPORT_SYMBOL(pmac_xpram_read);
  414. EXPORT_SYMBOL(pmac_xpram_write);