cycx_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * cycx_drv.c Cyclom 2X Support Module.
  3. *
  4. * This module is a library of common hardware specific
  5. * functions used by the Cyclades Cyclom 2X sync card.
  6. *
  7. * Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  8. *
  9. * Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo
  10. *
  11. * Based on sdladrv.c by Gene Kozin <genek@compuserve.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. * ============================================================================
  18. * 1999/11/11 acme set_current_state(TASK_INTERRUPTIBLE), code
  19. * cleanup
  20. * 1999/11/08 acme init_cyc2x deleted, doing nothing
  21. * 1999/11/06 acme back to read[bw], write[bw] and memcpy_to and
  22. * fromio to use dpmbase ioremaped
  23. * 1999/10/26 acme use isa_read[bw], isa_write[bw] & isa_memcpy_to
  24. * & fromio
  25. * 1999/10/23 acme cleanup to only supports cyclom2x: all the other
  26. * boards are no longer manufactured by cyclades,
  27. * if someone wants to support them... be my guest!
  28. * 1999/05/28 acme cycx_intack & cycx_intde gone for good
  29. * 1999/05/18 acme lots of unlogged work, submitting to Linus...
  30. * 1999/01/03 acme more judicious use of data types
  31. * 1999/01/03 acme judicious use of data types :>
  32. * cycx_inten trying to reset pending interrupts
  33. * from cyclom 2x - I think this isn't the way to
  34. * go, but for now...
  35. * 1999/01/02 acme cycx_intack ok, I think there's nothing to do
  36. * to ack an int in cycx_drv.c, only handle it in
  37. * cyx_isr (or in the other protocols: cyp_isr,
  38. * cyf_isr, when they get implemented.
  39. * Dec 31, 1998 acme cycx_data_boot & cycx_code_boot fixed, crossing
  40. * fingers to see x25_configure in cycx_x25.c
  41. * work... :)
  42. * Dec 26, 1998 acme load implementation fixed, seems to work! :)
  43. * cycx_2x_dpmbase_options with all the possible
  44. * DPM addresses (20).
  45. * cycx_intr implemented (test this!)
  46. * general code cleanup
  47. * Dec 8, 1998 Ivan Passos Cyclom-2X firmware load implementation.
  48. * Aug 8, 1998 acme Initial version.
  49. */
  50. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  51. #include <linux/init.h> /* __init */
  52. #include <linux/module.h>
  53. #include <linux/kernel.h> /* printk(), and other useful stuff */
  54. #include <linux/stddef.h> /* offsetof(), etc. */
  55. #include <linux/errno.h> /* return codes */
  56. #include <linux/cycx_drv.h> /* API definitions */
  57. #include <linux/cycx_cfm.h> /* CYCX firmware module definitions */
  58. #include <linux/delay.h> /* udelay, msleep_interruptible */
  59. #include <asm/io.h> /* read[wl], write[wl], ioremap, iounmap */
  60. #define MOD_VERSION 0
  61. #define MOD_RELEASE 6
  62. MODULE_AUTHOR("Arnaldo Carvalho de Melo");
  63. MODULE_DESCRIPTION("Cyclom 2x Sync Card Driver");
  64. MODULE_LICENSE("GPL");
  65. /* Hardware-specific functions */
  66. static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len);
  67. static void cycx_bootcfg(struct cycx_hw *hw);
  68. static int reset_cyc2x(void __iomem *addr);
  69. static int detect_cyc2x(void __iomem *addr);
  70. /* Miscellaneous functions */
  71. static int get_option_index(const long *optlist, long optval);
  72. static u16 checksum(u8 *buf, u32 len);
  73. #define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
  74. /* Global Data */
  75. /* private data */
  76. static const char fullname[] = "Cyclom 2X Support Module";
  77. static const char copyright[] =
  78. "(c) 1998-2003 Arnaldo Carvalho de Melo <acme@conectiva.com.br>";
  79. /* Hardware configuration options.
  80. * These are arrays of configuration options used by verification routines.
  81. * The first element of each array is its size (i.e. number of options).
  82. */
  83. static const long cyc2x_dpmbase_options[] = {
  84. 20,
  85. 0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
  86. 0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
  87. 0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
  88. };
  89. static const long cycx_2x_irq_options[] = { 7, 3, 5, 9, 10, 11, 12, 15 };
  90. /* Kernel Loadable Module Entry Points */
  91. /* Module 'insert' entry point.
  92. * o print announcement
  93. * o initialize static data
  94. *
  95. * Return: 0 Ok
  96. * < 0 error.
  97. * Context: process */
  98. static int __init cycx_drv_init(void)
  99. {
  100. pr_info("%s v%u.%u %s\n",
  101. fullname, MOD_VERSION, MOD_RELEASE, copyright);
  102. return 0;
  103. }
  104. /* Module 'remove' entry point.
  105. * o release all remaining system resources */
  106. static void cycx_drv_cleanup(void)
  107. {
  108. }
  109. /* Kernel APIs */
  110. /* Set up adapter.
  111. * o detect adapter type
  112. * o verify hardware configuration options
  113. * o check for hardware conflicts
  114. * o set up adapter shared memory
  115. * o test adapter memory
  116. * o load firmware
  117. * Return: 0 ok.
  118. * < 0 error */
  119. EXPORT_SYMBOL(cycx_setup);
  120. int cycx_setup(struct cycx_hw *hw, void *cfm, u32 len, unsigned long dpmbase)
  121. {
  122. int err;
  123. /* Verify IRQ configuration options */
  124. if (!get_option_index(cycx_2x_irq_options, hw->irq)) {
  125. pr_err("IRQ %d is invalid!\n", hw->irq);
  126. return -EINVAL;
  127. }
  128. /* Setup adapter dual-port memory window and test memory */
  129. if (!dpmbase) {
  130. pr_err("you must specify the dpm address!\n");
  131. return -EINVAL;
  132. } else if (!get_option_index(cyc2x_dpmbase_options, dpmbase)) {
  133. pr_err("memory address 0x%lX is invalid!\n", dpmbase);
  134. return -EINVAL;
  135. }
  136. hw->dpmbase = ioremap(dpmbase, CYCX_WINDOWSIZE);
  137. hw->dpmsize = CYCX_WINDOWSIZE;
  138. if (!detect_cyc2x(hw->dpmbase)) {
  139. pr_err("adapter Cyclom 2X not found at address 0x%lX!\n",
  140. dpmbase);
  141. return -EINVAL;
  142. }
  143. pr_info("found Cyclom 2X card at address 0x%lX\n", dpmbase);
  144. /* Load firmware. If loader fails then shut down adapter */
  145. err = load_cyc2x(hw, cfm, len);
  146. if (err)
  147. cycx_down(hw); /* shutdown adapter */
  148. return err;
  149. }
  150. EXPORT_SYMBOL(cycx_down);
  151. int cycx_down(struct cycx_hw *hw)
  152. {
  153. iounmap(hw->dpmbase);
  154. return 0;
  155. }
  156. /* Enable interrupt generation. */
  157. static void cycx_inten(struct cycx_hw *hw)
  158. {
  159. writeb(0, hw->dpmbase);
  160. }
  161. /* Generate an interrupt to adapter's CPU. */
  162. EXPORT_SYMBOL(cycx_intr);
  163. void cycx_intr(struct cycx_hw *hw)
  164. {
  165. writew(0, hw->dpmbase + GEN_CYCX_INTR);
  166. }
  167. /* Execute Adapter Command.
  168. * o Set exec flag.
  169. * o Busy-wait until flag is reset. */
  170. EXPORT_SYMBOL(cycx_exec);
  171. int cycx_exec(void __iomem *addr)
  172. {
  173. u16 i = 0;
  174. /* wait till addr content is zeroed */
  175. while (readw(addr)) {
  176. udelay(1000);
  177. if (++i > 50)
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. /* Read absolute adapter memory.
  183. * Transfer data from adapter's memory to data buffer. */
  184. EXPORT_SYMBOL(cycx_peek);
  185. int cycx_peek(struct cycx_hw *hw, u32 addr, void *buf, u32 len)
  186. {
  187. if (len == 1)
  188. *(u8*)buf = readb(hw->dpmbase + addr);
  189. else
  190. memcpy_fromio(buf, hw->dpmbase + addr, len);
  191. return 0;
  192. }
  193. /* Write Absolute Adapter Memory.
  194. * Transfer data from data buffer to adapter's memory. */
  195. EXPORT_SYMBOL(cycx_poke);
  196. int cycx_poke(struct cycx_hw *hw, u32 addr, void *buf, u32 len)
  197. {
  198. if (len == 1)
  199. writeb(*(u8*)buf, hw->dpmbase + addr);
  200. else
  201. memcpy_toio(hw->dpmbase + addr, buf, len);
  202. return 0;
  203. }
  204. /* Hardware-Specific Functions */
  205. /* Load Aux Routines */
  206. /* Reset board hardware.
  207. return 1 if memory exists at addr and 0 if not. */
  208. static int memory_exists(void __iomem *addr)
  209. {
  210. int tries = 0;
  211. for (; tries < 3 ; tries++) {
  212. writew(TEST_PATTERN, addr + 0x10);
  213. if (readw(addr + 0x10) == TEST_PATTERN)
  214. if (readw(addr + 0x10) == TEST_PATTERN)
  215. return 1;
  216. msleep_interruptible(1 * 1000);
  217. }
  218. return 0;
  219. }
  220. /* Load reset code. */
  221. static void reset_load(void __iomem *addr, u8 *buffer, u32 cnt)
  222. {
  223. void __iomem *pt_code = addr + RESET_OFFSET;
  224. u16 i; /*, j; */
  225. for (i = 0 ; i < cnt ; i++) {
  226. /* for (j = 0 ; j < 50 ; j++); Delay - FIXME busy waiting... */
  227. writeb(*buffer++, pt_code++);
  228. }
  229. }
  230. /* Load buffer using boot interface.
  231. * o copy data from buffer to Cyclom-X memory
  232. * o wait for reset code to copy it to right portion of memory */
  233. static int buffer_load(void __iomem *addr, u8 *buffer, u32 cnt)
  234. {
  235. memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
  236. writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
  237. return wait_cyc(addr);
  238. }
  239. /* Set up entry point and kick start Cyclom-X CPU. */
  240. static void cycx_start(void __iomem *addr)
  241. {
  242. /* put in 0x30 offset the jump instruction to the code entry point */
  243. writeb(0xea, addr + 0x30);
  244. writeb(0x00, addr + 0x31);
  245. writeb(0xc4, addr + 0x32);
  246. writeb(0x00, addr + 0x33);
  247. writeb(0x00, addr + 0x34);
  248. /* cmd to start executing code */
  249. writew(GEN_START, addr + CMD_OFFSET);
  250. }
  251. /* Load and boot reset code. */
  252. static void cycx_reset_boot(void __iomem *addr, u8 *code, u32 len)
  253. {
  254. void __iomem *pt_start = addr + START_OFFSET;
  255. writeb(0xea, pt_start++); /* jmp to f000:3f00 */
  256. writeb(0x00, pt_start++);
  257. writeb(0xfc, pt_start++);
  258. writeb(0x00, pt_start++);
  259. writeb(0xf0, pt_start);
  260. reset_load(addr, code, len);
  261. /* 80186 was in hold, go */
  262. writeb(0, addr + START_CPU);
  263. msleep_interruptible(1 * 1000);
  264. }
  265. /* Load data.bin file through boot (reset) interface. */
  266. static int cycx_data_boot(void __iomem *addr, u8 *code, u32 len)
  267. {
  268. void __iomem *pt_boot_cmd = addr + CMD_OFFSET;
  269. u32 i;
  270. /* boot buffer length */
  271. writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
  272. writew(GEN_DEFPAR, pt_boot_cmd);
  273. if (wait_cyc(addr) < 0)
  274. return -1;
  275. writew(0, pt_boot_cmd + sizeof(u16));
  276. writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
  277. writew(GEN_SET_SEG, pt_boot_cmd);
  278. if (wait_cyc(addr) < 0)
  279. return -1;
  280. for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
  281. if (buffer_load(addr, code + i,
  282. min_t(u32, CFM_LOAD_BUFSZ, (len - i))) < 0) {
  283. pr_err("Error !!\n");
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. /* Load code.bin file through boot (reset) interface. */
  289. static int cycx_code_boot(void __iomem *addr, u8 *code, u32 len)
  290. {
  291. void __iomem *pt_boot_cmd = addr + CMD_OFFSET;
  292. u32 i;
  293. /* boot buffer length */
  294. writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
  295. writew(GEN_DEFPAR, pt_boot_cmd);
  296. if (wait_cyc(addr) < 0)
  297. return -1;
  298. writew(0x0000, pt_boot_cmd + sizeof(u16));
  299. writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
  300. writew(GEN_SET_SEG, pt_boot_cmd);
  301. if (wait_cyc(addr) < 0)
  302. return -1;
  303. for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
  304. if (buffer_load(addr, code + i,
  305. min_t(u32, CFM_LOAD_BUFSZ, (len - i)))) {
  306. pr_err("Error !!\n");
  307. return -1;
  308. }
  309. return 0;
  310. }
  311. /* Load adapter from the memory image of the CYCX firmware module.
  312. * o verify firmware integrity and compatibility
  313. * o start adapter up */
  314. static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len)
  315. {
  316. int i, j;
  317. struct cycx_fw_header *img_hdr;
  318. u8 *reset_image,
  319. *data_image,
  320. *code_image;
  321. void __iomem *pt_cycld = hw->dpmbase + 0x400;
  322. u16 cksum;
  323. /* Announce */
  324. pr_info("firmware signature=\"%s\"\n", cfm->signature);
  325. /* Verify firmware signature */
  326. if (strcmp(cfm->signature, CFM_SIGNATURE)) {
  327. pr_err("load_cyc2x: not Cyclom-2X firmware!\n");
  328. return -EINVAL;
  329. }
  330. pr_info("firmware version=%u\n", cfm->version);
  331. /* Verify firmware module format version */
  332. if (cfm->version != CFM_VERSION) {
  333. pr_err("%s: firmware format %u rejected! Expecting %u.\n",
  334. __func__, cfm->version, CFM_VERSION);
  335. return -EINVAL;
  336. }
  337. /* Verify firmware module length and checksum */
  338. cksum = checksum((u8*)&cfm->info, sizeof(struct cycx_fw_info) +
  339. cfm->info.codesize);
  340. /*
  341. FIXME cfm->info.codesize is off by 2
  342. if (((len - sizeof(struct cycx_firmware) - 1) != cfm->info.codesize) ||
  343. */
  344. if (cksum != cfm->checksum) {
  345. pr_err("%s: firmware corrupted!\n", __func__);
  346. pr_err(" cdsize = 0x%x (expected 0x%lx)\n",
  347. len - (int)sizeof(struct cycx_firmware) - 1,
  348. cfm->info.codesize);
  349. pr_err(" chksum = 0x%x (expected 0x%x)\n",
  350. cksum, cfm->checksum);
  351. return -EINVAL;
  352. }
  353. /* If everything is ok, set reset, data and code pointers */
  354. img_hdr = (struct cycx_fw_header *)&cfm->image;
  355. #ifdef FIRMWARE_DEBUG
  356. pr_info("%s: image sizes\n", __func__);
  357. pr_info(" reset=%lu\n", img_hdr->reset_size);
  358. pr_info(" data=%lu\n", img_hdr->data_size);
  359. pr_info(" code=%lu\n", img_hdr->code_size);
  360. #endif
  361. reset_image = ((u8 *)img_hdr) + sizeof(struct cycx_fw_header);
  362. data_image = reset_image + img_hdr->reset_size;
  363. code_image = data_image + img_hdr->data_size;
  364. /*---- Start load ----*/
  365. /* Announce */
  366. pr_info("loading firmware %s (ID=%u)...\n",
  367. cfm->descr[0] ? cfm->descr : "unknown firmware",
  368. cfm->info.codeid);
  369. for (i = 0 ; i < 5 ; i++) {
  370. /* Reset Cyclom hardware */
  371. if (!reset_cyc2x(hw->dpmbase)) {
  372. pr_err("dpm problem or board not found\n");
  373. return -EINVAL;
  374. }
  375. /* Load reset.bin */
  376. cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
  377. /* reset is waiting for boot */
  378. writew(GEN_POWER_ON, pt_cycld);
  379. msleep_interruptible(1 * 1000);
  380. for (j = 0 ; j < 3 ; j++)
  381. if (!readw(pt_cycld))
  382. goto reset_loaded;
  383. else
  384. msleep_interruptible(1 * 1000);
  385. }
  386. pr_err("reset not started\n");
  387. return -EINVAL;
  388. reset_loaded:
  389. /* Load data.bin */
  390. if (cycx_data_boot(hw->dpmbase, data_image, img_hdr->data_size)) {
  391. pr_err("cannot load data file\n");
  392. return -EINVAL;
  393. }
  394. /* Load code.bin */
  395. if (cycx_code_boot(hw->dpmbase, code_image, img_hdr->code_size)) {
  396. pr_err("cannot load code file\n");
  397. return -EINVAL;
  398. }
  399. /* Prepare boot-time configuration data */
  400. cycx_bootcfg(hw);
  401. /* kick-off CPU */
  402. cycx_start(hw->dpmbase);
  403. /* Arthur Ganzert's tip: wait a while after the firmware loading...
  404. seg abr 26 17:17:12 EST 1999 - acme */
  405. msleep_interruptible(7 * 1000);
  406. pr_info("firmware loaded!\n");
  407. /* enable interrupts */
  408. cycx_inten(hw);
  409. return 0;
  410. }
  411. /* Prepare boot-time firmware configuration data.
  412. * o initialize configuration data area
  413. From async.doc - V_3.4.0 - 07/18/1994
  414. - As of now, only static buffers are available to the user.
  415. So, the bit VD_RXDIRC must be set in 'valid'. That means that user
  416. wants to use the static transmission and reception buffers. */
  417. static void cycx_bootcfg(struct cycx_hw *hw)
  418. {
  419. /* use fixed buffers */
  420. writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET);
  421. }
  422. /* Detect Cyclom 2x adapter.
  423. * Following tests are used to detect Cyclom 2x adapter:
  424. * to be completed based on the tests done below
  425. * Return 1 if detected o.k. or 0 if failed.
  426. * Note: This test is destructive! Adapter will be left in shutdown
  427. * state after the test. */
  428. static int detect_cyc2x(void __iomem *addr)
  429. {
  430. reset_cyc2x(addr);
  431. return memory_exists(addr);
  432. }
  433. /* Miscellaneous */
  434. /* Get option's index into the options list.
  435. * Return option's index (1 .. N) or zero if option is invalid. */
  436. static int get_option_index(const long *optlist, long optval)
  437. {
  438. int i = 1;
  439. for (; i <= optlist[0]; ++i)
  440. if (optlist[i] == optval)
  441. return i;
  442. return 0;
  443. }
  444. /* Reset adapter's CPU. */
  445. static int reset_cyc2x(void __iomem *addr)
  446. {
  447. writeb(0, addr + RST_ENABLE);
  448. msleep_interruptible(2 * 1000);
  449. writeb(0, addr + RST_DISABLE);
  450. msleep_interruptible(2 * 1000);
  451. return memory_exists(addr);
  452. }
  453. /* Calculate 16-bit CRC using CCITT polynomial. */
  454. static u16 checksum(u8 *buf, u32 len)
  455. {
  456. u16 crc = 0;
  457. u16 mask, flag;
  458. for (; len; --len, ++buf)
  459. for (mask = 0x80; mask; mask >>= 1) {
  460. flag = (crc & 0x8000);
  461. crc <<= 1;
  462. crc |= ((*buf & mask) ? 1 : 0);
  463. if (flag)
  464. crc ^= 0x1021;
  465. }
  466. return crc;
  467. }
  468. module_init(cycx_drv_init);
  469. module_exit(cycx_drv_cleanup);
  470. /* End */