sym_nvram.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
  4. * of PCI-SCSI IO processors.
  5. *
  6. * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
  7. *
  8. * This driver is derived from the Linux sym53c8xx driver.
  9. * Copyright (C) 1998-2000 Gerard Roudier
  10. *
  11. * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
  12. * a port of the FreeBSD ncr driver to Linux-1.2.13.
  13. *
  14. * The original ncr driver has been written for 386bsd and FreeBSD by
  15. * Wolfgang Stanglmeier <wolf@cologne.de>
  16. * Stefan Esser <se@mi.Uni-Koeln.de>
  17. * Copyright (C) 1994 Wolfgang Stanglmeier
  18. *
  19. * Other major contributions:
  20. *
  21. * NVRAM detection and reading.
  22. * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
  23. *
  24. *-----------------------------------------------------------------------------
  25. */
  26. #include "sym_glue.h"
  27. #include "sym_nvram.h"
  28. #ifdef SYM_CONF_DEBUG_NVRAM
  29. static u_char Tekram_boot_delay[7] = {3, 5, 10, 20, 30, 60, 120};
  30. #endif
  31. /*
  32. * Get host setup from NVRAM.
  33. */
  34. void sym_nvram_setup_host(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
  35. {
  36. /*
  37. * Get parity checking, host ID, verbose mode
  38. * and miscellaneous host flags from NVRAM.
  39. */
  40. switch (nvram->type) {
  41. case SYM_SYMBIOS_NVRAM:
  42. if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
  43. np->rv_scntl0 &= ~0x0a;
  44. np->myaddr = nvram->data.Symbios.host_id & 0x0f;
  45. if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
  46. np->verbose += 1;
  47. if (nvram->data.Symbios.flags1 & SYMBIOS_SCAN_HI_LO)
  48. shost->reverse_ordering = 1;
  49. if (nvram->data.Symbios.flags2 & SYMBIOS_AVOID_BUS_RESET)
  50. np->usrflags |= SYM_AVOID_BUS_RESET;
  51. break;
  52. case SYM_TEKRAM_NVRAM:
  53. np->myaddr = nvram->data.Tekram.host_id & 0x0f;
  54. break;
  55. #ifdef CONFIG_PARISC
  56. case SYM_PARISC_PDC:
  57. if (nvram->data.parisc.host_id != -1)
  58. np->myaddr = nvram->data.parisc.host_id;
  59. if (nvram->data.parisc.factor != -1)
  60. np->minsync = nvram->data.parisc.factor;
  61. if (nvram->data.parisc.width != -1)
  62. np->maxwide = nvram->data.parisc.width;
  63. switch (nvram->data.parisc.mode) {
  64. case 0: np->scsi_mode = SMODE_SE; break;
  65. case 1: np->scsi_mode = SMODE_HVD; break;
  66. case 2: np->scsi_mode = SMODE_LVD; break;
  67. default: break;
  68. }
  69. #endif
  70. default:
  71. break;
  72. }
  73. }
  74. /*
  75. * Get target set-up from Symbios format NVRAM.
  76. */
  77. static void
  78. sym_Symbios_setup_target(struct sym_tcb *tp, int target, Symbios_nvram *nvram)
  79. {
  80. Symbios_target *tn = &nvram->target[target];
  81. if (!(tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED))
  82. tp->usrtags = 0;
  83. if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
  84. tp->usrflags &= ~SYM_DISC_ENABLED;
  85. if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
  86. tp->usrflags |= SYM_SCAN_BOOT_DISABLED;
  87. if (!(tn->flags & SYMBIOS_SCAN_LUNS))
  88. tp->usrflags |= SYM_SCAN_LUNS_DISABLED;
  89. tp->usr_period = (tn->sync_period + 3) / 4;
  90. tp->usr_width = (tn->bus_width == 0x8) ? 0 : 1;
  91. }
  92. static const unsigned char Tekram_sync[16] = {
  93. 25, 31, 37, 43, 50, 62, 75, 125, 12, 15, 18, 21, 6, 7, 9, 10
  94. };
  95. /*
  96. * Get target set-up from Tekram format NVRAM.
  97. */
  98. static void
  99. sym_Tekram_setup_target(struct sym_tcb *tp, int target, Tekram_nvram *nvram)
  100. {
  101. struct Tekram_target *tn = &nvram->target[target];
  102. if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
  103. tp->usrtags = 2 << nvram->max_tags_index;
  104. }
  105. if (tn->flags & TEKRAM_DISCONNECT_ENABLE)
  106. tp->usrflags |= SYM_DISC_ENABLED;
  107. if (tn->flags & TEKRAM_SYNC_NEGO)
  108. tp->usr_period = Tekram_sync[tn->sync_index & 0xf];
  109. tp->usr_width = (tn->flags & TEKRAM_WIDE_NEGO) ? 1 : 0;
  110. }
  111. /*
  112. * Get target setup from NVRAM.
  113. */
  114. void sym_nvram_setup_target(struct sym_tcb *tp, int target, struct sym_nvram *nvp)
  115. {
  116. switch (nvp->type) {
  117. case SYM_SYMBIOS_NVRAM:
  118. sym_Symbios_setup_target(tp, target, &nvp->data.Symbios);
  119. break;
  120. case SYM_TEKRAM_NVRAM:
  121. sym_Tekram_setup_target(tp, target, &nvp->data.Tekram);
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. #ifdef SYM_CONF_DEBUG_NVRAM
  128. /*
  129. * Dump Symbios format NVRAM for debugging purpose.
  130. */
  131. static void sym_display_Symbios_nvram(struct sym_device *np, Symbios_nvram *nvram)
  132. {
  133. int i;
  134. /* display Symbios nvram host data */
  135. printf("%s: HOST ID=%d%s%s%s%s%s%s\n",
  136. sym_name(np), nvram->host_id & 0x0f,
  137. (nvram->flags & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"",
  138. (nvram->flags & SYMBIOS_PARITY_ENABLE) ? " PARITY" :"",
  139. (nvram->flags & SYMBIOS_VERBOSE_MSGS) ? " VERBOSE" :"",
  140. (nvram->flags & SYMBIOS_CHS_MAPPING) ? " CHS_ALT" :"",
  141. (nvram->flags2 & SYMBIOS_AVOID_BUS_RESET)?" NO_RESET" :"",
  142. (nvram->flags1 & SYMBIOS_SCAN_HI_LO) ? " HI_LO" :"");
  143. /* display Symbios nvram drive data */
  144. for (i = 0 ; i < 15 ; i++) {
  145. struct Symbios_target *tn = &nvram->target[i];
  146. printf("%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
  147. sym_name(np), i,
  148. (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC" : "",
  149. (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT" : "",
  150. (tn->flags & SYMBIOS_SCAN_LUNS) ? " SCAN_LUNS" : "",
  151. (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ" : "",
  152. tn->bus_width,
  153. tn->sync_period / 4,
  154. tn->timeout);
  155. }
  156. }
  157. /*
  158. * Dump TEKRAM format NVRAM for debugging purpose.
  159. */
  160. static void sym_display_Tekram_nvram(struct sym_device *np, Tekram_nvram *nvram)
  161. {
  162. int i, tags, boot_delay;
  163. char *rem;
  164. /* display Tekram nvram host data */
  165. tags = 2 << nvram->max_tags_index;
  166. boot_delay = 0;
  167. if (nvram->boot_delay_index < 6)
  168. boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
  169. switch ((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
  170. default:
  171. case 0: rem = ""; break;
  172. case 1: rem = " REMOVABLE=boot device"; break;
  173. case 2: rem = " REMOVABLE=all"; break;
  174. }
  175. printf("%s: HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
  176. sym_name(np), nvram->host_id & 0x0f,
  177. (nvram->flags1 & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"",
  178. (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES":"",
  179. (nvram->flags & TEKRAM_DRIVES_SUP_1GB) ? " >1GB" :"",
  180. (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET" :"",
  181. (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG" :"",
  182. (nvram->flags & TEKRAM_IMMEDIATE_SEEK) ? " IMM_SEEK" :"",
  183. (nvram->flags & TEKRAM_SCAN_LUNS) ? " SCAN_LUNS" :"",
  184. (nvram->flags1 & TEKRAM_F2_F6_ENABLED) ? " F2_F6" :"",
  185. rem, boot_delay, tags);
  186. /* display Tekram nvram drive data */
  187. for (i = 0; i <= 15; i++) {
  188. int sync, j;
  189. struct Tekram_target *tn = &nvram->target[i];
  190. j = tn->sync_index & 0xf;
  191. sync = Tekram_sync[j];
  192. printf("%s-%d:%s%s%s%s%s%s PERIOD=%d\n",
  193. sym_name(np), i,
  194. (tn->flags & TEKRAM_PARITY_CHECK) ? " PARITY" : "",
  195. (tn->flags & TEKRAM_SYNC_NEGO) ? " SYNC" : "",
  196. (tn->flags & TEKRAM_DISCONNECT_ENABLE) ? " DISC" : "",
  197. (tn->flags & TEKRAM_START_CMD) ? " START" : "",
  198. (tn->flags & TEKRAM_TAGGED_COMMANDS) ? " TCQ" : "",
  199. (tn->flags & TEKRAM_WIDE_NEGO) ? " WIDE" : "",
  200. sync);
  201. }
  202. }
  203. #else
  204. static void sym_display_Symbios_nvram(struct sym_device *np, Symbios_nvram *nvram) { (void)np; (void)nvram; }
  205. static void sym_display_Tekram_nvram(struct sym_device *np, Tekram_nvram *nvram) { (void)np; (void)nvram; }
  206. #endif /* SYM_CONF_DEBUG_NVRAM */
  207. /*
  208. * 24C16 EEPROM reading.
  209. *
  210. * GPIO0 - data in/data out
  211. * GPIO1 - clock
  212. * Symbios NVRAM wiring now also used by Tekram.
  213. */
  214. #define SET_BIT 0
  215. #define CLR_BIT 1
  216. #define SET_CLK 2
  217. #define CLR_CLK 3
  218. /*
  219. * Set/clear data/clock bit in GPIO0
  220. */
  221. static void S24C16_set_bit(struct sym_device *np, u_char write_bit, u_char *gpreg,
  222. int bit_mode)
  223. {
  224. udelay(5);
  225. switch (bit_mode) {
  226. case SET_BIT:
  227. *gpreg |= write_bit;
  228. break;
  229. case CLR_BIT:
  230. *gpreg &= 0xfe;
  231. break;
  232. case SET_CLK:
  233. *gpreg |= 0x02;
  234. break;
  235. case CLR_CLK:
  236. *gpreg &= 0xfd;
  237. break;
  238. }
  239. OUTB(np, nc_gpreg, *gpreg);
  240. INB(np, nc_mbox1);
  241. udelay(5);
  242. }
  243. /*
  244. * Send START condition to NVRAM to wake it up.
  245. */
  246. static void S24C16_start(struct sym_device *np, u_char *gpreg)
  247. {
  248. S24C16_set_bit(np, 1, gpreg, SET_BIT);
  249. S24C16_set_bit(np, 0, gpreg, SET_CLK);
  250. S24C16_set_bit(np, 0, gpreg, CLR_BIT);
  251. S24C16_set_bit(np, 0, gpreg, CLR_CLK);
  252. }
  253. /*
  254. * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
  255. */
  256. static void S24C16_stop(struct sym_device *np, u_char *gpreg)
  257. {
  258. S24C16_set_bit(np, 0, gpreg, SET_CLK);
  259. S24C16_set_bit(np, 1, gpreg, SET_BIT);
  260. }
  261. /*
  262. * Read or write a bit to the NVRAM,
  263. * read if GPIO0 input else write if GPIO0 output
  264. */
  265. static void S24C16_do_bit(struct sym_device *np, u_char *read_bit, u_char write_bit,
  266. u_char *gpreg)
  267. {
  268. S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
  269. S24C16_set_bit(np, 0, gpreg, SET_CLK);
  270. if (read_bit)
  271. *read_bit = INB(np, nc_gpreg);
  272. S24C16_set_bit(np, 0, gpreg, CLR_CLK);
  273. S24C16_set_bit(np, 0, gpreg, CLR_BIT);
  274. }
  275. /*
  276. * Output an ACK to the NVRAM after reading,
  277. * change GPIO0 to output and when done back to an input
  278. */
  279. static void S24C16_write_ack(struct sym_device *np, u_char write_bit, u_char *gpreg,
  280. u_char *gpcntl)
  281. {
  282. OUTB(np, nc_gpcntl, *gpcntl & 0xfe);
  283. S24C16_do_bit(np, NULL, write_bit, gpreg);
  284. OUTB(np, nc_gpcntl, *gpcntl);
  285. }
  286. /*
  287. * Input an ACK from NVRAM after writing,
  288. * change GPIO0 to input and when done back to an output
  289. */
  290. static void S24C16_read_ack(struct sym_device *np, u_char *read_bit, u_char *gpreg,
  291. u_char *gpcntl)
  292. {
  293. OUTB(np, nc_gpcntl, *gpcntl | 0x01);
  294. S24C16_do_bit(np, read_bit, 1, gpreg);
  295. OUTB(np, nc_gpcntl, *gpcntl);
  296. }
  297. /*
  298. * WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
  299. * GPIO0 must already be set as an output
  300. */
  301. static void S24C16_write_byte(struct sym_device *np, u_char *ack_data, u_char write_data,
  302. u_char *gpreg, u_char *gpcntl)
  303. {
  304. int x;
  305. for (x = 0; x < 8; x++)
  306. S24C16_do_bit(np, NULL, (write_data >> (7 - x)) & 0x01, gpreg);
  307. S24C16_read_ack(np, ack_data, gpreg, gpcntl);
  308. }
  309. /*
  310. * READ a byte from the NVRAM and then send an ACK to say we have got it,
  311. * GPIO0 must already be set as an input
  312. */
  313. static void S24C16_read_byte(struct sym_device *np, u_char *read_data, u_char ack_data,
  314. u_char *gpreg, u_char *gpcntl)
  315. {
  316. int x;
  317. u_char read_bit;
  318. *read_data = 0;
  319. for (x = 0; x < 8; x++) {
  320. S24C16_do_bit(np, &read_bit, 1, gpreg);
  321. *read_data |= ((read_bit & 0x01) << (7 - x));
  322. }
  323. S24C16_write_ack(np, ack_data, gpreg, gpcntl);
  324. }
  325. #ifdef SYM_CONF_NVRAM_WRITE_SUPPORT
  326. /*
  327. * Write 'len' bytes starting at 'offset'.
  328. */
  329. static int sym_write_S24C16_nvram(struct sym_device *np, int offset,
  330. u_char *data, int len)
  331. {
  332. u_char gpcntl, gpreg;
  333. u_char old_gpcntl, old_gpreg;
  334. u_char ack_data;
  335. int x;
  336. /* save current state of GPCNTL and GPREG */
  337. old_gpreg = INB(np, nc_gpreg);
  338. old_gpcntl = INB(np, nc_gpcntl);
  339. gpcntl = old_gpcntl & 0x1c;
  340. /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
  341. OUTB(np, nc_gpreg, old_gpreg);
  342. OUTB(np, nc_gpcntl, gpcntl);
  343. /* this is to set NVRAM into a known state with GPIO0/1 both low */
  344. gpreg = old_gpreg;
  345. S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
  346. S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
  347. /* now set NVRAM inactive with GPIO0/1 both high */
  348. S24C16_stop(np, &gpreg);
  349. /* NVRAM has to be written in segments of 16 bytes */
  350. for (x = 0; x < len ; x += 16) {
  351. do {
  352. S24C16_start(np, &gpreg);
  353. S24C16_write_byte(np, &ack_data,
  354. 0xa0 | (((offset+x) >> 7) & 0x0e),
  355. &gpreg, &gpcntl);
  356. } while (ack_data & 0x01);
  357. S24C16_write_byte(np, &ack_data, (offset+x) & 0xff,
  358. &gpreg, &gpcntl);
  359. for (y = 0; y < 16; y++)
  360. S24C16_write_byte(np, &ack_data, data[x+y],
  361. &gpreg, &gpcntl);
  362. S24C16_stop(np, &gpreg);
  363. }
  364. /* return GPIO0/1 to original states after having accessed NVRAM */
  365. OUTB(np, nc_gpcntl, old_gpcntl);
  366. OUTB(np, nc_gpreg, old_gpreg);
  367. return 0;
  368. }
  369. #endif /* SYM_CONF_NVRAM_WRITE_SUPPORT */
  370. /*
  371. * Read 'len' bytes starting at 'offset'.
  372. */
  373. static int sym_read_S24C16_nvram(struct sym_device *np, int offset, u_char *data, int len)
  374. {
  375. u_char gpcntl, gpreg;
  376. u_char old_gpcntl, old_gpreg;
  377. u_char ack_data;
  378. int retv = 1;
  379. int x;
  380. /* save current state of GPCNTL and GPREG */
  381. old_gpreg = INB(np, nc_gpreg);
  382. old_gpcntl = INB(np, nc_gpcntl);
  383. gpcntl = old_gpcntl & 0x1c;
  384. /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
  385. OUTB(np, nc_gpreg, old_gpreg);
  386. OUTB(np, nc_gpcntl, gpcntl);
  387. /* this is to set NVRAM into a known state with GPIO0/1 both low */
  388. gpreg = old_gpreg;
  389. S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
  390. S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
  391. /* now set NVRAM inactive with GPIO0/1 both high */
  392. S24C16_stop(np, &gpreg);
  393. /* activate NVRAM */
  394. S24C16_start(np, &gpreg);
  395. /* write device code and random address MSB */
  396. S24C16_write_byte(np, &ack_data,
  397. 0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
  398. if (ack_data & 0x01)
  399. goto out;
  400. /* write random address LSB */
  401. S24C16_write_byte(np, &ack_data,
  402. offset & 0xff, &gpreg, &gpcntl);
  403. if (ack_data & 0x01)
  404. goto out;
  405. /* regenerate START state to set up for reading */
  406. S24C16_start(np, &gpreg);
  407. /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
  408. S24C16_write_byte(np, &ack_data,
  409. 0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
  410. if (ack_data & 0x01)
  411. goto out;
  412. /* now set up GPIO0 for inputting data */
  413. gpcntl |= 0x01;
  414. OUTB(np, nc_gpcntl, gpcntl);
  415. /* input all requested data - only part of total NVRAM */
  416. for (x = 0; x < len; x++)
  417. S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl);
  418. /* finally put NVRAM back in inactive mode */
  419. gpcntl &= 0xfe;
  420. OUTB(np, nc_gpcntl, gpcntl);
  421. S24C16_stop(np, &gpreg);
  422. retv = 0;
  423. out:
  424. /* return GPIO0/1 to original states after having accessed NVRAM */
  425. OUTB(np, nc_gpcntl, old_gpcntl);
  426. OUTB(np, nc_gpreg, old_gpreg);
  427. return retv;
  428. }
  429. #undef SET_BIT
  430. #undef CLR_BIT
  431. #undef SET_CLK
  432. #undef CLR_CLK
  433. /*
  434. * Try reading Symbios NVRAM.
  435. * Return 0 if OK.
  436. */
  437. static int sym_read_Symbios_nvram(struct sym_device *np, Symbios_nvram *nvram)
  438. {
  439. static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
  440. u_char *data = (u_char *) nvram;
  441. int len = sizeof(*nvram);
  442. u_short csum;
  443. int x;
  444. /* probe the 24c16 and read the SYMBIOS 24c16 area */
  445. if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
  446. return 1;
  447. /* check valid NVRAM signature, verify byte count and checksum */
  448. if (nvram->type != 0 ||
  449. memcmp(nvram->trailer, Symbios_trailer, 6) ||
  450. nvram->byte_count != len - 12)
  451. return 1;
  452. /* verify checksum */
  453. for (x = 6, csum = 0; x < len - 6; x++)
  454. csum += data[x];
  455. if (csum != nvram->checksum)
  456. return 1;
  457. return 0;
  458. }
  459. /*
  460. * 93C46 EEPROM reading.
  461. *
  462. * GPIO0 - data in
  463. * GPIO1 - data out
  464. * GPIO2 - clock
  465. * GPIO4 - chip select
  466. *
  467. * Used by Tekram.
  468. */
  469. /*
  470. * Pulse clock bit in GPIO0
  471. */
  472. static void T93C46_Clk(struct sym_device *np, u_char *gpreg)
  473. {
  474. OUTB(np, nc_gpreg, *gpreg | 0x04);
  475. INB(np, nc_mbox1);
  476. udelay(2);
  477. OUTB(np, nc_gpreg, *gpreg);
  478. }
  479. /*
  480. * Read bit from NVRAM
  481. */
  482. static void T93C46_Read_Bit(struct sym_device *np, u_char *read_bit, u_char *gpreg)
  483. {
  484. udelay(2);
  485. T93C46_Clk(np, gpreg);
  486. *read_bit = INB(np, nc_gpreg);
  487. }
  488. /*
  489. * Write bit to GPIO0
  490. */
  491. static void T93C46_Write_Bit(struct sym_device *np, u_char write_bit, u_char *gpreg)
  492. {
  493. if (write_bit & 0x01)
  494. *gpreg |= 0x02;
  495. else
  496. *gpreg &= 0xfd;
  497. *gpreg |= 0x10;
  498. OUTB(np, nc_gpreg, *gpreg);
  499. INB(np, nc_mbox1);
  500. udelay(2);
  501. T93C46_Clk(np, gpreg);
  502. }
  503. /*
  504. * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
  505. */
  506. static void T93C46_Stop(struct sym_device *np, u_char *gpreg)
  507. {
  508. *gpreg &= 0xef;
  509. OUTB(np, nc_gpreg, *gpreg);
  510. INB(np, nc_mbox1);
  511. udelay(2);
  512. T93C46_Clk(np, gpreg);
  513. }
  514. /*
  515. * Send read command and address to NVRAM
  516. */
  517. static void T93C46_Send_Command(struct sym_device *np, u_short write_data,
  518. u_char *read_bit, u_char *gpreg)
  519. {
  520. int x;
  521. /* send 9 bits, start bit (1), command (2), address (6) */
  522. for (x = 0; x < 9; x++)
  523. T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
  524. *read_bit = INB(np, nc_gpreg);
  525. }
  526. /*
  527. * READ 2 bytes from the NVRAM
  528. */
  529. static void T93C46_Read_Word(struct sym_device *np,
  530. unsigned short *nvram_data, unsigned char *gpreg)
  531. {
  532. int x;
  533. u_char read_bit;
  534. *nvram_data = 0;
  535. for (x = 0; x < 16; x++) {
  536. T93C46_Read_Bit(np, &read_bit, gpreg);
  537. if (read_bit & 0x01)
  538. *nvram_data |= (0x01 << (15 - x));
  539. else
  540. *nvram_data &= ~(0x01 << (15 - x));
  541. }
  542. }
  543. /*
  544. * Read Tekram NvRAM data.
  545. */
  546. static int T93C46_Read_Data(struct sym_device *np, unsigned short *data,
  547. int len, unsigned char *gpreg)
  548. {
  549. int x;
  550. for (x = 0; x < len; x++) {
  551. unsigned char read_bit;
  552. /* output read command and address */
  553. T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
  554. if (read_bit & 0x01)
  555. return 1; /* Bad */
  556. T93C46_Read_Word(np, &data[x], gpreg);
  557. T93C46_Stop(np, gpreg);
  558. }
  559. return 0;
  560. }
  561. /*
  562. * Try reading 93C46 Tekram NVRAM.
  563. */
  564. static int sym_read_T93C46_nvram(struct sym_device *np, Tekram_nvram *nvram)
  565. {
  566. u_char gpcntl, gpreg;
  567. u_char old_gpcntl, old_gpreg;
  568. int retv;
  569. /* save current state of GPCNTL and GPREG */
  570. old_gpreg = INB(np, nc_gpreg);
  571. old_gpcntl = INB(np, nc_gpcntl);
  572. /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
  573. 1/2/4 out */
  574. gpreg = old_gpreg & 0xe9;
  575. OUTB(np, nc_gpreg, gpreg);
  576. gpcntl = (old_gpcntl & 0xe9) | 0x09;
  577. OUTB(np, nc_gpcntl, gpcntl);
  578. /* input all of NVRAM, 64 words */
  579. retv = T93C46_Read_Data(np, (u_short *) nvram,
  580. sizeof(*nvram) / sizeof(short), &gpreg);
  581. /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
  582. OUTB(np, nc_gpcntl, old_gpcntl);
  583. OUTB(np, nc_gpreg, old_gpreg);
  584. return retv;
  585. }
  586. /*
  587. * Try reading Tekram NVRAM.
  588. * Return 0 if OK.
  589. */
  590. static int sym_read_Tekram_nvram (struct sym_device *np, Tekram_nvram *nvram)
  591. {
  592. u_char *data = (u_char *) nvram;
  593. int len = sizeof(*nvram);
  594. u_short csum;
  595. int x;
  596. switch (np->pdev->device) {
  597. case PCI_DEVICE_ID_NCR_53C885:
  598. case PCI_DEVICE_ID_NCR_53C895:
  599. case PCI_DEVICE_ID_NCR_53C896:
  600. x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
  601. data, len);
  602. break;
  603. case PCI_DEVICE_ID_NCR_53C875:
  604. x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
  605. data, len);
  606. if (!x)
  607. break;
  608. fallthrough;
  609. default:
  610. x = sym_read_T93C46_nvram(np, nvram);
  611. break;
  612. }
  613. if (x)
  614. return 1;
  615. /* verify checksum */
  616. for (x = 0, csum = 0; x < len - 1; x += 2)
  617. csum += data[x] + (data[x+1] << 8);
  618. if (csum != 0x1234)
  619. return 1;
  620. return 0;
  621. }
  622. #ifdef CONFIG_PARISC
  623. /*
  624. * Host firmware (PDC) keeps a table for altering SCSI capabilities.
  625. * Many newer machines export one channel of 53c896 chip as SE, 50-pin HD.
  626. * Also used for Multi-initiator SCSI clusters to set the SCSI Initiator ID.
  627. */
  628. static int sym_read_parisc_pdc(struct sym_device *np, struct pdc_initiator *pdc)
  629. {
  630. struct hardware_path hwpath;
  631. get_pci_node_path(np->pdev, &hwpath);
  632. if (!pdc_get_initiator(&hwpath, pdc))
  633. return 0;
  634. return SYM_PARISC_PDC;
  635. }
  636. #else
  637. static inline int sym_read_parisc_pdc(struct sym_device *np,
  638. struct pdc_initiator *x)
  639. {
  640. return 0;
  641. }
  642. #endif
  643. /*
  644. * Try reading Symbios or Tekram NVRAM
  645. */
  646. int sym_read_nvram(struct sym_device *np, struct sym_nvram *nvp)
  647. {
  648. if (!sym_read_Symbios_nvram(np, &nvp->data.Symbios)) {
  649. nvp->type = SYM_SYMBIOS_NVRAM;
  650. sym_display_Symbios_nvram(np, &nvp->data.Symbios);
  651. } else if (!sym_read_Tekram_nvram(np, &nvp->data.Tekram)) {
  652. nvp->type = SYM_TEKRAM_NVRAM;
  653. sym_display_Tekram_nvram(np, &nvp->data.Tekram);
  654. } else {
  655. nvp->type = sym_read_parisc_pdc(np, &nvp->data.parisc);
  656. }
  657. return nvp->type;
  658. }
  659. char *sym_nvram_type(struct sym_nvram *nvp)
  660. {
  661. switch (nvp->type) {
  662. case SYM_SYMBIOS_NVRAM:
  663. return "Symbios NVRAM";
  664. case SYM_TEKRAM_NVRAM:
  665. return "Tekram NVRAM";
  666. case SYM_PARISC_PDC:
  667. return "PA-RISC Firmware";
  668. default:
  669. return "No NVRAM";
  670. }
  671. }