pmac_nvram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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/nvram.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/errno.h>
  23. #include <linux/adb.h>
  24. #include <linux/pmu.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/completion.h>
  27. #include <linux/spinlock.h>
  28. #include <asm/sections.h>
  29. #include <asm/io.h>
  30. #include <asm/system.h>
  31. #include <asm/prom.h>
  32. #include <asm/machdep.h>
  33. #include <asm/nvram.h>
  34. #define DEBUG
  35. #ifdef DEBUG
  36. #define DBG(x...) printk(x)
  37. #else
  38. #define DBG(x...)
  39. #endif
  40. #define NVRAM_SIZE 0x2000 /* 8kB of non-volatile RAM */
  41. #define CORE99_SIGNATURE 0x5a
  42. #define CORE99_ADLER_START 0x14
  43. /* On Core99, nvram is either a sharp, a micron or an AMD flash */
  44. #define SM_FLASH_STATUS_DONE 0x80
  45. #define SM_FLASH_STATUS_ERR 0x38
  46. #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
  47. #define SM_FLASH_CMD_ERASE_SETUP 0x20
  48. #define SM_FLASH_CMD_RESET 0xff
  49. #define SM_FLASH_CMD_WRITE_SETUP 0x40
  50. #define SM_FLASH_CMD_CLEAR_STATUS 0x50
  51. #define SM_FLASH_CMD_READ_STATUS 0x70
  52. /* CHRP NVRAM header */
  53. struct chrp_header {
  54. u8 signature;
  55. u8 cksum;
  56. u16 len;
  57. char name[12];
  58. u8 data[0];
  59. };
  60. struct core99_header {
  61. struct chrp_header hdr;
  62. u32 adler;
  63. u32 generation;
  64. u32 reserved[2];
  65. };
  66. /*
  67. * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
  68. */
  69. static int nvram_naddrs;
  70. static volatile unsigned char *nvram_addr;
  71. static volatile unsigned char *nvram_data;
  72. static int nvram_mult, is_core_99;
  73. static int core99_bank = 0;
  74. static int nvram_partitions[3];
  75. static DEFINE_SPINLOCK(nv_lock);
  76. extern int pmac_newworld;
  77. extern int system_running;
  78. static int (*core99_write_bank)(int bank, u8* datas);
  79. static int (*core99_erase_bank)(int bank);
  80. static char *nvram_image __pmacdata;
  81. static unsigned char __pmac core99_nvram_read_byte(int addr)
  82. {
  83. if (nvram_image == NULL)
  84. return 0xff;
  85. return nvram_image[addr];
  86. }
  87. static void __pmac core99_nvram_write_byte(int addr, unsigned char val)
  88. {
  89. if (nvram_image == NULL)
  90. return;
  91. nvram_image[addr] = val;
  92. }
  93. static unsigned char __openfirmware direct_nvram_read_byte(int addr)
  94. {
  95. return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);
  96. }
  97. static void __openfirmware direct_nvram_write_byte(int addr, unsigned char val)
  98. {
  99. out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);
  100. }
  101. static unsigned char __pmac indirect_nvram_read_byte(int addr)
  102. {
  103. unsigned char val;
  104. unsigned long flags;
  105. spin_lock_irqsave(&nv_lock, flags);
  106. out_8(nvram_addr, addr >> 5);
  107. val = in_8(&nvram_data[(addr & 0x1f) << 4]);
  108. spin_unlock_irqrestore(&nv_lock, flags);
  109. return val;
  110. }
  111. static void __pmac indirect_nvram_write_byte(int addr, unsigned char val)
  112. {
  113. unsigned long flags;
  114. spin_lock_irqsave(&nv_lock, flags);
  115. out_8(nvram_addr, addr >> 5);
  116. out_8(&nvram_data[(addr & 0x1f) << 4], val);
  117. spin_unlock_irqrestore(&nv_lock, flags);
  118. }
  119. #ifdef CONFIG_ADB_PMU
  120. static void __pmac pmu_nvram_complete(struct adb_request *req)
  121. {
  122. if (req->arg)
  123. complete((struct completion *)req->arg);
  124. }
  125. static unsigned char __pmac pmu_nvram_read_byte(int addr)
  126. {
  127. struct adb_request req;
  128. DECLARE_COMPLETION(req_complete);
  129. req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
  130. if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,
  131. (addr >> 8) & 0xff, addr & 0xff))
  132. return 0xff;
  133. if (system_state == SYSTEM_RUNNING)
  134. wait_for_completion(&req_complete);
  135. while (!req.complete)
  136. pmu_poll();
  137. return req.reply[0];
  138. }
  139. static void __pmac pmu_nvram_write_byte(int addr, unsigned char val)
  140. {
  141. struct adb_request req;
  142. DECLARE_COMPLETION(req_complete);
  143. req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
  144. if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,
  145. (addr >> 8) & 0xff, addr & 0xff, val))
  146. return;
  147. if (system_state == SYSTEM_RUNNING)
  148. wait_for_completion(&req_complete);
  149. while (!req.complete)
  150. pmu_poll();
  151. }
  152. #endif /* CONFIG_ADB_PMU */
  153. static u8 __pmac chrp_checksum(struct chrp_header* hdr)
  154. {
  155. u8 *ptr;
  156. u16 sum = hdr->signature;
  157. for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
  158. sum += *ptr;
  159. while (sum > 0xFF)
  160. sum = (sum & 0xFF) + (sum>>8);
  161. return sum;
  162. }
  163. static u32 __pmac core99_calc_adler(u8 *buffer)
  164. {
  165. int cnt;
  166. u32 low, high;
  167. buffer += CORE99_ADLER_START;
  168. low = 1;
  169. high = 0;
  170. for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
  171. if ((cnt % 5000) == 0) {
  172. high %= 65521UL;
  173. high %= 65521UL;
  174. }
  175. low += buffer[cnt];
  176. high += low;
  177. }
  178. low %= 65521UL;
  179. high %= 65521UL;
  180. return (high << 16) | low;
  181. }
  182. static u32 __pmac core99_check(u8* datas)
  183. {
  184. struct core99_header* hdr99 = (struct core99_header*)datas;
  185. if (hdr99->hdr.signature != CORE99_SIGNATURE) {
  186. DBG("Invalid signature\n");
  187. return 0;
  188. }
  189. if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
  190. DBG("Invalid checksum\n");
  191. return 0;
  192. }
  193. if (hdr99->adler != core99_calc_adler(datas)) {
  194. DBG("Invalid adler\n");
  195. return 0;
  196. }
  197. return hdr99->generation;
  198. }
  199. static int __pmac sm_erase_bank(int bank)
  200. {
  201. int stat, i;
  202. unsigned long timeout;
  203. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  204. DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
  205. out_8(base, SM_FLASH_CMD_ERASE_SETUP);
  206. out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
  207. timeout = 0;
  208. do {
  209. if (++timeout > 1000000) {
  210. printk(KERN_ERR "nvram: Sharp/Miron flash erase timeout !\n");
  211. break;
  212. }
  213. out_8(base, SM_FLASH_CMD_READ_STATUS);
  214. stat = in_8(base);
  215. } while (!(stat & SM_FLASH_STATUS_DONE));
  216. out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
  217. out_8(base, SM_FLASH_CMD_RESET);
  218. for (i=0; i<NVRAM_SIZE; i++)
  219. if (base[i] != 0xff) {
  220. printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
  221. return -ENXIO;
  222. }
  223. return 0;
  224. }
  225. static int __pmac sm_write_bank(int bank, u8* datas)
  226. {
  227. int i, stat = 0;
  228. unsigned long timeout;
  229. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  230. DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
  231. for (i=0; i<NVRAM_SIZE; i++) {
  232. out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
  233. udelay(1);
  234. out_8(base+i, datas[i]);
  235. timeout = 0;
  236. do {
  237. if (++timeout > 1000000) {
  238. printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
  239. break;
  240. }
  241. out_8(base, SM_FLASH_CMD_READ_STATUS);
  242. stat = in_8(base);
  243. } while (!(stat & SM_FLASH_STATUS_DONE));
  244. if (!(stat & SM_FLASH_STATUS_DONE))
  245. break;
  246. }
  247. out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
  248. out_8(base, SM_FLASH_CMD_RESET);
  249. for (i=0; i<NVRAM_SIZE; i++)
  250. if (base[i] != datas[i]) {
  251. printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
  252. return -ENXIO;
  253. }
  254. return 0;
  255. }
  256. static int __pmac amd_erase_bank(int bank)
  257. {
  258. int i, stat = 0;
  259. unsigned long timeout;
  260. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  261. DBG("nvram: AMD Erasing bank %d...\n", bank);
  262. /* Unlock 1 */
  263. out_8(base+0x555, 0xaa);
  264. udelay(1);
  265. /* Unlock 2 */
  266. out_8(base+0x2aa, 0x55);
  267. udelay(1);
  268. /* Sector-Erase */
  269. out_8(base+0x555, 0x80);
  270. udelay(1);
  271. out_8(base+0x555, 0xaa);
  272. udelay(1);
  273. out_8(base+0x2aa, 0x55);
  274. udelay(1);
  275. out_8(base, 0x30);
  276. udelay(1);
  277. timeout = 0;
  278. do {
  279. if (++timeout > 1000000) {
  280. printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
  281. break;
  282. }
  283. stat = in_8(base) ^ in_8(base);
  284. } while (stat != 0);
  285. /* Reset */
  286. out_8(base, 0xf0);
  287. udelay(1);
  288. for (i=0; i<NVRAM_SIZE; i++)
  289. if (base[i] != 0xff) {
  290. printk(KERN_ERR "nvram: AMD flash erase failed !\n");
  291. return -ENXIO;
  292. }
  293. return 0;
  294. }
  295. static int __pmac amd_write_bank(int bank, u8* datas)
  296. {
  297. int i, stat = 0;
  298. unsigned long timeout;
  299. u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
  300. DBG("nvram: AMD Writing bank %d...\n", bank);
  301. for (i=0; i<NVRAM_SIZE; i++) {
  302. /* Unlock 1 */
  303. out_8(base+0x555, 0xaa);
  304. udelay(1);
  305. /* Unlock 2 */
  306. out_8(base+0x2aa, 0x55);
  307. udelay(1);
  308. /* Write single word */
  309. out_8(base+0x555, 0xa0);
  310. udelay(1);
  311. out_8(base+i, datas[i]);
  312. timeout = 0;
  313. do {
  314. if (++timeout > 1000000) {
  315. printk(KERN_ERR "nvram: AMD flash write timeout !\n");
  316. break;
  317. }
  318. stat = in_8(base) ^ in_8(base);
  319. } while (stat != 0);
  320. if (stat != 0)
  321. break;
  322. }
  323. /* Reset */
  324. out_8(base, 0xf0);
  325. udelay(1);
  326. for (i=0; i<NVRAM_SIZE; i++)
  327. if (base[i] != datas[i]) {
  328. printk(KERN_ERR "nvram: AMD flash write failed !\n");
  329. return -ENXIO;
  330. }
  331. return 0;
  332. }
  333. static void __init lookup_partitions(void)
  334. {
  335. u8 buffer[17];
  336. int i, offset;
  337. struct chrp_header* hdr;
  338. if (pmac_newworld) {
  339. nvram_partitions[pmac_nvram_OF] = -1;
  340. nvram_partitions[pmac_nvram_XPRAM] = -1;
  341. nvram_partitions[pmac_nvram_NR] = -1;
  342. hdr = (struct chrp_header *)buffer;
  343. offset = 0;
  344. buffer[16] = 0;
  345. do {
  346. for (i=0;i<16;i++)
  347. buffer[i] = nvram_read_byte(offset+i);
  348. if (!strcmp(hdr->name, "common"))
  349. nvram_partitions[pmac_nvram_OF] = offset + 0x10;
  350. if (!strcmp(hdr->name, "APL,MacOS75")) {
  351. nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;
  352. nvram_partitions[pmac_nvram_NR] = offset + 0x110;
  353. }
  354. offset += (hdr->len * 0x10);
  355. } while(offset < NVRAM_SIZE);
  356. } else {
  357. nvram_partitions[pmac_nvram_OF] = 0x1800;
  358. nvram_partitions[pmac_nvram_XPRAM] = 0x1300;
  359. nvram_partitions[pmac_nvram_NR] = 0x1400;
  360. }
  361. DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);
  362. DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);
  363. DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);
  364. }
  365. static void __pmac core99_nvram_sync(void)
  366. {
  367. struct core99_header* hdr99;
  368. unsigned long flags;
  369. if (!is_core_99 || !nvram_data || !nvram_image)
  370. return;
  371. spin_lock_irqsave(&nv_lock, flags);
  372. if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
  373. NVRAM_SIZE))
  374. goto bail;
  375. DBG("Updating nvram...\n");
  376. hdr99 = (struct core99_header*)nvram_image;
  377. hdr99->generation++;
  378. hdr99->hdr.signature = CORE99_SIGNATURE;
  379. hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
  380. hdr99->adler = core99_calc_adler(nvram_image);
  381. core99_bank = core99_bank ? 0 : 1;
  382. if (core99_erase_bank)
  383. if (core99_erase_bank(core99_bank)) {
  384. printk("nvram: Error erasing bank %d\n", core99_bank);
  385. goto bail;
  386. }
  387. if (core99_write_bank)
  388. if (core99_write_bank(core99_bank, nvram_image))
  389. printk("nvram: Error writing bank %d\n", core99_bank);
  390. bail:
  391. spin_unlock_irqrestore(&nv_lock, flags);
  392. #ifdef DEBUG
  393. mdelay(2000);
  394. #endif
  395. }
  396. void __init pmac_nvram_init(void)
  397. {
  398. struct device_node *dp;
  399. nvram_naddrs = 0;
  400. dp = find_devices("nvram");
  401. if (dp == NULL) {
  402. printk(KERN_ERR "Can't find NVRAM device\n");
  403. return;
  404. }
  405. nvram_naddrs = dp->n_addrs;
  406. is_core_99 = device_is_compatible(dp, "nvram,flash");
  407. if (is_core_99) {
  408. int i;
  409. u32 gen_bank0, gen_bank1;
  410. if (nvram_naddrs < 1) {
  411. printk(KERN_ERR "nvram: no address\n");
  412. return;
  413. }
  414. nvram_image = alloc_bootmem(NVRAM_SIZE);
  415. if (nvram_image == NULL) {
  416. printk(KERN_ERR "nvram: can't allocate ram image\n");
  417. return;
  418. }
  419. nvram_data = ioremap(dp->addrs[0].address, NVRAM_SIZE*2);
  420. nvram_naddrs = 1; /* Make sure we get the correct case */
  421. DBG("nvram: Checking bank 0...\n");
  422. gen_bank0 = core99_check((u8 *)nvram_data);
  423. gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
  424. core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
  425. DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
  426. DBG("nvram: Active bank is: %d\n", core99_bank);
  427. for (i=0; i<NVRAM_SIZE; i++)
  428. nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
  429. ppc_md.nvram_read_val = core99_nvram_read_byte;
  430. ppc_md.nvram_write_val = core99_nvram_write_byte;
  431. ppc_md.nvram_sync = core99_nvram_sync;
  432. /*
  433. * Maybe we could be smarter here though making an exclusive list
  434. * of known flash chips is a bit nasty as older OF didn't provide us
  435. * with a useful "compatible" entry. A solution would be to really
  436. * identify the chip using flash id commands and base ourselves on
  437. * a list of known chips IDs
  438. */
  439. if (device_is_compatible(dp, "amd-0137")) {
  440. core99_erase_bank = amd_erase_bank;
  441. core99_write_bank = amd_write_bank;
  442. } else {
  443. core99_erase_bank = sm_erase_bank;
  444. core99_write_bank = sm_write_bank;
  445. }
  446. } else if (_machine == _MACH_chrp && nvram_naddrs == 1) {
  447. nvram_data = ioremap(dp->addrs[0].address + isa_mem_base,
  448. dp->addrs[0].size);
  449. nvram_mult = 1;
  450. ppc_md.nvram_read_val = direct_nvram_read_byte;
  451. ppc_md.nvram_write_val = direct_nvram_write_byte;
  452. } else if (nvram_naddrs == 1) {
  453. nvram_data = ioremap(dp->addrs[0].address, dp->addrs[0].size);
  454. nvram_mult = (dp->addrs[0].size + NVRAM_SIZE - 1) / NVRAM_SIZE;
  455. ppc_md.nvram_read_val = direct_nvram_read_byte;
  456. ppc_md.nvram_write_val = direct_nvram_write_byte;
  457. } else if (nvram_naddrs == 2) {
  458. nvram_addr = ioremap(dp->addrs[0].address, dp->addrs[0].size);
  459. nvram_data = ioremap(dp->addrs[1].address, dp->addrs[1].size);
  460. ppc_md.nvram_read_val = indirect_nvram_read_byte;
  461. ppc_md.nvram_write_val = indirect_nvram_write_byte;
  462. } else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {
  463. #ifdef CONFIG_ADB_PMU
  464. nvram_naddrs = -1;
  465. ppc_md.nvram_read_val = pmu_nvram_read_byte;
  466. ppc_md.nvram_write_val = pmu_nvram_write_byte;
  467. #endif /* CONFIG_ADB_PMU */
  468. } else {
  469. printk(KERN_ERR "Don't know how to access NVRAM with %d addresses\n",
  470. nvram_naddrs);
  471. }
  472. lookup_partitions();
  473. }
  474. int __pmac pmac_get_partition(int partition)
  475. {
  476. return nvram_partitions[partition];
  477. }
  478. u8 __pmac pmac_xpram_read(int xpaddr)
  479. {
  480. int offset = nvram_partitions[pmac_nvram_XPRAM];
  481. if (offset < 0)
  482. return 0xff;
  483. return ppc_md.nvram_read_val(xpaddr + offset);
  484. }
  485. void __pmac pmac_xpram_write(int xpaddr, u8 data)
  486. {
  487. int offset = nvram_partitions[pmac_nvram_XPRAM];
  488. if (offset < 0)
  489. return;
  490. ppc_md.nvram_write_val(xpaddr + offset, data);
  491. }
  492. EXPORT_SYMBOL(pmac_get_partition);
  493. EXPORT_SYMBOL(pmac_xpram_read);
  494. EXPORT_SYMBOL(pmac_xpram_write);